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 static int packet_alloc(AVBufferRef **buf, int size)
54 if ((unsigned)size >= (unsigned)size + AV_INPUT_BUFFER_PADDING_SIZE)
55 return AVERROR(EINVAL);
57 ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
61 memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
66 int av_new_packet(AVPacket *pkt, int size)
68 AVBufferRef *buf = NULL;
69 int ret = packet_alloc(&buf, size);
75 pkt->data = buf->data;
81 void av_shrink_packet(AVPacket *pkt, int size)
83 if (pkt->size <= size)
86 memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
89 int av_grow_packet(AVPacket *pkt, int grow_by)
92 av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
94 return av_new_packet(pkt, grow_by);
95 if ((unsigned)grow_by >
96 INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
99 new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
101 int ret = av_buffer_realloc(&pkt->buf, new_size);
105 pkt->buf = av_buffer_alloc(new_size);
107 return AVERROR(ENOMEM);
108 memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
110 pkt->data = pkt->buf->data;
111 pkt->size += grow_by;
112 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
117 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
119 if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
120 return AVERROR(EINVAL);
122 pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
123 av_buffer_default_free, NULL, 0);
125 return AVERROR(ENOMEM);
133 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
134 #define ALLOC_BUF(data, size) \
136 av_buffer_realloc(&pkt->buf, size); \
137 data = pkt->buf ? pkt->buf->data : NULL; \
140 #define DUP_DATA(dst, src, size, padding, ALLOC) \
144 if ((unsigned)(size) > \
145 (unsigned)(size) + AV_INPUT_BUFFER_PADDING_SIZE) \
147 ALLOC(data, size + AV_INPUT_BUFFER_PADDING_SIZE); \
153 memcpy(data, src, size); \
155 memset((uint8_t *)data + size, 0, \
156 AV_INPUT_BUFFER_PADDING_SIZE); \
160 /* Makes duplicates of data, side_data, but does not copy any other fields */
161 static int copy_packet_data(AVPacket *pkt, const AVPacket *src, int dup)
164 pkt->side_data = NULL;
166 AVBufferRef *ref = av_buffer_ref(src->buf);
168 return AVERROR(ENOMEM);
170 pkt->data = ref->data;
172 DUP_DATA(pkt->data, src->data, pkt->size, 1, ALLOC_BUF);
174 if (pkt->side_data_elems && dup)
175 pkt->side_data = src->side_data;
176 if (pkt->side_data_elems && !dup) {
177 return av_copy_packet_side_data(pkt, src);
183 return AVERROR(ENOMEM);
186 int av_copy_packet_side_data(AVPacket *pkt, const AVPacket *src)
188 if (src->side_data_elems) {
190 DUP_DATA(pkt->side_data, src->side_data,
191 src->side_data_elems * sizeof(*src->side_data), 0, ALLOC_MALLOC);
193 memset(pkt->side_data, 0,
194 src->side_data_elems * sizeof(*src->side_data));
196 for (i = 0; i < src->side_data_elems; i++) {
197 DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
198 src->side_data[i].size, 1, ALLOC_MALLOC);
199 pkt->side_data[i].size = src->side_data[i].size;
200 pkt->side_data[i].type = src->side_data[i].type;
203 pkt->side_data_elems = src->side_data_elems;
208 return AVERROR(ENOMEM);
211 int av_dup_packet(AVPacket *pkt)
215 if (!pkt->buf && pkt->data) {
217 return copy_packet_data(pkt, &tmp_pkt, 1);
222 int av_copy_packet(AVPacket *dst, const AVPacket *src)
225 return copy_packet_data(dst, src, 0);
228 void av_packet_free_side_data(AVPacket *pkt)
231 for (i = 0; i < pkt->side_data_elems; i++)
232 av_freep(&pkt->side_data[i].data);
233 av_freep(&pkt->side_data);
234 pkt->side_data_elems = 0;
237 void av_free_packet(AVPacket *pkt)
241 av_buffer_unref(&pkt->buf);
245 av_packet_free_side_data(pkt);
249 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
252 int elems = pkt->side_data_elems;
254 if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
256 if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
259 pkt->side_data = av_realloc(pkt->side_data,
260 (elems + 1) * sizeof(*pkt->side_data));
264 pkt->side_data[elems].data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
265 if (!pkt->side_data[elems].data)
267 pkt->side_data[elems].size = size;
268 pkt->side_data[elems].type = type;
269 pkt->side_data_elems++;
271 return pkt->side_data[elems].data;
274 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
279 for (i = 0; i < pkt->side_data_elems; i++) {
280 if (pkt->side_data[i].type == type) {
282 *size = pkt->side_data[i].size;
283 return pkt->side_data[i].data;
289 const char *av_packet_side_data_name(enum AVPacketSideDataType type)
292 case AV_PKT_DATA_PALETTE: return "Palette";
293 case AV_PKT_DATA_NEW_EXTRADATA: return "New Extradata";
294 case AV_PKT_DATA_PARAM_CHANGE: return "Param Change";
295 case AV_PKT_DATA_H263_MB_INFO: return "H263 MB Info";
296 case AV_PKT_DATA_REPLAYGAIN: return "Replay Gain";
297 case AV_PKT_DATA_DISPLAYMATRIX: return "Display Matrix";
298 case AV_PKT_DATA_STEREO3D: return "Stereo 3D";
299 case AV_PKT_DATA_AUDIO_SERVICE_TYPE: return "Audio Service Type";
300 case AV_PKT_DATA_SKIP_SAMPLES: return "Skip Samples";
301 case AV_PKT_DATA_JP_DUALMONO: return "JP Dual Mono";
302 case AV_PKT_DATA_STRINGS_METADATA: return "Strings Metadata";
303 case AV_PKT_DATA_SUBTITLE_POSITION: return "Subtitle Position";
304 case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL: return "Matroska BlockAdditional";
305 case AV_PKT_DATA_WEBVTT_IDENTIFIER: return "WebVTT ID";
306 case AV_PKT_DATA_WEBVTT_SETTINGS: return "WebVTT Settings";
307 case AV_PKT_DATA_METADATA_UPDATE: return "Metadata Update";
312 #define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
314 int av_packet_merge_side_data(AVPacket *pkt){
315 if(pkt->side_data_elems){
319 uint64_t size= pkt->size + 8LL + AV_INPUT_BUFFER_PADDING_SIZE;
321 for (i=0; i<old.side_data_elems; i++) {
322 size += old.side_data[i].size + 5LL;
325 return AVERROR(EINVAL);
326 buf = av_buffer_alloc(size);
328 return AVERROR(ENOMEM);
330 pkt->data = p = buf->data;
331 pkt->size = size - AV_INPUT_BUFFER_PADDING_SIZE;
332 bytestream_put_buffer(&p, old.data, old.size);
333 for (i=old.side_data_elems-1; i>=0; i--) {
334 bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
335 bytestream_put_be32(&p, old.side_data[i].size);
336 *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
338 bytestream_put_be64(&p, FF_MERGE_MARKER);
339 av_assert0(p-pkt->data == pkt->size);
340 memset(p, 0, AV_INPUT_BUFFER_PADDING_SIZE);
341 av_free_packet(&old);
342 pkt->side_data_elems = 0;
343 pkt->side_data = NULL;
349 int av_packet_split_side_data(AVPacket *pkt){
350 if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
355 p = pkt->data + pkt->size - 8 - 5;
358 if (size>INT_MAX || p - pkt->data < size)
365 pkt->side_data = av_malloc_array(i, sizeof(*pkt->side_data));
367 return AVERROR(ENOMEM);
369 p= pkt->data + pkt->size - 8 - 5;
372 av_assert0(size<=INT_MAX && p - pkt->data >= size);
373 pkt->side_data[i].data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
374 pkt->side_data[i].size = size;
375 pkt->side_data[i].type = p[4]&127;
376 if (!pkt->side_data[i].data)
377 return AVERROR(ENOMEM);
378 memcpy(pkt->side_data[i].data, p-size, size);
379 pkt->size -= size + 5;
385 pkt->side_data_elems = i+1;
391 uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size)
393 AVDictionaryEntry *t = NULL;
394 uint8_t *data = NULL;
400 while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX))) {
401 const size_t keylen = strlen(t->key);
402 const size_t valuelen = strlen(t->value);
403 const size_t new_size = *size + keylen + 1 + valuelen + 1;
404 uint8_t *const new_data = av_realloc(data, new_size);
409 if (new_size > INT_MAX)
412 memcpy(data + *size, t->key, keylen + 1);
413 memcpy(data + *size + keylen + 1, t->value, valuelen + 1);
426 int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict)
428 const uint8_t *end = data + size;
431 if (!dict || !data || !size)
434 return AVERROR_INVALIDDATA;
436 const uint8_t *key = data;
437 const uint8_t *val = data + strlen(key) + 1;
440 return AVERROR_INVALIDDATA;
442 ret = av_dict_set(dict, key, val, 0);
445 data = val + strlen(val) + 1;
451 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
456 for (i = 0; i < pkt->side_data_elems; i++) {
457 if (pkt->side_data[i].type == type) {
458 if (size > pkt->side_data[i].size)
459 return AVERROR(ENOMEM);
460 pkt->side_data[i].size = size;
464 return AVERROR(ENOENT);
467 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
474 dst->duration = src->duration;
475 #if FF_API_CONVERGENCE_DURATION
476 FF_DISABLE_DEPRECATION_WARNINGS
477 dst->convergence_duration = src->convergence_duration;
478 FF_ENABLE_DEPRECATION_WARNINGS
480 dst->flags = src->flags;
481 dst->stream_index = src->stream_index;
483 for (i = 0; i < src->side_data_elems; i++) {
484 enum AVPacketSideDataType type = src->side_data[i].type;
485 int size = src->side_data[i].size;
486 uint8_t *src_data = src->side_data[i].data;
487 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
490 av_packet_free_side_data(dst);
491 return AVERROR(ENOMEM);
493 memcpy(dst_data, src_data, size);
499 void av_packet_unref(AVPacket *pkt)
501 av_packet_free_side_data(pkt);
502 av_buffer_unref(&pkt->buf);
508 int av_packet_ref(AVPacket *dst, const AVPacket *src)
512 ret = av_packet_copy_props(dst, src);
517 ret = packet_alloc(&dst->buf, src->size);
520 memcpy(dst->buf->data, src->data, src->size);
522 dst->buf = av_buffer_ref(src->buf);
524 ret = AVERROR(ENOMEM);
529 dst->size = src->size;
530 dst->data = dst->buf->data;
533 av_packet_free_side_data(dst);
537 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
543 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
545 if (pkt->pts != AV_NOPTS_VALUE)
546 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
547 if (pkt->dts != AV_NOPTS_VALUE)
548 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
549 if (pkt->duration > 0)
550 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
551 #if FF_API_CONVERGENCE_DURATION
552 FF_DISABLE_DEPRECATION_WARNINGS
553 if (pkt->convergence_duration > 0)
554 pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
555 FF_ENABLE_DEPRECATION_WARNINGS
559 int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
565 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
567 side_data_size = 4+4+8*error_count;
568 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
572 if (!side_data || side_data_size < 4+4+8*error_count)
573 return AVERROR(ENOMEM);
575 AV_WL32(side_data , quality );
576 side_data[4] = pict_type;
577 side_data[5] = error_count;
578 for (i = 0; i<error_count; i++)
579 AV_WL64(side_data+8 + 8*i , error[i]);