hwcontext_cuda.c 12 KB

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