1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2020 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Data structures that represent video format information in virtio video devices.

use std::convert::From;
use std::convert::Into;
use std::convert::TryFrom;
use std::fmt;
use std::fmt::Display;
use std::io;

use base::error;
use data_model::Le32;
use enumn::N;

use crate::virtio::video::command::ReadCmdError;
use crate::virtio::video::protocol::*;
use crate::virtio::video::response::Response;
use crate::virtio::Writer;

#[derive(PartialEq, Eq, PartialOrd, Ord, N, Clone, Copy, Debug)]
#[repr(u32)]
pub enum Profile {
    H264Baseline = VIRTIO_VIDEO_PROFILE_H264_BASELINE,
    H264Main = VIRTIO_VIDEO_PROFILE_H264_MAIN,
    H264Extended = VIRTIO_VIDEO_PROFILE_H264_EXTENDED,
    H264High = VIRTIO_VIDEO_PROFILE_H264_HIGH,
    H264High10 = VIRTIO_VIDEO_PROFILE_H264_HIGH10PROFILE,
    H264High422 = VIRTIO_VIDEO_PROFILE_H264_HIGH422PROFILE,
    H264High444PredictiveProfile = VIRTIO_VIDEO_PROFILE_H264_HIGH444PREDICTIVEPROFILE,
    H264ScalableBaseline = VIRTIO_VIDEO_PROFILE_H264_SCALABLEBASELINE,
    H264ScalableHigh = VIRTIO_VIDEO_PROFILE_H264_SCALABLEHIGH,
    H264StereoHigh = VIRTIO_VIDEO_PROFILE_H264_STEREOHIGH,
    H264MultiviewHigh = VIRTIO_VIDEO_PROFILE_H264_MULTIVIEWHIGH,
    HevcMain = VIRTIO_VIDEO_PROFILE_HEVC_MAIN,
    HevcMain10 = VIRTIO_VIDEO_PROFILE_HEVC_MAIN10,
    HevcMainStillPicture = VIRTIO_VIDEO_PROFILE_HEVC_MAIN_STILL_PICTURE,
    VP8Profile0 = VIRTIO_VIDEO_PROFILE_VP8_PROFILE0,
    VP8Profile1 = VIRTIO_VIDEO_PROFILE_VP8_PROFILE1,
    VP8Profile2 = VIRTIO_VIDEO_PROFILE_VP8_PROFILE2,
    VP8Profile3 = VIRTIO_VIDEO_PROFILE_VP8_PROFILE3,
    VP9Profile0 = VIRTIO_VIDEO_PROFILE_VP9_PROFILE0,
    VP9Profile1 = VIRTIO_VIDEO_PROFILE_VP9_PROFILE1,
    VP9Profile2 = VIRTIO_VIDEO_PROFILE_VP9_PROFILE2,
    VP9Profile3 = VIRTIO_VIDEO_PROFILE_VP9_PROFILE3,
}
impl_try_from_le32_for_enumn!(Profile, "profile");

impl Profile {
    #[cfg(any(feature = "video-encoder", feature = "libvda", feature = "vaapi"))]
    pub fn to_format(self) -> Format {
        use Profile::*;
        match self {
            H264Baseline
            | H264Main
            | H264Extended
            | H264High
            | H264High10
            | H264High422
            | H264High444PredictiveProfile
            | H264ScalableBaseline
            | H264ScalableHigh
            | H264StereoHigh
            | H264MultiviewHigh => Format::H264,
            HevcMain | HevcMain10 | HevcMainStillPicture => Format::Hevc,
            VP8Profile0 | VP8Profile1 | VP8Profile2 | VP8Profile3 => Format::VP8,
            VP9Profile0 | VP9Profile1 | VP9Profile2 | VP9Profile3 => Format::VP9,
        }
    }
}

