vf_gblur.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright (c) 2011 Pascal Getreuer
  3. * Copyright (c) 2016 Paul B Mahol
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following
  12. * disclaimer in the documentation and/or other materials provided
  13. * with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. * HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "gblur.h"
  33. #include "internal.h"
  34. #include "video.h"
  35. #define OFFSET(x) offsetof(GBlurContext, x)
  36. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  37. static const AVOption gblur_options[] = {
  38. { "sigma", "set sigma", OFFSET(sigma), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0.0, 1024, FLAGS },
  39. { "steps", "set number of steps", OFFSET(steps), AV_OPT_TYPE_INT, {.i64=1}, 1, 6, FLAGS },
  40. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
  41. { "sigmaV", "set vertical sigma", OFFSET(sigmaV), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 1024, FLAGS },
  42. { NULL }
  43. };
  44. AVFILTER_DEFINE_CLASS(gblur);
  45. typedef struct ThreadData {
  46. int height;
  47. int width;
  48. } ThreadData;
  49. static void horiz_slice_c(float *buffer, int width, int height, int steps,
  50. float nu, float bscale)
  51. {
  52. int step, x, y;
  53. float *ptr;
  54. for (y = 0; y < height; y++) {
  55. for (step = 0; step < steps; step++) {
  56. ptr = buffer + width * y;
  57. ptr[0] *= bscale;
  58. /* Filter rightwards */
  59. for (x = 1; x < width; x++)
  60. ptr[x] += nu * ptr[x - 1];
  61. ptr[x = width - 1] *= bscale;
  62. /* Filter leftwards */
  63. for (; x > 0; x--)
  64. ptr[x - 1] += nu * ptr[x];
  65. }
  66. }
  67. }
  68. static int filter_horizontally(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  69. {
  70. GBlurContext *s = ctx->priv;
  71. ThreadData *td = arg;
  72. const int height = td->height;
  73. const int width = td->width;
  74. const int slice_start = (height * jobnr ) / nb_jobs;
  75. const int slice_end = (height * (jobnr+1)) / nb_jobs;
  76. const float boundaryscale = s->boundaryscale;
  77. const int steps = s->steps;
  78. const float nu = s->nu;
  79. float *buffer = s->buffer;
  80. s->horiz_slice(buffer + width * slice_start, width, slice_end - slice_start,
  81. steps, nu, boundaryscale);
  82. emms_c();
  83. return 0;
  84. }
  85. static void do_vertical_columns(float *buffer, int width, int height,
  86. int column_begin, int column_end, int steps,
  87. float nu, float boundaryscale, int column_step)
  88. {
  89. const int numpixels = width * height;
  90. int i, x, k, step;
  91. float *ptr;
  92. for (x = column_begin; x < column_end;) {
  93. for (step = 0; step < steps; step++) {
  94. ptr = buffer + x;
  95. for (k = 0; k < column_step; k++) {
  96. ptr[k] *= boundaryscale;
  97. }
  98. /* Filter downwards */
  99. for (i = width; i < numpixels; i += width) {
  100. for (k = 0; k < column_step; k++) {
  101. ptr[i + k] += nu * ptr[i - width + k];
  102. }
  103. }
  104. i = numpixels - width;
  105. for (k = 0; k < column_step; k++)
  106. ptr[i + k] *= boundaryscale;
  107. /* Filter upwards */
  108. for (; i > 0; i -= width) {
  109. for (k = 0; k < column_step; k++)
  110. ptr[i - width + k] += nu * ptr[i + k];
  111. }
  112. }
  113. x += column_step;
  114. }
  115. }
  116. static int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  117. {
  118. GBlurContext *s = ctx->priv;
  119. ThreadData *td = arg;
  120. const int height = td->height;
  121. const int width = td->width;
  122. const int slice_start = (width * jobnr ) / nb_jobs;
  123. const int slice_end = (width * (jobnr+1)) / nb_jobs;
  124. const float boundaryscale = s->boundaryscaleV;
  125. const int steps = s->steps;
  126. const float nu = s->nuV;
  127. float *buffer = s->buffer;
  128. int aligned_end;
  129. aligned_end = slice_start + (((slice_end - slice_start) >> 3) << 3);
  130. /* Filter vertically along columns (process 8 columns in each step) */
  131. do_vertical_columns(buffer, width, height, slice_start, aligned_end,
  132. steps, nu, boundaryscale, 8);
  133. /* Filter un-aligned columns one by one */
  134. do_vertical_columns(buffer, width, height, aligned_end, slice_end,
  135. steps, nu, boundaryscale, 1);
  136. return 0;
  137. }
  138. static int filter_postscale(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  139. {
  140. GBlurContext *s = ctx->priv;
  141. ThreadData *td = arg;
  142. const int height = td->height;
  143. const int width = td->width;
  144. const int64_t numpixels = width * (int64_t)height;
  145. const unsigned slice_start = (numpixels * jobnr ) / nb_jobs;
  146. const unsigned slice_end = (numpixels * (jobnr+1)) / nb_jobs;
  147. const float postscale = s->postscale * s->postscaleV;
  148. float *buffer = s->buffer;
  149. unsigned i;
  150. for (i = slice_start; i < slice_end; i++)
  151. buffer[i] *= postscale;
  152. return 0;
  153. }
  154. static void gaussianiir2d(AVFilterContext *ctx, int plane)
  155. {
  156. GBlurContext *s = ctx->priv;
  157. const int width = s->planewidth[plane];
  158. const int height = s->planeheight[plane];
  159. const int nb_threads = ff_filter_get_nb_threads(ctx);
  160. ThreadData td;
  161. if (s->sigma <= 0 || s->steps < 0)
  162. return;
  163. td.width = width;
  164. td.height = height;
  165. ctx->internal->execute(ctx, filter_horizontally, &td, NULL, FFMIN(height, nb_threads));
  166. ctx->internal->execute(ctx, filter_vertically, &td, NULL, FFMIN(width, nb_threads));
  167. ctx->internal->execute(ctx, filter_postscale, &td, NULL, FFMIN(width * height, nb_threads));
  168. }
  169. static int query_formats(AVFilterContext *ctx)
  170. {
  171. static const enum AVPixelFormat pix_fmts[] = {
  172. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  173. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  174. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  175. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  176. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  177. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  178. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  179. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  180. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  181. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  182. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  183. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  184. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  185. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  186. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  187. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  188. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  189. AV_PIX_FMT_NONE
  190. };
  191. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  192. }
  193. void ff_gblur_init(GBlurContext *s)
  194. {
  195. s->horiz_slice = horiz_slice_c;
  196. if (ARCH_X86_64)
  197. ff_gblur_init_x86(s);
  198. }
  199. static int config_input(AVFilterLink *inlink)
  200. {
  201. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  202. GBlurContext *s = inlink->dst->priv;
  203. s->depth = desc->comp[0].depth;
  204. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  205. s->planewidth[0] = s->planewidth[3] = inlink->w;
  206. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  207. s->planeheight[0] = s->planeheight[3] = inlink->h;
  208. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  209. s->buffer = av_malloc_array(inlink->w, inlink->h * sizeof(*s->buffer));
  210. if (!s->buffer)
  211. return AVERROR(ENOMEM);
  212. if (s->sigmaV < 0) {
  213. s->sigmaV = s->sigma;
  214. }
  215. ff_gblur_init(s);
  216. return 0;
  217. }
  218. static void set_params(float sigma, int steps, float *postscale, float *boundaryscale, float *nu)
  219. {
  220. double dnu, lambda;
  221. lambda = (sigma * sigma) / (2.0 * steps);
  222. dnu = (1.0 + 2.0 * lambda - sqrt(1.0 + 4.0 * lambda)) / (2.0 * lambda);
  223. *postscale = pow(dnu / lambda, steps);
  224. *boundaryscale = 1.0 / (1.0 - dnu);
  225. *nu = (float)dnu;
  226. }
  227. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  228. {
  229. AVFilterContext *ctx = inlink->dst;
  230. GBlurContext *s = ctx->priv;
  231. AVFilterLink *outlink = ctx->outputs[0];
  232. AVFrame *out;
  233. int plane;
  234. set_params(s->sigma, s->steps, &s->postscale, &s->boundaryscale, &s->nu);
  235. set_params(s->sigmaV, s->steps, &s->postscaleV, &s->boundaryscaleV, &s->nuV);
  236. if (av_frame_is_writable(in)) {
  237. out = in;
  238. } else {
  239. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  240. if (!out) {
  241. av_frame_free(&in);
  242. return AVERROR(ENOMEM);
  243. }
  244. av_frame_copy_props(out, in);
  245. }
  246. for (plane = 0; plane < s->nb_planes; plane++) {
  247. const int height = s->planeheight[plane];
  248. const int width = s->planewidth[plane];
  249. float *bptr = s->buffer;
  250. const uint8_t *src = in->data[plane];
  251. const uint16_t *src16 = (const uint16_t *)in->data[plane];
  252. uint8_t *dst = out->data[plane];
  253. uint16_t *dst16 = (uint16_t *)out->data[plane];
  254. int y, x;
  255. if (!s->sigma || !(s->planes & (1 << plane))) {
  256. if (out != in)
  257. av_image_copy_plane(out->data[plane], out->linesize[plane],
  258. in->data[plane], in->linesize[plane],
  259. width * ((s->depth + 7) / 8), height);
  260. continue;
  261. }
  262. if (s->depth == 8) {
  263. for (y = 0; y < height; y++) {
  264. for (x = 0; x < width; x++) {
  265. bptr[x] = src[x];
  266. }
  267. bptr += width;
  268. src += in->linesize[plane];
  269. }
  270. } else {
  271. for (y = 0; y < height; y++) {
  272. for (x = 0; x < width; x++) {
  273. bptr[x] = src16[x];
  274. }
  275. bptr += width;
  276. src16 += in->linesize[plane] / 2;
  277. }
  278. }
  279. gaussianiir2d(ctx, plane);
  280. bptr = s->buffer;
  281. if (s->depth == 8) {
  282. for (y = 0; y < height; y++) {
  283. for (x = 0; x < width; x++) {
  284. dst[x] = bptr[x];
  285. }
  286. bptr += width;
  287. dst += out->linesize[plane];
  288. }
  289. } else {
  290. for (y = 0; y < height; y++) {
  291. for (x = 0; x < width; x++) {
  292. dst16[x] = bptr[x];
  293. }
  294. bptr += width;
  295. dst16 += out->linesize[plane] / 2;
  296. }
  297. }
  298. }
  299. if (out != in)
  300. av_frame_free(&in);
  301. return ff_filter_frame(outlink, out);
  302. }
  303. static av_cold void uninit(AVFilterContext *ctx)
  304. {
  305. GBlurContext *s = ctx->priv;
  306. av_freep(&s->buffer);
  307. }
  308. static const AVFilterPad gblur_inputs[] = {
  309. {
  310. .name = "default",
  311. .type = AVMEDIA_TYPE_VIDEO,
  312. .config_props = config_input,
  313. .filter_frame = filter_frame,
  314. },
  315. { NULL }
  316. };
  317. static const AVFilterPad gblur_outputs[] = {
  318. {
  319. .name = "default",
  320. .type = AVMEDIA_TYPE_VIDEO,
  321. },
  322. { NULL }
  323. };
  324. AVFilter ff_vf_gblur = {
  325. .name = "gblur",
  326. .description = NULL_IF_CONFIG_SMALL("Apply Gaussian Blur filter."),
  327. .priv_size = sizeof(GBlurContext),
  328. .priv_class = &gblur_class,
  329. .uninit = uninit,
  330. .query_formats = query_formats,
  331. .inputs = gblur_inputs,
  332. .outputs = gblur_outputs,
  333. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  334. };