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