• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 //! Data structures that represent video format information in virtio video devices.
6 
7 use std::convert::{From, Into, TryFrom};
8 use std::fmt::{self, Display};
9 use std::io;
10 
11 use base::error;
12 use data_model::Le32;
13 use enumn::N;
14 
15 use crate::virtio::video::command::ReadCmdError;
16 use crate::virtio::video::protocol::*;
17 use crate::virtio::video::response::Response;
18 use crate::virtio::Writer;
19 
20 #[derive(PartialEq, Eq, PartialOrd, Ord, N, Clone, Copy, Debug)]
21 #[repr(u32)]
22 pub enum Profile {
23     H264Baseline = VIRTIO_VIDEO_PROFILE_H264_BASELINE,
24     H264Main = VIRTIO_VIDEO_PROFILE_H264_MAIN,
25     H264Extended = VIRTIO_VIDEO_PROFILE_H264_EXTENDED,
26     H264High = VIRTIO_VIDEO_PROFILE_H264_HIGH,
27     H264High10 = VIRTIO_VIDEO_PROFILE_H264_HIGH10PROFILE,
28     H264High422 = VIRTIO_VIDEO_PROFILE_H264_HIGH422PROFILE,
29     H264High444PredictiveProfile = VIRTIO_VIDEO_PROFILE_H264_HIGH444PREDICTIVEPROFILE,
30     H264ScalableBaseline = VIRTIO_VIDEO_PROFILE_H264_SCALABLEBASELINE,
31     H264ScalableHigh = VIRTIO_VIDEO_PROFILE_H264_SCALABLEHIGH,
32     H264StereoHigh = VIRTIO_VIDEO_PROFILE_H264_STEREOHIGH,
33     H264MultiviewHigh = VIRTIO_VIDEO_PROFILE_H264_MULTIVIEWHIGH,
34     HevcMain = VIRTIO_VIDEO_PROFILE_HEVC_MAIN,
35     HevcMain10 = VIRTIO_VIDEO_PROFILE_HEVC_MAIN10,
36     HevcMainStillPicture = VIRTIO_VIDEO_PROFILE_HEVC_MAIN_STILL_PICTURE,
37     VP8Profile0 = VIRTIO_VIDEO_PROFILE_VP8_PROFILE0,
38     VP8Profile1 = VIRTIO_VIDEO_PROFILE_VP8_PROFILE1,
39     VP8Profile2 = VIRTIO_VIDEO_PROFILE_VP8_PROFILE2,
40     VP8Profile3 = VIRTIO_VIDEO_PROFILE_VP8_PROFILE3,
41     VP9Profile0 = VIRTIO_VIDEO_PROFILE_VP9_PROFILE0,
42     VP9Profile1 = VIRTIO_VIDEO_PROFILE_VP9_PROFILE1,
43     VP9Profile2 = VIRTIO_VIDEO_PROFILE_VP9_PROFILE2,
44     VP9Profile3 = VIRTIO_VIDEO_PROFILE_VP9_PROFILE3,
45 }
46 impl_try_from_le32_for_enumn!(Profile, "profile");
47 
48 impl Profile {
49     #[cfg(any(feature = "video-encoder", feature = "libvda"))]
to_format(&self) -> Format50     pub fn to_format(&self) -> Format {
51         use Profile::*;
52         match self {
53             H264Baseline
54             | H264Main
55             | H264Extended
56             | H264High
57             | H264High10
58             | H264High422
59             | H264High444PredictiveProfile
60             | H264ScalableBaseline
61             | H264ScalableHigh
62             | H264StereoHigh
63             | H264MultiviewHigh => Format::H264,
64             HevcMain | HevcMain10 | HevcMainStillPicture => Format::HEVC,
65             VP8Profile0 | VP8Profile1 | VP8Profile2 | VP8Profile3 => Format::VP8,
66             VP9Profile0 | VP9Profile1 | VP9Profile2 | VP9Profile3 => Format::VP9,
67         }
68     }
69 }
70 
71 #[derive(PartialEq, Eq, PartialOrd, Ord, N, Clone, Copy, Debug)]
72 #[repr(u32)]
73 pub enum Level {
74     H264_1_0 = VIRTIO_VIDEO_LEVEL_H264_1_0,
75     H264_1_1 = VIRTIO_VIDEO_LEVEL_H264_1_1,
76     H264_1_2 = VIRTIO_VIDEO_LEVEL_H264_1_2,
77     H264_1_3 = VIRTIO_VIDEO_LEVEL_H264_1_3,
78     H264_2_0 = VIRTIO_VIDEO_LEVEL_H264_2_0,
79     H264_2_1 = VIRTIO_VIDEO_LEVEL_H264_2_1,
80     H264_2_2 = VIRTIO_VIDEO_LEVEL_H264_2_2,
81     H264_3_0 = VIRTIO_VIDEO_LEVEL_H264_3_0,
82     H264_3_1 = VIRTIO_VIDEO_LEVEL_H264_3_1,
83     H264_3_2 = VIRTIO_VIDEO_LEVEL_H264_3_2,
84     H264_4_0 = VIRTIO_VIDEO_LEVEL_H264_4_0,
85     H264_4_1 = VIRTIO_VIDEO_LEVEL_H264_4_1,
86     H264_4_2 = VIRTIO_VIDEO_LEVEL_H264_4_2,
87     H264_5_0 = VIRTIO_VIDEO_LEVEL_H264_5_0,
88     H264_5_1 = VIRTIO_VIDEO_LEVEL_H264_5_1,
89 }
90 impl_try_from_le32_for_enumn!(Level, "level");
91 
92 #[derive(PartialEq, Eq, PartialOrd, Ord, N, Clone, Copy, Debug)]
93 #[repr(u32)]
94 pub enum Format {
95     // Raw formats
96     NV12 = VIRTIO_VIDEO_FORMAT_NV12,
97     YUV420 = VIRTIO_VIDEO_FORMAT_YUV420,
98 
99     // Bitstream formats
100     H264 = VIRTIO_VIDEO_FORMAT_H264,
101     HEVC = VIRTIO_VIDEO_FORMAT_HEVC,
102     VP8 = VIRTIO_VIDEO_FORMAT_VP8,
103     VP9 = VIRTIO_VIDEO_FORMAT_VP9,
104 }
105 impl_try_from_le32_for_enumn!(Format, "format");
106 
107 impl Display for Format {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result108     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
109         use Format::*;
110         match self {
111             NV12 => write!(f, "NV12"),
112             YUV420 => write!(f, "YUV420"),
113             H264 => write!(f, "H264"),
114             HEVC => write!(f, "HEVC"),
115             VP8 => write!(f, "VP8"),
116             VP9 => write!(f, "VP9"),
117         }
118     }
119 }
120 
121 #[derive(PartialEq, Eq, PartialOrd, Ord, N, Clone, Copy, Debug)]
122 #[repr(u32)]
123 pub enum BitrateMode {
124     VBR = VIRTIO_VIDEO_BITRATE_MODE_VBR,
125     CBR = VIRTIO_VIDEO_BITRATE_MODE_CBR,
126 }
127 impl_try_from_le32_for_enumn!(BitrateMode, "bitrate_mode");
128 
129 #[allow(dead_code)]
130 #[derive(Debug, Copy, Clone)]
131 pub enum Bitrate {
132     /// Constant bitrate.
133     CBR { target: u32 },
134     /// Variable bitrate.
135     VBR { target: u32, peak: u32 },
136 }
137 
138 #[cfg(feature = "video-encoder")]
139 impl Bitrate {
mode(&self) -> BitrateMode140     pub fn mode(&self) -> BitrateMode {
141         match self {
142             Bitrate::CBR { .. } => BitrateMode::CBR,
143             Bitrate::VBR { .. } => BitrateMode::VBR,
144         }
145     }
146 
target(&self) -> u32147     pub fn target(&self) -> u32 {
148         match self {
149             Bitrate::CBR { target } => *target,
150             Bitrate::VBR { target, .. } => *target,
151         }
152     }
153 }
154 
155 #[derive(Debug, Default, Copy, Clone)]
156 pub struct Crop {
157     pub left: u32,
158     pub top: u32,
159     pub width: u32,
160     pub height: u32,
161 }
162 impl_from_for_interconvertible_structs!(virtio_video_crop, Crop, left, top, width, height);
163 
164 #[derive(PartialEq, Eq, Debug, Default, Clone, Copy)]
165 pub struct PlaneFormat {
166     pub plane_size: u32,
167     pub stride: u32,
168 }
169 impl_from_for_interconvertible_structs!(virtio_video_plane_format, PlaneFormat, plane_size, stride);
170 
171 impl PlaneFormat {
get_plane_layout(format: Format, width: u32, height: u32) -> Option<Vec<PlaneFormat>>172     pub fn get_plane_layout(format: Format, width: u32, height: u32) -> Option<Vec<PlaneFormat>> {
173         match format {
174             Format::NV12 => Some(vec![
175                 // Y plane, 1 sample per pixel.
176                 PlaneFormat {
177                     plane_size: width * height,
178                     stride: width,
179                 },
180                 // UV plane, 1 sample per group of 4 pixels for U and V.
181                 PlaneFormat {
182                     // Add one vertical line so odd resolutions result in an extra UV line to cover all the
183                     // Y samples.
184                     plane_size: width * ((height + 1) / 2),
185                     stride: width,
186                 },
187             ]),
188             _ => None,
189         }
190     }
191 }
192 
193 #[derive(Debug, Default, Clone, Copy)]
194 pub struct FormatRange {
195     pub min: u32,
196     pub max: u32,
197     pub step: u32,
198 }
199 impl_from_for_interconvertible_structs!(virtio_video_format_range, FormatRange, min, max, step);
200 
201 #[derive(Debug, Default, Clone)]
202 pub struct FrameFormat {
203     pub width: FormatRange,
204     pub height: FormatRange,
205     pub bitrates: Vec<FormatRange>,
206 }
207 
208 impl Response for FrameFormat {
write(&self, w: &mut Writer) -> Result<(), io::Error>209     fn write(&self, w: &mut Writer) -> Result<(), io::Error> {
210         w.write_obj(virtio_video_format_frame {
211             width: self.width.into(),
212             height: self.height.into(),
213             num_rates: Le32::from(self.bitrates.len() as u32),
214             ..Default::default()
215         })?;
216         w.write_iter(
217             self.bitrates
218                 .iter()
219                 .map(|r| Into::<virtio_video_format_range>::into(*r)),
220         )
221     }
222 }
223 
224 #[derive(Debug, Clone)]
225 pub struct FormatDesc {
226     pub mask: u64,
227     pub format: Format,
228     pub frame_formats: Vec<FrameFormat>,
229 }
230 
231 impl Response for FormatDesc {
write(&self, w: &mut Writer) -> Result<(), io::Error>232     fn write(&self, w: &mut Writer) -> Result<(), io::Error> {
233         w.write_obj(virtio_video_format_desc {
234             mask: self.mask.into(),
235             format: Le32::from(self.format as u32),
236             // ChromeOS only supports single-buffer mode.
237             planes_layout: Le32::from(VIRTIO_VIDEO_PLANES_LAYOUT_SINGLE_BUFFER),
238             // No alignment is required on boards that we currently support.
239             plane_align: Le32::from(0),
240             num_frames: Le32::from(self.frame_formats.len() as u32),
241         })?;
242         self.frame_formats.iter().try_for_each(|ff| ff.write(w))
243     }
244 }
245 
246 #[cfg(feature = "video-encoder")]
clamp_size(size: u32, min: u32, step: u32) -> u32247 fn clamp_size(size: u32, min: u32, step: u32) -> u32 {
248     match step {
249         0 | 1 => size,
250         _ => {
251             let step_mod = (size - min) % step;
252             if step_mod == 0 {
253                 size
254             } else {
255                 size - step_mod + step
256             }
257         }
258     }
259 }
260 
261 /// Parses a slice of valid frame formats and the desired resolution
262 /// and returns the closest available resolution.
263 #[cfg(feature = "video-encoder")]
find_closest_resolution( frame_formats: &[FrameFormat], desired_width: u32, desired_height: u32, ) -> (u32, u32)264 pub fn find_closest_resolution(
265     frame_formats: &[FrameFormat],
266     desired_width: u32,
267     desired_height: u32,
268 ) -> (u32, u32) {
269     for FrameFormat { width, height, .. } in frame_formats.iter() {
270         if desired_width < width.min || desired_width > width.max {
271             continue;
272         }
273         if desired_height < height.min || desired_height > height.max {
274             continue;
275         }
276         let allowed_width = clamp_size(desired_width, width.min, width.step);
277         let allowed_height = clamp_size(desired_height, height.min, height.step);
278         return (allowed_width, allowed_height);
279     }
280 
281     // Return the resolution with maximum surface if nothing better is found.
282     match frame_formats
283         .iter()
284         .max_by_key(|format| format.width.max * format.height.max)
285     {
286         None => (0, 0),
287         Some(format) => (format.width.max, format.height.max),
288     }
289 }
290 
291 /// A rectangle used to describe portions of a frame.
292 #[derive(Debug, Eq, PartialEq)]
293 pub struct Rect {
294     pub left: i32,
295     pub top: i32,
296     pub right: i32,
297     pub bottom: i32,
298 }
299 
300 /// Description of the layout for a single plane.
301 #[derive(Debug, Clone)]
302 pub struct FramePlane {
303     pub offset: usize,
304     pub stride: usize,
305 }
306