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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 unsigned int get_be24(ByteIOContext *s)
24 val = get_byte(s) << 16;
25 val |= get_byte(s) << 8;
30 static int flv_probe(AVProbeData *p)
37 if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V') {
43 static int flv_read_header(AVFormatContext *s,
44 AVFormatParameters *ap)
49 s->ctx_flags |= AVFMTCTX_NOHEADER; //ok we have a header but theres no fps, codec type, sample_rate, ...
52 flags = get_byte(&s->pb);
54 offset = get_be32(&s->pb);
55 url_fseek(&s->pb, offset, SEEK_SET);
60 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
62 int ret, i, type, size, pts, flags, is_audio;
66 url_fskip(&s->pb, 4); /* size of previous packet */
67 type = get_byte(&s->pb);
68 size = get_be24(&s->pb);
69 pts = get_be24(&s->pb);
70 // av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, pts:%d\n", type, size, pts);
73 url_fskip(&s->pb, 4); /* reserved */
81 flags = get_byte(&s->pb);
83 } else if (type == 9) {
85 flags = get_byte(&s->pb);
89 av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
90 url_fskip(&s->pb, size);
95 for(i=0;i<s->nb_streams;i++) {
97 if (st->id == is_audio)
100 if(i == s->nb_streams){
101 st = av_new_stream(s, is_audio);
103 return AVERROR_NOMEM;
105 av_set_pts_info(st, 24, 1, 1000); /* 24 bit pts in ms */
106 st->codec.frame_rate_base= 1;
107 st->codec.frame_rate= 1000;
110 url_fskip(&s->pb, size);
117 if(st->codec.sample_rate == 0){
118 st->codec.codec_type = CODEC_TYPE_AUDIO;
119 st->codec.channels = (flags&1)+1;
120 if((flags >> 4) == 5)
121 st->codec.sample_rate= 8000;
123 st->codec.sample_rate = (44100<<((flags>>2)&3))>>3;
124 switch(flags >> 4){/* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser*/
125 case 0: if (flags&2) st->codec.codec_id = CODEC_ID_PCM_S16BE;
126 else st->codec.codec_id = CODEC_ID_PCM_S8; break;
127 case 1: st->codec.codec_id = CODEC_ID_ADPCM_SWF; break;
128 case 2: st->codec.codec_id = CODEC_ID_MP3; break;
129 // this is not listed at FLV but at SWF, strange...
130 case 3: if (flags&2) st->codec.codec_id = CODEC_ID_PCM_S16LE;
131 else st->codec.codec_id = CODEC_ID_PCM_S8; break;
133 av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flags >> 4);
134 st->codec.codec_tag= (flags >> 4);
138 st->codec.codec_type = CODEC_TYPE_VIDEO;
140 case 2: st->codec.codec_id = CODEC_ID_FLV1; break;
142 av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flags & 0xf);
143 st->codec.codec_tag= flags & 0xF;
147 if (av_new_packet(pkt, size) < 0)
150 ret = get_buffer(&s->pb, pkt->data, size);
155 /* note: we need to modify the packet size here to handle the last
159 pkt->stream_index = st->index;
161 if (!is_audio && ((flags >> 4)==1))
162 pkt->flags |= PKT_FLAG_KEY;
167 static int flv_read_close(AVFormatContext *s)
172 AVInputFormat flv_iformat = {
181 .value = CODEC_ID_FLV1,
184 int flvdec_init(void)
186 av_register_input_format(&flv_iformat);