Mercurial > libavformat.hg
annotate mov.c @ 1949:18891c1e3e46 libavformat
add support for V_THEORA into Matroska
Original thread:
Date: Thu, 22 Mar 2007 20:23:08 -0400
Subject: [Ffmpeg-devel] [PATCH] Theora in MKV (GSoC '07 Qualification)
author | aurel |
---|---|
date | Sun, 25 Mar 2007 01:11:00 +0000 |
parents | 80d330650d35 |
children | 4ef37f929c21 |
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 } | |
247 for (i = 0; c->parse_table[i].type != 0L | |
248 && c->parse_table[i].type != a.type; i++) | |
249 /* empty */; | |
250 | |
251 a.size -= 8; | |
252 | |
253 if(a.size < 0) | |
254 break; | |
255 | |
256 if (c->parse_table[i].type == 0) { /* skip leaf atoms data */ | |
257 url_fskip(pb, a.size); | |
258 } else { | |
259 offset_t start_pos = url_ftell(pb); | |
260 int64_t left; | |
261 err = (c->parse_table[i].func)(c, pb, a); | |
262 left = a.size - url_ftell(pb) + start_pos; | |
263 if (left > 0) /* skip garbage at atom end */ | |
264 url_fskip(pb, left); | |
265 } | |
266 | |
267 a.offset += a.size; | |
268 total_size += a.size; | |
269 } | |
270 | |
271 if (!err && total_size < atom.size && atom.size < 0x7ffff) { | |
272 url_fskip(pb, atom.size - total_size); | |
273 } | |
274 | |
275 return err; | |
276 } | |
277 | |
278 static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
279 { | |
280 #if 1 | |
281 url_fskip(pb, atom.size); // for now | |
282 #else | |
283 VERY VERY BROKEN, NEVER execute this, needs rewrite | |
284 unsigned int len; | |
285 MOV_ctab_t *t; | |
286 c->ctab = av_realloc(c->ctab, ++c->ctab_size); | |
287 t = c->ctab[c->ctab_size]; | |
288 t->seed = get_be32(pb); | |
289 t->flags = get_be16(pb); | |
290 t->size = get_be16(pb) + 1; | |
291 len = 2 * t->size * 4; | |
292 if (len > 0) { | |
293 t->clrs = av_malloc(len); // 16bit A R G B | |
294 if (t->clrs) | |
295 get_buffer(pb, t->clrs, len); | |
296 } | |
297 #endif | |
298 | |
299 return 0; | |
300 } | |
301 | |
302 static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
303 { | |
304 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
305 uint32_t type; | |
306 uint32_t ctype; | |
307 | |
308 get_byte(pb); /* version */ | |
309 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
310 | |
311 /* component type */ | |
312 ctype = get_le32(pb); | |
313 type = get_le32(pb); /* component subtype */ | |
314 | |
1907 | 315 dprintf(c->fc, "ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype); |
316 dprintf(c->fc, "stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]); | |
1845 | 317 if(!ctype) |
318 c->isom = 1; | |
319 if(type == MKTAG('v', 'i', 'd', 'e')) | |
320 st->codec->codec_type = CODEC_TYPE_VIDEO; | |
321 else if(type == MKTAG('s', 'o', 'u', 'n')) | |
322 st->codec->codec_type = CODEC_TYPE_AUDIO; | |
323 else if(type == MKTAG('m', '1', 'a', ' ')) | |
324 st->codec->codec_id = CODEC_ID_MP2; | |
325 else if(type == MKTAG('s', 'u', 'b', 'p')) { | |
326 st->codec->codec_type = CODEC_TYPE_SUBTITLE; | |
327 st->codec->codec_id = CODEC_ID_DVD_SUBTITLE; | |
328 } | |
329 get_be32(pb); /* component manufacture */ | |
330 get_be32(pb); /* component flags */ | |
331 get_be32(pb); /* component flags mask */ | |
332 | |
333 if(atom.size <= 24) | |
334 return 0; /* nothing left to read */ | |
335 | |
336 url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset)); | |
337 return 0; | |
338 } | |
339 | |
340 static int mov_mp4_read_descr_len(ByteIOContext *pb) | |
341 { | |
342 int len = 0; | |
343 int count = 4; | |
344 while (count--) { | |
345 int c = get_byte(pb); | |
346 len = (len << 7) | (c & 0x7f); | |
347 if (!(c & 0x80)) | |
348 break; | |
349 } | |
350 return len; | |
351 } | |
352 | |
1907 | 353 static int mov_mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag) |
1845 | 354 { |
355 int len; | |
356 *tag = get_byte(pb); | |
357 len = mov_mp4_read_descr_len(pb); | |
1907 | 358 dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len); |
1845 | 359 return len; |
360 } | |
361 | |
362 static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
363 { | |
364 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
365 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data; | |
366 int tag, len; | |
367 | |
368 /* Well, broken but suffisant for some MP4 streams */ | |
369 get_be32(pb); /* version + flags */ | |
1907 | 370 len = mov_mp4_read_descr(c, pb, &tag); |
1845 | 371 if (tag == MP4ESDescrTag) { |
372 get_be16(pb); /* ID */ | |
373 get_byte(pb); /* priority */ | |
374 } else | |
375 get_be16(pb); /* ID */ | |
376 | |
1907 | 377 len = mov_mp4_read_descr(c, pb, &tag); |
1845 | 378 if (tag == MP4DecConfigDescrTag) { |
379 sc->esds.object_type_id = get_byte(pb); | |
380 sc->esds.stream_type = get_byte(pb); | |
381 sc->esds.buffer_size_db = get_be24(pb); | |
382 sc->esds.max_bitrate = get_be32(pb); | |
383 sc->esds.avg_bitrate = get_be32(pb); | |
384 | |
1948 | 385 st->codec->codec_id= codec_get_id(ff_mp4_obj_type, sc->esds.object_type_id); |
1907 | 386 dprintf(c->fc, "esds object type id %d\n", sc->esds.object_type_id); |
387 len = mov_mp4_read_descr(c, pb, &tag); | |
1845 | 388 if (tag == MP4DecSpecificDescrTag) { |
1907 | 389 dprintf(c->fc, "Specific MPEG4 header len=%d\n", len); |
1845 | 390 st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE); |
391 if (st->codec->extradata) { | |
392 get_buffer(pb, st->codec->extradata, len); | |
393 st->codec->extradata_size = len; | |
394 /* from mplayer */ | |
395 if ((*st->codec->extradata >> 3) == 29) { | |
396 st->codec->codec_id = CODEC_ID_MP3ON4; | |
397 } | |
398 } | |
399 } | |
400 } | |
401 return 0; | |
402 } | |
403 | |
404 /* this atom contains actual media data */ | |
405 static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
406 { | |
407 if(atom.size == 0) /* wrong one (MP4) */ | |
408 return 0; | |
409 c->mdat_list = av_realloc(c->mdat_list, (c->mdat_count + 1) * sizeof(*c->mdat_list)); | |
410 c->mdat_list[c->mdat_count].offset = atom.offset; | |
411 c->mdat_list[c->mdat_count].size = atom.size; | |
412 c->mdat_count++; | |
413 c->found_mdat=1; | |
414 c->mdat_offset = atom.offset; | |
415 if(c->found_moov) | |
416 return 1; /* found both, just go */ | |
417 url_fskip(pb, atom.size); | |
418 return 0; /* now go for moov */ | |
419 } | |
420 | |
421 static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
422 { | |
423 uint32_t type = get_le32(pb); | |
424 | |
425 if (type != MKTAG('q','t',' ',' ')) | |
426 c->isom = 1; | |
427 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type); | |
428 get_be32(pb); /* minor version */ | |
429 url_fskip(pb, atom.size - 8); | |
430 return 0; | |
431 } | |
432 | |
433 /* this atom should contain all header atoms */ | |
434 static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
435 { | |
436 int err; | |
437 | |
438 err = mov_read_default(c, pb, atom); | |
439 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */ | |
440 /* so we don't parse the whole file if over a network */ | |
441 c->found_moov=1; | |
442 if(c->found_mdat) | |
443 return 1; /* found both, just go */ | |
444 return 0; /* now go for mdat */ | |
445 } | |
446 | |
447 | |
448 static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
449 { | |
450 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
451 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data; | |
452 int version = get_byte(pb); | |
453 int lang; | |
454 | |
455 if (version > 1) | |
456 return 1; /* unsupported */ | |
457 | |
458 get_byte(pb); get_byte(pb); | |
459 get_byte(pb); /* flags */ | |
460 | |
461 if (version == 1) { | |
462 get_be64(pb); | |
463 get_be64(pb); | |
464 } else { | |
465 get_be32(pb); /* creation time */ | |
466 get_be32(pb); /* modification time */ | |
467 } | |
468 | |
469 sc->time_scale = get_be32(pb); | |
470 st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */ | |
471 | |
472 lang = get_be16(pb); /* language */ | |
473 ff_mov_lang_to_iso639(lang, st->language); | |
474 get_be16(pb); /* quality */ | |
475 | |
476 return 0; | |
477 } | |
478 | |
479 static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
480 { | |
481 int version = get_byte(pb); /* version */ | |
482 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
483 | |
484 if (version == 1) { | |
485 get_be64(pb); | |
486 get_be64(pb); | |
487 } else { | |
488 get_be32(pb); /* creation time */ | |
489 get_be32(pb); /* modification time */ | |
490 } | |
491 c->time_scale = get_be32(pb); /* time scale */ | |
492 #ifdef DEBUG | |
493 av_log(NULL, AV_LOG_DEBUG, "time scale = %i\n", c->time_scale); | |
494 #endif | |
495 c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */ | |
496 get_be32(pb); /* preferred scale */ | |
497 | |
498 get_be16(pb); /* preferred volume */ | |
499 | |
500 url_fskip(pb, 10); /* reserved */ | |
501 | |
502 url_fskip(pb, 36); /* display matrix */ | |
503 | |
504 get_be32(pb); /* preview time */ | |
505 get_be32(pb); /* preview duration */ | |
506 get_be32(pb); /* poster time */ | |
507 get_be32(pb); /* selection time */ | |
508 get_be32(pb); /* selection duration */ | |
509 get_be32(pb); /* current time */ | |
510 get_be32(pb); /* next track ID */ | |
511 | |
512 return 0; | |
513 } | |
514 | |
515 static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
516 { | |
517 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
518 | |
519 if((uint64_t)atom.size > (1<<30)) | |
520 return -1; | |
521 | |
522 // currently SVQ3 decoder expect full STSD header - so let's fake it | |
523 // this should be fixed and just SMI header should be passed | |
524 av_free(st->codec->extradata); | |
525 st->codec->extradata_size = 0x5a + atom.size; | |
526 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
527 | |
528 if (st->codec->extradata) { | |
529 memcpy(st->codec->extradata, "SVQ3", 4); // fake | |
530 get_buffer(pb, st->codec->extradata + 0x5a, atom.size); | |
1907 | 531 dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a); |
1845 | 532 } else |
533 url_fskip(pb, atom.size); | |
534 | |
535 return 0; | |
536 } | |
537 | |
538 static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
539 { | |
540 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
541 int little_endian = get_be16(pb); | |
542 | |
543 if (little_endian) { | |
544 switch (st->codec->codec_id) { | |
545 case CODEC_ID_PCM_S24BE: | |
546 st->codec->codec_id = CODEC_ID_PCM_S24LE; | |
547 break; | |
548 case CODEC_ID_PCM_S32BE: | |
549 st->codec->codec_id = CODEC_ID_PCM_S32LE; | |
550 break; | |
551 default: | |
552 break; | |
553 } | |
554 } | |
555 return 0; | |
556 } | |
557 | |
558 /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */ | |
559 static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
560 { | |
561 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
562 if((uint64_t)atom.size > (1<<30)) | |
563 return -1; | |
564 av_free(st->codec->extradata); | |
565 st->codec->extradata_size = atom.size + 8; | |
566 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
567 if (st->codec->extradata) { | |
568 AV_WL32(st->codec->extradata + 4, atom.type); | |
569 get_buffer(pb, st->codec->extradata + 8, atom.size); | |
570 } else | |
571 url_fskip(pb, atom.size); | |
572 return 0; | |
573 } | |
574 | |
575 static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
576 { | |
577 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
578 | |
579 if((uint64_t)atom.size > (1<<30)) | |
580 return -1; | |
581 | |
582 if (st->codec->codec_id == CODEC_ID_QDM2) { | |
583 // pass all frma atom to codec, needed at least for QDM2 | |
584 av_free(st->codec->extradata); | |
585 st->codec->extradata_size = atom.size; | |
586 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
587 | |
588 if (st->codec->extradata) { | |
589 get_buffer(pb, st->codec->extradata, atom.size); | |
590 } else | |
591 url_fskip(pb, atom.size); | |
592 } else if (atom.size > 8) { /* to read frma, esds atoms */ | |
593 mov_read_default(c, pb, atom); | |
594 } else | |
595 url_fskip(pb, atom.size); | |
596 return 0; | |
597 } | |
598 | |
599 static int mov_read_avcC(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
600 { | |
601 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
602 | |
603 if((uint64_t)atom.size > (1<<30)) | |
604 return -1; | |
605 | |
606 av_free(st->codec->extradata); | |
607 | |
608 st->codec->extradata_size = atom.size; | |
609 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
610 | |
611 if (st->codec->extradata) { | |
612 get_buffer(pb, st->codec->extradata, atom.size); | |
613 } else | |
614 url_fskip(pb, atom.size); | |
615 | |
616 return 0; | |
617 } | |
618 | |
619 static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
620 { | |
621 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
622 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data; | |
623 unsigned int i, entries; | |
624 | |
625 get_byte(pb); /* version */ | |
626 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
627 | |
628 entries = get_be32(pb); | |
629 | |
630 if(entries >= UINT_MAX/sizeof(int64_t)) | |
631 return -1; | |
632 | |
633 sc->chunk_count = entries; | |
634 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t)); | |
635 if (!sc->chunk_offsets) | |
636 return -1; | |
637 if (atom.type == MKTAG('s', 't', 'c', 'o')) { | |
638 for(i=0; i<entries; i++) { | |
639 sc->chunk_offsets[i] = get_be32(pb); | |
640 } | |
641 } else if (atom.type == MKTAG('c', 'o', '6', '4')) { | |
642 for(i=0; i<entries; i++) { | |
643 sc->chunk_offsets[i] = get_be64(pb); | |
644 } | |
645 } else | |
646 return -1; | |
647 | |
648 return 0; | |
649 } | |
650 | |
651 static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom) | |
652 { | |
653 AVStream *st = c->fc->streams[c->fc->nb_streams-1]; | |
654 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data; | |
655 int entries, frames_per_sample; | |
656 uint32_t format; | |
657 uint8_t codec_name[32]; | |
658 | |
659 /* for palette traversal */ | |
660 int color_depth; | |
661 int color_start; | |
662 int color_count; | |
663 int color_end; | |
664 int color_index; | |
665 int color_dec; | |
666 int color_greyscale; | |
667 unsigned char *color_table; | |
668 int j; | |
669 unsigned char r, g, b; | |
670 | |
671 get_byte(pb); /* version */ | |
672 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */ | |
673 | |
674 entries = get_be32(pb); | |
675 | |
676 while(entries--) { //Parsing Sample description table | |
677 enum CodecID id; | |
678 MOV_atom_t a = { 0, 0, 0 }; | |
679 offset_t start_pos = url_ftell(pb); | |
680 int size = get_be32(pb); /* size */ | |
681 format = get_le32(pb); /* data format */ | |
682 | |
683 get_be32(pb); /* reserved */ | |
684 get_be16(pb); /* reserved */ | |
685 get_be16(pb); /* index */ | |
686 | |
687 if (st->codec->codec_tag) { | |
688 /* multiple fourcc, just skip for now */ | |
689 url_fskip(pb, size - (url_ftell(pb) - start_pos)); | |
690 continue; | |
691 } | |
692 | |
693 st->codec->codec_tag = format; | |
1847
922180a45610
recommit of the change below after reverting earlier cosmetic-functional mix
michael
parents:
1846
diff
changeset
|
694 id = codec_get_id(codec_movaudio_tags, format); |
1845 | 695 if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) { |
696 st->codec->codec_type = CODEC_TYPE_AUDIO; | |
697 } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */ | |
698 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
|
699 id = codec_get_id(codec_movvideo_tags, format); |
1845 | 700 if (id <= 0) |
701 id = codec_get_id(codec_bmp_tags, format); | |
702 if (id > 0) | |
703 st->codec->codec_type = CODEC_TYPE_VIDEO; | |
704 } | |
705 | |
1907 | 706 dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", |
1845 | 707 size, |
708 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, (format >> 24) & 0xff, | |
709 st->codec->codec_type); | |
710 | |
711 if(st->codec->codec_type==CODEC_TYPE_VIDEO) { | |
712 st->codec->codec_id = id; | |
713 get_be16(pb); /* version */ | |
714 get_be16(pb); /* revision level */ | |
715 get_be32(pb); /* vendor */ | |
716 get_be32(pb); /* temporal quality */ | |
717 get_be32(pb); /* spacial quality */ | |
718 | |
719 st->codec->width = get_be16(pb); /* width */ | |
720 st->codec->height = get_be16(pb); /* height */ | |
721 | |
722 get_be32(pb); /* horiz resolution */ | |
723 get_be32(pb); /* vert resolution */ | |
724 get_be32(pb); /* data size, always 0 */ | |
725 frames_per_sample = get_be16(pb); /* frames per samples */ | |
726 #ifdef DEBUG | |
727 av_log(NULL, AV_LOG_DEBUG, "frames/samples = %d\n", frames_per_sample); | |
728 #endif | |
729 get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */ | |
730 if (codec_name[0] <= 31) { | |
731 memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]); | |
732 st->codec->codec_name[codec_name[0]] = 0; | |
733 } | |
734 | |
735 st->codec->bits_per_sample = get_be16(pb); /* depth */ | |
736 st->codec->color_table_id = get_be16(pb); /* colortable id */ | |
737 | |
738 /* figure out the palette situation */ | |
739 color_depth = st->codec->bits_per_sample & 0x1F; | |
740 color_greyscale = st->codec->bits_per_sample & 0x20; | |
741 | |
742 /* if the depth is 2, 4, or 8 bpp, file is palettized */ | |
743 if ((color_depth == 2) || (color_depth == 4) || | |
744 (color_depth == 8)) { | |
745 | |
746 if (color_greyscale) { | |
747 | |
748 /* compute the greyscale palette */ | |
749 color_count = 1 << color_depth; | |
750 color_index = 255; | |
751 color_dec = 256 / (color_count - 1); | |
752 for (j = 0; j < color_count; j++) { | |
753 r = g = b = color_index; | |
754 c->palette_control.palette[j] = | |
755 (r << 16) | (g << 8) | (b); | |
756 color_index -= color_dec; | |
757 if (color_index < 0) | |
758 color_index = 0; | |
759 } | |
760 | |
761 } else if (st->codec->color_table_id & 0x08) { | |
762 | |
763 /* if flag bit 3 is set, use the default palette */ | |
764 color_count = 1 << color_depth; | |
765 if (color_depth == 2) | |
766 color_table = ff_qt_default_palette_4; | |
767 else if (color_depth == 4) | |
768 color_table = ff_qt_default_palette_16; | |
769 else | |
770 color_table = ff_qt_default_palette_256; | |
771 | |
772 for (j = 0; j < color_count; j++) { | |
773 r = color_table[j * 4 + 0]; | |
774 g = color_table[j * 4 + 1]; | |
775 b = color_table[j * 4 + 2]; | |
776 c->palette_control.palette[j] = | |
777 (r << 16) | (g << 8) | (b); | |
778 } | |
779 | |
780 } else { | |
781 | |
782 /* load the palette from the file */ | |
783 color_start = get_be32(pb); | |
784 color_count = get_be16(pb); | |
785 color_end = get_be16(pb); | |
786 for (j = color_start; j <= color_end; j++) { | |
787 /* each R, G, or B component is 16 bits; | |
788 * only use the top 8 bits; skip alpha bytes | |
789 * up front */ | |
790 get_byte(pb); | |
791 get_byte(pb); | |
792 r = get_byte(pb); | |
793 get_byte(pb); | |
794 g = get_byte(pb); | |
795 get_byte(pb); | |
796 b = get_byte(pb); | |
797 get_byte(pb); | |
798 c->palette_control.palette[j] = | |
799 (r << 16) | (g << 8) | (b); | |
800 } | |
801 } | |
802 | |
803 st->codec->palctrl = &c->palette_control; | |
804 st->codec->palctrl->palette_changed = 1; | |
805 } else | |
806 st->codec->palctrl = NULL; | |
807 } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) { | |
808 int bits_per_sample; | |
809 uint16_t version = get_be16(pb); | |
810 | |
811 st->codec->codec_id = id; | |
812 get_be16(pb); /* revision level */ | |
813 get_be32(pb); /* vendor */ | |
814 | |
815 st->codec->channels = get_be16(pb); /* channel count */ | |
1907 | 816 dprintf(c->fc, "audio channels %d\n", st->codec->channels); |
1845 | 817 st->codec->bits_per_sample = get_be16(pb); /* sample size */ |
818 /* do we need to force to 16 for AMR ? */ | |
819 | |
820 /* handle specific s8 codec */ | |
821 get_be16(pb); /* compression id = 0*/ | |
822 get_be16(pb); /* packet size = 0 */ | |
823 | |
824 st->codec->sample_rate = ((get_be32(pb) >> 16)); | |
825 | |
826 switch (st->codec->codec_id) { | |
827 case CODEC_ID_PCM_S8: | |
828 case CODEC_ID_PCM_U8: | |
829 if (st->codec->bits_per_sample == 16) | |
830 st->codec->codec_id = CODEC_ID_PCM_S16BE; | |
831 break; | |
832 case CODEC_ID_PCM_S16LE: | |
833 case CODEC_ID_PCM_S16BE: | |
834 if (st->codec->bits_per_sample == 8) | |
835 st->codec->codec_id = CODEC_ID_PCM_S8; | |
836 else if (st->codec->bits_per_sample == 24) | |
837 st->codec->codec_id = CODEC_ID_PCM_S24BE; | |
838 break; | |
839 default: | |
840 break; | |
841 } | |
842 | |
843 //Read QT version 1 fields. In version 0 theese dont exist | |
1907 | 844 dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom); |
1845 | 845 if(!c->isom) { |
846 if(version==1) { | |
1940
1a7f66384792
cosmetics, sample_size_v1 -> bytes_per_frame / samples_per_frame
bcoudurier
parents:
1939
diff
changeset
|
847 sc->samples_per_frame = get_be32(pb); |
1845 | 848 get_be32(pb); /* bytes per packet */ |
1940
1a7f66384792
cosmetics, sample_size_v1 -> bytes_per_frame / samples_per_frame
bcoudurier
parents:
1939
diff
changeset
|
849 sc->bytes_per_frame = get_be32(pb); |
1845 | 850 get_be32(pb); /* bytes per sample */ |
851 } else if(version==2) { | |
852 get_be32(pb); /* sizeof struct only */ | |
853 st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */ | |
854 st->codec->channels = get_be32(pb); | |
855 get_be32(pb); /* always 0x7F000000 */ | |
856 get_be32(pb); /* bits per channel if sound is uncompressed */ | |
857 get_be32(pb); /* lcpm format specific flag */ | |
858 get_be32(pb); /* bytes per audio packet if constant */ | |
859 get_be32(pb); /* lpcm frames per audio packet if constant */ | |
860 } | |
861 } | |
862 | |
863 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id); | |
864 if (bits_per_sample) { | |
865 st->codec->bits_per_sample = bits_per_sample; | |
866 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels; | |
867 } | |
868 } else { | |
869 /* other codec type, just skip (rtp, mp4s, tmcd ...) */ | |
870 url_fskip(pb, size - (url_ftell(pb) - start_pos)); | |
871 } | |
872 /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */ | |
873 a.size = size - (url_ftell(pb) - start_pos); | |
874 if (a.size > 8) | |
875 mov_read_default(c, pb, a); | |
876 else if (a.size > 0) | |
877 url_fskip(pb, a.size); | |
878 } | |
879 | |
880 if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1) { | |
881 st->codec->sample_rate= sc->time_scale; | |
882 } | |
883 | |
884 /* special codec parameters handling */ | |
885 switch (st->codec->codec_id) { | |
886 #ifdef CONFIG_H261_DECODER | |
887 case CODEC_ID_H261: | |
888 #endif | |
889 #ifdef CONFIG_H263_DECODER | |
890 case CODEC_ID_H263: | |
891 #endif | |
892 #ifdef CONFIG_MPEG4_DECODER | |
893 case CODEC_ID_MPEG4: | |
894 #endif | |
895 st->codec->width= 0; /* let decoder init width/height */ | |
896 st->codec->height= 0; | |
897 break; | |
898 #ifdef CONFIG_LIBFAAD | |
899 case CODEC_ID_AAC: | |
900 #endif | |
901 #ifdef CONFIG_VORBIS_DECODER | |
902 case CODEC_ID_VORBIS: | |
903 #endif | |
904 case CODEC_ID_MP3ON4: | |
905 st->codec->sample_rate= 0; /* let decoder init parameters properly */ | |
906 break; | |
907 #ifdef CONFIG_DV_DEMUXER | |
908 case CODEC_ID_DVAUDIO: | |
909 c->dv_fctx = av_alloc_format_context(); | |
910 c->dv_demux = dv_init_demux(c->dv_fctx); | |
911 if (!c->dv_demux) { | |
912 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n"); | |
913 return -1; | |
914 } | |
915 sc->dv_audio_container = 1; | |
916 st->codec->codec_id = CODEC_ID_PCM_S16LE; | |
917 break; | |
918 #endif | |
919 /* no ifdef since parameters are always those */ | |
920 case CODEC_ID_AMR_WB: | |
921 st->codec->sample_rate= 16000; | |
922 st->codec->channels= 1; /* really needed */ | |
923 break; | |
924 case CODEC_ID_AMR_NB: | |
925 st->codec->sample_rate= 8000; | |
926 st->codec->channels= 1; /* really needed */ | |
927 break; | |
928 case CODEC_ID_MP2: | |
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]; | |
942 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data; | |
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]; | |
971 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data; | |
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]; | |
1001 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data; | |
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]; | |
1036 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data; | |
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]; | |
1082 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data; | |
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 if (p->buf_size <= 12) | |
1324 return 0; | |
1325 offset = 0; | |
1326 for(;;) { | |
1327 /* ignore invalid offset */ | |
1328 if ((offset + 8) > (unsigned int)p->buf_size) | |
1329 return score; | |
1330 tag = AV_RL32(p->buf + offset + 4); | |
1331 switch(tag) { | |
1332 /* check for obvious tags */ | |
1333 case MKTAG( 'j', 'P', ' ', ' ' ): /* jpeg 2000 signature */ | |
1334 case MKTAG( 'm', 'o', 'o', 'v' ): | |
1335 case MKTAG( 'm', 'd', 'a', 't' ): | |
1336 case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */ | |
1337 case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */ | |
1338 return AVPROBE_SCORE_MAX; | |
1339 /* those are more common words, so rate then a bit less */ | |
1340 case MKTAG( 'w', 'i', 'd', 'e' ): | |
1341 case MKTAG( 'f', 'r', 'e', 'e' ): | |
1342 case MKTAG( 'j', 'u', 'n', 'k' ): | |
1343 case MKTAG( 'p', 'i', 'c', 't' ): | |
1344 return AVPROBE_SCORE_MAX - 5; | |
1345 case MKTAG( 'f', 't', 'y', 'p' ): | |
1346 case MKTAG( 's', 'k', 'i', 'p' ): | |
1347 case MKTAG( 'u', 'u', 'i', 'd' ): | |
1348 offset = AV_RB32(p->buf+offset) + offset; | |
1349 /* if we only find those cause probedata is too small at least rate them */ | |
1350 score = AVPROBE_SCORE_MAX - 50; | |
1351 break; | |
1352 default: | |
1353 /* unrecognized tag */ | |
1354 return score; | |
1355 } | |
1356 } | |
1357 return score; | |
1358 } | |
1359 | |
1360 static void mov_build_index(MOVContext *mov, AVStream *st) | |
1361 { | |
1362 MOVStreamContext *sc = st->priv_data; | |
1363 offset_t current_offset; | |
1364 int64_t current_dts = 0; | |
1365 unsigned int stts_index = 0; | |
1366 unsigned int stsc_index = 0; | |
1367 unsigned int stss_index = 0; | |
1368 unsigned int i, j, k; | |
1369 | |
1370 if (sc->sample_sizes || st->codec->codec_type == CODEC_TYPE_VIDEO || sc->dv_audio_container) { | |
1371 unsigned int current_sample = 0; | |
1372 unsigned int stts_sample = 0; | |
1373 unsigned int keyframe, sample_size; | |
1374 unsigned int distance = 0; | |
1375 | |
1376 st->nb_frames = sc->sample_count; | |
1377 for (i = 0; i < sc->chunk_count; i++) { | |
1378 current_offset = sc->chunk_offsets[i]; | |
1379 if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first) | |
1380 stsc_index++; | |
1381 for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) { | |
1382 if (current_sample >= sc->sample_count) { | |
1383 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n"); | |
1384 goto out; | |
1385 } | |
1386 keyframe = !sc->keyframe_count || current_sample + 1 == sc->keyframes[stss_index]; | |
1387 if (keyframe) { | |
1388 distance = 0; | |
1389 if (stss_index + 1 < sc->keyframe_count) | |
1390 stss_index++; | |
1391 } | |
1392 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample]; | |
1907 | 1393 dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", size %d, distance %d, keyframe %d\n", |
1845 | 1394 st->index, current_sample, current_offset, current_dts, sample_size, distance, keyframe); |
1395 av_add_index_entry(st, current_offset, current_dts, sample_size, distance, keyframe ? AVINDEX_KEYFRAME : 0); | |
1396 current_offset += sample_size; | |
1397 assert(sc->stts_data[stts_index].duration % sc->time_rate == 0); | |
1398 current_dts += sc->stts_data[stts_index].duration / sc->time_rate; | |
1399 distance++; | |
1400 stts_sample++; | |
1401 current_sample++; | |
1402 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) { | |
1403 stts_sample = 0; | |
1404 stts_index++; | |
1405 } | |
1406 } | |
1407 } | |
1408 } else { /* read whole chunk */ | |
1409 unsigned int chunk_samples, chunk_size, chunk_duration; | |
1410 | |
1411 for (i = 0; i < sc->chunk_count; i++) { | |
1412 current_offset = sc->chunk_offsets[i]; | |
1413 if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first) | |
1414 stsc_index++; | |
1415 chunk_samples = sc->sample_to_chunk[stsc_index].count; | |
1416 /* get chunk size */ | |
1417 if (sc->sample_size > 1 || st->codec->codec_id == CODEC_ID_PCM_U8 || st->codec->codec_id == CODEC_ID_PCM_S8) | |
1418 chunk_size = chunk_samples * sc->sample_size; | |
1940
1a7f66384792
cosmetics, sample_size_v1 -> bytes_per_frame / samples_per_frame
bcoudurier
parents:
1939
diff
changeset
|
1419 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
|
1420 chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame; |
1845 | 1421 else { /* workaround to find nearest next chunk offset */ |
1422 chunk_size = INT_MAX; | |
1423 for (j = 0; j < mov->total_streams; j++) { | |
1424 MOVStreamContext *msc = mov->streams[j]; | |
1425 | |
1426 for (k = msc->next_chunk; k < msc->chunk_count; k++) { | |
1427 if (msc->chunk_offsets[k] > current_offset && msc->chunk_offsets[k] - current_offset < chunk_size) { | |
1428 chunk_size = msc->chunk_offsets[k] - current_offset; | |
1429 msc->next_chunk = k; | |
1430 break; | |
1431 } | |
1432 } | |
1433 } | |
1434 /* check for last chunk */ | |
1435 if (chunk_size == INT_MAX) | |
1436 for (j = 0; j < mov->mdat_count; j++) { | |
1907 | 1437 dprintf(mov->fc, "mdat %d, offset %"PRIx64", size %"PRId64", current offset %"PRIx64"\n", |
1845 | 1438 j, mov->mdat_list[j].offset, mov->mdat_list[j].size, current_offset); |
1439 if (mov->mdat_list[j].offset <= current_offset && mov->mdat_list[j].offset + mov->mdat_list[j].size > current_offset) | |
1440 chunk_size = mov->mdat_list[j].offset + mov->mdat_list[j].size - current_offset; | |
1441 } | |
1442 assert(chunk_size != INT_MAX); | |
1443 for (j = 0; j < mov->total_streams; j++) { | |
1444 mov->streams[j]->next_chunk = 0; | |
1445 } | |
1446 } | |
1447 av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME); | |
1448 /* get chunk duration */ | |
1449 chunk_duration = 0; | |
1450 while (chunk_samples > 0) { | |
1451 if (chunk_samples < sc->stts_data[stts_index].count) { | |
1452 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples; | |
1453 sc->stts_data[stts_index].count -= chunk_samples; | |
1454 break; | |
1455 } else { | |
1456 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples; | |
1457 chunk_samples -= sc->stts_data[stts_index].count; | |
1458 if (stts_index + 1 < sc->stts_count) { | |
1459 stts_index++; | |
1460 } | |
1461 } | |
1462 } | |
1907 | 1463 dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", size %d, duration %d\n", |
1845 | 1464 st->index, i, current_offset, current_dts, chunk_size, chunk_duration); |
1465 assert(chunk_duration % sc->time_rate == 0); | |
1466 current_dts += chunk_duration / sc->time_rate; | |
1467 } | |
1468 } | |
1469 out: | |
1470 /* adjust sample count to avindex entries */ | |
1471 sc->sample_count = st->nb_index_entries; | |
1472 } | |
1473 | |
1474 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
1475 { | |
1476 MOVContext *mov = (MOVContext *) s->priv_data; | |
1477 ByteIOContext *pb = &s->pb; | |
1478 int i, err; | |
1479 MOV_atom_t atom = { 0, 0, 0 }; | |
1480 | |
1481 mov->fc = s; | |
1482 mov->parse_table = mov_default_parse_table; | |
1483 | |
1484 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */ | |
1485 atom.size = url_fsize(pb); | |
1486 else | |
1487 atom.size = 0x7FFFFFFFFFFFFFFFLL; | |
1488 | |
1489 /* check MOV header */ | |
1490 err = mov_read_default(mov, pb, atom); | |
1491 if (err<0 || (!mov->found_moov && !mov->found_mdat)) { | |
1492 av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%"PRId64"\n", | |
1493 err, mov->found_moov, mov->found_mdat, url_ftell(pb)); | |
1494 return -1; | |
1495 } | |
1907 | 1496 dprintf(mov->fc, "on_parse_exit_offset=%d\n", (int) url_ftell(pb)); |
1845 | 1497 |
1498 /* some cleanup : make sure we are on the mdat atom */ | |
1499 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset)) | |
1500 url_fseek(pb, mov->mdat_offset, SEEK_SET); | |
1501 | |
1502 mov->total_streams = s->nb_streams; | |
1503 | |
1504 for(i=0; i<mov->total_streams; i++) { | |
1505 MOVStreamContext *sc = mov->streams[i]; | |
1938 | 1506 AVStream *st = s->streams[i]; |
1845 | 1507 /* sanity checks */ |
1508 if(!sc->stts_count || !sc->chunk_count || !sc->sample_to_chunk_sz || | |
1509 (!sc->sample_size && !sc->sample_count)){ | |
1510 av_log(s, AV_LOG_ERROR, "missing mandatory atoms, broken header\n"); | |
1511 return -1; | |
1512 } | |
1513 if(!sc->time_rate) | |
1514 sc->time_rate=1; | |
1515 if(!sc->time_scale) | |
1516 sc->time_scale= mov->time_scale; | |
1939 | 1517 av_set_pts_info(st, 64, sc->time_rate, sc->time_scale); |
1845 | 1518 |
1938 | 1519 if (st->codec->codec_type == CODEC_TYPE_AUDIO && sc->stts_count == 1) |
1520 st->codec->frame_size = sc->stts_data[0].duration; | |
1521 | |
1939 | 1522 if(st->duration != AV_NOPTS_VALUE){ |
1523 assert(st->duration % sc->time_rate == 0); | |
1524 st->duration /= sc->time_rate; | |
1845 | 1525 } |
1526 sc->ffindex = i; | |
1939 | 1527 mov_build_index(mov, st); |
1845 | 1528 } |
1529 | |
1530 for(i=0; i<mov->total_streams; i++) { | |
1531 /* dont need those anymore */ | |
1532 av_freep(&mov->streams[i]->chunk_offsets); | |
1533 av_freep(&mov->streams[i]->sample_to_chunk); | |
1534 av_freep(&mov->streams[i]->sample_sizes); | |
1535 av_freep(&mov->streams[i]->keyframes); | |
1536 av_freep(&mov->streams[i]->stts_data); | |
1537 } | |
1538 av_freep(&mov->mdat_list); | |
1539 return 0; | |
1540 } | |
1541 | |
1542 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) | |
1543 { | |
1544 MOVContext *mov = s->priv_data; | |
1545 MOVStreamContext *sc = 0; | |
1546 AVIndexEntry *sample = 0; | |
1547 int64_t best_dts = INT64_MAX; | |
1548 int i; | |
1549 | |
1550 for (i = 0; i < mov->total_streams; i++) { | |
1551 MOVStreamContext *msc = mov->streams[i]; | |
1552 | |
1553 if (s->streams[i]->discard != AVDISCARD_ALL && msc->current_sample < msc->sample_count) { | |
1554 AVIndexEntry *current_sample = &s->streams[i]->index_entries[msc->current_sample]; | |
1555 int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate, AV_TIME_BASE, msc->time_scale); | |
1556 | |
1907 | 1557 dprintf(s, "stream %d, sample %ld, dts %"PRId64"\n", i, msc->current_sample, dts); |
1845 | 1558 if (dts < best_dts) { |
1559 sample = current_sample; | |
1560 best_dts = dts; | |
1561 sc = msc; | |
1562 } | |
1563 } | |
1564 } | |
1565 if (!sample) | |
1566 return -1; | |
1567 /* must be done just before reading, to avoid infinite loop on sample */ | |
1568 sc->current_sample++; | |
1569 if (sample->pos >= url_fsize(&s->pb)) { | |
1570 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", sc->ffindex, sample->pos); | |
1571 return -1; | |
1572 } | |
1573 #ifdef CONFIG_DV_DEMUXER | |
1574 if (sc->dv_audio_container) { | |
1575 dv_get_packet(mov->dv_demux, pkt); | |
1907 | 1576 dprintf(s, "dv audio pkt size %d\n", pkt->size); |
1845 | 1577 } else { |
1578 #endif | |
1579 url_fseek(&s->pb, sample->pos, SEEK_SET); | |
1580 av_get_packet(&s->pb, pkt, sample->size); | |
1581 #ifdef CONFIG_DV_DEMUXER | |
1582 if (mov->dv_demux) { | |
1583 void *pkt_destruct_func = pkt->destruct; | |
1584 dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size); | |
1585 pkt->destruct = pkt_destruct_func; | |
1586 } | |
1587 } | |
1588 #endif | |
1589 pkt->stream_index = sc->ffindex; | |
1590 pkt->dts = sample->timestamp; | |
1591 if (sc->ctts_data) { | |
1592 assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0); | |
1593 pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate; | |
1594 /* update ctts context */ | |
1595 sc->sample_to_ctime_sample++; | |
1596 if (sc->sample_to_ctime_index < sc->ctts_count && sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) { | |
1597 sc->sample_to_ctime_index++; | |
1598 sc->sample_to_ctime_sample = 0; | |
1599 } | |
1600 } else { | |
1601 pkt->pts = pkt->dts; | |
1602 } | |
1603 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0; | |
1604 pkt->pos = sample->pos; | |
1907 | 1605 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 | 1606 return 0; |
1607 } | |
1608 | |
1609 static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags) | |
1610 { | |
1611 MOVStreamContext *sc = st->priv_data; | |
1612 int sample, time_sample; | |
1613 int i; | |
1614 | |
1615 sample = av_index_search_timestamp(st, timestamp, flags); | |
1907 | 1616 dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample); |
1845 | 1617 if (sample < 0) /* not sure what to do */ |
1618 return -1; | |
1619 sc->current_sample = sample; | |
1907 | 1620 dprintf(st->codec, "stream %d, found sample %ld\n", st->index, sc->current_sample); |
1845 | 1621 /* adjust ctts index */ |
1622 if (sc->ctts_data) { | |
1623 time_sample = 0; | |
1624 for (i = 0; i < sc->ctts_count; i++) { | |
1625 time_sample += sc->ctts_data[i].count; | |
1626 if (time_sample >= sc->current_sample) { | |
1627 sc->sample_to_ctime_index = i; | |
1628 sc->sample_to_ctime_sample = time_sample - sc->current_sample; | |
1629 break; | |
1630 } | |
1631 } | |
1632 } | |
1633 return sample; | |
1634 } | |
1635 | |
1636 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags) | |
1637 { | |
1638 AVStream *st; | |
1639 int64_t seek_timestamp, timestamp; | |
1640 int sample; | |
1641 int i; | |
1642 | |
1643 if (stream_index >= s->nb_streams) | |
1644 return -1; | |
1645 | |
1646 st = s->streams[stream_index]; | |
1647 sample = mov_seek_stream(st, sample_time, flags); | |
1648 if (sample < 0) | |
1649 return -1; | |
1650 | |
1651 /* adjust seek timestamp to found sample timestamp */ | |
1652 seek_timestamp = st->index_entries[sample].timestamp; | |
1653 | |
1654 for (i = 0; i < s->nb_streams; i++) { | |
1655 st = s->streams[i]; | |
1656 if (stream_index == i || st->discard == AVDISCARD_ALL) | |
1657 continue; | |
1658 | |
1659 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base); | |
1660 mov_seek_stream(st, timestamp, flags); | |
1661 } | |
1662 return 0; | |
1663 } | |
1664 | |
1665 static int mov_read_close(AVFormatContext *s) | |
1666 { | |
1667 int i; | |
1668 MOVContext *mov = (MOVContext *) s->priv_data; | |
1669 for(i=0; i<mov->total_streams; i++) | |
1670 mov_free_stream_context(mov->streams[i]); | |
1671 /* free color tabs */ | |
1672 for(i=0; i<mov->ctab_size; i++) | |
1673 av_freep(&mov->ctab[i]); | |
1674 if(mov->dv_demux){ | |
1675 for(i=0; i<mov->dv_fctx->nb_streams; i++){ | |
1676 av_freep(&mov->dv_fctx->streams[i]->codec); | |
1677 av_freep(&mov->dv_fctx->streams[i]); | |
1678 } | |
1679 av_freep(&mov->dv_fctx); | |
1680 av_freep(&mov->dv_demux); | |
1681 } | |
1682 av_freep(&mov->ctab); | |
1683 return 0; | |
1684 } | |
1685 | |
1686 AVInputFormat mov_demuxer = { | |
1687 "mov,mp4,m4a,3gp,3g2,mj2", | |
1688 "QuickTime/MPEG4/Motion JPEG 2000 format", | |
1689 sizeof(MOVContext), | |
1690 mov_probe, | |
1691 mov_read_header, | |
1692 mov_read_packet, | |
1693 mov_read_close, | |
1694 mov_read_seek, | |
1695 }; |