Convert 8-bit AVIF pictures to RGBA in fixed point across the cores
libavif built without libyuv upsamples subsampled chroma with a float loop per pixel, most of a phone's AVIF decode (about 20 s of Jungle's launch on an ARMv7 Moto). The native bridge now converts 8-bit 4:2:0, 4:2:2 and 4:4:4 pictures itself with libavif's matrix coefficients and centred bilinear chroma in 16-bit fixed point, one row band per core, and leaves premultiplied alpha, identity and deeper pictures to libavif. A table-driven contract checks every channel within one level of the scalar reference across subsampling, matrices, ranges, alpha and odd edges, single-threaded and with POSIX threads.
This commit is contained in:
1 parent
2611e727e2
commit
8f706ac535
6 files changed
+242
-1
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
- The native bridge converts 8-bit pictures (4:2:0, 4:2:2, 4:4:4; BT.601, BT.709, BT.2020; full or limited range) to RGBA itself: libavif's centred bilinear chroma in fixed point, split across the cores, within one level of the reference. Built without libyuv, libavif's float path was most of a phone's decode.
|
||||
- Add bounded still-image AVIF encoding with quality 35 by default, lossless alpha, metadata and orientation preservation, and explicit Lanczos RGBA previews.
|
||||
- Native artifact project tools prepare pinned Meson and Ninja on fresh consumers.
|
||||
- Headless AVIF APIs and command, bounded container policy and pinned native libavif/libaom artifacts with verification fixtures.
|
||||
|
||||
+8
-1
@@ -2,6 +2,7 @@ using Engine :: #import "engine"
|
||||
#import "bmff"
|
||||
#import "transform"
|
||||
#import "std/byte_text_linux"
|
||||
#import "fast_rgba"
|
||||
|
||||
// Pinned libavif 1.3.0 public ABI, allocated/destroyed only by libavif.
|
||||
RawBytes :: struct { data: *u8; size: usize; }
|
||||
@@ -218,7 +219,13 @@ BridgeInternal :: (api: AvifAPI, input: BorrowedBytes, options: *DecodeOptions,
|
||||
if !VecResizeBounded(scratch, count, count) { return DecodeFailure(Error(AllocationFailed)) }
|
||||
rgb.row_bytes = image.width * 4; rgb.pixels = *scratch[0]
|
||||
}
|
||||
error = NativeError(api.convert(image, *rgb))
|
||||
// 8-bit pictures convert here, much faster than libavif without libyuv.
|
||||
fast := image.depth == 8 && image.premultiplied == 0 &&
|
||||
FastRGBA8(image.planes[0], cast(s64)image.rows[0], image.planes[1], cast(s64)image.rows[1],
|
||||
image.planes[2], cast(s64)image.rows[2], image.alpha, cast(s64)image.alpha_row,
|
||||
cast(s32)image.width, cast(s32)image.height, image.format, cast(s32)image.matrix,
|
||||
image.range == 1, rgb.pixels, cast(s64)rgb.row_bytes)
|
||||
if !fast { error = NativeError(api.convert(image, *rgb)) }
|
||||
if error.code != DecodeOK { return DecodeFailure(error) }
|
||||
error = CancellationError(options.cancellation)
|
||||
if error.code != DecodeOK { return DecodeFailure(error) }
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
// Fast 8-bit YUV to straight RGBA8 for the native decoder. Built without
|
||||
// libyuv, libavif converts subsampled chroma with a float loop per pixel,
|
||||
// most of a phone's AVIF decode. This keeps libavif's matrix coefficients
|
||||
// and its centred bilinear chroma (9/16, 3/16, 3/16, 1/16 of the nearest
|
||||
// four samples, as in color.zi) in 16-bit fixed point, and splits the rows
|
||||
// across the cores. Anything else returns false for libavif to convert.
|
||||
|
||||
FAST_RGBA_MAX_CHROMA :: 4096
|
||||
FAST_RGBA_SHIFT :: 16
|
||||
|
||||
FastRGBAPlanes :: struct {
|
||||
y: *u8; u: *u8; v: *u8; alpha: *u8
|
||||
y_row: s64; u_row: s64; v_row: s64; alpha_row: s64
|
||||
width: s32; chroma_width: s32; chroma_height: s32
|
||||
shift_x: s32; shift_y: s32
|
||||
// Fixed point: luma (y - bias) * scale; chroma per 1/16 step from 128.
|
||||
bias: s32; scale: s32
|
||||
red_v: s32; green_u: s32; green_v: s32; blue_u: s32
|
||||
output: *u8; output_row: s64
|
||||
}
|
||||
|
||||
#if #defined(POSIX_THREADS) {
|
||||
FastWorkers :: #import "std/parallel_linux";
|
||||
}
|
||||
|
||||
// Straight RGBA8 from 8-bit planes. MATRIX is the image's CICP matrix,
|
||||
// FORMAT libavif's pixel format (1 444, 2 422, 3 420) and FULL its range.
|
||||
FastRGBA8 :: (y: *u8, y_row: s64, u: *u8, u_row: s64, v: *u8, v_row: s64,
|
||||
alpha: *u8, alpha_row: s64, width: s32, height: s32, format: s32, matrix: s32,
|
||||
full: bool, output: *u8, output_row: s64) -> bool {
|
||||
if y == null || u == null || v == null || output == null || width <= 0 || height <= 0 { return false }
|
||||
kr := 0.299; kb := 0.114
|
||||
if matrix == 1 { kr = 0.2126; kb = 0.0722 }
|
||||
else if matrix == 9 { kr = 0.2627; kb = 0.0593 }
|
||||
else if matrix != 2 && matrix != 5 && matrix != 6 { return false }
|
||||
planes: FastRGBAPlanes
|
||||
planes.shift_x = ifx format == 1 then 0 else 1
|
||||
planes.shift_y = ifx format == 3 then 1 else 0
|
||||
if format < 1 || format > 3 { return false }
|
||||
planes.chroma_width = (width + planes.shift_x) >> planes.shift_x
|
||||
planes.chroma_height = (height + planes.shift_y) >> planes.shift_y
|
||||
if planes.chroma_width > FAST_RGBA_MAX_CHROMA { return false }
|
||||
planes.y = y; planes.u = u; planes.v = v; planes.alpha = alpha
|
||||
planes.y_row = y_row; planes.u_row = u_row; planes.v_row = v_row; planes.alpha_row = alpha_row
|
||||
planes.width = width; planes.output = output; planes.output_row = output_row
|
||||
// Output units are 0..255, so 255 / span maps a sample step onto them.
|
||||
one := cast(float64)(1 << FAST_RGBA_SHIFT)
|
||||
span_y := ifx full then 255.0 else 219.0
|
||||
span_c := ifx full then 255.0 else 224.0
|
||||
planes.bias = ifx full then 0 else 16
|
||||
planes.scale = cast(s32)(255.0 / span_y * one + 0.5)
|
||||
chroma := 255.0 / span_c / 16.0 * one
|
||||
kg := 1.0 - kr - kb
|
||||
planes.red_v = cast(s32)(2.0 * (1.0 - kr) * chroma + 0.5)
|
||||
planes.blue_u = cast(s32)(2.0 * (1.0 - kb) * chroma + 0.5)
|
||||
planes.green_u = cast(s32)(2.0 * kb * (1.0 - kb) / kg * chroma + 0.5)
|
||||
planes.green_v = cast(s32)(2.0 * kr * (1.0 - kr) / kg * chroma + 0.5)
|
||||
#if #defined(POSIX_THREADS) {
|
||||
// A pool per image: decodes on different threads never share one.
|
||||
if cast(s64)width * height >= 65536 {
|
||||
workers: FastWorkers.ParallelPool
|
||||
callback: FastWorkers.RangeProcedure = FastRGBARows
|
||||
FastWorkers.ParallelRun(*workers, 0, cast(s64)height, callback, cast(*void)*planes)
|
||||
FastWorkers.ParallelClose(*workers)
|
||||
return true
|
||||
}
|
||||
}
|
||||
FastRGBARows(0, cast(s64)height, cast(*void)*planes)
|
||||
return true
|
||||
}
|
||||
|
||||
FastRGBAByte :: (value: s32) -> u8 {
|
||||
rounded := (value + (1 << (FAST_RGBA_SHIFT - 1))) >> FAST_RGBA_SHIFT
|
||||
return ifx rounded < 0 then cast(u8)0 else ifx rounded > 255 then cast(u8)255 else cast(u8)rounded
|
||||
}
|
||||
|
||||
// Rows [START, END). Each row blends its two chroma rows once (3:1), then
|
||||
// each chroma column gives its two pixels their own 3:1 horizontal blend.
|
||||
FastRGBARows :: (start: s64, end: s64, argument: *void) {
|
||||
p := cast(*FastRGBAPlanes)argument
|
||||
blend_u: [FAST_RGBA_MAX_CHROMA]s32
|
||||
blend_v: [FAST_RGBA_MAX_CHROMA]s32
|
||||
last_x := p.chroma_width - 1
|
||||
for row: start..end-1 {
|
||||
cy := cast(s32)row >> p.shift_y
|
||||
near := cy
|
||||
if p.shift_y != 0 {
|
||||
if (row & 1) != 0 { if cy + 1 < p.chroma_height { near = cy + 1 } }
|
||||
else if cy > 0 { near = cy - 1 }
|
||||
}
|
||||
// Vertical blend in quarters: 3 x the own row + 1 x the nearest one.
|
||||
u0 := cast(s64)cy * p.u_row; u1 := cast(s64)near * p.u_row
|
||||
v0 := cast(s64)cy * p.v_row; v1 := cast(s64)near * p.v_row
|
||||
for cx: cast(s32)0..last_x {
|
||||
blend_u[cx] = 3 * cast(s32)p.u[u0 + cx] + cast(s32)p.u[u1 + cx]
|
||||
blend_v[cx] = 3 * cast(s32)p.v[v0 + cx] + cast(s32)p.v[v1 + cx]
|
||||
}
|
||||
luma := row * p.y_row
|
||||
alpha := row * p.alpha_row
|
||||
out := row * p.output_row
|
||||
for x: cast(s32)0..p.width-1 {
|
||||
cx := x >> p.shift_x
|
||||
// In sixteenths of a sample: the horizontal 3:1 blend of quarters.
|
||||
cu: s32; cv: s32
|
||||
if p.shift_x == 0 { cu = blend_u[cx] * 4; cv = blend_v[cx] * 4 }
|
||||
else {
|
||||
side := cx
|
||||
if (x & 1) != 0 { if cx < last_x { side = cx + 1 } }
|
||||
else if cx > 0 { side = cx - 1 }
|
||||
cu = 3 * blend_u[cx] + blend_u[side]
|
||||
cv = 3 * blend_v[cx] + blend_v[side]
|
||||
}
|
||||
cu -= 2048; cv -= 2048
|
||||
base := (cast(s32)p.y[luma + x] - p.bias) * p.scale
|
||||
at := out + cast(s64)x * 4
|
||||
p.output[at] = FastRGBAByte(base + p.red_v * cv)
|
||||
p.output[at + 1] = FastRGBAByte(base - p.green_u * cu - p.green_v * cv)
|
||||
p.output[at + 2] = FastRGBAByte(base + p.blue_u * cu)
|
||||
p.output[at + 3] = ifx p.alpha != null then p.alpha[alpha + x] else cast(u8)255
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,7 @@ sh tests/container.sh
|
||||
sh tests/dynamic.sh
|
||||
sh tests/native_recipe.sh
|
||||
sh tests/transforms_color.sh
|
||||
sh tests/fast_rgba.sh
|
||||
sh tests/encode.sh
|
||||
sh tests/preview.sh
|
||||
# Authored AV1 decode of every fixture item through the container parser,
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
# The native bridge's fast 8-bit RGBA conversion against the scalar reference.
|
||||
set -eu
|
||||
. "$(dirname -- "$0")/common.sh"
|
||||
out="$root/build/scratch/fast-rgba"
|
||||
mkdir -p "$out"
|
||||
set -- --root tests --module-path "$root/src" \
|
||||
--module-path "image_codec=$foundation/src" --module-path "std=$toolchain/std"
|
||||
for threads in single posix; do
|
||||
defines=""
|
||||
if test "$threads" = posix; then defines="--define POSIX_THREADS"; fi
|
||||
LDLIBS=-lpthread "$ziran" build --target=c --exe $defines --entry fast_rgba_contract:main "$@" \
|
||||
-o "$out/$threads-c" tests/fast_rgba_contract.zi
|
||||
"$out/$threads-c/fast_rgba_contract"
|
||||
done
|
||||
@@ -0,0 +1,95 @@
|
||||
// The native bridge's fast 8-bit conversion (src/bridge/fast_rgba.zi) against
|
||||
// the scalar reference (src/color.zi), which matches libavif's centred
|
||||
// bilinear chroma: every channel within one level, across subsampling,
|
||||
// matrices, ranges, alpha and odd edges. It also times both.
|
||||
#import "color"
|
||||
#import "output"
|
||||
#import "fast_rgba"
|
||||
#import "std/vec"
|
||||
#import "std/timer_linux"
|
||||
|
||||
FastCase :: struct { width: s32; height: s32; format: s32; matrix: s32; full: bool; alpha: bool }
|
||||
|
||||
FastNext :: (state: *u32) -> u32 {
|
||||
state[0] = state[0] * cast(u32)1664525 + cast(u32)1013904223
|
||||
return state[0] >> 24
|
||||
}
|
||||
|
||||
// Smooth gradients with noise, so both flat runs and sharp edges occur.
|
||||
FastFill :: (plane: []u8, width: s32, height: s32, seed: u32) {
|
||||
state := seed
|
||||
for y: cast(s32)0..height-1 {
|
||||
for x: cast(s32)0..width-1 {
|
||||
ramp := (cast(u32)(x * 7 + y * 3) + seed) & cast(u32)255
|
||||
noise := FastNext(*state) & cast(u32)63
|
||||
value := ifx (FastNext(*state) & cast(u32)15) == 0 then FastNext(*state) else (ramp + noise) & cast(u32)255
|
||||
plane[cast(s64)y * width + x] = cast(u8)value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FastCheck :: (case: FastCase, index: s64, timed: bool) -> s32 {
|
||||
sx: s32 = ifx case.format == 1 then 0 else 1
|
||||
sy: s32 = ifx case.format == 3 then 1 else 0
|
||||
w := case.width; h := case.height
|
||||
cw := (w + sx) >> sx; ch := (h + sy) >> sy
|
||||
y: Vec(u8); u: Vec(u8); v: Vec(u8); a: Vec(u8); expected: Vec(u8); actual: Vec(u8)
|
||||
defer { VecFree(y); VecFree(u); VecFree(v); VecFree(a); VecFree(expected); VecFree(actual) }
|
||||
if !VecResize(y, cast(s64)w * h) || !VecResize(u, cast(s64)cw * ch) || !VecResize(v, cast(s64)cw * ch) ||
|
||||
!VecResize(a, cast(s64)w * h) || !VecResize(expected, cast(s64)w * h * 4) || !VecResize(actual, cast(s64)w * h * 4) {
|
||||
print("fast rgba case %: allocation failed\n", index); return 1
|
||||
}
|
||||
FastFill(y[:], w, h, cast(u32)(index * 31 + 1))
|
||||
FastFill(u[:], cw, ch, cast(u32)(index * 31 + 7))
|
||||
FastFill(v[:], cw, ch, cast(u32)(index * 31 + 13))
|
||||
FastFill(a[:], w, h, cast(u32)(index * 31 + 19))
|
||||
source: PlanarImage
|
||||
source.color = ColorSpec.{8, case.full, case.matrix, sx, sy, false, BilinearChroma, 1}
|
||||
source.y = PlaneView.{w, h, cast(s64)w, 1, y[:]}
|
||||
source.u = PlaneView.{cw, ch, cast(s64)cw, 1, u[:]}
|
||||
source.v = PlaneView.{cw, ch, cast(s64)cw, 1, v[:]}
|
||||
source.alpha = PlaneView.{w, h, cast(s64)w, 1, a[:]}
|
||||
source.has_alpha = case.alpha
|
||||
options := DefaultOptions(); budget := NewWorkerBudget(options.limits); options.workers = *budget
|
||||
started := MonotonicNanoseconds()
|
||||
reference := ConvertRGBA8Into(source, OutputBuffer.{expected[:], cast(s64)w * 4}, options)
|
||||
reference_ns := MonotonicNanoseconds() - started
|
||||
if !reference.is_ok { print("fast rgba case %: reference error %\n", index, reference.error.code); return 1 }
|
||||
alpha: *u8 = ifx case.alpha then *a[0] else cast(*u8)null
|
||||
started = MonotonicNanoseconds()
|
||||
ok := FastRGBA8(*y[0], cast(s64)w, *u[0], cast(s64)cw, *v[0], cast(s64)cw, alpha, cast(s64)w,
|
||||
w, h, case.format, case.matrix, case.full, *actual[0], cast(s64)w * 4)
|
||||
fast_ns := MonotonicNanoseconds() - started
|
||||
if !ok { print("fast rgba case %: not converted\n", index); return 1 }
|
||||
worst: s32 = 0; off: s64 = 0
|
||||
for at: cast(s64)0..expected.count-1 {
|
||||
difference := cast(s32)expected[at] - cast(s32)actual[at]
|
||||
if difference < 0 { difference = -difference }
|
||||
if difference > worst { worst = difference }
|
||||
if difference > 0 { off += 1 }
|
||||
}
|
||||
if worst > 1 {
|
||||
print("fast rgba case % (%x% format % matrix % full %): channel off by %\n", index, w, h, case.format, case.matrix, case.full, worst)
|
||||
return 1
|
||||
}
|
||||
if timed {
|
||||
print("fast rgba %x% format %: reference % ms, fast % ms, % of % channels one level off\n", w, h, case.format,
|
||||
reference_ns / 1000000, fast_ns / 1000000, off, expected.count)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
#program_export
|
||||
main :: () -> s32 {
|
||||
cases: [12]FastCase = .[
|
||||
.{7, 5, 3, 6, true, false}, .{8, 6, 3, 1, true, true}, .{9, 7, 3, 9, false, false},
|
||||
.{1, 1, 3, 6, true, false}, .{2, 3, 3, 5, false, true}, .{33, 17, 3, 1, false, false},
|
||||
.{7, 5, 2, 6, true, true}, .{10, 3, 2, 1, false, false},
|
||||
.{7, 5, 1, 6, true, false}, .{6, 4, 1, 9, false, true},
|
||||
.{640, 360, 3, 6, true, false}, .{640, 360, 1, 6, true, true},
|
||||
]
|
||||
failures: s32 = 0
|
||||
for case, index: cases { failures += FastCheck(case, cast(s64)index, case.width >= 640) }
|
||||
print("fast rgba: % cases, % failures\n", cases.count, failures)
|
||||
return failures
|
||||
}
|
||||
Reference in new issue
Block a user