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