Libav
|
00001 /* 00002 * MOV demuxer 00003 * Copyright (c) 2001 Fabrice Bellard 00004 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com> 00005 * 00006 * This file is part of FFmpeg. 00007 * 00008 * FFmpeg is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * FFmpeg is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with FFmpeg; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #include <limits.h> 00024 00025 //#define DEBUG 00026 //#define DEBUG_METADATA 00027 //#define MOV_EXPORT_ALL_METADATA 00028 00029 #include "libavutil/intreadwrite.h" 00030 #include "libavutil/avstring.h" 00031 #include "avformat.h" 00032 #include "riff.h" 00033 #include "isom.h" 00034 #include "libavcodec/mpeg4audio.h" 00035 #include "libavcodec/mpegaudiodata.h" 00036 #include "libavcodec/get_bits.h" 00037 00038 #if CONFIG_ZLIB 00039 #include <zlib.h> 00040 #endif 00041 00042 /* 00043 * First version by Francois Revol revol@free.fr 00044 * Seek function by Gael Chardon gael.dev@4now.net 00045 * 00046 * Features and limitations: 00047 * - reads most of the QT files I have (at least the structure), 00048 * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html 00049 * - the code is quite ugly... maybe I won't do it recursive next time :-) 00050 * 00051 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/ 00052 * when coding this :) (it's a writer anyway) 00053 * 00054 * Reference documents: 00055 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt 00056 * Apple: 00057 * http://developer.apple.com/documentation/QuickTime/QTFF/ 00058 * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf 00059 * QuickTime is a trademark of Apple (AFAIK :)) 00060 */ 00061 00062 #include "qtpalette.h" 00063 00064 00065 #undef NDEBUG 00066 #include <assert.h> 00067 00068 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */ 00069 00070 /* those functions parse an atom */ 00071 /* return code: 00072 0: continue to parse next atom 00073 <0: error occurred, exit 00074 */ 00075 /* links atom IDs to parse functions */ 00076 typedef struct MOVParseTableEntry { 00077 uint32_t type; 00078 int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOVAtom atom); 00079 } MOVParseTableEntry; 00080 00081 static const MOVParseTableEntry mov_default_parse_table[]; 00082 00083 static int mov_metadata_trkn(MOVContext *c, ByteIOContext *pb, unsigned len) 00084 { 00085 char buf[16]; 00086 00087 get_be16(pb); // unknown 00088 snprintf(buf, sizeof(buf), "%d", get_be16(pb)); 00089 av_metadata_set2(&c->fc->metadata, "track", buf, 0); 00090 00091 get_be16(pb); // total tracks 00092 00093 return 0; 00094 } 00095 00096 static const uint32_t mac_to_unicode[128] = { 00097 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1, 00098 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8, 00099 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3, 00100 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC, 00101 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF, 00102 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8, 00103 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211, 00104 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8, 00105 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB, 00106 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153, 00107 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA, 00108 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02, 00109 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1, 00110 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4, 00111 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC, 00112 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7, 00113 }; 00114 00115 static int mov_read_mac_string(MOVContext *c, ByteIOContext *pb, int len, 00116 char *dst, int dstlen) 00117 { 00118 char *p = dst; 00119 char *end = dst+dstlen-1; 00120 int i; 00121 00122 for (i = 0; i < len; i++) { 00123 uint8_t t, c = get_byte(pb); 00124 if (c < 0x80 && p < end) 00125 *p++ = c; 00126 else 00127 PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;); 00128 } 00129 *p = 0; 00130 return p - dst; 00131 } 00132 00133 static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00134 { 00135 #ifdef MOV_EXPORT_ALL_METADATA 00136 char tmp_key[5]; 00137 #endif 00138 char str[1024], key2[16], language[4] = {0}; 00139 const char *key = NULL; 00140 uint16_t str_size, langcode = 0; 00141 uint32_t data_type = 0; 00142 int (*parse)(MOVContext*, ByteIOContext*, unsigned) = NULL; 00143 00144 switch (atom.type) { 00145 case MKTAG(0xa9,'n','a','m'): key = "title"; break; 00146 case MKTAG(0xa9,'a','u','t'): 00147 case MKTAG(0xa9,'A','R','T'): key = "artist"; break; 00148 case MKTAG(0xa9,'w','r','t'): key = "composer"; break; 00149 case MKTAG( 'c','p','r','t'): 00150 case MKTAG(0xa9,'c','p','y'): key = "copyright"; break; 00151 case MKTAG(0xa9,'c','m','t'): 00152 case MKTAG(0xa9,'i','n','f'): key = "comment"; break; 00153 case MKTAG(0xa9,'a','l','b'): key = "album"; break; 00154 case MKTAG(0xa9,'d','a','y'): key = "date"; break; 00155 case MKTAG(0xa9,'g','e','n'): key = "genre"; break; 00156 case MKTAG(0xa9,'t','o','o'): 00157 case MKTAG(0xa9,'e','n','c'): key = "encoder"; break; 00158 case MKTAG( 'd','e','s','c'): key = "description";break; 00159 case MKTAG( 'l','d','e','s'): key = "synopsis"; break; 00160 case MKTAG( 't','v','s','h'): key = "show"; break; 00161 case MKTAG( 't','v','e','n'): key = "episode_id";break; 00162 case MKTAG( 't','v','n','n'): key = "network"; break; 00163 case MKTAG( 't','r','k','n'): key = "track"; 00164 parse = mov_metadata_trkn; break; 00165 } 00166 00167 if (c->itunes_metadata && atom.size > 8) { 00168 int data_size = get_be32(pb); 00169 int tag = get_le32(pb); 00170 if (tag == MKTAG('d','a','t','a')) { 00171 data_type = get_be32(pb); // type 00172 get_be32(pb); // unknown 00173 str_size = data_size - 16; 00174 atom.size -= 16; 00175 } else return 0; 00176 } else if (atom.size > 4 && key && !c->itunes_metadata) { 00177 str_size = get_be16(pb); // string length 00178 langcode = get_be16(pb); 00179 ff_mov_lang_to_iso639(langcode, language); 00180 atom.size -= 4; 00181 } else 00182 str_size = atom.size; 00183 00184 #ifdef MOV_EXPORT_ALL_METADATA 00185 if (!key) { 00186 snprintf(tmp_key, 5, "%.4s", (char*)&atom.type); 00187 key = tmp_key; 00188 } 00189 #endif 00190 00191 if (!key) 00192 return 0; 00193 if (atom.size < 0) 00194 return -1; 00195 00196 str_size = FFMIN3(sizeof(str)-1, str_size, atom.size); 00197 00198 if (parse) 00199 parse(c, pb, str_size); 00200 else { 00201 if (data_type == 3 || (data_type == 0 && langcode < 0x800)) { // MAC Encoded 00202 mov_read_mac_string(c, pb, str_size, str, sizeof(str)); 00203 } else { 00204 get_buffer(pb, str, str_size); 00205 str[str_size] = 0; 00206 } 00207 av_metadata_set2(&c->fc->metadata, key, str, 0); 00208 if (*language && strcmp(language, "und")) { 00209 snprintf(key2, sizeof(key2), "%s-%s", key, language); 00210 av_metadata_set2(&c->fc->metadata, key2, str, 0); 00211 } 00212 } 00213 #ifdef DEBUG_METADATA 00214 av_log(c->fc, AV_LOG_DEBUG, "lang \"%3s\" ", language); 00215 av_log(c->fc, AV_LOG_DEBUG, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %lld\n", 00216 key, str, (char*)&atom.type, str_size, atom.size); 00217 #endif 00218 00219 return 0; 00220 } 00221 00222 static int mov_read_chpl(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00223 { 00224 int64_t start; 00225 int i, nb_chapters, str_len; 00226 char str[256+1]; 00227 00228 if ((atom.size -= 5) < 0) 00229 return 0; 00230 00231 get_be32(pb); // version + flags 00232 get_be32(pb); // ??? 00233 nb_chapters = get_byte(pb); 00234 00235 for (i = 0; i < nb_chapters; i++) { 00236 if (atom.size < 9) 00237 return 0; 00238 00239 start = get_be64(pb); 00240 str_len = get_byte(pb); 00241 00242 if ((atom.size -= 9+str_len) < 0) 00243 return 0; 00244 00245 get_buffer(pb, str, str_len); 00246 str[str_len] = 0; 00247 ff_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str); 00248 } 00249 return 0; 00250 } 00251 00252 static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00253 { 00254 int64_t total_size = 0; 00255 MOVAtom a; 00256 int i; 00257 00258 if (atom.size < 0) 00259 atom.size = INT64_MAX; 00260 while (total_size + 8 < atom.size && !url_feof(pb)) { 00261 int (*parse)(MOVContext*, ByteIOContext*, MOVAtom) = NULL; 00262 a.size = atom.size; 00263 a.type=0; 00264 if(atom.size >= 8) { 00265 a.size = get_be32(pb); 00266 a.type = get_le32(pb); 00267 } 00268 total_size += 8; 00269 dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n", 00270 a.type, (char*)&a.type, a.size, atom.size, total_size); 00271 if (a.size == 1) { /* 64 bit extended size */ 00272 a.size = get_be64(pb) - 8; 00273 total_size += 8; 00274 } 00275 if (a.size == 0) { 00276 a.size = atom.size - total_size; 00277 if (a.size <= 8) 00278 break; 00279 } 00280 a.size -= 8; 00281 if(a.size < 0) 00282 break; 00283 a.size = FFMIN(a.size, atom.size - total_size); 00284 00285 for (i = 0; mov_default_parse_table[i].type; i++) 00286 if (mov_default_parse_table[i].type == a.type) { 00287 parse = mov_default_parse_table[i].parse; 00288 break; 00289 } 00290 00291 // container is user data 00292 if (!parse && (atom.type == MKTAG('u','d','t','a') || 00293 atom.type == MKTAG('i','l','s','t'))) 00294 parse = mov_read_udta_string; 00295 00296 if (!parse) { /* skip leaf atoms data */ 00297 url_fskip(pb, a.size); 00298 } else { 00299 int64_t start_pos = url_ftell(pb); 00300 int64_t left; 00301 int err = parse(c, pb, a); 00302 if (err < 0) 00303 return err; 00304 if (c->found_moov && c->found_mdat && 00305 (url_is_streamed(pb) || start_pos + a.size == url_fsize(pb))) 00306 return 0; 00307 left = a.size - url_ftell(pb) + start_pos; 00308 if (left > 0) /* skip garbage at atom end */ 00309 url_fskip(pb, left); 00310 } 00311 00312 total_size += a.size; 00313 } 00314 00315 if (total_size < atom.size && atom.size < 0x7ffff) 00316 url_fskip(pb, atom.size - total_size); 00317 00318 return 0; 00319 } 00320 00321 static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00322 { 00323 AVStream *st; 00324 MOVStreamContext *sc; 00325 int entries, i, j; 00326 00327 if (c->fc->nb_streams < 1) 00328 return 0; 00329 st = c->fc->streams[c->fc->nb_streams-1]; 00330 sc = st->priv_data; 00331 00332 get_be32(pb); // version + flags 00333 entries = get_be32(pb); 00334 if (entries >= UINT_MAX / sizeof(*sc->drefs)) 00335 return -1; 00336 sc->drefs = av_mallocz(entries * sizeof(*sc->drefs)); 00337 if (!sc->drefs) 00338 return AVERROR(ENOMEM); 00339 sc->drefs_count = entries; 00340 00341 for (i = 0; i < sc->drefs_count; i++) { 00342 MOVDref *dref = &sc->drefs[i]; 00343 uint32_t size = get_be32(pb); 00344 int64_t next = url_ftell(pb) + size - 4; 00345 00346 dref->type = get_le32(pb); 00347 get_be32(pb); // version + flags 00348 dprintf(c->fc, "type %.4s size %d\n", (char*)&dref->type, size); 00349 00350 if (dref->type == MKTAG('a','l','i','s') && size > 150) { 00351 /* macintosh alias record */ 00352 uint16_t volume_len, len; 00353 int16_t type; 00354 00355 url_fskip(pb, 10); 00356 00357 volume_len = get_byte(pb); 00358 volume_len = FFMIN(volume_len, 27); 00359 get_buffer(pb, dref->volume, 27); 00360 dref->volume[volume_len] = 0; 00361 av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len); 00362 00363 url_fskip(pb, 12); 00364 00365 len = get_byte(pb); 00366 len = FFMIN(len, 63); 00367 get_buffer(pb, dref->filename, 63); 00368 dref->filename[len] = 0; 00369 av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len); 00370 00371 url_fskip(pb, 16); 00372 00373 /* read next level up_from_alias/down_to_target */ 00374 dref->nlvl_from = get_be16(pb); 00375 dref->nlvl_to = get_be16(pb); 00376 av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n", 00377 dref->nlvl_from, dref->nlvl_to); 00378 00379 url_fskip(pb, 16); 00380 00381 for (type = 0; type != -1 && url_ftell(pb) < next; ) { 00382 type = get_be16(pb); 00383 len = get_be16(pb); 00384 av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len); 00385 if (len&1) 00386 len += 1; 00387 if (type == 2) { // absolute path 00388 av_free(dref->path); 00389 dref->path = av_mallocz(len+1); 00390 if (!dref->path) 00391 return AVERROR(ENOMEM); 00392 get_buffer(pb, dref->path, len); 00393 if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) { 00394 len -= volume_len; 00395 memmove(dref->path, dref->path+volume_len, len); 00396 dref->path[len] = 0; 00397 } 00398 for (j = 0; j < len; j++) 00399 if (dref->path[j] == ':') 00400 dref->path[j] = '/'; 00401 av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path); 00402 } else if (type == 0) { // directory name 00403 av_free(dref->dir); 00404 dref->dir = av_malloc(len+1); 00405 if (!dref->dir) 00406 return AVERROR(ENOMEM); 00407 get_buffer(pb, dref->dir, len); 00408 dref->dir[len] = 0; 00409 for (j = 0; j < len; j++) 00410 if (dref->dir[j] == ':') 00411 dref->dir[j] = '/'; 00412 av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir); 00413 } else 00414 url_fskip(pb, len); 00415 } 00416 } 00417 url_fseek(pb, next, SEEK_SET); 00418 } 00419 return 0; 00420 } 00421 00422 static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00423 { 00424 AVStream *st; 00425 uint32_t type; 00426 uint32_t ctype; 00427 00428 if (c->fc->nb_streams < 1) // meta before first trak 00429 return 0; 00430 00431 st = c->fc->streams[c->fc->nb_streams-1]; 00432 00433 get_byte(pb); /* version */ 00434 get_be24(pb); /* flags */ 00435 00436 /* component type */ 00437 ctype = get_le32(pb); 00438 type = get_le32(pb); /* component subtype */ 00439 00440 dprintf(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype); 00441 dprintf(c->fc, "stype= %.4s\n", (char*)&type); 00442 00443 if (type == MKTAG('v','i','d','e')) 00444 st->codec->codec_type = AVMEDIA_TYPE_VIDEO; 00445 else if(type == MKTAG('s','o','u','n')) 00446 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; 00447 else if(type == MKTAG('m','1','a',' ')) 00448 st->codec->codec_id = CODEC_ID_MP2; 00449 else if(type == MKTAG('s','u','b','p')) 00450 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; 00451 00452 get_be32(pb); /* component manufacture */ 00453 get_be32(pb); /* component flags */ 00454 get_be32(pb); /* component flags mask */ 00455 00456 return 0; 00457 } 00458 00459 int ff_mp4_read_descr_len(ByteIOContext *pb) 00460 { 00461 int len = 0; 00462 int count = 4; 00463 while (count--) { 00464 int c = get_byte(pb); 00465 len = (len << 7) | (c & 0x7f); 00466 if (!(c & 0x80)) 00467 break; 00468 } 00469 return len; 00470 } 00471 00472 static int mp4_read_descr(AVFormatContext *fc, ByteIOContext *pb, int *tag) 00473 { 00474 int len; 00475 *tag = get_byte(pb); 00476 len = ff_mp4_read_descr_len(pb); 00477 dprintf(fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len); 00478 return len; 00479 } 00480 00481 #define MP4ESDescrTag 0x03 00482 #define MP4DecConfigDescrTag 0x04 00483 #define MP4DecSpecificDescrTag 0x05 00484 00485 static const AVCodecTag mp4_audio_types[] = { 00486 { CODEC_ID_MP3ON4, AOT_PS }, /* old mp3on4 draft */ 00487 { CODEC_ID_MP3ON4, AOT_L1 }, /* layer 1 */ 00488 { CODEC_ID_MP3ON4, AOT_L2 }, /* layer 2 */ 00489 { CODEC_ID_MP3ON4, AOT_L3 }, /* layer 3 */ 00490 { CODEC_ID_MP4ALS, AOT_ALS }, /* MPEG-4 ALS */ 00491 { CODEC_ID_NONE, AOT_NULL }, 00492 }; 00493 00494 int ff_mov_read_esds(AVFormatContext *fc, ByteIOContext *pb, MOVAtom atom) 00495 { 00496 AVStream *st; 00497 int tag, len; 00498 00499 if (fc->nb_streams < 1) 00500 return 0; 00501 st = fc->streams[fc->nb_streams-1]; 00502 00503 get_be32(pb); /* version + flags */ 00504 len = mp4_read_descr(fc, pb, &tag); 00505 if (tag == MP4ESDescrTag) { 00506 get_be16(pb); /* ID */ 00507 get_byte(pb); /* priority */ 00508 } else 00509 get_be16(pb); /* ID */ 00510 00511 len = mp4_read_descr(fc, pb, &tag); 00512 if (tag == MP4DecConfigDescrTag) { 00513 int object_type_id = get_byte(pb); 00514 get_byte(pb); /* stream type */ 00515 get_be24(pb); /* buffer size db */ 00516 get_be32(pb); /* max bitrate */ 00517 get_be32(pb); /* avg bitrate */ 00518 00519 st->codec->codec_id= ff_codec_get_id(ff_mp4_obj_type, object_type_id); 00520 dprintf(fc, "esds object type id 0x%02x\n", object_type_id); 00521 len = mp4_read_descr(fc, pb, &tag); 00522 if (tag == MP4DecSpecificDescrTag) { 00523 dprintf(fc, "Specific MPEG4 header len=%d\n", len); 00524 if((uint64_t)len > (1<<30)) 00525 return -1; 00526 st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE); 00527 if (!st->codec->extradata) 00528 return AVERROR(ENOMEM); 00529 get_buffer(pb, st->codec->extradata, len); 00530 st->codec->extradata_size = len; 00531 if (st->codec->codec_id == CODEC_ID_AAC) { 00532 MPEG4AudioConfig cfg; 00533 ff_mpeg4audio_get_config(&cfg, st->codec->extradata, 00534 st->codec->extradata_size); 00535 st->codec->channels = cfg.channels; 00536 if (cfg.object_type == 29 && cfg.sampling_index < 3) // old mp3on4 00537 st->codec->sample_rate = ff_mpa_freq_tab[cfg.sampling_index]; 00538 else 00539 st->codec->sample_rate = cfg.sample_rate; // ext sample rate ? 00540 dprintf(fc, "mp4a config channels %d obj %d ext obj %d " 00541 "sample rate %d ext sample rate %d\n", st->codec->channels, 00542 cfg.object_type, cfg.ext_object_type, 00543 cfg.sample_rate, cfg.ext_sample_rate); 00544 if (!(st->codec->codec_id = ff_codec_get_id(mp4_audio_types, 00545 cfg.object_type))) 00546 st->codec->codec_id = CODEC_ID_AAC; 00547 } 00548 } 00549 } 00550 return 0; 00551 } 00552 00553 static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00554 { 00555 return ff_mov_read_esds(c->fc, pb, atom); 00556 } 00557 00558 static int mov_read_pasp(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00559 { 00560 const int num = get_be32(pb); 00561 const int den = get_be32(pb); 00562 AVStream *st; 00563 00564 if (c->fc->nb_streams < 1) 00565 return 0; 00566 st = c->fc->streams[c->fc->nb_streams-1]; 00567 00568 if (den != 0) { 00569 if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default 00570 (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) 00571 av_log(c->fc, AV_LOG_WARNING, 00572 "sample aspect ratio already set to %d:%d, overriding by 'pasp' atom\n", 00573 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den); 00574 st->sample_aspect_ratio.num = num; 00575 st->sample_aspect_ratio.den = den; 00576 } 00577 return 0; 00578 } 00579 00580 /* this atom contains actual media data */ 00581 static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00582 { 00583 if(atom.size == 0) /* wrong one (MP4) */ 00584 return 0; 00585 c->found_mdat=1; 00586 return 0; /* now go for moov */ 00587 } 00588 00589 /* read major brand, minor version and compatible brands and store them as metadata */ 00590 static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00591 { 00592 uint32_t minor_ver; 00593 int comp_brand_size; 00594 char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */ 00595 char* comp_brands_str; 00596 uint8_t type[5] = {0}; 00597 00598 get_buffer(pb, type, 4); 00599 if (strcmp(type, "qt ")) 00600 c->isom = 1; 00601 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type); 00602 av_metadata_set2(&c->fc->metadata, "major_brand", type, 0); 00603 minor_ver = get_be32(pb); /* minor version */ 00604 snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver); 00605 av_metadata_set2(&c->fc->metadata, "minor_version", minor_ver_str, 0); 00606 00607 comp_brand_size = atom.size - 8; 00608 if (comp_brand_size < 0) 00609 return -1; 00610 comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */ 00611 if (!comp_brands_str) 00612 return AVERROR(ENOMEM); 00613 get_buffer(pb, comp_brands_str, comp_brand_size); 00614 comp_brands_str[comp_brand_size] = 0; 00615 av_metadata_set2(&c->fc->metadata, "compatible_brands", comp_brands_str, 0); 00616 av_freep(&comp_brands_str); 00617 00618 return 0; 00619 } 00620 00621 /* this atom should contain all header atoms */ 00622 static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00623 { 00624 if (mov_read_default(c, pb, atom) < 0) 00625 return -1; 00626 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */ 00627 /* so we don't parse the whole file if over a network */ 00628 c->found_moov=1; 00629 return 0; /* now go for mdat */ 00630 } 00631 00632 static int mov_read_moof(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00633 { 00634 c->fragment.moof_offset = url_ftell(pb) - 8; 00635 dprintf(c->fc, "moof offset %llx\n", c->fragment.moof_offset); 00636 return mov_read_default(c, pb, atom); 00637 } 00638 00639 static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00640 { 00641 AVStream *st; 00642 MOVStreamContext *sc; 00643 int version; 00644 char language[4] = {0}; 00645 unsigned lang; 00646 00647 if (c->fc->nb_streams < 1) 00648 return 0; 00649 st = c->fc->streams[c->fc->nb_streams-1]; 00650 sc = st->priv_data; 00651 00652 version = get_byte(pb); 00653 if (version > 1) 00654 return -1; /* unsupported */ 00655 00656 get_be24(pb); /* flags */ 00657 if (version == 1) { 00658 get_be64(pb); 00659 get_be64(pb); 00660 } else { 00661 get_be32(pb); /* creation time */ 00662 get_be32(pb); /* modification time */ 00663 } 00664 00665 sc->time_scale = get_be32(pb); 00666 st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */ 00667 00668 lang = get_be16(pb); /* language */ 00669 if (ff_mov_lang_to_iso639(lang, language)) 00670 av_metadata_set2(&st->metadata, "language", language, 0); 00671 get_be16(pb); /* quality */ 00672 00673 return 0; 00674 } 00675 00676 static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00677 { 00678 int version = get_byte(pb); /* version */ 00679 get_be24(pb); /* flags */ 00680 00681 if (version == 1) { 00682 get_be64(pb); 00683 get_be64(pb); 00684 } else { 00685 get_be32(pb); /* creation time */ 00686 get_be32(pb); /* modification time */ 00687 } 00688 c->time_scale = get_be32(pb); /* time scale */ 00689 00690 dprintf(c->fc, "time scale = %i\n", c->time_scale); 00691 00692 c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */ 00693 get_be32(pb); /* preferred scale */ 00694 00695 get_be16(pb); /* preferred volume */ 00696 00697 url_fskip(pb, 10); /* reserved */ 00698 00699 url_fskip(pb, 36); /* display matrix */ 00700 00701 get_be32(pb); /* preview time */ 00702 get_be32(pb); /* preview duration */ 00703 get_be32(pb); /* poster time */ 00704 get_be32(pb); /* selection time */ 00705 get_be32(pb); /* selection duration */ 00706 get_be32(pb); /* current time */ 00707 get_be32(pb); /* next track ID */ 00708 00709 return 0; 00710 } 00711 00712 static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00713 { 00714 AVStream *st; 00715 00716 if (c->fc->nb_streams < 1) 00717 return 0; 00718 st = c->fc->streams[c->fc->nb_streams-1]; 00719 00720 if((uint64_t)atom.size > (1<<30)) 00721 return -1; 00722 00723 // currently SVQ3 decoder expect full STSD header - so let's fake it 00724 // this should be fixed and just SMI header should be passed 00725 av_free(st->codec->extradata); 00726 st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE); 00727 if (!st->codec->extradata) 00728 return AVERROR(ENOMEM); 00729 st->codec->extradata_size = 0x5a + atom.size; 00730 memcpy(st->codec->extradata, "SVQ3", 4); // fake 00731 get_buffer(pb, st->codec->extradata + 0x5a, atom.size); 00732 dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a); 00733 return 0; 00734 } 00735 00736 static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00737 { 00738 AVStream *st; 00739 int little_endian; 00740 00741 if (c->fc->nb_streams < 1) 00742 return 0; 00743 st = c->fc->streams[c->fc->nb_streams-1]; 00744 00745 little_endian = get_be16(pb); 00746 dprintf(c->fc, "enda %d\n", little_endian); 00747 if (little_endian == 1) { 00748 switch (st->codec->codec_id) { 00749 case CODEC_ID_PCM_S24BE: 00750 st->codec->codec_id = CODEC_ID_PCM_S24LE; 00751 break; 00752 case CODEC_ID_PCM_S32BE: 00753 st->codec->codec_id = CODEC_ID_PCM_S32LE; 00754 break; 00755 case CODEC_ID_PCM_F32BE: 00756 st->codec->codec_id = CODEC_ID_PCM_F32LE; 00757 break; 00758 case CODEC_ID_PCM_F64BE: 00759 st->codec->codec_id = CODEC_ID_PCM_F64LE; 00760 break; 00761 default: 00762 break; 00763 } 00764 } 00765 return 0; 00766 } 00767 00768 /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */ 00769 static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00770 { 00771 AVStream *st; 00772 uint64_t size; 00773 uint8_t *buf; 00774 00775 if (c->fc->nb_streams < 1) // will happen with jp2 files 00776 return 0; 00777 st= c->fc->streams[c->fc->nb_streams-1]; 00778 size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE; 00779 if(size > INT_MAX || (uint64_t)atom.size > INT_MAX) 00780 return -1; 00781 buf= av_realloc(st->codec->extradata, size); 00782 if(!buf) 00783 return -1; 00784 st->codec->extradata= buf; 00785 buf+= st->codec->extradata_size; 00786 st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE; 00787 AV_WB32( buf , atom.size + 8); 00788 AV_WL32( buf + 4, atom.type); 00789 get_buffer(pb, buf + 8, atom.size); 00790 return 0; 00791 } 00792 00793 static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00794 { 00795 AVStream *st; 00796 00797 if (c->fc->nb_streams < 1) 00798 return 0; 00799 st = c->fc->streams[c->fc->nb_streams-1]; 00800 00801 if((uint64_t)atom.size > (1<<30)) 00802 return -1; 00803 00804 if (st->codec->codec_id == CODEC_ID_QDM2) { 00805 // pass all frma atom to codec, needed at least for QDM2 00806 av_free(st->codec->extradata); 00807 st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE); 00808 if (!st->codec->extradata) 00809 return AVERROR(ENOMEM); 00810 st->codec->extradata_size = atom.size; 00811 get_buffer(pb, st->codec->extradata, atom.size); 00812 } else if (atom.size > 8) { /* to read frma, esds atoms */ 00813 if (mov_read_default(c, pb, atom) < 0) 00814 return -1; 00815 } else 00816 url_fskip(pb, atom.size); 00817 return 0; 00818 } 00819 00824 static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00825 { 00826 AVStream *st; 00827 00828 if (c->fc->nb_streams < 1) 00829 return 0; 00830 st = c->fc->streams[c->fc->nb_streams-1]; 00831 00832 if((uint64_t)atom.size > (1<<30)) 00833 return -1; 00834 00835 av_free(st->codec->extradata); 00836 st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE); 00837 if (!st->codec->extradata) 00838 return AVERROR(ENOMEM); 00839 st->codec->extradata_size = atom.size; 00840 get_buffer(pb, st->codec->extradata, atom.size); 00841 return 0; 00842 } 00843 00849 static int mov_read_strf(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00850 { 00851 AVStream *st; 00852 00853 if (c->fc->nb_streams < 1) 00854 return 0; 00855 if (atom.size <= 40) 00856 return 0; 00857 st = c->fc->streams[c->fc->nb_streams-1]; 00858 00859 if((uint64_t)atom.size > (1<<30)) 00860 return -1; 00861 00862 av_free(st->codec->extradata); 00863 st->codec->extradata = av_mallocz(atom.size - 40 + FF_INPUT_BUFFER_PADDING_SIZE); 00864 if (!st->codec->extradata) 00865 return AVERROR(ENOMEM); 00866 st->codec->extradata_size = atom.size - 40; 00867 url_fskip(pb, 40); 00868 get_buffer(pb, st->codec->extradata, atom.size - 40); 00869 return 0; 00870 } 00871 00872 static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00873 { 00874 AVStream *st; 00875 MOVStreamContext *sc; 00876 unsigned int i, entries; 00877 00878 if (c->fc->nb_streams < 1) 00879 return 0; 00880 st = c->fc->streams[c->fc->nb_streams-1]; 00881 sc = st->priv_data; 00882 00883 get_byte(pb); /* version */ 00884 get_be24(pb); /* flags */ 00885 00886 entries = get_be32(pb); 00887 00888 if(entries >= UINT_MAX/sizeof(int64_t)) 00889 return -1; 00890 00891 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t)); 00892 if (!sc->chunk_offsets) 00893 return AVERROR(ENOMEM); 00894 sc->chunk_count = entries; 00895 00896 if (atom.type == MKTAG('s','t','c','o')) 00897 for(i=0; i<entries; i++) 00898 sc->chunk_offsets[i] = get_be32(pb); 00899 else if (atom.type == MKTAG('c','o','6','4')) 00900 for(i=0; i<entries; i++) 00901 sc->chunk_offsets[i] = get_be64(pb); 00902 else 00903 return -1; 00904 00905 return 0; 00906 } 00907 00912 enum CodecID ff_mov_get_lpcm_codec_id(int bps, int flags) 00913 { 00914 if (flags & 1) { // floating point 00915 if (flags & 2) { // big endian 00916 if (bps == 32) return CODEC_ID_PCM_F32BE; 00917 else if (bps == 64) return CODEC_ID_PCM_F64BE; 00918 } else { 00919 if (bps == 32) return CODEC_ID_PCM_F32LE; 00920 else if (bps == 64) return CODEC_ID_PCM_F64LE; 00921 } 00922 } else { 00923 if (flags & 2) { 00924 if (bps == 8) 00925 // signed integer 00926 if (flags & 4) return CODEC_ID_PCM_S8; 00927 else return CODEC_ID_PCM_U8; 00928 else if (bps == 16) return CODEC_ID_PCM_S16BE; 00929 else if (bps == 24) return CODEC_ID_PCM_S24BE; 00930 else if (bps == 32) return CODEC_ID_PCM_S32BE; 00931 } else { 00932 if (bps == 8) 00933 if (flags & 4) return CODEC_ID_PCM_S8; 00934 else return CODEC_ID_PCM_U8; 00935 else if (bps == 16) return CODEC_ID_PCM_S16LE; 00936 else if (bps == 24) return CODEC_ID_PCM_S24LE; 00937 else if (bps == 32) return CODEC_ID_PCM_S32LE; 00938 } 00939 } 00940 return CODEC_ID_NONE; 00941 } 00942 00943 static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 00944 { 00945 AVStream *st; 00946 MOVStreamContext *sc; 00947 int j, entries, pseudo_stream_id; 00948 00949 if (c->fc->nb_streams < 1) 00950 return 0; 00951 st = c->fc->streams[c->fc->nb_streams-1]; 00952 sc = st->priv_data; 00953 00954 get_byte(pb); /* version */ 00955 get_be24(pb); /* flags */ 00956 00957 entries = get_be32(pb); 00958 00959 for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) { 00960 //Parsing Sample description table 00961 enum CodecID id; 00962 int dref_id = 1; 00963 MOVAtom a = { 0 }; 00964 int64_t start_pos = url_ftell(pb); 00965 int size = get_be32(pb); /* size */ 00966 uint32_t format = get_le32(pb); /* data format */ 00967 00968 if (size >= 16) { 00969 get_be32(pb); /* reserved */ 00970 get_be16(pb); /* reserved */ 00971 dref_id = get_be16(pb); 00972 } 00973 00974 if (st->codec->codec_tag && 00975 st->codec->codec_tag != format && 00976 (c->fc->video_codec_id ? ff_codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id 00977 : st->codec->codec_tag != MKTAG('j','p','e','g')) 00978 ){ 00979 /* Multiple fourcc, we skip JPEG. This is not correct, we should 00980 * export it as a separate AVStream but this needs a few changes 00981 * in the MOV demuxer, patch welcome. */ 00982 av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n"); 00983 url_fskip(pb, size - (url_ftell(pb) - start_pos)); 00984 continue; 00985 } 00986 sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id; 00987 sc->dref_id= dref_id; 00988 00989 st->codec->codec_tag = format; 00990 id = ff_codec_get_id(codec_movaudio_tags, format); 00991 if (id<=0 && ((format&0xFFFF) == 'm'+('s'<<8) || (format&0xFFFF) == 'T'+('S'<<8))) 00992 id = ff_codec_get_id(ff_codec_wav_tags, bswap_32(format)&0xFFFF); 00993 00994 if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) { 00995 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; 00996 } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO && /* do not overwrite codec type */ 00997 format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */ 00998 id = ff_codec_get_id(codec_movvideo_tags, format); 00999 if (id <= 0) 01000 id = ff_codec_get_id(ff_codec_bmp_tags, format); 01001 if (id > 0) 01002 st->codec->codec_type = AVMEDIA_TYPE_VIDEO; 01003 else if(st->codec->codec_type == AVMEDIA_TYPE_DATA){ 01004 id = ff_codec_get_id(ff_codec_movsubtitle_tags, format); 01005 if(id > 0) 01006 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; 01007 } 01008 } 01009 01010 dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size, 01011 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, 01012 (format >> 24) & 0xff, st->codec->codec_type); 01013 01014 if(st->codec->codec_type==AVMEDIA_TYPE_VIDEO) { 01015 unsigned int color_depth, len; 01016 int color_greyscale; 01017 01018 st->codec->codec_id = id; 01019 get_be16(pb); /* version */ 01020 get_be16(pb); /* revision level */ 01021 get_be32(pb); /* vendor */ 01022 get_be32(pb); /* temporal quality */ 01023 get_be32(pb); /* spatial quality */ 01024 01025 st->codec->width = get_be16(pb); /* width */ 01026 st->codec->height = get_be16(pb); /* height */ 01027 01028 get_be32(pb); /* horiz resolution */ 01029 get_be32(pb); /* vert resolution */ 01030 get_be32(pb); /* data size, always 0 */ 01031 get_be16(pb); /* frames per samples */ 01032 01033 len = get_byte(pb); /* codec name, pascal string */ 01034 if (len > 31) 01035 len = 31; 01036 mov_read_mac_string(c, pb, len, st->codec->codec_name, 32); 01037 if (len < 31) 01038 url_fskip(pb, 31 - len); 01039 /* codec_tag YV12 triggers an UV swap in rawdec.c */ 01040 if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) 01041 st->codec->codec_tag=MKTAG('I', '4', '2', '0'); 01042 01043 st->codec->bits_per_coded_sample = get_be16(pb); /* depth */ 01044 st->codec->color_table_id = get_be16(pb); /* colortable id */ 01045 dprintf(c->fc, "depth %d, ctab id %d\n", 01046 st->codec->bits_per_coded_sample, st->codec->color_table_id); 01047 /* figure out the palette situation */ 01048 color_depth = st->codec->bits_per_coded_sample & 0x1F; 01049 color_greyscale = st->codec->bits_per_coded_sample & 0x20; 01050 01051 /* if the depth is 2, 4, or 8 bpp, file is palettized */ 01052 if ((color_depth == 2) || (color_depth == 4) || 01053 (color_depth == 8)) { 01054 /* for palette traversal */ 01055 unsigned int color_start, color_count, color_end; 01056 unsigned char r, g, b; 01057 01058 st->codec->palctrl = av_malloc(sizeof(*st->codec->palctrl)); 01059 if (color_greyscale) { 01060 int color_index, color_dec; 01061 /* compute the greyscale palette */ 01062 st->codec->bits_per_coded_sample = color_depth; 01063 color_count = 1 << color_depth; 01064 color_index = 255; 01065 color_dec = 256 / (color_count - 1); 01066 for (j = 0; j < color_count; j++) { 01067 r = g = b = color_index; 01068 st->codec->palctrl->palette[j] = 01069 (r << 16) | (g << 8) | (b); 01070 color_index -= color_dec; 01071 if (color_index < 0) 01072 color_index = 0; 01073 } 01074 } else if (st->codec->color_table_id) { 01075 const uint8_t *color_table; 01076 /* if flag bit 3 is set, use the default palette */ 01077 color_count = 1 << color_depth; 01078 if (color_depth == 2) 01079 color_table = ff_qt_default_palette_4; 01080 else if (color_depth == 4) 01081 color_table = ff_qt_default_palette_16; 01082 else 01083 color_table = ff_qt_default_palette_256; 01084 01085 for (j = 0; j < color_count; j++) { 01086 r = color_table[j * 3 + 0]; 01087 g = color_table[j * 3 + 1]; 01088 b = color_table[j * 3 + 2]; 01089 st->codec->palctrl->palette[j] = 01090 (r << 16) | (g << 8) | (b); 01091 } 01092 } else { 01093 /* load the palette from the file */ 01094 color_start = get_be32(pb); 01095 color_count = get_be16(pb); 01096 color_end = get_be16(pb); 01097 if ((color_start <= 255) && 01098 (color_end <= 255)) { 01099 for (j = color_start; j <= color_end; j++) { 01100 /* each R, G, or B component is 16 bits; 01101 * only use the top 8 bits; skip alpha bytes 01102 * up front */ 01103 get_byte(pb); 01104 get_byte(pb); 01105 r = get_byte(pb); 01106 get_byte(pb); 01107 g = get_byte(pb); 01108 get_byte(pb); 01109 b = get_byte(pb); 01110 get_byte(pb); 01111 st->codec->palctrl->palette[j] = 01112 (r << 16) | (g << 8) | (b); 01113 } 01114 } 01115 } 01116 st->codec->palctrl->palette_changed = 1; 01117 } 01118 } else if(st->codec->codec_type==AVMEDIA_TYPE_AUDIO) { 01119 int bits_per_sample, flags; 01120 uint16_t version = get_be16(pb); 01121 01122 st->codec->codec_id = id; 01123 get_be16(pb); /* revision level */ 01124 get_be32(pb); /* vendor */ 01125 01126 st->codec->channels = get_be16(pb); /* channel count */ 01127 dprintf(c->fc, "audio channels %d\n", st->codec->channels); 01128 st->codec->bits_per_coded_sample = get_be16(pb); /* sample size */ 01129 01130 sc->audio_cid = get_be16(pb); 01131 get_be16(pb); /* packet size = 0 */ 01132 01133 st->codec->sample_rate = ((get_be32(pb) >> 16)); 01134 01135 //Read QT version 1 fields. In version 0 these do not exist. 01136 dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom); 01137 if(!c->isom) { 01138 if(version==1) { 01139 sc->samples_per_frame = get_be32(pb); 01140 get_be32(pb); /* bytes per packet */ 01141 sc->bytes_per_frame = get_be32(pb); 01142 get_be32(pb); /* bytes per sample */ 01143 } else if(version==2) { 01144 get_be32(pb); /* sizeof struct only */ 01145 st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */ 01146 st->codec->channels = get_be32(pb); 01147 get_be32(pb); /* always 0x7F000000 */ 01148 st->codec->bits_per_coded_sample = get_be32(pb); /* bits per channel if sound is uncompressed */ 01149 flags = get_be32(pb); /* lpcm format specific flag */ 01150 sc->bytes_per_frame = get_be32(pb); /* bytes per audio packet if constant */ 01151 sc->samples_per_frame = get_be32(pb); /* lpcm frames per audio packet if constant */ 01152 if (format == MKTAG('l','p','c','m')) 01153 st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags); 01154 } 01155 } 01156 01157 switch (st->codec->codec_id) { 01158 case CODEC_ID_PCM_S8: 01159 case CODEC_ID_PCM_U8: 01160 if (st->codec->bits_per_coded_sample == 16) 01161 st->codec->codec_id = CODEC_ID_PCM_S16BE; 01162 break; 01163 case CODEC_ID_PCM_S16LE: 01164 case CODEC_ID_PCM_S16BE: 01165 if (st->codec->bits_per_coded_sample == 8) 01166 st->codec->codec_id = CODEC_ID_PCM_S8; 01167 else if (st->codec->bits_per_coded_sample == 24) 01168 st->codec->codec_id = 01169 st->codec->codec_id == CODEC_ID_PCM_S16BE ? 01170 CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE; 01171 break; 01172 /* set values for old format before stsd version 1 appeared */ 01173 case CODEC_ID_MACE3: 01174 sc->samples_per_frame = 6; 01175 sc->bytes_per_frame = 2*st->codec->channels; 01176 break; 01177 case CODEC_ID_MACE6: 01178 sc->samples_per_frame = 6; 01179 sc->bytes_per_frame = 1*st->codec->channels; 01180 break; 01181 case CODEC_ID_ADPCM_IMA_QT: 01182 sc->samples_per_frame = 64; 01183 sc->bytes_per_frame = 34*st->codec->channels; 01184 break; 01185 case CODEC_ID_GSM: 01186 sc->samples_per_frame = 160; 01187 sc->bytes_per_frame = 33; 01188 break; 01189 default: 01190 break; 01191 } 01192 01193 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id); 01194 if (bits_per_sample) { 01195 st->codec->bits_per_coded_sample = bits_per_sample; 01196 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels; 01197 } 01198 } else if(st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){ 01199 // ttxt stsd contains display flags, justification, background 01200 // color, fonts, and default styles, so fake an atom to read it 01201 MOVAtom fake_atom = { .size = size - (url_ftell(pb) - start_pos) }; 01202 if (format != AV_RL32("mp4s")) // mp4s contains a regular esds atom 01203 mov_read_glbl(c, pb, fake_atom); 01204 st->codec->codec_id= id; 01205 st->codec->width = sc->width; 01206 st->codec->height = sc->height; 01207 } else { 01208 /* other codec type, just skip (rtp, mp4s, tmcd ...) */ 01209 url_fskip(pb, size - (url_ftell(pb) - start_pos)); 01210 } 01211 /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */ 01212 a.size = size - (url_ftell(pb) - start_pos); 01213 if (a.size > 8) { 01214 if (mov_read_default(c, pb, a) < 0) 01215 return -1; 01216 } else if (a.size > 0) 01217 url_fskip(pb, a.size); 01218 } 01219 01220 if(st->codec->codec_type==AVMEDIA_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1) 01221 st->codec->sample_rate= sc->time_scale; 01222 01223 /* special codec parameters handling */ 01224 switch (st->codec->codec_id) { 01225 #if CONFIG_DV_DEMUXER 01226 case CODEC_ID_DVAUDIO: 01227 c->dv_fctx = avformat_alloc_context(); 01228 c->dv_demux = dv_init_demux(c->dv_fctx); 01229 if (!c->dv_demux) { 01230 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n"); 01231 return -1; 01232 } 01233 sc->dv_audio_container = 1; 01234 st->codec->codec_id = CODEC_ID_PCM_S16LE; 01235 break; 01236 #endif 01237 /* no ifdef since parameters are always those */ 01238 case CODEC_ID_QCELP: 01239 // force sample rate for qcelp when not stored in mov 01240 if (st->codec->codec_tag != MKTAG('Q','c','l','p')) 01241 st->codec->sample_rate = 8000; 01242 st->codec->frame_size= 160; 01243 st->codec->channels= 1; /* really needed */ 01244 break; 01245 case CODEC_ID_AMR_NB: 01246 case CODEC_ID_AMR_WB: 01247 st->codec->frame_size= sc->samples_per_frame; 01248 st->codec->channels= 1; /* really needed */ 01249 /* force sample rate for amr, stsd in 3gp does not store sample rate */ 01250 if (st->codec->codec_id == CODEC_ID_AMR_NB) 01251 st->codec->sample_rate = 8000; 01252 else if (st->codec->codec_id == CODEC_ID_AMR_WB) 01253 st->codec->sample_rate = 16000; 01254 break; 01255 case CODEC_ID_MP2: 01256 case CODEC_ID_MP3: 01257 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; /* force type after stsd for m1a hdlr */ 01258 st->need_parsing = AVSTREAM_PARSE_FULL; 01259 break; 01260 case CODEC_ID_GSM: 01261 case CODEC_ID_ADPCM_MS: 01262 case CODEC_ID_ADPCM_IMA_WAV: 01263 st->codec->block_align = sc->bytes_per_frame; 01264 break; 01265 case CODEC_ID_ALAC: 01266 if (st->codec->extradata_size == 36) { 01267 st->codec->frame_size = AV_RB32(st->codec->extradata+12); 01268 st->codec->channels = AV_RB8 (st->codec->extradata+21); 01269 } 01270 break; 01271 default: 01272 break; 01273 } 01274 01275 return 0; 01276 } 01277 01278 static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 01279 { 01280 AVStream *st; 01281 MOVStreamContext *sc; 01282 unsigned int i, entries; 01283 01284 if (c->fc->nb_streams < 1) 01285 return 0; 01286 st = c->fc->streams[c->fc->nb_streams-1]; 01287 sc = st->priv_data; 01288 01289 get_byte(pb); /* version */ 01290 get_be24(pb); /* flags */ 01291 01292 entries = get_be32(pb); 01293 01294 dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries); 01295 01296 if(entries >= UINT_MAX / sizeof(*sc->stsc_data)) 01297 return -1; 01298 sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data)); 01299 if (!sc->stsc_data) 01300 return AVERROR(ENOMEM); 01301 sc->stsc_count = entries; 01302 01303 for(i=0; i<entries; i++) { 01304 sc->stsc_data[i].first = get_be32(pb); 01305 sc->stsc_data[i].count = get_be32(pb); 01306 sc->stsc_data[i].id = get_be32(pb); 01307 } 01308 return 0; 01309 } 01310 01311 static int mov_read_stps(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 01312 { 01313 AVStream *st; 01314 MOVStreamContext *sc; 01315 unsigned i, entries; 01316 01317 if (c->fc->nb_streams < 1) 01318 return 0; 01319 st = c->fc->streams[c->fc->nb_streams-1]; 01320 sc = st->priv_data; 01321 01322 get_be32(pb); // version + flags 01323 01324 entries = get_be32(pb); 01325 if (entries >= UINT_MAX / sizeof(*sc->stps_data)) 01326 return -1; 01327 sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data)); 01328 if (!sc->stps_data) 01329 return AVERROR(ENOMEM); 01330 sc->stps_count = entries; 01331 01332 for (i = 0; i < entries; i++) { 01333 sc->stps_data[i] = get_be32(pb); 01334 //dprintf(c->fc, "stps %d\n", sc->stps_data[i]); 01335 } 01336 01337 return 0; 01338 } 01339 01340 static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 01341 { 01342 AVStream *st; 01343 MOVStreamContext *sc; 01344 unsigned int i, entries; 01345 01346 if (c->fc->nb_streams < 1) 01347 return 0; 01348 st = c->fc->streams[c->fc->nb_streams-1]; 01349 sc = st->priv_data; 01350 01351 get_byte(pb); /* version */ 01352 get_be24(pb); /* flags */ 01353 01354 entries = get_be32(pb); 01355 01356 dprintf(c->fc, "keyframe_count = %d\n", entries); 01357 01358 if(entries >= UINT_MAX / sizeof(int)) 01359 return -1; 01360 sc->keyframes = av_malloc(entries * sizeof(int)); 01361 if (!sc->keyframes) 01362 return AVERROR(ENOMEM); 01363 sc->keyframe_count = entries; 01364 01365 for(i=0; i<entries; i++) { 01366 sc->keyframes[i] = get_be32(pb); 01367 //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]); 01368 } 01369 return 0; 01370 } 01371 01372 static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 01373 { 01374 AVStream *st; 01375 MOVStreamContext *sc; 01376 unsigned int i, entries, sample_size, field_size, num_bytes; 01377 GetBitContext gb; 01378 unsigned char* buf; 01379 01380 if (c->fc->nb_streams < 1) 01381 return 0; 01382 st = c->fc->streams[c->fc->nb_streams-1]; 01383 sc = st->priv_data; 01384 01385 get_byte(pb); /* version */ 01386 get_be24(pb); /* flags */ 01387 01388 if (atom.type == MKTAG('s','t','s','z')) { 01389 sample_size = get_be32(pb); 01390 if (!sc->sample_size) /* do not overwrite value computed in stsd */ 01391 sc->sample_size = sample_size; 01392 field_size = 32; 01393 } else { 01394 sample_size = 0; 01395 get_be24(pb); /* reserved */ 01396 field_size = get_byte(pb); 01397 } 01398 entries = get_be32(pb); 01399 01400 dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries); 01401 01402 sc->sample_count = entries; 01403 if (sample_size) 01404 return 0; 01405 01406 if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) { 01407 av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size); 01408 return -1; 01409 } 01410 01411 if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size) 01412 return -1; 01413 sc->sample_sizes = av_malloc(entries * sizeof(int)); 01414 if (!sc->sample_sizes) 01415 return AVERROR(ENOMEM); 01416 01417 num_bytes = (entries*field_size+4)>>3; 01418 01419 buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE); 01420 if (!buf) { 01421 av_freep(&sc->sample_sizes); 01422 return AVERROR(ENOMEM); 01423 } 01424 01425 if (get_buffer(pb, buf, num_bytes) < num_bytes) { 01426 av_freep(&sc->sample_sizes); 01427 av_free(buf); 01428 return -1; 01429 } 01430 01431 init_get_bits(&gb, buf, 8*num_bytes); 01432 01433 for(i=0; i<entries; i++) 01434 sc->sample_sizes[i] = get_bits_long(&gb, field_size); 01435 01436 av_free(buf); 01437 return 0; 01438 } 01439 01440 static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 01441 { 01442 AVStream *st; 01443 MOVStreamContext *sc; 01444 unsigned int i, entries; 01445 int64_t duration=0; 01446 int64_t total_sample_count=0; 01447 01448 if (c->fc->nb_streams < 1) 01449 return 0; 01450 st = c->fc->streams[c->fc->nb_streams-1]; 01451 sc = st->priv_data; 01452 01453 get_byte(pb); /* version */ 01454 get_be24(pb); /* flags */ 01455 entries = get_be32(pb); 01456 01457 dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries); 01458 01459 if(entries >= UINT_MAX / sizeof(*sc->stts_data)) 01460 return -1; 01461 sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data)); 01462 if (!sc->stts_data) 01463 return AVERROR(ENOMEM); 01464 sc->stts_count = entries; 01465 01466 for(i=0; i<entries; i++) { 01467 int sample_duration; 01468 int sample_count; 01469 01470 sample_count=get_be32(pb); 01471 sample_duration = get_be32(pb); 01472 sc->stts_data[i].count= sample_count; 01473 sc->stts_data[i].duration= sample_duration; 01474 01475 dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration); 01476 01477 duration+=(int64_t)sample_duration*sample_count; 01478 total_sample_count+=sample_count; 01479 } 01480 01481 st->nb_frames= total_sample_count; 01482 if(duration) 01483 st->duration= duration; 01484 return 0; 01485 } 01486 01487 static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 01488 { 01489 AVStream *st; 01490 MOVStreamContext *sc; 01491 unsigned int i, entries; 01492 01493 if (c->fc->nb_streams < 1) 01494 return 0; 01495 st = c->fc->streams[c->fc->nb_streams-1]; 01496 sc = st->priv_data; 01497 01498 get_byte(pb); /* version */ 01499 get_be24(pb); /* flags */ 01500 entries = get_be32(pb); 01501 01502 dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries); 01503 01504 if(entries >= UINT_MAX / sizeof(*sc->ctts_data)) 01505 return -1; 01506 sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data)); 01507 if (!sc->ctts_data) 01508 return AVERROR(ENOMEM); 01509 sc->ctts_count = entries; 01510 01511 for(i=0; i<entries; i++) { 01512 int count =get_be32(pb); 01513 int duration =get_be32(pb); 01514 01515 sc->ctts_data[i].count = count; 01516 sc->ctts_data[i].duration= duration; 01517 if (duration < 0) 01518 sc->dts_shift = FFMAX(sc->dts_shift, -duration); 01519 } 01520 01521 dprintf(c->fc, "dts shift %d\n", sc->dts_shift); 01522 01523 return 0; 01524 } 01525 01526 static void mov_build_index(MOVContext *mov, AVStream *st) 01527 { 01528 MOVStreamContext *sc = st->priv_data; 01529 int64_t current_offset; 01530 int64_t current_dts = 0; 01531 unsigned int stts_index = 0; 01532 unsigned int stsc_index = 0; 01533 unsigned int stss_index = 0; 01534 unsigned int stps_index = 0; 01535 unsigned int i, j; 01536 uint64_t stream_size = 0; 01537 01538 /* adjust first dts according to edit list */ 01539 if (sc->time_offset) { 01540 int rescaled = sc->time_offset < 0 ? av_rescale(sc->time_offset, sc->time_scale, mov->time_scale) : sc->time_offset; 01541 current_dts = -rescaled; 01542 if (sc->ctts_data && sc->ctts_data[0].duration / sc->stts_data[0].duration > 16) { 01543 /* more than 16 frames delay, dts are likely wrong 01544 this happens with files created by iMovie */ 01545 sc->wrong_dts = 1; 01546 st->codec->has_b_frames = 1; 01547 } 01548 } 01549 01550 /* only use old uncompressed audio chunk demuxing when stts specifies it */ 01551 if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && 01552 sc->stts_count == 1 && sc->stts_data[0].duration == 1)) { 01553 unsigned int current_sample = 0; 01554 unsigned int stts_sample = 0; 01555 unsigned int sample_size; 01556 unsigned int distance = 0; 01557 int key_off = sc->keyframes && sc->keyframes[0] == 1; 01558 01559 current_dts -= sc->dts_shift; 01560 01561 if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries)) 01562 return; 01563 st->index_entries = av_malloc(sc->sample_count*sizeof(*st->index_entries)); 01564 if (!st->index_entries) 01565 return; 01566 st->index_entries_allocated_size = sc->sample_count*sizeof(*st->index_entries); 01567 01568 for (i = 0; i < sc->chunk_count; i++) { 01569 current_offset = sc->chunk_offsets[i]; 01570 if (stsc_index + 1 < sc->stsc_count && 01571 i + 1 == sc->stsc_data[stsc_index + 1].first) 01572 stsc_index++; 01573 for (j = 0; j < sc->stsc_data[stsc_index].count; j++) { 01574 int keyframe = 0; 01575 if (current_sample >= sc->sample_count) { 01576 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n"); 01577 return; 01578 } 01579 01580 if (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index]) { 01581 keyframe = 1; 01582 if (stss_index + 1 < sc->keyframe_count) 01583 stss_index++; 01584 } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) { 01585 keyframe = 1; 01586 if (stps_index + 1 < sc->stps_count) 01587 stps_index++; 01588 } 01589 if (keyframe) 01590 distance = 0; 01591 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample]; 01592 if(sc->pseudo_stream_id == -1 || 01593 sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) { 01594 AVIndexEntry *e = &st->index_entries[st->nb_index_entries++]; 01595 e->pos = current_offset; 01596 e->timestamp = current_dts; 01597 e->size = sample_size; 01598 e->min_distance = distance; 01599 e->flags = keyframe ? AVINDEX_KEYFRAME : 0; 01600 dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", " 01601 "size %d, distance %d, keyframe %d\n", st->index, current_sample, 01602 current_offset, current_dts, sample_size, distance, keyframe); 01603 } 01604 01605 current_offset += sample_size; 01606 stream_size += sample_size; 01607 current_dts += sc->stts_data[stts_index].duration; 01608 distance++; 01609 stts_sample++; 01610 current_sample++; 01611 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) { 01612 stts_sample = 0; 01613 stts_index++; 01614 } 01615 } 01616 } 01617 if (st->duration > 0) 01618 st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration; 01619 } else { 01620 unsigned chunk_samples, total = 0; 01621 01622 // compute total chunk count 01623 for (i = 0; i < sc->stsc_count; i++) { 01624 unsigned count, chunk_count; 01625 01626 chunk_samples = sc->stsc_data[i].count; 01627 if (sc->samples_per_frame && chunk_samples % sc->samples_per_frame) { 01628 av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n"); 01629 return; 01630 } 01631 01632 if (sc->samples_per_frame >= 160) { // gsm 01633 count = chunk_samples / sc->samples_per_frame; 01634 } else if (sc->samples_per_frame > 1) { 01635 unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame; 01636 count = (chunk_samples+samples-1) / samples; 01637 } else { 01638 count = (chunk_samples+1023) / 1024; 01639 } 01640 01641 if (i < sc->stsc_count - 1) 01642 chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first; 01643 else 01644 chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1); 01645 total += chunk_count * count; 01646 } 01647 01648 dprintf(mov->fc, "chunk count %d\n", total); 01649 if (total >= UINT_MAX / sizeof(*st->index_entries)) 01650 return; 01651 st->index_entries = av_malloc(total*sizeof(*st->index_entries)); 01652 if (!st->index_entries) 01653 return; 01654 st->index_entries_allocated_size = total*sizeof(*st->index_entries); 01655 01656 // populate index 01657 for (i = 0; i < sc->chunk_count; i++) { 01658 current_offset = sc->chunk_offsets[i]; 01659 if (stsc_index + 1 < sc->stsc_count && 01660 i + 1 == sc->stsc_data[stsc_index + 1].first) 01661 stsc_index++; 01662 chunk_samples = sc->stsc_data[stsc_index].count; 01663 01664 while (chunk_samples > 0) { 01665 AVIndexEntry *e; 01666 unsigned size, samples; 01667 01668 if (sc->samples_per_frame >= 160) { // gsm 01669 samples = sc->samples_per_frame; 01670 size = sc->bytes_per_frame; 01671 } else { 01672 if (sc->samples_per_frame > 1) { 01673 samples = FFMIN((1024 / sc->samples_per_frame)* 01674 sc->samples_per_frame, chunk_samples); 01675 size = (samples / sc->samples_per_frame) * sc->bytes_per_frame; 01676 } else { 01677 samples = FFMIN(1024, chunk_samples); 01678 size = samples * sc->sample_size; 01679 } 01680 } 01681 01682 if (st->nb_index_entries >= total) { 01683 av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total); 01684 return; 01685 } 01686 e = &st->index_entries[st->nb_index_entries++]; 01687 e->pos = current_offset; 01688 e->timestamp = current_dts; 01689 e->size = size; 01690 e->min_distance = 0; 01691 e->flags = AVINDEX_KEYFRAME; 01692 dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", " 01693 "size %d, duration %d\n", st->index, i, current_offset, current_dts, 01694 size, samples); 01695 01696 current_offset += size; 01697 current_dts += samples; 01698 chunk_samples -= samples; 01699 } 01700 } 01701 } 01702 } 01703 01704 static int mov_open_dref(ByteIOContext **pb, char *src, MOVDref *ref) 01705 { 01706 /* try relative path, we do not try the absolute because it can leak information about our 01707 system to an attacker */ 01708 if (ref->nlvl_to > 0 && ref->nlvl_from > 0) { 01709 char filename[1024]; 01710 char *src_path; 01711 int i, l; 01712 01713 /* find a source dir */ 01714 src_path = strrchr(src, '/'); 01715 if (src_path) 01716 src_path++; 01717 else 01718 src_path = src; 01719 01720 /* find a next level down to target */ 01721 for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--) 01722 if (ref->path[l] == '/') { 01723 if (i == ref->nlvl_to - 1) 01724 break; 01725 else 01726 i++; 01727 } 01728 01729 /* compose filename if next level down to target was found */ 01730 if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) { 01731 memcpy(filename, src, src_path - src); 01732 filename[src_path - src] = 0; 01733 01734 for (i = 1; i < ref->nlvl_from; i++) 01735 av_strlcat(filename, "../", 1024); 01736 01737 av_strlcat(filename, ref->path + l + 1, 1024); 01738 01739 if (!url_fopen(pb, filename, URL_RDONLY)) 01740 return 0; 01741 } 01742 } 01743 01744 return AVERROR(ENOENT); 01745 }; 01746 01747 static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 01748 { 01749 AVStream *st; 01750 MOVStreamContext *sc; 01751 int ret; 01752 01753 st = av_new_stream(c->fc, c->fc->nb_streams); 01754 if (!st) return AVERROR(ENOMEM); 01755 sc = av_mallocz(sizeof(MOVStreamContext)); 01756 if (!sc) return AVERROR(ENOMEM); 01757 01758 st->priv_data = sc; 01759 st->codec->codec_type = AVMEDIA_TYPE_DATA; 01760 sc->ffindex = st->index; 01761 01762 if ((ret = mov_read_default(c, pb, atom)) < 0) 01763 return ret; 01764 01765 /* sanity checks */ 01766 if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count || 01767 (!sc->sample_size && !sc->sample_count))) { 01768 av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n", 01769 st->index); 01770 return 0; 01771 } 01772 01773 if (!sc->time_scale) { 01774 av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index); 01775 sc->time_scale = c->time_scale; 01776 if (!sc->time_scale) 01777 sc->time_scale = 1; 01778 } 01779 01780 av_set_pts_info(st, 64, 1, sc->time_scale); 01781 01782 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && 01783 !st->codec->frame_size && sc->stts_count == 1) { 01784 st->codec->frame_size = av_rescale(sc->stts_data[0].duration, 01785 st->codec->sample_rate, sc->time_scale); 01786 dprintf(c->fc, "frame size %d\n", st->codec->frame_size); 01787 } 01788 01789 mov_build_index(c, st); 01790 01791 if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) { 01792 MOVDref *dref = &sc->drefs[sc->dref_id - 1]; 01793 if (mov_open_dref(&sc->pb, c->fc->filename, dref) < 0) 01794 av_log(c->fc, AV_LOG_ERROR, 01795 "stream %d, error opening alias: path='%s', dir='%s', " 01796 "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n", 01797 st->index, dref->path, dref->dir, dref->filename, 01798 dref->volume, dref->nlvl_from, dref->nlvl_to); 01799 } else 01800 sc->pb = c->fc->pb; 01801 01802 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { 01803 if (st->codec->width != sc->width || st->codec->height != sc->height) { 01804 AVRational r = av_d2q(((double)st->codec->height * sc->width) / 01805 ((double)st->codec->width * sc->height), INT_MAX); 01806 if (st->sample_aspect_ratio.num) 01807 st->sample_aspect_ratio = av_mul_q(st->sample_aspect_ratio, r); 01808 else 01809 st->sample_aspect_ratio = r; 01810 } 01811 01812 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den, 01813 sc->time_scale*st->nb_frames, st->duration, INT_MAX); 01814 } 01815 01816 switch (st->codec->codec_id) { 01817 #if CONFIG_H261_DECODER 01818 case CODEC_ID_H261: 01819 #endif 01820 #if CONFIG_H263_DECODER 01821 case CODEC_ID_H263: 01822 #endif 01823 #if CONFIG_H264_DECODER 01824 case CODEC_ID_H264: 01825 #endif 01826 #if CONFIG_MPEG4_DECODER 01827 case CODEC_ID_MPEG4: 01828 #endif 01829 st->codec->width = 0; /* let decoder init width/height */ 01830 st->codec->height= 0; 01831 break; 01832 } 01833 01834 /* Do not need those anymore. */ 01835 av_freep(&sc->chunk_offsets); 01836 av_freep(&sc->stsc_data); 01837 av_freep(&sc->sample_sizes); 01838 av_freep(&sc->keyframes); 01839 av_freep(&sc->stts_data); 01840 av_freep(&sc->stps_data); 01841 01842 return 0; 01843 } 01844 01845 static int mov_read_ilst(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 01846 { 01847 int ret; 01848 c->itunes_metadata = 1; 01849 ret = mov_read_default(c, pb, atom); 01850 c->itunes_metadata = 0; 01851 return ret; 01852 } 01853 01854 static int mov_read_meta(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 01855 { 01856 while (atom.size > 8) { 01857 uint32_t tag = get_le32(pb); 01858 atom.size -= 4; 01859 if (tag == MKTAG('h','d','l','r')) { 01860 url_fseek(pb, -8, SEEK_CUR); 01861 atom.size += 8; 01862 return mov_read_default(c, pb, atom); 01863 } 01864 } 01865 return 0; 01866 } 01867 01868 static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 01869 { 01870 int i; 01871 int width; 01872 int height; 01873 int64_t disp_transform[2]; 01874 int display_matrix[3][2]; 01875 AVStream *st; 01876 MOVStreamContext *sc; 01877 int version; 01878 01879 if (c->fc->nb_streams < 1) 01880 return 0; 01881 st = c->fc->streams[c->fc->nb_streams-1]; 01882 sc = st->priv_data; 01883 01884 version = get_byte(pb); 01885 get_be24(pb); /* flags */ 01886 /* 01887 MOV_TRACK_ENABLED 0x0001 01888 MOV_TRACK_IN_MOVIE 0x0002 01889 MOV_TRACK_IN_PREVIEW 0x0004 01890 MOV_TRACK_IN_POSTER 0x0008 01891 */ 01892 01893 if (version == 1) { 01894 get_be64(pb); 01895 get_be64(pb); 01896 } else { 01897 get_be32(pb); /* creation time */ 01898 get_be32(pb); /* modification time */ 01899 } 01900 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/ 01901 get_be32(pb); /* reserved */ 01902 01903 /* highlevel (considering edits) duration in movie timebase */ 01904 (version == 1) ? get_be64(pb) : get_be32(pb); 01905 get_be32(pb); /* reserved */ 01906 get_be32(pb); /* reserved */ 01907 01908 get_be16(pb); /* layer */ 01909 get_be16(pb); /* alternate group */ 01910 get_be16(pb); /* volume */ 01911 get_be16(pb); /* reserved */ 01912 01913 //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2) 01914 // they're kept in fixed point format through all calculations 01915 // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio 01916 for (i = 0; i < 3; i++) { 01917 display_matrix[i][0] = get_be32(pb); // 16.16 fixed point 01918 display_matrix[i][1] = get_be32(pb); // 16.16 fixed point 01919 get_be32(pb); // 2.30 fixed point (not used) 01920 } 01921 01922 width = get_be32(pb); // 16.16 fixed point track width 01923 height = get_be32(pb); // 16.16 fixed point track height 01924 sc->width = width >> 16; 01925 sc->height = height >> 16; 01926 01927 // transform the display width/height according to the matrix 01928 // skip this if the display matrix is the default identity matrix 01929 // or if it is rotating the picture, ex iPhone 3GS 01930 // to keep the same scale, use [width height 1<<16] 01931 if (width && height && 01932 ((display_matrix[0][0] != 65536 || 01933 display_matrix[1][1] != 65536) && 01934 !display_matrix[0][1] && 01935 !display_matrix[1][0] && 01936 !display_matrix[2][0] && !display_matrix[2][1])) { 01937 for (i = 0; i < 2; i++) 01938 disp_transform[i] = 01939 (int64_t) width * display_matrix[0][i] + 01940 (int64_t) height * display_matrix[1][i] + 01941 ((int64_t) display_matrix[2][i] << 16); 01942 01943 //sample aspect ratio is new width/height divided by old width/height 01944 st->sample_aspect_ratio = av_d2q( 01945 ((double) disp_transform[0] * height) / 01946 ((double) disp_transform[1] * width), INT_MAX); 01947 } 01948 return 0; 01949 } 01950 01951 static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 01952 { 01953 MOVFragment *frag = &c->fragment; 01954 MOVTrackExt *trex = NULL; 01955 int flags, track_id, i; 01956 01957 get_byte(pb); /* version */ 01958 flags = get_be24(pb); 01959 01960 track_id = get_be32(pb); 01961 if (!track_id) 01962 return -1; 01963 frag->track_id = track_id; 01964 for (i = 0; i < c->trex_count; i++) 01965 if (c->trex_data[i].track_id == frag->track_id) { 01966 trex = &c->trex_data[i]; 01967 break; 01968 } 01969 if (!trex) { 01970 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n"); 01971 return -1; 01972 } 01973 01974 if (flags & 0x01) frag->base_data_offset = get_be64(pb); 01975 else frag->base_data_offset = frag->moof_offset; 01976 if (flags & 0x02) frag->stsd_id = get_be32(pb); 01977 else frag->stsd_id = trex->stsd_id; 01978 01979 frag->duration = flags & 0x08 ? get_be32(pb) : trex->duration; 01980 frag->size = flags & 0x10 ? get_be32(pb) : trex->size; 01981 frag->flags = flags & 0x20 ? get_be32(pb) : trex->flags; 01982 dprintf(c->fc, "frag flags 0x%x\n", frag->flags); 01983 return 0; 01984 } 01985 01986 static int mov_read_chap(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 01987 { 01988 c->chapter_track = get_be32(pb); 01989 return 0; 01990 } 01991 01992 static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 01993 { 01994 MOVTrackExt *trex; 01995 01996 if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data)) 01997 return -1; 01998 trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data)); 01999 if (!trex) 02000 return AVERROR(ENOMEM); 02001 c->trex_data = trex; 02002 trex = &c->trex_data[c->trex_count++]; 02003 get_byte(pb); /* version */ 02004 get_be24(pb); /* flags */ 02005 trex->track_id = get_be32(pb); 02006 trex->stsd_id = get_be32(pb); 02007 trex->duration = get_be32(pb); 02008 trex->size = get_be32(pb); 02009 trex->flags = get_be32(pb); 02010 return 0; 02011 } 02012 02013 static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 02014 { 02015 MOVFragment *frag = &c->fragment; 02016 AVStream *st = NULL; 02017 MOVStreamContext *sc; 02018 uint64_t offset; 02019 int64_t dts; 02020 int data_offset = 0; 02021 unsigned entries, first_sample_flags = frag->flags; 02022 int flags, distance, i; 02023 02024 for (i = 0; i < c->fc->nb_streams; i++) { 02025 if (c->fc->streams[i]->id == frag->track_id) { 02026 st = c->fc->streams[i]; 02027 break; 02028 } 02029 } 02030 if (!st) { 02031 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id); 02032 return -1; 02033 } 02034 sc = st->priv_data; 02035 if (sc->pseudo_stream_id+1 != frag->stsd_id) 02036 return 0; 02037 get_byte(pb); /* version */ 02038 flags = get_be24(pb); 02039 entries = get_be32(pb); 02040 dprintf(c->fc, "flags 0x%x entries %d\n", flags, entries); 02041 if (flags & 0x001) data_offset = get_be32(pb); 02042 if (flags & 0x004) first_sample_flags = get_be32(pb); 02043 if (flags & 0x800) { 02044 MOVStts *ctts_data; 02045 if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data)) 02046 return -1; 02047 ctts_data = av_realloc(sc->ctts_data, 02048 (entries+sc->ctts_count)*sizeof(*sc->ctts_data)); 02049 if (!ctts_data) 02050 return AVERROR(ENOMEM); 02051 sc->ctts_data = ctts_data; 02052 } 02053 dts = st->duration; 02054 offset = frag->base_data_offset + data_offset; 02055 distance = 0; 02056 dprintf(c->fc, "first sample flags 0x%x\n", first_sample_flags); 02057 for (i = 0; i < entries; i++) { 02058 unsigned sample_size = frag->size; 02059 int sample_flags = i ? frag->flags : first_sample_flags; 02060 unsigned sample_duration = frag->duration; 02061 int keyframe; 02062 02063 if (flags & 0x100) sample_duration = get_be32(pb); 02064 if (flags & 0x200) sample_size = get_be32(pb); 02065 if (flags & 0x400) sample_flags = get_be32(pb); 02066 if (flags & 0x800) { 02067 sc->ctts_data[sc->ctts_count].count = 1; 02068 sc->ctts_data[sc->ctts_count].duration = get_be32(pb); 02069 sc->ctts_count++; 02070 } 02071 if ((keyframe = st->codec->codec_type == AVMEDIA_TYPE_AUDIO || 02072 (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000)) 02073 distance = 0; 02074 av_add_index_entry(st, offset, dts, sample_size, distance, 02075 keyframe ? AVINDEX_KEYFRAME : 0); 02076 dprintf(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", " 02077 "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i, 02078 offset, dts, sample_size, distance, keyframe); 02079 distance++; 02080 dts += sample_duration; 02081 offset += sample_size; 02082 } 02083 frag->moof_offset = offset; 02084 st->duration = dts; 02085 return 0; 02086 } 02087 02088 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */ 02089 /* like the files created with Adobe Premiere 5.0, for samples see */ 02090 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */ 02091 static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 02092 { 02093 int err; 02094 02095 if (atom.size < 8) 02096 return 0; /* continue */ 02097 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */ 02098 url_fskip(pb, atom.size - 4); 02099 return 0; 02100 } 02101 atom.type = get_le32(pb); 02102 atom.size -= 8; 02103 if (atom.type != MKTAG('m','d','a','t')) { 02104 url_fskip(pb, atom.size); 02105 return 0; 02106 } 02107 err = mov_read_mdat(c, pb, atom); 02108 return err; 02109 } 02110 02111 static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 02112 { 02113 #if CONFIG_ZLIB 02114 ByteIOContext ctx; 02115 uint8_t *cmov_data; 02116 uint8_t *moov_data; /* uncompressed data */ 02117 long cmov_len, moov_len; 02118 int ret = -1; 02119 02120 get_be32(pb); /* dcom atom */ 02121 if (get_le32(pb) != MKTAG('d','c','o','m')) 02122 return -1; 02123 if (get_le32(pb) != MKTAG('z','l','i','b')) { 02124 av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !"); 02125 return -1; 02126 } 02127 get_be32(pb); /* cmvd atom */ 02128 if (get_le32(pb) != MKTAG('c','m','v','d')) 02129 return -1; 02130 moov_len = get_be32(pb); /* uncompressed size */ 02131 cmov_len = atom.size - 6 * 4; 02132 02133 cmov_data = av_malloc(cmov_len); 02134 if (!cmov_data) 02135 return AVERROR(ENOMEM); 02136 moov_data = av_malloc(moov_len); 02137 if (!moov_data) { 02138 av_free(cmov_data); 02139 return AVERROR(ENOMEM); 02140 } 02141 get_buffer(pb, cmov_data, cmov_len); 02142 if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK) 02143 goto free_and_return; 02144 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0) 02145 goto free_and_return; 02146 atom.type = MKTAG('m','o','o','v'); 02147 atom.size = moov_len; 02148 #ifdef DEBUG 02149 // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); } 02150 #endif 02151 ret = mov_read_default(c, &ctx, atom); 02152 free_and_return: 02153 av_free(moov_data); 02154 av_free(cmov_data); 02155 return ret; 02156 #else 02157 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n"); 02158 return -1; 02159 #endif 02160 } 02161 02162 /* edit list atom */ 02163 static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 02164 { 02165 MOVStreamContext *sc; 02166 int i, edit_count; 02167 02168 if (c->fc->nb_streams < 1) 02169 return 0; 02170 sc = c->fc->streams[c->fc->nb_streams-1]->priv_data; 02171 02172 get_byte(pb); /* version */ 02173 get_be24(pb); /* flags */ 02174 edit_count = get_be32(pb); /* entries */ 02175 02176 if((uint64_t)edit_count*12+8 > atom.size) 02177 return -1; 02178 02179 for(i=0; i<edit_count; i++){ 02180 int time; 02181 int duration = get_be32(pb); /* Track duration */ 02182 time = get_be32(pb); /* Media time */ 02183 get_be32(pb); /* Media rate */ 02184 if (i == 0 && time >= -1) { 02185 sc->time_offset = time != -1 ? time : -duration; 02186 } 02187 } 02188 02189 if(edit_count > 1) 02190 av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, " 02191 "a/v desync might occur, patch welcome\n"); 02192 02193 dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count); 02194 return 0; 02195 } 02196 02197 static const MOVParseTableEntry mov_default_parse_table[] = { 02198 { MKTAG('a','v','s','s'), mov_read_extradata }, 02199 { MKTAG('c','h','p','l'), mov_read_chpl }, 02200 { MKTAG('c','o','6','4'), mov_read_stco }, 02201 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */ 02202 { MKTAG('d','i','n','f'), mov_read_default }, 02203 { MKTAG('d','r','e','f'), mov_read_dref }, 02204 { MKTAG('e','d','t','s'), mov_read_default }, 02205 { MKTAG('e','l','s','t'), mov_read_elst }, 02206 { MKTAG('e','n','d','a'), mov_read_enda }, 02207 { MKTAG('f','i','e','l'), mov_read_extradata }, 02208 { MKTAG('f','t','y','p'), mov_read_ftyp }, 02209 { MKTAG('g','l','b','l'), mov_read_glbl }, 02210 { MKTAG('h','d','l','r'), mov_read_hdlr }, 02211 { MKTAG('i','l','s','t'), mov_read_ilst }, 02212 { MKTAG('j','p','2','h'), mov_read_extradata }, 02213 { MKTAG('m','d','a','t'), mov_read_mdat }, 02214 { MKTAG('m','d','h','d'), mov_read_mdhd }, 02215 { MKTAG('m','d','i','a'), mov_read_default }, 02216 { MKTAG('m','e','t','a'), mov_read_meta }, 02217 { MKTAG('m','i','n','f'), mov_read_default }, 02218 { MKTAG('m','o','o','f'), mov_read_moof }, 02219 { MKTAG('m','o','o','v'), mov_read_moov }, 02220 { MKTAG('m','v','e','x'), mov_read_default }, 02221 { MKTAG('m','v','h','d'), mov_read_mvhd }, 02222 { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */ 02223 { MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */ 02224 { MKTAG('a','v','c','C'), mov_read_glbl }, 02225 { MKTAG('p','a','s','p'), mov_read_pasp }, 02226 { MKTAG('s','t','b','l'), mov_read_default }, 02227 { MKTAG('s','t','c','o'), mov_read_stco }, 02228 { MKTAG('s','t','p','s'), mov_read_stps }, 02229 { MKTAG('s','t','r','f'), mov_read_strf }, 02230 { MKTAG('s','t','s','c'), mov_read_stsc }, 02231 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */ 02232 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */ 02233 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */ 02234 { MKTAG('s','t','t','s'), mov_read_stts }, 02235 { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */ 02236 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */ 02237 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */ 02238 { MKTAG('t','r','a','k'), mov_read_trak }, 02239 { MKTAG('t','r','a','f'), mov_read_default }, 02240 { MKTAG('t','r','e','f'), mov_read_default }, 02241 { MKTAG('c','h','a','p'), mov_read_chap }, 02242 { MKTAG('t','r','e','x'), mov_read_trex }, 02243 { MKTAG('t','r','u','n'), mov_read_trun }, 02244 { MKTAG('u','d','t','a'), mov_read_default }, 02245 { MKTAG('w','a','v','e'), mov_read_wave }, 02246 { MKTAG('e','s','d','s'), mov_read_esds }, 02247 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */ 02248 { MKTAG('c','m','o','v'), mov_read_cmov }, 02249 { 0, NULL } 02250 }; 02251 02252 static int mov_probe(AVProbeData *p) 02253 { 02254 unsigned int offset; 02255 uint32_t tag; 02256 int score = 0; 02257 02258 /* check file header */ 02259 offset = 0; 02260 for(;;) { 02261 /* ignore invalid offset */ 02262 if ((offset + 8) > (unsigned int)p->buf_size) 02263 return score; 02264 tag = AV_RL32(p->buf + offset + 4); 02265 switch(tag) { 02266 /* check for obvious tags */ 02267 case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */ 02268 case MKTAG('m','o','o','v'): 02269 case MKTAG('m','d','a','t'): 02270 case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */ 02271 case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */ 02272 case MKTAG('f','t','y','p'): 02273 return AVPROBE_SCORE_MAX; 02274 /* those are more common words, so rate then a bit less */ 02275 case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */ 02276 case MKTAG('w','i','d','e'): 02277 case MKTAG('f','r','e','e'): 02278 case MKTAG('j','u','n','k'): 02279 case MKTAG('p','i','c','t'): 02280 return AVPROBE_SCORE_MAX - 5; 02281 case MKTAG(0x82,0x82,0x7f,0x7d): 02282 case MKTAG('s','k','i','p'): 02283 case MKTAG('u','u','i','d'): 02284 case MKTAG('p','r','f','l'): 02285 offset = AV_RB32(p->buf+offset) + offset; 02286 /* if we only find those cause probedata is too small at least rate them */ 02287 score = AVPROBE_SCORE_MAX - 50; 02288 break; 02289 default: 02290 /* unrecognized tag */ 02291 return score; 02292 } 02293 } 02294 return score; 02295 } 02296 02297 // must be done after parsing all trak because there's no order requirement 02298 static void mov_read_chapters(AVFormatContext *s) 02299 { 02300 MOVContext *mov = s->priv_data; 02301 AVStream *st = NULL; 02302 MOVStreamContext *sc; 02303 int64_t cur_pos; 02304 uint8_t *title = NULL; 02305 int i, len, i8, i16; 02306 02307 for (i = 0; i < s->nb_streams; i++) 02308 if (s->streams[i]->id == mov->chapter_track) { 02309 st = s->streams[i]; 02310 break; 02311 } 02312 if (!st) { 02313 av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n"); 02314 return; 02315 } 02316 02317 st->discard = AVDISCARD_ALL; 02318 sc = st->priv_data; 02319 cur_pos = url_ftell(sc->pb); 02320 02321 for (i = 0; i < st->nb_index_entries; i++) { 02322 AVIndexEntry *sample = &st->index_entries[i]; 02323 int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration; 02324 02325 if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) { 02326 av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i); 02327 goto finish; 02328 } 02329 02330 title = av_malloc(sample->size+2); 02331 get_buffer(sc->pb, title, sample->size); 02332 02333 // the first two bytes are the length of the title 02334 len = AV_RB16(title); 02335 if (len > sample->size-2) 02336 continue; 02337 02338 // The samples could theoretically be in any encoding if there's an encd 02339 // atom following, but in practice are only utf-8 or utf-16, distinguished 02340 // instead by the presence of a BOM 02341 if (AV_RB16(title+2) == 0xfeff) { 02342 uint8_t *utf8 = av_malloc(2*len+3); 02343 02344 i8 = i16 = 0; 02345 while (i16 < len) { 02346 uint32_t ch; 02347 uint8_t tmp; 02348 GET_UTF16(ch, i16 < len ? AV_RB16(title + (i16+=2)) : 0, break;) 02349 PUT_UTF8(ch, tmp, if (i8 < 2*len) utf8[2+i8++] = tmp;) 02350 } 02351 utf8[2+i8] = 0; 02352 av_freep(&title); 02353 title = utf8; 02354 } 02355 02356 ff_new_chapter(s, i, st->time_base, sample->timestamp, end, title+2); 02357 av_freep(&title); 02358 } 02359 finish: 02360 av_free(title); 02361 url_fseek(sc->pb, cur_pos, SEEK_SET); 02362 } 02363 02364 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap) 02365 { 02366 MOVContext *mov = s->priv_data; 02367 ByteIOContext *pb = s->pb; 02368 int err; 02369 MOVAtom atom = { 0 }; 02370 02371 mov->fc = s; 02372 /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */ 02373 if(!url_is_streamed(pb)) 02374 atom.size = url_fsize(pb); 02375 else 02376 atom.size = INT64_MAX; 02377 02378 /* check MOV header */ 02379 if ((err = mov_read_default(mov, pb, atom)) < 0) { 02380 av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err); 02381 return err; 02382 } 02383 if (!mov->found_moov) { 02384 av_log(s, AV_LOG_ERROR, "moov atom not found\n"); 02385 return -1; 02386 } 02387 dprintf(mov->fc, "on_parse_exit_offset=%lld\n", url_ftell(pb)); 02388 02389 if (!url_is_streamed(pb) && mov->chapter_track > 0) 02390 mov_read_chapters(s); 02391 02392 return 0; 02393 } 02394 02395 static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st) 02396 { 02397 AVIndexEntry *sample = NULL; 02398 int64_t best_dts = INT64_MAX; 02399 int i; 02400 for (i = 0; i < s->nb_streams; i++) { 02401 AVStream *avst = s->streams[i]; 02402 MOVStreamContext *msc = avst->priv_data; 02403 if (msc->pb && msc->current_sample < avst->nb_index_entries) { 02404 AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample]; 02405 int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale); 02406 dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts); 02407 if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) || 02408 (!url_is_streamed(s->pb) && 02409 ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb && 02410 ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) || 02411 (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) { 02412 sample = current_sample; 02413 best_dts = dts; 02414 *st = avst; 02415 } 02416 } 02417 } 02418 return sample; 02419 } 02420 02421 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) 02422 { 02423 MOVContext *mov = s->priv_data; 02424 MOVStreamContext *sc; 02425 AVIndexEntry *sample; 02426 AVStream *st = NULL; 02427 int ret; 02428 retry: 02429 sample = mov_find_next_sample(s, &st); 02430 if (!sample) { 02431 mov->found_mdat = 0; 02432 if (!url_is_streamed(s->pb) || 02433 mov_read_default(mov, s->pb, (MOVAtom){ 0, INT64_MAX }) < 0 || 02434 url_feof(s->pb)) 02435 return AVERROR_EOF; 02436 dprintf(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb)); 02437 goto retry; 02438 } 02439 sc = st->priv_data; 02440 /* must be done just before reading, to avoid infinite loop on sample */ 02441 sc->current_sample++; 02442 02443 if (st->discard != AVDISCARD_ALL) { 02444 if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) { 02445 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", 02446 sc->ffindex, sample->pos); 02447 return -1; 02448 } 02449 ret = av_get_packet(sc->pb, pkt, sample->size); 02450 if (ret < 0) 02451 return ret; 02452 #if CONFIG_DV_DEMUXER 02453 if (mov->dv_demux && sc->dv_audio_container) { 02454 dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size); 02455 av_free(pkt->data); 02456 pkt->size = 0; 02457 ret = dv_get_packet(mov->dv_demux, pkt); 02458 if (ret < 0) 02459 return ret; 02460 } 02461 #endif 02462 } 02463 02464 pkt->stream_index = sc->ffindex; 02465 pkt->dts = sample->timestamp; 02466 if (sc->ctts_data) { 02467 pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration; 02468 /* update ctts context */ 02469 sc->ctts_sample++; 02470 if (sc->ctts_index < sc->ctts_count && 02471 sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) { 02472 sc->ctts_index++; 02473 sc->ctts_sample = 0; 02474 } 02475 if (sc->wrong_dts) 02476 pkt->dts = AV_NOPTS_VALUE; 02477 } else { 02478 int64_t next_dts = (sc->current_sample < st->nb_index_entries) ? 02479 st->index_entries[sc->current_sample].timestamp : st->duration; 02480 pkt->duration = next_dts - pkt->dts; 02481 pkt->pts = pkt->dts; 02482 } 02483 if (st->discard == AVDISCARD_ALL) 02484 goto retry; 02485 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0; 02486 pkt->pos = sample->pos; 02487 dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n", 02488 pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration); 02489 return 0; 02490 } 02491 02492 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags) 02493 { 02494 MOVStreamContext *sc = st->priv_data; 02495 int sample, time_sample; 02496 int i; 02497 02498 sample = av_index_search_timestamp(st, timestamp, flags); 02499 dprintf(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample); 02500 if (sample < 0) /* not sure what to do */ 02501 return -1; 02502 sc->current_sample = sample; 02503 dprintf(s, "stream %d, found sample %d\n", st->index, sc->current_sample); 02504 /* adjust ctts index */ 02505 if (sc->ctts_data) { 02506 time_sample = 0; 02507 for (i = 0; i < sc->ctts_count; i++) { 02508 int next = time_sample + sc->ctts_data[i].count; 02509 if (next > sc->current_sample) { 02510 sc->ctts_index = i; 02511 sc->ctts_sample = sc->current_sample - time_sample; 02512 break; 02513 } 02514 time_sample = next; 02515 } 02516 } 02517 return sample; 02518 } 02519 02520 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags) 02521 { 02522 AVStream *st; 02523 int64_t seek_timestamp, timestamp; 02524 int sample; 02525 int i; 02526 02527 if (stream_index >= s->nb_streams) 02528 return -1; 02529 if (sample_time < 0) 02530 sample_time = 0; 02531 02532 st = s->streams[stream_index]; 02533 sample = mov_seek_stream(s, st, sample_time, flags); 02534 if (sample < 0) 02535 return -1; 02536 02537 /* adjust seek timestamp to found sample timestamp */ 02538 seek_timestamp = st->index_entries[sample].timestamp; 02539 02540 for (i = 0; i < s->nb_streams; i++) { 02541 st = s->streams[i]; 02542 if (stream_index == i) 02543 continue; 02544 02545 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base); 02546 mov_seek_stream(s, st, timestamp, flags); 02547 } 02548 return 0; 02549 } 02550 02551 static int mov_read_close(AVFormatContext *s) 02552 { 02553 MOVContext *mov = s->priv_data; 02554 int i, j; 02555 02556 for (i = 0; i < s->nb_streams; i++) { 02557 AVStream *st = s->streams[i]; 02558 MOVStreamContext *sc = st->priv_data; 02559 02560 av_freep(&sc->ctts_data); 02561 for (j = 0; j < sc->drefs_count; j++) { 02562 av_freep(&sc->drefs[j].path); 02563 av_freep(&sc->drefs[j].dir); 02564 } 02565 av_freep(&sc->drefs); 02566 if (sc->pb && sc->pb != s->pb) 02567 url_fclose(sc->pb); 02568 02569 av_freep(&st->codec->palctrl); 02570 } 02571 02572 if (mov->dv_demux) { 02573 for(i = 0; i < mov->dv_fctx->nb_streams; i++) { 02574 av_freep(&mov->dv_fctx->streams[i]->codec); 02575 av_freep(&mov->dv_fctx->streams[i]); 02576 } 02577 av_freep(&mov->dv_fctx); 02578 av_freep(&mov->dv_demux); 02579 } 02580 02581 av_freep(&mov->trex_data); 02582 02583 return 0; 02584 } 02585 02586 AVInputFormat mov_demuxer = { 02587 "mov,mp4,m4a,3gp,3g2,mj2", 02588 NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"), 02589 sizeof(MOVContext), 02590 mov_probe, 02591 mov_read_header, 02592 mov_read_packet, 02593 mov_read_close, 02594 mov_read_seek, 02595 };