avpacket: properly reset data/size in av_packet_move_ref()
[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 (size < 0 || size >= INT_MAX - 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 int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
241 uint8_t *data, size_t size)
242 {
243 int elems = pkt->side_data_elems;
244
245 if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
246 return AVERROR(ERANGE);
247
248 pkt->side_data = av_realloc(pkt->side_data,
249 (elems + 1) * sizeof(*pkt->side_data));
250 if (!pkt->side_data)
251 return AVERROR(ENOMEM);
252
253 pkt->side_data[elems].data = data;
254 pkt->side_data[elems].size = size;
255 pkt->side_data[elems].type = type;
256 pkt->side_data_elems++;
257
258 return 0;
259 }
260
261
262 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
263 int size)
264 {
265 int ret;
266 uint8_t *data;
267
268 if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
269 return NULL;
270 data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
271 if (!data)
272 return NULL;
273
274 ret = av_packet_add_side_data(pkt, type, data, size);
275 if (ret < 0) {
276 av_freep(&data);
277 return NULL;
278 }
279
280 return data;
281 }
282
283 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
284 int *size)
285 {
286 int i;
287
288 for (i = 0; i < pkt->side_data_elems; i++) {
289 if (pkt->side_data[i].type == type) {
290 if (size)
291 *size = pkt->side_data[i].size;
292 return pkt->side_data[i].data;
293 }
294 }
295 return NULL;
296 }
297
298 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
299 int size)
300 {
301 int i;
302
303 for (i = 0; i < pkt->side_data_elems; i++) {
304 if (pkt->side_data[i].type == type) {
305 if (size > pkt->side_data[i].size)
306 return AVERROR(ENOMEM);
307 pkt->side_data[i].size = size;
308 return 0;
309 }
310 }
311 return AVERROR(ENOENT);
312 }
313
314 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
315 {
316 int i;
317
318 dst->pts = src->pts;
319 dst->dts = src->dts;
320 dst->pos = src->pos;
321 dst->duration = src->duration;
322 #if FF_API_CONVERGENCE_DURATION
323 FF_DISABLE_DEPRECATION_WARNINGS
324 dst->convergence_duration = src->convergence_duration;
325 FF_ENABLE_DEPRECATION_WARNINGS
326 #endif
327 dst->flags = src->flags;
328 dst->stream_index = src->stream_index;
329
330 for (i = 0; i < src->side_data_elems; i++) {
331 enum AVPacketSideDataType type = src->side_data[i].type;
332 int size = src->side_data[i].size;
333 uint8_t *src_data = src->side_data[i].data;
334 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
335
336 if (!dst_data) {
337 av_packet_free_side_data(dst);
338 return AVERROR(ENOMEM);
339 }
340 memcpy(dst_data, src_data, size);
341 }
342
343 return 0;
344 }
345
346 void av_packet_unref(AVPacket *pkt)
347 {
348 av_packet_free_side_data(pkt);
349 av_buffer_unref(&pkt->buf);
350 av_init_packet(pkt);
351 pkt->data = NULL;
352 pkt->size = 0;
353 }
354
355 int av_packet_ref(AVPacket *dst, AVPacket *src)
356 {
357 int ret;
358
359 ret = av_packet_copy_props(dst, src);
360 if (ret < 0)
361 return ret;
362
363 if (!src->buf) {
364 ret = packet_alloc(&dst->buf, src->size);
365 if (ret < 0)
366 goto fail;
367 memcpy(dst->buf->data, src->data, src->size);
368 } else {
369 dst->buf = av_buffer_ref(src->buf);
370 if (!dst->buf) {
371 ret = AVERROR(ENOMEM);
372 goto fail;
373 }
374 }
375
376 dst->size = src->size;
377 dst->data = dst->buf->data;
378 return 0;
379 fail:
380 av_packet_free_side_data(dst);
381 return ret;
382 }
383
384 AVPacket *av_packet_clone(AVPacket *src)
385 {
386 AVPacket *ret = av_packet_alloc();
387
388 if (!ret)
389 return ret;
390
391 if (av_packet_ref(ret, src))
392 av_packet_free(&ret);
393
394 return ret;
395 }
396
397 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
398 {
399 *dst = *src;
400 av_init_packet(src);
401 src->data = NULL;
402 src->size = 0;
403 }
404
405 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
406 {
407 if (pkt->pts != AV_NOPTS_VALUE)
408 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
409 if (pkt->dts != AV_NOPTS_VALUE)
410 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
411 if (pkt->duration > 0)
412 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
413 #if FF_API_CONVERGENCE_DURATION
414 FF_DISABLE_DEPRECATION_WARNINGS
415 if (pkt->convergence_duration > 0)
416 pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
417 FF_ENABLE_DEPRECATION_WARNINGS
418 #endif
419 }