2 * AVPacket functions for libavcodec
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
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"
30 #include "bytestream.h"
33 void av_init_packet(AVPacket *pkt)
35 pkt->pts = AV_NOPTS_VALUE;
36 pkt->dts = AV_NOPTS_VALUE;
39 #if FF_API_CONVERGENCE_DURATION
40 FF_DISABLE_DEPRECATION_WARNINGS
41 pkt->convergence_duration = 0;
42 FF_ENABLE_DEPRECATION_WARNINGS
45 pkt->stream_index = 0;
47 pkt->side_data = NULL;
48 pkt->side_data_elems = 0;
51 AVPacket *av_packet_alloc(void)
53 AVPacket *pkt = av_mallocz(sizeof(AVPacket));
62 void av_packet_free(AVPacket **pkt)
67 av_packet_unref(*pkt);
71 static int packet_alloc(AVBufferRef **buf, int size)
74 if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
75 return AVERROR(EINVAL);
77 ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
81 memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
86 int av_new_packet(AVPacket *pkt, int size)
88 AVBufferRef *buf = NULL;
89 int ret = packet_alloc(&buf, size);
95 pkt->data = buf->data;
101 void av_shrink_packet(AVPacket *pkt, int size)
103 if (pkt->size <= size)
106 memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
109 int av_grow_packet(AVPacket *pkt, int grow_by)
112 av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
113 if ((unsigned)grow_by >
114 INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
117 new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
120 uint8_t *old_data = pkt->data;
121 if (pkt->data == NULL) {
123 pkt->data = pkt->buf->data;
125 data_offset = pkt->data - pkt->buf->data;
126 if (data_offset > INT_MAX - new_size)
130 if (new_size + data_offset > pkt->buf->size) {
131 int ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
133 pkt->data = old_data;
136 pkt->data = pkt->buf->data + data_offset;
139 pkt->buf = av_buffer_alloc(new_size);
141 return AVERROR(ENOMEM);
143 memcpy(pkt->buf->data, pkt->data, pkt->size);
144 pkt->data = pkt->buf->data;
146 pkt->size += grow_by;
147 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
152 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
154 if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
155 return AVERROR(EINVAL);
157 pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
158 av_buffer_default_free, NULL, 0);
160 return AVERROR(ENOMEM);
168 #if FF_API_AVPACKET_OLD_API
169 FF_DISABLE_DEPRECATION_WARNINGS
170 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
171 #define ALLOC_BUF(data, size) \
173 av_buffer_realloc(&pkt->buf, size); \
174 data = pkt->buf ? pkt->buf->data : NULL; \
177 #define DUP_DATA(dst, src, size, padding, ALLOC) \
181 if ((unsigned)(size) > \
182 (unsigned)(size) + AV_INPUT_BUFFER_PADDING_SIZE) \
184 ALLOC(data, size + AV_INPUT_BUFFER_PADDING_SIZE); \
190 memcpy(data, src, size); \
192 memset((uint8_t *)data + size, 0, \
193 AV_INPUT_BUFFER_PADDING_SIZE); \
197 /* Makes duplicates of data, side_data, but does not copy any other fields */
198 static int copy_packet_data(AVPacket *pkt, const AVPacket *src, int dup)
201 pkt->side_data = NULL;
202 pkt->side_data_elems = 0;
204 AVBufferRef *ref = av_buffer_ref(src->buf);
206 return AVERROR(ENOMEM);
208 pkt->data = ref->data;
210 DUP_DATA(pkt->data, src->data, pkt->size, 1, ALLOC_BUF);
212 if (src->side_data_elems && dup) {
213 pkt->side_data = src->side_data;
214 pkt->side_data_elems = src->side_data_elems;
216 if (src->side_data_elems && !dup) {
217 return av_copy_packet_side_data(pkt, src);
222 av_packet_unref(pkt);
223 return AVERROR(ENOMEM);
226 int av_copy_packet_side_data(AVPacket *pkt, const AVPacket *src)
228 if (src->side_data_elems) {
230 DUP_DATA(pkt->side_data, src->side_data,
231 src->side_data_elems * sizeof(*src->side_data), 0, ALLOC_MALLOC);
233 memset(pkt->side_data, 0,
234 src->side_data_elems * sizeof(*src->side_data));
236 for (i = 0; i < src->side_data_elems; i++) {
237 DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
238 src->side_data[i].size, 1, ALLOC_MALLOC);
239 pkt->side_data[i].size = src->side_data[i].size;
240 pkt->side_data[i].type = src->side_data[i].type;
243 pkt->side_data_elems = src->side_data_elems;
247 av_packet_unref(pkt);
248 return AVERROR(ENOMEM);
250 FF_ENABLE_DEPRECATION_WARNINGS
253 int av_dup_packet(AVPacket *pkt)
257 if (!pkt->buf && pkt->data) {
259 return copy_packet_data(pkt, &tmp_pkt, 1);
264 int av_copy_packet(AVPacket *dst, const AVPacket *src)
267 return copy_packet_data(dst, src, 0);
270 void av_packet_free_side_data(AVPacket *pkt)
273 for (i = 0; i < pkt->side_data_elems; i++)
274 av_freep(&pkt->side_data[i].data);
275 av_freep(&pkt->side_data);
276 pkt->side_data_elems = 0;
279 #if FF_API_AVPACKET_OLD_API
280 FF_DISABLE_DEPRECATION_WARNINGS
281 void av_free_packet(AVPacket *pkt)
285 av_buffer_unref(&pkt->buf);
289 av_packet_free_side_data(pkt);
292 FF_ENABLE_DEPRECATION_WARNINGS
295 int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
296 uint8_t *data, size_t size)
298 AVPacketSideData *tmp;
299 int elems = pkt->side_data_elems;
301 if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
302 return AVERROR(ERANGE);
304 tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
306 return AVERROR(ENOMEM);
308 pkt->side_data = tmp;
309 pkt->side_data[elems].data = data;
310 pkt->side_data[elems].size = size;
311 pkt->side_data[elems].type = type;
312 pkt->side_data_elems++;
318 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
324 if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
326 data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
330 ret = av_packet_add_side_data(pkt, type, data, size);
339 uint8_t *av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
344 for (i = 0; i < pkt->side_data_elems; i++) {
345 if (pkt->side_data[i].type == type) {
347 *size = pkt->side_data[i].size;
348 return pkt->side_data[i].data;
356 const char *av_packet_side_data_name(enum AVPacketSideDataType type)
359 case AV_PKT_DATA_PALETTE: return "Palette";
360 case AV_PKT_DATA_NEW_EXTRADATA: return "New Extradata";
361 case AV_PKT_DATA_PARAM_CHANGE: return "Param Change";
362 case AV_PKT_DATA_H263_MB_INFO: return "H263 MB Info";
363 case AV_PKT_DATA_REPLAYGAIN: return "Replay Gain";
364 case AV_PKT_DATA_DISPLAYMATRIX: return "Display Matrix";
365 case AV_PKT_DATA_STEREO3D: return "Stereo 3D";
366 case AV_PKT_DATA_AUDIO_SERVICE_TYPE: return "Audio Service Type";
367 case AV_PKT_DATA_SKIP_SAMPLES: return "Skip Samples";
368 case AV_PKT_DATA_JP_DUALMONO: return "JP Dual Mono";
369 case AV_PKT_DATA_STRINGS_METADATA: return "Strings Metadata";
370 case AV_PKT_DATA_SUBTITLE_POSITION: return "Subtitle Position";
371 case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL: return "Matroska BlockAdditional";
372 case AV_PKT_DATA_WEBVTT_IDENTIFIER: return "WebVTT ID";
373 case AV_PKT_DATA_WEBVTT_SETTINGS: return "WebVTT Settings";
374 case AV_PKT_DATA_METADATA_UPDATE: return "Metadata Update";
375 case AV_PKT_DATA_MPEGTS_STREAM_ID: return "MPEGTS Stream ID";
376 case AV_PKT_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
377 case AV_PKT_DATA_SPHERICAL: return "Spherical Mapping";
382 #define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
384 int av_packet_merge_side_data(AVPacket *pkt){
385 if(pkt->side_data_elems){
389 uint64_t size= pkt->size + 8LL + AV_INPUT_BUFFER_PADDING_SIZE;
391 for (i=0; i<old.side_data_elems; i++) {
392 size += old.side_data[i].size + 5LL;
395 return AVERROR(EINVAL);
396 buf = av_buffer_alloc(size);
398 return AVERROR(ENOMEM);
400 pkt->data = p = buf->data;
401 pkt->size = size - AV_INPUT_BUFFER_PADDING_SIZE;
402 bytestream_put_buffer(&p, old.data, old.size);
403 for (i=old.side_data_elems-1; i>=0; i--) {
404 bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
405 bytestream_put_be32(&p, old.side_data[i].size);
406 *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
408 bytestream_put_be64(&p, FF_MERGE_MARKER);
409 av_assert0(p-pkt->data == pkt->size);
410 memset(p, 0, AV_INPUT_BUFFER_PADDING_SIZE);
411 av_packet_unref(&old);
412 pkt->side_data_elems = 0;
413 pkt->side_data = NULL;
419 int av_packet_split_side_data(AVPacket *pkt){
420 if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
425 p = pkt->data + pkt->size - 8 - 5;
428 if (size>INT_MAX - 5 || p - pkt->data < size)
432 if (p - pkt->data < size + 5)
437 pkt->side_data = av_malloc_array(i, sizeof(*pkt->side_data));
439 return AVERROR(ENOMEM);
441 p= pkt->data + pkt->size - 8 - 5;
444 av_assert0(size<=INT_MAX - 5 && p - pkt->data >= size);
445 pkt->side_data[i].data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
446 pkt->side_data[i].size = size;
447 pkt->side_data[i].type = p[4]&127;
448 if (!pkt->side_data[i].data)
449 return AVERROR(ENOMEM);
450 memcpy(pkt->side_data[i].data, p-size, size);
451 pkt->size -= size + 5;
457 pkt->side_data_elems = i+1;
463 uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size)
465 AVDictionaryEntry *t = NULL;
466 uint8_t *data = NULL;
472 while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX))) {
473 const size_t keylen = strlen(t->key);
474 const size_t valuelen = strlen(t->value);
475 const size_t new_size = *size + keylen + 1 + valuelen + 1;
476 uint8_t *const new_data = av_realloc(data, new_size);
481 if (new_size > INT_MAX)
484 memcpy(data + *size, t->key, keylen + 1);
485 memcpy(data + *size + keylen + 1, t->value, valuelen + 1);
498 int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict)
500 const uint8_t *end = data + size;
503 if (!dict || !data || !size)
506 return AVERROR_INVALIDDATA;
508 const uint8_t *key = data;
509 const uint8_t *val = data + strlen(key) + 1;
511 if (val >= end || !*key)
512 return AVERROR_INVALIDDATA;
514 ret = av_dict_set(dict, key, val, 0);
517 data = val + strlen(val) + 1;
523 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
528 for (i = 0; i < pkt->side_data_elems; i++) {
529 if (pkt->side_data[i].type == type) {
530 if (size > pkt->side_data[i].size)
531 return AVERROR(ENOMEM);
532 pkt->side_data[i].size = size;
536 return AVERROR(ENOENT);
539 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
546 dst->duration = src->duration;
547 #if FF_API_CONVERGENCE_DURATION
548 FF_DISABLE_DEPRECATION_WARNINGS
549 dst->convergence_duration = src->convergence_duration;
550 FF_ENABLE_DEPRECATION_WARNINGS
552 dst->flags = src->flags;
553 dst->stream_index = src->stream_index;
555 for (i = 0; i < src->side_data_elems; i++) {
556 enum AVPacketSideDataType type = src->side_data[i].type;
557 int size = src->side_data[i].size;
558 uint8_t *src_data = src->side_data[i].data;
559 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
562 av_packet_free_side_data(dst);
563 return AVERROR(ENOMEM);
565 memcpy(dst_data, src_data, size);
571 void av_packet_unref(AVPacket *pkt)
573 av_packet_free_side_data(pkt);
574 av_buffer_unref(&pkt->buf);
580 int av_packet_ref(AVPacket *dst, const AVPacket *src)
584 ret = av_packet_copy_props(dst, src);
589 ret = packet_alloc(&dst->buf, src->size);
593 memcpy(dst->buf->data, src->data, src->size);
595 dst->data = dst->buf->data;
597 dst->buf = av_buffer_ref(src->buf);
599 ret = AVERROR(ENOMEM);
602 dst->data = src->data;
605 dst->size = src->size;
609 av_packet_free_side_data(dst);
613 AVPacket *av_packet_clone(AVPacket *src)
615 AVPacket *ret = av_packet_alloc();
620 if (av_packet_ref(ret, src))
621 av_packet_free(&ret);
626 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
634 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
636 if (pkt->pts != AV_NOPTS_VALUE)
637 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
638 if (pkt->dts != AV_NOPTS_VALUE)
639 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
640 if (pkt->duration > 0)
641 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
642 #if FF_API_CONVERGENCE_DURATION
643 FF_DISABLE_DEPRECATION_WARNINGS
644 if (pkt->convergence_duration > 0)
645 pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
646 FF_ENABLE_DEPRECATION_WARNINGS
650 int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
656 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
658 side_data_size = 4+4+8*error_count;
659 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
663 if (!side_data || side_data_size < 4+4+8*error_count)
664 return AVERROR(ENOMEM);
666 AV_WL32(side_data , quality );
667 side_data[4] = pict_type;
668 side_data[5] = error_count;
669 for (i = 0; i<error_count; i++)
670 AV_WL64(side_data+8 + 8*i , error[i]);