avpacket: Provide an alloc and a free function for the struct
[libav.git] / libavcodec / avpacket.c
1 /*
2 * AVPacket functions for libavcodec
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of Libav.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <string.h>
23
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"
29 #include "avcodec.h"
30
31 void av_init_packet(AVPacket *pkt)
32 {
33 pkt->pts = AV_NOPTS_VALUE;
34 pkt->dts = AV_NOPTS_VALUE;
35 pkt->pos = -1;
36 pkt->duration = 0;
37 #if FF_API_CONVERGENCE_DURATION
38 FF_DISABLE_DEPRECATION_WARNINGS
39 pkt->convergence_duration = 0;
40 FF_ENABLE_DEPRECATION_WARNINGS
41 #endif
42 pkt->flags = 0;
43 pkt->stream_index = 0;
44 pkt->buf = NULL;
45 pkt->side_data = NULL;
46 pkt->side_data_elems = 0;
47 }
48
49 AVPacket *av_packet_alloc(void)
50 {
51 AVPacket *pkt = av_mallocz(sizeof(AVPacket));
52 if (!pkt)
53 return pkt;
54
55 av_packet_unref(pkt);
56
57 return pkt;
58 }
59
60 void av_packet_free(AVPacket **pkt)
61 {
62 if (!pkt || !*pkt)
63 return;
64
65 av_packet_unref(*pkt);
66 av_freep(pkt);
67 }
68
69 static int packet_alloc(AVBufferRef **buf, int size)
70 {
71 int ret;
72 if ((unsigned)size >= (unsigned)size + AV_INPUT_BUFFER_PADDING_SIZE)
73 return AVERROR(EINVAL);
74
75 ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
76 if (ret < 0)
77 return ret;
78
79 memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
80
81 return 0;
82 }
83
84 int av_new_packet(AVPacket *pkt, int size)
85 {
86 AVBufferRef *buf = NULL;
87 int ret = packet_alloc(&buf, size);
88 if (ret < 0)
89 return ret;
90
91 av_init_packet(pkt);
92 pkt->buf = buf;
93 pkt->data = buf->data;
94 pkt->size = size;
95
96 return 0;
97 }
98
99 void av_shrink_packet(AVPacket *pkt, int size)
100 {
101 if (pkt->size <= size)
102 return;
103 pkt->size = size;
104 memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
105 }
106
107 int av_grow_packet(AVPacket *pkt, int grow_by)
108 {
109 int new_size;
110 av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
111 if (!pkt->size)
112 return av_new_packet(pkt, grow_by);
113 if ((unsigned)grow_by >
114 INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
115 return -1;
116
117 new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
118 if (pkt->buf) {
119 int ret = av_buffer_realloc(&pkt->buf, new_size);
120 if (ret < 0)
121 return ret;
122 } else {
123 pkt->buf = av_buffer_alloc(new_size);
124 if (!pkt->buf)
125 return AVERROR(ENOMEM);
126 memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
127 }
128 pkt->data = pkt->buf->data;
129 pkt->size += grow_by;
130 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
131
132 return 0;
133 }
134
135 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
136 {
137 if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
138 return AVERROR(EINVAL);
139
140 pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
141 av_buffer_default_free, NULL, 0);
142 if (!pkt->buf)
143 return AVERROR(ENOMEM);
144
145 pkt->data = data;
146 pkt->size = size;
147
148 return 0;
149 }
150
151 #if FF_API_AVPACKET_OLD_API
152 FF_DISABLE_DEPRECATION_WARNINGS
153 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
154 #define ALLOC_BUF(data, size) \
155 do { \
156 av_buffer_realloc(&pkt->buf, size); \
157 data = pkt->buf ? pkt->buf->data : NULL; \
158 } while (0)
159
160 #define DUP_DATA(dst, src, size, padding, ALLOC) \
161 do { \
162 void *data; \
163 if (padding) { \
164 if ((unsigned)(size) > \
165 (unsigned)(size) + AV_INPUT_BUFFER_PADDING_SIZE) \
166 goto failed_alloc; \
167 ALLOC(data, size + AV_INPUT_BUFFER_PADDING_SIZE); \
168 } else { \
169 ALLOC(data, size); \
170 } \
171 if (!data) \
172 goto failed_alloc; \
173 memcpy(data, src, size); \
174 if (padding) \
175 memset((uint8_t *)data + size, 0, \
176 AV_INPUT_BUFFER_PADDING_SIZE); \
177 dst = data; \
178 } while (0)
179
180 int av_dup_packet(AVPacket *pkt)
181 {
182 AVPacket tmp_pkt;
183
184 if (!pkt->buf && pkt->data) {
185 tmp_pkt = *pkt;
186
187 pkt->data = NULL;
188 pkt->side_data = NULL;
189 DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1, ALLOC_BUF);
190
191 if (pkt->side_data_elems) {
192 int i;
193
194 DUP_DATA(pkt->side_data, tmp_pkt.side_data,
195 pkt->side_data_elems * sizeof(*pkt->side_data), 0, ALLOC_MALLOC);
196 memset(pkt->side_data, 0,
197 pkt->side_data_elems * sizeof(*pkt->side_data));
198 for (i = 0; i < pkt->side_data_elems; i++) {
199 DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
200 tmp_pkt.side_data[i].size, 1, ALLOC_MALLOC);
201 pkt->side_data[i].size = tmp_pkt.side_data[i].size;
202 pkt->side_data[i].type = tmp_pkt.side_data[i].type;
203 }
204 }
205 }
206 return 0;
207
208 failed_alloc:
209 av_packet_unref(pkt);
210 return AVERROR(ENOMEM);
211 }
212 FF_ENABLE_DEPRECATION_WARNINGS
213 #endif
214
215 void av_packet_free_side_data(AVPacket *pkt)
216 {
217 int i;
218 for (i = 0; i < pkt->side_data_elems; i++)
219 av_free(pkt->side_data[i].data);
220 av_freep(&pkt->side_data);
221 pkt->side_data_elems = 0;
222 }
223
224 #if FF_API_AVPACKET_OLD_API
225 FF_DISABLE_DEPRECATION_WARNINGS
226 void av_free_packet(AVPacket *pkt)
227 {
228 if (pkt) {
229 if (pkt->buf)
230 av_buffer_unref(&pkt->buf);
231 pkt->data = NULL;
232 pkt->size = 0;
233
234 av_packet_free_side_data(pkt);
235 }
236 }
237 FF_ENABLE_DEPRECATION_WARNINGS
238 #endif
239
240 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
241 int size)
242 {
243 int elems = pkt->side_data_elems;
244
245 if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
246 return NULL;
247 if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
248 return NULL;
249
250 pkt->side_data = av_realloc(pkt->side_data,
251 (elems + 1) * sizeof(*pkt->side_data));
252 if (!pkt->side_data)
253 return NULL;
254
255 pkt->side_data[elems].data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
256 if (!pkt->side_data[elems].data)
257 return NULL;
258 pkt->side_data[elems].size = size;
259 pkt->side_data[elems].type = type;
260 pkt->side_data_elems++;
261
262 return pkt->side_data[elems].data;
263 }
264
265 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
266 int *size)
267 {
268 int i;
269
270 for (i = 0; i < pkt->side_data_elems; i++) {
271 if (pkt->side_data[i].type == type) {
272 if (size)
273 *size = pkt->side_data[i].size;
274 return pkt->side_data[i].data;
275 }
276 }
277 return NULL;
278 }
279
280 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
281 int size)
282 {
283 int i;
284
285 for (i = 0; i < pkt->side_data_elems; i++) {
286 if (pkt->side_data[i].type == type) {
287 if (size > pkt->side_data[i].size)
288 return AVERROR(ENOMEM);
289 pkt->side_data[i].size = size;
290 return 0;
291 }
292 }
293 return AVERROR(ENOENT);
294 }
295
296 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
297 {
298 int i;
299
300 dst->pts = src->pts;
301 dst->dts = src->dts;
302 dst->pos = src->pos;
303 dst->duration = src->duration;
304 #if FF_API_CONVERGENCE_DURATION
305 FF_DISABLE_DEPRECATION_WARNINGS
306 dst->convergence_duration = src->convergence_duration;
307 FF_ENABLE_DEPRECATION_WARNINGS
308 #endif
309 dst->flags = src->flags;
310 dst->stream_index = src->stream_index;
311
312 for (i = 0; i < src->side_data_elems; i++) {
313 enum AVPacketSideDataType type = src->side_data[i].type;
314 int size = src->side_data[i].size;
315 uint8_t *src_data = src->side_data[i].data;
316 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
317
318 if (!dst_data) {
319 av_packet_free_side_data(dst);
320 return AVERROR(ENOMEM);
321 }
322 memcpy(dst_data, src_data, size);
323 }
324
325 return 0;
326 }
327
328 void av_packet_unref(AVPacket *pkt)
329 {
330 av_packet_free_side_data(pkt);
331 av_buffer_unref(&pkt->buf);
332 av_init_packet(pkt);
333 pkt->data = NULL;
334 pkt->size = 0;
335 }
336
337 int av_packet_ref(AVPacket *dst, AVPacket *src)
338 {
339 int ret;
340
341 ret = av_packet_copy_props(dst, src);
342 if (ret < 0)
343 return ret;
344
345 if (!src->buf) {
346 ret = packet_alloc(&dst->buf, src->size);
347 if (ret < 0)
348 goto fail;
349 memcpy(dst->buf->data, src->data, src->size);
350 } else {
351 dst->buf = av_buffer_ref(src->buf);
352 if (!dst->buf) {
353 ret = AVERROR(ENOMEM);
354 goto fail;
355 }
356 }
357
358 dst->size = src->size;
359 dst->data = dst->buf->data;
360 return 0;
361 fail:
362 av_packet_free_side_data(dst);
363 return ret;
364 }
365
366 AVPacket *av_packet_clone(AVPacket *src)
367 {
368 AVPacket *ret = av_packet_alloc();
369
370 if (!ret)
371 return ret;
372
373 if (av_packet_ref(ret, src))
374 av_packet_free(&ret);
375
376 return ret;
377 }
378
379 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
380 {
381 *dst = *src;
382 av_init_packet(src);
383 }
384
385 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
386 {
387 if (pkt->pts != AV_NOPTS_VALUE)
388 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
389 if (pkt->dts != AV_NOPTS_VALUE)
390 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
391 if (pkt->duration > 0)
392 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
393 #if FF_API_CONVERGENCE_DURATION
394 FF_DISABLE_DEPRECATION_WARNINGS
395 if (pkt->convergence_duration > 0)
396 pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
397 FF_ENABLE_DEPRECATION_WARNINGS
398 #endif
399 }