avidec.c 46.2 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1
/*
2
 * AVI demuxer
Diego Biurrun's avatar
Diego Biurrun committed
3
 * Copyright (c) 2001 Fabrice Bellard
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
Fabrice Bellard's avatar
Fabrice Bellard committed
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
11
 *
12
 * Libav is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Fabrice Bellard's avatar
Fabrice Bellard committed
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
16
 *
Fabrice Bellard's avatar
Fabrice Bellard committed
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellard's avatar
Fabrice Bellard committed
20
 */
21

22 23 24
//#define DEBUG
//#define DEBUG_SEEK

25
#include <strings.h>
26
#include "libavutil/intreadwrite.h"
27
#include "libavutil/bswap.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
28 29
#include "avformat.h"
#include "avi.h"
30
#include "dv.h"
31
#include "riff.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
32

33 34 35
#undef NDEBUG
#include <assert.h>

Fabrice Bellard's avatar
Fabrice Bellard committed
36
typedef struct AVIStream {
37
    int64_t frame_offset; /* current frame (video) or byte (audio) counter
Fabrice Bellard's avatar
Fabrice Bellard committed
38
                         (used to compute the pts) */
39 40 41
    int remaining;
    int packet_size;

Fabrice Bellard's avatar
Fabrice Bellard committed
42
    int scale;
43
    int rate;
44
    int sample_size; /* size of one sample (or packet) (in the rate/scale sense) in bytes */
45

46
    int64_t cum_len; /* temporary storage (used during seek) */
47

48 49
    int prefix;                       ///< normally 'd'<<8 + 'c' or 'w'<<8 + 'b'
    int prefix_count;
50 51
    uint32_t pal[256];
    int has_pal;
52
    int dshow_block_align;            ///< block align variable used to emulate bugs in the MS dshow demuxer
53 54 55 56

    AVFormatContext *sub_ctx;
    AVPacket sub_pkt;
    uint8_t *sub_buffer;
Fabrice Bellard's avatar
Fabrice Bellard committed
57
} AVIStream;
Fabrice Bellard's avatar
Fabrice Bellard committed
58 59

typedef struct {
60 61
    int64_t  riff_end;
    int64_t  movi_end;
62
    int64_t  fsize;
63
    int64_t movi_list;
64
    int64_t last_pkt_pos;
Fabrice Bellard's avatar
Fabrice Bellard committed
65
    int index_loaded;
66
    int is_odml;
67 68
    int non_interleaved;
    int stream_index;
69
    DVDemuxContext* dv_demux;
70 71
    int odml_depth;
#define MAX_ODML_DEPTH 1000
Fabrice Bellard's avatar
Fabrice Bellard committed
72 73
} AVIContext;

74 75 76 77
static const char avi_headers[][8] = {
    { 'R', 'I', 'F', 'F',    'A', 'V', 'I', ' ' },
    { 'R', 'I', 'F', 'F',    'A', 'V', 'I', 'X' },
    { 'R', 'I', 'F', 'F',    'A', 'V', 'I', 0x19},
78
    { 'O', 'N', '2', ' ',    'O', 'N', '2', 'f' },
79
    { 'R', 'I', 'F', 'F',    'A', 'M', 'V', ' ' },
80 81 82
    { 0 }
};

83
static int avi_load_index(AVFormatContext *s);
Michael Niedermayer's avatar
Michael Niedermayer committed
84
static int guess_ni_flag(AVFormatContext *s);
85

86
#define print_tag(str, tag, size)                       \
Luca Barbato's avatar
Luca Barbato committed
87
    av_dlog(NULL, "%s: tag=%c%c%c%c size=0x%x\n",       \
88 89 90 91 92
           str, tag & 0xff,                             \
           (tag >> 8) & 0xff,                           \
           (tag >> 16) & 0xff,                          \
           (tag >> 24) & 0xff,                          \
           size)
Fabrice Bellard's avatar
Fabrice Bellard committed
93

