patch by Fran├žois Revol <revol at free dot fr>
[libav.git] / libav / mov.c
1 /*
2 * MOV decoder.
3 * Copyright (c) 2001 Fabrice Bellard.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "avformat.h"
20 #include "avi.h"
21
22 #ifdef CONFIG_ZLIB
23 #include <zlib.h>
24 #endif
25
26 /*
27 * First version by Francois Revol revol@free.fr
28 *
29 * Features and limitations:
30 * - reads most of the QT files I have (at least the structure),
31 * the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
32 * FIXED, Francois Revol, 07/17/2002
33 * - ffmpeg has nearly none of the usual QuickTime codecs,
34 * although I succesfully dumped raw and mp3 audio tracks off .mov files.
35 * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
36 * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
37 * (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
38 * http://mpeg.telecomitalialab.com/faq.htm
39 * - the code is quite ugly... maybe I won't do it recursive next time :-)
40 *
41 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
42 * when coding this :) (it's a writer anyway)
43 *
44 * Reference documents:
45 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
46 * Apple:
47 * http://developer.apple.com/techpubs/quicktime/qtdevdocs/QTFF/qtff.html
48 * http://developer.apple.com/techpubs/quicktime/qtdevdocs/PDF/QTFileFormat.pdf
49 * QuickTime is a trademark of Apple (AFAIK :))
50 */
51
52 //#define DEBUG
53
54 /* allows chunk splitting - should work now... */
55 /* in case you can't read a file, try commenting */
56 #define MOV_SPLIT_CHUNKS
57
58 #ifdef DEBUG
59 /*
60 * XXX: static sux, even more in a multithreaded environment...
61 * Avoid them. This is here just to help debugging.
62 */
63 static int debug_indent = 0;
64 void print_atom(const char *str, UINT32 type, UINT64 offset, UINT64 size)
65 {
66 unsigned int tag, i;
67 tag = (unsigned int) type;
68 i=debug_indent;
69 if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
70 while(i--)
71 printf("|");
72 printf("parse:");
73 printf(" %s: tag=%c%c%c%c offset=0x%x size=0x%x\n",
74 str, tag & 0xff,
75 (tag >> 8) & 0xff,
76 (tag >> 16) & 0xff,
77 (tag >> 24) & 0xff,
78 (unsigned int)offset,
79 (unsigned int)size);
80 }
81 #endif
82
83 /* some streams in QT (and in MP4 mostly) aren't either video nor audio */
84 /* so we first list them as this, then clean up the list of streams we give back, */
85 /* getting rid of these */
86 #define CODEC_TYPE_MOV_OTHER 2
87
88 static const CodecTag mov_video_tags[] = {
89 /* { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
90 /* { CODEC_ID_JPEG, MKTAG('j', 'p', 'e', 'g') }, *//* JPEG */
91 /* { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
92 /* { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
93 /* Graphics */
94 /* Animation */
95 /* Apple video */
96 /* Kodak Photo CD */
97 { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
98 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
99 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
100 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
101 /* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
102 /* Sorenson video */
103 { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
104 { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
105 { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
106 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') }, /* OpenDiVX *//* yeah ! */
107 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
108 /* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
109 { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
110 { 0, 0 },
111 };
112
113 static const CodecTag mov_audio_tags[] = {
114 /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
115 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
116 { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') }, /* 8 bits */
117 { CODEC_ID_PCM_U8, 0x20776172 }, /* 8 bits unsigned */
118 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
119 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
120 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
121 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
122
123 { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
124 { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
125 { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
126 /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
127 /* MP4 tags */
128 /* { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, *//* MPEG 4 AAC or audio ? */
129 /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
130 { 0, 0 },
131 };
132
133 /* the QuickTime file format is quite convoluted...
134 * it has lots of index tables, each indexing something in another one...
135 * Here we just use what is needed to read the chunks
136 */
137
138 typedef struct MOV_sample_to_chunk_tbl {
139 long first;
140 long count;
141 long id;
142 } MOV_sample_to_chunk_tbl;
143
144 typedef struct MOVStreamContext {
145 int ffindex; /* the ffmpeg stream id */
146 int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
147 long next_chunk;
148 long chunk_count;
149 INT64 *chunk_offsets;
150 long sample_to_chunk_sz;
151 MOV_sample_to_chunk_tbl *sample_to_chunk;
152 long sample_to_chunk_index;
153 long sample_size;
154 long sample_count;
155 long *sample_sizes;
156 long time_scale;
157 long current_sample;
158 long left_in_chunk; /* how many samples before next chunk */
159 } MOVStreamContext;
160
161 typedef struct MOVContext {
162 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) */
163 AVFormatContext *fc;
164 long time_scale;
165 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
166 int found_mdat; /* we suppose we have enough data to read the file */
167 INT64 mdat_size;
168 INT64 mdat_offset;
169 int total_streams;
170 /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
171 * but we need the info to be able to skip data from those streams in the 'mdat' section
172 */
173 MOVStreamContext *streams[MAX_STREAMS];
174
175 INT64 next_chunk_offset;
176 int partial; /* != 0 : there is still to read in the current chunk (=id of the stream + 1) */
177 } MOVContext;
178
179
180 struct MOVParseTableEntry;
181
182 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
183
184 /* those functions parse an atom */
185 /* return code:
186 1: found what I wanted, exit
187 0: continue to parse next atom
188 -1: error occured, exit
189 */
190 typedef int (*mov_parse_function)(const struct MOVParseTableEntry *parse_table,
191 ByteIOContext *pb,
192 UINT32 atom_type,
193 INT64 atom_offset, /* after the size and type field (and eventually the extended size) */
194 INT64 atom_size, /* total size (excluding the size and type fields) */
195 void *param);
196
197 /* links atom IDs to parse functions */
198 typedef struct MOVParseTableEntry {
199 UINT32 type;
200 mov_parse_function func;
201 } MOVParseTableEntry;
202
203 static int parse_leaf(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
204 {
205 #ifdef DEBUG
206 print_atom("leaf", atom_type, atom_offset, atom_size);
207 #endif
208 if(atom_size>1)
209 url_fskip(pb, atom_size);
210 /* url_seek(pb, atom_offset+atom_size, SEEK_SET); */
211 return 0;
212 }
213
214
215 static int parse_default(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
216 {
217 UINT32 type, foo=0;
218 UINT64 offset, size;
219 UINT64 total_size = 0;
220 int i;
221 int err = 0;
222 foo=0;
223 #ifdef DEBUG
224 print_atom("default", atom_type, atom_offset, atom_size);
225 debug_indent++;
226 #endif
227
228 offset = atom_offset;
229
230 if(atom_size < 0)
231 atom_size = 0x0FFFFFFFFFFFFFFF;
232 while((total_size < atom_size) && !url_feof(pb) && !err) {
233 size=atom_size;
234 type=0L;
235 if(atom_size >= 8) {
236 size = get_be32(pb);
237 type = get_le32(pb);
238 }
239 total_size += 8;
240 offset+=8;
241 // printf("type: %08lx sz: %08lx", type, size);
242 if(size == 1) { /* 64 bit extended size */
243 size = get_be64(pb);
244 offset+=8;
245 total_size+=8;
246 size-=8;
247 }
248 if(size == 0)
249 size = atom_size - total_size;
250 size-=8;
251 for(i=0; parse_table[i].type != 0L && parse_table[i].type != type; i++);
252
253 // printf(" i=%ld\n", i);
254 if (parse_table[i].type == 0) { /* skip leaf atoms data */
255 // url_seek(pb, atom_offset+atom_size, SEEK_SET);
256 #ifdef DEBUG
257 print_atom("unknown", type, offset, size);
258 #endif
259 url_fskip(pb, size);
260 } else
261 err = (parse_table[i].func)(parse_table, pb, type, offset, size, param);
262
263 offset+=size;
264 total_size+=size;
265 }
266
267 #ifdef DEBUG
268 debug_indent--;
269 #endif
270 return err;
271 }
272
273 static int parse_mvhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
274 {
275 MOVContext *c;
276 #ifdef DEBUG
277 print_atom("mvhd", atom_type, atom_offset, atom_size);
278 #endif
279 c = (MOVContext *)param;
280
281 get_byte(pb); /* version */
282 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
283
284 get_be32(pb); /* creation time */
285 get_be32(pb); /* modification time */
286 c->time_scale = get_be32(pb); /* time scale */
287 #ifdef DEBUG
288 printf("time scale = %li\n", c->time_scale);
289 #endif
290 get_be32(pb); /* duration */
291 get_be32(pb); /* preferred scale */
292
293 get_be16(pb); /* preferred volume */
294
295 url_fskip(pb, 10); /* reserved */
296
297 url_fskip(pb, 36); /* display matrix */
298
299 get_be32(pb); /* preview time */
300 get_be32(pb); /* preview duration */
301 get_be32(pb); /* poster time */
302 get_be32(pb); /* selection time */
303 get_be32(pb); /* selection duration */
304 get_be32(pb); /* current time */
305 get_be32(pb); /* next track ID */
306
307 return 0;
308 }
309
310 /* this atom should contain all header atoms */
311 static int parse_moov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
312 {
313 int err;
314 MOVContext *c;
315 #ifdef DEBUG
316 print_atom("moov", atom_type, atom_offset, atom_size);
317 #endif
318 c = (MOVContext *)param;
319
320 err = parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
321 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
322 /* so we don't parse the whole file if over a network */
323 c->found_moov=1;
324 if(c->found_mdat)
325 return 1; /* found both, just go */
326 return 0; /* now go for mdat */
327 }
328
329 /* this atom contains actual media data */
330 static int parse_mdat(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
331 {
332 MOVContext *c;
333 #ifdef DEBUG
334 print_atom("mdat", atom_type, atom_offset, atom_size);
335 #endif
336 c = (MOVContext *)param;
337
338 if(atom_size == 0) /* wrong one (MP4) */
339 return 0;
340 c->found_mdat=1;
341 c->mdat_offset = atom_offset;
342 c->mdat_size = atom_size;
343 if(c->found_moov)
344 return 1; /* found both, just go */
345 url_fskip(pb, atom_size);
346 return 0; /* now go for moov */
347 }
348
349 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
350 /* like the files created with Adobe Premiere 5.0, for samples see */
351 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
352 static int parse_wide(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
353 {
354 int err;
355 UINT32 type;
356 #ifdef DEBUG
357 print_atom("wide", atom_type, atom_offset, atom_size);
358 debug_indent++;
359 #endif
360 if (atom_size < 8)
361 return 0; /* continue */
362 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
363 url_fskip(pb, atom_size - 4);
364 return 0;
365 }
366 type = get_le32(pb);
367 if (type != MKTAG('m', 'd', 'a', 't')) {
368 url_fskip(pb, atom_size - 8);
369 return 0;
370 }
371 err = parse_mdat(parse_table, pb, type, atom_offset + 8, atom_size - 8, param);
372 #ifdef DEBUG
373 debug_indent--;
374 #endif
375 return err;
376 }
377
378 static int parse_trak(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
379 {
380 MOVContext *c;
381 AVStream *st;
382 MOVStreamContext *sc;
383 #ifdef DEBUG
384 print_atom("trak", atom_type, atom_offset, atom_size);
385 #endif
386
387 c = (MOVContext *)param;
388 st = av_new_stream(c->fc, c->fc->nb_streams);
389 if (!st) return -2;
390 sc = av_malloc(sizeof(MOVStreamContext));
391 sc->sample_to_chunk_index = -1;
392 st->priv_data = sc;
393 st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
394 c->streams[c->fc->nb_streams-1] = sc;
395 return parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
396 }
397
398 static int parse_tkhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
399 {
400 MOVContext *c;
401 AVStream *st;
402 #ifdef DEBUG
403 print_atom("tkhd", atom_type, atom_offset, atom_size);
404 #endif
405
406 c = (MOVContext *)param;
407 st = c->fc->streams[c->fc->nb_streams-1];
408
409 get_byte(pb); /* version */
410
411 get_byte(pb); get_byte(pb);
412 get_byte(pb); /* flags */
413 /*
414 MOV_TRACK_ENABLED 0x0001
415 MOV_TRACK_IN_MOVIE 0x0002
416 MOV_TRACK_IN_PREVIEW 0x0004
417 MOV_TRACK_IN_POSTER 0x0008
418 */
419
420 get_be32(pb); /* creation time */
421 get_be32(pb); /* modification time */
422 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
423 get_be32(pb); /* reserved */
424 get_be32(pb); /* duration */
425 get_be32(pb); /* reserved */
426 get_be32(pb); /* reserved */
427
428 get_be16(pb); /* layer */
429 get_be16(pb); /* alternate group */
430 get_be16(pb); /* volume */
431 get_be16(pb); /* reserved */
432
433 url_fskip(pb, 36); /* display matrix */
434
435 /* those are fixed-point */
436 st->codec.width = get_be32(pb) >> 16; /* track width */
437 st->codec.height = get_be32(pb) >> 16; /* track height */
438
439 return 0;
440 }
441
442 static int parse_mdhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
443 {
444 MOVContext *c;
445 AVStream *st;
446 #ifdef DEBUG
447 print_atom("mdhd", atom_type, atom_offset, atom_size);
448 #endif
449
450 c = (MOVContext *)param;
451 st = c->fc->streams[c->fc->nb_streams-1];
452
453 get_byte(pb); /* version */
454
455 get_byte(pb); get_byte(pb);
456 get_byte(pb); /* flags */
457
458 get_be32(pb); /* creation time */
459 get_be32(pb); /* modification time */
460
461 c->streams[c->total_streams]->time_scale = get_be32(pb);
462
463 #ifdef DEBUG
464 printf("track[%i].time_scale = %li\n", c->fc->nb_streams-1, c->streams[c->total_streams]->time_scale); /* time scale */
465 #endif
466 get_be32(pb); /* duration */
467
468 get_be16(pb); /* language */
469 get_be16(pb); /* quality */
470
471 return 0;
472 }
473
474 static int parse_hdlr(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
475 {
476 MOVContext *c;
477 int len = 0;
478 char *buf;
479 UINT32 type;
480 AVStream *st;
481 UINT32 ctype;
482 #ifdef DEBUG
483 print_atom("hdlr", atom_type, atom_offset, atom_size);
484 #endif
485 c = (MOVContext *)param;
486 st = c->fc->streams[c->fc->nb_streams-1];
487
488 get_byte(pb); /* version */
489 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
490
491 /* component type */
492 ctype = get_le32(pb);
493 type = get_le32(pb); /* component subtype */
494
495 #ifdef DEBUG
496 printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
497 printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
498 #endif
499 #ifdef DEBUG
500 /* XXX: yeah this is ugly... */
501 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
502 if(type == MKTAG('v', 'i', 'd', 'e'))
503 puts("hdlr: vide");
504 else if(type == MKTAG('s', 'o', 'u', 'n'))
505 puts("hdlr: soun");
506 } else if(ctype == 0) { /* MP4 */
507 if(type == MKTAG('v', 'i', 'd', 'e'))
508 puts("hdlr: vide");
509 else if(type == MKTAG('s', 'o', 'u', 'n'))
510 puts("hdlr: soun");
511 else if(type == MKTAG('o', 'd', 's', 'm'))
512 puts("hdlr: odsm");
513 else if(type == MKTAG('s', 'd', 's', 'm'))
514 puts("hdlr: sdsm");
515 } else puts("hdlr: meta");
516 #endif
517
518 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
519 /* helps parsing the string hereafter... */
520 c->mp4 = 0;
521 if(type == MKTAG('v', 'i', 'd', 'e'))
522 st->codec.codec_type = CODEC_TYPE_VIDEO;
523 else if(type == MKTAG('s', 'o', 'u', 'n'))
524 st->codec.codec_type = CODEC_TYPE_AUDIO;
525 } else if(ctype == 0) { /* MP4 */
526 /* helps parsing the string hereafter... */
527 c->mp4 = 1;
528 if(type == MKTAG('v', 'i', 'd', 'e'))
529 st->codec.codec_type = CODEC_TYPE_VIDEO;
530 else if(type == MKTAG('s', 'o', 'u', 'n'))
531 st->codec.codec_type = CODEC_TYPE_AUDIO;
532 }
533 get_be32(pb); /* component manufacture */
534 get_be32(pb); /* component flags */
535 get_be32(pb); /* component flags mask */
536
537 if(atom_size <= 24)
538 return 0; /* nothing left to read */
539 /* XXX: MP4 uses a C string, not a pascal one */
540 /* component name */
541
542 if(c->mp4) {
543 /* .mp4: C string */
544 while(get_byte(pb) && (++len < (atom_size - 24)));
545 } else {
546 /* .mov: PASCAL string */
547 len = get_byte(pb);
548 buf = av_malloc(len+1);
549 get_buffer(pb, buf, len);
550 buf[len] = '\0';
551 #ifdef DEBUG
552 printf("**buf='%s'\n", buf);
553 #endif
554 av_free(buf);
555 }
556 #if 0
557 len = get_byte(pb);
558 /* XXX: use a better heuristic */
559 if(len < 32) {
560 /* assume that it is a Pascal like string */
561 buf = av_malloc(len+1);
562 get_buffer(pb, buf, len);
563 buf[len] = '\0';
564 #ifdef DEBUG
565 printf("**buf='%s'\n", buf);
566 #endif
567 av_free(buf);
568 } else {
569 /* MP4 string */
570 for(;;) {
571 if (len == 0)
572 break;
573 len = get_byte(pb);
574 }
575 }
576 #endif
577
578 return 0;
579 }
580
581 static int parse_stsd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
582 {
583 MOVContext *c;
584 int entries, size, samp_sz, frames_per_sample;
585 UINT32 format;
586 AVStream *st;
587 #ifdef DEBUG
588 print_atom("stsd", atom_type, atom_offset, atom_size);
589 #endif
590 c = (MOVContext *)param;
591 st = c->fc->streams[c->fc->nb_streams-1];
592
593 get_byte(pb); /* version */
594 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
595
596
597 entries = get_be32(pb);
598
599 while(entries--) {
600 size = get_be32(pb); /* size */
601 format = get_le32(pb); /* data format */
602
603 get_be32(pb); /* reserved */
604 get_be16(pb); /* reserved */
605 get_be16(pb); /* index */
606 /* if(format == MKTAG('m', 'p', '4', 'v')) */
607 /* st->codec.codec_type=CODEC_TYPE_VIDEO; *//* force things (XXX: was this for .mp4 ?) */
608 if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
609 st->codec.codec_tag = format;
610 st->codec.codec_id = codec_get_id(mov_video_tags, format);
611 get_be16(pb); /* version */
612 get_be16(pb); /* revision level */
613 get_be32(pb); /* vendor */
614 get_be32(pb); /* temporal quality */
615 get_be32(pb); /* spacial quality */
616 st->codec.width = get_be16(pb); /* width */
617 st->codec.height = get_be16(pb); /* height */
618 get_be32(pb); /* horiz resolution */
619 get_be32(pb); /* vert resolution */
620 get_be32(pb); /* data size, always 0 */
621 frames_per_sample = get_be16(pb); /* frame per samples */
622 #ifdef DEBUG
623 printf("frames/samples = %d\n", frames_per_sample);
624 #endif
625 url_fskip(pb, 32); /* codec name */
626
627 get_be16(pb); /* depth */
628 get_be16(pb); /* colortable id */
629 get_be16(pb); /* */
630 get_be16(pb); /* */
631
632 st->codec.sample_rate = 25 * FRAME_RATE_BASE;
633
634 if(size > 16)
635 url_fskip(pb, size-(16+24+18+32));
636 } else {
637 st->codec.codec_tag = format;
638
639 get_be16(pb); /* version */
640 get_be16(pb); /* revision level */
641 get_be32(pb); /* vendor */
642
643 st->codec.channels = get_be16(pb);/* channel count */
644 samp_sz = get_be16(pb); /* sample size */
645 #ifdef DEBUG
646 if(samp_sz != 16)
647 puts("!!! stsd: audio sample size is not 16 bit !");
648 #endif
649 st->codec.codec_id = codec_get_id(mov_audio_tags, format);
650 /* handle specific s8 codec */
651 if (st->codec.codec_id == CODEC_ID_PCM_S16BE && samp_sz == 8)
652 st->codec.codec_id = CODEC_ID_PCM_S8;
653
654 get_be16(pb); /* compression id = 0*/
655 get_be16(pb); /* packet size = 0 */
656
657 st->codec.sample_rate = ((get_be32(pb) >> 16));
658 st->codec.bit_rate = 0;
659
660 /* this is for .mp4 files */
661 if(format == MKTAG('m', 'p', '4', 'v')) { /* XXX */
662 st->codec.codec_type=CODEC_TYPE_VIDEO; /* force things */
663 st->codec.codec_id = CODEC_ID_MPEG4;
664 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
665 st->codec.bit_rate = 100000;
666 }
667 // if(format == MKTAG('m', 'p', '4', 'a')) { /* XXX */
668 // st->codec.codec_type=CODEC_TYPE_AUDIO; /* force things */
669 // st->codec.codec_id = CODEC_ID_AAC;
670 //// st->codec.frame_rate = 25 * FRAME_RATE_BASE;
671 //// st->codec.bit_rate = 100000;
672 // }
673
674 #if 0
675
676 get_be16(pb); get_be16(pb); /* */
677 get_be16(pb); /* */
678 get_be16(pb); /* */
679 get_be16(pb); /* */
680 get_be16(pb); /* */
681 #endif
682 if(size > 16)
683 url_fskip(pb, size-(16+20));
684 }
685 #ifdef DEBUG
686 printf("4CC= %c%c%c%c\n", *((char *)&format), ((char *)&format)[1], ((char *)&format)[2], ((char *)&format)[3]);
687 #endif
688 }
689 /*
690 if(len) {
691 buf = av_malloc(len+1);
692 get_buffer(pb, buf, len);
693 buf[len] = '\0';
694 puts(buf);
695 av_free(buf);
696 }
697 */
698 return 0;
699 }
700
701 static int parse_stco(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
702 {
703 MOVContext *c;
704 int entries, i;
705 AVStream *st;
706 MOVStreamContext *sc;
707 #ifdef DEBUG
708 print_atom("stco", atom_type, atom_offset, atom_size);
709 #endif
710 c = (MOVContext *)param;
711 st = c->fc->streams[c->fc->nb_streams-1];
712 sc = (MOVStreamContext *)st->priv_data;
713
714 get_byte(pb); /* version */
715 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
716
717 entries = get_be32(pb);
718 sc->chunk_count = entries;
719 sc->chunk_offsets = av_malloc(entries * sizeof(INT64));
720 if(atom_type == MKTAG('s', 't', 'c', 'o')) {
721 for(i=0; i<entries; i++) {
722 sc->chunk_offsets[i] = get_be32(pb);
723 /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
724 }
725 } else if(atom_type == MKTAG('c', 'o', '6', '4')) {
726 for(i=0; i<entries; i++) {
727 sc->chunk_offsets[i] = get_be64(pb);
728 /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
729 }
730 } else
731 return -1;
732 return 0;
733 }
734
735 static int parse_stsc(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
736 {
737 MOVContext *c;
738 int entries, i;
739 AVStream *st;
740 MOVStreamContext *sc;
741 #ifdef DEBUG
742 print_atom("stsc", atom_type, atom_offset, atom_size);
743 #endif
744 c = (MOVContext *)param;
745 st = c->fc->streams[c->fc->nb_streams-1];
746 sc = (MOVStreamContext *)st->priv_data;
747
748 get_byte(pb); /* version */
749 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
750
751 entries = get_be32(pb);
752 #ifdef DEBUG
753 printf("track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
754 #endif
755 sc->sample_to_chunk_sz = entries;
756 sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
757 for(i=0; i<entries; i++) {
758 sc->sample_to_chunk[i].first = get_be32(pb);
759 sc->sample_to_chunk[i].count = get_be32(pb);
760 sc->sample_to_chunk[i].id = get_be32(pb);
761 #ifdef DEBUG
762 /* 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); */
763 #endif
764 }
765 return 0;
766 }
767
768 static int parse_stsz(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
769 {
770 MOVContext *c;
771 int entries, i;
772 AVStream *st;
773 MOVStreamContext *sc;
774 #ifdef DEBUG
775 print_atom("stsz", atom_type, atom_offset, atom_size);
776 #endif
777 c = (MOVContext *)param;
778 st = c->fc->streams[c->fc->nb_streams-1];
779 sc = (MOVStreamContext *)st->priv_data;
780
781 get_byte(pb); /* version */
782 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
783
784 sc->sample_size = get_be32(pb);
785 entries = get_be32(pb);
786 sc->sample_count = entries;
787 #ifdef DEBUG
788 printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
789 #endif
790 if(sc->sample_size)
791 return 0; /* there isn't any table following */
792 sc->sample_sizes = av_malloc(entries * sizeof(long));
793 for(i=0; i<entries; i++) {
794 sc->sample_sizes[i] = get_be32(pb);
795 #ifdef DEBUG
796 /* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
797 #endif
798 }
799 return 0;
800 }
801
802 static int parse_stts(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
803 {
804 MOVContext *c;
805 int entries, i;
806 AVStream *st;
807 MOVStreamContext *sc;
808 #ifdef DEBUG
809 print_atom("stts", atom_type, atom_offset, atom_size);
810 #endif
811 c = (MOVContext *)param;
812 st = c->fc->streams[c->fc->nb_streams-1];
813 sc = (MOVStreamContext *)st->priv_data;
814
815 get_byte(pb); /* version */
816 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
817 entries = get_be32(pb);
818 #ifdef DEBUG
819 printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
820 #endif
821 for(i=0; i<entries; i++) {
822 int sample_duration;
823
824 get_be32(pb);
825 sample_duration = get_be32(pb);
826
827 if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
828 st->codec.frame_rate = FRAME_RATE_BASE * c->streams[c->total_streams]->time_scale;
829 if (sample_duration)
830 st->codec.frame_rate /= sample_duration;
831 #ifdef DEBUG
832 printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
833 #endif
834 }
835 }
836 return 0;
837 }
838
839 #ifdef CONFIG_ZLIB
840 static int null_read_packet(void *opaque, UINT8 *buf, int buf_size)
841 {
842 return -1;
843 }
844
845 static int parse_cmov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
846 {
847 MOVContext *c;
848 ByteIOContext ctx;
849 char *cmov_data;
850 unsigned char *moov_data; /* uncompressed data */
851 long cmov_len, moov_len;
852 int ret;
853 #ifdef DEBUG
854 print_atom("cmov", atom_type, atom_offset, atom_size);
855 #endif
856 c = (MOVContext *)param;
857
858 get_be32(pb); /* dcom atom */
859 if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
860 return -1;
861 if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
862 puts("unknown compression for cmov atom !");
863 return -1;
864 }
865 get_be32(pb); /* cmvd atom */
866 if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
867 return -1;
868 moov_len = get_be32(pb); /* uncompressed size */
869 cmov_len = atom_size - 6 * 4;
870
871 cmov_data = av_malloc(cmov_len);
872 if (!cmov_data)
873 return -1;
874 moov_data = av_malloc(moov_len);
875 if (!moov_data) {
876 av_free(cmov_data);
877 return -1;
878 }
879 get_buffer(pb, cmov_data, cmov_len);
880 if(uncompress (moov_data, &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
881 return -1;
882 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
883 return -1;
884 ctx.buf_end = ctx.buffer + moov_len;
885 ret = parse_default(parse_table, &ctx, MKTAG( 'm', 'o', 'o', 'v' ), 0, moov_len, param);
886 av_free(moov_data);
887 av_free(cmov_data);
888 return ret;
889 }
890 #endif
891
892 static const MOVParseTableEntry mov_default_parse_table[] = {
893 /* mp4 atoms */
894 { MKTAG( 'm', 'p', '4', 'a' ), parse_default },
895 { MKTAG( 'c', 'o', '6', '4' ), parse_stco },
896 { MKTAG( 's', 't', 'c', 'o' ), parse_stco },
897 { MKTAG( 'c', 'r', 'h', 'd' ), parse_default },
898 { MKTAG( 'c', 't', 't', 's' ), parse_leaf },
899 { MKTAG( 'c', 'p', 'r', 't' ), parse_default },
900 { MKTAG( 'u', 'r', 'l', ' ' ), parse_leaf },
901 { MKTAG( 'u', 'r', 'n', ' ' ), parse_leaf },
902 { MKTAG( 'd', 'i', 'n', 'f' ), parse_default },
903 { MKTAG( 'd', 'r', 'e', 'f' ), parse_leaf },
904 { MKTAG( 's', 't', 'd', 'p' ), parse_default },
905 { MKTAG( 'e', 's', 'd', 's' ), parse_default },
906 { MKTAG( 'e', 'd', 't', 's' ), parse_default },
907 { MKTAG( 'e', 'l', 's', 't' ), parse_leaf },
908 { MKTAG( 'u', 'u', 'i', 'd' ), parse_default },
909 { MKTAG( 'f', 'r', 'e', 'e' ), parse_leaf },
910 { MKTAG( 'h', 'd', 'l', 'r' ), parse_hdlr },
911 { MKTAG( 'h', 'm', 'h', 'd' ), parse_leaf },
912 { MKTAG( 'h', 'i', 'n', 't' ), parse_leaf },
913 { MKTAG( 'n', 'm', 'h', 'd' ), parse_leaf },
914 { MKTAG( 'm', 'p', '4', 's' ), parse_default },
915 { MKTAG( 'm', 'd', 'i', 'a' ), parse_default },
916 { MKTAG( 'm', 'd', 'a', 't' ), parse_mdat },
917 { MKTAG( 'm', 'd', 'h', 'd' ), parse_mdhd },
918 { MKTAG( 'm', 'i', 'n', 'f' ), parse_default },
919 { MKTAG( 'm', 'o', 'o', 'v' ), parse_moov },
920 { MKTAG( 'm', 'v', 'h', 'd' ), parse_mvhd },
921 { MKTAG( 'i', 'o', 'd', 's' ), parse_leaf },
922 { MKTAG( 'o', 'd', 'h', 'd' ), parse_default },
923 { MKTAG( 'm', 'p', 'o', 'd' ), parse_leaf },
924 { MKTAG( 's', 't', 's', 'd' ), parse_stsd },
925 { MKTAG( 's', 't', 's', 'z' ), parse_stsz },
926 { MKTAG( 's', 't', 'b', 'l' ), parse_default },
927 { MKTAG( 's', 't', 's', 'c' ), parse_stsc },
928 { MKTAG( 's', 'd', 'h', 'd' ), parse_default },
929 { MKTAG( 's', 't', 's', 'h' ), parse_default },
930 { MKTAG( 's', 'k', 'i', 'p' ), parse_default },
931 { MKTAG( 's', 'm', 'h', 'd' ), parse_leaf },
932 { MKTAG( 'd', 'p', 'n', 'd' ), parse_leaf },
933 { MKTAG( 's', 't', 's', 's' ), parse_leaf },
934 { MKTAG( 's', 't', 't', 's' ), parse_stts },
935 { MKTAG( 't', 'r', 'a', 'k' ), parse_trak },
936 { MKTAG( 't', 'k', 'h', 'd' ), parse_tkhd },
937 { MKTAG( 't', 'r', 'e', 'f' ), parse_default }, /* not really */
938 { MKTAG( 'u', 'd', 't', 'a' ), parse_leaf },
939 { MKTAG( 'v', 'm', 'h', 'd' ), parse_leaf },
940 { MKTAG( 'm', 'p', '4', 'v' ), parse_default },
941 /* extra mp4 */
942 { MKTAG( 'M', 'D', 'E', 'S' ), parse_leaf },
943 /* QT atoms */
944 { MKTAG( 'c', 'h', 'a', 'p' ), parse_leaf },
945 { MKTAG( 'c', 'l', 'i', 'p' ), parse_default },
946 { MKTAG( 'c', 'r', 'g', 'n' ), parse_leaf },
947 { MKTAG( 'k', 'm', 'a', 't' ), parse_leaf },
948 { MKTAG( 'm', 'a', 't', 't' ), parse_default },
949 { MKTAG( 'r', 'd', 'r', 'f' ), parse_leaf },
950 { MKTAG( 'r', 'm', 'd', 'a' ), parse_default },
951 { MKTAG( 'r', 'm', 'd', 'r' ), parse_leaf },
952 //{ MKTAG( 'r', 'm', 'q', 'u' ), parse_leaf },
953 { MKTAG( 'r', 'm', 'r', 'a' ), parse_default },
954 { MKTAG( 's', 'c', 'p', 't' ), parse_leaf },
955 { MKTAG( 's', 'y', 'n', 'c' ), parse_leaf },
956 { MKTAG( 's', 's', 'r', 'c' ), parse_leaf },
957 { MKTAG( 't', 'c', 'm', 'd' ), parse_leaf },
958 { MKTAG( 'w', 'i', 'd', 'e' ), parse_wide }, /* place holder */
959 #ifdef CONFIG_ZLIB
960 { MKTAG( 'c', 'm', 'o', 'v' ), parse_cmov },
961 #else
962 { MKTAG( 'c', 'm', 'o', 'v' ), parse_leaf },
963 #endif
964 { 0L, parse_leaf }
965 };
966
967 static void mov_free_stream_context(MOVStreamContext *sc)
968 {
969 if(sc) {
970 av_free(sc->chunk_offsets);
971 av_free(sc->sample_to_chunk);
972 av_free(sc);
973 }
974 }
975
976 /* XXX: is it suffisant ? */
977 static int mov_probe(AVProbeData *p)
978 {
979 int offset;
980
981 /* check file header */
982 if (p->buf_size <= 12)
983 return 0;
984 if ((p->buf[4] == 'm' && p->buf[5] == 'o' &&
985 p->buf[6] == 'o' && p->buf[7] == 'v') ||
986 (p->buf[4] == 'w' && p->buf[5] == 'i' &&
987 p->buf[6] == 'd' && p->buf[7] == 'e') ||
988 (p->buf[4] == 'f' && p->buf[5] == 'r' &&
989 p->buf[6] == 'e' && p->buf[7] == 'e') ||
990 (p->buf[4] == 'm' && p->buf[5] == 'd' &&
991 p->buf[6] == 'a' && p->buf[7] == 't'))
992 return AVPROBE_SCORE_MAX;
993 if (p->buf[4] == 'f' && p->buf[5] == 't' &&
994 p->buf[6] == 'y' && p->buf[7] == 'p') {
995 offset = p->buf[0]; offset <<= 8;
996 offset |= p->buf[1]; offset <<= 8;
997 offset |= p->buf[2]; offset <<= 8;
998 offset |= p->buf[3];
999 if (offset > p->buf_size)
1000 return 0;
1001 if ((p->buf[offset+4] == 'm' && p->buf[offset+5] == 'o' &&
1002 p->buf[offset+6] == 'o' && p->buf[offset+7] == 'v') ||
1003 (p->buf[offset+4] == 'w' && p->buf[offset+5] == 'i' &&
1004 p->buf[offset+6] == 'd' && p->buf[offset+7] == 'e') ||
1005 (p->buf[offset+4] == 'm' && p->buf[offset+5] == 'd' &&
1006 p->buf[offset+6] == 'a' && p->buf[offset+7] == 't'))
1007 return AVPROBE_SCORE_MAX;
1008
1009 }
1010 return 0;
1011 }
1012
1013 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1014 {
1015 MOVContext *mov = s->priv_data;
1016 ByteIOContext *pb = &s->pb;
1017 int i, j, nb, err;
1018 INT64 size;
1019
1020 mov->fc = s;
1021 #if 0
1022 /* XXX: I think we should auto detect */
1023 if(s->iformat->name[1] == 'p')
1024 mov->mp4 = 1;
1025 #endif
1026 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1027 size = url_filesize(url_fileno(pb));
1028 else
1029 size = 0x7FFFFFFFFFFFFFFF;
1030
1031 #ifdef DEBUG
1032 printf("filesz=%Ld\n", size);
1033 #endif
1034
1035 /* check MOV header */
1036 err = parse_default(mov_default_parse_table, pb, 0L, 0LL, size, mov);
1037 if(err<0 || (!mov->found_moov || !mov->found_mdat)) {
1038 puts("header not found !!!");
1039 exit(1);
1040 }
1041 #ifdef DEBUG
1042 printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
1043 #endif
1044 /* some cleanup : make sure we are on the mdat atom */
1045 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1046 url_fseek(pb, mov->mdat_offset, SEEK_SET);
1047
1048 mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
1049
1050 #ifdef DEBUG
1051 printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
1052 #endif
1053
1054 #ifdef DEBUG
1055 printf("streams= %d\n", s->nb_streams);
1056 #endif
1057 mov->total_streams = nb = s->nb_streams;
1058
1059 #if 1
1060 for(i=0; i<s->nb_streams;) {
1061 if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
1062 av_free(s->streams[i]);
1063 for(j=i+1; j<s->nb_streams; j++)
1064 s->streams[j-1] = s->streams[j];
1065 s->nb_streams--;
1066 } else
1067 i++;
1068 }
1069 for(i=0; i<s->nb_streams;i++) {
1070 MOVStreamContext *sc;
1071 sc = (MOVStreamContext *)s->streams[i]->priv_data;
1072 sc->ffindex = i;
1073 sc->is_ff_stream = 1;
1074 }
1075 #endif
1076 #ifdef DEBUG
1077 printf("real streams= %d\n", s->nb_streams);
1078 #endif
1079 return 0;
1080 }
1081
1082 /* Yes, this is ugly... I didn't write the specs of QT :p */
1083 /* XXX:remove useless commented code sometime */
1084 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1085 {
1086 MOVContext *mov = s->priv_data;
1087 INT64 offset = 0x0FFFFFFFFFFFFFFF;
1088 int i;
1089 int st_id = 0, size;
1090 size = 0x0FFFFFFF;
1091
1092 #ifdef MOV_SPLIT_CHUNKS
1093 if (mov->partial) {
1094
1095 int idx;
1096
1097 st_id = mov->partial - 1;
1098 idx = mov->streams[st_id]->sample_to_chunk_index;
1099 if (idx < 0) return 0;
1100 size = mov->streams[st_id]->sample_sizes[mov->streams[st_id]->current_sample];
1101
1102 mov->streams[st_id]->current_sample++;
1103 mov->streams[st_id]->left_in_chunk--;
1104
1105 if(mov->streams[st_id]->left_in_chunk <= 0)
1106 mov->partial = 0;
1107 offset = mov->next_chunk_offset;
1108 /* extract the sample */
1109
1110 goto readchunk;
1111 }
1112 #endif
1113
1114 again:
1115 for(i=0; i<mov->total_streams; i++) {
1116 if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
1117 && (mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] < offset)) {
1118 st_id = i;
1119 offset = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk];
1120 }
1121 }
1122 mov->streams[st_id]->next_chunk++;
1123 if(offset==0x0FFFFFFFFFFFFFFF)
1124 return -1;
1125
1126 if(mov->next_chunk_offset < offset) { /* some meta data */
1127 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1128 mov->next_chunk_offset = offset;
1129 }
1130
1131 //printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
1132 if(!mov->streams[st_id]->is_ff_stream) {
1133 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1134 mov->next_chunk_offset = offset;
1135 offset = 0x0FFFFFFFFFFFFFFF;
1136 goto again;
1137 }
1138
1139 /* now get the chunk size... */
1140
1141 for(i=0; i<mov->total_streams; i++) {
1142 if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
1143 && ((mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset) < size)) {
1144 size = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset;
1145 }
1146 }
1147 #ifdef MOV_SPLIT_CHUNKS
1148 /* split chunks into samples */
1149 if(mov->streams[st_id]->sample_size == 0) {
1150 int idx;
1151 idx = mov->streams[st_id]->sample_to_chunk_index;
1152 if ((idx + 1 < mov->streams[st_id]->sample_to_chunk_sz)
1153 && (mov->streams[st_id]->next_chunk >= mov->streams[st_id]->sample_to_chunk[idx + 1].first))
1154 idx++;
1155 mov->streams[st_id]->sample_to_chunk_index = idx;
1156 if(idx >= 0 && mov->streams[st_id]->sample_to_chunk[idx].count != 1) {
1157 mov->partial = st_id+1;
1158 /* we'll have to get those samples before next chunk */
1159 mov->streams[st_id]->left_in_chunk = (mov->streams[st_id]->sample_to_chunk[idx].count) - 1;
1160 size = mov->streams[st_id]->sample_sizes[mov->streams[st_id]->current_sample];
1161 }
1162
1163 mov->streams[st_id]->current_sample++;
1164 }
1165 #endif
1166
1167 readchunk:
1168
1169 //printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size);
1170 if(size == 0x0FFFFFFF)
1171 size = mov->mdat_size + mov->mdat_offset - offset;
1172 if(size < 0)
1173 return -1;
1174 if(size == 0)
1175 return -1;
1176 av_new_packet(pkt, size);
1177 pkt->stream_index = mov->streams[st_id]->ffindex;
1178
1179 get_buffer(&s->pb, pkt->data, pkt->size);
1180
1181 #ifdef DEBUG
1182 /*
1183 printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
1184 for(i=0; i<8; i++)
1185 printf("%02x ", pkt->data[i]);
1186 for(i=0; i<8; i++)
1187 printf("%c ", (pkt->data[i]) & 0x7F);
1188 puts("");
1189 */
1190 #endif
1191
1192 mov->next_chunk_offset = offset + size;
1193
1194 return 0;
1195 }
1196
1197 static int mov_read_close(AVFormatContext *s)
1198 {
1199 int i;
1200 MOVContext *mov = s->priv_data;
1201 for(i=0; i<mov->total_streams; i++)
1202 mov_free_stream_context(mov->streams[i]);
1203 for(i=0; i<s->nb_streams; i++)
1204 av_free(s->streams[i]);
1205 return 0;
1206 }
1207
1208 static AVInputFormat mov_iformat = {
1209 "mov",
1210 "QuickTime/MPEG4 format",
1211 sizeof(MOVContext),
1212 mov_probe,
1213 mov_read_header,
1214 mov_read_packet,
1215 mov_read_close,
1216 };
1217
1218 int mov_init(void)
1219 {
1220 av_register_input_format(&mov_iformat);
1221 return 0;
1222 }