aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAman Gupta <aman@tmm1.net>2017-02-01 16:30:18 -0800
committerMark Thompson <sw@jkqxz.net>2017-02-02 22:58:54 +0000
commit037bb4021c0b0b33c752850f58cf7e2ea44359b7 (patch)
treed92a56bd24ab29e5019cb1f71f4ef6caf4f204ca
parentc8467abbadab424757ea23f71a1036abfb7f14b4 (diff)
downloadffmpeg-037bb4021c0b0b33c752850f58cf7e2ea44359b7.tar.gz
avfilter/scale: refactor common code for scaling height/width expressions
Implements support for height/width expressions in vf_scale_vaapi, by refactoring common code into a new libavfilter/scale.c Signed-off-by: Mark Thompson <sw@jkqxz.net>
-rw-r--r--libavfilter/Makefile8
-rw-r--r--libavfilter/scale.c152
-rw-r--r--libavfilter/scale.h28
-rw-r--r--libavfilter/vf_scale.c109
-rw-r--r--libavfilter/vf_scale_npp.c93
-rw-r--r--libavfilter/vf_scale_vaapi.c19
6 files changed, 216 insertions, 193 deletions
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 68a94be4a6..3231f08a8d 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -257,10 +257,10 @@ OBJS-$(CONFIG_REPEATFIELDS_FILTER) += vf_repeatfields.o
OBJS-$(CONFIG_REVERSE_FILTER) += f_reverse.o
OBJS-$(CONFIG_ROTATE_FILTER) += vf_rotate.o
OBJS-$(CONFIG_SAB_FILTER) += vf_sab.o
-OBJS-$(CONFIG_SCALE_FILTER) += vf_scale.o
-OBJS-$(CONFIG_SCALE_NPP_FILTER) += vf_scale_npp.o
-OBJS-$(CONFIG_SCALE_VAAPI_FILTER) += vf_scale_vaapi.o
-OBJS-$(CONFIG_SCALE2REF_FILTER) += vf_scale.o
+OBJS-$(CONFIG_SCALE_FILTER) += vf_scale.o scale.o
+OBJS-$(CONFIG_SCALE_NPP_FILTER) += vf_scale_npp.o scale.o
+OBJS-$(CONFIG_SCALE_VAAPI_FILTER) += vf_scale_vaapi.o scale.o
+OBJS-$(CONFIG_SCALE2REF_FILTER) += vf_scale.o scale.o
OBJS-$(CONFIG_SELECT_FILTER) += f_select.o
OBJS-$(CONFIG_SELECTIVECOLOR_FILTER) += vf_selectivecolor.o
OBJS-$(CONFIG_SENDCMD_FILTER) += f_sendcmd.o
diff --git a/libavfilter/scale.c b/libavfilter/scale.c
new file mode 100644
index 0000000000..50cd442849
--- /dev/null
+++ b/libavfilter/scale.c
@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 2007 Bobby Bingham
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdint.h>
+#include "scale.h"
+#include "libavutil/eval.h"
+#include "libavutil/mathematics.h"
+#include "libavutil/pixdesc.h"
+
+static const char *const var_names[] = {
+ "PI",
+ "PHI",
+ "E",
+ "in_w", "iw",
+ "in_h", "ih",
+ "out_w", "ow",
+ "out_h", "oh",
+ "a",
+ "sar",
+ "dar",
+ "hsub",
+ "vsub",
+ "ohsub",
+ "ovsub",
+ NULL
+};
+
+enum var_name {
+ VAR_PI,
+ VAR_PHI,
+ VAR_E,
+ VAR_IN_W, VAR_IW,
+ VAR_IN_H, VAR_IH,
+ VAR_OUT_W, VAR_OW,
+ VAR_OUT_H, VAR_OH,
+ VAR_A,
+ VAR_SAR,
+ VAR_DAR,
+ VAR_HSUB,
+ VAR_VSUB,
+ VAR_OHSUB,
+ VAR_OVSUB,
+ VARS_NB
+};
+
+int ff_scale_eval_dimensions(void *log_ctx,
+ const char *w_expr, const char *h_expr,
+ AVFilterLink *inlink, AVFilterLink *outlink,
+ int *ret_w, int *ret_h)
+{
+ 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;
+ double var_values[VARS_NB], res;
+
+ var_values[VAR_PI] = M_PI;
+ var_values[VAR_PHI] = M_PHI;
+ var_values[VAR_E] = M_E;
+ var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
+ var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
+ var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
+ var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
+ var_values[VAR_A] = (double) inlink->w / inlink->h;
+ var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
+ (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
+ var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
+ var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
+ var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
+ var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w;
+ var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
+
+ /* evaluate width and height */
+ av_expr_parse_and_eval(&res, (expr = w_expr),
+ var_names, var_values,
+ NULL, NULL, NULL, NULL, NULL, 0, log_ctx);
+ eval_w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
+
+ if ((ret = av_expr_parse_and_eval(&res, (expr = h_expr),
+ var_names, var_values,
+ NULL, NULL, NULL, NULL, NULL, 0, log_ctx)) < 0)
+ goto fail;
+ eval_h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
+ /* evaluate again the width, as it may depend on the output height */
+ if ((ret = av_expr_parse_and_eval(&res, (expr = w_expr),
+ var_names, var_values,
+ NULL, NULL, NULL, NULL, NULL, 0, log_ctx)) < 0)
+ goto fail;
+ eval_w = res;
+
+ w = eval_w;
+ h = eval_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). */
+ factor_w = 1;
+ factor_h = 1;
+ if (w < -1) {
+ factor_w = -w;
+ }
+ if (h < -1) {
+ factor_h = -h;
+ }
+
+ if (w < 0 && h < 0)
+ eval_w = eval_h = 0;
+
+ if (!(w = eval_w))
+ w = inlink->w;
+ if (!(h = eval_h))
+ h = inlink->h;
+
+ /* 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 */
+ if (w < 0)
+ w = av_rescale(h, inlink->w, inlink->h * factor_w) * factor_w;
+ if (h < 0)
+ h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h;
+
+ *ret_w = w;
+ *ret_h = 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;
+}
diff --git a/libavfilter/scale.h b/libavfilter/scale.h
new file mode 100644
index 0000000000..dfe67d0be0
--- /dev/null
+++ b/libavfilter/scale.h
@@ -0,0 +1,28 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVFILTER_SCALE_H
+#define AVFILTER_SCALE_H
+
+#include "avfilter.h"
+
+int ff_scale_eval_dimensions(void *ctx,
+ const char *w_expr, const char *h_expr,
+ AVFilterLink *inlink, AVFilterLink *outlink,
+ int *ret_w, int *ret_h);
+#endif
diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c
index 22bee96d75..8645da7868 100644
--- a/libavfilter/vf_scale.c
+++ b/libavfilter/vf_scale.c
@@ -29,9 +29,9 @@
#include "avfilter.h"
#include "formats.h"
#include "internal.h"
+#include "scale.h"
#include "video.h"
#include "libavutil/avstring.h"
-#include "libavutil/eval.h"
#include "libavutil/internal.h"
#include "libavutil/mathematics.h"
#include "libavutil/opt.h"
@@ -41,43 +41,12 @@
#include "libavutil/avassert.h"
#include "libswscale/swscale.h"
-static const char *const var_names[] = {
- "in_w", "iw",
- "in_h", "ih",
- "out_w", "ow",
- "out_h", "oh",
- "a",
- "sar",
- "dar",
- "hsub",
- "vsub",
- "ohsub",
- "ovsub",
- NULL
-};
-
-enum var_name {
- VAR_IN_W, VAR_IW,
- VAR_IN_H, VAR_IH,
- VAR_OUT_W, VAR_OW,
- VAR_OUT_H, VAR_OH,
- VAR_A,
- VAR_SAR,
- VAR_DAR,
- VAR_HSUB,
- VAR_VSUB,
- VAR_OHSUB,
- VAR_OVSUB,
- VARS_NB
-};
-
enum EvalMode {
EVAL_MODE_INIT,
EVAL_MODE_FRAME,
EVAL_MODE_NB
};
-
typedef struct ScaleContext {
const AVClass *class;
struct SwsContext *sws; ///< software scaler context
@@ -256,74 +225,16 @@ static int config_props(AVFilterLink *outlink)
outlink->src->inputs[1] :
outlink->src->inputs[0];
enum AVPixelFormat outfmt = outlink->format;
- ScaleContext *scale = ctx->priv;
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
- const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
- int64_t w, h;
- double var_values[VARS_NB], res;
- char *expr;
+ ScaleContext *scale = ctx->priv;
+ int w, h;
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;
- var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
- var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
- var_values[VAR_A] = (double) inlink->w / inlink->h;
- var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
- (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
- var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
- var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
- var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
- var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w;
- var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
-
- /* evaluate width and height */
- av_expr_parse_and_eval(&res, (expr = scale->w_expr),
- var_names, var_values,
- NULL, NULL, NULL, NULL, NULL, 0, ctx);
- scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
- if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
- var_names, var_values,
- NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
- goto fail;
- scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
- /* evaluate again the width, as it may depend on the output height */
- if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
- var_names, var_values,
- NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
- goto fail;
- scale->w = res;
-
- w = scale->w;
- h = scale->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). */
- factor_w = 1;
- factor_h = 1;
- if (w < -1) {
- factor_w = -w;
- }
- if (h < -1) {
- factor_h = -h;
- }
-
- if (w < 0 && h < 0)
- scale->w = scale->h = 0;
- if (!(w = scale->w))
- w = inlink->w;
- if (!(h = scale->h))
- h = inlink->h;
-
- /* 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 */
- if (w < 0)
- w = av_rescale(h, inlink->w, inlink->h * factor_w) * factor_w;
- if (h < 0)
- h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h;
+ if ((ret = ff_scale_eval_dimensions(ctx,
+ scale->w_expr, scale->h_expr,
+ inlink, outlink,
+ &w, &h)) < 0)
+ goto fail;
/* Note that force_original_aspect_ratio may overwrite the previous set
* dimensions so that it is not divisible by the set factors anymore. */
@@ -439,10 +350,6 @@ static int config_props(AVFilterLink *outlink)
return 0;
fail:
- av_log(NULL, 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, scale->w_expr, scale->h_expr);
return ret;
}
diff --git a/libavfilter/vf_scale_npp.c b/libavfilter/vf_scale_npp.c
index 1abfdd1f11..4b45174742 100644
--- a/libavfilter/vf_scale_npp.c
+++ b/libavfilter/vf_scale_npp.c
@@ -27,17 +27,16 @@
#include "libavutil/avstring.h"
#include "libavutil/common.h"
-#include "libavutil/eval.h"
#include "libavutil/hwcontext.h"
#include "libavutil/hwcontext_cuda_internal.h"
#include "libavutil/internal.h"
-#include "libavutil/mathematics.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "avfilter.h"
#include "formats.h"
#include "internal.h"
+#include "scale.h"
#include "video.h"
static const enum AVPixelFormat supported_formats[] = {
@@ -50,32 +49,6 @@ static const enum AVPixelFormat deinterleaved_formats[][2] = {
{ AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P },
};
-static const char *const var_names[] = {
- "PI",
- "PHI",
- "E",
- "in_w", "iw",
- "in_h", "ih",
- "out_w", "ow",
- "out_h", "oh",
- "a", "dar",
- "sar",
- NULL
-};
-
-enum var_name {
- VAR_PI,
- VAR_PHI,
- VAR_E,
- VAR_IN_W, VAR_IW,
- VAR_IN_H, VAR_IH,
- VAR_OUT_W, VAR_OW,
- VAR_OUT_H, VAR_OH,
- VAR_A, VAR_DAR,
- VAR_SAR,
- VARS_NB
-};
-
enum ScaleStage {
STAGE_DEINTERLEAVE,
STAGE_RESIZE,
@@ -359,64 +332,18 @@ static int nppscale_config_props(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
AVFilterLink *inlink = outlink->src->inputs[0];
- NPPScaleContext *s = ctx->priv;
- int64_t w, h;
- double var_values[VARS_NB], res;
- char *expr;
+ NPPScaleContext *s = ctx->priv;
+ int w, h;
int ret;
- var_values[VAR_PI] = M_PI;
- var_values[VAR_PHI] = M_PHI;
- var_values[VAR_E] = M_E;
- var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
- var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
- var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
- var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
- var_values[VAR_A] = (double) inlink->w / inlink->h;
- var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
- (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
- var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
-
- /* evaluate width and height */
- av_expr_parse_and_eval(&res, (expr = s->w_expr),
- var_names, var_values,
- NULL, NULL, NULL, NULL, NULL, 0, ctx);
- s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
- if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
- var_names, var_values,
- NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
- goto fail;
- s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
- /* evaluate again the width, as it may depend on the output height */
- if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
- var_names, var_values,
- NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
+ if ((ret = ff_scale_eval_dimensions(s,
+ s->w_expr, s->h_expr,
+ inlink, outlink,
+ &w, &h)) < 0)
goto fail;
- s->w = res;
-
- w = s->w;
- h = s->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);
- }
- if (w == -1 && h == -1)
- s->w = s->h = 0;
-
- if (!(w = s->w))
- w = inlink->w;
- if (!(h = s->h))
- h = inlink->h;
- if (w == -1)
- w = av_rescale(h, inlink->w, inlink->h);
- if (h == -1)
- h = av_rescale(w, inlink->h, inlink->w);
-
- if (w > INT_MAX || h > INT_MAX ||
- (h * inlink->w) > INT_MAX ||
- (w * inlink->h) > INT_MAX)
+ if (((int64_t)h * inlink->w) > INT_MAX ||
+ ((int64_t)w * inlink->h) > INT_MAX)
av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
outlink->w = w;
@@ -439,8 +366,6 @@ static int nppscale_config_props(AVFilterLink *outlink)
return 0;
fail:
- av_log(NULL, AV_LOG_ERROR,
- "Error when evaluating the expression '%s'\n", expr);
return ret;
}
diff --git a/libavfilter/vf_scale_vaapi.c b/libavfilter/vf_scale_vaapi.c
index d1cb246d1f..604cd95767 100644
--- a/libavfilter/vf_scale_vaapi.c
+++ b/libavfilter/vf_scale_vaapi.c
@@ -31,6 +31,7 @@
#include "avfilter.h"
#include "formats.h"
#include "internal.h"
+#include "scale.h"
typedef struct ScaleVAAPIContext {
const AVClass *class;
@@ -50,9 +51,12 @@ typedef struct ScaleVAAPIContext {
char *output_format_string;
enum AVPixelFormat output_format;
- int output_width;
- int output_height;
+ char *w_expr; // width expression string
+ char *h_expr; // height expression string
+
+ int output_width; // computed width
+ int output_height; // computed height
} ScaleVAAPIContext;
@@ -110,6 +114,7 @@ static int scale_vaapi_config_input(AVFilterLink *inlink)
static int scale_vaapi_config_output(AVFilterLink *outlink)
{
+ AVFilterLink *inlink = outlink->src->inputs[0];
AVFilterContext *avctx = outlink->src;
ScaleVAAPIContext *ctx = avctx->priv;
AVVAAPIHWConfig *hwconfig = NULL;
@@ -162,6 +167,12 @@ static int scale_vaapi_config_output(AVFilterLink *outlink)
}
}
+ if ((err = ff_scale_eval_dimensions(ctx,
+ ctx->w_expr, ctx->h_expr,
+ inlink, outlink,
+ &ctx->output_width, &ctx->output_height)) < 0)
+ goto fail;
+
if (ctx->output_width < constraints->min_width ||
ctx->output_height < constraints->min_height ||
ctx->output_width > constraints->max_width ||
@@ -414,9 +425,9 @@ static av_cold void scale_vaapi_uninit(AVFilterContext *avctx)
#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
static const AVOption scale_vaapi_options[] = {
{ "w", "Output video width",
- OFFSET(output_width), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
+ OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, .flags = FLAGS },
{ "h", "Output video height",
- OFFSET(output_height), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
+ OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, .flags = FLAGS },
{ "format", "Output video format (software format of hardware frames)",
OFFSET(output_format_string), AV_OPT_TYPE_STRING, .flags = FLAGS },
{ NULL },