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.

384 lines
11KB

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