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.

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