vf_nlmeans_opencl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <float.h>
  19. #include "libavutil/avassert.h"
  20. #include "libavutil/common.h"
  21. #include "libavutil/imgutils.h"
  22. #include "libavutil/mem.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. #include "opencl.h"
  28. #include "opencl_source.h"
  29. #include "video.h"
  30. // TODO:
  31. // the integral image may overflow 32bit, consider using 64bit
  32. static const enum AVPixelFormat supported_formats[] = {
  33. AV_PIX_FMT_YUV420P,
  34. AV_PIX_FMT_YUV444P,
  35. AV_PIX_FMT_GBRP,
  36. };
  37. static int is_format_supported(enum AVPixelFormat fmt)
  38. {
  39. int i;
  40. for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
  41. if (supported_formats[i] == fmt)
  42. return 1;
  43. return 0;
  44. }
  45. typedef struct NLMeansOpenCLContext {
  46. OpenCLFilterContext ocf;
  47. int initialised;
  48. cl_kernel vert_kernel;
  49. cl_kernel horiz_kernel;
  50. cl_kernel accum_kernel;
  51. cl_kernel average_kernel;
  52. cl_mem integral_img;
  53. cl_mem weight;
  54. cl_mem sum;
  55. cl_mem overflow; // overflow in integral image?
  56. double sigma;
  57. float h;
  58. int chroma_w;
  59. int chroma_h;
  60. int patch_size;
  61. int patch_size_uv;
  62. int research_size;
  63. int research_size_uv;
  64. cl_command_queue command_queue;
  65. } NLMeansOpenCLContext;
  66. static int nlmeans_opencl_init(AVFilterContext *avctx, int width, int height)
  67. {
  68. NLMeansOpenCLContext *ctx = avctx->priv;
  69. cl_int cle;
  70. int err;
  71. int weight_buf_size = width * height * sizeof(float);
  72. ctx->h = ctx->sigma * 10;
  73. if (!(ctx->research_size & 1)) {
  74. ctx->research_size |= 1;
  75. av_log(avctx, AV_LOG_WARNING,
  76. "research_size should be odd, set to %d",
  77. ctx->research_size);
  78. }
  79. if (!(ctx->patch_size & 1)) {
  80. ctx->patch_size |= 1;
  81. av_log(avctx, AV_LOG_WARNING,
  82. "patch_size should be odd, set to %d",
  83. ctx->patch_size);
  84. }
  85. if (!ctx->research_size_uv)
  86. ctx->research_size_uv = ctx->research_size;
  87. if (!ctx->patch_size_uv)
  88. ctx->patch_size_uv = ctx->patch_size;
  89. err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_nlmeans, 1);
  90. if (err < 0)
  91. goto fail;
  92. ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
  93. ctx->ocf.hwctx->device_id,
  94. 0, &cle);
  95. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
  96. "command queue %d.\n", cle);
  97. ctx->vert_kernel = clCreateKernel(ctx->ocf.program,
  98. "vert_sum", &cle);
  99. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create "
  100. "vert_sum kernel %d.\n", cle);
  101. ctx->horiz_kernel = clCreateKernel(ctx->ocf.program,
  102. "horiz_sum", &cle);
  103. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create "
  104. "horiz_sum kernel %d.\n", cle);
  105. ctx->accum_kernel = clCreateKernel(ctx->ocf.program,
  106. "weight_accum", &cle);
  107. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create "
  108. "accum kernel %d.\n", cle);
  109. ctx->average_kernel = clCreateKernel(ctx->ocf.program,
  110. "average", &cle);
  111. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create "
  112. "average kernel %d.\n", cle);
  113. ctx->integral_img = clCreateBuffer(ctx->ocf.hwctx->context, 0,
  114. 4 * width * height * sizeof(cl_int),
  115. NULL, &cle);
  116. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create "
  117. "integral image %d.\n", cle);
  118. ctx->weight = clCreateBuffer(ctx->ocf.hwctx->context, 0,
  119. weight_buf_size, NULL, &cle);
  120. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create "
  121. "weight buffer %d.\n", cle);
  122. ctx->sum = clCreateBuffer(ctx->ocf.hwctx->context, 0,
  123. weight_buf_size, NULL, &cle);
  124. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create "
  125. "sum buffer %d.\n", cle);
  126. ctx->overflow = clCreateBuffer(ctx->ocf.hwctx->context, 0,
  127. sizeof(cl_int), NULL, &cle);
  128. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create "
  129. "overflow buffer %d.\n", cle);
  130. ctx->initialised = 1;
  131. return 0;
  132. fail:
  133. CL_RELEASE_KERNEL(ctx->vert_kernel);
  134. CL_RELEASE_KERNEL(ctx->horiz_kernel);
  135. CL_RELEASE_KERNEL(ctx->accum_kernel);
  136. CL_RELEASE_KERNEL(ctx->average_kernel);
  137. CL_RELEASE_MEMORY(ctx->integral_img);
  138. CL_RELEASE_MEMORY(ctx->weight);
  139. CL_RELEASE_MEMORY(ctx->sum);
  140. CL_RELEASE_MEMORY(ctx->overflow);
  141. CL_RELEASE_QUEUE(ctx->command_queue);
  142. return err;
  143. }
  144. static int nlmeans_plane(AVFilterContext *avctx, cl_mem dst, cl_mem src,
  145. cl_int width, cl_int height, cl_int p, cl_int r)
  146. {
  147. NLMeansOpenCLContext *ctx = avctx->priv;
  148. const float zero = 0.0f;
  149. const size_t worksize1[] = {height};
  150. const size_t worksize2[] = {width};
  151. const size_t worksize3[2] = {width, height};
  152. int i, dx, dy, err = 0, weight_buf_size;
  153. cl_int cle;
  154. int nb_pixel, *tmp = NULL, idx = 0;
  155. cl_int *dxdy = NULL;
  156. weight_buf_size = width * height * sizeof(float);
  157. cle = clEnqueueFillBuffer(ctx->command_queue, ctx->weight,
  158. &zero, sizeof(float), 0, weight_buf_size,
  159. 0, NULL, NULL);
  160. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to fill weight buffer: %d.\n",
  161. cle);
  162. cle = clEnqueueFillBuffer(ctx->command_queue, ctx->sum,
  163. &zero, sizeof(float), 0, weight_buf_size,
  164. 0, NULL, NULL);
  165. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to fill sum buffer: %d.\n",
  166. cle);
  167. nb_pixel = (2 * r + 1) * (2 * r + 1) - 1;
  168. dxdy = av_malloc(nb_pixel * 2 * sizeof(cl_int));
  169. tmp = av_malloc(nb_pixel * 2 * sizeof(int));
  170. if (!dxdy || !tmp)
  171. goto fail;
  172. for (dx = -r; dx <= r; dx++) {
  173. for (dy = -r; dy <= r; dy++) {
  174. if (dx || dy) {
  175. tmp[idx++] = dx;
  176. tmp[idx++] = dy;
  177. }
  178. }
  179. }
  180. // repack dx/dy seperately, as we want to do four pairs of dx/dy in a batch
  181. for (i = 0; i < nb_pixel / 4; i++) {
  182. dxdy[i * 8] = tmp[i * 8]; // dx0
  183. dxdy[i * 8 + 1] = tmp[i * 8 + 2]; // dx1
  184. dxdy[i * 8 + 2] = tmp[i * 8 + 4]; // dx2
  185. dxdy[i * 8 + 3] = tmp[i * 8 + 6]; // dx3
  186. dxdy[i * 8 + 4] = tmp[i * 8 + 1]; // dy0
  187. dxdy[i * 8 + 5] = tmp[i * 8 + 3]; // dy1
  188. dxdy[i * 8 + 6] = tmp[i * 8 + 5]; // dy2
  189. dxdy[i * 8 + 7] = tmp[i * 8 + 7]; // dy3
  190. }
  191. av_freep(&tmp);
  192. for (i = 0; i < nb_pixel / 4; i++) {
  193. cl_int *dx_cur = dxdy + 8 * i;
  194. cl_int *dy_cur = dxdy + 8 * i + 4;
  195. // horizontal pass
  196. // integral(x,y) = sum([u(v,y) - u(v+dx,y+dy)]^2) for v in [0, x]
  197. CL_SET_KERNEL_ARG(ctx->horiz_kernel, 0, cl_mem, &ctx->integral_img);
  198. CL_SET_KERNEL_ARG(ctx->horiz_kernel, 1, cl_mem, &src);
  199. CL_SET_KERNEL_ARG(ctx->horiz_kernel, 2, cl_int, &width);
  200. CL_SET_KERNEL_ARG(ctx->horiz_kernel, 3, cl_int, &height);
  201. CL_SET_KERNEL_ARG(ctx->horiz_kernel, 4, cl_int4, dx_cur);
  202. CL_SET_KERNEL_ARG(ctx->horiz_kernel, 5, cl_int4, dy_cur);
  203. cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->horiz_kernel, 1,
  204. NULL, worksize1, NULL, 0, NULL, NULL);
  205. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue horiz_kernel: %d.\n",
  206. cle);
  207. // vertical pass
  208. // integral(x, y) = sum(integral(x, v)) for v in [0, y]
  209. CL_SET_KERNEL_ARG(ctx->vert_kernel, 0, cl_mem, &ctx->integral_img);
  210. CL_SET_KERNEL_ARG(ctx->vert_kernel, 1, cl_mem, &ctx->overflow);
  211. CL_SET_KERNEL_ARG(ctx->vert_kernel, 2, cl_int, &width);
  212. CL_SET_KERNEL_ARG(ctx->vert_kernel, 3, cl_int, &height);
  213. cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->vert_kernel,
  214. 1, NULL, worksize2, NULL, 0, NULL, NULL);
  215. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue vert_kernel: %d.\n",
  216. cle);
  217. // accumulate weights
  218. CL_SET_KERNEL_ARG(ctx->accum_kernel, 0, cl_mem, &ctx->sum);
  219. CL_SET_KERNEL_ARG(ctx->accum_kernel, 1, cl_mem, &ctx->weight);
  220. CL_SET_KERNEL_ARG(ctx->accum_kernel, 2, cl_mem, &ctx->integral_img);
  221. CL_SET_KERNEL_ARG(ctx->accum_kernel, 3, cl_mem, &src);
  222. CL_SET_KERNEL_ARG(ctx->accum_kernel, 4, cl_int, &width);
  223. CL_SET_KERNEL_ARG(ctx->accum_kernel, 5, cl_int, &height);
  224. CL_SET_KERNEL_ARG(ctx->accum_kernel, 6, cl_int, &p);
  225. CL_SET_KERNEL_ARG(ctx->accum_kernel, 7, cl_float, &ctx->h);
  226. CL_SET_KERNEL_ARG(ctx->accum_kernel, 8, cl_int4, dx_cur);
  227. CL_SET_KERNEL_ARG(ctx->accum_kernel, 9, cl_int4, dy_cur);
  228. cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->accum_kernel,
  229. 2, NULL, worksize3, NULL, 0, NULL, NULL);
  230. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue kernel: %d.\n", cle);
  231. }
  232. av_freep(&dxdy);
  233. // average
  234. CL_SET_KERNEL_ARG(ctx->average_kernel, 0, cl_mem, &dst);
  235. CL_SET_KERNEL_ARG(ctx->average_kernel, 1, cl_mem, &src);
  236. CL_SET_KERNEL_ARG(ctx->average_kernel, 2, cl_mem, &ctx->sum);
  237. CL_SET_KERNEL_ARG(ctx->average_kernel, 3, cl_mem, &ctx->weight);
  238. cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->average_kernel, 2,
  239. NULL, worksize3, NULL, 0, NULL, NULL);
  240. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue average kernel: %d.\n",
  241. cle);
  242. cle = clFlush(ctx->command_queue);
  243. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to flush command queue: %d.\n", cle);
  244. fail:
  245. if (tmp)
  246. av_freep(&tmp);
  247. if (dxdy)
  248. av_freep(&dxdy);
  249. return err;
  250. }
  251. static int nlmeans_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
  252. {
  253. AVFilterContext *avctx = inlink->dst;
  254. AVFilterLink *outlink = avctx->outputs[0];
  255. NLMeansOpenCLContext *ctx = avctx->priv;
  256. AVFrame *output = NULL;
  257. AVHWFramesContext *input_frames_ctx;
  258. const AVPixFmtDescriptor *desc;
  259. enum AVPixelFormat in_format;
  260. cl_mem src, dst;
  261. const cl_int zero = 0;
  262. int w, h, err, cle, overflow, p, patch, research;
  263. av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
  264. av_get_pix_fmt_name(input->format),
  265. input->width, input->height, input->pts);
  266. if (!input->hw_frames_ctx)
  267. return AVERROR(EINVAL);
  268. input_frames_ctx = (AVHWFramesContext*)input->hw_frames_ctx->data;
  269. in_format = input_frames_ctx->sw_format;
  270. output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  271. if (!output) {
  272. err = AVERROR(ENOMEM);
  273. goto fail;
  274. }
  275. err = av_frame_copy_props(output, input);
  276. if (err < 0)
  277. goto fail;
  278. if (!ctx->initialised) {
  279. desc = av_pix_fmt_desc_get(in_format);
  280. if (!is_format_supported(in_format)) {
  281. err = AVERROR(EINVAL);
  282. av_log(avctx, AV_LOG_ERROR, "input format %s not supported\n",
  283. av_get_pix_fmt_name(in_format));
  284. goto fail;
  285. }
  286. ctx->chroma_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  287. ctx->chroma_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  288. err = nlmeans_opencl_init(avctx, inlink->w, inlink->h);
  289. if (err < 0)
  290. goto fail;
  291. }
  292. cle = clEnqueueWriteBuffer(ctx->command_queue, ctx->overflow, CL_FALSE,
  293. 0, sizeof(cl_int), &zero, 0, NULL, NULL);
  294. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to initialize overflow"
  295. "detection buffer %d.\n", cle);
  296. for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) {
  297. src = (cl_mem) input->data[p];
  298. dst = (cl_mem) output->data[p];
  299. if (!dst)
  300. break;
  301. av_assert0(src);
  302. w = p ? ctx->chroma_w : inlink->w;
  303. h = p ? ctx->chroma_h : inlink->h;
  304. patch = (p ? ctx->patch_size_uv : ctx->patch_size) / 2;
  305. research = (p ? ctx->research_size_uv : ctx->research_size) / 2;
  306. err = nlmeans_plane(avctx, dst, src, w, h, patch, research);
  307. if (err < 0)
  308. goto fail;
  309. }
  310. // overflow occurred?
  311. cle = clEnqueueReadBuffer(ctx->command_queue, ctx->overflow, CL_FALSE,
  312. 0, sizeof(cl_int), &overflow, 0, NULL, NULL);
  313. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to read overflow: %d.\n", cle);
  314. cle = clFinish(ctx->command_queue);
  315. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish kernel: %d.\n", cle);
  316. if (overflow > 0)
  317. av_log(avctx, AV_LOG_ERROR, "integral image overflow %d\n", overflow);
  318. av_frame_free(&input);
  319. av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
  320. av_get_pix_fmt_name(output->format),
  321. output->width, output->height, output->pts);
  322. return ff_filter_frame(outlink, output);
  323. fail:
  324. clFinish(ctx->command_queue);
  325. av_frame_free(&input);
  326. av_frame_free(&output);
  327. return err;
  328. }
  329. static av_cold void nlmeans_opencl_uninit(AVFilterContext *avctx)
  330. {
  331. NLMeansOpenCLContext *ctx = avctx->priv;
  332. cl_int cle;
  333. CL_RELEASE_KERNEL(ctx->vert_kernel);
  334. CL_RELEASE_KERNEL(ctx->horiz_kernel);
  335. CL_RELEASE_KERNEL(ctx->accum_kernel);
  336. CL_RELEASE_KERNEL(ctx->average_kernel);
  337. CL_RELEASE_MEMORY(ctx->integral_img);
  338. CL_RELEASE_MEMORY(ctx->weight);
  339. CL_RELEASE_MEMORY(ctx->sum);
  340. CL_RELEASE_MEMORY(ctx->overflow);
  341. CL_RELEASE_QUEUE(ctx->command_queue);
  342. ff_opencl_filter_uninit(avctx);
  343. }
  344. #define OFFSET(x) offsetof(NLMeansOpenCLContext, x)
  345. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  346. static const AVOption nlmeans_opencl_options[] = {
  347. { "s", "denoising strength", OFFSET(sigma), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, 1.0, 30.0, FLAGS },
  348. { "p", "patch size", OFFSET(patch_size), AV_OPT_TYPE_INT, { .i64 = 2*3+1 }, 0, 99, FLAGS },
  349. { "pc", "patch size for chroma planes", OFFSET(patch_size_uv), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 99, FLAGS },
  350. { "r", "research window", OFFSET(research_size), AV_OPT_TYPE_INT, { .i64 = 7*2+1 }, 0, 99, FLAGS },
  351. { "rc", "research window for chroma planes", OFFSET(research_size_uv), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 99, FLAGS },
  352. { NULL }
  353. };
  354. AVFILTER_DEFINE_CLASS(nlmeans_opencl);
  355. static const AVFilterPad nlmeans_opencl_inputs[] = {
  356. {
  357. .name = "default",
  358. .type = AVMEDIA_TYPE_VIDEO,
  359. .filter_frame = &nlmeans_opencl_filter_frame,
  360. .config_props = &ff_opencl_filter_config_input,
  361. },
  362. { NULL }
  363. };
  364. static const AVFilterPad nlmeans_opencl_outputs[] = {
  365. {
  366. .name = "default",
  367. .type = AVMEDIA_TYPE_VIDEO,
  368. .config_props = &ff_opencl_filter_config_output,
  369. },
  370. { NULL }
  371. };
  372. AVFilter ff_vf_nlmeans_opencl = {
  373. .name = "nlmeans_opencl",
  374. .description = NULL_IF_CONFIG_SMALL("Non-local means denoiser through OpenCL"),
  375. .priv_size = sizeof(NLMeansOpenCLContext),
  376. .priv_class = &nlmeans_opencl_class,
  377. .init = &ff_opencl_filter_init,
  378. .uninit = &nlmeans_opencl_uninit,
  379. .query_formats = &ff_opencl_filter_query_formats,
  380. .inputs = nlmeans_opencl_inputs,
  381. .outputs = nlmeans_opencl_outputs,
  382. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  383. };