Skip to content

media: apple: scaler: scale on the MMIO engines - #4165

Open
RyanTheTide wants to merge 1 commit into
mainfrom
media-parity/mmio-scale
Open

RyanTheTide wants to merge 1 commit into
mainfrom
media-parity/mmio-scale

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

The MMIO scaler engines (M1 to M4 generations, A18 Pro) now scale. Until now they only converted: a CAPTURE format of another size came back at the source size, and GStreamer's V4L2 converter passed a top-left crop of the source off as a resized picture.

Closes #4102
Refs #3614

Problem: on MSR hardware-version 10-22 the driver forced CAPTURE to the OUTPUT size and ran every job with the polyphase filters off. NV12 1920x1080 -> 1280x720 returned 1920x1080 on the M4 Pro and the A18 Pro.

Change: program the scaler. Each axis gets its steps, a centred initial phase and a generated filter in both banks. The filters use the same Kaiser windowed-sinc kernels and ratio bins as the MSR23 table, generated by tools/media/apple-scaler/mmio_filters.py. CAPTURE keeps its own size within 4x either way per axis. The lossless layout stays at 1:1. Jobs at 1:1 program the same values as before.

Tested: 7.3.0-aurora-fleet94 with this module (W=1 clean, checkpatch --strict clean, KUnit arm64 29/29 including 3 new cases) on the M4 Pro (J616s, MSR21). Every scaled job ran at the requested size with no kernel messages. PSNR-Y against swscale lanczos (frame 0 detail / frame 1 bars):

  • NV12 1080p -> 720p: 49.8/56.1 dB
  • -> 960x540: 55.3/57.5 dB
  • 720p -> 1080p: 40.5/46.2 dB (43.4/48.8 dB against bicubic)
  • 4K -> 1080p: 54.1/60.5 dB
  • P010 1080p -> NV12 720p: 47.6/48.8 dB

Against the old top-left crop the same rows score about 10 dB. All 9 same-size conversions (including NV12 -> BX24 640x480) are byte-identical to the stock driver. GStreamer 1.28.9 v4l2convert resizing to 1280x720, 960x540 and 1080p gives byte-identical pixels to those jobs.

Risk: two-way door. It changes the driver only (no DT or UAPI). Applications that asked for another CAPTURE size and relied on getting the source size back now get the size they asked for.

Still to run: the same rows on the MacBook Neo (A18 Pro, MSR22). The engine was busy there, held by the camera service, so it is not tested yet. Also an M1/M2/M3 row (hardware-version 10-17; same register layout per the public m1n1 map). #2804 changes the same asc_fill() area; whichever lands second needs a small rebase.

How the register values were chosen
  • Bank layout from the public m1n1 scaler map: V 9 taps, H 15 taps, 32 phase slots per tap. Slot s is the phase s/32 right of the centre tap for s < 16, and (32-s)/32 left of it otherwise. Bank 0 holds one Q12 coefficient per word in both halves, bank 1 two consecutive coefficients per word. The 9-tap kernel sits on the centre taps of the 15-tap bank.
  • Flags (0x1e1/0x1e3 downscaling, 0x61/0x63 upscaling), steps, inverse steps and chroma up/down-sampling come from our own macOS register trace of scaled jobs. The filter coefficients and de-ringing values are not used; the filters are generated.
  • Initial phase: macOS starts downscales on the first source pixel. Centring, (step-1)/2 like swscale and GStreamer, gains 10 dB or more. The field is signed Q1.22 (23 bits; wider values wrap), so from 3x down the phase is held just under one pixel. 4x scores 39.0 dB, against 32.8 dB with phase 0.
  • The +0x08 word is the chroma phase of a subsampled source. Between two 4:2:0 pictures twice the luma phase matches best; for Y'CbCr -> RGB the luma phase does. It has no effect for an RGB source.
  • Pseudo-linear mode (0x480) stays off. macOS sets it for RGB sources, but it moves the result 9 dB away from gamma-space swscale.
  • Phase sign checked by mirroring the banks: mirrored, the 1.5x row drops from 38.3 to 32.0 dB.
CSC rows (scale-only reference)

The engine's own 1:1 colour conversion differs from swscale's (stock BGRA -> NV12 at 1:1 scores 28 dB, NV12 -> BGRA 32 dB). So these rows are also compared with swscale applied to the engine's 1:1 output:

  • NV12 1080p -> BGRA 720p: 47.6/51.6 dB RGB
  • BGRA 1080p -> NV12 720p: luma 48.4/54.4 dB
  • BGRA -> BGRA 720p (no CSC): 44.8/52.0 dB against plain swscale lanczos.

