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.

408 lines
14KB

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