win32 fixes
[libav.git] / libav / avidec.c
1 /*
2 * AVI decoder.
3 * Copyright (c) 2001 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 #include "avformat.h"
20 #include "avi.h"
21
22 //#define DEBUG
23
24 typedef struct AVIIndex {
25 unsigned char tag[4];
26 unsigned int flags, pos, len;
27 struct AVIIndex *next;
28 } AVIIndex;
29
30 typedef struct {
31 INT64 movi_end;
32 offset_t movi_list;
33 AVIIndex *first, *last;
34 } AVIContext;
35
36 #ifdef DEBUG
37 void 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
48 int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
49 {
50 AVIContext *avi;
51 ByteIOContext *pb = &s->pb;
52 UINT32 tag, tag1;
53 int codec_type, stream_index, size, frame_period, bit_rate;
54 int i;
55 AVStream *st;
56
57 avi = malloc(sizeof(AVIContext));
58 if (!avi)
59 return -1;
60 memset(avi, 0, sizeof(AVIContext));
61 s->priv_data = avi;
62
63 /* check RIFF header */
64 tag = get_le32(pb);
65
66 if (tag != MKTAG('R', 'I', 'F', 'F'))
67 return -1;
68 get_le32(pb); /* file size */
69 tag = get_le32(pb);
70 if (tag != MKTAG('A', 'V', 'I', ' '))
71 return -1;
72
73 /* first list tag */
74 stream_index = -1;
75 codec_type = -1;
76 frame_period = 0;
77 for(;;) {
78 if (url_feof(pb))
79 goto fail;
80 tag = get_le32(pb);
81 size = get_le32(pb);
82 #ifdef DEBUG
83 print_tag("tag", tag, size);
84 #endif
85
86 switch(tag) {
87 case MKTAG('L', 'I', 'S', 'T'):
88 /* ignored, except when start of video packets */
89 tag1 = get_le32(pb);
90 #ifdef DEBUG
91 print_tag("list", tag1, 0);
92 #endif
93 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
94 avi->movi_end = url_ftell(pb) + size - 4;
95 #ifdef DEBUG
96 printf("movi end=%Lx\n", avi->movi_end);
97 #endif
98 goto end_of_header;
99 }
100 break;
101 case MKTAG('a', 'v', 'i', 'h'):
102 /* avi header */
103 frame_period = get_le32(pb);
104 bit_rate = get_le32(pb) * 8;
105 url_fskip(pb, 4 * 4);
106 s->nb_streams = get_le32(pb);
107 for(i=0;i<s->nb_streams;i++) {
108 AVStream *st;
109 st = malloc(sizeof(AVStream));
110 if (!st)
111 goto fail;
112 memset(st, 0, sizeof(AVStream));
113 s->streams[i] = st;
114 }
115 url_fskip(pb, size - 7 * 4);
116 break;
117 case MKTAG('s', 't', 'r', 'h'):
118 /* stream header */
119 stream_index++;
120 tag1 = get_le32(pb);
121 switch(tag1) {
122 case MKTAG('v', 'i', 'd', 's'):
123 codec_type = CODEC_TYPE_VIDEO;
124 get_le32(pb); /* codec tag */
125 get_le32(pb); /* flags */
126 get_le16(pb); /* priority */
127 get_le16(pb); /* language */
128 get_le32(pb); /* XXX: initial frame ? */
129 get_le32(pb); /* scale */
130 get_le32(pb); /* rate */
131 url_fskip(pb, size - 7 * 4);
132 break;
133 case MKTAG('a', 'u', 'd', 's'):
134 codec_type = CODEC_TYPE_AUDIO;
135 /* nothing really useful */
136 url_fskip(pb, size - 4);
137 break;
138 default:
139 goto fail;
140 }
141 break;
142 case MKTAG('s', 't', 'r', 'f'):
143 /* stream header */
144 if (stream_index >= s->nb_streams) {
145 url_fskip(pb, size);
146 } else {
147 st = s->streams[stream_index];
148 switch(codec_type) {
149 case CODEC_TYPE_VIDEO:
150 get_le32(pb); /* size */
151 st->codec.width = get_le32(pb);
152 st->codec.height = get_le32(pb);
153 if (frame_period)
154 st->codec.frame_rate = (INT64_C(1000000) * FRAME_RATE_BASE) / frame_period;
155 else
156 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
157 get_le16(pb); /* panes */
158 get_le16(pb); /* depth */
159 tag1 = get_le32(pb);
160 #ifdef DEBUG
161 print_tag("video", tag1, 0);
162 #endif
163 st->codec.codec_type = CODEC_TYPE_VIDEO;
164 st->codec.codec_tag = tag1;
165 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
166 url_fskip(pb, size - 5 * 4);
167 break;
168 case CODEC_TYPE_AUDIO:
169 tag1 = get_le16(pb);
170 st->codec.codec_type = CODEC_TYPE_AUDIO;
171 st->codec.codec_tag = tag1;
172 st->codec.codec_id = codec_get_id(codec_wav_tags, tag1);
173 #ifdef DEBUG
174 printf("audio: 0x%x\n", tag1);
175 #endif
176 st->codec.channels = get_le16(pb);
177 st->codec.sample_rate = get_le32(pb);
178 st->codec.bit_rate = get_le32(pb) * 8;
179 url_fskip(pb, size - 3 * 4);
180 break;
181 default:
182 url_fskip(pb, size);
183 break;
184 }
185 }
186 break;
187 default:
188 /* skip tag */
189 size += (size & 1);
190 url_fskip(pb, size);
191 break;
192 }
193 }
194 end_of_header:
195 /* check stream number */
196 if (stream_index != s->nb_streams - 1) {
197 fail:
198 for(i=0;i<s->nb_streams;i++) {
199 if (s->streams[i])
200 free(s->streams[i]);
201 }
202 return -1;
203 }
204
205 return 0;
206 }
207
208 int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
209 {
210 AVIContext *avi = s->priv_data;
211 ByteIOContext *pb = &s->pb;
212 int n, d1, d2, size;
213
214 find_next:
215 if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
216 return -1;
217 d1 = get_byte(pb);
218 if (d1 < '0' || d1 > '9')
219 goto find_next;
220 d2 = get_byte(pb);
221 if (d2 < '0' || d2 > '9')
222 goto find_next;
223 n = (d1 - '0') * 10 + (d2 - '0');
224
225 if (n < 0 || n >= s->nb_streams)
226 goto find_next;
227
228 d1 = get_byte(pb);
229 d2 = get_byte(pb);
230 if ((d1 != 'd' && d2 != 'c') &&
231 (d1 != 'w' && d2 != 'b'))
232 goto find_next;
233
234 size = get_le32(pb);
235 av_new_packet(pkt, size);
236 pkt->stream_index = n;
237
238 get_buffer(pb, pkt->data, pkt->size);
239
240 if (size & 1)
241 get_byte(pb);
242
243 return 0;
244 }
245
246 int avi_read_close(AVFormatContext *s)
247 {
248 AVIContext *avi = s->priv_data;
249 free(avi);
250 return 0;
251 }