lavfi/vulkan: split off lavfi-specific code into vulkan_filter.c

The issue is that libavfilter depends on libavcodec, and when doing a
static build, if libavcodec also includes "libavfilter/vulkan.c", then
during link-time, compiling programs will fail as there would be multiple
definitions of the same symbols in both libavfilter and libavcodec's
object files.
Linkers are, however, more permitting if both files that include
a common file that's used as a template are one-to-one identical.
Hence, to make both files the same in the future, export all avfilter
specific functions to a separate file.
There is some work in progress to make templated files like this be
compiled only once, so this is not a long-term solution.

This also removes a macro that could be used to toggle SPIRV compilation
capability on #include-time, as this could cause the files to be different.
This commit is contained in:
Lynne 2021-11-19 15:13:35 +01:00
parent e7f3279ba0
commit f6dd30df24
No known key found for this signature in database
GPG Key ID: A2FEA5F03F034464
13 changed files with 240 additions and 200 deletions

View File

@ -180,7 +180,7 @@ OBJS-$(CONFIG_ATADENOISE_FILTER) += vf_atadenoise.o
OBJS-$(CONFIG_AVGBLUR_FILTER) += vf_avgblur.o
OBJS-$(CONFIG_AVGBLUR_OPENCL_FILTER) += vf_avgblur_opencl.o opencl.o \
opencl/avgblur.o boxblur.o
OBJS-$(CONFIG_AVGBLUR_VULKAN_FILTER) += vf_avgblur_vulkan.o vulkan.o
OBJS-$(CONFIG_AVGBLUR_VULKAN_FILTER) += vf_avgblur_vulkan.o vulkan.o vulkan_filter.o
OBJS-$(CONFIG_BBOX_FILTER) += bbox.o vf_bbox.o
OBJS-$(CONFIG_BENCH_FILTER) += f_bench.o
OBJS-$(CONFIG_BILATERAL_FILTER) += vf_bilateral.o
@ -194,7 +194,7 @@ OBJS-$(CONFIG_BOXBLUR_OPENCL_FILTER) += vf_avgblur_opencl.o opencl.o \
opencl/avgblur.o boxblur.o
OBJS-$(CONFIG_BWDIF_FILTER) += vf_bwdif.o yadif_common.o
OBJS-$(CONFIG_CAS_FILTER) += vf_cas.o
OBJS-$(CONFIG_CHROMABER_VULKAN_FILTER) += vf_chromaber_vulkan.o vulkan.o
OBJS-$(CONFIG_CHROMABER_VULKAN_FILTER) += vf_chromaber_vulkan.o vulkan.o vulkan_filter.o
OBJS-$(CONFIG_CHROMAHOLD_FILTER) += vf_chromakey.o
OBJS-$(CONFIG_CHROMAKEY_FILTER) += vf_chromakey.o
OBJS-$(CONFIG_CHROMANR_FILTER) += vf_chromanr.o
@ -289,7 +289,7 @@ OBJS-$(CONFIG_FREEZEFRAMES_FILTER) += vf_freezeframes.o
OBJS-$(CONFIG_FREI0R_FILTER) += vf_frei0r.o
OBJS-$(CONFIG_FSPP_FILTER) += vf_fspp.o qp_table.o
OBJS-$(CONFIG_GBLUR_FILTER) += vf_gblur.o
OBJS-$(CONFIG_GBLUR_VULKAN_FILTER) += vf_gblur_vulkan.o vulkan.o
OBJS-$(CONFIG_GBLUR_VULKAN_FILTER) += vf_gblur_vulkan.o vulkan.o vulkan_filter.o
OBJS-$(CONFIG_GEQ_FILTER) += vf_geq.o
OBJS-$(CONFIG_GRADFUN_FILTER) += vf_gradfun.o
OBJS-$(CONFIG_GRAPHMONITOR_FILTER) += f_graphmonitor.o
@ -324,7 +324,7 @@ OBJS-$(CONFIG_LAGFUN_FILTER) += vf_lagfun.o
OBJS-$(CONFIG_LATENCY_FILTER) += f_latency.o
OBJS-$(CONFIG_LENSCORRECTION_FILTER) += vf_lenscorrection.o
OBJS-$(CONFIG_LENSFUN_FILTER) += vf_lensfun.o
OBJS-$(CONFIG_LIBPLACEBO_FILTER) += vf_libplacebo.o vulkan.o
OBJS-$(CONFIG_LIBPLACEBO_FILTER) += vf_libplacebo.o vulkan.o vulkan_filter.o
OBJS-$(CONFIG_LIBVMAF_FILTER) += vf_libvmaf.o framesync.o
OBJS-$(CONFIG_LIMITDIFF_FILTER) += vf_limitdiff.o framesync.o
OBJS-$(CONFIG_LIMITER_FILTER) += vf_limiter.o
@ -370,7 +370,7 @@ OBJS-$(CONFIG_OVERLAY_CUDA_FILTER) += vf_overlay_cuda.o framesync.o vf
OBJS-$(CONFIG_OVERLAY_OPENCL_FILTER) += vf_overlay_opencl.o opencl.o \
opencl/overlay.o framesync.o
OBJS-$(CONFIG_OVERLAY_QSV_FILTER) += vf_overlay_qsv.o framesync.o
OBJS-$(CONFIG_OVERLAY_VULKAN_FILTER) += vf_overlay_vulkan.o vulkan.o
OBJS-$(CONFIG_OVERLAY_VULKAN_FILTER) += vf_overlay_vulkan.o vulkan.o vulkan_filter.o
OBJS-$(CONFIG_OWDENOISE_FILTER) += vf_owdenoise.o
OBJS-$(CONFIG_PAD_FILTER) += vf_pad.o
OBJS-$(CONFIG_PAD_OPENCL_FILTER) += vf_pad_opencl.o opencl.o opencl/pad.o
@ -415,7 +415,7 @@ OBJS-$(CONFIG_SCALE_CUDA_FILTER) += vf_scale_cuda.o scale_eval.o \
OBJS-$(CONFIG_SCALE_NPP_FILTER) += vf_scale_npp.o scale_eval.o
OBJS-$(CONFIG_SCALE_QSV_FILTER) += vf_scale_qsv.o
OBJS-$(CONFIG_SCALE_VAAPI_FILTER) += vf_scale_vaapi.o scale_eval.o vaapi_vpp.o
OBJS-$(CONFIG_SCALE_VULKAN_FILTER) += vf_scale_vulkan.o vulkan.o
OBJS-$(CONFIG_SCALE_VULKAN_FILTER) += vf_scale_vulkan.o vulkan.o vulkan_filter.o
OBJS-$(CONFIG_SCALE2REF_FILTER) += vf_scale.o scale_eval.o
OBJS-$(CONFIG_SCALE2REF_NPP_FILTER) += vf_scale_npp.o scale_eval.o
OBJS-$(CONFIG_SCDET_FILTER) += vf_scdet.o

