FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/packet.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 340 447 76.1%
Functions: 33 36 91.7%
Branches: 147 239 61.5%

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