vf_sab.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. /**
  21. * @file
  22. * Shape Adaptive Blur filter, ported from MPlayer libmpcodecs/vf_sab.c
  23. */
  24. #include "libavutil/opt.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "libswscale/swscale.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. typedef struct FilterParam {
  31. float radius;
  32. float pre_filter_radius;
  33. float strength;
  34. float quality;
  35. struct SwsContext *pre_filter_context;
  36. uint8_t *pre_filter_buf;
  37. int pre_filter_linesize;
  38. int dist_width;
  39. int dist_linesize;
  40. int *dist_coeff;
  41. #define COLOR_DIFF_COEFF_SIZE 512
  42. int color_diff_coeff[COLOR_DIFF_COEFF_SIZE];
  43. } FilterParam;
  44. typedef struct SabContext {
  45. const AVClass *class;
  46. FilterParam luma;
  47. FilterParam chroma;
  48. int hsub;
  49. int vsub;
  50. unsigned int sws_flags;
  51. } SabContext;
  52. static int query_formats(AVFilterContext *ctx)
  53. {
  54. static const enum AVPixelFormat pix_fmts[] = {
  55. AV_PIX_FMT_YUV420P,
  56. AV_PIX_FMT_YUV410P,
  57. AV_PIX_FMT_YUV444P,
  58. AV_PIX_FMT_YUV422P,
  59. AV_PIX_FMT_YUV411P,
  60. AV_PIX_FMT_NONE
  61. };
  62. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  63. if (!fmts_list)
  64. return AVERROR(ENOMEM);
  65. return ff_set_common_formats(ctx, fmts_list);
  66. }
  67. #define RADIUS_MIN 0.1
  68. #define RADIUS_MAX 4.0
  69. #define PRE_FILTER_RADIUS_MIN 0.1
  70. #define PRE_FILTER_RADIUS_MAX 2.0
  71. #define STRENGTH_MIN 0.1
  72. #define STRENGTH_MAX 100.0
  73. #define OFFSET(x) offsetof(SabContext, x)
  74. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  75. static const AVOption sab_options[] = {
  76. { "luma_radius", "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
  77. { "lr" , "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
  78. { "luma_pre_filter_radius", "set luma pre-filter radius", OFFSET(luma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, PRE_FILTER_RADIUS_MIN, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
  79. { "lpfr", "set luma pre-filter radius", OFFSET(luma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, PRE_FILTER_RADIUS_MIN, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
  80. { "luma_strength", "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
  81. { "ls", "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
  82. { "chroma_radius", "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
  83. { "cr", "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
  84. { "chroma_pre_filter_radius", "set chroma pre-filter radius", OFFSET(chroma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=PRE_FILTER_RADIUS_MIN-1},
  85. PRE_FILTER_RADIUS_MIN-1, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
  86. { "cpfr", "set chroma pre-filter radius", OFFSET(chroma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=PRE_FILTER_RADIUS_MIN-1},
  87. PRE_FILTER_RADIUS_MIN-1, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
  88. { "chroma_strength", "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
  89. { "cs", "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
  90. { NULL }
  91. };
  92. AVFILTER_DEFINE_CLASS(sab);
  93. static av_cold int init(AVFilterContext *ctx)
  94. {
  95. SabContext *s = ctx->priv;
  96. /* make chroma default to luma values, if not explicitly set */
  97. if (s->chroma.radius < RADIUS_MIN)
  98. s->chroma.radius = s->luma.radius;
  99. if (s->chroma.pre_filter_radius < PRE_FILTER_RADIUS_MIN)
  100. s->chroma.pre_filter_radius = s->luma.pre_filter_radius;
  101. if (s->chroma.strength < STRENGTH_MIN)
  102. s->chroma.strength = s->luma.strength;
  103. s->luma.quality = s->chroma.quality = 3.0;
  104. s->sws_flags = SWS_POINT;
  105. av_log(ctx, AV_LOG_VERBOSE,
  106. "luma_radius:%f luma_pre_filter_radius::%f luma_strength:%f "
  107. "chroma_radius:%f chroma_pre_filter_radius:%f chroma_strength:%f\n",
  108. s->luma .radius, s->luma .pre_filter_radius, s->luma .strength,
  109. s->chroma.radius, s->chroma.pre_filter_radius, s->chroma.strength);
  110. return 0;
  111. }
  112. static void close_filter_param(FilterParam *f)
  113. {
  114. if (f->pre_filter_context) {
  115. sws_freeContext(f->pre_filter_context);
  116. f->pre_filter_context = NULL;
  117. }
  118. av_freep(&f->pre_filter_buf);
  119. av_freep(&f->dist_coeff);
  120. }
  121. static av_cold void uninit(AVFilterContext *ctx)
  122. {
  123. SabContext *s = ctx->priv;
  124. close_filter_param(&s->luma);
  125. close_filter_param(&s->chroma);
  126. }
  127. static int open_filter_param(FilterParam *f, int width, int height, unsigned int sws_flags)
  128. {
  129. SwsVector *vec;
  130. SwsFilter sws_f;
  131. int i, x, y;
  132. int linesize = FFALIGN(width, 8);
  133. f->pre_filter_buf = av_malloc(linesize * height);
  134. if (!f->pre_filter_buf)
  135. return AVERROR(ENOMEM);
  136. f->pre_filter_linesize = linesize;
  137. vec = sws_getGaussianVec(f->pre_filter_radius, f->quality);
  138. sws_f.lumH = sws_f.lumV = vec;
  139. sws_f.chrH = sws_f.chrV = NULL;
  140. f->pre_filter_context = sws_getContext(width, height, AV_PIX_FMT_GRAY8,
  141. width, height, AV_PIX_FMT_GRAY8,
  142. sws_flags, &sws_f, NULL, NULL);
  143. sws_freeVec(vec);
  144. vec = sws_getGaussianVec(f->strength, 5.0);
  145. for (i = 0; i < COLOR_DIFF_COEFF_SIZE; i++) {
  146. double d;
  147. int index = i-COLOR_DIFF_COEFF_SIZE/2 + vec->length/2;
  148. if (index < 0 || index >= vec->length) d = 0.0;
  149. else d = vec->coeff[index];
  150. f->color_diff_coeff[i] = (int)(d/vec->coeff[vec->length/2]*(1<<12) + 0.5);
  151. }
  152. sws_freeVec(vec);
  153. vec = sws_getGaussianVec(f->radius, f->quality);
  154. f->dist_width = vec->length;
  155. f->dist_linesize = FFALIGN(vec->length, 8);
  156. f->dist_coeff = av_malloc_array(f->dist_width, f->dist_linesize * sizeof(*f->dist_coeff));
  157. if (!f->dist_coeff) {
  158. sws_freeVec(vec);
  159. return AVERROR(ENOMEM);
  160. }
  161. for (y = 0; y < vec->length; y++) {
  162. for (x = 0; x < vec->length; x++) {
  163. double d = vec->coeff[x] * vec->coeff[y];
  164. f->dist_coeff[x + y*f->dist_linesize] = (int)(d*(1<<10) + 0.5);
  165. }
  166. }
  167. sws_freeVec(vec);
  168. return 0;
  169. }
  170. static int config_props(AVFilterLink *inlink)
  171. {
  172. SabContext *s = inlink->dst->priv;
  173. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  174. int ret;
  175. s->hsub = desc->log2_chroma_w;
  176. s->vsub = desc->log2_chroma_h;
  177. close_filter_param(&s->luma);
  178. ret = open_filter_param(&s->luma, inlink->w, inlink->h, s->sws_flags);
  179. if (ret < 0)
  180. return ret;
  181. close_filter_param(&s->chroma);
  182. ret = open_filter_param(&s->chroma,
  183. AV_CEIL_RSHIFT(inlink->w, s->hsub),
  184. AV_CEIL_RSHIFT(inlink->h, s->vsub), s->sws_flags);
  185. return ret;
  186. }
  187. #define NB_PLANES 4
  188. static void blur(uint8_t *dst, const int dst_linesize,
  189. const uint8_t *src, const int src_linesize,
  190. const int w, const int h, FilterParam *fp)
  191. {
  192. int x, y;
  193. FilterParam f = *fp;
  194. const int radius = f.dist_width/2;
  195. const uint8_t * const src2[NB_PLANES] = { src };
  196. int src2_linesize[NB_PLANES] = { src_linesize };
  197. uint8_t *dst2[NB_PLANES] = { f.pre_filter_buf };
  198. int dst2_linesize[NB_PLANES] = { f.pre_filter_linesize };
  199. sws_scale(f.pre_filter_context, src2, src2_linesize, 0, h, dst2, dst2_linesize);
  200. #define UPDATE_FACTOR do { \
  201. int factor; \
  202. factor = f.color_diff_coeff[COLOR_DIFF_COEFF_SIZE/2 + pre_val - \
  203. f.pre_filter_buf[ix + iy*f.pre_filter_linesize]] * f.dist_coeff[dx + dy*f.dist_linesize]; \
  204. sum += src[ix + iy*src_linesize] * factor; \
  205. div += factor; \
  206. } while (0)
  207. for (y = 0; y < h; y++) {
  208. for (x = 0; x < w; x++) {
  209. int sum = 0;
  210. int div = 0;
  211. int dy;
  212. const int pre_val = f.pre_filter_buf[x + y*f.pre_filter_linesize];
  213. if (x >= radius && x < w - radius) {
  214. for (dy = 0; dy < radius*2 + 1; dy++) {
  215. int dx;
  216. int iy = y+dy - radius;
  217. iy = avpriv_mirror(iy, h-1);
  218. for (dx = 0; dx < radius*2 + 1; dx++) {
  219. const int ix = x+dx - radius;
  220. UPDATE_FACTOR;
  221. }
  222. }
  223. } else {
  224. for (dy = 0; dy < radius*2+1; dy++) {
  225. int dx;
  226. int iy = y+dy - radius;
  227. iy = avpriv_mirror(iy, h-1);
  228. for (dx = 0; dx < radius*2 + 1; dx++) {
  229. int ix = x+dx - radius;
  230. ix = avpriv_mirror(ix, w-1);
  231. UPDATE_FACTOR;
  232. }
  233. }
  234. }
  235. dst[x + y*dst_linesize] = (sum + div/2) / div;
  236. }
  237. }
  238. }
  239. static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
  240. {
  241. SabContext *s = inlink->dst->priv;
  242. AVFilterLink *outlink = inlink->dst->outputs[0];
  243. AVFrame *outpic;
  244. outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  245. if (!outpic) {
  246. av_frame_free(&inpic);
  247. return AVERROR(ENOMEM);
  248. }
  249. av_frame_copy_props(outpic, inpic);
  250. blur(outpic->data[0], outpic->linesize[0], inpic->data[0], inpic->linesize[0],
  251. inlink->w, inlink->h, &s->luma);
  252. if (inpic->data[2]) {
  253. int cw = AV_CEIL_RSHIFT(inlink->w, s->hsub);
  254. int ch = AV_CEIL_RSHIFT(inlink->h, s->vsub);
  255. blur(outpic->data[1], outpic->linesize[1], inpic->data[1], inpic->linesize[1], cw, ch, &s->chroma);
  256. blur(outpic->data[2], outpic->linesize[2], inpic->data[2], inpic->linesize[2], cw, ch, &s->chroma);
  257. }
  258. av_frame_free(&inpic);
  259. return ff_filter_frame(outlink, outpic);
  260. }
  261. static const AVFilterPad sab_inputs[] = {
  262. {
  263. .name = "default",
  264. .type = AVMEDIA_TYPE_VIDEO,
  265. .filter_frame = filter_frame,
  266. .config_props = config_props,
  267. },
  268. { NULL }
  269. };
  270. static const AVFilterPad sab_outputs[] = {
  271. {
  272. .name = "default",
  273. .type = AVMEDIA_TYPE_VIDEO,
  274. },
  275. { NULL }
  276. };
  277. AVFilter ff_vf_sab = {
  278. .name = "sab",
  279. .description = NULL_IF_CONFIG_SMALL("Apply shape adaptive blur."),
  280. .priv_size = sizeof(SabContext),
  281. .init = init,
  282. .uninit = uninit,
  283. .query_formats = query_formats,
  284. .inputs = sab_inputs,
  285. .outputs = sab_outputs,
  286. .priv_class = &sab_class,
  287. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  288. };