Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Tristan Schmelcher <tschmelcher-at-google.com@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Tristan Schmelcher <tschmelcher@google.com>,
	Thierry Foucu <tfoucu@google.com>
Subject: [FFmpeg-devel] [PATCH] Reduce rounding error in ff_scale_adjust_dimensions.
Date: Mon, 19 Sep 2022 16:42:37 +0000
Message-ID: <20220919164237.312664-1-tschmelcher@google.com> (raw)

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 <tfoucu@google.com>
Signed-off-by: Tristan Schmelcher <tschmelcher@google.com>
---
 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".

             reply	other threads:[~2022-09-19 16:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-19 16:42 Tristan Schmelcher [this message]
2022-09-26 17:14 ` [FFmpeg-devel] [PATCH] avfilter/scale_eval: Reduce rounding error Tristan Schmelcher
2022-09-26 22:02   ` Michael Niedermayer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220919164237.312664-1-tschmelcher@google.com \
    --to=tschmelcher-at-google.com@ffmpeg.org \
    --cc=ffmpeg-devel@ffmpeg.org \
    --cc=tfoucu@google.com \
    --cc=tschmelcher@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git