Chroma of RGB -> 4:2:0 is limited by the existing 1:1 downsampler, not by scaling.

The MMIO engines (MSR hardware-version 10 to 22) forced the CAPTURE
size to the OUTPUT size and ran every job with the polyphase filters
off. A CAPTURE format of another size came back at the source size, and
GStreamer's V4L2 converter then took a top-left crop of the source as a
resized picture.

Program the scaler. Each axis gets its step and inverse step in Q22,
an initial phase and a generated polyphase filter in both of its banks.
The filters use the kernels and ratio bins of the MSR23 table (a Kaiser
windowed sinc low-pass when downscaling, an interpolating 4-tap kernel
when upscaling), generated by tools/media/apple-scaler/mmio_filters.py.
The bank layout (32 phase slots per tap, signed phases, one or two
coefficients per word) follows the public m1n1 scaler register map; the
flags, steps and chroma resampling follow our own macOS register trace.

The initial phase centres output on source, as swscale and GStreamer
do: (step - 1) / 2, limited to the signed Q1.22 field. The chroma phase
is twice that between two 4:2:0 pictures. The pseudo-linear mode stays
off. Each of these was measured against swscale on the M4 Pro.

CAPTURE now keeps its own size within 4x of the OUTPUT size per axis
(the lossless layout stays at 1:1), and a scaled job grows to about
1400 register writes. Jobs at 1:1 program the same values as before.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide
RyanTheTide requested review from a team as code owners December 3, 2026 08:29
@github-actions github-actions Bot added area:media Area from CODEOWNERS (bot) blast:m1 Macs affected, from Merge risk (bot) blast:m2 Macs affected, from Merge risk (bot) blast:m3 Macs affected, from Merge risk (bot) blast:neo Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Merge readiness at e0100ffe: not ready yet

Check Detail
CI at head ok 9 checks passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes no requested by DjDeveloperr
Hardware row at head ok comment at e0100ffe
Conflicts ok none
PR text ok Risk: two-way · . It changes the driver only (no DT or UAPI). Applications that asked for another CAPTURE size and relied on getting the source size back now get the size they asked for. Still to run: the same rows on the MacBook Neo (A18 Pro, MSR22). The engine was busy there, held by the camera service, so it is not tested yet. Also an M1/M2/M3 row (hardware-version 10-17; same register layout per the public m1n1 map). #2804 changes the same asc_fill() area; whichever lands second needs a small rebase.

Areas: area:media

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Neo (J700, T8140, 7.3.0-aurora-fleet94) row at this head (e0100ffe), module built from the PR against the fleet94 tree. The camera denoise user service holds the scaler node, so it was stopped for the swap window and restarted afterwards.

  • Stock driver: every scaled request comes back at the source size (11/11).
  • This PR: 11/11 scaled rows at the requested size. PSNR-Y against swscale lanczos (frame 0 / frame 1) is the same as on the J616s: 1080p->720p 49.76 / 56.12 dB, ->960x540 55.34 / 57.53, 720p->1080p 40.49 / 46.18, 4K->1080p 54.09 / 60.45, P010->NV12 720p 47.59 / 48.76, 4x down 38.96 / 39.45. The old top-left crop scores ~9.9 dB on the same rows.
  • 9/9 same-size conversions are byte-identical (sha256) to the stock driver.
  • GStreamer 1.28.9 v4l2video12convert: 1080p->720p and 720p->1080p output is byte-identical to the v4l2-ctl jobs. BGRx caps are refused on stock and on this PR alike (the driver offers BGRA).
  • 0 kernel lines during the jobs, no new DART faults.

One thing to look at, not a blocker for the YUV paths: scaled RGB->NV12 (rgbdown, BGRA 1080p -> NV12 720p) has Y at 48.4 / 54.4 dB against the engine's own 1:1 conversion scaled by swscale, but UV only 29.9 / 32.0 dB (max diff 86 / 110). The NV12->NV12 rows have UV at 38-42 dB, so the chroma phase/step for a 4:4:4 RGB source going to 4:2:0 may be off.

@github-actions github-actions Bot removed the needs-hw-row No hardware row at the PR head (bot) label Dec 3, 2026

