FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_avgblur.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 64 89 71.9%
Functions: 6 10 60.0%
Branches: 43 148 29.1%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2016 Paul B Mahol
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "libavutil/avassert.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31
32 typedef struct AverageBlurContext {
33 const AVClass *class;
34
35 int radius;
36 int radiusV;
37 int planes;
38
39 int depth;
40 int max;
41 int area;
42 int planewidth[4];
43 int planeheight[4];
44 void *buffer;
45 uint16_t lut[256 * 256 * 256];
46 int nb_planes;
47
48 int (*filter[2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
49 } AverageBlurContext;
50
51 #define OFFSET(x) offsetof(AverageBlurContext, x)
52 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
53
54 static const AVOption avgblur_options[] = {
55 { "sizeX", "set horizontal size", OFFSET(radius), AV_OPT_TYPE_INT, {.i64=1}, 1, 1024, FLAGS },
56 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
57 { "sizeY", "set vertical size", OFFSET(radiusV), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, FLAGS },
58 { NULL }
59 };
60
61 AVFILTER_DEFINE_CLASS(avgblur);
62
63 typedef struct ThreadData {
64 int height;
65 int width;
66 const void *ptr;
67 void *dptr;
68 int linesize, dlinesize;
69 } ThreadData;
70
71 #define LUT_DIV(sum, area) (lut[(sum)])
72 #define SLOW_DIV(sum, area) ((sum) / (area))
73
74 #define FILTER(name, type, btype, lutunused, areaunused, lutdiv) \
75 static int filter_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
76 { \
77 AverageBlurContext *s = ctx->priv; \
78 ThreadData *td = arg; \
79 areaunused const int area = s->area; \
80 lutunused const uint16_t *lut = s->lut; \
81 const int size_w = s->radius; \
82 const int size_h = s->radiusV; \
83 btype *col_sum = (btype *)s->buffer + size_w; \
84 const int dlinesize = td->dlinesize / sizeof(type); \
85 const int linesize = td->linesize / sizeof(type); \
86 const int height = td->height; \
87 const int width = td->width; \
88 const type *src = td->ptr; \
89 type *dst = td->dptr; \
90 btype sum = 0; \
91 \
92 for (int x = -size_w; x < 0; x++) { \
93 sum = src[0] * size_h; \
94 for (int y = 0; y <= size_h; y++) \
95 sum += src[y * linesize]; \
96 av_assert2(sum >= 0); \
97 col_sum[x] = sum; \
98 } \
99 \
100 for (int x = 0; x < width; x++) { \
101 sum = src[x] * size_h; \
102 for (int y = 0; y <= size_h; y++) \
103 sum += src[x + y * linesize]; \
104 av_assert2(sum >= 0); \
105 col_sum[x] = sum; \
106 } \
107 \
108 for (int x = width; x < width + size_w; x++) { \
109 sum = src[width - 1] * size_h; \
110 for (int y = 0; y <= size_h; y++) \
111 sum += src[width - 1 + y * linesize]; \
112 av_assert2(sum >= 0); \
113 col_sum[x] = sum; \
114 } \
115 \
116 sum = 0; \
117 for (int x = -size_w; x <= size_w; x++) \
118 sum += col_sum[x]; \
119 av_assert2(sum >= 0); \
120 dst[0] = lutdiv(sum, area); \
121 \
122 for (int x = 1; x < width; x++) { \
123 sum = sum - col_sum[x - size_w - 1] + col_sum[x + size_w]; \
124 av_assert2(sum >= 0); \
125 dst[x] = lutdiv(sum, area); \
126 } \
127 \
128 src = td->ptr; \
129 src += linesize; \
130 dst += dlinesize; \
131 \
132 for (int y = 1; y < height; y++) { \
133 const int syp = FFMIN(size_h, height - y - 1) * linesize; \
134 const int syn = FFMIN(y, size_h + 1) * linesize; \
135 \
136 sum = 0; \
137 \
138 for (int x = -size_w; x < 0; x++) \
139 col_sum[x] += src[0 + syp] - src[0 - syn]; \
140 \
141 for (int x = 0; x < width; x++) \
142 col_sum[x] += src[x + syp] - src[x - syn]; \
143 \
144 for (int x = width; x < width + size_w; x++) \
145 col_sum[x] += src[width - 1 + syp] - src[width - 1 - syn]; \
146 \
147 for (int x = -size_w; x <= size_w; x++) \
148 sum += col_sum[x]; \
149 av_assert2(sum >= 0); \
150 dst[0] = lutdiv(sum, area); \
151 \
152 for (int x = 1; x < width; x++) { \
153 sum = sum - col_sum[x - size_w - 1] + col_sum[x + size_w]; \
154 av_assert2(sum >= 0); \
155 dst[x] = lutdiv(sum, area); \
156 } \
157 \
158 src += linesize; \
159 dst += dlinesize; \
160 } \
161 \
162 return 0; \
163 }
164
165
30/30
✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 240 times.
✓ Branch 2 taken 240 times.
✓ Branch 3 taken 60 times.
✓ Branch 4 taken 75000 times.
✓ Branch 5 taken 15000 times.
✓ Branch 6 taken 15000 times.
✓ Branch 7 taken 60 times.
✓ Branch 8 taken 1200 times.
✓ Branch 9 taken 240 times.
✓ Branch 10 taken 240 times.
✓ Branch 11 taken 60 times.
✓ Branch 12 taken 540 times.
✓ Branch 13 taken 60 times.
✓ Branch 14 taken 14940 times.
✓ Branch 15 taken 60 times.
✓ Branch 16 taken 240 times.
✓ Branch 17 taken 11700 times.
✓ Branch 18 taken 47760 times.
✓ Branch 19 taken 11940 times.
✓ Branch 20 taken 2985000 times.
✓ Branch 21 taken 11940 times.
✓ Branch 22 taken 47760 times.
✓ Branch 23 taken 11940 times.
✓ Branch 24 taken 107460 times.
✓ Branch 25 taken 11940 times.
✓ Branch 26 taken 2973060 times.
✓ Branch 27 taken 11940 times.
✓ Branch 28 taken 11940 times.
✓ Branch 29 taken 60 times.
6281400 FILTER(lut8, uint8_t, int32_t, , av_unused, LUT_DIV)
166 FILTER(lut16, uint16_t, int64_t, , av_unused, LUT_DIV)
167
168 FILTER(slow8, uint8_t, int32_t, av_unused, , SLOW_DIV)
169 FILTER(slow16, uint16_t, int64_t, av_unused, , SLOW_DIV)
170
171 4 static void build_lut(AVFilterContext *ctx, int max)
172 {
173 4 AverageBlurContext *s = ctx->priv;
174 4 const int area = (2 * s->radiusV + 1) * (2 * s->radius + 1);
175
176 4 s->area = area;
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (max * area >= FF_ARRAY_ELEMS(s->lut))
178 return;
179
180
2/2
✓ Branch 0 taken 82944 times.
✓ Branch 1 taken 4 times.
82948 for (int i = 0, j = 0, k = 0; i < max * area; i++, j++) {
181
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 81924 times.
82944 if (j == area) {
182 1020 k++;
183 1020 j = 0;
184 }
185
186 82944 s->lut[i] = k;
187 }
188 }
189
190 12 static av_cold void uninit(AVFilterContext *ctx)
191 {
192 12 AverageBlurContext *s = ctx->priv;
193
194 12 av_freep(&s->buffer);
195 12 }
196
197 4 static int config_input(AVFilterLink *inlink)
198 {
199 4 AVFilterContext *ctx = inlink->dst;
200 4 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
201 4 AverageBlurContext *s = ctx->priv;
202
203 4 uninit(ctx);
204
205 4 s->depth = desc->comp[0].depth;
206 4 s->max = 1 << s->depth;
207 4 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
208 4 s->planewidth[0] = s->planewidth[3] = inlink->w;
209 4 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
210 4 s->planeheight[0] = s->planeheight[3] = inlink->h;
211
212 4 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
213
214 4 s->buffer = av_calloc(inlink->w + (1024 * 2 + 1), 4 * ((s->depth + 7) / 8));
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!s->buffer)
216 return AVERROR(ENOMEM);
217
218
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (s->radiusV <= 0)
219 4 s->radiusV = s->radius;
220
221
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 s->filter[0] = s->depth <= 8 ? filter_lut8 : filter_lut16;
222
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 s->filter[1] = s->depth <= 8 ? filter_slow8 : filter_slow16;
223
224 4 s->radius = FFMIN(s->planewidth[1] / 2, s->radius);
225 4 s->radiusV = FFMIN(s->planeheight[1] / 2, s->radiusV);
226
227 4 build_lut(ctx, s->max);
228
229 4 return 0;
230 }
231
232 60 static void averageiir2d(AVFilterContext *ctx, AVFrame *in, AVFrame *out, int plane)
233 {
234 60 AverageBlurContext *s = ctx->priv;
235 60 const int width = s->planewidth[plane];
236 60 const int height = s->planeheight[plane];
237 60 const int slow = (s->max * s->area) >= FF_ARRAY_ELEMS(s->lut);
238 ThreadData td;
239
240 60 td.width = width;
241 60 td.height = height;
242 60 td.ptr = in->data[plane];
243 60 td.linesize = in->linesize[plane];
244 60 td.dptr = out->data[plane];
245 60 td.dlinesize = out->linesize[plane];
246 60 s->filter[slow](ctx, &td, 0, 0);
247 60 }
248
249 static const enum AVPixelFormat pix_fmts[] = {
250 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
251 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
252 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
253 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
254 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
255 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
256 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
257 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
258 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
259 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
260 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
261 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
262 AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
263 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
264 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
265 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
266 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
267 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
268 AV_PIX_FMT_NONE
269 };
270
271 20 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
272 {
273 20 AVFilterContext *ctx = inlink->dst;
274 20 AverageBlurContext *s = ctx->priv;
275 20 AVFilterLink *outlink = ctx->outputs[0];
276 AVFrame *out;
277 int plane;
278
279 20 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!out) {
281 av_frame_free(&in);
282 return AVERROR(ENOMEM);
283 }
284 20 av_frame_copy_props(out, in);
285
286
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
80 for (plane = 0; plane < s->nb_planes; plane++) {
287 60 const int height = s->planeheight[plane];
288 60 const int width = s->planewidth[plane];
289
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!(s->planes & (1 << plane))) {
291 if (out != in)
292 av_image_copy_plane(out->data[plane], out->linesize[plane],
293 in->data[plane], in->linesize[plane],
294 width * ((s->depth + 7) / 8), height);
295 continue;
296 }
297
298 60 averageiir2d(ctx, in, out, plane);
299 }
300
301 20 av_frame_free(&in);
302 20 return ff_filter_frame(outlink, out);
303 }
304
305 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
306 char *res, int res_len, int flags)
307 {
308 AverageBlurContext *s = ctx->priv;
309 const int area = s->area;
310 int ret;
311
312 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
313 if (ret < 0)
314 return ret;
315
316 if (s->radiusV <= 0)
317 s->radiusV = s->radius;
318
319 s->radius = FFMIN(s->planewidth[1] / 2, s->radius);
320 s->radiusV = FFMIN(s->planeheight[1] / 2, s->radiusV);
321
322 if (area != (2 * s->radiusV + 1) * (2 * s->radius + 1))
323 build_lut(ctx, s->max);
324
325 return 0;
326 }
327
328 static const AVFilterPad avgblur_inputs[] = {
329 {
330 .name = "default",
331 .type = AVMEDIA_TYPE_VIDEO,
332 .config_props = config_input,
333 .filter_frame = filter_frame,
334 },
335 };
336
337 static const AVFilterPad avgblur_outputs[] = {
338 {
339 .name = "default",
340 .type = AVMEDIA_TYPE_VIDEO,
341 },
342 };
343
344 const AVFilter ff_vf_avgblur = {
345 .name = "avgblur",
346 .description = NULL_IF_CONFIG_SMALL("Apply Average Blur filter."),
347 .priv_size = sizeof(AverageBlurContext),
348 .priv_class = &avgblur_class,
349 .uninit = uninit,
350 FILTER_INPUTS(avgblur_inputs),
351 FILTER_OUTPUTS(avgblur_outputs),
352 FILTER_PIXFMTS_ARRAY(pix_fmts),
353 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
354 .process_command = process_command,
355 };
356