Imported Debian version 2.5.0~trusty1.1
[deb_ffmpeg.git] / ffmpeg / libavcodec / avpacket.c
CommitLineData
2ba45a60
DM
1/*
2 * AVPacket functions for libavcodec
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
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#include "bytestream.h"
31#include "internal.h"
32
33#if FF_API_DESTRUCT_PACKET
34
35void av_destruct_packet(AVPacket *pkt)
36{
f6fa7814 37 av_freep(&pkt->data);
2ba45a60
DM
38 pkt->size = 0;
39}
40
41/* a dummy destruct callback for the callers that assume AVPacket.destruct ==
42 * NULL => static data */
43static void dummy_destruct_packet(AVPacket *pkt)
44{
45 av_assert0(0);
46}
47#endif
48
49void av_init_packet(AVPacket *pkt)
50{
51 pkt->pts = AV_NOPTS_VALUE;
52 pkt->dts = AV_NOPTS_VALUE;
53 pkt->pos = -1;
54 pkt->duration = 0;
55 pkt->convergence_duration = 0;
56 pkt->flags = 0;
57 pkt->stream_index = 0;
58#if FF_API_DESTRUCT_PACKET
59FF_DISABLE_DEPRECATION_WARNINGS
60 pkt->destruct = NULL;
61FF_ENABLE_DEPRECATION_WARNINGS
62#endif
63 pkt->buf = NULL;
64 pkt->side_data = NULL;
65 pkt->side_data_elems = 0;
66}
67
68static int packet_alloc(AVBufferRef **buf, int size)
69{
70 int ret;
71 if ((unsigned)size >= (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
72 return AVERROR(EINVAL);
73
74 ret = av_buffer_realloc(buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
75 if (ret < 0)
76 return ret;
77
78 memset((*buf)->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
79
80 return 0;
81}
82
83int av_new_packet(AVPacket *pkt, int size)
84{
85 AVBufferRef *buf = NULL;
86 int ret = packet_alloc(&buf, size);
87 if (ret < 0)
88 return ret;
89
90 av_init_packet(pkt);
91 pkt->buf = buf;
92 pkt->data = buf->data;
93 pkt->size = size;
94#if FF_API_DESTRUCT_PACKET
95FF_DISABLE_DEPRECATION_WARNINGS
96 pkt->destruct = dummy_destruct_packet;
97FF_ENABLE_DEPRECATION_WARNINGS
98#endif
99
100 return 0;
101}
102
103void av_shrink_packet(AVPacket *pkt, int size)
104{
105 if (pkt->size <= size)
106 return;
107 pkt->size = size;
108 memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
109}
110
111int av_grow_packet(AVPacket *pkt, int grow_by)
112{
113 int new_size;
114 av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
115 if (!pkt->size)
116 return av_new_packet(pkt, grow_by);
117 if ((unsigned)grow_by >
118 INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
119 return -1;
120
121 new_size = pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE;
122 if (pkt->buf) {
123 int ret = av_buffer_realloc(&pkt->buf, new_size);
124 if (ret < 0)
125 return ret;
126 } else {
127 pkt->buf = av_buffer_alloc(new_size);
128 if (!pkt->buf)
129 return AVERROR(ENOMEM);
130 memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
131#if FF_API_DESTRUCT_PACKET
132FF_DISABLE_DEPRECATION_WARNINGS
133 pkt->destruct = dummy_destruct_packet;
134FF_ENABLE_DEPRECATION_WARNINGS
135#endif
136 }
137 pkt->data = pkt->buf->data;
138 pkt->size += grow_by;
139 memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
140
141 return 0;
142}
143
144int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
145{
146 if (size >= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
147 return AVERROR(EINVAL);
148
149 pkt->buf = av_buffer_create(data, size + FF_INPUT_BUFFER_PADDING_SIZE,
150 av_buffer_default_free, NULL, 0);
151 if (!pkt->buf)
152 return AVERROR(ENOMEM);
153
154 pkt->data = data;
155 pkt->size = size;
156#if FF_API_DESTRUCT_PACKET
157FF_DISABLE_DEPRECATION_WARNINGS
158 pkt->destruct = dummy_destruct_packet;
159FF_ENABLE_DEPRECATION_WARNINGS
160#endif
161
162 return 0;
163}
164
165#define ALLOC_MALLOC(data, size) data = av_malloc(size)
166#define ALLOC_BUF(data, size) \
167do { \
168 av_buffer_realloc(&pkt->buf, size); \
169 data = pkt->buf ? pkt->buf->data : NULL; \
170} while (0)
171
172#define DUP_DATA(dst, src, size, padding, ALLOC) \
173 do { \
174 void *data; \
175 if (padding) { \
176 if ((unsigned)(size) > \
177 (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE) \
178 goto failed_alloc; \
179 ALLOC(data, size + FF_INPUT_BUFFER_PADDING_SIZE); \
180 } else { \
181 ALLOC(data, size); \
182 } \
183 if (!data) \
184 goto failed_alloc; \
185 memcpy(data, src, size); \
186 if (padding) \
187 memset((uint8_t *)data + size, 0, \
188 FF_INPUT_BUFFER_PADDING_SIZE); \
189 dst = data; \
190 } while (0)
191
192/* Makes duplicates of data, side_data, but does not copy any other fields */
193static int copy_packet_data(AVPacket *pkt, const AVPacket *src, int dup)
194{
195 pkt->data = NULL;
196 pkt->side_data = NULL;
197 if (pkt->buf) {
198 AVBufferRef *ref = av_buffer_ref(src->buf);
199 if (!ref)
200 return AVERROR(ENOMEM);
201 pkt->buf = ref;
202 pkt->data = ref->data;
203 } else {
204 DUP_DATA(pkt->data, src->data, pkt->size, 1, ALLOC_BUF);
205 }
206#if FF_API_DESTRUCT_PACKET
207FF_DISABLE_DEPRECATION_WARNINGS
208 pkt->destruct = dummy_destruct_packet;
209FF_ENABLE_DEPRECATION_WARNINGS
210#endif
211 if (pkt->side_data_elems && dup)
212 pkt->side_data = src->side_data;
213 if (pkt->side_data_elems && !dup) {
214 return av_copy_packet_side_data(pkt, src);
215 }
216 return 0;
217
218failed_alloc:
219 av_free_packet(pkt);
220 return AVERROR(ENOMEM);
221}
222
223int av_copy_packet_side_data(AVPacket *pkt, const AVPacket *src)
224{
225 if (src->side_data_elems) {
226 int i;
227 DUP_DATA(pkt->side_data, src->side_data,
228 src->side_data_elems * sizeof(*src->side_data), 0, ALLOC_MALLOC);
229 if (src != pkt) {
230 memset(pkt->side_data, 0,
231 src->side_data_elems * sizeof(*src->side_data));
232 }
233 for (i = 0; i < src->side_data_elems; i++) {
234 DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
235 src->side_data[i].size, 1, ALLOC_MALLOC);
236 pkt->side_data[i].size = src->side_data[i].size;
237 pkt->side_data[i].type = src->side_data[i].type;
238 }
239 }
240 pkt->side_data_elems = src->side_data_elems;
241 return 0;
242
243failed_alloc:
244 av_free_packet(pkt);
245 return AVERROR(ENOMEM);
246}
247
248int av_dup_packet(AVPacket *pkt)
249{
250 AVPacket tmp_pkt;
251
252FF_DISABLE_DEPRECATION_WARNINGS
253 if (!pkt->buf && pkt->data
254#if FF_API_DESTRUCT_PACKET
255 && !pkt->destruct
256#endif
257 ) {
258FF_ENABLE_DEPRECATION_WARNINGS
259 tmp_pkt = *pkt;
260 return copy_packet_data(pkt, &tmp_pkt, 1);
261 }
262 return 0;
263}
264
265int av_copy_packet(AVPacket *dst, const AVPacket *src)
266{
267 *dst = *src;
268 return copy_packet_data(dst, src, 0);
269}
270
271void av_packet_free_side_data(AVPacket *pkt)
272{
273 int i;
274 for (i = 0; i < pkt->side_data_elems; i++)
f6fa7814 275 av_freep(&pkt->side_data[i].data);
2ba45a60
DM
276 av_freep(&pkt->side_data);
277 pkt->side_data_elems = 0;
278}
279
280void av_free_packet(AVPacket *pkt)
281{
282 if (pkt) {
283FF_DISABLE_DEPRECATION_WARNINGS
284 if (pkt->buf)
285 av_buffer_unref(&pkt->buf);
286#if FF_API_DESTRUCT_PACKET
287 else if (pkt->destruct)
288 pkt->destruct(pkt);
289 pkt->destruct = NULL;
290#endif
291FF_ENABLE_DEPRECATION_WARNINGS
292 pkt->data = NULL;
293 pkt->size = 0;
294
295 av_packet_free_side_data(pkt);
296 }
297}
298
299uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
300 int size)
301{
302 int elems = pkt->side_data_elems;
303
304 if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
305 return NULL;
306 if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
307 return NULL;
308
309 pkt->side_data = av_realloc(pkt->side_data,
310 (elems + 1) * sizeof(*pkt->side_data));
311 if (!pkt->side_data)
312 return NULL;
313
314 pkt->side_data[elems].data = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
315 if (!pkt->side_data[elems].data)
316 return NULL;
317 pkt->side_data[elems].size = size;
318 pkt->side_data[elems].type = type;
319 pkt->side_data_elems++;
320
321 return pkt->side_data[elems].data;
322}
323
324uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
325 int *size)
326{
327 int i;
328
329 for (i = 0; i < pkt->side_data_elems; i++) {
330 if (pkt->side_data[i].type == type) {
331 if (size)
332 *size = pkt->side_data[i].size;
333 return pkt->side_data[i].data;
334 }
335 }
336 return NULL;
337}
338
339#define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
340
341int av_packet_merge_side_data(AVPacket *pkt){
342 if(pkt->side_data_elems){
343 AVBufferRef *buf;
344 int i;
345 uint8_t *p;
346 uint64_t size= pkt->size + 8LL + FF_INPUT_BUFFER_PADDING_SIZE;
347 AVPacket old= *pkt;
348 for (i=0; i<old.side_data_elems; i++) {
349 size += old.side_data[i].size + 5LL;
350 }
351 if (size > INT_MAX)
352 return AVERROR(EINVAL);
353 buf = av_buffer_alloc(size);
354 if (!buf)
355 return AVERROR(ENOMEM);
356 pkt->buf = buf;
357 pkt->data = p = buf->data;
358#if FF_API_DESTRUCT_PACKET
359FF_DISABLE_DEPRECATION_WARNINGS
360 pkt->destruct = dummy_destruct_packet;
361FF_ENABLE_DEPRECATION_WARNINGS
362#endif
363 pkt->size = size - FF_INPUT_BUFFER_PADDING_SIZE;
364 bytestream_put_buffer(&p, old.data, old.size);
365 for (i=old.side_data_elems-1; i>=0; i--) {
366 bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
367 bytestream_put_be32(&p, old.side_data[i].size);
368 *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
369 }
370 bytestream_put_be64(&p, FF_MERGE_MARKER);
371 av_assert0(p-pkt->data == pkt->size);
372 memset(p, 0, FF_INPUT_BUFFER_PADDING_SIZE);
373 av_free_packet(&old);
374 pkt->side_data_elems = 0;
375 pkt->side_data = NULL;
376 return 1;
377 }
378 return 0;
379}
380
381int av_packet_split_side_data(AVPacket *pkt){
382 if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
383 int i;
384 unsigned int size;
385 uint8_t *p;
386
387 p = pkt->data + pkt->size - 8 - 5;
388 for (i=1; ; i++){
389 size = AV_RB32(p);
390 if (size>INT_MAX || p - pkt->data < size)
391 return 0;
392 if (p[4]&128)
393 break;
394 p-= size+5;
395 }
396
397 pkt->side_data = av_malloc_array(i, sizeof(*pkt->side_data));
398 if (!pkt->side_data)
399 return AVERROR(ENOMEM);
400
401 p= pkt->data + pkt->size - 8 - 5;
402 for (i=0; ; i++){
403 size= AV_RB32(p);
404 av_assert0(size<=INT_MAX && p - pkt->data >= size);
405 pkt->side_data[i].data = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
406 pkt->side_data[i].size = size;
407 pkt->side_data[i].type = p[4]&127;
408 if (!pkt->side_data[i].data)
409 return AVERROR(ENOMEM);
410 memcpy(pkt->side_data[i].data, p-size, size);
411 pkt->size -= size + 5;
412 if(p[4]&128)
413 break;
414 p-= size+5;
415 }
416 pkt->size -= 8;
417 pkt->side_data_elems = i+1;
418 return 1;
419 }
420 return 0;
421}
422
423uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size)
424{
425 AVDictionaryEntry *t = NULL;
426 uint8_t *data = NULL;
427 *size = 0;
428
429 if (!dict)
430 return NULL;
431
432 while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX))) {
433 const size_t keylen = strlen(t->key);
434 const size_t valuelen = strlen(t->value);
435 const size_t new_size = *size + keylen + 1 + valuelen + 1;
436 uint8_t *const new_data = av_realloc(data, new_size);
437
438 if (!new_data)
439 goto fail;
440 data = new_data;
441 if (new_size > INT_MAX)
442 goto fail;
443
444 memcpy(data + *size, t->key, keylen + 1);
445 memcpy(data + *size + keylen + 1, t->value, valuelen + 1);
446
447 *size = new_size;
448 }
449
450 return data;
451
452fail:
453 av_freep(&data);
454 *size = 0;
455 return NULL;
456}
457
458int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict)
459{
460 const uint8_t *end = data + size;
461 int ret = 0;
462
463 if (!dict || !data || !size)
464 return ret;
465 if (size && end[-1])
466 return AVERROR_INVALIDDATA;
467 while (data < end) {
468 const uint8_t *key = data;
469 const uint8_t *val = data + strlen(key) + 1;
470
471 if (val >= end)
472 return AVERROR_INVALIDDATA;
473
474 ret = av_dict_set(dict, key, val, 0);
475 if (ret < 0)
476 break;
477 data = val + strlen(val) + 1;
478 }
479
480 return ret;
481}
482
483int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
484 int size)
485{
486 int i;
487
488 for (i = 0; i < pkt->side_data_elems; i++) {
489 if (pkt->side_data[i].type == type) {
490 if (size > pkt->side_data[i].size)
491 return AVERROR(ENOMEM);
492 pkt->side_data[i].size = size;
493 return 0;
494 }
495 }
496 return AVERROR(ENOENT);
497}
498
499int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
500{
501 int i;
502
503 dst->pts = src->pts;
504 dst->dts = src->dts;
505 dst->pos = src->pos;
506 dst->duration = src->duration;
507 dst->convergence_duration = src->convergence_duration;
508 dst->flags = src->flags;
509 dst->stream_index = src->stream_index;
510
511 for (i = 0; i < src->side_data_elems; i++) {
512 enum AVPacketSideDataType type = src->side_data[i].type;
513 int size = src->side_data[i].size;
514 uint8_t *src_data = src->side_data[i].data;
515 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
516
517 if (!dst_data) {
518 av_packet_free_side_data(dst);
519 return AVERROR(ENOMEM);
520 }
521 memcpy(dst_data, src_data, size);
522 }
523
524 return 0;
525}
526
527void av_packet_unref(AVPacket *pkt)
528{
529 av_packet_free_side_data(pkt);
530 av_buffer_unref(&pkt->buf);
531 av_init_packet(pkt);
532 pkt->data = NULL;
533 pkt->size = 0;
534}
535
536int av_packet_ref(AVPacket *dst, const AVPacket *src)
537{
538 int ret;
539
540 ret = av_packet_copy_props(dst, src);
541 if (ret < 0)
542 return ret;
543
544 if (!src->buf) {
545 ret = packet_alloc(&dst->buf, src->size);
546 if (ret < 0)
547 goto fail;
548 memcpy(dst->buf->data, src->data, src->size);
549 } else
550 dst->buf = av_buffer_ref(src->buf);
551
552 dst->size = src->size;
553 dst->data = dst->buf->data;
554 return 0;
555fail:
556 av_packet_free_side_data(dst);
557 return ret;
558}
559
560void av_packet_move_ref(AVPacket *dst, AVPacket *src)
561{
562 *dst = *src;
563 av_init_packet(src);
564}
565
566void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
567{
568 if (pkt->pts != AV_NOPTS_VALUE)
569 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
570 if (pkt->dts != AV_NOPTS_VALUE)
571 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
572 if (pkt->duration > 0)
573 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
574 if (pkt->convergence_duration > 0)
575 pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
576}