From 8c4bcf9f5987a1b92ab65547dcabeee90e2b3eb4 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Sat, 1 Feb 2020 12:28:13 +0800 Subject: [PATCH] ffmpeg: Add ListFormats and ListEncoders These two functions allow the frontend to get a list of encoders/formats and their specific options. Retrieving the options is harder than it sounds due to FFmpeg's strange AVClass and AVOption system. For example, for integer and flags options, 'named constants' can be set. They are of type `AV_OPT_TYPE_CONST` and are categoried according to the `unit` field. An option can recognize all constants of the same `unit`. --- src/core/CMakeLists.txt | 2 +- src/core/dumping/ffmpeg_backend.cpp | 243 +++++++++++++++++++++++++++- src/core/dumping/ffmpeg_backend.h | 41 +++++ 3 files changed, 284 insertions(+), 2 deletions(-) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index ab9f52e32..225a68796 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -492,5 +492,5 @@ if (ARCHITECTURE_x86_64) endif() if (ENABLE_FFMPEG_VIDEO_DUMPER) - target_link_libraries(core PRIVATE FFmpeg::avcodec FFmpeg::avformat FFmpeg::swscale FFmpeg::swresample FFmpeg::avutil) + target_link_libraries(core PUBLIC FFmpeg::avcodec FFmpeg::avformat FFmpeg::swscale FFmpeg::swresample FFmpeg::avutil) endif() diff --git a/src/core/dumping/ffmpeg_backend.cpp b/src/core/dumping/ffmpeg_backend.cpp index 3c34c8440..68b148bd3 100644 --- a/src/core/dumping/ffmpeg_backend.cpp +++ b/src/core/dumping/ffmpeg_backend.cpp @@ -2,17 +2,19 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include "common/assert.h" #include "common/file_util.h" #include "common/logging/log.h" #include "common/param_package.h" +#include "common/string_util.h" #include "core/dumping/ffmpeg_backend.h" #include "core/settings.h" #include "video_core/renderer_base.h" #include "video_core/video_core.h" extern "C" { -#include +#include } namespace VideoDumper { @@ -591,4 +593,243 @@ void FFmpegBackend::EndDumping() { processing_ended.Set(); } +// To std string, but handles nullptr +std::string ToStdString(const char* str, const std::string& fallback = "") { + return str ? std::string{str} : fallback; +} + +std::string FormatDuration(s64 duration) { + // The following is implemented according to libavutil code (opt.c) + std::string out; + if (duration < 0 && duration != std::numeric_limits::min()) { + out.append("-"); + duration = -duration; + } + if (duration == std::numeric_limits::max()) { + return "INT64_MAX"; + } else if (duration == std::numeric_limits::min()) { + return "INT64_MIN"; + } else if (duration > 3600ll * 1000000ll) { + out.append(fmt::format("{}:{:02d}:{:02d}.{:06d}", duration / 3600000000ll, + ((duration / 60000000ll) % 60), ((duration / 1000000ll) % 60), + duration % 1000000)); + } else if (duration > 60ll * 1000000ll) { + out.append(fmt::format("{}:{:02d}.{:06d}", duration / 60000000ll, + ((duration / 1000000ll) % 60), duration % 1000000)); + } else { + out.append(fmt::format("{}.{:06d}", duration / 1000000ll, duration % 1000000)); + } + while (out.back() == '0') { + out.erase(out.size() - 1, 1); + } + if (out.back() == '.') { + out.erase(out.size() - 1, 1); + } + return out; +} + +std::string FormatDefaultValue(const AVOption* option, + const std::vector& named_constants) { + // The following is taken and modified from libavutil code (opt.c) + switch (option->type) { + case AV_OPT_TYPE_BOOL: { + const auto value = option->default_val.i64; + if (value < 0) { + return "auto"; + } + return value ? "true" : "false"; + } + case AV_OPT_TYPE_FLAGS: { + const auto value = option->default_val.i64; + std::string out; + for (const auto& constant : named_constants) { + if (!(value & constant.value)) { + continue; + } + if (!out.empty()) { + out.append("+"); + } + out.append(constant.name); + } + return out.empty() ? fmt::format("{}", value) : out; + } + case AV_OPT_TYPE_DURATION: { + return FormatDuration(option->default_val.i64); + } + case AV_OPT_TYPE_INT: + case AV_OPT_TYPE_UINT64: + case AV_OPT_TYPE_INT64: { + const auto value = option->default_val.i64; + for (const auto& constant : named_constants) { + if (constant.value == value) { + return constant.name; + } + } + return fmt::format("{}", value); + } + case AV_OPT_TYPE_DOUBLE: + case AV_OPT_TYPE_FLOAT: { + return fmt::format("{}", option->default_val.dbl); + } + case AV_OPT_TYPE_RATIONAL: { + const auto q = av_d2q(option->default_val.dbl, std::numeric_limits::max()); + return fmt::format("{}/{}", q.num, q.den); + } + case AV_OPT_TYPE_PIXEL_FMT: { + const char* name = av_get_pix_fmt_name(static_cast(option->default_val.i64)); + return ToStdString(name, "none"); + } + case AV_OPT_TYPE_SAMPLE_FMT: { + const char* name = + av_get_sample_fmt_name(static_cast(option->default_val.i64)); + return ToStdString(name, "none"); + } + case AV_OPT_TYPE_COLOR: + case AV_OPT_TYPE_IMAGE_SIZE: + case AV_OPT_TYPE_STRING: + case AV_OPT_TYPE_DICT: + case AV_OPT_TYPE_VIDEO_RATE: { + return ToStdString(option->default_val.str); + } + case AV_OPT_TYPE_CHANNEL_LAYOUT: { + return fmt::format("{:#x}", option->default_val.i64); + } + default: + return ""; + } +} + +void GetOptionListSingle(std::vector& out, const AVClass* av_class) { + if (av_class == nullptr) { + return; + } + + const AVOption* current = nullptr; + std::unordered_map> named_constants_map; + // First iteration: find and place all named constants + while ((current = av_opt_next(&av_class, current))) { + if (current->type != AV_OPT_TYPE_CONST || !current->unit) { + continue; + } + named_constants_map[current->unit].push_back( + {current->name, ToStdString(current->help), current->default_val.i64}); + } + // Second iteration: find all options + current = nullptr; + while ((current = av_opt_next(&av_class, current))) { + // Currently we cannot handle binary options + if (current->type == AV_OPT_TYPE_CONST || current->type == AV_OPT_TYPE_BINARY) { + continue; + } + std::vector named_constants; + if (current->unit && named_constants_map.count(current->unit)) { + named_constants = named_constants_map.at(current->unit); + } + const auto default_value = FormatDefaultValue(current, named_constants); + out.push_back({current->name, ToStdString(current->help), current->type, default_value, + std::move(named_constants), current->min, current->max}); + } +} + +void GetOptionList(std::vector& out, const AVClass* av_class) { + if (av_class == nullptr) { + return; + } + + GetOptionListSingle(out, av_class); + + const AVClass* child_class = nullptr; + while ((child_class = av_opt_child_class_next(av_class, child_class))) { + GetOptionListSingle(out, child_class); + } +} + +std::vector GetOptionList(const AVClass* av_class) { + std::vector out; + GetOptionList(out, av_class); + + // Filter out identical options (why do they exist in the first place?) + std::unordered_set option_name_set; + std::vector final_out; + for (auto& option : out) { + if (option_name_set.count(option.name)) { + continue; + } + option_name_set.emplace(option.name); + final_out.emplace_back(std::move(option)); + } + + return final_out; +} + +std::vector ListEncoders(AVMediaType type) { + InitializeFFmpegLibraries(); + + const auto general_options = GetOptionList(avcodec_get_class()); + + std::vector out; + + const AVCodec* current = nullptr; +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100) + while ((current = av_codec_next(current))) { +#else + void* data = nullptr; // For libavcodec to save the iteration state + while ((current = av_codec_iterate(&data))) { +#endif + if (!av_codec_is_encoder(current) || current->type != type) { + continue; + } + auto options = GetOptionList(current->priv_class); + options.insert(options.end(), general_options.begin(), general_options.end()); + out.push_back( + {current->name, ToStdString(current->long_name), current->id, std::move(options)}); + } + return out; +} + +std::vector ListFormats() { + InitializeFFmpegLibraries(); + + const auto general_options = GetOptionList(avformat_get_class()); + + std::vector out; + + const AVOutputFormat* current = nullptr; +#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 9, 100) + while ((current = av_oformat_next(current))) { +#else + void* data = nullptr; // For libavformat to save the iteration state + while ((current = av_muxer_iterate(&data))) { +#endif + auto options = GetOptionList(current->priv_class); + options.insert(options.end(), general_options.begin(), general_options.end()); + + std::vector extensions; + Common::SplitString(ToStdString(current->extensions), ',', extensions); + + std::set supported_video_codecs; + std::set supported_audio_codecs; + // Go through all codecs + const AVCodecDescriptor* codec = nullptr; + while ((codec = avcodec_descriptor_next(codec))) { + if (avformat_query_codec(current, codec->id, FF_COMPLIANCE_NORMAL) == 1) { + if (codec->type == AVMEDIA_TYPE_VIDEO) { + supported_video_codecs.emplace(codec->id); + } else if (codec->type == AVMEDIA_TYPE_AUDIO) { + supported_audio_codecs.emplace(codec->id); + } + } + } + + if (supported_video_codecs.empty() || supported_audio_codecs.empty()) { + continue; + } + + out.push_back({current->name, ToStdString(current->long_name), std::move(extensions), + std::move(supported_video_codecs), std::move(supported_audio_codecs), + std::move(options)}); + } + return out; +} + } // namespace VideoDumper diff --git a/src/core/dumping/ffmpeg_backend.h b/src/core/dumping/ffmpeg_backend.h index f0962189e..e2c605c8c 100644 --- a/src/core/dumping/ffmpeg_backend.h +++ b/src/core/dumping/ffmpeg_backend.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include "common/common_types.h" @@ -19,6 +20,7 @@ extern "C" { #include #include +#include #include #include } @@ -188,4 +190,43 @@ private: Common::Event processing_ended; }; +/// Struct describing encoder/muxer options +struct OptionInfo { + std::string name; + std::string description; + AVOptionType type; + std::string default_value; + struct NamedConstant { + std::string name; + std::string description; + s64 value; + }; + std::vector named_constants; + + // If this is a scalar type + double min; + double max; +}; + +/// Struct describing an encoder +struct EncoderInfo { + std::string name; + std::string long_name; + AVCodecID codec; + std::vector options; +}; + +/// Struct describing a format +struct FormatInfo { + std::string name; + std::string long_name; + std::vector extensions; + std::set supported_video_codecs; + std::set supported_audio_codecs; + std::vector options; +}; + +std::vector ListEncoders(AVMediaType type); +std::vector ListFormats(); + } // namespace VideoDumper