forge
Explore
Log in

Don't you dare go Hollow

wip(hbd): thread real bit_depth through dequant + deblock filter4 centering

Browse files at this commit

386939c2401693f86953690f1d4631d95f319084Ahmet Kaan GümüşParent: f49a7af
crates/ec-av1/src/decode.rsmodified+23 -14
@@ -92,6 +92,11 @@ thread_local! {
92 pub(crate) fn sample_max() -> i32 {92 pub(crate) fn sample_max() -> i32 {
93 (1i32 << BIT_DEPTH.with(std::cell::Cell::get)) - 193 (1i32 << BIT_DEPTH.with(std::cell::Cell::get)) - 1
94 }94 }
95+/// The current stream's bit depth, for callers (e.g. dequant) that need the
96+/// raw value rather than the derived clamp bound.
97+pub(crate) fn bit_depth() -> u8 {
98+ BIT_DEPTH.with(std::cell::Cell::get)
99+}
95 /// Sets [`BIT_DEPTH`] for the current thread, called once per frame from100 /// Sets [`BIT_DEPTH`] for the current thread, called once per frame from
96 /// [`crate::stream`]'s decode loop.101 /// [`crate::stream`]'s decode loop.
97 pub(crate) fn set_bit_depth(bit_depth: u8) {102 pub(crate) fn set_bit_depth(bit_depth: u8) {
@@ -3210,7 +3215,7 @@ fn decode_block_rect(
3210 &luma_levels,3215 &luma_levels,
3211 bw,3216 bw,
3212 bh,3217 bh,
3213- 8,3218+ crate::decode::bit_depth(),
3214 CURRENT_Q_IDX.with(|c| c.get()),3219 CURRENT_Q_IDX.with(|c| c.get()),
3215 luma_tx_type,3220 luma_tx_type,
3216 );3221 );
@@ -3249,7 +3254,7 @@ fn decode_block_rect(
3249 &u_levels,3254 &u_levels,
3250 chroma_w,3255 chroma_w,
3251 chroma_h,3256 chroma_h,
3252- 8,3257+ crate::decode::bit_depth(),
3253 CURRENT_Q_IDX.with(|c| c.get()),3258 CURRENT_Q_IDX.with(|c| c.get()),
3254 u_tx_type,3259 u_tx_type,
3255 );3260 );
@@ -3287,7 +3292,7 @@ fn decode_block_rect(
3287 &v_levels,3292 &v_levels,
3288 chroma_w,3293 chroma_w,
3289 chroma_h,3294 chroma_h,
3290- 8,3295+ crate::decode::bit_depth(),
3291 CURRENT_Q_IDX.with(|c| c.get()),3296 CURRENT_Q_IDX.with(|c| c.get()),
3292 v_tx_type,3297 v_tx_type,
3293 );3298 );
@@ -3498,7 +3503,7 @@ fn decode_block_rect64(
3498 &luma_levels,3503 &luma_levels,
3499 bw,3504 bw,
3500 bh,3505 bh,
3501- 8,3506+ crate::decode::bit_depth(),
3502 i32::from(base_q_idx),3507 i32::from(base_q_idx),
3503 luma_tx_type,3508 luma_tx_type,
3504 );3509 );
@@ -3543,7 +3548,7 @@ fn decode_block_rect64(
3543 &u_levels,3548 &u_levels,
3544 chroma_w,3549 chroma_w,
3545 chroma_h,3550 chroma_h,
3546- 8,3551+ crate::decode::bit_depth(),
3547 i32::from(base_q_idx),3552 i32::from(base_q_idx),
3548 u_tx_type,3553 u_tx_type,
3549 );3554 );
@@ -3584,7 +3589,7 @@ fn decode_block_rect64(
3584 &v_levels,3589 &v_levels,
3585 chroma_w,3590 chroma_w,
3586 chroma_h,3591 chroma_h,
3587- 8,3592+ crate::decode::bit_depth(),
3588 i32::from(base_q_idx),3593 i32::from(base_q_idx),
3589 v_tx_type,3594 v_tx_type,
3590 );3595 );
@@ -4305,7 +4310,7 @@ fn read_plane(
4305 }4310 }
4306 full4311 full
4307 };4312 };
4308- let residual = dequant_and_inverse_typed(&levels, side, 8, CURRENT_Q_IDX.with(|c| c.get()), tx_type);4313+ let residual = dequant_and_inverse_typed(&levels, side, bit_depth(), CURRENT_Q_IDX.with(|c| c.get()), tx_type);
4309 if std::env::var_os("EC_AV1_TRACE").is_some() {4314 if std::env::var_os("EC_AV1_TRACE").is_some() {
4310 eprintln!(4315 eprintln!(
4311 "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}",4316 "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}",
@@ -5509,7 +5514,10 @@ fn deblock_thresholds(level: i32, sharpness: u8) -> (i32, i32, i32) {
5509 }5514 }
5510 5515
5511 fn sclamp(v: i32) -> i32 {5516 fn sclamp(v: i32) -> i32 {
5512- v.clamp(-128, 127)5517+ // libaom's `signed_char_clamp_high`: the clamp range scales with
5518+ // `128 << (bit_depth - 8)` alongside the centering constant below.
5519+ let scale = 1i32 << (bit_depth() - 8);
5520+ v.clamp(-128 * scale, 128 * scale - 1)
5513 }5521 }
5514 5522
5515 fn round_pow2(v: i32, n: u32) -> i32 {5523 fn round_pow2(v: i32, n: u32) -> i32 {
@@ -5523,16 +5531,17 @@ fn filter4(mask: bool, thresh: i32, p1: i32, p0: i32, q0: i32, q1: i32) -> [i32;
5523 return [p1, p0, q0, q1];5531 return [p1, p0, q0, q1];
5524 }5532 }
5525 let hev = (p1 - p0).abs() > thresh || (q1 - q0).abs() > thresh;5533 let hev = (p1 - p0).abs() > thresh || (q1 - q0).abs() > thresh;
5526- let (ps1, ps0, qs0, qs1) = (p1 - 128, p0 - 128, q0 - 128, q1 - 128);5534+ let centre = 128i32 << (bit_depth() - 8);
5535+ let (ps1, ps0, qs0, qs1) = (p1 - centre, p0 - centre, q0 - centre, q1 - centre);
5527 let outer = if hev { sclamp(ps1 - qs1) } else { 0 };5536 let outer = if hev { sclamp(ps1 - qs1) } else { 0 };
5528 let filter = sclamp(outer + 3 * (qs0 - ps0));5537 let filter = sclamp(outer + 3 * (qs0 - ps0));
5529 let filter1 = sclamp(filter + 4) >> 3;5538 let filter1 = sclamp(filter + 4) >> 3;
5530 let filter2 = sclamp(filter + 3) >> 3;5539 let filter2 = sclamp(filter + 3) >> 3;
5531- let oq0 = sclamp(qs0 - filter1) + 128;5540+ let oq0 = sclamp(qs0 - filter1) + centre;
5532- let op0 = sclamp(ps0 + filter2) + 128;5541+ let op0 = sclamp(ps0 + filter2) + centre;
5533 let tap = if hev { 0 } else { round_pow2(filter1, 1) };5542 let tap = if hev { 0 } else { round_pow2(filter1, 1) };
5534- let oq1 = sclamp(qs1 - tap) + 128;5543+ let oq1 = sclamp(qs1 - tap) + centre;
5535- let op1 = sclamp(ps1 + tap) + 128;5544+ let op1 = sclamp(ps1 + tap) + centre;
5536 [op1, op0, oq0, oq1]5545 [op1, op0, oq0, oq1]
5537 }5546 }
5538 5547
@@ -7178,7 +7187,7 @@ fn read_inter_plane(
7178 default_tx_type,7187 default_tx_type,
7179 None,7188 None,
7180 )?;7189 )?;
7181- let residual = dequant_and_inverse_typed(&grid, side, 8, CURRENT_Q_IDX.with(|c| c.get()), tx_type);7190+ let residual = dequant_and_inverse_typed(&grid, side, bit_depth(), CURRENT_Q_IDX.with(|c| c.get()), tx_type);
7182 plane.reconstruct_mc(x, y, side, prediction, &residual);7191 plane.reconstruct_mc(x, y, side, prediction, &residual);
7183 Ok((grid, tx_type))7192 Ok((grid, tx_type))
7184 }7193 }