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