Statistics
| Branch: | Revision:

ffmpeg / libavformat / mov.c @ 62b6498d

History | View | Annotate | Download (56.6 KB)

1
/*
2
 * MOV 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 <limits.h>
23

    
24
//#define DEBUG
25

    
26
#include "avformat.h"
27
#include "riff.h"
28
#include "isom.h"
29
#include "dv.h"
30

    
31
#ifdef CONFIG_ZLIB
32
#include <zlib.h>
33
#endif
34

    
35
/*
36
 * First version by Francois Revol revol@free.fr
37
 * Seek function by Gael Chardon gael.dev@4now.net
38
 *
39
 * Features and limitations:
40
 * - reads most of the QT files I have (at least the structure),
41
 *   the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
42
 *   FIXED, Francois Revol, 07/17/2002
43
 * - ffmpeg has nearly none of the usual QuickTime codecs,
44
 *   although I succesfully dumped raw and mp3 audio tracks off .mov files.
45
 *   Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
46
 * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
47
 *   (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
48
 *   http://mpeg.telecomitalialab.com/faq.htm
49
 * - the code is quite ugly... maybe I won't do it recursive next time :-)
50
 * - seek is not supported with files that contain edit list
51
 *
52
 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
53
 * when coding this :) (it's a writer anyway)
54
 *
55
 * Reference documents:
56
 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
57
 * Apple:
58
 *  http://developer.apple.com/documentation/QuickTime/QTFF/
59
 *  http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
60
 * QuickTime is a trademark of Apple (AFAIK :))
61
 */
62

    
63
#include "qtpalette.h"
64

    
65

    
66
#undef NDEBUG
67
#include <assert.h>
68

    
69
/* the QuickTime file format is quite convoluted...
70
 * it has lots of index tables, each indexing something in another one...
71
 * Here we just use what is needed to read the chunks
72
 */
73

    
74
typedef struct MOV_sample_to_chunk_tbl {
75
    long first;
76
    long count;
77
    long id;
78
} MOV_sample_to_chunk_tbl;
79

    
80
typedef struct {
81
    uint32_t type;
82
    int64_t offset;
83
    int64_t size; /* total size (excluding the size and type fields) */
84
} MOV_atom_t;
85

    
86
typedef struct {
87
    int seed;
88
    int flags;
89
    int size;
90
    void* clrs;
91
} MOV_ctab_t;
92

    
93
typedef struct MOV_mdat_atom_s {
94
    offset_t offset;
95
    int64_t size;
96
} MOV_mdat_atom_t;
97

    
98
typedef struct {
99
    uint8_t  version;
100
    uint32_t flags; // 24bit
101

    
102
    /* 0x03 ESDescrTag */
103
    uint16_t es_id;
104
#define MP4ODescrTag                    0x01
105
#define MP4IODescrTag                   0x02
106
#define MP4ESDescrTag                   0x03
107
#define MP4DecConfigDescrTag            0x04
108
#define MP4DecSpecificDescrTag          0x05
109
#define MP4SLConfigDescrTag             0x06
110
#define MP4ContentIdDescrTag            0x07
111
#define MP4SupplContentIdDescrTag       0x08
112
#define MP4IPIPtrDescrTag               0x09
113
#define MP4IPMPPtrDescrTag              0x0A
114
#define MP4IPMPDescrTag                 0x0B
115
#define MP4RegistrationDescrTag         0x0D
116
#define MP4ESIDIncDescrTag              0x0E
117
#define MP4ESIDRefDescrTag              0x0F
118
#define MP4FileIODescrTag               0x10
119
#define MP4FileODescrTag                0x11
120
#define MP4ExtProfileLevelDescrTag      0x13
121
#define MP4ExtDescrTagsStart            0x80
122
#define MP4ExtDescrTagsEnd              0xFE
123
    uint8_t  stream_priority;
124

    
125
    /* 0x04 DecConfigDescrTag */
126
    uint8_t  object_type_id;
127
    uint8_t  stream_type;
128
    /* XXX: really streamType is
129
     * only 6bit, followed by:
130
     * 1bit  upStream
131
     * 1bit  reserved
132
     */
133
    uint32_t buffer_size_db; // 24
134
    uint32_t max_bitrate;
135
    uint32_t avg_bitrate;
136

    
137
    /* 0x05 DecSpecificDescrTag */
138
    uint8_t  decoder_cfg_len;
139
    uint8_t *decoder_cfg;
140

    
141
    /* 0x06 SLConfigDescrTag */
142
    uint8_t  sl_config_len;
143
    uint8_t *sl_config;
144
} MOV_esds_t;
145

    
146
struct MOVParseTableEntry;
147

    
148
typedef struct MOVStreamContext {
149
    int ffindex; /* the ffmpeg stream id */
150
    long next_chunk;
151
    unsigned int chunk_count;
152
    int64_t *chunk_offsets;
153
    unsigned int stts_count;
154
    Time2Sample *stts_data;
155
    unsigned int ctts_count;
156
    Time2Sample *ctts_data;
157
    unsigned int edit_count; /* number of 'edit' (elst atom) */
158
    unsigned int sample_to_chunk_sz;
159
    MOV_sample_to_chunk_tbl *sample_to_chunk;
160
    int sample_to_ctime_index;
161
    int sample_to_ctime_sample;
162
    unsigned int sample_size;
163
    unsigned int sample_count;
164
    long *sample_sizes;
165
    unsigned int keyframe_count;
166
    long *keyframes;
167
    int time_scale;
168
    int time_rate;
169
    long current_sample;
170
    MOV_esds_t esds;
171
    unsigned int bytes_per_frame;
172
    unsigned int samples_per_frame;
173
    int dv_audio_container;
174
} MOVStreamContext;
175

    
176
typedef struct MOVContext {
177
    AVFormatContext *fc;
178
    int time_scale;
179
    int64_t duration; /* duration of the longest track */
180
    int found_moov; /* when both 'moov' and 'mdat' sections has been found */
181
    int found_mdat; /* we suppose we have enough data to read the file */
182
    int64_t mdat_offset;
183
    int total_streams;
184
    MOVStreamContext *streams[MAX_STREAMS];
185

    
186
    int ctab_size;
187
    MOV_ctab_t **ctab;           /* color tables */
188
    const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
189
    /* NOTE: for recursion save to/ restore from local variable! */
190

    
191
    AVPaletteControl palette_control;
192
    MOV_mdat_atom_t *mdat_list;
193
    int mdat_count;
194
    DVDemuxContext *dv_demux;
195
    AVFormatContext *dv_fctx;
196
    int isom; /* 1 if file is ISO Media (mp4/3gp) */
197
} MOVContext;
198

    
199

    
200
/* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
201

    
202
/* those functions parse an atom */
203
/* return code:
204
 1: found what I wanted, exit
205
 0: continue to parse next atom
206
 -1: error occured, exit
207
 */
