]> git.sesse.net Git - ffmpeg/blob - libavcodec/avpacket.c
mpeg12dec: also print progressive seq and chroma format
[ffmpeg] / libavcodec / avpacket.c
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/mem.h"
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "internal.h"
31
32 #if FF_API_DESTRUCT_PACKET
33
34 void av_destruct_packet(AVPacket *pkt)
35 {
36     av_free(pkt->data);
37     pkt->data = NULL;
38     pkt->size = 0;
39 }
40
41 /* a dummy destruct callback for the callers that assume AVPacket.destruct ==
42  * NULL => static data */
43 static void dummy_destruct_packet(AVPacket *pkt)
44 {
45     av_assert0(0);
46 }
47 #endif
48
49 void 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
59 FF_DISABLE_DEPRECATION_WARNINGS
60     pkt->destruct             = NULL;
61 FF_ENABLE_DEPRECATION_WARNINGS
62 #endif
63     pkt->buf                  = NULL;
64     pkt->side_data            = NULL;
65     pkt->side_data_elems      = 0;
66 }
67
68 static int packet_alloc(AVBufferRef **buf, int size)
69 {
70     if ((unsigned)size >= (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
71         return AVERROR(EINVAL);
72
73     av_buffer_realloc(buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
74     if (!*buf)
75         return AVERROR(ENOMEM);
76
77     memset((*buf)->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
78
79     return 0;
80 }
81
82 int av_new_packet(AVPacket *pkt, int size)
83 {
84     AVBufferRef *buf = NULL;
85     int ret = packet_alloc(&buf, size);
86     if (ret < 0)
87         return ret;
88
89     av_init_packet(pkt);
90     pkt->buf      = buf;
91     pkt->data     = buf->data;
92     pkt->size     = size;
93 #if FF_API_DESTRUCT_PACKET
94 FF_DISABLE_DEPRECATION_WARNINGS
95     pkt->destruct = dummy_destruct_packet;
96 FF_ENABLE_DEPRECATION_WARNINGS
97 #endif
98
99     return 0;
100 }
101
102 void av_shrink_packet(AVPacket *pkt, int size)
103 {
104     if (pkt->size <= size)
105         return;
106     pkt->size = size;
107     memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
108 }
109
110 int av_grow_packet(AVPacket *pkt, int grow_by)
111 {
112     int new_size;
113     av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
114     if (!pkt->size)
115         return av_new_packet(pkt, grow_by);
116     if ((unsigned)grow_by >
117         INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
118         return -1;
119
120     new_size = pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE;
121     if (pkt->buf) {
122         int ret = av_buffer_realloc(&pkt->buf, new_size);
123         if (ret < 0)
124             return ret;
125     } else {
126         pkt->buf = av_buffer_alloc(new_size);
127         if (!pkt->buf)
128             return AVERROR(ENOMEM);
129         memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
130 #if FF_API_DESTRUCT_PACKET
131 FF_DISABLE_DEPRECATION_WARNINGS
132         pkt->destruct = dummy_destruct_packet;
133 FF_ENABLE_DEPRECATION_WARNINGS
134 #endif
135     }
136     pkt->data  = pkt->buf->data;
137     pkt->size += grow_by;
138     memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
139
140     return 0;
141 }
142
143 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
144 {
145     if (size >= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
146         return AVERROR(EINVAL);
147
148     pkt->buf = av_buffer_create(data, size + FF_INPUT_BUFFER_PADDING_SIZE,
149                                 av_buffer_default_free, NULL, 0);
150     if (!pkt->buf)
151         return AVERROR(ENOMEM);
152
153     pkt->data = data;
154     pkt->size = size;
155 #if FF_API_DESTRUCT_PACKET
156 FF_DISABLE_DEPRECATION_WARNINGS
157     pkt->destruct = dummy_destruct_packet;
158 FF_ENABLE_DEPRECATION_WARNINGS
159 #endif
160
161     return 0;
162 }
163
164 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
165 #define ALLOC_BUF(data, size)                \
166 do {                                         \
167     av_buffer_realloc(&pkt->buf, size);      \
168     data = pkt->buf ? pkt->buf->data : NULL; \
169 } while (0)
170
171 #define DUP_DATA(dst, src, size, padding, ALLOC)                        \
172     do {                                                                \
173         void *data;                                                     \
174         if (padding) {                                                  \
175             if ((unsigned)(size) >                                      \
176                 (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE)        \
177                 goto failed_alloc;                                      \
178             ALLOC(data, size + FF_INPUT_BUFFER_PADDING_SIZE);           \
179         } else {                                                        \
180             ALLOC(data, size);                                          \
181         }                                                               \
182         if (!data)                                                      \
183             goto failed_alloc;                                          \
184         memcpy(data, src, size);                                        \
185         if (padding)                                                    \
186             memset((uint8_t *)data + size, 0,                           \
187                    FF_INPUT_BUFFER_PADDING_SIZE);                       \
188         dst = data;                                                     \
189     } while (0)
190
191 /* Makes duplicates of data, side_data, but does not copy any other fields */
192 static int copy_packet_data(AVPacket *pkt, AVPacket *src, int dup)
193 {
194     pkt->data      = NULL;
195     pkt->side_data = NULL;
196     if (pkt->buf) {
197         AVBufferRef *ref = av_buffer_ref(src->buf);
198         if (!ref)
199             return AVERROR(ENOMEM);
200         pkt->buf  = ref;
201         pkt->data = ref->data;
202     } else {
203         DUP_DATA(pkt->data, src->data, pkt->size, 1, ALLOC_BUF);
204     }
205 #if FF_API_DESTRUCT_PACKET
206 FF_DISABLE_DEPRECATION_WARNINGS
207     pkt->destruct = dummy_destruct_packet;
208 FF_ENABLE_DEPRECATION_WARNINGS
209 #endif
210     if (pkt->side_data_elems && dup)
211         pkt->side_data = src->side_data;
212     if (pkt->side_data_elems && !dup) {
213         return av_copy_packet_side_data(pkt, src);
214     }
215     return 0;
216
217 failed_alloc:
218     av_destruct_packet(pkt);
219     return AVERROR(ENOMEM);
220 }
221
222 int av_copy_packet_side_data(AVPacket *pkt, AVPacket *src)
223 {
224     if (src->side_data_elems) {
225         int i;
226         DUP_DATA(pkt->side_data, src->side_data,
227                 src->side_data_elems * sizeof(*src->side_data), 0, ALLOC_MALLOC);
228         memset(pkt->side_data, 0,
229                 src->side_data_elems * sizeof(*src->side_data));
230         for (i = 0; i < src->side_data_elems; i++) {
231             DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
232                     src->side_data[i].size, 1, ALLOC_MALLOC);
233             pkt->side_data[i].size = src->side_data[i].size;
234             pkt->side_data[i].type = src->side_data[i].type;
235         }
236     }
237     return 0;
238
239 failed_alloc:
240     av_destruct_packet(pkt);
241     return AVERROR(ENOMEM);
242 }
243
244 int av_dup_packet(AVPacket *pkt)
245 {
246     AVPacket tmp_pkt;
247
248 FF_DISABLE_DEPRECATION_WARNINGS
249     if (!pkt->buf && pkt->data
250 #if FF_API_DESTRUCT_PACKET
251         && !pkt->destruct
252 #endif
253         ) {
254 FF_ENABLE_DEPRECATION_WARNINGS
255         tmp_pkt = *pkt;
256         return copy_packet_data(pkt, &tmp_pkt, 1);
257     }
258     return 0;
259 }
260
261 int av_copy_packet(AVPacket *dst, AVPacket *src)
262 {
263     *dst = *src;
264     return copy_packet_data(dst, src, 0);
265 }
266
267 void av_packet_free_side_data(AVPacket *pkt)
268 {
269     int i;
270     for (i = 0; i < pkt->side_data_elems; i++)
271         av_free(pkt->side_data[i].data);
272     av_freep(&pkt->side_data);
273     pkt->side_data_elems = 0;
274 }
275
276 void av_free_packet(AVPacket *pkt)
277 {
278     if (pkt) {
279 FF_DISABLE_DEPRECATION_WARNINGS
280         if (pkt->buf)
281             av_buffer_unref(&pkt->buf);
282 #if FF_API_DESTRUCT_PACKET
283         else if (pkt->destruct)
284             pkt->destruct(pkt);
285         pkt->destruct = NULL;
286 #endif
287 FF_ENABLE_DEPRECATION_WARNINGS
288         pkt->data            = NULL;
289         pkt->size            = 0;
290
291         av_packet_free_side_data(pkt);
292     }
293 }
294
295 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
296                                  int size)
297 {
298     int elems = pkt->side_data_elems;
299
300     if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
301         return NULL;
302     if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
303         return NULL;
304
305     pkt->side_data = av_realloc(pkt->side_data,
306                                 (elems + 1) * sizeof(*pkt->side_data));
307     if (!pkt->side_data)
308         return NULL;
309
310     pkt->side_data[elems].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
311     if (!pkt->side_data[elems].data)
312         return NULL;
313     pkt->side_data[elems].size = size;
314     pkt->side_data[elems].type = type;
315     pkt->side_data_elems++;
316
317     return pkt->side_data[elems].data;
318 }
319
320 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
321                                  int *size)
322 {
323     int i;
324
325     for (i = 0; i < pkt->side_data_elems; i++) {
326         if (pkt->side_data[i].type == type) {
327             if (size)
328                 *size = pkt->side_data[i].size;
329             return pkt->side_data[i].data;
330         }
331     }
332     return NULL;
333 }
334
335 #define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
336
337 int av_packet_merge_side_data(AVPacket *pkt){
338     if(pkt->side_data_elems){
339         AVBufferRef *buf;
340         int i;
341         uint8_t *p;
342         uint64_t size= pkt->size + 8LL + FF_INPUT_BUFFER_PADDING_SIZE;
343         AVPacket old= *pkt;
344         for (i=0; i<old.side_data_elems; i++) {
345             size += old.side_data[i].size + 5LL;
346         }
347         if (size > INT_MAX)
348             return AVERROR(EINVAL);
349         buf = av_buffer_alloc(size);
350         if (!buf)
351             return AVERROR(ENOMEM);
352         pkt->buf = buf;
353         pkt->data = p = buf->data;
354 #if FF_API_DESTRUCT_PACKET
355 FF_DISABLE_DEPRECATION_WARNINGS
356         pkt->destruct = dummy_destruct_packet;
357 FF_ENABLE_DEPRECATION_WARNINGS
358 #endif
359         pkt->size = size - FF_INPUT_BUFFER_PADDING_SIZE;
360         bytestream_put_buffer(&p, old.data, old.size);
361         for (i=old.side_data_elems-1; i>=0; i--) {
362             bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
363             bytestream_put_be32(&p, old.side_data[i].size);
364             *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
365         }
366         bytestream_put_be64(&p, FF_MERGE_MARKER);
367         av_assert0(p-pkt->data == pkt->size);
368         memset(p, 0, FF_INPUT_BUFFER_PADDING_SIZE);
369         av_free_packet(&old);
370         pkt->side_data_elems = 0;
371         pkt->side_data = NULL;
372         return 1;
373     }
374     return 0;
375 }
376
377 int av_packet_split_side_data(AVPacket *pkt){
378     if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
379         int i;
380         unsigned int size, orig_pktsize = pkt->size;
381         uint8_t *p;
382
383         p = pkt->data + pkt->size - 8 - 5;
384         for (i=1; ; i++){
385             size = AV_RB32(p);
386             if (size>INT_MAX || p - pkt->data < size)
387                 return 0;
388             if (p[4]&128)
389                 break;
390             p-= size+5;
391         }
392
393         pkt->side_data = av_malloc(i * sizeof(*pkt->side_data));
394         if (!pkt->side_data)
395             return AVERROR(ENOMEM);
396
397         p= pkt->data + pkt->size - 8 - 5;
398         for (i=0; ; i++){
399             size= AV_RB32(p);
400             av_assert0(size<=INT_MAX && p - pkt->data >= size);
401             pkt->side_data[i].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
402             pkt->side_data[i].size = size;
403             pkt->side_data[i].type = p[4]&127;
404             if (!pkt->side_data[i].data)
405                 return AVERROR(ENOMEM);
406             memcpy(pkt->side_data[i].data, p-size, size);
407             pkt->size -= size + 5;
408             if(p[4]&128)
409                 break;
410             p-= size+5;
411         }
412         pkt->size -= 8;
413         /* FFMIN() prevents overflow in case the packet wasn't allocated with
414          * proper padding.
415          * If the side data is smaller than the buffer padding size, the
416          * remaining bytes should have already been filled with zeros by the
417          * original packet allocation anyway. */
418         memset(pkt->data + pkt->size, 0,
419                FFMIN(orig_pktsize - pkt->size, FF_INPUT_BUFFER_PADDING_SIZE));
420         pkt->side_data_elems = i+1;
421         return 1;
422     }
423     return 0;
424 }
425
426 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
427                                int size)
428 {
429     int i;
430
431     for (i = 0; i < pkt->side_data_elems; i++) {
432         if (pkt->side_data[i].type == type) {
433             if (size > pkt->side_data[i].size)
434                 return AVERROR(ENOMEM);
435             pkt->side_data[i].size = size;
436             return 0;
437         }
438     }
439     return AVERROR(ENOENT);
440 }
441
442 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
443 {
444     int i;
445
446     dst->pts                  = src->pts;
447     dst->dts                  = src->dts;
448     dst->pos                  = src->pos;
449     dst->duration             = src->duration;
450     dst->convergence_duration = src->convergence_duration;
451     dst->flags                = src->flags;
452     dst->stream_index         = src->stream_index;
453     dst->side_data_elems      = src->side_data_elems;
454
455     for (i = 0; i < src->side_data_elems; i++) {
456          enum AVPacketSideDataType type = src->side_data[i].type;
457          int size          = src->side_data[i].size;
458          uint8_t *src_data = src->side_data[i].data;
459          uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
460
461         if (!dst_data) {
462             av_packet_free_side_data(dst);
463             return AVERROR(ENOMEM);
464         }
465         memcpy(dst_data, src_data, size);
466     }
467
468     return 0;
469 }
470
471 void av_packet_unref(AVPacket *pkt)
472 {
473     av_packet_free_side_data(pkt);
474     av_buffer_unref(&pkt->buf);
475     av_init_packet(pkt);
476     pkt->data = NULL;
477     pkt->size = 0;
478 }
479
480 int av_packet_ref(AVPacket *dst, AVPacket *src)
481 {
482     int ret;
483
484     ret = av_packet_copy_props(dst, src);
485     if (ret < 0)
486         return ret;
487
488     if (!src->buf) {
489         ret = packet_alloc(&dst->buf, src->size);
490         if (ret < 0)
491             goto fail;
492         memcpy(dst->buf->data, src->data, src->size);
493     } else
494         dst->buf = av_buffer_ref(src->buf);
495
496     dst->size = src->size;
497     dst->data = dst->buf->data;
498     return 0;
499 fail:
500     av_packet_free_side_data(dst);
501     return ret;
502 }
503
504 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
505 {
506     *dst = *src;
507     av_init_packet(src);
508 }