X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fmov.c;h=11b4582766c33339463ac9112bd211cbf031bb62;hb=b7327887ea260d26e4fe98d34149cd57168f2ba3;hp=eef53e6d66e2f2995751348034a843e2403b1f0d;hpb=1fdf18f4b7e9f9983b6f07ef1033a51b289692f2;p=ffmpeg diff --git a/libavformat/mov.c b/libavformat/mov.c index eef53e6d66e..11b4582766c 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -25,11 +25,13 @@ //#define DEBUG //#define MOV_EXPORT_ALL_METADATA +#include "libavutil/audioconvert.h" #include "libavutil/intreadwrite.h" -#include "libavutil/intfloat_readwrite.h" +#include "libavutil/intfloat.h" #include "libavutil/mathematics.h" #include "libavutil/avstring.h" #include "libavutil/dict.h" +#include "libavcodec/ac3tab.h" #include "avformat.h" #include "internal.h" #include "avio_internal.h" @@ -239,7 +241,7 @@ static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (!key) return 0; if (atom.size < 0) - return -1; + return AVERROR_INVALIDDATA; str_size = FFMIN3(sizeof(str)-1, str_size, atom.size); @@ -350,8 +352,12 @@ static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (err < 0) return err; if (c->found_moov && c->found_mdat && - (!pb->seekable || start_pos + a.size == avio_size(pb))) + ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) || + start_pos + a.size == avio_size(pb))) { + if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) + c->next_root_atom = start_pos + a.size; return 0; + } left = a.size - avio_tell(pb) + start_pos; if (left > 0) /* skip garbage at atom end */ avio_skip(pb, left); @@ -380,7 +386,8 @@ static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom) avio_rb32(pb); // version + flags entries = avio_rb32(pb); if (entries >= UINT_MAX / sizeof(*sc->drefs)) - return -1; + return AVERROR_INVALIDDATA; + av_free(sc->drefs); sc->drefs = av_mallocz(entries * sizeof(*sc->drefs)); if (!sc->drefs) return AVERROR(ENOMEM); @@ -392,7 +399,7 @@ static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom) int64_t next = avio_tell(pb) + size - 4; if (size < 12) - return -1; + return AVERROR_INVALIDDATA; dref->type = avio_rl32(pb); avio_rb32(pb); // version + flags @@ -548,6 +555,9 @@ static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom) acmod = (ac3info >> 11) & 0x7; lfeon = (ac3info >> 10) & 0x1; st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon; + st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod]; + if (lfeon) + st->codec->channel_layout |= AV_CH_LOW_FREQUENCY; st->codec->audio_service_type = bsmod; if (st->codec->channels > 1 && bsmod == 0x7) st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE; @@ -559,7 +569,8 @@ static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom) { AVStream *st; uint8_t version; - uint32_t flags, layout_tag, bitmap, num_descr; + uint32_t flags, layout_tag, bitmap, num_descr, label_mask; + int i; if (c->fc->nb_streams < 1) return 0; @@ -581,21 +592,27 @@ static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom) av_dlog(c->fc, "chan: size=%ld version=%u flags=%u layout=%u bitmap=%u num_descr=%u\n", atom.size, version, flags, layout_tag, bitmap, num_descr); -#if 0 - /* TODO: use the channel descriptions if the layout tag is 0 */ - int i; + label_mask = 0; for (i = 0; i < num_descr; i++) { uint32_t label, cflags; - float coords[3]; label = avio_rb32(pb); // mChannelLabel cflags = avio_rb32(pb); // mChannelFlags - AV_WN32(&coords[0], avio_rl32(pb)); // mCoordinates[0] - AV_WN32(&coords[1], avio_rl32(pb)); // mCoordinates[1] - AV_WN32(&coords[2], avio_rl32(pb)); // mCoordinates[2] + avio_rl32(pb); // mCoordinates[0] + avio_rl32(pb); // mCoordinates[1] + avio_rl32(pb); // mCoordinates[2] + if (layout_tag == 0) { + uint32_t mask_incr = ff_mov_get_channel_label(label); + if (mask_incr == 0) { + label_mask = 0; + break; + } + label_mask |= mask_incr; + } } -#endif - - st->codec->channel_layout = ff_mov_get_channel_layout(layout_tag, bitmap); + if (layout_tag == 0) + st->codec->channel_layout = label_mask; + else + st->codec->channel_layout = ff_mov_get_channel_layout(layout_tag, bitmap); return 0; } @@ -665,7 +682,7 @@ static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom) comp_brand_size = atom.size - 8; if (comp_brand_size < 0) - return -1; + return AVERROR_INVALIDDATA; comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */ if (!comp_brands_str) return AVERROR(ENOMEM); @@ -680,8 +697,10 @@ static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom) /* this atom should contain all header atoms */ static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom) { - if (mov_read_default(c, pb, atom) < 0) - return -1; + int ret; + + if ((ret = mov_read_default(c, pb, atom)) < 0) + return ret; /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */ /* so we don't parse the whole file if over a network */ c->found_moov=1; @@ -723,9 +742,10 @@ static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) sc = st->priv_data; version = avio_r8(pb); - if (version > 1) - return -1; /* unsupported */ - + if (version > 1) { + av_log_ask_for_sample(c, "unsupported version %d\n", version); + return AVERROR_PATCHWELCOME; + } avio_rb24(pb); /* flags */ if (version == 1) { creation_time = avio_rb64(pb); @@ -794,7 +814,7 @@ static int mov_read_smi(MOVContext *c, AVIOContext *pb, MOVAtom atom) st = c->fc->streams[c->fc->nb_streams-1]; if ((uint64_t)atom.size > (1<<30)) - return -1; + return AVERROR_INVALIDDATA; // currently SVQ3 decoder expect full STSD header - so let's fake it // this should be fixed and just SMI header should be passed @@ -841,6 +861,40 @@ static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; } +static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + AVStream *st; + unsigned mov_field_order; + enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN; + + if (c->fc->nb_streams < 1) // will happen with jp2 files + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + if (atom.size < 2) + return AVERROR_INVALIDDATA; + mov_field_order = avio_rb16(pb); + if ((mov_field_order & 0xFF00) == 0x0100) + decoded_field_order = AV_FIELD_PROGRESSIVE; + else if ((mov_field_order & 0xFF00) == 0x0200) { + switch (mov_field_order & 0xFF) { + case 0x01: decoded_field_order = AV_FIELD_TT; + break; + case 0x06: decoded_field_order = AV_FIELD_BB; + break; + case 0x09: decoded_field_order = AV_FIELD_TB; + break; + case 0x0E: decoded_field_order = AV_FIELD_BT; + break; + } + } + if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) { + av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order); + } + st->codec->field_order = decoded_field_order; + + return 0; +} + /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */ static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom) { @@ -853,10 +907,10 @@ static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom) st= c->fc->streams[c->fc->nb_streams-1]; size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE; if (size > INT_MAX || (uint64_t)atom.size > INT_MAX) - return -1; + return AVERROR_INVALIDDATA; buf= av_realloc(st->codec->extradata, size); if (!buf) - return -1; + return AVERROR(ENOMEM); st->codec->extradata= buf; buf+= st->codec->extradata_size; st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE; @@ -875,7 +929,7 @@ static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom) st = c->fc->streams[c->fc->nb_streams-1]; if ((uint64_t)atom.size > (1<<30)) - return -1; + return AVERROR_INVALIDDATA; if (st->codec->codec_id == CODEC_ID_QDM2 || st->codec->codec_id == CODEC_ID_QDMC) { // pass all frma atom to codec, needed at least for QDMC and QDM2 @@ -886,8 +940,9 @@ static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom) st->codec->extradata_size = atom.size; avio_read(pb, st->codec->extradata, atom.size); } else if (atom.size > 8) { /* to read frma, esds atoms */ - if (mov_read_default(c, pb, atom) < 0) - return -1; + int ret; + if ((ret = mov_read_default(c, pb, atom)) < 0) + return ret; } else avio_skip(pb, atom.size); return 0; @@ -906,8 +961,17 @@ static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom) st = c->fc->streams[c->fc->nb_streams-1]; if ((uint64_t)atom.size > (1<<30)) - return -1; - + return AVERROR_INVALIDDATA; + + if (atom.size >= 10) { + // Broken files created by legacy versions of Libav and FFmpeg will + // wrap a whole fiel atom inside of a glbl atom. + unsigned size = avio_rb32(pb); + unsigned type = avio_rl32(pb); + avio_seek(pb, -8, SEEK_CUR); + if (type == MKTAG('f','i','e','l') && size == atom.size) + return mov_read_default(c, pb, atom); + } av_free(st->codec->extradata); st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE); if (!st->codec->extradata) @@ -917,6 +981,32 @@ static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; } +static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + AVStream *st; + uint8_t profile_level; + + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + + if (atom.size >= (1<<28) || atom.size < 7) + return AVERROR_INVALIDDATA; + + profile_level = avio_r8(pb); + if (profile_level & 0xf0 != 0xc0) + return 0; + + av_free(st->codec->extradata); + st->codec->extradata = av_mallocz(atom.size - 7 + FF_INPUT_BUFFER_PADDING_SIZE); + if (!st->codec->extradata) + return AVERROR(ENOMEM); + st->codec->extradata_size = atom.size - 7; + avio_seek(pb, 6, SEEK_CUR); + avio_read(pb, st->codec->extradata, st->codec->extradata_size); + return 0; +} + /** * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself, * but can have extradata appended at the end after the 40 bytes belonging @@ -933,7 +1023,7 @@ static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom) st = c->fc->streams[c->fc->nb_streams-1]; if ((uint64_t)atom.size > (1<<30)) - return -1; + return AVERROR_INVALIDDATA; av_free(st->codec->extradata); st->codec->extradata = av_mallocz(atom.size - 40 + FF_INPUT_BUFFER_PADDING_SIZE); @@ -964,7 +1054,7 @@ static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (!entries) return 0; if (entries >= UINT_MAX/sizeof(int64_t)) - return -1; + return AVERROR_INVALIDDATA; sc->chunk_offsets = av_malloc(entries * sizeof(int64_t)); if (!sc->chunk_offsets) @@ -978,7 +1068,7 @@ static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom) for (i=0; ichunk_offsets[i] = avio_rb64(pb); else - return -1; + return AVERROR_INVALIDDATA; return 0; } @@ -1046,7 +1136,7 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) if (st->codec->codec_tag && st->codec->codec_tag != format && - (c->fc->video_codec_id ? ff_codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id + (c->fc->video_codec_id ? ff_codec_get_id(ff_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 @@ -1064,7 +1154,7 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) sc->dref_id= dref_id; st->codec->codec_tag = format; - id = ff_codec_get_id(codec_movaudio_tags, format); + id = ff_codec_get_id(ff_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, av_bswap32(format)&0xFFFF); @@ -1072,7 +1162,7 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) st->codec->codec_type = AVMEDIA_TYPE_AUDIO; } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO && /* do not overwrite codec type */ format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */ - id = ff_codec_get_id(codec_movvideo_tags, format); + id = ff_codec_get_id(ff_codec_movvideo_tags, format); if (id <= 0) id = ff_codec_get_id(ff_codec_bmp_tags, format); if (id > 0) @@ -1091,6 +1181,7 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) { unsigned int color_depth, len; int color_greyscale; + int color_table_id; st->codec->codec_id = id; avio_rb16(pb); /* version */ @@ -1118,9 +1209,9 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) st->codec->codec_tag=MKTAG('I', '4', '2', '0'); st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */ - st->codec->color_table_id = avio_rb16(pb); /* colortable id */ + color_table_id = avio_rb16(pb); /* colortable id */ av_dlog(c->fc, "depth %d, ctab id %d\n", - st->codec->bits_per_coded_sample, st->codec->color_table_id); + st->codec->bits_per_coded_sample, color_table_id); /* figure out the palette situation */ color_depth = st->codec->bits_per_coded_sample & 0x1F; color_greyscale = st->codec->bits_per_coded_sample & 0x20; @@ -1147,7 +1238,7 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) if (color_index < 0) color_index = 0; } - } else if (st->codec->color_table_id) { + } else if (color_table_id) { const uint8_t *color_table; /* if flag bit 3 is set, use the default palette */ color_count = 1 << color_depth; @@ -1218,7 +1309,7 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) avio_rb32(pb); /* bytes per sample */ } else if (version==2) { avio_rb32(pb); /* sizeof struct only */ - st->codec->sample_rate = av_int2dbl(avio_rb64(pb)); /* float 64 */ + st->codec->sample_rate = av_int2double(avio_rb64(pb)); /* float 64 */ st->codec->channels = avio_rb32(pb); avio_rb32(pb); /* always 0x7F000000 */ st->codec->bits_per_coded_sample = avio_rb32(pb); /* bits per channel if sound is uncompressed */ @@ -1287,8 +1378,9 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */ a.size = size - (avio_tell(pb) - start_pos); if (a.size > 8) { - if (mov_read_default(c, pb, a) < 0) - return -1; + int ret; + if ((ret = mov_read_default(c, pb, a)) < 0) + return ret; } else if (a.size > 0) avio_skip(pb, a.size); } @@ -1304,7 +1396,7 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) c->dv_demux = avpriv_dv_init_demux(c->dv_fctx); if (!c->dv_demux) { av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n"); - return -1; + return AVERROR(ENOMEM); } sc->dv_audio_container = 1; st->codec->codec_id = CODEC_ID_PCM_S16LE; @@ -1315,20 +1407,16 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) // 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; case CODEC_ID_AMR_NB: st->codec->channels= 1; /* really needed */ /* force sample rate for amr, stsd in 3gp does not store sample rate */ st->codec->sample_rate = 8000; - /* force frame_size, too, samples_per_frame isn't always set properly */ - st->codec->frame_size = 160; break; case CODEC_ID_AMR_WB: st->codec->channels = 1; st->codec->sample_rate = 16000; - st->codec->frame_size = 320; break; case CODEC_ID_MP2: case CODEC_ID_MP3: @@ -1338,12 +1426,10 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) case CODEC_ID_GSM: case CODEC_ID_ADPCM_MS: case CODEC_ID_ADPCM_IMA_WAV: - st->codec->frame_size = sc->samples_per_frame; st->codec->block_align = sc->bytes_per_frame; break; case CODEC_ID_ALAC: if (st->codec->extradata_size == 36) { - st->codec->frame_size = AV_RB32(st->codec->extradata+12); st->codec->channels = AV_RB8 (st->codec->extradata+21); st->codec->sample_rate = AV_RB32(st->codec->extradata+32); } @@ -1387,7 +1473,7 @@ static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (!entries) return 0; if (entries >= UINT_MAX / sizeof(*sc->stsc_data)) - return -1; + return AVERROR_INVALIDDATA; sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data)); if (!sc->stsc_data) return AVERROR(ENOMEM); @@ -1416,7 +1502,7 @@ static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom) entries = avio_rb32(pb); if (entries >= UINT_MAX / sizeof(*sc->stps_data)) - return -1; + return AVERROR_INVALIDDATA; sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data)); if (!sc->stps_data) return AVERROR(ENOMEM); @@ -1448,8 +1534,13 @@ static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom) av_dlog(c->fc, "keyframe_count = %d\n", entries); + if (!entries) + { + sc->keyframe_absent = 1; + return 0; + } if (entries >= UINT_MAX / sizeof(int)) - return -1; + return AVERROR_INVALIDDATA; sc->keyframes = av_malloc(entries * sizeof(int)); if (!sc->keyframes) return AVERROR(ENOMEM); @@ -1498,13 +1589,13 @@ static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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; + return AVERROR_INVALIDDATA; } if (!entries) return 0; if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size) - return -1; + return AVERROR_INVALIDDATA; sc->sample_sizes = av_malloc(entries * sizeof(int)); if (!sc->sample_sizes) return AVERROR(ENOMEM); @@ -1520,13 +1611,15 @@ static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (avio_read(pb, buf, num_bytes) < num_bytes) { av_freep(&sc->sample_sizes); av_free(buf); - return -1; + return AVERROR_INVALIDDATA; } init_get_bits(&gb, buf, 8*num_bytes); - for (i=0; isample_sizes[i] = get_bits_long(&gb, field_size); + sc->data_size += sc->sample_sizes[i]; + } av_free(buf); return 0; @@ -1582,6 +1675,7 @@ static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom) st->nb_frames= total_sample_count; if (duration) st->duration= duration; + sc->track_end = duration; return 0; } @@ -1605,7 +1699,7 @@ static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (!entries) return 0; if (entries >= UINT_MAX / sizeof(*sc->ctts_data)) - return -1; + return AVERROR_INVALIDDATA; sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data)); if (!sc->ctts_data) return AVERROR(ENOMEM); @@ -1637,6 +1731,7 @@ static void mov_build_index(MOVContext *mov, AVStream *st) unsigned int stps_index = 0; unsigned int i, j; uint64_t stream_size = 0; + AVIndexEntry *mem; /* adjust first dts according to edit list */ if (sc->time_offset && mov->time_scale > 0) { @@ -1665,12 +1760,13 @@ static void mov_build_index(MOVContext *mov, AVStream *st) if (!sc->sample_count) return; - if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries)) + if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries) return; - st->index_entries = av_malloc(sc->sample_count*sizeof(*st->index_entries)); - if (!st->index_entries) + mem = av_realloc(st->index_entries, (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries)); + if (!mem) return; - st->index_entries_allocated_size = sc->sample_count*sizeof(*st->index_entries); + st->index_entries = mem; + st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries); for (i = 0; i < sc->chunk_count; i++) { current_offset = sc->chunk_offsets[i]; @@ -1684,7 +1780,7 @@ static void mov_build_index(MOVContext *mov, AVStream *st) return; } - if (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index]) { + if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) { keyframe = 1; if (stss_index + 1 < sc->keyframe_count) stss_index++; @@ -1731,7 +1827,8 @@ static void mov_build_index(MOVContext *mov, AVStream *st) unsigned count, chunk_count; chunk_samples = sc->stsc_data[i].count; - if (sc->samples_per_frame && chunk_samples % sc->samples_per_frame) { + if (i != sc->stsc_count - 1 && + sc->samples_per_frame && chunk_samples % sc->samples_per_frame) { av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n"); return; } @@ -1753,12 +1850,13 @@ static void mov_build_index(MOVContext *mov, AVStream *st) } av_dlog(mov->fc, "chunk count %d\n", total); - if (total >= UINT_MAX / sizeof(*st->index_entries)) + if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries) return; - st->index_entries = av_malloc(total*sizeof(*st->index_entries)); - if (!st->index_entries) + mem = av_realloc(st->index_entries, (st->nb_index_entries + total) * sizeof(*st->index_entries)); + if (!mem) return; - st->index_entries_allocated_size = total*sizeof(*st->index_entries); + st->index_entries = mem; + st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries); // populate index for (i = 0; i < sc->chunk_count; i++) { @@ -1888,13 +1986,6 @@ static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom) avpriv_set_pts_info(st, 64, 1, sc->time_scale); - if (st->codec->codec_type == AVMEDIA_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); - av_dlog(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) { @@ -2069,7 +2160,7 @@ static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) track_id = avio_rb32(pb); if (!track_id) - return -1; + return AVERROR_INVALIDDATA; frag->track_id = track_id; for (i = 0; i < c->trex_count; i++) if (c->trex_data[i].track_id == frag->track_id) { @@ -2078,17 +2169,19 @@ static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) } if (!trex) { av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n"); - return -1; + return AVERROR_INVALIDDATA; } - if (flags & 0x01) frag->base_data_offset = avio_rb64(pb); - else frag->base_data_offset = frag->moof_offset; - if (flags & 0x02) frag->stsd_id = avio_rb32(pb); - else frag->stsd_id = trex->stsd_id; + frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ? + avio_rb64(pb) : frag->moof_offset; + frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id; - frag->duration = flags & 0x08 ? avio_rb32(pb) : trex->duration; - frag->size = flags & 0x10 ? avio_rb32(pb) : trex->size; - frag->flags = flags & 0x20 ? avio_rb32(pb) : trex->flags; + frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ? + avio_rb32(pb) : trex->duration; + frag->size = flags & MOV_TFHD_DEFAULT_SIZE ? + avio_rb32(pb) : trex->size; + frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ? + avio_rb32(pb) : trex->flags; av_dlog(c->fc, "frag flags 0x%x\n", frag->flags); return 0; } @@ -2104,7 +2197,7 @@ static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom) MOVTrackExt *trex; if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data)) - return -1; + return AVERROR_INVALIDDATA; trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data)); if (!trex) return AVERROR(ENOMEM); @@ -2130,7 +2223,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) int64_t dts; int data_offset = 0; unsigned entries, first_sample_flags = frag->flags; - int flags, distance, i; + int flags, distance, i, found_keyframe = 0; for (i = 0; i < c->fc->nb_streams; i++) { if (c->fc->streams[i]->id == frag->track_id) { @@ -2140,7 +2233,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) } if (!st) { av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id); - return -1; + return AVERROR_INVALIDDATA; } sc = st->priv_data; if (sc->pseudo_stream_id+1 != frag->stsd_id) @@ -2167,16 +2260,16 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) sc->ctts_count++; } if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data)) - return -1; + return AVERROR_INVALIDDATA; 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; - if (flags & 0x001) data_offset = avio_rb32(pb); - if (flags & 0x004) first_sample_flags = avio_rb32(pb); - dts = st->duration - sc->time_offset; + if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb); + if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb); + dts = sc->track_end - sc->time_offset; offset = frag->base_data_offset + data_offset; distance = 0; av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags); @@ -2184,16 +2277,22 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) unsigned sample_size = frag->size; int sample_flags = i ? frag->flags : first_sample_flags; unsigned sample_duration = frag->duration; - int keyframe; + int keyframe = 0; - if (flags & 0x100) sample_duration = avio_rb32(pb); - if (flags & 0x200) sample_size = avio_rb32(pb); - if (flags & 0x400) sample_flags = avio_rb32(pb); + if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb); + if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb); + if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb); sc->ctts_data[sc->ctts_count].count = 1; - sc->ctts_data[sc->ctts_count].duration = (flags & 0x800) ? avio_rb32(pb) : 0; + sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ? + avio_rb32(pb) : 0; sc->ctts_count++; - if ((keyframe = st->codec->codec_type == AVMEDIA_TYPE_AUDIO || - (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000)) + if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) + keyframe = 1; + else if (!found_keyframe) + keyframe = found_keyframe = + !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC | + MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES)); + if (keyframe) distance = 0; av_add_index_entry(st, offset, dts, sample_size, distance, keyframe ? AVINDEX_KEYFRAME : 0); @@ -2203,9 +2302,10 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) distance++; dts += sample_duration; offset += sample_size; + sc->data_size += sample_size; } frag->moof_offset = offset; - st->duration = dts + sc->time_offset; + st->duration = sc->track_end = dts + sc->time_offset; return 0; } @@ -2243,14 +2343,14 @@ static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom) avio_rb32(pb); /* dcom atom */ if (avio_rl32(pb) != MKTAG('d','c','o','m')) - return -1; + return AVERROR_INVALIDDATA; if (avio_rl32(pb) != MKTAG('z','l','i','b')) { av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !"); - return -1; + return AVERROR_INVALIDDATA; } avio_rb32(pb); /* cmvd atom */ if (avio_rl32(pb) != MKTAG('c','m','v','d')) - return -1; + return AVERROR_INVALIDDATA; moov_len = avio_rb32(pb); /* uncompressed size */ cmov_len = atom.size - 6 * 4; @@ -2276,7 +2376,7 @@ free_and_return: return ret; #else av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n"); - return -1; + return AVERROR(ENOSYS); #endif } @@ -2295,7 +2395,7 @@ static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom) edit_count = avio_rb32(pb); /* entries */ if ((uint64_t)edit_count*12+8 > atom.size) - return -1; + return AVERROR_INVALIDDATA; for (i=0; ipb, cur_pos, SEEK_SET); } -static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap) +static int mov_read_close(AVFormatContext *s) +{ + MOVContext *mov = s->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; 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) + avio_close(sc->pb); + } + + 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; +} + +static int mov_read_header(AVFormatContext *s) { MOVContext *mov = s->priv_data; AVIOContext *pb = s->pb; @@ -2510,17 +2644,29 @@ static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap) /* check MOV header */ if ((err = mov_read_default(mov, pb, atom)) < 0) { av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err); + mov_read_close(s); return err; } if (!mov->found_moov) { av_log(s, AV_LOG_ERROR, "moov atom not found\n"); - return -1; + mov_read_close(s); + return AVERROR_INVALIDDATA; } av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb)); if (pb->seekable && mov->chapter_track > 0) mov_read_chapters(s); + if (mov->trex_data) { + int i; + for (i = 0; i < s->nb_streams; i++) { + AVStream *st = s->streams[i]; + MOVStreamContext *sc = st->priv_data; + if (st->duration) + st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration; + } + } + return 0; } @@ -2561,8 +2707,11 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) sample = mov_find_next_sample(s, &st); if (!sample) { mov->found_mdat = 0; - if (s->pb->seekable|| - mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 || + if (!mov->next_root_atom) + return AVERROR_EOF; + avio_seek(s->pb, mov->next_root_atom, SEEK_SET); + mov->next_root_atom = 0; + if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 || s->pb->eof_reached) return AVERROR_EOF; av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb)); @@ -2576,7 +2725,7 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) if (avio_seek(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; + return AVERROR_INVALIDDATA; } ret = av_get_packet(sc->pb, pkt, sample->size); if (ret < 0) @@ -2606,7 +2755,7 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) pkt->stream_index = sc->ffindex; pkt->dts = sample->timestamp; - if (sc->ctts_data) { + if (sc->ctts_data && sc->ctts_index < sc->ctts_count) { pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration; /* update ctts context */ sc->ctts_sample++; @@ -2643,7 +2792,7 @@ static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp) sample = 0; if (sample < 0) /* not sure what to do */ - return -1; + return AVERROR_INVALIDDATA; sc->current_sample = sample; av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample); /* adjust ctts index */ @@ -2670,14 +2819,14 @@ static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti int i; if (stream_index >= s->nb_streams) - return -1; + return AVERROR_INVALIDDATA; if (sample_time < 0) sample_time = 0; st = s->streams[stream_index]; sample = mov_seek_stream(s, st, sample_time, flags); if (sample < 0) - return -1; + return sample; /* adjust seek timestamp to found sample timestamp */ seek_timestamp = st->index_entries[sample].timestamp; @@ -2693,39 +2842,6 @@ static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti return 0; } -static int mov_read_close(AVFormatContext *s) -{ - MOVContext *mov = s->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; 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) - avio_close(sc->pb); - } - - 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; -} - AVInputFormat ff_mov_demuxer = { .name = "mov,mp4,m4a,3gp,3g2,mj2", .long_name = NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),