fix copy-paste error
[nihav.git] / src / frame.rs
CommitLineData
5869fd63 1use std::collections::HashMap;
83e603fa 2use std::fmt;
8869d452 3use std::rc::Rc;
fba6f8e4 4use formats::*;
94dbb551 5
5869fd63 6#[allow(dead_code)]
66116504 7#[derive(Clone,Copy,PartialEq)]
5869fd63
KS
8pub struct NAAudioInfo {
9 sample_rate: u32,
10 channels: u8,
11 format: NASoniton,
12 block_len: usize,
13}
14
15impl NAAudioInfo {
16 pub fn new(sr: u32, ch: u8, fmt: NASoniton, bl: usize) -> Self {
17 NAAudioInfo { sample_rate: sr, channels: ch, format: fmt, block_len: bl }
18 }
66116504
KS
19 pub fn get_sample_rate(&self) -> u32 { self.sample_rate }
20 pub fn get_channels(&self) -> u8 { self.channels }
21 pub fn get_format(&self) -> NASoniton { self.format }
22 pub fn get_block_len(&self) -> usize { self.block_len }
5869fd63
KS
23}
24
83e603fa
KS
25impl fmt::Display for NAAudioInfo {
26 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27 write!(f, "{} Hz, {} ch", self.sample_rate, self.channels)
28 }
29}
30
5869fd63 31#[allow(dead_code)]
66116504 32#[derive(Clone,Copy,PartialEq)]
5869fd63 33pub struct NAVideoInfo {
66116504
KS
34 width: usize,
35 height: usize,
5869fd63
KS
36 flipped: bool,
37 format: NAPixelFormaton,
38}
39
40impl NAVideoInfo {
66116504 41 pub fn new(w: usize, h: usize, flip: bool, fmt: NAPixelFormaton) -> Self {
5869fd63
KS
42 NAVideoInfo { width: w, height: h, flipped: flip, format: fmt }
43 }
66116504
KS
44 pub fn get_width(&self) -> usize { self.width as usize }
45 pub fn get_height(&self) -> usize { self.height as usize }
46 pub fn is_flipped(&self) -> bool { self.flipped }
47 pub fn get_format(&self) -> NAPixelFormaton { self.format }
5869fd63
KS
48}
49
83e603fa
KS
50impl fmt::Display for NAVideoInfo {
51 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52 write!(f, "{}x{}", self.width, self.height)
53 }
54}
55
66116504 56#[derive(Clone,Copy,PartialEq)]
5869fd63
KS
57pub enum NACodecTypeInfo {
58 None,
59 Audio(NAAudioInfo),
60 Video(NAVideoInfo),
61}
62
83e603fa
KS
63impl fmt::Display for NACodecTypeInfo {
64 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65 let ret = match *self {
66 NACodecTypeInfo::None => format!(""),
67 NACodecTypeInfo::Audio(fmt) => format!("{}", fmt),
68 NACodecTypeInfo::Video(fmt) => format!("{}", fmt),
69 };
70 write!(f, "{}", ret)
71 }
72}
73
74
5869fd63 75#[allow(dead_code)]
66116504
KS
76#[derive(Clone)]
77pub struct NABuffer {
5869fd63 78 id: u64,
66116504
KS
79 data: Rc<Vec<u8>>,
80}
81
82impl Drop for NABuffer {
83 fn drop(&mut self) { }
84}
85
86impl NABuffer {
87 pub fn get_data(&self) -> Rc<Vec<u8>> { self.data.clone() }
88 pub fn get_data_mut(&mut self) -> Option<&mut Vec<u8>> { Rc::get_mut(&mut self.data) }
5869fd63
KS
89}
90
91#[allow(dead_code)]
8869d452
KS
92#[derive(Clone)]
93pub struct NACodecInfo {
ccae5343 94 name: &'static str,
5869fd63 95 properties: NACodecTypeInfo,
8869d452 96 extradata: Option<Rc<Vec<u8>>>,
5869fd63
KS
97}
98
8869d452 99impl NACodecInfo {
ccae5343 100 pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
8869d452
KS
101 let extradata = match edata {
102 None => None,
103 Some(vec) => Some(Rc::new(vec)),
104 };
ccae5343 105 NACodecInfo { name: name, properties: p, extradata: extradata }
8869d452 106 }
66116504
KS
107 pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Rc<Vec<u8>>>) -> Self {
108 NACodecInfo { name: name, properties: p, extradata: edata }
109 }
8869d452
KS
110 pub fn get_properties(&self) -> NACodecTypeInfo { self.properties }
111 pub fn get_extradata(&self) -> Option<Rc<Vec<u8>>> {
112 if let Some(ref vec) = self.extradata { return Some(vec.clone()); }
113 None
5869fd63 114 }
66116504
KS
115 pub fn get_name(&self) -> &'static str { self.name }
116 pub fn is_video(&self) -> bool {
117 if let NACodecTypeInfo::Video(_) = self.properties { return true; }
118 false
119 }
120 pub fn is_audio(&self) -> bool {
121 if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
122 false
123 }
124}
125
126impl fmt::Display for NACodecInfo {
127 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128 let edata = match self.extradata.clone() {
129 None => format!("no extradata"),
130 Some(v) => format!("{} byte(s) of extradata", v.len()),
131 };
132 write!(f, "{}: {} {}", self.name, self.properties, edata)
133 }
134}
135
136pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
137 name: "none",
138 properties: NACodecTypeInfo::None,
139 extradata: None };
140
141fn alloc_video_buf(vinfo: NAVideoInfo, data: &mut Vec<u8>, offs: &mut Vec<usize>) {
142//todo use overflow detection mul
143 let width = vinfo.width as usize;
144 let height = vinfo.height as usize;
145 let fmt = &vinfo.format;
146 let mut new_size = 0;
147 for i in 0..fmt.get_num_comp() {
148 let chr = fmt.get_chromaton(i).unwrap();
149 if !vinfo.is_flipped() {
150 offs.push(new_size as usize);
151 }
152 new_size += chr.get_data_size(width, height);
153 if vinfo.is_flipped() {
154 offs.push(new_size as usize);
155 }
156 }
157 data.resize(new_size, 0);
5869fd63
KS
158}
159
66116504
KS
160fn alloc_audio_buf(ainfo: NAAudioInfo, data: &mut Vec<u8>, offs: &mut Vec<usize>) {
161//todo better alloc
162 let length = ((ainfo.sample_rate as usize) * (ainfo.format.get_bits() as usize)) >> 3;
163 let new_size: usize = length * (ainfo.channels as usize);
164 data.resize(new_size, 0);
165 for i in 0..ainfo.channels {
166 if ainfo.format.is_planar() {
167 offs.push((i as usize) * length);
168 } else {
169 offs.push(((i * ainfo.format.get_bits()) >> 3) as usize);
170 }
171 }
5869fd63
KS
172}
173
66116504
KS
174pub fn alloc_buf(info: &NACodecInfo) -> (Rc<NABuffer>, Vec<usize>) {
175 let mut data: Vec<u8> = Vec::new();
176 let mut offs: Vec<usize> = Vec::new();
177 match info.properties {
178 NACodecTypeInfo::Audio(ainfo) => alloc_audio_buf(ainfo, &mut data, &mut offs),
179 NACodecTypeInfo::Video(vinfo) => alloc_video_buf(vinfo, &mut data, &mut offs),
180 _ => (),
181 }
182 (Rc::new(NABuffer { id: 0, data: Rc::new(data) }), offs)
183}
184
185pub fn copy_buf(buf: &NABuffer) -> Rc<NABuffer> {
186 let mut data: Vec<u8> = Vec::new();
187 data.clone_from(buf.get_data().as_ref());
188 Rc::new(NABuffer { id: 0, data: Rc::new(data) })
189}
190
191#[derive(Debug,Clone)]
192pub enum NAValue {
5869fd63
KS
193 None,
194 Int(i32),
195 Long(i64),
196 String(String),
66116504 197 Data(Rc<Vec<u8>>),
5869fd63
KS
198}
199
200#[allow(dead_code)]
66116504
KS
201#[derive(Clone)]
202pub struct NAFrame {
5869fd63
KS
203 pts: Option<u64>,
204 dts: Option<u64>,
205 duration: Option<u64>,
66116504
KS
206 buffer: Rc<NABuffer>,
207 info: Rc<NACodecInfo>,
208 offsets: Vec<usize>,
209 options: HashMap<String, NAValue>,
210}
211
212fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
213 let chromaton = info.get_format().get_chromaton(idx);
214 if let None = chromaton { return (0, 0); }
215 let (hs, vs) = chromaton.unwrap().get_subsampling();
216 let w = (info.get_width() + ((1 << hs) - 1)) >> hs;
217 let h = (info.get_height() + ((1 << vs) - 1)) >> vs;
218 (w, h)
219}
220
221impl NAFrame {
222 pub fn new(pts: Option<u64>,
223 dts: Option<u64>,
224 duration: Option<u64>,
225 info: Rc<NACodecInfo>,
226 options: HashMap<String, NAValue>) -> Self {
227 let (buf, offs) = alloc_buf(&info);
228 NAFrame { pts: pts, dts: dts, duration: duration, buffer: buf, offsets: offs, info: info, options: options }
229 }
230 pub fn from_copy(src: &NAFrame) -> Self {
231 let buf = copy_buf(src.get_buffer().as_ref());
232 let mut offs: Vec<usize> = Vec::new();
233 offs.clone_from(&src.offsets);
234 NAFrame { pts: None, dts: None, duration: None, buffer: buf, offsets: offs, info: src.info.clone(), options: src.options.clone() }
235 }
236 pub fn get_pts(&self) -> Option<u64> { self.pts }
237 pub fn get_dts(&self) -> Option<u64> { self.dts }
238 pub fn get_duration(&self) -> Option<u64> { self.duration }
239 pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
240 pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
241 pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
242
243 pub fn get_offset(&self, idx: usize) -> usize { self.offsets[idx] }
244 pub fn get_buffer(&self) -> Rc<NABuffer> { self.buffer.clone() }
245 pub fn get_buffer_mut(&mut self) -> Option<&mut NABuffer> { Rc::get_mut(&mut self.buffer) }
246 pub fn get_stride(&self, idx: usize) -> usize {
247 if let NACodecTypeInfo::Video(vinfo) = self.info.get_properties() {
248 if idx >= vinfo.get_format().get_num_comp() { return 0; }
249 vinfo.get_format().get_chromaton(idx).unwrap().get_linesize(vinfo.get_width())
250 } else {
251 0
252 }
253 }
254 pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
255 match self.info.get_properties() {
256 NACodecTypeInfo::Video(ref vinfo) => get_plane_size(vinfo, idx),
257 _ => (0, 0),
258 }
259 }
5869fd63
KS
260}
261
262#[allow(dead_code)]
263pub struct NACodecContext<'a> {
8869d452 264 info: &'a NACodecInfo,
5869fd63 265}