From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 91F2F445F9 for ; Mon, 19 Sep 2022 16:43:40 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E67E868BA02; Mon, 19 Sep 2022 19:43:37 +0300 (EEST) 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-Google-Smtp-Source: AMsMyM4PRtY4KvhKR8g15joRD4LYVSBEGmLOFgoTffLRgX7b8Wi9W4S2w+7wR+YBJ0Upa+54NIx3jnZFR5rDBxNY9g== 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: 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. */ -- 2.37.3.968.ga6b4b080e4-goog _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".