virtio: video: Add support for converting AvPixelFormat -> Format.

For simplicity, use the same error type as Format -> AvPixelFormat.

This will be used and tested in the FFmpeg encoder backend.

BUG=b:239897269
TEST=cargo build --features "video-encoder,video-decoder,ffmpeg"

Change-Id: Ie5cf2a29379c0d52c78cee50ed6571f4859eee75
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3924881
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Commit-Queue: Alexandre Courbot <acourbot@chromium.org>
This commit is contained in:
Tatsuyuki Ishi 2022-09-29 19:46:11 +09:00 committed by crosvm LUCI
parent 244033437e
commit d3e58b0275

View file

@ -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<Format> for AvPixelFormat {
TryFromFormatError(()))
}
}
impl TryFrom<AvPixelFormat> for Format {
type Error = TryFromFormatError;
fn try_from(fmt: AvPixelFormat) -> Result<Self, Self::Error> {
// 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(())),
})
}
}