Skip to content

Commit f9b1f96

Browse files
committed
struct LossLess: Make Rav1dFrameHeader_segmentation::lossless a bit array.
1 parent 8ada302 commit f9b1f96

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

include/dav1d/headers.rs

+45-4
Original file line numberDiff line numberDiff line change
@@ -2096,6 +2096,48 @@ pub struct Dav1dFrameHeader_segmentation {
20962096
pub qidx: [u8; DAV1D_MAX_SEGMENTS as usize],
20972097
}
20982098

2099+
/// Like a `[bool; RAV1D_MAX_SEGMENTS as usize]`,
2100+
/// but the [`bool`]s are compressed into the bits of the [`u8`].
2101+
#[derive(Clone, Copy, Default)]
2102+
pub struct LossLess {
2103+
bits: u8,
2104+
}
2105+
2106+
impl LossLess {
2107+
pub const fn empty() -> Self {
2108+
Self { bits: 0 }
2109+
}
2110+
2111+
pub const fn get(&self, index: u8) -> bool {
2112+
const { assert!(u8::BITS >= RAV1D_MAX_SEGMENTS as _) };
2113+
self.bits & (1 << index) != 0
2114+
}
2115+
2116+
pub const fn all(&self) -> bool {
2117+
self.bits == ((1u64 << RAV1D_MAX_SEGMENTS) - 1) as u8
2118+
}
2119+
2120+
pub const fn to_array(&self) -> [bool; RAV1D_MAX_SEGMENTS as usize] {
2121+
let mut a = [false; RAV1D_MAX_SEGMENTS as usize];
2122+
let mut i = 0;
2123+
while i < RAV1D_MAX_SEGMENTS {
2124+
a[i as usize] = self.get(i);
2125+
i += 1;
2126+
}
2127+
a
2128+
}
2129+
2130+
pub fn from_array(a: [bool; RAV1D_MAX_SEGMENTS as usize]) -> Self {
2131+
let mut this = Self::empty();
2132+
let mut i = 0;
2133+
while i < RAV1D_MAX_SEGMENTS {
2134+
this.bits |= (a[i as usize] as u8) << i;
2135+
i += 1;
2136+
}
2137+
this
2138+
}
2139+
}
2140+
20992141
#[derive(Clone, Default)]
21002142
#[repr(C)]
21012143
pub struct Rav1dFrameHeader_segmentation {
@@ -2104,8 +2146,7 @@ pub struct Rav1dFrameHeader_segmentation {
21042146
pub temporal: u8,
21052147
pub update_data: u8,
21062148
pub seg_data: Rav1dSegmentationDataSet,
2107-
/// TODO compress `[bool; 8]` into `u8`.
2108-
pub lossless: [bool; RAV1D_MAX_SEGMENTS as usize],
2149+
pub lossless: LossLess,
21092150
pub qidx: [u8; RAV1D_MAX_SEGMENTS as usize],
21102151
}
21112152

@@ -2126,7 +2167,7 @@ impl From<Dav1dFrameHeader_segmentation> for Rav1dFrameHeader_segmentation {
21262167
temporal,
21272168
update_data,
21282169
seg_data: seg_data.into(),
2129-
lossless: lossless.map(|e| e != 0),
2170+
lossless: LossLess::from_array(lossless.map(|e| e != 0)),
21302171
qidx,
21312172
}
21322173
}
@@ -2149,7 +2190,7 @@ impl From<Rav1dFrameHeader_segmentation> for Dav1dFrameHeader_segmentation {
21492190
temporal,
21502191
update_data,
21512192
seg_data: seg_data.into(),
2152-
lossless: lossless.map(|e| e as u8),
2193+
lossless: lossless.to_array().map(|e| e as u8),
21532194
qidx,
21542195
}
21552196
}

