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