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 representing coded/raw formats.
6
7 use std::os::unix::io::RawFd;
8
9 use enumn::N;
10
11 use super::bindings;
12 use super::error::*;
13
14 /// Represents a FD for bitstream/frame buffer.
15 /// Files described by BufferFd must be accessed from outside of this crate.
16 pub type BufferFd = RawFd;
17
18 /// Represents a video frame plane.
19 pub struct FramePlane {
20 pub offset: i32,
21 pub stride: i32,
22 }
23
24 impl FramePlane {
to_raw_frame_plane(&self) -> bindings::video_frame_plane_t25 pub fn to_raw_frame_plane(&self) -> bindings::video_frame_plane_t {
26 bindings::video_frame_plane_t {
27 offset: self.offset,
28 stride: self.stride,
29 }
30 }
31 }
32
33 // The callers must guarantee that `ptr` is valid for |`num`| elements when both `ptr` and `num`
34 // are valid.
validate_formats<T, U, F>(ptr: *const T, num: usize, f: F) -> Result<Vec<U>> where F: FnMut(&T) -> Result<U>,35 pub(crate) unsafe fn validate_formats<T, U, F>(ptr: *const T, num: usize, f: F) -> Result<Vec<U>>
36 where
37 F: FnMut(&T) -> Result<U>,
38 {
39 if num == 0 {
40 return Err(Error::InvalidCapabilities("num must not be 0".to_string()));
41 }
42 if ptr.is_null() {
43 return Err(Error::InvalidCapabilities(
44 "pointer must not be NULL".to_string(),
45 ));
46 }
47
48 std::slice::from_raw_parts(ptr, num)
49 .iter()
50 .map(f)
51 .collect::<Result<Vec<_>>>()
52 }
53
54 /// Represents a video codec.
55 #[derive(Debug, Clone, Copy, N)]
56 #[repr(i32)]
57 pub enum Profile {
58 H264ProfileBaseline = bindings::video_codec_profile_H264PROFILE_BASELINE,
59 H264ProfileMain = bindings::video_codec_profile_H264PROFILE_MAIN,
60 H264ProfileExtended = bindings::video_codec_profile_H264PROFILE_EXTENDED,
61 H264ProfileHigh = bindings::video_codec_profile_H264PROFILE_HIGH,
62 H264ProfileHigh10Profile = bindings::video_codec_profile_H264PROFILE_HIGH10PROFILE,
63 H264ProfileHigh422Profile = bindings::video_codec_profile_H264PROFILE_HIGH422PROFILE,
64 H264ProfileHigh444PredictiveProfile =
65 bindings::video_codec_profile_H264PROFILE_HIGH444PREDICTIVEPROFILE,
66 H264ProfileScalableBaseline = bindings::video_codec_profile_H264PROFILE_SCALABLEBASELINE,
67 H264ProfileScalableHigh = bindings::video_codec_profile_H264PROFILE_SCALABLEHIGH,
68 H264ProfileStereoHigh = bindings::video_codec_profile_H264PROFILE_STEREOHIGH,
69 H264ProfileMultiviewHigh = bindings::video_codec_profile_H264PROFILE_MULTIVIEWHIGH,
70 VP8 = bindings::video_codec_profile_VP8PROFILE_MIN,
71 VP9Profile0 = bindings::video_codec_profile_VP9PROFILE_PROFILE0,
72 VP9Profile1 = bindings::video_codec_profile_VP9PROFILE_PROFILE1,
73 VP9Profile2 = bindings::video_codec_profile_VP9PROFILE_PROFILE2,
74 VP9Profile3 = bindings::video_codec_profile_VP9PROFILE_PROFILE3,
75 HevcProfileMain = bindings::video_codec_profile_HEVCPROFILE_MAIN,
76 HevcProfileMain10 = bindings::video_codec_profile_HEVCPROFILE_MAIN10,
77 HevcProfileMainStillPicture = bindings::video_codec_profile_HEVCPROFILE_MAIN_STILL_PICTURE,
78 DolbyVisionProfile0 = bindings::video_codec_profile_DOLBYVISION_PROFILE0,
79 DolbyVisionProfile4 = bindings::video_codec_profile_DOLBYVISION_PROFILE4,
80 DolbyVisionProfile5 = bindings::video_codec_profile_DOLBYVISION_PROFILE5,
81 DolbyVisionProfile7 = bindings::video_codec_profile_DOLBYVISION_PROFILE7,
82 Theora = bindings::video_codec_profile_THEORAPROFILE_ANY,
83 Av1ProfileMain = bindings::video_codec_profile_AV1PROFILE_PROFILE_MAIN,
84 Av1ProfileHigh = bindings::video_codec_profile_AV1PROFILE_PROFILE_HIGH,
85 Av1ProfilePro = bindings::video_codec_profile_AV1PROFILE_PROFILE_PRO,
86 }
87
88 impl Profile {
new(p: bindings::video_codec_profile_t) -> Result<Self>89 pub(crate) fn new(p: bindings::video_codec_profile_t) -> Result<Self> {
90 Self::n(p).ok_or(Error::UnknownProfile(p))
91 }
92
to_raw_profile(self) -> bindings::video_codec_profile_t93 pub(crate) fn to_raw_profile(self) -> bindings::video_codec_profile_t {
94 self as bindings::video_codec_profile_t
95 }
96 }
97
98 /// Represents a raw pixel format.
99 #[derive(Debug, Clone, Copy, N)]
100 #[repr(u32)]
101 pub enum PixelFormat {
102 YV12 = bindings::video_pixel_format_YV12,
103 NV12 = bindings::video_pixel_format_NV12,
104 }
105
106 impl PixelFormat {
new(f: bindings::video_pixel_format_t) -> Result<PixelFormat>107 pub(crate) fn new(f: bindings::video_pixel_format_t) -> Result<PixelFormat> {
108 PixelFormat::n(f).ok_or(Error::UnknownPixelFormat(f))
109 }
110
to_raw_pixel_format(self) -> bindings::video_pixel_format_t111 pub(crate) fn to_raw_pixel_format(self) -> bindings::video_pixel_format_t {
112 match self {
113 PixelFormat::YV12 => bindings::video_pixel_format_YV12,
114 PixelFormat::NV12 => bindings::video_pixel_format_NV12,
115 }
116 }
117
from_raw_parts( data: *const bindings::video_pixel_format_t, len: usize, ) -> Result<Vec<Self>>118 pub(crate) unsafe fn from_raw_parts(
119 data: *const bindings::video_pixel_format_t,
120 len: usize,
121 ) -> Result<Vec<Self>> {
122 validate_formats(data, len, |f| Self::new(*f))
123 }
124 }
125