vf_convolution_opencl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright (c) 2018 Danil Iashchenko
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  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 "libavutil/avstring.h"
  26. #include "avfilter.h"
  27. #include "internal.h"
  28. #include "opencl.h"
  29. #include "opencl_source.h"
  30. #include "video.h"
  31. typedef struct ConvolutionOpenCLContext {
  32. OpenCLFilterContext ocf;
  33. int initialised;
  34. cl_kernel kernel;
  35. cl_command_queue command_queue;
  36. char *matrix_str[4];
  37. cl_mem matrix[4];
  38. cl_int matrix_sizes[4];
  39. cl_int dims[4];
  40. cl_float rdivs[4];
  41. cl_float biases[4];
  42. cl_int planes;
  43. cl_float scale;
  44. cl_float delta;
  45. } ConvolutionOpenCLContext;
  46. static int convolution_opencl_init(AVFilterContext *avctx)
  47. {
  48. ConvolutionOpenCLContext *ctx = avctx->priv;
  49. const char *kernel_name;
  50. cl_int cle;
  51. int err;
  52. err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_convolution, 1);
  53. if (err < 0)
  54. goto fail;
  55. ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
  56. ctx->ocf.hwctx->device_id,
  57. 0, &cle);
  58. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
  59. "command queue %d.\n", cle);
  60. if (!strcmp(avctx->filter->name, "convolution_opencl")) {
  61. kernel_name = "convolution_global";
  62. } else if (!strcmp(avctx->filter->name, "sobel_opencl")) {
  63. kernel_name = "sobel_global";
  64. } else if (!strcmp(avctx->filter->name, "prewitt_opencl")){
  65. kernel_name = "prewitt_global";
  66. } else if (!strcmp(avctx->filter->name, "roberts_opencl")){
  67. kernel_name = "roberts_global";
  68. }
  69. ctx->kernel = clCreateKernel(ctx->ocf.program, kernel_name, &cle);
  70. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create "
  71. "kernel %d.\n", cle);
  72. ctx->initialised = 1;
  73. return 0;
  74. fail:
  75. if (ctx->command_queue)
  76. clReleaseCommandQueue(ctx->command_queue);
  77. if (ctx->kernel)
  78. clReleaseKernel(ctx->kernel);
  79. return err;
  80. }
  81. static int convolution_opencl_make_filter_params(AVFilterContext *avctx)
  82. {
  83. ConvolutionOpenCLContext *ctx = avctx->priv;
  84. float *matrix = NULL;
  85. size_t matrix_bytes;
  86. cl_mem buffer;
  87. cl_int cle;
  88. int i, j;
  89. int sscanf_err;
  90. char *p, *arg, *saveptr = NULL;
  91. float input_matrix[4][49];
  92. for (i = 0; i < 4; i++) {
  93. ctx->biases[i] = ctx->biases[i] / 255.0;
  94. }
  95. for (i = 0; i < 4; i++) {
  96. p = ctx->matrix_str[i];
  97. while (ctx->matrix_sizes[i] < 49) {
  98. arg = av_strtok(p, " ", &saveptr);
  99. if (!arg) {
  100. break;
  101. }
  102. p = NULL;
  103. sscanf_err = sscanf(arg, "%f", &input_matrix[i][ctx->matrix_sizes[i]]);
  104. if (sscanf_err != 1) {
  105. av_log(ctx, AV_LOG_ERROR, "Matrix is sequence of 9, 25 or 49 signed numbers\n");
  106. return AVERROR(EINVAL);
  107. }
  108. ctx->matrix_sizes[i]++;
  109. }
  110. if (ctx->matrix_sizes[i] == 9) {
  111. ctx->dims[i] = 3;
  112. } else if (ctx->matrix_sizes[i] == 25) {
  113. ctx->dims[i] = 5;
  114. } else if (ctx->matrix_sizes[i] == 49) {
  115. ctx->dims[i] = 7;
  116. } else {
  117. av_log(ctx, AV_LOG_ERROR, "Invalid matrix size:%d\n", ctx->matrix_sizes[i]);
  118. return AVERROR(EINVAL);
  119. }
  120. }
  121. for (j = 0; j < 4; j++) {
  122. matrix_bytes = sizeof(float)*ctx->matrix_sizes[j];
  123. matrix = av_malloc(matrix_bytes);
  124. if (!matrix) {
  125. av_freep(&matrix);
  126. return AVERROR(ENOMEM);
  127. }
  128. for (i = 0; i < ctx->matrix_sizes[j]; i++)
  129. matrix[i] = input_matrix[j][i];
  130. buffer = clCreateBuffer(ctx->ocf.hwctx->context,
  131. CL_MEM_READ_ONLY |
  132. CL_MEM_COPY_HOST_PTR |
  133. CL_MEM_HOST_NO_ACCESS,
  134. matrix_bytes, matrix, &cle);
  135. if (!buffer) {
  136. av_log(avctx, AV_LOG_ERROR, "Failed to create matrix buffer: "
  137. "%d.\n", cle);
  138. av_freep(&matrix);
  139. return AVERROR(EIO);
  140. }
  141. ctx->matrix[j] = buffer;
  142. av_freep(&matrix);
  143. }
  144. return 0;
  145. }
  146. static int convolution_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
  147. {
  148. AVFilterContext *avctx = inlink->dst;
  149. AVFilterLink *outlink = avctx->outputs[0];
  150. ConvolutionOpenCLContext *ctx = avctx->priv;
  151. AVFrame *output = NULL;
  152. cl_int cle;
  153. size_t global_work[2];
  154. cl_mem src, dst;
  155. int err, p;
  156. size_t origin[3] = {0, 0, 0};
  157. size_t region[3] = {0, 0, 1};
  158. av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
  159. av_get_pix_fmt_name(input->format),
  160. input->width, input->height, input->pts);
  161. if (!input->hw_frames_ctx)
  162. return AVERROR(EINVAL);
  163. if (!ctx->initialised) {
  164. err = convolution_opencl_init(avctx);
  165. if (err < 0)
  166. goto fail;
  167. if (!strcmp(avctx->filter->name, "convolution_opencl")) {
  168. err = convolution_opencl_make_filter_params(avctx);
  169. if (err < 0)
  170. goto fail;
  171. } else {
  172. ctx->delta /= 255.0;
  173. }
  174. }
  175. output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  176. if (!output) {
  177. err = AVERROR(ENOMEM);
  178. goto fail;
  179. }
  180. for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) {
  181. src = (cl_mem) input->data[p];
  182. dst = (cl_mem)output->data[p];
  183. if (!dst)
  184. break;
  185. if (!strcmp(avctx->filter->name, "convolution_opencl")) {
  186. CL_SET_KERNEL_ARG(ctx->kernel, 0, cl_mem, &dst);
  187. CL_SET_KERNEL_ARG(ctx->kernel, 1, cl_mem, &src);
  188. CL_SET_KERNEL_ARG(ctx->kernel, 2, cl_int, &ctx->dims[p]);
  189. CL_SET_KERNEL_ARG(ctx->kernel, 3, cl_mem, &ctx->matrix[p]);
  190. CL_SET_KERNEL_ARG(ctx->kernel, 4, cl_float, &ctx->rdivs[p]);
  191. CL_SET_KERNEL_ARG(ctx->kernel, 5, cl_float, &ctx->biases[p]);
  192. err = ff_opencl_filter_work_size_from_image(avctx, global_work, output, p, 0);
  193. if (err < 0)
  194. goto fail;
  195. av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
  196. "(%"SIZE_SPECIFIER"x%"SIZE_SPECIFIER").\n",
  197. p, global_work[0], global_work[1]);
  198. cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
  199. global_work, NULL,
  200. 0, NULL, NULL);
  201. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue "
  202. "kernel: %d.\n", cle);
  203. } else {
  204. if (!(ctx->planes & (1 << p))) {
  205. err = ff_opencl_filter_work_size_from_image(avctx, region, output, p, 0);
  206. if (err < 0)
  207. goto fail;
  208. cle = clEnqueueCopyImage(ctx->command_queue, src, dst,
  209. origin, origin, region, 0, NULL, NULL);
  210. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to copy plane %d: %d.\n",
  211. p, cle);
  212. } else {
  213. CL_SET_KERNEL_ARG(ctx->kernel, 0, cl_mem, &dst);
  214. CL_SET_KERNEL_ARG(ctx->kernel, 1, cl_mem, &src);
  215. CL_SET_KERNEL_ARG(ctx->kernel, 2, cl_float, &ctx->scale);
  216. CL_SET_KERNEL_ARG(ctx->kernel, 3, cl_float, &ctx->delta);
  217. err = ff_opencl_filter_work_size_from_image(avctx, global_work, output, p, 0);
  218. if (err < 0)
  219. goto fail;
  220. av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
  221. "(%"SIZE_SPECIFIER"x%"SIZE_SPECIFIER").\n",
  222. p, global_work[0], global_work[1]);
  223. cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
  224. global_work, NULL,
  225. 0, NULL, NULL);
  226. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue "
  227. "kernel: %d.\n", cle);
  228. }
  229. }
  230. }
  231. cle = clFinish(ctx->command_queue);
  232. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
  233. err = av_frame_copy_props(output, input);
  234. if (err < 0)
  235. goto fail;
  236. av_frame_free(&input);
  237. av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
  238. av_get_pix_fmt_name(output->format),
  239. output->width, output->height, output->pts);
  240. return ff_filter_frame(outlink, output);
  241. fail:
  242. clFinish(ctx->command_queue);
  243. av_frame_free(&input);
  244. av_frame_free(&output);
  245. return err;
  246. }
  247. static av_cold void convolution_opencl_uninit(AVFilterContext *avctx)
  248. {
  249. ConvolutionOpenCLContext *ctx = avctx->priv;
  250. cl_int cle;
  251. int i;
  252. for (i = 0; i < 4; i++) {
  253. clReleaseMemObject(ctx->matrix[i]);
  254. }
  255. if (ctx->kernel) {
  256. cle = clReleaseKernel(ctx->kernel);
  257. if (cle != CL_SUCCESS)
  258. av_log(avctx, AV_LOG_ERROR, "Failed to release "
  259. "kernel: %d.\n", cle);
  260. }
  261. if (ctx->command_queue) {
  262. cle = clReleaseCommandQueue(ctx->command_queue);
  263. if (cle != CL_SUCCESS)
  264. av_log(avctx, AV_LOG_ERROR, "Failed to release "
  265. "command queue: %d.\n", cle);
  266. }
  267. ff_opencl_filter_uninit(avctx);
  268. }
  269. static const AVFilterPad convolution_opencl_inputs[] = {
  270. {
  271. .name = "default",
  272. .type = AVMEDIA_TYPE_VIDEO,
  273. .filter_frame = &convolution_opencl_filter_frame,
  274. .config_props = &ff_opencl_filter_config_input,
  275. },
  276. { NULL }
  277. };
  278. static const AVFilterPad convolution_opencl_outputs[] = {
  279. {
  280. .name = "default",
  281. .type = AVMEDIA_TYPE_VIDEO,
  282. .config_props = &ff_opencl_filter_config_output,
  283. },
  284. { NULL }
  285. };
  286. #define OFFSET(x) offsetof(ConvolutionOpenCLContext, x)
  287. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  288. #if CONFIG_CONVOLUTION_OPENCL_FILTER
  289. static const AVOption convolution_opencl_options[] = {
  290. { "0m", "set matrix for 2nd plane", OFFSET(matrix_str[0]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
  291. { "1m", "set matrix for 2nd plane", OFFSET(matrix_str[1]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
  292. { "2m", "set matrix for 3rd plane", OFFSET(matrix_str[2]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
  293. { "3m", "set matrix for 4th plane", OFFSET(matrix_str[3]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
  294. { "0rdiv", "set rdiv for 1nd plane", OFFSET(rdivs[0]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
  295. { "1rdiv", "set rdiv for 2nd plane", OFFSET(rdivs[1]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
  296. { "2rdiv", "set rdiv for 3rd plane", OFFSET(rdivs[2]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
  297. { "3rdiv", "set rdiv for 4th plane", OFFSET(rdivs[3]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
  298. { "0bias", "set bias for 1st plane", OFFSET(biases[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  299. { "1bias", "set bias for 2nd plane", OFFSET(biases[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  300. { "2bias", "set bias for 3rd plane", OFFSET(biases[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  301. { "3bias", "set bias for 4th plane", OFFSET(biases[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  302. { NULL }
  303. };
  304. AVFILTER_DEFINE_CLASS(convolution_opencl);
  305. AVFilter ff_vf_convolution_opencl = {
  306. .name = "convolution_opencl",
  307. .description = NULL_IF_CONFIG_SMALL("Apply convolution mask to input video"),
  308. .priv_size = sizeof(ConvolutionOpenCLContext),
  309. .priv_class = &convolution_opencl_class,
  310. .init = &ff_opencl_filter_init,
  311. .uninit = &convolution_opencl_uninit,
  312. .query_formats = &ff_opencl_filter_query_formats,
  313. .inputs = convolution_opencl_inputs,
  314. .outputs = convolution_opencl_outputs,
  315. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  316. };
  317. #endif /* CONFIG_CONVOLUTION_OPENCL_FILTER */
  318. #if CONFIG_SOBEL_OPENCL_FILTER
  319. static const AVOption sobel_opencl_options[] = {
  320. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
  321. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
  322. { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
  323. { NULL }
  324. };
  325. AVFILTER_DEFINE_CLASS(sobel_opencl);
  326. AVFilter ff_vf_sobel_opencl = {
  327. .name = "sobel_opencl",
  328. .description = NULL_IF_CONFIG_SMALL("Apply sobel operator"),
  329. .priv_size = sizeof(ConvolutionOpenCLContext),
  330. .priv_class = &sobel_opencl_class,
  331. .init = &ff_opencl_filter_init,
  332. .uninit = &convolution_opencl_uninit,
  333. .query_formats = &ff_opencl_filter_query_formats,
  334. .inputs = convolution_opencl_inputs,
  335. .outputs = convolution_opencl_outputs,
  336. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  337. };
  338. #endif /* CONFIG_SOBEL_OPENCL_FILTER */
  339. #if CONFIG_PREWITT_OPENCL_FILTER
  340. static const AVOption prewitt_opencl_options[] = {
  341. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
  342. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
  343. { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
  344. { NULL }
  345. };
  346. AVFILTER_DEFINE_CLASS(prewitt_opencl);
  347. AVFilter ff_vf_prewitt_opencl = {
  348. .name = "prewitt_opencl",
  349. .description = NULL_IF_CONFIG_SMALL("Apply prewitt operator"),
  350. .priv_size = sizeof(ConvolutionOpenCLContext),
  351. .priv_class = &prewitt_opencl_class,
  352. .init = &ff_opencl_filter_init,
  353. .uninit = &convolution_opencl_uninit,
  354. .query_formats = &ff_opencl_filter_query_formats,
  355. .inputs = convolution_opencl_inputs,
  356. .outputs = convolution_opencl_outputs,
  357. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  358. };
  359. #endif /* CONFIG_PREWITT_OPENCL_FILTER */
  360. #if CONFIG_ROBERTS_OPENCL_FILTER
  361. static const AVOption roberts_opencl_options[] = {
  362. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
  363. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
  364. { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
  365. { NULL }
  366. };
  367. AVFILTER_DEFINE_CLASS(roberts_opencl);
  368. AVFilter ff_vf_roberts_opencl = {
  369. .name = "roberts_opencl",
  370. .description = NULL_IF_CONFIG_SMALL("Apply roberts operator"),
  371. .priv_size = sizeof(ConvolutionOpenCLContext),
  372. .priv_class = &roberts_opencl_class,
  373. .init = &ff_opencl_filter_init,
  374. .uninit = &convolution_opencl_uninit,
  375. .query_formats = &ff_opencl_filter_query_formats,
  376. .inputs = convolution_opencl_inputs,
  377. .outputs = convolution_opencl_outputs,
  378. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  379. };
  380. #endif /* CONFIG_ROBERTS_OPENCL_FILTER */