2 * This file is part of FFmpeg.
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.
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.
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
21 #include "hwcontext.h"
22 #include "hwcontext_internal.h"
23 #include "hwcontext_cuda_internal.h"
28 #define CUDA_FRAME_ALIGNMENT 256
30 typedef struct CUDAFramesContext {
31 int shift_width, shift_height;
34 static const enum AVPixelFormat supported_formats[] = {
42 static int cuda_frames_get_constraints(AVHWDeviceContext *ctx,
44 AVHWFramesConstraints *constraints)
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);
53 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
54 constraints->valid_sw_formats[i] = supported_formats[i];
55 constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_formats)] = AV_PIX_FMT_NONE;
57 constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
58 if (!constraints->valid_hw_formats)
59 return AVERROR(ENOMEM);
61 constraints->valid_hw_formats[0] = AV_PIX_FMT_CUDA;
62 constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
67 static void cuda_buffer_free(void *opaque, uint8_t *data)
69 AVHWFramesContext *ctx = opaque;
70 AVCUDADeviceContext *hwctx = ctx->device_ctx->hwctx;
71 CudaFunctions *cu = hwctx->internal->cuda_dl;
75 cu->cuCtxPushCurrent(hwctx->cuda_ctx);
77 cu->cuMemFree((CUdeviceptr)data);
79 cu->cuCtxPopCurrent(&dummy);
82 static AVBufferRef *cuda_pool_alloc(void *opaque, int size)
84 AVHWFramesContext *ctx = opaque;
85 AVCUDADeviceContext *hwctx = ctx->device_ctx->hwctx;
86 CudaFunctions *cu = hwctx->internal->cuda_dl;
88 AVBufferRef *ret = NULL;
89 CUcontext dummy = NULL;
93 err = cu->cuCtxPushCurrent(hwctx->cuda_ctx);
94 if (err != CUDA_SUCCESS) {
95 av_log(ctx, AV_LOG_ERROR, "Error setting current CUDA context\n");
99 err = cu->cuMemAlloc(&data, size);
100 if (err != CUDA_SUCCESS)
103 ret = av_buffer_create((uint8_t*)data, size, cuda_buffer_free, ctx, 0);
110 cu->cuCtxPopCurrent(&dummy);
114 static int cuda_frames_init(AVHWFramesContext *ctx)
116 CUDAFramesContext *priv = ctx->internal->priv;
117 int aligned_width = FFALIGN(ctx->width, CUDA_FRAME_ALIGNMENT);
120 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
121 if (ctx->sw_format == supported_formats[i])
124 if (i == FF_ARRAY_ELEMS(supported_formats)) {
125 av_log(ctx, AV_LOG_ERROR, "Pixel format '%s' is not supported\n",
126 av_get_pix_fmt_name(ctx->sw_format));
127 return AVERROR(ENOSYS);
130 av_pix_fmt_get_chroma_sub_sample(ctx->sw_format, &priv->shift_width, &priv->shift_height);
135 switch (ctx->sw_format) {
136 case AV_PIX_FMT_NV12:
137 case AV_PIX_FMT_YUV420P:
138 size = aligned_width * ctx->height * 3 / 2;
140 case AV_PIX_FMT_YUV444P:
141 case AV_PIX_FMT_P010:
142 case AV_PIX_FMT_P016:
143 size = aligned_width * ctx->height * 3;
146 av_log(ctx, AV_LOG_ERROR, "BUG: Pixel format missing from size calculation.");
150 ctx->internal->pool_internal = av_buffer_pool_init2(size, ctx, cuda_pool_alloc, NULL);
151 if (!ctx->internal->pool_internal)
152 return AVERROR(ENOMEM);
158 static int cuda_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
161 int width_in_bytes = ctx->width;
163 if (ctx->sw_format == AV_PIX_FMT_P010 ||
164 ctx->sw_format == AV_PIX_FMT_P016) {
167 aligned_width = FFALIGN(width_in_bytes, CUDA_FRAME_ALIGNMENT);
169 frame->buf[0] = av_buffer_pool_get(ctx->pool);
171 return AVERROR(ENOMEM);
173 switch (ctx->sw_format) {
174 case AV_PIX_FMT_NV12:
175 case AV_PIX_FMT_P010:
176 case AV_PIX_FMT_P016:
177 frame->data[0] = frame->buf[0]->data;
178 frame->data[1] = frame->data[0] + aligned_width * ctx->height;
179 frame->linesize[0] = aligned_width;
180 frame->linesize[1] = aligned_width;
182 case AV_PIX_FMT_YUV420P:
183 frame->data[0] = frame->buf[0]->data;
184 frame->data[2] = frame->data[0] + aligned_width * ctx->height;
185 frame->data[1] = frame->data[2] + aligned_width * ctx->height / 4;
186 frame->linesize[0] = aligned_width;
187 frame->linesize[1] = aligned_width / 2;
188 frame->linesize[2] = aligned_width / 2;
190 case AV_PIX_FMT_YUV444P:
191 frame->data[0] = frame->buf[0]->data;
192 frame->data[1] = frame->data[0] + aligned_width * ctx->height;
193 frame->data[2] = frame->data[1] + aligned_width * ctx->height;
194 frame->linesize[0] = aligned_width;
195 frame->linesize[1] = aligned_width;
196 frame->linesize[2] = aligned_width;
199 av_frame_unref(frame);
203 frame->format = AV_PIX_FMT_CUDA;
204 frame->width = ctx->width;
205 frame->height = ctx->height;
210 static int cuda_transfer_get_formats(AVHWFramesContext *ctx,
211 enum AVHWFrameTransferDirection dir,
212 enum AVPixelFormat **formats)
214 enum AVPixelFormat *fmts;
216 fmts = av_malloc_array(2, sizeof(*fmts));
218 return AVERROR(ENOMEM);
220 fmts[0] = ctx->sw_format;
221 fmts[1] = AV_PIX_FMT_NONE;
228 static int cuda_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
231 CUDAFramesContext *priv = ctx->internal->priv;
232 AVCUDADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
233 CudaFunctions *cu = device_hwctx->internal->cuda_dl;
239 err = cu->cuCtxPushCurrent(device_hwctx->cuda_ctx);
240 if (err != CUDA_SUCCESS)
241 return AVERROR_UNKNOWN;
243 for (i = 0; i < FF_ARRAY_ELEMS(src->data) && src->data[i]; i++) {
244 CUDA_MEMCPY2D cpy = {
245 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
246 .dstMemoryType = CU_MEMORYTYPE_HOST,
247 .srcDevice = (CUdeviceptr)src->data[i],
248 .dstHost = dst->data[i],
249 .srcPitch = src->linesize[i],
250 .dstPitch = dst->linesize[i],
251 .WidthInBytes = FFMIN(src->linesize[i], dst->linesize[i]),
252 .Height = src->height >> (i ? priv->shift_height : 0),
255 err = cu->cuMemcpy2D(&cpy);
256 if (err != CUDA_SUCCESS) {
257 av_log(ctx, AV_LOG_ERROR, "Error transferring the data from the CUDA frame\n");
258 return AVERROR_UNKNOWN;
262 cu->cuCtxPopCurrent(&dummy);
267 static int cuda_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
270 CUDAFramesContext *priv = ctx->internal->priv;
271 AVCUDADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
272 CudaFunctions *cu = device_hwctx->internal->cuda_dl;
278 err = cu->cuCtxPushCurrent(device_hwctx->cuda_ctx);
279 if (err != CUDA_SUCCESS)
280 return AVERROR_UNKNOWN;
282 for (i = 0; i < FF_ARRAY_ELEMS(src->data) && src->data[i]; i++) {
283 CUDA_MEMCPY2D cpy = {
284 .srcMemoryType = CU_MEMORYTYPE_HOST,
285 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
286 .srcHost = src->data[i],
287 .dstDevice = (CUdeviceptr)dst->data[i],
288 .srcPitch = src->linesize[i],
289 .dstPitch = dst->linesize[i],
290 .WidthInBytes = FFMIN(src->linesize[i], dst->linesize[i]),
291 .Height = src->height >> (i ? priv->shift_height : 0),
294 err = cu->cuMemcpy2D(&cpy);
295 if (err != CUDA_SUCCESS) {
296 av_log(ctx, AV_LOG_ERROR, "Error transferring the data from the CUDA frame\n");
297 return AVERROR_UNKNOWN;
301 cu->cuCtxPopCurrent(&dummy);
306 static void cuda_device_uninit(AVHWDeviceContext *ctx)
308 AVCUDADeviceContext *hwctx = ctx->hwctx;
310 if (hwctx->internal) {
311 if (hwctx->internal->is_allocated && hwctx->cuda_ctx) {
312 hwctx->internal->cuda_dl->cuCtxDestroy(hwctx->cuda_ctx);
313 hwctx->cuda_ctx = NULL;
315 cuda_free_functions(&hwctx->internal->cuda_dl);
318 av_freep(&hwctx->internal);
321 static int cuda_device_init(AVHWDeviceContext *ctx)
323 AVCUDADeviceContext *hwctx = ctx->hwctx;
326 if (!hwctx->internal) {
327 hwctx->internal = av_mallocz(sizeof(*hwctx->internal));
328 if (!hwctx->internal)
329 return AVERROR(ENOMEM);
332 if (!hwctx->internal->cuda_dl) {
333 ret = cuda_load_functions(&hwctx->internal->cuda_dl);
335 av_log(ctx, AV_LOG_ERROR, "Could not dynamically load CUDA\n");
343 cuda_device_uninit(ctx);
347 static int cuda_device_create(AVHWDeviceContext *ctx, const char *device,
348 AVDictionary *opts, int flags)
350 AVCUDADeviceContext *hwctx = ctx->hwctx;
358 device_idx = strtol(device, NULL, 0);
360 if (cuda_device_init(ctx) < 0)
363 cu = hwctx->internal->cuda_dl;
366 if (err != CUDA_SUCCESS) {
367 av_log(ctx, AV_LOG_ERROR, "Could not initialize the CUDA driver API\n");
371 err = cu->cuDeviceGet(&cu_device, device_idx);
372 if (err != CUDA_SUCCESS) {
373 av_log(ctx, AV_LOG_ERROR, "Could not get the device number %d\n", device_idx);
377 err = cu->cuCtxCreate(&hwctx->cuda_ctx, CU_CTX_SCHED_BLOCKING_SYNC, cu_device);
378 if (err != CUDA_SUCCESS) {
379 av_log(ctx, AV_LOG_ERROR, "Error creating a CUDA context\n");
383 cu->cuCtxPopCurrent(&dummy);
385 hwctx->internal->is_allocated = 1;
390 cuda_device_uninit(ctx);
391 return AVERROR_UNKNOWN;
394 const HWContextType ff_hwcontext_type_cuda = {
395 .type = AV_HWDEVICE_TYPE_CUDA,
398 .device_hwctx_size = sizeof(AVCUDADeviceContext),
399 .frames_priv_size = sizeof(CUDAFramesContext),
401 .device_create = cuda_device_create,
402 .device_init = cuda_device_init,
403 .device_uninit = cuda_device_uninit,
404 .frames_get_constraints = cuda_frames_get_constraints,
405 .frames_init = cuda_frames_init,
406 .frames_get_buffer = cuda_get_buffer,
407 .transfer_get_formats = cuda_transfer_get_formats,
408 .transfer_data_to = cuda_transfer_data_to,
409 .transfer_data_from = cuda_transfer_data_from,
411 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE },