From e2c83f546eca13f60cdbe6e37093f26e41f74870 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sat, 7 Mar 2020 00:35:10 +0100 Subject: [PATCH] ffmpeg/tools: Allow overriding option context --- source/ffmpeg/tools.cpp | 60 +++++++++++++++++++++++++++++------------ source/ffmpeg/tools.hpp | 5 ++++ 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/source/ffmpeg/tools.cpp b/source/ffmpeg/tools.cpp index 697dd750..d08a66a9 100644 --- a/source/ffmpeg/tools.cpp +++ b/source/ffmpeg/tools.cpp @@ -308,39 +308,65 @@ const char* tools::get_thread_type_name(int thread_type) } } -void tools::print_av_option_bool(AVCodecContext* context, const char* option, std::string text) +void tools::print_av_option_bool(AVCodecContext* ctx_codec, const char* option, std::string text) +{ + print_av_option_bool(ctx_codec, ctx_codec, option, text); +} + +void ffmpeg::tools::print_av_option_bool(AVCodecContext* ctx_codec, void* ctx_option, const char* option, + std::string text) { int64_t v = 0; - if (av_opt_get_int(context, option, AV_OPT_SEARCH_CHILDREN, &v) == 0) { - LOG_INFO("[%s] %s: %s%s", context->codec->name, text.c_str(), v == 0 ? "Disabled" : "Enabled", - av_opt_is_set_to_default_by_name(context, option, AV_OPT_SEARCH_CHILDREN) == 0 ? " " : ""); + if (int err = av_opt_get_int(ctx_option, option, AV_OPT_SEARCH_CHILDREN, &v); err != 0) { + LOG_INFO("[%s] %s: ", ctx_codec->codec->name, text.c_str(), + ffmpeg::tools::get_error_description(err)); } else { - LOG_INFO("[%s] %s: ", context->codec->name, text.c_str()); + LOG_INFO("[%s] %s: %s%s", ctx_codec->codec->name, text.c_str(), v == 0 ? "Disabled" : "Enabled", + av_opt_is_set_to_default_by_name(ctx_option, option, AV_OPT_SEARCH_CHILDREN) > 0 ? " " : ""); } } -void tools::print_av_option_int(AVCodecContext* context, const char* option, std::string text, std::string suffix) +void tools::print_av_option_int(AVCodecContext* ctx_codec, const char* option, std::string text, std::string suffix) { - int64_t v = 0; - if (av_opt_get_int(context, option, AV_OPT_SEARCH_CHILDREN, &v) == 0) { - LOG_INFO("[%s] %s: %lld %s%s", context->codec->name, text.c_str(), v, suffix.c_str(), - av_opt_is_set_to_default_by_name(context, option, AV_OPT_SEARCH_CHILDREN) == 0 ? " " : ""); + print_av_option_int(ctx_codec, ctx_codec, option, text, suffix); +} + +void ffmpeg::tools::print_av_option_int(AVCodecContext* ctx_codec, void* ctx_option, const char* option, + std::string text, std::string suffix) +{ + int64_t v = 0; + bool is_default = av_opt_is_set_to_default_by_name(ctx_option, option, AV_OPT_SEARCH_CHILDREN) > 0; + if (int err = av_opt_get_int(ctx_option, option, AV_OPT_SEARCH_CHILDREN, &v); err != 0) { + if (is_default) { + LOG_INFO("[%s] %s: ", ctx_codec->codec->name, text.c_str()); + } else { + LOG_INFO("[%s] %s: ", ctx_codec->codec->name, text.c_str(), + ffmpeg::tools::get_error_description(err)); + } } else { - LOG_INFO("[%s] %s: ", context->codec->name, text.c_str()); + LOG_INFO("[%s] %s: %lld %s%s", ctx_codec->codec->name, text.c_str(), v, suffix.c_str(), + is_default ? " " : ""); } } -void tools::print_av_option_string(AVCodecContext* context, const char* option, std::string text, +void tools::print_av_option_string(AVCodecContext* ctx_codec, const char* option, std::string text, std::function decoder) +{ + print_av_option_string(ctx_codec, ctx_codec, option, text, decoder); +} + +void ffmpeg::tools::print_av_option_string(AVCodecContext* ctx_codec, void* ctx_option, const char* option, + std::string text, std::function decoder) { int64_t v = 0; - if (av_opt_get_int(context, option, AV_OPT_SEARCH_CHILDREN, &v) == 0) { + if (int err = av_opt_get_int(ctx_option, option, AV_OPT_SEARCH_CHILDREN, &v); err != 0) { + LOG_INFO("[%s] %s: ", ctx_codec->codec->name, text.c_str(), + ffmpeg::tools::get_error_description(err)); + } else { std::string name = ""; if (decoder) name = decoder(v); - LOG_INFO("[%s] %s: %s%s", context->codec->name, text.c_str(), name.c_str(), - av_opt_is_set_to_default_by_name(context, option, AV_OPT_SEARCH_CHILDREN) == 0 ? " " : ""); - } else { - LOG_INFO("[%s] %s: ", context->codec->name, text.c_str()); + LOG_INFO("[%s] %s: %s%s", ctx_codec->codec->name, text.c_str(), name.c_str(), + av_opt_is_set_to_default_by_name(ctx_option, option, AV_OPT_SEARCH_CHILDREN) > 0 ? " " : ""); } } diff --git a/source/ffmpeg/tools.hpp b/source/ffmpeg/tools.hpp index 143291e6..ff30021f 100644 --- a/source/ffmpeg/tools.hpp +++ b/source/ffmpeg/tools.hpp @@ -60,10 +60,15 @@ namespace ffmpeg::tools { const char* get_thread_type_name(int thread_type); void print_av_option_bool(AVCodecContext* context, const char* option, std::string text); + void print_av_option_bool(AVCodecContext* ctx_codec, void* ctx_option, const char* option, std::string text); void print_av_option_int(AVCodecContext* context, const char* option, std::string text, std::string suffix); + void print_av_option_int(AVCodecContext* ctx_codec, void* ctx_option, const char* option, std::string text, + std::string suffix); void print_av_option_string(AVCodecContext* context, const char* option, std::string text, std::function decoder); + void print_av_option_string(AVCodecContext* ctx_codec, void* ctx_option, const char* option, std::string text, + std::function decoder); } // namespace ffmpeg::tools