]> git.sesse.net Git - ffmpeg/blob - libavformat/flvdec.c
Merge commit '10aa6f9db44e96977e7155fdc3e62619e0e00a15'
[ffmpeg] / libavformat / flvdec.c
1 /*
2  * FLV demuxer
3  * Copyright (c) 2003 The FFmpeg Project
4  *
5  * This demuxer will generate a 1 byte extradata for VP6F content.
6  * It is composed of:
7  *  - upper 4bits: difference between encoded width and visible width
8  *  - lower 4bits: difference between encoded height and visible height
9  *
10  * This file is part of FFmpeg.
11  *
12  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 VALIDATE_INDEX_TS_THRESH 2500
41
42 #define RESYNC_BUFFER_SIZE (1<<20)
43
44 typedef struct FLVContext {
45     const AVClass *class; ///< Class for private options.
46     int trust_metadata;   ///< configure streams according onMetaData
47     int wrong_dts;        ///< wrong dts due to negative cts
48     uint8_t *new_extradata[FLV_STREAM_TYPE_NB];
49     int new_extradata_size[FLV_STREAM_TYPE_NB];
50     int last_sample_rate;
51     int last_channels;
52     struct {
53         int64_t dts;
54         int64_t pos;
55     } validate_index[2];
56     int validate_next;
57     int validate_count;
58     int searched_for_end;
59
60     uint8_t resync_buffer[2*RESYNC_BUFFER_SIZE];
61
62     int broken_sizes;
63 } FLVContext;
64
65 static int probe(AVProbeData *p, int live)
66 {
67     const uint8_t *d = p->buf;
68     unsigned offset = AV_RB32(d + 5);
69
70     if (d[0] == 'F' &&
71         d[1] == 'L' &&
72         d[2] == 'V' &&
73         d[3] < 5 && d[5] == 0 &&
74         offset + 100 < p->buf_size &&
75         offset > 8) {
76         int is_live = !memcmp(d + offset + 40, "NGINX RTMP", 10);
77
78         if (live == is_live)
79             return AVPROBE_SCORE_MAX;
80     }
81     return 0;
82 }
83
84 static int flv_probe(AVProbeData *p)
85 {
86     return probe(p, 0);
87 }
88
89 static int live_flv_probe(AVProbeData *p)
90 {
91     return probe(p, 1);
92 }
93
94 static AVStream *create_stream(AVFormatContext *s, int codec_type)
95 {
96     AVStream *st = avformat_new_stream(s, NULL);
97     if (!st)
98         return NULL;
99     st->codec->codec_type = codec_type;
100     if (s->nb_streams>=3 ||(   s->nb_streams==2
101                            && s->streams[0]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE
102                            && s->streams[1]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE))
103         s->ctx_flags &= ~AVFMTCTX_NOHEADER;
104
105     avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
106     return st;
107 }
108
109 static int flv_same_audio_codec(AVCodecContext *acodec, int flags)
110 {
111     int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
112     int flv_codecid           = flags & FLV_AUDIO_CODECID_MASK;
113     int codec_id;
114
115     if (!acodec->codec_id && !acodec->codec_tag)
116         return 1;
117
118     if (acodec->bits_per_coded_sample != bits_per_coded_sample)
119         return 0;
120
121     switch (flv_codecid) {
122     // no distinction between S16 and S8 PCM codec flags
123     case FLV_CODECID_PCM:
124         codec_id = bits_per_coded_sample == 8
125                    ? AV_CODEC_ID_PCM_U8
126 #if HAVE_BIGENDIAN
127                    : AV_CODEC_ID_PCM_S16BE;
128 #else
129                    : AV_CODEC_ID_PCM_S16LE;
130 #endif
131         return codec_id == acodec->codec_id;
132     case FLV_CODECID_PCM_LE:
133         codec_id = bits_per_coded_sample == 8
134                    ? AV_CODEC_ID_PCM_U8
135                    : AV_CODEC_ID_PCM_S16LE;
136         return codec_id == acodec->codec_id;
137     case FLV_CODECID_AAC:
138         return acodec->codec_id == AV_CODEC_ID_AAC;
139     case FLV_CODECID_ADPCM:
140         return acodec->codec_id == AV_CODEC_ID_ADPCM_SWF;
141     case FLV_CODECID_SPEEX:
142         return acodec->codec_id == AV_CODEC_ID_SPEEX;
143     case FLV_CODECID_MP3:
144         return acodec->codec_id == AV_CODEC_ID_MP3;
145     case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
146     case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
147     case FLV_CODECID_NELLYMOSER:
148         return acodec->codec_id == AV_CODEC_ID_NELLYMOSER;
149     case FLV_CODECID_PCM_MULAW:
150         return acodec->sample_rate == 8000 &&
151                acodec->codec_id    == AV_CODEC_ID_PCM_MULAW;
152     case FLV_CODECID_PCM_ALAW:
153         return acodec->sample_rate == 8000 &&
154                acodec->codec_id    == AV_CODEC_ID_PCM_ALAW;
155     default:
156         return acodec->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
157     }
158 }
159
160 static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream,
161                                 AVCodecContext *acodec, int flv_codecid)
162 {
163     switch (flv_codecid) {
164     // no distinction between S16 and S8 PCM codec flags
165     case FLV_CODECID_PCM:
166         acodec->codec_id = acodec->bits_per_coded_sample == 8
167                            ? AV_CODEC_ID_PCM_U8
168 #if HAVE_BIGENDIAN
169                            : AV_CODEC_ID_PCM_S16BE;
170 #else
171                            : AV_CODEC_ID_PCM_S16LE;
172 #endif
173         break;
174     case FLV_CODECID_PCM_LE:
175         acodec->codec_id = acodec->bits_per_coded_sample == 8
176                            ? AV_CODEC_ID_PCM_U8
177                            : AV_CODEC_ID_PCM_S16LE;
178         break;
179     case FLV_CODECID_AAC:
180         acodec->codec_id = AV_CODEC_ID_AAC;
181         break;
182     case FLV_CODECID_ADPCM:
183         acodec->codec_id = AV_CODEC_ID_ADPCM_SWF;
184         break;
185     case FLV_CODECID_SPEEX:
186         acodec->codec_id    = AV_CODEC_ID_SPEEX;
187         acodec->sample_rate = 16000;
188         break;
189     case FLV_CODECID_MP3:
190         acodec->codec_id      = AV_CODEC_ID_MP3;
191         astream->need_parsing = AVSTREAM_PARSE_FULL;
192         break;
193     case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
194         // in case metadata does not otherwise declare samplerate
195         acodec->sample_rate = 8000;
196         acodec->codec_id    = AV_CODEC_ID_NELLYMOSER;
197         break;
198     case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
199         acodec->sample_rate = 16000;
200         acodec->codec_id    = AV_CODEC_ID_NELLYMOSER;
201         break;
202     case FLV_CODECID_NELLYMOSER:
203         acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
204         break;
205     case FLV_CODECID_PCM_MULAW:
206         acodec->sample_rate = 8000;
207         acodec->codec_id    = AV_CODEC_ID_PCM_MULAW;
208         break;
209     case FLV_CODECID_PCM_ALAW:
210         acodec->sample_rate = 8000;
211         acodec->codec_id    = AV_CODEC_ID_PCM_ALAW;
212         break;
213     default:
214         avpriv_request_sample(s, "Audio codec (%x)",
215                flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
216         acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
217     }
218 }
219
220 static int flv_same_video_codec(AVCodecContext *vcodec, int flags)
221 {
222     int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
223
224     if (!vcodec->codec_id && !vcodec->codec_tag)
225         return 1;
226
227     switch (flv_codecid) {
228     case FLV_CODECID_H263:
229         return vcodec->codec_id == AV_CODEC_ID_FLV1;
230     case FLV_CODECID_SCREEN:
231         return vcodec->codec_id == AV_CODEC_ID_FLASHSV;
232     case FLV_CODECID_SCREEN2:
233         return vcodec->codec_id == AV_CODEC_ID_FLASHSV2;
234     case FLV_CODECID_VP6:
235         return vcodec->codec_id == AV_CODEC_ID_VP6F;
236     case FLV_CODECID_VP6A:
237         return vcodec->codec_id == AV_CODEC_ID_VP6A;
238     case FLV_CODECID_H264:
239         return vcodec->codec_id == AV_CODEC_ID_H264;
240     default:
241         return vcodec->codec_tag == flv_codecid;
242     }
243 }
244
245 static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream,
246                                int flv_codecid, int read)
247 {
248     AVCodecContext *vcodec = vstream->codec;
249     switch (flv_codecid) {
250     case FLV_CODECID_H263:
251         vcodec->codec_id = AV_CODEC_ID_FLV1;
252         break;
253     case FLV_CODECID_REALH263:
254         vcodec->codec_id = AV_CODEC_ID_H263;
255         break; // Really mean it this time
256     case FLV_CODECID_SCREEN:
257         vcodec->codec_id = AV_CODEC_ID_FLASHSV;
258         break;
259     case FLV_CODECID_SCREEN2:
260         vcodec->codec_id = AV_CODEC_ID_FLASHSV2;
261         break;
262     case FLV_CODECID_VP6:
263         vcodec->codec_id = AV_CODEC_ID_VP6F;
264     case FLV_CODECID_VP6A:
265         if (flv_codecid == FLV_CODECID_VP6A)
266             vcodec->codec_id = AV_CODEC_ID_VP6A;
267         if (read) {
268             if (vcodec->extradata_size != 1) {
269                 ff_alloc_extradata(vcodec, 1);
270             }
271             if (vcodec->extradata)
272                 vcodec->extradata[0] = avio_r8(s->pb);
273             else
274                 avio_skip(s->pb, 1);
275         }
276         return 1;     // 1 byte body size adjustment for flv_read_packet()
277     case FLV_CODECID_H264:
278         vcodec->codec_id = AV_CODEC_ID_H264;
279         vstream->need_parsing = AVSTREAM_PARSE_HEADERS;
280         return 3;     // not 4, reading packet type will consume one byte
281     case FLV_CODECID_MPEG4:
282         vcodec->codec_id = AV_CODEC_ID_MPEG4;
283         return 3;
284     default:
285         avpriv_request_sample(s, "Video codec (%x)", flv_codecid);
286         vcodec->codec_tag = flv_codecid;
287     }
288
289     return 0;
290 }
291
292 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
293 {
294     int length = avio_rb16(ioc);
295     if (length >= buffsize) {
296         avio_skip(ioc, length);
297         return -1;
298     }
299
300     avio_read(ioc, buffer, length);
301
302     buffer[length] = '\0';
303
304     return length;
305 }
306
307 static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc,
308                                  AVStream *vstream, int64_t max_pos)
309 {
310     FLVContext *flv       = s->priv_data;
311     unsigned int timeslen = 0, fileposlen = 0, i;
312     char str_val[256];
313     int64_t *times         = NULL;
314     int64_t *filepositions = NULL;
315     int ret                = AVERROR(ENOSYS);
316     int64_t initial_pos    = avio_tell(ioc);
317
318     if (vstream->nb_index_entries>0) {
319         av_log(s, AV_LOG_WARNING, "Skipping duplicate index\n");
320         return 0;
321     }
322
323     if (s->flags & AVFMT_FLAG_IGNIDX)
324         return 0;
325
326     while (avio_tell(ioc) < max_pos - 2 &&
327            amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
328         int64_t **current_array;
329         unsigned int arraylen;
330
331         // Expect array object in context
332         if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
333             break;
334
335         arraylen = avio_rb32(ioc);
336         if (arraylen>>28)
337             break;
338
339         if       (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times) {
340             current_array = &times;
341             timeslen      = arraylen;
342         } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) &&
343                    !filepositions) {
344             current_array = &filepositions;
345             fileposlen    = arraylen;
346         } else
347             // unexpected metatag inside keyframes, will not use such
348             // metadata for indexing
349             break;
350
351         if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
352             ret = AVERROR(ENOMEM);
353             goto finish;
354         }
355
356         for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
357             if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
358                 goto invalid;
359             current_array[0][i] = av_int2double(avio_rb64(ioc));
360         }
361         if (times && filepositions) {
362             // All done, exiting at a position allowing amf_parse_object
363             // to finish parsing the object
364             ret = 0;
365             break;
366         }
367     }
368
369     if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
370         for (i = 0; i < fileposlen; i++) {
371             av_add_index_entry(vstream, filepositions[i], times[i] * 1000,
372                                0, 0, AVINDEX_KEYFRAME);
373             if (i < 2) {
374                 flv->validate_index[i].pos = filepositions[i];
375                 flv->validate_index[i].dts = times[i] * 1000;
376                 flv->validate_count        = i + 1;
377             }
378         }
379     } else {
380 invalid:
381         av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
382     }
383
384 finish:
385     av_freep(&times);
386     av_freep(&filepositions);
387     avio_seek(ioc, initial_pos, SEEK_SET);
388     return ret;
389 }
390
391 static int amf_parse_object(AVFormatContext *s, AVStream *astream,
392                             AVStream *vstream, const char *key,
393                             int64_t max_pos, int depth)
394 {
395     AVCodecContext *acodec, *vcodec;
396     FLVContext *flv = s->priv_data;
397     AVIOContext *ioc;
398     AMFDataType amf_type;
399     char str_val[1024];
400     double num_val;
401
402     num_val  = 0;
403     ioc      = s->pb;
404     amf_type = avio_r8(ioc);
405
406     switch (amf_type) {
407     case AMF_DATA_TYPE_NUMBER:
408         num_val = av_int2double(avio_rb64(ioc));
409         break;
410     case AMF_DATA_TYPE_BOOL:
411         num_val = avio_r8(ioc);
412         break;
413     case AMF_DATA_TYPE_STRING:
414         if (amf_get_string(ioc, str_val, sizeof(str_val)) < 0) {
415             av_log(s, AV_LOG_ERROR, "AMF_DATA_TYPE_STRING parsing failed\n");
416             return -1;
417         }
418         break;
419     case AMF_DATA_TYPE_OBJECT:
420         if ((vstream || astream) && key &&
421             ioc->seekable &&
422             !strcmp(KEYFRAMES_TAG, key) && depth == 1)
423             if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
424                                       max_pos) < 0)
425                 av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
426
427         while (avio_tell(ioc) < max_pos - 2 &&
428                amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
429             if (amf_parse_object(s, astream, vstream, str_val, max_pos,
430                                  depth + 1) < 0)
431                 return -1;     // if we couldn't skip, bomb out.
432         if (avio_r8(ioc) != AMF_END_OF_OBJECT) {
433             av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_OBJECT\n");
434             return -1;
435         }
436         break;
437     case AMF_DATA_TYPE_NULL:
438     case AMF_DATA_TYPE_UNDEFINED:
439     case AMF_DATA_TYPE_UNSUPPORTED:
440         break;     // these take up no additional space
441     case AMF_DATA_TYPE_MIXEDARRAY:
442     {
443         unsigned v;
444         avio_skip(ioc, 4);     // skip 32-bit max array index
445         while (avio_tell(ioc) < max_pos - 2 &&
446                amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
447             // this is the only case in which we would want a nested
448             // parse to not skip over the object
449             if (amf_parse_object(s, astream, vstream, str_val, max_pos,
450                                  depth + 1) < 0)
451                 return -1;
452         v = avio_r8(ioc);
453         if (v != AMF_END_OF_OBJECT) {
454             av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_MIXEDARRAY, found %d\n", v);
455             return -1;
456         }
457         break;
458     }
459     case AMF_DATA_TYPE_ARRAY:
460     {
461         unsigned int arraylen, i;
462
463         arraylen = avio_rb32(ioc);
464         for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++)
465             if (amf_parse_object(s, NULL, NULL, NULL, max_pos,
466                                  depth + 1) < 0)
467                 return -1;      // if we couldn't skip, bomb out.
468     }
469     break;
470     case AMF_DATA_TYPE_DATE:
471         avio_skip(ioc, 8 + 2);  // timestamp (double) and UTC offset (int16)
472         break;
473     default:                    // unsupported type, we couldn't skip
474         av_log(s, AV_LOG_ERROR, "unsupported amf type %d\n", amf_type);
475         return -1;
476     }
477
478     if (key) {
479         acodec = astream ? astream->codec : NULL;
480         vcodec = vstream ? vstream->codec : NULL;
481
482         // stream info doesn't live any deeper than the first object
483         if (depth == 1) {
484             if (amf_type == AMF_DATA_TYPE_NUMBER ||
485                 amf_type == AMF_DATA_TYPE_BOOL) {
486                 if (!strcmp(key, "duration"))
487                     s->duration = num_val * AV_TIME_BASE;
488                 else if (!strcmp(key, "videodatarate") && vcodec &&
489                          0 <= (int)(num_val * 1024.0))
490                     vcodec->bit_rate = num_val * 1024.0;
491                 else if (!strcmp(key, "audiodatarate") && acodec &&
492                          0 <= (int)(num_val * 1024.0))
493                     acodec->bit_rate = num_val * 1024.0;
494                 else if (!strcmp(key, "datastream")) {
495                     AVStream *st = create_stream(s, AVMEDIA_TYPE_SUBTITLE);
496                     if (!st)
497                         return AVERROR(ENOMEM);
498                     st->codec->codec_id = AV_CODEC_ID_TEXT;
499                 } else if (flv->trust_metadata) {
500                     if (!strcmp(key, "videocodecid") && vcodec) {
501                         flv_set_video_codec(s, vstream, num_val, 0);
502                     } else if (!strcmp(key, "audiocodecid") && acodec) {
503                         int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
504                         flv_set_audio_codec(s, astream, acodec, id);
505                     } else if (!strcmp(key, "audiosamplerate") && acodec) {
506                         acodec->sample_rate = num_val;
507                     } else if (!strcmp(key, "audiosamplesize") && acodec) {
508                         acodec->bits_per_coded_sample = num_val;
509                     } else if (!strcmp(key, "stereo") && acodec) {
510                         acodec->channels       = num_val + 1;
511                         acodec->channel_layout = acodec->channels == 2 ?
512                                                  AV_CH_LAYOUT_STEREO :
513                                                  AV_CH_LAYOUT_MONO;
514                     } else if (!strcmp(key, "width") && vcodec) {
515                         vcodec->width = num_val;
516                     } else if (!strcmp(key, "height") && vcodec) {
517                         vcodec->height = num_val;
518                     }
519                 }
520             }
521             if (amf_type == AMF_DATA_TYPE_STRING) {
522                 if (!strcmp(key, "encoder")) {
523                     int version = -1;
524                     if (1 == sscanf(str_val, "Open Broadcaster Software v0.%d", &version)) {
525                         if (version > 0 && version <= 655)
526                             flv->broken_sizes = 1;
527                     }
528                 }
529             }
530         }
531
532         if (amf_type == AMF_DATA_TYPE_OBJECT && s->nb_streams == 1 &&
533            ((!acodec && !strcmp(key, "audiocodecid")) ||
534             (!vcodec && !strcmp(key, "videocodecid"))))
535                 s->ctx_flags &= ~AVFMTCTX_NOHEADER; //If there is either audio/video missing, codecid will be an empty object
536
537         if (!strcmp(key, "duration")        ||
538             !strcmp(key, "filesize")        ||
539             !strcmp(key, "width")           ||
540             !strcmp(key, "height")          ||
541             !strcmp(key, "videodatarate")   ||
542             !strcmp(key, "framerate")       ||
543             !strcmp(key, "videocodecid")    ||
544             !strcmp(key, "audiodatarate")   ||
545             !strcmp(key, "audiosamplerate") ||
546             !strcmp(key, "audiosamplesize") ||
547             !strcmp(key, "stereo")          ||
548             !strcmp(key, "audiocodecid")    ||
549             !strcmp(key, "datastream"))
550             return 0;
551
552         s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
553         if (amf_type == AMF_DATA_TYPE_BOOL) {
554             av_strlcpy(str_val, num_val > 0 ? "true" : "false",
555                        sizeof(str_val));
556             av_dict_set(&s->metadata, key, str_val, 0);
557         } else if (amf_type == AMF_DATA_TYPE_NUMBER) {
558             snprintf(str_val, sizeof(str_val), "%.f", num_val);
559             av_dict_set(&s->metadata, key, str_val, 0);
560         } else if (amf_type == AMF_DATA_TYPE_STRING)
561             av_dict_set(&s->metadata, key, str_val, 0);
562     }
563
564     return 0;
565 }
566
567 #define TYPE_ONTEXTDATA 1
568 #define TYPE_ONCAPTION 2
569 #define TYPE_ONCAPTIONINFO 3
570 #define TYPE_UNKNOWN 9
571
572 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
573 {
574     AMFDataType type;
575     AVStream *stream, *astream, *vstream;
576     AVStream av_unused *dstream;
577     AVIOContext *ioc;
578     int i;
579     // only needs to hold the string "onMetaData".
580     // Anything longer is something we don't want.
581     char buffer[32];
582
583     astream = NULL;
584     vstream = NULL;
585     dstream = NULL;
586     ioc     = s->pb;
587
588     // first object needs to be "onMetaData" string
589     type = avio_r8(ioc);
590     if (type != AMF_DATA_TYPE_STRING ||
591         amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
592         return TYPE_UNKNOWN;
593
594     if (!strcmp(buffer, "onTextData"))
595         return TYPE_ONTEXTDATA;
596
597     if (!strcmp(buffer, "onCaption"))
598         return TYPE_ONCAPTION;
599
600     if (!strcmp(buffer, "onCaptionInfo"))
601         return TYPE_ONCAPTIONINFO;
602
603     if (strcmp(buffer, "onMetaData") && strcmp(buffer, "onCuePoint")) {
604         av_log(s, AV_LOG_DEBUG, "Unknown type %s\n", buffer);
605         return TYPE_UNKNOWN;
606     }
607
608     // find the streams now so that amf_parse_object doesn't need to do
609     // the lookup every time it is called.
610     for (i = 0; i < s->nb_streams; i++) {
611         stream = s->streams[i];
612         if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO)
613             vstream = stream;
614         else if (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO)
615             astream = stream;
616         else if (stream->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
617             dstream = stream;
618     }
619
620     // parse the second object (we want a mixed array)
621     if (amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
622         return -1;
623
624     return 0;
625 }
626
627 static int flv_read_header(AVFormatContext *s)
628 {
629     int offset, flags;
630
631     avio_skip(s->pb, 4);
632     flags = avio_r8(s->pb);
633
634     s->ctx_flags |= AVFMTCTX_NOHEADER;
635
636     if (flags & FLV_HEADER_FLAG_HASVIDEO)
637         if (!create_stream(s, AVMEDIA_TYPE_VIDEO))
638             return AVERROR(ENOMEM);
639     if (flags & FLV_HEADER_FLAG_HASAUDIO)
640         if (!create_stream(s, AVMEDIA_TYPE_AUDIO))
641             return AVERROR(ENOMEM);
642     // Flag doesn't indicate whether or not there is script-data present. Must
643     // create that stream if it's encountered.
644
645     offset = avio_rb32(s->pb);
646     avio_seek(s->pb, offset, SEEK_SET);
647     avio_skip(s->pb, 4);
648
649     s->start_time = 0;
650
651     return 0;
652 }
653
654 static int flv_read_close(AVFormatContext *s)
655 {
656     int i;
657     FLVContext *flv = s->priv_data;
658     for (i=0; i<FLV_STREAM_TYPE_NB; i++)
659         av_freep(&flv->new_extradata[i]);
660     return 0;
661 }
662
663 static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
664 {
665     av_freep(&st->codec->extradata);
666     if (ff_get_extradata(st->codec, s->pb, size) < 0)
667         return AVERROR(ENOMEM);
668     return 0;
669 }
670
671 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
672                                int size)
673 {
674     av_free(flv->new_extradata[stream]);
675     flv->new_extradata[stream] = av_mallocz(size +
676                                             AV_INPUT_BUFFER_PADDING_SIZE);
677     if (!flv->new_extradata[stream])
678         return AVERROR(ENOMEM);
679     flv->new_extradata_size[stream] = size;
680     avio_read(pb, flv->new_extradata[stream], size);
681     return 0;
682 }
683
684 static void clear_index_entries(AVFormatContext *s, int64_t pos)
685 {
686     int i, j, out;
687     av_log(s, AV_LOG_WARNING,
688            "Found invalid index entries, clearing the index.\n");
689     for (i = 0; i < s->nb_streams; i++) {
690         AVStream *st = s->streams[i];
691         /* Remove all index entries that point to >= pos */
692         out = 0;
693         for (j = 0; j < st->nb_index_entries; j++)
694             if (st->index_entries[j].pos < pos)
695                 st->index_entries[out++] = st->index_entries[j];
696         st->nb_index_entries = out;
697     }
698 }
699
700 static int amf_skip_tag(AVIOContext *pb, AMFDataType type)
701 {
702     int nb = -1, ret, parse_name = 1;
703
704     switch (type) {
705     case AMF_DATA_TYPE_NUMBER:
706         avio_skip(pb, 8);
707         break;
708     case AMF_DATA_TYPE_BOOL:
709         avio_skip(pb, 1);
710         break;
711     case AMF_DATA_TYPE_STRING:
712         avio_skip(pb, avio_rb16(pb));
713         break;
714     case AMF_DATA_TYPE_ARRAY:
715         parse_name = 0;
716     case AMF_DATA_TYPE_MIXEDARRAY:
717         nb = avio_rb32(pb);
718     case AMF_DATA_TYPE_OBJECT:
719         while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) {
720             if (parse_name) {
721                 int size = avio_rb16(pb);
722                 if (!size) {
723                     avio_skip(pb, 1);
724                     break;
725                 }
726                 avio_skip(pb, size);
727             }
728             if ((ret = amf_skip_tag(pb, avio_r8(pb))) < 0)
729                 return ret;
730         }
731         break;
732     case AMF_DATA_TYPE_NULL:
733     case AMF_DATA_TYPE_OBJECT_END:
734         break;
735     default:
736         return AVERROR_INVALIDDATA;
737     }
738     return 0;
739 }
740
741 static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
742                            int64_t dts, int64_t next)
743 {
744     AVIOContext *pb = s->pb;
745     AVStream *st    = NULL;
746     char buf[20];
747     int ret = AVERROR_INVALIDDATA;
748     int i, length = -1;
749     int array = 0;
750
751     switch (avio_r8(pb)) {
752     case AMF_DATA_TYPE_ARRAY:
753         array = 1;
754     case AMF_DATA_TYPE_MIXEDARRAY:
755         avio_seek(pb, 4, SEEK_CUR);
756     case AMF_DATA_TYPE_OBJECT:
757         break;
758     default:
759         goto skip;
760     }
761
762     while (array || (ret = amf_get_string(pb, buf, sizeof(buf))) > 0) {
763         AMFDataType type = avio_r8(pb);
764         if (type == AMF_DATA_TYPE_STRING && (array || !strcmp(buf, "text"))) {
765             length = avio_rb16(pb);
766             ret    = av_get_packet(pb, pkt, length);
767             if (ret < 0)
768                 goto skip;
769             else
770                 break;
771         } else {
772             if ((ret = amf_skip_tag(pb, type)) < 0)
773                 goto skip;
774         }
775     }
776
777     if (length < 0) {
778         ret = AVERROR_INVALIDDATA;
779         goto skip;
780     }
781
782     for (i = 0; i < s->nb_streams; i++) {
783         st = s->streams[i];
784         if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
785             break;
786     }
787
788     if (i == s->nb_streams) {
789         st = create_stream(s, AVMEDIA_TYPE_SUBTITLE);
790         if (!st)
791             return AVERROR(ENOMEM);
792         st->codec->codec_id = AV_CODEC_ID_TEXT;
793     }
794
795     pkt->dts  = dts;
796     pkt->pts  = dts;
797     pkt->size = ret;
798
799     pkt->stream_index = st->index;
800     pkt->flags       |= AV_PKT_FLAG_KEY;
801
802 skip:
803     avio_seek(s->pb, next + 4, SEEK_SET);
804
805     return ret;
806 }
807
808 static int resync(AVFormatContext *s)
809 {
810     FLVContext *flv = s->priv_data;
811     int64_t i;
812     int64_t pos = avio_tell(s->pb);
813
814     for (i=0; !avio_feof(s->pb); i++) {
815         int j  = i & (RESYNC_BUFFER_SIZE-1);
816         int j1 = j + RESYNC_BUFFER_SIZE;
817         flv->resync_buffer[j ] =
818         flv->resync_buffer[j1] = avio_r8(s->pb);
819
820         if (i > 22) {
821             unsigned lsize2 = AV_RB32(flv->resync_buffer + j1 - 4);
822             if (lsize2 >= 11 && lsize2 + 8LL < FFMIN(i, RESYNC_BUFFER_SIZE)) {
823                 unsigned  size2 = AV_RB24(flv->resync_buffer + j1 - lsize2 + 1 - 4);
824                 unsigned lsize1 = AV_RB32(flv->resync_buffer + j1 - lsize2 - 8);
825                 if (lsize1 >= 11 && lsize1 + 8LL + lsize2 < FFMIN(i, RESYNC_BUFFER_SIZE)) {
826                     unsigned  size1 = AV_RB24(flv->resync_buffer + j1 - lsize1 + 1 - lsize2 - 8);
827                     if (size1 == lsize1 - 11 && size2  == lsize2 - 11) {
828                         avio_seek(s->pb, pos + i - lsize1 - lsize2 - 8, SEEK_SET);
829                         return 1;
830                     }
831                 }
832             }
833         }
834     }
835     return AVERROR_EOF;
836 }
837
838 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
839 {
840     FLVContext *flv = s->priv_data;
841     int ret, i, size, flags;
842     enum FlvTagType type;
843     int stream_type=-1;
844     int64_t next, pos, meta_pos;
845     int64_t dts, pts = AV_NOPTS_VALUE;
846     int av_uninit(channels);
847     int av_uninit(sample_rate);
848     AVStream *st    = NULL;
849     int last = -1;
850     int orig_size;
851
852 retry:
853     /* pkt size is repeated at end. skip it */
854     for (;; last = avio_rb32(s->pb)) {
855         pos  = avio_tell(s->pb);
856         type = (avio_r8(s->pb) & 0x1F);
857         orig_size =
858         size = avio_rb24(s->pb);
859         dts  = avio_rb24(s->pb);
860         dts |= avio_r8(s->pb) << 24;
861         av_log(s, AV_LOG_TRACE, "type:%d, size:%d, last:%d, dts:%"PRId64" pos:%"PRId64"\n", type, size, last, dts, avio_tell(s->pb));
862         if (avio_feof(s->pb))
863             return AVERROR_EOF;
864         avio_skip(s->pb, 3); /* stream id, always 0 */
865         flags = 0;
866
867         if (flv->validate_next < flv->validate_count) {
868             int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
869             if (pos == validate_pos) {
870                 if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
871                     VALIDATE_INDEX_TS_THRESH) {
872                     flv->validate_next++;
873                 } else {
874                     clear_index_entries(s, validate_pos);
875                     flv->validate_count = 0;
876                 }
877             } else if (pos > validate_pos) {
878                 clear_index_entries(s, validate_pos);
879                 flv->validate_count = 0;
880             }
881         }
882
883         if (size == 0) {
884             ret = AVERROR(EAGAIN);
885             goto leave;
886         }
887
888         next = size + avio_tell(s->pb);
889
890         if (type == FLV_TAG_TYPE_AUDIO) {
891             stream_type = FLV_STREAM_TYPE_AUDIO;
892             flags    = avio_r8(s->pb);
893             size--;
894         } else if (type == FLV_TAG_TYPE_VIDEO) {
895             stream_type = FLV_STREAM_TYPE_VIDEO;
896             flags    = avio_r8(s->pb);
897             size--;
898             if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_VIDEO_INFO_CMD)
899                 goto skip;
900         } else if (type == FLV_TAG_TYPE_META) {
901             stream_type=FLV_STREAM_TYPE_DATA;
902             if (size > 13 + 1 + 4) { // Header-type metadata stuff
903                 int type;
904                 meta_pos = avio_tell(s->pb);
905                 type = flv_read_metabody(s, next);
906                 if (type == 0 && dts == 0 || type < 0 || type == TYPE_UNKNOWN) {
907                     if (type < 0 && flv->validate_count &&
908                         flv->validate_index[0].pos     > next &&
909                         flv->validate_index[0].pos - 4 < next
910                     ) {
911                         av_log(s, AV_LOG_WARNING, "Adjusting next position due to index mismatch\n");
912                         next = flv->validate_index[0].pos - 4;
913                     }
914                     goto skip;
915                 } else if (type == TYPE_ONTEXTDATA) {
916                     avpriv_request_sample(s, "OnTextData packet");
917                     return flv_data_packet(s, pkt, dts, next);
918                 } else if (type == TYPE_ONCAPTION) {
919                     return flv_data_packet(s, pkt, dts, next);
920                 }
921                 avio_seek(s->pb, meta_pos, SEEK_SET);
922             }
923         } else {
924             av_log(s, AV_LOG_DEBUG,
925                    "Skipping flv packet: type %d, size %d, flags %d.\n",
926                    type, size, flags);
927 skip:
928             avio_seek(s->pb, next, SEEK_SET);
929             ret = AVERROR(EAGAIN);
930             goto leave;
931         }
932
933         /* skip empty data packets */
934         if (!size) {
935             ret = AVERROR(EAGAIN);
936             goto leave;
937         }
938
939         /* now find stream */
940         for (i = 0; i < s->nb_streams; i++) {
941             st = s->streams[i];
942             if (stream_type == FLV_STREAM_TYPE_AUDIO) {
943                 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
944                     (s->audio_codec_id || flv_same_audio_codec(st->codec, flags)))
945                     break;
946             } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
947                 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
948                     (s->video_codec_id || flv_same_video_codec(st->codec, flags)))
949                     break;
950             } else if (stream_type == FLV_STREAM_TYPE_DATA) {
951                 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
952                     break;
953             }
954         }
955         if (i == s->nb_streams) {
956             static const enum AVMediaType stream_types[] = {AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_SUBTITLE};
957             av_log(s, AV_LOG_WARNING, "Stream discovered after head already parsed\n");
958             st = create_stream(s, stream_types[stream_type]);
959             if (!st)
960                 return AVERROR(ENOMEM);
961
962         }
963         av_log(s, AV_LOG_TRACE, "%d %X %d \n", stream_type, flags, st->discard);
964
965         if (s->pb->seekable &&
966             ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
967               stream_type == FLV_STREAM_TYPE_AUDIO))
968             av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
969
970         if (  (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || (stream_type == FLV_STREAM_TYPE_AUDIO)))
971             ||(st->discard >= AVDISCARD_BIDIR  &&  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && (stream_type == FLV_STREAM_TYPE_VIDEO)))
972             || st->discard >= AVDISCARD_ALL
973         ) {
974             avio_seek(s->pb, next, SEEK_SET);
975             ret = AVERROR(EAGAIN);
976             goto leave;
977         }
978         break;
979     }
980
981     // if not streamed and no duration from metadata then seek to end to find
982     // the duration from the timestamps
983     if (s->pb->seekable && (!s->duration || s->duration == AV_NOPTS_VALUE) && !flv->searched_for_end) {
984         int size;
985         const int64_t pos   = avio_tell(s->pb);
986         // Read the last 4 bytes of the file, this should be the size of the
987         // previous FLV tag. Use the timestamp of its payload as duration.
988         int64_t fsize       = avio_size(s->pb);
989 retry_duration:
990         avio_seek(s->pb, fsize - 4, SEEK_SET);
991         size = avio_rb32(s->pb);
992         // Seek to the start of the last FLV tag at position (fsize - 4 - size)
993         // but skip the byte indicating the type.
994         avio_seek(s->pb, fsize - 3 - size, SEEK_SET);
995         if (size == avio_rb24(s->pb) + 11) {
996             uint32_t ts = avio_rb24(s->pb);
997             ts         |= avio_r8(s->pb) << 24;
998             if (ts)
999                 s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
1000             else if (fsize >= 8 && fsize - 8 >= size) {
1001                 fsize -= size+4;
1002                 goto retry_duration;
1003             }
1004         }
1005
1006         avio_seek(s->pb, pos, SEEK_SET);
1007         flv->searched_for_end = 1;
1008     }
1009
1010     if (stream_type == FLV_STREAM_TYPE_AUDIO) {
1011         int bits_per_coded_sample;
1012         channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
1013         sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
1014                                 FLV_AUDIO_SAMPLERATE_OFFSET) >> 3;
1015         bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
1016         if (!st->codec->channels || !st->codec->sample_rate ||
1017             !st->codec->bits_per_coded_sample) {
1018             st->codec->channels              = channels;
1019             st->codec->channel_layout        = channels == 1
1020                                                ? AV_CH_LAYOUT_MONO
1021                                                : AV_CH_LAYOUT_STEREO;
1022             st->codec->sample_rate           = sample_rate;
1023             st->codec->bits_per_coded_sample = bits_per_coded_sample;
1024         }
1025         if (!st->codec->codec_id) {
1026             flv_set_audio_codec(s, st, st->codec,
1027                                 flags & FLV_AUDIO_CODECID_MASK);
1028             flv->last_sample_rate =
1029             sample_rate           = st->codec->sample_rate;
1030             flv->last_channels    =
1031             channels              = st->codec->channels;
1032         } else {
1033             AVCodecContext ctx = {0};
1034             ctx.sample_rate = sample_rate;
1035             ctx.bits_per_coded_sample = bits_per_coded_sample;
1036             flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK);
1037             sample_rate = ctx.sample_rate;
1038         }
1039     } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
1040         size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
1041     } else if (stream_type == FLV_STREAM_TYPE_DATA) {
1042         st->codec->codec_id = AV_CODEC_ID_TEXT;
1043     }
1044
1045     if (st->codec->codec_id == AV_CODEC_ID_AAC ||
1046         st->codec->codec_id == AV_CODEC_ID_H264 ||
1047         st->codec->codec_id == AV_CODEC_ID_MPEG4) {
1048         int type = avio_r8(s->pb);
1049         size--;
1050         if (st->codec->codec_id == AV_CODEC_ID_H264 || st->codec->codec_id == AV_CODEC_ID_MPEG4) {
1051             // sign extension
1052             int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
1053             pts = dts + cts;
1054             if (cts < 0) { // dts might be wrong
1055                 if (!flv->wrong_dts)
1056                     av_log(s, AV_LOG_WARNING,
1057                         "Negative cts, previous timestamps might be wrong.\n");
1058                 flv->wrong_dts = 1;
1059             } else if (FFABS(dts - pts) > 1000*60*15) {
1060                 av_log(s, AV_LOG_WARNING,
1061                        "invalid timestamps %"PRId64" %"PRId64"\n", dts, pts);
1062                 dts = pts = AV_NOPTS_VALUE;
1063             }
1064         }
1065         if (type == 0 && (!st->codec->extradata || st->codec->codec_id == AV_CODEC_ID_AAC ||
1066             st->codec->codec_id == AV_CODEC_ID_H264)) {
1067             AVDictionaryEntry *t;
1068
1069             if (st->codec->extradata) {
1070                 if ((ret = flv_queue_extradata(flv, s->pb, stream_type, size)) < 0)
1071                     return ret;
1072                 ret = AVERROR(EAGAIN);
1073                 goto leave;
1074             }
1075             if ((ret = flv_get_extradata(s, st, size)) < 0)
1076                 return ret;
1077
1078             /* Workaround for buggy Omnia A/XE encoder */
1079             t = av_dict_get(s->metadata, "Encoder", NULL, 0);
1080             if (st->codec->codec_id == AV_CODEC_ID_AAC && t && !strcmp(t->value, "Omnia A/XE"))
1081                 st->codec->extradata_size = 2;
1082
1083             if (st->codec->codec_id == AV_CODEC_ID_AAC && 0) {
1084                 MPEG4AudioConfig cfg;
1085
1086                 if (avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
1087                                              st->codec->extradata_size * 8, 1) >= 0) {
1088                 st->codec->channels       = cfg.channels;
1089                 st->codec->channel_layout = 0;
1090                 if (cfg.ext_sample_rate)
1091                     st->codec->sample_rate = cfg.ext_sample_rate;
1092                 else
1093                     st->codec->sample_rate = cfg.sample_rate;
1094                 av_log(s, AV_LOG_TRACE, "mp4a config channels %d sample rate %d\n",
1095                         st->codec->channels, st->codec->sample_rate);
1096                 }
1097             }
1098
1099             ret = AVERROR(EAGAIN);
1100             goto leave;
1101         }
1102     }
1103
1104     /* skip empty data packets */
1105     if (!size) {
1106         ret = AVERROR(EAGAIN);
1107         goto leave;
1108     }
1109
1110     ret = av_get_packet(s->pb, pkt, size);
1111     if (ret < 0)
1112         return ret;
1113     pkt->dts          = dts;
1114     pkt->pts          = pts == AV_NOPTS_VALUE ? dts : pts;
1115     pkt->stream_index = st->index;
1116     if (flv->new_extradata[stream_type]) {
1117         uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
1118                                                 flv->new_extradata_size[stream_type]);
1119         if (side) {
1120             memcpy(side, flv->new_extradata[stream_type],
1121                    flv->new_extradata_size[stream_type]);
1122             av_freep(&flv->new_extradata[stream_type]);
1123             flv->new_extradata_size[stream_type] = 0;
1124         }
1125     }
1126     if (stream_type == FLV_STREAM_TYPE_AUDIO &&
1127                     (sample_rate != flv->last_sample_rate ||
1128                      channels    != flv->last_channels)) {
1129         flv->last_sample_rate = sample_rate;
1130         flv->last_channels    = channels;
1131         ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
1132     }
1133
1134     if (    stream_type == FLV_STREAM_TYPE_AUDIO ||
1135             ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) ||
1136             stream_type == FLV_STREAM_TYPE_DATA)
1137         pkt->flags |= AV_PKT_FLAG_KEY;
1138
1139 leave:
1140     last = avio_rb32(s->pb);
1141     if (last != orig_size + 11 &&
1142         (last != orig_size || !last) &&
1143         !flv->broken_sizes) {
1144         av_log(s, AV_LOG_ERROR, "Packet mismatch %d %d\n", last, orig_size + 11);
1145         avio_seek(s->pb, pos + 1, SEEK_SET);
1146         ret = resync(s);
1147         av_free_packet(pkt);
1148         if (ret >= 0) {
1149             goto retry;
1150         }
1151     }
1152     return ret;
1153 }
1154
1155 static int flv_read_seek(AVFormatContext *s, int stream_index,
1156                          int64_t ts, int flags)
1157 {
1158     FLVContext *flv = s->priv_data;
1159     flv->validate_count = 0;
1160     return avio_seek_time(s->pb, stream_index, ts, flags);
1161 }
1162
1163 #define OFFSET(x) offsetof(FLVContext, x)
1164 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1165 static const AVOption options[] = {
1166     { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
1167     { NULL }
1168 };
1169
1170 static const AVClass flv_class = {
1171     .class_name = "flvdec",
1172     .item_name  = av_default_item_name,
1173     .option     = options,
1174     .version    = LIBAVUTIL_VERSION_INT,
1175 };
1176
1177 AVInputFormat ff_flv_demuxer = {
1178     .name           = "flv",
1179     .long_name      = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
1180     .priv_data_size = sizeof(FLVContext),
1181     .read_probe     = flv_probe,
1182     .read_header    = flv_read_header,
1183     .read_packet    = flv_read_packet,
1184     .read_seek      = flv_read_seek,
1185     .read_close     = flv_read_close,
1186     .extensions     = "flv",
1187     .priv_class     = &flv_class,
1188 };
1189
1190 static const AVClass live_flv_class = {
1191     .class_name = "live_flvdec",
1192     .item_name  = av_default_item_name,
1193     .option     = options,
1194     .version    = LIBAVUTIL_VERSION_INT,
1195 };
1196
1197 AVInputFormat ff_live_flv_demuxer = {
1198     .name           = "live_flv",
1199     .long_name      = NULL_IF_CONFIG_SMALL("live RTMP FLV (Flash Video)"),
1200     .priv_data_size = sizeof(FLVContext),
1201     .read_probe     = live_flv_probe,
1202     .read_header    = flv_read_header,
1203     .read_packet    = flv_read_packet,
1204     .read_seek      = flv_read_seek,
1205     .read_close     = flv_read_close,
1206     .extensions     = "flv",
1207     .priv_class     = &live_flv_class,
1208     .flags          = AVFMT_TS_DISCONT
1209 };