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.

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