| 1 | |
| 2 | |
| 3 | pub(crate) const FLAG_INTERLACED: u16 = 1; |
| 4 | pub(crate) const FLAG_MBAFF: u16 = 2; |
| 5 | #[allow(dead_code)] |
| 6 | pub(crate) const FLAG_444: u16 = 4; |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | pub(crate) struct CidEntry { |
| 12 | pub cid: u32, |
| 13 | pub width: u32, |
| 14 | #[allow(dead_code)] |
| 15 | pub height: u32, |
| 16 | pub coding_unit_size: u32, |
| 17 | pub flags: u16, |
| 18 | pub bit_depth: u32, |
| 19 | pub eob_index: u32, |
| 20 | pub luma_weight: &'static [u8], |
| 21 | pub chroma_weight: &'static [u8], |
| 22 | pub dc_codes: &'static [u8], |
| 23 | pub dc_bits: &'static [u8], |
| 24 | pub ac_codes: &'static [u16], |
| 25 | pub ac_bits: &'static [u8], |
| 26 | pub ac_info: &'static [u8], |
| 27 | pub run_codes: &'static [u16], |
| 28 | pub run_bits: &'static [u8], |
| 29 | pub run: &'static [u8], |
| 30 | } |
| 31 | |
| 32 | include!("tables_gen.rs"); |
| 33 | |
| 34 | pub(crate) fn cid_entry(cid: u32) -> Option<&'static CidEntry> { |
| 35 | CID_TABLE.iter().find(|e| e.cid == cid) |
| 36 | } |
| 37 | |
| 38 | |
| 39 | pub(crate) const ZIGZAG: [u8; 64] = [ |
| 40 | 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, |
| 41 | 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, |
| 42 | 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, |
| 43 | ]; |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | pub(crate) struct Vlc { |
| 49 | pub bits: u32, |
| 50 | pub table: Vec<u32>, |
| 51 | } |
| 52 | |
| 53 | const PRIMARY_BITS: u32 = 9; |
| 54 | |
| 55 | impl Vlc { |
| 56 | |
| 57 | |
| 58 | fn build(lens: &[u8], codes: impl Fn(usize) -> u32, symbols: impl Fn(usize) -> u32) -> Vlc { |
| 59 | let maxl = lens.iter().copied().max().unwrap_or(1).max(1) as u32; |
| 60 | let bits = maxl.min(PRIMARY_BITS); |
| 61 | let mut table = vec![0u32; 1 << bits]; |
| 62 | |
| 63 | let mut sub = vec![0u32; 1 << bits]; |
| 64 | for (i, &l) in lens.iter().enumerate() { |
| 65 | let l = l as u32; |
| 66 | if l > bits { |
| 67 | let pre = (codes(i) >> (l - bits)) as usize; |
| 68 | sub[pre] = sub[pre].max(l - bits); |
| 69 | } |
| 70 | } |
| 71 | for (pre, &sb) in sub.iter().enumerate() { |
| 72 | if sb > 0 { |
| 73 | table[pre] = (table.len() as u32) << 8 | 0x80 | sb; |
| 74 | let n = table.len(); |
| 75 | table.resize(n + (1 << sb), 0); |
| 76 | } |
| 77 | } |
| 78 | for (i, &l) in lens.iter().enumerate() { |
| 79 | if l == 0 { |
| 80 | continue; |
| 81 | } |
| 82 | let l = l as u32; |
| 83 | if l <= bits { |
| 84 | let start = (codes(i) << (bits - l)) as usize; |
| 85 | for e in &mut table[start..start + (1usize << (bits - l))] { |
| 86 | *e = symbols(i) << 8 | l; |
| 87 | } |
| 88 | } else { |
| 89 | let pre = (codes(i) >> (l - bits)) as usize; |
| 90 | let sb = sub[pre]; |
| 91 | let off = (table[pre] >> 8) as usize; |
| 92 | let low = codes(i) & ((1 << (l - bits)) - 1); |
| 93 | let start = off + ((low << (sb - (l - bits))) as usize); |
| 94 | for e in &mut table[start..start + (1usize << (sb - (l - bits)))] { |
| 95 | *e = symbols(i) << 8 | l; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | Vlc { bits, table } |
| 100 | } |
| 101 | |
| 102 | |
| 103 | #[inline(always)] |
| 104 | pub fn get(&self, r: &mut crate::bits::BitReader) -> Option<u32> { |
| 105 | let w = r.peek32(); |
| 106 | let mut e = self.table[(w >> (32 - self.bits)) as usize]; |
| 107 | if e & 0x80 != 0 { |
| 108 | let sb = e & 0x7f; |
| 109 | let idx = (e >> 8) as usize + ((w << self.bits) >> (32 - sb)) as usize; |
| 110 | e = self.table[idx]; |
| 111 | } |
| 112 | let l = e & 0xff; |
| 113 | if l == 0 { |
| 114 | return None; |
| 115 | } |
| 116 | r.skip(l); |
| 117 | Some(e >> 8) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | pub(crate) struct CodeSet { |
| 123 | pub cid: &'static CidEntry, |
| 124 | pub bit_depth: u32, |
| 125 | pub ac: Vlc, |
| 126 | pub dc: Vlc, |
| 127 | pub run: Vlc, |
| 128 | } |
| 129 | |
| 130 | impl CodeSet { |
| 131 | pub fn new(cid: &'static CidEntry, bit_depth: u32) -> CodeSet { |
| 132 | let ndc = if bit_depth > 8 { 14 } else { 12 }; |
| 133 | CodeSet { |
| 134 | cid, |
| 135 | bit_depth, |
| 136 | ac: Vlc::build(cid.ac_bits, |i| cid.ac_codes[i] as u32, |i| i as u32), |
| 137 | dc: Vlc::build(&cid.dc_bits[..ndc], |i| cid.dc_codes[i] as u32, |i| i as u32), |
| 138 | run: Vlc::build(cid.run_bits, |i| cid.run_codes[i] as u32, |i| cid.run[i] as u32), |
| 139 | } |
| 140 | } |
| 141 | } |