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.

397 lines
13KB

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