X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fmov.c;h=4765f18f00267d68957fda6f411b44a0ef7ce8fe;hb=b381a823cd97fb37f95706e66a7eb8b025243b3c;hp=698240ae2d27bdf3a08ea508fa00e040f5ad7238;hpb=1975c52c2adafbaa76b4c47fd1d30facef27e057;p=ffmpeg diff --git a/libavformat/mov.c b/libavformat/mov.c index 698240ae2d2..4765f18f002 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -1,6 +1,7 @@ /* * MOV demuxer * Copyright (c) 2001 Fabrice Bellard + * Copyright (c) 2009 Baptiste Coudurier * * This file is part of FFmpeg. * @@ -22,14 +23,17 @@ #include //#define DEBUG +//#define DEBUG_METADATA +//#define MOV_EXPORT_ALL_METADATA #include "libavutil/intreadwrite.h" +#include "libavutil/avstring.h" #include "avformat.h" #include "riff.h" #include "isom.h" -#include "dv.h" #include "libavcodec/mpeg4audio.h" #include "libavcodec/mpegaudiodata.h" +#include "libavcodec/get_bits.h" #if CONFIG_ZLIB #include @@ -61,101 +65,6 @@ #undef NDEBUG #include -/* the QuickTime file format is quite convoluted... - * it has lots of index tables, each indexing something in another one... - * Here we just use what is needed to read the chunks - */ - -typedef struct { - int first; - int count; - int id; -} MOVStsc; - -typedef struct { - uint32_t type; - char *path; -} MOVDref; - -typedef struct { - uint32_t type; - int64_t offset; - int64_t size; /* total size (excluding the size and type fields) */ -} MOVAtom; - -struct MOVParseTableEntry; - -typedef struct { - unsigned track_id; - uint64_t base_data_offset; - uint64_t moof_offset; - unsigned stsd_id; - unsigned duration; - unsigned size; - unsigned flags; -} MOVFragment; - -typedef struct { - unsigned track_id; - unsigned stsd_id; - unsigned duration; - unsigned size; - unsigned flags; -} MOVTrackExt; - -typedef struct MOVStreamContext { - ByteIOContext *pb; - int ffindex; /* the ffmpeg stream id */ - int next_chunk; - unsigned int chunk_count; - int64_t *chunk_offsets; - unsigned int stts_count; - MOVStts *stts_data; - unsigned int ctts_count; - MOVStts *ctts_data; - unsigned int sample_to_chunk_sz; - MOVStsc *sample_to_chunk; - int sample_to_ctime_index; - int sample_to_ctime_sample; - unsigned int sample_size; - unsigned int sample_count; - int *sample_sizes; - unsigned int keyframe_count; - int *keyframes; - int time_scale; - int time_rate; - int time_offset; ///< time offset of the first edit list entry - int current_sample; - unsigned int bytes_per_frame; - unsigned int samples_per_frame; - int dv_audio_container; - int pseudo_stream_id; ///< -1 means demux all ids - int16_t audio_cid; ///< stsd audio compression id - unsigned drefs_count; - MOVDref *drefs; - int dref_id; - int wrong_dts; ///< dts are wrong due to negative ctts - int width; ///< tkhd width - int height; ///< tkhd height -} MOVStreamContext; - -typedef struct MOVContext { - AVFormatContext *fc; - int time_scale; - int64_t duration; /* duration of the longest track */ - int found_moov; /* when both 'moov' and 'mdat' sections has been found */ - int found_mdat; /* we suppose we have enough data to read the file */ - AVPaletteControl palette_control; - DVDemuxContext *dv_demux; - AVFormatContext *dv_fctx; - int isom; /* 1 if file is ISO Media (mp4/3gp) */ - MOVFragment fragment; ///< current fragment in moof atom - MOVTrackExt *trex_data; - unsigned trex_count; - int itunes_metadata; ///< metadata are itunes style -} MOVContext; - - /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */ /* those functions parse an atom */ @@ -171,18 +80,112 @@ typedef struct MOVParseTableEntry { static const MOVParseTableEntry mov_default_parse_table[]; +static int mov_metadata_trkn(MOVContext *c, ByteIOContext *pb, unsigned len) +{ + char buf[16]; + + get_be16(pb); // unknown + snprintf(buf, sizeof(buf), "%d", get_be16(pb)); + av_metadata_set(&c->fc->metadata, "track", buf); + + get_be16(pb); // total tracks + + return 0; +} + +static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom) +{ +#ifdef MOV_EXPORT_ALL_METADATA + char tmp_key[5]; +#endif + char str[1024], key2[16], language[4] = {0}; + const char *key = NULL; + uint16_t str_size; + 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'): 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,'g','e','n'): key = "genre"; break; + case MKTAG(0xa9,'t','o','o'): + 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; + } + + if (c->itunes_metadata && atom.size > 8) { + int data_size = get_be32(pb); + int tag = get_le32(pb); + if (tag == MKTAG('d','a','t','a')) { + 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); + atom.size -= 4; + } else + str_size = atom.size; + +#ifdef MOV_EXPORT_ALL_METADATA + if (!key) { + snprintf(tmp_key, 5, "%.4s", (char*)&atom.type); + key = tmp_key; + } +#endif + + if (!key) + return 0; + if (atom.size < 0) + return -1; + + str_size = FFMIN3(sizeof(str)-1, str_size, atom.size); + + if (parse) + parse(c, pb, str_size); + 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); + av_metadata_set(&c->fc->metadata, key2, str); + } + } +#ifdef DEBUG_METADATA + av_log(c->fc, AV_LOG_DEBUG, "lang \"%3s\" ", language); + av_log(c->fc, AV_LOG_DEBUG, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %lld\n", + key, str, (char*)&atom.type, str_size, atom.size); +#endif + + return 0; +} + 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; if(atom.size >= 8) { @@ -190,12 +193,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) { @@ -208,45 +209,61 @@ static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOVAtom atom) break; a.size = FFMIN(a.size, atom.size - total_size); - for (i = 0; mov_default_parse_table[i].type != 0 - && mov_default_parse_table[i].type != a.type; i++) - /* empty */; + for (i = 0; mov_default_parse_table[i].type; i++) + if (mov_default_parse_table[i].type == a.type) { + parse = mov_default_parse_table[i].parse; + break; + } + + // container is user data + if (!parse && (atom.type == MKTAG('u','d','t','a') || + atom.type == MKTAG('i','l','s','t'))) + parse = mov_read_udta_string; - if (mov_default_parse_table[i].type == 0) { /* skip leaf atoms data */ + if (!parse) { /* skip leaf atoms data */ url_fskip(pb, a.size); } else { int64_t start_pos = url_ftell(pb); int64_t left; - err = mov_default_parse_table[i].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)) return -1; - sc->drefs_count = entries; sc->drefs = av_mallocz(entries * sizeof(*sc->drefs)); + if (!sc->drefs) + return AVERROR(ENOMEM); + sc->drefs_count = entries; for (i = 0; i < sc->drefs_count; i++) { MOVDref *dref = &sc->drefs[i]; @@ -260,18 +277,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); + + /* 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, 112); + url_fskip(pb, 16); for (type = 0; type != -1 && url_ftell(pb) < next; ) { type = get_be16(pb); @@ -285,7 +317,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; @@ -294,6 +326,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); } @@ -305,10 +348,15 @@ static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom) static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - AVStream *st = c->fc->streams[c->fc->nb_streams-1]; + AVStream *st; uint32_t type; uint32_t ctype; + if (c->fc->nb_streams < 1) // meta before first trak + return 0; + + st = c->fc->streams[c->fc->nb_streams-1]; + get_byte(pb); /* version */ get_be24(pb); /* flags */ @@ -316,33 +364,26 @@ static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOVAtom atom) ctype = get_le32(pb); type = get_le32(pb); /* component subtype */ - dprintf(c->fc, "ctype= %c%c%c%c (0x%08x)\n", *((char *)&ctype), ((char *)&ctype)[1], - ((char *)&ctype)[2], ((char *)&ctype)[3], (int) ctype); - dprintf(c->fc, "stype= %c%c%c%c\n", - *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]); - if(!ctype) - c->isom = 1; + dprintf(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype); + dprintf(c->fc, "stype= %.4s\n", (char*)&type); + if (type == MKTAG('v','i','d','e')) st->codec->codec_type = CODEC_TYPE_VIDEO; else if(type == MKTAG('s','o','u','n')) st->codec->codec_type = CODEC_TYPE_AUDIO; else if(type == MKTAG('m','1','a',' ')) st->codec->codec_id = CODEC_ID_MP2; - else if(type == MKTAG('s','u','b','p')) { + else if(type == MKTAG('s','u','b','p')) st->codec->codec_type = CODEC_TYPE_SUBTITLE; - } + get_be32(pb); /* component manufacture */ 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; @@ -355,12 +396,12 @@ static int mp4_read_descr_len(ByteIOContext *pb) return len; } -static int mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag) +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; } @@ -369,27 +410,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 */ @@ -397,11 +443,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); @@ -413,19 +459,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; } } @@ -433,11 +477,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)) @@ -459,15 +513,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; } @@ -491,11 +565,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 */ @@ -512,7 +593,8 @@ static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */ lang = get_be16(pb); /* language */ - ff_mov_lang_to_iso639(lang, st->language); + if (ff_mov_lang_to_iso639(lang, language)) + av_metadata_set(&st->metadata, "language", language); get_be16(pb); /* quality */ return 0; @@ -556,7 +638,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; @@ -576,9 +662,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) { @@ -628,7 +719,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; @@ -655,7 +750,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; @@ -671,10 +770,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 */ @@ -683,10 +787,11 @@ static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOVAtom atom) if(entries >= UINT_MAX/sizeof(int64_t)) return -1; - sc->chunk_count = entries; sc->chunk_offsets = av_malloc(entries * sizeof(int64_t)); if (!sc->chunk_offsets) - return -1; + return AVERROR(ENOMEM); + sc->chunk_count = entries; + if (atom.type == MKTAG('s','t','c','o')) for(i=0; ichunk_offsets[i] = get_be32(pb); @@ -703,7 +808,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 @@ -736,10 +841,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 */ @@ -748,19 +858,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 @@ -774,21 +886,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; } @@ -820,8 +932,16 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) 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; + int i; + int pos = 0; + for (i = 0; i < codec_name[0] && pos < sizeof(st->codec->codec_name) - 3; i++) { + uint8_t tmp; + PUT_UTF8(codec_name[i+1], tmp, st->codec->codec_name[pos++] = tmp;) + } + st->codec->codec_name[pos] = 0; + /* 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 */ @@ -839,6 +959,7 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) unsigned int color_start, color_count, color_end; unsigned char r, g, b; + st->codec->palctrl = av_malloc(sizeof(*st->codec->palctrl)); if (color_greyscale) { int color_index, color_dec; /* compute the greyscale palette */ @@ -848,7 +969,7 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) color_dec = 256 / (color_count - 1); for (j = 0; j < color_count; j++) { r = g = b = color_index; - c->palette_control.palette[j] = + st->codec->palctrl->palette[j] = (r << 16) | (g << 8) | (b); color_index -= color_dec; if (color_index < 0) @@ -866,10 +987,10 @@ 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]; - c->palette_control.palette[j] = + 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); } } else { @@ -891,15 +1012,13 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) get_byte(pb); b = get_byte(pb); get_byte(pb); - c->palette_control.palette[j] = + st->codec->palctrl->palette[j] = (r << 16) | (g << 8) | (b); } } } - st->codec->palctrl = &c->palette_control; st->codec->palctrl->palette_changed = 1; - } else - st->codec->palctrl = NULL; + } } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) { int bits_per_sample, flags; uint16_t version = get_be16(pb); @@ -931,11 +1050,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); } } @@ -984,7 +1103,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; @@ -1020,6 +1140,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; @@ -1058,53 +1181,91 @@ 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 */ entries = get_be32(pb); - if(entries >= UINT_MAX / sizeof(*sc->sample_to_chunk)) - return -1; - dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries); - sc->sample_to_chunk_sz = entries; - sc->sample_to_chunk = av_malloc(entries * sizeof(*sc->sample_to_chunk)); - if (!sc->sample_to_chunk) + if(entries >= UINT_MAX / sizeof(*sc->stsc_data)) return -1; + sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data)); + if (!sc->stsc_data) + return AVERROR(ENOMEM); + sc->stsc_count = entries; + for(i=0; isample_to_chunk[i].first = get_be32(pb); - sc->sample_to_chunk[i].count = get_be32(pb); - sc->sample_to_chunk[i].id = get_be32(pb); + sc->stsc_data[i].first = get_be32(pb); + sc->stsc_data[i].count = get_be32(pb); + sc->stsc_data[i].id = get_be32(pb); + } + 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 */ entries = get_be32(pb); + dprintf(c->fc, "keyframe_count = %d\n", entries); + if(entries >= UINT_MAX / sizeof(int)) return -1; - - sc->keyframe_count = entries; - - dprintf(c->fc, "keyframe_count = %d\n", sc->keyframe_count); - sc->keyframes = av_malloc(entries * sizeof(int)); if (!sc->keyframes) - return -1; + return AVERROR(ENOMEM); + sc->keyframe_count = entries; + for(i=0; ikeyframes[i] = get_be32(pb); //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]); @@ -1114,55 +1275,97 @@ 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); - if(entries >= UINT_MAX / sizeof(int)) - return -1; + + dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries); sc->sample_count = entries; if (sample_size) return 0; - dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count); + 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); + + dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries); + if(entries >= UINT_MAX / sizeof(*sc->stts_data)) return -1; - - sc->stts_count = entries; sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data)); if (!sc->stts_data) - return -1; - dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries); - - sc->time_rate=0; + return AVERROR(ENOMEM); + sc->stts_count = entries; for(i=0; istts_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; @@ -1189,35 +1390,40 @@ 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); + + dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries); + if(entries >= UINT_MAX / sizeof(*sc->ctts_data)) return -1; - - sc->ctts_count = entries; sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data)); if (!sc->ctts_data) - return -1; - dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries); + return AVERROR(ENOMEM); + sc->ctts_count = entries; for(i=0; iwrong_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; } @@ -1229,12 +1435,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 */ @@ -1242,39 +1456,48 @@ 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; + for (i = 0; i < sc->chunk_count; i++) { current_offset = sc->chunk_offsets[i]; - if (stsc_index + 1 < sc->sample_to_chunk_sz && - i + 1 == sc->sample_to_chunk[stsc_index + 1].first) + if (stsc_index + 1 < sc->stsc_count && + i + 1 == sc->stsc_data[stsc_index + 1].first) stsc_index++; - for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) { + 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"); - goto out; + 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->sample_to_chunk[stsc_index].id - 1 == sc->pseudo_stream_id) { + 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); 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++; @@ -1284,57 +1507,99 @@ 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 { for (i = 0; i < sc->chunk_count; i++) { + unsigned chunk_samples; + current_offset = sc->chunk_offsets[i]; - if (stsc_index + 1 < sc->sample_to_chunk_sz && - i + 1 == sc->sample_to_chunk[stsc_index + 1].first) + if (stsc_index + 1 < sc->stsc_count && + i + 1 == sc->stsc_data[stsc_index + 1].first) stsc_index++; - chunk_samples = sc->sample_to_chunk[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; + chunk_samples = sc->stsc_data[stsc_index].count; + + if (sc->samples_per_frame && chunk_samples % sc->samples_per_frame) { + av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n"); + return; + } + + while (chunk_samples > 0) { + 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; + + av_add_index_entry(st, current_offset, current_dts, size, 0, 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; } } } - out: - /* adjust sample count to avindex entries */ - sc->sample_count = st->nb_index_entries; } +static int mov_open_dref(ByteIOContext **pb, char *src, MOVDref *ref) +{ + /* try absolute path */ + if (!url_fopen(pb, ref->path, URL_RDONLY)) + return 0; + + /* try relative path */ + 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) { + 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; @@ -1348,44 +1613,62 @@ static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom) st->priv_data = sc; st->codec->codec_type = CODEC_TYPE_DATA; - st->start_time = 0; /* XXX: check */ sc->ffindex = st->index; if ((ret = mov_read_default(c, pb, atom)) < 0) return ret; /* sanity checks */ - if(sc->chunk_count && (!sc->stts_count || !sc->sample_to_chunk_sz || - (!sc->sample_size && !sc->sample_count))){ + if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count || + (!sc->sample_size && !sc->sample_count))) { av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n", st->index); - sc->sample_count = 0; //ignore track return 0; } - if(!sc->time_rate) - sc->time_rate=1; - if(!sc->time_scale) - sc->time_scale= c->time_scale; - av_set_pts_info(st, 64, sc->time_rate, sc->time_scale); - if (st->codec->codec_type == CODEC_TYPE_AUDIO && - !st->codec->frame_size && sc->stts_count == 1) - st->codec->frame_size = av_rescale(sc->time_rate, st->codec->sample_rate, 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; + } - if(st->duration != AV_NOPTS_VALUE){ - assert(st->duration % sc->time_rate == 0); - st->duration /= sc->time_rate; + 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) { + st->codec->frame_size = av_rescale(sc->stts_data[0].duration, + st->codec->sample_rate, sc->time_scale); + dprintf(c->fc, "frame size %d\n", st->codec->frame_size); } 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: @@ -1393,20 +1676,24 @@ 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 - st->codec->width= 0; /* let decoder init width/height */ + st->codec->width = 0; /* let decoder init width/height */ st->codec->height= 0; break; } /* Do not need those anymore. */ av_freep(&sc->chunk_offsets); - av_freep(&sc->sample_to_chunk); + av_freep(&sc->stsc_data); av_freep(&sc->sample_sizes); av_freep(&sc->keyframes); av_freep(&sc->stts_data); + av_freep(&sc->stps_data); return 0; } @@ -1422,61 +1709,15 @@ static int mov_read_ilst(MOVContext *c, ByteIOContext *pb, MOVAtom atom) static int mov_read_meta(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { - url_fskip(pb, 4); // version + flags - atom.size -= 4; - return mov_read_default(c, pb, atom); -} - -static int mov_read_trkn(MOVContext *c, ByteIOContext *pb, MOVAtom atom) -{ - get_be32(pb); // type - get_be32(pb); // unknown - c->fc->track = get_be32(pb); - dprintf(c->fc, "%.4s %d\n", (char*)&atom.type, c->fc->track); - return 0; -} - -static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom) -{ - char *str = NULL; - int size; - uint16_t str_size; - - if (c->itunes_metadata) { - int data_size = get_be32(pb); - int tag = get_le32(pb); - if (tag == MKTAG('d','a','t','a')) { - get_be32(pb); // type - get_be32(pb); // unknown - str_size = data_size - 16; - atom.size -= 16; - } else return 0; - } else { - str_size = get_be16(pb); // string length - get_be16(pb); // language + while (atom.size > 8) { + uint32_t tag = get_le32(pb); atom.size -= 4; + if (tag == MKTAG('h','d','l','r')) { + url_fseek(pb, -8, SEEK_CUR); + atom.size += 8; + return mov_read_default(c, pb, atom); + } } - switch (atom.type) { - case MKTAG(0xa9,'n','a','m'): - str = c->fc->title; size = sizeof(c->fc->title); break; - case MKTAG(0xa9,'A','R','T'): - case MKTAG(0xa9,'w','r','t'): - str = c->fc->author; size = sizeof(c->fc->author); break; - case MKTAG(0xa9,'c','p','y'): - str = c->fc->copyright; size = sizeof(c->fc->copyright); break; - case MKTAG(0xa9,'c','m','t'): - case MKTAG(0xa9,'i','n','f'): - str = c->fc->comment; size = sizeof(c->fc->comment); break; - case MKTAG(0xa9,'a','l','b'): - str = c->fc->album; size = sizeof(c->fc->album); break; - } - if (!str) - return 0; - if (atom.size < 0) - return -1; - - get_buffer(pb, str, FFMIN3(size, str_size, atom.size)); - dprintf(c->fc, "%.4s %s %d %lld\n", (char*)&atom.type, str, str_size, atom.size); return 0; } @@ -1487,10 +1728,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 @@ -1508,7 +1755,7 @@ static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) } st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/ get_be32(pb); /* reserved */ - st->start_time = 0; /* check */ + /* highlevel (considering edits) duration in movie timebase */ (version == 1) ? get_be64(pb) : get_be32(pb); get_be32(pb); /* reserved */ @@ -1533,13 +1780,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] + @@ -1564,7 +1814,7 @@ static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom) flags = get_be24(pb); track_id = get_be32(pb); - if (!track_id || track_id > c->fc->nb_streams) + if (!track_id) return -1; frag->track_id = track_id; for (i = 0; i < c->trex_count; i++) @@ -1595,9 +1845,10 @@ static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOVAtom atom) if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data)) return -1; - c->trex_data = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data)); - if (!c->trex_data) + trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data)); + if (!trex) return AVERROR(ENOMEM); + c->trex_data = trex; trex = &c->trex_data[c->trex_count++]; get_byte(pb); /* version */ get_be24(pb); /* flags */ @@ -1612,7 +1863,7 @@ static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOVAtom atom) static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom) { MOVFragment *frag = &c->fragment; - AVStream *st; + AVStream *st = NULL; MOVStreamContext *sc; uint64_t offset; int64_t dts; @@ -1620,9 +1871,16 @@ static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom) unsigned entries, first_sample_flags = frag->flags; int flags, distance, i; - if (!frag->track_id || frag->track_id > c->fc->nb_streams) + for (i = 0; i < c->fc->nb_streams; i++) { + if (c->fc->streams[i]->id == frag->track_id) { + st = c->fc->streams[i]; + break; + } + } + if (!st) { + av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id); return -1; - st = c->fc->streams[frag->track_id-1]; + } sc = st->priv_data; if (sc->pseudo_stream_id+1 != frag->stsd_id) return 0; @@ -1633,12 +1891,14 @@ static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom) if (flags & 0x001) data_offset = get_be32(pb); if (flags & 0x004) first_sample_flags = get_be32(pb); if (flags & 0x800) { + MOVStts *ctts_data; if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data)) return -1; - sc->ctts_data = av_realloc(sc->ctts_data, - (entries+sc->ctts_count)*sizeof(*sc->ctts_data)); - if (!sc->ctts_data) + ctts_data = av_realloc(sc->ctts_data, + (entries+sc->ctts_count)*sizeof(*sc->ctts_data)); + if (!ctts_data) return AVERROR(ENOMEM); + sc->ctts_data = ctts_data; } dts = st->duration; offset = frag->base_data_offset + data_offset; @@ -1667,12 +1927,10 @@ 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; - sc->sample_count = st->nb_index_entries; st->duration = dts; return 0; } @@ -1691,7 +1949,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); @@ -1714,7 +1971,7 @@ static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOVAtom atom) if (get_le32(pb) != MKTAG('d','c','o','m')) return -1; if (get_le32(pb) != MKTAG('z','l','i','b')) { - av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !"); + av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !"); return -1; } get_be32(pb); /* cmvd atom */ @@ -1725,11 +1982,11 @@ static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOVAtom atom) cmov_data = av_malloc(cmov_len); if (!cmov_data) - return -1; + return AVERROR(ENOMEM); moov_data = av_malloc(moov_len); if (!moov_data) { av_free(cmov_data); - return -1; + return AVERROR(ENOMEM); } get_buffer(pb, cmov_data, cmov_len); if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK) @@ -1737,7 +1994,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); } @@ -1756,27 +2012,35 @@ 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; + if (i == 0 && time >= -1) { + sc->time_offset = time != -1 ? time : -duration; + } } if(edit_count > 1) av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, " "a/v desync might occur, patch welcome\n"); - dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, sc->edit_count); + dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count); return 0; } @@ -1810,31 +2074,24 @@ 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 }, { MKTAG('t','r','a','f'), mov_read_default }, { MKTAG('t','r','e','x'), mov_read_trex }, -{ MKTAG('t','r','k','n'), mov_read_trkn }, { MKTAG('t','r','u','n'), mov_read_trun }, { MKTAG('u','d','t','a'), mov_read_default }, { MKTAG('w','a','v','e'), mov_read_wave }, { MKTAG('e','s','d','s'), mov_read_esds }, { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */ { MKTAG('c','m','o','v'), mov_read_cmov }, -{ MKTAG(0xa9,'n','a','m'), mov_read_udta_string }, -{ MKTAG(0xa9,'w','r','t'), mov_read_udta_string }, -{ MKTAG(0xa9,'c','p','y'), mov_read_udta_string }, -{ MKTAG(0xa9,'i','n','f'), mov_read_udta_string }, -{ MKTAG(0xa9,'i','n','f'), mov_read_udta_string }, -{ MKTAG(0xa9,'A','R','T'), mov_read_udta_string }, -{ MKTAG(0xa9,'a','l','b'), mov_read_udta_string }, -{ MKTAG(0xa9,'c','m','t'), mov_read_udta_string }, { 0, NULL } }; @@ -1888,7 +2145,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) */ @@ -1911,21 +2168,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; - retry: 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 < msc->sample_count) { - 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) && @@ -1934,57 +2187,77 @@ 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; - } - av_get_packet(sc->pb, pkt, sample->size); -#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->sample_to_ctime_index].duration % sc->time_rate == 0); - pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate; + pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration; /* update ctts context */ - sc->sample_to_ctime_sample++; - if (sc->sample_to_ctime_index < sc->ctts_count && - sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) { - sc->sample_to_ctime_index++; - sc->sample_to_ctime_sample = 0; + sc->ctts_sample++; + if (sc->ctts_index < sc->ctts_count && + sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) { + sc->ctts_index++; + sc->ctts_sample = 0; } if (sc->wrong_dts) pkt->dts = AV_NOPTS_VALUE; } else { - AVStream *st = s->streams[sc->ffindex]; - int64_t next_dts = (sc->current_sample < sc->sample_count) ? + 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", @@ -1992,26 +2265,26 @@ 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; for (i = 0; i < sc->ctts_count; i++) { int next = time_sample + sc->ctts_data[i].count; if (next > sc->current_sample) { - sc->sample_to_ctime_index = i; - sc->sample_to_ctime_sample = sc->current_sample - time_sample; + sc->ctts_index = i; + sc->ctts_sample = sc->current_sample - time_sample; break; } time_sample = next; @@ -2033,7 +2306,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; @@ -2042,37 +2315,47 @@ 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; } static int mov_read_close(AVFormatContext *s) { - int i, j; MOVContext *mov = s->priv_data; - for(i=0; inb_streams; i++) { - MOVStreamContext *sc = s->streams[i]->priv_data; + int i, j; + + for (i = 0; i < s->nb_streams; i++) { + AVStream *st = s->streams[i]; + MOVStreamContext *sc = st->priv_data; + av_freep(&sc->ctts_data); - for (j=0; jdrefs_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); + + av_freep(&st->codec->palctrl); } - if(mov->dv_demux){ - for(i=0; idv_fctx->nb_streams; i++){ + + if (mov->dv_demux) { + for(i = 0; i < mov->dv_fctx->nb_streams; i++) { av_freep(&mov->dv_fctx->streams[i]->codec); av_freep(&mov->dv_fctx->streams[i]); } av_freep(&mov->dv_fctx); av_freep(&mov->dv_demux); } + av_freep(&mov->trex_data); + return 0; }