]>
Commit | Line | Data |
---|---|---|
1 | use std::string::*; | |
2 | use std::fmt; | |
3 | ||
4 | #[derive(Debug,Copy,Clone,PartialEq)] | |
5 | pub struct NASoniton { | |
6 | bits: u8, | |
7 | be: bool, | |
8 | packed: bool, | |
9 | planar: bool, | |
10 | float: bool, | |
11 | signed: bool, | |
12 | } | |
13 | ||
14 | bitflags! { | |
15 | pub flags SonitonFlags: u8 { | |
16 | const SONITON_FLAG_BE = 0x01, | |
17 | const SONITON_FLAG_PACKED = 0x02, | |
18 | const SONITON_FLAG_PLANAR = 0x04, | |
19 | const SONITON_FLAG_FLOAT = 0x08, | |
20 | const SONITON_FLAG_SIGNED = 0x10, | |
21 | } | |
22 | } | |
23 | ||
24 | pub const SND_U8_FORMAT: NASoniton = NASoniton { bits: 8, be: false, packed: false, planar: false, float: false, signed: false }; | |
25 | pub const SND_S16_FORMAT: NASoniton = NASoniton { bits: 16, be: false, packed: false, planar: false, float: false, signed: true }; | |
26 | pub const SND_F32_FORMAT: NASoniton = NASoniton { bits: 32, be: false, packed: false, planar: false, float: true, signed: true }; | |
27 | ||
28 | impl NASoniton { | |
29 | pub fn new(bits: u8, flags: SonitonFlags) -> Self { | |
30 | let is_be = (flags.bits & SONITON_FLAG_BE.bits) != 0; | |
31 | let is_pk = (flags.bits & SONITON_FLAG_PACKED.bits) != 0; | |
32 | let is_pl = (flags.bits & SONITON_FLAG_PLANAR.bits) != 0; | |
33 | let is_fl = (flags.bits & SONITON_FLAG_FLOAT.bits) != 0; | |
34 | let is_sg = (flags.bits & SONITON_FLAG_SIGNED.bits) != 0; | |
35 | NASoniton { bits: bits, be: is_be, packed: is_pk, planar: is_pl, float: is_fl, signed: is_sg } | |
36 | } | |
37 | ||
38 | pub fn get_bits(&self) -> u8 { self.bits } | |
39 | pub fn is_be(&self) -> bool { self.be } | |
40 | pub fn is_packed(&self) -> bool { self.packed } | |
41 | pub fn is_planar(&self) -> bool { self.planar } | |
42 | pub fn is_float(&self) -> bool { self.float } | |
43 | pub fn is_signed(&self) -> bool { self.signed } | |
44 | ||
45 | pub fn get_audio_size(&self, length: u64) -> usize { | |
46 | if self.packed { | |
47 | ((length * (self.bits as u64) + 7) >> 3) as usize | |
48 | } else { | |
49 | (length * (((self.bits + 7) >> 3) as u64)) as usize | |
50 | } | |
51 | } | |
52 | } | |
53 | ||
54 | impl fmt::Display for NASoniton { | |
55 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
56 | let fmt = if self.float { "float" } else if self.signed { "int" } else { "uint" }; | |
57 | let end = if self.be { "BE" } else { "LE" }; | |
58 | write!(f, "({} bps, {} planar: {} packed: {} {})", self.bits, end, self.packed, self.planar, fmt) | |
59 | } | |
60 | } | |
61 | ||
62 | #[derive(Debug,Clone,Copy,PartialEq)] | |
63 | pub enum NAChannelType { | |
64 | C, L, R, Cs, Ls, Rs, Lss, Rss, LFE, Lc, Rc, Lh, Rh, Ch, LFE2, Lw, Rw, Ov, Lhs, Rhs, Chs, Ll, Rl, Cl, Lt, Rt, Lo, Ro | |
65 | } | |
66 | ||
67 | impl NAChannelType { | |
68 | pub fn is_center(&self) -> bool { | |
69 | match *self { | |
70 | NAChannelType::C => true, NAChannelType::Ch => true, | |
71 | NAChannelType::Cl => true, NAChannelType::Ov => true, | |
72 | NAChannelType::LFE => true, NAChannelType::LFE2 => true, | |
73 | NAChannelType::Cs => true, NAChannelType::Chs => true, | |
74 | _ => false, | |
75 | } | |
76 | } | |
77 | pub fn is_left(&self) -> bool { | |
78 | match *self { | |
79 | NAChannelType::L => true, NAChannelType::Ls => true, | |
80 | NAChannelType::Lss => true, NAChannelType::Lc => true, | |
81 | NAChannelType::Lh => true, NAChannelType::Lw => true, | |
82 | NAChannelType::Lhs => true, NAChannelType::Ll => true, | |
83 | NAChannelType::Lt => true, NAChannelType::Lo => true, | |
84 | _ => false, | |
85 | } | |
86 | } | |
87 | pub fn is_right(&self) -> bool { | |
88 | match *self { | |
89 | NAChannelType::R => true, NAChannelType::Rs => true, | |
90 | NAChannelType::Rss => true, NAChannelType::Rc => true, | |
91 | NAChannelType::Rh => true, NAChannelType::Rw => true, | |
92 | NAChannelType::Rhs => true, NAChannelType::Rl => true, | |
93 | NAChannelType::Rt => true, NAChannelType::Ro => true, | |
94 | _ => false, | |
95 | } | |
96 | } | |
97 | } | |
98 | ||
99 | impl fmt::Display for NAChannelType { | |
100 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
101 | let name = match *self { | |
102 | NAChannelType::C => "C".to_string(), | |
103 | NAChannelType::L => "L".to_string(), | |
104 | NAChannelType::R => "R".to_string(), | |
105 | NAChannelType::Cs => "Cs".to_string(), | |
106 | NAChannelType::Ls => "Ls".to_string(), | |
107 | NAChannelType::Rs => "Rs".to_string(), | |
108 | NAChannelType::Lss => "Lss".to_string(), | |
109 | NAChannelType::Rss => "Rss".to_string(), | |
110 | NAChannelType::LFE => "LFE".to_string(), | |
111 | NAChannelType::Lc => "Lc".to_string(), | |
112 | NAChannelType::Rc => "Rc".to_string(), | |
113 | NAChannelType::Lh => "Lh".to_string(), | |
114 | NAChannelType::Rh => "Rh".to_string(), | |
115 | NAChannelType::Ch => "Ch".to_string(), | |
116 | NAChannelType::LFE2 => "LFE2".to_string(), | |
117 | NAChannelType::Lw => "Lw".to_string(), | |
118 | NAChannelType::Rw => "Rw".to_string(), | |
119 | NAChannelType::Ov => "Ov".to_string(), | |
120 | NAChannelType::Lhs => "Lhs".to_string(), | |
121 | NAChannelType::Rhs => "Rhs".to_string(), | |
122 | NAChannelType::Chs => "Chs".to_string(), | |
123 | NAChannelType::Ll => "Ll".to_string(), | |
124 | NAChannelType::Rl => "Rl".to_string(), | |
125 | NAChannelType::Cl => "Cl".to_string(), | |
126 | NAChannelType::Lt => "Lt".to_string(), | |
127 | NAChannelType::Rt => "Rt".to_string(), | |
128 | NAChannelType::Lo => "Lo".to_string(), | |
129 | NAChannelType::Ro => "Ro".to_string(), | |
130 | }; | |
131 | write!(f, "{}", name) | |
132 | } | |
133 | } | |
134 | ||
135 | #[derive(Clone)] | |
136 | pub struct NAChannelMap { | |
137 | ids: Vec<NAChannelType>, | |
138 | } | |
139 | ||
140 | impl NAChannelMap { | |
141 | pub fn new() -> Self { NAChannelMap { ids: Vec::new() } } | |
142 | pub fn add_channel(&mut self, ch: NAChannelType) { | |
143 | self.ids.push(ch); | |
144 | } | |
145 | pub fn add_channels(&mut self, chs: &[NAChannelType]) { | |
146 | for i in 0..chs.len() { | |
147 | self.ids.push(chs[i]); | |
148 | } | |
149 | } | |
150 | pub fn num_channels(&self) -> usize { | |
151 | self.ids.len() | |
152 | } | |
153 | pub fn get_channel(&self, idx: usize) -> NAChannelType { | |
154 | self.ids[idx] | |
155 | } | |
156 | pub fn find_channel_id(&self, t: NAChannelType) -> Option<u8> { | |
157 | for i in 0..self.ids.len() { | |
158 | if self.ids[i] as i32 == t as i32 { return Some(i as u8); } | |
159 | } | |
160 | None | |
161 | } | |
162 | } | |
163 | ||
164 | #[derive(Debug,Clone,Copy,PartialEq)] | |
165 | pub enum RGBSubmodel { | |
166 | RGB, | |
167 | SRGB, | |
168 | } | |
169 | ||
170 | impl fmt::Display for RGBSubmodel { | |
171 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
172 | let name = match *self { | |
173 | RGBSubmodel::RGB => "RGB".to_string(), | |
174 | RGBSubmodel::SRGB => "sRGB".to_string(), | |
175 | }; | |
176 | write!(f, "{}", name) | |
177 | } | |
178 | } | |
179 | ||
180 | #[derive(Debug,Clone,Copy,PartialEq)] | |
181 | pub enum YUVSubmodel { | |
182 | YCbCr, | |
183 | YIQ, | |
184 | YUVJ, | |
185 | } | |
186 | ||
187 | impl fmt::Display for YUVSubmodel { | |
188 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
189 | let name = match *self { | |
190 | YUVSubmodel::YCbCr => "YCbCr".to_string(), | |
191 | YUVSubmodel::YIQ => "YIQ".to_string(), | |
192 | YUVSubmodel::YUVJ => "YUVJ".to_string(), | |
193 | }; | |
194 | write!(f, "{}", name) | |
195 | } | |
196 | } | |
197 | ||
198 | #[derive(Debug, Clone,Copy,PartialEq)] | |
199 | pub enum ColorModel { | |
200 | RGB(RGBSubmodel), | |
201 | YUV(YUVSubmodel), | |
202 | CMYK, | |
203 | HSV, | |
204 | LAB, | |
205 | XYZ, | |
206 | } | |
207 | ||
208 | impl ColorModel { | |
209 | pub fn get_default_components(&self) -> usize { | |
210 | match *self { | |
211 | ColorModel::CMYK => 4, | |
212 | _ => 3, | |
213 | } | |
214 | } | |
215 | } | |
216 | ||
217 | impl fmt::Display for ColorModel { | |
218 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
219 | let name = match *self { | |
220 | ColorModel::RGB(fmt) => format!("RGB({})", fmt).to_string(), | |
221 | ColorModel::YUV(fmt) => format!("YUV({})", fmt).to_string(), | |
222 | ColorModel::CMYK => "CMYK".to_string(), | |
223 | ColorModel::HSV => "HSV".to_string(), | |
224 | ColorModel::LAB => "LAB".to_string(), | |
225 | ColorModel::XYZ => "XYZ".to_string(), | |
226 | }; | |
227 | write!(f, "{}", name) | |
228 | } | |
229 | } | |
230 | ||
231 | #[derive(Clone,Copy,PartialEq)] | |
232 | pub struct NAPixelChromaton { | |
233 | h_ss: u8, | |
234 | v_ss: u8, | |
235 | packed: bool, | |
236 | depth: u8, | |
237 | shift: u8, | |
238 | comp_offs: u8, | |
239 | next_elem: u8, | |
240 | } | |
241 | ||
242 | bitflags! { | |
243 | pub flags FormatonFlags: u8 { | |
244 | const FORMATON_FLAG_BE = 0x01, | |
245 | const FORMATON_FLAG_ALPHA = 0x02, | |
246 | const FORMATON_FLAG_PALETTE = 0x04, | |
247 | } | |
248 | } | |
249 | ||
250 | ||
251 | #[derive(Clone,Copy,PartialEq)] | |
252 | pub struct NAPixelFormaton { | |
253 | model: ColorModel, | |
254 | components: u8, | |
255 | comp_info: [Option<NAPixelChromaton>; 5], | |
256 | elem_size: u8, | |
257 | be: bool, | |
258 | alpha: bool, | |
259 | palette: bool, | |
260 | } | |
261 | ||
262 | macro_rules! chromaton { | |
263 | ($hs: expr, $vs: expr, $pck: expr, $d: expr, $sh: expr, $co: expr, $ne: expr) => ({ | |
264 | Some(NAPixelChromaton{ h_ss: $hs, v_ss: $vs, packed: $pck, depth: $d, shift: $sh, comp_offs: $co, next_elem: $ne }) | |
265 | }); | |
266 | (yuv8; $hs: expr, $vs: expr, $co: expr) => ({ | |
267 | Some(NAPixelChromaton{ h_ss: $hs, v_ss: $vs, packed: false, depth: 8, shift: 0, comp_offs: $co, next_elem: 1 }) | |
268 | }); | |
269 | (packrgb; $d: expr, $s: expr, $co: expr, $ne: expr) => ({ | |
270 | Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: $d, shift: $s, comp_offs: $co, next_elem: $ne }) | |
271 | }); | |
272 | (pal8; $co: expr) => ({ | |
273 | Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: $co, next_elem: 3 }) | |
274 | }); | |
275 | } | |
276 | ||
277 | pub const YUV420_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 3, | |
278 | comp_info: [ | |
279 | chromaton!(0, 0, false, 8, 0, 0, 1), | |
280 | chromaton!(yuv8; 1, 1, 1), | |
281 | chromaton!(yuv8; 1, 1, 2), | |
282 | None, None], | |
283 | elem_size: 0, be: false, alpha: false, palette: false }; | |
284 | ||
285 | pub const YUV410_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 3, | |
286 | comp_info: [ | |
287 | chromaton!(0, 0, false, 8, 0, 0, 1), | |
288 | chromaton!(yuv8; 2, 2, 1), | |
289 | chromaton!(yuv8; 2, 2, 2), | |
290 | None, None], | |
291 | elem_size: 0, be: false, alpha: false, palette: false }; | |
292 | ||
293 | pub const PAL8_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3, | |
294 | comp_info: [ | |
295 | chromaton!(pal8; 0), | |
296 | chromaton!(pal8; 1), | |
297 | chromaton!(pal8; 2), | |
298 | None, None], | |
299 | elem_size: 3, be: false, alpha: false, palette: true }; | |
300 | ||
301 | pub const RGB565_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3, | |
302 | comp_info: [ | |
303 | chromaton!(packrgb; 5, 11, 0, 2), | |
304 | chromaton!(packrgb; 6, 5, 0, 2), | |
305 | chromaton!(packrgb; 5, 0, 0, 2), | |
306 | None, None], | |
307 | elem_size: 2, be: false, alpha: false, palette: false }; | |
308 | ||
309 | pub const RGB24_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3, | |
310 | comp_info: [ | |
311 | chromaton!(packrgb; 8, 0, 2, 3), | |
312 | chromaton!(packrgb; 8, 0, 1, 3), | |
313 | chromaton!(packrgb; 8, 0, 0, 3), | |
314 | None, None], | |
315 | elem_size: 3, be: false, alpha: false, palette: false }; | |
316 | ||
317 | impl NAPixelChromaton { | |
318 | pub fn get_subsampling(&self) -> (u8, u8) { (self.h_ss, self.v_ss) } | |
319 | pub fn is_packed(&self) -> bool { self.packed } | |
320 | pub fn get_depth(&self) -> u8 { self.depth } | |
321 | pub fn get_shift(&self) -> u8 { self.shift } | |
322 | pub fn get_offset(&self) -> u8 { self.comp_offs } | |
323 | pub fn get_step(&self) -> u8 { self.next_elem } | |
324 | ||
325 | pub fn get_width(&self, width: usize) -> usize { | |
326 | (width + ((1 << self.h_ss) - 1)) >> self.h_ss | |
327 | } | |
328 | pub fn get_height(&self, height: usize) -> usize { | |
329 | (height + ((1 << self.v_ss) - 1)) >> self.v_ss | |
330 | } | |
331 | pub fn get_linesize(&self, width: usize) -> usize { | |
332 | let d = self.depth as usize; | |
333 | (self.get_width(width) * d + d - 1) >> 3 | |
334 | } | |
335 | pub fn get_data_size(&self, width: usize, height: usize) -> usize { | |
336 | let nh = (height + ((1 << self.v_ss) - 1)) >> self.v_ss; | |
337 | self.get_linesize(width) * nh | |
338 | } | |
339 | } | |
340 | ||
341 | impl fmt::Display for NAPixelChromaton { | |
342 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
343 | let pfmt = if self.packed { | |
344 | let mask = ((1 << self.depth) - 1) << self.shift; | |
345 | format!("packed(+{},{:X}, step {})", self.comp_offs, mask, self.next_elem) | |
346 | } else { | |
347 | format!("planar({},{})", self.comp_offs, self.next_elem) | |
348 | }; | |
349 | write!(f, "({}x{}, {})", self.h_ss, self.v_ss, pfmt) | |
350 | } | |
351 | } | |
352 | ||
353 | impl NAPixelFormaton { | |
354 | pub fn new(model: ColorModel, | |
355 | comp1: Option<NAPixelChromaton>, | |
356 | comp2: Option<NAPixelChromaton>, | |
357 | comp3: Option<NAPixelChromaton>, | |
358 | comp4: Option<NAPixelChromaton>, | |
359 | comp5: Option<NAPixelChromaton>, | |
360 | flags: FormatonFlags, elem_size: u8) -> Self { | |
361 | let mut chromatons: [Option<NAPixelChromaton>; 5] = [None; 5]; | |
362 | let mut ncomp = 0; | |
363 | let be = (flags.bits & FORMATON_FLAG_BE.bits) != 0; | |
364 | let alpha = (flags.bits & FORMATON_FLAG_ALPHA.bits) != 0; | |
365 | let palette = (flags.bits & FORMATON_FLAG_PALETTE.bits) != 0; | |
366 | if let Some(c) = comp1 { chromatons[0] = Some(c); ncomp += 1; } | |
367 | if let Some(c) = comp2 { chromatons[1] = Some(c); ncomp += 1; } | |
368 | if let Some(c) = comp3 { chromatons[2] = Some(c); ncomp += 1; } | |
369 | if let Some(c) = comp4 { chromatons[3] = Some(c); ncomp += 1; } | |
370 | if let Some(c) = comp5 { chromatons[4] = Some(c); ncomp += 1; } | |
371 | NAPixelFormaton { model: model, | |
372 | components: ncomp, | |
373 | comp_info: chromatons, | |
374 | elem_size: elem_size, | |
375 | be: be, alpha: alpha, palette: palette } | |
376 | } | |
377 | ||
378 | pub fn get_model(&self) -> ColorModel { self.model } | |
379 | pub fn get_num_comp(&self) -> usize { self.components as usize } | |
380 | pub fn get_chromaton(&self, idx: usize) -> Option<NAPixelChromaton> { | |
381 | if idx < self.comp_info.len() { return self.comp_info[idx]; } | |
382 | None | |
383 | } | |
384 | pub fn is_be(&self) -> bool { self.be } | |
385 | pub fn has_alpha(&self) -> bool { self.alpha } | |
386 | pub fn is_paletted(&self) -> bool { self.palette } | |
387 | pub fn get_elem_size(&self) -> u8 { self.elem_size } | |
388 | } | |
389 | ||
390 | impl fmt::Display for NAPixelFormaton { | |
391 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
392 | let end = if self.be { "BE" } else { "LE" }; | |
393 | let palstr = if self.palette { "palette " } else { "" }; | |
394 | let astr = if self.alpha { "alpha " } else { "" }; | |
395 | let mut str = format!("Formaton for {} ({}{}elem {} size {}): ", self.model, palstr, astr,end, self.elem_size); | |
396 | for i in 0..self.comp_info.len() { | |
397 | if let Some(chr) = self.comp_info[i] { | |
398 | str = format!("{} {}", str, chr); | |
399 | } | |
400 | } | |
401 | write!(f, "[{}]", str) | |
402 | } | |
403 | } | |
404 | ||
405 | #[cfg(test)] | |
406 | mod test { | |
407 | use super::*; | |
408 | ||
409 | #[test] | |
410 | fn test_fmt() { | |
411 | println!("{}", SND_S16_FORMAT); | |
412 | println!("{}", SND_U8_FORMAT); | |
413 | println!("{}", SND_F32_FORMAT); | |
414 | println!("formaton yuv- {}", YUV420_FORMAT); | |
415 | println!("formaton pal- {}", PAL8_FORMAT); | |
416 | println!("formaton rgb565- {}", RGB565_FORMAT); | |
417 | } | |
418 | } |