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 */
42 int new_frame_offset; /* temporary storage (used during seek) */
43 int cum_len; /* temporary storage (used during seek) */
51 DVDemuxContext* dv_demux;
55 static void print_tag(const char *str, unsigned int tag, int size)
57 printf("%s: tag=%c%c%c%c size=0x%x\n",
66 static int get_riff(AVIContext *avi, ByteIOContext *pb)
69 /* check RIFF header */
72 if (tag != MKTAG('R', 'I', 'F', 'F'))
74 avi->riff_end = get_le32(pb); /* RIFF chunk size */
75 avi->riff_end += url_ftell(pb); /* RIFF chunk end */
77 if (tag != MKTAG('A', 'V', 'I', ' ') && tag != MKTAG('A', 'V', 'I', 'X'))
83 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
85 AVIContext *avi = s->priv_data;
86 ByteIOContext *pb = &s->pb;
87 uint32_t tag, tag1, handler;
88 int codec_type, stream_index, frame_period, bit_rate, scale, rate;
89 unsigned int size, nb_frames;
93 int xan_video = 0; /* hack to support Xan A/V */
95 av_set_pts_info(s, 64, 1, AV_TIME_BASE);
97 if (get_riff(avi, pb) < 0)
110 print_tag("tag", tag, size);
114 case MKTAG('L', 'I', 'S', 'T'):
115 /* ignored, except when start of video packets */
118 print_tag("list", tag1, 0);
120 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
121 avi->movi_list = url_ftell(pb) - 4;
122 avi->movi_end = avi->movi_list + size;
124 printf("movi end=%Lx\n", avi->movi_end);
129 case MKTAG('a', 'v', 'i', 'h'):
131 /* using frame_period is bad idea */
132 frame_period = get_le32(pb);
133 bit_rate = get_le32(pb) * 8;
134 url_fskip(pb, 4 * 4);
138 st = av_new_stream(s, i);
141 ast = av_mallocz(sizeof(AVIStream));
146 url_fskip(pb, size - 7 * 4);
148 case MKTAG('s', 't', 'r', 'h'):
152 handler = get_le32(pb); /* codec tag */
154 case MKTAG('i', 'a', 'v', 's'):
155 case MKTAG('i', 'v', 'a', 's'):
157 * After some consideration -- I don't think we
158 * have to support anything but DV in a type1 AVIs.
160 if (s->nb_streams != 1)
163 if (handler != MKTAG('d', 'v', 's', 'd') &&
164 handler != MKTAG('d', 'v', 'h', 'd') &&
165 handler != MKTAG('d', 'v', 's', 'l'))
168 av_freep(&s->streams[0]->codec.extradata);
169 av_freep(&s->streams[0]);
171 avi->dv_demux = dv_init_demux(s);
174 stream_index = s->nb_streams - 1;
175 url_fskip(pb, size - 8);
177 case MKTAG('v', 'i', 'd', 's'):
178 codec_type = CODEC_TYPE_VIDEO;
180 if (stream_index >= s->nb_streams) {
181 url_fskip(pb, size - 8);
185 st = s->streams[stream_index];
187 st->codec.stream_codec_tag= handler;
189 get_le32(pb); /* flags */
190 get_le16(pb); /* priority */
191 get_le16(pb); /* language */
192 get_le32(pb); /* XXX: initial frame ? */
193 scale = get_le32(pb); /* scale */
194 rate = get_le32(pb); /* rate */
197 }else if(frame_period){
199 scale = frame_period;
206 st->codec.frame_rate = rate;
207 st->codec.frame_rate_base = scale;
208 get_le32(pb); /* start */
209 nb_frames = get_le32(pb);
211 st->duration = (double)nb_frames *
212 st->codec.frame_rate_base * AV_TIME_BASE /
213 st->codec.frame_rate;
214 url_fskip(pb, size - 9 * 4);
216 case MKTAG('a', 'u', 'd', 's'):
220 codec_type = CODEC_TYPE_AUDIO;
222 if (stream_index >= s->nb_streams) {
223 url_fskip(pb, size - 8);
226 st = s->streams[stream_index];
229 get_le32(pb); /* flags */
230 get_le16(pb); /* priority */
231 get_le16(pb); /* language */
232 get_le32(pb); /* initial frame */
233 ast->scale = get_le32(pb); /* scale */
234 ast->rate = get_le32(pb);
235 get_le32(pb); /* start */
236 length = get_le32(pb); /* length, in samples or bytes */
237 get_le32(pb); /* buffer size */
238 get_le32(pb); /* quality */
239 ast->sample_size = get_le32(pb); /* sample ssize */
242 st->duration = (int64_t)length * AV_TIME_BASE / ast->rate;
243 url_fskip(pb, size - 12 * 4);
250 case MKTAG('s', 't', 'r', 'f'):
252 if (stream_index >= s->nb_streams || avi->dv_demux) {
255 st = s->streams[stream_index];
257 case CODEC_TYPE_VIDEO:
258 get_le32(pb); /* size */
259 st->codec.width = get_le32(pb);
260 st->codec.height = get_le32(pb);
261 get_le16(pb); /* panes */
262 st->codec.bits_per_sample= get_le16(pb); /* depth */
264 get_le32(pb); /* ImageSize */
265 get_le32(pb); /* XPelsPerMeter */
266 get_le32(pb); /* YPelsPerMeter */
267 get_le32(pb); /* ClrUsed */
268 get_le32(pb); /* ClrImportant */
270 st->codec.extradata_size= size - 10*4;
271 st->codec.extradata= av_malloc(st->codec.extradata_size);
272 get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
274 if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
277 /* Extract palette from extradata if bpp <= 8 */
278 /* This code assumes that extradata contains only palette */
279 /* This is true for all paletted codecs implemented in ffmpeg */
280 if (st->codec.extradata_size && (st->codec.bits_per_sample <= 8)) {
281 st->codec.palctrl = av_mallocz(sizeof(AVPaletteControl));
282 #ifdef WORDS_BIGENDIAN
283 for (i = 0; i < FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)/4; i++)
284 st->codec.palctrl->palette[i] = bswap_32(((uint32_t*)st->codec.extradata)[i]);
286 memcpy(st->codec.palctrl->palette, st->codec.extradata,
287 FFMIN(st->codec.extradata_size, AVPALETTE_SIZE));
289 st->codec.palctrl->palette_changed = 1;
293 print_tag("video", tag1, 0);
295 st->codec.codec_type = CODEC_TYPE_VIDEO;
296 st->codec.codec_tag = tag1;
297 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
298 if (st->codec.codec_id == CODEC_ID_XAN_WC4)
300 // url_fskip(pb, size - 5 * 4);
302 case CODEC_TYPE_AUDIO:
303 get_wav_header(pb, &st->codec, size);
304 if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
306 /* special case time: To support Xan DPCM, hardcode
307 * the format if Xxan is the video codec */
308 st->need_parsing = 1;
309 /* force parsing as several audio frames can be in
312 st->codec.codec_id = CODEC_ID_XAN_DPCM;
328 /* check stream number */
329 if (stream_index != s->nb_streams - 1) {
331 for(i=0;i<s->nb_streams;i++) {
332 av_freep(&s->streams[i]->codec.extradata);
333 av_freep(&s->streams[i]);
341 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
343 AVIContext *avi = s->priv_data;
344 ByteIOContext *pb = &s->pb;
345 int n, d[8], size, i;
348 memset(d, -1, sizeof(int)*8);
351 size = dv_get_packet(avi->dv_demux, pkt);
356 for(i=url_ftell(pb); !url_feof(pb); i++) {
359 if (i >= avi->movi_end) { /* Let's see if it's an OpenDML AVI */
360 uint32_t tag, size, tag2;
361 url_fskip(pb, avi->riff_end - url_ftell(pb));
362 if (get_riff(avi, pb) < 0)
368 if (tag == MKTAG('L','I','S','T') && tag2 == MKTAG('m','o','v','i'))
369 avi->movi_end = url_ftell(pb) + size - 4;
378 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
381 n= (d[2] - '0') * 10 + (d[3] - '0');
382 if( d[2] >= '0' && d[2] <= '9'
383 && d[3] >= '0' && d[3] <= '9'
384 && d[0] == 'i' && d[1] == 'x'
386 && i + size <= avi->movi_end){
392 n= (d[0] - '0') * 10 + (d[1] - '0');
393 if( d[0] >= '0' && d[0] <= '9'
394 && d[1] >= '0' && d[1] <= '9'
395 && ((d[2] == 'd' && d[3] == 'c') ||
396 (d[2] == 'w' && d[3] == 'b') ||
397 (d[2] == 'd' && d[3] == 'b') ||
398 (d[2] == '_' && d[3] == '_'))
400 && i + size <= avi->movi_end) {
402 av_new_packet(pkt, size);
403 get_buffer(pb, pkt->data, size);
410 dstr = pkt->destruct;
411 size = dv_produce_packet(avi->dv_demux, pkt,
412 pkt->data, pkt->size);
413 pkt->destruct = dstr;
414 pkt->flags |= PKT_FLAG_KEY;
421 /* XXX: how to handle B frames in avi ? */
422 if(st->codec.codec_type == CODEC_TYPE_VIDEO)
423 pkt->pts = ((int64_t)ast->frame_offset * ast->scale* AV_TIME_BASE) / ast->rate;
424 else //FIXME this is proably not correct for all weird avis
425 pkt->pts = ((int64_t)ast->frame_offset * ast->scale* AV_TIME_BASE) / (ast->rate * st->codec.block_align);
426 //printf("%Ld %d %d %d %d\n", pkt->pts, ast->frame_offset, ast->scale, AV_TIME_BASE, ast->rate);
427 pkt->stream_index = n;
428 /* FIXME: We really should read index for that */
429 if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
430 if (ast->frame_offset < ast->nb_index_entries) {
431 if (ast->index_entries[ast->frame_offset].flags & AVIIF_INDEX)
432 pkt->flags |= PKT_FLAG_KEY;
434 /* if no index, better to say that all frames
436 pkt->flags |= PKT_FLAG_KEY;
440 ast->frame_offset += pkt->size;
441 pkt->flags |= PKT_FLAG_KEY;
450 /* XXX: we make the implicit supposition that the position are sorted
452 static int avi_read_idx1(AVFormatContext *s, int size)
454 ByteIOContext *pb = &s->pb;
455 int nb_index_entries, i;
458 AVIIndexEntry *ie, *entries;
459 unsigned int index, tag, flags, pos, len;
461 nb_index_entries = size / 16;
462 if (nb_index_entries <= 0)
465 /* read the entries and sort them in each stream component */
466 for(i = 0; i < nb_index_entries; i++) {
468 flags = get_le32(pb);
471 #if defined(DEBUG_SEEK) && 0
472 printf("%d: tag=0x%x flags=0x%x pos=0x%x len=%d\n",
473 i, tag, flags, pos, len);
475 index = ((tag & 0xff) - '0') * 10;
476 index += ((tag >> 8) & 0xff) - '0';
477 if (index >= s->nb_streams)
479 st = s->streams[index];
482 entries = av_fast_realloc(ast->index_entries,
483 &ast->index_entries_allocated_size,
484 (ast->nb_index_entries + 1) *
485 sizeof(AVIIndexEntry));
487 ast->index_entries = entries;
488 ie = &entries[ast->nb_index_entries++];
491 ie->cum_len = ast->cum_len;
498 static int avi_load_index(AVFormatContext *s)
500 AVIContext *avi = s->priv_data;
501 ByteIOContext *pb = &s->pb;
503 offset_t pos= url_ftell(pb);
505 url_fseek(pb, avi->movi_end, SEEK_SET);
507 printf("movi_end=0x%llx\n", avi->movi_end);
515 printf("tag=%c%c%c%c size=0x%x\n",
523 case MKTAG('i', 'd', 'x', '1'):
524 if (avi_read_idx1(s, size) < 0)
537 url_fseek(pb, pos, SEEK_SET);
541 /* return the index entry whose position is immediately >= 'wanted_pos' */
542 static int locate_frame_in_index(AVIIndexEntry *entries,
543 int nb_entries, int wanted_pos)
551 pos = entries[m].pos;
552 if (pos == wanted_pos)
554 else if (pos > wanted_pos) {
567 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp)
569 AVIContext *avi = s->priv_data;
575 if (!avi->index_loaded) {
576 /* we only load the index on demand */
578 avi->index_loaded = 1;
580 if (stream_index < 0) {
581 for(i = 0; i < s->nb_streams; i++) {
583 if (st->codec.codec_type == CODEC_TYPE_VIDEO)
591 st = s->streams[stream_index];
592 if (st->codec.codec_type != CODEC_TYPE_VIDEO)
595 /* compute the frame number */
596 frame_number = (timestamp * ast->rate) /
597 (ast->scale * (int64_t)AV_TIME_BASE);
599 printf("timestamp=%0.3f nb_indexes=%d frame_number=%d\n",
600 (double)timestamp / AV_TIME_BASE,
601 ast->nb_index_entries, frame_number);
603 /* find a closest key frame before */
604 if (frame_number >= ast->nb_index_entries)
606 while (frame_number >= 0 &&
607 !(ast->index_entries[frame_number].flags & AVIIF_INDEX))
609 if (frame_number < 0)
611 ast->new_frame_offset = frame_number;
613 /* find the position */
614 pos = ast->index_entries[frame_number].pos;
617 printf("key_frame_number=%d pos=0x%llx\n",
621 /* update the frame counters for all the other stream by looking
622 at the positions just after the one found */
623 for(i = 0; i < s->nb_streams; i++) {
625 if (i != stream_index) {
628 if (ast->nb_index_entries <= 0)
630 j = locate_frame_in_index(ast->index_entries,
631 ast->nb_index_entries,
634 if ((j + 1) < ast->nb_index_entries)
636 /* extract the current frame number */
637 if (st->codec.codec_type == CODEC_TYPE_VIDEO)
638 ast->new_frame_offset = j;
640 ast->new_frame_offset = ast->index_entries[j].cum_len;
644 /* everything is OK now. We can update the frame offsets */
645 for(i = 0; i < s->nb_streams; i++) {
648 ast->frame_offset = ast->new_frame_offset;
650 printf("%d: frame_offset=%d\n", i,
655 pos += avi->movi_list;
656 url_fseek(&s->pb, pos, SEEK_SET);
660 static int avi_read_close(AVFormatContext *s)
663 AVIContext *avi = s->priv_data;
665 for(i=0;i<s->nb_streams;i++) {
666 AVStream *st = s->streams[i];
667 AVIStream *ast = st->priv_data;
669 av_free(ast->index_entries);
672 av_free(st->codec.extradata);
673 av_free(st->codec.palctrl);
677 av_free(avi->dv_demux);
682 static int avi_probe(AVProbeData *p)
684 /* check file header */
685 if (p->buf_size <= 32)
687 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
688 p->buf[2] == 'F' && p->buf[3] == 'F' &&
689 p->buf[8] == 'A' && p->buf[9] == 'V' &&
690 p->buf[10] == 'I' && p->buf[11] == ' ')
691 return AVPROBE_SCORE_MAX;
696 static AVInputFormat avi_iformat = {
707 int avidec_init(void)
709 av_register_input_format(&avi_iformat);