]> git.sesse.net Git - ffmpeg/blob - libav/mov.c
- Added r_frame_rate in AVStream to store the real frame rate.
[ffmpeg] / libav / mov.c
1 /*
2  * MOV decoder.
3  * Copyright (c) 2001 Gerard Lantau.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 #include "avformat.h"
20 #include "avi.h"
21
22 /*
23  * First version by Francois Revol revol@free.fr
24  * 
25  * Features and limitations:
26  * - reads most of the QT files I have (at least the structure), 
27  *   the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
28  * - ffmpeg has nearly none of the usual QuickTime codecs,
29  *   although I succesfully dumped raw and mp3 audio tracks off .mov files.
30  *   Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
31  * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
32  *   (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
33  *   http://mpeg.telecomitalialab.com/faq.htm
34  * - the code is quite ugly... maybe I won't do it recursive next time :-)
35  * 
36  * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
37  * when coding this :) (it's a writer anyway)
38  * 
39  * Reference documents:
40  * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
41  * Apple:
42  *  http://developer.apple.com/techpubs/quicktime/qtdevdocs/QTFF/qtff.html
43  *  http://developer.apple.com/techpubs/quicktime/qtdevdocs/PDF/QTFileFormat.pdf
44  * QuickTime is a trademark of Apple (AFAIK :))
45  */
46
47 #define DEBUG
48
49 #ifdef DEBUG
50 /*
51  * XXX: static sux, even more in a multithreaded environment...
52  * Avoid them. This is here just to help debugging.
53  */
54 static int debug_indent = 0;
55 void print_atom(const char *str, UINT32 type, UINT64 offset, UINT64 size)
56 {
57     unsigned int tag, i;
58     tag = (unsigned int) type;
59     i=debug_indent;
60     if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
61     while(i--)
62         printf("|");
63     printf("parse:");
64     printf(" %s: tag=%c%c%c%c offset=%d size=0x%x\n",
65            str, tag & 0xff,
66            (tag >> 8) & 0xff,
67            (tag >> 16) & 0xff,
68            (tag >> 24) & 0xff,
69            (unsigned int)offset,
70            (unsigned int)size);
71 }
72 #endif
73
74 /* some streams in QT (and in MP4 mostly) aren't either video nor audio */
75 /* so we first list them as this, then clean up the list of streams we give back, */
76 /* getting rid of these */
77 #define CODEC_TYPE_MOV_OTHER 2
78
79 static const CodecTag mov_video_tags[] = {
80 /*  { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
81 /*  { CODEC_ID_JPEG, MKTAG('j', 'p', 'e', 'g') }, *//* JPEG */
82     { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, /* Uncompressed RGB */
83     { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, /* Uncompressed YUV422 */
84 /* Graphics */
85 /* Animation */
86 /* Apple video */
87 /* Kodak Photo CD */
88 /*    { CODEC_ID_JPEG, MKTAG('j', 'p', 'e', 'g') }, *//* JPEG ? */
89     { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
90     { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format A) */
91     { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
92 /*    { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
93 /* Sorenson video */
94     { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') }, /* OpenDiVX *//* yeah ! */
95     { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
96 /*    { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
97     { 0, 0 }, 
98 };
99
100 static const CodecTag mov_audio_tags[] = {
101 /*    { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
102     { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
103     { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') }, /* 8 bits */
104     { CODEC_ID_PCM_U8, 0x20776172 }, /* 8 bits unsigned */
105     { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /*  */
106     { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /*  */
107     { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /*  */
108 /*    { CODEC_ID_, MKTAG('i', 'm', 'a', '4') }, *//* IMA-4 */
109
110     { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
111     { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
112     { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
113 /*    { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
114 /* MP4 tags */
115 /*    { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, *//* MPEG 4 AAC or audio ? */
116                                                      /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
117     { 0, 0 }, 
118 };
119
120 /* the QuickTime file format is quite convoluted...
121  * it has lots of index tables, each indexing something in another one...
122  * Here we just use what is needed to read the chunks
123  */
124
125 typedef struct MOV_sample_to_chunk_tbl {
126     long first;
127     long count;
128     long id;
129 } MOV_sample_to_chunk_tbl;
130
131 typedef struct MOVStreamContext {
132     int ffindex; /* the ffmpeg stream id */
133     int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
134     long next_chunk;
135     long chunk_count;
136     INT64 *chunk_offsets;
137     long sample_to_chunk_sz;
138     MOV_sample_to_chunk_tbl *sample_to_chunk;
139     long sample_size;
140     long sample_count;
141     long *sample_sizes;
142 } MOVStreamContext;
143
144 typedef struct MOVContext {
145     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) */
146     AVFormatContext *fc;
147     long time_scale;
148     int found_moov; /* when both 'moov' and 'mdat' sections has been found */
149     int found_mdat; /* we suppose we have enough data to read the file */
150     INT64 mdat_size;
151     INT64 mdat_offset;
152     int total_streams;
153     /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
154      * but we need the info to be able to skip data from those streams in the 'mdat' section
155      */
156     MOVStreamContext *streams[MAX_STREAMS];
157     
158     INT64 next_chunk_offset;
159 } MOVContext;
160
161
162 struct MOVParseTableEntry;
163
164 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
165
166 /* those functions parse an atom */
167 /* return code:
168  1: found what I wanted, exit
169  0: continue to parse next atom
170  -1: error occured, exit
171  */
172 typedef int (*mov_parse_function)(const struct MOVParseTableEntry *parse_table,
173                                   ByteIOContext *pb,
174                                   UINT32 atom_type,
175                                   INT64 atom_offset, /* after the size and type field (and eventually the extended size) */
176                                   INT64 atom_size, /* total size (excluding the size and type fields) */
177                                   void *param);
178
179 /* links atom IDs to parse functions */
180 typedef struct MOVParseTableEntry {
181     UINT32 type;
182     mov_parse_function func;
183 } MOVParseTableEntry;
184
185 static int parse_leaf(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
186 {
187 #ifdef DEBUG
188     print_atom("leaf", atom_type, atom_offset, atom_size);
189 #endif
190     if(atom_size>1)
191         url_fskip(pb, atom_size);
192 /*        url_seek(pb, atom_offset+atom_size, SEEK_SET); */
193     return 0;
194 }
195
196
197 static int parse_default(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
198 {
199     UINT32 type, foo=0;
200     UINT64 offset, size;
201     UINT64 total_size = 0;
202     int i;
203     int err = 0;
204     foo=0;
205 #ifdef DEBUG
206     print_atom("default", atom_type, atom_offset, atom_size);
207     debug_indent++;
208 #endif
209     
210     offset = atom_offset;
211
212     if(atom_size < 0)
213         atom_size = 0x0FFFFFFFFFFFFFFF;
214     while((total_size < atom_size) && !url_feof(pb) && !err) {
215         size=atom_size;
216         type=0L;
217         if(atom_size >= 8) {
218             size = get_be32(pb);
219             type = get_le32(pb);
220         }
221         total_size += 8;
222         offset+=8;
223 //        printf("type: %08lx  sz: %08lx", type, size);
224         if(size == 1) { /* 64 bit extended size */
225             size = get_be64(pb);
226             offset+=8;
227             total_size+=8;
228             size-=8;
229         }
230         if(size == 0)
231             size = atom_size - total_size;
232         size-=8;
233         for(i=0; parse_table[i].type != 0L && parse_table[i].type != type; i++);
234         
235 //        printf(" i=%ld\n", i);
236         if (parse_table[i].type == 0) { /* skip leaf atoms data */
237 //            url_seek(pb, atom_offset+atom_size, SEEK_SET);
238 #ifdef DEBUG
239             print_atom("unknown", type, offset, size);
240 #endif
241             url_fskip(pb, size);
242         } else
243             err = (parse_table[i].func)(parse_table, pb, type, offset, size, param);
244
245         offset+=size;
246         total_size+=size;
247     }
248
249 #ifdef DEBUG
250     debug_indent--;
251 #endif
252     return err;
253 }
254
255 static int parse_mvhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
256 {
257     MOVContext *c;
258 #ifdef DEBUG
259     print_atom("mvhd", atom_type, atom_offset, atom_size);
260 #endif
261     c = (MOVContext *)param;
262
263     get_byte(pb); /* version */
264     get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
265
266     get_be32(pb); /* creation time */
267     get_be32(pb); /* modification time */
268     c->time_scale = get_be32(pb); /* time scale */
269     get_be32(pb); /* duration */
270     get_be32(pb); /* preferred scale */
271     
272     get_be16(pb); /* preferred volume */
273
274     url_fskip(pb, 10); /* reserved */
275
276     url_fskip(pb, 36); /* display matrix */
277
278     get_be32(pb); /* preview time */
279     get_be32(pb); /* preview duration */
280     get_be32(pb); /* poster time */
281     get_be32(pb); /* selection time */
282     get_be32(pb); /* selection duration */
283     get_be32(pb); /* current time */
284     get_be32(pb); /* next track ID */
285     
286     return 0;
287 }
288
289 /* this atom should contain all header atoms */
290 static int parse_moov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
291 {
292     int err;
293     MOVContext *c;
294 #ifdef DEBUG
295     print_atom("moov", atom_type, atom_offset, atom_size);
296 #endif
297     c = (MOVContext *)param;
298
299     err = parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
300     /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
301     /* so we don't parse the whole file if over a network */
302     c->found_moov=1;
303     if(c->found_mdat)
304         return 1; /* found both, just go */
305     return 0; /* now go for mdat */
306 }
307
308 /* this atom contains actual media data */
309 static int parse_mdat(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
310 {
311     MOVContext *c;
312 #ifdef DEBUG
313     print_atom("mdat", atom_type, atom_offset, atom_size);
314 #endif
315     c = (MOVContext *)param;
316
317     if(atom_size == 0) /* wrong one (MP4) */
318         return 0;
319     c->found_mdat=1;
320     c->mdat_offset = atom_offset;
321     c->mdat_size = atom_size;
322     if(c->found_moov)
323         return 1; /* found both, just go */
324     url_fskip(pb, atom_size);
325     return 0; /* now go for moov */
326 }
327
328 static int parse_trak(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
329 {
330     MOVContext *c;
331     AVStream *st;
332     MOVStreamContext *sc;
333 #ifdef DEBUG
334     print_atom("trak", atom_type, atom_offset, atom_size);
335 #endif
336
337     c = (MOVContext *)param;
338     st = malloc(sizeof(AVStream));
339         if (!st) return -2;
340     memset(st, 0, sizeof(AVStream));
341     c->fc->streams[c->fc->nb_streams] = st;
342     sc = malloc(sizeof(MOVStreamContext));
343     st->priv_data = sc;
344     st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
345     c->streams[c->fc->nb_streams++] = sc;
346     return parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
347 }
348
349 static int parse_tkhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
350 {
351     MOVContext *c;
352     AVStream *st;
353 #ifdef DEBUG
354     print_atom("tkhd", atom_type, atom_offset, atom_size);
355 #endif
356
357     c = (MOVContext *)param;
358     st = c->fc->streams[c->fc->nb_streams-1];
359     
360     get_byte(pb); /* version */
361
362     get_byte(pb); get_byte(pb);
363     get_byte(pb); /* flags */
364     /*
365     MOV_TRACK_ENABLED 0x0001
366     MOV_TRACK_IN_MOVIE 0x0002
367     MOV_TRACK_IN_PREVIEW 0x0004
368     MOV_TRACK_IN_POSTER 0x0008
369     */
370
371     get_be32(pb); /* creation time */
372     get_be32(pb); /* modification time */
373     st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
374     get_be32(pb); /* reserved */
375     get_be32(pb); /* duration */
376     get_be32(pb); /* reserved */
377     get_be32(pb); /* reserved */
378     
379     get_be16(pb); /* layer */
380     get_be16(pb); /* alternate group */
381     get_be16(pb); /* volume */
382     get_be16(pb); /* reserved */
383
384     url_fskip(pb, 36); /* display matrix */
385
386     /* those are fixed-point */
387     st->codec.width = get_be32(pb) >> 16; /* track width */
388     st->codec.height = get_be32(pb) >> 16; /* track height */
389     
390     return 0;
391 }
392
393 static int parse_hdlr(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
394 {
395     MOVContext *c;
396     int len;
397     char *buf, ch;
398     UINT32 type;
399     AVStream *st;
400     UINT32 ctype;
401 #ifdef DEBUG
402     print_atom("hdlr", atom_type, atom_offset, atom_size);
403 #endif
404     c = (MOVContext *)param;
405     st = c->fc->streams[c->fc->nb_streams-1];
406
407     get_byte(pb); /* version */
408     get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
409
410     /* component type */
411     ctype = get_le32(pb);
412     type = get_le32(pb); /* component subtype */
413
414 #ifdef DEBUG
415     printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
416     printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
417 #endif
418 #ifdef DEBUG
419 /* XXX: yeah this is ugly... */
420     if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
421         if(type == MKTAG('v', 'i', 'd', 'e'))
422             puts("hdlr: vide");
423         else if(type == MKTAG('s', 'o', 'u', 'n'))
424             puts("hdlr: soun");
425     } else if(ctype == 0) { /* MP4 */
426         if(type == MKTAG('v', 'i', 'd', 'e'))
427             puts("hdlr: vide");
428         else if(type == MKTAG('s', 'o', 'u', 'n'))
429             puts("hdlr: soun");
430         else if(type == MKTAG('o', 'd', 's', 'm'))
431             puts("hdlr: odsm");
432         else if(type == MKTAG('s', 'd', 's', 'm'))
433             puts("hdlr: sdsm");
434     } else puts("hdlr: meta");
435 #endif
436
437     if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
438         if(type == MKTAG('v', 'i', 'd', 'e'))
439             st->codec.codec_type = CODEC_TYPE_VIDEO;
440         else if(type == MKTAG('s', 'o', 'u', 'n'))
441             st->codec.codec_type = CODEC_TYPE_AUDIO;
442     } else if(ctype == 0) { /* MP4 */
443         if(type == MKTAG('v', 'i', 'd', 'e'))
444             st->codec.codec_type = CODEC_TYPE_VIDEO;
445         else if(type == MKTAG('s', 'o', 'u', 'n'))
446             st->codec.codec_type = CODEC_TYPE_AUDIO;
447     }
448     get_be32(pb); /* component  manufacture */
449     get_be32(pb); /* component flags */
450     get_be32(pb); /* component flags mask */
451
452     if(atom_size <= 24)
453         return 0; /* nothing left to read */
454     /* XXX: MP4 uses a C string, not a pascal one */
455     /* component name */
456     if(c->mp4) {
457 #ifdef DEBUG
458         puts("MP4!!!");
459 #endif
460         while ((ch = get_byte(pb)));
461     } else {
462         len = get_byte(pb);
463         if(len) {
464             buf = malloc(len+1);
465             get_buffer(pb, buf, len);
466             buf[len] = '\0';
467 #ifdef DEBUG
468             puts(buf);
469 #endif
470             free(buf);
471         }
472     }
473     
474     return 0;
475 }
476
477 static int parse_stsd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
478 {
479     MOVContext *c;
480     int entries, size, samp_sz, frames_per_sample;
481     UINT32 format;
482     AVStream *st;
483 #ifdef DEBUG
484     print_atom("stsd", atom_type, atom_offset, atom_size);
485 #endif
486     c = (MOVContext *)param;
487     st = c->fc->streams[c->fc->nb_streams-1];
488
489     get_byte(pb); /* version */
490     get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
491
492
493     entries = get_be32(pb);
494
495     while(entries--) {
496         size = get_be32(pb); /* size */
497         format = get_le32(pb); /* data format */
498         
499         get_be32(pb); /* reserved */
500         get_be16(pb); /* reserved */
501         get_be16(pb); /* index */
502 /*        if(format == MKTAG('m', 'p', '4', 'v')) */
503 /*            st->codec.codec_type=CODEC_TYPE_VIDEO; *//* force things (XXX: was this for .mp4 ?) */
504         if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
505             st->codec.codec_tag = format;
506             st->codec.codec_id = codec_get_id(mov_video_tags, format);
507             get_be16(pb); /* version */
508             get_be16(pb); /* revision level */
509             get_be32(pb); /* vendor */
510             get_be32(pb); /* temporal quality */
511             get_be32(pb); /* spacial quality */
512             st->codec.width = get_be16(pb); /* width */
513             st->codec.height = get_be16(pb); /* height */
514             get_be32(pb); /* horiz resolution */
515             get_be32(pb); /* vert resolution */
516             get_be32(pb); /* data size, always 0 */
517             frames_per_sample = get_be16(pb); /* frame per samples */
518 #ifdef DEBUG
519             printf("frames/samples = %d\n", frames_per_sample);
520 #endif
521             url_fskip(pb, 32); /* codec name */
522
523             get_be16(pb); /* depth */
524             get_be16(pb); /* colortable id */
525             get_be16(pb); /*  */
526             get_be16(pb); /*  */
527             
528             st->codec.sample_rate = 25 * FRAME_RATE_BASE;
529             
530             if(size > 16)
531                 url_fskip(pb, size-(16+24+18+32));
532         } else {
533             st->codec.codec_tag = format;
534
535             get_be16(pb); /* version */
536             get_be16(pb); /* revision level */
537             get_be32(pb); /* vendor */
538
539             st->codec.channels = get_be16(pb);/* channel count */
540             samp_sz = get_be16(pb); /* sample size */
541 #ifdef DEBUG
542             if(samp_sz != 16)
543                 puts("!!! stsd: audio sample size is not 16 bit !");
544 #endif            
545             st->codec.codec_id = codec_get_id(mov_audio_tags, format);
546             /* handle specific s8 codec */
547             if (st->codec.codec_id == CODEC_ID_PCM_S16BE && samp_sz == 8)
548             st->codec.codec_id = CODEC_ID_PCM_S8;
549
550             get_be16(pb); /* compression id = 0*/
551             get_be16(pb); /* packet size = 0 */
552             
553             st->codec.sample_rate = ((get_be32(pb) >> 16));
554             st->codec.bit_rate = 0;
555
556             /* this is for .mp4 files */
557             if(format == MKTAG('m', 'p', '4', 'v')) { /* XXX */
558                 st->codec.codec_type=CODEC_TYPE_VIDEO; /* force things */
559                 st->codec.codec_id = CODEC_ID_MPEG4;
560                 st->codec.frame_rate = 25;
561                 st->codec.bit_rate = 100000;
562             }
563
564 #if 0
565
566             get_be16(pb); get_be16(pb); /*  */
567             get_be16(pb); /*  */
568             get_be16(pb); /*  */
569             get_be16(pb); /*  */
570             get_be16(pb); /*  */
571 #endif            
572             if(size > 16)
573                 url_fskip(pb, size-(16+20));
574         }
575 #ifdef DEBUG
576         printf("4CC= %c%c%c%c\n", *((char *)&format), ((char *)&format)[1], ((char *)&format)[2], ((char *)&format)[3]);
577 #endif
578     }
579 /*
580     if(len) {
581         buf = malloc(len+1);
582         get_buffer(pb, buf, len);
583         buf[len] = '\0';
584         puts(buf);
585         free(buf);
586     }
587 */
588     return 0;
589 }
590
591 static int parse_stco(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
592 {
593     MOVContext *c;
594     int entries, i;
595     AVStream *st;
596     MOVStreamContext *sc;
597 #ifdef DEBUG
598     print_atom("stco", atom_type, atom_offset, atom_size);
599 #endif
600     c = (MOVContext *)param;
601     st = c->fc->streams[c->fc->nb_streams-1];
602     sc = (MOVStreamContext *)st->priv_data;
603     
604     get_byte(pb); /* version */
605     get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
606
607     entries = get_be32(pb);
608     sc->chunk_count = entries;
609     sc->chunk_offsets = malloc(entries * sizeof(INT64));
610     if(atom_type == MKTAG('s', 't', 'c', 'o')) {
611         for(i=0; i<entries; i++) {
612             sc->chunk_offsets[i] = get_be32(pb);
613             /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
614         }
615     } else if(atom_type == MKTAG('c', 'o', '6', '4')) {
616         for(i=0; i<entries; i++) {
617             sc->chunk_offsets[i] = get_be64(pb);
618             /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
619         }
620     } else
621         return -1;
622     return 0;
623 }
624
625 static int parse_stsc(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
626 {
627     MOVContext *c;
628     int entries, i;
629     AVStream *st;
630     MOVStreamContext *sc;
631 #ifdef DEBUG
632     print_atom("stsc", atom_type, atom_offset, atom_size);
633 #endif
634     c = (MOVContext *)param;
635     st = c->fc->streams[c->fc->nb_streams-1];
636     sc = (MOVStreamContext *)st->priv_data;
637     
638     get_byte(pb); /* version */
639     get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
640
641     entries = get_be32(pb);
642     sc->sample_to_chunk_sz = entries;
643     sc->sample_to_chunk = malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
644     for(i=0; i<entries; i++) {
645         sc->sample_to_chunk[i].first = get_be32(pb);
646         sc->sample_to_chunk[i].count = get_be32(pb);
647         sc->sample_to_chunk[i].id = get_be32(pb);
648 #ifdef DEBUG
649 /*        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); */
650 #endif
651     }
652     return 0;
653 }
654
655 static int parse_stsz(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
656 {
657     MOVContext *c;
658     int entries, i;
659     AVStream *st;
660     MOVStreamContext *sc;
661 #ifdef DEBUG
662     print_atom("stsz", atom_type, atom_offset, atom_size);
663 #endif
664     c = (MOVContext *)param;
665     st = c->fc->streams[c->fc->nb_streams-1];
666     sc = (MOVStreamContext *)st->priv_data;
667     
668     get_byte(pb); /* version */
669     get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
670     
671     sc->sample_size = get_be32(pb);
672     entries = get_be32(pb);
673     sc->sample_count = entries;
674     printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
675     if(sc->sample_size)
676         return 0; /* there isn't any table following */
677     sc->sample_sizes = malloc(entries * sizeof(long));
678     for(i=0; i<entries; i++) {
679         sc->sample_sizes[i] = get_be32(pb);
680 #ifdef DEBUG
681 /*        printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
682 #endif
683     }
684     return 0;
685 }
686
687 static const MOVParseTableEntry mov_default_parse_table[] = {
688 /* mp4 atoms */
689 { MKTAG( 'm', 'p', '4', 'a' ), parse_default },
690 { MKTAG( 'c', 'o', '6', '4' ), parse_stco },
691 { MKTAG( 's', 't', 'c', 'o' ), parse_stco },
692 { MKTAG( 'c', 'r', 'h', 'd' ), parse_default },
693 { MKTAG( 'c', 't', 't', 's' ), parse_leaf },
694 { MKTAG( 'c', 'p', 'r', 't' ), parse_default },
695 { MKTAG( 'u', 'r', 'l', ' ' ), parse_leaf },
696 { MKTAG( 'u', 'r', 'n', ' ' ), parse_leaf },
697 { MKTAG( 'd', 'i', 'n', 'f' ), parse_default },
698 { MKTAG( 'd', 'r', 'e', 'f' ), parse_leaf },
699 { MKTAG( 's', 't', 'd', 'p' ), parse_default },
700 { MKTAG( 'e', 's', 'd', 's' ), parse_default },
701 { MKTAG( 'e', 'd', 't', 's' ), parse_default },
702 { MKTAG( 'e', 'l', 's', 't' ), parse_leaf },
703 { MKTAG( 'u', 'u', 'i', 'd' ), parse_default },
704 { MKTAG( 'f', 'r', 'e', 'e' ), parse_leaf },
705 { MKTAG( 'h', 'd', 'l', 'r' ), parse_hdlr },
706 { MKTAG( 'h', 'm', 'h', 'd' ), parse_default },
707 { MKTAG( 'h', 'i', 'n', 't' ), parse_leaf },
708 { MKTAG( 'n', 'm', 'h', 'd' ), parse_leaf },
709 { MKTAG( 'm', 'p', '4', 's' ), parse_default },
710 { MKTAG( 'm', 'd', 'i', 'a' ), parse_default },
711 { MKTAG( 'm', 'd', 'a', 't' ), parse_mdat },
712 { MKTAG( 'm', 'd', 'h', 'd' ), parse_leaf },
713 { MKTAG( 'm', 'i', 'n', 'f' ), parse_default },
714 { MKTAG( 'm', 'o', 'o', 'v' ), parse_moov },
715 { MKTAG( 'm', 'v', 'h', 'd' ), parse_mvhd },
716 { MKTAG( 'i', 'o', 'd', 's' ), parse_leaf },
717 { MKTAG( 'o', 'd', 'h', 'd' ), parse_default },
718 { MKTAG( 'm', 'p', 'o', 'd' ), parse_leaf },
719 { MKTAG( 's', 't', 's', 'd' ), parse_stsd },
720 { MKTAG( 's', 't', 's', 'z' ), parse_stsz },
721 { MKTAG( 's', 't', 'b', 'l' ), parse_default },
722 { MKTAG( 's', 't', 's', 'c' ), parse_stsc },
723 { MKTAG( 's', 'd', 'h', 'd' ), parse_default },
724 { MKTAG( 's', 't', 's', 'h' ), parse_default },
725 { MKTAG( 's', 'k', 'i', 'p' ), parse_default },
726 { MKTAG( 's', 'm', 'h', 'd' ), parse_leaf },
727 { MKTAG( 'd', 'p', 'n', 'd' ), parse_leaf },
728 { MKTAG( 's', 't', 's', 's' ), parse_leaf },
729 { MKTAG( 's', 't', 't', 's' ), parse_leaf },
730 { MKTAG( 't', 'r', 'a', 'k' ), parse_trak },
731 { MKTAG( 't', 'k', 'h', 'd' ), parse_tkhd },
732 { MKTAG( 't', 'r', 'e', 'f' ), parse_default }, /* not really */
733 { MKTAG( 'u', 'd', 't', 'a' ), parse_leaf },
734 { MKTAG( 'v', 'm', 'h', 'd' ), parse_leaf },
735 { MKTAG( 'm', 'p', '4', 'v' ), parse_default },
736 /* extra mp4 */
737 { MKTAG( 'M', 'D', 'E', 'S' ), parse_leaf },
738 /* QT atoms */
739 { MKTAG( 'c', 'h', 'a', 'p' ), parse_leaf },
740 { MKTAG( 'c', 'l', 'i', 'p' ), parse_default },
741 { MKTAG( 'c', 'r', 'g', 'n' ), parse_leaf },
742 { MKTAG( 'k', 'm', 'a', 't' ), parse_leaf },
743 { MKTAG( 'm', 'a', 't', 't' ), parse_default },
744 { MKTAG( 'r', 'd', 'r', 'f' ), parse_leaf },
745 { MKTAG( 'r', 'm', 'd', 'a' ), parse_default },
746 { MKTAG( 'r', 'm', 'd', 'r' ), parse_leaf },
747 //{ MKTAG( 'r', 'm', 'q', 'u' ), parse_leaf },
748 { MKTAG( 'r', 'm', 'r', 'a' ), parse_default },
749 { MKTAG( 's', 'c', 'p', 't' ), parse_leaf },
750 { MKTAG( 's', 'y', 'n', 'c' ), parse_leaf },
751 { MKTAG( 's', 's', 'r', 'c' ), parse_leaf },
752 { MKTAG( 't', 'c', 'm', 'd' ), parse_leaf },
753 { MKTAG( 'w', 'i', 'd', 'e' ), parse_leaf }, /* place holder */
754 { 0L, parse_leaf }
755 };
756
757 static void mov_free_stream_context(MOVStreamContext *sc)
758 {
759     if(sc) {
760         if(sc->chunk_offsets)
761             free(sc->chunk_offsets);
762         if(sc->sample_to_chunk)
763             free(sc->sample_to_chunk);
764         free(sc);
765     }
766 }
767
768 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
769 {
770     MOVContext *mov;
771     ByteIOContext *pb = &s->pb;
772     int i, j, nb, err;
773     INT64 size;
774
775     mov = malloc(sizeof(MOVContext));
776     if (!mov)
777         return -1;
778     memset(mov, 0, sizeof(MOVContext));
779     s->priv_data = mov;
780
781     mov->fc = s;
782     if(s->format->name[1] == 'p')
783         mov->mp4 = 1;
784     if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
785         size = url_filesize(url_fileno(pb));
786     else
787         size = 0x7FFFFFFFFFFFFFFF;
788
789 #ifdef DEBUG
790     printf("filesz=%Ld\n", size);
791 #endif
792
793     /* check MOV header */
794     err = parse_default(mov_default_parse_table, pb, 0L, 0LL, size, mov);
795     if(err<0 || (!mov->found_moov || !mov->found_mdat)) {
796         puts("header not found !!!");
797         exit(1);
798     }
799 #ifdef DEBUG
800     printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
801 #endif
802     /* some cleanup : make sure we are on the mdat atom */
803     if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
804         url_fseek(pb, mov->mdat_offset, SEEK_SET);
805
806     mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
807
808 #ifdef DEBUG
809     printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
810 #endif
811
812 #ifdef DEBUG
813     printf("streams= %d\n", s->nb_streams);
814 #endif
815     mov->total_streams = nb = s->nb_streams;
816     
817 #if 1
818     for(i=0; i<s->nb_streams;) {
819         if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
820             free(s->streams[i]);
821             for(j=i+1; j<s->nb_streams; j++)
822                 s->streams[j-1] = s->streams[j];
823             s->nb_streams--;
824         } else
825             i++;
826     }
827     for(i=0; i<s->nb_streams;i++) {
828         MOVStreamContext *sc;
829         sc = (MOVStreamContext *)s->streams[i]->priv_data;
830         sc->ffindex = i;
831         sc->is_ff_stream = 1;
832     }
833 #endif
834 #ifdef DEBUG
835     printf("real streams= %d\n", s->nb_streams);
836 #endif
837     return 0;
838 }
839
840 /* Yes, this is ugly... I didn't write the specs of QT :p */
841 /* XXX:remove useless commented code sometime */
842 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
843 {
844     MOVContext *mov = s->priv_data;
845     INT64 offset = 0x0FFFFFFFFFFFFFFF;
846     int i;
847     int st_id = 0, size;
848     size = 0x0FFFFFFF;
849     
850 again:
851     for(i=0; i<mov->total_streams; i++) {
852 /*        printf("%8ld ", mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk]); */
853         if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
854         && (mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] < offset)) {
855 /*            printf("y"); */
856             st_id = i;
857             offset = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk];
858         }
859 /*         else printf("n"); */
860     }
861     mov->streams[st_id]->next_chunk++;
862     if(offset==0x0FFFFFFFFFFFFFFF)
863         return -1;
864     
865     if(mov->next_chunk_offset < offset) /* some meta data */
866         url_fskip(&s->pb, (offset - mov->next_chunk_offset));
867     if(!mov->streams[st_id]->is_ff_stream) {
868         url_fskip(&s->pb, (offset - mov->next_chunk_offset));
869         offset = 0x0FFFFFFFFFFFFFFF;
870 /*        puts("*"); */
871         goto again;
872     }
873 /* printf("\nchunk offset = %ld\n", offset); */
874
875     /* now get the chunk size... */
876
877     for(i=0; i<mov->total_streams; i++) {
878 /*        printf("%ld ", mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset); */
879         if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
880         && ((mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset) < size)) {
881 /*            printf("y"); */
882             size = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset;
883         }
884 /*         else printf("n"); */
885     }
886 /* printf("\nchunk size = %ld\n", size); */
887     if(size == 0x0FFFFFFF)
888         size = mov->mdat_size + mov->mdat_offset - offset;
889     if(size < 0)
890         return -1;
891     if(size == 0)
892         return -1;
893     av_new_packet(pkt, size);
894     pkt->stream_index = mov->streams[st_id]->ffindex;
895
896     get_buffer(&s->pb, pkt->data, pkt->size);
897
898 #ifdef DEBUG
899 /*
900     printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
901     for(i=0; i<8; i++)
902         printf("%02x ", pkt->data[i]);
903     for(i=0; i<8; i++)
904         printf("%c ", (pkt->data[i]) & 0x7F);
905     puts("");
906 */
907 #endif
908
909     mov->next_chunk_offset = offset + size;
910
911     return 0;
912 }
913
914 static int mov_read_close(AVFormatContext *s)
915 {
916     int i;
917     MOVContext *mov = s->priv_data;
918     for(i=0; i<mov->total_streams; i++)
919         mov_free_stream_context(mov->streams[i]);
920     for(i=0; i<s->nb_streams; i++)
921         free(s->streams[i]);
922     free(mov);
923     return 0;
924 }
925
926 AVFormat mov_format = {
927     "mov",
928     "QuickTime format",
929     "video/quicktime",
930     "mov",
931     CODEC_ID_MP2,
932     CODEC_ID_MJPEG,
933     NULL,
934     NULL,
935     NULL,
936
937     mov_read_header,
938     mov_read_packet,
939     mov_read_close,
940 };
941
942 AVFormat mp4_format = {
943     "mp4",
944     "MPEG4 file format",
945     "video/mpeg4",
946     "mp4",
947     CODEC_ID_MP2,
948     CODEC_ID_MJPEG,
949     NULL,
950     NULL,
951     NULL,
952
953     mov_read_header,
954     mov_read_packet,
955     mov_read_close,
956 };