3 * Copyright (c) 2001 Fabrice Bellard.
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.
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.
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
26 typedef struct AVIIndexEntry {
29 unsigned int cum_len; /* sum of all lengths before this packet */
32 typedef struct AVIStream {
33 AVIIndexEntry *index_entries;
35 int index_entries_allocated_size;
36 int frame_offset; /* current frame (video) or byte (audio) counter
37 (used to compute the pts) */
40 int sample_size; /* audio only data */
43 int new_frame_offset; /* temporary storage (used during seek) */
44 int cum_len; /* temporary storage (used during seek) */
52 DVDemuxContext* dv_demux;
55 static int avi_load_index(AVFormatContext *s);
58 static void print_tag(const char *str, unsigned int tag, int size)
60 printf("%s: tag=%c%c%c%c size=0x%x\n",
69 static int get_riff(AVIContext *avi, ByteIOContext *pb)
72 /* check RIFF header */
75 if (tag != MKTAG('R', 'I', 'F', 'F'))
77 avi->riff_end = get_le32(pb); /* RIFF chunk size */
78 avi->riff_end += url_ftell(pb); /* RIFF chunk end */
80 if (tag != MKTAG('A', 'V', 'I', ' ') && tag != MKTAG('A', 'V', 'I', 'X'))
86 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
88 AVIContext *avi = s->priv_data;
89 ByteIOContext *pb = &s->pb;
90 uint32_t tag, tag1, handler;
91 int codec_type, stream_index, frame_period, bit_rate, scale, rate;
92 unsigned int size, nb_frames;
96 int xan_video = 0; /* hack to support Xan A/V */
98 if (get_riff(avi, pb) < 0)
111 print_tag("tag", tag, size);
115 case MKTAG('L', 'I', 'S', 'T'):
116 /* ignored, except when start of video packets */
119 print_tag("list", tag1, 0);
121 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
122 avi->movi_list = url_ftell(pb) - 4;
123 avi->movi_end = avi->movi_list + size;
125 printf("movi end=%Lx\n", avi->movi_end);
130 case MKTAG('a', 'v', 'i', 'h'):
132 /* using frame_period is bad idea */
133 frame_period = get_le32(pb);
134 bit_rate = get_le32(pb) * 8;
135 url_fskip(pb, 4 * 4);
139 st = av_new_stream(s, i);
143 ast = av_mallocz(sizeof(AVIStream));
148 url_fskip(pb, size - 7 * 4);
150 case MKTAG('s', 't', 'r', 'h'):
154 handler = get_le32(pb); /* codec tag */
156 print_tag("strh", tag1, -1);
159 case MKTAG('i', 'a', 'v', 's'):
160 case MKTAG('i', 'v', 'a', 's'):
162 * After some consideration -- I don't think we
163 * have to support anything but DV in a type1 AVIs.
165 if (s->nb_streams != 1)
168 if (handler != MKTAG('d', 'v', 's', 'd') &&
169 handler != MKTAG('d', 'v', 'h', 'd') &&
170 handler != MKTAG('d', 'v', 's', 'l'))
173 av_freep(&s->streams[0]->codec.extradata);
174 av_freep(&s->streams[0]);
176 avi->dv_demux = dv_init_demux(s);
179 stream_index = s->nb_streams - 1;
180 url_fskip(pb, size - 8);
182 case MKTAG('v', 'i', 'd', 's'):
183 codec_type = CODEC_TYPE_VIDEO;
185 if (stream_index >= s->nb_streams) {
186 url_fskip(pb, size - 8);
190 st = s->streams[stream_index];
192 st->codec.stream_codec_tag= handler;
194 get_le32(pb); /* flags */
195 get_le16(pb); /* priority */
196 get_le16(pb); /* language */
197 get_le32(pb); /* XXX: initial frame ? */
198 scale = get_le32(pb); /* scale */
199 rate = get_le32(pb); /* rate */
202 }else if(frame_period){
204 scale = frame_period;
211 av_set_pts_info(st, 64, scale, rate);
212 st->codec.frame_rate = rate;
213 st->codec.frame_rate_base = scale;
214 get_le32(pb); /* start */
215 nb_frames = get_le32(pb);
217 st->duration = av_rescale(nb_frames,
218 st->codec.frame_rate_base * AV_TIME_BASE,
219 st->codec.frame_rate);
220 url_fskip(pb, size - 9 * 4);
222 case MKTAG('a', 'u', 'd', 's'):
226 codec_type = CODEC_TYPE_AUDIO;
228 if (stream_index >= s->nb_streams) {
229 url_fskip(pb, size - 8);
232 st = s->streams[stream_index];
235 get_le32(pb); /* flags */
236 get_le16(pb); /* priority */
237 get_le16(pb); /* language */
238 get_le32(pb); /* initial frame */
239 ast->scale = get_le32(pb); /* scale */
240 ast->rate = get_le32(pb);
241 av_set_pts_info(st, 64, ast->scale, ast->rate);
242 ast->start= get_le32(pb); /* start */
243 length = get_le32(pb); /* length, in samples or bytes */
244 get_le32(pb); /* buffer size */
245 get_le32(pb); /* quality */
246 ast->sample_size = get_le32(pb); /* sample ssize */
247 //av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d\n", ast->scale, ast->rate, ast->sample_size, ast->start);
250 st->duration = (int64_t)length * AV_TIME_BASE / ast->rate;
251 url_fskip(pb, size - 12 * 4);
254 case MKTAG('t', 'x', 't', 's'):
256 codec_type = CODEC_TYPE_DATA; //CODEC_TYPE_SUB ? FIXME
257 url_fskip(pb, size - 8);
263 case MKTAG('s', 't', 'r', 'f'):
265 if (stream_index >= s->nb_streams || avi->dv_demux) {
268 st = s->streams[stream_index];
270 case CODEC_TYPE_VIDEO:
271 get_le32(pb); /* size */
272 st->codec.width = get_le32(pb);
273 st->codec.height = get_le32(pb);
274 get_le16(pb); /* panes */
275 st->codec.bits_per_sample= get_le16(pb); /* depth */
277 get_le32(pb); /* ImageSize */
278 get_le32(pb); /* XPelsPerMeter */
279 get_le32(pb); /* YPelsPerMeter */
280 get_le32(pb); /* ClrUsed */
281 get_le32(pb); /* ClrImportant */
283 st->codec.extradata_size= size - 10*4;
284 st->codec.extradata= av_malloc(st->codec.extradata_size);
285 get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
287 if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
290 /* Extract palette from extradata if bpp <= 8 */
291 /* This code assumes that extradata contains only palette */
292 /* This is true for all paletted codecs implemented in ffmpeg */
293 if (st->codec.extradata_size && (st->codec.bits_per_sample <= 8)) {
294 st->codec.palctrl = av_mallocz(sizeof(AVPaletteControl));
295 #ifdef WORDS_BIGENDIAN
296 for (i = 0; i < FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)/4; i++)
297 st->codec.palctrl->palette[i] = bswap_32(((uint32_t*)st->codec.extradata)[i]);
299 memcpy(st->codec.palctrl->palette, st->codec.extradata,
300 FFMIN(st->codec.extradata_size, AVPALETTE_SIZE));
302 st->codec.palctrl->palette_changed = 1;
306 print_tag("video", tag1, 0);
308 st->codec.codec_type = CODEC_TYPE_VIDEO;
309 st->codec.codec_tag = tag1;
310 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
311 if (st->codec.codec_id == CODEC_ID_XAN_WC4)
313 // url_fskip(pb, size - 5 * 4);
315 case CODEC_TYPE_AUDIO:
316 get_wav_header(pb, &st->codec, size);
317 if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
319 /* special case time: To support Xan DPCM, hardcode
320 * the format if Xxan is the video codec */
321 st->need_parsing = 1;
322 /* force parsing as several audio frames can be in
325 st->codec.codec_id = CODEC_ID_XAN_DPCM;
341 /* check stream number */
342 if (stream_index != s->nb_streams - 1) {
344 for(i=0;i<s->nb_streams;i++) {
345 av_freep(&s->streams[i]->codec.extradata);
346 av_freep(&s->streams[i]);
351 assert(!avi->index_loaded);
353 avi->index_loaded = 1;
358 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
360 AVIContext *avi = s->priv_data;
361 ByteIOContext *pb = &s->pb;
362 int n, d[8], size, i;
365 memset(d, -1, sizeof(int)*8);
368 size = dv_get_packet(avi->dv_demux, pkt);
373 for(i=url_ftell(pb); !url_feof(pb); i++) {
376 if (i >= avi->movi_end) { /* Let's see if it's an OpenDML AVI */
377 uint32_t tag, size, tag2;
378 url_fskip(pb, avi->riff_end - url_ftell(pb));
379 if (get_riff(avi, pb) < 0)
385 if (tag == MKTAG('L','I','S','T') && tag2 == MKTAG('m','o','v','i'))
386 avi->movi_end = url_ftell(pb) + size - 4;
395 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
398 n= (d[2] - '0') * 10 + (d[3] - '0');
399 if( d[2] >= '0' && d[2] <= '9'
400 && d[3] >= '0' && d[3] <= '9'
401 && d[0] == 'i' && d[1] == 'x'
403 && i + size <= avi->movi_end){
409 n= (d[0] - '0') * 10 + (d[1] - '0');
410 if( d[0] >= '0' && d[0] <= '9'
411 && d[1] >= '0' && d[1] <= '9'
412 && ((d[2] == 'd' && d[3] == 'c') ||
413 (d[2] == 'w' && d[3] == 'b') ||
414 (d[2] == 'd' && d[3] == 'b') ||
415 (d[2] == '_' && d[3] == '_'))
417 && i + size <= avi->movi_end) {
419 av_new_packet(pkt, size);
420 get_buffer(pb, pkt->data, size);
427 dstr = pkt->destruct;
428 size = dv_produce_packet(avi->dv_demux, pkt,
429 pkt->data, pkt->size);
430 pkt->destruct = dstr;
431 pkt->flags |= PKT_FLAG_KEY;
438 /* XXX: how to handle B frames in avi ? */
439 pkt->dts = ast->frame_offset;
440 // pkt->dts += ast->start;
442 pkt->dts /= ast->sample_size;
443 //av_log(NULL, AV_LOG_DEBUG, "dts:%Ld offset:%d %d/%d %d st:%d size:%d\n", pkt->dts, ast->frame_offset, ast->scale, ast->rate, AV_TIME_BASE, n, size);
444 pkt->stream_index = n;
445 /* FIXME: We really should read index for that */
446 if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
447 if (ast->frame_offset < ast->nb_index_entries) {
448 if (ast->index_entries[ast->frame_offset].flags & AVIIF_INDEX)
449 pkt->flags |= PKT_FLAG_KEY;
451 /* if no index, better to say that all frames
453 pkt->flags |= PKT_FLAG_KEY;
456 pkt->flags |= PKT_FLAG_KEY;
459 ast->frame_offset += pkt->size;
469 /* XXX: we make the implicit supposition that the position are sorted
471 static int avi_read_idx1(AVFormatContext *s, int size)
473 ByteIOContext *pb = &s->pb;
474 int nb_index_entries, i;
477 AVIIndexEntry *ie, *entries;
478 unsigned int index, tag, flags, pos, len;
480 nb_index_entries = size / 16;
481 if (nb_index_entries <= 0)
484 /* read the entries and sort them in each stream component */
485 for(i = 0; i < nb_index_entries; i++) {
487 flags = get_le32(pb);
490 #if defined(DEBUG_SEEK) && 0
491 printf("%d: tag=0x%x flags=0x%x pos=0x%x len=%d\n",
492 i, tag, flags, pos, len);
494 index = ((tag & 0xff) - '0') * 10;
495 index += ((tag >> 8) & 0xff) - '0';
496 if (index >= s->nb_streams)
498 st = s->streams[index];
501 entries = av_fast_realloc(ast->index_entries,
502 &ast->index_entries_allocated_size,
503 (ast->nb_index_entries + 1) *
504 sizeof(AVIIndexEntry));
506 ast->index_entries = entries;
507 ie = &entries[ast->nb_index_entries++];
510 ie->cum_len = ast->cum_len;
517 static int avi_load_index(AVFormatContext *s)
519 AVIContext *avi = s->priv_data;
520 ByteIOContext *pb = &s->pb;
522 offset_t pos= url_ftell(pb);
524 url_fseek(pb, avi->movi_end, SEEK_SET);
526 printf("movi_end=0x%llx\n", avi->movi_end);
534 printf("tag=%c%c%c%c size=0x%x\n",
542 case MKTAG('i', 'd', 'x', '1'):
543 if (avi_read_idx1(s, size) < 0)
556 url_fseek(pb, pos, SEEK_SET);
560 /* return the index entry whose position is immediately >= 'wanted_pos' */
561 static int locate_frame_in_index(AVIIndexEntry *entries,
562 int nb_entries, int wanted_pos)
570 pos = entries[m].pos;
571 if (pos == wanted_pos)
573 else if (pos > wanted_pos) {
586 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp)
588 AVIContext *avi = s->priv_data;
594 if (!avi->index_loaded) {
595 /* we only load the index on demand */
597 avi->index_loaded = 1;
599 if (stream_index < 0) {
600 for(i = 0; i < s->nb_streams; i++) {
602 if (st->codec.codec_type == CODEC_TYPE_VIDEO)
610 st = s->streams[stream_index];
611 if (st->codec.codec_type != CODEC_TYPE_VIDEO)
614 /* compute the frame number */
615 frame_number = timestamp;
617 printf("timestamp=%0.3f nb_indexes=%d frame_number=%d\n",
618 (double)timestamp / AV_TIME_BASE,
619 ast->nb_index_entries, frame_number);
621 /* find a closest key frame before */
622 if (frame_number >= ast->nb_index_entries)
624 while (frame_number >= 0 &&
625 !(ast->index_entries[frame_number].flags & AVIIF_INDEX))
627 if (frame_number < 0)
629 ast->new_frame_offset = frame_number;
631 /* find the position */
632 pos = ast->index_entries[frame_number].pos;
635 printf("key_frame_number=%d pos=0x%llx\n",
639 /* update the frame counters for all the other stream by looking
640 at the positions just after the one found */
641 for(i = 0; i < s->nb_streams; i++) {
643 if (i != stream_index) {
646 if (ast->nb_index_entries <= 0)
648 j = locate_frame_in_index(ast->index_entries,
649 ast->nb_index_entries,
652 if ((j + 1) < ast->nb_index_entries)
654 /* extract the current frame number */
655 if (ast->sample_size==0)
656 ast->new_frame_offset = j;
658 ast->new_frame_offset = ast->index_entries[j].cum_len;
662 /* everything is OK now. We can update the frame offsets */
663 for(i = 0; i < s->nb_streams; i++) {
666 ast->frame_offset = ast->new_frame_offset;
668 printf("%d: frame_offset=%d\n", i,
673 pos += avi->movi_list;
674 url_fseek(&s->pb, pos, SEEK_SET);
678 static int avi_read_close(AVFormatContext *s)
681 AVIContext *avi = s->priv_data;
683 for(i=0;i<s->nb_streams;i++) {
684 AVStream *st = s->streams[i];
685 AVIStream *ast = st->priv_data;
687 av_free(ast->index_entries);
690 av_free(st->codec.extradata);
691 av_free(st->codec.palctrl);
695 av_free(avi->dv_demux);
700 static int avi_probe(AVProbeData *p)
702 /* check file header */
703 if (p->buf_size <= 32)
705 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
706 p->buf[2] == 'F' && p->buf[3] == 'F' &&
707 p->buf[8] == 'A' && p->buf[9] == 'V' &&
708 p->buf[10] == 'I' && p->buf[11] == ' ')
709 return AVPROBE_SCORE_MAX;
714 static AVInputFormat avi_iformat = {
725 int avidec_init(void)
727 av_register_input_format(&avi_iformat);