vf_boxblur.c 12 KB

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