1 use nihav_core::frame::FrameType;
2 use nihav_core::io::bitwriter::*;
3 use nihav_core::io::intcode::*;
4 use nihav_codec_support::codecs::MV;
6 use super::super::rv34codes::*;
7 use super::super::rv40data::*;
9 pub fn write_slice_header(bw: &mut BitWriter, ftype: FrameType, q: usize, set_idx: usize, deblock: bool, pts: u32) {
12 FrameType::I => bw.write(0, 2),
13 FrameType::P => bw.write(2, 2),
14 FrameType::B => bw.write(3, 2),
17 bw.write(q as u32, 5);
18 bw.write(0, 2); // unknown
19 bw.write(set_idx as u32, 2);
20 bw.write(!deblock as u32, 1);
24 pub fn write_slice_dimensions(bw: &mut BitWriter, width: usize, height: usize) {
25 let wcode = match width {
37 let mut w = width >> 2;
42 bw.write(w as u32, 8);
45 let hcode = match height {
60 bw.write(hcode + 6, 4);
62 let mut h = height >> 2;
67 bw.write(h as u32, 8);
72 pub fn write_slice_mb_idx(bw: &mut BitWriter, mb_idx: usize, num_mbs: usize) {
73 let mba_bits = match num_mbs - 1 {
82 bw.write(mb_idx as u32, mba_bits);
85 pub fn write_skip_count(bw: &mut BitWriter, skip_count: u32) {
86 bw.write_code(UintCodeType::Gamma, skip_count);
89 fn write_mv(bw: &mut BitWriter, mv: MV) {
90 let xcode = if mv.x > 0 { (mv.x - 1) * 2 + 1 } else { -mv.x * 2 } as u32;
91 let ycode = if mv.y > 0 { (mv.y - 1) * 2 + 1 } else { -mv.y * 2 } as u32;
93 bw.write_code(UintCodeType::Gamma, xcode);
94 bw.write_code(UintCodeType::Gamma, ycode);
97 pub fn write_mb_header(bw: &mut BitWriter, ftype: FrameType, sstate: &SliceState, mbstate: &MBState) {
98 let mb_idx = mbstate.get_mb_idx(sstate.mb_x, sstate.mb_y);
99 let pred_mbt = mbstate.get_pred_mbtype(sstate, ftype == FrameType::B);
101 let set_id = pred_mbt.to_code();
103 if ftype != FrameType::I {
104 let (codes, lens) = if ftype == FrameType::P {
105 (&RV40_PTYPE_CODES[set_id][..], &RV40_PTYPE_BITS[set_id][..])
107 (&RV40_BTYPE_CODES[set_id][..], &RV40_BTYPE_BITS[set_id][..])
109 let idx = mbstate.mb_type[mb_idx].to_code();
110 bw.write(codes[idx].into(), lens[idx]);
112 match mbstate.mb_type[mb_idx] {
114 if ftype == FrameType::I {
117 bw.write(mbstate.ipred[mbstate.get_blk4_idx(sstate.mb_x, sstate.mb_y)] as u32, 2);
120 if ftype == FrameType::I {
122 bw.write1(); //dquant
124 let ystart = if sstate.has_t { 0 } else { 1 };
125 let mut blk4_idx = mbstate.get_blk4_idx(sstate.mb_x, sstate.mb_y);
129 for &el in mbstate.ipred[blk4_idx..][..4].iter() {
130 code = code * 2 + if el == 0 { 0 } else { 1 };
132 bw.write(RV40_AIC_TOP_CODES[code].into(), RV40_AIC_TOP_BITS[code]);
133 blk4_idx += mbstate.blk4_stride;
138 let (lctx, tctx, trctx) = mbstate.get_ipred4x4_ctx(sstate.mb_x, sstate.mb_y, x, y);
139 let mode = mbstate.ipred[blk4_idx + x];
140 let ctx_word = if x < 3 {
141 ((trctx & 0xF) as u16) + (((tctx & 0xF) as u16) << 4) + (((lctx & 0xF) as u16) << 8)
143 if let Some(idx) = RV40_AIC_PATTERNS.iter().position(|&x| x == ctx_word) {
144 let mode1 = mbstate.ipred[blk4_idx + x + 1];
145 let code = mode * 9 + mode1;
146 bw.write(RV40_AIC_MODE2_CODES[idx][code as usize].into(),
147 RV40_AIC_MODE2_BITS[idx][code as usize]);
149 } else if tctx != -1 && lctx != -1 {
150 let idx = (tctx + lctx * 10) as usize;
151 let code = mode as usize;
152 bw.write(RV40_AIC_MODE1_CODES[idx][code].into(),
153 RV40_AIC_MODE1_BITS[idx][code]);
180 blk4_idx += mbstate.blk4_stride;
183 MBType::P16x16 | MBType::P16x16Mix => {
184 let diff_mv = mbstate.get_diff_mv(sstate, true, 0, 0);
185 write_mv(bw, diff_mv);
188 let diff_mv = mbstate.get_diff_mv(sstate, true, 0, 0);
189 write_mv(bw, diff_mv);
190 let diff_mv = mbstate.get_diff_mv(sstate, true, 0, 1);
191 write_mv(bw, diff_mv);
194 let diff_mv = mbstate.get_diff_mv(sstate, false, 0, 0);
195 write_mv(bw, diff_mv);
196 let diff_mv = mbstate.get_diff_mv(sstate, false, 1, 0);
197 write_mv(bw, diff_mv);
201 let diff_mv = mbstate.get_diff_mv(sstate, false, i & 1, i >> 1);
202 write_mv(bw, diff_mv);
206 let fwd_diff = mbstate.get_diff_mv_b(sstate, true);
207 write_mv(bw, fwd_diff);
209 MBType::Backward => {
210 let bwd_diff = mbstate.get_diff_mv_b(sstate, false);
211 write_mv(bw, bwd_diff);
214 let fwd_diff = mbstate.get_diff_mv_b(sstate, true);
215 let bwd_diff = mbstate.get_diff_mv_b(sstate, false);
216 write_mv(bw, fwd_diff);
217 write_mv(bw, bwd_diff);
219 MBType::Invalid => unreachable!(),
220 _ => unimplemented!(),
225 fn write(&self, bw: &mut BitWriter, code: u16);
228 impl CodeWriter for RV34CodeReader {
229 fn write(&self, bw: &mut BitWriter, to_write: u16) {
230 for (&sym, (&code, &bits)) in self.syms.iter().zip(self.codes.iter().zip(self.lengths.iter())) {
232 bw.write(code, bits);
240 impl CodeWriter for RV34CBPCodeReader {
241 fn write(&self, bw: &mut BitWriter, to_write: u16) {
242 for (&sym, (&code, &bits)) in self.syms.iter().zip(self.codes.iter().zip(self.lengths.iter())) {
243 if u16::from(sym) == to_write {
244 bw.write(code, bits);
253 cbp_pattern: RV34CodeReader,
254 cbp: [RV34CBPCodeReader; 4]
258 fn new(intra: bool, set: usize, subset: usize) -> Self {
260 let cbp_pat = RV34CodeReader::new(&RV34_INTRA_CBPPAT[set][subset]);
261 let cbp0 = RV34CBPCodeReader::new(&RV34_INTRA_CBP[set][subset]);
262 let cbp1 = RV34CBPCodeReader::new(&RV34_INTRA_CBP[set][subset + 1*2]);
263 let cbp2 = RV34CBPCodeReader::new(&RV34_INTRA_CBP[set][subset + 2*2]);
264 let cbp3 = RV34CBPCodeReader::new(&RV34_INTRA_CBP[set][subset + 3*2]);
265 CBPSet { cbp_pattern: cbp_pat, cbp: [cbp0, cbp1, cbp2, cbp3] }
267 let cbp_pat = RV34CodeReader::new(&RV34_INTER_CBPPAT[set]);
268 let cbp0 = RV34CBPCodeReader::new(&RV34_INTER_CBP[set][0]);
269 let cbp1 = RV34CBPCodeReader::new(&RV34_INTER_CBP[set][1]);
270 let cbp2 = RV34CBPCodeReader::new(&RV34_INTER_CBP[set][2]);
271 let cbp3 = RV34CBPCodeReader::new(&RV34_INTER_CBP[set][3]);
272 CBPSet { cbp_pattern: cbp_pat, cbp: [cbp0, cbp1, cbp2, cbp3] }
278 pat0: Vec<RV34CodeReader>,
279 pat1: Vec<RV34CodeReader>,
280 pat2: Vec<RV34CodeReader>,
284 fn new(intra: bool, set: usize) -> Self {
286 let first0 = RV34CodeReader::new(&RV34_INTRA_FIRSTPAT[set][0]);
287 let first1 = RV34CodeReader::new(&RV34_INTRA_FIRSTPAT[set][1]);
288 let first2 = RV34CodeReader::new(&RV34_INTRA_FIRSTPAT[set][2]);
289 let first3 = RV34CodeReader::new(&RV34_INTRA_FIRSTPAT[set][3]);
290 let firsts = vec![first0, first1, first2, first3];
292 let second0 = RV34CodeReader::new(&RV34_INTRA_SECONDPAT[set][0]);
293 let second1 = RV34CodeReader::new(&RV34_INTRA_SECONDPAT[set][1]);
294 let seconds = vec![second0, second1];
296 let third0 = RV34CodeReader::new(&RV34_INTRA_THIRDPAT[set][0]);
297 let third1 = RV34CodeReader::new(&RV34_INTRA_THIRDPAT[set][1]);
298 let thirds = vec![third0, third1];
300 CoefSet { pat0: firsts, pat1: seconds, pat2: thirds }
302 let first0 = RV34CodeReader::new(&RV34_INTER_FIRSTPAT[set][0]);
303 let first1 = RV34CodeReader::new(&RV34_INTER_FIRSTPAT[set][1]);
304 let firsts = vec![first0, first1];
306 let second0 = RV34CodeReader::new(&RV34_INTER_SECONDPAT[set][0]);
307 let second1 = RV34CodeReader::new(&RV34_INTER_SECONDPAT[set][1]);
308 let seconds = vec![second0, second1];
310 let third0 = RV34CodeReader::new(&RV34_INTER_THIRDPAT[set][0]);
311 let third1 = RV34CodeReader::new(&RV34_INTER_THIRDPAT[set][1]);
312 let thirds = vec![third0, third1];
314 CoefSet { pat0: firsts, pat1: seconds, pat2: thirds }
322 coeffs: RV34CodeReader,
326 fn new(intra: bool, set: usize) -> Self {
328 let cbp0 = CBPSet::new(intra, set, 0);
329 let cbp1 = CBPSet::new(intra, set, 1);
330 let cbp: Vec<CBPSet> = vec![cbp0, cbp1];
331 let cset = CoefSet::new(intra, set);
332 let coeffs = RV34CodeReader::new(&RV34_INTRA_COEFFS[set]);
333 FullSet { cbp, cset, coeffs }
335 let cbp0 = CBPSet::new(intra, set, 0);
336 let cbp: Vec<CBPSet> = vec![cbp0];
337 let cset = CoefSet::new(intra, set);
338 let coeffs = RV34CodeReader::new(&RV34_INTER_COEFFS[set]);
339 FullSet { cbp, cset, coeffs }
342 fn write_block(&self, bw: &mut BitWriter, blk: &Block, subset_idx: usize, luma: bool) {
343 let sblk0 = [blk.coeffs[0], blk.coeffs[1], blk.coeffs[4], blk.coeffs[5]];
344 let sblk1 = [blk.coeffs[2], blk.coeffs[3], blk.coeffs[6], blk.coeffs[7]];
345 let sblk2 = [blk.coeffs[8], blk.coeffs[12], blk.coeffs[9], blk.coeffs[13]]; // sub-block 2 has different order
346 let sblk3 = [blk.coeffs[10], blk.coeffs[11], blk.coeffs[14], blk.coeffs[15]];
348 let idx0 = get_subblock_index(&sblk0);
349 let idx1 = get_subblock_index(&sblk1);
350 let idx2 = get_subblock_index(&sblk2);
351 let idx3 = get_subblock_index(&sblk3);
353 let mut cflags = idx0;
354 cflags = (cflags << 1) | ((idx1 != 0) as u16);
355 cflags = (cflags << 1) | ((idx2 != 0) as u16);
356 cflags = (cflags << 1) | ((idx3 != 0) as u16);
358 self.cset.pat0[subset_idx].write(bw, cflags);
360 if matches!(idx0, 0 | 27 | 54 | 81) { // only first coefficient is set
361 write_single_coeff(bw, &self.coeffs, sblk0[0], 3);
363 write_coeffs(bw, &self.coeffs, &sblk0);
366 self.cset.pat1[!luma as usize].write(bw, idx1);
367 write_coeffs(bw, &self.coeffs, &sblk1);
370 self.cset.pat1[!luma as usize].write(bw, idx2);
371 write_coeffs(bw, &self.coeffs, &sblk2);
374 self.cset.pat2[!luma as usize].write(bw, idx3);
375 write_coeffs(bw, &self.coeffs, &sblk3);
380 fn write_coeffs(bw: &mut BitWriter, coeffs: &RV34CodeReader, blk: &[i16; 4]) {
381 for (&val, &limit) in blk.iter().zip([3i16, 2, 2, 2].iter()) {
382 write_single_coeff(bw, coeffs, val, limit);
386 fn write_single_coeff(bw: &mut BitWriter, coeffs: &RV34CodeReader, val: i16, limit: i16) {
388 if val.abs() >= limit {
389 let mut val = (val.abs() - limit) as u16;
392 let bits = (15 - val.leading_zeros()) as u16;
393 coeffs.write(bw, bits + 23);
394 bw.write(u32::from(val - (1 << bits)), bits as u8);
396 coeffs.write(bw, val);
407 pub struct CodeSets {
419 pub fn new() -> Self {
420 let mut iset: Vec<FullSet> = Vec::with_capacity(5);
421 for set in 0..5 { iset.push(FullSet::new(true, set)); }
422 let mut pset: Vec<FullSet> = Vec::with_capacity(7);
423 for set in 0..7 { pset.push(FullSet::new(false, set)); }
434 pub fn init(&mut self, quant: usize, subset: usize) {
436 if (subset == 2) && (idx < 19) {
438 } else if (subset != 0) && (idx < 26) {
444 self.super_idx = idx;
446 pub fn set_params(&mut self, mbtype: &MacroblockType) {
447 self.is_p16 = matches!(*mbtype, MacroblockType::InterMix(_));
448 self.intra = mbtype.is_intra() || self.is_p16;
449 self.is16 = mbtype.is_16();
450 self.set_idx = if self.intra {
451 RV34_SET_IDX_INTRA[self.super_idx]
453 RV34_SET_IDX_INTER[self.super_idx]
456 fn write_cbp(&self, bw: &mut BitWriter, coded_pat: [bool; 24], cbp_code: &CBPSet) {
457 let mut cbp_pat = 0u16;
459 cbp_pat = cbp_pat * 3 + (coded_pat[i] as u16) + (coded_pat[i + 4] as u16);
461 let mut nnz = 0usize;
462 for blk4 in coded_pat[..16].chunks(4) {
463 let cur_nz = blk4.contains(&true);
467 cbp_pat = cbp_pat * 2 + (cur_nz as u16);
469 nnz = nnz.saturating_sub(1);
471 cbp_code.cbp_pattern.write(bw, cbp_pat);
472 for blk4 in coded_pat[..16].chunks(4) {
473 let pat = (blk4[3] as u16) * 32 + (blk4[2] as u16) * 16 + (blk4[1] as u16) * 2 + (blk4[0] as u16);
475 cbp_code.cbp[nnz].write(bw, pat);
479 if coded_pat[i] ^ coded_pat[i + 4] {
488 pub fn write_coeffs(&mut self, bw: &mut BitWriter, coeffs: &[Block; 25]) {
489 let mut fset = if self.intra { &self.iset[self.set_idx] } else { &self.pset[self.set_idx] };
491 const CODED_ORDER: [usize; 24] = [0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23];
492 let cbp_code = &fset.cbp[if self.is16 { 1 } else { 0 }];
493 let mut coded_blk = [false; 24];
494 let mut coded_pat = [false; 24];
495 for (i, ((cpat, cblk), &seq)) in coded_pat.iter_mut().zip(coded_blk.iter_mut())
496 .zip(CODED_ORDER.iter()).enumerate() {
497 *cpat = !coeffs[seq].is_empty();
498 *cblk = !coeffs[i].is_empty();
500 self.write_cbp(bw, coded_pat, cbp_code);
503 fset.write_block(bw, &coeffs[24], 3, true);
505 let (luma_set, chroma_set) = if self.intra {
506 (if self.is16 { 2 } else { 1 }, if !self.is_p16 { 0 } else { 1 })
510 let mut citer = coded_blk.iter();
511 for blk in coeffs[..16].iter() {
512 if let Some(true) = citer.next() {
513 fset.write_block(bw, blk, luma_set, true);
517 self.set_idx = RV34_SET_IDX_INTER[self.super_idx];
518 fset = &self.pset[self.set_idx];
520 for blk in coeffs[16..24].iter() {
521 if let Some(true) = citer.next() {
522 fset.write_block(bw, blk, chroma_set, false);
528 fn get_subblock_index(blk: &[i16; 4]) -> u16 {
529 let mut idx = blk[0].abs().min(3) as u16;
530 idx = idx * 3 + (blk[1].abs().min(2) as u16);
531 idx = idx * 3 + (blk[2].abs().min(2) as u16);
532 idx = idx * 3 + (blk[3].abs().min(2) as u16);