suppressed mpeg demux mess - use now dynamic stream creation api
[libav.git] / libav / avidec.c
CommitLineData
de6d9b64
FB
1/*
2 * AVI decoder.
3 * Copyright (c) 2001 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 {
31 INT64 movi_end;
32 offset_t movi_list;
33 AVIIndex *first, *last;
34} AVIContext;
35
36#ifdef DEBUG
37void 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
48int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
49{
50 AVIContext *avi;
51 ByteIOContext *pb = &s->pb;
52 UINT32 tag, tag1;
53 int codec_type, stream_index, size, frame_period, bit_rate;
46a3d068 54 int i, bps;
de6d9b64
FB
55 AVStream *st;
56
1ea4f593 57 avi = av_malloc(sizeof(AVIContext));
de6d9b64
FB
58 if (!avi)
59 return -1;
60 memset(avi, 0, sizeof(AVIContext));
61 s->priv_data = avi;
62
63 /* check RIFF header */
64 tag = get_le32(pb);
65
66 if (tag != MKTAG('R', 'I', 'F', 'F'))
67 return -1;
68 get_le32(pb); /* file size */
69 tag = get_le32(pb);
70 if (tag != MKTAG('A', 'V', 'I', ' '))
71 return -1;
72
73 /* first list tag */
74 stream_index = -1;
75 codec_type = -1;
76 frame_period = 0;
77 for(;;) {
78 if (url_feof(pb))
79 goto fail;
80 tag = get_le32(pb);
81 size = get_le32(pb);
82#ifdef DEBUG
83 print_tag("tag", tag, size);
84#endif
85
86 switch(tag) {
87 case MKTAG('L', 'I', 'S', 'T'):
88 /* ignored, except when start of video packets */
89 tag1 = get_le32(pb);
90#ifdef DEBUG
91 print_tag("list", tag1, 0);
92#endif
93 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
94 avi->movi_end = url_ftell(pb) + size - 4;
95#ifdef DEBUG
96 printf("movi end=%Lx\n", avi->movi_end);
97#endif
98 goto end_of_header;
99 }
100 break;
101 case MKTAG('a', 'v', 'i', 'h'):
102 /* avi header */
103 frame_period = get_le32(pb);
104 bit_rate = get_le32(pb) * 8;
105 url_fskip(pb, 4 * 4);
106 s->nb_streams = get_le32(pb);
107 for(i=0;i<s->nb_streams;i++) {
108 AVStream *st;
1ea4f593 109 st = av_malloc(sizeof(AVStream));
de6d9b64
FB
110 if (!st)
111 goto fail;
112 memset(st, 0, sizeof(AVStream));
113 s->streams[i] = st;
114 }
115 url_fskip(pb, size - 7 * 4);
116 break;
117 case MKTAG('s', 't', 'r', 'h'):
118 /* stream header */
119 stream_index++;
120 tag1 = get_le32(pb);
121 switch(tag1) {
122 case MKTAG('v', 'i', 'd', 's'):
123 codec_type = CODEC_TYPE_VIDEO;
124 get_le32(pb); /* codec tag */
125 get_le32(pb); /* flags */
126 get_le16(pb); /* priority */
127 get_le16(pb); /* language */
128 get_le32(pb); /* XXX: initial frame ? */
129 get_le32(pb); /* scale */
130 get_le32(pb); /* rate */
131 url_fskip(pb, size - 7 * 4);
132 break;
133 case MKTAG('a', 'u', 'd', 's'):
134 codec_type = CODEC_TYPE_AUDIO;
135 /* nothing really useful */
136 url_fskip(pb, size - 4);
137 break;
138 default:
139 goto fail;
140 }
141 break;
142 case MKTAG('s', 't', 'r', 'f'):
143 /* stream header */
144 if (stream_index >= s->nb_streams) {
145 url_fskip(pb, size);
146 } else {
147 st = s->streams[stream_index];
148 switch(codec_type) {
149 case CODEC_TYPE_VIDEO:
150 get_le32(pb); /* size */
151 st->codec.width = get_le32(pb);
152 st->codec.height = get_le32(pb);
153 if (frame_period)
8be1c656 154 st->codec.frame_rate = (INT64_C(1000000) * FRAME_RATE_BASE) / frame_period;
de6d9b64
FB
155 else
156 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
157 get_le16(pb); /* panes */
158 get_le16(pb); /* depth */
159 tag1 = get_le32(pb);
160#ifdef DEBUG
161 print_tag("video", tag1, 0);
162#endif
163 st->codec.codec_type = CODEC_TYPE_VIDEO;
164 st->codec.codec_tag = tag1;
165 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
166 url_fskip(pb, size - 5 * 4);
167 break;
168 case CODEC_TYPE_AUDIO:
169 tag1 = get_le16(pb);
170 st->codec.codec_type = CODEC_TYPE_AUDIO;
171 st->codec.codec_tag = tag1;
de6d9b64
FB
172#ifdef DEBUG
173 printf("audio: 0x%x\n", tag1);
174#endif
175 st->codec.channels = get_le16(pb);
176 st->codec.sample_rate = get_le32(pb);
177 st->codec.bit_rate = get_le32(pb) * 8;
46a3d068
FB
178 get_le16(pb); /* block align */
179 bps = get_le16(pb);
180 st->codec.codec_id = wav_codec_get_id(tag1, bps);
181 url_fskip(pb, size - 4 * 4);
de6d9b64
FB
182 break;
183 default:
184 url_fskip(pb, size);
185 break;
186 }
187 }
188 break;
189 default:
190 /* skip tag */
191 size += (size & 1);
192 url_fskip(pb, size);
193 break;
194 }
195 }
196 end_of_header:
197 /* check stream number */
198 if (stream_index != s->nb_streams - 1) {
199 fail:
200 for(i=0;i<s->nb_streams;i++) {
1ea4f593 201 av_freep(&s->streams[i]);
de6d9b64
FB
202 }
203 return -1;
204 }
205
206 return 0;
207}
208
209int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
210{
211 AVIContext *avi = s->priv_data;
212 ByteIOContext *pb = &s->pb;
213 int n, d1, d2, size;
214
215 find_next:
216 if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
217 return -1;
218 d1 = get_byte(pb);
219 if (d1 < '0' || d1 > '9')
220 goto find_next;
221 d2 = get_byte(pb);
222 if (d2 < '0' || d2 > '9')
223 goto find_next;
224 n = (d1 - '0') * 10 + (d2 - '0');
225
226 if (n < 0 || n >= s->nb_streams)
227 goto find_next;
228
229 d1 = get_byte(pb);
230 d2 = get_byte(pb);
231 if ((d1 != 'd' && d2 != 'c') &&
232 (d1 != 'w' && d2 != 'b'))
233 goto find_next;
234
235 size = get_le32(pb);
236 av_new_packet(pkt, size);
237 pkt->stream_index = n;
238
239 get_buffer(pb, pkt->data, pkt->size);
240
241 if (size & 1)
242 get_byte(pb);
243
244 return 0;
245}
246
247int avi_read_close(AVFormatContext *s)
248{
249 AVIContext *avi = s->priv_data;
1ea4f593 250 av_free(avi);
de6d9b64
FB
251 return 0;
252}