3 * Copyright (c) 2003 The FFmpeg Project.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 static int flv_probe(AVProbeData *p)
28 if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V') {
34 static int flv_read_header(AVFormatContext *s,
35 AVFormatParameters *ap)
37 int offset, flags, size;
39 s->ctx_flags |= AVFMTCTX_NOHEADER; //ok we have a header but theres no fps, codec type, sample_rate, ...
42 flags = get_byte(&s->pb);
44 offset = get_be32(&s->pb);
46 if(!url_is_streamed(&s->pb)){
47 const int fsize= url_fsize(&s->pb);
48 url_fseek(&s->pb, fsize-4, SEEK_SET);
49 size= get_be32(&s->pb);
50 url_fseek(&s->pb, fsize-3-size, SEEK_SET);
51 if(size == get_be24(&s->pb) + 11){
52 s->duration= get_be24(&s->pb) * (int64_t)AV_TIME_BASE / 1000;
56 url_fseek(&s->pb, offset, SEEK_SET);
61 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
63 int ret, i, type, size, pts, flags, is_audio, next;
67 url_fskip(&s->pb, 4); /* size of previous packet */
68 type = get_byte(&s->pb);
69 size = get_be24(&s->pb);
70 pts = get_be24(&s->pb);
71 // av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, pts:%d\n", type, size, pts);
74 url_fskip(&s->pb, 4); /* reserved */
80 next= size + url_ftell(&s->pb);
84 flags = get_byte(&s->pb);
85 } else if (type == 9) {
87 flags = get_byte(&s->pb);
88 } else if (type == 18 && size > 13+1+4) {
89 url_fskip(&s->pb, 13); //onMetaData blah
90 if(get_byte(&s->pb) == 8){
93 while(url_ftell(&s->pb) + 5 < next){
98 len= get_be16(&s->pb);
99 if(len >= sizeof(tmp) || !len)
101 get_buffer(&s->pb, tmp, len);
104 type= get_byte(&s->pb);
106 d= av_int2dbl(get_be64(&s->pb));
108 len= get_be16(&s->pb);
109 if(len >= sizeof(tmp))
111 url_fskip(&s->pb, len);
116 d= av_int2dbl(get_be64(&s->pb));
120 if(!strcmp(tmp, "duration")){
121 s->duration = d*AV_TIME_BASE;
122 }else if(!strcmp(tmp, "videodatarate")){
123 }else if(!strcmp(tmp, "audiodatarate")){
126 url_fseek(&s->pb, next, SEEK_SET);
130 av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
131 url_fseek(&s->pb, next, SEEK_SET);
135 /* now find stream */
136 for(i=0;i<s->nb_streams;i++) {
138 if (st->id == is_audio)
141 if(i == s->nb_streams){
142 st = av_new_stream(s, is_audio);
144 return AVERROR_NOMEM;
146 av_set_pts_info(st, 24, 1, 1000); /* 24 bit pts in ms */
147 st->codec->time_base= (AVRational){1,1000};
149 // av_log(NULL, AV_LOG_DEBUG, "%d %X %d \n", is_audio, flags, st->discard);
150 if( (st->discard >= AVDISCARD_NONKEY && !((flags >> 4)==1 || is_audio))
151 ||(st->discard >= AVDISCARD_BIDIR && ((flags >> 4)==3 && !is_audio))
152 || st->discard >= AVDISCARD_ALL
154 url_fseek(&s->pb, next, SEEK_SET);
161 if(st->codec->sample_rate == 0){
162 st->codec->codec_type = CODEC_TYPE_AUDIO;
163 st->codec->channels = (flags&1)+1;
164 if((flags >> 4) == 5)
165 st->codec->sample_rate= 8000;
167 st->codec->sample_rate = (44100<<((flags>>2)&3))>>3;
168 switch(flags >> 4){/* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser*/
169 case 0: if (flags&2) st->codec->codec_id = CODEC_ID_PCM_S16BE;
170 else st->codec->codec_id = CODEC_ID_PCM_S8; break;
171 case 1: st->codec->codec_id = CODEC_ID_ADPCM_SWF; break;
172 case 2: st->codec->codec_id = CODEC_ID_MP3; break;
173 // this is not listed at FLV but at SWF, strange...
174 case 3: if (flags&2) st->codec->codec_id = CODEC_ID_PCM_S16LE;
175 else st->codec->codec_id = CODEC_ID_PCM_S8; break;
177 av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flags >> 4);
178 st->codec->codec_tag= (flags >> 4);
180 st->codec->bits_per_sample = (flags & 2) ? 16 : 8;
183 st->codec->codec_type = CODEC_TYPE_VIDEO;
185 case 2: st->codec->codec_id = CODEC_ID_FLV1; break;
187 av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flags & 0xf);
188 st->codec->codec_tag= flags & 0xF;
192 ret= av_get_packet(&s->pb, pkt, size - 1);
196 /* note: we need to modify the packet size here to handle the last
200 pkt->stream_index = st->index;
202 if (is_audio || ((flags >> 4)==1))
203 pkt->flags |= PKT_FLAG_KEY;
208 static int flv_read_close(AVFormatContext *s)
213 AVInputFormat flv_iformat = {
222 .value = CODEC_ID_FLV1,
225 int flvdec_init(void)
227 av_register_input_format(&flv_iformat);