vf_unsharp_opencl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 "libavutil/common.h"
  19. #include "libavutil/imgutils.h"
  20. #include "libavutil/mem.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "avfilter.h"
  24. #include "internal.h"
  25. #include "opencl.h"
  26. #include "opencl_source.h"
  27. #include "video.h"
  28. #define MAX_DIAMETER 23
  29. typedef struct UnsharpOpenCLContext {
  30. OpenCLFilterContext ocf;
  31. int initialised;
  32. cl_kernel kernel;
  33. cl_command_queue command_queue;
  34. float luma_size_x;
  35. float luma_size_y;
  36. float luma_amount;
  37. float chroma_size_x;
  38. float chroma_size_y;
  39. float chroma_amount;
  40. int global;
  41. int nb_planes;
  42. struct {
  43. float blur_x[MAX_DIAMETER];
  44. float blur_y[MAX_DIAMETER];
  45. cl_mem matrix;
  46. cl_mem coef_x;
  47. cl_mem coef_y;
  48. cl_int size_x;
  49. cl_int size_y;
  50. cl_float amount;
  51. cl_float threshold;
  52. } plane[4];
  53. } UnsharpOpenCLContext;
  54. static int unsharp_opencl_init(AVFilterContext *avctx)
  55. {
  56. UnsharpOpenCLContext *ctx = avctx->priv;
  57. cl_int cle;
  58. int err;
  59. err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_unsharp, 1);
  60. if (err < 0)
  61. goto fail;
  62. ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
  63. ctx->ocf.hwctx->device_id,
  64. 0, &cle);
  65. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
  66. "command queue %d.\n", cle);
  67. // Use global kernel if mask size will be too big for the local store..
  68. ctx->global = (ctx->luma_size_x > 17.0f ||
  69. ctx->luma_size_y > 17.0f ||
  70. ctx->chroma_size_x > 17.0f ||
  71. ctx->chroma_size_y > 17.0f);
  72. ctx->kernel = clCreateKernel(ctx->ocf.program,
  73. ctx->global ? "unsharp_global"
  74. : "unsharp_local", &cle);
  75. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create kernel %d.\n", cle);
  76. ctx->initialised = 1;
  77. return 0;
  78. fail:
  79. if (ctx->command_queue)
  80. clReleaseCommandQueue(ctx->command_queue);
  81. if (ctx->kernel)
  82. clReleaseKernel(ctx->kernel);
  83. return err;
  84. }
  85. static int unsharp_opencl_make_filter_params(AVFilterContext *avctx)
  86. {
  87. UnsharpOpenCLContext *ctx = avctx->priv;
  88. const AVPixFmtDescriptor *desc;
  89. float *matrix;
  90. double val, sum;
  91. cl_int cle;
  92. cl_mem buffer;
  93. size_t matrix_bytes;
  94. float diam_x, diam_y, amount;
  95. int err, p, x, y, size_x, size_y;
  96. desc = av_pix_fmt_desc_get(ctx->ocf.output_format);
  97. ctx->nb_planes = 0;
  98. for (p = 0; p < desc->nb_components; p++)
  99. ctx->nb_planes = FFMAX(ctx->nb_planes, desc->comp[p].plane + 1);
  100. for (p = 0; p < ctx->nb_planes; p++) {
  101. if (p == 0 || (desc->flags & AV_PIX_FMT_FLAG_RGB)) {
  102. diam_x = ctx->luma_size_x;
  103. diam_y = ctx->luma_size_y;
  104. amount = ctx->luma_amount;
  105. } else {
  106. diam_x = ctx->chroma_size_x;
  107. diam_y = ctx->chroma_size_y;
  108. amount = ctx->chroma_amount;
  109. }
  110. size_x = (int)ceil(diam_x) | 1;
  111. size_y = (int)ceil(diam_y) | 1;
  112. matrix_bytes = size_x * size_y * sizeof(float);
  113. matrix = av_malloc(matrix_bytes);
  114. if (!matrix) {
  115. err = AVERROR(ENOMEM);
  116. goto fail;
  117. }
  118. sum = 0.0;
  119. for (x = 0; x < size_x; x++) {
  120. double dx = (double)(x - size_x / 2) / diam_x;
  121. sum += ctx->plane[p].blur_x[x] = exp(-16.0 * (dx * dx));
  122. }
  123. for (x = 0; x < size_x; x++)
  124. ctx->plane[p].blur_x[x] /= sum;
  125. sum = 0.0;
  126. for (y = 0; y < size_y; y++) {
  127. double dy = (double)(y - size_y / 2) / diam_y;
  128. sum += ctx->plane[p].blur_y[y] = exp(-16.0 * (dy * dy));
  129. }
  130. for (y = 0; y < size_y; y++)
  131. ctx->plane[p].blur_y[y] /= sum;
  132. for (y = 0; y < size_y; y++) {
  133. for (x = 0; x < size_x; x++) {
  134. val = ctx->plane[p].blur_x[x] * ctx->plane[p].blur_y[y];
  135. matrix[y * size_x + x] = val;
  136. }
  137. }
  138. if (ctx->global) {
  139. buffer = clCreateBuffer(ctx->ocf.hwctx->context,
  140. CL_MEM_READ_ONLY |
  141. CL_MEM_COPY_HOST_PTR |
  142. CL_MEM_HOST_NO_ACCESS,
  143. matrix_bytes, matrix, &cle);
  144. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create matrix buffer: "
  145. "%d.\n", cle);
  146. ctx->plane[p].matrix = buffer;
  147. } else {
  148. buffer = clCreateBuffer(ctx->ocf.hwctx->context,
  149. CL_MEM_READ_ONLY |
  150. CL_MEM_COPY_HOST_PTR |
  151. CL_MEM_HOST_NO_ACCESS,
  152. sizeof(ctx->plane[p].blur_x),
  153. ctx->plane[p].blur_x, &cle);
  154. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create x-coef buffer: "
  155. "%d.\n", cle);
  156. ctx->plane[p].coef_x = buffer;
  157. buffer = clCreateBuffer(ctx->ocf.hwctx->context,
  158. CL_MEM_READ_ONLY |
  159. CL_MEM_COPY_HOST_PTR |
  160. CL_MEM_HOST_NO_ACCESS,
  161. sizeof(ctx->plane[p].blur_y),
  162. ctx->plane[p].blur_y, &cle);
  163. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create y-coef buffer: "
  164. "%d.\n", cle);
  165. ctx->plane[p].coef_y = buffer;
  166. }
  167. av_freep(&matrix);
  168. ctx->plane[p].size_x = size_x;
  169. ctx->plane[p].size_y = size_y;
  170. ctx->plane[p].amount = amount;
  171. }
  172. err = 0;
  173. fail:
  174. av_freep(&matrix);
  175. return err;
  176. }
  177. static int unsharp_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
  178. {
  179. AVFilterContext *avctx = inlink->dst;
  180. AVFilterLink *outlink = avctx->outputs[0];
  181. UnsharpOpenCLContext *ctx = avctx->priv;
  182. AVFrame *output = NULL;
  183. cl_int cle;
  184. size_t global_work[2];
  185. size_t local_work[2];
  186. cl_mem src, dst;
  187. int err, p;
  188. av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
  189. av_get_pix_fmt_name(input->format),
  190. input->width, input->height, input->pts);
  191. if (!input->hw_frames_ctx)
  192. return AVERROR(EINVAL);
  193. if (!ctx->initialised) {
  194. err = unsharp_opencl_init(avctx);
  195. if (err < 0)
  196. goto fail;
  197. err = unsharp_opencl_make_filter_params(avctx);
  198. if (err < 0)
  199. goto fail;
  200. }
  201. output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  202. if (!output) {
  203. err = AVERROR(ENOMEM);
  204. goto fail;
  205. }
  206. for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) {
  207. src = (cl_mem) input->data[p];
  208. dst = (cl_mem)output->data[p];
  209. if (!dst)
  210. break;
  211. CL_SET_KERNEL_ARG(ctx->kernel, 0, cl_mem, &dst);
  212. CL_SET_KERNEL_ARG(ctx->kernel, 1, cl_mem, &src);
  213. CL_SET_KERNEL_ARG(ctx->kernel, 2, cl_int, &ctx->plane[p].size_x);
  214. CL_SET_KERNEL_ARG(ctx->kernel, 3, cl_int, &ctx->plane[p].size_y);
  215. CL_SET_KERNEL_ARG(ctx->kernel, 4, cl_float, &ctx->plane[p].amount);
  216. if (ctx->global) {
  217. CL_SET_KERNEL_ARG(ctx->kernel, 5, cl_mem, &ctx->plane[p].matrix);
  218. } else {
  219. CL_SET_KERNEL_ARG(ctx->kernel, 5, cl_mem, &ctx->plane[p].coef_x);
  220. CL_SET_KERNEL_ARG(ctx->kernel, 6, cl_mem, &ctx->plane[p].coef_y);
  221. }
  222. err = ff_opencl_filter_work_size_from_image(avctx, global_work, output, p,
  223. ctx->global ? 0 : 16);
  224. if (err < 0)
  225. goto fail;
  226. local_work[0] = 16;
  227. local_work[1] = 16;
  228. av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
  229. "(%"SIZE_SPECIFIER"x%"SIZE_SPECIFIER").\n",
  230. p, global_work[0], global_work[1]);
  231. cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
  232. global_work, ctx->global ? NULL : local_work,
  233. 0, NULL, NULL);
  234. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue kernel: %d.\n", cle);
  235. }
  236. cle = clFinish(ctx->command_queue);
  237. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
  238. err = av_frame_copy_props(output, input);
  239. if (err < 0)
  240. goto fail;
  241. av_frame_free(&input);
  242. av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
  243. av_get_pix_fmt_name(output->format),
  244. output->width, output->height, output->pts);
  245. return ff_filter_frame(outlink, output);
  246. fail:
  247. clFinish(ctx->command_queue);
  248. av_frame_free(&input);
  249. av_frame_free(&output);
  250. return err;
  251. }
  252. static av_cold void unsharp_opencl_uninit(AVFilterContext *avctx)
  253. {
  254. UnsharpOpenCLContext *ctx = avctx->priv;
  255. cl_int cle;
  256. int i;
  257. for (i = 0; i < ctx->nb_planes; i++) {
  258. if (ctx->plane[i].matrix)
  259. clReleaseMemObject(ctx->plane[i].matrix);
  260. if (ctx->plane[i].coef_x)
  261. clReleaseMemObject(ctx->plane[i].coef_x);
  262. if (ctx->plane[i].coef_y)
  263. clReleaseMemObject(ctx->plane[i].coef_y);
  264. }
  265. if (ctx->kernel) {
  266. cle = clReleaseKernel(ctx->kernel);
  267. if (cle != CL_SUCCESS)
  268. av_log(avctx, AV_LOG_ERROR, "Failed to release "
  269. "kernel: %d.\n", cle);
  270. }
  271. if (ctx->command_queue) {
  272. cle = clReleaseCommandQueue(ctx->command_queue);
  273. if (cle != CL_SUCCESS)
  274. av_log(avctx, AV_LOG_ERROR, "Failed to release "
  275. "command queue: %d.\n", cle);
  276. }
  277. ff_opencl_filter_uninit(avctx);
  278. }
  279. #define OFFSET(x) offsetof(UnsharpOpenCLContext, x)
  280. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  281. static const AVOption unsharp_opencl_options[] = {
  282. { "luma_msize_x", "Set luma mask horizontal diameter (pixels)",
  283. OFFSET(luma_size_x), AV_OPT_TYPE_FLOAT,
  284. { .dbl = 5.0 }, 1, MAX_DIAMETER, FLAGS },
  285. { "lx", "Set luma mask horizontal diameter (pixels)",
  286. OFFSET(luma_size_x), AV_OPT_TYPE_FLOAT,
  287. { .dbl = 5.0 }, 1, MAX_DIAMETER, FLAGS },
  288. { "luma_msize_y", "Set luma mask vertical diameter (pixels)",
  289. OFFSET(luma_size_y), AV_OPT_TYPE_FLOAT,
  290. { .dbl = 5.0 }, 1, MAX_DIAMETER, FLAGS },
  291. { "ly", "Set luma mask vertical diameter (pixels)",
  292. OFFSET(luma_size_y), AV_OPT_TYPE_FLOAT,
  293. { .dbl = 5.0 }, 1, MAX_DIAMETER, FLAGS },
  294. { "luma_amount", "Set luma amount (multiplier)",
  295. OFFSET(luma_amount), AV_OPT_TYPE_FLOAT,
  296. { .dbl = 1.0 }, -10, 10, FLAGS },
  297. { "la", "Set luma amount (multiplier)",
  298. OFFSET(luma_amount), AV_OPT_TYPE_FLOAT,
  299. { .dbl = 1.0 }, -10, 10, FLAGS },
  300. { "chroma_msize_x", "Set chroma mask horizontal diameter (pixels after subsampling)",
  301. OFFSET(chroma_size_x), AV_OPT_TYPE_FLOAT,
  302. { .dbl = 5.0 }, 1, MAX_DIAMETER, FLAGS },
  303. { "cx", "Set chroma mask horizontal diameter (pixels after subsampling)",
  304. OFFSET(chroma_size_x), AV_OPT_TYPE_FLOAT,
  305. { .dbl = 5.0 }, 1, MAX_DIAMETER, FLAGS },
  306. { "chroma_msize_y", "Set chroma mask vertical diameter (pixels after subsampling)",
  307. OFFSET(chroma_size_y), AV_OPT_TYPE_FLOAT,
  308. { .dbl = 5.0 }, 1, MAX_DIAMETER, FLAGS },
  309. { "cy", "Set chroma mask vertical diameter (pixels after subsampling)",
  310. OFFSET(chroma_size_y), AV_OPT_TYPE_FLOAT,
  311. { .dbl = 5.0 }, 1, MAX_DIAMETER, FLAGS },
  312. { "chroma_amount", "Set chroma amount (multiplier)",
  313. OFFSET(chroma_amount), AV_OPT_TYPE_FLOAT,
  314. { .dbl = 0.0 }, -10, 10, FLAGS },
  315. { "ca", "Set chroma amount (multiplier)",
  316. OFFSET(chroma_amount), AV_OPT_TYPE_FLOAT,
  317. { .dbl = 0.0 }, -10, 10, FLAGS },
  318. { NULL }
  319. };
  320. AVFILTER_DEFINE_CLASS(unsharp_opencl);
  321. static const AVFilterPad unsharp_opencl_inputs[] = {
  322. {
  323. .name = "default",
  324. .type = AVMEDIA_TYPE_VIDEO,
  325. .filter_frame = &unsharp_opencl_filter_frame,
  326. .config_props = &ff_opencl_filter_config_input,
  327. },
  328. { NULL }
  329. };
  330. static const AVFilterPad unsharp_opencl_outputs[] = {
  331. {
  332. .name = "default",
  333. .type = AVMEDIA_TYPE_VIDEO,
  334. .config_props = &ff_opencl_filter_config_output,
  335. },
  336. { NULL }
  337. };
  338. AVFilter ff_vf_unsharp_opencl = {
  339. .name = "unsharp_opencl",
  340. .description = NULL_IF_CONFIG_SMALL("Apply unsharp mask to input video"),
  341. .priv_size = sizeof(UnsharpOpenCLContext),
  342. .priv_class = &unsharp_opencl_class,
  343. .init = &ff_opencl_filter_init,
  344. .uninit = &unsharp_opencl_uninit,
  345. .query_formats = &ff_opencl_filter_query_formats,
  346. .inputs = unsharp_opencl_inputs,
  347. .outputs = unsharp_opencl_outputs,
  348. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  349. };