~ubuntu-branches/ubuntu/saucy/gst-libav1.0/saucy-proposed

« back to all changes in this revision

Viewing changes to gst-libs/ext/libav/libavutil/eval.c

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2013-07-30 09:00:15 UTC
  • mfrom: (1.1.16) (7.1.7 experimental)
  • Revision ID: package-import@ubuntu.com-20130730090015-sc1ou2yssu7q5w4e
Tags: 1.1.3-1
* New upstream development snapshot:
  + debian/control:
    - Build depend on GStreamer and gst-plugins-base >= 1.1.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 */
28
28
 
29
29
#include "avutil.h"
 
30
#include "common.h"
30
31
#include "eval.h"
31
32
#include "log.h"
 
33
#include "mathematics.h"
32
34
 
33
35
typedef struct Parser {
34
36
    const AVClass *class;
79
81
    d = strtod(numstr, &next);
80
82
    /* if parsing succeeded, check for and interpret postfixes */
81
83
    if (next!=numstr) {
82
 
        if (*next >= 'E' && *next <= 'z') {
 
84
        if (next[0] == 'd' && next[1] == 'B') {
 
85
            /* treat dB as decibels instead of decibytes */
 
86
            d = pow(10, d / 20);
 
87
            next += 2;
 
88
        } else if (*next >= 'E' && *next <= 'z') {
83
89
            int e= si_prefixes[*next - 'E'];
84
90
            if (e) {
85
91
                if (next[1] == 'i') {
119
125
struct AVExpr {
120
126
    enum {
121
127
        e_value, e_const, e_func0, e_func1, e_func2,
122
 
        e_squish, e_gauss, e_ld, e_isnan,
 
128
        e_squish, e_gauss, e_ld, e_isnan, e_isinf,
123
129
        e_mod, e_max, e_min, e_eq, e_gt, e_gte,
124
130
        e_pow, e_mul, e_div, e_add,
125
131
        e_last, e_st, e_while, e_floor, e_ceil, e_trunc,
147
153
        case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
148
154
        case e_ld:     return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
149
155
        case e_isnan:  return e->value * !!isnan(eval_expr(p, e->param[0]));
 
156
        case e_isinf:  return e->value * !!isinf(eval_expr(p, e->param[0]));
150
157
        case e_floor:  return e->value * floor(eval_expr(p, e->param[0]));
151
158
        case e_ceil :  return e->value * ceil (eval_expr(p, e->param[0]));
152
159
        case e_trunc:  return e->value * trunc(eval_expr(p, e->param[0]));
281
288
    else if (strmatch(next, "lt"    )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
282
289
    else if (strmatch(next, "ld"    )) d->type = e_ld;
283
290
    else if (strmatch(next, "isnan" )) d->type = e_isnan;
 
291
    else if (strmatch(next, "isinf" )) d->type = e_isinf;
284
292
    else if (strmatch(next, "st"    )) d->type = e_st;
285
293
    else if (strmatch(next, "while" )) d->type = e_while;
286
294
    else if (strmatch(next, "floor" )) d->type = e_floor;
335
343
    return parse_primary(e, p);
336
344
}
337
345
 
 
346
static int parse_dB(AVExpr **e, Parser *p, int *sign)
 
347
{
 
348
    /* do not filter out the negative sign when parsing a dB value.
 
349
       for example, -3dB is not the same as -(3dB) */
 
350
    if (*p->s == '-') {
 
351
        char *next;
 
352
        strtod(p->s, &next);
 
353
        if (next != p->s && next[0] == 'd' && next[1] == 'B') {
 
354
            *sign = 0;
 
355
            return parse_primary(e, p);
 
356
        }
 
357
    }
 
358
    return parse_pow(e, p, sign);
 
359
}
 
360
 
338
361
static int parse_factor(AVExpr **e, Parser *p)
339
362
{
340
363
    int sign, sign2, ret;
341
364
    AVExpr *e0, *e1, *e2;
342
 
    if ((ret = parse_pow(&e0, p, &sign)) < 0)
 
365
    if ((ret = parse_dB(&e0, p, &sign)) < 0)
343
366
        return ret;
344
367
    while(p->s[0]=='^'){
345
368
        e1 = e0;
346
369
        p->s++;
347
 
        if ((ret = parse_pow(&e2, p, &sign2)) < 0) {
 
370
        if ((ret = parse_dB(&e2, p, &sign2)) < 0) {
348
371
            av_expr_free(e1);
349
372
            return ret;
350
373
        }
452
475
        case e_ld:
453
476
        case e_gauss:
454
477
        case e_isnan:
 
478
        case e_isinf:
455
479
        case e_floor:
456
480
        case e_ceil:
457
481
        case e_trunc:
540
564
}
541
565
 
542
566
#ifdef TEST
543
 
#undef printf
544
567
#include <string.h>
545
568
 
546
 
static double const_values[] = {
 
569
static const double const_values[] = {
547
570
    M_PI,
548
571
    M_E,
549
572
    0
550
573
};
551
574
 
552
 
static const char *const_names[] = {
 
575
static const char *const const_names[] = {
553
576
    "PI",
554
577
    "E",
555
578
    0
559
582
{
560
583
    int i;
561
584
    double d;
562
 
    const char **expr, *exprs[] = {
 
585
    const char *const *expr;
 
586
    static const char *const exprs[] = {
563
587
        "",
564
588
        "1;2",
565
589
        "-20",
592
616
        "1Gi",
593
617
        "st(0, 123)",
594
618
        "st(1, 123); ld(1)",
 
619
        "lte(0, 1)",
 
620
        "lte(1, 1)",
 
621
        "lte(1, 0)",
 
622
        "lt(0, 1)",
 
623
        "lt(1, 1)",
 
624
        "gt(1, 0)",
 
625
        "gt(2, 7)",
 
626
        "gte(122, 122)",
595
627
        /* compute 1+2+...+N */
596
628
        "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
597
629
        /* compute Fib(N) */
600
632
        "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
601
633
        "isnan(1)",
602
634
        "isnan(NAN)",
 
635
        "isnan(INF)",
 
636
        "isinf(1)",
 
637
        "isinf(NAN)",
 
638
        "isinf(INF)",
603
639
        "floor(NAN)",
604
640
        "floor(123.123)",
605
641
        "floor(-123.123)",
612
648
        "not(1)",
613
649
        "not(NAN)",
614
650
        "not(0)",
 
651
        "6.0206dB",
 
652
        "-3.0103dB",
615
653
        NULL
616
654
    };
617
655
 
620
658
        av_expr_parse_and_eval(&d, *expr,
621
659
                               const_names, const_values,
622
660
                               NULL, NULL, NULL, NULL, NULL, 0, NULL);
623
 
        printf("'%s' -> %f\n\n", *expr, d);
 
661
        if (isnan(d))
 
662
            printf("'%s' -> nan\n\n", *expr);
 
663
        else
 
664
            printf("'%s' -> %f\n\n", *expr, d);
624
665
    }
625
666
 
626
667
    av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",