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