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);
142 memcpy(pkt->buf->data, pkt->data, pkt->size);
143 pkt->data = pkt->buf->data;
145 pkt->size += grow_by;
146 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
151 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
153 if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
154 return AVERROR(EINVAL);
156 pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
157 av_buffer_default_free, NULL, 0);
159 return AVERROR(ENOMEM);
167 #if FF_API_AVPACKET_OLD_API
168 FF_DISABLE_DEPRECATION_WARNINGS
169 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
170 #define ALLOC_BUF(data, size) \
172 av_buffer_realloc(&pkt->buf, size); \
173 data = pkt->buf ? pkt->buf->data : NULL; \
176 #define DUP_DATA(dst, src, size, padding, ALLOC) \
180 if ((unsigned)(size) > \
181 (unsigned)(size) + AV_INPUT_BUFFER_PADDING_SIZE) \
183 ALLOC(data, size + AV_INPUT_BUFFER_PADDING_SIZE); \
189 memcpy(data, src, size); \
191 memset((uint8_t *)data + size, 0, \
192 AV_INPUT_BUFFER_PADDING_SIZE); \
196 /* Makes duplicates of data, side_data, but does not copy any other fields */
197 static int copy_packet_data(AVPacket *pkt, const AVPacket *src, int dup)
200 pkt->side_data = NULL;
202 AVBufferRef *ref = av_buffer_ref(src->buf);
204 return AVERROR(ENOMEM);
206 pkt->data = ref->data;
208 DUP_DATA(pkt->data, src->data, pkt->size, 1, ALLOC_BUF);
210 if (pkt->side_data_elems && dup)
211 pkt->side_data = src->side_data;
212 if (pkt->side_data_elems && !dup) {
213 return av_copy_packet_side_data(pkt, src);
218 av_packet_unref(pkt);
219 return AVERROR(ENOMEM);
222 int av_copy_packet_side_data(AVPacket *pkt, const AVPacket *src)
224 if (src->side_data_elems) {
226 DUP_DATA(pkt->side_data, src->side_data,
227 src->side_data_elems * sizeof(*src->side_data), 0, ALLOC_MALLOC);
229 memset(pkt->side_data, 0,
230 src->side_data_elems * sizeof(*src->side_data));
232 for (i = 0; i < src->side_data_elems; i++) {
233 DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
234 src->side_data[i].size, 1, ALLOC_MALLOC);
235 pkt->side_data[i].size = src->side_data[i].size;
236 pkt->side_data[i].type = src->side_data[i].type;
239 pkt->side_data_elems = src->side_data_elems;
243 av_packet_unref(pkt);
244 return AVERROR(ENOMEM);
246 FF_ENABLE_DEPRECATION_WARNINGS
249 int av_dup_packet(AVPacket *pkt)
253 if (!pkt->buf && pkt->data) {
255 return copy_packet_data(pkt, &tmp_pkt, 1);
260 int av_copy_packet(AVPacket *dst, const AVPacket *src)
263 return copy_packet_data(dst, src, 0);
266 void av_packet_free_side_data(AVPacket *pkt)
269 for (i = 0; i < pkt->side_data_elems; i++)
270 av_freep(&pkt->side_data[i].data);
271 av_freep(&pkt->side_data);
272 pkt->side_data_elems = 0;
275 #if FF_API_AVPACKET_OLD_API
276 FF_DISABLE_DEPRECATION_WARNINGS
277 void av_free_packet(AVPacket *pkt)
281 av_buffer_unref(&pkt->buf);
285 av_packet_free_side_data(pkt);
288 FF_ENABLE_DEPRECATION_WARNINGS
291 int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
292 uint8_t *data, size_t size)
294 int elems = pkt->side_data_elems;
296 if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
297 return AVERROR(ERANGE);
299 pkt->side_data = av_realloc(pkt->side_data,
300 (elems + 1) * sizeof(*pkt->side_data));
302 return AVERROR(ENOMEM);
304 pkt->side_data[elems].data = data;
305 pkt->side_data[elems].size = size;
306 pkt->side_data[elems].type = type;
307 pkt->side_data_elems++;
313 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
319 if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
321 data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
325 ret = av_packet_add_side_data(pkt, type, data, size);
334 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
339 for (i = 0; i < pkt->side_data_elems; i++) {
340 if (pkt->side_data[i].type == type) {
342 *size = pkt->side_data[i].size;
343 return pkt->side_data[i].data;
349 const char *av_packet_side_data_name(enum AVPacketSideDataType type)
352 case AV_PKT_DATA_PALETTE: return "Palette";
353 case AV_PKT_DATA_NEW_EXTRADATA: return "New Extradata";
354 case AV_PKT_DATA_PARAM_CHANGE: return "Param Change";
355 case AV_PKT_DATA_H263_MB_INFO: return "H263 MB Info";
356 case AV_PKT_DATA_REPLAYGAIN: return "Replay Gain";
357 case AV_PKT_DATA_DISPLAYMATRIX: return "Display Matrix";
358 case AV_PKT_DATA_STEREO3D: return "Stereo 3D";
359 case AV_PKT_DATA_AUDIO_SERVICE_TYPE: return "Audio Service Type";
360 case AV_PKT_DATA_SKIP_SAMPLES: return "Skip Samples";
361 case AV_PKT_DATA_JP_DUALMONO: return "JP Dual Mono";
362 case AV_PKT_DATA_STRINGS_METADATA: return "Strings Metadata";
363 case AV_PKT_DATA_SUBTITLE_POSITION: return "Subtitle Position";
364 case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL: return "Matroska BlockAdditional";
365 case AV_PKT_DATA_WEBVTT_IDENTIFIER: return "WebVTT ID";
366 case AV_PKT_DATA_WEBVTT_SETTINGS: return "WebVTT Settings";
367 case AV_PKT_DATA_METADATA_UPDATE: return "Metadata Update";
368 case AV_PKT_DATA_MPEGTS_STREAM_ID: return "MPEGTS Stream ID";
369 case AV_PKT_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
374 #define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
376 int av_packet_merge_side_data(AVPacket *pkt){
377 if(pkt->side_data_elems){
381 uint64_t size= pkt->size + 8LL + AV_INPUT_BUFFER_PADDING_SIZE;
383 for (i=0; i<old.side_data_elems; i++) {
384 size += old.side_data[i].size + 5LL;
387 return AVERROR(EINVAL);
388 buf = av_buffer_alloc(size);
390 return AVERROR(ENOMEM);
392 pkt->data = p = buf->data;
393 pkt->size = size - AV_INPUT_BUFFER_PADDING_SIZE;
394 bytestream_put_buffer(&p, old.data, old.size);
395 for (i=old.side_data_elems-1; i>=0; i--) {
396 bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
397 bytestream_put_be32(&p, old.side_data[i].size);
398 *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
400 bytestream_put_be64(&p, FF_MERGE_MARKER);
401 av_assert0(p-pkt->data == pkt->size);
402 memset(p, 0, AV_INPUT_BUFFER_PADDING_SIZE);
403 av_packet_unref(&old);
404 pkt->side_data_elems = 0;
405 pkt->side_data = NULL;
411 int av_packet_split_side_data(AVPacket *pkt){
412 if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
417 p = pkt->data + pkt->size - 8 - 5;
420 if (size>INT_MAX - 5 || p - pkt->data < size)
424 if (p - pkt->data < size + 5)
429 pkt->side_data = av_malloc_array(i, sizeof(*pkt->side_data));
431 return AVERROR(ENOMEM);
433 p= pkt->data + pkt->size - 8 - 5;
436 av_assert0(size<=INT_MAX - 5 && p - pkt->data >= size);
437 pkt->side_data[i].data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
438 pkt->side_data[i].size = size;
439 pkt->side_data[i].type = p[4]&127;
440 if (!pkt->side_data[i].data)
441 return AVERROR(ENOMEM);
442 memcpy(pkt->side_data[i].data, p-size, size);
443 pkt->size -= size + 5;
449 pkt->side_data_elems = i+1;
455 uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size)
457 AVDictionaryEntry *t = NULL;
458 uint8_t *data = NULL;
464 while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX))) {
465 const size_t keylen = strlen(t->key);
466 const size_t valuelen = strlen(t->value);
467 const size_t new_size = *size + keylen + 1 + valuelen + 1;
468 uint8_t *const new_data = av_realloc(data, new_size);
473 if (new_size > INT_MAX)
476 memcpy(data + *size, t->key, keylen + 1);
477 memcpy(data + *size + keylen + 1, t->value, valuelen + 1);
490 int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict)
492 const uint8_t *end = data + size;
495 if (!dict || !data || !size)
498 return AVERROR_INVALIDDATA;
500 const uint8_t *key = data;
501 const uint8_t *val = data + strlen(key) + 1;
504 return AVERROR_INVALIDDATA;
506 ret = av_dict_set(dict, key, val, 0);
509 data = val + strlen(val) + 1;
515 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
520 for (i = 0; i < pkt->side_data_elems; i++) {
521 if (pkt->side_data[i].type == type) {
522 if (size > pkt->side_data[i].size)
523 return AVERROR(ENOMEM);
524 pkt->side_data[i].size = size;
528 return AVERROR(ENOENT);
531 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
538 dst->duration = src->duration;
539 #if FF_API_CONVERGENCE_DURATION
540 FF_DISABLE_DEPRECATION_WARNINGS
541 dst->convergence_duration = src->convergence_duration;
542 FF_ENABLE_DEPRECATION_WARNINGS
544 dst->flags = src->flags;
545 dst->stream_index = src->stream_index;
547 for (i = 0; i < src->side_data_elems; i++) {
548 enum AVPacketSideDataType type = src->side_data[i].type;
549 int size = src->side_data[i].size;
550 uint8_t *src_data = src->side_data[i].data;
551 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
554 av_packet_free_side_data(dst);
555 return AVERROR(ENOMEM);
557 memcpy(dst_data, src_data, size);
563 void av_packet_unref(AVPacket *pkt)
565 av_packet_free_side_data(pkt);
566 av_buffer_unref(&pkt->buf);
572 int av_packet_ref(AVPacket *dst, const AVPacket *src)
576 ret = av_packet_copy_props(dst, src);
581 ret = packet_alloc(&dst->buf, src->size);
584 memcpy(dst->buf->data, src->data, src->size);
585 dst->data = dst->buf->data;
587 dst->buf = av_buffer_ref(src->buf);
589 ret = AVERROR(ENOMEM);
592 dst->data = src->data;
595 dst->size = src->size;
599 av_packet_free_side_data(dst);
603 AVPacket *av_packet_clone(AVPacket *src)
605 AVPacket *ret = av_packet_alloc();
610 if (av_packet_ref(ret, src))
611 av_packet_free(&ret);
616 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
624 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
626 if (pkt->pts != AV_NOPTS_VALUE)
627 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
628 if (pkt->dts != AV_NOPTS_VALUE)
629 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
630 if (pkt->duration > 0)
631 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
632 #if FF_API_CONVERGENCE_DURATION
633 FF_DISABLE_DEPRECATION_WARNINGS
634 if (pkt->convergence_duration > 0)
635 pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
636 FF_ENABLE_DEPRECATION_WARNINGS
640 int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
646 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
648 side_data_size = 4+4+8*error_count;
649 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
653 if (!side_data || side_data_size < 4+4+8*error_count)
654 return AVERROR(ENOMEM);
656 AV_WL32(side_data , quality );
657 side_data[4] = pict_type;
658 side_data[5] = error_count;
659 for (i = 0; i<error_count; i++)
660 AV_WL64(side_data+8 + 8*i , error[i]);