]> git.sesse.net Git - ffmpeg/blob - libavformat/avidec.c
skip subtitle streams instead of perishing
[ffmpeg] / libavformat / avidec.c
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 #include "dv.h"
22
23 //#define DEBUG
24 //#define DEBUG_SEEK
25
26 typedef struct AVIIndexEntry {
27     unsigned int flags;
28     unsigned int pos;
29     unsigned int cum_len; /* sum of all lengths before this packet */
30 } AVIIndexEntry;
31
32 typedef struct AVIStream {
33     AVIIndexEntry *index_entries;
34     int nb_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) */
38     int scale;
39     int rate;    
40     int sample_size; /* audio only data */
41     int start;
42     
43     int new_frame_offset; /* temporary storage (used during seek) */
44     int cum_len; /* temporary storage (used during seek) */
45 } AVIStream;
46
47 typedef struct {
48     int64_t  riff_end;
49     int64_t  movi_end;
50     offset_t movi_list;
51     int index_loaded;
52     DVDemuxContext* dv_demux;
53 } AVIContext;
54
55 static int avi_load_index(AVFormatContext *s);
56
57 #ifdef DEBUG
58 static void print_tag(const char *str, unsigned int tag, int size)
59 {
60     printf("%s: tag=%c%c%c%c size=0x%x\n",
61            str, tag & 0xff,
62            (tag >> 8) & 0xff,
63            (tag >> 16) & 0xff,
64            (tag >> 24) & 0xff,
65            size);
66 }
67 #endif
68
69 static int get_riff(AVIContext *avi, ByteIOContext *pb)
70 {
71     uint32_t tag; 
72     /* check RIFF header */
73     tag = get_le32(pb);
74
75     if (tag != MKTAG('R', 'I', 'F', 'F'))
76         return -1;
77     avi->riff_end = get_le32(pb);   /* RIFF chunk size */
78     avi->riff_end += url_ftell(pb); /* RIFF chunk end */
79     tag = get_le32(pb);
80     if (tag != MKTAG('A', 'V', 'I', ' ') && tag != MKTAG('A', 'V', 'I', 'X'))
81         return -1;
82     
83     return 0;
84 }
85
86 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
87 {
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;
93     int i, n;
94     AVStream *st;
95     AVIStream *ast;
96     int xan_video = 0;  /* hack to support Xan A/V */
97
98     if (get_riff(avi, pb) < 0)
99         return -1;
100
101     /* first list tag */
102     stream_index = -1;
103     codec_type = -1;
104     frame_period = 0;
105     for(;;) {
106         if (url_feof(pb))
107             goto fail;
108         tag = get_le32(pb);
109         size = get_le32(pb);
110 #ifdef DEBUG
111         print_tag("tag", tag, size);
112 #endif
113
114         switch(tag) {
115         case MKTAG('L', 'I', 'S', 'T'):
116             /* ignored, except when start of video packets */
117             tag1 = get_le32(pb);
118 #ifdef DEBUG
119             print_tag("list", tag1, 0);
120 #endif
121             if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
122                 avi->movi_list = url_ftell(pb) - 4;
123                 avi->movi_end = avi->movi_list + size;
124 #ifdef DEBUG
125                 printf("movi end=%Lx\n", avi->movi_end);
126 #endif
127                 goto end_of_header;
128             }
129             break;
130         case MKTAG('a', 'v', 'i', 'h'):
131             /* avi header */
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);
136             n = get_le32(pb);
137             for(i=0;i<n;i++) {
138                 AVIStream *ast;
139                 st = av_new_stream(s, i);
140                 if (!st)
141                     goto fail;
142
143                 ast = av_mallocz(sizeof(AVIStream));
144                 if (!ast)
145                     goto fail;
146                 st->priv_data = ast;
147             }
148             url_fskip(pb, size - 7 * 4);
149             break;
150         case MKTAG('s', 't', 'r', 'h'):
151             /* stream header */
152             stream_index++;
153             tag1 = get_le32(pb);
154             handler = get_le32(pb); /* codec tag */
155 #ifdef DEBUG
156         print_tag("strh", tag1, -1);
157 #endif
158             switch(tag1) {
159             case MKTAG('i', 'a', 'v', 's'):
160             case MKTAG('i', 'v', 'a', 's'):
161                 /* 
162                  * After some consideration -- I don't think we 
163                  * have to support anything but DV in a type1 AVIs.
164                  */
165                 if (s->nb_streams != 1)
166                     goto fail;
167                 
168                 if (handler != MKTAG('d', 'v', 's', 'd') &&
169                     handler != MKTAG('d', 'v', 'h', 'd') &&
170                     handler != MKTAG('d', 'v', 's', 'l'))
171                    goto fail;
172                 
173                 av_freep(&s->streams[0]->codec.extradata);
174                 av_freep(&s->streams[0]);
175                 s->nb_streams = 0;
176                 avi->dv_demux = dv_init_demux(s);
177                 if (!avi->dv_demux)
178                     goto fail;
179                 stream_index = s->nb_streams - 1;
180                 url_fskip(pb, size - 8);
181                 break;
182             case MKTAG('v', 'i', 'd', 's'):
183                 codec_type = CODEC_TYPE_VIDEO;
184
185                 if (stream_index >= s->nb_streams) {
186                     url_fskip(pb, size - 8);
187                     break;
188                 } 
189
190                 st = s->streams[stream_index];
191                 ast = st->priv_data;
192                 st->codec.stream_codec_tag= handler;
193                 
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 */
200
201                 if(scale && rate){
202                 }else if(frame_period){
203                     rate = 1000000;
204                     scale = frame_period;
205                 }else{
206                     rate = 25;
207                     scale = 1;
208                 }
209                 ast->rate = rate;
210                 ast->scale = scale;
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);
216                 st->start_time = 0;
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);
221                 break;
222             case MKTAG('a', 'u', 'd', 's'):
223                 {
224                     unsigned int length;
225
226                     codec_type = CODEC_TYPE_AUDIO;
227
228                     if (stream_index >= s->nb_streams) {
229                         url_fskip(pb, size - 8);
230                         break;
231                     } 
232                     st = s->streams[stream_index];
233                     ast = st->priv_data;
234                     
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);
248                     st->start_time = 0;
249                     if (ast->rate != 0)
250                         st->duration = (int64_t)length * AV_TIME_BASE / ast->rate;
251                     url_fskip(pb, size - 12 * 4);
252                 }
253                 break;
254             case MKTAG('t', 'x', 't', 's'):
255                 //FIXME 
256                 codec_type = CODEC_TYPE_DATA; //CODEC_TYPE_SUB ?  FIXME
257                 url_fskip(pb, size - 8);
258                 break;
259             default:
260                 goto fail;
261             }
262             break;
263         case MKTAG('s', 't', 'r', 'f'):
264             /* stream header */
265             if (stream_index >= s->nb_streams || avi->dv_demux) {
266                 url_fskip(pb, size);
267             } else {
268                 st = s->streams[stream_index];
269                 switch(codec_type) {
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 */
276                     tag1 = get_le32(pb);
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 */
282
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);
286                     
287                     if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
288                         get_byte(pb);
289
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]);
298 #else
299                         memcpy(st->codec.palctrl->palette, st->codec.extradata,
300                                FFMIN(st->codec.extradata_size, AVPALETTE_SIZE));
301 #endif
302                         st->codec.palctrl->palette_changed = 1;
303                     }
304
305 #ifdef DEBUG
306                     print_tag("video", tag1, 0);
307 #endif
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)
312                         xan_video = 1;
313 //                    url_fskip(pb, size - 5 * 4);
314                     break;
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) */
318                         url_fskip(pb, 1);
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
323                        one packet */
324                     if (xan_video)
325                         st->codec.codec_id = CODEC_ID_XAN_DPCM;
326                     break;
327                 default:
328                     url_fskip(pb, size);
329                     break;
330                 }
331             }
332             break;
333         default:
334             /* skip tag */
335             size += (size & 1);
336             url_fskip(pb, size);
337             break;
338         }
339     }
340  end_of_header:
341     /* check stream number */
342     if (stream_index != s->nb_streams - 1) {
343     fail:
344         for(i=0;i<s->nb_streams;i++) {
345             av_freep(&s->streams[i]->codec.extradata);
346             av_freep(&s->streams[i]);
347         }
348         return -1;
349     }
350
351     assert(!avi->index_loaded);
352     avi_load_index(s);
353     avi->index_loaded = 1;
354  
355     return 0;
356 }
357
358 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
359 {
360     AVIContext *avi = s->priv_data;
361     ByteIOContext *pb = &s->pb;
362     int n, d[8], size, i;
363     void* dstr;
364
365     memset(d, -1, sizeof(int)*8);
366    
367     if (avi->dv_demux) {
368         size = dv_get_packet(avi->dv_demux, pkt);
369         if (size >= 0)
370             return size;
371     }
372         
373     for(i=url_ftell(pb); !url_feof(pb); i++) {
374         int j;
375
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)
380                 return -1;
381             
382             tag = get_le32(pb);
383             size = get_le32(pb);
384             tag2 = get_le32(pb);
385             if (tag == MKTAG('L','I','S','T') && tag2 == MKTAG('m','o','v','i'))
386                 avi->movi_end = url_ftell(pb) + size - 4; 
387             else
388                 return -1;
389         }
390
391         for(j=0; j<7; j++)
392             d[j]= d[j+1];
393         d[7]= get_byte(pb);
394         
395         size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
396         
397         //parse ix##
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'
402             && n < s->nb_streams
403             && i + size <= avi->movi_end){
404             
405             url_fskip(pb, size);
406         }
407         
408         //parse ##dc/##wb
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] == '_'))
416             && n < s->nb_streams
417             && i + size <= avi->movi_end) {
418         
419             av_new_packet(pkt, size);
420             get_buffer(pb, pkt->data, size);
421             if (size & 1) {
422                 get_byte(pb);
423                 size++;
424             }
425         
426             if (avi->dv_demux) {
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;
432             } else {
433                 AVStream *st;
434                 AVIStream *ast;
435                 st = s->streams[n];
436                 ast = st->priv_data;
437                 
438                 /* XXX: how to handle B frames in avi ? */
439                 pkt->dts = ast->frame_offset;
440 //                pkt->dts += ast->start;
441                 if(ast->sample_size)
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; 
450                     } else {
451                         /* if no index, better to say that all frames
452                            are key frames */
453                         pkt->flags |= PKT_FLAG_KEY;
454                     }
455                 } else {
456                     pkt->flags |= PKT_FLAG_KEY; 
457                 }
458                 if(ast->sample_size)
459                     ast->frame_offset += pkt->size;
460                 else
461                     ast->frame_offset++;
462             }
463             return size;
464         }
465     }
466     return -1;
467 }
468
469 /* XXX: we make the implicit supposition that the position are sorted
470    for each stream */
471 static int avi_read_idx1(AVFormatContext *s, int size)
472 {
473     ByteIOContext *pb = &s->pb;
474     int nb_index_entries, i;
475     AVStream *st;
476     AVIStream *ast;
477     AVIIndexEntry *ie, *entries;
478     unsigned int index, tag, flags, pos, len;
479     
480     nb_index_entries = size / 16;
481     if (nb_index_entries <= 0)
482         return -1;
483
484     /* read the entries and sort them in each stream component */
485     for(i = 0; i < nb_index_entries; i++) {
486         tag = get_le32(pb);
487         flags = get_le32(pb);
488         pos = get_le32(pb);
489         len = 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);
493 #endif
494         index = ((tag & 0xff) - '0') * 10;
495         index += ((tag >> 8) & 0xff) - '0';
496         if (index >= s->nb_streams)
497             continue;
498         st = s->streams[index];
499         ast = st->priv_data;
500         
501         entries = av_fast_realloc(ast->index_entries,
502                                   &ast->index_entries_allocated_size,
503                                   (ast->nb_index_entries + 1) * 
504                                   sizeof(AVIIndexEntry));
505         if (entries) {
506             ast->index_entries = entries;
507             ie = &entries[ast->nb_index_entries++];
508             ie->flags = flags;
509             ie->pos = pos;
510             ie->cum_len = ast->cum_len;
511             ast->cum_len += len;
512         }
513     }
514     return 0;
515 }
516
517 static int avi_load_index(AVFormatContext *s)
518 {
519     AVIContext *avi = s->priv_data;
520     ByteIOContext *pb = &s->pb;
521     uint32_t tag, size;
522     offset_t pos= url_ftell(pb);
523     
524     url_fseek(pb, avi->movi_end, SEEK_SET);
525 #ifdef DEBUG_SEEK
526     printf("movi_end=0x%llx\n", avi->movi_end);
527 #endif
528     for(;;) {
529         if (url_feof(pb))
530             break;
531         tag = get_le32(pb);
532         size = get_le32(pb);
533 #ifdef DEBUG_SEEK
534         printf("tag=%c%c%c%c size=0x%x\n",
535                tag & 0xff,
536                (tag >> 8) & 0xff,
537                (tag >> 16) & 0xff,
538                (tag >> 24) & 0xff,
539                size);
540 #endif
541         switch(tag) {
542         case MKTAG('i', 'd', 'x', '1'):
543             if (avi_read_idx1(s, size) < 0)
544                 goto skip;
545             else
546                 goto the_end;
547             break;
548         default:
549         skip:
550             size += (size & 1);
551             url_fskip(pb, size);
552             break;
553         }
554     }
555  the_end:
556     url_fseek(pb, pos, SEEK_SET);
557     return 0;
558 }
559
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)
563 {
564     int a, b, m, pos;
565     
566     a = 0;
567     b = nb_entries - 1;
568     while (a <= b) {
569         m = (a + b) >> 1;
570         pos = entries[m].pos;
571         if (pos == wanted_pos)
572             goto found;
573         else if (pos > wanted_pos) {
574             b = m - 1;
575         } else {
576             a = m + 1;
577         }
578     }
579     m = a;
580     if (m > 0)
581         m--;
582  found:
583     return m;
584 }
585
586 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp)
587 {
588     AVIContext *avi = s->priv_data;
589     AVStream *st;
590     AVIStream *ast;
591     int frame_number, i;
592     int64_t pos;
593
594     if (!avi->index_loaded) {
595         /* we only load the index on demand */
596         avi_load_index(s);
597         avi->index_loaded = 1;
598     }
599     if (stream_index < 0) {
600         for(i = 0; i < s->nb_streams; i++) {
601             st = s->streams[i];
602             if (st->codec.codec_type == CODEC_TYPE_VIDEO)
603                 goto found;
604         }
605         return -1;
606     found:
607         stream_index = i;
608     }
609
610     st = s->streams[stream_index];
611     if (st->codec.codec_type != CODEC_TYPE_VIDEO)
612         return -1;
613     ast = st->priv_data;
614     /* compute the frame number */
615     frame_number = timestamp;
616 #ifdef DEBUG_SEEK
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);
620 #endif
621     /* find a closest key frame before */
622     if (frame_number >= ast->nb_index_entries)
623         return -1;
624     while (frame_number >= 0 &&
625            !(ast->index_entries[frame_number].flags & AVIIF_INDEX))
626         frame_number--;
627     if (frame_number < 0)
628         return -1;
629     ast->new_frame_offset = frame_number;
630
631     /* find the position */
632     pos = ast->index_entries[frame_number].pos;
633
634 #ifdef DEBUG_SEEK
635     printf("key_frame_number=%d pos=0x%llx\n", 
636            frame_number, pos);
637 #endif
638     
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++) {
642         int j;
643         if (i != stream_index) {
644             st = s->streams[i];
645             ast = st->priv_data;
646             if (ast->nb_index_entries <= 0)
647                 return -1;
648             j = locate_frame_in_index(ast->index_entries,
649                                       ast->nb_index_entries,
650                                       pos);
651             /* get next frame */
652             if ((j  + 1) < ast->nb_index_entries)
653                 j++;
654             /* extract the current frame number */
655             if (ast->sample_size==0)
656                 ast->new_frame_offset = j;
657             else
658                 ast->new_frame_offset = ast->index_entries[j].cum_len;
659         }
660     }
661     
662     /* everything is OK now. We can update the frame offsets */
663     for(i = 0; i < s->nb_streams; i++) {
664         st = s->streams[i];
665         ast = st->priv_data;
666         ast->frame_offset = ast->new_frame_offset;
667 #ifdef DEBUG_SEEK
668         printf("%d: frame_offset=%d\n", i, 
669                ast->frame_offset);
670 #endif
671     }
672     /* do the seek */
673     pos += avi->movi_list;
674     url_fseek(&s->pb, pos, SEEK_SET);
675     return 0;
676 }
677
678 static int avi_read_close(AVFormatContext *s)
679 {
680     int i;
681     AVIContext *avi = s->priv_data;
682
683     for(i=0;i<s->nb_streams;i++) {
684         AVStream *st = s->streams[i];
685         AVIStream *ast = st->priv_data;
686         if(ast){
687             av_free(ast->index_entries);
688             av_free(ast);
689         }
690         av_free(st->codec.extradata);
691         av_free(st->codec.palctrl);
692     }
693
694     if (avi->dv_demux)
695         av_free(avi->dv_demux);
696
697     return 0;
698 }
699
700 static int avi_probe(AVProbeData *p)
701 {
702     /* check file header */
703     if (p->buf_size <= 32)
704         return 0;
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;
710     else
711         return 0;
712 }
713
714 static AVInputFormat avi_iformat = {
715     "avi",
716     "avi format",
717     sizeof(AVIContext),
718     avi_probe,
719     avi_read_header,
720     avi_read_packet,
721     avi_read_close,
722     avi_read_seek,
723 };
724
725 int avidec_init(void)
726 {
727     av_register_input_format(&avi_iformat);
728     return 0;
729 }