2 * AVPacket functions for libavcodec
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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 #if FF_API_DESTRUCT_PACKET
31 void av_destruct_packet(AVPacket *pkt)
38 /* a dummy destruct callback for the callers that assume AVPacket.destruct ==
39 * NULL => static data */
40 static void dummy_destruct_packet(AVPacket *pkt)
46 void av_init_packet(AVPacket *pkt)
48 pkt->pts = AV_NOPTS_VALUE;
49 pkt->dts = AV_NOPTS_VALUE;
52 pkt->convergence_duration = 0;
54 pkt->stream_index = 0;
55 #if FF_API_DESTRUCT_PACKET
56 FF_DISABLE_DEPRECATION_WARNINGS
58 FF_ENABLE_DEPRECATION_WARNINGS
61 pkt->side_data = NULL;
62 pkt->side_data_elems = 0;
65 static int packet_alloc(AVBufferRef **buf, int size)
67 if ((unsigned)size >= (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
68 return AVERROR(EINVAL);
70 av_buffer_realloc(buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
72 return AVERROR(ENOMEM);
74 memset((*buf)->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
79 int av_new_packet(AVPacket *pkt, int size)
81 AVBufferRef *buf = NULL;
82 int ret = packet_alloc(&buf, size);
88 pkt->data = buf->data;
90 #if FF_API_DESTRUCT_PACKET
91 FF_DISABLE_DEPRECATION_WARNINGS
92 pkt->destruct = dummy_destruct_packet;
93 FF_ENABLE_DEPRECATION_WARNINGS
99 void av_shrink_packet(AVPacket *pkt, int size)
101 if (pkt->size <= size)
104 memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
107 int av_grow_packet(AVPacket *pkt, int grow_by)
110 av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
112 return av_new_packet(pkt, grow_by);
113 if ((unsigned)grow_by >
114 INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
117 new_size = pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE;
119 int ret = av_buffer_realloc(&pkt->buf, new_size);
123 pkt->buf = av_buffer_alloc(new_size);
125 return AVERROR(ENOMEM);
126 memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
127 #if FF_API_DESTRUCT_PACKET
128 FF_DISABLE_DEPRECATION_WARNINGS
129 pkt->destruct = dummy_destruct_packet;
130 FF_ENABLE_DEPRECATION_WARNINGS
133 pkt->data = pkt->buf->data;
134 pkt->size += grow_by;
135 memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
140 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
142 if (size >= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
143 return AVERROR(EINVAL);
145 pkt->buf = av_buffer_create(data, size + FF_INPUT_BUFFER_PADDING_SIZE,
146 av_buffer_default_free, NULL, 0);
148 return AVERROR(ENOMEM);
152 #if FF_API_DESTRUCT_PACKET
153 FF_DISABLE_DEPRECATION_WARNINGS
154 pkt->destruct = dummy_destruct_packet;
155 FF_ENABLE_DEPRECATION_WARNINGS
161 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
162 #define ALLOC_BUF(data, size) \
164 av_buffer_realloc(&pkt->buf, size); \
165 data = pkt->buf ? pkt->buf->data : NULL; \
168 #define DUP_DATA(dst, src, size, padding, ALLOC) \
172 if ((unsigned)(size) > \
173 (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE) \
175 ALLOC(data, size + FF_INPUT_BUFFER_PADDING_SIZE); \
181 memcpy(data, src, size); \
183 memset((uint8_t *)data + size, 0, \
184 FF_INPUT_BUFFER_PADDING_SIZE); \
188 int av_dup_packet(AVPacket *pkt)
192 FF_DISABLE_DEPRECATION_WARNINGS
193 if (!pkt->buf && pkt->data
194 #if FF_API_DESTRUCT_PACKET
198 FF_ENABLE_DEPRECATION_WARNINGS
202 pkt->side_data = NULL;
203 DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1, ALLOC_BUF);
204 #if FF_API_DESTRUCT_PACKET
205 FF_DISABLE_DEPRECATION_WARNINGS
206 pkt->destruct = dummy_destruct_packet;
207 FF_ENABLE_DEPRECATION_WARNINGS
210 if (pkt->side_data_elems) {
213 DUP_DATA(pkt->side_data, tmp_pkt.side_data,
214 pkt->side_data_elems * sizeof(*pkt->side_data), 0, ALLOC_MALLOC);
215 memset(pkt->side_data, 0,
216 pkt->side_data_elems * sizeof(*pkt->side_data));
217 for (i = 0; i < pkt->side_data_elems; i++) {
218 DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
219 tmp_pkt.side_data[i].size, 1, ALLOC_MALLOC);
220 pkt->side_data[i].size = tmp_pkt.side_data[i].size;
221 pkt->side_data[i].type = tmp_pkt.side_data[i].type;
229 return AVERROR(ENOMEM);
232 void av_packet_free_side_data(AVPacket *pkt)
235 for (i = 0; i < pkt->side_data_elems; i++)
236 av_free(pkt->side_data[i].data);
237 av_freep(&pkt->side_data);
238 pkt->side_data_elems = 0;
241 void av_free_packet(AVPacket *pkt)
244 FF_DISABLE_DEPRECATION_WARNINGS
246 av_buffer_unref(&pkt->buf);
247 #if FF_API_DESTRUCT_PACKET
248 else if (pkt->destruct)
250 pkt->destruct = NULL;
252 FF_ENABLE_DEPRECATION_WARNINGS
256 av_packet_free_side_data(pkt);
260 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
263 int elems = pkt->side_data_elems;
265 if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
267 if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
270 pkt->side_data = av_realloc(pkt->side_data,
271 (elems + 1) * sizeof(*pkt->side_data));
275 pkt->side_data[elems].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
276 if (!pkt->side_data[elems].data)
278 pkt->side_data[elems].size = size;
279 pkt->side_data[elems].type = type;
280 pkt->side_data_elems++;
282 return pkt->side_data[elems].data;
285 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
290 for (i = 0; i < pkt->side_data_elems; i++) {
291 if (pkt->side_data[i].type == type) {
293 *size = pkt->side_data[i].size;
294 return pkt->side_data[i].data;
300 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
305 for (i = 0; i < pkt->side_data_elems; i++) {
306 if (pkt->side_data[i].type == type) {
307 if (size > pkt->side_data[i].size)
308 return AVERROR(ENOMEM);
309 pkt->side_data[i].size = size;
313 return AVERROR(ENOENT);
316 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
323 dst->duration = src->duration;
324 dst->convergence_duration = src->convergence_duration;
325 dst->flags = src->flags;
326 dst->stream_index = src->stream_index;
327 dst->side_data_elems = src->side_data_elems;
329 for (i = 0; i < src->side_data_elems; i++) {
330 enum AVPacketSideDataType type = src->side_data[i].type;
331 int size = src->side_data[i].size;
332 uint8_t *src_data = src->side_data[i].data;
333 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
336 av_packet_free_side_data(dst);
337 return AVERROR(ENOMEM);
339 memcpy(dst_data, src_data, size);
345 void av_packet_unref(AVPacket *pkt)
347 av_packet_free_side_data(pkt);
348 av_buffer_unref(&pkt->buf);
354 int av_packet_ref(AVPacket *dst, AVPacket *src)
358 ret = av_packet_copy_props(dst, src);
363 ret = packet_alloc(&dst->buf, src->size);
366 memcpy(dst->buf->data, src->data, src->size);
368 dst->buf = av_buffer_ref(src->buf);
370 dst->size = src->size;
371 dst->data = dst->buf->data;
374 av_packet_free_side_data(dst);
378 void av_packet_move_ref(AVPacket *dst, AVPacket *src)