History

lavfi: add a filter for uploading normal frames to CUDA

Anton Khirnov committed 10 years ago

Commit 21f7cd41 parent 1bf34135 files · +251 −0Browse files

Unified · Split

5 files
  1. configure +1 −0
  2. doc/filters.texi +11 −0
  3. libavfilter/Makefile +1 −0
  4. libavfilter/allfilters.c +1 −0
  5. libavfilter/vf_hwupload_cuda.c +237 −0
configure+1−0
@@ -2382,6 +2382,7 @@ frei0r_filter_extralibs='$ldl' · 2381 unmodified lines
2382frei0r_src_filter_deps="frei0r dlopen"2382frei0r_src_filter_deps="frei0r dlopen"
2383frei0r_src_filter_extralibs='$ldl'2383frei0r_src_filter_extralibs='$ldl'
2384hqdn3d_filter_deps="gpl"2384hqdn3d_filter_deps="gpl"
2385hwupload_cuda_filter_deps="cuda"
2385interlace_filter_deps="gpl"2386interlace_filter_deps="gpl"
2386ocv_filter_deps="libopencv"2387ocv_filter_deps="libopencv"
2387resample_filter_deps="avresample"2388resample_filter_deps="avresample"
doc/filters.texi+11−0
@@ -1610,6 +1610,17 @@ A floating point number which specifies chroma temporal strength. It defaults to · 1609 unmodified lines
1610@var{luma_tmp}*@var{chroma_spatial}/@var{luma_spatial}.1610@var{luma_tmp}*@var{chroma_spatial}/@var{luma_spatial}.
1611@end table1611@end table
1612 1612
1613@section hwupload_cuda
1614
1615Upload system memory frames to a CUDA device.
1616
1617It accepts the following optional parameters:
1618
1619@table @option
1620@item device
1621The number of the CUDA device to use
1622@end table
1623
1613@section interlace1624@section interlace
1614 1625
1615Simple interlacing filter from progressive contents. This interleaves upper (or1626Simple interlacing filter from progressive contents. This interleaves upper (or
libavfilter/Makefile+1−0
@@ -56,6 +56,7 @@ OBJS-$(CONFIG_FREI0R_FILTER) += vf_frei0r.o · 55 unmodified lines
56OBJS-$(CONFIG_GRADFUN_FILTER) += vf_gradfun.o56OBJS-$(CONFIG_GRADFUN_FILTER) += vf_gradfun.o
57OBJS-$(CONFIG_HFLIP_FILTER) += vf_hflip.o57OBJS-$(CONFIG_HFLIP_FILTER) += vf_hflip.o
58OBJS-$(CONFIG_HQDN3D_FILTER) += vf_hqdn3d.o58OBJS-$(CONFIG_HQDN3D_FILTER) += vf_hqdn3d.o
59OBJS-$(CONFIG_HWUPLOAD_CUDA_FILTER) += vf_hwupload_cuda.o
59OBJS-$(CONFIG_INTERLACE_FILTER) += vf_interlace.o60OBJS-$(CONFIG_INTERLACE_FILTER) += vf_interlace.o
60OBJS-$(CONFIG_LUT_FILTER) += vf_lut.o61OBJS-$(CONFIG_LUT_FILTER) += vf_lut.o
61OBJS-$(CONFIG_LUTRGB_FILTER) += vf_lut.o62OBJS-$(CONFIG_LUTRGB_FILTER) += vf_lut.o
libavfilter/allfilters.c+1−0
@@ -82,6 +82,7 @@ void avfilter_register_all(void) · 81 unmodified lines
82 REGISTER_FILTER(GRADFUN, gradfun, vf);82 REGISTER_FILTER(GRADFUN, gradfun, vf);
83 REGISTER_FILTER(HFLIP, hflip, vf);83 REGISTER_FILTER(HFLIP, hflip, vf);
84 REGISTER_FILTER(HQDN3D, hqdn3d, vf);84 REGISTER_FILTER(HQDN3D, hqdn3d, vf);
85 REGISTER_FILTER(HWUPLOAD_CUDA, hwupload_cuda, vf);
85 REGISTER_FILTER(INTERLACE, interlace, vf);86 REGISTER_FILTER(INTERLACE, interlace, vf);
86 REGISTER_FILTER(LUT, lut, vf);87 REGISTER_FILTER(LUT, lut, vf);
87 REGISTER_FILTER(LUTRGB, lutrgb, vf);88 REGISTER_FILTER(LUTRGB, lutrgb, vf);
libavfilter/vf_hwupload_cuda.c+237−0
@@ -0,0 +1,237 @@
1/*
2 * This file is part of Libav.
3 *
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.
8 *
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.
13 *
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
17 */
18
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"
24
25#include "avfilter.h"
26#include "formats.h"
27#include "internal.h"
28#include "video.h"
29
30typedef struct CudaUploadContext {
31 const AVClass *class;
32 int device_idx;
33
34 AVBufferRef *hwdevice;
35 AVBufferRef *hwframe;
36} CudaUploadContext;
37
38static void cudaupload_ctx_free(AVHWDeviceContext *ctx)
39{
40 AVCUDADeviceContext *hwctx = ctx->hwctx;
41 cuCtxDestroy(hwctx->cuda_ctx);
42}
43
44static av_cold int cudaupload_init(AVFilterContext *ctx)
45{
46 CudaUploadContext *s = ctx->priv;
47
48 AVHWDeviceContext *device_ctx;
49 AVCUDADeviceContext *device_hwctx;
50 CUdevice device;
51 CUcontext cuda_ctx = NULL, dummy;
52 CUresult err;
53 int ret;
54
55 err = cuInit(0);
56 if (err != CUDA_SUCCESS) {
57 av_log(ctx, AV_LOG_ERROR, "Could not initialize the CUDA driver API\n");
58 return AVERROR_UNKNOWN;
59 }
60
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;
65 }
66
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;
71 }
72
73 cuCtxPopCurrent(&dummy);
74
75 s->hwdevice = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_CUDA);
76 if (!s->hwdevice) {
77 cuCtxDestroy(cuda_ctx);
78 return AVERROR(ENOMEM);
79 }
80
81 device_ctx = (AVHWDeviceContext*)s->hwdevice->data;
82 device_ctx->free = cudaupload_ctx_free;
83
84 device_hwctx = device_ctx->hwctx;
85 device_hwctx->cuda_ctx = cuda_ctx;
86
87 ret = av_hwdevice_ctx_init(s->hwdevice);
88 if (ret < 0)
89 return ret;
90
91 return 0;
92}
93
94static av_cold void cudaupload_uninit(AVFilterContext *ctx)
95{
96 CudaUploadContext *s = ctx->priv;
97
98 av_buffer_unref(&s->hwframe);
99 av_buffer_unref(&s->hwdevice);
100}
101
102static int cudaupload_query_formats(AVFilterContext *ctx)
103{
104 static const enum AVPixelFormat input_pix_fmts[] = {
105 AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P,
106 AV_PIX_FMT_NONE,
107 };
108 static const enum AVPixelFormat output_pix_fmts[] = {
109 AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE,
110 };
111 AVFilterFormats *in_fmts = ff_make_format_list(input_pix_fmts);
112 AVFilterFormats *out_fmts = ff_make_format_list(output_pix_fmts);
113
114 ff_formats_ref(in_fmts, &ctx->inputs[0]->out_formats);
115 ff_formats_ref(out_fmts, &ctx->outputs[0]->in_formats);
116
117 return 0;
118}
119
120static int cudaupload_config_output(AVFilterLink *outlink)
121{
122 AVFilterContext *ctx = outlink->src;
123 AVFilterLink *inlink = ctx->inputs[0];
124 CudaUploadContext *s = ctx->priv;
125
126 AVHWFramesContext *hwframe_ctx;
127 int ret;
128
129 av_buffer_unref(&s->hwframe);
130 s->hwframe = av_hwframe_ctx_alloc(s->hwdevice);
131 if (!s->hwframe)
132 return AVERROR(ENOMEM);
133
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);
139
140 ret = av_hwframe_ctx_init(s->hwframe);
141 if (ret < 0)
142 return ret;
143
144 outlink->hw_frames_ctx = av_buffer_ref(s->hwframe);
145 if (!outlink->hw_frames_ctx)
146 return AVERROR(ENOMEM);
147
148 return 0;
149}
150
151static int cudaupload_filter_frame(AVFilterLink *link, AVFrame *in)
152{
153 AVFilterContext *ctx = link->dst;
154 CudaUploadContext *s = ctx->priv;
155
156 AVFrame *out = NULL;
157 int ret;
158
159 out = av_frame_alloc();
160 if (!out) {
161 ret = AVERROR(ENOMEM);
162 goto fail;
163 }
164
165 ret = av_hwframe_get_buffer(s->hwframe, out, 0);
166 if (ret < 0)
167 goto fail;
168
169 out->width = in->width;
170 out->height = in->height;
171
172 ret = av_hwframe_transfer_data(out, in, 0);
173 if (ret < 0) {
174 av_log(ctx, AV_LOG_ERROR, "Error transferring data to the GPU\n");
175 goto fail;
176 }
177
178 ret = av_frame_copy_props(out, in);
179 if (ret < 0)
180 goto fail;
181
182 av_frame_free(&in);
183
184 return ff_filter_frame(ctx->outputs[0], out);
185fail:
186 av_frame_free(&in);
187 av_frame_free(&out);
188 return ret;
189}
190
191#define OFFSET(x) offsetof(CudaUploadContext, x)
192#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
193static const AVOption options[] = {
194 { "device", "Number of the device to use", OFFSET(device_idx), AV_OPT_TYPE_INT, { .i64 = 0 }, .flags = FLAGS },
195 { NULL },
196};
197
198static const AVClass cudaupload_class = {
199 .class_name = "cudaupload",
200 .item_name = av_default_item_name,
201 .option = options,
202 .version = LIBAVUTIL_VERSION_INT,
203};
204
205static const AVFilterPad cudaupload_inputs[] = {
206 {
207 .name = "default",
208 .type = AVMEDIA_TYPE_VIDEO,
209 .filter_frame = cudaupload_filter_frame,
210 },
211 { NULL }
212};
213
214static const AVFilterPad cudaupload_outputs[] = {
215 {
216 .name = "default",
217 .type = AVMEDIA_TYPE_VIDEO,
218 .config_props = cudaupload_config_output,
219 },
220 { NULL }
221};
222
223AVFilter ff_vf_hwupload_cuda = {
224 .name = "hwupload_cuda",
225 .description = NULL_IF_CONFIG_SMALL("Upload a system memory frame to a CUDA device"),
226
227 .init = cudaupload_init,
228 .uninit = cudaupload_uninit,
229
230 .query_formats = cudaupload_query_formats,
231
232 .priv_size = sizeof(CudaUploadContext),
233 .priv_class = &cudaupload_class,
234
235 .inputs = cudaupload_inputs,
236 .outputs = cudaupload_outputs,
237};

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t