2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; 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.h"
28 typedef struct CUDAFramesContext {
29 int shift_width, shift_height;
32 static const enum AVPixelFormat supported_formats[] = {
40 static void cuda_buffer_free(void *opaque, uint8_t *data)
42 AVHWFramesContext *ctx = opaque;
43 AVCUDADeviceContext *hwctx = ctx->device_ctx->hwctx;
47 cuCtxPushCurrent(hwctx->cuda_ctx);
49 cuMemFree((CUdeviceptr)data);
51 cuCtxPopCurrent(&dummy);
54 static AVBufferRef *cuda_pool_alloc(void *opaque, int size)
56 AVHWFramesContext *ctx = opaque;
57 AVCUDADeviceContext *hwctx = ctx->device_ctx->hwctx;
59 AVBufferRef *ret = NULL;
60 CUcontext dummy = NULL;
64 err = cuCtxPushCurrent(hwctx->cuda_ctx);
65 if (err != CUDA_SUCCESS) {
66 av_log(ctx, AV_LOG_ERROR, "Error setting current CUDA context\n");
70 err = cuMemAlloc(&data, size);
71 if (err != CUDA_SUCCESS)
74 ret = av_buffer_create((uint8_t*)data, size, cuda_buffer_free, ctx, 0);
81 cuCtxPopCurrent(&dummy);
85 static int cuda_frames_init(AVHWFramesContext *ctx)
87 CUDAFramesContext *priv = ctx->internal->priv;
90 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
91 if (ctx->sw_format == supported_formats[i])
94 if (i == FF_ARRAY_ELEMS(supported_formats)) {
95 av_log(ctx, AV_LOG_ERROR, "Pixel format '%s' is not supported\n",
96 av_get_pix_fmt_name(ctx->sw_format));
97 return AVERROR(ENOSYS);
100 av_pix_fmt_get_chroma_sub_sample(ctx->sw_format, &priv->shift_width, &priv->shift_height);
105 switch (ctx->sw_format) {
106 case AV_PIX_FMT_NV12:
107 case AV_PIX_FMT_YUV420P:
108 size = ctx->width * ctx->height * 3 / 2;
110 case AV_PIX_FMT_P010:
111 size = ctx->width * ctx->height * 3;
113 case AV_PIX_FMT_YUV444P:
114 size = ctx->width * ctx->height * 3;
116 case AV_PIX_FMT_YUV444P16:
117 size = ctx->width * ctx->height * 6;
121 ctx->internal->pool_internal = av_buffer_pool_init2(size, ctx, cuda_pool_alloc, NULL);
122 if (!ctx->internal->pool_internal)
123 return AVERROR(ENOMEM);
129 static int cuda_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
131 frame->buf[0] = av_buffer_pool_get(ctx->pool);
133 return AVERROR(ENOMEM);
135 switch (ctx->sw_format) {
136 case AV_PIX_FMT_NV12:
137 frame->data[0] = frame->buf[0]->data;
138 frame->data[1] = frame->data[0] + ctx->width * ctx->height;
139 frame->linesize[0] = ctx->width;
140 frame->linesize[1] = ctx->width;
142 case AV_PIX_FMT_YUV420P:
143 frame->data[0] = frame->buf[0]->data;
144 frame->data[2] = frame->data[0] + ctx->width * ctx->height;
145 frame->data[1] = frame->data[2] + ctx->width * ctx->height / 4;
146 frame->linesize[0] = ctx->width;
147 frame->linesize[1] = ctx->width / 2;
148 frame->linesize[2] = ctx->width / 2;
150 case AV_PIX_FMT_P010:
151 frame->data[0] = frame->buf[0]->data;
152 frame->data[1] = frame->data[0] + 2 * ctx->width * ctx->height;
153 frame->linesize[0] = 2 * ctx->width;
154 frame->linesize[1] = 2 * ctx->width;
156 case AV_PIX_FMT_YUV444P:
157 frame->data[0] = frame->buf[0]->data;
158 frame->data[1] = frame->data[0] + ctx->width * ctx->height;
159 frame->data[2] = frame->data[1] + ctx->width * ctx->height;
160 frame->linesize[0] = ctx->width;
161 frame->linesize[1] = ctx->width;
162 frame->linesize[2] = ctx->width;
164 case AV_PIX_FMT_YUV444P16:
165 frame->data[0] = frame->buf[0]->data;
166 frame->data[1] = frame->data[0] + 2 * ctx->width * ctx->height;
167 frame->data[2] = frame->data[1] + 2 * ctx->width * ctx->height;
168 frame->linesize[0] = 2 * ctx->width;
169 frame->linesize[1] = 2 * ctx->width;
170 frame->linesize[2] = 2 * ctx->width;
173 av_frame_unref(frame);
177 frame->format = AV_PIX_FMT_CUDA;
178 frame->width = ctx->width;
179 frame->height = ctx->height;
184 static int cuda_transfer_get_formats(AVHWFramesContext *ctx,
185 enum AVHWFrameTransferDirection dir,
186 enum AVPixelFormat **formats)
188 enum AVPixelFormat *fmts;
190 fmts = av_malloc_array(2, sizeof(*fmts));
192 return AVERROR(ENOMEM);
194 fmts[0] = ctx->sw_format;
195 fmts[1] = AV_PIX_FMT_NONE;
202 static int cuda_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
205 CUDAFramesContext *priv = ctx->internal->priv;
206 AVCUDADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
212 err = cuCtxPushCurrent(device_hwctx->cuda_ctx);
213 if (err != CUDA_SUCCESS)
214 return AVERROR_UNKNOWN;
216 for (i = 0; i < FF_ARRAY_ELEMS(src->data) && src->data[i]; i++) {
217 CUDA_MEMCPY2D cpy = {
218 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
219 .dstMemoryType = CU_MEMORYTYPE_HOST,
220 .srcDevice = (CUdeviceptr)src->data[i],
221 .dstHost = dst->data[i],
222 .srcPitch = src->linesize[i],
223 .dstPitch = dst->linesize[i],
224 .WidthInBytes = FFMIN(src->linesize[i], dst->linesize[i]),
225 .Height = src->height >> (i ? priv->shift_height : 0),
228 err = cuMemcpy2D(&cpy);
229 if (err != CUDA_SUCCESS) {
230 av_log(ctx, AV_LOG_ERROR, "Error transferring the data from the CUDA frame\n");
231 return AVERROR_UNKNOWN;
235 cuCtxPopCurrent(&dummy);
240 static int cuda_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
243 CUDAFramesContext *priv = ctx->internal->priv;
244 AVCUDADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
250 err = cuCtxPushCurrent(device_hwctx->cuda_ctx);
251 if (err != CUDA_SUCCESS)
252 return AVERROR_UNKNOWN;
254 for (i = 0; i < FF_ARRAY_ELEMS(src->data) && src->data[i]; i++) {
255 CUDA_MEMCPY2D cpy = {
256 .srcMemoryType = CU_MEMORYTYPE_HOST,
257 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
258 .srcHost = src->data[i],
259 .dstDevice = (CUdeviceptr)dst->data[i],
260 .srcPitch = src->linesize[i],
261 .dstPitch = dst->linesize[i],
262 .WidthInBytes = FFMIN(src->linesize[i], dst->linesize[i]),
263 .Height = src->height >> (i ? priv->shift_height : 0),
266 err = cuMemcpy2D(&cpy);
267 if (err != CUDA_SUCCESS) {
268 av_log(ctx, AV_LOG_ERROR, "Error transferring the data from the CUDA frame\n");
269 return AVERROR_UNKNOWN;
273 cuCtxPopCurrent(&dummy);
278 static void cuda_device_free(AVHWDeviceContext *ctx)
280 AVCUDADeviceContext *hwctx = ctx->hwctx;
281 cuCtxDestroy(hwctx->cuda_ctx);
284 static int cuda_device_create(AVHWDeviceContext *ctx, const char *device,
285 AVDictionary *opts, int flags)
287 AVCUDADeviceContext *hwctx = ctx->hwctx;
294 device_idx = strtol(device, NULL, 0);
297 if (err != CUDA_SUCCESS) {
298 av_log(ctx, AV_LOG_ERROR, "Could not initialize the CUDA driver API\n");
299 return AVERROR_UNKNOWN;
302 err = cuDeviceGet(&cu_device, device_idx);
303 if (err != CUDA_SUCCESS) {
304 av_log(ctx, AV_LOG_ERROR, "Could not get the device number %d\n", device_idx);
305 return AVERROR_UNKNOWN;
308 err = cuCtxCreate(&hwctx->cuda_ctx, 0, cu_device);
309 if (err != CUDA_SUCCESS) {
310 av_log(ctx, AV_LOG_ERROR, "Error creating a CUDA context\n");
311 return AVERROR_UNKNOWN;
314 cuCtxPopCurrent(&dummy);
316 ctx->free = cuda_device_free;
321 const HWContextType ff_hwcontext_type_cuda = {
322 .type = AV_HWDEVICE_TYPE_CUDA,
325 .device_hwctx_size = sizeof(AVCUDADeviceContext),
326 .frames_priv_size = sizeof(CUDAFramesContext),
328 .device_create = cuda_device_create,
329 .frames_init = cuda_frames_init,
330 .frames_get_buffer = cuda_get_buffer,
331 .transfer_get_formats = cuda_transfer_get_formats,
332 .transfer_data_to = cuda_transfer_data_to,
333 .transfer_data_from = cuda_transfer_data_from,
335 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE },