3 * Copyright (c) 2003 The FFmpeg Project
5 * This demuxer will generate a 1 byte extradata for VP6F content.
7 * - upper 4bits: difference between encoded width and visible width
8 * - lower 4bits: difference between encoded height and visible height
10 * This file is part of FFmpeg.
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.
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.
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
27 #include "libavutil/avstring.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/intfloat.h"
31 #include "libavutil/mathematics.h"
32 #include "libavcodec/bytestream.h"
33 #include "libavcodec/mpeg4audio.h"
36 #include "avio_internal.h"
39 #define VALIDATE_INDEX_TS_THRESH 2500
42 const AVClass *class; ///< Class for private options.
43 int trust_metadata; ///< configure streams according onMetaData
44 int wrong_dts; ///< wrong dts due to negative cts
45 uint8_t *new_extradata[FLV_STREAM_TYPE_NB];
46 int new_extradata_size[FLV_STREAM_TYPE_NB];
58 static int flv_probe(AVProbeData *p)
63 if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
64 return AVPROBE_SCORE_MAX;
69 static AVStream *create_stream(AVFormatContext *s, int codec_type)
71 AVStream *st = avformat_new_stream(s, NULL);
74 st->codec->codec_type = codec_type;
75 if(s->nb_streams>=3 ||( s->nb_streams==2
76 && s->streams[0]->codec->codec_type != AVMEDIA_TYPE_DATA
77 && s->streams[1]->codec->codec_type != AVMEDIA_TYPE_DATA))
78 s->ctx_flags &= ~AVFMTCTX_NOHEADER;
80 avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
83 static int flv_same_audio_codec(AVCodecContext *acodec, int flags)
85 int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
86 int flv_codecid = flags & FLV_AUDIO_CODECID_MASK;
89 if (!acodec->codec_id && !acodec->codec_tag)
92 if (acodec->bits_per_coded_sample != bits_per_coded_sample)
96 //no distinction between S16 and S8 PCM codec flags
98 codec_id = bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 :
100 AV_CODEC_ID_PCM_S16BE;
102 AV_CODEC_ID_PCM_S16LE;
104 return codec_id == acodec->codec_id;
105 case FLV_CODECID_PCM_LE:
106 codec_id = bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 : AV_CODEC_ID_PCM_S16LE;
107 return codec_id == acodec->codec_id;
108 case FLV_CODECID_AAC:
109 return acodec->codec_id == AV_CODEC_ID_AAC;
110 case FLV_CODECID_ADPCM:
111 return acodec->codec_id == AV_CODEC_ID_ADPCM_SWF;
112 case FLV_CODECID_SPEEX:
113 return acodec->codec_id == AV_CODEC_ID_SPEEX;
114 case FLV_CODECID_MP3:
115 return acodec->codec_id == AV_CODEC_ID_MP3;
116 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
117 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
118 case FLV_CODECID_NELLYMOSER:
119 return acodec->codec_id == AV_CODEC_ID_NELLYMOSER;
120 case FLV_CODECID_PCM_MULAW:
121 return acodec->sample_rate == 8000 &&
122 acodec->codec_id == AV_CODEC_ID_PCM_MULAW;
123 case FLV_CODECID_PCM_ALAW:
124 return acodec->sample_rate = 8000 &&
125 acodec->codec_id == AV_CODEC_ID_PCM_ALAW;
127 return acodec->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
131 static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, AVCodecContext *acodec, int flv_codecid) {
132 switch(flv_codecid) {
133 //no distinction between S16 and S8 PCM codec flags
134 case FLV_CODECID_PCM:
135 acodec->codec_id = acodec->bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 :
137 AV_CODEC_ID_PCM_S16BE;
139 AV_CODEC_ID_PCM_S16LE;
142 case FLV_CODECID_PCM_LE:
143 acodec->codec_id = acodec->bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 : AV_CODEC_ID_PCM_S16LE; break;
144 case FLV_CODECID_AAC : acodec->codec_id = AV_CODEC_ID_AAC; break;
145 case FLV_CODECID_ADPCM: acodec->codec_id = AV_CODEC_ID_ADPCM_SWF; break;
146 case FLV_CODECID_SPEEX:
147 acodec->codec_id = AV_CODEC_ID_SPEEX;
148 acodec->sample_rate = 16000;
150 case FLV_CODECID_MP3 : acodec->codec_id = AV_CODEC_ID_MP3 ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
151 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
152 acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
153 acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
155 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
156 acodec->sample_rate = 16000;
157 acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
159 case FLV_CODECID_NELLYMOSER:
160 acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
162 case FLV_CODECID_PCM_MULAW:
163 acodec->sample_rate = 8000;
164 acodec->codec_id = AV_CODEC_ID_PCM_MULAW;
166 case FLV_CODECID_PCM_ALAW:
167 acodec->sample_rate = 8000;
168 acodec->codec_id = AV_CODEC_ID_PCM_ALAW;
171 av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
172 acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
176 static int flv_same_video_codec(AVCodecContext *vcodec, int flags)
178 int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
180 if (!vcodec->codec_id && !vcodec->codec_tag)
183 switch (flv_codecid) {
184 case FLV_CODECID_H263:
185 return vcodec->codec_id == AV_CODEC_ID_FLV1;
186 case FLV_CODECID_SCREEN:
187 return vcodec->codec_id == AV_CODEC_ID_FLASHSV;
188 case FLV_CODECID_SCREEN2:
189 return vcodec->codec_id == AV_CODEC_ID_FLASHSV2;
190 case FLV_CODECID_VP6:
191 return vcodec->codec_id == AV_CODEC_ID_VP6F;
192 case FLV_CODECID_VP6A:
193 return vcodec->codec_id == AV_CODEC_ID_VP6A;
194 case FLV_CODECID_H264:
195 return vcodec->codec_id == AV_CODEC_ID_H264;
197 return vcodec->codec_tag == flv_codecid;
201 static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
202 AVCodecContext *vcodec = vstream->codec;
203 switch(flv_codecid) {
204 case FLV_CODECID_H263 : vcodec->codec_id = AV_CODEC_ID_FLV1 ; break;
205 case FLV_CODECID_REALH263: vcodec->codec_id = AV_CODEC_ID_H263 ; break; // Really mean it this time
206 case FLV_CODECID_SCREEN: vcodec->codec_id = AV_CODEC_ID_FLASHSV; break;
207 case FLV_CODECID_SCREEN2: vcodec->codec_id = AV_CODEC_ID_FLASHSV2; break;
208 case FLV_CODECID_VP6 : vcodec->codec_id = AV_CODEC_ID_VP6F ;
209 case FLV_CODECID_VP6A :
210 if(flv_codecid == FLV_CODECID_VP6A)
211 vcodec->codec_id = AV_CODEC_ID_VP6A;
212 if(vcodec->extradata_size != 1) {
213 vcodec->extradata_size = 1;
214 vcodec->extradata = av_malloc(1 + FF_INPUT_BUFFER_PADDING_SIZE);
216 vcodec->extradata[0] = avio_r8(s->pb);
217 return 1; // 1 byte body size adjustment for flv_read_packet()
218 case FLV_CODECID_H264:
219 vcodec->codec_id = AV_CODEC_ID_H264;
220 return 3; // not 4, reading packet type will consume one byte
221 case FLV_CODECID_MPEG4:
222 vcodec->codec_id = AV_CODEC_ID_MPEG4;
225 av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
226 vcodec->codec_tag = flv_codecid;
232 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize) {
233 int length = avio_rb16(ioc);
234 if(length >= buffsize) {
235 avio_skip(ioc, length);
239 avio_read(ioc, buffer, length);
241 buffer[length] = '\0';
246 static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, AVStream *vstream, int64_t max_pos) {
247 FLVContext *flv = s->priv_data;
248 unsigned int timeslen = 0, fileposlen = 0, i;
250 int64_t *times = NULL;
251 int64_t *filepositions = NULL;
252 int ret = AVERROR(ENOSYS);
253 int64_t initial_pos = avio_tell(ioc);
255 if(vstream->nb_index_entries>0){
256 av_log(s, AV_LOG_WARNING, "Skiping duplicate index\n");
260 if (s->flags & AVFMT_FLAG_IGNIDX)
263 while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
264 int64_t** current_array;
265 unsigned int arraylen;
267 // Expect array object in context
268 if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
271 arraylen = avio_rb32(ioc);
275 if (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times){
276 current_array= ×
278 }else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) && !filepositions){
279 current_array= &filepositions;
280 fileposlen= arraylen;
281 }else // unexpected metatag inside keyframes, will not use such metadata for indexing
284 if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
285 ret = AVERROR(ENOMEM);
289 for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
290 if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
292 current_array[0][i] = av_int2double(avio_rb64(ioc));
294 if (times && filepositions) {
295 // All done, exiting at a position allowing amf_parse_object
296 // to finish parsing the object
302 if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
303 for (i = 0; i < fileposlen; i++) {
304 av_add_index_entry(vstream, filepositions[i], times[i]*1000,
305 0, 0, AVINDEX_KEYFRAME);
307 flv->validate_index[i].pos = filepositions[i];
308 flv->validate_index[i].dts = times[i] * 1000;
309 flv->validate_count = i + 1;
314 av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
319 av_freep(&filepositions);
320 avio_seek(ioc, initial_pos, SEEK_SET);
324 static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
325 AVCodecContext *acodec, *vcodec;
326 FLVContext *flv = s->priv_data;
328 AMFDataType amf_type;
335 amf_type = avio_r8(ioc);
338 case AMF_DATA_TYPE_NUMBER:
339 num_val = av_int2double(avio_rb64(ioc)); break;
340 case AMF_DATA_TYPE_BOOL:
341 num_val = avio_r8(ioc); break;
342 case AMF_DATA_TYPE_STRING:
343 if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
346 case AMF_DATA_TYPE_OBJECT:
347 if ((vstream || astream) && ioc->seekable && key && !strcmp(KEYFRAMES_TAG, key) && depth == 1)
348 if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
350 av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
352 while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
353 if (amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
354 return -1; //if we couldn't skip, bomb out.
356 if(avio_r8(ioc) != AMF_END_OF_OBJECT)
359 case AMF_DATA_TYPE_NULL:
360 case AMF_DATA_TYPE_UNDEFINED:
361 case AMF_DATA_TYPE_UNSUPPORTED:
362 break; //these take up no additional space
363 case AMF_DATA_TYPE_MIXEDARRAY:
364 avio_skip(ioc, 4); //skip 32-bit max array index
365 while(avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
366 //this is the only case in which we would want a nested parse to not skip over the object
367 if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
370 if(avio_r8(ioc) != AMF_END_OF_OBJECT)
373 case AMF_DATA_TYPE_ARRAY: {
374 unsigned int arraylen, i;
376 arraylen = avio_rb32(ioc);
377 for(i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
378 if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
379 return -1; //if we couldn't skip, bomb out.
383 case AMF_DATA_TYPE_DATE:
384 avio_skip(ioc, 8 + 2); //timestamp (double) and UTC offset (int16)
386 default: //unsupported type, we couldn't skip
390 if(depth == 1 && key) { //only look for metadata values when we are not nested and key != NULL
391 acodec = astream ? astream->codec : NULL;
392 vcodec = vstream ? vstream->codec : NULL;
394 if (amf_type == AMF_DATA_TYPE_NUMBER) {
395 if (!strcmp(key, "duration"))
396 s->duration = num_val * AV_TIME_BASE;
397 else if (!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
398 vcodec->bit_rate = num_val * 1024.0;
399 else if (!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
400 acodec->bit_rate = num_val * 1024.0;
401 else if (!strcmp(key, "datastream")) {
402 AVStream *st = create_stream(s, AVMEDIA_TYPE_DATA);
404 return AVERROR(ENOMEM);
405 st->codec->codec_id = AV_CODEC_ID_TEXT;
406 } else if (flv->trust_metadata) {
407 if (!strcmp(key, "videocodecid") && vcodec) {
408 flv_set_video_codec(s, vstream, num_val);
410 if (!strcmp(key, "audiocodecid") && acodec) {
411 flv_set_audio_codec(s, astream, acodec, num_val);
413 if (!strcmp(key, "audiosamplerate") && acodec) {
414 acodec->sample_rate = num_val;
416 if (!strcmp(key, "width") && vcodec) {
417 vcodec->width = num_val;
419 if (!strcmp(key, "height") && vcodec) {
420 vcodec->height = num_val;
425 if (amf_type == AMF_DATA_TYPE_OBJECT && s->nb_streams == 1 &&
426 ((!acodec && !strcmp(key, "audiocodecid")) ||
427 (!vcodec && !strcmp(key, "videocodecid"))))
428 s->ctx_flags &= ~AVFMTCTX_NOHEADER; //If there is either audio/video missing, codecid will be an empty object
430 if (!strcmp(key, "duration") ||
431 !strcmp(key, "filesize") ||
432 !strcmp(key, "width") ||
433 !strcmp(key, "height") ||
434 !strcmp(key, "videodatarate") ||
435 !strcmp(key, "framerate") ||
436 !strcmp(key, "videocodecid") ||
437 !strcmp(key, "audiodatarate") ||
438 !strcmp(key, "audiosamplerate") ||
439 !strcmp(key, "audiosamplesize") ||
440 !strcmp(key, "stereo") ||
441 !strcmp(key, "audiocodecid"))
444 if(amf_type == AMF_DATA_TYPE_BOOL) {
445 av_strlcpy(str_val, num_val > 0 ? "true" : "false", sizeof(str_val));
446 av_dict_set(&s->metadata, key, str_val, 0);
447 } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
448 snprintf(str_val, sizeof(str_val), "%.f", num_val);
449 av_dict_set(&s->metadata, key, str_val, 0);
450 } else if (amf_type == AMF_DATA_TYPE_STRING)
451 av_dict_set(&s->metadata, key, str_val, 0);
457 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
459 AVStream *stream, *astream, *vstream, *dstream;
462 char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
464 vstream = astream = dstream = NULL;
467 //first object needs to be "onMetaData" string
469 if (type != AMF_DATA_TYPE_STRING ||
470 amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
473 if (!strcmp(buffer, "onTextData"))
476 if (strcmp(buffer, "onMetaData"))
479 //find the streams now so that amf_parse_object doesn't need to do the lookup every time it is called.
480 for(i = 0; i < s->nb_streams; i++) {
481 stream = s->streams[i];
482 if(stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) vstream = stream;
483 else if(stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) astream = stream;
484 else if(stream->codec->codec_type == AVMEDIA_TYPE_DATA) dstream = stream;
487 //parse the second object (we want a mixed array)
488 if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
494 static int flv_read_header(AVFormatContext *s)
499 flags = avio_r8(s->pb);
500 /* old flvtool cleared this field */
501 /* FIXME: better fix needed */
503 flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
504 av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
507 s->ctx_flags |= AVFMTCTX_NOHEADER;
509 if(flags & FLV_HEADER_FLAG_HASVIDEO){
510 if(!create_stream(s, AVMEDIA_TYPE_VIDEO))
511 return AVERROR(ENOMEM);
513 if(flags & FLV_HEADER_FLAG_HASAUDIO){
514 if(!create_stream(s, AVMEDIA_TYPE_AUDIO))
515 return AVERROR(ENOMEM);
517 // Flag doesn't indicate whether or not there is script-data present. Must
518 // create that stream if it's encountered.
520 offset = avio_rb32(s->pb);
521 avio_seek(s->pb, offset, SEEK_SET);
529 static int flv_read_close(AVFormatContext *s)
532 FLVContext *flv = s->priv_data;
533 for(i=0; i<FLV_STREAM_TYPE_NB; i++)
534 av_freep(&flv->new_extradata[i]);
538 static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
540 av_free(st->codec->extradata);
541 st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
542 if (!st->codec->extradata)
543 return AVERROR(ENOMEM);
544 st->codec->extradata_size = size;
545 avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
549 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
552 av_free(flv->new_extradata[stream]);
553 flv->new_extradata[stream] = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
554 if (!flv->new_extradata[stream])
555 return AVERROR(ENOMEM);
556 flv->new_extradata_size[stream] = size;
557 avio_read(pb, flv->new_extradata[stream], size);
561 static void clear_index_entries(AVFormatContext *s, int64_t pos)
564 av_log(s, AV_LOG_WARNING, "Found invalid index entries, clearing the index.\n");
565 for (i = 0; i < s->nb_streams; i++) {
566 AVStream *st = s->streams[i];
567 /* Remove all index entries that point to >= pos */
569 for (j = 0; j < st->nb_index_entries; j++) {
570 if (st->index_entries[j].pos < pos)
571 st->index_entries[out++] = st->index_entries[j];
573 st->nb_index_entries = out;
578 static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
579 int64_t dts, int64_t next)
581 int ret = AVERROR_INVALIDDATA, i;
582 AVIOContext *pb = s->pb;
589 if (type == AMF_DATA_TYPE_MIXEDARRAY)
590 avio_seek(pb, 4, SEEK_CUR);
591 else if (type != AMF_DATA_TYPE_OBJECT)
594 amf_get_string(pb, buf, sizeof(buf));
595 if (strcmp(buf, "type") || avio_r8(pb) != AMF_DATA_TYPE_STRING)
598 amf_get_string(pb, buf, sizeof(buf));
599 //FIXME parse it as codec_id
600 amf_get_string(pb, buf, sizeof(buf));
601 if (strcmp(buf, "text") || avio_r8(pb) != AMF_DATA_TYPE_STRING)
604 length = avio_rb16(pb);
605 ret = av_get_packet(s->pb, pkt, length);
611 for (i = 0; i < s->nb_streams; i++) {
613 if (st->codec->codec_type == AVMEDIA_TYPE_DATA)
617 if (i == s->nb_streams) {
618 st = create_stream(s, AVMEDIA_TYPE_DATA);
621 st->codec->codec_id = AV_CODEC_ID_TEXT;
628 pkt->stream_index = st->index;
629 pkt->flags |= AV_PKT_FLAG_KEY;
631 avio_seek(s->pb, next + 4, SEEK_SET);
636 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
638 FLVContext *flv = s->priv_data;
639 int ret, i, type, size, flags;
642 int64_t dts, pts = AV_NOPTS_VALUE;
643 int av_uninit(channels);
644 int av_uninit(sample_rate);
647 for(;;avio_skip(s->pb, 4)){ /* pkt size is repeated at end. skip it */
648 pos = avio_tell(s->pb);
649 type = avio_r8(s->pb);
650 size = avio_rb24(s->pb);
651 dts = avio_rb24(s->pb);
652 dts |= avio_r8(s->pb) << 24;
653 av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
656 avio_skip(s->pb, 3); /* stream id, always 0 */
659 if (flv->validate_next < flv->validate_count) {
660 int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
661 if (pos == validate_pos) {
662 if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
663 VALIDATE_INDEX_TS_THRESH) {
664 flv->validate_next++;
666 clear_index_entries(s, validate_pos);
667 flv->validate_count = 0;
669 } else if (pos > validate_pos) {
670 clear_index_entries(s, validate_pos);
671 flv->validate_count = 0;
678 next= size + avio_tell(s->pb);
680 if (type == FLV_TAG_TYPE_AUDIO) {
681 stream_type=FLV_STREAM_TYPE_AUDIO;
682 flags = avio_r8(s->pb);
684 } else if (type == FLV_TAG_TYPE_VIDEO) {
685 stream_type=FLV_STREAM_TYPE_VIDEO;
686 flags = avio_r8(s->pb);
688 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_VIDEO_INFO_CMD)
690 } else if (type == FLV_TAG_TYPE_META) {
691 if (size > 13+1+4 && dts == 0) { // Header-type metadata stuff
692 flv_read_metabody(s, next);
694 } else if (dts != 0) { // Script-data "special" metadata frames - don't skip
695 stream_type=FLV_STREAM_TYPE_DATA;
700 av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
702 avio_seek(s->pb, next, SEEK_SET);
706 /* skip empty data packets */
710 /* now find stream */
711 for(i=0;i<s->nb_streams;i++) {
713 if (stream_type == FLV_STREAM_TYPE_AUDIO) {
714 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
715 flv_same_audio_codec(st->codec, flags)) {
719 if (stream_type == FLV_STREAM_TYPE_VIDEO) {
720 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
721 flv_same_video_codec(st->codec, flags)) {
724 } else if (stream_type == FLV_STREAM_TYPE_DATA) {
725 if (st->codec->codec_type == AVMEDIA_TYPE_DATA)
729 if(i == s->nb_streams){
730 av_log(s, AV_LOG_WARNING, "Stream discovered after head already parsed\n");
731 st = create_stream(s,
732 (int[]){AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_DATA}[stream_type]);
734 return AVERROR(ENOMEM);
737 av_dlog(s, "%d %X %d \n", stream_type, flags, st->discard);
738 if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || (stream_type == FLV_STREAM_TYPE_AUDIO)))
739 ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && (stream_type == FLV_STREAM_TYPE_VIDEO)))
740 || st->discard >= AVDISCARD_ALL
742 avio_seek(s->pb, next, SEEK_SET);
745 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
746 av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
750 // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps
751 if(s->pb->seekable && (!s->duration || s->duration==AV_NOPTS_VALUE) && !flv->searched_for_end){
753 const int64_t pos= avio_tell(s->pb);
754 int64_t fsize= avio_size(s->pb);
756 avio_seek(s->pb, fsize-4, SEEK_SET);
757 size= avio_rb32(s->pb);
758 avio_seek(s->pb, fsize-3-size, SEEK_SET);
759 if(size == avio_rb24(s->pb) + 11){
760 uint32_t ts = avio_rb24(s->pb);
761 ts |= avio_r8(s->pb) << 24;
763 s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
764 else if (fsize >= 8 && fsize - 8 >= size){
770 avio_seek(s->pb, pos, SEEK_SET);
771 flv->searched_for_end = 1;
774 if(stream_type == FLV_STREAM_TYPE_AUDIO){
775 int bits_per_coded_sample;
776 channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
777 sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
778 bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
779 if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
780 st->codec->channels = channels;
781 st->codec->sample_rate = sample_rate;
782 st->codec->bits_per_coded_sample = bits_per_coded_sample;
784 if(!st->codec->codec_id){
785 flv_set_audio_codec(s, st, st->codec, flags & FLV_AUDIO_CODECID_MASK);
786 flv->last_sample_rate = sample_rate = st->codec->sample_rate;
787 flv->last_channels = channels = st->codec->channels;
790 ctx.sample_rate = sample_rate;
791 flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK);
792 sample_rate = ctx.sample_rate;
794 } else if(stream_type == FLV_STREAM_TYPE_VIDEO) {
795 size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
798 if (st->codec->codec_id == AV_CODEC_ID_AAC ||
799 st->codec->codec_id == AV_CODEC_ID_H264 ||
800 st->codec->codec_id == AV_CODEC_ID_MPEG4) {
801 int type = avio_r8(s->pb);
803 if (st->codec->codec_id == AV_CODEC_ID_H264 || st->codec->codec_id == AV_CODEC_ID_MPEG4) {
804 int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000; // sign extension
806 if (cts < 0) { // dts are wrong
808 av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
811 dts = AV_NOPTS_VALUE;
813 if (type == 0 && (!st->codec->extradata || st->codec->codec_id == AV_CODEC_ID_AAC)) {
814 if (st->codec->extradata) {
815 if ((ret = flv_queue_extradata(flv, s->pb, stream_type, size)) < 0)
817 ret = AVERROR(EAGAIN);
820 if ((ret = flv_get_extradata(s, st, size)) < 0)
822 if (st->codec->codec_id == AV_CODEC_ID_AAC) {
823 MPEG4AudioConfig cfg;
824 if (avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
825 st->codec->extradata_size * 8, 1) >= 0) {
826 st->codec->channels = cfg.channels;
827 if (cfg.ext_sample_rate)
828 st->codec->sample_rate = cfg.ext_sample_rate;
830 st->codec->sample_rate = cfg.sample_rate;
831 av_dlog(s, "mp4a config channels %d sample rate %d\n",
832 st->codec->channels, st->codec->sample_rate);
836 ret = AVERROR(EAGAIN);
841 /* skip empty data packets */
843 ret = AVERROR(EAGAIN);
847 ret= av_get_packet(s->pb, pkt, size);
851 pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
852 pkt->stream_index = st->index;
853 if (flv->new_extradata[stream_type]) {
854 uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
855 flv->new_extradata_size[stream_type]);
857 memcpy(side, flv->new_extradata[stream_type],
858 flv->new_extradata_size[stream_type]);
859 av_freep(&flv->new_extradata[stream_type]);
860 flv->new_extradata_size[stream_type] = 0;
863 if (stream_type == FLV_STREAM_TYPE_AUDIO && (sample_rate != flv->last_sample_rate ||
864 channels != flv->last_channels)) {
865 flv->last_sample_rate = sample_rate;
866 flv->last_channels = channels;
867 ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
870 if ( stream_type == FLV_STREAM_TYPE_AUDIO ||
871 ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) ||
872 stream_type == FLV_STREAM_TYPE_DATA)
873 pkt->flags |= AV_PKT_FLAG_KEY;
880 static int flv_read_seek(AVFormatContext *s, int stream_index,
881 int64_t ts, int flags)
883 FLVContext *flv = s->priv_data;
884 flv->validate_count = 0;
885 return avio_seek_time(s->pb, stream_index, ts, flags);
888 #define OFFSET(x) offsetof(FLVContext, x)
889 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
890 static const AVOption options[] = {
891 { "flv_metadata", "Allocate streams according the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD},
895 static const AVClass class = {
896 .class_name = "flvdec",
897 .item_name = av_default_item_name,
899 .version = LIBAVUTIL_VERSION_INT,
902 AVInputFormat ff_flv_demuxer = {
904 .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
905 .priv_data_size = sizeof(FLVContext),
906 .read_probe = flv_probe,
907 .read_header = flv_read_header,
908 .read_packet = flv_read_packet,
909 .read_seek = flv_read_seek,
910 .read_close = flv_read_close,
912 .priv_class = &class,