Commit | Line | Data |
---|---|---|
2ba45a60 DM |
1 | /* |
2 | * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at> | |
3 | * Copyright (c) 2012 Jeremy Tran | |
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 smartblur filter to the input video | |
25 | * Ported from MPlayer libmpcodecs/vf_smartblur.c by Michael Niedermayer. | |
26 | */ | |
27 | ||
28 | #include "libavutil/opt.h" | |
29 | #include "libavutil/pixdesc.h" | |
30 | #include "libswscale/swscale.h" | |
31 | ||
32 | #include "avfilter.h" | |
33 | #include "formats.h" | |
34 | #include "internal.h" | |
35 | ||
36 | #define RADIUS_MIN 0.1 | |
37 | #define RADIUS_MAX 5.0 | |
38 | ||
39 | #define STRENGTH_MIN -1.0 | |
40 | #define STRENGTH_MAX 1.0 | |
41 | ||
42 | #define THRESHOLD_MIN -30 | |
43 | #define THRESHOLD_MAX 30 | |
44 | ||
45 | typedef struct { | |
46 | float radius; | |
47 | float strength; | |
48 | int threshold; | |
49 | float quality; | |
50 | struct SwsContext *filter_context; | |
51 | } FilterParam; | |
52 | ||
53 | typedef struct { | |
54 | const AVClass *class; | |
55 | FilterParam luma; | |
56 | FilterParam chroma; | |
57 | int hsub; | |
58 | int vsub; | |
59 | unsigned int sws_flags; | |
60 | } SmartblurContext; | |
61 | ||
62 | #define OFFSET(x) offsetof(SmartblurContext, x) | |
63 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM | |
64 | ||
65 | static const AVOption smartblur_options[] = { | |
66 | { "luma_radius", "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS }, | |
67 | { "lr" , "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS }, | |
68 | { "luma_strength", "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS }, | |
69 | { "ls", "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS }, | |
70 | { "luma_threshold", "set luma threshold", OFFSET(luma.threshold), AV_OPT_TYPE_INT, {.i64=0}, THRESHOLD_MIN, THRESHOLD_MAX, .flags=FLAGS }, | |
71 | { "lt", "set luma threshold", OFFSET(luma.threshold), AV_OPT_TYPE_INT, {.i64=0}, THRESHOLD_MIN, THRESHOLD_MAX, .flags=FLAGS }, | |
72 | ||
73 | { "chroma_radius", "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS }, | |
74 | { "cr", "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS }, | |
75 | { "chroma_strength", "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS }, | |
76 | { "cs", "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS }, | |
77 | { "chroma_threshold", "set chroma threshold", OFFSET(chroma.threshold), AV_OPT_TYPE_INT, {.i64=THRESHOLD_MIN-1}, THRESHOLD_MIN-1, THRESHOLD_MAX, .flags=FLAGS }, | |
78 | { "ct", "set chroma threshold", OFFSET(chroma.threshold), AV_OPT_TYPE_INT, {.i64=THRESHOLD_MIN-1}, THRESHOLD_MIN-1, THRESHOLD_MAX, .flags=FLAGS }, | |
79 | ||
80 | { NULL } | |
81 | }; | |
82 | ||
83 | AVFILTER_DEFINE_CLASS(smartblur); | |
84 | ||
85 | static av_cold int init(AVFilterContext *ctx) | |
86 | { | |
87 | SmartblurContext *sblur = ctx->priv; | |
88 | ||
89 | /* make chroma default to luma values, if not explicitly set */ | |
90 | if (sblur->chroma.radius < RADIUS_MIN) | |
91 | sblur->chroma.radius = sblur->luma.radius; | |
92 | if (sblur->chroma.strength < STRENGTH_MIN) | |
93 | sblur->chroma.strength = sblur->luma.strength; | |
94 | if (sblur->chroma.threshold < THRESHOLD_MIN) | |
95 | sblur->chroma.threshold = sblur->luma.threshold; | |
96 | ||
97 | sblur->luma.quality = sblur->chroma.quality = 3.0; | |
98 | sblur->sws_flags = SWS_BICUBIC; | |
99 | ||
100 | av_log(ctx, AV_LOG_VERBOSE, | |
101 | "luma_radius:%f luma_strength:%f luma_threshold:%d " | |
102 | "chroma_radius:%f chroma_strength:%f chroma_threshold:%d\n", | |
103 | sblur->luma.radius, sblur->luma.strength, sblur->luma.threshold, | |
104 | sblur->chroma.radius, sblur->chroma.strength, sblur->chroma.threshold); | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
109 | static av_cold void uninit(AVFilterContext *ctx) | |
110 | { | |
111 | SmartblurContext *sblur = ctx->priv; | |
112 | ||
113 | sws_freeContext(sblur->luma.filter_context); | |
114 | sws_freeContext(sblur->chroma.filter_context); | |
115 | } | |
116 | ||
117 | static int query_formats(AVFilterContext *ctx) | |
118 | { | |
119 | static const enum AVPixelFormat pix_fmts[] = { | |
120 | AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, | |
121 | AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, | |
122 | AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P, | |
123 | AV_PIX_FMT_GRAY8, | |
124 | AV_PIX_FMT_NONE | |
125 | }; | |
126 | ||
127 | ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); | |
128 | ||
129 | return 0; | |
130 | } | |
131 | ||
132 | static int alloc_sws_context(FilterParam *f, int width, int height, unsigned int flags) | |
133 | { | |
134 | SwsVector *vec; | |
135 | SwsFilter sws_filter; | |
136 | ||
137 | vec = sws_getGaussianVec(f->radius, f->quality); | |
138 | ||
139 | if (!vec) | |
140 | return AVERROR(EINVAL); | |
141 | ||
142 | sws_scaleVec(vec, f->strength); | |
143 | vec->coeff[vec->length / 2] += 1.0 - f->strength; | |
144 | sws_filter.lumH = sws_filter.lumV = vec; | |
145 | sws_filter.chrH = sws_filter.chrV = NULL; | |
146 | f->filter_context = sws_getCachedContext(NULL, | |
147 | width, height, AV_PIX_FMT_GRAY8, | |
148 | width, height, AV_PIX_FMT_GRAY8, | |
149 | flags, &sws_filter, NULL, NULL); | |
150 | ||
151 | sws_freeVec(vec); | |
152 | ||
153 | if (!f->filter_context) | |
154 | return AVERROR(EINVAL); | |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
159 | static int config_props(AVFilterLink *inlink) | |
160 | { | |
161 | SmartblurContext *sblur = inlink->dst->priv; | |
162 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
163 | ||
164 | sblur->hsub = desc->log2_chroma_w; | |
165 | sblur->vsub = desc->log2_chroma_h; | |
166 | ||
167 | alloc_sws_context(&sblur->luma, inlink->w, inlink->h, sblur->sws_flags); | |
168 | alloc_sws_context(&sblur->chroma, | |
169 | FF_CEIL_RSHIFT(inlink->w, sblur->hsub), | |
170 | FF_CEIL_RSHIFT(inlink->h, sblur->vsub), | |
171 | sblur->sws_flags); | |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
176 | static void blur(uint8_t *dst, const int dst_linesize, | |
177 | const uint8_t *src, const int src_linesize, | |
178 | const int w, const int h, const int threshold, | |
179 | struct SwsContext *filter_context) | |
180 | { | |
181 | int x, y; | |
182 | int orig, filtered; | |
183 | int diff; | |
184 | /* Declare arrays of 4 to get aligned data */ | |
185 | const uint8_t* const src_array[4] = {src}; | |
186 | uint8_t *dst_array[4] = {dst}; | |
187 | int src_linesize_array[4] = {src_linesize}; | |
188 | int dst_linesize_array[4] = {dst_linesize}; | |
189 | ||
190 | sws_scale(filter_context, src_array, src_linesize_array, | |
191 | 0, h, dst_array, dst_linesize_array); | |
192 | ||
193 | if (threshold > 0) { | |
194 | for (y = 0; y < h; ++y) { | |
195 | for (x = 0; x < w; ++x) { | |
196 | orig = src[x + y * src_linesize]; | |
197 | filtered = dst[x + y * dst_linesize]; | |
198 | diff = orig - filtered; | |
199 | ||
200 | if (diff > 0) { | |
201 | if (diff > 2 * threshold) | |
202 | dst[x + y * dst_linesize] = orig; | |
203 | else if (diff > threshold) | |
204 | /* add 'diff' and subtract 'threshold' from 'filtered' */ | |
205 | dst[x + y * dst_linesize] = orig - threshold; | |
206 | } else { | |
207 | if (-diff > 2 * threshold) | |
208 | dst[x + y * dst_linesize] = orig; | |
209 | else if (-diff > threshold) | |
210 | /* add 'diff' and 'threshold' to 'filtered' */ | |
211 | dst[x + y * dst_linesize] = orig + threshold; | |
212 | } | |
213 | } | |
214 | } | |
215 | } else if (threshold < 0) { | |
216 | for (y = 0; y < h; ++y) { | |
217 | for (x = 0; x < w; ++x) { | |
218 | orig = src[x + y * src_linesize]; | |
219 | filtered = dst[x + y * dst_linesize]; | |
220 | diff = orig - filtered; | |
221 | ||
222 | if (diff > 0) { | |
223 | if (diff <= -threshold) | |
224 | dst[x + y * dst_linesize] = orig; | |
225 | else if (diff <= -2 * threshold) | |
226 | /* subtract 'diff' and 'threshold' from 'orig' */ | |
227 | dst[x + y * dst_linesize] = filtered - threshold; | |
228 | } else { | |
229 | if (diff >= threshold) | |
230 | dst[x + y * dst_linesize] = orig; | |
231 | else if (diff >= 2 * threshold) | |
232 | /* add 'threshold' and subtract 'diff' from 'orig' */ | |
233 | dst[x + y * dst_linesize] = filtered + threshold; | |
234 | } | |
235 | } | |
236 | } | |
237 | } | |
238 | } | |
239 | ||
240 | static int filter_frame(AVFilterLink *inlink, AVFrame *inpic) | |
241 | { | |
242 | SmartblurContext *sblur = inlink->dst->priv; | |
243 | AVFilterLink *outlink = inlink->dst->outputs[0]; | |
244 | AVFrame *outpic; | |
245 | int cw = FF_CEIL_RSHIFT(inlink->w, sblur->hsub); | |
246 | int ch = FF_CEIL_RSHIFT(inlink->h, sblur->vsub); | |
247 | ||
248 | outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
249 | if (!outpic) { | |
250 | av_frame_free(&inpic); | |
251 | return AVERROR(ENOMEM); | |
252 | } | |
253 | av_frame_copy_props(outpic, inpic); | |
254 | ||
255 | blur(outpic->data[0], outpic->linesize[0], | |
256 | inpic->data[0], inpic->linesize[0], | |
257 | inlink->w, inlink->h, sblur->luma.threshold, | |
258 | sblur->luma.filter_context); | |
259 | ||
260 | if (inpic->data[2]) { | |
261 | blur(outpic->data[1], outpic->linesize[1], | |
262 | inpic->data[1], inpic->linesize[1], | |
263 | cw, ch, sblur->chroma.threshold, | |
264 | sblur->chroma.filter_context); | |
265 | blur(outpic->data[2], outpic->linesize[2], | |
266 | inpic->data[2], inpic->linesize[2], | |
267 | cw, ch, sblur->chroma.threshold, | |
268 | sblur->chroma.filter_context); | |
269 | } | |
270 | ||
271 | av_frame_free(&inpic); | |
272 | return ff_filter_frame(outlink, outpic); | |
273 | } | |
274 | ||
275 | static const AVFilterPad smartblur_inputs[] = { | |
276 | { | |
277 | .name = "default", | |
278 | .type = AVMEDIA_TYPE_VIDEO, | |
279 | .filter_frame = filter_frame, | |
280 | .config_props = config_props, | |
281 | }, | |
282 | { NULL } | |
283 | }; | |
284 | ||
285 | static const AVFilterPad smartblur_outputs[] = { | |
286 | { | |
287 | .name = "default", | |
288 | .type = AVMEDIA_TYPE_VIDEO, | |
289 | }, | |
290 | { NULL } | |
291 | }; | |
292 | ||
293 | AVFilter ff_vf_smartblur = { | |
294 | .name = "smartblur", | |
295 | .description = NULL_IF_CONFIG_SMALL("Blur the input video without impacting the outlines."), | |
296 | .priv_size = sizeof(SmartblurContext), | |
297 | .init = init, | |
298 | .uninit = uninit, | |
299 | .query_formats = query_formats, | |
300 | .inputs = smartblur_inputs, | |
301 | .outputs = smartblur_outputs, | |
302 | .priv_class = &smartblur_class, | |
303 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, | |
304 | }; |