Re: [FFmpeg-devel] [PATCH] avfilter/scale.c: factorize ff_scale_eval_dimensions

2019-12-08 Thread Gyan



On 08-12-2019 10:03 am, Gyan wrote:



On 07-12-2019 11:47 pm, Michael Niedermayer wrote:

On Sat, Dec 07, 2019 at 12:08:53PM +0530, Gyan wrote:


  doc/filters.texi |   40 +
  libavfilter/scale.c  |   59 
++-

  libavfilter/scale.h  |   20 ++
  libavfilter/vf_scale.c   |   28 ++--
  libavfilter/vf_scale_cuda.c  |   11 
  libavfilter/vf_scale_npp.c   |   11 
  libavfilter/vf_scale_vaapi.c |   11 
  7 files changed, 144 insertions(+), 36 deletions(-)
f0a65d11857e709ee7cdf862ba108c765c4f5a2f 
v2-0001-avfilter-scale.c-factorize-ff_scale_eval_dimensio.patch

 From 863c6feac1790560f7bdc29358cfcd0e5ee370f6 Mon Sep 17 00:00:00 2001
From: Gyan Doshi 
Date: Mon, 2 Dec 2019 21:11:21 +0530
Subject: [PATCH v2] avfilter/scale.c: factorize 
ff_scale_eval_dimensions


Adjustment of evaluated values shifted to ff_adjust_scale_dimensions
Shifted code for force_original_aspect_ratio and force_divisble_by from
vf_scale so it is now available for scale_cuda, scale_npp and
scale_vaapi as well.
---
  doc/filters.texi | 40 
  libavfilter/scale.c  | 59 
+---

  libavfilter/scale.h  | 20 
  libavfilter/vf_scale.c   | 28 ++---
  libavfilter/vf_scale_cuda.c  | 11 +++
  libavfilter/vf_scale_npp.c   | 11 +++
  libavfilter/vf_scale_vaapi.c | 11 +++
  7 files changed, 144 insertions(+), 36 deletions(-)

The changes should be ok i think.


Will push in a few hours.


Pushed with a couple of doc typos also corrected as 
1b4f473d181abaa0ff4e2d63862f61763a5a6860


Gyan
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Re: [FFmpeg-devel] [PATCH] avfilter/scale.c: factorize ff_scale_eval_dimensions

2019-12-07 Thread Gyan



On 07-12-2019 11:47 pm, Michael Niedermayer wrote:

On Sat, Dec 07, 2019 at 12:08:53PM +0530, Gyan wrote:


  doc/filters.texi |   40 +
  libavfilter/scale.c  |   59 
++-
  libavfilter/scale.h  |   20 ++
  libavfilter/vf_scale.c   |   28 ++--
  libavfilter/vf_scale_cuda.c  |   11 
  libavfilter/vf_scale_npp.c   |   11 
  libavfilter/vf_scale_vaapi.c |   11 
  7 files changed, 144 insertions(+), 36 deletions(-)
f0a65d11857e709ee7cdf862ba108c765c4f5a2f  
v2-0001-avfilter-scale.c-factorize-ff_scale_eval_dimensio.patch
 From 863c6feac1790560f7bdc29358cfcd0e5ee370f6 Mon Sep 17 00:00:00 2001
From: Gyan Doshi 
Date: Mon, 2 Dec 2019 21:11:21 +0530
Subject: [PATCH v2] avfilter/scale.c: factorize ff_scale_eval_dimensions

Adjustment of evaluated values shifted to ff_adjust_scale_dimensions
Shifted code for force_original_aspect_ratio and force_divisble_by from
vf_scale so it is now available for scale_cuda, scale_npp and
scale_vaapi as well.
---
  doc/filters.texi | 40 
  libavfilter/scale.c  | 59 +---
  libavfilter/scale.h  | 20 
  libavfilter/vf_scale.c   | 28 ++---
  libavfilter/vf_scale_cuda.c  | 11 +++
  libavfilter/vf_scale_npp.c   | 11 +++
  libavfilter/vf_scale_vaapi.c | 11 +++
  7 files changed, 144 insertions(+), 36 deletions(-)

The changes should be ok i think.


Will push in a few hours.

Thanks,
Gyan
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Re: [FFmpeg-devel] [PATCH] avfilter/scale.c: factorize ff_scale_eval_dimensions

