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.

343 lines
9.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 "libavutil/pixdesc.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "opencl.h"
  27. int ff_opencl_filter_query_formats(AVFilterContext *avctx)
  28. {
  29. const static enum AVPixelFormat pix_fmts[] = {
  30. AV_PIX_FMT_OPENCL,
  31. AV_PIX_FMT_NONE,
  32. };
  33. AVFilterFormats *formats;
  34. formats = ff_make_format_list(pix_fmts);
  35. if (!formats)
  36. return AVERROR(ENOMEM);
  37. return ff_set_common_formats(avctx, formats);
  38. }
  39. static int opencl_filter_set_device(AVFilterContext *avctx,
  40. AVBufferRef *device)
  41. {
  42. OpenCLFilterContext *ctx = avctx->priv;
  43. av_buffer_unref(&ctx->device_ref);
  44. ctx->device_ref = av_buffer_ref(device);
  45. if (!ctx->device_ref)
  46. return AVERROR(ENOMEM);
  47. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  48. ctx->hwctx = ctx->device->hwctx;
  49. return 0;
  50. }
  51. int ff_opencl_filter_config_input(AVFilterLink *inlink)
  52. {
  53. AVFilterContext *avctx = inlink->dst;
  54. OpenCLFilterContext *ctx = avctx->priv;
  55. AVHWFramesContext *input_frames;
  56. int err;
  57. if (!inlink->hw_frames_ctx) {
  58. av_log(avctx, AV_LOG_ERROR, "OpenCL filtering requires a "
  59. "hardware frames context on the input.\n");
  60. return AVERROR(EINVAL);
  61. }
  62. // Extract the device and default output format from the first input.
  63. if (avctx->inputs[0] != inlink)
  64. return 0;
  65. input_frames = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
  66. if (input_frames->format != AV_PIX_FMT_OPENCL)
  67. return AVERROR(EINVAL);
  68. err = opencl_filter_set_device(avctx, input_frames->device_ref);
  69. if (err < 0)
  70. return err;
  71. // Default output parameters match input parameters.
  72. if (ctx->output_format == AV_PIX_FMT_NONE)
  73. ctx->output_format = input_frames->sw_format;
  74. if (!ctx->output_width)
  75. ctx->output_width = inlink->w;
  76. if (!ctx->output_height)
  77. ctx->output_height = inlink->h;
  78. return 0;
  79. }
  80. int ff_opencl_filter_config_output(AVFilterLink *outlink)
  81. {
  82. AVFilterContext *avctx = outlink->src;
  83. OpenCLFilterContext *ctx = avctx->priv;
  84. AVBufferRef *output_frames_ref = NULL;
  85. AVHWFramesContext *output_frames;
  86. int err;
  87. av_buffer_unref(&outlink->hw_frames_ctx);
  88. if (!ctx->device_ref) {
  89. if (!avctx->hw_device_ctx) {
  90. av_log(avctx, AV_LOG_ERROR, "OpenCL filtering requires an "
  91. "OpenCL device.\n");
  92. return AVERROR(EINVAL);
  93. }
  94. err = opencl_filter_set_device(avctx, avctx->hw_device_ctx);
  95. if (err < 0)
  96. return err;
  97. }
  98. output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
  99. if (!output_frames_ref) {
  100. err = AVERROR(ENOMEM);
  101. goto fail;
  102. }
  103. output_frames = (AVHWFramesContext*)output_frames_ref->data;
  104. output_frames->format = AV_PIX_FMT_OPENCL;
  105. output_frames->sw_format = ctx->output_format;
  106. output_frames->width = ctx->output_width;
  107. output_frames->height = ctx->output_height;
  108. err = av_hwframe_ctx_init(output_frames_ref);
  109. if (err < 0) {
  110. av_log(avctx, AV_LOG_ERROR, "Failed to initialise output "
  111. "frames: %d.\n", err);
  112. goto fail;
  113. }
  114. outlink->hw_frames_ctx = output_frames_ref;
  115. outlink->w = ctx->output_width;
  116. outlink->h = ctx->output_height;
  117. return 0;
  118. fail:
  119. av_buffer_unref(&output_frames_ref);
  120. return err;
  121. }
  122. int ff_opencl_filter_init(AVFilterContext *avctx)
  123. {
  124. OpenCLFilterContext *ctx = avctx->priv;
  125. ctx->output_format = AV_PIX_FMT_NONE;
  126. return 0;
  127. }
  128. void ff_opencl_filter_uninit(AVFilterContext *avctx)
  129. {
  130. OpenCLFilterContext *ctx = avctx->priv;
  131. cl_int cle;
  132. if (ctx->program) {
  133. cle = clReleaseProgram(ctx->program);
  134. if (cle != CL_SUCCESS)
  135. av_log(avctx, AV_LOG_ERROR, "Failed to release "
  136. "program: %d.\n", cle);
  137. }
  138. av_buffer_unref(&ctx->device_ref);
  139. }
  140. int ff_opencl_filter_load_program(AVFilterContext *avctx,
  141. const char **program_source_array,
  142. int nb_strings)
  143. {
  144. OpenCLFilterContext *ctx = avctx->priv;
  145. cl_int cle;
  146. ctx->program = clCreateProgramWithSource(ctx->hwctx->context, nb_strings,
  147. program_source_array,
  148. NULL, &cle);
  149. if (!ctx->program) {
  150. av_log(avctx, AV_LOG_ERROR, "Failed to create program: %d.\n", cle);
  151. return AVERROR(EIO);
  152. }
  153. cle = clBuildProgram(ctx->program, 1, &ctx->hwctx->device_id,
  154. NULL, NULL, NULL);
  155. if (cle != CL_SUCCESS) {
  156. av_log(avctx, AV_LOG_ERROR, "Failed to build program: %d.\n", cle);
  157. if (cle == CL_BUILD_PROGRAM_FAILURE) {
  158. char *log;
  159. size_t log_length;
  160. clGetProgramBuildInfo(ctx->program, ctx->hwctx->device_id,
  161. CL_PROGRAM_BUILD_LOG, 0, NULL, &log_length);
  162. log = av_malloc(log_length);
  163. if (log) {
  164. cle = clGetProgramBuildInfo(ctx->program,
  165. ctx->hwctx->device_id,
  166. CL_PROGRAM_BUILD_LOG,
  167. log_length, log, NULL);
  168. if (cle == CL_SUCCESS)
  169. av_log(avctx, AV_LOG_ERROR, "Build log:\n%s\n", log);
  170. }
  171. av_free(log);
  172. }
  173. clReleaseProgram(ctx->program);
  174. ctx->program = NULL;
  175. return AVERROR(EIO);
  176. }
  177. return 0;
  178. }
  179. int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx,
  180. const char *filename)
  181. {
  182. FILE *file;
  183. char *src = NULL;
  184. size_t pos, len, rb;
  185. const char *src_const;
  186. int err;
  187. file = fopen(filename, "r");
  188. if (!file) {
  189. av_log(avctx, AV_LOG_ERROR, "Unable to open program "
  190. "source file \"%s\".\n", filename);
  191. return AVERROR(ENOENT);
  192. }
  193. len = 1 << 16;
  194. pos = 0;
  195. err = av_reallocp(&src, len);
  196. if (err < 0)
  197. goto fail;
  198. err = snprintf(src, len, "#line 1 \"%s\"\n", filename);
  199. if (err < 0) {
  200. err = AVERROR(errno);
  201. goto fail;
  202. }
  203. if (err > len / 2) {
  204. err = AVERROR(EINVAL);
  205. goto fail;
  206. }
  207. pos = err;
  208. while (1) {
  209. rb = fread(src + pos, 1, len - pos - 1, file);
  210. if (rb == 0 && ferror(file)) {
  211. err = AVERROR(EIO);
  212. goto fail;
  213. }
  214. pos += rb;
  215. if (pos < len)
  216. break;
  217. len <<= 1;
  218. err = av_reallocp(&src, len);
  219. if (err < 0)
  220. goto fail;
  221. }
  222. src[pos] = 0;
  223. src_const = src;
  224. err = ff_opencl_filter_load_program(avctx, &src_const, 1);
  225. fail:
  226. fclose(file);
  227. av_freep(&src);
  228. return err;
  229. }
  230. int ff_opencl_filter_work_size_from_image(AVFilterContext *avctx,
  231. size_t *work_size,
  232. AVFrame *frame, int plane,
  233. int block_alignment)
  234. {
  235. cl_mem image;
  236. cl_mem_object_type type;
  237. size_t width, height;
  238. cl_int cle;
  239. if (frame->format != AV_PIX_FMT_OPENCL) {
  240. av_log(avctx, AV_LOG_ERROR, "Invalid frame format %s, "
  241. "opencl required.\n", av_get_pix_fmt_name(frame->format));
  242. return AVERROR(EINVAL);
  243. }
  244. image = (cl_mem)frame->data[plane];
  245. if (!image) {
  246. av_log(avctx, AV_LOG_ERROR, "Plane %d required but not set.\n",
  247. plane);
  248. return AVERROR(EINVAL);
  249. }
  250. cle = clGetMemObjectInfo(image, CL_MEM_TYPE, sizeof(type),
  251. &type, NULL);
  252. if (cle != CL_SUCCESS) {
  253. av_log(avctx, AV_LOG_ERROR, "Failed to query object type of "
  254. "plane %d: %d.\n", plane, cle);
  255. return AVERROR_UNKNOWN;
  256. }
  257. if (type != CL_MEM_OBJECT_IMAGE2D) {
  258. av_log(avctx, AV_LOG_ERROR, "Plane %d is not a 2D image.\n",
  259. plane);
  260. return AVERROR(EINVAL);
  261. }
  262. cle = clGetImageInfo(image, CL_IMAGE_WIDTH, sizeof(size_t),
  263. &width, NULL);
  264. if (cle != CL_SUCCESS) {
  265. av_log(avctx, AV_LOG_ERROR, "Failed to query plane %d width: %d.\n",
  266. plane, cle);
  267. return AVERROR_UNKNOWN;
  268. }
  269. cle = clGetImageInfo(image, CL_IMAGE_HEIGHT, sizeof(size_t),
  270. &height, NULL);
  271. if (cle != CL_SUCCESS) {
  272. av_log(avctx, AV_LOG_ERROR, "Failed to query plane %d height: %d.\n",
  273. plane, cle);
  274. return AVERROR_UNKNOWN;
  275. }
  276. if (block_alignment) {
  277. width = FFALIGN(width, block_alignment);
  278. height = FFALIGN(height, block_alignment);
  279. }
  280. work_size[0] = width;
  281. work_size[1] = height;
  282. return 0;
  283. }