diff --git a/cpp/obs/src/moq-dock.cpp b/cpp/obs/src/moq-dock.cpp index e95e372f31e..894b57f6f99 100644 --- a/cpp/obs/src/moq-dock.cpp +++ b/cpp/obs/src/moq-dock.cpp @@ -6,6 +6,7 @@ #include "moq-dial.h" #include "moq-spark-data.h" #include "moq-encoder-latency.h" +#include "moq-encoder-rate.h" #include "moq-quality-defaults.h" #include "moq-settings.h" #include "logger.h" @@ -967,8 +968,7 @@ bool MoQDock::CreateCustomEncoders() OBSDataAutoRelease videoSettings = obs_data_create(); OBSDataAutoRelease audioSettings = obs_data_create(); - obs_data_set_int(videoSettings, "bitrate", videoBitrate); - obs_data_set_string(videoSettings, "rate_control", "CBR"); + const bool capped = ApplyEncoderRate(videoId.toStdString(), videoSettings, videoBitrate); obs_data_set_bool(videoSettings, "repeat_headers", true); obs_data_set_int(audioSettings, "bitrate", audioBitrate); @@ -995,8 +995,9 @@ bool MoQDock::CreateCustomEncoders() obs_encoder_set_video(videoEncoder, obs_get_video()); obs_encoder_set_audio(audioEncoder, obs_get_audio()); - LOG_INFO("Using dock custom encoders: %s / %s (profile %s, %d kbps)", videoId.toUtf8().constData(), - audioId.toUtf8().constData(), high ? "quality" : "performance", videoBitrate); + LOG_INFO("Using dock custom encoders: %s / %s (profile %s, %s %d kbps)", videoId.toUtf8().constData(), + audioId.toUtf8().constData(), high ? "quality" : "performance", capped ? "capped VBR, peak" : "CBR", + videoBitrate); QString profileLabel = high ? QStringLiteral("Quality") : QStringLiteral("Performance"); if (profile == "auto") @@ -1004,10 +1005,11 @@ bool MoQDock::CreateCustomEncoders() const char *videoDisplay = obs_encoder_get_display_name(videoId.toUtf8().constData()); const char *audioDisplay = obs_encoder_get_display_name(audioId.toUtf8().constData()); const QString pathLabel = preferHw ? QStringLiteral("Hardware") : QStringLiteral("Software"); - publishSummary = QString("Preset %1 · %2\nVideo %3 · %4 · %5 kbps\nAudio %6 · %7 · %8 kbps") + publishSummary = QString("Preset %1 · %2\nVideo %3 · %4 · %5 %6 kbps\nAudio %7 · %8 · %9 kbps") .arg(profileLabel, pathLabel, codec.isEmpty() ? QStringLiteral("video") : codec.toUpper(), - videoDisplay && *videoDisplay ? QString::fromUtf8(videoDisplay) : videoId) + videoDisplay && *videoDisplay ? QString::fromUtf8(videoDisplay) : videoId, + capped ? QStringLiteral("up to") : QStringLiteral("CBR")) .arg(videoBitrate) .arg(audioChoice.isEmpty() ? QStringLiteral("audio") : audioChoice.toUpper(), audioDisplay && *audioDisplay ? QString::fromUtf8(audioDisplay) : audioId) diff --git a/cpp/obs/src/moq-encoder-rate.h b/cpp/obs/src/moq-encoder-rate.h new file mode 100644 index 00000000000..10870dc3f52 --- /dev/null +++ b/cpp/obs/src/moq-encoder-rate.h @@ -0,0 +1,232 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +inline std::string Upper(std::string text) +{ + for (char &c : text) + c = static_cast(std::toupper(static_cast(c))); + return text; +} + +// The setting that holds the hard peak an encoder honors under `rate_control`, or null +// when it has none (CQP, CRF, ICQ, or an encoder whose peak is not modeled, like QSV and AMF). +// The peak is what reservations, the catalog, and dynamic bitrate price a rendition at. +inline const char *EncoderPeakKey(const std::string &id, std::string rate_control) +{ + rate_control = Upper(std::move(rate_control)); + // CBR holds the encoder at its bitrate, so that is also its peak. + if (rate_control == "CBR") + return "bitrate"; + // x264's VBR is constant quality (CRF) and its ABR an average, both under a VBV whose max + // rate is the bitrate. + if (id == "obs_x264") + return rate_control == "VBR" || rate_control == "ABR" ? "bitrate" : nullptr; + // NVENC's VBR averages at "bitrate" but never exceeds "max_bitrate"; CQVBR is + // constant quality under that same max, and ignores "bitrate". + if (id.rfind("obs_nvenc_", 0) == 0) + return rate_control == "VBR" || rate_control == "CQVBR" ? "max_bitrate" : nullptr; + return nullptr; +} + +// An encoder's hard peak in kbps and the setting that holds it. +struct MoQPeak { + const char *key = nullptr; + long long kbps = 0; + // The average NVENC's plain VBR holds beside its peak, which must stay at or below it, + // or 0 when the encoder has none. + long long average_kbps = 0; +}; + +// The peak these encoder settings configure, with a null key when they configure none. +inline MoQPeak EncoderPeak(const std::string &id, obs_data_t *settings) +{ + const std::string rate_control = obs_data_get_string(settings, "rate_control"); + const char *key = EncoderPeakKey(id, rate_control); + if (!key) + return {}; + const long long kbps = obs_data_get_int(settings, key); + if (kbps <= 0) + return {}; + // NVENC's plain VBR averages at "bitrate" beside its peak in "max_bitrate". + const bool averaged = std::string(key) == "max_bitrate" && Upper(rate_control) == "VBR"; + return {key, kbps, averaged ? obs_data_get_int(settings, "bitrate") : 0}; +} + +// x264's own default constant quality level, which a lossless CRF of 0 gives way to. +constexpr long long kDefaultX264Crf = 23; + +inline bool IsNvenc(const std::string &id) +{ + return id.rfind("obs_nvenc_", 0) == 0; +} + +// The cap in kbps the settings name: NVENC's own peak when its VBR or constant quality +// sets one, otherwise the bitrate. 0 when the settings name none. +inline long long ConfiguredCapKbps(const std::string &id, obs_data_t *settings) +{ + if (IsNvenc(id)) { + const std::string rate_control = Upper(obs_data_get_string(settings, "rate_control")); + const long long peak = obs_data_get_int(settings, "max_bitrate"); + if ((rate_control == "VBR" || rate_control == "CQVBR") && peak > 0) + return peak; + } + return obs_data_get_int(settings, "bitrate"); +} + +// The hard peak in kbps an encoder already running with `settings` holds, or 0 when it runs +// uncapped: a quality mode with no peak (CQP, CRF, ICQ), x264's lossless CRF of 0 (constant +// QP with no VBV), or x264 with a buffer of its own set to 0, which makes it ignore the VBV +// in every mode. The settings are read as they are, never rewritten. +inline long long RunningCapKbps(const std::string &id, obs_data_t *settings) +{ + const MoQPeak peak = EncoderPeak(id, settings); + if (!peak.key) + return 0; + if (id == "obs_x264") { + if (obs_data_get_bool(settings, "use_bufsize") && obs_data_get_int(settings, "buffer_size") <= 0) + return 0; + // Only VBR reads the CRF: ABR and CBR zero it. + if (Upper(obs_data_get_string(settings, "rate_control")) == "VBR" && + obs_data_get_int(settings, "crf") <= 0) + return 0; + } + return peak.kbps; +} + +// The setting that holds an encoder's free-form options, which OBS applies after its own +// rate settings, or null when it has none. +inline const char *EncoderOptionsKey(const std::string &id) +{ + if (id == "obs_x264") + return "x264opts"; + return IsNvenc(id) ? "opts" : nullptr; +} + +// The options the encoder's free-form options set a rate with, which replace the cap. +// For x264, `vbv-maxrate` and `vbv-bufsize` replace the VBV (0 disables it), `bitrate` +// replaces the rate mode, and `qp` or a `crf` of 0 (lossless) drops the VBV for constant QP. +// For NVENC, `maxBitRate`, `averageBitRate`, and `vbvBufferSize` replace the rate it was +// given. The plugin never rewrites the text, so these can override the cap. +inline std::vector RateOptions(const std::string &id, obs_data_t *settings) +{ + const char *key = EncoderOptionsKey(id); + std::vector found; + if (!key) + return found; + const bool x264 = id == "obs_x264"; + std::istringstream input(obs_data_get_string(settings, key)); + std::string option; + while (input >> option) { + std::string name = option.substr(0, option.find('=')); + for (char &c : name) + if (c == '_') + c = '-'; + const size_t assign = option.find('='); + const bool lossless = name == "crf" && assign != std::string::npos && + std::atof(option.c_str() + assign + 1) <= 0; + if (x264 ? name == "vbv-maxrate" || name == "vbv-bufsize" || name == "bitrate" || name == "qp" || + lossless + : name == "maxBitRate" || name == "averageBitRate" || name == "vbvBufferSize") + found.push_back(option); + } + return found; +} + +// What EnforceCap changed. +struct MoQCap { + // Whether `write` holds anything the encoder's settings did not already say. + bool changed = false; + // The user's rate settings it replaced, each described for a log line. + std::vector replaced; + // The free-form options that can override the cap, which it leaves as the user wrote them. + std::vector overridden; +}; + +// Makes `id` hold `cap_kbps` as its hard peak, writing what differs from `read` into +// `write` (the same settings, to rewrite them in place). The cap is the plugin's: it +// replaces any rate the user set that would exceed it, and says which in `replaced`. +// - x264 keeps CBR and otherwise runs VBR (CRF under a VBV whose max rate and buffer are +// the bitrate). It writes only OBS's own settings: `x264opts` (and NVENC's `opts`) stay as +// the user wrote them, and the options in them that set a rate are reported in `overridden`. +// - NVENC keeps CBR and VBR, with `max_bitrate` at the cap and the average under it, and +// otherwise runs CQVBR (constant quality under `max_bitrate`). +// - Everything else runs CBR at the cap: its quality modes carry no hard peak, or depend +// on a driver or a Mac to check. +// The quality level is the encoder's own default. An unset rate mode (a fresh dock profile) +// takes the quality mode where there is one without being reported as replaced. +inline MoQCap EnforceCap(const std::string &id, obs_data_t *read, obs_data_t *write, long long cap_kbps) +{ + MoQCap result; + const std::string before = obs_data_get_string(read, "rate_control"); + const std::string mode = Upper(before); + const long long average = obs_data_get_int(read, "bitrate"); + const bool use_bufsize = obs_data_get_bool(read, "use_bufsize"); + + auto set_int = [&](const char *key, long long value) { + if (obs_data_get_int(read, key) == value) + return; + obs_data_set_int(write, key, value); + result.changed = true; + }; + auto set_mode = [&](const char *want) { + if (mode == want) + return; + obs_data_set_string(write, "rate_control", want); + result.changed = true; + if (!before.empty()) + result.replaced.push_back("rate_control " + before + " -> " + want); + }; + + if (id == "obs_x264") { + set_mode(mode == "CBR" ? "CBR" : "VBR"); + set_int("bitrate", cap_kbps); + // A CRF of 0 is lossless, which x264 runs as constant QP with no VBV. CBR ignores the setting. + if (mode != "CBR" && obs_data_get_int(read, "crf") <= 0) { + obs_data_set_int(write, "crf", kDefaultX264Crf); + result.changed = true; + result.replaced.push_back("crf 0"); + } + // A buffer of its own can hold a burst past the cap; unset, OBS sizes it to the bitrate. + if (use_bufsize) { + obs_data_set_bool(write, "use_bufsize", false); + result.changed = true; + result.replaced.push_back("use_bufsize"); + } + } else if (IsNvenc(id)) { + set_mode(mode == "CBR" || mode == "VBR" ? mode.c_str() : "CQVBR"); + // CBR's peak is its bitrate and constant quality ignores it; VBR averages under its peak. + if (mode == "CBR") { + set_int("bitrate", cap_kbps); + } else { + set_int("max_bitrate", cap_kbps); + if (mode == "VBR" && average > cap_kbps) { + set_int("bitrate", cap_kbps); + result.replaced.push_back("bitrate " + std::to_string(average)); + } + } + } else { + set_mode("CBR"); + set_int("bitrate", cap_kbps); + } + result.overridden = RateOptions(id, read); + return result; +} + +// Configures a fresh `settings` to run `id` at constant quality under a peak of `cap_kbps`, +// where the encoder honors one, and at CBR at that rate where it does not. Returns whether +// the quality mode took. +inline bool ApplyEncoderRate(const std::string &id, obs_data_t *settings, long long cap_kbps) +{ + obs_data_set_string(settings, "rate_control", ""); + EnforceCap(id, settings, settings, cap_kbps); + return EncoderPeakKey(id, obs_data_get_string(settings, "rate_control")) != nullptr && + Upper(obs_data_get_string(settings, "rate_control")) != "CBR"; +} diff --git a/cpp/obs/src/moq-output.cpp b/cpp/obs/src/moq-output.cpp index 399b8774c1c..266b9f8a595 100644 --- a/cpp/obs/src/moq-output.cpp +++ b/cpp/obs/src/moq-output.cpp @@ -2,6 +2,7 @@ #include #include "moq-output.h" +#include "moq-encoder-rate.h" #include "moq-error.h" #include "moq-implementation.h" #include "moq-settings.h" @@ -72,14 +73,86 @@ const moq::Error *PhaseFailure(const moq::ConnectionPhase &phase) phase.get_variant()); } -// Retune `encoder` to `bitrate_bps`, the way OBS's own RTMP dynamic bitrate does. -void SetBitrate(obs_encoder_t *encoder, uint64_t bitrate_bps) +// Retune `encoder` to `bitrate_bps` in its `key` setting, the way OBS's own RTMP dynamic +// bitrate does. The key is where the encoder holds its peak: "bitrate" for most, but +// "max_bitrate" for NVENC's constant quality. An encoder that also averages (NVENC's plain +// VBR) keeps that average at `average_kbps`, lowered to the peak so the pair stays valid. +void SetBitrate(obs_encoder_t *encoder, const char *key, uint64_t bitrate_bps, long long average_kbps) { OBSDataAutoRelease settings = obs_data_create(); - obs_data_set_int(settings, "bitrate", (long long)(bitrate_bps / 1000)); + const long long peak_kbps = (long long)(bitrate_bps / 1000); + obs_data_set_int(settings, key, peak_kbps); + if (average_kbps > 0) + obs_data_set_int(settings, "bitrate", std::min(average_kbps, peak_kbps)); obs_encoder_update(encoder, settings); } +// The kbps this encoder is priced at: its hard peak when it has one, else its bitrate +// setting, which a quality target (CQP, CRF) only reserves. +int EncoderCapKbps(const MoQPeak &peak, obs_data_t *settings) +{ + if (peak.key) + return (int)peak.kbps; + return settings ? (int)obs_data_get_int(settings, "bitrate") : 0; +} + +// The hard peak `encoder`'s settings configure, if any. +MoQPeak PeakOf(obs_encoder_t *encoder, obs_data_t *settings) +{ + const char *id = obs_encoder_get_id(encoder); + return settings && id ? EncoderPeak(id, settings) : MoQPeak{}; +} + +// Makes every video encoder of `output` hold the cap its settings name as a hard peak, before +// OBS initializes it: the cap the catalog advertises is the one set here, whatever else the +// user's rate settings say. An encoder whose settings name no bitrate is left alone. +// An encoder already running (shared with a recording that started first) cannot switch rate +// control, so its settings stay as they are and the catalog advertises the cap it runs with. +// Returns why the output cannot start, or an empty string: one that runs uncapped (CQP, CRF, +// lossless) has no cap to advertise. +std::string CapEncoders(obs_output_t *output) +{ + for (uint32_t idx = 0; idx < MAX_OUTPUT_VIDEO_ENCODERS; idx++) { + obs_encoder_t *encoder = obs_output_get_video_encoder2(output, idx); + const char *id = encoder ? obs_encoder_get_id(encoder) : nullptr; + OBSDataAutoRelease settings = id ? obs_encoder_get_settings(encoder) : nullptr; + if (!settings) + continue; + const long long cap = ConfiguredCapKbps(id, settings); + if (cap <= 0) { + LOG_WARNING("Video encoder %s names no bitrate, so it has no cap to advertise", id); + continue; + } + const bool active = obs_encoder_active(encoder); + if (active) { + const long long running = RunningCapKbps(id, settings); + if (running <= 0) + return std::string("Video encoder ") + id + + " is already running without a bitrate cap (rate control " + + obs_data_get_string(settings, "rate_control") + + "). Stop the recording first, or give it its own encoder."; + for (const std::string &option : RateOptions(id, settings)) + LOG_WARNING("Video encoder %s: %s %s can override its %lld kbps cap", id, + EncoderOptionsKey(id), option.c_str(), running); + LOG_INFO("Video encoder %s already running, advertising its %lld kbps cap", id, running); + continue; + } + OBSDataAutoRelease patch = obs_data_create(); + const MoQCap result = EnforceCap(id, settings, patch, cap); + for (const std::string &replaced : result.replaced) + LOG_WARNING("Video encoder %s: replacing %s to hold its %lld kbps cap", id, replaced.c_str(), + cap); + for (const std::string &option : result.overridden) + LOG_WARNING("Video encoder %s: %s %s can override the %lld kbps cap. Use the MoQ " + "dock's Quality or Performance profile, which hold it without it.", + id, EncoderOptionsKey(id), option.c_str(), cap); + if (result.changed) + obs_encoder_update(encoder, patch); + LOG_INFO("Video encoder %s capped at %lld kbps", id, cap); + } + return ""; +} + // Whether `rate_control` lets the "bitrate" setting drive the encoder's output. // Encoders without the setting are bitrate-driven by construction. bool BitrateDriven(const std::string &rate_control) @@ -150,6 +223,14 @@ bool MoQOutput::Start() return false; } + // The cap goes on before initialization, which reads the settings it builds the encoder from. + const std::string uncapped = CapEncoders(output); + if (!uncapped.empty()) { + LOG_ERROR("%s", uncapped.c_str()); + obs_output_set_last_error(output, uncapped.c_str()); + return false; + } + if (!obs_output_initialize_encoders(output, 0)) { LOG_ERROR("Failed to initialize encoders"); return false; @@ -392,7 +473,13 @@ void MoQOutput::Reset() // Following the grant rewrote the encoder's bitrate, which the next Start() // reads back as the configured rate. Restored after media_mutex is released: // an idle encoder applies the update inline. - std::vector> restore; + struct Restore { + OBSEncoder encoder; + const char *key; + uint64_t bitrate_bps; + long long average_kbps; + }; + std::vector restore; std::unique_lock lock(media_mutex); for (auto &[encoder, track] : video_tracks) { @@ -400,7 +487,8 @@ void MoQOutput::Reset() track.producer->finish(); if (track.rate && track.applied_bps != track.bitrate_bps) { if (OBSEncoder strong = OBSGetStrongRef(track.encoder)) - restore.emplace_back(std::move(strong), track.bitrate_bps); + restore.push_back( + {std::move(strong), track.rate_key, track.bitrate_bps, track.average_kbps}); } } video_tracks.clear(); @@ -436,8 +524,8 @@ void MoQOutput::Reset() lock.unlock(); - for (auto &[encoder, bitrate] : restore) - SetBitrate(encoder, bitrate); + for (auto &r : restore) + SetBitrate(r.encoder, r.key, r.bitrate_bps, r.average_kbps); } std::string MoQOutput::GetInstance() @@ -738,7 +826,7 @@ void MoQOutput::VideoData(struct encoder_packet *packet) void MoQOutput::Retarget(obs_encoder_t *encoder, Track &track, const moq::RateTarget &target) { if (const auto *enabled = std::get_if(&target.get_variant())) { - SetBitrate(encoder, enabled->bitrate_bps); + SetBitrate(encoder, track.rate_key, enabled->bitrate_bps, track.average_kbps); track.applied_bps = enabled->bitrate_bps; track.enabled = true; return; @@ -788,7 +876,8 @@ void MoQOutput::VideoInit(obs_encoder_t *encoder) OBSDataAutoRelease settings = obs_encoder_get_settings(encoder); const auto video_width = obs_encoder_get_width(encoder); const auto video_height = obs_encoder_get_height(encoder); - const int video_bitrate_kbps = settings ? (int)obs_data_get_int(settings, "bitrate") : 0; + const MoQPeak peak = PeakOf(encoder, settings); + const int video_bitrate_kbps = EncoderCapKbps(peak, settings); uint8_t *extra_data = nullptr; size_t extra_size = 0; @@ -820,13 +909,14 @@ void MoQOutput::VideoInit(obs_encoder_t *encoder) config.data.assign(extra_data, extra_data + extra_size); // Seed catalog fields a downstream moq-transcode needs before measured rates - // arrive: coded size (also from SPS once parsed) and configured CBR bitrate so - // same-height ladder rungs can undercut the mezzanine. + // arrive: coded size (also from SPS once parsed) and the encoder's hard peak, which + // CBR and capped VBR both configure, so same-height ladder rungs can undercut the + // mezzanine. The catalog prices a rendition at that peak, never its measured rate. moq::VideoHint hint{}; if (video_width > 0 && video_height > 0) hint.coded = moq::Dimensions{video_width, video_height}; const std::string rate_control = settings ? obs_data_get_string(settings, "rate_control") : ""; - if (video_bitrate_kbps > 0 && (rate_control == "CBR" || rate_control == "cbr")) + if (peak.key) hint.bitrate = (uint64_t)video_bitrate_kbps * 1000ULL; hint.optimize_for_latency = true; config.hint = hint; @@ -847,7 +937,7 @@ void MoQOutput::VideoInit(obs_encoder_t *encoder) kbps = (int)(known->second.bitrate_bps / 1000); } else { OBSDataAutoRelease other_settings = obs_encoder_get_settings(other); - kbps = other_settings ? (int)obs_data_get_int(other_settings, "bitrate") : 0; + kbps = EncoderCapKbps(PeakOf(other, other_settings), other_settings); } if (kbps > lower_kbps && kbps < video_bitrate_kbps) { lower = other; @@ -887,10 +977,12 @@ void MoQOutput::VideoInit(obs_encoder_t *encoder) entry.producer = *track; if (video_bitrate_kbps > 0) { entry.bitrate_bps = (uint64_t)video_bitrate_kbps * 1000ULL; - // Following needs an encoder that retunes live and a rate control the - // bitrate drives; a quality target (CQP, CRF) only reserves. + // Following needs an encoder that retunes live and either holds a peak or has a + // rate control the bitrate drives; a quality target (CQP, CRF) only reserves. const bool dynamic = obs_encoder_get_caps(encoder) & OBS_ENCODER_CAP_DYN_BITRATE; - if (dynamic && BitrateDriven(rate_control)) { + if (dynamic && (peak.key || BitrateDriven(rate_control))) { + entry.rate_key = peak.key ? peak.key : "bitrate"; + entry.average_kbps = peak.average_kbps; entry.rate = moq::RateControl::init(moq::RatePolicy{entry.bitrate_bps, lower_bps}); entry.applied_bps = entry.bitrate_bps; entry.encoder = OBSGetWeakRef(encoder); diff --git a/cpp/obs/src/moq-output.h b/cpp/obs/src/moq-output.h index c3d49eea326..c1c15cd5d00 100644 --- a/cpp/obs/src/moq-output.h +++ b/cpp/obs/src/moq-output.h @@ -130,6 +130,11 @@ class MoQOutput { // weak ref so Reset() can hand the configured bitrate back. std::shared_ptr rate; OBSWeakEncoder encoder; + // The encoder setting that holds its peak, which following the grant retunes. + const char *rate_key = "bitrate"; + // The average NVENC's plain VBR holds beside its peak, or 0: following lowers it + // with the peak and Reset() restores it. + long long average_kbps = 0; // The bitrate the encoder was last told, which Reset() restores when it moved. uint64_t applied_bps = 0; // Whether the last target enabled this rendition. diff --git a/cpp/obs/test/moq-encoder-rate-test.cpp b/cpp/obs/test/moq-encoder-rate-test.cpp new file mode 100644 index 00000000000..1f053888123 --- /dev/null +++ b/cpp/obs/test/moq-encoder-rate-test.cpp @@ -0,0 +1,257 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#include "moq-encoder-rate.h" + +#include +#include +#include +#include +#include + +struct obs_data { + std::map> values; +}; + +bool obs_data_get_bool(obs_data_t *data, const char *key) +{ + auto entry = data->values.find(key); + return entry != data->values.end() && std::get(entry->second) != 0; +} +void obs_data_set_bool(obs_data_t *data, const char *key, bool value) +{ + data->values[key] = (long long)value; +} + +const char *obs_data_get_string(obs_data_t *data, const char *key) +{ + auto entry = data->values.find(key); + return entry == data->values.end() ? "" : std::get(entry->second).c_str(); +} +long long obs_data_get_int(obs_data_t *data, const char *key) +{ + auto entry = data->values.find(key); + return entry == data->values.end() ? 0 : std::get(entry->second); +} +void obs_data_set_string(obs_data_t *data, const char *key, const char *value) +{ + data->values[key] = std::string(value); +} +void obs_data_set_int(obs_data_t *data, const char *key, long long value) +{ + data->values[key] = value; +} + +static std::string rate_control(obs_data_t &settings) +{ + return obs_data_get_string(&settings, "rate_control"); +} + +int main() +{ + // x264 and NVENC take constant quality under the cap. + obs_data_t x264; + assert(ApplyEncoderRate("obs_x264", &x264, 8000)); + assert(rate_control(x264) == "VBR" && obs_data_get_int(&x264, "bitrate") == 8000); + assert(EncoderPeak("obs_x264", &x264).kbps == 8000); + + // A running encoder's cap is what its settings hold: its peak, or none for a quality mode, + // x264's lossless CRF, and x264's own buffer of 0 (which drops the VBV in every mode). + struct Running { + const char *id; + const char *mode; + long long crf; + long long expect; + bool use_bufsize = false; + long long buffer_size = 0; + }; + for (const Running &c : {Running{"obs_x264", "CBR", 0, 6000}, + Running{"obs_x264", "VBR", 23, 6000}, + Running{"obs_x264", "VBR", 0, 0}, + Running{"obs_x264", "ABR", 0, 6000}, + Running{"obs_x264", "CRF", 23, 0}, + Running{"obs_x264", "CQP", 23, 0}, + Running{"obs_x264", "CBR", 0, 6000, true, 12000}, + Running{"obs_x264", "VBR", 23, 6000, true, 12000}, + Running{"obs_x264", "CBR", 0, 0, true, 0}, + Running{"obs_x264", "VBR", 23, 0, true, 0}, + Running{"obs_x264", "ABR", 0, 0, true, 0}, + Running{"obs_x264", "VBR", 23, 6000, false, 0}, + Running{"obs_nvenc_h264_tex", "CQP", 0, 0}, + Running{"obs_nvenc_h264_tex", "CQVBR", 0, 7000}, + Running{"obs_nvenc_h264_tex", "VBR", 0, 7000}, + Running{"obs_nvenc_h264_tex", "CBR", 0, 6000}, + Running{"obs_nvenc_h264_tex", "lossless", 0, 0}, + Running{"obs_qsv11_v2", "ICQ", 0, 0}, + Running{"obs_qsv11_v2", "VBR", 0, 0}, + Running{"obs_qsv11_v2", "CBR", 0, 6000}}) { + obs_data_t running; + obs_data_set_string(&running, "rate_control", c.mode); + obs_data_set_int(&running, "bitrate", 6000); + obs_data_set_int(&running, "max_bitrate", 7000); + obs_data_set_int(&running, "crf", c.crf); + obs_data_set_bool(&running, "use_bufsize", c.use_bufsize); + obs_data_set_int(&running, "buffer_size", c.buffer_size); + assert(RunningCapKbps(c.id, &running) == c.expect); + } + + // NVENC's opts can replace its rates, and are reported rather than rewritten. + obs_data_t nvenc_opts; + obs_data_set_string(&nvenc_opts, "opts", "maxBitRate=20000 lookaheadLevel=1 vbvBufferSize=0"); + assert((RateOptions("obs_nvenc_h264_tex", &nvenc_opts) == + std::vector{"maxBitRate=20000", "vbvBufferSize=0"})); + assert(RateOptions("obs_qsv11_v2", &nvenc_opts).empty()); + + // x264 runs the user's CBR or VBR at the cap, and anything else as VBR. A buffer of + // its own is replaced. x264opts is never rewritten: the options in it that set a rate + // are reported as overriding the cap, and the rest are not. + struct Case { + const char *before; + const char *after; + }; + for (const Case &c : {Case{"CBR", "CBR"}, Case{"cbr", "cbr"}, Case{"VBR", "VBR"}, Case{"CRF", "VBR"}, + Case{"ABR", "VBR"}, Case{"CQP", "VBR"}, Case{"", "VBR"}}) { + obs_data_t user, patch; + obs_data_set_string(&user, "rate_control", c.before); + obs_data_set_int(&user, "bitrate", 6000); + obs_data_set_int(&user, "buffer_size", 20000); + obs_data_set_int(&user, "crf", 23); + obs_data_set_bool(&user, "use_bufsize", true); + obs_data_set_string(&user, "x264opts", + "tune=film vbv-bufsize=0 vbv_maxrate=0 qp=20 bitrate=1 crf=20 crf=0"); + assert(ConfiguredCapKbps("obs_x264", &user) == 6000); + const MoQCap cap = EnforceCap("obs_x264", &user, &patch, 6000); + assert(cap.changed); + const std::string after = + patch.values.count("rate_control") ? obs_data_get_string(&patch, "rate_control") : c.before; + assert(after == c.after); + // The bitrate is already the cap; the buffer and the options are not. + assert(!patch.values.count("bitrate")); + assert(!obs_data_get_bool(&patch, "use_bufsize")); + assert(!patch.values.count("x264opts")); + assert((cap.overridden == + std::vector{"vbv-bufsize=0", "vbv_maxrate=0", "qp=20", "bitrate=1", "crf=0"})); + // The buffer, plus the mode when it changed. + assert(cap.replaced.size() == 1 + (std::string(c.before) != c.after && *c.before ? 1 : 0)); + } + // A lossless CRF setting is a constant QP with no VBV. + obs_data_t lossless, lossless_patch; + obs_data_set_string(&lossless, "rate_control", "CRF"); + obs_data_set_int(&lossless, "bitrate", 6000); + obs_data_set_int(&lossless, "crf", 0); + const MoQCap crf0 = EnforceCap("obs_x264", &lossless, &lossless_patch, 6000); + assert(crf0.changed && obs_data_get_int(&lossless_patch, "crf") == 23); + // CBR ignores the setting, and only a zero option is lossless; x264opts is left alone. + obs_data_t cbr_crf, cbr_crf_patch; + obs_data_set_string(&cbr_crf, "rate_control", "CBR"); + obs_data_set_int(&cbr_crf, "bitrate", 6000); + obs_data_set_int(&cbr_crf, "crf", 0); + obs_data_set_string(&cbr_crf, "x264opts", "crf=0.0 crf=18"); + EnforceCap("obs_x264", &cbr_crf, &cbr_crf_patch, 6000); + assert(!cbr_crf_patch.values.count("crf")); + assert(!cbr_crf_patch.values.count("x264opts")); + assert(RateOptions("obs_x264", &cbr_crf) == std::vector{"crf=0.0"}); + + // A compliant encoder is not touched. + obs_data_t clean, nothing; + obs_data_set_string(&clean, "rate_control", "VBR"); + obs_data_set_int(&clean, "crf", 23); + obs_data_set_int(&clean, "bitrate", 6000); + obs_data_set_string(&clean, "x264opts", "tune=film crf=20"); + const MoQCap untouched = EnforceCap("obs_x264", &clean, ¬hing, 6000); + assert(!untouched.changed && untouched.replaced.empty() && untouched.overridden.empty() && + nothing.values.empty()); + + for (const char *id : {"obs_nvenc_h264_tex", "obs_nvenc_hevc_soft", "obs_nvenc_av1_tex"}) { + obs_data_t nvenc; + assert(ApplyEncoderRate(id, &nvenc, 2500)); + assert(rate_control(nvenc) == "CQVBR" && obs_data_get_int(&nvenc, "max_bitrate") == 2500); + // The peak is max_bitrate: CQVBR ignores the bitrate. + const MoQPeak peak = EncoderPeak(id, &nvenc); + assert(std::string(peak.key) == "max_bitrate" && peak.kbps == 2500); + } + + // An encoder whose quality mode carries no hard peak stays at CBR, which is its peak. + for (const char *id : {"obs_qsv11_v2", "obs_qsv11_av1", "h264_texture_amf", "ffmpeg_vaapi_tex", + "com.apple.videotoolbox.videoencoder.ave.avc", "ffmpeg_nvenc", "unknown"}) { + obs_data_t other; + assert(!ApplyEncoderRate(id, &other, 8000)); + assert(rate_control(other) == "CBR" && obs_data_get_int(&other, "bitrate") == 8000); + const MoQPeak peak = EncoderPeak(id, &other); + assert(std::string(peak.key) == "bitrate" && peak.kbps == 8000); + } + + // NVENC keeps CBR and VBR, and runs anything else as constant quality, under max_bitrate. + // Its own peak is the cap when it names one, and VBR's average stays under it. + obs_data_t vbr, vbr_patch; + obs_data_set_string(&vbr, "rate_control", "VBR"); + obs_data_set_int(&vbr, "bitrate", 9000); + obs_data_set_int(&vbr, "max_bitrate", 6000); + assert(ConfiguredCapKbps("obs_nvenc_h264_tex", &vbr) == 6000); + const MoQCap lowered = EnforceCap("obs_nvenc_h264_tex", &vbr, &vbr_patch, 6000); + assert(lowered.changed && lowered.replaced.size() == 1); + assert(obs_data_get_int(&vbr_patch, "bitrate") == 6000 && !vbr_patch.values.count("max_bitrate")); + assert(!vbr_patch.values.count("rate_control")); + // An unset peak is the bitrate. + obs_data_set_int(&vbr, "max_bitrate", 0); + assert(ConfiguredCapKbps("obs_nvenc_h264_tex", &vbr) == 9000); + obs_data_t vbr_unset_patch; + EnforceCap("obs_nvenc_h264_tex", &vbr, &vbr_unset_patch, 9000); + assert(obs_data_get_int(&vbr_unset_patch, "max_bitrate") == 9000 && !vbr_unset_patch.values.count("bitrate")); + obs_data_t cbr, cbr_patch; + obs_data_set_string(&cbr, "rate_control", "CBR"); + obs_data_set_int(&cbr, "bitrate", 5000); + obs_data_set_int(&cbr, "max_bitrate", 20000); + assert(ConfiguredCapKbps("obs_nvenc_h264_tex", &cbr) == 5000); + assert(!EnforceCap("obs_nvenc_h264_tex", &cbr, &cbr_patch, 5000).changed); + for (const char *mode : {"CQP", "lossless", ""}) { + obs_data_t user, patch; + obs_data_set_string(&user, "rate_control", mode); + obs_data_set_int(&user, "bitrate", 5000); + const MoQCap cap = EnforceCap("obs_nvenc_h264_tex", &user, &patch, 5000); + assert(cap.changed && std::string(obs_data_get_string(&patch, "rate_control")) == "CQVBR"); + assert(obs_data_get_int(&patch, "max_bitrate") == 5000); + } + + // An encoder with no quality mode that holds a peak runs CBR at the cap, whatever the user chose. + obs_data_t qsv, qsv_patch; + obs_data_set_string(&qsv, "rate_control", "ICQ"); + obs_data_set_int(&qsv, "bitrate", 3000); + const MoQCap cbr_cap = EnforceCap("obs_qsv11_v2", &qsv, &qsv_patch, 3000); + assert(cbr_cap.replaced.size() == 1 && std::string(obs_data_get_string(&qsv_patch, "rate_control")) == "CBR"); + assert(ConfiguredCapKbps("obs_qsv11_v2", &qsv) == 3000); + + // A settings with no bitrate names no cap. + obs_data_t bare; + assert(ConfiguredCapKbps("obs_x264", &bare) == 0); + + // What the settings hold decides the peak, whatever the encoder. + assert(std::string(EncoderPeakKey("obs_x264", "cbr")) == "bitrate"); + assert(std::string(EncoderPeakKey("obs_nvenc_h264_tex", "vbr")) == "max_bitrate"); + assert(std::string(EncoderPeakKey("obs_nvenc_h264_tex", "CBR")) == "bitrate"); + // x264's ABR is VBV-capped at its bitrate, so it has a peak that other encoders' ABR does not. + assert(std::string(EncoderPeakKey("obs_x264", "ABR")) == "bitrate"); + assert(std::string(EncoderPeakKey("obs_x264", "VBR")) == "bitrate"); + assert(!EncoderPeakKey("obs_qsv11_v2", "ABR")); + for (const char *mode : {"CRF", "CQP", "ICQ", "QVBR", "lossless", ""}) { + assert(!EncoderPeakKey("obs_x264", mode) && !EncoderPeakKey("obs_nvenc_h264_tex", mode)); + } + assert(!EncoderPeakKey("obs_qsv11_v2", "VBR")); + + // NVENC's plain VBR averages beside its peak; CQVBR and CBR have no average to keep. + obs_data_set_int(&vbr, "bitrate", 6000); + obs_data_set_int(&vbr, "max_bitrate", 8000); + const MoQPeak averaged = EncoderPeak("obs_nvenc_h264_tex", &vbr); + assert(averaged.kbps == 8000 && averaged.average_kbps == 6000); + assert(EncoderPeak("obs_x264", &x264).average_kbps == 0); + obs_data_t cq; + assert(ApplyEncoderRate("obs_nvenc_h264_tex", &cq, 2500)); + assert(EncoderPeak("obs_nvenc_h264_tex", &cq).average_kbps == 0); + + // A peak of 0 is none: NVENC reads it as the driver's own limit. + obs_data_t unbounded; + obs_data_set_string(&unbounded, "rate_control", "CQVBR"); + obs_data_set_int(&unbounded, "max_bitrate", 0); + assert(!EncoderPeak("obs_nvenc_h264_tex", &unbounded).key); + + printf("encoder rate: ok\n"); + return 0; +} diff --git a/cpp/obs/test/moq-output-test.cpp b/cpp/obs/test/moq-output-test.cpp index b883589d693..6a51a0e71f1 100644 --- a/cpp/obs/test/moq-output-test.cpp +++ b/cpp/obs/test/moq-output-test.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -55,11 +56,21 @@ std::atomic g_destroyed{false}; std::string g_url; std::string g_rate_control = "CBR"; +std::string g_encoder_id = "obs_x264"; uint32_t g_encoder_caps = OBS_ENCODER_CAP_DYN_BITRATE; -// Every obs_encoder_update, as the encoder and the bitrate it was told in kbps. +// Every obs_encoder_update, as the encoder, the setting, and the kbps it was told. +using Update = std::tuple; std::mutex g_updates_mutex; -std::vector> g_updates; +std::vector g_updates; std::atomic g_settings_ok{true}; +// The encoders are already running, as when a recording that started first shares them. +std::atomic g_encoder_active{false}; +// The x264 CRF and "use_bufsize" the settings report; a negative CRF is the stub's default. +std::atomic g_crf{-1}; +std::atomic g_use_bufsize{false}; +std::atomic g_buffer_size{-1}; +// How many times Start() or a retune rewrote an encoder's settings. +std::atomic g_update_calls{0}; // A reason Configure reports itself, for a setting the config cannot express. std::string g_settings_error; // Lets a test run something inside obs_output_signal_stop, standing in for a @@ -175,6 +186,11 @@ bool obs_encoder_get_extra_data(const obs_encoder_t *encoder, uint8_t **data, si return true; } +const char *obs_encoder_get_id(const obs_encoder_t *) +{ + return g_encoder_id.c_str(); +} + const char *obs_encoder_get_codec(const obs_encoder_t *encoder) { return encoder == AUDIO_ENCODER ? "opus" : "h264"; @@ -203,26 +219,49 @@ const char *obs_data_get_string(obs_data_t *, const char *) return g_rate_control.c_str(); } -long long obs_data_get_int(obs_data_t *settings, const char *) +long long obs_data_get_int(obs_data_t *settings, const char *key) { + if (g_crf >= 0 && std::string(key) == "crf") + return g_crf; + if (g_buffer_size >= 0 && std::string(key) == "buffer_size") + return g_buffer_size; if (settings == reinterpret_cast(LOW_ENCODER)) return 2000; return settings == reinterpret_cast(MID_ENCODER) ? 4000 : 8000; } -// The settings a retune builds carry only the bitrate, so the stub is that bitrate. +// The settings a retune builds carry only its rates, so the stub is those settings. Start() +// also builds a patch that caps the encoder, whose rate mode lands on the stub encoder. struct obs_data { - long long bitrate = 0; + std::vector> rates; + std::vector> strings; }; +bool obs_data_get_bool(obs_data_t *, const char *key) +{ + return g_use_bufsize && std::string(key) == "use_bufsize"; +} + +bool obs_encoder_active(const obs_encoder_t *) +{ + return g_encoder_active; +} + +void obs_data_set_bool(obs_data_t *, const char *, bool) {} + +void obs_data_set_string(obs_data_t *data, const char *key, const char *value) +{ + data->strings.emplace_back(key, value); +} + obs_data_t *obs_data_create() { return new obs_data; } -void obs_data_set_int(obs_data_t *data, const char *, long long value) +void obs_data_set_int(obs_data_t *data, const char *key, long long value) { - data->bitrate = value; + data->rates.emplace_back(key, value); } uint32_t obs_encoder_get_caps(const obs_encoder_t *) @@ -232,8 +271,14 @@ uint32_t obs_encoder_get_caps(const obs_encoder_t *) void obs_encoder_update(obs_encoder_t *encoder, obs_data_t *settings) { + g_update_calls++; std::lock_guard lock(g_updates_mutex); - g_updates.emplace_back(encoder, settings->bitrate); + for (const auto &[key, kbps] : settings->rates) + g_updates.emplace_back(encoder, key, kbps); + for (const auto &[key, value] : settings->strings) { + if (key == "rate_control") + g_rate_control = value; + } delete settings; } @@ -318,6 +363,12 @@ void reset(std::string url) g_settings_ok = true; g_settings_error.clear(); g_rate_control = "CBR"; + g_encoder_active = false; + g_crf = -1; + g_use_bufsize = false; + g_buffer_size = -1; + g_update_calls = 0; + g_encoder_id = "obs_x264"; g_encoder_caps = OBS_ENCODER_CAP_DYN_BITRATE; g_ladder = false; g_mid = false; @@ -504,7 +555,7 @@ struct MoQOutputStatsTest { } }; -std::vector> updates() +std::vector updates() { std::lock_guard lock(g_updates_mutex); return g_updates; @@ -578,15 +629,29 @@ int main() printf("retired stats never replace the accepted sample: ok\n"); // Publishes over a real session: connects, reports itself live, carries - // stats, and lands video and audio renditions in the relay's catalog. Only CBR - // publishes the configured bitrate as a hint. - for (const char *mode : {"CBR", "VBR"}) { + // stats, and lands video and audio renditions in the relay's catalog. The output + // owns the cap: whatever rate control the user chose, it leaves the encoder holding a + // hard peak (x264's and NVENC's capped VBR, CBR elsewhere) and the catalog prices the + // rendition at that peak. + struct HintCase { + const char *id; + const char *mode; + const char *held; + }; + for (const auto &c : + {HintCase{"obs_x264", "CBR", "CBR"}, HintCase{"obs_x264", "VBR", "VBR"}, + HintCase{"obs_x264", "CRF", "VBR"}, HintCase{"obs_x264", "ABR", "VBR"}, + HintCase{"obs_nvenc_h264_tex", "CQVBR", "CQVBR"}, HintCase{"obs_nvenc_h264_tex", "VBR", "VBR"}, + HintCase{"obs_nvenc_h264_tex", "CBR", "CBR"}, HintCase{"obs_nvenc_h264_tex", "CQP", "CQVBR"}, + HintCase{"obs_qsv11_v2", "VBR", "CBR"}, HintCase{"obs_qsv11_v2", "ICQ", "CBR"}}) { TestRelay relay; reset(relay.Url()); - g_rate_control = mode; + g_encoder_id = c.id; + g_rate_control = c.mode; { MoQOutput o(nullptr, OUTPUT); CHECK(o.Start()); + CHECK(g_rate_control == c.held); CHECK(WaitFor([&] { return o.IsLiveSession(); })); CHECK(o.GetConnectTime() > 0); CHECK(o.GetReconnectCount() == 0); @@ -613,7 +678,8 @@ int main() CHECK(catalog->audio.size() == 1); for (const auto &[name, video] : catalog->video) { CHECK(video.coded.has_value()); - CHECK(video.bitrate.has_value() == (g_rate_control == "CBR")); + CHECK(video.bitrate.has_value()); + CHECK(video.bitrate.value_or(0) == 8'000'000); } } @@ -626,7 +692,7 @@ int main() // The destructor stops too, which OBS ignores for an output already stopped. CHECK(signalCount() == 2); } - printf("publishes, and only CBR hints its bitrate: ok\n"); + printf("publishes, and hints the cap it set on its encoder: ok\n"); // OBS reads the byte total as this stream's, so a stopped output that starts // again counts from zero. @@ -648,18 +714,25 @@ int main() } printf("a restart counts bytes from zero: ok\n"); - // Video follows the grant only when the encoder retunes live and the bitrate - // drives it; every track reserves either way once the session connects. + // Video follows the grant when the encoder retunes live: the output made every + // rate control one the bitrate drives. Every track reserves either way once the + // session connects. struct FollowCase { + const char *id; const char *rate_control; uint32_t caps; bool follows; }; - for (const auto &c : {FollowCase{"CBR", OBS_ENCODER_CAP_DYN_BITRATE, true}, - FollowCase{"vbr", OBS_ENCODER_CAP_DYN_BITRATE, true}, - FollowCase{"CQP", OBS_ENCODER_CAP_DYN_BITRATE, false}, FollowCase{"CBR", 0, false}}) { + for (const auto &c : {FollowCase{"obs_x264", "CBR", OBS_ENCODER_CAP_DYN_BITRATE, true}, + FollowCase{"obs_x264", "vbr", OBS_ENCODER_CAP_DYN_BITRATE, true}, + FollowCase{"obs_nvenc_h264_tex", "CQVBR", OBS_ENCODER_CAP_DYN_BITRATE, true}, + FollowCase{"obs_qsv11_v2", "vbr", OBS_ENCODER_CAP_DYN_BITRATE, true}, + FollowCase{"obs_x264", "CQP", OBS_ENCODER_CAP_DYN_BITRATE, true}, + FollowCase{"obs_nvenc_h264_tex", "CQP", OBS_ENCODER_CAP_DYN_BITRATE, true}, + FollowCase{"obs_x264", "CBR", 0, false}}) { TestRelay relay; reset(relay.Url()); + g_encoder_id = c.id; g_rate_control = c.rate_control; g_encoder_caps = c.caps; MoQOutput o(nullptr, OUTPUT); @@ -682,7 +755,60 @@ int main() } o.Stop(); } - printf("video follows only a live-retunable, bitrate-driven encoder: ok\n"); + printf("video follows only a live-retunable encoder: ok\n"); + + // An encoder already running (shared with a recording that started first) cannot switch + // rate control, so its settings are left as they are: a cap it runs with is advertised, + // and one it runs without refuses the output with a reason. + { + struct ActiveCase { + const char *id; + const char *rate_control; + long long crf; + bool use_bufsize; + // The custom buffer in kbit, or -1 for the stub's own. + long long buffer_size; + bool active; + bool starts; + // Whether Start() rewrites the encoder's settings. + bool rewrites; + }; + for (const auto &c : + {// Not running yet: today's behavior, the buffer and the lossless CRF are replaced. + ActiveCase{"obs_x264", "VBR", 23, true, -1, false, true, true}, + ActiveCase{"obs_x264", "VBR", 0, false, -1, false, true, true}, + ActiveCase{"obs_x264", "CBR", 0, true, 0, false, true, true}, + // Running with a cap: kept as it is, even with a buffer of its own. + ActiveCase{"obs_x264", "VBR", 23, true, -1, true, true, false}, + ActiveCase{"obs_x264", "ABR", 0, false, -1, true, true, false}, + ActiveCase{"obs_x264", "CBR", 0, false, -1, true, true, false}, + ActiveCase{"obs_nvenc_h264_tex", "CQVBR", -1, false, -1, true, true, false}, + // Running without one: refused. + ActiveCase{"obs_x264", "CQP", -1, false, -1, true, false, false}, + ActiveCase{"obs_x264", "VBR", 0, false, -1, true, false, false}, + ActiveCase{"obs_x264", "VBR", 23, true, 0, true, false, false}, + ActiveCase{"obs_x264", "ABR", 0, true, 0, true, false, false}, + ActiveCase{"obs_x264", "CBR", 0, true, 0, true, false, false}, + ActiveCase{"obs_nvenc_h264_tex", "CQP", -1, false, -1, true, false, false}}) { + TestRelay relay; + reset(relay.Url()); + g_encoder_id = c.id; + g_rate_control = c.rate_control; + g_crf = c.crf; + g_use_bufsize = c.use_bufsize; + g_buffer_size = c.buffer_size; + g_encoder_active = c.active; + MoQOutput o(nullptr, OUTPUT); + CHECK(o.Start() == c.starts); + CHECK((g_update_calls > 0) == c.rewrites); + { + std::lock_guard lock(g_signals_mutex); + CHECK(g_last_error.empty() == c.starts); + } + o.Stop(); + } + } + printf("a running encoder keeps its settings and must hold a cap: ok\n"); // Stopping hands the configured bitrate back to an encoder the grant lowered, // so the next Start() reads the user's rate rather than the last grant. @@ -703,10 +829,61 @@ int main() o.Stop(); const auto applied = updates(); CHECK(!applied.empty()); - CHECK(!applied.empty() && applied.back() == std::make_pair(VIDEO_ENCODER, 8000LL)); + CHECK(!applied.empty() && applied.back() == (Update{VIDEO_ENCODER, "bitrate", 8000})); } printf("stop restores the configured bitrate: ok\n"); + // Capped VBR moves its peak: NVENC's constant quality ignores "bitrate" and holds + // its ceiling in "max_bitrate", for the grant and for the restore alike. + { + TestRelay relay; + reset(relay.Url()); + g_encoder_id = "obs_nvenc_h264_tex"; + g_rate_control = "CQVBR"; + MoQOutput o(nullptr, OUTPUT); + CHECK(o.Start()); + CHECK(WaitFor([&] { return o.IsLiveSession(); })); + auto keyframe = TestH264Keyframe(); + auto video = videoPacket(keyframe, 0); + o.Data(&video); + MoQOutputStatsTest::Retarget(o, VIDEO_ENCODER, moq::RateTarget::kEnabled{800'000}); + o.Stop(); + const auto applied = updates(); + CHECK(applied.size() >= 2); + if (applied.size() >= 2) { + CHECK(applied[applied.size() - 2] == (Update{VIDEO_ENCODER, "max_bitrate", 800})); + CHECK(applied.back() == (Update{VIDEO_ENCODER, "max_bitrate", 8000})); + } + } + printf("capped VBR retunes its peak setting: ok\n"); + + // NVENC's plain VBR also averages at "bitrate", which must not exceed the lowered peak + // or the encoder rejects the pair, so the grant and the restore move both. + { + TestRelay relay; + reset(relay.Url()); + g_encoder_id = "obs_nvenc_h264_tex"; + g_rate_control = "VBR"; + MoQOutput o(nullptr, OUTPUT); + CHECK(o.Start()); + CHECK(WaitFor([&] { return o.IsLiveSession(); })); + auto keyframe = TestH264Keyframe(); + auto video = videoPacket(keyframe, 0); + o.Data(&video); + MoQOutputStatsTest::Retarget(o, VIDEO_ENCODER, moq::RateTarget::kEnabled{800'000}); + o.Stop(); + const auto applied = updates(); + CHECK(applied.size() >= 4); + if (applied.size() >= 4) { + const auto tail = applied.size() - 4; + CHECK(applied[tail] == (Update{VIDEO_ENCODER, "max_bitrate", 800})); + CHECK(applied[tail + 1] == (Update{VIDEO_ENCODER, "bitrate", 800})); + CHECK(applied[tail + 2] == (Update{VIDEO_ENCODER, "max_bitrate", 8000})); + CHECK(applied[tail + 3] == (Update{VIDEO_ENCODER, "bitrate", 8000})); + } + } + printf("plain VBR retunes its average with its peak: ok\n"); + // Two video encoders form a ladder: the higher rung sits one priority below the // lower, and a disabled rung leaves the catalog disabled, dropping its packets // until a keyframe after the grant funds it again. No encoder follows here, so diff --git a/doc/bin/obs.md b/doc/bin/obs.md index ddf5c2bd888..c66899f29c5 100644 --- a/doc/bin/obs.md +++ b/doc/bin/obs.md @@ -40,19 +40,51 @@ settings** off. A keyframe interval shorter than 2 s stays. Both tune x264, NVENC, Quick Sync, AMF, VA-API, and VideoToolbox, and leave any other encoder's settings alone. -The video bitrate follows the connection's bandwidth estimate, with the -configured bitrate as the ceiling: it drops as soon as the uplink narrows and -climbs back over a few seconds. Audio holds its configured bitrate, and video -gets what is left. Only encoders that can retune while live, with CBR, VBR, or -ABR rate control, follow; CQP and CRF keep their setting. The dock's Stats tab -shows the current video target beside the configured rate. An encoder shared -with a recording follows too, so record with a separate encoder to keep the -recording at the full rate. +Every video encoder the MoQ output starts has a cap, and the catalog's `bitrate` +is that cap, never the measured rate, so viewers price the rendition by what it +can send. The cap is the encoder's bitrate setting (NVENC's `max_bitrate` when its +VBR or constant quality sets one). The plugin writes it into the encoder itself, so +it holds whatever else the settings say: + +- x264 keeps CBR and otherwise runs constant quality (VBR) under a VBV whose max + rate and buffer are the cap. A CRF of 0 and a buffer size of your own are + replaced. Your x264 options are never rewritten, so `vbv-maxrate`, + `vbv-bufsize`, `bitrate`, `qp`, or a lossless `crf=0` in them can override the + cap: the log warns, and the dock's Quality and Performance profiles hold it + without them. +- NVENC keeps CBR and VBR, and otherwise runs constant quality (CQVBR), with + `max_bitrate` at the cap and VBR's average under it. Its `opts` are never + rewritten either, so `maxBitRate`, `averageBitRate`, or `vbvBufferSize` in them + can override the cap, and the log warns. +- Every other encoder runs CBR at the cap: Quick Sync's ICQ and AMF's QVBR have + no hard peak, and VA-API's and VideoToolbox's quality modes have not been + checked. + +The log names each setting of yours it replaced. An encoder shared with a +recording takes the cap too, unless the recording started first: a running +encoder cannot switch rate control, so its settings stay as they are and the +catalog advertises the cap it runs with. If it runs without one (constant +quality such as CQP or CRF, lossless, or x264 with a buffer size of your own set +to 0, which turns its VBV off), the output refuses to start. Stop the +recording first, or give it its own encoder. + +The dock's Custom settings profiles start from +constant quality under the cap on x264 and NVENC: Quality caps at 8 Mbps and +Performance at 2.5 Mbps, which a complex scene reaches and a calm one stays well +under. **Use OBS Output settings** starts from the rate control OBS configured. + +The video bitrate follows the connection's bandwidth estimate, with the cap as +the ceiling: it drops as soon as the uplink narrows and climbs back over a few +seconds. Audio holds its configured bitrate, and video gets what is left. Only +encoders that can retune while live follow. The dock's Stats tab shows the +current video target beside the configured rate. An encoder shared with a +recording follows too, so record with a separate encoder to keep the recording +at the full rate. ## Simulcast renditions Every video encoder on OBS's stream output is its own rendition of the -broadcast, with its size, codec, and (under CBR) configured bitrate in the +broadcast, with its size, codec, and cap in the catalog, so `` picks between them. OBS adds encoders from its WHIP simulcast layer count, `Stream1` / `WHIPSimulcastTotalLayers` in the profile, whatever the service: `N` layers add `N - 1` copies of the stream encoder, diff --git a/quest/m1/video-rate/obs.md b/quest/m1/video-rate/obs.md index 6cf3554bc05..1b831be5fab 100644 --- a/quest/m1/video-rate/obs.md +++ b/quest/m1/video-rate/obs.md @@ -1,25 +1,52 @@ -# [S] OBS defaults to capped VBR +# [S] OBS runs capped VBR on VideoToolbox and VA-API ## Goal -The OBS plugin's dock profiles configure the user's encoder for capped VBR -(constant quality with the profile's bitrate as the peak) instead of CBR, the -catalog `bitrate` carries that peak for a VBR encoder instead of the measured -running maximum, and dynamic bitrate moves the peak. +The OBS plugin's dock profiles run VideoToolbox (macOS) and VA-API encoders at +constant quality under the profile's peak, where that peak is measured to hold, +instead of the CBR they fall back to today. ## Plan The epic's decisions apply: quality mode with a peak where honored, otherwise CBR. -Today `cpp/obs/src/moq-dock.cpp` sets `rate_control = "CBR"` with -2500 kbps, or 8000/2500 for the Quality/Performance profiles, and -`moq-output.cpp` supplies a catalog bitrate only for CBR, so a VBR stream -advertises its measured peak, which starts low on a calm opening scene. -Candidates: x264's `VBR` (CRF under a VBV max rate) and NVENC's `VBR` or `CQVBR` -with `max_bitrate`; check each encoder's settings keys and the -`OBS_ENCODER_CAP_DYN_BITRATE` path. The profile bitrates follow the -default cap formula (`Config::resolved_bitrate` in -`rs/moq-video/src/encode/encoder.rs`) for their resolution. +Done: the MoQ output owns the cap on every video encoder it starts (maintainer +decision on the PR). `EnforceCap` in `cpp/obs/src/moq-encoder-rate.h` writes it +before OBS initializes the encoder, replaces any conflicting user rate option +with a logged warning, and the catalog advertises exactly that cap: x264 keeps +CBR and otherwise runs `VBR` (CRF under a VBV whose max rate and buffer are the +bitrate; `x264opts` is never rewritten, and a rate option in it logs a warning that +recommends the dock profiles), NVENC keeps +CBR and VBR and otherwise runs `CQVBR` with `max_bitrate`, and every other +encoder runs CBR at the cap. The output prices and retunes that peak (NVENC +under `max_bitrate`). Quick Sync (ICQ has no max rate) and AMF (QVBR sets no +peak) stay CBR for good. An encoder already running when the output starts keeps its +settings and the catalog advertises the cap it runs with; one running uncapped (CQP, CRF, +lossless) refuses the output. + +Remaining, each needing hardware to measure that the peak holds: + +- VideoToolbox: `rate_control` `CRF` (Apple Silicon only) with `limit_bitrate`, + `max_bitrate` and `max_bitrate_window`, which becomes a `DataRateLimits` hint + that may exceed the cap; an Intel Mac falls back to ABR. `vt_update` only + re-applies on a `bitrate` change, so `SetBitrate` must write `bitrate` beside + `max_bitrate` and clamp it to the granted peak, as it does for NVENC's plain + VBR (`MoQPeak::average_kbps`); `Reset()` restores both. +- VA-API: `QVBR` where the driver lists it (`vaapi_device_rc_supported`), with + `maxrate` as the peak; check what the driver does with `bitrate` and whether + the encoder advertises `OBS_ENCODER_CAP_DYN_BITRATE` (it does not today). + +Whatever measures as not holding stays CBR. Decided by the maintainer: "Use +OBS Output settings" in Simple mode stays CBR, and every encoder keeps its own +default quality level (x264 CRF 23, NVENC target quality 20) until the VMAF +harness ([quest/m9/video-quality](/quest/m9/video-quality/README.md)) picks one. Extend `EnforceCap` and +`EncoderPeakKey` for the mode, and the retune and restore in `moq-output.cpp` for +the setting a grant writes. Public API: none. Wire: none. + +## Hosts + +- macos - VideoToolbox's rate limit can only be measured on a Mac +- linux - VA-API's quality mode depends on the driver, so it is measured on one diff --git a/sh/obs/unit.sh b/sh/obs/unit.sh index bfe89fbbd4b..14bb197fd73 100755 --- a/sh/obs/unit.sh +++ b/sh/obs/unit.sh @@ -64,7 +64,7 @@ for name in moq-audio-encoder moq-output moq-settings moq-source obs-moq; do -o "$out/$name-test" "${sources[@]}" "$out/moq.o" $moq_libs TSAN_OPTIONS="halt_on_error=1 suppressions=$PWD/test/tsan.supp" "$out/$name-test" done -for name in moq-quality-defaults moq-dock-stop moq-error moq-encoder-latency moq-dial moq-spark; do +for name in moq-quality-defaults moq-dock-stop moq-error moq-encoder-latency moq-encoder-rate moq-dial moq-spark; do "$cxx" "${flags[@]}" "${includes[@]}" \ -o "$out/$name-test" "test/$name-test.cpp" TSAN_OPTIONS="halt_on_error=1 suppressions=$PWD/test/tsan.supp" "$out/$name-test"