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
19 #include "libavutil/buffer.h"
20 #include "libavutil/hwcontext.h"
21 #include "libavutil/hwcontext_cuda.h"
22 #include "libavutil/log.h"
23 #include "libavutil/opt.h"
30 typedef struct CudaUploadContext {
34 AVBufferRef *hwdevice;
38 static void cudaupload_ctx_free(AVHWDeviceContext *ctx)
40 AVCUDADeviceContext *hwctx = ctx->hwctx;
41 cuCtxDestroy(hwctx->cuda_ctx);
44 static av_cold int cudaupload_init(AVFilterContext *ctx)
46 CudaUploadContext *s = ctx->priv;
48 AVHWDeviceContext *device_ctx;
49 AVCUDADeviceContext *device_hwctx;
51 CUcontext cuda_ctx = NULL, dummy;
56 if (err != CUDA_SUCCESS) {
57 av_log(ctx, AV_LOG_ERROR, "Could not initialize the CUDA driver API\n");
58 return AVERROR_UNKNOWN;
61 err = cuDeviceGet(&device, s->device_idx);
62 if (err != CUDA_SUCCESS) {
63 av_log(ctx, AV_LOG_ERROR, "Could not get the device number %d\n", s->device_idx);
64 return AVERROR_UNKNOWN;
67 err = cuCtxCreate(&cuda_ctx, 0, device);
68 if (err != CUDA_SUCCESS) {
69 av_log(ctx, AV_LOG_ERROR, "Error creating a CUDA context\n");
70 return AVERROR_UNKNOWN;
73 cuCtxPopCurrent(&dummy);
75 s->hwdevice = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_CUDA);
77 cuCtxDestroy(cuda_ctx);
78 return AVERROR(ENOMEM);
81 device_ctx = (AVHWDeviceContext*)s->hwdevice->data;
82 device_ctx->free = cudaupload_ctx_free;
84 device_hwctx = device_ctx->hwctx;
85 device_hwctx->cuda_ctx = cuda_ctx;
87 ret = av_hwdevice_ctx_init(s->hwdevice);
94 static av_cold void cudaupload_uninit(AVFilterContext *ctx)
96 CudaUploadContext *s = ctx->priv;
98 av_buffer_unref(&s->hwframe);
99 av_buffer_unref(&s->hwdevice);
102 static int cudaupload_query_formats(AVFilterContext *ctx)
104 static const enum AVPixelFormat input_pix_fmts[] = {
105 AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P,
108 static const enum AVPixelFormat output_pix_fmts[] = {
109 AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE,
111 AVFilterFormats *in_fmts = ff_make_format_list(input_pix_fmts);
112 AVFilterFormats *out_fmts = ff_make_format_list(output_pix_fmts);
114 ff_formats_ref(in_fmts, &ctx->inputs[0]->out_formats);
115 ff_formats_ref(out_fmts, &ctx->outputs[0]->in_formats);
120 static int cudaupload_config_output(AVFilterLink *outlink)
122 AVFilterContext *ctx = outlink->src;
123 AVFilterLink *inlink = ctx->inputs[0];
124 CudaUploadContext *s = ctx->priv;
126 AVHWFramesContext *hwframe_ctx;
129 av_buffer_unref(&s->hwframe);
130 s->hwframe = av_hwframe_ctx_alloc(s->hwdevice);
132 return AVERROR(ENOMEM);
134 hwframe_ctx = (AVHWFramesContext*)s->hwframe->data;
135 hwframe_ctx->format = AV_PIX_FMT_CUDA;
136 hwframe_ctx->sw_format = inlink->format;
137 hwframe_ctx->width = FFALIGN(inlink->w, 16);
138 hwframe_ctx->height = FFALIGN(inlink->h, 16);
140 ret = av_hwframe_ctx_init(s->hwframe);
144 outlink->hw_frames_ctx = av_buffer_ref(s->hwframe);
145 if (!outlink->hw_frames_ctx)
146 return AVERROR(ENOMEM);
151 static int cudaupload_filter_frame(AVFilterLink *link, AVFrame *in)
153 AVFilterContext *ctx = link->dst;
154 CudaUploadContext *s = ctx->priv;
159 out = av_frame_alloc();
161 ret = AVERROR(ENOMEM);
165 ret = av_hwframe_get_buffer(s->hwframe, out, 0);
169 out->width = in->width;
170 out->height = in->height;
172 ret = av_hwframe_transfer_data(out, in, 0);
174 av_log(ctx, AV_LOG_ERROR, "Error transferring data to the GPU\n");
178 ret = av_frame_copy_props(out, in);
184 return ff_filter_frame(ctx->outputs[0], out);
191 #define OFFSET(x) offsetof(CudaUploadContext, x)
192 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
193 static const AVOption options[] = {
194 { "device", "Number of the device to use", OFFSET(device_idx), AV_OPT_TYPE_INT, { .i64 = 0 }, .flags = FLAGS },
198 static const AVClass cudaupload_class = {
199 .class_name = "cudaupload",
200 .item_name = av_default_item_name,
202 .version = LIBAVUTIL_VERSION_INT,
205 static const AVFilterPad cudaupload_inputs[] = {
208 .type = AVMEDIA_TYPE_VIDEO,
209 .filter_frame = cudaupload_filter_frame,
214 static const AVFilterPad cudaupload_outputs[] = {
217 .type = AVMEDIA_TYPE_VIDEO,
218 .config_props = cudaupload_config_output,
223 AVFilter ff_vf_hwupload_cuda = {
224 .name = "hwupload_cuda",
225 .description = NULL_IF_CONFIG_SMALL("Upload a system memory frame to a CUDA device"),
227 .init = cudaupload_init,
228 .uninit = cudaupload_uninit,
230 .query_formats = cudaupload_query_formats,
232 .priv_size = sizeof(CudaUploadContext),
233 .priv_class = &cudaupload_class,
235 .inputs = cudaupload_inputs,
236 .outputs = cudaupload_outputs,