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.

390 lines
13KB

  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 "internal.h"
  33. #include "video.h"
  34. typedef struct GBlurContext {
  35. const AVClass *class;
  36. float sigma;
  37. float sigmaV;
  38. int steps;
  39. int planes;
  40. int depth;
  41. int planewidth[4];
  42. int planeheight[4];
  43. float *buffer;
  44. float boundaryscale;
  45. float boundaryscaleV;
  46. float postscale;
  47. float postscaleV;
  48. float nu;
  49. float nuV;
  50. int nb_planes;
  51. } GBlurContext;
  52. #define OFFSET(x) offsetof(GBlurContext, x)
  53. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  54. static const AVOption gblur_options[] = {
  55. { "sigma", "set sigma", OFFSET(sigma), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0.0, 1024, FLAGS },
  56. { "steps", "set number of steps", OFFSET(steps), AV_OPT_TYPE_INT, {.i64=1}, 1, 6, FLAGS },
  57. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
  58. { "sigmaV", "set vertical sigma", OFFSET(sigmaV), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 1024, FLAGS },
  59. { NULL }
  60. };
  61. AVFILTER_DEFINE_CLASS(gblur);
  62. typedef struct ThreadData {
  63. int height;
  64. int width;
  65. } ThreadData;
  66. static int filter_horizontally(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  67. {
  68. GBlurContext *s = ctx->priv;
  69. ThreadData *td = arg;
  70. const int height = td->height;
  71. const int width = td->width;
  72. const int slice_start = (height * jobnr ) / nb_jobs;
  73. const int slice_end = (height * (jobnr+1)) / nb_jobs;
  74. const float boundaryscale = s->boundaryscale;
  75. const int steps = s->steps;
  76. const float nu = s->nu;
  77. float *buffer = s->buffer;
  78. int y, x, step;
  79. float *ptr;
  80. /* Filter horizontally along each row */
  81. for (y = slice_start; y < slice_end; y++) {
  82. for (step = 0; step < steps; step++) {
  83. ptr = buffer + width * y;
  84. ptr[0] *= boundaryscale;
  85. /* Filter rightwards */
  86. for (x = 1; x < width; x++)
  87. ptr[x] += nu * ptr[x - 1];
  88. ptr[x = width - 1] *= boundaryscale;
  89. /* Filter leftwards */
  90. for (; x > 0; x--)
  91. ptr[x - 1] += nu * ptr[x];
  92. }
  93. }
  94. return 0;
  95. }
  96. static void do_vertical_columns(float *buffer, int width, int height,
  97. int column_begin, int column_end, int steps,
  98. float nu, float boundaryscale, int column_step)
  99. {
  100. const int numpixels = width * height;
  101. int i, x, k, step;
  102. float *ptr;
  103. for (x = column_begin; x < column_end;) {
  104. for (step = 0; step < steps; step++) {
  105. ptr = buffer + x;
  106. for (k = 0; k < column_step; k++) {
  107. ptr[k] *= boundaryscale;
  108. }
  109. /* Filter downwards */
  110. for (i = width; i < numpixels; i += width) {
  111. for (k = 0; k < column_step; k++) {
  112. ptr[i + k] += nu * ptr[i - width + k];
  113. }
  114. }
  115. i = numpixels - width;
  116. for (k = 0; k < column_step; k++)
  117. ptr[i + k] *= boundaryscale;
  118. /* Filter upwards */
  119. for (; i > 0; i -= width) {
  120. for (k = 0; k < column_step; k++)
  121. ptr[i - width + k] += nu * ptr[i + k];
  122. }
  123. }
  124. x += column_step;
  125. }
  126. }
  127. static int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  128. {
  129. GBlurContext *s = ctx->priv;
  130. ThreadData *td = arg;
  131. const int height = td->height;
  132. const int width = td->width;
  133. const int slice_start = (width * jobnr ) / nb_jobs;
  134. const int slice_end = (width * (jobnr+1)) / nb_jobs;
  135. const float boundaryscale = s->boundaryscaleV;
  136. const int steps = s->steps;
  137. const float nu = s->nuV;
  138. float *buffer = s->buffer;
  139. int aligned_end;
  140. aligned_end = slice_start + (((slice_end - slice_start) >> 3) << 3);
  141. /* Filter vertically along columns (process 8 columns in each step) */
  142. do_vertical_columns(buffer, width, height, slice_start, aligned_end,
  143. steps, nu, boundaryscale, 8);
  144. /* Filter un-aligned columns one by one */
  145. do_vertical_columns(buffer, width, height, aligned_end, slice_end,
  146. steps, nu, boundaryscale, 1);
  147. return 0;
  148. }
  149. static int filter_postscale(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  150. {
  151. GBlurContext *s = ctx->priv;
  152. ThreadData *td = arg;
  153. const int height = td->height;
  154. const int width = td->width;
  155. const int64_t numpixels = width * (int64_t)height;
  156. const unsigned slice_start = (numpixels * jobnr ) / nb_jobs;
  157. const unsigned slice_end = (numpixels * (jobnr+1)) / nb_jobs;
  158. const float postscale = s->postscale * s->postscaleV;
  159. float *buffer = s->buffer;
  160. unsigned i;
  161. for (i = slice_start; i < slice_end; i++)
  162. buffer[i] *= postscale;
  163. return 0;
  164. }
  165. static void gaussianiir2d(AVFilterContext *ctx, int plane)
  166. {
  167. GBlurContext *s = ctx->priv;
  168. const int width = s->planewidth[plane];
  169. const int height = s->planeheight[plane];
  170. const int nb_threads = ff_filter_get_nb_threads(ctx);
  171. ThreadData td;
  172. if (s->sigma <= 0 || s->steps < 0)
  173. return;
  174. td.width = width;
  175. td.height = height;
  176. ctx->internal->execute(ctx, filter_horizontally, &td, NULL, FFMIN(height, nb_threads));
  177. ctx->internal->execute(ctx, filter_vertically, &td, NULL, FFMIN(width, nb_threads));
  178. ctx->internal->execute(ctx, filter_postscale, &td, NULL, FFMIN(width * height, nb_threads));
  179. }
  180. static int query_formats(AVFilterContext *ctx)
  181. {
  182. static const enum AVPixelFormat pix_fmts[] = {
  183. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  184. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  185. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  186. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  187. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  188. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  189. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  190. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  191. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  192. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  193. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  194. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  195. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  196. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  197. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  198. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  199. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  200. AV_PIX_FMT_NONE
  201. };
  202. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  203. }
  204. static int config_input(AVFilterLink *inlink)
  205. {
  206. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  207. GBlurContext *s = inlink->dst->priv;
  208. s->depth = desc->comp[0].depth;
  209. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  210. s->planewidth[0] = s->planewidth[3] = inlink->w;
  211. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  212. s->planeheight[0] = s->planeheight[3] = inlink->h;
  213. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  214. s->buffer = av_malloc_array(inlink->w, inlink->h * sizeof(*s->buffer));
  215. if (!s->buffer)
  216. return AVERROR(ENOMEM);
  217. if (s->sigmaV < 0) {
  218. s->sigmaV = s->sigma;
  219. }
  220. return 0;
  221. }
  222. static void set_params(float sigma, int steps, float *postscale, float *boundaryscale, float *nu)
  223. {
  224. double dnu, lambda;
  225. lambda = (sigma * sigma) / (2.0 * steps);
  226. dnu = (1.0 + 2.0 * lambda - sqrt(1.0 + 4.0 * lambda)) / (2.0 * lambda);
  227. *postscale = pow(dnu / lambda, steps);
  228. *boundaryscale = 1.0 / (1.0 - dnu);
  229. *nu = (float)dnu;
  230. }
  231. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  232. {
  233. AVFilterContext *ctx = inlink->dst;
  234. GBlurContext *s = ctx->priv;
  235. AVFilterLink *outlink = ctx->outputs[0];
  236. AVFrame *out;
  237. int plane;
  238. set_params(s->sigma, s->steps, &s->postscale, &s->boundaryscale, &s->nu);
  239. set_params(s->sigmaV, s->steps, &s->postscaleV, &s->boundaryscaleV, &s->nuV);
  240. if (av_frame_is_writable(in)) {
  241. out = in;
  242. } else {
  243. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  244. if (!out) {
  245. av_frame_free(&in);
  246. return AVERROR(ENOMEM);
  247. }
  248. av_frame_copy_props(out, in);
  249. }
  250. for (plane = 0; plane < s->nb_planes; plane++) {
  251. const int height = s->planeheight[plane];
  252. const int width = s->planewidth[plane];
  253. float *bptr = s->buffer;
  254. const uint8_t *src = in->data[plane];
  255. const uint16_t *src16 = (const uint16_t *)in->data[plane];
  256. uint8_t *dst = out->data[plane];
  257. uint16_t *dst16 = (uint16_t *)out->data[plane];
  258. int y, x;
  259. if (!s->sigma || !(s->planes & (1 << plane))) {
  260. if (out != in)
  261. av_image_copy_plane(out->data[plane], out->linesize[plane],
  262. in->data[plane], in->linesize[plane],
  263. width * ((s->depth + 7) / 8), height);
  264. continue;
  265. }
  266. if (s->depth == 8) {
  267. for (y = 0; y < height; y++) {
  268. for (x = 0; x < width; x++) {
  269. bptr[x] = src[x];
  270. }
  271. bptr += width;
  272. src += in->linesize[plane];
  273. }
  274. } else {
  275. for (y = 0; y < height; y++) {
  276. for (x = 0; x < width; x++) {
  277. bptr[x] = src16[x];
  278. }
  279. bptr += width;
  280. src16 += in->linesize[plane] / 2;
  281. }
  282. }
  283. gaussianiir2d(ctx, plane);
  284. bptr = s->buffer;
  285. if (s->depth == 8) {
  286. for (y = 0; y < height; y++) {
  287. for (x = 0; x < width; x++) {
  288. dst[x] = bptr[x];
  289. }
  290. bptr += width;
  291. dst += out->linesize[plane];
  292. }
  293. } else {
  294. for (y = 0; y < height; y++) {
  295. for (x = 0; x < width; x++) {
  296. dst16[x] = bptr[x];
  297. }
  298. bptr += width;
  299. dst16 += out->linesize[plane] / 2;
  300. }
  301. }
  302. }
  303. if (out != in)
  304. av_frame_free(&in);
  305. return ff_filter_frame(outlink, out);
  306. }
  307. static av_cold void uninit(AVFilterContext *ctx)
  308. {
  309. GBlurContext *s = ctx->priv;
  310. av_freep(&s->buffer);
  311. }
  312. static const AVFilterPad gblur_inputs[] = {
  313. {
  314. .name = "default",
  315. .type = AVMEDIA_TYPE_VIDEO,
  316. .config_props = config_input,
  317. .filter_frame = filter_frame,
  318. },
  319. { NULL }
  320. };
  321. static const AVFilterPad gblur_outputs[] = {
  322. {
  323. .name = "default",
  324. .type = AVMEDIA_TYPE_VIDEO,
  325. },
  326. { NULL }
  327. };
  328. AVFilter ff_vf_gblur = {
  329. .name = "gblur",
  330. .description = NULL_IF_CONFIG_SMALL("Apply Gaussian Blur filter."),
  331. .priv_size = sizeof(GBlurContext),
  332. .priv_class = &gblur_class,
  333. .uninit = uninit,
  334. .query_formats = query_formats,
  335. .inputs = gblur_inputs,
  336. .outputs = gblur_outputs,
  337. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  338. };