Skip to content

fix(av1): apply SVT-AV1 rate control through the svtav1-params option - #13901

Closed
Jia-ben00 wants to merge 1 commit into
obsproject:masterfrom
Jia-ben00:fix/svtav1-params-cbr
Closed

Jia-ben00 wants to merge 1 commit into
obsproject:masterfrom
Jia-ben00:fix/svtav1-params-cbr

Conversation

@Jia-ben00

Copy link
Copy Markdown

Fixes #13849

av1_update() built the SVT-AV1 private parameter dictionary and then handed it to av_opt_set_dict_val() under the name svtav1_opts. That is the name of the struct member the option is bound to, not the name of the option itself: FFmpeg registers the dictionary as svtav1-params.

{ "svtav1-params", "Set the SVT-AV1 configuration using a :-separated list of key=value parameters",
  OFFSET(svtav1_opts), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },

The lookup therefore never matched, the dictionary was silently dropped, and the return value was ignored.

That matters specifically for CBR. The SVT-AV1 CBR branch sets rc_min_rate but deliberately leaves rc_max_rate at 0, so libsvtav1 derives rate control from the codec context:

if (avctx->bit_rate) {
    param->target_bit_rate = avctx->bit_rate;
    if (avctx->rc_max_rate != avctx->bit_rate)
        param->rate_control_mode = 1;   /* VBR */
    else
        param->rate_control_mode = 2;   /* CBR */
}

With rc_max_rate (0) != bit_rate, mode 1 (VBR) is selected. The rc=2 entry in the dictionary was the mechanism intended to override that, so losing the dictionary left the encoder running VBR while the UI reported CBR.

Two more entries in the same dictionary only become reachable once the assignment actually works, so they are corrected here as well:

  • tbr was set to bitrate * 1000 (bits per second). SVT-AV1 parses an unsuffixed tbr as kilobits per second, and libsvtav1 already copies avctx->bit_rate into param->target_bit_rate before it applies the private dictionary. The entry is removed rather than rescaled - the target bitrate keeps arriving through AVCodecContext.
  • bias-pct is no longer registered by SVT-AV1 >= 2.0.0. The Windows dependency set for OBS 32.2.2 ships SVT-AV1 2.3.0, so this entry would now be rejected. It is removed.

The result of the assignment is no longer discarded: a failed assignment is logged as a warning instead of silently leaving the encoder on the wrong rate control mode.

Verification

  • The option name was confirmed against FFmpeg source directly, on both master and release/8.1: the AVOption is svtav1-params, and its OFFSET() points at the svtav1_opts member. svtav1_opts is not itself an AVOption name.
  • The rc_max_rate / bit_rate derivation above was read from the same file (libavcodec/libsvtav1.c).
  • The change is a string-literal correction plus two dictionary entries removed; no API or control flow is added.

Not covered

There is no encoder-level test here. Confirming the change end to end needs a build with SVT-AV1 and a CBR encode with the output bitrate measured. I have not built OBS locally yet - if you want that confirmation before merging, tell me the build/test path you prefer and I will follow up. I am also happy to split the tbr / bias-pct removals into a separate PR if you would rather take the option-name fix on its own.

av1_update() built the SVT-AV1 private parameter dictionary and then handed
it to av_opt_set_dict_val() under the name "svtav1_opts". That is the name of
the struct member the option is bound to, not the name of the option itself:
FFmpeg registers the dictionary as "svtav1-params".

  { "svtav1-params", "Set the SVT-AV1 configuration using a :-separated list
    of key=value parameters", OFFSET(svtav1_opts), AV_OPT_TYPE_DICT, { 0 },
    0, 0, VE },

The lookup therefore never matched, the dictionary was silently dropped, and
the return value was ignored.

