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, size, frame_period, bit_rate;
57 /* check RIFF header */
60 if (tag != MKTAG('R', 'I', 'F', 'F'))
62 get_le32(pb); /* file size */
64 if (tag != MKTAG('A', 'V', 'I', ' '))
77 print_tag("tag", tag, size);
81 case MKTAG('L', 'I', 'S', 'T'):
82 /* ignored, except when start of video packets */
85 print_tag("list", tag1, 0);
87 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
88 avi->movi_end = url_ftell(pb) + size - 4;
90 printf("movi end=%Lx\n", avi->movi_end);
95 case MKTAG('a', 'v', 'i', 'h'):
97 /* using frame_period is bad idea */
98 frame_period = get_le32(pb);
99 bit_rate = get_le32(pb) * 8;
100 url_fskip(pb, 4 * 4);
101 s->nb_streams = get_le32(pb);
102 for(i=0;i<s->nb_streams;i++) {
103 AVStream *st = av_mallocz(sizeof(AVStream));
106 avcodec_get_context_defaults(&st->codec);
110 url_fskip(pb, size - 7 * 4);
112 case MKTAG('s', 't', 'r', 'h'):
117 case MKTAG('v', 'i', 'd', 's'):
118 codec_type = CODEC_TYPE_VIDEO;
119 get_le32(pb); /* codec tag */
120 get_le32(pb); /* flags */
121 get_le16(pb); /* priority */
122 get_le16(pb); /* language */
123 get_le32(pb); /* XXX: initial frame ? */
124 get_le32(pb); /* scale */
125 get_le32(pb); /* rate */
128 case MKTAG('a', 'u', 'd', 's'):
129 codec_type = CODEC_TYPE_AUDIO;
130 /* nothing really useful */
132 url_fskip(pb, size - 4);
134 case MKTAG('s', 't', 'r', 'f'):
136 if (stream_index >= s->nb_streams) {
139 st = s->streams[stream_index];
141 case CODEC_TYPE_VIDEO:
142 get_le32(pb); /* size */
143 st->codec.width = get_le32(pb);
144 st->codec.height = get_le32(pb);
146 st->codec.frame_rate = (INT64_C(1000000) * FRAME_RATE_BASE) / frame_period;
148 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
149 get_le16(pb); /* panes */
150 get_le16(pb); /* depth */
153 print_tag("video", tag1, 0);
155 st->codec.codec_type = CODEC_TYPE_VIDEO;
156 st->codec.codec_tag = tag1;
157 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
158 url_fskip(pb, size - 5 * 4);
160 case CODEC_TYPE_AUDIO:
161 get_wav_header(pb, &st->codec, (size >= 18));
162 if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
179 /* check stream number */
180 if (stream_index != s->nb_streams - 1) {
182 for(i=0;i<s->nb_streams;i++) {
183 av_freep(&s->streams[i]);
191 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
193 AVIContext *avi = s->priv_data;
194 ByteIOContext *pb = &s->pb;
198 if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
200 d1 = get_byte(pb) - '0';
201 d2 = get_byte(pb) - '0';
202 if (d1 < 0 || d1 > 9 || d2 < 0 || d2 > 9)
206 if (n < 0 || n >= s->nb_streams)
211 if ((d1 == 'd' && d2 == 'c')
212 || (d1 == 'w' && d2 == 'b'))
216 av_new_packet(pkt, size);
217 pkt->stream_index = n;
219 get_buffer(pb, pkt->data, pkt->size);
227 static int avi_read_close(AVFormatContext *s)
232 static int avi_probe(AVProbeData *p)
234 /* check file header */
235 if (p->buf_size <= 32)
237 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
238 p->buf[2] == 'F' && p->buf[3] == 'F' &&
239 p->buf[8] == 'A' && p->buf[9] == 'V' &&
240 p->buf[10] == 'I' && p->buf[11] == ' ')
241 return AVPROBE_SCORE_MAX;
246 static AVInputFormat avi_iformat = {
256 int avidec_init(void)
258 av_register_input_format(&avi_iformat);