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