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