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]->codec.extradata);
213 av_freep(&s->streams[i]);
221 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
223 AVIContext *avi = s->priv_data;
224 ByteIOContext *pb = &s->pb;
225 int n, d[8], size, i;
227 memset(d, -1, sizeof(int)*8);
229 for(i=url_ftell(pb); (!url_feof(pb)) && i < avi->movi_end; i++){
236 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
239 n= (d[2] - '0') * 10 + (d[3] - '0');
240 if( d[2] >= '0' && d[2] <= '9'
241 && d[3] >= '0' && d[3] <= '9'
242 && d[0] == 'i' && d[1] == 'x'
244 && i + size <= avi->movi_end){
250 n= (d[0] - '0') * 10 + (d[1] - '0');
251 if( d[0] >= '0' && d[0] <= '9'
252 && d[1] >= '0' && d[1] <= '9'
253 &&((d[2] == 'd' && d[3] == 'c') || (d[2] == 'w' && d[3] == 'b'))
255 && i + size <= avi->movi_end){
257 av_new_packet(pkt, size);
258 pkt->stream_index = n;
260 get_buffer(pb, pkt->data, pkt->size);
272 static int avi_read_close(AVFormatContext *s)
277 static int avi_probe(AVProbeData *p)
279 /* check file header */
280 if (p->buf_size <= 32)
282 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
283 p->buf[2] == 'F' && p->buf[3] == 'F' &&
284 p->buf[8] == 'A' && p->buf[9] == 'V' &&
285 p->buf[10] == 'I' && p->buf[11] == ' ')
286 return AVPROBE_SCORE_MAX;
291 static AVInputFormat avi_iformat = {
301 int avidec_init(void)
303 av_register_input_format(&avi_iformat);