History

avfilter/scale: add function to check expressions

Allows finer identification and logging of invalid expressions and use of a single list for symbols and their index for both scale and scale2ref.

Gyan Doshi committed 6 years ago

Commit ce2cfa61 parent 6310fbd1 file · +67 −46Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/vf_scale.c+67−46
@@ -54,6 +54,13 @@ static const char *const var_names[] = { · 53 unmodified lines
5454 "vsub",
5555 "ohsub",
5656 "ovsub",
57+ "main_w",
58+ "main_h",
59+ "main_a",
60+ "main_sar",
61+ "main_dar", "mdar",
62+ "main_hsub",
63+ "main_vsub",
5764 NULL
5865 };
5966
@@ -69,39 +76,6 @@ enum var_name { · 9 unmodified lines
6976 VAR_VSUB,
7077 VAR_OHSUB,
7178 VAR_OVSUB,
72− VARS_NB
73−};
74−
75−/**
76− * This must be kept in sync with var_names so that it is always a
77− * complete list of var_names with the scale2ref specific names
78− * appended. scale2ref values must appear in the order they appear
79− * in the var_name_scale2ref enum but also be below all of the
80− * non-scale2ref specific values.
81− */
82−static const char *const var_names_scale2ref[] = {
83− "in_w", "iw",
84− "in_h", "ih",
85− "out_w", "ow",
86− "out_h", "oh",
87− "a",
88− "sar",
89− "dar",
90− "hsub",
91− "vsub",
92− "ohsub",
93− "ovsub",
94− "main_w",
95− "main_h",
96− "main_a",
97− "main_sar",
98− "main_dar", "mdar",
99− "main_hsub",
100− "main_vsub",
101− NULL
102−};
103−
104−enum var_name_scale2ref {
10579 VAR_S2R_MAIN_W,
10680 VAR_S2R_MAIN_H,
10781 VAR_S2R_MAIN_A,
@@ -109,7 +83,7 @@ enum var_name_scale2ref { · 1 unmodified line
10983 VAR_S2R_MAIN_DAR, VAR_S2R_MDAR,
11084 VAR_S2R_MAIN_HSUB,
11185 VAR_S2R_MAIN_VSUB,
112− VARS_S2R_NB
86+ VARS_NB
11387 };
11488
11589 enum EvalMode {
@@ -145,7 +119,7 @@ typedef struct ScaleContext { · 29 unmodified lines
145119 char *h_expr; ///< height expression string
146120 AVExpr *w_pexpr;
147121 AVExpr *h_pexpr;
148− double var_values[VARS_NB + VARS_S2R_NB];
122+ double var_values[VARS_NB];
149123
150124 char *flags_str;
151125
@@ -173,14 +147,57 @@ AVFilter ff_vf_scale2ref; · 21 unmodified lines
173147
174148 static int config_props(AVFilterLink *outlink);
175149
150+static int check_exprs(AVFilterContext *ctx)
151+{
152+ ScaleContext *scale = ctx->priv;
153+ unsigned vars_w[VARS_NB] = { 0 }, vars_h[VARS_NB] = { 0 };
154+
155+ if (!scale->w_pexpr && !scale->h_pexpr)
156+ return AVERROR(EINVAL);
157+
158+ if (scale->w_pexpr)
159+ av_expr_count_vars(scale->w_pexpr, vars_w, VARS_NB);
160+ if (scale->h_pexpr)
161+ av_expr_count_vars(scale->h_pexpr, vars_h, VARS_NB);
162+
163+ if (vars_w[VAR_OUT_W] || vars_w[VAR_OW]) {
164+ av_log(ctx, AV_LOG_ERROR, "Width expression cannot be self-referencing: '%s'.\n", scale->w_expr);
165+ return AVERROR(EINVAL);
166+ }
167+
168+ if (vars_h[VAR_OUT_H] || vars_h[VAR_OH]) {
169+ av_log(ctx, AV_LOG_ERROR, "Height expression cannot be self-referencing: '%s'.\n", scale->h_expr);
170+ return AVERROR(EINVAL);
171+ }
172+
173+ if ((vars_w[VAR_OUT_H] || vars_w[VAR_OH]) &&
174+ (vars_h[VAR_OUT_W] || vars_h[VAR_OW])) {
175+ av_log(ctx, AV_LOG_ERROR, "Circular expressions invalid for width '%s' and height '%s'.\n", scale->w_expr, scale->h_expr);
176+ return AVERROR(EINVAL);
177+ }
178+
179+ if (ctx->filter != &ff_vf_scale2ref &&
180+ (vars_w[VAR_S2R_MAIN_W] || vars_h[VAR_S2R_MAIN_W] ||
181+ vars_w[VAR_S2R_MAIN_H] || vars_h[VAR_S2R_MAIN_H] ||
182+ vars_w[VAR_S2R_MAIN_A] || vars_h[VAR_S2R_MAIN_A] ||
183+ vars_w[VAR_S2R_MAIN_SAR] || vars_h[VAR_S2R_MAIN_SAR] ||
184+ vars_w[VAR_S2R_MAIN_DAR] || vars_h[VAR_S2R_MAIN_DAR] ||
185+ vars_w[VAR_S2R_MDAR] || vars_h[VAR_S2R_MDAR] ||
186+ vars_w[VAR_S2R_MAIN_HSUB] || vars_h[VAR_S2R_MAIN_HSUB] ||
187+ vars_w[VAR_S2R_MAIN_VSUB] || vars_h[VAR_S2R_MAIN_VSUB]) ) {
188+ av_log(ctx, AV_LOG_ERROR, "Expressions with scale2ref variables are not valid in scale filter.\n");
189+ return AVERROR(EINVAL);
190+ }
191+
192+ return 0;
193+}
194+
176195 static int scale_parse_expr(AVFilterContext *ctx, char *str_expr, AVExpr **pexpr_ptr, const char *var, const char *args)
177196 {
178197 ScaleContext *scale = ctx->priv;
179198 int ret, is_inited = 0;
180199 char *old_str_expr = NULL;
181200 AVExpr *old_pexpr = NULL;
182− const char scale2ref = ctx->filter == &ff_vf_scale2ref;
183− const char *const *names = scale2ref ? var_names_scale2ref : var_names;
184201
185202 if (str_expr) {
186203 old_str_expr = av_strdup(str_expr);
@@ -195,13 +212,17 @@ static int scale_parse_expr(AVFilterContext *ctx, char *str_expr, AVExpr **pexpr · 8 unmodified lines
195212 is_inited = 1;
196213 }
197214
198− ret = av_expr_parse(pexpr_ptr, args, names,
215+ ret = av_expr_parse(pexpr_ptr, args, var_names,
199216 NULL, NULL, NULL, NULL, 0, ctx);
200217 if (ret < 0) {
201218 av_log(ctx, AV_LOG_ERROR, "Cannot parse expression for %s: '%s'\n", var, args);
202219 goto revert;
203220 }
204221
222+ ret = check_exprs(ctx);
223+ if (ret < 0)
224+ goto revert;
225+
205226 if (is_inited && (ret = config_props(ctx->outputs[0])) < 0)
206227 goto revert;
207228
@@ -391,15 +412,15 @@ static int scale_eval_dimensions(AVFilterContext *ctx) · 183 unmodified lines
391412 scale->var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
392413
393414 if (scale2ref) {
394− scale->var_values[VARS_NB + VAR_S2R_MAIN_W] = main_link->w;
395− scale->var_values[VARS_NB + VAR_S2R_MAIN_H] = main_link->h;
396− scale->var_values[VARS_NB + VAR_S2R_MAIN_A] = (double) main_link->w / main_link->h;
397− scale->var_values[VARS_NB + VAR_S2R_MAIN_SAR] = main_link->sample_aspect_ratio.num ?
415+ scale->var_values[VAR_S2R_MAIN_W] = main_link->w;
416+ scale->var_values[VAR_S2R_MAIN_H] = main_link->h;
417+ scale->var_values[VAR_S2R_MAIN_A] = (double) main_link->w / main_link->h;
418+ scale->var_values[VAR_S2R_MAIN_SAR] = main_link->sample_aspect_ratio.num ?
398419 (double) main_link->sample_aspect_ratio.num / main_link->sample_aspect_ratio.den : 1;
399− scale->var_values[VARS_NB + VAR_S2R_MAIN_DAR] = scale->var_values[VARS_NB + VAR_S2R_MDAR] =
400− scale->var_values[VARS_NB + VAR_S2R_MAIN_A] * scale->var_values[VARS_NB + VAR_S2R_MAIN_SAR];
401− scale->var_values[VARS_NB + VAR_S2R_MAIN_HSUB] = 1 << main_desc->log2_chroma_w;
402− scale->var_values[VARS_NB + VAR_S2R_MAIN_VSUB] = 1 << main_desc->log2_chroma_h;
420+ scale->var_values[VAR_S2R_MAIN_DAR] = scale->var_values[VAR_S2R_MDAR] =
421+ scale->var_values[VAR_S2R_MAIN_A] * scale->var_values[VAR_S2R_MAIN_SAR];
422+ scale->var_values[VAR_S2R_MAIN_HSUB] = 1 << main_desc->log2_chroma_w;
423+ scale->var_values[VAR_S2R_MAIN_VSUB] = 1 << main_desc->log2_chroma_h;
403424 }
404425
405426 res = av_expr_eval(scale->w_pexpr, scale->var_values, NULL);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t