View File

@ -18,7 +18,7 @@
#include "libavutil/random_seed.h"
#include "libavutil/opt.h"
#include "vulkan.h"
#include "vulkan_filter.h"
#include "internal.h"
#define CGS 32

View File

@ -18,7 +18,7 @@
#include "libavutil/random_seed.h"
#include "libavutil/opt.h"
#include "vulkan.h"
#include "vulkan_filter.h"
#include "internal.h"
#define CGROUPS (int [3]){ 32, 32, 1 }

View File

@ -19,7 +19,7 @@
#include "libavutil/random_seed.h"
#include "libavutil/opt.h"
#include "vulkan.h"
#include "vulkan_filter.h"
#include "internal.h"
#define CGS 32

View File

@ -19,7 +19,7 @@
#include "libavutil/file.h"
#include "libavutil/opt.h"
#include "internal.h"
#include "vulkan.h"
#include "vulkan_filter.h"
#include "scale_eval.h"
#include <libplacebo/renderer.h>

View File

@ -18,7 +18,7 @@
#include "libavutil/random_seed.h"
#include "libavutil/opt.h"
#include "vulkan.h"
#include "vulkan_filter.h"
#include "internal.h"
#include "framesync.h"

View File

@ -18,7 +18,7 @@
#include "libavutil/random_seed.h"
#include "libavutil/opt.h"
#include "vulkan.h"
#include "vulkan_filter.h"
#include "scale_eval.h"
#include "internal.h"
#include "colorspace.h"

View File

