hang/catalog/video/
vp9.rs1use serde::{Deserialize, Serialize};
2
3use crate::Error;
4
5#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
10pub struct VP9 {
11 pub profile: u8,
13 pub level: u8,
15 pub bit_depth: u8,
17 pub chroma_subsampling: u8,
19 pub color_primaries: u8,
21 pub transfer_characteristics: u8,
23 pub matrix_coefficients: u8,
25 pub full_range: bool,
27}
28
29impl std::fmt::Display for VP9 {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 write!(f, "vp09.{:02}.{:02}.{:02}", self.profile, self.level, self.bit_depth)?;
34
35 let short = VP9 {
36 profile: self.profile,
37 level: self.level,
38 bit_depth: self.bit_depth,
39 ..Default::default()
40 };
41
42 if self == &short {
43 return Ok(());
44 }
45
46 write!(
47 f,
48 ".{:02}.{:02}.{:02}.{:02}.{:02}",
49 self.chroma_subsampling,
50 self.color_primaries,
51 self.transfer_characteristics,
52 self.matrix_coefficients,
53 self.full_range as u8,
54 )
55 }
56}
57
58impl std::str::FromStr for VP9 {
59 type Err = Error;
60
61 fn from_str(s: &str) -> Result<Self, Self::Err> {
62 let parts = s
64 .strip_prefix("vp09.")
65 .ok_or(Error::InvalidCodec)?
66 .split('.')
67 .map(|field| match field.len() {
68 2 => crate::catalog::codec_number(field, 10),
69 _ => Err(Error::InvalidCodec),
70 })
71 .collect::<Result<Vec<u8>, _>>()?;
72
73 if parts.len() < 3 {
74 return Err(Error::InvalidCodec);
75 }
76
77 let mut vp9 = VP9 {
78 profile: parts[0],
79 level: parts[1],
80 bit_depth: parts[2],
81 ..Default::default()
82 };
83
84 if parts.len() == 3 {
85 return Ok(vp9);
86 } else if parts.len() != 8 {
87 return Err(Error::InvalidCodec);
88 }
89
90 vp9.chroma_subsampling = parts[3];
91 vp9.color_primaries = parts[4];
92 vp9.transfer_characteristics = parts[5];
93 vp9.matrix_coefficients = parts[6];
94 vp9.full_range = parts[7] == 1;
95
96 Ok(vp9)
97 }
98}
99
100impl Default for VP9 {
101 fn default() -> Self {
102 Self {
103 profile: 0,
104 level: 0,
105 bit_depth: 0,
106 chroma_subsampling: 1,
107 color_primaries: 1,
108 transfer_characteristics: 1,
109 matrix_coefficients: 1,
110 full_range: false,
111 }
112 }
113}
114
115#[cfg(test)]
116mod test {
117 use std::str::FromStr;
118
119 use crate::catalog::VideoCodec;
120
121 use super::*;
122
123 #[test]
124 fn test_vp9() {
125 let encoded = "vp09.02.10.10.01.09.16.09.01";
126 let decoded = VP9 {
127 profile: 2,
128 level: 10,
129 bit_depth: 10,
130 chroma_subsampling: 1,
131 color_primaries: 9,
132 transfer_characteristics: 16,
133 matrix_coefficients: 9,
134 full_range: true,
135 }
136 .into();
137
138 let output = VideoCodec::from_str(encoded).expect("failed to parse");
139 assert_eq!(output, decoded);
140
141 let output = decoded.to_string();
142 assert_eq!(output, encoded);
143 }
144
145 #[test]
146 fn test_vp9_short() {
147 let encoded = "vp09.00.41.08";
148 let decoded = VP9 {
149 profile: 0,
150 level: 41,
151 bit_depth: 8,
152 ..Default::default()
153 }
154 .into();
155
156 let output = VideoCodec::from_str(encoded).expect("failed to parse");
157 assert_eq!(output, decoded);
158
159 let output = decoded.to_string();
160 assert_eq!(output, encoded);
161 }
162}