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