]> git.sesse.net Git - ffmpeg/blob - libavformat/flvdec.c
mov: move stsd finalization to an appropriate place
[ffmpeg] / libavformat / flvdec.c
1 /*
2  * FLV demuxer
3  * Copyright (c) 2003 The Libav Project
4  *
5  * This demuxer will generate a 1 byte extradata for VP6F content.
6  * It is composed of:
7  *  - upper 4 bits: difference between encoded width and visible width
8  *  - lower 4 bits: difference between encoded height and visible height
9  *
10  * This file is part of Libav.
11  *
12  * Libav is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * Libav is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with Libav; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  */
26
27 #include "libavutil/avstring.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/intfloat.h"
32 #include "libavutil/mathematics.h"
33 #include "libavcodec/bytestream.h"
34 #include "libavcodec/mpeg4audio.h"
35 #include "avformat.h"
36 #include "internal.h"
37 #include "avio_internal.h"
38 #include "flv.h"
39
40 #define KEYFRAMES_TAG            "keyframes"
41 #define KEYFRAMES_TIMESTAMP_TAG  "times"
42 #define KEYFRAMES_BYTEOFFSET_TAG "filepositions"
43
44 #define VALIDATE_INDEX_TS_THRESH 2500
45
46 typedef struct FLVContext {
47     const AVClass *class; ///< Class for private options.
48     int trust_metadata;   ///< configure streams according onMetaData
49     int wrong_dts;        ///< wrong dts due to negative cts
50     uint8_t *new_extradata[2];
51     int new_extradata_size[2];
52     int last_sample_rate;
53     int last_channels;
54     struct {
55         int64_t dts;
56         int64_t pos;
57     } validate_index[2];
58     int validate_next;
59     int validate_count;
60     int searched_for_end;
61     AVRational framerate;
62 } FLVContext;
63
64 static int flv_probe(AVProbeData *p)
65 {
66     const uint8_t *d;
67
68     d = p->buf;
69     if (d[0] == 'F' &&
70         d[1] == 'L' &&
71         d[2] == 'V' &&
72         d[3] < 5 && d[5] == 0 &&
73         AV_RB32(d + 5) > 8) {
74         return AVPROBE_SCORE_MAX;
75     }
76     return 0;
77 }
78
79 static AVStream *create_stream(AVFormatContext *s, int codec_type)
80 {
81     FLVContext *flv = s->priv_data;
82     AVStream *st = avformat_new_stream(s, NULL);
83     if (!st)
84         return NULL;
85     st->codecpar->codec_type = codec_type;
86     if (codec_type == AVMEDIA_TYPE_VIDEO)
87         st->avg_frame_rate = flv->framerate;
88
89     avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
90     return st;
91 }
92
93 static int flv_same_audio_codec(AVCodecParameters *apar, int flags)
94 {
95     int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
96     int flv_codecid           = flags & FLV_AUDIO_CODECID_MASK;
97     int codec_id;
98
99     if (!apar->codec_id && !apar->codec_tag)
100         return 1;
101
102     if (apar->bits_per_coded_sample != bits_per_coded_sample)
103         return 0;
104
105     switch (flv_codecid) {
106     // no distinction between S16 and S8 PCM codec flags
107     case FLV_CODECID_PCM:
108         codec_id = bits_per_coded_sample == 8
109                    ? AV_CODEC_ID_PCM_U8
110 #if HAVE_BIGENDIAN
111                    : AV_CODEC_ID_PCM_S16BE;
112 #else
113                    : AV_CODEC_ID_PCM_S16LE;
114 #endif
115         return codec_id == apar->codec_id;
116     case FLV_CODECID_PCM_LE:
117         codec_id = bits_per_coded_sample == 8
118                    ? AV_CODEC_ID_PCM_U8
119                    : AV_CODEC_ID_PCM_S16LE;
120         return codec_id == apar->codec_id;
121     case FLV_CODECID_AAC:
122         return apar->codec_id == AV_CODEC_ID_AAC;
123     case FLV_CODECID_ADPCM:
124         return apar->codec_id == AV_CODEC_ID_ADPCM_SWF;
125     case FLV_CODECID_SPEEX:
126         return apar->codec_id == AV_CODEC_ID_SPEEX;
127     case FLV_CODECID_MP3:
128         return apar->codec_id == AV_CODEC_ID_MP3;
129     case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
130     case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
131     case FLV_CODECID_NELLYMOSER:
132         return apar->codec_id == AV_CODEC_ID_NELLYMOSER;
133     case FLV_CODECID_PCM_MULAW:
134         return apar->sample_rate == 8000 &&
135                apar->codec_id    == AV_CODEC_ID_PCM_MULAW;
136     case FLV_CODECID_PCM_ALAW:
137         return apar->sample_rate == 8000 &&
138                apar->codec_id    == AV_CODEC_ID_PCM_ALAW;
139     default:
140         return apar->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
141     }
142 }
143
144 static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream,
145                                 AVCodecParameters *apar, int flv_codecid)
146 {
147     switch (flv_codecid) {
148     // no distinction between S16 and S8 PCM codec flags
149     case FLV_CODECID_PCM:
150         apar->codec_id = apar->bits_per_coded_sample == 8
151                            ? AV_CODEC_ID_PCM_U8
152 #if HAVE_BIGENDIAN
153                            : AV_CODEC_ID_PCM_S16BE;
154 #else
155                            : AV_CODEC_ID_PCM_S16LE;
156 #endif
157         break;
158     case FLV_CODECID_PCM_LE:
159         apar->codec_id = apar->bits_per_coded_sample == 8
160                            ? AV_CODEC_ID_PCM_U8
161                            : AV_CODEC_ID_PCM_S16LE;
162         break;
163     case FLV_CODECID_AAC:
164         apar->codec_id = AV_CODEC_ID_AAC;
165         break;
166     case FLV_CODECID_ADPCM:
167         apar->codec_id = AV_CODEC_ID_ADPCM_SWF;
168         break;
169     case FLV_CODECID_SPEEX:
170         apar->codec_id    = AV_CODEC_ID_SPEEX;
171         apar->sample_rate = 16000;
172         break;
173     case FLV_CODECID_MP3:
174         apar->codec_id      = AV_CODEC_ID_MP3;
175         astream->need_parsing = AVSTREAM_PARSE_FULL;
176         break;
177     case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
178         // in case metadata does not otherwise declare samplerate
179         apar->sample_rate = 8000;
180         apar->codec_id    = AV_CODEC_ID_NELLYMOSER;
181         break;
182     case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
183         apar->sample_rate = 16000;
184         apar->codec_id    = AV_CODEC_ID_NELLYMOSER;
185         break;
186     case FLV_CODECID_NELLYMOSER:
187         apar->codec_id = AV_CODEC_ID_NELLYMOSER;
188         break;
189     case FLV_CODECID_PCM_MULAW:
190         apar->sample_rate = 8000;
191         apar->codec_id    = AV_CODEC_ID_PCM_MULAW;
192         break;
193     case FLV_CODECID_PCM_ALAW:
194         apar->sample_rate = 8000;
195         apar->codec_id    = AV_CODEC_ID_PCM_ALAW;
196         break;
197     default:
198         av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n",
199                flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
200         apar->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
201     }
202 }
203
204 static int flv_same_video_codec(AVCodecParameters *vpar, int flags)
205 {
206     int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
207
208     if (!vpar->codec_id && !vpar->codec_tag)
209         return 1;
210
211     switch (flv_codecid) {
212     case FLV_CODECID_H263:
213         return vpar->codec_id == AV_CODEC_ID_FLV1;
214     case FLV_CODECID_SCREEN:
215         return vpar->codec_id == AV_CODEC_ID_FLASHSV;
216     case FLV_CODECID_SCREEN2:
217         return vpar->codec_id == AV_CODEC_ID_FLASHSV2;
218     case FLV_CODECID_VP6:
219         return vpar->codec_id == AV_CODEC_ID_VP6F;
220     case FLV_CODECID_VP6A:
221         return vpar->codec_id == AV_CODEC_ID_VP6A;
222     case FLV_CODECID_H264:
223         return vpar->codec_id == AV_CODEC_ID_H264;
224     default:
225         return vpar->codec_tag == flv_codecid;
226     }
227 }
228
229 static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream,
230                                int flv_codecid, int read)
231 {
232     AVCodecParameters *par = vstream->codecpar;
233     switch (flv_codecid) {
234     case FLV_CODECID_H263:
235         par->codec_id = AV_CODEC_ID_FLV1;
236         break;
237     case FLV_CODECID_SCREEN:
238         par->codec_id = AV_CODEC_ID_FLASHSV;
239         break;
240     case FLV_CODECID_SCREEN2:
241         par->codec_id = AV_CODEC_ID_FLASHSV2;
242         break;
243     case FLV_CODECID_VP6:
244         par->codec_id = AV_CODEC_ID_VP6F;
245     case FLV_CODECID_VP6A:
246         if (flv_codecid == FLV_CODECID_VP6A)
247             par->codec_id = AV_CODEC_ID_VP6A;
248         if (read) {
249             if (par->extradata_size != 1) {
250                 par->extradata = av_malloc(1);
251                 if (par->extradata)
252                     par->extradata_size = 1;
253             }
254             if (par->extradata)
255                 par->extradata[0] = avio_r8(s->pb);
256             else
257                 avio_skip(s->pb, 1);
258         }
259         return 1;     // 1 byte body size adjustment for flv_read_packet()
260     case FLV_CODECID_H264:
261         par->codec_id = AV_CODEC_ID_H264;
262         return 3;     // not 4, reading packet type will consume one byte
263     default:
264         av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
265         par->codec_tag = flv_codecid;
266     }
267
268     return 0;
269 }
270
271 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
272 {
273     int length = avio_rb16(ioc);
274     if (length >= buffsize) {
275         avio_skip(ioc, length);
276         return -1;
277     }
278
279     avio_read(ioc, buffer, length);
280
281     buffer[length] = '\0';
282
283     return length;
284 }
285
286 static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc,
287                                  AVStream *vstream, int64_t max_pos)
288 {
289     FLVContext *flv       = s->priv_data;
290     unsigned int arraylen = 0, timeslen = 0, fileposlen = 0, i;
291     double num_val;
292     char str_val[256];
293     int64_t *times         = NULL;
294     int64_t *filepositions = NULL;
295     int ret                = AVERROR(ENOSYS);
296     int64_t initial_pos    = avio_tell(ioc);
297
298     if (s->flags & AVFMT_FLAG_IGNIDX)
299         return 0;
300
301     while (avio_tell(ioc) < max_pos - 2 &&
302            amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
303         int64_t *current_array;
304
305         // Expect array object in context
306         if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
307             break;
308
309         arraylen = avio_rb32(ioc);
310         if (arraylen >> 28)
311             break;
312
313         /* Expect only 'times' or 'filepositions' sub-arrays in other
314          * case refuse to use such metadata for indexing. */
315         if (!strcmp(KEYFRAMES_TIMESTAMP_TAG, str_val) && !times) {
316             if (!(times = av_mallocz(sizeof(*times) * arraylen))) {
317                 ret = AVERROR(ENOMEM);
318                 goto finish;
319             }
320             timeslen      = arraylen;
321             current_array = times;
322         } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) &&
323                    !filepositions) {
324             if (!(filepositions = av_mallocz(sizeof(*filepositions) * arraylen))) {
325                 ret = AVERROR(ENOMEM);
326                 goto finish;
327             }
328             fileposlen    = arraylen;
329             current_array = filepositions;
330         } else
331             // unexpected metatag inside keyframes, will not use such
332             // metadata for indexing
333             break;
334
335         for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
336             if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
337                 goto finish;
338             num_val          = av_int2double(avio_rb64(ioc));
339             current_array[i] = num_val;
340         }
341         if (times && filepositions) {
342             // All done, exiting at a position allowing amf_parse_object
343             // to finish parsing the object
344             ret = 0;
345             break;
346         }
347     }
348
349     if (!ret && timeslen == fileposlen) {
350         for (i = 0; i < fileposlen; i++) {
351             av_add_index_entry(vstream, filepositions[i], times[i] * 1000,
352                                0, 0, AVINDEX_KEYFRAME);
353             if (i < 2) {
354                 flv->validate_index[i].pos = filepositions[i];
355                 flv->validate_index[i].dts = times[i] * 1000;
356                 flv->validate_count        = i + 1;
357             }
358         }
359     } else
360         av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
361
362 finish:
363     av_freep(&times);
364     av_freep(&filepositions);
365     // If we got unexpected data, but successfully reset back to
366     // the start pos, the caller can continue parsing
367     if (ret < 0 && avio_seek(ioc, initial_pos, SEEK_SET) > 0)
368         return 0;
369     return ret;
370 }
371
372 static int amf_parse_object(AVFormatContext *s, AVStream *astream,
373                             AVStream *vstream, const char *key,
374                             int64_t max_pos, int depth)
375 {
376     AVCodecParameters *apar, *vpar;
377     FLVContext *flv = s->priv_data;
378     AVIOContext *ioc;
379     AMFDataType amf_type;
380     char str_val[256];
381     double num_val;
382
383     num_val  = 0;
384     ioc      = s->pb;
385     amf_type = avio_r8(ioc);
386
387     switch (amf_type) {
388     case AMF_DATA_TYPE_NUMBER:
389         num_val = av_int2double(avio_rb64(ioc));
390         break;
391     case AMF_DATA_TYPE_BOOL:
392         num_val = avio_r8(ioc);
393         break;
394     case AMF_DATA_TYPE_STRING:
395         if (amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
396             return -1;
397         break;
398     case AMF_DATA_TYPE_OBJECT:
399         if ((vstream || astream) && key &&
400             !strcmp(KEYFRAMES_TAG, key) && depth == 1)
401             if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
402                                       max_pos) < 0)
403                 return -1;
404
405         while (avio_tell(ioc) < max_pos - 2 &&
406                amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
407             if (amf_parse_object(s, astream, vstream, str_val, max_pos,
408                                  depth + 1) < 0)
409                 return -1;     // if we couldn't skip, bomb out.
410         if (avio_r8(ioc) != AMF_END_OF_OBJECT)
411             return -1;
412         break;
413     case AMF_DATA_TYPE_NULL:
414     case AMF_DATA_TYPE_UNDEFINED:
415     case AMF_DATA_TYPE_UNSUPPORTED:
416         break;     // these take up no additional space
417     case AMF_DATA_TYPE_MIXEDARRAY:
418         avio_skip(ioc, 4);     // skip 32-bit max array index
419         while (avio_tell(ioc) < max_pos - 2 &&
420                amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
421             // this is the only case in which we would want a nested
422             // parse to not skip over the object
423             if (amf_parse_object(s, astream, vstream, str_val, max_pos,
424                                  depth + 1) < 0)
425                 return -1;
426         if (avio_r8(ioc) != AMF_END_OF_OBJECT)
427             return -1;
428         break;
429     case AMF_DATA_TYPE_ARRAY:
430     {
431         unsigned int arraylen, i;
432
433         arraylen = avio_rb32(ioc);
434         for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++)
435             if (amf_parse_object(s, NULL, NULL, NULL, max_pos,
436                                  depth + 1) < 0)
437                 return -1;      // if we couldn't skip, bomb out.
438     }
439     break;
440     case AMF_DATA_TYPE_DATE:
441         avio_skip(ioc, 8 + 2);  // timestamp (double) and UTC offset (int16)
442         break;
443     default:                    // unsupported type, we couldn't skip
444         return -1;
445     }
446
447     if (key) {
448         // stream info doesn't live any deeper than the first object
449         if (depth == 1) {
450             apar = astream ? astream->codecpar : NULL;
451             vpar = vstream ? vstream->codecpar : NULL;
452
453             if (amf_type == AMF_DATA_TYPE_NUMBER ||
454                 amf_type == AMF_DATA_TYPE_BOOL) {
455                 if (!strcmp(key, "duration"))
456                     s->duration = num_val * AV_TIME_BASE;
457                 else if (!strcmp(key, "videodatarate") && vpar &&
458                          0 <= (int)(num_val * 1024.0))
459                     vpar->bit_rate = num_val * 1024.0;
460                 else if (!strcmp(key, "audiodatarate") && apar &&
461                          0 <= (int)(num_val * 1024.0))
462                     apar->bit_rate = num_val * 1024.0;
463                 else if (!strcmp(key, "datastream")) {
464                     AVStream *st = create_stream(s, AVMEDIA_TYPE_DATA);
465                     if (!st)
466                         return AVERROR(ENOMEM);
467                     st->codecpar->codec_id = AV_CODEC_ID_TEXT;
468                 } else if (!strcmp(key, "framerate")) {
469                     flv->framerate = av_d2q(num_val, 1000);
470                     if (vstream)
471                         vstream->avg_frame_rate = flv->framerate;
472                 } else if (flv->trust_metadata) {
473                     if (!strcmp(key, "videocodecid") && vpar) {
474                         flv_set_video_codec(s, vstream, num_val, 0);
475                     } else if (!strcmp(key, "audiocodecid") && apar) {
476                         int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
477                         flv_set_audio_codec(s, astream, apar, id);
478                     } else if (!strcmp(key, "audiosamplerate") && apar) {
479                         apar->sample_rate = num_val;
480                     } else if (!strcmp(key, "audiosamplesize") && apar) {
481                         apar->bits_per_coded_sample = num_val;
482                     } else if (!strcmp(key, "stereo") && apar) {
483                         apar->channels       = num_val + 1;
484                         apar->channel_layout = apar->channels == 2 ?
485                                                AV_CH_LAYOUT_STEREO :
486                                                AV_CH_LAYOUT_MONO;
487                     } else if (!strcmp(key, "width") && vpar) {
488                         vpar->width = num_val;
489                     } else if (!strcmp(key, "height") && vpar) {
490                         vpar->height = num_val;
491                     }
492                 }
493             }
494         }
495
496         if (!strcmp(key, "duration")        ||
497             !strcmp(key, "filesize")        ||
498             !strcmp(key, "width")           ||
499             !strcmp(key, "height")          ||
500             !strcmp(key, "videodatarate")   ||
501             !strcmp(key, "framerate")       ||
502             !strcmp(key, "videocodecid")    ||
503             !strcmp(key, "audiodatarate")   ||
504             !strcmp(key, "audiosamplerate") ||
505             !strcmp(key, "audiosamplesize") ||
506             !strcmp(key, "stereo")          ||
507             !strcmp(key, "audiocodecid")    ||
508             !strcmp(key, "datastream"))
509             return 0;
510
511         s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
512         if (amf_type == AMF_DATA_TYPE_BOOL) {
513             av_strlcpy(str_val, num_val > 0 ? "true" : "false",
514                        sizeof(str_val));
515             av_dict_set(&s->metadata, key, str_val, 0);
516         } else if (amf_type == AMF_DATA_TYPE_NUMBER) {
517             snprintf(str_val, sizeof(str_val), "%.f", num_val);
518             av_dict_set(&s->metadata, key, str_val, 0);
519         } else if (amf_type == AMF_DATA_TYPE_STRING)
520             av_dict_set(&s->metadata, key, str_val, 0);
521     }
522
523     return 0;
524 }
525
526 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
527 {
528     AMFDataType type;
529     AVStream *stream, *astream, *vstream;
530     AVIOContext *ioc;
531     int i;
532     // only needs to hold the string "onMetaData".
533     // Anything longer is something we don't want.
534     char buffer[11];
535
536     astream = NULL;
537     vstream = NULL;
538     ioc     = s->pb;
539
540     // first object needs to be "onMetaData" string
541     type = avio_r8(ioc);
542     if (type != AMF_DATA_TYPE_STRING ||
543         amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
544         return -1;
545
546     if (!strcmp(buffer, "onTextData"))
547         return 1;
548
549     if (strcmp(buffer, "onMetaData") && strcmp(buffer, "onCuePoint"))
550         return -1;
551
552     // find the streams now so that amf_parse_object doesn't need to do
553     // the lookup every time it is called.
554     for (i = 0; i < s->nb_streams; i++) {
555         stream = s->streams[i];
556         if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
557             astream = stream;
558         else if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
559             vstream = stream;
560     }
561
562     // parse the second object (we want a mixed array)
563     if (amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
564         return -1;
565
566     return 0;
567 }
568
569 static int flv_read_header(AVFormatContext *s)
570 {
571     int offset;
572
573     avio_skip(s->pb, 4);
574     avio_r8(s->pb); // flags
575
576     s->ctx_flags |= AVFMTCTX_NOHEADER;
577
578     offset = avio_rb32(s->pb);
579     avio_seek(s->pb, offset, SEEK_SET);
580     avio_skip(s->pb, 4);
581
582     s->start_time = 0;
583
584     return 0;
585 }
586
587 static int flv_read_close(AVFormatContext *s)
588 {
589     FLVContext *flv = s->priv_data;
590     av_freep(&flv->new_extradata[0]);
591     av_freep(&flv->new_extradata[1]);
592     return 0;
593 }
594
595 static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
596 {
597     av_free(st->codecpar->extradata);
598     st->codecpar->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
599     if (!st->codecpar->extradata)
600         return AVERROR(ENOMEM);
601     st->codecpar->extradata_size = size;
602     avio_read(s->pb, st->codecpar->extradata, st->codecpar->extradata_size);
603     return 0;
604 }
605
606 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
607                                int size)
608 {
609     av_free(flv->new_extradata[stream]);
610     flv->new_extradata[stream] = av_mallocz(size +
611                                             AV_INPUT_BUFFER_PADDING_SIZE);
612     if (!flv->new_extradata[stream])
613         return AVERROR(ENOMEM);
614     flv->new_extradata_size[stream] = size;
615     avio_read(pb, flv->new_extradata[stream], size);
616     return 0;
617 }
618
619 static void clear_index_entries(AVFormatContext *s, int64_t pos)
620 {
621     int i, j, out;
622     av_log(s, AV_LOG_WARNING,
623            "Found invalid index entries, clearing the index.\n");
624     for (i = 0; i < s->nb_streams; i++) {
625         AVStream *st = s->streams[i];
626         /* Remove all index entries that point to >= pos */
627         out = 0;
628         for (j = 0; j < st->nb_index_entries; j++)
629             if (st->index_entries[j].pos < pos)
630                 st->index_entries[out++] = st->index_entries[j];
631         st->nb_index_entries = out;
632     }
633 }
634
635 static int amf_skip_tag(AVIOContext *pb, AMFDataType type)
636 {
637     int nb = -1, ret, parse_name = 1;
638
639     switch (type) {
640     case AMF_DATA_TYPE_NUMBER:
641         avio_skip(pb, 8);
642         break;
643     case AMF_DATA_TYPE_BOOL:
644         avio_skip(pb, 1);
645         break;
646     case AMF_DATA_TYPE_STRING:
647         avio_skip(pb, avio_rb16(pb));
648         break;
649     case AMF_DATA_TYPE_ARRAY:
650         parse_name = 0;
651     case AMF_DATA_TYPE_MIXEDARRAY:
652         nb = avio_rb32(pb);
653     case AMF_DATA_TYPE_OBJECT:
654         while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) {
655             if (parse_name) {
656                 int size = avio_rb16(pb);
657                 if (!size) {
658                     avio_skip(pb, 1);
659                     break;
660                 }
661                 avio_skip(pb, size);
662             }
663             if ((ret = amf_skip_tag(pb, avio_r8(pb))) < 0)
664                 return ret;
665         }
666         break;
667     case AMF_DATA_TYPE_NULL:
668     case AMF_DATA_TYPE_OBJECT_END:
669         break;
670     default:
671         return AVERROR_INVALIDDATA;
672     }
673     return 0;
674 }
675
676 static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
677                            int64_t dts, int64_t next)
678 {
679     AVIOContext *pb = s->pb;
680     AVStream *st    = NULL;
681     char buf[20];
682     int ret = AVERROR_INVALIDDATA;
683     int i, length = -1;
684
685     switch (avio_r8(pb)) {
686     case AMF_DATA_TYPE_MIXEDARRAY:
687         avio_seek(pb, 4, SEEK_CUR);
688     case AMF_DATA_TYPE_OBJECT:
689         break;
690     default:
691         goto skip;
692     }
693
694     while ((ret = amf_get_string(pb, buf, sizeof(buf))) > 0) {
695         AMFDataType type = avio_r8(pb);
696         if (type == AMF_DATA_TYPE_STRING && !strcmp(buf, "text")) {
697             length = avio_rb16(pb);
698             ret    = av_get_packet(pb, pkt, length);
699             if (ret < 0)
700                 goto skip;
701             else
702                 break;
703         } else {
704             if ((ret = amf_skip_tag(pb, type)) < 0)
705                 goto skip;
706         }
707     }
708
709     if (length < 0) {
710         ret = AVERROR_INVALIDDATA;
711         goto skip;
712     }
713
714     for (i = 0; i < s->nb_streams; i++) {
715         st = s->streams[i];
716         if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
717             break;
718     }
719
720     if (i == s->nb_streams) {
721         st = create_stream(s, AVMEDIA_TYPE_DATA);
722         if (!st)
723             return AVERROR(ENOMEM);
724         st->codecpar->codec_id = AV_CODEC_ID_TEXT;
725     }
726
727     pkt->dts  = dts;
728     pkt->pts  = dts;
729     pkt->size = ret;
730
731     pkt->stream_index = st->index;
732     pkt->flags       |= AV_PKT_FLAG_KEY;
733
734 skip:
735     avio_seek(s->pb, next + 4, SEEK_SET);
736
737     return ret;
738 }
739
740 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
741 {
742     FLVContext *flv = s->priv_data;
743     int ret, i, size, flags, is_audio;
744     enum FlvTagType type;
745     int64_t next, pos;
746     int64_t dts, pts = AV_NOPTS_VALUE;
747     int sample_rate = 0, channels = 0;
748     AVStream *st    = NULL;
749
750     /* pkt size is repeated at end. skip it */
751     for (;; avio_skip(s->pb, 4)) {
752         pos  = avio_tell(s->pb);
753         type = avio_r8(s->pb);
754         size = avio_rb24(s->pb);
755         dts  = avio_rb24(s->pb);
756         dts |= avio_r8(s->pb) << 24;
757         av_log(s, AV_LOG_TRACE, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
758         if (s->pb->eof_reached)
759             return AVERROR_EOF;
760         avio_skip(s->pb, 3); /* stream id, always 0 */
761         flags = 0;
762
763         if (flv->validate_next < flv->validate_count) {
764             int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
765             if (pos == validate_pos) {
766                 if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
767                     VALIDATE_INDEX_TS_THRESH) {
768                     flv->validate_next++;
769                 } else {
770                     clear_index_entries(s, validate_pos);
771                     flv->validate_count = 0;
772                 }
773             } else if (pos > validate_pos) {
774                 clear_index_entries(s, validate_pos);
775                 flv->validate_count = 0;
776             }
777         }
778
779         if (size == 0)
780             continue;
781
782         next = size + avio_tell(s->pb);
783
784         if (type == FLV_TAG_TYPE_AUDIO) {
785             is_audio = 1;
786             flags    = avio_r8(s->pb);
787             size--;
788         } else if (type == FLV_TAG_TYPE_VIDEO) {
789             is_audio = 0;
790             flags    = avio_r8(s->pb);
791             size--;
792             if ((flags & 0xf0) == 0x50) /* video info / command frame */
793                 goto skip;
794         } else {
795             if (type == FLV_TAG_TYPE_META && size > 13 + 1 + 4)
796                 if (flv_read_metabody(s, next) > 0) {
797                     return flv_data_packet(s, pkt, dts, next);
798                 } else /* skip packet */
799                     av_log(s, AV_LOG_DEBUG,
800                            "Skipping flv packet: type %d, size %d, flags %d.\n",
801                            type, size, flags);
802
803 skip:
804             avio_seek(s->pb, next, SEEK_SET);
805             continue;
806         }
807
808         /* skip empty data packets */
809         if (!size)
810             continue;
811
812         /* now find stream */
813         for (i = 0; i < s->nb_streams; i++) {
814             st = s->streams[i];
815             if (is_audio && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
816                 if (flv_same_audio_codec(st->codecpar, flags))
817                     break;
818             } else if (!is_audio &&
819                        st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
820                 if (flv_same_video_codec(st->codecpar, flags))
821                     break;
822             }
823         }
824         if (i == s->nb_streams) {
825             st = create_stream(s, is_audio ? AVMEDIA_TYPE_AUDIO
826                                            : AVMEDIA_TYPE_VIDEO);
827             if (!st)
828                 return AVERROR(ENOMEM);
829         }
830         av_log(s, AV_LOG_TRACE, "%d %X %d \n", is_audio, flags, st->discard);
831
832         if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
833             is_audio)
834             av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
835
836         if ((st->discard >= AVDISCARD_NONKEY &&
837              !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio)) ||
838             (st->discard >= AVDISCARD_BIDIR &&
839              ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio)) ||
840             st->discard >= AVDISCARD_ALL) {
841             avio_seek(s->pb, next, SEEK_SET);
842             continue;
843         }
844         break;
845     }
846
847     // if not streamed and no duration from metadata then seek to end to find
848     // the duration from the timestamps
849     if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
850         (!s->duration || s->duration == AV_NOPTS_VALUE) &&
851         !flv->searched_for_end) {
852         int size;
853         const int64_t pos   = avio_tell(s->pb);
854         // Read the last 4 bytes of the file, this should be the size of the
855         // previous FLV tag. Use the timestamp of its payload as duration.
856         const int64_t fsize = avio_size(s->pb);
857         avio_seek(s->pb, fsize - 4, SEEK_SET);
858         size = avio_rb32(s->pb);
859         if (size > 0 && size < fsize) {
860             // Seek to the start of the last FLV tag at position (fsize - 4 - size)
861             // but skip the byte indicating the type.
862             avio_seek(s->pb, fsize - 3 - size, SEEK_SET);
863             if (size == avio_rb24(s->pb) + 11) {
864                 uint32_t ts = avio_rb24(s->pb);
865                 ts         |= avio_r8(s->pb) << 24;
866                 s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
867             }
868         }
869         avio_seek(s->pb, pos, SEEK_SET);
870         flv->searched_for_end = 1;
871     }
872
873     if (is_audio) {
874         int bits_per_coded_sample;
875         channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
876         sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
877                                 FLV_AUDIO_SAMPLERATE_OFFSET) >> 3;
878         bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
879         if (!st->codecpar->channels || !st->codecpar->sample_rate ||
880             !st->codecpar->bits_per_coded_sample) {
881             st->codecpar->channels              = channels;
882             st->codecpar->channel_layout        = channels == 1
883                                                ? AV_CH_LAYOUT_MONO
884                                                : AV_CH_LAYOUT_STEREO;
885             st->codecpar->sample_rate           = sample_rate;
886             st->codecpar->bits_per_coded_sample = bits_per_coded_sample;
887         }
888         if (!st->codecpar->codec_id) {
889             flv_set_audio_codec(s, st, st->codecpar,
890                                 flags & FLV_AUDIO_CODECID_MASK);
891             flv->last_sample_rate =
892             sample_rate           = st->codecpar->sample_rate;
893             flv->last_channels    =
894             channels              = st->codecpar->channels;
895         } else {
896             AVCodecParameters *par = avcodec_parameters_alloc();
897             if (!par) {
898                 ret = AVERROR(ENOMEM);
899                 goto leave;
900             }
901             par->sample_rate = sample_rate;
902             par->bits_per_coded_sample = bits_per_coded_sample;
903             flv_set_audio_codec(s, st, par, flags & FLV_AUDIO_CODECID_MASK);
904             sample_rate = par->sample_rate;
905             avcodec_parameters_free(&par);
906         }
907     } else {
908         size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
909     }
910
911     if (st->codecpar->codec_id == AV_CODEC_ID_AAC ||
912         st->codecpar->codec_id == AV_CODEC_ID_H264) {
913         int type = avio_r8(s->pb);
914         size--;
915
916         if (size < 0) {
917             ret = AVERROR_INVALIDDATA;
918             goto leave;
919         }
920
921         if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
922             // sign extension
923             int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
924             pts = dts + cts;
925             if (cts < 0 && !flv->wrong_dts) { // dts might be wrong
926                 flv->wrong_dts = 1;
927                 av_log(s, AV_LOG_WARNING,
928                        "Negative cts, previous timestamps might be wrong.\n");
929             }
930         }
931         if (type == 0) {
932             if (st->codecpar->extradata) {
933                 if ((ret = flv_queue_extradata(flv, s->pb, is_audio, size)) < 0)
934                     return ret;
935                 ret = AVERROR(EAGAIN);
936                 goto leave;
937             }
938             if ((ret = flv_get_extradata(s, st, size)) < 0)
939                 return ret;
940             if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
941                 MPEG4AudioConfig cfg;
942
943                 /* Workaround for buggy Omnia A/XE encoder */
944                 AVDictionaryEntry *t = av_dict_get(s->metadata, "Encoder", NULL, 0);
945                 if (t && !strcmp(t->value, "Omnia A/XE"))
946                     st->codecpar->extradata_size = 2;
947
948                 avpriv_mpeg4audio_get_config(&cfg, st->codecpar->extradata,
949                                              st->codecpar->extradata_size * 8, 1);
950                 st->codecpar->channels       = cfg.channels;
951                 st->codecpar->channel_layout = 0;
952                 if (cfg.ext_sample_rate)
953                     st->codecpar->sample_rate = cfg.ext_sample_rate;
954                 else
955                     st->codecpar->sample_rate = cfg.sample_rate;
956                 av_log(s, AV_LOG_TRACE, "mp4a config channels %d sample rate %d\n",
957                        st->codecpar->channels, st->codecpar->sample_rate);
958             }
959
960             ret = AVERROR(EAGAIN);
961             goto leave;
962         }
963     }
964
965     /* skip empty data packets */
966     if (!size) {
967         ret = AVERROR(EAGAIN);
968         goto leave;
969     }
970
971     ret = av_get_packet(s->pb, pkt, size);
972     if (ret < 0)
973         return AVERROR(EIO);
974     /* note: we need to modify the packet size here to handle the last
975      * packet */
976     pkt->size         = ret;
977     pkt->dts          = dts;
978     pkt->pts          = pts == AV_NOPTS_VALUE ? dts : pts;
979     pkt->stream_index = st->index;
980     if (flv->new_extradata[is_audio]) {
981         uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
982                                                 flv->new_extradata_size[is_audio]);
983         if (side) {
984             memcpy(side, flv->new_extradata[is_audio],
985                    flv->new_extradata_size[is_audio]);
986             av_freep(&flv->new_extradata[is_audio]);
987             flv->new_extradata_size[is_audio] = 0;
988         }
989     }
990     if (is_audio && (sample_rate != flv->last_sample_rate ||
991                      channels    != flv->last_channels)) {
992         flv->last_sample_rate = sample_rate;
993         flv->last_channels    = channels;
994         ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
995     }
996
997     if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
998         pkt->flags |= AV_PKT_FLAG_KEY;
999
1000 leave:
1001     avio_skip(s->pb, 4);
1002     return ret;
1003 }
1004
1005 static int flv_read_seek(AVFormatContext *s, int stream_index,
1006                          int64_t ts, int flags)
1007 {
1008     FLVContext *flv = s->priv_data;
1009     flv->validate_count = 0;
1010     return avio_seek_time(s->pb, stream_index, ts, flags);
1011 }
1012
1013 #define OFFSET(x) offsetof(FLVContext, x)
1014 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1015 static const AVOption options[] = {
1016     { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
1017     { NULL }
1018 };
1019
1020 static const AVClass class = {
1021     .class_name = "flvdec",
1022     .item_name  = av_default_item_name,
1023     .option     = options,
1024     .version    = LIBAVUTIL_VERSION_INT,
1025 };
1026
1027 AVInputFormat ff_flv_demuxer = {
1028     .name           = "flv",
1029     .long_name      = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
1030     .priv_data_size = sizeof(FLVContext),
1031     .read_probe     = flv_probe,
1032     .read_header    = flv_read_header,
1033     .read_packet    = flv_read_packet,
1034     .read_seek      = flv_read_seek,
1035     .read_close     = flv_read_close,
1036     .extensions     = "flv",
1037     .priv_class     = &class,
1038 };