OSDN Git Service

avcodec/snow: Initialize spatial_decomposition_count to a valid value
[android-x86/external-ffmpeg.git] / libavformat / avidec.c
1 /*
2  * AVI demuxer
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <inttypes.h>
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/bswap.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mathematics.h"
32 #include "avformat.h"
33 #include "avi.h"
34 #include "dv.h"
35 #include "internal.h"
36 #include "riff.h"
37 #include "libavcodec/bytestream.h"
38 #include "libavcodec/exif.h"
39
40 typedef struct AVIStream {
41     int64_t frame_offset;   /* current frame (video) or byte (audio) counter
42                              * (used to compute the pts) */
43     int remaining;
44     int packet_size;
45
46     uint32_t handler;
47     uint32_t scale;
48     uint32_t rate;
49     int sample_size;        /* size of one sample (or packet)
50                              * (in the rate/scale sense) in bytes */
51
52     int64_t cum_len;        /* temporary storage (used during seek) */
53     int prefix;             /* normally 'd'<<8 + 'c' or 'w'<<8 + 'b' */
54     int prefix_count;
55     uint32_t pal[256];
56     int has_pal;
57     int dshow_block_align;  /* block align variable used to emulate bugs in
58                              * the MS dshow demuxer */
59
60     AVFormatContext *sub_ctx;
61     AVPacket sub_pkt;
62     uint8_t *sub_buffer;
63
64     int64_t seek_pos;
65 } AVIStream;
66
67 typedef struct AVIContext {
68     const AVClass *class;
69     int64_t riff_end;
70     int64_t movi_end;
71     int64_t fsize;
72     int64_t io_fsize;
73     int64_t movi_list;
74     int64_t last_pkt_pos;
75     int index_loaded;
76     int is_odml;
77     int non_interleaved;
78     int stream_index;
79     DVDemuxContext *dv_demux;
80     int odml_depth;
81     int use_odml;
82 #define MAX_ODML_DEPTH 1000
83     int64_t dts_max;
84 } AVIContext;
85
86
87 static const AVOption options[] = {
88     { "use_odml", "use odml index", offsetof(AVIContext, use_odml), AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM},
89     { NULL },
90 };
91
92 static const AVClass demuxer_class = {
93     .class_name = "avi",
94     .item_name  = av_default_item_name,
95     .option     = options,
96     .version    = LIBAVUTIL_VERSION_INT,
97     .category   = AV_CLASS_CATEGORY_DEMUXER,
98 };
99
100
101 static const char avi_headers[][8] = {
102     { 'R', 'I', 'F', 'F', 'A', 'V', 'I', ' '  },
103     { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 'X'  },
104     { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 0x19 },
105     { 'O', 'N', '2', ' ', 'O', 'N', '2', 'f'  },
106     { 'R', 'I', 'F', 'F', 'A', 'M', 'V', ' '  },
107     { 0 }
108 };
109
110 static const AVMetadataConv avi_metadata_conv[] = {
111     { "strn", "title" },
112     { 0 },
113 };
114
115 static int avi_load_index(AVFormatContext *s);
116 static int guess_ni_flag(AVFormatContext *s);
117
118 #define print_tag(str, tag, size)                        \
119     av_dlog(NULL, "pos:%"PRIX64" %s: tag=%c%c%c%c size=0x%x\n", \
120             avio_tell(pb), str, tag & 0xff,              \
121             (tag >> 8) & 0xff,                           \
122             (tag >> 16) & 0xff,                          \
123             (tag >> 24) & 0xff,                          \
124             size)
125
126 static inline int get_duration(AVIStream *ast, int len)
127 {
128     if (ast->sample_size)
129         return len;
130     else if (ast->dshow_block_align > 1)
131         return (len + ast->dshow_block_align - 1) / ast->dshow_block_align;
132     else
133         return 1;
134 }
135
136 static int get_riff(AVFormatContext *s, AVIOContext *pb)
137 {
138     AVIContext *avi = s->priv_data;
139     char header[8];
140     int i;
141
142     /* check RIFF header */
143     avio_read(pb, header, 4);
144     avi->riff_end  = avio_rl32(pb); /* RIFF chunk size */
145     avi->riff_end += avio_tell(pb); /* RIFF chunk end */
146     avio_read(pb, header + 4, 4);
147
148     for (i = 0; avi_headers[i][0]; i++)
149         if (!memcmp(header, avi_headers[i], 8))
150             break;
151     if (!avi_headers[i][0])
152         return AVERROR_INVALIDDATA;
153
154     if (header[7] == 0x19)
155         av_log(s, AV_LOG_INFO,
156                "This file has been generated by a totally broken muxer.\n");
157
158     return 0;
159 }
160
161 static int read_braindead_odml_indx(AVFormatContext *s, int frame_num)
162 {
163     AVIContext *avi     = s->priv_data;
164     AVIOContext *pb     = s->pb;
165     int longs_pre_entry = avio_rl16(pb);
166     int index_sub_type  = avio_r8(pb);
167     int index_type      = avio_r8(pb);
168     int entries_in_use  = avio_rl32(pb);
169     int chunk_id        = avio_rl32(pb);
170     int64_t base        = avio_rl64(pb);
171     int stream_id       = ((chunk_id      & 0xFF) - '0') * 10 +
172                           ((chunk_id >> 8 & 0xFF) - '0');
173     AVStream *st;
174     AVIStream *ast;
175     int i;
176     int64_t last_pos = -1;
177     int64_t filesize = avi->fsize;
178
179     av_dlog(s,
180             "longs_pre_entry:%d index_type:%d entries_in_use:%d "
181             "chunk_id:%X base:%16"PRIX64"\n",
182             longs_pre_entry,
183             index_type,
184             entries_in_use,
185             chunk_id,
186             base);
187
188     if (stream_id >= s->nb_streams || stream_id < 0)
189         return AVERROR_INVALIDDATA;
190     st  = s->streams[stream_id];
191     ast = st->priv_data;
192
193     if (index_sub_type)
194         return AVERROR_INVALIDDATA;
195
196     avio_rl32(pb);
197
198     if (index_type && longs_pre_entry != 2)
199         return AVERROR_INVALIDDATA;
200     if (index_type > 1)
201         return AVERROR_INVALIDDATA;
202
203     if (filesize > 0 && base >= filesize) {
204         av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
205         if (base >> 32 == (base & 0xFFFFFFFF) &&
206             (base & 0xFFFFFFFF) < filesize    &&
207             filesize <= 0xFFFFFFFF)
208             base &= 0xFFFFFFFF;
209         else
210             return AVERROR_INVALIDDATA;
211     }
212
213     for (i = 0; i < entries_in_use; i++) {
214         if (index_type) {
215             int64_t pos = avio_rl32(pb) + base - 8;
216             int len     = avio_rl32(pb);
217             int key     = len >= 0;
218             len &= 0x7FFFFFFF;
219
220 #ifdef DEBUG_SEEK
221             av_log(s, AV_LOG_ERROR, "pos:%"PRId64", len:%X\n", pos, len);
222 #endif
223             if (avio_feof(pb))
224                 return AVERROR_INVALIDDATA;
225
226             if (last_pos == pos || pos == base - 8)
227                 avi->non_interleaved = 1;
228             if (last_pos != pos && len)
229                 av_add_index_entry(st, pos, ast->cum_len, len, 0,
230                                    key ? AVINDEX_KEYFRAME : 0);
231
232             ast->cum_len += get_duration(ast, len);
233             last_pos      = pos;
234         } else {
235             int64_t offset, pos;
236             int duration;
237             offset = avio_rl64(pb);
238             avio_rl32(pb);       /* size */
239             duration = avio_rl32(pb);
240
241             if (avio_feof(pb))
242                 return AVERROR_INVALIDDATA;
243
244             pos = avio_tell(pb);
245
246             if (avi->odml_depth > MAX_ODML_DEPTH) {
247                 av_log(s, AV_LOG_ERROR, "Too deeply nested ODML indexes\n");
248                 return AVERROR_INVALIDDATA;
249             }
250
251             if (avio_seek(pb, offset + 8, SEEK_SET) < 0)
252                 return -1;
253             avi->odml_depth++;
254             read_braindead_odml_indx(s, frame_num);
255             avi->odml_depth--;
256             frame_num += duration;
257
258             if (avio_seek(pb, pos, SEEK_SET) < 0) {
259                 av_log(s, AV_LOG_ERROR, "Failed to restore position after reading index\n");
260                 return -1;
261             }
262
263         }
264     }
265     avi->index_loaded = 2;
266     return 0;
267 }
268
269 static void clean_index(AVFormatContext *s)
270 {
271     int i;
272     int64_t j;
273
274     for (i = 0; i < s->nb_streams; i++) {
275         AVStream *st   = s->streams[i];
276         AVIStream *ast = st->priv_data;
277         int n          = st->nb_index_entries;
278         int max        = ast->sample_size;
279         int64_t pos, size, ts;
280
281         if (n != 1 || ast->sample_size == 0)
282             continue;
283
284         while (max < 1024)
285             max += max;
286
287         pos  = st->index_entries[0].pos;
288         size = st->index_entries[0].size;
289         ts   = st->index_entries[0].timestamp;
290
291         for (j = 0; j < size; j += max)
292             av_add_index_entry(st, pos + j, ts + j, FFMIN(max, size - j), 0,
293                                AVINDEX_KEYFRAME);
294     }
295 }
296
297 static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag,
298                         uint32_t size)
299 {
300     AVIOContext *pb = s->pb;
301     char key[5]     = { 0 };
302     char *value;
303
304     size += (size & 1);
305
306     if (size == UINT_MAX)
307         return AVERROR(EINVAL);
308     value = av_malloc(size + 1);
309     if (!value)
310         return AVERROR(ENOMEM);
311     avio_read(pb, value, size);
312     value[size] = 0;
313
314     AV_WL32(key, tag);
315
316     return av_dict_set(st ? &st->metadata : &s->metadata, key, value,
317                        AV_DICT_DONT_STRDUP_VAL);
318 }
319
320 static const char months[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
321                                     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
322
323 static void avi_metadata_creation_time(AVDictionary **metadata, char *date)
324 {
325     char month[4], time[9], buffer[64];
326     int i, day, year;
327     /* parse standard AVI date format (ie. "Mon Mar 10 15:04:43 2003") */
328     if (sscanf(date, "%*3s%*[ ]%3s%*[ ]%2d%*[ ]%8s%*[ ]%4d",
329                month, &day, time, &year) == 4) {
330         for (i = 0; i < 12; i++)
331             if (!av_strcasecmp(month, months[i])) {
332                 snprintf(buffer, sizeof(buffer), "%.4d-%.2d-%.2d %s",
333                          year, i + 1, day, time);
334                 av_dict_set(metadata, "creation_time", buffer, 0);
335             }
336     } else if (date[4] == '/' && date[7] == '/') {
337         date[4] = date[7] = '-';
338         av_dict_set(metadata, "creation_time", date, 0);
339     }
340 }
341
342 static void avi_read_nikon(AVFormatContext *s, uint64_t end)
343 {
344     while (avio_tell(s->pb) < end) {
345         uint32_t tag  = avio_rl32(s->pb);
346         uint32_t size = avio_rl32(s->pb);
347         switch (tag) {
348         case MKTAG('n', 'c', 't', 'g'):  /* Nikon Tags */
349         {
350             uint64_t tag_end = avio_tell(s->pb) + size;
351             while (avio_tell(s->pb) < tag_end) {
352                 uint16_t tag     = avio_rl16(s->pb);
353                 uint16_t size    = avio_rl16(s->pb);
354                 const char *name = NULL;
355                 char buffer[64]  = { 0 };
356                 size = FFMIN(size, tag_end - avio_tell(s->pb));
357                 size -= avio_read(s->pb, buffer,
358                                   FFMIN(size, sizeof(buffer) - 1));
359                 switch (tag) {
360                 case 0x03:
361                     name = "maker";
362                     break;
363                 case 0x04:
364                     name = "model";
365                     break;
366                 case 0x13:
367                     name = "creation_time";
368                     if (buffer[4] == ':' && buffer[7] == ':')
369                         buffer[4] = buffer[7] = '-';
370                     break;
371                 }
372                 if (name)
373                     av_dict_set(&s->metadata, name, buffer, 0);
374                 avio_skip(s->pb, size);
375             }
376             break;
377         }
378         default:
379             avio_skip(s->pb, size);
380             break;
381         }
382     }
383 }
384
385 static int avi_extract_stream_metadata(AVStream *st)
386 {
387     GetByteContext gb;
388     uint8_t *data = st->codec->extradata;
389     int data_size = st->codec->extradata_size;
390     int tag, offset;
391
392     if (!data || data_size < 8) {
393         return AVERROR_INVALIDDATA;
394     }
395
396     bytestream2_init(&gb, data, data_size);
397
398     tag = bytestream2_get_le32(&gb);
399
400     switch (tag) {
401     case MKTAG('A', 'V', 'I', 'F'):
402         // skip 4 byte padding
403         bytestream2_skip(&gb, 4);
404         offset = bytestream2_tell(&gb);
405         bytestream2_init(&gb, data + offset, data_size - offset);
406
407         // decode EXIF tags from IFD, AVI is always little-endian
408         return avpriv_exif_decode_ifd(st->codec, &gb, 1, 0, &st->metadata);
409         break;
410     case MKTAG('C', 'A', 'S', 'I'):
411         avpriv_request_sample(st->codec, "RIFF stream data tag type CASI (%u)", tag);
412         break;
413     case MKTAG('Z', 'o', 'r', 'a'):
414         avpriv_request_sample(st->codec, "RIFF stream data tag type Zora (%u)", tag);
415         break;
416     default:
417         break;
418     }
419
420     return 0;
421 }
422
423 static int calculate_bitrate(AVFormatContext *s)
424 {
425     AVIContext *avi = s->priv_data;
426     int i, j;
427     int64_t lensum = 0;
428     int64_t maxpos = 0;
429
430     for (i = 0; i<s->nb_streams; i++) {
431         int64_t len = 0;
432         AVStream *st = s->streams[i];
433
434         if (!st->nb_index_entries)
435             continue;
436
437         for (j = 0; j < st->nb_index_entries; j++)
438             len += st->index_entries[j].size;
439         maxpos = FFMAX(maxpos, st->index_entries[j-1].pos);
440         lensum += len;
441     }
442     if (maxpos < avi->io_fsize*9/10) // index does not cover the whole file
443         return 0;
444     if (lensum*9/10 > maxpos || lensum < maxpos*9/10) // frame sum and filesize mismatch
445         return 0;
446
447     for (i = 0; i<s->nb_streams; i++) {
448         int64_t len = 0;
449         AVStream *st = s->streams[i];
450         int64_t duration;
451
452         for (j = 0; j < st->nb_index_entries; j++)
453             len += st->index_entries[j].size;
454
455         if (st->nb_index_entries < 2 || st->codec->bit_rate > 0)
456             continue;
457         duration = st->index_entries[j-1].timestamp - st->index_entries[0].timestamp;
458         st->codec->bit_rate = av_rescale(8*len, st->time_base.den, duration * st->time_base.num);
459     }
460     return 1;
461 }
462
463 static int avi_read_header(AVFormatContext *s)
464 {
465     AVIContext *avi = s->priv_data;
466     AVIOContext *pb = s->pb;
467     unsigned int tag, tag1, handler;
468     int codec_type, stream_index, frame_period;
469     unsigned int size;
470     int i;
471     AVStream *st;
472     AVIStream *ast      = NULL;
473     int avih_width      = 0, avih_height = 0;
474     int amv_file_format = 0;
475     uint64_t list_end   = 0;
476     int ret;
477     AVDictionaryEntry *dict_entry;
478
479     avi->stream_index = -1;
480
481     ret = get_riff(s, pb);
482     if (ret < 0)
483         return ret;
484
485     av_log(avi, AV_LOG_DEBUG, "use odml:%d\n", avi->use_odml);
486
487     avi->io_fsize = avi->fsize = avio_size(pb);
488     if (avi->fsize <= 0 || avi->fsize < avi->riff_end)
489         avi->fsize = avi->riff_end == 8 ? INT64_MAX : avi->riff_end;
490
491     /* first list tag */
492     stream_index = -1;
493     codec_type   = -1;
494     frame_period = 0;
495     for (;;) {
496         if (avio_feof(pb))
497             goto fail;
498         tag  = avio_rl32(pb);
499         size = avio_rl32(pb);
500
501         print_tag("tag", tag, size);
502
503         switch (tag) {
504         case MKTAG('L', 'I', 'S', 'T'):
505             list_end = avio_tell(pb) + size;
506             /* Ignored, except at start of video packets. */
507             tag1 = avio_rl32(pb);
508
509             print_tag("list", tag1, 0);
510
511             if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
512                 avi->movi_list = avio_tell(pb) - 4;
513                 if (size)
514                     avi->movi_end = avi->movi_list + size + (size & 1);
515                 else
516                     avi->movi_end = avi->fsize;
517                 av_dlog(NULL, "movi end=%"PRIx64"\n", avi->movi_end);
518                 goto end_of_header;
519             } else if (tag1 == MKTAG('I', 'N', 'F', 'O'))
520                 ff_read_riff_info(s, size - 4);
521             else if (tag1 == MKTAG('n', 'c', 'd', 't'))
522                 avi_read_nikon(s, list_end);
523
524             break;
525         case MKTAG('I', 'D', 'I', 'T'):
526         {
527             unsigned char date[64] = { 0 };
528             size += (size & 1);
529             size -= avio_read(pb, date, FFMIN(size, sizeof(date) - 1));
530             avio_skip(pb, size);
531             avi_metadata_creation_time(&s->metadata, date);
532             break;
533         }
534         case MKTAG('d', 'm', 'l', 'h'):
535             avi->is_odml = 1;
536             avio_skip(pb, size + (size & 1));
537             break;
538         case MKTAG('a', 'm', 'v', 'h'):
539             amv_file_format = 1;
540         case MKTAG('a', 'v', 'i', 'h'):
541             /* AVI header */
542             /* using frame_period is bad idea */
543             frame_period = avio_rl32(pb);
544             avio_rl32(pb); /* max. bytes per second */
545             avio_rl32(pb);
546             avi->non_interleaved |= avio_rl32(pb) & AVIF_MUSTUSEINDEX;
547
548             avio_skip(pb, 2 * 4);
549             avio_rl32(pb);
550             avio_rl32(pb);
551             avih_width  = avio_rl32(pb);
552             avih_height = avio_rl32(pb);
553
554             avio_skip(pb, size - 10 * 4);
555             break;
556         case MKTAG('s', 't', 'r', 'h'):
557             /* stream header */
558
559             tag1    = avio_rl32(pb);
560             handler = avio_rl32(pb); /* codec tag */
561
562             if (tag1 == MKTAG('p', 'a', 'd', 's')) {
563                 avio_skip(pb, size - 8);
564                 break;
565             } else {
566                 stream_index++;
567                 st = avformat_new_stream(s, NULL);
568                 if (!st)
569                     goto fail;
570
571                 st->id = stream_index;
572                 ast    = av_mallocz(sizeof(AVIStream));
573                 if (!ast)
574                     goto fail;
575                 st->priv_data = ast;
576             }
577             if (amv_file_format)
578                 tag1 = stream_index ? MKTAG('a', 'u', 'd', 's')
579                                     : MKTAG('v', 'i', 'd', 's');
580
581             print_tag("strh", tag1, -1);
582
583             if (tag1 == MKTAG('i', 'a', 'v', 's') ||
584                 tag1 == MKTAG('i', 'v', 'a', 's')) {
585                 int64_t dv_dur;
586
587                 /* After some consideration -- I don't think we
588                  * have to support anything but DV in type1 AVIs. */
589                 if (s->nb_streams != 1)
590                     goto fail;
591
592                 if (handler != MKTAG('d', 'v', 's', 'd') &&
593                     handler != MKTAG('d', 'v', 'h', 'd') &&
594                     handler != MKTAG('d', 'v', 's', 'l'))
595                     goto fail;
596
597                 ast = s->streams[0]->priv_data;
598                 av_freep(&s->streams[0]->codec->extradata);
599                 av_freep(&s->streams[0]->codec);
600                 if (s->streams[0]->info)
601                     av_freep(&s->streams[0]->info->duration_error);
602                 av_freep(&s->streams[0]->info);
603                 av_freep(&s->streams[0]);
604                 s->nb_streams = 0;
605                 if (CONFIG_DV_DEMUXER) {
606                     avi->dv_demux = avpriv_dv_init_demux(s);
607                     if (!avi->dv_demux)
608                         goto fail;
609                 } else
610                     goto fail;
611                 s->streams[0]->priv_data = ast;
612                 avio_skip(pb, 3 * 4);
613                 ast->scale = avio_rl32(pb);
614                 ast->rate  = avio_rl32(pb);
615                 avio_skip(pb, 4);  /* start time */
616
617                 dv_dur = avio_rl32(pb);
618                 if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
619                     dv_dur     *= AV_TIME_BASE;
620                     s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
621                 }
622                 /* else, leave duration alone; timing estimation in utils.c
623                  * will make a guess based on bitrate. */
624
625                 stream_index = s->nb_streams - 1;
626                 avio_skip(pb, size - 9 * 4);
627                 break;
628             }
629
630             av_assert0(stream_index < s->nb_streams);
631             ast->handler = handler;
632
633             avio_rl32(pb); /* flags */
634             avio_rl16(pb); /* priority */
635             avio_rl16(pb); /* language */
636             avio_rl32(pb); /* initial frame */
637             ast->scale = avio_rl32(pb);
638             ast->rate  = avio_rl32(pb);
639             if (!(ast->scale && ast->rate)) {
640                 av_log(s, AV_LOG_WARNING,
641                        "scale/rate is %"PRIu32"/%"PRIu32" which is invalid. "
642                        "(This file has been generated by broken software.)\n",
643                        ast->scale,
644                        ast->rate);
645                 if (frame_period) {
646                     ast->rate  = 1000000;
647                     ast->scale = frame_period;
648                 } else {
649                     ast->rate  = 25;
650                     ast->scale = 1;
651                 }
652             }
653             avpriv_set_pts_info(st, 64, ast->scale, ast->rate);
654
655             ast->cum_len  = avio_rl32(pb); /* start */
656             st->nb_frames = avio_rl32(pb);
657
658             st->start_time = 0;
659             avio_rl32(pb); /* buffer size */
660             avio_rl32(pb); /* quality */
661             if (ast->cum_len*ast->scale/ast->rate > 3600) {
662                 av_log(s, AV_LOG_ERROR, "crazy start time, iam scared, giving up\n");
663                 ast->cum_len = 0;
664             }
665             ast->sample_size = avio_rl32(pb); /* sample ssize */
666             ast->cum_len    *= FFMAX(1, ast->sample_size);
667             av_dlog(s, "%"PRIu32" %"PRIu32" %d\n",
668                     ast->rate, ast->scale, ast->sample_size);
669
670             switch (tag1) {
671             case MKTAG('v', 'i', 'd', 's'):
672                 codec_type = AVMEDIA_TYPE_VIDEO;
673
674                 ast->sample_size = 0;
675                 st->avg_frame_rate = av_inv_q(st->time_base);
676                 break;
677             case MKTAG('a', 'u', 'd', 's'):
678                 codec_type = AVMEDIA_TYPE_AUDIO;
679                 break;
680             case MKTAG('t', 'x', 't', 's'):
681                 codec_type = AVMEDIA_TYPE_SUBTITLE;
682                 break;
683             case MKTAG('d', 'a', 't', 's'):
684                 codec_type = AVMEDIA_TYPE_DATA;
685                 break;
686             default:
687                 av_log(s, AV_LOG_INFO, "unknown stream type %X\n", tag1);
688             }
689             if (ast->sample_size == 0) {
690                 st->duration = st->nb_frames;
691                 if (st->duration > 0 && avi->io_fsize > 0 && avi->riff_end > avi->io_fsize) {
692                     av_log(s, AV_LOG_DEBUG, "File is truncated adjusting duration\n");
693                     st->duration = av_rescale(st->duration, avi->io_fsize, avi->riff_end);
694                 }
695             }
696             ast->frame_offset = ast->cum_len;
697             avio_skip(pb, size - 12 * 4);
698             break;
699         case MKTAG('s', 't', 'r', 'f'):
700             /* stream header */
701             if (!size)
702                 break;
703             if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
704                 avio_skip(pb, size);
705             } else {
706                 uint64_t cur_pos = avio_tell(pb);
707                 unsigned esize;
708                 if (cur_pos < list_end)
709                     size = FFMIN(size, list_end - cur_pos);
710                 st = s->streams[stream_index];
711                 if (st->codec->codec_type != AVMEDIA_TYPE_UNKNOWN) {
712                     avio_skip(pb, size);
713                     break;
714                 }
715                 switch (codec_type) {
716                 case AVMEDIA_TYPE_VIDEO:
717                     if (amv_file_format) {
718                         st->codec->width      = avih_width;
719                         st->codec->height     = avih_height;
720                         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
721                         st->codec->codec_id   = AV_CODEC_ID_AMV;
722                         avio_skip(pb, size);
723                         break;
724                     }
725                     tag1 = ff_get_bmp_header(pb, st, &esize);
726
727                     if (tag1 == MKTAG('D', 'X', 'S', 'B') ||
728                         tag1 == MKTAG('D', 'X', 'S', 'A')) {
729                         st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
730                         st->codec->codec_tag  = tag1;
731                         st->codec->codec_id   = AV_CODEC_ID_XSUB;
732                         break;
733                     }
734
735                     if (size > 10 * 4 && size < (1 << 30) && size < avi->fsize) {
736                         if (esize == size-1 && (esize&1)) {
737                             st->codec->extradata_size = esize - 10 * 4;
738                         } else
739                             st->codec->extradata_size =  size - 10 * 4;
740                         if (ff_get_extradata(st->codec, pb, st->codec->extradata_size) < 0)
741                             return AVERROR(ENOMEM);
742                     }
743
744                     // FIXME: check if the encoder really did this correctly
745                     if (st->codec->extradata_size & 1)
746                         avio_r8(pb);
747
748                     /* Extract palette from extradata if bpp <= 8.
749                      * This code assumes that extradata contains only palette.
750                      * This is true for all paletted codecs implemented in
751                      * FFmpeg. */
752                     if (st->codec->extradata_size &&
753                         (st->codec->bits_per_coded_sample <= 8)) {
754                         int pal_size = (1 << st->codec->bits_per_coded_sample) << 2;
755                         const uint8_t *pal_src;
756
757                         pal_size = FFMIN(pal_size, st->codec->extradata_size);
758                         pal_src  = st->codec->extradata +
759                                    st->codec->extradata_size - pal_size;
760                         /* Exclude the "BottomUp" field from the palette */
761                         if (pal_src - st->codec->extradata >= 9 &&
762                             !memcmp(st->codec->extradata + st->codec->extradata_size - 9, "BottomUp", 9))
763                             pal_src -= 9;
764                         for (i = 0; i < pal_size / 4; i++)
765                             ast->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
766                         ast->has_pal = 1;
767                     }
768
769                     print_tag("video", tag1, 0);
770
771                     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
772                     st->codec->codec_tag  = tag1;
773                     st->codec->codec_id   = ff_codec_get_id(ff_codec_bmp_tags,
774                                                             tag1);
775                     /* This is needed to get the pict type which is necessary
776                      * for generating correct pts. */
777                     st->need_parsing = AVSTREAM_PARSE_HEADERS;
778
779                     if (st->codec->codec_id == AV_CODEC_ID_MPEG4 &&
780                         ast->handler == MKTAG('X', 'V', 'I', 'D'))
781                         st->codec->codec_tag = MKTAG('X', 'V', 'I', 'D');
782
783                     if (st->codec->codec_tag == MKTAG('V', 'S', 'S', 'H'))
784                         st->need_parsing = AVSTREAM_PARSE_FULL;
785
786                     if (st->codec->codec_tag == 0 && st->codec->height > 0 &&
787                         st->codec->extradata_size < 1U << 30) {
788                         st->codec->extradata_size += 9;
789                         if ((ret = av_reallocp(&st->codec->extradata,
790                                                st->codec->extradata_size +
791                                                FF_INPUT_BUFFER_PADDING_SIZE)) < 0) {
792                             st->codec->extradata_size = 0;
793                             return ret;
794                         } else
795                             memcpy(st->codec->extradata + st->codec->extradata_size - 9,
796                                    "BottomUp", 9);
797                     }
798                     st->codec->height = FFABS(st->codec->height);
799
800 //                    avio_skip(pb, size - 5 * 4);
801                     break;
802                 case AVMEDIA_TYPE_AUDIO:
803                     ret = ff_get_wav_header(pb, st->codec, size, 0);
804                     if (ret < 0)
805                         return ret;
806                     ast->dshow_block_align = st->codec->block_align;
807                     if (ast->sample_size && st->codec->block_align &&
808                         ast->sample_size != st->codec->block_align) {
809                         av_log(s,
810                                AV_LOG_WARNING,
811                                "sample size (%d) != block align (%d)\n",
812                                ast->sample_size,
813                                st->codec->block_align);
814                         ast->sample_size = st->codec->block_align;
815                     }
816                     /* 2-aligned
817                      * (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
818                     if (size & 1)
819                         avio_skip(pb, 1);
820                     /* Force parsing as several audio frames can be in
821                      * one packet and timestamps refer to packet start. */
822                     st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
823                     /* ADTS header is in extradata, AAC without header must be
824                      * stored as exact frames. Parser not needed and it will
825                      * fail. */
826                     if (st->codec->codec_id == AV_CODEC_ID_AAC &&
827                         st->codec->extradata_size)
828                         st->need_parsing = AVSTREAM_PARSE_NONE;
829                     /* AVI files with Xan DPCM audio (wrongly) declare PCM
830                      * audio in the header but have Axan as stream_code_tag. */
831                     if (ast->handler == AV_RL32("Axan")) {
832                         st->codec->codec_id  = AV_CODEC_ID_XAN_DPCM;
833                         st->codec->codec_tag = 0;
834                         ast->dshow_block_align = 0;
835                     }
836                     if (amv_file_format) {
837                         st->codec->codec_id    = AV_CODEC_ID_ADPCM_IMA_AMV;
838                         ast->dshow_block_align = 0;
839                     }
840                     if (st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align <= 4 && ast->dshow_block_align) {
841                         av_log(s, AV_LOG_DEBUG, "overriding invalid dshow_block_align of %d\n", ast->dshow_block_align);
842                         ast->dshow_block_align = 0;
843                     }
844                     if (st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 1024 && ast->sample_size == 1024 ||
845                        st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 4096 && ast->sample_size == 4096 ||
846                        st->codec->codec_id == AV_CODEC_ID_MP3 && ast->dshow_block_align == 1152 && ast->sample_size == 1152) {
847                         av_log(s, AV_LOG_DEBUG, "overriding sample_size\n");
848                         ast->sample_size = 0;
849                     }
850                     break;
851                 case AVMEDIA_TYPE_SUBTITLE:
852                     st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
853                     st->request_probe= 1;
854                     avio_skip(pb, size);
855                     break;
856                 default:
857                     st->codec->codec_type = AVMEDIA_TYPE_DATA;
858                     st->codec->codec_id   = AV_CODEC_ID_NONE;
859                     st->codec->codec_tag  = 0;
860                     avio_skip(pb, size);
861                     break;
862                 }
863             }
864             break;
865         case MKTAG('s', 't', 'r', 'd'):
866             if (stream_index >= (unsigned)s->nb_streams
867                 || s->streams[stream_index]->codec->extradata_size
868                 || s->streams[stream_index]->codec->codec_tag == MKTAG('H','2','6','4')) {
869                 avio_skip(pb, size);
870             } else {
871                 uint64_t cur_pos = avio_tell(pb);
872                 if (cur_pos < list_end)
873                     size = FFMIN(size, list_end - cur_pos);
874                 st = s->streams[stream_index];
875
876                 if (size<(1<<30)) {
877                     if (ff_get_extradata(st->codec, pb, size) < 0)
878                         return AVERROR(ENOMEM);
879                 }
880
881                 if (st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly
882                     avio_r8(pb);
883
884                 ret = avi_extract_stream_metadata(st);
885                 if (ret < 0) {
886                     av_log(s, AV_LOG_WARNING, "could not decoding EXIF data in stream header.\n");
887                 }
888             }
889             break;
890         case MKTAG('i', 'n', 'd', 'x'):
891             i = avio_tell(pb);
892             if (pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
893                 avi->use_odml &&
894                 read_braindead_odml_indx(s, 0) < 0 &&
895                 (s->error_recognition & AV_EF_EXPLODE))
896                 goto fail;
897             avio_seek(pb, i + size, SEEK_SET);
898             break;
899         case MKTAG('v', 'p', 'r', 'p'):
900             if (stream_index < (unsigned)s->nb_streams && size > 9 * 4) {
901                 AVRational active, active_aspect;
902
903                 st = s->streams[stream_index];
904                 avio_rl32(pb);
905                 avio_rl32(pb);
906                 avio_rl32(pb);
907                 avio_rl32(pb);
908                 avio_rl32(pb);
909
910                 active_aspect.den = avio_rl16(pb);
911                 active_aspect.num = avio_rl16(pb);
912                 active.num        = avio_rl32(pb);
913                 active.den        = avio_rl32(pb);
914                 avio_rl32(pb); // nbFieldsPerFrame
915
916                 if (active_aspect.num && active_aspect.den &&
917                     active.num && active.den) {
918                     st->sample_aspect_ratio = av_div_q(active_aspect, active);
919                     av_dlog(s, "vprp %d/%d %d/%d\n",
920                             active_aspect.num, active_aspect.den,
921                             active.num, active.den);
922                 }
923                 size -= 9 * 4;
924             }
925             avio_skip(pb, size);
926             break;
927         case MKTAG('s', 't', 'r', 'n'):
928             if (s->nb_streams) {
929                 ret = avi_read_tag(s, s->streams[s->nb_streams - 1], tag, size);
930                 if (ret < 0)
931                     return ret;
932                 break;
933             }
934         default:
935             if (size > 1000000) {
936                 av_log(s, AV_LOG_ERROR,
937                        "Something went wrong during header parsing, "
938                        "I will ignore it and try to continue anyway.\n");
939                 if (s->error_recognition & AV_EF_EXPLODE)
940                     goto fail;
941                 avi->movi_list = avio_tell(pb) - 4;
942                 avi->movi_end  = avi->fsize;
943                 goto end_of_header;
944             }
945             /* skip tag */
946             size += (size & 1);
947             avio_skip(pb, size);
948             break;
949         }
950     }
951
952 end_of_header:
953     /* check stream number */
954     if (stream_index != s->nb_streams - 1) {
955
956 fail:
957         return AVERROR_INVALIDDATA;
958     }
959
960     if (!avi->index_loaded && pb->seekable)
961         avi_load_index(s);
962     calculate_bitrate(s);
963     avi->index_loaded    |= 1;
964
965     if ((ret = guess_ni_flag(s)) < 0)
966         return ret;
967
968     avi->non_interleaved |= ret | (s->flags & AVFMT_FLAG_SORT_DTS);
969
970     dict_entry = av_dict_get(s->metadata, "ISFT", NULL, 0);
971     if (dict_entry && !strcmp(dict_entry->value, "PotEncoder"))
972         for (i = 0; i < s->nb_streams; i++) {
973             AVStream *st = s->streams[i];
974             if (   st->codec->codec_id == AV_CODEC_ID_MPEG1VIDEO
975                 || st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO)
976                 st->need_parsing = AVSTREAM_PARSE_FULL;
977         }
978
979     for (i = 0; i < s->nb_streams; i++) {
980         AVStream *st = s->streams[i];
981         if (st->nb_index_entries)
982             break;
983     }
984     // DV-in-AVI cannot be non-interleaved, if set this must be
985     // a mis-detection.
986     if (avi->dv_demux)
987         avi->non_interleaved = 0;
988     if (i == s->nb_streams && avi->non_interleaved) {
989         av_log(s, AV_LOG_WARNING,
990                "Non-interleaved AVI without index, switching to interleaved\n");
991         avi->non_interleaved = 0;
992     }
993
994     if (avi->non_interleaved) {
995         av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
996         clean_index(s);
997     }
998
999     ff_metadata_conv_ctx(s, NULL, avi_metadata_conv);
1000     ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
1001
1002     return 0;
1003 }
1004
1005 static int read_gab2_sub(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1006 {
1007     if (pkt->size >= 7 &&
1008         pkt->size < INT_MAX - AVPROBE_PADDING_SIZE &&
1009         !strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data + 5) == 2) {
1010         uint8_t desc[256];
1011         int score      = AVPROBE_SCORE_EXTENSION, ret;
1012         AVIStream *ast = st->priv_data;
1013         AVInputFormat *sub_demuxer;
1014         AVRational time_base;
1015         int size;
1016         AVIOContext *pb = avio_alloc_context(pkt->data + 7,
1017                                              pkt->size - 7,
1018                                              0, NULL, NULL, NULL, NULL);
1019         AVProbeData pd;
1020         unsigned int desc_len = avio_rl32(pb);
1021
1022         if (desc_len > pb->buf_end - pb->buf_ptr)
1023             goto error;
1024
1025         ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
1026         avio_skip(pb, desc_len - ret);
1027         if (*desc)
1028             av_dict_set(&st->metadata, "title", desc, 0);
1029
1030         avio_rl16(pb);   /* flags? */
1031         avio_rl32(pb);   /* data size */
1032
1033         size = pb->buf_end - pb->buf_ptr;
1034         pd = (AVProbeData) { .buf      = av_mallocz(size + AVPROBE_PADDING_SIZE),
1035                              .buf_size = size };
1036         if (!pd.buf)
1037             goto error;
1038         memcpy(pd.buf, pb->buf_ptr, size);
1039         sub_demuxer = av_probe_input_format2(&pd, 1, &score);
1040         av_freep(&pd.buf);
1041         if (!sub_demuxer)
1042             goto error;
1043
1044         if (!(ast->sub_ctx = avformat_alloc_context()))
1045             goto error;
1046
1047         ast->sub_ctx->pb = pb;
1048
1049         if (ff_copy_whitelists(ast->sub_ctx, s) < 0)
1050             goto error;
1051
1052         if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
1053             ff_read_packet(ast->sub_ctx, &ast->sub_pkt);
1054             *st->codec = *ast->sub_ctx->streams[0]->codec;
1055             ast->sub_ctx->streams[0]->codec->extradata = NULL;
1056             time_base = ast->sub_ctx->streams[0]->time_base;
1057             avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
1058         }
1059         ast->sub_buffer = pkt->data;
1060         memset(pkt, 0, sizeof(*pkt));
1061         return 1;
1062
1063 error:
1064         av_freep(&ast->sub_ctx);
1065         av_freep(&pb);
1066     }
1067     return 0;
1068 }
1069
1070 static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
1071                                   AVPacket *pkt)
1072 {
1073     AVIStream *ast, *next_ast = next_st->priv_data;
1074     int64_t ts, next_ts, ts_min = INT64_MAX;
1075     AVStream *st, *sub_st = NULL;
1076     int i;
1077
1078     next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
1079                            AV_TIME_BASE_Q);
1080
1081     for (i = 0; i < s->nb_streams; i++) {
1082         st  = s->streams[i];
1083         ast = st->priv_data;
1084         if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt.data) {
1085             ts = av_rescale_q(ast->sub_pkt.dts, st->time_base, AV_TIME_BASE_Q);
1086             if (ts <= next_ts && ts < ts_min) {
1087                 ts_min = ts;
1088                 sub_st = st;
1089             }
1090         }
1091     }
1092
1093     if (sub_st) {
1094         ast               = sub_st->priv_data;
1095         *pkt              = ast->sub_pkt;
1096         pkt->stream_index = sub_st->index;
1097
1098         if (ff_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
1099             ast->sub_pkt.data = NULL;
1100     }
1101     return sub_st;
1102 }
1103
1104 static int get_stream_idx(const unsigned *d)
1105 {
1106     if (d[0] >= '0' && d[0] <= '9' &&
1107         d[1] >= '0' && d[1] <= '9') {
1108         return (d[0] - '0') * 10 + (d[1] - '0');
1109     } else {
1110         return 100; // invalid stream ID
1111     }
1112 }
1113
1114 /**
1115  *
1116  * @param exit_early set to 1 to just gather packet position without making the changes needed to actually read & return the packet
1117  */
1118 static int avi_sync(AVFormatContext *s, int exit_early)
1119 {
1120     AVIContext *avi = s->priv_data;
1121     AVIOContext *pb = s->pb;
1122     int n;
1123     unsigned int d[8];
1124     unsigned int size;
1125     int64_t i, sync;
1126
1127 start_sync:
1128     memset(d, -1, sizeof(d));
1129     for (i = sync = avio_tell(pb); !avio_feof(pb); i++) {
1130         int j;
1131
1132         for (j = 0; j < 7; j++)
1133             d[j] = d[j + 1];
1134         d[7] = avio_r8(pb);
1135
1136         size = d[4] + (d[5] << 8) + (d[6] << 16) + (d[7] << 24);
1137
1138         n = get_stream_idx(d + 2);
1139         av_dlog(s, "%X %X %X %X %X %X %X %X %"PRId64" %u %d\n",
1140                 d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
1141         if (i*(avi->io_fsize>0) + (uint64_t)size > avi->fsize || d[0] > 127)
1142             continue;
1143
1144         // parse ix##
1145         if ((d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) ||
1146             // parse JUNK
1147             (d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K') ||
1148             (d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')) {
1149             avio_skip(pb, size);
1150             goto start_sync;
1151         }
1152
1153         // parse stray LIST
1154         if (d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T') {
1155             avio_skip(pb, 4);
1156             goto start_sync;
1157         }
1158
1159         n = get_stream_idx(d);
1160
1161         if (!((i - avi->last_pkt_pos) & 1) &&
1162             get_stream_idx(d + 1) < s->nb_streams)
1163             continue;
1164
1165         // detect ##ix chunk and skip
1166         if (d[2] == 'i' && d[3] == 'x' && n < s->nb_streams) {
1167             avio_skip(pb, size);
1168             goto start_sync;
1169         }
1170
1171         if (avi->dv_demux && n != 0)
1172             continue;
1173
1174         // parse ##dc/##wb
1175         if (n < s->nb_streams) {
1176             AVStream *st;
1177             AVIStream *ast;
1178             st  = s->streams[n];
1179             ast = st->priv_data;
1180
1181             if (!ast) {
1182                 av_log(s, AV_LOG_WARNING, "Skipping foreign stream %d packet\n", n);
1183                 continue;
1184             }
1185
1186             if (s->nb_streams >= 2) {
1187                 AVStream *st1   = s->streams[1];
1188                 AVIStream *ast1 = st1->priv_data;
1189                 // workaround for broken small-file-bug402.avi
1190                 if (   d[2] == 'w' && d[3] == 'b'
1191                    && n == 0
1192                    && st ->codec->codec_type == AVMEDIA_TYPE_VIDEO
1193                    && st1->codec->codec_type == AVMEDIA_TYPE_AUDIO
1194                    && ast->prefix == 'd'*256+'c'
1195                    && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
1196                   ) {
1197                     n   = 1;
1198                     st  = st1;
1199                     ast = ast1;
1200                     av_log(s, AV_LOG_WARNING,
1201                            "Invalid stream + prefix combination, assuming audio.\n");
1202                 }
1203             }
1204
1205             if (!avi->dv_demux &&
1206                 ((st->discard >= AVDISCARD_DEFAULT && size == 0) /* ||
1207                  // FIXME: needs a little reordering
1208                  (st->discard >= AVDISCARD_NONKEY &&
1209                  !(pkt->flags & AV_PKT_FLAG_KEY)) */
1210                 || st->discard >= AVDISCARD_ALL)) {
1211                 if (!exit_early) {
1212                     ast->frame_offset += get_duration(ast, size);
1213                     avio_skip(pb, size);
1214                     goto start_sync;
1215                 }
1216             }
1217
1218             if (d[2] == 'p' && d[3] == 'c' && size <= 4 * 256 + 4) {
1219                 int k    = avio_r8(pb);
1220                 int last = (k + avio_r8(pb) - 1) & 0xFF;
1221
1222                 avio_rl16(pb); // flags
1223
1224                 // b + (g << 8) + (r << 16);
1225                 for (; k <= last; k++)
1226                     ast->pal[k] = 0xFFU<<24 | avio_rb32(pb)>>8;
1227
1228                 ast->has_pal = 1;
1229                 goto start_sync;
1230             } else if (((ast->prefix_count < 5 || sync + 9 > i) &&
1231                         d[2] < 128 && d[3] < 128) ||
1232                        d[2] * 256 + d[3] == ast->prefix /* ||
1233                        (d[2] == 'd' && d[3] == 'c') ||
1234                        (d[2] == 'w' && d[3] == 'b') */) {
1235                 if (exit_early)
1236                     return 0;
1237                 if (d[2] * 256 + d[3] == ast->prefix)
1238                     ast->prefix_count++;
1239                 else {
1240                     ast->prefix       = d[2] * 256 + d[3];
1241                     ast->prefix_count = 0;
1242                 }
1243
1244                 avi->stream_index = n;
1245                 ast->packet_size  = size + 8;
1246                 ast->remaining    = size;
1247
1248                 if (size) {
1249                     uint64_t pos = avio_tell(pb) - 8;
1250                     if (!st->index_entries || !st->nb_index_entries ||
1251                         st->index_entries[st->nb_index_entries - 1].pos < pos) {
1252                         av_add_index_entry(st, pos, ast->frame_offset, size,
1253                                            0, AVINDEX_KEYFRAME);
1254                     }
1255                 }
1256                 return 0;
1257             }
1258         }
1259     }
1260
1261     if (pb->error)
1262         return pb->error;
1263     return AVERROR_EOF;
1264 }
1265
1266 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
1267 {
1268     AVIContext *avi = s->priv_data;
1269     AVIOContext *pb = s->pb;
1270     int err;
1271 #if FF_API_DESTRUCT_PACKET
1272     void *dstr;
1273 #endif
1274
1275     if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1276         int size = avpriv_dv_get_packet(avi->dv_demux, pkt);
1277         if (size >= 0)
1278             return size;
1279         else
1280             goto resync;
1281     }
1282
1283     if (avi->non_interleaved) {
1284         int best_stream_index = 0;
1285         AVStream *best_st     = NULL;
1286         AVIStream *best_ast;
1287         int64_t best_ts = INT64_MAX;
1288         int i;
1289
1290         for (i = 0; i < s->nb_streams; i++) {
1291             AVStream *st   = s->streams[i];
1292             AVIStream *ast = st->priv_data;
1293             int64_t ts     = ast->frame_offset;
1294             int64_t last_ts;
1295
1296             if (!st->nb_index_entries)
1297                 continue;
1298
1299             last_ts = st->index_entries[st->nb_index_entries - 1].timestamp;
1300             if (!ast->remaining && ts > last_ts)
1301                 continue;
1302
1303             ts = av_rescale_q(ts, st->time_base,
1304                               (AVRational) { FFMAX(1, ast->sample_size),
1305                                              AV_TIME_BASE });
1306
1307             av_dlog(s, "%"PRId64" %d/%d %"PRId64"\n", ts,
1308                     st->time_base.num, st->time_base.den, ast->frame_offset);
1309             if (ts < best_ts) {
1310                 best_ts           = ts;
1311                 best_st           = st;
1312                 best_stream_index = i;
1313             }
1314         }
1315         if (!best_st)
1316             return AVERROR_EOF;
1317
1318         best_ast = best_st->priv_data;
1319         best_ts  = best_ast->frame_offset;
1320         if (best_ast->remaining) {
1321             i = av_index_search_timestamp(best_st,
1322                                           best_ts,
1323                                           AVSEEK_FLAG_ANY |
1324                                           AVSEEK_FLAG_BACKWARD);
1325         } else {
1326             i = av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
1327             if (i >= 0)
1328                 best_ast->frame_offset = best_st->index_entries[i].timestamp;
1329         }
1330
1331         if (i >= 0) {
1332             int64_t pos = best_st->index_entries[i].pos;
1333             pos += best_ast->packet_size - best_ast->remaining;
1334             if (avio_seek(s->pb, pos + 8, SEEK_SET) < 0)
1335               return AVERROR_EOF;
1336
1337             av_assert0(best_ast->remaining <= best_ast->packet_size);
1338
1339             avi->stream_index = best_stream_index;
1340             if (!best_ast->remaining)
1341                 best_ast->packet_size =
1342                 best_ast->remaining   = best_st->index_entries[i].size;
1343         }
1344         else
1345           return AVERROR_EOF;
1346     }
1347
1348 resync:
1349     if (avi->stream_index >= 0) {
1350         AVStream *st   = s->streams[avi->stream_index];
1351         AVIStream *ast = st->priv_data;
1352         int size, err;
1353
1354         if (get_subtitle_pkt(s, st, pkt))
1355             return 0;
1356
1357         // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
1358         if (ast->sample_size <= 1)
1359             size = INT_MAX;
1360         else if (ast->sample_size < 32)
1361             // arbitrary multiplier to avoid tiny packets for raw PCM data
1362             size = 1024 * ast->sample_size;
1363         else
1364             size = ast->sample_size;
1365
1366         if (size > ast->remaining)
1367             size = ast->remaining;
1368         avi->last_pkt_pos = avio_tell(pb);
1369         err               = av_get_packet(pb, pkt, size);
1370         if (err < 0)
1371             return err;
1372         size = err;
1373
1374         if (ast->has_pal && pkt->size < (unsigned)INT_MAX / 2) {
1375             uint8_t *pal;
1376             pal = av_packet_new_side_data(pkt,
1377                                           AV_PKT_DATA_PALETTE,
1378                                           AVPALETTE_SIZE);
1379             if (!pal) {
1380                 av_log(s, AV_LOG_ERROR,
1381                        "Failed to allocate data for palette\n");
1382             } else {
1383                 memcpy(pal, ast->pal, AVPALETTE_SIZE);
1384                 ast->has_pal = 0;
1385             }
1386         }
1387
1388         if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1389             AVBufferRef *avbuf = pkt->buf;
1390 #if FF_API_DESTRUCT_PACKET
1391 FF_DISABLE_DEPRECATION_WARNINGS
1392             dstr = pkt->destruct;
1393 FF_ENABLE_DEPRECATION_WARNINGS
1394 #endif
1395             size = avpriv_dv_produce_packet(avi->dv_demux, pkt,
1396                                             pkt->data, pkt->size, pkt->pos);
1397 #if FF_API_DESTRUCT_PACKET
1398 FF_DISABLE_DEPRECATION_WARNINGS
1399             pkt->destruct = dstr;
1400 FF_ENABLE_DEPRECATION_WARNINGS
1401 #endif
1402             pkt->buf    = avbuf;
1403             pkt->flags |= AV_PKT_FLAG_KEY;
1404             if (size < 0)
1405                 av_free_packet(pkt);
1406         } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1407                    !st->codec->codec_tag && read_gab2_sub(s, st, pkt)) {
1408             ast->frame_offset++;
1409             avi->stream_index = -1;
1410             ast->remaining    = 0;
1411             goto resync;
1412         } else {
1413             /* XXX: How to handle B-frames in AVI? */
1414             pkt->dts = ast->frame_offset;
1415 //                pkt->dts += ast->start;
1416             if (ast->sample_size)
1417                 pkt->dts /= ast->sample_size;
1418             av_dlog(s,
1419                     "dts:%"PRId64" offset:%"PRId64" %d/%d smpl_siz:%d "
1420                     "base:%d st:%d size:%d\n",
1421                     pkt->dts,
1422                     ast->frame_offset,
1423                     ast->scale,
1424                     ast->rate,
1425                     ast->sample_size,
1426                     AV_TIME_BASE,
1427                     avi->stream_index,
1428                     size);
1429             pkt->stream_index = avi->stream_index;
1430
1431             if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->index_entries) {
1432                 AVIndexEntry *e;
1433                 int index;
1434
1435                 index = av_index_search_timestamp(st, ast->frame_offset, AVSEEK_FLAG_ANY);
1436                 e     = &st->index_entries[index];
1437
1438                 if (index >= 0 && e->timestamp == ast->frame_offset) {
1439                     if (index == st->nb_index_entries-1) {
1440                         int key=1;
1441                         int i;
1442                         uint32_t state=-1;
1443                         for (i=0; i<FFMIN(size,256); i++) {
1444                             if (st->codec->codec_id == AV_CODEC_ID_MPEG4) {
1445                                 if (state == 0x1B6) {
1446                                     key= !(pkt->data[i]&0xC0);
1447                                     break;
1448                                 }
1449                             }else
1450                                 break;
1451                             state= (state<<8) + pkt->data[i];
1452                         }
1453                         if (!key)
1454                             e->flags &= ~AVINDEX_KEYFRAME;
1455                     }
1456                     if (e->flags & AVINDEX_KEYFRAME)
1457                         pkt->flags |= AV_PKT_FLAG_KEY;
1458                 }
1459             } else {
1460                 pkt->flags |= AV_PKT_FLAG_KEY;
1461             }
1462             ast->frame_offset += get_duration(ast, pkt->size);
1463         }
1464         ast->remaining -= err;
1465         if (!ast->remaining) {
1466             avi->stream_index = -1;
1467             ast->packet_size  = 0;
1468         }
1469
1470         if (!avi->non_interleaved && pkt->pos >= 0 && ast->seek_pos > pkt->pos) {
1471             av_free_packet(pkt);
1472             goto resync;
1473         }
1474         ast->seek_pos= 0;
1475
1476         if (!avi->non_interleaved && st->nb_index_entries>1 && avi->index_loaded>1) {
1477             int64_t dts= av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q);
1478
1479             if (avi->dts_max - dts > 2*AV_TIME_BASE) {
1480                 avi->non_interleaved= 1;
1481                 av_log(s, AV_LOG_INFO, "Switching to NI mode, due to poor interleaving\n");
1482             }else if (avi->dts_max < dts)
1483                 avi->dts_max = dts;
1484         }
1485
1486         return 0;
1487     }
1488
1489     if ((err = avi_sync(s, 0)) < 0)
1490         return err;
1491     goto resync;
1492 }
1493
1494 /* XXX: We make the implicit supposition that the positions are sorted
1495  * for each stream. */
1496 static int avi_read_idx1(AVFormatContext *s, int size)
1497 {
1498     AVIContext *avi = s->priv_data;
1499     AVIOContext *pb = s->pb;
1500     int nb_index_entries, i;
1501     AVStream *st;
1502     AVIStream *ast;
1503     unsigned int index, tag, flags, pos, len, first_packet = 1;
1504     unsigned last_pos = -1;
1505     unsigned last_idx = -1;
1506     int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
1507     int anykey = 0;
1508
1509     nb_index_entries = size / 16;
1510     if (nb_index_entries <= 0)
1511         return AVERROR_INVALIDDATA;
1512
1513     idx1_pos = avio_tell(pb);
1514     avio_seek(pb, avi->movi_list + 4, SEEK_SET);
1515     if (avi_sync(s, 1) == 0)
1516         first_packet_pos = avio_tell(pb) - 8;
1517     avi->stream_index = -1;
1518     avio_seek(pb, idx1_pos, SEEK_SET);
1519
1520     if (s->nb_streams == 1 && s->streams[0]->codec->codec_tag == AV_RL32("MMES")) {
1521         first_packet_pos = 0;
1522         data_offset = avi->movi_list;
1523     }
1524
1525     /* Read the entries and sort them in each stream component. */
1526     for (i = 0; i < nb_index_entries; i++) {
1527         if (avio_feof(pb))
1528             return -1;
1529
1530         tag   = avio_rl32(pb);
1531         flags = avio_rl32(pb);
1532         pos   = avio_rl32(pb);
1533         len   = avio_rl32(pb);
1534         av_dlog(s, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
1535                 i, tag, flags, pos, len);
1536
1537         index  = ((tag      & 0xff) - '0') * 10;
1538         index +=  (tag >> 8 & 0xff) - '0';
1539         if (index >= s->nb_streams)
1540             continue;
1541         st  = s->streams[index];
1542         ast = st->priv_data;
1543
1544         if (first_packet && first_packet_pos) {
1545             data_offset  = first_packet_pos - pos;
1546             first_packet = 0;
1547         }
1548         pos += data_offset;
1549
1550         av_dlog(s, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
1551
1552         // even if we have only a single stream, we should
1553         // switch to non-interleaved to get correct timestamps
1554         if (last_pos == pos)
1555             avi->non_interleaved = 1;
1556         if (last_idx != pos && len) {
1557             av_add_index_entry(st, pos, ast->cum_len, len, 0,
1558                                (flags & AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
1559             last_idx= pos;
1560         }
1561         ast->cum_len += get_duration(ast, len);
1562         last_pos      = pos;
1563         anykey       |= flags&AVIIF_INDEX;
1564     }
1565     if (!anykey) {
1566         for (index = 0; index < s->nb_streams; index++) {
1567             st = s->streams[index];
1568             if (st->nb_index_entries)
1569                 st->index_entries[0].flags |= AVINDEX_KEYFRAME;
1570         }
1571     }
1572     return 0;
1573 }
1574
1575 /* Scan the index and consider any file with streams more than
1576  * 2 seconds or 64MB apart non-interleaved. */
1577 static int check_stream_max_drift(AVFormatContext *s)
1578 {
1579     int64_t min_pos, pos;
1580     int i;
1581     int *idx = av_mallocz_array(s->nb_streams, sizeof(*idx));
1582     if (!idx)
1583         return AVERROR(ENOMEM);
1584     for (min_pos = pos = 0; min_pos != INT64_MAX; pos = min_pos + 1LU) {
1585         int64_t max_dts = INT64_MIN / 2;
1586         int64_t min_dts = INT64_MAX / 2;
1587         int64_t max_buffer = 0;
1588
1589         min_pos = INT64_MAX;
1590
1591         for (i = 0; i < s->nb_streams; i++) {
1592             AVStream *st = s->streams[i];
1593             AVIStream *ast = st->priv_data;
1594             int n = st->nb_index_entries;
1595             while (idx[i] < n && st->index_entries[idx[i]].pos < pos)
1596                 idx[i]++;
1597             if (idx[i] < n) {
1598                 int64_t dts;
1599                 dts = av_rescale_q(st->index_entries[idx[i]].timestamp /
1600                                    FFMAX(ast->sample_size, 1),
1601                                    st->time_base, AV_TIME_BASE_Q);
1602                 min_dts = FFMIN(min_dts, dts);
1603                 min_pos = FFMIN(min_pos, st->index_entries[idx[i]].pos);
1604             }
1605         }
1606         for (i = 0; i < s->nb_streams; i++) {
1607             AVStream *st = s->streams[i];
1608             AVIStream *ast = st->priv_data;
1609
1610             if (idx[i] && min_dts != INT64_MAX / 2) {
1611                 int64_t dts;
1612                 dts = av_rescale_q(st->index_entries[idx[i] - 1].timestamp /
1613                                    FFMAX(ast->sample_size, 1),
1614                                    st->time_base, AV_TIME_BASE_Q);
1615                 max_dts = FFMAX(max_dts, dts);
1616                 max_buffer = FFMAX(max_buffer,
1617                                    av_rescale(dts - min_dts,
1618                                               st->codec->bit_rate,
1619                                               AV_TIME_BASE));
1620             }
1621         }
1622         if (max_dts - min_dts > 2 * AV_TIME_BASE ||
1623             max_buffer > 1024 * 1024 * 8 * 8) {
1624             av_free(idx);
1625             return 1;
1626         }
1627     }
1628     av_free(idx);
1629     return 0;
1630 }
1631
1632 static int guess_ni_flag(AVFormatContext *s)
1633 {
1634     int i;
1635     int64_t last_start = 0;
1636     int64_t first_end  = INT64_MAX;
1637     int64_t oldpos     = avio_tell(s->pb);
1638
1639     for (i = 0; i < s->nb_streams; i++) {
1640         AVStream *st = s->streams[i];
1641         int n        = st->nb_index_entries;
1642         unsigned int size;
1643
1644         if (n <= 0)
1645             continue;
1646
1647         if (n >= 2) {
1648             int64_t pos = st->index_entries[0].pos;
1649             avio_seek(s->pb, pos + 4, SEEK_SET);
1650             size = avio_rl32(s->pb);
1651             if (pos + size > st->index_entries[1].pos)
1652                 last_start = INT64_MAX;
1653         }
1654
1655         if (st->index_entries[0].pos > last_start)
1656             last_start = st->index_entries[0].pos;
1657         if (st->index_entries[n - 1].pos < first_end)
1658             first_end = st->index_entries[n - 1].pos;
1659     }
1660     avio_seek(s->pb, oldpos, SEEK_SET);
1661
1662     if (last_start > first_end)
1663         return 1;
1664
1665     return check_stream_max_drift(s);
1666 }
1667
1668 static int avi_load_index(AVFormatContext *s)
1669 {
1670     AVIContext *avi = s->priv_data;
1671     AVIOContext *pb = s->pb;
1672     uint32_t tag, size;
1673     int64_t pos = avio_tell(pb);
1674     int64_t next;
1675     int ret     = -1;
1676
1677     if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
1678         goto the_end; // maybe truncated file
1679     av_dlog(s, "movi_end=0x%"PRIx64"\n", avi->movi_end);
1680     for (;;) {
1681         tag  = avio_rl32(pb);
1682         size = avio_rl32(pb);
1683         if (avio_feof(pb))
1684             break;
1685         next = avio_tell(pb) + size + (size & 1);
1686
1687         av_dlog(s, "tag=%c%c%c%c size=0x%x\n",
1688                  tag        & 0xff,
1689                 (tag >>  8) & 0xff,
1690                 (tag >> 16) & 0xff,
1691                 (tag >> 24) & 0xff,
1692                 size);
1693
1694         if (tag == MKTAG('i', 'd', 'x', '1') &&
1695             avi_read_idx1(s, size) >= 0) {
1696             avi->index_loaded=2;
1697             ret = 0;
1698         }else if (tag == MKTAG('L', 'I', 'S', 'T')) {
1699             uint32_t tag1 = avio_rl32(pb);
1700
1701             if (tag1 == MKTAG('I', 'N', 'F', 'O'))
1702                 ff_read_riff_info(s, size - 4);
1703         }else if (!ret)
1704             break;
1705
1706         if (avio_seek(pb, next, SEEK_SET) < 0)
1707             break; // something is wrong here
1708     }
1709
1710 the_end:
1711     avio_seek(pb, pos, SEEK_SET);
1712     return ret;
1713 }
1714
1715 static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
1716 {
1717     AVIStream *ast2 = st2->priv_data;
1718     int64_t ts2     = av_rescale_q(timestamp, st->time_base, st2->time_base);
1719     av_free_packet(&ast2->sub_pkt);
1720     if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
1721         avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
1722         ff_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
1723 }
1724
1725 static int avi_read_seek(AVFormatContext *s, int stream_index,
1726                          int64_t timestamp, int flags)
1727 {
1728     AVIContext *avi = s->priv_data;
1729     AVStream *st;
1730     int i, index;
1731     int64_t pos, pos_min;
1732     AVIStream *ast;
1733
1734     /* Does not matter which stream is requested dv in avi has the
1735      * stream information in the first video stream.
1736      */
1737     if (avi->dv_demux)
1738         stream_index = 0;
1739
1740     if (!avi->index_loaded) {
1741         /* we only load the index on demand */
1742         avi_load_index(s);
1743         avi->index_loaded |= 1;
1744     }
1745     av_assert0(stream_index >= 0);
1746
1747     st    = s->streams[stream_index];
1748     ast   = st->priv_data;
1749     index = av_index_search_timestamp(st,
1750                                       timestamp * FFMAX(ast->sample_size, 1),
1751                                       flags);
1752     if (index < 0) {
1753         if (st->nb_index_entries > 0)
1754             av_log(s, AV_LOG_DEBUG, "Failed to find timestamp %"PRId64 " in index %"PRId64 " .. %"PRId64 "\n",
1755                    timestamp * FFMAX(ast->sample_size, 1),
1756                    st->index_entries[0].timestamp,
1757                    st->index_entries[st->nb_index_entries - 1].timestamp);
1758         return AVERROR_INVALIDDATA;
1759     }
1760
1761     /* find the position */
1762     pos       = st->index_entries[index].pos;
1763     timestamp = st->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
1764
1765     av_dlog(s, "XX %"PRId64" %d %"PRId64"\n",
1766             timestamp, index, st->index_entries[index].timestamp);
1767
1768     if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1769         /* One and only one real stream for DV in AVI, and it has video  */
1770         /* offsets. Calling with other stream indexes should have failed */
1771         /* the av_index_search_timestamp call above.                     */
1772
1773         if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1774             return -1;
1775
1776         /* Feed the DV video stream version of the timestamp to the */
1777         /* DV demux so it can synthesize correct timestamps.        */
1778         ff_dv_offset_reset(avi->dv_demux, timestamp);
1779
1780         avi->stream_index = -1;
1781         return 0;
1782     }
1783
1784     pos_min = pos;
1785     for (i = 0; i < s->nb_streams; i++) {
1786         AVStream *st2   = s->streams[i];
1787         AVIStream *ast2 = st2->priv_data;
1788
1789         ast2->packet_size =
1790         ast2->remaining   = 0;
1791
1792         if (ast2->sub_ctx) {
1793             seek_subtitle(st, st2, timestamp);
1794             continue;
1795         }
1796
1797         if (st2->nb_index_entries <= 0)
1798             continue;
1799
1800 //        av_assert1(st2->codec->block_align);
1801         av_assert0(fabs(av_q2d(st2->time_base) - ast2->scale / (double)ast2->rate) < av_q2d(st2->time_base) * 0.00000001);
1802         index = av_index_search_timestamp(st2,
1803                                           av_rescale_q(timestamp,
1804                                                        st->time_base,
1805                                                        st2->time_base) *
1806                                           FFMAX(ast2->sample_size, 1),
1807                                           flags |
1808                                           AVSEEK_FLAG_BACKWARD |
1809                                           (st2->codec->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
1810         if (index < 0)
1811             index = 0;
1812         ast2->seek_pos = st2->index_entries[index].pos;
1813         pos_min = FFMIN(pos_min,ast2->seek_pos);
1814     }
1815     for (i = 0; i < s->nb_streams; i++) {
1816         AVStream *st2 = s->streams[i];
1817         AVIStream *ast2 = st2->priv_data;
1818
1819         if (ast2->sub_ctx || st2->nb_index_entries <= 0)
1820             continue;
1821
1822         index = av_index_search_timestamp(
1823                 st2,
1824                 av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
1825                 flags | AVSEEK_FLAG_BACKWARD | (st2->codec->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
1826         if (index < 0)
1827             index = 0;
1828         while (!avi->non_interleaved && index>0 && st2->index_entries[index-1].pos >= pos_min)
1829             index--;
1830         ast2->frame_offset = st2->index_entries[index].timestamp;
1831     }
1832
1833     /* do the seek */
1834     if (avio_seek(s->pb, pos_min, SEEK_SET) < 0) {
1835         av_log(s, AV_LOG_ERROR, "Seek failed\n");
1836         return -1;
1837     }
1838     avi->stream_index = -1;
1839     avi->dts_max      = INT_MIN;
1840     return 0;
1841 }
1842
1843 static int avi_read_close(AVFormatContext *s)
1844 {
1845     int i;
1846     AVIContext *avi = s->priv_data;
1847
1848     for (i = 0; i < s->nb_streams; i++) {
1849         AVStream *st   = s->streams[i];
1850         AVIStream *ast = st->priv_data;
1851         if (ast) {
1852             if (ast->sub_ctx) {
1853                 av_freep(&ast->sub_ctx->pb);
1854                 avformat_close_input(&ast->sub_ctx);
1855             }
1856             av_freep(&ast->sub_buffer);
1857             av_free_packet(&ast->sub_pkt);
1858         }
1859     }
1860
1861     av_freep(&avi->dv_demux);
1862
1863     return 0;
1864 }
1865
1866 static int avi_probe(AVProbeData *p)
1867 {
1868     int i;
1869
1870     /* check file header */
1871     for (i = 0; avi_headers[i][0]; i++)
1872         if (AV_RL32(p->buf    ) == AV_RL32(avi_headers[i]    ) &&
1873             AV_RL32(p->buf + 8) == AV_RL32(avi_headers[i] + 4))
1874             return AVPROBE_SCORE_MAX;
1875
1876     return 0;
1877 }
1878
1879 AVInputFormat ff_avi_demuxer = {
1880     .name           = "avi",
1881     .long_name      = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
1882     .priv_data_size = sizeof(AVIContext),
1883     .extensions     = "avi",
1884     .read_probe     = avi_probe,
1885     .read_header    = avi_read_header,
1886     .read_packet    = avi_read_packet,
1887     .read_close     = avi_read_close,
1888     .read_seek      = avi_read_seek,
1889     .priv_class = &demuxer_class,
1890 };