FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/packet.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 356 465 76.6%
Functions: 38 41 92.7%
Branches: 148 243 60.9%

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/container_fifo.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/rational.h"
31
32 #include "defs.h"
33 #include "packet.h"
34 #include "packet_internal.h"
35
36 #if FF_API_INIT_PACKET
37 1353836 void av_init_packet(AVPacket *pkt)
38 {
39 1353836 pkt->pts = AV_NOPTS_VALUE;
40 1353836 pkt->dts = AV_NOPTS_VALUE;
41 1353836 pkt->pos = -1;
42 1353836 pkt->duration = 0;
43 1353836 pkt->flags = 0;
44 1353836 pkt->stream_index = 0;
45 1353836 pkt->buf = NULL;
46 1353836 pkt->side_data = NULL;
47 1353836 pkt->side_data_elems = 0;
48 1353836 pkt->opaque = NULL;
49 1353836 pkt->opaque_ref = NULL;
50 1353836 pkt->time_base = av_make_q(0, 1);
51 1353836 }
52 #endif
53
54 12257909 static void get_packet_defaults(AVPacket *pkt)
55 {
56 12257909 memset(pkt, 0, sizeof(*pkt));
57
58 12257909 pkt->pts = AV_NOPTS_VALUE;
59 12257909 pkt->dts = AV_NOPTS_VALUE;
60 12257909 pkt->pos = -1;
61 12257909 pkt->time_base = av_make_q(0, 1);
62 12257909 }
63
64 281104 AVPacket *av_packet_alloc(void)
65 {
66 281104 AVPacket *pkt = av_malloc(sizeof(AVPacket));
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 281104 times.
281104 if (!pkt)
68 return pkt;
69
70 281104 get_packet_defaults(pkt);
71
72 281104 return pkt;
73 }
74
75 356424 void av_packet_free(AVPacket **pkt)
76 {
77
3/4
✓ Branch 0 taken 356424 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75320 times.
✓ Branch 3 taken 281104 times.
356424 if (!pkt || !*pkt)
78 75320 return;
79
80 281104 av_packet_unref(*pkt);
81 281104 av_freep(pkt);
82 }
83
84 882212 static int packet_alloc(AVBufferRef **buf, int size)
85 {
86 int ret;
87
3/4
✓ Branch 0 taken 882212 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 882211 times.
882212 if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
88 1 return AVERROR(EINVAL);
89
90 882211 ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 882211 times.
882211 if (ret < 0)
92 return ret;
93
94 882211 memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
95
96 882211 return 0;
97 }
98
99 760617 int av_new_packet(AVPacket *pkt, int size)
100 {
101 760617 AVBufferRef *buf = NULL;
102 760617 int ret = packet_alloc(&buf, size);
103
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 760616 times.
760617 if (ret < 0)
104 1 return ret;
105
106 760616 get_packet_defaults(pkt);
107 760616 pkt->buf = buf;
108 760616 pkt->data = buf->data;
109 760616 pkt->size = size;
110
111 760616 return 0;
112 }
113
114 595096 void av_shrink_packet(AVPacket *pkt, int size)
115 {
116
2/2
✓ Branch 0 taken 589802 times.
✓ Branch 1 taken 5294 times.
595096 if (pkt->size <= size)
117 589802 return;
118 5294 pkt->size = size;
119 5294 memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
120 }
121
122 279995 int av_grow_packet(AVPacket *pkt, int grow_by)
123 {
124 int new_size;
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 279995 times.
279995 av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
126 279995 if ((unsigned)grow_by >
127
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 279994 times.
279995 INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
128 1 return AVERROR(ENOMEM);
129
130 279994 new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
131
2/2
✓ Branch 0 taken 13781 times.
✓ Branch 1 taken 266213 times.
279994 if (pkt->buf) {
132 size_t data_offset;
133 13781 uint8_t *old_data = pkt->data;
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13781 times.
13781 if (pkt->data == NULL) {
135 data_offset = 0;
136 pkt->data = pkt->buf->data;
137 } else {
138 13781 data_offset = pkt->data - pkt->buf->data;
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13781 times.
13781 if (data_offset > INT_MAX - new_size)
140 return AVERROR(ENOMEM);
141 }
142
143
3/4
✓ Branch 0 taken 2985 times.
✓ Branch 1 taken 10796 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2985 times.
16766 if (new_size + data_offset > pkt->buf->size ||
144 2985 !av_buffer_is_writable(pkt->buf)) {
145 int ret;
146
147 // allocate slightly more than requested to avoid excessive
148 // reallocations
149
1/2
✓ Branch 0 taken 10796 times.
✗ Branch 1 not taken.
10796 if (new_size + data_offset < INT_MAX - new_size/16)
150 10796 new_size += new_size/16;
151
152 10796 ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10796 times.
10796 if (ret < 0) {
154 pkt->data = old_data;
155 return ret;
156 }
157 10796 pkt->data = pkt->buf->data + data_offset;
158 }
159 } else {
160 266213 pkt->buf = av_buffer_alloc(new_size);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266213 times.
266213 if (!pkt->buf)
162 return AVERROR(ENOMEM);
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266213 times.
266213 if (pkt->size > 0)
164 memcpy(pkt->buf->data, pkt->data, pkt->size);
165 266213 pkt->data = pkt->buf->data;
166 }
167 279994 pkt->size += grow_by;
168 279994 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
169
170 279994 return 0;
171 }
172
173 3 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
174 {
175
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
176 1 return AVERROR(EINVAL);
177
178 2 pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
179 av_buffer_default_free, NULL, 0);
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!pkt->buf)
181 return AVERROR(ENOMEM);
182
183 2 pkt->data = data;
184 2 pkt->size = size;
185
186 2 return 0;
187 }
188
189 7679408 void av_packet_free_side_data(AVPacket *pkt)
190 {
191 int i;
192
2/2
✓ Branch 0 taken 55260 times.
✓ Branch 1 taken 7679408 times.
7734668 for (i = 0; i < pkt->side_data_elems; i++)
193 55260 av_freep(&pkt->side_data[i].data);
194 7679408 av_freep(&pkt->side_data);
195 7679408 pkt->side_data_elems = 0;
196 7679408 }
197
198 55262 int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
199 uint8_t *data, size_t size)
200 {
201 AVPacketSideData *tmp;
202 55262 int i, elems = pkt->side_data_elems;
203
204
2/2
✓ Branch 0 taken 222 times.
✓ Branch 1 taken 55260 times.
55482 for (i = 0; i < elems; i++) {
205 222 AVPacketSideData *sd = &pkt->side_data[i];
206
207
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 220 times.
222 if (sd->type == type) {
208 2 av_free(sd->data);
209 2 sd->data = data;
210 2 sd->size = size;
211 2 return 0;
212 }
213 }
214
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55260 times.
55260 if ((unsigned)elems + 1 > AV_PKT_DATA_NB)
216 return AVERROR(ERANGE);
217
218 55260 tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55260 times.
55260 if (!tmp)
220 return AVERROR(ENOMEM);
221
222 55260 pkt->side_data = tmp;
223 55260 pkt->side_data[elems].data = data;
224 55260 pkt->side_data[elems].size = size;
225 55260 pkt->side_data[elems].type = type;
226 55260 pkt->side_data_elems++;
227
228 55260 return 0;
229 }
230
231
232 53824 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
233 size_t size)
234 {
235 int ret;
236 uint8_t *data;
237
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53824 times.
53824 if (size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
239 return NULL;
240 53824 data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53824 times.
53824 if (!data)
242 return NULL;
243
244 53824 ret = av_packet_add_side_data(pkt, type, data, size);
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53824 times.
53824 if (ret < 0) {
246 av_freep(&data);
247 return NULL;
248 }
249
250 53824 return data;
251 }
252
253 1167032 uint8_t *av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
254 size_t *size)
255 {
256 int i;
257
258
2/2
✓ Branch 0 taken 29975 times.
✓ Branch 1 taken 1154613 times.
1184588 for (i = 0; i < pkt->side_data_elems; i++) {
259
2/2
✓ Branch 0 taken 12419 times.
✓ Branch 1 taken 17556 times.
29975 if (pkt->side_data[i].type == type) {
260
2/2
✓ Branch 0 taken 1086 times.
✓ Branch 1 taken 11333 times.
12419 if (size)
261 1086 *size = pkt->side_data[i].size;
262 12419 return pkt->side_data[i].data;
263 }
264 }
265
2/2
✓ Branch 0 taken 997132 times.
✓ Branch 1 taken 157481 times.
1154613 if (size)
266 997132 *size = 0;
267 1154613 return NULL;
268 }
269
270 1605 const char *av_packet_side_data_name(enum AVPacketSideDataType type)
271 {
272
17/39
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 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 taken 1 times.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
1605 switch(type) {
273 case AV_PKT_DATA_PALETTE: return "Palette";
274 5 case AV_PKT_DATA_NEW_EXTRADATA: return "New Extradata";
275 case AV_PKT_DATA_PARAM_CHANGE: return "Param Change";
276 case AV_PKT_DATA_H263_MB_INFO: return "H263 MB Info";
277 case AV_PKT_DATA_REPLAYGAIN: return "Replay Gain";
278 8 case AV_PKT_DATA_DISPLAYMATRIX: return "Display Matrix";
279 9 case AV_PKT_DATA_STEREO3D: return "Stereo 3D";
280 4 case AV_PKT_DATA_AUDIO_SERVICE_TYPE: return "Audio Service Type";
281 case AV_PKT_DATA_QUALITY_STATS: return "Quality stats";
282 case AV_PKT_DATA_FALLBACK_TRACK: return "Fallback track";
283 13 case AV_PKT_DATA_CPB_PROPERTIES: return "CPB properties";
284 20 case AV_PKT_DATA_SKIP_SAMPLES: return "Skip Samples";
285 case AV_PKT_DATA_JP_DUALMONO: return "JP Dual Mono";
286 162 case AV_PKT_DATA_STRINGS_METADATA: return "Strings Metadata";
287 case AV_PKT_DATA_SUBTITLE_POSITION: return "Subtitle Position";
288 case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL: return "Matroska BlockAdditional";
289 8 case AV_PKT_DATA_WEBVTT_IDENTIFIER: return "WebVTT ID";
290 16 case AV_PKT_DATA_WEBVTT_SETTINGS: return "WebVTT Settings";
291 case AV_PKT_DATA_METADATA_UPDATE: return "Metadata Update";
292 1338 case AV_PKT_DATA_MPEGTS_STREAM_ID: return "MPEGTS Stream ID";
293 6 case AV_PKT_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
294 5 case AV_PKT_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
295 4 case AV_PKT_DATA_SPHERICAL: return "Spherical Mapping";
296 case AV_PKT_DATA_A53_CC: return "A53 Closed Captions";
297 case AV_PKT_DATA_ENCRYPTION_INIT_INFO: return "Encryption initialization data";
298 case AV_PKT_DATA_ENCRYPTION_INFO: return "Encryption info";
299 case AV_PKT_DATA_AFD: return "Active Format Description data";
300 case AV_PKT_DATA_PRFT: return "Producer Reference Time";
301 case AV_PKT_DATA_ICC_PROFILE: return "ICC Profile";
302 2 case AV_PKT_DATA_DOVI_CONF: return "DOVI configuration record";
303 case AV_PKT_DATA_S12M_TIMECODE: return "SMPTE ST 12-1:2014 timecode";
304 2 case AV_PKT_DATA_DYNAMIC_HDR10_PLUS: return "HDR10+ Dynamic Metadata (SMPTE 2094-40)";
305 2 case AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT:return "Ambient viewing environment";
306 case AV_PKT_DATA_IAMF_MIX_GAIN_PARAM: return "IAMF Mix Gain Parameter Data";
307 case AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM: return "IAMF Demixing Info Parameter Data";
308 case AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM: return "IAMF Recon Gain Info Parameter Data";
309 1 case AV_PKT_DATA_FRAME_CROPPING: return "Frame Cropping";
310 case AV_PKT_DATA_LCEVC: return "LCEVC NAL data";
311 }
312 return NULL;
313 }
314
315 612 uint8_t *av_packet_pack_dictionary(AVDictionary *dict, size_t *size)
316 {
317 612 uint8_t *data = NULL;
318 612 *size = 0;
319
320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 if (!dict)
321 return NULL;
322
323
1/2
✓ Branch 0 taken 1224 times.
✗ Branch 1 not taken.
1224 for (int pass = 0; pass < 2; pass++) {
324 1224 const AVDictionaryEntry *t = NULL;
325 1224 size_t total_length = 0;
326
327
2/2
✓ Branch 1 taken 4978 times.
✓ Branch 2 taken 1224 times.
6202 while ((t = av_dict_iterate(dict, t))) {
328
2/2
✓ Branch 0 taken 9956 times.
✓ Branch 1 taken 4978 times.
14934 for (int i = 0; i < 2; i++) {
329
2/2
✓ Branch 0 taken 4978 times.
✓ Branch 1 taken 4978 times.
9956 const char *str = i ? t->value : t->key;
330 9956 const size_t len = strlen(str) + 1;
331
332
2/2
✓ Branch 0 taken 4978 times.
✓ Branch 1 taken 4978 times.
9956 if (pass)
333 4978 memcpy(data + total_length, str, len);
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4978 times.
4978 else if (len > SIZE_MAX - total_length)
335 return NULL;
336 9956 total_length += len;
337 }
338 }
339
2/2
✓ Branch 0 taken 612 times.
✓ Branch 1 taken 612 times.
1224 if (pass)
340 612 break;
341 612 data = av_malloc(total_length);
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 if (!data)
343 return NULL;
344 612 *size = total_length;
345 }
346
347 612 return data;
348 }
349
350 408938 int av_packet_unpack_dictionary(const uint8_t *data, size_t size,
351 AVDictionary **dict)
352 {
353 const uint8_t *end;
354 int ret;
355
356
4/6
✓ Branch 0 taken 408938 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 560 times.
✓ Branch 3 taken 408378 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 560 times.
408938 if (!dict || !data || !size)
357 408378 return 0;
358 560 end = data + size;
359
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])
360 return AVERROR_INVALIDDATA;
361
2/2
✓ Branch 0 taken 2399 times.
✓ Branch 1 taken 560 times.
2959 while (data < end) {
362 2399 const uint8_t *key = data;
363 2399 const uint8_t *val = data + strlen(key) + 1;
364
365
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)
366 return AVERROR_INVALIDDATA;
367
368 2399 ret = av_dict_set(dict, key, val, 0);
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2399 times.
2399 if (ret < 0)
370 return ret;
371 2399 data = val + strlen(val) + 1;
372 }
373
374 560 return 0;
375 }
376
377 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
378 size_t size)
379 {
380 int i;
381
382 for (i = 0; i < pkt->side_data_elems; i++) {
383 if (pkt->side_data[i].type == type) {
384 if (size > pkt->side_data[i].size)
385 return AVERROR(ENOMEM);
386 pkt->side_data[i].size = size;
387 return 0;
388 }
389 }
390 return AVERROR(ENOENT);
391 }
392
393 799535 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
394 {
395 int i, ret;
396
397 799535 dst->pts = src->pts;
398 799535 dst->dts = src->dts;
399 799535 dst->pos = src->pos;
400 799535 dst->duration = src->duration;
401 799535 dst->flags = src->flags;
402 799535 dst->stream_index = src->stream_index;
403 799535 dst->opaque = src->opaque;
404 799535 dst->time_base = src->time_base;
405 799535 dst->opaque_ref = NULL;
406 799535 dst->side_data = NULL;
407 799535 dst->side_data_elems = 0;
408
409 799535 ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 799535 times.
799535 if (ret < 0)
411 return ret;
412
413
2/2
✓ Branch 0 taken 12440 times.
✓ Branch 1 taken 799535 times.
811975 for (i = 0; i < src->side_data_elems; i++) {
414 12440 enum AVPacketSideDataType type = src->side_data[i].type;
415 12440 size_t size = src->side_data[i].size;
416 12440 uint8_t *src_data = src->side_data[i].data;
417 12440 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
418
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12440 times.
12440 if (!dst_data) {
420 av_buffer_unref(&dst->opaque_ref);
421 av_packet_free_side_data(dst);
422 return AVERROR(ENOMEM);
423 }
424 12440 memcpy(dst_data, src_data, size);
425 }
426
427 799535 return 0;
428 }
429
430 7679408 void av_packet_unref(AVPacket *pkt)
431 {
432 7679408 av_packet_free_side_data(pkt);
433 7679408 av_buffer_unref(&pkt->opaque_ref);
434 7679408 av_buffer_unref(&pkt->buf);
435 7679408 get_packet_defaults(pkt);
436 7679408 }
437
438 396922 int av_packet_ref(AVPacket *dst, const AVPacket *src)
439 {
440 int ret;
441
442 396922 dst->buf = NULL;
443
444 396922 ret = av_packet_copy_props(dst, src);
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 396922 times.
396922 if (ret < 0)
446 goto fail;
447
448
2/2
✓ Branch 0 taken 1030 times.
✓ Branch 1 taken 395892 times.
396922 if (!src->buf) {
449 1030 ret = packet_alloc(&dst->buf, src->size);
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1030 times.
1030 if (ret < 0)
451 goto fail;
452 av_assert1(!src->size || src->data);
453
1/2
✓ Branch 0 taken 1030 times.
✗ Branch 1 not taken.
1030 if (src->size)
454 1030 memcpy(dst->buf->data, src->data, src->size);
455
456 1030 dst->data = dst->buf->data;
457 } else {
458 395892 dst->buf = av_buffer_ref(src->buf);
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 395892 times.
395892 if (!dst->buf) {
460 ret = AVERROR(ENOMEM);
461 goto fail;
462 }
463 395892 dst->data = src->data;
464 }
465
466 396922 dst->size = src->size;
467
468 396922 return 0;
469 fail:
470 av_packet_unref(dst);
471 return ret;
472 }
473
474 6 AVPacket *av_packet_clone(const AVPacket *src)
475 {
476 6 AVPacket *ret = av_packet_alloc();
477
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!ret)
479 return ret;
480
481
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (av_packet_ref(ret, src))
482 av_packet_free(&ret);
483
484 6 return ret;
485 }
486
487 3536725 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
488 {
489 3536725 *dst = *src;
490 3536725 get_packet_defaults(src);
491 3536725 }
492
493 2054135 int av_packet_make_refcounted(AVPacket *pkt)
494 {
495 int ret;
496
497
2/2
✓ Branch 0 taken 1933570 times.
✓ Branch 1 taken 120565 times.
2054135 if (pkt->buf)
498 1933570 return 0;
499
500 120565 ret = packet_alloc(&pkt->buf, pkt->size);
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120565 times.
120565 if (ret < 0)
502 return ret;
503 av_assert1(!pkt->size || pkt->data);
504
2/2
✓ Branch 0 taken 119009 times.
✓ Branch 1 taken 1556 times.
120565 if (pkt->size)
505 119009 memcpy(pkt->buf->data, pkt->data, pkt->size);
506
507 120565 pkt->data = pkt->buf->data;
508
509 120565 return 0;
510 }
511
512 142 int av_packet_make_writable(AVPacket *pkt)
513 {
514 142 AVBufferRef *buf = NULL;
515 int ret;
516
517
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))
518 142 return 0;
519
520 ret = packet_alloc(&buf, pkt->size);
521 if (ret < 0)
522 return ret;
523 av_assert1(!pkt->size || pkt->data);
524 if (pkt->size)
525 memcpy(buf->data, pkt->data, pkt->size);
526
527 av_buffer_unref(&pkt->buf);
528 pkt->buf = buf;
529 pkt->data = buf->data;
530
531 return 0;
532 }
533
534 467985 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
535 {
536
2/2
✓ Branch 0 taken 452794 times.
✓ Branch 1 taken 15191 times.
467985 if (pkt->pts != AV_NOPTS_VALUE)
537 452794 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
538
2/2
✓ Branch 0 taken 467984 times.
✓ Branch 1 taken 1 times.
467985 if (pkt->dts != AV_NOPTS_VALUE)
539 467984 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
540
2/2
✓ Branch 0 taken 449296 times.
✓ Branch 1 taken 18689 times.
467985 if (pkt->duration > 0)
541 449296 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
542 467985 }
543
544 430484 int avpriv_packet_list_put(PacketList *packet_buffer,
545 AVPacket *pkt,
546 int (*copy)(AVPacket *dst, const AVPacket *src),
547 int flags)
548 {
549 430484 PacketListEntry *pktl = av_malloc(sizeof(*pktl));
550 int ret;
551
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 430484 times.
430484 if (!pktl)
553 return AVERROR(ENOMEM);
554
555
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 430428 times.
430484 if (copy) {
556 56 get_packet_defaults(&pktl->pkt);
557 56 ret = copy(&pktl->pkt, pkt);
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (ret < 0) {
559 av_free(pktl);
560 return ret;
561 }
562 } else {
563 430428 ret = av_packet_make_refcounted(pkt);
564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 430428 times.
430428 if (ret < 0) {
565 av_free(pktl);
566 return ret;
567 }
568 430428 av_packet_move_ref(&pktl->pkt, pkt);
569 }
570
571 430484 pktl->next = NULL;
572
573
2/2
✓ Branch 0 taken 256096 times.
✓ Branch 1 taken 174388 times.
430484 if (packet_buffer->head)
574 256096 packet_buffer->tail->next = pktl;
575 else
576 174388 packet_buffer->head = pktl;
577
578 /* Add the packet in the buffered packet list. */
579 430484 packet_buffer->tail = pktl;
580 430484 return 0;
581 }
582
583 395261 int avpriv_packet_list_get(PacketList *pkt_buffer,
584 AVPacket *pkt)
585 {
586 395261 PacketListEntry *pktl = pkt_buffer->head;
587
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 395259 times.
395261 if (!pktl)
588 2 return AVERROR(EAGAIN);
589 395259 *pkt = pktl->pkt;
590 395259 pkt_buffer->head = pktl->next;
591
2/2
✓ Branch 0 taken 170409 times.
✓ Branch 1 taken 224850 times.
395259 if (!pkt_buffer->head)
592 170409 pkt_buffer->tail = NULL;
593 395259 av_freep(&pktl);
594 395259 return 0;
595 }
596
597 54277 void avpriv_packet_list_free(PacketList *pkt_buf)
598 {
599 54277 PacketListEntry *tmp = pkt_buf->head;
600
601
2/2
✓ Branch 0 taken 62463 times.
✓ Branch 1 taken 54277 times.
116740 while (tmp) {
602 62463 PacketListEntry *pktl = tmp;
603 62463 tmp = pktl->next;
604 62463 av_packet_unref(&pktl->pkt);
605 62463 av_freep(&pktl);
606 }
607 54277 pkt_buf->head = pkt_buf->tail = NULL;
608 54277 }
609
610 11330 int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
611 {
612 uint8_t *side_data;
613 size_t side_data_size;
614 int i;
615
616 11330 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
617
1/2
✓ Branch 0 taken 11330 times.
✗ Branch 1 not taken.
11330 if (!side_data) {
618 11330 side_data_size = 4+4+8*error_count;
619 11330 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
620 side_data_size);
621 }
622
623
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)
624 return AVERROR(ENOMEM);
625
626 11330 AV_WL32(side_data , quality );
627 11330 side_data[4] = pict_type;
628 11330 side_data[5] = error_count;
629
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11330 times.
11330 for (i = 0; i<error_count; i++)
630 AV_WL64(side_data+8 + 8*i , error[i]);
631
632 11330 return 0;
633 }
634
635 int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp)
636 {
637 AVProducerReferenceTime *prft;
638 uint8_t *side_data;
639 size_t side_data_size;
640
641 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &side_data_size);
642 if (!side_data) {
643 side_data_size = sizeof(AVProducerReferenceTime);
644 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT, side_data_size);
645 }
646
647 if (!side_data || side_data_size < sizeof(AVProducerReferenceTime))
648 return AVERROR(ENOMEM);
649
650 prft = (AVProducerReferenceTime *)side_data;
651 prft->wallclock = timestamp;
652 prft->flags = 0;
653
654 return 0;
655 }
656
657 8688 const AVPacketSideData *av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd,
658 enum AVPacketSideDataType type)
659 {
660
2/2
✓ Branch 0 taken 772 times.
✓ Branch 1 taken 8619 times.
9391 for (int i = 0; i < nb_sd; i++)
661
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 703 times.
772 if (sd[i].type == type)
662 69 return &sd[i];
663
664 8619 return NULL;
665 }
666
667 816 static AVPacketSideData *packet_side_data_add(AVPacketSideData **psd, int *pnb_sd,
668 enum AVPacketSideDataType type,
669 void *data, size_t size)
670 {
671 816 AVPacketSideData *sd = *psd, *tmp;
672 816 int nb_sd = *pnb_sd;
673
674
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 815 times.
873 for (int i = 0; i < nb_sd; i++) {
675
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 1 times.
58 if (sd[i].type != type)
676 57 continue;
677
678 1 av_free(sd[i].data);
679 1 sd[i].data = data;
680 1 sd[i].size = size;
681 1 return &sd[i];
682 }
683
684
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 815 times.
815 if (nb_sd == INT_MAX)
685 return NULL;
686
687 815 tmp = av_realloc_array(sd, nb_sd + 1, sizeof(*tmp));
688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 815 times.
815 if (!tmp)
689 return NULL;
690
691 815 *psd = sd = tmp;
692 815 sd[nb_sd].type = type;
693 815 sd[nb_sd].data = data;
694 815 sd[nb_sd].size = size;
695 815 *pnb_sd = nb_sd + 1;
696
697 815 return &sd[nb_sd];
698 }
699
700 479 AVPacketSideData *av_packet_side_data_add(AVPacketSideData **psd, int *pnb_sd,
701 enum AVPacketSideDataType type,
702 void *data, size_t size, int flags)
703 {
704 479 return packet_side_data_add(psd, pnb_sd, type, data, size);
705 }
706
707 337 AVPacketSideData *av_packet_side_data_new(AVPacketSideData **psd, int *pnb_sd,
708 enum AVPacketSideDataType type,
709 size_t size, int flags)
710 {
711 337 AVPacketSideData *sd = NULL;
712 uint8_t *data;
713
714
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 337 times.
337 if (size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
715 return NULL;
716
717 337 data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
718
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 337 times.
337 if (!data)
719 return NULL;
720 337 memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
721
722 337 sd = packet_side_data_add(psd, pnb_sd, type, data, size);
723
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 337 times.
337 if (!sd)
724 av_freep(&data);
725
726 337 return sd;
727 }
728
729 void av_packet_side_data_remove(AVPacketSideData *sd, int *pnb_sd,
730 enum AVPacketSideDataType type)
731 {
732 int nb_sd = *pnb_sd;
733
734 for (int i = nb_sd - 1; i >= 0; i--) {
735 if (sd[i].type != type)
736 continue;
737 av_free(sd[i].data);
738 sd[i] = sd[--nb_sd];
739 break;
740 }
741
742 *pnb_sd = nb_sd;
743 }
744
745 295161 void av_packet_side_data_free(AVPacketSideData **psd, int *pnb_sd)
746 {
747 295161 AVPacketSideData *sd = *psd;
748 295161 int nb_sd = *pnb_sd;
749
750
2/2
✓ Branch 0 taken 4590 times.
✓ Branch 1 taken 295161 times.
299751 for (int i = 0; i < nb_sd; i++)
751 4590 av_free(sd[i].data);
752
753 295161 av_freep(psd);
754 295161 *pnb_sd = 0;
755 295161 }
756
757 68665 static void *container_packet_alloc(void *opaque)
758 {
759 68665 return av_packet_alloc();
760 }
761
762 899411 static void container_packet_reset(void *opaque, void *obj)
763 {
764 899411 av_packet_unref(obj);
765 899411 }
766
767 68665 static void container_packet_free(void *opaque, void *obj)
768 {
769 68665 AVPacket *pkt = obj;
770 68665 av_packet_free(&pkt);
771 68665 }
772
773 1771682 static int container_packet_transfer(void *opaque, void *dst, void *src, unsigned flags)
774 {
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1771682 times.
1771682 if (flags & AV_CONTAINER_FIFO_FLAG_REF)
776 return av_packet_ref(dst, src);
777
778 1771682 av_packet_move_ref(dst, src);
779 1771682 return 0;
780 }
781
782 14727 AVContainerFifo *av_container_fifo_alloc_avpacket(unsigned flags)
783 {
784 14727 return av_container_fifo_alloc(NULL, container_packet_alloc,
785 container_packet_reset, container_packet_free,
786 container_packet_transfer, 0);
787 }
788