vf_boxblur.c 12 KB

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