bitreader: drop unneeded mut
[nihav.git] / src / formats.rs
CommitLineData
fba6f8e4
KS
1use std::string::*;
2use std::fmt;
3
b68ff5ae 4#[derive(Debug,Copy,Clone,PartialEq)]
fba6f8e4
KS
5pub struct NASoniton {
6 bits: u8,
7 be: bool,
8 packed: bool,
9 planar: bool,
10 float: bool,
11 signed: bool,
12}
13
14bitflags! {
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
24pub const SND_U8_FORMAT: NASoniton = NASoniton { bits: 8, be: false, packed: false, planar: false, float: false, signed: false };
25pub const SND_S16_FORMAT: NASoniton = NASoniton { bits: 16, be: false, packed: false, planar: false, float: false, signed: true };
126b7eb8 26pub const SND_F32P_FORMAT: NASoniton = NASoniton { bits: 32, be: false, packed: false, planar: true, float: true, signed: true };
fba6f8e4
KS
27
28impl 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 }
15e41b31
KS
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 }
fba6f8e4
KS
52}
53
54impl 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
10a00d52 62#[derive(Debug,Clone,Copy,PartialEq)]
fba6f8e4
KS
63pub 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
67impl 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
99impl 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
15e41b31 135#[derive(Clone)]
fba6f8e4
KS
136pub struct NAChannelMap {
137 ids: Vec<NAChannelType>,
138}
139
140impl 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 }
10a00d52
KS
145 pub fn add_channels(&mut self, chs: &[NAChannelType]) {
146 for i in 0..chs.len() {
147 self.ids.push(chs[i]);
148 }
149 }
fba6f8e4
KS
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
b68ff5ae 164#[derive(Debug,Clone,Copy,PartialEq)]
fba6f8e4
KS
165pub enum RGBSubmodel {
166 RGB,
167 SRGB,
168}
169
170impl 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
b68ff5ae 180#[derive(Debug,Clone,Copy,PartialEq)]
fba6f8e4
KS
181pub enum YUVSubmodel {
182 YCbCr,
183 YIQ,
184 YUVJ,
185}
186
187impl 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
b68ff5ae 198#[derive(Debug, Clone,Copy,PartialEq)]
fba6f8e4
KS
199pub enum ColorModel {
200 RGB(RGBSubmodel),
201 YUV(YUVSubmodel),
202 CMYK,
203 HSV,
204 LAB,
205 XYZ,
206}
207
208impl ColorModel {
209 pub fn get_default_components(&self) -> usize {
210 match *self {
211 ColorModel::CMYK => 4,
212 _ => 3,
213 }
214 }
215}
216
217impl 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
b68ff5ae 231#[derive(Clone,Copy,PartialEq)]
fba6f8e4
KS
232pub 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
242bitflags! {
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
b68ff5ae 251#[derive(Clone,Copy,PartialEq)]
fba6f8e4
KS
252pub 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
262macro_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
277pub 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
b68ff5ae
KS
285pub 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
fba6f8e4
KS
293pub 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
301pub 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
309pub 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
317impl 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
345impl 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
357impl 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>,
364 flags: FormatonFlags, elem_size: u8) -> Self {
365 let mut chromatons: [Option<NAPixelChromaton>; 5] = [None; 5];
366 let mut ncomp = 0;
367 let be = (flags.bits & FORMATON_FLAG_BE.bits) != 0;
368 let alpha = (flags.bits & FORMATON_FLAG_ALPHA.bits) != 0;
369 let palette = (flags.bits & FORMATON_FLAG_PALETTE.bits) != 0;
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
394impl 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)]
410mod 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}