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