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 #if FF_API_DESTRUCT_PACKET
35 void av_destruct_packet(AVPacket *pkt)
41 /* a dummy destruct callback for the callers that assume AVPacket.destruct ==
42 * NULL => static data */
43 static void dummy_destruct_packet(AVPacket *pkt)
49 void av_init_packet(AVPacket *pkt)
51 pkt->pts = AV_NOPTS_VALUE;
52 pkt->dts = AV_NOPTS_VALUE;
55 pkt->convergence_duration = 0;
57 pkt->stream_index = 0;
58 #if FF_API_DESTRUCT_PACKET
59 FF_DISABLE_DEPRECATION_WARNINGS
61 FF_ENABLE_DEPRECATION_WARNINGS
64 pkt->side_data = NULL;
65 pkt->side_data_elems = 0;
68 static int packet_alloc(AVBufferRef **buf, int size)
71 if ((unsigned)size >= (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
72 return AVERROR(EINVAL);
74 ret = av_buffer_realloc(buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
78 memset((*buf)->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
83 int av_new_packet(AVPacket *pkt, int size)
85 AVBufferRef *buf = NULL;
86 int ret = packet_alloc(&buf, size);
92 pkt->data = buf->data;
94 #if FF_API_DESTRUCT_PACKET
95 FF_DISABLE_DEPRECATION_WARNINGS
96 pkt->destruct = dummy_destruct_packet;
97 FF_ENABLE_DEPRECATION_WARNINGS
103 void av_shrink_packet(AVPacket *pkt, int size)
105 if (pkt->size <= size)
108 memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
111 int av_grow_packet(AVPacket *pkt, int grow_by)
114 av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
116 return av_new_packet(pkt, grow_by);
117 if ((unsigned)grow_by >
118 INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
121 new_size = pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE;
123 int ret = av_buffer_realloc(&pkt->buf, new_size);
127 pkt->buf = av_buffer_alloc(new_size);
129 return AVERROR(ENOMEM);
130 memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
131 #if FF_API_DESTRUCT_PACKET
132 FF_DISABLE_DEPRECATION_WARNINGS
133 pkt->destruct = dummy_destruct_packet;
134 FF_ENABLE_DEPRECATION_WARNINGS
137 pkt->data = pkt->buf->data;
138 pkt->size += grow_by;
139 memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
144 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
146 if (size >= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
147 return AVERROR(EINVAL);
149 pkt->buf = av_buffer_create(data, size + FF_INPUT_BUFFER_PADDING_SIZE,
150 av_buffer_default_free, NULL, 0);
152 return AVERROR(ENOMEM);
156 #if FF_API_DESTRUCT_PACKET
157 FF_DISABLE_DEPRECATION_WARNINGS
158 pkt->destruct = dummy_destruct_packet;
159 FF_ENABLE_DEPRECATION_WARNINGS
165 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
166 #define ALLOC_BUF(data, size) \
168 av_buffer_realloc(&pkt->buf, size); \
169 data = pkt->buf ? pkt->buf->data : NULL; \
172 #define DUP_DATA(dst, src, size, padding, ALLOC) \
176 if ((unsigned)(size) > \
177 (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE) \
179 ALLOC(data, size + FF_INPUT_BUFFER_PADDING_SIZE); \
185 memcpy(data, src, size); \
187 memset((uint8_t *)data + size, 0, \
188 FF_INPUT_BUFFER_PADDING_SIZE); \
192 /* Makes duplicates of data, side_data, but does not copy any other fields */
193 static int copy_packet_data(AVPacket *pkt, const AVPacket *src, int dup)
196 pkt->side_data = NULL;
198 AVBufferRef *ref = av_buffer_ref(src->buf);
200 return AVERROR(ENOMEM);
202 pkt->data = ref->data;
204 DUP_DATA(pkt->data, src->data, pkt->size, 1, ALLOC_BUF);
206 #if FF_API_DESTRUCT_PACKET
207 FF_DISABLE_DEPRECATION_WARNINGS
208 pkt->destruct = dummy_destruct_packet;
209 FF_ENABLE_DEPRECATION_WARNINGS
211 if (pkt->side_data_elems && dup)
212 pkt->side_data = src->side_data;
213 if (pkt->side_data_elems && !dup) {
214 return av_copy_packet_side_data(pkt, src);
220 return AVERROR(ENOMEM);
223 int av_copy_packet_side_data(AVPacket *pkt, const AVPacket *src)
225 if (src->side_data_elems) {
227 DUP_DATA(pkt->side_data, src->side_data,
228 src->side_data_elems * sizeof(*src->side_data), 0, ALLOC_MALLOC);
230 memset(pkt->side_data, 0,
231 src->side_data_elems * sizeof(*src->side_data));
233 for (i = 0; i < src->side_data_elems; i++) {
234 DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
235 src->side_data[i].size, 1, ALLOC_MALLOC);
236 pkt->side_data[i].size = src->side_data[i].size;
237 pkt->side_data[i].type = src->side_data[i].type;
240 pkt->side_data_elems = src->side_data_elems;
245 return AVERROR(ENOMEM);
248 int av_dup_packet(AVPacket *pkt)
252 FF_DISABLE_DEPRECATION_WARNINGS
253 if (!pkt->buf && pkt->data
254 #if FF_API_DESTRUCT_PACKET
258 FF_ENABLE_DEPRECATION_WARNINGS
260 return copy_packet_data(pkt, &tmp_pkt, 1);
265 int av_copy_packet(AVPacket *dst, const AVPacket *src)
268 return copy_packet_data(dst, src, 0);
271 void av_packet_free_side_data(AVPacket *pkt)
274 for (i = 0; i < pkt->side_data_elems; i++)
275 av_freep(&pkt->side_data[i].data);
276 av_freep(&pkt->side_data);
277 pkt->side_data_elems = 0;
280 void av_free_packet(AVPacket *pkt)
283 FF_DISABLE_DEPRECATION_WARNINGS
285 av_buffer_unref(&pkt->buf);
286 #if FF_API_DESTRUCT_PACKET
287 else if (pkt->destruct)
289 pkt->destruct = NULL;
291 FF_ENABLE_DEPRECATION_WARNINGS
295 av_packet_free_side_data(pkt);
299 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
302 int elems = pkt->side_data_elems;
304 if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
306 if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
309 pkt->side_data = av_realloc(pkt->side_data,
310 (elems + 1) * sizeof(*pkt->side_data));
314 pkt->side_data[elems].data = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
315 if (!pkt->side_data[elems].data)
317 pkt->side_data[elems].size = size;
318 pkt->side_data[elems].type = type;
319 pkt->side_data_elems++;
321 return pkt->side_data[elems].data;
324 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
329 for (i = 0; i < pkt->side_data_elems; i++) {
330 if (pkt->side_data[i].type == type) {
332 *size = pkt->side_data[i].size;
333 return pkt->side_data[i].data;
339 const char *av_packet_side_data_name(enum AVPacketSideDataType type)
342 case AV_PKT_DATA_PALETTE: return "Palette";
343 case AV_PKT_DATA_NEW_EXTRADATA: return "New Extradata";
344 case AV_PKT_DATA_PARAM_CHANGE: return "Param Change";
345 case AV_PKT_DATA_H263_MB_INFO: return "H263 MB Info";
346 case AV_PKT_DATA_REPLAYGAIN: return "Replay Gain";
347 case AV_PKT_DATA_DISPLAYMATRIX: return "Display Matrix";
348 case AV_PKT_DATA_STEREO3D: return "Stereo 3D";
349 case AV_PKT_DATA_AUDIO_SERVICE_TYPE: return "Audio Service Type";
350 case AV_PKT_DATA_SKIP_SAMPLES: return "Skip Samples";
351 case AV_PKT_DATA_JP_DUALMONO: return "JP Dual Mono";
352 case AV_PKT_DATA_STRINGS_METADATA: return "Strings Metadata";
353 case AV_PKT_DATA_SUBTITLE_POSITION: return "Subtitle Position";
354 case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL: return "Matroska BlockAdditional";
355 case AV_PKT_DATA_WEBVTT_IDENTIFIER: return "WebVTT ID";
356 case AV_PKT_DATA_WEBVTT_SETTINGS: return "WebVTT Settings";
357 case AV_PKT_DATA_METADATA_UPDATE: return "Metadata Update";
362 #define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
364 int av_packet_merge_side_data(AVPacket *pkt){
365 if(pkt->side_data_elems){
369 uint64_t size= pkt->size + 8LL + FF_INPUT_BUFFER_PADDING_SIZE;
371 for (i=0; i<old.side_data_elems; i++) {
372 size += old.side_data[i].size + 5LL;
375 return AVERROR(EINVAL);
376 buf = av_buffer_alloc(size);
378 return AVERROR(ENOMEM);
380 pkt->data = p = buf->data;
381 #if FF_API_DESTRUCT_PACKET
382 FF_DISABLE_DEPRECATION_WARNINGS
383 pkt->destruct = dummy_destruct_packet;
384 FF_ENABLE_DEPRECATION_WARNINGS
386 pkt->size = size - FF_INPUT_BUFFER_PADDING_SIZE;
387 bytestream_put_buffer(&p, old.data, old.size);
388 for (i=old.side_data_elems-1; i>=0; i--) {
389 bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
390 bytestream_put_be32(&p, old.side_data[i].size);
391 *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
393 bytestream_put_be64(&p, FF_MERGE_MARKER);
394 av_assert0(p-pkt->data == pkt->size);
395 memset(p, 0, FF_INPUT_BUFFER_PADDING_SIZE);
396 av_free_packet(&old);
397 pkt->side_data_elems = 0;
398 pkt->side_data = NULL;
404 int av_packet_split_side_data(AVPacket *pkt){
405 if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
410 p = pkt->data + pkt->size - 8 - 5;
413 if (size>INT_MAX || p - pkt->data < size)
420 pkt->side_data = av_malloc_array(i, sizeof(*pkt->side_data));
422 return AVERROR(ENOMEM);
424 p= pkt->data + pkt->size - 8 - 5;
427 av_assert0(size<=INT_MAX && p - pkt->data >= size);
428 pkt->side_data[i].data = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
429 pkt->side_data[i].size = size;
430 pkt->side_data[i].type = p[4]&127;
431 if (!pkt->side_data[i].data)
432 return AVERROR(ENOMEM);
433 memcpy(pkt->side_data[i].data, p-size, size);
434 pkt->size -= size + 5;
440 pkt->side_data_elems = i+1;
446 uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size)
448 AVDictionaryEntry *t = NULL;
449 uint8_t *data = NULL;
455 while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX))) {
456 const size_t keylen = strlen(t->key);
457 const size_t valuelen = strlen(t->value);
458 const size_t new_size = *size + keylen + 1 + valuelen + 1;
459 uint8_t *const new_data = av_realloc(data, new_size);
464 if (new_size > INT_MAX)
467 memcpy(data + *size, t->key, keylen + 1);
468 memcpy(data + *size + keylen + 1, t->value, valuelen + 1);
481 int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict)
483 const uint8_t *end = data + size;
486 if (!dict || !data || !size)
489 return AVERROR_INVALIDDATA;
491 const uint8_t *key = data;
492 const uint8_t *val = data + strlen(key) + 1;
495 return AVERROR_INVALIDDATA;
497 ret = av_dict_set(dict, key, val, 0);
500 data = val + strlen(val) + 1;
506 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
511 for (i = 0; i < pkt->side_data_elems; i++) {
512 if (pkt->side_data[i].type == type) {
513 if (size > pkt->side_data[i].size)
514 return AVERROR(ENOMEM);
515 pkt->side_data[i].size = size;
519 return AVERROR(ENOENT);
522 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
529 dst->duration = src->duration;
530 dst->convergence_duration = src->convergence_duration;
531 dst->flags = src->flags;
532 dst->stream_index = src->stream_index;
534 for (i = 0; i < src->side_data_elems; i++) {
535 enum AVPacketSideDataType type = src->side_data[i].type;
536 int size = src->side_data[i].size;
537 uint8_t *src_data = src->side_data[i].data;
538 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
541 av_packet_free_side_data(dst);
542 return AVERROR(ENOMEM);
544 memcpy(dst_data, src_data, size);
550 void av_packet_unref(AVPacket *pkt)
552 av_packet_free_side_data(pkt);
553 av_buffer_unref(&pkt->buf);
559 int av_packet_ref(AVPacket *dst, const AVPacket *src)
563 ret = av_packet_copy_props(dst, src);
568 ret = packet_alloc(&dst->buf, src->size);
571 memcpy(dst->buf->data, src->data, src->size);
573 dst->buf = av_buffer_ref(src->buf);
575 ret = AVERROR(ENOMEM);
580 dst->size = src->size;
581 dst->data = dst->buf->data;
584 av_packet_free_side_data(dst);
588 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
594 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
596 if (pkt->pts != AV_NOPTS_VALUE)
597 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
598 if (pkt->dts != AV_NOPTS_VALUE)
599 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
600 if (pkt->duration > 0)
601 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
602 if (pkt->convergence_duration > 0)
603 pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
606 int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
612 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
614 side_data_size = 4+4+8*error_count;
615 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
619 if (!side_data || side_data_size < 4+4+8*error_count)
620 return AVERROR(ENOMEM);
622 AV_WL32(side_data , quality );
623 side_data[4] = pict_type;
624 side_data[5] = error_count;
625 for (i = 0; i<error_count; i++)
626 AV_WL64(side_data+8 + 8*i , error[i]);