| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at> | ||
| 3 | * Copyright (c) 2011 Stefano Sabatini | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU General Public License as published by | ||
| 9 | * the Free Software Foundation; either version 2 of the License, or | ||
| 10 | * (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | * GNU General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License along | ||
| 18 | * with FFmpeg; if not, write to the Free Software Foundation, Inc., | ||
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | */ | ||
| 21 | |||
| 22 | /** | ||
| 23 | * @file | ||
| 24 | * Apply a boxblur filter to the input video. | ||
| 25 | * Ported from MPlayer libmpcodecs/vf_boxblur.c. | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include "libavutil/common.h" | ||
| 29 | #include "libavutil/mem.h" | ||
| 30 | #include "libavutil/opt.h" | ||
| 31 | #include "avfilter.h" | ||
| 32 | #include "filters.h" | ||
| 33 | #include "formats.h" | ||
| 34 | #include "video.h" | ||
| 35 | #include "boxblur.h" | ||
| 36 | |||
| 37 | |||
| 38 | typedef struct BoxBlurContext { | ||
| 39 | const AVClass *class; | ||
| 40 | FilterParam luma_param; | ||
| 41 | FilterParam chroma_param; | ||
| 42 | FilterParam alpha_param; | ||
| 43 | |||
| 44 | int hsub, vsub; | ||
| 45 | int radius[4]; | ||
| 46 | int power[4]; | ||
| 47 | uint8_t *temp[2]; ///< temporary buffer used in blur_power() | ||
| 48 | } BoxBlurContext; | ||
| 49 | |||
| 50 | 2 | static av_cold void uninit(AVFilterContext *ctx) | |
| 51 | { | ||
| 52 | 2 | BoxBlurContext *s = ctx->priv; | |
| 53 | |||
| 54 | 2 | av_freep(&s->temp[0]); | |
| 55 | 2 | av_freep(&s->temp[1]); | |
| 56 | 2 | } | |
| 57 | |||
| 58 | 1 | static int query_formats(const AVFilterContext *ctx, | |
| 59 | AVFilterFormatsConfig **cfg_in, | ||
| 60 | AVFilterFormatsConfig **cfg_out) | ||
| 61 | { | ||
| 62 | 1 | AVFilterFormats *formats = NULL; | |
| 63 | int fmt, ret; | ||
| 64 | |||
| 65 |
2/2✓ Branch 1 taken 267 times.
✓ Branch 2 taken 1 times.
|
268 | for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) { |
| 66 | 267 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt); | |
| 67 |
2/2✓ Branch 0 taken 244 times.
✓ Branch 1 taken 23 times.
|
267 | if (!(desc->flags & (AV_PIX_FMT_FLAG_HWACCEL | AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_PAL)) && |
| 68 |
4/4✓ Branch 0 taken 111 times.
✓ Branch 1 taken 133 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 94 times.
|
244 | (desc->flags & AV_PIX_FMT_FLAG_PLANAR || desc->nb_components == 1) && |
| 69 |
4/6✓ Branch 0 taken 64 times.
✓ Branch 1 taken 86 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 86 times.
|
236 | (!(desc->flags & AV_PIX_FMT_FLAG_BE) == !HAVE_BIGENDIAN || desc->comp[0].depth == 8) && |
| 70 | 86 | (ret = ff_add_format(&formats, fmt)) < 0) | |
| 71 | ✗ | return ret; | |
| 72 | } | ||
| 73 | |||
| 74 | 1 | return ff_set_common_formats2(ctx, cfg_in, cfg_out, formats); | |
| 75 | } | ||
| 76 | |||
| 77 | 1 | static int config_input(AVFilterLink *inlink) | |
| 78 | { | ||
| 79 | 1 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
| 80 | 1 | AVFilterContext *ctx = inlink->dst; | |
| 81 | 1 | BoxBlurContext *s = ctx->priv; | |
| 82 | 1 | int w = inlink->w, h = inlink->h; | |
| 83 | int ret; | ||
| 84 | |||
| 85 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (!(s->temp[0] = av_malloc(2*FFMAX(w, h))) || |
| 86 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | !(s->temp[1] = av_malloc(2*FFMAX(w, h)))) |
| 87 | ✗ | return AVERROR(ENOMEM); | |
| 88 | |||
| 89 | 1 | s->hsub = desc->log2_chroma_w; | |
| 90 | 1 | s->vsub = desc->log2_chroma_h; | |
| 91 | |||
| 92 | 1 | ret = ff_boxblur_eval_filter_params(inlink, | |
| 93 | &s->luma_param, | ||
| 94 | &s->chroma_param, | ||
| 95 | &s->alpha_param); | ||
| 96 | |||
| 97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret != 0) { |
| 98 | ✗ | av_log(ctx, AV_LOG_ERROR, "Failed to evaluate " | |
| 99 | "filter params: %d.\n", ret); | ||
| 100 | ✗ | return ret; | |
| 101 | } | ||
| 102 | |||
| 103 | 1 | s->radius[Y] = s->luma_param.radius; | |
| 104 | 1 | s->radius[U] = s->radius[V] = s->chroma_param.radius; | |
| 105 | 1 | s->radius[A] = s->alpha_param.radius; | |
| 106 | |||
| 107 | 1 | s->power[Y] = s->luma_param.power; | |
| 108 | 1 | s->power[U] = s->power[V] = s->chroma_param.power; | |
| 109 | 1 | s->power[A] = s->alpha_param.power; | |
| 110 | |||
| 111 | 1 | return 0; | |
| 112 | } | ||
| 113 | |||
| 114 | /* Naive boxblur would sum source pixels from x-radius .. x+radius | ||
| 115 | * for destination pixel x. That would be O(radius*width). | ||
| 116 | * If you now look at what source pixels represent 2 consecutive | ||
| 117 | * output pixels, then you see they are almost identical and only | ||
| 118 | * differ by 2 pixels, like: | ||
| 119 | * src0 111111111 | ||
| 120 | * dst0 1 | ||
| 121 | * src1 111111111 | ||
| 122 | * dst1 1 | ||
| 123 | * src0-src1 1 -1 | ||
| 124 | * so when you know one output pixel you can find the next by just adding | ||
| 125 | * and subtracting 1 input pixel. | ||
| 126 | * The following code adopts this faster variant. | ||
| 127 | */ | ||
| 128 | #define BLUR(type, depth) \ | ||
| 129 | static inline void blur ## depth(type *dst, int dst_step, const type *src, \ | ||
| 130 | int src_step, int len, int radius) \ | ||
| 131 | { \ | ||
| 132 | const int length = radius*2 + 1; \ | ||
| 133 | const int inv = ((1<<16) + length/2)/length; \ | ||
| 134 | int x, sum = src[radius*src_step]; \ | ||
| 135 | \ | ||
| 136 | for (x = 0; x < radius; x++) \ | ||
| 137 | sum += src[x*src_step]<<1; \ | ||
| 138 | \ | ||
| 139 | sum = sum*inv + (1<<15); \ | ||
| 140 | \ | ||
| 141 | for (x = 0; x <= radius; x++) { \ | ||
| 142 | sum += (src[(radius+x)*src_step] - src[(radius-x)*src_step])*inv; \ | ||
| 143 | dst[x*dst_step] = sum>>16; \ | ||
| 144 | } \ | ||
| 145 | \ | ||
| 146 | for (; x < len-radius; x++) { \ | ||
| 147 | sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv; \ | ||
| 148 | dst[x*dst_step] = sum >>16; \ | ||
| 149 | } \ | ||
| 150 | \ | ||
| 151 | for (; x < len; x++) { \ | ||
| 152 | sum += (src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step])*inv; \ | ||
| 153 | dst[x*dst_step] = sum>>16; \ | ||
| 154 | } \ | ||
| 155 | } | ||
| 156 | |||
| 157 |
8/8✓ Branch 0 taken 128000 times.
✓ Branch 1 taken 64000 times.
✓ Branch 2 taken 192000 times.
✓ Branch 3 taken 64000 times.
✓ Branch 4 taken 14886400 times.
✓ Branch 5 taken 64000 times.
✓ Branch 6 taken 128000 times.
✓ Branch 7 taken 64000 times.
|
15398400 | BLUR(uint8_t, 8) |
| 158 | ✗ | BLUR(uint16_t, 16) | |
| 159 | |||
| 160 | #undef BLUR | ||
| 161 | |||
| 162 | 64000 | static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step, | |
| 163 | int len, int radius, int pixsize) | ||
| 164 | { | ||
| 165 |
1/2✓ Branch 0 taken 64000 times.
✗ Branch 1 not taken.
|
64000 | if (pixsize == 1) blur8 (dst, dst_step , src, src_step , len, radius); |
| 166 | ✗ | else blur16((uint16_t*)dst, dst_step>>1, (const uint16_t*)src, src_step>>1, len, radius); | |
| 167 | 64000 | } | |
| 168 | |||
| 169 | 64000 | static inline void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step, | |
| 170 | int len, int radius, int power, uint8_t *temp[2], int pixsize) | ||
| 171 | { | ||
| 172 | 64000 | uint8_t *a = temp[0], *b = temp[1]; | |
| 173 | |||
| 174 |
2/4✓ Branch 0 taken 64000 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64000 times.
✗ Branch 3 not taken.
|
64000 | if (radius && power) { |
| 175 | 64000 | blur(a, pixsize, src, src_step, len, radius, pixsize); | |
| 176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64000 times.
|
64000 | for (; power > 2; power--) { |
| 177 | uint8_t *c; | ||
| 178 | ✗ | blur(b, pixsize, a, pixsize, len, radius, pixsize); | |
| 179 | ✗ | c = a; a = b; b = c; | |
| 180 | } | ||
| 181 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64000 times.
|
64000 | if (power > 1) { |
| 182 | ✗ | blur(dst, dst_step, a, pixsize, len, radius, pixsize); | |
| 183 | } else { | ||
| 184 | int i; | ||
| 185 |
1/2✓ Branch 0 taken 64000 times.
✗ Branch 1 not taken.
|
64000 | if (pixsize == 1) { |
| 186 |
2/2✓ Branch 0 taken 15206400 times.
✓ Branch 1 taken 64000 times.
|
15270400 | for (i = 0; i < len; i++) |
| 187 | 15206400 | dst[i*dst_step] = a[i]; | |
| 188 | } else | ||
| 189 | ✗ | for (i = 0; i < len; i++) | |
| 190 | ✗ | *(uint16_t*)(dst + i*dst_step) = ((uint16_t*)a)[i]; | |
| 191 | } | ||
| 192 | } else { | ||
| 193 | int i; | ||
| 194 | ✗ | if (pixsize == 1) { | |
| 195 | ✗ | for (i = 0; i < len; i++) | |
| 196 | ✗ | dst[i*dst_step] = src[i*src_step]; | |
| 197 | } else | ||
| 198 | ✗ | for (i = 0; i < len; i++) | |
| 199 | ✗ | *(uint16_t*)(dst + i*dst_step) = *(uint16_t*)(src + i*src_step); | |
| 200 | } | ||
| 201 | 64000 | } | |
| 202 | |||
| 203 | 150 | static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, | |
| 204 | int w, int h, int radius, int power, uint8_t *temp[2], int pixsize) | ||
| 205 | { | ||
| 206 | int y; | ||
| 207 | |||
| 208 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
150 | if (radius == 0 && dst == src) |
| 209 | ✗ | return; | |
| 210 | |||
| 211 |
2/2✓ Branch 0 taken 28800 times.
✓ Branch 1 taken 150 times.
|
28950 | for (y = 0; y < h; y++) |
| 212 | 28800 | blur_power(dst + y*dst_linesize, pixsize, src + y*src_linesize, pixsize, | |
| 213 | w, radius, power, temp, pixsize); | ||
| 214 | } | ||
| 215 | |||
| 216 | 150 | static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, | |
| 217 | int w, int h, int radius, int power, uint8_t *temp[2], int pixsize) | ||
| 218 | { | ||
| 219 | int x; | ||
| 220 | |||
| 221 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
150 | if (radius == 0 && dst == src) |
| 222 | ✗ | return; | |
| 223 | |||
| 224 |
2/2✓ Branch 0 taken 35200 times.
✓ Branch 1 taken 150 times.
|
35350 | for (x = 0; x < w; x++) |
| 225 | 35200 | blur_power(dst + x*pixsize, dst_linesize, src + x*pixsize, src_linesize, | |
| 226 | h, radius, power, temp, pixsize); | ||
| 227 | } | ||
| 228 | |||
| 229 | 50 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | |
| 230 | { | ||
| 231 | 50 | AVFilterContext *ctx = inlink->dst; | |
| 232 | 50 | BoxBlurContext *s = ctx->priv; | |
| 233 | 50 | AVFilterLink *outlink = inlink->dst->outputs[0]; | |
| 234 | AVFrame *out; | ||
| 235 | int plane; | ||
| 236 | 50 | int cw = AV_CEIL_RSHIFT(inlink->w, s->hsub), ch = AV_CEIL_RSHIFT(in->height, s->vsub); | |
| 237 | 50 | int w[4] = { inlink->w, cw, cw, inlink->w }; | |
| 238 | 50 | int h[4] = { in->height, ch, ch, in->height }; | |
| 239 | 50 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
| 240 | 50 | const int depth = desc->comp[0].depth; | |
| 241 | 50 | const int pixsize = (depth+7)/8; | |
| 242 | |||
| 243 | 50 | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
| 244 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if (!out) { |
| 245 | ✗ | av_frame_free(&in); | |
| 246 | ✗ | return AVERROR(ENOMEM); | |
| 247 | } | ||
| 248 | 50 | av_frame_copy_props(out, in); | |
| 249 | |||
| 250 |
4/6✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 150 times.
✓ Branch 3 taken 50 times.
✓ Branch 4 taken 150 times.
✗ Branch 5 not taken.
|
200 | for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) |
| 251 | 150 | hblur(out->data[plane], out->linesize[plane], | |
| 252 | 150 | in ->data[plane], in ->linesize[plane], | |
| 253 | w[plane], h[plane], s->radius[plane], s->power[plane], | ||
| 254 | 150 | s->temp, pixsize); | |
| 255 | |||
| 256 |
4/6✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 150 times.
✓ Branch 3 taken 50 times.
✓ Branch 4 taken 150 times.
✗ Branch 5 not taken.
|
200 | for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) |
| 257 | 150 | vblur(out->data[plane], out->linesize[plane], | |
| 258 | 150 | out->data[plane], out->linesize[plane], | |
| 259 | w[plane], h[plane], s->radius[plane], s->power[plane], | ||
| 260 | 150 | s->temp, pixsize); | |
| 261 | |||
| 262 | 50 | av_frame_free(&in); | |
| 263 | |||
| 264 | 50 | return ff_filter_frame(outlink, out); | |
| 265 | } | ||
| 266 | |||
| 267 | #define OFFSET(x) offsetof(BoxBlurContext, x) | ||
| 268 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
| 269 | |||
| 270 | static const AVOption boxblur_options[] = { | ||
| 271 | { "luma_radius", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS }, | ||
| 272 | { "lr", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS }, | ||
| 273 | { "luma_power", "How many times should the boxblur be applied to luma", OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS }, | ||
| 274 | { "lp", "How many times should the boxblur be applied to luma", OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS }, | ||
| 275 | |||
| 276 | { "chroma_radius", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS }, | ||
| 277 | { "cr", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS }, | ||
| 278 | { "chroma_power", "How many times should the boxblur be applied to chroma", OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS }, | ||
| 279 | { "cp", "How many times should the boxblur be applied to chroma", OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS }, | ||
| 280 | |||
| 281 | { "alpha_radius", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS }, | ||
| 282 | { "ar", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS }, | ||
| 283 | { "alpha_power", "How many times should the boxblur be applied to alpha", OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS }, | ||
| 284 | { "ap", "How many times should the boxblur be applied to alpha", OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS }, | ||
| 285 | |||
| 286 | { NULL } | ||
| 287 | }; | ||
| 288 | |||
| 289 | AVFILTER_DEFINE_CLASS(boxblur); | ||
| 290 | |||
| 291 | static const AVFilterPad avfilter_vf_boxblur_inputs[] = { | ||
| 292 | { | ||
| 293 | .name = "default", | ||
| 294 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 295 | .config_props = config_input, | ||
| 296 | .filter_frame = filter_frame, | ||
| 297 | }, | ||
| 298 | }; | ||
| 299 | |||
| 300 | const FFFilter ff_vf_boxblur = { | ||
| 301 | .p.name = "boxblur", | ||
| 302 | .p.description = NULL_IF_CONFIG_SMALL("Blur the input."), | ||
| 303 | .p.priv_class = &boxblur_class, | ||
| 304 | .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, | ||
| 305 | .priv_size = sizeof(BoxBlurContext), | ||
| 306 | .uninit = uninit, | ||
| 307 | FILTER_INPUTS(avfilter_vf_boxblur_inputs), | ||
| 308 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 309 | FILTER_QUERY_FUNC2(query_formats), | ||
| 310 | }; | ||
| 311 |