From 7fc4c184630043587ace5b44ef70ccf70c7409fe Mon Sep 17 00:00:00 2001 From: Lars Kiesow Date: Sat, 25 Jan 2014 14:40:48 +0100 Subject: [PATCH 1/4] Factors for scale filter --- libavfilter/vf_scale.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c index 2e692cff5b..bda536df91 100644 --- a/libavfilter/vf_scale.c +++ b/libavfilter/vf_scale.c @@ -270,11 +270,20 @@ static int config_props(AVFilterLink *outlink) w = scale->w; h = scale->h; - /* sanity check params */ - if (w < -1 || h < -1) { - av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n"); - return AVERROR(EINVAL); + /* 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). After we got the factor, + * we set w/h back to -1 so that the automatic scaling is done. */ + int factor_w = 1; + int factor_h = 1; + if (w < -1) { + factor_w = -w; + w = -1; } + if (h < -1) { + factor_h = -h; + h = -1; + } + if (w == -1 && h == -1) scale->w = scale->h = 0; @@ -287,6 +296,15 @@ static int config_props(AVFilterLink *outlink) if (h == -1) h = av_rescale(w, inlink->h, inlink->w); + /* Make sure that the result is divisible by the factor we determined + * earlier. If no factor was set, it is nothing will happen as the default + * factor is 1 */ + w = (w / factor_w) * factor_w; + h = (h / 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. */ if (scale->force_original_aspect_ratio) { int tmp_w = av_rescale(h, inlink->w, inlink->h); int tmp_h = av_rescale(w, inlink->h, inlink->w); From e395f8de5ac682d8c99673d431ad7c0575bf2a7e Mon Sep 17 00:00:00 2001 From: Lars Kiesow Date: Sat, 25 Jan 2014 15:00:02 +0100 Subject: [PATCH 2/4] Fixed factor for scale filter --- libavfilter/vf_scale.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c index bda536df91..2ea55ef441 100644 --- a/libavfilter/vf_scale.c +++ b/libavfilter/vf_scale.c @@ -236,6 +236,7 @@ static int config_props(AVFilterLink *outlink) double var_values[VARS_NB], res; char *expr; int ret; + int factor_w, factor_h; var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w; var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h; @@ -273,15 +274,17 @@ static int config_props(AVFilterLink *outlink) /* 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). After we got the factor, * we set w/h back to -1 so that the automatic scaling is done. */ - int factor_w = 1; - int factor_h = 1; + factor_w = 1; + factor_h = 1; if (w < -1) { factor_w = -w; w = -1; + scale->w = -1; } if (h < -1) { factor_h = -h; h = -1; + scale->h = -1; } if (w == -1 && h == -1) @@ -302,7 +305,6 @@ static int config_props(AVFilterLink *outlink) w = (w / factor_w) * factor_w; h = (h / 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. */ if (scale->force_original_aspect_ratio) { From 69b1d1d99bc5384d4ca305aa76e246a06c5b98b5 Mon Sep 17 00:00:00 2001 From: Lars Kiesow Date: Sat, 25 Jan 2014 15:02:15 +0100 Subject: [PATCH 3/4] Documentation for scale filter factor --- libavfilter/vf_scale.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c index 2ea55ef441..945ed121dd 100644 --- a/libavfilter/vf_scale.c +++ b/libavfilter/vf_scale.c @@ -81,6 +81,7 @@ typedef struct { * New dimensions. Special values are: * 0 = original width/height * -1 = keep original aspect + * -N = try to keep aspect but make sure it is divisible by N */ int w, h; char *size_str; From c49b0360966da9cdbdbafd0ce926269e777bc774 Mon Sep 17 00:00:00 2001 From: Lars Kiesow Date: Sat, 25 Jan 2014 15:34:23 +0100 Subject: [PATCH 4/4] Documentation for scale filter factor --- doc/filters.texi | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/filters.texi b/doc/filters.texi index 9e67db4888..28e7a24af9 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -6911,6 +6911,11 @@ maintains the aspect ratio of the input image, calculated from the other specified dimension. If both of them are -1, the input size is used +If one of the values is -n with n > 1, the scale filter will also use a value +that maintains the aspect ratio of the input image, calculated from the other +specified dimension. After that it will, however, make sure that the calculated +dimension is divisible by n and adjust the value if necessary. + See below for the list of accepted constants for use in the dimension expression.