Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fe71944199 | |||
| cbd39a8c2a | |||
| 62eae3827b | |||
| 38e7639862 | |||
| 6bc1cb9c88 | |||
| c63900d575 | |||
| 9efda8af8d | |||
| 0c9764a15c | |||
| 5c5a235502 | |||
| 2ebf90ffd7 | |||
| a32f8dd28b | |||
| d8a692de93 | |||
| a9f39527f6 | |||
| 71440ed3c5 | |||
| 3bd147e6e7 | |||
| 8b6af720bf | |||
| 993a4f8110 |
@@ -272,6 +272,8 @@ set(PROJECT_PRIVATE
|
||||
"${PROJECT_SOURCE_DIR}/source/codecs/hevc.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/source/codecs/h264.hpp"
|
||||
"${PROJECT_SOURCE_DIR}/source/codecs/h264.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/source/codecs/prores.hpp"
|
||||
"${PROJECT_SOURCE_DIR}/source/codecs/prores.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/source/ffmpeg/avframe-queue.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/source/ffmpeg/avframe-queue.hpp"
|
||||
"${PROJECT_SOURCE_DIR}/source/ffmpeg/swscale.hpp"
|
||||
|
||||
+9
-14
@@ -48,20 +48,6 @@ KeyFrames.IntervalType.Frames="Frames"
|
||||
KeyFrames.IntervalType.Seconds="Seconds"
|
||||
KeyFrames.Interval="Interval"
|
||||
|
||||
# Apple ProRes
|
||||
AppleProRes.Profile="Profile"
|
||||
AppleProRes.Profile.APCO="Proxy"
|
||||
AppleProRes.Profile.APCS="LT"
|
||||
AppleProRes.Profile.APCN="Standard Definition"
|
||||
AppleProRes.Profile.APCH="High Quality"
|
||||
AppleProRes.Profile.AP4H="4444"
|
||||
|
||||
# ProRes
|
||||
ProRes.Profile.Proxy="Proxy (PXY)"
|
||||
ProRes.Profile.Light="Light (LT)"
|
||||
ProRes.Profile.Standard="Standard"
|
||||
ProRes.Profile.HighQuality="High Quality (HQ)"
|
||||
|
||||
# Codec: H264
|
||||
Codec.H264="H264"
|
||||
Codec.H264.Profile="Profile"
|
||||
@@ -82,6 +68,15 @@ Codec.HEVC.Tier.main="Main"
|
||||
Codec.HEVC.Tier.high="High"
|
||||
Codec.HEVC.Level="Level"
|
||||
|
||||
# Codec: Apple ProRes
|
||||
Codec.ProRes.Profile="Profile"
|
||||
Codec.ProRes.Profile.APCO="422 Proxy/PXY (APCO)"
|
||||
Codec.ProRes.Profile.APCS="422 Light/LT (APCS)"
|
||||
Codec.ProRes.Profile.APCN="422 Standard (APCN)"
|
||||
Codec.ProRes.Profile.APCH="422 High Quality/HQ (APCH)"
|
||||
Codec.ProRes.Profile.AP4H="4444 Standard (AP4H)"
|
||||
Codec.ProRes.Profile.AP4X="4444 Extra Quality/XQ (AP4X)"
|
||||
|
||||
# NVENC
|
||||
NVENC.Preset="Preset"
|
||||
NVENC.Preset.Default="Default"
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// FFMPEG Video Encoder Integration for OBS Studio
|
||||
// Copyright (c) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include "prores.hpp"
|
||||
@@ -0,0 +1,32 @@
|
||||
// FFMPEG Video Encoder Integration for OBS Studio
|
||||
// Copyright (c) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#pragma once
|
||||
|
||||
// Codec: ProRes
|
||||
#define P_PRORES "Codec.ProRes"
|
||||
#define P_PRORES_PROFILE "Codec.ProRes.Profile"
|
||||
#define P_PRORES_PROFILE_APCS "Codec.ProRes.Profile.APCS"
|
||||
#define P_PRORES_PROFILE_APCO "Codec.ProRes.Profile.APCO"
|
||||
#define P_PRORES_PROFILE_APCN "Codec.ProRes.Profile.APCN"
|
||||
#define P_PRORES_PROFILE_APCH "Codec.ProRes.Profile.APCH"
|
||||
#define P_PRORES_PROFILE_AP4H "Codec.ProRes.Profile.AP4H"
|
||||
#define P_PRORES_PROFILE_AP4X "Codec.ProRes.Profile.AP4X"
|
||||
+442
-307
@@ -25,6 +25,7 @@
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
#include <util/profiler.hpp>
|
||||
#include <vector>
|
||||
#include "codecs/hevc.hpp"
|
||||
#include "ffmpeg/tools.hpp"
|
||||
#include "plugin.hpp"
|
||||
@@ -44,6 +45,13 @@ extern "C" {
|
||||
#pragma warning(pop)
|
||||
}
|
||||
|
||||
//#define DEBUG_CALL_ORDER
|
||||
// Call Order should be:
|
||||
// - create_texture/create
|
||||
// - get_video_info
|
||||
// - encode_texture/encode
|
||||
// I don't understand what get_video_info is actually for in this order, as this postpones initialization to encode...
|
||||
|
||||
// FFmpeg
|
||||
#define ST_FFMPEG "FFmpeg"
|
||||
#define ST_FFMPEG_CUSTOMSETTINGS "FFmpeg.CustomSettings"
|
||||
@@ -53,8 +61,230 @@ extern "C" {
|
||||
|
||||
enum class keyframe_type { SECONDS, FRAMES };
|
||||
|
||||
obsffmpeg::encoder_factory::encoder_factory(const AVCodec* codec) : avcodec_ptr(codec), info()
|
||||
static void* _create(obs_data_t* settings, obs_encoder_t* encoder) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX %llX", __FUNCTION_NAME__, settings, encoder);
|
||||
#endif
|
||||
return reinterpret_cast<void*>(new obsffmpeg::encoder(settings, encoder));
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
return nullptr;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void* _create_texture(obs_data_t* settings, obs_encoder_t* encoder) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX %llX", __FUNCTION_NAME__, settings, encoder);
|
||||
#endif
|
||||
return reinterpret_cast<void*>(new obsffmpeg::encoder(settings, encoder, true));
|
||||
} catch (const obsffmpeg::unsupported_gpu_exception&) {
|
||||
obsffmpeg::encoder_factory* fac =
|
||||
reinterpret_cast<obsffmpeg::encoder_factory*>(obs_encoder_get_type_data(encoder));
|
||||
PLOG_WARNING("<%s> GPU not supported for hardware encoding, falling back to software.",
|
||||
fac->get_avcodec()->name);
|
||||
return obs_encoder_create_rerouted(encoder, fac->get_fallback().oei.id);
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
return nullptr;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void _destroy(void* ptr) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX", __FUNCTION_NAME__, ptr);
|
||||
#endif
|
||||
if (ptr)
|
||||
delete reinterpret_cast<obsffmpeg::encoder*>(ptr);
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
}
|
||||
|
||||
static const char* _get_name(void* type_data) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX", __FUNCTION_NAME__, type_data);
|
||||
#endif
|
||||
return reinterpret_cast<obsffmpeg::encoder_factory*>(type_data)->get_info().readable_name.c_str();
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
return nullptr;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static const char* _get_name_fallback(void* type_data) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX", __FUNCTION_NAME__, type_data);
|
||||
#endif
|
||||
return reinterpret_cast<obsffmpeg::encoder_factory*>(type_data)->get_fallback().readable_name.c_str();
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
return nullptr;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void _get_defaults(obs_data_t* settings, void* type_data) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX %llX", __FUNCTION_NAME__, settings, type_data);
|
||||
#endif
|
||||
reinterpret_cast<obsffmpeg::encoder_factory*>(type_data)->get_defaults(settings);
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
};
|
||||
|
||||
static obs_properties_t* _get_properties(void* ptr, void* type_data) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX %llX", __FUNCTION_NAME__, ptr, type_data);
|
||||
#endif
|
||||
obs_properties_t* props = obs_properties_create();
|
||||
if (type_data != nullptr) {
|
||||
reinterpret_cast<obsffmpeg::encoder_factory*>(type_data)->get_properties(props);
|
||||
}
|
||||
if (ptr != nullptr) {
|
||||
reinterpret_cast<obsffmpeg::encoder*>(ptr)->get_properties(props);
|
||||
}
|
||||
return props;
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
return reinterpret_cast<obs_properties_t*>(0);
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
return reinterpret_cast<obs_properties_t*>(0);
|
||||
}
|
||||
|
||||
static bool _update(void* ptr, obs_data_t* settings) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX %llX", __FUNCTION_NAME__, ptr, settings);
|
||||
#endif
|
||||
return reinterpret_cast<obsffmpeg::encoder*>(ptr)->update(settings);
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
return false;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool _get_sei_data(void* ptr, uint8_t** sei_data, size_t* size) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX %llX %llX", __FUNCTION_NAME__, ptr, sei_data, size);
|
||||
#endif
|
||||
return reinterpret_cast<obsffmpeg::encoder*>(ptr)->get_sei_data(sei_data, size);
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
return false;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool _get_extra_data(void* ptr, uint8_t** extra_data, size_t* size) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX %llX %llX", __FUNCTION_NAME__, ptr, extra_data, size);
|
||||
#endif
|
||||
return reinterpret_cast<obsffmpeg::encoder*>(ptr)->get_extra_data(extra_data, size);
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
return false;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
return false;
|
||||
}
|
||||
|
||||
static void _get_video_info(void* ptr, struct video_scale_info* info) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX %llX", __FUNCTION_NAME__, ptr, info);
|
||||
#endif
|
||||
reinterpret_cast<obsffmpeg::encoder*>(ptr)->get_video_info(info);
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
}
|
||||
|
||||
static bool _encode(void* ptr, struct encoder_frame* frame, struct encoder_packet* packet,
|
||||
bool* received_packet) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX %llX %llX %llX", __FUNCTION_NAME__, ptr, frame, packet, received_packet);
|
||||
#endif
|
||||
return reinterpret_cast<obsffmpeg::encoder*>(ptr)->video_encode(frame, packet, received_packet);
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
return false;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool _encode_texture(void* ptr, uint32_t handle, int64_t pts, uint64_t lock_key, uint64_t* next_key,
|
||||
struct encoder_packet* packet, bool* received_packet) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %lI %llI %llU %llX %llX %llX", __FUNCTION_NAME__, ptr, handle, pts, lock_key, next_key, packet,
|
||||
received_packet);
|
||||
#endif
|
||||
return reinterpret_cast<obsffmpeg::encoder*>(ptr)->video_encode_texture(handle, pts, lock_key, next_key, packet,
|
||||
received_packet);
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
return false;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
return false;
|
||||
}
|
||||
|
||||
static void _get_audio_info(void* ptr, struct audio_convert_info* info) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX %llX", __FUNCTION_NAME__, ptr, info);
|
||||
#endif
|
||||
reinterpret_cast<obsffmpeg::encoder*>(ptr)->get_audio_info(info);
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
}
|
||||
|
||||
static size_t _get_frame_size(void* ptr) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX", __FUNCTION_NAME__, ptr);
|
||||
#endif
|
||||
return reinterpret_cast<obsffmpeg::encoder*>(ptr)->get_frame_size();
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
return 0;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool _encode_audio(void* ptr, struct encoder_frame* frame, struct encoder_packet* packet,
|
||||
bool* received_packet) noexcept try {
|
||||
#ifdef DEBUG_CALL_ORDER
|
||||
PLOG_INFO("%s %llX %llX %llX %llX", __FUNCTION_NAME__, ptr, frame, packet, received_packet);
|
||||
#endif
|
||||
return reinterpret_cast<obsffmpeg::encoder*>(ptr)->audio_encode(frame, packet, received_packet);
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
return false;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
return false;
|
||||
}
|
||||
|
||||
obsffmpeg::encoder_factory::encoder_factory(const AVCodec* codec) : avcodec_ptr(codec), info(), info_fallback()
|
||||
{
|
||||
// Find Codec UI handler.
|
||||
_handler = obsffmpeg::find_codec_handler(avcodec_ptr->name);
|
||||
|
||||
// Unique Id is FFmpeg name.
|
||||
info.uid = avcodec_ptr->name;
|
||||
|
||||
@@ -71,8 +301,8 @@ obsffmpeg::encoder_factory::encoder_factory(const AVCodec* codec) : avcodec_ptr(
|
||||
info.readable_name = sstr.str();
|
||||
|
||||
// Allow UI Handler to replace visible name.
|
||||
obsffmpeg::find_codec_handler(avcodec_ptr->name)
|
||||
->override_visible_name(avcodec_ptr, info.readable_name);
|
||||
if (_handler)
|
||||
_handler->override_visible_name(avcodec_ptr, info.readable_name);
|
||||
}
|
||||
|
||||
// Assign Ids.
|
||||
@@ -96,6 +326,21 @@ obsffmpeg::encoder_factory::encoder_factory(const AVCodec* codec) : avcodec_ptr(
|
||||
info.oei.caps |= OBS_ENCODER_CAP_DEPRECATED;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Hardware encoder?
|
||||
if (ffmpeg::tools::can_hardware_encode(avcodec_ptr)) {
|
||||
info_fallback.uid = info.uid + "_sw";
|
||||
info_fallback.codec = info.codec;
|
||||
info_fallback.readable_name = info.readable_name + " (Software)";
|
||||
|
||||
// Copy capabilities and hide from view.
|
||||
info_fallback.oei.id = info_fallback.uid.c_str();
|
||||
info_fallback.oei.codec = info.oei.codec;
|
||||
info_fallback.oei.caps = info.oei.caps;
|
||||
//info_fallback.oei.caps |= OBS_ENCODER_CAP_DEPRECATED;
|
||||
|
||||
info.oei.caps |= OBS_ENCODER_CAP_PASS_TEXTURE;
|
||||
}
|
||||
}
|
||||
|
||||
obsffmpeg::encoder_factory::~encoder_factory() {}
|
||||
@@ -112,198 +357,59 @@ void obsffmpeg::encoder_factory::register_encoder()
|
||||
}
|
||||
|
||||
// Register functions.
|
||||
info.oei.create = [](obs_data_t* settings, obs_encoder_t* encoder) {
|
||||
try {
|
||||
return reinterpret_cast<void*>(new obsffmpeg::encoder(settings, encoder));
|
||||
} catch (std::exception const& e) {
|
||||
PLOG_ERROR("exception: %s", e.what());
|
||||
return reinterpret_cast<void*>(0);
|
||||
} catch (...) {
|
||||
PLOG_ERROR("unknown exception");
|
||||
return reinterpret_cast<void*>(0);
|
||||
}
|
||||
};
|
||||
info.oei.destroy = [](void* ptr) {
|
||||
try {
|
||||
delete reinterpret_cast<encoder*>(ptr);
|
||||
} catch (std::exception const& e) {
|
||||
PLOG_ERROR("exception: %s", e.what());
|
||||
throw e;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("unknown exception");
|
||||
throw;
|
||||
}
|
||||
};
|
||||
info.oei.get_name = [](void* type_data) {
|
||||
try {
|
||||
return reinterpret_cast<encoder_factory*>(type_data)->get_name();
|
||||
} catch (std::exception const& e) {
|
||||
PLOG_ERROR("exception: %s", e.what());
|
||||
throw e;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("unknown exception");
|
||||
throw;
|
||||
}
|
||||
};
|
||||
info.oei.get_defaults2 = [](obs_data_t* settings, void* type_data) {
|
||||
try {
|
||||
reinterpret_cast<encoder_factory*>(type_data)->get_defaults(settings);
|
||||
} catch (std::exception const& e) {
|
||||
PLOG_ERROR("exception: %s", e.what());
|
||||
throw e;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("unknown exception");
|
||||
throw;
|
||||
}
|
||||
};
|
||||
info.oei.get_properties2 = [](void* ptr, void* type_data) {
|
||||
try {
|
||||
obs_properties_t* props = obs_properties_create();
|
||||
if (type_data != nullptr) {
|
||||
reinterpret_cast<encoder_factory*>(type_data)->get_properties(props);
|
||||
}
|
||||
if (ptr != nullptr) {
|
||||
reinterpret_cast<encoder*>(ptr)->get_properties(props);
|
||||
}
|
||||
return props;
|
||||
} catch (std::exception const& e) {
|
||||
PLOG_ERROR("exception: %s", e.what());
|
||||
throw e;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("unknown exception");
|
||||
throw;
|
||||
}
|
||||
};
|
||||
info.oei.update = [](void* ptr, obs_data_t* settings) {
|
||||
try {
|
||||
return reinterpret_cast<encoder*>(ptr)->update(settings);
|
||||
} catch (std::exception const& e) {
|
||||
PLOG_ERROR("exception: %s", e.what());
|
||||
throw e;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("unknown exception");
|
||||
throw;
|
||||
}
|
||||
};
|
||||
info.oei.get_sei_data = [](void* ptr, uint8_t** sei_data, size_t* size) {
|
||||
try {
|
||||
return reinterpret_cast<encoder*>(ptr)->get_sei_data(sei_data, size);
|
||||
} catch (std::exception const& e) {
|
||||
PLOG_ERROR("exception: %s", e.what());
|
||||
throw e;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("unknown exception");
|
||||
throw;
|
||||
}
|
||||
};
|
||||
info.oei.get_extra_data = [](void* ptr, uint8_t** extra_data, size_t* size) {
|
||||
try {
|
||||
return reinterpret_cast<encoder*>(ptr)->get_extra_data(extra_data, size);
|
||||
} catch (std::exception const& e) {
|
||||
PLOG_ERROR("exception: %s", e.what());
|
||||
throw e;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("unknown exception");
|
||||
throw;
|
||||
}
|
||||
};
|
||||
info.oei.destroy = _destroy;
|
||||
info.oei.get_name = _get_name;
|
||||
info.oei.get_defaults2 = _get_defaults;
|
||||
info.oei.get_properties2 = _get_properties;
|
||||
info.oei.update = _update;
|
||||
info.oei.get_sei_data = _get_sei_data;
|
||||
info.oei.get_extra_data = _get_extra_data;
|
||||
|
||||
if (avcodec_ptr->type == AVMediaType::AVMEDIA_TYPE_VIDEO) {
|
||||
info.oei.get_video_info = [](void* ptr, struct video_scale_info* info) {
|
||||
try {
|
||||
reinterpret_cast<encoder*>(ptr)->get_video_info(info);
|
||||
} catch (std::exception const& e) {
|
||||
PLOG_ERROR("exception: %s", e.what());
|
||||
throw e;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("unknown exception");
|
||||
throw;
|
||||
}
|
||||
};
|
||||
info.oei.encode = [](void* ptr, struct encoder_frame* frame, struct encoder_packet* packet,
|
||||
bool* received_packet) {
|
||||
try {
|
||||
return reinterpret_cast<encoder*>(ptr)->video_encode(frame, packet, received_packet);
|
||||
} catch (std::exception const& e) {
|
||||
PLOG_ERROR("exception: %s", e.what());
|
||||
throw e;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("unknown exception");
|
||||
throw;
|
||||
}
|
||||
};
|
||||
info.oei.encode_texture = [](void* ptr, uint32_t handle, int64_t pts, uint64_t lock_key,
|
||||
uint64_t* next_key, struct encoder_packet* packet, bool* received_packet) {
|
||||
try {
|
||||
return reinterpret_cast<encoder*>(ptr)->video_encode_texture(
|
||||
handle, pts, lock_key, next_key, packet, received_packet);
|
||||
} catch (std::exception const& e) {
|
||||
PLOG_ERROR("exception: %s", e.what());
|
||||
throw e;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("unknown exception");
|
||||
throw;
|
||||
}
|
||||
};
|
||||
|
||||
info.oei.get_video_info = _get_video_info;
|
||||
} else if (avcodec_ptr->type == AVMediaType::AVMEDIA_TYPE_AUDIO) {
|
||||
info.oei.get_audio_info = [](void* ptr, struct audio_convert_info* info) {
|
||||
try {
|
||||
reinterpret_cast<encoder*>(ptr)->get_audio_info(info);
|
||||
} catch (std::exception const& e) {
|
||||
PLOG_ERROR("exception: %s", e.what());
|
||||
throw e;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("unknown exception");
|
||||
throw;
|
||||
}
|
||||
};
|
||||
info.oei.get_frame_size = [](void* ptr) {
|
||||
try {
|
||||
return reinterpret_cast<encoder*>(ptr)->get_frame_size();
|
||||
} catch (std::exception const& e) {
|
||||
PLOG_ERROR("exception: %s", e.what());
|
||||
throw e;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("unknown exception");
|
||||
throw;
|
||||
}
|
||||
};
|
||||
info.oei.encode = [](void* ptr, struct encoder_frame* frame, struct encoder_packet* packet,
|
||||
bool* received_packet) {
|
||||
try {
|
||||
return reinterpret_cast<encoder*>(ptr)->audio_encode(frame, packet, received_packet);
|
||||
} catch (std::exception const& e) {
|
||||
PLOG_ERROR("exception: %s", e.what());
|
||||
throw e;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("unknown exception");
|
||||
throw;
|
||||
}
|
||||
};
|
||||
info.oei.get_audio_info = _get_audio_info;
|
||||
info.oei.get_frame_size = _get_frame_size;
|
||||
info.oei.encode = _encode_audio;
|
||||
}
|
||||
|
||||
// Finally store ourself as type data.
|
||||
info.oei.type_data = this;
|
||||
|
||||
if (ffmpeg::tools::can_hardware_encode(avcodec_ptr)) {
|
||||
info.oei.create = _create_texture;
|
||||
info.oei.encode_texture = _encode_texture;
|
||||
|
||||
info_fallback.oei.type = info.oei.type;
|
||||
info_fallback.oei.create = _create;
|
||||
info_fallback.oei.destroy = _destroy;
|
||||
info_fallback.oei.get_name = _get_name_fallback;
|
||||
info_fallback.oei.get_defaults2 = _get_defaults;
|
||||
info_fallback.oei.get_properties2 = _get_properties;
|
||||
info_fallback.oei.update = _update;
|
||||
info_fallback.oei.get_sei_data = _get_sei_data;
|
||||
info_fallback.oei.get_extra_data = _get_extra_data;
|
||||
info_fallback.oei.get_video_info = _get_video_info;
|
||||
info_fallback.oei.encode = _encode;
|
||||
info_fallback.oei.type_data = this;
|
||||
|
||||
obs_register_encoder(&info_fallback.oei);
|
||||
PLOG_DEBUG("Registered software fallback for encoder #%llX", avcodec_ptr);
|
||||
} else {
|
||||
// Is not a GPU Encoder, don't implement fallback.
|
||||
info.oei.create = _create;
|
||||
info.oei.encode = _encode;
|
||||
}
|
||||
|
||||
obs_register_encoder(&info.oei);
|
||||
PLOG_DEBUG("Registered encoder #%llX with name '%s' and long name '%s' and caps %llX", avcodec_ptr,
|
||||
avcodec_ptr->name, avcodec_ptr->long_name, avcodec_ptr->capabilities);
|
||||
}
|
||||
|
||||
const char* obsffmpeg::encoder_factory::get_name()
|
||||
{
|
||||
return info.readable_name.c_str();
|
||||
}
|
||||
|
||||
void obsffmpeg::encoder_factory::get_defaults(obs_data_t* settings)
|
||||
{
|
||||
{ // Handler
|
||||
auto ptr = obsffmpeg::find_codec_handler(avcodec_ptr->name);
|
||||
if (ptr) {
|
||||
ptr->get_defaults(settings, avcodec_ptr, nullptr);
|
||||
}
|
||||
}
|
||||
if (_handler)
|
||||
_handler->get_defaults(settings, avcodec_ptr, nullptr);
|
||||
|
||||
if ((avcodec_ptr->capabilities & AV_CODEC_CAP_INTRA_ONLY) == 0) {
|
||||
obs_data_set_default_int(settings, S_KEYFRAMES_INTERVALTYPE, 0);
|
||||
@@ -320,22 +426,23 @@ void obsffmpeg::encoder_factory::get_defaults(obs_data_t* settings)
|
||||
}
|
||||
}
|
||||
|
||||
static bool modified_keyframes(obs_properties_t* props, obs_property_t*, obs_data_t* settings)
|
||||
{
|
||||
static bool modified_keyframes(obs_properties_t* props, obs_property_t*, obs_data_t* settings) try {
|
||||
bool is_seconds = obs_data_get_int(settings, S_KEYFRAMES_INTERVALTYPE) == 0;
|
||||
obs_property_set_visible(obs_properties_get(props, S_KEYFRAMES_INTERVAL_FRAMES), !is_seconds);
|
||||
obs_property_set_visible(obs_properties_get(props, S_KEYFRAMES_INTERVAL_SECONDS), is_seconds);
|
||||
return true;
|
||||
} catch (const std::exception& ex) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
|
||||
return false;
|
||||
} catch (...) {
|
||||
PLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
||||
return false;
|
||||
}
|
||||
|
||||
void obsffmpeg::encoder_factory::get_properties(obs_properties_t* props)
|
||||
{
|
||||
{ // Handler
|
||||
auto ptr = obsffmpeg::find_codec_handler(avcodec_ptr->name);
|
||||
if (ptr) {
|
||||
ptr->get_properties(props, avcodec_ptr, nullptr);
|
||||
}
|
||||
}
|
||||
if (_handler)
|
||||
_handler->get_properties(props, avcodec_ptr, nullptr);
|
||||
|
||||
if ((avcodec_ptr->capabilities & AV_CODEC_CAP_INTRA_ONLY) == 0) {
|
||||
// Key-Frame Options
|
||||
@@ -424,9 +531,23 @@ const AVCodec* obsffmpeg::encoder_factory::get_avcodec()
|
||||
return avcodec_ptr;
|
||||
}
|
||||
|
||||
obsffmpeg::encoder::encoder(obs_data_t* settings, obs_encoder_t* encoder)
|
||||
: _self(encoder), _lag_in_frames(0), _count_send_frames(0), _have_first_frame(false), _initialized(false)
|
||||
const obsffmpeg::encoder_info& obsffmpeg::encoder_factory::get_info()
|
||||
{
|
||||
return info;
|
||||
}
|
||||
|
||||
const obsffmpeg::encoder_info& obsffmpeg::encoder_factory::get_fallback()
|
||||
{
|
||||
return info_fallback;
|
||||
}
|
||||
|
||||
obsffmpeg::encoder::encoder(obs_data_t* settings, obs_encoder_t* encoder, bool is_texture_encode)
|
||||
: _self(encoder), _lag_in_frames(0), _count_send_frames(0), _have_first_frame(false)
|
||||
{
|
||||
if (is_texture_encode) {
|
||||
throw obsffmpeg::unsupported_gpu_exception("not implemented yet");
|
||||
}
|
||||
|
||||
_factory = reinterpret_cast<encoder_factory*>(obs_encoder_get_type_data(_self));
|
||||
|
||||
// Verify that the codec actually still exists.
|
||||
@@ -436,6 +557,9 @@ obsffmpeg::encoder::encoder(obs_data_t* settings, obs_encoder_t* encoder)
|
||||
throw std::runtime_error("failed to find codec");
|
||||
}
|
||||
|
||||
// Find Codec UI handler.
|
||||
_handler = obsffmpeg::find_codec_handler(_codec->name);
|
||||
|
||||
// Initialize context.
|
||||
_context = avcodec_alloc_context3(_codec);
|
||||
if (!_context) {
|
||||
@@ -466,11 +590,110 @@ obsffmpeg::encoder::encoder(obs_data_t* settings, obs_encoder_t* encoder)
|
||||
}
|
||||
}
|
||||
|
||||
// Create 8MB of precached Packet data for use later on.
|
||||
av_init_packet(&_current_packet);
|
||||
av_new_packet(&_current_packet, 8 * 1024 * 1024); // 8 MB precached Packet size.
|
||||
|
||||
if (_codec->type == AVMEDIA_TYPE_VIDEO) {
|
||||
// Initialize Video Encoding
|
||||
auto voi = video_output_get_info(obs_encoder_video(_self));
|
||||
|
||||
// Find a suitable Pixel Format.
|
||||
AVPixelFormat _pixfmt_source = ffmpeg::tools::obs_videoformat_to_avpixelformat(voi->format);
|
||||
AVPixelFormat _pixfmt_target =
|
||||
static_cast<AVPixelFormat>(obs_data_get_int(settings, ST_FFMPEG_COLORFORMAT));
|
||||
if (_pixfmt_target == AV_PIX_FMT_NONE) {
|
||||
// Find the best conversion format.
|
||||
std::vector<AVPixelFormat> fmts = ffmpeg::tools::get_software_formats(_codec->pix_fmts);
|
||||
_pixfmt_target = ffmpeg::tools::get_best_compatible_format(fmts.data(), _pixfmt_source);
|
||||
|
||||
if (_handler) // Allow Handler to override the automatic color format for sanity reasons.
|
||||
_handler->override_colorformat(_pixfmt_target, settings, _codec, _context);
|
||||
} else {
|
||||
// Use user override, guaranteed to be supported.
|
||||
bool is_format_supported = false;
|
||||
for (auto ptr = _codec->pix_fmts; *ptr != AV_PIX_FMT_NONE; ptr++) {
|
||||
if (*ptr == _pixfmt_target) {
|
||||
is_format_supported = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_format_supported) {
|
||||
std::stringstream sstr;
|
||||
sstr << "Color Format '" << ffmpeg::tools::get_pixel_format_name(_pixfmt_target)
|
||||
<< "' is not supported by the encoder.";
|
||||
throw std::exception(sstr.str().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
_context->width = voi->width;
|
||||
_context->height = voi->height;
|
||||
ffmpeg::tools::setup_obs_color(voi->colorspace, voi->range, _context);
|
||||
|
||||
_context->pix_fmt = _pixfmt_target;
|
||||
_context->field_order = AV_FIELD_PROGRESSIVE;
|
||||
_context->ticks_per_frame = 1;
|
||||
_context->sample_aspect_ratio.num = _context->sample_aspect_ratio.den = 1;
|
||||
_context->framerate.num = _context->time_base.den = voi->fps_num;
|
||||
_context->framerate.den = _context->time_base.num = voi->fps_den;
|
||||
|
||||
_swscale.set_source_size(_context->width, _context->height);
|
||||
_swscale.set_source_color(_context->color_range == AVCOL_RANGE_JPEG, _context->colorspace);
|
||||
_swscale.set_source_format(_pixfmt_source);
|
||||
|
||||
_swscale.set_target_size(_context->width, _context->height);
|
||||
_swscale.set_target_color(_context->color_range == AVCOL_RANGE_JPEG, _context->colorspace);
|
||||
_swscale.set_target_format(_pixfmt_target);
|
||||
|
||||
// Create Scaler
|
||||
if (!_swscale.initialize(SWS_POINT)) {
|
||||
std::stringstream sstr;
|
||||
sstr << "Initializing scaler failed for conversion from '"
|
||||
<< ffmpeg::tools::get_pixel_format_name(_swscale.get_source_format()) << "' to '"
|
||||
<< ffmpeg::tools::get_pixel_format_name(_swscale.get_target_format())
|
||||
<< "' with color space '"
|
||||
<< ffmpeg::tools::get_color_space_name(_swscale.get_source_colorspace()) << "' and "
|
||||
<< (_swscale.is_source_full_range() ? "full" : "partial") << " range.";
|
||||
throw std::runtime_error(sstr.str());
|
||||
}
|
||||
}
|
||||
|
||||
{ // Log Encoder info
|
||||
PLOG_INFO("[%s] Initializing...", _codec->name);
|
||||
PLOG_INFO("[%s] Video Input: %ldx%ld %s %s %s", _codec->name, _swscale.get_source_width(),
|
||||
_swscale.get_source_height(),
|
||||
ffmpeg::tools::get_pixel_format_name(_swscale.get_source_format()),
|
||||
ffmpeg::tools::get_color_space_name(_swscale.get_source_colorspace()),
|
||||
_swscale.is_source_full_range() ? "Full" : "Partial");
|
||||
PLOG_INFO("[%s] Video Output: %ldx%ld %s %s %s", _codec->name, _swscale.get_target_width(),
|
||||
_swscale.get_target_height(),
|
||||
ffmpeg::tools::get_pixel_format_name(_swscale.get_target_format()),
|
||||
ffmpeg::tools::get_color_space_name(_swscale.get_target_colorspace()),
|
||||
_swscale.is_target_full_range() ? "Full" : "Partial");
|
||||
PLOG_INFO("[%s] Framerate: %ld/%ld (%f FPS)", _codec->name, _context->time_base.den,
|
||||
_context->time_base.num,
|
||||
static_cast<double_t>(_context->time_base.den)
|
||||
/ static_cast<double_t>(_context->time_base.num));
|
||||
PLOG_INFO("[%s] Custom Settings: %s", _codec->name,
|
||||
obs_data_get_string(settings, ST_FFMPEG_CUSTOMSETTINGS));
|
||||
}
|
||||
|
||||
// Update settings
|
||||
update(settings);
|
||||
|
||||
av_init_packet(&_current_packet);
|
||||
av_new_packet(&_current_packet, 8 * 1024 * 1024); // 8 MB precached Packet size.
|
||||
// Initialize Encoder
|
||||
int res = avcodec_open2(_context, _codec, NULL);
|
||||
if (res < 0) {
|
||||
std::stringstream sstr;
|
||||
sstr << "Initializing encoder '" << _codec->name
|
||||
<< "' failed with error: " << ffmpeg::tools::get_error_description(res) << " (code " << res << ")";
|
||||
throw std::runtime_error(sstr.str());
|
||||
}
|
||||
|
||||
// Create Frame queue
|
||||
_frame_queue.set_pixel_format(_context->pix_fmt);
|
||||
_frame_queue.set_resolution(_context->width, _context->height);
|
||||
_frame_queue.precache(2);
|
||||
}
|
||||
|
||||
obsffmpeg::encoder::~encoder()
|
||||
@@ -497,93 +720,10 @@ obsffmpeg::encoder::~encoder()
|
||||
_swscale.finalize();
|
||||
}
|
||||
|
||||
bool obsffmpeg::encoder::initialize()
|
||||
{
|
||||
if (_initialized)
|
||||
return true;
|
||||
|
||||
// Detect supported
|
||||
bool is_format_supported = false;
|
||||
for (auto ptr = _codec->pix_fmts; *ptr != AV_PIX_FMT_NONE; ptr++) {
|
||||
if (*ptr == _format) {
|
||||
is_format_supported = true;
|
||||
}
|
||||
}
|
||||
if (!is_format_supported) {
|
||||
std::stringstream sstr;
|
||||
sstr << "The color format " << ffmpeg::tools::get_pixel_format_name(_format)
|
||||
<< " is not supported by the encoder.";
|
||||
throw std::exception(sstr.str().c_str());
|
||||
}
|
||||
|
||||
if (_codec->type == AVMEDIA_TYPE_VIDEO) {
|
||||
auto encvideo = obs_encoder_video(_self);
|
||||
auto voi = video_output_get_info(encvideo);
|
||||
|
||||
// Set Resolution
|
||||
_context->width = static_cast<int>(_resolution.first);
|
||||
_context->height = static_cast<int>(_resolution.second);
|
||||
|
||||
// Set Color Space, Format and Range
|
||||
_context->colorspace = ffmpeg::tools::obs_videocolorspace_to_avcolorspace(voi->colorspace);
|
||||
_context->color_range = ffmpeg::tools::obs_videorangetype_to_avcolorrange(voi->range);
|
||||
_context->pix_fmt = _format;
|
||||
|
||||
// Set Framerate and Field Order
|
||||
_context->field_order = AV_FIELD_PROGRESSIVE;
|
||||
_context->time_base.num = voi->fps_den;
|
||||
_context->time_base.den = voi->fps_num;
|
||||
_context->ticks_per_frame = 1;
|
||||
}
|
||||
|
||||
// Initialize
|
||||
int res = avcodec_open2(_context, _codec, NULL);
|
||||
if (res < 0) {
|
||||
std::stringstream sstr;
|
||||
sstr << "Failed to initalized encoder '" << _codec->name
|
||||
<< "' due to error: " << ffmpeg::tools::get_error_description(res) << "(" << res
|
||||
<< ")";
|
||||
throw std::runtime_error(sstr.str().c_str());
|
||||
}
|
||||
|
||||
// Initialize Scaler and Frame Queue
|
||||
if (_codec->type == AVMEDIA_TYPE_VIDEO) {
|
||||
_swscale.set_source_format(_format);
|
||||
_swscale.set_target_format(_context->pix_fmt);
|
||||
_swscale.set_target_size(_context->width, _context->height);
|
||||
_swscale.set_source_size(_context->width, _context->height);
|
||||
_swscale.set_target_size(_context->width, _context->height);
|
||||
_swscale.set_source_color(_context->color_range, _context->colorspace);
|
||||
_swscale.set_target_color(_context->color_range, _context->colorspace);
|
||||
|
||||
// Create Scaler
|
||||
if (!_swscale.initialize(SWS_FAST_BILINEAR)) {
|
||||
std::stringstream sstr;
|
||||
sstr << "Failed to initialize frame scaler for pixel format '"
|
||||
<< ffmpeg::tools::get_pixel_format_name(_context->pix_fmt) << "' with color space '"
|
||||
<< ffmpeg::tools::get_color_space_name(_context->colorspace) << "' and "
|
||||
<< (_swscale.is_source_full_range() ? "full" : "partial") << " color range.";
|
||||
throw std::runtime_error(sstr.str().c_str());
|
||||
}
|
||||
|
||||
// Create Frame queue
|
||||
_frame_queue.set_pixel_format(_context->pix_fmt);
|
||||
_frame_queue.set_resolution(_context->width, _context->height);
|
||||
_frame_queue.precache(2);
|
||||
}
|
||||
|
||||
_initialized = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void obsffmpeg::encoder::get_properties(obs_properties_t* props)
|
||||
{
|
||||
{ // Handler
|
||||
auto ptr = obsffmpeg::find_codec_handler(_codec->name);
|
||||
if (ptr) {
|
||||
ptr->get_properties(props, _codec, _context);
|
||||
}
|
||||
}
|
||||
if (_handler)
|
||||
_handler->get_properties(props, _codec, _context);
|
||||
|
||||
obs_property_set_enabled(obs_properties_get(props, S_KEYFRAMES), false);
|
||||
obs_property_set_enabled(obs_properties_get(props, S_KEYFRAMES_INTERVALTYPE), false);
|
||||
@@ -597,12 +737,8 @@ void obsffmpeg::encoder::get_properties(obs_properties_t* props)
|
||||
|
||||
bool obsffmpeg::encoder::update(obs_data_t* settings)
|
||||
{
|
||||
{ // Handler
|
||||
auto ptr = obsffmpeg::find_codec_handler(_codec->name);
|
||||
if (ptr) {
|
||||
ptr->update(settings, _codec, _context);
|
||||
}
|
||||
}
|
||||
if (_handler)
|
||||
_handler->update(settings, _codec, _context);
|
||||
|
||||
if ((_codec->capabilities & AV_CODEC_CAP_INTRA_ONLY) == 0) {
|
||||
// Key-Frame Options
|
||||
@@ -628,7 +764,12 @@ bool obsffmpeg::encoder::update(obs_data_t* settings)
|
||||
av_opt_set_from_string(_context->priv_data, obs_data_get_string(settings, ST_FFMPEG_CUSTOMSETTINGS),
|
||||
nullptr, "=", ";");
|
||||
}
|
||||
return false;
|
||||
|
||||
// Handler Logging
|
||||
if (_handler)
|
||||
_handler->log_options(settings, _codec, _context);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void obsffmpeg::encoder::get_audio_info(audio_convert_info*) {}
|
||||
@@ -645,18 +786,9 @@ bool obsffmpeg::encoder::audio_encode(encoder_frame*, encoder_packet*, bool*)
|
||||
|
||||
void obsffmpeg::encoder::get_video_info(video_scale_info* vsi)
|
||||
{
|
||||
obs_data_t* settings = obs_encoder_get_settings(_self);
|
||||
|
||||
AVPixelFormat avformat = static_cast<AVPixelFormat>(obs_data_get_int(settings, ST_FFMPEG_COLORFORMAT));
|
||||
video_format obsformat = ffmpeg::tools::avpixelformat_to_obs_videoformat(avformat);
|
||||
|
||||
if (obsformat != VIDEO_FORMAT_NONE) {
|
||||
vsi->format = obsformat;
|
||||
}
|
||||
|
||||
_resolution.first = vsi->width;
|
||||
_resolution.second = vsi->height;
|
||||
_format = ffmpeg::tools::obs_videoformat_to_avpixelformat(vsi->format);
|
||||
vsi->width = _swscale.get_source_width();
|
||||
vsi->height = _swscale.get_source_height();
|
||||
vsi->format = ffmpeg::tools::avpixelformat_to_obs_videoformat(_swscale.get_source_format());
|
||||
}
|
||||
|
||||
bool obsffmpeg::encoder::get_sei_data(uint8_t** data, size_t* size)
|
||||
@@ -711,15 +843,6 @@ static inline void copy_data(encoder_frame* frame, AVFrame* vframe)
|
||||
|
||||
bool obsffmpeg::encoder::video_encode(encoder_frame* frame, encoder_packet* packet, bool* received_packet)
|
||||
{
|
||||
// Ensure that the encoder was initialized.
|
||||
try {
|
||||
if (!initialize())
|
||||
return false;
|
||||
} catch (std::exception& ex) {
|
||||
PLOG_ERROR("%s", ex.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Convert frame.
|
||||
std::shared_ptr<AVFrame> vframe = _frame_queue.pop(); // Retrieve an empty frame.
|
||||
{
|
||||
@@ -731,6 +854,8 @@ bool obsffmpeg::encoder::video_encode(encoder_frame* frame, encoder_packet* pack
|
||||
vframe->format = _context->pix_fmt;
|
||||
vframe->color_range = _context->color_range;
|
||||
vframe->colorspace = _context->colorspace;
|
||||
vframe->color_primaries = _context->color_primaries;
|
||||
vframe->color_trc = _context->color_trc;
|
||||
vframe->pts = frame->pts;
|
||||
|
||||
if ((_swscale.is_source_full_range() == _swscale.is_target_full_range())
|
||||
@@ -846,6 +971,8 @@ bool obsffmpeg::encoder::video_encode_texture(uint32_t, int64_t, uint64_t, uint6
|
||||
|
||||
int obsffmpeg::encoder::receive_packet(bool* received_packet, struct encoder_packet* packet)
|
||||
{
|
||||
av_packet_unref(&_current_packet);
|
||||
|
||||
int res = avcodec_receive_packet(_context, &_current_packet);
|
||||
if (res == 0) {
|
||||
if (!_have_first_frame) {
|
||||
@@ -868,8 +995,9 @@ int obsffmpeg::encoder::receive_packet(bool* received_packet, struct encoder_pac
|
||||
std::memcpy(_sei_data.data(), tmp_sei, sz_sei);
|
||||
}
|
||||
|
||||
std::memcpy(_current_packet.data, tmp_packet, sz_packet);
|
||||
_current_packet.size = static_cast<int>(sz_packet);
|
||||
// Not required, we only need the Extra Data and SEI Data anyway.
|
||||
//std::memcpy(_current_packet.data, tmp_packet, sz_packet);
|
||||
//_current_packet.size = static_cast<int>(sz_packet);
|
||||
|
||||
bfree(tmp_packet);
|
||||
bfree(tmp_header);
|
||||
@@ -877,10 +1005,17 @@ int obsffmpeg::encoder::receive_packet(bool* received_packet, struct encoder_pac
|
||||
} else if (_codec->id == AV_CODEC_ID_HEVC) {
|
||||
obsffmpeg::codecs::hevc::extract_header_sei(_current_packet.data, _current_packet.size,
|
||||
_extra_data, _sei_data);
|
||||
} else if (_context->extradata != nullptr) {
|
||||
_extra_data.resize(_context->extradata_size);
|
||||
std::memcpy(_extra_data.data(), _context->extradata, _context->extradata_size);
|
||||
}
|
||||
_have_first_frame = true;
|
||||
}
|
||||
|
||||
// Allow Handler Post-Processing
|
||||
if (_handler)
|
||||
_handler->process_avpacket(_current_packet, _codec, _context);
|
||||
|
||||
packet->type = OBS_ENCODER_VIDEO;
|
||||
packet->pts = _current_packet.pts;
|
||||
packet->dts = _current_packet.dts;
|
||||
|
||||
+21
-11
@@ -24,8 +24,10 @@
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include "ffmpeg/avframe-queue.hpp"
|
||||
#include "ffmpeg/swscale.hpp"
|
||||
#include "ui/handler.hpp"
|
||||
|
||||
extern "C" {
|
||||
#include <obs-properties.h>
|
||||
@@ -38,28 +40,40 @@ extern "C" {
|
||||
}
|
||||
|
||||
namespace obsffmpeg {
|
||||
class encoder_factory {
|
||||
struct info {
|
||||
class unsupported_gpu_exception : public std::runtime_error {
|
||||
public:
|
||||
unsupported_gpu_exception(const std::string& reason) : runtime_error(reason) {}
|
||||
};
|
||||
|
||||
struct encoder_info {
|
||||
std::string uid;
|
||||
std::string codec;
|
||||
std::string readable_name;
|
||||
obs_encoder_info oei;
|
||||
} info;
|
||||
};
|
||||
|
||||
class encoder_factory {
|
||||
encoder_info info;
|
||||
encoder_info info_fallback;
|
||||
const AVCodec* avcodec_ptr;
|
||||
|
||||
std::shared_ptr<obsffmpeg::ui::handler> _handler;
|
||||
|
||||
public:
|
||||
encoder_factory(const AVCodec* codec);
|
||||
virtual ~encoder_factory();
|
||||
|
||||
void register_encoder();
|
||||
|
||||
const char* get_name();
|
||||
|
||||
void get_defaults(obs_data_t* settings);
|
||||
|
||||
void get_properties(obs_properties_t* props);
|
||||
|
||||
const AVCodec* get_avcodec();
|
||||
|
||||
const encoder_info& get_info();
|
||||
|
||||
const encoder_info& get_fallback();
|
||||
};
|
||||
|
||||
class encoder {
|
||||
@@ -69,9 +83,7 @@ namespace obsffmpeg {
|
||||
const AVCodec* _codec;
|
||||
AVCodecContext* _context;
|
||||
|
||||
bool _initialized;
|
||||
std::pair<size_t, size_t> _resolution;
|
||||
AVPixelFormat _format;
|
||||
std::shared_ptr<obsffmpeg::ui::handler> _handler;
|
||||
|
||||
ffmpeg::avframe_queue _frame_queue;
|
||||
ffmpeg::avframe_queue _frame_queue_used;
|
||||
@@ -87,11 +99,9 @@ namespace obsffmpeg {
|
||||
std::vector<uint8_t> _sei_data;
|
||||
|
||||
public:
|
||||
encoder(obs_data_t* settings, obs_encoder_t* encoder);
|
||||
encoder(obs_data_t* settings, obs_encoder_t* encoder, bool is_texture_encode = false);
|
||||
virtual ~encoder();
|
||||
|
||||
bool initialize();
|
||||
|
||||
public: // OBS API
|
||||
// Shared
|
||||
void get_properties(obs_properties_t* props);
|
||||
|
||||
+220
-44
@@ -20,6 +20,8 @@
|
||||
// SOFTWARE.
|
||||
|
||||
#include "tools.hpp"
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
@@ -159,59 +161,49 @@ const char* ffmpeg::tools::get_error_description(int error)
|
||||
return "Not Translated Yet";
|
||||
}
|
||||
|
||||
static std::map<video_format, AVPixelFormat> obs_to_av_format_map = {
|
||||
{VIDEO_FORMAT_I420, AV_PIX_FMT_YUV420P}, // YUV 4:2:0
|
||||
{VIDEO_FORMAT_NV12, AV_PIX_FMT_NV12}, // NV12 Packed YUV
|
||||
{VIDEO_FORMAT_YVYU, AV_PIX_FMT_YVYU422}, // YVYU Packed YUV
|
||||
{VIDEO_FORMAT_YUY2, AV_PIX_FMT_YUYV422}, // YUYV Packed YUV
|
||||
{VIDEO_FORMAT_UYVY, AV_PIX_FMT_UYVY422}, // UYVY Packed YUV
|
||||
{VIDEO_FORMAT_RGBA, AV_PIX_FMT_RGBA}, //
|
||||
{VIDEO_FORMAT_BGRA, AV_PIX_FMT_BGRA}, //
|
||||
{VIDEO_FORMAT_BGRX, AV_PIX_FMT_BGR0}, //
|
||||
{VIDEO_FORMAT_Y800, AV_PIX_FMT_GRAY8}, //
|
||||
{VIDEO_FORMAT_I444, AV_PIX_FMT_YUV444P}, //
|
||||
{VIDEO_FORMAT_BGR3, AV_PIX_FMT_BGR24}, //
|
||||
{VIDEO_FORMAT_I422, AV_PIX_FMT_YUV422P}, //
|
||||
{VIDEO_FORMAT_I40A, AV_PIX_FMT_YUVA420P}, //
|
||||
{VIDEO_FORMAT_I42A, AV_PIX_FMT_YUVA422P}, //
|
||||
{VIDEO_FORMAT_YUVA, AV_PIX_FMT_YUVA444P}, //
|
||||
//{VIDEO_FORMAT_AYUV, AV_PIX_FMT_AYUV444P}, //
|
||||
};
|
||||
|
||||
AVPixelFormat ffmpeg::tools::obs_videoformat_to_avpixelformat(video_format v)
|
||||
{
|
||||
switch (v) {
|
||||
// 32-Bits
|
||||
case VIDEO_FORMAT_BGRX:
|
||||
return AV_PIX_FMT_BGR0;
|
||||
case VIDEO_FORMAT_BGRA:
|
||||
return AV_PIX_FMT_BGRA;
|
||||
case VIDEO_FORMAT_RGBA:
|
||||
return AV_PIX_FMT_RGBA;
|
||||
case VIDEO_FORMAT_I444:
|
||||
return AV_PIX_FMT_YUV444P;
|
||||
case VIDEO_FORMAT_YUY2:
|
||||
return AV_PIX_FMT_YUYV422;
|
||||
case VIDEO_FORMAT_YVYU:
|
||||
return AV_PIX_FMT_YVYU422;
|
||||
case VIDEO_FORMAT_UYVY:
|
||||
return AV_PIX_FMT_UYVY422;
|
||||
case VIDEO_FORMAT_I420:
|
||||
return AV_PIX_FMT_YUV420P;
|
||||
case VIDEO_FORMAT_NV12:
|
||||
return AV_PIX_FMT_NV12;
|
||||
auto found = obs_to_av_format_map.find(v);
|
||||
if (found != obs_to_av_format_map.end()) {
|
||||
return found->second;
|
||||
}
|
||||
throw std::invalid_argument("unknown format");
|
||||
return AV_PIX_FMT_NONE;
|
||||
}
|
||||
|
||||
video_format ffmpeg::tools::avpixelformat_to_obs_videoformat(AVPixelFormat v)
|
||||
{
|
||||
switch (v) {
|
||||
case AV_PIX_FMT_YUV420P:
|
||||
return VIDEO_FORMAT_I420;
|
||||
case AV_PIX_FMT_NV12:
|
||||
return VIDEO_FORMAT_NV12;
|
||||
case AV_PIX_FMT_YVYU422:
|
||||
return VIDEO_FORMAT_YVYU;
|
||||
case AV_PIX_FMT_YUYV422:
|
||||
return VIDEO_FORMAT_YUY2;
|
||||
case AV_PIX_FMT_UYVY422:
|
||||
return VIDEO_FORMAT_UYVY;
|
||||
case AV_PIX_FMT_RGBA:
|
||||
return VIDEO_FORMAT_RGBA;
|
||||
case AV_PIX_FMT_BGRA:
|
||||
return VIDEO_FORMAT_BGRA;
|
||||
case AV_PIX_FMT_BGR0:
|
||||
return VIDEO_FORMAT_BGRX;
|
||||
case AV_PIX_FMT_GRAY8:
|
||||
return VIDEO_FORMAT_Y800;
|
||||
case AV_PIX_FMT_YUV444P:
|
||||
return VIDEO_FORMAT_I444;
|
||||
for (const auto& kv : obs_to_av_format_map) {
|
||||
if (kv.second == v)
|
||||
return kv.first;
|
||||
}
|
||||
return VIDEO_FORMAT_NONE;
|
||||
}
|
||||
|
||||
AVPixelFormat ffmpeg::tools::get_least_lossy_format(const AVPixelFormat* haystack, AVPixelFormat needle)
|
||||
{
|
||||
int data_loss = 0;
|
||||
return avcodec_find_best_pix_fmt_of_list(haystack, needle, 0, &data_loss);
|
||||
}
|
||||
|
||||
AVColorSpace ffmpeg::tools::obs_videocolorspace_to_avcolorspace(video_colorspace v)
|
||||
{
|
||||
switch (v) {
|
||||
@@ -228,10 +220,194 @@ AVColorRange ffmpeg::tools::obs_videorangetype_to_avcolorrange(video_range_type
|
||||
{
|
||||
switch (v) {
|
||||
case VIDEO_RANGE_DEFAULT:
|
||||
case VIDEO_RANGE_FULL:
|
||||
return AVCOL_RANGE_JPEG;
|
||||
case VIDEO_RANGE_PARTIAL:
|
||||
return AVCOL_RANGE_MPEG;
|
||||
case VIDEO_RANGE_FULL:
|
||||
return AVCOL_RANGE_JPEG;
|
||||
}
|
||||
throw std::invalid_argument("unknown range");
|
||||
}
|
||||
|
||||
bool ffmpeg::tools::can_hardware_encode(const AVCodec* codec)
|
||||
{
|
||||
AVPixelFormat hardware_formats[] = {AV_PIX_FMT_D3D11};
|
||||
|
||||
for (const AVPixelFormat* fmt = codec->pix_fmts; (fmt != nullptr) && (*fmt != AV_PIX_FMT_NONE); fmt++) {
|
||||
for (auto cmp : hardware_formats) {
|
||||
if (*fmt == cmp) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<AVPixelFormat> ffmpeg::tools::get_software_formats(const AVPixelFormat* list)
|
||||
{
|
||||
AVPixelFormat hardware_formats[] = {
|
||||
#if FF_API_VAAPI
|
||||
AV_PIX_FMT_VAAPI_MOCO,
|
||||
AV_PIX_FMT_VAAPI_IDCT,
|
||||
#endif
|
||||
AV_PIX_FMT_VAAPI,
|
||||
AV_PIX_FMT_DXVA2_VLD,
|
||||
AV_PIX_FMT_VDPAU,
|
||||
AV_PIX_FMT_QSV,
|
||||
AV_PIX_FMT_MMAL,
|
||||
AV_PIX_FMT_D3D11VA_VLD,
|
||||
AV_PIX_FMT_CUDA,
|
||||
AV_PIX_FMT_XVMC,
|
||||
AV_PIX_FMT_VIDEOTOOLBOX,
|
||||
AV_PIX_FMT_MEDIACODEC,
|
||||
AV_PIX_FMT_D3D11,
|
||||
AV_PIX_FMT_OPENCL,
|
||||
};
|
||||
|
||||
std::vector<AVPixelFormat> fmts;
|
||||
for (auto fmt = list; fmt && (*fmt != AV_PIX_FMT_NONE); fmt++) {
|
||||
bool is_blacklisted = false;
|
||||
for (auto blacklisted : hardware_formats) {
|
||||
if (*fmt == blacklisted)
|
||||
is_blacklisted = true;
|
||||
}
|
||||
if (!is_blacklisted)
|
||||
fmts.push_back(*fmt);
|
||||
}
|
||||
|
||||
fmts.push_back(AV_PIX_FMT_NONE);
|
||||
|
||||
return std::move(fmts);
|
||||
}
|
||||
|
||||
static std::map<std::pair<AVPixelFormat, AVPixelFormat>, double_t> format_compatibility = {
|
||||
{{AV_PIX_FMT_NV12, AV_PIX_FMT_NV12}, std::numeric_limits<double_t>::max()},
|
||||
{{AV_PIX_FMT_NV12, AV_PIX_FMT_NV21}, 65535.0},
|
||||
|
||||
{{AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P}, std::numeric_limits<double_t>::max()},
|
||||
{{AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P}, 65535.0},
|
||||
{{AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9}, 58981.5},
|
||||
{{AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10}, 53083.35},
|
||||
{{AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P12}, 47775.015},
|
||||
{{AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P14}, 42997.5135},
|
||||
{{AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P16}, 38697.76215},
|
||||
|
||||
{{AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P}, std::numeric_limits<double_t>::max()},
|
||||
{{AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9}, 65535.0},
|
||||
{{AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P10}, 58981.5},
|
||||
{{AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P16}, 53083.35},
|
||||
{{AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P}, 32767.0},
|
||||
|
||||
{{AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P}, std::numeric_limits<double_t>::max()},
|
||||
{{AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P}, 65535.0},
|
||||
{{AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9}, 58981.5},
|
||||
{{AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10}, 53083.35},
|
||||
{{AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P12}, 47775.015},
|
||||
{{AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P14}, 42997.5135},
|
||||
{{AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P16}, 38697.76215},
|
||||
|
||||
{{AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P}, std::numeric_limits<double_t>::max()},
|
||||
{{AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P9}, 65535.0},
|
||||
{{AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P10}, 58981.5},
|
||||
{{AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P16}, 53083.35},
|
||||
{{AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P}, 32767.0},
|
||||
|
||||
{{AV_PIX_FMT_YVYU422, AV_PIX_FMT_YVYU422}, std::numeric_limits<double_t>::max()},
|
||||
{{AV_PIX_FMT_YVYU422, AV_PIX_FMT_YUYV422}, 65535.0},
|
||||
|
||||
{{AV_PIX_FMT_UYVY422, AV_PIX_FMT_UYVY422}, std::numeric_limits<double_t>::max()},
|
||||
{{AV_PIX_FMT_UYVY422, AV_PIX_FMT_YVYU422}, 65535.0},
|
||||
|
||||
{{AV_PIX_FMT_YUYV422, AV_PIX_FMT_YUYV422}, std::numeric_limits<double_t>::max()},
|
||||
|
||||
{{AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P}, std::numeric_limits<double_t>::max()},
|
||||
{{AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P}, 65535.0},
|
||||
{{AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9}, 58981.5},
|
||||
{{AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10}, 53083.35},
|
||||
{{AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P12}, 47775.015},
|
||||
{{AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P14}, 42997.5135},
|
||||
{{AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P16}, 38697.76215},
|
||||
|
||||
{{AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P}, std::numeric_limits<double_t>::max()},
|
||||
{{AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9}, 65535.0},
|
||||
{{AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P10}, 58981.5},
|
||||
{{AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P16}, 53083.35},
|
||||
{{AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P}, 32767.0},
|
||||
|
||||
{{AV_PIX_FMT_RGBA, AV_PIX_FMT_RGBA}, std::numeric_limits<double_t>::max()},
|
||||
{{AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB0}, 65535.0},
|
||||
{{AV_PIX_FMT_RGBA, AV_PIX_FMT_0RGB}, 32767.0},
|
||||
{{AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB24}, 16384.0},
|
||||
|
||||
{{AV_PIX_FMT_BGRA, AV_PIX_FMT_BGRA}, std::numeric_limits<double_t>::max()},
|
||||
{{AV_PIX_FMT_BGRA, AV_PIX_FMT_BGR0}, 65535.0},
|
||||
{{AV_PIX_FMT_BGRA, AV_PIX_FMT_0BGR}, 32767.0},
|
||||
{{AV_PIX_FMT_BGRA, AV_PIX_FMT_BGR24}, 16384.0},
|
||||
|
||||
{{AV_PIX_FMT_BGR0, AV_PIX_FMT_BGR0}, std::numeric_limits<double_t>::max()},
|
||||
{{AV_PIX_FMT_BGR0, AV_PIX_FMT_BGRA}, 65535.0},
|
||||
{{AV_PIX_FMT_BGR0, AV_PIX_FMT_BGR24}, 32767.0},
|
||||
|
||||
{{AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8}, std::numeric_limits<double_t>::max()},
|
||||
{{AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9}, 65535.0},
|
||||
{{AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10}, 58981.5},
|
||||
{{AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY12}, 53083.35},
|
||||
{{AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY14}, 47775.015},
|
||||
{{AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16}, 42997.5135},
|
||||
|
||||
{{AV_PIX_FMT_BGR24, AV_PIX_FMT_BGR24}, std::numeric_limits<double_t>::max()},
|
||||
{{AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24}, 32767.0},
|
||||
};
|
||||
|
||||
AVPixelFormat ffmpeg::tools::get_best_compatible_format(const AVPixelFormat* list, AVPixelFormat source)
|
||||
{
|
||||
double_t score = std::numeric_limits<double_t>::min();
|
||||
AVPixelFormat best = source;
|
||||
|
||||
for (auto fmt = list; fmt && (*fmt != AV_PIX_FMT_NONE); fmt++) {
|
||||
auto found = format_compatibility.find(std::pair{source, *fmt});
|
||||
if (found != format_compatibility.end() && (score < found->second)) {
|
||||
score = found->second;
|
||||
best = *fmt;
|
||||
}
|
||||
}
|
||||
|
||||
if (score <= 0) {
|
||||
int data_loss = 0;
|
||||
return avcodec_find_best_pix_fmt_of_list(list, source, 0, &data_loss);
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
|
||||
void ffmpeg::tools::setup_obs_color(video_colorspace colorspace, video_range_type range, AVCodecContext* context)
|
||||
{
|
||||
std::map<video_colorspace, std::tuple<AVColorSpace, AVColorPrimaries, AVColorTransferCharacteristic>>
|
||||
colorspaces = {
|
||||
{VIDEO_CS_DEFAULT, {AVCOL_SPC_BT470BG, AVCOL_PRI_BT470BG, AVCOL_TRC_SMPTE170M}},
|
||||
{VIDEO_CS_601, {AVCOL_SPC_BT470BG, AVCOL_PRI_BT470BG, AVCOL_TRC_SMPTE170M}},
|
||||
{VIDEO_CS_709, {AVCOL_SPC_BT709, AVCOL_PRI_BT709, AVCOL_TRC_BT709}},
|
||||
};
|
||||
std::map<video_range_type, AVColorRange> colorranges = {
|
||||
{VIDEO_RANGE_DEFAULT, AVCOL_RANGE_MPEG},
|
||||
{VIDEO_RANGE_PARTIAL, AVCOL_RANGE_MPEG},
|
||||
{VIDEO_RANGE_FULL, AVCOL_RANGE_JPEG},
|
||||
};
|
||||
|
||||
{
|
||||
auto found = colorspaces.find(colorspace);
|
||||
if (found != colorspaces.end()) {
|
||||
context->colorspace = std::get<AVColorSpace>(found->second);
|
||||
context->color_primaries = std::get<AVColorPrimaries>(found->second);
|
||||
context->color_trc = std::get<AVColorTransferCharacteristic>(found->second);
|
||||
}
|
||||
}
|
||||
{
|
||||
auto found = colorranges.find(range);
|
||||
if (found != colorranges.end()) {
|
||||
context->color_range = found->second;
|
||||
}
|
||||
}
|
||||
|
||||
// Downscaling should result in downscaling, not pixelation
|
||||
context->chroma_sample_location = AVCHROMA_LOC_CENTER;
|
||||
}
|
||||
|
||||
@@ -25,8 +25,10 @@
|
||||
|
||||
#include <obs.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
extern "C" {
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavutil/pixfmt.h>
|
||||
}
|
||||
|
||||
@@ -44,9 +46,19 @@ namespace ffmpeg {
|
||||
|
||||
video_format avpixelformat_to_obs_videoformat(AVPixelFormat v);
|
||||
|
||||
AVPixelFormat get_least_lossy_format(const AVPixelFormat* haystack, AVPixelFormat needle);
|
||||
|
||||
AVColorSpace obs_videocolorspace_to_avcolorspace(video_colorspace v);
|
||||
|
||||
AVColorRange obs_videorangetype_to_avcolorrange(video_range_type v);
|
||||
|
||||
bool can_hardware_encode(const AVCodec* codec);
|
||||
|
||||
std::vector<AVPixelFormat> get_software_formats(const AVPixelFormat* list);
|
||||
|
||||
AVPixelFormat get_best_compatible_format(const AVPixelFormat* list, AVPixelFormat source);
|
||||
|
||||
void setup_obs_color(video_colorspace colorspace, video_range_type range, AVCodecContext* context);
|
||||
} // namespace tools
|
||||
} // namespace ffmpeg
|
||||
|
||||
|
||||
@@ -22,3 +22,29 @@
|
||||
#include "handler.hpp"
|
||||
|
||||
void obsffmpeg::ui::handler::override_visible_name(const AVCodec*, std::string&) {}
|
||||
|
||||
void obsffmpeg::ui::handler::override_info(obs_encoder_info* main, obs_encoder_info* fallback) {}
|
||||
|
||||
void obsffmpeg::ui::handler::override_colorformat(AVPixelFormat& target_format, obs_data_t* settings,
|
||||
const AVCodec* codec, AVCodecContext* context)
|
||||
{}
|
||||
|
||||
void obsffmpeg::ui::handler::get_defaults(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context) {}
|
||||
|
||||
void obsffmpeg::ui::handler::get_properties(obs_properties_t* props, const AVCodec* codec, AVCodecContext* context) {}
|
||||
|
||||
void obsffmpeg::ui::handler::update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context) {}
|
||||
|
||||
void obsffmpeg::ui::handler::log_options(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context) {}
|
||||
|
||||
void obsffmpeg::ui::handler::import_from_ffmpeg(const std::string ffmpeg, obs_data_t* settings, const AVCodec* codec,
|
||||
AVCodecContext* context)
|
||||
{}
|
||||
|
||||
std::string obsffmpeg::ui::handler::export_for_ffmpeg(obs_data_t* settings, const AVCodec* codec,
|
||||
AVCodecContext* context)
|
||||
{
|
||||
return std::string();
|
||||
}
|
||||
|
||||
void obsffmpeg::ui::handler::process_avpacket(AVPacket& packet, const AVCodec* codec, AVCodecContext* context) {}
|
||||
|
||||
+21
-4
@@ -24,7 +24,10 @@
|
||||
#include <string>
|
||||
|
||||
extern "C" {
|
||||
#include <obs.h>
|
||||
|
||||
#include <obs-data.h>
|
||||
#include <obs-encoder.h>
|
||||
#include <obs-properties.h>
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4244)
|
||||
@@ -38,13 +41,27 @@ namespace obsffmpeg {
|
||||
public:
|
||||
virtual void override_visible_name(const AVCodec* codec, std::string& name);
|
||||
|
||||
virtual void get_defaults(obs_data_t* settings, const AVCodec* codec,
|
||||
AVCodecContext* context) = 0;
|
||||
virtual void override_info(obs_encoder_info* main, obs_encoder_info* fallback);
|
||||
|
||||
virtual void override_colorformat(AVPixelFormat& target_format, obs_data_t* settings,
|
||||
const AVCodec* codec, AVCodecContext* context);
|
||||
|
||||
virtual void get_defaults(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context);
|
||||
|
||||
virtual void get_properties(obs_properties_t* props, const AVCodec* codec,
|
||||
AVCodecContext* context) = 0;
|
||||
AVCodecContext* context);
|
||||
|
||||
virtual void update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context) = 0;
|
||||
virtual void update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context);
|
||||
|
||||
virtual void log_options(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context);
|
||||
|
||||
virtual void import_from_ffmpeg(const std::string ffmpeg, obs_data_t* settings,
|
||||
const AVCodec* codec, AVCodecContext* context);
|
||||
|
||||
virtual std::string export_for_ffmpeg(obs_data_t* settings, const AVCodec* codec,
|
||||
AVCodecContext* context);
|
||||
|
||||
virtual void process_avpacket(AVPacket& packet, const AVCodec* codec, AVCodecContext* context);
|
||||
};
|
||||
} // namespace ui
|
||||
} // namespace obsffmpeg
|
||||
|
||||
@@ -57,6 +57,7 @@ Useless except for strict_gop maybe?
|
||||
*/
|
||||
|
||||
using namespace obsffmpeg::codecs::h264;
|
||||
using namespace obsffmpeg::nvenc;
|
||||
|
||||
std::map<profile, std::string> profiles{
|
||||
{profile::BASELINE, "baseline"},
|
||||
@@ -125,6 +126,22 @@ void obsffmpeg::ui::nvenc_h264_handler::update(obs_data_t* settings, const AVCod
|
||||
}
|
||||
}
|
||||
|
||||
void obsffmpeg::ui::nvenc_h264_handler::log_options(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context)
|
||||
{
|
||||
nvenc::log_options(settings, codec, context);
|
||||
|
||||
profile cfg_profile = static_cast<profile>(obs_data_get_int(settings, P_H264_PROFILE));
|
||||
level cfg_level = static_cast<level>(obs_data_get_int(settings, P_H264_LEVEL));
|
||||
|
||||
auto found1 = profiles.find(cfg_profile);
|
||||
if (found1 != profiles.end())
|
||||
PLOG_INFO("[%s] H.264 Profile: %s", codec->name, found1->second.c_str());
|
||||
|
||||
auto found2 = levels.find(cfg_level);
|
||||
if (found2 != levels.end())
|
||||
PLOG_INFO("[%s] H.264 Level: %s", codec->name, found2->second.c_str());
|
||||
}
|
||||
|
||||
void obsffmpeg::ui::nvenc_h264_handler::get_encoder_properties(obs_properties_t* props, const AVCodec* codec)
|
||||
{
|
||||
nvenc::get_properties_pre(props, codec);
|
||||
|
||||
@@ -45,6 +45,9 @@ namespace obsffmpeg {
|
||||
virtual void update(obs_data_t* settings, const AVCodec* codec,
|
||||
AVCodecContext* context) override;
|
||||
|
||||
virtual void log_options(obs_data_t* settings, const AVCodec* codec,
|
||||
AVCodecContext* context) override;
|
||||
|
||||
private:
|
||||
void get_encoder_properties(obs_properties_t* props, const AVCodec* codec);
|
||||
|
||||
|
||||
@@ -133,6 +133,27 @@ void obsffmpeg::ui::nvenc_hevc_handler::update(obs_data_t* settings, const AVCod
|
||||
}
|
||||
}
|
||||
|
||||
void obsffmpeg::ui::nvenc_hevc_handler::log_options(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context)
|
||||
{
|
||||
nvenc::log_options(settings, codec, context);
|
||||
|
||||
profile cfg_profile = static_cast<profile>(obs_data_get_int(settings, P_HEVC_PROFILE));
|
||||
tier cfg_tier = static_cast<tier>(obs_data_get_int(settings, P_HEVC_TIER));
|
||||
level cfg_level = static_cast<level>(obs_data_get_int(settings, P_HEVC_LEVEL));
|
||||
|
||||
auto found1 = profiles.find(cfg_profile);
|
||||
if (found1 != profiles.end())
|
||||
PLOG_INFO("[%s] H.265 Profile: %s", codec->name, found1->second.c_str());
|
||||
|
||||
auto found2 = levels.find(cfg_level);
|
||||
if (found2 != levels.end())
|
||||
PLOG_INFO("[%s] H.265 Level: %s", codec->name, found2->second.c_str());
|
||||
|
||||
auto found3 = tiers.find(cfg_tier);
|
||||
if (found3 != tiers.end())
|
||||
PLOG_INFO("[%s] H.265 Tier: %s", codec->name, found3->second.c_str());
|
||||
}
|
||||
|
||||
void obsffmpeg::ui::nvenc_hevc_handler::get_encoder_properties(obs_properties_t* props, const AVCodec* codec)
|
||||
{
|
||||
nvenc::get_properties_pre(props, codec);
|
||||
|
||||
@@ -45,6 +45,9 @@ namespace obsffmpeg {
|
||||
virtual void update(obs_data_t* settings, const AVCodec* codec,
|
||||
AVCodecContext* context) override;
|
||||
|
||||
virtual void log_options(obs_data_t* settings, const AVCodec* codec,
|
||||
AVCodecContext* context) override;
|
||||
|
||||
private:
|
||||
void get_encoder_properties(obs_properties_t* props, const AVCodec* codec);
|
||||
|
||||
|
||||
+118
-8
@@ -57,7 +57,7 @@ extern "C" {
|
||||
#define ST_RATECONTROL_QP_I ST_RATECONTROL_QP ".I"
|
||||
#define ST_RATECONTROL_QP_I_INITIAL ST_RATECONTROL_QP_I ".Initial"
|
||||
#define ST_RATECONTROL_QP_P ST_RATECONTROL_QP ".P"
|
||||
#define ST_RATECONTROL_QP_ST_INITIAL ST_RATECONTROL_QP_P ".Initial"
|
||||
#define ST_RATECONTROL_QP_P_INITIAL ST_RATECONTROL_QP_P ".Initial"
|
||||
#define ST_RATECONTROL_QP_B ST_RATECONTROL_QP ".B"
|
||||
#define ST_RATECONTROL_QP_B_INITIAL ST_RATECONTROL_QP_B ".Initial"
|
||||
|
||||
@@ -152,7 +152,7 @@ void obsffmpeg::nvenc::get_defaults(obs_data_t* settings, const AVCodec*, AVCode
|
||||
obs_data_set_default_int(settings, ST_RATECONTROL_QP_I, 21);
|
||||
obs_data_set_default_int(settings, ST_RATECONTROL_QP_I_INITIAL, -1);
|
||||
obs_data_set_default_int(settings, ST_RATECONTROL_QP_P, 21);
|
||||
obs_data_set_default_int(settings, ST_RATECONTROL_QP_ST_INITIAL, -1);
|
||||
obs_data_set_default_int(settings, ST_RATECONTROL_QP_P_INITIAL, -1);
|
||||
obs_data_set_default_int(settings, ST_RATECONTROL_QP_B, 21);
|
||||
obs_data_set_default_int(settings, ST_RATECONTROL_QP_B_INITIAL, -1);
|
||||
|
||||
@@ -211,7 +211,7 @@ static bool modified_ratecontrol(obs_properties_t* props, obs_property_t*, obs_d
|
||||
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QP_P), have_qp);
|
||||
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QP_B), have_qp);
|
||||
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QP_I_INITIAL), have_qp_init);
|
||||
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QP_ST_INITIAL), have_qp_init);
|
||||
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QP_P_INITIAL), have_qp_init);
|
||||
obs_property_set_visible(obs_properties_get(props, ST_RATECONTROL_QP_B_INITIAL), have_qp_init);
|
||||
|
||||
return true;
|
||||
@@ -379,9 +379,9 @@ void obsffmpeg::nvenc::get_properties_post(obs_properties_t* props, const AVCode
|
||||
obs_property_set_long_description(p, TRANSLATE(DESC(ST_RATECONTROL_QP_P)));
|
||||
}
|
||||
{
|
||||
auto p = obs_properties_add_int_slider(grp, ST_RATECONTROL_QP_ST_INITIAL,
|
||||
TRANSLATE(ST_RATECONTROL_QP_ST_INITIAL), -1, 51, 1);
|
||||
obs_property_set_long_description(p, TRANSLATE(DESC(ST_RATECONTROL_QP_ST_INITIAL)));
|
||||
auto p = obs_properties_add_int_slider(grp, ST_RATECONTROL_QP_P_INITIAL,
|
||||
TRANSLATE(ST_RATECONTROL_QP_P_INITIAL), -1, 51, 1);
|
||||
obs_property_set_long_description(p, TRANSLATE(DESC(ST_RATECONTROL_QP_P_INITIAL)));
|
||||
}
|
||||
{
|
||||
auto p = obs_properties_add_int_slider(grp, ST_RATECONTROL_QP_B, TRANSLATE(ST_RATECONTROL_QP_B),
|
||||
@@ -483,7 +483,7 @@ void obsffmpeg::nvenc::get_runtime_properties(obs_properties_t* props, const AVC
|
||||
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QP_I), false);
|
||||
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QP_I_INITIAL), false);
|
||||
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QP_P), false);
|
||||
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QP_ST_INITIAL), false);
|
||||
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QP_P_INITIAL), false);
|
||||
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QP_B), false);
|
||||
obs_property_set_enabled(obs_properties_get(props, ST_RATECONTROL_QP_B_INITIAL), false);
|
||||
obs_property_set_enabled(obs_properties_get(props, ST_AQ), false);
|
||||
@@ -597,7 +597,7 @@ void obsffmpeg::nvenc::update(obs_data_t* settings, const AVCodec* codec, AVCode
|
||||
av_opt_set_int(context->priv_data, "init_qpI",
|
||||
obs_data_get_int(settings, ST_RATECONTROL_QP_I_INITIAL), 0);
|
||||
av_opt_set_int(context->priv_data, "init_qpP",
|
||||
obs_data_get_int(settings, ST_RATECONTROL_QP_ST_INITIAL), 0);
|
||||
obs_data_get_int(settings, ST_RATECONTROL_QP_P_INITIAL), 0);
|
||||
av_opt_set_int(context->priv_data, "init_qpB",
|
||||
obs_data_get_int(settings, ST_RATECONTROL_QP_B_INITIAL), 0);
|
||||
}
|
||||
@@ -646,3 +646,113 @@ void obsffmpeg::nvenc::update(obs_data_t* settings, const AVCodec* codec, AVCode
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void obsffmpeg::nvenc::log_options(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context)
|
||||
{
|
||||
preset cfg_preset = static_cast<preset>(obs_data_get_int(settings, ST_PRESET));
|
||||
|
||||
auto found1 = preset_to_opt.find(cfg_preset);
|
||||
if (found1 != preset_to_opt.end())
|
||||
PLOG_INFO("[%s] Preset: %s", codec->name, found1->second.c_str());
|
||||
|
||||
ratecontrolmode cfg_rc_mode = static_cast<ratecontrolmode>(obs_data_get_int(settings, ST_RATECONTROL_MODE));
|
||||
int64_t cfg_rc_2pass = obs_data_get_int(settings, ST_RATECONTROL_TWOPASS);
|
||||
int64_t cfg_rc_lahead = obs_data_get_int(settings, ST_RATECONTROL_LOOKAHEAD);
|
||||
bool cfg_rc_adapti = obs_data_get_bool(settings, ST_RATECONTROL_ADAPTIVEI);
|
||||
bool cfg_rc_adaptb = obs_data_get_bool(settings, ST_RATECONTROL_ADAPTIVEB);
|
||||
|
||||
auto found2 = ratecontrolmode_to_opt.find(cfg_rc_mode);
|
||||
if (found2 != ratecontrolmode_to_opt.end())
|
||||
PLOG_INFO("[%s] Rate Control: %s", codec->name, found2->second.c_str());
|
||||
PLOG_INFO("[%s] Two Pass: %s", codec->name,
|
||||
cfg_rc_2pass == 1 ? "Enabled" : (cfg_rc_2pass == 0 ? "Disabled" : "Default"));
|
||||
PLOG_INFO("[%s] Lookahead: %" PRId64 " Frames", codec->name, cfg_rc_lahead);
|
||||
if (cfg_rc_adapti && cfg_rc_lahead > 0)
|
||||
PLOG_INFO("[%s] Adaptive I-Frames Enabled", codec->name);
|
||||
if (cfg_rc_adaptb && cfg_rc_lahead > 0)
|
||||
PLOG_INFO("[%s] Adaptive B-Frames Enabled", codec->name);
|
||||
|
||||
int64_t cfg_rc_bitrate = obs_data_get_int(settings, ST_RATECONTROL_BITRATE_TARGET);
|
||||
int64_t cfg_rc_max_bitrate = obs_data_get_int(settings, ST_RATECONTROL_BITRATE_MAXIMUM);
|
||||
int64_t cfg_rc_bufsize = obs_data_get_int(settings, S_RATECONTROL_BUFFERSIZE);
|
||||
bool cfg_rc_quality = obs_data_get_bool(settings, ST_RATECONTROL_QUALITY);
|
||||
int64_t cfg_rc_quality_min = obs_data_get_int(settings, ST_RATECONTROL_QUALITY_MINIMUM);
|
||||
int64_t cfg_rc_quality_max = obs_data_get_int(settings, ST_RATECONTROL_QUALITY_MAXIMUM);
|
||||
double_t cfg_rc_quality_tgt = obs_data_get_int(settings, ST_RATECONTROL_QUALITY_TARGET);
|
||||
int64_t cfg_rc_qp_i = obs_data_get_int(settings, ST_RATECONTROL_QP_I);
|
||||
int64_t cfg_rc_qp_p = obs_data_get_int(settings, ST_RATECONTROL_QP_P);
|
||||
int64_t cfg_rc_qp_b = obs_data_get_int(settings, ST_RATECONTROL_QP_B);
|
||||
int64_t cfg_rc_qp_i_init = obs_data_get_int(settings, ST_RATECONTROL_QP_I_INITIAL);
|
||||
int64_t cfg_rc_qp_p_init = obs_data_get_int(settings, ST_RATECONTROL_QP_P_INITIAL);
|
||||
int64_t cfg_rc_qp_b_init = obs_data_get_int(settings, ST_RATECONTROL_QP_B_INITIAL);
|
||||
|
||||
{
|
||||
bool have_bitrate = false;
|
||||
bool have_bitrate_max = false;
|
||||
bool have_quality = false;
|
||||
bool have_qp = false;
|
||||
bool have_qp_init = false;
|
||||
|
||||
switch (cfg_rc_mode) {
|
||||
case ratecontrolmode::CQP:
|
||||
have_qp = true;
|
||||
break;
|
||||
case ratecontrolmode::CBR:
|
||||
case ratecontrolmode::CBR_HQ:
|
||||
case ratecontrolmode::CBR_LD_HQ:
|
||||
have_bitrate = true;
|
||||
av_opt_set_int(context->priv_data, "cbr", 1, 0);
|
||||
break;
|
||||
case ratecontrolmode::VBR:
|
||||
case ratecontrolmode::VBR_HQ:
|
||||
have_bitrate_max = true;
|
||||
have_bitrate = true;
|
||||
have_quality = true;
|
||||
have_qp_init = true;
|
||||
break;
|
||||
}
|
||||
|
||||
PLOG_INFO("[%s] Buffer Size: %" PRId64, codec->name, cfg_rc_bufsize);
|
||||
if (have_bitrate)
|
||||
PLOG_INFO("[%s] Bitrate Target: %" PRId64, codec->name, cfg_rc_bitrate);
|
||||
if (have_bitrate_max)
|
||||
PLOG_INFO("[%s] Bitrate Maximum: %" PRId64, codec->name, cfg_rc_max_bitrate);
|
||||
if (have_quality && cfg_rc_quality) {
|
||||
PLOG_INFO("[%s] Quality Limits:", codec->name);
|
||||
PLOG_INFO("[%s] Minimum: %" PRId64, codec->name, cfg_rc_quality_min);
|
||||
PLOG_INFO("[%s] Maximum: %" PRId64, codec->name, cfg_rc_quality_max);
|
||||
PLOG_INFO("[%s] Target: %f", codec->name, cfg_rc_quality_tgt);
|
||||
}
|
||||
if (have_qp)
|
||||
PLOG_INFO("[%s] QP Values: %" PRId64 "/%" PRId64 "/%" PRId64, codec->name, cfg_rc_qp_i,
|
||||
cfg_rc_qp_p, cfg_rc_qp_b);
|
||||
if (have_qp_init)
|
||||
PLOG_INFO("[%s] Initial QP Values: %" PRId64 "/%" PRId64 "/%" PRId64, codec->name,
|
||||
cfg_rc_qp_i_init, cfg_rc_qp_p_init, cfg_rc_qp_b_init);
|
||||
}
|
||||
|
||||
bool cfg_aq_spatial = obs_data_get_bool(settings, ST_AQ_SPATIAL);
|
||||
int64_t cfg_aq_strength = obs_data_get_int(settings, ST_AQ_STRENGTH);
|
||||
bool cfg_aq_temporal = obs_data_get_bool(settings, ST_AQ_TEMPORAL);
|
||||
if (cfg_aq_spatial)
|
||||
PLOG_INFO("[%s] Spatial AQ Enabled: Strength %" PRId64, codec->name, cfg_aq_strength);
|
||||
if (cfg_aq_temporal)
|
||||
PLOG_INFO("[%s] Temporal AQ Enabled", codec->name);
|
||||
|
||||
int64_t cfg_bf = obs_data_get_int(settings, ST_OTHER_BFRAMES);
|
||||
b_ref_mode cfg_bf_mode = static_cast<b_ref_mode>(obs_data_get_int(settings, ST_OTHER_BFRAME_REFERENCEMODE));
|
||||
bool cfg_zerolatency = obs_data_get_bool(settings, ST_OTHER_ZEROLATENCY);
|
||||
bool cfg_weightp = obs_data_get_bool(settings, ST_OTHER_WEIGHTED_PREDICTION);
|
||||
bool cfg_nonrefp = obs_data_get_bool(settings, ST_OTHER_NONREFERENCE_PFRAMES);
|
||||
|
||||
PLOG_INFO("[%s] B-Frames: %" PRId64, codec->name, cfg_bf);
|
||||
auto found3 = b_ref_mode_to_opt.find(cfg_bf_mode);
|
||||
if (found3 != b_ref_mode_to_opt.end())
|
||||
PLOG_INFO("[%s] Reference Mode: %s", codec->name, found3->second.c_str());
|
||||
if (cfg_zerolatency)
|
||||
PLOG_INFO("[%s] Zero Latency Enabled", codec->name);
|
||||
if (cfg_weightp)
|
||||
PLOG_INFO("[%s] Weighted Prediction Enabled", codec->name);
|
||||
if (cfg_nonrefp)
|
||||
PLOG_INFO("[%s] Non-Ref P-Frames Enabled", codec->name);
|
||||
}
|
||||
|
||||
@@ -84,5 +84,7 @@ namespace obsffmpeg {
|
||||
void get_runtime_properties(obs_properties_t* props, const AVCodec* codec, AVCodecContext* context);
|
||||
|
||||
void update(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context);
|
||||
|
||||
void log_options(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context);
|
||||
} // namespace nvenc
|
||||
} // namespace obsffmpeg
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
// SOFTWARE.
|
||||
|
||||
#include "prores_aw_handler.hpp"
|
||||
#include "codecs/prores.hpp"
|
||||
#include "plugin.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
@@ -27,13 +28,6 @@ extern "C" {
|
||||
#include <obs-module.h>
|
||||
}
|
||||
|
||||
#define P_PROFILE "AppleProRes.Profile"
|
||||
#define P_PROFILE_APCO "AppleProRes.Profile.APCO"
|
||||
#define P_PROFILE_APCS "AppleProRes.Profile.APCS"
|
||||
#define P_PROFILE_APCN "AppleProRes.Profile.APCN"
|
||||
#define P_PROFILE_APCH "AppleProRes.Profile.APCH"
|
||||
#define P_PROFILE_AP4H "AppleProRes.Profile.AP4H"
|
||||
|
||||
INITIALIZER(prores_aw_handler_init)
|
||||
{
|
||||
obsffmpeg::initializers.push_back([]() {
|
||||
@@ -41,44 +35,94 @@ INITIALIZER(prores_aw_handler_init)
|
||||
});
|
||||
};
|
||||
|
||||
void obsffmpeg::ui::prores_aw_handler::override_colorformat(AVPixelFormat& target_format, obs_data_t* settings,
|
||||
const AVCodec* codec, AVCodecContext* context)
|
||||
{
|
||||
std::string profile = "";
|
||||
|
||||
int profile_id = static_cast<int>(obs_data_get_int(settings, P_PRORES_PROFILE));
|
||||
for (auto ptr = codec->profiles; ptr->profile != FF_PROFILE_UNKNOWN; ptr++) {
|
||||
if (ptr->profile == profile_id) {
|
||||
profile = ptr->name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::unordered_map<AVPixelFormat, std::list<std::string>> valid_formats = {
|
||||
{AV_PIX_FMT_YUV422P10, {"apco", "apcs", "apcn", "apch"}}, {AV_PIX_FMT_YUV444P10, {"ap4h", "ap4x"}}};
|
||||
|
||||
for (auto kv : valid_formats) {
|
||||
for (auto name : kv.second) {
|
||||
if (profile == name) {
|
||||
target_format = kv.first;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void obsffmpeg::ui::prores_aw_handler::get_defaults(obs_data_t* settings, const AVCodec*, AVCodecContext*)
|
||||
{
|
||||
obs_data_set_default_int(settings, P_PROFILE, 0);
|
||||
obs_data_set_default_int(settings, P_PRORES_PROFILE, 0);
|
||||
}
|
||||
|
||||
void obsffmpeg::ui::prores_aw_handler::get_properties(obs_properties_t* props, const AVCodec* codec,
|
||||
AVCodecContext* context)
|
||||
{
|
||||
if (!context) {
|
||||
auto p = obs_properties_add_list(props, P_PROFILE, TRANSLATE(P_PROFILE), OBS_COMBO_TYPE_LIST,
|
||||
OBS_COMBO_FORMAT_INT);
|
||||
obs_property_set_long_description(p, TRANSLATE(DESC(P_PROFILE)));
|
||||
auto p = obs_properties_add_list(props, P_PRORES_PROFILE, TRANSLATE(P_PRORES_PROFILE),
|
||||
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
|
||||
obs_property_set_long_description(p, TRANSLATE(DESC(P_PRORES_PROFILE)));
|
||||
for (auto ptr = codec->profiles; ptr->profile != FF_PROFILE_UNKNOWN; ptr++) {
|
||||
if (strcmp("apco", ptr->name) == 0) {
|
||||
obs_property_list_add_int(p, TRANSLATE(P_PROFILE_APCO),
|
||||
obs_property_list_add_int(p, TRANSLATE(P_PRORES_PROFILE_APCO),
|
||||
static_cast<int64_t>(ptr->profile));
|
||||
} else if (strcmp("apcs", ptr->name) == 0) {
|
||||
obs_property_list_add_int(p, TRANSLATE(P_PROFILE_APCS),
|
||||
obs_property_list_add_int(p, TRANSLATE(P_PRORES_PROFILE_APCS),
|
||||
static_cast<int64_t>(ptr->profile));
|
||||
} else if (strcmp("apcn", ptr->name) == 0) {
|
||||
obs_property_list_add_int(p, TRANSLATE(P_PROFILE_APCN),
|
||||
obs_property_list_add_int(p, TRANSLATE(P_PRORES_PROFILE_APCN),
|
||||
static_cast<int64_t>(ptr->profile));
|
||||
} else if (strcmp("apch", ptr->name) == 0) {
|
||||
obs_property_list_add_int(p, TRANSLATE(P_PROFILE_APCH),
|
||||
obs_property_list_add_int(p, TRANSLATE(P_PRORES_PROFILE_APCH),
|
||||
static_cast<int64_t>(ptr->profile));
|
||||
} else if (strcmp("ap4h", ptr->name) == 0) {
|
||||
obs_property_list_add_int(p, TRANSLATE(P_PROFILE_AP4H),
|
||||
obs_property_list_add_int(p, TRANSLATE(P_PRORES_PROFILE_AP4H),
|
||||
static_cast<int64_t>(ptr->profile));
|
||||
} else if (strcmp("ap4x", ptr->name) == 0) {
|
||||
obs_property_list_add_int(p, TRANSLATE(P_PRORES_PROFILE_AP4X),
|
||||
static_cast<int64_t>(ptr->profile));
|
||||
} else {
|
||||
obs_property_list_add_int(p, ptr->name, ptr->profile);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
obs_property_set_enabled(obs_properties_get(props, P_PROFILE), false);
|
||||
obs_property_set_enabled(obs_properties_get(props, P_PRORES_PROFILE), false);
|
||||
}
|
||||
}
|
||||
|
||||
void obsffmpeg::ui::prores_aw_handler::update(obs_data_t* settings, const AVCodec*, AVCodecContext* context)
|
||||
{
|
||||
context->profile = static_cast<int>(obs_data_get_int(settings, P_PROFILE));
|
||||
context->profile = static_cast<int>(obs_data_get_int(settings, P_PRORES_PROFILE));
|
||||
}
|
||||
|
||||
void obsffmpeg::ui::prores_aw_handler::log_options(obs_data_t* settings, const AVCodec* codec, AVCodecContext* context)
|
||||
{
|
||||
for (auto ptr = codec->profiles; ptr->profile != FF_PROFILE_UNKNOWN; ptr++) {
|
||||
if (ptr->profile == static_cast<int>(obs_data_get_int(settings, P_PRORES_PROFILE)))
|
||||
PLOG_INFO("[%s] Profile: %s", codec->name, ptr->name);
|
||||
}
|
||||
}
|
||||
|
||||
void obsffmpeg::ui::prores_aw_handler::process_avpacket(AVPacket& packet, const AVCodec* codec, AVCodecContext* context)
|
||||
{
|
||||
//FFmpeg Bug:
|
||||
// When ProRes content is stored in Matroska, FFmpeg strips the size
|
||||
// from the atom. Later when the ProRes content is demuxed from Matroska,
|
||||
// FFmpeg creates an atom with the incorrect size, as the ATOM size
|
||||
// should be content + atom, but FFmpeg set it to only be content. This
|
||||
// difference leads to decoders to be off by 8 bytes.
|
||||
//Fix (until FFmpeg stops being broken):
|
||||
// Pad the packet with 8 bytes of 0x00.
|
||||
|
||||
av_grow_packet(&packet, 8);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,9 @@ namespace obsffmpeg {
|
||||
namespace ui {
|
||||
class prores_aw_handler : public handler {
|
||||
public:
|
||||
virtual void override_colorformat(AVPixelFormat& target_format, obs_data_t* settings,
|
||||
const AVCodec* codec, AVCodecContext* context) override;
|
||||
|
||||
virtual void get_defaults(obs_data_t* settings, const AVCodec* codec,
|
||||
AVCodecContext* context) override;
|
||||
|
||||
@@ -42,6 +45,11 @@ namespace obsffmpeg {
|
||||
|
||||
virtual void update(obs_data_t* settings, const AVCodec* codec,
|
||||
AVCodecContext* context) override;
|
||||
|
||||
virtual void log_options(obs_data_t* settings, const AVCodec* codec,
|
||||
AVCodecContext* context) override;
|
||||
|
||||
virtual void process_avpacket(AVPacket& packet, const AVCodec* codec, AVCodecContext* context) override;
|
||||
};
|
||||
} // namespace ui
|
||||
} // namespace obsffmpeg
|
||||
|
||||
Reference in New Issue
Block a user