diff --git a/include/libvideo2x/libplacebo.h b/include/libvideo2x/libplacebo.h index 647672e..b8ab418 100644 --- a/include/libvideo2x/libplacebo.h +++ b/include/libvideo2x/libplacebo.h @@ -9,13 +9,13 @@ extern "C" { } int init_libplacebo( - AVBufferRef *hw_ctx, AVFilterGraph **filter_graph, AVFilterContext **buffersrc_ctx, AVFilterContext **buffersink_ctx, AVCodecContext *dec_ctx, int out_width, int out_height, + uint32_t vk_device_index, const std::filesystem::path &shader_path ); diff --git a/include/libvideo2x/libplacebo_filter.h b/include/libvideo2x/libplacebo_filter.h index f03a8ef..aed1b4f 100644 --- a/include/libvideo2x/libplacebo_filter.h +++ b/include/libvideo2x/libplacebo_filter.h @@ -17,15 +17,21 @@ class LibplaceboFilter : public Filter { AVFilterGraph *filter_graph; AVFilterContext *buffersrc_ctx; AVFilterContext *buffersink_ctx; + uint32_t vk_device_index; + const std::filesystem::path shader_path; int out_width; int out_height; - const std::filesystem::path shader_path; AVRational in_time_base; AVRational out_time_base; public: // Constructor - LibplaceboFilter(int width, int height, const std::filesystem::path &shader_path); + LibplaceboFilter( + uint32_t vk_device_index, + const std::filesystem::path &shader_path, + int width, + int height + ); // Destructor virtual ~LibplaceboFilter() override; diff --git a/src/libplacebo.cpp b/src/libplacebo.cpp index feaf052..5d446d9 100644 --- a/src/libplacebo.cpp +++ b/src/libplacebo.cpp @@ -2,25 +2,37 @@ #include #include +#include extern "C" { +#include #include } #include int init_libplacebo( - AVBufferRef *hw_ctx, AVFilterGraph **filter_graph, AVFilterContext **buffersrc_ctx, AVFilterContext **buffersink_ctx, AVCodecContext *dec_ctx, int out_width, int out_height, + uint32_t vk_device_index, const std::filesystem::path &shader_path ) { int ret; + // Create the Vulkan hardware device context + AVBufferRef *vk_hw_device_ctx = nullptr; + ret = av_hwdevice_ctx_create( + &vk_hw_device_ctx, AV_HWDEVICE_TYPE_VULKAN, std::to_string(vk_device_index).c_str(), NULL, 0 + ); + if (ret < 0) { + spdlog::error("Failed to create Vulkan hardware device context."); + return ret; + } + AVFilterGraph *graph = avfilter_graph_alloc(); if (!graph) { spdlog::error("Unable to create filter graph."); @@ -106,9 +118,8 @@ int init_libplacebo( } // Set the hardware device context to Vulkan - if (hw_ctx != nullptr) { - libplacebo_ctx->hw_device_ctx = av_buffer_ref(hw_ctx); - } + libplacebo_ctx->hw_device_ctx = av_buffer_ref(vk_hw_device_ctx); + av_buffer_unref(&vk_hw_device_ctx); // Link buffersrc to libplacebo ret = avfilter_link(last_filter, 0, libplacebo_ctx, 0); diff --git a/src/libplacebo_filter.cpp b/src/libplacebo_filter.cpp index 36f8965..22947c7 100644 --- a/src/libplacebo_filter.cpp +++ b/src/libplacebo_filter.cpp @@ -9,16 +9,18 @@ #include "libplacebo.h" LibplaceboFilter::LibplaceboFilter( + uint32_t vk_device_index, + const std::filesystem::path &shader_path, int out_width, - int out_height, - const std::filesystem::path &shader_path + int out_height ) : filter_graph(nullptr), buffersrc_ctx(nullptr), buffersink_ctx(nullptr), + vk_device_index(vk_device_index), + shader_path(std::move(shader_path)), out_width(out_width), - out_height(out_height), - shader_path(std::move(shader_path)) {} + out_height(out_height) {} LibplaceboFilter::~LibplaceboFilter() { if (buffersrc_ctx) { @@ -35,7 +37,7 @@ LibplaceboFilter::~LibplaceboFilter() { } } -int LibplaceboFilter::init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVBufferRef *hw_ctx) { +int LibplaceboFilter::init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVBufferRef *_) { // Construct the shader path std::filesystem::path shader_full_path; if (filepath_is_readable(shader_path)) { @@ -61,13 +63,13 @@ int LibplaceboFilter::init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVB // Initialize the libplacebo filter int ret = init_libplacebo( - hw_ctx, &filter_graph, &buffersrc_ctx, &buffersink_ctx, dec_ctx, out_width, out_height, + vk_device_index, shader_full_path ); diff --git a/src/libvideo2x.cpp b/src/libvideo2x.cpp index 0d55d45..354c697 100644 --- a/src/libvideo2x.cpp +++ b/src/libvideo2x.cpp @@ -413,7 +413,10 @@ extern "C" int process_video( return -1; } filter = new LibplaceboFilter{ - config.out_width, config.out_height, std::filesystem::path(config.shader_path) + vk_device_index, + std::filesystem::path(config.shader_path), + config.out_width, + config.out_height }; } else if (filter_config->filter_type == FILTER_REALESRGAN) { const auto &config = filter_config->config.realesrgan; diff --git a/src/video2x.cpp b/src/video2x.cpp index c7f3078..84ff597 100644 --- a/src/video2x.cpp +++ b/src/video2x.cpp @@ -702,7 +702,7 @@ int main(int argc, char **argv) { spdlog::warn("Video processing aborted"); return 2; } else if (proc_ret != 0) { - spdlog::error("Video processing failed with error code {}", proc_ret); + spdlog::critical("Video processing failed with error code {}", proc_ret); return 1; } else { spdlog::info("Video processed successfully");