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