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.

352 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 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/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. AVFilterContext *ctx = inlink->dst;
  122. BoxBlurContext *boxblur = ctx->priv;
  123. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
  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. if (!(boxblur->temp[0] = av_malloc(FFMAX(w, h))) ||
  130. !(boxblur->temp[1] = av_malloc(FFMAX(w, h))))
  131. return AVERROR(ENOMEM);
  132. boxblur->hsub = desc->log2_chroma_w;
  133. boxblur->vsub = desc->log2_chroma_h;
  134. var_values[VAR_W] = inlink->w;
  135. var_values[VAR_H] = inlink->h;
  136. var_values[VAR_CW] = cw = w>>boxblur->hsub;
  137. var_values[VAR_CH] = ch = h>>boxblur->vsub;
  138. var_values[VAR_HSUB] = 1<<boxblur->hsub;
  139. var_values[VAR_VSUB] = 1<<boxblur->vsub;
  140. #define EVAL_RADIUS_EXPR(comp) \
  141. expr = boxblur->comp##_radius_expr; \
  142. ret = av_expr_parse_and_eval(&res, expr, var_names, var_values, \
  143. NULL, NULL, NULL, NULL, NULL, 0, ctx); \
  144. boxblur->comp##_param.radius = res; \
  145. if (ret < 0) { \
  146. av_log(NULL, AV_LOG_ERROR, \
  147. "Error when evaluating " #comp " radius expression '%s'\n", expr); \
  148. return ret; \
  149. }
  150. EVAL_RADIUS_EXPR(luma);
  151. EVAL_RADIUS_EXPR(chroma);
  152. EVAL_RADIUS_EXPR(alpha);
  153. av_log(ctx, AV_LOG_INFO,
  154. "luma_radius:%d luma_power:%d "
  155. "chroma_radius:%d chroma_power:%d "
  156. "alpha_radius:%d alpha_power:%d "
  157. "w:%d chroma_w:%d h:%d chroma_h:%d\n",
  158. boxblur->luma_param .radius, boxblur->luma_param .power,
  159. boxblur->chroma_param.radius, boxblur->chroma_param.power,
  160. boxblur->alpha_param .radius, boxblur->alpha_param .power,
  161. w, cw, h, ch);
  162. #define CHECK_RADIUS_VAL(w_, h_, comp) \
  163. if (boxblur->comp##_param.radius < 0 || \
  164. 2*boxblur->comp##_param.radius > FFMIN(w_, h_)) { \
  165. av_log(ctx, AV_LOG_ERROR, \
  166. "Invalid " #comp " radius value %d, must be >= 0 and <= %d\n", \
  167. boxblur->comp##_param.radius, FFMIN(w_, h_)/2); \
  168. return AVERROR(EINVAL); \
  169. }
  170. CHECK_RADIUS_VAL(w, h, luma);
  171. CHECK_RADIUS_VAL(cw, ch, chroma);
  172. CHECK_RADIUS_VAL(w, h, alpha);
  173. boxblur->radius[Y] = boxblur->luma_param.radius;
  174. boxblur->radius[U] = boxblur->radius[V] = boxblur->chroma_param.radius;
  175. boxblur->radius[A] = boxblur->alpha_param.radius;
  176. boxblur->power[Y] = boxblur->luma_param.power;
  177. boxblur->power[U] = boxblur->power[V] = boxblur->chroma_param.power;
  178. boxblur->power[A] = boxblur->alpha_param.power;
  179. return 0;
  180. }
  181. static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
  182. int len, int radius)
  183. {
  184. /* Naive boxblur would sum source pixels from x-radius .. x+radius
  185. * for destination pixel x. That would be O(radius*width).
  186. * If you now look at what source pixels represent 2 consecutive
  187. * output pixels, then you see they are almost identical and only
  188. * differ by 2 pixels, like:
  189. * src0 111111111
  190. * dst0 1
  191. * src1 111111111
  192. * dst1 1
  193. * src0-src1 1 -1
  194. * so when you know one output pixel you can find the next by just adding
  195. * and subtracting 1 input pixel.
  196. * The following code adopts this faster variant.
  197. */
  198. int x, sum = 0;
  199. const int length = radius*2 + 1;
  200. const int inv = ((1<<16) + length/2)/length;
  201. for (x = 0; x < radius; x++)
  202. sum += src[x*src_step]<<1;
  203. sum += src[radius*src_step];
  204. for (x = 0; x <= radius; x++) {
  205. sum += src[(radius+x)*src_step] - src[(radius-x)*src_step];
  206. dst[x*dst_step] = (sum*inv + (1<<15))>>16;
  207. }
  208. for (; x < len-radius; x++) {
  209. sum += src[(radius+x)*src_step] - src[(x-radius-1)*src_step];
  210. dst[x*dst_step] = (sum*inv + (1<<15))>>16;
  211. }
  212. for (; x < len; x++) {
  213. sum += src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step];
  214. dst[x*dst_step] = (sum*inv + (1<<15))>>16;
  215. }
  216. }
  217. static inline void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
  218. int len, int radius, int power, uint8_t *temp[2])
  219. {
  220. uint8_t *a = temp[0], *b = temp[1];
  221. if (radius && power) {
  222. blur(a, 1, src, src_step, len, radius);
  223. for (; power > 2; power--) {
  224. uint8_t *c;
  225. blur(b, 1, a, 1, len, radius);
  226. c = a; a = b; b = c;
  227. }
  228. if (power > 1) {
  229. blur(dst, dst_step, a, 1, len, radius);
  230. } else {
  231. int i;
  232. for (i = 0; i < len; i++)
  233. dst[i*dst_step] = a[i];
  234. }
  235. } else {
  236. int i;
  237. for (i = 0; i < len; i++)
  238. dst[i*dst_step] = src[i*src_step];
  239. }
  240. }
  241. static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
  242. int w, int h, int radius, int power, uint8_t *temp[2])
  243. {
  244. int y;
  245. if (radius == 0 && dst == src)
  246. return;
  247. for (y = 0; y < h; y++)
  248. blur_power(dst + y*dst_linesize, 1, src + y*src_linesize, 1,
  249. w, radius, power, temp);
  250. }
  251. static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
  252. int w, int h, int radius, int power, uint8_t *temp[2])
  253. {
  254. int x;
  255. if (radius == 0 && dst == src)
  256. return;
  257. for (x = 0; x < w; x++)
  258. blur_power(dst + x, dst_linesize, src + x, src_linesize,
  259. h, radius, power, temp);
  260. }
  261. static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
  262. static void end_frame(AVFilterLink *inlink)
  263. {
  264. AVFilterContext *ctx = inlink->dst;
  265. BoxBlurContext *boxblur = ctx->priv;
  266. AVFilterLink *outlink = inlink->dst->outputs[0];
  267. AVFilterBufferRef *inpicref = inlink ->cur_buf;
  268. AVFilterBufferRef *outpicref = outlink->out_buf;
  269. int plane;
  270. int cw = inlink->w >> boxblur->hsub, ch = inlink->h >> boxblur->vsub;
  271. int w[4] = { inlink->w, cw, cw, inlink->w };
  272. int h[4] = { inlink->h, ch, ch, inlink->h };
  273. for (plane = 0; inpicref->data[plane] && plane < 4; plane++)
  274. hblur(outpicref->data[plane], outpicref->linesize[plane],
  275. inpicref ->data[plane], inpicref ->linesize[plane],
  276. w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane],
  277. boxblur->temp);
  278. for (plane = 0; inpicref->data[plane] && plane < 4; plane++)
  279. vblur(outpicref->data[plane], outpicref->linesize[plane],
  280. outpicref->data[plane], outpicref->linesize[plane],
  281. w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane],
  282. boxblur->temp);
  283. ff_draw_slice(outlink, 0, inlink->h, 1);
  284. avfilter_default_end_frame(inlink);
  285. }
  286. AVFilter avfilter_vf_boxblur = {
  287. .name = "boxblur",
  288. .description = NULL_IF_CONFIG_SMALL("Blur the input."),
  289. .priv_size = sizeof(BoxBlurContext),
  290. .init = init,
  291. .uninit = uninit,
  292. .query_formats = query_formats,
  293. .inputs = (const AVFilterPad[]) {{ .name = "default",
  294. .type = AVMEDIA_TYPE_VIDEO,
  295. .config_props = config_input,
  296. .draw_slice = null_draw_slice,
  297. .end_frame = end_frame,
  298. .min_perms = AV_PERM_READ },
  299. { .name = NULL}},
  300. .outputs = (const AVFilterPad[]) {{ .name = "default",
  301. .type = AVMEDIA_TYPE_VIDEO, },
  302. { .name = NULL}},
  303. };