From b259c6104553ff0ddf49fea6b6f568ac28aed848 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Tue, 24 Oct 2023 02:20:56 -0700 Subject: [PATCH] `static ss_size_mul`: Make an `EnumMap`. --- include/dav1d/headers.rs | 9 +++++++++ src/decode.rs | 25 +++++++++++++++---------- src/enum_map.rs | 5 +---- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/include/dav1d/headers.rs b/include/dav1d/headers.rs index d4eeee3fb..23d5f4117 100644 --- a/include/dav1d/headers.rs +++ b/include/dav1d/headers.rs @@ -1,3 +1,4 @@ +use crate::src::enum_map::EnumKey; use std::ffi::c_int; use std::ffi::c_uint; use std::ops::BitAnd; @@ -208,6 +209,14 @@ pub(crate) enum Rav1dPixelLayout { I444, } +impl EnumKey<4> for Rav1dPixelLayout { + const VALUES: [Self; 4] = [Self::I400, Self::I420, Self::I422, Self::I444]; + + fn as_usize(self) -> usize { + self as usize + } +} + impl BitAnd for Rav1dPixelLayout { type Output = bool; diff --git a/src/decode.rs b/src/decode.rs index 189198bc8..82ee4d9e6 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -42,6 +42,9 @@ use crate::src::ctx::CaseSet; use crate::src::data::rav1d_data_props_copy; use crate::src::data::rav1d_data_unref_internal; use crate::src::dequant_tables::dav1d_dq_tbl; +use crate::src::enum_map::enum_map; +use crate::src::enum_map::DefaultValue; +use crate::src::enum_map::EnumMap; use crate::src::env::av1_get_bwd_ref_1_ctx; use crate::src::env::av1_get_bwd_ref_ctx; use crate::src::env::av1_get_fwd_ref_1_ctx; @@ -3809,15 +3812,17 @@ fn reset_context(ctx: &mut BlockContext, keyframe: bool, pass: c_int) { ctx.pal_sz.0.fill(0); } +impl DefaultValue for [u8; 2] { + const DEFAULT: Self = [0; 2]; +} + /// `{ Y+U+V, Y+U } * 4` -static ss_size_mul: [[u8; 2]; 4] = { - let mut a = [[0; 2]; 4]; - a[Rav1dPixelLayout::I400 as usize] = [4, 4]; - a[Rav1dPixelLayout::I420 as usize] = [6, 5]; - a[Rav1dPixelLayout::I422 as usize] = [8, 6]; - a[Rav1dPixelLayout::I444 as usize] = [12, 8]; - a -}; +static ss_size_mul: EnumMap = enum_map!(Rav1dPixelLayout => [u8; 2]; match key { + I400 => [4, 4], + I420 => [6, 5], + I422 => [8, 6], + I444 => [12, 8], +}); unsafe fn setup_tile( ts: &mut Rav1dTileState, @@ -3834,7 +3839,7 @@ unsafe fn setup_tile( let row_sb_end = (*f.frame_hdr).tiling.row_start_sb[tile_row + 1] as c_int; let sb_shift = f.sb_shift; - let size_mul = &ss_size_mul[f.cur.p.layout as usize]; + let size_mul = &ss_size_mul[f.cur.p.layout]; for p in 0..2 { ts.frame_thread[p].pal_idx = if !(f.frame_thread.pal_idx).is_null() { f.frame_thread @@ -4290,7 +4295,7 @@ pub(crate) unsafe fn rav1d_decode_frame_init(f: &mut Rav1dFrameContext) -> Rav1d } let num_sb128 = f.sb128w * f.sb128h; - let size_mul = &ss_size_mul[f.cur.p.layout as usize]; + let size_mul = &ss_size_mul[f.cur.p.layout]; let hbd = ((*f.seq_hdr).hbd != 0) as c_int; if c.n_fc > 1 { let mut tile_idx = 0; diff --git a/src/enum_map.rs b/src/enum_map.rs index c74ff568d..0cc6d96be 100644 --- a/src/enum_map.rs +++ b/src/enum_map.rs @@ -31,7 +31,6 @@ where { /// Create an [`EnumMap`] from an existing array /// where the array's indices correspond to `K`'s values `as usize`. - #[allow(dead_code)] pub const fn new(array: [V; N]) -> Self { Self { array, @@ -46,7 +45,7 @@ where V: DefaultValue, { /// Create an [`EnumMap`] with default values when `V: ` [`DefaultValue`]. - #[allow(dead_code)] + #[allow(dead_code)] // TODO(kkysen) remove when used pub const fn default() -> Self { Self { array: [V::DEFAULT; N], @@ -87,7 +86,6 @@ where /// but it also doesn't yet work in `const` contexts. /// /// [`MaybeUninit`]: std::mem::MaybeUninit -#[allow(unused_macros)] macro_rules! enum_map { ($K:ty => $V:ty; match key { $($t:tt)* }) => {{ use $crate::src::enum_map::EnumKey; @@ -106,5 +104,4 @@ macro_rules! enum_map { }}; } -#[allow(unused_imports)] pub(crate) use enum_map;