208
typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
209

    
210
/* links atom IDs to parse functions */
211
typedef struct MOVParseTableEntry {
212
    uint32_t type;
213
    mov_parse_function func;
214
} MOVParseTableEntry;
215

    
216
static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
217
{
218
    int64_t total_size = 0;
219
    MOV_atom_t a;
220
    int i;
221
    int err = 0;
222

    
223
    a.offset = atom.offset;
224

    
225
    if (atom.size < 0)
226
        atom.size = 0x7fffffffffffffffLL;
227
    while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
228
        a.size = atom.size;
229
        a.type=0L;
230
        if(atom.size >= 8) {
231
            a.size = get_be32(pb);
232
            a.type = get_le32(pb);
233
        }
234
        total_size += 8;
235
        a.offset += 8;
236
        dprintf(c->fc, "type: %08x  %.4s  sz: %"PRIx64"  %"PRIx64"   %"PRIx64"\n", a.type, (char*)&a.type, a.size, atom.size, total_size);
237
        if (a.size == 1) { /* 64 bit extended size */
238
            a.size = get_be64(pb) - 8;
239
            a.offset += 8;
240
            total_size += 8;
241
        }
242
        if (a.size == 0) {
243
            a.size = atom.size - total_size;
244
            if (a.size <= 8)
245
                break;
246
        }
247
        for (i = 0; c->parse_table[i].type != 0L
248
             && c->parse_table[i].type != a.type; i++)
249
            /* empty */;
250

    
251
        a.size -= 8;
252

    
253
        if(a.size < 0 || a.size > atom.size - total_size)
254
            break;
255

    
256
        if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
257
            url_fskip(pb, a.size);
258
        } else {
259
            offset_t start_pos = url_ftell(pb);
260
            int64_t left;
261
            err = (c->parse_table[i].func)(c, pb, a);
262
            left = a.size - url_ftell(pb) + start_pos;
263
            if (left > 0) /* skip garbage at atom end */
264
                url_fskip(pb, left);
265
        }
266

    
267
        a.offset += a.size;
268
        total_size += a.size;
269
    }
270

    
271
    if (!err && total_size < atom.size && atom.size < 0x7ffff) {
272
        url_fskip(pb, atom.size - total_size);
273
    }
274

    
275
    return err;
