diff --git a/crates/ec-dvbsub/src/lib.rs b/crates/ec-dvbsub/src/lib.rs index f314302c..a768f427 100644 --- a/crates/ec-dvbsub/src/lib.rs +++ b/crates/ec-dvbsub/src/lib.rs @@ -43,14 +43,40 @@ pub struct DvbCue { pub timeout_ms: u32, /// No region is shown: this set clears the screen. pub empty: bool, - /// Page state: 0 normal case, 1 acquisition point, 2 mode change. - pub page_state: u8, + /// Page state: normal case, acquisition point, mode change. + pub page_state: PageState, /// The regions shown: x, y, width, height. pub regions: Vec<(u32, u32, u32, u32)>, } type Rgba = [u8; 4]; +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub enum PageState { + Normal, + Acquisition, + ModeChange, + Reserved, +} + +impl PageState { + fn from_bits(v: u8) -> PageState { + match v { + 0 => PageState::Normal, + 1 => PageState::Acquisition, + 2 => PageState::ModeChange, + _ => PageState::Reserved, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +enum Depth { + Bits2, + Bits4, + Bits8, +} + struct Clut { c2: [Rgba; 4], c4: [Rgba; 16], @@ -60,7 +86,7 @@ struct Clut { struct Region { width: u32, height: u32, - depth: u8, // bits: 2, 4, 8 + depth: Depth, clut: u8, pix: Vec, objects: Vec<(u16, u32, u32)>, // id, x, y @@ -73,7 +99,7 @@ pub struct DvbSubDecoder { cluts: HashMap, page: Vec<(u8, u32, u32)>, timeout: u8, - page_state: u8, + page_state: PageState, display: (u32, u32), ready: VecDeque, pts: Option, @@ -180,12 +206,12 @@ impl Bits<'_> { /// One pixel-data sub-block's code string, calling `put(count, code)` for each run. Returns the /// bytes the string took (through its end code and byte alignment; all of `d` if truncated). -fn code_string(d: &[u8], depth: u8, mut put: impl FnMut(usize, u8)) -> usize { +fn code_string(d: &[u8], depth: Depth, mut put: impl FnMut(usize, u8)) -> usize { let mut b = Bits { d, pos: 0 }; loop { let r = (|| -> Option> { Some(match depth { - 2 => { + Depth::Bits2 => { let v = b.get(2)?; if v != 0 { Some((1, v as u8)) @@ -209,7 +235,7 @@ fn code_string(d: &[u8], depth: u8, mut put: impl FnMut(usize, u8)) -> usize { } } } - 4 => { + Depth::Bits4 => { let v = b.get(4)?; if v != 0 { Some((1, v as u8)) @@ -234,7 +260,7 @@ fn code_string(d: &[u8], depth: u8, mut put: impl FnMut(usize, u8)) -> usize { } } } - _ => { + Depth::Bits8 => { let v = b.get(8)?; if v != 0 { Some((1, v as u8)) @@ -268,7 +294,7 @@ impl DvbSubDecoder { cluts: HashMap::new(), page: Vec::new(), timeout: 0, - page_state: 0, + page_state: PageState::Normal, display: (720, 576), ready: VecDeque::new(), pts: None, @@ -328,8 +354,8 @@ impl DvbSubDecoder { return Err(Error::corrupt("dvbsub: short page composition")); } self.timeout = b[0]; - self.page_state = (b[1] >> 2) & 3; - if self.page_state != 0 { + self.page_state = PageState::from_bits((b[1] >> 2) & 3); + if self.page_state != PageState::Normal { // Acquisition point / mode change: nothing from before is kept. self.regions.clear(); self.cluts.clear(); @@ -357,16 +383,16 @@ impl DvbSubDecoder { return Err(Error::corrupt("dvbsub: region size out of range")); } let depth = match (b[6] >> 2) & 7 { - 1 => 2, - 2 => 4, - 3 => 8, + 1 => Depth::Bits2, + 2 => Depth::Bits4, + 3 => Depth::Bits8, _ => return Err(Error::corrupt("dvbsub: bad region depth")), }; let clut = b[7]; let bg = match depth { - 2 => (b[9] >> 2) & 3, - 4 => b[9] >> 4, - _ => b[8], + Depth::Bits2 => (b[9] >> 2) & 3, + Depth::Bits4 => b[9] >> 4, + Depth::Bits8 => b[8], }; let mut pix = vec![0u8; (w * h) as usize]; if fill { @@ -486,9 +512,9 @@ impl DvbSubDecoder { } let code = usize::from(r.pix[(row * r.width + col) as usize]); let c = match r.depth { - 2 => clut.c2[code & 3], - 4 => clut.c4[code & 15], - _ => clut.c8[code], + Depth::Bits2 => clut.c2[code & 3], + Depth::Bits4 => clut.c4[code & 15], + Depth::Bits8 => clut.c8[code], }; let o = ty as usize * stride + tx as usize * 4; px[o..o + 4].copy_from_slice(&c); @@ -528,17 +554,17 @@ fn paint_field(region: &mut Region, data: &[u8], x: u32, y: u32, field: u32) { match kind { 0x10..=0x12 => { let src_depth = match kind { - 0x10 => 2, - 0x11 => 4, - _ => 8, + 0x10 => Depth::Bits2, + 0x11 => Depth::Bits4, + _ => Depth::Bits8, }; let depth = region.depth; let (m24c, m28c, m48c) = (m24, m28, m48); let mapped = |c: u8| -> u8 { match (src_depth, depth) { - (2, 4) => m24c[usize::from(c & 3)], - (2, 8) => m28c[usize::from(c & 3)], - (4, 8) => m48c[usize::from(c & 15)], + (Depth::Bits2, Depth::Bits4) => m24c[usize::from(c & 3)], + (Depth::Bits2, Depth::Bits8) => m28c[usize::from(c & 3)], + (Depth::Bits4, Depth::Bits8) => m48c[usize::from(c & 15)], _ => c, } };