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