FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_sab.c
Date: 2022-12-05 03:11:11
Exec Total Coverage
Lines: 0 113 0.0%
Functions: 0 7 0.0%
Branches: 0 44 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 /**
22 * @file
23 * Shape Adaptive Blur filter, ported from MPlayer libmpcodecs/vf_sab.c
24 */
25
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "libswscale/swscale.h"
29
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33
34 typedef struct FilterParam {
35 float radius;
36 float pre_filter_radius;
37 float strength;
38 float quality;
39 struct SwsContext *pre_filter_context;
40 uint8_t *pre_filter_buf;
41 int pre_filter_linesize;
42 int dist_width;
43 int dist_linesize;
44 int *dist_coeff;
45 #define COLOR_DIFF_COEFF_SIZE 512
46 int color_diff_coeff[COLOR_DIFF_COEFF_SIZE];
47 } FilterParam;
48
49 typedef struct SabContext {
50 const AVClass *class;
51 FilterParam luma;
52 FilterParam chroma;
53 int hsub;
54 int vsub;
55 unsigned int sws_flags;
56 } SabContext;
57
58 static const enum AVPixelFormat pix_fmts[] = {
59 AV_PIX_FMT_YUV420P,
60 AV_PIX_FMT_YUV410P,
61 AV_PIX_FMT_YUV444P,
62 AV_PIX_FMT_YUV422P,
63 AV_PIX_FMT_YUV411P,
64 AV_PIX_FMT_NONE
65 };
66
67 #define RADIUS_MIN 0.1
68 #define RADIUS_MAX 4.0
69
70 #define PRE_FILTER_RADIUS_MIN 0.1
71 #define PRE_FILTER_RADIUS_MAX 2.0
72
73 #define STRENGTH_MIN 0.1
74 #define STRENGTH_MAX 100.0
75
76 #define OFFSET(x) offsetof(SabContext, x)
77 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
78
79 static const AVOption sab_options[] = {
80 { "luma_radius", "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
81 { "lr" , "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
82 { "luma_pre_filter_radius", "set luma pre-filter radius", OFFSET(luma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, PRE_FILTER_RADIUS_MIN, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
83 { "lpfr", "set luma pre-filter radius", OFFSET(luma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, PRE_FILTER_RADIUS_MIN, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
84 { "luma_strength", "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
85 { "ls", "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
86
87 { "chroma_radius", "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
88 { "cr", "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
89 { "chroma_pre_filter_radius", "set chroma pre-filter radius", OFFSET(chroma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=PRE_FILTER_RADIUS_MIN-1},
90 PRE_FILTER_RADIUS_MIN-1, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
91 { "cpfr", "set chroma pre-filter radius", OFFSET(chroma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=PRE_FILTER_RADIUS_MIN-1},
92 PRE_FILTER_RADIUS_MIN-1, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
93 { "chroma_strength", "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
94 { "cs", "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
95
96 { NULL }
97 };
98
99 AVFILTER_DEFINE_CLASS(sab);
100
101 static av_cold int init(AVFilterContext *ctx)
102 {
103 SabContext *s = ctx->priv;
104
105 /* make chroma default to luma values, if not explicitly set */
106 if (s->chroma.radius < RADIUS_MIN)
107 s->chroma.radius = s->luma.radius;
108 if (s->chroma.pre_filter_radius < PRE_FILTER_RADIUS_MIN)
109 s->chroma.pre_filter_radius = s->luma.pre_filter_radius;
110 if (s->chroma.strength < STRENGTH_MIN)
111 s->chroma.strength = s->luma.strength;
112
113 s->luma.quality = s->chroma.quality = 3.0;
114 s->sws_flags = SWS_POINT;
115
116 av_log(ctx, AV_LOG_VERBOSE,
117 "luma_radius:%f luma_pre_filter_radius::%f luma_strength:%f "
118 "chroma_radius:%f chroma_pre_filter_radius:%f chroma_strength:%f\n",
119 s->luma .radius, s->luma .pre_filter_radius, s->luma .strength,
120 s->chroma.radius, s->chroma.pre_filter_radius, s->chroma.strength);
121 return 0;
122 }
123
124 static void close_filter_param(FilterParam *f)
125 {
126 if (f->pre_filter_context) {
127 sws_freeContext(f->pre_filter_context);
128 f->pre_filter_context = NULL;
129 }
130 av_freep(&f->pre_filter_buf);
131 av_freep(&f->dist_coeff);
132 }
133
134 static av_cold void uninit(AVFilterContext *ctx)
135 {
136 SabContext *s = ctx->priv;
137
138 close_filter_param(&s->luma);
139 close_filter_param(&s->chroma);
140 }
141
142 static int open_filter_param(FilterParam *f, int width, int height, unsigned int sws_flags)
143 {
144 SwsVector *vec;
145 SwsFilter sws_f;
146 int i, x, y;
147 int linesize = FFALIGN(width, 8);
148
149 f->pre_filter_buf = av_malloc(linesize * height);
150 if (!f->pre_filter_buf)
151 return AVERROR(ENOMEM);
152
153 f->pre_filter_linesize = linesize;
154 vec = sws_getGaussianVec(f->pre_filter_radius, f->quality);
155 sws_f.lumH = sws_f.lumV = vec;
156 sws_f.chrH = sws_f.chrV = NULL;
157 f->pre_filter_context = sws_getContext(width, height, AV_PIX_FMT_GRAY8,
158 width, height, AV_PIX_FMT_GRAY8,
159 sws_flags, &sws_f, NULL, NULL);
160 sws_freeVec(vec);
161
162 vec = sws_getGaussianVec(f->strength, 5.0);
163 for (i = 0; i < COLOR_DIFF_COEFF_SIZE; i++) {
164 double d;
165 int index = i-COLOR_DIFF_COEFF_SIZE/2 + vec->length/2;
166
167 if (index < 0 || index >= vec->length) d = 0.0;
168 else d = vec->coeff[index];
169
170 f->color_diff_coeff[i] = (int)(d/vec->coeff[vec->length/2]*(1<<12) + 0.5);
171 }
172 sws_freeVec(vec);
173
174 vec = sws_getGaussianVec(f->radius, f->quality);
175 f->dist_width = vec->length;
176 f->dist_linesize = FFALIGN(vec->length, 8);
177 f->dist_coeff = av_malloc_array(f->dist_width, f->dist_linesize * sizeof(*f->dist_coeff));
178 if (!f->dist_coeff) {
179 sws_freeVec(vec);
180 return AVERROR(ENOMEM);
181 }
182
183 for (y = 0; y < vec->length; y++) {
184 for (x = 0; x < vec->length; x++) {
185 double d = vec->coeff[x] * vec->coeff[y];
186 f->dist_coeff[x + y*f->dist_linesize] = (int)(d*(1<<10) + 0.5);
187 }
188 }
189 sws_freeVec(vec);
190
191 return 0;
192 }
193
194 static int config_props(AVFilterLink *inlink)
195 {
196 SabContext *s = inlink->dst->priv;
197 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
198 int ret;
199
200 s->hsub = desc->log2_chroma_w;
201 s->vsub = desc->log2_chroma_h;
202
203 close_filter_param(&s->luma);
204 ret = open_filter_param(&s->luma, inlink->w, inlink->h, s->sws_flags);
205 if (ret < 0)
206 return ret;
207
208 close_filter_param(&s->chroma);
209 ret = open_filter_param(&s->chroma,
210 AV_CEIL_RSHIFT(inlink->w, s->hsub),
211 AV_CEIL_RSHIFT(inlink->h, s->vsub), s->sws_flags);
212 return ret;
213 }
214
215 #define NB_PLANES 4
216
217 static void blur(uint8_t *dst, const int dst_linesize,
218 const uint8_t *src, const int src_linesize,
219 const int w, const int h, FilterParam *fp)
220 {
221 int x, y;
222 FilterParam f = *fp;
223 const int radius = f.dist_width/2;
224
225 const uint8_t * const src2[NB_PLANES] = { src };
226 int src2_linesize[NB_PLANES] = { src_linesize };
227 uint8_t *dst2[NB_PLANES] = { f.pre_filter_buf };
228 int dst2_linesize[NB_PLANES] = { f.pre_filter_linesize };
229
230 sws_scale(f.pre_filter_context, src2, src2_linesize, 0, h, dst2, dst2_linesize);
231
232 #define UPDATE_FACTOR do { \
233 int factor; \
234 factor = f.color_diff_coeff[COLOR_DIFF_COEFF_SIZE/2 + pre_val - \
235 f.pre_filter_buf[ix + iy*f.pre_filter_linesize]] * f.dist_coeff[dx + dy*f.dist_linesize]; \
236 sum += src[ix + iy*src_linesize] * factor; \
237 div += factor; \
238 } while (0)
239
240 for (y = 0; y < h; y++) {
241 for (x = 0; x < w; x++) {
242 int sum = 0;
243 int div = 0;
244 int dy;
245 const int pre_val = f.pre_filter_buf[x + y*f.pre_filter_linesize];
246 if (x >= radius && x < w - radius) {
247 for (dy = 0; dy < radius*2 + 1; dy++) {
248 int dx;
249 int iy = y+dy - radius;
250 iy = avpriv_mirror(iy, h-1);
251
252 for (dx = 0; dx < radius*2 + 1; dx++) {
253 const int ix = x+dx - radius;
254 UPDATE_FACTOR;
255 }
256 }
257 } else {
258 for (dy = 0; dy < radius*2+1; dy++) {
259 int dx;
260 int iy = y+dy - radius;
261 iy = avpriv_mirror(iy, h-1);
262
263 for (dx = 0; dx < radius*2 + 1; dx++) {
264 int ix = x+dx - radius;
265 ix = avpriv_mirror(ix, w-1);
266 UPDATE_FACTOR;
267 }
268 }
269 }
270 dst[x + y*dst_linesize] = (sum + div/2) / div;
271 }
272 }
273 }
274
275 static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
276 {
277 SabContext *s = inlink->dst->priv;
278 AVFilterLink *outlink = inlink->dst->outputs[0];
279 AVFrame *outpic;
280
281 outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
282 if (!outpic) {
283 av_frame_free(&inpic);
284 return AVERROR(ENOMEM);
285 }
286 av_frame_copy_props(outpic, inpic);
287
288 blur(outpic->data[0], outpic->linesize[0], inpic->data[0], inpic->linesize[0],
289 inlink->w, inlink->h, &s->luma);
290 if (inpic->data[2]) {
291 int cw = AV_CEIL_RSHIFT(inlink->w, s->hsub);
292 int ch = AV_CEIL_RSHIFT(inlink->h, s->vsub);
293 blur(outpic->data[1], outpic->linesize[1], inpic->data[1], inpic->linesize[1], cw, ch, &s->chroma);
294 blur(outpic->data[2], outpic->linesize[2], inpic->data[2], inpic->linesize[2], cw, ch, &s->chroma);
295 }
296
297 av_frame_free(&inpic);
298 return ff_filter_frame(outlink, outpic);
299 }
300
301 static const AVFilterPad sab_inputs[] = {
302 {
303 .name = "default",
304 .type = AVMEDIA_TYPE_VIDEO,
305 .filter_frame = filter_frame,
306 .config_props = config_props,
307 },
308 };
309
310 static const AVFilterPad sab_outputs[] = {
311 {
312 .name = "default",
313 .type = AVMEDIA_TYPE_VIDEO,
314 },
315 };
316
317 const AVFilter ff_vf_sab = {
318 .name = "sab",
319 .description = NULL_IF_CONFIG_SMALL("Apply shape adaptive blur."),
320 .priv_size = sizeof(SabContext),
321 .init = init,
322 .uninit = uninit,
323 FILTER_INPUTS(sab_inputs),
324 FILTER_OUTPUTS(sab_outputs),
325 FILTER_PIXFMTS_ARRAY(pix_fmts),
326 .priv_class = &sab_class,
327 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
328 };
329