From patchwork Tue Jul 18 18:49:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dale Curtis X-Patchwork-Id: 4359 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp1944131vsb; Tue, 18 Jul 2017 11:57:10 -0700 (PDT) X-Received: by 10.28.174.204 with SMTP id x195mr2575729wme.116.1500404230629; Tue, 18 Jul 2017 11:57:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1500404230; cv=none; d=google.com; s=arc-20160816; b=d6u7UNdAUqJn/69xqfTIxGvLIZJB+VfMoePjb+SMycvT+CJX0g7q39QhQNS3h0m762 HxG3sqE46cNMHcwGc7mLeQe/1gDLxTL/LPMTEAE16j2Awjw+7TmLxj1zmnzYhfeq2dDQ xqySq4D7UUhHqyC63oxhoyrYo3zcihXkDE5iO+doR9KD00qEQAXNnuc2e4472slhxHcZ jvhaeHh9qQcYPwQLVB/vKsABNRvroc4NVrphpCsEoZaadwCwX+c3Dhjjr5MSTzG9waoI xa8Mek2g480LtTt3WtX5nHyosN5azfZsAUoAddIs2EZ9YsdwDcTKfk8DuCPIS7kzYTfA GOlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:to :message-id:date:from:references:in-reply-to:mime-version :dkim-signature:dkim-signature:delivered-to :arc-authentication-results; bh=J89kqynE3N8+HfdpyWTZFem1jNWslCwWispcpA0wDZo=; b=ZPP7BhYewktGUCFXJtF5FCCvORmkALqw5eG937oQyNFu7BcL70dR41LjVtSJRzAKm6 4NGd036Ej7IEZbOrnjZJzWXKp3Ty21PUSLYuccI+/BJjhC0q0KcnBI9LNzt41MihSc8r V+1qWS/Qn4wme3ltkSpp9ykSj5BK2PInSGr/t7514LIVOUbYz3+Uv8S3jfHOSa96C6An fRqkcqbFB43003UDuLxkMoaGdf4BWLSdKUmx8KwECSKpVaNz13Cwca3tpJdl4LMvTNU8 myQ0D2AZxrO1dmzVU8Y6Te2709606q7/jPH7nr+U5o1MoN66wvwtUP9nEGEfDTdvmEe1 UWpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.b=rZtafF1F; dkim=neutral (body hash did not verify) header.i=@chromium.org header.b=arwADfQO; 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 sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id e139si13688210wmd.59.2017.07.18.11.57.09; Tue, 18 Jul 2017 11:57:10 -0700 (PDT) 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=@google.com header.b=rZtafF1F; dkim=neutral (body hash did not verify) header.i=@chromium.org header.b=arwADfQO; 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 sp=NONE dis=NONE) header.from=chromium.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3C8816899DC; Tue, 18 Jul 2017 21:57:00 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f179.google.com (mail-wr0-f179.google.com [209.85.128.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1E18068992F for ; Tue, 18 Jul 2017 21:56:54 +0300 (EEST) Received: by mail-wr0-f179.google.com with SMTP id 12so42145887wrb.1 for ; Tue, 18 Jul 2017 11:57:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to; bh=DEsP2kdiafNJRE3VJxmrRKlGPE7DBugTTTNKWUdZPrk=; b=rZtafF1FL+8/ZQFKq4IJe3ab3FEuKdFiIZ6KDWUz7Fx+drr23kICpjPZnzw0oYBJB2 YcAE3+f5OqV9550h1OryBMaht7Q+6f8VlEcxZxJ0JwfYoMwq0klL8Q1L6ABOMij+9F6T z+BpU4QLfEggjNKdF2U92WNJKgXPfBNl9bnTfW6wxkwOmYCO7UXEZbtIhrrEXh5LzFB4 b81EtTmLG8b6VpMqrvD5TTjR/OnDQfv4aCxdjIPopSSsHZbe2OubkpFmG2sxtoQhB1Rb EWe6SmjITgZRto5j2J9T1iNDMJ0ZcUfpFHlbBE2VqT+OqWL+2k3d0IjrHc3kPa1tKgFn 0D2A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to; bh=DEsP2kdiafNJRE3VJxmrRKlGPE7DBugTTTNKWUdZPrk=; b=arwADfQOSWdEKbUHoFewnQ/teRdh75sgb+2irQ/BdQEp1NiHIkw8yvsBF1K+l0iNs7 l/09w06Q3Kry/+h3XNhzK3phB2d7pjyJRy9VqpmoWWtJeqSXMEl8NbDUYKKG/48ZyrLi Wm9E6OhGpJr8yfzvYBWdaybnaLJMzlmjlhFkc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to; bh=DEsP2kdiafNJRE3VJxmrRKlGPE7DBugTTTNKWUdZPrk=; b=TTkmPYfkdydoqDSaGkMbqU2MTW41uqtFE/7IWaPCXadfGp2dhw00mCQNIvENLBPiaw 8jWkmKoWZY3QnaeVVoxarMFHkV3BUQu2D4irgew0meS65iTcW9OHTTxqp/c2BWUX2gro MfkqqOSBA6YVv2B2vUBL6O9atsM2E8IUXG2RcfPL+rABhYpZFUNxsGn5pDMtV/jQ5ua7 /iLQc4lQ+o87b63Lvx9zmnxO1AzZ5p/0MoA7ZEsmD8AOTUPtua2xzXtdboWkNb2FqiW0 2/FDDe2nB4XIjRpjVHBpnFONY2NaZX7F4qeM/L3hklv7X/KjXhflPwdvleUA1IHP4fWq 1DXw== X-Gm-Message-State: AIVw112lAlut/lNda24bedviUh+isCu5dMqddY9mjfARkpsjXXb5jjVM RB9r3LhDSwaLQtYCyYGElj1ZMNqsNInS/Y0= X-Received: by 10.223.133.35 with SMTP id 32mr2042796wrh.30.1500403787805; Tue, 18 Jul 2017 11:49:47 -0700 (PDT) MIME-Version: 1.0 Received: by 10.28.71.149 with HTTP; Tue, 18 Jul 2017 11:49:26 -0700 (PDT) In-Reply-To: References: From: Dale Curtis Date: Tue, 18 Jul 2017 11:49:26 -0700 X-Google-Sender-Auth: GzPEI83kgggp1oHvdNEneNEemEM Message-ID: To: FFmpeg development discussions and patches X-Content-Filtered-By: Mailman/MimeDel 2.1.20 Subject: Re: [FFmpeg-devel] [mov] Fix trampling of ctts during seeks when sidx support is enabled. 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Updated patch that fixes other ctts modification code to use the new ctts_allocated_size value; I've verified this passes fate. - dale On Tue, Jul 18, 2017 at 9:53 AM, Dale Curtis wrote: > Resending as it's own message per contribution rules. I've also attached > the patch in case the formatting gets trashed. > > When sidx box support is enabled, the code will skip reading all > trun boxes (each containing ctts entries for samples inthat box). > > If seeks are attempted before all ctts values are known, the old > code would dump ctts entries into the wrong location. These are > then used to compute pts values which leads to out of order and > incorrectly timestamped packets. > > This patch fixes ctts processing by always using the index returned > by av_add_index_entry() as the ctts_data index. When the index gains > new entries old values are reshuffled as appropriate. > > This approach makes sense since the mov demuxer is already relying > on the mapping of AVIndex entries to samples for correct demuxing. > > Notes for future improvement: > Probably there are other boxes (stts, stsc, etc) that are impacted > by this issue... this patch only attempts to fix ctts since it > completely breaks packet timestamping. > > This patch continues using an array for the ctts data, which is not > the most ideal given the rearrangement that needs to happen (via > memmove as new entries are read in). Ideally AVIndex and the ctts > data would be set-type structures so addition is always worst case > O(lg(n)) instead of the O(n^2) that exists now; this slowdown is > noticeable during seeks. > > Additionally since ctts samples from trun boxes always have a count > of 1, there's probably an opportunity to avoid the post-seek fixup > that iterates O(n) over all samples with an O(1) when no non-1 count > samples are present. > > Signed-off-by: Dale Curtis > --- > libavformat/isom.h | 1 + > libavformat/mov.c | 46 +++++++++++++++++++++++++++++++--------------- > 2 files changed, 32 insertions(+), 15 deletions(-) > > diff --git a/libavformat/isom.h b/libavformat/isom.h > index ff009b0896..fdd98c28f5 100644 > --- a/libavformat/isom.h > +++ b/libavformat/isom.h > @@ -137,6 +137,7 @@ typedef struct MOVStreamContext { > unsigned int stts_count; > MOVStts *stts_data; > unsigned int ctts_count; > + unsigned int ctts_allocated_size; > MOVStts *ctts_data; > unsigned int stsc_count; > MOVStsc *stsc_data; > diff --git a/libavformat/mov.c b/libavformat/mov.c > index 63f84be782..412290b435 100644 > --- a/libavformat/mov.c > +++ b/libavformat/mov.c > @@ -4297,11 +4297,6 @@ static int mov_read_trun(MOVContext *c, AVIOContext > *pb, MOVAtom atom) > } > if ((uint64_t)entries+sc->ctts_count >= > UINT_MAX/sizeof(*sc->ctts_data)) > return AVERROR_INVALIDDATA; > - if ((err = av_reallocp_array(&sc->ctts_data, entries + > sc->ctts_count, > - sizeof(*sc->ctts_data))) < 0) { > - sc->ctts_count = 0; > - return err; > - } > 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; > @@ -4312,26 +4307,28 @@ 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; > + unsigned ctts_duration = 0; > int keyframe = 0; > + int ctts_index = 0; > + int old_nb_index_entries = st->nb_index_entries; > > 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 & > MOV_TRUN_SAMPLE_CTS) ? > - avio_rb32(pb) : 0; > - mov_update_dts_shift(sc, sc->ctts_data[sc->ctts_count].duration); > + if (flags & MOV_TRUN_SAMPLE_CTS) ctts_duration = > avio_rb32(pb); > + > + mov_update_dts_shift(sc, ctts_duration); > if (frag->time != AV_NOPTS_VALUE) { > if (c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) { > int64_t pts = frag->time; > av_log(c->fc, AV_LOG_DEBUG, "found frag time %"PRId64 > " sc->dts_shift %d ctts.duration %d" > " sc->time_offset %"PRId64" flags & > MOV_TRUN_SAMPLE_CTS %d\n", pts, > - sc->dts_shift, sc->ctts_data[sc->ctts_count]. > duration, > + sc->dts_shift, ctts_duration, > sc->time_offset, flags & MOV_TRUN_SAMPLE_CTS); > dts = pts - sc->dts_shift; > if (flags & MOV_TRUN_SAMPLE_CTS) { > - dts -= sc->ctts_data[sc->ctts_count].duration; > + dts -= ctts_duration; > } else { > dts -= sc->time_offset; > } > @@ -4343,7 +4340,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext > *pb, MOVAtom atom) > } > frag->time = AV_NOPTS_VALUE; > } > - sc->ctts_count++; > + > if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) > keyframe = 1; > else > @@ -4352,11 +4349,30 @@ static int mov_read_trun(MOVContext *c, > AVIOContext *pb, MOVAtom atom) > MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES)); > if (keyframe) > distance = 0; > - err = av_add_index_entry(st, offset, dts, sample_size, distance, > - keyframe ? AVINDEX_KEYFRAME : 0); > - if (err < 0) { > + ctts_index = av_add_index_entry(st, offset, dts, sample_size, > distance, > + keyframe ? AVINDEX_KEYFRAME : 0); > + if (ctts_index >= 0 && old_nb_index_entries < > st->nb_index_entries) { > + unsigned int size_needed = st->nb_index_entries * > sizeof(*sc->ctts_data); > + unsigned int request_size = size_needed > > sc->ctts_allocated_size ? > + FFMAX(size_needed, 2 * sc->ctts_allocated_size) : > size_needed; > + sc->ctts_data = av_fast_realloc(sc->ctts_data, > &sc->ctts_allocated_size, request_size); > + if (!sc->ctts_data) { > + sc->ctts_count = 0; > + return AVERROR(ENOMEM); > + } > + > + if (ctts_index != old_nb_index_entries) { > + memmove(sc->ctts_data + ctts_index + 1, sc->ctts_data + > ctts_index, > + sizeof(*sc->ctts_data) * (sc->ctts_count - > ctts_index)); > + } > + > + sc->ctts_data[ctts_index].count = 1; > + sc->ctts_data[ctts_index].duration = ctts_duration; > + sc->ctts_count++; > + } else { > av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n"); > } > + > av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %u, offset > %"PRIx64", dts %"PRId64", " > "size %u, distance %d, keyframe %d\n", st->index, > sc->sample_count+i, > offset, dts, sample_size, distance, keyframe); > -- > 2.13.2.932.g7449e964c-goog > From 0f94ed2f3cca57bd6dc164e750e92efbbf5b612f Mon Sep 17 00:00:00 2001 From: Dale Curtis Date: Mon, 17 Jul 2017 17:38:09 -0700 Subject: [PATCH] Fix trampling of ctts during seeks when sidx support is enabled. When sidx box support is enabled, the code will skip reading all trun boxes (each containing ctts entries for samples inthat box). If seeks are attempted before all ctts values are known, the old code would dump ctts entries into the wrong location. These are then used to compute pts values which leads to out of order and incorrectly timestamped packets. This patch fixes ctts processing by always using the index returned by av_add_index_entry() as the ctts_data index. When the index gains new entries old values are reshuffled as appropriate. This approach makes sense since the mov demuxer is already relying on the mapping of AVIndex entries to samples for correct demuxing. Notes for future improvement: Probably there are other boxes (stts, stsc, etc) that are impacted by this issue... this patch only attempts to fix ctts since it completely breaks packet timestamping. This patch continues using an array for the ctts data, which is not the most ideal given the rearrangement that needs to happen (via memmove as new entries are read in). Ideally AVIndex and the ctts data would be set-type structures so addition is always worst case O(lg(n)) instead of the O(n^2) that exists now; this slowdown is noticeable during seeks. Additionally since ctts samples from trun boxes always have a count of 1, there's probably an opportunity to avoid the post-seek fixup that iterates O(n) over all samples with an O(1) when no non-1 count samples are present. Signed-off-by: Dale Curtis --- libavformat/isom.h | 1 + libavformat/mov.c | 58 ++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/libavformat/isom.h b/libavformat/isom.h index ff009b0896..fdd98c28f5 100644 --- a/libavformat/isom.h +++ b/libavformat/isom.h @@ -137,6 +137,7 @@ typedef struct MOVStreamContext { unsigned int stts_count; MOVStts *stts_data; unsigned int ctts_count; + unsigned int ctts_allocated_size; MOVStts *ctts_data; unsigned int stsc_count; MOVStsc *stsc_data; diff --git a/libavformat/mov.c b/libavformat/mov.c index 63f84be782..ab8e914581 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -2726,7 +2726,7 @@ static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (entries >= UINT_MAX / sizeof(*sc->ctts_data)) return AVERROR_INVALIDDATA; av_freep(&sc->ctts_data); - sc->ctts_data = av_realloc(NULL, entries * sizeof(*sc->ctts_data)); + sc->ctts_data = av_fast_realloc(NULL, &sc->ctts_allocated_size, entries * sizeof(*sc->ctts_data)); if (!sc->ctts_data) return AVERROR(ENOMEM); @@ -3046,7 +3046,6 @@ static void mov_fix_index(MOVContext *mov, AVStream *st) int64_t index; int64_t index_ctts_count; int flags; - unsigned int ctts_allocated_size = 0; int64_t start_dts = 0; int64_t edit_list_media_time_dts = 0; int64_t edit_list_start_encountered = 0; @@ -3081,6 +3080,7 @@ static void mov_fix_index(MOVContext *mov, AVStream *st) msc->ctts_count = 0; msc->ctts_index = 0; msc->ctts_sample = 0; + msc->ctts_allocated_size = 0; // If the dts_shift is positive (in case of negative ctts values in mov), // then negate the DTS by dts_shift @@ -3190,7 +3190,7 @@ static void mov_fix_index(MOVContext *mov, AVStream *st) ctts_sample_old++; if (ctts_sample_old == ctts_data_old[ctts_index_old].count) { if (add_ctts_entry(&msc->ctts_data, &msc->ctts_count, - &ctts_allocated_size, + &msc->ctts_allocated_size, ctts_data_old[ctts_index_old].count - edit_list_start_ctts_sample, ctts_data_old[ctts_index_old].duration) == -1) { av_log(mov->fc, AV_LOG_ERROR, "Cannot add CTTS entry %"PRId64" - {%"PRId64", %d}\n", @@ -3289,7 +3289,7 @@ static void mov_fix_index(MOVContext *mov, AVStream *st) if (ctts_data_old && ctts_sample_old != 0) { if (add_ctts_entry(&msc->ctts_data, &msc->ctts_count, - &ctts_allocated_size, + &msc->ctts_allocated_size, ctts_sample_old - edit_list_start_ctts_sample, ctts_data_old[ctts_index_old].duration) == -1) { av_log(mov->fc, AV_LOG_ERROR, "Cannot add CTTS entry %"PRId64" - {%"PRId64", %d}\n", @@ -4259,7 +4259,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, err; + int flags, distance, i; for (i = 0; i < c->fc->nb_streams; i++) { if (c->fc->streams[i]->id == frag->track_id) { @@ -4287,7 +4287,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (!sc->ctts_count && sc->sample_count) { /* Complement ctts table if moov atom doesn't have ctts atom. */ - ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data)); + ctts_data = av_fast_realloc(NULL, &sc->ctts_allocated_size, sizeof(*sc->ctts_data)); if (!ctts_data) return AVERROR(ENOMEM); sc->ctts_data = ctts_data; @@ -4297,11 +4297,6 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) } if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data)) return AVERROR_INVALIDDATA; - if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count, - sizeof(*sc->ctts_data))) < 0) { - sc->ctts_count = 0; - return err; - } 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; @@ -4312,26 +4307,28 @@ 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; + unsigned ctts_duration = 0; int keyframe = 0; + int ctts_index = 0; + int old_nb_index_entries = st->nb_index_entries; 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 & MOV_TRUN_SAMPLE_CTS) ? - avio_rb32(pb) : 0; - mov_update_dts_shift(sc, sc->ctts_data[sc->ctts_count].duration); + if (flags & MOV_TRUN_SAMPLE_CTS) ctts_duration = avio_rb32(pb); + + mov_update_dts_shift(sc, ctts_duration); if (frag->time != AV_NOPTS_VALUE) { if (c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) { int64_t pts = frag->time; av_log(c->fc, AV_LOG_DEBUG, "found frag time %"PRId64 " sc->dts_shift %d ctts.duration %d" " sc->time_offset %"PRId64" flags & MOV_TRUN_SAMPLE_CTS %d\n", pts, - sc->dts_shift, sc->ctts_data[sc->ctts_count].duration, + sc->dts_shift, ctts_duration, sc->time_offset, flags & MOV_TRUN_SAMPLE_CTS); dts = pts - sc->dts_shift; if (flags & MOV_TRUN_SAMPLE_CTS) { - dts -= sc->ctts_data[sc->ctts_count].duration; + dts -= ctts_duration; } else { dts -= sc->time_offset; } @@ -4343,7 +4340,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) } frag->time = AV_NOPTS_VALUE; } - sc->ctts_count++; + if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) keyframe = 1; else @@ -4352,11 +4349,30 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES)); if (keyframe) distance = 0; - err = av_add_index_entry(st, offset, dts, sample_size, distance, - keyframe ? AVINDEX_KEYFRAME : 0); - if (err < 0) { + ctts_index = av_add_index_entry(st, offset, dts, sample_size, distance, + keyframe ? AVINDEX_KEYFRAME : 0); + if (ctts_index >= 0 && old_nb_index_entries < st->nb_index_entries) { + unsigned int size_needed = st->nb_index_entries * sizeof(*sc->ctts_data); + unsigned int request_size = size_needed > sc->ctts_allocated_size ? + FFMAX(size_needed, 2 * sc->ctts_allocated_size) : size_needed; + sc->ctts_data = av_fast_realloc(sc->ctts_data, &sc->ctts_allocated_size, request_size); + if (!sc->ctts_data) { + sc->ctts_count = 0; + return AVERROR(ENOMEM); + } + + if (ctts_index != old_nb_index_entries) { + memmove(sc->ctts_data + ctts_index + 1, sc->ctts_data + ctts_index, + sizeof(*sc->ctts_data) * (sc->ctts_count - ctts_index)); + } + + sc->ctts_data[ctts_index].count = 1; + sc->ctts_data[ctts_index].duration = ctts_duration; + sc->ctts_count++; + } else { av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n"); } + av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %u, offset %"PRIx64", dts %"PRId64", " "size %u, distance %d, keyframe %d\n", st->index, sc->sample_count+i, offset, dts, sample_size, distance, keyframe); -- 2.13.2.932.g7449e964c-goog