aac: clear M/S flags
[nihav.git] / nihav-duck / src / codecs / vpcommon.rs
index d4fdea5fc8a267bea315443dc82b06d8f246d3f9..eb84f4ef19582f295d679460e314882730f48cdf 100644 (file)
@@ -1,9 +1,26 @@
 use nihav_core::codecs::*;
-use nihav_core::codecs::blockdsp::*;
+use nihav_codec_support::codecs::blockdsp;
+use nihav_codec_support::codecs::blockdsp::*;
+
+pub const VP_YUVA420_FORMAT: NAPixelFormaton = NAPixelFormaton{
+        model:      ColorModel::YUV(YUVSubmodel::YUVJ),
+        components: 4,
+        comp_info:  [
+                Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: false, depth: 8, shift: 0, comp_offs: 0, next_elem: 1}),
+                Some(NAPixelChromaton{ h_ss: 1, v_ss: 1, packed: false, depth: 8, shift: 0, comp_offs: 1, next_elem: 1}),
+                Some(NAPixelChromaton{ h_ss: 1, v_ss: 1, packed: false, depth: 8, shift: 0, comp_offs: 2, next_elem: 1}),
+                Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: false, depth: 8, shift: 0, comp_offs: 3, next_elem: 1}),
+                None ],
+        elem_size:  0,
+        be:         false,
+        alpha:      true,
+        palette:    false
+    };
 
-#[derive(Clone,Copy,Debug,PartialEq)]
+#[derive(Clone,Copy,Debug,PartialEq,Default)]
 #[allow(dead_code)]
 pub enum VPMBType {
+    #[default]
     Intra,
     InterNoMV,
     InterMV,
@@ -35,10 +52,6 @@ impl VPMBType {
     }
 }
 