2019-12-07 Thread Michael Niedermayer
On Sat, Dec 07, 2019 at 12:08:53PM +0530, Gyan wrote:
> 
> 
> On 07-12-2019 12:36 am, Michael Niedermayer wrote:
> >On Wed, Dec 04, 2019 at 02:14:35PM +0530, Gyan wrote:
> >>Will help reduce code duplication when adding animation support to vf_scale.
> >>See Michael's last comment in https://patchwork.ffmpeg.org/patch/16272/
> >>
> >>Gyan
> >>  doc/filters.texi |   40 +
> >>  libavfilter/scale.c  |   59 
> >> ++-
> >>  libavfilter/scale.h  |4 ++
> >>  libavfilter/vf_scale.c   |   28 ++--
> >>  libavfilter/vf_scale_cuda.c  |   11 
> >>  libavfilter/vf_scale_npp.c   |   11 
> >>  libavfilter/vf_scale_vaapi.c |   11 
> >>  7 files changed, 128 insertions(+), 36 deletions(-)
> >>380bf799c14d6286cc625afe019aa553614a7d53  
> >>0001-avfilter-scale.c-factorize-ff_scale_eval_dimensions.patch
> >> From 138a8dba766674a1b017614c58fa99aeca98e9e5 Mon Sep 17 00:00:00 2001
> >>From: Gyan Doshi 
> >>Date: Mon, 2 Dec 2019 21:11:21 +0530
> >>Subject: [PATCH] avfilter/scale.c: factorize ff_scale_eval_dimensions
> >>
> >>Adjustment of evaluated values shifted to ff_adjust_scale_dimensions
> >>Shifted code for force_original_aspect_ratio and force_divisble_by from
> >>vf_scale so it is now available for scale_cuda, scale_npp and
> >>scale_vaapi as well.
> >>---
> >>  doc/filters.texi | 40 
> >>  libavfilter/scale.c  | 59 +---
> >>  libavfilter/scale.h  |  4 +++
> >>  libavfilter/vf_scale.c   | 28 ++---
> >>  libavfilter/vf_scale_cuda.c  | 11 +++
> >>  libavfilter/vf_scale_npp.c   | 11 +++
> >>  libavfilter/vf_scale_vaapi.c | 11 +++
> >>  7 files changed, 128 insertions(+), 36 deletions(-)
> >>
> >>diff --git a/doc/filters.texi b/doc/filters.texi
> >>index 5fdec6f015..9129f7e3a5 100644
> >>--- a/doc/filters.texi
> >>+++ b/doc/filters.texi
> >>@@ -16210,6 +16210,46 @@ Supersampling
> >>  @item lanczos
> >>  @end table
> >>+@item force_original_aspect_ratio
> >>+Enable decreasing or increasing output video width or height if necessary 
> >>to
> >>+keep the original aspect ratio. Possible values:
> >>+
> >>+@table @samp
> >>+@item disable
> >>+Scale the video as specified and disable this feature.
> >>+
> >>+@item decrease
> >>+The output video dimensions will automatically be decreased if needed.
> >>+
> >>+@item increase
> >>+The output video dimensions will automatically be increased if needed.
> >>+
> >>+@end table
> >>+
> >>+One useful instance of this option is that when you know a specific 
> >>device's
> >>+maximum allowed resolution, you can use this to limit the output video to
> >>+that, while retaining the aspect ratio. For example, device A allows
> >>+1280x720 playback, and your video is 1920x800. Using this option (set it to
> >>+decrease) and specifying 1280x720 to the command line makes the output
> >>+1280x533.
> >>+
> >>+Please note that this is a different thing than specifying -1 for 
> >>@option{w}
> >>+or @option{h}, you still need to specify the output resolution for this 
> >>option
> >>+to work.
> >>+
> >>+@item force_divisible_by
> >>+Ensures that both the output dimensions, width and height, are divisible 
> >>by the
> >>+given integer when used together with 
> >>@option{force_original_aspect_ratio}. This
> >>+works similar to using @code{-n} in the @option{w} and @option{h} options.
> >>+
> >>+This option respects the value set for 
> >>@option{force_original_aspect_ratio},
> >>+increasing or decreasing the resolution accordingly. The video's aspect 
> >>ratio
> >>+may be slightly modified.
> >>+
> >>+This option can be handy if you need to have a video fit within or exceed
> >>+a defined resolution using @option{force_original_aspect_ratio} but also 
> >>have
> >>+encoder restrictions on width or height divisibility.
> >>+
> >>  @end table
> >>  @section scale2ref
> >>diff --git a/libavfilter/scale.c b/libavfilter/scale.c
> >>index eaee95fac6..5e9f97230c 100644
> >>--- a/libavfilter/scale.c
> >>+++ b/libavfilter/scale.c
> >>@@ -111,8 +111,6 @@ int ff_scale_eval_dimensions(void *log_ctx,
> >>  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
> >>  const AVPixFmtDescriptor *out_desc = 
> >> av_pix_fmt_desc_get(outlink->format);
> >>  const char *expr;
> >>-int w, h;
> >>-int factor_w, factor_h;
> >>  int eval_w, eval_h;
> >>  int ret;
> >>  const char scale2ref = outlink->src->nb_inputs == 2 && 
> >> outlink->src->inputs[1] == inlink;
> >>@@ -172,8 +170,28 @@ int ff_scale_eval_dimensions(void *log_ctx,
> >>  goto fail;
> >>  eval_w = (int) res == 0 ? inlink->w : (int) res;
> >>-w = eval_w;
> >>-h = eval_h;
> >>+*ret_w = eval_w;
> >>+*ret_h = eval_h;
> >>+
> >>+return 0;
> >>+
> >>+fail:
> >>+av_log(log_ctx, AV_LOG_ERROR,
> >>+   "Error when evaluating the 

Re: [FFmpeg-devel] [PATCH] avfilter/scale.c: factorize ff_scale_eval_dimensions

2019-12-06 Thread Gyan



On 07-12-2019 12:36 am, Michael Niedermayer wrote:

On Wed, Dec 04, 2019 at 02:14:35PM +0530, Gyan wrote:

Will help reduce code duplication when adding animation support to vf_scale.
See Michael's last comment in https://patchwork.ffmpeg.org/patch/16272/

Gyan
  doc/filters.texi |   40 +
  libavfilter/scale.c  |   59 
++-
  libavfilter/scale.h  |4 ++
  libavfilter/vf_scale.c   |   28 ++--
  libavfilter/vf_scale_cuda.c  |   11 
  libavfilter/vf_scale_npp.c   |   11 
  libavfilter/vf_scale_vaapi.c |   11 
  7 files changed, 128 insertions(+), 36 deletions(-)
380bf799c14d6286cc625afe019aa553614a7d53  
0001-avfilter-scale.c-factorize-ff_scale_eval_dimensions.patch
 From 138a8dba766674a1b017614c58fa99aeca98e9e5 Mon Sep 17 00:00:00 2001
From: Gyan Doshi 
Date: Mon, 2 Dec 2019 21:11:21 +0530
Subject: [PATCH] avfilter/scale.c: factorize ff_scale_eval_dimensions

Adjustment of evaluated values shifted to ff_adjust_scale_dimensions
Shifted code for force_original_aspect_ratio and force_divisble_by from
vf_scale so it is now available for scale_cuda, scale_npp and
scale_vaapi as well.
---
  doc/filters.texi | 40 
  libavfilter/scale.c  | 59 +---
  libavfilter/scale.h  |  4 +++
  libavfilter/vf_scale.c   | 28 ++---
  libavfilter/vf_scale_cuda.c  | 11 +++
  libavfilter/vf_scale_npp.c   | 11 +++
  libavfilter/vf_scale_vaapi.c | 11 +++
  7 files changed, 128 insertions(+), 36 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 5fdec6f015..9129f7e3a5 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -16210,6 +16210,46 @@ Supersampling
  @item lanczos
  @end table
  
+@item force_original_aspect_ratio

+Enable decreasing or increasing output video width or height if necessary to
+keep the original aspect ratio. Possible values:
+
+@table @samp
+@item disable
+Scale the video as specified and disable this feature.
+
+@item decrease
+The output video dimensions will automatically be decreased if needed.
+
+@item increase
+The output video dimensions will automatically be increased if needed.
+
+@end table
+
+One useful instance of this option is that when you know a specific device's
+maximum allowed resolution, you can use this to limit the output video to
+that, while retaining the aspect ratio. For example, device A allows
+1280x720 playback, and your video is 1920x800. Using this option (set it to
+decrease) and specifying 1280x720 to the command line makes the output
+1280x533.
+
+Please note that this is a different thing than specifying -1 for @option{w}
+or @option{h}, you still need to specify the output resolution for this option
+to work.
+
+@item force_divisible_by
+Ensures that both the output dimensions, width and height, are divisible by the
+given integer when used together with @option{force_original_aspect_ratio}. 
This
+works similar to using @code{-n} in the @option{w} and @option{h} options.
+
+This option respects the value set for @option{force_original_aspect_ratio},
+increasing or decreasing the resolution accordingly. The video's aspect ratio
+may be slightly modified.
+
+This option can be handy if you need to have a video fit within or exceed
+a defined resolution using @option{force_original_aspect_ratio} but also have
+encoder restrictions on width or height divisibility.
+
  @end table
  
  @section scale2ref

diff --git a/libavfilter/scale.c b/libavfilter/scale.c
index eaee95fac6..5e9f97230c 100644
--- a/libavfilter/scale.c
+++ b/libavfilter/scale.c
@@ -111,8 +111,6 @@ int ff_scale_eval_dimensions(void *log_ctx,
  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
  const char *expr;
-int w, h;
-int factor_w, factor_h;
  int eval_w, eval_h;
  int ret;
  const char scale2ref = outlink->src->nb_inputs == 2 && 
outlink->src->inputs[1] == inlink;
@@ -172,8 +170,28 @@ int ff_scale_eval_dimensions(void *log_ctx,
  goto fail;
  eval_w = (int) res == 0 ? inlink->w : (int) res;
  
-w = eval_w;

-h = eval_h;
+*ret_w = eval_w;
+*ret_h = eval_h;
+
+return 0;
+
+fail:
+av_log(log_ctx, AV_LOG_ERROR,
+   "Error when evaluating the expression '%s'.\n"
+   "Maybe the expression for out_w:'%s' or for out_h:'%s' is 
self-referencing.\n",
+   expr, w_expr, h_expr);
+return ret;
+}
+
+int ff_scale_adjust_dimensions(AVFilterLink *inlink,
+int *ret_w, int *ret_h,
+int force_original_aspect_ratio, int force_divisible_by)
+{
+int w, h;
+int factor_w, factor_h;
+
+w = *ret_w;
+h = *ret_h;
  
  /* Check if it is requested that the result has to be divisible by a some

   * factor (w or h = -n with n being the factor). */

Re: [FFmpeg-devel] [PATCH] avfilter/scale.c: factorize ff_scale_eval_dimensions

2019-12-06 Thread Michael Niedermayer
On Wed, Dec 04, 2019 at 02:14:35PM +0530, Gyan wrote:
> Will help reduce code duplication when adding animation support to vf_scale.
> See Michael's last comment in https://patchwork.ffmpeg.org/patch/16272/
> 
> Gyan

>  doc/filters.texi |   40 +
>  libavfilter/scale.c  |   59 
> ++-
>  libavfilter/scale.h  |4 ++
>  libavfilter/vf_scale.c   |   28 ++--
>  libavfilter/vf_scale_cuda.c  |   11 
>  libavfilter/vf_scale_npp.c   |   11 
>  libavfilter/vf_scale_vaapi.c |   11 
>  7 files changed, 128 insertions(+), 36 deletions(-)
> 380bf799c14d6286cc625afe019aa553614a7d53  
> 0001-avfilter-scale.c-factorize-ff_scale_eval_dimensions.patch
> From 138a8dba766674a1b017614c58fa99aeca98e9e5 Mon Sep 17 00:00:00 2001
> From: Gyan Doshi 
> Date: Mon, 2 Dec 2019 21:11:21 +0530
> Subject: [PATCH] avfilter/scale.c: factorize ff_scale_eval_dimensions
> 
> Adjustment of evaluated values shifted to ff_adjust_scale_dimensions
> Shifted code for force_original_aspect_ratio and force_divisble_by from
> vf_scale so it is now available for scale_cuda, scale_npp and
> scale_vaapi as well.
> ---
>  doc/filters.texi | 40 
>  libavfilter/scale.c  | 59 +---
>  libavfilter/scale.h  |  4 +++
>  libavfilter/vf_scale.c   | 28 ++---
>  libavfilter/vf_scale_cuda.c  | 11 +++
>  libavfilter/vf_scale_npp.c   | 11 +++
>  libavfilter/vf_scale_vaapi.c | 11 +++
>  7 files changed, 128 insertions(+), 36 deletions(-)
> 
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 5fdec6f015..9129f7e3a5 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -16210,6 +16210,46 @@ Supersampling
>  @item lanczos
>  @end table
>  
> +@item force_original_aspect_ratio
> +Enable decreasing or increasing output video width or height if necessary to
> +keep the original aspect ratio. Possible values:
> +
> +@table @samp
> +@item disable
> +Scale the video as specified and disable this feature.
> +
> +@item decrease
> +The output video dimensions will automatically be decreased if needed.
> +
> +@item increase
> +The output video dimensions will automatically be increased if needed.
> +
> +@end table
> +
> +One useful instance of this option is that when you know a specific device's
> +maximum allowed resolution, you can use this to limit the output video to
> +that, while retaining the aspect ratio. For example, device A allows
> +1280x720 playback, and your video is 1920x800. Using this option (set it to
> +decrease) and specifying 1280x720 to the command line makes the output
> +1280x533.
> +
> +Please note that this is a different thing than specifying -1 for @option{w}
> +or @option{h}, you still need to specify the output resolution for this 
> option
> +to work.
> +
> +@item force_divisible_by
> +Ensures that both the output dimensions, width and height, are divisible by 
> the
> +given integer when used together with @option{force_original_aspect_ratio}. 
> This
> +works similar to using @code{-n} in the @option{w} and @option{h} options.
> +
> +This option respects the value set for @option{force_original_aspect_ratio},
> +increasing or decreasing the resolution accordingly. The video's aspect ratio
> +may be slightly modified.
> +
> +This option can be handy if you need to have a video fit within or exceed
> +a defined resolution using @option{force_original_aspect_ratio} but also have
> +encoder restrictions on width or height divisibility.
> +
>  @end table
>  
>  @section scale2ref
> diff --git a/libavfilter/scale.c b/libavfilter/scale.c
> index eaee95fac6..5e9f97230c 100644
> --- a/libavfilter/scale.c
> +++ b/libavfilter/scale.c
> @@ -111,8 +111,6 @@ int ff_scale_eval_dimensions(void *log_ctx,
>  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
>  const AVPixFmtDescriptor *out_desc = 
> av_pix_fmt_desc_get(outlink->format);
>  const char *expr;
> -int w, h;
> -int factor_w, factor_h;
>  int eval_w, eval_h;
>  int ret;
>  const char scale2ref = outlink->src->nb_inputs == 2 && 
> outlink->src->inputs[1] == inlink;
> @@ -172,8 +170,28 @@ int ff_scale_eval_dimensions(void *log_ctx,
>  goto fail;
>  eval_w = (int) res == 0 ? inlink->w : (int) res;
>  
> -w = eval_w;
> -h = eval_h;
> +*ret_w = eval_w;
> +*ret_h = eval_h;
> +
> +return 0;
> +
> +fail:
> +av_log(log_ctx, AV_LOG_ERROR,
> +   "Error when evaluating the expression '%s'.\n"
> +   "Maybe the expression for out_w:'%s' or for out_h:'%s' is 
> self-referencing.\n",
> +   expr, w_expr, h_expr);
> +return ret;
> +}
> +
> +int ff_scale_adjust_dimensions(AVFilterLink *inlink,
> +int *ret_w, int *ret_h,
> +int force_original_aspect_ratio, int force_divisible_by)
> +{
> +int w, h;
> +int factor_w, factor_h;
> +
> + 

Re: [FFmpeg-devel] [PATCH] avfilter/scale.c: factorize ff_scale_eval_dimensions

2019-12-06 Thread Gyan



On 04-12-2019 02:14 pm, Gyan wrote:
Will help reduce code duplication when adding animation support to 
vf_scale.

See Michael's last comment in https://patchwork.ffmpeg.org/patch/16272/


Ping.

Gyan
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH] avfilter/scale.c: factorize ff_scale_eval_dimensions

2019-12-04 Thread Gyan

Will help reduce code duplication when adding animation support to vf_scale.
See Michael's last comment in https://patchwork.ffmpeg.org/patch/16272/

Gyan
From 138a8dba766674a1b017614c58fa99aeca98e9e5 Mon Sep 17 00:00:00 2001
From: Gyan Doshi 
Date: Mon, 2 Dec 2019 21:11:21 +0530
Subject: [PATCH] avfilter/scale.c: factorize ff_scale_eval_dimensions

Adjustment of evaluated values shifted to ff_adjust_scale_dimensions
Shifted code for force_original_aspect_ratio and force_divisble_by from
vf_scale so it is now available for scale_cuda, scale_npp and
scale_vaapi as well.
---
 doc/filters.texi | 40 
 libavfilter/scale.c  | 59 +---
 libavfilter/scale.h  |  4 +++
 libavfilter/vf_scale.c   | 28 ++---
 libavfilter/vf_scale_cuda.c  | 11 +++
 libavfilter/vf_scale_npp.c   | 11 +++
 libavfilter/vf_scale_vaapi.c | 11 +++
 7 files changed, 128 insertions(+), 36 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 5fdec6f015..9129f7e3a5 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -16210,6 +16210,46 @@ Supersampling
 @item lanczos
 @end table
 
+@item force_original_aspect_ratio
+Enable decreasing or increasing output video width or height if necessary to
+keep the original aspect ratio. Possible values:
+
+@table @samp
+@item disable
+Scale the video as specified and disable this feature.
+
+@item decrease
+The output video dimensions will automatically be decreased if needed.
+
+@item increase
+The output video dimensions will automatically be increased if needed.
+
+@end table
+
+One useful instance of this option is that when you know a specific device's
+maximum allowed resolution, you can use this to limit the output video to
+that, while retaining the aspect ratio. For example, device A allows
+1280x720 playback, and your video is 1920x800. Using this option (set it to
+decrease) and specifying 1280x720 to the command line makes the output
+1280x533.
+
+Please note that this is a different thing than specifying -1 for @option{w}
+or @option{h}, you still need to specify the output resolution for this option
+to work.
+
+@item force_divisible_by
+Ensures that both the output dimensions, width and height, are divisible by the
+given integer when used together with @option{force_original_aspect_ratio}. 
This
+works similar to using @code{-n} in the @option{w} and @option{h} options.
+
+This option respects the value set for @option{force_original_aspect_ratio},
+increasing or decreasing the resolution accordingly. The video's aspect ratio
+may be slightly modified.
+
+This option can be handy if you need to have a video fit within or exceed
+a defined resolution using @option{force_original_aspect_ratio} but also have
+encoder restrictions on width or height divisibility.
+
 @end table
 
 @section scale2ref
diff --git a/libavfilter/scale.c b/libavfilter/scale.c
index eaee95fac6..5e9f97230c 100644
--- a/libavfilter/scale.c
+++ b/libavfilter/scale.c
@@ -111,8 +111,6 @@ int ff_scale_eval_dimensions(void *log_ctx,
 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
 const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
 const char *expr;
-int w, h;
-int factor_w, factor_h;
 int eval_w, eval_h;
 int ret;
 const char scale2ref = outlink->src->nb_inputs == 2 && 
outlink->src->inputs[1] == inlink;
@@ -172,8 +170,28 @@ int ff_scale_eval_dimensions(void *log_ctx,
 goto fail;
 eval_w = (int) res == 0 ? inlink->w : (int) res;
 
-w = eval_w;
-h = eval_h;
+*ret_w = eval_w;
+*ret_h = eval_h;
+
+return 0;
+
+fail:
+av_log(log_ctx, AV_LOG_ERROR,
+   "Error when evaluating the expression '%s'.\n"
+   "Maybe the expression for out_w:'%s' or for out_h:'%s' is 
self-referencing.\n",
+   expr, w_expr, h_expr);
+return ret;
+}
+
+int ff_scale_adjust_dimensions(AVFilterLink *inlink,
+int *ret_w, int *ret_h,
+int force_original_aspect_ratio, int force_divisible_by)
+{
+int w, h;
+int factor_w, factor_h;
+
+w = *ret_w;
+h = *ret_h;
 
 /* Check if it is requested that the result has to be divisible by a some
  * factor (w or h = -n with n being the factor). */
@@ -199,15 +217,34 @@ int ff_scale_eval_dimensions(void *log_ctx,
 if (h < 0)
 h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h;
 
+/* Note that force_original_aspect_ratio may overwrite the previous set
+ * 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);
+
+if (force_original_aspect_ratio == 1) {
+ w = FFMIN(tmp_w, w);
+ h = FFMIN(tmp_h, h);
+ if (force_divisible_by > 1) {
+