Mercurial > libavformat.hg
annotate mov.c @ 2016:0bd30777841f libavformat
add D-10 PAL 40 ul
author | bcoudurier |
---|---|
date | Tue, 10 Apr 2007 14:04:59 +0000 |
parents | 2f0154760e5f |
children | a3e79d6e4e3c |
rev | line source |
---|---|
1845 | 1 /* |
2 * MOV demuxer | |
3 * Copyright (c) 2001 Fabrice Bellard. | |
4 * | |
5 * This file is part of FFmpeg. | |
6 * | |
7 * FFmpeg is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public | |
9 * License as published by the Free Software Foundation; either | |
10 * version 2.1 of the License, or (at your option) any later version. | |
11 * | |
12 * FFmpeg is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
18 * License along with FFmpeg; if not, write to the Free Software | |
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 */ | |
21 | |
22 #include <limits.h> | |
23 | |
24 //#define DEBUG | |
25 | |
26 #include "avformat.h" | |
27 #include "riff.h" | |
28 #include "isom.h" | |
29 #include "dv.h" | |
30 | |
31 #ifdef CONFIG_ZLIB | |
32 #include <zlib.h> | |
33 #endif | |
34 | |
35 /* | |
36 * First version by Francois Revol revol@free.fr | |
37 * Seek function by Gael Chardon gael.dev@4now.net | |
38 * | |
39 * Features and limitations: | |
40 * - reads most of the QT files I have (at least the structure), | |
41 * the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement. | |
42 * FIXED, Francois Revol, 07/17/2002 | |
43 * - ffmpeg has nearly none of the usual QuickTime codecs, | |
44 * although I succesfully dumped raw and mp3 audio tracks off .mov files. | |
45 * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html | |
46 * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes | |
47 * (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at | |
48 * http://mpeg.telecomitalialab.com/faq.htm | |
49 * - the code is quite ugly... maybe I won't do it recursive next time :-) | |
50 * - seek is not supported with files that contain edit list | |
51 * | |
52 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/ | |
53 * when coding this :) (it's a writer anyway) | |
54 * | |
55 * Reference documents: | |
56 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt | |
57 * Apple: | |
58 * http://developer.apple.com/documentation/QuickTime/QTFF/ | |
59 * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf | |
60 * QuickTime is a trademark of Apple (AFAIK :)) | |
61 */ | |
62 | |
63 #include "qtpalette.h" | |
64 | |
65 | |
66 #undef NDEBUG | |
67 #include <assert.h> | |
68 | |
69 /* the QuickTime file format is quite convoluted... | |
70 * it has lots of index tables, each indexing something in another one... | |
71 * Here we just use what is needed to read the chunks | |
72 */ | |
73 | |
74 typedef struct MOV_sample_to_chunk_tbl { | |
75 long first; | |
76 long count; | |
77 long id; | |
78 } MOV_sample_to_chunk_tbl; | |
79 | |
80 typedef struct { | |
81 uint32_t type; | |
82 int64_t offset; | |
83 int64_t size; /* total size (excluding the size and type fields) */ | |
84 } MOV_atom_t; | |
85 | |
86 typedef struct { | |
87 int seed; | |
88 int flags; | |
89 int size; | |
90 void* clrs; | |
91 } MOV_ctab_t; | |
92 | |
93 typedef struct MOV_mdat_atom_s { | |
94 offset_t offset; | |
95 int64_t size; | |
96 } MOV_mdat_atom_t; | |
97 | |
98 typedef struct { | |
99 uint8_t version; | |
100 uint32_t flags; // 24bit | |
101 | |
102 /* 0x03 ESDescrTag */ | |
103 uint16_t es_id; | |
104 #define MP4ODescrTag 0x01 | |
105 #define MP4IODescrTag 0x02 | |
106 #define MP4ESDescrTag 0x03 | |
107 #define MP4DecConfigDescrTag 0x04 | |
108 #define MP4DecSpecificDescrTag 0x05 | |
109 #define MP4SLConfigDescrTag 0x06 | |
110 #define MP4ContentIdDescrTag 0x07 | |
111 #define MP4SupplContentIdDescrTag 0x08 | |
112 #define MP4IPIPtrDescrTag 0x09 | |
113 #define MP4IPMPPtrDescrTag 0x0A | |
114 #define MP4IPMPDescrTag 0x0B | |
115 #define MP4RegistrationDescrTag 0x0D | |
116 #define MP4ESIDIncDescrTag 0x0E | |
117 #define MP4ESIDRefDescrTag 0x0F | |
118 #define MP4FileIODescrTag 0x10 | |
119 #define MP4FileODescrTag 0x11 | |
120 #define MP4ExtProfileLevelDescrTag 0x13 | |
121 #define MP4ExtDescrTagsStart 0x80 | |
122 #define MP4ExtDescrTagsEnd 0xFE | |
123 uint8_t stream_priority; | |
124 | |
125 /* 0x04 DecConfigDescrTag */ | |
126 uint8_t object_type_id; | |
127 uint8_t stream_type; | |
128 /* XXX: really streamType is | |
129 * only 6bit, followed by: | |
130 * 1bit upStream | |
131 * 1bit reserved | |
132 */ | |
133 uint32_t buffer_size_db; // 24 | |
134 uint32_t max_bitrate; | |
135 uint32_t avg_bitrate; | |
136 | |
137 /* 0x05 DecSpecificDescrTag */ | |
138 uint8_t decoder_cfg_len; | |
139 uint8_t *decoder_cfg; | |
140 | |
141 /* 0x06 SLConfigDescrTag */ | |
142 uint8_t sl_config_len; | |
143 uint8_t *sl_config; | |
144 } MOV_esds_t; | |
145 | |
146 struct MOVParseTableEntry; | |
147 | |
148 typedef struct MOVStreamContext { | |
149 int ffindex; /* the ffmpeg stream id */ | |
150 long next_chunk; | |
151 unsigned int chunk_count; | |
152 int64_t *chunk_offsets; | |
153 unsigned int stts_count; | |
154 Time2Sample *stts_data; | |
155 unsigned int ctts_count; | |
156 Time2Sample *ctts_data; | |
157 unsigned int edit_count; /* number of 'edit' (elst atom) */ | |
158 unsigned int sample_to_chunk_sz; | |
159 MOV_sample_to_chunk_tbl *sample_to_chunk; | |
160 int sample_to_ctime_index; | |
161 int sample_to_ctime_sample; | |
162 unsigned int sample_size; | |
163 unsigned int sample_count; | |
164 long *sample_sizes; | |
165 unsigned int keyframe_count; | |
166 long *keyframes; | |
167 int time_scale; | |
168 int time_rate; | |
169 long current_sample; | |
170 MOV_esds_t esds; | |
1940
1a7f66384792
cosmetics, sample_size_v1 -> bytes_per_frame / samples_per_frame
bcoudurier
parents:
1939
diff
changeset
|
171 unsigned int bytes_per_frame; |
1a7f66384792
cosmetics, sample_size_v1 -> bytes_per_frame / samples_per_frame
bcoudurier
parents:
1939
diff
changeset
|
172 unsigned int samples_per_frame; |
1845 | 173 int dv_audio_container; |
174 } MOVStreamContext; | |
175 | |
176 typedef struct MOVContext { | |
177 AVFormatContext *fc; | |
178 int time_scale; | |
179 int64_t duration; /* duration of the longest track */ | |
180 int found_moov; /* when both 'moov' and 'mdat' sections has been found */ | |
181 int found_mdat; /* we suppose we have enough data to read the file */ | |
182 int64_t mdat_offset; | |
183 int total_streams; | |
184 MOVStreamContext *streams[MAX_STREAMS]; | |
185 | |
186 int ctab_size; | |
187 MOV_ctab_t **ctab; /* color tables */ | |
188 const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */ | |
189 /* NOTE: for recursion save to/ restore from local variable! */ | |
190 | |
191 AVPaletteControl palette_control; | |
192 MOV_mdat_atom_t *mdat_list; | |
193 int mdat_count; | |
194 DVDemuxContext *dv_demux; | |
195 AVFormatContext *dv_fctx; | |
196 int isom; /* 1 if file is ISO Media (mp4/3gp) */ | |
197 } MOVContext; | |
198 | |
199 | |
200 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */ | |
201 | |
202 /* those functions parse an atom */ | |
203 /* return code: | |
204 1: found what I wanted, exit | |
205 0: continue to parse next atom | |
206 -1: error occured, exit | |
207 */ | |
208 typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom); | |
209 | |
210 /* links atom IDs to parse functions */ | |
211 typedef struct MOVParseTableEntry { | |
212 uint32_t type; | |
213 mov_parse_function func; | |
214 } MOVParseTableEntry; | |
215 | |
216 static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
217 { | |
218 int64_t total_size = 0; | |
219 MOV_atom_t a; | |
220 int i; | |
221 int err = 0; | |
222 | |
223 a.offset = atom.offset; | |
224 | |
225 if (atom.size < 0) | |
226 atom.size = 0x7fffffffffffffffLL; | |
227 while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) { | |
228 a.size = atom.size; | |
229 a.type=0L; | |
230 if(atom.size >= 8) { | |
231 a.size = get_be32(pb); | |
232 a.type = get_le32(pb); | |
233 } | |
234 total_size += 8; | |
235 a.offset += 8; | |
1907 | 236 dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n", a.type, (char*)&a.type, a.size, atom.size, total_size); |
1845 | 237 if (a.size == 1) { /* 64 bit extended size */ |
238 a.size = get_be64(pb) - 8; | |
239 a.offset += 8; | |
240 total_size += 8; | |
241 } | |
242 if (a.size == 0) { | |
243 a.size = atom.size - total_size; | |
244 if (a.size <= 8) | |
245 break; | |
246 } | |
1964
4571a481081d
move atom size check before parsing function search
bcoudurier
parents:
1963
diff
changeset
|
247 a.size -= 8; |
4571a481081d
move atom size check before parsing function search
bcoudurier
parents:
1963
diff
changeset
|
248 if(a.size < 0 || a.size > atom.size - total_size) |
4571a481081d
move atom size check before parsing function search
bcoudurier
parents:
1963
diff
changeset
|
249 break; |
4571a481081d
move atom size check before parsing function search
bcoudurier
parents:
1963
diff
changeset
|
250 |
1845 | 251 for (i = 0; c->parse_table[i].type != 0L |
252 && c->parse_table[i].type != a.type; i++) | |
253 /* empty */; | |
254 | |
255 if (c->parse_table[i].type == 0) { /* skip leaf atoms data */ | |
256 url_fskip(pb, a.size); | |
257 } else { | |
258 offset_t start_pos = url_ftell(pb); | |
259 int64_t left; | |
260 err = (c->parse_table[i].func)(c, pb, a); | |
261 left = a.size - url_ftell(pb) + start_pos; | |
262 if (left > 0) /* skip garbage at atom end */ | |
263 url_fskip(pb, left); | |
264 } | |
265 | |
266 a.offset += a.size; | |
267 total_size += a.size; | |
268 } | |
269 | |
270 if (!err && total_size < atom.size && atom.size < 0x7ffff) { | |
271 url_fskip(pb, atom.size - total_size); | |
272 } | |
273 | |
274 return err; | |
275 } | |
276 | |
277 static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
278 { | |
279 #if 1 | |
280 url_fskip(pb, atom.size); // for now | |
281 #else | |
282 VERY VERY BROKEN, NEVER execute this, needs rewrite | |
283 unsigned int len; | |
284 MOV_ctab_t *t; | |
285 c->ctab = av_realloc(c->ctab, ++c->ctab_size); | |
286 t = c->ctab[c->ctab_size]; | |
287 t->seed = get_be32(pb); | |
288 t->flags = get_be16(pb); | |
289 t->size = get_be16(pb) + 1; | |
290 len = 2 * t->size * 4; | |
291 if (len > 0) { | |
292 t->clrs = av_malloc(len); // 16bit A R G B | |
293 if (t->clrs) | |
294 get_buffer(pb, t->clrs, len); | |
295 } | |
296 #endif | |
297 | |
298 return 0; | |
299 } | |
300 | |
301 static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
302 { | |
303 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
304 uint32_t type; | |
305 uint32_t ctype; | |
306 | |
307 get_byte(pb); /* version */ | |
308 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
309 | |
310 /* component type */ | |
311 ctype = get_le32(pb); | |
312 type = get_le32(pb); /* component subtype */ | |
313 | |
1907 | 314 dprintf(c->fc, "ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype); |
315 dprintf(c->fc, "stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]); | |
1845 | 316 if(!ctype) |
317 c->isom = 1; | |
318 if(type == MKTAG('v', 'i', 'd', 'e')) | |
319 st->codec->codec_type = CODEC_TYPE_VIDEO; | |
320 else if(type == MKTAG('s', 'o', 'u', 'n')) | |
321 st->codec->codec_type = CODEC_TYPE_AUDIO; | |
322 else if(type == MKTAG('m', '1', 'a', ' ')) | |
323 st->codec->codec_id = CODEC_ID_MP2; | |
324 else if(type == MKTAG('s', 'u', 'b', 'p')) { | |
325 st->codec->codec_type = CODEC_TYPE_SUBTITLE; | |
326 st->codec->codec_id = CODEC_ID_DVD_SUBTITLE; | |
327 } | |
328 get_be32(pb); /* component manufacture */ | |
329 get_be32(pb); /* component flags */ | |
330 get_be32(pb); /* component flags mask */ | |
331 | |
332 if(atom.size <= 24) | |
333 return 0; /* nothing left to read */ | |
334 | |
335 url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset)); | |
336 return 0; | |
337 } | |
338 | |
339 static int mov_mp4_read_descr_len(ByteIOContext *pb) | |
340 { | |
341 int len = 0; | |
342 int count = 4; | |
343 while (count--) { | |
344 int c = get_byte(pb); | |
345 len = (len << 7) | (c & 0x7f); | |
346 if (!(c & 0x80)) | |
347 break; | |
348 } | |
349 return len; | |
350 } | |
351 | |
1907 | 352 static int mov_mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag) |
1845 | 353 { |
354 int len; | |
355 *tag = get_byte(pb); | |
356 len = mov_mp4_read_descr_len(pb); | |
1907 | 357 dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len); |
1845 | 358 return len; |
359 } | |
360 | |
361 static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
362 { | |
363 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
2006 | 364 MOVStreamContext *sc = st->priv_data; |
1845 | 365 int tag, len; |
366 | |
367 /* Well, broken but suffisant for some MP4 streams */ | |
368 get_be32(pb); /* version + flags */ | |
1907 | 369 len = mov_mp4_read_descr(c, pb, &tag); |
1845 | 370 if (tag == MP4ESDescrTag) { |
371 get_be16(pb); /* ID */ | |
372 get_byte(pb); /* priority */ | |
373 } else | |
374 get_be16(pb); /* ID */ | |
375 | |
1907 | 376 len = mov_mp4_read_descr(c, pb, &tag); |
1845 | 377 if (tag == MP4DecConfigDescrTag) { |
378 sc->esds.object_type_id = get_byte(pb); | |
379 sc->esds.stream_type = get_byte(pb); | |
380 sc->esds.buffer_size_db = get_be24(pb); | |
381 sc->esds.max_bitrate = get_be32(pb); | |
382 sc->esds.avg_bitrate = get_be32(pb); | |
383 | |
1948 | 384 st->codec->codec_id= codec_get_id(ff_mp4_obj_type, sc->esds.object_type_id); |
1907 | 385 dprintf(c->fc, "esds object type id %d\n", sc->esds.object_type_id); |
386 len = mov_mp4_read_descr(c, pb, &tag); | |
1845 | 387 if (tag == MP4DecSpecificDescrTag) { |
1907 | 388 dprintf(c->fc, "Specific MPEG4 header len=%d\n", len); |
1845 | 389 st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE); |
390 if (st->codec->extradata) { | |
391 get_buffer(pb, st->codec->extradata, len); | |
392 st->codec->extradata_size = len; | |
393 /* from mplayer */ | |
394 if ((*st->codec->extradata >> 3) == 29) { | |
395 st->codec->codec_id = CODEC_ID_MP3ON4; | |
396 } | |
397 } | |
398 } | |
399 } | |
400 return 0; | |
401 } | |
402 | |
403 /* this atom contains actual media data */ | |
404 static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
405 { | |
406 if(atom.size == 0) /* wrong one (MP4) */ | |
407 return 0; | |
408 c->mdat_list = av_realloc(c->mdat_list, (c->mdat_count + 1) * sizeof(*c->mdat_list)); | |
409 c->mdat_list[c->mdat_count].offset = atom.offset; | |
410 c->mdat_list[c->mdat_count].size = atom.size; | |
411 c->mdat_count++; | |
412 c->found_mdat=1; | |
413 c->mdat_offset = atom.offset; | |
414 if(c->found_moov) | |
415 return 1; /* found both, just go */ | |
416 url_fskip(pb, atom.size); | |
417 return 0; /* now go for moov */ | |
418 } | |
419 | |
420 static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
421 { | |
422 uint32_t type = get_le32(pb); | |
423 | |
424 if (type != MKTAG('q','t',' ',' ')) | |
425 c->isom = 1; | |
426 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type); | |
427 get_be32(pb); /* minor version */ | |
428 url_fskip(pb, atom.size - 8); | |
429 return 0; | |
430 } | |
431 | |
432 /* this atom should contain all header atoms */ | |
433 static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
434 { | |
435 int err; | |
436 | |
437 err = mov_read_default(c, pb, atom); | |
438 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */ | |
439 /* so we don't parse the whole file if over a network */ | |
440 c->found_moov=1; | |
441 if(c->found_mdat) | |
442 return 1; /* found both, just go */ | |
443 return 0; /* now go for mdat */ | |
444 } | |
445 | |
446 | |
447 static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
448 { | |
449 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
2006 | 450 MOVStreamContext *sc = st->priv_data; |
1845 | 451 int version = get_byte(pb); |
452 int lang; | |
453 | |
454 if (version > 1) | |
455 return 1; /* unsupported */ | |
456 | |
457 get_byte(pb); get_byte(pb); | |
458 get_byte(pb); /* flags */ | |
459 | |
460 if (version == 1) { | |
461 get_be64(pb); | |
462 get_be64(pb); | |
463 } else { | |
464 get_be32(pb); /* creation time */ | |
465 get_be32(pb); /* modification time */ | |
466 } | |
467 | |
468 sc->time_scale = get_be32(pb); | |
469 st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */ | |
470 | |
471 lang = get_be16(pb); /* language */ | |
472 ff_mov_lang_to_iso639(lang, st->language); | |
473 get_be16(pb); /* quality */ | |
474 | |
475 return 0; | |
476 } | |
477 | |
478 static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
479 { | |
480 int version = get_byte(pb); /* version */ | |
481 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
482 | |
483 if (version == 1) { | |
484 get_be64(pb); | |
485 get_be64(pb); | |
486 } else { | |
487 get_be32(pb); /* creation time */ | |
488 get_be32(pb); /* modification time */ | |
489 } | |
490 c->time_scale = get_be32(pb); /* time scale */ | |
491 #ifdef DEBUG | |
492 av_log(NULL, AV_LOG_DEBUG, "time scale = %i\n", c->time_scale); | |
493 #endif | |
494 c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */ | |
495 get_be32(pb); /* preferred scale */ | |
496 | |
497 get_be16(pb); /* preferred volume */ | |
498 | |
499 url_fskip(pb, 10); /* reserved */ | |
500 | |
501 url_fskip(pb, 36); /* display matrix */ | |
502 | |
503 get_be32(pb); /* preview time */ | |
504 get_be32(pb); /* preview duration */ | |
505 get_be32(pb); /* poster time */ | |
506 get_be32(pb); /* selection time */ | |
507 get_be32(pb); /* selection duration */ | |
508 get_be32(pb); /* current time */ | |
509 get_be32(pb); /* next track ID */ | |
510 | |
511 return 0; | |
512 } | |
513 | |
514 static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
515 { | |
516 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
517 | |
518 if((uint64_t)atom.size > (1<<30)) | |
519 return -1; | |
520 | |
521 // currently SVQ3 decoder expect full STSD header - so let's fake it | |
522 // this should be fixed and just SMI header should be passed | |
523 av_free(st->codec->extradata); | |
524 st->codec->extradata_size = 0x5a + atom.size; | |
525 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
526 | |
527 if (st->codec->extradata) { | |
528 memcpy(st->codec->extradata, "SVQ3", 4); // fake | |
529 get_buffer(pb, st->codec->extradata + 0x5a, atom.size); | |
1907 | 530 dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a); |
1845 | 531 } else |
532 url_fskip(pb, atom.size); | |
533 | |
534 return 0; | |
535 } | |
536 | |
537 static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
538 { | |
539 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
540 int little_endian = get_be16(pb); | |
541 | |
542 if (little_endian) { | |
543 switch (st->codec->codec_id) { | |
544 case CODEC_ID_PCM_S24BE: | |
545 st->codec->codec_id = CODEC_ID_PCM_S24LE; | |
546 break; | |
547 case CODEC_ID_PCM_S32BE: | |
548 st->codec->codec_id = CODEC_ID_PCM_S32LE; | |
549 break; | |
550 default: | |
551 break; | |
552 } | |
553 } | |
554 return 0; | |
555 } | |
556 | |
557 /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */ | |
558 static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
559 { | |
560 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
561 if((uint64_t)atom.size > (1<<30)) | |
562 return -1; | |
563 av_free(st->codec->extradata); | |
564 st->codec->extradata_size = atom.size + 8; | |
565 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
566 if (st->codec->extradata) { | |
567 AV_WL32(st->codec->extradata + 4, atom.type); | |
568 get_buffer(pb, st->codec->extradata + 8, atom.size); | |
569 } else | |
570 url_fskip(pb, atom.size); | |
571 return 0; | |
572 } | |
573 | |
574 static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
575 { | |
576 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
577 | |
578 if((uint64_t)atom.size > (1<<30)) | |
579 return -1; | |
580 | |
581 if (st->codec->codec_id == CODEC_ID_QDM2) { | |
582 // pass all frma atom to codec, needed at least for QDM2 | |
583 av_free(st->codec->extradata); | |
584 st->codec->extradata_size = atom.size; | |
585 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
586 | |
587 if (st->codec->extradata) { | |
588 get_buffer(pb, st->codec->extradata, atom.size); | |
589 } else | |
590 url_fskip(pb, atom.size); | |
591 } else if (atom.size > 8) { /* to read frma, esds atoms */ | |
592 mov_read_default(c, pb, atom); | |
593 } else | |
594 url_fskip(pb, atom.size); | |
595 return 0; | |
596 } | |
597 | |
598 static int mov_read_avcC(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
599 { | |
600 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
601 | |
602 if((uint64_t)atom.size > (1<<30)) | |
603 return -1; | |
604 | |
605 av_free(st->codec->extradata); | |
606 | |
607 st->codec->extradata_size = atom.size; | |
608 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
609 | |
610 if (st->codec->extradata) { | |
611 get_buffer(pb, st->codec->extradata, atom.size); | |
612 } else | |
613 url_fskip(pb, atom.size); | |
614 | |
615 return 0; | |
616 } | |
617 | |
618 static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
619 { | |
620 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
2006 | 621 MOVStreamContext *sc = st->priv_data; |
1845 | 622 unsigned int i, entries; |
623 | |
624 get_byte(pb); /* version */ | |
625 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
626 | |
627 entries = get_be32(pb); | |
628 | |
629 if(entries >= UINT_MAX/sizeof(int64_t)) | |
630 return -1; | |
631 | |
632 sc->chunk_count = entries; | |
633 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t)); | |
634 if (!sc->chunk_offsets) | |
635 return -1; | |
636 if (atom.type == MKTAG('s', 't', 'c', 'o')) { | |
637 for(i=0; i<entries; i++) { | |
638 sc->chunk_offsets[i] = get_be32(pb); | |
639 } | |
640 } else if (atom.type == MKTAG('c', 'o', '6', '4')) { | |
641 for(i=0; i<entries; i++) { | |
642 sc->chunk_offsets[i] = get_be64(pb); | |
643 } | |
644 } else | |
645 return -1; | |
646 | |
647 return 0; | |
648 } | |
649 | |
650 static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
651 { | |
652 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
2006 | 653 MOVStreamContext *sc = st->priv_data; |
1845 | 654 int entries, frames_per_sample; |
655 uint32_t format; | |
656 uint8_t codec_name[32]; | |
657 | |
658 /* for palette traversal */ | |
659 int color_depth; | |
660 int color_start; | |
661 int color_count; | |
662 int color_end; | |
663 int color_index; | |
664 int color_dec; | |
665 int color_greyscale; | |
666 unsigned char *color_table; | |
667 int j; | |
668 unsigned char r, g, b; | |
669 | |
670 get_byte(pb); /* version */ | |
671 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
672 | |
673 entries = get_be32(pb); | |
674 | |
675 while(entries--) { //Parsing Sample description table | |
676 enum CodecID id; | |
677 MOV_atom_t a = { 0, 0, 0 }; | |
678 offset_t start_pos = url_ftell(pb); | |
679 int size = get_be32(pb); /* size */ | |
680 format = get_le32(pb); /* data format */ | |
681 | |
682 get_be32(pb); /* reserved */ | |
683 get_be16(pb); /* reserved */ | |
684 get_be16(pb); /* index */ | |
685 | |
686 if (st->codec->codec_tag) { | |
687 /* multiple fourcc, just skip for now */ | |
688 url_fskip(pb, size - (url_ftell(pb) - start_pos)); | |
689 continue; | |
690 } | |
691 | |
692 st->codec->codec_tag = format; | |
1847
922180a45610
recommit of the change below after reverting earlier cosmetic-functional mix
michael
parents:
1846
diff
changeset
|
693 id = codec_get_id(codec_movaudio_tags, format); |
1845 | 694 if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) { |
695 st->codec->codec_type = CODEC_TYPE_AUDIO; | |
696 } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */ | |
697 format && format != MKTAG('m', 'p', '4', 's')) { /* skip old asf mpeg4 tag */ | |
1847
922180a45610
recommit of the change below after reverting earlier cosmetic-functional mix
michael
parents:
1846
diff
changeset
|
698 id = codec_get_id(codec_movvideo_tags, format); |
1845 | 699 if (id <= 0) |
700 id = codec_get_id(codec_bmp_tags, format); | |
701 if (id > 0) | |
702 st->codec->codec_type = CODEC_TYPE_VIDEO; | |
703 } | |
704 | |
1907 | 705 dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", |
1845 | 706 size, |
707 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, (format >> 24) & 0xff, | |
708 st->codec->codec_type); | |
709 | |
710 if(st->codec->codec_type==CODEC_TYPE_VIDEO) { | |
711 st->codec->codec_id = id; | |
712 get_be16(pb); /* version */ | |
713 get_be16(pb); /* revision level */ | |
714 get_be32(pb); /* vendor */ | |
715 get_be32(pb); /* temporal quality */ | |
716 get_be32(pb); /* spacial quality */ | |
717 | |
718 st->codec->width = get_be16(pb); /* width */ | |
719 st->codec->height = get_be16(pb); /* height */ | |
720 | |
721 get_be32(pb); /* horiz resolution */ | |
722 get_be32(pb); /* vert resolution */ | |
723 get_be32(pb); /* data size, always 0 */ | |
724 frames_per_sample = get_be16(pb); /* frames per samples */ | |
725 #ifdef DEBUG | |
726 av_log(NULL, AV_LOG_DEBUG, "frames/samples = %d\n", frames_per_sample); | |
727 #endif | |
728 get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */ | |
729 if (codec_name[0] <= 31) { | |
730 memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]); | |
731 st->codec->codec_name[codec_name[0]] = 0; | |
732 } | |
733 | |
734 st->codec->bits_per_sample = get_be16(pb); /* depth */ | |
735 st->codec->color_table_id = get_be16(pb); /* colortable id */ | |
736 | |
737 /* figure out the palette situation */ | |
738 color_depth = st->codec->bits_per_sample & 0x1F; | |
739 color_greyscale = st->codec->bits_per_sample & 0x20; | |
740 | |
741 /* if the depth is 2, 4, or 8 bpp, file is palettized */ | |
742 if ((color_depth == 2) || (color_depth == 4) || | |
743 (color_depth == 8)) { | |
744 | |
745 if (color_greyscale) { | |
746 | |
747 /* compute the greyscale palette */ | |
748 color_count = 1 << color_depth; | |
749 color_index = 255; | |
750 color_dec = 256 / (color_count - 1); | |
751 for (j = 0; j < color_count; j++) { | |
752 r = g = b = color_index; | |
753 c->palette_control.palette[j] = | |
754 (r << 16) | (g << 8) | (b); | |
755 color_index -= color_dec; | |
756 if (color_index < 0) | |
757 color_index = 0; | |
758 } | |
759 | |
760 } else if (st->codec->color_table_id & 0x08) { | |
761 | |
762 /* if flag bit 3 is set, use the default palette */ | |
763 color_count = 1 << color_depth; | |
764 if (color_depth == 2) | |
765 color_table = ff_qt_default_palette_4; | |
766 else if (color_depth == 4) | |
767 color_table = ff_qt_default_palette_16; | |
768 else | |
769 color_table = ff_qt_default_palette_256; | |
770 | |
771 for (j = 0; j < color_count; j++) { | |
772 r = color_table[j * 4 + 0]; | |
773 g = color_table[j * 4 + 1]; | |
774 b = color_table[j * 4 + 2]; | |
775 c->palette_control.palette[j] = | |
776 (r << 16) | (g << 8) | (b); | |
777 } | |
778 | |
779 } else { | |
780 | |
781 /* load the palette from the file */ | |
782 color_start = get_be32(pb); | |
783 color_count = get_be16(pb); | |
784 color_end = get_be16(pb); | |
785 for (j = color_start; j <= color_end; j++) { | |
786 /* each R, G, or B component is 16 bits; | |
787 * only use the top 8 bits; skip alpha bytes | |
788 * up front */ | |
789 get_byte(pb); | |
790 get_byte(pb); | |
791 r = get_byte(pb); | |
792 get_byte(pb); | |
793 g = get_byte(pb); | |
794 get_byte(pb); | |
795 b = get_byte(pb); | |
796 get_byte(pb); | |
797 c->palette_control.palette[j] = | |
798 (r << 16) | (g << 8) | (b); | |
799 } | |
800 } | |
801 | |
802 st->codec->palctrl = &c->palette_control; | |
803 st->codec->palctrl->palette_changed = 1; | |
804 } else | |
805 st->codec->palctrl = NULL; | |
806 } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) { | |
807 int bits_per_sample; | |
808 uint16_t version = get_be16(pb); | |
809 | |
810 st->codec->codec_id = id; | |
811 get_be16(pb); /* revision level */ | |
812 get_be32(pb); /* vendor */ | |
813 | |
814 st->codec->channels = get_be16(pb); /* channel count */ | |
1907 | 815 dprintf(c->fc, "audio channels %d\n", st->codec->channels); |
1845 | 816 st->codec->bits_per_sample = get_be16(pb); /* sample size */ |
817 /* do we need to force to 16 for AMR ? */ | |
818 | |
819 /* handle specific s8 codec */ | |
820 get_be16(pb); /* compression id = 0*/ | |
821 get_be16(pb); /* packet size = 0 */ | |
822 | |
823 st->codec->sample_rate = ((get_be32(pb) >> 16)); | |
824 | |
825 switch (st->codec->codec_id) { | |
826 case CODEC_ID_PCM_S8: | |
827 case CODEC_ID_PCM_U8: | |
828 if (st->codec->bits_per_sample == 16) | |
829 st->codec->codec_id = CODEC_ID_PCM_S16BE; | |
830 break; | |
831 case CODEC_ID_PCM_S16LE: | |
832 case CODEC_ID_PCM_S16BE: | |
833 if (st->codec->bits_per_sample == 8) | |
834 st->codec->codec_id = CODEC_ID_PCM_S8; | |
835 else if (st->codec->bits_per_sample == 24) | |
836 st->codec->codec_id = CODEC_ID_PCM_S24BE; | |
837 break; | |
838 default: | |
839 break; | |
840 } | |
841 | |
842 //Read QT version 1 fields. In version 0 theese dont exist | |
1907 | 843 dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom); |
1845 | 844 if(!c->isom) { |
845 if(version==1) { | |
1940
1a7f66384792
cosmetics, sample_size_v1 -> bytes_per_frame / samples_per_frame
bcoudurier
parents:
1939
diff
changeset
|
846 sc->samples_per_frame = get_be32(pb); |
1845 | 847 get_be32(pb); /* bytes per packet */ |
1940
1a7f66384792
cosmetics, sample_size_v1 -> bytes_per_frame / samples_per_frame
bcoudurier
parents:
1939
diff
changeset
|
848 sc->bytes_per_frame = get_be32(pb); |
1845 | 849 get_be32(pb); /* bytes per sample */ |
850 } else if(version==2) { | |
851 get_be32(pb); /* sizeof struct only */ | |
852 st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */ | |
853 st->codec->channels = get_be32(pb); | |
854 get_be32(pb); /* always 0x7F000000 */ | |
855 get_be32(pb); /* bits per channel if sound is uncompressed */ | |
856 get_be32(pb); /* lcpm format specific flag */ | |
857 get_be32(pb); /* bytes per audio packet if constant */ | |
858 get_be32(pb); /* lpcm frames per audio packet if constant */ | |
859 } | |
860 } | |
861 | |
862 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id); | |
863 if (bits_per_sample) { | |
864 st->codec->bits_per_sample = bits_per_sample; | |
865 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels; | |
866 } | |
867 } else { | |
868 /* other codec type, just skip (rtp, mp4s, tmcd ...) */ | |
869 url_fskip(pb, size - (url_ftell(pb) - start_pos)); | |
870 } | |
871 /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */ | |
872 a.size = size - (url_ftell(pb) - start_pos); | |
873 if (a.size > 8) | |
874 mov_read_default(c, pb, a); | |
875 else if (a.size > 0) | |
876 url_fskip(pb, a.size); | |
877 } | |
878 | |
879 if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1) { | |
880 st->codec->sample_rate= sc->time_scale; | |
881 } | |
882 | |
883 /* special codec parameters handling */ | |
884 switch (st->codec->codec_id) { | |
885 #ifdef CONFIG_H261_DECODER | |
886 case CODEC_ID_H261: | |
887 #endif | |
888 #ifdef CONFIG_H263_DECODER | |
889 case CODEC_ID_H263: | |
890 #endif | |
891 #ifdef CONFIG_MPEG4_DECODER | |
892 case CODEC_ID_MPEG4: | |
893 #endif | |
894 st->codec->width= 0; /* let decoder init width/height */ | |
895 st->codec->height= 0; | |
896 break; | |
897 #ifdef CONFIG_LIBFAAD | |
898 case CODEC_ID_AAC: | |
899 #endif | |
900 #ifdef CONFIG_VORBIS_DECODER | |
901 case CODEC_ID_VORBIS: | |
902 #endif | |
903 case CODEC_ID_MP3ON4: | |
904 st->codec->sample_rate= 0; /* let decoder init parameters properly */ | |
905 break; | |
906 #ifdef CONFIG_DV_DEMUXER | |
907 case CODEC_ID_DVAUDIO: | |
908 c->dv_fctx = av_alloc_format_context(); | |
909 c->dv_demux = dv_init_demux(c->dv_fctx); | |
910 if (!c->dv_demux) { | |
911 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n"); | |
912 return -1; | |
913 } | |
914 sc->dv_audio_container = 1; | |
915 st->codec->codec_id = CODEC_ID_PCM_S16LE; | |
916 break; | |
917 #endif | |
918 /* no ifdef since parameters are always those */ | |
919 case CODEC_ID_AMR_WB: | |
920 st->codec->sample_rate= 16000; | |
921 st->codec->channels= 1; /* really needed */ | |
922 break; | |
923 case CODEC_ID_AMR_NB: | |
924 st->codec->sample_rate= 8000; | |
925 st->codec->channels= 1; /* really needed */ | |
926 break; | |
927 case CODEC_ID_MP2: | |
1953
edfd6b33d1f6
activate parser on MP3 id, fix [A-Destiny]_Konjiki_no_Gash_Bell_-_65_[71EE362C].mp4
bcoudurier
parents:
1950
diff
changeset
|
928 case CODEC_ID_MP3: |
1845 | 929 st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */ |
930 st->need_parsing = 1; | |
931 break; | |
932 default: | |
933 break; | |
934 } | |
935 | |
936 return 0; | |
937 } | |
938 | |
939 static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
940 { | |
941 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
2006 | 942 MOVStreamContext *sc = st->priv_data; |
1845 | 943 unsigned int i, entries; |
944 | |
945 get_byte(pb); /* version */ | |
946 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
947 | |
948 entries = get_be32(pb); | |
949 | |
950 if(entries >= UINT_MAX / sizeof(MOV_sample_to_chunk_tbl)) | |
951 return -1; | |
952 | |
953 #ifdef DEBUG | |
954 av_log(NULL, AV_LOG_DEBUG, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries); | |
955 #endif | |
956 sc->sample_to_chunk_sz = entries; | |
957 sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl)); | |
958 if (!sc->sample_to_chunk) | |
959 return -1; | |
960 for(i=0; i<entries; i++) { | |
961 sc->sample_to_chunk[i].first = get_be32(pb); | |
962 sc->sample_to_chunk[i].count = get_be32(pb); | |
963 sc->sample_to_chunk[i].id = get_be32(pb); | |
964 } | |
965 return 0; | |
966 } | |
967 | |
968 static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
969 { | |
970 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
2006 | 971 MOVStreamContext *sc = st->priv_data; |
1845 | 972 unsigned int i, entries; |
973 | |
974 get_byte(pb); /* version */ | |
975 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
976 | |
977 entries = get_be32(pb); | |
978 | |
979 if(entries >= UINT_MAX / sizeof(long)) | |
980 return -1; | |
981 | |
982 sc->keyframe_count = entries; | |
983 #ifdef DEBUG | |
984 av_log(NULL, AV_LOG_DEBUG, "keyframe_count = %d\n", sc->keyframe_count); | |
985 #endif | |
986 sc->keyframes = av_malloc(entries * sizeof(long)); | |
987 if (!sc->keyframes) | |
988 return -1; | |
989 for(i=0; i<entries; i++) { | |
990 sc->keyframes[i] = get_be32(pb); | |
991 #ifdef DEBUG | |
992 /* av_log(NULL, AV_LOG_DEBUG, "keyframes[]=%ld\n", sc->keyframes[i]); */ | |
993 #endif | |
994 } | |
995 return 0; | |
996 } | |
997 | |
998 static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
999 { | |
1000 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
2006 | 1001 MOVStreamContext *sc = st->priv_data; |
1845 | 1002 unsigned int i, entries, sample_size; |
1003 | |
1004 get_byte(pb); /* version */ | |
1005 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
1006 | |
1007 sample_size = get_be32(pb); | |
1008 if (!sc->sample_size) /* do not overwrite value computed in stsd */ | |
1009 sc->sample_size = sample_size; | |
1010 entries = get_be32(pb); | |
1011 if(entries >= UINT_MAX / sizeof(long)) | |
1012 return -1; | |
1013 | |
1014 sc->sample_count = entries; | |
1015 if (sample_size) | |
1016 return 0; | |
1017 | |
1018 #ifdef DEBUG | |
1019 av_log(NULL, AV_LOG_DEBUG, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count); | |
1020 #endif | |
1021 sc->sample_sizes = av_malloc(entries * sizeof(long)); | |
1022 if (!sc->sample_sizes) | |
1023 return -1; | |
1024 for(i=0; i<entries; i++) { | |
1025 sc->sample_sizes[i] = get_be32(pb); | |
1026 #ifdef DEBUG | |
1027 av_log(NULL, AV_LOG_DEBUG, "sample_sizes[]=%ld\n", sc->sample_sizes[i]); | |
1028 #endif | |
1029 } | |
1030 return 0; | |
1031 } | |
1032 | |
1033 static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
1034 { | |
1035 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
2006 | 1036 MOVStreamContext *sc = st->priv_data; |
1845 | 1037 unsigned int i, entries; |
1038 int64_t duration=0; | |
1039 int64_t total_sample_count=0; | |
1040 | |
1041 get_byte(pb); /* version */ | |
1042 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
1043 entries = get_be32(pb); | |
1044 if(entries >= UINT_MAX / sizeof(Time2Sample)) | |
1045 return -1; | |
1046 | |
1047 sc->stts_count = entries; | |
1048 sc->stts_data = av_malloc(entries * sizeof(Time2Sample)); | |
1049 | |
1050 #ifdef DEBUG | |
1051 av_log(NULL, AV_LOG_DEBUG, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries); | |
1052 #endif | |
1053 | |
1054 sc->time_rate=0; | |
1055 | |
1056 for(i=0; i<entries; i++) { | |
1057 int sample_duration; | |
1058 int sample_count; | |
1059 | |
1060 sample_count=get_be32(pb); | |
1061 sample_duration = get_be32(pb); | |
1062 sc->stts_data[i].count= sample_count; | |
1063 sc->stts_data[i].duration= sample_duration; | |
1064 | |
1065 sc->time_rate= ff_gcd(sc->time_rate, sample_duration); | |
1066 | |
1907 | 1067 dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration); |
1845 | 1068 |
1069 duration+=(int64_t)sample_duration*sample_count; | |
1070 total_sample_count+=sample_count; | |
1071 } | |
1072 | |
1073 st->nb_frames= total_sample_count; | |
1074 if(duration) | |
1075 st->duration= duration; | |
1076 return 0; | |
1077 } | |
1078 | |
1079 static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
1080 { | |
1081 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
2006 | 1082 MOVStreamContext *sc = st->priv_data; |
1845 | 1083 unsigned int i, entries; |
1084 | |
1085 get_byte(pb); /* version */ | |
1086 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
1087 entries = get_be32(pb); | |
1088 if(entries >= UINT_MAX / sizeof(Time2Sample)) | |
1089 return -1; | |
1090 | |
1091 sc->ctts_count = entries; | |
1092 sc->ctts_data = av_malloc(entries * sizeof(Time2Sample)); | |
1093 | |
1907 | 1094 dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries); |
1845 | 1095 |
1096 for(i=0; i<entries; i++) { | |
1097 int count =get_be32(pb); | |
1098 int duration =get_be32(pb); | |
1099 | |
1100 if (duration < 0) { | |
1101 av_log(c->fc, AV_LOG_ERROR, "negative ctts, ignoring\n"); | |
1102 sc->ctts_count = 0; | |
1103 url_fskip(pb, 8 * (entries - i - 1)); | |
1104 break; | |
1105 } | |
1106 sc->ctts_data[i].count = count; | |
1107 sc->ctts_data[i].duration= duration; | |
1108 | |
1109 sc->time_rate= ff_gcd(sc->time_rate, duration); | |
1110 } | |
1111 return 0; | |
1112 } | |
1113 | |
1114 static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
1115 { | |
1116 AVStream *st; | |
1117 MOVStreamContext *sc; | |
1118 | |
1119 st = av_new_stream(c->fc, c->fc->nb_streams); | |
1120 if (!st) return -2; | |
1121 sc = av_mallocz(sizeof(MOVStreamContext)); | |
1122 if (!sc) { | |
1123 av_free(st); | |
1124 return -1; | |
1125 } | |
1126 | |
1127 st->priv_data = sc; | |
1128 st->codec->codec_type = CODEC_TYPE_DATA; | |
1129 st->start_time = 0; /* XXX: check */ | |
1130 c->streams[c->fc->nb_streams-1] = sc; | |
1131 | |
1132 return mov_read_default(c, pb, atom); | |
1133 } | |
1134 | |
1135 static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
1136 { | |
1137 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
1138 int version = get_byte(pb); | |
1139 | |
1140 get_byte(pb); get_byte(pb); | |
1141 get_byte(pb); /* flags */ | |
1142 /* | |
1143 MOV_TRACK_ENABLED 0x0001 | |
1144 MOV_TRACK_IN_MOVIE 0x0002 | |
1145 MOV_TRACK_IN_PREVIEW 0x0004 | |
1146 MOV_TRACK_IN_POSTER 0x0008 | |
1147 */ | |
1148 | |
1149 if (version == 1) { | |
1150 get_be64(pb); | |
1151 get_be64(pb); | |
1152 } else { | |
1153 get_be32(pb); /* creation time */ | |
1154 get_be32(pb); /* modification time */ | |
1155 } | |
1156 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/ | |
1157 get_be32(pb); /* reserved */ | |
1158 st->start_time = 0; /* check */ | |
1159 (version == 1) ? get_be64(pb) : get_be32(pb); /* highlevel (considering edits) duration in movie timebase */ | |
1160 get_be32(pb); /* reserved */ | |
1161 get_be32(pb); /* reserved */ | |
1162 | |
1163 get_be16(pb); /* layer */ | |
1164 get_be16(pb); /* alternate group */ | |
1165 get_be16(pb); /* volume */ | |
1166 get_be16(pb); /* reserved */ | |
1167 | |
1168 url_fskip(pb, 36); /* display matrix */ | |
1169 | |
1170 /* those are fixed-point */ | |
1171 get_be32(pb); /* track width */ | |
1172 get_be32(pb); /* track height */ | |
1173 | |
1174 return 0; | |
1175 } | |
1176 | |
1177 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */ | |
1178 /* like the files created with Adobe Premiere 5.0, for samples see */ | |
1179 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */ | |
1180 static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
1181 { | |
1182 int err; | |
1183 | |
1184 if (atom.size < 8) | |
1185 return 0; /* continue */ | |
1186 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */ | |
1187 url_fskip(pb, atom.size - 4); | |
1188 return 0; | |
1189 } | |
1190 atom.type = get_le32(pb); | |
1191 atom.offset += 8; | |
1192 atom.size -= 8; | |
1193 if (atom.type != MKTAG('m', 'd', 'a', 't')) { | |
1194 url_fskip(pb, atom.size); | |
1195 return 0; | |
1196 } | |
1197 err = mov_read_mdat(c, pb, atom); | |
1198 return err; | |
1199 } | |
1200 | |
1201 static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
1202 { | |
1203 #ifdef CONFIG_ZLIB | |
1204 ByteIOContext ctx; | |
1205 uint8_t *cmov_data; | |
1206 uint8_t *moov_data; /* uncompressed data */ | |
1207 long cmov_len, moov_len; | |
1208 int ret; | |
1209 | |
1210 get_be32(pb); /* dcom atom */ | |
1211 if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' )) | |
1212 return -1; | |
1213 if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) { | |
1214 av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !"); | |
1215 return -1; | |
1216 } | |
1217 get_be32(pb); /* cmvd atom */ | |
1218 if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' )) | |
1219 return -1; | |
1220 moov_len = get_be32(pb); /* uncompressed size */ | |
1221 cmov_len = atom.size - 6 * 4; | |
1222 | |
1223 cmov_data = av_malloc(cmov_len); | |
1224 if (!cmov_data) | |
1225 return -1; | |
1226 moov_data = av_malloc(moov_len); | |
1227 if (!moov_data) { | |
1228 av_free(cmov_data); | |
1229 return -1; | |
1230 } | |
1231 get_buffer(pb, cmov_data, cmov_len); | |
1232 if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK) | |
1233 return -1; | |
1234 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0) | |
1235 return -1; | |
1236 atom.type = MKTAG( 'm', 'o', 'o', 'v' ); | |
1237 atom.offset = 0; | |
1238 atom.size = moov_len; | |
1239 #ifdef DEBUG | |
1240 // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); } | |
1241 #endif | |
1242 ret = mov_read_default(c, &ctx, atom); | |
1243 av_free(moov_data); | |
1244 av_free(cmov_data); | |
1245 return ret; | |
1246 #else | |
1247 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n"); | |
1248 return -1; | |
1249 #endif | |
1250 } | |
1251 | |
1252 /* edit list atom */ | |
1253 static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
1254 { | |
1255 int i, edit_count; | |
1256 | |
1257 get_byte(pb); /* version */ | |
1258 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
1259 edit_count= c->streams[c->fc->nb_streams-1]->edit_count = get_be32(pb); /* entries */ | |
1260 | |
1261 for(i=0; i<edit_count; i++){ | |
1262 get_be32(pb); /* Track duration */ | |
1263 get_be32(pb); /* Media time */ | |
1264 get_be32(pb); /* Media rate */ | |
1265 } | |
1907 | 1266 dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, c->streams[c->fc->nb_streams-1]->edit_count); |
1845 | 1267 return 0; |
1268 } | |
1269 | |
1270 static const MOVParseTableEntry mov_default_parse_table[] = { | |
1271 /* mp4 atoms */ | |
1272 { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco }, | |
1273 { MKTAG( 'c', 't', 't', 's' ), mov_read_ctts }, /* composition time to sample */ | |
1274 { MKTAG( 'e', 'd', 't', 's' ), mov_read_default }, | |
1275 { MKTAG( 'e', 'l', 's', 't' ), mov_read_elst }, | |
1276 { MKTAG( 'e', 'n', 'd', 'a' ), mov_read_enda }, | |
1277 { MKTAG( 'f', 'i', 'e', 'l' ), mov_read_extradata }, | |
1278 { MKTAG( 'f', 't', 'y', 'p' ), mov_read_ftyp }, | |
1279 { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr }, | |
1280 { MKTAG( 'j', 'p', '2', 'h' ), mov_read_extradata }, | |
1281 { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat }, | |
1282 { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd }, | |
1283 { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default }, | |
1284 { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default }, | |
1285 { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov }, | |
1286 { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd }, | |
1287 { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */ | |
1288 { MKTAG( 'a', 'l', 'a', 'c' ), mov_read_extradata }, /* alac specific atom */ | |
1289 { MKTAG( 'a', 'v', 'c', 'C' ), mov_read_avcC }, | |
1290 { MKTAG( 's', 't', 'b', 'l' ), mov_read_default }, | |
1291 { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco }, | |
1292 { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc }, | |
1293 { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */ | |
1294 { MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */ | |
1295 { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */ | |
1296 { MKTAG( 's', 't', 't', 's' ), mov_read_stts }, | |
1297 { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */ | |
1298 { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak }, | |
1299 { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_wave }, | |
1300 { MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab }, | |
1301 { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds }, | |
1302 { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */ | |
1303 { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov }, | |
1304 { 0L, NULL } | |
1305 }; | |
1306 | |
1307 static void mov_free_stream_context(MOVStreamContext *sc) | |
1308 { | |
1309 if(sc) { | |
1310 av_freep(&sc->ctts_data); | |
1311 av_freep(&sc); | |
1312 } | |
1313 } | |
1314 | |
1315 /* XXX: is it sufficient ? */ | |
1316 static int mov_probe(AVProbeData *p) | |
1317 { | |
1318 unsigned int offset; | |
1319 uint32_t tag; | |
1320 int score = 0; | |
1321 | |
1322 /* check file header */ | |
1323 offset = 0; | |
1324 for(;;) { | |
1325 /* ignore invalid offset */ | |
1326 if ((offset + 8) > (unsigned int)p->buf_size) | |
1327 return score; | |
1328 tag = AV_RL32(p->buf + offset + 4); | |
1329 switch(tag) { | |
1330 /* check for obvious tags */ | |
1331 case MKTAG( 'j', 'P', ' ', ' ' ): /* jpeg 2000 signature */ | |
1332 case MKTAG( 'm', 'o', 'o', 'v' ): | |
1333 case MKTAG( 'm', 'd', 'a', 't' ): | |
1334 case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */ | |
1335 case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */ | |
1336 return AVPROBE_SCORE_MAX; | |
1337 /* those are more common words, so rate then a bit less */ | |
1338 case MKTAG( 'w', 'i', 'd', 'e' ): | |
1339 case MKTAG( 'f', 'r', 'e', 'e' ): | |
1340 case MKTAG( 'j', 'u', 'n', 'k' ): | |
1341 case MKTAG( 'p', 'i', 'c', 't' ): | |
1342 return AVPROBE_SCORE_MAX - 5; | |
1343 case MKTAG( 'f', 't', 'y', 'p' ): | |
1344 case MKTAG( 's', 'k', 'i', 'p' ): | |
1345 case MKTAG( 'u', 'u', 'i', 'd' ): | |
1346 offset = AV_RB32(p->buf+offset) + offset; | |
1347 /* if we only find those cause probedata is too small at least rate them */ | |
1348 score = AVPROBE_SCORE_MAX - 50; | |
1349 break; | |
1350 default: | |
1351 /* unrecognized tag */ | |
1352 return score; | |
1353 } | |
1354 } | |
1355 return score; | |
1356 } | |
1357 | |
1358 static void mov_build_index(MOVContext *mov, AVStream *st) | |
1359 { | |
1360 MOVStreamContext *sc = st->priv_data; | |
1361 offset_t current_offset; | |
1362 int64_t current_dts = 0; | |
1363 unsigned int stts_index = 0; | |
1364 unsigned int stsc_index = 0; | |
1365 unsigned int stss_index = 0; | |
1366 unsigned int i, j, k; | |
1367 | |
1368 if (sc->sample_sizes || st->codec->codec_type == CODEC_TYPE_VIDEO || sc->dv_audio_container) { | |
1369 unsigned int current_sample = 0; | |
1370 unsigned int stts_sample = 0; | |
1371 unsigned int keyframe, sample_size; | |
1372 unsigned int distance = 0; | |
1373 | |
1374 st->nb_frames = sc->sample_count; | |
1375 for (i = 0; i < sc->chunk_count; i++) { | |
1376 current_offset = sc->chunk_offsets[i]; | |
1377 if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first) | |
1378 stsc_index++; | |
1379 for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) { | |
1380 if (current_sample >= sc->sample_count) { | |
1381 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n"); | |
1382 goto out; | |
1383 } | |
1384 keyframe = !sc->keyframe_count || current_sample + 1 == sc->keyframes[stss_index]; | |
1385 if (keyframe) { | |
1386 distance = 0; | |
1387 if (stss_index + 1 < sc->keyframe_count) | |
1388 stss_index++; | |
1389 } | |
1390 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample]; | |
1907 | 1391 dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", size %d, distance %d, keyframe %d\n", |
1845 | 1392 st->index, current_sample, current_offset, current_dts, sample_size, distance, keyframe); |
1393 av_add_index_entry(st, current_offset, current_dts, sample_size, distance, keyframe ? AVINDEX_KEYFRAME : 0); | |
1394 current_offset += sample_size; | |
1395 assert(sc->stts_data[stts_index].duration % sc->time_rate == 0); | |
1396 current_dts += sc->stts_data[stts_index].duration / sc->time_rate; | |
1397 distance++; | |
1398 stts_sample++; | |
1399 current_sample++; | |
1400 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) { | |
1401 stts_sample = 0; | |
1402 stts_index++; | |
1403 } | |
1404 } | |
1405 } | |
1406 } else { /* read whole chunk */ | |
1407 unsigned int chunk_samples, chunk_size, chunk_duration; | |
1408 | |
1409 for (i = 0; i < sc->chunk_count; i++) { | |
1410 current_offset = sc->chunk_offsets[i]; | |
1411 if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first) | |
1412 stsc_index++; | |
1413 chunk_samples = sc->sample_to_chunk[stsc_index].count; | |
1414 /* get chunk size */ | |
1415 if (sc->sample_size > 1 || st->codec->codec_id == CODEC_ID_PCM_U8 || st->codec->codec_id == CODEC_ID_PCM_S8) | |
1416 chunk_size = chunk_samples * sc->sample_size; | |
1940
1a7f66384792
cosmetics, sample_size_v1 -> bytes_per_frame / samples_per_frame
bcoudurier
parents:
1939
diff
changeset
|
1417 else if (sc->samples_per_frame > 0 && (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0)) |
1a7f66384792
cosmetics, sample_size_v1 -> bytes_per_frame / samples_per_frame
bcoudurier
parents:
1939
diff
changeset
|
1418 chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame; |
1845 | 1419 else { /* workaround to find nearest next chunk offset */ |
1420 chunk_size = INT_MAX; | |
1421 for (j = 0; j < mov->total_streams; j++) { | |
1422 MOVStreamContext *msc = mov->streams[j]; | |
1423 | |
1424 for (k = msc->next_chunk; k < msc->chunk_count; k++) { | |
1425 if (msc->chunk_offsets[k] > current_offset && msc->chunk_offsets[k] - current_offset < chunk_size) { | |
1426 chunk_size = msc->chunk_offsets[k] - current_offset; | |
1427 msc->next_chunk = k; | |
1428 break; | |
1429 } | |
1430 } | |
1431 } | |
1432 /* check for last chunk */ | |
1433 if (chunk_size == INT_MAX) | |
1434 for (j = 0; j < mov->mdat_count; j++) { | |
1907 | 1435 dprintf(mov->fc, "mdat %d, offset %"PRIx64", size %"PRId64", current offset %"PRIx64"\n", |
1845 | 1436 j, mov->mdat_list[j].offset, mov->mdat_list[j].size, current_offset); |
1437 if (mov->mdat_list[j].offset <= current_offset && mov->mdat_list[j].offset + mov->mdat_list[j].size > current_offset) | |
1438 chunk_size = mov->mdat_list[j].offset + mov->mdat_list[j].size - current_offset; | |
1439 } | |
1440 assert(chunk_size != INT_MAX); | |
1441 for (j = 0; j < mov->total_streams; j++) { | |
1442 mov->streams[j]->next_chunk = 0; | |
1443 } | |
1444 } | |
1445 av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME); | |
1446 /* get chunk duration */ | |
1447 chunk_duration = 0; | |
1448 while (chunk_samples > 0) { | |
1449 if (chunk_samples < sc->stts_data[stts_index].count) { | |
1450 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples; | |
1451 sc->stts_data[stts_index].count -= chunk_samples; | |
1452 break; | |
1453 } else { | |
1454 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples; | |
1455 chunk_samples -= sc->stts_data[stts_index].count; | |
1456 if (stts_index + 1 < sc->stts_count) { | |
1457 stts_index++; | |
1458 } | |
1459 } | |
1460 } | |
1907 | 1461 dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", size %d, duration %d\n", |
1845 | 1462 st->index, i, current_offset, current_dts, chunk_size, chunk_duration); |
1463 assert(chunk_duration % sc->time_rate == 0); | |
1464 current_dts += chunk_duration / sc->time_rate; | |
1465 } | |
1466 } | |
1467 out: | |
1468 /* adjust sample count to avindex entries */ | |
1469 sc->sample_count = st->nb_index_entries; | |
1470 } | |
1471 | |
1472 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
1473 { | |
2006 | 1474 MOVContext *mov = s->priv_data; |
1845 | 1475 ByteIOContext *pb = &s->pb; |
1476 int i, err; | |
1477 MOV_atom_t atom = { 0, 0, 0 }; | |
1478 | |
1479 mov->fc = s; | |
1480 mov->parse_table = mov_default_parse_table; | |
1481 | |
1482 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */ | |
1483 atom.size = url_fsize(pb); | |
1484 else | |
1485 atom.size = 0x7FFFFFFFFFFFFFFFLL; | |
1486 | |
1487 /* check MOV header */ | |
1488 err = mov_read_default(mov, pb, atom); | |
1489 if (err<0 || (!mov->found_moov && !mov->found_mdat)) { | |
1490 av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%"PRId64"\n", | |
1491 err, mov->found_moov, mov->found_mdat, url_ftell(pb)); | |
1492 return -1; | |
1493 } | |
1907 | 1494 dprintf(mov->fc, "on_parse_exit_offset=%d\n", (int) url_ftell(pb)); |
1845 | 1495 |
1496 /* some cleanup : make sure we are on the mdat atom */ | |
1497 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset)) | |
1498 url_fseek(pb, mov->mdat_offset, SEEK_SET); | |
1499 | |
1500 mov->total_streams = s->nb_streams; | |
1501 | |
1502 for(i=0; i<mov->total_streams; i++) { | |
1503 MOVStreamContext *sc = mov->streams[i]; | |
1938 | 1504 AVStream *st = s->streams[i]; |
1845 | 1505 /* sanity checks */ |
1506 if(!sc->stts_count || !sc->chunk_count || !sc->sample_to_chunk_sz || | |
1507 (!sc->sample_size && !sc->sample_count)){ | |
1508 av_log(s, AV_LOG_ERROR, "missing mandatory atoms, broken header\n"); | |
1963
81268e2bd9aa
unset sample count to disable track when is broken
bcoudurier
parents:
1962
diff
changeset
|
1509 sc->sample_count = 0; //ignore track |
1950
4ef37f929c21
dont fail immediately when a somehow broken track is detected, some tracks might be good, fix mi2_vorbis51.mp4
bcoudurier
parents:
1948
diff
changeset
|
1510 continue; |
1845 | 1511 } |
1512 if(!sc->time_rate) | |
1513 sc->time_rate=1; | |
1514 if(!sc->time_scale) | |
1515 sc->time_scale= mov->time_scale; | |
1939 | 1516 av_set_pts_info(st, 64, sc->time_rate, sc->time_scale); |
1845 | 1517 |
1938 | 1518 if (st->codec->codec_type == CODEC_TYPE_AUDIO && sc->stts_count == 1) |
1519 st->codec->frame_size = sc->stts_data[0].duration; | |
1520 | |
1939 | 1521 if(st->duration != AV_NOPTS_VALUE){ |
1522 assert(st->duration % sc->time_rate == 0); | |
1523 st->duration /= sc->time_rate; | |
1845 | 1524 } |
1525 sc->ffindex = i; | |
1939 | 1526 mov_build_index(mov, st); |
1845 | 1527 } |
1528 | |
1529 for(i=0; i<mov->total_streams; i++) { | |
1530 /* dont need those anymore */ | |
1531 av_freep(&mov->streams[i]->chunk_offsets); | |
1532 av_freep(&mov->streams[i]->sample_to_chunk); | |
1533 av_freep(&mov->streams[i]->sample_sizes); | |
1534 av_freep(&mov->streams[i]->keyframes); | |
1535 av_freep(&mov->streams[i]->stts_data); | |
1536 } | |
1537 av_freep(&mov->mdat_list); | |
1538 return 0; | |
1539 } | |
1540 | |
1541 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) | |
1542 { | |
1543 MOVContext *mov = s->priv_data; | |
1544 MOVStreamContext *sc = 0; | |
1545 AVIndexEntry *sample = 0; | |
1546 int64_t best_dts = INT64_MAX; | |
1547 int i; | |
1548 | |
1549 for (i = 0; i < mov->total_streams; i++) { | |
1550 MOVStreamContext *msc = mov->streams[i]; | |
1551 | |
1552 if (s->streams[i]->discard != AVDISCARD_ALL && msc->current_sample < msc->sample_count) { | |
1553 AVIndexEntry *current_sample = &s->streams[i]->index_entries[msc->current_sample]; | |
1554 int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate, AV_TIME_BASE, msc->time_scale); | |
1555 | |
1907 | 1556 dprintf(s, "stream %d, sample %ld, dts %"PRId64"\n", i, msc->current_sample, dts); |
1845 | 1557 if (dts < best_dts) { |
1558 sample = current_sample; | |
1559 best_dts = dts; | |
1560 sc = msc; | |
1561 } | |
1562 } | |
1563 } | |
1564 if (!sample) | |
1565 return -1; | |
1566 /* must be done just before reading, to avoid infinite loop on sample */ | |
1567 sc->current_sample++; | |
1568 if (sample->pos >= url_fsize(&s->pb)) { | |
1569 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", sc->ffindex, sample->pos); | |
1570 return -1; | |
1571 } | |
1572 #ifdef CONFIG_DV_DEMUXER | |
1573 if (sc->dv_audio_container) { | |
1574 dv_get_packet(mov->dv_demux, pkt); | |
1907 | 1575 dprintf(s, "dv audio pkt size %d\n", pkt->size); |
1845 | 1576 } else { |
1577 #endif | |
1578 url_fseek(&s->pb, sample->pos, SEEK_SET); | |
1579 av_get_packet(&s->pb, pkt, sample->size); | |
1580 #ifdef CONFIG_DV_DEMUXER | |
1581 if (mov->dv_demux) { | |
1582 void *pkt_destruct_func = pkt->destruct; | |
1583 dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size); | |
1584 pkt->destruct = pkt_destruct_func; | |
1585 } | |
1586 } | |
1587 #endif | |
1588 pkt->stream_index = sc->ffindex; | |
1589 pkt->dts = sample->timestamp; | |
1590 if (sc->ctts_data) { | |
1591 assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0); | |
1592 pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate; | |
1593 /* update ctts context */ | |
1594 sc->sample_to_ctime_sample++; | |
1595 if (sc->sample_to_ctime_index < sc->ctts_count && sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) { | |
1596 sc->sample_to_ctime_index++; | |
1597 sc->sample_to_ctime_sample = 0; | |
1598 } | |
1599 } else { | |
1600 pkt->pts = pkt->dts; | |
1601 } | |
1602 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0; | |
1603 pkt->pos = sample->pos; | |
1907 | 1604 dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n", pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration); |
1845 | 1605 return 0; |
1606 } | |
1607 | |
1608 static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags) | |
1609 { | |
1610 MOVStreamContext *sc = st->priv_data; | |
1611 int sample, time_sample; | |
1612 int i; | |
1613 | |
1614 sample = av_index_search_timestamp(st, timestamp, flags); | |
1907 | 1615 dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample); |
1845 | 1616 if (sample < 0) /* not sure what to do */ |
1617 return -1; | |
1618 sc->current_sample = sample; | |
1907 | 1619 dprintf(st->codec, "stream %d, found sample %ld\n", st->index, sc->current_sample); |
1845 | 1620 /* adjust ctts index */ |
1621 if (sc->ctts_data) { | |
1622 time_sample = 0; | |
1623 for (i = 0; i < sc->ctts_count; i++) { | |
1624 time_sample += sc->ctts_data[i].count; | |
1625 if (time_sample >= sc->current_sample) { | |
1626 sc->sample_to_ctime_index = i; | |
1627 sc->sample_to_ctime_sample = time_sample - sc->current_sample; | |
1628 break; | |
1629 } | |
1630 } | |
1631 } | |
1632 return sample; | |
1633 } | |
1634 | |
1635 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags) | |
1636 { | |
1637 AVStream *st; | |
1638 int64_t seek_timestamp, timestamp; | |
1639 int sample; | |
1640 int i; | |
1641 | |
1642 if (stream_index >= s->nb_streams) | |
1643 return -1; | |
1644 | |
1645 st = s->streams[stream_index]; | |
1646 sample = mov_seek_stream(st, sample_time, flags); | |
1647 if (sample < 0) | |
1648 return -1; | |
1649 | |
1650 /* adjust seek timestamp to found sample timestamp */ | |
1651 seek_timestamp = st->index_entries[sample].timestamp; | |
1652 | |
1653 for (i = 0; i < s->nb_streams; i++) { | |
1654 st = s->streams[i]; | |
1655 if (stream_index == i || st->discard == AVDISCARD_ALL) | |
1656 continue; | |
1657 | |
1658 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base); | |
1659 mov_seek_stream(st, timestamp, flags); | |
1660 } | |
1661 return 0; | |
1662 } | |
1663 | |
1664 static int mov_read_close(AVFormatContext *s) | |
1665 { | |
1666 int i; | |
2006 | 1667 MOVContext *mov = s->priv_data; |
1845 | 1668 for(i=0; i<mov->total_streams; i++) |
1669 mov_free_stream_context(mov->streams[i]); | |
1670 /* free color tabs */ | |
1671 for(i=0; i<mov->ctab_size; i++) | |
1672 av_freep(&mov->ctab[i]); | |
1673 if(mov->dv_demux){ | |
1674 for(i=0; i<mov->dv_fctx->nb_streams; i++){ | |
1675 av_freep(&mov->dv_fctx->streams[i]->codec); | |
1676 av_freep(&mov->dv_fctx->streams[i]); | |
1677 } | |
1678 av_freep(&mov->dv_fctx); | |
1679 av_freep(&mov->dv_demux); | |
1680 } | |
1681 av_freep(&mov->ctab); | |
1682 return 0; | |
1683 } | |
1684 | |
1685 AVInputFormat mov_demuxer = { | |
1686 "mov,mp4,m4a,3gp,3g2,mj2", | |
1687 "QuickTime/MPEG4/Motion JPEG 2000 format", | |
1688 sizeof(MOVContext), | |
1689 mov_probe, | |
1690 mov_read_header, | |
1691 mov_read_packet, | |
1692 mov_read_close, | |
1693 mov_read_seek, | |
1694 }; |