Files
punktfunk/clients/apple/Tests/PunktfunkKitTests/VideoToolboxRoundTripTests.swift
T
enricobuehler 739a5f76bf
ci / web (push) Successful in 1m3s
ci / docs-site (push) Successful in 58s
decky / build-publish (push) Successful in 16s
docker / build-push (--build-arg FEDORA_VERSION=44, ci, ci/fedora-rpm.Dockerfile, punktfunk-fedora44-rpm) (push) Successful in 8s
docker / build-push (., web/Dockerfile, punktfunk-web) (push) Successful in 7s
docker / build-push (ci, ci/fedora-rpm.Dockerfile, punktfunk-fedora-rpm) (push) Successful in 7s
docker / build-push (ci, ci/rust-ci.Dockerfile, punktfunk-rust-ci) (push) Successful in 8s
docker / build-push (docs-site, docs-site/Dockerfile, punktfunk-docs) (push) Successful in 43s
ci / bench (push) Successful in 5m21s
arch / build-publish (push) Successful in 10m58s
docker / deploy-docs (push) Successful in 28s
android / android (push) Successful in 13m23s
deb / build-publish (push) Successful in 12m58s
ci / rust (push) Successful in 18m5s
rpm / build-publish (43, bazzite, punktfunk-fedora-rpm) (push) Successful in 12m31s
rpm / build-publish (44, fedora-44, punktfunk-fedora44-rpm) (push) Successful in 12m56s
flatpak / build-publish (push) Successful in 5m53s
windows-host / package (push) Successful in 14m37s
windows-msix / package (arm64, C:\Users\Public\ffmpeg-arm64, --no-default-features, aarch64-pc-windows-msvc, C:\t-a64) (push) Successful in 4m9s
windows-msix / package (x64, C:\Users\Public\ffmpeg, , x86_64-pc-windows-msvc, C:\t) (push) Successful in 4m15s
windows / build (aarch64-pc-windows-msvc) (push) Successful in 5m12s
windows / build (x86_64-pc-windows-msvc) (push) Successful in 6m6s
apple / swift (push) Has been cancelled
apple / screenshots (push) Has been cancelled
release / apple (push) Has been cancelled
feat(apple): PyroWave Phase 5 — native Metal decode on Mac / Apple TV / iPad (§4.7)
The Apple client now decodes PyroWave natively on the presenter's own MTLDevice —
no MoltenVK, no upstream C++ in the app. Completes and wires up the decoder whose
early working-tree snapshot rode along in 9127c346:

- MetalWaveletShaders.swift: wavelet_dequant + idwt hand-ported from the vendored
  GLSL (STORAGE_MODE 0 only; subgroup scans → 32-wide simdgroups; DCShift spec
  constant → function constant; precision-1 split: fp16 levels 0-1 / fp32 2-4).
- MetalWaveletDecoder.swift: Swift reimplementation of push_packet/decode_packet
  incl. the Phase-4 chunk-aligned window walk (FRAG chains, zeroed missing shards,
  the >half-blocks partial rule), init_block_meta's block-index space, and the
  42-dequant + 13-idwt dispatch structure with encoder-boundary barriers. SOF-dims
  changes rebuild the size-dependent resources, which is also the mid-stream
  resize path. Ring of 4 output plane sets on the presenter's queue.
- Presenter: pf_frag_planar (3xR8, the planar_csc.frag twin) + renderPlanar with
  a shared present tail; ReadyFrame carries an image enum (.video | .planar).
- Stage2Pipeline: a dedicated PyroWave pump — no VideoToolbox machinery, no
  keyframe/re-anchor recovery (all-intra; partials render as localized blur by
  design), newest-frame-index staleness guard for late partials.
- Opt-in: "PyroWave (wired LAN)" codec entry (probe-gated, ≈A13 floor via a real
  kernel-compile probe), selecting it advertises + prefers the codec and forces
  the session SDR (HDR/10-bit/4:4:4 caps dropped, plan contract).
- Core ABI: punktfunk_connection_shard_payload() — the Welcome's negotiated shard
  payload, needed by native decoders to walk chunk-aligned AUs.
- Validation: golden fixtures generated by the host encoder + upstream's own
  decoder (pyrowave_dump_golden, RTX 5070 Ti); the Metal decode PSNR-matches at
  77-88 dB across all planes for dense AND chunk-aligned AUs, and a hole-punched
  partial still decodes. Parser unit tests cover the window walk, FRAG chains,
  broken chains, the half-blocks gate, and the block-index layout.

Tests: apple 134 green (mac; iOS/tvOS build), host 312 w/ pyrowave on .21,
core 148 w/ quic; clippy/fmt clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 12:16:43 +02:00

226 lines
10 KiB
Swift

