vf_avgblur_opencl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (c) 2018 Dylan Fernando
  3. * Copyright (c) 2018 Danil Iashchenko
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/common.h"
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/opt.h"
  24. #include "avfilter.h"
  25. #include "internal.h"
  26. #include "opencl.h"
  27. #include "opencl_source.h"
  28. #include "video.h"
  29. #include "boxblur.h"
  30. typedef struct AverageBlurOpenCLContext {
  31. OpenCLFilterContext ocf;
  32. int initialised;
  33. cl_kernel kernel_horiz;
  34. cl_kernel kernel_vert;
  35. cl_command_queue command_queue;
  36. int radiusH;
  37. int radiusV;
  38. int planes;
  39. FilterParam luma_param;
  40. FilterParam chroma_param;
  41. FilterParam alpha_param;
  42. int radius[4];
  43. int power[4];
  44. } AverageBlurOpenCLContext;
  45. static int avgblur_opencl_init(AVFilterContext *avctx)
  46. {
  47. AverageBlurOpenCLContext *ctx = avctx->priv;
  48. cl_int cle;
  49. int err;
  50. err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_avgblur, 1);
  51. if (err < 0)
  52. goto fail;
  53. ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
  54. ctx->ocf.hwctx->device_id,
  55. 0, &cle);
  56. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
  57. "command queue %d.\n", cle);
  58. ctx->kernel_horiz = clCreateKernel(ctx->ocf.program,"avgblur_horiz", &cle);
  59. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create horizontal "
  60. "kernel %d.\n", cle);
  61. ctx->kernel_vert = clCreateKernel(ctx->ocf.program,"avgblur_vert", &cle);
  62. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create vertical "
  63. "kernel %d.\n", cle);
  64. ctx->initialised = 1;
  65. return 0;
  66. fail:
  67. if (ctx->command_queue)
  68. clReleaseCommandQueue(ctx->command_queue);
  69. if (ctx->kernel_horiz)
  70. clReleaseKernel(ctx->kernel_horiz);
  71. if (ctx->kernel_vert)
  72. clReleaseKernel(ctx->kernel_vert);
  73. return err;
  74. }
  75. static int avgblur_opencl_make_filter_params(AVFilterLink *inlink)
  76. {
  77. AVFilterContext *ctx = inlink->dst;
  78. AverageBlurOpenCLContext *s = ctx->priv;
  79. int i;
  80. if (s->radiusV <= 0) {
  81. s->radiusV = s->radiusH;
  82. }
  83. for (i = 0; i < 4; i++) {
  84. s->power[i] = 1;
  85. }
  86. return 0;
  87. }
  88. static int boxblur_opencl_make_filter_params(AVFilterLink *inlink)
  89. {
  90. AVFilterContext *ctx = inlink->dst;
  91. AverageBlurOpenCLContext *s = ctx->priv;
  92. int err, i;
  93. err = ff_boxblur_eval_filter_params(inlink,
  94. &s->luma_param,
  95. &s->chroma_param,
  96. &s->alpha_param);
  97. if (err != 0) {
  98. av_log(ctx, AV_LOG_ERROR, "Failed to evaluate "
  99. "filter params: %d.\n", err);
  100. return err;
  101. }
  102. s->radius[Y] = s->luma_param.radius;
  103. s->radius[U] = s->radius[V] = s->chroma_param.radius;
  104. s->radius[A] = s->alpha_param.radius;
  105. s->power[Y] = s->luma_param.power;
  106. s->power[U] = s->power[V] = s->chroma_param.power;
  107. s->power[A] = s->alpha_param.power;
  108. for (i = 0; i < 4; i++) {
  109. if (s->power[i] == 0) {
  110. s->power[i] = 1;
  111. s->radius[i] = 0;
  112. }
  113. }
  114. return 0;
  115. }
  116. static int avgblur_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
  117. {
  118. AVFilterContext *avctx = inlink->dst;
  119. AVFilterLink *outlink = avctx->outputs[0];
  120. AverageBlurOpenCLContext *ctx = avctx->priv;
  121. AVFrame *output = NULL;
  122. AVFrame *intermediate = NULL;
  123. cl_int cle;
  124. size_t global_work[2];
  125. cl_mem src, dst, inter;
  126. int err, p, radius_x, radius_y, i;
  127. av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
  128. av_get_pix_fmt_name(input->format),
  129. input->width, input->height, input->pts);
  130. if (!input->hw_frames_ctx)
  131. return AVERROR(EINVAL);
  132. if (!ctx->initialised) {
  133. err = avgblur_opencl_init(avctx);
  134. if (err < 0)
  135. goto fail;
  136. if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
  137. err = avgblur_opencl_make_filter_params(inlink);
  138. if (err < 0)
  139. goto fail;
  140. } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
  141. err = boxblur_opencl_make_filter_params(inlink);
  142. if (err < 0)
  143. goto fail;
  144. }
  145. }
  146. output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  147. if (!output) {
  148. err = AVERROR(ENOMEM);
  149. goto fail;
  150. }
  151. intermediate = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  152. if (!intermediate) {
  153. err = AVERROR(ENOMEM);
  154. goto fail;
  155. }
  156. for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) {
  157. src = (cl_mem) input->data[p];
  158. dst = (cl_mem) output->data[p];
  159. inter = (cl_mem)intermediate->data[p];
  160. if (!dst)
  161. break;
  162. radius_x = ctx->radiusH;
  163. radius_y = ctx->radiusV;
  164. if (!(ctx->planes & (1 << p))) {
  165. radius_x = 0;
  166. radius_y = 0;
  167. }
  168. for (i = 0; i < ctx->power[p]; i++) {
  169. CL_SET_KERNEL_ARG(ctx->kernel_horiz, 0, cl_mem, &inter);
  170. CL_SET_KERNEL_ARG(ctx->kernel_horiz, 1, cl_mem, i == 0 ? &src : &dst);
  171. if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
  172. CL_SET_KERNEL_ARG(ctx->kernel_horiz, 2, cl_int, &radius_x);
  173. } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
  174. CL_SET_KERNEL_ARG(ctx->kernel_horiz, 2, cl_int, &ctx->radius[p]);
  175. }
  176. err = ff_opencl_filter_work_size_from_image(avctx, global_work,
  177. i == 0 ? intermediate : output, p, 0);
  178. if (err < 0)
  179. goto fail;
  180. av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
  181. "(%"SIZE_SPECIFIER"x%"SIZE_SPECIFIER").\n",
  182. p, global_work[0], global_work[1]);
  183. cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel_horiz, 2, NULL,
  184. global_work, NULL,
  185. 0, NULL, NULL);
  186. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue horizontal "
  187. "kernel: %d.\n", cle);
  188. err = ff_opencl_filter_work_size_from_image(avctx, global_work,
  189. i == 0 ? output : intermediate, p, 0);
  190. CL_SET_KERNEL_ARG(ctx->kernel_vert, 0, cl_mem, &dst);
  191. CL_SET_KERNEL_ARG(ctx->kernel_vert, 1, cl_mem, &inter);
  192. if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
  193. CL_SET_KERNEL_ARG(ctx->kernel_vert, 2, cl_int, &radius_y);
  194. } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
  195. CL_SET_KERNEL_ARG(ctx->kernel_vert, 2, cl_int, &ctx->radius[p]);
  196. }
  197. cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel_vert, 2, NULL,
  198. global_work, NULL,
  199. 0, NULL, NULL);
  200. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue vertical "
  201. "kernel: %d.\n", cle);
  202. }
  203. }
  204. cle = clFinish(ctx->command_queue);
  205. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
  206. err = av_frame_copy_props(output, input);
  207. if (err < 0)
  208. goto fail;
  209. av_frame_free(&input);
  210. av_frame_free(&intermediate);
  211. av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
  212. av_get_pix_fmt_name(output->format),
  213. output->width, output->height, output->pts);
  214. return ff_filter_frame(outlink, output);
  215. fail:
  216. clFinish(ctx->command_queue);
  217. av_frame_free(&input);
  218. av_frame_free(&output);
  219. av_frame_free(&intermediate);
  220. return err;
  221. }
  222. static av_cold void avgblur_opencl_uninit(AVFilterContext *avctx)
  223. {
  224. AverageBlurOpenCLContext *ctx = avctx->priv;
  225. cl_int cle;
  226. if (ctx->kernel_horiz) {
  227. cle = clReleaseKernel(ctx->kernel_horiz);
  228. if (cle != CL_SUCCESS)
  229. av_log(avctx, AV_LOG_ERROR, "Failed to release "
  230. "kernel: %d.\n", cle);
  231. }
  232. if (ctx->kernel_vert) {
  233. cle = clReleaseKernel(ctx->kernel_vert);
  234. if (cle != CL_SUCCESS)
  235. av_log(avctx, AV_LOG_ERROR, "Failed to release "
  236. "kernel: %d.\n", cle);
  237. }
  238. if (ctx->command_queue) {
  239. cle = clReleaseCommandQueue(ctx->command_queue);
  240. if (cle != CL_SUCCESS)
  241. av_log(avctx, AV_LOG_ERROR, "Failed to release "
  242. "command queue: %d.\n", cle);
  243. }
  244. ff_opencl_filter_uninit(avctx);
  245. }
  246. static const AVFilterPad avgblur_opencl_inputs[] = {
  247. {
  248. .name = "default",
  249. .type = AVMEDIA_TYPE_VIDEO,
  250. .filter_frame = &avgblur_opencl_filter_frame,
  251. .config_props = &ff_opencl_filter_config_input,
  252. },
  253. { NULL }
  254. };
  255. static const AVFilterPad avgblur_opencl_outputs[] = {
  256. {
  257. .name = "default",
  258. .type = AVMEDIA_TYPE_VIDEO,
  259. .config_props = &ff_opencl_filter_config_output,
  260. },
  261. { NULL }
  262. };
  263. #define OFFSET(x) offsetof(AverageBlurOpenCLContext, x)
  264. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  265. #if CONFIG_AVGBLUR_OPENCL_FILTER
  266. static const AVOption avgblur_opencl_options[] = {
  267. { "sizeX", "set horizontal size", OFFSET(radiusH), AV_OPT_TYPE_INT, {.i64=1}, 1, 1024, FLAGS },
  268. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
  269. { "sizeY", "set vertical size", OFFSET(radiusV), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, FLAGS },
  270. { NULL }
  271. };
  272. AVFILTER_DEFINE_CLASS(avgblur_opencl);
  273. AVFilter ff_vf_avgblur_opencl = {
  274. .name = "avgblur_opencl",
  275. .description = NULL_IF_CONFIG_SMALL("Apply average blur filter"),
  276. .priv_size = sizeof(AverageBlurOpenCLContext),
  277. .priv_class = &avgblur_opencl_class,
  278. .init = &ff_opencl_filter_init,
  279. .uninit = &avgblur_opencl_uninit,
  280. .query_formats = &ff_opencl_filter_query_formats,
  281. .inputs = avgblur_opencl_inputs,
  282. .outputs = avgblur_opencl_outputs,
  283. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  284. };
  285. #endif /* CONFIG_AVGBLUR_OPENCL_FILTER */
  286. #if CONFIG_BOXBLUR_OPENCL_FILTER
  287. static const AVOption boxblur_opencl_options[] = {
  288. { "luma_radius", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
  289. { "lr", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
  290. { "luma_power", "How many times should the boxblur be applied to luma", OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS },
  291. { "lp", "How many times should the boxblur be applied to luma", OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS },
  292. { "chroma_radius", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  293. { "cr", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  294. { "chroma_power", "How many times should the boxblur be applied to chroma", OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  295. { "cp", "How many times should the boxblur be applied to chroma", OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  296. { "alpha_radius", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  297. { "ar", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  298. { "alpha_power", "How many times should the boxblur be applied to alpha", OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  299. { "ap", "How many times should the boxblur be applied to alpha", OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
  300. { NULL }
  301. };
  302. AVFILTER_DEFINE_CLASS(boxblur_opencl);
  303. AVFilter ff_vf_boxblur_opencl = {
  304. .name = "boxblur_opencl",
  305. .description = NULL_IF_CONFIG_SMALL("Apply boxblur filter to input video"),
  306. .priv_size = sizeof(AverageBlurOpenCLContext),
  307. .priv_class = &boxblur_opencl_class,
  308. .init = &ff_opencl_filter_init,
  309. .uninit = &avgblur_opencl_uninit,
  310. .query_formats = &ff_opencl_filter_query_formats,
  311. .inputs = avgblur_opencl_inputs,
  312. .outputs = avgblur_opencl_outputs,
  313. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  314. };
  315. #endif /* CONFIG_BOXBLUR_OPENCL_FILTER */