vf_boxblur.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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;
  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_minus1 == 7))
  111. ff_add_format(&formats, fmt);
  112. }
  113. ff_set_common_formats(ctx, formats);
  114. return 0;
  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. static inline void blur8(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
  179. int len, int radius)
  180. {
  181. /* Naive boxblur would sum source pixels from x-radius .. x+radius
  182. * for destination pixel x. That would be O(radius*width).
  183. * If you now look at what source pixels represent 2 consecutive
  184. * output pixels, then you see they are almost identical and only
  185. * differ by 2 pixels, like:
  186. * src0 111111111
  187. * dst0 1
  188. * src1 111111111
  189. * dst1 1
  190. * src0-src1 1 -1
  191. * so when you know one output pixel you can find the next by just adding
  192. * and subtracting 1 input pixel.
  193. * The following code adopts this faster variant.
  194. */
  195. const int length = radius*2 + 1;
  196. const int inv = ((1<<16) + length/2)/length;
  197. int x, sum = src[radius*src_step];
  198. for (x = 0; x < radius; x++)
  199. sum += src[x*src_step]<<1;
  200. sum = sum*inv + (1<<15);
  201. for (x = 0; x <= radius; x++) {
  202. sum += (src[(radius+x)*src_step] - src[(radius-x)*src_step])*inv;
  203. dst[x*dst_step] = sum>>16;
  204. }
  205. for (; x < len-radius; x++) {
  206. sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv;
  207. dst[x*dst_step] = sum >>16;
  208. }
  209. for (; x < len; x++) {
  210. sum += (src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step])*inv;
  211. dst[x*dst_step] = sum>>16;
  212. }
  213. }
  214. static inline void blur16(uint16_t *dst, int dst_step, const uint16_t *src, int src_step,
  215. int len, int radius)
  216. {
  217. const int length = radius*2 + 1;
  218. const int inv = ((1<<16) + length/2)/length;
  219. int x, sum = src[radius*src_step];
  220. for (x = 0; x < radius; x++)
  221. sum += src[x*src_step]<<1;
  222. sum = sum*inv + (1<<15);
  223. for (x = 0; x <= radius; x++) {
  224. sum += (src[(radius+x)*src_step] - src[(radius-x)*src_step])*inv;
  225. dst[x*dst_step] = sum>>16;
  226. }
  227. for (; x < len-radius; x++) {
  228. sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv;
  229. dst[x*dst_step] = sum >>16;
  230. }
  231. for (; x < len; x++) {
  232. sum += (src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step])*inv;
  233. dst[x*dst_step] = sum>>16;
  234. }
  235. }
  236. static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
  237. int len, int radius, int pixsize)
  238. {
  239. if (pixsize == 1) blur8 (dst, dst_step , src, src_step , len, radius);
  240. else blur16((uint16_t*)dst, dst_step>>1, (const uint16_t*)src, src_step>>1, len, radius);
  241. }
  242. static inline void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
  243. int len, int radius, int power, uint8_t *temp[2], int pixsize)
  244. {
  245. uint8_t *a = temp[0], *b = temp[1];
  246. if (radius && power) {
  247. blur(a, pixsize, src, src_step, len, radius, pixsize);
  248. for (; power > 2; power--) {
  249. uint8_t *c;
  250. blur(b, pixsize, a, pixsize, len, radius, pixsize);
  251. c = a; a = b; b = c;
  252. }
  253. if (power > 1) {
  254. blur(dst, dst_step, a, pixsize, len, radius, pixsize);
  255. } else {
  256. int i;
  257. if (pixsize == 1) {
  258. for (i = 0; i < len; i++)
  259. dst[i*dst_step] = a[i];
  260. } else
  261. for (i = 0; i < len; i++)
  262. *(uint16_t*)(dst + i*dst_step) = ((uint16_t*)a)[i];
  263. }
  264. } else {
  265. int i;
  266. if (pixsize == 1) {
  267. for (i = 0; i < len; i++)
  268. dst[i*dst_step] = src[i*src_step];
  269. } else
  270. for (i = 0; i < len; i++)
  271. *(uint16_t*)(dst + i*dst_step) = *(uint16_t*)(src + i*src_step);
  272. }
  273. }
  274. static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
  275. int w, int h, int radius, int power, uint8_t *temp[2], int pixsize)
  276. {
  277. int y;
  278. if (radius == 0 && dst == src)
  279. return;
  280. for (y = 0; y < h; y++)
  281. blur_power(dst + y*dst_linesize, pixsize, src + y*src_linesize, pixsize,
  282. w, radius, power, temp, pixsize);
  283. }
  284. static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
  285. int w, int h, int radius, int power, uint8_t *temp[2], int pixsize)
  286. {
  287. int x;
  288. if (radius == 0 && dst == src)
  289. return;
  290. for (x = 0; x < w; x++)
  291. blur_power(dst + x*pixsize, dst_linesize, src + x*pixsize, src_linesize,
  292. h, radius, power, temp, pixsize);
  293. }
  294. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  295. {
  296. AVFilterContext *ctx = inlink->dst;
  297. BoxBlurContext *s = ctx->priv;
  298. AVFilterLink *outlink = inlink->dst->outputs[0];
  299. AVFrame *out;
  300. int plane;
  301. int cw = FF_CEIL_RSHIFT(inlink->w, s->hsub), ch = FF_CEIL_RSHIFT(in->height, s->vsub);
  302. int w[4] = { inlink->w, cw, cw, inlink->w };
  303. int h[4] = { in->height, ch, ch, in->height };
  304. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  305. const int depth = desc->comp[0].depth_minus1 + 1;
  306. const int pixsize = (depth+7)/8;
  307. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  308. if (!out) {
  309. av_frame_free(&in);
  310. return AVERROR(ENOMEM);
  311. }
  312. av_frame_copy_props(out, in);
  313. for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++)
  314. hblur(out->data[plane], out->linesize[plane],
  315. in ->data[plane], in ->linesize[plane],
  316. w[plane], h[plane], s->radius[plane], s->power[plane],
  317. s->temp, pixsize);
  318. for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++)
  319. vblur(out->data[plane], out->linesize[plane],
  320. out->data[plane], out->linesize[plane],
  321. w[plane], h[plane], s->radius[plane], s->power[plane],
  322. s->temp, pixsize);
  323. av_frame_free(&in);
  324. return ff_filter_frame(outlink, out);
  325. }
  326. #define OFFSET(x) offsetof(BoxBlurContext, x)
  327. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  328. static const AVOption boxblur_options[] = {
  329. { "luma_radius", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
  330. { "lr", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
  331. { "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 },
  332. { "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 },
  333. { "chroma_radius", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  334. { "cr", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  335. { "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 },
  336. { "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 },
  337. { "alpha_radius", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  338. { "ar", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  339. { "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 },
  340. { "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 },
  341. { NULL }
  342. };
  343. AVFILTER_DEFINE_CLASS(boxblur);
  344. static const AVFilterPad avfilter_vf_boxblur_inputs[] = {
  345. {
  346. .name = "default",
  347. .type = AVMEDIA_TYPE_VIDEO,
  348. .config_props = config_input,
  349. .filter_frame = filter_frame,
  350. },
  351. { NULL }
  352. };
  353. static const AVFilterPad avfilter_vf_boxblur_outputs[] = {
  354. {
  355. .name = "default",
  356. .type = AVMEDIA_TYPE_VIDEO,
  357. },
  358. { NULL }
  359. };
  360. AVFilter ff_vf_boxblur = {
  361. .name = "boxblur",
  362. .description = NULL_IF_CONFIG_SMALL("Blur the input."),
  363. .priv_size = sizeof(BoxBlurContext),
  364. .priv_class = &boxblur_class,
  365. .init = init,
  366. .uninit = uninit,
  367. .query_formats = query_formats,
  368. .inputs = avfilter_vf_boxblur_inputs,
  369. .outputs = avfilter_vf_boxblur_outputs,
  370. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  371. };