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.

368 lines
12KB

  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 int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  97. {
  98. GBlurContext *s = ctx->priv;
  99. ThreadData *td = arg;
  100. const int height = td->height;
  101. const int width = td->width;
  102. const int slice_start = (width * jobnr ) / nb_jobs;
  103. const int slice_end = (width * (jobnr+1)) / nb_jobs;
  104. const float boundaryscale = s->boundaryscaleV;
  105. const int numpixels = width * height;
  106. const int steps = s->steps;
  107. const float nu = s->nuV;
  108. float *buffer = s->buffer;
  109. int i, x, step;
  110. float *ptr;
  111. /* Filter vertically along each column */
  112. for (x = slice_start; x < slice_end; x++) {
  113. for (step = 0; step < steps; step++) {
  114. ptr = buffer + x;
  115. ptr[0] *= boundaryscale;
  116. /* Filter downwards */
  117. for (i = width; i < numpixels; i += width)
  118. ptr[i] += nu * ptr[i - width];
  119. ptr[i = numpixels - width] *= boundaryscale;
  120. /* Filter upwards */
  121. for (; i > 0; i -= width)
  122. ptr[i - width] += nu * ptr[i];
  123. }
  124. }
  125. return 0;
  126. }
  127. static int filter_postscale(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 int64_t numpixels = width * (int64_t)height;
  134. const unsigned slice_start = (numpixels * jobnr ) / nb_jobs;
  135. const unsigned slice_end = (numpixels * (jobnr+1)) / nb_jobs;
  136. const float postscale = s->postscale * s->postscaleV;
  137. float *buffer = s->buffer;
  138. unsigned i;
  139. for (i = slice_start; i < slice_end; i++)
  140. buffer[i] *= postscale;
  141. return 0;
  142. }
  143. static void gaussianiir2d(AVFilterContext *ctx, int plane)
  144. {
  145. GBlurContext *s = ctx->priv;
  146. const int width = s->planewidth[plane];
  147. const int height = s->planeheight[plane];
  148. const int nb_threads = ff_filter_get_nb_threads(ctx);
  149. ThreadData td;
  150. if (s->sigma <= 0 || s->steps < 0)
  151. return;
  152. td.width = width;
  153. td.height = height;
  154. ctx->internal->execute(ctx, filter_horizontally, &td, NULL, FFMIN(height, nb_threads));
  155. ctx->internal->execute(ctx, filter_vertically, &td, NULL, FFMIN(width, nb_threads));
  156. ctx->internal->execute(ctx, filter_postscale, &td, NULL, FFMIN(width * height, nb_threads));
  157. }
  158. static int query_formats(AVFilterContext *ctx)
  159. {
  160. static const enum AVPixelFormat pix_fmts[] = {
  161. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  162. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  163. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  164. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  165. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  166. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  167. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  168. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  169. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  170. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  171. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  172. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  173. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  174. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  175. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  176. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  177. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
  178. AV_PIX_FMT_NONE
  179. };
  180. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  181. }
  182. static int config_input(AVFilterLink *inlink)
  183. {
  184. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  185. GBlurContext *s = inlink->dst->priv;
  186. s->depth = desc->comp[0].depth;
  187. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  188. s->planewidth[0] = s->planewidth[3] = inlink->w;
  189. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  190. s->planeheight[0] = s->planeheight[3] = inlink->h;
  191. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  192. s->buffer = av_malloc_array(inlink->w, inlink->h * sizeof(*s->buffer));
  193. if (!s->buffer)
  194. return AVERROR(ENOMEM);
  195. if (s->sigmaV < 0) {
  196. s->sigmaV = s->sigma;
  197. }
  198. return 0;
  199. }
  200. static void set_params(float sigma, int steps, float *postscale, float *boundaryscale, float *nu)
  201. {
  202. double dnu, lambda;
  203. lambda = (sigma * sigma) / (2.0 * steps);
  204. dnu = (1.0 + 2.0 * lambda - sqrt(1.0 + 4.0 * lambda)) / (2.0 * lambda);
  205. *postscale = pow(dnu / lambda, steps);
  206. *boundaryscale = 1.0 / (1.0 - dnu);
  207. *nu = (float)dnu;
  208. }
  209. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  210. {
  211. AVFilterContext *ctx = inlink->dst;
  212. GBlurContext *s = ctx->priv;
  213. AVFilterLink *outlink = ctx->outputs[0];
  214. AVFrame *out;
  215. int plane;
  216. set_params(s->sigma, s->steps, &s->postscale, &s->boundaryscale, &s->nu);
  217. set_params(s->sigmaV, s->steps, &s->postscaleV, &s->boundaryscaleV, &s->nuV);
  218. if (av_frame_is_writable(in)) {
  219. out = in;
  220. } else {
  221. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  222. if (!out) {
  223. av_frame_free(&in);
  224. return AVERROR(ENOMEM);
  225. }
  226. av_frame_copy_props(out, in);
  227. }
  228. for (plane = 0; plane < s->nb_planes; plane++) {
  229. const int height = s->planeheight[plane];
  230. const int width = s->planewidth[plane];
  231. float *bptr = s->buffer;
  232. const uint8_t *src = in->data[plane];
  233. const uint16_t *src16 = (const uint16_t *)in->data[plane];
  234. uint8_t *dst = out->data[plane];
  235. uint16_t *dst16 = (uint16_t *)out->data[plane];
  236. int y, x;
  237. if (!s->sigma || !(s->planes & (1 << plane))) {
  238. if (out != in)
  239. av_image_copy_plane(out->data[plane], out->linesize[plane],
  240. in->data[plane], in->linesize[plane],
  241. width * ((s->depth + 7) / 8), height);
  242. continue;
  243. }
  244. if (s->depth == 8) {
  245. for (y = 0; y < height; y++) {
  246. for (x = 0; x < width; x++) {
  247. bptr[x] = src[x];
  248. }
  249. bptr += width;
  250. src += in->linesize[plane];
  251. }
  252. } else {
  253. for (y = 0; y < height; y++) {
  254. for (x = 0; x < width; x++) {
  255. bptr[x] = src16[x];
  256. }
  257. bptr += width;
  258. src16 += in->linesize[plane] / 2;
  259. }
  260. }
  261. gaussianiir2d(ctx, plane);
  262. bptr = s->buffer;
  263. if (s->depth == 8) {
  264. for (y = 0; y < height; y++) {
  265. for (x = 0; x < width; x++) {
  266. dst[x] = bptr[x];
  267. }
  268. bptr += width;
  269. dst += out->linesize[plane];
  270. }
  271. } else {
  272. for (y = 0; y < height; y++) {
  273. for (x = 0; x < width; x++) {
  274. dst16[x] = bptr[x];
  275. }
  276. bptr += width;
  277. dst16 += out->linesize[plane] / 2;
  278. }
  279. }
  280. }
  281. if (out != in)
  282. av_frame_free(&in);
  283. return ff_filter_frame(outlink, out);
  284. }
  285. static av_cold void uninit(AVFilterContext *ctx)
  286. {
  287. GBlurContext *s = ctx->priv;
  288. av_freep(&s->buffer);
  289. }
  290. static const AVFilterPad gblur_inputs[] = {
  291. {
  292. .name = "default",
  293. .type = AVMEDIA_TYPE_VIDEO,
  294. .config_props = config_input,
  295. .filter_frame = filter_frame,
  296. },
  297. { NULL }
  298. };
  299. static const AVFilterPad gblur_outputs[] = {
  300. {
  301. .name = "default",
  302. .type = AVMEDIA_TYPE_VIDEO,
  303. },
  304. { NULL }
  305. };
  306. AVFilter ff_vf_gblur = {
  307. .name = "gblur",
  308. .description = NULL_IF_CONFIG_SMALL("Apply Gaussian Blur filter."),
  309. .priv_size = sizeof(GBlurContext),
  310. .priv_class = &gblur_class,
  311. .uninit = uninit,
  312. .query_formats = query_formats,
  313. .inputs = gblur_inputs,
  314. .outputs = gblur_outputs,
  315. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  316. };