FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/packet.c
Date: 2024-09-07 18:49:03
Exec Total Coverage
Lines: 340 448 75.9%
Functions: 33 36 91.7%
Branches: 146 240 60.8%

Line Branch Exec Source
1 /*
2 * AVPacket functions for libavcodec
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <string.h>
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/avutil.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/rational.h"
30
31 #include "defs.h"
32 #include "packet.h"
33 #include "packet_internal.h"
34
35 #if FF_API_INIT_PACKET
36 1333974 void av_init_packet(AVPacket *pkt)
37 {
38 1333974 pkt->pts = AV_NOPTS_VALUE;
39 1333974 pkt->dts = AV_NOPTS_VALUE;
40 1333974 pkt->pos = -1;
41 1333974 pkt->duration = 0;
42 1333974 pkt->flags = 0;
43 1333974 pkt->stream_index = 0;
44 1333974 pkt->buf = NULL;
45 1333974 pkt->side_data = NULL;
46 1333974 pkt->side_data_elems = 0;
47 1333974 pkt->opaque = NULL;
48 1333974 pkt->opaque_ref = NULL;
49 1333974 pkt->time_base = av_make_q(0, 1);
50 1333974 }
51 #endif
52
53 11819206 static void get_packet_defaults(AVPacket *pkt)
54 {
55 11819206 memset(pkt, 0, sizeof(*pkt));
56
57 11819206 pkt->pts = AV_NOPTS_VALUE;
58 11819206 pkt->dts = AV_NOPTS_VALUE;
59 11819206 pkt->pos = -1;
60 11819206 pkt->time_base = av_make_q(0, 1);
61 11819206 }
62
63 263275 AVPacket *av_packet_alloc(void)
64 {
65 263275 AVPacket *pkt = av_malloc(sizeof(AVPacket));
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 263275 times.
263275 if (!pkt)
67 return pkt;
68
69 263275 get_packet_defaults(pkt);
70
71 263275 return pkt;
72 }
73
74 331789 void av_packet_free(AVPacket **pkt)
75 {
76
3/4
✓ Branch 0 taken 331789 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 68514 times.
✓ Branch 3 taken 263275 times.
331789 if (!pkt || !*pkt)
77 68514 return;
78
79 263275 av_packet_unref(*pkt);
80 263275 av_freep(pkt);
81 }
82
83 867910 static int packet_alloc(AVBufferRef **buf, int size)
84 {
85 int ret;
86
3/4
✓ Branch 0 taken 867910 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 867909 times.
867910 if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
87 1 return AVERROR(EINVAL);
88
89 867909 ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 867909 times.
867909 if (ret < 0)
91 return ret;
92
93 867909 memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
94
95 867909 return 0;
96 }
97
98 746941 int av_new_packet(AVPacket *pkt, int size)
99 {
100 746941 AVBufferRef *buf = NULL;
101 746941 int ret = packet_alloc(&buf, size);
102
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 746940 times.
746941 if (ret < 0)
103 1 return ret;
104
105 746940 get_packet_defaults(pkt);
106 746940 pkt->buf = buf;
107 746940 pkt->data = buf->data;
108 746940 pkt->size = size;
109
110 746940 return 0;
111 }
112
113 591114 void av_shrink_packet(AVPacket *pkt, int size)
114 {
115
2/2
✓ Branch 0 taken 585853 times.
✓ Branch 1 taken 5261 times.
591114 if (pkt->size <= size)
116 585853 return;
117 5261 pkt->size = size;
118 5261 memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
119 }
120
121 276871 int av_grow_packet(AVPacket *pkt, int grow_by)
122 {
123 int new_size;
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 276871 times.
276871 av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
125 276871 if ((unsigned)grow_by >
126
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 276870 times.
276871 INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
127 1 return AVERROR(ENOMEM);
128
129 276870 new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
130
2/2
✓ Branch 0 taken 13772 times.
✓ Branch 1 taken 263098 times.
276870 if (pkt->buf) {
131 size_t data_offset;
132 13772 uint8_t *old_data = pkt->data;
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13772 times.
13772 if (pkt->data == NULL) {
134 data_offset = 0;
135 pkt->data = pkt->buf->data;
136 } else {
137 13772 data_offset = pkt->data - pkt->buf->data;
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13772 times.
13772 if (data_offset > INT_MAX - new_size)
139 return AVERROR(ENOMEM);
140 }
141
142
3/4
✓ Branch 0 taken 2985 times.
✓ Branch 1 taken 10787 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2985 times.
16757 if (new_size + data_offset > pkt->buf->size ||
143 2985 !av_buffer_is_writable(pkt->buf)) {
144 int ret;
145
146 // allocate slightly more than requested to avoid excessive
147 // reallocations
148
1/2
✓ Branch 0 taken 10787 times.
✗ Branch 1 not taken.
10787 if (new_size + data_offset < INT_MAX - new_size/16)
149 10787 new_size += new_size/16;
150
151 10787 ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10787 times.
10787 if (ret < 0) {
153 pkt->data = old_data;
154 return ret;
155 }
156 10787 pkt->data = pkt->buf->data + data_offset;
157 }
158 } else {
159 263098 pkt->buf = av_buffer_alloc(new_size);
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 263098 times.
263098 if (!pkt->buf)
161 return AVERROR(ENOMEM);
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 263098 times.
263098 if (pkt->size > 0)
163 memcpy(pkt->buf->data, pkt->data, pkt->size);
164 263098 pkt->data = pkt->buf->data;
165 }
166 276870 pkt->size += grow_by;
167 276870 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
168
169 276870 return 0;
170 }
171
172 3 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
173 {
174
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
175 1 return AVERROR(EINVAL);
176
177 2 pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
178 av_buffer_default_free, NULL, 0);
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!pkt->buf)
180 return AVERROR(ENOMEM);
181
182 2 pkt->data = data;
183 2 pkt->size = size;
184
185 2 return 0;
186 }
187
188 7399206 void av_packet_free_side_data(AVPacket *pkt)
189 {
190 int i;
191
2/2
✓ Branch 0 taken 55156 times.
✓ Branch 1 taken 7399206 times.
7454362 for (i = 0; i < pkt->side_data_elems; i++)
192 55156 av_freep(&pkt->side_data[i].data);
193 7399206 av_freep(&pkt->side_data);
194 7399206 pkt->side_data_elems = 0;
195 7399206 }
196
197 55158 int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
198 uint8_t *data, size_t size)
199 {
200 AVPacketSideData *tmp;
201 55158 int i, elems = pkt->side_data_elems;
202
203
2/2
✓ Branch 0 taken 221 times.
✓ Branch 1 taken 55156 times.
55377 for (i = 0; i < elems; i++) {
204 221 AVPacketSideData *sd = &pkt->side_data[i];
205
206
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 219 times.
221 if (sd->type == type) {
207 2 av_free(sd->data);
208 2 sd->data = data;
209 2 sd->size = size;
210 2 return 0;
211 }
212 }
213
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55156 times.
55156 if ((unsigned)elems + 1 > AV_PKT_DATA_NB)
215 return AVERROR(ERANGE);
216
217 55156 tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55156 times.
55156 if (!tmp)
219 return AVERROR(ENOMEM);
220
221 55156 pkt->side_data = tmp;
222 55156 pkt->side_data[elems].data = data;
223 55156 pkt->side_data[elems].size = size;
224 55156 pkt->side_data[elems].type = type;
225 55156 pkt->side_data_elems++;
226
227 55156 return 0;
228 }
229
230
231 53732 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
232 size_t size)
233 {
234 int ret;
235 uint8_t *data;
236
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53732 times.
53732 if (size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
238 return NULL;
239 53732 data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53732 times.
53732 if (!data)
241 return NULL;
242
243 53732 ret = av_packet_add_side_data(pkt, type, data, size);
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53732 times.
53732 if (ret < 0) {
245 av_freep(&data);
246 return NULL;
247 }
248
249 53732 return data;
250 }
251
252 6750352 uint8_t *av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
253 size_t *size)
254 {
255 int i;
256
257
2/2
✓ Branch 0 taken 102925 times.
✓ Branch 1 taken 6737821 times.
6840746 for (i = 0; i < pkt->side_data_elems; i++) {
258
2/2
✓ Branch 0 taken 12531 times.
✓ Branch 1 taken 90394 times.
102925 if (pkt->side_data[i].type == type) {
259
2/2
✓ Branch 0 taken 1198 times.
✓ Branch 1 taken 11333 times.
12531 if (size)
260 1198 *size = pkt->side_data[i].size;
261 12531 return pkt->side_data[i].data;
262 }
263 }
264
2/2
✓ Branch 0 taken 6602935 times.
✓ Branch 1 taken 134886 times.
6737821 if (size)
265 6602935 *size = 0;
266 6737821 return NULL;
267 }
268
269 1602 const char *av_packet_side_data_name(enum AVPacketSideDataType type)
270 {
271
16/38
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 9 times.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 13 times.
✓ Branch 11 taken 20 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 162 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 8 times.
✓ Branch 17 taken 16 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 1338 times.
✓ Branch 20 taken 6 times.
✓ Branch 21 taken 5 times.
✓ Branch 22 taken 4 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✓ Branch 29 taken 2 times.
✗ Branch 30 not taken.
✓ Branch 31 taken 2 times.
✓ Branch 32 taken 2 times.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
1602 switch(type) {
272 case AV_PKT_DATA_PALETTE: return "Palette";
273 5 case AV_PKT_DATA_NEW_EXTRADATA: return "New Extradata";
274 case AV_PKT_DATA_PARAM_CHANGE: return "Param Change";
275 case AV_PKT_DATA_H263_MB_INFO: return "H263 MB Info";
276 case AV_PKT_DATA_REPLAYGAIN: return "Replay Gain";
277 6 case AV_PKT_DATA_DISPLAYMATRIX: return "Display Matrix";
278 9 case AV_PKT_DATA_STEREO3D: return "Stereo 3D";
279 4 case AV_PKT_DATA_AUDIO_SERVICE_TYPE: return "Audio Service Type";
280 case AV_PKT_DATA_QUALITY_STATS: return "Quality stats";
281 case AV_PKT_DATA_FALLBACK_TRACK: return "Fallback track";
282 13 case AV_PKT_DATA_CPB_PROPERTIES: return "CPB properties";
283 20 case AV_PKT_DATA_SKIP_SAMPLES: return "Skip Samples";
284 case AV_PKT_DATA_JP_DUALMONO: return "JP Dual Mono";
285 162 case AV_PKT_DATA_STRINGS_METADATA: return "Strings Metadata";
286 case AV_PKT_DATA_SUBTITLE_POSITION: return "Subtitle Position";
287 case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL: return "Matroska BlockAdditional";
288 8 case AV_PKT_DATA_WEBVTT_IDENTIFIER: return "WebVTT ID";
289 16 case AV_PKT_DATA_WEBVTT_SETTINGS: return "WebVTT Settings";
290 case AV_PKT_DATA_METADATA_UPDATE: return "Metadata Update";
291 1338 case AV_PKT_DATA_MPEGTS_STREAM_ID: return "MPEGTS Stream ID";
292 6 case AV_PKT_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
293 5 case AV_PKT_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
294 4 case AV_PKT_DATA_SPHERICAL: return "Spherical Mapping";
295 case AV_PKT_DATA_A53_CC: return "A53 Closed Captions";
296 case AV_PKT_DATA_ENCRYPTION_INIT_INFO: return "Encryption initialization data";
297 case AV_PKT_DATA_ENCRYPTION_INFO: return "Encryption info";
298 case AV_PKT_DATA_AFD: return "Active Format Description data";
299 case AV_PKT_DATA_PRFT: return "Producer Reference Time";
300 case AV_PKT_DATA_ICC_PROFILE: return "ICC Profile";
301 2 case AV_PKT_DATA_DOVI_CONF: return "DOVI configuration record";
302 case AV_PKT_DATA_S12M_TIMECODE: return "SMPTE ST 12-1:2014 timecode";
303 2 case AV_PKT_DATA_DYNAMIC_HDR10_PLUS: return "HDR10+ Dynamic Metadata (SMPTE 2094-40)";
304 2 case AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT:return "Ambient viewing environment";
305 case AV_PKT_DATA_IAMF_MIX_GAIN_PARAM: return "IAMF Mix Gain Parameter Data";
306 case AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM: return "IAMF Demixing Info Parameter Data";
307 case AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM: return "IAMF Recon Gain Info Parameter Data";
308 case AV_PKT_DATA_FRAME_CROPPING: return "Frame Cropping";
309 }
310 return NULL;
311 }
312
313 612 uint8_t *av_packet_pack_dictionary(AVDictionary *dict, size_t *size)
314 {
315 612 uint8_t *data = NULL;
316 612 *size = 0;
317
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 if (!dict)
319 return NULL;
320
321
1/2
✓ Branch 0 taken 1224 times.
✗ Branch 1 not taken.
1224 for (int pass = 0; pass < 2; pass++) {
322 1224 const AVDictionaryEntry *t = NULL;
323 1224 size_t total_length = 0;
324
325
2/2
✓ Branch 1 taken 4978 times.
✓ Branch 2 taken 1224 times.
6202 while ((t = av_dict_iterate(dict, t))) {
326
2/2
✓ Branch 0 taken 9956 times.
✓ Branch 1 taken 4978 times.
14934 for (int i = 0; i < 2; i++) {
327
2/2
✓ Branch 0 taken 4978 times.
✓ Branch 1 taken 4978 times.
9956 const char *str = i ? t->value : t->key;
328 9956 const size_t len = strlen(str) + 1;
329
330
2/2
✓ Branch 0 taken 4978 times.
✓ Branch 1 taken 4978 times.
9956 if (pass)
331 4978 memcpy(data + total_length, str, len);
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4978 times.
4978 else if (len > SIZE_MAX - total_length)
333 return NULL;
334 9956 total_length += len;
335 }
336 }
337
2/2
✓ Branch 0 taken 612 times.
✓ Branch 1 taken 612 times.
1224 if (pass)
338 612 break;
339 612 data = av_malloc(total_length);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 if (!data)
341 return NULL;
342 612 *size = total_length;
343 }
344
345 612 return data;
346 }
347
348 401703 int av_packet_unpack_dictionary(const uint8_t *data, size_t size,
349 AVDictionary **dict)
350 {
351 const uint8_t *end;
352 int ret;
353
354
4/6
✓ Branch 0 taken 401703 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 560 times.
✓ Branch 3 taken 401143 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 560 times.
401703 if (!dict || !data || !size)
355 401143 return 0;
356 560 end = data + size;
357
2/4
✓ Branch 0 taken 560 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 560 times.
560 if (size && end[-1])
358 return AVERROR_INVALIDDATA;
359
2/2
✓ Branch 0 taken 2399 times.
✓ Branch 1 taken 560 times.
2959 while (data < end) {
360 2399 const uint8_t *key = data;
361 2399 const uint8_t *val = data + strlen(key) + 1;
362
363
2/4
✓ Branch 0 taken 2399 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2399 times.
2399 if (val >= end || !*key)
364 return AVERROR_INVALIDDATA;
365
366 2399 ret = av_dict_set(dict, key, val, 0);
367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2399 times.
2399 if (ret < 0)
368 return ret;
369 2399 data = val + strlen(val) + 1;
370 }
371
372 560 return 0;
373 }
374
375 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
376 size_t size)
377 {
378 int i;
379
380 for (i = 0; i < pkt->side_data_elems; i++) {
381 if (pkt->side_data[i].type == type) {
382 if (size > pkt->side_data[i].size)
383 return AVERROR(ENOMEM);
384 pkt->side_data[i].size = size;
385 return 0;
386 }
387 }
388 return AVERROR(ENOENT);
389 }
390
391 784857 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
392 {
393 int i, ret;
394
395 784857 dst->pts = src->pts;
396 784857 dst->dts = src->dts;
397 784857 dst->pos = src->pos;
398 784857 dst->duration = src->duration;
399 784857 dst->flags = src->flags;
400 784857 dst->stream_index = src->stream_index;
401 784857 dst->opaque = src->opaque;
402 784857 dst->time_base = src->time_base;
403 784857 dst->opaque_ref = NULL;
404 784857 dst->side_data = NULL;
405 784857 dst->side_data_elems = 0;
406
407 784857 ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 784857 times.
784857 if (ret < 0)
409 return ret;
410
411
2/2
✓ Branch 0 taken 12410 times.
✓ Branch 1 taken 784857 times.
797267 for (i = 0; i < src->side_data_elems; i++) {
412 12410 enum AVPacketSideDataType type = src->side_data[i].type;
413 12410 size_t size = src->side_data[i].size;
414 12410 uint8_t *src_data = src->side_data[i].data;
415 12410 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
416
417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12410 times.
12410 if (!dst_data) {
418 av_buffer_unref(&dst->opaque_ref);
419 av_packet_free_side_data(dst);
420 return AVERROR(ENOMEM);
421 }
422 12410 memcpy(dst_data, src_data, size);
423 }
424
425 784857 return 0;
426 }
427
428 7399206 void av_packet_unref(AVPacket *pkt)
429 {
430 7399206 av_packet_free_side_data(pkt);
431 7399206 av_buffer_unref(&pkt->opaque_ref);
432 7399206 av_buffer_unref(&pkt->buf);
433 7399206 get_packet_defaults(pkt);
434 7399206 }
435
436 389563 int av_packet_ref(AVPacket *dst, const AVPacket *src)
437 {
438 int ret;
439
440 389563 dst->buf = NULL;
441
442 389563 ret = av_packet_copy_props(dst, src);
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 389563 times.
389563 if (ret < 0)
444 goto fail;
445
446
2/2
✓ Branch 0 taken 1030 times.
✓ Branch 1 taken 388533 times.
389563 if (!src->buf) {
447 1030 ret = packet_alloc(&dst->buf, src->size);
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1030 times.
1030 if (ret < 0)
449 goto fail;
450 av_assert1(!src->size || src->data);
451
1/2
✓ Branch 0 taken 1030 times.
✗ Branch 1 not taken.
1030 if (src->size)
452 1030 memcpy(dst->buf->data, src->data, src->size);
453
454 1030 dst->data = dst->buf->data;
455 } else {
456 388533 dst->buf = av_buffer_ref(src->buf);
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 388533 times.
388533 if (!dst->buf) {
458 ret = AVERROR(ENOMEM);
459 goto fail;
460 }
461 388533 dst->data = src->data;
462 }
463
464 389563 dst->size = src->size;
465
466 389563 return 0;
467 fail:
468 av_packet_unref(dst);
469 return ret;
470 }
471
472 6 AVPacket *av_packet_clone(const AVPacket *src)
473 {
474 6 AVPacket *ret = av_packet_alloc();
475
476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!ret)
477 return ret;
478
479
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (av_packet_ref(ret, src))
480 av_packet_free(&ret);
481
482 6 return ret;
483 }
484
485 3409729 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
486 {
487 3409729 *dst = *src;
488 3409729 get_packet_defaults(src);
489 3409729 }
490
491 2013639 int av_packet_make_refcounted(AVPacket *pkt)
492 {
493 int ret;
494
495
2/2
✓ Branch 0 taken 1893700 times.
✓ Branch 1 taken 119939 times.
2013639 if (pkt->buf)
496 1893700 return 0;
497
498 119939 ret = packet_alloc(&pkt->buf, pkt->size);
499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 119939 times.
119939 if (ret < 0)
500 return ret;
501 av_assert1(!pkt->size || pkt->data);
502
2/2
✓ Branch 0 taken 118409 times.
✓ Branch 1 taken 1530 times.
119939 if (pkt->size)
503 118409 memcpy(pkt->buf->data, pkt->data, pkt->size);
504
505 119939 pkt->data = pkt->buf->data;
506
507 119939 return 0;
508 }
509
510 142 int av_packet_make_writable(AVPacket *pkt)
511 {
512 142 AVBufferRef *buf = NULL;
513 int ret;
514
515
2/4
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 142 times.
✗ Branch 4 not taken.
142 if (pkt->buf && av_buffer_is_writable(pkt->buf))
516 142 return 0;
517
518 ret = packet_alloc(&buf, pkt->size);
519 if (ret < 0)
520 return ret;
521 av_assert1(!pkt->size || pkt->data);
522 if (pkt->size)
523 memcpy(buf->data, pkt->data, pkt->size);
524
525 av_buffer_unref(&pkt->buf);
526 pkt->buf = buf;
527 pkt->data = buf->data;
528
529 return 0;
530 }
531
532 444749 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
533 {
534
2/2
✓ Branch 0 taken 429647 times.
✓ Branch 1 taken 15102 times.
444749 if (pkt->pts != AV_NOPTS_VALUE)
535 429647 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
536
2/2
✓ Branch 0 taken 444748 times.
✓ Branch 1 taken 1 times.
444749 if (pkt->dts != AV_NOPTS_VALUE)
537 444748 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
538
2/2
✓ Branch 0 taken 426606 times.
✓ Branch 1 taken 18143 times.
444749 if (pkt->duration > 0)
539 426606 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
540 444749 }
541
542 410714 int avpriv_packet_list_put(PacketList *packet_buffer,
543 AVPacket *pkt,
544 int (*copy)(AVPacket *dst, const AVPacket *src),
545 int flags)
546 {
547 410714 PacketListEntry *pktl = av_malloc(sizeof(*pktl));
548 int ret;
549
550
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410714 times.
410714 if (!pktl)
551 return AVERROR(ENOMEM);
552
553
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 410658 times.
410714 if (copy) {
554 56 get_packet_defaults(&pktl->pkt);
555 56 ret = copy(&pktl->pkt, pkt);
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (ret < 0) {
557 av_free(pktl);
558 return ret;
559 }
560 } else {
561 410658 ret = av_packet_make_refcounted(pkt);
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410658 times.
410658 if (ret < 0) {
563 av_free(pktl);
564 return ret;
565 }
566 410658 av_packet_move_ref(&pktl->pkt, pkt);
567 }
568
569 410714 pktl->next = NULL;
570
571
2/2
✓ Branch 0 taken 245306 times.
✓ Branch 1 taken 165408 times.
410714 if (packet_buffer->head)
572 245306 packet_buffer->tail->next = pktl;
573 else
574 165408 packet_buffer->head = pktl;
575
576 /* Add the packet in the buffered packet list. */
577 410714 packet_buffer->tail = pktl;
578 410714 return 0;
579 }
580
581 375613 int avpriv_packet_list_get(PacketList *pkt_buffer,
582 AVPacket *pkt)
583 {
584 375613 PacketListEntry *pktl = pkt_buffer->head;
585
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 375611 times.
375613 if (!pktl)
586 2 return AVERROR(EAGAIN);
587 375611 *pkt = pktl->pkt;
588 375611 pkt_buffer->head = pktl->next;
589
2/2
✓ Branch 0 taken 161712 times.
✓ Branch 1 taken 213899 times.
375611 if (!pkt_buffer->head)
590 161712 pkt_buffer->tail = NULL;
591 375611 av_freep(&pktl);
592 375611 return 0;
593 }
594
595 58232 void avpriv_packet_list_free(PacketList *pkt_buf)
596 {
597 58232 PacketListEntry *tmp = pkt_buf->head;
598
599
2/2
✓ Branch 0 taken 60529 times.
✓ Branch 1 taken 58232 times.
118761 while (tmp) {
600 60529 PacketListEntry *pktl = tmp;
601 60529 tmp = pktl->next;
602 60529 av_packet_unref(&pktl->pkt);
603 60529 av_freep(&pktl);
604 }
605 58232 pkt_buf->head = pkt_buf->tail = NULL;
606 58232 }
607
608 11330 int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
609 {
610 uint8_t *side_data;
611 size_t side_data_size;
612 int i;
613
614 11330 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
615
1/2
✓ Branch 0 taken 11330 times.
✗ Branch 1 not taken.
11330 if (!side_data) {
616 11330 side_data_size = 4+4+8*error_count;
617 11330 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
618 side_data_size);
619 }
620
621
2/4
✓ Branch 0 taken 11330 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11330 times.
11330 if (!side_data || side_data_size < 4+4+8*error_count)
622 return AVERROR(ENOMEM);
623
624 11330 AV_WL32(side_data , quality );
625 11330 side_data[4] = pict_type;
626 11330 side_data[5] = error_count;
627
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11330 times.
11330 for (i = 0; i<error_count; i++)
628 AV_WL64(side_data+8 + 8*i , error[i]);
629
630 11330 return 0;
631 }
632
633 int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp)
634 {
635 AVProducerReferenceTime *prft;
636 uint8_t *side_data;
637 size_t side_data_size;
638
639 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &side_data_size);
640 if (!side_data) {
641 side_data_size = sizeof(AVProducerReferenceTime);
642 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT, side_data_size);
643 }
644
645 if (!side_data || side_data_size < sizeof(AVProducerReferenceTime))
646 return AVERROR(ENOMEM);
647
648 prft = (AVProducerReferenceTime *)side_data;
649 prft->wallclock = timestamp;
650 prft->flags = 0;
651
652 return 0;
653 }
654
655 7958 const AVPacketSideData *av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd,
656 enum AVPacketSideDataType type)
657 {
658
2/2
✓ Branch 0 taken 735 times.
✓ Branch 1 taken 7889 times.
8624 for (int i = 0; i < nb_sd; i++)
659
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 666 times.
735 if (sd[i].type == type)
660 69 return &sd[i];
661
662 7889 return NULL;
663 }
664
665 801 static AVPacketSideData *packet_side_data_add(AVPacketSideData **psd, int *pnb_sd,
666 enum AVPacketSideDataType type,
667 void *data, size_t size)
668 {
669 801 AVPacketSideData *sd = *psd, *tmp;
670 801 int nb_sd = *pnb_sd;
671
672
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 800 times.
852 for (int i = 0; i < nb_sd; i++) {
673
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 1 times.
52 if (sd[i].type != type)
674 51 continue;
675
676 1 av_free(sd[i].data);
677 1 sd[i].data = data;
678 1 sd[i].size = size;
679 1 return &sd[i];
680 }
681
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 800 times.
800 if (nb_sd == INT_MAX)
683 return NULL;
684
685 800 tmp = av_realloc_array(sd, nb_sd + 1, sizeof(*tmp));
686
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 800 times.
800 if (!tmp)
687 return NULL;
688
689 800 *psd = sd = tmp;
690 800 sd[nb_sd].type = type;
691 800 sd[nb_sd].data = data;
692 800 sd[nb_sd].size = size;
693 800 *pnb_sd = nb_sd + 1;
694
695 800 return &sd[nb_sd];
696 }
697
698 477 AVPacketSideData *av_packet_side_data_add(AVPacketSideData **psd, int *pnb_sd,
699 enum AVPacketSideDataType type,
700 void *data, size_t size, int flags)
701 {
702 477 return packet_side_data_add(psd, pnb_sd, type, data, size);
703 }
704
705 324 AVPacketSideData *av_packet_side_data_new(AVPacketSideData **psd, int *pnb_sd,
706 enum AVPacketSideDataType type,
707 size_t size, int flags)
708 {
709 324 AVPacketSideData *sd = NULL;
710 uint8_t *data;
711
712
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 324 times.
324 if (size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
713 return NULL;
714
715 324 data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
716
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 324 times.
324 if (!data)
717 return NULL;
718 324 memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
719
720 324 sd = packet_side_data_add(psd, pnb_sd, type, data, size);
721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 324 times.
324 if (!sd)
722 av_freep(&data);
723
724 324 return sd;
725 }
726
727 void av_packet_side_data_remove(AVPacketSideData *sd, int *pnb_sd,
728 enum AVPacketSideDataType type)
729 {
730 int nb_sd = *pnb_sd;
731
732 for (int i = nb_sd - 1; i >= 0; i--) {
733 if (sd[i].type != type)
734 continue;
735 av_free(sd[i].data);
736 sd[i] = sd[--nb_sd];
737 break;
738 }
739
740 *pnb_sd = nb_sd;
741 }
742
743 275243 void av_packet_side_data_free(AVPacketSideData **psd, int *pnb_sd)
744 {
745 275243 AVPacketSideData *sd = *psd;
746 275243 int nb_sd = *pnb_sd;
747
748
2/2
✓ Branch 0 taken 4506 times.
✓ Branch 1 taken 275243 times.
279749 for (int i = 0; i < nb_sd; i++)
749 4506 av_free(sd[i].data);
750
751 275243 av_freep(psd);
752 275243 *pnb_sd = 0;
753 275243 }
754