Statistics
| Branch: | Revision:

ffmpeg / libavformat / mov.c @ 4cfbf61b

History | View | Annotate | Download (49.7 KB)

1
/*
2
 * MOV decoder.
3
 * Copyright (c) 2001 Fabrice Bellard.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
#include "avformat.h"
20
#include "avi.h"
21

    
22
#ifdef CONFIG_ZLIB
23
#include <zlib.h>
24
#endif
25

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

    
52
//#define DEBUG
53
#ifdef DEBUG
54
#include <stdio.h>
55
#include <fcntl.h>
56
#endif
57

    
58
/* allows chunk splitting - should work now... */
59
/* in case you can't read a file, try commenting */
60
#define MOV_SPLIT_CHUNKS
61

    
62
/* some streams in QT (and in MP4 mostly) aren't either video nor audio */
63
/* so we first list them as this, then clean up the list of streams we give back, */
64
/* getting rid of these */
65
#define CODEC_TYPE_MOV_OTHER        (enum CodecType) 2
66

    
67
static const CodecTag mov_video_tags[] = {
68
/*  { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
69
/*  { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
70
/*  { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
71
/*    { CODEC_ID_RAWVIDEO, MKTAG('A', 'V', 'U', 'I') }, *//* YUV with alpha-channel (AVID Uncompressed) */
72
/* Graphics */
73
/* Animation */
74
/* Apple video */
75
/* Kodak Photo CD */
76
    { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
77
    { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
78
    { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
79
    { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
80
    { CODEC_ID_MJPEG, MKTAG('A', 'V', 'D', 'J') }, /* MJPEG with alpha-channel (AVID JFIF meridien compressed) */
81
/*    { CODEC_ID_MJPEG, MKTAG('A', 'V', 'R', 'n') }, *//* MJPEG with alpha-channel (AVID ABVB/Truevision NuVista) */
82
/*    { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
83
/* Sorenson video */
84
    { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
85
    { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
86
    { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
87
    { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') }, /* Sorenson Video v3 */
88
    { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
89
    { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
90
/*    { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
91
    { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
92
    { CODEC_ID_H263, MKTAG('s', '2', '6', '3') }, /* H263 ?? works */
93
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
94
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
95
/*    { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, *//* AVID dv */
96
    { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') }, /* On2 VP3 */
97
    { CODEC_ID_NONE, 0 },
98
};
99

    
100
static const CodecTag mov_audio_tags[] = {
101
/*    { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
102
    { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
103
    /* { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') },*/ /* 8 bits */
104
    { CODEC_ID_PCM_U8, MKTAG('r', 'a', 'w', ' ') }, /* 8 bits unsigned */
105
    { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /*  */
106
    { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /*  */
107
    { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /*  */
108
    { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
109
    { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */
110
    { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */
111

    
112
    { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
113
    { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
114
    { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
115
/*    { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
116
/* MP4 tags */
117
    { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, /* MPEG 4 AAC or audio ? */
118
    /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
119
    { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') }, /* AMR-NB 3gp */
120
    { CODEC_ID_NONE, 0 },
121
};
122

    
123
/* the QuickTime file format is quite convoluted...
124
 * it has lots of index tables, each indexing something in another one...
125
 * Here we just use what is needed to read the chunks
126
 */
127

    
128
typedef struct MOV_sample_to_chunk_tbl {
129
    long first;
130
    long count;
131
    long id;
132
} MOV_sample_to_chunk_tbl;
133

    
134
typedef struct {
135
    uint32_t type;
136
    int64_t offset;
137
    int64_t size; /* total size (excluding the size and type fields) */
138
} MOV_atom_t;
139

    
140
typedef struct {
141
    int seed;
142
    int flags;
143
    int size;
144
    void* clrs;
145
} MOV_ctab_t;
146

    
147
typedef struct {
148
    uint8_t  version;
149
    uint32_t flags; // 24bit
150

    
151
    /* 0x03 ESDescrTag */
152
    uint16_t es_id;
153
#define MP4ODescrTag                        0x01
154
#define MP4IODescrTag                        0x02
155
#define MP4ESDescrTag                        0x03
156
#define MP4DecConfigDescrTag                0x04
157
#define MP4DecSpecificDescrTag                0x05
158
#define MP4SLConfigDescrTag                0x06
159
#define MP4ContentIdDescrTag                0x07
160
#define MP4SupplContentIdDescrTag        0x08
161
#define MP4IPIPtrDescrTag                0x09
162
#define MP4IPMPPtrDescrTag                0x0A
163
#define MP4IPMPDescrTag                        0x0B
164
#define MP4RegistrationDescrTag                0x0D
165
#define MP4ESIDIncDescrTag                0x0E
166
#define MP4ESIDRefDescrTag                0x0F
167
#define MP4FileIODescrTag                0x10
168
#define MP4FileODescrTag                0x11
169
#define MP4ExtProfileLevelDescrTag        0x13
170
#define MP4ExtDescrTagsStart                0x80
171
#define MP4ExtDescrTagsEnd                0xFE
172
    uint8_t  stream_priority;
173

    
174
    /* 0x04 DecConfigDescrTag */
175
    uint8_t  object_type_id;
176
    uint8_t  stream_type;
177
    /* XXX: really streamType is
178
     * only 6bit, followed by:
179
     * 1bit  upStream
180
     * 1bit  reserved
181
     */
182
    uint32_t buffer_size_db; // 24
183
    uint32_t max_bitrate;
184
    uint32_t avg_bitrate;
185

    
186
    /* 0x05 DecSpecificDescrTag */
187
    uint8_t  decoder_cfg_len;
188
    uint8_t *decoder_cfg;
189

    
190
    /* 0x06 SLConfigDescrTag */
191
    uint8_t  sl_config_len;
192
    uint8_t *sl_config;
193
} MOV_esds_t;
194

    
195
struct MOVParseTableEntry;
196

    
197
typedef struct MOVStreamContext {
198
    int ffindex; /* the ffmpeg stream id */
199
    int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
200
    long next_chunk;
201
    long chunk_count;
202
    int64_t *chunk_offsets;
203
    long sample_to_chunk_sz;
204
    MOV_sample_to_chunk_tbl *sample_to_chunk;
205
    long sample_to_chunk_index;
206
    long sample_size;
207
    long sample_count;
208
    long *sample_sizes;
209
    int time_scale;
210
    long current_sample;
211
    long left_in_chunk; /* how many samples before next chunk */
212
    /* specific MPEG4 header which is added at the beginning of the stream */
213
    int header_len;
214
    uint8_t *header_data;
215
    MOV_esds_t esds;
216
} MOVStreamContext;
217

    
218
typedef struct MOVContext {
219
    int mp4; /* set to 1 as soon as we are sure that the file is an .mp4 file (even some header parsing depends on this) */
220
    AVFormatContext *fc;
221
    int time_scale;
222
    int duration; /* duration of the longest track */
223
    int found_moov; /* when both 'moov' and 'mdat' sections has been found */
224
    int found_mdat; /* we suppose we have enough data to read the file */
225
    int64_t mdat_size;
226
    int64_t mdat_offset;
227
    int total_streams;
228
    /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
229
     * but we need the info to be able to skip data from those streams in the 'mdat' section
230
     */
231
    MOVStreamContext *streams[MAX_STREAMS];
232

    
233
    int64_t next_chunk_offset;
234
    MOVStreamContext *partial; /* != 0 : there is still to read in the current chunk */
235
    int ctab_size;
236
    MOV_ctab_t **ctab;           /* color tables */
237
    const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
238
    /* NOTE: for recursion save to/ restore from local variable! */
239
} MOVContext;
240

    
241

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

    
244
/* those functions parse an atom */
245
/* return code:
246
 1: found what I wanted, exit
247
 0: continue to parse next atom
248
 -1: error occured, exit
249
 */
250
typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
251

    
252
/* links atom IDs to parse functions */
253
typedef struct MOVParseTableEntry {
254
    uint32_t type;
255
    mov_parse_function func;
256
} MOVParseTableEntry;
257

    
258
#ifdef DEBUG
259
/*
260
 * XXX: static sux, even more in a multithreaded environment...
261
 * Avoid them. This is here just to help debugging.
262
 */
263
static int debug_indent = 0;
264
void print_atom(const char *str, MOV_atom_t atom)
265
{
266
    unsigned int tag, i;
267
    tag = (unsigned int) atom.type;
268
    i=debug_indent;
269
    if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
270
    while(i--)
271
        printf("|");
272
    printf("parse:");
273
    printf(" %s: tag=%c%c%c%c offset=0x%x size=0x%x\n",
274
           str, tag & 0xff,
275
           (tag >> 8) & 0xff,
276
           (tag >> 16) & 0xff,
277
           (tag >> 24) & 0xff,
278
           (unsigned int)atom.offset,
279
           (unsigned int)atom.size);
280
    assert((unsigned int)atom.size < 0x7fffffff);// catching errors
281
}
282
#else
283
#define print_atom(a,b)
284
#endif
285

    
286

    
287
static int mov_read_leaf(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
288
{
289
    print_atom("leaf", atom);
290

    
291
    if (atom.size>1)
292
        url_fskip(pb, atom.size);
293
/*        url_seek(pb, atom_offset+atom.size, SEEK_SET); */
294
    return 0;
295
}
296

    
297
static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
298
{
299
    int64_t total_size = 0;
300
    MOV_atom_t a;
301
    int i;
302
    int err = 0;
303

    
304
#ifdef DEBUG
305
    print_atom("default", atom);
306
    debug_indent++;
307
#endif
308

    
309
    a.offset = atom.offset;
310

    
311
    if (atom.size < 0)
312
        atom.size = 0x7fffffffffffffffLL;
313
    while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
314
        a.size = atom.size;
315
        a.type=0L;
316
        if(atom.size >= 8) {
317
            a.size = get_be32(pb);
318
            a.type = get_le32(pb);
319
        }
320
        total_size += 8;
321
        a.offset += 8;
322
        //printf("type: %08x  %.4s  sz: %Lx  %Lx   %Lx\n", type, (char*)&type, size, atom.size, total_size);
323
        if (a.size == 1) { /* 64 bit extended size */
324
            a.size = get_be64(pb) - 8;
325
            a.offset += 8;
326
            total_size += 8;
327
        }
328
        if (a.size == 0) {
329
            a.size = atom.size - total_size;
330
            if (a.size <= 8)
331
                break;
332
        }
333
        for (i = 0; c->parse_table[i].type != 0L
334
             && c->parse_table[i].type != a.type; i++)
335
            /* empty */;
336

    
337
        a.size -= 8;
338
//        printf(" i=%ld\n", i);
339
        if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
340
//            url_seek(pb, atom.offset+atom.size, SEEK_SET);
341
#ifdef DEBUG
342
            print_atom("unknown", a);
343
#endif
344
            url_fskip(pb, a.size);
345
        } else {
346
#ifdef DEBUG
347
            //char b[5] = { type & 0xff, (type >> 8) & 0xff, (type >> 16) & 0xff, (type >> 24) & 0xff, 0 };
348
            //print_atom(b, type, offset, size);
349
#endif
350
            err = (c->parse_table[i].func)(c, pb, a);
351
        }
352

    
353
        a.offset += a.size;
354
        total_size += a.size;
355
    }
356

    
357
    if (!err && total_size < atom.size && atom.size < 0x7ffff) {
358
        //printf("RESET  %Ld  %Ld  err:%d\n", atom.size, total_size, err);
359
        url_fskip(pb, atom.size - total_size);
360
    }
361

    
362
#ifdef DEBUG
363
    debug_indent--;
364
#endif
365
    return err;
366
}
367

    
368
static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
369
{
370
    unsigned int len;
371
    MOV_ctab_t *t;
372
    //url_fskip(pb, atom.size); // for now
373
    c->ctab = av_realloc(c->ctab, ++c->ctab_size);
374
    t = c->ctab[c->ctab_size];
375
    t->seed = get_be32(pb);
376
    t->flags = get_be16(pb);
377
    t->size = get_be16(pb) + 1;
378
    len = 2 * t->size * 4;
379
    if (len > 0) {
380
        t->clrs = av_malloc(len); // 16bit A R G B
381
        if (t->clrs)
382
            get_buffer(pb, t->clrs, len);
383
    }
384

    
385
    return 0;
386
}
387

    
388
static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
389
{
390
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
391
    int len = 0;
392
    uint32_t type;
393
    uint32_t ctype;
394

    
395
    print_atom("hdlr", atom);
396

    
397
    get_byte(pb); /* version */
398
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
399

    
400
    /* component type */
401
    ctype = get_le32(pb);
402
    type = get_le32(pb); /* component subtype */
403

    
404
#ifdef DEBUG
405
    printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
406
    printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
407
#endif
408
#ifdef DEBUG
409
/* XXX: yeah this is ugly... */
410
    if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
411
        if(type == MKTAG('v', 'i', 'd', 'e'))
412
            puts("hdlr: vide");
413
        else if(type == MKTAG('s', 'o', 'u', 'n'))
414
            puts("hdlr: soun");
415
    } else if(ctype == 0) { /* MP4 */
416
        if(type == MKTAG('v', 'i', 'd', 'e'))
417
            puts("hdlr: vide");
418
        else if(type == MKTAG('s', 'o', 'u', 'n'))
419
            puts("hdlr: soun");
420
        else if(type == MKTAG('o', 'd', 's', 'm'))
421
            puts("hdlr: odsm");
422
        else if(type == MKTAG('s', 'd', 's', 'm'))
423
            puts("hdlr: sdsm");
424
    } else puts("hdlr: meta");
425
#endif
426

    
427
    if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
428
        /* helps parsing the string hereafter... */
429
        c->mp4 = 0;
430
        if(type == MKTAG('v', 'i', 'd', 'e'))
431
            st->codec.codec_type = CODEC_TYPE_VIDEO;
432
        else if(type == MKTAG('s', 'o', 'u', 'n'))
433
            st->codec.codec_type = CODEC_TYPE_AUDIO;
434
    } else if(ctype == 0) { /* MP4 */
435
        /* helps parsing the string hereafter... */
436
        c->mp4 = 1;
437
        if(type == MKTAG('v', 'i', 'd', 'e'))
438
            st->codec.codec_type = CODEC_TYPE_VIDEO;
439
        else if(type == MKTAG('s', 'o', 'u', 'n'))
440
            st->codec.codec_type = CODEC_TYPE_AUDIO;
441
    }
442
    get_be32(pb); /* component  manufacture */
443
    get_be32(pb); /* component flags */
444
    get_be32(pb); /* component flags mask */
445

    
446
    if(atom.size <= 24)
447
        return 0; /* nothing left to read */
448
    /* XXX: MP4 uses a C string, not a pascal one */
449
    /* component name */
450

    
451
    if(c->mp4) {
452
        /* .mp4: C string */
453
        while(get_byte(pb) && (++len < (atom.size - 24)));
454
    } else {
455
        /* .mov: PASCAL string */
456
#ifdef DEBUG
457
        char* buf;
458
#endif
459
        len = get_byte(pb);
460
#ifdef DEBUG
461
        buf = (uint8_t*) av_malloc(len+1);
462
        if (buf) {
463
            get_buffer(pb, buf, len);
464
            buf[len] = '\0';
465
            printf("**buf='%s'\n", buf);
466
            av_free(buf);
467
        } else
468
#endif
469
            url_fskip(pb, len);
470
    }
471

    
472
    return 0;
473
}
474

    
475
static int mov_mp4_read_descr_len(ByteIOContext *pb)
476
{
477
    int len = 0;
478
    int count = 4;
479
    while (count--) {
480
        int c = get_byte(pb);
481
        len = (len << 7) | (c & 0x7f);
482
        if (!(c & 0x80))
483
            break;
484
    }
485
    return len;
486
}
487

    
488
static int mov_mp4_read_descr(ByteIOContext *pb, int *tag)
489
{
490
    int len;
491
    *tag = get_byte(pb);
492
    len = mov_mp4_read_descr_len(pb);
493
#ifdef DEBUG
494
    printf("MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
495
#endif
496
    return len;
497
}
498

    
499
static inline unsigned int get_be24(ByteIOContext *s)
500
{
501
    unsigned int val;
502
    val = get_byte(s) << 16;
503
    val |= get_byte(s) << 8;
504
    val |= get_byte(s);
505
    return val;
506
}
507

    
508
static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
509
{
510
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
511
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
512
    int64_t start_pos = url_ftell(pb);
513
    int tag, len;
514

    
515
    print_atom("esds", atom);
516

    
517
    /* Well, broken but suffisant for some MP4 streams */
518
    get_be32(pb); /* version + flags */
519
    len = mov_mp4_read_descr(pb, &tag);
520
    if (tag == MP4ESDescrTag) {
521
        get_be16(pb); /* ID */
522
        get_byte(pb); /* priority */
523
    } else
524
        get_be16(pb); /* ID */
525

    
526
    len = mov_mp4_read_descr(pb, &tag);
527
    if (tag == MP4DecConfigDescrTag) {
528
        sc->esds.object_type_id = get_byte(pb);
529
        sc->esds.stream_type = get_byte(pb);
530
        sc->esds.buffer_size_db = get_be24(pb);
531
        sc->esds.max_bitrate = get_be32(pb);
532
        sc->esds.avg_bitrate = get_be32(pb);
533

    
534
        len = mov_mp4_read_descr(pb, &tag);
535
        //printf("LEN %d  TAG %d  m:%d a:%d\n", len, tag, sc->esds.max_bitrate, sc->esds.avg_bitrate);
536
        if (tag == MP4DecSpecificDescrTag) {
537
#ifdef DEBUG
538
            printf("Specific MPEG4 header len=%d\n", len);
539
#endif
540
            st->codec.extradata = (uint8_t*) av_mallocz(len);
541
            if (st->codec.extradata) {
542
                get_buffer(pb, st->codec.extradata, len);
543
                st->codec.extradata_size = len;
544
            }
545
        }
546
    }
547
    /* in any case, skip garbage */
548
    url_fskip(pb, atom.size - ((url_ftell(pb) - start_pos)));
549
    return 0;
550
}
551

    
552
/* this atom contains actual media data */
553
static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
554
{
555
    print_atom("mdat", atom);
556

    
557
    if(atom.size == 0) /* wrong one (MP4) */
558
        return 0;
559
    c->found_mdat=1;
560
    c->mdat_offset = atom.offset;
561
    c->mdat_size = atom.size;
562
    if(c->found_moov)
563
        return 1; /* found both, just go */
564
    url_fskip(pb, atom.size);
565
    return 0; /* now go for moov */
566
}
567

    
568
/* this atom should contain all header atoms */
569
static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
570
{
571
    int err;
572

    
573
    print_atom("moov", atom);
574

    
575
    err = mov_read_default(c, pb, atom);
576
    /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
577
    /* so we don't parse the whole file if over a network */
578
    c->found_moov=1;
579
    if(c->found_mdat)
580
        return 1; /* found both, just go */
581
    return 0; /* now go for mdat */
582
}
583

    
584

    
585
static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
586
{
587
    print_atom("mdhd", atom);
588

    
589
    get_byte(pb); /* version */
590

    
591
    get_byte(pb); get_byte(pb);
592
    get_byte(pb); /* flags */
593

    
594
    get_be32(pb); /* creation time */
595
    get_be32(pb); /* modification time */
596

    
597
    c->streams[c->total_streams]->time_scale = get_be32(pb);
598

    
599
#ifdef DEBUG
600
    printf("track[%i].time_scale = %i\n", c->fc->nb_streams-1, c->streams[c->total_streams]->time_scale); /* time scale */
601
#endif
602
    get_be32(pb); /* duration */
603

    
604
    get_be16(pb); /* language */
605
    get_be16(pb); /* quality */
606

    
607
    return 0;
608
}
609

    
610
static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
611
{
612
    print_atom("mvhd", atom);
613

    
614
    get_byte(pb); /* version */
615
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
616

    
617
    get_be32(pb); /* creation time */
618
    get_be32(pb); /* modification time */
619
    c->time_scale = get_be32(pb); /* time scale */
620
#ifdef DEBUG
621
    printf("time scale = %i\n", c->time_scale);
622
#endif
623
    c->duration = get_be32(pb); /* duration */
624
    get_be32(pb); /* preferred scale */
625

    
626
    get_be16(pb); /* preferred volume */
627

    
628
    url_fskip(pb, 10); /* reserved */
629

    
630
    url_fskip(pb, 36); /* display matrix */
631

    
632
    get_be32(pb); /* preview time */
633
    get_be32(pb); /* preview duration */
634
    get_be32(pb); /* poster time */
635
    get_be32(pb); /* selection time */
636
    get_be32(pb); /* selection duration */
637
    get_be32(pb); /* current time */
638
    get_be32(pb); /* next track ID */
639

    
640
    return 0;
641
}
642

    
643
static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
644
{
645
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
646

    
647
    // currently SVQ3 decoder expect full STSD header - so let's fake it
648
    // this should be fixed and just SMI header should be passed
649
    av_free(st->codec.extradata);
650
    st->codec.extradata_size = 0x5a + atom.size;
651
    st->codec.extradata = (uint8_t*) av_mallocz(st->codec.extradata_size);
652

    
653
    if (st->codec.extradata) {
654
        int i;
655
        strcpy(st->codec.extradata, "SVQ3"); // fake
656
        get_buffer(pb, st->codec.extradata + 0x5a, atom.size);
657
        //printf("Reading SMI %Ld  %s\n", atom.size, (char*)st->codec.extradata + 0x5a);
658
    } else
659
        url_fskip(pb, atom.size);
660

    
661
    return 0;
662
}
663

    
664
static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
665
{
666
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
667
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
668
    int entries, i;
669

    
670
    print_atom("stco", atom);
671

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

    
675
    entries = get_be32(pb);
676
    sc->chunk_count = entries;
677
    sc->chunk_offsets = (int64_t*) av_malloc(entries * sizeof(int64_t));
678
    if (!sc->chunk_offsets)
679
        return -1;
680
    if (atom.type == MKTAG('s', 't', 'c', 'o')) {
681
        for(i=0; i<entries; i++) {
682
            sc->chunk_offsets[i] = get_be32(pb);
683
        }
684
    } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
685
        for(i=0; i<entries; i++) {
686
            sc->chunk_offsets[i] = get_be64(pb);
687
        }
688
    } else
689
        return -1;
690
#ifdef DEBUG
691
/*
692
    for(i=0; i<entries; i++) {
693
        printf("chunk offset=0x%Lx\n", sc->chunk_offsets[i]);
694
    }
695
*/
696
#endif
697
    return 0;
698
}
699

    
700
static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
701
{
702
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
703
    //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
704
    int entries, frames_per_sample;
705
    uint32_t format;
706

    
707
    print_atom("stsd", atom);
708

    
709
    get_byte(pb); /* version */
710
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
711

    
712
    entries = get_be32(pb);
713

    
714
    while(entries--) { //Parsing Sample description table
715
        enum CodecID id;
716
        int size = get_be32(pb); /* size */
717
        format = get_le32(pb); /* data format */
718

    
719
        get_be32(pb); /* reserved */
720
        get_be16(pb); /* reserved */
721
        get_be16(pb); /* index */
722

    
723
        /* for MPEG4: set codec type by looking for it */
724
        id = codec_get_id(mov_video_tags, format);
725
        if (id >= 0) {
726
            AVCodec *codec;
727
            codec = avcodec_find_decoder(id);
728
            if (codec)
729
                st->codec.codec_type = codec->type;
730
        }
731
#ifdef DEBUG
732
        printf("size=%d 4CC= %c%c%c%c codec_type=%d\n",
733
               size,
734
               (format >> 0) & 0xff,
735
               (format >> 8) & 0xff,
736
               (format >> 16) & 0xff,
737
               (format >> 24) & 0xff,
738
               st->codec.codec_type);
739
#endif
740
        st->codec.codec_tag = format;
741
        if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
742
            MOV_atom_t a = { 0, 0, 0 };
743
            st->codec.codec_id = id;
744
            get_be16(pb); /* version */
745
            get_be16(pb); /* revision level */
746
            get_be32(pb); /* vendor */
747
            get_be32(pb); /* temporal quality */
748
            get_be32(pb); /* spacial quality */
749
            st->codec.width = get_be16(pb); /* width */
750
            st->codec.height = get_be16(pb); /* height */
751
#if 1
752
            if (st->codec.codec_id == CODEC_ID_MPEG4) {
753
                /* in some MPEG4 the width/height are not correct, so
754
                   we ignore this info */
755
                st->codec.width = 0;
756
                st->codec.height = 0;
757
            }
758
#endif
759
            get_be32(pb); /* horiz resolution */
760
            get_be32(pb); /* vert resolution */
761
            get_be32(pb); /* data size, always 0 */
762
            frames_per_sample = get_be16(pb); /* frames per samples */
763
#ifdef DEBUG
764
            printf("frames/samples = %d\n", frames_per_sample);
765
#endif
766
            get_buffer(pb, (uint8_t *)st->codec.codec_name, 32); /* codec name */
767

    
768
            st->codec.bits_per_sample = get_be16(pb); /* depth */
769
            st->codec.color_table_id = get_be16(pb); /* colortable id */
770

    
771
            st->codec.frame_rate      = 25;
772
            st->codec.frame_rate_base = 1;
773

    
774
            size -= (16+8*4+2+32+2*2);
775
#if 0
776
            while (size >= 8) {
777
                MOV_atom_t a;
778
                int64_t start_pos;
779

780
                a.size = get_be32(pb);
781
                a.type = get_le32(pb);
782
                size -= 8;
783
#ifdef DEBUG
784
                printf("VIDEO: atom_type=%c%c%c%c atom.size=%Ld size_left=%d\n",
785
                       (a.type >> 0) & 0xff,
786
                       (a.type >> 8) & 0xff,
787
                       (a.type >> 16) & 0xff,
788
                       (a.type >> 24) & 0xff,
789
                       a.size, size);
790
#endif
791
                start_pos = url_ftell(pb);
792

    
793
                switch(a.type) {
794
                case MKTAG('e', 's', 'd', 's'):
795
                    {
796
                        int tag, len;
797
                        /* Well, broken but suffisant for some MP4 streams */
798
                        get_be32(pb); /* version + flags */
799
                        len = mov_mp4_read_descr(pb, &tag);
800
                        if (tag == 0x03) {
801
                            /* MP4ESDescrTag */
802
                            get_be16(pb); /* ID */
803
                            get_byte(pb); /* priority */
804
                            len = mov_mp4_read_descr(pb, &tag);
805
                            if (tag != 0x04)
806
                                goto fail;
807
                            /* MP4DecConfigDescrTag */
808
                            get_byte(pb); /* objectTypeId */
809
                            get_be32(pb); /* streamType + buffer size */
810
                            get_be32(pb); /* max bit rate */
811
                            get_be32(pb); /* avg bit rate */
812
                            len = mov_mp4_read_descr(pb, &tag);
813
                            if (tag != 0x05)
814
                                goto fail;
815
                            /* MP4DecSpecificDescrTag */
816
#ifdef DEBUG
817
                            printf("Specific MPEG4 header len=%d\n", len);
818
#endif
819
                            sc->header_data = av_mallocz(len);
820
                            if (sc->header_data) {
821
                                get_buffer(pb, sc->header_data, len);
822
                                sc->header_len = len;
823
                            }
824
                        }
825
                        /* in any case, skip garbage */
826
                    }
827
                    break;
828
                default:
829
                    break;
830
                }
831
            fail:
832
                printf("ATOMENEWSIZE %Ld   %d\n", atom.size, url_ftell(pb) - start_pos);
833
                if (atom.size > 8) {
834
                    url_fskip(pb, (atom.size - 8) -
835
                              ((url_ftell(pb) - start_pos)));
836
                    size -= atom.size - 8;
837
                }
838
            }
839
            if (size > 0) {
840
                /* unknown extension */
841
                url_fskip(pb, size);
842
            }
843
#else
844
            a.size = size;
845
            mov_read_default(c, pb, a);
846
#endif
847
        } else {
848
            st->codec.codec_id = codec_get_id(mov_audio_tags, format);
849
            if(st->codec.codec_id==CODEC_ID_AMR_NB) //from TS26.244
850
            {
851
#ifdef DEBUG
852
               printf("AMR-NB audio identified!!\n");
853
#endif
854
               get_be32(pb);get_be32(pb); //Reserved_8
855
               get_be16(pb);//Reserved_2
856
               get_be16(pb);//Reserved_2
857
               get_be32(pb);//Reserved_4
858
               get_be16(pb);//TimeScale
859
               get_be16(pb);//Reserved_2
860

    
861
                //AMRSpecificBox.(10 bytes)
862
                
863
#ifdef DEBUG
864
               int damr_size=
865
#endif
866
               get_be32(pb); //size
867
#ifdef DEBUG
868
               int damr_type=
869
#endif
870
               get_be32(pb); //type=='damr'
871
#ifdef DEBUG
872
               int damr_vendor=
873
#endif
874
               get_be32(pb); //vendor
875
               get_byte(pb); //decoder version
876
               get_be16(pb); //mode_set
877
               get_byte(pb); //mode_change_period
878
               get_byte(pb); //frames_per_sample
879

    
880
#ifdef DEBUG
881
               printf("Audio: damr_type=%c%c%c%c damr_size=%Ld damr_vendor=%c%c%c%c\n",
882
                       (damr_type >> 24) & 0xff,
883
                       (damr_type >> 16) & 0xff,
884
                       (damr_type >> 8) & 0xff,
885
                       (damr_type >> 0) & 0xff,
886
                       damr_size, 
887
                       (damr_vendor >> 24) & 0xff,
888
                       (damr_vendor >> 16) & 0xff,
889
                       (damr_vendor >> 8) & 0xff,
890
                       (damr_vendor >> 0) & 0xff
891
                       );
892
#endif
893
                
894
               st->time_length=0;//Not possible to get from this info, must count number of AMR frames
895
               st->codec.sample_rate=8000;
896
               st->codec.channels=1;
897
               st->codec.bits_per_sample=16;
898
               st->codec.bit_rate=0; /*It is not possible to tell this before we have 
899
                                       an audio frame and even then every frame can be different*/
900
            }
901
            else
902
            {
903
                get_be16(pb); /* version */
904
                get_be16(pb); /* revision level */
905
                get_be32(pb); /* vendor */
906

    
907
                st->codec.channels = get_be16(pb);                /* channel count */
908
                st->codec.bits_per_sample = get_be16(pb);        /* sample size */
909

    
910
                /* handle specific s8 codec */
911
                get_be16(pb); /* compression id = 0*/
912
                get_be16(pb); /* packet size = 0 */
913

    
914
                st->codec.sample_rate = ((get_be32(pb) >> 16));
915
                //printf("CODECID %d  %d  %.4s\n", st->codec.codec_id, CODEC_ID_PCM_S16BE, (char*)&format);
916

    
917
                switch (st->codec.codec_id) {
918
                case CODEC_ID_PCM_S16BE:
919
                    if (st->codec.bits_per_sample == 8)
920
                        st->codec.codec_id = CODEC_ID_PCM_S8;
921
                    /* fall */
922
                case CODEC_ID_PCM_U8:
923
                    st->codec.bit_rate = st->codec.sample_rate * 8;
924
                    break;
925
                default:
926
                    ;
927
                }
928
                get_be32(pb); /* samples per packet */
929
                get_be32(pb); /* bytes per packet */
930
                get_be32(pb); /* bytes per frame */
931
                get_be32(pb); /* bytes per sample */
932
                {
933
                    MOV_atom_t a = { format, url_ftell(pb), size - (16 + 20 + 16 + 8) };
934
                    mov_read_default(c, pb, a);
935
                }
936
            }
937
        }
938
    }
939

    
940
    return 0;
941
}
942

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

    
949
    print_atom("stsc", atom);
950

    
951
    get_byte(pb); /* version */
952
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
953

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

    
973
static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
974
{
975
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
976
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
977
    int entries, i;
978

    
979
    print_atom("stsz", atom);
980

    
981
    get_byte(pb); /* version */
982
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
983

    
984
    sc->sample_size = get_be32(pb);
985
    entries = get_be32(pb);
986
    sc->sample_count = entries;
987
#ifdef DEBUG
988
    printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
989
#endif
990
    if(sc->sample_size)
991
        return 0; /* there isn't any table following */
992
    sc->sample_sizes = (long*) av_malloc(entries * sizeof(long));
993
    if (!sc->sample_sizes)
994
        return -1;
995
    for(i=0; i<entries; i++) {
996
        sc->sample_sizes[i] = get_be32(pb);
997
#ifdef DEBUG
998
/*        printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
999
#endif
1000
    }
1001
    return 0;
1002
}
1003

    
1004
static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1005
{
1006
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1007
    //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
1008
    int entries, i;
1009
    int duration=0;
1010
    int total_sample_count=0;
1011

    
1012
    print_atom("stts", atom);
1013

    
1014
    get_byte(pb); /* version */
1015
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1016
    entries = get_be32(pb);
1017

    
1018

    
1019
#ifdef DEBUG
1020
printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
1021
#endif
1022
    for(i=0; i<entries; i++) {
1023
        int sample_duration;
1024
        int sample_count;
1025

    
1026
        sample_count=get_be32(pb);
1027
        sample_duration = get_be32(pb);
1028
#ifdef DEBUG
1029
        printf("sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
1030
#endif
1031
        duration+=sample_duration*sample_count;
1032
        total_sample_count+=sample_count;
1033

    
1034
#if 0 //We calculate an average instead, needed by .mp4-files created with nec e606 3g phone
1035

1036
        if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
1037
            st->codec.frame_rate_base = sample_duration ? sample_duration : 1;
1038
            st->codec.frame_rate = c->streams[c->total_streams]->time_scale;
1039
#ifdef DEBUG
1040
            printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
1041
#endif
1042
        }
1043
#endif
1044
    }
1045

    
1046
    if(st->codec.codec_type==CODEC_TYPE_VIDEO)
1047
    {
1048
        //Only support constant frame rate. But lets calculate the average. Needed by .mp4-files created with nec e606 3g phone.
1049
        //Hmm, lets set base to 1
1050
        st->codec.frame_rate_base=1;
1051
        st->codec.frame_rate = st->codec.frame_rate_base * c->streams[c->total_streams]->time_scale * total_sample_count / duration;
1052
#ifdef DEBUG
1053
        printf("VIDEO FRAME RATE average= %i (tot sample count= %i ,tot dur= %i)\n", st->codec.frame_rate,total_sample_count,duration);
1054
#endif
1055
    }
1056
    return 0;
1057
}
1058

    
1059
static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1060
{
1061
    AVStream *st;
1062
    MOVStreamContext *sc;
1063

    
1064
    print_atom("trak", atom);
1065

    
1066
    st = av_new_stream(c->fc, c->fc->nb_streams);
1067
    if (!st) return -2;
1068
    sc = (MOVStreamContext*) av_mallocz(sizeof(MOVStreamContext));
1069
    if (!sc) {
1070
        av_free(st);
1071
        return -1;
1072
    }
1073

    
1074
    sc->sample_to_chunk_index = -1;
1075
    st->priv_data = sc;
1076
    st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
1077
    st->time_length = (c->duration * 1000) / c->time_scale; // time in miliseconds
1078
    c->streams[c->fc->nb_streams-1] = sc;
1079

    
1080
    return mov_read_default(c, pb, atom);
1081
}
1082

    
1083
static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1084
{
1085
    AVStream *st;
1086

    
1087
    print_atom("tkhd", atom);
1088

    
1089
    st = c->fc->streams[c->fc->nb_streams-1];
1090

    
1091
    get_byte(pb); /* version */
1092

    
1093
    get_byte(pb); get_byte(pb);
1094
    get_byte(pb); /* flags */
1095
    /*
1096
    MOV_TRACK_ENABLED 0x0001
1097
    MOV_TRACK_IN_MOVIE 0x0002
1098
    MOV_TRACK_IN_PREVIEW 0x0004
1099
    MOV_TRACK_IN_POSTER 0x0008
1100
    */
1101

    
1102
    get_be32(pb); /* creation time */
1103
    get_be32(pb); /* modification time */
1104
    st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
1105
    get_be32(pb); /* reserved */
1106
    st->time_length = get_be32(pb) * 1000 / c->time_scale; /* duration */
1107
    get_be32(pb); /* reserved */
1108
    get_be32(pb); /* reserved */
1109

    
1110
    get_be16(pb); /* layer */
1111
    get_be16(pb); /* alternate group */
1112
    get_be16(pb); /* volume */
1113
    get_be16(pb); /* reserved */
1114

    
1115
    url_fskip(pb, 36); /* display matrix */
1116

    
1117
    /* those are fixed-point */
1118
    st->codec.width = get_be32(pb) >> 16; /* track width */
1119
    st->codec.height = get_be32(pb) >> 16; /* track height */
1120

    
1121
    return 0;
1122
}
1123

    
1124
/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
1125
/* like the files created with Adobe Premiere 5.0, for samples see */
1126
/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
1127
static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1128
{
1129
    int err;
1130

    
1131
#ifdef DEBUG
1132
    print_atom("wide", atom);
1133
    debug_indent++;
1134
#endif
1135
    if (atom.size < 8)
1136
        return 0; /* continue */
1137
    if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
1138
        url_fskip(pb, atom.size - 4);
1139
        return 0;
1140
    }
1141
    atom.type = get_le32(pb);
1142
    atom.offset += 8;
1143
    atom.size -= 8;
1144
    if (atom.type != MKTAG('m', 'd', 'a', 't')) {
1145
        url_fskip(pb, atom.size);
1146
        return 0;
1147
    }
1148
    err = mov_read_mdat(c, pb, atom);
1149
#ifdef DEBUG
1150
    debug_indent--;
1151
#endif
1152
    return err;
1153
}
1154

    
1155

    
1156
#ifdef CONFIG_ZLIB
1157
static int null_read_packet(void *opaque, uint8_t *buf, int buf_size)
1158
{
1159
    return -1;
1160
}
1161

    
1162
static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1163
{
1164
    ByteIOContext ctx;
1165
    uint8_t *cmov_data;
1166
    uint8_t *moov_data; /* uncompressed data */
1167
    long cmov_len, moov_len;
1168
    int ret;
1169

    
1170
    print_atom("cmov", atom);
1171

    
1172
    get_be32(pb); /* dcom atom */
1173
    if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
1174
        return -1;
1175
    if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
1176
        dprintf("unknown compression for cmov atom !");
1177
        return -1;
1178
    }
1179
    get_be32(pb); /* cmvd atom */
1180
    if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
1181
        return -1;
1182
    moov_len = get_be32(pb); /* uncompressed size */
1183
    cmov_len = atom.size - 6 * 4;
1184

    
1185
    cmov_data = (uint8_t *) av_malloc(cmov_len);
1186
    if (!cmov_data)
1187
        return -1;
1188
    moov_data = (uint8_t *) av_malloc(moov_len);
1189
    if (!moov_data) {
1190
        av_free(cmov_data);
1191
        return -1;
1192
    }
1193
    get_buffer(pb, cmov_data, cmov_len);
1194
    if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
1195
        return -1;
1196
    if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
1197
        return -1;
1198
    ctx.buf_end = ctx.buffer + moov_len;
1199
    atom.type = MKTAG( 'm', 'o', 'o', 'v' );
1200
    atom.offset = 0;
1201
    atom.size = moov_len;
1202
#ifdef DEBUG
1203
    { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
1204
#endif
1205
    ret = mov_read_default(c, &ctx, atom);
1206
    av_free(moov_data);
1207
    av_free(cmov_data);
1208

    
1209
    return ret;
1210
}
1211
#endif
1212

    
1213
static const MOVParseTableEntry mov_default_parse_table[] = {
1214
/* mp4 atoms */
1215
{ MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
1216
{ MKTAG( 'c', 'p', 'r', 't' ), mov_read_default },
1217
{ MKTAG( 'c', 'r', 'h', 'd' ), mov_read_default },
1218
{ MKTAG( 'c', 't', 't', 's' ), mov_read_leaf }, /* composition time to sample */
1219
{ MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default }, /* data information */
1220
{ MKTAG( 'd', 'p', 'n', 'd' ), mov_read_leaf },
1221
{ MKTAG( 'd', 'r', 'e', 'f' ), mov_read_leaf },
1222
{ MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
1223
{ MKTAG( 'e', 'l', 's', 't' ), mov_read_leaf },
1224
{ MKTAG( 'f', 'r', 'e', 'e' ), mov_read_leaf },
1225
{ MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
1226
{ MKTAG( 'h', 'i', 'n', 't' ), mov_read_leaf },
1227
{ MKTAG( 'h', 'm', 'h', 'd' ), mov_read_leaf },
1228
{ MKTAG( 'i', 'o', 'd', 's' ), mov_read_leaf },
1229
{ MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
1230
{ MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
1231
{ MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
1232
{ MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
1233
{ MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
1234
{ MKTAG( 'm', 'p', '4', 'a' ), mov_read_default },
1235
{ MKTAG( 'm', 'p', '4', 's' ), mov_read_default },
1236
{ MKTAG( 'm', 'p', '4', 'v' ), mov_read_default },
1237
{ MKTAG( 'm', 'p', 'o', 'd' ), mov_read_leaf },
1238
{ MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
1239
{ MKTAG( 'n', 'm', 'h', 'd' ), mov_read_leaf },
1240
{ MKTAG( 'o', 'd', 'h', 'd' ), mov_read_default },
1241
{ MKTAG( 's', 'd', 'h', 'd' ), mov_read_default },
1242
{ MKTAG( 's', 'k', 'i', 'p' ), mov_read_default },
1243
{ MKTAG( 's', 'm', 'h', 'd' ), mov_read_leaf }, /* sound media info header */
1244
{ MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorrenson extension ??? */
1245
{ MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
1246
{ MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
1247
{ MKTAG( 's', 't', 'd', 'p' ), mov_read_default },
1248
{ MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
1249
{ MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
1250
{ MKTAG( 's', 't', 's', 'h' ), mov_read_default },
1251
{ MKTAG( 's', 't', 's', 's' ), mov_read_leaf }, /* sync sample */
1252
{ MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
1253
{ MKTAG( 's', 't', 't', 's' ), mov_read_stts },
1254
{ MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
1255
{ MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
1256
{ MKTAG( 't', 'r', 'e', 'f' ), mov_read_default }, /* not really */
1257
{ MKTAG( 'u', 'd', 't', 'a' ), mov_read_leaf },
1258
{ MKTAG( 'u', 'r', 'l', ' ' ), mov_read_leaf },
1259
{ MKTAG( 'u', 'r', 'n', ' ' ), mov_read_leaf },
1260
{ MKTAG( 'u', 'u', 'i', 'd' ), mov_read_default },
1261
{ MKTAG( 'v', 'm', 'h', 'd' ), mov_read_leaf }, /* video media info header */
1262
{ MKTAG( 'w', 'a', 'v', 'e' ), mov_read_default },
1263
/* extra mp4 */
1264
{ MKTAG( 'M', 'D', 'E', 'S' ), mov_read_leaf },
1265
/* QT atoms */
1266
{ MKTAG( 'c', 'h', 'a', 'p' ), mov_read_leaf },
1267
{ MKTAG( 'c', 'l', 'i', 'p' ), mov_read_default },
1268
{ MKTAG( 'c', 'r', 'g', 'n' ), mov_read_leaf },
1269
{ MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
1270
{ MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
1271
{ MKTAG( 'k', 'm', 'a', 't' ), mov_read_leaf },
1272
{ MKTAG( 'm', 'a', 't', 't' ), mov_read_default },
1273
{ MKTAG( 'r', 'd', 'r', 'f' ), mov_read_leaf },
1274
{ MKTAG( 'r', 'm', 'd', 'a' ), mov_read_default },
1275
{ MKTAG( 'r', 'm', 'd', 'r' ), mov_read_leaf },
1276
{ MKTAG( 'r', 'm', 'r', 'a' ), mov_read_default },
1277
{ MKTAG( 's', 'c', 'p', 't' ), mov_read_leaf },
1278
{ MKTAG( 's', 's', 'r', 'c' ), mov_read_leaf },
1279
{ MKTAG( 's', 'y', 'n', 'c' ), mov_read_leaf },
1280
{ MKTAG( 't', 'c', 'm', 'd' ), mov_read_leaf },
1281
{ MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
1282
//{ MKTAG( 'r', 'm', 'q', 'u' ), mov_read_leaf },
1283
#ifdef CONFIG_ZLIB
1284
{ MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
1285
#else
1286
{ MKTAG( 'c', 'm', 'o', 'v' ), mov_read_leaf },
1287
#endif
1288
{ 0L, mov_read_leaf }
1289
};
1290

    
1291
static void mov_free_stream_context(MOVStreamContext *sc)
1292
{
1293
    if(sc) {
1294
        av_free(sc->chunk_offsets);
1295
        av_free(sc->sample_to_chunk);
1296
        av_free(sc->sample_sizes);
1297
        av_free(sc->header_data);
1298
        av_free(sc);
1299
    }
1300
}
1301

    
1302
static inline uint32_t mov_to_tag(uint8_t *buf)
1303
{
1304
    return MKTAG(buf[0], buf[1], buf[2], buf[3]);
1305
}
1306

    
1307
static inline uint32_t to_be32(uint8_t *buf)
1308
{
1309
    return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
1310
}
1311

    
1312
/* XXX: is it sufficient ? */
1313
static int mov_probe(AVProbeData *p)
1314
{
1315
    unsigned int offset;
1316
    uint32_t tag;
1317

    
1318
    /* check file header */
1319
    if (p->buf_size <= 12)
1320
        return 0;
1321
    offset = 0;
1322
    for(;;) {
1323
        /* ignore invalid offset */
1324
        if ((offset + 8) > (unsigned int)p->buf_size)
1325
            return 0;
1326
        tag = mov_to_tag(p->buf + offset + 4);
1327
        switch(tag) {
1328
        case MKTAG( 'm', 'o', 'o', 'v' ):
1329
        case MKTAG( 'w', 'i', 'd', 'e' ):
1330
        case MKTAG( 'f', 'r', 'e', 'e' ):
1331
        case MKTAG( 'm', 'd', 'a', 't' ):
1332
        case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
1333
            return AVPROBE_SCORE_MAX;
1334
        case MKTAG( 'f', 't', 'y', 'p' ):
1335
        case MKTAG( 's', 'k', 'i', 'p' ):
1336
            offset = to_be32(p->buf) + offset;
1337
            break;
1338
        default:
1339
            /* unrecognized tag */
1340
            return 0;
1341
        }
1342
    }
1343
    return 0;
1344
}
1345

    
1346
static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1347
{
1348
    MOVContext *mov = (MOVContext *) s->priv_data;
1349
    ByteIOContext *pb = &s->pb;
1350
    int i, j, nb, err;
1351
    MOV_atom_t atom = { 0, 0, 0 };
1352

    
1353
    mov->fc = s;
1354
    mov->parse_table = mov_default_parse_table;
1355
#if 0
1356
    /* XXX: I think we should auto detect */
1357
    if(s->iformat->name[1] == 'p')
1358
        mov->mp4 = 1;
1359
#endif
1360
    if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1361
        atom.size = url_filesize(url_fileno(pb));
1362
    else
1363
        atom.size = 0x7FFFFFFFFFFFFFFFLL;
1364

    
1365
#ifdef DEBUG
1366
    printf("filesz=%Ld\n", atom.size);
1367
#endif
1368

    
1369
    /* check MOV header */
1370
    err = mov_read_default(mov, pb, atom);
1371
    if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
1372
        fprintf(stderr, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%lld\n",
1373
                err, mov->found_moov, mov->found_mdat, url_ftell(pb));
1374
        return -1;
1375
    }
1376
#ifdef DEBUG
1377
    printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
1378
#endif
1379
    /* some cleanup : make sure we are on the mdat atom */
1380
    if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1381
        url_fseek(pb, mov->mdat_offset, SEEK_SET);
1382

    
1383
    mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
1384

    
1385
#ifdef DEBUG
1386
    printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
1387
#endif
1388

    
1389
#ifdef DEBUG
1390
    printf("streams= %d\n", s->nb_streams);
1391
#endif
1392
    mov->total_streams = nb = s->nb_streams;
1393

    
1394
#if 1
1395
    for(i=0; i<s->nb_streams;) {
1396
        if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
1397
            av_free(s->streams[i]);
1398
            for(j=i+1; j<s->nb_streams; j++)
1399
                s->streams[j-1] = s->streams[j];
1400
            s->nb_streams--;
1401
        } else
1402
            i++;
1403
    }
1404
    for(i=0; i<s->nb_streams;i++) {
1405
        MOVStreamContext *sc;
1406
        sc = (MOVStreamContext *)s->streams[i]->priv_data;
1407
        sc->ffindex = i;
1408
        sc->is_ff_stream = 1;
1409
    }
1410
#endif
1411
#ifdef DEBUG
1412
    printf("real streams= %d\n", s->nb_streams);
1413
#endif
1414
    return 0;
1415
}
1416

    
1417
/* Yes, this is ugly... I didn't write the specs of QT :p */
1418
/* XXX:remove useless commented code sometime */
1419
static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1420
{
1421
    MOVContext *mov = (MOVContext *) s->priv_data;
1422
    MOVStreamContext *sc;
1423
    int64_t offset = 0x0FFFFFFFFFFFFFFFLL;
1424
    int i;
1425
    int size;
1426
    size = 0x0FFFFFFF;
1427

    
1428
#ifdef MOV_SPLIT_CHUNKS
1429
    if (mov->partial) {
1430

    
1431
        int idx;
1432

    
1433
        sc = mov->partial;
1434
        idx = sc->sample_to_chunk_index;
1435

    
1436
        if (idx < 0) return 0;
1437
        size = sc->sample_sizes[sc->current_sample];
1438

    
1439
        sc->current_sample++;
1440
        sc->left_in_chunk--;
1441

    
1442
        if (sc->left_in_chunk <= 0)
1443
            mov->partial = 0;
1444
        offset = mov->next_chunk_offset;
1445
        /* extract the sample */
1446

    
1447
        goto readchunk;
1448
    }
1449
#endif
1450

    
1451
again:
1452
    sc = 0;
1453
    for(i=0; i<mov->total_streams; i++) {
1454
        MOVStreamContext *msc = mov->streams[i];
1455
        //printf("MOCHUNK %ld  %d   %p  pos:%Ld\n", mov->streams[i]->next_chunk, mov->total_streams, mov->streams[i], url_ftell(&s->pb));
1456
        if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0
1457
           && (msc->chunk_offsets[msc->next_chunk] < offset)) {
1458
            sc = msc;
1459
            offset = msc->chunk_offsets[msc->next_chunk];
1460
            //printf("SELETED  %Ld  i:%d\n", offset, i);
1461
        }
1462
    }
1463
    if (!sc || offset==0x0FFFFFFFFFFFFFFFLL)
1464
        return -1;
1465

    
1466
    sc->next_chunk++;
1467

    
1468
    if(mov->next_chunk_offset < offset) { /* some meta data */
1469
        url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1470
        mov->next_chunk_offset = offset;
1471
    }
1472

    
1473
//printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
1474
    if(!sc->is_ff_stream) {
1475
        url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1476
        mov->next_chunk_offset = offset;
1477
        offset = 0x0FFFFFFFFFFFFFFFLL;
1478
        goto again;
1479
    }
1480

    
1481
    /* now get the chunk size... */
1482

    
1483
    for(i=0; i<mov->total_streams; i++) {
1484
        MOVStreamContext *msc = mov->streams[i];
1485
        if ((msc->next_chunk < msc->chunk_count)
1486
            && ((msc->chunk_offsets[msc->next_chunk] - offset) < size))
1487
            size = msc->chunk_offsets[msc->next_chunk] - offset;
1488
    }
1489
#ifdef MOV_SPLIT_CHUNKS
1490
    /* split chunks into samples */
1491
    if (sc->sample_size == 0) {
1492
        int idx = sc->sample_to_chunk_index;
1493
        if ((idx + 1 < sc->sample_to_chunk_sz)
1494
            && (sc->next_chunk >= sc->sample_to_chunk[idx + 1].first))
1495
           idx++;
1496
        sc->sample_to_chunk_index = idx;
1497
        if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) {
1498
            mov->partial = sc;
1499
            /* we'll have to get those samples before next chunk */
1500
            sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1;
1501
            size = sc->sample_sizes[sc->current_sample];
1502
        }
1503

    
1504
        sc->current_sample++;
1505
    }
1506
#endif
1507

    
1508
readchunk:
1509
//printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size);
1510
    if(size == 0x0FFFFFFF)
1511
        size = mov->mdat_size + mov->mdat_offset - offset;
1512
    if(size < 0)
1513
        return -1;
1514
    if(size == 0)
1515
        return -1;
1516
    url_fseek(&s->pb, offset, SEEK_SET);
1517

    
1518
    //printf("READCHUNK hlen: %d  %d off: %Ld   pos:%Ld\n", size, sc->header_len, offset, url_ftell(&s->pb));
1519
    if (sc->header_len > 0) {
1520
        av_new_packet(pkt, size + sc->header_len);
1521
        memcpy(pkt->data, sc->header_data, sc->header_len);
1522
        get_buffer(&s->pb, pkt->data + sc->header_len, size);
1523
        /* free header */
1524
        av_freep(&sc->header_data);
1525
        sc->header_len = 0;
1526
    } else {
1527
        av_new_packet(pkt, size);
1528
        get_buffer(&s->pb, pkt->data, pkt->size);
1529
    }
1530
    pkt->stream_index = sc->ffindex;
1531

    
1532
#ifdef DEBUG
1533
/*
1534
    printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
1535
    for(i=0; i<8; i++)
1536
        printf("%02x ", pkt->data[i]);
1537
    for(i=0; i<8; i++)
1538
        printf("%c ", (pkt->data[i]) & 0x7F);
1539
    puts("");
1540
*/
1541
#endif
1542

    
1543
    mov->next_chunk_offset = offset + size;
1544

    
1545
    return 0;
1546
}
1547

    
1548
static int mov_read_close(AVFormatContext *s)
1549
{
1550
    int i;
1551
    MOVContext *mov = (MOVContext *) s->priv_data;
1552
    for(i=0; i<mov->total_streams; i++)
1553
        mov_free_stream_context(mov->streams[i]);
1554
    for(i=0; i<s->nb_streams; i++)
1555
        av_freep(&s->streams[i]);
1556
    /* free color tabs */
1557
    for(i=0; i<mov->ctab_size; i++)
1558
        av_freep(&mov->ctab[i]);
1559
    av_freep(&mov->ctab);
1560
    return 0;
1561
}
1562

    
1563
static AVInputFormat mov_iformat = {
1564
    "mov",
1565
    "QuickTime/MPEG4 format",
1566
    sizeof(MOVContext),
1567
    mov_probe,
1568
    mov_read_header,
1569
    mov_read_packet,
1570
    mov_read_close,
1571
};
1572

    
1573
int mov_init(void)
1574
{
1575
    av_register_input_format(&mov_iformat);
1576
    return 0;
1577
}