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
36
37
43
47 uint8_t *
temp[2];
///< temporary buffer used in blur_power()
49
51 {
53
56 }
57
59 {
62
70 }
71
73 }
74
76 {
82
86
87 s->hsub =
desc->log2_chroma_w;
88 s->vsub =
desc->log2_chroma_h;
89
94
97 "filter params: %d.\n",
ret);
99 }
100
101 s->radius[
Y] =
s->luma_param.radius;
102 s->radius[
U] =
s->radius[
V] =
s->chroma_param.radius;
103 s->radius[
A] =
s->alpha_param.radius;
104
105 s->power[
Y] =
s->luma_param.power;
106 s->power[
U] =
s->power[
V] =
s->chroma_param.power;
107 s->power[
A] =
s->alpha_param.power;
108
109 return 0;
110 }
111
112 /* Naive boxblur would sum source pixels from x-radius .. x+radius
113 * for destination pixel x. That would be O(radius*width).
114 * If you now look at what source pixels represent 2 consecutive
115 * output pixels, then you see they are almost identical and only
116 * differ by 2 pixels, like:
117 * src0 111111111
118 * dst0 1
119 * src1 111111111
120 * dst1 1
121 * src0-src1 1 -1
122 * so when you know one output pixel you can find the next by just adding
123 * and subtracting 1 input pixel.
124 * The following code adopts this faster variant.
125 */
126 #define BLUR(type, depth) \
127 static inline void blur ## depth(type *dst, int dst_step, const type *src, \
128 int src_step, int len, int radius) \
129 { \
130 const int length = radius*2 + 1; \
131 const int inv = ((1<<16) + length/2)/length; \
132 int x, sum = src[radius*src_step]; \
133 \
134 for (x = 0; x < radius; x++) \
135 sum += src[x*src_step]<<1; \
136 \
137 sum = sum*inv + (1<<15); \
138 \
139 for (x = 0; x <= radius; x++) { \
140 sum += (src[(radius+x)*src_step] - src[(radius-x)*src_step])*inv; \
141 dst[x*dst_step] = sum>>16; \
142 } \
143 \
144 for (; x < len-radius; x++) { \
145 sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv; \
146 dst[x*dst_step] = sum >>16; \
147 } \
148 \
149 for (; x < len; x++) { \
150 sum += (src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step])*inv; \
151 dst[x*dst_step] = sum>>16; \
152 } \
153 }
154
157
158 #undef BLUR
159
160 static inline void blur(uint8_t *dst,
int dst_step,
const uint8_t *
src,
int src_step,
161 int len,
int radius,
int pixsize)
162 {
163 if (pixsize == 1) blur8 (dst, dst_step ,
src, src_step ,
len, radius);
164 else blur16((uint16_t*)dst, dst_step>>1, (
const uint16_t*)
src, src_step>>1,
len, radius);
165 }
166
167 static inline void blur_power(uint8_t *dst,
int dst_step,
const uint8_t *
src,
int src_step,
168 int len,
int radius,
int power, uint8_t *
temp[2],
int pixsize)
169 {
171
172 if (radius &&
power) {
173 blur(
a, pixsize,
src, src_step,
len, radius, pixsize);
176 blur(
b, pixsize,
a, pixsize,
len, radius, pixsize);
178 }
180 blur(dst, dst_step,
a, pixsize,
len, radius, pixsize);
181 } else {
183 if (pixsize == 1) {
185 dst[
i*dst_step] =
a[
i];
186 } else
188 *(uint16_t*)(dst +
i*dst_step) = ((uint16_t*)
a)[
i];
189 }
190 } else {
192 if (pixsize == 1) {
194 dst[
i*dst_step] =
src[
i*src_step];
195 } else
197 *(uint16_t*)(dst +
i*dst_step) = *(uint16_t*)(
src +
i*src_step);
198 }
199 }
200
201 static void hblur(uint8_t *dst,
int dst_linesize,
const uint8_t *
src,
int src_linesize,
202 int w,
int h,
int radius,
int power, uint8_t *
temp[2],
int pixsize)
203 {
204 int y;
205
206 if (radius == 0 && dst ==
src)
207 return;
208
209 for (y = 0; y <
h; y++)
210 blur_power(dst + y*dst_linesize, pixsize,
src + y*src_linesize, pixsize,
212 }
213
214 static void vblur(uint8_t *dst,
int dst_linesize,
const uint8_t *
src,
int src_linesize,
215 int w,
int h,
int radius,
int power, uint8_t *
temp[2],
int pixsize)
216 {
217 int x;
218
219 if (radius == 0 && dst ==
src)
220 return;
221
222 for (x = 0; x <
w; x++)
223 blur_power(dst + x*pixsize, dst_linesize,
src + x*pixsize, src_linesize,
225 }
226
228 {
233 int plane;
238 const int depth =
desc->comp[0].depth;
239 const int pixsize = (depth+7)/8;
240
245 }
247
248 for (plane = 0; plane < 4 && in->
data[plane] && in->
linesize[plane]; plane++)
251 w[plane],
h[plane],
s->radius[plane],
s->power[plane],
253
254 for (plane = 0; plane < 4 && in->
data[plane] && in->
linesize[plane]; plane++)
256 out->data[plane],
out->linesize[plane],
257 w[plane],
h[plane],
s->radius[plane],
s->power[plane],
259
261
263 }
264
265 #define OFFSET(x) offsetof(BoxBlurContext, x)
266 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
267
271 {
"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 },
272 {
"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 },
273
276 {
"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 },
277 {
"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 },
278
281 {
"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 },
282 {
"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 },
283
285 };
286
288
290 {
295 },
296 };
297
299 {
302 },
303 };
304
309 .priv_class = &boxblur_class,
315 };