vf_boxblur.c 12 KB

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