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

Unified · Split

libavfilter/vf_scale.c+67−46
@@ -54,6 +54,13 @@ static const char *const var_names[] = { · 53 unmodified lines
54 "vsub",54 "vsub",
55 "ohsub",55 "ohsub",
56 "ovsub",56 "ovsub",
57 "main_w",
58 "main_h",
59 "main_a",
60 "main_sar",
61 "main_dar", "mdar",
62 "main_hsub",
63 "main_vsub",
57 NULL64 NULL
58};65};
59 66
@@ -69,39 +76,6 @@ enum var_name { · 9 unmodified lines
69 VAR_VSUB,76 VAR_VSUB,
70 VAR_OHSUB,77 VAR_OHSUB,
71 VAR_OVSUB,78 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 */
82static 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
104enum var_name_scale2ref {
105 VAR_S2R_MAIN_W,79 VAR_S2R_MAIN_W,
106 VAR_S2R_MAIN_H,80 VAR_S2R_MAIN_H,
107 VAR_S2R_MAIN_A,81 VAR_S2R_MAIN_A,
@@ -109,7 +83,7 @@ enum var_name_scale2ref { · 1 unmodified line
109 VAR_S2R_MAIN_DAR, VAR_S2R_MDAR,83 VAR_S2R_MAIN_DAR, VAR_S2R_MDAR,
110 VAR_S2R_MAIN_HSUB,84 VAR_S2R_MAIN_HSUB,
111 VAR_S2R_MAIN_VSUB,85 VAR_S2R_MAIN_VSUB,
112 VARS_S2R_NB86 VARS_NB
113};87};
114 88
115enum EvalMode {89enum EvalMode {
@@ -145,7 +119,7 @@ typedef struct ScaleContext { · 29 unmodified lines
145 char *h_expr; ///< height expression string119 char *h_expr; ///< height expression string
146 AVExpr *w_pexpr;120 AVExpr *w_pexpr;
147 AVExpr *h_pexpr;121 AVExpr *h_pexpr;
148 double var_values[VARS_NB + VARS_S2R_NB];122 double var_values[VARS_NB];
149 123
150 char *flags_str;124 char *flags_str;
151 125
@@ -173,14 +147,57 @@ AVFilter ff_vf_scale2ref; · 21 unmodified lines
173 147
174static int config_props(AVFilterLink *outlink);148static int config_props(AVFilterLink *outlink);
175 149
150static 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
176static int scale_parse_expr(AVFilterContext *ctx, char *str_expr, AVExpr **pexpr_ptr, const char *var, const char *args)195static int scale_parse_expr(AVFilterContext *ctx, char *str_expr, AVExpr **pexpr_ptr, const char *var, const char *args)
177{196{
178 ScaleContext *scale = ctx->priv;197 ScaleContext *scale = ctx->priv;
179 int ret, is_inited = 0;198 int ret, is_inited = 0;
180 char *old_str_expr = NULL;199 char *old_str_expr = NULL;
181 AVExpr *old_pexpr = NULL;200 AVExpr *old_pexpr = NULL;
182 const char scale2ref = ctx->filter == &ff_vf_scale2ref;
183 const char *const *names = scale2ref ? var_names_scale2ref : var_names;
184 201
185 if (str_expr) {202 if (str_expr) {
186 old_str_expr = av_strdup(str_expr);203 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
195 is_inited = 1;212 is_inited = 1;
196 }213 }
197 214
198 ret = av_expr_parse(pexpr_ptr, args, names,215 ret = av_expr_parse(pexpr_ptr, args, var_names,
199 NULL, NULL, NULL, NULL, 0, ctx);216 NULL, NULL, NULL, NULL, 0, ctx);
200 if (ret < 0) {217 if (ret < 0) {
201 av_log(ctx, AV_LOG_ERROR, "Cannot parse expression for %s: '%s'\n", var, args);218 av_log(ctx, AV_LOG_ERROR, "Cannot parse expression for %s: '%s'\n", var, args);
202 goto revert;219 goto revert;
203 }220 }
204 221
222 ret = check_exprs(ctx);
223 if (ret < 0)
224 goto revert;
225
205 if (is_inited && (ret = config_props(ctx->outputs[0])) < 0)226 if (is_inited && (ret = config_props(ctx->outputs[0])) < 0)
206 goto revert;227 goto revert;
207 228
@@ -391,15 +412,15 @@ static int scale_eval_dimensions(AVFilterContext *ctx) · 183 unmodified lines
391 scale->var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;412 scale->var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
392 413
393 if (scale2ref) {414 if (scale2ref) {
394 scale->var_values[VARS_NB + VAR_S2R_MAIN_W] = main_link->w;415 scale->var_values[VAR_S2R_MAIN_W] = main_link->w;
395 scale->var_values[VARS_NB + VAR_S2R_MAIN_H] = main_link->h;416 scale->var_values[VAR_S2R_MAIN_H] = main_link->h;
396 scale->var_values[VARS_NB + VAR_S2R_MAIN_A] = (double) main_link->w / main_link->h;417 scale->var_values[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 ?418 scale->var_values[VAR_S2R_MAIN_SAR] = main_link->sample_aspect_ratio.num ?
398 (double) main_link->sample_aspect_ratio.num / main_link->sample_aspect_ratio.den : 1;419 (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] =420 scale->var_values[VAR_S2R_MAIN_DAR] = scale->var_values[VAR_S2R_MDAR] =
400 scale->var_values[VARS_NB + VAR_S2R_MAIN_A] * scale->var_values[VARS_NB + VAR_S2R_MAIN_SAR];421 scale->var_values[VAR_S2R_MAIN_A] * scale->var_values[VAR_S2R_MAIN_SAR];
401 scale->var_values[VARS_NB + VAR_S2R_MAIN_HSUB] = 1 << main_desc->log2_chroma_w;422 scale->var_values[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;423 scale->var_values[VAR_S2R_MAIN_VSUB] = 1 << main_desc->log2_chroma_h;
403 }424 }
404 425
405 res = av_expr_eval(scale->w_pexpr, scale->var_values, NULL);426 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