00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/pixdesc.h"
00029 #include "libswscale/swscale.h"
00030
00031 #include "avfilter.h"
00032 #include "formats.h"
00033 #include "internal.h"
00034
00035 #define RADIUS_MIN 0.1
00036 #define RADIUS_MAX 5.0
00037
00038 #define STRENGTH_MIN -1.0
00039 #define STRENGTH_MAX 1.0
00040
00041 #define THRESHOLD_MIN -30
00042 #define THRESHOLD_MAX 30
00043
00044 typedef struct {
00045 float radius;
00046 float strength;
00047 int threshold;
00048 float quality;
00049 struct SwsContext *filter_context;
00050 } FilterParam;
00051
00052 typedef struct {
00053 FilterParam luma;
00054 FilterParam chroma;
00055 int hsub;
00056 int vsub;
00057 unsigned int sws_flags;
00058 } SmartblurContext;
00059
00060 #define CHECK_PARAM(param, name, min, max, format, ret) \
00061 if (param < min || param > max) { \
00062 av_log(ctx, AV_LOG_ERROR, \
00063 "Invalid " #name " value " #format ": " \
00064 "must be included between range " #format " and " #format "\n",\
00065 param, min, max); \
00066 ret = AVERROR(EINVAL); \
00067 }
00068
00069 static av_cold int init(AVFilterContext *ctx, const char *args)
00070 {
00071 SmartblurContext *sblur = ctx->priv;
00072 int n = 0, ret = 0;
00073 float lradius, lstrength, cradius, cstrength;
00074 int lthreshold, cthreshold;
00075
00076 if (args)
00077 n = sscanf(args, "%f:%f:%d:%f:%f:%d",
00078 &lradius, &lstrength, <hreshold,
00079 &cradius, &cstrength, &cthreshold);
00080
00081 if (n != 3 && n != 6) {
00082 av_log(ctx, AV_LOG_ERROR,
00083 "Incorrect number of parameters or invalid syntax: "
00084 "must be luma_radius:luma_strength:luma_threshold"
00085 "[:chroma_radius:chroma_strength:chroma_threshold]\n");
00086 return AVERROR(EINVAL);
00087 }
00088
00089 sblur->luma.radius = lradius;
00090 sblur->luma.strength = lstrength;
00091 sblur->luma.threshold = lthreshold;
00092
00093 if (n == 3) {
00094 sblur->chroma.radius = sblur->luma.radius;
00095 sblur->chroma.strength = sblur->luma.strength;
00096 sblur->chroma.threshold = sblur->luma.threshold;
00097 } else {
00098 sblur->chroma.radius = cradius;
00099 sblur->chroma.strength = cstrength;
00100 sblur->chroma.threshold = cthreshold;
00101 }
00102
00103 sblur->luma.quality = sblur->chroma.quality = 3.0;
00104 sblur->sws_flags = SWS_BICUBIC;
00105
00106 CHECK_PARAM(lradius, luma radius, RADIUS_MIN, RADIUS_MAX, %0.1f, ret)
00107 CHECK_PARAM(lstrength, luma strength, STRENGTH_MIN, STRENGTH_MAX, %0.1f, ret)
00108 CHECK_PARAM(lthreshold, luma threshold, THRESHOLD_MIN, THRESHOLD_MAX, %d, ret)
00109
00110 if (n != 3) {
00111 CHECK_PARAM(sblur->chroma.radius, chroma radius, RADIUS_MIN, RADIUS_MAX, %0.1f, ret)
00112 CHECK_PARAM(sblur->chroma.strength, chroma strength, STRENGTH_MIN, STRENGTH_MAX, %0.1f, ret)
00113 CHECK_PARAM(sblur->chroma.threshold, chroma threshold, THRESHOLD_MIN,THRESHOLD_MAX, %d, ret)
00114 }
00115
00116 return ret;
00117 }
00118
00119 static av_cold void uninit(AVFilterContext *ctx)
00120 {
00121 SmartblurContext *sblur = ctx->priv;
00122
00123 sws_freeContext(sblur->luma.filter_context);
00124 sws_freeContext(sblur->chroma.filter_context);
00125 }
00126
00127 static int query_formats(AVFilterContext *ctx)
00128 {
00129 static const enum AVPixelFormat pix_fmts[] = {
00130 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
00131 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
00132 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
00133 AV_PIX_FMT_GRAY8,
00134 AV_PIX_FMT_NONE
00135 };
00136
00137 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00138
00139 return 0;
00140 }
00141
00142 static int alloc_sws_context(FilterParam *f, int width, int height, unsigned int flags)
00143 {
00144 SwsVector *vec;
00145 SwsFilter sws_filter;
00146
00147 vec = sws_getGaussianVec(f->radius, f->quality);
00148
00149 if (!vec)
00150 return AVERROR(EINVAL);
00151
00152 sws_scaleVec(vec, f->strength);
00153 vec->coeff[vec->length / 2] += 1.0 - f->strength;
00154 sws_filter.lumH = sws_filter.lumV = vec;
00155 sws_filter.chrH = sws_filter.chrV = NULL;
00156 f->filter_context = sws_getCachedContext(NULL,
00157 width, height, AV_PIX_FMT_GRAY8,
00158 width, height, AV_PIX_FMT_GRAY8,
00159 flags, &sws_filter, NULL, NULL);
00160
00161 sws_freeVec(vec);
00162
00163 if (!f->filter_context)
00164 return AVERROR(EINVAL);
00165
00166 return 0;
00167 }
00168
00169 static int config_props(AVFilterLink *inlink)
00170 {
00171 SmartblurContext *sblur = inlink->dst->priv;
00172 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
00173
00174 sblur->hsub = desc->log2_chroma_w;
00175 sblur->vsub = desc->log2_chroma_h;
00176
00177 alloc_sws_context(&sblur->luma, inlink->w, inlink->h, sblur->sws_flags);
00178 alloc_sws_context(&sblur->chroma,
00179 inlink->w >> sblur->hsub, inlink->h >> sblur->vsub,
00180 sblur->sws_flags);
00181
00182 return 0;
00183 }
00184
00185 static void blur(uint8_t *dst, const int dst_linesize,
00186 const uint8_t *src, const int src_linesize,
00187 const int w, const int h, const int threshold,
00188 struct SwsContext *filter_context)
00189 {
00190 int x, y;
00191 int orig, filtered;
00192 int diff;
00193
00194 const uint8_t* const src_array[4] = {src};
00195 uint8_t *dst_array[4] = {dst};
00196 int src_linesize_array[4] = {src_linesize};
00197 int dst_linesize_array[4] = {dst_linesize};
00198
00199 sws_scale(filter_context, src_array, src_linesize_array,
00200 0, h, dst_array, dst_linesize_array);
00201
00202 if (threshold > 0) {
00203 for (y = 0; y < h; ++y) {
00204 for (x = 0; x < w; ++x) {
00205 orig = src[x + y * src_linesize];
00206 filtered = dst[x + y * dst_linesize];
00207 diff = orig - filtered;
00208
00209 if (diff > 0) {
00210 if (diff > 2 * threshold)
00211 dst[x + y * dst_linesize] = orig;
00212 else if (diff > threshold)
00213
00214 dst[x + y * dst_linesize] = orig - threshold;
00215 } else {
00216 if (-diff > 2 * threshold)
00217 dst[x + y * dst_linesize] = orig;
00218 else if (-diff > threshold)
00219
00220 dst[x + y * dst_linesize] = orig + threshold;
00221 }
00222 }
00223 }
00224 } else if (threshold < 0) {
00225 for (y = 0; y < h; ++y) {
00226 for (x = 0; x < w; ++x) {
00227 orig = src[x + y * src_linesize];
00228 filtered = dst[x + y * dst_linesize];
00229 diff = orig - filtered;
00230
00231 if (diff > 0) {
00232 if (diff <= -threshold)
00233 dst[x + y * dst_linesize] = orig;
00234 else if (diff <= -2 * threshold)
00235
00236 dst[x + y * dst_linesize] = filtered - threshold;
00237 } else {
00238 if (diff >= threshold)
00239 dst[x + y * dst_linesize] = orig;
00240 else if (diff >= 2 * threshold)
00241
00242 dst[x + y * dst_linesize] = filtered + threshold;
00243 }
00244 }
00245 }
00246 }
00247 }
00248
00249 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
00250 {
00251 SmartblurContext *sblur = inlink->dst->priv;
00252 AVFilterLink *outlink = inlink->dst->outputs[0];
00253 AVFilterBufferRef *outpic;
00254 int cw = inlink->w >> sblur->hsub;
00255 int ch = inlink->h >> sblur->vsub;
00256
00257 outpic = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
00258 if (!outpic) {
00259 avfilter_unref_bufferp(&inpic);
00260 return AVERROR(ENOMEM);
00261 }
00262 avfilter_copy_buffer_ref_props(outpic, inpic);
00263
00264 blur(outpic->data[0], outpic->linesize[0],
00265 inpic->data[0], inpic->linesize[0],
00266 inlink->w, inlink->h, sblur->luma.threshold,
00267 sblur->luma.filter_context);
00268
00269 if (inpic->data[2]) {
00270 blur(outpic->data[1], outpic->linesize[1],
00271 inpic->data[1], inpic->linesize[1],
00272 cw, ch, sblur->chroma.threshold,
00273 sblur->chroma.filter_context);
00274 blur(outpic->data[2], outpic->linesize[2],
00275 inpic->data[2], inpic->linesize[2],
00276 cw, ch, sblur->chroma.threshold,
00277 sblur->chroma.filter_context);
00278 }
00279
00280 avfilter_unref_bufferp(&inpic);
00281 return ff_filter_frame(outlink, outpic);
00282 }
00283
00284 static const AVFilterPad smartblur_inputs[] = {
00285 {
00286 .name = "default",
00287 .type = AVMEDIA_TYPE_VIDEO,
00288 .filter_frame = filter_frame,
00289 .config_props = config_props,
00290 .min_perms = AV_PERM_READ,
00291 },
00292 { NULL }
00293 };
00294
00295 static const AVFilterPad smartblur_outputs[] = {
00296 {
00297 .name = "default",
00298 .type = AVMEDIA_TYPE_VIDEO,
00299 },
00300 { NULL }
00301 };
00302
00303 AVFilter avfilter_vf_smartblur = {
00304 .name = "smartblur",
00305 .description = NULL_IF_CONFIG_SMALL("Blur the input video without impacting the outlines."),
00306
00307 .priv_size = sizeof(SmartblurContext),
00308
00309 .init = init,
00310 .uninit = uninit,
00311 .query_formats = query_formats,
00312 .inputs = smartblur_inputs,
00313 .outputs = smartblur_outputs,
00314 };