From patchwork Tue Nov 23 12:45:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyan Doshi X-Patchwork-Id: 31561 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:d206:0:0:0:0:0 with SMTP id q6csp8607556iob; Tue, 23 Nov 2021 04:45:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJzCWzICICocbc7TEb0mi20rzDH4NQVjb8lgEosFj/gBXocPg97iIZttkNabQ9eNft4xQ2cy X-Received: by 2002:a17:906:945:: with SMTP id j5mr7316661ejd.446.1637671553227; Tue, 23 Nov 2021 04:45:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637671553; cv=none; d=google.com; s=arc-20160816; b=WCGCJ0/IGDaRR6MQybfXrlaOp88VJtHaIfMjFzT5PdjHWbnPa9bxS1VjA3aDDXvVG8 +KgsCRdk/bwH5EDszS5q1dwEk/G/zj7F3FkKpRDakbtpDgIIpE64mUOsqU1OZrWVRgkd T5KRMTIohrpqkC/YwMddrzHq7kUHM3S0zFJQ+AxNtZXyWHrqEdMXhbi6K0U+H6pSr/Hc +0TQzeGiWSW5NfzSWdmazrzDEwliyiuNsJ4c8yUu8f3m9k/DQy4iBuufnusjlUSq7KmS 6Ev0rVmWP9Ih/8W8iN5UkVexRtCdd+VAUyocbCIiI0dOamjhfZnwFwc/WJ+qlCrPTpfF ey2A== 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:message-id:date:to:from :delivered-to; bh=Ms/xYTm6fTU1bnsm9mnuw2yNTP+UCXXrqlBOEgNyCoc=; b=m/t524YTZvYvHltEWHLKErmX/x9SZ2GTuX/1+vlwRyeBhotaFI6/lbL7b5YjVJBxIr a4xxGes5WCEA+l7N0miuizVdbvw6Xrx4hcWpQ0rfqjmmdH4Ld7W/Ch4J5K9k1e5yjcL9 R/KzLx1zgGw5vyJfV2X26fJm+2ai00q5yh1QNeShvPdqQLll1ofkyQF1pzgKN/OvCMTX Hs3fMmKlF8EACtO/YkEXwmIxZmd+yicqUlxcM6MPesh6YQDpIKf0KFxULoLBQHP6ERt0 qr1mNtLvvUNeIfPi+SF9X9eGsTlfNeniavMd+xK9y31ImArQxpShVc1NLvGBencHiLT+ mO7w== 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 f6si37173261edd.473.2021.11.23.04.45.52; Tue, 23 Nov 2021 04:45:53 -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 D1ECB68AF0E; Tue, 23 Nov 2021 14:45:47 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 222B768AEA2 for ; Tue, 23 Nov 2021 14:45:39 +0200 (EET) Received: from mout-p-103.mailbox.org (mout-p-103.mailbox.org [80.241.56.161]) (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-102.mailbox.org (Postfix) with ESMTPS id 4Hz3lQ5C8QzQkM3 for ; Tue, 23 Nov 2021 13:45:38 +0100 (CET) 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-103.mailbox.org (Postfix) with ESMTPS id 4Hz3lQ4sYZzQj97 for ; Tue, 23 Nov 2021 13:45:38 +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:15:07 +0530 Message-Id: <20211123124507.8634-1-ffmpeg@gyani.pro> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] 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: DdfRi0ia1lg+ 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 | 5 +++++ libavformat/isom.h | 1 + libavformat/mov.c | 14 +++++++++----- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/doc/demuxers.texi b/doc/demuxers.texi index cab8a7072c..f91ac92cf1 100644 --- a/doc/demuxers.texi +++ b/doc/demuxers.texi @@ -715,6 +715,11 @@ specify. 16-byte key, in hex, to decrypt files encrypted using ISO Common Encryption (CENC/AES-128 CTR; ISO/IEC 23001-7). @end table +@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. + @subsection Audible AAX Audible AAX files are encrypted M4B files, and they can be decrypted by specifying a 4 byte activation secret. 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 }, };