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 #define FRAME_RATE_UNKNOWN 31415 //yes i know its beatifull
45 static int flv_read_header(AVFormatContext *s,
46 AVFormatParameters *ap)
51 s->ctx_flags |= AVFMTCTX_NOHEADER; //ok we have a header but theres no fps, codec type, sample_rate, ...
53 av_set_pts_info(s, 24, 1, 1000); /* 24 bit pts in ms */
56 flags = get_byte(&s->pb);
59 st = av_new_stream(s, 0);
62 st->codec.codec_type = CODEC_TYPE_VIDEO;
63 st->codec.codec_id = CODEC_ID_FLV1;
64 st->codec.frame_rate= FRAME_RATE_UNKNOWN;
65 // st->codec.frame_rate= ap->frame_rate;
66 // st->codec.frame_rate_base= ap->frame_rate_base;
70 st = av_new_stream(s, 1);
73 st->codec.codec_type = CODEC_TYPE_AUDIO;
74 st->codec.codec_id = CODEC_ID_MP3;
77 offset = get_be32(&s->pb);
78 url_fseek(&s->pb, offset, SEEK_SET);
83 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
85 int ret, i, type, size, pts, flags;
89 url_fskip(&s->pb, 4); /* size of previous packet */
90 type = get_byte(&s->pb);
91 size = get_be24(&s->pb);
92 pts = get_be24(&s->pb);
93 // av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, pts:%d\n", type, size, pts);
96 url_fskip(&s->pb, 4); /* reserved */
99 flags = get_byte(&s->pb);
101 if ((flags >> 4) != 2) { /* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser*/
104 } else if (type == 9) {
105 flags = get_byte(&s->pb);
107 if ((flags & 0xF) != 2) { /* 2: only format */
113 av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
114 url_fskip(&s->pb, size);
118 /* now find stream */
119 for(i=0;i<s->nb_streams;i++) {
121 if (st->id == ((type == 9) ? 0 : 1))
126 if(type == 8 && st->codec.sample_rate == 0){
127 st->codec.channels = (flags&1)+1;
128 st->codec.sample_rate = (44100<<((flags>>2)&3))>>3;
131 //guess the frame rate
132 if(type==9 && st->codec.frame_rate == FRAME_RATE_UNKNOWN && pts){
133 st->codec.frame_rate_base=1;
134 st->codec.frame_rate= (1000 + pts/2)/pts;
137 if (av_new_packet(pkt, size) < 0)
140 ret = get_buffer(&s->pb, pkt->data, size);
145 /* note: we need to modify the packet size here to handle the last
149 pkt->stream_index = st->index;
153 static int flv_read_close(AVFormatContext *s)
158 AVInputFormat flv_iformat = {
167 .value = CODEC_ID_FLV1,
170 int flvdec_init(void)
172 av_register_input_format(&flv_iformat);