From patchwork Mon Sep 26 17:14:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tristan Schmelcher X-Patchwork-Id: 38370 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp2407776pzh; Mon, 26 Sep 2022 10:14:59 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7DHk/YOm7IF/ITKL1xELQZ49M5vI2RaH1g/slOtToI2R9MoAei0ryov/wEYMmYd2/38zsU X-Received: by 2002:aa7:c61a:0:b0:44e:7d1d:7814 with SMTP id h26-20020aa7c61a000000b0044e7d1d7814mr23407355edq.44.1664212499713; Mon, 26 Sep 2022 10:14:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664212499; cv=none; d=google.com; s=arc-20160816; b=MtUPKFDK3K6TJSg1LlqNs4TK3w7ufXhjRiAQ7COKJ/H/sncKsj5nq6vG+5OSQq6MOV RwVSHjSS9+NLAkSz7lRjal8vnZ0Bwqn6GzUkNkIn2bieImDE66c1rCIO77WBbkE1gT26 530nLQfe8o96Ls2JWlM2Lh4VmMLUZc2V59KJ4NHI3dzW/+Wm/1RKpnkZTmhGAHQy2iyu 7ZPhMMX10BGZtd+CRVDQP12gpn8ClYs/VTwMkQ3zEz5yCMvlSXuBqenC224V5Recxf4L IQs8ApHfDzIh+SB2xD2dflg7Jr07Zys6ktSi8kB0xR2wg9Epb9Mf/jx4hiABQk1ZmEOR +1Sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:to:from:message-id:references :mime-version:in-reply-to:date:dkim-signature:delivered-to; bh=ysH9aCBYg64c0EByI7Bifmvke4ey/GMg25ZEJkid1lc=; b=ntHGMMYnYNUz6hgrHEOJu+zZyUWl0v6MFOR9kQekzkjCvFFQDAz7PwIEdErKEWmoIX wWJkxM30SDFZ49FRWdYk+q+uBSSOMa5e1j9AouJ/lyuyQdCQnal48TGTuZayATH3hfbL ih5a7MZUtHdUs/WEL6MY3k+hbNr8S9DXpHtvRAdo9HKaNg5idKb3uHcoKSxT8fKbJVoA 9Ixs0Y4XNsjbQer/UVa0FZ8MuCC0IgtCalB4GsH4vjQz1wHzzUT58Gm6jot5nLn19M+y JVPnwuYTrb6TQCxPXlKIGWv/yNMnWVx+eHX0gFVYuSsPIAOVszDs28MZqP+hmt8HeimT 8XIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20210112 header.b=Ut6egGcQ; 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 d11-20020a50fb0b000000b004573e23619esi3637313edq.91.2022.09.26.10.14.57; Mon, 26 Sep 2022 10:14:59 -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.s=20210112 header.b=Ut6egGcQ; 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 DE4C768BBC5; Mon, 26 Sep 2022 20:14:31 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F3DBD68B64C for ; Mon, 26 Sep 2022 20:14:25 +0300 (EEST) Received: by mail-pl1-f201.google.com with SMTP id k2-20020a170902c40200b001782bd6c416so4827993plk.20 for ; Mon, 26 Sep 2022 10:14:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=g+2dxxhuW5MtfSqpaROnk3q+D4v23zzi9SiTxFIpXE4=; b=Ut6egGcQFLz3y1NTdSFdanJW8jW6KXGon8lz6X86/3fDrhnv1ZP0NM/7aoT+za1vPm ZL9kmhkb/0TBQke/Iw8jSH3A6yeNrlk7BMi6UtEjKwbUa0uOfszdb5RD3tjd12xEe9Ma 0o9irPTIeTPgucAZuHmItCLg2ck2uykTsYUf4QDg7CBSIJeHwuWvWP9mTEnvVE+KZY2c mouBdp55zEo0pGQsZWYlFua0eWJxU1wP4Pq1vk8BTabIatqT3A0eycmVdO5F7b4epJbM DmryRLv3PwO3dnPKtHCWaBch3oSsXATgSdBCBvN1Pi2w5Kp0C7+RAI9dDQNLUzmy3M6p r9Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=g+2dxxhuW5MtfSqpaROnk3q+D4v23zzi9SiTxFIpXE4=; b=dkTnZkm/d1gKg7gBQ8lCvJpVeFat/oUVIHr4SJ7PSNsjfuiHiUOOspMtLns2VywqWb bhKfRekwubqWAlE2eyo+U4oVgYeZ9tzicLApIKFs0IeUl/iKesUiuUEY+v75R8WwHVDW y4Rw5lT+sVlvEBRwzj3z8vO+1nLXS0zlb7Ii6ohR0TxHWIQOOwJMoCGeR0dAcnWqsmIl KZKp9Ek+fmqDJ6Er5JTZ57hOkSBD39rdiD+wT/KXLVv1L3q20F97P3nxVFrr1olA4SJB ok9oRhpbJo9SKlZ2ARCJCC8l0/tD2c4ev0PAyGx5Nb/mmHXIHuFustI8c5SO9iljZpGc kiwA== X-Gm-Message-State: ACrzQf3tBInpT39yK4QkJcE/m1ehFlySZJ3n61wPcMq/0r0QdYUeeWBo qoZRdOqb3zeRg82XlT6M+jzvw/ZTuoBbNlKCEVdsmrAV1qlXY8WEehTgapZIJaLuonTgSL8ABW1 OeyAySgyJHNknmNvggOyRpPGZecZq05tHSbwNeVYFNPEux6/UXKIKwtJCUL2rdyhT+d9rxeZwGl o= X-Received: from tschmelcher-cloudtop2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:436c]) (user=tschmelcher job=sendgmr) by 2002:a17:903:186:b0:178:2ca7:fae5 with SMTP id z6-20020a170903018600b001782ca7fae5mr22870626plg.173.1664212463750; Mon, 26 Sep 2022 10:14:23 -0700 (PDT) Date: Mon, 26 Sep 2022 17:14:09 +0000 In-Reply-To: <20220919164237.312664-1-tschmelcher@google.com> Mime-Version: 1.0 References: <20220919164237.312664-1-tschmelcher@google.com> X-Mailer: git-send-email 2.37.3.998.g577e59143f-goog Message-ID: <20220926171409.2782171-1-tschmelcher@google.com> From: Tristan Schmelcher To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH] avfilter/scale_eval: Reduce rounding error. 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 Cc: Tristan Schmelcher , Thierry Foucu Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: OVtuHgqK6Szx When force_original_aspect_ratio and force_divisible_by are both used, dimensions are now rounded to the nearest allowed multiple of force_divisible_by rather than first rounding to the nearest integer and then rounding in a static direction. This results in less distortion of the aspect ratio. Reviewed-by: Thierry Foucu Signed-off-by: Tristan Schmelcher --- libavfilter/scale_eval.c | 11 +++++++---- libavfilter/scale_eval.h | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/libavfilter/scale_eval.c b/libavfilter/scale_eval.c index dfec081e15..75ed503f15 100644 --- a/libavfilter/scale_eval.c +++ b/libavfilter/scale_eval.c @@ -148,14 +148,17 @@ int ff_scale_adjust_dimensions(AVFilterLink *inlink, * dimensions so that it is not divisible by the set factors anymore * unless force_divisible_by is defined as well */ if (force_original_aspect_ratio) { - int tmp_w = av_rescale(h, inlink->w, inlink->h); - int tmp_h = av_rescale(w, inlink->h, inlink->w); + // Including force_divisible_by here rounds to the nearest multiple of it. + int tmp_w = av_rescale(h, inlink->w, inlink->h * (int64_t)force_divisible_by) + * force_divisible_by; + int tmp_h = av_rescale(w, inlink->h, inlink->w * (int64_t)force_divisible_by) + * force_divisible_by; if (force_original_aspect_ratio == 1) { w = FFMIN(tmp_w, w); h = FFMIN(tmp_h, h); if (force_divisible_by > 1) { - // round down + // round down in case provided w or h is not divisible. w = w / force_divisible_by * force_divisible_by; h = h / force_divisible_by * force_divisible_by; } @@ -163,7 +166,7 @@ int ff_scale_adjust_dimensions(AVFilterLink *inlink, w = FFMAX(tmp_w, w); h = FFMAX(tmp_h, h); if (force_divisible_by > 1) { - // round up + // round up in case provided w or h is not divisible. w = (w + force_divisible_by - 1) / force_divisible_by * force_divisible_by; h = (h + force_divisible_by - 1) / force_divisible_by * force_divisible_by; } diff --git a/libavfilter/scale_eval.h b/libavfilter/scale_eval.h index fceb023fec..2eb6970aad 100644 --- a/libavfilter/scale_eval.h +++ b/libavfilter/scale_eval.h @@ -38,7 +38,8 @@ int ff_scale_eval_dimensions(void *ctx, * Transform evaluated width and height obtained from ff_scale_eval_dimensions * into actual target width and height for scaling. Adjustment can occur if one * or both of the evaluated values are of the form '-n' or if - * force_original_aspect_ratio is set. + * force_original_aspect_ratio is set. force_divisible_by is used only when + * force_original_aspect_ratio is set and must be at least 1. * * Returns 0. */