@ -16,177 +16,4 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "vulkan.h"
#include "libavutil/vulkan.c"
static int vulkan_filter_set_device(AVFilterContext *avctx,
AVBufferRef *device)
{
FFVulkanContext *s = avctx->priv;
av_buffer_unref(&s->device_ref);
s->device_ref = av_buffer_ref(device);
if (!s->device_ref)
return AVERROR(ENOMEM);
s->device = (AVHWDeviceContext*)s->device_ref->data;
s->hwctx = s->device->hwctx;
return 0;
}
static int vulkan_filter_set_frames(AVFilterContext *avctx,
AVBufferRef *frames)
{
FFVulkanContext *s = avctx->priv;
av_buffer_unref(&s->frames_ref);
s->frames_ref = av_buffer_ref(frames);
if (!s->frames_ref)
return AVERROR(ENOMEM);
return 0;
}
int ff_vk_filter_config_input(AVFilterLink *inlink)
{
int err;
AVFilterContext *avctx = inlink->dst;
FFVulkanContext *s = avctx->priv;
FFVulkanFunctions *vk = &s->vkfn;
AVHWFramesContext *input_frames;
if (!inlink->hw_frames_ctx) {
av_log(avctx, AV_LOG_ERROR, "Vulkan filtering requires a "
"hardware frames context on the input.\n");
return AVERROR(EINVAL);
}
/* Extract the device and default output format from the first input. */
if (avctx->inputs[0] != inlink)
return 0;
input_frames = (AVHWFramesContext *)inlink->hw_frames_ctx->data;
if (input_frames->format != AV_PIX_FMT_VULKAN)
return AVERROR(EINVAL);
err = vulkan_filter_set_device(avctx, input_frames->device_ref);
if (err < 0)
return err;
err = vulkan_filter_set_frames(avctx, inlink->hw_frames_ctx);
if (err < 0)
return err;
s->extensions = ff_vk_extensions_to_mask(s->hwctx->enabled_dev_extensions,
s->hwctx->nb_enabled_dev_extensions);
err = ff_vk_load_functions(s->device, &s->vkfn, s->extensions, 1, 1);
if (err < 0)
return err;
vk->GetPhysicalDeviceProperties(s->hwctx->phys_dev, &s->props);
vk->GetPhysicalDeviceMemoryProperties(s->hwctx->phys_dev, &s->mprops);
/* Default output parameters match input parameters. */
s->input_format = input_frames->sw_format;
if (s->output_format == AV_PIX_FMT_NONE)
s->output_format = input_frames->sw_format;
if (!s->output_width)
s->output_width = inlink->w;
if (!s->output_height)
s->output_height = inlink->h;
return 0;
}
int ff_vk_filter_config_output_inplace(AVFilterLink *outlink)
{
int err;
AVFilterContext *avctx = outlink->src;
FFVulkanContext *s = avctx->priv;
av_buffer_unref(&outlink->hw_frames_ctx);
if (!s->device_ref) {
if (!avctx->hw_device_ctx) {
av_log(avctx, AV_LOG_ERROR, "Vulkan filtering requires a "
"Vulkan device.\n");
return AVERROR(EINVAL);
}
err = vulkan_filter_set_device(avctx, avctx->hw_device_ctx);
if (err < 0)
return err;
}
outlink->hw_frames_ctx = av_buffer_ref(s->frames_ref);
if (!outlink->hw_frames_ctx)
return AVERROR(ENOMEM);
outlink->w = s->output_width;
outlink->h = s->output_height;
return 0;
}
int ff_vk_filter_config_output(AVFilterLink *outlink)
{
int err;
AVFilterContext *avctx = outlink->src;
FFVulkanContext *s = avctx->priv;
AVBufferRef *output_frames_ref;
AVHWFramesContext *output_frames;
av_buffer_unref(&outlink->hw_frames_ctx);
if (!s->device_ref) {
if (!avctx->hw_device_ctx) {
av_log(avctx, AV_LOG_ERROR, "Vulkan filtering requires a "
"Vulkan device.\n");
return AVERROR(EINVAL);
}
err = vulkan_filter_set_device(avctx, avctx->hw_device_ctx);
if (err < 0)
return err;
}
output_frames_ref = av_hwframe_ctx_alloc(s->device_ref);
if (!output_frames_ref) {
err = AVERROR(ENOMEM);
goto fail;
}
output_frames = (AVHWFramesContext*)output_frames_ref->data;
output_frames->format = AV_PIX_FMT_VULKAN;
output_frames->sw_format = s->output_format;
output_frames->width = s->output_width;
output_frames->height = s->output_height;
err = av_hwframe_ctx_init(output_frames_ref);
if (err < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to initialise output "
"frames: %d.\n", err);
goto fail;
}
outlink->hw_frames_ctx = output_frames_ref;
outlink->w = s->output_width;
outlink->h = s->output_height;
return 0;
fail:
av_buffer_unref(&output_frames_ref);
return err;
}
int ff_vk_filter_init(AVFilterContext *avctx)
{
FFVulkanContext *s = avctx->priv;
s->output_format = AV_PIX_FMT_NONE;
return 0;
}

