FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/avpacket.c
Date: 2022-01-28 07:56:06
Exec Total Coverage
Lines: 299 385 77.7%
Branches: 132 211 62.6%

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/common.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/rational.h"
30
31 #include "bytestream.h"
32 #include "internal.h"
33 #include "packet.h"
34 #include "packet_internal.h"
35
36 #if FF_API_INIT_PACKET
37 1269986 void av_init_packet(AVPacket *pkt)
38 {
39 1269986 pkt->pts = AV_NOPTS_VALUE;
40 1269986 pkt->dts = AV_NOPTS_VALUE;
41 1269986 pkt->pos = -1;
42 1269986 pkt->duration = 0;
43 1269986 pkt->flags = 0;
44 1269986 pkt->stream_index = 0;
45 1269986 pkt->buf = NULL;
46 1269986 pkt->side_data = NULL;
47 1269986 pkt->side_data_elems = 0;
48 1269986 pkt->opaque = NULL;
49 1269986 pkt->opaque_ref = NULL;
50 1269986 pkt->time_base = av_make_q(0, 1);
51 1269986 }
52 #endif
53
54 8277299 static void get_packet_defaults(AVPacket *pkt)
55 {
56 8277299 memset(pkt, 0, sizeof(*pkt));
57
58 8277299 pkt->pts = AV_NOPTS_VALUE;
59 8277299 pkt->dts = AV_NOPTS_VALUE;
60 8277299 pkt->pos = -1;
61 8277299 pkt->time_base = av_make_q(0, 1);
62 8277299 }
63
64 178051 AVPacket *av_packet_alloc(void)
65 {
66 178051 AVPacket *pkt = av_malloc(sizeof(AVPacket));
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 178051 times.
178051 if (!pkt)
68 return pkt;
69
70 178051 get_packet_defaults(pkt);
71
72 178051 return pkt;
73 }
74
75 178761 void av_packet_free(AVPacket **pkt)
76 {
77
3/4
✓ Branch 0 taken 178761 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 710 times.
✓ Branch 3 taken 178051 times.
178761 if (!pkt || !*pkt)
78 710 return;
79
80 178051 av_packet_unref(*pkt);
81 178051 av_freep(pkt);
82 }
83
84 772585 static int packet_alloc(AVBufferRef **buf, int size)
85 {
86 int ret;
87
3/4
✓ Branch 0 taken 772585 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 772584 times.
772585 if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
88 1 return AVERROR(EINVAL);
89
90 772584 ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 772584 times.
772584 if (ret < 0)
92 return ret;
93
94 772584 memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
95
96 772584 return 0;
97 }
98
99 646830 int av_new_packet(AVPacket *pkt, int size)
100 {
101 646830 AVBufferRef *buf = NULL;
102 646830 int ret = packet_alloc(&buf, size);
103
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 646829 times.
646830 if (ret < 0)
104 1 return ret;
105
106 646829 get_packet_defaults(pkt);
107 646829 pkt->buf = buf;
108 646829 pkt->data = buf->data;
109 646829 pkt->size = size;
110
111 646829 return 0;
112 }
113
114 507285 void av_shrink_packet(AVPacket *pkt, int size)
115 {
116
2/2
✓ Branch 0 taken 503742 times.
✓ Branch 1 taken 3543 times.
507285 if (pkt->size <= size)
117 503742 return;
118 3543 pkt->size = size;
119 3543 memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
120 }
121
122 296821 int av_grow_packet(AVPacket *pkt, int grow_by)
123 {
124 int new_size;
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 296821 times.
296821 av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
126 296821 if ((unsigned)grow_by >
127
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 296820 times.
296821 INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
128 1 return AVERROR(ENOMEM);
129
130 296820 new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
131
2/2
✓ Branch 0 taken 13306 times.
✓ Branch 1 taken 283514 times.
296820 if (pkt->buf) {
132 size_t data_offset;
133 13306 uint8_t *old_data = pkt->data;
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13306 times.
13306 if (pkt->data == NULL) {
135 data_offset = 0;
136 pkt->data = pkt->buf->data;
137 } else {
138 13306 data_offset = pkt->data - pkt->buf->data;
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13306 times.
13306 if (data_offset > INT_MAX - new_size)
140 return AVERROR(ENOMEM);
141 }
142
143
3/4
✓ Branch 0 taken 2971 times.
✓ Branch 1 taken 10335 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2971 times.
16277 if (new_size + data_offset > pkt->buf->size ||
144 2971 !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 10335 times.
✗ Branch 1 not taken.
10335 if (new_size + data_offset < INT_MAX - new_size/16)
150 10335 new_size += new_size/16;
151
152 10335 ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10335 times.
10335 if (ret < 0) {
154 pkt->data = old_data;
155 return ret;
156 }
157 10335 pkt->data = pkt->buf->data + data_offset;
158 }
159 } else {
160 283514 pkt->buf = av_buffer_alloc(new_size);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 283514 times.
283514 if (!pkt->buf)
162 return AVERROR(ENOMEM);
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 283514 times.
283514 if (pkt->size > 0)
164 memcpy(pkt->buf->data, pkt->data, pkt->size);
165 283514 pkt->data = pkt->buf->data;
166 }
167 296820 pkt->size += grow_by;
168 296820 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
169
170 296820 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 5810869 void av_packet_free_side_data(AVPacket *pkt)
190 {
191 int i;
192
2/2
✓ Branch 0 taken 51751 times.
✓ Branch 1 taken 5810869 times.
5862620 for (i = 0; i < pkt->side_data_elems; i++)
193 51751 av_freep(&pkt->side_data[i].data);
194 5810869 av_freep(&pkt->side_data);
195 5810869 pkt->side_data_elems = 0;
196 5810869 }
197
198 51751 int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
199 uint8_t *data, size_t size)
200 {
201 AVPacketSideData *tmp;
202 51751 int i, elems = pkt->side_data_elems;
203
204
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 51751 times.
51884 for (i = 0; i < elems; i++) {
205 133 AVPacketSideData *sd = &pkt->side_data[i];
206
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
133 if (sd->type == type) {
208 av_free(sd->data);
209 sd->data = data;
210 sd->size = size;
211 return 0;
212 }
213 }
214
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51751 times.
51751 if ((unsigned)elems + 1 > AV_PKT_DATA_NB)
216 return AVERROR(ERANGE);
217
218 51751 tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51751 times.
51751 if (!tmp)
220 return AVERROR(ENOMEM);
221
222 51751 pkt->side_data = tmp;
223 51751 pkt->side_data[elems].data = data;
224 51751 pkt->side_data[elems].size = size;
225 51751 pkt->side_data[elems].type = type;
226 51751 pkt->side_data_elems++;
227
228 51751 return 0;
229 }
230
231
232 50452 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 50452 times.
50452 if (size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
239 return NULL;
240 50452 data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50452 times.
50452 if (!data)
242 return NULL;
243
244 50452 ret = av_packet_add_side_data(pkt, type, data, size);
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50452 times.
50452 if (ret < 0) {
246 av_freep(&data);
247 return NULL;
248 }
249
250 50452 return data;
251 }
252
253 5983189 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 90432 times.
✓ Branch 1 taken 5972556 times.
6062988 for (i = 0; i < pkt->side_data_elems; i++) {
259
2/2
✓ Branch 0 taken 10633 times.
✓ Branch 1 taken 79799 times.
90432 if (pkt->side_data[i].type == type) {
260
2/2
✓ Branch 0 taken 1089 times.
✓ Branch 1 taken 9544 times.
10633 if (size)
261 1089 *size = pkt->side_data[i].size;
262 10633 return pkt->side_data[i].data;
263 }
264 }
265
2/2
✓ Branch 0 taken 5838079 times.
✓ Branch 1 taken 134477 times.
5972556 if (size)
266 5838079 *size = 0;
267 5972556 return NULL;
268 }
269
270 866 const char *av_packet_side_data_name(enum AVPacketSideDataType type)
271 {
272
14/33
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 8 times.
✓ Branch 11 taken 10 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 131 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 667 times.
✓ Branch 20 taken 3 times.
✓ Branch 21 taken 3 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 not taken.
✗ Branch 32 not taken.
866 switch(type) {
273 case AV_PKT_DATA_PALETTE: return "Palette";
274 3 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 2 case AV_PKT_DATA_DISPLAYMATRIX: return "Display Matrix";
279 5 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 8 case AV_PKT_DATA_CPB_PROPERTIES: return "CPB properties";
284 10 case AV_PKT_DATA_SKIP_SAMPLES: return "Skip Samples";
285 case AV_PKT_DATA_JP_DUALMONO: return "JP Dual Mono";
286 131 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 667 case AV_PKT_DATA_MPEGTS_STREAM_ID: return "MPEGTS Stream ID";
293 3 case AV_PKT_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
294 3 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 case AV_PKT_DATA_DYNAMIC_HDR10_PLUS: return "HDR10+ Dynamic Metadata (SMPTE 2094-40)";
305 }
306 return NULL;
307 }
308
309 587 uint8_t *av_packet_pack_dictionary(AVDictionary *dict, size_t *size)
310 {
311 587 uint8_t *data = NULL;
312 587 *size = 0;
313
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if (!dict)
315 return NULL;
316
317
1/2
✓ Branch 0 taken 1174 times.
✗ Branch 1 not taken.
1174 for (int pass = 0; pass < 2; pass++) {
318 1174 const AVDictionaryEntry *t = NULL;
319 1174 size_t total_length = 0;
320
321
2/2
✓ Branch 1 taken 4638 times.
✓ Branch 2 taken 1174 times.
5812 while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX))) {
322
2/2
✓ Branch 0 taken 9276 times.
✓ Branch 1 taken 4638 times.
13914 for (int i = 0; i < 2; i++) {
323
2/2
✓ Branch 0 taken 4638 times.
✓ Branch 1 taken 4638 times.
9276 const char *str = i ? t->value : t->key;
324 9276 const size_t len = strlen(str) + 1;
325
326
2/2
✓ Branch 0 taken 4638 times.
✓ Branch 1 taken 4638 times.
9276 if (pass)
327 4638 memcpy(data + total_length, str, len);
328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4638 times.
4638 else if (len > SIZE_MAX - total_length)
329 return NULL;
330 9276 total_length += len;
331 }
332 }
333
2/2
✓ Branch 0 taken 587 times.
✓ Branch 1 taken 587 times.
1174 if (pass)
334 587 break;
335 587 data = av_malloc(total_length);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if (!data)
337 return NULL;
338 587 *size = total_length;
339 }
340
341 587 return data;
342 }
343
344 414112 int av_packet_unpack_dictionary(const uint8_t *data, size_t size,
345 AVDictionary **dict)
346 {
347 const uint8_t *end;
348 int ret;
349
350
4/6
✓ Branch 0 taken 414112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 536 times.
✓ Branch 3 taken 413576 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 536 times.
414112 if (!dict || !data || !size)
351 413576 return 0;
352 536 end = data + size;
353
2/4
✓ Branch 0 taken 536 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 536 times.
536 if (size && end[-1])
354 return AVERROR_INVALIDDATA;
355
2/2
✓ Branch 0 taken 2233 times.
✓ Branch 1 taken 536 times.
2769 while (data < end) {
356 2233 const uint8_t *key = data;
357 2233 const uint8_t *val = data + strlen(key) + 1;
358
359
2/4
✓ Branch 0 taken 2233 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2233 times.
2233 if (val >= end || !*key)
360 return AVERROR_INVALIDDATA;
361
362 2233 ret = av_dict_set(dict, key, val, 0);
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2233 times.
2233 if (ret < 0)
364 return ret;
365 2233 data = val + strlen(val) + 1;
366 }
367
368 536 return 0;
369 }
370
371 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
372 size_t size)
373 {
374 int i;
375
376 for (i = 0; i < pkt->side_data_elems; i++) {
377 if (pkt->side_data[i].type == type) {
378 if (size > pkt->side_data[i].size)
379 return AVERROR(ENOMEM);
380 pkt->side_data[i].size = size;
381 return 0;
382 }
383 }
384 return AVERROR(ENOENT);
385 }
386
387 1244620 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
388 {
389 int i, ret;
390
391 1244620 dst->pts = src->pts;
392 1244620 dst->dts = src->dts;
393 1244620 dst->pos = src->pos;
394 1244620 dst->duration = src->duration;
395 1244620 dst->flags = src->flags;
396 1244620 dst->stream_index = src->stream_index;
397 1244620 dst->opaque = src->opaque;
398 1244620 dst->time_base = src->time_base;
399 1244620 dst->opaque_ref = NULL;
400 1244620 dst->side_data = NULL;
401 1244620 dst->side_data_elems = 0;
402
403 1244620 ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1244620 times.
1244620 if (ret < 0)
405 return ret;
406
407
2/2
✓ Branch 0 taken 19721 times.
✓ Branch 1 taken 1244620 times.
1264341 for (i = 0; i < src->side_data_elems; i++) {
408 19721 enum AVPacketSideDataType type = src->side_data[i].type;
409 19721 size_t size = src->side_data[i].size;
410 19721 uint8_t *src_data = src->side_data[i].data;
411 19721 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
412
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19721 times.
19721 if (!dst_data) {
414 av_buffer_unref(&dst->opaque_ref);
415 av_packet_free_side_data(dst);
416 return AVERROR(ENOMEM);
417 }
418 19721 memcpy(dst_data, src_data, size);
419 }
420
421 1244620 return 0;
422 }
423
424 5810869 void av_packet_unref(AVPacket *pkt)
425 {
426 5810869 av_packet_free_side_data(pkt);
427 5810869 av_buffer_unref(&pkt->opaque_ref);
428 5810869 av_buffer_unref(&pkt->buf);
429 5810869 get_packet_defaults(pkt);
430 5810869 }
431
432 848735 int av_packet_ref(AVPacket *dst, const AVPacket *src)
433 {
434 int ret;
435
436 848735 dst->buf = NULL;
437
438 848735 ret = av_packet_copy_props(dst, src);
439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 848735 times.
848735 if (ret < 0)
440 goto fail;
441
442
2/2
✓ Branch 0 taken 1042 times.
✓ Branch 1 taken 847693 times.
848735 if (!src->buf) {
443 1042 ret = packet_alloc(&dst->buf, src->size);
444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1042 times.
1042 if (ret < 0)
445 goto fail;
446 av_assert1(!src->size || src->data);
447
2/2
✓ Branch 0 taken 1030 times.
✓ Branch 1 taken 12 times.
1042 if (src->size)
448 1030 memcpy(dst->buf->data, src->data, src->size);
449
450 1042 dst->data = dst->buf->data;
451 } else {
452 847693 dst->buf = av_buffer_ref(src->buf);
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 847693 times.
847693 if (!dst->buf) {
454 ret = AVERROR(ENOMEM);
455 goto fail;
456 }
457 847693 dst->data = src->data;
458 }
459
460 848735 dst->size = src->size;
461
462 848735 return 0;
463 fail:
464 av_packet_unref(dst);
465 return ret;
466 }
467
468 6 AVPacket *av_packet_clone(const AVPacket *src)
469 {
470 6 AVPacket *ret = av_packet_alloc();
471
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!ret)
473 return ret;
474
475
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (av_packet_ref(ret, src))
476 av_packet_free(&ret);
477
478 6 return ret;
479 }
480
481 1641498 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
482 {
483 1641498 *dst = *src;
484 1641498 get_packet_defaults(src);
485 1641498 }
486
487 2302902 int av_packet_make_refcounted(AVPacket *pkt)
488 {
489 int ret;
490
491
2/2
✓ Branch 0 taken 2178325 times.
✓ Branch 1 taken 124577 times.
2302902 if (pkt->buf)
492 2178325 return 0;
493
494 124577 ret = packet_alloc(&pkt->buf, pkt->size);
495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124577 times.
124577 if (ret < 0)
496 return ret;
497 av_assert1(!pkt->size || pkt->data);
498
2/2
✓ Branch 0 taken 123096 times.
✓ Branch 1 taken 1481 times.
124577 if (pkt->size)
499 123096 memcpy(pkt->buf->data, pkt->data, pkt->size);
500
501 124577 pkt->data = pkt->buf->data;
502
503 124577 return 0;
504 }
505
506 140 int av_packet_make_writable(AVPacket *pkt)
507 {
508 140 AVBufferRef *buf = NULL;
509 int ret;
510
511
3/4
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 136 times.
140 if (pkt->buf && av_buffer_is_writable(pkt->buf))
512 4 return 0;
513
514 136 ret = packet_alloc(&buf, pkt->size);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (ret < 0)
516 return ret;
517 av_assert1(!pkt->size || pkt->data);
518
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
136 if (pkt->size)
519 136 memcpy(buf->data, pkt->data, pkt->size);
520
521 136 av_buffer_unref(&pkt->buf);
522 136 pkt->buf = buf;
523 136 pkt->data = buf->data;
524
525 136 return 0;
526 }
527
528 856999 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
529 {
530
2/2
✓ Branch 0 taken 851522 times.
✓ Branch 1 taken 5477 times.
856999 if (pkt->pts != AV_NOPTS_VALUE)
531 851522 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
532
2/2
✓ Branch 0 taken 856995 times.
✓ Branch 1 taken 4 times.
856999 if (pkt->dts != AV_NOPTS_VALUE)
533 856995 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
534
2/2
✓ Branch 0 taken 665417 times.
✓ Branch 1 taken 191582 times.
856999 if (pkt->duration > 0)
535 665417 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
536 856999 }
537
538 359826 int avpriv_packet_list_put(PacketList *packet_buffer,
539 AVPacket *pkt,
540 int (*copy)(AVPacket *dst, const AVPacket *src),
541 int flags)
542 {
543 359826 PacketListEntry *pktl = av_malloc(sizeof(*pktl));
544 int ret;
545
546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 359826 times.
359826 if (!pktl)
547 return AVERROR(ENOMEM);
548
549
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 359774 times.
359826 if (copy) {
550 52 get_packet_defaults(&pktl->pkt);
551 52 ret = copy(&pktl->pkt, pkt);
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ret < 0) {
553 av_free(pktl);
554 return ret;
555 }
556 } else {
557 359774 ret = av_packet_make_refcounted(pkt);
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 359774 times.
359774 if (ret < 0) {
559 av_free(pktl);
560 return ret;
561 }
562 359774 av_packet_move_ref(&pktl->pkt, pkt);
563 }
564
565 359826 pktl->next = NULL;
566
567
2/2
✓ Branch 0 taken 212772 times.
✓ Branch 1 taken 147054 times.
359826 if (packet_buffer->head)
568 212772 packet_buffer->tail->next = pktl;
569 else
570 147054 packet_buffer->head = pktl;
571
572 /* Add the packet in the buffered packet list. */
573 359826 packet_buffer->tail = pktl;
574 359826 return 0;
575 }
576
577 288030 int avpriv_packet_list_get(PacketList *pkt_buffer,
578 AVPacket *pkt)
579 {
580 288030 PacketListEntry *pktl = pkt_buffer->head;
581
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 288028 times.
288030 if (!pktl)
582 2 return AVERROR(EAGAIN);
583 288028 *pkt = pktl->pkt;
584 288028 pkt_buffer->head = pktl->next;
585
2/2
✓ Branch 0 taken 143361 times.
✓ Branch 1 taken 144667 times.
288028 if (!pkt_buffer->head)
586 143361 pkt_buffer->tail = NULL;
587 288028 av_freep(&pktl);
588 288028 return 0;
589 }
590
591 54221 void avpriv_packet_list_free(PacketList *pkt_buf)
592 {
593 54221 PacketListEntry *tmp = pkt_buf->head;
594
595
2/2
✓ Branch 0 taken 96268 times.
✓ Branch 1 taken 54221 times.
150489 while (tmp) {
596 96268 PacketListEntry *pktl = tmp;
597 96268 tmp = pktl->next;
598 96268 av_packet_unref(&pktl->pkt);
599 96268 av_freep(&pktl);
600 }
601 54221 pkt_buf->head = pkt_buf->tail = NULL;
602 54221 }
603
604 9538 int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
605 {
606 uint8_t *side_data;
607 size_t side_data_size;
608 int i;
609
610 9538 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
611
1/2
✓ Branch 0 taken 9538 times.
✗ Branch 1 not taken.
9538 if (!side_data) {
612 9538 side_data_size = 4+4+8*error_count;
613 9538 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
614 side_data_size);
615 }
616
617
2/4
✓ Branch 0 taken 9538 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9538 times.
9538 if (!side_data || side_data_size < 4+4+8*error_count)
618 return AVERROR(ENOMEM);
619
620 9538 AV_WL32(side_data , quality );
621 9538 side_data[4] = pict_type;
622 9538 side_data[5] = error_count;
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9538 times.
9538 for (i = 0; i<error_count; i++)
624 AV_WL64(side_data+8 + 8*i , error[i]);
625
626 9538 return 0;
627 }
628
629 int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp)
630 {
631 AVProducerReferenceTime *prft;
632 uint8_t *side_data;
633 size_t side_data_size;
634
635 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &side_data_size);
636 if (!side_data) {
637 side_data_size = sizeof(AVProducerReferenceTime);
638 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT, side_data_size);
639 }
640
641 if (!side_data || side_data_size < sizeof(AVProducerReferenceTime))
642 return AVERROR(ENOMEM);
643
644 prft = (AVProducerReferenceTime *)side_data;
645 prft->wallclock = timestamp;
646 prft->flags = 0;
647
648 return 0;
649 }
650