utils: vaapi: rework the Rc and Refcell in SurfacePoolHandle

This type has a bug that has gone unnoticed for quite a while.

In order to get better ergonomics and hide away the
borrow()/borrow_mut() calls, the Rc<RefCell<T>> was placed in the
"surfaces" member. #derive(Clone) was also added so that this type could
be cloned without the hassle of the Rc::clone(..) syntax.

This means that calling SurfacePoolHandle::clone() will clone the inner
Rc, i.e. all handles will automatically refer to the same VecDeque. It
will also clone the "coded_resolution" member, which will obviously just
bit-wise copy the u32.

This means that all invariants based on the "coded_resolution" member
will be silently rendered useless, as each SurfacePoolHandle instance
will refer to its own copy of "coded_resolution" to enforce them.

This is not what this type was intended for. It only went unnoticed
because a new pool would be created on every DRC, and the
'coded_resolution' member would never be updated on the old pool.

To fix this, move the Rc<RefCell<T>> to the outer type, such that we can
refer to a single instance for all members of the pool struct. Also,
rename this type from SurfacePoolHandle to SurfacePool, to indicate that
users must heap-allocate the struct themselves if they want to share it.

This fix will let us reuse the pool on DRC without breaking its
invariants.
4 files changed
tree: 3abe5147bb1c43def9c8fc0bdfe2ddbd593404b2
  1. .github/
  2. examples/
  3. src/
  4. .gitignore
  5. Cargo.lock
  6. Cargo.toml
  7. CONTRIBUTING.md
  8. LICENSE
  9. README.md
README.md

Cros-codecs

A lightweight, simple, low-dependency, and hopefully safe crate for hardware-accelerated video decoding and encoding on Linux.

It is developed for use in ChromeOS (particularly crosvm), but has no dependency to ChromeOS and should be usable anywhere.

This crate is still under heavy development and is not recommended for use yet.

Current features

  • Simple decoder API,
  • VAAPI decoder support (using cros-libva) for H.264, VP8 and VP9.

Planned features:

  • Stateful V4L2 decoder support.
  • Stateless V4L2 decoder support.
  • Vaapi encoder support.
  • V4L2 encoder support.
  • Support for H.265 and AV1.
  • C API to be used in non-Rust projects.

Non-goals

  • Support for systems other than Linux.

Example programs

The ccdec example program is included. It can decode an encoded stream and write the decoded frames to a file.