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) */
46 int prefix; ///< normally 'd'<<8 + 'c' or 'w'<<8 + 'b'
56 DVDemuxContext* dv_demux;
59 static int avi_load_index(AVFormatContext *s);
62 static void print_tag(const char *str, unsigned int tag, int size)
64 printf("%s: tag=%c%c%c%c size=0x%x\n",
73 static int get_riff(AVIContext *avi, ByteIOContext *pb)
76 /* check RIFF header */
79 if (tag != MKTAG('R', 'I', 'F', 'F'))
81 avi->riff_end = get_le32(pb); /* RIFF chunk size */
82 avi->riff_end += url_ftell(pb); /* RIFF chunk end */
84 if (tag != MKTAG('A', 'V', 'I', ' ') && tag != MKTAG('A', 'V', 'I', 'X'))
90 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
92 AVIContext *avi = s->priv_data;
93 ByteIOContext *pb = &s->pb;
94 uint32_t tag, tag1, handler;
95 int codec_type, stream_index, frame_period, bit_rate, scale, rate;
96 unsigned int size, nb_frames;
100 int xan_video = 0; /* hack to support Xan A/V */
102 if (get_riff(avi, pb) < 0)
115 print_tag("tag", tag, size);
119 case MKTAG('L', 'I', 'S', 'T'):
120 /* ignored, except when start of video packets */
123 print_tag("list", tag1, 0);
125 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
126 avi->movi_list = url_ftell(pb) - 4;
127 if(size) avi->movi_end = avi->movi_list + size;
128 else avi->movi_end = url_filesize(url_fileno(pb));
130 printf("movi end=%Lx\n", avi->movi_end);
135 case MKTAG('d', 'm', 'l', 'h'):
137 url_fskip(pb, size + (size & 1));
139 case MKTAG('a', 'v', 'i', 'h'):
141 /* using frame_period is bad idea */
142 frame_period = get_le32(pb);
143 bit_rate = get_le32(pb) * 8;
144 url_fskip(pb, 4 * 4);
148 st = av_new_stream(s, i);
152 ast = av_mallocz(sizeof(AVIStream));
157 url_fskip(pb, size - 7 * 4);
159 case MKTAG('s', 't', 'r', 'h'):
163 handler = get_le32(pb); /* codec tag */
165 print_tag("strh", tag1, -1);
168 case MKTAG('i', 'a', 'v', 's'):
169 case MKTAG('i', 'v', 'a', 's'):
171 * After some consideration -- I don't think we
172 * have to support anything but DV in a type1 AVIs.
174 if (s->nb_streams != 1)
177 if (handler != MKTAG('d', 'v', 's', 'd') &&
178 handler != MKTAG('d', 'v', 'h', 'd') &&
179 handler != MKTAG('d', 'v', 's', 'l'))
182 ast = s->streams[0]->priv_data;
183 av_freep(&s->streams[0]->codec.extradata);
184 av_freep(&s->streams[0]);
186 avi->dv_demux = dv_init_demux(s);
189 s->streams[0]->priv_data = ast;
190 url_fskip(pb, 3 * 4);
191 ast->scale = get_le32(pb);
192 ast->rate = get_le32(pb);
193 stream_index = s->nb_streams - 1;
194 url_fskip(pb, size - 7*4);
196 case MKTAG('v', 'i', 'd', 's'):
197 codec_type = CODEC_TYPE_VIDEO;
199 if (stream_index >= s->nb_streams) {
200 url_fskip(pb, size - 8);
204 st = s->streams[stream_index];
206 st->codec.stream_codec_tag= handler;
208 get_le32(pb); /* flags */
209 get_le16(pb); /* priority */
210 get_le16(pb); /* language */
211 get_le32(pb); /* XXX: initial frame ? */
212 scale = get_le32(pb); /* scale */
213 rate = get_le32(pb); /* rate */
216 }else if(frame_period){
218 scale = frame_period;
225 av_set_pts_info(st, 64, scale, rate);
226 st->codec.frame_rate = rate;
227 st->codec.frame_rate_base = scale;
228 get_le32(pb); /* start */
229 nb_frames = get_le32(pb);
231 st->duration = av_rescale(nb_frames,
232 st->codec.frame_rate_base * AV_TIME_BASE,
233 st->codec.frame_rate);
234 url_fskip(pb, size - 9 * 4);
236 case MKTAG('a', 'u', 'd', 's'):
240 codec_type = CODEC_TYPE_AUDIO;
242 if (stream_index >= s->nb_streams) {
243 url_fskip(pb, size - 8);
246 st = s->streams[stream_index];
249 get_le32(pb); /* flags */
250 get_le16(pb); /* priority */
251 get_le16(pb); /* language */
252 get_le32(pb); /* initial frame */
253 ast->scale = get_le32(pb); /* scale */
254 ast->rate = get_le32(pb);
256 ast->rate= 1; //wrong but better then 1/0
257 av_set_pts_info(st, 64, ast->scale, ast->rate);
258 ast->start= get_le32(pb); /* start */
259 length = get_le32(pb); /* length, in samples or bytes */
260 get_le32(pb); /* buffer size */
261 get_le32(pb); /* quality */
262 ast->sample_size = get_le32(pb); /* sample ssize */
263 //av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d\n", ast->scale, ast->rate, ast->sample_size, ast->start);
266 st->duration = (int64_t)length * AV_TIME_BASE / ast->rate;
267 url_fskip(pb, size - 12 * 4);
270 case MKTAG('t', 'x', 't', 's'):
272 codec_type = CODEC_TYPE_DATA; //CODEC_TYPE_SUB ? FIXME
273 url_fskip(pb, size - 8);
275 case MKTAG('p', 'a', 'd', 's'):
276 codec_type = CODEC_TYPE_UNKNOWN;
277 url_fskip(pb, size - 8);
281 av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1);
285 case MKTAG('s', 't', 'r', 'f'):
287 if (stream_index >= s->nb_streams || avi->dv_demux) {
290 st = s->streams[stream_index];
292 case CODEC_TYPE_VIDEO:
293 get_le32(pb); /* size */
294 st->codec.width = get_le32(pb);
295 st->codec.height = get_le32(pb);
296 get_le16(pb); /* panes */
297 st->codec.bits_per_sample= get_le16(pb); /* depth */
299 get_le32(pb); /* ImageSize */
300 get_le32(pb); /* XPelsPerMeter */
301 get_le32(pb); /* YPelsPerMeter */
302 get_le32(pb); /* ClrUsed */
303 get_le32(pb); /* ClrImportant */
305 st->codec.extradata_size= size - 10*4;
306 st->codec.extradata= av_malloc(st->codec.extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
307 get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
309 if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
312 /* Extract palette from extradata if bpp <= 8 */
313 /* This code assumes that extradata contains only palette */
314 /* This is true for all paletted codecs implemented in ffmpeg */
315 if (st->codec.extradata_size && (st->codec.bits_per_sample <= 8)) {
316 st->codec.palctrl = av_mallocz(sizeof(AVPaletteControl));
317 #ifdef WORDS_BIGENDIAN
318 for (i = 0; i < FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)/4; i++)
319 st->codec.palctrl->palette[i] = bswap_32(((uint32_t*)st->codec.extradata)[i]);
321 memcpy(st->codec.palctrl->palette, st->codec.extradata,
322 FFMIN(st->codec.extradata_size, AVPALETTE_SIZE));
324 st->codec.palctrl->palette_changed = 1;
328 print_tag("video", tag1, 0);
330 st->codec.codec_type = CODEC_TYPE_VIDEO;
331 st->codec.codec_tag = tag1;
332 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
333 if (st->codec.codec_id == CODEC_ID_XAN_WC4)
335 // url_fskip(pb, size - 5 * 4);
337 case CODEC_TYPE_AUDIO:
338 get_wav_header(pb, &st->codec, size);
339 if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
341 /* special case time: To support Xan DPCM, hardcode
342 * the format if Xxan is the video codec */
343 st->need_parsing = 1;
344 /* force parsing as several audio frames can be in
347 st->codec.codec_id = CODEC_ID_XAN_DPCM;
350 st->codec.codec_type = CODEC_TYPE_DATA;
351 st->codec.codec_id= CODEC_ID_NONE;
352 st->codec.codec_tag= 0;
366 /* check stream number */
367 if (stream_index != s->nb_streams - 1) {
369 for(i=0;i<s->nb_streams;i++) {
370 av_freep(&s->streams[i]->codec.extradata);
371 av_freep(&s->streams[i]);
376 assert(!avi->index_loaded);
378 avi->index_loaded = 1;
383 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
385 AVIContext *avi = s->priv_data;
386 ByteIOContext *pb = &s->pb;
392 size = dv_get_packet(avi->dv_demux, pkt);
398 memset(d, -1, sizeof(int)*8);
399 for(i=sync=url_ftell(pb); !url_feof(pb); i++) {
402 if (i >= avi->movi_end) {
404 url_fskip(pb, avi->riff_end - i);
405 avi->riff_end = avi->movi_end = url_filesize(url_fileno(pb));
414 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
416 if( d[2] >= '0' && d[2] <= '9'
417 && d[3] >= '0' && d[3] <= '9'){
418 n= (d[2] - '0') * 10 + (d[3] - '0');
420 n= 100; //invalid stream id
422 //av_log(NULL, AV_LOG_DEBUG, "%X %X %X %X %X %X %X %X %lld %d %d\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
423 if(i + size > avi->movi_end || d[0]<0)
427 if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
429 ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')){
431 //av_log(NULL, AV_LOG_DEBUG, "SKIP\n");
435 if( d[0] >= '0' && d[0] <= '9'
436 && d[1] >= '0' && d[1] <= '9'){
437 n= (d[0] - '0') * 10 + (d[1] - '0');
439 n= 100; //invalid stream id
443 if(n < s->nb_streams){
449 if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
450 d[2]*256+d[3] == ast->prefix /*||
451 (d[2] == 'd' && d[3] == 'c') ||
452 (d[2] == 'w' && d[3] == 'b')*/) {
454 //av_log(NULL, AV_LOG_DEBUG, "OK\n");
455 if(d[2]*256+d[3] == ast->prefix)
458 ast->prefix= d[2]*256+d[3];
459 ast->prefix_count= 0;
462 av_new_packet(pkt, size);
463 get_buffer(pb, pkt->data, size);
470 dstr = pkt->destruct;
471 size = dv_produce_packet(avi->dv_demux, pkt,
472 pkt->data, pkt->size);
473 pkt->destruct = dstr;
474 pkt->flags |= PKT_FLAG_KEY;
476 /* XXX: how to handle B frames in avi ? */
477 pkt->dts = ast->frame_offset;
478 // pkt->dts += ast->start;
480 pkt->dts /= ast->sample_size;
481 //av_log(NULL, AV_LOG_DEBUG, "dts:%Ld offset:%d %d/%d smpl_siz:%d base:%d st:%d size:%d\n", pkt->dts, ast->frame_offset, ast->scale, ast->rate, ast->sample_size, AV_TIME_BASE, n, size);
482 pkt->stream_index = n;
483 /* FIXME: We really should read index for that */
484 if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
485 if (ast->frame_offset < ast->nb_index_entries) {
486 if (ast->index_entries[ast->frame_offset].flags & AVIIF_INDEX)
487 pkt->flags |= PKT_FLAG_KEY;
489 /* if no index, better to say that all frames
491 pkt->flags |= PKT_FLAG_KEY;
494 pkt->flags |= PKT_FLAG_KEY;
497 ast->frame_offset += pkt->size;
504 /* palette changed chunk */
505 if ( d[0] >= '0' && d[0] <= '9'
506 && d[1] >= '0' && d[1] <= '9'
507 && ((d[2] == 'p' && d[3] == 'c'))
508 && n < s->nb_streams && i + size <= avi->movi_end) {
511 int first, clr, flags, k, p;
515 first = get_byte(pb);
517 if(!clr) /* all 256 colors used */
519 flags = get_le16(pb);
521 for (k = first; k < clr + first; k++) {
527 st->codec.palctrl->palette[k] = b + (g << 8) + (r << 16);
529 st->codec.palctrl->palette_changed = 1;
538 /* XXX: we make the implicit supposition that the position are sorted
540 static int avi_read_idx1(AVFormatContext *s, int size)
542 ByteIOContext *pb = &s->pb;
543 int nb_index_entries, i;
546 AVIIndexEntry *ie, *entries;
547 unsigned int index, tag, flags, pos, len;
549 nb_index_entries = size / 16;
550 if (nb_index_entries <= 0)
553 /* read the entries and sort them in each stream component */
554 for(i = 0; i < nb_index_entries; i++) {
556 flags = get_le32(pb);
559 #if defined(DEBUG_SEEK) && 0
560 printf("%d: tag=0x%x flags=0x%x pos=0x%x len=%d\n",
561 i, tag, flags, pos, len);
563 index = ((tag & 0xff) - '0') * 10;
564 index += ((tag >> 8) & 0xff) - '0';
565 if (index >= s->nb_streams)
567 st = s->streams[index];
570 entries = av_fast_realloc(ast->index_entries,
571 &ast->index_entries_allocated_size,
572 (ast->nb_index_entries + 1) *
573 sizeof(AVIIndexEntry));
575 ast->index_entries = entries;
576 ie = &entries[ast->nb_index_entries++];
579 ie->cum_len = ast->cum_len;
586 static int avi_load_index(AVFormatContext *s)
588 AVIContext *avi = s->priv_data;
589 ByteIOContext *pb = &s->pb;
591 offset_t pos= url_ftell(pb);
593 url_fseek(pb, avi->movi_end, SEEK_SET);
595 printf("movi_end=0x%llx\n", avi->movi_end);
603 printf("tag=%c%c%c%c size=0x%x\n",
611 case MKTAG('i', 'd', 'x', '1'):
612 if (avi_read_idx1(s, size) < 0)
625 url_fseek(pb, pos, SEEK_SET);
629 /* return the index entry whose position is immediately >= 'wanted_pos' */
630 static int locate_frame_in_index(AVIIndexEntry *entries,
631 int nb_entries, int wanted_pos)
639 pos = entries[m].pos;
640 if (pos == wanted_pos)
642 else if (pos > wanted_pos) {
655 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
657 AVIContext *avi = s->priv_data;
663 if (!avi->index_loaded) {
664 /* we only load the index on demand */
666 avi->index_loaded = 1;
668 if (stream_index < 0) {
669 for(i = 0; i < s->nb_streams; i++) {
671 if (st->codec.codec_type == CODEC_TYPE_VIDEO)
679 st = s->streams[stream_index];
680 if (st->codec.codec_type != CODEC_TYPE_VIDEO)
683 /* compute the frame number */
684 frame_number = timestamp;
686 printf("timestamp=%0.3f nb_indexes=%d frame_number=%d\n",
687 (double)timestamp / AV_TIME_BASE,
688 ast->nb_index_entries, frame_number);
690 /* find a closest key frame before */
691 if (frame_number >= ast->nb_index_entries)
693 while (frame_number >= 0 &&
694 !(ast->index_entries[frame_number].flags & AVIIF_INDEX))
696 if (frame_number < 0)
698 ast->new_frame_offset = frame_number;
700 /* find the position */
701 pos = ast->index_entries[frame_number].pos;
704 printf("key_frame_number=%d pos=0x%llx\n",
708 /* update the frame counters for all the other stream by looking
709 at the positions just after the one found */
710 for(i = 0; i < s->nb_streams; i++) {
712 if (i != stream_index) {
715 if (ast->nb_index_entries <= 0)
717 j = locate_frame_in_index(ast->index_entries,
718 ast->nb_index_entries,
721 if ((j + 1) < ast->nb_index_entries)
723 /* extract the current frame number */
724 if (ast->sample_size==0)
725 ast->new_frame_offset = j;
727 ast->new_frame_offset = ast->index_entries[j].cum_len;
731 /* everything is OK now. We can update the frame offsets */
732 for(i = 0; i < s->nb_streams; i++) {
735 ast->frame_offset = ast->new_frame_offset;
737 printf("%d: frame_offset=%d\n", i,
742 dv_flush_audio_packets(avi->dv_demux);
744 pos += avi->movi_list;
745 url_fseek(&s->pb, pos, SEEK_SET);
749 static int avi_read_close(AVFormatContext *s)
752 AVIContext *avi = s->priv_data;
754 for(i=0;i<s->nb_streams;i++) {
755 AVStream *st = s->streams[i];
756 AVIStream *ast = st->priv_data;
758 av_free(ast->index_entries);
761 av_free(st->codec.extradata);
762 av_free(st->codec.palctrl);
766 av_free(avi->dv_demux);
771 static int avi_probe(AVProbeData *p)
773 /* check file header */
774 if (p->buf_size <= 32)
776 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
777 p->buf[2] == 'F' && p->buf[3] == 'F' &&
778 p->buf[8] == 'A' && p->buf[9] == 'V' &&
779 p->buf[10] == 'I' && p->buf[11] == ' ')
780 return AVPROBE_SCORE_MAX;
785 static AVInputFormat avi_iformat = {
796 int avidec_init(void)
798 av_register_input_format(&avi_iformat);