-impl Default for VPMBType {
-    fn default() -> Self { VPMBType::Intra }
-}
-
 #[derive(Default)]
 pub struct VPShuffler {
     lastframe: Option<NAVideoBufferRef<u8>>,
@@ -55,18 +68,10 @@ impl VPShuffler {
         self.goldframe = Some(buf);
     }
     pub fn get_last(&mut self) -> Option<NAVideoBufferRef<u8>> {
-        if let Some(ref frm) = self.lastframe {
-            Some(frm.clone())
-        } else {
-            None
-        }
+        self.lastframe.as_ref().cloned()
     }
     pub fn get_golden(&mut self) -> Option<NAVideoBufferRef<u8>> {
-        if let Some(ref frm) = self.goldframe {
-            Some(frm.clone())
-        } else {
-            None
-        }
+        self.goldframe.as_ref().cloned()
     }
     pub fn has_refs(&self) -> bool {
         self.lastframe.is_some()
@@ -96,7 +101,7 @@ pub struct BoolCoder<'a> {
 impl<'a> BoolCoder<'a> {
     pub fn new(src: &'a [u8]) -> DecoderResult<Self> {
         if src.len() < 3 { return Err(DecoderError::ShortData); }
-        let value = ((src[0] as u32) << 24) | ((src[1] as u32) << 16) | ((src[2] as u32) << 8) | (src[3] as u32);
+        let value = (u32::from(src[0]) << 24) | (u32::from(src[1]) << 16) | (u32::from(src[2]) << 8) | u32::from(src[3]);
         Ok(Self { src, pos: 4, value, range: 255, bits: 8 })
     }
     pub fn read_bool(&mut self) -> bool {
@@ -104,7 +109,7 @@ impl<'a> BoolCoder<'a> {
     }
     pub fn read_prob(&mut self, prob: u8) -> bool {
         self.renorm();
-        let split = 1 + (((self.range - 1) * (prob as u32)) >> 8);
+        let split = 1 + (((self.range - 1) * u32::from(prob)) >> 8);
         let bit;
         if self.value < (split << 24) {
             self.range = split;
@@ -151,7 +156,7 @@ impl<'a> BoolCoder<'a> {
         self.value <<= shift;
         self.bits   -= shift as i32;
         if (self.bits <= 0) && (self.pos < self.src.len()) {
-            self.value |= (self.src[self.pos] as u32) << (-self.bits as u8);
+            self.value |= u32::from(self.src[self.pos]) << (-self.bits as u8);
             self.pos += 1;
             self.bits += 8;
         }
@@ -160,7 +165,7 @@ impl<'a> BoolCoder<'a> {
             self.value <<= 1;
             self.bits   -= 1;
             if (self.bits <= 0) && (self.pos < self.src.len()) {
-                self.value |= self.src[self.pos] as u32;
+                self.value |= u32::from(self.src[self.pos]);
                 self.pos += 1;
                 self.bits = 8;
             }
@@ -170,7 +175,7 @@ impl<'a> BoolCoder<'a> {
         for _ in 0..nbytes {
             self.value <<= 8;
             if self.pos < self.src.len() {
-                self.value |= self.src[self.pos] as u32;
+                self.value |= u32::from(self.src[self.pos]);
                 self.pos += 1;
             }
         }
@@ -178,11 +183,11 @@ impl<'a> BoolCoder<'a> {
 }
 
 #[allow(dead_code)]
+#[allow(clippy::trivially_copy_pass_by_ref)]
 pub fn rescale_prob(prob: u8, weights: &[i16; 2], maxval: i32) -> u8 {
-    ((((prob as i32) * (weights[0] as i32) + 128) >> 8) + (weights[1] as i32)).min(maxval).max(1) as u8
+    (((i32::from(prob) * i32::from(weights[0]) + 128) >> 8) + i32::from(weights[1])).min(maxval).max(1) as u8
 }
 
-#[macro_export]
 macro_rules! vp_tree {
     ($bc: expr, $prob: expr, $node1: expr, $node2: expr) => {
         if !$bc.read_prob($prob) {
@@ -308,7 +313,7 @@ pub fn vp_add_block(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usize,
     let mut off = frm.offset[plane] + bx * 8 + by * 8 * frm.stride[plane];
     for y in 0..8 {
         for x in 0..8 {
-            frm.data[off + x] = (coeffs[x + y * 8] + (frm.data[off + x] as i16)).min(255).max(0) as u8;
+            frm.data[off + x] = (coeffs[x + y * 8] + i16::from(frm.data[off + x])).min(255).max(0) as u8;
         }
         off += frm.stride[plane];
     }
@@ -319,7 +324,7 @@ pub fn vp_add_block_ilace(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: u
     let mut off = frm.offset[plane] + bx * 8 + ((by & !1) * 8 + (by & 1)) * frm.stride[plane];
     for y in 0..8 {
         for x in 0..8 {
-            frm.data[off + x] = (coeffs[x + y * 8] + (frm.data[off + x] as i16)).min(255).max(0) as u8;
+            frm.data[off + x] = (coeffs[x + y * 8] + i16::from(frm.data[off + x])).min(255).max(0) as u8;
         }
         off += frm.stride[plane] * 2;
     }
@@ -331,7 +336,7 @@ pub fn vp_add_block_dc(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usiz
     let mut off = frm.offset[plane] + bx * 8 + by * 8 * frm.stride[plane];
     for _ in 0..8 {
         for x in 0..8 {
-            frm.data[off + x] = (dc + (frm.data[off + x] as i16)).min(255).max(0) as u8;
+            frm.data[off + x] = (dc + i16::from(frm.data[off + x])).min(255).max(0) as u8;
         }
         off += frm.stride[plane];
     }
@@ -340,10 +345,10 @@ pub fn vp_add_block_dc(coeffs: &mut [i16; 64], bx: usize, by: usize, plane: usiz
 pub fn vp31_loop_filter(data: &mut [u8], mut off: usize, step: usize, stride: usize,
                         len: usize, loop_str: i16) {
     for _ in 0..len {
-        let a = data[off - step * 2] as i16;
-        let b = data[off - step] as i16;
-        let c = data[off] as i16;
-        let d = data[off + step] as i16;
+        let a = i16::from(data[off - step * 2]);
+        let b = i16::from(data[off - step]);
+        let c = i16::from(data[off]);
+        let d = i16::from(data[off + step]);
         let mut diff = ((a - d) + 3 * (c - b) + 4) >> 3;
         if diff.abs() >= 2 * loop_str {
             diff = 0;
@@ -380,17 +385,18 @@ pub fn vp_copy_block(dst: &mut NASimpleVideoFrame<u8>, src: NAVideoBufferRef<u8>
     let src_x = sx - (pre as isize);
     let src_y = sy - (pre as isize);
     {
-        let mut tmp_buf = NASimpleVideoFrame::from_video_buf(&mut mc_buf).unwrap();
-        copy_block(&mut tmp_buf, src, comp, 0, 0, src_x as i16, src_y as i16,
-                   bsize, bsize, 0, 0, 0, interp);
-        if ((sx & 7) != 0) || (mode != 0) {
+        let tmp_buf = NASimpleVideoFrame::from_video_buf(&mut mc_buf).unwrap();
+        edge_emu(src.as_ref(), src_x, src_y, bsize, bsize, &mut tmp_buf.data[tmp_buf.offset[comp]..], tmp_buf.stride[comp], comp, 0);
+//        copy_block(&mut tmp_buf, src, comp, 0, 0, src_x as i16, src_y as i16,
+//                   bsize, bsize, 0, 0, 0, interp);
+        if (sx & 7) != 0 {
             let foff = (8 - (sx & 7)) as usize;
-            let off = pre + foff;
+            let off = pre + foff + tmp_buf.offset[comp];
             vp31_loop_filter(tmp_buf.data, off, 1, tmp_buf.stride[comp], bsize, loop_str);
         }
-        if ((sy & 7) != 0) || (mode != 0) {
+        if (sy & 7) != 0 {
             let foff = (8 - (sy & 7)) as usize;
-            let off = (pre + foff) * tmp_buf.stride[comp];
+            let off = (pre + foff) * tmp_buf.stride[comp] + tmp_buf.offset[comp];
             vp31_loop_filter(tmp_buf.data, off, tmp_buf.stride[comp], 1, bsize, loop_str);
         }
     }
@@ -404,7 +410,7 @@ fn vp3_interp00(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw:
     let mut didx = 0;
     let mut sidx = 0;
     for _ in 0..bh {
-        for x in 0..bw { dst[didx + x] = src[sidx + x]; }
+        dst[didx..][..bw].copy_from_slice(&src[sidx..][..bw]);
         didx += dstride;
         sidx += sstride;
     }
@@ -415,7 +421,7 @@ fn vp3_interp01(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw:
     let mut didx = 0;
     let mut sidx = 0;
     for _ in 0..bh {
-        for x in 0..bw { dst[didx + x] = (((src[sidx + x] as u16) + (src[sidx + x + 1] as u16)) >> 1) as u8; }
+        for x in 0..bw { dst[didx + x] = ((u16::from(src[sidx + x]) + u16::from(src[sidx + x + 1])) >> 1) as u8; }
         didx += dstride;
         sidx += sstride;
     }
@@ -426,7 +432,7 @@ fn vp3_interp10(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw:
     let mut didx = 0;
     let mut sidx = 0;
     for _ in 0..bh {
-        for x in 0..bw { dst[didx + x] = (((src[sidx + x] as u16) + (src[sidx + x + sstride] as u16)) >> 1) as u8; }
+        for x in 0..bw { dst[didx + x] = ((u16::from(src[sidx + x]) + u16::from(src[sidx + x + sstride])) >> 1) as u8; }
         didx += dstride;
         sidx += sstride;
     }
@@ -438,8 +444,8 @@ fn vp3_interp1x(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw:
     let mut sidx = 0;
     for _ in 0..bh {
         for x in 0..bw {
-            dst[didx + x] = (((src[sidx + x] as u16) +
-                              (src[sidx + x + sstride + 1] as u16)) >> 1) as u8;
+            dst[didx + x] = ((u16::from(src[sidx + x]) +
+                              u16::from(src[sidx + x + sstride + 1])) >> 1) as u8;
         }
         didx += dstride;
         sidx += sstride;
@@ -452,8 +458,8 @@ fn vp3_interp1y(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw:
     let mut sidx = 0;
     for _ in 0..bh {
         for x in 0..bw {
-            dst[didx + x] = (((src[sidx + x + 1] as u16) +
-                              (src[sidx + x + sstride] as u16)) >> 1) as u8;
+            dst[didx + x] = ((u16::from(src[sidx + x + 1]) +
+                              u16::from(src[sidx + x + sstride])) >> 1) as u8;
         }
         didx += dstride;
         sidx += sstride;