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.

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