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