]> git.sesse.net Git - ffmpeg/blob - libavformat/avidec.c
huffyuv encoding fixed
[ffmpeg] / libavformat / avidec.c
1 /*
2  * AVI decoder.
3  * Copyright (c) 2001 Fabrice Bellard.
4  *
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.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
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
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_t movi_end;
32     offset_t movi_list;
33     AVIIndex *first, *last;
34 } AVIContext;
35
36 #ifdef DEBUG
37 static 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 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
49 {
50     AVIContext *avi = s->priv_data;
51     ByteIOContext *pb = &s->pb;
52     uint32_t tag, tag1;
53     int codec_type, stream_index, frame_period, bit_rate, scale, rate;
54     unsigned int size;
55     int i;
56     AVStream *st;
57
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;
67
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'):
97             /* avi header */
98             /* using frame_period is bad idea */
99             frame_period = get_le32(pb);
100             bit_rate = get_le32(pb) * 8;
101             url_fskip(pb, 4 * 4);
102             s->nb_streams = get_le32(pb);
103             for(i=0;i<s->nb_streams;i++) {
104                 AVStream *st = av_mallocz(sizeof(AVStream));
105                 if (!st)
106                     goto fail;
107                 avcodec_get_context_defaults(&st->codec);
108
109                 s->streams[i] = st;
110             }
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;
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
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 ? */
133                 scale= get_le32(pb); /* scale */
134                 rate= get_le32(pb); /* rate */
135
136                 if(scale && rate)
137                     st->codec.frame_rate= (rate * (uint64_t)FRAME_RATE_BASE + scale/2) / scale;
138                 else if(frame_period)
139                     st->codec.frame_rate = (1000000LL * FRAME_RATE_BASE + frame_period/2) / frame_period;
140                 else
141                     st->codec.frame_rate = 25 * FRAME_RATE_BASE;
142                 
143                 url_fskip(pb, size - 7 * 4);
144                 break;
145             case MKTAG('a', 'u', 'd', 's'):
146                 codec_type = CODEC_TYPE_AUDIO;
147                 /* nothing really useful */
148                 url_fskip(pb, size - 4);
149                 break;
150             default:
151                 goto fail;
152             }
153             break;
154         case MKTAG('s', 't', 'r', 'f'):
155             /* stream header */
156             if (stream_index >= s->nb_streams) {
157                 url_fskip(pb, size);
158             } else {
159                 st = s->streams[stream_index];
160                 switch(codec_type) {
161                 case CODEC_TYPE_VIDEO:
162                     get_le32(pb); /* size */
163                     st->codec.width = get_le32(pb);
164                     st->codec.height = get_le32(pb);
165                     get_le16(pb); /* panes */
166                     st->codec.bits_per_sample= get_le16(pb); /* depth */
167                     tag1 = get_le32(pb);
168                     get_le32(pb); /* ImageSize */
169                     get_le32(pb); /* XPelsPerMeter */
170                     get_le32(pb); /* YPelsPerMeter */
171                     get_le32(pb); /* ClrUsed */
172                     get_le32(pb); /* ClrImportant */
173
174                     st->codec.extradata_size= size - 10*4;
175                     st->codec.extradata= av_malloc(st->codec.extradata_size); //FIXME where should we free this?
176                     get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
177                     
178                     if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
179                         get_byte(pb);
180
181 #ifdef DEBUG
182                     print_tag("video", tag1, 0);
183 #endif
184                     st->codec.codec_type = CODEC_TYPE_VIDEO;
185                     st->codec.codec_tag = tag1;
186                     st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
187 //                    url_fskip(pb, size - 5 * 4);
188                     break;
189                 case CODEC_TYPE_AUDIO:
190                     get_wav_header(pb, &st->codec, (size >= 18));
191                     if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
192                         url_fskip(pb, 1);
193                     break;
194                 default:
195                     url_fskip(pb, size);
196                     break;
197                 }
198             }
199             break;
200         default:
201             /* skip tag */
202             size += (size & 1);
203             url_fskip(pb, size);
204             break;
205         }
206     }
207  end_of_header:
208     /* check stream number */
209     if (stream_index != s->nb_streams - 1) {
210     fail:
211         for(i=0;i<s->nb_streams;i++) {
212             av_freep(&s->streams[i]);
213         }
214         return -1;
215     }
216
217     return 0;
218 }
219
220 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
221 {
222     AVIContext *avi = s->priv_data;
223     ByteIOContext *pb = &s->pb;
224     int n, d1, d2, size;
225
226  find_next:
227     if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
228         return -1;
229     d1 = get_byte(pb);
230     if (d1 < '0' || d1 > '9')
231         goto find_next;
232     d2 = get_byte(pb);
233     if (d2 < '0' || d2 > '9')
234         goto find_next;
235     n = (d1 - '0') * 10 + (d2 - '0');
236     
237     if (n < 0 || n >= s->nb_streams)
238         goto find_next;
239     
240     d1 = get_byte(pb);
241     d2 = get_byte(pb);
242     if ((d1 != 'd' && d2 != 'c') &&
243         (d1 != 'w' && d2 != 'b'))
244         goto find_next;
245     
246     size = get_le32(pb);
247     av_new_packet(pkt, size);
248     pkt->stream_index = n;
249
250     get_buffer(pb, pkt->data, pkt->size);
251
252     if (size & 1)
253         get_byte(pb);
254
255     return 0;
256 }
257
258 static int avi_read_close(AVFormatContext *s)
259 {
260     return 0;
261 }
262
263 static int avi_probe(AVProbeData *p)
264 {
265     /* check file header */
266     if (p->buf_size <= 32)
267         return 0;
268     if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
269         p->buf[2] == 'F' && p->buf[3] == 'F' &&
270         p->buf[8] == 'A' && p->buf[9] == 'V' &&
271         p->buf[10] == 'I' && p->buf[11] == ' ')
272         return AVPROBE_SCORE_MAX;
273     else
274         return 0;
275 }
276
277 static AVInputFormat avi_iformat = {
278     "avi",
279     "avi format",
280     sizeof(AVIContext),
281     avi_probe,
282     avi_read_header,
283     avi_read_packet,
284     avi_read_close,
285 };
286
287 int avidec_init(void)
288 {
289     av_register_input_format(&avi_iformat);
290     return 0;
291 }