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 * (uint64_t)FRAME_RATE_BASE + scale/2) / scale;
138 else if(frame_period)
139 st->codec.frame_rate = (1000000LL * FRAME_RATE_BASE + frame_period/2) / frame_period;
141 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
143 url_fskip(pb, size - 7 * 4);
145 case MKTAG('a', 'u', 'd', 's'):
146 codec_type = CODEC_TYPE_AUDIO;
147 /* nothing really useful */
148 url_fskip(pb, size - 4);
154 case MKTAG('s', 't', 'r', 'f'):
156 if (stream_index >= s->nb_streams) {
159 st = s->streams[stream_index];
161 case CODEC_TYPE_VIDEO:
162 get_le32(pb); /* size */
163 st->codec.width = get_le32(pb);
164 st->codec.height = get_le32(pb);
165 get_le16(pb); /* panes */
166 st->codec.bits_per_sample= get_le16(pb); /* depth */
168 get_le32(pb); /* ImageSize */
169 get_le32(pb); /* XPelsPerMeter */
170 get_le32(pb); /* YPelsPerMeter */
171 get_le32(pb); /* ClrUsed */
172 get_le32(pb); /* ClrImportant */
174 st->codec.extradata_size= size - 10*4;
175 st->codec.extradata= av_malloc(st->codec.extradata_size); //FIXME where should we free this?
176 get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
178 if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
182 print_tag("video", tag1, 0);
184 st->codec.codec_type = CODEC_TYPE_VIDEO;
185 st->codec.codec_tag = tag1;
186 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
187 // url_fskip(pb, size - 5 * 4);
189 case CODEC_TYPE_AUDIO:
190 get_wav_header(pb, &st->codec, (size >= 18));
191 if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
208 /* check stream number */
209 if (stream_index != s->nb_streams - 1) {
211 for(i=0;i<s->nb_streams;i++) {
212 av_freep(&s->streams[i]);
220 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
222 AVIContext *avi = s->priv_data;
223 ByteIOContext *pb = &s->pb;
227 if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
230 if (d1 < '0' || d1 > '9')
233 if (d2 < '0' || d2 > '9')
235 n = (d1 - '0') * 10 + (d2 - '0');
237 if (n < 0 || n >= s->nb_streams)
242 if ((d1 != 'd' && d2 != 'c') &&
243 (d1 != 'w' && d2 != 'b'))
247 av_new_packet(pkt, size);
248 pkt->stream_index = n;
250 get_buffer(pb, pkt->data, pkt->size);
258 static int avi_read_close(AVFormatContext *s)
263 static int avi_probe(AVProbeData *p)
265 /* check file header */
266 if (p->buf_size <= 32)
268 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
269 p->buf[2] == 'F' && p->buf[3] == 'F' &&
270 p->buf[8] == 'A' && p->buf[9] == 'V' &&
271 p->buf[10] == 'I' && p->buf[11] == ' ')
272 return AVPROBE_SCORE_MAX;
277 static AVInputFormat avi_iformat = {
287 int avidec_init(void)
289 av_register_input_format(&avi_iformat);