media: ffmpeg: Add helper to create AvPixelFormat from raw enum values.

Since re-enumerating all the formats supported by FFmpeg in our bindings
would be a rather large effort, it looks like a good compromise here is
to allow the user to create an AvPixelFormat from the raw enum values
exposed by bindgen.

Add a helper that checks that the value is in range of valid formats and
convert it into our enum wrapper.

BUG=b:239897269
TEST=cargo test --features ffmpeg -p ffmpeg

Change-Id: I3c1433e9d76d645b7615bde81433e1df2cfa3910
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3911108
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
This commit is contained in:
Tatsuyuki Ishi 2022-09-22 19:53:09 +09:00 committed by crosvm LUCI
parent a6396869a4
commit 0d94089855

View file

@ -350,6 +350,21 @@ impl AvPixelFormat {
}
}
#[derive(Debug)]
pub struct FromAVPixelFormatError(());
impl TryFrom<ffi::AVPixelFormat> for AvPixelFormat {
type Error = FromAVPixelFormatError;
fn try_from(value: ffi::AVPixelFormat) -> Result<Self, Self::Error> {
if value > ffi::AVPixelFormat_AV_PIX_FMT_NONE && value < ffi::AVPixelFormat_AV_PIX_FMT_NB {
Ok(AvPixelFormat(value))
} else {
Err(FromAVPixelFormatError(()))
}
}
}
impl Display for AvPixelFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.name())