FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/eval.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 420 471 89.2%
Functions: 18 20 90.0%
Branches: 377 463 81.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * simple arithmetic expression evaluator.
25 *
26 * see http://joe.hotchkiss.com/programming/eval/eval.html
27 */
28
29 #include <float.h>
30 #include "attributes.h"
31 #include "avutil.h"
32 #include "common.h"
33 #include "eval.h"
34 #include "ffmath.h"
35 #include "internal.h"
36 #include "log.h"
37 #include "mathematics.h"
38 #include "sfc64.h"
39 #include "time.h"
40 #include "avstring.h"
41 #include "timer.h"
42 #include "reverse.h"
43
44 typedef struct Parser {
45 const AVClass *class;
46 int stack_index;
47 char *s;
48 const double *const_values;
49 const char * const *const_names; // NULL terminated
50 double (* const *funcs1)(void *, double a); // NULL terminated
51 const char * const *func1_names; // NULL terminated
52 double (* const *funcs2)(void *, double a, double b); // NULL terminated
53 const char * const *func2_names; // NULL terminated
54 void *opaque;
55 int log_offset;
56 void *log_ctx;
57 #define VARS 10
58 double *var;
59 FFSFC64 *prng_state;
60 } Parser;
61
62 static const AVClass eval_class = {
63 .class_name = "Eval",
64 .item_name = av_default_item_name,
65 .option = NULL,
66 .version = LIBAVUTIL_VERSION_INT,
67 .log_level_offset_offset = offsetof(Parser, log_offset),
68 .parent_log_context_offset = offsetof(Parser, log_ctx),
69 };
70
71 static const struct {
72 double bin_val;
73 double dec_val;
74 int8_t exp;
75 } si_prefixes['z' - 'E' + 1] = {
76 ['y'-'E']= { 8.271806125530276749e-25, 1e-24, -24 },
77 ['z'-'E']= { 8.4703294725430034e-22, 1e-21, -21 },
78 ['a'-'E']= { 8.6736173798840355e-19, 1e-18, -18 },
79 ['f'-'E']= { 8.8817841970012523e-16, 1e-15, -15 },
80 ['p'-'E']= { 9.0949470177292824e-13, 1e-12, -12 },
81 ['n'-'E']= { 9.3132257461547852e-10, 1e-9, -9 },
82 ['u'-'E']= { 9.5367431640625e-7, 1e-6, -6 },
83 ['m'-'E']= { 9.765625e-4, 1e-3, -3 },
84 ['c'-'E']= { 9.8431332023036951e-3, 1e-2, -2 },
85 ['d'-'E']= { 9.921256574801246e-2, 1e-1, -1 },
86 ['h'-'E']= { 1.0159366732596479e2, 1e2, 2 },
87 ['k'-'E']= { 1.024e3, 1e3, 3 },
88 ['K'-'E']= { 1.024e3, 1e3, 3 },
89 ['M'-'E']= { 1.048576e6, 1e6, 6 },
90 ['G'-'E']= { 1.073741824e9, 1e9, 9 },
91 ['T'-'E']= { 1.099511627776e12, 1e12, 12 },
92 ['P'-'E']= { 1.125899906842624e15, 1e15, 15 },
93 ['E'-'E']= { 1.152921504606847e18, 1e18, 18 },
94 ['Z'-'E']= { 1.1805916207174113e21, 1e21, 21 },
95 ['Y'-'E']= { 1.2089258196146292e24, 1e24, 24 },
96 };
97
98 static const struct {
99 const char *name;
100 double value;
101 } constants[] = {
102 { "E", M_E },
103 { "PI", M_PI },
104 { "PHI", M_PHI },
105 { "QP2LAMBDA", FF_QP2LAMBDA },
106 };
107
108 122528 double av_strtod(const char *numstr, char **tail)
109 {
110 double d;
111 char *next;
112
4/4
✓ Branch 0 taken 30780 times.
✓ Branch 1 taken 91748 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 30776 times.
122528 if(numstr[0]=='0' && (numstr[1]|0x20)=='x') {
113 4 d = strtoul(numstr, &next, 16);
114 } else
115 122524 d = strtod(numstr, &next);
116 /* if parsing succeeded, check for and interpret postfixes */
117
2/2
✓ Branch 0 taken 90138 times.
✓ Branch 1 taken 32390 times.
122528 if (next!=numstr) {
118
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 90132 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
90138 if (next[0] == 'd' && next[1] == 'B') {
119 /* treat dB as decibels instead of decibytes */
120 6 d = ff_exp10(d / 20);
121 6 next += 2;
122
3/4
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 89954 times.
✓ Branch 2 taken 178 times.
✗ Branch 3 not taken.
90132 } else if (*next >= 'E' && *next <= 'z') {
123 178 int e= si_prefixes[*next - 'E'].exp;
124
2/2
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 2 times.
178 if (e) {
125
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 170 times.
176 if (next[1] == 'i') {
126 6 d*= si_prefixes[*next - 'E'].bin_val;
127 6 next+=2;
128 } else {
129 170 d*= si_prefixes[*next - 'E'].dec_val;
130 170 next++;
131 }
132 }
133 }
134
135
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 90137 times.
90138 if (*next=='B') {
136 1 d*=8;
137 1 next++;
138 }
139 }
140 /* if requested, fill in tail with the position after the last parsed
141 character */
142
2/2
✓ Branch 0 taken 122523 times.
✓ Branch 1 taken 5 times.
122528 if (tail)
143 122523 *tail = next;
144 122528 return d;
145 }
146
147 #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
148
149 389876 static int strmatch(const char *s, const char *prefix)
150 {
151 int i;
152
2/2
✓ Branch 0 taken 451400 times.
✓ Branch 1 taken 28408 times.
479808 for (i=0; prefix[i]; i++) {
153
2/2
✓ Branch 0 taken 361468 times.
✓ Branch 1 taken 89932 times.
451400 if (prefix[i] != s[i]) return 0;
154 }
155 /* return 1 only if the s identifier is terminated */
156
6/8
✓ Branch 0 taken 28408 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28382 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 28369 times.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 28369 times.
✗ Branch 7 not taken.
28408 return !IS_IDENTIFIER_CHAR(s[i]);
157 }
158
159 struct AVExpr {
160 enum {
161 e_value, e_const, e_func0, e_func1, e_func2,
162 e_squish, e_gauss, e_ld, e_isnan, e_isinf,
163 e_mod, e_max, e_min, e_eq, e_gt, e_gte, e_lte, e_lt,
164 e_pow, e_mul, e_div, e_add,
165 e_last, e_st, e_while, e_taylor, e_root, e_floor, e_ceil, e_trunc, e_round,
166 e_sqrt, e_not, e_random, e_hypot, e_gcd,
167 e_if, e_ifnot, e_print, e_bitand, e_bitor, e_between, e_clip, e_atan2, e_lerp,
168 e_sgn, e_randomi
169 } type;
170 double value; // is sign in other types
171 int const_index;
172 union {
173 double (*func0)(double);
174 double (*func1)(void *, double);
175 double (*func2)(void *, double, double);
176 } a;
177 struct AVExpr *param[3];
178 double *var;
179 FFSFC64 *prng_state;
180 };
181
182 static double etime(double v)
183 {
184 return av_gettime() * 0.000001;
185 }
186
187 207071974 static double eval_expr(Parser *p, AVExpr *e)
188 {
189
25/29
✓ Branch 0 taken 66650412 times.
✓ Branch 1 taken 38796805 times.
✓ Branch 2 taken 17334499 times.
✓ Branch 3 taken 280986 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 1695 times.
✓ Branch 8 taken 4 times.
✓ Branch 9 taken 3 times.
✓ Branch 10 taken 20 times.
✓ Branch 11 taken 2 times.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 2 times.
✓ Branch 16 taken 299 times.
✓ Branch 17 taken 32885 times.
✓ Branch 18 taken 4 times.
✓ Branch 19 taken 3 times.
✓ Branch 20 taken 1323198 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 times.
✓ Branch 23 taken 264913 times.
✗ Branch 24 not taken.
✓ Branch 25 taken 4 times.
✓ Branch 26 taken 2 times.
✓ Branch 27 taken 2 times.
✓ Branch 28 taken 82386225 times.
207071974 switch (e->type) {
190 66650412 case e_value: return e->value;
191 38796805 case e_const: return e->value * p->const_values[e->const_index];
192 17334499 case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
193 280986 case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
194 6 case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
195 1 case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
196 1 case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
197 1695 case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
198
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
4 case e_isnan: return e->value * !!isnan(eval_expr(p, e->param[0]));
199
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
3 case e_isinf: return e->value * !!isinf(eval_expr(p, e->param[0]));
200 20 case e_floor: return e->value * floor(eval_expr(p, e->param[0]));
201 2 case e_ceil : return e->value * ceil (eval_expr(p, e->param[0]));
202 2 case e_trunc: return e->value * trunc(eval_expr(p, e->param[0]));
203 case e_round: return e->value * round(eval_expr(p, e->param[0]));
204 case e_sgn: return e->value * FFDIFFSIGN(eval_expr(p, e->param[0]), 0);
205 2 case e_sqrt: return e->value * sqrt (eval_expr(p, e->param[0]));
206
2/2
✓ Branch 1 taken 201 times.
✓ Branch 2 taken 98 times.
299 case e_not: return e->value * (eval_expr(p, e->param[0]) == 0);
207
2/2
✓ Branch 1 taken 16391 times.
✓ Branch 2 taken 16494 times.
49379 case e_if: return e->value * (eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) :
208
2/2
✓ Branch 0 taken 16493 times.
✓ Branch 1 taken 1 times.
16494 e->param[2] ? eval_expr(p, e->param[2]) : 0);
209
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
6 case e_ifnot: return e->value * (!eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) :
210
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 e->param[2] ? eval_expr(p, e->param[2]) : 0);
211 3 case e_clip: {
212 3 double x = eval_expr(p, e->param[0]);
213 3 double min = eval_expr(p, e->param[1]), max = eval_expr(p, e->param[2]);
214
6/8
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
3 if (isnan(min) || isnan(max) || isnan(x) || min > max)
215 3 return NAN;
216 return e->value * av_clipd(eval_expr(p, e->param[0]), min, max);
217 }
218 1323198 case e_between: {
219 1323198 double d = eval_expr(p, e->param[0]);
220
4/4
✓ Branch 1 taken 793893 times.
✓ Branch 2 taken 529305 times.
✓ Branch 3 taken 176442 times.
✓ Branch 4 taken 617451 times.
2117091 return e->value * (d >= eval_expr(p, e->param[1]) &&
221 793893 d <= eval_expr(p, e->param[2]));
222 }
223 case e_lerp: {
224 double v0 = eval_expr(p, e->param[0]);
225 double v1 = eval_expr(p, e->param[1]);
226 double f = eval_expr(p, e->param[2]);
227 return v0 + (v1 - v0) * f;
228 }
229 1 case e_print: {
230 1 double x = eval_expr(p, e->param[0]);
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 int level = e->param[1] ? av_clip(eval_expr(p, e->param[1]), INT_MIN, INT_MAX) : AV_LOG_INFO;
232 1 av_log(p, level, "%f\n", x);
233 1 return x;
234 }
235
236 #define COMPUTE_NEXT_RANDOM() \
237 int idx = av_clip(eval_expr(p, e->param[0]), 0, VARS-1); \
238 FFSFC64 *s = p->prng_state + idx; \
239 uint64_t r; \
240 \
241 if (!s->counter) { \
242 r = isnan(p->var[idx]) ? 0 : p->var[idx]; \
243 ff_sfc64_init(s, r, r, r, 12); \
244 } \
245 r = ff_sfc64_get(s); \
246 p->var[idx] = r; \
247
248 264913 case e_random: {
249
3/4
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 264859 times.
✓ Branch 3 taken 54 times.
✗ Branch 4 not taken.
264913 COMPUTE_NEXT_RANDOM();
250 264913 return r * (1.0/UINT64_MAX);
251 }
252 case e_randomi: {
253 double min = eval_expr(p, e->param[1]);
254 double max = eval_expr(p, e->param[2]);
255 COMPUTE_NEXT_RANDOM();
256 return min + (max - min) * r / UINT64_MAX;
257 }
258 4 case e_while: {
259 4 double d = NAN;
260
2/2
✓ Branch 1 taken 210 times.
✓ Branch 2 taken 4 times.
214 while (eval_expr(p, e->param[0]))
261 210 d=eval_expr(p, e->param[1]);
262 4 return d;
263 }
264 2 case e_taylor: {
265 2 double t = 1, d = 0, v;
266 2 double x = eval_expr(p, e->param[1]);
267
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 int id = e->param[2] ? av_clip(eval_expr(p, e->param[2]), 0, VARS-1) : 0;
268 int i;
269 2 double var0 = p->var[id];
270
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 for(i=0; i<1000; i++) {
271 43 double ld = d;
272 43 p->var[id] = i;
273 43 v = eval_expr(p, e->param[0]);
274 43 d += t*v;
275
4/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 12 times.
43 if(ld==d && v)
276 2 break;
277 41 t *= x / (i+1);
278 }
279 2 p->var[id] = var0;
280 2 return d;
281 }
282 2 case e_root: {
283 int i, j;
284 2 double low = -1, high = -1, v, low_v = -DBL_MAX, high_v = DBL_MAX;
285 2 double var0 = p->var[0];
286 2 double x_max = eval_expr(p, e->param[1]);
287
1/2
✓ Branch 0 taken 491 times.
✗ Branch 1 not taken.
491 for(i=-1; i<1024; i++) {
288
2/2
✓ Branch 0 taken 315 times.
✓ Branch 1 taken 176 times.
491 if(i<255) {
289 315 p->var[0] = ff_reverse[i&255]*x_max/255;
290 } else {
291 176 p->var[0] = x_max*pow(0.9, i-255);
292
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 88 times.
176 if (i&1) p->var[0] *= -1;
293
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 88 times.
176 if (i&2) p->var[0] += low;
294 88 else p->var[0] += high;
295 }
296 491 v = eval_expr(p, e->param[0]);
297
4/4
✓ Branch 0 taken 433 times.
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 403 times.
491 if (v<=0 && v>low_v) {
298 30 low = p->var[0];
299 30 low_v = v;
300 }
301
4/4
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 432 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 53 times.
491 if (v>=0 && v<high_v) {
302 6 high = p->var[0];
303 6 high_v = v;
304 }
305
4/4
✓ Branch 0 taken 433 times.
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 431 times.
491 if (low>=0 && high>=0){
306
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 for (j=0; j<1000; j++) {
307 48 p->var[0] = (low+high)*0.5;
308
3/4
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
48 if (low == p->var[0] || high == p->var[0])
309 break;
310 46 v = eval_expr(p, e->param[0]);
311
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 25 times.
46 if (v<=0) low = p->var[0];
312
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 20 times.
46 if (v>=0) high= p->var[0];
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (isnan(v)) {
314 low = high = v;
315 break;
316 }
317 }
318 2 break;
319 }
320 }
321 2 p->var[0] = var0;
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 return -low_v<high_v ? low : high;
323 }
324 82386225 default: {
325 82386225 double d = eval_expr(p, e->param[0]);
326 82386225 double d2 = eval_expr(p, e->param[1]);
327
18/20
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 234 times.
✓ Branch 5 taken 1333 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 216 times.
✓ Branch 9 taken 3358 times.
✓ Branch 10 taken 67680151 times.
✓ Branch 11 taken 3611056 times.
✓ Branch 12 taken 11088961 times.
✓ Branch 13 taken 243 times.
✓ Branch 14 taken 464 times.
✓ Branch 15 taken 1 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 2 times.
✓ Branch 18 taken 1 times.
✗ Branch 19 not taken.
82386225 switch (e->type) {
328
1/2
✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
198 case e_mod: return e->value * (d - floor(d2 ? d / d2 : d * INFINITY) * d2);
329 1 case e_gcd: return e->value * av_gcd(d,d2);
330
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 case e_max: return e->value * (d > d2 ? d : d2);
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 case e_min: return e->value * (d < d2 ? d : d2);
332
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 177 times.
234 case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
333
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1321 times.
1333 case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
334
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
335
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 case e_lt: return e->value * (d < d2 ? 1.0 : 0.0);
336
2/2
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 4 times.
216 case e_lte: return e->value * (d <= d2 ? 1.0 : 0.0);
337 3358 case e_pow: return e->value * pow(d, d2);
338 67680151 case e_mul: return e->value * (d * d2);
339
2/2
✓ Branch 0 taken 3611050 times.
✓ Branch 1 taken 6 times.
3611056 case e_div: return e->value * (d2 ? (d / d2) : d * INFINITY);
340 11088961 case e_add: return e->value * (d + d2);
341 243 case e_last:return e->value * d2;
342 464 case e_st : {
343 464 int index = av_clip(d, 0, VARS-1);
344 464 p->prng_state[index].counter = 0;
345 464 return e->value * (p->var[index]= d2);
346 }
347 1 case e_hypot:return e->value * hypot(d, d2);
348 case e_atan2:return e->value * atan2(d, d2);
349
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 case e_bitand: return isnan(d) || isnan(d2) ? NAN : e->value * ((long int)d & (long int)d2);
350
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 case e_bitor: return isnan(d) || isnan(d2) ? NAN : e->value * ((long int)d | (long int)d2);
351 }
352 }
353 }
354 return NAN;
355 }
356
357 static int parse_expr(AVExpr **e, Parser *p);
358
359 510502 void av_expr_free(AVExpr *e)
360 {
361
2/2
✓ Branch 0 taken 414294 times.
✓ Branch 1 taken 96208 times.
510502 if (!e) return;
362 96208 av_expr_free(e->param[0]);
363 96208 av_expr_free(e->param[1]);
364 96208 av_expr_free(e->param[2]);
365 96208 av_freep(&e->var);
366 96208 av_freep(&e->prng_state);
367 96208 av_freep(&e);
368 }
369
370 90984 static int parse_primary(AVExpr **e, Parser *p)
371 {
372 90984 AVExpr *d = av_mallocz(sizeof(AVExpr));
373 90984 char *next = p->s, *s0 = p->s;
374 int ret, i;
375
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90984 times.
90984 if (!d)
377 return AVERROR(ENOMEM);
378
379 /* number */
380 90984 d->value = av_strtod(p->s, &next);
381
2/2
✓ Branch 0 taken 58594 times.
✓ Branch 1 taken 32390 times.
90984 if (next != p->s) {
382 58594 d->type = e_value;
383 58594 p->s= next;
384 58594 *e = d;
385 58594 return 0;
386 }
387 32390 d->value = 1;
388
389 /* named constants */
390
4/4
✓ Branch 0 taken 367106 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 362724 times.
✓ Branch 3 taken 4382 times.
367121 for (i=0; p->const_names && p->const_names[i]; i++) {
391
2/2
✓ Branch 1 taken 27993 times.
✓ Branch 2 taken 334731 times.
362724 if (strmatch(p->s, p->const_names[i])) {
392 27993 p->s+= strlen(p->const_names[i]);
393 27993 d->type = e_const;
394 27993 d->const_index = i;
395 27993 *e = d;
396 27993 return 0;
397 }
398 }
399
2/2
✓ Branch 0 taken 17466 times.
✓ Branch 1 taken 4336 times.
21802 for (i = 0; i < FF_ARRAY_ELEMS(constants); i++) {
400
2/2
✓ Branch 1 taken 61 times.
✓ Branch 2 taken 17405 times.
17466 if (strmatch(p->s, constants[i].name)) {
401 61 p->s += strlen(constants[i].name);
402 61 d->type = e_value;
403 61 d->value = constants[i].value;
404 61 *e = d;
405 61 return 0;
406 }
407 }
408
409 4336 p->s= strchr(p->s, '(');
410
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 4310 times.
4336 if (!p->s) {
411 26 av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0);
412 26 p->s= next;
413 26 av_expr_free(d);
414 26 return AVERROR(EINVAL);
415 }
416 4310 p->s++; // "("
417
2/2
✓ Branch 0 taken 3990 times.
✓ Branch 1 taken 320 times.
4310 if (*next == '(') { // special case do-nothing
418 3990 av_freep(&d);
419
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3990 times.
3990 if ((ret = parse_expr(&d, p)) < 0)
420 return ret;
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3990 times.
3990 if (p->s[0] != ')') {
422 av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0);
423 av_expr_free(d);
424 return AVERROR(EINVAL);
425 }
426 3990 p->s++; // ")"
427 3990 *e = d;
428 3990 return 0;
429 }
430
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 316 times.
320 if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
431 4 av_expr_free(d);
432 4 return ret;
433 }
434
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 208 times.
316 if (p->s[0]== ',') {
435 108 p->s++; // ","
436 108 parse_expr(&d->param[1], p);
437 }
438
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 287 times.
316 if (p->s[0]== ',') {
439 29 p->s++; // ","
440 29 parse_expr(&d->param[2], p);
441 }
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (p->s[0] != ')') {
443 av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0);
444 av_expr_free(d);
445 return AVERROR(EINVAL);
446 }
447 316 p->s++; // ")"
448
449 316 d->type = e_func0;
450
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
316 if (strmatch(next, "sinh" )) d->a.func0 = sinh;
451
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
316 else if (strmatch(next, "cosh" )) d->a.func0 = cosh;
452
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
316 else if (strmatch(next, "tanh" )) d->a.func0 = tanh;
453
2/2
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 288 times.
316 else if (strmatch(next, "sin" )) d->a.func0 = sin;
454
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 277 times.
288 else if (strmatch(next, "cos" )) d->a.func0 = cos;
455
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 277 times.
277 else if (strmatch(next, "tan" )) d->a.func0 = tan;
456
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 277 times.
277 else if (strmatch(next, "atan" )) d->a.func0 = atan;
457
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 277 times.
277 else if (strmatch(next, "asin" )) d->a.func0 = asin;
458
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 277 times.
277 else if (strmatch(next, "acos" )) d->a.func0 = acos;
459
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 277 times.
277 else if (strmatch(next, "exp" )) d->a.func0 = exp;
460
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 276 times.
277 else if (strmatch(next, "log" )) d->a.func0 = log;
461
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 276 times.
276 else if (strmatch(next, "abs" )) d->a.func0 = fabs;
462
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 276 times.
276 else if (strmatch(next, "time" )) d->a.func0 = etime;
463
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 275 times.
276 else if (strmatch(next, "squish")) d->type = e_squish;
464
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 274 times.
275 else if (strmatch(next, "gauss" )) d->type = e_gauss;
465
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 266 times.
274 else if (strmatch(next, "mod" )) d->type = e_mod;
466
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 264 times.
266 else if (strmatch(next, "max" )) d->type = e_max;
467
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 263 times.
264 else if (strmatch(next, "min" )) d->type = e_min;
468
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 252 times.
263 else if (strmatch(next, "eq" )) d->type = e_eq;
469
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 251 times.
252 else if (strmatch(next, "gte" )) d->type = e_gte;
470
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 246 times.
251 else if (strmatch(next, "gt" )) d->type = e_gt;
471
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 240 times.
246 else if (strmatch(next, "lte" )) d->type = e_lte;
472
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 238 times.
240 else if (strmatch(next, "lt" )) d->type = e_lt;
473
2/2
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 213 times.
238 else if (strmatch(next, "ld" )) d->type = e_ld;
474
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 209 times.
213 else if (strmatch(next, "isnan" )) d->type = e_isnan;
475
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 206 times.
209 else if (strmatch(next, "isinf" )) d->type = e_isinf;
476
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 190 times.
206 else if (strmatch(next, "st" )) d->type = e_st;
477
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 186 times.
190 else if (strmatch(next, "while" )) d->type = e_while;
478
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 184 times.
186 else if (strmatch(next, "taylor")) d->type = e_taylor;
479
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 182 times.
184 else if (strmatch(next, "root" )) d->type = e_root;
480
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 178 times.
182 else if (strmatch(next, "floor" )) d->type = e_floor;
481
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 176 times.
178 else if (strmatch(next, "ceil" )) d->type = e_ceil;
482
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 174 times.
176 else if (strmatch(next, "trunc" )) d->type = e_trunc;
483
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 174 times.
174 else if (strmatch(next, "round" )) d->type = e_round;
484
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 172 times.
174 else if (strmatch(next, "sqrt" )) d->type = e_sqrt;
485
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 159 times.
172 else if (strmatch(next, "not" )) d->type = e_not;
486
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 156 times.
159 else if (strmatch(next, "pow" )) d->type = e_pow;
487
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 155 times.
156 else if (strmatch(next, "print" )) d->type = e_print;
488
2/2
✓ Branch 1 taken 107 times.
✓ Branch 2 taken 48 times.
155 else if (strmatch(next, "random")) d->type = e_random;
489
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
48 else if (strmatch(next, "randomi")) d->type = e_randomi;
490
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 47 times.
48 else if (strmatch(next, "hypot" )) d->type = e_hypot;
491
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 46 times.
47 else if (strmatch(next, "gcd" )) d->type = e_gcd;
492
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 33 times.
46 else if (strmatch(next, "if" )) d->type = e_if;
493
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 29 times.
33 else if (strmatch(next, "ifnot" )) d->type = e_ifnot;
494
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 27 times.
29 else if (strmatch(next, "bitand")) d->type = e_bitand;
495
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 26 times.
27 else if (strmatch(next, "bitor" )) d->type = e_bitor;
496
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 14 times.
26 else if (strmatch(next, "between"))d->type = e_between;
497
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 11 times.
14 else if (strmatch(next, "clip" )) d->type = e_clip;
498
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
11 else if (strmatch(next, "atan2" )) d->type = e_atan2;
499
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
11 else if (strmatch(next, "lerp" )) d->type = e_lerp;
500
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
11 else if (strmatch(next, "sgn" )) d->type = e_sgn;
501 else {
502
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
11 for (i=0; p->func1_names && p->func1_names[i]; i++) {
503
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (strmatch(next, p->func1_names[i])) {
504 4 d->a.func1 = p->funcs1[i];
505 4 d->type = e_func1;
506 4 d->const_index = i;
507 4 *e = d;
508 4 return 0;
509 }
510 }
511
512
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
7 for (i=0; p->func2_names && p->func2_names[i]; i++) {
513
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 if (strmatch(next, p->func2_names[i])) {
514 6 d->a.func2 = p->funcs2[i];
515 6 d->type = e_func2;
516 6 d->const_index = i;
517 6 *e = d;
518 6 return 0;
519 }
520 }
521
522 1 av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0);
523 1 av_expr_free(d);
524 1 return AVERROR(EINVAL);
525 }
526
527 305 *e = d;
528 305 return 0;
529 }
530
531 9214 static AVExpr *make_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
532 {
533 9214 AVExpr *e = av_mallocz(sizeof(AVExpr));
534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9214 times.
9214 if (!e)
535 return NULL;
536 9214 e->type =type ;
537 9214 e->value =value ;
538 9214 e->param[0] =p0 ;
539 9214 e->param[1] =p1 ;
540 9214 return e;
541 }
542
543 90981 static int parse_pow(AVExpr **e, Parser *p, int *sign)
544 {
545 90981 *sign= (*p->s == '+') - (*p->s == '-');
546 90981 p->s += *sign&1;
547 90981 return parse_primary(e, p);
548 }
549
550 90984 static int parse_dB(AVExpr **e, Parser *p, int *sign)
551 {
552 /* do not filter out the negative sign when parsing a dB value.
553 for example, -3dB is not the same as -(3dB) */
554
2/2
✓ Branch 0 taken 382 times.
✓ Branch 1 taken 90602 times.
90984 if (*p->s == '-') {
555 char *next;
556 382 double av_unused ignored = strtod(p->s, &next);
557
5/6
✓ Branch 0 taken 365 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 362 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
382 if (next != p->s && next[0] == 'd' && next[1] == 'B') {
558 3 *sign = 0;
559 3 return parse_primary(e, p);
560 }
561 }
562 90981 return parse_pow(e, p, sign);
563 }
564
565 90781 static int parse_factor(AVExpr **e, Parser *p)
566 {
567 int sign, sign2, ret;
568 AVExpr *e0, *e1, *e2;
569
2/2
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 90750 times.
90781 if ((ret = parse_dB(&e0, p, &sign)) < 0)
570 31 return ret;
571
2/2
✓ Branch 0 taken 203 times.
✓ Branch 1 taken 90750 times.
181703 while(p->s[0]=='^'){
572 203 e1 = e0;
573 203 p->s++;
574
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 203 times.
203 if ((ret = parse_dB(&e2, p, &sign2)) < 0) {
575 av_expr_free(e1);
576 return ret;
577 }
578 203 e0 = make_eval_expr(e_pow, 1, e1, e2);
579
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 203 times.
203 if (!e0) {
580 av_expr_free(e1);
581 av_expr_free(e2);
582 return AVERROR(ENOMEM);
583 }
584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 203 times.
203 if (e0->param[1]) e0->param[1]->value *= (sign2|1);
585 }
586
1/2
✓ Branch 0 taken 90750 times.
✗ Branch 1 not taken.
90750 if (e0) e0->value *= (sign|1);
587
588 90750 *e = e0;
589 90750 return 0;
590 }
591
592 86066 static int parse_term(AVExpr **e, Parser *p)
593 {
594 int ret;
595 AVExpr *e0, *e1, *e2;
596
2/2
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 86037 times.
86066 if ((ret = parse_factor(&e0, p)) < 0)
597 29 return ret;
598
4/4
✓ Branch 0 taken 4374 times.
✓ Branch 1 taken 86376 times.
✓ Branch 2 taken 341 times.
✓ Branch 3 taken 86035 times.
90750 while (p->s[0]=='*' || p->s[0]=='/') {
599 4715 int c= *p->s++;
600 4715 e1 = e0;
601
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4713 times.
4715 if ((ret = parse_factor(&e2, p)) < 0) {
602 2 av_expr_free(e1);
603 2 return ret;
604 }
605
2/2
✓ Branch 0 taken 4374 times.
✓ Branch 1 taken 339 times.
4713 e0 = make_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
606
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4713 times.
4713 if (!e0) {
607 av_expr_free(e1);
608 av_expr_free(e2);
609 return AVERROR(ENOMEM);
610 }
611 }
612 86035 *e = e0;
613 86035 return 0;
614 }
615
616 81785 static int parse_subexpr(AVExpr **e, Parser *p)
617 {
618 int ret;
619 AVExpr *e0, *e1, *e2;
620
2/2
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 81755 times.
81785 if ((ret = parse_term(&e0, p)) < 0)
621 30 return ret;
622
4/4
✓ Branch 0 taken 4125 times.
✓ Branch 1 taken 81910 times.
✓ Branch 2 taken 156 times.
✓ Branch 3 taken 81754 times.
86035 while (*p->s == '+' || *p->s == '-') {
623 4281 e1 = e0;
624
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4280 times.
4281 if ((ret = parse_term(&e2, p)) < 0) {
625 1 av_expr_free(e1);
626 1 return ret;
627 }
628 4280 e0 = make_eval_expr(e_add, 1, e1, e2);
629
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4280 times.
4280 if (!e0) {
630 av_expr_free(e1);
631 av_expr_free(e2);
632 return AVERROR(ENOMEM);
633 }
634 };
635
636 81754 *e = e0;
637 81754 return 0;
638 }
639
640 81767 static int parse_expr(AVExpr **e, Parser *p)
641 {
642 int ret;
643 AVExpr *e0, *e1, *e2;
644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81767 times.
81767 if (p->stack_index <= 0) //protect against stack overflows
645 return AVERROR(EINVAL);
646 81767 p->stack_index--;
647
648
2/2
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 81736 times.
81767 if ((ret = parse_subexpr(&e0, p)) < 0)
649 31 return ret;
650
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 81736 times.
81754 while (*p->s == ';') {
651 18 p->s++;
652 18 e1 = e0;
653
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if ((ret = parse_subexpr(&e2, p)) < 0) {
654 av_expr_free(e1);
655 return ret;
656 }
657 18 e0 = make_eval_expr(e_last, 1, e1, e2);
658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!e0) {
659 av_expr_free(e1);
660 av_expr_free(e2);
661 return AVERROR(ENOMEM);
662 }
663 };
664
665 81736 p->stack_index++;
666 81736 *e = e0;
667 81736 return 0;
668 }
669
670 96162 static int verify_expr(AVExpr *e)
671 {
672
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 96161 times.
96162 if (!e) return 0;
673
6/6
✓ Branch 0 taken 86634 times.
✓ Branch 1 taken 208 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 15 times.
✓ Branch 5 taken 9284 times.
96161 switch (e->type) {
674 86634 case e_value:
675 86634 case e_const: return 1;
676 208 case e_func0:
677 case e_func1:
678 case e_squish:
679 case e_ld:
680 case e_gauss:
681 case e_isnan:
682 case e_isinf:
683 case e_floor:
684 case e_ceil:
685 case e_trunc:
686 case e_round:
687 case e_sqrt:
688 case e_not:
689 case e_random:
690 case e_sgn:
691
3/4
✓ Branch 1 taken 208 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 207 times.
✓ Branch 4 taken 1 times.
208 return verify_expr(e->param[0]) && !e->param[1];
692 1 case e_print:
693 1 return verify_expr(e->param[0])
694
2/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
1 && (!e->param[1] || verify_expr(e->param[1]));
695 19 case e_if:
696 case e_ifnot:
697 case e_taylor:
698
1/2
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
38 return verify_expr(e->param[0]) && verify_expr(e->param[1])
699
4/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 5 times.
✓ Branch 5 taken 14 times.
✗ Branch 6 not taken.
38 && (!e->param[2] || verify_expr(e->param[2]));
700 15 case e_between:
701 case e_clip:
702 case e_lerp:
703 case e_randomi:
704
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
30 return verify_expr(e->param[0]) &&
705
3/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 1 times.
30 verify_expr(e->param[1]) &&
706 15 verify_expr(e->param[2]);
707
3/6
✓ Branch 1 taken 9284 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9284 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9284 times.
✗ Branch 7 not taken.
9284 default: return verify_expr(e->param[0]) && verify_expr(e->param[1]) && !e->param[2];
708 }
709 }
710
711 77320 int av_expr_parse(AVExpr **expr, const char *s,
712 const char * const *const_names,
713 const char * const *func1_names, double (* const *funcs1)(void *, double),
714 const char * const *func2_names, double (* const *funcs2)(void *, double, double),
715 int log_offset, void *log_ctx)
716 {
717 77320 Parser p = { 0 };
718 77320 AVExpr *e = NULL;
719 77320 char *w = av_malloc(strlen(s) + 1);
720 77320 char *wp = w;
721 77320 const char *s0 = s;
722 77320 int ret = 0;
723
724
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77320 times.
77320 if (!w)
725 return AVERROR(ENOMEM);
726
727
2/2
✓ Branch 0 taken 167375 times.
✓ Branch 1 taken 77320 times.
244695 while (*s)
728
2/2
✓ Branch 0 taken 167230 times.
✓ Branch 1 taken 145 times.
167375 if (!av_isspace(*s++)) *wp++ = s[-1];
729 77320 *wp++ = 0;
730
731 77320 p.class = &eval_class;
732 77320 p.stack_index=100;
733 77320 p.s= w;
734 77320 p.const_names = const_names;
735 77320 p.funcs1 = funcs1;
736 77320 p.func1_names = func1_names;
737 77320 p.funcs2 = funcs2;
738 77320 p.func2_names = func2_names;
739 77320 p.log_offset = log_offset;
740 77320 p.log_ctx = log_ctx;
741
742
2/2
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 77293 times.
77320 if ((ret = parse_expr(&e, &p)) < 0)
743 27 goto end;
744
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 77288 times.
77293 if (*p.s) {
745 5 av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
746 5 ret = AVERROR(EINVAL);
747 5 goto end;
748 }
749
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 77286 times.
77288 if (!verify_expr(e)) {
750 2 ret = AVERROR(EINVAL);
751 2 goto end;
752 }
753 77286 e->var= av_mallocz(sizeof(double) *VARS);
754 77286 e->prng_state = av_mallocz(sizeof(*e->prng_state) *VARS);
755
2/4
✓ Branch 0 taken 77286 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 77286 times.
77286 if (!e->var || !e->prng_state) {
756 ret = AVERROR(ENOMEM);
757 goto end;
758 }
759 77286 *expr = e;
760 77286 e = NULL;
761 77320 end:
762 77320 av_expr_free(e);
763 77320 av_free(w);
764 77320 return ret;
765 }
766
767 20676 static int expr_count(AVExpr *e, unsigned *counter, int size, int type)
768 {
769 int i;
770
771
3/6
✓ Branch 0 taken 20676 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20676 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 20676 times.
20676 if (!e || !counter || !size)
772 return AVERROR(EINVAL);
773
774
5/6
✓ Branch 0 taken 2290 times.
✓ Branch 1 taken 18398 times.
✓ Branch 2 taken 2290 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 2278 times.
20688 for (i = 0; e->type != type && i < 3 && e->param[i]; i++)
775 12 expr_count(e->param[i], counter, size, type);
776
777
3/4
✓ Branch 0 taken 18398 times.
✓ Branch 1 taken 2278 times.
✓ Branch 2 taken 18398 times.
✗ Branch 3 not taken.
20676 if (e->type == type && e->const_index < size)
778 18398 counter[e->const_index]++;
779
780 20676 return 0;
781 }
782
783 20664 int av_expr_count_vars(AVExpr *e, unsigned *counter, int size)
784 {
785 20664 return expr_count(e, counter, size, e_const);
786 }
787
788 int av_expr_count_func(AVExpr *e, unsigned *counter, int size, int arg)
789 {
790 return expr_count(e, counter, size, ((int[]){e_const, e_func1, e_func2})[arg]);
791 }
792
793 20910001 double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
794 {
795 20910001 Parser p = { 0 };
796 20910001 p.var= e->var;
797 20910001 p.prng_state= e->prng_state;
798
799 20910001 p.const_values = const_values;
800 20910001 p.opaque = opaque;
801 20910001 return eval_expr(&p, e);
802 }
803
804 62607 int av_expr_parse_and_eval(double *d, const char *s,
805 const char * const *const_names, const double *const_values,
806 const char * const *func1_names, double (* const *funcs1)(void *, double),
807 const char * const *func2_names, double (* const *funcs2)(void *, double, double),
808 void *opaque, int log_offset, void *log_ctx)
809 {
810 62607 AVExpr *e = NULL;
811 62607 int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
812
813
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 62573 times.
62607 if (ret < 0) {
814 34 *d = NAN;
815 34 return ret;
816 }
817 62573 *d = av_expr_eval(e, const_values, opaque);
818 62573 av_expr_free(e);
819
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 62564 times.
62573 return isnan(*d) ? AVERROR(EINVAL) : 0;
820 }
821