94 95 96
static inline int get_duration(AVIStream *ast, int len){
    if(ast->sample_size){
        return len;
97 98
    }else if (ast->dshow_block_align){
        return (len + ast->dshow_block_align - 1)/ast->dshow_block_align;
99 100 101 102
    }else
        return 1;
}

103
static int get_riff(AVFormatContext *s, AVIOContext *pb)
104
{
105
    AVIContext *avi = s->priv_data;
106 107
    char header[8];
    int i;
108

109
    /* check RIFF header */
110 111
    avio_read(pb, header, 4);
    avi->riff_end = avio_rl32(pb);  /* RIFF chunk size */
112
    avi->riff_end += avio_tell(pb); /* RIFF chunk end */
113
    avio_read(pb, header+4, 4);
114 115 116 117 118

    for(i=0; avi_headers[i][0]; i++)
        if(!memcmp(header, avi_headers[i], 8))
            break;
    if(!avi_headers[i][0])
119
        return -1;
120

121
    if(header[7] == 0x19)
122
        av_log(s, AV_LOG_INFO, "This file has been generated by a totally broken muxer.\n");
123

124 125 126
    return 0;
}

127
static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
128
    AVIContext *avi = s->priv_data;
129
    AVIOContext *pb = s->pb;
130 131 132 133 134 135
    int longs_pre_entry= avio_rl16(pb);
    int index_sub_type = avio_r8(pb);
    int index_type     = avio_r8(pb);
    int entries_in_use = avio_rl32(pb);
    int chunk_id       = avio_rl32(pb);
    int64_t base       = avio_rl64(pb);
136 137 138 139
    int stream_id= 10*((chunk_id&0xFF) - '0') + (((chunk_id>>8)&0xFF) - '0');
    AVStream *st;
    AVIStream *ast;
    int i;
140
    int64_t last_pos= -1;
141
    int64_t filesize= avio_size(s->pb);
142

143
#ifdef DEBUG_SEEK
144
    av_log(s, AV_LOG_ERROR, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%16"PRIX64"\n",
145 146
        longs_pre_entry,index_type, entries_in_use, chunk_id, base);
#endif
147

148
    if(stream_id >= s->nb_streams || stream_id < 0)
149 150 151 152 153 154 155
        return -1;
    st= s->streams[stream_id];
    ast = st->priv_data;

    if(index_sub_type)
        return -1;

156
    avio_rl32(pb);
157 158 159 160 161 162

    if(index_type && longs_pre_entry != 2)
        return -1;
    if(index_type>1)
        return -1;

163 164 165 166 167 168 169 170
    if(filesize > 0 && base >= filesize){
        av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
        if(base>>32 == (base & 0xFFFFFFFF) && (base & 0xFFFFFFFF) < filesize && filesize <= 0xFFFFFFFF)
            base &= 0xFFFFFFFF;
        else
            return -1;
    }

171 172
    for(i=0; i<entries_in_use; i++){
        if(index_type){
173 174
            int64_t pos= avio_rl32(pb) + base - 8;
            int len    = avio_rl32(pb);
Michael Niedermayer's avatar
Michael Niedermayer committed
175
            int key= len >= 0;
176 177
            len &= 0x7FFFFFFF;

178
#ifdef DEBUG_SEEK
179
            av_log(s, AV_LOG_ERROR, "pos:%"PRId64", len:%X\n", pos, len);
180
#endif
Anton Khirnov's avatar
Anton Khirnov committed
181
            if(pb->eof_reached)
182 183
                return -1;

184 185
            if(last_pos == pos || pos == base - 8)
                avi->non_interleaved= 1;
186
            if(last_pos != pos && (len || !ast->sample_size))
187
                av_add_index_entry(st, pos, ast->cum_len, len, 0, key ? AVINDEX_KEYFRAME : 0);
Michael Niedermayer's avatar
Michael Niedermayer committed
188

189
            ast->cum_len += get_duration(ast, len);
190
            last_pos= pos;
191
        }else{
Måns Rullgård's avatar
Måns Rullgård committed
192 193
            int64_t offset, pos;
            int duration;
194 195 196
            offset = avio_rl64(pb);
            avio_rl32(pb);       /* size */
            duration = avio_rl32(pb);
197

Anton Khirnov's avatar
Anton Khirnov committed
198
            if(pb->eof_reached)
199 200
                return -1;

201
            pos = avio_tell(pb);
202

203 204 205 206 207
            if(avi->odml_depth > MAX_ODML_DEPTH){
                av_log(s, AV_LOG_ERROR, "Too deeply nested ODML indexes\n");
                return -1;
            }

208
            avio_seek(pb, offset+8, SEEK_SET);
209
            avi->odml_depth++;
210
            read_braindead_odml_indx(s, frame_num);
211
            avi->odml_depth--;
212 213
            frame_num += duration;

214
            avio_seek(pb, pos, SEEK_SET);
215 216
        }
    }
