vf_boxblur.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/opt.h"
  29. #include "avfilter.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. #include "boxblur.h"
  34. typedef struct BoxBlurContext {
  35. const AVClass *class;
  36. FilterParam luma_param;
  37. FilterParam chroma_param;
  38. FilterParam alpha_param;
  39. int hsub, vsub;
  40. int radius[4];
  41. int power[4];
  42. uint8_t *temp[2]; ///< temporary buffer used in blur_power()
  43. } BoxBlurContext;
  44. static av_cold void uninit(AVFilterContext *ctx)
  45. {
  46. BoxBlurContext *s = ctx->priv;
  47. av_freep(&s->temp[0]);
  48. av_freep(&s->temp[1]);
  49. }
  50. static int query_formats(AVFilterContext *ctx)
  51. {
  52. AVFilterFormats *formats = NULL;
  53. int fmt, ret;
  54. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  55. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  56. if (!(desc->flags & (AV_PIX_FMT_FLAG_HWACCEL | AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_PAL)) &&
  57. (desc->flags & AV_PIX_FMT_FLAG_PLANAR || desc->nb_components == 1) &&
  58. (!(desc->flags & AV_PIX_FMT_FLAG_BE) == !HAVE_BIGENDIAN || desc->comp[0].depth == 8) &&
  59. (ret = ff_add_format(&formats, fmt)) < 0)
  60. return ret;
  61. }
  62. return ff_set_common_formats(ctx, formats);
  63. }
  64. static int config_input(AVFilterLink *inlink)
  65. {
  66. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  67. AVFilterContext *ctx = inlink->dst;
  68. BoxBlurContext *s = ctx->priv;
  69. int w = inlink->w, h = inlink->h;
  70. int ret;
  71. if (!(s->temp[0] = av_malloc(2*FFMAX(w, h))) ||
  72. !(s->temp[1] = av_malloc(2*FFMAX(w, h))))
  73. return AVERROR(ENOMEM);
  74. s->hsub = desc->log2_chroma_w;
  75. s->vsub = desc->log2_chroma_h;
  76. ret = ff_boxblur_eval_filter_params(inlink,
  77. &s->luma_param,
  78. &s->chroma_param,
  79. &s->alpha_param);
  80. if (ret != 0) {
  81. av_log(ctx, AV_LOG_ERROR, "Failed to evaluate "
  82. "filter params: %d.\n", ret);
  83. return ret;
  84. }
  85. s->radius[Y] = s->luma_param.radius;
  86. s->radius[U] = s->radius[V] = s->chroma_param.radius;
  87. s->radius[A] = s->alpha_param.radius;
  88. s->power[Y] = s->luma_param.power;
  89. s->power[U] = s->power[V] = s->chroma_param.power;
  90. s->power[A] = s->alpha_param.power;
  91. return 0;
  92. }
  93. /* Naive boxblur would sum source pixels from x-radius .. x+radius
  94. * for destination pixel x. That would be O(radius*width).
  95. * If you now look at what source pixels represent 2 consecutive
  96. * output pixels, then you see they are almost identical and only
  97. * differ by 2 pixels, like:
  98. * src0 111111111
  99. * dst0 1
  100. * src1 111111111
  101. * dst1 1
  102. * src0-src1 1 -1
  103. * so when you know one output pixel you can find the next by just adding
  104. * and subtracting 1 input pixel.
  105. * The following code adopts this faster variant.
  106. */
  107. #define BLUR(type, depth) \
  108. static inline void blur ## depth(type *dst, int dst_step, const type *src, \
  109. int src_step, int len, int radius) \
  110. { \
  111. const int length = radius*2 + 1; \
  112. const int inv = ((1<<16) + length/2)/length; \
  113. int x, sum = src[radius*src_step]; \
  114. \
  115. for (x = 0; x < radius; x++) \
  116. sum += src[x*src_step]<<1; \
  117. \
  118. sum = sum*inv + (1<<15); \
  119. \
  120. for (x = 0; x <= radius; x++) { \
  121. sum += (src[(radius+x)*src_step] - src[(radius-x)*src_step])*inv; \
  122. dst[x*dst_step] = sum>>16; \
  123. } \
  124. \
  125. for (; x < len-radius; x++) { \
  126. sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv; \
  127. dst[x*dst_step] = sum >>16; \
  128. } \
  129. \
  130. for (; x < len; x++) { \
  131. sum += (src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step])*inv; \
  132. dst[x*dst_step] = sum>>16; \
  133. } \
  134. }
  135. BLUR(uint8_t, 8)
  136. BLUR(uint16_t, 16)
  137. #undef BLUR
  138. static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
  139. int len, int radius, int pixsize)
  140. {
  141. if (pixsize == 1) blur8 (dst, dst_step , src, src_step , len, radius);
  142. else blur16((uint16_t*)dst, dst_step>>1, (const uint16_t*)src, src_step>>1, len, radius);
  143. }
  144. static inline void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
  145. int len, int radius, int power, uint8_t *temp[2], int pixsize)
  146. {
  147. uint8_t *a = temp[0], *b = temp[1];
  148. if (radius && power) {
  149. blur(a, pixsize, src, src_step, len, radius, pixsize);
  150. for (; power > 2; power--) {
  151. uint8_t *c;
  152. blur(b, pixsize, a, pixsize, len, radius, pixsize);
  153. c = a; a = b; b = c;
  154. }
  155. if (power > 1) {
  156. blur(dst, dst_step, a, pixsize, len, radius, pixsize);
  157. } else {
  158. int i;
  159. if (pixsize == 1) {
  160. for (i = 0; i < len; i++)
  161. dst[i*dst_step] = a[i];
  162. } else
  163. for (i = 0; i < len; i++)
  164. *(uint16_t*)(dst + i*dst_step) = ((uint16_t*)a)[i];
  165. }
  166. } else {
  167. int i;
  168. if (pixsize == 1) {
  169. for (i = 0; i < len; i++)
  170. dst[i*dst_step] = src[i*src_step];
  171. } else
  172. for (i = 0; i < len; i++)
  173. *(uint16_t*)(dst + i*dst_step) = *(uint16_t*)(src + i*src_step);
  174. }
  175. }
  176. static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
  177. int w, int h, int radius, int power, uint8_t *temp[2], int pixsize)
  178. {
  179. int y;
  180. if (radius == 0 && dst == src)
  181. return;
  182. for (y = 0; y < h; y++)
  183. blur_power(dst + y*dst_linesize, pixsize, src + y*src_linesize, pixsize,
  184. w, radius, power, temp, pixsize);
  185. }
  186. static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
  187. int w, int h, int radius, int power, uint8_t *temp[2], int pixsize)
  188. {
  189. int x;
  190. if (radius == 0 && dst == src)
  191. return;
  192. for (x = 0; x < w; x++)
  193. blur_power(dst + x*pixsize, dst_linesize, src + x*pixsize, src_linesize,
  194. h, radius, power, temp, pixsize);
  195. }
  196. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  197. {
  198. AVFilterContext *ctx = inlink->dst;
  199. BoxBlurContext *s = ctx->priv;
  200. AVFilterLink *outlink = inlink->dst->outputs[0];
  201. AVFrame *out;
  202. int plane;
  203. int cw = AV_CEIL_RSHIFT(inlink->w, s->hsub), ch = AV_CEIL_RSHIFT(in->height, s->vsub);
  204. int w[4] = { inlink->w, cw, cw, inlink->w };
  205. int h[4] = { in->height, ch, ch, in->height };
  206. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  207. const int depth = desc->comp[0].depth;
  208. const int pixsize = (depth+7)/8;
  209. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  210. if (!out) {
  211. av_frame_free(&in);
  212. return AVERROR(ENOMEM);
  213. }
  214. av_frame_copy_props(out, in);
  215. for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++)
  216. hblur(out->data[plane], out->linesize[plane],
  217. in ->data[plane], in ->linesize[plane],
  218. w[plane], h[plane], s->radius[plane], s->power[plane],
  219. s->temp, pixsize);
  220. for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++)
  221. vblur(out->data[plane], out->linesize[plane],
  222. out->data[plane], out->linesize[plane],
  223. w[plane], h[plane], s->radius[plane], s->power[plane],
  224. s->temp, pixsize);
  225. av_frame_free(&in);
  226. return ff_filter_frame(outlink, out);
  227. }
  228. #define OFFSET(x) offsetof(BoxBlurContext, x)
  229. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  230. static const AVOption boxblur_options[] = {
  231. { "luma_radius", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
  232. { "lr", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
  233. { "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 },
  234. { "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 },
  235. { "chroma_radius", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  236. { "cr", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  237. { "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 },
  238. { "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 },
  239. { "alpha_radius", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  240. { "ar", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  241. { "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 },
  242. { "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 },
  243. { NULL }
  244. };
  245. AVFILTER_DEFINE_CLASS(boxblur);
  246. static const AVFilterPad avfilter_vf_boxblur_inputs[] = {
  247. {
  248. .name = "default",
  249. .type = AVMEDIA_TYPE_VIDEO,
  250. .config_props = config_input,
  251. .filter_frame = filter_frame,
  252. },
  253. { NULL }
  254. };
  255. static const AVFilterPad avfilter_vf_boxblur_outputs[] = {
  256. {
  257. .name = "default",
  258. .type = AVMEDIA_TYPE_VIDEO,
  259. },
  260. { NULL }
  261. };
  262. AVFilter ff_vf_boxblur = {
  263. .name = "boxblur",
  264. .description = NULL_IF_CONFIG_SMALL("Blur the input."),
  265. .priv_size = sizeof(BoxBlurContext),
  266. .priv_class = &boxblur_class,
  267. .uninit = uninit,
  268. .query_formats = query_formats,
  269. .inputs = avfilter_vf_boxblur_inputs,
  270. .outputs = avfilter_vf_boxblur_outputs,
  271. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  272. };