Statistics
| Branch: | Revision:

ffmpeg / libavformat / mov.c @ b6a17df4

History | View | Annotate | Download (47.3 KB)

1 6cea494e Zdenek Kabelac
/*
2
 * MOV decoder.
3 19720f15 Fabrice Bellard
 * Copyright (c) 2001 Fabrice Bellard.
4 6cea494e Zdenek Kabelac
 *
5 19720f15 Fabrice Bellard
 * 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 6cea494e Zdenek Kabelac
 *
10 19720f15 Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 6cea494e Zdenek Kabelac
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 6cea494e Zdenek Kabelac
 *
15 19720f15 Fabrice Bellard
 * 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 6cea494e Zdenek Kabelac
 */
19
#include "avformat.h"
20
#include "avi.h"
21
22 0147f198 François Revol
#ifdef CONFIG_ZLIB
23
#include <zlib.h>
24
#endif
25
26 6cea494e Zdenek Kabelac
/*
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 0147f198 François Revol
 *   FIXED, Francois Revol, 07/17/2002
33 6cea494e Zdenek Kabelac
 * - 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 c9a65ca8 Fabrice Bellard
//#define DEBUG
53 6cea494e Zdenek Kabelac
54 3ffe3793 François Revol
/* allows chunk splitting - should work now... */
55
/* in case you can't read a file, try commenting */
56
#define MOV_SPLIT_CHUNKS
57
58 6cea494e Zdenek Kabelac
#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 0c1a9eda Zdenek Kabelac
void print_atom(const char *str, uint32_t type, uint64_t offset, uint64_t size)
65 6cea494e Zdenek Kabelac
{
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 3ffe3793 François Revol
    printf(" %s: tag=%c%c%c%c offset=0x%x size=0x%x\n",
74 6cea494e Zdenek Kabelac
           str, tag & 0xff,
75
           (tag >> 8) & 0xff,
76
           (tag >> 16) & 0xff,
77
           (tag >> 24) & 0xff,
78
           (unsigned int)offset,
79 5cd62665 Zdenek Kabelac
           (unsigned int)size);
80
    assert((unsigned int)size < 0x7fffffff);// catching errors
81 6cea494e Zdenek Kabelac
}
82 b6a17df4 Zdenek Kabelac
#else
83
#define print_atom(a,b,c,d)
84 6cea494e Zdenek Kabelac
#endif
85
86
/* some streams in QT (and in MP4 mostly) aren't either video nor audio */
87
/* so we first list them as this, then clean up the list of streams we give back, */
88
/* getting rid of these */
89 b6a17df4 Zdenek Kabelac
#define CODEC_TYPE_MOV_OTHER        (enum CodecType) 2
90 6cea494e Zdenek Kabelac
91 a266644f Zdenek Kabelac
static const CodecTag mov_video_tags[] = {
92 6cea494e Zdenek Kabelac
/*  { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
93 0147f198 François Revol
/*  { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
94
/*  { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
95 53e27dd5 Sebastien Bechet
/*    { CODEC_ID_RAWVIDEO, MKTAG('A', 'V', 'U', 'I') }, *//* YUV with alpha-channel (AVID Uncompressed) */
96 6cea494e Zdenek Kabelac
/* Graphics */
97
/* Animation */
98
/* Apple video */
99
/* Kodak Photo CD */
100 3ffe3793 François Revol
    { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
101 6cea494e Zdenek Kabelac
    { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
102 3ffe3793 François Revol
    { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
103 6cea494e Zdenek Kabelac
    { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
104 53e27dd5 Sebastien Bechet
    { CODEC_ID_MJPEG, MKTAG('A', 'V', 'D', 'J') }, /* MJPEG with alpha-channel (AVID JFIF meridien compressed) */
105
/*    { CODEC_ID_MJPEG, MKTAG('A', 'V', 'R', 'n') }, *//* MJPEG with alpha-channel (AVID ABVB/Truevision NuVista) */
106 6cea494e Zdenek Kabelac
/*    { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
107
/* Sorenson video */
108 0147f198 François Revol
    { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
109
    { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
110 3ffe3793 François Revol
    { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
111 0e7eed09 Fabrice Bellard
    { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
112 6cea494e Zdenek Kabelac
    { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
113
/*    { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
114 0147f198 François Revol
    { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
115 e1687cc0 Fabrice Bellard
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
116
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
117 53e27dd5 Sebastien Bechet
/*    { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, *//* AVID dv */
118 5cd62665 Zdenek Kabelac
    { 0, 0 },
119 6cea494e Zdenek Kabelac
};
120
121 a266644f Zdenek Kabelac
static const CodecTag mov_audio_tags[] = {
122 6cea494e Zdenek Kabelac
/*    { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
123
    { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
124
    { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') }, /* 8 bits */
125 5cd62665 Zdenek Kabelac
    { CODEC_ID_PCM_U8, MKTAG('r', 'a', 'w', ' ') }, /* 8 bits unsigned */
126 6cea494e Zdenek Kabelac
    { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /*  */
127
    { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /*  */
128
    { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /*  */
129 0147f198 François Revol
    { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
130 3f95e843 François Revol
    { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */
131
    { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */
132 6cea494e Zdenek Kabelac
133
    { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
134
    { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
135
    { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
136
/*    { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
137
/* MP4 tags */
138
/*    { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, *//* MPEG 4 AAC or audio ? */
139
                                                     /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
140
    { 0, 0 }, 
141
};
142
143
/* the QuickTime file format is quite convoluted...
144
 * it has lots of index tables, each indexing something in another one...
145
 * Here we just use what is needed to read the chunks
146
 */
147
148
typedef struct MOV_sample_to_chunk_tbl {
149
    long first;
150
    long count;
151
    long id;
152
} MOV_sample_to_chunk_tbl;
153
154 5cd62665 Zdenek Kabelac
typedef struct {
155
    uint8_t  version;
156
    uint32_t flags; // 24bit
157
158
    /* 0x03 ESDescrTag */
159
    uint16_t es_id;
160
#define MP4ODescrTag                        0x01
161
#define MP4IODescrTag                        0x02
162
#define MP4ESDescrTag                        0x03
163
#define MP4DecConfigDescrTag                0x04
164
#define MP4DecSpecificDescrTag                0x05
165
#define MP4SLConfigDescrTag                0x06
166
#define MP4ContentIdDescrTag                0x07
167
#define MP4SupplContentIdDescrTag        0x08
168
#define MP4IPIPtrDescrTag                0x09
169
#define MP4IPMPPtrDescrTag                0x0A
170
#define MP4IPMPDescrTag                        0x0B
171
#define MP4RegistrationDescrTag                0x0D
172
#define MP4ESIDIncDescrTag                0x0E
173
#define MP4ESIDRefDescrTag                0x0F
174
#define MP4FileIODescrTag                0x10
175
#define MP4FileODescrTag                0x11
176
#define MP4ExtProfileLevelDescrTag        0x13
177
#define MP4ExtDescrTagsStart                0x80
178
#define MP4ExtDescrTagsEnd                0xFE
179
    uint8_t  stream_priority;
180
181
    /* 0x04 DecConfigDescrTag */
182
    uint8_t  object_type_id;
183
    uint8_t  stream_type;
184
    /* XXX: really streamType is
185
     * only 6bit, followed by:
186
     * 1bit  upStream
187
     * 1bit  reserved
188
     */
189
    uint32_t buffer_size_db; // 24
190
    uint32_t max_bitrate;
191
    uint32_t avg_bitrate;
192
193
    /* 0x05 DecSpecificDescrTag */
194
    uint8_t  decoder_cfg_len;
195
    uint8_t *decoder_cfg;
196
197
    /* 0x06 SLConfigDescrTag */
198
    uint8_t  sl_config_len;
199
    uint8_t *sl_config;
200
} MOV_esds_t;
201
202 b6a17df4 Zdenek Kabelac
struct MOVParseTableEntry;
203
204 6cea494e Zdenek Kabelac
typedef struct MOVStreamContext {
205
    int ffindex; /* the ffmpeg stream id */
206
    int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
207
    long next_chunk;
208
    long chunk_count;
209 0c1a9eda Zdenek Kabelac
    int64_t *chunk_offsets;
210 6cea494e Zdenek Kabelac
    long sample_to_chunk_sz;
211
    MOV_sample_to_chunk_tbl *sample_to_chunk;
212 3ffe3793 François Revol
    long sample_to_chunk_index;
213 6cea494e Zdenek Kabelac
    long sample_size;
214
    long sample_count;
215
    long *sample_sizes;
216 5cd62665 Zdenek Kabelac
    int time_scale;
217 3ffe3793 François Revol
    long current_sample;
218
    long left_in_chunk; /* how many samples before next chunk */
219 0e7eed09 Fabrice Bellard
    /* specific MPEG4 header which is added at the beginning of the stream */
220
    int header_len;
221 5cd62665 Zdenek Kabelac
    MOV_esds_t esds;
222 0e7eed09 Fabrice Bellard
    uint8_t *header_data;
223 6cea494e Zdenek Kabelac
} MOVStreamContext;
224
225
typedef struct MOVContext {
226
    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) */
227
    AVFormatContext *fc;
228 5cd62665 Zdenek Kabelac
    int time_scale;
229
    int duration; /* duration of the longest track */
230 6cea494e Zdenek Kabelac
    int found_moov; /* when both 'moov' and 'mdat' sections has been found */
231
    int found_mdat; /* we suppose we have enough data to read the file */
232 0c1a9eda Zdenek Kabelac
    int64_t mdat_size;
233
    int64_t mdat_offset;
234 6cea494e Zdenek Kabelac
    int total_streams;
235
    /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
236
     * but we need the info to be able to skip data from those streams in the 'mdat' section
237
     */
238
    MOVStreamContext *streams[MAX_STREAMS];
239 5cd62665 Zdenek Kabelac
240 0c1a9eda Zdenek Kabelac
    int64_t next_chunk_offset;
241 5cd62665 Zdenek Kabelac
    MOVStreamContext *partial; /* != 0 : there is still to read in the current chunk */
242 b6a17df4 Zdenek Kabelac
    const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
243
    /* NOTE: for recursion save to/ restore from local variable! */
244 6cea494e Zdenek Kabelac
} MOVContext;
245
246
247
/* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
248
249
/* those functions parse an atom */
250
/* return code:
251
 1: found what I wanted, exit
252
 0: continue to parse next atom
253
 -1: error occured, exit
254
 */
255 b6a17df4 Zdenek Kabelac
typedef int (*mov_parse_function)(MOVContext *ctx,
256 6cea494e Zdenek Kabelac
                                  ByteIOContext *pb,
257 0c1a9eda Zdenek Kabelac
                                  uint32_t atom_type,
258
                                  int64_t atom_offset, /* after the size and type field (and eventually the extended size) */
259 b6a17df4 Zdenek Kabelac
                                  int64_t atom_size); /* total size (excluding the size and type fields) */
260 6cea494e Zdenek Kabelac
261
/* links atom IDs to parse functions */
262
typedef struct MOVParseTableEntry {
263 0c1a9eda Zdenek Kabelac
    uint32_t type;
264 6cea494e Zdenek Kabelac
    mov_parse_function func;
265
} MOVParseTableEntry;
266
267 b6a17df4 Zdenek Kabelac
static int parse_leaf(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
268 6cea494e Zdenek Kabelac
{
269
    print_atom("leaf", atom_type, atom_offset, atom_size);
270 b6a17df4 Zdenek Kabelac
271 6cea494e Zdenek Kabelac
    if(atom_size>1)
272
        url_fskip(pb, atom_size);
273
/*        url_seek(pb, atom_offset+atom_size, SEEK_SET); */
274
    return 0;
275
}
276
277 b6a17df4 Zdenek Kabelac
static int parse_default(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
278 6cea494e Zdenek Kabelac
{
279 0c1a9eda Zdenek Kabelac
    uint32_t type, foo=0;
280
    uint64_t offset, size;
281 b6a17df4 Zdenek Kabelac
    int64_t total_size = 0;
282 6cea494e Zdenek Kabelac
    int i;
283
    int err = 0;
284
    foo=0;
285
#ifdef DEBUG
286
    print_atom("default", atom_type, atom_offset, atom_size);
287
    debug_indent++;
288
#endif
289 5cd62665 Zdenek Kabelac
290 6cea494e Zdenek Kabelac
    offset = atom_offset;
291
292
    if(atom_size < 0)
293
        atom_size = 0x0FFFFFFFFFFFFFFF;
294 5cd62665 Zdenek Kabelac
    while(((total_size + 8) < atom_size) && !url_feof(pb) && !err) {
295 6cea494e Zdenek Kabelac
        size=atom_size;
296
        type=0L;
297
        if(atom_size >= 8) {
298 5cd62665 Zdenek Kabelac
            size = get_be32(pb);
299 6cea494e Zdenek Kabelac
            type = get_le32(pb);
300
        }
301 5cd62665 Zdenek Kabelac
        total_size += 8;
302
        offset += 8;
303
        //printf("type: %08x  %.4s  sz: %Lx  %Lx   %Lx\n", type, (char*)&type, size, atom_size, total_size);
304 6cea494e Zdenek Kabelac
        if(size == 1) { /* 64 bit extended size */
305 5cd62665 Zdenek Kabelac
            size = get_be64(pb) - 8;
306 6cea494e Zdenek Kabelac
            offset+=8;
307
            total_size+=8;
308
        }
309 5cd62665 Zdenek Kabelac
        if(size == 0) {
310
            size = atom_size - total_size;
311
            if (size <= 8)
312
                break;
313
        }
314 b6a17df4 Zdenek Kabelac
        for (i=0; c->parse_table[i].type != 0L && c->parse_table[i].type != type; i++)
315 5cd62665 Zdenek Kabelac
            /* empty */;
316
317
        size -= 8;
318 6cea494e Zdenek Kabelac
//        printf(" i=%ld\n", i);
319 b6a17df4 Zdenek Kabelac
        if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
320 6cea494e Zdenek Kabelac
//            url_seek(pb, atom_offset+atom_size, SEEK_SET);
321
#ifdef DEBUG
322
            print_atom("unknown", type, offset, size);
323
#endif
324
            url_fskip(pb, size);
325 b6a17df4 Zdenek Kabelac
        } else {
326
#ifdef DEBUG
327
            //char b[5] = { type & 0xff, (type >> 8) & 0xff, (type >> 16) & 0xff, (type >> 24) & 0xff, 0 };
328
            //print_atom(b, type, offset, size);
329
#endif
330
            err = (c->parse_table[i].func)(c, pb, type, offset, size);
331
        }
332 6cea494e Zdenek Kabelac
333
        offset+=size;
334
        total_size+=size;
335
    }
336
337 5cd62665 Zdenek Kabelac
    if (!err && total_size < atom_size && atom_size < 0x7ffff) {
338
        printf("RESET  %Ld  %Ld  err:%d\n", atom_size, total_size, err);
339
        url_fskip(pb, atom_size - total_size);
340
    }
341
342 6cea494e Zdenek Kabelac
#ifdef DEBUG
343
    debug_indent--;
344
#endif
345
    return err;
346
}
347
348 b6a17df4 Zdenek Kabelac
static int parse_ctab(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
349 5cd62665 Zdenek Kabelac
{
350
    url_fskip(pb, atom_size); // for now
351
    return 0;
352
}
353
354 b6a17df4 Zdenek Kabelac
static int parse_mvhd(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
355 6cea494e Zdenek Kabelac
{
356
    print_atom("mvhd", atom_type, atom_offset, atom_size);
357
358
    get_byte(pb); /* version */
359
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
360
361
    get_be32(pb); /* creation time */
362
    get_be32(pb); /* modification time */
363
    c->time_scale = get_be32(pb); /* time scale */
364 0147f198 François Revol
#ifdef DEBUG
365 5cd62665 Zdenek Kabelac
    printf("time scale = %i\n", c->time_scale);
366 0147f198 François Revol
#endif
367 5cd62665 Zdenek Kabelac
    c->duration = get_be32(pb); /* duration */
368 6cea494e Zdenek Kabelac
    get_be32(pb); /* preferred scale */
369 5cd62665 Zdenek Kabelac
370 6cea494e Zdenek Kabelac
    get_be16(pb); /* preferred volume */
371
372
    url_fskip(pb, 10); /* reserved */
373
374
    url_fskip(pb, 36); /* display matrix */
375
376
    get_be32(pb); /* preview time */
377
    get_be32(pb); /* preview duration */
378
    get_be32(pb); /* poster time */
379
    get_be32(pb); /* selection time */
380
    get_be32(pb); /* selection duration */
381
    get_be32(pb); /* current time */
382
    get_be32(pb); /* next track ID */
383
    
384
    return 0;
385
}
386
387
/* this atom should contain all header atoms */
388 b6a17df4 Zdenek Kabelac
static int parse_moov(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
389 6cea494e Zdenek Kabelac
{
390
    int err;
391 b6a17df4 Zdenek Kabelac
392 6cea494e Zdenek Kabelac
    print_atom("moov", atom_type, atom_offset, atom_size);
393
394 b6a17df4 Zdenek Kabelac
    err = parse_default(c, pb, atom_type, atom_offset, atom_size);
395 6cea494e Zdenek Kabelac
    /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
396
    /* so we don't parse the whole file if over a network */
397
    c->found_moov=1;
398
    if(c->found_mdat)
399
        return 1; /* found both, just go */
400
    return 0; /* now go for mdat */
401
}
402
403
/* this atom contains actual media data */
404 b6a17df4 Zdenek Kabelac
static int parse_mdat(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
405 6cea494e Zdenek Kabelac
{
406
    print_atom("mdat", atom_type, atom_offset, atom_size);
407
408
    if(atom_size == 0) /* wrong one (MP4) */
409
        return 0;
410
    c->found_mdat=1;
411
    c->mdat_offset = atom_offset;
412
    c->mdat_size = atom_size;
413
    if(c->found_moov)
414
        return 1; /* found both, just go */
415
    url_fskip(pb, atom_size);
416
    return 0; /* now go for moov */
417
}
418
419 3ffe3793 François Revol
/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
420
/* like the files created with Adobe Premiere 5.0, for samples see */
421
/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
422 b6a17df4 Zdenek Kabelac
static int parse_wide(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
423 3ffe3793 François Revol
{
424
    int err;
425 0c1a9eda Zdenek Kabelac
    uint32_t type;
426 b6a17df4 Zdenek Kabelac
427 3ffe3793 François Revol
#ifdef DEBUG
428
    print_atom("wide", atom_type, atom_offset, atom_size);
429
    debug_indent++;
430
#endif
431
    if (atom_size < 8)
432
        return 0; /* continue */
433
    if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
434
        url_fskip(pb, atom_size - 4);
435
        return 0;
436
    }
437
    type = get_le32(pb);
438
    if (type != MKTAG('m', 'd', 'a', 't')) {
439
        url_fskip(pb, atom_size - 8);
440
        return 0;
441
    }
442 b6a17df4 Zdenek Kabelac
    err = parse_mdat(c, pb, type, atom_offset + 8, atom_size - 8);
443 3ffe3793 François Revol
#ifdef DEBUG
444
    debug_indent--;
445
#endif
446
    return err;
447
}
448
449 b6a17df4 Zdenek Kabelac
static int parse_trak(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
450 6cea494e Zdenek Kabelac
{
451
    AVStream *st;
452
    MOVStreamContext *sc;
453 b6a17df4 Zdenek Kabelac
454 6cea494e Zdenek Kabelac
    print_atom("trak", atom_type, atom_offset, atom_size);
455
456 3ffe3793 François Revol
    st = av_new_stream(c->fc, c->fc->nb_streams);
457 1ea4f593 Fabrice Bellard
    if (!st) return -2;
458 b6a17df4 Zdenek Kabelac
    sc = (MOVStreamContext*) av_mallocz(sizeof(MOVStreamContext));
459
    if (!sc) {
460
        av_free(st);
461
        return -1;
462
    }
463
464 3ffe3793 François Revol
    sc->sample_to_chunk_index = -1;
465 6cea494e Zdenek Kabelac
    st->priv_data = sc;
466
    st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
467 5cd62665 Zdenek Kabelac
    st->time_length = (c->duration * 1000) / c->time_scale; // time in miliseconds
468 3ffe3793 François Revol
    c->streams[c->fc->nb_streams-1] = sc;
469 b6a17df4 Zdenek Kabelac
    return parse_default(c, pb, atom_type, atom_offset, atom_size);
470 6cea494e Zdenek Kabelac
}
471
472 b6a17df4 Zdenek Kabelac
static int parse_tkhd(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
473 6cea494e Zdenek Kabelac
{
474
    AVStream *st;
475 b6a17df4 Zdenek Kabelac
476 6cea494e Zdenek Kabelac
    print_atom("tkhd", atom_type, atom_offset, atom_size);
477
478
    st = c->fc->streams[c->fc->nb_streams-1];
479 5cd62665 Zdenek Kabelac
480 6cea494e Zdenek Kabelac
    get_byte(pb); /* version */
481
482
    get_byte(pb); get_byte(pb);
483
    get_byte(pb); /* flags */
484
    /*
485
    MOV_TRACK_ENABLED 0x0001
486
    MOV_TRACK_IN_MOVIE 0x0002
487
    MOV_TRACK_IN_PREVIEW 0x0004
488
    MOV_TRACK_IN_POSTER 0x0008
489
    */
490
491
    get_be32(pb); /* creation time */
492
    get_be32(pb); /* modification time */
493
    st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
494
    get_be32(pb); /* reserved */
495 5cd62665 Zdenek Kabelac
    st->time_length = get_be32(pb) * 1000 / c->time_scale; /* duration */
496 6cea494e Zdenek Kabelac
    get_be32(pb); /* reserved */
497
    get_be32(pb); /* reserved */
498 5cd62665 Zdenek Kabelac
499 6cea494e Zdenek Kabelac
    get_be16(pb); /* layer */
500
    get_be16(pb); /* alternate group */
501
    get_be16(pb); /* volume */
502
    get_be16(pb); /* reserved */
503
504
    url_fskip(pb, 36); /* display matrix */
505
506
    /* those are fixed-point */
507
    st->codec.width = get_be32(pb) >> 16; /* track width */
508
    st->codec.height = get_be32(pb) >> 16; /* track height */
509
    
510
    return 0;
511
}
512
513 b6a17df4 Zdenek Kabelac
static int parse_mdhd(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
514 0147f198 François Revol
{
515
    AVStream *st;
516 b6a17df4 Zdenek Kabelac
517 0147f198 François Revol
    print_atom("mdhd", atom_type, atom_offset, atom_size);
518
519
    st = c->fc->streams[c->fc->nb_streams-1];
520 5cd62665 Zdenek Kabelac
521 0147f198 François Revol
    get_byte(pb); /* version */
522
523
    get_byte(pb); get_byte(pb);
524
    get_byte(pb); /* flags */
525
526
    get_be32(pb); /* creation time */
527
    get_be32(pb); /* modification time */
528
529
    c->streams[c->total_streams]->time_scale = get_be32(pb);
530
531
#ifdef DEBUG
532 5cd62665 Zdenek Kabelac
    printf("track[%i].time_scale = %i\n", c->fc->nb_streams-1, c->streams[c->total_streams]->time_scale); /* time scale */
533 0147f198 François Revol
#endif
534
    get_be32(pb); /* duration */
535
536
    get_be16(pb); /* language */
537
    get_be16(pb); /* quality */
538 5cd62665 Zdenek Kabelac
539 0147f198 François Revol
    return 0;
540
}
541
542 b6a17df4 Zdenek Kabelac
static int parse_hdlr(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
543 6cea494e Zdenek Kabelac
{
544 3ffe3793 François Revol
    int len = 0;
545 b6a17df4 Zdenek Kabelac
    uint8_t *buf;
546 0c1a9eda Zdenek Kabelac
    uint32_t type;
547 6cea494e Zdenek Kabelac
    AVStream *st;
548 0c1a9eda Zdenek Kabelac
    uint32_t ctype;
549 b6a17df4 Zdenek Kabelac
550 6cea494e Zdenek Kabelac
    print_atom("hdlr", atom_type, atom_offset, atom_size);
551 b6a17df4 Zdenek Kabelac
552 6cea494e Zdenek Kabelac
    st = c->fc->streams[c->fc->nb_streams-1];
553
554
    get_byte(pb); /* version */
555
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
556
557
    /* component type */
558
    ctype = get_le32(pb);
559
    type = get_le32(pb); /* component subtype */
560
561
#ifdef DEBUG
562 684f44d9 Zdenek Kabelac
    printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
563 6cea494e Zdenek Kabelac
    printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
564
#endif
565
#ifdef DEBUG
566
/* XXX: yeah this is ugly... */
567
    if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
568
        if(type == MKTAG('v', 'i', 'd', 'e'))
569
            puts("hdlr: vide");
570
        else if(type == MKTAG('s', 'o', 'u', 'n'))
571
            puts("hdlr: soun");
572
    } else if(ctype == 0) { /* MP4 */
573
        if(type == MKTAG('v', 'i', 'd', 'e'))
574
            puts("hdlr: vide");
575
        else if(type == MKTAG('s', 'o', 'u', 'n'))
576
            puts("hdlr: soun");
577
        else if(type == MKTAG('o', 'd', 's', 'm'))
578
            puts("hdlr: odsm");
579
        else if(type == MKTAG('s', 'd', 's', 'm'))
580
            puts("hdlr: sdsm");
581
    } else puts("hdlr: meta");
582
#endif
583
584
    if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
585 0147f198 François Revol
        /* helps parsing the string hereafter... */
586
        c->mp4 = 0;
587 6cea494e Zdenek Kabelac
        if(type == MKTAG('v', 'i', 'd', 'e'))
588
            st->codec.codec_type = CODEC_TYPE_VIDEO;
589
        else if(type == MKTAG('s', 'o', 'u', 'n'))
590
            st->codec.codec_type = CODEC_TYPE_AUDIO;
591
    } else if(ctype == 0) { /* MP4 */
592 0147f198 François Revol
        /* helps parsing the string hereafter... */
593
        c->mp4 = 1;
594 6cea494e Zdenek Kabelac
        if(type == MKTAG('v', 'i', 'd', 'e'))
595
            st->codec.codec_type = CODEC_TYPE_VIDEO;
596
        else if(type == MKTAG('s', 'o', 'u', 'n'))
597
            st->codec.codec_type = CODEC_TYPE_AUDIO;
598
    }
599
    get_be32(pb); /* component  manufacture */
600
    get_be32(pb); /* component flags */
601
    get_be32(pb); /* component flags mask */
602
603
    if(atom_size <= 24)
604
        return 0; /* nothing left to read */
605
    /* XXX: MP4 uses a C string, not a pascal one */
606
    /* component name */
607 0147f198 François Revol
608
    if(c->mp4) {
609
        /* .mp4: C string */
610
        while(get_byte(pb) && (++len < (atom_size - 24)));
611
    } else {
612
        /* .mov: PASCAL string */
613
        len = get_byte(pb);
614 b6a17df4 Zdenek Kabelac
        buf = (uint8_t*) av_malloc(len+1);
615
        if (buf) {
616
            get_buffer(pb, buf, len);
617 0147f198 François Revol
#ifdef DEBUG
618 b6a17df4 Zdenek Kabelac
            buf[len] = '\0';
619
            printf("**buf='%s'\n", buf);
620 0147f198 François Revol
#endif
621 b6a17df4 Zdenek Kabelac
            av_free(buf);
622
        } else
623
            url_fskip(pb, len);
624 0147f198 François Revol
    }
625
#if 0
626 c9a65ca8 Fabrice Bellard
    len = get_byte(pb);
627
    /* XXX: use a better heuristic */
628
    if(len < 32) {
629
        /* assume that it is a Pascal like string */
630 b6a17df4 Zdenek Kabelac
        buf = av_malloc(len+1);
631
        if (buf) {
632
            get_buffer(pb, buf, len);
633
            buf[len] = '\0';
634 6cea494e Zdenek Kabelac
#ifdef DEBUG
635 b6a17df4 Zdenek Kabelac
            printf("**buf='%s'\n", buf);
636 6cea494e Zdenek Kabelac
#endif
637 b6a17df4 Zdenek Kabelac
            av_free(buf);
638
        } else
639
            url_fskip(pb, len)l
640 6cea494e Zdenek Kabelac
    } else {
641 c9a65ca8 Fabrice Bellard
        /* MP4 string */
642
        for(;;) {
643
            if (len == 0)
644
                break;
645
            len = get_byte(pb);
646 6cea494e Zdenek Kabelac
        }
647
    }
648 0147f198 François Revol
#endif
649 5cd62665 Zdenek Kabelac
650 6cea494e Zdenek Kabelac
    return 0;
651
}
652
653 0e7eed09 Fabrice Bellard
static int mp4_read_descr_len(ByteIOContext *pb)
654
{
655 5cd62665 Zdenek Kabelac
    int len = 0;
656
    int count = 0;
657 0e7eed09 Fabrice Bellard
    for(;;) {
658 5cd62665 Zdenek Kabelac
        int c = get_byte(pb);
659
        len = (len << 7) | (c & 0x7f);
660 0e7eed09 Fabrice Bellard
        if ((c & 0x80) == 0)
661
            break;
662
        if (++count == 4)
663
            break;
664
    }
665
    return len;
666
}
667
668
static int mp4_read_descr(ByteIOContext *pb, int *tag)
669
{
670
    int len;
671
    *tag = get_byte(pb);
672
    len = mp4_read_descr_len(pb);
673
#ifdef DEBUG
674
    printf("MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
675
#endif
676
    return len;
677
}
678
679 5cd62665 Zdenek Kabelac
static inline unsigned int get_be24(ByteIOContext *s)
680
{
681
    unsigned int val;
682
    val = get_byte(s) << 16;
683
    val |= get_byte(s) << 8;
684
    val |= get_byte(s);
685
    return val;
686
}
687
688 b6a17df4 Zdenek Kabelac
static int parse_esds(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
689 5cd62665 Zdenek Kabelac
{
690
691
    int64_t start_pos = url_ftell(pb);
692
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
693
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
694
    int tag, len;
695 b6a17df4 Zdenek Kabelac
696 5cd62665 Zdenek Kabelac
    print_atom("esds", atom_type, atom_offset, atom_size);
697
698
    /* Well, broken but suffisant for some MP4 streams */
699
    get_be32(pb); /* version + flags */
700
    len = mp4_read_descr(pb, &tag);
701
    if (tag == MP4ESDescrTag) {
702
        get_be16(pb); /* ID */
703
        get_byte(pb); /* priority */
704
    } else
705
        get_be16(pb); /* ID */
706
707
    len = mp4_read_descr(pb, &tag);
708
    if (tag == MP4DecConfigDescrTag) {
709
        sc->esds.object_type_id = get_byte(pb);
710
        sc->esds.stream_type = get_be24(pb);
711
        sc->esds.max_bitrate = get_be32(pb);
712
        sc->esds.avg_bitrate = get_be32(pb);
713
714
        len = mp4_read_descr(pb, &tag);
715
        printf("LEN %d  TAG %d  m:%d a:%d\n", len, tag, sc->esds.max_bitrate, sc->esds.avg_bitrate);
716
        if (tag == MP4DecSpecificDescrTag) {
717
#ifdef DEBUG
718
            printf("Specific MPEG4 header len=%d\n", len);
719
#endif
720 b6a17df4 Zdenek Kabelac
            sc->header_data = (uint8_t*) av_mallocz(len);
721 5cd62665 Zdenek Kabelac
            if (sc->header_data) {
722
                get_buffer(pb, sc->header_data, len);
723
                sc->header_len = len;
724
            }
725
        }
726
    }
727
    /* in any case, skip garbage */
728
    url_fskip(pb, (atom_size - 8) - ((url_ftell(pb) - start_pos)));
729
    return 0;
730
}
731
732 b6a17df4 Zdenek Kabelac
static int parse_stsd(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
733 6cea494e Zdenek Kabelac
{
734 b6a17df4 Zdenek Kabelac
    int entries, size, frames_per_sample;
735 0c1a9eda Zdenek Kabelac
    uint32_t format;
736 6cea494e Zdenek Kabelac
    AVStream *st;
737 0e7eed09 Fabrice Bellard
    MOVStreamContext *sc;
738 b6a17df4 Zdenek Kabelac
739 6cea494e Zdenek Kabelac
    print_atom("stsd", atom_type, atom_offset, atom_size);
740 b6a17df4 Zdenek Kabelac
741 6cea494e Zdenek Kabelac
    st = c->fc->streams[c->fc->nb_streams-1];
742 0e7eed09 Fabrice Bellard
    sc = (MOVStreamContext *)st->priv_data;
743 6cea494e Zdenek Kabelac
744
    get_byte(pb); /* version */
745
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
746
747
    entries = get_be32(pb);
748
749
    while(entries--) {
750 b6a17df4 Zdenek Kabelac
        enum CodecID id;
751 5cd62665 Zdenek Kabelac
        size = get_be32(pb); /* size */
752 6cea494e Zdenek Kabelac
        format = get_le32(pb); /* data format */
753 5cd62665 Zdenek Kabelac
754 6cea494e Zdenek Kabelac
        get_be32(pb); /* reserved */
755
        get_be16(pb); /* reserved */
756
        get_be16(pb); /* index */
757 0e7eed09 Fabrice Bellard
758
        /* for MPEG4: set codec type by looking for it */
759
        id = codec_get_id(mov_video_tags, format);
760
        if (id >= 0) {
761
            AVCodec *codec;
762 b6a17df4 Zdenek Kabelac
            codec = avcodec_find_decoder(id);
763 0e7eed09 Fabrice Bellard
            if (codec)
764 b6a17df4 Zdenek Kabelac
                st->codec.codec_type = codec->type;
765 0e7eed09 Fabrice Bellard
        }
766
#ifdef DEBUG
767 5cd62665 Zdenek Kabelac
        printf("size=%d 4CC= %c%c%c%c codec_type=%d\n",
768
               size,
769 0e7eed09 Fabrice Bellard
               (format >> 0) & 0xff,
770
               (format >> 8) & 0xff,
771
               (format >> 16) & 0xff,
772
               (format >> 24) & 0xff,
773
               st->codec.codec_type);
774
#endif
775 5cd62665 Zdenek Kabelac
        st->codec.codec_tag = format;
776 6cea494e Zdenek Kabelac
        if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
777
            st->codec.codec_id = codec_get_id(mov_video_tags, format);
778
            get_be16(pb); /* version */
779
            get_be16(pb); /* revision level */
780
            get_be32(pb); /* vendor */
781
            get_be32(pb); /* temporal quality */
782
            get_be32(pb); /* spacial quality */
783
            st->codec.width = get_be16(pb); /* width */
784
            st->codec.height = get_be16(pb); /* height */
785 0e7eed09 Fabrice Bellard
#if 1
786
            if (st->codec.codec_id == CODEC_ID_MPEG4) {
787
                /* in some MPEG4 the width/height are not correct, so
788
                   we ignore this info */
789
                st->codec.width = 0;
790
                st->codec.height = 0;
791
            }
792
#endif
793 6cea494e Zdenek Kabelac
            get_be32(pb); /* horiz resolution */
794
            get_be32(pb); /* vert resolution */
795
            get_be32(pb); /* data size, always 0 */
796 5cd62665 Zdenek Kabelac
            frames_per_sample = get_be16(pb); /* frames per samples */
797 6cea494e Zdenek Kabelac
#ifdef DEBUG
798 684f44d9 Zdenek Kabelac
            printf("frames/samples = %d\n", frames_per_sample);
799 6cea494e Zdenek Kabelac
#endif
800 b6a17df4 Zdenek Kabelac
            get_buffer(pb, (uint8_t *)st->codec.codec_name, 32); /* codec name */
801 5cd62665 Zdenek Kabelac
802
            st->codec.bits_per_sample = get_be16(pb); /* depth */
803
            st->codec.color_table_id = get_be16(pb); /* colortable id */
804 6cea494e Zdenek Kabelac
805 14bea432 Michael Niedermayer
            st->codec.frame_rate      = 25;
806
            st->codec.frame_rate_base = 1;
807 5cd62665 Zdenek Kabelac
808 0e7eed09 Fabrice Bellard
            size -= (16+8*4+2+32+2*2);
809 5cd62665 Zdenek Kabelac
#if 0
810
            while (size >= 8) {
811 0e7eed09 Fabrice Bellard
                int atom_size, atom_type;
812 0c1a9eda Zdenek Kabelac
                int64_t start_pos;
813 5cd62665 Zdenek Kabelac

814 0e7eed09 Fabrice Bellard
                atom_size = get_be32(pb);
815
                atom_type = get_le32(pb);
816 5cd62665 Zdenek Kabelac
                size -= 8;
817
                printf("NEWSIZE %d\n", size);
818 0e7eed09 Fabrice Bellard
#ifdef DEBUG
819 5cd62665 Zdenek Kabelac
                printf("VIDEO: atom_type=%c%c%c%c atom_size=%d size_left=%d\n",
820 0e7eed09 Fabrice Bellard
                       (atom_type >> 0) & 0xff,
821
                       (atom_type >> 8) & 0xff,
822
                       (atom_type >> 16) & 0xff,
823
                       (atom_type >> 24) & 0xff,
824
                       atom_size, size);
825
#endif
826
                start_pos = url_ftell(pb);
827
828
                switch(atom_type) {
829
                case MKTAG('e', 's', 'd', 's'):
830
                    {
831
                        int tag, len;
832
                        /* Well, broken but suffisant for some MP4 streams */
833
                        get_be32(pb); /* version + flags */
834
                        len = mp4_read_descr(pb, &tag);
835
                        if (tag == 0x03) {
836
                            /* MP4ESDescrTag */
837
                            get_be16(pb); /* ID */
838
                            get_byte(pb); /* priority */
839
                            len = mp4_read_descr(pb, &tag);
840
                            if (tag != 0x04)
841
                                goto fail;
842
                            /* MP4DecConfigDescrTag */
843
                            get_byte(pb); /* objectTypeId */
844
                            get_be32(pb); /* streamType + buffer size */
845 5cd62665 Zdenek Kabelac
                            get_be32(pb); /* max bit rate */
846 0e7eed09 Fabrice Bellard
                            get_be32(pb); /* avg bit rate */
847
                            len = mp4_read_descr(pb, &tag);
848
                            if (tag != 0x05)
849
                                goto fail;
850
                            /* MP4DecSpecificDescrTag */
851
#ifdef DEBUG
852
                            printf("Specific MPEG4 header len=%d\n", len);
853
#endif
854
                            sc->header_data = av_mallocz(len);
855
                            if (sc->header_data) {
856
                                get_buffer(pb, sc->header_data, len);
857 5cd62665 Zdenek Kabelac
                                sc->header_len = len;
858 0e7eed09 Fabrice Bellard
                            }
859
                        }
860
                        /* in any case, skip garbage */
861
                    }
862
                    break;
863
                default:
864
                    break;
865
                }
866 5cd62665 Zdenek Kabelac
            fail:
867
                printf("ATOMENEWSIZE %d   %d\n", atom_size, url_ftell(pb) - start_pos);
868
                if (atom_size > 8) {
869
                    url_fskip(pb, (atom_size - 8) -
870
                              ((url_ftell(pb) - start_pos)));
871
                    size -= atom_size - 8;
872
                    printf("NEWSIZE %d\n", size);
873
                }
874
            }
875 0e7eed09 Fabrice Bellard
            if (size > 0) {
876
                /* unknown extension */
877
                url_fskip(pb, size);
878
            }
879 5cd62665 Zdenek Kabelac
#else
880 b6a17df4 Zdenek Kabelac
            parse_default(c, pb, 0L, 0LL, size);
881 5cd62665 Zdenek Kabelac
#endif
882
        } else {
883 6cea494e Zdenek Kabelac
            get_be16(pb); /* version */
884
            get_be16(pb); /* revision level */
885
            get_be32(pb); /* vendor */
886
887
            st->codec.channels = get_be16(pb);/* channel count */
888 5cd62665 Zdenek Kabelac
            st->codec.bits_per_sample = get_be16(pb); /* sample size */
889 6cea494e Zdenek Kabelac
890 5cd62665 Zdenek Kabelac
            st->codec.codec_id = codec_get_id(mov_audio_tags, format);
891
            /* handle specific s8 codec */
892 6cea494e Zdenek Kabelac
            get_be16(pb); /* compression id = 0*/
893
            get_be16(pb); /* packet size = 0 */
894 5cd62665 Zdenek Kabelac
895 6cea494e Zdenek Kabelac
            st->codec.sample_rate = ((get_be32(pb) >> 16));
896 b6a17df4 Zdenek Kabelac
            printf("CODECID %d  %d  %.4s\n", st->codec.codec_id, CODEC_ID_PCM_S16BE, (char*)&format);
897 6cea494e Zdenek Kabelac
898 b6a17df4 Zdenek Kabelac
            switch (st->codec.codec_id) {
899
            case CODEC_ID_PCM_S16BE:
900 5cd62665 Zdenek Kabelac
                if (st->codec.bits_per_sample == 8)
901
                    st->codec.codec_id = CODEC_ID_PCM_S8;
902 b6a17df4 Zdenek Kabelac
                /* fall */
903
            case CODEC_ID_PCM_U8:
904
                st->codec.bit_rate = st->codec.sample_rate * 8;
905
                break;
906
            default:
907
                ;
908 5cd62665 Zdenek Kabelac
            }
909
            get_be32(pb); /* samples per packet */
910
            get_be32(pb); /* bytes per packet */
911
            get_be32(pb); /* bytes per frame */
912
            get_be32(pb); /* bytes per sample */
913
914
            //if (size > 16) url_fskip(pb, size-(16+20));
915
#if 1
916
            if (size >= 44 + 8) {
917
                int fcc;
918
                st->codec.extradata_size = get_be32(pb) - 8;
919
                fcc = get_le32(pb); // evaw
920
                //printf("%x  %.4s  %d\n", fcc, (char*)&fcc, st->codec.extradata_size);
921
                st->codec.extradata = av_mallocz(st->codec.extradata_size);
922 b6a17df4 Zdenek Kabelac
                if (st->codec.extradata)
923
                    get_buffer(pb, st->codec.extradata, st->codec.extradata_size); // FIXME url_fskip
924 5cd62665 Zdenek Kabelac
                url_fskip(pb, size-(16 + 20 + 16 + 8 + st->codec.extradata_size));
925
            }
926
            else
927
                url_fskip(pb, size-(16 + 20 + 16));
928
#else
929 b6a17df4 Zdenek Kabelac
            parse_default(c, pb, 0L, 0LL, size - (16 + 20 + 16 + 8));
930 5cd62665 Zdenek Kabelac
#endif
931 6cea494e Zdenek Kabelac
        }
932
    }
933
/*
934
    if(len) {
935 1ea4f593 Fabrice Bellard
    buf = av_malloc(len+1);
936 6cea494e Zdenek Kabelac
        get_buffer(pb, buf, len);
937
        buf[len] = '\0';
938
        puts(buf);
939 1ea4f593 Fabrice Bellard
        av_free(buf);
940 6cea494e Zdenek Kabelac
    }
941
*/
942
    return 0;
943
}
944
945 b6a17df4 Zdenek Kabelac
static int parse_stco(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
946 6cea494e Zdenek Kabelac
{
947
    int entries, i;
948
    AVStream *st;
949
    MOVStreamContext *sc;
950 b6a17df4 Zdenek Kabelac
951 6cea494e Zdenek Kabelac
    print_atom("stco", atom_type, atom_offset, atom_size);
952 b6a17df4 Zdenek Kabelac
953 6cea494e Zdenek Kabelac
    st = c->fc->streams[c->fc->nb_streams-1];
954
    sc = (MOVStreamContext *)st->priv_data;
955 5cd62665 Zdenek Kabelac
956 6cea494e Zdenek Kabelac
    get_byte(pb); /* version */
957
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
958
959
    entries = get_be32(pb);
960
    sc->chunk_count = entries;
961 b6a17df4 Zdenek Kabelac
    sc->chunk_offsets = (int64_t*) av_malloc(entries * sizeof(int64_t));
962
    if (!sc->chunk_offsets)
963
        return -1;
964 6cea494e Zdenek Kabelac
    if(atom_type == MKTAG('s', 't', 'c', 'o')) {
965
        for(i=0; i<entries; i++) {
966
            sc->chunk_offsets[i] = get_be32(pb);
967
        }
968
    } else if(atom_type == MKTAG('c', 'o', '6', '4')) {
969
        for(i=0; i<entries; i++) {
970
            sc->chunk_offsets[i] = get_be64(pb);
971
        }
972
    } else
973
        return -1;
974 0e7eed09 Fabrice Bellard
#ifdef DEBUG
975 3f95e843 François Revol
/*
976 0e7eed09 Fabrice Bellard
    for(i=0; i<entries; i++) {
977
        printf("chunk offset=0x%Lx\n", sc->chunk_offsets[i]);
978
    }
979 3f95e843 François Revol
*/
980 0e7eed09 Fabrice Bellard
#endif
981 6cea494e Zdenek Kabelac
    return 0;
982
}
983
984 b6a17df4 Zdenek Kabelac
static int parse_stsc(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
985 6cea494e Zdenek Kabelac
{
986
    int entries, i;
987
    AVStream *st;
988
    MOVStreamContext *sc;
989 b6a17df4 Zdenek Kabelac
990 6cea494e Zdenek Kabelac
    print_atom("stsc", atom_type, atom_offset, atom_size);
991 b6a17df4 Zdenek Kabelac
992 6cea494e Zdenek Kabelac
    st = c->fc->streams[c->fc->nb_streams-1];
993
    sc = (MOVStreamContext *)st->priv_data;
994 5cd62665 Zdenek Kabelac
995 6cea494e Zdenek Kabelac
    get_byte(pb); /* version */
996
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
997
998
    entries = get_be32(pb);
999 3ffe3793 François Revol
#ifdef DEBUG
1000
printf("track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
1001
#endif
1002 6cea494e Zdenek Kabelac
    sc->sample_to_chunk_sz = entries;
1003 b6a17df4 Zdenek Kabelac
    sc->sample_to_chunk = (MOV_sample_to_chunk_tbl*) av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
1004
    if (!sc->sample_to_chunk)
1005
        return -1;
1006 6cea494e Zdenek Kabelac
    for(i=0; i<entries; i++) {
1007
        sc->sample_to_chunk[i].first = get_be32(pb);
1008
        sc->sample_to_chunk[i].count = get_be32(pb);
1009
        sc->sample_to_chunk[i].id = get_be32(pb);
1010
#ifdef DEBUG
1011
/*        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); */
1012
#endif
1013
    }
1014
    return 0;
1015
}
1016
1017 b6a17df4 Zdenek Kabelac
static int parse_stsz(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
1018 6cea494e Zdenek Kabelac
{
1019
    int entries, i;
1020
    AVStream *st;
1021
    MOVStreamContext *sc;
1022 b6a17df4 Zdenek Kabelac
1023 6cea494e Zdenek Kabelac
    print_atom("stsz", atom_type, atom_offset, atom_size);
1024 b6a17df4 Zdenek Kabelac
1025 6cea494e Zdenek Kabelac
    st = c->fc->streams[c->fc->nb_streams-1];
1026
    sc = (MOVStreamContext *)st->priv_data;
1027 5cd62665 Zdenek Kabelac
1028 6cea494e Zdenek Kabelac
    get_byte(pb); /* version */
1029
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1030 5cd62665 Zdenek Kabelac
1031 6cea494e Zdenek Kabelac
    sc->sample_size = get_be32(pb);
1032
    entries = get_be32(pb);
1033
    sc->sample_count = entries;
1034 0147f198 François Revol
#ifdef DEBUG
1035 6cea494e Zdenek Kabelac
    printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
1036 0147f198 François Revol
#endif
1037 6cea494e Zdenek Kabelac
    if(sc->sample_size)
1038
        return 0; /* there isn't any table following */
1039 b6a17df4 Zdenek Kabelac
    sc->sample_sizes = (long*) av_malloc(entries * sizeof(long));
1040
    if (!sc->sample_sizes)
1041
        return -1;
1042 6cea494e Zdenek Kabelac
    for(i=0; i<entries; i++) {
1043
        sc->sample_sizes[i] = get_be32(pb);
1044
#ifdef DEBUG
1045
/*        printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
1046
#endif
1047
    }
1048
    return 0;
1049
}
1050
1051 b6a17df4 Zdenek Kabelac
static int parse_stts(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
1052 0147f198 François Revol
{
1053
    int entries, i;
1054
    AVStream *st;
1055
    MOVStreamContext *sc;
1056 b6a17df4 Zdenek Kabelac
1057 0147f198 François Revol
    print_atom("stts", atom_type, atom_offset, atom_size);
1058 b6a17df4 Zdenek Kabelac
1059 0147f198 François Revol
    st = c->fc->streams[c->fc->nb_streams-1];
1060
    sc = (MOVStreamContext *)st->priv_data;
1061
1062
    get_byte(pb); /* version */
1063
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1064
    entries = get_be32(pb);
1065 3ffe3793 François Revol
#ifdef DEBUG
1066
printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
1067
#endif
1068 0147f198 François Revol
    for(i=0; i<entries; i++) {
1069
        int sample_duration;
1070
1071
        get_be32(pb);
1072
        sample_duration = get_be32(pb);
1073
1074
        if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
1075 14bea432 Michael Niedermayer
            st->codec.frame_rate_base = sample_duration ? sample_duration : 1;
1076
            st->codec.frame_rate = c->streams[c->total_streams]->time_scale;
1077 0147f198 François Revol
#ifdef DEBUG
1078 3ffe3793 François Revol
            printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
1079 0147f198 François Revol
#endif
1080
        }
1081
    }
1082
    return 0;
1083
}
1084
1085
#ifdef CONFIG_ZLIB
1086 0c1a9eda Zdenek Kabelac
static int null_read_packet(void *opaque, uint8_t *buf, int buf_size)
1087 0147f198 François Revol
{
1088
    return -1;
1089
}
1090
1091 b6a17df4 Zdenek Kabelac
static int parse_cmov(MOVContext *c, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size)
1092 0147f198 François Revol
{
1093
    ByteIOContext ctx;
1094 b6a17df4 Zdenek Kabelac
    uint8_t *cmov_data;
1095
    uint8_t *moov_data; /* uncompressed data */
1096 0147f198 François Revol
    long cmov_len, moov_len;
1097
    int ret;
1098 b6a17df4 Zdenek Kabelac
1099 0147f198 François Revol
    print_atom("cmov", atom_type, atom_offset, atom_size);
1100
1101
    get_be32(pb); /* dcom atom */
1102
    if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
1103
        return -1;
1104
    if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
1105
        puts("unknown compression for cmov atom !");
1106
        return -1;
1107
    }
1108
    get_be32(pb); /* cmvd atom */
1109
    if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
1110
        return -1;
1111
    moov_len = get_be32(pb); /* uncompressed size */
1112
    cmov_len = atom_size - 6 * 4;
1113 5cd62665 Zdenek Kabelac
1114 b6a17df4 Zdenek Kabelac
    cmov_data = (uint8_t *) av_malloc(cmov_len);
1115 0147f198 François Revol
    if (!cmov_data)
1116
        return -1;
1117 b6a17df4 Zdenek Kabelac
    moov_data = (uint8_t *) av_malloc(moov_len);
1118 0147f198 François Revol
    if (!moov_data) {
1119
        av_free(cmov_data);
1120
        return -1;
1121
    }
1122
    get_buffer(pb, cmov_data, cmov_len);
1123 b6a17df4 Zdenek Kabelac
    if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
1124 0147f198 François Revol
        return -1;
1125
    if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
1126
        return -1;
1127
    ctx.buf_end = ctx.buffer + moov_len;
1128 b6a17df4 Zdenek Kabelac
    ret = parse_default(c, &ctx, MKTAG( 'm', 'o', 'o', 'v' ), 0, moov_len);
1129 0147f198 François Revol
    av_free(moov_data);
1130
    av_free(cmov_data);
1131
    return ret;
1132
}
1133
#endif
1134
1135 6cea494e Zdenek Kabelac
static const MOVParseTableEntry mov_default_parse_table[] = {
1136
/* mp4 atoms */
1137
{ MKTAG( 'm', 'p', '4', 'a' ), parse_default },
1138
{ MKTAG( 'c', 'o', '6', '4' ), parse_stco },
1139
{ MKTAG( 's', 't', 'c', 'o' ), parse_stco },
1140
{ MKTAG( 'c', 'r', 'h', 'd' ), parse_default },
1141
{ MKTAG( 'c', 't', 't', 's' ), parse_leaf },
1142
{ MKTAG( 'c', 'p', 'r', 't' ), parse_default },
1143
{ MKTAG( 'u', 'r', 'l', ' ' ), parse_leaf },
1144
{ MKTAG( 'u', 'r', 'n', ' ' ), parse_leaf },
1145
{ MKTAG( 'd', 'i', 'n', 'f' ), parse_default },
1146
{ MKTAG( 'd', 'r', 'e', 'f' ), parse_leaf },
1147
{ MKTAG( 's', 't', 'd', 'p' ), parse_default },
1148
{ MKTAG( 'e', 'd', 't', 's' ), parse_default },
1149
{ MKTAG( 'e', 'l', 's', 't' ), parse_leaf },
1150
{ MKTAG( 'u', 'u', 'i', 'd' ), parse_default },
1151
{ MKTAG( 'f', 'r', 'e', 'e' ), parse_leaf },
1152
{ MKTAG( 'h', 'd', 'l', 'r' ), parse_hdlr },
1153 3ffe3793 François Revol
{ MKTAG( 'h', 'm', 'h', 'd' ), parse_leaf },
1154 6cea494e Zdenek Kabelac
{ MKTAG( 'h', 'i', 'n', 't' ), parse_leaf },
1155
{ MKTAG( 'n', 'm', 'h', 'd' ), parse_leaf },
1156
{ MKTAG( 'm', 'p', '4', 's' ), parse_default },
1157
{ MKTAG( 'm', 'd', 'i', 'a' ), parse_default },
1158
{ MKTAG( 'm', 'd', 'a', 't' ), parse_mdat },
1159 0147f198 François Revol
{ MKTAG( 'm', 'd', 'h', 'd' ), parse_mdhd },
1160 6cea494e Zdenek Kabelac
{ MKTAG( 'm', 'i', 'n', 'f' ), parse_default },
1161
{ MKTAG( 'm', 'o', 'o', 'v' ), parse_moov },
1162
{ MKTAG( 'm', 'v', 'h', 'd' ), parse_mvhd },
1163
{ MKTAG( 'i', 'o', 'd', 's' ), parse_leaf },
1164
{ MKTAG( 'o', 'd', 'h', 'd' ), parse_default },
1165
{ MKTAG( 'm', 'p', 'o', 'd' ), parse_leaf },
1166
{ MKTAG( 's', 't', 's', 'd' ), parse_stsd },
1167
{ MKTAG( 's', 't', 's', 'z' ), parse_stsz },
1168
{ MKTAG( 's', 't', 'b', 'l' ), parse_default },
1169
{ MKTAG( 's', 't', 's', 'c' ), parse_stsc },
1170
{ MKTAG( 's', 'd', 'h', 'd' ), parse_default },
1171
{ MKTAG( 's', 't', 's', 'h' ), parse_default },
1172
{ MKTAG( 's', 'k', 'i', 'p' ), parse_default },
1173
{ MKTAG( 's', 'm', 'h', 'd' ), parse_leaf },
1174
{ MKTAG( 'd', 'p', 'n', 'd' ), parse_leaf },
1175
{ MKTAG( 's', 't', 's', 's' ), parse_leaf },
1176 0147f198 François Revol
{ MKTAG( 's', 't', 't', 's' ), parse_stts },
1177 6cea494e Zdenek Kabelac
{ MKTAG( 't', 'r', 'a', 'k' ), parse_trak },
1178
{ MKTAG( 't', 'k', 'h', 'd' ), parse_tkhd },
1179
{ MKTAG( 't', 'r', 'e', 'f' ), parse_default }, /* not really */
1180
{ MKTAG( 'u', 'd', 't', 'a' ), parse_leaf },
1181
{ MKTAG( 'v', 'm', 'h', 'd' ), parse_leaf },
1182
{ MKTAG( 'm', 'p', '4', 'v' ), parse_default },
1183
/* extra mp4 */
1184
{ MKTAG( 'M', 'D', 'E', 'S' ), parse_leaf },
1185
/* QT atoms */
1186
{ MKTAG( 'c', 'h', 'a', 'p' ), parse_leaf },
1187
{ MKTAG( 'c', 'l', 'i', 'p' ), parse_default },
1188
{ MKTAG( 'c', 'r', 'g', 'n' ), parse_leaf },
1189
{ MKTAG( 'k', 'm', 'a', 't' ), parse_leaf },
1190
{ MKTAG( 'm', 'a', 't', 't' ), parse_default },
1191
{ MKTAG( 'r', 'd', 'r', 'f' ), parse_leaf },
1192
{ MKTAG( 'r', 'm', 'd', 'a' ), parse_default },
1193
{ MKTAG( 'r', 'm', 'd', 'r' ), parse_leaf },
1194
//{ MKTAG( 'r', 'm', 'q', 'u' ), parse_leaf },
1195
{ MKTAG( 'r', 'm', 'r', 'a' ), parse_default },
1196
{ MKTAG( 's', 'c', 'p', 't' ), parse_leaf },
1197
{ MKTAG( 's', 'y', 'n', 'c' ), parse_leaf },
1198
{ MKTAG( 's', 's', 'r', 'c' ), parse_leaf },
1199
{ MKTAG( 't', 'c', 'm', 'd' ), parse_leaf },
1200 3ffe3793 François Revol
{ MKTAG( 'w', 'i', 'd', 'e' ), parse_wide }, /* place holder */
1201 5cd62665 Zdenek Kabelac
{ MKTAG( 'c', 't', 'a', 'b' ), parse_ctab },
1202
{ MKTAG( 'e', 's', 'd', 's' ), parse_esds },
1203 0147f198 François Revol
#ifdef CONFIG_ZLIB
1204
{ MKTAG( 'c', 'm', 'o', 'v' ), parse_cmov },
1205
#else
1206
{ MKTAG( 'c', 'm', 'o', 'v' ), parse_leaf },
1207
#endif
1208 6cea494e Zdenek Kabelac
{ 0L, parse_leaf }
1209
};
1210
1211
static void mov_free_stream_context(MOVStreamContext *sc)
1212
{
1213
    if(sc) {
1214 1ea4f593 Fabrice Bellard
        av_free(sc->chunk_offsets);
1215
        av_free(sc->sample_to_chunk);
1216 5cd62665 Zdenek Kabelac
        av_free(sc->sample_sizes);
1217 0e7eed09 Fabrice Bellard
        av_free(sc->header_data);
1218 1ea4f593 Fabrice Bellard
        av_free(sc);
1219 6cea494e Zdenek Kabelac
    }
1220
}
1221
1222 5cd62665 Zdenek Kabelac
static inline uint32_t to_tag(uint8_t *buf)
1223 0e7eed09 Fabrice Bellard
{
1224
    return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
1225
}
1226
1227 5cd62665 Zdenek Kabelac
static inline uint32_t to_be32(uint8_t *buf)
1228 0e7eed09 Fabrice Bellard
{
1229
    return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
1230
}
1231
1232 b6a17df4 Zdenek Kabelac
/* XXX: is it sufficient ? */
1233 c9a65ca8 Fabrice Bellard
static int mov_probe(AVProbeData *p)
1234
{
1235 0e7eed09 Fabrice Bellard
    unsigned int offset;
1236
    uint32_t tag;
1237 3ffe3793 François Revol
1238 c9a65ca8 Fabrice Bellard
    /* check file header */
1239
    if (p->buf_size <= 12)
1240
        return 0;
1241 0e7eed09 Fabrice Bellard
    offset = 0;
1242
    for(;;) {
1243
        /* ignore invalid offset */
1244
        if ((offset + 8) > (unsigned int)p->buf_size)
1245 3ffe3793 François Revol
            return 0;
1246 0e7eed09 Fabrice Bellard
        tag = to_tag(p->buf + offset + 4);
1247
        switch(tag) {
1248
        case MKTAG( 'm', 'o', 'o', 'v' ):
1249
        case MKTAG( 'w', 'i', 'd', 'e' ):
1250
        case MKTAG( 'f', 'r', 'e', 'e' ):
1251 8b879f18 François Revol
        case MKTAG( 'm', 'd', 'a', 't' ):
1252
        case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
1253 3ffe3793 François Revol
            return AVPROBE_SCORE_MAX;
1254 0e7eed09 Fabrice Bellard
        case MKTAG( 'f', 't', 'y', 'p' ):
1255
        case MKTAG( 's', 'k', 'i', 'p' ):            
1256
            offset = to_be32(p->buf) + offset;
1257
            break;
1258
        default:
1259
            /* unrecognized tag */
1260
            return 0;
1261
        }
1262 3ffe3793 François Revol
    }
1263
    return 0;
1264 c9a65ca8 Fabrice Bellard
}
1265
1266 a266644f Zdenek Kabelac
static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1267 6cea494e Zdenek Kabelac
{
1268 b6a17df4 Zdenek Kabelac
    MOVContext *mov = (MOVContext *) s->priv_data;
1269 6cea494e Zdenek Kabelac
    ByteIOContext *pb = &s->pb;
1270 684f44d9 Zdenek Kabelac
    int i, j, nb, err;
1271 0c1a9eda Zdenek Kabelac
    int64_t size;
1272 6cea494e Zdenek Kabelac
1273
    mov->fc = s;
1274 b6a17df4 Zdenek Kabelac
    mov->parse_table = mov_default_parse_table;
1275 c9a65ca8 Fabrice Bellard
#if 0
1276
    /* XXX: I think we should auto detect */
1277
    if(s->iformat->name[1] == 'p')
1278 6cea494e Zdenek Kabelac
        mov->mp4 = 1;
1279 c9a65ca8 Fabrice Bellard
#endif
1280 6cea494e Zdenek Kabelac
    if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1281
        size = url_filesize(url_fileno(pb));
1282
    else
1283
        size = 0x7FFFFFFFFFFFFFFF;
1284
1285
#ifdef DEBUG
1286 684f44d9 Zdenek Kabelac
    printf("filesz=%Ld\n", size);
1287 6cea494e Zdenek Kabelac
#endif
1288
1289
    /* check MOV header */
1290 b6a17df4 Zdenek Kabelac
    err = parse_default(mov, pb, 0L, 0LL, size);
1291 6cea494e Zdenek Kabelac
    if(err<0 || (!mov->found_moov || !mov->found_mdat)) {
1292
        puts("header not found !!!");
1293
        exit(1);
1294
    }
1295
#ifdef DEBUG
1296 684f44d9 Zdenek Kabelac
    printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
1297 6cea494e Zdenek Kabelac
#endif
1298
    /* some cleanup : make sure we are on the mdat atom */
1299
    if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1300
        url_fseek(pb, mov->mdat_offset, SEEK_SET);
1301
1302
    mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
1303
1304
#ifdef DEBUG
1305 684f44d9 Zdenek Kabelac
    printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
1306 6cea494e Zdenek Kabelac
#endif
1307
1308
#ifdef DEBUG
1309 684f44d9 Zdenek Kabelac
    printf("streams= %d\n", s->nb_streams);
1310 6cea494e Zdenek Kabelac
#endif
1311
    mov->total_streams = nb = s->nb_streams;
1312
    
1313
#if 1
1314
    for(i=0; i<s->nb_streams;) {
1315
        if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
1316 1ea4f593 Fabrice Bellard
            av_free(s->streams[i]);
1317 6cea494e Zdenek Kabelac
            for(j=i+1; j<s->nb_streams; j++)
1318
                s->streams[j-1] = s->streams[j];
1319
            s->nb_streams--;
1320
        } else
1321
            i++;
1322
    }
1323
    for(i=0; i<s->nb_streams;i++) {
1324
        MOVStreamContext *sc;
1325
        sc = (MOVStreamContext *)s->streams[i]->priv_data;
1326
        sc->ffindex = i;
1327
        sc->is_ff_stream = 1;
1328
    }
1329
#endif
1330
#ifdef DEBUG
1331 684f44d9 Zdenek Kabelac
    printf("real streams= %d\n", s->nb_streams);
1332 6cea494e Zdenek Kabelac
#endif
1333
    return 0;
1334
}
1335
1336
/* Yes, this is ugly... I didn't write the specs of QT :p */
1337
/* XXX:remove useless commented code sometime */
1338 a266644f Zdenek Kabelac
static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1339 6cea494e Zdenek Kabelac
{
1340 b6a17df4 Zdenek Kabelac
    MOVContext *mov = (MOVContext *) s->priv_data;
1341 0e7eed09 Fabrice Bellard
    MOVStreamContext *sc;
1342 0c1a9eda Zdenek Kabelac
    int64_t offset = 0x0FFFFFFFFFFFFFFF;
1343 684f44d9 Zdenek Kabelac
    int i;
1344 5cd62665 Zdenek Kabelac
    int size;
1345 6cea494e Zdenek Kabelac
    size = 0x0FFFFFFF;
1346 5cd62665 Zdenek Kabelac
1347 3ffe3793 François Revol
#ifdef MOV_SPLIT_CHUNKS
1348
    if (mov->partial) {
1349 5cd62665 Zdenek Kabelac
1350 3ffe3793 François Revol
        int idx;
1351
1352 5cd62665 Zdenek Kabelac
        sc = mov->partial;
1353
        idx = sc->sample_to_chunk_index;
1354
1355 3ffe3793 François Revol
        if (idx < 0) return 0;
1356 5cd62665 Zdenek Kabelac
        size = sc->sample_sizes[sc->current_sample];
1357 3ffe3793 François Revol
1358 5cd62665 Zdenek Kabelac
        sc->current_sample++;
1359
        sc->left_in_chunk--;
1360 3ffe3793 François Revol
1361 5cd62665 Zdenek Kabelac
        if (sc->left_in_chunk <= 0)
1362 3ffe3793 François Revol
            mov->partial = 0;
1363
        offset = mov->next_chunk_offset;
1364
        /* extract the sample */
1365
1366
        goto readchunk;
1367
    }
1368
#endif
1369
1370 6cea494e Zdenek Kabelac
again:
1371 5cd62665 Zdenek Kabelac
    sc = 0;
1372 6cea494e Zdenek Kabelac
    for(i=0; i<mov->total_streams; i++) {
1373 5cd62665 Zdenek Kabelac
        MOVStreamContext *msc = mov->streams[i];
1374
        //printf("MOCHUNK %ld  %d   %p  pos:%Ld\n", mov->streams[i]->next_chunk, mov->total_streams, mov->streams[i], url_ftell(&s->pb));
1375
        if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0
1376
           && (msc->chunk_offsets[msc->next_chunk] < offset)) {
1377
            sc = msc;
1378
            offset = msc->chunk_offsets[msc->next_chunk];
1379
            //printf("SELETED  %Ld  i:%d\n", offset, i);
1380 6cea494e Zdenek Kabelac
        }
1381
    }
1382 5cd62665 Zdenek Kabelac
    if (!sc || offset==0x0FFFFFFFFFFFFFFF)
1383
        return -1;
1384
1385
    sc->next_chunk++;
1386
1387 3ffe3793 François Revol
    if(mov->next_chunk_offset < offset) { /* some meta data */
1388 6cea494e Zdenek Kabelac
        url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1389 3ffe3793 François Revol
        mov->next_chunk_offset = offset;
1390
    }
1391
1392
//printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
1393 5cd62665 Zdenek Kabelac
    if(!sc->is_ff_stream) {
1394 6cea494e Zdenek Kabelac
        url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1395 3ffe3793 François Revol
        mov->next_chunk_offset = offset;
1396 6cea494e Zdenek Kabelac
        offset = 0x0FFFFFFFFFFFFFFF;
1397
        goto again;
1398
    }
1399
1400
    /* now get the chunk size... */
1401
1402
    for(i=0; i<mov->total_streams; i++) {
1403 5cd62665 Zdenek Kabelac
        MOVStreamContext *msc = mov->streams[i];
1404
        if ((msc->next_chunk < msc->chunk_count)
1405
            && ((msc->chunk_offsets[msc->next_chunk] - offset) < size))
1406
            size = msc->chunk_offsets[msc->next_chunk] - offset;
1407 6cea494e Zdenek Kabelac
    }
1408 3ffe3793 François Revol
#ifdef MOV_SPLIT_CHUNKS
1409
    /* split chunks into samples */
1410 5cd62665 Zdenek Kabelac
    if (sc->sample_size == 0) {
1411
        int idx = sc->sample_to_chunk_index;
1412
        if ((idx + 1 < sc->sample_to_chunk_sz)
1413
            && (sc->next_chunk >= sc->sample_to_chunk[idx + 1].first))
1414
           idx++;
1415
        sc->sample_to_chunk_index = idx;
1416
        if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) {
1417
            mov->partial = sc;
1418 3ffe3793 François Revol
            /* we'll have to get those samples before next chunk */
1419 5cd62665 Zdenek Kabelac
            sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1;
1420
            size = sc->sample_sizes[sc->current_sample];
1421 3ffe3793 François Revol
        }
1422
1423 5cd62665 Zdenek Kabelac
        sc->current_sample++;
1424 3ffe3793 François Revol
    }
1425
#endif
1426
1427
readchunk:
1428
//printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size);
1429 6cea494e Zdenek Kabelac
    if(size == 0x0FFFFFFF)
1430
        size = mov->mdat_size + mov->mdat_offset - offset;
1431
    if(size < 0)
1432
        return -1;
1433
    if(size == 0)
1434
        return -1;
1435 0e7eed09 Fabrice Bellard
    url_fseek(&s->pb, offset, SEEK_SET);
1436 5cd62665 Zdenek Kabelac
1437
    //printf("READCHUNK hlen: %d  %d off: %Ld   pos:%Ld\n", size, sc->header_len, offset, url_ftell(&s->pb));
1438 0e7eed09 Fabrice Bellard
    if (sc->header_len > 0) {
1439
        av_new_packet(pkt, size + sc->header_len);
1440
        memcpy(pkt->data, sc->header_data, sc->header_len);
1441
        get_buffer(&s->pb, pkt->data + sc->header_len, size);
1442
        /* free header */
1443
        av_freep(&sc->header_data);
1444
        sc->header_len = 0;
1445
    } else {
1446
        av_new_packet(pkt, size);
1447
        get_buffer(&s->pb, pkt->data, pkt->size);
1448
    }
1449
    pkt->stream_index = sc->ffindex;
1450 6cea494e Zdenek Kabelac
1451
#ifdef DEBUG
1452
/*
1453
    printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
1454
    for(i=0; i<8; i++)
1455
        printf("%02x ", pkt->data[i]);
1456
    for(i=0; i<8; i++)
1457
        printf("%c ", (pkt->data[i]) & 0x7F);
1458
    puts("");
1459
*/
1460
#endif
1461
1462
    mov->next_chunk_offset = offset + size;
1463
1464
    return 0;
1465
}
1466
1467 a266644f Zdenek Kabelac
static int mov_read_close(AVFormatContext *s)
1468 6cea494e Zdenek Kabelac
{
1469
    int i;
1470 b6a17df4 Zdenek Kabelac
    MOVContext *mov = (MOVContext *) s->priv_data;
1471 6cea494e Zdenek Kabelac
    for(i=0; i<mov->total_streams; i++)
1472
        mov_free_stream_context(mov->streams[i]);
1473
    for(i=0; i<s->nb_streams; i++)
1474 4533d2d6 Zdenek Kabelac
        av_freep(&s->streams[i]);
1475 6cea494e Zdenek Kabelac
    return 0;
1476
}
1477
1478 c9a65ca8 Fabrice Bellard
static AVInputFormat mov_iformat = {
1479 6cea494e Zdenek Kabelac
    "mov",
1480 c9a65ca8 Fabrice Bellard
    "QuickTime/MPEG4 format",
1481
    sizeof(MOVContext),
1482
    mov_probe,
1483 6cea494e Zdenek Kabelac
    mov_read_header,
1484
    mov_read_packet,
1485
    mov_read_close,
1486
};
1487
1488 c9a65ca8 Fabrice Bellard
int mov_init(void)
1489
{
1490
    av_register_input_format(&mov_iformat);
1491
    return 0;
1492
}