217
    avi->index_loaded=1;
218 219 220
    return 0;
}

221
static void clean_index(AVFormatContext *s){
222 223
    int i;
    int64_t j;
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

    for(i=0; i<s->nb_streams; i++){
        AVStream *st = s->streams[i];
        AVIStream *ast = st->priv_data;
        int n= st->nb_index_entries;
        int max= ast->sample_size;
        int64_t pos, size, ts;

        if(n != 1 || ast->sample_size==0)
            continue;

        while(max < 1024) max+=max;

        pos= st->index_entries[0].pos;
        size= st->index_entries[0].size;
        ts= st->index_entries[0].timestamp;

        for(j=0; j<size; j+=max){
242
            av_add_index_entry(st, pos+j, ts+j, FFMIN(max, size-j), 0, AVINDEX_KEYFRAME);
243 244 245 246
        }
    }
}

247
static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag, uint32_t size)
248
{
249
    AVIOContext *pb = s->pb;
250
    char key[5] = {0}, *value;
Michael Niedermayer's avatar
Michael Niedermayer committed
251

252
    size += (size & 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
253

254 255 256 257 258
    if (size == UINT_MAX)
        return -1;
    value = av_malloc(size+1);
    if (!value)
        return -1;
259
    avio_read(pb, value, size);
260
    value[size]=0;
261

262 263
    AV_WL32(key, tag);

264 265
    return av_metadata_set2(st ? &st->metadata : &s->metadata, key, value,
                            AV_METADATA_DONT_STRDUP_VAL);
266 267
}

268 269
static void avi_read_info(AVFormatContext *s, uint64_t end)
{
270
    while (avio_tell(s->pb) < end) {
271 272
        uint32_t tag  = avio_rl32(s->pb);
        uint32_t size = avio_rl32(s->pb);
273 274 275 276
        avi_read_tag(s, NULL, tag, size);
    }
}

277 278 279 280 281 282 283 284 285
static const char months[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

static void avi_metadata_creation_time(AVMetadata **metadata, char *date)
{
    char month[4], time[9], buffer[64];
    int i, day, year;
    /* parse standard AVI date format (ie. "Mon Mar 10 15:04:43 2003") */
    if (sscanf(date, "%*3s%*[ ]%3s%*[ ]%2d%*[ ]%8s%*[ ]%4d",
286
               month, &day, time, &year) == 4) {
287 288 289 290 291 292
        for (i=0; i<12; i++)
            if (!strcasecmp(month, months[i])) {
                snprintf(buffer, sizeof(buffer), "%.4d-%.2d-%.2d %s",
                         year, i+1, day, time);
                av_metadata_set2(metadata, "creation_time", buffer, 0);
            }
293 294 295 296
    } else if (date[4] == '/' && date[7] == '/') {
        date[4] = date[7] = '-';
        av_metadata_set2(metadata, "creation_time", date, 0);
    }
297 298
}

299 300
static void avi_read_nikon(AVFormatContext *s, uint64_t end)
{
301
    while (avio_tell(s->pb) < end) {
302 303
        uint32_t tag  = avio_rl32(s->pb);
        uint32_t size = avio_rl32(s->pb);
304 305
        switch (tag) {
        case MKTAG('n', 'c', 't', 'g'): {  /* Nikon Tags */
306 307
            uint64_t tag_end = avio_tell(s->pb) + size;
            while (avio_tell(s->pb) < tag_end) {
308 309
                uint16_t tag  = avio_rl16(s->pb);
                uint16_t size = avio_rl16(s->pb);
310 311
                const char *name = NULL;
                char buffer[64] = {0};
312
                size -= avio_read(s->pb, buffer,
313 314 315 316 317 318 319 320 321 322 323
                                   FFMIN(size, sizeof(buffer)-1));
                switch (tag) {
                case 0x03:  name = "maker";  break;
                case 0x04:  name = "model";  break;
                case 0x13:  name = "creation_time";
                    if (buffer[4] == ':' && buffer[7] == ':')
                        buffer[4] = buffer[7] = '-';
                    break;
                }
                if (name)
                    av_metadata_set2(&s->metadata, name, buffer, 0);
324
                avio_skip(s->pb, size);
325 326 327 328
            }
            break;
        }
        default:
329
            avio_skip(s->pb, size);
330 331 332 333 334
            break;
        }
    }
}

Zdenek Kabelac's avatar
Zdenek Kabelac committed
335
static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
Fabrice Bellard's avatar
Fabrice Bellard committed
336
{
Fabrice Bellard's avatar
Fabrice Bellard committed
337
    AVIContext *avi = s->priv_data;
338
    AVIOContext *pb = s->pb;
339
    unsigned int tag, tag1, handler;
Michael Niedermayer's avatar
Michael Niedermayer committed
340
    int codec_type, stream_index, frame_period, bit_rate;
Michael Niedermayer's avatar
Michael Niedermayer committed
341
    unsigned int size;
342
    int i;
Fabrice Bellard's avatar
Fabrice Bellard committed
343
    AVStream *st;
Måns Rullgård's avatar
Måns Rullgård committed
344
    AVIStream *ast = NULL;
345 346
    int avih_width=0, avih_height=0;
    int amv_file_format=0;
347
    uint64_t list_end = 0;
348
    int ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
349

350
    avi->stream_index= -1;
351

352
    if (get_riff(s, pb) < 0)
Fabrice Bellard's avatar
Fabrice Bellard committed
353
        return -1;
Zdenek Kabelac's avatar
Zdenek Kabelac committed
354

355
    avi->fsize = avio_size(pb);
356
    if(avi->fsize<=0)
357
        avi->fsize= avi->riff_end == 8 ? INT64_MAX : avi->riff_end;
358

Fabrice Bellard's avatar
Fabrice Bellard committed
359 360 361 362 363
    /* first list tag */
    stream_index = -1;
    codec_type = -1;
    frame_period = 0;
    for(;;) {
Anton Khirnov's avatar
Anton Khirnov committed
364
        if (pb->eof_reached)
Fabrice Bellard's avatar
Fabrice Bellard committed
365
            goto fail;
366 367
        tag = avio_rl32(pb);
        size = avio_rl32(pb);
368

Fabrice Bellard's avatar
Fabrice Bellard committed
369 370 371 372
        print_tag("tag", tag, size);

        switch(tag) {
        case MKTAG('L', 'I', 'S', 'T'):
373
            list_end = avio_tell(pb) + size;
Diego Biurrun's avatar
Diego Biurrun committed
374
            /* Ignored, except at start of video packets. */
375
            tag1 = avio_rl32(pb);
376

Fabrice Bellard's avatar
Fabrice Bellard committed
377
            print_tag("list", tag1, 0);
378

Fabrice Bellard's avatar
Fabrice Bellard committed
379
            if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
380
                avi->movi_list = avio_tell(pb) - 4;
381
                if(size) avi->movi_end = avi->movi_list + size + (size & 1);
382
                else     avi->movi_end = avio_size(pb);
Luca Barbato's avatar
Luca Barbato committed
383
                av_dlog(NULL, "movi end=%"PRIx64"\n", avi->movi_end);
Fabrice Bellard's avatar
Fabrice Bellard committed
384 385
                goto end_of_header;
            }
386 387
            else if (tag1 == MKTAG('I', 'N', 'F', 'O'))
                avi_read_info(s, list_end);
388 389
            else if (tag1 == MKTAG('n', 'c', 'd', 't'))
                avi_read_nikon(s, list_end);
390

Fabrice Bellard's avatar
Fabrice Bellard committed
391
            break;
392 393 394
        case MKTAG('I', 'D', 'I', 'T'): {
            unsigned char date[64] = {0};
            size += (size & 1);
395
            size -= avio_read(pb, date, FFMIN(size, sizeof(date)-1));
396
            avio_skip(pb, size);
397 398 399
            avi_metadata_creation_time(&s->metadata, date);
            break;
        }
400
        case MKTAG('d', 'm', 'l', 'h'):
401
            avi->is_odml = 1;
402
            avio_skip(pb, size + (size & 1));
403
            break;
404 405
        case MKTAG('a', 'm', 'v', 'h'):
            amv_file_format=1;
Fabrice Bellard's avatar
Fabrice Bellard committed
406
        case MKTAG('a', 'v', 'i', 'h'):
Diego Biurrun's avatar
Diego Biurrun committed
407
            /* AVI header */
Zdenek Kabelac's avatar
Zdenek Kabelac committed
408
            /* using frame_period is bad idea */
409 410 411 412
            frame_period = avio_rl32(pb);
            bit_rate = avio_rl32(pb) * 8;
            avio_rl32(pb);
            avi->non_interleaved |= avio_rl32(pb) & AVIF_MUSTUSEINDEX;
413

414
            avio_skip(pb, 2 * 4);
415 416 417 418
            avio_rl32(pb);
            avio_rl32(pb);
            avih_width=avio_rl32(pb);
            avih_height=avio_rl32(pb);
419

420
            avio_skip(pb, size - 10 * 4);
421 422 423 424
            break;
        case MKTAG('s', 't', 'r', 'h'):
            /* stream header */

425 426
            tag1 = avio_rl32(pb);
            handler = avio_rl32(pb); /* codec tag */
427 428

            if(tag1 == MKTAG('p', 'a', 'd', 's')){
429
                avio_skip(pb, size - 8);
430 431 432 433
                break;
            }else{
                stream_index++;
                st = av_new_stream(s, stream_index);
Fabrice Bellard's avatar
Fabrice Bellard committed
434 435
                if (!st)
                    goto fail;
436

Fabrice Bellard's avatar
Fabrice Bellard committed
437 438 439 440
                ast = av_mallocz(sizeof(AVIStream));
                if (!ast)
                    goto fail;
                st->priv_data = ast;
441
            }
442 443
            if(amv_file_format)
                tag1 = stream_index ? MKTAG('a','u','d','s') : MKTAG('v','i','d','s');
444

445
            print_tag("strh", tag1, -1);
446

Michael Niedermayer's avatar
Michael Niedermayer committed
447
            if(tag1 == MKTAG('i', 'a', 'v', 's') || tag1 == MKTAG('i', 'v', 'a', 's')){
448 449
                int64_t dv_dur;

450
                /*
451
                 * After some consideration -- I don't think we
Diego Biurrun's avatar
Diego Biurrun committed
452
                 * have to support anything but DV in type1 AVIs.
453 454 455 456 457 458 459 460 461 462 463
                 */
                if (s->nb_streams != 1)
                    goto fail;

                if (handler != MKTAG('d', 'v', 's', 'd') &&
                    handler != MKTAG('d', 'v', 'h', 'd') &&
                    handler != MKTAG('d', 'v', 's', 'l'))
                   goto fail;

                ast = s->streams[0]->priv_data;
                av_freep(&s->streams[0]->codec->extradata);
464
                av_freep(&s->streams[0]->codec);
465 466
                av_freep(&s->streams[0]);
                s->nb_streams = 0;
467
                if (CONFIG_DV_DEMUXER) {
468 469 470
                    avi->dv_demux = dv_init_demux(s);
                    if (!avi->dv_demux)
                        goto fail;
471
                }
472
                s->streams[0]->priv_data = ast;
473
                avio_skip(pb, 3 * 4);
474 475
                ast->scale = avio_rl32(pb);
                ast->rate = avio_rl32(pb);
476
                avio_skip(pb, 4);  /* start time */
477

478
                dv_dur = avio_rl32(pb);
479 480 481 482 483 484
                if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
                    dv_dur *= AV_TIME_BASE;
                    s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
                }
                /*
                 * else, leave duration alone; timing estimation in utils.c
Diego Biurrun's avatar
Diego Biurrun committed
485
                 *      will make a guess based on bitrate.
486 487
                 */

488
                stream_index = s->nb_streams - 1;
489
                avio_skip(pb, size - 9*4);
Michael Niedermayer's avatar
Michael Niedermayer committed
490 491
                break;
            }
492

493
            assert(stream_index < s->nb_streams);
494
            st->codec->stream_codec_tag= handler;
495

496 497 498 499 500 501
            avio_rl32(pb); /* flags */
            avio_rl16(pb); /* priority */
            avio_rl16(pb); /* language */
            avio_rl32(pb); /* initial frame */
            ast->scale = avio_rl32(pb);
            ast->rate = avio_rl32(pb);
502
            if(!(ast->scale && ast->rate)){
Diego Biurrun's avatar
Diego Biurrun committed
503
                av_log(s, AV_LOG_WARNING, "scale/rate is %u/%u which is invalid. (This file has been generated by broken software.)\n", ast->scale, ast->rate);
Michael Niedermayer's avatar
indent  
Michael Niedermayer committed
504 505 506 507 508 509 510
                if(frame_period){
                    ast->rate = 1000000;
                    ast->scale = frame_period;
                }else{
                    ast->rate = 25;
                    ast->scale = 1;
                }
511
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
512
            av_set_pts_info(st, 64, ast->scale, ast->rate);
513

514 515
            ast->cum_len=avio_rl32(pb); /* start */
            st->nb_frames = avio_rl32(pb);
516

Michael Niedermayer's avatar
Michael Niedermayer committed
517
            st->start_time = 0;
518 519 520
            avio_rl32(pb); /* buffer size */
            avio_rl32(pb); /* quality */
            ast->sample_size = avio_rl32(pb); /* sample ssize */
521
            ast->cum_len *= FFMAX(1, ast->sample_size);
522
//            av_log(s, AV_LOG_DEBUG, "%d %d %d %d\n", ast->rate, ast->scale, ast->start, ast->sample_size);
523

Michael Niedermayer's avatar
Michael Niedermayer committed
524
            switch(tag1) {
525
            case MKTAG('v', 'i', 'd', 's'):
526
                codec_type = AVMEDIA_TYPE_VIDEO;
527

Michael Niedermayer's avatar
Michael Niedermayer committed
528 529 530
                ast->sample_size = 0;
                break;
            case MKTAG('a', 'u', 'd', 's'):
531
                codec_type = AVMEDIA_TYPE_AUDIO;
532
                break;
533
            case MKTAG('t', 'x', 't', 's'):
534
                codec_type = AVMEDIA_TYPE_SUBTITLE;
535
                break;
Florian Echtler's avatar
Florian Echtler committed
536
            case MKTAG('d', 'a', 't', 's'):
537
                codec_type = AVMEDIA_TYPE_DATA;
Florian Echtler's avatar
Florian Echtler committed
538
                break;
539
            default:
Michael Niedermayer's avatar
Michael Niedermayer committed
540
                av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1);
541
                goto fail;
Fabrice Bellard's avatar
Fabrice Bellard committed
542
            }
543 544
            if(ast->sample_size == 0)
                st->duration = st->nb_frames;
545
            ast->frame_offset= ast->cum_len;
546
            avio_skip(pb, size - 12 * 4);
Fabrice Bellard's avatar
Fabrice Bellard committed
547 548 549
            break;
        case MKTAG('s', 't', 'r', 'f'):
            /* stream header */
550
            if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
551
                avio_skip(pb, size);
Fabrice Bellard's avatar
Fabrice Bellard committed
552
            } else {
553
                uint64_t cur_pos = avio_tell(pb);
554 555
                if (cur_pos < list_end)
                    size = FFMIN(size, list_end - cur_pos);
Fabrice Bellard's avatar
Fabrice Bellard committed
556 557
                st = s->streams[stream_index];
                switch(codec_type) {
558
                case AVMEDIA_TYPE_VIDEO:
559 560 561
                    if(amv_file_format){
                        st->codec->width=avih_width;
                        st->codec->height=avih_height;
562
                        st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
563
                        st->codec->codec_id = CODEC_ID_AMV;
564
                        avio_skip(pb, size);
565 566
                        break;
                    }
Peter Ross's avatar
Peter Ross committed
567
                    tag1 = ff_get_bmp_header(pb, st);
568

Michael Niedermayer's avatar
Michael Niedermayer committed
569
                    if (tag1 == MKTAG('D', 'X', 'S', 'B') || tag1 == MKTAG('D','X','S','A')) {
570
                        st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
571 572 573 574 575
                        st->codec->codec_tag = tag1;
                        st->codec->codec_id = CODEC_ID_XSUB;
                        break;
                    }

576 577 578
                    if(size > 10*4 && size<(1<<30)){
                        st->codec->extradata_size= size - 10*4;
                        st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
579 580 581 582
                        if (!st->codec->extradata) {
                            st->codec->extradata_size= 0;
                            return AVERROR(ENOMEM);
                        }
583
                        avio_read(pb, st->codec->extradata, st->codec->extradata_size);
584
                    }
585

586
                    if(st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly
587
                        avio_r8(pb);
588

Diego Biurrun's avatar
Diego Biurrun committed
589 590 591
                    /* Extract palette from extradata if bpp <= 8. */
                    /* This code assumes that extradata contains only palette. */
                    /* This is true for all paletted codecs implemented in FFmpeg. */
592
                    if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
593
                        st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
594
#if HAVE_BIGENDIAN
595
                        for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
596
                            st->codec->palctrl->palette[i] = av_bswap32(((uint32_t*)st->codec->extradata)[i]);
597
#else
598 599
                        memcpy(st->codec->palctrl->palette, st->codec->extradata,
                               FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
600
#endif
601
                        st->codec->palctrl->palette_changed = 1;
602 603
                    }

Fabrice Bellard's avatar
Fabrice Bellard committed
604
                    print_tag("video", tag1, 0);
605

606
                    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
607
                    st->codec->codec_tag = tag1;
608
                    st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
Diego Biurrun's avatar
Diego Biurrun committed
609
                    st->need_parsing = AVSTREAM_PARSE_HEADERS; // This is needed to get the pict type which is necessary for generating correct pts.
610 611 612 613 614
                    // Support "Resolution 1:1" for Avid AVI Codec
                    if(tag1 == MKTAG('A', 'V', 'R', 'n') &&
                       st->codec->extradata_size >= 31 &&
                       !memcmp(&st->codec->extradata[28], "1:1", 3))
                        st->codec->codec_id = CODEC_ID_RAWVIDEO;
615 616 617 618 619 620 621 622 623

                    if(st->codec->codec_tag==0 && st->codec->height > 0 && st->codec->extradata_size < 1U<<30){
                        st->codec->extradata_size+= 9;
                        st->codec->extradata= av_realloc(st->codec->extradata, st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
                        if(st->codec->extradata)
                            memcpy(st->codec->extradata + st->codec->extradata_size - 9, "BottomUp", 9);
                    }
                    st->codec->height= FFABS(st->codec->height);

624
//                    avio_skip(pb, size - 5 * 4);
Fabrice Bellard's avatar
Fabrice Bellard committed
625
                    break;
626
                case AVMEDIA_TYPE_AUDIO:
627 628 629
                    ret = ff_get_wav_header(pb, st->codec, size);
                    if (ret < 0)
                        return ret;
630
                    ast->dshow_block_align= st->codec->block_align;
631
                    if(ast->sample_size && st->codec->block_align && ast->sample_size != st->codec->block_align){
632
                        av_log(s, AV_LOG_WARNING, "sample size (%d) != block align (%d)\n", ast->sample_size, st->codec->block_align);
633 634
                        ast->sample_size= st->codec->block_align;
                    }
635
                    if (size&1) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
636
                        avio_skip(pb, 1);
Diego Biurrun's avatar
Diego Biurrun committed
637
                    /* Force parsing as several audio frames can be in
Diego Biurrun's avatar
Diego Biurrun committed
638
                     * one packet and timestamps refer to packet start. */
639
                    st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
Diego Biurrun's avatar
Diego Biurrun committed
640 641 642
                    /* ADTS header is in extradata, AAC without header must be
                     * stored as exact frames. Parser not needed and it will
                     * fail. */
643
                    if (st->codec->codec_id == CODEC_ID_AAC && st->codec->extradata_size)
Aurelien Jacobs's avatar
Aurelien Jacobs committed
644
                        st->need_parsing = AVSTREAM_PARSE_NONE;
645 646
                    /* AVI files with Xan DPCM audio (wrongly) declare PCM
                     * audio in the header but have Axan as stream_code_tag. */
647
                    if (st->codec->stream_codec_tag == AV_RL32("Axan")){
648 649 650
                        st->codec->codec_id  = CODEC_ID_XAN_DPCM;
                        st->codec->codec_tag = 0;
                    }
651
                    if (amv_file_format){
652
                        st->codec->codec_id  = CODEC_ID_ADPCM_IMA_AMV;
653 654
                        ast->dshow_block_align = 0;
                    }
Fabrice Bellard's avatar
Fabrice Bellard committed
655
                    break;
656 657 658 659
                case AVMEDIA_TYPE_SUBTITLE:
                    st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
                    st->codec->codec_id   = CODEC_ID_PROBE;
                    break;
Fabrice Bellard's avatar
Fabrice Bellard committed
660
                default:
661
                    st->codec->codec_type = AVMEDIA_TYPE_DATA;
662 663
                    st->codec->codec_id= CODEC_ID_NONE;
                    st->codec->codec_tag= 0;
664
                    avio_skip(pb, size);
Fabrice Bellard's avatar
Fabrice Bellard committed
665 666 667 668
                    break;
                }
            }
            break;
669
        case MKTAG('i', 'n', 'd', 'x'):
670
            i= avio_tell(pb);
671
            if(pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX)){
672 673
                read_braindead_odml_indx(s, 0);
            }
674
            avio_seek(pb, i+size, SEEK_SET);
675
            break;
676 677 678 679 680
        case MKTAG('v', 'p', 'r', 'p'):
            if(stream_index < (unsigned)s->nb_streams && size > 9*4){
                AVRational active, active_aspect;

                st = s->streams[stream_index];
681 682 683 684 685 686 687 688 689 690 691
                avio_rl32(pb);
                avio_rl32(pb);
                avio_rl32(pb);
                avio_rl32(pb);
                avio_rl32(pb);

                active_aspect.den= avio_rl16(pb);
                active_aspect.num= avio_rl16(pb);
                active.num       = avio_rl32(pb);
                active.den       = avio_rl32(pb);
                avio_rl32(pb); //nbFieldsPerFrame
692 693

                if(active_aspect.num && active_aspect.den && active.num && active.den){
694
                    st->sample_aspect_ratio= av_div_q(active_aspect, active);
695 696 697 698
//av_log(s, AV_LOG_ERROR, "vprp %d/%d %d/%d\n", active_aspect.num, active_aspect.den, active.num, active.den);
                }
                size -= 9*4;
            }
699
            avio_skip(pb, size);
700
            break;
701 702
        case MKTAG('s', 't', 'r', 'n'):
            if(s->nb_streams){
703
                avi_read_tag(s, s->streams[s->nb_streams-1], tag, size);
704 705
                break;
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
706
        default:
707
            if(size > 1000000){
Diego Biurrun's avatar
Diego Biurrun committed
708 709
                av_log(s, AV_LOG_ERROR, "Something went wrong during header parsing, "
                                        "I will ignore it and try to continue anyway.\n");
710
                avi->movi_list = avio_tell(pb) - 4;
711
                avi->movi_end  = avio_size(pb);