aboutsummaryrefslogtreecommitdiffstats
path: root/libavfilter/boxblur.c
blob: 43c724daeae1a1fd93641942c6e7dc45e21113bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
 * Copyright (c) 2011 Stefano Sabatini
 * Copyright (c) 2018 Danil Iashchenko
 *
 * 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 "libavutil/mem.h"
#include "boxblur.h"

static const char *const var_names[] = {
    "w",
    "h",
    "cw",
    "ch",
    "hsub",
    "vsub",
    NULL
};

enum var_name {
    VAR_W,
    VAR_H,
    VAR_CW,
    VAR_CH,
    VAR_HSUB,
    VAR_VSUB,
    VARS_NB
};


int ff_boxblur_eval_filter_params(AVFilterLink *inlink,
                                  FilterParam *luma_param,
                                  FilterParam *chroma_param,
                                  FilterParam *alpha_param)
{
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
    AVFilterContext *ctx = inlink->dst;
    int w = inlink->w, h = inlink->h;
    int cw, ch;
    double var_values[VARS_NB], res;
    char *expr;
    int ret;

    if (!luma_param->radius_expr) {
        av_log(ctx, AV_LOG_ERROR, "Luma radius expression is not set.\n");
        return AVERROR(EINVAL);
    }

    /* fill missing params */
    if (!chroma_param->radius_expr) {
        chroma_param->radius_expr = av_strdup(luma_param->radius_expr);
        if (!chroma_param->radius_expr)
            return AVERROR(ENOMEM);
    }
    if (chroma_param->power < 0)
        chroma_param->power = luma_param->power;

    if (!alpha_param->radius_expr) {
        alpha_param->radius_expr = av_strdup(luma_param->radius_expr);
        if (!alpha_param->radius_expr)
            return AVERROR(ENOMEM);
    }
    if (alpha_param->power < 0)
        alpha_param->power = luma_param->power;

    var_values[VAR_W]       = inlink->w;
    var_values[VAR_H]       = inlink->h;
    var_values[VAR_CW] = cw = w>>(desc->log2_chroma_w);
    var_values[VAR_CH] = ch = h>>(desc->log2_chroma_h);
    var_values[VAR_HSUB]    = 1<<(desc->log2_chroma_w);
    var_values[VAR_VSUB]    = 1<<(desc->log2_chroma_h);

#define EVAL_RADIUS_EXPR(comp)                                          \
    expr = comp->radius_expr;                                           \
    ret = av_expr_parse_and_eval(&res, expr, var_names, var_values,     \
                                 NULL, NULL, NULL, NULL, NULL, 0, ctx); \
    comp->radius = res;                                                 \
    if (ret < 0) {                                                      \
        av_log(ctx, AV_LOG_ERROR,                                      \
               "Error when evaluating " #comp " radius expression '%s'\n", expr); \
        return ret;                                                     \
    }

    EVAL_RADIUS_EXPR(luma_param);
    EVAL_RADIUS_EXPR(chroma_param);
    EVAL_RADIUS_EXPR(alpha_param);

    av_log(ctx, AV_LOG_VERBOSE,
           "luma_radius:%d luma_power:%d "
           "chroma_radius:%d chroma_power:%d "
           "alpha_radius:%d alpha_power:%d "
           "w:%d chroma_w:%d h:%d chroma_h:%d\n",
           luma_param  ->radius, luma_param  ->power,
           chroma_param->radius, chroma_param->power,
           alpha_param ->radius, alpha_param ->power,
           w, cw, h, ch);


#define CHECK_RADIUS_VAL(w_, h_, comp)                                  \
    if (comp->radius < 0 ||                                   \
        2*comp->radius > FFMIN(w_, h_)) {                     \
        av_log(ctx, AV_LOG_ERROR,                                       \
               "Invalid " #comp " radius value %d, must be >= 0 and <= %d\n", \
               comp->radius, FFMIN(w_, h_)/2);                \
        return AVERROR(EINVAL);                                         \
    }
    CHECK_RADIUS_VAL(w,  h,  luma_param);
    CHECK_RADIUS_VAL(cw, ch, chroma_param);
    CHECK_RADIUS_VAL(w,  h,  alpha_param);

    return 0;
}