From patchwork Wed Nov 9 12:07:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: mail@nodoa.me X-Patchwork-Id: 39239 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:999a:b0:a4:2148:650a with SMTP id ve26csp260158pzb; Wed, 9 Nov 2022 04:07:40 -0800 (PST) X-Google-Smtp-Source: AMsMyM59qX4fscWsQwA/cAD5mvVBhdmZxTInBftLG/xVCUzdjZjvWAVMMbqwcuB9ipDCyCOG+tqo X-Received: by 2002:a17:906:2681:b0:783:6a92:4c38 with SMTP id t1-20020a170906268100b007836a924c38mr56512209ejc.75.1667995660492; Wed, 09 Nov 2022 04:07:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667995660; cv=none; d=google.com; s=arc-20160816; b=M20lCWk9bfyc6LuEj3jQ0DLz+dt25kefkeRX7sBb6qZMx4nsYXj9w50Ujssp5k2YpI VfqLwO0k2eAgSikkKanJFnO99hbhfi/BLh71hc/K+wZ788iTsbQDf87EYIorunvvKY7s jdybMMy15QgQ03PlsezC/LZlD+ciPoDgS5wdJ2UjmN0ZK7gx2ArfdNRZg2vp2uDSbF2H +6HbowVmIw4GJPS0HDsZltVewuTlvvoaGjEVm3ADYywm17ltwUYYHqD5b1rcP4skUpLT MsWxVaT7mvbv+I6J9jq0aePcCP5NKlC1npxsqGXEKGSDsXbLGeRgjMT42KFU41c4aBAi JuNg== 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:content-language:mime-version:message-id:date:to :from:dkim-signature:delivered-to; bh=QMiN8eMqss6aDS0tn7ou7CFgZmjrF6JGA8e7HKfwxWQ=; b=zCXpekYWeHP5uZf2XfWu870eQznpE6uPFYOEZ4Fv5cfTNDrZPfvndwybIbJdLpUYel FvOqxjQxD5f7W2gf8g76Xw3AIzVgQ4HxNYsOQrnHmnMARDPA0z4PFigk5SLNwt4OkjDp DtvoOBEpzROUA6ch4TOnsjombjHDeU1mm7By7iN/4BrbLsU+1nMBvgbrmJOFqu3i3/EH JVovHyatoQgPcbIJBOFudxUvLeRTNoZRvgJwmXEJgw10EnE8i6FclMdykdlz1cWduj3D +tTxt8xbAQIVMNJWKZqUov2Ri7CZvubW7R5SXXB54oQI+Xs8aizeMdJ2022q2lYDlMye 5hHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@nodoa.me header.s=key1 header.b=J7C6i9+c; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=nodoa.me Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id o12-20020a170906974c00b007ae98ea45e0si1129029ejy.751.2022.11.09.04.07.39; Wed, 09 Nov 2022 04:07:40 -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=@nodoa.me header.s=key1 header.b=J7C6i9+c; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=nodoa.me Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8F20F68B000; Wed, 9 Nov 2022 14:07:36 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0BCDC68B934 for ; Wed, 9 Nov 2022 14:07:27 +0200 (EET) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nodoa.me; s=key1; t=1667995646; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=aTM0pou6Ho9pAq3gNG9ObjfzX4GPm2+6FQHRI1qXmGg=; b=J7C6i9+cgoWjCpSH/FDNcmoYh/RDJnEpk7yszlGezaG5aN6Wgq7E7rQlNWdsQ27acNg1og 80lZnKd6KvKIt1LgGFGMRsh7QLfNHihHHlj4LpbrM6JfZhSf970QThVMl1L96k7u8BVTIw fWowejoDz8mN4soNlSZPc5HcKVETCyU= From: To: Date: Wed, 9 Nov 2022 21:07:14 +0900 Message-ID: <001201d8f433$d50629d0$7f127d70$@nodoa.me> MIME-Version: 1.0 Content-Language: ja X-Migadu-Flow: FLOW_OUT Subject: [FFmpeg-devel] [PATCH] lavfi/vf_decimate: add mixed option to process input only partially to be decimated 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: qpisH3Mata3t Enabling the option will only decimate frames below dupthresh and output at variable frame rate. Signed-off-by: lovesyk --- libavfilter/vf_decimate.c | 42 +++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) calculations", OFFSET(chroma), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS }, + { "mixed", "set whether or not the input only partially contains content to be decimated", OFFSET(mixed), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS }, { NULL } }; @@ -193,7 +199,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) } if (dm->queue[lowest].maxbdiff < dm->dupthresh) duppos = lowest; - drop = scpos >= 0 && duppos < 0 ? scpos : lowest; + + if (dm->mixed && duppos < 0) { + drop = -1; // no drop if mixed content + no frame in cycle below threshold + } else { + drop = scpos >= 0 && duppos < 0 ? scpos : lowest; + } } /* metrics debug */ @@ -212,7 +223,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) /* push all frames except the drop */ ret = 0; for (i = 0; i < dm->cycle && dm->queue[i].frame; i++) { - AVRational in_tb = ctx->inputs[INPUT_MAIN]->time_base; if (i == drop) { if (dm->ppsrc) av_frame_free(&dm->clean_src[i]); @@ -221,7 +231,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) AVFrame *frame = dm->queue[i].frame; dm->queue[i].frame = NULL; if (frame->pts != AV_NOPTS_VALUE && dm->start_pts == AV_NOPTS_VALUE) - dm->start_pts = av_rescale_q(frame->pts, in_tb, outlink->time_base); + dm->start_pts = av_rescale_q(frame->pts, dm->in_tb, outlink->time_base); if (dm->ppsrc) { av_frame_free(&frame); @@ -230,9 +240,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) continue; dm->clean_src[i] = NULL; } - frame->pts = outlink->frame_count_in + + + frame->pts = dm->last_duration ? dm->last_pts + dm->last_duration : (dm->start_pts == AV_NOPTS_VALUE ? 0 : dm->start_pts); - frame->duration = 1; + frame->duration = dm->mixed ? av_div_q(drop < 0 ? dm->nondec_tb : dm->dec_tb, outlink->time_base).num : 1; + dm->last_duration = frame->duration; dm->last_pts = frame->pts; ret = ff_filter_frame(outlink, frame); if (ret < 0) @@ -329,6 +341,7 @@ static av_cold int decimate_init(AVFilterContext *ctx) } dm->start_pts = AV_NOPTS_VALUE; + dm->last_duration = 0; return 0; } @@ -388,6 +401,9 @@ static int config_output(AVFilterLink *outlink) dm->bdiffsize = dm->nxblocks * dm->nyblocks; dm->bdiffs = av_malloc_array(dm->bdiffsize, sizeof(*dm->bdiffs)); dm->queue = av_calloc(dm->cycle, sizeof(*dm->queue)); + dm->in_tb = inlink->time_base; + dm->nondec_tb = av_inv_q(fps); + dm->dec_tb = av_mul_q(dm->nondec_tb, (AVRational){dm->cycle, dm->cycle - 1}); if (!dm->bdiffs || !dm->queue) return AVERROR(ENOMEM); @@ -403,11 +419,17 @@ static int config_output(AVFilterLink *outlink) "current rate of %d/%d is invalid\n", fps.num, fps.den); return AVERROR(EINVAL); } - fps = av_mul_q(fps, (AVRational){dm->cycle - 1, dm->cycle}); - av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n", - inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den); - outlink->time_base = av_inv_q(fps); - outlink->frame_rate = fps; + + if (dm->mixed) { + outlink->time_base = av_gcd_q(dm->nondec_tb, dm->dec_tb, AV_TIME_BASE / 2, AV_TIME_BASE_Q); + av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> VFR (use %d/%d if CFR required)\n", + fps.num, fps.den, outlink->time_base.den, outlink->time_base.num); + } else { + outlink->time_base = dm->dec_tb; + outlink->frame_rate = av_inv_q(outlink->time_base); + av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n", + fps.num, fps.den, outlink->frame_rate.num, outlink->frame_rate.den); + } outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; if (dm->ppsrc) { outlink->w = ctx->inputs[INPUT_CLEANSRC]->w; diff --git a/libavfilter/vf_decimate.c b/libavfilter/vf_decimate.c index f61e501c96..dbeca427f1 100644 --- a/libavfilter/vf_decimate.c +++ b/libavfilter/vf_decimate.c @@ -44,6 +44,7 @@ typedef struct DecimateContext { AVFrame **clean_src; ///< frame queue for the clean source int got_frame[2]; ///< frame request flag for each input stream int64_t last_pts; ///< last output timestamp + int64_t last_duration; ///< last output duration int64_t start_pts; ///< base for output timestamps uint32_t eof; ///< bitmask for end of stream int hsub, vsub; ///< chroma subsampling values @@ -51,6 +52,9 @@ typedef struct DecimateContext { int nxblocks, nyblocks; int bdiffsize; int64_t *bdiffs; + AVRational in_tb; // input time-base + AVRational nondec_tb; // non-decimated time-base + AVRational dec_tb; // decimated time-base /* options */ int cycle; @@ -61,6 +65,7 @@ typedef struct DecimateContext { int blockx, blocky; int ppsrc; int chroma; + int mixed; } DecimateContext; #define OFFSET(x) offsetof(DecimateContext, x) @@ -74,6 +79,7 @@ static const AVOption decimate_options[] = { { "blocky", "set the size of the y-axis blocks used during metric calculations", OFFSET(blocky), AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9, FLAGS }, { "ppsrc", "mark main input as a pre-processed input and activate clean source input stream", OFFSET(ppsrc), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS }, { "chroma", "set whether or not chroma is considered in the metric