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.

388 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 "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|AV_OPT_FLAG_RUNTIME_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 float max = (1 << s->depth) - 1;
  143. const int height = td->height;
  144. const int width = td->width;
  145. const int64_t numpixels = width * (int64_t)height;
  146. const unsigned slice_start = (numpixels * jobnr ) / nb_jobs;
  147. const unsigned slice_end = (numpixels * (jobnr+1)) / nb_jobs;
  148. const float postscale = s->postscale * s->postscaleV;
  149. float *buffer = s->buffer;
  150. unsigned i;
  151. for (i = slice_start; i < slice_end; i++) {
  152. buffer[i] *= postscale;
  153. buffer[i] = av_clipf(buffer[i], 0.f, max);
  154. }
  155. return 0;
  156. }
  157. static void gaussianiir2d(AVFilterContext *ctx, int plane)
  158. {
  159. GBlurContext *s = ctx->priv;
  160. const int width = s->planewidth[plane];
  161. const int height = s->planeheight[plane];
  162. const int nb_threads = ff_filter_get_nb_threads(ctx);
  163. ThreadData td;
  164. if (s->sigma <= 0 || s->steps < 0)
  165. return;
  166. td.width = width;
  167. td.height = height;
  168. ctx->internal->execute(ctx, filter_horizontally, &td, NULL, FFMIN(height, nb_threads));
  169. ctx->internal->execute(ctx, filter_vertically, &td, NULL, FFMIN(width, nb_threads));
  170. ctx->internal->execute(ctx, filter_postscale, &td, NULL, FFMIN(width * height, nb_threads));
  171. }
  172. static int query_formats(AVFilterContext *ctx)
  173. {
  174. static const enum AVPixelFormat pix_fmts[] = {
  175. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  176. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  177. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  178. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  179. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  180. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  181. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  182. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  183. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  184. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  185. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  186. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  187. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  188. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  189. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  190. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  191. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  192. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  193. AV_PIX_FMT_NONE
  194. };
  195. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  196. }
  197. void ff_gblur_init(GBlurContext *s)
  198. {
  199. s->horiz_slice = horiz_slice_c;
  200. if (ARCH_X86_64)
  201. ff_gblur_init_x86(s);
  202. }
  203. static int config_input(AVFilterLink *inlink)
  204. {
  205. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  206. GBlurContext *s = inlink->dst->priv;
  207. s->depth = desc->comp[0].depth;
  208. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  209. s->planewidth[0] = s->planewidth[3] = inlink->w;
  210. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  211. s->planeheight[0] = s->planeheight[3] = inlink->h;
  212. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  213. s->buffer = av_malloc_array(FFALIGN(inlink->w, 16), FFALIGN(inlink->h, 16) * sizeof(*s->buffer));
  214. if (!s->buffer)
  215. return AVERROR(ENOMEM);
  216. if (s->sigmaV < 0) {
  217. s->sigmaV = s->sigma;
  218. }
  219. ff_gblur_init(s);
  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. .process_command = ff_filter_process_command,
  339. };