changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext...
[libav.git] / libavformat / flvdec.c
CommitLineData
d4f5d74a
GM
1/*
2 * FLV encoder.
3 * Copyright (c) 2003 The FFmpeg Project.
4 *
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.
9 *
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.
14 *
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
18 */
19#include "avformat.h"
20
21unsigned int get_be24(ByteIOContext *s)
22{
23 unsigned int val;
24 val = get_byte(s) << 16;
25 val |= get_byte(s) << 8;
26 val |= get_byte(s);
27 return val;
28}
29
30static int flv_probe(AVProbeData *p)
31{
32 const uint8_t *d;
33
34 if (p->buf_size < 6)
35 return 0;
36 d = p->buf;
37 if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V') {
38 return 50;
39 }
40 return 0;
41}
42
43static int flv_read_header(AVFormatContext *s,
44 AVFormatParameters *ap)
45{
46 int offset, flags;
bb01a3f0
MN
47
48 s->ctx_flags |= AVFMTCTX_NOHEADER; //ok we have a header but theres no fps, codec type, sample_rate, ...
d4f5d74a 49
d4f5d74a
GM
50 url_fskip(&s->pb, 4);
51 flags = get_byte(&s->pb);
52
d4f5d74a
GM
53 offset = get_be32(&s->pb);
54 url_fseek(&s->pb, offset, SEEK_SET);
55
56 return 0;
57}
58
59static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
60{
068f2a22 61 int ret, i, type, size, pts, flags, is_audio;
923bd441 62 AVStream *st = NULL;
d4f5d74a 63
068f2a22 64 for(;;){
d4f5d74a
GM
65 url_fskip(&s->pb, 4); /* size of previous packet */
66 type = get_byte(&s->pb);
67 size = get_be24(&s->pb);
68 pts = get_be24(&s->pb);
92a26775 69// av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, pts:%d\n", type, size, pts);
d4f5d74a 70 if (url_feof(&s->pb))
0bd586c5 71 return AVERROR_IO;
d4f5d74a
GM
72 url_fskip(&s->pb, 4); /* reserved */
73 flags = 0;
068f2a22
MN
74
75 if(size == 0)
76 continue;
77
d4f5d74a 78 if (type == 8) {
068f2a22 79 is_audio=1;
d4f5d74a
GM
80 flags = get_byte(&s->pb);
81 size--;
d4f5d74a 82 } else if (type == 9) {
068f2a22 83 is_audio=0;
d4f5d74a
GM
84 flags = get_byte(&s->pb);
85 size--;
d4f5d74a 86 } else {
d4f5d74a 87 /* skip packet */
bc874dae 88 av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
d4f5d74a 89 url_fskip(&s->pb, size);
068f2a22 90 continue;
d4f5d74a
GM
91 }
92
93 /* now find stream */
94 for(i=0;i<s->nb_streams;i++) {
95 st = s->streams[i];
068f2a22
MN
96 if (st->id == is_audio)
97 break;
d4f5d74a 98 }
068f2a22
MN
99 if(i == s->nb_streams){
100 st = av_new_stream(s, is_audio);
101 if (!st)
102 return AVERROR_NOMEM;
9ee91c2f
MN
103
104 av_set_pts_info(st, 24, 1, 1000); /* 24 bit pts in ms */
01f4895c 105 st->codec->time_base= (AVRational){1,1000};
6ed08157 106 }
f3356e9c
MN
107// av_log(NULL, AV_LOG_DEBUG, "%d %X %d \n", is_audio, flags, st->discard);
108 if( (st->discard >= AVDISCARD_NONKEY && !((flags >> 4)==1 || is_audio))
109 ||(st->discard >= AVDISCARD_BIDIR && ((flags >> 4)==3 && !is_audio))
110 || st->discard >= AVDISCARD_ALL
111 ){
b9866ebc
MN
112 url_fskip(&s->pb, size);
113 continue;
114 }
068f2a22
MN
115 break;
116 }
117
118 if(is_audio){
01f4895c
MN
119 if(st->codec->sample_rate == 0){
120 st->codec->codec_type = CODEC_TYPE_AUDIO;
121 st->codec->channels = (flags&1)+1;
068f2a22 122 if((flags >> 4) == 5)
01f4895c 123 st->codec->sample_rate= 8000;
068f2a22 124 else
01f4895c 125 st->codec->sample_rate = (44100<<((flags>>2)&3))>>3;
068f2a22 126 switch(flags >> 4){/* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser*/
01f4895c
MN
127 case 0: if (flags&2) st->codec->codec_id = CODEC_ID_PCM_S16BE;
128 else st->codec->codec_id = CODEC_ID_PCM_S8; break;
129 case 1: st->codec->codec_id = CODEC_ID_ADPCM_SWF; break;
130 case 2: st->codec->codec_id = CODEC_ID_MP3; break;
923bd441 131 // this is not listed at FLV but at SWF, strange...
01f4895c
MN
132 case 3: if (flags&2) st->codec->codec_id = CODEC_ID_PCM_S16LE;
133 else st->codec->codec_id = CODEC_ID_PCM_S8; break;
068f2a22 134 default:
ae26a016 135 av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flags >> 4);
01f4895c 136 st->codec->codec_tag= (flags >> 4);
068f2a22 137 }
01f4895c 138 st->codec->bits_per_sample = (flags & 2) ? 16 : 8;
068f2a22
MN
139 }
140 }else{
01f4895c 141 st->codec->codec_type = CODEC_TYPE_VIDEO;
068f2a22 142 switch(flags & 0xF){
01f4895c 143 case 2: st->codec->codec_id = CODEC_ID_FLV1; break;
068f2a22 144 default:
ae26a016 145 av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flags & 0xf);
01f4895c 146 st->codec->codec_tag= flags & 0xF;
068f2a22 147 }
bb01a3f0
MN
148 }
149
2692067a 150 ret= av_get_packet(&s->pb, pkt, size);
d4f5d74a 151 if (ret <= 0) {
0bd586c5 152 return AVERROR_IO;
d4f5d74a
GM
153 }
154 /* note: we need to modify the packet size here to handle the last
155 packet */
156 pkt->size = ret;
157 pkt->pts = pts;
158 pkt->stream_index = st->index;
d08f4bcb 159
f3356e9c 160 if (is_audio || ((flags >> 4)==1))
d08f4bcb
AB
161 pkt->flags |= PKT_FLAG_KEY;
162
d4f5d74a
GM
163 return ret;
164}
165
166static int flv_read_close(AVFormatContext *s)
167{
168 return 0;
169}
170
171AVInputFormat flv_iformat = {
172 "flv",
173 "flv format",
174 0,
175 flv_probe,
176 flv_read_header,
177 flv_read_packet,
178 flv_read_close,
179 .extensions = "flv",
180 .value = CODEC_ID_FLV1,
181};
182
183int flvdec_init(void)
184{
185 av_register_input_format(&flv_iformat);
186 return 0;
187}