276
}
277

    
278
static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
279
{
280
#if 1
281
    url_fskip(pb, atom.size); // for now
282
#else
283
    VERY VERY BROKEN, NEVER execute this, needs rewrite
284
    unsigned int len;
285
    MOV_ctab_t *t;
286
    c->ctab = av_realloc(c->ctab, ++c->ctab_size);
287
    t = c->ctab[c->ctab_size];
288
    t->seed = get_be32(pb);
289
    t->flags = get_be16(pb);
290
    t->size = get_be16(pb) + 1;
291
    len = 2 * t->size * 4;
292
    if (len > 0) {
293
        t->clrs = av_malloc(len); // 16bit A R G B
294
        if (t->clrs)
295
            get_buffer(pb, t->clrs, len);
296
    }
297
#endif
298

    
299
    return 0;
300
}
301

    
302
static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
303
{
304
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
305
    uint32_t type;
306
    uint32_t ctype;
307

    
308
    get_byte(pb); /* version */
309
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
310

    
311
    /* component type */
312
    ctype = get_le32(pb);
313
    type = get_le32(pb); /* component subtype */
314

    
315
    dprintf(c->fc, "ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
316
    dprintf(c->fc, "stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
317
    if(!ctype)
318
        c->isom = 1;
319
    if(type == MKTAG('v', 'i', 'd', 'e'))
320
        st->codec->codec_type = CODEC_TYPE_VIDEO;
321
    else if(type == MKTAG('s', 'o', 'u', 'n'))
322
        st->codec->codec_type = CODEC_TYPE_AUDIO;
323
    else if(type == MKTAG('m', '1', 'a', ' '))
324
        st->codec->codec_id = CODEC_ID_MP2;
325
    else if(type == MKTAG('s', 'u', 'b', 'p')) {
326
        st->codec->codec_type = CODEC_TYPE_SUBTITLE;
327
        st->codec->codec_id = CODEC_ID_DVD_SUBTITLE;
328
    }
329
    get_be32(pb); /* component  manufacture */
330
    get_be32(pb); /* component flags */
331
    get_be32(pb); /* component flags mask */
332

    
333
    if(atom.size <= 24)
334
        return 0; /* nothing left to read */
335

    
336
    url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
337
    return 0;
338
}
339

    
340
static int mov_mp4_read_descr_len(ByteIOContext *pb)
341
{
342
    int len = 0;
343
    int count = 4;
344
    while (count--) {
345
        int c = get_byte(pb);
346
        len = (len << 7) | (c & 0x7f);
347
        if (!(c & 0x80))
348
            break;
349
    }
350
    return len;
351
}
352

    
353
static int mov_mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
354
{
355
    int len;
356
    *tag = get_byte(pb);
357
    len = mov_mp4_read_descr_len(pb);
358
    dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
359
    return len;
360
}
361

    
362
static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
363
{
364
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
365
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
366
    int tag, len;
367

    
368
    /* Well, broken but suffisant for some MP4 streams */
369
    get_be32(pb); /* version + flags */
370
    len = mov_mp4_read_descr(c, pb, &tag);
371
    if (tag == MP4ESDescrTag) {
372
        get_be16(pb); /* ID */
373
        get_byte(pb); /* priority */
374
    } else
375
        get_be16(pb); /* ID */
376

    
377
    len = mov_mp4_read_descr(c, pb, &tag);
378
    if (tag == MP4DecConfigDescrTag) {
379
        sc->esds.object_type_id = get_byte(pb);
380
        sc->esds.stream_type = get_byte(pb);
381
        sc->esds.buffer_size_db = get_be24(pb);
382
        sc->esds.max_bitrate = get_be32(pb);
383
        sc->esds.avg_bitrate = get_be32(pb);
384

    
385
        st->codec->codec_id= codec_get_id(ff_mp4_obj_type, sc->esds.object_type_id);
386
        dprintf(c->fc, "esds object type id %d\n", sc->esds.object_type_id);
387
        len = mov_mp4_read_descr(c, pb, &tag);
388
        if (tag == MP4DecSpecificDescrTag) {
389
            dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
390
            st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
391
            if (st->codec->extradata) {
392
                get_buffer(pb, st->codec->extradata, len);
393
                st->codec->extradata_size = len;
394
                /* from mplayer */
395
                if ((*st->codec->extradata >> 3) == 29) {
396
                    st->codec->codec_id = CODEC_ID_MP3ON4;
397
                }
398
            }
399
        }
400
    }
401
    return 0;
402
}
403

    
404
/* this atom contains actual media data */
405
static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
406
{
407
    if(atom.size == 0) /* wrong one (MP4) */
408
        return 0;
409
    c->mdat_list = av_realloc(c->mdat_list, (c->mdat_count + 1) * sizeof(*c->mdat_list));
410
    c->mdat_list[c->mdat_count].offset = atom.offset;
411
    c->mdat_list[c->mdat_count].size = atom.size;
412
    c->mdat_count++;
413
    c->found_mdat=1;
414
    c->mdat_offset = atom.offset;
415
    if(c->found_moov)
416
        return 1; /* found both, just go */
417
    url_fskip(pb, atom.size);
418
    return 0; /* now go for moov */
419
}
420

    
421
static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
422
{
423
    uint32_t type = get_le32(pb);
424

    
425
    if (type != MKTAG('q','t',' ',' '))
426
        c->isom = 1;
427
    av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
428
    get_be32(pb); /* minor version */
429
    url_fskip(pb, atom.size - 8);
430
    return 0;
431
}
432

    
433
/* this atom should contain all header atoms */
434
static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
435
{
436
    int err;
437

    
438
    err = mov_read_default(c, pb, atom);
439
    /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
440
    /* so we don't parse the whole file if over a network */
441
    c->found_moov=1;
442
    if(c->found_mdat)
443
        return 1; /* found both, just go */
444
    return 0; /* now go for mdat */
445
}
446

    
447

    
448
static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
449
{
450
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
451
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
452
    int version = get_byte(pb);
453
    int lang;
454

    
455
    if (version > 1)
456
        return 1; /* unsupported */
457

    
458
    get_byte(pb); get_byte(pb);
459
    get_byte(pb); /* flags */
460

    
461
    if (version == 1) {
462
        get_be64(pb);
463
        get_be64(pb);
464
    } else {
465
        get_be32(pb); /* creation time */
466
        get_be32(pb); /* modification time */
467
    }
468

    
469
    sc->time_scale = get_be32(pb);
470
    st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
471

    
472
    lang = get_be16(pb); /* language */
473
    ff_mov_lang_to_iso639(lang, st->language);
474
    get_be16(pb); /* quality */
475

    
476
    return 0;
477
}
478

    
479
static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
480
{
481
    int version = get_byte(pb); /* version */
482
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
483

    
484
    if (version == 1) {
485
        get_be64(pb);
486
        get_be64(pb);
487
    } else {
488
        get_be32(pb); /* creation time */
489
        get_be32(pb); /* modification time */
490
    }
491
    c->time_scale = get_be32(pb); /* time scale */
492
#ifdef DEBUG
493
    av_log(NULL, AV_LOG_DEBUG, "time scale = %i\n", c->time_scale);
494
#endif
495
    c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
496
    get_be32(pb); /* preferred scale */
497

    
498
    get_be16(pb); /* preferred volume */
499

    
500
    url_fskip(pb, 10); /* reserved */
501

    
502
    url_fskip(pb, 36); /* display matrix */
503

    
504
    get_be32(pb); /* preview time */
505
    get_be32(pb); /* preview duration */
506
    get_be32(pb); /* poster time */
507
    get_be32(pb); /* selection time */
508
    get_be32(pb); /* selection duration */
509
    get_be32(pb); /* current time */
510
    get_be32(pb); /* next track ID */
511

    
512
    return 0;
513
}
514

    
515
static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
516
{
517
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
518

    
519
    if((uint64_t)atom.size > (1<<30))
520
        return -1;
521

    
522
    // currently SVQ3 decoder expect full STSD header - so let's fake it
523
    // this should be fixed and just SMI header should be passed
524
    av_free(st->codec->extradata);
525
    st->codec->extradata_size = 0x5a + atom.size;
526
    st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
527

    
528
    if (st->codec->extradata) {
529
        memcpy(st->codec->extradata, "SVQ3", 4); // fake
530
        get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
531
        dprintf(c->fc, "Reading SMI %"PRId64"  %s\n", atom.size, st->codec->extradata + 0x5a);
532
    } else
533
        url_fskip(pb, atom.size);
534

    
535
    return 0;
536
}
537

    
538
static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
539
{
540
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
541
    int little_endian = get_be16(pb);
542

    
543
    if (little_endian) {
544
        switch (st->codec->codec_id) {
545
        case CODEC_ID_PCM_S24BE:
546
            st->codec->codec_id = CODEC_ID_PCM_S24LE;
547
            break;
548
        case CODEC_ID_PCM_S32BE:
549
            st->codec->codec_id = CODEC_ID_PCM_S32LE;
550
            break;
551
        default:
552
            break;
553
        }
554
    }
555
    return 0;
556
}
557

    
558
/* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
559
static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
560
{
561
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
562
    if((uint64_t)atom.size > (1<<30))
563
        return -1;
564
    av_free(st->codec->extradata);
565
    st->codec->extradata_size = atom.size + 8;
566
    st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
567
    if (st->codec->extradata) {
568
        AV_WL32(st->codec->extradata + 4, atom.type);
569
        get_buffer(pb, st->codec->extradata + 8, atom.size);
570
    } else
571
        url_fskip(pb, atom.size);
572
    return 0;
573
}
574

    
575
static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
576
{
577
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
578

    
579
    if((uint64_t)atom.size > (1<<30))
580
        return -1;
581

    
582
    if (st->codec->codec_id == CODEC_ID_QDM2) {
583
        // pass all frma atom to codec, needed at least for QDM2
584
        av_free(st->codec->extradata);
585
        st->codec->extradata_size = atom.size;
586
        st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
587

    
588
        if (st->codec->extradata) {
589
            get_buffer(pb, st->codec->extradata, atom.size);
590
        } else
591
            url_fskip(pb, atom.size);
592
    } else if (atom.size > 8) { /* to read frma, esds atoms */
593
        mov_read_default(c, pb, atom);
594
    } else
595
        url_fskip(pb, atom.size);
596
    return 0;
597
}
598

    
599
static int mov_read_avcC(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
600
{
601
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
602

    
603
    if((uint64_t)atom.size > (1<<30))
604
        return -1;
605

    
606
    av_free(st->codec->extradata);
607

    
608
    st->codec->extradata_size = atom.size;
609
    st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
610

    
611
    if (st->codec->extradata) {
612
        get_buffer(pb, st->codec->extradata, atom.size);
613
    } else
614
        url_fskip(pb, atom.size);
615

    
616
    return 0;
617
}
618

    
619
static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
620
{
621
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
622
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
623
    unsigned int i, entries;
624

    
625
    get_byte(pb); /* version */
626
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
627

    
628
    entries = get_be32(pb);
629

    
630
    if(entries >= UINT_MAX/sizeof(int64_t))
631
        return -1;
632

    
633
    sc->chunk_count = entries;
634
    sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
635
    if (!sc->chunk_offsets)
636
        return -1;
637
    if (atom.type == MKTAG('s', 't', 'c', 'o')) {
638
        for(i=0; i<entries; i++) {
639
            sc->chunk_offsets[i] = get_be32(pb);
640
        }
641
    } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
642
        for(i=0; i<entries; i++) {
643
            sc->chunk_offsets[i] = get_be64(pb);
644
        }
645
    } else
646
        return -1;
647

    
648
    return 0;
649
}
650

    
651
static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
652
{
653
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
654
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
655
    int entries, frames_per_sample;
656
    uint32_t format;
657
    uint8_t codec_name[32];
658

    
659
    /* for palette traversal */
660
    int color_depth;
661
    int color_start;
662
    int color_count;
663
    int color_end;
664
    int color_index;
665
    int color_dec;
666
    int color_greyscale;
667
    unsigned char *color_table;
668
    int j;
669
    unsigned char r, g, b;
670

    
671
    get_byte(pb); /* version */
672
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
673

    
674
    entries = get_be32(pb);
675

    
676
    while(entries--) { //Parsing Sample description table
677
        enum CodecID id;
678
        MOV_atom_t a = { 0, 0, 0 };
679
        offset_t start_pos = url_ftell(pb);
680
        int size = get_be32(pb); /* size */
681
        format = get_le32(pb); /* data format */
682

    
683
        get_be32(pb); /* reserved */
684
        get_be16(pb); /* reserved */
685
        get_be16(pb); /* index */
686

    
687
        if (st->codec->codec_tag) {
688
            /* multiple fourcc, just skip for now */
689
            url_fskip(pb, size - (url_ftell(pb) - start_pos));
690
            continue;
691
        }
692

    
693
        st->codec->codec_tag = format;
694
        id = codec_get_id(codec_movaudio_tags, format);
695
        if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
696
            st->codec->codec_type = CODEC_TYPE_AUDIO;
697
        } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
698
                   format && format != MKTAG('m', 'p', '4', 's')) { /* skip old asf mpeg4 tag */
699
            id = codec_get_id(codec_movvideo_tags, format);
700
            if (id <= 0)
701
                id = codec_get_id(codec_bmp_tags, format);
702
            if (id > 0)
703
                st->codec->codec_type = CODEC_TYPE_VIDEO;
704
        }
705

    
706
        dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n",
707
                size,
708
                (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, (format >> 24) & 0xff,
709
                st->codec->codec_type);
710

    
711
        if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
712
            st->codec->codec_id = id;
713
            get_be16(pb); /* version */
714
            get_be16(pb); /* revision level */
715
            get_be32(pb); /* vendor */
716
            get_be32(pb); /* temporal quality */
717
            get_be32(pb); /* spacial quality */
718

    
719
            st->codec->width = get_be16(pb); /* width */
720
            st->codec->height = get_be16(pb); /* height */
721

    
722
            get_be32(pb); /* horiz resolution */
723
            get_be32(pb); /* vert resolution */
724
            get_be32(pb); /* data size, always 0 */
725
            frames_per_sample = get_be16(pb); /* frames per samples */
726
#ifdef DEBUG
727
            av_log(NULL, AV_LOG_DEBUG, "frames/samples = %d\n", frames_per_sample);
728
#endif
729
            get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */
730
            if (codec_name[0] <= 31) {
731
                memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
732
                st->codec->codec_name[codec_name[0]] = 0;
733
            }
734

    
735
            st->codec->bits_per_sample = get_be16(pb); /* depth */
736
            st->codec->color_table_id = get_be16(pb); /* colortable id */
737

    
738
            /* figure out the palette situation */
739
            color_depth = st->codec->bits_per_sample & 0x1F;
740
            color_greyscale = st->codec->bits_per_sample & 0x20;
741

    
742
            /* if the depth is 2, 4, or 8 bpp, file is palettized */
743
            if ((color_depth == 2) || (color_depth == 4) ||
744
                (color_depth == 8)) {
745

    
746
                if (color_greyscale) {
747

    
748
                    /* compute the greyscale palette */
749
                    color_count = 1 << color_depth;
750
                    color_index = 255;
751
                    color_dec = 256 / (color_count - 1);
752
                    for (j = 0; j < color_count; j++) {
753
                        r = g = b = color_index;
754
                        c->palette_control.palette[j] =
755
                            (r << 16) | (g << 8) | (b);
756
                        color_index -= color_dec;
757
                        if (color_index < 0)
758
                            color_index = 0;
759
                    }
760

    
761
                } else if (st->codec->color_table_id & 0x08) {
762

    
763
                    /* if flag bit 3 is set, use the default palette */
764
                    color_count = 1 << color_depth;
765
                    if (color_depth == 2)
766
                        color_table = ff_qt_default_palette_4;
767
                    else if (color_depth == 4)
768
                        color_table = ff_qt_default_palette_16;
769
                    else
770
                        color_table = ff_qt_default_palette_256;
771

    
772
                    for (j = 0; j < color_count; j++) {
773
                        r = color_table[j * 4 + 0];
774
                        g = color_table[j * 4 + 1];
775
                        b = color_table[j * 4 + 2];
776
                        c->palette_control.palette[j] =
777
                            (r << 16) | (g << 8) | (b);
778
                    }
779

    
780
                } else {
781

    
782
                    /* load the palette from the file */
783
                    color_start = get_be32(pb);
784
                    color_count = get_be16(pb);
785
                    color_end = get_be16(pb);
786
                    for (j = color_start; j <= color_end; j++) {
787
                        /* each R, G, or B component is 16 bits;
788
                         * only use the top 8 bits; skip alpha bytes
789
                         * up front */
790
                        get_byte(pb);
791
                        get_byte(pb);
792
                        r = get_byte(pb);
793
                        get_byte(pb);
794
                        g = get_byte(pb);
795
                        get_byte(pb);
796
                        b = get_byte(pb);
797
                        get_byte(pb);
798
                        c->palette_control.palette[j] =
799
                            (r << 16) | (g << 8) | (b);
800
                    }
801
                }
802

    
803
                st->codec->palctrl = &c->palette_control;
804
                st->codec->palctrl->palette_changed = 1;
805
            } else
806
                st->codec->palctrl = NULL;
807
        } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
808
            int bits_per_sample;
809
            uint16_t version = get_be16(pb);
810

    
811
            st->codec->codec_id = id;
812
            get_be16(pb); /* revision level */
813
            get_be32(pb); /* vendor */
814

    
815
            st->codec->channels = get_be16(pb);             /* channel count */
816
            dprintf(c->fc, "audio channels %d\n", st->codec->channels);
817
            st->codec->bits_per_sample = get_be16(pb);      /* sample size */
818
            /* do we need to force to 16 for AMR ? */
819

    
820
            /* handle specific s8 codec */
821
            get_be16(pb); /* compression id = 0*/
822
            get_be16(pb); /* packet size = 0 */
823

    
824
            st->codec->sample_rate = ((get_be32(pb) >> 16));
825

    
826
            switch (st->codec->codec_id) {
827
            case CODEC_ID_PCM_S8:
828
            case CODEC_ID_PCM_U8:
829
                if (st->codec->bits_per_sample == 16)
830
                    st->codec->codec_id = CODEC_ID_PCM_S16BE;
831
                break;
832
            case CODEC_ID_PCM_S16LE:
833
            case CODEC_ID_PCM_S16BE:
834
                if (st->codec->bits_per_sample == 8)
835
                    st->codec->codec_id = CODEC_ID_PCM_S8;
836
                else if (st->codec->bits_per_sample == 24)
837
                    st->codec->codec_id = CODEC_ID_PCM_S24BE;
838
                break;
839
            default:
840
                break;
841
            }
842

    
843
            //Read QT version 1 fields. In version 0 theese dont exist
844
            dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
845
            if(!c->isom) {
846
                if(version==1) {
847
                    sc->samples_per_frame = get_be32(pb);
848
                    get_be32(pb); /* bytes per packet */
849
                    sc->bytes_per_frame = get_be32(pb);
850
                    get_be32(pb); /* bytes per sample */
851
                } else if(version==2) {
852
                    get_be32(pb); /* sizeof struct only */
853
                    st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
854
                    st->codec->channels = get_be32(pb);
855
                    get_be32(pb); /* always 0x7F000000 */
856
                    get_be32(pb); /* bits per channel if sound is uncompressed */
857
                    get_be32(pb); /* lcpm format specific flag */
858
                    get_be32(pb); /* bytes per audio packet if constant */
859
                    get_be32(pb); /* lpcm frames per audio packet if constant */
860
                }
861
            }
862

    
863
            bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
864
            if (bits_per_sample) {
865
                st->codec->bits_per_sample = bits_per_sample;
866
                sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
867
            }
868
        } else {
869
            /* other codec type, just skip (rtp, mp4s, tmcd ...) */
870
            url_fskip(pb, size - (url_ftell(pb) - start_pos));
871
        }
872
        /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
873
        a.size = size - (url_ftell(pb) - start_pos);
874
        if (a.size > 8)
875
            mov_read_default(c, pb, a);
876
        else if (a.size > 0)
877
            url_fskip(pb, a.size);
878
    }
879

    
880
    if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1) {
881
        st->codec->sample_rate= sc->time_scale;
882
    }
883

    
884
    /* special codec parameters handling */