#[derive(PartialEq, Eq, PartialOrd, Ord, N, Clone, Copy, Debug)]
#[repr(u32)]
pub enum Level {
    H264_1_0 = VIRTIO_VIDEO_LEVEL_H264_1_0,
    H264_1_1 = VIRTIO_VIDEO_LEVEL_H264_1_1,
    H264_1_2 = VIRTIO_VIDEO_LEVEL_H264_1_2,
    H264_1_3 = VIRTIO_VIDEO_LEVEL_H264_1_3,
    H264_2_0 = VIRTIO_VIDEO_LEVEL_H264_2_0,
    H264_2_1 = VIRTIO_VIDEO_LEVEL_H264_2_1,
    H264_2_2 = VIRTIO_VIDEO_LEVEL_H264_2_2,
    H264_3_0 = VIRTIO_VIDEO_LEVEL_H264_3_0,
    H264_3_1 = VIRTIO_VIDEO_LEVEL_H264_3_1,
    H264_3_2 = VIRTIO_VIDEO_LEVEL_H264_3_2,
    H264_4_0 = VIRTIO_VIDEO_LEVEL_H264_4_0,
    H264_4_1 = VIRTIO_VIDEO_LEVEL_H264_4_1,
    H264_4_2 = VIRTIO_VIDEO_LEVEL_H264_4_2,
    H264_5_0 = VIRTIO_VIDEO_LEVEL_H264_5_0,
    H264_5_1 = VIRTIO_VIDEO_LEVEL_H264_5_1,
}
impl_try_from_le32_for_enumn!(Level, "level");

#[derive(PartialEq, Eq, PartialOrd, Ord, N, Clone, Copy, Debug)]
#[repr(u32)]
pub enum Format {
    // Raw formats
    NV12 = VIRTIO_VIDEO_FORMAT_NV12,
    YUV420 = VIRTIO_VIDEO_FORMAT_YUV420,

    // Bitstream formats
    H264 = VIRTIO_VIDEO_FORMAT_H264,
    Hevc = VIRTIO_VIDEO_FORMAT_HEVC,
    VP8 = VIRTIO_VIDEO_FORMAT_VP8,
    VP9 = VIRTIO_VIDEO_FORMAT_VP9,
}
impl_try_from_le32_for_enumn!(Format, "format");

impl Display for Format {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use Format::*;
        match self {
            NV12 => write!(f, "NV12"),
            YUV420 => write!(f, "YUV420"),
            H264 => write!(f, "H264"),
            Hevc => write!(f, "HEVC"),
            VP8 => write!(f, "VP8"),
            VP9 => write!(f, "VP9"),
        }
    }
}

#[derive(PartialEq, Eq, PartialOrd, Ord, N, Clone, Copy, Debug)]
#[repr(u32)]
pub enum BitrateMode {
    Vbr = VIRTIO_VIDEO_BITRATE_MODE_VBR,
    Cbr = VIRTIO_VIDEO_BITRATE_MODE_CBR,
}
impl_try_from_le32_for_enumn!(BitrateMode, "bitrate_mode");

#[allow(dead_code)]
#[derive(Debug, Copy, Clone)]
pub enum Bitrate {
    /// Constant bitrate.
    Cbr { target: u32 },
    /// Variable bitrate.
    Vbr { target: u32, peak: u32 },
}

#[cfg(feature = "video-encoder")]
impl Bitrate {
    pub fn mode(&self) -> BitrateMode {
        match self {
            Bitrate::Cbr { .. } => BitrateMode::Cbr,
            Bitrate::Vbr { .. } => BitrateMode::Vbr,
        }
    }

    pub fn target(&self) -> u32 {
        match self {
            Bitrate::Cbr { target } => *target,
            Bitrate::Vbr { target, .. } => *target,
        }
    }
}

#[derive(Debug, Default, Copy, Clone)]
pub struct Crop {
    pub left: u32,
    pub top: u32,
    pub width: u32,
    pub height: u32,
}
impl_from_for_interconvertible_structs!(virtio_video_crop, Crop, left, top, width, height);

#[derive(PartialEq, Eq, Debug, Default, Clone, Copy)]
pub struct PlaneFormat {
    pub plane_size: u32,
    pub stride: u32,
}
impl_from_for_interconvertible_structs!(virtio_video_plane_format, PlaneFormat, plane_size, stride);

