feat(libwebrtc): implement the WebRtcOpus encoder functions #13736

Merged
2Swagger merged 1 commit from upstream-pr/13736 into main 2026-10-31 21:53:12 +00:00
2Swagger commented 2026-10-31 03:23:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 2Swagger on 2026-10-31T03:23:08Z.


Stacked on #13692 (which includes #13687 and #13689): this diff includes their commits until they merge. This PR's own change is commit 9b9f9bca7.

What

Implements the 16 WebRtcOpus_* encoder exports of libwebrtc, which threw and were declared without parameters: EncoderCreate, EncoderFree, Encode, SetBitRate, SetPacketLossRate, SetMaxPlaybackRate, EnableFec/DisableFec, EnableDtx/DisableDtx, EnableCbr/DisableCbr, SetComplexity, GetBandwidth, SetBandwidth and SetForceChannels.

They are adapted from WebRTC's opus_interface.cc and encode through the libopus the repo already builds (3rdparty/opus, linked like libSceOpusCeltEnc). #13687 places the module between M127 (branch-heads/6533) and M134 (branch-heads/6998); I compared both ends:

  • Signatures: the 16 declarations in opus_interface.h are identical at both revisions; include/Api.hpp now carries them, with the opaque WebRtcOpusEncInst from include/Opus.hpp.
  • Bodies: 14 are identical. EncoderCreate and Encode differ only in M127's WebRTC-Audio-OpusAvoidNoisePumpingDuringDtx field trial, which rewrites high-energy refresh DTX packets. Field trials are off unless webrtc::field_trial::InitFieldTrialsFromString turns them on, and that export still throws here, so both ends behave the same.
  • What each does: create maps application 0/1 to OPUS_APPLICATION_VOIP/AUDIO and returns -1 for anything else or for a libopus error; Encode returns -1 for a libopus error, returns the first packet of 2 bytes or less and then 0 while the encoder stays in DTX, as upstream does; the setters forward to opus_encoder_ctl (SET_BITRATE, SET_PACKET_LOSS_PERC, SET_MAX_BANDWIDTH from the Hz thresholds, SET_INBAND_FEC, SET_DTX, SET_VBR, SET_COMPLEXITY, SET_BANDWIDTH, SET_FORCE_CHANNELS) and return its result; every function returns -1 for a null encoder, as upstream does.
  • Throws: frames over 60 ms and up to 120 ms per channel, because upstream accepts them only when built with WEBRTC_OPUS_SUPPORT_120MS_PTIME, which is unknown for this module (over 120 ms returns -1 in both builds); and, as a host-side check, a null encoder or buffer in Encode, which upstream reads.
  • libopus version: the bundled libopus is 1.6.1, not WebRTC's pinned copy, so packet bytes can differ; the tests never compare bytes.

src/Opus.cpp starts with the WebRTC copyright notice of opus_interface.cc and the full BSD LICENSE text, both verbatim from 8d78f5de6c (identical at e0b28a6a81), in the same format as #13689's src/ExpFilter.cpp and #13692's src/RtcpCommonHeader.cpp. Opus.cpp adapts about 100 lines of WebRTC's opus_interface.cc wrapper (with its notice and BSD licence verbatim), the same way #13088 adapted LibreSSL's stack.c; libopus itself is the existing 3rdparty/opus submodule. The debt file records the source, the field-trial reasoning, the 60/120 ms gap and the libopus difference, and the inventory drops from 408 to 392.

