ffmpeg/libavfilter/vf_hwupload_cuda.c

201 lines
5.4 KiB
C
Raw Permalink Normal View History

/*
* This file is part of FFmpeg.
*
* FFmpeg 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.
*
* FFmpeg 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 FFmpeg; 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/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 av_cold int cudaupload_init(AVFilterContext *ctx)
{
CudaUploadContext *s = ctx->priv;
char buf[64] = { 0 };
snprintf(buf, sizeof(buf), "%d", s->device_idx);
return av_hwdevice_ctx_create(&s->hwdevice, AV_HWDEVICE_TYPE_CUDA, buf, NULL, 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)
{
int ret;
static const enum AVPixelFormat input_pix_fmts[] = {
AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV444P,
AV_PIX_FMT_P010, AV_PIX_FMT_P016, AV_PIX_FMT_YUV444P16,
AV_PIX_FMT_0RGB32, AV_PIX_FMT_0BGR32,
#if CONFIG_VULKAN
AV_PIX_FMT_VULKAN,
#endif
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;
ret = ff_formats_ref(in_fmts, &ctx->inputs[0]->outcfg.formats);
if (ret < 0)
return ret;
out_fmts = ff_make_format_list(output_pix_fmts);
ret = ff_formats_ref(out_fmts, &ctx->outputs[0]->incfg.formats);
if (ret < 0)
return ret;
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;
if (inlink->hw_frames_ctx) {
AVHWFramesContext *in_hwframe_ctx = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
hwframe_ctx->sw_format = in_hwframe_ctx->sw_format;
} else {
hwframe_ctx->sw_format = inlink->format;
}
hwframe_ctx->width = inlink->w;
hwframe_ctx->height = inlink->h;
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;
AVFilterLink *outlink = ctx->outputs[0];
AVFrame *out = NULL;
int ret;
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
if (!out) {
ret = AVERROR(ENOMEM);
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_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
static const AVOption cudaupload_options[] = {
{ "device", "Number of the device to use", OFFSET(device_idx), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
{ NULL },
};
AVFILTER_DEFINE_CLASS(cudaupload);
static const AVFilterPad cudaupload_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.filter_frame = cudaupload_filter_frame,
},
};
static const AVFilterPad cudaupload_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.config_props = cudaupload_config_output,
},
};
const 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,
.priv_size = sizeof(CudaUploadContext),
.priv_class = &cudaupload_class,
2021-08-12 13:05:31 +02:00
FILTER_INPUTS(cudaupload_inputs),
FILTER_OUTPUTS(cudaupload_outputs),
avfilter: Replace query_formats callback with union of list and callback If one looks at the many query_formats callbacks in existence, one will immediately recognize that there is one type of default callback for video and a slightly different default callback for audio: It is "return ff_set_common_formats_from_list(ctx, pix_fmts);" for video with a filter-specific pix_fmts list. For audio, it is the same with a filter-specific sample_fmts list together with ff_set_common_all_samplerates() and ff_set_common_all_channel_counts(). This commit allows to remove the boilerplate query_formats callbacks by replacing said callback with a union consisting the old callback and pointers for pixel and sample format arrays. For the not uncommon case in which these lists only contain a single entry (besides the sentinel) enum AVPixelFormat and enum AVSampleFormat fields are also added to the union to store them directly in the AVFilter, thereby avoiding a relocation. The state of said union will be contained in a new, dedicated AVFilter field (the nb_inputs and nb_outputs fields have been shrunk to uint8_t in order to create a hole for this new field; this is no problem, as the maximum of all the nb_inputs is four; for nb_outputs it is only two). The state's default value coincides with the earlier default of query_formats being unset, namely that the filter accepts all formats (and also sample rates and channel counts/layouts for audio) provided that these properties agree coincide for all inputs and outputs. By using different union members for audio and video filters the type-unsafety of using the same functions for audio and video lists will furthermore be more confined to formats.c than before. When the new fields are used, they will also avoid allocations: Currently something nearly equivalent to ff_default_query_formats() is called after every successful call to a query_formats callback; yet in the common case that the newly allocated AVFilterFormats are not used at all (namely if there are no free links) these newly allocated AVFilterFormats are freed again without ever being used. Filters no longer using the callback will not exhibit this any more. Reviewed-by: Paul B Mahol <onemda@gmail.com> Reviewed-by: Nicolas George <george@nsup.org> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-27 12:07:35 +02:00
FILTER_QUERY_FUNC(cudaupload_query_formats),
.flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
};