impl PlaneFormat {
    pub fn get_plane_layout(format: Format, width: u32, height: u32) -> Option<Vec<PlaneFormat>> {
        // Halved size for UV sampling, but rounded up to cover all samples in case of odd input
        // resolution.
        let half_width = (width + 1) / 2;
        let half_height = (height + 1) / 2;
        match format {
            Format::NV12 => Some(vec![
                // Y plane, 1 sample per pixel.
                PlaneFormat {
                    plane_size: width * height,
                    stride: width,
                },
                // UV plane, 1 sample per group of 4 pixels for U and V.
                PlaneFormat {
                    // Add one vertical line so odd resolutions result in an extra UV line to cover
                    // all the Y samples.
                    plane_size: width * half_height,
                    stride: width,
                },
            ]),
            Format::YUV420 => Some(vec![
                // Y plane, 1 sample per pixel.
                PlaneFormat {
                    plane_size: width * height,
                    stride: width,
                },
                // U plane, 1 sample per group of 4 pixels.
                PlaneFormat {
                    plane_size: half_width * half_height,
                    stride: half_width,
                },
                // V plane, same layout as U plane.
                PlaneFormat {
                    plane_size: half_width * half_height,
                    stride: half_width,
                },
            ]),
            _ => None,
        }
    }
}

#[derive(Debug, Default, Clone, Copy)]
pub struct FormatRange {
    pub min: u32,
    pub max: u32,
    pub step: u32,
}
impl_from_for_interconvertible_structs!(virtio_video_format_range, FormatRange, min, max, step);

#[derive(Debug, Default, Clone)]
pub struct FrameFormat {
    pub width: FormatRange,
    pub height: FormatRange,
    pub bitrates: Vec<FormatRange>,
}

impl Response for FrameFormat {
    fn write(&self, w: &mut Writer) -> Result<(), io::Error> {
        w.write_obj(virtio_video_format_frame {
            width: self.width.into(),
            height: self.height.into(),
            num_rates: Le32::from(self.bitrates.len() as u32),
            ..Default::default()
        })?;
        w.write_iter(
            self.bitrates
                .iter()
                .map(|r| Into::<virtio_video_format_range>::into(*r)),
        )
    }
}

#[derive(Debug, Clone)]
pub struct FormatDesc {
    pub mask: u64,
    pub format: Format,
    pub frame_formats: Vec<FrameFormat>,
    pub plane_align: u32,
}

impl Response for FormatDesc {
    fn write(&self, w: &mut Writer) -> Result<(), io::Error> {
        w.write_obj(virtio_video_format_desc {
            mask: self.mask.into(),
            format: Le32::from(self.format as u32),
            // ChromeOS only supports single-buffer mode.
            planes_layout: Le32::from(VIRTIO_VIDEO_PLANES_LAYOUT_SINGLE_BUFFER),
            // No alignment is required on boards that we currently support.
            plane_align: Le32::from(self.plane_align),
            num_frames: Le32::from(self.frame_formats.len() as u32),
        })?;
        self.frame_formats.iter().try_for_each(|ff| ff.write(w))
    }
}

#[cfg(feature = "video-encoder")]
fn clamp_size(size: u32, min: u32, step: u32) -> u32 {
    match step {
        0 | 1 => size,
        _ => {
            let step_mod = (size - min) % step;
            if step_mod == 0 {
                size
            } else {
                size - step_mod + step
            }
        }
    }
}

/// Parses a slice of valid frame formats and the desired resolution
/// and returns the closest available resolution.
#[cfg(feature = "video-encoder")]
pub fn find_closest_resolution(
    frame_formats: &[FrameFormat],
    desired_width: u32,
    desired_height: u32,
) -> (u32, u32) {
    for FrameFormat { width, height, .. } in frame_formats.iter() {
        if desired_width < width.min || desired_width > width.max {
            continue;
        }
        if desired_height < height.min || desired_height > height.max {
            continue;
        }
        let allowed_width = clamp_size(desired_width, width.min, width.step);
        let allowed_height = clamp_size(desired_height, height.min, height.step);
        return (allowed_width, allowed_height);
    }

    // Return the resolution with maximum surface if nothing better is found.
    match frame_formats
        .iter()
        .max_by_key(|format| format.width.max * format.height.max)
    {
        None => (0, 0),
        Some(format) => (format.width.max, format.height.max),
    }
}

/// A rectangle used to describe portions of a frame.
#[derive(Debug, Eq, PartialEq)]
pub struct Rect {
    pub left: i32,
    pub top: i32,
    pub right: i32,
    pub bottom: i32,
}

/// Description of the layout for a single plane.
#[derive(Debug, Clone)]
pub struct FramePlane {
    pub offset: usize,
    pub stride: usize,
    pub size: usize,
}