3 * Copyright (c) 2003 The Libav 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 Libav.
12 * Libav 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 * Libav 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 Libav; 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/channel_layout.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/intfloat.h"
32 #include "libavutil/mathematics.h"
33 #include "libavcodec/bytestream.h"
34 #include "libavcodec/mpeg4audio.h"
37 #include "avio_internal.h"
40 #define KEYFRAMES_TAG "keyframes"
41 #define KEYFRAMES_TIMESTAMP_TAG "times"
42 #define KEYFRAMES_BYTEOFFSET_TAG "filepositions"
44 #define VALIDATE_INDEX_TS_THRESH 2500
47 const AVClass *class; ///< Class for private options.
48 int trust_metadata; ///< configure streams according onMetaData
49 int wrong_dts; ///< wrong dts due to negative cts
50 uint8_t *new_extradata[2];
51 int new_extradata_size[2];
62 static int flv_probe(AVProbeData *p)
67 if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
68 return AVPROBE_SCORE_MAX;
73 static AVStream *create_stream(AVFormatContext *s, int codec_type)
75 AVStream *st = avformat_new_stream(s, NULL);
78 st->codec->codec_type = codec_type;
79 avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
82 static int flv_same_audio_codec(AVCodecContext *acodec, int flags)
84 int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
85 int flv_codecid = flags & FLV_AUDIO_CODECID_MASK;
88 if (!acodec->codec_id && !acodec->codec_tag)
91 if (acodec->bits_per_coded_sample != bits_per_coded_sample)
95 //no distinction between S16 and S8 PCM codec flags
97 codec_id = bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 :
99 AV_CODEC_ID_PCM_S16BE;
101 AV_CODEC_ID_PCM_S16LE;
103 return codec_id == acodec->codec_id;
104 case FLV_CODECID_PCM_LE:
105 codec_id = bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 : AV_CODEC_ID_PCM_S16LE;
106 return codec_id == acodec->codec_id;
107 case FLV_CODECID_AAC:
108 return acodec->codec_id == AV_CODEC_ID_AAC;
109 case FLV_CODECID_ADPCM:
110 return acodec->codec_id == AV_CODEC_ID_ADPCM_SWF;
111 case FLV_CODECID_SPEEX:
112 return acodec->codec_id == AV_CODEC_ID_SPEEX;
113 case FLV_CODECID_MP3:
114 return acodec->codec_id == AV_CODEC_ID_MP3;
115 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
116 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
117 case FLV_CODECID_NELLYMOSER:
118 return acodec->codec_id == AV_CODEC_ID_NELLYMOSER;
119 case FLV_CODECID_PCM_MULAW:
120 return acodec->sample_rate == 8000 &&
121 acodec->codec_id == AV_CODEC_ID_PCM_MULAW;
122 case FLV_CODECID_PCM_ALAW:
123 return acodec->sample_rate = 8000 &&
124 acodec->codec_id == AV_CODEC_ID_PCM_ALAW;
126 return acodec->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
132 static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, AVCodecContext *acodec, int flv_codecid) {
133 switch(flv_codecid) {
134 //no distinction between S16 and S8 PCM codec flags
135 case FLV_CODECID_PCM:
136 acodec->codec_id = acodec->bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 :
138 AV_CODEC_ID_PCM_S16BE;
140 AV_CODEC_ID_PCM_S16LE;
143 case FLV_CODECID_PCM_LE:
144 acodec->codec_id = acodec->bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 : AV_CODEC_ID_PCM_S16LE; break;
145 case FLV_CODECID_AAC : acodec->codec_id = AV_CODEC_ID_AAC; break;
146 case FLV_CODECID_ADPCM: acodec->codec_id = AV_CODEC_ID_ADPCM_SWF; break;
147 case FLV_CODECID_SPEEX:
148 acodec->codec_id = AV_CODEC_ID_SPEEX;
149 acodec->sample_rate = 16000;
151 case FLV_CODECID_MP3 : acodec->codec_id = AV_CODEC_ID_MP3 ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
152 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
153 acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
154 acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
156 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
157 acodec->sample_rate = 16000;
158 acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
160 case FLV_CODECID_NELLYMOSER:
161 acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
163 case FLV_CODECID_PCM_MULAW:
164 acodec->sample_rate = 8000;
165 acodec->codec_id = AV_CODEC_ID_PCM_MULAW;
167 case FLV_CODECID_PCM_ALAW:
168 acodec->sample_rate = 8000;
169 acodec->codec_id = AV_CODEC_ID_PCM_ALAW;
172 av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
173 acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
177 static int flv_same_video_codec(AVCodecContext *vcodec, int flags)
179 int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
181 if (!vcodec->codec_id && !vcodec->codec_tag)
184 switch (flv_codecid) {
185 case FLV_CODECID_H263:
186 return vcodec->codec_id == AV_CODEC_ID_FLV1;
187 case FLV_CODECID_SCREEN:
188 return vcodec->codec_id == AV_CODEC_ID_FLASHSV;
189 case FLV_CODECID_SCREEN2:
190 return vcodec->codec_id == AV_CODEC_ID_FLASHSV2;
191 case FLV_CODECID_VP6:
192 return vcodec->codec_id == AV_CODEC_ID_VP6F;
193 case FLV_CODECID_VP6A:
194 return vcodec->codec_id == AV_CODEC_ID_VP6A;
195 case FLV_CODECID_H264:
196 return vcodec->codec_id == AV_CODEC_ID_H264;
198 return vcodec->codec_tag == flv_codecid;
204 static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid, int read) {
205 AVCodecContext *vcodec = vstream->codec;
206 switch(flv_codecid) {
207 case FLV_CODECID_H263 : vcodec->codec_id = AV_CODEC_ID_FLV1 ; break;
208 case FLV_CODECID_SCREEN: vcodec->codec_id = AV_CODEC_ID_FLASHSV; break;
209 case FLV_CODECID_SCREEN2: vcodec->codec_id = AV_CODEC_ID_FLASHSV2; break;
210 case FLV_CODECID_VP6 : vcodec->codec_id = AV_CODEC_ID_VP6F ;
211 case FLV_CODECID_VP6A :
212 if(flv_codecid == FLV_CODECID_VP6A)
213 vcodec->codec_id = AV_CODEC_ID_VP6A;
215 if (vcodec->extradata_size != 1) {
216 vcodec->extradata = av_malloc(1);
217 if (vcodec->extradata)
218 vcodec->extradata_size = 1;
220 if (vcodec->extradata)
221 vcodec->extradata[0] = avio_r8(s->pb);
225 return 1; // 1 byte body size adjustment for flv_read_packet()
226 case FLV_CODECID_H264:
227 vcodec->codec_id = AV_CODEC_ID_H264;
228 return 3; // not 4, reading packet type will consume one byte
230 av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
231 vcodec->codec_tag = flv_codecid;
237 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize) {
238 int length = avio_rb16(ioc);
239 if(length >= buffsize) {
240 avio_skip(ioc, length);
244 avio_read(ioc, buffer, length);
246 buffer[length] = '\0';
251 static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, AVStream *vstream, int64_t max_pos) {
252 FLVContext *flv = s->priv_data;
253 unsigned int arraylen = 0, timeslen = 0, fileposlen = 0, i;
256 int64_t *times = NULL;
257 int64_t *filepositions = NULL;
258 int ret = AVERROR(ENOSYS);
259 int64_t initial_pos = avio_tell(ioc);
261 if (s->flags & AVFMT_FLAG_IGNIDX)
264 while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
265 int64_t* current_array;
267 // Expect array object in context
268 if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
271 arraylen = avio_rb32(ioc);
276 * Expect only 'times' or 'filepositions' sub-arrays in other case refuse to use such metadata
279 if (!strcmp(KEYFRAMES_TIMESTAMP_TAG, str_val) && !times) {
280 if (!(times = av_mallocz(sizeof(*times) * arraylen))) {
281 ret = AVERROR(ENOMEM);
285 current_array = times;
286 } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) && !filepositions) {
287 if (!(filepositions = av_mallocz(sizeof(*filepositions) * arraylen))) {
288 ret = AVERROR(ENOMEM);
291 fileposlen = arraylen;
292 current_array = filepositions;
293 } else // unexpected metatag inside keyframes, will not use such metadata for indexing
296 for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
297 if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
299 num_val = av_int2double(avio_rb64(ioc));
300 current_array[i] = num_val;
302 if (times && filepositions) {
303 // All done, exiting at a position allowing amf_parse_object
304 // to finish parsing the object
310 if (!ret && timeslen == fileposlen) {
311 for (i = 0; i < fileposlen; i++) {
312 av_add_index_entry(vstream, filepositions[i], times[i]*1000,
313 0, 0, AVINDEX_KEYFRAME);
315 flv->validate_index[i].pos = filepositions[i];
316 flv->validate_index[i].dts = times[i] * 1000;
317 flv->validate_count = i + 1;
321 av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
325 av_freep(&filepositions);
326 // If we got unexpected data, but successfully reset back to
327 // the start pos, the caller can continue parsing
328 if (ret < 0 && avio_seek(ioc, initial_pos, SEEK_SET) > 0)
333 static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
334 AVCodecContext *acodec, *vcodec;
335 FLVContext *flv = s->priv_data;
337 AMFDataType amf_type;
344 amf_type = avio_r8(ioc);
347 case AMF_DATA_TYPE_NUMBER:
348 num_val = av_int2double(avio_rb64(ioc)); break;
349 case AMF_DATA_TYPE_BOOL:
350 num_val = avio_r8(ioc); break;
351 case AMF_DATA_TYPE_STRING:
352 if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
355 case AMF_DATA_TYPE_OBJECT:
356 if ((vstream || astream) && key && !strcmp(KEYFRAMES_TAG, key) && depth == 1)
357 if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
361 while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
362 if (amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
363 return -1; //if we couldn't skip, bomb out.
365 if(avio_r8(ioc) != AMF_END_OF_OBJECT)
368 case AMF_DATA_TYPE_NULL:
369 case AMF_DATA_TYPE_UNDEFINED:
370 case AMF_DATA_TYPE_UNSUPPORTED:
371 break; //these take up no additional space
372 case AMF_DATA_TYPE_MIXEDARRAY:
373 avio_skip(ioc, 4); //skip 32-bit max array index
374 while(avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
375 //this is the only case in which we would want a nested parse to not skip over the object
376 if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
379 if(avio_r8(ioc) != AMF_END_OF_OBJECT)
382 case AMF_DATA_TYPE_ARRAY: {
383 unsigned int arraylen, i;
385 arraylen = avio_rb32(ioc);
386 for(i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
387 if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
388 return -1; //if we couldn't skip, bomb out.
392 case AMF_DATA_TYPE_DATE:
393 avio_skip(ioc, 8 + 2); //timestamp (double) and UTC offset (int16)
395 default: //unsupported type, we couldn't skip
399 if(depth == 1 && key) { //only look for metadata values when we are not nested and key != NULL
400 acodec = astream ? astream->codec : NULL;
401 vcodec = vstream ? vstream->codec : NULL;
403 if (amf_type == AMF_DATA_TYPE_NUMBER || amf_type == AMF_DATA_TYPE_BOOL) {
404 if (!strcmp(key, "duration"))
405 s->duration = num_val * AV_TIME_BASE;
406 else if (!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
407 vcodec->bit_rate = num_val * 1024.0;
408 else if (!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
409 acodec->bit_rate = num_val * 1024.0;
410 else if (!strcmp(key, "datastream")) {
411 AVStream *st = create_stream(s, AVMEDIA_TYPE_DATA);
413 return AVERROR(ENOMEM);
414 st->codec->codec_id = AV_CODEC_ID_TEXT;
415 } else if (flv->trust_metadata) {
416 if (!strcmp(key, "videocodecid") && vcodec) {
417 flv_set_video_codec(s, vstream, num_val, 0);
419 if (!strcmp(key, "audiocodecid") && acodec) {
420 int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
421 flv_set_audio_codec(s, astream, acodec, id);
423 if (!strcmp(key, "audiosamplerate") && acodec) {
424 acodec->sample_rate = num_val;
425 } else if (!strcmp(key, "audiosamplesize") && acodec) {
426 acodec->bits_per_coded_sample = num_val;
427 } else if (!strcmp(key, "stereo") && acodec) {
428 acodec->channels = num_val + 1;
429 acodec->channel_layout = acodec->channels == 2 ?
430 AV_CH_LAYOUT_STEREO :
433 if (!strcmp(key, "width") && vcodec) {
434 vcodec->width = num_val;
436 if (!strcmp(key, "height") && vcodec) {
437 vcodec->height = num_val;
442 if (!strcmp(key, "duration") ||
443 !strcmp(key, "filesize") ||
444 !strcmp(key, "width") ||
445 !strcmp(key, "height") ||
446 !strcmp(key, "videodatarate") ||
447 !strcmp(key, "framerate") ||
448 !strcmp(key, "videocodecid") ||
449 !strcmp(key, "audiodatarate") ||
450 !strcmp(key, "audiosamplerate") ||
451 !strcmp(key, "audiosamplesize") ||
452 !strcmp(key, "stereo") ||
453 !strcmp(key, "audiocodecid"))
456 if(amf_type == AMF_DATA_TYPE_BOOL) {
457 av_strlcpy(str_val, num_val > 0 ? "true" : "false", sizeof(str_val));
458 av_dict_set(&s->metadata, key, str_val, 0);
459 } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
460 snprintf(str_val, sizeof(str_val), "%.f", num_val);
461 av_dict_set(&s->metadata, key, str_val, 0);
462 } else if (amf_type == AMF_DATA_TYPE_STRING)
463 av_dict_set(&s->metadata, key, str_val, 0);
469 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
471 AVStream *stream, *astream, *vstream;
474 char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
480 //first object needs to be "onMetaData" string
482 if (type != AMF_DATA_TYPE_STRING ||
483 amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
486 if (!strcmp(buffer, "onTextData"))
489 if (strcmp(buffer, "onMetaData"))
492 //find the streams now so that amf_parse_object doesn't need to do the lookup every time it is called.
493 for(i = 0; i < s->nb_streams; i++) {
494 stream = s->streams[i];
495 if (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) astream = stream;
496 else if(stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) vstream = stream;
499 //parse the second object (we want a mixed array)
500 if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
506 static int flv_read_header(AVFormatContext *s)
511 flags = avio_r8(s->pb);
512 /* old flvtool cleared this field */
513 /* FIXME: better fix needed */
515 flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
516 av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
519 s->ctx_flags |= AVFMTCTX_NOHEADER;
521 if(flags & FLV_HEADER_FLAG_HASVIDEO){
522 if(!create_stream(s, AVMEDIA_TYPE_VIDEO))
523 return AVERROR(ENOMEM);
525 if(flags & FLV_HEADER_FLAG_HASAUDIO){
526 if(!create_stream(s, AVMEDIA_TYPE_AUDIO))
527 return AVERROR(ENOMEM);
530 offset = avio_rb32(s->pb);
531 avio_seek(s->pb, offset, SEEK_SET);
539 static int flv_read_close(AVFormatContext *s)
541 FLVContext *flv = s->priv_data;
542 av_freep(&flv->new_extradata[0]);
543 av_freep(&flv->new_extradata[1]);
547 static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
549 av_free(st->codec->extradata);
550 st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
551 if (!st->codec->extradata)
552 return AVERROR(ENOMEM);
553 st->codec->extradata_size = size;
554 avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
558 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
561 av_free(flv->new_extradata[stream]);
562 flv->new_extradata[stream] = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
563 if (!flv->new_extradata[stream])
564 return AVERROR(ENOMEM);
565 flv->new_extradata_size[stream] = size;
566 avio_read(pb, flv->new_extradata[stream], size);
570 static void clear_index_entries(AVFormatContext *s, int64_t pos)
573 av_log(s, AV_LOG_WARNING, "Found invalid index entries, clearing the index.\n");
574 for (i = 0; i < s->nb_streams; i++) {
575 AVStream *st = s->streams[i];
576 /* Remove all index entries that point to >= pos */
578 for (j = 0; j < st->nb_index_entries; j++) {
579 if (st->index_entries[j].pos < pos)
580 st->index_entries[out++] = st->index_entries[j];
582 st->nb_index_entries = out;
587 static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
588 int64_t dts, int64_t next)
590 int ret = AVERROR_INVALIDDATA, i;
591 AVIOContext *pb = s->pb;
598 if (type == AMF_DATA_TYPE_MIXEDARRAY)
599 avio_seek(pb, 4, SEEK_CUR);
600 else if (type != AMF_DATA_TYPE_OBJECT)
603 amf_get_string(pb, buf, sizeof(buf));
604 if (strcmp(buf, "type") || avio_r8(pb) != AMF_DATA_TYPE_STRING)
607 amf_get_string(pb, buf, sizeof(buf));
608 //FIXME parse it as codec_id
609 amf_get_string(pb, buf, sizeof(buf));
610 if (strcmp(buf, "text") || avio_r8(pb) != AMF_DATA_TYPE_STRING)
613 length = avio_rb16(pb);
614 ret = av_get_packet(s->pb, pkt, length);
620 for (i = 0; i < s->nb_streams; i++) {
622 if (st->codec->codec_type == AVMEDIA_TYPE_DATA)
626 if (i == s->nb_streams) {
627 st = create_stream(s, AVMEDIA_TYPE_DATA);
630 st->codec->codec_id = AV_CODEC_ID_TEXT;
637 pkt->stream_index = st->index;
638 pkt->flags |= AV_PKT_FLAG_KEY;
640 avio_seek(s->pb, next + 4, SEEK_SET);
645 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
647 FLVContext *flv = s->priv_data;
648 int ret, i, type, size, flags, is_audio;
650 int64_t dts, pts = AV_NOPTS_VALUE;
651 int sample_rate = 0, channels = 0;
654 for(;;avio_skip(s->pb, 4)){ /* pkt size is repeated at end. skip it */
655 pos = avio_tell(s->pb);
656 type = avio_r8(s->pb);
657 size = avio_rb24(s->pb);
658 dts = avio_rb24(s->pb);
659 dts |= avio_r8(s->pb) << 24;
660 av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
661 if (s->pb->eof_reached)
663 avio_skip(s->pb, 3); /* stream id, always 0 */
666 if (flv->validate_next < flv->validate_count) {
667 int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
668 if (pos == validate_pos) {
669 if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
670 VALIDATE_INDEX_TS_THRESH) {
671 flv->validate_next++;
673 clear_index_entries(s, validate_pos);
674 flv->validate_count = 0;
676 } else if (pos > validate_pos) {
677 clear_index_entries(s, validate_pos);
678 flv->validate_count = 0;
685 next= size + avio_tell(s->pb);
687 if (type == FLV_TAG_TYPE_AUDIO) {
689 flags = avio_r8(s->pb);
691 } else if (type == FLV_TAG_TYPE_VIDEO) {
693 flags = avio_r8(s->pb);
695 if ((flags & 0xf0) == 0x50) /* video info / command frame */
698 if (type == FLV_TAG_TYPE_META && size > 13+1+4)
699 if (flv_read_metabody(s, next) > 0) {
700 return flv_data_packet(s, pkt, dts, next);
702 else /* skip packet */
703 av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
705 avio_seek(s->pb, next, SEEK_SET);
709 /* skip empty data packets */
713 /* now find stream */
714 for(i=0;i<s->nb_streams;i++) {
716 if (is_audio && st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
717 if (flv_same_audio_codec(st->codec, flags)) {
721 if (!is_audio && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
722 if (flv_same_video_codec(st->codec, flags)) {
727 if(i == s->nb_streams){
728 st = create_stream(s,
729 is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO);
731 av_dlog(s, "%d %X %d \n", is_audio, flags, st->discard);
732 if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))
733 ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
734 || st->discard >= AVDISCARD_ALL
736 avio_seek(s->pb, next, SEEK_SET);
739 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
740 av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
744 // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps
745 if(s->pb->seekable && (!s->duration || s->duration==AV_NOPTS_VALUE)){
747 const int64_t pos= avio_tell(s->pb);
748 const int64_t fsize= avio_size(s->pb);
749 avio_seek(s->pb, fsize-4, SEEK_SET);
750 size= avio_rb32(s->pb);
751 avio_seek(s->pb, fsize-3-size, SEEK_SET);
752 if(size == avio_rb24(s->pb) + 11){
753 uint32_t ts = avio_rb24(s->pb);
754 ts |= avio_r8(s->pb) << 24;
755 s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
757 avio_seek(s->pb, pos, SEEK_SET);
761 int bits_per_coded_sample;
762 channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
763 sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
764 bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
765 if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
766 st->codec->channels = channels;
767 st->codec->channel_layout = channels == 1 ? AV_CH_LAYOUT_MONO :
769 st->codec->sample_rate = sample_rate;
770 st->codec->bits_per_coded_sample = bits_per_coded_sample;
772 if(!st->codec->codec_id){
773 flv_set_audio_codec(s, st, st->codec, flags & FLV_AUDIO_CODECID_MASK);
774 flv->last_sample_rate = sample_rate = st->codec->sample_rate;
775 flv->last_channels = channels = st->codec->channels;
778 ctx.sample_rate = sample_rate;
779 flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK);
780 sample_rate = ctx.sample_rate;
783 size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
786 if (st->codec->codec_id == AV_CODEC_ID_AAC ||
787 st->codec->codec_id == AV_CODEC_ID_H264) {
788 int type = avio_r8(s->pb);
790 if (st->codec->codec_id == AV_CODEC_ID_H264) {
791 int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000; // sign extension
793 if (cts < 0) { // dts are wrong
795 av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
798 dts = AV_NOPTS_VALUE;
801 if (st->codec->extradata) {
802 if ((ret = flv_queue_extradata(flv, s->pb, is_audio, size)) < 0)
804 ret = AVERROR(EAGAIN);
807 if ((ret = flv_get_extradata(s, st, size)) < 0)
809 if (st->codec->codec_id == AV_CODEC_ID_AAC) {
810 MPEG4AudioConfig cfg;
811 avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
812 st->codec->extradata_size * 8, 1);
813 st->codec->channels = cfg.channels;
814 st->codec->channel_layout = 0;
815 if (cfg.ext_sample_rate)
816 st->codec->sample_rate = cfg.ext_sample_rate;
818 st->codec->sample_rate = cfg.sample_rate;
819 av_dlog(s, "mp4a config channels %d sample rate %d\n",
820 st->codec->channels, st->codec->sample_rate);
823 ret = AVERROR(EAGAIN);
828 /* skip empty data packets */
830 ret = AVERROR(EAGAIN);
834 ret= av_get_packet(s->pb, pkt, size);
838 /* note: we need to modify the packet size here to handle the last
842 pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
843 pkt->stream_index = st->index;
844 if (flv->new_extradata[is_audio]) {
845 uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
846 flv->new_extradata_size[is_audio]);
848 memcpy(side, flv->new_extradata[is_audio],
849 flv->new_extradata_size[is_audio]);
850 av_freep(&flv->new_extradata[is_audio]);
851 flv->new_extradata_size[is_audio] = 0;
854 if (is_audio && (sample_rate != flv->last_sample_rate ||
855 channels != flv->last_channels)) {
856 flv->last_sample_rate = sample_rate;
857 flv->last_channels = channels;
858 ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
861 if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
862 pkt->flags |= AV_PKT_FLAG_KEY;
869 static int flv_read_seek(AVFormatContext *s, int stream_index,
870 int64_t ts, int flags)
872 FLVContext *flv = s->priv_data;
873 flv->validate_count = 0;
874 return avio_seek_time(s->pb, stream_index, ts, flags);
877 #define OFFSET(x) offsetof(FLVContext, x)
878 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
879 static const AVOption options[] = {
880 { "flv_metadata", "Allocate streams according the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD},
884 static const AVClass class = {
885 .class_name = "flvdec",
886 .item_name = av_default_item_name,
888 .version = LIBAVUTIL_VERSION_INT,
891 AVInputFormat ff_flv_demuxer = {
893 .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
894 .priv_data_size = sizeof(FLVContext),
895 .read_probe = flv_probe,
896 .read_header = flv_read_header,
897 .read_packet = flv_read_packet,
898 .read_seek = flv_read_seek,
899 .read_close = flv_read_close,
901 .priv_class = &class,