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.

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