blob: 7d026df7638182409fe30c8700a5d91f8bb6d964
1 | /* |
2 | * MOV demuxer |
3 | * Copyright (c) 2001 Fabrice Bellard |
4 | * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com> |
5 | * |
6 | * first version by Francois Revol <revol@free.fr> |
7 | * seek function by Gael Chardon <gael.dev@4now.net> |
8 | * |
9 | * This file is part of FFmpeg. |
10 | * |
11 | * FFmpeg is free software; you can redistribute it and/or |
12 | * modify it under the terms of the GNU Lesser General Public |
13 | * License as published by the Free Software Foundation; either |
14 | * version 2.1 of the License, or (at your option) any later version. |
15 | * |
16 | * FFmpeg is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19 | * Lesser General Public License for more details. |
20 | * |
21 | * You should have received a copy of the GNU Lesser General Public |
22 | * License along with FFmpeg; if not, write to the Free Software |
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
24 | */ |
25 | |
26 | #include <limits.h> |
27 | |
28 | //#define MOV_EXPORT_ALL_METADATA |
29 | |
30 | #include "libavutil/attributes.h" |
31 | #include "libavutil/channel_layout.h" |
32 | #include "libavutil/intreadwrite.h" |
33 | #include "libavutil/intfloat.h" |
34 | #include "libavutil/mathematics.h" |
35 | #include "libavutil/avstring.h" |
36 | #include "libavutil/dict.h" |
37 | #include "libavutil/opt.h" |
38 | #include "libavutil/timecode.h" |
39 | #include "libavcodec/ac3tab.h" |
40 | #include "avformat.h" |
41 | #include "internal.h" |
42 | #include "avio_internal.h" |
43 | #include "riff.h" |
44 | #include "isom.h" |
45 | #include "libavcodec/get_bits.h" |
46 | #include "id3v1.h" |
47 | #include "mov_chan.h" |
48 | #include "seek.h" |
49 | |
50 | #if CONFIG_ZLIB |
51 | #include <zlib.h> |
52 | #endif |
53 | |
54 | #include "qtpalette.h" |
55 | |
56 | |
57 | #undef NDEBUG |
58 | #include <assert.h> |
59 | |
60 | #include "id3v2.h" |
61 | /* those functions parse an atom */ |
62 | /* links atom IDs to parse functions */ |
63 | typedef struct MOVParseTableEntry { |
64 | uint32_t type; |
65 | int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom); |
66 | } MOVParseTableEntry; |
67 | |
68 | static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom); |
69 | |
70 | static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb, |
71 | unsigned len, const char *key) |
72 | { |
73 | char buf[16]; |
74 | |
75 | short current, total = 0; |
76 | avio_rb16(pb); // unknown |
77 | current = avio_rb16(pb); |
78 | if (len >= 6) |
79 | total = avio_rb16(pb); |
80 | if (!total) |
81 | snprintf(buf, sizeof(buf), "%d", current); |
82 | else |
83 | snprintf(buf, sizeof(buf), "%d/%d", current, total); |
84 | av_dict_set(&c->fc->metadata, key, buf, 0); |
85 | |
86 | return 0; |
87 | } |
88 | |
89 | static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb, |
90 | unsigned len, const char *key) |
91 | { |
92 | char buf[16]; |
93 | |
94 | /* bypass padding bytes */ |
95 | avio_r8(pb); |
96 | avio_r8(pb); |
97 | avio_r8(pb); |
98 | |
99 | snprintf(buf, sizeof(buf), "%d", avio_r8(pb)); |
100 | av_dict_set(&c->fc->metadata, key, buf, 0); |
101 | |
102 | return 0; |
103 | } |
104 | |
105 | static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb, |
106 | unsigned len, const char *key) |
107 | { |
108 | char buf[16]; |
109 | |
110 | snprintf(buf, sizeof(buf), "%d", avio_r8(pb)); |
111 | av_dict_set(&c->fc->metadata, key, buf, 0); |
112 | |
113 | return 0; |
114 | } |
115 | |
116 | static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb, |
117 | unsigned len, const char *key) |
118 | { |
119 | short genre; |
120 | char buf[20]; |
121 | |
122 | avio_r8(pb); // unknown |
123 | |
124 | genre = avio_r8(pb); |
125 | if (genre < 1 || genre > ID3v1_GENRE_MAX) |
126 | return 0; |
127 | snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]); |
128 | av_dict_set(&c->fc->metadata, key, buf, 0); |
129 | |
130 | return 0; |
131 | } |
132 | |
133 | static int mov_read_custom_metadata(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
134 | { |
135 | char key[1024]={0}, data[1024]={0}; |
136 | int i; |
137 | AVStream *st; |
138 | MOVStreamContext *sc; |
139 | |
140 | if (c->fc->nb_streams < 1) |
141 | return 0; |
142 | st = c->fc->streams[c->fc->nb_streams-1]; |
143 | sc = st->priv_data; |
144 | |
145 | if (atom.size <= 8) return 0; |
146 | |
147 | for (i = 0; i < 3; i++) { // Parse up to three sub-atoms looking for name and data. |
148 | int data_size = avio_rb32(pb); |
149 | int tag = avio_rl32(pb); |
150 | int str_size = 0, skip_size = 0; |
151 | char *target = NULL; |
152 | |
153 | switch (tag) { |
154 | case MKTAG('n','a','m','e'): |
155 | avio_rb32(pb); // version/flags |
156 | str_size = skip_size = data_size - 12; |
157 | atom.size -= 12; |
158 | target = key; |
159 | break; |
160 | case MKTAG('d','a','t','a'): |
161 | avio_rb32(pb); // version/flags |
162 | avio_rb32(pb); // reserved (zero) |
163 | str_size = skip_size = data_size - 16; |
164 | atom.size -= 16; |
165 | target = data; |
166 | break; |
167 | default: |
168 | skip_size = data_size - 8; |
169 | str_size = 0; |
170 | break; |
171 | } |
172 | |
173 | if (target) { |
174 | str_size = FFMIN3(sizeof(data)-1, str_size, atom.size); |
175 | avio_read(pb, target, str_size); |
176 | target[str_size] = 0; |
177 | } |
178 | atom.size -= skip_size; |
179 | |
180 | // If we didn't read the full data chunk for the sub-atom, skip to the end of it. |
181 | if (skip_size > str_size) avio_skip(pb, skip_size - str_size); |
182 | } |
183 | |
184 | if (*key && *data) { |
185 | if (strcmp(key, "iTunSMPB") == 0) { |
186 | int priming, remainder, samples; |
187 | if(sscanf(data, "%*X %X %X %X", &priming, &remainder, &samples) == 3){ |
188 | if(priming>0 && priming<16384) |
189 | sc->start_pad = priming; |
190 | return 1; |
191 | } |
192 | } |
193 | if (strcmp(key, "cdec") == 0) { |
194 | // av_dict_set(&st->metadata, key, data, 0); |
195 | return 1; |
196 | } |
197 | } |
198 | return 0; |
199 | } |
200 | |
201 | static const uint32_t mac_to_unicode[128] = { |
202 | 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1, |
203 | 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8, |
204 | 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3, |
205 | 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC, |
206 | 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF, |
207 | 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8, |
208 | 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211, |
209 | 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8, |
210 | 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB, |
211 | 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153, |
212 | 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA, |
213 | 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02, |
214 | 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1, |
215 | 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4, |
216 | 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC, |
217 | 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7, |
218 | }; |
219 | |
220 | static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len, |
221 | char *dst, int dstlen) |
222 | { |
223 | char *p = dst; |
224 | char *end = dst+dstlen-1; |
225 | int i; |
226 | |
227 | for (i = 0; i < len; i++) { |
228 | uint8_t t, c = avio_r8(pb); |
229 | if (c < 0x80 && p < end) |
230 | *p++ = c; |
231 | else if (p < end) |
232 | PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;); |
233 | } |
234 | *p = 0; |
235 | return p - dst; |
236 | } |
237 | |
238 | static int mov_extract_cover_pic(AVFormatContext *s, AVIOContext *pb, int type, int size, char *value) |
239 | { |
240 | if(s->cover_data){ |
241 | av_log(s, AV_LOG_INFO, "Extract cover picture in other atom!\n"); |
242 | return 0; |
243 | } |
244 | |
245 | s->cover_data = av_malloc(size); |
246 | if(!s->cover_data){ |
247 | av_log(s, AV_LOG_INFO, "no memery, av_alloc failed!\n"); |
248 | return -1; |
249 | } |
250 | s->cover_data_len = size; |
251 | avio_read(pb, s->cover_data, size); |
252 | |
253 | if (type == 13) |
254 | strcpy(value, "image/jpeg"); // jpeg |
255 | else if (type == 14) |
256 | strcpy(value, "image/png"); // png |
257 | |
258 | return 0; |
259 | } |
260 | |
261 | static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len) |
262 | { |
263 | AVPacket pkt; |
264 | AVStream *st; |
265 | MOVStreamContext *sc; |
266 | enum AVCodecID id; |
267 | int ret; |
268 | |
269 | switch (type) { |
270 | case 0xd: id = AV_CODEC_ID_MJPEG; break; |
271 | case 0xe: id = AV_CODEC_ID_PNG; break; |
272 | case 0x1b: id = AV_CODEC_ID_BMP; break; |
273 | default: |
274 | av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type); |
275 | avio_skip(pb, len); |
276 | return 0; |
277 | } |
278 | |
279 | st = avformat_new_stream(c->fc, NULL); |
280 | if (!st) |
281 | return AVERROR(ENOMEM); |
282 | sc = av_mallocz(sizeof(*sc)); |
283 | if (!sc) |
284 | return AVERROR(ENOMEM); |
285 | st->priv_data = sc; |
286 | |
287 | ret = av_get_packet(pb, &pkt, len); |
288 | if (ret < 0) |
289 | return ret; |
290 | |
291 | st->disposition |= AV_DISPOSITION_ATTACHED_PIC; |
292 | |
293 | st->attached_pic = pkt; |
294 | st->attached_pic.stream_index = st->index; |
295 | st->attached_pic.flags |= AV_PKT_FLAG_KEY; |
296 | |
297 | st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
298 | st->codec->codec_id = id; |
299 | |
300 | return 0; |
301 | } |
302 | |
303 | static int mov_metadata_raw(MOVContext *c, AVIOContext *pb, |
304 | unsigned len, const char *key) |
305 | { |
306 | char *value = av_malloc(len + 1); |
307 | if (!value) |
308 | return AVERROR(ENOMEM); |
309 | avio_read(pb, value, len); |
310 | value[len] = 0; |
311 | return av_dict_set(&c->fc->metadata, key, value, AV_DICT_DONT_STRDUP_VAL); |
312 | } |
313 | |
314 | static int mov_metadata_3gpp_general(MOVContext *c, AVIOContext *pb, |
315 | unsigned len, const char *key) |
316 | { |
317 | char key2[16]; |
318 | uint8_t version; |
319 | uint32_t flags; |
320 | uint8_t pad; |
321 | uint16_t langcode; |
322 | uint16_t shortbytes; |
323 | char language[4] = {0}; |
324 | char byte = 0; |
325 | char str[128] = {0}; |
326 | int offset = 0; |
327 | uint16_t byteOrderMark = 0; |
328 | |
329 | version = avio_r8(pb); // version |
330 | flags = avio_rb24(pb); //flags |
331 | shortbytes = avio_rb16(pb); |
332 | pad = (shortbytes & 0x8000) >> 15; |
333 | langcode = shortbytes & 0x7ffff; |
334 | ff_mov_lang_to_iso639(langcode, language); |
335 | len -= 6; |
336 | |
337 | //read BYTE ORDER MARK |
338 | byteOrderMark = avio_rb16(pb); |
339 | if (byteOrderMark == 0xFEFF) {//UTF-16BE |
340 | len -= 2; |
341 | offset += 2; |
342 | avio_get_str16be(pb, len, str, 128); |
343 | av_dict_set(&c->fc->metadata, key, str, 0); |
344 | } else if (byteOrderMark == 0xFFFE) {//UTF-16LE |
345 | len -= 2; |
346 | offset += 2; |
347 | avio_get_str16le(pb, len, str, 128); |
348 | av_dict_set(&c->fc->metadata, key, str, 0); |
349 | } else { //not BYTE ORDER MARK,UTF-8 format |
350 | avio_seek(pb, -2, SEEK_CUR); |
351 | avio_read(pb, str+offset, len); |
352 | str[len] = 0; |
353 | av_dict_set(&c->fc->metadata, key, str, 0); |
354 | if (*language && strcmp(language, "und")) { |
355 | snprintf(key2, sizeof(key2), "%s-%s", key, language); |
356 | av_dict_set(&c->fc->metadata, key2, str, 0); |
357 | } |
358 | } |
359 | return 0; |
360 | } |
361 | |
362 | static int mov_metadata_3gpp_yrrc(MOVContext *c, AVIOContext *pb, |
363 | unsigned len, const char *key) |
364 | { |
365 | uint8_t version; |
366 | uint32_t flags; |
367 | char byte = 0; |
368 | char str[128] = {0}; |
369 | char year[32] = {0}; |
370 | uint16_t yearCode = 0; |
371 | |
372 | version = avio_r8(pb); // version |
373 | flags = avio_rb24(pb); //flags |
374 | len -= 4; |
375 | |
376 | //get the year when the media was recorded |
377 | yearCode = avio_rb16(pb); |
378 | snprintf(year, sizeof(year), "%d", yearCode); |
379 | av_dict_set(&c->fc->metadata, key, year, 0); |
380 | |
381 | return 0; |
382 | } |
383 | |
384 | static int mov_metadata_3gpp_album(MOVContext *c, AVIOContext *pb, |
385 | unsigned len, const char *key) |
386 | { |
387 | char key2[16]; |
388 | uint8_t version; |
389 | uint32_t flags; |
390 | uint8_t pad; |
391 | uint16_t langcode; |
392 | uint16_t shortbytes; |
393 | char language[4] = {0}; |
394 | char byte = 0; |
395 | int read_byte = 0; |
396 | char str[128] = {0}; |
397 | int offset = 0; |
398 | uint16_t byteOrderMark = 0; |
399 | uint8_t trackNum[16] ={0}; |
400 | |
401 | version = avio_r8(pb); // version |
402 | flags = avio_rb24(pb); //flags |
403 | shortbytes = avio_rb16(pb); |
404 | pad = (shortbytes & 0x8000) >> 15; //pad |
405 | langcode = shortbytes & 0x7ffff; //language |
406 | ff_mov_lang_to_iso639(langcode, language); |
407 | len -= 6; |
408 | |
409 | //get track num |
410 | do { |
411 | byte = avio_r8(pb); |
412 | read_byte++; |
413 | } while(byte != '\0'); |
414 | byte = avio_r8(pb); |
415 | read_byte++; |
416 | snprintf(trackNum, sizeof(trackNum), "%d", byte); |
417 | av_dict_set(&c->fc->metadata, "track", trackNum, 0); |
418 | |
419 | //seek back to get Text of album title |
420 | avio_seek(pb, -read_byte, SEEK_CUR); |
421 | |
422 | //read BYTE ORDER MARK |
423 | byteOrderMark = avio_rb16(pb); |
424 | if (byteOrderMark == 0xFEFF) {//UTF-16BE |
425 | len -= 2; |
426 | offset += 2; |
427 | avio_get_str16be(pb, len, str, 128); |
428 | av_dict_set(&c->fc->metadata, key, str, 0); |
429 | } else if (byteOrderMark == 0xFFFE) {//UTF-16LE |
430 | len -= 2; |
431 | offset += 2; |
432 | avio_get_str16le(pb, len, str, 128); |
433 | av_dict_set(&c->fc->metadata, key, str, 0); |
434 | } else { //not BYTE ORDER MARK,UTF-8 format |
435 | avio_seek(pb, -2, SEEK_CUR); |
436 | avio_read(pb, str+offset, len); |
437 | str[len] = 0; |
438 | av_dict_set(&c->fc->metadata, key, str, 0); |
439 | if (*language && strcmp(language, "und")) { |
440 | snprintf(key2, sizeof(key2), "%s-%s", key, language); |
441 | av_dict_set(&c->fc->metadata, key2, str, 0); |
442 | } |
443 | } |
444 | |
445 | return 0; |
446 | } |
447 | |
448 | static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
449 | { |
450 | #ifdef MOV_EXPORT_ALL_METADATA |
451 | char tmp_key[5]; |
452 | #endif |
453 | char str[1024], key2[16], language[4] = {0}; |
454 | const char *key = NULL; |
455 | uint16_t langcode = 0; |
456 | uint32_t data_type = 0, str_size; |
457 | uint32_t cover_size = 0; |
458 | int skip_read = 0; |
459 | int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL; |
460 | |
461 | if (c->itunes_metadata && atom.type == MKTAG('-','-','-','-')) |
462 | return mov_read_custom_metadata(c, pb, atom); |
463 | |
464 | switch (atom.type) { |
465 | case MKTAG('t', 'i', 't', 'l'): |
466 | key = "title"; |
467 | skip_read = 1; |
468 | parse = mov_metadata_3gpp_general; |
469 | break; |
470 | case MKTAG('a', 'l', 'b', 'm'): |
471 | key = "album"; |
472 | skip_read = 1; |
473 | parse = mov_metadata_3gpp_album; |
474 | break; |
475 | case MKTAG('d', 's', 'c', 'p'): |
476 | key = "description"; |
477 | skip_read = 1; |
478 | parse = mov_metadata_3gpp_general; |
479 | break; |
480 | case MKTAG('p', 'e', 'r', 'f'): |
481 | key = "artist"; |
482 | skip_read = 1; |
483 | parse = mov_metadata_3gpp_general; |
484 | break; |
485 | case MKTAG('y', 'r', 'r', 'c'): |
486 | key = "year"; |
487 | skip_read = 1; |
488 | parse = mov_metadata_3gpp_yrrc; |
489 | break; |
490 | |
491 | case MKTAG(0xa9,'n','a','m'): key = "title"; break; |
492 | case MKTAG(0xa9,'a','u','t'): |
493 | case MKTAG(0xa9,'A','R','T'): key = "artist"; break; |
494 | case MKTAG( 'a','A','R','T'): key = "album_artist"; break; |
495 | case MKTAG(0xa9,'w','r','t'): key = "composer"; break; |
496 | case MKTAG( 'c','p','r','t'): |
497 | case MKTAG(0xa9,'c','p','y'): key = "copyright"; break; |
498 | case MKTAG(0xa9,'g','r','p'): key = "grouping"; break; |
499 | case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break; |
500 | case MKTAG(0xa9,'c','m','t'): |
501 | case MKTAG(0xa9,'i','n','f'): key = "comment"; break; |
502 | case MKTAG(0xa9,'a','l','b'): key = "album"; break; |
503 | case MKTAG(0xa9,'d','a','y'): key = "date"; break; |
504 | case MKTAG(0xa9,'g','e','n'): key = "genre"; break; |
505 | case MKTAG( 'g','n','r','e'): key = "genre"; |
506 | parse = mov_metadata_gnre; break; |
507 | case MKTAG(0xa9,'t','o','o'): |
508 | case MKTAG(0xa9,'s','w','r'): key = "encoder"; break; |
509 | case MKTAG(0xa9,'e','n','c'): key = "encoder"; break; |
510 | case MKTAG(0xa9,'m','a','k'): key = "make"; break; |
511 | case MKTAG(0xa9,'m','o','d'): key = "model"; break; |
512 | case MKTAG(0xa9,'x','y','z'): key = "location"; break; |
513 | case MKTAG( 'd','e','s','c'): key = "description";break; |
514 | case MKTAG( 'l','d','e','s'): key = "synopsis"; break; |
515 | case MKTAG( 't','v','s','h'): key = "show"; break; |
516 | case MKTAG( 't','v','e','n'): key = "episode_id";break; |
517 | case MKTAG( 't','v','n','n'): key = "network"; break; |
518 | case MKTAG( 't','r','k','n'): key = "track"; |
519 | parse = mov_metadata_track_or_disc_number; break; |
520 | case MKTAG( 'd','i','s','k'): key = "disc"; |
521 | parse = mov_metadata_track_or_disc_number; break; |
522 | case MKTAG( 't','v','e','s'): key = "episode_sort"; |
523 | parse = mov_metadata_int8_bypass_padding; break; |
524 | case MKTAG( 't','v','s','n'): key = "season_number"; |
525 | parse = mov_metadata_int8_bypass_padding; break; |
526 | case MKTAG( 's','t','i','k'): key = "media_type"; |
527 | parse = mov_metadata_int8_no_padding; break; |
528 | case MKTAG( 'h','d','v','d'): key = "hd_video"; |
529 | parse = mov_metadata_int8_no_padding; break; |
530 | case MKTAG( 'p','g','a','p'): key = "gapless_playback"; |
531 | parse = mov_metadata_int8_no_padding; break; |
532 | case MKTAG( '@','P','R','M'): |
533 | return mov_metadata_raw(c, pb, atom.size, "premiere_version"); |
534 | case MKTAG( '@','P','R','Q'): |
535 | return mov_metadata_raw(c, pb, atom.size, "quicktime_version"); |
536 | } |
537 | |
538 | if (c->itunes_metadata && atom.size > 8) { |
539 | int data_size = avio_rb32(pb); |
540 | int tag = avio_rl32(pb); |
541 | if (tag == MKTAG('d','a','t','a')) { |
542 | data_type = avio_rb32(pb); // type |
543 | avio_rb32(pb); // unknown |
544 | str_size = data_size - 16; |
545 | cover_size = data_size -16; |
546 | atom.size -= 16; |
547 | |
548 | if (atom.type == MKTAG('c', 'o', 'v', 'r')) { |
549 | int ret = mov_read_covr(c, pb, data_type, str_size); |
550 | if (ret < 0) { |
551 | av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n"); |
552 | return ret; |
553 | } |
554 | } |
555 | } else return 0; |
556 | } else if (atom.size > 4 && key && !c->itunes_metadata) { |
557 | if (skip_read) { |
558 | str_size = atom.size; |
559 | } |
560 | else{ |
561 | str_size = avio_rb16(pb); // string length |
562 | langcode = avio_rb16(pb); |
563 | ff_mov_lang_to_iso639(langcode, language); |
564 | atom.size -= 4; |
565 | } |
566 | } else |
567 | str_size = atom.size; |
568 | |
569 | #ifdef MOV_EXPORT_ALL_METADATA |
570 | if (!key) { |
571 | snprintf(tmp_key, 5, "%.4s", (char*)&atom.type); |
572 | key = tmp_key; |
573 | } |
574 | #endif |
575 | |
576 | if (!key) |
577 | return 0; |
578 | if (atom.size < 0) |
579 | return AVERROR_INVALIDDATA; |
580 | |
581 | str_size = FFMIN3(sizeof(str)-1, str_size, atom.size); |
582 | |
583 | if (parse) |
584 | parse(c, pb, str_size, key); |
585 | else { |
586 | if (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff))) { // MAC Encoded |
587 | mov_read_mac_string(c, pb, str_size, str, sizeof(str)); |
588 | } else if (data_type == 13 || data_type == 14){ |
589 | mov_extract_cover_pic(c->fc, pb, data_type, cover_size, str); |
590 | } else { |
591 | avio_read(pb, str, str_size); |
592 | str[str_size] = 0; |
593 | } |
594 | // Android MP4 writer put an additional '/' at the end, discard it. |
595 | // The CTS test seems the added '/' is not needed. |
596 | if ((atom.type == MKTAG(0xa9,'x','y','z')) && (str[str_size-1] == 0x2f)) { |
597 | str[str_size-1] = 0; |
598 | } |
599 | av_dict_set(&c->fc->metadata, key, str, 0); |
600 | if (*language && strcmp(language, "und")) { |
601 | snprintf(key2, sizeof(key2), "%s-%s", key, language); |
602 | av_dict_set(&c->fc->metadata, key2, str, 0); |
603 | } |
604 | } |
605 | av_dlog(c->fc, "lang \"%3s\" ", language); |
606 | av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n", |
607 | key, str, (char*)&atom.type, str_size, atom.size); |
608 | |
609 | return 0; |
610 | } |
611 | |
612 | static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
613 | { |
614 | int64_t start; |
615 | int i, nb_chapters, str_len, version; |
616 | char str[256+1]; |
617 | |
618 | if ((atom.size -= 5) < 0) |
619 | return 0; |
620 | |
621 | version = avio_r8(pb); |
622 | avio_rb24(pb); |
623 | if (version) |
624 | avio_rb32(pb); // ??? |
625 | nb_chapters = avio_r8(pb); |
626 | |
627 | for (i = 0; i < nb_chapters; i++) { |
628 | if (atom.size < 9) |
629 | return 0; |
630 | |
631 | start = avio_rb64(pb); |
632 | str_len = avio_r8(pb); |
633 | |
634 | if ((atom.size -= 9+str_len) < 0) |
635 | return 0; |
636 | |
637 | avio_read(pb, str, str_len); |
638 | str[str_len] = 0; |
639 | avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str); |
640 | } |
641 | return 0; |
642 | } |
643 | |
644 | #define MIN_DATA_ENTRY_BOX_SIZE 12 |
645 | static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
646 | { |
647 | AVStream *st; |
648 | MOVStreamContext *sc; |
649 | int entries, i, j; |
650 | |
651 | if (c->fc->nb_streams < 1) |
652 | return 0; |
653 | st = c->fc->streams[c->fc->nb_streams-1]; |
654 | sc = st->priv_data; |
655 | |
656 | avio_rb32(pb); // version + flags |
657 | entries = avio_rb32(pb); |
658 | if (entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 || |
659 | entries >= UINT_MAX / sizeof(*sc->drefs)) |
660 | return AVERROR_INVALIDDATA; |
661 | av_free(sc->drefs); |
662 | sc->drefs_count = 0; |
663 | sc->drefs = av_mallocz(entries * sizeof(*sc->drefs)); |
664 | if (!sc->drefs) |
665 | return AVERROR(ENOMEM); |
666 | sc->drefs_count = entries; |
667 | |
668 | for (i = 0; i < sc->drefs_count; i++) { |
669 | MOVDref *dref = &sc->drefs[i]; |
670 | uint32_t size = avio_rb32(pb); |
671 | int64_t next = avio_tell(pb) + size - 4; |
672 | |
673 | if (size < 12) |
674 | return AVERROR_INVALIDDATA; |
675 | |
676 | dref->type = avio_rl32(pb); |
677 | avio_rb32(pb); // version + flags |
678 | av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size); |
679 | |
680 | if (dref->type == MKTAG('a','l','i','s') && size > 150) { |
681 | /* macintosh alias record */ |
682 | uint16_t volume_len, len; |
683 | int16_t type; |
684 | |
685 | avio_skip(pb, 10); |
686 | |
687 | volume_len = avio_r8(pb); |
688 | volume_len = FFMIN(volume_len, 27); |
689 | avio_read(pb, dref->volume, 27); |
690 | dref->volume[volume_len] = 0; |
691 | av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len); |
692 | |
693 | avio_skip(pb, 12); |
694 | |
695 | len = avio_r8(pb); |
696 | len = FFMIN(len, 63); |
697 | avio_read(pb, dref->filename, 63); |
698 | dref->filename[len] = 0; |
699 | av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len); |
700 | |
701 | avio_skip(pb, 16); |
702 | |
703 | /* read next level up_from_alias/down_to_target */ |
704 | dref->nlvl_from = avio_rb16(pb); |
705 | dref->nlvl_to = avio_rb16(pb); |
706 | av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n", |
707 | dref->nlvl_from, dref->nlvl_to); |
708 | |
709 | avio_skip(pb, 16); |
710 | |
711 | for (type = 0; type != -1 && avio_tell(pb) < next; ) { |
712 | if(url_feof(pb)) |
713 | return AVERROR_EOF; |
714 | type = avio_rb16(pb); |
715 | len = avio_rb16(pb); |
716 | av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len); |
717 | if (len&1) |
718 | len += 1; |
719 | if (type == 2) { // absolute path |
720 | av_free(dref->path); |
721 | dref->path = av_mallocz(len+1); |
722 | if (!dref->path) |
723 | return AVERROR(ENOMEM); |
724 | avio_read(pb, dref->path, len); |
725 | if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) { |
726 | len -= volume_len; |
727 | memmove(dref->path, dref->path+volume_len, len); |
728 | dref->path[len] = 0; |
729 | } |
730 | for (j = 0; j < len; j++) |
731 | if (dref->path[j] == ':') |
732 | dref->path[j] = '/'; |
733 | av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path); |
734 | } else if (type == 0) { // directory name |
735 | av_free(dref->dir); |
736 | dref->dir = av_malloc(len+1); |
737 | if (!dref->dir) |
738 | return AVERROR(ENOMEM); |
739 | avio_read(pb, dref->dir, len); |
740 | dref->dir[len] = 0; |
741 | for (j = 0; j < len; j++) |
742 | if (dref->dir[j] == ':') |
743 | dref->dir[j] = '/'; |
744 | av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir); |
745 | } else |
746 | avio_skip(pb, len); |
747 | } |
748 | } |
749 | avio_seek(pb, next, SEEK_SET); |
750 | } |
751 | return 0; |
752 | } |
753 | |
754 | static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
755 | { |
756 | AVStream *st; |
757 | uint32_t type; |
758 | uint32_t av_unused ctype; |
759 | int title_size; |
760 | char *title_str; |
761 | |
762 | if (c->fc->nb_streams < 1) // meta before first trak |
763 | return 0; |
764 | |
765 | st = c->fc->streams[c->fc->nb_streams-1]; |
766 | |
767 | avio_r8(pb); /* version */ |
768 | avio_rb24(pb); /* flags */ |
769 | |
770 | /* component type */ |
771 | ctype = avio_rl32(pb); |
772 | type = avio_rl32(pb); /* component subtype */ |
773 | |
774 | av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype); |
775 | av_dlog(c->fc, "stype= %.4s\n", (char*)&type); |
776 | |
777 | if (type == MKTAG('v','i','d','e')) |
778 | st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
779 | else if (type == MKTAG('s','o','u','n')) |
780 | st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
781 | else if (type == MKTAG('m','1','a',' ')) |
782 | st->codec->codec_id = AV_CODEC_ID_MP2; |
783 | else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p'))) |
784 | st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; |
785 | |
786 | avio_rb32(pb); /* component manufacture */ |
787 | avio_rb32(pb); /* component flags */ |
788 | avio_rb32(pb); /* component flags mask */ |
789 | |
790 | title_size = atom.size - 24; |
791 | if (title_size > 0) { |
792 | title_str = av_malloc(title_size + 1); /* Add null terminator */ |
793 | if (!title_str) |
794 | return AVERROR(ENOMEM); |
795 | avio_read(pb, title_str, title_size); |
796 | title_str[title_size] = 0; |
797 | if (title_str[0]) |
798 | av_dict_set(&st->metadata, "handler_name", title_str + |
799 | (!c->isom && title_str[0] == title_size - 1), 0); |
800 | av_freep(&title_str); |
801 | } |
802 | |
803 | return 0; |
804 | } |
805 | |
806 | int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom) |
807 | { |
808 | AVStream *st; |
809 | int tag; |
810 | |
811 | if (fc->nb_streams < 1) |
812 | return 0; |
813 | st = fc->streams[fc->nb_streams-1]; |
814 | |
815 | avio_rb32(pb); /* version + flags */ |
816 | ff_mp4_read_descr(fc, pb, &tag); |
817 | if (tag == MP4ESDescrTag) { |
818 | ff_mp4_parse_es_descr(pb, NULL); |
819 | } else |
820 | avio_rb16(pb); /* ID */ |
821 | |
822 | ff_mp4_read_descr(fc, pb, &tag); |
823 | if (tag == MP4DecConfigDescrTag) |
824 | ff_mp4_read_dec_config_descr(fc, st, pb); |
825 | return 0; |
826 | } |
827 | |
828 | static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
829 | { |
830 | return ff_mov_read_esds(c->fc, pb, atom); |
831 | } |
832 | |
833 | static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
834 | { |
835 | AVStream *st; |
836 | int ac3info, acmod, lfeon, bsmod; |
837 | |
838 | if (c->fc->nb_streams < 1) |
839 | return 0; |
840 | st = c->fc->streams[c->fc->nb_streams-1]; |
841 | |
842 | ac3info = avio_rb24(pb); |
843 | bsmod = (ac3info >> 14) & 0x7; |
844 | acmod = (ac3info >> 11) & 0x7; |
845 | lfeon = (ac3info >> 10) & 0x1; |
846 | st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon; |
847 | st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod]; |
848 | if (lfeon) |
849 | st->codec->channel_layout |= AV_CH_LOW_FREQUENCY; |
850 | st->codec->audio_service_type = bsmod; |
851 | if (st->codec->channels > 1 && bsmod == 0x7) |
852 | st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE; |
853 | |
854 | return 0; |
855 | } |
856 | |
857 | static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
858 | { |
859 | AVStream *st; |
860 | int eac3info, acmod, lfeon, bsmod; |
861 | |
862 | if (c->fc->nb_streams < 1) |
863 | return 0; |
864 | st = c->fc->streams[c->fc->nb_streams-1]; |
865 | |
866 | /* No need to parse fields for additional independent substreams and its |
867 | * associated dependent substreams since libavcodec's E-AC-3 decoder |
868 | * does not support them yet. */ |
869 | avio_rb16(pb); /* data_rate and num_ind_sub */ |
870 | eac3info = avio_rb24(pb); |
871 | bsmod = (eac3info >> 12) & 0x1f; |
872 | acmod = (eac3info >> 9) & 0x7; |
873 | lfeon = (eac3info >> 8) & 0x1; |
874 | st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod]; |
875 | if (lfeon) |
876 | st->codec->channel_layout |= AV_CH_LOW_FREQUENCY; |
877 | st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout); |
878 | st->codec->audio_service_type = bsmod; |
879 | if (st->codec->channels > 1 && bsmod == 0x7) |
880 | st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE; |
881 | |
882 | return 0; |
883 | } |
884 | |
885 | static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
886 | { |
887 | AVStream *st; |
888 | |
889 | if (c->fc->nb_streams < 1) |
890 | return 0; |
891 | st = c->fc->streams[c->fc->nb_streams-1]; |
892 | |
893 | if (atom.size < 16) |
894 | return 0; |
895 | |
896 | /* skip version and flags */ |
897 | avio_skip(pb, 4); |
898 | |
899 | ff_mov_read_chan(c->fc, pb, st, atom.size - 4); |
900 | |
901 | return 0; |
902 | } |
903 | |
904 | static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
905 | { |
906 | AVStream *st; |
907 | |
908 | if (c->fc->nb_streams < 1) |
909 | return 0; |
910 | st = c->fc->streams[c->fc->nb_streams-1]; |
911 | |
912 | if (ff_get_wav_header(pb, st->codec, atom.size) < 0) { |
913 | av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n"); |
914 | } |
915 | |
916 | return 0; |
917 | } |
918 | |
919 | static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
920 | { |
921 | const int num = avio_rb32(pb); |
922 | const int den = avio_rb32(pb); |
923 | AVStream *st; |
924 | |
925 | if (c->fc->nb_streams < 1) |
926 | return 0; |
927 | st = c->fc->streams[c->fc->nb_streams-1]; |
928 | |
929 | if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default |
930 | (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) { |
931 | av_log(c->fc, AV_LOG_WARNING, |
932 | "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n", |
933 | st->sample_aspect_ratio.num, st->sample_aspect_ratio.den, |
934 | num, den); |
935 | } else if (den != 0) { |
936 | st->sample_aspect_ratio.num = num; |
937 | st->sample_aspect_ratio.den = den; |
938 | } |
939 | return 0; |
940 | } |
941 | |
942 | /* this atom contains actual media data */ |
943 | static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
944 | { |
945 | if (atom.size == 0) /* wrong one (MP4) */ |
946 | return 0; |
947 | c->found_mdat=1; |
948 | return 0; /* now go for moov */ |
949 | } |
950 | |
951 | /* read major brand, minor version and compatible brands and store them as metadata */ |
952 | static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
953 | { |
954 | uint32_t minor_ver; |
955 | int comp_brand_size; |
956 | char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */ |
957 | char* comp_brands_str; |
958 | uint8_t type[5] = {0}; |
959 | |
960 | avio_read(pb, type, 4); |
961 | if (strcmp(type, "qt ")) |
962 | c->isom = 1; |
963 | av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type); |
964 | av_dict_set(&c->fc->metadata, "major_brand", type, 0); |
965 | minor_ver = avio_rb32(pb); /* minor version */ |
966 | snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver); |
967 | av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0); |
968 | |
969 | comp_brand_size = atom.size - 8; |
970 | if (comp_brand_size < 0) |
971 | return AVERROR_INVALIDDATA; |
972 | comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */ |
973 | if (!comp_brands_str) |
974 | return AVERROR(ENOMEM); |
975 | avio_read(pb, comp_brands_str, comp_brand_size); |
976 | comp_brands_str[comp_brand_size] = 0; |
977 | av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0); |
978 | av_freep(&comp_brands_str); |
979 | |
980 | return 0; |
981 | } |
982 | |
983 | /* this atom should contain all header atoms */ |
984 | static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
985 | { |
986 | int ret; |
987 | |
988 | if (c->found_moov) { |
989 | av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n"); |
990 | avio_skip(pb, atom.size); |
991 | return 0; |
992 | } |
993 | |
994 | if ((ret = mov_read_default(c, pb, atom)) < 0) |
995 | return ret; |
996 | /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */ |
997 | /* so we don't parse the whole file if over a network */ |
998 | c->found_moov=1; |
999 | return 0; /* now go for mdat */ |
1000 | } |
1001 | |
1002 | static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1003 | { |
1004 | c->fragment.moof_offset = avio_tell(pb) - 8; |
1005 | av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset); |
1006 | return mov_read_default(c, pb, atom); |
1007 | } |
1008 | |
1009 | static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time) |
1010 | { |
1011 | char buffer[32]; |
1012 | if (time) { |
1013 | struct tm *ptm; |
1014 | time_t timet; |
1015 | if(time >= 2082844800) |
1016 | time -= 2082844800; /* seconds between 1904-01-01 and Epoch */ |
1017 | timet = time; |
1018 | ptm = gmtime(&timet); |
1019 | if (!ptm) return; |
1020 | strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm); |
1021 | av_dict_set(metadata, "creation_time", buffer, 0); |
1022 | } |
1023 | } |
1024 | |
1025 | static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1026 | { |
1027 | AVStream *st; |
1028 | MOVStreamContext *sc; |
1029 | int version; |
1030 | char language[4] = {0}; |
1031 | unsigned lang; |
1032 | int64_t creation_time; |
1033 | |
1034 | if (c->fc->nb_streams < 1) |
1035 | return 0; |
1036 | st = c->fc->streams[c->fc->nb_streams-1]; |
1037 | sc = st->priv_data; |
1038 | |
1039 | if (sc->time_scale) { |
1040 | av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n"); |
1041 | return AVERROR_INVALIDDATA; |
1042 | } |
1043 | |
1044 | version = avio_r8(pb); |
1045 | if (version > 1) { |
1046 | avpriv_request_sample(c->fc, "Version %d", version); |
1047 | return AVERROR_PATCHWELCOME; |
1048 | } |
1049 | avio_rb24(pb); /* flags */ |
1050 | if (version == 1) { |
1051 | creation_time = avio_rb64(pb); |
1052 | avio_rb64(pb); |
1053 | } else { |
1054 | creation_time = avio_rb32(pb); |
1055 | avio_rb32(pb); /* modification time */ |
1056 | } |
1057 | mov_metadata_creation_time(&st->metadata, creation_time); |
1058 | |
1059 | sc->time_scale = avio_rb32(pb); |
1060 | st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */ |
1061 | |
1062 | lang = avio_rb16(pb); /* language */ |
1063 | if (ff_mov_lang_to_iso639(lang, language)) |
1064 | av_dict_set(&st->metadata, "language", language, 0); |
1065 | avio_rb16(pb); /* quality */ |
1066 | |
1067 | return 0; |
1068 | } |
1069 | |
1070 | static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1071 | { |
1072 | int64_t creation_time; |
1073 | int version = avio_r8(pb); /* version */ |
1074 | avio_rb24(pb); /* flags */ |
1075 | |
1076 | if (version == 1) { |
1077 | creation_time = avio_rb64(pb); |
1078 | avio_rb64(pb); |
1079 | } else { |
1080 | creation_time = avio_rb32(pb); |
1081 | avio_rb32(pb); /* modification time */ |
1082 | } |
1083 | mov_metadata_creation_time(&c->fc->metadata, creation_time); |
1084 | c->time_scale = avio_rb32(pb); /* time scale */ |
1085 | |
1086 | av_dlog(c->fc, "time scale = %i\n", c->time_scale); |
1087 | |
1088 | c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */ |
1089 | // set the AVCodecContext duration because the duration of individual tracks |
1090 | // may be inaccurate |
1091 | if (c->time_scale > 0 && !c->trex_data) |
1092 | c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale); |
1093 | avio_rb32(pb); /* preferred scale */ |
1094 | |
1095 | avio_rb16(pb); /* preferred volume */ |
1096 | |
1097 | avio_skip(pb, 10); /* reserved */ |
1098 | |
1099 | avio_skip(pb, 36); /* display matrix */ |
1100 | |
1101 | avio_rb32(pb); /* preview time */ |
1102 | avio_rb32(pb); /* preview duration */ |
1103 | avio_rb32(pb); /* poster time */ |
1104 | avio_rb32(pb); /* selection time */ |
1105 | avio_rb32(pb); /* selection duration */ |
1106 | avio_rb32(pb); /* current time */ |
1107 | avio_rb32(pb); /* next track ID */ |
1108 | return 0; |
1109 | } |
1110 | |
1111 | static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1112 | { |
1113 | AVStream *st; |
1114 | int little_endian; |
1115 | |
1116 | if (c->fc->nb_streams < 1) |
1117 | return 0; |
1118 | st = c->fc->streams[c->fc->nb_streams-1]; |
1119 | |
1120 | little_endian = avio_rb16(pb) & 0xFF; |
1121 | av_dlog(c->fc, "enda %d\n", little_endian); |
1122 | if (little_endian == 1) { |
1123 | switch (st->codec->codec_id) { |
1124 | case AV_CODEC_ID_PCM_S24BE: |
1125 | st->codec->codec_id = AV_CODEC_ID_PCM_S24LE; |
1126 | break; |
1127 | case AV_CODEC_ID_PCM_S32BE: |
1128 | st->codec->codec_id = AV_CODEC_ID_PCM_S32LE; |
1129 | break; |
1130 | case AV_CODEC_ID_PCM_F32BE: |
1131 | st->codec->codec_id = AV_CODEC_ID_PCM_F32LE; |
1132 | break; |
1133 | case AV_CODEC_ID_PCM_F64BE: |
1134 | st->codec->codec_id = AV_CODEC_ID_PCM_F64LE; |
1135 | break; |
1136 | default: |
1137 | break; |
1138 | } |
1139 | } |
1140 | return 0; |
1141 | } |
1142 | |
1143 | static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1144 | { |
1145 | AVStream *st; |
1146 | unsigned mov_field_order; |
1147 | enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN; |
1148 | |
1149 | if (c->fc->nb_streams < 1) // will happen with jp2 files |
1150 | return 0; |
1151 | st = c->fc->streams[c->fc->nb_streams-1]; |
1152 | if (atom.size < 2) |
1153 | return AVERROR_INVALIDDATA; |
1154 | mov_field_order = avio_rb16(pb); |
1155 | if ((mov_field_order & 0xFF00) == 0x0100) |
1156 | decoded_field_order = AV_FIELD_PROGRESSIVE; |
1157 | else if ((mov_field_order & 0xFF00) == 0x0200) { |
1158 | switch (mov_field_order & 0xFF) { |
1159 | case 0x01: decoded_field_order = AV_FIELD_TT; |
1160 | break; |
1161 | case 0x06: decoded_field_order = AV_FIELD_BB; |
1162 | break; |
1163 | case 0x09: decoded_field_order = AV_FIELD_TB; |
1164 | break; |
1165 | case 0x0E: decoded_field_order = AV_FIELD_BT; |
1166 | break; |
1167 | } |
1168 | } |
1169 | if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) { |
1170 | av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order); |
1171 | } |
1172 | st->codec->field_order = decoded_field_order; |
1173 | |
1174 | return 0; |
1175 | } |
1176 | |
1177 | /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */ |
1178 | static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom, |
1179 | enum AVCodecID codec_id) |
1180 | { |
1181 | AVStream *st; |
1182 | uint64_t size; |
1183 | uint8_t *buf; |
1184 | int err; |
1185 | |
1186 | if (c->fc->nb_streams < 1) // will happen with jp2 files |
1187 | return 0; |
1188 | st= c->fc->streams[c->fc->nb_streams-1]; |
1189 | |
1190 | if (st->codec->codec_id != codec_id) |
1191 | return 0; /* unexpected codec_id - don't mess with extradata */ |
1192 | |
1193 | size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE; |
1194 | if (size > INT_MAX || (uint64_t)atom.size > INT_MAX) |
1195 | return AVERROR_INVALIDDATA; |
1196 | if ((err = av_reallocp(&st->codec->extradata, size)) < 0) { |
1197 | st->codec->extradata_size = 0; |
1198 | return err; |
1199 | } |
1200 | buf = st->codec->extradata + st->codec->extradata_size; |
1201 | st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE; |
1202 | AV_WB32( buf , atom.size + 8); |
1203 | AV_WL32( buf + 4, atom.type); |
1204 | avio_read(pb, buf + 8, atom.size); |
1205 | return 0; |
1206 | } |
1207 | |
1208 | /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */ |
1209 | static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1210 | { |
1211 | return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC); |
1212 | } |
1213 | |
1214 | static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1215 | { |
1216 | return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS); |
1217 | } |
1218 | |
1219 | static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1220 | { |
1221 | return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000); |
1222 | } |
1223 | |
1224 | static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1225 | { |
1226 | return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI); |
1227 | } |
1228 | |
1229 | static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1230 | { |
1231 | int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216); |
1232 | |
1233 | if (!ret && c->fc->nb_streams >= 1) { |
1234 | AVCodecContext *avctx = c->fc->streams[c->fc->nb_streams-1]->codec; |
1235 | if (avctx->extradata_size >= 40) { |
1236 | avctx->height = AV_RB16(&avctx->extradata[36]); |
1237 | avctx->width = AV_RB16(&avctx->extradata[38]); |
1238 | } |
1239 | } |
1240 | return ret; |
1241 | } |
1242 | |
1243 | static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1244 | { |
1245 | AVCodecContext *codec = c->fc->streams[c->fc->nb_streams-1]->codec; |
1246 | if (codec->codec_tag == MKTAG('A', 'V', 'i', 'n') && |
1247 | codec->codec_id == AV_CODEC_ID_H264 && |
1248 | atom.size > 11) { |
1249 | avio_skip(pb, 10); |
1250 | /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */ |
1251 | if (avio_rb16(pb) == 0xd4d) |
1252 | codec->width = 1440; |
1253 | return 0; |
1254 | } |
1255 | |
1256 | return mov_read_avid(c, pb, atom); |
1257 | } |
1258 | |
1259 | static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1260 | { |
1261 | return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3); |
1262 | } |
1263 | |
1264 | static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1265 | { |
1266 | AVStream *st; |
1267 | |
1268 | if (c->fc->nb_streams < 1) |
1269 | return 0; |
1270 | st = c->fc->streams[c->fc->nb_streams-1]; |
1271 | |
1272 | if ((uint64_t)atom.size > (1<<30)) |
1273 | return AVERROR_INVALIDDATA; |
1274 | |
1275 | if (st->codec->codec_id == AV_CODEC_ID_QDM2 || |
1276 | st->codec->codec_id == AV_CODEC_ID_QDMC || |
1277 | st->codec->codec_id == AV_CODEC_ID_SPEEX) { |
1278 | // pass all frma atom to codec, needed at least for QDMC and QDM2 |
1279 | av_free(st->codec->extradata); |
1280 | if (ff_alloc_extradata(st->codec, atom.size)) |
1281 | return AVERROR(ENOMEM); |
1282 | avio_read(pb, st->codec->extradata, atom.size); |
1283 | } else if (atom.size > 8) { /* to read frma, esds atoms */ |
1284 | int ret; |
1285 | if ((ret = mov_read_default(c, pb, atom)) < 0) |
1286 | return ret; |
1287 | } else |
1288 | avio_skip(pb, atom.size); |
1289 | return 0; |
1290 | } |
1291 | |
1292 | /** |
1293 | * This function reads atom content and puts data in extradata without tag |
1294 | * nor size unlike mov_read_extradata. |
1295 | */ |
1296 | static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1297 | { |
1298 | AVStream *st; |
1299 | |
1300 | if (c->fc->nb_streams < 1) |
1301 | return 0; |
1302 | st = c->fc->streams[c->fc->nb_streams-1]; |
1303 | |
1304 | if ((uint64_t)atom.size > (1<<30)) |
1305 | return AVERROR_INVALIDDATA; |
1306 | |
1307 | if (atom.size >= 10) { |
1308 | // Broken files created by legacy versions of libavformat will |
1309 | // wrap a whole fiel atom inside of a glbl atom. |
1310 | unsigned size = avio_rb32(pb); |
1311 | unsigned type = avio_rl32(pb); |
1312 | avio_seek(pb, -8, SEEK_CUR); |
1313 | if (type == MKTAG('f','i','e','l') && size == atom.size) |
1314 | return mov_read_default(c, pb, atom); |
1315 | } |
1316 | av_free(st->codec->extradata); |
1317 | if (ff_alloc_extradata(st->codec, atom.size)) |
1318 | return AVERROR(ENOMEM); |
1319 | avio_read(pb, st->codec->extradata, atom.size); |
1320 | return 0; |
1321 | } |
1322 | |
1323 | static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1324 | { |
1325 | AVStream *st; |
1326 | uint8_t profile_level; |
1327 | |
1328 | if (c->fc->nb_streams < 1) |
1329 | return 0; |
1330 | st = c->fc->streams[c->fc->nb_streams-1]; |
1331 | |
1332 | if (atom.size >= (1<<28) || atom.size < 7) |
1333 | return AVERROR_INVALIDDATA; |
1334 | |
1335 | profile_level = avio_r8(pb); |
1336 | if ((profile_level & 0xf0) != 0xc0) |
1337 | return 0; |
1338 | |
1339 | av_free(st->codec->extradata); |
1340 | if (ff_alloc_extradata(st->codec, atom.size - 7)) |
1341 | return AVERROR(ENOMEM); |
1342 | avio_seek(pb, 6, SEEK_CUR); |
1343 | avio_read(pb, st->codec->extradata, st->codec->extradata_size); |
1344 | return 0; |
1345 | } |
1346 | |
1347 | /** |
1348 | * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself, |
1349 | * but can have extradata appended at the end after the 40 bytes belonging |
1350 | * to the struct. |
1351 | */ |
1352 | static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1353 | { |
1354 | AVStream *st; |
1355 | |
1356 | if (c->fc->nb_streams < 1) |
1357 | return 0; |
1358 | if (atom.size <= 40) |
1359 | return 0; |
1360 | st = c->fc->streams[c->fc->nb_streams-1]; |
1361 | |
1362 | if ((uint64_t)atom.size > (1<<30)) |
1363 | return AVERROR_INVALIDDATA; |
1364 | |
1365 | av_free(st->codec->extradata); |
1366 | if (ff_alloc_extradata(st->codec, atom.size - 40)) |
1367 | return AVERROR(ENOMEM); |
1368 | avio_skip(pb, 40); |
1369 | avio_read(pb, st->codec->extradata, atom.size - 40); |
1370 | return 0; |
1371 | } |
1372 | |
1373 | static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1374 | { |
1375 | AVStream *st; |
1376 | MOVStreamContext *sc; |
1377 | unsigned int i, entries; |
1378 | |
1379 | if (c->fc->nb_streams < 1) |
1380 | return 0; |
1381 | st = c->fc->streams[c->fc->nb_streams-1]; |
1382 | sc = st->priv_data; |
1383 | |
1384 | avio_r8(pb); /* version */ |
1385 | avio_rb24(pb); /* flags */ |
1386 | |
1387 | entries = avio_rb32(pb); |
1388 | |
1389 | if (!entries) |
1390 | return 0; |
1391 | if (entries >= UINT_MAX/sizeof(int64_t)) |
1392 | return AVERROR_INVALIDDATA; |
1393 | |
1394 | sc->chunk_offsets = av_malloc(entries * sizeof(int64_t)); |
1395 | if (!sc->chunk_offsets) |
1396 | return AVERROR(ENOMEM); |
1397 | sc->chunk_count = entries; |
1398 | |
1399 | if (atom.type == MKTAG('s','t','c','o')) |
1400 | for (i = 0; i < entries && !pb->eof_reached; i++) |
1401 | sc->chunk_offsets[i] = avio_rb32(pb); |
1402 | else if (atom.type == MKTAG('c','o','6','4')) |
1403 | for (i = 0; i < entries && !pb->eof_reached; i++) |
1404 | sc->chunk_offsets[i] = avio_rb64(pb); |
1405 | else |
1406 | return AVERROR_INVALIDDATA; |
1407 | |
1408 | sc->chunk_count = i; |
1409 | |
1410 | if (pb->eof_reached) |
1411 | return AVERROR_EOF; |
1412 | |
1413 | return 0; |
1414 | } |
1415 | |
1416 | /** |
1417 | * Compute codec id for 'lpcm' tag. |
1418 | * See CoreAudioTypes and AudioStreamBasicDescription at Apple. |
1419 | */ |
1420 | enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags) |
1421 | { |
1422 | /* lpcm flags: |
1423 | * 0x1 = float |
1424 | * 0x2 = big-endian |
1425 | * 0x4 = signed |
1426 | */ |
1427 | return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0); |
1428 | } |
1429 | |
1430 | static int mov_codec_id(AVStream *st, uint32_t format) |
1431 | { |
1432 | int id = ff_codec_get_id(ff_codec_movaudio_tags, format); |
1433 | |
1434 | if (id <= 0 && |
1435 | ((format & 0xFFFF) == 'm' + ('s' << 8) || |
1436 | (format & 0xFFFF) == 'T' + ('S' << 8))) |
1437 | id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF); |
1438 | |
1439 | if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) { |
1440 | st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
1441 | } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO && |
1442 | /* skip old asf mpeg4 tag */ |
1443 | format && format != MKTAG('m','p','4','s')) { |
1444 | id = ff_codec_get_id(ff_codec_movvideo_tags, format); |
1445 | if (id <= 0) |
1446 | id = ff_codec_get_id(ff_codec_bmp_tags, format); |
1447 | if (id > 0) |
1448 | st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
1449 | else if (st->codec->codec_type == AVMEDIA_TYPE_DATA || |
1450 | (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE && |
1451 | st->codec->codec_id == AV_CODEC_ID_NONE)) { |
1452 | id = ff_codec_get_id(ff_codec_movsubtitle_tags, format); |
1453 | if (id > 0) |
1454 | st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; |
1455 | } |
1456 | } |
1457 | |
1458 | st->codec->codec_tag = format; |
1459 | |
1460 | return id; |
1461 | } |
1462 | |
1463 | static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb, |
1464 | AVStream *st, MOVStreamContext *sc) |
1465 | { |
1466 | unsigned int color_depth, len, j; |
1467 | int color_greyscale; |
1468 | int color_table_id; |
1469 | |
1470 | avio_rb16(pb); /* version */ |
1471 | avio_rb16(pb); /* revision level */ |
1472 | avio_rb32(pb); /* vendor */ |
1473 | avio_rb32(pb); /* temporal quality */ |
1474 | avio_rb32(pb); /* spatial quality */ |
1475 | |
1476 | st->codec->width = avio_rb16(pb); /* width */ |
1477 | st->codec->height = avio_rb16(pb); /* height */ |
1478 | |
1479 | avio_rb32(pb); /* horiz resolution */ |
1480 | avio_rb32(pb); /* vert resolution */ |
1481 | avio_rb32(pb); /* data size, always 0 */ |
1482 | avio_rb16(pb); /* frames per samples */ |
1483 | |
1484 | len = avio_r8(pb); /* codec name, pascal string */ |
1485 | if (len > 31) |
1486 | len = 31; |
1487 | mov_read_mac_string(c, pb, len, st->codec->codec_name, 32); |
1488 | if (len < 31) |
1489 | avio_skip(pb, 31 - len); |
1490 | /* codec_tag YV12 triggers an UV swap in rawdec.c */ |
1491 | if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) { |
1492 | st->codec->codec_tag = MKTAG('I', '4', '2', '0'); |
1493 | st->codec->width &= ~1; |
1494 | st->codec->height &= ~1; |
1495 | } |
1496 | /* Flash Media Server uses tag H263 with Sorenson Spark */ |
1497 | if (st->codec->codec_tag == MKTAG('H','2','6','3') && |
1498 | !memcmp(st->codec->codec_name, "Sorenson H263", 13)) |
1499 | st->codec->codec_id = AV_CODEC_ID_FLV1; |
1500 | |
1501 | st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */ |
1502 | color_table_id = avio_rb16(pb); /* colortable id */ |
1503 | av_dlog(c->fc, "depth %d, ctab id %d\n", |
1504 | st->codec->bits_per_coded_sample, color_table_id); |
1505 | /* figure out the palette situation */ |
1506 | color_depth = st->codec->bits_per_coded_sample & 0x1F; |
1507 | color_greyscale = st->codec->bits_per_coded_sample & 0x20; |
1508 | |
1509 | /* if the depth is 2, 4, or 8 bpp, file is palettized */ |
1510 | if ((color_depth == 2) || (color_depth == 4) || (color_depth == 8)) { |
1511 | /* for palette traversal */ |
1512 | unsigned int color_start, color_count, color_end; |
1513 | unsigned char a, r, g, b; |
1514 | |
1515 | if (color_greyscale) { |
1516 | int color_index, color_dec; |
1517 | /* compute the greyscale palette */ |
1518 | st->codec->bits_per_coded_sample = color_depth; |
1519 | color_count = 1 << color_depth; |
1520 | color_index = 255; |
1521 | color_dec = 256 / (color_count - 1); |
1522 | for (j = 0; j < color_count; j++) { |
1523 | if (st->codec->codec_id == AV_CODEC_ID_CINEPAK){ |
1524 | r = g = b = color_count - 1 - color_index; |
1525 | } else |
1526 | r = g = b = color_index; |
1527 | sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b); |
1528 | color_index -= color_dec; |
1529 | if (color_index < 0) |
1530 | color_index = 0; |
1531 | } |
1532 | } else if (color_table_id) { |
1533 | const uint8_t *color_table; |
1534 | /* if flag bit 3 is set, use the default palette */ |
1535 | color_count = 1 << color_depth; |
1536 | if (color_depth == 2) |
1537 | color_table = ff_qt_default_palette_4; |
1538 | else if (color_depth == 4) |
1539 | color_table = ff_qt_default_palette_16; |
1540 | else |
1541 | color_table = ff_qt_default_palette_256; |
1542 | |
1543 | for (j = 0; j < color_count; j++) { |
1544 | r = color_table[j * 3 + 0]; |
1545 | g = color_table[j * 3 + 1]; |
1546 | b = color_table[j * 3 + 2]; |
1547 | sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b); |
1548 | } |
1549 | } else { |
1550 | /* load the palette from the file */ |
1551 | color_start = avio_rb32(pb); |
1552 | color_count = avio_rb16(pb); |
1553 | color_end = avio_rb16(pb); |
1554 | if ((color_start <= 255) && (color_end <= 255)) { |
1555 | for (j = color_start; j <= color_end; j++) { |
1556 | /* each A, R, G, or B component is 16 bits; |
1557 | * only use the top 8 bits */ |
1558 | a = avio_r8(pb); |
1559 | avio_r8(pb); |
1560 | r = avio_r8(pb); |
1561 | avio_r8(pb); |
1562 | g = avio_r8(pb); |
1563 | avio_r8(pb); |
1564 | b = avio_r8(pb); |
1565 | avio_r8(pb); |
1566 | sc->palette[j] = (a << 24 ) | (r << 16) | (g << 8) | (b); |
1567 | } |
1568 | } |
1569 | } |
1570 | sc->has_palette = 1; |
1571 | } |
1572 | } |
1573 | |
1574 | static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb, |
1575 | AVStream *st, MOVStreamContext *sc) |
1576 | { |
1577 | int bits_per_sample, flags; |
1578 | uint16_t version = avio_rb16(pb); |
1579 | AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE); |
1580 | |
1581 | avio_rb16(pb); /* revision level */ |
1582 | avio_rb32(pb); /* vendor */ |
1583 | |
1584 | st->codec->channels = avio_rb16(pb); /* channel count */ |
1585 | st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */ |
1586 | av_dlog(c->fc, "audio channels %d\n", st->codec->channels); |
1587 | |
1588 | sc->audio_cid = avio_rb16(pb); |
1589 | avio_rb16(pb); /* packet size = 0 */ |
1590 | |
1591 | st->codec->sample_rate = ((avio_rb32(pb) >> 16)); |
1592 | |
1593 | // Read QT version 1 fields. In version 0 these do not exist. |
1594 | av_dlog(c->fc, "version =%d, isom =%d\n", version, c->isom); |
1595 | if (!c->isom || |
1596 | (compatible_brands && strstr(compatible_brands->value, "qt "))) { |
1597 | |
1598 | if (version == 1) { |
1599 | sc->samples_per_frame = avio_rb32(pb); |
1600 | avio_rb32(pb); /* bytes per packet */ |
1601 | sc->bytes_per_frame = avio_rb32(pb); |
1602 | avio_rb32(pb); /* bytes per sample */ |
1603 | } else if (version == 2) { |
1604 | avio_rb32(pb); /* sizeof struct only */ |
1605 | st->codec->sample_rate = av_int2double(avio_rb64(pb)); |
1606 | st->codec->channels = avio_rb32(pb); |
1607 | avio_rb32(pb); /* always 0x7F000000 */ |
1608 | st->codec->bits_per_coded_sample = avio_rb32(pb); |
1609 | |
1610 | flags = avio_rb32(pb); /* lpcm format specific flag */ |
1611 | sc->bytes_per_frame = avio_rb32(pb); |
1612 | sc->samples_per_frame = avio_rb32(pb); |
1613 | if (st->codec->codec_tag == MKTAG('l','p','c','m')) |
1614 | st->codec->codec_id = |
1615 | ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, |
1616 | flags); |
1617 | } |
1618 | } |
1619 | |
1620 | switch (st->codec->codec_id) { |
1621 | case AV_CODEC_ID_PCM_S8: |
1622 | case AV_CODEC_ID_PCM_U8: |
1623 | if (st->codec->bits_per_coded_sample == 16) |
1624 | st->codec->codec_id = AV_CODEC_ID_PCM_S16BE; |
1625 | break; |
1626 | case AV_CODEC_ID_PCM_S16LE: |
1627 | case AV_CODEC_ID_PCM_S16BE: |
1628 | if (st->codec->bits_per_coded_sample == 8) |
1629 | st->codec->codec_id = AV_CODEC_ID_PCM_S8; |
1630 | else if (st->codec->bits_per_coded_sample == 24) |
1631 | st->codec->codec_id = |
1632 | st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ? |
1633 | AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE; |
1634 | break; |
1635 | /* set values for old format before stsd version 1 appeared */ |
1636 | case AV_CODEC_ID_MACE3: |
1637 | sc->samples_per_frame = 6; |
1638 | sc->bytes_per_frame = 2 * st->codec->channels; |
1639 | break; |
1640 | case AV_CODEC_ID_MACE6: |
1641 | sc->samples_per_frame = 6; |
1642 | sc->bytes_per_frame = 1 * st->codec->channels; |
1643 | break; |
1644 | case AV_CODEC_ID_ADPCM_IMA_QT: |
1645 | sc->samples_per_frame = 64; |
1646 | sc->bytes_per_frame = 34 * st->codec->channels; |
1647 | break; |
1648 | case AV_CODEC_ID_GSM: |
1649 | sc->samples_per_frame = 160; |
1650 | sc->bytes_per_frame = 33; |
1651 | break; |
1652 | default: |
1653 | break; |
1654 | } |
1655 | |
1656 | bits_per_sample = av_get_bits_per_sample(st->codec->codec_id); |
1657 | if (bits_per_sample) { |
1658 | st->codec->bits_per_coded_sample = bits_per_sample; |
1659 | sc->sample_size = (bits_per_sample >> 3) * st->codec->channels; |
1660 | } |
1661 | } |
1662 | |
1663 | static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb, |
1664 | AVStream *st, MOVStreamContext *sc, |
1665 | int size) |
1666 | { |
1667 | // ttxt stsd contains display flags, justification, background |
1668 | // color, fonts, and default styles, so fake an atom to read it |
1669 | MOVAtom fake_atom = { .size = size }; |
1670 | // mp4s contains a regular esds atom |
1671 | if (st->codec->codec_tag != AV_RL32("mp4s")) |
1672 | mov_read_glbl(c, pb, fake_atom); |
1673 | st->codec->width = sc->width; |
1674 | st->codec->height = sc->height; |
1675 | } |
1676 | |
1677 | static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb, |
1678 | AVStream *st, MOVStreamContext *sc, |
1679 | int size) |
1680 | { |
1681 | if (st->codec->codec_tag == MKTAG('t','m','c','d')) { |
1682 | if (ff_alloc_extradata(st->codec, size)) |
1683 | return AVERROR(ENOMEM); |
1684 | avio_read(pb, st->codec->extradata, size); |
1685 | if (size > 16) { |
1686 | MOVStreamContext *tmcd_ctx = st->priv_data; |
1687 | int val; |
1688 | val = AV_RB32(st->codec->extradata + 4); |
1689 | tmcd_ctx->tmcd_flags = val; |
1690 | if (val & 1) |
1691 | st->codec->flags2 |= CODEC_FLAG2_DROP_FRAME_TIMECODE; |
1692 | st->codec->time_base.den = st->codec->extradata[16]; /* number of frame */ |
1693 | st->codec->time_base.num = 1; |
1694 | } |
1695 | } else { |
1696 | /* other codec type, just skip (rtp, mp4s ...) */ |
1697 | avio_skip(pb, size); |
1698 | } |
1699 | return 0; |
1700 | } |
1701 | |
1702 | static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb, |
1703 | AVStream *st, MOVStreamContext *sc) |
1704 | { |
1705 | if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && |
1706 | !st->codec->sample_rate && sc->time_scale > 1) |
1707 | st->codec->sample_rate = sc->time_scale; |
1708 | |
1709 | /* special codec parameters handling */ |
1710 | switch (st->codec->codec_id) { |
1711 | #if CONFIG_DV_DEMUXER |
1712 | case AV_CODEC_ID_DVAUDIO: |
1713 | c->dv_fctx = avformat_alloc_context(); |
1714 | c->dv_demux = avpriv_dv_init_demux(c->dv_fctx); |
1715 | if (!c->dv_demux) { |
1716 | av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n"); |
1717 | return AVERROR(ENOMEM); |
1718 | } |
1719 | sc->dv_audio_container = 1; |
1720 | st->codec->codec_id = AV_CODEC_ID_PCM_S16LE; |
1721 | break; |
1722 | #endif |
1723 | /* no ifdef since parameters are always those */ |
1724 | case AV_CODEC_ID_QCELP: |
1725 | st->codec->channels = 1; |
1726 | // force sample rate for qcelp when not stored in mov |
1727 | if (st->codec->codec_tag != MKTAG('Q','c','l','p')) |
1728 | st->codec->sample_rate = 8000; |
1729 | break; |
1730 | case AV_CODEC_ID_AMR_NB: |
1731 | st->codec->channels = 1; |
1732 | /* force sample rate for amr, stsd in 3gp does not store sample rate */ |
1733 | st->codec->sample_rate = 8000; |
1734 | break; |
1735 | case AV_CODEC_ID_AMR_WB: |
1736 | st->codec->channels = 1; |
1737 | st->codec->sample_rate = 16000; |
1738 | break; |
1739 | case AV_CODEC_ID_MP2: |
1740 | case AV_CODEC_ID_MP3: |
1741 | /* force type after stsd for m1a hdlr */ |
1742 | st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
1743 | st->need_parsing = AVSTREAM_PARSE_FULL; |
1744 | break; |
1745 | case AV_CODEC_ID_GSM: |
1746 | case AV_CODEC_ID_ADPCM_MS: |
1747 | case AV_CODEC_ID_ADPCM_IMA_WAV: |
1748 | case AV_CODEC_ID_ILBC: |
1749 | case AV_CODEC_ID_MACE3: |
1750 | case AV_CODEC_ID_MACE6: |
1751 | case AV_CODEC_ID_QDM2: |
1752 | st->codec->block_align = sc->bytes_per_frame; |
1753 | break; |
1754 | case AV_CODEC_ID_ALAC: |
1755 | if (st->codec->extradata_size == 36) { |
1756 | st->codec->channels = AV_RB8 (st->codec->extradata + 21); |
1757 | st->codec->sample_rate = AV_RB32(st->codec->extradata + 32); |
1758 | } |
1759 | break; |
1760 | case AV_CODEC_ID_AC3: |
1761 | st->need_parsing = AVSTREAM_PARSE_FULL; |
1762 | break; |
1763 | case AV_CODEC_ID_MPEG1VIDEO: |
1764 | st->need_parsing = AVSTREAM_PARSE_FULL; |
1765 | break; |
1766 | case AV_CODEC_ID_VC1: |
1767 | st->need_parsing = AVSTREAM_PARSE_FULL; |
1768 | break; |
1769 | case AV_CODEC_ID_HEVC: |
1770 | st->need_parsing = AVSTREAM_PARSE_HEADERS; |
1771 | break; |
1772 | default: |
1773 | break; |
1774 | } |
1775 | return 0; |
1776 | } |
1777 | |
1778 | static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb, |
1779 | int codec_tag, int format, |
1780 | int size) |
1781 | { |
1782 | int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format); |
1783 | |
1784 | if (codec_tag && |
1785 | (codec_tag != format && |
1786 | (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id |
1787 | : codec_tag != MKTAG('j','p','e','g')))) { |
1788 | /* Multiple fourcc, we skip JPEG. This is not correct, we should |
1789 | * export it as a separate AVStream but this needs a few changes |
1790 | * in the MOV demuxer, patch welcome. */ |
1791 | |
1792 | av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n"); |
1793 | avio_skip(pb, size); |
1794 | return 1; |
1795 | } |
1796 | if ( codec_tag == AV_RL32("avc1") || |
1797 | codec_tag == AV_RL32("hvc1") || |
1798 | codec_tag == AV_RL32("hev1") |
1799 | ) |
1800 | av_log(c->fc, AV_LOG_WARNING, "Concatenated H.264 or H.265 might not play correctly.\n"); |
1801 | |
1802 | return 0; |
1803 | } |
1804 | |
1805 | int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) |
1806 | { |
1807 | AVStream *st; |
1808 | MOVStreamContext *sc; |
1809 | int pseudo_stream_id; |
1810 | |
1811 | if (c->fc->nb_streams < 1) |
1812 | return 0; |
1813 | st = c->fc->streams[c->fc->nb_streams-1]; |
1814 | sc = st->priv_data; |
1815 | |
1816 | for (pseudo_stream_id = 0; |
1817 | pseudo_stream_id < entries && !pb->eof_reached; |
1818 | pseudo_stream_id++) { |
1819 | //Parsing Sample description table |
1820 | enum AVCodecID id; |
1821 | int ret, dref_id = 1; |
1822 | MOVAtom a = { AV_RL32("stsd") }; |
1823 | int64_t start_pos = avio_tell(pb); |
1824 | int64_t size = avio_rb32(pb); /* size */ |
1825 | uint32_t format = avio_rl32(pb); /* data format */ |
1826 | |
1827 | if (size >= 16) { |
1828 | avio_rb32(pb); /* reserved */ |
1829 | avio_rb16(pb); /* reserved */ |
1830 | dref_id = avio_rb16(pb); |
1831 | }else if (size <= 7){ |
1832 | av_log(c->fc, AV_LOG_ERROR, "invalid size %"PRId64" in stsd\n", size); |
1833 | return AVERROR_INVALIDDATA; |
1834 | } |
1835 | |
1836 | if (mov_skip_multiple_stsd(c, pb, st->codec->codec_tag, format, |
1837 | size - (avio_tell(pb) - start_pos))) |
1838 | continue; |
1839 | |
1840 | sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id; |
1841 | sc->dref_id= dref_id; |
1842 | |
1843 | id = mov_codec_id(st, format); |
1844 | |
1845 | av_dlog(c->fc, "size=%"PRId64" 4CC= %c%c%c%c codec_type=%d\n", size, |
1846 | (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, |
1847 | (format >> 24) & 0xff, st->codec->codec_type); |
1848 | |
1849 | if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) { |
1850 | st->codec->codec_id = id; |
1851 | mov_parse_stsd_video(c, pb, st, sc); |
1852 | } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) { |
1853 | st->codec->codec_id = id; |
1854 | mov_parse_stsd_audio(c, pb, st, sc); |
1855 | } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){ |
1856 | st->codec->codec_id = id; |
1857 | mov_parse_stsd_subtitle(c, pb, st, sc, |
1858 | size - (avio_tell(pb) - start_pos)); |
1859 | } else { |
1860 | ret = mov_parse_stsd_data(c, pb, st, sc, |
1861 | size - (avio_tell(pb) - start_pos)); |
1862 | if (ret < 0) |
1863 | return ret; |
1864 | } |
1865 | /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */ |
1866 | a.size = size - (avio_tell(pb) - start_pos); |
1867 | if (a.size > 8) { |
1868 | if ((ret = mov_read_default(c, pb, a)) < 0) |
1869 | return ret; |
1870 | } else if (a.size > 0) |
1871 | avio_skip(pb, a.size); |
1872 | } |
1873 | |
1874 | if (pb->eof_reached) |
1875 | return AVERROR_EOF; |
1876 | |
1877 | return mov_finalize_stsd_codec(c, pb, st, sc); |
1878 | } |
1879 | |
1880 | static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1881 | { |
1882 | int entries; |
1883 | |
1884 | avio_r8(pb); /* version */ |
1885 | avio_rb24(pb); /* flags */ |
1886 | entries = avio_rb32(pb); |
1887 | |
1888 | return ff_mov_read_stsd_entries(c, pb, entries); |
1889 | } |
1890 | |
1891 | static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1892 | { |
1893 | AVStream *st; |
1894 | MOVStreamContext *sc; |
1895 | unsigned int i, entries; |
1896 | |
1897 | if (c->fc->nb_streams < 1) |
1898 | return 0; |
1899 | st = c->fc->streams[c->fc->nb_streams-1]; |
1900 | sc = st->priv_data; |
1901 | |
1902 | avio_r8(pb); /* version */ |
1903 | avio_rb24(pb); /* flags */ |
1904 | |
1905 | entries = avio_rb32(pb); |
1906 | |
1907 | av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries); |
1908 | |
1909 | if (!entries) |
1910 | return 0; |
1911 | if (entries >= UINT_MAX / sizeof(*sc->stsc_data)) |
1912 | return AVERROR_INVALIDDATA; |
1913 | sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data)); |
1914 | if (!sc->stsc_data) |
1915 | return AVERROR(ENOMEM); |
1916 | |
1917 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
1918 | sc->stsc_data[i].first = avio_rb32(pb); |
1919 | sc->stsc_data[i].count = avio_rb32(pb); |
1920 | sc->stsc_data[i].id = avio_rb32(pb); |
1921 | } |
1922 | |
1923 | sc->stsc_count = i; |
1924 | |
1925 | if (pb->eof_reached) |
1926 | return AVERROR_EOF; |
1927 | |
1928 | return 0; |
1929 | } |
1930 | |
1931 | static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1932 | { |
1933 | AVStream *st; |
1934 | MOVStreamContext *sc; |
1935 | unsigned i, entries; |
1936 | |
1937 | if (c->fc->nb_streams < 1) |
1938 | return 0; |
1939 | st = c->fc->streams[c->fc->nb_streams-1]; |
1940 | sc = st->priv_data; |
1941 | |
1942 | avio_rb32(pb); // version + flags |
1943 | |
1944 | entries = avio_rb32(pb); |
1945 | if (entries >= UINT_MAX / sizeof(*sc->stps_data)) |
1946 | return AVERROR_INVALIDDATA; |
1947 | sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data)); |
1948 | if (!sc->stps_data) |
1949 | return AVERROR(ENOMEM); |
1950 | |
1951 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
1952 | sc->stps_data[i] = avio_rb32(pb); |
1953 | //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]); |
1954 | } |
1955 | |
1956 | sc->stps_count = i; |
1957 | |
1958 | if (pb->eof_reached) |
1959 | return AVERROR_EOF; |
1960 | |
1961 | return 0; |
1962 | } |
1963 | |
1964 | static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1965 | { |
1966 | AVStream *st; |
1967 | MOVStreamContext *sc; |
1968 | unsigned int i, entries; |
1969 | |
1970 | if (c->fc->nb_streams < 1) |
1971 | return 0; |
1972 | st = c->fc->streams[c->fc->nb_streams-1]; |
1973 | sc = st->priv_data; |
1974 | |
1975 | avio_r8(pb); /* version */ |
1976 | avio_rb24(pb); /* flags */ |
1977 | |
1978 | entries = avio_rb32(pb); |
1979 | |
1980 | av_dlog(c->fc, "keyframe_count = %d\n", entries); |
1981 | |
1982 | if (!entries) |
1983 | { |
1984 | sc->keyframe_absent = 1; |
1985 | if (!st->need_parsing) |
1986 | st->need_parsing = AVSTREAM_PARSE_HEADERS; |
1987 | return 0; |
1988 | } |
1989 | if (entries >= UINT_MAX / sizeof(int)) |
1990 | return AVERROR_INVALIDDATA; |
1991 | sc->keyframes = av_malloc(entries * sizeof(int)); |
1992 | if (!sc->keyframes) |
1993 | return AVERROR(ENOMEM); |
1994 | |
1995 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
1996 | sc->keyframes[i] = avio_rb32(pb); |
1997 | //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]); |
1998 | } |
1999 | |
2000 | sc->keyframe_count = i; |
2001 | |
2002 | if (pb->eof_reached) |
2003 | return AVERROR_EOF; |
2004 | |
2005 | return 0; |
2006 | } |
2007 | |
2008 | static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2009 | { |
2010 | AVStream *st; |
2011 | MOVStreamContext *sc; |
2012 | unsigned int i, entries, sample_size, field_size, num_bytes; |
2013 | GetBitContext gb; |
2014 | unsigned char* buf; |
2015 | |
2016 | if (c->fc->nb_streams < 1) |
2017 | return 0; |
2018 | st = c->fc->streams[c->fc->nb_streams-1]; |
2019 | sc = st->priv_data; |
2020 | |
2021 | avio_r8(pb); /* version */ |
2022 | avio_rb24(pb); /* flags */ |
2023 | |
2024 | if (atom.type == MKTAG('s','t','s','z')) { |
2025 | sample_size = avio_rb32(pb); |
2026 | if (!sc->sample_size) /* do not overwrite value computed in stsd */ |
2027 | sc->sample_size = sample_size; |
2028 | sc->stsz_sample_size = sample_size; |
2029 | field_size = 32; |
2030 | } else { |
2031 | sample_size = 0; |
2032 | avio_rb24(pb); /* reserved */ |
2033 | field_size = avio_r8(pb); |
2034 | } |
2035 | entries = avio_rb32(pb); |
2036 | |
2037 | av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries); |
2038 | |
2039 | sc->sample_count = entries; |
2040 | if (sample_size) |
2041 | return 0; |
2042 | |
2043 | if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) { |
2044 | av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size); |
2045 | return AVERROR_INVALIDDATA; |
2046 | } |
2047 | |
2048 | if (!entries) |
2049 | return 0; |
2050 | if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size) |
2051 | return AVERROR_INVALIDDATA; |
2052 | sc->sample_sizes = av_malloc(entries * sizeof(int)); |
2053 | if (!sc->sample_sizes) |
2054 | return AVERROR(ENOMEM); |
2055 | |
2056 | num_bytes = (entries*field_size+4)>>3; |
2057 | |
2058 | buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE); |
2059 | if (!buf) { |
2060 | av_freep(&sc->sample_sizes); |
2061 | return AVERROR(ENOMEM); |
2062 | } |
2063 | |
2064 | if (avio_read(pb, buf, num_bytes) < num_bytes) { |
2065 | av_freep(&sc->sample_sizes); |
2066 | av_free(buf); |
2067 | return AVERROR_INVALIDDATA; |
2068 | } |
2069 | |
2070 | init_get_bits(&gb, buf, 8*num_bytes); |
2071 | |
2072 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
2073 | sc->sample_sizes[i] = get_bits_long(&gb, field_size); |
2074 | sc->data_size += sc->sample_sizes[i]; |
2075 | } |
2076 | |
2077 | sc->sample_count = i; |
2078 | |
2079 | if (pb->eof_reached) |
2080 | return AVERROR_EOF; |
2081 | |
2082 | av_free(buf); |
2083 | return 0; |
2084 | } |
2085 | |
2086 | static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2087 | { |
2088 | AVStream *st; |
2089 | MOVStreamContext *sc; |
2090 | unsigned int i, entries; |
2091 | int64_t duration=0; |
2092 | int64_t total_sample_count=0; |
2093 | |
2094 | if (c->fc->nb_streams < 1) |
2095 | return 0; |
2096 | st = c->fc->streams[c->fc->nb_streams-1]; |
2097 | sc = st->priv_data; |
2098 | |
2099 | avio_r8(pb); /* version */ |
2100 | avio_rb24(pb); /* flags */ |
2101 | entries = avio_rb32(pb); |
2102 | |
2103 | av_dlog(c->fc, "track[%i].stts.entries = %i\n", |
2104 | c->fc->nb_streams-1, entries); |
2105 | |
2106 | if (entries >= UINT_MAX / sizeof(*sc->stts_data)) |
2107 | return -1; |
2108 | |
2109 | sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data)); |
2110 | if (!sc->stts_data) |
2111 | return AVERROR(ENOMEM); |
2112 | |
2113 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
2114 | int sample_duration; |
2115 | int sample_count; |
2116 | |
2117 | sample_count=avio_rb32(pb); |
2118 | sample_duration = avio_rb32(pb); |
2119 | |
2120 | /* sample_duration < 0 is invalid based on the spec */ |
2121 | if (sample_duration < 0) { |
2122 | av_log(c->fc, AV_LOG_ERROR, "Invalid SampleDelta in STTS %d\n", sample_duration); |
2123 | sample_duration = 1; |
2124 | } |
2125 | if (sample_count < 0) { |
2126 | av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count); |
2127 | return AVERROR_INVALIDDATA; |
2128 | } |
2129 | sc->stts_data[i].count= sample_count; |
2130 | sc->stts_data[i].duration= sample_duration; |
2131 | |
2132 | av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n", |
2133 | sample_count, sample_duration); |
2134 | |
2135 | duration+=(int64_t)sample_duration*sample_count; |
2136 | total_sample_count+=sample_count; |
2137 | } |
2138 | |
2139 | sc->stts_count = i; |
2140 | |
2141 | if (pb->eof_reached) |
2142 | return AVERROR_EOF; |
2143 | |
2144 | st->nb_frames= total_sample_count; |
2145 | if (duration) |
2146 | st->duration= duration; |
2147 | sc->track_end = duration; |
2148 | return 0; |
2149 | } |
2150 | |
2151 | static void mov_update_dts_shift(MOVStreamContext *sc, int duration) |
2152 | { |
2153 | if (duration < 0) { |
2154 | sc->dts_shift = FFMAX(sc->dts_shift, -duration); |
2155 | } |
2156 | } |
2157 | |
2158 | static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2159 | { |
2160 | AVStream *st; |
2161 | MOVStreamContext *sc; |
2162 | unsigned int i, entries; |
2163 | |
2164 | if (c->fc->nb_streams < 1) |
2165 | return 0; |
2166 | st = c->fc->streams[c->fc->nb_streams-1]; |
2167 | sc = st->priv_data; |
2168 | |
2169 | avio_r8(pb); /* version */ |
2170 | avio_rb24(pb); /* flags */ |
2171 | entries = avio_rb32(pb); |
2172 | |
2173 | av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries); |
2174 | |
2175 | if (!entries) |
2176 | return 0; |
2177 | if (entries >= UINT_MAX / sizeof(*sc->ctts_data)) |
2178 | return AVERROR_INVALIDDATA; |
2179 | sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data)); |
2180 | if (!sc->ctts_data) |
2181 | return AVERROR(ENOMEM); |
2182 | |
2183 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
2184 | int count =avio_rb32(pb); |
2185 | int duration =avio_rb32(pb); |
2186 | |
2187 | sc->ctts_data[i].count = count; |
2188 | sc->ctts_data[i].duration= duration; |
2189 | |
2190 | av_dlog(c->fc, "count=%d, duration=%d\n", |
2191 | count, duration); |
2192 | |
2193 | if (FFABS(duration) > (1<<28) && i+2<entries) { |
2194 | av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n"); |
2195 | av_freep(&sc->ctts_data); |
2196 | sc->ctts_count = 0; |
2197 | return 0; |
2198 | } |
2199 | |
2200 | if (i+2<entries) |
2201 | mov_update_dts_shift(sc, duration); |
2202 | } |
2203 | |
2204 | sc->ctts_count = i; |
2205 | |
2206 | if (pb->eof_reached) |
2207 | return AVERROR_EOF; |
2208 | |
2209 | av_dlog(c->fc, "dts shift %d\n", sc->dts_shift); |
2210 | |
2211 | return 0; |
2212 | } |
2213 | |
2214 | static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2215 | { |
2216 | AVStream *st; |
2217 | MOVStreamContext *sc; |
2218 | unsigned int i, entries; |
2219 | uint8_t version; |
2220 | uint32_t grouping_type; |
2221 | |
2222 | if (c->fc->nb_streams < 1) |
2223 | return 0; |
2224 | st = c->fc->streams[c->fc->nb_streams-1]; |
2225 | sc = st->priv_data; |
2226 | |
2227 | version = avio_r8(pb); /* version */ |
2228 | avio_rb24(pb); /* flags */ |
2229 | grouping_type = avio_rl32(pb); |
2230 | if (grouping_type != MKTAG( 'r','a','p',' ')) |
2231 | return 0; /* only support 'rap ' grouping */ |
2232 | if (version == 1) |
2233 | avio_rb32(pb); /* grouping_type_parameter */ |
2234 | |
2235 | entries = avio_rb32(pb); |
2236 | if (!entries) |
2237 | return 0; |
2238 | if (entries >= UINT_MAX / sizeof(*sc->rap_group)) |
2239 | return AVERROR_INVALIDDATA; |
2240 | sc->rap_group = av_malloc(entries * sizeof(*sc->rap_group)); |
2241 | if (!sc->rap_group) |
2242 | return AVERROR(ENOMEM); |
2243 | |
2244 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
2245 | sc->rap_group[i].count = avio_rb32(pb); /* sample_count */ |
2246 | sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */ |
2247 | } |
2248 | |
2249 | sc->rap_group_count = i; |
2250 | |
2251 | return pb->eof_reached ? AVERROR_EOF : 0; |
2252 | } |
2253 | |
2254 | static void mov_build_index(MOVContext *mov, AVStream *st) |
2255 | { |
2256 | MOVStreamContext *sc = st->priv_data; |
2257 | int64_t current_offset; |
2258 | int64_t current_dts = 0; |
2259 | unsigned int stts_index = 0; |
2260 | unsigned int stsc_index = 0; |
2261 | unsigned int stss_index = 0; |
2262 | unsigned int stps_index = 0; |
2263 | unsigned int i, j; |
2264 | uint64_t stream_size = 0; |
2265 | |
2266 | /* adjust first dts according to edit list */ |
2267 | if ((sc->empty_duration || sc->start_time) && mov->time_scale > 0) { |
2268 | if (sc->empty_duration) |
2269 | sc->empty_duration = av_rescale(sc->empty_duration, sc->time_scale, mov->time_scale); |
2270 | sc->time_offset = sc->start_time - sc->empty_duration; |
2271 | current_dts = -sc->time_offset; |
2272 | if (sc->ctts_count>0 && sc->stts_count>0 && |
2273 | sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) { |
2274 | /* more than 16 frames delay, dts are likely wrong |
2275 | this happens with files created by iMovie */ |
2276 | sc->wrong_dts = 1; |
2277 | st->codec->has_b_frames = 1; |
2278 | } |
2279 | } |
2280 | |
2281 | /* only use old uncompressed audio chunk demuxing when stts specifies it */ |
2282 | if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && |
2283 | sc->stts_count == 1 && sc->stts_data[0].duration == 1)) { |
2284 | unsigned int current_sample = 0; |
2285 | unsigned int stts_sample = 0; |
2286 | unsigned int sample_size; |
2287 | unsigned int distance = 0; |
2288 | unsigned int rap_group_index = 0; |
2289 | unsigned int rap_group_sample = 0; |
2290 | int rap_group_present = sc->rap_group_count && sc->rap_group; |
2291 | int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0); |
2292 | |
2293 | current_dts -= sc->dts_shift; |
2294 | |
2295 | if (!sc->sample_count || st->nb_index_entries) |
2296 | return; |
2297 | if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries) |
2298 | return; |
2299 | if (av_reallocp_array(&st->index_entries, |
2300 | st->nb_index_entries + sc->sample_count, |
2301 | sizeof(*st->index_entries)) < 0) { |
2302 | st->nb_index_entries = 0; |
2303 | return; |
2304 | } |
2305 | st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries); |
2306 | |
2307 | for (i = 0; i < sc->chunk_count; i++) { |
2308 | int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX; |
2309 | current_offset = sc->chunk_offsets[i]; |
2310 | while (stsc_index + 1 < sc->stsc_count && |
2311 | i + 1 == sc->stsc_data[stsc_index + 1].first) |
2312 | stsc_index++; |
2313 | |
2314 | if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size && |
2315 | sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) { |
2316 | av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size); |
2317 | sc->stsz_sample_size = sc->sample_size; |
2318 | } |
2319 | if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) { |
2320 | av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size); |
2321 | sc->stsz_sample_size = sc->sample_size; |
2322 | } |
2323 | |
2324 | for (j = 0; j < sc->stsc_data[stsc_index].count; j++) { |
2325 | int keyframe = 0; |
2326 | if (current_sample >= sc->sample_count) { |
2327 | av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n"); |
2328 | return; |
2329 | } |
2330 | |
2331 | if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) { |
2332 | keyframe = 1; |
2333 | if (stss_index + 1 < sc->keyframe_count) |
2334 | stss_index++; |
2335 | } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) { |
2336 | keyframe = 1; |
2337 | if (stps_index + 1 < sc->stps_count) |
2338 | stps_index++; |
2339 | } |
2340 | if (rap_group_present && rap_group_index < sc->rap_group_count) { |
2341 | if (sc->rap_group[rap_group_index].index > 0) |
2342 | keyframe = 1; |
2343 | if (++rap_group_sample == sc->rap_group[rap_group_index].count) { |
2344 | rap_group_sample = 0; |
2345 | rap_group_index++; |
2346 | } |
2347 | } |
2348 | if (keyframe) |
2349 | distance = 0; |
2350 | sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample]; |
2351 | if (sc->pseudo_stream_id == -1 || |
2352 | sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) { |
2353 | AVIndexEntry *e = &st->index_entries[st->nb_index_entries++]; |
2354 | e->pos = current_offset; |
2355 | e->timestamp = current_dts; |
2356 | e->size = sample_size; |
2357 | e->min_distance = distance; |
2358 | e->flags = keyframe ? AVINDEX_KEYFRAME : 0; |
2359 | av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", " |
2360 | "size %d, distance %d, keyframe %d\n", st->index, current_sample, |
2361 | current_offset, current_dts, sample_size, distance, keyframe); |
2362 | } |
2363 | |
2364 | current_offset += sample_size; |
2365 | stream_size += sample_size; |
2366 | current_dts += sc->stts_data[stts_index].duration; |
2367 | distance++; |
2368 | stts_sample++; |
2369 | current_sample++; |
2370 | if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) { |
2371 | stts_sample = 0; |
2372 | stts_index++; |
2373 | } |
2374 | } |
2375 | } |
2376 | if (st->duration > 0) |
2377 | st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration; |
2378 | } else { |
2379 | unsigned chunk_samples, total = 0; |
2380 | |
2381 | // compute total chunk count |
2382 | for (i = 0; i < sc->stsc_count; i++) { |
2383 | unsigned count, chunk_count; |
2384 | |
2385 | chunk_samples = sc->stsc_data[i].count; |
2386 | if (i != sc->stsc_count - 1 && |
2387 | sc->samples_per_frame && chunk_samples % sc->samples_per_frame) { |
2388 | av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n"); |
2389 | return; |
2390 | } |
2391 | |
2392 | if (sc->samples_per_frame >= 160) { // gsm |
2393 | count = chunk_samples / sc->samples_per_frame; |
2394 | } else if (sc->samples_per_frame > 1) { |
2395 | unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame; |
2396 | count = (chunk_samples+samples-1) / samples; |
2397 | } else { |
2398 | count = (chunk_samples+1023) / 1024; |
2399 | } |
2400 | |
2401 | if (i < sc->stsc_count - 1) |
2402 | chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first; |
2403 | else |
2404 | chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1); |
2405 | total += chunk_count * count; |
2406 | } |
2407 | |
2408 | av_dlog(mov->fc, "chunk count %d\n", total); |
2409 | if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries) |
2410 | return; |
2411 | if (av_reallocp_array(&st->index_entries, |
2412 | st->nb_index_entries + total, |
2413 | sizeof(*st->index_entries)) < 0) { |
2414 | st->nb_index_entries = 0; |
2415 | return; |
2416 | } |
2417 | st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries); |
2418 | |
2419 | // populate index |
2420 | for (i = 0; i < sc->chunk_count; i++) { |
2421 | current_offset = sc->chunk_offsets[i]; |
2422 | if (stsc_index + 1 < sc->stsc_count && |
2423 | i + 1 == sc->stsc_data[stsc_index + 1].first) |
2424 | stsc_index++; |
2425 | chunk_samples = sc->stsc_data[stsc_index].count; |
2426 | |
2427 | while (chunk_samples > 0) { |
2428 | AVIndexEntry *e; |
2429 | unsigned size, samples; |
2430 | |
2431 | if (sc->samples_per_frame >= 160) { // gsm |
2432 | samples = sc->samples_per_frame; |
2433 | size = sc->bytes_per_frame; |
2434 | } else { |
2435 | if (sc->samples_per_frame > 1) { |
2436 | samples = FFMIN((1024 / sc->samples_per_frame)* |
2437 | sc->samples_per_frame, chunk_samples); |
2438 | size = (samples / sc->samples_per_frame) * sc->bytes_per_frame; |
2439 | } else { |
2440 | samples = FFMIN(1024, chunk_samples); |
2441 | size = samples * sc->sample_size; |
2442 | } |
2443 | } |
2444 | |
2445 | if (st->nb_index_entries >= total) { |
2446 | av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total); |
2447 | return; |
2448 | } |
2449 | e = &st->index_entries[st->nb_index_entries++]; |
2450 | e->pos = current_offset; |
2451 | e->timestamp = current_dts; |
2452 | e->size = size; |
2453 | e->min_distance = 0; |
2454 | e->flags = AVINDEX_KEYFRAME; |
2455 | av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", " |
2456 | "size %d, duration %d\n", st->index, i, current_offset, current_dts, |
2457 | size, samples); |
2458 | |
2459 | current_offset += size; |
2460 | current_dts += samples; |
2461 | chunk_samples -= samples; |
2462 | } |
2463 | } |
2464 | } |
2465 | } |
2466 | |
2467 | static int mov_open_dref(AVIOContext **pb, const char *src, MOVDref *ref, |
2468 | AVIOInterruptCB *int_cb, int use_absolute_path, AVFormatContext *fc) |
2469 | { |
2470 | /* try relative path, we do not try the absolute because it can leak information about our |
2471 | system to an attacker */ |
2472 | if (ref->nlvl_to > 0 && ref->nlvl_from > 0) { |
2473 | char filename[1024]; |
2474 | const char *src_path; |
2475 | int i, l; |
2476 | |
2477 | /* find a source dir */ |
2478 | src_path = strrchr(src, '/'); |
2479 | if (src_path) |
2480 | src_path++; |
2481 | else |
2482 | src_path = src; |
2483 | |
2484 | /* find a next level down to target */ |
2485 | for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--) |
2486 | if (ref->path[l] == '/') { |
2487 | if (i == ref->nlvl_to - 1) |
2488 | break; |
2489 | else |
2490 | i++; |
2491 | } |
2492 | |
2493 | /* compose filename if next level down to target was found */ |
2494 | if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) { |
2495 | memcpy(filename, src, src_path - src); |
2496 | filename[src_path - src] = 0; |
2497 | |
2498 | for (i = 1; i < ref->nlvl_from; i++) |
2499 | av_strlcat(filename, "../", 1024); |
2500 | |
2501 | av_strlcat(filename, ref->path + l + 1, 1024); |
2502 | |
2503 | if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL)) |
2504 | return 0; |
2505 | } |
2506 | } else if (use_absolute_path) { |
2507 | av_log(fc, AV_LOG_WARNING, "Using absolute path on user request, " |
2508 | "this is a possible security issue\n"); |
2509 | if (!avio_open2(pb, ref->path, AVIO_FLAG_READ, int_cb, NULL)) |
2510 | return 0; |
2511 | } |
2512 | |
2513 | return AVERROR(ENOENT); |
2514 | } |
2515 | |
2516 | static void fix_timescale(MOVContext *c, MOVStreamContext *sc) |
2517 | { |
2518 | if (sc->time_scale <= 0) { |
2519 | av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex); |
2520 | sc->time_scale = c->time_scale; |
2521 | if (sc->time_scale <= 0) |
2522 | sc->time_scale = 1; |
2523 | } |
2524 | } |
2525 | |
2526 | static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2527 | { |
2528 | AVStream *st; |
2529 | MOVStreamContext *sc; |
2530 | int ret; |
2531 | |
2532 | st = avformat_new_stream(c->fc, NULL); |
2533 | if (!st) return AVERROR(ENOMEM); |
2534 | st->id = c->fc->nb_streams; |
2535 | sc = av_mallocz(sizeof(MOVStreamContext)); |
2536 | if (!sc) return AVERROR(ENOMEM); |
2537 | |
2538 | st->priv_data = sc; |
2539 | st->codec->codec_type = AVMEDIA_TYPE_DATA; |
2540 | sc->ffindex = st->index; |
2541 | |
2542 | if ((ret = mov_read_default(c, pb, atom)) < 0) |
2543 | return ret; |
2544 | |
2545 | /* sanity checks */ |
2546 | if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count || |
2547 | (!sc->sample_size && !sc->sample_count))) { |
2548 | av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n", |
2549 | st->index); |
2550 | return 0; |
2551 | } |
2552 | |
2553 | fix_timescale(c, sc); |
2554 | |
2555 | avpriv_set_pts_info(st, 64, 1, sc->time_scale); |
2556 | |
2557 | mov_build_index(c, st); |
2558 | |
2559 | if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) { |
2560 | MOVDref *dref = &sc->drefs[sc->dref_id - 1]; |
2561 | if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback, |
2562 | c->use_absolute_path, c->fc) < 0) |
2563 | av_log(c->fc, AV_LOG_ERROR, |
2564 | "stream %d, error opening alias: path='%s', dir='%s', " |
2565 | "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n", |
2566 | st->index, dref->path, dref->dir, dref->filename, |
2567 | dref->volume, dref->nlvl_from, dref->nlvl_to); |
2568 | } else { |
2569 | sc->pb = c->fc->pb; |
2570 | sc->pb_is_copied = 1; |
2571 | } |
2572 | |
2573 | if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { |
2574 | if (!st->sample_aspect_ratio.num && |
2575 | (st->codec->width != sc->width || st->codec->height != sc->height)) { |
2576 | st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) / |
2577 | ((double)st->codec->width * sc->height), INT_MAX); |
2578 | } |
2579 | |
2580 | if (st->duration > 0) |
2581 | av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den, |
2582 | sc->time_scale*st->nb_frames, st->duration, INT_MAX); |
2583 | |
2584 | #if FF_API_R_FRAME_RATE |
2585 | if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1)) |
2586 | av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, |
2587 | sc->time_scale, sc->stts_data[0].duration, INT_MAX); |
2588 | #endif |
2589 | } |
2590 | |
2591 | // done for ai5q, ai52, ai55, ai1q, ai12 and ai15. |
2592 | if (!st->codec->extradata_size && st->codec->codec_id == AV_CODEC_ID_H264 && |
2593 | st->codec->codec_tag != MKTAG('a', 'v', 'c', '1')) { |
2594 | ff_generate_avci_extradata(st); |
2595 | } |
2596 | |
2597 | switch (st->codec->codec_id) { |
2598 | #if CONFIG_H261_DECODER |
2599 | case AV_CODEC_ID_H261: |
2600 | #endif |
2601 | #if CONFIG_H263_DECODER |
2602 | case AV_CODEC_ID_H263: |
2603 | #endif |
2604 | #if CONFIG_MPEG4_DECODER |
2605 | case AV_CODEC_ID_MPEG4: |
2606 | #endif |
2607 | st->codec->width = 0; /* let decoder init width/height */ |
2608 | st->codec->height= 0; |
2609 | break; |
2610 | } |
2611 | |
2612 | /* Do not need those anymore. */ |
2613 | av_freep(&sc->chunk_offsets); |
2614 | av_freep(&sc->stsc_data); |
2615 | av_freep(&sc->sample_sizes); |
2616 | av_freep(&sc->keyframes); |
2617 | av_freep(&sc->stts_data); |
2618 | av_freep(&sc->stps_data); |
2619 | av_freep(&sc->rap_group); |
2620 | |
2621 | return 0; |
2622 | } |
2623 | |
2624 | static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2625 | { |
2626 | int ret; |
2627 | c->itunes_metadata = 1; |
2628 | ret = mov_read_default(c, pb, atom); |
2629 | c->itunes_metadata = 0; |
2630 | return ret; |
2631 | } |
2632 | |
2633 | static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2634 | { |
2635 | while (atom.size > 8) { |
2636 | uint32_t tag = avio_rl32(pb); |
2637 | atom.size -= 4; |
2638 | if (tag == MKTAG('h','d','l','r')) { |
2639 | avio_seek(pb, -8, SEEK_CUR); |
2640 | atom.size += 8; |
2641 | return mov_read_default(c, pb, atom); |
2642 | } |
2643 | } |
2644 | return 0; |
2645 | } |
2646 | |
2647 | static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2648 | { |
2649 | int i; |
2650 | int width; |
2651 | int height; |
2652 | int64_t disp_transform[2]; |
2653 | int display_matrix[3][2]; |
2654 | AVStream *st; |
2655 | MOVStreamContext *sc; |
2656 | int version; |
2657 | int flags; |
2658 | |
2659 | if (c->fc->nb_streams < 1) |
2660 | return 0; |
2661 | st = c->fc->streams[c->fc->nb_streams-1]; |
2662 | sc = st->priv_data; |
2663 | |
2664 | version = avio_r8(pb); |
2665 | flags = avio_rb24(pb); |
2666 | st->disposition |= (flags & MOV_TKHD_FLAG_ENABLED) ? AV_DISPOSITION_DEFAULT : 0; |
2667 | |
2668 | if (version == 1) { |
2669 | avio_rb64(pb); |
2670 | avio_rb64(pb); |
2671 | } else { |
2672 | avio_rb32(pb); /* creation time */ |
2673 | avio_rb32(pb); /* modification time */ |
2674 | } |
2675 | st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/ |
2676 | avio_rb32(pb); /* reserved */ |
2677 | |
2678 | /* highlevel (considering edits) duration in movie timebase */ |
2679 | (version == 1) ? avio_rb64(pb) : avio_rb32(pb); |
2680 | avio_rb32(pb); /* reserved */ |
2681 | avio_rb32(pb); /* reserved */ |
2682 | |
2683 | avio_rb16(pb); /* layer */ |
2684 | avio_rb16(pb); /* alternate group */ |
2685 | avio_rb16(pb); /* volume */ |
2686 | avio_rb16(pb); /* reserved */ |
2687 | |
2688 | //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2) |
2689 | // they're kept in fixed point format through all calculations |
2690 | // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio |
2691 | for (i = 0; i < 3; i++) { |
2692 | display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point |
2693 | display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point |
2694 | avio_rb32(pb); // 2.30 fixed point (not used) |
2695 | } |
2696 | |
2697 | width = avio_rb32(pb); // 16.16 fixed point track width |
2698 | height = avio_rb32(pb); // 16.16 fixed point track height |
2699 | sc->width = width >> 16; |
2700 | sc->height = height >> 16; |
2701 | |
2702 | //Assign clockwise rotate values based on transform matrix so that |
2703 | //we can compensate for iPhone orientation during capture. |
2704 | |
2705 | if (display_matrix[0][0] == 65536 && display_matrix[1][1] == 65536) { |
2706 | av_dict_set(&st->metadata, "rotate", "0", 0); |
2707 | st->rotation_degree = 0; |
2708 | } |
2709 | |
2710 | if (display_matrix[1][0] == -65536 && display_matrix[0][1] == 65536) { |
2711 | av_dict_set(&st->metadata, "rotate", "90", 0); |
2712 | st->rotation_degree = 1; |
2713 | } |
2714 | |
2715 | if (display_matrix[0][0] == -65536 && display_matrix[1][1] == -65536) { |
2716 | av_dict_set(&st->metadata, "rotate", "180", 0); |
2717 | st->rotation_degree = 2; |
2718 | } |
2719 | |
2720 | if (display_matrix[1][0] == 65536 && display_matrix[0][1] == -65536) { |
2721 | av_dict_set(&st->metadata, "rotate", "270", 0); |
2722 | st->rotation_degree = 3; |
2723 | } |
2724 | |
2725 | // transform the display width/height according to the matrix |
2726 | // skip this if the display matrix is the default identity matrix |
2727 | // or if it is rotating the picture, ex iPhone 3GS |
2728 | // to keep the same scale, use [width height 1<<16] |
2729 | if (width && height && |
2730 | ((display_matrix[0][0] != 65536 || |
2731 | display_matrix[1][1] != 65536) && |
2732 | !display_matrix[0][1] && |
2733 | !display_matrix[1][0] && |
2734 | !display_matrix[2][0] && !display_matrix[2][1])) { |
2735 | for (i = 0; i < 2; i++) |
2736 | disp_transform[i] = |
2737 | (int64_t) width * display_matrix[0][i] + |
2738 | (int64_t) height * display_matrix[1][i] + |
2739 | ((int64_t) display_matrix[2][i] << 16); |
2740 | |
2741 | //sample aspect ratio is new width/height divided by old width/height |
2742 | st->sample_aspect_ratio = av_d2q( |
2743 | ((double) disp_transform[0] * height) / |
2744 | ((double) disp_transform[1] * width), INT_MAX); |
2745 | } |
2746 | return 0; |
2747 | } |
2748 | |
2749 | static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2750 | { |
2751 | MOVFragment *frag = &c->fragment; |
2752 | MOVTrackExt *trex = NULL; |
2753 | int flags, track_id, i; |
2754 | |
2755 | avio_r8(pb); /* version */ |
2756 | flags = avio_rb24(pb); |
2757 | |
2758 | track_id = avio_rb32(pb); |
2759 | if (!track_id) |
2760 | return AVERROR_INVALIDDATA; |
2761 | frag->track_id = track_id; |
2762 | for (i = 0; i < c->trex_count; i++) |
2763 | if (c->trex_data[i].track_id == frag->track_id) { |
2764 | trex = &c->trex_data[i]; |
2765 | break; |
2766 | } |
2767 | if (!trex) { |
2768 | av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n"); |
2769 | return AVERROR_INVALIDDATA; |
2770 | } |
2771 | |
2772 | frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ? |
2773 | avio_rb64(pb) : frag->moof_offset; |
2774 | frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id; |
2775 | |
2776 | frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ? |
2777 | avio_rb32(pb) : trex->duration; |
2778 | frag->size = flags & MOV_TFHD_DEFAULT_SIZE ? |
2779 | avio_rb32(pb) : trex->size; |
2780 | frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ? |
2781 | avio_rb32(pb) : trex->flags; |
2782 | av_dlog(c->fc, "frag flags 0x%x\n", frag->flags); |
2783 | return 0; |
2784 | } |
2785 | |
2786 | static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2787 | { |
2788 | c->chapter_track = avio_rb32(pb); |
2789 | return 0; |
2790 | } |
2791 | |
2792 | static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2793 | { |
2794 | MOVTrackExt *trex; |
2795 | int err; |
2796 | |
2797 | if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data)) |
2798 | return AVERROR_INVALIDDATA; |
2799 | if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1, |
2800 | sizeof(*c->trex_data))) < 0) { |
2801 | c->trex_count = 0; |
2802 | return err; |
2803 | } |
2804 | |
2805 | c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used. |
2806 | |
2807 | trex = &c->trex_data[c->trex_count++]; |
2808 | avio_r8(pb); /* version */ |
2809 | avio_rb24(pb); /* flags */ |
2810 | trex->track_id = avio_rb32(pb); |
2811 | trex->stsd_id = avio_rb32(pb); |
2812 | trex->duration = avio_rb32(pb); |
2813 | trex->size = avio_rb32(pb); |
2814 | trex->flags = avio_rb32(pb); |
2815 | return 0; |
2816 | } |
2817 | |
2818 | static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2819 | { |
2820 | MOVFragment *frag = &c->fragment; |
2821 | AVStream *st = NULL; |
2822 | MOVStreamContext *sc; |
2823 | MOVStts *ctts_data; |
2824 | uint64_t offset; |
2825 | int64_t dts; |
2826 | int data_offset = 0; |
2827 | unsigned entries, first_sample_flags = frag->flags; |
2828 | int flags, distance, i, found_keyframe = 0, err; |
2829 | |
2830 | for (i = 0; i < c->fc->nb_streams; i++) { |
2831 | if (c->fc->streams[i]->id == frag->track_id) { |
2832 | st = c->fc->streams[i]; |
2833 | break; |
2834 | } |
2835 | } |
2836 | if (!st) { |
2837 | av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id); |
2838 | return AVERROR_INVALIDDATA; |
2839 | } |
2840 | sc = st->priv_data; |
2841 | if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1) |
2842 | return 0; |
2843 | avio_r8(pb); /* version */ |
2844 | flags = avio_rb24(pb); |
2845 | entries = avio_rb32(pb); |
2846 | av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries); |
2847 | |
2848 | /* Always assume the presence of composition time offsets. |
2849 | * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following. |
2850 | * 1) in the initial movie, there are no samples. |
2851 | * 2) in the first movie fragment, there is only one sample without composition time offset. |
2852 | * 3) in the subsequent movie fragments, there are samples with composition time offset. */ |
2853 | if (!sc->ctts_count && sc->sample_count) |
2854 | { |
2855 | /* Complement ctts table if moov atom doesn't have ctts atom. */ |
2856 | ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data)); |
2857 | if (!ctts_data) |
2858 | return AVERROR(ENOMEM); |
2859 | sc->ctts_data = ctts_data; |
2860 | sc->ctts_data[sc->ctts_count].count = sc->sample_count; |
2861 | sc->ctts_data[sc->ctts_count].duration = 0; |
2862 | sc->ctts_count++; |
2863 | } |
2864 | if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data)) |
2865 | return AVERROR_INVALIDDATA; |
2866 | if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count, |
2867 | sizeof(*sc->ctts_data))) < 0) { |
2868 | sc->ctts_count = 0; |
2869 | return err; |
2870 | } |
2871 | if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb); |
2872 | if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb); |
2873 | dts = sc->track_end - sc->time_offset; |
2874 | offset = frag->base_data_offset + data_offset; |
2875 | distance = 0; |
2876 | av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags); |
2877 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
2878 | unsigned sample_size = frag->size; |
2879 | int sample_flags = i ? frag->flags : first_sample_flags; |
2880 | unsigned sample_duration = frag->duration; |
2881 | int keyframe = 0; |
2882 | |
2883 | if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb); |
2884 | if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb); |
2885 | if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb); |
2886 | sc->ctts_data[sc->ctts_count].count = 1; |
2887 | sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ? |
2888 | avio_rb32(pb) : 0; |
2889 | mov_update_dts_shift(sc, sc->ctts_data[sc->ctts_count].duration); |
2890 | sc->ctts_count++; |
2891 | if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) |
2892 | keyframe = 1; |
2893 | else if (!found_keyframe) |
2894 | keyframe = found_keyframe = |
2895 | !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC | |
2896 | MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES)); |
2897 | if (keyframe) |
2898 | distance = 0; |
2899 | av_add_index_entry(st, offset, dts, sample_size, distance, |
2900 | keyframe ? AVINDEX_KEYFRAME : 0); |
2901 | av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", " |
2902 | "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i, |
2903 | offset, dts, sample_size, distance, keyframe); |
2904 | distance++; |
2905 | dts += sample_duration; |
2906 | offset += sample_size; |
2907 | sc->data_size += sample_size; |
2908 | } |
2909 | |
2910 | if (pb->eof_reached) |
2911 | return AVERROR_EOF; |
2912 | |
2913 | frag->moof_offset = offset; |
2914 | st->duration = sc->track_end = dts + sc->time_offset; |
2915 | return 0; |
2916 | } |
2917 | |
2918 | /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */ |
2919 | /* like the files created with Adobe Premiere 5.0, for samples see */ |
2920 | /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */ |
2921 | static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2922 | { |
2923 | int err; |
2924 | |
2925 | if (atom.size < 8) |
2926 | return 0; /* continue */ |
2927 | if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */ |
2928 | avio_skip(pb, atom.size - 4); |
2929 | return 0; |
2930 | } |
2931 | atom.type = avio_rl32(pb); |
2932 | atom.size -= 8; |
2933 | if (atom.type != MKTAG('m','d','a','t')) { |
2934 | avio_skip(pb, atom.size); |
2935 | return 0; |
2936 | } |
2937 | err = mov_read_mdat(c, pb, atom); |
2938 | return err; |
2939 | } |
2940 | |
2941 | static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2942 | { |
2943 | #if CONFIG_ZLIB |
2944 | AVIOContext ctx; |
2945 | uint8_t *cmov_data; |
2946 | uint8_t *moov_data; /* uncompressed data */ |
2947 | long cmov_len, moov_len; |
2948 | int ret = -1; |
2949 | |
2950 | avio_rb32(pb); /* dcom atom */ |
2951 | if (avio_rl32(pb) != MKTAG('d','c','o','m')) |
2952 | return AVERROR_INVALIDDATA; |
2953 | if (avio_rl32(pb) != MKTAG('z','l','i','b')) { |
2954 | av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n"); |
2955 | return AVERROR_INVALIDDATA; |
2956 | } |
2957 | avio_rb32(pb); /* cmvd atom */ |
2958 | if (avio_rl32(pb) != MKTAG('c','m','v','d')) |
2959 | return AVERROR_INVALIDDATA; |
2960 | moov_len = avio_rb32(pb); /* uncompressed size */ |
2961 | cmov_len = atom.size - 6 * 4; |
2962 | |
2963 | cmov_data = av_malloc(cmov_len); |
2964 | if (!cmov_data) |
2965 | return AVERROR(ENOMEM); |
2966 | moov_data = av_malloc(moov_len); |
2967 | if (!moov_data) { |
2968 | av_free(cmov_data); |
2969 | return AVERROR(ENOMEM); |
2970 | } |
2971 | avio_read(pb, cmov_data, cmov_len); |
2972 | if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK) |
2973 | goto free_and_return; |
2974 | if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0) |
2975 | goto free_and_return; |
2976 | atom.type = MKTAG('m','o','o','v'); |
2977 | atom.size = moov_len; |
2978 | ret = mov_read_default(c, &ctx, atom); |
2979 | free_and_return: |
2980 | av_free(moov_data); |
2981 | av_free(cmov_data); |
2982 | return ret; |
2983 | #else |
2984 | av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n"); |
2985 | return AVERROR(ENOSYS); |
2986 | #endif |
2987 | } |
2988 | |
2989 | /* edit list atom */ |
2990 | static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2991 | { |
2992 | MOVStreamContext *sc; |
2993 | int i, edit_count, version, edit_start_index = 0; |
2994 | int unsupported = 0; |
2995 | |
2996 | if (c->fc->nb_streams < 1 || c->ignore_editlist) |
2997 | return 0; |
2998 | sc = c->fc->streams[c->fc->nb_streams-1]->priv_data; |
2999 | |
3000 | version = avio_r8(pb); /* version */ |
3001 | avio_rb24(pb); /* flags */ |
3002 | edit_count = avio_rb32(pb); /* entries */ |
3003 | |
3004 | if ((uint64_t)edit_count*12+8 > atom.size) |
3005 | return AVERROR_INVALIDDATA; |
3006 | |
3007 | av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count); |
3008 | for (i=0; i<edit_count; i++){ |
3009 | int64_t time; |
3010 | int64_t duration; |
3011 | int rate; |
3012 | if (version == 1) { |
3013 | duration = avio_rb64(pb); |
3014 | time = avio_rb64(pb); |
3015 | } else { |
3016 | duration = avio_rb32(pb); /* segment duration */ |
3017 | time = (int32_t)avio_rb32(pb); /* media time */ |
3018 | } |
3019 | rate = avio_rb32(pb); |
3020 | if (i == 0 && time == -1) { |
3021 | sc->empty_duration = duration; |
3022 | edit_start_index = 1; |
3023 | } else if (i == edit_start_index && time >= 0) |
3024 | sc->start_time = time; |
3025 | else |
3026 | unsupported = 1; |
3027 | |
3028 | av_dlog(c->fc, "duration=%"PRId64" time=%"PRId64" rate=%f\n", |
3029 | duration, time, rate / 65536.0); |
3030 | } |
3031 | |
3032 | if (unsupported) |
3033 | av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, " |
3034 | "a/v desync might occur, patch welcome\n"); |
3035 | |
3036 | return 0; |
3037 | } |
3038 | |
3039 | static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
3040 | { |
3041 | MOVStreamContext *sc; |
3042 | |
3043 | if (c->fc->nb_streams < 1) |
3044 | return AVERROR_INVALIDDATA; |
3045 | sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data; |
3046 | sc->timecode_track = avio_rb32(pb); |
3047 | return 0; |
3048 | } |
3049 | |
3050 | static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
3051 | { |
3052 | int ret; |
3053 | uint8_t uuid[16]; |
3054 | static const uint8_t uuid_isml_manifest[] = { |
3055 | 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd, |
3056 | 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 |
3057 | }; |
3058 | |
3059 | if (atom.size < sizeof(uuid) || atom.size == INT64_MAX) |
3060 | return AVERROR_INVALIDDATA; |
3061 | |
3062 | ret = avio_read(pb, uuid, sizeof(uuid)); |
3063 | if (ret < 0) { |
3064 | return ret; |
3065 | } else if (ret != sizeof(uuid)) { |
3066 | return AVERROR_INVALIDDATA; |
3067 | } |
3068 | if (!memcmp(uuid, uuid_isml_manifest, sizeof(uuid))) { |
3069 | uint8_t *buffer, *ptr; |
3070 | char *endptr; |
3071 | size_t len = atom.size - sizeof(uuid); |
3072 | |
3073 | if (len < 4) { |
3074 | return AVERROR_INVALIDDATA; |
3075 | } |
3076 | ret = avio_skip(pb, 4); // zeroes |
3077 | len -= 4; |
3078 | |
3079 | buffer = av_mallocz(len + 1); |
3080 | if (!buffer) { |
3081 | return AVERROR(ENOMEM); |
3082 | } |
3083 | ret = avio_read(pb, buffer, len); |
3084 | if (ret < 0) { |
3085 | av_free(buffer); |
3086 | return ret; |
3087 | } else if (ret != len) { |
3088 | av_free(buffer); |
3089 | return AVERROR_INVALIDDATA; |
3090 | } |
3091 | |
3092 | ptr = buffer; |
3093 | while ((ptr = av_stristr(ptr, "systemBitrate=\"")) != NULL) { |
3094 | ptr += sizeof("systemBitrate=\"") - 1; |
3095 | c->bitrates_count++; |
3096 | c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates)); |
3097 | if (!c->bitrates) { |
3098 | c->bitrates_count = 0; |
3099 | av_free(buffer); |
3100 | return AVERROR(ENOMEM); |
3101 | } |
3102 | errno = 0; |
3103 | ret = strtol(ptr, &endptr, 10); |
3104 | if (ret < 0 || errno || *endptr != '"') { |
3105 | c->bitrates[c->bitrates_count - 1] = 0; |
3106 | } else { |
3107 | c->bitrates[c->bitrates_count - 1] = ret; |
3108 | } |
3109 | } |
3110 | |
3111 | av_free(buffer); |
3112 | } |
3113 | return 0; |
3114 | } |
3115 | |
3116 | static void mov_id32_date2year(AVDictionary **m) |
3117 | { |
3118 | AVDictionaryEntry *t; |
3119 | if (t = av_dict_get(*m, "date", t, AV_DICT_MATCH_CASE)) { |
3120 | av_dict_set(m, "year", t->value, 0); |
3121 | av_log(NULL, AV_LOG_INFO, "[%s:%d]========date:%s\n", __FUNCTION__, __LINE__, t->value); |
3122 | } |
3123 | } |
3124 | |
3125 | static int mov_read_id32(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
3126 | { |
3127 | uint8_t version; |
3128 | uint32_t flags; |
3129 | uint8_t pad; |
3130 | uint16_t langcode; |
3131 | uint16_t shortbytes; |
3132 | char language[4] = {0}; |
3133 | uint32_t str_size; |
3134 | AVFormatContext *s = c->fc; |
3135 | ID3v2ExtraMeta *id3v2_extra_meta = NULL; |
3136 | |
3137 | str_size = atom.size; |
3138 | version = avio_r8(pb); // version |
3139 | flags = avio_rb24(pb); //flags |
3140 | shortbytes = avio_rb16(pb); |
3141 | pad = (shortbytes & 0x8000) >> 15; //pad |
3142 | langcode = shortbytes & 0x7ffff; //language |
3143 | ff_mov_lang_to_iso639(langcode, language); |
3144 | str_size -= 6; |
3145 | ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta); |
3146 | if (id3v2_extra_meta) { |
3147 | int err = ff_id3v2_parse_apic(s, &id3v2_extra_meta); |
3148 | if (err < 0) { |
3149 | av_log(NULL, AV_LOG_INFO, "[%s:%d]ff_id3v2_parse_apic err:%d\n", __FUNCTION__, __LINE__, err); |
3150 | return err; |
3151 | } |
3152 | } |
3153 | ff_id3v2_free_extra_meta(&id3v2_extra_meta); |
3154 | mov_id32_date2year(&s->metadata); |
3155 | |
3156 | return 0; |
3157 | } |
3158 | |
3159 | static const MOVParseTableEntry mov_default_parse_table[] = { |
3160 | { MKTAG('A','C','L','R'), mov_read_avid }, |
3161 | { MKTAG('A','P','R','G'), mov_read_avid }, |
3162 | { MKTAG('A','A','L','P'), mov_read_avid }, |
3163 | { MKTAG('A','R','E','S'), mov_read_ares }, |
3164 | { MKTAG('a','v','s','s'), mov_read_avss }, |
3165 | { MKTAG('c','h','p','l'), mov_read_chpl }, |
3166 | { MKTAG('c','o','6','4'), mov_read_stco }, |
3167 | { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */ |
3168 | { MKTAG('d','i','n','f'), mov_read_default }, |
3169 | { MKTAG('d','r','e','f'), mov_read_dref }, |
3170 | { MKTAG('e','d','t','s'), mov_read_default }, |
3171 | { MKTAG('e','l','s','t'), mov_read_elst }, |
3172 | { MKTAG('e','n','d','a'), mov_read_enda }, |
3173 | { MKTAG('f','i','e','l'), mov_read_fiel }, |
3174 | { MKTAG('f','t','y','p'), mov_read_ftyp }, |
3175 | { MKTAG('g','l','b','l'), mov_read_glbl }, |
3176 | { MKTAG('h','d','l','r'), mov_read_hdlr }, |
3177 | { MKTAG('i','l','s','t'), mov_read_ilst }, |
3178 | { MKTAG('j','p','2','h'), mov_read_jp2h }, |
3179 | { MKTAG('m','d','a','t'), mov_read_mdat }, |
3180 | { MKTAG('m','d','h','d'), mov_read_mdhd }, |
3181 | { MKTAG('m','d','i','a'), mov_read_default }, |
3182 | { MKTAG('m','e','t','a'), mov_read_meta }, |
3183 | { MKTAG('m','i','n','f'), mov_read_default }, |
3184 | { MKTAG('m','o','o','f'), mov_read_moof }, |
3185 | { MKTAG('m','o','o','v'), mov_read_moov }, |
3186 | { MKTAG('m','v','e','x'), mov_read_default }, |
3187 | { MKTAG('m','v','h','d'), mov_read_mvhd }, |
3188 | { MKTAG('S','M','I',' '), mov_read_svq3 }, |
3189 | { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */ |
3190 | { MKTAG('a','v','c','C'), mov_read_glbl }, |
3191 | { MKTAG('p','a','s','p'), mov_read_pasp }, |
3192 | { MKTAG('s','t','b','l'), mov_read_default }, |
3193 | { MKTAG('s','t','c','o'), mov_read_stco }, |
3194 | { MKTAG('s','t','p','s'), mov_read_stps }, |
3195 | { MKTAG('s','t','r','f'), mov_read_strf }, |
3196 | { MKTAG('s','t','s','c'), mov_read_stsc }, |
3197 | { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */ |
3198 | { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */ |
3199 | { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */ |
3200 | { MKTAG('s','t','t','s'), mov_read_stts }, |
3201 | { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */ |
3202 | { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */ |
3203 | { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */ |
3204 | { MKTAG('t','r','a','k'), mov_read_trak }, |
3205 | { MKTAG('t','r','a','f'), mov_read_default }, |
3206 | { MKTAG('t','r','e','f'), mov_read_default }, |
3207 | { MKTAG('t','m','c','d'), mov_read_tmcd }, |
3208 | { MKTAG('c','h','a','p'), mov_read_chap }, |
3209 | { MKTAG('t','r','e','x'), mov_read_trex }, |
3210 | { MKTAG('t','r','u','n'), mov_read_trun }, |
3211 | { MKTAG('u','d','t','a'), mov_read_default }, |
3212 | { MKTAG('w','a','v','e'), mov_read_wave }, |
3213 | { MKTAG('e','s','d','s'), mov_read_esds }, |
3214 | { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */ |
3215 | { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */ |
3216 | { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */ |
3217 | { MKTAG('w','f','e','x'), mov_read_wfex }, |
3218 | { MKTAG('c','m','o','v'), mov_read_cmov }, |
3219 | { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */ |
3220 | { MKTAG('d','v','c','1'), mov_read_dvc1 }, |
3221 | { MKTAG('s','b','g','p'), mov_read_sbgp }, |
3222 | { MKTAG('h','v','c','C'), mov_read_glbl }, |
3223 | { MKTAG('u','u','i','d'), mov_read_uuid }, |
3224 | { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 }, |
3225 | { MKTAG('I','D','3','2'), mov_read_id32 }, |
3226 | |
3227 | { 0, NULL } |
3228 | }; |
3229 | |
3230 | static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
3231 | { |
3232 | int64_t total_size = 0; |
3233 | MOVAtom a; |
3234 | int i; |
3235 | |
3236 | if (atom.size < 0) |
3237 | atom.size = INT64_MAX; |
3238 | while (total_size + 8 <= atom.size && !url_feof(pb)) { |
3239 | int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL; |
3240 | a.size = atom.size; |
3241 | a.type=0; |
3242 | if (atom.size >= 8) { |
3243 | a.size = avio_rb32(pb); |
3244 | a.type = avio_rl32(pb); |
3245 | if (atom.type != MKTAG('r','o','o','t') && |
3246 | atom.type != MKTAG('m','o','o','v')) |
3247 | { |
3248 | if (a.type == MKTAG('t','r','a','k') || a.type == MKTAG('m','d','a','t')) |
3249 | { |
3250 | av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n"); |
3251 | avio_skip(pb, -8); |
3252 | return 0; |
3253 | } |
3254 | } |
3255 | total_size += 8; |
3256 | if (a.size == 1) { /* 64 bit extended size */ |
3257 | a.size = avio_rb64(pb) - 8; |
3258 | total_size += 8; |
3259 | } |
3260 | } |
3261 | av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n", |
3262 | a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size); |
3263 | if (a.size == 0) { |
3264 | a.size = atom.size - total_size + 8; |
3265 | } |
3266 | a.size -= 8; |
3267 | if (a.size < 0) |
3268 | break; |
3269 | a.size = FFMIN(a.size, atom.size - total_size); |
3270 | |
3271 | for (i = 0; mov_default_parse_table[i].type; i++) |
3272 | if (mov_default_parse_table[i].type == a.type) { |
3273 | parse = mov_default_parse_table[i].parse; |
3274 | break; |
3275 | } |
3276 | |
3277 | // container is user data |
3278 | if (!parse && (atom.type == MKTAG('u','d','t','a') || |
3279 | atom.type == MKTAG('i','l','s','t'))) |
3280 | parse = mov_read_udta_string; |
3281 | |
3282 | if (!parse) { /* skip leaf atoms data */ |
3283 | avio_skip(pb, a.size); |
3284 | } else { |
3285 | int64_t start_pos = avio_tell(pb); |
3286 | int64_t left; |
3287 | int err = parse(c, pb, a); |
3288 | if (err < 0) |
3289 | return err; |
3290 | if (c->found_moov && c->found_mdat && |
3291 | ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) || |
3292 | start_pos + a.size == avio_size(pb))) { |
3293 | if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) |
3294 | c->next_root_atom = start_pos + a.size; |
3295 | return 0; |
3296 | } |
3297 | left = a.size - avio_tell(pb) + start_pos; |
3298 | if (left > 0) /* skip garbage at atom end */ |
3299 | avio_skip(pb, left); |
3300 | else if (left < 0) { |
3301 | av_log(c->fc, AV_LOG_WARNING, |
3302 | "overread end of atom '%.4s' by %"PRId64" bytes\n", |
3303 | (char*)&a.type, -left); |
3304 | avio_seek(pb, left, SEEK_CUR); |
3305 | } |
3306 | } |
3307 | |
3308 | total_size += a.size; |
3309 | } |
3310 | |
3311 | if (total_size < atom.size && atom.size < 0x7ffff) |
3312 | avio_skip(pb, atom.size - total_size); |
3313 | |
3314 | return 0; |
3315 | } |
3316 | |
3317 | static int mov_probe(AVProbeData *p) |
3318 | { |
3319 | int64_t offset; |
3320 | uint32_t tag; |
3321 | int score = 0; |
3322 | int moov_offset = -1; |
3323 | |
3324 | /* check file header */ |
3325 | offset = 0; |
3326 | for (;;) { |
3327 | /* ignore invalid offset */ |
3328 | if ((offset + 8) > p->buf_size) |
3329 | break; |
3330 | tag = AV_RL32(p->buf + offset + 4); |
3331 | switch(tag) { |
3332 | /* check for obvious tags */ |
3333 | case MKTAG('m','o','o','v'): |
3334 | moov_offset = offset + 4; |
3335 | case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */ |
3336 | case MKTAG('m','d','a','t'): |
3337 | case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */ |
3338 | case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */ |
3339 | case MKTAG('f','t','y','p'): |
3340 | if (AV_RB32(p->buf+offset) < 8 && |
3341 | (AV_RB32(p->buf+offset) != 1 || |
3342 | offset + 12 > (unsigned int)p->buf_size || |
3343 | AV_RB64(p->buf+offset + 8) == 0)) { |
3344 | score = FFMAX(score, AVPROBE_SCORE_EXTENSION); |
3345 | } else { |
3346 | score = AVPROBE_SCORE_MAX; |
3347 | } |
3348 | offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset; |
3349 | break; |
3350 | /* those are more common words, so rate then a bit less */ |
3351 | case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */ |
3352 | case MKTAG('w','i','d','e'): |
3353 | case MKTAG('f','r','e','e'): |
3354 | case MKTAG('j','u','n','k'): |
3355 | case MKTAG('p','i','c','t'): |
3356 | score = FFMAX(score, AVPROBE_SCORE_MAX - 5); |
3357 | offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset; |
3358 | break; |
3359 | case MKTAG(0x82,0x82,0x7f,0x7d): |
3360 | case MKTAG('s','k','i','p'): |
3361 | case MKTAG('u','u','i','d'): |
3362 | case MKTAG('p','r','f','l'): |
3363 | /* if we only find those cause probedata is too small at least rate them */ |
3364 | score = FFMAX(score, AVPROBE_SCORE_EXTENSION); |
3365 | offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset; |
3366 | break; |
3367 | default: |
3368 | offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset; |
3369 | } |
3370 | } |
3371 | if(score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) { |
3372 | /* moov atom in the header - we should make sure that this is not a |
3373 | * MOV-packed MPEG-PS */ |
3374 | offset = moov_offset; |
3375 | |
3376 | while(offset < (p->buf_size - 16)){ /* Sufficient space */ |
3377 | /* We found an actual hdlr atom */ |
3378 | if(AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') && |
3379 | AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') && |
3380 | AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')){ |
3381 | av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n"); |
3382 | /* We found a media handler reference atom describing an |
3383 | * MPEG-PS-in-MOV, return a |
3384 | * low score to force expanding the probe window until |
3385 | * mpegps_probe finds what it needs */ |
3386 | return 5; |
3387 | }else |
3388 | /* Keep looking */ |
3389 | offset+=2; |
3390 | } |
3391 | } |
3392 | |
3393 | return score; |
3394 | } |
3395 | |
3396 | // must be done after parsing all trak because there's no order requirement |
3397 | static void mov_read_chapters(AVFormatContext *s) |
3398 | { |
3399 | MOVContext *mov = s->priv_data; |
3400 | AVStream *st = NULL; |
3401 | MOVStreamContext *sc; |
3402 | int64_t cur_pos; |
3403 | int i; |
3404 | |
3405 | for (i = 0; i < s->nb_streams; i++) |
3406 | if (s->streams[i]->id == mov->chapter_track) { |
3407 | st = s->streams[i]; |
3408 | break; |
3409 | } |
3410 | if (!st) { |
3411 | av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n"); |
3412 | return; |
3413 | } |
3414 | |
3415 | st->discard = AVDISCARD_ALL; |
3416 | sc = st->priv_data; |
3417 | cur_pos = avio_tell(sc->pb); |
3418 | |
3419 | for (i = 0; i < st->nb_index_entries; i++) { |
3420 | AVIndexEntry *sample = &st->index_entries[i]; |
3421 | int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration; |
3422 | uint8_t *title; |
3423 | uint16_t ch; |
3424 | int len, title_len; |
3425 | |
3426 | if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) { |
3427 | av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i); |
3428 | goto finish; |
3429 | } |
3430 | |
3431 | // the first two bytes are the length of the title |
3432 | len = avio_rb16(sc->pb); |
3433 | if (len > sample->size-2) |
3434 | continue; |
3435 | title_len = 2*len + 1; |
3436 | if (!(title = av_mallocz(title_len))) |
3437 | goto finish; |
3438 | |
3439 | // The samples could theoretically be in any encoding if there's an encd |
3440 | // atom following, but in practice are only utf-8 or utf-16, distinguished |
3441 | // instead by the presence of a BOM |
3442 | if (!len) { |
3443 | title[0] = 0; |
3444 | } else { |
3445 | ch = avio_rb16(sc->pb); |
3446 | if (ch == 0xfeff) |
3447 | avio_get_str16be(sc->pb, len, title, title_len); |
3448 | else if (ch == 0xfffe) |
3449 | avio_get_str16le(sc->pb, len, title, title_len); |
3450 | else { |
3451 | AV_WB16(title, ch); |
3452 | if (len == 1 || len == 2) |
3453 | title[len] = 0; |
3454 | else |
3455 | avio_get_str(sc->pb, INT_MAX, title + 2, len - 1); |
3456 | } |
3457 | } |
3458 | |
3459 | avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title); |
3460 | av_freep(&title); |
3461 | } |
3462 | finish: |
3463 | avio_seek(sc->pb, cur_pos, SEEK_SET); |
3464 | } |
3465 | |
3466 | static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st, |
3467 | uint32_t value, int flags) |
3468 | { |
3469 | AVTimecode tc; |
3470 | char buf[AV_TIMECODE_STR_SIZE]; |
3471 | AVRational rate = {st->codec->time_base.den, |
3472 | st->codec->time_base.num}; |
3473 | int ret = av_timecode_init(&tc, rate, flags, 0, s); |
3474 | if (ret < 0) |
3475 | return ret; |
3476 | av_dict_set(&st->metadata, "timecode", |
3477 | av_timecode_make_string(&tc, buf, value), 0); |
3478 | return 0; |
3479 | } |
3480 | |
3481 | static int mov_read_timecode_track(AVFormatContext *s, AVStream *st) |
3482 | { |
3483 | MOVStreamContext *sc = st->priv_data; |
3484 | int flags = 0; |
3485 | int64_t cur_pos = avio_tell(sc->pb); |
3486 | uint32_t value; |
3487 | |
3488 | if (!st->nb_index_entries) |
3489 | return -1; |
3490 | |
3491 | avio_seek(sc->pb, st->index_entries->pos, SEEK_SET); |
3492 | value = avio_rb32(s->pb); |
3493 | |
3494 | if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME; |
3495 | if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX; |
3496 | if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE; |
3497 | |
3498 | /* Assume Counter flag is set to 1 in tmcd track (even though it is likely |
3499 | * not the case) and thus assume "frame number format" instead of QT one. |
3500 | * No sample with tmcd track can be found with a QT timecode at the moment, |
3501 | * despite what the tmcd track "suggests" (Counter flag set to 0 means QT |
3502 | * format). */ |
3503 | parse_timecode_in_framenum_format(s, st, value, flags); |
3504 | |
3505 | avio_seek(sc->pb, cur_pos, SEEK_SET); |
3506 | return 0; |
3507 | } |
3508 | |
3509 | static int mov_read_close(AVFormatContext *s) |
3510 | { |
3511 | MOVContext *mov = s->priv_data; |
3512 | int i, j; |
3513 | |
3514 | for (i = 0; i < s->nb_streams; i++) { |
3515 | AVStream *st = s->streams[i]; |
3516 | MOVStreamContext *sc = st->priv_data; |
3517 | |
3518 | av_freep(&sc->ctts_data); |
3519 | for (j = 0; j < sc->drefs_count; j++) { |
3520 | av_freep(&sc->drefs[j].path); |
3521 | av_freep(&sc->drefs[j].dir); |
3522 | } |
3523 | av_freep(&sc->drefs); |
3524 | if (!sc->pb_is_copied) |
3525 | avio_close(sc->pb); |
3526 | sc->pb = NULL; |
3527 | av_freep(&sc->chunk_offsets); |
3528 | av_freep(&sc->keyframes); |
3529 | av_freep(&sc->sample_sizes); |
3530 | av_freep(&sc->stps_data); |
3531 | av_freep(&sc->stsc_data); |
3532 | av_freep(&sc->stts_data); |
3533 | } |
3534 | |
3535 | if (mov->dv_demux) { |
3536 | for (i = 0; i < mov->dv_fctx->nb_streams; i++) { |
3537 | av_freep(&mov->dv_fctx->streams[i]->codec); |
3538 | av_freep(&mov->dv_fctx->streams[i]); |
3539 | } |
3540 | av_freep(&mov->dv_fctx); |
3541 | av_freep(&mov->dv_demux); |
3542 | } |
3543 | |
3544 | av_freep(&mov->trex_data); |
3545 | av_freep(&mov->bitrates); |
3546 | |
3547 | return 0; |
3548 | } |
3549 | |
3550 | static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id) |
3551 | { |
3552 | int i; |
3553 | |
3554 | for (i = 0; i < s->nb_streams; i++) { |
3555 | AVStream *st = s->streams[i]; |
3556 | MOVStreamContext *sc = st->priv_data; |
3557 | |
3558 | if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && |
3559 | sc->timecode_track == tmcd_id) |
3560 | return 1; |
3561 | } |
3562 | return 0; |
3563 | } |
3564 | |
3565 | /* look for a tmcd track not referenced by any video track, and export it globally */ |
3566 | static void export_orphan_timecode(AVFormatContext *s) |
3567 | { |
3568 | int i; |
3569 | |
3570 | for (i = 0; i < s->nb_streams; i++) { |
3571 | AVStream *st = s->streams[i]; |
3572 | |
3573 | if (st->codec->codec_tag == MKTAG('t','m','c','d') && |
3574 | !tmcd_is_referenced(s, i + 1)) { |
3575 | AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0); |
3576 | if (tcr) { |
3577 | av_dict_set(&s->metadata, "timecode", tcr->value, 0); |
3578 | break; |
3579 | } |
3580 | } |
3581 | } |
3582 | } |
3583 | |
3584 | static int mov_read_header(AVFormatContext *s) |
3585 | { |
3586 | MOVContext *mov = s->priv_data; |
3587 | AVIOContext *pb = s->pb; |
3588 | int i, j, err; |
3589 | MOVAtom atom = { AV_RL32("root") }; |
3590 | |
3591 | mov->fc = s; |
3592 | /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */ |
3593 | if (pb->seekable) |
3594 | atom.size = avio_size(pb); |
3595 | else |
3596 | atom.size = INT64_MAX; |
3597 | |
3598 | /* check MOV header */ |
3599 | if ((err = mov_read_default(mov, pb, atom)) < 0) { |
3600 | av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err); |
3601 | mov_read_close(s); |
3602 | return err; |
3603 | } |
3604 | if (!mov->found_moov) { |
3605 | av_log(s, AV_LOG_ERROR, "moov atom not found\n"); |
3606 | mov_read_close(s); |
3607 | return AVERROR_INVALIDDATA; |
3608 | } |
3609 | av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb)); |
3610 | |
3611 | if (pb->seekable) { |
3612 | if (mov->chapter_track > 0) |
3613 | mov_read_chapters(s); |
3614 | for (i = 0; i < s->nb_streams; i++) |
3615 | if (s->streams[i]->codec->codec_tag == AV_RL32("tmcd")) |
3616 | mov_read_timecode_track(s, s->streams[i]); |
3617 | } |
3618 | |
3619 | /* copy timecode metadata from tmcd tracks to the related video streams */ |
3620 | for (i = 0; i < s->nb_streams; i++) { |
3621 | AVStream *st = s->streams[i]; |
3622 | MOVStreamContext *sc = st->priv_data; |
3623 | if (sc->timecode_track > 0) { |
3624 | AVDictionaryEntry *tcr; |
3625 | int tmcd_st_id = -1; |
3626 | |
3627 | for (j = 0; j < s->nb_streams; j++) |
3628 | if (s->streams[j]->id == sc->timecode_track) |
3629 | tmcd_st_id = j; |
3630 | |
3631 | if (tmcd_st_id < 0 || tmcd_st_id == i) |
3632 | continue; |
3633 | tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0); |
3634 | if (tcr) |
3635 | av_dict_set(&st->metadata, "timecode", tcr->value, 0); |
3636 | } |
3637 | } |
3638 | export_orphan_timecode(s); |
3639 | |
3640 | for (i = 0; i < s->nb_streams; i++) { |
3641 | AVStream *st = s->streams[i]; |
3642 | MOVStreamContext *sc = st->priv_data; |
3643 | fix_timescale(mov, sc); |
3644 | if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->codec->codec_id == AV_CODEC_ID_AAC) { |
3645 | st->skip_samples = sc->start_pad; |
3646 | } |
3647 | } |
3648 | |
3649 | if (mov->trex_data) { |
3650 | for (i = 0; i < s->nb_streams; i++) { |
3651 | AVStream *st = s->streams[i]; |
3652 | MOVStreamContext *sc = st->priv_data; |
3653 | if (st->duration > 0) |
3654 | st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration; |
3655 | } |
3656 | } |
3657 | |
3658 | for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) { |
3659 | if (mov->bitrates[i]) { |
3660 | s->streams[i]->codec->bit_rate = mov->bitrates[i]; |
3661 | } |
3662 | } |
3663 | |
3664 | return 0; |
3665 | } |
3666 | |
3667 | static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st) |
3668 | { |
3669 | AVIndexEntry *sample = NULL; |
3670 | int64_t best_dts = INT64_MAX; |
3671 | int i; |
3672 | for (i = 0; i < s->nb_streams; i++) { |
3673 | AVStream *avst = s->streams[i]; |
3674 | MOVStreamContext *msc = avst->priv_data; |
3675 | if (msc->pb && msc->current_sample < avst->nb_index_entries) { |
3676 | AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample]; |
3677 | int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale); |
3678 | av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts); |
3679 | if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) || |
3680 | (s->pb->seekable && |
3681 | ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb && |
3682 | ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) || |
3683 | (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) { |
3684 | sample = current_sample; |
3685 | best_dts = dts; |
3686 | *st = avst; |
3687 | } |
3688 | } |
3689 | } |
3690 | return sample; |
3691 | } |
3692 | |
3693 | static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) |
3694 | { |
3695 | MOVContext *mov = s->priv_data; |
3696 | MOVStreamContext *sc; |
3697 | AVIndexEntry *sample; |
3698 | AVStream *st = NULL; |
3699 | int ret; |
3700 | mov->fc = s; |
3701 | retry: |
3702 | sample = mov_find_next_sample(s, &st); |
3703 | if (!sample) { |
3704 | mov->found_mdat = 0; |
3705 | if (!mov->next_root_atom) |
3706 | return AVERROR_EOF; |
3707 | avio_seek(s->pb, mov->next_root_atom, SEEK_SET); |
3708 | mov->next_root_atom = 0; |
3709 | if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 || |
3710 | url_feof(s->pb)) |
3711 | return AVERROR_EOF; |
3712 | av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb)); |
3713 | goto retry; |
3714 | } |
3715 | sc = st->priv_data; |
3716 | /* must be done just before reading, to avoid infinite loop on sample */ |
3717 | sc->current_sample++; |
3718 | |
3719 | if (mov->next_root_atom) { |
3720 | sample->pos = FFMIN(sample->pos, mov->next_root_atom); |
3721 | sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos)); |
3722 | } |
3723 | |
3724 | if (st->discard != AVDISCARD_ALL) { |
3725 | if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) { |
3726 | av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", |
3727 | sc->ffindex, sample->pos); |
3728 | return AVERROR_INVALIDDATA; |
3729 | } |
3730 | ret = av_get_packet(sc->pb, pkt, sample->size); |
3731 | if (ret < 0) |
3732 | return ret; |
3733 | if (sc->has_palette) { |
3734 | uint8_t *pal; |
3735 | |
3736 | pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE); |
3737 | if (!pal) { |
3738 | av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n"); |
3739 | } else { |
3740 | memcpy(pal, sc->palette, AVPALETTE_SIZE); |
3741 | sc->has_palette = 0; |
3742 | } |
3743 | } |
3744 | #if CONFIG_DV_DEMUXER |
3745 | if (mov->dv_demux && sc->dv_audio_container) { |
3746 | avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos); |
3747 | av_free(pkt->data); |
3748 | pkt->size = 0; |
3749 | ret = avpriv_dv_get_packet(mov->dv_demux, pkt); |
3750 | if (ret < 0) |
3751 | return ret; |
3752 | } |
3753 | #endif |
3754 | } |
3755 | |
3756 | pkt->stream_index = sc->ffindex; |
3757 | pkt->dts = sample->timestamp; |
3758 | if (sc->ctts_data && sc->ctts_index < sc->ctts_count) { |
3759 | pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration; |
3760 | /* update ctts context */ |
3761 | sc->ctts_sample++; |
3762 | if (sc->ctts_index < sc->ctts_count && |
3763 | sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) { |
3764 | sc->ctts_index++; |
3765 | sc->ctts_sample = 0; |
3766 | } |
3767 | if (sc->wrong_dts) |
3768 | pkt->dts = AV_NOPTS_VALUE; |
3769 | } else { |
3770 | int64_t next_dts = (sc->current_sample < st->nb_index_entries) ? |
3771 | st->index_entries[sc->current_sample].timestamp : st->duration; |
3772 | pkt->duration = next_dts - pkt->dts; |
3773 | pkt->pts = pkt->dts; |
3774 | } |
3775 | if (st->discard == AVDISCARD_ALL) |
3776 | goto retry; |
3777 | pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0; |
3778 | pkt->pos = sample->pos; |
3779 | av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n", |
3780 | pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration); |
3781 | return 0; |
3782 | } |
3783 | |
3784 | |
3785 | static int64_t mov_read_seek_sync(AVFormatContext *s, |
3786 | int stream_index, |
3787 | int64_t min_ts, |
3788 | int64_t target_ts, |
3789 | int64_t max_ts, |
3790 | int flags) |
3791 | { |
3792 | int64_t pos, t_pos; |
3793 | |
3794 | int64_t ts_ret, ts_adj; |
3795 | int stream_index_gen_search = stream_index; |
3796 | int sample, i; |
3797 | AVStream *st; |
3798 | AVParserState *backup; |
3799 | |
3800 | backup = ff_store_parser_state(s); |
3801 | |
3802 | // detect direction of seeking for search purposes |
3803 | flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ? |
3804 | AVSEEK_FLAG_BACKWARD : 0; |
3805 | |
3806 | st = s->streams[stream_index_gen_search]; |
3807 | sample = av_index_search_timestamp(st, target_ts, AVSEEK_FLAG_ANY); |
3808 | pos = st->index_entries[sample].pos; |
3809 | target_ts = st->index_entries[sample].timestamp; |
3810 | for (i = 0; i < s->nb_streams; i++) { |
3811 | MOVStreamContext *sc = s->streams[i]->priv_data; |
3812 | sc->current_sample = (sample - 500) > 0 ? (sample - 500) : 0; // hard code for mov, repos the sample. |
3813 | } |
3814 | |
3815 | // search for actual matching keyframe/starting position for all streams |
3816 | if ((t_pos = ff_gen_syncpoint_search(s, stream_index, pos, |
3817 | min_ts, target_ts, max_ts, |
3818 | flags)) < 0) { |
3819 | ff_restore_parser_state(s, backup); |
3820 | return -1; |
3821 | } |
3822 | |
3823 | ff_free_parser_state(s, backup); |
3824 | return t_pos; |
3825 | } |
3826 | |
3827 | static int64_t mov_read_seek2(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){ |
3828 | int ret; |
3829 | if (flags & AVSEEK_FLAG_BACKWARD) { |
3830 | flags &= ~AVSEEK_FLAG_BACKWARD; |
3831 | ret = mov_read_seek_sync(s, stream_index, INT64_MIN, target_ts, target_ts, flags); |
3832 | if (ret < 0) { |
3833 | // for compatibility reasons, seek to the best-fitting timestamp |
3834 | ret = mov_read_seek_sync(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags); |
3835 | } |
3836 | } else { |
3837 | ret = mov_read_seek_sync(s, stream_index, target_ts, target_ts, INT64_MAX, flags); |
3838 | if (ret < 0) |
3839 | // for compatibility reasons, seek to the best-fitting timestamp |
3840 | ret = mov_read_seek_sync(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags); |
3841 | } |
3842 | return ret; |
3843 | } |
3844 | |
3845 | static int mov_index_search_pos(const AVIndexEntry *entries, int nb_entries, |
3846 | int64_t pos, int flags) |
3847 | { |
3848 | int a, b, m; |
3849 | int64_t ppos; |
3850 | |
3851 | a = - 1; |
3852 | b = nb_entries; |
3853 | |
3854 | //optimize appending index entries at the end |
3855 | if(b && entries[b-1].pos < pos) |
3856 | a= b-1; |
3857 | |
3858 | while (b - a > 1) { |
3859 | m = (a + b) >> 1; |
3860 | ppos = entries[m].pos; |
3861 | if(ppos >= pos) |
3862 | b = m; |
3863 | if(ppos <= pos) |
3864 | a = m; |
3865 | } |
3866 | |
3867 | m= (flags & AVSEEK_FLAG_BACKWARD) ? a : b; |
3868 | return m; |
3869 | } |
3870 | |
3871 | static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags) |
3872 | { |
3873 | MOVStreamContext *sc = st->priv_data; |
3874 | int sample, time_sample; |
3875 | int i; |
3876 | |
3877 | sample = av_index_search_timestamp(st, timestamp, flags); |
3878 | |
3879 | // mov's stss is wrong sometimes, need to read seek |
3880 | // added by senbai.tao |
3881 | if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && sample <=0 && st->nb_index_entries && sc->keyframe_count <= 1) { |
3882 | int64_t sync_point = mov_read_seek2(s, st->index, timestamp, flags); |
3883 | sample = mov_index_search_pos(st->index_entries, st->nb_index_entries, sync_point, AVSEEK_FLAG_ANY); |
3884 | } |
3885 | |
3886 | av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample); |
3887 | if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp) |
3888 | sample = 0; |
3889 | if (sample < 0) /* not sure what to do */ |
3890 | return AVERROR_INVALIDDATA; |
3891 | sc->current_sample = sample; |
3892 | av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample); |
3893 | /* adjust ctts index */ |
3894 | if (sc->ctts_data) { |
3895 | time_sample = 0; |
3896 | for (i = 0; i < sc->ctts_count; i++) { |
3897 | int next = time_sample + sc->ctts_data[i].count; |
3898 | if (next > sc->current_sample) { |
3899 | sc->ctts_index = i; |
3900 | sc->ctts_sample = sc->current_sample - time_sample; |
3901 | break; |
3902 | } |
3903 | time_sample = next; |
3904 | } |
3905 | } |
3906 | return sample; |
3907 | } |
3908 | |
3909 | static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags) |
3910 | { |
3911 | AVStream *st; |
3912 | int64_t seek_timestamp, timestamp; |
3913 | int sample; |
3914 | int i; |
3915 | |
3916 | if (stream_index >= s->nb_streams) |
3917 | return AVERROR_INVALIDDATA; |
3918 | |
3919 | st = s->streams[stream_index]; |
3920 | sample = mov_seek_stream(s, st, sample_time, flags); |
3921 | if (sample < 0) |
3922 | return sample; |
3923 | |
3924 | /* adjust seek timestamp to found sample timestamp */ |
3925 | seek_timestamp = st->index_entries[sample].timestamp; |
3926 | |
3927 | for (i = 0; i < s->nb_streams; i++) { |
3928 | MOVStreamContext *sc = s->streams[i]->priv_data; |
3929 | st = s->streams[i]; |
3930 | st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0; |
3931 | |
3932 | if (stream_index == i) |
3933 | continue; |
3934 | |
3935 | timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base); |
3936 | mov_seek_stream(s, st, timestamp, flags); |
3937 | } |
3938 | return 0; |
3939 | } |
3940 | |
3941 | static const AVOption options[] = { |
3942 | {"use_absolute_path", |
3943 | "allow using absolute path when opening alias, this is a possible security issue", |
3944 | offsetof(MOVContext, use_absolute_path), FF_OPT_TYPE_INT, {.i64 = 0}, |
3945 | 0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM}, |
3946 | {"ignore_editlist", "", offsetof(MOVContext, ignore_editlist), FF_OPT_TYPE_INT, {.i64 = 0}, |
3947 | 0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM}, |
3948 | {NULL} |
3949 | }; |
3950 | |
3951 | static const AVClass mov_class = { |
3952 | .class_name = "mov,mp4,m4a,3gp,3g2,mj2", |
3953 | .item_name = av_default_item_name, |
3954 | .option = options, |
3955 | .version = LIBAVUTIL_VERSION_INT, |
3956 | }; |
3957 | |
3958 | AVInputFormat ff_mov_demuxer = { |
3959 | .name = "mov,mp4,m4a,3gp,3g2,mj2", |
3960 | .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"), |
3961 | .priv_data_size = sizeof(MOVContext), |
3962 | .read_probe = mov_probe, |
3963 | .read_header = mov_read_header, |
3964 | .read_packet = mov_read_packet, |
3965 | .read_close = mov_read_close, |
3966 | .read_seek = mov_read_seek, |
3967 | .priv_class = &mov_class, |
3968 | .flags = AVFMT_NO_BYTE_SEEK, |
3969 | }; |
3970 |