avcodec const correctness patch by (Drew Hess <dhess at ilm dot com>)
[libav.git] / libavformat / 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
deb0a292
RS
24static const struct AVI1Handler {
25 enum CodecID vcid;
26 enum CodecID acid;
27 uint32_t tag;
28} AVI1Handlers[] = {
29 { CODEC_ID_DVVIDEO, CODEC_ID_DVAUDIO, MKTAG('d', 'v', 's', 'd') },
30 { CODEC_ID_DVVIDEO, CODEC_ID_DVAUDIO, MKTAG('d', 'v', 'h', 'd') },
31 { CODEC_ID_DVVIDEO, CODEC_ID_DVAUDIO, MKTAG('d', 'v', 's', 'l') },
32 /* This is supposed to be the last one */
33 { CODEC_ID_NONE, CODEC_ID_NONE, 0 },
34};
35
de6d9b64
FB
36typedef struct AVIIndex {
37 unsigned char tag[4];
38 unsigned int flags, pos, len;
39 struct AVIIndex *next;
40} AVIIndex;
41
42typedef struct {
06219cb1 43 int64_t riff_end;
0c1a9eda 44 int64_t movi_end;
deb0a292
RS
45 int type;
46 uint8_t *buf;
47 int buf_size;
48 int stream_index;
de6d9b64
FB
49 offset_t movi_list;
50 AVIIndex *first, *last;
51} AVIContext;
52
53#ifdef DEBUG
1101abfe 54static void print_tag(const char *str, unsigned int tag, int size)
de6d9b64
FB
55{
56 printf("%s: tag=%c%c%c%c size=0x%x\n",
57 str, tag & 0xff,
58 (tag >> 8) & 0xff,
59 (tag >> 16) & 0xff,
60 (tag >> 24) & 0xff,
61 size);
62}
63#endif
64
06219cb1
RS
65static int get_riff(AVIContext *avi, ByteIOContext *pb)
66{
67 uint32_t tag;
68 /* check RIFF header */
69 tag = get_le32(pb);
70
71 if (tag != MKTAG('R', 'I', 'F', 'F'))
72 return -1;
73 avi->riff_end = get_le32(pb); /* RIFF chunk size */
74 avi->riff_end += url_ftell(pb); /* RIFF chunk end */
75 tag = get_le32(pb);
76 if (tag != MKTAG('A', 'V', 'I', ' ') && tag != MKTAG('A', 'V', 'I', 'X'))
77 return -1;
78
79 return 0;
80}
81
1101abfe 82static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
de6d9b64 83{
c9a65ca8 84 AVIContext *avi = s->priv_data;
de6d9b64 85 ByteIOContext *pb = &s->pb;
deb0a292 86 uint32_t tag, tag1, handler;
b559b29b 87 int codec_type, stream_index, frame_period, bit_rate, scale, rate;
247eadca
FB
88 unsigned int size, nb_frames;
89 int i, n;
de6d9b64 90 AVStream *st;
9e8cd0db 91 int xan_video = 0; /* hack to support Xan A/V */
de6d9b64 92
06219cb1 93 if (get_riff(avi, pb) < 0)
de6d9b64 94 return -1;
1101abfe 95
de6d9b64
FB
96 /* first list tag */
97 stream_index = -1;
98 codec_type = -1;
99 frame_period = 0;
deb0a292
RS
100 avi->type = 2;
101 avi->buf = av_malloc(1);
102 if (!avi->buf)
103 return -1;
104 avi->buf_size = 1;
de6d9b64
FB
105 for(;;) {
106 if (url_feof(pb))
107 goto fail;
108 tag = get_le32(pb);
109 size = get_le32(pb);
110#ifdef DEBUG
111 print_tag("tag", tag, size);
112#endif
113
114 switch(tag) {
115 case MKTAG('L', 'I', 'S', 'T'):
116 /* ignored, except when start of video packets */
117 tag1 = get_le32(pb);
118#ifdef DEBUG
119 print_tag("list", tag1, 0);
120#endif
121 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
122 avi->movi_end = url_ftell(pb) + size - 4;
123#ifdef DEBUG
124 printf("movi end=%Lx\n", avi->movi_end);
125#endif
126 goto end_of_header;
127 }
128 break;
129 case MKTAG('a', 'v', 'i', 'h'):
1101abfe
ZK
130 /* avi header */
131 /* using frame_period is bad idea */
de6d9b64
FB
132 frame_period = get_le32(pb);
133 bit_rate = get_le32(pb) * 8;
1101abfe 134 url_fskip(pb, 4 * 4);
247eadca
FB
135 n = get_le32(pb);
136 for(i=0;i<n;i++) {
137 st = av_new_stream(s, 0);
de6d9b64
FB
138 if (!st)
139 goto fail;
1101abfe 140 }
de6d9b64
FB
141 url_fskip(pb, size - 7 * 4);
142 break;
143 case MKTAG('s', 't', 'r', 'h'):
144 /* stream header */
145 stream_index++;
146 tag1 = get_le32(pb);
147 switch(tag1) {
deb0a292
RS
148 case MKTAG('i', 'a', 'v', 's'):
149 case MKTAG('i', 'v', 'a', 's'):
150 if (s->nb_streams != 1)
151 goto fail;
152 avi->type = 1;
153 avi->stream_index = 0;
154 case MKTAG('v', 'i', 'd', 's'):
de6d9b64 155 codec_type = CODEC_TYPE_VIDEO;
b559b29b
MN
156
157 if (stream_index >= s->nb_streams) {
158 url_fskip(pb, size - 4);
159 break;
160 }
161
162 st = s->streams[stream_index];
163
deb0a292 164 handler = get_le32(pb); /* codec tag */
de6d9b64
FB
165 get_le32(pb); /* flags */
166 get_le16(pb); /* priority */
167 get_le16(pb); /* language */
168 get_le32(pb); /* XXX: initial frame ? */
deb0a292
RS
169 scale = get_le32(pb); /* scale */
170 rate = get_le32(pb); /* rate */
b559b29b 171
14bea432
MN
172 if(scale && rate){
173 st->codec.frame_rate = rate;
deb0a292 174 st->codec.frame_rate_base = scale;
14bea432
MN
175 }else if(frame_period){
176 st->codec.frame_rate = 1000000;
deb0a292 177 st->codec.frame_rate_base = frame_period;
14bea432
MN
178 }else{
179 st->codec.frame_rate = 25;
180 st->codec.frame_rate_base = 1;
181 }
247eadca
FB
182 get_le32(pb); /* start */
183 nb_frames = get_le32(pb);
184 st->start_time = 0;
185 st->duration = (double)nb_frames *
186 st->codec.frame_rate_base * AV_TIME_BASE /
187 st->codec.frame_rate;
b559b29b 188
deb0a292 189 if (avi->type == 1) {
247eadca
FB
190 AVStream *st;
191
192 st = av_new_stream(s, 0);
deb0a292
RS
193 if (!st)
194 goto fail;
195
deb0a292
RS
196 stream_index++;
197
198 for (i=0; AVI1Handlers[i].tag != 0; ++i)
199 if (AVI1Handlers[i].tag == handler)
200 break;
201
202 if (AVI1Handlers[i].tag != 0) {
203 s->streams[0]->codec.codec_type = CODEC_TYPE_VIDEO;
204 s->streams[0]->codec.codec_id = AVI1Handlers[i].vcid;
205 s->streams[1]->codec.codec_type = CODEC_TYPE_AUDIO;
206 s->streams[1]->codec.codec_id = AVI1Handlers[i].acid;
247eadca 207 } else {
deb0a292 208 goto fail;
247eadca 209 }
deb0a292
RS
210 }
211
247eadca 212 url_fskip(pb, size - 9 * 4);
de6d9b64
FB
213 break;
214 case MKTAG('a', 'u', 'd', 's'):
247eadca
FB
215 {
216 unsigned int length, rate;
217
218 codec_type = CODEC_TYPE_AUDIO;
219
220 if (stream_index >= s->nb_streams) {
221 url_fskip(pb, size - 4);
222 break;
223 }
224 st = s->streams[stream_index];
225
226 get_le32(pb); /* tag */
227 get_le32(pb); /* flags */
228 get_le16(pb); /* priority */
229 get_le16(pb); /* language */
230 get_le32(pb); /* initial frame */
231 get_le32(pb); /* scale */
232 rate = get_le32(pb);
233 get_le32(pb); /* start */
234 length = get_le32(pb); /* length, in samples or bytes */
235 st->start_time = 0;
236 if (rate != 0)
237 st->duration = (int64_t)length * AV_TIME_BASE / rate;
238 url_fskip(pb, size - 9 * 4);
239 }
9bf9a5fc
MN
240 break;
241 default:
242 goto fail;
de6d9b64
FB
243 }
244 break;
245 case MKTAG('s', 't', 'r', 'f'):
246 /* stream header */
deb0a292 247 if (stream_index >= s->nb_streams || avi->type == 1) {
de6d9b64
FB
248 url_fskip(pb, size);
249 } else {
250 st = s->streams[stream_index];
251 switch(codec_type) {
252 case CODEC_TYPE_VIDEO:
253 get_le32(pb); /* size */
254 st->codec.width = get_le32(pb);
255 st->codec.height = get_le32(pb);
de6d9b64 256 get_le16(pb); /* panes */
b559b29b 257 st->codec.bits_per_sample= get_le16(pb); /* depth */
de6d9b64 258 tag1 = get_le32(pb);
b559b29b
MN
259 get_le32(pb); /* ImageSize */
260 get_le32(pb); /* XPelsPerMeter */
261 get_le32(pb); /* YPelsPerMeter */
262 get_le32(pb); /* ClrUsed */
263 get_le32(pb); /* ClrImportant */
264
265 st->codec.extradata_size= size - 10*4;
5ae2c73e 266 st->codec.extradata= av_malloc(st->codec.extradata_size);
b559b29b 267 get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
952c69c4
MN
268
269 if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
270 get_byte(pb);
b559b29b 271
de6d9b64
FB
272#ifdef DEBUG
273 print_tag("video", tag1, 0);
274#endif
275 st->codec.codec_type = CODEC_TYPE_VIDEO;
276 st->codec.codec_tag = tag1;
277 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
9e8cd0db
MM
278 if (st->codec.codec_id == CODEC_ID_XAN_WC4)
279 xan_video = 1;
b559b29b 280// url_fskip(pb, size - 5 * 4);
de6d9b64 281 break;
9bf9a5fc 282 case CODEC_TYPE_AUDIO:
2e7973bb 283 get_wav_header(pb, &st->codec, size);
1cef9527
FR
284 if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
285 url_fskip(pb, 1);
9e8cd0db
MM
286 /* special case time: To support Xan DPCM, hardcode
287 * the format if Xxan is the video codec */
288 if (xan_video)
289 st->codec.codec_id = CODEC_ID_XAN_DPCM;
de6d9b64
FB
290 break;
291 default:
292 url_fskip(pb, size);
293 break;
294 }
295 }
296 break;
297 default:
298 /* skip tag */
299 size += (size & 1);
300 url_fskip(pb, size);
301 break;
302 }
303 }
304 end_of_header:
305 /* check stream number */
306 if (stream_index != s->nb_streams - 1) {
307 fail:
5ae2c73e 308 av_free(avi->buf);
de6d9b64 309 for(i=0;i<s->nb_streams;i++) {
8a7b1b18 310 av_freep(&s->streams[i]->codec.extradata);
9145f8b3 311 av_freep(&s->streams[i]);
de6d9b64
FB
312 }
313 return -1;
314 }
1101abfe 315
de6d9b64
FB
316 return 0;
317}
318
deb0a292
RS
319static void __destruct_pkt(struct AVPacket *pkt)
320{
321 pkt->data = NULL; pkt->size = 0;
322 return;
323}
324
1101abfe 325static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
de6d9b64
FB
326{
327 AVIContext *avi = s->priv_data;
328 ByteIOContext *pb = &s->pb;
df99755b 329 int n, d[8], size, i;
deb0a292 330
df99755b 331 memset(d, -1, sizeof(int)*8);
2af7e610
FB
332
333 if (avi->type == 1 && avi->stream_index) {
334 /* duplicate DV packet */
335 av_init_packet(pkt);
336 pkt->data = avi->buf;
337 pkt->size = avi->buf_size;
338 pkt->destruct = __destruct_pkt;
339 pkt->stream_index = avi->stream_index;
340 avi->stream_index = !avi->stream_index;
341 return 0;
342 }
deb0a292 343
06219cb1 344 for(i=url_ftell(pb); !url_feof(pb); i++) {
df99755b 345 int j;
1101abfe 346
06219cb1
RS
347 if (i >= avi->movi_end) { /* Let's see if it's an OpenDML AVI */
348 uint32_t tag, size, tag2;
349 url_fskip(pb, avi->riff_end - url_ftell(pb));
350 if (get_riff(avi, pb) < 0)
351 return -1;
352
353 tag = get_le32(pb);
354 size = get_le32(pb);
355 tag2 = get_le32(pb);
356 if (tag == MKTAG('L','I','S','T') && tag2 == MKTAG('m','o','v','i'))
357 avi->movi_end = url_ftell(pb) + size - 4;
358 else
359 return -1;
360 }
361
df99755b
MN
362 for(j=0; j<7; j++)
363 d[j]= d[j+1];
364 d[7]= get_byte(pb);
365
366 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
367
368 //parse ix##
369 n= (d[2] - '0') * 10 + (d[3] - '0');
370 if( d[2] >= '0' && d[2] <= '9'
371 && d[3] >= '0' && d[3] <= '9'
372 && d[0] == 'i' && d[1] == 'x'
373 && n < s->nb_streams
374 && i + size <= avi->movi_end){
375
376 url_fskip(pb, size);
377 }
378
379 //parse ##dc/##wb
380 n= (d[0] - '0') * 10 + (d[1] - '0');
381 if( d[0] >= '0' && d[0] <= '9'
382 && d[1] >= '0' && d[1] <= '9'
deb0a292
RS
383 && ((d[2] == 'd' && d[3] == 'c') ||
384 (d[2] == 'w' && d[3] == 'b') ||
385 (d[2] == 'd' && d[3] == 'b') ||
386 (d[2] == '_' && d[3] == '_'))
df99755b 387 && n < s->nb_streams
deb0a292 388 && i + size <= avi->movi_end) {
df99755b 389
2af7e610
FB
390 if (avi->type == 1) {
391 uint8_t *tbuf = av_realloc(avi->buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
392 if (!tbuf)
393 return -1;
394 avi->buf = tbuf;
395 avi->buf_size = size;
396 av_init_packet(pkt);
397 pkt->data = avi->buf;
398 pkt->size = avi->buf_size;
399 pkt->destruct = __destruct_pkt;
400 avi->stream_index = n;
401 } else {
402 av_new_packet(pkt, size);
403 }
404 get_buffer(pb, pkt->data, size);
405 if (size & 1)
406 get_byte(pb);
407 pkt->stream_index = n;
408 pkt->flags |= PKT_FLAG_KEY; // FIXME: We really should read index for that
409 return 0;
df99755b
MN
410 }
411 }
df99755b 412 return -1;
de6d9b64
FB
413}
414
1101abfe 415static int avi_read_close(AVFormatContext *s)
de6d9b64 416{
5ae2c73e
MN
417 int i;
418 AVIContext *avi = s->priv_data;
419 av_free(avi->buf);
420
421 for(i=0;i<s->nb_streams;i++) {
422 AVStream *st = s->streams[i];
423// av_free(st->priv_data);
424 av_free(st->codec.extradata);
425 }
426
c9a65ca8
FB
427 return 0;
428}
429
430static int avi_probe(AVProbeData *p)
431{
432 /* check file header */
433 if (p->buf_size <= 32)
434 return 0;
435 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
436 p->buf[2] == 'F' && p->buf[3] == 'F' &&
437 p->buf[8] == 'A' && p->buf[9] == 'V' &&
438 p->buf[10] == 'I' && p->buf[11] == ' ')
439 return AVPROBE_SCORE_MAX;
440 else
441 return 0;
442}
443
444static AVInputFormat avi_iformat = {
445 "avi",
446 "avi format",
447 sizeof(AVIContext),
448 avi_probe,
449 avi_read_header,
450 avi_read_packet,
451 avi_read_close,
452};
453
454int avidec_init(void)
455{
456 av_register_input_format(&avi_iformat);
de6d9b64
FB
457 return 0;
458}