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