// Real-bitstream proof of the decode-prep path: VTCompressionSession encodes HEVC, we
// rebuild the host's wire shape (Annex-B AU with in-band VPS/SPS/PPS — exactly what
// punktfunk-host emits on every IDR), run it through AnnexB, and hand the result to a real
// VTDecompressionSession. Pixels out = the whole client decode path is sound.
import AVFoundation
import CoreMedia
import VideoToolbox
import XCTest
@testable import PunktfunkKit
/// Sendable holder for the values the (background-thread) decode callback writes.
private final class FrameBox: @unchecked Sendable {
let lock = NSLock()
var frame: ReadyFrame?
var error: OSStatus?
}
final class VideoToolboxRoundTripTests: XCTestCase {
private let width = 320
private let height = 240
func testEncodeAnnexBDecodeRoundTrip() throws {
let (formatDesc, avccSample) = try encodeOneHEVCKeyframe()
// Rebuild the host's wire format: Annex-B AU, parameter sets in-band before the VCL.
let annexB = try annexBAU(formatDesc: formatDesc, avccSample: avccSample)
// 1) Parameter-set extraction → format description.
let rebuilt = try XCTUnwrap(
AnnexB.formatDescription(fromIDR: annexB, codec: .hevc),
"in-band VPS/SPS/PPS should yield a format description")
let dims = CMVideoFormatDescriptionGetDimensions(rebuilt)
XCTAssertEqual(Int(dims.width), width)
XCTAssertEqual(Int(dims.height), height)
// 2) Annex-B → AVCC re-pack must reproduce the encoder's own sample bytes.
XCTAssertEqual(AnnexB.avcc(from: annexB, codec: .hevc), avccSample)
// 3) Sample buffer → real decoder → pixels.
let au = AccessUnit(
data: annexB, ptsNs: 1_000_000, frameIndex: 0, flags: 0, receivedNs: 0)
let sample = try XCTUnwrap(AnnexB.sampleBuffer(au: au, format: rebuilt, codec: .hevc))
var session: VTDecompressionSession?
XCTAssertEqual(
VTDecompressionSessionCreate(
allocator: nil, formatDescription: rebuilt, decoderSpecification: nil,
imageBufferAttributes: nil, outputCallback: nil,
decompressionSessionOut: &session),
noErr)
let decoder = try XCTUnwrap(session)
defer { VTDecompressionSessionInvalidate(decoder) }
var decoded: CVImageBuffer?
var decodeStatus: OSStatus = -1
// No async flag → the handler runs before DecodeFrame returns.
VTDecompressionSessionDecodeFrame(
decoder, sampleBuffer: sample, flags: [], infoFlagsOut: nil
) { status, _, imageBuffer, _, _ in
decodeStatus = status
decoded = imageBuffer
}
XCTAssertEqual(decodeStatus, noErr)
let pixels = try XCTUnwrap(decoded) // CVImageBuffer and CVPixelBuffer are the same CF type
XCTAssertEqual(CVPixelBufferGetWidth(pixels), width)
XCTAssertEqual(CVPixelBufferGetHeight(pixels), height)
}
/// Stage-2 decode half: the same known IDR through `VideoDecoder` — assert its async output
/// callback fires with a CVPixelBuffer of the right dimensions, the pts and the receipt stamp
/// round-trip (the latter rides the frame refcon), and decode-completion is stamped.
func testVideoDecoderAsyncCallbackDeliversPixels() throws {
let (formatDesc, avccSample) = try encodeOneHEVCKeyframe()
let annexB = try annexBAU(formatDesc: formatDesc, avccSample: avccSample)
let format = try XCTUnwrap(AnnexB.formatDescription(fromIDR: annexB, codec: .hevc))
let au = AccessUnit(
data: annexB, ptsNs: 42_000_000, frameIndex: 0, flags: 0, receivedNs: 41_000_000)
let box = FrameBox()
let done = DispatchSemaphore(value: 0)
let decoder = VideoDecoder(
onDecoded: { frame in
box.lock.lock(); box.frame = frame; box.lock.unlock()
done.signal()
},
onDecodeError: { status in
box.lock.lock(); box.error = status; box.lock.unlock()
done.signal()
})
XCTAssertTrue(decoder.decode(au: au, format: format), "frame submit should succeed")
XCTAssertEqual(done.wait(timeout: .now() + 10), .success, "the decode callback must fire")
decoder.reset()
box.lock.lock()
let frame = box.frame
let error = box.error
box.lock.unlock()
XCTAssertNil(error.map { "decode error \($0)" })
let ready = try XCTUnwrap(frame, "the async output callback must deliver a ReadyFrame")
let buffer = try XCTUnwrap(ready.pixelBuffer, "a VT decode delivers a .video frame")
XCTAssertEqual(CVPixelBufferGetWidth(buffer), width)
XCTAssertEqual(CVPixelBufferGetHeight(buffer), height)
XCTAssertEqual(ready.ptsNs, 42_000_000, "pts round-trips through the decoder")
XCTAssertEqual(
ready.receivedNs, 41_000_000, "receivedNs round-trips through the frame refcon")
XCTAssertGreaterThan(ready.decodedNs, 0, "decode-completion is stamped")
}
// MARK: - encode helpers
/// One forced-IDR HEVC frame; returns its format description and raw AVCC sample bytes.
private func encodeOneHEVCKeyframe() throws -> (CMVideoFormatDescription, Data) {
var session: VTCompressionSession?
let rc = VTCompressionSessionCreate(
allocator: nil, width: Int32(width), height: Int32(height),
codecType: kCMVideoCodecType_HEVC, encoderSpecification: nil,
imageBufferAttributes: nil, compressedDataAllocator: nil,
outputCallback: nil, refcon: nil, compressionSessionOut: &session)
guard rc == noErr, let encoder = session else {
throw XCTSkip("no HEVC encoder available (\(rc))")
}
defer { VTCompressionSessionInvalidate(encoder) }
VTSessionSetProperty(encoder, key: kVTCompressionPropertyKey_RealTime, value: kCFBooleanTrue)
VTSessionSetProperty(
encoder, key: kVTCompressionPropertyKey_AllowFrameReordering, value: kCFBooleanFalse)
let lock = NSLock()
var output: CMSampleBuffer?
let done = expectation(description: "encoded")
VTCompressionSessionEncodeFrame(
encoder, imageBuffer: try gradientPixelBuffer(),
presentationTimeStamp: CMTime(value: 0, timescale: 30),
duration: CMTime(value: 1, timescale: 30),
frameProperties: [kVTEncodeFrameOptionKey_ForceKeyFrame: kCFBooleanTrue] as CFDictionary,
infoFlagsOut: nil
) { status, _, sample in
XCTAssertEqual(status, noErr)
lock.lock()
output = sample
lock.unlock()
done.fulfill()
}
VTCompressionSessionCompleteFrames(encoder, untilPresentationTimeStamp: .invalid)
wait(for: [done], timeout: 10)
lock.lock()
defer { lock.unlock() }
let sample = try XCTUnwrap(output)
let desc = try XCTUnwrap(CMSampleBufferGetFormatDescription(sample))
let block = try XCTUnwrap(CMSampleBufferGetDataBuffer(sample))
var bytes = Data(count: CMBlockBufferGetDataLength(block))
try bytes.withUnsafeMutableBytes { raw in
let rc = CMBlockBufferCopyDataBytes(
block, atOffset: 0, dataLength: raw.count,
destination: raw.baseAddress!)
if rc != noErr { throw NSError(domain: "CMBlockBuffer", code: Int(rc)) }
}
return (desc, bytes)
}
/// The host's wire shape: 4-byte start codes, VPS/SPS/PPS in-band, then the VCL NALs.
private func annexBAU(formatDesc: CMVideoFormatDescription, avccSample: Data) throws -> Data {
var au = Data()
var psCount = 0
var nalHeaderLen: Int32 = 0
XCTAssertEqual(
CMVideoFormatDescriptionGetHEVCParameterSetAtIndex(
formatDesc, parameterSetIndex: 0, parameterSetPointerOut: nil,
parameterSetSizeOut: nil, parameterSetCountOut: &psCount,
nalUnitHeaderLengthOut: &nalHeaderLen),
noErr)
XCTAssertEqual(nalHeaderLen, 4, "AnnexB.avcc assumes 4-byte NAL length prefixes")
for i in 0..<psCount {
var ptr: UnsafePointer<UInt8>?
var size = 0
XCTAssertEqual(
CMVideoFormatDescriptionGetHEVCParameterSetAtIndex(
formatDesc, parameterSetIndex: i, parameterSetPointerOut: &ptr,
parameterSetSizeOut: &size, parameterSetCountOut: nil,
nalUnitHeaderLengthOut: nil),
noErr)
au.append(contentsOf: [0, 0, 0, 1])
au.append(Data(bytes: try XCTUnwrap(ptr), count: size))
}
// AVCC sample (4-byte BE length per NAL) → start codes.
var i = avccSample.startIndex
while i + 4 <= avccSample.endIndex {
let len = avccSample[i..<i + 4].reduce(0) { ($0 << 8) | Int($1) }
let body = avccSample.index(i, offsetBy: 4)
guard let end = avccSample.index(body, offsetBy: len, limitedBy: avccSample.endIndex)
else { break }
au.append(contentsOf: [0, 0, 0, 1])
au.append(avccSample[body..<end])
i = end
}
return au
}
private func gradientPixelBuffer() throws -> CVPixelBuffer {
var pb: CVPixelBuffer?
let attrs = [kCVPixelBufferIOSurfacePropertiesKey: [:]] as CFDictionary
XCTAssertEqual(
CVPixelBufferCreate(nil, width, height, kCVPixelFormatType_32BGRA, attrs, &pb),
kCVReturnSuccess)
let buf = try XCTUnwrap(pb)
CVPixelBufferLockBaseAddress(buf, [])
defer { CVPixelBufferUnlockBaseAddress(buf, []) }
let base = try XCTUnwrap(CVPixelBufferGetBaseAddress(buf))
let stride = CVPixelBufferGetBytesPerRow(buf)
for y in 0..<height {
let row = base.advanced(by: y * stride).assumingMemoryBound(to: UInt8.self)
for x in 0..<width {
row[x * 4 + 0] = UInt8(x & 0xFF) // B
row[x * 4 + 1] = UInt8(y & 0xFF) // G
row[x * 4 + 2] = UInt8((x ^ y) & 0xFF) // R
row[x * 4 + 3] = 0xFF
}
}
return buf
}
}