rename register_all_codecs to register_all_decoders
[nihav.git] / nihav-indeo / src / codecs / indeo3.rs
index e0f8e563234661108bd173dfe92a86e919f105d2..cc3bed877c9cf6dd6863960bb0f3fdad7e0edb53 100644 (file)
@@ -76,7 +76,7 @@ impl Buffers {
         let mut sidx = soff;
         let mut didx = doff;
         for _ in 0..h {
-            for i in 0..w { self.dbuf[didx + i] = self.sbuf[sidx + i]; }
+            self.dbuf[didx..][..w].copy_from_slice(&self.sbuf[sidx..][..w]);
             sidx += stride;
             didx += stride;
         }
@@ -92,7 +92,7 @@ impl Buffers {
         } else {
             for i in 0..w { buf[i] = self.dbuf[didx - stride + i]; }
             for _ in 0..h {
-                for i in 0..w { self.dbuf[didx + i] = buf[i]; }
+                self.dbuf[didx..][..w].copy_from_slice(&buf[..w]);
                 didx += stride;
             }
         }
@@ -153,12 +153,12 @@ fn copy_line_top(bufs: &mut Buffers, off: usize, stride: usize, bw: usize, topli
     let mut buf: [u8; 8] = [0; 8];
     if !topline {
         let src = &bufs.dbuf[(off - stride)..(off - stride + bw)];
-        for i in 0..bw { buf[i] = src[i]; }
+        buf[..bw].copy_from_slice(&src[..bw]);
     } else {
         for i in 0..bw { buf[i] = DEFAULT_PIXEL; }
     }
     let dst = &mut bufs.dbuf[off..][..bw];
-    for i in 0..bw { dst[i] = buf[i]; }
+    dst.copy_from_slice(&buf[..bw]);
 }
 
 fn copy_line_top4x4(bufs: &mut Buffers, off: usize, stride: usize, topline: bool) {
@@ -179,7 +179,7 @@ fn copy_line_top8x8(bufs: &mut Buffers, off: usize, stride: usize, topline: bool
         for i in 0..8 { buf[i] = DEFAULT_PIXEL; }
     }
     let dst = &mut bufs.dbuf[off..][..8];
-    for i in 0..8 {dst[i] = buf[i]; }
+    dst.copy_from_slice(&buf[..8]);
 }
 
 fn fill_block8x8(bufs: &mut Buffers, doff: usize, stride: usize, h: usize, topline: bool, firstline: bool) {
@@ -200,7 +200,7 @@ fn fill_block8x8(bufs: &mut Buffers, doff: usize, stride: usize, h: usize, topli
         didx += stride;
     }
     for _ in start..h {
-        for i in 0..8 { bufs.dbuf[didx + i] = buf[i]; }
+        bufs.dbuf[didx..][..8].copy_from_slice(&buf[..8]);
         didx += stride;
     }
 }
@@ -215,6 +215,7 @@ struct Indeo3Decoder {
     altquant:   [u8; 16],
     vq_offset:  u8,
     bufs:       Buffers,
+    requant_tab: [[u8; 128]; 8],
 }
 
 #[derive(Clone,Copy)]
@@ -282,10 +283,32 @@ const SKIP_OR_TREE: u8 = 2;
 
 impl Indeo3Decoder {
     fn new() -> Self {
+        const REQUANT_OFF: [i32; 8] = [ 0, 1, 0, 4, 4, 1, 0, 1 ];
+
         let dummy_info = NACodecInfo::new_dummy();
+
+        let mut requant_tab = [[0u8; 128]; 8];
+        for i in 0..8 {
+            let step = (i as i32) + 2;
+            let start = if (i == 3) || (i == 4) { -3 } else { step / 2 };
+            let mut last = 0;
+            for j in 0..128 {
+                requant_tab[i][j] = (((j as i32) + start) / step * step + REQUANT_OFF[i]) as u8;
+                if requant_tab[i][j] < 128 {
+                    last = requant_tab[i][j];
+                } else {
+                    requant_tab[i][j] = last;
+                }
+            }
+        }
+        requant_tab[1][7]   =  10;
+        requant_tab[1][119] = 118;
+        requant_tab[1][120] = 118;
+        requant_tab[4][8]   =  10;
+
         Indeo3Decoder { info: dummy_info, bpos: 0, bbuf: 0, width: 0, height: 0,
                         mvs: Vec::new(), altquant: [0; 16],
-                        vq_offset: 0, bufs: Buffers::new() }
+                        vq_offset: 0, bufs: Buffers::new(), requant_tab }
     }
 
     fn br_reset(&mut self) {
@@ -302,8 +325,9 @@ impl Indeo3Decoder {
         Ok((self.bbuf >> self.bpos) & 0x3)
     }
 
+    #[allow(clippy::cyclomatic_complexity)]
     fn decode_cell_data(&mut self, br: &mut ByteReader, cell: IV3Cell,
-                        off: usize, stride: usize, params: CellDecParams) -> DecoderResult<()> {
+                        off: usize, stride: usize, params: CellDecParams, vq_idx: u8) -> DecoderResult<()> {
         let blk_w = cell.w * 4 / params.bw;
         let blk_h = cell.h * 4 / params.bh;
         let scale: usize = if params.bh == 4 { 1 } else { 2 };
@@ -332,6 +356,20 @@ impl Indeo3Decoder {
             validate!(b <= (self.height as i16));
             sidx = (l as usize) + (t as usize) * stride + off;
         }
+        if vq_idx >= 8 {
+            let requant_tab = &self.requant_tab[(vq_idx & 7) as usize];
+            if cell.no_mv() {
+                if cell.y > 0 {
+                    for x in 0..(cell.w as usize) * 4 {
+                        self.bufs.dbuf[didx + x - stride] = requant_tab[self.bufs.dbuf[didx + x - stride] as usize];
+                    }
+                }
+            } else {
+                for x in 0..(cell.w as usize) * 4 {
+                    self.bufs.sbuf[sidx + x] = requant_tab[self.bufs.sbuf[sidx + x] as usize];
+                }
+            }
+        }
         for y in 0..blk_h {
             let mut xoff: usize = 0;
             for _ in 0..blk_w {
@@ -541,7 +579,7 @@ impl Indeo3Decoder {
         } else {
             return Err(DecoderError::InvalidData);
         }
-        self.decode_cell_data(br, cell, off, stride, cp)
+        self.decode_cell_data(br, cell, off, stride, cp, vq_idx)
     }
 
     fn parse_tree(&mut self, br: &mut ByteReader, cell: IV3Cell, off: usize,
@@ -680,6 +718,12 @@ impl NADecoder for Indeo3Decoder {
         if ver != 32 { return Err(DecoderError::NotImplemented); }
         let flags   = br.read_u16le()?;
         let size2   = br.read_u32le()?;
+        if size2 == 0x80 {
+            let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
+            frm.set_keyframe(false);
+            frm.set_frame_type(FrameType::Skip);
+            return Ok(frm.into_ref());
+        }
         validate!(((size2 + 7) >> 3) <= size);
         let cb      = br.read_byte()?;
         self.vq_offset = cb;
@@ -689,8 +733,12 @@ impl NADecoder for Indeo3Decoder {
         validate!((width  >= 16) && (width  <= 640));
         validate!((height >= 16) && (height <= 640));
         validate!(((width & 3) == 0) && ((height & 3) == 0));
+        let vinfo;
         if (self.bufs.width != (width as usize)) || (self.bufs.height != (height as usize)) {
             self.bufs.alloc(width as usize, height as usize);
+            vinfo = NAVideoInfo::new(width as usize, height as usize, false, formats::YUV410_FORMAT);
+        } else {
+            vinfo = self.info.get_properties().get_video_info().unwrap();
         }
         self.width  = width;
         self.height = height;
@@ -716,9 +764,6 @@ impl NADecoder for Indeo3Decoder {
         if (uoff < vend) && (uoff > voff) { vend = uoff; }
 
         let intraframe = (flags & FLAG_KEYFRAME) != 0;
-        let vinfo = self.info.get_properties().get_video_info().unwrap();
-        validate!((vinfo.get_width() & !3) == (self.width & !3).into());
-        validate!((vinfo.get_height() & !3) == (self.height & !3).into());
         let bufinfo = alloc_video_buffer(vinfo, 4)?;
         let mut buf = bufinfo.get_vbuf().unwrap();
         let ystart  = data_start + u64::from(yoff);
@@ -748,6 +793,12 @@ impl NADecoder for Indeo3Decoder {
     }
 }
 
+impl NAOptionHandler for Indeo3Decoder {
+    fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
+    fn set_options(&mut self, _options: &[NAOption]) { }
+    fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
+}
+
 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
     Box::new(Indeo3Decoder::new())
 }
@@ -756,17 +807,29 @@ pub fn get_decoder() -> Box<dyn NADecoder + Send> {
 mod test {
     use nihav_core::codecs::RegisteredDecoders;
     use nihav_core::demuxers::RegisteredDemuxers;
-    use nihav_core::test::dec_video::*;
-    use crate::codecs::indeo_register_all_codecs;
-    use nihav_commonfmt::demuxers::generic_register_all_demuxers;
+    use nihav_codec_support::test::dec_video::*;
+    use crate::indeo_register_all_decoders;
+    use nihav_commonfmt::generic_register_all_demuxers;
     #[test]
     fn test_indeo3() {
         let mut dmx_reg = RegisteredDemuxers::new();
         generic_register_all_demuxers(&mut dmx_reg);
         let mut dec_reg = RegisteredDecoders::new();
-        indeo_register_all_codecs(&mut dec_reg);
-
-        test_file_decoding("avi", "assets/Indeo/iv32_example.avi", Some(10), true, false, None, &dmx_reg, &dec_reg);
+        indeo_register_all_decoders(&mut dec_reg);
+
+        test_decoding("avi", "indeo3", "assets/Indeo/iv32_example.avi", Some(10),
+                      &dmx_reg, &dec_reg, ExpectedTestResult::MD5Frames(vec![
+                            [0x90be698e, 0x326db071, 0x08e8c6a5, 0x39349acc],
+                            [0x25d677fc, 0x63f96aaa, 0xd412ca98, 0x61416313],
+                            [0xc4368250, 0x63e7b6bc, 0xffcff950, 0x11f13239],
+                            [0x7e869758, 0x027abc2e, 0x25204bca, 0x93fbaa03],
+                            [0x5a1e822c, 0x2b1a4cd5, 0x72059843, 0xe5689ad1],
+                            [0x3a971cce, 0x5ec22135, 0x1a45f802, 0x0f5f9264],
+                            [0x0a65f782, 0xd8767cf3, 0x878b4b8d, 0xfc94c88b],
+                            [0x4ac70139, 0x3300eac1, 0xba84b068, 0x47f5ff29],
+                            [0x3e8c8ec4, 0x9421b38c, 0x580abbbd, 0x92792d19],
+                            [0x9096ee9b, 0x8dd9fb14, 0x981e31e3, 0x3ffd7d29],
+                            [0x22dc71ec, 0x3d8f6f7e, 0x1a198982, 0x41d17ecc]]));
     }
 }