From patchwork Mon Sep 19 16:42:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tristan Schmelcher X-Patchwork-Id: 38055 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp1356878pzh; Mon, 19 Sep 2022 09:43:42 -0700 (PDT) X-Google-Smtp-Source: AMsMyM55wcUHMs40tWAvTR4/Ej4jBJhu+6yb7ax9BlorlPHhanIvVI/T7n2cEQN/568k6HtjGKY7 X-Received: by 2002:a17:907:60d6:b0:77d:8aed:cf86 with SMTP id hv22-20020a17090760d600b0077d8aedcf86mr13513005ejc.43.1663605821792; Mon, 19 Sep 2022 09:43:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663605821; cv=none; d=google.com; s=arc-20160816; b=MHRJmyMF3VNoFg/52v4/xRGXCRWgTUxPFGBJoup0KubE1TWtdH/yhKYJXuhOuiYFaZ SWwREtc3cRRXCnPf/Z7ZB31Hb0ALrMplhJYK3jEpEdLTJRN8xRNryWO6UsfiWiMksdIq oRMyWZ+kThIsskrmpFwjSLU/N7YeV2zMZ82g2x5toLt/JVsp0Ztf5MeDWwM8c+tZKjim velnumbMm38AMArveMSUYZnqRAbs9rNqITJ37Ue7MCgq0+I0NbtPp8E9nUpKG5nkmaCV pn0pfzlhSm2l4TqxNWJTHicyhVHhWOT67enFqAxE/p8GJ6fhP5OvYy6lKm/D8Mll3k3h bLDA== 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:mime-version:date :dkim-signature:delivered-to; bh=zWPKETIF2mUY95UXUfSs/rckYmj9wH5Orzf/YnhOT/U=; b=OCYzo4RqEh5Zejl9yeJbNdA4oPu34haGN6vwcwFcUsAlG1q6YKwHwmx1C8pYoHHWy1 Js9tymHW4hT+kdkf/8rQKP3LoaTfC9h0snIXN8uUU4KiIzdTKCkiV8KbVDrgRNBVeLZU U+U2607NPffHs3B9rE3+6xGHiXmqSaZw7A720N01yO/AE06Hn/JWO2RY+z0a2xGAAkWR lFKOKcNwrb9HpR/TvLiVqzkYndusDU0kcMSGBUaj1m+EL27wBRstl1uUka+8LAqYw0Q/ FiXhoILQPrNC2CgRHjOelbKE0Z4rPDhH46FFYISPnM3NVcjoXntSm02jH56safaHyJo3 tVoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20210112 header.b=oYG9155c; 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 nd21-20020a170907629500b0074b26b6bbbcsi25838110ejc.419.2022.09.19.09.43.41; Mon, 19 Sep 2022 09:43:41 -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=oYG9155c; 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 280E268BA09; Mon, 19 Sep 2022 19:43:38 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 190A068B9DE for ; Mon, 19 Sep 2022 19:43:31 +0300 (EEST) Received: by mail-pf1-f201.google.com with SMTP id u131-20020a627989000000b0054d3cf50780so41420pfc.22 for ; Mon, 19 Sep 2022 09:43:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date; bh=vw7OX/CrYYpf8JAK2zxBldZTHI7eBIA40Fbklyw3Swg=; b=oYG9155c3DYeBSXB0eIWMzSZqZRJpezgjwbwJdAdgJOQKqTBDkxB94l+VayKj6DAq1 0PjPAH6Q6L1eon12tQwcE7jNTbBuGpK6hTdF4sP+LYTVi05nZ5Jbl64KSwE7EJLlgwx1 8ACX8NhPkD9loPnLN0fhowqepIf5kgAlH/wqguRnuvj4anWkteyiCVBvY5+H5/t0/6SQ Y0U+GSkbC21wME4QJdiay6l2N3Eg6WJNqAMVk/rirFeOCVy02ofjIoWuElfKj2ciJdlL 4oGwDJdaZc92upIZOuKtzS5D8+CzksSCSxwqFKdO2+9Ulq7WV/C98KpNETMHauhwbuQF 4J3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date; bh=vw7OX/CrYYpf8JAK2zxBldZTHI7eBIA40Fbklyw3Swg=; b=6Ei2gJN9gRuPogaY+UPnrAcUmT8Vb7agWgi3g0rCHWmpvJWaJUtuiH9qeIiiNM3qpK gBJ7nuesbdX8AKighNYDv6f3vggbkjhJxGQgYMb04vuwP/RPuRUO2aZsq752Fc3jkwSN kpwV+rqry0rWIMeolLdUTHXEuZWJsN3F4xRpY+U6lRc1qgTTTB1P1WjJnfuOALy3gCLb aqN8a+Wg6Q8XAcA5nJx1a37hmGvJL5J7l637xnHRrIGJS5fklasH/ghA0+CUzDMT5202 GaDZMuSZeVbFw+Zh9GzUymc2FHg9RQSGIpZ2MWXxnCNMOEx2VpVBoP5GFinnA9W9TQq8 YKyA== X-Gm-Message-State: ACrzQf2N+g4z9rqlz73KjY2wziM+5WAPiEYwS33sIyqpsHkr4m9rURDL VXQd7o9I1pzR+6p52o4RXzMTy6M30fZYxksJtss1kdmfN30SawZPVpmk/3avCJX4u5SMJ9QBtc9 FaGdmZuEquvX8c+g/eiszK/wYJGGeY00GCUO7c+dNwNesUnrlzpJmWSsvRBKB1Xg4tBVOvdJkEv I= X-Received: from tschmelcher-cloudtop2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:436c]) (user=tschmelcher job=sendgmr) by 2002:a05:6a00:1691:b0:53b:3f2c:3257 with SMTP id k17-20020a056a00169100b0053b3f2c3257mr19555036pfc.21.1663605808466; Mon, 19 Sep 2022 09:43:28 -0700 (PDT) Date: Mon, 19 Sep 2022 16:42:37 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.37.3.968.ga6b4b080e4-goog Message-ID: <20220919164237.312664-1-tschmelcher@google.com> From: Tristan Schmelcher To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH] Reduce rounding error in ff_scale_adjust_dimensions. 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: z95SqM3vKDfo 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. */