X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fmov.c;h=f8b2d8d0cbf941314f92f518efc25a44d21f1b04;hb=59b4b540148e6f7ff1f82983f9eb1aeec3dec359;hp=e6ac071e4f34e0cedeb133d2c89b11570aa6992a;hpb=f05783c2db8662a38fa0a99d5b655c8781aacb49;p=ffmpeg diff --git a/libavformat/mov.c b/libavformat/mov.c index e6ac071e4f3..f8b2d8d0cbf 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -33,6 +33,7 @@ #include "isom.h" #include "libavcodec/mpeg4audio.h" #include "libavcodec/mpegaudiodata.h" +#include "libavcodec/get_bits.h" #if CONFIG_ZLIB #include @@ -92,6 +93,43 @@ static int mov_metadata_trkn(MOVContext *c, ByteIOContext *pb, unsigned len) return 0; } +static const uint32_t mac_to_unicode[128] = { + 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1, + 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8, + 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3, + 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC, + 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF, + 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8, + 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211, + 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8, + 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB, + 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153, + 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA, + 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02, + 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1, + 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4, + 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC, + 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7, +}; + +static int mov_read_mac_string(MOVContext *c, ByteIOContext *pb, int len, + char *dst, int dstlen) +{ + char *p = dst; + char *end = dst+dstlen-1; + int i; + + for (i = 0; i < len; i++) { + uint8_t t, c = get_byte(pb); + if (c < 0x80 && p < end) + *p++ = c; + else + PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;); + } + *p = 0; + return p - dst; +} + static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { #ifdef MOV_EXPORT_ALL_METADATA @@ -99,22 +137,29 @@ static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom) #endif char str[1024], key2[16], language[4] = {0}; const char *key = NULL; - uint16_t str_size; + uint16_t str_size, langcode = 0; + uint32_t data_type = 0; int (*parse)(MOVContext*, ByteIOContext*, unsigned) = NULL; switch (atom.type) { case MKTAG(0xa9,'n','a','m'): key = "title"; break; case MKTAG(0xa9,'a','u','t'): - case MKTAG(0xa9,'A','R','T'): - case MKTAG(0xa9,'w','r','t'): key = "author"; break; + case MKTAG(0xa9,'A','R','T'): key = "author"; break; + case MKTAG(0xa9,'w','r','t'): key = "composer"; break; + case MKTAG( 'c','p','r','t'): case MKTAG(0xa9,'c','p','y'): key = "copyright"; break; case MKTAG(0xa9,'c','m','t'): case MKTAG(0xa9,'i','n','f'): key = "comment"; break; case MKTAG(0xa9,'a','l','b'): key = "album"; break; - case MKTAG(0xa9,'d','a','y'): key = "year"; break; + case MKTAG(0xa9,'d','a','y'): key = "date"; break; case MKTAG(0xa9,'g','e','n'): key = "genre"; break; case MKTAG(0xa9,'t','o','o'): - case MKTAG(0xa9,'e','n','c'): key = "muxer"; break; + case MKTAG(0xa9,'e','n','c'): key = "encoder"; break; + case MKTAG( 'd','e','s','c'): key = "description";break; + case MKTAG( 'l','d','e','s'): key = "synopsis"; break; + case MKTAG( 't','v','s','h'): key = "show"; break; + case MKTAG( 't','v','e','n'): key = "episode_id";break; + case MKTAG( 't','v','n','n'): key = "network"; break; case MKTAG( 't','r','k','n'): key = "track"; parse = mov_metadata_trkn; break; } @@ -123,14 +168,15 @@ static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom) int data_size = get_be32(pb); int tag = get_le32(pb); if (tag == MKTAG('d','a','t','a')) { - get_be32(pb); // type + data_type = get_be32(pb); // type get_be32(pb); // unknown str_size = data_size - 16; atom.size -= 16; } else return 0; } else if (atom.size > 4 && key && !c->itunes_metadata) { str_size = get_be16(pb); // string length - ff_mov_lang_to_iso639(get_be16(pb), language); + langcode = get_be16(pb); + ff_mov_lang_to_iso639(langcode, language); atom.size -= 4; } else str_size = atom.size; @@ -152,8 +198,12 @@ static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom) if (parse) parse(c, pb, str_size); else { - get_buffer(pb, str, str_size); - str[str_size] = 0; + if (data_type == 3 || (data_type == 0 && langcode < 0x800)) { // MAC Encoded + mov_read_mac_string(c, pb, str_size, str, sizeof(str)); + } else { + get_buffer(pb, str, str_size); + str[str_size] = 0; + } av_metadata_set(&c->fc->metadata, key, str); if (*language && strcmp(language, "und")) { snprintf(key2, sizeof(key2), "%s-%s", key, language); @@ -174,13 +224,10 @@ static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOVAtom atom) int64_t total_size = 0; MOVAtom a; int i; - int err = 0; - - a.offset = atom.offset; if (atom.size < 0) atom.size = INT64_MAX; - while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) { + while (total_size + 8 < atom.size && !url_feof(pb)) { int (*parse)(MOVContext*, ByteIOContext*, MOVAtom) = NULL; a.size = atom.size; a.type=0; @@ -189,12 +236,10 @@ static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOVAtom atom) a.type = get_le32(pb); } total_size += 8; - a.offset += 8; dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n", a.type, (char*)&a.type, a.size, atom.size, total_size); if (a.size == 1) { /* 64 bit extended size */ a.size = get_be64(pb) - 8; - a.offset += 8; total_size += 8; } if (a.size == 0) { @@ -223,30 +268,37 @@ static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOVAtom atom) } else { int64_t start_pos = url_ftell(pb); int64_t left; - err = parse(c, pb, a); - if (url_is_streamed(pb) && c->found_moov && c->found_mdat) - break; + int err = parse(c, pb, a); + if (err < 0) + return err; + if (c->found_moov && c->found_mdat && + (url_is_streamed(pb) || start_pos + a.size == url_fsize(pb))) + return 0; left = a.size - url_ftell(pb) + start_pos; if (left > 0) /* skip garbage at atom end */ url_fskip(pb, left); } - a.offset += a.size; total_size += a.size; } - if (!err && total_size < atom.size && atom.size < 0x7ffff) + if (total_size < atom.size && atom.size < 0x7ffff) url_fskip(pb, atom.size - total_size); - return err; + return 0; } static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; - MOVStreamContext *sc = st->priv_data; + AVStream *st; + MOVStreamContext *sc; int entries, i, j; + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + sc = st->priv_data; + get_be32(pb); // version + flags entries = get_be32(pb); if (entries >= UINT_MAX / sizeof(*sc->drefs)) @@ -268,18 +320,33 @@ static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom) if (dref->type == MKTAG('a','l','i','s') && size > 150) { /* macintosh alias record */ uint16_t volume_len, len; - char volume[28]; int16_t type; url_fskip(pb, 10); volume_len = get_byte(pb); volume_len = FFMIN(volume_len, 27); - get_buffer(pb, volume, 27); - volume[volume_len] = 0; - av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", volume, volume_len); + get_buffer(pb, dref->volume, 27); + dref->volume[volume_len] = 0; + av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len); + + url_fskip(pb, 12); + + len = get_byte(pb); + len = FFMIN(len, 63); + get_buffer(pb, dref->filename, 63); + dref->filename[len] = 0; + av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len); + + url_fskip(pb, 16); - url_fskip(pb, 112); + /* read next level up_from_alias/down_to_target */ + dref->nlvl_from = get_be16(pb); + dref->nlvl_to = get_be16(pb); + av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n", + dref->nlvl_from, dref->nlvl_to); + + url_fskip(pb, 16); for (type = 0; type != -1 && url_ftell(pb) < next; ) { type = get_be16(pb); @@ -293,7 +360,7 @@ static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom) if (!dref->path) return AVERROR(ENOMEM); get_buffer(pb, dref->path, len); - if (len > volume_len && !strncmp(dref->path, volume, volume_len)) { + if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) { len -= volume_len; memmove(dref->path, dref->path+volume_len, len); dref->path[len] = 0; @@ -302,6 +369,17 @@ static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom) if (dref->path[j] == ':') dref->path[j] = '/'; av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path); + } else if (type == 0) { // directory name + av_free(dref->dir); + dref->dir = av_malloc(len+1); + if (!dref->dir) + return AVERROR(ENOMEM); + get_buffer(pb, dref->dir, len); + dref->dir[len] = 0; + for (j = 0; j < len; j++) + if (dref->dir[j] == ':') + dref->dir[j] = '/'; + av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir); } else url_fskip(pb, len); } @@ -345,14 +423,10 @@ static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOVAtom atom) get_be32(pb); /* component flags */ get_be32(pb); /* component flags mask */ - if(atom.size <= 24) - return 0; /* nothing left to read */ - - url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset)); return 0; } -static int mp4_read_descr_len(ByteIOContext *pb) +int ff_mp4_read_descr_len(ByteIOContext *pb) { int len = 0; int count = 4; @@ -365,12 +439,12 @@ static int mp4_read_descr_len(ByteIOContext *pb) return len; } -static int mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag) +static int mp4_read_descr(AVFormatContext *fc, ByteIOContext *pb, int *tag) { int len; *tag = get_byte(pb); - len = mp4_read_descr_len(pb); - dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len); + len = ff_mp4_read_descr_len(pb); + dprintf(fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len); return len; } @@ -379,27 +453,32 @@ static int mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag) #define MP4DecSpecificDescrTag 0x05 static const AVCodecTag mp4_audio_types[] = { - { CODEC_ID_MP3ON4, 29 }, /* old mp3on4 draft */ - { CODEC_ID_MP3ON4, 32 }, /* layer 1 */ - { CODEC_ID_MP3ON4, 33 }, /* layer 2 */ - { CODEC_ID_MP3ON4, 34 }, /* layer 3 */ - { CODEC_ID_NONE, 0 }, + { CODEC_ID_MP3ON4, AOT_PS }, /* old mp3on4 draft */ + { CODEC_ID_MP3ON4, AOT_L1 }, /* layer 1 */ + { CODEC_ID_MP3ON4, AOT_L2 }, /* layer 2 */ + { CODEC_ID_MP3ON4, AOT_L3 }, /* layer 3 */ + { CODEC_ID_MP4ALS, AOT_ALS }, /* MPEG-4 ALS */ + { CODEC_ID_NONE, AOT_NULL }, }; -static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom) +int ff_mov_read_esds(AVFormatContext *fc, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; + AVStream *st; int tag, len; + if (fc->nb_streams < 1) + return 0; + st = fc->streams[fc->nb_streams-1]; + get_be32(pb); /* version + flags */ - len = mp4_read_descr(c, pb, &tag); + len = mp4_read_descr(fc, pb, &tag); if (tag == MP4ESDescrTag) { get_be16(pb); /* ID */ get_byte(pb); /* priority */ } else get_be16(pb); /* ID */ - len = mp4_read_descr(c, pb, &tag); + len = mp4_read_descr(fc, pb, &tag); if (tag == MP4DecConfigDescrTag) { int object_type_id = get_byte(pb); get_byte(pb); /* stream type */ @@ -407,11 +486,11 @@ static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom) get_be32(pb); /* max bitrate */ get_be32(pb); /* avg bitrate */ - st->codec->codec_id= codec_get_id(ff_mp4_obj_type, object_type_id); - dprintf(c->fc, "esds object type id %d\n", object_type_id); - len = mp4_read_descr(c, pb, &tag); + st->codec->codec_id= ff_codec_get_id(ff_mp4_obj_type, object_type_id); + dprintf(fc, "esds object type id 0x%02x\n", object_type_id); + len = mp4_read_descr(fc, pb, &tag); if (tag == MP4DecSpecificDescrTag) { - dprintf(c->fc, "Specific MPEG4 header len=%d\n", len); + dprintf(fc, "Specific MPEG4 header len=%d\n", len); if((uint64_t)len > (1<<30)) return -1; st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE); @@ -423,19 +502,17 @@ static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom) MPEG4AudioConfig cfg; ff_mpeg4audio_get_config(&cfg, st->codec->extradata, st->codec->extradata_size); - if (cfg.chan_config > 7) - return -1; - st->codec->channels = ff_mpeg4audio_channels[cfg.chan_config]; + st->codec->channels = cfg.channels; if (cfg.object_type == 29 && cfg.sampling_index < 3) // old mp3on4 st->codec->sample_rate = ff_mpa_freq_tab[cfg.sampling_index]; else st->codec->sample_rate = cfg.sample_rate; // ext sample rate ? - dprintf(c->fc, "mp4a config channels %d obj %d ext obj %d " + dprintf(fc, "mp4a config channels %d obj %d ext obj %d " "sample rate %d ext sample rate %d\n", st->codec->channels, cfg.object_type, cfg.ext_object_type, cfg.sample_rate, cfg.ext_sample_rate); - if (!(st->codec->codec_id = codec_get_id(mp4_audio_types, - cfg.object_type))) + if (!(st->codec->codec_id = ff_codec_get_id(mp4_audio_types, + cfg.object_type))) st->codec->codec_id = CODEC_ID_AAC; } } @@ -443,11 +520,21 @@ static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom) return 0; } +static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom) +{ + return ff_mov_read_esds(c->fc, pb, atom); +} + static int mov_read_pasp(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { const int num = get_be32(pb); const int den = get_be32(pb); - AVStream * const st = c->fc->streams[c->fc->nb_streams-1]; + AVStream *st; + + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + if (den != 0) { if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) @@ -469,15 +556,35 @@ static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOVAtom atom) return 0; /* now go for moov */ } +/* read major brand, minor version and compatible brands and store them as metadata */ static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - uint32_t type = get_le32(pb); - - if (type != MKTAG('q','t',' ',' ')) + uint32_t minor_ver; + int comp_brand_size; + char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */ + char* comp_brands_str; + uint8_t type[5] = {0}; + + get_buffer(pb, type, 4); + if (strcmp(type, "qt ")) c->isom = 1; av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type); - get_be32(pb); /* minor version */ - url_fskip(pb, atom.size - 8); + av_metadata_set(&c->fc->metadata, "major_brand", type); + minor_ver = get_be32(pb); /* minor version */ + snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver); + av_metadata_set(&c->fc->metadata, "minor_version", minor_ver_str); + + comp_brand_size = atom.size - 8; + if (comp_brand_size < 0) + return -1; + comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */ + if (!comp_brands_str) + return AVERROR(ENOMEM); + get_buffer(pb, comp_brands_str, comp_brand_size); + comp_brands_str[comp_brand_size] = 0; + av_metadata_set(&c->fc->metadata, "compatible_brands", comp_brands_str); + av_freep(&comp_brands_str); + return 0; } @@ -501,12 +608,18 @@ static int mov_read_moof(MOVContext *c, ByteIOContext *pb, MOVAtom atom) static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; - MOVStreamContext *sc = st->priv_data; - int version = get_byte(pb); + AVStream *st; + MOVStreamContext *sc; + int version; char language[4] = {0}; unsigned lang; + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + sc = st->priv_data; + + version = get_byte(pb); if (version > 1) return -1; /* unsupported */ @@ -568,7 +681,11 @@ static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; + AVStream *st; + + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; if((uint64_t)atom.size > (1<<30)) return -1; @@ -588,9 +705,14 @@ static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOVAtom atom) static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; - int little_endian = get_be16(pb); + AVStream *st; + int little_endian; + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + + little_endian = get_be16(pb); dprintf(c->fc, "enda %d\n", little_endian); if (little_endian == 1) { switch (st->codec->codec_id) { @@ -640,7 +762,11 @@ static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOVAtom atom) static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; + AVStream *st; + + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; if((uint64_t)atom.size > (1<<30)) return -1; @@ -667,7 +793,11 @@ static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOVAtom atom) */ static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; + AVStream *st; + + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; if((uint64_t)atom.size > (1<<30)) return -1; @@ -683,10 +813,15 @@ static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOVAtom atom) static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; - MOVStreamContext *sc = st->priv_data; + AVStream *st; + MOVStreamContext *sc; unsigned int i, entries; + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + sc = st->priv_data; + get_byte(pb); /* version */ get_be24(pb); /* flags */ @@ -716,7 +851,7 @@ static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOVAtom atom) * Compute codec id for 'lpcm' tag. * See CoreAudioTypes and AudioStreamBasicDescription at Apple. */ -static enum CodecID mov_get_lpcm_codec_id(int bps, int flags) +enum CodecID ff_mov_get_lpcm_codec_id(int bps, int flags) { if (flags & 1) { // floating point if (flags & 2) { // big endian @@ -749,10 +884,15 @@ static enum CodecID mov_get_lpcm_codec_id(int bps, int flags) static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; - MOVStreamContext *sc = st->priv_data; + AVStream *st; + MOVStreamContext *sc; int j, entries, pseudo_stream_id; + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + sc = st->priv_data; + get_byte(pb); /* version */ get_be24(pb); /* flags */ @@ -761,19 +901,21 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) for(pseudo_stream_id=0; pseudo_stream_id= 16) { + get_be32(pb); /* reserved */ + get_be16(pb); /* reserved */ + dref_id = get_be16(pb); + } if (st->codec->codec_tag && st->codec->codec_tag != format && - (c->fc->video_codec_id ? codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id + (c->fc->video_codec_id ? ff_codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id : st->codec->codec_tag != MKTAG('j','p','e','g')) ){ /* Multiple fourcc, we skip JPEG. This is not correct, we should @@ -787,21 +929,21 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) sc->dref_id= dref_id; st->codec->codec_tag = format; - id = codec_get_id(codec_movaudio_tags, format); - if (id<=0 && (format&0xFFFF) == 'm'+('s'<<8)) - id = codec_get_id(codec_wav_tags, bswap_32(format)&0xFFFF); + id = ff_codec_get_id(codec_movaudio_tags, format); + if (id<=0 && ((format&0xFFFF) == 'm'+('s'<<8) || (format&0xFFFF) == 'T'+('S'<<8))) + id = ff_codec_get_id(ff_codec_wav_tags, bswap_32(format)&0xFFFF); if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) { st->codec->codec_type = CODEC_TYPE_AUDIO; } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */ format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */ - id = codec_get_id(codec_movvideo_tags, format); + id = ff_codec_get_id(codec_movvideo_tags, format); if (id <= 0) - id = codec_get_id(codec_bmp_tags, format); + id = ff_codec_get_id(ff_codec_bmp_tags, format); if (id > 0) st->codec->codec_type = CODEC_TYPE_VIDEO; else if(st->codec->codec_type == CODEC_TYPE_DATA){ - id = codec_get_id(ff_codec_movsubtitle_tags, format); + id = ff_codec_get_id(ff_codec_movsubtitle_tags, format); if(id > 0) st->codec->codec_type = CODEC_TYPE_SUBTITLE; } @@ -812,8 +954,7 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) (format >> 24) & 0xff, st->codec->codec_type); if(st->codec->codec_type==CODEC_TYPE_VIDEO) { - uint8_t codec_name[32]; - unsigned int color_depth; + unsigned int color_depth, len; int color_greyscale; st->codec->codec_id = id; @@ -831,11 +972,15 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) get_be32(pb); /* data size, always 0 */ get_be16(pb); /* frames per samples */ - get_buffer(pb, codec_name, 32); /* codec name, pascal string */ - if (codec_name[0] <= 31) { - memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]); - st->codec->codec_name[codec_name[0]] = 0; - } + len = get_byte(pb); /* codec name, pascal string */ + if (len > 31) + len = 31; + mov_read_mac_string(c, pb, len, st->codec->codec_name, 32); + if (len < 31) + url_fskip(pb, 31 - len); + /* codec_tag YV12 triggers an UV swap in rawdec.c */ + if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) + st->codec->codec_tag=MKTAG('I', '4', '2', '0'); st->codec->bits_per_coded_sample = get_be16(pb); /* depth */ st->codec->color_table_id = get_be16(pb); /* colortable id */ @@ -880,9 +1025,9 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) color_table = ff_qt_default_palette_256; for (j = 0; j < color_count; j++) { - r = color_table[j * 4 + 0]; - g = color_table[j * 4 + 1]; - b = color_table[j * 4 + 2]; + r = color_table[j * 3 + 0]; + g = color_table[j * 3 + 1]; + b = color_table[j * 3 + 2]; st->codec->palctrl->palette[j] = (r << 16) | (g << 8) | (b); } @@ -943,11 +1088,11 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) st->codec->channels = get_be32(pb); get_be32(pb); /* always 0x7F000000 */ st->codec->bits_per_coded_sample = get_be32(pb); /* bits per channel if sound is uncompressed */ - flags = get_be32(pb); /* lcpm format specific flag */ + flags = get_be32(pb); /* lpcm format specific flag */ sc->bytes_per_frame = get_be32(pb); /* bytes per audio packet if constant */ sc->samples_per_frame = get_be32(pb); /* lpcm frames per audio packet if constant */ if (format == MKTAG('l','p','c','m')) - st->codec->codec_id = mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags); + st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags); } } @@ -996,7 +1141,8 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) // ttxt stsd contains display flags, justification, background // color, fonts, and default styles, so fake an atom to read it MOVAtom fake_atom = { .size = size - (url_ftell(pb) - start_pos) }; - mov_read_glbl(c, pb, fake_atom); + if (format != AV_RL32("mp4s")) // mp4s contains a regular esds atom + mov_read_glbl(c, pb, fake_atom); st->codec->codec_id= id; st->codec->width = sc->width; st->codec->height = sc->height; @@ -1032,6 +1178,9 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) #endif /* no ifdef since parameters are always those */ case CODEC_ID_QCELP: + // force sample rate for qcelp when not stored in mov + if (st->codec->codec_tag != MKTAG('Q','c','l','p')) + st->codec->sample_rate = 8000; st->codec->frame_size= 160; st->codec->channels= 1; /* really needed */ break; @@ -1070,10 +1219,15 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; - MOVStreamContext *sc = st->priv_data; + AVStream *st; + MOVStreamContext *sc; unsigned int i, entries; + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + sc = st->priv_data; + get_byte(pb); /* version */ get_be24(pb); /* flags */ @@ -1096,12 +1250,46 @@ static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOVAtom atom) return 0; } +static int mov_read_stps(MOVContext *c, ByteIOContext *pb, MOVAtom atom) +{ + AVStream *st; + MOVStreamContext *sc; + unsigned i, entries; + + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + sc = st->priv_data; + + get_be32(pb); // version + flags + + entries = get_be32(pb); + if (entries >= UINT_MAX / sizeof(*sc->stps_data)) + return -1; + sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data)); + if (!sc->stps_data) + return AVERROR(ENOMEM); + sc->stps_count = entries; + + for (i = 0; i < entries; i++) { + sc->stps_data[i] = get_be32(pb); + //dprintf(c->fc, "stps %d\n", sc->stps_data[i]); + } + + return 0; +} + static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; - MOVStreamContext *sc = st->priv_data; + AVStream *st; + MOVStreamContext *sc; unsigned int i, entries; + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + sc = st->priv_data; + get_byte(pb); /* version */ get_be24(pb); /* flags */ @@ -1125,16 +1313,30 @@ static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOVAtom atom) static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; - MOVStreamContext *sc = st->priv_data; - unsigned int i, entries, sample_size; + AVStream *st; + MOVStreamContext *sc; + unsigned int i, entries, sample_size, field_size, num_bytes; + GetBitContext gb; + unsigned char* buf; + + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + sc = st->priv_data; get_byte(pb); /* version */ get_be24(pb); /* flags */ - sample_size = get_be32(pb); - if (!sc->sample_size) /* do not overwrite value computed in stsd */ - sc->sample_size = sample_size; + if (atom.type == MKTAG('s','t','s','z')) { + sample_size = get_be32(pb); + if (!sc->sample_size) /* do not overwrite value computed in stsd */ + sc->sample_size = sample_size; + field_size = 32; + } else { + sample_size = 0; + get_be24(pb); /* reserved */ + field_size = get_byte(pb); + } entries = get_be32(pb); dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries); @@ -1143,25 +1345,53 @@ static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOVAtom atom) if (sample_size) return 0; - if(entries >= UINT_MAX / sizeof(int)) + if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) { + av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size); + return -1; + } + + if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size) return -1; sc->sample_sizes = av_malloc(entries * sizeof(int)); if (!sc->sample_sizes) return AVERROR(ENOMEM); + num_bytes = (entries*field_size+4)>>3; + + buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE); + if (!buf) { + av_freep(&sc->sample_sizes); + return AVERROR(ENOMEM); + } + + if (get_buffer(pb, buf, num_bytes) < num_bytes) { + av_freep(&sc->sample_sizes); + av_free(buf); + return -1; + } + + init_get_bits(&gb, buf, 8*num_bytes); + for(i=0; isample_sizes[i] = get_be32(pb); + sc->sample_sizes[i] = get_bits_long(&gb, field_size); + + av_free(buf); return 0; } static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; - MOVStreamContext *sc = st->priv_data; + AVStream *st; + MOVStreamContext *sc; unsigned int i, entries; int64_t duration=0; int64_t total_sample_count=0; + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + sc = st->priv_data; + get_byte(pb); /* version */ get_be24(pb); /* flags */ entries = get_be32(pb); @@ -1184,8 +1414,6 @@ static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom) sc->stts_data[i].count= sample_count; sc->stts_data[i].duration= sample_duration; - sc->time_rate= av_gcd(sc->time_rate, sample_duration); - dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration); duration+=(int64_t)sample_duration*sample_count; @@ -1200,10 +1428,15 @@ static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom) static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; - MOVStreamContext *sc = st->priv_data; + AVStream *st; + MOVStreamContext *sc; unsigned int i, entries; + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + sc = st->priv_data; + get_byte(pb); /* version */ get_be24(pb); /* flags */ entries = get_be32(pb); @@ -1221,15 +1454,14 @@ static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom) int count =get_be32(pb); int duration =get_be32(pb); - if (duration < 0) { - sc->wrong_dts = 1; - st->codec->has_b_frames = 1; - } sc->ctts_data[i].count = count; sc->ctts_data[i].duration= duration; - - sc->time_rate= av_gcd(sc->time_rate, FFABS(duration)); + if (duration < 0) + sc->dts_shift = FFMAX(sc->dts_shift, -duration); } + + dprintf(c->fc, "dts shift %d\n", sc->dts_shift); + return 0; } @@ -1241,12 +1473,20 @@ static void mov_build_index(MOVContext *mov, AVStream *st) unsigned int stts_index = 0; unsigned int stsc_index = 0; unsigned int stss_index = 0; + unsigned int stps_index = 0; unsigned int i, j; + uint64_t stream_size = 0; /* adjust first dts according to edit list */ if (sc->time_offset) { - assert(sc->time_offset % sc->time_rate == 0); - current_dts = - (sc->time_offset / sc->time_rate); + int rescaled = sc->time_offset < 0 ? av_rescale(sc->time_offset, sc->time_scale, mov->time_scale) : sc->time_offset; + current_dts = -rescaled; + if (sc->ctts_data && sc->ctts_data[0].duration / sc->stts_data[0].duration > 16) { + /* more than 16 frames delay, dts are likely wrong + this happens with files created by iMovie */ + sc->wrong_dts = 1; + st->codec->has_b_frames = 1; + } } /* only use old uncompressed audio chunk demuxing when stts specifies it */ @@ -1254,40 +1494,59 @@ static void mov_build_index(MOVContext *mov, AVStream *st) sc->stts_count == 1 && sc->stts_data[0].duration == 1)) { unsigned int current_sample = 0; unsigned int stts_sample = 0; - unsigned int keyframe, sample_size; + unsigned int sample_size; unsigned int distance = 0; int key_off = sc->keyframes && sc->keyframes[0] == 1; - st->nb_frames = sc->sample_count; + current_dts -= sc->dts_shift; + + if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries)) + return; + st->index_entries = av_malloc(sc->sample_count*sizeof(*st->index_entries)); + if (!st->index_entries) + return; + st->index_entries_allocated_size = sc->sample_count*sizeof(*st->index_entries); + for (i = 0; i < sc->chunk_count; i++) { current_offset = sc->chunk_offsets[i]; if (stsc_index + 1 < sc->stsc_count && i + 1 == sc->stsc_data[stsc_index + 1].first) stsc_index++; for (j = 0; j < sc->stsc_data[stsc_index].count; j++) { + int keyframe = 0; if (current_sample >= sc->sample_count) { av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n"); return; } - keyframe = !sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index]; - if (keyframe) { - distance = 0; + + if (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index]) { + keyframe = 1; if (stss_index + 1 < sc->keyframe_count) stss_index++; + } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) { + keyframe = 1; + if (stps_index + 1 < sc->stps_count) + stps_index++; } + if (keyframe) + distance = 0; sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample]; if(sc->pseudo_stream_id == -1 || sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) { - av_add_index_entry(st, current_offset, current_dts, sample_size, distance, - keyframe ? AVINDEX_KEYFRAME : 0); + AVIndexEntry *e = &st->index_entries[st->nb_index_entries++]; + e->pos = current_offset; + e->timestamp = current_dts; + e->size = sample_size; + e->min_distance = distance; + e->flags = keyframe ? AVINDEX_KEYFRAME : 0; dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", " "size %d, distance %d, keyframe %d\n", st->index, current_sample, current_offset, current_dts, sample_size, distance, keyframe); } current_offset += sample_size; - assert(sc->stts_data[stts_index].duration % sc->time_rate == 0); - current_dts += sc->stts_data[stts_index].duration / sc->time_rate; + stream_size += sample_size; + current_dts += sc->stts_data[stts_index].duration; distance++; stts_sample++; current_sample++; @@ -1297,54 +1556,136 @@ static void mov_build_index(MOVContext *mov, AVStream *st) } } } - } else { /* read whole chunk */ - unsigned int chunk_samples, chunk_size, chunk_duration; - unsigned int frames = 1; + if (st->duration > 0) + st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration; + } else { + unsigned chunk_samples, total = 0; + + // compute total chunk count + for (i = 0; i < sc->stsc_count; i++) { + unsigned count, chunk_count; + + chunk_samples = sc->stsc_data[i].count; + if (sc->samples_per_frame && chunk_samples % sc->samples_per_frame) { + av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n"); + return; + } + + if (sc->samples_per_frame >= 160) { // gsm + count = chunk_samples / sc->samples_per_frame; + } else if (sc->samples_per_frame > 1) { + unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame; + count = (chunk_samples+samples-1) / samples; + } else { + count = (chunk_samples+1023) / 1024; + } + + if (i < sc->stsc_count - 1) + chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first; + else + chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1); + total += chunk_count * count; + } + + dprintf(mov->fc, "chunk count %d\n", total); + if (total >= UINT_MAX / sizeof(*st->index_entries)) + return; + st->index_entries = av_malloc(total*sizeof(*st->index_entries)); + if (!st->index_entries) + return; + st->index_entries_allocated_size = total*sizeof(*st->index_entries); + + // populate index for (i = 0; i < sc->chunk_count; i++) { current_offset = sc->chunk_offsets[i]; if (stsc_index + 1 < sc->stsc_count && i + 1 == sc->stsc_data[stsc_index + 1].first) stsc_index++; chunk_samples = sc->stsc_data[stsc_index].count; - /* get chunk size, beware of alaw/ulaw/mace */ - if (sc->samples_per_frame > 0 && - (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0)) { - if (sc->samples_per_frame < 160) - chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame; - else { - chunk_size = sc->bytes_per_frame; - frames = chunk_samples / sc->samples_per_frame; - chunk_samples = sc->samples_per_frame; - } - } else - chunk_size = chunk_samples * sc->sample_size; - for (j = 0; j < frames; j++) { - av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME); - /* get chunk duration */ - chunk_duration = 0; - while (chunk_samples > 0) { - if (chunk_samples < sc->stts_data[stts_index].count) { - chunk_duration += sc->stts_data[stts_index].duration * chunk_samples; - sc->stts_data[stts_index].count -= chunk_samples; - break; + + while (chunk_samples > 0) { + AVIndexEntry *e; + unsigned size, samples; + + if (sc->samples_per_frame >= 160) { // gsm + samples = sc->samples_per_frame; + size = sc->bytes_per_frame; + } else { + if (sc->samples_per_frame > 1) { + samples = FFMIN((1024 / sc->samples_per_frame)* + sc->samples_per_frame, chunk_samples); + size = (samples / sc->samples_per_frame) * sc->bytes_per_frame; } else { - chunk_duration += sc->stts_data[stts_index].duration * chunk_samples; - chunk_samples -= sc->stts_data[stts_index].count; - if (stts_index + 1 < sc->stts_count) - stts_index++; + samples = FFMIN(1024, chunk_samples); + size = samples * sc->sample_size; } } - current_offset += sc->bytes_per_frame; + + if (st->nb_index_entries >= total) { + av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total); + return; + } + e = &st->index_entries[st->nb_index_entries++]; + e->pos = current_offset; + e->timestamp = current_dts; + e->size = size; + e->min_distance = 0; + e->flags = AVINDEX_KEYFRAME; dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", " "size %d, duration %d\n", st->index, i, current_offset, current_dts, - chunk_size, chunk_duration); - assert(chunk_duration % sc->time_rate == 0); - current_dts += chunk_duration / sc->time_rate; + size, samples); + + current_offset += size; + current_dts += samples; + chunk_samples -= samples; } } } } +static int mov_open_dref(ByteIOContext **pb, char *src, MOVDref *ref) +{ + /* try relative path, we do not try the absolute because it can leak information about our + system to an attacker */ + if (ref->nlvl_to > 0 && ref->nlvl_from > 0) { + char filename[1024]; + char *src_path; + int i, l; + + /* find a source dir */ + src_path = strrchr(src, '/'); + if (src_path) + src_path++; + else + src_path = src; + + /* find a next level down to target */ + for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--) + if (ref->path[l] == '/') { + if (i == ref->nlvl_to - 1) + break; + else + i++; + } + + /* compose filename if next level down to target was found */ + if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) { + memcpy(filename, src, src_path - src); + filename[src_path - src] = 0; + + for (i = 1; i < ref->nlvl_from; i++) + av_strlcat(filename, "../", 1024); + + av_strlcat(filename, ref->path + l + 1, 1024); + + if (!url_fopen(pb, filename, URL_RDONLY)) + return 0; + } + } + + return AVERROR(ENOENT); +}; + static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { AVStream *st; @@ -1371,12 +1712,14 @@ static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom) return 0; } - if (!sc->time_rate) - sc->time_rate = 1; - if (!sc->time_scale) + if (!sc->time_scale) { + av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index); sc->time_scale = c->time_scale; + if (!sc->time_scale) + sc->time_scale = 1; + } - av_set_pts_info(st, 64, sc->time_rate, sc->time_scale); + av_set_pts_info(st, 64, 1, sc->time_scale); if (st->codec->codec_type == CODEC_TYPE_AUDIO && !st->codec->frame_size && sc->stts_count == 1) { @@ -1385,20 +1728,33 @@ static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom) dprintf(c->fc, "frame size %d\n", st->codec->frame_size); } - if (st->duration != AV_NOPTS_VALUE) { - assert(st->duration % sc->time_rate == 0); - st->duration /= sc->time_rate; - } - mov_build_index(c, st); if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) { - if (url_fopen(&sc->pb, sc->drefs[sc->dref_id-1].path, URL_RDONLY) < 0) - av_log(c->fc, AV_LOG_ERROR, "stream %d, error opening file %s: %s\n", - st->index, sc->drefs[sc->dref_id-1].path, strerror(errno)); + MOVDref *dref = &sc->drefs[sc->dref_id - 1]; + if (mov_open_dref(&sc->pb, c->fc->filename, dref) < 0) + av_log(c->fc, AV_LOG_ERROR, + "stream %d, error opening alias: path='%s', dir='%s', " + "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n", + st->index, dref->path, dref->dir, dref->filename, + dref->volume, dref->nlvl_from, dref->nlvl_to); } else sc->pb = c->fc->pb; + if (st->codec->codec_type == CODEC_TYPE_VIDEO) { + if (st->codec->width != sc->width || st->codec->height != sc->height) { + AVRational r = av_d2q(((double)st->codec->height * sc->width) / + ((double)st->codec->width * sc->height), INT_MAX); + if (st->sample_aspect_ratio.num) + st->sample_aspect_ratio = av_mul_q(st->sample_aspect_ratio, r); + else + st->sample_aspect_ratio = r; + } + + av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den, + sc->time_scale*st->nb_frames, st->duration, INT_MAX); + } + switch (st->codec->codec_id) { #if CONFIG_H261_DECODER case CODEC_ID_H261: @@ -1406,6 +1762,9 @@ static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom) #if CONFIG_H263_DECODER case CODEC_ID_H263: #endif +#if CONFIG_H264_DECODER + case CODEC_ID_H264: +#endif #if CONFIG_MPEG4_DECODER case CODEC_ID_MPEG4: #endif @@ -1420,6 +1779,7 @@ static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom) av_freep(&sc->sample_sizes); av_freep(&sc->keyframes); av_freep(&sc->stts_data); + av_freep(&sc->stps_data); return 0; } @@ -1454,10 +1814,16 @@ static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) int height; int64_t disp_transform[2]; int display_matrix[3][2]; - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; - MOVStreamContext *sc = st->priv_data; - int version = get_byte(pb); + AVStream *st; + MOVStreamContext *sc; + int version; + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + sc = st->priv_data; + + version = get_byte(pb); get_be24(pb); /* flags */ /* MOV_TRACK_ENABLED 0x0001 @@ -1500,13 +1866,16 @@ static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) sc->width = width >> 16; sc->height = height >> 16; - //transform the display width/height according to the matrix + // transform the display width/height according to the matrix // skip this if the display matrix is the default identity matrix + // or if it is rotating the picture, ex iPhone 3GS // to keep the same scale, use [width height 1<<16] if (width && height && - (display_matrix[0][0] != 65536 || display_matrix[0][1] || - display_matrix[1][0] || display_matrix[1][1] != 65536 || - display_matrix[2][0] || display_matrix[2][1])) { + ((display_matrix[0][0] != 65536 || + display_matrix[1][1] != 65536) && + !display_matrix[0][1] && + !display_matrix[1][0] && + !display_matrix[2][0] && !display_matrix[2][1])) { for (i = 0; i < 2; i++) disp_transform[i] = (int64_t) width * display_matrix[0][i] + @@ -1644,8 +2013,7 @@ static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom) "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i, offset, dts, sample_size, distance, keyframe); distance++; - assert(sample_duration % sc->time_rate == 0); - dts += sample_duration / sc->time_rate; + dts += sample_duration; offset += sample_size; } frag->moof_offset = offset; @@ -1667,7 +2035,6 @@ static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOVAtom atom) return 0; } atom.type = get_le32(pb); - atom.offset += 8; atom.size -= 8; if (atom.type != MKTAG('m','d','a','t')) { url_fskip(pb, atom.size); @@ -1713,7 +2080,6 @@ static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOVAtom atom) if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0) goto free_and_return; atom.type = MKTAG('m','o','o','v'); - atom.offset = 0; atom.size = moov_len; #ifdef DEBUG // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); } @@ -1732,21 +2098,27 @@ free_and_return: /* edit list atom */ static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - MOVStreamContext *sc = c->fc->streams[c->fc->nb_streams-1]->priv_data; + MOVStreamContext *sc; int i, edit_count; + if (c->fc->nb_streams < 1) + return 0; + sc = c->fc->streams[c->fc->nb_streams-1]->priv_data; + get_byte(pb); /* version */ get_be24(pb); /* flags */ edit_count = get_be32(pb); /* entries */ + if((uint64_t)edit_count*12+8 > atom.size) + return -1; + for(i=0; itime_offset = time; - sc->time_rate = av_gcd(sc->time_rate, time); + if (i == 0 && time >= -1) { + sc->time_offset = time != -1 ? time : -duration; } } @@ -1788,11 +2160,13 @@ static const MOVParseTableEntry mov_default_parse_table[] = { { MKTAG('p','a','s','p'), mov_read_pasp }, { MKTAG('s','t','b','l'), mov_read_default }, { MKTAG('s','t','c','o'), mov_read_stco }, +{ MKTAG('s','t','p','s'), mov_read_stps }, { MKTAG('s','t','s','c'), mov_read_stsc }, { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */ { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */ { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */ { MKTAG('s','t','t','s'), mov_read_stts }, +{ MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */ { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */ { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */ { MKTAG('t','r','a','k'), mov_read_trak }, @@ -1857,7 +2231,7 @@ static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap) MOVContext *mov = s->priv_data; ByteIOContext *pb = s->pb; int err; - MOVAtom atom = { 0, 0, 0 }; + MOVAtom atom = { 0 }; mov->fc = s; /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */ @@ -1880,21 +2254,17 @@ static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap) return 0; } -static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) +static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st) { - MOVContext *mov = s->priv_data; - MOVStreamContext *sc = 0; - AVIndexEntry *sample = 0; + AVIndexEntry *sample = NULL; int64_t best_dts = INT64_MAX; - int i, ret; - retry: + int i; for (i = 0; i < s->nb_streams; i++) { - AVStream *st = s->streams[i]; - MOVStreamContext *msc = st->priv_data; - if (st->discard != AVDISCARD_ALL && msc->pb && msc->current_sample < st->nb_index_entries) { - AVIndexEntry *current_sample = &st->index_entries[msc->current_sample]; - int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate, - AV_TIME_BASE, msc->time_scale); + AVStream *avst = s->streams[i]; + MOVStreamContext *msc = avst->priv_data; + if (msc->pb && msc->current_sample < avst->nb_index_entries) { + AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample]; + int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale); dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts); if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) || (!url_is_streamed(s->pb) && @@ -1903,43 +2273,60 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) { sample = current_sample; best_dts = dts; - sc = msc; + *st = avst; } } } + return sample; +} + +static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + MOVContext *mov = s->priv_data; + MOVStreamContext *sc; + AVIndexEntry *sample; + AVStream *st = NULL; + int ret; + retry: + sample = mov_find_next_sample(s, &st); if (!sample) { mov->found_mdat = 0; if (!url_is_streamed(s->pb) || - mov_read_default(mov, s->pb, (MOVAtom){ 0, 0, INT64_MAX }) < 0 || + mov_read_default(mov, s->pb, (MOVAtom){ 0, INT64_MAX }) < 0 || url_feof(s->pb)) - return -1; + return AVERROR_EOF; dprintf(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb)); goto retry; } + sc = st->priv_data; /* must be done just before reading, to avoid infinite loop on sample */ sc->current_sample++; - if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) { - av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", - sc->ffindex, sample->pos); - return -1; - } - ret = av_get_packet(sc->pb, pkt, sample->size); - if (ret < 0) - return ret; -#if CONFIG_DV_DEMUXER - if (mov->dv_demux && sc->dv_audio_container) { - dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size); - av_free(pkt->data); - pkt->size = 0; - if (dv_get_packet(mov->dv_demux, pkt) < 0) + + if (st->discard != AVDISCARD_ALL) { + if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) { + av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", + sc->ffindex, sample->pos); return -1; - } + } + ret = av_get_packet(sc->pb, pkt, sample->size); + if (ret < 0) + return ret; +#if CONFIG_DV_DEMUXER + if (mov->dv_demux && sc->dv_audio_container) { + dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size); + av_free(pkt->data); + pkt->size = 0; + ret = dv_get_packet(mov->dv_demux, pkt); + if (ret < 0) + return ret; + } #endif + } + pkt->stream_index = sc->ffindex; pkt->dts = sample->timestamp; if (sc->ctts_data) { - assert(sc->ctts_data[sc->ctts_index].duration % sc->time_rate == 0); - pkt->pts = pkt->dts + sc->ctts_data[sc->ctts_index].duration / sc->time_rate; + pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration; /* update ctts context */ sc->ctts_sample++; if (sc->ctts_index < sc->ctts_count && @@ -1950,12 +2337,13 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) if (sc->wrong_dts) pkt->dts = AV_NOPTS_VALUE; } else { - AVStream *st = s->streams[sc->ffindex]; int64_t next_dts = (sc->current_sample < st->nb_index_entries) ? st->index_entries[sc->current_sample].timestamp : st->duration; pkt->duration = next_dts - pkt->dts; pkt->pts = pkt->dts; } + if (st->discard == AVDISCARD_ALL) + goto retry; pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0; pkt->pos = sample->pos; dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n", @@ -1963,18 +2351,18 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) return 0; } -static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags) +static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags) { MOVStreamContext *sc = st->priv_data; int sample, time_sample; int i; sample = av_index_search_timestamp(st, timestamp, flags); - dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample); + dprintf(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample); if (sample < 0) /* not sure what to do */ return -1; sc->current_sample = sample; - dprintf(st->codec, "stream %d, found sample %d\n", st->index, sc->current_sample); + dprintf(s, "stream %d, found sample %d\n", st->index, sc->current_sample); /* adjust ctts index */ if (sc->ctts_data) { time_sample = 0; @@ -2004,7 +2392,7 @@ static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti sample_time = 0; st = s->streams[stream_index]; - sample = mov_seek_stream(st, sample_time, flags); + sample = mov_seek_stream(s, st, sample_time, flags); if (sample < 0) return -1; @@ -2013,11 +2401,11 @@ static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti for (i = 0; i < s->nb_streams; i++) { st = s->streams[i]; - if (stream_index == i || st->discard == AVDISCARD_ALL) + if (stream_index == i) continue; timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base); - mov_seek_stream(st, timestamp, flags); + mov_seek_stream(s, st, timestamp, flags); } return 0; } @@ -2032,8 +2420,10 @@ static int mov_read_close(AVFormatContext *s) MOVStreamContext *sc = st->priv_data; av_freep(&sc->ctts_data); - for (j = 0; j < sc->drefs_count; j++) + for (j = 0; j < sc->drefs_count; j++) { av_freep(&sc->drefs[j].path); + av_freep(&sc->drefs[j].dir); + } av_freep(&sc->drefs); if (sc->pb && sc->pb != s->pb) url_fclose(sc->pb);