make all codec crates export just register_all functions and document them
[nihav.git] / nihav-duck / src / codecs / vp3.rs
index 0e4a6bb79059219e99c6e560d1897df7130bad98..3b8560ee0a1bd09d2c24f1ba906853323ff4dbc3 100644 (file)
@@ -243,7 +243,7 @@ impl BitRunDecoder {
             self.run = (self.read_run)(br)?;
         }
         self.run -= 1;
-        Ok(self.value)        
+        Ok(self.value)
     }
 }
 
@@ -426,6 +426,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
@@ -1100,7 +1108,7 @@ impl VP34Decoder {
                             cur_blk += 1;
                         }
                     }
-                }            
+                }
             }
             Ok(())
         } else {
@@ -1123,7 +1131,7 @@ impl VP34Decoder {
                     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];
+                        let blk = &mut self.blocks[saddr + (i & 1) + (i >> 1) * self.mb_w * 2];
                         blk.mv.x = br.read_cb(x_cb)? as i16;
                         if x_sign {
                             blk.mv.x = -blk.mv.x;
@@ -1184,7 +1192,7 @@ impl VP34Decoder {
     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
             ];
@@ -1400,10 +1408,10 @@ impl VP34Decoder {
         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;
@@ -1816,7 +1824,7 @@ impl NADecoder for VP34Decoder {
     fn decode(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
         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 {
@@ -1868,8 +1876,8 @@ 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 crate::duck_register_all_codecs;
+    use nihav_commonfmt::generic_register_all_demuxers;
 
     #[test]
     fn test_vp30() {
@@ -1905,9 +1913,8 @@ mod test {
         let mut dec_reg = RegisteredDecoders::new();
         duck_register_all_codecs(&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]));
     }
 }