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/mathematics.h"
28 #include "libavutil/mem.h"
31 void av_init_packet(AVPacket *pkt)
33 pkt->pts = AV_NOPTS_VALUE;
34 pkt->dts = AV_NOPTS_VALUE;
37 #if FF_API_CONVERGENCE_DURATION
38 FF_DISABLE_DEPRECATION_WARNINGS
39 pkt->convergence_duration = 0;
40 FF_ENABLE_DEPRECATION_WARNINGS
43 pkt->stream_index = 0;
45 pkt->side_data = NULL;
46 pkt->side_data_elems = 0;
49 static int packet_alloc(AVBufferRef **buf, int size)
52 if ((unsigned)size >= (unsigned)size + AV_INPUT_BUFFER_PADDING_SIZE)
53 return AVERROR(EINVAL);
55 ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
59 memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
64 int av_new_packet(AVPacket *pkt, int size)
66 AVBufferRef *buf = NULL;
67 int ret = packet_alloc(&buf, size);
73 pkt->data = buf->data;
79 void av_shrink_packet(AVPacket *pkt, int size)
81 if (pkt->size <= size)
84 memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
87 int av_grow_packet(AVPacket *pkt, int grow_by)
90 av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
92 return av_new_packet(pkt, grow_by);
93 if ((unsigned)grow_by >
94 INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
97 new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
99 int ret = av_buffer_realloc(&pkt->buf, new_size);
103 pkt->buf = av_buffer_alloc(new_size);
105 return AVERROR(ENOMEM);
106 memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
108 pkt->data = pkt->buf->data;
109 pkt->size += grow_by;
110 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
115 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
117 if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
118 return AVERROR(EINVAL);
120 pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
121 av_buffer_default_free, NULL, 0);
123 return AVERROR(ENOMEM);
131 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
132 #define ALLOC_BUF(data, size) \
134 av_buffer_realloc(&pkt->buf, size); \
135 data = pkt->buf ? pkt->buf->data : NULL; \
138 #define DUP_DATA(dst, src, size, padding, ALLOC) \
142 if ((unsigned)(size) > \
143 (unsigned)(size) + AV_INPUT_BUFFER_PADDING_SIZE) \
145 ALLOC(data, size + AV_INPUT_BUFFER_PADDING_SIZE); \
151 memcpy(data, src, size); \
153 memset((uint8_t *)data + size, 0, \
154 AV_INPUT_BUFFER_PADDING_SIZE); \
158 int av_dup_packet(AVPacket *pkt)
162 if (!pkt->buf && pkt->data) {
166 pkt->side_data = NULL;
167 DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1, ALLOC_BUF);
169 if (pkt->side_data_elems) {
172 DUP_DATA(pkt->side_data, tmp_pkt.side_data,
173 pkt->side_data_elems * sizeof(*pkt->side_data), 0, ALLOC_MALLOC);
174 memset(pkt->side_data, 0,
175 pkt->side_data_elems * sizeof(*pkt->side_data));
176 for (i = 0; i < pkt->side_data_elems; i++) {
177 DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
178 tmp_pkt.side_data[i].size, 1, ALLOC_MALLOC);
179 pkt->side_data[i].size = tmp_pkt.side_data[i].size;
180 pkt->side_data[i].type = tmp_pkt.side_data[i].type;
187 av_packet_unref(pkt);
188 return AVERROR(ENOMEM);
191 void av_packet_free_side_data(AVPacket *pkt)
194 for (i = 0; i < pkt->side_data_elems; i++)
195 av_free(pkt->side_data[i].data);
196 av_freep(&pkt->side_data);
197 pkt->side_data_elems = 0;
200 #if FF_API_AVPACKET_OLD_API
201 FF_DISABLE_DEPRECATION_WARNINGS
202 void av_free_packet(AVPacket *pkt)
206 av_buffer_unref(&pkt->buf);
210 av_packet_free_side_data(pkt);
213 FF_ENABLE_DEPRECATION_WARNINGS
216 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
219 int elems = pkt->side_data_elems;
221 if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
223 if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
226 pkt->side_data = av_realloc(pkt->side_data,
227 (elems + 1) * sizeof(*pkt->side_data));
231 pkt->side_data[elems].data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
232 if (!pkt->side_data[elems].data)
234 pkt->side_data[elems].size = size;
235 pkt->side_data[elems].type = type;
236 pkt->side_data_elems++;
238 return pkt->side_data[elems].data;
241 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
246 for (i = 0; i < pkt->side_data_elems; i++) {
247 if (pkt->side_data[i].type == type) {
249 *size = pkt->side_data[i].size;
250 return pkt->side_data[i].data;
256 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
261 for (i = 0; i < pkt->side_data_elems; i++) {
262 if (pkt->side_data[i].type == type) {
263 if (size > pkt->side_data[i].size)
264 return AVERROR(ENOMEM);
265 pkt->side_data[i].size = size;
269 return AVERROR(ENOENT);
272 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
279 dst->duration = src->duration;
280 #if FF_API_CONVERGENCE_DURATION
281 FF_DISABLE_DEPRECATION_WARNINGS
282 dst->convergence_duration = src->convergence_duration;
283 FF_ENABLE_DEPRECATION_WARNINGS
285 dst->flags = src->flags;
286 dst->stream_index = src->stream_index;
288 for (i = 0; i < src->side_data_elems; i++) {
289 enum AVPacketSideDataType type = src->side_data[i].type;
290 int size = src->side_data[i].size;
291 uint8_t *src_data = src->side_data[i].data;
292 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
295 av_packet_free_side_data(dst);
296 return AVERROR(ENOMEM);
298 memcpy(dst_data, src_data, size);
304 void av_packet_unref(AVPacket *pkt)
306 av_packet_free_side_data(pkt);
307 av_buffer_unref(&pkt->buf);
313 int av_packet_ref(AVPacket *dst, AVPacket *src)
317 ret = av_packet_copy_props(dst, src);
322 ret = packet_alloc(&dst->buf, src->size);
325 memcpy(dst->buf->data, src->data, src->size);
327 dst->buf = av_buffer_ref(src->buf);
329 ret = AVERROR(ENOMEM);
334 dst->size = src->size;
335 dst->data = dst->buf->data;
338 av_packet_free_side_data(dst);
342 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
348 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
350 if (pkt->pts != AV_NOPTS_VALUE)
351 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
352 if (pkt->dts != AV_NOPTS_VALUE)
353 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
354 if (pkt->duration > 0)
355 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
356 #if FF_API_CONVERGENCE_DURATION
357 FF_DISABLE_DEPRECATION_WARNINGS
358 if (pkt->convergence_duration > 0)
359 pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
360 FF_ENABLE_DEPRECATION_WARNINGS