+/// Video frame pool.
+///
+/// This structure allows codec to effectively reuse old frames instead of allocating and de-allocating frames every time.
+/// Caller can also reserve some frames for its own purposes e.g. display queue.
+pub struct NAVideoBufferPool<T:Copy> {
+ pool: Vec<NAVideoBufferRef<T>>,
+ max_len: usize,
+ add_len: usize,
+}
+
+impl<T:Copy> NAVideoBufferPool<T> {
+ /// Constructs a new `NAVideoBufferPool` instance.
+ pub fn new(max_len: usize) -> Self {
+ Self {
+ pool: Vec::with_capacity(max_len),
+ max_len,
+ add_len: 0,
+ }
+ }
+ /// Sets the number of buffers reserved for the user.
+ pub fn set_dec_bufs(&mut self, add_len: usize) {
+ self.add_len = add_len;
+ }
+ /// Returns an unused buffer from the pool.
+ pub fn get_free(&mut self) -> Option<NAVideoBufferRef<T>> {
+ for e in self.pool.iter() {
+ if e.get_num_refs() == 1 {
+ return Some(e.clone());
+ }
+ }
+ None
+ }
+ /// Clones provided frame data into a free pool frame.
+ pub fn get_copy(&mut self, rbuf: &NAVideoBufferRef<T>) -> Option<NAVideoBufferRef<T>> {
+ let mut dbuf = self.get_free()?;
+ dbuf.data.copy_from_slice(&rbuf.data);
+ Some(dbuf)
+ }
+ /// Clears the pool from all frames.
+ pub fn reset(&mut self) {
+ self.pool.truncate(0);
+ }
+}
+
+impl NAVideoBufferPool<u8> {
+ /// Allocates the target amount of video frames using [`alloc_video_buffer`].
+ ///
+ /// [`alloc_video_buffer`]: ./fn.alloc_video_buffer.html
+ pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
+ let nbufs = self.max_len + self.add_len - self.pool.len();
+ for _ in 0..nbufs {
+ let vbuf = alloc_video_buffer(vinfo, align)?;
+ if let NABufferType::Video(buf) = vbuf {
+ self.pool.push(buf);
+ } else if let NABufferType::VideoPacked(buf) = vbuf {
+ self.pool.push(buf);
+ } else {
+ return Err(AllocatorError::FormatError);
+ }
+ }
+ Ok(())
+ }
+}
+
+impl NAVideoBufferPool<u16> {
+ /// Allocates the target amount of video frames using [`alloc_video_buffer`].
+ ///
+ /// [`alloc_video_buffer`]: ./fn.alloc_video_buffer.html
+ pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
+ let nbufs = self.max_len + self.add_len - self.pool.len();
+ for _ in 0..nbufs {
+ let vbuf = alloc_video_buffer(vinfo, align)?;
+ if let NABufferType::Video16(buf) = vbuf {
+ self.pool.push(buf);
+ } else {
+ return Err(AllocatorError::FormatError);
+ }
+ }
+ Ok(())
+ }
+}
+
+impl NAVideoBufferPool<u32> {
+ /// Allocates the target amount of video frames using [`alloc_video_buffer`].
+ ///
+ /// [`alloc_video_buffer`]: ./fn.alloc_video_buffer.html
+ pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
+ let nbufs = self.max_len + self.add_len - self.pool.len();
+ for _ in 0..nbufs {
+ let vbuf = alloc_video_buffer(vinfo, align)?;
+ if let NABufferType::Video32(buf) = vbuf {
+ self.pool.push(buf);
+ } else {
+ return Err(AllocatorError::FormatError);
+ }
+ }
+ Ok(())
+ }
+}
+
+/// Information about codec contained in a stream.