X-Git-Url: https://git.nihav.org/?p=nihav.git;a=blobdiff_plain;f=nihav-duck%2Fsrc%2Fcodecs%2Fvp3.rs;h=9d6ef6aff37bb321f9ef0ebc08609646630dc9dc;hp=8612831d20dbf17b9b93817217f7fbadc31029f7;hb=78fb6560c73965d834b215fb0b49505ae5443288;hpb=5d00209dc3ffb2f57aa2f44629254ea6b26c4a3c diff --git a/nihav-duck/src/codecs/vp3.rs b/nihav-duck/src/codecs/vp3.rs index 8612831..9d6ef6a 100644 --- a/nihav-duck/src/codecs/vp3.rs +++ b/nihav-duck/src/codecs/vp3.rs @@ -1,7 +1,8 @@ use std::mem; use std::ptr; use nihav_core::codecs::*; -use nihav_core::codecs::blockdsp::*; +use nihav_codec_support::codecs::{MV, ZERO_MV, ZIGZAG}; +use nihav_codec_support::codecs::blockdsp::*; use nihav_core::io::bitreader::*; use nihav_core::io::codebook::*; use nihav_core::io::intcode::*; @@ -167,6 +168,7 @@ impl VP40AuxCodes { } } +#[allow(clippy::large_enum_variant)] enum Codes { None, VP30(VP30Codes), @@ -243,7 +245,7 @@ impl BitRunDecoder { self.run = (self.read_run)(br)?; } self.run -= 1; - Ok(self.value) + Ok(self.value) } } @@ -289,7 +291,7 @@ fn vp30_read_coded_run1(br: &mut BitReader) -> DecoderResult { let idx = br.peek(5) as usize; let sym = (VP30_CRUN1_LUT[idx] >> 4) as usize; let bits = VP30_CRUN1_LUT[idx] & 0xF; - br.skip(bits as u32)?; + br.skip(u32::from(bits))?; if sym < 7 { Ok(sym) } else if sym == 7 { @@ -312,6 +314,7 @@ struct VP34Decoder { mb_h: usize, version: u8, is_intra: bool, + update_gf: bool, quant: usize, shuf: VPShuffler, codes: Codes, @@ -425,6 +428,14 @@ fn rescale_qmat(dst_qmat: &mut [i16; 64], base_qmat: &[i16; 64], dc_quant: i16, dst_qmat[0] = (base_qmat[0] * dc_quant / 100).max(minval * 2) << 2; } +fn rescale_qmat_vp4(dst_qmat: &mut [i16; 64], base_qmat: &[i16; 64], dc_quant: i16, ac_quant: i16, is_intra: bool) { + let (bias, minval) = if is_intra { (3, 4) } else { (6, 8) }; + for (dst, src) in dst_qmat.iter_mut().zip(base_qmat.iter()) { + *dst = ((src - bias).wrapping_mul(ac_quant) / 100 + bias) << 2; + } + dst_qmat[0] = (base_qmat[0] * dc_quant / 100).max(minval) << 2; +} + fn expand_token(blk: &mut Block, br: &mut BitReader, eob_run: &mut usize, token: u8) -> DecoderResult<()> { match token { // EOBs @@ -536,7 +547,7 @@ fn expand_token(blk: &mut Block, br: &mut BitReader, eob_run: &mut usize, token: macro_rules! fill_dc_pred { ($self: expr, $ref_id: expr, $pred: expr, $pp: expr, $bit: expr, $idx: expr) => { if $self.blocks[$idx].coded && $self.blocks[$idx].btype.get_ref_id() == $ref_id { - $pred[$bit] = $self.blocks[$idx].coeffs[0] as i32; + $pred[$bit] = i32::from($self.blocks[$idx].coeffs[0]); $pp |= 1 << $bit; } }; @@ -573,6 +584,7 @@ impl VP34Decoder { mb_h: 0, version, is_intra: true, + update_gf: false, quant: 0, shuf: VPShuffler::new(), codes: Codes::None, @@ -611,7 +623,6 @@ impl VP34Decoder { validate!(self.version == 3 || self.version == 30); let mb_w = br.read(8)? as usize; let mb_h = br.read(8)? as usize; -println!(" VP30 {}x{} ({}x{})", mb_w, mb_h, self.mb_w, self.mb_h); validate!(mb_w == self.mb_w && mb_h == self.mb_h); if self.version == 3 { self.version = 30; @@ -619,7 +630,6 @@ println!(" VP30 {}x{} ({}x{})", mb_w, mb_h, self.mb_w, self.mb_h); } } else { let version = br.read(13)?; -println!("intra, ver {} (self {})", version, self.version); let coding_type = br.read(1)?; validate!(coding_type == 0); br.skip(2)?; @@ -633,7 +643,6 @@ println!("intra, ver {} (self {})", version, self.version); validate!(self.version == 4); let mb_h = br.read(8)? as usize; let mb_w = br.read(8)? as usize; -//println!(" VP40 {}x{} ({}x{})", mb_w, mb_h, self.mb_w, self.mb_h); validate!(mb_w == self.mb_w && mb_h == self.mb_h); let fact1 = br.read(5)?; let fact2 = br.read(3)?; @@ -652,7 +661,6 @@ println!("intra, ver {} (self {})", version, self.version); } else { VP40_LOOP_STRENGTH[self.quant] }; -//println!("decode frame({},{},{})", self.is_intra as u8, self.is_intra as u8, self.quant); Ok(()) } fn vp30_unpack_sb_info(&mut self, br: &mut BitReader) -> DecoderResult<()> { @@ -1102,7 +1110,7 @@ println!("intra, ver {} (self {})", version, self.version); cur_blk += 1; } } - } + } } Ok(()) } else { @@ -1125,12 +1133,12 @@ println!("intra, ver {} (self {})", version, self.version); last2_mv = last_mv; let saddr = (self.blk_addr[cur_blk] >> 2).min(self.blk_addr[cur_blk + 1] >> 2).min(self.blk_addr[cur_blk + 2] >> 2).min(self.blk_addr[cur_blk + 3] >> 2); for i in 0..4 { - let blk = &mut self.blocks[saddr + (i & 1) + (i >> 1) * self.mb_w]; - blk.mv.x = br.read_cb(x_cb)? as i16; + let blk = &mut self.blocks[saddr + (i & 1) + (i >> 1) * self.mb_w * 2]; + blk.mv.x = i16::from(br.read_cb(x_cb)?); if x_sign { blk.mv.x = -blk.mv.x; } - blk.mv.y = br.read_cb(y_cb)? as i16; + blk.mv.y = i16::from(br.read_cb(y_cb)?); if y_sign { blk.mv.y = -blk.mv.y; } @@ -1148,8 +1156,8 @@ println!("intra, ver {} (self {})", version, self.version); let y_cb = &codes.mv_y_cb[VP40_MV_LUT_INDEX[last_mv.y.abs() as usize]]; let x_sign = last_mv.x < 0; let y_sign = last_mv.y < 0; - let x = br.read_cb(x_cb)? as i16; - let y = br.read_cb(y_cb)? as i16; + let x = i16::from(br.read_cb(x_cb)?); + let y = i16::from(br.read_cb(y_cb)?); cur_mv = MV { x: if !x_sign { x } else { -x }, y: if !y_sign { y } else { -y } }; last2_mv = last_mv; last_mv = cur_mv; @@ -1166,8 +1174,8 @@ println!("intra, ver {} (self {})", version, self.version); let y_cb = &codes.mv_y_cb[VP40_MV_LUT_INDEX[last_mv_g.y.abs() as usize]]; let x_sign = last_mv_g.x < 0; let y_sign = last_mv_g.y < 0; - let x = br.read_cb(x_cb)? as i16; - let y = br.read_cb(y_cb)? as i16; + let x = i16::from(br.read_cb(x_cb)?); + let y = i16::from(br.read_cb(y_cb)?); cur_mv = MV { x: if !x_sign { x } else { -x }, y: if !y_sign { y } else { -y } }; last_mv_g = cur_mv; }, @@ -1186,7 +1194,7 @@ println!("intra, ver {} (self {})", version, self.version); fn vp40_unpack_coeffs(&mut self, br: &mut BitReader, dc_table_y: usize, dc_table_c: usize, ac_table_y: usize, ac_table_c: usize) -> DecoderResult<()> { const VP40_PRED_MASKS: [usize; 16] = [ // top, bottom, left, right 0b1010, 0b1010, 0b1000, 0b1011, - 0b1010, 0b1010, 0b0010, 0b1111, + 0b1010, 0b1010, 0b0010, 0b1110, 0b0010, 0b1010, 0b0010, 0b0110, 0b0100, 0b0111, 0b1110, 0b1110 ]; @@ -1270,6 +1278,9 @@ println!("intra, ver {} (self {})", version, self.version); } let dc_quant = VP30_DC_SCALES[self.quant] * 10; let ac_quant = VP30_AC_SCALES[self.quant]; + + self.update_gf = ac_quant <= 50; + rescale_qmat(&mut self.qmat_y, VP3_QMAT_Y, dc_quant, ac_quant, 2); rescale_qmat(&mut self.qmat_c, VP3_QMAT_C, dc_quant, ac_quant, 2); rescale_qmat(&mut self.qmat_y_p, VP3_QMAT_INTER, dc_quant, ac_quant, 4); @@ -1399,10 +1410,10 @@ println!("intra, ver {} (self {})", version, self.version); let dc_y_quant = VP40_DC_Y_SCALES[self.quant]; let dc_c_quant = VP40_DC_C_SCALES[self.quant]; let ac_quant = VP40_AC_SCALES[self.quant]; - rescale_qmat(&mut self.qmat_y, VP40_QMAT, dc_y_quant, ac_quant, 2); - rescale_qmat(&mut self.qmat_c, VP40_QMAT, dc_c_quant, ac_quant, 2); - self.qmat_y_p.copy_from_slice(&self.qmat_y); - self.qmat_c_p.copy_from_slice(&self.qmat_c); + rescale_qmat_vp4(&mut self.qmat_y, VP40_QMAT, dc_y_quant, ac_quant, true); + rescale_qmat_vp4(&mut self.qmat_y_p, VP40_QMAT, dc_c_quant, ac_quant, false); + self.qmat_c.copy_from_slice(&self.qmat_y); + self.qmat_c_p.copy_from_slice(&self.qmat_y_p); self.eob_run = 0; let dc_table_y = br.read(4)? as usize; @@ -1440,10 +1451,10 @@ println!("intra, ver {} (self {})", version, self.version); let mut pred = 0i32; for i in 0..4 { if (pp & (1 << i)) != 0 { - pred += (preds[i] as i32) * (VP31_DC_WEIGHTS[pp][i] as i32); + pred += (preds[i] as i32) * i32::from(VP31_DC_WEIGHTS[pp][i]); } } - pred /= VP31_DC_WEIGHTS[pp][4] as i32; + pred /= i32::from(VP31_DC_WEIGHTS[pp][4]); if (pp & 7) == 7 { if (pred - preds[2]).abs() > 128 { return preds[2] as i16; } if (pred - preds[0]).abs() > 128 { return preds[0] as i16; } @@ -1542,6 +1553,7 @@ println!("intra, ver {} (self {})", version, self.version); } } } + #[allow(clippy::cyclomatic_complexity)] fn output_blocks_inter(&mut self, frm: &mut NASimpleVideoFrame) { let mut blk_idx = 0; let bstride = self.mb_w * 2; @@ -1815,7 +1827,7 @@ impl NADecoder for VP34Decoder { fn decode(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult { let src = pkt.get_buffer(); validate!(src.len() > 0); - let mut br = BitReader::new(&src, src.len(), BitReaderMode::BE); + let mut br = BitReader::new(&src, BitReaderMode::BE); self.parse_header(&mut br)?; if self.is_intra { @@ -1839,7 +1851,7 @@ impl NADecoder for VP34Decoder { _ => return Err(DecoderError::Bug), } - if self.is_intra { + if self.is_intra || self.update_gf { self.shuf.add_golden_frame(buf.clone()); } self.shuf.add_frame(buf.clone()); @@ -1854,6 +1866,12 @@ impl NADecoder for VP34Decoder { } } +impl NAOptionHandler for VP34Decoder { + fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } + fn set_options(&mut self, _options: &[NAOption]) { } + fn query_option_value(&self, _name: &str) -> Option { None } +} + pub fn get_decoder_vp3() -> Box { Box::new(VP34Decoder::new(3)) } @@ -1866,19 +1884,19 @@ pub fn get_decoder_vp4() -> Box { mod test { use nihav_core::codecs::RegisteredDecoders; use nihav_core::demuxers::RegisteredDemuxers; - use nihav_core::test::dec_video::*; - use crate::codecs::duck_register_all_codecs; - use nihav_commonfmt::demuxers::generic_register_all_demuxers; + use nihav_codec_support::test::dec_video::*; + use crate::duck_register_all_decoders; + use nihav_commonfmt::generic_register_all_demuxers; #[test] fn test_vp30() { let mut dmx_reg = RegisteredDemuxers::new(); generic_register_all_demuxers(&mut dmx_reg); let mut dec_reg = RegisteredDecoders::new(); - duck_register_all_codecs(&mut dec_reg); + duck_register_all_decoders(&mut dec_reg); - let file = "assets/Duck/vp30-logo.avi"; - test_file_decoding("avi", file, Some(23), true, false, None/*Some("vp30")*/, &dmx_reg, &dec_reg); + test_decoding("avi", "vp3", "assets/Duck/vp30-logo.avi", Some(23), &dmx_reg, &dec_reg, + ExpectedTestResult::MD5([0x51aba7df, 0x6e42534d, 0xef6c5b13, 0x26c38d1f])); } #[test] @@ -1886,13 +1904,15 @@ mod test { let mut dmx_reg = RegisteredDemuxers::new(); generic_register_all_demuxers(&mut dmx_reg); let mut dec_reg = RegisteredDecoders::new(); - duck_register_all_codecs(&mut dec_reg); + duck_register_all_decoders(&mut dec_reg); - let file = "assets/Duck/vp31.avi"; +// let file = "assets/Duck/vp31.avi"; // let file = "assets/Duck/vp31_crash.avi"; // let file = "assets/Duck/01-vp31-0500.avi"; - test_file_decoding("avi", file, Some(3), true, false, None/*Some("vp31")*/, &dmx_reg, &dec_reg); +// test_file_decoding("avi", file, Some(3), true, false, None/*Some("vp31")*/, &dmx_reg, &dec_reg); //panic!("end"); + test_decoding("avi", "vp3", "assets/Duck/01-vp31-0500.avi", Some(16), &dmx_reg, &dec_reg, + ExpectedTestResult::MD5([0x65112f7e, 0x2914f29b, 0x2908ed2f, 0xce5fc8c5])); } #[test] @@ -1900,11 +1920,10 @@ mod test { let mut dmx_reg = RegisteredDemuxers::new(); generic_register_all_demuxers(&mut dmx_reg); let mut dec_reg = RegisteredDecoders::new(); - duck_register_all_codecs(&mut dec_reg); + duck_register_all_decoders(&mut dec_reg); - let file = "assets/Duck/ot171_vp40.avi"; - test_file_decoding("avi", file, Some(16), true, false, None/*Some("vp4")*/, &dmx_reg, &dec_reg); -//panic!("end"); + test_decoding("avi", "vp3", "assets/Duck/ot171_vp40.avi", Some(86), &dmx_reg, &dec_reg, + ExpectedTestResult::MD5([0xd41d8cd9, 0x8f00b204, 0xe9800998, 0xecf8427e])); } }