add flush() to decoder interface
[nihav.git] / nihav-indeo / src / codecs / indeo3.rs
index 892f193e1423c919cbd65374fe80faa59d904359..6e03cb3b308da41ed49dc8b065d9c521fc696df0 100644 (file)
@@ -1,5 +1,3 @@
-use std::rc::Rc;
-use std::cell::RefCell;
 use nihav_core::formats;
 use nihav_core::codecs::*;
 use nihav_core::io::byteio::*;
@@ -20,6 +18,8 @@ struct MV {
 struct Buffers {
     width:      usize,
     height:     usize,
+    cw:         usize,
+    ch:         usize,
     buf1:       Vec<u8>,
     buf2:       Vec<u8>,
     fbuf:       bool,
@@ -28,7 +28,7 @@ struct Buffers {
 const DEFAULT_PIXEL: u8 = 0x40;
 
 impl Buffers {
-    fn new() -> Self { Buffers { width: 0, height: 0, buf1: Vec::new(), buf2: Vec::new(), fbuf: true } }
+    fn new() -> Self { Buffers { width: 0, height: 0, cw: 0, ch: 0, buf1: Vec::new(), buf2: Vec::new(), fbuf: true } }
     fn reset(&mut self) {
         self.width  = 0;
         self.height = 0;
@@ -38,17 +38,19 @@ impl Buffers {
     fn alloc(&mut self, w: usize, h: usize) {
         self.width  = w;
         self.height = h;
-        self.buf1.resize(w * h + (w >> 2) * (h >> 2) * 2, DEFAULT_PIXEL);
-        self.buf2.resize(w * h + (w >> 2) * (h >> 2) * 2, DEFAULT_PIXEL);
+        self.cw = ((w >> 2) + 3) & !3;
+        self.ch = ((h >> 2) + 3) & !3;
+        self.buf1.resize(w * h + self.cw * self.ch * 2, DEFAULT_PIXEL);
+        self.buf2.resize(w * h + self.cw * self.ch * 2, DEFAULT_PIXEL);
     }
     fn flip(&mut self) { self.fbuf = !self.fbuf; }
     fn get_stride(&mut self, planeno: usize) -> usize {
-        if planeno == 0 { self.width } else { self.width >> 2 }
+        if planeno == 0 { self.width } else { self.cw }
     }
     fn get_offset(&mut self, planeno: usize) -> usize {
         match planeno {
             1 => self.width * self.height,
-            2 => self.width * self.height + (self.width >> 2) * (self.height >> 2),
+            2 => self.width * self.height + self.cw * self.ch,
             _ => 0,
         }
     }
@@ -58,10 +60,10 @@ impl Buffers {
             let mut doff = fbuf.get_offset(planeno);
             let sstride = self.get_stride(planeno);
             let dstride = fbuf.get_stride(planeno);
-            let width  = if planeno == 0 { self.width }  else { self.width  >> 2 };
+            let width  = if planeno == 0 { self.width }  else { self.width >> 2 };
             let height = if planeno == 0 { self.height } else { self.height >> 2 };
             let src = if self.fbuf { &self.buf1[0..] } else { &self.buf2[0..] };
-            let mut dst = fbuf.get_data_mut();
+            let dst = fbuf.get_data_mut().unwrap();
             for _ in 0..height {
                 for x in 0..width {
                     dst[doff + x] = src[soff + x] * 2;
@@ -221,7 +223,7 @@ fn fill_block8x8(bufs: &mut Buffers, doff: usize, stride: usize, h: usize, topli
     } else if bufs.fbuf {
         for i in 0..8 { buf[i] = bufs.buf1[doff - stride + i]; }
     } else {
-        for i in 0..8 { buf[i] = bufs.buf1[doff - stride + i]; }
+        for i in 0..8 { buf[i] = bufs.buf2[doff - stride + i]; }
     }
     if topline && !firstline {
         for i in 0..4 { buf[i * 2 + 1] = buf[i * 2]; }
@@ -233,6 +235,9 @@ fn fill_block8x8(bufs: &mut Buffers, doff: usize, stride: usize, h: usize, topli
     }
 
     let start = if !topline { 0 } else { 1 };
+    if topline {
+        didx += stride;
+    }
     if bufs.fbuf {
         for _ in start..h {
             for i in 0..8 { bufs.buf1[didx + i] = buf[i]; }
@@ -247,7 +252,7 @@ fn fill_block8x8(bufs: &mut Buffers, doff: usize, stride: usize, h: usize, topli
 }
 
 struct Indeo3Decoder {
-    info:       Rc<NACodecInfo>,
+    info:       NACodecInfoRef,
     bpos:       u8,
     bbuf:       u8,
     width:      u16,
@@ -271,7 +276,7 @@ struct IV3Cell {
 
 impl IV3Cell {
     fn new(w: u16, h: u16) -> Self {
-        IV3Cell { x: 0, y: 0, w: w, h: h, d: 20, vqt: false, mv: None }
+        IV3Cell { x: 0, y: 0, w, h, d: 20, vqt: false, mv: None }
     }
     fn split_h(&self) -> (Self, Self) {
         let h1 = if self.h > 2 { ((self.h + 2) >> 2) << 1 } else { 1 };
@@ -301,7 +306,7 @@ impl IV3Cell {
         cell2.d -= 1;
         (cell1, cell2)
     }
-    fn no_mv(&self) -> bool { match self.mv { None => true, Some(_) => false } }
+    fn no_mv(&self) -> bool { self.mv.is_none() }
 }
 
 struct CellDecParams {
@@ -323,7 +328,7 @@ const SKIP_OR_TREE: u8 = 2;
 
 impl Indeo3Decoder {
     fn new() -> Self {
-        let dummy_info = Rc::new(DUMMY_CODEC_INFO);
+        let dummy_info = NACodecInfo::new_dummy();
         Indeo3Decoder { info: dummy_info, bpos: 0, bbuf: 0, width: 0, height: 0,
                         mvs: Vec::new(), altquant: [0; 16],
                         vq_offset: 0, bufs: Buffers::new() }
@@ -360,8 +365,8 @@ impl Indeo3Decoder {
             sidx = 0;
         } else {
             let mv = cell.mv.unwrap();
-            let mx = mv.x as i16;
-            let my = mv.y as i16;
+            let mx = i16::from(mv.x);
+            let my = i16::from(mv.y);
             let l = (cell.x as i16) * 4 + mx;
             let t = (cell.y as i16) * 4 + my;
             let r = ((cell.x + cell.w) as i16) * 4 + mx;
@@ -468,7 +473,7 @@ impl Indeo3Decoder {
                                 tocopy = 4 - line;
                             }
                             if c >= 0xFD {
-                                let nl = 257 - (c as i16) - (line as i16);
+                                let nl = 257 - i16::from(c) - (line as i16);
                                 validate!(nl > 0);
                                 tocopy = nl as usize;
                             }
@@ -508,8 +513,8 @@ impl Indeo3Decoder {
     fn copy_cell(&mut self, cell: IV3Cell, off: usize, stride: usize) -> DecoderResult<()> {
         if cell.no_mv() { return Err(DecoderError::InvalidData); }
         let mv = cell.mv.unwrap();
-        let mx = mv.x as i16;
-        let my = mv.y as i16;
+        let mx = i16::from(mv.x);
+        let my = i16::from(mv.y);
         let l = (cell.x as i16) * 4 + mx;
         let t = (cell.y as i16) * 4 + my;
         let r = ((cell.x + cell.w) as i16) * 4 + mx;
@@ -641,12 +646,13 @@ impl Indeo3Decoder {
         for _ in 0..nvec {
             let x = br.read_byte()? as i8;
             let y = br.read_byte()? as i8;
-            self.mvs.push(MV{ x: x, y: y });
+            self.mvs.push(MV{ x, y });
         }
 
         let shift = if planeno == 0 { 2 } else { 4 };
-        let cell = IV3Cell::new((self.bufs.width  >> shift) as u16,
-                                (self.bufs.height >> shift) as u16);
+        let round = (1 << shift) - 1;
+        let cell = IV3Cell::new(((self.bufs.width  + round) >> shift) as u16,
+                                ((self.bufs.height + round) >> shift) as u16);
         self.br_reset();
         self.parse_tree(br, cell, offs, stride, if planeno > 0 { 10 } else { 40 }, true)?;
         validate!(br.tell() <= end);
@@ -665,7 +671,7 @@ impl Indeo3Decoder {
         for _ in 0..nvec {
             let y = br.read_byte()? as i8;
             let x = br.read_byte()? as i8;
-            self.mvs.push(MV{ x: x, y: y });
+            self.mvs.push(MV{ x, y });
         }
 
         let shift = if planeno == 0 { 2 } else { 4 };
@@ -682,20 +688,20 @@ const FLAG_KEYFRAME: u16 = 1 << 2;
 const FLAG_NONREF:   u16 = 1 << 8;
 
 impl NADecoder for Indeo3Decoder {
-    fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()> {
+    fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
         if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
             let w = vinfo.get_width();
             let h = vinfo.get_height();
             let fmt = formats::YUV410_FORMAT;
             let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt));
-            self.info = Rc::new(NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()));
+            self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
             self.bufs.reset();
             Ok(())
         } else {
             Err(DecoderError::InvalidData)
         }
     }
-    fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
+    fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
         let src = pkt.get_buffer();
         let mut mr = MemoryReader::new_read(&src);
         let mut br = ByteReader::new(&mut mr);
@@ -709,7 +715,7 @@ impl NADecoder for Indeo3Decoder {
         if (frameno ^ hdr_2 ^ size ^ FRMH_TAG) != check {
             return Err(DecoderError::InvalidData);
         }
-        if (size as i64) > br.left() { return Err(DecoderError::InvalidData); }
+        if i64::from(size) > br.left() { return Err(DecoderError::InvalidData); }
         let ver     = br.read_u16le()?;
         if ver != 32 { return Err(DecoderError::NotImplemented); }
         let flags   = br.read_u16le()?;
@@ -751,16 +757,16 @@ impl NADecoder for Indeo3Decoder {
 
         let intraframe = (flags & FLAG_KEYFRAME) != 0;
         let vinfo = self.info.get_properties().get_video_info().unwrap();
-        let bufret = alloc_video_buffer(vinfo, 2);
-        if let Err(_) = bufret { return Err(DecoderError::InvalidData); }
-        let mut bufinfo = bufret.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 + (yoff as u64);
-        let ustart  = data_start + (uoff as u64);
-        let vstart  = data_start + (voff as u64);
-        let yendpos = data_start + (yend as u64);
-        let uendpos = data_start + (uend as u64);
-        let vendpos = data_start + (vend as u64);
+        let ystart  = data_start + u64::from(yoff);
+        let ustart  = data_start + u64::from(uoff);
+        let vstart  = data_start + u64::from(voff);
+        let yendpos = data_start + u64::from(yend);
+        let uendpos = data_start + u64::from(uend);
+        let vendpos = data_start + u64::from(vend);
         if intraframe {
             self.decode_plane_intra(&mut br, 0, ystart, yendpos)?;
             self.decode_plane_intra(&mut br, 1, ustart, uendpos)?;
@@ -775,11 +781,14 @@ impl NADecoder for Indeo3Decoder {
         let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
         frm.set_keyframe(intraframe);
         frm.set_frame_type(if intraframe { FrameType::I } else { FrameType::P });
-        Ok(Rc::new(RefCell::new(frm)))
+        Ok(frm.into_ref())
+    }
+    fn flush(&mut self) {
+        self.bufs.reset();
     }
 }
 
-pub fn get_decoder() -> Box<NADecoder> {
+pub fn get_decoder() -> Box<dyn NADecoder> {
     Box::new(Indeo3Decoder::new())
 }
 
@@ -797,7 +806,7 @@ mod test {
         let mut dec_reg = RegisteredDecoders::new();
         indeo_register_all_codecs(&mut dec_reg);
 
-        test_file_decoding("avi", "assets/iv32_example.avi", Some(10), true, false, None, &dmx_reg, &dec_reg);
+        test_file_decoding("avi", "assets/Indeo/iv32_example.avi", Some(10), true, false, None, &dmx_reg, &dec_reg);
     }
 }