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