Mercurial > libavformat.hg
annotate avidec.c @ 73:d40ddc73858a libavformat
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
this seems to fix all avi parsing bugs
kabi, can u explain what this was good for?
author | michaelni |
---|---|
date | Sat, 08 Mar 2003 00:44:52 +0000 |
parents | a58a8a53eb46 |
children | 78bec272ce3a |
rev | line source |
---|---|
0 | 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 { | |
65 | 31 int64_t movi_end; |
0 | 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; | |
65 | 52 uint32_t tag, tag1; |
45 | 53 int codec_type, stream_index, frame_period, bit_rate; |
54 unsigned int size; | |
0 | 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; | |
5 | 107 avcodec_get_context_defaults(&st->codec); |
108 | |
0 | 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 get_le32(pb); /* codec tag */ | |
121 get_le32(pb); /* flags */ | |
122 get_le16(pb); /* priority */ | |
123 get_le16(pb); /* language */ | |
124 get_le32(pb); /* XXX: initial frame ? */ | |
125 get_le32(pb); /* scale */ | |
126 get_le32(pb); /* rate */ | |
73
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
127 url_fskip(pb, size - 7 * 4); |
0 | 128 break; |
129 case MKTAG('a', 'u', 'd', 's'): | |
130 codec_type = CODEC_TYPE_AUDIO; | |
131 /* nothing really useful */ | |
73
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
132 url_fskip(pb, size - 4); |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
133 break; |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
134 default: |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
135 goto fail; |
0 | 136 } |
137 break; | |
138 case MKTAG('s', 't', 'r', 'f'): | |
139 /* stream header */ | |
140 if (stream_index >= s->nb_streams) { | |
141 url_fskip(pb, size); | |
142 } else { | |
143 st = s->streams[stream_index]; | |
144 switch(codec_type) { | |
145 case CODEC_TYPE_VIDEO: | |
146 get_le32(pb); /* size */ | |
147 st->codec.width = get_le32(pb); | |
148 st->codec.height = get_le32(pb); | |
149 if (frame_period) | |
65 | 150 st->codec.frame_rate = (int64_t_C(1000000) * FRAME_RATE_BASE) / frame_period; |
0 | 151 else |
152 st->codec.frame_rate = 25 * FRAME_RATE_BASE; | |
153 get_le16(pb); /* panes */ | |
154 get_le16(pb); /* depth */ | |
155 tag1 = get_le32(pb); | |
156 #ifdef DEBUG | |
157 print_tag("video", tag1, 0); | |
158 #endif | |
159 st->codec.codec_type = CODEC_TYPE_VIDEO; | |
160 st->codec.codec_tag = tag1; | |
161 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1); | |
162 url_fskip(pb, size - 5 * 4); | |
163 break; | |
73
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
164 case CODEC_TYPE_AUDIO: |
0 | 165 get_wav_header(pb, &st->codec, (size >= 18)); |
13
8a5285a0ca2f
Fix for odd strf tag in Stargate SG-1 - 3x18 - Shades of Grey.avi
mmu_man
parents:
5
diff
changeset
|
166 if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */ |
8a5285a0ca2f
Fix for odd strf tag in Stargate SG-1 - 3x18 - Shades of Grey.avi
mmu_man
parents:
5
diff
changeset
|
167 url_fskip(pb, 1); |
0 | 168 break; |
169 default: | |
170 url_fskip(pb, size); | |
171 break; | |
172 } | |
173 } | |
174 break; | |
175 default: | |
176 /* skip tag */ | |
177 size += (size & 1); | |
178 url_fskip(pb, size); | |
179 break; | |
180 } | |
181 } | |
182 end_of_header: | |
183 /* check stream number */ | |
184 if (stream_index != s->nb_streams - 1) { | |
185 fail: | |
186 for(i=0;i<s->nb_streams;i++) { | |
187 av_freep(&s->streams[i]); | |
188 } | |
189 return -1; | |
190 } | |
191 | |
192 return 0; | |
193 } | |
194 | |
195 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt) | |
196 { | |
197 AVIContext *avi = s->priv_data; | |
198 ByteIOContext *pb = &s->pb; | |
199 int n, d1, d2, size; | |
73
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
200 |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
201 find_next: |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
202 if (url_feof(pb) || url_ftell(pb) >= avi->movi_end) |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
203 return -1; |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
204 d1 = get_byte(pb); |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
205 if (d1 < '0' || d1 > '9') |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
206 goto find_next; |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
207 d2 = get_byte(pb); |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
208 if (d2 < '0' || d2 > '9') |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
209 goto find_next; |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
210 n = (d1 - '0') * 10 + (d2 - '0'); |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
211 |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
212 if (n < 0 || n >= s->nb_streams) |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
213 goto find_next; |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
214 |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
215 d1 = get_byte(pb); |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
216 d2 = get_byte(pb); |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
217 if ((d1 != 'd' && d2 != 'c') && |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
218 (d1 != 'w' && d2 != 'b')) |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
219 goto find_next; |
d40ddc73858a
reversing not yet reversed changes from r1.7 -> r1.8 except the static/const stuff
michaelni
parents:
65
diff
changeset
|
220 |
0 | 221 size = get_le32(pb); |
222 av_new_packet(pkt, size); | |
223 pkt->stream_index = n; | |
224 | |
225 get_buffer(pb, pkt->data, pkt->size); | |
226 | |
227 if (size & 1) | |
228 get_byte(pb); | |
229 | |
230 return 0; | |
231 } | |
232 | |
233 static int avi_read_close(AVFormatContext *s) | |
234 { | |
235 return 0; | |
236 } | |
237 | |
238 static int avi_probe(AVProbeData *p) | |
239 { | |
240 /* check file header */ | |
241 if (p->buf_size <= 32) | |
242 return 0; | |
243 if (p->buf[0] == 'R' && p->buf[1] == 'I' && | |
244 p->buf[2] == 'F' && p->buf[3] == 'F' && | |
245 p->buf[8] == 'A' && p->buf[9] == 'V' && | |
246 p->buf[10] == 'I' && p->buf[11] == ' ') | |
247 return AVPROBE_SCORE_MAX; | |
248 else | |
249 return 0; | |
250 } | |
251 | |
252 static AVInputFormat avi_iformat = { | |
253 "avi", | |
254 "avi format", | |
255 sizeof(AVIContext), | |
256 avi_probe, | |
257 avi_read_header, | |
258 avi_read_packet, | |
259 avi_read_close, | |
260 }; | |
261 | |
262 int avidec_init(void) | |
263 { | |
264 av_register_input_format(&avi_iformat); | |
265 return 0; | |
266 } |