vf_boxblur.c 15 KB

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