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.h"
28 typedef struct CUDAFramesContext {
29 int shift_width, shift_height;
32 static const enum AVPixelFormat supported_formats[] = {
38 static void cuda_buffer_free(void *opaque, uint8_t *data)
40 AVHWFramesContext *ctx = opaque;
41 AVCUDADeviceContext *hwctx = ctx->device_ctx->hwctx;
45 cuCtxPushCurrent(hwctx->cuda_ctx);
47 cuMemFree((CUdeviceptr)data);
49 cuCtxPopCurrent(&dummy);
52 static AVBufferRef *cuda_pool_alloc(void *opaque, int size)
54 AVHWFramesContext *ctx = opaque;
55 AVCUDADeviceContext *hwctx = ctx->device_ctx->hwctx;
57 AVBufferRef *ret = NULL;
58 CUcontext dummy = NULL;
62 err = cuCtxPushCurrent(hwctx->cuda_ctx);
63 if (err != CUDA_SUCCESS) {
64 av_log(ctx, AV_LOG_ERROR, "Error setting current CUDA context\n");
68 err = cuMemAlloc(&data, size);
69 if (err != CUDA_SUCCESS)
72 ret = av_buffer_create((uint8_t*)data, size, cuda_buffer_free, ctx, 0);
79 cuCtxPopCurrent(&dummy);
83 static int cuda_frames_init(AVHWFramesContext *ctx)
85 CUDAFramesContext *priv = ctx->internal->priv;
88 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
89 if (ctx->sw_format == supported_formats[i])
92 if (i == FF_ARRAY_ELEMS(supported_formats)) {
93 av_log(ctx, AV_LOG_ERROR, "Pixel format '%s' is not supported\n",
94 av_get_pix_fmt_name(ctx->sw_format));
95 return AVERROR(ENOSYS);
98 av_pix_fmt_get_chroma_sub_sample(ctx->sw_format, &priv->shift_width, &priv->shift_height);
103 switch (ctx->sw_format) {
104 case AV_PIX_FMT_NV12:
105 case AV_PIX_FMT_YUV420P:
106 size = ctx->width * ctx->height * 3 / 2;
108 case AV_PIX_FMT_YUV444P:
109 size = ctx->width * ctx->height * 3;
113 ctx->internal->pool_internal = av_buffer_pool_init2(size, ctx, cuda_pool_alloc, NULL);
114 if (!ctx->internal->pool_internal)
115 return AVERROR(ENOMEM);
121 static int cuda_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
123 frame->buf[0] = av_buffer_pool_get(ctx->pool);
125 return AVERROR(ENOMEM);
127 switch (ctx->sw_format) {
128 case AV_PIX_FMT_NV12:
129 frame->data[0] = frame->buf[0]->data;
130 frame->data[1] = frame->data[0] + ctx->width * ctx->height;
131 frame->linesize[0] = ctx->width;
132 frame->linesize[1] = ctx->width;
134 case AV_PIX_FMT_YUV420P:
135 frame->data[0] = frame->buf[0]->data;
136 frame->data[2] = frame->data[0] + ctx->width * ctx->height;
137 frame->data[1] = frame->data[2] + ctx->width * ctx->height / 4;
138 frame->linesize[0] = ctx->width;
139 frame->linesize[1] = ctx->width / 2;
140 frame->linesize[2] = ctx->width / 2;
142 case AV_PIX_FMT_YUV444P:
143 frame->data[0] = frame->buf[0]->data;
144 frame->data[1] = frame->data[0] + ctx->width * ctx->height;
145 frame->data[2] = frame->data[1] + ctx->width * ctx->height;
146 frame->linesize[0] = ctx->width;
147 frame->linesize[1] = ctx->width;
148 frame->linesize[2] = ctx->width;
151 av_frame_unref(frame);
155 frame->format = AV_PIX_FMT_CUDA;
156 frame->width = ctx->width;
157 frame->height = ctx->height;
162 static int cuda_transfer_get_formats(AVHWFramesContext *ctx,
163 enum AVHWFrameTransferDirection dir,
164 enum AVPixelFormat **formats)
166 enum AVPixelFormat *fmts;
168 fmts = av_malloc_array(2, sizeof(*fmts));
170 return AVERROR(ENOMEM);
172 fmts[0] = ctx->sw_format;
173 fmts[1] = AV_PIX_FMT_NONE;
180 static int cuda_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
183 CUDAFramesContext *priv = ctx->internal->priv;
184 AVCUDADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
190 err = cuCtxPushCurrent(device_hwctx->cuda_ctx);
191 if (err != CUDA_SUCCESS)
192 return AVERROR_UNKNOWN;
194 for (i = 0; i < FF_ARRAY_ELEMS(src->data) && src->data[i]; i++) {
195 CUDA_MEMCPY2D cpy = {
196 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
197 .dstMemoryType = CU_MEMORYTYPE_HOST,
198 .srcDevice = (CUdeviceptr)src->data[i],
199 .dstHost = dst->data[i],
200 .srcPitch = src->linesize[i],
201 .dstPitch = dst->linesize[i],
202 .WidthInBytes = FFMIN(src->linesize[i], dst->linesize[i]),
203 .Height = src->height >> (i ? priv->shift_height : 0),
206 err = cuMemcpy2D(&cpy);
207 if (err != CUDA_SUCCESS) {
208 av_log(ctx, AV_LOG_ERROR, "Error transferring the data from the CUDA frame\n");
209 return AVERROR_UNKNOWN;
213 cuCtxPopCurrent(&dummy);
218 static int cuda_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
221 CUDAFramesContext *priv = ctx->internal->priv;
222 AVCUDADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
228 err = cuCtxPushCurrent(device_hwctx->cuda_ctx);
229 if (err != CUDA_SUCCESS)
230 return AVERROR_UNKNOWN;
232 for (i = 0; i < FF_ARRAY_ELEMS(src->data) && src->data[i]; i++) {
233 CUDA_MEMCPY2D cpy = {
234 .srcMemoryType = CU_MEMORYTYPE_HOST,
235 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
236 .srcHost = src->data[i],
237 .dstDevice = (CUdeviceptr)dst->data[i],
238 .srcPitch = src->linesize[i],
239 .dstPitch = dst->linesize[i],
240 .WidthInBytes = FFMIN(src->linesize[i], dst->linesize[i]),
241 .Height = src->height >> (i ? priv->shift_height : 0),
244 err = cuMemcpy2D(&cpy);
245 if (err != CUDA_SUCCESS) {
246 av_log(ctx, AV_LOG_ERROR, "Error transferring the data from the CUDA frame\n");
247 return AVERROR_UNKNOWN;
251 cuCtxPopCurrent(&dummy);
256 const HWContextType ff_hwcontext_type_cuda = {
257 .type = AV_HWDEVICE_TYPE_CUDA,
260 .device_hwctx_size = sizeof(AVCUDADeviceContext),
261 .frames_priv_size = sizeof(CUDAFramesContext),
263 .frames_init = cuda_frames_init,
264 .frames_get_buffer = cuda_get_buffer,
265 .transfer_get_formats = cuda_transfer_get_formats,
266 .transfer_data_to = cuda_transfer_data_to,
267 .transfer_data_from = cuda_transfer_data_from,
269 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE },