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 "libavutil/intreadwrite.h"
27#include "libavutil/avstring.h"
28#include "avformat.h"
29#include "riff.h"
30#include "isom.h"
31#include "dv.h"
32#include "libavcodec/mpeg4audio.h"
33#include "libavcodec/mpegaudiodata.h"
34
35#if CONFIG_ZLIB
36#include <zlib.h>
37#endif
38
39/*
40 * First version by Francois Revol revol@free.fr
41 * Seek function by Gael Chardon gael.dev@4now.net
42 *
43 * Features and limitations:
44 * - reads most of the QT files I have (at least the structure),
45 *   Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
46 * - the code is quite ugly... maybe I won't do it recursive next time :-)
47 *
48 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
49 * when coding this :) (it's a writer anyway)
50 *
51 * Reference documents:
52 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
53 * Apple:
54 *  http://developer.apple.com/documentation/QuickTime/QTFF/
55 *  http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
56 * QuickTime is a trademark of Apple (AFAIK :))
57 */
58
59#include "qtpalette.h"
60
61
62#undef NDEBUG
63#include <assert.h>
64
65/* the QuickTime file format is quite convoluted...
66 * it has lots of index tables, each indexing something in another one...
67 * Here we just use what is needed to read the chunks
68 */
69
70typedef struct {
71    int first;
72    int count;
73    int id;
74} MOVStsc;
75
76typedef struct {
77    uint32_t type;
78    char *path;
79} MOVDref;
80
81typedef struct {
82    uint32_t type;
83    int64_t offset;
84    int64_t size; /* total size (excluding the size and type fields) */
85} MOVAtom;
86
87struct MOVParseTableEntry;
88
89typedef struct {
90    unsigned track_id;
91    uint64_t base_data_offset;
92    uint64_t moof_offset;
93    unsigned stsd_id;
94    unsigned duration;
95    unsigned size;
96    unsigned flags;
97} MOVFragment;
98
99typedef struct {
100    unsigned track_id;
101    unsigned stsd_id;
102    unsigned duration;
103    unsigned size;
104    unsigned flags;
105} MOVTrackExt;
106
107typedef struct MOVStreamContext {
108    ByteIOContext *pb;
109    int ffindex; /* the ffmpeg stream id */
110    int next_chunk;
111    unsigned int chunk_count;
112    int64_t *chunk_offsets;
113    unsigned int stts_count;
114    MOVStts *stts_data;
115    unsigned int ctts_count;
116    MOVStts *ctts_data;
117    unsigned int stsc_count;
118    MOVStsc *stsc_data;
119    int ctts_index;
120    int ctts_sample;
121    unsigned int sample_size;
122    unsigned int sample_count;
123    int *sample_sizes;
124    unsigned int keyframe_count;
125    int *keyframes;
126    int time_scale;
127    int time_rate;
128    int time_offset; ///< time offset of the first edit list entry
129    int current_sample;
130    unsigned int bytes_per_frame;
131    unsigned int samples_per_frame;
132    int dv_audio_container;
133    int pseudo_stream_id; ///< -1 means demux all ids
134    int16_t audio_cid; ///< stsd audio compression id
135    unsigned drefs_count;
136    MOVDref *drefs;
137    int dref_id;
138    int wrong_dts; ///< dts are wrong due to negative ctts
139    int width;  ///< tkhd width
140    int height; ///< tkhd height
141} MOVStreamContext;
142
143typedef struct MOVContext {
144    AVFormatContext *fc;
145    int time_scale;
146    int64_t duration; /* duration of the longest track */
147    int found_moov; /* when both 'moov' and 'mdat' sections has been found */
148    int found_mdat; /* we suppose we have enough data to read the file */
149    AVPaletteControl palette_control;
150    DVDemuxContext *dv_demux;
151    AVFormatContext *dv_fctx;
152    int isom; /* 1 if file is ISO Media (mp4/3gp) */
153    MOVFragment fragment; ///< current fragment in moof atom
154    MOVTrackExt *trex_data;
155    unsigned trex_count;
156    int itunes_metadata; ///< metadata are itunes style
157} MOVContext;
158
159
160/* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
161
162/* those functions parse an atom */
163/* return code:
164  0: continue to parse next atom
165 <0: error occurred, exit
166*/
167/* links atom IDs to parse functions */
168typedef struct MOVParseTableEntry {
169    uint32_t type;
170    int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOVAtom atom);
171} MOVParseTableEntry;
172
173static const MOVParseTableEntry mov_default_parse_table[];
174
175static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
176{
177    int64_t total_size = 0;
178    MOVAtom a;
179    int i;
180    int err = 0;
181
182    a.offset = atom.offset;
183
184    if (atom.size < 0)
185        atom.size = INT64_MAX;
186    while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
187        a.size = atom.size;
188        a.type=0;
189        if(atom.size >= 8) {
190            a.size = get_be32(pb);
191            a.type = get_le32(pb);
192        }
193        total_size += 8;
194        a.offset += 8;
195        dprintf(c->fc, "type: %08x  %.4s  sz: %"PRIx64"  %"PRIx64"   %"PRIx64"\n",
196                a.type, (char*)&a.type, a.size, atom.size, total_size);
197        if (a.size == 1) { /* 64 bit extended size */
198            a.size = get_be64(pb) - 8;
199            a.offset += 8;
200            total_size += 8;
201        }
202        if (a.size == 0) {
203            a.size = atom.size - total_size;
204            if (a.size <= 8)
205                break;
206        }
207        a.size -= 8;
208        if(a.size < 0)
209            break;
210        a.size = FFMIN(a.size, atom.size - total_size);
211
212        for (i = 0; mov_default_parse_table[i].type != 0
213             && mov_default_parse_table[i].type != a.type; i++)
214            /* empty */;
215
216        if (mov_default_parse_table[i].type == 0) { /* skip leaf atoms data */
217            url_fskip(pb, a.size);
218        } else {
219            int64_t start_pos = url_ftell(pb);
220            int64_t left;
221            err = mov_default_parse_table[i].parse(c, pb, a);
222            if (url_is_streamed(pb) && c->found_moov && c->found_mdat)
223                break;
224            left = a.size - url_ftell(pb) + start_pos;
225            if (left > 0) /* skip garbage at atom end */
226                url_fskip(pb, left);
227        }
228
229        a.offset += a.size;
230        total_size += a.size;
231    }
232
233    if (!err && total_size < atom.size && atom.size < 0x7ffff)
234        url_fskip(pb, atom.size - total_size);
235
236    return err;
237}
238
239static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
240{
241    AVStream *st;
242    MOVStreamContext *sc;
243    int entries, i, j;
244
245    if (c->fc->nb_streams < 1)
246        return 0;
247    st = c->fc->streams[c->fc->nb_streams-1];
248    sc = st->priv_data;
249
250    get_be32(pb); // version + flags
251    entries = get_be32(pb);
252    if (entries >= UINT_MAX / sizeof(*sc->drefs))
253        return -1;
254    sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
255    if (!sc->drefs)
256        return AVERROR(ENOMEM);
257    sc->drefs_count = entries;
258
259    for (i = 0; i < sc->drefs_count; i++) {
260        MOVDref *dref = &sc->drefs[i];
261        uint32_t size = get_be32(pb);
262        int64_t next = url_ftell(pb) + size - 4;
263
264        dref->type = get_le32(pb);
265        get_be32(pb); // version + flags
266        dprintf(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
267
268        if (dref->type == MKTAG('a','l','i','s') && size > 150) {
269            /* macintosh alias record */
270            uint16_t volume_len, len;
271            char volume[28];
272            int16_t type;
273
274            url_fskip(pb, 10);
275
276            volume_len = get_byte(pb);
277            volume_len = FFMIN(volume_len, 27);
278            get_buffer(pb, volume, 27);
279            volume[volume_len] = 0;
280            av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", volume, volume_len);
281
282            url_fskip(pb, 112);
283
284            for (type = 0; type != -1 && url_ftell(pb) < next; ) {
285                type = get_be16(pb);
286                len = get_be16(pb);
287                av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
288                if (len&1)
289                    len += 1;
290                if (type == 2) { // absolute path
291                    av_free(dref->path);
292                    dref->path = av_mallocz(len+1);
293                    if (!dref->path)
294                        return AVERROR(ENOMEM);
295                    get_buffer(pb, dref->path, len);
296                    if (len > volume_len && !strncmp(dref->path, volume, volume_len)) {
297                        len -= volume_len;
298                        memmove(dref->path, dref->path+volume_len, len);
299                        dref->path[len] = 0;
300                    }
301                    for (j = 0; j < len; j++)
302                        if (dref->path[j] == ':')
303                            dref->path[j] = '/';
304                    av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
305                } else
306                    url_fskip(pb, len);
307            }
308        }
309        url_fseek(pb, next, SEEK_SET);
310    }
311    return 0;
312}
313
314static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
315{
316    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
317    uint32_t type;
318    uint32_t ctype;
319
320    get_byte(pb); /* version */
321    get_be24(pb); /* flags */
322
323    /* component type */
324    ctype = get_le32(pb);
325    type = get_le32(pb); /* component subtype */
326
327    dprintf(c->fc, "ctype= %c%c%c%c (0x%08x)\n", *((char *)&ctype), ((char *)&ctype)[1],
328            ((char *)&ctype)[2], ((char *)&ctype)[3], (int) ctype);
329    dprintf(c->fc, "stype= %c%c%c%c\n",
330            *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
331    if(!ctype)
332        c->isom = 1;
333    if     (type == MKTAG('v','i','d','e'))
334        st->codec->codec_type = CODEC_TYPE_VIDEO;
335    else if(type == MKTAG('s','o','u','n'))
336        st->codec->codec_type = CODEC_TYPE_AUDIO;
337    else if(type == MKTAG('m','1','a',' '))
338        st->codec->codec_id = CODEC_ID_MP2;
339    else if(type == MKTAG('s','u','b','p')) {
340        st->codec->codec_type = CODEC_TYPE_SUBTITLE;
341    }
342    get_be32(pb); /* component  manufacture */
343    get_be32(pb); /* component flags */
344    get_be32(pb); /* component flags mask */
345
346    if(atom.size <= 24)
347        return 0; /* nothing left to read */
348
349    url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
350    return 0;
351}
352
353static int mp4_read_descr_len(ByteIOContext *pb)
354{
355    int len = 0;
356    int count = 4;
357    while (count--) {
358        int c = get_byte(pb);
359        len = (len << 7) | (c & 0x7f);
360        if (!(c & 0x80))
361            break;
362    }
363    return len;
364}
365
366static int mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
367{
368    int len;
369    *tag = get_byte(pb);
370    len = mp4_read_descr_len(pb);
371    dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
372    return len;
373}
374
375#define MP4ESDescrTag                   0x03
376#define MP4DecConfigDescrTag            0x04
377#define MP4DecSpecificDescrTag          0x05
378
379static const AVCodecTag mp4_audio_types[] = {
380    { CODEC_ID_MP3ON4, 29 }, /* old mp3on4 draft */
381    { CODEC_ID_MP3ON4, 32 }, /* layer 1 */
382    { CODEC_ID_MP3ON4, 33 }, /* layer 2 */
383    { CODEC_ID_MP3ON4, 34 }, /* layer 3 */
384    { CODEC_ID_NONE,    0 },
385};
386
387static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
388{
389    AVStream *st;
390    int tag, len;
391
392    if (c->fc->nb_streams < 1)
393        return 0;
394    st = c->fc->streams[c->fc->nb_streams-1];
395
396    get_be32(pb); /* version + flags */
397    len = mp4_read_descr(c, pb, &tag);
398    if (tag == MP4ESDescrTag) {
399        get_be16(pb); /* ID */
400        get_byte(pb); /* priority */
401    } else
402        get_be16(pb); /* ID */
403
404    len = mp4_read_descr(c, pb, &tag);
405    if (tag == MP4DecConfigDescrTag) {
406        int object_type_id = get_byte(pb);
407        get_byte(pb); /* stream type */
408        get_be24(pb); /* buffer size db */
409        get_be32(pb); /* max bitrate */
410        get_be32(pb); /* avg bitrate */
411
412        st->codec->codec_id= codec_get_id(ff_mp4_obj_type, object_type_id);
413        dprintf(c->fc, "esds object type id %d\n", object_type_id);
414        len = mp4_read_descr(c, pb, &tag);
415        if (tag == MP4DecSpecificDescrTag) {
416            dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
417            if((uint64_t)len > (1<<30))
418                return -1;
419            st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
420            if (!st->codec->extradata)
421                return AVERROR(ENOMEM);
422            get_buffer(pb, st->codec->extradata, len);
423            st->codec->extradata_size = len;
424            if (st->codec->codec_id == CODEC_ID_AAC) {
425                MPEG4AudioConfig cfg;
426                ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
427                                         st->codec->extradata_size);
428                if (cfg.chan_config > 7)
429                    return -1;
430                st->codec->channels = ff_mpeg4audio_channels[cfg.chan_config];
431                if (cfg.object_type == 29 && cfg.sampling_index < 3) // old mp3on4
432                    st->codec->sample_rate = ff_mpa_freq_tab[cfg.sampling_index];
433                else
434                    st->codec->sample_rate = cfg.sample_rate; // ext sample rate ?
435                dprintf(c->fc, "mp4a config channels %d obj %d ext obj %d "
436                        "sample rate %d ext sample rate %d\n", st->codec->channels,
437                        cfg.object_type, cfg.ext_object_type,
438                        cfg.sample_rate, cfg.ext_sample_rate);
439                if (!(st->codec->codec_id = codec_get_id(mp4_audio_types,
440                                                         cfg.object_type)))
441                    st->codec->codec_id = CODEC_ID_AAC;
442            }
443        }
444    }
445    return 0;
446}
447
448static int mov_read_pasp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
449{
450    const int num = get_be32(pb);
451    const int den = get_be32(pb);
452    AVStream *st;
453
454    if (c->fc->nb_streams < 1)
455        return 0;
456    st = c->fc->streams[c->fc->nb_streams-1];
457
458    if (den != 0) {
459        if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
460            (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num))
461            av_log(c->fc, AV_LOG_WARNING,
462                   "sample aspect ratio already set to %d:%d, overriding by 'pasp' atom\n",
463                   st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
464        st->sample_aspect_ratio.num = num;
465        st->sample_aspect_ratio.den = den;
466    }
467    return 0;
468}
469
470/* this atom contains actual media data */
471static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
472{
473    if(atom.size == 0) /* wrong one (MP4) */
474        return 0;
475    c->found_mdat=1;
476    return 0; /* now go for moov */
477}
478
479static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
480{
481    uint32_t type = get_le32(pb);
482
483    if (type != MKTAG('q','t',' ',' '))
484        c->isom = 1;
485    av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
486    get_be32(pb); /* minor version */
487    url_fskip(pb, atom.size - 8);
488    return 0;
489}
490
491/* this atom should contain all header atoms */
492static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
493{
494    if (mov_read_default(c, pb, atom) < 0)
495        return -1;
496    /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
497    /* so we don't parse the whole file if over a network */
498    c->found_moov=1;
499    return 0; /* now go for mdat */
500}
501
502static int mov_read_moof(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
503{
504    c->fragment.moof_offset = url_ftell(pb) - 8;
505    dprintf(c->fc, "moof offset %llx\n", c->fragment.moof_offset);
506    return mov_read_default(c, pb, atom);
507}
508
509static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
510{
511    AVStream *st;
512    MOVStreamContext *sc;
513    int version;
514    char language[4] = {0};
515    unsigned lang;
516
517    if (c->fc->nb_streams < 1)
518        return 0;
519    st = c->fc->streams[c->fc->nb_streams-1];
520    sc = st->priv_data;
521
522    version = get_byte(pb);
523    if (version > 1)
524        return -1; /* unsupported */
525
526    get_be24(pb); /* flags */
527    if (version == 1) {
528        get_be64(pb);
529        get_be64(pb);
530    } else {
531        get_be32(pb); /* creation time */
532        get_be32(pb); /* modification time */
533    }
534
535    sc->time_scale = get_be32(pb);
536    st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
537
538    lang = get_be16(pb); /* language */
539    if (ff_mov_lang_to_iso639(lang, language))
540        av_metadata_set(&st->metadata, "language", language);
541    get_be16(pb); /* quality */
542
543    return 0;
544}
545
546static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
547{
548    int version = get_byte(pb); /* version */
549    get_be24(pb); /* flags */
550
551    if (version == 1) {
552        get_be64(pb);
553        get_be64(pb);
554    } else {
555        get_be32(pb); /* creation time */
556        get_be32(pb); /* modification time */
557    }
558    c->time_scale = get_be32(pb); /* time scale */
559
560    dprintf(c->fc, "time scale = %i\n", c->time_scale);
561
562    c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
563    get_be32(pb); /* preferred scale */
564
565    get_be16(pb); /* preferred volume */
566
567    url_fskip(pb, 10); /* reserved */
568
569    url_fskip(pb, 36); /* display matrix */
570
571    get_be32(pb); /* preview time */
572    get_be32(pb); /* preview duration */
573    get_be32(pb); /* poster time */
574    get_be32(pb); /* selection time */
575    get_be32(pb); /* selection duration */
576    get_be32(pb); /* current time */
577    get_be32(pb); /* next track ID */
578
579    return 0;
580}
581
582static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
583{
584    AVStream *st;
585
586    if (c->fc->nb_streams < 1)
587        return 0;
588    st = c->fc->streams[c->fc->nb_streams-1];
589
590    if((uint64_t)atom.size > (1<<30))
591        return -1;
592
593    // currently SVQ3 decoder expect full STSD header - so let's fake it
594    // this should be fixed and just SMI header should be passed
595    av_free(st->codec->extradata);
596    st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
597    if (!st->codec->extradata)
598        return AVERROR(ENOMEM);
599    st->codec->extradata_size = 0x5a + atom.size;
600    memcpy(st->codec->extradata, "SVQ3", 4); // fake
601    get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
602    dprintf(c->fc, "Reading SMI %"PRId64"  %s\n", atom.size, st->codec->extradata + 0x5a);
603    return 0;
604}
605
606static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
607{
608    AVStream *st;
609    int little_endian;
610
611    if (c->fc->nb_streams < 1)
612        return 0;
613    st = c->fc->streams[c->fc->nb_streams-1];
614
615    little_endian = get_be16(pb);
616    dprintf(c->fc, "enda %d\n", little_endian);
617    if (little_endian == 1) {
618        switch (st->codec->codec_id) {
619        case CODEC_ID_PCM_S24BE:
620            st->codec->codec_id = CODEC_ID_PCM_S24LE;
621            break;
622        case CODEC_ID_PCM_S32BE:
623            st->codec->codec_id = CODEC_ID_PCM_S32LE;
624            break;
625        case CODEC_ID_PCM_F32BE:
626            st->codec->codec_id = CODEC_ID_PCM_F32LE;
627            break;
628        case CODEC_ID_PCM_F64BE:
629            st->codec->codec_id = CODEC_ID_PCM_F64LE;
630            break;
631        default:
632            break;
633        }
634    }
635    return 0;
636}
637
638/* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
639static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
640{
641    AVStream *st;
642    uint64_t size;
643    uint8_t *buf;
644
645    if (c->fc->nb_streams < 1) // will happen with jp2 files
646        return 0;
647    st= c->fc->streams[c->fc->nb_streams-1];
648    size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
649    if(size > INT_MAX || (uint64_t)atom.size > INT_MAX)
650        return -1;
651    buf= av_realloc(st->codec->extradata, size);
652    if(!buf)
653        return -1;
654    st->codec->extradata= buf;
655    buf+= st->codec->extradata_size;
656    st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
657    AV_WB32(       buf    , atom.size + 8);
658    AV_WL32(       buf + 4, atom.type);
659    get_buffer(pb, buf + 8, atom.size);
660    return 0;
661}
662
663static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
664{
665    AVStream *st;
666
667    if (c->fc->nb_streams < 1)
668        return 0;
669    st = c->fc->streams[c->fc->nb_streams-1];
670
671    if((uint64_t)atom.size > (1<<30))
672        return -1;
673
674    if (st->codec->codec_id == CODEC_ID_QDM2) {
675        // pass all frma atom to codec, needed at least for QDM2
676        av_free(st->codec->extradata);
677        st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
678        if (!st->codec->extradata)
679            return AVERROR(ENOMEM);
680        st->codec->extradata_size = atom.size;
681        get_buffer(pb, st->codec->extradata, atom.size);
682    } else if (atom.size > 8) { /* to read frma, esds atoms */
683        if (mov_read_default(c, pb, atom) < 0)
684            return -1;
685    } else
686        url_fskip(pb, atom.size);
687    return 0;
688}
689
690/**
691 * This function reads atom content and puts data in extradata without tag
692 * nor size unlike mov_read_extradata.
693 */
694static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
695{
696    AVStream *st;
697
698    if (c->fc->nb_streams < 1)
699        return 0;
700    st = c->fc->streams[c->fc->nb_streams-1];
701
702    if((uint64_t)atom.size > (1<<30))
703        return -1;
704
705    av_free(st->codec->extradata);
706    st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
707    if (!st->codec->extradata)
708        return AVERROR(ENOMEM);
709    st->codec->extradata_size = atom.size;
710    get_buffer(pb, st->codec->extradata, atom.size);
711    return 0;
712}
713
714static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
715{
716    AVStream *st;
717    MOVStreamContext *sc;
718    unsigned int i, entries;
719
720    if (c->fc->nb_streams < 1)
721        return 0;
722    st = c->fc->streams[c->fc->nb_streams-1];
723    sc = st->priv_data;
724
725    get_byte(pb); /* version */
726    get_be24(pb); /* flags */
727
728    entries = get_be32(pb);
729
730    if(entries >= UINT_MAX/sizeof(int64_t))
731        return -1;
732
733    sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
734    if (!sc->chunk_offsets)
735        return AVERROR(ENOMEM);
736    sc->chunk_count = entries;
737
738    if      (atom.type == MKTAG('s','t','c','o'))
739        for(i=0; i<entries; i++)
740            sc->chunk_offsets[i] = get_be32(pb);
741    else if (atom.type == MKTAG('c','o','6','4'))
742        for(i=0; i<entries; i++)
743            sc->chunk_offsets[i] = get_be64(pb);
744    else
745        return -1;
746
747    return 0;
748}
749
750/**
751 * Compute codec id for 'lpcm' tag.
752 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
753 */
754static enum CodecID mov_get_lpcm_codec_id(int bps, int flags)
755{
756    if (flags & 1) { // floating point
757        if (flags & 2) { // big endian
758            if      (bps == 32) return CODEC_ID_PCM_F32BE;
759            else if (bps == 64) return CODEC_ID_PCM_F64BE;
760        } else {
761            if      (bps == 32) return CODEC_ID_PCM_F32LE;
762            else if (bps == 64) return CODEC_ID_PCM_F64LE;
763        }
764    } else {
765        if (flags & 2) {
766            if      (bps == 8)
767                // signed integer
768                if (flags & 4)  return CODEC_ID_PCM_S8;
769                else            return CODEC_ID_PCM_U8;
770            else if (bps == 16) return CODEC_ID_PCM_S16BE;
771            else if (bps == 24) return CODEC_ID_PCM_S24BE;
772            else if (bps == 32) return CODEC_ID_PCM_S32BE;
773        } else {
774            if      (bps == 8)
775                if (flags & 4)  return CODEC_ID_PCM_S8;
776                else            return CODEC_ID_PCM_U8;
777            else if (bps == 16) return CODEC_ID_PCM_S16LE;
778            else if (bps == 24) return CODEC_ID_PCM_S24LE;
779            else if (bps == 32) return CODEC_ID_PCM_S32LE;
780        }
781    }
782    return CODEC_ID_NONE;
783}
784
785static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
786{
787    AVStream *st;
788    MOVStreamContext *sc;
789    int j, entries, pseudo_stream_id;
790
791    if (c->fc->nb_streams < 1)
792        return 0;
793    st = c->fc->streams[c->fc->nb_streams-1];
794    sc = st->priv_data;
795
796    get_byte(pb); /* version */
797    get_be24(pb); /* flags */
798
799    entries = get_be32(pb);
800
801    for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
802        //Parsing Sample description table
803        enum CodecID id;
804        int dref_id;
805        MOVAtom a = { 0, 0, 0 };
806        int64_t start_pos = url_ftell(pb);
807        int size = get_be32(pb); /* size */
808        uint32_t format = get_le32(pb); /* data format */
809
810        get_be32(pb); /* reserved */
811        get_be16(pb); /* reserved */
812        dref_id = get_be16(pb);
813
814        if (st->codec->codec_tag &&
815            st->codec->codec_tag != format &&
816            (c->fc->video_codec_id ? codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
817                                   : st->codec->codec_tag != MKTAG('j','p','e','g'))
818           ){
819            /* Multiple fourcc, we skip JPEG. This is not correct, we should
820             * export it as a separate AVStream but this needs a few changes
821             * in the MOV demuxer, patch welcome. */
822            av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
823            url_fskip(pb, size - (url_ftell(pb) - start_pos));
824            continue;
825        }
826        sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
827        sc->dref_id= dref_id;
828
829        st->codec->codec_tag = format;
830        id = codec_get_id(codec_movaudio_tags, format);
831        if (id<=0 && (format&0xFFFF) == 'm'+('s'<<8))
832            id = codec_get_id(codec_wav_tags, bswap_32(format)&0xFFFF);
833
834        if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
835            st->codec->codec_type = CODEC_TYPE_AUDIO;
836        } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
837                   format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
838            id = codec_get_id(codec_movvideo_tags, format);
839            if (id <= 0)
840                id = codec_get_id(codec_bmp_tags, format);
841            if (id > 0)
842                st->codec->codec_type = CODEC_TYPE_VIDEO;
843            else if(st->codec->codec_type == CODEC_TYPE_DATA){
844                id = codec_get_id(ff_codec_movsubtitle_tags, format);
845                if(id > 0)
846                    st->codec->codec_type = CODEC_TYPE_SUBTITLE;
847            }
848        }
849
850        dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
851                (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
852                (format >> 24) & 0xff, st->codec->codec_type);
853
854        if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
855            uint8_t codec_name[32];
856            unsigned int color_depth;
857            int color_greyscale;
858
859            st->codec->codec_id = id;
860            get_be16(pb); /* version */
861            get_be16(pb); /* revision level */
862            get_be32(pb); /* vendor */
863            get_be32(pb); /* temporal quality */
864            get_be32(pb); /* spatial quality */
865
866            st->codec->width = get_be16(pb); /* width */
867            st->codec->height = get_be16(pb); /* height */
868
869            get_be32(pb); /* horiz resolution */
870            get_be32(pb); /* vert resolution */
871            get_be32(pb); /* data size, always 0 */
872            get_be16(pb); /* frames per samples */
873
874            get_buffer(pb, codec_name, 32); /* codec name, pascal string */
875            if (codec_name[0] <= 31) {
876                memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
877                st->codec->codec_name[codec_name[0]] = 0;
878            }
879
880            st->codec->bits_per_coded_sample = get_be16(pb); /* depth */
881            st->codec->color_table_id = get_be16(pb); /* colortable id */
882            dprintf(c->fc, "depth %d, ctab id %d\n",
883                   st->codec->bits_per_coded_sample, st->codec->color_table_id);
884            /* figure out the palette situation */
885            color_depth = st->codec->bits_per_coded_sample & 0x1F;
886            color_greyscale = st->codec->bits_per_coded_sample & 0x20;
887
888            /* if the depth is 2, 4, or 8 bpp, file is palettized */
889            if ((color_depth == 2) || (color_depth == 4) ||
890                (color_depth == 8)) {
891                /* for palette traversal */
892                unsigned int color_start, color_count, color_end;
893                unsigned char r, g, b;
894
895                if (color_greyscale) {
896                    int color_index, color_dec;
897                    /* compute the greyscale palette */
898                    st->codec->bits_per_coded_sample = color_depth;
899                    color_count = 1 << color_depth;
900                    color_index = 255;
901                    color_dec = 256 / (color_count - 1);
902                    for (j = 0; j < color_count; j++) {
903                        r = g = b = color_index;
904                        c->palette_control.palette[j] =
905                            (r << 16) | (g << 8) | (b);
906                        color_index -= color_dec;
907                        if (color_index < 0)
908                            color_index = 0;
909                    }
910                } else if (st->codec->color_table_id) {
911                    const uint8_t *color_table;
912                    /* if flag bit 3 is set, use the default palette */
913                    color_count = 1 << color_depth;
914                    if (color_depth == 2)
915                        color_table = ff_qt_default_palette_4;
916                    else if (color_depth == 4)
917                        color_table = ff_qt_default_palette_16;
918                    else
919                        color_table = ff_qt_default_palette_256;
920
921                    for (j = 0; j < color_count; j++) {
922                        r = color_table[j * 4 + 0];
923                        g = color_table[j * 4 + 1];
924                        b = color_table[j * 4 + 2];
925                        c->palette_control.palette[j] =
926                            (r << 16) | (g << 8) | (b);
927                    }
928                } else {
929                    /* load the palette from the file */
930                    color_start = get_be32(pb);
931                    color_count = get_be16(pb);
932                    color_end = get_be16(pb);
933                    if ((color_start <= 255) &&
934                        (color_end <= 255)) {
935                        for (j = color_start; j <= color_end; j++) {
936                            /* each R, G, or B component is 16 bits;
937                             * only use the top 8 bits; skip alpha bytes
938                             * up front */
939                            get_byte(pb);
940                            get_byte(pb);
941                            r = get_byte(pb);
942                            get_byte(pb);
943                            g = get_byte(pb);
944                            get_byte(pb);
945                            b = get_byte(pb);
946                            get_byte(pb);
947                            c->palette_control.palette[j] =
948                                (r << 16) | (g << 8) | (b);
949                        }
950                    }
951                }
952                st->codec->palctrl = &c->palette_control;
953                st->codec->palctrl->palette_changed = 1;
954            } else
955                st->codec->palctrl = NULL;
956        } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
957            int bits_per_sample, flags;
958            uint16_t version = get_be16(pb);
959
960            st->codec->codec_id = id;
961            get_be16(pb); /* revision level */
962            get_be32(pb); /* vendor */
963
964            st->codec->channels = get_be16(pb);             /* channel count */
965            dprintf(c->fc, "audio channels %d\n", st->codec->channels);
966            st->codec->bits_per_coded_sample = get_be16(pb);      /* sample size */
967
968            sc->audio_cid = get_be16(pb);
969            get_be16(pb); /* packet size = 0 */
970
971            st->codec->sample_rate = ((get_be32(pb) >> 16));
972
973            //Read QT version 1 fields. In version 0 these do not exist.
974            dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
975            if(!c->isom) {
976                if(version==1) {
977                    sc->samples_per_frame = get_be32(pb);
978                    get_be32(pb); /* bytes per packet */
979                    sc->bytes_per_frame = get_be32(pb);
980                    get_be32(pb); /* bytes per sample */
981                } else if(version==2) {
982                    get_be32(pb); /* sizeof struct only */
983                    st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
984                    st->codec->channels = get_be32(pb);
985                    get_be32(pb); /* always 0x7F000000 */
986                    st->codec->bits_per_coded_sample = get_be32(pb); /* bits per channel if sound is uncompressed */
987                    flags = get_be32(pb); /* lcpm format specific flag */
988                    sc->bytes_per_frame = get_be32(pb); /* bytes per audio packet if constant */
989                    sc->samples_per_frame = get_be32(pb); /* lpcm frames per audio packet if constant */
990                    if (format == MKTAG('l','p','c','m'))
991                        st->codec->codec_id = mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
992                }
993            }
994
995            switch (st->codec->codec_id) {
996            case CODEC_ID_PCM_S8:
997            case CODEC_ID_PCM_U8:
998                if (st->codec->bits_per_coded_sample == 16)
999                    st->codec->codec_id = CODEC_ID_PCM_S16BE;
1000                break;
1001            case CODEC_ID_PCM_S16LE:
1002            case CODEC_ID_PCM_S16BE:
1003                if (st->codec->bits_per_coded_sample == 8)
1004                    st->codec->codec_id = CODEC_ID_PCM_S8;
1005                else if (st->codec->bits_per_coded_sample == 24)
1006                    st->codec->codec_id =
1007                        st->codec->codec_id == CODEC_ID_PCM_S16BE ?
1008                        CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
1009                break;
1010            /* set values for old format before stsd version 1 appeared */
1011            case CODEC_ID_MACE3:
1012                sc->samples_per_frame = 6;
1013                sc->bytes_per_frame = 2*st->codec->channels;
1014                break;
1015            case CODEC_ID_MACE6:
1016                sc->samples_per_frame = 6;
1017                sc->bytes_per_frame = 1*st->codec->channels;
1018                break;
1019            case CODEC_ID_ADPCM_IMA_QT:
1020                sc->samples_per_frame = 64;
1021                sc->bytes_per_frame = 34*st->codec->channels;
1022                break;
1023            case CODEC_ID_GSM:
1024                sc->samples_per_frame = 160;
1025                sc->bytes_per_frame = 33;
1026                break;
1027            default:
1028                break;
1029            }
1030
1031            bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
1032            if (bits_per_sample) {
1033                st->codec->bits_per_coded_sample = bits_per_sample;
1034                sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
1035            }
1036        } else if(st->codec->codec_type==CODEC_TYPE_SUBTITLE){
1037            // ttxt stsd contains display flags, justification, background
1038            // color, fonts, and default styles, so fake an atom to read it
1039            MOVAtom fake_atom = { .size = size - (url_ftell(pb) - start_pos) };
1040            mov_read_glbl(c, pb, fake_atom);
1041            st->codec->codec_id= id;
1042            st->codec->width = sc->width;
1043            st->codec->height = sc->height;
1044        } else {
1045            /* other codec type, just skip (rtp, mp4s, tmcd ...) */
1046            url_fskip(pb, size - (url_ftell(pb) - start_pos));
1047        }
1048        /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
1049        a.size = size - (url_ftell(pb) - start_pos);
1050        if (a.size > 8) {
1051            if (mov_read_default(c, pb, a) < 0)
1052                return -1;
1053        } else if (a.size > 0)
1054            url_fskip(pb, a.size);
1055    }
1056
1057    if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
1058        st->codec->sample_rate= sc->time_scale;
1059
1060    /* special codec parameters handling */
1061    switch (st->codec->codec_id) {
1062#if CONFIG_DV_DEMUXER
1063    case CODEC_ID_DVAUDIO:
1064        c->dv_fctx = avformat_alloc_context();
1065        c->dv_demux = dv_init_demux(c->dv_fctx);
1066        if (!c->dv_demux) {
1067            av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
1068            return -1;
1069        }
1070        sc->dv_audio_container = 1;
1071        st->codec->codec_id = CODEC_ID_PCM_S16LE;
1072        break;
1073#endif
1074    /* no ifdef since parameters are always those */
1075    case CODEC_ID_QCELP:
1076        st->codec->frame_size= 160;
1077        st->codec->channels= 1; /* really needed */
1078        break;
1079    case CODEC_ID_AMR_NB:
1080    case CODEC_ID_AMR_WB:
1081        st->codec->frame_size= sc->samples_per_frame;
1082        st->codec->channels= 1; /* really needed */
1083        /* force sample rate for amr, stsd in 3gp does not store sample rate */
1084        if (st->codec->codec_id == CODEC_ID_AMR_NB)
1085            st->codec->sample_rate = 8000;
1086        else if (st->codec->codec_id == CODEC_ID_AMR_WB)
1087            st->codec->sample_rate = 16000;
1088        break;
1089    case CODEC_ID_MP2:
1090    case CODEC_ID_MP3:
1091        st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
1092        st->need_parsing = AVSTREAM_PARSE_FULL;
1093        break;
1094    case CODEC_ID_GSM:
1095    case CODEC_ID_ADPCM_MS:
1096    case CODEC_ID_ADPCM_IMA_WAV:
1097        st->codec->block_align = sc->bytes_per_frame;
1098        break;
1099    case CODEC_ID_ALAC:
1100        if (st->codec->extradata_size == 36) {
1101            st->codec->frame_size = AV_RB32(st->codec->extradata+12);
1102            st->codec->channels   = AV_RB8 (st->codec->extradata+21);
1103        }
1104        break;
1105    default:
1106        break;
1107    }
1108
1109    return 0;
1110}
1111
1112static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1113{
1114    AVStream *st;
1115    MOVStreamContext *sc;
1116    unsigned int i, entries;
1117
1118    if (c->fc->nb_streams < 1)
1119        return 0;
1120    st = c->fc->streams[c->fc->nb_streams-1];
1121    sc = st->priv_data;
1122
1123    get_byte(pb); /* version */
1124    get_be24(pb); /* flags */
1125
1126    entries = get_be32(pb);
1127
1128    dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
1129
1130    if(entries >= UINT_MAX / sizeof(*sc->stsc_data))
1131        return -1;
1132    sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
1133    if (!sc->stsc_data)
1134        return AVERROR(ENOMEM);
1135    sc->stsc_count = entries;
1136
1137    for(i=0; i<entries; i++) {
1138        sc->stsc_data[i].first = get_be32(pb);
1139        sc->stsc_data[i].count = get_be32(pb);
1140        sc->stsc_data[i].id = get_be32(pb);
1141    }
1142    return 0;
1143}
1144
1145static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1146{
1147    AVStream *st;
1148    MOVStreamContext *sc;
1149    unsigned int i, entries;
1150
1151    if (c->fc->nb_streams < 1)
1152        return 0;
1153    st = c->fc->streams[c->fc->nb_streams-1];
1154    sc = st->priv_data;
1155
1156    get_byte(pb); /* version */
1157    get_be24(pb); /* flags */
1158
1159    entries = get_be32(pb);
1160
1161    dprintf(c->fc, "keyframe_count = %d\n", entries);
1162
1163    if(entries >= UINT_MAX / sizeof(int))
1164        return -1;
1165    sc->keyframes = av_malloc(entries * sizeof(int));
1166    if (!sc->keyframes)
1167        return AVERROR(ENOMEM);
1168    sc->keyframe_count = entries;
1169
1170    for(i=0; i<entries; i++) {
1171        sc->keyframes[i] = get_be32(pb);
1172        //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
1173    }
1174    return 0;
1175}
1176
1177static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1178{
1179    AVStream *st;
1180    MOVStreamContext *sc;
1181    unsigned int i, entries, sample_size;
1182
1183    if (c->fc->nb_streams < 1)
1184        return 0;
1185    st = c->fc->streams[c->fc->nb_streams-1];
1186    sc = st->priv_data;
1187
1188    get_byte(pb); /* version */
1189    get_be24(pb); /* flags */
1190
1191    sample_size = get_be32(pb);
1192    if (!sc->sample_size) /* do not overwrite value computed in stsd */
1193        sc->sample_size = sample_size;
1194    entries = get_be32(pb);
1195
1196    dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
1197
1198    sc->sample_count = entries;
1199    if (sample_size)
1200        return 0;
1201
1202    if(entries >= UINT_MAX / sizeof(int))
1203        return -1;
1204    sc->sample_sizes = av_malloc(entries * sizeof(int));
1205    if (!sc->sample_sizes)
1206        return AVERROR(ENOMEM);
1207
1208    for(i=0; i<entries; i++)
1209        sc->sample_sizes[i] = get_be32(pb);
1210    return 0;
1211}
1212
1213static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1214{
1215    AVStream *st;
1216    MOVStreamContext *sc;
1217    unsigned int i, entries;
1218    int64_t duration=0;
1219    int64_t total_sample_count=0;
1220
1221    if (c->fc->nb_streams < 1)
1222        return 0;
1223    st = c->fc->streams[c->fc->nb_streams-1];
1224    sc = st->priv_data;
1225
1226    get_byte(pb); /* version */
1227    get_be24(pb); /* flags */
1228    entries = get_be32(pb);
1229
1230    dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
1231
1232    if(entries >= UINT_MAX / sizeof(*sc->stts_data))
1233        return -1;
1234    sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
1235    if (!sc->stts_data)
1236        return AVERROR(ENOMEM);
1237    sc->stts_count = entries;
1238
1239    for(i=0; i<entries; i++) {
1240        int sample_duration;
1241        int sample_count;
1242
1243        sample_count=get_be32(pb);
1244        sample_duration = get_be32(pb);
1245        sc->stts_data[i].count= sample_count;
1246        sc->stts_data[i].duration= sample_duration;
1247
1248        sc->time_rate= av_gcd(sc->time_rate, sample_duration);
1249
1250        dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
1251
1252        duration+=(int64_t)sample_duration*sample_count;
1253        total_sample_count+=sample_count;
1254    }
1255
1256    st->nb_frames= total_sample_count;
1257    if(duration)
1258        st->duration= duration;
1259    return 0;
1260}
1261
1262static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1263{
1264    AVStream *st;
1265    MOVStreamContext *sc;
1266    unsigned int i, entries;
1267
1268    if (c->fc->nb_streams < 1)
1269        return 0;
1270    st = c->fc->streams[c->fc->nb_streams-1];
1271    sc = st->priv_data;
1272
1273    get_byte(pb); /* version */
1274    get_be24(pb); /* flags */
1275    entries = get_be32(pb);
1276
1277    dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
1278
1279    if(entries >= UINT_MAX / sizeof(*sc->ctts_data))
1280        return -1;
1281    sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
1282    if (!sc->ctts_data)
1283        return AVERROR(ENOMEM);
1284    sc->ctts_count = entries;
1285
1286    for(i=0; i<entries; i++) {
1287        int count    =get_be32(pb);
1288        int duration =get_be32(pb);
1289
1290        if (duration < 0) {
1291            sc->wrong_dts = 1;
1292            st->codec->has_b_frames = 1;
1293        }
1294        sc->ctts_data[i].count   = count;
1295        sc->ctts_data[i].duration= duration;
1296
1297        sc->time_rate= av_gcd(sc->time_rate, FFABS(duration));
1298    }
1299    return 0;
1300}
1301
1302static void mov_build_index(MOVContext *mov, AVStream *st)
1303{
1304    MOVStreamContext *sc = st->priv_data;
1305    int64_t current_offset;
1306    int64_t current_dts = 0;
1307    unsigned int stts_index = 0;
1308    unsigned int stsc_index = 0;
1309    unsigned int stss_index = 0;
1310    unsigned int i, j;
1311
1312    /* adjust first dts according to edit list */
1313    if (sc->time_offset) {
1314        assert(sc->time_offset % sc->time_rate == 0);
1315        current_dts = - (sc->time_offset / sc->time_rate);
1316    }
1317
1318    /* only use old uncompressed audio chunk demuxing when stts specifies it */
1319    if (!(st->codec->codec_type == CODEC_TYPE_AUDIO &&
1320          sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
1321        unsigned int current_sample = 0;
1322        unsigned int stts_sample = 0;
1323        unsigned int keyframe, sample_size;
1324        unsigned int distance = 0;
1325        int key_off = sc->keyframes && sc->keyframes[0] == 1;
1326
1327        st->nb_frames = sc->sample_count;
1328        for (i = 0; i < sc->chunk_count; i++) {
1329            current_offset = sc->chunk_offsets[i];
1330            if (stsc_index + 1 < sc->stsc_count &&
1331                i + 1 == sc->stsc_data[stsc_index + 1].first)
1332                stsc_index++;
1333            for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
1334                if (current_sample >= sc->sample_count) {
1335                    av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
1336                    goto out;
1337                }
1338                keyframe = !sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index];
1339                if (keyframe) {
1340                    distance = 0;
1341                    if (stss_index + 1 < sc->keyframe_count)
1342                        stss_index++;
1343                }
1344                sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
1345                if(sc->pseudo_stream_id == -1 ||
1346                   sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
1347                    av_add_index_entry(st, current_offset, current_dts, sample_size, distance,
1348                                    keyframe ? AVINDEX_KEYFRAME : 0);
1349                    dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
1350                            "size %d, distance %d, keyframe %d\n", st->index, current_sample,
1351                            current_offset, current_dts, sample_size, distance, keyframe);
1352                }
1353                current_offset += sample_size;
1354                assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
1355                current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
1356                distance++;
1357                stts_sample++;
1358                current_sample++;
1359                if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
1360                    stts_sample = 0;
1361                    stts_index++;
1362                }
1363            }
1364        }
1365    } else { /* read whole chunk */
1366        unsigned int chunk_samples, chunk_size, chunk_duration;
1367        unsigned int frames = 1;
1368        for (i = 0; i < sc->chunk_count; i++) {
1369            current_offset = sc->chunk_offsets[i];
1370            if (stsc_index + 1 < sc->stsc_count &&
1371                i + 1 == sc->stsc_data[stsc_index + 1].first)
1372                stsc_index++;
1373            chunk_samples = sc->stsc_data[stsc_index].count;
1374            /* get chunk size, beware of alaw/ulaw/mace */
1375            if (sc->samples_per_frame > 0 &&
1376                (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0)) {
1377                if (sc->samples_per_frame < 160)
1378                    chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
1379                else {
1380                    chunk_size = sc->bytes_per_frame;
1381                    frames = chunk_samples / sc->samples_per_frame;
1382                    chunk_samples = sc->samples_per_frame;
1383                }
1384            } else
1385                chunk_size = chunk_samples * sc->sample_size;
1386            for (j = 0; j < frames; j++) {
1387                av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
1388                /* get chunk duration */
1389                chunk_duration = 0;
1390                while (chunk_samples > 0) {
1391                    if (chunk_samples < sc->stts_data[stts_index].count) {
1392                        chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1393                        sc->stts_data[stts_index].count -= chunk_samples;
1394                        break;
1395                    } else {
1396                        chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1397                        chunk_samples -= sc->stts_data[stts_index].count;
1398                        if (stts_index + 1 < sc->stts_count)
1399                            stts_index++;
1400                    }
1401                }
1402                current_offset += sc->bytes_per_frame;
1403                dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
1404                        "size %d, duration %d\n", st->index, i, current_offset, current_dts,
1405                        chunk_size, chunk_duration);
1406                assert(chunk_duration % sc->time_rate == 0);
1407                current_dts += chunk_duration / sc->time_rate;
1408            }
1409        }
1410    }
1411 out:
1412    /* adjust sample count to avindex entries */
1413    sc->sample_count = st->nb_index_entries;
1414}
1415
1416static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1417{
1418    AVStream *st;
1419    MOVStreamContext *sc;
1420    int ret;
1421
1422    st = av_new_stream(c->fc, c->fc->nb_streams);
1423    if (!st) return AVERROR(ENOMEM);
1424    sc = av_mallocz(sizeof(MOVStreamContext));
1425    if (!sc) return AVERROR(ENOMEM);
1426
1427    st->priv_data = sc;
1428    st->codec->codec_type = CODEC_TYPE_DATA;
1429    sc->ffindex = st->index;
1430
1431    if ((ret = mov_read_default(c, pb, atom)) < 0)
1432        return ret;
1433
1434    /* sanity checks */
1435    if(sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
1436                           (!sc->sample_size && !sc->sample_count))){
1437        av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
1438               st->index);
1439        sc->sample_count = 0; //ignore track
1440        return 0;
1441    }
1442    if(!sc->time_rate)
1443        sc->time_rate=1;
1444    if(!sc->time_scale)
1445        sc->time_scale= c->time_scale;
1446    av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
1447
1448    if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
1449        !st->codec->frame_size && sc->stts_count == 1) {
1450        st->codec->frame_size = av_rescale(sc->stts_data[0].duration,
1451                                           st->codec->sample_rate, sc->time_scale);
1452        dprintf(c->fc, "frame size %d\n", st->codec->frame_size);
1453    }
1454
1455    if(st->duration != AV_NOPTS_VALUE){
1456        assert(st->duration % sc->time_rate == 0);
1457        st->duration /= sc->time_rate;
1458    }
1459
1460    mov_build_index(c, st);
1461
1462    if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
1463        if (url_fopen(&sc->pb, sc->drefs[sc->dref_id-1].path, URL_RDONLY) < 0)
1464            av_log(c->fc, AV_LOG_ERROR, "stream %d, error opening file %s: %s\n",
1465                   st->index, sc->drefs[sc->dref_id-1].path, strerror(errno));
1466    } else
1467        sc->pb = c->fc->pb;
1468
1469    switch (st->codec->codec_id) {
1470#if CONFIG_H261_DECODER
1471    case CODEC_ID_H261:
1472#endif
1473#if CONFIG_H263_DECODER
1474    case CODEC_ID_H263:
1475#endif
1476#if CONFIG_MPEG4_DECODER
1477    case CODEC_ID_MPEG4:
1478#endif
1479        st->codec->width= 0; /* let decoder init width/height */
1480        st->codec->height= 0;
1481        break;
1482    }
1483
1484    /* Do not need those anymore. */
1485    av_freep(&sc->chunk_offsets);
1486    av_freep(&sc->stsc_data);
1487    av_freep(&sc->sample_sizes);
1488    av_freep(&sc->keyframes);
1489    av_freep(&sc->stts_data);
1490
1491    return 0;
1492}
1493
1494static int mov_read_ilst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1495{
1496    int ret;
1497    c->itunes_metadata = 1;
1498    ret = mov_read_default(c, pb, atom);
1499    c->itunes_metadata = 0;
1500    return ret;
1501}
1502
1503static int mov_read_meta(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1504{
1505    url_fskip(pb, 4); // version + flags
1506    atom.size -= 4;
1507    return mov_read_default(c, pb, atom);
1508}
1509
1510static int mov_read_trkn(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1511{
1512    char track[16];
1513    get_be32(pb); // type
1514    get_be32(pb); // unknown
1515    snprintf(track, sizeof(track), "%d", get_be32(pb));
1516    av_metadata_set(&c->fc->metadata, "track", track);
1517    dprintf(c->fc, "%.4s %s\n", (char*)&atom.type, track);
1518    return 0;
1519}
1520
1521static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1522{
1523    char str[1024], key2[16], language[4] = {0};
1524    const char *key = NULL;
1525    uint16_t str_size;
1526
1527    if (c->itunes_metadata) {
1528        int data_size = get_be32(pb);
1529        int tag = get_le32(pb);
1530        if (tag == MKTAG('d','a','t','a')) {
1531            get_be32(pb); // type
1532            get_be32(pb); // unknown
1533            str_size = data_size - 16;
1534            atom.size -= 16;
1535        } else return 0;
1536    } else {
1537        str_size = get_be16(pb); // string length
1538        ff_mov_lang_to_iso639(get_be16(pb), language);
1539        atom.size -= 4;
1540    }
1541    switch (atom.type) {
1542    case MKTAG(0xa9,'n','a','m'): key = "title";     break;
1543    case MKTAG(0xa9,'a','u','t'):
1544    case MKTAG(0xa9,'A','R','T'):
1545    case MKTAG(0xa9,'w','r','t'): key = "author";    break;
1546    case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
1547    case MKTAG(0xa9,'c','m','t'):
1548    case MKTAG(0xa9,'i','n','f'): key = "comment";   break;
1549    case MKTAG(0xa9,'a','l','b'): key = "album";     break;
1550    case MKTAG(0xa9,'d','a','y'): key = "year";      break;
1551    case MKTAG(0xa9,'g','e','n'): key = "genre";     break;
1552    case MKTAG(0xa9,'t','o','o'):
1553    case MKTAG(0xa9,'e','n','c'): key = "muxer";     break;
1554    }
1555    if (!key)
1556        return 0;
1557    if (atom.size < 0)
1558        return -1;
1559
1560    str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
1561    get_buffer(pb, str, str_size);
1562    str[str_size] = 0;
1563    av_metadata_set(&c->fc->metadata, key, str);
1564    if (*language && strcmp(language, "und")) {
1565        snprintf(key2, sizeof(key2), "%s-%s", key, language);
1566        av_metadata_set(&c->fc->metadata, key2, str);
1567    }
1568    dprintf(c->fc, "%.4s %s %d %lld\n", (char*)&atom.type, str, str_size, atom.size);
1569    return 0;
1570}
1571
1572static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1573{
1574    int i;
1575    int width;
1576    int height;
1577    int64_t disp_transform[2];
1578    int display_matrix[3][2];
1579    AVStream *st;
1580    MOVStreamContext *sc;
1581    int version;
1582
1583    if (c->fc->nb_streams < 1)
1584        return 0;
1585    st = c->fc->streams[c->fc->nb_streams-1];
1586    sc = st->priv_data;
1587
1588    version = get_byte(pb);
1589    get_be24(pb); /* flags */
1590    /*
1591    MOV_TRACK_ENABLED 0x0001
1592    MOV_TRACK_IN_MOVIE 0x0002
1593    MOV_TRACK_IN_PREVIEW 0x0004
1594    MOV_TRACK_IN_POSTER 0x0008
1595    */
1596
1597    if (version == 1) {
1598        get_be64(pb);
1599        get_be64(pb);
1600    } else {
1601        get_be32(pb); /* creation time */
1602        get_be32(pb); /* modification time */
1603    }
1604    st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
1605    get_be32(pb); /* reserved */
1606
1607    /* highlevel (considering edits) duration in movie timebase */
1608    (version == 1) ? get_be64(pb) : get_be32(pb);
1609    get_be32(pb); /* reserved */
1610    get_be32(pb); /* reserved */
1611
1612    get_be16(pb); /* layer */
1613    get_be16(pb); /* alternate group */
1614    get_be16(pb); /* volume */
1615    get_be16(pb); /* reserved */
1616
1617    //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
1618    // they're kept in fixed point format through all calculations
1619    // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
1620    for (i = 0; i < 3; i++) {
1621        display_matrix[i][0] = get_be32(pb);   // 16.16 fixed point
1622        display_matrix[i][1] = get_be32(pb);   // 16.16 fixed point
1623        get_be32(pb);           // 2.30 fixed point (not used)
1624    }
1625
1626    width = get_be32(pb);       // 16.16 fixed point track width
1627    height = get_be32(pb);      // 16.16 fixed point track height
1628    sc->width = width >> 16;
1629    sc->height = height >> 16;
1630
1631    //transform the display width/height according to the matrix
1632    // skip this if the display matrix is the default identity matrix
1633    // to keep the same scale, use [width height 1<<16]
1634    if (width && height &&
1635        (display_matrix[0][0] != 65536 || display_matrix[0][1]           ||
1636        display_matrix[1][0]           || display_matrix[1][1] != 65536  ||
1637        display_matrix[2][0]           || display_matrix[2][1])) {
1638        for (i = 0; i < 2; i++)
1639            disp_transform[i] =
1640                (int64_t)  width  * display_matrix[0][i] +
1641                (int64_t)  height * display_matrix[1][i] +
1642                ((int64_t) display_matrix[2][i] << 16);
1643
1644        //sample aspect ratio is new width/height divided by old width/height
1645        st->sample_aspect_ratio = av_d2q(
1646            ((double) disp_transform[0] * height) /
1647            ((double) disp_transform[1] * width), INT_MAX);
1648    }
1649    return 0;
1650}
1651
1652static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1653{
1654    MOVFragment *frag = &c->fragment;
1655    MOVTrackExt *trex = NULL;
1656    int flags, track_id, i;
1657
1658    get_byte(pb); /* version */
1659    flags = get_be24(pb);
1660
1661    track_id = get_be32(pb);
1662    if (!track_id || track_id > c->fc->nb_streams)
1663        return -1;
1664    frag->track_id = track_id;
1665    for (i = 0; i < c->trex_count; i++)
1666        if (c->trex_data[i].track_id == frag->track_id) {
1667            trex = &c->trex_data[i];
1668            break;
1669        }
1670    if (!trex) {
1671        av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
1672        return -1;
1673    }
1674
1675    if (flags & 0x01) frag->base_data_offset = get_be64(pb);
1676    else              frag->base_data_offset = frag->moof_offset;
1677    if (flags & 0x02) frag->stsd_id          = get_be32(pb);
1678    else              frag->stsd_id          = trex->stsd_id;
1679
1680    frag->duration = flags & 0x08 ? get_be32(pb) : trex->duration;
1681    frag->size     = flags & 0x10 ? get_be32(pb) : trex->size;
1682    frag->flags    = flags & 0x20 ? get_be32(pb) : trex->flags;
1683    dprintf(c->fc, "frag flags 0x%x\n", frag->flags);
1684    return 0;
1685}
1686
1687static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1688{
1689    MOVTrackExt *trex;
1690
1691    if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
1692        return -1;
1693    trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
1694    if (!trex)
1695        return AVERROR(ENOMEM);
1696    c->trex_data = trex;
1697    trex = &c->trex_data[c->trex_count++];
1698    get_byte(pb); /* version */
1699    get_be24(pb); /* flags */
1700    trex->track_id = get_be32(pb);
1701    trex->stsd_id  = get_be32(pb);
1702    trex->duration = get_be32(pb);
1703    trex->size     = get_be32(pb);
1704    trex->flags    = get_be32(pb);
1705    return 0;
1706}
1707
1708static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1709{
1710    MOVFragment *frag = &c->fragment;
1711    AVStream *st;
1712    MOVStreamContext *sc;
1713    uint64_t offset;
1714    int64_t dts;
1715    int data_offset = 0;
1716    unsigned entries, first_sample_flags = frag->flags;
1717    int flags, distance, i;
1718
1719    if (!frag->track_id || frag->track_id > c->fc->nb_streams)
1720        return -1;
1721    st = c->fc->streams[frag->track_id-1];
1722    sc = st->priv_data;
1723    if (sc->pseudo_stream_id+1 != frag->stsd_id)
1724        return 0;
1725    get_byte(pb); /* version */
1726    flags = get_be24(pb);
1727    entries = get_be32(pb);
1728    dprintf(c->fc, "flags 0x%x entries %d\n", flags, entries);
1729    if (flags & 0x001) data_offset        = get_be32(pb);
1730    if (flags & 0x004) first_sample_flags = get_be32(pb);
1731    if (flags & 0x800) {
1732        MOVStts *ctts_data;
1733        if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
1734            return -1;
1735        ctts_data = av_realloc(sc->ctts_data,
1736                               (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
1737        if (!ctts_data)
1738            return AVERROR(ENOMEM);
1739        sc->ctts_data = ctts_data;
1740    }
1741    dts = st->duration;
1742    offset = frag->base_data_offset + data_offset;
1743    distance = 0;
1744    dprintf(c->fc, "first sample flags 0x%x\n", first_sample_flags);
1745    for (i = 0; i < entries; i++) {
1746        unsigned sample_size = frag->size;
1747        int sample_flags = i ? frag->flags : first_sample_flags;
1748        unsigned sample_duration = frag->duration;
1749        int keyframe;
1750
1751        if (flags & 0x100) sample_duration = get_be32(pb);
1752        if (flags & 0x200) sample_size     = get_be32(pb);
1753        if (flags & 0x400) sample_flags    = get_be32(pb);
1754        if (flags & 0x800) {
1755            sc->ctts_data[sc->ctts_count].count = 1;
1756            sc->ctts_data[sc->ctts_count].duration = get_be32(pb);
1757            sc->ctts_count++;
1758        }
1759        if ((keyframe = st->codec->codec_type == CODEC_TYPE_AUDIO ||
1760             (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
1761            distance = 0;
1762        av_add_index_entry(st, offset, dts, sample_size, distance,
1763                           keyframe ? AVINDEX_KEYFRAME : 0);
1764        dprintf(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
1765                "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
1766                offset, dts, sample_size, distance, keyframe);
1767        distance++;
1768        assert(sample_duration % sc->time_rate == 0);
1769        dts += sample_duration / sc->time_rate;
1770        offset += sample_size;
1771    }
1772    frag->moof_offset = offset;
1773    sc->sample_count = st->nb_index_entries;
1774    st->duration = dts;
1775    return 0;
1776}
1777
1778/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
1779/* like the files created with Adobe Premiere 5.0, for samples see */
1780/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
1781static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1782{
1783    int err;
1784
1785    if (atom.size < 8)
1786        return 0; /* continue */
1787    if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
1788        url_fskip(pb, atom.size - 4);
1789        return 0;
1790    }
1791    atom.type = get_le32(pb);
1792    atom.offset += 8;
1793    atom.size -= 8;
1794    if (atom.type != MKTAG('m','d','a','t')) {
1795        url_fskip(pb, atom.size);
1796        return 0;
1797    }
1798    err = mov_read_mdat(c, pb, atom);
1799    return err;
1800}
1801
1802static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1803{
1804#if CONFIG_ZLIB
1805    ByteIOContext ctx;
1806    uint8_t *cmov_data;
1807    uint8_t *moov_data; /* uncompressed data */
1808    long cmov_len, moov_len;
1809    int ret = -1;
1810
1811    get_be32(pb); /* dcom atom */
1812    if (get_le32(pb) != MKTAG('d','c','o','m'))
1813        return -1;
1814    if (get_le32(pb) != MKTAG('z','l','i','b')) {
1815        av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
1816        return -1;
1817    }
1818    get_be32(pb); /* cmvd atom */
1819    if (get_le32(pb) != MKTAG('c','m','v','d'))
1820        return -1;
1821    moov_len = get_be32(pb); /* uncompressed size */
1822    cmov_len = atom.size - 6 * 4;
1823
1824    cmov_data = av_malloc(cmov_len);
1825    if (!cmov_data)
1826        return AVERROR(ENOMEM);
1827    moov_data = av_malloc(moov_len);
1828    if (!moov_data) {
1829        av_free(cmov_data);
1830        return AVERROR(ENOMEM);
1831    }
1832    get_buffer(pb, cmov_data, cmov_len);
1833    if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
1834        goto free_and_return;
1835    if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
1836        goto free_and_return;
1837    atom.type = MKTAG('m','o','o','v');
1838    atom.offset = 0;
1839    atom.size = moov_len;
1840#ifdef DEBUG
1841//    { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
1842#endif
1843    ret = mov_read_default(c, &ctx, atom);
1844free_and_return:
1845    av_free(moov_data);
1846    av_free(cmov_data);
1847    return ret;
1848#else
1849    av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
1850    return -1;
1851#endif
1852}
1853
1854/* edit list atom */
1855static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1856{
1857    MOVStreamContext *sc;
1858    int i, edit_count;
1859
1860    if (c->fc->nb_streams < 1)
1861        return 0;
1862    sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
1863
1864    get_byte(pb); /* version */
1865    get_be24(pb); /* flags */
1866    edit_count = get_be32(pb); /* entries */
1867
1868    for(i=0; i<edit_count; i++){
1869        int time;
1870        get_be32(pb); /* Track duration */
1871        time = get_be32(pb); /* Media time */
1872        get_be32(pb); /* Media rate */
1873        if (i == 0 && time != -1) {
1874            sc->time_offset = time;
1875            sc->time_rate = av_gcd(sc->time_rate, time);
1876        }
1877    }
1878
1879    if(edit_count > 1)
1880        av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
1881               "a/v desync might occur, patch welcome\n");
1882
1883    dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
1884    return 0;
1885}
1886
1887static const MOVParseTableEntry mov_default_parse_table[] = {
1888{ MKTAG('a','v','s','s'), mov_read_extradata },
1889{ MKTAG('c','o','6','4'), mov_read_stco },
1890{ MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
1891{ MKTAG('d','i','n','f'), mov_read_default },
1892{ MKTAG('d','r','e','f'), mov_read_dref },
1893{ MKTAG('e','d','t','s'), mov_read_default },
1894{ MKTAG('e','l','s','t'), mov_read_elst },
1895{ MKTAG('e','n','d','a'), mov_read_enda },
1896{ MKTAG('f','i','e','l'), mov_read_extradata },
1897{ MKTAG('f','t','y','p'), mov_read_ftyp },
1898{ MKTAG('g','l','b','l'), mov_read_glbl },
1899{ MKTAG('h','d','l','r'), mov_read_hdlr },
1900{ MKTAG('i','l','s','t'), mov_read_ilst },
1901{ MKTAG('j','p','2','h'), mov_read_extradata },
1902{ MKTAG('m','d','a','t'), mov_read_mdat },
1903{ MKTAG('m','d','h','d'), mov_read_mdhd },
1904{ MKTAG('m','d','i','a'), mov_read_default },
1905{ MKTAG('m','e','t','a'), mov_read_meta },
1906{ MKTAG('m','i','n','f'), mov_read_default },
1907{ MKTAG('m','o','o','f'), mov_read_moof },
1908{ MKTAG('m','o','o','v'), mov_read_moov },
1909{ MKTAG('m','v','e','x'), mov_read_default },
1910{ MKTAG('m','v','h','d'), mov_read_mvhd },
1911{ MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
1912{ MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */
1913{ MKTAG('a','v','c','C'), mov_read_glbl },
1914{ MKTAG('p','a','s','p'), mov_read_pasp },
1915{ MKTAG('s','t','b','l'), mov_read_default },
1916{ MKTAG('s','t','c','o'), mov_read_stco },
1917{ MKTAG('s','t','s','c'), mov_read_stsc },
1918{ MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
1919{ MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
1920{ MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
1921{ MKTAG('s','t','t','s'), mov_read_stts },
1922{ MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
1923{ MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
1924{ MKTAG('t','r','a','k'), mov_read_trak },
1925{ MKTAG('t','r','a','f'), mov_read_default },
1926{ MKTAG('t','r','e','x'), mov_read_trex },
1927{ MKTAG('t','r','k','n'), mov_read_trkn },
1928{ MKTAG('t','r','u','n'), mov_read_trun },
1929{ MKTAG('u','d','t','a'), mov_read_default },
1930{ MKTAG('w','a','v','e'), mov_read_wave },
1931{ MKTAG('e','s','d','s'), mov_read_esds },
1932{ MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
1933{ MKTAG('c','m','o','v'), mov_read_cmov },
1934{ MKTAG(0xa9,'n','a','m'), mov_read_udta_string },
1935{ MKTAG(0xa9,'w','r','t'), mov_read_udta_string },
1936{ MKTAG(0xa9,'c','p','y'), mov_read_udta_string },
1937{ MKTAG(0xa9,'i','n','f'), mov_read_udta_string },
1938{ MKTAG(0xa9,'i','n','f'), mov_read_udta_string },
1939{ MKTAG(0xa9,'A','R','T'), mov_read_udta_string },
1940{ MKTAG(0xa9,'a','l','b'), mov_read_udta_string },
1941{ MKTAG(0xa9,'c','m','t'), mov_read_udta_string },
1942{ MKTAG(0xa9,'a','u','t'), mov_read_udta_string },
1943{ MKTAG(0xa9,'d','a','y'), mov_read_udta_string },
1944{ MKTAG(0xa9,'g','e','n'), mov_read_udta_string },
1945{ MKTAG(0xa9,'e','n','c'), mov_read_udta_string },
1946{ MKTAG(0xa9,'t','o','o'), mov_read_udta_string },
1947{ 0, NULL }
1948};
1949
1950static int mov_probe(AVProbeData *p)
1951{
1952    unsigned int offset;
1953    uint32_t tag;
1954    int score = 0;
1955
1956    /* check file header */
1957    offset = 0;
1958    for(;;) {
1959        /* ignore invalid offset */
1960        if ((offset + 8) > (unsigned int)p->buf_size)
1961            return score;
1962        tag = AV_RL32(p->buf + offset + 4);
1963        switch(tag) {
1964        /* check for obvious tags */
1965        case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
1966        case MKTAG('m','o','o','v'):
1967        case MKTAG('m','d','a','t'):
1968        case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
1969        case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
1970        case MKTAG('f','t','y','p'):
1971            return AVPROBE_SCORE_MAX;
1972        /* those are more common words, so rate then a bit less */
1973        case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
1974        case MKTAG('w','i','d','e'):
1975        case MKTAG('f','r','e','e'):
1976        case MKTAG('j','u','n','k'):
1977        case MKTAG('p','i','c','t'):
1978            return AVPROBE_SCORE_MAX - 5;
1979        case MKTAG(0x82,0x82,0x7f,0x7d):
1980        case MKTAG('s','k','i','p'):
1981        case MKTAG('u','u','i','d'):
1982        case MKTAG('p','r','f','l'):
1983            offset = AV_RB32(p->buf+offset) + offset;
1984            /* if we only find those cause probedata is too small at least rate them */
1985            score = AVPROBE_SCORE_MAX - 50;
1986            break;
1987        default:
1988            /* unrecognized tag */
1989            return score;
1990        }
1991    }
1992    return score;
1993}
1994
1995static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1996{
1997    MOVContext *mov = s->priv_data;
1998    ByteIOContext *pb = s->pb;
1999    int err;
2000    MOVAtom atom = { 0, 0, 0 };
2001
2002    mov->fc = s;
2003    /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
2004    if(!url_is_streamed(pb))
2005        atom.size = url_fsize(pb);
2006    else
2007        atom.size = INT64_MAX;
2008
2009    /* check MOV header */
2010    if ((err = mov_read_default(mov, pb, atom)) < 0) {
2011        av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
2012        return err;
2013    }
2014    if (!mov->found_moov) {
2015        av_log(s, AV_LOG_ERROR, "moov atom not found\n");
2016        return -1;
2017    }
2018    dprintf(mov->fc, "on_parse_exit_offset=%lld\n", url_ftell(pb));
2019
2020    return 0;
2021}
2022
2023static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
2024{
2025    MOVContext *mov = s->priv_data;
2026    MOVStreamContext *sc = 0;
2027    AVIndexEntry *sample = 0;
2028    int64_t best_dts = INT64_MAX;
2029    int i, ret;
2030 retry:
2031    for (i = 0; i < s->nb_streams; i++) {
2032        AVStream *st = s->streams[i];
2033        MOVStreamContext *msc = st->priv_data;
2034        if (st->discard != AVDISCARD_ALL && msc->pb && msc->current_sample < msc->sample_count) {
2035            AVIndexEntry *current_sample = &st->index_entries[msc->current_sample];
2036            int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate,
2037                                     AV_TIME_BASE, msc->time_scale);
2038            dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
2039            if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
2040                (!url_is_streamed(s->pb) &&
2041                 ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
2042                 ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
2043                  (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
2044                sample = current_sample;
2045                best_dts = dts;
2046                sc = msc;
2047            }
2048        }
2049    }
2050    if (!sample) {
2051        mov->found_mdat = 0;
2052        if (!url_is_streamed(s->pb) ||
2053            mov_read_default(mov, s->pb, (MOVAtom){ 0, 0, INT64_MAX }) < 0 ||
2054            url_feof(s->pb))
2055            return -1;
2056        dprintf(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb));
2057        goto retry;
2058    }
2059    /* must be done just before reading, to avoid infinite loop on sample */
2060    sc->current_sample++;
2061    if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
2062        av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
2063               sc->ffindex, sample->pos);
2064        return -1;
2065    }
2066    ret = av_get_packet(sc->pb, pkt, sample->size);
2067    if (ret < 0)
2068        return ret;
2069#if CONFIG_DV_DEMUXER
2070    if (mov->dv_demux && sc->dv_audio_container) {
2071        dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
2072        av_free(pkt->data);
2073        pkt->size = 0;
2074        if (dv_get_packet(mov->dv_demux, pkt) < 0)
2075            return -1;
2076    }
2077#endif
2078    pkt->stream_index = sc->ffindex;
2079    pkt->dts = sample->timestamp;
2080    if (sc->ctts_data) {
2081        assert(sc->ctts_data[sc->ctts_index].duration % sc->time_rate == 0);
2082        pkt->pts = pkt->dts + sc->ctts_data[sc->ctts_index].duration / sc->time_rate;
2083        /* update ctts context */
2084        sc->ctts_sample++;
2085        if (sc->ctts_index < sc->ctts_count &&
2086            sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
2087            sc->ctts_index++;
2088            sc->ctts_sample = 0;
2089        }
2090        if (sc->wrong_dts)
2091            pkt->dts = AV_NOPTS_VALUE;
2092    } else {
2093        AVStream *st = s->streams[sc->ffindex];
2094        int64_t next_dts = (sc->current_sample < sc->sample_count) ?
2095            st->index_entries[sc->current_sample].timestamp : st->duration;
2096        pkt->duration = next_dts - pkt->dts;
2097        pkt->pts = pkt->dts;
2098    }
2099    pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
2100    pkt->pos = sample->pos;
2101    dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
2102            pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
2103    return 0;
2104}
2105
2106static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
2107{
2108    MOVStreamContext *sc = st->priv_data;
2109    int sample, time_sample;
2110    int i;
2111
2112    sample = av_index_search_timestamp(st, timestamp, flags);
2113    dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
2114    if (sample < 0) /* not sure what to do */
2115        return -1;
2116    sc->current_sample = sample;
2117    dprintf(st->codec, "stream %d, found sample %d\n", st->index, sc->current_sample);
2118    /* adjust ctts index */
2119    if (sc->ctts_data) {
2120        time_sample = 0;
2121        for (i = 0; i < sc->ctts_count; i++) {
2122            int next = time_sample + sc->ctts_data[i].count;
2123            if (next > sc->current_sample) {
2124                sc->ctts_index = i;
2125                sc->ctts_sample = sc->current_sample - time_sample;
2126                break;
2127            }
2128            time_sample = next;
2129        }
2130    }
2131    return sample;
2132}
2133
2134static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
2135{
2136    AVStream *st;
2137    int64_t seek_timestamp, timestamp;
2138    int sample;
2139    int i;
2140
2141    if (stream_index >= s->nb_streams)
2142        return -1;
2143    if (sample_time < 0)
2144        sample_time = 0;
2145
2146    st = s->streams[stream_index];
2147    sample = mov_seek_stream(st, sample_time, flags);
2148    if (sample < 0)
2149        return -1;
2150
2151    /* adjust seek timestamp to found sample timestamp */
2152    seek_timestamp = st->index_entries[sample].timestamp;
2153
2154    for (i = 0; i < s->nb_streams; i++) {
2155        st = s->streams[i];
2156        if (stream_index == i || st->discard == AVDISCARD_ALL)
2157            continue;
2158
2159        timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
2160        mov_seek_stream(st, timestamp, flags);
2161    }
2162    return 0;
2163}
2164
2165static int mov_read_close(AVFormatContext *s)
2166{
2167    int i, j;
2168    MOVContext *mov = s->priv_data;
2169    for(i=0; i<s->nb_streams; i++) {
2170        MOVStreamContext *sc = s->streams[i]->priv_data;
2171        av_freep(&sc->ctts_data);
2172        for (j=0; j<sc->drefs_count; j++)
2173            av_freep(&sc->drefs[j].path);
2174        av_freep(&sc->drefs);
2175        if (sc->pb && sc->pb != s->pb)
2176            url_fclose(sc->pb);
2177    }
2178    if(mov->dv_demux){
2179        for(i=0; i<mov->dv_fctx->nb_streams; i++){
2180            av_freep(&mov->dv_fctx->streams[i]->codec);
2181            av_freep(&mov->dv_fctx->streams[i]);
2182        }
2183        av_freep(&mov->dv_fctx);
2184        av_freep(&mov->dv_demux);
2185    }
2186    av_freep(&mov->trex_data);
2187    return 0;
2188}
2189
2190AVInputFormat mov_demuxer = {
2191    "mov,mp4,m4a,3gp,3g2,mj2",
2192    NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
2193    sizeof(MOVContext),
2194    mov_probe,
2195    mov_read_header,
2196    mov_read_packet,
2197    mov_read_close,
2198    mov_read_seek,
2199};
2200