885
    switch (st->codec->codec_id) {
886
#ifdef CONFIG_H261_DECODER
887
    case CODEC_ID_H261:
888
#endif
889
#ifdef CONFIG_H263_DECODER
890
    case CODEC_ID_H263:
891
#endif
892
#ifdef CONFIG_MPEG4_DECODER
893
    case CODEC_ID_MPEG4:
894
#endif
895
        st->codec->width= 0; /* let decoder init width/height */
896
        st->codec->height= 0;
897
        break;
898
#ifdef CONFIG_LIBFAAD
899
    case CODEC_ID_AAC:
900
#endif
901
#ifdef CONFIG_VORBIS_DECODER
902
    case CODEC_ID_VORBIS:
903
#endif
904
    case CODEC_ID_MP3ON4:
905
        st->codec->sample_rate= 0; /* let decoder init parameters properly */
906
        break;
907
#ifdef CONFIG_DV_DEMUXER
908
    case CODEC_ID_DVAUDIO:
909
        c->dv_fctx = av_alloc_format_context();
910
        c->dv_demux = dv_init_demux(c->dv_fctx);
911
        if (!c->dv_demux) {
912
            av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
913
            return -1;
914
        }
915
        sc->dv_audio_container = 1;
916
        st->codec->codec_id = CODEC_ID_PCM_S16LE;
917
        break;
918
#endif
919
    /* no ifdef since parameters are always those */
920
    case CODEC_ID_AMR_WB:
921
        st->codec->sample_rate= 16000;
922
        st->codec->channels= 1; /* really needed */
923
        break;
924
    case CODEC_ID_AMR_NB:
925
        st->codec->sample_rate= 8000;
926
        st->codec->channels= 1; /* really needed */
927
        break;
928
    case CODEC_ID_MP2:
929
    case CODEC_ID_MP3:
930
        st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
931
        st->need_parsing = 1;
932
        break;
933
    default:
934
        break;
935
    }
936

    
937
    return 0;
938
}
939

    
940
static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
941
{
942
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
943
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
944
    unsigned int i, entries;
945

    
946
    get_byte(pb); /* version */
947
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
948

    
949
    entries = get_be32(pb);
950

    
951
    if(entries >= UINT_MAX / sizeof(MOV_sample_to_chunk_tbl))
952
        return -1;
953

    
954
#ifdef DEBUG
955
av_log(NULL, AV_LOG_DEBUG, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
956
#endif
957
    sc->sample_to_chunk_sz = entries;
958
    sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
959
    if (!sc->sample_to_chunk)
960
        return -1;
961
    for(i=0; i<entries; i++) {
962
        sc->sample_to_chunk[i].first = get_be32(pb);
963
        sc->sample_to_chunk[i].count = get_be32(pb);
964
        sc->sample_to_chunk[i].id = get_be32(pb);
965
    }
966
    return 0;
967
}
968

    
969
static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
970
{
971
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
972
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
973
    unsigned int i, entries;
974

    
975
    get_byte(pb); /* version */
976
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
977

    
978
    entries = get_be32(pb);
979

    
980
    if(entries >= UINT_MAX / sizeof(long))
981
        return -1;
982

    
983
    sc->keyframe_count = entries;
984
#ifdef DEBUG
985
    av_log(NULL, AV_LOG_DEBUG, "keyframe_count = %d\n", sc->keyframe_count);
986
#endif
987
    sc->keyframes = av_malloc(entries * sizeof(long));
988
    if (!sc->keyframes)
989
        return -1;
990
    for(i=0; i<entries; i++) {
991
        sc->keyframes[i] = get_be32(pb);
992
#ifdef DEBUG
993
/*        av_log(NULL, AV_LOG_DEBUG, "keyframes[]=%ld\n", sc->keyframes[i]); */
994
#endif
995
    }
996
    return 0;
997
}
998

    
999
static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1000
{
1001
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1002
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
1003
    unsigned int i, entries, sample_size;
1004

    
1005
    get_byte(pb); /* version */
1006
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1007

    
1008
    sample_size = get_be32(pb);
1009
    if (!sc->sample_size) /* do not overwrite value computed in stsd */
1010
        sc->sample_size = sample_size;
1011
    entries = get_be32(pb);
1012
    if(entries >= UINT_MAX / sizeof(long))
1013
        return -1;
1014

    
1015
    sc->sample_count = entries;
1016
    if (sample_size)
1017
        return 0;
1018

    
1019
#ifdef DEBUG
1020
    av_log(NULL, AV_LOG_DEBUG, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count);
1021
#endif
1022
    sc->sample_sizes = av_malloc(entries * sizeof(long));
1023
    if (!sc->sample_sizes)
1024
        return -1;
1025
    for(i=0; i<entries; i++) {
1026
        sc->sample_sizes[i] = get_be32(pb);
1027
#ifdef DEBUG
1028
        av_log(NULL, AV_LOG_DEBUG, "sample_sizes[]=%ld\n", sc->sample_sizes[i]);
1029
#endif
1030
    }
1031
    return 0;
1032
}
1033

    
1034
static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1035
{
1036
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1037
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
1038
    unsigned int i, entries;
1039
    int64_t duration=0;
1040
    int64_t total_sample_count=0;
1041

    
1042
    get_byte(pb); /* version */
1043
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1044
    entries = get_be32(pb);
1045
    if(entries >= UINT_MAX / sizeof(Time2Sample))
1046
        return -1;
1047

    
1048
    sc->stts_count = entries;
1049
    sc->stts_data = av_malloc(entries * sizeof(Time2Sample));
1050

    
1051
#ifdef DEBUG
1052
av_log(NULL, AV_LOG_DEBUG, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
1053
#endif
1054

    
1055
    sc->time_rate=0;
1056

    
1057
    for(i=0; i<entries; i++) {
1058
        int sample_duration;
1059
        int sample_count;
1060

    
1061
        sample_count=get_be32(pb);
1062
        sample_duration = get_be32(pb);
1063
        sc->stts_data[i].count= sample_count;
1064
        sc->stts_data[i].duration= sample_duration;
1065

    
1066
        sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
1067

    
1068
        dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
1069

    
1070
        duration+=(int64_t)sample_duration*sample_count;
1071
        total_sample_count+=sample_count;
1072
    }
1073

    
1074
    st->nb_frames= total_sample_count;
1075
    if(duration)
1076
        st->duration= duration;
1077
    return 0;
1078
}
1079

    
1080
static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1081
{
1082
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1083
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
1084
    unsigned int i, entries;
1085

    
1086
    get_byte(pb); /* version */
1087
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1088
    entries = get_be32(pb);
1089
    if(entries >= UINT_MAX / sizeof(Time2Sample))
1090
        return -1;
1091

    
1092
    sc->ctts_count = entries;
1093
    sc->ctts_data = av_malloc(entries * sizeof(Time2Sample));
1094

    
1095
    dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
1096

    
1097
    for(i=0; i<entries; i++) {
1098
        int count    =get_be32(pb);
1099
        int duration =get_be32(pb);
1100

    
1101
        if (duration < 0) {
1102
            av_log(c->fc, AV_LOG_ERROR, "negative ctts, ignoring\n");
1103
            sc->ctts_count = 0;
1104
            url_fskip(pb, 8 * (entries - i - 1));
1105
            break;
1106
        }
1107
        sc->ctts_data[i].count   = count;
1108
        sc->ctts_data[i].duration= duration;
1109

    
1110
        sc->time_rate= ff_gcd(sc->time_rate, duration);
1111
    }
1112
    return 0;
1113
}
1114

    
1115
static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1116
{
1117
    AVStream *st;
1118
    MOVStreamContext *sc;
1119

    
1120
    st = av_new_stream(c->fc, c->fc->nb_streams);
1121
    if (!st) return -2;
1122
    sc = av_mallocz(sizeof(MOVStreamContext));
1123
    if (!sc) {
1124
        av_free(st);
1125
        return -1;
1126
    }
1127

    
1128
    st->priv_data = sc;
1129
    st->codec->codec_type = CODEC_TYPE_DATA;
1130
    st->start_time = 0; /* XXX: check */
1131
    c->streams[c->fc->nb_streams-1] = sc;
1132

    
1133
    return mov_read_default(c, pb, atom);
1134
}
1135

    
1136
static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1137
{
1138
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1139
    int version = get_byte(pb);
1140

    
1141
    get_byte(pb); get_byte(pb);
1142
    get_byte(pb); /* flags */
1143
    /*
1144
    MOV_TRACK_ENABLED 0x0001
1145
    MOV_TRACK_IN_MOVIE 0x0002
1146
    MOV_TRACK_IN_PREVIEW 0x0004
1147
    MOV_TRACK_IN_POSTER 0x0008
1148
    */
1149

    
1150
    if (version == 1) {
1151
        get_be64(pb);
1152
        get_be64(pb);
1153
    } else {
1154
        get_be32(pb); /* creation time */
1155
        get_be32(pb); /* modification time */
1156
    }
1157
    st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
1158
    get_be32(pb); /* reserved */
1159
    st->start_time = 0; /* check */
1160
    (version == 1) ? get_be64(pb) : get_be32(pb); /* highlevel (considering edits) duration in movie timebase */
1161
    get_be32(pb); /* reserved */
1162
    get_be32(pb); /* reserved */
1163

    
1164
    get_be16(pb); /* layer */
1165
    get_be16(pb); /* alternate group */
1166
    get_be16(pb); /* volume */
1167
    get_be16(pb); /* reserved */
1168

    
1169
    url_fskip(pb, 36); /* display matrix */
1170

    
1171
    /* those are fixed-point */
1172
    get_be32(pb); /* track width */
1173
    get_be32(pb); /* track height */
1174

    
1175
    return 0;
1176
}
1177

    
1178
/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
1179
/* like the files created with Adobe Premiere 5.0, for samples see */
1180
/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
1181
static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1182
{
1183
    int err;
1184

    
1185
    if (atom.size < 8)
1186
        return 0; /* continue */
1187
    if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
1188
        url_fskip(pb, atom.size - 4);
1189
        return 0;
1190
    }
1191
    atom.type = get_le32(pb);
1192
    atom.offset += 8;
1193
    atom.size -= 8;
1194
    if (atom.type != MKTAG('m', 'd', 'a', 't')) {
1195
        url_fskip(pb, atom.size);
1196
        return 0;
1197
    }
1198
    err = mov_read_mdat(c, pb, atom);
1199
    return err;
1200
}
1201

    
1202
static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1203
{
1204
#ifdef CONFIG_ZLIB
1205
    ByteIOContext ctx;
1206
    uint8_t *cmov_data;
1207
    uint8_t *moov_data; /* uncompressed data */
1208
    long cmov_len, moov_len;
1209
    int ret;
1210

    
1211
    get_be32(pb); /* dcom atom */
1212
    if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
1213
        return -1;
1214
    if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
1215
        av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
1216
        return -1;
1217
    }
1218
    get_be32(pb); /* cmvd atom */
1219
    if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
1220
        return -1;
1221
    moov_len = get_be32(pb); /* uncompressed size */
1222
    cmov_len = atom.size - 6 * 4;
1223

    
1224
    cmov_data = av_malloc(cmov_len);
1225
    if (!cmov_data)
1226
        return -1;
1227
    moov_data = av_malloc(moov_len);
1228
    if (!moov_data) {
1229
        av_free(cmov_data);
1230
        return -1;
1231
    }
1232
    get_buffer(pb, cmov_data, cmov_len);
1233
    if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
1234
        return -1;
1235
    if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
1236
        return -1;
1237
    atom.type = MKTAG( 'm', 'o', 'o', 'v' );
1238
    atom.offset = 0;
1239
    atom.size = moov_len;
1240
#ifdef DEBUG
1241
//    { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
1242
#endif
1243
    ret = mov_read_default(c, &ctx, atom);
1244
    av_free(moov_data);
1245
    av_free(cmov_data);
1246
    return ret;
1247
#else
1248
    av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
1249
    return -1;
1250
#endif
1251
}
1252

    
1253
/* edit list atom */
1254
static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1255
{
1256
    int i, edit_count;
1257

    
1258
    get_byte(pb); /* version */
1259
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1260
    edit_count= c->streams[c->fc->nb_streams-1]->edit_count = get_be32(pb);     /* entries */
1261

    
1262
    for(i=0; i<edit_count; i++){
1263
        get_be32(pb); /* Track duration */
1264
        get_be32(pb); /* Media time */
1265
        get_be32(pb); /* Media rate */
1266
    }
1267
    dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, c->streams[c->fc->nb_streams-1]->edit_count);
