replace vec.truncate(0) with vec.clear()
[nihav.git] / nihav-realmedia / src / codecs / rv3040.rs
index dde0b04774ae413c54ae6b0949228e2b5a60e405..762d05459b3b11172f9f89b3c2aa321d8ac1086a 100644 (file)
@@ -1,9 +1,10 @@
 use nihav_core::formats::YUV420_FORMAT;
 use nihav_core::frame::{NABufferType, NAVideoInfo, NAVideoBuffer, NAVideoBufferRef, FrameType, alloc_video_buffer};
-use nihav_core::codecs::{NADecoderSupport, MV, ZERO_MV, DecoderError, DecoderResult, IPBShuffler};
+use nihav_core::codecs::{NADecoderSupport, DecoderError, DecoderResult};
+use nihav_codec_support::codecs::{MV, ZERO_MV, IPBShuffler};
 use nihav_core::io::bitreader::{BitReader,BitReaderMode};
 use nihav_core::io::intcode::*;
-use nihav_core::data::GenericCache;
+use nihav_codec_support::data::GenericCache;
 use std::mem;
 
 use super::rv34codes::*;
@@ -325,13 +326,13 @@ impl MVInfo {
     }
     fn reset(&mut self) {
         let size = self.w * self.h;
-        self.mv_f.truncate(0);
+        self.mv_f.clear();
         self.mv_f.resize(size, ZERO_MV);
-        self.mv_b.truncate(0);
+        self.mv_b.clear();
         self.mv_b.resize(size, ZERO_MV);
-        self.has_f.truncate(0);
+        self.has_f.clear();
         self.has_f.resize(size >> 2, false);
-        self.has_b.truncate(0);
+        self.has_b.clear();
         self.has_b.resize(size >> 2, false);
     }
     fn fill(&mut self, mb_x: usize, mb_y: usize, fwd: bool, mv: MV) {
@@ -502,7 +503,7 @@ pub trait RV34BitstreamDecoder {
 }
 
 pub trait RV34DSP {
-    fn loop_filter(&self, frame: &mut NAVideoBuffer<u8>, ftype: FrameType, mbinfo: &[RV34MBInfo], mb_w: usize, row: usize);
+    fn loop_filter(&self, frame: &mut NAVideoBuffer<u8>, ftype: FrameType, mbinfo: &[RV34MBInfo], mb_w: usize, mb_h: usize, row: usize);
     fn do_luma_mc(&self, frame: &mut NAVideoBuffer<u8>, prev_frame: &NAVideoBuffer<u8>, x: usize, y: usize, mv: MV, use16: bool, avg: bool);
     fn do_chroma_mc(&self, frame: &mut NAVideoBuffer<u8>, prev_frame: &NAVideoBuffer<u8>, x: usize, y: usize, comp: usize, mv: MV, use8: bool, avg: bool);
 }
@@ -510,11 +511,11 @@ pub trait RV34DSP {
 fn parse_slice_offsets(src: &[u8], offsets: &mut Vec<usize>) -> DecoderResult<()> {
     let num_slices = (src[0] as usize) + 1;
     let ini_off = num_slices * 8 + 1;
-    offsets.truncate(0);
+    offsets.clear();
 
     if ini_off >= src.len() { return Err(DecoderError::ShortData); }
 
-    let mut br = BitReader::new(&src[1..], ini_off - 1, BitReaderMode::BE);
+    let mut br = BitReader::new(&src[1..ini_off], BitReaderMode::BE);
 
     for i in 0..num_slices {
         br.skip(32)?;
@@ -531,17 +532,33 @@ fn parse_slice_offsets(src: &[u8], offsets: &mut Vec<usize>) -> DecoderResult<()
     Ok(())
 }
 
-fn decode_slice_header(br: &mut BitReader, bd: &mut RV34BitstreamDecoder, slice_no: usize, slice_offs: &[usize], old_width: usize, old_height: usize) -> DecoderResult<RV34SliceHeader> {
+fn decode_slice_header(br: &mut BitReader, bd: &mut dyn RV34BitstreamDecoder, slice_no: usize, slice_offs: &[usize], old_width: usize, old_height: usize) -> DecoderResult<RV34SliceHeader> {
     validate!(slice_no < slice_offs.len());
     br.seek((slice_offs[slice_no] * 8) as u32)?;
     let mut shdr = bd.decode_slice_header(br, old_width, old_height)?;
+    if ((shdr.width == 0) || (shdr.height == 0)) && (shdr.ftype != FrameType::I) {
+        return Err(DecoderError::MissingReference);
+    }
     if slice_no < slice_offs.len() - 1 {
         let cur_pos = br.tell() as u32;
         br.seek((slice_offs[slice_no + 1] * 8) as u32)?;
-        let nhdr = bd.decode_slice_header(br, shdr.width, shdr.height)?;
+        if let Ok(nhdr) = bd.decode_slice_header(br, shdr.width, shdr.height) {
+            validate!(nhdr.start > shdr.start);
+            shdr.end = nhdr.start;
+        } else {
+            if slice_no + 2 < slice_offs.len() {
+                br.seek((slice_offs[slice_no + 2] * 8) as u32)?;
+                if let Ok(nhdr) = bd.decode_slice_header(br, shdr.width, shdr.height) {
+                    validate!(nhdr.start > shdr.start);
+                    shdr.end = nhdr.start;
+                } else {
+                    shdr.end = ((shdr.width + 15) >> 4) * ((shdr.height + 15) >> 4);
+                }
+            } else {
+                shdr.end = ((shdr.width + 15) >> 4) * ((shdr.height + 15) >> 4);
+            }
+        }
         br.seek(cur_pos)?;
-        validate!(nhdr.start > shdr.start);
-        shdr.end = nhdr.start;
     } else {
         shdr.end = ((shdr.width + 15) >> 4) * ((shdr.height + 15) >> 4);
     }
@@ -552,6 +569,9 @@ const RV34_MB_MAX_SIZES: [usize; 6] = [ 0x2F, 0x62, 0x18B, 0x62F, 0x18BF, 0x23FF
 const RV34_SLICE_START_BITS: [u8; 6] = [ 6, 7, 9, 11, 13, 14 ];
 
 pub fn get_slice_start_offset_bits(w: usize, h: usize) -> u8 {
+    if (w == 0) || (h == 0) {
+        return 0;
+    }
     let mb_size = ((w + 15) >> 4) * ((h + 15) >> 4) - 1;
     let mut idx: usize = 0;
     while (idx < 5) && (RV34_MB_MAX_SIZES[idx] < mb_size) { idx += 1; }
@@ -679,13 +699,13 @@ fn decode_mv(br: &mut BitReader) -> DecoderResult<MV> {
     Ok(MV{ x, y })
 }
 
-fn do_mc_16x16(dsp: &Box<dyn RV34DSP>, buf: &mut NAVideoBuffer<u8>, prevbuf: &NAVideoBuffer<u8>, mb_x: usize, mb_y: usize, mv: MV, avg: bool) {
+fn do_mc_16x16(dsp: &mut dyn RV34DSP, buf: &mut NAVideoBuffer<u8>, prevbuf: &NAVideoBuffer<u8>, mb_x: usize, mb_y: usize, mv: MV, avg: bool) {
     dsp.do_luma_mc  (buf, prevbuf, mb_x * 16, mb_y * 16,    mv, true, avg);
     dsp.do_chroma_mc(buf, prevbuf, mb_x *  8, mb_y *  8, 1, mv, true, avg);
     dsp.do_chroma_mc(buf, prevbuf, mb_x *  8, mb_y *  8, 2, mv, true, avg);
 }
 
-fn do_mc_8x8(dsp: &Box<dyn RV34DSP>, buf: &mut NAVideoBuffer<u8>, prevbuf: &NAVideoBuffer<u8>, mb_x: usize, xoff: usize, mb_y: usize, yoff: usize, mv: MV, avg: bool) {
+fn do_mc_8x8(dsp: &mut dyn RV34DSP, buf: &mut NAVideoBuffer<u8>, prevbuf: &NAVideoBuffer<u8>, mb_x: usize, xoff: usize, mb_y: usize, yoff: usize, mv: MV, avg: bool) {
     dsp.do_luma_mc  (buf, prevbuf, mb_x * 16 + xoff * 8, mb_y * 16 + yoff * 8,    mv, false, avg);
     dsp.do_chroma_mc(buf, prevbuf, mb_x *  8 + xoff * 4, mb_y *  8 + yoff * 4, 1, mv, false, avg);
     dsp.do_chroma_mc(buf, prevbuf, mb_x *  8 + xoff * 4, mb_y *  8 + yoff * 4, 2, mv, false, avg);
@@ -717,7 +737,7 @@ fn do_avg(cdsp: &RV34CommonDSP, buf: &mut NAVideoBuffer<u8>, avg_buf: &NAVideoBu
 pub struct RV34Decoder {
     is_rv30:    bool,
     coderead:   RV34Codes,
-    dsp:        Box<dyn RV34DSP>,
+    dsp:        Box<dyn RV34DSP + Send>,
     cdsp:       RV34CommonDSP,
     width:      usize,
     height:     usize,
@@ -735,7 +755,7 @@ pub struct RV34Decoder {
 }
 
 impl RV34Decoder {
-    pub fn new(is_rv30: bool, dsp: Box<dyn RV34DSP>) -> Self {
+    pub fn new(is_rv30: bool, dsp: Box<dyn RV34DSP + Send>) -> Self {
         let tmp_vinfo = NAVideoInfo::new(16, 16, false, YUV420_FORMAT);
         let vt = alloc_video_buffer(tmp_vinfo, 4).unwrap();
         let vb = vt.get_vbuf();
@@ -757,7 +777,7 @@ impl RV34Decoder {
             base_ts:    0,
         }
     }
-    fn decode_mb_header_intra(&mut self, bd: &mut RV34BitstreamDecoder, br: &mut BitReader, is_i16: bool, im: &mut IntraModeState, q: u8, has_top: bool, has_dq: bool) -> DecoderResult<MBInfo> {
+    fn decode_mb_header_intra(&mut self, bd: &mut dyn RV34BitstreamDecoder, br: &mut BitReader, is_i16: bool, im: &mut IntraModeState, q: u8, has_top: bool, has_dq: bool) -> DecoderResult<MBInfo> {
         if is_i16 {
             let imode = br.read(2)? as i8;
             im.fill_block(imode);
@@ -773,7 +793,7 @@ impl RV34Decoder {
             Ok(MBInfo { mbtype: MBType::MBIntra, skip_run: 0, dquant: dq })
         }
     }
-    fn decode_mb_header_inter(&mut self, bd: &mut RV34BitstreamDecoder, br: &mut BitReader, ftype: FrameType, mbtype: MBType, im: &mut IntraModeState, q: u8, has_top: bool) -> DecoderResult<MBInfo> {
+    fn decode_mb_header_inter(&mut self, bd: &mut dyn RV34BitstreamDecoder, br: &mut BitReader, ftype: FrameType, mbtype: MBType, im: &mut IntraModeState, q: u8, has_top: bool) -> DecoderResult<MBInfo> {
         let hdr = bd.decode_inter_mb_hdr(br, ftype, mbtype)?;
         validate!(hdr.mbtype != MBType::Invalid);
         if hdr.dquant {
@@ -904,19 +924,19 @@ impl RV34Decoder {
             MBType::MBP16x16 | MBType::MBP16x16Mix => {
                     if let Some(ref prevbuf) = self.ipbs.get_lastref() {
                         let mv = self.mvi.get_mv(mb_x, mb_y, 0, 0, true);
-                        do_mc_16x16(&self.dsp, buf, prevbuf, mb_x, mb_y, mv, false);
+                        do_mc_16x16(self.dsp.as_mut(), buf, prevbuf, mb_x, mb_y, mv, false);
                     }
                 },
             MBType::MBForward => {
                     if let Some(ref fwdbuf) = self.ipbs.get_b_fwdref() {
                         let mv = self.mvi.get_mv(mb_x, mb_y, 0, 0, true);
-                        do_mc_16x16(&self.dsp, buf, fwdbuf, mb_x, mb_y, mv, false);
+                        do_mc_16x16(self.dsp.as_mut(), buf, fwdbuf, mb_x, mb_y, mv, false);
                     }
                 },
             MBType::MBBackward => {
                     if let Some(ref bwdbuf) = self.ipbs.get_b_bwdref() {
                         let mv = self.mvi.get_mv(mb_x, mb_y, 0, 0, false);
-                        do_mc_16x16(&self.dsp, buf, bwdbuf, mb_x, mb_y, mv, false);
+                        do_mc_16x16(self.dsp.as_mut(), buf, bwdbuf, mb_x, mb_y, mv, false);
                     }
                 },
             MBType::MBP8x8 | MBType::MBP8x16 | MBType::MBP16x8 => {
@@ -924,14 +944,14 @@ impl RV34Decoder {
                         for y in 0..2 {
                             for x in 0..2 {
                                 let mv = self.mvi.get_mv(mb_x, mb_y, x, y, true);
-                                do_mc_8x8(&self.dsp, buf, prevbuf, mb_x, x, mb_y, y, mv, false);
+                                do_mc_8x8(self.dsp.as_mut(), buf, prevbuf, mb_x, x, mb_y, y, mv, false);
                             }
                         }
                     }
                 },
             MBType::MBSkip if !self.is_b => {
                     if let Some(ref prevbuf) = self.ipbs.get_lastref() {
-                        do_mc_16x16(&self.dsp, buf, prevbuf, mb_x, mb_y, ZERO_MV, false);
+                        do_mc_16x16(self.dsp.as_mut(), buf, prevbuf, mb_x, mb_y, ZERO_MV, false);
                     }
                 },
             MBType::MBSkip | MBType::MBDirect => {
@@ -939,8 +959,8 @@ impl RV34Decoder {
                         for y in 0..2 {
                             for x in 0..2 {
                                 let (mv_f, mv_b) = self.ref_mvi.get_mv(mb_x, mb_y, x, y, true).scale(sstate.trd, sstate.trb);
-                                do_mc_8x8(&self.dsp, buf, fwdbuf, mb_x, x, mb_y, y, mv_f, false);
-                                do_mc_8x8(&self.dsp, &mut self.avg_buf, bwdbuf, mb_x, x, mb_y, y, mv_b, true);
+                                do_mc_8x8(self.dsp.as_mut(), buf, fwdbuf, mb_x, x, mb_y, y, mv_f, false);
+                                do_mc_8x8(self.dsp.as_mut(), &mut self.avg_buf, bwdbuf, mb_x, x, mb_y, y, mv_b, true);
                                 do_avg(&self.cdsp, buf, &self.avg_buf, mb_x, x, mb_y, y, 8, self.ratio1, self.ratio2);
                             }
                         }
@@ -950,8 +970,8 @@ impl RV34Decoder {
                     if let (Some(ref fwdbuf), Some(ref bwdbuf)) = (self.ipbs.get_b_fwdref(), self.ipbs.get_b_bwdref()) {
                         let mv_f = self.mvi.get_mv(mb_x, mb_y, 0, 0, true);
                         let mv_b = self.mvi.get_mv(mb_x, mb_y, 0, 0, false);
-                        do_mc_16x16(&self.dsp, buf, fwdbuf, mb_x, mb_y, mv_f, false);
-                        do_mc_16x16(&self.dsp, &mut self.avg_buf, bwdbuf, mb_x, mb_y, mv_b, true);
+                        do_mc_16x16(self.dsp.as_mut(), buf, fwdbuf, mb_x, mb_y, mv_f, false);
+                        do_mc_16x16(self.dsp.as_mut(), &mut self.avg_buf, bwdbuf, mb_x, mb_y, mv_b, true);
                         do_avg(&self.cdsp, buf, &self.avg_buf, mb_x, 0, mb_y, 0, 16, self.ratio1, self.ratio2);
                     }
                 },
@@ -1075,12 +1095,13 @@ impl RV34Decoder {
         }
     }
 
-    pub fn parse_frame(&mut self, supp: &mut NADecoderSupport, src: &[u8], bd: &mut RV34BitstreamDecoder) -> DecoderResult<(NABufferType, FrameType, u64)> {
+    #[allow(clippy::cognitive_complexity)]
+    pub fn parse_frame(&mut self, supp: &mut NADecoderSupport, src: &[u8], bd: &mut dyn RV34BitstreamDecoder) -> DecoderResult<(NABufferType, FrameType, u64)> {
         let mut slice_offs: Vec<usize> = Vec::new();
         parse_slice_offsets(src, &mut slice_offs)?;
         let ini_off = slice_offs.len() * 8 + 1;
 
-        let mut br = BitReader::new(&src[ini_off..], src.len() - ini_off, BitReaderMode::BE);
+        let mut br = BitReader::new(&src[ini_off..], BitReaderMode::BE);
         let hdr0 = decode_slice_header(&mut br, bd, 0, slice_offs.as_slice(), self.width, self.height)?;
         validate!((hdr0.width != 0) && (hdr0.height != 0));
         self.width  = hdr0.width;
@@ -1106,6 +1127,22 @@ impl RV34Decoder {
                 self.base_ts += 1 << 13;
             }
         }
+        match hdr0.ftype {
+            FrameType::P => {
+                if self.ipbs.get_lastref().is_none() {
+                    return Err(DecoderError::MissingReference);
+                }
+            },
+            FrameType::B => {
+                if self.ipbs.get_lastref().is_none() {
+                    return Err(DecoderError::MissingReference);
+                }
+                if self.ipbs.get_nextref().is_none() {
+                    return Err(DecoderError::MissingReference);
+                }
+            },
+            _ => {},
+        };
         let ts_diff = (self.next_ts << 3).wrapping_sub(hdr0.pts << 3) >> 3;
         let ts = self.base_ts + (self.next_ts as u64) - (ts_diff as u64);
         sstate.trd = (self.next_ts << 3).wrapping_sub(self.last_ts << 3) >> 3;
@@ -1219,6 +1256,7 @@ impl RV34Decoder {
                     sstate.q_dc = bd.quant_dc(true, q);
                     self.decode_mb_intra(&sstate, &imode, &mut buf, &mut br, is_16)?;
                 } else {
+                    sstate.q_dc = bd.quant_dc(false, q);
                     imode.fill_block(0);
                     self.decode_mb_inter(&sstate, &mbh, &mut buf, &mut br, is_16)?;
                 }
@@ -1235,12 +1273,12 @@ impl RV34Decoder {
                 mb_pos += 1;
             }
             if hdr0.deblock && (mb_y >= 1) {
-                self.dsp.loop_filter(&mut buf, hdr0.ftype, &mbinfo, mb_w, mb_y - 1);
+                self.dsp.loop_filter(&mut buf, hdr0.ftype, &mbinfo, mb_w, mb_h, mb_y - 1);
             }
             imode.update();
         }
         if hdr0.deblock {
-            self.dsp.loop_filter(&mut buf, hdr0.ftype, &mbinfo, mb_w, mb_h - 1);
+            self.dsp.loop_filter(&mut buf, hdr0.ftype, &mbinfo, mb_w, mb_h, mb_h - 1);
         }
         if !self.is_b {
             self.ipbs.add_frame(buf.clone());
@@ -1250,4 +1288,7 @@ impl RV34Decoder {
 
         Ok((NABufferType::Video(buf), hdr0.ftype, ts))
     }
+    pub fn flush(&mut self) {
+        self.ipbs.clear();
+    }
 }