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 {
|
|
31 INT64 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 tag, tag1;
|
|
53 int codec_type, stream_index, size, frame_period, bit_rate;
|
|
54 int i;
|
|
55 AVStream *st;
|
|
56
|
|
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 /* using frame_period is bad idea */
|
|
98 frame_period = get_le32(pb);
|
|
99 bit_rate = get_le32(pb) * 8;
|
|
100 url_fskip(pb, 4 * 4);
|
|
101 s->nb_streams = get_le32(pb);
|
|
102 for(i=0;i<s->nb_streams;i++) {
|
|
103 AVStream *st = av_mallocz(sizeof(AVStream));
|
|
104 if (!st)
|
|
105 goto fail;
|
|
106 s->streams[i] = st;
|
|
107 }
|
|
108 url_fskip(pb, size - 7 * 4);
|
|
109 break;
|
|
110 case MKTAG('s', 't', 'r', 'h'):
|
|
111 /* stream header */
|
|
112 stream_index++;
|
|
113 tag1 = get_le32(pb);
|
|
114 switch(tag1) {
|
|
115 case MKTAG('v', 'i', 'd', 's'):
|
|
116 codec_type = CODEC_TYPE_VIDEO;
|
|
117 get_le32(pb); /* codec tag */
|
|
118 get_le32(pb); /* flags */
|
|
119 get_le16(pb); /* priority */
|
|
120 get_le16(pb); /* language */
|
|
121 get_le32(pb); /* XXX: initial frame ? */
|
|
122 get_le32(pb); /* scale */
|
|
123 get_le32(pb); /* rate */
|
|
124 size -= 6 * 4;
|
|
125 break;
|
|
126 case MKTAG('a', 'u', 'd', 's'):
|
|
127 codec_type = CODEC_TYPE_AUDIO;
|
|
128 /* nothing really useful */
|
|
129 }
|
|
130 url_fskip(pb, size - 4);
|
|
131 break;
|
|
132 case MKTAG('s', 't', 'r', 'f'):
|
|
133 /* stream header */
|
|
134 if (stream_index >= s->nb_streams) {
|
|
135 url_fskip(pb, size);
|
|
136 } else {
|
|
137 st = s->streams[stream_index];
|
|
138 switch(codec_type) {
|
|
139 case CODEC_TYPE_VIDEO:
|
|
140 get_le32(pb); /* size */
|
|
141 st->codec.width = get_le32(pb);
|
|
142 st->codec.height = get_le32(pb);
|
|
143 if (frame_period)
|
|
144 st->codec.frame_rate = (INT64_C(1000000) * FRAME_RATE_BASE) / frame_period;
|
|
145 else
|
|
146 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
|
|
147 get_le16(pb); /* panes */
|
|
148 get_le16(pb); /* depth */
|
|
149 tag1 = get_le32(pb);
|
|
150 #ifdef DEBUG
|
|
151 print_tag("video", tag1, 0);
|
|
152 #endif
|
|
153 st->codec.codec_type = CODEC_TYPE_VIDEO;
|
|
154 st->codec.codec_tag = tag1;
|
|
155 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
|
|
156 url_fskip(pb, size - 5 * 4);
|
|
157 break;
|
|
158 case CODEC_TYPE_AUDIO:
|
|
159 get_wav_header(pb, &st->codec, (size >= 18));
|
|
160 break;
|
|
161 default:
|
|
162 url_fskip(pb, size);
|
|
163 break;
|
|
164 }
|
|
165 }
|
|
166 break;
|
|
167 default:
|
|
168 /* skip tag */
|
|
169 size += (size & 1);
|
|
170 url_fskip(pb, size);
|
|
171 break;
|
|
172 }
|
|
173 }
|
|
174 end_of_header:
|
|
175 /* check stream number */
|
|
176 if (stream_index != s->nb_streams - 1) {
|
|
177 fail:
|
|
178 for(i=0;i<s->nb_streams;i++) {
|
|
179 av_freep(&s->streams[i]);
|
|
180 }
|
|
181 return -1;
|
|
182 }
|
|
183
|
|
184 return 0;
|
|
185 }
|
|
186
|
|
187 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
188 {
|
|
189 AVIContext *avi = s->priv_data;
|
|
190 ByteIOContext *pb = &s->pb;
|
|
191 int n, d1, d2, size;
|
|
192
|
|
193 for(;;) {
|
|
194 if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
|
|
195 return -1;
|
|
196 d1 = get_byte(pb) - '0';
|
|
197 d2 = get_byte(pb) - '0';
|
|
198 if (d1 < 0 || d1 > 9 || d2 < 0 || d2 > 9)
|
|
199 continue;
|
|
200
|
|
201 n = d1 * 10 + d2;
|
|
202 if (n < 0 || n >= s->nb_streams)
|
|
203 continue;
|
|
204
|
|
205 d1 = get_byte(pb);
|
|
206 d2 = get_byte(pb);
|
|
207 if ((d1 == 'd' && d2 == 'c')
|
|
208 || (d1 == 'w' && d2 == 'b'))
|
|
209 break;
|
|
210 }
|
|
211 size = get_le32(pb);
|
|
212 av_new_packet(pkt, size);
|
|
213 pkt->stream_index = n;
|
|
214
|
|
215 get_buffer(pb, pkt->data, pkt->size);
|
|
216
|
|
217 if (size & 1)
|
|
218 get_byte(pb);
|
|
219
|
|
220 return 0;
|
|
221 }
|
|
222
|
|
223 static int avi_read_close(AVFormatContext *s)
|
|
224 {
|
|
225 return 0;
|
|
226 }
|
|
227
|
|
228 static int avi_probe(AVProbeData *p)
|
|
229 {
|
|
230 /* check file header */
|
|
231 if (p->buf_size <= 32)
|
|
232 return 0;
|
|
233 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
|
|
234 p->buf[2] == 'F' && p->buf[3] == 'F' &&
|
|
235 p->buf[8] == 'A' && p->buf[9] == 'V' &&
|
|
236 p->buf[10] == 'I' && p->buf[11] == ' ')
|
|
237 return AVPROBE_SCORE_MAX;
|
|
238 else
|
|
239 return 0;
|
|
240 }
|
|
241
|
|
242 static AVInputFormat avi_iformat = {
|
|
243 "avi",
|
|
244 "avi format",
|
|
245 sizeof(AVIContext),
|
|
246 avi_probe,
|
|
247 avi_read_header,
|
|
248 avi_read_packet,
|
|
249 avi_read_close,
|
|
250 };
|
|
251
|
|
252 int avidec_init(void)
|
|
253 {
|
|
254 av_register_input_format(&avi_iformat);
|
|
255 return 0;
|
|
256 }
|