deshake_opencl.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.com>
  3. * Copyright (C) 2013 Lenny Wang
  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. /**
  22. * @file
  23. * transform input video
  24. */
  25. #include "libavutil/common.h"
  26. #include "libavutil/dict.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "deshake_opencl.h"
  29. #include "libavutil/opencl_internal.h"
  30. #define PLANE_NUM 3
  31. #define ROUND_TO_16(a) ((((a - 1)/16)+1)*16)
  32. int ff_opencl_transform(AVFilterContext *ctx,
  33. int width, int height, int cw, int ch,
  34. const float *matrix_y, const float *matrix_uv,
  35. enum InterpolateMethod interpolate,
  36. enum FillMethod fill, AVFrame *in, AVFrame *out)
  37. {
  38. int ret = 0;
  39. cl_int status;
  40. DeshakeContext *deshake = ctx->priv;
  41. float4 packed_matrix_lu = {matrix_y[0], matrix_y[1], matrix_y[2], matrix_y[5]};
  42. float4 packed_matrix_ch = {matrix_uv[0], matrix_uv[1], matrix_uv[2], matrix_uv[5]};
  43. size_t global_worksize_lu[2] = {(size_t)ROUND_TO_16(width), (size_t)ROUND_TO_16(height)};
  44. size_t global_worksize_ch[2] = {(size_t)ROUND_TO_16(cw), (size_t)(2*ROUND_TO_16(ch))};
  45. size_t local_worksize[2] = {16, 16};
  46. FFOpenclParam param_lu = {0};
  47. FFOpenclParam param_ch = {0};
  48. param_lu.ctx = param_ch.ctx = ctx;
  49. param_lu.kernel = deshake->opencl_ctx.kernel_luma;
  50. param_ch.kernel = deshake->opencl_ctx.kernel_chroma;
  51. if ((unsigned int)interpolate > INTERPOLATE_BIQUADRATIC) {
  52. av_log(ctx, AV_LOG_ERROR, "Selected interpolate method is invalid\n");
  53. return AVERROR(EINVAL);
  54. }
  55. ret = ff_opencl_set_parameter(&param_lu,
  56. FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_inbuf),
  57. FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_outbuf),
  58. FF_OPENCL_PARAM_INFO(packed_matrix_lu),
  59. FF_OPENCL_PARAM_INFO(interpolate),
  60. FF_OPENCL_PARAM_INFO(fill),
  61. FF_OPENCL_PARAM_INFO(in->linesize[0]),
  62. FF_OPENCL_PARAM_INFO(out->linesize[0]),
  63. FF_OPENCL_PARAM_INFO(height),
  64. FF_OPENCL_PARAM_INFO(width),
  65. NULL);
  66. if (ret < 0)
  67. return ret;
  68. ret = ff_opencl_set_parameter(&param_ch,
  69. FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_inbuf),
  70. FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_outbuf),
  71. FF_OPENCL_PARAM_INFO(packed_matrix_ch),
  72. FF_OPENCL_PARAM_INFO(interpolate),
  73. FF_OPENCL_PARAM_INFO(fill),
  74. FF_OPENCL_PARAM_INFO(in->linesize[0]),
  75. FF_OPENCL_PARAM_INFO(out->linesize[0]),
  76. FF_OPENCL_PARAM_INFO(in->linesize[1]),
  77. FF_OPENCL_PARAM_INFO(out->linesize[1]),
  78. FF_OPENCL_PARAM_INFO(height),
  79. FF_OPENCL_PARAM_INFO(width),
  80. FF_OPENCL_PARAM_INFO(ch),
  81. FF_OPENCL_PARAM_INFO(cw),
  82. NULL);
  83. if (ret < 0)
  84. return ret;
  85. status = clEnqueueNDRangeKernel(deshake->opencl_ctx.command_queue,
  86. deshake->opencl_ctx.kernel_luma, 2, NULL,
  87. global_worksize_lu, local_worksize, 0, NULL, NULL);
  88. status |= clEnqueueNDRangeKernel(deshake->opencl_ctx.command_queue,
  89. deshake->opencl_ctx.kernel_chroma, 2, NULL,
  90. global_worksize_ch, local_worksize, 0, NULL, NULL);
  91. if (status != CL_SUCCESS) {
  92. av_log(ctx, AV_LOG_ERROR, "OpenCL run kernel error occurred: %s\n", av_opencl_errstr(status));
  93. return AVERROR_EXTERNAL;
  94. }
  95. ret = av_opencl_buffer_read_image(out->data, deshake->opencl_ctx.out_plane_size,
  96. deshake->opencl_ctx.plane_num, deshake->opencl_ctx.cl_outbuf,
  97. deshake->opencl_ctx.cl_outbuf_size);
  98. if (ret < 0)
  99. return ret;
  100. return ret;
  101. }
  102. int ff_opencl_deshake_init(AVFilterContext *ctx)
  103. {
  104. int ret = 0;
  105. DeshakeContext *deshake = ctx->priv;
  106. ret = av_opencl_init(NULL);
  107. if (ret < 0)
  108. return ret;
  109. deshake->opencl_ctx.plane_num = PLANE_NUM;
  110. deshake->opencl_ctx.command_queue = av_opencl_get_command_queue();
  111. if (!deshake->opencl_ctx.command_queue) {
  112. av_log(ctx, AV_LOG_ERROR, "Unable to get OpenCL command queue in filter 'deshake'\n");
  113. return AVERROR(EINVAL);
  114. }
  115. deshake->opencl_ctx.program = av_opencl_compile("avfilter_transform", NULL);
  116. if (!deshake->opencl_ctx.program) {
  117. av_log(ctx, AV_LOG_ERROR, "OpenCL failed to compile program 'avfilter_transform'\n");
  118. return AVERROR(EINVAL);
  119. }
  120. if (!deshake->opencl_ctx.kernel_luma) {
  121. deshake->opencl_ctx.kernel_luma = clCreateKernel(deshake->opencl_ctx.program,
  122. "avfilter_transform_luma", &ret);
  123. if (ret != CL_SUCCESS) {
  124. av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel 'avfilter_transform_luma'\n");
  125. return AVERROR(EINVAL);
  126. }
  127. }
  128. if (!deshake->opencl_ctx.kernel_chroma) {
  129. deshake->opencl_ctx.kernel_chroma = clCreateKernel(deshake->opencl_ctx.program,
  130. "avfilter_transform_chroma", &ret);
  131. if (ret != CL_SUCCESS) {
  132. av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel 'avfilter_transform_chroma'\n");
  133. return AVERROR(EINVAL);
  134. }
  135. }
  136. return ret;
  137. }
  138. void ff_opencl_deshake_uninit(AVFilterContext *ctx)
  139. {
  140. DeshakeContext *deshake = ctx->priv;
  141. av_opencl_buffer_release(&deshake->opencl_ctx.cl_inbuf);
  142. av_opencl_buffer_release(&deshake->opencl_ctx.cl_outbuf);
  143. clReleaseKernel(deshake->opencl_ctx.kernel_luma);
  144. clReleaseKernel(deshake->opencl_ctx.kernel_chroma);
  145. clReleaseProgram(deshake->opencl_ctx.program);
  146. deshake->opencl_ctx.command_queue = NULL;
  147. av_opencl_uninit();
  148. }
  149. int ff_opencl_deshake_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
  150. {
  151. int ret = 0;
  152. AVFilterLink *link = ctx->inputs[0];
  153. DeshakeContext *deshake = ctx->priv;
  154. const int hshift = av_pix_fmt_desc_get(link->format)->log2_chroma_h;
  155. int chroma_height = FF_CEIL_RSHIFT(link->h, hshift);
  156. if ((!deshake->opencl_ctx.cl_inbuf) || (!deshake->opencl_ctx.cl_outbuf)) {
  157. deshake->opencl_ctx.in_plane_size[0] = (in->linesize[0] * in->height);
  158. deshake->opencl_ctx.in_plane_size[1] = (in->linesize[1] * chroma_height);
  159. deshake->opencl_ctx.in_plane_size[2] = (in->linesize[2] * chroma_height);
  160. deshake->opencl_ctx.out_plane_size[0] = (out->linesize[0] * out->height);
  161. deshake->opencl_ctx.out_plane_size[1] = (out->linesize[1] * chroma_height);
  162. deshake->opencl_ctx.out_plane_size[2] = (out->linesize[2] * chroma_height);
  163. deshake->opencl_ctx.cl_inbuf_size = deshake->opencl_ctx.in_plane_size[0] +
  164. deshake->opencl_ctx.in_plane_size[1] +
  165. deshake->opencl_ctx.in_plane_size[2];
  166. deshake->opencl_ctx.cl_outbuf_size = deshake->opencl_ctx.out_plane_size[0] +
  167. deshake->opencl_ctx.out_plane_size[1] +
  168. deshake->opencl_ctx.out_plane_size[2];
  169. if (!deshake->opencl_ctx.cl_inbuf) {
  170. ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_inbuf,
  171. deshake->opencl_ctx.cl_inbuf_size,
  172. CL_MEM_READ_ONLY, NULL);
  173. if (ret < 0)
  174. return ret;
  175. }
  176. if (!deshake->opencl_ctx.cl_outbuf) {
  177. ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_outbuf,
  178. deshake->opencl_ctx.cl_outbuf_size,
  179. CL_MEM_READ_WRITE, NULL);
  180. if (ret < 0)
  181. return ret;
  182. }
  183. }
  184. ret = av_opencl_buffer_write_image(deshake->opencl_ctx.cl_inbuf,
  185. deshake->opencl_ctx.cl_inbuf_size,
  186. 0, in->data,deshake->opencl_ctx.in_plane_size,
  187. deshake->opencl_ctx.plane_num);
  188. if(ret < 0)
  189. return ret;
  190. return ret;
  191. }