FFmpeg  4.3.6
boxblur.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3  * Copyright (c) 2011 Stefano Sabatini
4  * Copyright (c) 2018 Danil Iashchenko
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "boxblur.h"
24 
25 static const char *const var_names[] = {
26  "w",
27  "h",
28  "cw",
29  "ch",
30  "hsub",
31  "vsub",
32  NULL
33 };
34 
35 enum var_name {
43 };
44 
45 
47  FilterParam *luma_param,
48  FilterParam *chroma_param,
49  FilterParam *alpha_param)
50 {
52  AVFilterContext *ctx = inlink->dst;
53  int w = inlink->w, h = inlink->h;
54  int cw, ch;
55  double var_values[VARS_NB], res;
56  char *expr;
57  int ret;
58 
59  if (!luma_param->radius_expr) {
60  av_log(ctx, AV_LOG_ERROR, "Luma radius expression is not set.\n");
61  return AVERROR(EINVAL);
62  }
63 
64  /* fill missing params */
65  if (!chroma_param->radius_expr) {
66  chroma_param->radius_expr = av_strdup(luma_param->radius_expr);
67  if (!chroma_param->radius_expr)
68  return AVERROR(ENOMEM);
69  }
70  if (chroma_param->power < 0)
71  chroma_param->power = luma_param->power;
72 
73  if (!alpha_param->radius_expr) {
74  alpha_param->radius_expr = av_strdup(luma_param->radius_expr);
75  if (!alpha_param->radius_expr)
76  return AVERROR(ENOMEM);
77  }
78  if (alpha_param->power < 0)
79  alpha_param->power = luma_param->power;
80 
81  var_values[VAR_W] = inlink->w;
82  var_values[VAR_H] = inlink->h;
83  var_values[VAR_CW] = cw = w>>(desc->log2_chroma_w);
84  var_values[VAR_CH] = ch = h>>(desc->log2_chroma_h);
85  var_values[VAR_HSUB] = 1<<(desc->log2_chroma_w);
86  var_values[VAR_VSUB] = 1<<(desc->log2_chroma_h);
87 
88 #define EVAL_RADIUS_EXPR(comp) \
89  expr = comp->radius_expr; \
90  ret = av_expr_parse_and_eval(&res, expr, var_names, var_values, \
91  NULL, NULL, NULL, NULL, NULL, 0, ctx); \
92  comp->radius = res; \
93  if (ret < 0) { \
94  av_log(ctx, AV_LOG_ERROR, \
95  "Error when evaluating " #comp " radius expression '%s'\n", expr); \
96  return ret; \
97  }
98 
99  EVAL_RADIUS_EXPR(luma_param);
100  EVAL_RADIUS_EXPR(chroma_param);
101  EVAL_RADIUS_EXPR(alpha_param);
102 
103  av_log(ctx, AV_LOG_VERBOSE,
104  "luma_radius:%d luma_power:%d "
105  "chroma_radius:%d chroma_power:%d "
106  "alpha_radius:%d alpha_power:%d "
107  "w:%d chroma_w:%d h:%d chroma_h:%d\n",
108  luma_param ->radius, luma_param ->power,
109  chroma_param->radius, chroma_param->power,
110  alpha_param ->radius, alpha_param ->power,
111  w, cw, h, ch);
112 
113 
114 #define CHECK_RADIUS_VAL(w_, h_, comp) \
115  if (comp->radius < 0 || \
116  2*comp->radius > FFMIN(w_, h_)) { \
117  av_log(ctx, AV_LOG_ERROR, \
118  "Invalid " #comp " radius value %d, must be >= 0 and <= %d\n", \
119  comp->radius, FFMIN(w_, h_)/2); \
120  return AVERROR(EINVAL); \
121  }
122  CHECK_RADIUS_VAL(w, h, luma_param);
123  CHECK_RADIUS_VAL(cw, ch, chroma_param);
124  CHECK_RADIUS_VAL(w, h, alpha_param);
125 
126  return 0;
127 }
#define NULL
Definition: coverity.c:32
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2549
int radius
Definition: boxblur.h:33
Definition: boxblur.c:39
const char * desc
Definition: nvenc.c:79
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
int ff_boxblur_eval_filter_params(AVFilterLink *inlink, FilterParam *luma_param, FilterParam *chroma_param, FilterParam *alpha_param)
Definition: boxblur.c:46
char * radius_expr
Definition: boxblur.h:35
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
#define AVERROR(e)
Definition: error.h:43
Definition: boxblur.c:36
int power
Definition: boxblur.h:34
#define EVAL_RADIUS_EXPR(comp)
var_name
Definition: aeval.c:46
Definition: boxblur.c:37
uint8_t w
Definition: llviddspenc.c:38
AVFormatContext * ctx
Definition: movenc.c:48
Definition: boxblur.c:38
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:253
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
static const char *const var_names[]
Definition: boxblur.c:25
#define CHECK_RADIUS_VAL(w_, h_, comp)
An instance of a filter.
Definition: avfilter.h:338