FFmpeg coverage


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