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