vf_overlay_opencl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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/log.h"
  19. #include "libavutil/mem.h"
  20. #include "libavutil/opt.h"
  21. #include "libavutil/pixdesc.h"
  22. #include "avfilter.h"
  23. #include "framesync.h"
  24. #include "internal.h"
  25. #include "opencl.h"
  26. #include "opencl_source.h"
  27. #include "video.h"
  28. typedef struct OverlayOpenCLContext {
  29. OpenCLFilterContext ocf;
  30. int initialised;
  31. cl_kernel kernel;
  32. cl_command_queue command_queue;
  33. FFFrameSync fs;
  34. int nb_planes;
  35. int x_subsample;
  36. int y_subsample;
  37. int alpha_separate;
  38. int x_position;
  39. int y_position;
  40. } OverlayOpenCLContext;
  41. static int overlay_opencl_load(AVFilterContext *avctx,
  42. enum AVPixelFormat main_format,
  43. enum AVPixelFormat overlay_format)
  44. {
  45. OverlayOpenCLContext *ctx = avctx->priv;
  46. cl_int cle;
  47. const char *source = ff_opencl_source_overlay;
  48. const char *kernel;
  49. const AVPixFmtDescriptor *main_desc, *overlay_desc;
  50. int err, i, main_planes, overlay_planes;
  51. main_desc = av_pix_fmt_desc_get(main_format);
  52. overlay_desc = av_pix_fmt_desc_get(overlay_format);
  53. main_planes = overlay_planes = 0;
  54. for (i = 0; i < main_desc->nb_components; i++)
  55. main_planes = FFMAX(main_planes,
  56. main_desc->comp[i].plane + 1);
  57. for (i = 0; i < overlay_desc->nb_components; i++)
  58. overlay_planes = FFMAX(overlay_planes,
  59. overlay_desc->comp[i].plane + 1);
  60. ctx->nb_planes = main_planes;
  61. ctx->x_subsample = 1 << main_desc->log2_chroma_w;
  62. ctx->y_subsample = 1 << main_desc->log2_chroma_h;
  63. if (ctx->x_position % ctx->x_subsample ||
  64. ctx->y_position % ctx->y_subsample) {
  65. av_log(avctx, AV_LOG_WARNING, "Warning: overlay position (%d, %d) "
  66. "does not match subsampling (%d, %d).\n",
  67. ctx->x_position, ctx->y_position,
  68. ctx->x_subsample, ctx->y_subsample);
  69. }
  70. if (main_planes == overlay_planes) {
  71. if (main_desc->nb_components == overlay_desc->nb_components)
  72. kernel = "overlay_no_alpha";
  73. else
  74. kernel = "overlay_internal_alpha";
  75. ctx->alpha_separate = 0;
  76. } else {
  77. kernel = "overlay_external_alpha";
  78. ctx->alpha_separate = 1;
  79. }
  80. av_log(avctx, AV_LOG_DEBUG, "Using kernel %s.\n", kernel);
  81. err = ff_opencl_filter_load_program(avctx, &source, 1);
  82. if (err < 0)
  83. goto fail;
  84. ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
  85. ctx->ocf.hwctx->device_id,
  86. 0, &cle);
  87. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
  88. "command queue %d.\n", cle);
  89. ctx->kernel = clCreateKernel(ctx->ocf.program, kernel, &cle);
  90. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create kernel %d.\n", cle);
  91. ctx->initialised = 1;
  92. return 0;
  93. fail:
  94. if (ctx->command_queue)
  95. clReleaseCommandQueue(ctx->command_queue);
  96. if (ctx->kernel)
  97. clReleaseKernel(ctx->kernel);
  98. return err;
  99. }
  100. static int overlay_opencl_blend(FFFrameSync *fs)
  101. {
  102. AVFilterContext *avctx = fs->parent;
  103. AVFilterLink *outlink = avctx->outputs[0];
  104. OverlayOpenCLContext *ctx = avctx->priv;
  105. AVFrame *input_main, *input_overlay;
  106. AVFrame *output;
  107. cl_mem mem;
  108. cl_int cle, x, y;
  109. size_t global_work[2];
  110. int kernel_arg = 0;
  111. int err, plane;
  112. err = ff_framesync_get_frame(fs, 0, &input_main, 0);
  113. if (err < 0)
  114. return err;
  115. err = ff_framesync_get_frame(fs, 1, &input_overlay, 0);
  116. if (err < 0)
  117. return err;
  118. if (!ctx->initialised) {
  119. AVHWFramesContext *main_fc =
  120. (AVHWFramesContext*)input_main->hw_frames_ctx->data;
  121. AVHWFramesContext *overlay_fc =
  122. (AVHWFramesContext*)input_overlay->hw_frames_ctx->data;
  123. err = overlay_opencl_load(avctx, main_fc->sw_format,
  124. overlay_fc->sw_format);
  125. if (err < 0)
  126. return err;
  127. }
  128. output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  129. if (!output) {
  130. err = AVERROR(ENOMEM);
  131. goto fail;
  132. }
  133. for (plane = 0; plane < ctx->nb_planes; plane++) {
  134. kernel_arg = 0;
  135. mem = (cl_mem)output->data[plane];
  136. CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_mem, &mem);
  137. kernel_arg++;
  138. mem = (cl_mem)input_main->data[plane];
  139. CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_mem, &mem);
  140. kernel_arg++;
  141. mem = (cl_mem)input_overlay->data[plane];
  142. CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_mem, &mem);
  143. kernel_arg++;
  144. if (ctx->alpha_separate) {
  145. mem = (cl_mem)input_overlay->data[ctx->nb_planes];
  146. CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_mem, &mem);
  147. kernel_arg++;
  148. }
  149. x = ctx->x_position / (plane == 0 ? 1 : ctx->x_subsample);
  150. y = ctx->y_position / (plane == 0 ? 1 : ctx->y_subsample);
  151. CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_int, &x);
  152. kernel_arg++;
  153. CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_int, &y);
  154. kernel_arg++;
  155. if (ctx->alpha_separate) {
  156. cl_int alpha_adj_x = plane == 0 ? 1 : ctx->x_subsample;
  157. cl_int alpha_adj_y = plane == 0 ? 1 : ctx->y_subsample;
  158. CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_int, &alpha_adj_x);
  159. kernel_arg++;
  160. CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_int, &alpha_adj_y);
  161. kernel_arg++;
  162. }
  163. err = ff_opencl_filter_work_size_from_image(avctx, global_work,
  164. output, plane, 0);
  165. if (err < 0)
  166. goto fail;
  167. cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
  168. global_work, NULL, 0, NULL, NULL);
  169. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue overlay kernel "
  170. "for plane %d: %d.\n", plane, cle);
  171. }
  172. cle = clFinish(ctx->command_queue);
  173. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
  174. err = av_frame_copy_props(output, input_main);
  175. av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
  176. av_get_pix_fmt_name(output->format),
  177. output->width, output->height, output->pts);
  178. return ff_filter_frame(outlink, output);
  179. fail:
  180. av_frame_free(&output);
  181. return err;
  182. }
  183. static int overlay_opencl_config_output(AVFilterLink *outlink)
  184. {
  185. AVFilterContext *avctx = outlink->src;
  186. OverlayOpenCLContext *ctx = avctx->priv;
  187. int err;
  188. err = ff_opencl_filter_config_output(outlink);
  189. if (err < 0)
  190. return err;
  191. err = ff_framesync_init_dualinput(&ctx->fs, avctx);
  192. if (err < 0)
  193. return err;
  194. return ff_framesync_configure(&ctx->fs);
  195. }
  196. static av_cold int overlay_opencl_init(AVFilterContext *avctx)
  197. {
  198. OverlayOpenCLContext *ctx = avctx->priv;
  199. ctx->fs.on_event = &overlay_opencl_blend;
  200. return ff_opencl_filter_init(avctx);
  201. }
  202. static int overlay_opencl_activate(AVFilterContext *avctx)
  203. {
  204. OverlayOpenCLContext *ctx = avctx->priv;
  205. return ff_framesync_activate(&ctx->fs);
  206. }
  207. static av_cold void overlay_opencl_uninit(AVFilterContext *avctx)
  208. {
  209. OverlayOpenCLContext *ctx = avctx->priv;
  210. cl_int cle;
  211. if (ctx->kernel) {
  212. cle = clReleaseKernel(ctx->kernel);
  213. if (cle != CL_SUCCESS)
  214. av_log(avctx, AV_LOG_ERROR, "Failed to release "
  215. "kernel: %d.\n", cle);
  216. }
  217. if (ctx->command_queue) {
  218. cle = clReleaseCommandQueue(ctx->command_queue);
  219. if (cle != CL_SUCCESS)
  220. av_log(avctx, AV_LOG_ERROR, "Failed to release "
  221. "command queue: %d.\n", cle);
  222. }
  223. ff_opencl_filter_uninit(avctx);
  224. ff_framesync_uninit(&ctx->fs);
  225. }
  226. #define OFFSET(x) offsetof(OverlayOpenCLContext, x)
  227. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  228. static const AVOption overlay_opencl_options[] = {
  229. { "x", "Overlay x position",
  230. OFFSET(x_position), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
  231. { "y", "Overlay y position",
  232. OFFSET(y_position), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
  233. { NULL },
  234. };
  235. AVFILTER_DEFINE_CLASS(overlay_opencl);
  236. static const AVFilterPad overlay_opencl_inputs[] = {
  237. {
  238. .name = "main",
  239. .type = AVMEDIA_TYPE_VIDEO,
  240. .config_props = &ff_opencl_filter_config_input,
  241. },
  242. {
  243. .name = "overlay",
  244. .type = AVMEDIA_TYPE_VIDEO,
  245. .config_props = &ff_opencl_filter_config_input,
  246. },
  247. { NULL }
  248. };
  249. static const AVFilterPad overlay_opencl_outputs[] = {
  250. {
  251. .name = "default",
  252. .type = AVMEDIA_TYPE_VIDEO,
  253. .config_props = &overlay_opencl_config_output,
  254. },
  255. { NULL }
  256. };
  257. AVFilter ff_vf_overlay_opencl = {
  258. .name = "overlay_opencl",
  259. .description = NULL_IF_CONFIG_SMALL("Overlay one video on top of another"),
  260. .priv_size = sizeof(OverlayOpenCLContext),
  261. .priv_class = &overlay_opencl_class,
  262. .init = &overlay_opencl_init,
  263. .uninit = &overlay_opencl_uninit,
  264. .query_formats = &ff_opencl_filter_query_formats,
  265. .activate = &overlay_opencl_activate,
  266. .inputs = overlay_opencl_inputs,
  267. .outputs = overlay_opencl_outputs,
  268. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  269. };