From patchwork Fri Dec 2 06:25:07 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhangjiejun1992@gmail.com X-Patchwork-Id: 1655 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.65.86 with SMTP id o83csp131156vsa; Thu, 1 Dec 2016 22:41:48 -0800 (PST) X-Received: by 10.28.15.138 with SMTP id 132mr1263660wmp.41.1480660908573; Thu, 01 Dec 2016 22:41:48 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id x125si1675879wmd.163.2016.12.01.22.41.47; Thu, 01 Dec 2016 22:41:48 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3E59C689EAF; Fri, 2 Dec 2016 08:41:37 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg0-f67.google.com (mail-pg0-f67.google.com [74.125.83.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0C094689759 for ; Fri, 2 Dec 2016 08:41:29 +0200 (EET) Received: by mail-pg0-f67.google.com with SMTP id e9so6962465pgc.1 for ; Thu, 01 Dec 2016 22:41:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=AvH9DWHf6BzZ0VAL2DA+ZmrB8FzPYCa5Vg+0Z4vPM9k=; b=yvDMyTafVMMSbhuxMS5/GDyJvjHZRYUGNbI8OxP0ule9K65YsrC2ARKY2QcEaPD65y xisSawKdldXka5ia/q7xOTaATV+YCDrn3ykD7jsemmIzenytwWn4Ye/KzJQkmI8rfwDB Qamp/qG22O2F12Di/m2TASAg9amNFmidqvxlojjH+bUIsrrgVYJIWKWbymkEyDCkgZ0L aHojMR+MZRsRVd5Nh3R2xjczLCo6eFLc1AV1W1ZfzbjY0l9qX+U81KYSRM6o/lK9xAd5 oUQC2mMNpjXzuPd6nM5YsNid74uj7+hRkcdUwXU1dOxbKP/ZuDFbujcgkN/3/9K84MaH 1ndQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=AvH9DWHf6BzZ0VAL2DA+ZmrB8FzPYCa5Vg+0Z4vPM9k=; b=i/tkDMAgoBt+pioo9DTyYzVmabh2sKpVKPM555MEjHvas0TGwSQoraKvYfpQDX+eZV d/FL9fiozpWbBLOClt+VHAfmdwOa/ozK7muwKjpLOptMjWGflIBds6JqXQWI/tdC22Od eg9aKccA1oWtBSG0KnUHz2HVGeeTo0Waru7gGE04V7usichDg+6Ow+2X7kCuAJFtbhzf 2iZkvvkpYhlvM9F38CjmDtv/PRkprAQYr0h6qnmPZS4QikdDrZouDK0gWUzNwM+gzCE/ xfpg+kClWqZyAhPafYHVFdsPcQoG4X6FUgQ6GWaXqEV7RY6HIfffbtXkzBi0PUtmD7bs 20Qw== X-Gm-Message-State: AKaTC01AIXUyClUS5GJvELUbuYMXOanB5mdEKkshLjIk7lw6sJQBSIY6ugT74z+Muvmm1Q== X-Received: by 10.99.226.83 with SMTP id y19mr76271385pgj.147.1480659928034; Thu, 01 Dec 2016 22:25:28 -0800 (PST) Received: from localhost.localdomain (ec2-52-77-239-182.ap-southeast-1.compute.amazonaws.com. [52.77.239.182]) by smtp.gmail.com with ESMTPSA id e11sm4498331pgp.10.2016.12.01.22.25.26 (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 01 Dec 2016 22:25:27 -0800 (PST) From: zhangjiejun1992@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Fri, 2 Dec 2016 14:25:07 +0800 Message-Id: <20161202062507.83287-1-zhangjiejun1992@gmail.com> X-Mailer: git-send-email 2.9.3 (Apple Git-75) Subject: [FFmpeg-devel] [PATCH] libavformat/mov: fix multiple stsd handling of MOV files with edit list X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Jiejun Zhang MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Jiejun Zhang When an edit list exists in a MOV file, counting by stscs no longer works because stscs' order is different from the actual timeline. This commit adds stsd-change markers to the actual timeline and changes stsd according to these markers. Test sample: https://www.dropbox.com/s/qz0ort4znqq9jdy/attachment_video.mov?dl=0 Run `ffmpeg -i attachment_video.mov -f image2 frames%03d.png`. The git-master version cannot decode frame 327 to 330 because a wrong stsd is used. After applying this patch they are decoded correctly. Signed-off-by: Jiejun Zhang --- libavformat/isom.h | 13 +++++- libavformat/mov.c | 125 +++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 109 insertions(+), 29 deletions(-) diff --git a/libavformat/isom.h b/libavformat/isom.h index 02bfedd..141237a 100644 --- a/libavformat/isom.h +++ b/libavformat/isom.h @@ -115,6 +115,14 @@ typedef struct MOVFragmentIndex { MOVFragmentIndexItem *items; } MOVFragmentIndex; +/** + * An entry for: stsd changes to `stsd_id` at sample `sample_index` (inclusive) + */ +typedef struct MOVStsdChangeEntry { + int64_t sample_index; + int stsd_id; +} MOVStsdChangeEntry; + typedef struct MOVStreamContext { AVIOContext *pb; int pb_is_copied; @@ -128,12 +136,13 @@ typedef struct MOVStreamContext { MOVStts *ctts_data; unsigned int stsc_count; MOVStsc *stsc_data; - int stsc_index; - int stsc_sample; unsigned int stps_count; unsigned *stps_data; ///< partial sync sample for mpeg-2 open gop MOVElst *elst_data; unsigned int elst_count; + MOVStsdChangeEntry *stsd_change_data; + int stsd_change_count; + int stsd_change_index; int ctts_index; int ctts_sample; unsigned int sample_size; ///< may contain value calculated from stsd or value from stsz atom diff --git a/libavformat/mov.c b/libavformat/mov.c index 88a79da..e5f7f72 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -2901,6 +2901,35 @@ static int64_t add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, return index; } +static int add_stsd_change_entry(MOVStreamContext *msc, int64_t sample_index, int stsd_id, + unsigned int* allocated_size) +{ + MOVStsdChangeEntry *entries; + const size_t min_size_needed = (msc->stsd_change_count + 1) * sizeof(MOVStsdChangeEntry); + + const size_t requested_size = + min_size_needed > *allocated_size ? + FFMAX(min_size_needed, 2 * (*allocated_size)) : + min_size_needed; + + if ((unsigned)msc->stsd_change_count + 1 >= UINT_MAX / sizeof(MOVStsdChangeEntry)) + return -1; + + entries = av_fast_realloc(msc->stsd_change_data, + allocated_size, + requested_size); + + if (!entries) + return -1; + + msc->stsd_change_data = entries; + entries[msc->stsd_change_count].sample_index = sample_index; + entries[msc->stsd_change_count].stsd_id = stsd_id; + msc->stsd_change_count++; + + return 0; +} + /** * Rewrite timestamps of index entries in the range [end_index - frame_duration_buffer_size, end_index) * by subtracting end_ts successively by the amounts given in frame_duration_buffer. @@ -2984,6 +3013,8 @@ static void mov_fix_index(MOVContext *mov, AVStream *st) int num_discarded_begin = 0; int first_non_zero_audio_edit = -1; int packet_skip_samples = 0; + int stsc_index = 0; + int stsc_sample = 0; if (!msc->elst_data || msc->elst_count <= 0 || nb_old <= 0) { return; @@ -3012,6 +3043,11 @@ static void mov_fix_index(MOVContext *mov, AVStream *st) start_dts = edit_list_dts_entry_end; + msc->last_stsd_index = -1; + unsigned int stsd_change_data_allocated_size = 0; + msc->stsd_change_data = NULL; + msc->stsd_change_count = 0; + while (get_edit_list_entry(mov, msc, edit_list_index, &edit_list_media_time, &edit_list_duration, mov->time_scale)) { av_log(mov->fc, AV_LOG_DEBUG, "Processing st: %d, edit list %"PRId64" - media time: %"PRId64", duration: %"PRId64"\n", @@ -3073,6 +3109,20 @@ static void mov_fix_index(MOVContext *mov, AVStream *st) } current = e_old + index; + // adjust stsd index + int time_sample = 0; + for (int i = 0; i < msc->stsc_count; i++) { + int next = time_sample + mov_get_stsc_samples(msc, i); + if (next > index) { + stsc_index = i; + stsc_sample = index - time_sample; + break; + } + time_sample = next; + } + + av_log(mov->fc, AV_LOG_INFO, "stream %d, adjusted stsc_index: %d, stsc_sample: %d\n", st->index, stsc_index, stsc_sample); + ctts_index_old = 0; ctts_sample_old = 0; @@ -3176,12 +3226,36 @@ static void mov_fix_index(MOVContext *mov, AVStream *st) } } - if (add_index_entry(st, current->pos, edit_list_dts_counter, current->size, - current->min_distance, flags) == -1) { + int index_entry_index = add_index_entry(st, current->pos, edit_list_dts_counter, current->size, + current->min_distance, flags); + if (index_entry_index == -1) { av_log(mov->fc, AV_LOG_ERROR, "Cannot add index entry\n"); break; } + if (msc->stsc_data) { + if (msc->stsc_data[stsc_index].id > 0 && + msc->stsc_data[stsc_index].id - 1 < msc->stsd_count && + msc->stsc_data[stsc_index].id - 1 != msc->last_stsd_index) { + msc->last_stsd_index = msc->stsc_data[stsc_index].id - 1; + int ret = add_stsd_change_entry(msc, index_entry_index, msc->last_stsd_index, &stsd_change_data_allocated_size); + if (ret < 0) { + av_log(mov->fc, AV_LOG_ERROR, "Cannot add stsd change entry\n"); + break; + } + av_log(mov->fc, AV_LOG_INFO, "added a stsd change entry sample_index: %d, stsd_index:%d\n", + index_entry_index, + msc->last_stsd_index); + } + + stsc_sample++; + if (stsc_index < msc->stsc_count - 1 && + mov_get_stsc_samples(msc, stsc_index) == stsc_sample) { + stsc_index++; + stsc_sample = 0; + } + } + // Only start incrementing DTS in frame_duration amounts, when we encounter a frame in edit list. if (edit_list_start_encountered > 0) { edit_list_dts_counter = edit_list_dts_counter + frame_duration; @@ -3206,6 +3280,7 @@ static void mov_fix_index(MOVContext *mov, AVStream *st) } } } + msc->last_stsd_index = 0; // Update av stream length st->duration = edit_list_dts_entry_end - start_dts; @@ -5385,6 +5460,7 @@ static int mov_read_close(AVFormatContext *s) av_freep(&sc->stts_data); av_freep(&sc->stps_data); av_freep(&sc->elst_data); + av_freep(&sc->stsd_change_data); av_freep(&sc->rap_group); av_freep(&sc->display_matrix); @@ -5807,9 +5883,6 @@ static int mov_change_extradata(MOVStreamContext *sc, AVPacket *pkt) uint8_t *side, *extradata; int extradata_size; - /* Save the current index. */ - sc->last_stsd_index = sc->stsc_data[sc->stsc_index].id - 1; - /* Notify the decoder that extradata changed. */ extradata_size = sc->extradata_size[sc->last_stsd_index]; extradata = sc->extradata[sc->last_stsd_index]; @@ -5923,21 +5996,22 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) pkt->pos = sample->pos; /* Multiple stsd handling. */ - if (sc->stsc_data) { - /* Keep track of the stsc index for the given sample, then check - * if the stsd index is different from the last used one. */ - sc->stsc_sample++; - if (sc->stsc_index < sc->stsc_count - 1 && - mov_get_stsc_samples(sc, sc->stsc_index) == sc->stsc_sample) { - sc->stsc_index++; - sc->stsc_sample = 0; - /* Do not check indexes after a switch. */ - } else if (sc->stsc_data[sc->stsc_index].id > 0 && - sc->stsc_data[sc->stsc_index].id - 1 < sc->stsd_count && - sc->stsc_data[sc->stsc_index].id - 1 != sc->last_stsd_index) { + if (sc->stsd_change_data) { + if (sc->stsd_change_index + 1 < sc->stsd_change_count && + // sc->current_sample is already the next sample, so subtract 1 + sc->stsd_change_data[sc->stsd_change_index + 1].sample_index == sc->current_sample - 1) { + ++sc->stsd_change_index; + } + if (sc->last_stsd_index != sc->stsd_change_data[sc->stsd_change_index].stsd_id) { + av_log(mov->fc, AV_LOG_INFO, "stsd id changed from %d to %d, at sample %d\n", + sc->last_stsd_index, + sc->stsd_change_data[sc->stsd_change_index].stsd_id, + sc->current_sample - 1); + sc->last_stsd_index = sc->stsd_change_data[sc->stsd_change_index].stsd_id; ret = mov_change_extradata(sc, pkt); - if (ret < 0) + if (ret < 0) { return ret; + } } } @@ -6012,16 +6086,13 @@ static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, } } - /* adjust stsd index */ - time_sample = 0; - for (i = 0; i < sc->stsc_count; i++) { - int next = time_sample + mov_get_stsc_samples(sc, i); - if (next > sc->current_sample) { - sc->stsc_index = i; - sc->stsc_sample = sc->current_sample - time_sample; - break; + /* adjust stsd change index */ + for (i = 0; i < sc->stsd_change_count; i++) { + if (sc->current_sample >= sc->stsd_change_data[i].sample_index && + (i + 1 == sc->stsd_change_count || + sc->current_sample < sc->stsd_change_data[i + 1].sample_index)) { + sc->stsd_change_index = i; } - time_sample = next; } ret = mov_seek_auxiliary_info(s, sc);