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>
diff --git a/devices/src/virtio/video/ffmpeg.rs b/devices/src/virtio/video/ffmpeg.rs
index 0105e89..4389001 100644
--- a/devices/src/virtio/video/ffmpeg.rs
+++ b/devices/src/virtio/video/ffmpeg.rs
@@ -127,7 +127,8 @@
     }
 }
 
-/// 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 @@
             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(())),
+        })
+    }
+}