Tested

  • New guest_webrtc_opus, through the bundled libopus:
    • Round trip: a mono VOIP encoder at 32 kbps and complexity 5 encodes 50 frames of a 440 Hz tone; every packet decodes with opus_decode, and after the codec delay the output correlates above 0.9 with the input at a gain between 0.7 and 1.3.
    • Settings: with CBR, every 20 ms packet is exactly 40, 80 or 160 bytes at 16, 32 and 64 kbps, and without it the sizes vary; SetForceChannels(1) makes a stereo encoder emit mono packets and 0 brings stereo back, while 3 returns -1; with DTX on, silence returns one packet of 2 bytes or less, then 0 while silent, and noise brings packets back; SetMaxPlaybackRate(8000) gives narrowband and SetBandwidth wideband, as GetBandwidth reports; FEC with 20 % expected loss adds LBRR data, and after DisableFec it is gone.
    • Errors: application 2, 44.1 kHz and 3 channels fail without touching the output pointer; every function returns -1 for a null encoder; out-of-range complexity, bitrate, loss and bandwidth return libopus's OPUS_BAD_ARG; Encode returns -1 above 120 ms, for an invalid frame size and for a zero-length buffer, and encodes a 60 ms frame. Under 0.2 s.
    • Each of four deliberate mistakes (CBR enabling VBR, DTX returning the packet size while in DTX, SetForceChannels ignoring its argument, the 8 kHz cap mapped to wideband) fails the test. With the previous throwing stubs the test does not build, since they had no parameters.
  • Head 9b9f9bca7, on #13692 at 42087bf11, merged onto main 014c19ab19. Windows 11 (WinLibs GCC 15.2.0, Release): guest_webrtc_opus, the chain's guest_webrtc_fast_int_to_buffer, guest_webrtc_exp_filter and guest_webrtc_rtcp_common_header, and public_headers with its compile batches pass, 69 of 69. Linux (Ubuntu 24.04 under WSL2, GCC 13.3.0, Release): the same set, 69 of 69.
  • guest_webrtc_opus passed 20 consecutive runs on both platforms (ctest --repeat until-fail:20).
  • Conventions and test registration pass. Not tested on a console or a title; no title is known to import these functions.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #13687, #13689, #13692
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5094056156 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13736). Originally posted by [2Swagger](https://github.com/2Swagger) on 2026-10-31T03:23:08Z. --- Stacked on #13692 (which includes #13687 and #13689): this diff includes their commits until they merge. This PR's own change is commit 9b9f9bca7. ### What Implements the 16 `WebRtcOpus_*` encoder exports of libwebrtc, which threw and were declared without parameters: `EncoderCreate`, `EncoderFree`, `Encode`, `SetBitRate`, `SetPacketLossRate`, `SetMaxPlaybackRate`, `EnableFec`/`DisableFec`, `EnableDtx`/`DisableDtx`, `EnableCbr`/`DisableCbr`, `SetComplexity`, `GetBandwidth`, `SetBandwidth` and `SetForceChannels`. They are adapted from WebRTC's [`opus_interface.cc`](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100/modules/audio_coding/codecs/opus/opus_interface.cc) and encode through the libopus the repo already builds (`3rdparty/opus`, linked like `libSceOpusCeltEnc`). #13687 places the module between M127 ([branch-heads/6533](https://webrtc.googlesource.com/src/+/e0b28a6a81a989c1f5c89e30fcd247870047390d)) and M134 ([branch-heads/6998](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100)); I compared both ends: - **Signatures:** the 16 declarations in `opus_interface.h` are identical at both revisions; `include/Api.hpp` now carries them, with the opaque `WebRtcOpusEncInst` from `include/Opus.hpp`. - **Bodies:** 14 are identical. `EncoderCreate` and `Encode` differ only in M127's `WebRTC-Audio-OpusAvoidNoisePumpingDuringDtx` field trial, which rewrites high-energy refresh DTX packets. Field trials are off unless `webrtc::field_trial::InitFieldTrialsFromString` turns them on, and that export still throws here, so both ends behave the same. - **What each does:** create maps application 0/1 to `OPUS_APPLICATION_VOIP`/`AUDIO` and returns -1 for anything else or for a libopus error; `Encode` returns -1 for a libopus error, returns the first packet of 2 bytes or less and then 0 while the encoder stays in DTX, as upstream does; the setters forward to `opus_encoder_ctl` (`SET_BITRATE`, `SET_PACKET_LOSS_PERC`, `SET_MAX_BANDWIDTH` from the Hz thresholds, `SET_INBAND_FEC`, `SET_DTX`, `SET_VBR`, `SET_COMPLEXITY`, `SET_BANDWIDTH`, `SET_FORCE_CHANNELS`) and return its result; every function returns -1 for a null encoder, as upstream does. - **Throws:** frames over 60 ms and up to 120 ms per channel, because upstream accepts them only when built with `WEBRTC_OPUS_SUPPORT_120MS_PTIME`, which is unknown for this module (over 120 ms returns -1 in both builds); and, as a host-side check, a null encoder or buffer in `Encode`, which upstream reads. - **libopus version:** the bundled libopus is 1.6.1, not WebRTC's pinned copy, so packet bytes can differ; the tests never compare bytes. `src/Opus.cpp` starts with the WebRTC copyright notice of `opus_interface.cc` and the full BSD `LICENSE` text, both verbatim from [8d78f5de6c](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100/LICENSE) (identical at e0b28a6a81), in the same format as #13689's `src/ExpFilter.cpp` and #13692's `src/RtcpCommonHeader.cpp`. Opus.cpp adapts about 100 lines of WebRTC's opus_interface.cc wrapper (with its notice and BSD licence verbatim), the same way #13088 adapted LibreSSL's stack.c; libopus itself is the existing 3rdparty/opus submodule. The debt file records the source, the field-trial reasoning, the 60/120 ms gap and the libopus difference, and the inventory drops from 408 to 392. ### Tested - New `guest_webrtc_opus`, through the bundled libopus: - **Round trip:** a mono VOIP encoder at 32 kbps and complexity 5 encodes 50 frames of a 440 Hz tone; every packet decodes with `opus_decode`, and after the codec delay the output correlates above 0.9 with the input at a gain between 0.7 and 1.3. - **Settings:** with CBR, every 20 ms packet is exactly 40, 80 or 160 bytes at 16, 32 and 64 kbps, and without it the sizes vary; `SetForceChannels(1)` makes a stereo encoder emit mono packets and `0` brings stereo back, while 3 returns -1; with DTX on, silence returns one packet of 2 bytes or less, then 0 while silent, and noise brings packets back; `SetMaxPlaybackRate(8000)` gives narrowband and `SetBandwidth` wideband, as `GetBandwidth` reports; FEC with 20 % expected loss adds LBRR data, and after `DisableFec` it is gone. - **Errors:** application 2, 44.1 kHz and 3 channels fail without touching the output pointer; every function returns -1 for a null encoder; out-of-range complexity, bitrate, loss and bandwidth return libopus's `OPUS_BAD_ARG`; `Encode` returns -1 above 120 ms, for an invalid frame size and for a zero-length buffer, and encodes a 60 ms frame. Under 0.2 s. - Each of four deliberate mistakes (CBR enabling VBR, DTX returning the packet size while in DTX, `SetForceChannels` ignoring its argument, the 8 kHz cap mapped to wideband) fails the test. With the previous throwing stubs the test does not build, since they had no parameters. - Head 9b9f9bca7, on #13692 at 42087bf11, merged onto `main` 014c19ab19. Windows 11 (WinLibs GCC 15.2.0, Release): `guest_webrtc_opus`, the chain's `guest_webrtc_fast_int_to_buffer`, `guest_webrtc_exp_filter` and `guest_webrtc_rtcp_common_header`, and `public_headers` with its compile batches pass, 69 of 69. Linux (Ubuntu 24.04 under WSL2, GCC 13.3.0, Release): the same set, 69 of 69. - `guest_webrtc_opus` passed 20 consecutive runs on both platforms (`ctest --repeat until-fail:20`). - Conventions and test registration pass. Not tested on a console or a title; no title is known to import these functions. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #13687, #13689, #13692 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-31 03:46:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T03:46:24Z.

Review: APPROVED


Reviewed — looks good

I reviewed head 9b9f9bca74e3d8fcddee4cd7e0361ac485665299; the encoder implementation, declared stack, and reported cross-platform tests show no blocking issue.

What I ran

Reviewed head 9b9f9bca74e3d8fcddee4cd7e0361ac485665299 against PR base 6475267b456de6a0a23b230aa751f0b917d12688; the stack preview also checked current main de824c2919ca65086c6324a1e0388866a7327eea and merged cleanly. No local build or C++ test was run. Environment: Linux x86_64, GCC 12.2.0, Python 3.11.2.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13736
python3 tools/pr_abi_audit.py --repo /marc/AnyPS5 --base 6475267b456de6a0a23b230aa751f0b917d12688 --head 9b9f9bca74e3d8fcddee4cd7e0361ac485665299 --json | python3 -c 'import json,sys; d=json.load(sys.stdin); ss=[s for s in d["symbols"] if s["name"].startswith("WebRtcOpus_")]; fs=[f for f in d["findings"] if f.get("symbol","").startswith("WebRtcOpus_")]; print("audit_status:",d["status"]); print("WebRtcOpus symbols:",len(ss)); print("declaration/definition mismatches:",sum(len(s["declarations"]) != 1 or len(s["definitions"]) != 1 for s in ss)); print("WebRtcOpus findings:",len(fs))'
python3 tools/pr_test_quality.py --root /marc/AnyPS5 --json core/libs/prx/libwebrtc/tests/Opus.cpp core/libs/prx/libwebrtc/tests/ExpFilter.cpp core/libs/prx/libwebrtc/tests/RtcpCommonHeader.cpp core/libs/prx/libwebrtc/tests/FastIntToBuffer.cpp | python3 -c 'import json,sys; d=json.load(sys.stdin); print("heuristic findings:",len(d["findings"])); [print(f["path"],f["kind"]) for f in d["findings"]]; print("negative-control candidates:",len(d["negative_control_candidates"]))' 

Results

Check Result
No-build precheck Diff, conventions, and dependency preview passed; its ABI/test-quality subprocesses could not find those helpers in the isolated checkout. I reran both helpers against the workspace and inspected the CMake registration.
Dependency/mergeability Passed; #13687 → #13689 → #13692 → #13736, with each declared prerequisite head included. All previewed cleanly against current main.
ABI audit All 16 new WebRtcOpus_* declarations have one matching definition; no findings for those symbols. The audit reports unrelated explicit-NID alias warnings elsewhere in libwebrtc.
Test registration heuristic It flagged four new test paths, but the changed CMake file registers all four executables with add_test; this is a heuristic false positive.
Hosted Windows job Passed and built/linked libwebrtc.prx; its log did not show guest_webrtc_opus executing.
Author-reported focused tests On the exact head, Windows and Ubuntu/WSL2 each report 69/69 selected tests passing; guest_webrtc_opus reportedly passed 20 consecutive runs on both. The author also reports four deliberate test mutations failed as expected.
Unedited outputs from focused checks
All conventions checks passed.
audit_status: passed_with_warnings
WebRtcOpus symbols: 16
declaration/definition mismatches: 0
WebRtcOpus findings: 0
heuristic findings: 4
core/libs/prx/libwebrtc/tests/Opus.cpp possible-unregistered-cpp-test
core/libs/prx/libwebrtc/tests/ExpFilter.cpp possible-unregistered-cpp-test
core/libs/prx/libwebrtc/tests/RtcpCommonHeader.cpp possible-unregistered-cpp-test
core/libs/prx/libwebrtc/tests/FastIntToBuffer.cpp possible-unregistered-cpp-test
negative-control candidates: 0
/usr/bin/python3: can't open file '/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13736-9b9f9bca74e3/head-9b9f9bca74e3-9e7aae2d/tools/pr_abi_audit.py': [Errno 2] No such file or directory
/usr/bin/python3: can't open file '/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13736-9b9f9bca74e3/head-9b9f9bca74e3-9e7aae2d/tools/pr_test_quality.py': [Errno 2] No such file or directory
Conclusion: checks failed

Findings

Blocking: None found.

Non-blocking: Frames over 60 ms and up to 120 ms intentionally throw because the module's WEBRTC_OPUS_SUPPORT_120MS_PTIME setting is unknown; this limitation is documented in the PR's debt entry. The implementation uses the repository's bundled libopus 1.6.1, so byte-for-byte output parity with WebRTC's pinned libopus is not claimed.

Sources

Not verified

I did not independently compile or execute tests. Linux CI was skipped; the Linux and Windows focused runs and negative controls above are author-reported. No console or title behavior was tested.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5721006809 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13736#pullrequestreview-5721006809). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T03:46:24Z. **Review:** APPROVED --- # Reviewed — looks good I reviewed head `9b9f9bca74e3d8fcddee4cd7e0361ac485665299`; the encoder implementation, declared stack, and reported cross-platform tests show no blocking issue. ## What I ran Reviewed head `9b9f9bca74e3d8fcddee4cd7e0361ac485665299` against PR base `6475267b456de6a0a23b230aa751f0b917d12688`; the stack preview also checked current `main` `de824c2919ca65086c6324a1e0388866a7327eea` and merged cleanly. No local build or C++ test was run. Environment: Linux x86_64, GCC 12.2.0, Python 3.11.2. ```sh python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13736 python3 tools/pr_abi_audit.py --repo /marc/AnyPS5 --base 6475267b456de6a0a23b230aa751f0b917d12688 --head 9b9f9bca74e3d8fcddee4cd7e0361ac485665299 --json | python3 -c 'import json,sys; d=json.load(sys.stdin); ss=[s for s in d["symbols"] if s["name"].startswith("WebRtcOpus_")]; fs=[f for f in d["findings"] if f.get("symbol","").startswith("WebRtcOpus_")]; print("audit_status:",d["status"]); print("WebRtcOpus symbols:",len(ss)); print("declaration/definition mismatches:",sum(len(s["declarations"]) != 1 or len(s["definitions"]) != 1 for s in ss)); print("WebRtcOpus findings:",len(fs))' python3 tools/pr_test_quality.py --root /marc/AnyPS5 --json core/libs/prx/libwebrtc/tests/Opus.cpp core/libs/prx/libwebrtc/tests/ExpFilter.cpp core/libs/prx/libwebrtc/tests/RtcpCommonHeader.cpp core/libs/prx/libwebrtc/tests/FastIntToBuffer.cpp | python3 -c 'import json,sys; d=json.load(sys.stdin); print("heuristic findings:",len(d["findings"])); [print(f["path"],f["kind"]) for f in d["findings"]]; print("negative-control candidates:",len(d["negative_control_candidates"]))' ``` ## Results | Check | Result | |---|---| | No-build precheck | Diff, conventions, and dependency preview passed; its ABI/test-quality subprocesses could not find those helpers in the isolated checkout. I reran both helpers against the workspace and inspected the CMake registration. | | Dependency/mergeability | Passed; #13687 → #13689 → #13692 → #13736, with each declared prerequisite head included. All previewed cleanly against current `main`. | | ABI audit | All 16 new `WebRtcOpus_*` declarations have one matching definition; no findings for those symbols. The audit reports unrelated explicit-NID alias warnings elsewhere in libwebrtc. | | Test registration heuristic | It flagged four new test paths, but the changed CMake file registers all four executables with `add_test`; this is a heuristic false positive. | | Hosted Windows job | Passed and built/linked `libwebrtc.prx`; its log did not show `guest_webrtc_opus` executing. | | Author-reported focused tests | On the exact head, Windows and Ubuntu/WSL2 each report 69/69 selected tests passing; `guest_webrtc_opus` reportedly passed 20 consecutive runs on both. The author also reports four deliberate test mutations failed as expected. | <details> <summary>Unedited outputs from focused checks</summary> ```text All conventions checks passed. ``` ```text audit_status: passed_with_warnings WebRtcOpus symbols: 16 declaration/definition mismatches: 0 WebRtcOpus findings: 0 ``` ```text heuristic findings: 4 core/libs/prx/libwebrtc/tests/Opus.cpp possible-unregistered-cpp-test core/libs/prx/libwebrtc/tests/ExpFilter.cpp possible-unregistered-cpp-test core/libs/prx/libwebrtc/tests/RtcpCommonHeader.cpp possible-unregistered-cpp-test core/libs/prx/libwebrtc/tests/FastIntToBuffer.cpp possible-unregistered-cpp-test negative-control candidates: 0 ``` ```text /usr/bin/python3: can't open file '/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13736-9b9f9bca74e3/head-9b9f9bca74e3-9e7aae2d/tools/pr_abi_audit.py': [Errno 2] No such file or directory /usr/bin/python3: can't open file '/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13736-9b9f9bca74e3/head-9b9f9bca74e3-9e7aae2d/tools/pr_test_quality.py': [Errno 2] No such file or directory Conclusion: checks failed ``` </details> ## Findings **Blocking:** None found. **Non-blocking:** Frames over 60 ms and up to 120 ms intentionally throw because the module's `WEBRTC_OPUS_SUPPORT_120MS_PTIME` setting is unknown; this limitation is documented in the PR's debt entry. The implementation uses the repository's bundled libopus 1.6.1, so byte-for-byte output parity with WebRTC's pinned libopus is not claimed. ## Sources - [Encoder wrapper at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/9b9f9bca74e3d8fcddee4cd7e0361ac485665299/core/libs/prx/libwebrtc/src/Opus.cpp) - [Pinned WebRTC Opus interface implementation used as reference](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100/modules/audio_coding/codecs/opus/opus_interface.cc) - [PR's exact-head test report](https://github.com/boykopovar/AnyPS5/pull/13736) ## Not verified I did not independently compile or execute tests. Linux CI was skipped; the Linux and Windows focused runs and negative controls above are author-reported. No console or title behavior was tested.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!13736
No description provided.