You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

353 lines
12KB

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