View File

@ -19,15 +19,6 @@
#ifndef AVFILTER_VULKAN_H
#define AVFILTER_VULKAN_H
#include "avfilter.h"
#include "libavutil/vulkan.h"
/**
* General lavfi IO functions
*/
int ff_vk_filter_init (AVFilterContext *avctx);
int ff_vk_filter_config_input (AVFilterLink *inlink);
int ff_vk_filter_config_output (AVFilterLink *outlink);
int ff_vk_filter_config_output_inplace(AVFilterLink *outlink);
#endif /* AVFILTER_VULKAN_H */

191
libavfilter/vulkan_filter.c Normal file
View File

@ -0,0 +1,191 @@
/*
* 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 "vulkan_filter.h"
static int vulkan_filter_set_device(AVFilterContext *avctx,
AVBufferRef *device)
{
FFVulkanContext *s = avctx->priv;
av_buffer_unref(&s->device_ref);
s->device_ref = av_buffer_ref(device);
if (!s->device_ref)
return AVERROR(ENOMEM);
s->device = (AVHWDeviceContext*)s->device_ref->data;
s->hwctx = s->device->hwctx;
return 0;
}
static int vulkan_filter_set_frames(AVFilterContext *avctx,
AVBufferRef *frames)
{
FFVulkanContext *s = avctx->priv;
av_buffer_unref(&s->frames_ref);
s->frames_ref = av_buffer_ref(frames);
if (!s->frames_ref)
return AVERROR(ENOMEM);
return 0;
}
int ff_vk_filter_config_input(AVFilterLink *inlink)
{
int err;
AVFilterContext *avctx = inlink->dst;
FFVulkanContext *s = avctx->priv;
FFVulkanFunctions *vk = &s->vkfn;
AVHWFramesContext *input_frames;
if (!inlink->hw_frames_ctx) {
av_log(avctx, AV_LOG_ERROR, "Vulkan filtering requires a "
"hardware frames context on the input.\n");
return AVERROR(EINVAL);
}
/* Extract the device and default output format from the first input. */
if (avctx->inputs[0] != inlink)
return 0;
input_frames = (AVHWFramesContext *)inlink->hw_frames_ctx->data;
if (input_frames->format != AV_PIX_FMT_VULKAN)
return AVERROR(EINVAL);
err = vulkan_filter_set_device(avctx, input_frames->device_ref);
if (err < 0)
return err;
err = vulkan_filter_set_frames(avctx, inlink->hw_frames_ctx);
if (err < 0)
return err;
s->extensions = ff_vk_extensions_to_mask(s->hwctx->enabled_dev_extensions,
s->hwctx->nb_enabled_dev_extensions);
err = ff_vk_load_functions(s->device, &s->vkfn, s->extensions, 1, 1);
if (err < 0)
return err;
vk->GetPhysicalDeviceProperties(s->hwctx->phys_dev, &s->props);
vk->GetPhysicalDeviceMemoryProperties(s->hwctx->phys_dev, &s->mprops);
/* Default output parameters match input parameters. */
s->input_format = input_frames->sw_format;
if (s->output_format == AV_PIX_FMT_NONE)
s->output_format = input_frames->sw_format;
if (!s->output_width)
s->output_width = inlink->w;
if (!s->output_height)
s->output_height = inlink->h;
return 0;
}
int ff_vk_filter_config_output_inplace(AVFilterLink *outlink)
{
int err;
AVFilterContext *avctx = outlink->src;
FFVulkanContext *s = avctx->priv;
av_buffer_unref(&outlink->hw_frames_ctx);
if (!s->device_ref) {
if (!avctx->hw_device_ctx) {
av_log(avctx, AV_LOG_ERROR, "Vulkan filtering requires a "
"Vulkan device.\n");
return AVERROR(EINVAL);
}
err = vulkan_filter_set_device(avctx, avctx->hw_device_ctx);
if (err < 0)
return err;
}
outlink->hw_frames_ctx = av_buffer_ref(s->frames_ref);
if (!outlink->hw_frames_ctx)
return AVERROR(ENOMEM);
outlink->w = s->output_width;
outlink->h = s->output_height;
return 0;
}
int ff_vk_filter_config_output(AVFilterLink *outlink)
{
int err;
AVFilterContext *avctx = outlink->src;
FFVulkanContext *s = avctx->priv;
AVBufferRef *output_frames_ref;
AVHWFramesContext *output_frames;
av_buffer_unref(&outlink->hw_frames_ctx);
if (!s->device_ref) {
if (!avctx->hw_device_ctx) {
av_log(avctx, AV_LOG_ERROR, "Vulkan filtering requires a "
"Vulkan device.\n");
return AVERROR(EINVAL);
}
err = vulkan_filter_set_device(avctx, avctx->hw_device_ctx);
if (err < 0)
return err;
}
output_frames_ref = av_hwframe_ctx_alloc(s->device_ref);
if (!output_frames_ref) {
err = AVERROR(ENOMEM);
goto fail;
}
output_frames = (AVHWFramesContext*)output_frames_ref->data;
output_frames->format = AV_PIX_FMT_VULKAN;
output_frames->sw_format = s->output_format;
output_frames->width = s->output_width;
output_frames->height = s->output_height;
err = av_hwframe_ctx_init(output_frames_ref);
if (err < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to initialise output "
"frames: %d.\n", err);
goto fail;
}
outlink->hw_frames_ctx = output_frames_ref;
outlink->w = s->output_width;
outlink->h = s->output_height;
return 0;
fail:
av_buffer_unref(&output_frames_ref);
return err;
}
int ff_vk_filter_init(AVFilterContext *avctx)
{
FFVulkanContext *s = avctx->priv;
s->output_format = AV_PIX_FMT_NONE;
return 0;
}

