hwcontext_cuda.c 11 KB

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