Skip to content

feat(libwebrtc): implement VideoFrameBuffer::CropAndScale on libyuv - #26446

Merged
oneandonlydean merged 6 commits into
boykopovar:mainfrom
ToRaNek:feat/webrtc-video-frame-buffer-crop-and-scale
Nov 25, 2026
Merged

oneandonlydean merged 6 commits into
boykopovar:mainfrom
ToRaNek:feat/webrtc-video-frame-buffer-crop-and-scale

Conversation

@ToRaNek

@ToRaNek ToRaNek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

What

Resubmission of #26079, which was closed when my fork left the fork network (I switched it to private by mistake) and cannot be reopened. Same head commit d3a8483c274; the reviews and the discussion are on #26079.

webrtc::VideoFrameBuffer::CropAndScale was a stub in libwebrtc, although it sits in the virtual table of every buffer I420Buffer::Create makes. This implements it from api/video/video_frame_buffer.cc and I420Buffer::CropAndScaleFrom of api/video/i420_buffer.cc, at the commit the library already pins (M134, 8d78f5de):

  • the buffer is converted through its own ToI420 slot, so a guest buffer of another type is scaled from what it converts to, and that reference is released at the end;
  • the crop rectangle must lie inside the converted buffer, a negative height (libyuv's vertical flip) being measured by its magnitude; odd offsets are made even so that the chroma planes stay aligned;
  • an I420 buffer of the scaled size is created, with the strides of the two-argument Create (the width, and half the width rounded up);
  • the three planes are scaled by libyuv::I420Scale with the box filter.

Order and refusals. Upstream creates the destination first and checks afterwards, with RTC_CHECKs that abort. Here every argument is checked before anything is allocated, and the buffer is created last; for a call that succeeds the two orders give the same result. A refused call throws std::invalid_argument and leaves the result pointer, the reference counts and the heap as they were:

  • a scaled size that is not positive, or whose three planes do not fit in int (computed in 64 bits; the chroma stride is width / 2 + width % 2, which cannot overflow);
  • a crop size libyuv refuses: a width that is not positive, a height of zero, or either above 32768. Upstream lets these through, libyuv returns an error that a release build ignores, and the caller gets the new buffer unscaled; INT_MIN would also be negated inside libyuv before its own check;
  • a crop rectangle outside the converted buffer, a ToI420 that returns nothing, a null plane.

If libyuv itself fails after that (its only remaining reason is its own allocation), the new buffer is released and std::runtime_error is thrown.

libyuv. The scaler is upstream's own: 3rdparty/libyuv is a new submodule at 10592b60, the revision the DEPS file of that WebRTC commit pins. A webrtc_yuv static target in core/libs/CMakeLists.txt builds the ten sources the scaler needs and is linked privately into libwebrtc, next to fvad; the configuration stops with the git submodule update --init 3rdparty/libyuv instruction when the submodule is empty. Its LICENSE and PATENTS join the notices tools/package_release.py ships with libwebrtc.prx.

libyuv is built as upstream builds it, with its x86 rows chosen from the host CPU. I measured the alternative: on a Zen 2 host, a build limited to the C rows gives other pixels than the SSSE3 and AVX2 rows for the 3/4 and 3/8 ratios (1280x720 to 960x540 for one), and the same pixels for the other ratios I tried, so a C-only build would not follow the console's CPU on common sizes.

Recorded in the debt entry: the module's libyuv revision is unknown; a host without SSSE3 can differ by a rounding step; the refusals above.

Tested

Windows 11, MinGW GCC 15.2, Ryzen 5 3600. Not tested on a console. A reviewer built and ran the two test targets on Ubuntu 24.04 with GCC 13.3 at an earlier head.

  • New guest_webrtc_crop_and_scale test.
    • Same size: the planes are copied whatever the source strides, for five sizes including odd ones and 1x1.
    • Half and quarter size: every pixel equals the rounded mean of its 2x2 or 4x4 box, on the three planes, with and without a crop.
    • A third: every pixel follows libyuv's fixed-point box sum, (sum * (65536 / 9)) >> 16.
    • Three quarters: a checksum of the SSSE3 rows on pseudo-random planes, which the C rows do not produce.
    • Offsets: six pairs, odd ones read from the even position below, chroma from half of it.
    • A negative crop height flips the picture, as libyuv defines it, from the row offset too.
    • Uniform planes for fifteen ratios, up and down, stay uniform, or lose one step where libyuv's general box truncates.
    • A crop next to a bright column: no scaled value leaves the range of the cropped area, for six target sizes.
    • A guest buffer with its own table and a native type: ToI420 is called once, its result is read and released once; a guest I420 buffer with unusual strides; the same buffer with each plane null in turn.
    • The scaler's limits on a 40000-pixel-wide and a 40000-pixel-high buffer: 32768 is accepted on each axis, flipped too, and 32769 refused.
    • Refusals: null result, null buffer, null table, 31 crop rectangles (too wide, too high, negative offsets, sums that overflow int, flipped crops taller than what is left below the offset, zero and negative widths, zero heights, INT_MIN), 15 scaled sizes (zero, negative, INT_MAX and INT_MIN on each axis, sizes whose luma or chroma alone would fit), a null conversion. After each one the result pointer is untouched, no reference is left and the allocator was not called; on a guest buffer a refused size or crop size is reported before ToI420 is called.
  • It passed 5 runs in a row. guest_webrtc_i420_buffer_create_virtuals required the stub to throw; it now checks the buffer the slot returns. The 119 guest_webrtc tests pass.
  • python -m unittest tools.tests.test_package_release passes (140 tests), with the libyuv notices added to the WebRTC case.
  • Negative controls: 89 single changes of src/VideoFrameBufferCropAndScale.cpp, each rebuilt and run against the test: every table slot, each term and bound of the size, crop and rectangle checks, the 32768 limit on both sides, the 64-bit sums, the magnitude of a negative height, the rounding of the offsets and of the chroma stride, each null-plane test, the origin and the stride of each plane, the order of the planes and of the sizes on both sides of the scaler, the filter, each release. All 89 make the test fail. Not covered by a control: the branch taken when libyuv fails after the buffer exists, and the release of the buffer on that branch, which the test cannot reach. Two terms of upstream's rectangle check, crop_width <= width and crop_height <= height, follow from the others and are not repeated.
  • python tools/check_conventions.py --base origin/main, python tools/debt_fold.py --check and python core/libs/tests/test_public_headers.py --audit-only pass.

Not verified. I read no console module: the revision of libyuv it embeds and the rows it selects are not known. The failure of libyuv after the buffer is created cannot be provoked from the test.

Overlap (python tools/pr_overlap.py --branch feat/webrtc-video-frame-buffer-crop-and-scale):

The tool reports no conflicting file and no shared export, then ends with overlap check failed closed: 17 open PRs that conflict with main share .gitmodules (#1835), core/libs/CMakeLists.txt (#7275, #8650, #9156, #9293, #9297, #9302, #9383, #12642, #16793, #19542, #20315, #23786, #24322, #24917) or Export.cpp and Api.hpp of libwebrtc (#18498, #24668), so it cannot compare them. I compared each by hand (its diff against main on those files and on core/libs/prx/libwebrtc): none mentions libyuv, I420Scale or CropAndScale. python tools/pr_overlap.py --names on the symbol answers open: no matching PS5 export changes.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

ToRaNek and others added 6 commits November 23, 2026 08:31
Add libyuv as the 3rdparty/libyuv submodule at the revision WebRTC M134
pins, build its scaler from source, and implement
webrtc::VideoFrameBuffer::CropAndScale from video_frame_buffer.cc and
I420Buffer::CropAndScaleFrom.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A negative crop height passed the rectangle check whatever its size,
and libyuv then read that many rows from the row offset. Compare
offsetY plus the magnitude of the height, in 64 bits, with the buffer
height, which also keeps INT_MIN away from libyuv. Stop the
configuration with an instruction when the libyuv submodule is not
initialized.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(scaledWidth + 1) / 2 overflowed for INT_MAX before I420Buffer::Create
could refuse the size. Compute the chroma stride as
scaledWidth / 2 + scaledWidth % 2.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Refuse a scaled size no I420 buffer holds and a crop size libyuv does
not accept (width below 1, height 0, either above 32768) before the
buffer is converted, and the rectangle and null planes before the
destination is created, so a refused call allocates nothing. Throw
when libyuv reports a failure instead of returning the buffer unscaled.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against d3a8483c27:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_webrtc_crop_and_scale_tests, guest_webrtc_i420_buffer_create_tests built cleanly.
  • CTest (guest_webrtc_i420_buffer_create_create, guest_webrtc_i420_buffer_create_virtuals, guest_webrtc_i420_buffer_create_invalid, guest_webrtc_crop_and_scale):
    1/4 Test #6393: guest_webrtc_i420_buffer_create_create .....   Passed    0.03 sec
    2/4 Test #6395: guest_webrtc_i420_buffer_create_invalid ....   Passed    0.03 sec
    3/4 Test #6394: guest_webrtc_i420_buffer_create_virtuals ...   Passed    0.03 sec
    4/4 Test #6396: guest_webrtc_crop_and_scale ................   Passed    0.05 sec
    100% tests passed, 0 tests failed out of 4
    
  • Negative control: with core/libs/prx/libwebrtc/Export.cpp, core/libs/prx/libwebrtc/include/Api.hpp, core/libs/prx/libwebrtc/src/VideoFrameBufferCropAndScale.cpp reverted to main and the new tests kept, the suite fails:
    1/4 Test #6396: guest_webrtc_crop_and_scale ................***Failed    0.02 sec
    2/4 Test #6393: guest_webrtc_i420_buffer_create_create .....   Passed    0.04 sec
    3/4 Test #6395: guest_webrtc_i420_buffer_create_invalid ....   Passed    0.04 sec
    4/4 Test #6394: guest_webrtc_i420_buffer_create_virtuals ...***Failed    0.02 sec
    50% tests passed, 2 tests failed out of 4
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified in core/libs/prx/libwebrtc/src/VideoFrameBufferCropAndScale.cpp that all caller pointers (result, buffer, buffer->vtable, source.object, dataY, dataU, dataV) are validated before dereferencing and throw std::invalid_argument on failure.
    • Verified in core/libs/prx/libwebrtc/src/VideoFrameBufferCropAndScale.cpp that crop and scaled dimensions are checked using int64_t arithmetic against int::max and MaximumCropDimension (32768), correctly handling negative cropHeight (vertical flip) without integer overflow on INT_MIN.
    • Verified RAII reference counting in VideoFrameBufferCropAndScale.cpp for both the converted source buffer and newly allocated destination buffer via struct Reference, preventing leaks if libyuv::I420Scale throws.
    • Verified in core/libs/CMakeLists.txt and .gitmodules that 3rdparty/libyuv is added as a submodule, built from source with CMake as a PIC hidden static library, and correctly linked into libwebrtc.
    • Verified in tools/package_release.py and tools/tests/test_package_release.py that libyuv LICENSE and PATENTS notices are packaged into release archives for libwebrtc.
    • Verified in docs/dev/debt/changes/feat-webrtc-video-frame-buffer-crop-and-scale.md that technical debt documentation adheres to repo fragment conventions without violating code comment restrictions.

Looks Good To Me

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built libwebrtc (with the new 3rdparty/libyuv submodule at 10592b60) and ran on Linux (x86-64, GCC 11):

  • guest_webrtc_crop_and_scale — passes
  • guest_webrtc_i420_buffer_create_{create,virtuals,invalid} — all three pass (note: this test is parameterized by case, so it needs the arg; running it bare aborts, which is expected)
  • python3 tools/tests/test_package_release.py — 140 tests OK (the renamed test_webrtc_library_archives_include_vad_and_scaler_notices covers the new libyuv LICENSE/PATENTS packaging)

New VideoFrameBuffer::CropAndScale / I420BufferCreate surface, so no meaningful main control. The debt fragment is honest: scaling follows M134 video_frame_buffer.cc/i420_buffer.cc with libyuv::I420Scale at M134's DEPS revision, warns the module revision is unknown and that without SSSE3 a pixel can differ by one step, and documents the argument-validation divergences (upstream aborts / reads past planes / returns unscaled where this throws std::invalid_argument). License/patent notices are correctly wired into package_release.py and the files exist in the submodule.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1835 .gitmodules
PR 7275 core/libs/CMakeLists.txt
PR 8650 core/libs/CMakeLists.txt
PR 9156 core/libs/CMakeLists.txt
PR 9293 core/libs/CMakeLists.txt
PR 9297 core/libs/CMakeLists.txt
PR 9302 core/libs/CMakeLists.txt
PR 9383 core/libs/CMakeLists.txt
PR 12642 core/libs/CMakeLists.txt
PR 16018 core/libs/CMakeLists.txt
PR 16052 core/libs/CMakeLists.txt
PR 16793 core/libs/CMakeLists.txt
PR 18498 core/libs/prx/libwebrtc/Export.cpp
core/libs/prx/libwebrtc/include/Api.hpp
PR 20315 core/libs/CMakeLists.txt
PR 23786 core/libs/CMakeLists.txt
PR 23975 core/libs/CMakeLists.txt
PR 24322 core/libs/CMakeLists.txt
PR 24917 core/libs/CMakeLists.txt

@github-actions

Copy link
Copy Markdown

Progress report (f4c9a67 → d3a8483)

📈 System libraries: 46.01% (+0.0025%, +1 implemented function)

✅ 1 implemented
Library Name
libwebrtc _ZN6webrtc16VideoFrameBuffer12CropAndScaleEiiiiii_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libwebrtc 289/412 (70.15%) 290/412 (70.39%) +0.24%

@oneandonlydean
oneandonlydean merged commit ebc9fea into boykopovar:main Nov 25, 2026
34 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants