From patchwork Tue Nov 23 13:11:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyan Doshi X-Patchwork-Id: 31564 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:d206:0:0:0:0:0 with SMTP id q6csp8642474iob; Tue, 23 Nov 2021 05:11:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJyxiWA5USc+9Z94lbfhW6B7Kzye9PhgF4OrZHPQfy6xtnTPOqkPIYjcnVeYaCIrtm9Xfx49 X-Received: by 2002:a17:906:974c:: with SMTP id o12mr4524258ejy.229.1637673108711; Tue, 23 Nov 2021 05:11:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637673108; cv=none; d=google.com; s=arc-20160816; b=q7xcVMUBbkdfmL5gJwz2sHlnZM663nv75MTsJ7NAZBWHjTB92SCzz2PJlH0btLP5Yf MTPuSR4PAbC6FMXoKNGtV3H1CCQDklwBIPBQZqrME7ypKXhiDIJIo2klTVDSpGQY/0so G4MayIddX1AbvsQCq9/zPlsdzIV/h1MWWGTOUmLrWJtVomaLViUgMmK1CVi6j83Y0x38 FiDrb+6EPiC3Hd37VkVddjsWAEdhF3wl+3S4oYpE84latcDtOS6YrzEHdPgb+3gsMGeh 2M+WKNC/Chez/Oarses9ccQYqvk/BSwvl28eJp8zg85mZ3HXwjNjjcFls6uL7cAxpwp1 WfNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=EsxW4jPpG2EBe+jdpdoKorlBfIWN7/NEPnIPHnV/wS0=; b=C1+kJnHspCUh2n5c9jnZvlQ27MGHUZdCw/WCvRNBvlKdVZVLOziKMPnRBGi6iQWmwu DPhDXaFI3SnAgO9zU9JlhTX7xSKtW8VErZkohlVAzJgty74Ssj26EuZj4YMRB1754yEv Cw2G4nUo6gtrFWFNac9rWPs13yV4OeuQ62iSxoAqaYahDUjYE7T/Mu1sRjT0QF5xQAew B0PECfcnlgwNxPGywaktUZ3nnEq56irrafA042LxwyvdThvgNfdCVJ4xKiUguMJZC4FM 3ud4vfbX+7nNb2ybKPtQmdPjWs+tQw5x8R70ukrf2wRqYzub0i4QYqg60IyH7ktQqAa2 xn/A== ARC-Authentication-Results: i=1; mx.google.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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id js5si41254081ejc.656.2021.11.23.05.11.47; Tue, 23 Nov 2021 05:11: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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E2F1968AF0E; Tue, 23 Nov 2021 15:11:42 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AC89368A874 for ; Tue, 23 Nov 2021 15:11:35 +0200 (EET) Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4Hz4KM1cZzzQkBw for ; Tue, 23 Nov 2021 14:11:35 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de From: Gyan Doshi To: ffmpeg-devel@ffmpeg.org Date: Tue, 23 Nov 2021 18:41:06 +0530 Message-Id: <20211123131106.8652-1-ffmpeg@gyani.pro> In-Reply-To: <20211123124507.8634-1-ffmpeg@gyani.pro> References: <20211123124507.8634-1-ffmpeg@gyani.pro> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] avformat/mov: add option max_stts_delta X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: PZwOxDvJixyt Very high stts sample deltas may occasionally be intended but usually they are written in error or used to store a negative value for dts correction when treated as signed 32-bit integers. This option lets the user set an upper limit, beyond which the delta is clamped to 1. Negative values of under 1 second are used to adjust dts. Unit is the track time scale. Default is INT_MAX which maintains current handling. --- doc/demuxers.texi | 6 ++++++ libavformat/isom.h | 1 + libavformat/mov.c | 14 +++++++++----- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/doc/demuxers.texi b/doc/demuxers.texi index cab8a7072c..15078b9b1b 100644 --- a/doc/demuxers.texi +++ b/doc/demuxers.texi @@ -713,6 +713,12 @@ specify. @item decryption_key 16-byte key, in hex, to decrypt files encrypted using ISO Common Encryption (CENC/AES-128 CTR; ISO/IEC 23001-7). + +@item max_stts_delta +The sample offsets stored in a track's stts box are 32-bit unsigned integers. However, very large values usually indicate +a value written by error or a storage of a small negative value as a way to correct accumulated DTS delay. +Range is 0 to UINT_MAX. Default is INT_MAX. + @end table @subsection Audible AAX diff --git a/libavformat/isom.h b/libavformat/isom.h index ef8f19b18c..625dea8421 100644 --- a/libavformat/isom.h +++ b/libavformat/isom.h @@ -305,6 +305,7 @@ typedef struct MOVContext { int32_t movie_display_matrix[3][3]; ///< display matrix from mvhd int have_read_mfra_size; uint32_t mfra_size; + uint32_t max_stts_delta; } MOVContext; int ff_mp4_read_descr_len(AVIOContext *pb); diff --git a/libavformat/mov.c b/libavformat/mov.c index 451cb78bbf..bbda07ac42 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -3965,14 +3965,17 @@ static void mov_build_index(MOVContext *mov, AVStream *st) current_offset += sample_size; stream_size += sample_size; - /* A negative sample duration is invalid based on the spec, - * but some samples need it to correct the DTS. */ - if (sc->stts_data[stts_index].duration < 0) { + /* STTS sample offsets are uint32 but some files store it as int32 + * with negative values used to correct DTS delays. + There may be abnormally large values as well. */ + if (sc->stts_data[stts_index].duration > mov->max_stts_delta) { + // assume high delta is a negative correction if less than 1 second + int32_t delta_magnitude = *((int32_t *)&sc->stts_data[stts_index].duration); av_log(mov->fc, AV_LOG_WARNING, - "Invalid SampleDelta %d in STTS, at %d st:%d\n", + "Correcting too large SampleDelta %u in STTS, at %d st:%d.\n", sc->stts_data[stts_index].duration, stts_index, st->index); - dts_correction += sc->stts_data[stts_index].duration - 1; + dts_correction += (delta_magnitude < 0 && FFABS(delta_magnitude) < sc->time_scale ? delta_magnitude - 1 : 0); sc->stts_data[stts_index].duration = 1; } current_dts += sc->stts_data[stts_index].duration; @@ -8566,6 +8569,7 @@ static const AVOption mov_options[] = { { "decryption_key", "The media decryption key (hex)", OFFSET(decryption_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM }, { "enable_drefs", "Enable external track support.", OFFSET(enable_drefs), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS }, + { "max_stts_delta", "treat offsets above this value as invalid", OFFSET(max_stts_delta), AV_OPT_TYPE_INT, {.i64 = INT_MAX}, 0, UINT_MAX, .flags = AV_OPT_FLAG_DECODING_PARAM }, { NULL }, };