vf_boxblur.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. * @file
  23. * Apply a boxblur filter to the input video.
  24. * Ported from MPlayer libmpcodecs/vf_boxblur.c.
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/eval.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. #include "video.h"
  35. static const char *const var_names[] = {
  36. "w",
  37. "h",
  38. "cw",
  39. "ch",
  40. "hsub",
  41. "vsub",
  42. NULL
  43. };
  44. enum var_name {
  45. VAR_W,
  46. VAR_H,
  47. VAR_CW,
  48. VAR_CH,
  49. VAR_HSUB,
  50. VAR_VSUB,
  51. VARS_NB
  52. };
  53. typedef struct {
  54. int radius;
  55. int power;
  56. char *radius_expr;
  57. } FilterParam;
  58. typedef struct {
  59. const AVClass *class;
  60. FilterParam luma_param;
  61. FilterParam chroma_param;
  62. FilterParam alpha_param;
  63. int hsub, vsub;
  64. int radius[4];
  65. int power[4];
  66. uint8_t *temp[2]; ///< temporary buffer used in blur_power()
  67. } BoxBlurContext;
  68. #define Y 0
  69. #define U 1
  70. #define V 2
  71. #define A 3
  72. static av_cold int init(AVFilterContext *ctx)
  73. {
  74. BoxBlurContext *s = ctx->priv;
  75. if (!s->luma_param.radius_expr) {
  76. av_log(ctx, AV_LOG_ERROR, "Luma radius expression is not set.\n");
  77. return AVERROR(EINVAL);
  78. }
  79. /* fill missing params */
  80. if (!s->chroma_param.radius_expr) {
  81. s->chroma_param.radius_expr = av_strdup(s->luma_param.radius_expr);
  82. if (!s->chroma_param.radius_expr)
  83. return AVERROR(ENOMEM);
  84. }
  85. if (s->chroma_param.power < 0)
  86. s->chroma_param.power = s->luma_param.power;
  87. if (!s->alpha_param.radius_expr) {
  88. s->alpha_param.radius_expr = av_strdup(s->luma_param.radius_expr);
  89. if (!s->alpha_param.radius_expr)
  90. return AVERROR(ENOMEM);
  91. }
  92. if (s->alpha_param.power < 0)
  93. s->alpha_param.power = s->luma_param.power;
  94. return 0;
  95. }
  96. static av_cold void uninit(AVFilterContext *ctx)
  97. {
  98. BoxBlurContext *s = ctx->priv;
  99. av_freep(&s->temp[0]);
  100. av_freep(&s->temp[1]);
  101. }
  102. static int query_formats(AVFilterContext *ctx)
  103. {
  104. static const enum AVPixelFormat pix_fmts[] = {
  105. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  106. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
  107. AV_PIX_FMT_YUV440P, AV_PIX_FMT_GRAY8,
  108. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  109. AV_PIX_FMT_YUVJ440P,
  110. AV_PIX_FMT_NONE
  111. };
  112. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  113. return 0;
  114. }
  115. static int config_input(AVFilterLink *inlink)
  116. {
  117. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  118. AVFilterContext *ctx = inlink->dst;
  119. BoxBlurContext *s = ctx->priv;
  120. int w = inlink->w, h = inlink->h;
  121. int cw, ch;
  122. double var_values[VARS_NB], res;
  123. char *expr;
  124. int ret;
  125. if (!(s->temp[0] = av_malloc(FFMAX(w, h))) ||
  126. !(s->temp[1] = av_malloc(FFMAX(w, h))))
  127. return AVERROR(ENOMEM);
  128. s->hsub = desc->log2_chroma_w;
  129. s->vsub = desc->log2_chroma_h;
  130. var_values[VAR_W] = inlink->w;
  131. var_values[VAR_H] = inlink->h;
  132. var_values[VAR_CW] = cw = w>>s->hsub;
  133. var_values[VAR_CH] = ch = h>>s->vsub;
  134. var_values[VAR_HSUB] = 1<<s->hsub;
  135. var_values[VAR_VSUB] = 1<<s->vsub;
  136. #define EVAL_RADIUS_EXPR(comp) \
  137. expr = s->comp##_param.radius_expr; \
  138. ret = av_expr_parse_and_eval(&res, expr, var_names, var_values, \
  139. NULL, NULL, NULL, NULL, NULL, 0, ctx); \
  140. s->comp##_param.radius = res; \
  141. if (ret < 0) { \
  142. av_log(NULL, AV_LOG_ERROR, \
  143. "Error when evaluating " #comp " radius expression '%s'\n", expr); \
  144. return ret; \
  145. }
  146. EVAL_RADIUS_EXPR(luma);
  147. EVAL_RADIUS_EXPR(chroma);
  148. EVAL_RADIUS_EXPR(alpha);
  149. av_log(ctx, AV_LOG_VERBOSE,
  150. "luma_radius:%d luma_power:%d "
  151. "chroma_radius:%d chroma_power:%d "
  152. "alpha_radius:%d alpha_power:%d "
  153. "w:%d chroma_w:%d h:%d chroma_h:%d\n",
  154. s->luma_param .radius, s->luma_param .power,
  155. s->chroma_param.radius, s->chroma_param.power,
  156. s->alpha_param .radius, s->alpha_param .power,
  157. w, cw, h, ch);
  158. #define CHECK_RADIUS_VAL(w_, h_, comp) \
  159. if (s->comp##_param.radius < 0 || \
  160. 2*s->comp##_param.radius > FFMIN(w_, h_)) { \
  161. av_log(ctx, AV_LOG_ERROR, \
  162. "Invalid " #comp " radius value %d, must be >= 0 and <= %d\n", \
  163. s->comp##_param.radius, FFMIN(w_, h_)/2); \
  164. return AVERROR(EINVAL); \
  165. }
  166. CHECK_RADIUS_VAL(w, h, luma);
  167. CHECK_RADIUS_VAL(cw, ch, chroma);
  168. CHECK_RADIUS_VAL(w, h, alpha);
  169. s->radius[Y] = s->luma_param.radius;
  170. s->radius[U] = s->radius[V] = s->chroma_param.radius;
  171. s->radius[A] = s->alpha_param.radius;
  172. s->power[Y] = s->luma_param.power;
  173. s->power[U] = s->power[V] = s->chroma_param.power;
  174. s->power[A] = s->alpha_param.power;
  175. return 0;
  176. }
  177. static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
  178. int len, int radius)
  179. {
  180. /* Naive boxblur would sum source pixels from x-radius .. x+radius
  181. * for destination pixel x. That would be O(radius*width).
  182. * If you now look at what source pixels represent 2 consecutive
  183. * output pixels, then you see they are almost identical and only
  184. * differ by 2 pixels, like:
  185. * src0 111111111
  186. * dst0 1
  187. * src1 111111111
  188. * dst1 1
  189. * src0-src1 1 -1
  190. * so when you know one output pixel you can find the next by just adding
  191. * and subtracting 1 input pixel.
  192. * The following code adopts this faster variant.
  193. */
  194. const int length = radius*2 + 1;
  195. const int inv = ((1<<16) + length/2)/length;
  196. int x, sum = 0;
  197. for (x = 0; x < radius; x++)
  198. sum += src[x*src_step]<<1;
  199. sum += src[radius*src_step];
  200. for (x = 0; x <= radius; x++) {
  201. sum += src[(radius+x)*src_step] - src[(radius-x)*src_step];
  202. dst[x*dst_step] = (sum*inv + (1<<15))>>16;
  203. }
  204. for (; x < len-radius; x++) {
  205. sum += src[(radius+x)*src_step] - src[(x-radius-1)*src_step];
  206. dst[x*dst_step] = (sum*inv + (1<<15))>>16;
  207. }
  208. for (; x < len; x++) {
  209. sum += src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step];
  210. dst[x*dst_step] = (sum*inv + (1<<15))>>16;
  211. }
  212. }
  213. static inline void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
  214. int len, int radius, int power, uint8_t *temp[2])
  215. {
  216. uint8_t *a = temp[0], *b = temp[1];
  217. if (radius && power) {
  218. blur(a, 1, src, src_step, len, radius);
  219. for (; power > 2; power--) {
  220. uint8_t *c;
  221. blur(b, 1, a, 1, len, radius);
  222. c = a; a = b; b = c;
  223. }
  224. if (power > 1) {
  225. blur(dst, dst_step, a, 1, len, radius);
  226. } else {
  227. int i;
  228. for (i = 0; i < len; i++)
  229. dst[i*dst_step] = a[i];
  230. }
  231. } else {
  232. int i;
  233. for (i = 0; i < len; i++)
  234. dst[i*dst_step] = src[i*src_step];
  235. }
  236. }
  237. static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
  238. int w, int h, int radius, int power, uint8_t *temp[2])
  239. {
  240. int y;
  241. if (radius == 0 && dst == src)
  242. return;
  243. for (y = 0; y < h; y++)
  244. blur_power(dst + y*dst_linesize, 1, src + y*src_linesize, 1,
  245. w, radius, power, temp);
  246. }
  247. static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
  248. int w, int h, int radius, int power, uint8_t *temp[2])
  249. {
  250. int x;
  251. if (radius == 0 && dst == src)
  252. return;
  253. for (x = 0; x < w; x++)
  254. blur_power(dst + x, dst_linesize, src + x, src_linesize,
  255. h, radius, power, temp);
  256. }
  257. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  258. {
  259. AVFilterContext *ctx = inlink->dst;
  260. BoxBlurContext *s = ctx->priv;
  261. AVFilterLink *outlink = inlink->dst->outputs[0];
  262. AVFrame *out;
  263. int plane;
  264. int cw = FF_CEIL_RSHIFT(inlink->w, s->hsub), ch = FF_CEIL_RSHIFT(in->height, s->vsub);
  265. int w[4] = { inlink->w, cw, cw, inlink->w };
  266. int h[4] = { in->height, ch, ch, in->height };
  267. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  268. if (!out) {
  269. av_frame_free(&in);
  270. return AVERROR(ENOMEM);
  271. }
  272. av_frame_copy_props(out, in);
  273. for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++)
  274. hblur(out->data[plane], out->linesize[plane],
  275. in ->data[plane], in ->linesize[plane],
  276. w[plane], h[plane], s->radius[plane], s->power[plane],
  277. s->temp);
  278. for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++)
  279. vblur(out->data[plane], out->linesize[plane],
  280. out->data[plane], out->linesize[plane],
  281. w[plane], h[plane], s->radius[plane], s->power[plane],
  282. s->temp);
  283. av_frame_free(&in);
  284. return ff_filter_frame(outlink, out);
  285. }
  286. #define OFFSET(x) offsetof(BoxBlurContext, x)
  287. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  288. static const AVOption boxblur_options[] = {
  289. { "luma_radius", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
  290. { "lr", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
  291. { "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 },
  292. { "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 },
  293. { "chroma_radius", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  294. { "cr", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  295. { "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 },
  296. { "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 },
  297. { "alpha_radius", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  298. { "ar", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  299. { "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 },
  300. { "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 },
  301. { NULL }
  302. };
  303. AVFILTER_DEFINE_CLASS(boxblur);
  304. static const AVFilterPad avfilter_vf_boxblur_inputs[] = {
  305. {
  306. .name = "default",
  307. .type = AVMEDIA_TYPE_VIDEO,
  308. .config_props = config_input,
  309. .filter_frame = filter_frame,
  310. },
  311. { NULL }
  312. };
  313. static const AVFilterPad avfilter_vf_boxblur_outputs[] = {
  314. {
  315. .name = "default",
  316. .type = AVMEDIA_TYPE_VIDEO,
  317. },
  318. { NULL }
  319. };
  320. AVFilter ff_vf_boxblur = {
  321. .name = "boxblur",
  322. .description = NULL_IF_CONFIG_SMALL("Blur the input."),
  323. .priv_size = sizeof(BoxBlurContext),
  324. .priv_class = &boxblur_class,
  325. .init = init,
  326. .uninit = uninit,
  327. .query_formats = query_formats,
  328. .inputs = avfilter_vf_boxblur_inputs,
  329. .outputs = avfilter_vf_boxblur_outputs,
  330. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  331. };