@aurora-silicon-review aurora-silicon-review Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: FINISH at e0100ff

Failure: The new lossless 1:1 path can negotiate a format that contradicts the advertised frame size steps. For example, an MMIO RGB OUTPUT width of 641 is valid, but asking for APPLE_L8V0 CAPTURE now copies 641 directly through asc_fill() at apple-scaler-drv.c:477–479. That format advertises an even-width step and the hardware job computes chroma width with integer division by two. The old format path aligned CAPTURE to its format's subsampling. Keep the lossless 1:1 constraint and reject an incompatible pair before accepting it.

Checked: The new resize path constrains each axis to the scale envelope, selects a generated filter from the step, and keeps unit-ratio programming on the existing path. I compared the new lossless branch with the base alignment path and traced the advertised size step and DMA plane-width calculation.

@DjDeveloperr DjDeveloperr 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.

Request changes at e0100ffe: one behaviour break, small fix. The rest of the scaling path looks right.

CAPTURE set before OUTPUT gives the wrong size. asc_s_fmt_out() (apple-scaler-drv.c:653-655) refills CAPTURE from its already clamped size. v4l2-ctl sets CAPTURE first, against the open-time 1920x1080 source. So 320x240 -> 320x240 is clamped to 480x270 by the CAPTURE call, and OUTPUT 320x240 then keeps 480x270. On main the same calls give 320x240. This hits every same-size job below 480x270 or above 7680x4320 set in that order. An application that sets only OUTPUT also gets 1920x1080 now instead of its source size. The colorimetry already has this fix (dst_ycbcr_req); do the same for the size:

/* struct apple_scaler_ctx: the CAPTURE size asked for, 0 until then */
u32 dst_w_req, dst_h_req;

/* asc_s_fmt_cap(), next to dst_quant_req, before asc_fill() */
ctx->dst_w_req = f->fmt.pix_mp.width;
ctx->dst_h_req = f->fmt.pix_mp.height;

/* asc_s_fmt_out(), before asc_fill(ctx->asc, &ctx->dst, ...) */
if (!ctx->asc->variant->fw_fronted) {
	ctx->dst.width = ctx->dst_w_req ?: ctx->src.width;
	ctx->dst.height = ctx->dst_h_req ?: ctx->src.height;
}

Odd sizes into the lossless layout (also in the earlier review): asc_fill() lines 477-479 skip the ALIGN. Put the lossless case inside the last else (if (ref && lossless) {w = ref->width; ...} else if (ref) {clamp}), so the ALIGN and clamp below still run. Then 641x481 RGB -> L8V0 fails in asc_job_build() as on main.

I built both changes on the merge with main: W=1 and checkpatch clean, KUnit passes.

Small: the mmio_filters.py header names asc_emit_filter(), the function is asc_emit_axis().

Checked on the merge of this head with main dc4d6f06:

  • W=1 arm64 objects clean, checkpatch --strict clean (one MAINTAINERS note for the new file), KUnit (UML) 29/29, mmio_filters.py --check matches the table, apple-scaler-recovery-tests.py passes.
  • Ratio and phase: no null filter set inside the 4x range, the phase cannot overflow, the write list and bank offsets fit the aperture and ASC_JOB_MAX_WRITES. At 1:1 every register gets the same value as before.
  • CAPTURE size cannot change under queued buffers, so the engine does not get a picture larger than the buffer. MSR23 (M5) formats are unchanged. T602x, T603x and T8122 do not bind this driver, so nothing changes there.

Rows: after the push, one row at the new head (J700 again, or our J773g/T8132, MSR20) that includes a 256x128 same-size job with CAPTURE set first. J616s is the second generation. An M1 or M2 row (MSR 10, 12 or 13) is wanted but does not block, since 1:1 is unchanged there. The RGB -> NV12 chroma level you noted can be a follow-up.

@github-actions github-actions Bot added the changes-requested A reviewer requested changes (bot) label Dec 3, 2026

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

area:media Area from CODEOWNERS (bot) blast:m1 Macs affected, from Merge risk (bot) blast:m2 Macs affected, from Merge risk (bot) blast:m3 Macs affected, from Merge risk (bot) blast:neo Macs affected, from Merge risk (bot) changes-requested A reviewer requested changes (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

media: apple: scaler: MMIO engines (v10-v22) never resize; CAPTURE forced to the source size

2 participants