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.

340 lines
12KB

  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 {
  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 {
  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. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  63. return 0;
  64. }
  65. #define RADIUS_MIN 0.1
  66. #define RADIUS_MAX 4.0
  67. #define PRE_FILTER_RADIUS_MIN 0.1
  68. #define PRE_FILTER_RADIUS_MAX 2.0
  69. #define STRENGTH_MIN 0.1
  70. #define STRENGTH_MAX 100.0
  71. #define OFFSET(x) offsetof(SabContext, x)
  72. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  73. static const AVOption sab_options[] = {
  74. { "luma_radius", "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
  75. { "lr" , "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
  76. { "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 },
  77. { "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 },
  78. { "luma_strength", "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
  79. { "ls", "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
  80. { "chroma_radius", "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
  81. { "cr", "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
  82. { "chroma_pre_filter_radius", "set chroma pre-filter radius", OFFSET(chroma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=PRE_FILTER_RADIUS_MIN-1},
  83. PRE_FILTER_RADIUS_MIN-1, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
  84. { "cpfr", "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. { "chroma_strength", "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
  87. { "cs", "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
  88. { NULL }
  89. };
  90. AVFILTER_DEFINE_CLASS(sab);
  91. static av_cold int init(AVFilterContext *ctx)
  92. {
  93. SabContext *sab = ctx->priv;
  94. /* make chroma default to luma values, if not explicitly set */
  95. if (sab->chroma.radius < RADIUS_MIN)
  96. sab->chroma.radius = sab->luma.radius;
  97. if (sab->chroma.pre_filter_radius < PRE_FILTER_RADIUS_MIN)
  98. sab->chroma.pre_filter_radius = sab->luma.pre_filter_radius;
  99. if (sab->chroma.strength < STRENGTH_MIN)
  100. sab->chroma.strength = sab->luma.strength;
  101. sab->luma.quality = sab->chroma.quality = 3.0;
  102. sab->sws_flags = SWS_POINT;
  103. av_log(ctx, AV_LOG_VERBOSE,
  104. "luma_radius:%f luma_pre_filter_radius::%f luma_strength:%f "
  105. "chroma_radius:%f chroma_pre_filter_radius:%f chroma_strength:%f\n",
  106. sab->luma .radius, sab->luma .pre_filter_radius, sab->luma .strength,
  107. sab->chroma.radius, sab->chroma.pre_filter_radius, sab->chroma.strength);
  108. return 0;
  109. }
  110. static void close_filter_param(FilterParam *f)
  111. {
  112. if (f->pre_filter_context) {
  113. sws_freeContext(f->pre_filter_context);
  114. f->pre_filter_context = NULL;
  115. }
  116. av_freep(&f->pre_filter_buf);
  117. av_freep(&f->dist_coeff);
  118. }
  119. static av_cold void uninit(AVFilterContext *ctx)
  120. {
  121. SabContext *sab = ctx->priv;
  122. close_filter_param(&sab->luma);
  123. close_filter_param(&sab->chroma);
  124. }
  125. static int open_filter_param(FilterParam *f, int width, int height, unsigned int sws_flags)
  126. {
  127. SwsVector *vec;
  128. SwsFilter sws_f;
  129. int i, x, y;
  130. int linesize = FFALIGN(width, 8);
  131. f->pre_filter_buf = av_malloc(linesize * height);
  132. if (!f->pre_filter_buf)
  133. return AVERROR(ENOMEM);
  134. f->pre_filter_linesize = linesize;
  135. vec = sws_getGaussianVec(f->pre_filter_radius, f->quality);
  136. sws_f.lumH = sws_f.lumV = vec;
  137. sws_f.chrH = sws_f.chrV = NULL;
  138. f->pre_filter_context = sws_getContext(width, height, AV_PIX_FMT_GRAY8,
  139. width, height, AV_PIX_FMT_GRAY8,
  140. sws_flags, &sws_f, NULL, NULL);
  141. sws_freeVec(vec);
  142. vec = sws_getGaussianVec(f->strength, 5.0);
  143. for (i = 0; i < COLOR_DIFF_COEFF_SIZE; i++) {
  144. double d;
  145. int index = i-COLOR_DIFF_COEFF_SIZE/2 + vec->length/2;
  146. if (index < 0 || index >= vec->length) d = 0.0;
  147. else d = vec->coeff[index];
  148. f->color_diff_coeff[i] = (int)(d/vec->coeff[vec->length/2]*(1<<12) + 0.5);
  149. }
  150. sws_freeVec(vec);
  151. vec = sws_getGaussianVec(f->radius, f->quality);
  152. f->dist_width = vec->length;
  153. f->dist_linesize = FFALIGN(vec->length, 8);
  154. f->dist_coeff = av_malloc_array(f->dist_width, f->dist_linesize * sizeof(*f->dist_coeff));
  155. if (!f->dist_coeff) {
  156. sws_freeVec(vec);
  157. return AVERROR(ENOMEM);
  158. }
  159. for (y = 0; y < vec->length; y++) {
  160. for (x = 0; x < vec->length; x++) {
  161. double d = vec->coeff[x] * vec->coeff[y];
  162. f->dist_coeff[x + y*f->dist_linesize] = (int)(d*(1<<10) + 0.5);
  163. }
  164. }
  165. sws_freeVec(vec);
  166. return 0;
  167. }
  168. static int config_props(AVFilterLink *inlink)
  169. {
  170. SabContext *sab = inlink->dst->priv;
  171. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  172. int ret;
  173. sab->hsub = desc->log2_chroma_w;
  174. sab->vsub = desc->log2_chroma_h;
  175. close_filter_param(&sab->luma);
  176. ret = open_filter_param(&sab->luma, inlink->w, inlink->h, sab->sws_flags);
  177. if (ret < 0)
  178. return ret;
  179. close_filter_param(&sab->chroma);
  180. ret = open_filter_param(&sab->chroma,
  181. FF_CEIL_RSHIFT(inlink->w, sab->hsub),
  182. FF_CEIL_RSHIFT(inlink->h, sab->vsub), sab->sws_flags);
  183. return ret;
  184. }
  185. #define NB_PLANES 4
  186. static void blur(uint8_t *dst, const int dst_linesize,
  187. const uint8_t *src, const int src_linesize,
  188. const int w, const int h, FilterParam *fp)
  189. {
  190. int x, y;
  191. FilterParam f = *fp;
  192. const int radius = f.dist_width/2;
  193. const uint8_t * const src2[NB_PLANES] = { src };
  194. int src2_linesize[NB_PLANES] = { src_linesize };
  195. uint8_t *dst2[NB_PLANES] = { f.pre_filter_buf };
  196. int dst2_linesize[NB_PLANES] = { f.pre_filter_linesize };
  197. sws_scale(f.pre_filter_context, src2, src2_linesize, 0, h, dst2, dst2_linesize);
  198. #define UPDATE_FACTOR do { \
  199. int factor; \
  200. factor = f.color_diff_coeff[COLOR_DIFF_COEFF_SIZE/2 + pre_val - \
  201. f.pre_filter_buf[ix + iy*f.pre_filter_linesize]] * f.dist_coeff[dx + dy*f.dist_linesize]; \
  202. sum += src[ix + iy*src_linesize] * factor; \
  203. div += factor; \
  204. } while (0)
  205. for (y = 0; y < h; y++) {
  206. for (x = 0; x < w; x++) {
  207. int sum = 0;
  208. int div = 0;
  209. int dy;
  210. const int pre_val = f.pre_filter_buf[x + y*f.pre_filter_linesize];
  211. if (x >= radius && x < w - radius) {
  212. for (dy = 0; dy < radius*2 + 1; dy++) {
  213. int dx;
  214. int iy = y+dy - radius;
  215. if (iy < 0) iy = -iy;
  216. else if (iy >= h) iy = h+h-iy-1;
  217. for (dx = 0; dx < radius*2 + 1; dx++) {
  218. const int ix = x+dx - radius;
  219. UPDATE_FACTOR;
  220. }
  221. }
  222. } else {
  223. for (dy = 0; dy < radius*2+1; dy++) {
  224. int dx;
  225. int iy = y+dy - radius;
  226. if (iy < 0) iy = -iy;
  227. else if (iy >= h) iy = h+h-iy-1;
  228. for (dx = 0; dx < radius*2 + 1; dx++) {
  229. int ix = x+dx - radius;
  230. if (ix < 0) ix = -ix;
  231. else if (ix >= w) ix = w+w-ix-1;
  232. UPDATE_FACTOR;
  233. }
  234. }
  235. }
  236. dst[x + y*dst_linesize] = (sum + div/2) / div;
  237. }
  238. }
  239. }
  240. static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
  241. {
  242. SabContext *sab = inlink->dst->priv;
  243. AVFilterLink *outlink = inlink->dst->outputs[0];
  244. AVFrame *outpic;
  245. outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  246. if (!outpic) {
  247. av_frame_free(&inpic);
  248. return AVERROR(ENOMEM);
  249. }
  250. av_frame_copy_props(outpic, inpic);
  251. blur(outpic->data[0], outpic->linesize[0], inpic->data[0], inpic->linesize[0],
  252. inlink->w, inlink->h, &sab->luma);
  253. if (inpic->data[2]) {
  254. int cw = FF_CEIL_RSHIFT(inlink->w, sab->hsub);
  255. int ch = FF_CEIL_RSHIFT(inlink->h, sab->vsub);
  256. blur(outpic->data[1], outpic->linesize[1], inpic->data[1], inpic->linesize[1], cw, ch, &sab->chroma);
  257. blur(outpic->data[2], outpic->linesize[2], inpic->data[2], inpic->linesize[2], cw, ch, &sab->chroma);
  258. }
  259. av_frame_free(&inpic);
  260. return ff_filter_frame(outlink, outpic);
  261. }
  262. static const AVFilterPad sab_inputs[] = {
  263. {
  264. .name = "default",
  265. .type = AVMEDIA_TYPE_VIDEO,
  266. .filter_frame = filter_frame,
  267. .config_props = config_props,
  268. },
  269. { NULL }
  270. };
  271. static const AVFilterPad sab_outputs[] = {
  272. {
  273. .name = "default",
  274. .type = AVMEDIA_TYPE_VIDEO,
  275. },
  276. { NULL }
  277. };
  278. AVFilter ff_vf_sab = {
  279. .name = "sab",
  280. .description = NULL_IF_CONFIG_SMALL("Apply shape adaptive blur."),
  281. .priv_size = sizeof(SabContext),
  282. .init = init,
  283. .uninit = uninit,
  284. .query_formats = query_formats,
  285. .inputs = sab_inputs,
  286. .outputs = sab_outputs,
  287. .priv_class = &sab_class,
  288. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  289. };