src/decode.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ fn read_vartx_tree(
798798
let frame_hdr = &***f.frame_hdr.as_ref().unwrap();
799799
let txfm_mode = frame_hdr.txfm_mode;
800800
let uvtx;
801-
if b.skip == 0 && (frame_hdr.segmentation.lossless[b.seg_id as usize] || max_ytx == TX_4X4) {
801+
if b.skip == 0 && (frame_hdr.segmentation.lossless.get(b.seg_id) || max_ytx == TX_4X4) {
802802
uvtx = TX_4X4;
803803
max_ytx = uvtx;
804804
if txfm_mode == Rav1dTxfmMode::Switchable {
@@ -1683,7 +1683,7 @@ fn decode_b(
16831683
let uv_angle;
16841684
let cfl_alpha;
16851685
if has_chroma {
1686-
let cfl_allowed = if frame_hdr.segmentation.lossless[b.seg_id as usize] {
1686+
let cfl_allowed = if frame_hdr.segmentation.lossless.get(b.seg_id) {
16871687
cbw4 == 1 && cbh4 == 1
16881688
} else {
16891689
(cfl_allowed_mask & (1 << bs as u8)) != 0
@@ -1882,7 +1882,7 @@ fn decode_b(
18821882

18831883
let frame_hdr = f.frame_hdr();
18841884

1885-
let tx = if frame_hdr.segmentation.lossless[b.seg_id as usize] {
1885+
let tx = if frame_hdr.segmentation.lossless.get(b.seg_id) {
18861886
b.uvtx = TX_4X4;
18871887
b.uvtx
18881888
} else {
@@ -3075,7 +3075,7 @@ fn decode_b(
30753075
let tx_split = [tx_split0 as u16, tx_split1];
30763076
let mut ytx = max_ytx;
30773077
let mut uvtx = b.uvtx;
3078-
if frame_hdr.segmentation.lossless[b.seg_id as usize] {
3078+
if frame_hdr.segmentation.lossless.get(b.seg_id) {
30793079
ytx = TX_4X4;
30803080
uvtx = TX_4X4;
30813081
}

src/obu.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::include::dav1d::data::Rav1dData;
77
use crate::include::dav1d::dav1d::Rav1dDecodeFrameType;
88
use crate::include::dav1d::headers::DRav1d;
99
use crate::include::dav1d::headers::Dav1dSequenceHeader;
10+
use crate::include::dav1d::headers::LossLess;
1011
use crate::include::dav1d::headers::Rav1dAdaptiveBoolean;
1112
use crate::include::dav1d::headers::Rav1dChromaSamplePosition;
1213
use crate::include::dav1d::headers::Rav1dColorPrimaries;
@@ -1195,7 +1196,7 @@ fn parse_segmentation(
11951196
quant.yac
11961197
}
11971198
});
1198-
let lossless = array::from_fn(|i| qidx[i] == 0 && delta_lossless);
1199+
let lossless = LossLess::from_array(array::from_fn(|i| qidx[i] == 0 && delta_lossless));
11991200
Ok(Rav1dFrameHeader_segmentation {
12001201
enabled,
12011202
update_map,
@@ -2001,7 +2002,7 @@ fn parse_frame_hdr(
20012002
let tiling = parse_tiling(seqhdr, &size, &debug, gb)?;
20022003
let quant = parse_quant(seqhdr, &debug, gb);
20032004
let segmentation = parse_segmentation(state, primary_ref_frame, &refidx, &quant, &debug, gb)?;
2004-
let all_lossless = segmentation.lossless.iter().all(|&it| it);
2005+
let all_lossless = segmentation.lossless.all();
20052006
let delta = parse_delta(&quant, allow_intrabc, &debug, gb);
20062007
let loopfilter = parse_loopfilter(
20072008
state,

src/recon.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ fn decode_coefs<BD: BitDepth>(
524524
let ts = &f.ts[ts];
525525
let chroma = plane != 0;
526526
let frame_hdr = &***f.frame_hdr.as_ref().unwrap();
527-
let lossless = frame_hdr.segmentation.lossless[b.seg_id as usize];
527+
let lossless = frame_hdr.segmentation.lossless.get(b.seg_id);
528528
let t_dim = &dav1d_txfm_dimensions[tx as usize];
529529
let dbg = dbg_block_info && plane != 0 && false;
530530

0 commit comments

Comments
 (0)