vf_boxblur.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/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 PixelFormat pix_fmts[] = {
  111. PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
  112. PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_YUVA420P,
  113. PIX_FMT_YUV440P, PIX_FMT_GRAY8,
  114. PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
  115. PIX_FMT_YUVJ440P,
  116. 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. AVFilterContext *ctx = inlink->dst;
  124. BoxBlurContext *boxblur = ctx->priv;
  125. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
  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. if (!(boxblur->temp[0] = av_malloc(FFMAX(w, h))) ||
  132. !(boxblur->temp[1] = av_malloc(FFMAX(w, h))))
  133. return AVERROR(ENOMEM);
  134. boxblur->hsub = desc->log2_chroma_w;
  135. boxblur->vsub = desc->log2_chroma_h;
  136. var_values[VAR_W] = inlink->w;
  137. var_values[VAR_H] = inlink->h;
  138. var_values[VAR_CW] = cw = w>>boxblur->hsub;
  139. var_values[VAR_CH] = ch = h>>boxblur->vsub;
  140. var_values[VAR_HSUB] = 1<<boxblur->hsub;
  141. var_values[VAR_VSUB] = 1<<boxblur->vsub;
  142. #define EVAL_RADIUS_EXPR(comp) \
  143. expr = boxblur->comp##_radius_expr; \
  144. ret = av_expr_parse_and_eval(&res, expr, var_names, var_values, \
  145. NULL, NULL, NULL, NULL, NULL, 0, ctx); \
  146. boxblur->comp##_param.radius = res; \
  147. if (ret < 0) { \
  148. av_log(NULL, AV_LOG_ERROR, \
  149. "Error when evaluating " #comp " radius expression '%s'\n", expr); \
  150. return ret; \
  151. }
  152. EVAL_RADIUS_EXPR(luma);
  153. EVAL_RADIUS_EXPR(chroma);
  154. EVAL_RADIUS_EXPR(alpha);
  155. av_log(ctx, AV_LOG_VERBOSE,
  156. "luma_radius:%d luma_power:%d "
  157. "chroma_radius:%d chroma_power:%d "
  158. "alpha_radius:%d alpha_power:%d "
  159. "w:%d chroma_w:%d h:%d chroma_h:%d\n",
  160. boxblur->luma_param .radius, boxblur->luma_param .power,
  161. boxblur->chroma_param.radius, boxblur->chroma_param.power,
  162. boxblur->alpha_param .radius, boxblur->alpha_param .power,
  163. w, cw, h, ch);
  164. #define CHECK_RADIUS_VAL(w_, h_, comp) \
  165. if (boxblur->comp##_param.radius < 0 || \
  166. 2*boxblur->comp##_param.radius > FFMIN(w_, h_)) { \
  167. av_log(ctx, AV_LOG_ERROR, \
  168. "Invalid " #comp " radius value %d, must be >= 0 and <= %d\n", \
  169. boxblur->comp##_param.radius, FFMIN(w_, h_)/2); \
  170. return AVERROR(EINVAL); \
  171. }
  172. CHECK_RADIUS_VAL(w, h, luma);
  173. CHECK_RADIUS_VAL(cw, ch, chroma);
  174. CHECK_RADIUS_VAL(w, h, alpha);
  175. boxblur->radius[Y] = boxblur->luma_param.radius;
  176. boxblur->radius[U] = boxblur->radius[V] = boxblur->chroma_param.radius;
  177. boxblur->radius[A] = boxblur->alpha_param.radius;
  178. boxblur->power[Y] = boxblur->luma_param.power;
  179. boxblur->power[U] = boxblur->power[V] = boxblur->chroma_param.power;
  180. boxblur->power[A] = boxblur->alpha_param.power;
  181. return 0;
  182. }
  183. static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
  184. int len, int radius)
  185. {
  186. /* Naive boxblur would sum source pixels from x-radius .. x+radius
  187. * for destination pixel x. That would be O(radius*width).
  188. * If you now look at what source pixels represent 2 consecutive
  189. * output pixels, then you see they are almost identical and only
  190. * differ by 2 pixels, like:
  191. * src0 111111111
  192. * dst0 1
  193. * src1 111111111
  194. * dst1 1
  195. * src0-src1 1 -1
  196. * so when you know one output pixel you can find the next by just adding
  197. * and subtracting 1 input pixel.
  198. * The following code adopts this faster variant.
  199. */
  200. int x, sum = 0;
  201. const int length = radius*2 + 1;
  202. const int inv = ((1<<16) + length/2)/length;
  203. for (x = 0; x < radius; x++)
  204. sum += src[x*src_step]<<1;
  205. sum += src[radius*src_step];
  206. for (x = 0; x <= radius; x++) {
  207. sum += src[(radius+x)*src_step] - src[(radius-x)*src_step];
  208. dst[x*dst_step] = (sum*inv + (1<<15))>>16;
  209. }
  210. for (; x < len-radius; x++) {
  211. sum += src[(radius+x)*src_step] - src[(x-radius-1)*src_step];
  212. dst[x*dst_step] = (sum*inv + (1<<15))>>16;
  213. }
  214. for (; x < len; x++) {
  215. sum += src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step];
  216. dst[x*dst_step] = (sum*inv + (1<<15))>>16;
  217. }
  218. }
  219. static inline void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
  220. int len, int radius, int power, uint8_t *temp[2])
  221. {
  222. uint8_t *a = temp[0], *b = temp[1];
  223. if (radius && power) {
  224. blur(a, 1, src, src_step, len, radius);
  225. for (; power > 2; power--) {
  226. uint8_t *c;
  227. blur(b, 1, a, 1, len, radius);
  228. c = a; a = b; b = c;
  229. }
  230. if (power > 1) {
  231. blur(dst, dst_step, a, 1, len, radius);
  232. } else {
  233. int i;
  234. for (i = 0; i < len; i++)
  235. dst[i*dst_step] = a[i];
  236. }
  237. } else {
  238. int i;
  239. for (i = 0; i < len; i++)
  240. dst[i*dst_step] = src[i*src_step];
  241. }
  242. }
  243. static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
  244. int w, int h, int radius, int power, uint8_t *temp[2])
  245. {
  246. int y;
  247. if (radius == 0 && dst == src)
  248. return;
  249. for (y = 0; y < h; y++)
  250. blur_power(dst + y*dst_linesize, 1, src + y*src_linesize, 1,
  251. w, radius, power, temp);
  252. }
  253. static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
  254. int w, int h, int radius, int power, uint8_t *temp[2])
  255. {
  256. int x;
  257. if (radius == 0 && dst == src)
  258. return;
  259. for (x = 0; x < w; x++)
  260. blur_power(dst + x, dst_linesize, src + x, src_linesize,
  261. h, radius, power, temp);
  262. }
  263. static int null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { return 0; }
  264. static int end_frame(AVFilterLink *inlink)
  265. {
  266. AVFilterContext *ctx = inlink->dst;
  267. BoxBlurContext *boxblur = ctx->priv;
  268. AVFilterLink *outlink = inlink->dst->outputs[0];
  269. AVFilterBufferRef *inpicref = inlink ->cur_buf;
  270. AVFilterBufferRef *outpicref = outlink->out_buf;
  271. int plane;
  272. int cw = inlink->w >> boxblur->hsub, ch = inlink->h >> boxblur->vsub;
  273. int w[4] = { inlink->w, cw, cw, inlink->w };
  274. int h[4] = { inlink->h, ch, ch, inlink->h };
  275. for (plane = 0; inpicref->data[plane] && plane < 4; plane++)
  276. hblur(outpicref->data[plane], outpicref->linesize[plane],
  277. inpicref ->data[plane], inpicref ->linesize[plane],
  278. w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane],
  279. boxblur->temp);
  280. for (plane = 0; inpicref->data[plane] && plane < 4; plane++)
  281. vblur(outpicref->data[plane], outpicref->linesize[plane],
  282. outpicref->data[plane], outpicref->linesize[plane],
  283. w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane],
  284. boxblur->temp);
  285. ff_draw_slice(outlink, 0, inlink->h, 1);
  286. return ff_end_frame(outlink);
  287. }
  288. AVFilter avfilter_vf_boxblur = {
  289. .name = "boxblur",
  290. .description = NULL_IF_CONFIG_SMALL("Blur the input."),
  291. .priv_size = sizeof(BoxBlurContext),
  292. .init = init,
  293. .uninit = uninit,
  294. .query_formats = query_formats,
  295. .inputs = (const AVFilterPad[]) {{ .name = "default",
  296. .type = AVMEDIA_TYPE_VIDEO,
  297. .config_props = config_input,
  298. .draw_slice = null_draw_slice,
  299. .end_frame = end_frame,
  300. .min_perms = AV_PERM_READ },
  301. { .name = NULL}},
  302. .outputs = (const AVFilterPad[]) {{ .name = "default",
  303. .type = AVMEDIA_TYPE_VIDEO, },
  304. { .name = NULL}},
  305. };