Skip to content

perf: obs-media-io: SIMD format conversion (Rust is scalar where the C is SSE2) #3004

Open

Description

@zackees

What

rust/obs-media-io/src/format_conversion.rs:106-305 (compress_uyvx_to_i420, the NV12/I420/UYVY family) is scalar and goes through per-pixel Source::read/Sink::write. The C it replaces, libobs/media-io/format-conversion.c.h, is SSE2 (29 _mm_ uses).

  • Runs: every frame for CPU-side format conversion (async sources, CPU encoders).
  • Running today: the Rust (scalar), when the Rust ports are on.
  • Expected gain: high. This is the one place where the Rust port is known to be slower than the C it replaces.
  • Parity: integer math, so exact output is expected. A ULP tolerance is allowed anyway, because the C is SIMD.

Acceptance criteria

  • An SSE2/AVX2 path (x86_64) and a NEON path (aarch64) behind runtime detection, plus the scalar fallback.
  • rust/obs-media-io/tests/video/format_conversion_parity.rs passes on each path, forcing each one in tests.
  • The format benchmark (depends on sub-issue 1) shows Rust ≤ C on one named x86_64 and one named aarch64 machine.

Failing-test sketch

RED: a benchmark-bound test (or record.py comparison) that fails while Rust/C > 1.10 for format/uyvx_to_i420/1080p. A parity test that forces the SIMD path fails until that path exists.

