]> git.sesse.net Git - ffmpeg/blob - libavcodec/avpacket.c
avcodec: add const qualifier to avcodec_find_best_pix_fmt2 args
[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_free_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         if (src != pkt) {
229             memset(pkt->side_data, 0,
230                    src->side_data_elems * sizeof(*src->side_data));
231         }
232         for (i = 0; i < src->side_data_elems; i++) {
233             DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
234                     src->side_data[i].size, 1, ALLOC_MALLOC);
235             pkt->side_data[i].size = src->side_data[i].size;
236             pkt->side_data[i].type = src->side_data[i].type;
237         }
238     }
239     return 0;
240
241 failed_alloc:
242     av_free_packet(pkt);
243     return AVERROR(ENOMEM);
244 }
245
246 int av_dup_packet(AVPacket *pkt)
247 {
248     AVPacket tmp_pkt;
249
250 FF_DISABLE_DEPRECATION_WARNINGS
251     if (!pkt->buf && pkt->data
252 #if FF_API_DESTRUCT_PACKET
253         && !pkt->destruct
254 #endif
255         ) {
256 FF_ENABLE_DEPRECATION_WARNINGS
257         tmp_pkt = *pkt;
258         return copy_packet_data(pkt, &tmp_pkt, 1);
259     }
260     return 0;
261 }
262
263 int av_copy_packet(AVPacket *dst, AVPacket *src)
264 {
265     *dst = *src;
266     return copy_packet_data(dst, src, 0);
267 }
268
269 void av_packet_free_side_data(AVPacket *pkt)
270 {
271     int i;
272     for (i = 0; i < pkt->side_data_elems; i++)
273         av_free(pkt->side_data[i].data);
274     av_freep(&pkt->side_data);
275     pkt->side_data_elems = 0;
276 }
277
278 void av_free_packet(AVPacket *pkt)
279 {
280     if (pkt) {
281 FF_DISABLE_DEPRECATION_WARNINGS
282         if (pkt->buf)
283             av_buffer_unref(&pkt->buf);
284 #if FF_API_DESTRUCT_PACKET
285         else if (pkt->destruct)
286             pkt->destruct(pkt);
287         pkt->destruct = NULL;
288 #endif
289 FF_ENABLE_DEPRECATION_WARNINGS
290         pkt->data            = NULL;
291         pkt->size            = 0;
292
293         av_packet_free_side_data(pkt);
294     }
295 }
296
297 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
298                                  int size)
299 {
300     int elems = pkt->side_data_elems;
301
302     if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
303         return NULL;
304     if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
305         return NULL;
306
307     pkt->side_data = av_realloc(pkt->side_data,
308                                 (elems + 1) * sizeof(*pkt->side_data));
309     if (!pkt->side_data)
310         return NULL;
311
312     pkt->side_data[elems].data = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
313     if (!pkt->side_data[elems].data)
314         return NULL;
315     pkt->side_data[elems].size = size;
316     pkt->side_data[elems].type = type;
317     pkt->side_data_elems++;
318
319     return pkt->side_data[elems].data;
320 }
321
322 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
323                                  int *size)
324 {
325     int i;
326
327     for (i = 0; i < pkt->side_data_elems; i++) {
328         if (pkt->side_data[i].type == type) {
329             if (size)
330                 *size = pkt->side_data[i].size;
331             return pkt->side_data[i].data;
332         }
333     }
334     return NULL;
335 }
336
337 #define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
338
339 int av_packet_merge_side_data(AVPacket *pkt){
340     if(pkt->side_data_elems){
341         AVBufferRef *buf;
342         int i;
343         uint8_t *p;
344         uint64_t size= pkt->size + 8LL + FF_INPUT_BUFFER_PADDING_SIZE;
345         AVPacket old= *pkt;
346         for (i=0; i<old.side_data_elems; i++) {
347             size += old.side_data[i].size + 5LL;
348         }
349         if (size > INT_MAX)
350             return AVERROR(EINVAL);
351         buf = av_buffer_alloc(size);
352         if (!buf)
353             return AVERROR(ENOMEM);
354         pkt->buf = buf;
355         pkt->data = p = buf->data;
356 #if FF_API_DESTRUCT_PACKET
357 FF_DISABLE_DEPRECATION_WARNINGS
358         pkt->destruct = dummy_destruct_packet;
359 FF_ENABLE_DEPRECATION_WARNINGS
360 #endif
361         pkt->size = size - FF_INPUT_BUFFER_PADDING_SIZE;
362         bytestream_put_buffer(&p, old.data, old.size);
363         for (i=old.side_data_elems-1; i>=0; i--) {
364             bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
365             bytestream_put_be32(&p, old.side_data[i].size);
366             *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
367         }
368         bytestream_put_be64(&p, FF_MERGE_MARKER);
369         av_assert0(p-pkt->data == pkt->size);
370         memset(p, 0, FF_INPUT_BUFFER_PADDING_SIZE);
371         av_free_packet(&old);
372         pkt->side_data_elems = 0;
373         pkt->side_data = NULL;
374         return 1;
375     }
376     return 0;
377 }
378
379 int av_packet_split_side_data(AVPacket *pkt){
380     if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
381         int i;
382         unsigned int size, orig_pktsize = pkt->size;
383         uint8_t *p;
384
385         p = pkt->data + pkt->size - 8 - 5;
386         for (i=1; ; i++){
387             size = AV_RB32(p);
388             if (size>INT_MAX || p - pkt->data < size)
389                 return 0;
390             if (p[4]&128)
391                 break;
392             p-= size+5;
393         }
394
395         pkt->side_data = av_malloc(i * sizeof(*pkt->side_data));
396         if (!pkt->side_data)
397             return AVERROR(ENOMEM);
398
399         p= pkt->data + pkt->size - 8 - 5;
400         for (i=0; ; i++){
401             size= AV_RB32(p);
402             av_assert0(size<=INT_MAX && p - pkt->data >= size);
403             pkt->side_data[i].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
404             pkt->side_data[i].size = size;
405             pkt->side_data[i].type = p[4]&127;
406             if (!pkt->side_data[i].data)
407                 return AVERROR(ENOMEM);
408             memcpy(pkt->side_data[i].data, p-size, size);
409             pkt->size -= size + 5;
410             if(p[4]&128)
411                 break;
412             p-= size+5;
413         }
414         pkt->size -= 8;
415         /* FFMIN() prevents overflow in case the packet wasn't allocated with
416          * proper padding.
417          * If the side data is smaller than the buffer padding size, the
418          * remaining bytes should have already been filled with zeros by the
419          * original packet allocation anyway. */
420         memset(pkt->data + pkt->size, 0,
421                FFMIN(orig_pktsize - pkt->size, FF_INPUT_BUFFER_PADDING_SIZE));
422         pkt->side_data_elems = i+1;
423         return 1;
424     }
425     return 0;
426 }
427
428 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
429                                int size)
430 {
431     int i;
432
433     for (i = 0; i < pkt->side_data_elems; i++) {
434         if (pkt->side_data[i].type == type) {
435             if (size > pkt->side_data[i].size)
436                 return AVERROR(ENOMEM);
437             pkt->side_data[i].size = size;
438             return 0;
439         }
440     }
441     return AVERROR(ENOENT);
442 }
443
444 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
445 {
446     int i;
447
448     dst->pts                  = src->pts;
449     dst->dts                  = src->dts;
450     dst->pos                  = src->pos;
451     dst->duration             = src->duration;
452     dst->convergence_duration = src->convergence_duration;
453     dst->flags                = src->flags;
454     dst->stream_index         = src->stream_index;
455     dst->side_data_elems      = src->side_data_elems;
456
457     for (i = 0; i < src->side_data_elems; i++) {
458          enum AVPacketSideDataType type = src->side_data[i].type;
459          int size          = src->side_data[i].size;
460          uint8_t *src_data = src->side_data[i].data;
461          uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
462
463         if (!dst_data) {
464             av_packet_free_side_data(dst);
465             return AVERROR(ENOMEM);
466         }
467         memcpy(dst_data, src_data, size);
468     }
469
470     return 0;
471 }
472
473 void av_packet_unref(AVPacket *pkt)
474 {
475     av_packet_free_side_data(pkt);
476     av_buffer_unref(&pkt->buf);
477     av_init_packet(pkt);
478     pkt->data = NULL;
479     pkt->size = 0;
480 }
481
482 int av_packet_ref(AVPacket *dst, AVPacket *src)
483 {
484     int ret;
485
486     ret = av_packet_copy_props(dst, src);
487     if (ret < 0)
488         return ret;
489
490     if (!src->buf) {
491         ret = packet_alloc(&dst->buf, src->size);
492         if (ret < 0)
493             goto fail;
494         memcpy(dst->buf->data, src->data, src->size);
495     } else
496         dst->buf = av_buffer_ref(src->buf);
497
498     dst->size = src->size;
499     dst->data = dst->buf->data;
500     return 0;
501 fail:
502     av_packet_free_side_data(dst);
503     return ret;
504 }
505
506 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
507 {
508     *dst = *src;
509     av_init_packet(src);
510 }