1268
    return 0;
1269
}
1270

    
1271
static const MOVParseTableEntry mov_default_parse_table[] = {
1272
/* mp4 atoms */
1273
{ MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
1274
{ MKTAG( 'c', 't', 't', 's' ), mov_read_ctts }, /* composition time to sample */
1275
{ MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
1276
{ MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
1277
{ MKTAG( 'e', 'n', 'd', 'a' ), mov_read_enda },
1278
{ MKTAG( 'f', 'i', 'e', 'l' ), mov_read_extradata },
1279
{ MKTAG( 'f', 't', 'y', 'p' ), mov_read_ftyp },
1280
{ MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
1281
{ MKTAG( 'j', 'p', '2', 'h' ), mov_read_extradata },
1282
{ MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
1283
{ MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
1284
{ MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
1285
{ MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
1286
{ MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
1287
{ MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
1288
{ MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
1289
{ MKTAG( 'a', 'l', 'a', 'c' ), mov_read_extradata }, /* alac specific atom */
1290
{ MKTAG( 'a', 'v', 'c', 'C' ), mov_read_avcC },
1291
{ MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
1292
{ MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
1293
{ MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
1294
{ MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
1295
{ MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
1296
{ MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
1297
{ MKTAG( 's', 't', 't', 's' ), mov_read_stts },
1298
{ MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
1299
{ MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
1300
{ MKTAG( 'w', 'a', 'v', 'e' ), mov_read_wave },
1301
{ MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
1302
{ MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
1303
{ MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
1304
{ MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
1305
{ 0L, NULL }
1306
};
1307

    
1308
static void mov_free_stream_context(MOVStreamContext *sc)
1309
{
1310
    if(sc) {
1311
        av_freep(&sc->ctts_data);
1312
        av_freep(&sc);
1313
    }
1314
}
1315

    
1316
/* XXX: is it sufficient ? */
1317
static int mov_probe(AVProbeData *p)
1318
{
1319
    unsigned int offset;
1320
    uint32_t tag;
1321
    int score = 0;
1322

    
1323
    /* check file header */
1324
    if (p->buf_size <= 12)
1325
        return 0;
1326
    offset = 0;
1327
    for(;;) {
1328
        /* ignore invalid offset */
1329
        if ((offset + 8) > (unsigned int)p->buf_size)
1330
            return score;
1331
        tag = AV_RL32(p->buf + offset + 4);
1332
        switch(tag) {
1333
        /* check for obvious tags */
1334
        case MKTAG( 'j', 'P', ' ', ' ' ): /* jpeg 2000 signature */
1335
        case MKTAG( 'm', 'o', 'o', 'v' ):
1336
        case MKTAG( 'm', 'd', 'a', 't' ):
1337
        case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
1338
        case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
1339
            return AVPROBE_SCORE_MAX;
1340
        /* those are more common words, so rate then a bit less */
1341
        case MKTAG( 'w', 'i', 'd', 'e' ):
1342
        case MKTAG( 'f', 'r', 'e', 'e' ):
1343
        case MKTAG( 'j', 'u', 'n', 'k' ):
1344
        case MKTAG( 'p', 'i', 'c', 't' ):
1345
            return AVPROBE_SCORE_MAX - 5;
1346
        case MKTAG( 'f', 't', 'y', 'p' ):
1347
        case MKTAG( 's', 'k', 'i', 'p' ):
1348
        case MKTAG( 'u', 'u', 'i', 'd' ):
1349
            offset = AV_RB32(p->buf+offset) + offset;
1350
            /* if we only find those cause probedata is too small at least rate them */
1351
            score = AVPROBE_SCORE_MAX - 50;
1352
            break;
1353
        default:
1354
            /* unrecognized tag */
1355
            return score;
1356
        }
1357
    }
1358
    return score;
1359
}
1360

    
1361
static void mov_build_index(MOVContext *mov, AVStream *st)
1362
{
1363
    MOVStreamContext *sc = st->priv_data;
1364
    offset_t current_offset;
1365
    int64_t current_dts = 0;
1366
    unsigned int stts_index = 0;
1367
    unsigned int stsc_index = 0;
1368
    unsigned int stss_index = 0;
1369
    unsigned int i, j, k;
1370

    
1371
    if (sc->sample_sizes || st->codec->codec_type == CODEC_TYPE_VIDEO || sc->dv_audio_container) {
1372
        unsigned int current_sample = 0;
1373
        unsigned int stts_sample = 0;
1374
        unsigned int keyframe, sample_size;
1375
        unsigned int distance = 0;
1376

    
1377
        st->nb_frames = sc->sample_count;
1378
        for (i = 0; i < sc->chunk_count; i++) {
1379
            current_offset = sc->chunk_offsets[i];
1380
            if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
1381
                stsc_index++;
1382
            for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) {
1383
                if (current_sample >= sc->sample_count) {
1384
                    av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
1385
                    goto out;
1386
                }
1387
                keyframe = !sc->keyframe_count || current_sample + 1 == sc->keyframes[stss_index];
1388
                if (keyframe) {
1389
                    distance = 0;
1390
                    if (stss_index + 1 < sc->keyframe_count)
1391
                        stss_index++;
1392
                }
1393
                sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
1394
                dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", size %d, distance %d, keyframe %d\n",
1395
                        st->index, current_sample, current_offset, current_dts, sample_size, distance, keyframe);
1396
                av_add_index_entry(st, current_offset, current_dts, sample_size, distance, keyframe ? AVINDEX_KEYFRAME : 0);
1397
                current_offset += sample_size;
1398
                assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
1399
                current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
1400
                distance++;
1401
                stts_sample++;
1402
                current_sample++;
1403
                if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
1404
                    stts_sample = 0;
1405
                    stts_index++;
1406
                }
1407
            }
1408
        }
1409
    } else { /* read whole chunk */
1410
        unsigned int chunk_samples, chunk_size, chunk_duration;
1411

    
1412
        for (i = 0; i < sc->chunk_count; i++) {
1413
            current_offset = sc->chunk_offsets[i];
1414
            if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
1415
                stsc_index++;
1416
            chunk_samples = sc->sample_to_chunk[stsc_index].count;
1417
            /* get chunk size */
1418
            if (sc->sample_size > 1 || st->codec->codec_id == CODEC_ID_PCM_U8 || st->codec->codec_id == CODEC_ID_PCM_S8)
1419
                chunk_size = chunk_samples * sc->sample_size;
1420
            else if (sc->samples_per_frame > 0 && (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0))
1421
                chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
1422
            else { /* workaround to find nearest next chunk offset */
1423
                chunk_size = INT_MAX;
1424
                for (j = 0; j < mov->total_streams; j++) {
1425
                    MOVStreamContext *msc = mov->streams[j];
1426

    
1427
                    for (k = msc->next_chunk; k < msc->chunk_count; k++) {
1428
                        if (msc->chunk_offsets[k] > current_offset && msc->chunk_offsets[k] - current_offset < chunk_size) {
1429
                            chunk_size = msc->chunk_offsets[k] - current_offset;
1430
                            msc->next_chunk = k;
1431
                            break;
1432
                        }
1433
                    }
1434
                }
1435
                /* check for last chunk */
1436
                if (chunk_size == INT_MAX)
1437
                    for (j = 0; j < mov->mdat_count; j++) {
1438
                        dprintf(mov->fc, "mdat %d, offset %"PRIx64", size %"PRId64", current offset %"PRIx64"\n",
1439
                                j, mov->mdat_list[j].offset, mov->mdat_list[j].size, current_offset);
1440
                        if (mov->mdat_list[j].offset <= current_offset && mov->mdat_list[j].offset + mov->mdat_list[j].size > current_offset)
1441
                            chunk_size = mov->mdat_list[j].offset + mov->mdat_list[j].size - current_offset;
1442
                    }
1443
                assert(chunk_size != INT_MAX);
1444
                for (j = 0; j < mov->total_streams; j++) {
1445
                    mov->streams[j]->next_chunk = 0;
1446
                }
1447
            }
1448
            av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
1449
            /* get chunk duration */
1450
            chunk_duration = 0;
1451
            while (chunk_samples > 0) {
1452
                if (chunk_samples < sc->stts_data[stts_index].count) {
1453
                    chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1454
                    sc->stts_data[stts_index].count -= chunk_samples;
1455
                    break;
1456
                } else {
1457
                    chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1458
                    chunk_samples -= sc->stts_data[stts_index].count;
1459
                    if (stts_index + 1 < sc->stts_count) {
1460
                        stts_index++;
1461
                    }
1462
                }
1463
            }
1464
            dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", size %d, duration %d\n",
1465
                    st->index, i, current_offset, current_dts, chunk_size, chunk_duration);
1466
            assert(chunk_duration % sc->time_rate == 0);
1467
            current_dts += chunk_duration / sc->time_rate;
1468
        }
1469
    }
1470
 out:
1471
    /* adjust sample count to avindex entries */
1472
    sc->sample_count = st->nb_index_entries;
1473
}
1474

    
1475
static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1476
{
1477
    MOVContext *mov = (MOVContext *) s->priv_data;
1478
    ByteIOContext *pb = &s->pb;
1479
    int i, err;
1480
    MOV_atom_t atom = { 0, 0, 0 };
1481

    
1482
    mov->fc = s;
1483
    mov->parse_table = mov_default_parse_table;
1484

    
1485
    if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1486
        atom.size = url_fsize(pb);
1487
    else
1488
        atom.size = 0x7FFFFFFFFFFFFFFFLL;
1489

    
1490
    /* check MOV header */
1491
    err = mov_read_default(mov, pb, atom);
1492
    if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
1493
        av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%"PRId64"\n",
1494
                err, mov->found_moov, mov->found_mdat, url_ftell(pb));
1495
        return -1;
1496
    }
1497
    dprintf(mov->fc, "on_parse_exit_offset=%d\n", (int) url_ftell(pb));
1498

    
1499
    /* some cleanup : make sure we are on the mdat atom */
1500
    if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1501
        url_fseek(pb, mov->mdat_offset, SEEK_SET);
1502

    
1503
    mov->total_streams = s->nb_streams;
1504

    
1505
    for(i=0; i<mov->total_streams; i++) {
1506
        MOVStreamContext *sc = mov->streams[i];
1507
        AVStream *st = s->streams[i];
1508
        /* sanity checks */
1509
        if(!sc->stts_count || !sc->chunk_count || !sc->sample_to_chunk_sz ||
1510
           (!sc->sample_size && !sc->sample_count)){
1511
            av_log(s, AV_LOG_ERROR, "missing mandatory atoms, broken header\n");
1512
            continue;
1513
        }
1514
        if(!sc->time_rate)
1515
            sc->time_rate=1;
1516
        if(!sc->time_scale)
1517
            sc->time_scale= mov->time_scale;
1518
        av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
1519

    
1520
        if (st->codec->codec_type == CODEC_TYPE_AUDIO && sc->stts_count == 1)
1521
            st->codec->frame_size = sc->stts_data[0].duration;
1522

    
1523
        if(st->duration != AV_NOPTS_VALUE){
1524
            assert(st->duration % sc->time_rate == 0);
1525
            st->duration /= sc->time_rate;
1526
        }
1527
        sc->ffindex = i;
1528
        mov_build_index(mov, st);
1529
    }
1530

    
1531
    for(i=0; i<mov->total_streams; i++) {
1532
        /* dont need those anymore */
1533
        av_freep(&mov->streams[i]->chunk_offsets);
1534
        av_freep(&mov->streams[i]->sample_to_chunk);
1535
        av_freep(&mov->streams[i]->sample_sizes);
1536
        av_freep(&mov->streams[i]->keyframes);
1537
        av_freep(&mov->streams[i]->stts_data);
1538
    }
1539
    av_freep(&mov->mdat_list);
1540
    return 0;
1541
}
1542

    
1543
static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1544
{
1545
    MOVContext *mov = s->priv_data;
1546
    MOVStreamContext *sc = 0;
1547
    AVIndexEntry *sample = 0;
1548
    int64_t best_dts = INT64_MAX;
1549
    int i;
1550

    
1551
    for (i = 0; i < mov->total_streams; i++) {
1552
        MOVStreamContext *msc = mov->streams[i];
1553

    
1554
        if (s->streams[i]->discard != AVDISCARD_ALL && msc->current_sample < msc->sample_count) {
1555
            AVIndexEntry *current_sample = &s->streams[i]->index_entries[msc->current_sample];
1556
            int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate, AV_TIME_BASE, msc->time_scale);
1557

    
1558
            dprintf(s, "stream %d, sample %ld, dts %"PRId64"\n", i, msc->current_sample, dts);
1559
            if (dts < best_dts) {
1560
                sample = current_sample;
1561
                best_dts = dts;
1562
                sc = msc;
1563
            }
1564
        }
1565
    }
1566
    if (!sample)
1567
        return -1;
1568
    /* must be done just before reading, to avoid infinite loop on sample */
1569
    sc->current_sample++;
1570
    if (sample->pos >= url_fsize(&s->pb)) {
1571
        av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", sc->ffindex, sample->pos);
1572
        return -1;
1573
    }
1574
#ifdef CONFIG_DV_DEMUXER
1575
    if (sc->dv_audio_container) {
1576
        dv_get_packet(mov->dv_demux, pkt);
1577
        dprintf(s, "dv audio pkt size %d\n", pkt->size);
1578
    } else {
1579
#endif
1580
        url_fseek(&s->pb, sample->pos, SEEK_SET);
1581
        av_get_packet(&s->pb, pkt, sample->size);
1582
#ifdef CONFIG_DV_DEMUXER
1583
        if (mov->dv_demux) {
1584
            void *pkt_destruct_func = pkt->destruct;
1585
            dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
1586
            pkt->destruct = pkt_destruct_func;
1587
        }
1588
    }
1589
#endif
1590
    pkt->stream_index = sc->ffindex;
1591
    pkt->dts = sample->timestamp;
1592
    if (sc->ctts_data) {
1593
        assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0);
1594
        pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
1595
        /* update ctts context */
1596
        sc->sample_to_ctime_sample++;
1597
        if (sc->sample_to_ctime_index < sc->ctts_count && sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
1598
            sc->sample_to_ctime_index++;
1599
            sc->sample_to_ctime_sample = 0;
1600
        }
1601
    } else {
1602
        pkt->pts = pkt->dts;
1603
    }
1604
    pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
1605
    pkt->pos = sample->pos;
1606
    dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n", pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
1607
    return 0;
1608
}
1609

    
1610
static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
1611
{
1612
    MOVStreamContext *sc = st->priv_data;
1613
    int sample, time_sample;
1614
    int i;
1615

    
1616
    sample = av_index_search_timestamp(st, timestamp, flags);
1617
    dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
1618
    if (sample < 0) /* not sure what to do */
1619
        return -1;
1620
    sc->current_sample = sample;
1621
    dprintf(st->codec, "stream %d, found sample %ld\n", st->index, sc->current_sample);
1622
    /* adjust ctts index */
1623
    if (sc->ctts_data) {
1624
        time_sample = 0;
1625
        for (i = 0; i < sc->ctts_count; i++) {
1626
            time_sample += sc->ctts_data[i].count;
1627
            if (time_sample >= sc->current_sample) {
1628
                sc->sample_to_ctime_index = i;
1629
                sc->sample_to_ctime_sample = time_sample - sc->current_sample;
1630
                break;
1631
            }
1632
        }
1633
    }
1634
    return sample;
1635
}
1636

    
1637
static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
1638
{
1639
    AVStream *st;
1640
    int64_t seek_timestamp, timestamp;
1641
    int sample;
1642
    int i;
1643

    
1644
    if (stream_index >= s->nb_streams)
1645
        return -1;
1646

    
1647
    st = s->streams[stream_index];
1648
    sample = mov_seek_stream(st, sample_time, flags);
1649
    if (sample < 0)
1650
        return -1;
1651

    
1652
    /* adjust seek timestamp to found sample timestamp */
1653
    seek_timestamp = st->index_entries[sample].timestamp;
1654

    
1655
    for (i = 0; i < s->nb_streams; i++) {
1656
        st = s->streams[i];
1657
        if (stream_index == i || st->discard == AVDISCARD_ALL)
1658
            continue;
1659

    
1660
        timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
1661
        mov_seek_stream(st, timestamp, flags);
1662
    }
1663
    return 0;
1664
}
1665

    
1666
static int mov_read_close(AVFormatContext *s)
1667
{
1668
    int i;
1669
    MOVContext *mov = (MOVContext *) s->priv_data;
1670
    for(i=0; i<mov->total_streams; i++)
1671
        mov_free_stream_context(mov->streams[i]);
1672
    /* free color tabs */
1673
    for(i=0; i<mov->ctab_size; i++)
1674
        av_freep(&mov->ctab[i]);
1675
    if(mov->dv_demux){
1676
        for(i=0; i<mov->dv_fctx->nb_streams; i++){
1677
            av_freep(&mov->dv_fctx->streams[i]->codec);
1678
            av_freep(&mov->dv_fctx->streams[i]);
1679
        }
1680
        av_freep(&mov->dv_fctx);
1681
        av_freep(&mov->dv_demux);
1682
    }
1683
    av_freep(&mov->ctab);
1684
    return 0;
1685
}
1686

    
1687
AVInputFormat mov_demuxer = {
1688
    "mov,mp4,m4a,3gp,3g2,mj2",
1689
    "QuickTime/MPEG4/Motion JPEG 2000 format",
1690
    sizeof(MOVContext),
1691
    mov_probe,
1692
    mov_read_header,
1693
    mov_read_packet,
1694
    mov_read_close,
1695
    mov_read_seek,
1696
};