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.

305 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)
  72. {
  73. SmartblurContext *sblur = ctx->priv;
  74. /* make chroma default to luma values, if not explicitly set */
  75. if (sblur->chroma.radius < RADIUS_MIN)
  76. sblur->chroma.radius = sblur->luma.radius;
  77. if (sblur->chroma.strength < STRENGTH_MIN)
  78. sblur->chroma.strength = sblur->luma.strength;
  79. if (sblur->chroma.threshold < THRESHOLD_MIN)
  80. sblur->chroma.threshold = sblur->luma.threshold;
  81. sblur->luma.quality = sblur->chroma.quality = 3.0;
  82. sblur->sws_flags = SWS_BICUBIC;
  83. av_log(ctx, AV_LOG_VERBOSE,
  84. "luma_radius:%f luma_strength:%f luma_threshold:%d "
  85. "chroma_radius:%f chroma_strength:%f chroma_threshold:%d\n",
  86. sblur->luma.radius, sblur->luma.strength, sblur->luma.threshold,
  87. sblur->chroma.radius, sblur->chroma.strength, sblur->chroma.threshold);
  88. return 0;
  89. }
  90. static av_cold void uninit(AVFilterContext *ctx)
  91. {
  92. SmartblurContext *sblur = ctx->priv;
  93. sws_freeContext(sblur->luma.filter_context);
  94. sws_freeContext(sblur->chroma.filter_context);
  95. }
  96. static int query_formats(AVFilterContext *ctx)
  97. {
  98. static const enum AVPixelFormat pix_fmts[] = {
  99. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  100. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  101. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  102. AV_PIX_FMT_GRAY8,
  103. AV_PIX_FMT_NONE
  104. };
  105. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  106. return 0;
  107. }
  108. static int alloc_sws_context(FilterParam *f, int width, int height, unsigned int flags)
  109. {
  110. SwsVector *vec;
  111. SwsFilter sws_filter;
  112. vec = sws_getGaussianVec(f->radius, f->quality);
  113. if (!vec)
  114. return AVERROR(EINVAL);
  115. sws_scaleVec(vec, f->strength);
  116. vec->coeff[vec->length / 2] += 1.0 - f->strength;
  117. sws_filter.lumH = sws_filter.lumV = vec;
  118. sws_filter.chrH = sws_filter.chrV = NULL;
  119. f->filter_context = sws_getCachedContext(NULL,
  120. width, height, AV_PIX_FMT_GRAY8,
  121. width, height, AV_PIX_FMT_GRAY8,
  122. flags, &sws_filter, NULL, NULL);
  123. sws_freeVec(vec);
  124. if (!f->filter_context)
  125. return AVERROR(EINVAL);
  126. return 0;
  127. }
  128. static int config_props(AVFilterLink *inlink)
  129. {
  130. SmartblurContext *sblur = inlink->dst->priv;
  131. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  132. sblur->hsub = desc->log2_chroma_w;
  133. sblur->vsub = desc->log2_chroma_h;
  134. alloc_sws_context(&sblur->luma, inlink->w, inlink->h, sblur->sws_flags);
  135. alloc_sws_context(&sblur->chroma,
  136. inlink->w >> sblur->hsub, inlink->h >> sblur->vsub,
  137. sblur->sws_flags);
  138. return 0;
  139. }
  140. static void blur(uint8_t *dst, const int dst_linesize,
  141. const uint8_t *src, const int src_linesize,
  142. const int w, const int h, const int threshold,
  143. struct SwsContext *filter_context)
  144. {
  145. int x, y;
  146. int orig, filtered;
  147. int diff;
  148. /* Declare arrays of 4 to get aligned data */
  149. const uint8_t* const src_array[4] = {src};
  150. uint8_t *dst_array[4] = {dst};
  151. int src_linesize_array[4] = {src_linesize};
  152. int dst_linesize_array[4] = {dst_linesize};
  153. sws_scale(filter_context, src_array, src_linesize_array,
  154. 0, h, dst_array, dst_linesize_array);
  155. if (threshold > 0) {
  156. for (y = 0; y < h; ++y) {
  157. for (x = 0; x < w; ++x) {
  158. orig = src[x + y * src_linesize];
  159. filtered = dst[x + y * dst_linesize];
  160. diff = orig - filtered;
  161. if (diff > 0) {
  162. if (diff > 2 * threshold)
  163. dst[x + y * dst_linesize] = orig;
  164. else if (diff > threshold)
  165. /* add 'diff' and substract 'threshold' from 'filtered' */
  166. dst[x + y * dst_linesize] = orig - threshold;
  167. } else {
  168. if (-diff > 2 * threshold)
  169. dst[x + y * dst_linesize] = orig;
  170. else if (-diff > threshold)
  171. /* add 'diff' and 'threshold' to 'filtered' */
  172. dst[x + y * dst_linesize] = orig + threshold;
  173. }
  174. }
  175. }
  176. } else if (threshold < 0) {
  177. for (y = 0; y < h; ++y) {
  178. for (x = 0; x < w; ++x) {
  179. orig = src[x + y * src_linesize];
  180. filtered = dst[x + y * dst_linesize];
  181. diff = orig - filtered;
  182. if (diff > 0) {
  183. if (diff <= -threshold)
  184. dst[x + y * dst_linesize] = orig;
  185. else if (diff <= -2 * threshold)
  186. /* substract 'diff' and 'threshold' from 'orig' */
  187. dst[x + y * dst_linesize] = filtered - threshold;
  188. } else {
  189. if (diff >= threshold)
  190. dst[x + y * dst_linesize] = orig;
  191. else if (diff >= 2 * threshold)
  192. /* add 'threshold' and substract 'diff' from 'orig' */
  193. dst[x + y * dst_linesize] = filtered + threshold;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
  200. {
  201. SmartblurContext *sblur = inlink->dst->priv;
  202. AVFilterLink *outlink = inlink->dst->outputs[0];
  203. AVFrame *outpic;
  204. int cw = inlink->w >> sblur->hsub;
  205. int ch = inlink->h >> sblur->vsub;
  206. outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  207. if (!outpic) {
  208. av_frame_free(&inpic);
  209. return AVERROR(ENOMEM);
  210. }
  211. av_frame_copy_props(outpic, inpic);
  212. blur(outpic->data[0], outpic->linesize[0],
  213. inpic->data[0], inpic->linesize[0],
  214. inlink->w, inlink->h, sblur->luma.threshold,
  215. sblur->luma.filter_context);
  216. if (inpic->data[2]) {
  217. blur(outpic->data[1], outpic->linesize[1],
  218. inpic->data[1], inpic->linesize[1],
  219. cw, ch, sblur->chroma.threshold,
  220. sblur->chroma.filter_context);
  221. blur(outpic->data[2], outpic->linesize[2],
  222. inpic->data[2], inpic->linesize[2],
  223. cw, ch, sblur->chroma.threshold,
  224. sblur->chroma.filter_context);
  225. }
  226. av_frame_free(&inpic);
  227. return ff_filter_frame(outlink, outpic);
  228. }
  229. static const AVFilterPad smartblur_inputs[] = {
  230. {
  231. .name = "default",
  232. .type = AVMEDIA_TYPE_VIDEO,
  233. .filter_frame = filter_frame,
  234. .config_props = config_props,
  235. },
  236. { NULL }
  237. };
  238. static const AVFilterPad smartblur_outputs[] = {
  239. {
  240. .name = "default",
  241. .type = AVMEDIA_TYPE_VIDEO,
  242. },
  243. { NULL }
  244. };
  245. AVFilter avfilter_vf_smartblur = {
  246. .name = "smartblur",
  247. .description = NULL_IF_CONFIG_SMALL("Blur the input video without impacting the outlines."),
  248. .priv_size = sizeof(SmartblurContext),
  249. .init = init,
  250. .uninit = uninit,
  251. .query_formats = query_formats,
  252. .inputs = smartblur_inputs,
  253. .outputs = smartblur_outputs,
  254. .priv_class = &smartblur_class,
  255. };