That matters specifically for CBR. The SVT-AV1 CBR branch sets rc_min_rate
but deliberately leaves rc_max_rate at 0, so libsvtav1 derives rate control
from the codec context, and with rc_max_rate (0) != bit_rate it selects mode
1 (VBR). The rc=2 entry in the dictionary was the mechanism intended to
override that, so losing the dictionary left the encoder running VBR while
the UI reported CBR.

Two more entries only become reachable once the assignment actually works,
so they are corrected here as well:

* tbr was set to bitrate * 1000 (bits per second). SVT-AV1 parses an
  unsuffixed tbr as kilobits per second, and libsvtav1 already copies
  avctx->bit_rate into param->target_bit_rate before applying the private
  dictionary. The entry is removed rather than rescaled.
* bias-pct is no longer registered by SVT-AV1 >= 2.0.0. The Windows
  dependency set for OBS 32.2.2 ships SVT-AV1 2.3.0, so this entry would now
  be rejected. It is removed.

The result of the assignment is no longer discarded: a failed assignment is
logged as a warning instead of silently leaving the encoder on the wrong
rate control mode.

Fixes obsproject#13849
@Jia-ben00

Copy link
Copy Markdown
Author

Verification notes

I checked this against the two CI gates this change can trip, and against the upstream sources the reasoning depends on.

Format — clang-format is pinned to major version 22 in .github/actions/run-clang-format (clang-format@22), and the repo config sets ColumnLimit: 120, UseTab: ForContinuationAndIndentation, IndentWidth: 8. Running clang-format 22.1.8 with the repo's .clang-format over the patched file produces output identical to the patch, byte for byte. The upstream file is already clean under the same check, so this change introduces no formatting delta. Longest added line is 107 columns.

Only Format and Build apply here — compatibility-validation and services-validation are globbed to plugins/win-capture/data/*.json and plugins/rtmp-services/data/*.json, neither of which is touched.

Root cause — the two halves of the mechanism, read from FFmpeg master:

  • libavcodec/libsvtav1.c:74 declares AVDictionary *svtav1_opts;
  • libsvtav1.c:830 registers { "svtav1-params", ..., OFFSET(svtav1_opts), AV_OPT_TYPE_DICT, ... }
  • libsvtav1.c:348 consumes it via av_dict_iterate(svt_enc->svtav1_opts, en) — i.e. straight off the struct member

So the member and the option name are two different things, and av_opt_set_dict_val() matches on the name. "svtav1_opts" is not in the option table's name column, the lookup misses, the member stays NULL, and the while loop at :348 never executes. Nothing is logged, because the return value was discarded.

For the CBR consequence, libsvtav1.c:240-245:

if (avctx->bit_rate) {
    param->target_bit_rate      = avctx->bit_rate;
    if (avctx->rc_max_rate != avctx->bit_rate)
        param->rate_control_mode = 1;   /* VBR */
    else
        param->rate_control_mode = 2;   /* CBR */
}

OBS's CBR branch sets rc_min_rate and intentionally leaves rc_max_rate at 0, so 0 != bit_rate holds and mode 1 (VBR) is chosen. rc=2 in the dictionary was the override for exactly that; with the dictionary dropped, the override is gone.

Model — I wrote a small executable model of the two behaviours (option-name lookup + the :240 derivation) so the reasoning can be run rather than just asserted. It reports, BEFORE: av_opt_set_dict_val returns AVERROR_OPTION_NOT_FOUND, member NULL, 0 params applied, effective mode 1 (VBR); AFTER: returns 0, member set, {rc: 2, pred-struct: 1} applied, effective mode 2 (CBR). Happy to push it into this PR if it would be useful for review.

Still not done — no OBS build here, so no end-to-end encode. That needs CMake, Qt6 and the dependency set; if you would like that before merging, say so and I will set it up.

@derrod

derrod commented Sep 15, 2026

Copy link
Copy Markdown
Member

slop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SVT-AV1 CBR parameters are not applied because OBS uses the wrong FFmpeg AVOption name

2 participants