Mercurial > libavformat.hg
annotate mov.c @ 92:5a4b5f03d13e libavformat
OpenDML AVI > 2Gb support patch by (Roman Shaposhnick <rvs at sun dot com>)
author | michaelni |
---|---|
date | Wed, 26 Mar 2003 09:28:52 +0000 |
parents | 25062c9b1f86 |
children | 0865b0aaa2af |
rev | line source |
---|---|
0 | 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; | |
65 | 64 void print_atom(const char *str, uint32_t type, uint64_t offset, uint64_t size) |
0 | 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_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */ | |
91 /* { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */ | |
92 /* Graphics */ | |
93 /* Animation */ | |
94 /* Apple video */ | |
95 /* Kodak Photo CD */ | |
96 { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */ | |
97 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */ | |
98 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */ | |
99 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */ | |
100 /* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */ | |
101 /* Sorenson video */ | |
102 { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */ | |
103 { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */ | |
104 { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/ | |
105 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') }, | |
106 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */ | |
107 /* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */ | |
108 { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */ | |
109 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */ | |
110 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */ | |
111 { 0, 0 }, | |
112 }; | |
113 | |
114 static const CodecTag mov_audio_tags[] = { | |
115 /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */ | |
116 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */ | |
117 { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') }, /* 8 bits */ | |
118 { CODEC_ID_PCM_U8, 0x20776172 }, /* 8 bits unsigned */ | |
119 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */ | |
120 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */ | |
121 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */ | |
122 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */ | |
123 { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */ | |
124 { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */ | |
125 | |
126 { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */ | |
127 { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */ | |
128 { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */ | |
129 /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */ | |
130 /* MP4 tags */ | |
131 /* { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, *//* MPEG 4 AAC or audio ? */ | |
132 /* The standard for mpeg4 audio is still not normalised AFAIK anyway */ | |
133 { 0, 0 }, | |
134 }; | |
135 | |
136 /* the QuickTime file format is quite convoluted... | |
137 * it has lots of index tables, each indexing something in another one... | |
138 * Here we just use what is needed to read the chunks | |
139 */ | |
140 | |
141 typedef struct MOV_sample_to_chunk_tbl { | |
142 long first; | |
143 long count; | |
144 long id; | |
145 } MOV_sample_to_chunk_tbl; | |
146 | |
147 typedef struct MOVStreamContext { | |
148 int ffindex; /* the ffmpeg stream id */ | |
149 int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */ | |
150 long next_chunk; | |
151 long chunk_count; | |
65 | 152 int64_t *chunk_offsets; |
0 | 153 long sample_to_chunk_sz; |
154 MOV_sample_to_chunk_tbl *sample_to_chunk; | |
155 long sample_to_chunk_index; | |
156 long sample_size; | |
157 long sample_count; | |
158 long *sample_sizes; | |
159 long time_scale; | |
160 long current_sample; | |
161 long left_in_chunk; /* how many samples before next chunk */ | |
162 /* specific MPEG4 header which is added at the beginning of the stream */ | |
163 int header_len; | |
164 uint8_t *header_data; | |
165 } MOVStreamContext; | |
166 | |
167 typedef struct MOVContext { | |
168 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) */ | |
169 AVFormatContext *fc; | |
170 long time_scale; | |
171 int found_moov; /* when both 'moov' and 'mdat' sections has been found */ | |
172 int found_mdat; /* we suppose we have enough data to read the file */ | |
65 | 173 int64_t mdat_size; |
174 int64_t mdat_offset; | |
0 | 175 int total_streams; |
176 /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio | |
177 * but we need the info to be able to skip data from those streams in the 'mdat' section | |
178 */ | |
179 MOVStreamContext *streams[MAX_STREAMS]; | |
180 | |
65 | 181 int64_t next_chunk_offset; |
0 | 182 int partial; /* != 0 : there is still to read in the current chunk (=id of the stream + 1) */ |
183 } MOVContext; | |
184 | |
185 | |
186 struct MOVParseTableEntry; | |
187 | |
188 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */ | |
189 | |
190 /* those functions parse an atom */ | |
191 /* return code: | |
192 1: found what I wanted, exit | |
193 0: continue to parse next atom | |
194 -1: error occured, exit | |
195 */ | |
196 typedef int (*mov_parse_function)(const struct MOVParseTableEntry *parse_table, | |
197 ByteIOContext *pb, | |
65 | 198 uint32_t atom_type, |
199 int64_t atom_offset, /* after the size and type field (and eventually the extended size) */ | |
200 int64_t atom_size, /* total size (excluding the size and type fields) */ | |
0 | 201 void *param); |
202 | |
203 /* links atom IDs to parse functions */ | |
204 typedef struct MOVParseTableEntry { | |
65 | 205 uint32_t type; |
0 | 206 mov_parse_function func; |
207 } MOVParseTableEntry; | |
208 | |
65 | 209 static int parse_leaf(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 210 { |
211 #ifdef DEBUG | |
212 print_atom("leaf", atom_type, atom_offset, atom_size); | |
213 #endif | |
214 if(atom_size>1) | |
215 url_fskip(pb, atom_size); | |
216 /* url_seek(pb, atom_offset+atom_size, SEEK_SET); */ | |
217 return 0; | |
218 } | |
219 | |
220 | |
65 | 221 static int parse_default(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 222 { |
65 | 223 uint32_t type, foo=0; |
224 uint64_t offset, size; | |
225 uint64_t total_size = 0; | |
0 | 226 int i; |
227 int err = 0; | |
228 foo=0; | |
229 #ifdef DEBUG | |
230 print_atom("default", atom_type, atom_offset, atom_size); | |
231 debug_indent++; | |
232 #endif | |
233 | |
234 offset = atom_offset; | |
235 | |
236 if(atom_size < 0) | |
237 atom_size = 0x0FFFFFFFFFFFFFFF; | |
238 while((total_size < atom_size) && !url_feof(pb) && !err) { | |
239 size=atom_size; | |
240 type=0L; | |
241 if(atom_size >= 8) { | |
242 size = get_be32(pb); | |
243 type = get_le32(pb); | |
244 } | |
245 total_size += 8; | |
246 offset+=8; | |
247 // printf("type: %08lx sz: %08lx", type, size); | |
248 if(size == 1) { /* 64 bit extended size */ | |
249 size = get_be64(pb); | |
250 offset+=8; | |
251 total_size+=8; | |
252 size-=8; | |
253 } | |
254 if(size == 0) | |
255 size = atom_size - total_size; | |
256 size-=8; | |
257 for(i=0; parse_table[i].type != 0L && parse_table[i].type != type; i++); | |
258 | |
259 // printf(" i=%ld\n", i); | |
260 if (parse_table[i].type == 0) { /* skip leaf atoms data */ | |
261 // url_seek(pb, atom_offset+atom_size, SEEK_SET); | |
262 #ifdef DEBUG | |
263 print_atom("unknown", type, offset, size); | |
264 #endif | |
265 url_fskip(pb, size); | |
266 } else | |
267 err = (parse_table[i].func)(parse_table, pb, type, offset, size, param); | |
268 | |
269 offset+=size; | |
270 total_size+=size; | |
271 } | |
272 | |
273 #ifdef DEBUG | |
274 debug_indent--; | |
275 #endif | |
276 return err; | |
277 } | |
278 | |
65 | 279 static int parse_mvhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 280 { |
281 MOVContext *c; | |
282 #ifdef DEBUG | |
283 print_atom("mvhd", atom_type, atom_offset, atom_size); | |
284 #endif | |
285 c = (MOVContext *)param; | |
286 | |
287 get_byte(pb); /* version */ | |
288 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
289 | |
290 get_be32(pb); /* creation time */ | |
291 get_be32(pb); /* modification time */ | |
292 c->time_scale = get_be32(pb); /* time scale */ | |
293 #ifdef DEBUG | |
294 printf("time scale = %li\n", c->time_scale); | |
295 #endif | |
296 get_be32(pb); /* duration */ | |
297 get_be32(pb); /* preferred scale */ | |
298 | |
299 get_be16(pb); /* preferred volume */ | |
300 | |
301 url_fskip(pb, 10); /* reserved */ | |
302 | |
303 url_fskip(pb, 36); /* display matrix */ | |
304 | |
305 get_be32(pb); /* preview time */ | |
306 get_be32(pb); /* preview duration */ | |
307 get_be32(pb); /* poster time */ | |
308 get_be32(pb); /* selection time */ | |
309 get_be32(pb); /* selection duration */ | |
310 get_be32(pb); /* current time */ | |
311 get_be32(pb); /* next track ID */ | |
312 | |
313 return 0; | |
314 } | |
315 | |
316 /* this atom should contain all header atoms */ | |
65 | 317 static int parse_moov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 318 { |
319 int err; | |
320 MOVContext *c; | |
321 #ifdef DEBUG | |
322 print_atom("moov", atom_type, atom_offset, atom_size); | |
323 #endif | |
324 c = (MOVContext *)param; | |
325 | |
326 err = parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param); | |
327 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */ | |
328 /* so we don't parse the whole file if over a network */ | |
329 c->found_moov=1; | |
330 if(c->found_mdat) | |
331 return 1; /* found both, just go */ | |
332 return 0; /* now go for mdat */ | |
333 } | |
334 | |
335 /* this atom contains actual media data */ | |
65 | 336 static int parse_mdat(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 337 { |
338 MOVContext *c; | |
339 #ifdef DEBUG | |
340 print_atom("mdat", atom_type, atom_offset, atom_size); | |
341 #endif | |
342 c = (MOVContext *)param; | |
343 | |
344 if(atom_size == 0) /* wrong one (MP4) */ | |
345 return 0; | |
346 c->found_mdat=1; | |
347 c->mdat_offset = atom_offset; | |
348 c->mdat_size = atom_size; | |
349 if(c->found_moov) | |
350 return 1; /* found both, just go */ | |
351 url_fskip(pb, atom_size); | |
352 return 0; /* now go for moov */ | |
353 } | |
354 | |
355 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */ | |
356 /* like the files created with Adobe Premiere 5.0, for samples see */ | |
357 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */ | |
65 | 358 static int parse_wide(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 359 { |
360 int err; | |
65 | 361 uint32_t type; |
0 | 362 #ifdef DEBUG |
363 print_atom("wide", atom_type, atom_offset, atom_size); | |
364 debug_indent++; | |
365 #endif | |
366 if (atom_size < 8) | |
367 return 0; /* continue */ | |
368 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */ | |
369 url_fskip(pb, atom_size - 4); | |
370 return 0; | |
371 } | |
372 type = get_le32(pb); | |
373 if (type != MKTAG('m', 'd', 'a', 't')) { | |
374 url_fskip(pb, atom_size - 8); | |
375 return 0; | |
376 } | |
377 err = parse_mdat(parse_table, pb, type, atom_offset + 8, atom_size - 8, param); | |
378 #ifdef DEBUG | |
379 debug_indent--; | |
380 #endif | |
381 return err; | |
382 } | |
383 | |
65 | 384 static int parse_trak(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 385 { |
386 MOVContext *c; | |
387 AVStream *st; | |
388 MOVStreamContext *sc; | |
389 #ifdef DEBUG | |
390 print_atom("trak", atom_type, atom_offset, atom_size); | |
391 #endif | |
392 | |
393 c = (MOVContext *)param; | |
394 st = av_new_stream(c->fc, c->fc->nb_streams); | |
395 if (!st) return -2; | |
396 sc = av_malloc(sizeof(MOVStreamContext)); | |
397 sc->sample_to_chunk_index = -1; | |
398 st->priv_data = sc; | |
399 st->codec.codec_type = CODEC_TYPE_MOV_OTHER; | |
400 c->streams[c->fc->nb_streams-1] = sc; | |
401 return parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param); | |
402 } | |
403 | |
65 | 404 static int parse_tkhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 405 { |
406 MOVContext *c; | |
407 AVStream *st; | |
408 #ifdef DEBUG | |
409 print_atom("tkhd", atom_type, atom_offset, atom_size); | |
410 #endif | |
411 | |
412 c = (MOVContext *)param; | |
413 st = c->fc->streams[c->fc->nb_streams-1]; | |
414 | |
415 get_byte(pb); /* version */ | |
416 | |
417 get_byte(pb); get_byte(pb); | |
418 get_byte(pb); /* flags */ | |
419 /* | |
420 MOV_TRACK_ENABLED 0x0001 | |
421 MOV_TRACK_IN_MOVIE 0x0002 | |
422 MOV_TRACK_IN_PREVIEW 0x0004 | |
423 MOV_TRACK_IN_POSTER 0x0008 | |
424 */ | |
425 | |
426 get_be32(pb); /* creation time */ | |
427 get_be32(pb); /* modification time */ | |
428 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/ | |
429 get_be32(pb); /* reserved */ | |
430 get_be32(pb); /* duration */ | |
431 get_be32(pb); /* reserved */ | |
432 get_be32(pb); /* reserved */ | |
433 | |
434 get_be16(pb); /* layer */ | |
435 get_be16(pb); /* alternate group */ | |
436 get_be16(pb); /* volume */ | |
437 get_be16(pb); /* reserved */ | |
438 | |
439 url_fskip(pb, 36); /* display matrix */ | |
440 | |
441 /* those are fixed-point */ | |
442 st->codec.width = get_be32(pb) >> 16; /* track width */ | |
443 st->codec.height = get_be32(pb) >> 16; /* track height */ | |
444 | |
445 return 0; | |
446 } | |
447 | |
65 | 448 static int parse_mdhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 449 { |
450 MOVContext *c; | |
451 AVStream *st; | |
452 #ifdef DEBUG | |
453 print_atom("mdhd", atom_type, atom_offset, atom_size); | |
454 #endif | |
455 | |
456 c = (MOVContext *)param; | |
457 st = c->fc->streams[c->fc->nb_streams-1]; | |
458 | |
459 get_byte(pb); /* version */ | |
460 | |
461 get_byte(pb); get_byte(pb); | |
462 get_byte(pb); /* flags */ | |
463 | |
464 get_be32(pb); /* creation time */ | |
465 get_be32(pb); /* modification time */ | |
466 | |
467 c->streams[c->total_streams]->time_scale = get_be32(pb); | |
468 | |
469 #ifdef DEBUG | |
470 printf("track[%i].time_scale = %li\n", c->fc->nb_streams-1, c->streams[c->total_streams]->time_scale); /* time scale */ | |
471 #endif | |
472 get_be32(pb); /* duration */ | |
473 | |
474 get_be16(pb); /* language */ | |
475 get_be16(pb); /* quality */ | |
476 | |
477 return 0; | |
478 } | |
479 | |
65 | 480 static int parse_hdlr(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 481 { |
482 MOVContext *c; | |
483 int len = 0; | |
484 char *buf; | |
65 | 485 uint32_t type; |
0 | 486 AVStream *st; |
65 | 487 uint32_t ctype; |
0 | 488 #ifdef DEBUG |
489 print_atom("hdlr", atom_type, atom_offset, atom_size); | |
490 #endif | |
491 c = (MOVContext *)param; | |
492 st = c->fc->streams[c->fc->nb_streams-1]; | |
493 | |
494 get_byte(pb); /* version */ | |
495 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
496 | |
497 /* component type */ | |
498 ctype = get_le32(pb); | |
499 type = get_le32(pb); /* component subtype */ | |
500 | |
501 #ifdef DEBUG | |
502 printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype); | |
503 printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]); | |
504 #endif | |
505 #ifdef DEBUG | |
506 /* XXX: yeah this is ugly... */ | |
507 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */ | |
508 if(type == MKTAG('v', 'i', 'd', 'e')) | |
509 puts("hdlr: vide"); | |
510 else if(type == MKTAG('s', 'o', 'u', 'n')) | |
511 puts("hdlr: soun"); | |
512 } else if(ctype == 0) { /* MP4 */ | |
513 if(type == MKTAG('v', 'i', 'd', 'e')) | |
514 puts("hdlr: vide"); | |
515 else if(type == MKTAG('s', 'o', 'u', 'n')) | |
516 puts("hdlr: soun"); | |
517 else if(type == MKTAG('o', 'd', 's', 'm')) | |
518 puts("hdlr: odsm"); | |
519 else if(type == MKTAG('s', 'd', 's', 'm')) | |
520 puts("hdlr: sdsm"); | |
521 } else puts("hdlr: meta"); | |
522 #endif | |
523 | |
524 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */ | |
525 /* helps parsing the string hereafter... */ | |
526 c->mp4 = 0; | |
527 if(type == MKTAG('v', 'i', 'd', 'e')) | |
528 st->codec.codec_type = CODEC_TYPE_VIDEO; | |
529 else if(type == MKTAG('s', 'o', 'u', 'n')) | |
530 st->codec.codec_type = CODEC_TYPE_AUDIO; | |
531 } else if(ctype == 0) { /* MP4 */ | |
532 /* helps parsing the string hereafter... */ | |
533 c->mp4 = 1; | |
534 if(type == MKTAG('v', 'i', 'd', 'e')) | |
535 st->codec.codec_type = CODEC_TYPE_VIDEO; | |
536 else if(type == MKTAG('s', 'o', 'u', 'n')) | |
537 st->codec.codec_type = CODEC_TYPE_AUDIO; | |
538 } | |
539 get_be32(pb); /* component manufacture */ | |
540 get_be32(pb); /* component flags */ | |
541 get_be32(pb); /* component flags mask */ | |
542 | |
543 if(atom_size <= 24) | |
544 return 0; /* nothing left to read */ | |
545 /* XXX: MP4 uses a C string, not a pascal one */ | |
546 /* component name */ | |
547 | |
548 if(c->mp4) { | |
549 /* .mp4: C string */ | |
550 while(get_byte(pb) && (++len < (atom_size - 24))); | |
551 } else { | |
552 /* .mov: PASCAL string */ | |
553 len = get_byte(pb); | |
554 buf = av_malloc(len+1); | |
555 get_buffer(pb, buf, len); | |
556 buf[len] = '\0'; | |
557 #ifdef DEBUG | |
558 printf("**buf='%s'\n", buf); | |
559 #endif | |
560 av_free(buf); | |
561 } | |
562 #if 0 | |
563 len = get_byte(pb); | |
564 /* XXX: use a better heuristic */ | |
565 if(len < 32) { | |
566 /* assume that it is a Pascal like string */ | |
567 buf = av_malloc(len+1); | |
568 get_buffer(pb, buf, len); | |
569 buf[len] = '\0'; | |
570 #ifdef DEBUG | |
571 printf("**buf='%s'\n", buf); | |
572 #endif | |
573 av_free(buf); | |
574 } else { | |
575 /* MP4 string */ | |
576 for(;;) { | |
577 if (len == 0) | |
578 break; | |
579 len = get_byte(pb); | |
580 } | |
581 } | |
582 #endif | |
583 | |
584 return 0; | |
585 } | |
586 | |
587 static int mp4_read_descr_len(ByteIOContext *pb) | |
588 { | |
589 int c, len, count; | |
590 | |
591 len = 0; | |
592 count = 0; | |
593 for(;;) { | |
594 c = get_byte(pb); | |
595 len = (len << 7) | (c & 0x7f); | |
596 if ((c & 0x80) == 0) | |
597 break; | |
598 if (++count == 4) | |
599 break; | |
600 } | |
601 return len; | |
602 } | |
603 | |
604 static int mp4_read_descr(ByteIOContext *pb, int *tag) | |
605 { | |
606 int len; | |
607 *tag = get_byte(pb); | |
608 len = mp4_read_descr_len(pb); | |
609 #ifdef DEBUG | |
610 printf("MPEG4 description: tag=0x%02x len=%d\n", *tag, len); | |
611 #endif | |
612 return len; | |
613 } | |
614 | |
65 | 615 static int parse_stsd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 616 { |
617 MOVContext *c; | |
618 int entries, size, samp_sz, frames_per_sample, id; | |
65 | 619 uint32_t format; |
0 | 620 AVStream *st; |
621 MOVStreamContext *sc; | |
622 #ifdef DEBUG | |
623 print_atom("stsd", atom_type, atom_offset, atom_size); | |
624 #endif | |
625 c = (MOVContext *)param; | |
626 st = c->fc->streams[c->fc->nb_streams-1]; | |
627 sc = (MOVStreamContext *)st->priv_data; | |
628 | |
629 get_byte(pb); /* version */ | |
630 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
631 | |
632 entries = get_be32(pb); | |
633 | |
634 while(entries--) { | |
635 size = get_be32(pb); /* size */ | |
636 format = get_le32(pb); /* data format */ | |
637 | |
638 get_be32(pb); /* reserved */ | |
639 get_be16(pb); /* reserved */ | |
640 get_be16(pb); /* index */ | |
641 | |
642 /* for MPEG4: set codec type by looking for it */ | |
643 id = codec_get_id(mov_video_tags, format); | |
644 if (id >= 0) { | |
645 AVCodec *codec; | |
646 codec = avcodec_find_decoder(id); | |
647 if (codec) | |
648 st->codec.codec_type = codec->type; | |
649 } | |
650 #ifdef DEBUG | |
651 printf("size=%d 4CC= %c%c%c%c codec_type=%d\n", | |
652 size, | |
653 (format >> 0) & 0xff, | |
654 (format >> 8) & 0xff, | |
655 (format >> 16) & 0xff, | |
656 (format >> 24) & 0xff, | |
657 st->codec.codec_type); | |
658 #endif | |
659 if(st->codec.codec_type==CODEC_TYPE_VIDEO) { | |
660 st->codec.codec_tag = format; | |
661 st->codec.codec_id = codec_get_id(mov_video_tags, format); | |
662 get_be16(pb); /* version */ | |
663 get_be16(pb); /* revision level */ | |
664 get_be32(pb); /* vendor */ | |
665 get_be32(pb); /* temporal quality */ | |
666 get_be32(pb); /* spacial quality */ | |
667 st->codec.width = get_be16(pb); /* width */ | |
668 st->codec.height = get_be16(pb); /* height */ | |
669 #if 1 | |
670 if (st->codec.codec_id == CODEC_ID_MPEG4) { | |
671 /* in some MPEG4 the width/height are not correct, so | |
672 we ignore this info */ | |
673 st->codec.width = 0; | |
674 st->codec.height = 0; | |
675 } | |
676 #endif | |
677 get_be32(pb); /* horiz resolution */ | |
678 get_be32(pb); /* vert resolution */ | |
679 get_be32(pb); /* data size, always 0 */ | |
680 frames_per_sample = get_be16(pb); /* frame per samples */ | |
681 #ifdef DEBUG | |
682 printf("frames/samples = %d\n", frames_per_sample); | |
683 #endif | |
684 url_fskip(pb, 32); /* codec name */ | |
685 | |
686 get_be16(pb); /* depth */ | |
687 get_be16(pb); /* colortable id */ | |
688 | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
689 st->codec.frame_rate = 25; |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
690 st->codec.frame_rate_base = 1; |
0 | 691 |
692 size -= (16+8*4+2+32+2*2); | |
693 while (size >= 8) { | |
694 int atom_size, atom_type; | |
65 | 695 int64_t start_pos; |
0 | 696 |
697 atom_size = get_be32(pb); | |
698 atom_type = get_le32(pb); | |
699 size -= 8; | |
700 #ifdef DEBUG | |
701 printf("VIDEO: atom_type=%c%c%c%c atom_size=%d size_left=%d\n", | |
702 (atom_type >> 0) & 0xff, | |
703 (atom_type >> 8) & 0xff, | |
704 (atom_type >> 16) & 0xff, | |
705 (atom_type >> 24) & 0xff, | |
706 atom_size, size); | |
707 #endif | |
708 start_pos = url_ftell(pb); | |
709 | |
710 switch(atom_type) { | |
711 case MKTAG('e', 's', 'd', 's'): | |
712 { | |
713 int tag, len; | |
714 /* Well, broken but suffisant for some MP4 streams */ | |
715 get_be32(pb); /* version + flags */ | |
716 len = mp4_read_descr(pb, &tag); | |
717 if (tag == 0x03) { | |
718 /* MP4ESDescrTag */ | |
719 get_be16(pb); /* ID */ | |
720 get_byte(pb); /* priority */ | |
721 len = mp4_read_descr(pb, &tag); | |
722 if (tag != 0x04) | |
723 goto fail; | |
724 /* MP4DecConfigDescrTag */ | |
725 get_byte(pb); /* objectTypeId */ | |
726 get_be32(pb); /* streamType + buffer size */ | |
727 get_be32(pb); /* max bit rate */ | |
728 get_be32(pb); /* avg bit rate */ | |
729 len = mp4_read_descr(pb, &tag); | |
730 if (tag != 0x05) | |
731 goto fail; | |
732 /* MP4DecSpecificDescrTag */ | |
733 #ifdef DEBUG | |
734 printf("Specific MPEG4 header len=%d\n", len); | |
735 #endif | |
736 sc->header_data = av_mallocz(len); | |
737 if (sc->header_data) { | |
738 get_buffer(pb, sc->header_data, len); | |
739 sc->header_len = len; | |
740 } | |
741 } | |
742 /* in any case, skip garbage */ | |
743 } | |
744 break; | |
745 default: | |
746 break; | |
747 } | |
748 fail: | |
749 url_fskip(pb, (atom_size - 8) - | |
750 ((url_ftell(pb) - start_pos))); | |
751 size -= atom_size - 8; | |
752 } | |
753 if (size > 0) { | |
754 /* unknown extension */ | |
755 url_fskip(pb, size); | |
756 } | |
757 } else { | |
758 st->codec.codec_tag = format; | |
759 | |
760 get_be16(pb); /* version */ | |
761 get_be16(pb); /* revision level */ | |
762 get_be32(pb); /* vendor */ | |
763 | |
764 st->codec.channels = get_be16(pb);/* channel count */ | |
765 samp_sz = get_be16(pb); /* sample size */ | |
766 #ifdef DEBUG | |
767 if(samp_sz != 16) | |
768 puts("!!! stsd: audio sample size is not 16 bit !"); | |
769 #endif | |
770 st->codec.codec_id = codec_get_id(mov_audio_tags, format); | |
771 /* handle specific s8 codec */ | |
772 if (st->codec.codec_id == CODEC_ID_PCM_S16BE && samp_sz == 8) | |
773 st->codec.codec_id = CODEC_ID_PCM_S8; | |
774 | |
775 get_be16(pb); /* compression id = 0*/ | |
776 get_be16(pb); /* packet size = 0 */ | |
777 | |
778 st->codec.sample_rate = ((get_be32(pb) >> 16)); | |
779 st->codec.bit_rate = 0; | |
780 #if 0 | |
781 | |
782 get_be16(pb); get_be16(pb); /* */ | |
783 get_be16(pb); /* */ | |
784 get_be16(pb); /* */ | |
785 get_be16(pb); /* */ | |
786 get_be16(pb); /* */ | |
787 #endif | |
788 if(size > 16) | |
789 url_fskip(pb, size-(16+20)); | |
790 } | |
791 } | |
792 /* | |
793 if(len) { | |
794 buf = av_malloc(len+1); | |
795 get_buffer(pb, buf, len); | |
796 buf[len] = '\0'; | |
797 puts(buf); | |
798 av_free(buf); | |
799 } | |
800 */ | |
801 return 0; | |
802 } | |
803 | |
65 | 804 static int parse_stco(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 805 { |
806 MOVContext *c; | |
807 int entries, i; | |
808 AVStream *st; | |
809 MOVStreamContext *sc; | |
810 #ifdef DEBUG | |
811 print_atom("stco", atom_type, atom_offset, atom_size); | |
812 #endif | |
813 c = (MOVContext *)param; | |
814 st = c->fc->streams[c->fc->nb_streams-1]; | |
815 sc = (MOVStreamContext *)st->priv_data; | |
816 | |
817 get_byte(pb); /* version */ | |
818 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
819 | |
820 entries = get_be32(pb); | |
821 sc->chunk_count = entries; | |
65 | 822 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t)); |
0 | 823 if(atom_type == MKTAG('s', 't', 'c', 'o')) { |
824 for(i=0; i<entries; i++) { | |
825 sc->chunk_offsets[i] = get_be32(pb); | |
826 } | |
827 } else if(atom_type == MKTAG('c', 'o', '6', '4')) { | |
828 for(i=0; i<entries; i++) { | |
829 sc->chunk_offsets[i] = get_be64(pb); | |
830 } | |
831 } else | |
832 return -1; | |
833 #ifdef DEBUG | |
834 /* | |
835 for(i=0; i<entries; i++) { | |
836 printf("chunk offset=0x%Lx\n", sc->chunk_offsets[i]); | |
837 } | |
838 */ | |
839 #endif | |
840 return 0; | |
841 } | |
842 | |
65 | 843 static int parse_stsc(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 844 { |
845 MOVContext *c; | |
846 int entries, i; | |
847 AVStream *st; | |
848 MOVStreamContext *sc; | |
849 #ifdef DEBUG | |
850 print_atom("stsc", atom_type, atom_offset, atom_size); | |
851 #endif | |
852 c = (MOVContext *)param; | |
853 st = c->fc->streams[c->fc->nb_streams-1]; | |
854 sc = (MOVStreamContext *)st->priv_data; | |
855 | |
856 get_byte(pb); /* version */ | |
857 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
858 | |
859 entries = get_be32(pb); | |
860 #ifdef DEBUG | |
861 printf("track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries); | |
862 #endif | |
863 sc->sample_to_chunk_sz = entries; | |
864 sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl)); | |
865 for(i=0; i<entries; i++) { | |
866 sc->sample_to_chunk[i].first = get_be32(pb); | |
867 sc->sample_to_chunk[i].count = get_be32(pb); | |
868 sc->sample_to_chunk[i].id = get_be32(pb); | |
869 #ifdef DEBUG | |
870 /* 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); */ | |
871 #endif | |
872 } | |
873 return 0; | |
874 } | |
875 | |
65 | 876 static int parse_stsz(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 877 { |
878 MOVContext *c; | |
879 int entries, i; | |
880 AVStream *st; | |
881 MOVStreamContext *sc; | |
882 #ifdef DEBUG | |
883 print_atom("stsz", atom_type, atom_offset, atom_size); | |
884 #endif | |
885 c = (MOVContext *)param; | |
886 st = c->fc->streams[c->fc->nb_streams-1]; | |
887 sc = (MOVStreamContext *)st->priv_data; | |
888 | |
889 get_byte(pb); /* version */ | |
890 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
891 | |
892 sc->sample_size = get_be32(pb); | |
893 entries = get_be32(pb); | |
894 sc->sample_count = entries; | |
895 #ifdef DEBUG | |
896 printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count); | |
897 #endif | |
898 if(sc->sample_size) | |
899 return 0; /* there isn't any table following */ | |
900 sc->sample_sizes = av_malloc(entries * sizeof(long)); | |
901 for(i=0; i<entries; i++) { | |
902 sc->sample_sizes[i] = get_be32(pb); | |
903 #ifdef DEBUG | |
904 /* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */ | |
905 #endif | |
906 } | |
907 return 0; | |
908 } | |
909 | |
65 | 910 static int parse_stts(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 911 { |
912 MOVContext *c; | |
913 int entries, i; | |
914 AVStream *st; | |
915 MOVStreamContext *sc; | |
916 #ifdef DEBUG | |
917 print_atom("stts", atom_type, atom_offset, atom_size); | |
918 #endif | |
919 c = (MOVContext *)param; | |
920 st = c->fc->streams[c->fc->nb_streams-1]; | |
921 sc = (MOVStreamContext *)st->priv_data; | |
922 | |
923 get_byte(pb); /* version */ | |
924 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
925 entries = get_be32(pb); | |
926 #ifdef DEBUG | |
927 printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries); | |
928 #endif | |
929 for(i=0; i<entries; i++) { | |
930 int sample_duration; | |
931 | |
932 get_be32(pb); | |
933 sample_duration = get_be32(pb); | |
934 | |
935 if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) { | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
936 st->codec.frame_rate_base = sample_duration ? sample_duration : 1; |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
937 st->codec.frame_rate = c->streams[c->total_streams]->time_scale; |
0 | 938 #ifdef DEBUG |
939 printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration); | |
940 #endif | |
941 } | |
942 } | |
943 return 0; | |
944 } | |
945 | |
946 #ifdef CONFIG_ZLIB | |
65 | 947 static int null_read_packet(void *opaque, uint8_t *buf, int buf_size) |
0 | 948 { |
949 return -1; | |
950 } | |
951 | |
65 | 952 static int parse_cmov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param) |
0 | 953 { |
954 MOVContext *c; | |
955 ByteIOContext ctx; | |
956 char *cmov_data; | |
957 unsigned char *moov_data; /* uncompressed data */ | |
958 long cmov_len, moov_len; | |
959 int ret; | |
960 #ifdef DEBUG | |
961 print_atom("cmov", atom_type, atom_offset, atom_size); | |
962 #endif | |
963 c = (MOVContext *)param; | |
964 | |
965 get_be32(pb); /* dcom atom */ | |
966 if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' )) | |
967 return -1; | |
968 if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) { | |
969 puts("unknown compression for cmov atom !"); | |
970 return -1; | |
971 } | |
972 get_be32(pb); /* cmvd atom */ | |
973 if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' )) | |
974 return -1; | |
975 moov_len = get_be32(pb); /* uncompressed size */ | |
976 cmov_len = atom_size - 6 * 4; | |
977 | |
978 cmov_data = av_malloc(cmov_len); | |
979 if (!cmov_data) | |
980 return -1; | |
981 moov_data = av_malloc(moov_len); | |
982 if (!moov_data) { | |
983 av_free(cmov_data); | |
984 return -1; | |
985 } | |
986 get_buffer(pb, cmov_data, cmov_len); | |
987 if(uncompress (moov_data, &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK) | |
988 return -1; | |
989 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0) | |
990 return -1; | |
991 ctx.buf_end = ctx.buffer + moov_len; | |
992 ret = parse_default(parse_table, &ctx, MKTAG( 'm', 'o', 'o', 'v' ), 0, moov_len, param); | |
993 av_free(moov_data); | |
994 av_free(cmov_data); | |
995 return ret; | |
996 } | |
997 #endif | |
998 | |
999 static const MOVParseTableEntry mov_default_parse_table[] = { | |
1000 /* mp4 atoms */ | |
1001 { MKTAG( 'm', 'p', '4', 'a' ), parse_default }, | |
1002 { MKTAG( 'c', 'o', '6', '4' ), parse_stco }, | |
1003 { MKTAG( 's', 't', 'c', 'o' ), parse_stco }, | |
1004 { MKTAG( 'c', 'r', 'h', 'd' ), parse_default }, | |
1005 { MKTAG( 'c', 't', 't', 's' ), parse_leaf }, | |
1006 { MKTAG( 'c', 'p', 'r', 't' ), parse_default }, | |
1007 { MKTAG( 'u', 'r', 'l', ' ' ), parse_leaf }, | |
1008 { MKTAG( 'u', 'r', 'n', ' ' ), parse_leaf }, | |
1009 { MKTAG( 'd', 'i', 'n', 'f' ), parse_default }, | |
1010 { MKTAG( 'd', 'r', 'e', 'f' ), parse_leaf }, | |
1011 { MKTAG( 's', 't', 'd', 'p' ), parse_default }, | |
1012 { MKTAG( 'e', 's', 'd', 's' ), parse_default }, | |
1013 { MKTAG( 'e', 'd', 't', 's' ), parse_default }, | |
1014 { MKTAG( 'e', 'l', 's', 't' ), parse_leaf }, | |
1015 { MKTAG( 'u', 'u', 'i', 'd' ), parse_default }, | |
1016 { MKTAG( 'f', 'r', 'e', 'e' ), parse_leaf }, | |
1017 { MKTAG( 'h', 'd', 'l', 'r' ), parse_hdlr }, | |
1018 { MKTAG( 'h', 'm', 'h', 'd' ), parse_leaf }, | |
1019 { MKTAG( 'h', 'i', 'n', 't' ), parse_leaf }, | |
1020 { MKTAG( 'n', 'm', 'h', 'd' ), parse_leaf }, | |
1021 { MKTAG( 'm', 'p', '4', 's' ), parse_default }, | |
1022 { MKTAG( 'm', 'd', 'i', 'a' ), parse_default }, | |
1023 { MKTAG( 'm', 'd', 'a', 't' ), parse_mdat }, | |
1024 { MKTAG( 'm', 'd', 'h', 'd' ), parse_mdhd }, | |
1025 { MKTAG( 'm', 'i', 'n', 'f' ), parse_default }, | |
1026 { MKTAG( 'm', 'o', 'o', 'v' ), parse_moov }, | |
1027 { MKTAG( 'm', 'v', 'h', 'd' ), parse_mvhd }, | |
1028 { MKTAG( 'i', 'o', 'd', 's' ), parse_leaf }, | |
1029 { MKTAG( 'o', 'd', 'h', 'd' ), parse_default }, | |
1030 { MKTAG( 'm', 'p', 'o', 'd' ), parse_leaf }, | |
1031 { MKTAG( 's', 't', 's', 'd' ), parse_stsd }, | |
1032 { MKTAG( 's', 't', 's', 'z' ), parse_stsz }, | |
1033 { MKTAG( 's', 't', 'b', 'l' ), parse_default }, | |
1034 { MKTAG( 's', 't', 's', 'c' ), parse_stsc }, | |
1035 { MKTAG( 's', 'd', 'h', 'd' ), parse_default }, | |
1036 { MKTAG( 's', 't', 's', 'h' ), parse_default }, | |
1037 { MKTAG( 's', 'k', 'i', 'p' ), parse_default }, | |
1038 { MKTAG( 's', 'm', 'h', 'd' ), parse_leaf }, | |
1039 { MKTAG( 'd', 'p', 'n', 'd' ), parse_leaf }, | |
1040 { MKTAG( 's', 't', 's', 's' ), parse_leaf }, | |
1041 { MKTAG( 's', 't', 't', 's' ), parse_stts }, | |
1042 { MKTAG( 't', 'r', 'a', 'k' ), parse_trak }, | |
1043 { MKTAG( 't', 'k', 'h', 'd' ), parse_tkhd }, | |
1044 { MKTAG( 't', 'r', 'e', 'f' ), parse_default }, /* not really */ | |
1045 { MKTAG( 'u', 'd', 't', 'a' ), parse_leaf }, | |
1046 { MKTAG( 'v', 'm', 'h', 'd' ), parse_leaf }, | |
1047 { MKTAG( 'm', 'p', '4', 'v' ), parse_default }, | |
1048 /* extra mp4 */ | |
1049 { MKTAG( 'M', 'D', 'E', 'S' ), parse_leaf }, | |
1050 /* QT atoms */ | |
1051 { MKTAG( 'c', 'h', 'a', 'p' ), parse_leaf }, | |
1052 { MKTAG( 'c', 'l', 'i', 'p' ), parse_default }, | |
1053 { MKTAG( 'c', 'r', 'g', 'n' ), parse_leaf }, | |
1054 { MKTAG( 'k', 'm', 'a', 't' ), parse_leaf }, | |
1055 { MKTAG( 'm', 'a', 't', 't' ), parse_default }, | |
1056 { MKTAG( 'r', 'd', 'r', 'f' ), parse_leaf }, | |
1057 { MKTAG( 'r', 'm', 'd', 'a' ), parse_default }, | |
1058 { MKTAG( 'r', 'm', 'd', 'r' ), parse_leaf }, | |
1059 //{ MKTAG( 'r', 'm', 'q', 'u' ), parse_leaf }, | |
1060 { MKTAG( 'r', 'm', 'r', 'a' ), parse_default }, | |
1061 { MKTAG( 's', 'c', 'p', 't' ), parse_leaf }, | |
1062 { MKTAG( 's', 'y', 'n', 'c' ), parse_leaf }, | |
1063 { MKTAG( 's', 's', 'r', 'c' ), parse_leaf }, | |
1064 { MKTAG( 't', 'c', 'm', 'd' ), parse_leaf }, | |
1065 { MKTAG( 'w', 'i', 'd', 'e' ), parse_wide }, /* place holder */ | |
1066 #ifdef CONFIG_ZLIB | |
1067 { MKTAG( 'c', 'm', 'o', 'v' ), parse_cmov }, | |
1068 #else | |
1069 { MKTAG( 'c', 'm', 'o', 'v' ), parse_leaf }, | |
1070 #endif | |
1071 { 0L, parse_leaf } | |
1072 }; | |
1073 | |
1074 static void mov_free_stream_context(MOVStreamContext *sc) | |
1075 { | |
1076 if(sc) { | |
1077 av_free(sc->chunk_offsets); | |
1078 av_free(sc->sample_to_chunk); | |
1079 av_free(sc->header_data); | |
1080 av_free(sc); | |
1081 } | |
1082 } | |
1083 | |
1084 static uint32_t to_tag(uint8_t *buf) | |
1085 { | |
1086 return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24); | |
1087 } | |
1088 | |
1089 static uint32_t to_be32(uint8_t *buf) | |
1090 { | |
1091 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; | |
1092 } | |
1093 | |
1094 /* XXX: is it suffisant ? */ | |
1095 static int mov_probe(AVProbeData *p) | |
1096 { | |
1097 unsigned int offset; | |
1098 uint32_t tag; | |
1099 | |
1100 /* check file header */ | |
1101 if (p->buf_size <= 12) | |
1102 return 0; | |
1103 offset = 0; | |
1104 for(;;) { | |
1105 /* ignore invalid offset */ | |
1106 if ((offset + 8) > (unsigned int)p->buf_size) | |
1107 return 0; | |
1108 tag = to_tag(p->buf + offset + 4); | |
1109 switch(tag) { | |
1110 case MKTAG( 'm', 'o', 'o', 'v' ): | |
1111 case MKTAG( 'w', 'i', 'd', 'e' ): | |
1112 case MKTAG( 'f', 'r', 'e', 'e' ): | |
24 | 1113 case MKTAG( 'm', 'd', 'a', 't' ): |
1114 case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */ | |
0 | 1115 return AVPROBE_SCORE_MAX; |
1116 case MKTAG( 'f', 't', 'y', 'p' ): | |
1117 case MKTAG( 's', 'k', 'i', 'p' ): | |
1118 offset = to_be32(p->buf) + offset; | |
1119 break; | |
1120 default: | |
1121 /* unrecognized tag */ | |
1122 return 0; | |
1123 } | |
1124 } | |
1125 return 0; | |
1126 } | |
1127 | |
1128 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
1129 { | |
1130 MOVContext *mov = s->priv_data; | |
1131 ByteIOContext *pb = &s->pb; | |
1132 int i, j, nb, err; | |
65 | 1133 int64_t size; |
0 | 1134 |
1135 mov->fc = s; | |
1136 #if 0 | |
1137 /* XXX: I think we should auto detect */ | |
1138 if(s->iformat->name[1] == 'p') | |
1139 mov->mp4 = 1; | |
1140 #endif | |
1141 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */ | |
1142 size = url_filesize(url_fileno(pb)); | |
1143 else | |
1144 size = 0x7FFFFFFFFFFFFFFF; | |
1145 | |
1146 #ifdef DEBUG | |
1147 printf("filesz=%Ld\n", size); | |
1148 #endif | |
1149 | |
1150 /* check MOV header */ | |
1151 err = parse_default(mov_default_parse_table, pb, 0L, 0LL, size, mov); | |
1152 if(err<0 || (!mov->found_moov || !mov->found_mdat)) { | |
1153 puts("header not found !!!"); | |
1154 exit(1); | |
1155 } | |
1156 #ifdef DEBUG | |
1157 printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb)); | |
1158 #endif | |
1159 /* some cleanup : make sure we are on the mdat atom */ | |
1160 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset)) | |
1161 url_fseek(pb, mov->mdat_offset, SEEK_SET); | |
1162 | |
1163 mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */ | |
1164 | |
1165 #ifdef DEBUG | |
1166 printf("mdat_reset_offset=%d\n", (int) url_ftell(pb)); | |
1167 #endif | |
1168 | |
1169 #ifdef DEBUG | |
1170 printf("streams= %d\n", s->nb_streams); | |
1171 #endif | |
1172 mov->total_streams = nb = s->nb_streams; | |
1173 | |
1174 #if 1 | |
1175 for(i=0; i<s->nb_streams;) { | |
1176 if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */ | |
1177 av_free(s->streams[i]); | |
1178 for(j=i+1; j<s->nb_streams; j++) | |
1179 s->streams[j-1] = s->streams[j]; | |
1180 s->nb_streams--; | |
1181 } else | |
1182 i++; | |
1183 } | |
1184 for(i=0; i<s->nb_streams;i++) { | |
1185 MOVStreamContext *sc; | |
1186 sc = (MOVStreamContext *)s->streams[i]->priv_data; | |
1187 sc->ffindex = i; | |
1188 sc->is_ff_stream = 1; | |
1189 } | |
1190 #endif | |
1191 #ifdef DEBUG | |
1192 printf("real streams= %d\n", s->nb_streams); | |
1193 #endif | |
1194 return 0; | |
1195 } | |
1196 | |
1197 /* Yes, this is ugly... I didn't write the specs of QT :p */ | |
1198 /* XXX:remove useless commented code sometime */ | |
1199 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) | |
1200 { | |
1201 MOVContext *mov = s->priv_data; | |
1202 MOVStreamContext *sc; | |
65 | 1203 int64_t offset = 0x0FFFFFFFFFFFFFFF; |
0 | 1204 int i; |
1205 int st_id = 0, size; | |
1206 size = 0x0FFFFFFF; | |
1207 | |
1208 #ifdef MOV_SPLIT_CHUNKS | |
1209 if (mov->partial) { | |
1210 | |
1211 int idx; | |
1212 | |
1213 st_id = mov->partial - 1; | |
1214 idx = mov->streams[st_id]->sample_to_chunk_index; | |
1215 if (idx < 0) return 0; | |
1216 size = mov->streams[st_id]->sample_sizes[mov->streams[st_id]->current_sample]; | |
1217 | |
1218 mov->streams[st_id]->current_sample++; | |
1219 mov->streams[st_id]->left_in_chunk--; | |
1220 | |
1221 if(mov->streams[st_id]->left_in_chunk <= 0) | |
1222 mov->partial = 0; | |
1223 offset = mov->next_chunk_offset; | |
1224 /* extract the sample */ | |
1225 | |
1226 goto readchunk; | |
1227 } | |
1228 #endif | |
1229 | |
1230 again: | |
1231 for(i=0; i<mov->total_streams; i++) { | |
1232 if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count) | |
1233 && (mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] < offset)) { | |
1234 st_id = i; | |
1235 offset = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk]; | |
1236 } | |
1237 } | |
1238 mov->streams[st_id]->next_chunk++; | |
1239 if(offset==0x0FFFFFFFFFFFFFFF) | |
1240 return -1; | |
1241 | |
1242 if(mov->next_chunk_offset < offset) { /* some meta data */ | |
1243 url_fskip(&s->pb, (offset - mov->next_chunk_offset)); | |
1244 mov->next_chunk_offset = offset; | |
1245 } | |
1246 | |
1247 //printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset); | |
1248 if(!mov->streams[st_id]->is_ff_stream) { | |
1249 url_fskip(&s->pb, (offset - mov->next_chunk_offset)); | |
1250 mov->next_chunk_offset = offset; | |
1251 offset = 0x0FFFFFFFFFFFFFFF; | |
1252 goto again; | |
1253 } | |
1254 | |
1255 /* now get the chunk size... */ | |
1256 | |
1257 for(i=0; i<mov->total_streams; i++) { | |
1258 if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count) | |
1259 && ((mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset) < size)) { | |
1260 size = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset; | |
1261 } | |
1262 } | |
1263 #ifdef MOV_SPLIT_CHUNKS | |
1264 /* split chunks into samples */ | |
1265 if(mov->streams[st_id]->sample_size == 0) { | |
1266 int idx; | |
1267 idx = mov->streams[st_id]->sample_to_chunk_index; | |
1268 if ((idx + 1 < mov->streams[st_id]->sample_to_chunk_sz) | |
1269 && (mov->streams[st_id]->next_chunk >= mov->streams[st_id]->sample_to_chunk[idx + 1].first)) | |
1270 idx++; | |
1271 mov->streams[st_id]->sample_to_chunk_index = idx; | |
1272 if(idx >= 0 && mov->streams[st_id]->sample_to_chunk[idx].count != 1) { | |
1273 mov->partial = st_id+1; | |
1274 /* we'll have to get those samples before next chunk */ | |
1275 mov->streams[st_id]->left_in_chunk = (mov->streams[st_id]->sample_to_chunk[idx].count) - 1; | |
1276 size = mov->streams[st_id]->sample_sizes[mov->streams[st_id]->current_sample]; | |
1277 } | |
1278 | |
1279 mov->streams[st_id]->current_sample++; | |
1280 } | |
1281 #endif | |
1282 | |
1283 readchunk: | |
1284 //printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size); | |
1285 if(size == 0x0FFFFFFF) | |
1286 size = mov->mdat_size + mov->mdat_offset - offset; | |
1287 if(size < 0) | |
1288 return -1; | |
1289 if(size == 0) | |
1290 return -1; | |
1291 url_fseek(&s->pb, offset, SEEK_SET); | |
1292 sc = mov->streams[st_id]; | |
1293 if (sc->header_len > 0) { | |
1294 av_new_packet(pkt, size + sc->header_len); | |
1295 memcpy(pkt->data, sc->header_data, sc->header_len); | |
1296 get_buffer(&s->pb, pkt->data + sc->header_len, size); | |
1297 /* free header */ | |
1298 av_freep(&sc->header_data); | |
1299 sc->header_len = 0; | |
1300 } else { | |
1301 av_new_packet(pkt, size); | |
1302 get_buffer(&s->pb, pkt->data, pkt->size); | |
1303 } | |
1304 pkt->stream_index = sc->ffindex; | |
1305 | |
1306 #ifdef DEBUG | |
1307 /* | |
1308 printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size); | |
1309 for(i=0; i<8; i++) | |
1310 printf("%02x ", pkt->data[i]); | |
1311 for(i=0; i<8; i++) | |
1312 printf("%c ", (pkt->data[i]) & 0x7F); | |
1313 puts(""); | |
1314 */ | |
1315 #endif | |
1316 | |
1317 mov->next_chunk_offset = offset + size; | |
1318 | |
1319 return 0; | |
1320 } | |
1321 | |
1322 static int mov_read_close(AVFormatContext *s) | |
1323 { | |
1324 int i; | |
1325 MOVContext *mov = s->priv_data; | |
1326 for(i=0; i<mov->total_streams; i++) | |
1327 mov_free_stream_context(mov->streams[i]); | |
1328 for(i=0; i<s->nb_streams; i++) | |
1329 av_freep(&s->streams[i]); | |
1330 return 0; | |
1331 } | |
1332 | |
1333 static AVInputFormat mov_iformat = { | |
1334 "mov", | |
1335 "QuickTime/MPEG4 format", | |
1336 sizeof(MOVContext), | |
1337 mov_probe, | |
1338 mov_read_header, | |
1339 mov_read_packet, | |
1340 mov_read_close, | |
1341 }; | |
1342 | |
1343 int mov_init(void) | |
1344 { | |
1345 av_register_input_format(&mov_iformat); | |
1346 return 0; | |
1347 } |