FFmpeg
Loading...
Searching...
No Matches
vf_avgblur.c
Go to the documentation of this file.
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/mem.h"
26#include "libavutil/opt.h"
27#include "libavutil/pixdesc.h"
28#include "avfilter.h"
29#include "filters.h"
30#include "video.h"
31
32typedef struct AverageBlurContext {
33 const AVClass *class;
34
35 int radius;
37 int planes;
38
39 int depth;
40 int max;
41 int area;
42 int planewidth[4];
44 void *buffer;
45 uint16_t lut[256 * 256 * 256];
47
48 int (*filter[2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
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
54static 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
62
63typedef struct ThreadData {
64 int height;
65 int width;
66 const void *ptr;
67 void *dptr;
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) \
75static 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
165FILTER(lut8, uint8_t, int32_t, , av_unused, LUT_DIV)
166FILTER(lut16, uint16_t, int64_t, , av_unused, LUT_DIV)
167
168FILTER(slow8, uint8_t, int32_t, av_unused, , SLOW_DIV)
169FILTER(slow16, uint16_t, int64_t, av_unused, , SLOW_DIV)
170
172{
173 AverageBlurContext *s = ctx->priv;
174 const int area = (2 * s->radiusV + 1) * (2 * s->radius + 1);
175
176 s->area = area;
177 if (max * area >= FF_ARRAY_ELEMS(s->lut))
178 return;
179
180 for (int i = 0, j = 0, k = 0; i < max * area; i++, j++) {
181 if (j == area) {
182 k++;
183 j = 0;
184 }
185
186 s->lut[i] = k;
187 }
188}
189
191{
192 AverageBlurContext *s = ctx->priv;
193
194 av_freep(&s->buffer);
195}
196
197static int config_input(AVFilterLink *inlink)
198{
199 AVFilterContext *ctx = inlink->dst;
201 AverageBlurContext *s = ctx->priv;
202
203 uninit(ctx);
204
205 s->depth = desc->comp[0].depth;
206 s->max = 1 << s->depth;
207 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
208 s->planewidth[0] = s->planewidth[3] = inlink->w;
209 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
210 s->planeheight[0] = s->planeheight[3] = inlink->h;
211
212 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
213
214 s->buffer = av_calloc(inlink->w + (1024 * 2 + 1), 4 * ((s->depth + 7) / 8));
215 if (!s->buffer)
216 return AVERROR(ENOMEM);
217
218 if (s->radiusV <= 0)
219 s->radiusV = s->radius;
220
221 s->filter[0] = s->depth <= 8 ? filter_lut8 : filter_lut16;
222 s->filter[1] = s->depth <= 8 ? filter_slow8 : filter_slow16;
223
224 s->radius = FFMIN(s->planewidth[1] / 2, s->radius);
225 s->radiusV = FFMIN(s->planeheight[1] / 2, s->radiusV);
226
227 build_lut(ctx, s->max);
228
229 return 0;
230}
231
232static void averageiir2d(AVFilterContext *ctx, AVFrame *in, AVFrame *out, int plane)
233{
234 AverageBlurContext *s = ctx->priv;
235 const int width = s->planewidth[plane];
236 const int height = s->planeheight[plane];
237 const int slow = (s->max * s->area) >= FF_ARRAY_ELEMS(s->lut);
238 ThreadData td;
239
240 td.width = width;
241 td.height = height;
242 td.ptr = in->data[plane];
243 td.linesize = in->linesize[plane];
244 td.dptr = out->data[plane];
245 td.dlinesize = out->linesize[plane];
246 s->filter[slow](ctx, &td, 0, 0);
247}
248
270
271static int filter_frame(AVFilterLink *inlink, AVFrame *in)
272{
273 AVFilterContext *ctx = inlink->dst;
274 AverageBlurContext *s = ctx->priv;
275 AVFilterLink *outlink = ctx->outputs[0];
276 AVFrame *out;
277 int plane;
278
279 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
280 if (!out) {
281 av_frame_free(&in);
282 return AVERROR(ENOMEM);
283 }
285
286 for (plane = 0; plane < s->nb_planes; plane++) {
287 const int height = s->planeheight[plane];
288 const int width = s->planewidth[plane];
289
290 if (!(s->planes & (1 << plane))) {
291 if (out->data[plane] != in->data[plane])
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 averageiir2d(ctx, in, out, plane);
299 }
300
301 av_frame_free(&in);
302 return ff_filter_frame(outlink, out);
303}
304
305static 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
328static 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
338 .p.name = "avgblur",
339 .p.description = NULL_IF_CONFIG_SMALL("Apply Average Blur filter."),
340 .p.priv_class = &avgblur_class,
342 .priv_size = sizeof(AverageBlurContext),
343 .uninit = uninit,
347 .process_command = process_command,
348};
static int config_input(AVFilterLink *inlink)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
const FFFilter ff_vf_avgblur
Definition vf_avgblur.c:337
static FILE * out
static AVFormatContext * ctx
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
Main libavfilter public API header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define max(a, b)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition imgutils.c:374
for(k=2;k<=8;++k)
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_unused
Definition attributes.h:164
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
const char * desc
Definition libsvtav1.c:83
static const struct @257111027162314367033347246032313251342043035002 planes[]
#define FFMIN(a, b)
Definition macros.h:49
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_GBRAP12
Definition pixfmt.h:569
#define AV_PIX_FMT_YUV420P16
Definition pixfmt.h:556
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:544
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
#define AV_PIX_FMT_YUV440P12
Definition pixfmt.h:551
#define AV_PIX_FMT_GRAY9
Definition pixfmt.h:524
#define AV_PIX_FMT_GBRAP16
Definition pixfmt.h:571
#define AV_PIX_FMT_GBRP9
Definition pixfmt.h:563
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:543
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:598
#define AV_PIX_FMT_YUVA420P16
Definition pixfmt.h:601
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUVA420P10
Definition pixfmt.h:596
#define AV_PIX_FMT_YUVA422P9
Definition pixfmt.h:594
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:564
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:526
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:565
#define AV_PIX_FMT_YUV420P9
Definition pixfmt.h:542
#define AV_PIX_FMT_YUVA420P9
Definition pixfmt.h:593
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:597
#define AV_PIX_FMT_YUV420P14
Definition pixfmt.h:553
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition pixfmt.h:107
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition pixfmt.h:283
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition pixfmt.h:86
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition pixfmt.h:87
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition pixfmt.h:85
#define AV_PIX_FMT_YUVA422P12
Definition pixfmt.h:599
#define AV_PIX_FMT_YUV422P14
Definition pixfmt.h:554
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:525
#define AV_PIX_FMT_GRAY14
Definition pixfmt.h:527
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:557
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:528
#define AV_PIX_FMT_GBRAP10
Definition pixfmt.h:568
#define AV_PIX_FMT_YUVA444P16
Definition pixfmt.h:603
#define AV_PIX_FMT_YUVA422P16
Definition pixfmt.h:602
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:555
#define AV_PIX_FMT_YUVA444P9
Definition pixfmt.h:595
#define AV_PIX_FMT_GBRP14
Definition pixfmt.h:566
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:600
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:558
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
int(* filter[2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_avgblur.c:48
uint16_t lut[256 *256 *256]
Definition vf_avgblur.c:45
Used for passing data between threads.
Definition dsddec.c:71
void * dptr
Definition vf_avgblur.c:67
int dlinesize
Definition vf_avgblur.c:68
const void * ptr
Definition vf_avgblur.c:66
#define av_freep(p)
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
#define FILTER(name, type, btype, lutunused, areaunused, lutdiv)
Definition vf_avgblur.c:74
#define LUT_DIV(sum, area)
Definition vf_avgblur.c:71
#define SLOW_DIV(sum, area)
Definition vf_avgblur.c:72
static int config_input(AVFilterLink *inlink)
Definition vf_avgblur.c:197
static const AVOption avgblur_options[]
Definition vf_avgblur.c:54
static const AVFilterPad avgblur_inputs[]
Definition vf_avgblur.c:328
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition vf_avgblur.c:271
static void averageiir2d(AVFilterContext *ctx, AVFrame *in, AVFrame *out, int plane)
Definition vf_avgblur.c:232
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition vf_avgblur.c:305
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_avgblur.c:190
static void build_lut(AVFilterContext *ctx, int max)
Definition vf_avgblur.c:171
#define OFFSET(x)
Definition vf_avgblur.c:51
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89