vf_transpose_opencl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 <float.h>
  19. #include "libavutil/avassert.h"
  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 "avfilter.h"
  26. #include "internal.h"
  27. #include "opencl.h"
  28. #include "opencl_source.h"
  29. #include "video.h"
  30. #include "transpose.h"
  31. typedef struct TransposeOpenCLContext {
  32. OpenCLFilterContext ocf;
  33. int initialised;
  34. int passthrough; ///< PassthroughType, landscape passthrough mode enabled
  35. int dir; ///< TransposeDir
  36. cl_kernel kernel;
  37. cl_command_queue command_queue;
  38. } TransposeOpenCLContext;
  39. static int transpose_opencl_init(AVFilterContext *avctx)
  40. {
  41. TransposeOpenCLContext *ctx = avctx->priv;
  42. cl_int cle;
  43. int err;
  44. err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_transpose, 1);
  45. if (err < 0)
  46. goto fail;
  47. ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
  48. ctx->ocf.hwctx->device_id,
  49. 0, &cle);
  50. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
  51. "command queue %d.\n", cle);
  52. ctx->kernel = clCreateKernel(ctx->ocf.program, "transpose", &cle);
  53. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create kernel %d.\n", cle);
  54. ctx->initialised = 1;
  55. return 0;
  56. fail:
  57. if (ctx->command_queue)
  58. clReleaseCommandQueue(ctx->command_queue);
  59. if (ctx->kernel)
  60. clReleaseKernel(ctx->kernel);
  61. return err;
  62. }
  63. static int transpose_opencl_config_output(AVFilterLink *outlink)
  64. {
  65. AVFilterContext *avctx = outlink->src;
  66. TransposeOpenCLContext *s = avctx->priv;
  67. AVFilterLink *inlink = avctx->inputs[0];
  68. const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
  69. int ret;
  70. if ((inlink->w >= inlink->h &&
  71. s->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
  72. (inlink->w <= inlink->h &&
  73. s->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
  74. if (inlink->hw_frames_ctx) {
  75. outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
  76. if (!outlink->hw_frames_ctx)
  77. return AVERROR(ENOMEM);
  78. }
  79. av_log(avctx, AV_LOG_VERBOSE,
  80. "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
  81. inlink->w, inlink->h, inlink->w, inlink->h);
  82. return 0;
  83. } else {
  84. s->passthrough = TRANSPOSE_PT_TYPE_NONE;
  85. }
  86. if (desc_in->log2_chroma_w != desc_in->log2_chroma_h) {
  87. av_log(avctx, AV_LOG_ERROR, "Input format %s not supported.\n",
  88. desc_in->name);
  89. return AVERROR(EINVAL);
  90. }
  91. s->ocf.output_width = inlink->h;
  92. s->ocf.output_height = inlink->w;
  93. ret = ff_opencl_filter_config_output(outlink);
  94. if (ret < 0)
  95. return ret;
  96. if (inlink->sample_aspect_ratio.num)
  97. outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 },
  98. inlink->sample_aspect_ratio);
  99. else
  100. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  101. av_log(avctx, AV_LOG_VERBOSE,
  102. "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
  103. inlink->w, inlink->h, s->dir, outlink->w, outlink->h,
  104. s->dir == 1 || s->dir == 3 ? "clockwise" : "counterclockwise",
  105. s->dir == 0 || s->dir == 3);
  106. return 0;
  107. }
  108. static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
  109. {
  110. TransposeOpenCLContext *s = inlink->dst->priv;
  111. return s->passthrough ?
  112. ff_null_get_video_buffer (inlink, w, h) :
  113. ff_default_get_video_buffer(inlink, w, h);
  114. }
  115. static int transpose_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
  116. {
  117. AVFilterContext *avctx = inlink->dst;
  118. AVFilterLink *outlink = avctx->outputs[0];
  119. TransposeOpenCLContext *ctx = avctx->priv;
  120. AVFrame *output = NULL;
  121. size_t global_work[2];
  122. cl_mem src, dst;
  123. cl_int cle;
  124. int err, p;
  125. av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
  126. av_get_pix_fmt_name(input->format),
  127. input->width, input->height, input->pts);
  128. if (!input->hw_frames_ctx)
  129. return AVERROR(EINVAL);
  130. if (ctx->passthrough)
  131. return ff_filter_frame(outlink, input);
  132. output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  133. if (!output) {
  134. err = AVERROR(ENOMEM);
  135. goto fail;
  136. }
  137. err = av_frame_copy_props(output, input);
  138. if (err < 0)
  139. goto fail;
  140. if (input->sample_aspect_ratio.num == 0) {
  141. output->sample_aspect_ratio = input->sample_aspect_ratio;
  142. } else {
  143. output->sample_aspect_ratio.num = input->sample_aspect_ratio.den;
  144. output->sample_aspect_ratio.den = input->sample_aspect_ratio.num;
  145. }
  146. if (!ctx->initialised) {
  147. err = transpose_opencl_init(avctx);
  148. if (err < 0)
  149. goto fail;
  150. }
  151. for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) {
  152. src = (cl_mem) input->data[p];
  153. dst = (cl_mem) output->data[p];
  154. if (!dst)
  155. break;
  156. CL_SET_KERNEL_ARG(ctx->kernel, 0, cl_mem, &dst);
  157. CL_SET_KERNEL_ARG(ctx->kernel, 1, cl_mem, &src);
  158. CL_SET_KERNEL_ARG(ctx->kernel, 2, cl_int, &ctx->dir);
  159. err = ff_opencl_filter_work_size_from_image(avctx, global_work, output,
  160. p, 16);
  161. cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
  162. global_work, NULL,
  163. 0, NULL, NULL);
  164. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue kernel: %d.\n", cle);
  165. }
  166. cle = clFinish(ctx->command_queue);
  167. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
  168. av_frame_free(&input);
  169. av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
  170. av_get_pix_fmt_name(output->format),
  171. output->width, output->height, output->pts);
  172. return ff_filter_frame(outlink, output);
  173. fail:
  174. clFinish(ctx->command_queue);
  175. av_frame_free(&input);
  176. av_frame_free(&output);
  177. return err;
  178. }
  179. static av_cold void transpose_opencl_uninit(AVFilterContext *avctx)
  180. {
  181. TransposeOpenCLContext *ctx = avctx->priv;
  182. cl_int cle;
  183. if (ctx->kernel) {
  184. cle = clReleaseKernel(ctx->kernel);
  185. if (cle != CL_SUCCESS)
  186. av_log(avctx, AV_LOG_ERROR, "Failed to release "
  187. "kernel: %d.\n", cle);
  188. }
  189. if (ctx->command_queue) {
  190. cle = clReleaseCommandQueue(ctx->command_queue);
  191. if (cle != CL_SUCCESS)
  192. av_log(avctx, AV_LOG_ERROR, "Failed to release "
  193. "command queue: %d.\n", cle);
  194. }
  195. ff_opencl_filter_uninit(avctx);
  196. }
  197. #define OFFSET(x) offsetof(TransposeOpenCLContext, x)
  198. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  199. static const AVOption transpose_opencl_options[] = {
  200. { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 3, FLAGS, "dir" },
  201. { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
  202. { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .flags=FLAGS, .unit = "dir" },
  203. { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .flags=FLAGS, .unit = "dir" },
  204. { "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
  205. { "passthrough", "do not apply transposition if the input matches the specified geometry",
  206. OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE}, 0, INT_MAX, FLAGS, "passthrough" },
  207. { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  208. { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  209. { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  210. { NULL }
  211. };
  212. AVFILTER_DEFINE_CLASS(transpose_opencl);
  213. static const AVFilterPad transpose_opencl_inputs[] = {
  214. {
  215. .name = "default",
  216. .type = AVMEDIA_TYPE_VIDEO,
  217. .get_video_buffer = get_video_buffer,
  218. .filter_frame = &transpose_opencl_filter_frame,
  219. .config_props = &ff_opencl_filter_config_input,
  220. },
  221. { NULL }
  222. };
  223. static const AVFilterPad transpose_opencl_outputs[] = {
  224. {
  225. .name = "default",
  226. .type = AVMEDIA_TYPE_VIDEO,
  227. .config_props = &transpose_opencl_config_output,
  228. },
  229. { NULL }
  230. };
  231. AVFilter ff_vf_transpose_opencl = {
  232. .name = "transpose_opencl",
  233. .description = NULL_IF_CONFIG_SMALL("Transpose input video"),
  234. .priv_size = sizeof(TransposeOpenCLContext),
  235. .priv_class = &transpose_opencl_class,
  236. .init = &ff_opencl_filter_init,
  237. .uninit = &transpose_opencl_uninit,
  238. .query_formats = &ff_opencl_filter_query_formats,
  239. .inputs = transpose_opencl_inputs,
  240. .outputs = transpose_opencl_outputs,
  241. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  242. };