hwcontext_cuda.c 12 KB

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