vf_program_opencl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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/avstring.h"
  19. #include "libavutil/log.h"
  20. #include "libavutil/mem.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "avfilter.h"
  24. #include "framesync.h"
  25. #include "internal.h"
  26. #include "opencl.h"
  27. #include "video.h"
  28. typedef struct ProgramOpenCLContext {
  29. OpenCLFilterContext ocf;
  30. int loaded;
  31. cl_uint index;
  32. cl_kernel kernel;
  33. cl_command_queue command_queue;
  34. FFFrameSync fs;
  35. AVFrame **frames;
  36. const char *source_file;
  37. const char *kernel_name;
  38. int nb_inputs;
  39. int width, height;
  40. enum AVPixelFormat source_format;
  41. AVRational source_rate;
  42. } ProgramOpenCLContext;
  43. static int program_opencl_load(AVFilterContext *avctx)
  44. {
  45. ProgramOpenCLContext *ctx = avctx->priv;
  46. cl_int cle;
  47. int err;
  48. err = ff_opencl_filter_load_program_from_file(avctx, ctx->source_file);
  49. if (err < 0)
  50. return err;
  51. ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
  52. ctx->ocf.hwctx->device_id,
  53. 0, &cle);
  54. if (!ctx->command_queue) {
  55. av_log(avctx, AV_LOG_ERROR, "Failed to create OpenCL "
  56. "command queue: %d.\n", cle);
  57. return AVERROR(EIO);
  58. }
  59. ctx->kernel = clCreateKernel(ctx->ocf.program, ctx->kernel_name, &cle);
  60. if (!ctx->kernel) {
  61. if (cle == CL_INVALID_KERNEL_NAME) {
  62. av_log(avctx, AV_LOG_ERROR, "Kernel function '%s' not found in "
  63. "program.\n", ctx->kernel_name);
  64. } else {
  65. av_log(avctx, AV_LOG_ERROR, "Failed to create kernel: %d.\n", cle);
  66. }
  67. return AVERROR(EIO);
  68. }
  69. ctx->loaded = 1;
  70. return 0;
  71. }
  72. static int program_opencl_run(AVFilterContext *avctx)
  73. {
  74. AVFilterLink *outlink = avctx->outputs[0];
  75. ProgramOpenCLContext *ctx = avctx->priv;
  76. AVFrame *output = NULL;
  77. cl_int cle;
  78. size_t global_work[2];
  79. cl_mem src, dst;
  80. int err, input, plane;
  81. if (!ctx->loaded) {
  82. err = program_opencl_load(avctx);
  83. if (err < 0)
  84. return err;
  85. }
  86. output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  87. if (!output) {
  88. err = AVERROR(ENOMEM);
  89. goto fail;
  90. }
  91. for (plane = 0; plane < FF_ARRAY_ELEMS(output->data); plane++) {
  92. dst = (cl_mem)output->data[plane];
  93. if (!dst)
  94. break;
  95. cle = clSetKernelArg(ctx->kernel, 0, sizeof(cl_mem), &dst);
  96. if (cle != CL_SUCCESS) {
  97. av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
  98. "destination image argument: %d.\n", cle);
  99. err = AVERROR_UNKNOWN;
  100. goto fail;
  101. }
  102. cle = clSetKernelArg(ctx->kernel, 1, sizeof(cl_uint), &ctx->index);
  103. if (cle != CL_SUCCESS) {
  104. av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
  105. "index argument: %d.\n", cle);
  106. err = AVERROR_UNKNOWN;
  107. goto fail;
  108. }
  109. for (input = 0; input < ctx->nb_inputs; input++) {
  110. av_assert0(ctx->frames[input]);
  111. src = (cl_mem)ctx->frames[input]->data[plane];
  112. av_assert0(src);
  113. cle = clSetKernelArg(ctx->kernel, 2 + input, sizeof(cl_mem), &src);
  114. if (cle != CL_SUCCESS) {
  115. av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
  116. "source image argument %d: %d.\n", input, cle);
  117. err = AVERROR_UNKNOWN;
  118. goto fail;
  119. }
  120. }
  121. err = ff_opencl_filter_work_size_from_image(avctx, global_work,
  122. output, plane, 0);
  123. if (err < 0)
  124. goto fail;
  125. av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
  126. "(%"SIZE_SPECIFIER"x%"SIZE_SPECIFIER").\n",
  127. plane, global_work[0], global_work[1]);
  128. cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
  129. global_work, NULL, 0, NULL, NULL);
  130. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue kernel: %d.\n", cle);
  131. }
  132. cle = clFinish(ctx->command_queue);
  133. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
  134. if (ctx->nb_inputs > 0) {
  135. err = av_frame_copy_props(output, ctx->frames[0]);
  136. if (err < 0)
  137. goto fail;
  138. } else {
  139. output->pts = ctx->index;
  140. }
  141. ++ctx->index;
  142. av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
  143. av_get_pix_fmt_name(output->format),
  144. output->width, output->height, output->pts);
  145. return ff_filter_frame(outlink, output);
  146. fail:
  147. clFinish(ctx->command_queue);
  148. av_frame_free(&output);
  149. return err;
  150. }
  151. static int program_opencl_request_frame(AVFilterLink *outlink)
  152. {
  153. AVFilterContext *avctx = outlink->src;
  154. return program_opencl_run(avctx);
  155. }
  156. static int program_opencl_filter(FFFrameSync *fs)
  157. {
  158. AVFilterContext *avctx = fs->parent;
  159. ProgramOpenCLContext *ctx = avctx->priv;
  160. int err, i;
  161. for (i = 0; i < ctx->nb_inputs; i++) {
  162. err = ff_framesync_get_frame(&ctx->fs, i, &ctx->frames[i], 0);
  163. if (err < 0)
  164. return err;
  165. }
  166. return program_opencl_run(avctx);
  167. }
  168. static int program_opencl_activate(AVFilterContext *avctx)
  169. {
  170. ProgramOpenCLContext *ctx = avctx->priv;
  171. av_assert0(ctx->nb_inputs > 0);
  172. return ff_framesync_activate(&ctx->fs);
  173. }
  174. static int program_opencl_config_output(AVFilterLink *outlink)
  175. {
  176. AVFilterContext *avctx = outlink->src;
  177. ProgramOpenCLContext *ctx = avctx->priv;
  178. int err;
  179. err = ff_opencl_filter_config_output(outlink);
  180. if (err < 0)
  181. return err;
  182. if (ctx->nb_inputs > 0) {
  183. FFFrameSyncIn *in;
  184. int i;
  185. err = ff_framesync_init(&ctx->fs, avctx, ctx->nb_inputs);
  186. if (err < 0)
  187. return err;
  188. ctx->fs.opaque = ctx;
  189. ctx->fs.on_event = &program_opencl_filter;
  190. in = ctx->fs.in;
  191. for (i = 0; i < ctx->nb_inputs; i++) {
  192. const AVFilterLink *inlink = avctx->inputs[i];
  193. in[i].time_base = inlink->time_base;
  194. in[i].sync = 1;
  195. in[i].before = EXT_STOP;
  196. in[i].after = EXT_INFINITY;
  197. }
  198. err = ff_framesync_configure(&ctx->fs);
  199. if (err < 0)
  200. return err;
  201. } else {
  202. outlink->time_base = av_inv_q(ctx->source_rate);
  203. }
  204. return 0;
  205. }
  206. static av_cold int program_opencl_init(AVFilterContext *avctx)
  207. {
  208. ProgramOpenCLContext *ctx = avctx->priv;
  209. int err;
  210. ff_opencl_filter_init(avctx);
  211. ctx->ocf.output_width = ctx->width;
  212. ctx->ocf.output_height = ctx->height;
  213. if (!strcmp(avctx->filter->name, "openclsrc")) {
  214. if (!ctx->ocf.output_width || !ctx->ocf.output_height) {
  215. av_log(avctx, AV_LOG_ERROR, "OpenCL source requires output "
  216. "dimensions to be specified.\n");
  217. return AVERROR(EINVAL);
  218. }
  219. ctx->nb_inputs = 0;
  220. ctx->ocf.output_format = ctx->source_format;
  221. } else {
  222. int i;
  223. ctx->frames = av_mallocz_array(ctx->nb_inputs,
  224. sizeof(*ctx->frames));
  225. if (!ctx->frames)
  226. return AVERROR(ENOMEM);
  227. for (i = 0; i < ctx->nb_inputs; i++) {
  228. AVFilterPad input;
  229. memset(&input, 0, sizeof(input));
  230. input.type = AVMEDIA_TYPE_VIDEO;
  231. input.name = av_asprintf("input%d", i);
  232. if (!input.name)
  233. return AVERROR(ENOMEM);
  234. input.config_props = &ff_opencl_filter_config_input;
  235. err = ff_insert_inpad(avctx, i, &input);
  236. if (err < 0) {
  237. av_freep(&input.name);
  238. return err;
  239. }
  240. }
  241. }
  242. return 0;
  243. }
  244. static av_cold void program_opencl_uninit(AVFilterContext *avctx)
  245. {
  246. ProgramOpenCLContext *ctx = avctx->priv;
  247. cl_int cle;
  248. int i;
  249. if (ctx->nb_inputs > 0) {
  250. ff_framesync_uninit(&ctx->fs);
  251. av_freep(&ctx->frames);
  252. for (i = 0; i < avctx->nb_inputs; i++)
  253. av_freep(&avctx->input_pads[i].name);
  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. #define OFFSET(x) offsetof(ProgramOpenCLContext, x)
  270. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
  271. #if CONFIG_PROGRAM_OPENCL_FILTER
  272. static const AVOption program_opencl_options[] = {
  273. { "source", "OpenCL program source file", OFFSET(source_file),
  274. AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
  275. { "kernel", "Kernel name in program", OFFSET(kernel_name),
  276. AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
  277. { "inputs", "Number of inputs", OFFSET(nb_inputs),
  278. AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, FLAGS },
  279. { "size", "Video size", OFFSET(width),
  280. AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, FLAGS },
  281. { "s", "Video size", OFFSET(width),
  282. AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, FLAGS },
  283. { NULL },
  284. };
  285. FRAMESYNC_DEFINE_CLASS(program_opencl, ProgramOpenCLContext, fs);
  286. static const AVFilterPad program_opencl_outputs[] = {
  287. {
  288. .name = "default",
  289. .type = AVMEDIA_TYPE_VIDEO,
  290. .config_props = &program_opencl_config_output,
  291. },
  292. { NULL }
  293. };
  294. AVFilter ff_vf_program_opencl = {
  295. .name = "program_opencl",
  296. .description = NULL_IF_CONFIG_SMALL("Filter video using an OpenCL program"),
  297. .priv_size = sizeof(ProgramOpenCLContext),
  298. .priv_class = &program_opencl_class,
  299. .preinit = &program_opencl_framesync_preinit,
  300. .init = &program_opencl_init,
  301. .uninit = &program_opencl_uninit,
  302. .query_formats = &ff_opencl_filter_query_formats,
  303. .activate = &program_opencl_activate,
  304. .inputs = NULL,
  305. .outputs = program_opencl_outputs,
  306. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  307. };
  308. #endif
  309. #if CONFIG_OPENCLSRC_FILTER
  310. static const AVOption openclsrc_options[] = {
  311. { "source", "OpenCL program source file", OFFSET(source_file),
  312. AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
  313. { "kernel", "Kernel name in program", OFFSET(kernel_name),
  314. AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
  315. { "size", "Video size", OFFSET(width),
  316. AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, FLAGS },
  317. { "s", "Video size", OFFSET(width),
  318. AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, FLAGS },
  319. { "format", "Video format", OFFSET(source_format),
  320. AV_OPT_TYPE_PIXEL_FMT, { .i64 = AV_PIX_FMT_NONE }, -1, INT_MAX, FLAGS },
  321. { "rate", "Video frame rate", OFFSET(source_rate),
  322. AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, FLAGS },
  323. { "r", "Video frame rate", OFFSET(source_rate),
  324. AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, FLAGS },
  325. { NULL },
  326. };
  327. AVFILTER_DEFINE_CLASS(openclsrc);
  328. static const AVFilterPad openclsrc_outputs[] = {
  329. {
  330. .name = "default",
  331. .type = AVMEDIA_TYPE_VIDEO,
  332. .config_props = &program_opencl_config_output,
  333. .request_frame = &program_opencl_request_frame,
  334. },
  335. { NULL }
  336. };
  337. AVFilter ff_vsrc_openclsrc = {
  338. .name = "openclsrc",
  339. .description = NULL_IF_CONFIG_SMALL("Generate video using an OpenCL program"),
  340. .priv_size = sizeof(ProgramOpenCLContext),
  341. .priv_class = &openclsrc_class,
  342. .init = &program_opencl_init,
  343. .uninit = &program_opencl_uninit,
  344. .query_formats = &ff_opencl_filter_query_formats,
  345. .inputs = NULL,
  346. .outputs = openclsrc_outputs,
  347. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  348. };
  349. #endif