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