fixed double free - fixed probe function
[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{
c9a65ca8 50 AVIContext *avi = s->priv_data;
de6d9b64
FB
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
de6d9b64
FB
57 /* check RIFF header */
58 tag = get_le32(pb);
59
60 if (tag != MKTAG('R', 'I', 'F', 'F'))
61 return -1;
62 get_le32(pb); /* file size */
63 tag = get_le32(pb);
64 if (tag != MKTAG('A', 'V', 'I', ' '))
65 return -1;
66
67 /* first list tag */
68 stream_index = -1;
69 codec_type = -1;
70 frame_period = 0;
71 for(;;) {
72 if (url_feof(pb))
73 goto fail;
74 tag = get_le32(pb);
75 size = get_le32(pb);
76#ifdef DEBUG
77 print_tag("tag", tag, size);
78#endif
79
80 switch(tag) {
81 case MKTAG('L', 'I', 'S', 'T'):
82 /* ignored, except when start of video packets */
83 tag1 = get_le32(pb);
84#ifdef DEBUG
85 print_tag("list", tag1, 0);
86#endif
87 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
88 avi->movi_end = url_ftell(pb) + size - 4;
89#ifdef DEBUG
90 printf("movi end=%Lx\n", avi->movi_end);
91#endif
92 goto end_of_header;
93 }
94 break;
95 case MKTAG('a', 'v', 'i', 'h'):
96 /* avi header */
97 frame_period = get_le32(pb);
98 bit_rate = get_le32(pb) * 8;
99 url_fskip(pb, 4 * 4);
100 s->nb_streams = get_le32(pb);
101 for(i=0;i<s->nb_streams;i++) {
102 AVStream *st;
1ea4f593 103 st = av_malloc(sizeof(AVStream));
de6d9b64
FB
104 if (!st)
105 goto fail;
106 memset(st, 0, sizeof(AVStream));
107 s->streams[i] = st;
108 }
109 url_fskip(pb, size - 7 * 4);
110 break;
111 case MKTAG('s', 't', 'r', 'h'):
112 /* stream header */
113 stream_index++;
114 tag1 = get_le32(pb);
115 switch(tag1) {
116 case MKTAG('v', 'i', 'd', 's'):
117 codec_type = CODEC_TYPE_VIDEO;
118 get_le32(pb); /* codec tag */
119 get_le32(pb); /* flags */
120 get_le16(pb); /* priority */
121 get_le16(pb); /* language */
122 get_le32(pb); /* XXX: initial frame ? */
123 get_le32(pb); /* scale */
124 get_le32(pb); /* rate */
125 url_fskip(pb, size - 7 * 4);
126 break;
127 case MKTAG('a', 'u', 'd', 's'):
128 codec_type = CODEC_TYPE_AUDIO;
129 /* nothing really useful */
130 url_fskip(pb, size - 4);
131 break;
132 default:
133 goto fail;
134 }
135 break;
136 case MKTAG('s', 't', 'r', 'f'):
137 /* stream header */
138 if (stream_index >= s->nb_streams) {
139 url_fskip(pb, size);
140 } else {
141 st = s->streams[stream_index];
142 switch(codec_type) {
143 case CODEC_TYPE_VIDEO:
144 get_le32(pb); /* size */
145 st->codec.width = get_le32(pb);
146 st->codec.height = get_le32(pb);
147 if (frame_period)
8be1c656 148 st->codec.frame_rate = (INT64_C(1000000) * FRAME_RATE_BASE) / frame_period;
de6d9b64
FB
149 else
150 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
151 get_le16(pb); /* panes */
152 get_le16(pb); /* depth */
153 tag1 = get_le32(pb);
154#ifdef DEBUG
155 print_tag("video", tag1, 0);
156#endif
157 st->codec.codec_type = CODEC_TYPE_VIDEO;
158 st->codec.codec_tag = tag1;
159 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
160 url_fskip(pb, size - 5 * 4);
161 break;
162 case CODEC_TYPE_AUDIO:
163 tag1 = get_le16(pb);
164 st->codec.codec_type = CODEC_TYPE_AUDIO;
165 st->codec.codec_tag = tag1;
de6d9b64
FB
166#ifdef DEBUG
167 printf("audio: 0x%x\n", tag1);
168#endif
169 st->codec.channels = get_le16(pb);
170 st->codec.sample_rate = get_le32(pb);
171 st->codec.bit_rate = get_le32(pb) * 8;
46a3d068
FB
172 get_le16(pb); /* block align */
173 bps = get_le16(pb);
174 st->codec.codec_id = wav_codec_get_id(tag1, bps);
175 url_fskip(pb, size - 4 * 4);
de6d9b64
FB
176 break;
177 default:
178 url_fskip(pb, size);
179 break;
180 }
181 }
182 break;
183 default:
184 /* skip tag */
185 size += (size & 1);
186 url_fskip(pb, size);
187 break;
188 }
189 }
190 end_of_header:
191 /* check stream number */
192 if (stream_index != s->nb_streams - 1) {
193 fail:
194 for(i=0;i<s->nb_streams;i++) {
1ea4f593 195 av_freep(&s->streams[i]);
de6d9b64
FB
196 }
197 return -1;
198 }
199
200 return 0;
201}
202
203int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
204{
205 AVIContext *avi = s->priv_data;
206 ByteIOContext *pb = &s->pb;
207 int n, d1, d2, size;
208
209 find_next:
210 if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
211 return -1;
212 d1 = get_byte(pb);
213 if (d1 < '0' || d1 > '9')
214 goto find_next;
215 d2 = get_byte(pb);
216 if (d2 < '0' || d2 > '9')
217 goto find_next;
218 n = (d1 - '0') * 10 + (d2 - '0');
219
220 if (n < 0 || n >= s->nb_streams)
221 goto find_next;
222
223 d1 = get_byte(pb);
224 d2 = get_byte(pb);
225 if ((d1 != 'd' && d2 != 'c') &&
226 (d1 != 'w' && d2 != 'b'))
227 goto find_next;
228
229 size = get_le32(pb);
230 av_new_packet(pkt, size);
231 pkt->stream_index = n;
232
233 get_buffer(pb, pkt->data, pkt->size);
234
235 if (size & 1)
236 get_byte(pb);
237
238 return 0;
239}
240
241int avi_read_close(AVFormatContext *s)
242{
c9a65ca8
FB
243 return 0;
244}
245
246static int avi_probe(AVProbeData *p)
247{
248 /* check file header */
249 if (p->buf_size <= 32)
250 return 0;
251 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
252 p->buf[2] == 'F' && p->buf[3] == 'F' &&
253 p->buf[8] == 'A' && p->buf[9] == 'V' &&
254 p->buf[10] == 'I' && p->buf[11] == ' ')
255 return AVPROBE_SCORE_MAX;
256 else
257 return 0;
258}
259
260static AVInputFormat avi_iformat = {
261 "avi",
262 "avi format",
263 sizeof(AVIContext),
264 avi_probe,
265 avi_read_header,
266 avi_read_packet,
267 avi_read_close,
268};
269
270int avidec_init(void)
271{
272 av_register_input_format(&avi_iformat);
de6d9b64
FB
273 return 0;
274}