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