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.

252 lines
6.9KB

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