3 * Copyright (c) 2001 Fabrice Bellard.
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
24 typedef struct AVIIndex {
26 unsigned int flags, pos, len;
27 struct AVIIndex *next;
33 AVIIndex *first, *last;
37 static void print_tag(const char *str, unsigned int tag, int size)
39 printf("%s: tag=%c%c%c%c size=0x%x\n",
48 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
50 AVIContext *avi = s->priv_data;
51 ByteIOContext *pb = &s->pb;
53 int codec_type, stream_index, frame_period, bit_rate, scale, rate;
58 /* check RIFF header */
61 if (tag != MKTAG('R', 'I', 'F', 'F'))
63 get_le32(pb); /* file size */
65 if (tag != MKTAG('A', 'V', 'I', ' '))
78 print_tag("tag", tag, size);
82 case MKTAG('L', 'I', 'S', 'T'):
83 /* ignored, except when start of video packets */
86 print_tag("list", tag1, 0);
88 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
89 avi->movi_end = url_ftell(pb) + size - 4;
91 printf("movi end=%Lx\n", avi->movi_end);
96 case MKTAG('a', 'v', 'i', 'h'):
98 /* using frame_period is bad idea */
99 frame_period = get_le32(pb);
100 bit_rate = get_le32(pb) * 8;
101 url_fskip(pb, 4 * 4);
102 s->nb_streams = get_le32(pb);
103 for(i=0;i<s->nb_streams;i++) {
104 AVStream *st = av_mallocz(sizeof(AVStream));
107 avcodec_get_context_defaults(&st->codec);
111 url_fskip(pb, size - 7 * 4);
113 case MKTAG('s', 't', 'r', 'h'):
118 case MKTAG('v', 'i', 'd', 's'):
119 codec_type = CODEC_TYPE_VIDEO;
121 if (stream_index >= s->nb_streams) {
122 url_fskip(pb, size - 4);
126 st = s->streams[stream_index];
128 get_le32(pb); /* codec tag */
129 get_le32(pb); /* flags */
130 get_le16(pb); /* priority */
131 get_le16(pb); /* language */
132 get_le32(pb); /* XXX: initial frame ? */
133 scale= get_le32(pb); /* scale */
134 rate= get_le32(pb); /* rate */
137 st->codec.frame_rate = rate;
138 st->codec.frame_rate_base= scale;
139 }else if(frame_period){
140 st->codec.frame_rate = 1000000;
141 st->codec.frame_rate_base= frame_period;
143 st->codec.frame_rate = 25;
144 st->codec.frame_rate_base = 1;
147 url_fskip(pb, size - 7 * 4);
149 case MKTAG('a', 'u', 'd', 's'):
150 codec_type = CODEC_TYPE_AUDIO;
151 /* nothing really useful */
152 url_fskip(pb, size - 4);
158 case MKTAG('s', 't', 'r', 'f'):
160 if (stream_index >= s->nb_streams) {
163 st = s->streams[stream_index];
165 case CODEC_TYPE_VIDEO:
166 get_le32(pb); /* size */
167 st->codec.width = get_le32(pb);
168 st->codec.height = get_le32(pb);
169 get_le16(pb); /* panes */
170 st->codec.bits_per_sample= get_le16(pb); /* depth */
172 get_le32(pb); /* ImageSize */
173 get_le32(pb); /* XPelsPerMeter */
174 get_le32(pb); /* YPelsPerMeter */
175 get_le32(pb); /* ClrUsed */
176 get_le32(pb); /* ClrImportant */
178 st->codec.extradata_size= size - 10*4;
179 st->codec.extradata= av_malloc(st->codec.extradata_size); //FIXME where should we free this?
180 get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
182 if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
186 print_tag("video", tag1, 0);
188 st->codec.codec_type = CODEC_TYPE_VIDEO;
189 st->codec.codec_tag = tag1;
190 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
191 // url_fskip(pb, size - 5 * 4);
193 case CODEC_TYPE_AUDIO:
194 get_wav_header(pb, &st->codec, size);
195 if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
212 /* check stream number */
213 if (stream_index != s->nb_streams - 1) {
215 for(i=0;i<s->nb_streams;i++) {
216 av_freep(&s->streams[i]->codec.extradata);
217 av_freep(&s->streams[i]);
225 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
227 AVIContext *avi = s->priv_data;
228 ByteIOContext *pb = &s->pb;
229 int n, d[8], size, i;
231 memset(d, -1, sizeof(int)*8);
233 for(i=url_ftell(pb); (!url_feof(pb)) && i < avi->movi_end; i++){
240 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
243 n= (d[2] - '0') * 10 + (d[3] - '0');
244 if( d[2] >= '0' && d[2] <= '9'
245 && d[3] >= '0' && d[3] <= '9'
246 && d[0] == 'i' && d[1] == 'x'
248 && i + size <= avi->movi_end){
254 n= (d[0] - '0') * 10 + (d[1] - '0');
255 if( d[0] >= '0' && d[0] <= '9'
256 && d[1] >= '0' && d[1] <= '9'
257 &&((d[2] == 'd' && d[3] == 'c') || (d[2] == 'w' && d[3] == 'b'))
259 && i + size <= avi->movi_end){
261 av_new_packet(pkt, size);
262 pkt->stream_index = n;
264 get_buffer(pb, pkt->data, pkt->size);
276 static int avi_read_close(AVFormatContext *s)
281 static int avi_probe(AVProbeData *p)
283 /* check file header */
284 if (p->buf_size <= 32)
286 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
287 p->buf[2] == 'F' && p->buf[3] == 'F' &&
288 p->buf[8] == 'A' && p->buf[9] == 'V' &&
289 p->buf[10] == 'I' && p->buf[11] == ' ')
290 return AVPROBE_SCORE_MAX;
295 static AVInputFormat avi_iformat = {
305 int avidec_init(void)
307 av_register_input_format(&avi_iformat);