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.

318 lines
11KB

  1. /*
  2. * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (c) 2012 Jeremy Tran
  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 smartblur filter to the input video
  24. * Ported from MPlayer libmpcodecs/vf_smartblur.c by Michael Niedermayer.
  25. */
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "libswscale/swscale.h"
  29. #include "avfilter.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. #define RADIUS_MIN 0.1
  33. #define RADIUS_MAX 5.0
  34. #define STRENGTH_MIN -1.0
  35. #define STRENGTH_MAX 1.0
  36. #define THRESHOLD_MIN -30
  37. #define THRESHOLD_MAX 30
  38. typedef struct {
  39. float radius;
  40. float strength;
  41. int threshold;
  42. float quality;
  43. struct SwsContext *filter_context;
  44. } FilterParam;
  45. typedef struct {
  46. const AVClass *class;
  47. FilterParam luma;
  48. FilterParam chroma;
  49. int hsub;
  50. int vsub;
  51. unsigned int sws_flags;
  52. } SmartblurContext;
  53. #define OFFSET(x) offsetof(SmartblurContext, x)
  54. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  55. static const AVOption smartblur_options[] = {
  56. { "luma_radius", "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
  57. { "lr" , "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
  58. { "luma_strength", "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
  59. { "ls", "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
  60. { "luma_threshold", "set luma threshold", OFFSET(luma.threshold), AV_OPT_TYPE_INT, {.i64=0}, THRESHOLD_MIN, THRESHOLD_MAX, .flags=FLAGS },
  61. { "lt", "set luma threshold", OFFSET(luma.threshold), AV_OPT_TYPE_INT, {.i64=0}, THRESHOLD_MIN, THRESHOLD_MAX, .flags=FLAGS },
  62. { "chroma_radius", "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
  63. { "cr", "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
  64. { "chroma_strength", "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
  65. { "cs", "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
  66. { "chroma_threshold", "set chroma threshold", OFFSET(chroma.threshold), AV_OPT_TYPE_INT, {.i64=THRESHOLD_MIN-1}, THRESHOLD_MIN-1, THRESHOLD_MAX, .flags=FLAGS },
  67. { "ct", "set chroma threshold", OFFSET(chroma.threshold), AV_OPT_TYPE_INT, {.i64=THRESHOLD_MIN-1}, THRESHOLD_MIN-1, THRESHOLD_MAX, .flags=FLAGS },
  68. { NULL }
  69. };
  70. AVFILTER_DEFINE_CLASS(smartblur);
  71. static av_cold int init(AVFilterContext *ctx, const char *args)
  72. {
  73. SmartblurContext *sblur = ctx->priv;
  74. int ret;
  75. static const char *shorthand[] = {
  76. "luma_radius", "luma_strength", "luma_threshold",
  77. "chroma_radius", "chroma_strength", "chroma_threshold",
  78. NULL
  79. };
  80. sblur->class = &smartblur_class;
  81. av_opt_set_defaults(sblur);
  82. if ((ret = av_opt_set_from_string(sblur, args, shorthand, "=", ":")) < 0)
  83. return ret;
  84. /* make chroma default to luma values, if not explicitly set */
  85. if (sblur->chroma.radius < RADIUS_MIN)
  86. sblur->chroma.radius = sblur->luma.radius;
  87. if (sblur->chroma.strength < STRENGTH_MIN)
  88. sblur->chroma.strength = sblur->luma.strength;
  89. if (sblur->chroma.threshold < THRESHOLD_MIN)
  90. sblur->chroma.threshold = sblur->luma.threshold;
  91. sblur->luma.quality = sblur->chroma.quality = 3.0;
  92. sblur->sws_flags = SWS_BICUBIC;
  93. av_log(ctx, AV_LOG_VERBOSE,
  94. "luma_radius:%f luma_strength:%f luma_threshold:%d "
  95. "chroma_radius:%f chroma_strength:%f chroma_threshold:%d ",
  96. sblur->luma.radius, sblur->luma.strength, sblur->luma.threshold,
  97. sblur->chroma.radius, sblur->chroma.strength, sblur->chroma.threshold);
  98. return 0;
  99. }
  100. static av_cold void uninit(AVFilterContext *ctx)
  101. {
  102. SmartblurContext *sblur = ctx->priv;
  103. sws_freeContext(sblur->luma.filter_context);
  104. sws_freeContext(sblur->chroma.filter_context);
  105. av_opt_free(sblur);
  106. }
  107. static int query_formats(AVFilterContext *ctx)
  108. {
  109. static const enum AVPixelFormat pix_fmts[] = {
  110. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  111. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  112. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  113. AV_PIX_FMT_GRAY8,
  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 alloc_sws_context(FilterParam *f, int width, int height, unsigned int flags)
  120. {
  121. SwsVector *vec;
  122. SwsFilter sws_filter;
  123. vec = sws_getGaussianVec(f->radius, f->quality);
  124. if (!vec)
  125. return AVERROR(EINVAL);
  126. sws_scaleVec(vec, f->strength);
  127. vec->coeff[vec->length / 2] += 1.0 - f->strength;
  128. sws_filter.lumH = sws_filter.lumV = vec;
  129. sws_filter.chrH = sws_filter.chrV = NULL;
  130. f->filter_context = sws_getCachedContext(NULL,
  131. width, height, AV_PIX_FMT_GRAY8,
  132. width, height, AV_PIX_FMT_GRAY8,
  133. flags, &sws_filter, NULL, NULL);
  134. sws_freeVec(vec);
  135. if (!f->filter_context)
  136. return AVERROR(EINVAL);
  137. return 0;
  138. }
  139. static int config_props(AVFilterLink *inlink)
  140. {
  141. SmartblurContext *sblur = inlink->dst->priv;
  142. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  143. sblur->hsub = desc->log2_chroma_w;
  144. sblur->vsub = desc->log2_chroma_h;
  145. alloc_sws_context(&sblur->luma, inlink->w, inlink->h, sblur->sws_flags);
  146. alloc_sws_context(&sblur->chroma,
  147. inlink->w >> sblur->hsub, inlink->h >> sblur->vsub,
  148. sblur->sws_flags);
  149. return 0;
  150. }
  151. static void blur(uint8_t *dst, const int dst_linesize,
  152. const uint8_t *src, const int src_linesize,
  153. const int w, const int h, const int threshold,
  154. struct SwsContext *filter_context)
  155. {
  156. int x, y;
  157. int orig, filtered;
  158. int diff;
  159. /* Declare arrays of 4 to get aligned data */
  160. const uint8_t* const src_array[4] = {src};
  161. uint8_t *dst_array[4] = {dst};
  162. int src_linesize_array[4] = {src_linesize};
  163. int dst_linesize_array[4] = {dst_linesize};
  164. sws_scale(filter_context, src_array, src_linesize_array,
  165. 0, h, dst_array, dst_linesize_array);
  166. if (threshold > 0) {
  167. for (y = 0; y < h; ++y) {
  168. for (x = 0; x < w; ++x) {
  169. orig = src[x + y * src_linesize];
  170. filtered = dst[x + y * dst_linesize];
  171. diff = orig - filtered;
  172. if (diff > 0) {
  173. if (diff > 2 * threshold)
  174. dst[x + y * dst_linesize] = orig;
  175. else if (diff > threshold)
  176. /* add 'diff' and substract 'threshold' from 'filtered' */
  177. dst[x + y * dst_linesize] = orig - threshold;
  178. } else {
  179. if (-diff > 2 * threshold)
  180. dst[x + y * dst_linesize] = orig;
  181. else if (-diff > threshold)
  182. /* add 'diff' and 'threshold' to 'filtered' */
  183. dst[x + y * dst_linesize] = orig + threshold;
  184. }
  185. }
  186. }
  187. } else if (threshold < 0) {
  188. for (y = 0; y < h; ++y) {
  189. for (x = 0; x < w; ++x) {
  190. orig = src[x + y * src_linesize];
  191. filtered = dst[x + y * dst_linesize];
  192. diff = orig - filtered;
  193. if (diff > 0) {
  194. if (diff <= -threshold)
  195. dst[x + y * dst_linesize] = orig;
  196. else if (diff <= -2 * threshold)
  197. /* substract 'diff' and 'threshold' from 'orig' */
  198. dst[x + y * dst_linesize] = filtered - threshold;
  199. } else {
  200. if (diff >= threshold)
  201. dst[x + y * dst_linesize] = orig;
  202. else if (diff >= 2 * threshold)
  203. /* add 'threshold' and substract 'diff' from 'orig' */
  204. dst[x + y * dst_linesize] = filtered + threshold;
  205. }
  206. }
  207. }
  208. }
  209. }
  210. static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
  211. {
  212. SmartblurContext *sblur = inlink->dst->priv;
  213. AVFilterLink *outlink = inlink->dst->outputs[0];
  214. AVFrame *outpic;
  215. int cw = inlink->w >> sblur->hsub;
  216. int ch = inlink->h >> sblur->vsub;
  217. outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  218. if (!outpic) {
  219. av_frame_free(&inpic);
  220. return AVERROR(ENOMEM);
  221. }
  222. av_frame_copy_props(outpic, inpic);
  223. blur(outpic->data[0], outpic->linesize[0],
  224. inpic->data[0], inpic->linesize[0],
  225. inlink->w, inlink->h, sblur->luma.threshold,
  226. sblur->luma.filter_context);
  227. if (inpic->data[2]) {
  228. blur(outpic->data[1], outpic->linesize[1],
  229. inpic->data[1], inpic->linesize[1],
  230. cw, ch, sblur->chroma.threshold,
  231. sblur->chroma.filter_context);
  232. blur(outpic->data[2], outpic->linesize[2],
  233. inpic->data[2], inpic->linesize[2],
  234. cw, ch, sblur->chroma.threshold,
  235. sblur->chroma.filter_context);
  236. }
  237. av_frame_free(&inpic);
  238. return ff_filter_frame(outlink, outpic);
  239. }
  240. static const AVFilterPad smartblur_inputs[] = {
  241. {
  242. .name = "default",
  243. .type = AVMEDIA_TYPE_VIDEO,
  244. .filter_frame = filter_frame,
  245. .config_props = config_props,
  246. },
  247. { NULL }
  248. };
  249. static const AVFilterPad smartblur_outputs[] = {
  250. {
  251. .name = "default",
  252. .type = AVMEDIA_TYPE_VIDEO,
  253. },
  254. { NULL }
  255. };
  256. AVFilter avfilter_vf_smartblur = {
  257. .name = "smartblur",
  258. .description = NULL_IF_CONFIG_SMALL("Blur the input video without impacting the outlines."),
  259. .priv_size = sizeof(SmartblurContext),
  260. .init = init,
  261. .uninit = uninit,
  262. .query_formats = query_formats,
  263. .inputs = smartblur_inputs,
  264. .outputs = smartblur_outputs,
  265. .priv_class = &smartblur_class,
  266. };