Quoting Hendrik Leppkes (2016-02-12 13:29:57) > On Fri, Feb 12, 2016 at 1:07 PM, wm4 <[email protected]> wrote: > > On Fri, 12 Feb 2016 10:09:16 +0100 > > Anton Khirnov <[email protected]> wrote: > > > >> --- > >> configure | 1 + > >> doc/filters.texi | 11 ++ > >> libavfilter/Makefile | 1 + > >> libavfilter/allfilters.c | 1 + > >> libavfilter/vf_hwupload_cuda.c | 237 > >> +++++++++++++++++++++++++++++++++++++++++ > >> 5 files changed, 251 insertions(+) > >> create mode 100644 libavfilter/vf_hwupload_cuda.c > >> > >> diff --git a/configure b/configure > >> index 76aa7b4..37212a4 100755 > >> --- a/configure > >> +++ b/configure > >> @@ -2376,6 +2376,7 @@ frei0r_filter_extralibs='$ldl' > >> frei0r_src_filter_deps="frei0r dlopen" > >> frei0r_src_filter_extralibs='$ldl' > >> hqdn3d_filter_deps="gpl" > >> +hwupload_cuda_filter_deps="cuda" > >> interlace_filter_deps="gpl" > >> ocv_filter_deps="libopencv" > >> resample_filter_deps="avresample" > >> diff --git a/doc/filters.texi b/doc/filters.texi > >> index b995d75..722c8b0 100644 > >> --- a/doc/filters.texi > >> +++ b/doc/filters.texi > >> @@ -1610,6 +1610,17 @@ A floating point number which specifies chroma > >> temporal strength. It defaults to > >> @var{luma_tmp}*@var{chroma_spatial}/@var{luma_spatial}. > >> @end table > >> > >> +@section hwupload_cuda > >> + > >> +Upload system memory frames to a CUDA device. > >> + > >> +It accepts the following optional parameters: > >> + > >> +@table @option > >> +@item device > >> +The number of the CUDA device to use > >> +@end table > >> + > >> @section interlace > >> > >> Simple interlacing filter from progressive contents. This interleaves > >> upper (or > >> diff --git a/libavfilter/Makefile b/libavfilter/Makefile > >> index a95a7eb..f5dd2e9 100644 > >> --- a/libavfilter/Makefile > >> +++ b/libavfilter/Makefile > >> @@ -56,6 +56,7 @@ OBJS-$(CONFIG_FREI0R_FILTER) += > >> vf_frei0r.o > >> OBJS-$(CONFIG_GRADFUN_FILTER) += vf_gradfun.o > >> OBJS-$(CONFIG_HFLIP_FILTER) += vf_hflip.o > >> OBJS-$(CONFIG_HQDN3D_FILTER) += vf_hqdn3d.o > >> +OBJS-$(CONFIG_HWUPLOAD_CUDA_FILTER) += vf_hwupload_cuda.o > >> OBJS-$(CONFIG_INTERLACE_FILTER) += vf_interlace.o > >> OBJS-$(CONFIG_LUT_FILTER) += vf_lut.o > >> OBJS-$(CONFIG_LUTRGB_FILTER) += vf_lut.o > >> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c > >> index 67a298d..4bdfaea 100644 > >> --- a/libavfilter/allfilters.c > >> +++ b/libavfilter/allfilters.c > >> @@ -82,6 +82,7 @@ void avfilter_register_all(void) > >> REGISTER_FILTER(GRADFUN, gradfun, vf); > >> REGISTER_FILTER(HFLIP, hflip, vf); > >> REGISTER_FILTER(HQDN3D, hqdn3d, vf); > >> + REGISTER_FILTER(HWUPLOAD_CUDA, hwupload_cuda, vf); > >> REGISTER_FILTER(INTERLACE, interlace, vf); > >> REGISTER_FILTER(LUT, lut, vf); > >> REGISTER_FILTER(LUTRGB, lutrgb, vf); > >> diff --git a/libavfilter/vf_hwupload_cuda.c > >> b/libavfilter/vf_hwupload_cuda.c > >> new file mode 100644 > >> index 0000000..dfb3506 > >> --- /dev/null > >> +++ b/libavfilter/vf_hwupload_cuda.c > >> @@ -0,0 +1,237 @@ > >> +/* > >> + * This file is part of Libav. > >> + * > >> + * Libav is free software; you can redistribute it and/or > >> + * modify it under the terms of the GNU Lesser General Public > >> + * License as published by the Free Software Foundation; either > >> + * version 2.1 of the License, or (at your option) any later version. > >> + * > >> + * Libav is distributed in the hope that it will be useful, > >> + * but WITHOUT ANY WARRANTY; without even the implied warranty of > >> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > >> + * Lesser General Public License for more details. > >> + * > >> + * You should have received a copy of the GNU Lesser General Public > >> + * License along with Libav; if not, write to the Free Software > >> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA > >> 02110-1301 USA > >> + */ > >> + > >> +#include "libavutil/buffer.h" > >> +#include "libavutil/hwcontext.h" > >> +#include "libavutil/hwcontext_cuda.h" > >> +#include "libavutil/log.h" > >> +#include "libavutil/opt.h" > >> + > >> +#include "avfilter.h" > >> +#include "formats.h" > >> +#include "internal.h" > >> +#include "video.h" > >> + > >> +typedef struct CudaUploadContext { > >> + const AVClass *class; > >> + int device_idx; > >> + > >> + AVBufferRef *hwdevice; > >> + AVBufferRef *hwframe; > >> +} CudaUploadContext; > >> + > >> +static void cudaupload_ctx_free(AVHWDeviceContext *ctx) > >> +{ > >> + AVCUDADeviceContext *hwctx = ctx->hwctx; > >> + cuCtxDestroy(hwctx->cuda_ctx); > >> +} > >> + > >> +static av_cold int cudaupload_init(AVFilterContext *ctx) > >> +{ > >> + CudaUploadContext *s = ctx->priv; > >> + > >> + AVHWDeviceContext *device_ctx; > >> + AVCUDADeviceContext *device_hwctx; > >> + CUdevice device; > >> + CUcontext cuda_ctx = NULL, dummy; > >> + CUresult err; > >> + int ret; > >> + > >> + err = cuInit(0); > >> + if (err != CUDA_SUCCESS) { > >> + av_log(ctx, AV_LOG_ERROR, "Could not initialize the CUDA driver > >> API\n"); > >> + return AVERROR_UNKNOWN; > >> + } > >> + > >> + err = cuDeviceGet(&device, s->device_idx); > >> + if (err != CUDA_SUCCESS) { > >> + av_log(ctx, AV_LOG_ERROR, "Could not get the device number %d\n", > >> s->device_idx); > >> + return AVERROR_UNKNOWN; > >> + } > >> + > >> + err = cuCtxCreate(&cuda_ctx, 0, device); > >> + if (err != CUDA_SUCCESS) { > >> + av_log(ctx, AV_LOG_ERROR, "Error creating a CUDA context\n"); > >> + return AVERROR_UNKNOWN; > >> + } > >> + > >> + cuCtxPopCurrent(&dummy); > >> + > >> + s->hwdevice = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_CUDA); > >> + if (!s->hwdevice) { > >> + cuCtxDestroy(cuda_ctx); > >> + return AVERROR(ENOMEM); > >> + } > >> + > >> + device_ctx = (AVHWDeviceContext*)s->hwdevice->data; > >> + device_ctx->free = cudaupload_ctx_free; > >> + > >> + device_hwctx = device_ctx->hwctx; > >> + device_hwctx->cuda_ctx = cuda_ctx; > >> + > >> + ret = av_hwdevice_ctx_init(s->hwdevice); > >> + if (ret < 0) > >> + return ret; > >> + > >> + return 0; > >> +} > >> + > >> +static av_cold void cudaupload_uninit(AVFilterContext *ctx) > >> +{ > >> + CudaUploadContext *s = ctx->priv; > >> + > >> + av_buffer_unref(&s->hwframe); > >> + av_buffer_unref(&s->hwdevice); > >> +} > >> + > >> +static int cudaupload_query_formats(AVFilterContext *ctx) > >> +{ > >> + static const enum AVPixelFormat input_pix_fmts[] = { > >> + AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P, > >> + AV_PIX_FMT_NONE, > >> + }; > >> + static const enum AVPixelFormat output_pix_fmts[] = { > >> + AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE, > >> + }; > >> + AVFilterFormats *in_fmts = ff_make_format_list(input_pix_fmts); > >> + AVFilterFormats *out_fmts = ff_make_format_list(output_pix_fmts); > >> + > >> + ff_formats_ref(in_fmts, &ctx->inputs[0]->out_formats); > >> + ff_formats_ref(out_fmts, &ctx->outputs[0]->in_formats); > >> + > >> + return 0; > >> +} > >> + > >> +static int cudaupload_config_output(AVFilterLink *outlink) > >> +{ > >> + AVFilterContext *ctx = outlink->src; > >> + AVFilterLink *inlink = ctx->inputs[0]; > >> + CudaUploadContext *s = ctx->priv; > >> + > >> + AVHWFramesContext *hwframe_ctx; > >> + int ret; > >> + > >> + av_buffer_unref(&s->hwframe); > >> + s->hwframe = av_hwframe_ctx_alloc(s->hwdevice); > >> + if (!s->hwframe) > >> + return AVERROR(ENOMEM); > >> + > >> + hwframe_ctx = (AVHWFramesContext*)s->hwframe->data; > >> + hwframe_ctx->format = AV_PIX_FMT_CUDA; > >> + hwframe_ctx->sw_format = inlink->format; > >> + hwframe_ctx->width = FFALIGN(inlink->w, 16); > >> + hwframe_ctx->height = FFALIGN(inlink->h, 16); > >> + > >> + ret = av_hwframe_ctx_init(s->hwframe); > >> + if (ret < 0) > >> + return ret; > >> + > >> + outlink->hw_frames_ctx = av_buffer_ref(s->hwframe); > >> + if (!outlink->hw_frames_ctx) > >> + return AVERROR(ENOMEM); > >> + > >> + return 0; > >> +} > >> + > >> +static int cudaupload_filter_frame(AVFilterLink *link, AVFrame *in) > >> +{ > >> + AVFilterContext *ctx = link->dst; > >> + CudaUploadContext *s = ctx->priv; > >> + > >> + AVFrame *out = NULL; > >> + int ret; > >> + > >> + out = av_frame_alloc(); > >> + if (!out) { > >> + ret = AVERROR(ENOMEM); > >> + goto fail; > >> + } > >> + > >> + ret = av_hwframe_get_buffer(s->hwframe, out, 0); > >> + if (ret < 0) > >> + goto fail; > >> + > >> + out->width = in->width; > >> + out->height = in->height; > >> + > >> + ret = av_hwframe_transfer_data(out, in, 0); > >> + if (ret < 0) { > >> + av_log(ctx, AV_LOG_ERROR, "Error transferring data to the GPU\n"); > >> + goto fail; > >> + } > >> + > >> + ret = av_frame_copy_props(out, in); > >> + if (ret < 0) > >> + goto fail; > >> + > >> + av_frame_free(&in); > >> + > >> + return ff_filter_frame(ctx->outputs[0], out); > >> +fail: > >> + av_frame_free(&in); > >> + av_frame_free(&out); > >> + return ret; > >> +} > >> + > >> +#define OFFSET(x) offsetof(CudaUploadContext, x) > >> +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM > >> +static const AVOption options[] = { > >> + { "device", "Number of the device to use", OFFSET(device_idx), > >> AV_OPT_TYPE_INT, { .i64 = 0 }, .flags = FLAGS }, > >> + { NULL }, > >> +}; > >> + > >> +static const AVClass cudaupload_class = { > >> + .class_name = "cudaupload", > >> + .item_name = av_default_item_name, > >> + .option = options, > >> + .version = LIBAVUTIL_VERSION_INT, > >> +}; > >> + > >> +static const AVFilterPad cudaupload_inputs[] = { > >> + { > >> + .name = "default", > >> + .type = AVMEDIA_TYPE_VIDEO, > >> + .filter_frame = cudaupload_filter_frame, > >> + }, > >> + { NULL } > >> +}; > >> + > >> +static const AVFilterPad cudaupload_outputs[] = { > >> + { > >> + .name = "default", > >> + .type = AVMEDIA_TYPE_VIDEO, > >> + .config_props = cudaupload_config_output, > >> + }, > >> + { NULL } > >> +}; > >> + > >> +AVFilter ff_vf_hwupload_cuda = { > >> + .name = "hwupload_cuda", > >> + .description = NULL_IF_CONFIG_SMALL("Upload a system memory frame to > >> a CUDA device"), > >> + > >> + .init = cudaupload_init, > >> + .uninit = cudaupload_uninit, > >> + > >> + .query_formats = cudaupload_query_formats, > >> + > >> + .priv_size = sizeof(CudaUploadContext), > >> + .priv_class = &cudaupload_class, > >> + > >> + .inputs = cudaupload_inputs, > >> + .outputs = cudaupload_outputs, > >> +}; > > > > Why is this a CUDA specific filter anyway, when you just added a > > generic framework to do it on all hwaccels? > > I asked that the last time already, and the answer was because its > easier and there is otherwise no built-in way to create a CUDA device. > I'm still against this filter btw, if you want to offer an API to > create a device, then do so, don't add a messy filter to do it for > you. >
Well that's exactly it -- I don't want to offer such an API yet, because it's not clear to me - whether it's reasonably doable - if it is, what should the API look like And in the end, there needs to be filters or some other lavfi components capable of doing this, since sometimes you want only a part of your filterchain on the GPU. -- Anton Khirnov _______________________________________________ libav-devel mailing list [email protected] https://lists.libav.org/mailman/listinfo/libav-devel
