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
30 #include "bytestream.h"
31 #include "internal.h"
32 #include "packet.h"
33 #include "packet_internal.h"
34
35 #if FF_API_INIT_PACKET
av_init_packet(AVPacket * pkt)36 void av_init_packet(AVPacket *pkt)
37 {
38 pkt->pts = AV_NOPTS_VALUE;
39 pkt->dts = AV_NOPTS_VALUE;
40 pkt->pos = -1;
41 pkt->duration = 0;
42 #if FF_API_CONVERGENCE_DURATION
43 FF_DISABLE_DEPRECATION_WARNINGS
44 pkt->convergence_duration = 0;
45 FF_ENABLE_DEPRECATION_WARNINGS
46 #endif
47 pkt->flags = 0;
48 pkt->stream_index = 0;
49 pkt->buf = NULL;
50 pkt->side_data = NULL;
51 pkt->side_data_elems = 0;
52 }
53 #endif
54
get_packet_defaults(AVPacket * pkt)55 static void get_packet_defaults(AVPacket *pkt)
56 {
57 memset(pkt, 0, sizeof(*pkt));
58
59 pkt->pts = AV_NOPTS_VALUE;
60 pkt->dts = AV_NOPTS_VALUE;
61 pkt->pos = -1;
62 }
63
av_packet_alloc(void)64 AVPacket *av_packet_alloc(void)
65 {
66 AVPacket *pkt = av_mallocz(sizeof(AVPacket));
67 if (!pkt)
68 return pkt;
69
70 get_packet_defaults(pkt);
71
72 return pkt;
73 }
74
av_packet_free(AVPacket ** pkt)75 void av_packet_free(AVPacket **pkt)
76 {
77 if (!pkt || !*pkt)
78 return;
79
80 av_packet_unref(*pkt);
81 av_freep(pkt);
82 }
83
packet_alloc(AVBufferRef ** buf,int size)84 static int packet_alloc(AVBufferRef **buf, int size)
85 {
86 int ret;
87 if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
88 return AVERROR(EINVAL);
89
90 ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
91 if (ret < 0)
92 return ret;
93
94 memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
95
96 return 0;
97 }
98
av_new_packet(AVPacket * pkt,int size)99 int av_new_packet(AVPacket *pkt, int size)
100 {
101 AVBufferRef *buf = NULL;
102 int ret = packet_alloc(&buf, size);
103 if (ret < 0)
104 return ret;
105
106 get_packet_defaults(pkt);
107 pkt->buf = buf;
108 pkt->data = buf->data;
109 pkt->size = size;
110
111 return 0;
112 }
113
av_shrink_packet(AVPacket * pkt,int size)114 void av_shrink_packet(AVPacket *pkt, int size)
115 {
116 if (pkt->size <= size)
117 return;
118 pkt->size = size;
119 memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
120 }
121
av_grow_packet(AVPacket * pkt,int grow_by)122 int av_grow_packet(AVPacket *pkt, int grow_by)
123 {
124 int new_size;
125 av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
126 if ((unsigned)grow_by >
127 INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
128 return AVERROR(ENOMEM);
129
130 new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
131 if (pkt->buf) {
132 size_t data_offset;
133 uint8_t *old_data = pkt->data;
134 if (pkt->data == NULL) {
135 data_offset = 0;
136 pkt->data = pkt->buf->data;
137 } else {
138 data_offset = pkt->data - pkt->buf->data;
139 if (data_offset > INT_MAX - new_size)
140 return AVERROR(ENOMEM);
141 }
142
143 if (new_size + data_offset > pkt->buf->size ||
144 !av_buffer_is_writable(pkt->buf)) {
145 int ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
146 if (ret < 0) {
147 pkt->data = old_data;
148 return ret;
149 }
150 pkt->data = pkt->buf->data + data_offset;
151 }
152 } else {
153 pkt->buf = av_buffer_alloc(new_size);
154 if (!pkt->buf)
155 return AVERROR(ENOMEM);
156 if (pkt->size > 0)
157 memcpy(pkt->buf->data, pkt->data, pkt->size);
158 pkt->data = pkt->buf->data;
159 }
160 pkt->size += grow_by;
161 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
162
163 return 0;
164 }
165
av_packet_from_data(AVPacket * pkt,uint8_t * data,int size)166 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
167 {
168 if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
169 return AVERROR(EINVAL);
170
171 pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
172 av_buffer_default_free, NULL, 0);
173 if (!pkt->buf)
174 return AVERROR(ENOMEM);
175
176 pkt->data = data;
177 pkt->size = size;
178
179 return 0;
180 }
181
182 #if FF_API_AVPACKET_OLD_API
183 FF_DISABLE_DEPRECATION_WARNINGS
184 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
185 #define ALLOC_BUF(data, size) \
186 do { \
187 av_buffer_realloc(&pkt->buf, size); \
188 data = pkt->buf ? pkt->buf->data : NULL; \
189 } while (0)
190
191 #define DUP_DATA(dst, src, size, padding, ALLOC) \
192 do { \
193 void *data; \
194 if (padding) { \
195 if ((unsigned)(size) > \
196 (unsigned)(size) + AV_INPUT_BUFFER_PADDING_SIZE) \
197 goto failed_alloc; \
198 ALLOC(data, size + AV_INPUT_BUFFER_PADDING_SIZE); \
199 } else { \
200 ALLOC(data, size); \
201 } \
202 if (!data) \
203 goto failed_alloc; \
204 memcpy(data, src, size); \
205 if (padding) \
206 memset((uint8_t *)data + size, 0, \
207 AV_INPUT_BUFFER_PADDING_SIZE); \
208 dst = data; \
209 } while (0)
210
211 /* Makes duplicates of data, side_data, but does not copy any other fields */
copy_packet_data(AVPacket * pkt,const AVPacket * src,int dup)212 static int copy_packet_data(AVPacket *pkt, const AVPacket *src, int dup)
213 {
214 pkt->data = NULL;
215 pkt->side_data = NULL;
216 pkt->side_data_elems = 0;
217 if (pkt->buf) {
218 AVBufferRef *ref = av_buffer_ref(src->buf);
219 if (!ref)
220 return AVERROR(ENOMEM);
221 pkt->buf = ref;
222 pkt->data = ref->data;
223 } else {
224 DUP_DATA(pkt->data, src->data, pkt->size, 1, ALLOC_BUF);
225 }
226 if (src->side_data_elems && dup) {
227 pkt->side_data = src->side_data;
228 pkt->side_data_elems = src->side_data_elems;
229 }
230 if (src->side_data_elems && !dup) {
231 return av_copy_packet_side_data(pkt, src);
232 }
233 return 0;
234
235 failed_alloc:
236 av_packet_unref(pkt);
237 return AVERROR(ENOMEM);
238 }
239
av_copy_packet_side_data(AVPacket * pkt,const AVPacket * src)240 int av_copy_packet_side_data(AVPacket *pkt, const AVPacket *src)
241 {
242 if (src->side_data_elems) {
243 int i;
244 DUP_DATA(pkt->side_data, src->side_data,
245 src->side_data_elems * sizeof(*src->side_data), 0, ALLOC_MALLOC);
246 if (src != pkt) {
247 memset(pkt->side_data, 0,
248 src->side_data_elems * sizeof(*src->side_data));
249 }
250 for (i = 0; i < src->side_data_elems; i++) {
251 DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
252 src->side_data[i].size, 1, ALLOC_MALLOC);
253 pkt->side_data[i].size = src->side_data[i].size;
254 pkt->side_data[i].type = src->side_data[i].type;
255 }
256 }
257 pkt->side_data_elems = src->side_data_elems;
258 return 0;
259
260 failed_alloc:
261 av_packet_unref(pkt);
262 return AVERROR(ENOMEM);
263 }
264
av_dup_packet(AVPacket * pkt)265 int av_dup_packet(AVPacket *pkt)
266 {
267 AVPacket tmp_pkt;
268
269 if (!pkt->buf && pkt->data) {
270 tmp_pkt = *pkt;
271 return copy_packet_data(pkt, &tmp_pkt, 1);
272 }
273 return 0;
274 }
275
av_copy_packet(AVPacket * dst,const AVPacket * src)276 int av_copy_packet(AVPacket *dst, const AVPacket *src)
277 {
278 *dst = *src;
279 return copy_packet_data(dst, src, 0);
280 }
281 FF_ENABLE_DEPRECATION_WARNINGS
282 #endif
283
av_packet_free_side_data(AVPacket * pkt)284 void av_packet_free_side_data(AVPacket *pkt)
285 {
286 int i;
287 for (i = 0; i < pkt->side_data_elems; i++)
288 av_freep(&pkt->side_data[i].data);
289 av_freep(&pkt->side_data);
290 pkt->side_data_elems = 0;
291 }
292
293 #if FF_API_AVPACKET_OLD_API
294 FF_DISABLE_DEPRECATION_WARNINGS
av_free_packet(AVPacket * pkt)295 void av_free_packet(AVPacket *pkt)
296 {
297 if (pkt) {
298 if (pkt->buf)
299 av_buffer_unref(&pkt->buf);
300 pkt->data = NULL;
301 pkt->size = 0;
302
303 av_packet_free_side_data(pkt);
304 }
305 }
306 FF_ENABLE_DEPRECATION_WARNINGS
307 #endif
308
av_packet_add_side_data(AVPacket * pkt,enum AVPacketSideDataType type,uint8_t * data,size_t size)309 int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
310 uint8_t *data, size_t size)
311 {
312 AVPacketSideData *tmp;
313 int i, elems = pkt->side_data_elems;
314
315 for (i = 0; i < elems; i++) {
316 AVPacketSideData *sd = &pkt->side_data[i];
317
318 if (sd->type == type) {
319 av_free(sd->data);
320 sd->data = data;
321 sd->size = size;
322 return 0;
323 }
324 }
325
326 if ((unsigned)elems + 1 > AV_PKT_DATA_NB)
327 return AVERROR(ERANGE);
328
329 tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
330 if (!tmp)
331 return AVERROR(ENOMEM);
332
333 pkt->side_data = tmp;
334 pkt->side_data[elems].data = data;
335 pkt->side_data[elems].size = size;
336 pkt->side_data[elems].type = type;
337 pkt->side_data_elems++;
338
339 return 0;
340 }
341
342
av_packet_new_side_data(AVPacket * pkt,enum AVPacketSideDataType type,buffer_size_t size)343 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
344 buffer_size_t size)
345 {
346 int ret;
347 uint8_t *data;
348
349 #if FF_API_BUFFER_SIZE_T
350 if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
351 #else
352 if (size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
353 #endif
354 return NULL;
355 data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
356 if (!data)
357 return NULL;
358
359 ret = av_packet_add_side_data(pkt, type, data, size);
360 if (ret < 0) {
361 av_freep(&data);
362 return NULL;
363 }
364
365 return data;
366 }
367
av_packet_get_side_data(const AVPacket * pkt,enum AVPacketSideDataType type,buffer_size_t * size)368 uint8_t *av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
369 buffer_size_t *size)
370 {
371 int i;
372
373 for (i = 0; i < pkt->side_data_elems; i++) {
374 if (pkt->side_data[i].type == type) {
375 if (size)
376 *size = pkt->side_data[i].size;
377 return pkt->side_data[i].data;
378 }
379 }
380 if (size)
381 *size = 0;
382 return NULL;
383 }
384
av_packet_side_data_name(enum AVPacketSideDataType type)385 const char *av_packet_side_data_name(enum AVPacketSideDataType type)
386 {
387 switch(type) {
388 case AV_PKT_DATA_PALETTE: return "Palette";
389 case AV_PKT_DATA_NEW_EXTRADATA: return "New Extradata";
390 case AV_PKT_DATA_PARAM_CHANGE: return "Param Change";
391 case AV_PKT_DATA_H263_MB_INFO: return "H263 MB Info";
392 case AV_PKT_DATA_REPLAYGAIN: return "Replay Gain";
393 case AV_PKT_DATA_DISPLAYMATRIX: return "Display Matrix";
394 case AV_PKT_DATA_STEREO3D: return "Stereo 3D";
395 case AV_PKT_DATA_AUDIO_SERVICE_TYPE: return "Audio Service Type";
396 case AV_PKT_DATA_QUALITY_STATS: return "Quality stats";
397 case AV_PKT_DATA_FALLBACK_TRACK: return "Fallback track";
398 case AV_PKT_DATA_CPB_PROPERTIES: return "CPB properties";
399 case AV_PKT_DATA_SKIP_SAMPLES: return "Skip Samples";
400 case AV_PKT_DATA_JP_DUALMONO: return "JP Dual Mono";
401 case AV_PKT_DATA_STRINGS_METADATA: return "Strings Metadata";
402 case AV_PKT_DATA_SUBTITLE_POSITION: return "Subtitle Position";
403 case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL: return "Matroska BlockAdditional";
404 case AV_PKT_DATA_WEBVTT_IDENTIFIER: return "WebVTT ID";
405 case AV_PKT_DATA_WEBVTT_SETTINGS: return "WebVTT Settings";
406 case AV_PKT_DATA_METADATA_UPDATE: return "Metadata Update";
407 case AV_PKT_DATA_MPEGTS_STREAM_ID: return "MPEGTS Stream ID";
408 case AV_PKT_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
409 case AV_PKT_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
410 case AV_PKT_DATA_SPHERICAL: return "Spherical Mapping";
411 case AV_PKT_DATA_A53_CC: return "A53 Closed Captions";
412 case AV_PKT_DATA_ENCRYPTION_INIT_INFO: return "Encryption initialization data";
413 case AV_PKT_DATA_ENCRYPTION_INFO: return "Encryption info";
414 case AV_PKT_DATA_AFD: return "Active Format Description data";
415 case AV_PKT_DATA_PRFT: return "Producer Reference Time";
416 case AV_PKT_DATA_ICC_PROFILE: return "ICC Profile";
417 case AV_PKT_DATA_DOVI_CONF: return "DOVI configuration record";
418 case AV_PKT_DATA_S12M_TIMECODE: return "SMPTE ST 12-1:2014 timecode";
419 }
420 return NULL;
421 }
422
423 #if FF_API_MERGE_SD_API
424
425 #define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
426
av_packet_merge_side_data(AVPacket * pkt)427 int av_packet_merge_side_data(AVPacket *pkt){
428 if(pkt->side_data_elems){
429 AVBufferRef *buf;
430 int i;
431 uint8_t *p;
432 uint64_t size= pkt->size + 8LL + AV_INPUT_BUFFER_PADDING_SIZE;
433 AVPacket old= *pkt;
434 for (i=0; i<old.side_data_elems; i++) {
435 size += old.side_data[i].size + 5LL;
436 }
437 if (size > INT_MAX)
438 return AVERROR(EINVAL);
439 buf = av_buffer_alloc(size);
440 if (!buf)
441 return AVERROR(ENOMEM);
442 pkt->buf = buf;
443 pkt->data = p = buf->data;
444 pkt->size = size - AV_INPUT_BUFFER_PADDING_SIZE;
445 bytestream_put_buffer(&p, old.data, old.size);
446 for (i=old.side_data_elems-1; i>=0; i--) {
447 bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
448 bytestream_put_be32(&p, old.side_data[i].size);
449 *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
450 }
451 bytestream_put_be64(&p, FF_MERGE_MARKER);
452 av_assert0(p-pkt->data == pkt->size);
453 memset(p, 0, AV_INPUT_BUFFER_PADDING_SIZE);
454 av_packet_unref(&old);
455 pkt->side_data_elems = 0;
456 pkt->side_data = NULL;
457 return 1;
458 }
459 return 0;
460 }
461
av_packet_split_side_data(AVPacket * pkt)462 int av_packet_split_side_data(AVPacket *pkt){
463 if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
464 int i;
465 unsigned int size;
466 uint8_t *p;
467
468 p = pkt->data + pkt->size - 8 - 5;
469 for (i=1; ; i++){
470 size = AV_RB32(p);
471 if (size>INT_MAX - 5 || p - pkt->data < size)
472 return 0;
473 if (p[4]&128)
474 break;
475 if (p - pkt->data < size + 5)
476 return 0;
477 p-= size+5;
478 }
479
480 if (i > AV_PKT_DATA_NB)
481 return AVERROR(ERANGE);
482
483 pkt->side_data = av_malloc_array(i, sizeof(*pkt->side_data));
484 if (!pkt->side_data)
485 return AVERROR(ENOMEM);
486
487 p= pkt->data + pkt->size - 8 - 5;
488 for (i=0; ; i++){
489 size= AV_RB32(p);
490 av_assert0(size<=INT_MAX - 5 && p - pkt->data >= size);
491 pkt->side_data[i].data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
492 pkt->side_data[i].size = size;
493 pkt->side_data[i].type = p[4]&127;
494 if (!pkt->side_data[i].data)
495 return AVERROR(ENOMEM);
496 memcpy(pkt->side_data[i].data, p-size, size);
497 pkt->size -= size + 5;
498 if(p[4]&128)
499 break;
500 p-= size+5;
501 }
502 pkt->size -= 8;
503 pkt->side_data_elems = i+1;
504 return 1;
505 }
506 return 0;
507 }
508 #endif
509
510 #if FF_API_BUFFER_SIZE_T
av_packet_pack_dictionary(AVDictionary * dict,int * size)511 uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size)
512 #else
513 uint8_t *av_packet_pack_dictionary(AVDictionary *dict, size_t *size)
514 #endif
515 {
516 uint8_t *data = NULL;
517 *size = 0;
518
519 if (!dict)
520 return NULL;
521
522 for (int pass = 0; pass < 2; pass++) {
523 const AVDictionaryEntry *t = NULL;
524 size_t total_length = 0;
525
526 while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX))) {
527 for (int i = 0; i < 2; i++) {
528 const char *str = i ? t->value : t->key;
529 const size_t len = strlen(str) + 1;
530
531 if (pass)
532 memcpy(data + total_length, str, len);
533 #if FF_API_BUFFER_SIZE_T
534 else if (len > INT_MAX - total_length)
535 #else
536 else if (len > SIZE_MAX - total_length)
537 #endif
538 return NULL;
539 total_length += len;
540 }
541 }
542 if (pass)
543 break;
544 data = av_malloc(total_length);
545 if (!data)
546 return NULL;
547 *size = total_length;
548 }
549
550 return data;
551 }
552
553 #if FF_API_BUFFER_SIZE_T
av_packet_unpack_dictionary(const uint8_t * data,int size,AVDictionary ** dict)554 int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict)
555 #else
556 int av_packet_unpack_dictionary(const uint8_t *data, size_t size,
557 AVDictionary **dict)
558 #endif
559 {
560 const uint8_t *end;
561 int ret;
562
563 if (!dict || !data || !size)
564 return 0;
565 end = data + size;
566 if (size && end[-1])
567 return AVERROR_INVALIDDATA;
568 while (data < end) {
569 const uint8_t *key = data;
570 const uint8_t *val = data + strlen(key) + 1;
571
572 if (val >= end || !*key)
573 return AVERROR_INVALIDDATA;
574
575 ret = av_dict_set(dict, key, val, 0);
576 if (ret < 0)
577 return ret;
578 data = val + strlen(val) + 1;
579 }
580
581 return 0;
582 }
583
av_packet_shrink_side_data(AVPacket * pkt,enum AVPacketSideDataType type,buffer_size_t size)584 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
585 buffer_size_t size)
586 {
587 int i;
588
589 for (i = 0; i < pkt->side_data_elems; i++) {
590 if (pkt->side_data[i].type == type) {
591 if (size > pkt->side_data[i].size)
592 return AVERROR(ENOMEM);
593 pkt->side_data[i].size = size;
594 return 0;
595 }
596 }
597 return AVERROR(ENOENT);
598 }
599
av_packet_copy_props(AVPacket * dst,const AVPacket * src)600 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
601 {
602 int i;
603
604 dst->pts = src->pts;
605 dst->dts = src->dts;
606 dst->pos = src->pos;
607 dst->duration = src->duration;
608 #if FF_API_CONVERGENCE_DURATION
609 FF_DISABLE_DEPRECATION_WARNINGS
610 dst->convergence_duration = src->convergence_duration;
611 FF_ENABLE_DEPRECATION_WARNINGS
612 #endif
613 dst->flags = src->flags;
614 dst->stream_index = src->stream_index;
615
616 dst->side_data = NULL;
617 dst->side_data_elems = 0;
618 for (i = 0; i < src->side_data_elems; i++) {
619 enum AVPacketSideDataType type = src->side_data[i].type;
620 buffer_size_t size = src->side_data[i].size;
621 uint8_t *src_data = src->side_data[i].data;
622 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
623
624 if (!dst_data) {
625 av_packet_free_side_data(dst);
626 return AVERROR(ENOMEM);
627 }
628 memcpy(dst_data, src_data, size);
629 }
630
631 return 0;
632 }
633
av_packet_unref(AVPacket * pkt)634 void av_packet_unref(AVPacket *pkt)
635 {
636 av_packet_free_side_data(pkt);
637 av_buffer_unref(&pkt->buf);
638 get_packet_defaults(pkt);
639 }
640
av_packet_ref(AVPacket * dst,const AVPacket * src)641 int av_packet_ref(AVPacket *dst, const AVPacket *src)
642 {
643 int ret;
644
645 dst->buf = NULL;
646
647 ret = av_packet_copy_props(dst, src);
648 if (ret < 0)
649 goto fail;
650
651 if (!src->buf) {
652 ret = packet_alloc(&dst->buf, src->size);
653 if (ret < 0)
654 goto fail;
655 av_assert1(!src->size || src->data);
656 if (src->size)
657 memcpy(dst->buf->data, src->data, src->size);
658
659 dst->data = dst->buf->data;
660 } else {
661 dst->buf = av_buffer_ref(src->buf);
662 if (!dst->buf) {
663 ret = AVERROR(ENOMEM);
664 goto fail;
665 }
666 dst->data = src->data;
667 }
668
669 dst->size = src->size;
670
671 return 0;
672 fail:
673 av_packet_unref(dst);
674 return ret;
675 }
676
av_packet_clone(const AVPacket * src)677 AVPacket *av_packet_clone(const AVPacket *src)
678 {
679 AVPacket *ret = av_packet_alloc();
680
681 if (!ret)
682 return ret;
683
684 if (av_packet_ref(ret, src))
685 av_packet_free(&ret);
686
687 return ret;
688 }
689
av_packet_move_ref(AVPacket * dst,AVPacket * src)690 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
691 {
692 *dst = *src;
693 get_packet_defaults(src);
694 }
695
av_packet_make_refcounted(AVPacket * pkt)696 int av_packet_make_refcounted(AVPacket *pkt)
697 {
698 int ret;
699
700 if (pkt->buf)
701 return 0;
702
703 ret = packet_alloc(&pkt->buf, pkt->size);
704 if (ret < 0)
705 return ret;
706 av_assert1(!pkt->size || pkt->data);
707 if (pkt->size)
708 memcpy(pkt->buf->data, pkt->data, pkt->size);
709
710 pkt->data = pkt->buf->data;
711
712 return 0;
713 }
714
av_packet_make_writable(AVPacket * pkt)715 int av_packet_make_writable(AVPacket *pkt)
716 {
717 AVBufferRef *buf = NULL;
718 int ret;
719
720 if (pkt->buf && av_buffer_is_writable(pkt->buf))
721 return 0;
722
723 ret = packet_alloc(&buf, pkt->size);
724 if (ret < 0)
725 return ret;
726 av_assert1(!pkt->size || pkt->data);
727 if (pkt->size)
728 memcpy(buf->data, pkt->data, pkt->size);
729
730 av_buffer_unref(&pkt->buf);
731 pkt->buf = buf;
732 pkt->data = buf->data;
733
734 return 0;
735 }
736
av_packet_rescale_ts(AVPacket * pkt,AVRational src_tb,AVRational dst_tb)737 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
738 {
739 if (pkt->pts != AV_NOPTS_VALUE)
740 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
741 if (pkt->dts != AV_NOPTS_VALUE)
742 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
743 if (pkt->duration > 0)
744 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
745 #if FF_API_CONVERGENCE_DURATION
746 FF_DISABLE_DEPRECATION_WARNINGS
747 if (pkt->convergence_duration > 0)
748 pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
749 FF_ENABLE_DEPRECATION_WARNINGS
750 #endif
751 }
752
avpriv_packet_list_put(PacketList ** packet_buffer,PacketList ** plast_pktl,AVPacket * pkt,int (* copy)(AVPacket * dst,const AVPacket * src),int flags)753 int avpriv_packet_list_put(PacketList **packet_buffer,
754 PacketList **plast_pktl,
755 AVPacket *pkt,
756 int (*copy)(AVPacket *dst, const AVPacket *src),
757 int flags)
758 {
759 PacketList *pktl = av_mallocz(sizeof(PacketList));
760 int ret;
761
762 if (!pktl)
763 return AVERROR(ENOMEM);
764
765 if (copy) {
766 ret = copy(&pktl->pkt, pkt);
767 if (ret < 0) {
768 av_free(pktl);
769 return ret;
770 }
771 } else {
772 ret = av_packet_make_refcounted(pkt);
773 if (ret < 0) {
774 av_free(pktl);
775 return ret;
776 }
777 av_packet_move_ref(&pktl->pkt, pkt);
778 }
779
780 if (*packet_buffer)
781 (*plast_pktl)->next = pktl;
782 else
783 *packet_buffer = pktl;
784
785 /* Add the packet in the buffered packet list. */
786 *plast_pktl = pktl;
787 return 0;
788 }
789
avpriv_packet_list_get(PacketList ** pkt_buffer,PacketList ** pkt_buffer_end,AVPacket * pkt)790 int avpriv_packet_list_get(PacketList **pkt_buffer,
791 PacketList **pkt_buffer_end,
792 AVPacket *pkt)
793 {
794 PacketList *pktl;
795 if (!*pkt_buffer)
796 return AVERROR(EAGAIN);
797 pktl = *pkt_buffer;
798 *pkt = pktl->pkt;
799 *pkt_buffer = pktl->next;
800 if (!pktl->next)
801 *pkt_buffer_end = NULL;
802 av_freep(&pktl);
803 return 0;
804 }
805
avpriv_packet_list_free(PacketList ** pkt_buf,PacketList ** pkt_buf_end)806 void avpriv_packet_list_free(PacketList **pkt_buf, PacketList **pkt_buf_end)
807 {
808 PacketList *tmp = *pkt_buf;
809
810 while (tmp) {
811 PacketList *pktl = tmp;
812 tmp = pktl->next;
813 av_packet_unref(&pktl->pkt);
814 av_freep(&pktl);
815 }
816 *pkt_buf = NULL;
817 *pkt_buf_end = NULL;
818 }
819
ff_side_data_set_encoder_stats(AVPacket * pkt,int quality,int64_t * error,int error_count,int pict_type)820 int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
821 {
822 uint8_t *side_data;
823 buffer_size_t side_data_size;
824 int i;
825
826 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
827 if (!side_data) {
828 side_data_size = 4+4+8*error_count;
829 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
830 side_data_size);
831 }
832
833 if (!side_data || side_data_size < 4+4+8*error_count)
834 return AVERROR(ENOMEM);
835
836 AV_WL32(side_data , quality );
837 side_data[4] = pict_type;
838 side_data[5] = error_count;
839 for (i = 0; i<error_count; i++)
840 AV_WL64(side_data+8 + 8*i , error[i]);
841
842 return 0;
843 }
844
ff_side_data_set_prft(AVPacket * pkt,int64_t timestamp)845 int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp)
846 {
847 AVProducerReferenceTime *prft;
848 uint8_t *side_data;
849 buffer_size_t side_data_size;
850
851 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &side_data_size);
852 if (!side_data) {
853 side_data_size = sizeof(AVProducerReferenceTime);
854 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT, side_data_size);
855 }
856
857 if (!side_data || side_data_size < sizeof(AVProducerReferenceTime))
858 return AVERROR(ENOMEM);
859
860 prft = (AVProducerReferenceTime *)side_data;
861 prft->wallclock = timestamp;
862 prft->flags = 0;
863
864 return 0;
865 }
866