forge
Explore
Log in

Don't you dare go Hollow

S13: dvbsub Depth and PageState enums replace the bit-count and state integers (values unchanged)

Browse files at this commit

a178dca25eb745c867c1692a360b3d47848414d0Ahmet Kaan GümüşParent: 165954c
crates/ec-dvbsub/src/lib.rsmodified+52 -26
@@ -43,14 +43,40 @@ pub struct DvbCue {
4343 pub timeout_ms: u32,
4444 /// No region is shown: this set clears the screen.
4545 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,
4848 /// The regions shown: x, y, width, height.
4949 pub regions: Vec<(u32, u32, u32, u32)>,
5050 }
5151
5252 type Rgba = [u8; 4];
5353
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+
5480 struct Clut {
5581 c2: [Rgba; 4],
5682 c4: [Rgba; 16],
@@ -60,7 +86,7 @@ struct Clut {
6086 struct Region {
6187 width: u32,
6288 height: u32,
63- depth: u8, // bits: 2, 4, 8
89+ depth: Depth,
6490 clut: u8,
6591 pix: Vec<u8>,
6692 objects: Vec<(u16, u32, u32)>, // id, x, y
@@ -73,7 +99,7 @@ pub struct DvbSubDecoder {
7399 cluts: HashMap<u8, Clut>,
74100 page: Vec<(u8, u32, u32)>,
75101 timeout: u8,
76- page_state: u8,
102+ page_state: PageState,
77103 display: (u32, u32),
78104 ready: VecDeque<DvbCue>,
79105 pts: Option<Timestamp>,
@@ -180,12 +206,12 @@ impl Bits<'_> {
180206
181207 /// One pixel-data sub-block's code string, calling `put(count, code)` for each run. Returns the
182208 /// 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 {
184210 let mut b = Bits { d, pos: 0 };
185211 loop {
186212 let r = (|| -> Option<Option<(usize, u8)>> {
187213 Some(match depth {
188- 2 => {
214+ Depth::Bits2 => {
189215 let v = b.get(2)?;
190216 if v != 0 {
191217 Some((1, v as u8))
@@ -209,7 +235,7 @@ fn code_string(d: &[u8], depth: u8, mut put: impl FnMut(usize, u8)) -> usize {
209235 }
210236 }
211237 }
212- 4 => {
238+ Depth::Bits4 => {
213239 let v = b.get(4)?;
214240 if v != 0 {
215241 Some((1, v as u8))
@@ -234,7 +260,7 @@ fn code_string(d: &[u8], depth: u8, mut put: impl FnMut(usize, u8)) -> usize {
234260 }
235261 }
236262 }
237- _ => {
263+ Depth::Bits8 => {
238264 let v = b.get(8)?;
239265 if v != 0 {
240266 Some((1, v as u8))
@@ -268,7 +294,7 @@ impl DvbSubDecoder {
268294 cluts: HashMap::new(),
269295 page: Vec::new(),
270296 timeout: 0,
271- page_state: 0,
297+ page_state: PageState::Normal,
272298 display: (720, 576),
273299 ready: VecDeque::new(),
274300 pts: None,
@@ -328,8 +354,8 @@ impl DvbSubDecoder {
328354 return Err(Error::corrupt("dvbsub: short page composition"));
329355 }
330356 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 {
333359 // Acquisition point / mode change: nothing from before is kept.
334360 self.regions.clear();
335361 self.cluts.clear();
@@ -357,16 +383,16 @@ impl DvbSubDecoder {
357383 return Err(Error::corrupt("dvbsub: region size out of range"));
358384 }
359385 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,
363389 _ => return Err(Error::corrupt("dvbsub: bad region depth")),
364390 };
365391 let clut = b[7];
366392 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],
370396 };
371397 let mut pix = vec![0u8; (w * h) as usize];
372398 if fill {
@@ -486,9 +512,9 @@ impl DvbSubDecoder {
486512 }
487513 let code = usize::from(r.pix[(row * r.width + col) as usize]);
488514 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],
492518 };
493519 let o = ty as usize * stride + tx as usize * 4;
494520 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) {
528554 match kind {
529555 0x10..=0x12 => {
530556 let src_depth = match kind {
531- 0x10 => 2,
532- 0x11 => 4,
533- _ => 8,
557+ 0x10 => Depth::Bits2,
558+ 0x11 => Depth::Bits4,
559+ _ => Depth::Bits8,
534560 };
535561 let depth = region.depth;
536562 let (m24c, m28c, m48c) = (m24, m28, m48);
537563 let mapped = |c: u8| -> u8 {
538564 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)],
542568 _ => c,
543569 }
544570 };