View File

@ -0,0 +1,34 @@
/*
* 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
*/
#ifndef AVFILTER_VULKAN_FILTER_H
#define AVFILTER_VULKAN_FILTER_H
#include "avfilter.h"
#include "vulkan.h"
/**
* General lavfi IO functions
*/
int ff_vk_filter_init (AVFilterContext *avctx);
int ff_vk_filter_config_input (AVFilterLink *inlink);
int ff_vk_filter_config_output (AVFilterLink *outlink);
int ff_vk_filter_config_output_inplace(AVFilterLink *outlink);
#endif /* AVFILTER_VULKAN_FILTER_H */

View File

@ -21,7 +21,7 @@
#include "vulkan.h"
#include "vulkan_loader.h"
#if FF_VK_ENABLE_SHADER_COMPILATION && CONFIG_LIBGLSLANG
#if CONFIG_LIBGLSLANG
#include "vulkan_glslang.c"
#endif
@ -857,7 +857,7 @@ int ff_vk_compile_shader(FFVulkanContext *s, FFVkSPIRVShader *shd,
shd->shader.pName = entrypoint;
if (!s->spirv_compiler) {
#if FF_VK_ENABLE_SHADER_COMPILATION && CONFIG_LIBGLSLANG
#if CONFIG_LIBGLSLANG
s->spirv_compiler = ff_vk_glslang_init();
#else
return AVERROR(ENOSYS);

View File

@ -22,15 +22,12 @@
#define VK_NO_PROTOTYPES
#define VK_ENABLE_BETA_EXTENSIONS
#ifndef FF_VK_ENABLE_SHADER_COMPILATION
#define FF_VK_ENABLE_SHADER_COMPILATION 1
#endif
#include "pixdesc.h"
#include "bprint.h"
#include "hwcontext.h"
#include "hwcontext_vulkan.h"
#include "vulkan_functions.h"
#include "vulkan_loader.h"
#define FF_VK_DEFAULT_USAGE_FLAGS (VK_IMAGE_USAGE_SAMPLED_BIT | \
VK_IMAGE_USAGE_STORAGE_BIT | \