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.

413 lines
12KB

  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 "buffer.h"
  19. #include "common.h"
  20. #include "hwcontext.h"
  21. #include "hwcontext_internal.h"
  22. #include "hwcontext_cuda_internal.h"
  23. #include "mem.h"
  24. #include "pixdesc.h"
  25. #include "pixfmt.h"
  26. #define CUDA_FRAME_ALIGNMENT 256
  27. typedef struct CUDAFramesContext {
  28. int shift_width, shift_height;
  29. } CUDAFramesContext;
  30. static const enum AVPixelFormat supported_formats[] = {
  31. AV_PIX_FMT_NV12,
  32. AV_PIX_FMT_YUV420P,
  33. AV_PIX_FMT_YUV444P,
  34. AV_PIX_FMT_P010,
  35. AV_PIX_FMT_P016,
  36. };
  37. static int cuda_frames_get_constraints(AVHWDeviceContext *ctx,
  38. const void *hwconfig,
  39. AVHWFramesConstraints *constraints)
  40. {
  41. int i;
  42. constraints->valid_sw_formats = av_malloc_array(FF_ARRAY_ELEMS(supported_formats) + 1,
  43. sizeof(*constraints->valid_sw_formats));
  44. if (!constraints->valid_sw_formats)
  45. return AVERROR(ENOMEM);
  46. for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
  47. constraints->valid_sw_formats[i] = supported_formats[i];
  48. constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_formats)] = AV_PIX_FMT_NONE;
  49. constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
  50. if (!constraints->valid_hw_formats)
  51. return AVERROR(ENOMEM);
  52. constraints->valid_hw_formats[0] = AV_PIX_FMT_CUDA;
  53. constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
  54. return 0;
  55. }
  56. static void cuda_buffer_free(void *opaque, uint8_t *data)
  57. {
  58. AVHWFramesContext *ctx = opaque;
  59. AVCUDADeviceContext *hwctx = ctx->device_ctx->hwctx;
  60. CudaFunctions *cu = hwctx->internal->cuda_dl;
  61. CUcontext dummy;
  62. cu->cuCtxPushCurrent(hwctx->cuda_ctx);
  63. cu->cuMemFree((CUdeviceptr)data);
  64. cu->cuCtxPopCurrent(&dummy);
  65. }
  66. static AVBufferRef *cuda_pool_alloc(void *opaque, int size)
  67. {
  68. AVHWFramesContext *ctx = opaque;
  69. AVCUDADeviceContext *hwctx = ctx->device_ctx->hwctx;
  70. CudaFunctions *cu = hwctx->internal->cuda_dl;
  71. AVBufferRef *ret = NULL;
  72. CUcontext dummy = NULL;
  73. CUdeviceptr data;
  74. CUresult err;
  75. err = cu->cuCtxPushCurrent(hwctx->cuda_ctx);
  76. if (err != CUDA_SUCCESS) {
  77. av_log(ctx, AV_LOG_ERROR, "Error setting current CUDA context\n");
  78. return NULL;
  79. }
  80. err = cu->cuMemAlloc(&data, size);
  81. if (err != CUDA_SUCCESS)
  82. goto fail;
  83. ret = av_buffer_create((uint8_t*)data, size, cuda_buffer_free, ctx, 0);
  84. if (!ret) {
  85. cu->cuMemFree(data);
  86. goto fail;
  87. }
  88. fail:
  89. cu->cuCtxPopCurrent(&dummy);
  90. return ret;
  91. }
  92. static int cuda_frames_init(AVHWFramesContext *ctx)
  93. {
  94. CUDAFramesContext *priv = ctx->internal->priv;
  95. int aligned_width = FFALIGN(ctx->width, CUDA_FRAME_ALIGNMENT);
  96. int i;
  97. for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
  98. if (ctx->sw_format == supported_formats[i])
  99. break;
  100. }
  101. if (i == FF_ARRAY_ELEMS(supported_formats)) {
  102. av_log(ctx, AV_LOG_ERROR, "Pixel format '%s' is not supported\n",
  103. av_get_pix_fmt_name(ctx->sw_format));
  104. return AVERROR(ENOSYS);
  105. }
  106. av_pix_fmt_get_chroma_sub_sample(ctx->sw_format, &priv->shift_width, &priv->shift_height);
  107. if (!ctx->pool) {
  108. int size;
  109. switch (ctx->sw_format) {
  110. case AV_PIX_FMT_NV12:
  111. case AV_PIX_FMT_YUV420P:
  112. size = aligned_width * ctx->height * 3 / 2;
  113. break;
  114. case AV_PIX_FMT_YUV444P:
  115. case AV_PIX_FMT_P010:
  116. case AV_PIX_FMT_P016:
  117. size = aligned_width * ctx->height * 3;
  118. break;
  119. default:
  120. av_log(ctx, AV_LOG_ERROR, "BUG: Pixel format missing from size calculation.");
  121. return AVERROR_BUG;
  122. }
  123. ctx->internal->pool_internal = av_buffer_pool_init2(size, ctx, cuda_pool_alloc, NULL);
  124. if (!ctx->internal->pool_internal)
  125. return AVERROR(ENOMEM);
  126. }
  127. return 0;
  128. }
  129. static int cuda_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
  130. {
  131. int aligned_width;
  132. int width_in_bytes = ctx->width;
  133. if (ctx->sw_format == AV_PIX_FMT_P010 ||
  134. ctx->sw_format == AV_PIX_FMT_P016) {
  135. width_in_bytes *= 2;
  136. }
  137. aligned_width = FFALIGN(width_in_bytes, CUDA_FRAME_ALIGNMENT);
  138. frame->buf[0] = av_buffer_pool_get(ctx->pool);
  139. if (!frame->buf[0])
  140. return AVERROR(ENOMEM);
  141. switch (ctx->sw_format) {
  142. case AV_PIX_FMT_NV12:
  143. case AV_PIX_FMT_P010:
  144. case AV_PIX_FMT_P016:
  145. frame->data[0] = frame->buf[0]->data;
  146. frame->data[1] = frame->data[0] + aligned_width * ctx->height;
  147. frame->linesize[0] = aligned_width;
  148. frame->linesize[1] = aligned_width;
  149. break;
  150. case AV_PIX_FMT_YUV420P:
  151. frame->data[0] = frame->buf[0]->data;
  152. frame->data[2] = frame->data[0] + aligned_width * ctx->height;
  153. frame->data[1] = frame->data[2] + aligned_width * ctx->height / 4;
  154. frame->linesize[0] = aligned_width;
  155. frame->linesize[1] = aligned_width / 2;
  156. frame->linesize[2] = aligned_width / 2;
  157. break;
  158. case AV_PIX_FMT_YUV444P:
  159. frame->data[0] = frame->buf[0]->data;
  160. frame->data[1] = frame->data[0] + aligned_width * ctx->height;
  161. frame->data[2] = frame->data[1] + aligned_width * ctx->height;
  162. frame->linesize[0] = aligned_width;
  163. frame->linesize[1] = aligned_width;
  164. frame->linesize[2] = aligned_width;
  165. break;
  166. default:
  167. av_frame_unref(frame);
  168. return AVERROR_BUG;
  169. }
  170. frame->format = AV_PIX_FMT_CUDA;
  171. frame->width = ctx->width;
  172. frame->height = ctx->height;
  173. return 0;
  174. }
  175. static int cuda_transfer_get_formats(AVHWFramesContext *ctx,
  176. enum AVHWFrameTransferDirection dir,
  177. enum AVPixelFormat **formats)
  178. {
  179. enum AVPixelFormat *fmts;
  180. fmts = av_malloc_array(2, sizeof(*fmts));
  181. if (!fmts)
  182. return AVERROR(ENOMEM);
  183. fmts[0] = ctx->sw_format;
  184. fmts[1] = AV_PIX_FMT_NONE;
  185. *formats = fmts;
  186. return 0;
  187. }
  188. static int cuda_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
  189. const AVFrame *src)
  190. {
  191. CUDAFramesContext *priv = ctx->internal->priv;
  192. AVCUDADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  193. CudaFunctions *cu = device_hwctx->internal->cuda_dl;
  194. CUcontext dummy;
  195. CUresult err;
  196. int i;
  197. err = cu->cuCtxPushCurrent(device_hwctx->cuda_ctx);
  198. if (err != CUDA_SUCCESS)
  199. return AVERROR_UNKNOWN;
  200. for (i = 0; i < FF_ARRAY_ELEMS(src->data) && src->data[i]; i++) {
  201. CUDA_MEMCPY2D cpy = {
  202. .srcMemoryType = CU_MEMORYTYPE_DEVICE,
  203. .dstMemoryType = CU_MEMORYTYPE_HOST,
  204. .srcDevice = (CUdeviceptr)src->data[i],
  205. .dstHost = dst->data[i],
  206. .srcPitch = src->linesize[i],
  207. .dstPitch = dst->linesize[i],
  208. .WidthInBytes = FFMIN(src->linesize[i], dst->linesize[i]),
  209. .Height = src->height >> (i ? priv->shift_height : 0),
  210. };
  211. err = cu->cuMemcpy2D(&cpy);
  212. if (err != CUDA_SUCCESS) {
  213. av_log(ctx, AV_LOG_ERROR, "Error transferring the data from the CUDA frame\n");
  214. return AVERROR_UNKNOWN;
  215. }
  216. }
  217. cu->cuCtxPopCurrent(&dummy);
  218. return 0;
  219. }
  220. static int cuda_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
  221. const AVFrame *src)
  222. {
  223. CUDAFramesContext *priv = ctx->internal->priv;
  224. AVCUDADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  225. CudaFunctions *cu = device_hwctx->internal->cuda_dl;
  226. CUcontext dummy;
  227. CUresult err;
  228. int i;
  229. err = cu->cuCtxPushCurrent(device_hwctx->cuda_ctx);
  230. if (err != CUDA_SUCCESS)
  231. return AVERROR_UNKNOWN;
  232. for (i = 0; i < FF_ARRAY_ELEMS(src->data) && src->data[i]; i++) {
  233. CUDA_MEMCPY2D cpy = {
  234. .srcMemoryType = CU_MEMORYTYPE_HOST,
  235. .dstMemoryType = CU_MEMORYTYPE_DEVICE,
  236. .srcHost = src->data[i],
  237. .dstDevice = (CUdeviceptr)dst->data[i],
  238. .srcPitch = src->linesize[i],
  239. .dstPitch = dst->linesize[i],
  240. .WidthInBytes = FFMIN(src->linesize[i], dst->linesize[i]),
  241. .Height = src->height >> (i ? priv->shift_height : 0),
  242. };
  243. err = cu->cuMemcpy2D(&cpy);
  244. if (err != CUDA_SUCCESS) {
  245. av_log(ctx, AV_LOG_ERROR, "Error transferring the data from the CUDA frame\n");
  246. return AVERROR_UNKNOWN;
  247. }
  248. }
  249. cu->cuCtxPopCurrent(&dummy);
  250. return 0;
  251. }
  252. static void cuda_device_uninit(AVHWDeviceContext *ctx)
  253. {
  254. AVCUDADeviceContext *hwctx = ctx->hwctx;
  255. if (hwctx->internal) {
  256. if (hwctx->internal->is_allocated && hwctx->cuda_ctx) {
  257. hwctx->internal->cuda_dl->cuCtxDestroy(hwctx->cuda_ctx);
  258. hwctx->cuda_ctx = NULL;
  259. }
  260. cuda_free_functions(&hwctx->internal->cuda_dl);
  261. }
  262. av_freep(&hwctx->internal);
  263. }
  264. static int cuda_device_init(AVHWDeviceContext *ctx)
  265. {
  266. AVCUDADeviceContext *hwctx = ctx->hwctx;
  267. int ret;
  268. if (!hwctx->internal) {
  269. hwctx->internal = av_mallocz(sizeof(*hwctx->internal));
  270. if (!hwctx->internal)
  271. return AVERROR(ENOMEM);
  272. }
  273. if (!hwctx->internal->cuda_dl) {
  274. ret = cuda_load_functions(&hwctx->internal->cuda_dl);
  275. if (ret < 0) {
  276. av_log(ctx, AV_LOG_ERROR, "Could not dynamically load CUDA\n");
  277. goto error;
  278. }
  279. }
  280. return 0;
  281. error:
  282. cuda_device_uninit(ctx);
  283. return ret;
  284. }
  285. static int cuda_device_create(AVHWDeviceContext *ctx, const char *device,
  286. AVDictionary *opts, int flags)
  287. {
  288. AVCUDADeviceContext *hwctx = ctx->hwctx;
  289. CudaFunctions *cu;
  290. CUdevice cu_device;
  291. CUcontext dummy;
  292. CUresult err;
  293. int device_idx = 0;
  294. if (device)
  295. device_idx = strtol(device, NULL, 0);
  296. if (cuda_device_init(ctx) < 0)
  297. goto error;
  298. cu = hwctx->internal->cuda_dl;
  299. err = cu->cuInit(0);
  300. if (err != CUDA_SUCCESS) {
  301. av_log(ctx, AV_LOG_ERROR, "Could not initialize the CUDA driver API\n");
  302. goto error;
  303. }
  304. err = cu->cuDeviceGet(&cu_device, device_idx);
  305. if (err != CUDA_SUCCESS) {
  306. av_log(ctx, AV_LOG_ERROR, "Could not get the device number %d\n", device_idx);
  307. goto error;
  308. }
  309. err = cu->cuCtxCreate(&hwctx->cuda_ctx, CU_CTX_SCHED_BLOCKING_SYNC, cu_device);
  310. if (err != CUDA_SUCCESS) {
  311. av_log(ctx, AV_LOG_ERROR, "Error creating a CUDA context\n");
  312. goto error;
  313. }
  314. cu->cuCtxPopCurrent(&dummy);
  315. hwctx->internal->is_allocated = 1;
  316. return 0;
  317. error:
  318. cuda_device_uninit(ctx);
  319. return AVERROR_UNKNOWN;
  320. }
  321. const HWContextType ff_hwcontext_type_cuda = {
  322. .type = AV_HWDEVICE_TYPE_CUDA,
  323. .name = "CUDA",
  324. .device_hwctx_size = sizeof(AVCUDADeviceContext),
  325. .frames_priv_size = sizeof(CUDAFramesContext),
  326. .device_create = cuda_device_create,
  327. .device_init = cuda_device_init,
  328. .device_uninit = cuda_device_uninit,
  329. .frames_get_constraints = cuda_frames_get_constraints,
  330. .frames_init = cuda_frames_init,
  331. .frames_get_buffer = cuda_get_buffer,
  332. .transfer_get_formats = cuda_transfer_get_formats,
  333. .transfer_data_to = cuda_transfer_data_to,
  334. .transfer_data_from = cuda_transfer_data_from,
  335. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE },
  336. };