Statistics
| Branch: | Revision:

ffmpeg / libav / mov.c @ e1687cc0

History | View | Annotate | Download (40.2 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

    
54
/* allows chunk splitting - should work now... */
55
/* in case you can't read a file, try commenting */
56
#define MOV_SPLIT_CHUNKS
57

    
58
#ifdef DEBUG
59
/*
60
 * XXX: static sux, even more in a multithreaded environment...
61
 * Avoid them. This is here just to help debugging.
62
 */
63
static int debug_indent = 0;
64
void print_atom(const char *str, UINT32 type, UINT64 offset, UINT64 size)
65
{
66
    unsigned int tag, i;
67
    tag = (unsigned int) type;
68
    i=debug_indent;
69
    if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
70
    while(i--)
71
        printf("|");
72
    printf("parse:");
73
    printf(" %s: tag=%c%c%c%c offset=0x%x size=0x%x\n",
74
           str, tag & 0xff,
75
           (tag >> 8) & 0xff,
76
           (tag >> 16) & 0xff,
77
           (tag >> 24) & 0xff,
78
           (unsigned int)offset,
79
           (unsigned int)size);
80
}
81
#endif
82

    
83
/* some streams in QT (and in MP4 mostly) aren't either video nor audio */
84
/* so we first list them as this, then clean up the list of streams we give back, */
85
/* getting rid of these */
86
#define CODEC_TYPE_MOV_OTHER 2
87

    
88
static const CodecTag mov_video_tags[] = {
89
/*  { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
90
/*  { CODEC_ID_JPEG, MKTAG('j', 'p', 'e', 'g') }, *//* JPEG */
91
/*  { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
92
/*  { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
93
/* Graphics */
94
/* Animation */
95
/* Apple video */
96
/* Kodak Photo CD */
97
    { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
98
    { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
99
    { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
100
    { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
101
/*    { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
102
/* Sorenson video */
103
    { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
104
    { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
105
    { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
106
    { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') }, /* OpenDiVX *//* yeah ! */
107
    { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
108
/*    { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
109
    { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
110
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
111
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
112
    { 0, 0 }, 
113
};
114

    
115
static const CodecTag mov_audio_tags[] = {
116
/*    { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
117
    { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
118
    { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') }, /* 8 bits */
119
    { CODEC_ID_PCM_U8, 0x20776172 }, /* 8 bits unsigned */
120
    { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /*  */
121
    { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /*  */
122
    { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /*  */
123
    { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
124

    
125
    { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
126
    { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
127
    { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
128
/*    { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
129
/* MP4 tags */
130
/*    { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, *//* MPEG 4 AAC or audio ? */
131
                                                     /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
132
    { 0, 0 }, 
133
};
134

    
135
/* the QuickTime file format is quite convoluted...
136
 * it has lots of index tables, each indexing something in another one...
137
 * Here we just use what is needed to read the chunks
138
 */
139

    
140
typedef struct MOV_sample_to_chunk_tbl {
141
    long first;
142
    long count;
143
    long id;
144
} MOV_sample_to_chunk_tbl;
145

    
146
typedef struct MOVStreamContext {
147
    int ffindex; /* the ffmpeg stream id */
148
    int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
149
    long next_chunk;
150
    long chunk_count;
151
    INT64 *chunk_offsets;
152
    long sample_to_chunk_sz;
153
    MOV_sample_to_chunk_tbl *sample_to_chunk;
154
    long sample_to_chunk_index;
155
    long sample_size;
156
    long sample_count;
157
    long *sample_sizes;
158
    long time_scale;
159
    long current_sample;
160
    long left_in_chunk; /* how many samples before next chunk */
161
} MOVStreamContext;
162

    
163
typedef struct MOVContext {
164
    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) */
165
    AVFormatContext *fc;
166
    long time_scale;
167
    int found_moov; /* when both 'moov' and 'mdat' sections has been found */
168
    int found_mdat; /* we suppose we have enough data to read the file */
169
    INT64 mdat_size;
170
    INT64 mdat_offset;
171
    int total_streams;
172
    /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
173
     * but we need the info to be able to skip data from those streams in the 'mdat' section
174
     */
175
    MOVStreamContext *streams[MAX_STREAMS];
176
    
177
    INT64 next_chunk_offset;
178
    int partial; /* != 0 : there is still to read in the current chunk (=id of the stream + 1) */
179
} MOVContext;
180

    
181

    
182
struct MOVParseTableEntry;
183

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

    
186
/* those functions parse an atom */
187
/* return code:
188
 1: found what I wanted, exit
189
 0: continue to parse next atom
190
 -1: error occured, exit
191
 */
192
typedef int (*mov_parse_function)(const struct MOVParseTableEntry *parse_table,
193
                                  ByteIOContext *pb,
194
                                  UINT32 atom_type,
195
                                  INT64 atom_offset, /* after the size and type field (and eventually the extended size) */
196
                                  INT64 atom_size, /* total size (excluding the size and type fields) */
197
                                  void *param);
198

    
199
/* links atom IDs to parse functions */
200
typedef struct MOVParseTableEntry {
201
    UINT32 type;
202
    mov_parse_function func;
203
} MOVParseTableEntry;
204

    
205
static int parse_leaf(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
206
{
207
#ifdef DEBUG
208
    print_atom("leaf", atom_type, atom_offset, atom_size);
209
#endif
210
    if(atom_size>1)
211
        url_fskip(pb, atom_size);
212
/*        url_seek(pb, atom_offset+atom_size, SEEK_SET); */
213
    return 0;
214
}
215

    
216

    
217
static int parse_default(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
218
{
219
    UINT32 type, foo=0;
220
    UINT64 offset, size;
221
    UINT64 total_size = 0;
222
    int i;
223
    int err = 0;
224
    foo=0;
225
#ifdef DEBUG
226
    print_atom("default", atom_type, atom_offset, atom_size);
227
    debug_indent++;
228
#endif
229
    
230
    offset = atom_offset;
231

    
232
    if(atom_size < 0)
233
        atom_size = 0x0FFFFFFFFFFFFFFF;
234
    while((total_size < atom_size) && !url_feof(pb) && !err) {
235
        size=atom_size;
236
        type=0L;
237
        if(atom_size >= 8) {
238
            size = get_be32(pb);
239
            type = get_le32(pb);
240
        }
241
        total_size += 8;
242
        offset+=8;
243
//        printf("type: %08lx  sz: %08lx", type, size);
244
        if(size == 1) { /* 64 bit extended size */
245
            size = get_be64(pb);
246
            offset+=8;
247
            total_size+=8;
248
            size-=8;
249
        }
250
        if(size == 0)
251
            size = atom_size - total_size;
252
        size-=8;
253
        for(i=0; parse_table[i].type != 0L && parse_table[i].type != type; i++);
254
        
255
//        printf(" i=%ld\n", i);
256
        if (parse_table[i].type == 0) { /* skip leaf atoms data */
257
//            url_seek(pb, atom_offset+atom_size, SEEK_SET);
258
#ifdef DEBUG
259
            print_atom("unknown", type, offset, size);
260
#endif
261
            url_fskip(pb, size);
262
        } else
263
            err = (parse_table[i].func)(parse_table, pb, type, offset, size, param);
264

    
265
        offset+=size;
266
        total_size+=size;
267
    }
268

    
269
#ifdef DEBUG
270
    debug_indent--;
271
#endif
272
    return err;
273
}
274

    
275
static int parse_mvhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
276
{
277
    MOVContext *c;
278
#ifdef DEBUG
279
    print_atom("mvhd", atom_type, atom_offset, atom_size);
280
#endif
281
    c = (MOVContext *)param;
282

    
283
    get_byte(pb); /* version */
284
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
285

    
286
    get_be32(pb); /* creation time */
287
    get_be32(pb); /* modification time */
288
    c->time_scale = get_be32(pb); /* time scale */
289
#ifdef DEBUG
290
    printf("time scale = %li\n", c->time_scale);
291
#endif
292
    get_be32(pb); /* duration */
293
    get_be32(pb); /* preferred scale */
294
    
295
    get_be16(pb); /* preferred volume */
296

    
297
    url_fskip(pb, 10); /* reserved */
298

    
299
    url_fskip(pb, 36); /* display matrix */
300

    
301
    get_be32(pb); /* preview time */
302
    get_be32(pb); /* preview duration */
303
    get_be32(pb); /* poster time */
304
    get_be32(pb); /* selection time */
305
    get_be32(pb); /* selection duration */
306
    get_be32(pb); /* current time */
307
    get_be32(pb); /* next track ID */
308
    
309
    return 0;
310
}
311

    
312
/* this atom should contain all header atoms */
313
static int parse_moov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
314
{
315
    int err;
316
    MOVContext *c;
317
#ifdef DEBUG
318
    print_atom("moov", atom_type, atom_offset, atom_size);
319
#endif
320
    c = (MOVContext *)param;
321

    
322
    err = parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
323
    /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
324
    /* so we don't parse the whole file if over a network */
325
    c->found_moov=1;
326
    if(c->found_mdat)
327
        return 1; /* found both, just go */
328
    return 0; /* now go for mdat */
329
}
330

    
331
/* this atom contains actual media data */
332
static int parse_mdat(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
333
{
334
    MOVContext *c;
335
#ifdef DEBUG
336
    print_atom("mdat", atom_type, atom_offset, atom_size);
337
#endif
338
    c = (MOVContext *)param;
339

    
340
    if(atom_size == 0) /* wrong one (MP4) */
341
        return 0;
342
    c->found_mdat=1;
343
    c->mdat_offset = atom_offset;
344
    c->mdat_size = atom_size;
345
    if(c->found_moov)
346
        return 1; /* found both, just go */
347
    url_fskip(pb, atom_size);
348
    return 0; /* now go for moov */
349
}
350

    
351
/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
352
/* like the files created with Adobe Premiere 5.0, for samples see */
353
/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
354
static int parse_wide(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
355
{
356
    int err;
357
    UINT32 type;
358
#ifdef DEBUG
359
    print_atom("wide", atom_type, atom_offset, atom_size);
360
    debug_indent++;
361
#endif
362
    if (atom_size < 8)
363
        return 0; /* continue */
364
    if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
365
        url_fskip(pb, atom_size - 4);
366
        return 0;
367
    }
368
    type = get_le32(pb);
369
    if (type != MKTAG('m', 'd', 'a', 't')) {
370
        url_fskip(pb, atom_size - 8);
371
        return 0;
372
    }
373
    err = parse_mdat(parse_table, pb, type, atom_offset + 8, atom_size - 8, param);
374
#ifdef DEBUG
375
    debug_indent--;
376
#endif
377
    return err;
378
}
379

    
380
static int parse_trak(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
381
{
382
    MOVContext *c;
383
    AVStream *st;
384
    MOVStreamContext *sc;
385
#ifdef DEBUG
386
    print_atom("trak", atom_type, atom_offset, atom_size);
387
#endif
388

    
389
    c = (MOVContext *)param;
390
    st = av_new_stream(c->fc, c->fc->nb_streams);
391
    if (!st) return -2;
392
    sc = av_malloc(sizeof(MOVStreamContext));
393
    sc->sample_to_chunk_index = -1;
394
    st->priv_data = sc;
395
    st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
396
    c->streams[c->fc->nb_streams-1] = sc;
397
    return parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
398
}
399

    
400
static int parse_tkhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
401
{
402
    MOVContext *c;
403
    AVStream *st;
404
#ifdef DEBUG
405
    print_atom("tkhd", atom_type, atom_offset, atom_size);
406
#endif
407

    
408
    c = (MOVContext *)param;
409
    st = c->fc->streams[c->fc->nb_streams-1];
410
    
411
    get_byte(pb); /* version */
412

    
413
    get_byte(pb); get_byte(pb);
414
    get_byte(pb); /* flags */
415
    /*
416
    MOV_TRACK_ENABLED 0x0001
417
    MOV_TRACK_IN_MOVIE 0x0002
418
    MOV_TRACK_IN_PREVIEW 0x0004
419
    MOV_TRACK_IN_POSTER 0x0008
420
    */
421

    
422
    get_be32(pb); /* creation time */
423
    get_be32(pb); /* modification time */
424
    st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
425
    get_be32(pb); /* reserved */
426
    get_be32(pb); /* duration */
427
    get_be32(pb); /* reserved */
428
    get_be32(pb); /* reserved */
429
    
430
    get_be16(pb); /* layer */
431
    get_be16(pb); /* alternate group */
432
    get_be16(pb); /* volume */
433
    get_be16(pb); /* reserved */
434

    
435
    url_fskip(pb, 36); /* display matrix */
436

    
437
    /* those are fixed-point */
438
    st->codec.width = get_be32(pb) >> 16; /* track width */
439
    st->codec.height = get_be32(pb) >> 16; /* track height */
440
    
441
    return 0;
442
}
443

    
444
static int parse_mdhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
445
{
446
    MOVContext *c;
447
    AVStream *st;
448
#ifdef DEBUG
449
    print_atom("mdhd", atom_type, atom_offset, atom_size);
450
#endif
451

    
452
    c = (MOVContext *)param;
453
    st = c->fc->streams[c->fc->nb_streams-1];
454
    
455
    get_byte(pb); /* version */
456

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

    
460
    get_be32(pb); /* creation time */
461
    get_be32(pb); /* modification time */
462

    
463
    c->streams[c->total_streams]->time_scale = get_be32(pb);
464

    
465
#ifdef DEBUG
466
    printf("track[%i].time_scale = %li\n", c->fc->nb_streams-1, c->streams[c->total_streams]->time_scale); /* time scale */
467
#endif
468
    get_be32(pb); /* duration */
469

    
470
    get_be16(pb); /* language */
471
    get_be16(pb); /* quality */
472
    
473
    return 0;
474
}
475

    
476
static int parse_hdlr(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
477
{
478
    MOVContext *c;
479
    int len = 0;
480
    char *buf;
481
    UINT32 type;
482
    AVStream *st;
483
    UINT32 ctype;
484
#ifdef DEBUG
485
    print_atom("hdlr", atom_type, atom_offset, atom_size);
486
#endif
487
    c = (MOVContext *)param;
488
    st = c->fc->streams[c->fc->nb_streams-1];
489

    
490
    get_byte(pb); /* version */
491
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
492

    
493
    /* component type */
494
    ctype = get_le32(pb);
495
    type = get_le32(pb); /* component subtype */
496

    
497
#ifdef DEBUG
498
    printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
499
    printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
500
#endif
501
#ifdef DEBUG
502
/* XXX: yeah this is ugly... */
503
    if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
504
        if(type == MKTAG('v', 'i', 'd', 'e'))
505
            puts("hdlr: vide");
506
        else if(type == MKTAG('s', 'o', 'u', 'n'))
507
            puts("hdlr: soun");
508
    } else if(ctype == 0) { /* MP4 */
509
        if(type == MKTAG('v', 'i', 'd', 'e'))
510
            puts("hdlr: vide");
511
        else if(type == MKTAG('s', 'o', 'u', 'n'))
512
            puts("hdlr: soun");
513
        else if(type == MKTAG('o', 'd', 's', 'm'))
514
            puts("hdlr: odsm");
515
        else if(type == MKTAG('s', 'd', 's', 'm'))
516
            puts("hdlr: sdsm");
517
    } else puts("hdlr: meta");
518
#endif
519

    
520
    if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
521
        /* helps parsing the string hereafter... */
522
        c->mp4 = 0;
523
        if(type == MKTAG('v', 'i', 'd', 'e'))
524
            st->codec.codec_type = CODEC_TYPE_VIDEO;
525
        else if(type == MKTAG('s', 'o', 'u', 'n'))
526
            st->codec.codec_type = CODEC_TYPE_AUDIO;
527
    } else if(ctype == 0) { /* MP4 */
528
        /* helps parsing the string hereafter... */
529
        c->mp4 = 1;
530
        if(type == MKTAG('v', 'i', 'd', 'e'))
531
            st->codec.codec_type = CODEC_TYPE_VIDEO;
532
        else if(type == MKTAG('s', 'o', 'u', 'n'))
533
            st->codec.codec_type = CODEC_TYPE_AUDIO;
534
    }
535
    get_be32(pb); /* component  manufacture */
536
    get_be32(pb); /* component flags */
537
    get_be32(pb); /* component flags mask */
538

    
539
    if(atom_size <= 24)
540
        return 0; /* nothing left to read */
541
    /* XXX: MP4 uses a C string, not a pascal one */
542
    /* component name */
543

    
544
    if(c->mp4) {
545
        /* .mp4: C string */
546
        while(get_byte(pb) && (++len < (atom_size - 24)));
547
    } else {
548
        /* .mov: PASCAL string */
549
        len = get_byte(pb);
550
        buf = av_malloc(len+1);
551
        get_buffer(pb, buf, len);
552
        buf[len] = '\0';
553
#ifdef DEBUG
554
        printf("**buf='%s'\n", buf);
555
#endif
556
        av_free(buf);
557
    }
558
#if 0
559
    len = get_byte(pb);
560
    /* XXX: use a better heuristic */
561
    if(len < 32) {
562
        /* assume that it is a Pascal like string */
563
        buf = av_malloc(len+1);
564
        get_buffer(pb, buf, len);
565
        buf[len] = '\0';
566
#ifdef DEBUG
567
        printf("**buf='%s'\n", buf);
568
#endif
569
        av_free(buf);
570
    } else {
571
        /* MP4 string */
572
        for(;;) {
573
            if (len == 0)
574
                break;
575
            len = get_byte(pb);
576
        }
577
    }
578
#endif
579
    
580
    return 0;
581
}
582

    
583
static int parse_stsd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
584
{
585
    MOVContext *c;
586
    int entries, size, samp_sz, frames_per_sample;
587
    UINT32 format;
588
    AVStream *st;
589
#ifdef DEBUG
590
    print_atom("stsd", atom_type, atom_offset, atom_size);
591
#endif
592
    c = (MOVContext *)param;
593
    st = c->fc->streams[c->fc->nb_streams-1];
594

    
595
    get_byte(pb); /* version */
596
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
597

    
598

    
599
    entries = get_be32(pb);
600

    
601
    while(entries--) {
602
        size = get_be32(pb); /* size */
603
        format = get_le32(pb); /* data format */
604
        
605
        get_be32(pb); /* reserved */
606
        get_be16(pb); /* reserved */
607
        get_be16(pb); /* index */
608
/*        if(format == MKTAG('m', 'p', '4', 'v')) */
609
/*            st->codec.codec_type=CODEC_TYPE_VIDEO; *//* force things (XXX: was this for .mp4 ?) */
610
        if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
611
            st->codec.codec_tag = format;
612
            st->codec.codec_id = codec_get_id(mov_video_tags, format);
613
            get_be16(pb); /* version */
614
            get_be16(pb); /* revision level */
615
            get_be32(pb); /* vendor */
616
            get_be32(pb); /* temporal quality */
617
            get_be32(pb); /* spacial quality */
618
            st->codec.width = get_be16(pb); /* width */
619
            st->codec.height = get_be16(pb); /* height */
620
            get_be32(pb); /* horiz resolution */
621
            get_be32(pb); /* vert resolution */
622
            get_be32(pb); /* data size, always 0 */
623
            frames_per_sample = get_be16(pb); /* frame per samples */
624
#ifdef DEBUG
625
            printf("frames/samples = %d\n", frames_per_sample);
626
#endif
627
            url_fskip(pb, 32); /* codec name */
628

    
629
            get_be16(pb); /* depth */
630
            get_be16(pb); /* colortable id */
631
            get_be16(pb); /*  */
632
            get_be16(pb); /*  */
633
            
634
            st->codec.sample_rate = 25 * FRAME_RATE_BASE;
635
            
636
            if(size > 16)
637
                url_fskip(pb, size-(16+24+18+32));
638
        } else {
639
            st->codec.codec_tag = format;
640

    
641
            get_be16(pb); /* version */
642
            get_be16(pb); /* revision level */
643
            get_be32(pb); /* vendor */
644

    
645
            st->codec.channels = get_be16(pb);/* channel count */
646
            samp_sz = get_be16(pb); /* sample size */
647
#ifdef DEBUG
648
            if(samp_sz != 16)
649
                puts("!!! stsd: audio sample size is not 16 bit !");
650
#endif            
651
            st->codec.codec_id = codec_get_id(mov_audio_tags, format);
652
            /* handle specific s8 codec */
653
            if (st->codec.codec_id == CODEC_ID_PCM_S16BE && samp_sz == 8)
654
            st->codec.codec_id = CODEC_ID_PCM_S8;
655

    
656
            get_be16(pb); /* compression id = 0*/
657
            get_be16(pb); /* packet size = 0 */
658
            
659
            st->codec.sample_rate = ((get_be32(pb) >> 16));
660
            st->codec.bit_rate = 0;
661

    
662
            /* this is for .mp4 files */
663
            if(format == MKTAG('m', 'p', '4', 'v')) { /* XXX */
664
                st->codec.codec_type=CODEC_TYPE_VIDEO; /* force things */
665
                st->codec.codec_id = CODEC_ID_MPEG4;
666
                st->codec.frame_rate = 25 * FRAME_RATE_BASE;
667
                st->codec.bit_rate = 100000;
668
            }
669
//            if(format == MKTAG('m', 'p', '4', 'a')) { /* XXX */
670
//                st->codec.codec_type=CODEC_TYPE_AUDIO; /* force things */
671
//                st->codec.codec_id = CODEC_ID_AAC;
672
////                st->codec.frame_rate = 25 * FRAME_RATE_BASE;
673
////                st->codec.bit_rate = 100000;
674
//            }
675

    
676
#if 0
677

678
            get_be16(pb); get_be16(pb); /*  */
679
            get_be16(pb); /*  */
680
            get_be16(pb); /*  */
681
            get_be16(pb); /*  */
682
            get_be16(pb); /*  */
683
#endif            
684
            if(size > 16)
685
                url_fskip(pb, size-(16+20));
686
        }
687
#ifdef DEBUG
688
        printf("4CC= %c%c%c%c\n", *((char *)&format), ((char *)&format)[1], ((char *)&format)[2], ((char *)&format)[3]);
689
#endif
690
    }
691
/*
692
    if(len) {
693
    buf = av_malloc(len+1);
694
        get_buffer(pb, buf, len);
695
        buf[len] = '\0';
696
        puts(buf);
697
        av_free(buf);
698
    }
699
*/
700
    return 0;
701
}
702

    
703
static int parse_stco(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
704
{
705
    MOVContext *c;
706
    int entries, i;
707
    AVStream *st;
708
    MOVStreamContext *sc;
709
#ifdef DEBUG
710
    print_atom("stco", atom_type, atom_offset, atom_size);
711
#endif
712
    c = (MOVContext *)param;
713
    st = c->fc->streams[c->fc->nb_streams-1];
714
    sc = (MOVStreamContext *)st->priv_data;
715
    
716
    get_byte(pb); /* version */
717
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
718

    
719
    entries = get_be32(pb);
720
    sc->chunk_count = entries;
721
    sc->chunk_offsets = av_malloc(entries * sizeof(INT64));
722
    if(atom_type == MKTAG('s', 't', 'c', 'o')) {
723
        for(i=0; i<entries; i++) {
724
            sc->chunk_offsets[i] = get_be32(pb);
725
            /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
726
        }
727
    } else if(atom_type == MKTAG('c', 'o', '6', '4')) {
728
        for(i=0; i<entries; i++) {
729
            sc->chunk_offsets[i] = get_be64(pb);
730
            /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
731
        }
732
    } else
733
        return -1;
734
    return 0;
735
}
736

    
737
static int parse_stsc(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
738
{
739
    MOVContext *c;
740
    int entries, i;
741
    AVStream *st;
742
    MOVStreamContext *sc;
743
#ifdef DEBUG
744
    print_atom("stsc", atom_type, atom_offset, atom_size);
745
#endif
746
    c = (MOVContext *)param;
747
    st = c->fc->streams[c->fc->nb_streams-1];
748
    sc = (MOVStreamContext *)st->priv_data;
749
    
750
    get_byte(pb); /* version */
751
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
752

    
753
    entries = get_be32(pb);
754
#ifdef DEBUG
755
printf("track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
756
#endif
757
    sc->sample_to_chunk_sz = entries;
758
    sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
759
    for(i=0; i<entries; i++) {
760
        sc->sample_to_chunk[i].first = get_be32(pb);
761
        sc->sample_to_chunk[i].count = get_be32(pb);
762
        sc->sample_to_chunk[i].id = get_be32(pb);
763
#ifdef DEBUG
764
/*        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); */
765
#endif
766
    }
767
    return 0;
768
}
769

    
770
static int parse_stsz(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
771
{
772
    MOVContext *c;
773
    int entries, i;
774
    AVStream *st;
775
    MOVStreamContext *sc;
776
#ifdef DEBUG
777
    print_atom("stsz", atom_type, atom_offset, atom_size);
778
#endif
779
    c = (MOVContext *)param;
780
    st = c->fc->streams[c->fc->nb_streams-1];
781
    sc = (MOVStreamContext *)st->priv_data;
782
    
783
    get_byte(pb); /* version */
784
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
785
    
786
    sc->sample_size = get_be32(pb);
787
    entries = get_be32(pb);
788
    sc->sample_count = entries;
789
#ifdef DEBUG
790
    printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
791
#endif
792
    if(sc->sample_size)
793
        return 0; /* there isn't any table following */
794
    sc->sample_sizes = av_malloc(entries * sizeof(long));
795
    for(i=0; i<entries; i++) {
796
        sc->sample_sizes[i] = get_be32(pb);
797
#ifdef DEBUG
798
/*        printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
799
#endif
800
    }
801
    return 0;
802
}
803

    
804
static int parse_stts(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
805
{
806
    MOVContext *c;
807
    int entries, i;
808
    AVStream *st;
809
    MOVStreamContext *sc;
810
#ifdef DEBUG
811
    print_atom("stts", atom_type, atom_offset, atom_size);
812
#endif
813
    c = (MOVContext *)param;
814
    st = c->fc->streams[c->fc->nb_streams-1];
815
    sc = (MOVStreamContext *)st->priv_data;
816

    
817
    get_byte(pb); /* version */
818
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
819
    entries = get_be32(pb);
820
#ifdef DEBUG
821
printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
822
#endif
823
    for(i=0; i<entries; i++) {
824
        int sample_duration;
825

    
826
        get_be32(pb);
827
        sample_duration = get_be32(pb);
828

    
829
        if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
830
            st->codec.frame_rate = FRAME_RATE_BASE * c->streams[c->total_streams]->time_scale;
831
            if (sample_duration)
832
                st->codec.frame_rate /= sample_duration;
833
#ifdef DEBUG
834
            printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
835
#endif
836
        }
837
    }
838
    return 0;
839
}
840

    
841
#ifdef CONFIG_ZLIB
842
static int null_read_packet(void *opaque, UINT8 *buf, int buf_size)
843
{
844
    return -1;
845
}
846

    
847
static int parse_cmov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
848
{
849
    MOVContext *c;
850
    ByteIOContext ctx;
851
    char *cmov_data;
852
    unsigned char *moov_data; /* uncompressed data */
853
    long cmov_len, moov_len;
854
    int ret;
855
#ifdef DEBUG
856
    print_atom("cmov", atom_type, atom_offset, atom_size);
857
#endif
858
    c = (MOVContext *)param;
859

    
860
    get_be32(pb); /* dcom atom */
861
    if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
862
        return -1;
863
    if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
864
        puts("unknown compression for cmov atom !");
865
        return -1;
866
    }
867
    get_be32(pb); /* cmvd atom */
868
    if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
869
        return -1;
870
    moov_len = get_be32(pb); /* uncompressed size */
871
    cmov_len = atom_size - 6 * 4;
872
    
873
    cmov_data = av_malloc(cmov_len);
874
    if (!cmov_data)
875
        return -1;
876
    moov_data = av_malloc(moov_len);
877
    if (!moov_data) {
878
        av_free(cmov_data);
879
        return -1;
880
    }
881
    get_buffer(pb, cmov_data, cmov_len);
882
    if(uncompress (moov_data, &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
883
        return -1;
884
    if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
885
        return -1;
886
    ctx.buf_end = ctx.buffer + moov_len;
887
    ret = parse_default(parse_table, &ctx, MKTAG( 'm', 'o', 'o', 'v' ), 0, moov_len, param);
888
    av_free(moov_data);
889
    av_free(cmov_data);
890
    return ret;
891
}
892
#endif
893

    
894
static const MOVParseTableEntry mov_default_parse_table[] = {
895
/* mp4 atoms */
896
{ MKTAG( 'm', 'p', '4', 'a' ), parse_default },
897
{ MKTAG( 'c', 'o', '6', '4' ), parse_stco },
898
{ MKTAG( 's', 't', 'c', 'o' ), parse_stco },
899
{ MKTAG( 'c', 'r', 'h', 'd' ), parse_default },
900
{ MKTAG( 'c', 't', 't', 's' ), parse_leaf },
901
{ MKTAG( 'c', 'p', 'r', 't' ), parse_default },
902
{ MKTAG( 'u', 'r', 'l', ' ' ), parse_leaf },
903
{ MKTAG( 'u', 'r', 'n', ' ' ), parse_leaf },
904
{ MKTAG( 'd', 'i', 'n', 'f' ), parse_default },
905
{ MKTAG( 'd', 'r', 'e', 'f' ), parse_leaf },
906
{ MKTAG( 's', 't', 'd', 'p' ), parse_default },
907
{ MKTAG( 'e', 's', 'd', 's' ), parse_default },
908
{ MKTAG( 'e', 'd', 't', 's' ), parse_default },
909
{ MKTAG( 'e', 'l', 's', 't' ), parse_leaf },
910
{ MKTAG( 'u', 'u', 'i', 'd' ), parse_default },
911
{ MKTAG( 'f', 'r', 'e', 'e' ), parse_leaf },
912
{ MKTAG( 'h', 'd', 'l', 'r' ), parse_hdlr },
913
{ MKTAG( 'h', 'm', 'h', 'd' ), parse_leaf },
914
{ MKTAG( 'h', 'i', 'n', 't' ), parse_leaf },
915
{ MKTAG( 'n', 'm', 'h', 'd' ), parse_leaf },
916
{ MKTAG( 'm', 'p', '4', 's' ), parse_default },
917
{ MKTAG( 'm', 'd', 'i', 'a' ), parse_default },
918
{ MKTAG( 'm', 'd', 'a', 't' ), parse_mdat },
919
{ MKTAG( 'm', 'd', 'h', 'd' ), parse_mdhd },
920
{ MKTAG( 'm', 'i', 'n', 'f' ), parse_default },
921
{ MKTAG( 'm', 'o', 'o', 'v' ), parse_moov },
922
{ MKTAG( 'm', 'v', 'h', 'd' ), parse_mvhd },
923
{ MKTAG( 'i', 'o', 'd', 's' ), parse_leaf },
924
{ MKTAG( 'o', 'd', 'h', 'd' ), parse_default },
925
{ MKTAG( 'm', 'p', 'o', 'd' ), parse_leaf },
926
{ MKTAG( 's', 't', 's', 'd' ), parse_stsd },
927
{ MKTAG( 's', 't', 's', 'z' ), parse_stsz },
928
{ MKTAG( 's', 't', 'b', 'l' ), parse_default },
929
{ MKTAG( 's', 't', 's', 'c' ), parse_stsc },
930
{ MKTAG( 's', 'd', 'h', 'd' ), parse_default },
931
{ MKTAG( 's', 't', 's', 'h' ), parse_default },
932
{ MKTAG( 's', 'k', 'i', 'p' ), parse_default },
933
{ MKTAG( 's', 'm', 'h', 'd' ), parse_leaf },
934
{ MKTAG( 'd', 'p', 'n', 'd' ), parse_leaf },
935
{ MKTAG( 's', 't', 's', 's' ), parse_leaf },
936
{ MKTAG( 's', 't', 't', 's' ), parse_stts },
937
{ MKTAG( 't', 'r', 'a', 'k' ), parse_trak },
938
{ MKTAG( 't', 'k', 'h', 'd' ), parse_tkhd },
939
{ MKTAG( 't', 'r', 'e', 'f' ), parse_default }, /* not really */
940
{ MKTAG( 'u', 'd', 't', 'a' ), parse_leaf },
941
{ MKTAG( 'v', 'm', 'h', 'd' ), parse_leaf },
942
{ MKTAG( 'm', 'p', '4', 'v' ), parse_default },
943
/* extra mp4 */
944
{ MKTAG( 'M', 'D', 'E', 'S' ), parse_leaf },
945
/* QT atoms */
946
{ MKTAG( 'c', 'h', 'a', 'p' ), parse_leaf },
947
{ MKTAG( 'c', 'l', 'i', 'p' ), parse_default },
948
{ MKTAG( 'c', 'r', 'g', 'n' ), parse_leaf },
949
{ MKTAG( 'k', 'm', 'a', 't' ), parse_leaf },
950
{ MKTAG( 'm', 'a', 't', 't' ), parse_default },
951
{ MKTAG( 'r', 'd', 'r', 'f' ), parse_leaf },
952
{ MKTAG( 'r', 'm', 'd', 'a' ), parse_default },
953
{ MKTAG( 'r', 'm', 'd', 'r' ), parse_leaf },
954
//{ MKTAG( 'r', 'm', 'q', 'u' ), parse_leaf },
955
{ MKTAG( 'r', 'm', 'r', 'a' ), parse_default },
956
{ MKTAG( 's', 'c', 'p', 't' ), parse_leaf },
957
{ MKTAG( 's', 'y', 'n', 'c' ), parse_leaf },
958
{ MKTAG( 's', 's', 'r', 'c' ), parse_leaf },
959
{ MKTAG( 't', 'c', 'm', 'd' ), parse_leaf },
960
{ MKTAG( 'w', 'i', 'd', 'e' ), parse_wide }, /* place holder */
961
#ifdef CONFIG_ZLIB
962
{ MKTAG( 'c', 'm', 'o', 'v' ), parse_cmov },
963
#else
964
{ MKTAG( 'c', 'm', 'o', 'v' ), parse_leaf },
965
#endif
966
{ 0L, parse_leaf }
967
};
968

    
969
static void mov_free_stream_context(MOVStreamContext *sc)
970
{
971
    if(sc) {
972
        av_free(sc->chunk_offsets);
973
        av_free(sc->sample_to_chunk);
974
        av_free(sc);
975
    }
976
}
977

    
978
/* XXX: is it suffisant ? */
979
static int mov_probe(AVProbeData *p)
980
{
981
    int offset;
982

    
983
    /* check file header */
984
    if (p->buf_size <= 12)
985
        return 0;
986
    if ((p->buf[4] == 'm' && p->buf[5] == 'o' &&
987
         p->buf[6] == 'o' && p->buf[7] == 'v') ||
988
        (p->buf[4] == 'w' && p->buf[5] == 'i' &&
989
         p->buf[6] == 'd' && p->buf[7] == 'e') ||
990
        (p->buf[4] == 'f' && p->buf[5] == 'r' &&
991
         p->buf[6] == 'e' && p->buf[7] == 'e') ||
992
        (p->buf[4] == 'm' && p->buf[5] == 'd' &&
993
         p->buf[6] == 'a' && p->buf[7] == 't'))
994
        return AVPROBE_SCORE_MAX;
995
    if (p->buf[4] == 'f' && p->buf[5] == 't' &&
996
        p->buf[6] == 'y' && p->buf[7] == 'p') {
997
        offset = p->buf[0]; offset <<= 8;
998
        offset |= p->buf[1]; offset <<= 8;
999
        offset |= p->buf[2]; offset <<= 8;
1000
        offset |= p->buf[3];
1001
        if (offset > p->buf_size)
1002
            return 0;
1003
        if ((p->buf[offset+4] == 'm' && p->buf[offset+5] == 'o' &&
1004
             p->buf[offset+6] == 'o' && p->buf[offset+7] == 'v') ||
1005
            (p->buf[offset+4] == 'w' && p->buf[offset+5] == 'i' &&
1006
             p->buf[offset+6] == 'd' && p->buf[offset+7] == 'e') ||
1007
            (p->buf[offset+4] == 'm' && p->buf[offset+5] == 'd' &&
1008
             p->buf[offset+6] == 'a' && p->buf[offset+7] == 't'))
1009
            return AVPROBE_SCORE_MAX;
1010

    
1011
    }
1012
    return 0;
1013
}
1014

    
1015
static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1016
{
1017
    MOVContext *mov = s->priv_data;
1018
    ByteIOContext *pb = &s->pb;
1019
    int i, j, nb, err;
1020
    INT64 size;
1021

    
1022
    mov->fc = s;
1023
#if 0
1024
    /* XXX: I think we should auto detect */
1025
    if(s->iformat->name[1] == 'p')
1026
        mov->mp4 = 1;
1027
#endif
1028
    if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1029
        size = url_filesize(url_fileno(pb));
1030
    else
1031
        size = 0x7FFFFFFFFFFFFFFF;
1032

    
1033
#ifdef DEBUG
1034
    printf("filesz=%Ld\n", size);
1035
#endif
1036

    
1037
    /* check MOV header */
1038
    err = parse_default(mov_default_parse_table, pb, 0L, 0LL, size, mov);
1039
    if(err<0 || (!mov->found_moov || !mov->found_mdat)) {
1040
        puts("header not found !!!");
1041
        exit(1);
1042
    }
1043
#ifdef DEBUG
1044
    printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
1045
#endif
1046
    /* some cleanup : make sure we are on the mdat atom */
1047
    if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1048
        url_fseek(pb, mov->mdat_offset, SEEK_SET);
1049

    
1050
    mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
1051

    
1052
#ifdef DEBUG
1053
    printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
1054
#endif
1055

    
1056
#ifdef DEBUG
1057
    printf("streams= %d\n", s->nb_streams);
1058
#endif
1059
    mov->total_streams = nb = s->nb_streams;
1060
    
1061
#if 1
1062
    for(i=0; i<s->nb_streams;) {
1063
        if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
1064
            av_free(s->streams[i]);
1065
            for(j=i+1; j<s->nb_streams; j++)
1066
                s->streams[j-1] = s->streams[j];
1067
            s->nb_streams--;
1068
        } else
1069
            i++;
1070
    }
1071
    for(i=0; i<s->nb_streams;i++) {
1072
        MOVStreamContext *sc;
1073
        sc = (MOVStreamContext *)s->streams[i]->priv_data;
1074
        sc->ffindex = i;
1075
        sc->is_ff_stream = 1;
1076
    }
1077
#endif
1078
#ifdef DEBUG
1079
    printf("real streams= %d\n", s->nb_streams);
1080
#endif
1081
    return 0;
1082
}
1083

    
1084
/* Yes, this is ugly... I didn't write the specs of QT :p */
1085
/* XXX:remove useless commented code sometime */
1086
static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1087
{
1088
    MOVContext *mov = s->priv_data;
1089
    INT64 offset = 0x0FFFFFFFFFFFFFFF;
1090
    int i;
1091
    int st_id = 0, size;
1092
    size = 0x0FFFFFFF;
1093

    
1094
#ifdef MOV_SPLIT_CHUNKS
1095
    if (mov->partial) {
1096
        
1097
        int idx;
1098

    
1099
        st_id = mov->partial - 1;
1100
        idx = mov->streams[st_id]->sample_to_chunk_index;
1101
        if (idx < 0) return 0;
1102
        size = mov->streams[st_id]->sample_sizes[mov->streams[st_id]->current_sample];
1103

    
1104
        mov->streams[st_id]->current_sample++;
1105
        mov->streams[st_id]->left_in_chunk--;
1106

    
1107
        if(mov->streams[st_id]->left_in_chunk <= 0)
1108
            mov->partial = 0;
1109
        offset = mov->next_chunk_offset;
1110
        /* extract the sample */
1111

    
1112
        goto readchunk;
1113
    }
1114
#endif
1115

    
1116
again:
1117
    for(i=0; i<mov->total_streams; i++) {
1118
        if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
1119
        && (mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] < offset)) {
1120
            st_id = i;
1121
            offset = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk];
1122
        }
1123
    }
1124
    mov->streams[st_id]->next_chunk++;
1125
    if(offset==0x0FFFFFFFFFFFFFFF)
1126
        return -1;
1127
    
1128
    if(mov->next_chunk_offset < offset) { /* some meta data */
1129
        url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1130
        mov->next_chunk_offset = offset;
1131
    }
1132

    
1133
//printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
1134
    if(!mov->streams[st_id]->is_ff_stream) {
1135
        url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1136
        mov->next_chunk_offset = offset;
1137
        offset = 0x0FFFFFFFFFFFFFFF;
1138
        goto again;
1139
    }
1140

    
1141
    /* now get the chunk size... */
1142

    
1143
    for(i=0; i<mov->total_streams; i++) {
1144
        if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
1145
        && ((mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset) < size)) {
1146
            size = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset;
1147
        }
1148
    }
1149
#ifdef MOV_SPLIT_CHUNKS
1150
    /* split chunks into samples */
1151
    if(mov->streams[st_id]->sample_size == 0) {
1152
        int idx;
1153
        idx = mov->streams[st_id]->sample_to_chunk_index;
1154
        if ((idx + 1 < mov->streams[st_id]->sample_to_chunk_sz)
1155
               && (mov->streams[st_id]->next_chunk >= mov->streams[st_id]->sample_to_chunk[idx + 1].first))
1156
           idx++; 
1157
        mov->streams[st_id]->sample_to_chunk_index = idx;
1158
        if(idx >= 0 && mov->streams[st_id]->sample_to_chunk[idx].count != 1) {
1159
            mov->partial = st_id+1;
1160
            /* we'll have to get those samples before next chunk */
1161
            mov->streams[st_id]->left_in_chunk = (mov->streams[st_id]->sample_to_chunk[idx].count) - 1;
1162
            size = mov->streams[st_id]->sample_sizes[mov->streams[st_id]->current_sample];
1163
        }
1164

    
1165
        mov->streams[st_id]->current_sample++;
1166
    }
1167
#endif
1168

    
1169
readchunk:
1170

    
1171
//printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size);
1172
    if(size == 0x0FFFFFFF)
1173
        size = mov->mdat_size + mov->mdat_offset - offset;
1174
    if(size < 0)
1175
        return -1;
1176
    if(size == 0)
1177
        return -1;
1178
    av_new_packet(pkt, size);
1179
    pkt->stream_index = mov->streams[st_id]->ffindex;
1180

    
1181
    get_buffer(&s->pb, pkt->data, pkt->size);
1182

    
1183
#ifdef DEBUG
1184
/*
1185
    printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
1186
    for(i=0; i<8; i++)
1187
        printf("%02x ", pkt->data[i]);
1188
    for(i=0; i<8; i++)
1189
        printf("%c ", (pkt->data[i]) & 0x7F);
1190
    puts("");
1191
*/
1192
#endif
1193

    
1194
    mov->next_chunk_offset = offset + size;
1195

    
1196
    return 0;
1197
}
1198

    
1199
static int mov_read_close(AVFormatContext *s)
1200
{
1201
    int i;
1202
    MOVContext *mov = s->priv_data;
1203
    for(i=0; i<mov->total_streams; i++)
1204
        mov_free_stream_context(mov->streams[i]);
1205
    for(i=0; i<s->nb_streams; i++)
1206
        av_free(s->streams[i]);
1207
    return 0;
1208
}
1209

    
1210
static AVInputFormat mov_iformat = {
1211
    "mov",
1212
    "QuickTime/MPEG4 format",
1213
    sizeof(MOVContext),
1214
    mov_probe,
1215
    mov_read_header,
1216
    mov_read_packet,
1217
    mov_read_close,
1218
};
1219

    
1220
int mov_init(void)
1221
{
1222
    av_register_input_format(&mov_iformat);
1223
    return 0;
1224
}