FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/packet.c
Date: 2024-12-12 01:08:13
Exec Total Coverage
Lines: 341 449 75.9%
Functions: 33 36 91.7%
Branches: 147 241 61.0%

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 1349562 void av_init_packet(AVPacket *pkt)
37 {
38 1349562 pkt->pts = AV_NOPTS_VALUE;
39 1349562 pkt->dts = AV_NOPTS_VALUE;
40 1349562 pkt->pos = -1;
41 1349562 pkt->duration = 0;
42 1349562 pkt->flags = 0;
43 1349562 pkt->stream_index = 0;
44 1349562 pkt->buf = NULL;
45 1349562 pkt->side_data = NULL;
46 1349562 pkt->side_data_elems = 0;
47 1349562 pkt->opaque = NULL;
48 1349562 pkt->opaque_ref = NULL;
49 1349562 pkt->time_base = av_make_q(0, 1);
50 1349562 }
51 #endif
52
53 12227501 static void get_packet_defaults(AVPacket *pkt)
54 {
55 12227501 memset(pkt, 0, sizeof(*pkt));
56
57 12227501 pkt->pts = AV_NOPTS_VALUE;
58 12227501 pkt->dts = AV_NOPTS_VALUE;
59 12227501 pkt->pos = -1;
60 12227501 pkt->time_base = av_make_q(0, 1);
61 12227501 }
62
63 280332 AVPacket *av_packet_alloc(void)
64 {
65 280332 AVPacket *pkt = av_malloc(sizeof(AVPacket));
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 280332 times.
280332 if (!pkt)
67 return pkt;
68
69 280332 get_packet_defaults(pkt);
70
71 280332 return pkt;
72 }
73
74 355529 void av_packet_free(AVPacket **pkt)
75 {
76
3/4
✓ Branch 0 taken 355529 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75197 times.
✓ Branch 3 taken 280332 times.
355529 if (!pkt || !*pkt)
77 75197 return;
78
79 280332 av_packet_unref(*pkt);
80 280332 av_freep(pkt);
81 }
82
83 881002 static int packet_alloc(AVBufferRef **buf, int size)
84 {
85 int ret;
86
3/4
✓ Branch 0 taken 881002 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 881001 times.
881002 if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
87 1 return AVERROR(EINVAL);
88
89 881001 ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 881001 times.
881001 if (ret < 0)
91 return ret;
92
93 881001 memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
94
95 881001 return 0;
96 }
97
98 759573 int av_new_packet(AVPacket *pkt, int size)
99 {
100 759573 AVBufferRef *buf = NULL;
101 759573 int ret = packet_alloc(&buf, size);
102
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 759572 times.
759573 if (ret < 0)
103 1 return ret;
104
105 759572 get_packet_defaults(pkt);
106 759572 pkt->buf = buf;
107 759572 pkt->data = buf->data;
108 759572 pkt->size = size;
109
110 759572 return 0;
111 }
112
113 593968 void av_shrink_packet(AVPacket *pkt, int size)
114 {
115
2/2
✓ Branch 0 taken 588677 times.
✓ Branch 1 taken 5291 times.
593968 if (pkt->size <= size)
116 588677 return;
117 5291 pkt->size = size;
118 5291 memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
119 }
120
121 278368 int av_grow_packet(AVPacket *pkt, int grow_by)
122 {
123 int new_size;
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 278368 times.
278368 av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
125 278368 if ((unsigned)grow_by >
126
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 278367 times.
278368 INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
127 1 return AVERROR(ENOMEM);
128
129 278367 new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
130
2/2
✓ Branch 0 taken 13772 times.
✓ Branch 1 taken 264595 times.
278367 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 264595 pkt->buf = av_buffer_alloc(new_size);
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 264595 times.
264595 if (!pkt->buf)
161 return AVERROR(ENOMEM);
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 264595 times.
264595 if (pkt->size > 0)
163 memcpy(pkt->buf->data, pkt->data, pkt->size);
164 264595 pkt->data = pkt->buf->data;
165 }
166 278367 pkt->size += grow_by;
167 278367 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
168
169 278367 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 7663611 void av_packet_free_side_data(AVPacket *pkt)
189 {
190 int i;
191
2/2
✓ Branch 0 taken 55201 times.
✓ Branch 1 taken 7663611 times.
7718812 for (i = 0; i < pkt->side_data_elems; i++)
192 55201 av_freep(&pkt->side_data[i].data);
193 7663611 av_freep(&pkt->side_data);
194 7663611 pkt->side_data_elems = 0;
195 7663611 }
196
197 55203 int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
198 uint8_t *data, size_t size)
199 {
200 AVPacketSideData *tmp;
201 55203 int i, elems = pkt->side_data_elems;
202
203
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 55201 times.
55419 for (i = 0; i < elems; i++) {
204 218 AVPacketSideData *sd = &pkt->side_data[i];
205
206
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 216 times.
218 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 55201 times.
55201 if ((unsigned)elems + 1 > AV_PKT_DATA_NB)
215 return AVERROR(ERANGE);
216
217 55201 tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55201 times.
55201 if (!tmp)
219 return AVERROR(ENOMEM);
220
221 55201 pkt->side_data = tmp;
222 55201 pkt->side_data[elems].data = data;
223 55201 pkt->side_data[elems].size = size;
224 55201 pkt->side_data[elems].type = type;
225 55201 pkt->side_data_elems++;
226
227 55201 return 0;
228 }
229
230
231 53768 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 53768 times.
53768 if (size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
238 return NULL;
239 53768 data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53768 times.
53768 if (!data)
241 return NULL;
242
243 53768 ret = av_packet_add_side_data(pkt, type, data, size);
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53768 times.
53768 if (ret < 0) {
245 av_freep(&data);
246 return NULL;
247 }
248
249 53768 return data;
250 }
251
252 1166057 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 29956 times.
✓ Branch 1 taken 1153648 times.
1183604 for (i = 0; i < pkt->side_data_elems; i++) {
258
2/2
✓ Branch 0 taken 12409 times.
✓ Branch 1 taken 17547 times.
29956 if (pkt->side_data[i].type == type) {
259
2/2
✓ Branch 0 taken 1076 times.
✓ Branch 1 taken 11333 times.
12409 if (size)
260 1076 *size = pkt->side_data[i].size;
261 12409 return pkt->side_data[i].data;
262 }
263 }
264
2/2
✓ Branch 0 taken 996377 times.
✓ Branch 1 taken 157271 times.
1153648 if (size)
265 996377 *size = 0;
266 1153648 return NULL;
267 }
268
269 1605 const char *av_packet_side_data_name(enum AVPacketSideDataType type)
270 {
271
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) {
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 8 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 1 case AV_PKT_DATA_FRAME_CROPPING: return "Frame Cropping";
309 case AV_PKT_DATA_LCEVC: return "LCEVC NAL data";
310 }
311 return NULL;
312 }
313
314 612 uint8_t *av_packet_pack_dictionary(AVDictionary *dict, size_t *size)
315 {
316 612 uint8_t *data = NULL;
317 612 *size = 0;
318
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 if (!dict)
320 return NULL;
321
322
1/2
✓ Branch 0 taken 1224 times.
✗ Branch 1 not taken.
1224 for (int pass = 0; pass < 2; pass++) {
323 1224 const AVDictionaryEntry *t = NULL;
324 1224 size_t total_length = 0;
325
326
2/2
✓ Branch 1 taken 4978 times.
✓ Branch 2 taken 1224 times.
6202 while ((t = av_dict_iterate(dict, t))) {
327
2/2
✓ Branch 0 taken 9956 times.
✓ Branch 1 taken 4978 times.
14934 for (int i = 0; i < 2; i++) {
328
2/2
✓ Branch 0 taken 4978 times.
✓ Branch 1 taken 4978 times.
9956 const char *str = i ? t->value : t->key;
329 9956 const size_t len = strlen(str) + 1;
330
331
2/2
✓ Branch 0 taken 4978 times.
✓ Branch 1 taken 4978 times.
9956 if (pass)
332 4978 memcpy(data + total_length, str, len);
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4978 times.
4978 else if (len > SIZE_MAX - total_length)
334 return NULL;
335 9956 total_length += len;
336 }
337 }
338
2/2
✓ Branch 0 taken 612 times.
✓ Branch 1 taken 612 times.
1224 if (pass)
339 612 break;
340 612 data = av_malloc(total_length);
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 if (!data)
342 return NULL;
343 612 *size = total_length;
344 }
345
346 612 return data;
347 }
348
349 408871 int av_packet_unpack_dictionary(const uint8_t *data, size_t size,
350 AVDictionary **dict)
351 {
352 const uint8_t *end;
353 int ret;
354
355
4/6
✓ Branch 0 taken 408871 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 560 times.
✓ Branch 3 taken 408311 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 560 times.
408871 if (!dict || !data || !size)
356 408311 return 0;
357 560 end = data + size;
358
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])
359 return AVERROR_INVALIDDATA;
360
2/2
✓ Branch 0 taken 2399 times.
✓ Branch 1 taken 560 times.
2959 while (data < end) {
361 2399 const uint8_t *key = data;
362 2399 const uint8_t *val = data + strlen(key) + 1;
363
364
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)
365 return AVERROR_INVALIDDATA;
366
367 2399 ret = av_dict_set(dict, key, val, 0);
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2399 times.
2399 if (ret < 0)
369 return ret;
370 2399 data = val + strlen(val) + 1;
371 }
372
373 560 return 0;
374 }
375
376 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
377 size_t size)
378 {
379 int i;
380
381 for (i = 0; i < pkt->side_data_elems; i++) {
382 if (pkt->side_data[i].type == type) {
383 if (size > pkt->side_data[i].size)
384 return AVERROR(ENOMEM);
385 pkt->side_data[i].size = size;
386 return 0;
387 }
388 }
389 return AVERROR(ENOENT);
390 }
391
392 799276 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
393 {
394 int i, ret;
395
396 799276 dst->pts = src->pts;
397 799276 dst->dts = src->dts;
398 799276 dst->pos = src->pos;
399 799276 dst->duration = src->duration;
400 799276 dst->flags = src->flags;
401 799276 dst->stream_index = src->stream_index;
402 799276 dst->opaque = src->opaque;
403 799276 dst->time_base = src->time_base;
404 799276 dst->opaque_ref = NULL;
405 799276 dst->side_data = NULL;
406 799276 dst->side_data_elems = 0;
407
408 799276 ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 799276 times.
799276 if (ret < 0)
410 return ret;
411
412
2/2
✓ Branch 0 taken 12434 times.
✓ Branch 1 taken 799276 times.
811710 for (i = 0; i < src->side_data_elems; i++) {
413 12434 enum AVPacketSideDataType type = src->side_data[i].type;
414 12434 size_t size = src->side_data[i].size;
415 12434 uint8_t *src_data = src->side_data[i].data;
416 12434 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
417
418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12434 times.
12434 if (!dst_data) {
419 av_buffer_unref(&dst->opaque_ref);
420 av_packet_free_side_data(dst);
421 return AVERROR(ENOMEM);
422 }
423 12434 memcpy(dst_data, src_data, size);
424 }
425
426 799276 return 0;
427 }
428
429 7663611 void av_packet_unref(AVPacket *pkt)
430 {
431 7663611 av_packet_free_side_data(pkt);
432 7663611 av_buffer_unref(&pkt->opaque_ref);
433 7663611 av_buffer_unref(&pkt->buf);
434 7663611 get_packet_defaults(pkt);
435 7663611 }
436
437 396781 int av_packet_ref(AVPacket *dst, const AVPacket *src)
438 {
439 int ret;
440
441 396781 dst->buf = NULL;
442
443 396781 ret = av_packet_copy_props(dst, src);
444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 396781 times.
396781 if (ret < 0)
445 goto fail;
446
447
2/2
✓ Branch 0 taken 1030 times.
✓ Branch 1 taken 395751 times.
396781 if (!src->buf) {
448 1030 ret = packet_alloc(&dst->buf, src->size);
449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1030 times.
1030 if (ret < 0)
450 goto fail;
451 av_assert1(!src->size || src->data);
452
1/2
✓ Branch 0 taken 1030 times.
✗ Branch 1 not taken.
1030 if (src->size)
453 1030 memcpy(dst->buf->data, src->data, src->size);
454
455 1030 dst->data = dst->buf->data;
456 } else {
457 395751 dst->buf = av_buffer_ref(src->buf);
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 395751 times.
395751 if (!dst->buf) {
459 ret = AVERROR(ENOMEM);
460 goto fail;
461 }
462 395751 dst->data = src->data;
463 }
464
465 396781 dst->size = src->size;
466
467 396781 return 0;
468 fail:
469 av_packet_unref(dst);
470 return ret;
471 }
472
473 6 AVPacket *av_packet_clone(const AVPacket *src)
474 {
475 6 AVPacket *ret = av_packet_alloc();
476
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!ret)
478 return ret;
479
480
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (av_packet_ref(ret, src))
481 av_packet_free(&ret);
482
483 6 return ret;
484 }
485
486 3523930 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
487 {
488 3523930 *dst = *src;
489 3523930 get_packet_defaults(src);
490 3523930 }
491
492 2047768 int av_packet_make_refcounted(AVPacket *pkt)
493 {
494 int ret;
495
496
2/2
✓ Branch 0 taken 1927369 times.
✓ Branch 1 taken 120399 times.
2047768 if (pkt->buf)
497 1927369 return 0;
498
499 120399 ret = packet_alloc(&pkt->buf, pkt->size);
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120399 times.
120399 if (ret < 0)
501 return ret;
502 av_assert1(!pkt->size || pkt->data);
503
2/2
✓ Branch 0 taken 118869 times.
✓ Branch 1 taken 1530 times.
120399 if (pkt->size)
504 118869 memcpy(pkt->buf->data, pkt->data, pkt->size);
505
506 120399 pkt->data = pkt->buf->data;
507
508 120399 return 0;
509 }
510
511 142 int av_packet_make_writable(AVPacket *pkt)
512 {
513 142 AVBufferRef *buf = NULL;
514 int ret;
515
516
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))
517 142 return 0;
518
519 ret = packet_alloc(&buf, pkt->size);
520 if (ret < 0)
521 return ret;
522 av_assert1(!pkt->size || pkt->data);
523 if (pkt->size)
524 memcpy(buf->data, pkt->data, pkt->size);
525
526 av_buffer_unref(&pkt->buf);
527 pkt->buf = buf;
528 pkt->data = buf->data;
529
530 return 0;
531 }
532
533 467408 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
534 {
535
2/2
✓ Branch 0 taken 452227 times.
✓ Branch 1 taken 15181 times.
467408 if (pkt->pts != AV_NOPTS_VALUE)
536 452227 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
537
2/2
✓ Branch 0 taken 467407 times.
✓ Branch 1 taken 1 times.
467408 if (pkt->dts != AV_NOPTS_VALUE)
538 467407 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
539
2/2
✓ Branch 0 taken 448923 times.
✓ Branch 1 taken 18485 times.
467408 if (pkt->duration > 0)
540 448923 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
541 467408 }
542
543 422361 int avpriv_packet_list_put(PacketList *packet_buffer,
544 AVPacket *pkt,
545 int (*copy)(AVPacket *dst, const AVPacket *src),
546 int flags)
547 {
548 422361 PacketListEntry *pktl = av_malloc(sizeof(*pktl));
549 int ret;
550
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 422361 times.
422361 if (!pktl)
552 return AVERROR(ENOMEM);
553
554
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 422305 times.
422361 if (copy) {
555 56 get_packet_defaults(&pktl->pkt);
556 56 ret = copy(&pktl->pkt, pkt);
557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (ret < 0) {
558 av_free(pktl);
559 return ret;
560 }
561 } else {
562 422305 ret = av_packet_make_refcounted(pkt);
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 422305 times.
422305 if (ret < 0) {
564 av_free(pktl);
565 return ret;
566 }
567 422305 av_packet_move_ref(&pktl->pkt, pkt);
568 }
569
570 422361 pktl->next = NULL;
571
572
2/2
✓ Branch 0 taken 254447 times.
✓ Branch 1 taken 167914 times.
422361 if (packet_buffer->head)
573 254447 packet_buffer->tail->next = pktl;
574 else
575 167914 packet_buffer->head = pktl;
576
577 /* Add the packet in the buffered packet list. */
578 422361 packet_buffer->tail = pktl;
579 422361 return 0;
580 }
581
582 386443 int avpriv_packet_list_get(PacketList *pkt_buffer,
583 AVPacket *pkt)
584 {
585 386443 PacketListEntry *pktl = pkt_buffer->head;
586
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 386441 times.
386443 if (!pktl)
587 2 return AVERROR(EAGAIN);
588 386441 *pkt = pktl->pkt;
589 386441 pkt_buffer->head = pktl->next;
590
2/2
✓ Branch 0 taken 163933 times.
✓ Branch 1 taken 222508 times.
386441 if (!pkt_buffer->head)
591 163933 pkt_buffer->tail = NULL;
592 386441 av_freep(&pktl);
593 386441 return 0;
594 }
595
596 38701 void avpriv_packet_list_free(PacketList *pkt_buf)
597 {
598 38701 PacketListEntry *tmp = pkt_buf->head;
599
600
2/2
✓ Branch 0 taken 62014 times.
✓ Branch 1 taken 38701 times.
100715 while (tmp) {
601 62014 PacketListEntry *pktl = tmp;
602 62014 tmp = pktl->next;
603 62014 av_packet_unref(&pktl->pkt);
604 62014 av_freep(&pktl);
605 }
606 38701 pkt_buf->head = pkt_buf->tail = NULL;
607 38701 }
608
609 11330 int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
610 {
611 uint8_t *side_data;
612 size_t side_data_size;
613 int i;
614
615 11330 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
616
1/2
✓ Branch 0 taken 11330 times.
✗ Branch 1 not taken.
11330 if (!side_data) {
617 11330 side_data_size = 4+4+8*error_count;
618 11330 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
619 side_data_size);
620 }
621
622
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)
623 return AVERROR(ENOMEM);
624
625 11330 AV_WL32(side_data , quality );
626 11330 side_data[4] = pict_type;
627 11330 side_data[5] = error_count;
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11330 times.
11330 for (i = 0; i<error_count; i++)
629 AV_WL64(side_data+8 + 8*i , error[i]);
630
631 11330 return 0;
632 }
633
634 int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp)
635 {
636 AVProducerReferenceTime *prft;
637 uint8_t *side_data;
638 size_t side_data_size;
639
640 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &side_data_size);
641 if (!side_data) {
642 side_data_size = sizeof(AVProducerReferenceTime);
643 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT, side_data_size);
644 }
645
646 if (!side_data || side_data_size < sizeof(AVProducerReferenceTime))
647 return AVERROR(ENOMEM);
648
649 prft = (AVProducerReferenceTime *)side_data;
650 prft->wallclock = timestamp;
651 prft->flags = 0;
652
653 return 0;
654 }
655
656 8673 const AVPacketSideData *av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd,
657 enum AVPacketSideDataType type)
658 {
659
2/2
✓ Branch 0 taken 772 times.
✓ Branch 1 taken 8604 times.
9376 for (int i = 0; i < nb_sd; i++)
660
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 703 times.
772 if (sd[i].type == type)
661 69 return &sd[i];
662
663 8604 return NULL;
664 }
665
666 816 static AVPacketSideData *packet_side_data_add(AVPacketSideData **psd, int *pnb_sd,
667 enum AVPacketSideDataType type,
668 void *data, size_t size)
669 {
670 816 AVPacketSideData *sd = *psd, *tmp;
671 816 int nb_sd = *pnb_sd;
672
673
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 815 times.
873 for (int i = 0; i < nb_sd; i++) {
674
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 1 times.
58 if (sd[i].type != type)
675 57 continue;
676
677 1 av_free(sd[i].data);
678 1 sd[i].data = data;
679 1 sd[i].size = size;
680 1 return &sd[i];
681 }
682
683
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 815 times.
815 if (nb_sd == INT_MAX)
684 return NULL;
685
686 815 tmp = av_realloc_array(sd, nb_sd + 1, sizeof(*tmp));
687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 815 times.
815 if (!tmp)
688 return NULL;
689
690 815 *psd = sd = tmp;
691 815 sd[nb_sd].type = type;
692 815 sd[nb_sd].data = data;
693 815 sd[nb_sd].size = size;
694 815 *pnb_sd = nb_sd + 1;
695
696 815 return &sd[nb_sd];
697 }
698
699 479 AVPacketSideData *av_packet_side_data_add(AVPacketSideData **psd, int *pnb_sd,
700 enum AVPacketSideDataType type,
701 void *data, size_t size, int flags)
702 {
703 479 return packet_side_data_add(psd, pnb_sd, type, data, size);
704 }
705
706 337 AVPacketSideData *av_packet_side_data_new(AVPacketSideData **psd, int *pnb_sd,
707 enum AVPacketSideDataType type,
708 size_t size, int flags)
709 {
710 337 AVPacketSideData *sd = NULL;
711 uint8_t *data;
712
713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 337 times.
337 if (size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
714 return NULL;
715
716 337 data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
717
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 337 times.
337 if (!data)
718 return NULL;
719 337 memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
720
721 337 sd = packet_side_data_add(psd, pnb_sd, type, data, size);
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 337 times.
337 if (!sd)
723 av_freep(&data);
724
725 337 return sd;
726 }
727
728 void av_packet_side_data_remove(AVPacketSideData *sd, int *pnb_sd,
729 enum AVPacketSideDataType type)
730 {
731 int nb_sd = *pnb_sd;
732
733 for (int i = nb_sd - 1; i >= 0; i--) {
734 if (sd[i].type != type)
735 continue;
736 av_free(sd[i].data);
737 sd[i] = sd[--nb_sd];
738 break;
739 }
740
741 *pnb_sd = nb_sd;
742 }
743
744 293138 void av_packet_side_data_free(AVPacketSideData **psd, int *pnb_sd)
745 {
746 293138 AVPacketSideData *sd = *psd;
747 293138 int nb_sd = *pnb_sd;
748
749
2/2
✓ Branch 0 taken 4590 times.
✓ Branch 1 taken 293138 times.
297728 for (int i = 0; i < nb_sd; i++)
750 4590 av_free(sd[i].data);
751
752 293138 av_freep(psd);
753 293138 *pnb_sd = 0;
754 293138 }
755