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.

279 lines
7.6KB

  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 <stdio.h>
  19. #include <string.h>
  20. #include "libavutil/hwcontext.h"
  21. #include "libavutil/hwcontext_opencl.h"
  22. #include "libavutil/mem.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "opencl.h"
  26. int ff_opencl_filter_query_formats(AVFilterContext *avctx)
  27. {
  28. const static enum AVPixelFormat pix_fmts[] = {
  29. AV_PIX_FMT_OPENCL,
  30. AV_PIX_FMT_NONE,
  31. };
  32. AVFilterFormats *formats;
  33. formats = ff_make_format_list(pix_fmts);
  34. if (!formats)
  35. return AVERROR(ENOMEM);
  36. return ff_set_common_formats(avctx, formats);
  37. }
  38. static int opencl_filter_set_device(AVFilterContext *avctx,
  39. AVBufferRef *device)
  40. {
  41. OpenCLFilterContext *ctx = avctx->priv;
  42. av_buffer_unref(&ctx->device_ref);
  43. ctx->device_ref = av_buffer_ref(device);
  44. if (!ctx->device_ref)
  45. return AVERROR(ENOMEM);
  46. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  47. ctx->hwctx = ctx->device->hwctx;
  48. return 0;
  49. }
  50. int ff_opencl_filter_config_input(AVFilterLink *inlink)
  51. {
  52. AVFilterContext *avctx = inlink->dst;
  53. OpenCLFilterContext *ctx = avctx->priv;
  54. AVHWFramesContext *input_frames;
  55. int err;
  56. if (!inlink->hw_frames_ctx) {
  57. av_log(avctx, AV_LOG_ERROR, "OpenCL filtering requires a "
  58. "hardware frames context on the input.\n");
  59. return AVERROR(EINVAL);
  60. }
  61. // Extract the device and default output format from the first input.
  62. if (avctx->inputs[0] != inlink)
  63. return 0;
  64. input_frames = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
  65. if (input_frames->format != AV_PIX_FMT_OPENCL)
  66. return AVERROR(EINVAL);
  67. err = opencl_filter_set_device(avctx, input_frames->device_ref);
  68. if (err < 0)
  69. return err;
  70. // Default output parameters match input parameters.
  71. if (ctx->output_format == AV_PIX_FMT_NONE)
  72. ctx->output_format = input_frames->sw_format;
  73. if (!ctx->output_width)
  74. ctx->output_width = inlink->w;
  75. if (!ctx->output_height)
  76. ctx->output_height = inlink->h;
  77. return 0;
  78. }
  79. int ff_opencl_filter_config_output(AVFilterLink *outlink)
  80. {
  81. AVFilterContext *avctx = outlink->src;
  82. OpenCLFilterContext *ctx = avctx->priv;
  83. AVBufferRef *output_frames_ref = NULL;
  84. AVHWFramesContext *output_frames;
  85. int err;
  86. av_buffer_unref(&outlink->hw_frames_ctx);
  87. if (!ctx->device_ref) {
  88. if (!avctx->hw_device_ctx) {
  89. av_log(avctx, AV_LOG_ERROR, "OpenCL filtering requires an "
  90. "OpenCL device.\n");
  91. return AVERROR(EINVAL);
  92. }
  93. err = opencl_filter_set_device(avctx, avctx->hw_device_ctx);
  94. if (err < 0)
  95. return err;
  96. }
  97. output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
  98. if (!output_frames_ref) {
  99. err = AVERROR(ENOMEM);
  100. goto fail;
  101. }
  102. output_frames = (AVHWFramesContext*)output_frames_ref->data;
  103. output_frames->format = AV_PIX_FMT_OPENCL;
  104. output_frames->sw_format = ctx->output_format;
  105. output_frames->width = ctx->output_width;
  106. output_frames->height = ctx->output_height;
  107. err = av_hwframe_ctx_init(output_frames_ref);
  108. if (err < 0) {
  109. av_log(avctx, AV_LOG_ERROR, "Failed to initialise output "
  110. "frames: %d.\n", err);
  111. goto fail;
  112. }
  113. outlink->hw_frames_ctx = output_frames_ref;
  114. outlink->w = ctx->output_width;
  115. outlink->h = ctx->output_height;
  116. return 0;
  117. fail:
  118. av_buffer_unref(&output_frames_ref);
  119. return err;
  120. }
  121. int ff_opencl_filter_init(AVFilterContext *avctx)
  122. {
  123. OpenCLFilterContext *ctx = avctx->priv;
  124. ctx->output_format = AV_PIX_FMT_NONE;
  125. return 0;
  126. }
  127. void ff_opencl_filter_uninit(AVFilterContext *avctx)
  128. {
  129. OpenCLFilterContext *ctx = avctx->priv;
  130. cl_int cle;
  131. if (ctx->program) {
  132. cle = clReleaseProgram(ctx->program);
  133. if (cle != CL_SUCCESS)
  134. av_log(avctx, AV_LOG_ERROR, "Failed to release "
  135. "program: %d.\n", cle);
  136. }
  137. av_buffer_unref(&ctx->device_ref);
  138. }
  139. int ff_opencl_filter_load_program(AVFilterContext *avctx,
  140. const char **program_source_array,
  141. int nb_strings)
  142. {
  143. OpenCLFilterContext *ctx = avctx->priv;
  144. cl_int cle;
  145. ctx->program = clCreateProgramWithSource(ctx->hwctx->context, nb_strings,
  146. program_source_array,
  147. NULL, &cle);
  148. if (!ctx->program) {
  149. av_log(avctx, AV_LOG_ERROR, "Failed to create program: %d.\n", cle);
  150. return AVERROR(EIO);
  151. }
  152. cle = clBuildProgram(ctx->program, 1, &ctx->hwctx->device_id,
  153. NULL, NULL, NULL);
  154. if (cle != CL_SUCCESS) {
  155. av_log(avctx, AV_LOG_ERROR, "Failed to build program: %d.\n", cle);
  156. if (cle == CL_BUILD_PROGRAM_FAILURE) {
  157. char *log;
  158. size_t log_length;
  159. clGetProgramBuildInfo(ctx->program, ctx->hwctx->device_id,
  160. CL_PROGRAM_BUILD_LOG, 0, NULL, &log_length);
  161. log = av_malloc(log_length);
  162. if (log) {
  163. cle = clGetProgramBuildInfo(ctx->program,
  164. ctx->hwctx->device_id,
  165. CL_PROGRAM_BUILD_LOG,
  166. log_length, log, NULL);
  167. if (cle == CL_SUCCESS)
  168. av_log(avctx, AV_LOG_ERROR, "Build log:\n%s\n", log);
  169. }
  170. av_free(log);
  171. }
  172. clReleaseProgram(ctx->program);
  173. ctx->program = NULL;
  174. return AVERROR(EIO);
  175. }
  176. return 0;
  177. }
  178. int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx,
  179. const char *filename)
  180. {
  181. FILE *file;
  182. char *src = NULL;
  183. size_t pos, len, rb;
  184. const char *src_const;
  185. int err;
  186. file = fopen(filename, "r");
  187. if (!file) {
  188. av_log(avctx, AV_LOG_ERROR, "Unable to open program "
  189. "source file \"%s\".\n", filename);
  190. return AVERROR(ENOENT);
  191. }
  192. len = 1 << 16;
  193. pos = 0;
  194. err = av_reallocp(&src, len);
  195. if (err < 0)
  196. goto fail;
  197. err = snprintf(src, len, "#line 1 \"%s\"\n", filename);
  198. if (err < 0) {
  199. err = AVERROR(errno);
  200. goto fail;
  201. }
  202. if (err > len / 2) {
  203. err = AVERROR(EINVAL);
  204. goto fail;
  205. }
  206. pos = err;
  207. while (1) {
  208. rb = fread(src + pos, 1, len - pos - 1, file);
  209. if (rb == 0 && ferror(file)) {
  210. err = AVERROR(EIO);
  211. goto fail;
  212. }
  213. pos += rb;
  214. if (pos < len)
  215. break;
  216. len <<= 1;
  217. err = av_reallocp(&src, len);
  218. if (err < 0)
  219. goto fail;
  220. }
  221. src[pos] = 0;
  222. src_const = src;
  223. err = ff_opencl_filter_load_program(avctx, &src_const, 1);
  224. fail:
  225. fclose(file);
  226. av_freep(&src);
  227. return err;
  228. }