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