use Self instead of explicit type
[nihav.git] / src / frame.rs
CommitLineData
5869fd63 1use std::collections::HashMap;
8869d452 2use std::rc::Rc;
5869fd63
KS
3
4#[allow(dead_code)]
8869d452 5#[derive(Copy,Clone)]
5869fd63
KS
6pub struct NASoniton {
7 bits: u8,
8 is_be: bool,
9 packed: bool,
10 planar: bool,
11 float: bool,
34e4b617 12 signed: bool,
5869fd63
KS
13}
14
15#[allow(dead_code)]
34e4b617 16pub const SND_U8_FORMAT: NASoniton = NASoniton { bits: 8, is_be: false, packed: false, planar: false, float: false, signed: false };
5869fd63 17#[allow(dead_code)]
34e4b617 18pub const SND_S16_FORMAT: NASoniton = NASoniton { bits: 16, is_be: false, packed: false, planar: false, float: false, signed: true };
5869fd63 19
3b501dcb 20#[derive(Debug,Clone,Copy)]
94dbb551
KS
21pub enum NAChannelType {
22 C, L, R, Ls, Rs, Lss, Rss, LFE, Lc, Rc, Lh, Rh, Ch, LFE2, Lw, Rw, Ov, Lhs, Rhs, Chr, Ll, Rl, Cl, Lt, Rt, Lo, Ro
23}
24
25impl NAChannelType {
26 pub fn is_center(&self) -> bool {
27 match *self {
28 NAChannelType::C => true, NAChannelType::Ch => true,
29 NAChannelType::Cl => true, NAChannelType::Ov => true,
30 NAChannelType::LFE => true, NAChannelType::LFE2 => true,
31 _ => false,
32 }
33 }
34 pub fn is_left(&self) -> bool {
35 match *self {
36 NAChannelType::L => true, NAChannelType::Ls => true,
37 NAChannelType::Lss => true, NAChannelType::Lc => true,
38 NAChannelType::Lh => true, NAChannelType::Lw => true,
39 NAChannelType::Lhs => true, NAChannelType::Ll => true,
40 NAChannelType::Lt => true, NAChannelType::Lo => true,
41 _ => false,
42 }
43 }
44 pub fn is_right(&self) -> bool {
45 match *self {
46 NAChannelType::R => true, NAChannelType::Rs => true,
47 NAChannelType::Rss => true, NAChannelType::Rc => true,
48 NAChannelType::Rh => true, NAChannelType::Rw => true,
49 NAChannelType::Rhs => true, NAChannelType::Rl => true,
50 NAChannelType::Rt => true, NAChannelType::Ro => true,
51 _ => false,
52 }
53 }
54}
55
94dbb551 56pub struct NAChannelMap {
3b501dcb 57 ids: Vec<NAChannelType>,
94dbb551
KS
58}
59
60impl NAChannelMap {
3b501dcb
KS
61 pub fn new() -> Self { NAChannelMap { ids: Vec::new() } }
62 pub fn add_channel(&mut self, ch: NAChannelType) {
63 self.ids.push(ch);
64 }
65 pub fn num_channels(&self) -> usize {
66 self.ids.len()
67 }
68 pub fn get_channel(&self, idx: usize) -> NAChannelType {
69 self.ids[idx]
94dbb551
KS
70 }
71 pub fn find_channel_id(&self, t: NAChannelType) -> Option<u8> {
3b501dcb
KS
72 for i in 0..self.ids.len() {
73 if self.ids[i] as i32 == t as i32 { return Some(i as u8); }
74 }
94dbb551
KS
75 None
76 }
77}
78
5869fd63 79#[allow(dead_code)]
8869d452 80#[derive(Clone,Copy)]
5869fd63
KS
81pub struct NAAudioInfo {
82 sample_rate: u32,
83 channels: u8,
84 format: NASoniton,
85 block_len: usize,
86}
87
88impl NAAudioInfo {
89 pub fn new(sr: u32, ch: u8, fmt: NASoniton, bl: usize) -> Self {
90 NAAudioInfo { sample_rate: sr, channels: ch, format: fmt, block_len: bl }
91 }
92}
93
8869d452 94#[derive(Debug,Clone,Copy)]
5869fd63
KS
95pub enum ColorModel {
96 RGB,
97 YUV,
98 CMYK,
99 HSV,
100 LAB,
101}
102
103#[allow(dead_code)]
8869d452 104#[derive(Clone,Copy)]
5869fd63
KS
105pub struct NAPixelChromaton {
106 h_ss: u8,
107 v_ss: u8,
108 is_packed: bool,
109 depth: u8,
110 shift: u8,
111 comp_offs: u8,
112 next_elem: u8,
113}
114
115#[allow(dead_code)]
8869d452 116#[derive(Clone,Copy)]
5869fd63
KS
117pub struct NAPixelFormaton {
118 model: ColorModel,
119 components: u8,
120 comp_info: [Option<NAPixelChromaton>; 5],
121 elem_size: u8,
122 has_alpha: bool,
123 is_palette: bool,
124}
125
126macro_rules! chromaton {
127 ($hs: expr, $vs: expr, $pck: expr, $d: expr, $sh: expr, $co: expr, $ne: expr) => ({
128 Some(NAPixelChromaton{ h_ss: $hs, v_ss: $vs, is_packed: $pck, depth: $d, shift: $sh, comp_offs: $co, next_elem: $ne })
129 });
130 (yuv8; $hs: expr, $vs: expr, $co: expr) => ({
131 Some(NAPixelChromaton{ h_ss: $hs, v_ss: $vs, is_packed: false, depth: 8, shift: 0, comp_offs: $co, next_elem: 1 })
132 });
133 (pal8; $co: expr) => ({
134 Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, is_packed: true, depth: 8, shift: 0, comp_offs: $co, next_elem: 3 })
135 });
136}
137
138#[allow(dead_code)]
139pub const YUV420_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::YUV, components: 3,
140 comp_info: [
141 chromaton!(0, 0, false, 8, 0, 0, 1),
142 chromaton!(yuv8; 1, 1, 1),
143 chromaton!(yuv8; 1, 1, 2),
144 None, None],
145 elem_size: 0, has_alpha: false, is_palette: false };
146
147#[allow(dead_code)]
148pub const PAL8_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB, components: 3,
149 comp_info: [
150 chromaton!(pal8; 0),
151 chromaton!(pal8; 1),
152 chromaton!(pal8; 2),
153 None, None],
154 elem_size: 1, has_alpha: false, is_palette: true };
155
156
157#[allow(dead_code)]
8869d452 158#[derive(Clone,Copy)]
5869fd63
KS
159pub struct NAVideoInfo {
160 width: u32,
161 height: u32,
162 flipped: bool,
163 format: NAPixelFormaton,
164}
165
166impl NAVideoInfo {
167 pub fn new(w: u32, h: u32, flip: bool, fmt: NAPixelFormaton) -> Self {
168 NAVideoInfo { width: w, height: h, flipped: flip, format: fmt }
169 }
170}
171
8869d452 172#[derive(Clone,Copy)]
5869fd63
KS
173pub enum NACodecTypeInfo {
174 None,
175 Audio(NAAudioInfo),
176 Video(NAVideoInfo),
177}
178
179#[allow(dead_code)]
180pub struct NABuffer<'a> {
181 id: u64,
182 data: &'a mut [u8],
183}
184
185#[allow(dead_code)]
8869d452
KS
186#[derive(Clone)]
187pub struct NACodecInfo {
5869fd63 188 properties: NACodecTypeInfo,
8869d452 189 extradata: Option<Rc<Vec<u8>>>,
5869fd63
KS
190}
191
8869d452
KS
192impl NACodecInfo {
193 pub fn new(p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
194 let extradata = match edata {
195 None => None,
196 Some(vec) => Some(Rc::new(vec)),
197 };
198 NACodecInfo { properties: p, extradata: extradata }
199 }
200 pub fn get_properties(&self) -> NACodecTypeInfo { self.properties }
201 pub fn get_extradata(&self) -> Option<Rc<Vec<u8>>> {
202 if let Some(ref vec) = self.extradata { return Some(vec.clone()); }
203 None
5869fd63
KS
204 }
205}
206
207pub trait NABufferAllocator {
208 fn alloc_buf(info: &NACodecInfo) -> NABuffer<'static>;
209}
210
211#[derive(Debug)]
212pub enum NAValue<'a> {
213 None,
214 Int(i32),
215 Long(i64),
216 String(String),
217 Data(&'a [u8]),
218}
219
220#[allow(dead_code)]
221pub struct NAFrame<'a> {
222 pts: Option<u64>,
223 dts: Option<u64>,
224 duration: Option<u64>,
225 buffer: &'a mut NABuffer<'a>,
8869d452 226 info: &'a NACodecInfo,
5869fd63
KS
227 options: HashMap<String, NAValue<'a>>,
228}
229
230#[allow(dead_code)]
231pub struct NACodecContext<'a> {
8869d452 232 info: &'a NACodecInfo,
5869fd63 233}