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/mem.h"
29 #include "bytestream.h"
32 #if FF_API_DESTRUCT_PACKET
34 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)
70 if ((unsigned)size >= (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
71 return AVERROR(EINVAL);
73 av_buffer_realloc(buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
75 return AVERROR(ENOMEM);
77 memset((*buf)->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
82 int av_new_packet(AVPacket *pkt, int size)
84 AVBufferRef *buf = NULL;
85 int ret = packet_alloc(&buf, size);
91 pkt->data = buf->data;
93 #if FF_API_DESTRUCT_PACKET
94 FF_DISABLE_DEPRECATION_WARNINGS
95 pkt->destruct = dummy_destruct_packet;
96 FF_ENABLE_DEPRECATION_WARNINGS
102 void av_shrink_packet(AVPacket *pkt, int size)
104 if (pkt->size <= size)
107 memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
110 int av_grow_packet(AVPacket *pkt, int grow_by)
113 av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
115 return av_new_packet(pkt, grow_by);
116 if ((unsigned)grow_by >
117 INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
120 new_size = pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE;
122 int ret = av_buffer_realloc(&pkt->buf, new_size);
126 pkt->buf = av_buffer_alloc(new_size);
128 return AVERROR(ENOMEM);
129 memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
130 #if FF_API_DESTRUCT_PACKET
131 FF_DISABLE_DEPRECATION_WARNINGS
132 pkt->destruct = dummy_destruct_packet;
133 FF_ENABLE_DEPRECATION_WARNINGS
136 pkt->data = pkt->buf->data;
137 pkt->size += grow_by;
138 memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
143 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
145 if (size >= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
146 return AVERROR(EINVAL);
148 pkt->buf = av_buffer_create(data, size + FF_INPUT_BUFFER_PADDING_SIZE,
149 av_buffer_default_free, NULL, 0);
151 return AVERROR(ENOMEM);
155 #if FF_API_DESTRUCT_PACKET
156 FF_DISABLE_DEPRECATION_WARNINGS
157 pkt->destruct = dummy_destruct_packet;
158 FF_ENABLE_DEPRECATION_WARNINGS
164 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
165 #define ALLOC_BUF(data, size) \
167 av_buffer_realloc(&pkt->buf, size); \
168 data = pkt->buf ? pkt->buf->data : NULL; \
171 #define DUP_DATA(dst, src, size, padding, ALLOC) \
175 if ((unsigned)(size) > \
176 (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE) \
178 ALLOC(data, size + FF_INPUT_BUFFER_PADDING_SIZE); \
184 memcpy(data, src, size); \
186 memset((uint8_t *)data + size, 0, \
187 FF_INPUT_BUFFER_PADDING_SIZE); \
191 /* Makes duplicates of data, side_data, but does not copy any other fields */
192 static int copy_packet_data(AVPacket *pkt, AVPacket *src, int dup)
195 pkt->side_data = NULL;
197 AVBufferRef *ref = av_buffer_ref(src->buf);
199 return AVERROR(ENOMEM);
201 pkt->data = ref->data;
203 DUP_DATA(pkt->data, src->data, pkt->size, 1, ALLOC_BUF);
205 #if FF_API_DESTRUCT_PACKET
206 FF_DISABLE_DEPRECATION_WARNINGS
207 pkt->destruct = dummy_destruct_packet;
208 FF_ENABLE_DEPRECATION_WARNINGS
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);
219 return AVERROR(ENOMEM);
222 int av_copy_packet_side_data(AVPacket *pkt, 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);
228 memset(pkt->side_data, 0,
229 src->side_data_elems * sizeof(*src->side_data));
230 for (i = 0; i < src->side_data_elems; i++) {
231 DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
232 src->side_data[i].size, 1, ALLOC_MALLOC);
233 pkt->side_data[i].size = src->side_data[i].size;
234 pkt->side_data[i].type = src->side_data[i].type;
241 return AVERROR(ENOMEM);
244 int av_dup_packet(AVPacket *pkt)
248 FF_DISABLE_DEPRECATION_WARNINGS
249 if (!pkt->buf && pkt->data
250 #if FF_API_DESTRUCT_PACKET
254 FF_ENABLE_DEPRECATION_WARNINGS
256 return copy_packet_data(pkt, &tmp_pkt, 1);
261 int av_copy_packet(AVPacket *dst, AVPacket *src)
264 return copy_packet_data(dst, src, 0);
267 void av_packet_free_side_data(AVPacket *pkt)
270 for (i = 0; i < pkt->side_data_elems; i++)
271 av_free(pkt->side_data[i].data);
272 av_freep(&pkt->side_data);
273 pkt->side_data_elems = 0;
276 void av_free_packet(AVPacket *pkt)
279 FF_DISABLE_DEPRECATION_WARNINGS
281 av_buffer_unref(&pkt->buf);
282 #if FF_API_DESTRUCT_PACKET
283 else if (pkt->destruct)
285 pkt->destruct = NULL;
287 FF_ENABLE_DEPRECATION_WARNINGS
291 av_packet_free_side_data(pkt);
295 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
298 int elems = pkt->side_data_elems;
300 if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
302 if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
305 pkt->side_data = av_realloc(pkt->side_data,
306 (elems + 1) * sizeof(*pkt->side_data));
310 pkt->side_data[elems].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
311 if (!pkt->side_data[elems].data)
313 pkt->side_data[elems].size = size;
314 pkt->side_data[elems].type = type;
315 pkt->side_data_elems++;
317 return pkt->side_data[elems].data;
320 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
325 for (i = 0; i < pkt->side_data_elems; i++) {
326 if (pkt->side_data[i].type == type) {
328 *size = pkt->side_data[i].size;
329 return pkt->side_data[i].data;
335 #define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
337 int av_packet_merge_side_data(AVPacket *pkt){
338 if(pkt->side_data_elems){
342 uint64_t size= pkt->size + 8LL + FF_INPUT_BUFFER_PADDING_SIZE;
344 for (i=0; i<old.side_data_elems; i++) {
345 size += old.side_data[i].size + 5LL;
348 return AVERROR(EINVAL);
349 buf = av_buffer_alloc(size);
351 return AVERROR(ENOMEM);
353 pkt->data = p = buf->data;
354 #if FF_API_DESTRUCT_PACKET
355 FF_DISABLE_DEPRECATION_WARNINGS
356 pkt->destruct = dummy_destruct_packet;
357 FF_ENABLE_DEPRECATION_WARNINGS
359 pkt->size = size - FF_INPUT_BUFFER_PADDING_SIZE;
360 bytestream_put_buffer(&p, old.data, old.size);
361 for (i=old.side_data_elems-1; i>=0; i--) {
362 bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
363 bytestream_put_be32(&p, old.side_data[i].size);
364 *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
366 bytestream_put_be64(&p, FF_MERGE_MARKER);
367 av_assert0(p-pkt->data == pkt->size);
368 memset(p, 0, FF_INPUT_BUFFER_PADDING_SIZE);
369 av_free_packet(&old);
370 pkt->side_data_elems = 0;
371 pkt->side_data = NULL;
377 int av_packet_split_side_data(AVPacket *pkt){
378 if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
380 unsigned int size, orig_pktsize = pkt->size;
383 p = pkt->data + pkt->size - 8 - 5;
386 if (size>INT_MAX || p - pkt->data < size)
393 pkt->side_data = av_malloc(i * sizeof(*pkt->side_data));
395 return AVERROR(ENOMEM);
397 p= pkt->data + pkt->size - 8 - 5;
400 av_assert0(size<=INT_MAX && p - pkt->data >= size);
401 pkt->side_data[i].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
402 pkt->side_data[i].size = size;
403 pkt->side_data[i].type = p[4]&127;
404 if (!pkt->side_data[i].data)
405 return AVERROR(ENOMEM);
406 memcpy(pkt->side_data[i].data, p-size, size);
407 pkt->size -= size + 5;
413 /* FFMIN() prevents overflow in case the packet wasn't allocated with
415 * If the side data is smaller than the buffer padding size, the
416 * remaining bytes should have already been filled with zeros by the
417 * original packet allocation anyway. */
418 memset(pkt->data + pkt->size, 0,
419 FFMIN(orig_pktsize - pkt->size, FF_INPUT_BUFFER_PADDING_SIZE));
420 pkt->side_data_elems = i+1;
426 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
431 for (i = 0; i < pkt->side_data_elems; i++) {
432 if (pkt->side_data[i].type == type) {
433 if (size > pkt->side_data[i].size)
434 return AVERROR(ENOMEM);
435 pkt->side_data[i].size = size;
439 return AVERROR(ENOENT);
442 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
449 dst->duration = src->duration;
450 dst->convergence_duration = src->convergence_duration;
451 dst->flags = src->flags;
452 dst->stream_index = src->stream_index;
453 dst->side_data_elems = src->side_data_elems;
455 for (i = 0; i < src->side_data_elems; i++) {
456 enum AVPacketSideDataType type = src->side_data[i].type;
457 int size = src->side_data[i].size;
458 uint8_t *src_data = src->side_data[i].data;
459 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
462 av_packet_free_side_data(dst);
463 return AVERROR(ENOMEM);
465 memcpy(dst_data, src_data, size);
471 void av_packet_unref(AVPacket *pkt)
473 av_packet_free_side_data(pkt);
474 av_buffer_unref(&pkt->buf);
480 int av_packet_ref(AVPacket *dst, AVPacket *src)
484 ret = av_packet_copy_props(dst, src);
489 ret = packet_alloc(&dst->buf, src->size);
492 memcpy(dst->buf->data, src->data, src->size);
494 dst->buf = av_buffer_ref(src->buf);
496 dst->size = src->size;
497 dst->data = dst->buf->data;
500 av_packet_free_side_data(dst);
504 void av_packet_move_ref(AVPacket *dst, AVPacket *src)