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.

438 lines
15KB

  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 FilterParam {
  54. int radius;
  55. int power;
  56. char *radius_expr;
  57. } FilterParam;
  58. typedef struct BoxBlurContext {
  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 Y 0
  69. #define U 1
  70. #define V 2
  71. #define A 3
  72. static av_cold int init(AVFilterContext *ctx)
  73. {
  74. BoxBlurContext *s = ctx->priv;
  75. if (!s->luma_param.radius_expr) {
  76. av_log(ctx, AV_LOG_ERROR, "Luma radius expression is not set.\n");
  77. return AVERROR(EINVAL);
  78. }
  79. /* fill missing params */
  80. if (!s->chroma_param.radius_expr) {
  81. s->chroma_param.radius_expr = av_strdup(s->luma_param.radius_expr);
  82. if (!s->chroma_param.radius_expr)
  83. return AVERROR(ENOMEM);
  84. }
  85. if (s->chroma_param.power < 0)
  86. s->chroma_param.power = s->luma_param.power;
  87. if (!s->alpha_param.radius_expr) {
  88. s->alpha_param.radius_expr = av_strdup(s->luma_param.radius_expr);
  89. if (!s->alpha_param.radius_expr)
  90. return AVERROR(ENOMEM);
  91. }
  92. if (s->alpha_param.power < 0)
  93. s->alpha_param.power = s->luma_param.power;
  94. return 0;
  95. }
  96. static av_cold void uninit(AVFilterContext *ctx)
  97. {
  98. BoxBlurContext *s = ctx->priv;
  99. av_freep(&s->temp[0]);
  100. av_freep(&s->temp[1]);
  101. }
  102. static int query_formats(AVFilterContext *ctx)
  103. {
  104. static const enum AVPixelFormat pix_fmts[] = {
  105. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  106. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
  107. AV_PIX_FMT_YUV440P, AV_PIX_FMT_GRAY8,
  108. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  109. AV_PIX_FMT_YUVJ440P,
  110. AV_PIX_FMT_GBRP,
  111. AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10,
  112. AV_PIX_FMT_YUVA420P10,
  113. AV_PIX_FMT_GBRP10,
  114. AV_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. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  122. AVFilterContext *ctx = inlink->dst;
  123. BoxBlurContext *s = ctx->priv;
  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 (!(s->temp[0] = av_malloc(2*FFMAX(w, h))) ||
  130. !(s->temp[1] = av_malloc(2*FFMAX(w, h))))
  131. return AVERROR(ENOMEM);
  132. s->hsub = desc->log2_chroma_w;
  133. s->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>>s->hsub;
  137. var_values[VAR_CH] = ch = h>>s->vsub;
  138. var_values[VAR_HSUB] = 1<<s->hsub;
  139. var_values[VAR_VSUB] = 1<<s->vsub;
  140. #define EVAL_RADIUS_EXPR(comp) \
  141. expr = s->comp##_param.radius_expr; \
  142. ret = av_expr_parse_and_eval(&res, expr, var_names, var_values, \
  143. NULL, NULL, NULL, NULL, NULL, 0, ctx); \
  144. s->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_VERBOSE,
  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. s->luma_param .radius, s->luma_param .power,
  159. s->chroma_param.radius, s->chroma_param.power,
  160. s->alpha_param .radius, s->alpha_param .power,
  161. w, cw, h, ch);
  162. #define CHECK_RADIUS_VAL(w_, h_, comp) \
  163. if (s->comp##_param.radius < 0 || \
  164. 2*s->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. s->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. s->radius[Y] = s->luma_param.radius;
  174. s->radius[U] = s->radius[V] = s->chroma_param.radius;
  175. s->radius[A] = s->alpha_param.radius;
  176. s->power[Y] = s->luma_param.power;
  177. s->power[U] = s->power[V] = s->chroma_param.power;
  178. s->power[A] = s->alpha_param.power;
  179. return 0;
  180. }
  181. static inline void blur8(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. const int length = radius*2 + 1;
  199. const int inv = ((1<<16) + length/2)/length;
  200. int x, sum = src[radius*src_step];
  201. for (x = 0; x < radius; x++)
  202. sum += src[x*src_step]<<1;
  203. sum = sum*inv + (1<<15);
  204. for (x = 0; x <= radius; x++) {
  205. sum += (src[(radius+x)*src_step] - src[(radius-x)*src_step])*inv;
  206. dst[x*dst_step] = sum>>16;
  207. }
  208. for (; x < len-radius; x++) {
  209. sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv;
  210. dst[x*dst_step] = sum >>16;
  211. }
  212. for (; x < len; x++) {
  213. sum += (src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step])*inv;
  214. dst[x*dst_step] = sum>>16;
  215. }
  216. }
  217. static inline void blur16(uint16_t *dst, int dst_step, const uint16_t *src, int src_step,
  218. int len, int radius)
  219. {
  220. const int length = radius*2 + 1;
  221. const int inv = ((1<<16) + length/2)/length;
  222. int x, sum = src[radius*src_step];
  223. for (x = 0; x < radius; x++)
  224. sum += src[x*src_step]<<1;
  225. sum = sum*inv + (1<<15);
  226. for (x = 0; x <= radius; x++) {
  227. sum += (src[(radius+x)*src_step] - src[(radius-x)*src_step])*inv;
  228. dst[x*dst_step] = sum>>16;
  229. }
  230. for (; x < len-radius; x++) {
  231. sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv;
  232. dst[x*dst_step] = sum >>16;
  233. }
  234. for (; x < len; x++) {
  235. sum += (src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step])*inv;
  236. dst[x*dst_step] = sum>>16;
  237. }
  238. }
  239. static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
  240. int len, int radius, int pixsize)
  241. {
  242. if (pixsize == 1) blur8 (dst, dst_step , src, src_step , len, radius);
  243. else blur16((uint16_t*)dst, dst_step>>1, (const uint16_t*)src, src_step>>1, len, radius);
  244. }
  245. static inline void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
  246. int len, int radius, int power, uint8_t *temp[2], int pixsize)
  247. {
  248. uint8_t *a = temp[0], *b = temp[1];
  249. if (radius && power) {
  250. blur(a, pixsize, src, src_step, len, radius, pixsize);
  251. for (; power > 2; power--) {
  252. uint8_t *c;
  253. blur(b, pixsize, a, pixsize, len, radius, pixsize);
  254. c = a; a = b; b = c;
  255. }
  256. if (power > 1) {
  257. blur(dst, dst_step, a, pixsize, len, radius, pixsize);
  258. } else {
  259. int i;
  260. if (pixsize == 1) {
  261. for (i = 0; i < len; i++)
  262. dst[i*dst_step] = a[i];
  263. } else
  264. for (i = 0; i < len; i++)
  265. *(uint16_t*)(dst + i*dst_step) = ((uint16_t*)a)[i];
  266. }
  267. } else {
  268. int i;
  269. if (pixsize == 1) {
  270. for (i = 0; i < len; i++)
  271. dst[i*dst_step] = src[i*src_step];
  272. } else
  273. for (i = 0; i < len; i++)
  274. *(uint16_t*)(dst + i*dst_step) = *(uint16_t*)(src + i*src_step);
  275. }
  276. }
  277. static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
  278. int w, int h, int radius, int power, uint8_t *temp[2], int pixsize)
  279. {
  280. int y;
  281. if (radius == 0 && dst == src)
  282. return;
  283. for (y = 0; y < h; y++)
  284. blur_power(dst + y*dst_linesize, pixsize, src + y*src_linesize, pixsize,
  285. w, radius, power, temp, pixsize);
  286. }
  287. static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
  288. int w, int h, int radius, int power, uint8_t *temp[2], int pixsize)
  289. {
  290. int x;
  291. if (radius == 0 && dst == src)
  292. return;
  293. for (x = 0; x < w; x++)
  294. blur_power(dst + x*pixsize, dst_linesize, src + x*pixsize, src_linesize,
  295. h, radius, power, temp, pixsize);
  296. }
  297. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  298. {
  299. AVFilterContext *ctx = inlink->dst;
  300. BoxBlurContext *s = ctx->priv;
  301. AVFilterLink *outlink = inlink->dst->outputs[0];
  302. AVFrame *out;
  303. int plane;
  304. int cw = FF_CEIL_RSHIFT(inlink->w, s->hsub), ch = FF_CEIL_RSHIFT(in->height, s->vsub);
  305. int w[4] = { inlink->w, cw, cw, inlink->w };
  306. int h[4] = { in->height, ch, ch, in->height };
  307. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  308. const int depth = desc->comp[0].depth_minus1 + 1;
  309. const int pixsize = (depth+7)/8;
  310. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  311. if (!out) {
  312. av_frame_free(&in);
  313. return AVERROR(ENOMEM);
  314. }
  315. av_frame_copy_props(out, in);
  316. for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++)
  317. hblur(out->data[plane], out->linesize[plane],
  318. in ->data[plane], in ->linesize[plane],
  319. w[plane], h[plane], s->radius[plane], s->power[plane],
  320. s->temp, pixsize);
  321. for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++)
  322. vblur(out->data[plane], out->linesize[plane],
  323. out->data[plane], out->linesize[plane],
  324. w[plane], h[plane], s->radius[plane], s->power[plane],
  325. s->temp, pixsize);
  326. av_frame_free(&in);
  327. return ff_filter_frame(outlink, out);
  328. }
  329. #define OFFSET(x) offsetof(BoxBlurContext, x)
  330. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  331. static const AVOption boxblur_options[] = {
  332. { "luma_radius", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
  333. { "lr", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
  334. { "luma_power", "How many times should the boxblur be applied to luma", OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS },
  335. { "lp", "How many times should the boxblur be applied to luma", OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS },
  336. { "chroma_radius", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  337. { "cr", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  338. { "chroma_power", "How many times should the boxblur be applied to chroma", OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  339. { "cp", "How many times should the boxblur be applied to chroma", OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  340. { "alpha_radius", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  341. { "ar", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  342. { "alpha_power", "How many times should the boxblur be applied to alpha", OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  343. { "ap", "How many times should the boxblur be applied to alpha", OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  344. { NULL }
  345. };
  346. AVFILTER_DEFINE_CLASS(boxblur);
  347. static const AVFilterPad avfilter_vf_boxblur_inputs[] = {
  348. {
  349. .name = "default",
  350. .type = AVMEDIA_TYPE_VIDEO,
  351. .config_props = config_input,
  352. .filter_frame = filter_frame,
  353. },
  354. { NULL }
  355. };
  356. static const AVFilterPad avfilter_vf_boxblur_outputs[] = {
  357. {
  358. .name = "default",
  359. .type = AVMEDIA_TYPE_VIDEO,
  360. },
  361. { NULL }
  362. };
  363. AVFilter ff_vf_boxblur = {
  364. .name = "boxblur",
  365. .description = NULL_IF_CONFIG_SMALL("Blur the input."),
  366. .priv_size = sizeof(BoxBlurContext),
  367. .priv_class = &boxblur_class,
  368. .init = init,
  369. .uninit = uninit,
  370. .query_formats = query_formats,
  371. .inputs = avfilter_vf_boxblur_inputs,
  372. .outputs = avfilter_vf_boxblur_outputs,
  373. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  374. };