diff --git a/devices/src/virtio/video/ffmpeg.rs b/devices/src/virtio/video/ffmpeg.rs index 0105e89700..4389001c2e 100644 --- a/devices/src/virtio/video/ffmpeg.rs +++ b/devices/src/virtio/video/ffmpeg.rs @@ -127,7 +127,8 @@ impl TryAsAvFrameExt for GuestResource { } } -/// The error returned by `AvPixelFormat::try_from` when there's no applicable format. +/// The error returned when converting between `AvPixelFormat` and `Format` and there's no +/// applicable format. // The empty field prevents constructing this and allows extending it in the future. #[derive(Debug)] pub struct TryFromFormatError(()); @@ -159,3 +160,18 @@ impl TryFrom for AvPixelFormat { TryFromFormatError(())) } } + +impl TryFrom for Format { + type Error = TryFromFormatError; + + fn try_from(fmt: AvPixelFormat) -> Result { + // https://github.com/rust-lang/rust/issues/39371 Lint wrongly warns the consumer + #![allow(non_upper_case_globals)] + use ffmpeg::*; + Ok(match fmt.pix_fmt() { + AVPixelFormat_AV_PIX_FMT_NV12 => Format::NV12, + AVPixelFormat_AV_PIX_FMT_YUV420P => Format::YUV420, + _ => return Err(TryFromFormatError(())), + }) + } +}