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