diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b41e1c9..609d50b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1203,8 +1203,8 @@ if(T_CHECK) is_feature_enabled(ENCODER_FFMPEG_DNXHR T_CHECK) if(T_CHECK) list(APPEND PROJECT_PRIVATE_SOURCE - "source/encoders/ffmpeg/dnxhd_handler.hpp" - "source/encoders/ffmpeg/dnxhd_handler.cpp" + "source/encoders/ffmpeg/dnxhd.hpp" + "source/encoders/ffmpeg/dnxhd.cpp" ) list(APPEND PROJECT_DEFINITIONS ENABLE_ENCODER_FFMPEG_DNXHR diff --git a/source/encoders/ffmpeg/dnxhd_handler.cpp b/source/encoders/ffmpeg/dnxhd.cpp similarity index 67% rename from source/encoders/ffmpeg/dnxhd_handler.cpp rename to source/encoders/ffmpeg/dnxhd.cpp index fe4598de..37abf20f 100644 --- a/source/encoders/ffmpeg/dnxhd_handler.cpp +++ b/source/encoders/ffmpeg/dnxhd.cpp @@ -4,7 +4,7 @@ // Copyright (C) 2022-2023 Michael Fabian 'Xaymar' Dirks // AUTOGENERATED COPYRIGHT HEADER END -#include "dnxhd_handler.hpp" +#include "dnxhd.hpp" #include "common.hpp" #include "../codecs/dnxhr.hpp" #include "ffmpeg/tools.hpp" @@ -14,47 +14,9 @@ #include #include "warning-enable.hpp" -using namespace streamfx::encoder::ffmpeg::handler; +using namespace streamfx::encoder::ffmpeg; using namespace streamfx::encoder::codec::dnxhr; -void dnxhd_handler::adjust_info(ffmpeg_factory* fac, const AVCodec*, std::string&, std::string& name, std::string&) -{ - //Most people don't know what VC3 is and only know it as DNx. - //Change name to make it easier to find. - name = "Avid DNxHR (via FFmpeg)"; -} - -void dnxhd_handler::override_colorformat(AVPixelFormat& target_format, obs_data_t* settings, const AVCodec* codec, AVCodecContext*) -{ - static const std::array, static_cast(5)> profile_to_format_map{std::pair{"dnxhr_lb", AV_PIX_FMT_YUV422P}, std::pair{"dnxhr_sq", AV_PIX_FMT_YUV422P}, std::pair{"dnxhr_hq", AV_PIX_FMT_YUV422P}, std::pair{"dnxhr_hqx", AV_PIX_FMT_YUV422P10}, std::pair{"dnxhr_444", AV_PIX_FMT_YUV444P10}}; - - const char* selected_profile = obs_data_get_string(settings, S_CODEC_DNXHR_PROFILE); - for (const auto& kv : profile_to_format_map) { - if (strcmp(kv.first, selected_profile) == 0) { - target_format = kv.second; - return; - } - } - - // Fallback for (yet) unknown formats - target_format = AV_PIX_FMT_YUV422P; -} - -void dnxhd_handler::get_defaults(obs_data_t* settings, const AVCodec*, AVCodecContext*, bool) -{ - obs_data_set_default_string(settings, S_CODEC_DNXHR_PROFILE, "dnxhr_sq"); -} - -bool dnxhd_handler::has_keyframe_support(ffmpeg_factory* instance) -{ - return false; -} - -bool dnxhd_handler::has_pixel_format_support(ffmpeg_factory* instance) -{ - return false; -} - inline const char* dnx_profile_to_display_name(const char* profile) { char buffer[1024]; @@ -62,13 +24,34 @@ inline const char* dnx_profile_to_display_name(const char* profile) return D_TRANSLATE(buffer); } -void dnxhd_handler::get_properties(obs_properties_t* props, const AVCodec* codec, AVCodecContext* context, bool) +dnxhd::dnxhd() : handler("dnxhd") {} + +dnxhd::~dnxhd() {} + +void dnxhd::adjust_info(ffmpeg_factory* factory, std::string& id, std::string& name, std::string& codec) { - AVCodecContext* ctx = context; + //Most people don't know what VC3 is and only know it as DNx. + //Change name to make it easier to find. + name = "Avid DNxHR (via FFmpeg)"; +} + +bool dnxhd::has_keyframes(ffmpeg_factory* instance) +{ + return false; +} + +void dnxhd::defaults(ffmpeg_factory* factory, obs_data_t* settings) +{ + obs_data_set_default_string(settings, S_CODEC_DNXHR_PROFILE, "dnxhr_sq"); +} + +void dnxhd::properties(ffmpeg_factory* factory, ffmpeg_instance* instance, obs_properties_t* props) +{ + AVCodecContext* ctx = instance->get_avcodeccontext(); //Create dummy context if null was passed to the function if (!ctx) { - ctx = avcodec_alloc_context3(codec); + ctx = avcodec_alloc_context3(factory->get_avcodec()); if (!ctx->priv_data) { avcodec_free_context(&ctx); return; @@ -90,19 +73,37 @@ void dnxhd_handler::get_properties(obs_properties_t* props, const AVCodec* codec }); //Free context if we created it here - if (ctx && ctx != context) { + if (ctx && ctx != instance->get_avcodeccontext()) { avcodec_free_context(&ctx); } } -void dnxhd_handler::update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context) +void dnxhd::update(ffmpeg_factory* factory, ffmpeg_instance* instance, obs_data_t* settings) { const char* profile = obs_data_get_string(settings, S_CODEC_DNXHR_PROFILE); - av_opt_set(context, "profile", profile, AV_OPT_SEARCH_CHILDREN); + av_opt_set(instance->get_avcodeccontext(), "profile", profile, AV_OPT_SEARCH_CHILDREN); } -void dnxhd_handler::log_options(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context) +void dnxhd::log(ffmpeg_factory* factory, ffmpeg_instance* instance, obs_data_t* settings) { - DLOG_INFO("[%s] Avid DNxHR:", codec->name); - streamfx::ffmpeg::tools::print_av_option_string2(context, "profile", " Profile", [](int64_t v, std::string_view o) { return std::string(o); }); + DLOG_INFO("[%s] Avid DNxHR:", factory->get_avcodec()->name); + streamfx::ffmpeg::tools::print_av_option_string2(instance->get_avcodeccontext(), "profile", " Profile", [](int64_t v, std::string_view o) { return std::string(o); }); } + +void dnxhd::override_colorformat(ffmpeg_factory* factory, ffmpeg_instance* instance, obs_data_t* settings, AVPixelFormat& target_format) +{ + static const std::array, static_cast(5)> profile_to_format_map{std::pair{"dnxhr_lb", AV_PIX_FMT_YUV422P}, std::pair{"dnxhr_sq", AV_PIX_FMT_YUV422P}, std::pair{"dnxhr_hq", AV_PIX_FMT_YUV422P}, std::pair{"dnxhr_hqx", AV_PIX_FMT_YUV422P10}, std::pair{"dnxhr_444", AV_PIX_FMT_YUV444P10}}; + + const char* selected_profile = obs_data_get_string(settings, S_CODEC_DNXHR_PROFILE); + for (const auto& kv : profile_to_format_map) { + if (strcmp(kv.first, selected_profile) == 0) { + target_format = kv.second; + return; + } + } + + // Fallback for (yet) unknown formats + target_format = AV_PIX_FMT_YUV422P; +} + +static auto inst = dnxhd(); diff --git a/source/encoders/ffmpeg/dnxhd.hpp b/source/encoders/ffmpeg/dnxhd.hpp new file mode 100644 index 00000000..0ba6cec5 --- /dev/null +++ b/source/encoders/ffmpeg/dnxhd.hpp @@ -0,0 +1,37 @@ +// AUTOGENERATED COPYRIGHT HEADER START +// Copyright (C) 2022 Carsten Braun +// Copyright (C) 2022-2023 Michael Fabian 'Xaymar' Dirks +// AUTOGENERATED COPYRIGHT HEADER END + +#pragma once +#include "encoders/encoder-ffmpeg.hpp" +#include "encoders/ffmpeg/handler.hpp" + +#include "warning-disable.hpp" +extern "C" { +#include +} +#include "warning-enable.hpp" + +namespace streamfx::encoder::ffmpeg { + class dnxhd : public handler { + public: + dnxhd(); + virtual ~dnxhd(); + + virtual bool has_keyframes(ffmpeg_factory* factory); + + virtual void adjust_info(ffmpeg_factory* factory, std::string& id, std::string& name, std::string& codec); + + virtual std::string help(ffmpeg_factory* factory) { + return "https://github.com/Xaymar/obs-StreamFX/wiki/Encoder-FFmpeg-Avid-DNxHR"; + } + + virtual void defaults(ffmpeg_factory* factory, obs_data_t* settings); + virtual void properties(ffmpeg_factory* factory, ffmpeg_instance* instance, obs_properties_t* props); + virtual void update(ffmpeg_factory* factory, ffmpeg_instance* instance, obs_data_t* settings); + virtual void log(ffmpeg_factory* factory, ffmpeg_instance* instance, obs_data_t* settings); + + virtual void override_colorformat(ffmpeg_factory* factory, ffmpeg_instance* instance, obs_data_t* settings, AVPixelFormat& target_format); + }; +} // namespace streamfx::encoder::ffmpeg diff --git a/source/encoders/ffmpeg/dnxhd_handler.hpp b/source/encoders/ffmpeg/dnxhd_handler.hpp deleted file mode 100644 index 919d806b..00000000 --- a/source/encoders/ffmpeg/dnxhd_handler.hpp +++ /dev/null @@ -1,47 +0,0 @@ -// AUTOGENERATED COPYRIGHT HEADER START -// Copyright (C) 2022 Carsten Braun -// Copyright (C) 2022-2023 Michael Fabian 'Xaymar' Dirks -// AUTOGENERATED COPYRIGHT HEADER END - -#pragma once -#include "handler.hpp" - -extern "C" { -#include "warning-disable.hpp" -#include -#include "warning-enable.hpp" -} - -namespace streamfx::encoder::ffmpeg::handler { - class dnxhd_handler : public handler { - public: - virtual ~dnxhd_handler(){}; - - public /*factory*/: - virtual void adjust_info(ffmpeg_factory* factory, const AVCodec* codec, std::string& id, std::string& name, std::string& codec_id); - - public /*factory*/: - void get_defaults(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context, bool hw_encode) override; - - virtual std::string_view get_help_url(const AVCodec* codec) override - { - return "https://github.com/Xaymar/obs-StreamFX/wiki/Encoder-FFmpeg-Avid-DNxHR"; - }; - - public /*support tests*/: - virtual bool has_keyframe_support(ffmpeg_factory* instance) override; - - public /*support tests*/: - bool has_pixel_format_support(ffmpeg_factory* instance) override; - - public /*settings*/: - void get_properties(obs_properties_t* props, const AVCodec* codec, AVCodecContext* context, bool hw_encode) override; - - void update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context) override; - - void log_options(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context) override; - - public /*instance*/: - void override_colorformat(AVPixelFormat& target_format, obs_data_t* settings, const AVCodec* codec, AVCodecContext* context) override; - }; -} // namespace streamfx::encoder::ffmpeg::handler