S13: dvbsub Depth and PageState enums replace the bit-count and state integers (values unchanged)
Browse files at this commita178dca25eb745c867c1692a360b3d47848414d0Parent: 165954c1 changed file +52 -26
- crates/ec-dvbsub/src/lib.rs +52 -26
crates/ec-dvbsub/src/lib.rsmodified+52 -26
| @@ -43,14 +43,40 @@ pub struct DvbCue { | |||
| 43 | pub timeout_ms: u32, | 43 | pub timeout_ms: u32, |
| 44 | /// No region is shown: this set clears the screen. | 44 | /// No region is shown: this set clears the screen. |
| 45 | pub empty: bool, | 45 | pub empty: bool, |
| 46 | - /// Page state: 0 normal case, 1 acquisition point, 2 mode change. | 46 | + /// Page state: normal case, acquisition point, mode change. |
| 47 | - pub page_state: u8, | 47 | + pub page_state: PageState, |
| 48 | /// The regions shown: x, y, width, height. | 48 | /// The regions shown: x, y, width, height. |
| 49 | pub regions: Vec<(u32, u32, u32, u32)>, | 49 | pub regions: Vec<(u32, u32, u32, u32)>, |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | type Rgba = [u8; 4]; | 52 | type Rgba = [u8; 4]; |
| 53 | 53 | ||
| 54 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] | ||
| 55 | +pub enum PageState { | ||
| 56 | + Normal, | ||
| 57 | + Acquisition, | ||
| 58 | + ModeChange, | ||
| 59 | + Reserved, | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +impl PageState { | ||
| 63 | + fn from_bits(v: u8) -> PageState { | ||
| 64 | + match v { | ||
| 65 | + 0 => PageState::Normal, | ||
| 66 | + 1 => PageState::Acquisition, | ||
| 67 | + 2 => PageState::ModeChange, | ||
| 68 | + _ => PageState::Reserved, | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] | ||
| 74 | +enum Depth { | ||
| 75 | + Bits2, | ||
| 76 | + Bits4, | ||
| 77 | + Bits8, | ||
| 78 | +} | ||
| 79 | + | ||
| 54 | struct Clut { | 80 | struct Clut { |
| 55 | c2: [Rgba; 4], | 81 | c2: [Rgba; 4], |
| 56 | c4: [Rgba; 16], | 82 | c4: [Rgba; 16], |
| @@ -60,7 +86,7 @@ struct Clut { | |||
| 60 | struct Region { | 86 | struct Region { |
| 61 | width: u32, | 87 | width: u32, |
| 62 | height: u32, | 88 | height: u32, |
| 63 | - depth: u8, // bits: 2, 4, 8 | 89 | + depth: Depth, |
| 64 | clut: u8, | 90 | clut: u8, |
| 65 | pix: Vec<u8>, | 91 | pix: Vec<u8>, |
| 66 | objects: Vec<(u16, u32, u32)>, // id, x, y | 92 | objects: Vec<(u16, u32, u32)>, // id, x, y |
| @@ -73,7 +99,7 @@ pub struct DvbSubDecoder { | |||
| 73 | cluts: HashMap<u8, Clut>, | 99 | cluts: HashMap<u8, Clut>, |
| 74 | page: Vec<(u8, u32, u32)>, | 100 | page: Vec<(u8, u32, u32)>, |
| 75 | timeout: u8, | 101 | timeout: u8, |
| 76 | - page_state: u8, | 102 | + page_state: PageState, |
| 77 | display: (u32, u32), | 103 | display: (u32, u32), |
| 78 | ready: VecDeque<DvbCue>, | 104 | ready: VecDeque<DvbCue>, |
| 79 | pts: Option<Timestamp>, | 105 | pts: Option<Timestamp>, |
| @@ -180,12 +206,12 @@ impl Bits<'_> { | |||
| 180 | 206 | ||
| 181 | /// One pixel-data sub-block's code string, calling `put(count, code)` for each run. Returns the | 207 | /// One pixel-data sub-block's code string, calling `put(count, code)` for each run. Returns the |
| 182 | /// bytes the string took (through its end code and byte alignment; all of `d` if truncated). | 208 | /// bytes the string took (through its end code and byte alignment; all of `d` if truncated). |
| 183 | -fn code_string(d: &[u8], depth: u8, mut put: impl FnMut(usize, u8)) -> usize { | 209 | +fn code_string(d: &[u8], depth: Depth, mut put: impl FnMut(usize, u8)) -> usize { |
| 184 | let mut b = Bits { d, pos: 0 }; | 210 | let mut b = Bits { d, pos: 0 }; |
| 185 | loop { | 211 | loop { |
| 186 | let r = (|| -> Option<Option<(usize, u8)>> { | 212 | let r = (|| -> Option<Option<(usize, u8)>> { |
| 187 | Some(match depth { | 213 | Some(match depth { |
| 188 | - 2 => { | 214 | + Depth::Bits2 => { |
| 189 | let v = b.get(2)?; | 215 | let v = b.get(2)?; |
| 190 | if v != 0 { | 216 | if v != 0 { |
| 191 | Some((1, v as u8)) | 217 | Some((1, v as u8)) |
| @@ -209,7 +235,7 @@ fn code_string(d: &[u8], depth: u8, mut put: impl FnMut(usize, u8)) -> usize { | |||
| 209 | } | 235 | } |
| 210 | } | 236 | } |
| 211 | } | 237 | } |
| 212 | - 4 => { | 238 | + Depth::Bits4 => { |
| 213 | let v = b.get(4)?; | 239 | let v = b.get(4)?; |
| 214 | if v != 0 { | 240 | if v != 0 { |
| 215 | Some((1, v as u8)) | 241 | Some((1, v as u8)) |
| @@ -234,7 +260,7 @@ fn code_string(d: &[u8], depth: u8, mut put: impl FnMut(usize, u8)) -> usize { | |||
| 234 | } | 260 | } |
| 235 | } | 261 | } |
| 236 | } | 262 | } |
| 237 | - _ => { | 263 | + Depth::Bits8 => { |
| 238 | let v = b.get(8)?; | 264 | let v = b.get(8)?; |
| 239 | if v != 0 { | 265 | if v != 0 { |
| 240 | Some((1, v as u8)) | 266 | Some((1, v as u8)) |
| @@ -268,7 +294,7 @@ impl DvbSubDecoder { | |||
| 268 | cluts: HashMap::new(), | 294 | cluts: HashMap::new(), |
| 269 | page: Vec::new(), | 295 | page: Vec::new(), |
| 270 | timeout: 0, | 296 | timeout: 0, |
| 271 | - page_state: 0, | 297 | + page_state: PageState::Normal, |
| 272 | display: (720, 576), | 298 | display: (720, 576), |
| 273 | ready: VecDeque::new(), | 299 | ready: VecDeque::new(), |
| 274 | pts: None, | 300 | pts: None, |
| @@ -328,8 +354,8 @@ impl DvbSubDecoder { | |||
| 328 | return Err(Error::corrupt("dvbsub: short page composition")); | 354 | return Err(Error::corrupt("dvbsub: short page composition")); |
| 329 | } | 355 | } |
| 330 | self.timeout = b[0]; | 356 | self.timeout = b[0]; |
| 331 | - self.page_state = (b[1] >> 2) & 3; | 357 | + self.page_state = PageState::from_bits((b[1] >> 2) & 3); |
| 332 | - if self.page_state != 0 { | 358 | + if self.page_state != PageState::Normal { |
| 333 | // Acquisition point / mode change: nothing from before is kept. | 359 | // Acquisition point / mode change: nothing from before is kept. |
| 334 | self.regions.clear(); | 360 | self.regions.clear(); |
| 335 | self.cluts.clear(); | 361 | self.cluts.clear(); |
| @@ -357,16 +383,16 @@ impl DvbSubDecoder { | |||
| 357 | return Err(Error::corrupt("dvbsub: region size out of range")); | 383 | return Err(Error::corrupt("dvbsub: region size out of range")); |
| 358 | } | 384 | } |
| 359 | let depth = match (b[6] >> 2) & 7 { | 385 | let depth = match (b[6] >> 2) & 7 { |
| 360 | - 1 => 2, | 386 | + 1 => Depth::Bits2, |
| 361 | - 2 => 4, | 387 | + 2 => Depth::Bits4, |
| 362 | - 3 => 8, | 388 | + 3 => Depth::Bits8, |
| 363 | _ => return Err(Error::corrupt("dvbsub: bad region depth")), | 389 | _ => return Err(Error::corrupt("dvbsub: bad region depth")), |
| 364 | }; | 390 | }; |
| 365 | let clut = b[7]; | 391 | let clut = b[7]; |
| 366 | let bg = match depth { | 392 | let bg = match depth { |
| 367 | - 2 => (b[9] >> 2) & 3, | 393 | + Depth::Bits2 => (b[9] >> 2) & 3, |
| 368 | - 4 => b[9] >> 4, | 394 | + Depth::Bits4 => b[9] >> 4, |
| 369 | - _ => b[8], | 395 | + Depth::Bits8 => b[8], |
| 370 | }; | 396 | }; |
| 371 | let mut pix = vec![0u8; (w * h) as usize]; | 397 | let mut pix = vec![0u8; (w * h) as usize]; |
| 372 | if fill { | 398 | if fill { |
| @@ -486,9 +512,9 @@ impl DvbSubDecoder { | |||
| 486 | } | 512 | } |
| 487 | let code = usize::from(r.pix[(row * r.width + col) as usize]); | 513 | let code = usize::from(r.pix[(row * r.width + col) as usize]); |
| 488 | let c = match r.depth { | 514 | let c = match r.depth { |
| 489 | - 2 => clut.c2[code & 3], | 515 | + Depth::Bits2 => clut.c2[code & 3], |
| 490 | - 4 => clut.c4[code & 15], | 516 | + Depth::Bits4 => clut.c4[code & 15], |
| 491 | - _ => clut.c8[code], | 517 | + Depth::Bits8 => clut.c8[code], |
| 492 | }; | 518 | }; |
| 493 | let o = ty as usize * stride + tx as usize * 4; | 519 | let o = ty as usize * stride + tx as usize * 4; |
| 494 | px[o..o + 4].copy_from_slice(&c); | 520 | 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) { | |||
| 528 | match kind { | 554 | match kind { |
| 529 | 0x10..=0x12 => { | 555 | 0x10..=0x12 => { |
| 530 | let src_depth = match kind { | 556 | let src_depth = match kind { |
| 531 | - 0x10 => 2, | 557 | + 0x10 => Depth::Bits2, |
| 532 | - 0x11 => 4, | 558 | + 0x11 => Depth::Bits4, |
| 533 | - _ => 8, | 559 | + _ => Depth::Bits8, |
| 534 | }; | 560 | }; |
| 535 | let depth = region.depth; | 561 | let depth = region.depth; |
| 536 | let (m24c, m28c, m48c) = (m24, m28, m48); | 562 | let (m24c, m28c, m48c) = (m24, m28, m48); |
| 537 | let mapped = |c: u8| -> u8 { | 563 | let mapped = |c: u8| -> u8 { |
| 538 | match (src_depth, depth) { | 564 | match (src_depth, depth) { |
| 539 | - (2, 4) => m24c[usize::from(c & 3)], | 565 | + (Depth::Bits2, Depth::Bits4) => m24c[usize::from(c & 3)], |
| 540 | - (2, 8) => m28c[usize::from(c & 3)], | 566 | + (Depth::Bits2, Depth::Bits8) => m28c[usize::from(c & 3)], |
| 541 | - (4, 8) => m48c[usize::from(c & 15)], | 567 | + (Depth::Bits4, Depth::Bits8) => m48c[usize::from(c & 15)], |
| 542 | _ => c, | 568 | _ => c, |
| 543 | } | 569 | } |
| 544 | }; | 570 | }; |