forge
Explore
Log in

Don't you dare go Hollow

Decode RGB downscale: separable Catmull-Rom (a=-0.5) widened by the scale ratio (support 2*in/out each side), Q12 integer kernel, taps normalised to 1<<10 with the residue on the centre tap, edge-clamped; replaces the integer box footprint that aliased text at non-integer ratios. nv12_to_rgb.comp and ec-dsp i420_to_packed compute the same integers (hw_decode_rgb max |diff| 0 native, 640 wide, 301 tall); out == in is the plain conversion as before

Browse files at this commit

1b795c60c405e617e9af8390719388e4cd710c5dAhmet Kaan GümüşParent: 9876fb6
crates/ec-dsp/src/rgb.rsmodified+69 -18
@@ -100,7 +100,7 @@ pub const BT709_LIMITED: YuvToRgb = YuvToRgb {
100100 bu: 541,
101101 };
102102
103-/// Planar 8-bit I420 (`u` and `v` at half size, nearest chroma) to packed 4-byte pixels at `dst` x `dst` size, box-downscaled with the footprints of [`rgb_box_downscale`]; each channel is the rounded mean of the converted source pixels. Alpha is 255. `bgra` picks the byte order.
103+/// Planar 8-bit I420 (`u` and `v` at half size, bilinear chroma) to packed 4-byte pixels at `dw` x `dh`, downscaled by a separable Catmull-Rom kernel widened by the scale ratio ([`taps`]); out == in is the plain conversion. Alpha is 255. `bgra` picks the byte order.
104104 pub fn i420_to_packed(
105105 y: &[u8],
106106 u: &[u8],
@@ -117,27 +117,45 @@ pub fn i420_to_packed(
117117 );
118118 assert!(y.len() == w * h && u.len() == w * h / 4 && v.len() == w * h / 4);
119119 let cw = w / 2;
120+ let mut rgb = vec![[0i32; 3]; w * h];
121+ for sy in 0..h {
122+ for sx in 0..w {
123+ let yy = i32::from(y[sy * w + sx]) - 16;
124+ let uu = chroma_at(u, (cw, h / 2), sx, sy) - 128;
125+ let vv = chroma_at(v, (cw, h / 2), sx, sy) - 128;
126+ let r = ((k.ky * yy + k.rv * vv + 128) >> 8).clamp(0, 255);
127+ let g = ((k.ky * yy + k.gu * uu + k.gv * vv + 128) >> 8).clamp(0, 255);
128+ let b = ((k.ky * yy + k.bu * uu + 128) >> 8).clamp(0, 255);
129+ rgb[sy * w + sx] = [r, g, b];
130+ }
131+ }
132+ let xt: Vec<(i32, Vec<i32>)> = (0..dw).map(|o| taps(o, w, dw)).collect();
133+ let yt: Vec<(i32, Vec<i32>)> = (0..dh).map(|o| taps(o, h, dh)).collect();
134+ let mut rows = vec![[0i32; 3]; h * dw];
135+ for sy in 0..h {
136+ for (ox, (lo, wx)) in xt.iter().enumerate() {
137+ let mut s = [0i32; 3];
138+ for (i, &c) in wx.iter().enumerate() {
139+ let px = rgb[sy * w + edge(lo + i as i32, w)];
140+ for (s, p) in s.iter_mut().zip(px) {
141+ *s += c * p;
142+ }
143+ }
144+ rows[sy * dw + ox] = s;
145+ }
146+ }
120147 let mut out = vec![255u8; dw * dh * 4];
121- for oy in 0..dh {
122- let (y0, y1) = footprint(oy, h, dh);
148+ for (oy, (lo, wy)) in yt.iter().enumerate() {
123149 for ox in 0..dw {
124- let (x0, x1) = footprint(ox, w, dw);
125- let mut sum = [0u32; 3];
126- for sy in y0..y1 {
127- for sx in x0..x1 {
128- let yy = i32::from(y[sy * w + sx]) - 16;
129- let uu = chroma_at(u, (cw, h / 2), sx, sy) - 128;
130- let vv = chroma_at(v, (cw, h / 2), sx, sy) - 128;
131- let r = ((k.ky * yy + k.rv * vv + 128) >> 8).clamp(0, 255);
132- let g = ((k.ky * yy + k.gu * uu + k.gv * vv + 128) >> 8).clamp(0, 255);
133- let b = ((k.ky * yy + k.bu * uu + 128) >> 8).clamp(0, 255);
134- for (s, c) in sum.iter_mut().zip([r, g, b]) {
135- *s += c as u32;
136- }
150+ let mut s = [0i32; 3];
151+ for (j, &c) in wy.iter().enumerate() {
152+ let row = rows[edge(lo + j as i32, h) * dw + ox];
153+ for (s, p) in s.iter_mut().zip(row) {
154+ *s += c * p;
137155 }
138156 }
139- let n = ((x1 - x0) * (y1 - y0)) as u32;
140- let [r, g, b] = sum.map(|s| ((s + n / 2) / n) as u8);
157+ let [r, g, b] =
158+ s.map(|s| ((s + (1 << (2 * TAP_BITS - 1))) >> (2 * TAP_BITS)).clamp(0, 255) as u8);
141159 let o = (oy * dw + ox) * 4;
142160 let px = if bgra { [b, g, r] } else { [r, g, b] };
143161 out[o..o + 3].copy_from_slice(&px);
@@ -146,6 +164,39 @@ pub fn i420_to_packed(
146164 out
147165 }
148166
167+const TAP_BITS: i32 = 10;
168+
169+/// Catmull-Rom (a = -0.5) taps of output sample `o` scaling `n_in` to `n_out <= n_in` samples: support 2 * n_in / n_out source samples each side of the output centre, weights in Q12 normalised to sum to 1 << TAP_BITS (the rounding residue goes to the tap nearest the centre). Returns the first tap's index (may lie outside the picture: [`edge`] clamps) and the weights. The Vulkan shader nv12_to_rgb.comp computes the same integers.
170+fn taps(o: usize, n_in: usize, n_out: usize) -> (i32, Vec<i32>) {
171+ let (o, n, m) = (o as i32, n_in as i32, n_out as i32);
172+ let c2 = (2 * o + 1) * n - m;
173+ let centre = (c2 + m) / (2 * m);
174+ let reach = 2 * n / m + 2;
175+ let (lo, hi) = (centre - reach, centre + reach);
176+ let kernel = |i: i32| {
177+ let t = ((2 * m * i - c2).abs() * 4096 / (2 * n)).min(8192);
178+ let t2 = (t * t) >> 12;
179+ let t3 = (t2 * t) >> 12;
180+ if t < 4096 {
181+ (3 * t3 - 5 * t2 + 8192) >> 1
182+ } else {
183+ (-t3 + 5 * t2 - 8 * t + 16384) >> 1
184+ }
185+ };
186+ let k: Vec<i32> = (lo..=hi).map(kernel).collect();
187+ let sum: i32 = k.iter().sum();
188+ let mut wts: Vec<i32> = k
189+ .iter()
190+ .map(|&k| k.signum() * ((k.abs() * (1 << TAP_BITS) + (sum >> 1)) / sum))
191+ .collect();
192+ wts[(centre - lo) as usize] += (1 << TAP_BITS) - wts.iter().sum::<i32>();
193+ (lo, wts)
194+}
195+
196+fn edge(i: i32, n: usize) -> usize {
197+ i.clamp(0, n as i32 - 1) as usize
198+}
199+
149200 /// Bilinear 4:2:0 chroma at luma `(x, y)`: samples left-sited on even columns and centred between row pairs, as the RGB to I420 conversions write them.
150201 fn chroma_at(plane: &[u8], (cw, ch): (usize, usize), x: usize, y: usize) -> i32 {
151202 let j = x / 2;
shims/vk-video-ffi/shaders/nv12_to_rgb.compmodified+74 -12
@@ -62,24 +62,86 @@ ivec3 rgb_at(uint x, uint y) {
6262 return clamp(ivec3(r, g, b), 0, 255);
6363 }
6464
65+const int TAP_BITS = 10;
66+
67+struct Axis {
68+ int n;
69+ int m;
70+ int c2;
71+ int centre;
72+ int lo;
73+ int hi;
74+ int sum;
75+ int resid;
76+};
77+
78+int kernel(Axis a, int i) {
79+ int t = min(abs(2 * a.m * i - a.c2) * 4096 / (2 * a.n), 8192);
80+ int t2 = (t * t) >> 12;
81+ int t3 = (t2 * t) >> 12;
82+ return t < 4096
83+ ? (3 * t3 - 5 * t2 + 8192) >> 1
84+ : (-t3 + 5 * t2 - 8 * t + 16384) >> 1;
85+}
86+
87+int norm(Axis a, int k) {
88+ return sign(k) * ((abs(k) * (1 << TAP_BITS) + (a.sum >> 1)) / a.sum);
89+}
90+
91+Axis axis(uint o, uint n_in, uint n_out) {
92+ Axis a;
93+ a.n = int(n_in);
94+ a.m = int(n_out);
95+ a.c2 = (2 * int(o) + 1) * a.n - a.m;
96+ a.centre = (a.c2 + a.m) / (2 * a.m);
97+ int reach = 2 * a.n / a.m + 2;
98+ a.lo = a.centre - reach;
99+ a.hi = a.centre + reach;
100+ a.sum = 0;
101+ for (int i = a.lo; i <= a.hi; ++i) {
102+ a.sum += kernel(a, i);
103+ }
104+ int total = 0;
105+ for (int i = a.lo; i <= a.hi; ++i) {
106+ total += norm(a, kernel(a, i));
107+ }
108+ a.resid = (1 << TAP_BITS) - total;
109+ return a;
110+}
111+
112+int weight(Axis a, int i) {
113+ return norm(a, kernel(a, i)) + (i == a.centre ? a.resid : 0);
114+}
115+
116+uvec3 scaled(uint ox, uint oy) {
117+ Axis ax = axis(ox, pc.w, pc.out_w);
118+ Axis ay = axis(oy, pc.h, pc.out_h);
119+ ivec3 acc = ivec3(0);
120+ for (int j = ay.lo; j <= ay.hi; ++j) {
121+ int wy = weight(ay, j);
122+ if (wy == 0) {
123+ continue;
124+ }
125+ uint sy = uint(clamp(j, 0, int(pc.h) - 1));
126+ ivec3 row = ivec3(0);
127+ for (int i = ax.lo; i <= ax.hi; ++i) {
128+ int wx = weight(ax, i);
129+ if (wx != 0) {
130+ row += wx * rgb_at(uint(clamp(i, 0, int(pc.w) - 1)), sy);
131+ }
132+ }
133+ acc += wy * row;
134+ }
135+ return uvec3(clamp((acc + (1 << (2 * TAP_BITS - 1))) >> (2 * TAP_BITS), 0, 255));
136+}
137+
65138 void main() {
66139 uint ox = gl_GlobalInvocationID.x;
67140 uint oy = gl_GlobalInvocationID.y;
68141 if (ox >= pc.out_w || oy >= pc.out_h) {
69142 return;
70143 }
71- uint x0 = ox * pc.w / pc.out_w;
72- uint x1 = ((ox + 1u) * pc.w + pc.out_w - 1u) / pc.out_w;
73- uint y0 = oy * pc.h / pc.out_h;
74- uint y1 = ((oy + 1u) * pc.h + pc.out_h - 1u) / pc.out_h;
75- uvec3 sum = uvec3(0u);
76- for (uint y = y0; y < y1; ++y) {
77- for (uint x = x0; x < x1; ++x) {
78- sum += uvec3(rgb_at(x, y));
79- }
80- }
81- uint n = (x1 - x0) * (y1 - y0);
82- uvec3 c = (sum + uvec3(n / 2u)) / uvec3(n);
144+ uvec3 c = pc.w == pc.out_w && pc.h == pc.out_h ? uvec3(rgb_at(ox, oy)) : scaled(ox, oy);
83145 uint px = pc.bgra != 0u
84146 ? (c.b | (c.g << 8) | (c.r << 16))
85147 : (c.r | (c.g << 8) | (c.b << 16));
shims/vk-video-ffi/shaders/nv12_to_rgb.spvmodified+0 -0

Binary file changed