Rules for this work (#756): portable stable Rust only (no std::simd/nightly), a scalar fallback with runtime CPU detection, ISA-specific code only in the crate's src/platform/ (Dylint ban_platform_cfg_outside_boundary). Tier 3 parity is bit-exact unless the C reference itself uses SIMD (docs/rust-port/testing-policy.md:296). RED then GREEN. Every kernel needs a benchmark in rust/tools/benchmarks/record.py first (sub-issue 1 of the meta issue).

Activity

  1. added
    enhancement
    P1
    size:L
    on Oct 30, 2026
  2. zackees commented on Oct 30, 2026

    @zackees
    MemberAuthor

    Triage: well defined (acceptance criteria and a failing-test sketch are in the body), but not Ready yet. It depends on #3003 (benchmark baseline in record.py), which is open. It becomes Ready once #3003 merges, and the baseline numbers decide whether this one is still worth doing.

  3. added
    P3
    and removed
    P1
    on Oct 30, 2026
  4. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Claiming: #3004 (SIMD format conversion in rust/obs-media-io)

    Plan (one kernel family per PR, RED then GREEN):

    1. RED: extend rust/obs-media-io/tests/video/format_conversion_parity.rs with a forced-path test — each plane conversion must produce the same bytes on the SIMD path and the scalar fallback, and the forcing hook does not exist yet, so it fails.
    2. GREEN: an x86_64 SSE2 path (runtime-detected, mirroring the C's _mm_* integer math) and the scalar fallback behind it for compress_uyvx_to_i420, compress_uyvx_to_nv12 and convert_uyvx_to_i444; ISA code only under rust/obs-media-io/src/platform/.
    3. Same PR: the format benchmark from perf(bench): Record the SIMD candidates and repair the benchmark recorder (#3003) #3029 (record.py) re-recorded before/after on this named host, plus a stated parity tolerance (exact — this is integer math).
    4. aarch64 NEON follows in this PR if it can be proven on an arm64 machine; otherwise it is a separate PR and the issue stays open.

    Master is green and there is no open PR against this area (checked gh pr list --search 3004).

    — worker tick

  5. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    /ready

    Triage: #3003 (the benchmark baseline) landed in #3029, so the dependency in the last triage is cleared. Acceptance criteria and the failing-test sketch are in the body and the format bench already exists.

  6. added
    ready
    on Oct 31, 2026
  7. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Releasing #3004 (SIMD format conversion in rust/obs-media-io) — measured, not shipped.

    I built the change and it is parity-clean, but it does not beat the scalar port here, so there is no PR to land. Work is parked as ~/dev/obs-rust/3004-simd-wip.patch (660 lines) for whoever picks this up.

    What I built

    • rust/obs-media-io/src/platform/uyvx_block.rs: the C's pack_shift/pack_val masks and shifts in SSE2 (x86_64) and NEON (aarch64), a scalar fallback with the same byte picks, runtime detection, and a test-only forcing hook.
    • compress_uyvx_to_i420, compress_uyvx_to_nv12 and convert_uyvx_to_i444 now pick a kernel once per call and inline it (generic BlockKernel, no per-block dispatch).
    • Parity: every forced path matches the C oracle byte for byte (uyvx_compress_kernels_match_c_on_every_path, plus the existing proptests, 5/5 green). RED, with the SIMD path stubbed out: assertion left != right failed: the host has no SIMD UYVX block kernel. cargo check --target aarch64-apple-darwin is clean, so the NEON path compiles; it has not run on an arm64 host.

    Why it was not pushed: the numbers
    record.py --category format --repeats 3 --cpu 15, master 88d49a917, same host, rust-over-C ratio (1080p):

    case before with the SSE2 block
    uyvx_to_i420 2.67 (2.63 ms) 3.52 (2.90 ms)
    uyvx_to_nv12 3.40 (2.74 ms) 5.98 (4.52 ms)
    uyvx_to_i444 1.98 (2.84 ms) 1.89 (1.95 ms)

    No win, and the host cannot settle it anyway: load average was 16-22 on 8 cores (four workers share this box), and the untouched C-side numbers moved 1.6x between the two runs (e.g. uyvx_to_nv12_c_scalar C 2.78 -> 4.53 ms), which is the documented 30-45% ratio drift and then some. Only the within-run ratio is comparable, and it did not improve.

    What I think blocks a real win
    The planes are Source/Sink byte-offset traits, so the kernel hands each 4-pixel block back as a 24-byte Block through memory, and the stores stay the C's 4-byte uint32_t writes. The C's advantage is that it keeps the two 16-byte rows in registers and stores 4/8 bytes straight into the plane. A faster port needs a row-slice view (Sink::bytes_mut, or a per-plane row cursor) so the loop can do the masks and the packus on whole 16-byte blocks and write 8/16-byte stores, plus a quiet host for the before/after.

    The issue's acceptance criteria also still need the format benchmark on a named x86_64 and arm64 machine — this box is not a usable measurement host while the worker fleet runs on it.

    No branch and no PR left behind; the checkout is back on detached origin/master with only .cache/ untracked. — worker tick

  8. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Claiming: #3004 (SIMD format conversion in rust/obs-media-io)

    Resuming the parked work (~/dev/obs-rust/3004-simd-wip.patch) with a different first step. That patch's per-block SSE2 kernel made uyvx_to_nv12 slower (3.40 -> 5.98 Rust/C) because the cost in these paths is not the lane arithmetic: it is the per-pixel/per-block Source::read copy and the 4-byte Sink::write stores, the same cost #3111 fixed on the decompress side with row slices.

    Plan, one PR:

    1. RED: a parity test that forces the block kernel reports "no SIMD kernel" while the SIMD path is stubbed, as the parked patch had. Keep the byte-exact C-oracle comparison as the gate.
    2. GREEN: pass the parked block kernel and remove the per-block copy (Source::bytes instead of read::<16>), then re-measure the three compress cases against the -O2 shipped-level oracle on this named host.
    3. Ship only what measures at or below the 1.10x bound; if the row-slice change wins and the SIMD kernel still loses, the PR carries the row-slice change alone and the SIMD work goes back to the parked patch with the numbers.

    aarch64 NEON stays in the same PR only if it can be proven on an arm64 machine; otherwise it stays a follow-up and the issue stays open.

  9. moved this from Triage to In progress in obs-rust Multiviewon Oct 31, 2026
  10. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Progress (#3004, worker tick obs-studio4, 2026-10-31): the compress side now borrows its output rows once per row pair instead of four Sink::write calls per block — #3163 (draft, Refs this issue).

    Paired branch-vs-master runs, format/uyvx_to_*/1080p, same host back to back (Ryzen 7 3700X, four worker checkouts resident):

    case master Rust #3163 Rust speedup C (both)
    uyvx_to_i420 2.050 ms 1.512 ms 1.36x 0.89-0.92 ms
    uyvx_to_nv12 1.649 ms 1.403 ms 1.18x 0.75-0.79 ms
    uyvx_to_i444 1.943 ms 1.686 ms 1.15x 1.13-1.19 ms

    All 32 lib tests in the module pass, C-oracle parity included. Still ~1.5-1.9x over the C, so the issue stays open.

    The parked SIMD kernel is a dead end in its current shape, measured, not guessed. I re-applied ~/dev/obs-rust/3004-simd-wip.patch's SSE2/NEON Block kernel twice: on master's per-block writes (i420 2.18 vs 1.89 ms, nv12 3.70 vs 1.66 ms) and on top of #3163's row writes (i420 1.89 vs 1.51, nv12 3.70 vs 1.40). Handling back Block { y, u, v: [[u8; 4]; 2] } per block costs more than the scalar byte picks it replaces. A SIMD path that can win must store into the output rows inside the kernel (raw pointers under src/platform/), not return a struct — that is the remaining work, and it is not in #3163.

    Two corrections from this tick, for whoever picks up the SIMD step:

    • A first cut of the I444 change looped the three planes outside the block loop, so it read each input block three times: 2.48 ms vs master's 1.94 ms. Packing all three channels from the same two 16-byte loads is what makes it 1.15x instead of -28 %.
    • Each block reads 16 bytes (_mm_load_si128), so a row slice must be blocks * 16 bytes even though the luma writes are only blocks * 4; sizing the read by the write panics the Tier 1 tests.
  11. removed their assignment
    on Nov 1, 2026
  12. moved this from In progress to Ready in obs-rust Multiviewon Nov 1, 2026
  13. zackees commented on Nov 5, 2026

    @zackees
    MemberAuthor

    Claiming: #3004 — direct-output SIMD kernels for UYVX compression.

    1. Preserve the merged row-slice work (perf(media-io): borrow each UYVX block instead of copying it (#3004) #3134/perf(media-io): write UYVX rows through one borrowed slice (#3004) #3163), inventory the FFI and overlapping-buffer contracts, and measure the existing format benchmark on this host.
    2. RED: forced scalar/SSE2/NEON path parity cases in the existing Media I/O category harness. GREEN: kernels under src/platform that consume borrowed rows and store directly into output rows, avoiding the measured slow Block-return design.
    3. Validate byte-exact C-oracle parity, scalar fallback and runtime dispatch, record before/after format measurements, and seek named aarch64 execution before claiming the full issue complete. One PR; incomplete architecture evidence remains Refs and is not counted as resolved. First draft after RED evidence.
  14. moved this from Ready to In progress in obs-rust Multiviewon Nov 5, 2026
  15. zackees commented on Nov 5, 2026

    @zackees
    MemberAuthor

    Releasing #3004. The work is committed and pushed in draft PR #3667, head 63f8050, but it does not meet this issue’s acceptance criterion and is not queued.

    The final isolated AMD EPYC 9V74 measurement (three paired record.py repeats, existing ratio-of-medians metric) is I420 1.0744× C, NV12 0.9266×, I444 0.9506×. I420’s paired range is 1.0714–1.0810, so the remaining 7.4% gap is stable; the exact Rust≤C guard correctly fails. Evidence: https://github.com/obs-rust/obs-studio/actions/runs/42003496128, Linux artifact.

    The draft includes direct-output SSE2/NEON kernels, scalar fallback, forced C-oracle parity, and an opt-in native comparison job in the existing Dispatch workflow. All 32 local format cases, scoped clippy, and fresh full pinned lint pass; the single primary reviewer found no blocking correctness issue. The earlier Apple M1 (Virtual) run passed native/scalar parity and improved Rust’s own medians about 2–2.7×; its C oracle is scalar SIMDe, so that is not a comparison against tuned production NEON C.

    The next worker can resume the draft’s remaining measured I420 optimization. No threshold was weakened, no public headers or existing C tests changed, and this issue was not counted as resolved.

  16. moved this from In progress to Ready in obs-rust Multiviewon Nov 5, 2026
  17. zackees commented on Nov 7, 2026

    @zackees
    MemberAuthor

    Releasing #3004 cleanup follow-up: #3667 is now closed, not merged, because its final isolated I420 Rust/C ratio 1.0744 misses the required ≤1.0 bound. Branch perf/3004-direct-simd-rows is retained with forced-path parity, SSE2/NEON kernels and measurement artifacts. The prior release remains in effect; this issue stays open and available for a new measured optimization. No acceptance criterion was changed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3enhancementreadysize:L

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions