Skip to content

media-io: Scale directly into compatible output planes (experimental) - #743

Draft
chriscoveries wants to merge 4 commits into
obs-rust:masterfrom
chriscoveries:draft/video-scaler-characterize
Draft

chriscoveries wants to merge 4 commits into
obs-rust:masterfrom
chriscoveries:draft/video-scaler-characterize

Conversation

@chriscoveries

@chriscoveries chriscoveries commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

GOOD MORNING OBSRUSTEES! 👋 Hi @zackees, hi everyone! We're still recovering on CI, but we're back in the game a little. @rdoupe, judging by how fast your fixes land, your CI must be the size of a small moon. 🌕

Draft. Our own CI is still in the repair shop (see our note on #699), so the results below are from local runs on our Mac. The speed-up hasn't been measured.

Direct-to-output video scaling (illustration)

Problem

video_scaler_scale always has swscale write each scaled frame into an internal scratch buffer, then copies it plane by plane into the caller's output. When the caller's planes are already aligned and packed, that copy is pure overhead on every frame.

Fix

  • When the destination planes meet the direct-output guards (aligned and packed), swscale writes straight into the caller's planes.
  • The direct path is now skipped whenever input and output memory overlap. That covers exact, partial and cross-plane overlaps, and overlapping destination planes. Writing straight into an output that overlaps the input would let swscale read pixels it has already overwritten. The overlap and span checks also refuse address or size overflow. In all of those cases the existing scratch copy runs, so output matches today's.
  • All other layouts (padded, misaligned, odd heights in vertically subsampled formats) keep the scratch-and-copy path too. Their observable output is unchanged.
  • Tests, all registered through tests.d:
    • test_video_scaler: characterization of packed, padded and misaligned destinations, repeated calls and odd heights.
    • test_video_scaler_alias: BGRA, I420, NV12, P010 and YUVA sources scaled into an output that overlaps the input. It compares three successive frames with the scratch-copy result and checks that memory outside the output is untouched.
    • test_video_scaler_spans: the overlap predicates themselves, including all 16 source and destination plane pairings and arithmetic limits.

Evidence

Local runs on an Apple Silicon Mac (macOS 27, Xcode 27.0, macos-ci preset, RelWithDebInfo):

  • This PR's head (77db966d1): all three tests pass with ENABLE_RUST_LIBOBS OFF and ON, 10 repeats each (60 of 60). The span checks also pass a standalone build under AddressSanitizer and UBSan: 65,536 range comparisons, plus the plane and overflow cases.
  • The test-only commit (a3a3e3fd0, before the overlap fix), OFF and ON: test_video_scaler_alias fails with Memory is not equal in scaler_alias_bgra and scaler_alias_planar. The existing test_video_scaler passes 10 of 10. So the direct path, as first posted here, corrupted overlapping output, and this update fixes it.
  • clang-format 22 --dry-run --Werror on the changed C files and gersemi 0.25 --check on the new CMake fragments: clean.
  • Not run: Linux or Windows, and any benchmark. Fork CI: not run, because our CI is still recovering. Upstream PR CI is the first CI run.

A local note for anyone running these on a Mac with Homebrew cmocka: RelWithDebInfo tests signed with the hardened runtime refuse to load it (different Team IDs). We re-signed the three test executables ad hoc with disable-library-validation and added absolute rpaths before running ctest.

Compatibility & risk

  • No API change.
  • Odd-height subsampled formats: the legacy path exposes only the floor-sized chroma rows. The test records that as characterized, not endorsed, and the direct path is disabled for those heights, so their behaviour doesn't change.

Status

Draft and experimental. Updated with the overlap fix and its tests.


Thank you to the obs-rust maintainers and contributors for building this project in the open. In the American spirit of free expression and individual independence, and with the principle of judging contributions on their merit alone, this PR is a small piece of what independent people can build when they come together.

@zackees

zackees commented Oct 15, 2026

Copy link
Copy Markdown
Member

he's pushing the CI to the remote and doing minimal builds on his local end

@zackees

zackees commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

i've put the AI into a dream state and it's consolidating the strategies of the last day and evolving them

last 24 hours we experimented with the merge queue

The end result was one of the PRS got ejected eight times so we're now working on version two of the CI four this repo

Hopefully the friction is reduced for you completely today. Please feel free to evolve the CI as we go. The guidelines are in the repo.

@zackees

zackees commented Oct 15, 2026

Copy link
Copy Markdown
Member

CI triage: the only failure is "Rust Tier 2 macos-26", and it is a runner/SDK problem, not your change: the Xcode link of libobs fails with MacOSX27.0.sdk/usr/lib/libSystem.B.tbd: error: unknown architecture. Master fixed this in #808 ("Keep macOS Rust links on the selected SDK"). Fix: rebase onto origin/master (git fetch origin && git rebase --autostash origin/master) and push; CI will rerun. Nothing to change in your code for this. Local check: ./lint and ./unittest (Linux; macOS is CI-only).

@chriscoveries
chriscoveries force-pushed the draft/video-scaler-characterize branch from 77db966 to 8b9a65b Compare October 15, 2026 19:18
@chriscoveries

Copy link
Copy Markdown
Contributor Author

Thanks for the triage, @zackees. I've rebased onto master (dd80fdec9) as you suggested, to pick up #808. The new head is 8b9a65bda, and the rebase was clean.

Re-tested locally at that exact head on an Apple Silicon Mac (macos-ci, RelWithDebInfo): test_video_scaler, test_video_scaler_alias and test_video_scaler_spans all pass with ENABLE_RUST_LIBOBS OFF and ON, 10 repeats each. The span checks also pass under ASan and UBSan.

@chriscoveries

Copy link
Copy Markdown
Contributor Author

Speed numbers, if you'd like them. We don't time on our shared machines, so we wrote a small before/after script that a reviewer can run on a quiet machine.

  • It builds only video-scaler-ffmpeg.c (merge base vs this PR) against the system FFmpeg, with no OBS build.
  • It checks that both versions give byte-identical output first.
  • It then alternates before and after for at least 7 rounds and reports the median and spread. Where the difference is inside the noise, it says "inconclusive".
git fetch https://github.com/chriscoveries/obsrust-fork bench/ab-scaler && git checkout FETCH_HEAD
bash test/review/scaler-bench/run.sh --measure

It prints a Markdown block with your machine details; please paste it here. The scaler source it compares is identical to this PR's rebased head.

Tested on our Mac:

  • correctness: 12 of 12 cases byte-identical;
  • the unit tests pass;
  • a smoke run labelled "NOT A MEASUREMENT" completes.

We haven't run a real timing.

@zackees

zackees commented Oct 16, 2026

Copy link
Copy Markdown
Member

Review from the maintainer side. I read the body, the comments and the diff at 8b9a65bda, and checked CI. I did not build or run it myself.

Benchmark. Thanks for writing the A/B script. To be plain about where it stands: your 19:20 comment gives the method and the correctness results (12 of 12 byte-identical, unit tests pass), but it has no timing numbers yet, and the body still says the speed-up is unmeasured. So nothing posted so far shows the change is faster, and I can't say the numbers support it. I have not run scaler-bench/run.sh either; the shared box I'd use is busy, and a noisy run would not be worth posting. If you or another reviewer paste the Markdown block from a quiet machine, that is the evidence this PR needs. If it says "inconclusive", say so and we'll take that as the answer.

Correctness (read, not run). The guards look sound to me: destination planes must be present, base_get_alignment()-aligned and at FFmpeg's packed linesize; odd heights on vertically subsampled formats stay on the old path; the span helpers reject overflow and negative-as-unsigned strides; any overlap between input and output planes (including output-to-output) falls back to the scratch copy. The one production caller, video-io.c, passes video_frame_init planes, which are aligned and packed, so the direct path would be taken there. That makes the benefit real in principle, but also means every convert-path frame depends on these guards.

Tests. The alias fix has the right shape: 9d8e836fd adds the failing alias test, 8b9a65bda fixes it. Two gaps against our RED then GREEN bar:

  1. 8c7ee5432 adds test_video_scaler.c (characterization) in the same commit as the direct-output change. A characterization test should be its own commit that passes on unchanged master, so a reader can see the old behaviour pinned before it changes. Please split it.
  2. Please state, or add a test, that the direct path is safe for widths that are not multiples of 16/32 with an exactly-sized destination (no slack behind the last row), under ASan. swscale's SIMD paths are the usual place for a row-tail overrun, and the scratch buffer is allocated with 32-byte alignment. In video_frame_init frames each plane is padded up to the alignment, which probably hides this in OBS, but the contract of video_scaler_scale allows tighter buffers. Your canary tests may already cover it; if so, name the case.

CI. 18 pass, 5 skipped, 0 failing, and the branch merges cleanly with master, so no rebase is needed.

Verdict: (a) move out of draft after these changes: (1) a timing block that shows a gain beyond noise; (2) split the characterization test into its own commit ahead of the behaviour change; (3) the tail-overrun evidence above. No edits to existing C tests or public headers are involved, which is good.

@obs-rust-project-manager obs-rust-project-manager Bot moved this to In progress in obs-rust Multiview Oct 25, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from In progress to Needs fix in obs-rust Multiview Oct 25, 2026
@zackees

zackees commented Oct 25, 2026

Copy link
Copy Markdown
Member

Coordinator check-in, @chriscoveries: there's been no activity here since the maintainer's review about 9 days ago. Are you still working on this? If you've moved on, say "handed off to the coordinator" and I'll keep the mechanical work (rebases, list conflicts, formatting) moving. Anything in the review that needs a design change stays with you.

@zackees

zackees commented Oct 27, 2026

Copy link
Copy Markdown
Member

Status from the coordinator, @chriscoveries — this draft is DIRTY (conflicting), and like #742 the cause is a rename on master rather than anything you did:

  • libobs/media-io/video-scaler-ffmpeg.c is now libobs/media-io/video-scaler-ffmpeg.c.h after f96e0daa1 ("libobs: Rename sources to implementation files"). That is the single conflict git merge-tree origin/master pull/743/head reports; the other seven files in the PR (the three test/cmocka sources, their tests.d entries, video-scaler-overlap.h) still apply cleanly.
  • So the mechanical fix is a rebase plus re-applying your hunks onto the renamed file. I will do that as a fixup: commit pushed with --force-with-lease if you want it — or reply handed off to the coordinator and I will own the mechanical part from here.

Context for why I am asking rather than just doing it: the last commit (8b9a65bda) is from 2026-10-15, the maintainer's review is from 2026-10-16, and the check-in on 2026-10-25 had no answer, so this draft has been silent for 12 days. If scaling directly into compatible output planes is no longer something you plan to finish, that is a perfectly good answer and I will report it to the maintainer instead of leaving it to rot as a "live" draft.

@obs-rust-project-manager obs-rust-project-manager Bot moved this from Needs fix to In progress in obs-rust Multiview Oct 30, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from In progress to Needs fix in obs-rust Multiview Oct 30, 2026
Add test_video_scaler, a characterization test that pins what
video_scaler_scale() writes today, before the direct-output change. It
scales a BGRA source into packed, padded and misaligned destinations of
several formats and checks:

- the visible bytes match a padded-stride reference;
- canary bytes behind each plane and the uncopied last chroma row on odd
  heights stay untouched (characterized, not endorsed);
- repeated calls through one scaler give the same output.

The exact-extent cases give each plane exactly linesize * rows bytes,
ending right before an inaccessible page, at widths that are not
multiples of 16 or 32 (33 to 998), for eight formats and the point, fast
bilinear and bicubic scale types. Any read or write past the last row
faults, whichever code makes it, including FFmpeg, which is not built
with ASan.

No library change; the test passes on the current scratch-copy path.
video_scaler_scale() always had swscale write into the scaler's own
scratch image and then copied every plane into the caller's output. When
the caller's planes already have the layout swscale would write, that
copy is extra work on every frame.

Let swscale write straight into the caller's planes when every plane the
destination format uses is present, aligned to base_get_alignment() and
at FFmpeg's packed linesize for the width (av_image_fill_linesizes), and
the height is a multiple of the vertical chroma subsampling. The frames
video-io.c converts into come from video_frame_init(), whose planes are
aligned and at these linesizes for even sizes. Every other layout keeps
the scratch copy, so its output does not change.

test_video_scaler passes unchanged, OFF and ON, including its
exact-extent cases, which now take the direct path.
Add test_video_scaler_alias: BGRA, I420, NV12, P010 and YUVA sources
scaled to BGRA into an output that overlaps the input (the same address,
128 bytes after or before it, or starting in a chroma plane). Each case
compares three successive frames with a scratch-copy reference and checks
that no byte outside the output changes.

It fails on the direct path of the previous commit, which lets swscale
read input rows it has already overwritten.
The direct-output path lets swscale write straight into the caller's
planes. When those planes overlap the input, including partial and
cross-plane overlaps, swscale reads pixels it has already overwritten.
Build checked spans for every source and destination plane, and take the
direct path only when all of them are disjoint, destination planes
included, and the span arithmetic cannot overflow. Otherwise use the
existing scratch copy.

The predicates live in media-io/video-scaler-overlap.h, which
libobs-media.pch.h includes for video-scaler-ffmpeg.c.h. They use no
pointer arithmetic, so test_video_scaler_spans compiles the header alone,
without libobs or FFmpeg, and checks them, including all 16 source and
destination plane pairings and the arithmetic limits.
@chriscoveries
chriscoveries force-pushed the draft/video-scaler-characterize branch from 8b9a65b to 81cf5ee Compare October 31, 2026 15:02
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Needs fix to In progress in obs-rust Multiview Oct 31, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from In progress to Needs fix in obs-rust Multiview Oct 31, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Needs fix to In progress in obs-rust Multiview Nov 1, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from In progress to Needs fix in obs-rust Multiview Nov 1, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Needs fix to Stalled in obs-rust Multiview Nov 3, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Stalled to Needs fix in obs-rust Multiview Nov 4, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Needs fix to Stalled in obs-rust Multiview Nov 5, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Stalled to Needs fix in obs-rust Multiview Nov 6, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Needs fix to Stalled in obs-rust Multiview Nov 6, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Stalled to Needs fix in obs-rust Multiview Nov 7, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Needs fix to Stalled in obs-rust Multiview Nov 21, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Stalled to Needs fix in obs-rust Multiview Nov 21, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Needs fix to Stalled in obs-rust Multiview Nov 25, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Stalled to Needs fix in obs-rust Multiview Nov 25, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Needs fix to Stalled in obs-rust Multiview Nov 29, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Stalled to Needs fix in obs-rust Multiview Nov 29, 2026
@zackees

zackees commented Dec 5, 2026

Copy link
Copy Markdown
Member

Rebase status (coordinator pass, 2026-12-04): this branch is 1500+ commits behind master and was not rebased. A test merge onto current master conflicts in three places:

  • libobs/build/source-sets.json no longer exists; source lists are per-set fragments in libobs/build/source-sets.d/*.json. Re-add the new files to the matching fragment instead.
  • libobs/build/libobs-media.pch.h: content conflict with master's include list; keep master's lines and add yours in sorted order.
  • libobs/media-io/video-scaler-ffmpeg.c.h: content conflict with later master edits to the scaler.

To rebase: git fetch upstream && git rebase upstream/master, resolve the three files as above, then run uv run --no-project ci/test_build_groups.py and ./lint. The new tests use tests.d/*.cmake, so the recent test-graph.json split (#4655) does not affect them. No action needed if this experiment is parked.

This branch has not been deployed

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

Labels

None yet

Projects

Status: Needs fix

Development

Successfully merging this pull request may close these issues.

2 participants