You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
8.3KB

  1. /*
  2. * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * transform input video
  23. */
  24. #include "libavutil/common.h"
  25. #include "libavutil/dict.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "deshake_opencl.h"
  28. #include "libavutil/opencl_internal.h"
  29. #define MATRIX_SIZE 6
  30. #define PLANE_NUM 3
  31. int ff_opencl_transform(AVFilterContext *ctx,
  32. int width, int height, int cw, int ch,
  33. const float *matrix_y, const float *matrix_uv,
  34. enum InterpolateMethod interpolate,
  35. enum FillMethod fill, AVFrame *in, AVFrame *out)
  36. {
  37. int ret = 0;
  38. const size_t global_work_size = width * height + 2 * ch * cw;
  39. cl_int status;
  40. DeshakeContext *deshake = ctx->priv;
  41. FFOpenclParam opencl_param = {0};
  42. opencl_param.ctx = ctx;
  43. opencl_param.kernel = deshake->opencl_ctx.kernel;
  44. ret = av_opencl_buffer_write(deshake->opencl_ctx.cl_matrix_y, (uint8_t *)matrix_y, deshake->opencl_ctx.matrix_size * sizeof(cl_float));
  45. if (ret < 0)
  46. return ret;
  47. ret = av_opencl_buffer_write(deshake->opencl_ctx.cl_matrix_uv, (uint8_t *)matrix_uv, deshake->opencl_ctx.matrix_size * sizeof(cl_float));
  48. if (ret < 0)
  49. return ret;
  50. if ((unsigned int)interpolate > INTERPOLATE_BIQUADRATIC) {
  51. av_log(ctx, AV_LOG_ERROR, "Selected interpolate method is invalid\n");
  52. return AVERROR(EINVAL);
  53. }
  54. ret = ff_opencl_set_parameter(&opencl_param,
  55. FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_inbuf),
  56. FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_outbuf),
  57. FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_matrix_y),
  58. FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_matrix_uv),
  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(in->linesize[1]),
  64. FF_OPENCL_PARAM_INFO(out->linesize[1]),
  65. FF_OPENCL_PARAM_INFO(height),
  66. FF_OPENCL_PARAM_INFO(width),
  67. FF_OPENCL_PARAM_INFO(ch),
  68. FF_OPENCL_PARAM_INFO(cw),
  69. NULL);
  70. if (ret < 0)
  71. return ret;
  72. status = clEnqueueNDRangeKernel(deshake->opencl_ctx.command_queue,
  73. deshake->opencl_ctx.kernel, 1, NULL,
  74. &global_work_size, NULL, 0, NULL, NULL);
  75. if (status != CL_SUCCESS) {
  76. av_log(ctx, AV_LOG_ERROR, "OpenCL run kernel error occurred: %s\n", av_opencl_errstr(status));
  77. return AVERROR_EXTERNAL;
  78. }
  79. clFinish(deshake->opencl_ctx.command_queue);
  80. ret = av_opencl_buffer_read_image(out->data, deshake->opencl_ctx.out_plane_size,
  81. deshake->opencl_ctx.plane_num, deshake->opencl_ctx.cl_outbuf,
  82. deshake->opencl_ctx.cl_outbuf_size);
  83. if (ret < 0)
  84. return ret;
  85. return ret;
  86. }
  87. int ff_opencl_deshake_init(AVFilterContext *ctx)
  88. {
  89. int ret = 0;
  90. DeshakeContext *deshake = ctx->priv;
  91. ret = av_opencl_init(NULL);
  92. if (ret < 0)
  93. return ret;
  94. deshake->opencl_ctx.matrix_size = MATRIX_SIZE;
  95. deshake->opencl_ctx.plane_num = PLANE_NUM;
  96. ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_matrix_y,
  97. deshake->opencl_ctx.matrix_size*sizeof(cl_float), CL_MEM_READ_ONLY, NULL);
  98. if (ret < 0)
  99. return ret;
  100. ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_matrix_uv,
  101. deshake->opencl_ctx.matrix_size*sizeof(cl_float), CL_MEM_READ_ONLY, NULL);
  102. if (ret < 0)
  103. return ret;
  104. deshake->opencl_ctx.command_queue = av_opencl_get_command_queue();
  105. if (!deshake->opencl_ctx.command_queue) {
  106. av_log(ctx, AV_LOG_ERROR, "Unable to get OpenCL command queue in filter 'deshake'\n");
  107. return AVERROR(EINVAL);
  108. }
  109. deshake->opencl_ctx.program = av_opencl_compile("avfilter_transform", NULL);
  110. if (!deshake->opencl_ctx.program) {
  111. av_log(ctx, AV_LOG_ERROR, "OpenCL failed to compile program 'avfilter_transform'\n");
  112. return AVERROR(EINVAL);
  113. }
  114. if (!deshake->opencl_ctx.kernel) {
  115. deshake->opencl_ctx.kernel = clCreateKernel(deshake->opencl_ctx.program, "avfilter_transform", &ret);
  116. if (ret != CL_SUCCESS) {
  117. av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel 'avfilter_transform'\n");
  118. return AVERROR(EINVAL);
  119. }
  120. }
  121. return ret;
  122. }
  123. void ff_opencl_deshake_uninit(AVFilterContext *ctx)
  124. {
  125. DeshakeContext *deshake = ctx->priv;
  126. av_opencl_buffer_release(&deshake->opencl_ctx.cl_inbuf);
  127. av_opencl_buffer_release(&deshake->opencl_ctx.cl_outbuf);
  128. av_opencl_buffer_release(&deshake->opencl_ctx.cl_matrix_y);
  129. av_opencl_buffer_release(&deshake->opencl_ctx.cl_matrix_uv);
  130. clReleaseKernel(deshake->opencl_ctx.kernel);
  131. clReleaseProgram(deshake->opencl_ctx.program);
  132. deshake->opencl_ctx.command_queue = NULL;
  133. av_opencl_uninit();
  134. }
  135. int ff_opencl_deshake_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
  136. {
  137. int ret = 0;
  138. AVFilterLink *link = ctx->inputs[0];
  139. DeshakeContext *deshake = ctx->priv;
  140. const int hshift = av_pix_fmt_desc_get(link->format)->log2_chroma_h;
  141. int chroma_height = FF_CEIL_RSHIFT(link->h, hshift);
  142. if ((!deshake->opencl_ctx.cl_inbuf) || (!deshake->opencl_ctx.cl_outbuf)) {
  143. deshake->opencl_ctx.in_plane_size[0] = (in->linesize[0] * in->height);
  144. deshake->opencl_ctx.in_plane_size[1] = (in->linesize[1] * chroma_height);
  145. deshake->opencl_ctx.in_plane_size[2] = (in->linesize[2] * chroma_height);
  146. deshake->opencl_ctx.out_plane_size[0] = (out->linesize[0] * out->height);
  147. deshake->opencl_ctx.out_plane_size[1] = (out->linesize[1] * chroma_height);
  148. deshake->opencl_ctx.out_plane_size[2] = (out->linesize[2] * chroma_height);
  149. deshake->opencl_ctx.cl_inbuf_size = deshake->opencl_ctx.in_plane_size[0] +
  150. deshake->opencl_ctx.in_plane_size[1] +
  151. deshake->opencl_ctx.in_plane_size[2];
  152. deshake->opencl_ctx.cl_outbuf_size = deshake->opencl_ctx.out_plane_size[0] +
  153. deshake->opencl_ctx.out_plane_size[1] +
  154. deshake->opencl_ctx.out_plane_size[2];
  155. if (!deshake->opencl_ctx.cl_inbuf) {
  156. ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_inbuf,
  157. deshake->opencl_ctx.cl_inbuf_size,
  158. CL_MEM_READ_ONLY, NULL);
  159. if (ret < 0)
  160. return ret;
  161. }
  162. if (!deshake->opencl_ctx.cl_outbuf) {
  163. ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_outbuf,
  164. deshake->opencl_ctx.cl_outbuf_size,
  165. CL_MEM_READ_WRITE, NULL);
  166. if (ret < 0)
  167. return ret;
  168. }
  169. }
  170. ret = av_opencl_buffer_write_image(deshake->opencl_ctx.cl_inbuf,
  171. deshake->opencl_ctx.cl_inbuf_size,
  172. 0, in->data,deshake->opencl_ctx.in_plane_size,
  173. deshake->opencl_ctx.plane_num);
  174. if(ret < 0)
  175. return ret;
  176. return ret;
  177. }