vf_boxblur.c 12 KB

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