binary compatibility fix (workaround for r.147, r.148)
[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
24typedef struct AVIIndex {
25 unsigned char tag[4];
26 unsigned int flags, pos, len;
27 struct AVIIndex *next;
28} AVIIndex;
29
30typedef struct {
0c1a9eda 31 int64_t movi_end;
de6d9b64
FB
32 offset_t movi_list;
33 AVIIndex *first, *last;
34} AVIContext;
35
36#ifdef DEBUG
1101abfe 37static void print_tag(const char *str, unsigned int tag, int size)
de6d9b64
FB
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
1101abfe 48static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
de6d9b64 49{
c9a65ca8 50 AVIContext *avi = s->priv_data;
de6d9b64 51 ByteIOContext *pb = &s->pb;
0c1a9eda 52 uint32_t tag, tag1;
b559b29b 53 int codec_type, stream_index, frame_period, bit_rate, scale, rate;
a26058a5 54 unsigned int size;
e095026a 55 int i;
de6d9b64
FB
56 AVStream *st;
57
de6d9b64
FB
58 /* check RIFF header */
59 tag = get_le32(pb);
60
61 if (tag != MKTAG('R', 'I', 'F', 'F'))
62 return -1;
63 get_le32(pb); /* file size */
64 tag = get_le32(pb);
65 if (tag != MKTAG('A', 'V', 'I', ' '))
66 return -1;
1101abfe 67
de6d9b64
FB
68 /* first list tag */
69 stream_index = -1;
70 codec_type = -1;
71 frame_period = 0;
72 for(;;) {
73 if (url_feof(pb))
74 goto fail;
75 tag = get_le32(pb);
76 size = get_le32(pb);
77#ifdef DEBUG
78 print_tag("tag", tag, size);
79#endif
80
81 switch(tag) {
82 case MKTAG('L', 'I', 'S', 'T'):
83 /* ignored, except when start of video packets */
84 tag1 = get_le32(pb);
85#ifdef DEBUG
86 print_tag("list", tag1, 0);
87#endif
88 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
89 avi->movi_end = url_ftell(pb) + size - 4;
90#ifdef DEBUG
91 printf("movi end=%Lx\n", avi->movi_end);
92#endif
93 goto end_of_header;
94 }
95 break;
96 case MKTAG('a', 'v', 'i', 'h'):
1101abfe
ZK
97 /* avi header */
98 /* using frame_period is bad idea */
de6d9b64
FB
99 frame_period = get_le32(pb);
100 bit_rate = get_le32(pb) * 8;
1101abfe 101 url_fskip(pb, 4 * 4);
de6d9b64
FB
102 s->nb_streams = get_le32(pb);
103 for(i=0;i<s->nb_streams;i++) {
1101abfe 104 AVStream *st = av_mallocz(sizeof(AVStream));
de6d9b64
FB
105 if (!st)
106 goto fail;
1e491e29
MN
107 avcodec_get_context_defaults(&st->codec);
108
de6d9b64 109 s->streams[i] = st;
1101abfe 110 }
de6d9b64
FB
111 url_fskip(pb, size - 7 * 4);
112 break;
113 case MKTAG('s', 't', 'r', 'h'):
114 /* stream header */
115 stream_index++;
116 tag1 = get_le32(pb);
117 switch(tag1) {
118 case MKTAG('v', 'i', 'd', 's'):
119 codec_type = CODEC_TYPE_VIDEO;
b559b29b
MN
120
121 if (stream_index >= s->nb_streams) {
122 url_fskip(pb, size - 4);
123 break;
124 }
125
126 st = s->streams[stream_index];
127
de6d9b64
FB
128 get_le32(pb); /* codec tag */
129 get_le32(pb); /* flags */
130 get_le16(pb); /* priority */
131 get_le16(pb); /* language */
132 get_le32(pb); /* XXX: initial frame ? */
b559b29b
MN
133 scale= get_le32(pb); /* scale */
134 rate= get_le32(pb); /* rate */
135
14bea432
MN
136 if(scale && rate){
137 st->codec.frame_rate = rate;
138 st->codec.frame_rate_base= scale;
139 }else if(frame_period){
140 st->codec.frame_rate = 1000000;
141 st->codec.frame_rate_base= frame_period;
142 }else{
143 st->codec.frame_rate = 25;
144 st->codec.frame_rate_base = 1;
145 }
b559b29b 146
9bf9a5fc 147 url_fskip(pb, size - 7 * 4);
de6d9b64
FB
148 break;
149 case MKTAG('a', 'u', 'd', 's'):
150 codec_type = CODEC_TYPE_AUDIO;
151 /* nothing really useful */
9bf9a5fc
MN
152 url_fskip(pb, size - 4);
153 break;
154 default:
155 goto fail;
de6d9b64
FB
156 }
157 break;
158 case MKTAG('s', 't', 'r', 'f'):
159 /* stream header */
160 if (stream_index >= s->nb_streams) {
161 url_fskip(pb, size);
162 } else {
163 st = s->streams[stream_index];
164 switch(codec_type) {
165 case CODEC_TYPE_VIDEO:
166 get_le32(pb); /* size */
167 st->codec.width = get_le32(pb);
168 st->codec.height = get_le32(pb);
de6d9b64 169 get_le16(pb); /* panes */
b559b29b 170 st->codec.bits_per_sample= get_le16(pb); /* depth */
de6d9b64 171 tag1 = get_le32(pb);
b559b29b
MN
172 get_le32(pb); /* ImageSize */
173 get_le32(pb); /* XPelsPerMeter */
174 get_le32(pb); /* YPelsPerMeter */
175 get_le32(pb); /* ClrUsed */
176 get_le32(pb); /* ClrImportant */
177
178 st->codec.extradata_size= size - 10*4;
179 st->codec.extradata= av_malloc(st->codec.extradata_size); //FIXME where should we free this?
180 get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
952c69c4
MN
181
182 if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
183 get_byte(pb);
b559b29b 184
de6d9b64
FB
185#ifdef DEBUG
186 print_tag("video", tag1, 0);
187#endif
188 st->codec.codec_type = CODEC_TYPE_VIDEO;
189 st->codec.codec_tag = tag1;
190 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
b559b29b 191// url_fskip(pb, size - 5 * 4);
de6d9b64 192 break;
9bf9a5fc 193 case CODEC_TYPE_AUDIO:
2e7973bb 194 get_wav_header(pb, &st->codec, size);
1cef9527
FR
195 if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
196 url_fskip(pb, 1);
de6d9b64
FB
197 break;
198 default:
199 url_fskip(pb, size);
200 break;
201 }
202 }
203 break;
204 default:
205 /* skip tag */
206 size += (size & 1);
207 url_fskip(pb, size);
208 break;
209 }
210 }
211 end_of_header:
212 /* check stream number */
213 if (stream_index != s->nb_streams - 1) {
214 fail:
215 for(i=0;i<s->nb_streams;i++) {
8a7b1b18 216 av_freep(&s->streams[i]->codec.extradata);
9145f8b3 217 av_freep(&s->streams[i]);
de6d9b64
FB
218 }
219 return -1;
220 }
1101abfe 221
de6d9b64
FB
222 return 0;
223}
224
1101abfe 225static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
de6d9b64
FB
226{
227 AVIContext *avi = s->priv_data;
228 ByteIOContext *pb = &s->pb;
df99755b 229 int n, d[8], size, i;
9bf9a5fc 230
df99755b 231 memset(d, -1, sizeof(int)*8);
de6d9b64 232
df99755b
MN
233 for(i=url_ftell(pb); (!url_feof(pb)) && i < avi->movi_end; i++){
234 int j;
1101abfe 235
df99755b
MN
236 for(j=0; j<7; j++)
237 d[j]= d[j+1];
238 d[7]= get_byte(pb);
239
240 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
241
242 //parse ix##
243 n= (d[2] - '0') * 10 + (d[3] - '0');
244 if( d[2] >= '0' && d[2] <= '9'
245 && d[3] >= '0' && d[3] <= '9'
246 && d[0] == 'i' && d[1] == 'x'
247 && n < s->nb_streams
248 && i + size <= avi->movi_end){
249
250 url_fskip(pb, size);
251 }
252
253 //parse ##dc/##wb
254 n= (d[0] - '0') * 10 + (d[1] - '0');
255 if( d[0] >= '0' && d[0] <= '9'
256 && d[1] >= '0' && d[1] <= '9'
57d03758
MN
257 &&((d[2] == 'd' && d[3] == 'c') || (d[2] == 'w' && d[3] == 'b') || (d[2] == 'd' && d[3] == 'b') )
258
df99755b
MN
259 && n < s->nb_streams
260 && i + size <= avi->movi_end){
261
262 av_new_packet(pkt, size);
263 pkt->stream_index = n;
1101abfe 264
df99755b
MN
265 get_buffer(pb, pkt->data, pkt->size);
266
267 if (size & 1)
268 get_byte(pb);
269
270 return 0;
271 }
272 }
273
274 return -1;
de6d9b64
FB
275}
276
1101abfe 277static int avi_read_close(AVFormatContext *s)
de6d9b64 278{
c9a65ca8
FB
279 return 0;
280}
281
282static int avi_probe(AVProbeData *p)
283{
284 /* check file header */
285 if (p->buf_size <= 32)
286 return 0;
287 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
288 p->buf[2] == 'F' && p->buf[3] == 'F' &&
289 p->buf[8] == 'A' && p->buf[9] == 'V' &&
290 p->buf[10] == 'I' && p->buf[11] == ' ')
291 return AVPROBE_SCORE_MAX;
292 else
293 return 0;
294}
295
296static AVInputFormat avi_iformat = {
297 "avi",
298 "avi format",
299 sizeof(AVIContext),
300 avi_probe,
301 avi_read_header,
302 avi_read_packet,
303 avi_read_close,
304};
305
306int avidec_init(void)
307{
308 av_register_input_format(&avi_iformat);
de6d9b64
FB
309 return 0;
310}