* cleanup statics & const
[libav.git] / libav / avidec.c
CommitLineData
de6d9b64
FB
1/*
2 * AVI decoder.
19720f15 3 * Copyright (c) 2001 Fabrice Bellard.
de6d9b64 4 *
19720f15
FB
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.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
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
de6d9b64 18 */
de6d9b64
FB
19#include "avformat.h"
20#include "avi.h"
21
22//#define DEBUG
23
24typedef struct AVIIndex {
25 unsigned char tag[4];
26 unsigned int flags, pos, len;
27 struct AVIIndex *next;
28} AVIIndex;
29
30typedef struct {
31 INT64 movi_end;
32 offset_t movi_list;
33 AVIIndex *first, *last;
34} AVIContext;
35
36#ifdef DEBUG
37void print_tag(const char *str, unsigned int tag, int size)
38{
39 printf("%s: tag=%c%c%c%c size=0x%x\n",
40 str, tag & 0xff,
41 (tag >> 8) & 0xff,
42 (tag >> 16) & 0xff,
43 (tag >> 24) & 0xff,
44 size);
45}
46#endif
47
48int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
49{
c9a65ca8 50 AVIContext *avi = s->priv_data;
de6d9b64
FB
51 ByteIOContext *pb = &s->pb;
52 UINT32 tag, tag1;
53 int codec_type, stream_index, size, frame_period, bit_rate;
e095026a 54 int i;
de6d9b64
FB
55 AVStream *st;
56
de6d9b64
FB
57 /* check RIFF header */
58 tag = get_le32(pb);
59
60 if (tag != MKTAG('R', 'I', 'F', 'F'))
61 return -1;
62 get_le32(pb); /* file size */
63 tag = get_le32(pb);
64 if (tag != MKTAG('A', 'V', 'I', ' '))
65 return -1;
66
67 /* first list tag */
68 stream_index = -1;
69 codec_type = -1;
70 frame_period = 0;
71 for(;;) {
72 if (url_feof(pb))
73 goto fail;
74 tag = get_le32(pb);
75 size = get_le32(pb);
76#ifdef DEBUG
77 print_tag("tag", tag, size);
78#endif
79
80 switch(tag) {
81 case MKTAG('L', 'I', 'S', 'T'):
82 /* ignored, except when start of video packets */
83 tag1 = get_le32(pb);
84#ifdef DEBUG
85 print_tag("list", tag1, 0);
86#endif
87 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
88 avi->movi_end = url_ftell(pb) + size - 4;
89#ifdef DEBUG
90 printf("movi end=%Lx\n", avi->movi_end);
91#endif
92 goto end_of_header;
93 }
94 break;
95 case MKTAG('a', 'v', 'i', 'h'):
96 /* avi header */
97 frame_period = get_le32(pb);
98 bit_rate = get_le32(pb) * 8;
99 url_fskip(pb, 4 * 4);
100 s->nb_streams = get_le32(pb);
101 for(i=0;i<s->nb_streams;i++) {
102 AVStream *st;
1ea4f593 103 st = av_malloc(sizeof(AVStream));
de6d9b64
FB
104 if (!st)
105 goto fail;
106 memset(st, 0, sizeof(AVStream));
107 s->streams[i] = st;
108 }
109 url_fskip(pb, size - 7 * 4);
110 break;
111 case MKTAG('s', 't', 'r', 'h'):
112 /* stream header */
113 stream_index++;
114 tag1 = get_le32(pb);
115 switch(tag1) {
116 case MKTAG('v', 'i', 'd', 's'):
117 codec_type = CODEC_TYPE_VIDEO;
118 get_le32(pb); /* codec tag */
119 get_le32(pb); /* flags */
120 get_le16(pb); /* priority */
121 get_le16(pb); /* language */
122 get_le32(pb); /* XXX: initial frame ? */
123 get_le32(pb); /* scale */
124 get_le32(pb); /* rate */
125 url_fskip(pb, size - 7 * 4);
126 break;
127 case MKTAG('a', 'u', 'd', 's'):
128 codec_type = CODEC_TYPE_AUDIO;
129 /* nothing really useful */
130 url_fskip(pb, size - 4);
131 break;
132 default:
133 goto fail;
134 }
135 break;
136 case MKTAG('s', 't', 'r', 'f'):
137 /* stream header */
138 if (stream_index >= s->nb_streams) {
139 url_fskip(pb, size);
140 } else {
141 st = s->streams[stream_index];
142 switch(codec_type) {
143 case CODEC_TYPE_VIDEO:
144 get_le32(pb); /* size */
145 st->codec.width = get_le32(pb);
146 st->codec.height = get_le32(pb);
147 if (frame_period)
8be1c656 148 st->codec.frame_rate = (INT64_C(1000000) * FRAME_RATE_BASE) / frame_period;
de6d9b64
FB
149 else
150 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
151 get_le16(pb); /* panes */
152 get_le16(pb); /* depth */
153 tag1 = get_le32(pb);
154#ifdef DEBUG
155 print_tag("video", tag1, 0);
156#endif
157 st->codec.codec_type = CODEC_TYPE_VIDEO;
158 st->codec.codec_tag = tag1;
159 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
160 url_fskip(pb, size - 5 * 4);
161 break;
162 case CODEC_TYPE_AUDIO:
e095026a 163 get_wav_header(pb, &st->codec, (size >= 18));
de6d9b64
FB
164 break;
165 default:
166 url_fskip(pb, size);
167 break;
168 }
169 }
170 break;
171 default:
172 /* skip tag */
173 size += (size & 1);
174 url_fskip(pb, size);
175 break;
176 }
177 }
178 end_of_header:
179 /* check stream number */
180 if (stream_index != s->nb_streams - 1) {
181 fail:
182 for(i=0;i<s->nb_streams;i++) {
1ea4f593 183 av_freep(&s->streams[i]);
de6d9b64
FB
184 }
185 return -1;
186 }
187
188 return 0;
189}
190
191int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
192{
193 AVIContext *avi = s->priv_data;
194 ByteIOContext *pb = &s->pb;
195 int n, d1, d2, size;
196
197 find_next:
198 if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
199 return -1;
200 d1 = get_byte(pb);
201 if (d1 < '0' || d1 > '9')
202 goto find_next;
203 d2 = get_byte(pb);
204 if (d2 < '0' || d2 > '9')
205 goto find_next;
206 n = (d1 - '0') * 10 + (d2 - '0');
207
208 if (n < 0 || n >= s->nb_streams)
209 goto find_next;
210
211 d1 = get_byte(pb);
212 d2 = get_byte(pb);
213 if ((d1 != 'd' && d2 != 'c') &&
214 (d1 != 'w' && d2 != 'b'))
215 goto find_next;
216
217 size = get_le32(pb);
218 av_new_packet(pkt, size);
219 pkt->stream_index = n;
220
221 get_buffer(pb, pkt->data, pkt->size);
222
223 if (size & 1)
224 get_byte(pb);
225
226 return 0;
227}
228
229int avi_read_close(AVFormatContext *s)
230{
c9a65ca8
FB
231 return 0;
232}
233
234static int avi_probe(AVProbeData *p)
235{
236 /* check file header */
237 if (p->buf_size <= 32)
238 return 0;
239 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
240 p->buf[2] == 'F' && p->buf[3] == 'F' &&
241 p->buf[8] == 'A' && p->buf[9] == 'V' &&
242 p->buf[10] == 'I' && p->buf[11] == ' ')
243 return AVPROBE_SCORE_MAX;
244 else
245 return 0;
246}
247
248static AVInputFormat avi_iformat = {
249 "avi",
250 "avi format",
251 sizeof(AVIContext),
252 avi_probe,
253 avi_read_header,
254 avi_read_packet,
255 avi_read_close,
256};
257
258int avidec_init(void)
259{
260 av_register_input_format(&avi_iformat);
de6d9b64
FB
261 return 0;
262}