FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/avpacket.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 297 384 77.3%
Branches: 130 209 62.2%

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 1239510 void av_init_packet(AVPacket *pkt)
38 {
39 1239510 pkt->pts = AV_NOPTS_VALUE;
40 1239510 pkt->dts = AV_NOPTS_VALUE;
41 1239510 pkt->pos = -1;
42 1239510 pkt->duration = 0;
43 1239510 pkt->flags = 0;
44 1239510 pkt->stream_index = 0;
45 1239510 pkt->buf = NULL;
46 1239510 pkt->side_data = NULL;
47 1239510 pkt->side_data_elems = 0;
48 1239510 pkt->opaque = NULL;
49 1239510 pkt->opaque_ref = NULL;
50 1239510 pkt->time_base = av_make_q(0, 1);
51 1239510 }
52 #endif
53
54 9588878 static void get_packet_defaults(AVPacket *pkt)
55 {
56 9588878 memset(pkt, 0, sizeof(*pkt));
57
58 9588878 pkt->pts = AV_NOPTS_VALUE;
59 9588878 pkt->dts = AV_NOPTS_VALUE;
60 9588878 pkt->pos = -1;
61 9588878 pkt->time_base = av_make_q(0, 1);
62 9588878 }
63
64 124377 AVPacket *av_packet_alloc(void)
65 {
66 124377 AVPacket *pkt = av_malloc(sizeof(AVPacket));
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124377 times.
124377 if (!pkt)
68 return pkt;
69
70 124377 get_packet_defaults(pkt);
71
72 124377 return pkt;
73 }
74
75 125323 void av_packet_free(AVPacket **pkt)
76 {
77
3/4
✓ Branch 0 taken 125323 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 946 times.
✓ Branch 3 taken 124377 times.
125323 if (!pkt || !*pkt)
78 946 return;
79
80 124377 av_packet_unref(*pkt);
81 124377 av_freep(pkt);
82 }
83
84 765998 static int packet_alloc(AVBufferRef **buf, int size)
85 {
86 int ret;
87
3/4
✓ Branch 0 taken 765998 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 765997 times.
765998 if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
88 1 return AVERROR(EINVAL);
89
90 765997 ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765997 times.
765997 if (ret < 0)
92 return ret;
93
94 765997 memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
95
96 765997 return 0;
97 }
98
99 643431 int av_new_packet(AVPacket *pkt, int size)
100 {
101 643431 AVBufferRef *buf = NULL;
102 643431 int ret = packet_alloc(&buf, size);
103
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 643430 times.
643431 if (ret < 0)
104 1 return ret;
105
106 643430 get_packet_defaults(pkt);
107 643430 pkt->buf = buf;
108 643430 pkt->data = buf->data;
109 643430 pkt->size = size;
110
111 643430 return 0;
112 }
113
114 505478 void av_shrink_packet(AVPacket *pkt, int size)
115 {
116
2/2
✓ Branch 0 taken 501961 times.
✓ Branch 1 taken 3517 times.
505478 if (pkt->size <= size)
117 501961 return;
118 3517 pkt->size = size;
119 3517 memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
120 }
121
122 284205 int av_grow_packet(AVPacket *pkt, int grow_by)
123 {
124 int new_size;
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 284205 times.
284205 av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
126 284205 if ((unsigned)grow_by >
127
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 284204 times.
284205 INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
128 1 return AVERROR(ENOMEM);
129
130 284204 new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
131
2/2
✓ Branch 0 taken 13306 times.
✓ Branch 1 taken 270898 times.
284204 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 1 times.
✓ Branch 1 taken 13305 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
13307 if (new_size + data_offset > pkt->buf->size ||
144 1 !av_buffer_is_writable(pkt->buf)) {
145 13305 int ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13305 times.
13305 if (ret < 0) {
147 pkt->data = old_data;
148 return ret;
149 }
150 13305 pkt->data = pkt->buf->data + data_offset;
151 }
152 } else {
153 270898 pkt->buf = av_buffer_alloc(new_size);
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270898 times.
270898 if (!pkt->buf)
155 return AVERROR(ENOMEM);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270898 times.
270898 if (pkt->size > 0)
157 memcpy(pkt->buf->data, pkt->data, pkt->size);
158 270898 pkt->data = pkt->buf->data;
159 }
160 284204 pkt->size += grow_by;
161 284204 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
162
163 284204 return 0;
164 }
165
166 3 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
167 {
168
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
169 1 return AVERROR(EINVAL);
170
171 2 pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
172 av_buffer_default_free, NULL, 0);
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!pkt->buf)
174 return AVERROR(ENOMEM);
175
176 2 pkt->data = data;
177 2 pkt->size = size;
178
179 2 return 0;
180 }
181
182 6858242 void av_packet_free_side_data(AVPacket *pkt)
183 {
184 int i;
185
2/2
✓ Branch 0 taken 47921 times.
✓ Branch 1 taken 6858242 times.
6906163 for (i = 0; i < pkt->side_data_elems; i++)
186 47921 av_freep(&pkt->side_data[i].data);
187 6858242 av_freep(&pkt->side_data);
188 6858242 pkt->side_data_elems = 0;
189 6858242 }
190
191 47921 int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
192 uint8_t *data, size_t size)
193 {
194 AVPacketSideData *tmp;
195 47921 int i, elems = pkt->side_data_elems;
196
197
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 47921 times.
48048 for (i = 0; i < elems; i++) {
198 127 AVPacketSideData *sd = &pkt->side_data[i];
199
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
127 if (sd->type == type) {
201 av_free(sd->data);
202 sd->data = data;
203 sd->size = size;
204 return 0;
205 }
206 }
207
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47921 times.
47921 if ((unsigned)elems + 1 > AV_PKT_DATA_NB)
209 return AVERROR(ERANGE);
210
211 47921 tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47921 times.
47921 if (!tmp)
213 return AVERROR(ENOMEM);
214
215 47921 pkt->side_data = tmp;
216 47921 pkt->side_data[elems].data = data;
217 47921 pkt->side_data[elems].size = size;
218 47921 pkt->side_data[elems].type = type;
219 47921 pkt->side_data_elems++;
220
221 47921 return 0;
222 }
223
224
225 46636 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
226 size_t size)
227 {
228 int ret;
229 uint8_t *data;
230
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46636 times.
46636 if (size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
232 return NULL;
233 46636 data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46636 times.
46636 if (!data)
235 return NULL;
236
237 46636 ret = av_packet_add_side_data(pkt, type, data, size);
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46636 times.
46636 if (ret < 0) {
239 av_freep(&data);
240 return NULL;
241 }
242
243 46636 return data;
244 }
245
246 5844505 uint8_t *av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
247 size_t *size)
248 {
249 int i;
250
251
2/2
✓ Branch 0 taken 87138 times.
✓ Branch 1 taken 5834105 times.
5921243 for (i = 0; i < pkt->side_data_elems; i++) {
252
2/2
✓ Branch 0 taken 10400 times.
✓ Branch 1 taken 76738 times.
87138 if (pkt->side_data[i].type == type) {
253
2/2
✓ Branch 0 taken 1061 times.
✓ Branch 1 taken 9339 times.
10400 if (size)
254 1061 *size = pkt->side_data[i].size;
255 10400 return pkt->side_data[i].data;
256 }
257 }
258
2/2
✓ Branch 0 taken 5701415 times.
✓ Branch 1 taken 132690 times.
5834105 if (size)
259 5701415 *size = 0;
260 5834105 return NULL;
261 }
262
263 855 const char *av_packet_side_data_name(enum AVPacketSideDataType type)
264 {
265
13/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 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 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 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
855 switch(type) {
266 case AV_PKT_DATA_PALETTE: return "Palette";
267 3 case AV_PKT_DATA_NEW_EXTRADATA: return "New Extradata";
268 case AV_PKT_DATA_PARAM_CHANGE: return "Param Change";
269 case AV_PKT_DATA_H263_MB_INFO: return "H263 MB Info";
270 case AV_PKT_DATA_REPLAYGAIN: return "Replay Gain";
271 2 case AV_PKT_DATA_DISPLAYMATRIX: return "Display Matrix";
272 5 case AV_PKT_DATA_STEREO3D: return "Stereo 3D";
273 1 case AV_PKT_DATA_AUDIO_SERVICE_TYPE: return "Audio Service Type";
274 case AV_PKT_DATA_QUALITY_STATS: return "Quality stats";
275 case AV_PKT_DATA_FALLBACK_TRACK: return "Fallback track";
276 2 case AV_PKT_DATA_CPB_PROPERTIES: return "CPB properties";
277 10 case AV_PKT_DATA_SKIP_SAMPLES: return "Skip Samples";
278 case AV_PKT_DATA_JP_DUALMONO: return "JP Dual Mono";
279 131 case AV_PKT_DATA_STRINGS_METADATA: return "Strings Metadata";
280 case AV_PKT_DATA_SUBTITLE_POSITION: return "Subtitle Position";
281 case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL: return "Matroska BlockAdditional";
282 8 case AV_PKT_DATA_WEBVTT_IDENTIFIER: return "WebVTT ID";
283 16 case AV_PKT_DATA_WEBVTT_SETTINGS: return "WebVTT Settings";
284 case AV_PKT_DATA_METADATA_UPDATE: return "Metadata Update";
285 667 case AV_PKT_DATA_MPEGTS_STREAM_ID: return "MPEGTS Stream ID";
286 3 case AV_PKT_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
287 3 case AV_PKT_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
288 4 case AV_PKT_DATA_SPHERICAL: return "Spherical Mapping";
289 case AV_PKT_DATA_A53_CC: return "A53 Closed Captions";
290 case AV_PKT_DATA_ENCRYPTION_INIT_INFO: return "Encryption initialization data";
291 case AV_PKT_DATA_ENCRYPTION_INFO: return "Encryption info";
292 case AV_PKT_DATA_AFD: return "Active Format Description data";
293 case AV_PKT_DATA_PRFT: return "Producer Reference Time";
294 case AV_PKT_DATA_ICC_PROFILE: return "ICC Profile";
295 case AV_PKT_DATA_DOVI_CONF: return "DOVI configuration record";
296 case AV_PKT_DATA_S12M_TIMECODE: return "SMPTE ST 12-1:2014 timecode";
297 case AV_PKT_DATA_DYNAMIC_HDR10_PLUS: return "HDR10+ Dynamic Metadata (SMPTE 2094-40)";
298 }
299 return NULL;
300 }
301
302 577 uint8_t *av_packet_pack_dictionary(AVDictionary *dict, size_t *size)
303 {
304 577 uint8_t *data = NULL;
305 577 *size = 0;
306
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 577 times.
577 if (!dict)
308 return NULL;
309
310
1/2
✓ Branch 0 taken 1154 times.
✗ Branch 1 not taken.
1154 for (int pass = 0; pass < 2; pass++) {
311 1154 const AVDictionaryEntry *t = NULL;
312 1154 size_t total_length = 0;
313
314
2/2
✓ Branch 1 taken 4618 times.
✓ Branch 2 taken 1154 times.
5772 while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX))) {
315
2/2
✓ Branch 0 taken 9236 times.
✓ Branch 1 taken 4618 times.
13854 for (int i = 0; i < 2; i++) {
316
2/2
✓ Branch 0 taken 4618 times.
✓ Branch 1 taken 4618 times.
9236 const char *str = i ? t->value : t->key;
317 9236 const size_t len = strlen(str) + 1;
318
319
2/2
✓ Branch 0 taken 4618 times.
✓ Branch 1 taken 4618 times.
9236 if (pass)
320 4618 memcpy(data + total_length, str, len);
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4618 times.
4618 else if (len > SIZE_MAX - total_length)
322 return NULL;
323 9236 total_length += len;
324 }
325 }
326
2/2
✓ Branch 0 taken 577 times.
✓ Branch 1 taken 577 times.
1154 if (pass)
327 577 break;
328 577 data = av_malloc(total_length);
329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 577 times.
577 if (!data)
330 return NULL;
331 577 *size = total_length;
332 }
333
334 577 return data;
335 }
336
337 406872 int av_packet_unpack_dictionary(const uint8_t *data, size_t size,
338 AVDictionary **dict)
339 {
340 const uint8_t *end;
341 int ret;
342
343
4/6
✓ Branch 0 taken 406872 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 536 times.
✓ Branch 3 taken 406336 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 536 times.
406872 if (!dict || !data || !size)
344 406336 return 0;
345 536 end = data + size;
346
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])
347 return AVERROR_INVALIDDATA;
348
2/2
✓ Branch 0 taken 2233 times.
✓ Branch 1 taken 536 times.
2769 while (data < end) {
349 2233 const uint8_t *key = data;
350 2233 const uint8_t *val = data + strlen(key) + 1;
351
352
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)
353 return AVERROR_INVALIDDATA;
354
355 2233 ret = av_dict_set(dict, key, val, 0);
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2233 times.
2233 if (ret < 0)
357 return ret;
358 2233 data = val + strlen(val) + 1;
359 }
360
361 536 return 0;
362 }
363
364 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
365 size_t size)
366 {
367 int i;
368
369 for (i = 0; i < pkt->side_data_elems; i++) {
370 if (pkt->side_data[i].type == type) {
371 if (size > pkt->side_data[i].size)
372 return AVERROR(ENOMEM);
373 pkt->side_data[i].size = size;
374 return 0;
375 }
376 }
377 return AVERROR(ENOENT);
378 }
379
380 1196190 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
381 {
382 int i, ret;
383
384 1196190 dst->pts = src->pts;
385 1196190 dst->dts = src->dts;
386 1196190 dst->pos = src->pos;
387 1196190 dst->duration = src->duration;
388 1196190 dst->flags = src->flags;
389 1196190 dst->stream_index = src->stream_index;
390 1196190 dst->opaque = src->opaque;
391 1196190 dst->time_base = src->time_base;
392 1196190 dst->opaque_ref = NULL;
393 1196190 dst->side_data = NULL;
394 1196190 dst->side_data_elems = 0;
395
396 1196190 ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1196190 times.
1196190 if (ret < 0)
398 return ret;
399
400
2/2
✓ Branch 0 taken 17508 times.
✓ Branch 1 taken 1196190 times.
1213698 for (i = 0; i < src->side_data_elems; i++) {
401 17508 enum AVPacketSideDataType type = src->side_data[i].type;
402 17508 size_t size = src->side_data[i].size;
403 17508 uint8_t *src_data = src->side_data[i].data;
404 17508 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
405
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17508 times.
17508 if (!dst_data) {
407 av_buffer_unref(&dst->opaque_ref);
408 av_packet_free_side_data(dst);
409 return AVERROR(ENOMEM);
410 }
411 17508 memcpy(dst_data, src_data, size);
412 }
413
414 1196190 return 0;
415 }
416
417 6858242 void av_packet_unref(AVPacket *pkt)
418 {
419 6858242 av_packet_free_side_data(pkt);
420 6858242 av_buffer_unref(&pkt->opaque_ref);
421 6858242 av_buffer_unref(&pkt->buf);
422 6858242 get_packet_defaults(pkt);
423 6858242 }
424
425 807367 int av_packet_ref(AVPacket *dst, const AVPacket *src)
426 {
427 int ret;
428
429 807367 dst->buf = NULL;
430
431 807367 ret = av_packet_copy_props(dst, src);
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 807367 times.
807367 if (ret < 0)
433 goto fail;
434
435
2/2
✓ Branch 0 taken 1042 times.
✓ Branch 1 taken 806325 times.
807367 if (!src->buf) {
436 1042 ret = packet_alloc(&dst->buf, src->size);
437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1042 times.
1042 if (ret < 0)
438 goto fail;
439 av_assert1(!src->size || src->data);
440
2/2
✓ Branch 0 taken 1030 times.
✓ Branch 1 taken 12 times.
1042 if (src->size)
441 1030 memcpy(dst->buf->data, src->data, src->size);
442
443 1042 dst->data = dst->buf->data;
444 } else {
445 806325 dst->buf = av_buffer_ref(src->buf);
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 806325 times.
806325 if (!dst->buf) {
447 ret = AVERROR(ENOMEM);
448 goto fail;
449 }
450 806325 dst->data = src->data;
451 }
452
453 807367 dst->size = src->size;
454
455 807367 return 0;
456 fail:
457 av_packet_unref(dst);
458 return ret;
459 }
460
461 1 AVPacket *av_packet_clone(const AVPacket *src)
462 {
463 1 AVPacket *ret = av_packet_alloc();
464
465
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ret)
466 return ret;
467
468
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (av_packet_ref(ret, src))
469 av_packet_free(&ret);
470
471 1 return ret;
472 }
473
474 1962705 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
475 {
476 1962705 *dst = *src;
477 1962705 get_packet_defaults(src);
478 1962705 }
479
480 2609598 int av_packet_make_refcounted(AVPacket *pkt)
481 {
482 int ret;
483
484
2/2
✓ Branch 0 taken 2488209 times.
✓ Branch 1 taken 121389 times.
2609598 if (pkt->buf)
485 2488209 return 0;
486
487 121389 ret = packet_alloc(&pkt->buf, pkt->size);
488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 121389 times.
121389 if (ret < 0)
489 return ret;
490 av_assert1(!pkt->size || pkt->data);
491
2/2
✓ Branch 0 taken 119901 times.
✓ Branch 1 taken 1488 times.
121389 if (pkt->size)
492 119901 memcpy(pkt->buf->data, pkt->data, pkt->size);
493
494 121389 pkt->data = pkt->buf->data;
495
496 121389 return 0;
497 }
498
499 140 int av_packet_make_writable(AVPacket *pkt)
500 {
501 140 AVBufferRef *buf = NULL;
502 int ret;
503
504
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))
505 4 return 0;
506
507 136 ret = packet_alloc(&buf, pkt->size);
508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (ret < 0)
509 return ret;
510 av_assert1(!pkt->size || pkt->data);
511
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
136 if (pkt->size)
512 136 memcpy(buf->data, pkt->data, pkt->size);
513
514 136 av_buffer_unref(&pkt->buf);
515 136 pkt->buf = buf;
516 136 pkt->data = buf->data;
517
518 136 return 0;
519 }
520
521 833061 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
522 {
523
2/2
✓ Branch 0 taken 827592 times.
✓ Branch 1 taken 5469 times.
833061 if (pkt->pts != AV_NOPTS_VALUE)
524 827592 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
525
2/2
✓ Branch 0 taken 833057 times.
✓ Branch 1 taken 4 times.
833061 if (pkt->dts != AV_NOPTS_VALUE)
526 833057 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
527
2/2
✓ Branch 0 taken 642722 times.
✓ Branch 1 taken 190339 times.
833061 if (pkt->duration > 0)
528 642722 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
529 833061 }
530
531 350852 int avpriv_packet_list_put(PacketList **packet_buffer,
532 PacketList **plast_pktl,
533 AVPacket *pkt,
534 int (*copy)(AVPacket *dst, const AVPacket *src),
535 int flags)
536 {
537 350852 PacketList *pktl = av_malloc(sizeof(PacketList));
538 int ret;
539
540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 350852 times.
350852 if (!pktl)
541 return AVERROR(ENOMEM);
542
543
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 350728 times.
350852 if (copy) {
544 124 get_packet_defaults(&pktl->pkt);
545 124 ret = copy(&pktl->pkt, pkt);
546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
124 if (ret < 0) {
547 av_free(pktl);
548 return ret;
549 }
550 } else {
551 350728 ret = av_packet_make_refcounted(pkt);
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 350728 times.
350728 if (ret < 0) {
553 av_free(pktl);
554 return ret;
555 }
556 350728 av_packet_move_ref(&pktl->pkt, pkt);
557 }
558
559 350852 pktl->next = NULL;
560
561
2/2
✓ Branch 0 taken 206025 times.
✓ Branch 1 taken 144827 times.
350852 if (*packet_buffer)
562 206025 (*plast_pktl)->next = pktl;
563 else
564 144827 *packet_buffer = pktl;
565
566 /* Add the packet in the buffered packet list. */
567 350852 *plast_pktl = pktl;
568 350852 return 0;
569 }
570
571 257776 int avpriv_packet_list_get(PacketList **pkt_buffer,
572 PacketList **pkt_buffer_end,
573 AVPacket *pkt)
574 {
575 PacketList *pktl;
576
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 257774 times.
257776 if (!*pkt_buffer)
577 2 return AVERROR(EAGAIN);
578 257774 pktl = *pkt_buffer;
579 257774 *pkt = pktl->pkt;
580 257774 *pkt_buffer = pktl->next;
581
2/2
✓ Branch 0 taken 140987 times.
✓ Branch 1 taken 116787 times.
257774 if (!pktl->next)
582 140987 *pkt_buffer_end = NULL;
583 257774 av_freep(&pktl);
584 257774 return 0;
585 }
586
587 53109 void avpriv_packet_list_free(PacketList **pkt_buf, PacketList **pkt_buf_end)
588 {
589 53109 PacketList *tmp = *pkt_buf;
590
591
2/2
✓ Branch 0 taken 93078 times.
✓ Branch 1 taken 53109 times.
146187 while (tmp) {
592 93078 PacketList *pktl = tmp;
593 93078 tmp = pktl->next;
594 93078 av_packet_unref(&pktl->pkt);
595 93078 av_freep(&pktl);
596 }
597 53109 *pkt_buf = NULL;
598 53109 *pkt_buf_end = NULL;
599 53109 }
600
601 9333 int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
602 {
603 uint8_t *side_data;
604 size_t side_data_size;
605 int i;
606
607 9333 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
608
1/2
✓ Branch 0 taken 9333 times.
✗ Branch 1 not taken.
9333 if (!side_data) {
609 9333 side_data_size = 4+4+8*error_count;
610 9333 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
611 side_data_size);
612 }
613
614
2/4
✓ Branch 0 taken 9333 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9333 times.
9333 if (!side_data || side_data_size < 4+4+8*error_count)
615 return AVERROR(ENOMEM);
616
617 9333 AV_WL32(side_data , quality );
618 9333 side_data[4] = pict_type;
619 9333 side_data[5] = error_count;
620
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9333 times.
9333 for (i = 0; i<error_count; i++)
621 AV_WL64(side_data+8 + 8*i , error[i]);
622
623 9333 return 0;
624 }
625
626 int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp)
627 {
628 AVProducerReferenceTime *prft;
629 uint8_t *side_data;
630 size_t side_data_size;
631
632 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &side_data_size);
633 if (!side_data) {
634 side_data_size = sizeof(AVProducerReferenceTime);
635 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT, side_data_size);
636 }
637
638 if (!side_data || side_data_size < sizeof(AVProducerReferenceTime))
639 return AVERROR(ENOMEM);
640
641 prft = (AVProducerReferenceTime *)side_data;
642 prft->wallclock = timestamp;
643 prft->flags = 0;
644
645 return 0;
646 }
647