check for missing reference frames in various decoders
[nihav.git] / nihav-realmedia / src / codecs / rv3040.rs
index f7f5334aa2dfe48c99e00ba08c2fe30a26e7c783..d19e3fe5c520805d8ae760e84da98ec7b78b7b4d 100644 (file)
@@ -3,49 +3,12 @@ use nihav_core::frame::{NABufferType, NAVideoInfo, NAVideoBuffer, NAVideoBufferR
 use nihav_core::codecs::{NADecoderSupport, MV, ZERO_MV, DecoderError, DecoderResult, IPBShuffler};
 use nihav_core::io::bitreader::{BitReader,BitReaderMode};
 use nihav_core::io::intcode::*;
+use nihav_core::data::GenericCache;
 use std::mem;
 
 use super::rv34codes::*;
 use super::rv34dsp::*;
 
-pub struct GenericCache<T: Copy> {
-    pub height: usize,
-    pub stride: usize,
-    pub xpos:   usize,
-    pub data:   Vec<T>,
-    pub default: T,
-}
-
-impl<T:Copy> GenericCache<T> {
-    pub fn new(height: usize, stride: usize, default: T) -> Self {
-        let mut ret = Self {
-                stride,
-                height,
-                xpos:   0,
-                data:   Vec::with_capacity((height + 1) * stride),
-                default,
-            };
-        ret.reset();
-        ret
-    }
-    fn full_size(&self) -> usize { self.stride * (self.height + 1) }
-    pub fn reset(&mut self) {
-        self.data.truncate(0);
-        let size = self.full_size();
-        self.data.resize(size, self.default);
-        self.xpos = self.stride + 1;
-    }
-    pub fn update_row(&mut self) {
-        for i in 0..self.stride {
-            self.data[i] = self.data[self.height * self.stride + i];
-        }
-        self.data.truncate(self.stride);
-        let size = self.full_size();
-        self.data.resize(size, self.default);
-        self.xpos = self.stride + 1;
-    }
-}
-
 trait RV34MVScale {
     fn scale(&self, trd: u16, trb: u16) -> (MV, MV);
 }
@@ -1143,6 +1106,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;
@@ -1287,4 +1266,7 @@ impl RV34Decoder {
 
         Ok((NABufferType::Video(buf), hdr0.ftype, ts))
     }
+    pub fn flush(&mut self) {
+        self.ipbs.clear();
+    }
 }