1 use std::collections::HashMap;
7 #[derive(Clone,Copy,PartialEq)]
8 pub struct 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 }
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 }
25 impl fmt::Display for NAAudioInfo {
26 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27 write!(f, "{} Hz, {} ch", self.sample_rate, self.channels)
32 #[derive(Clone,Copy,PartialEq)]
33 pub struct NAVideoInfo {
37 format: NAPixelFormaton,
41 pub fn new(w: usize, h: usize, flip: bool, fmt: NAPixelFormaton) -> Self {
42 NAVideoInfo { width: w, height: h, flipped: flip, format: fmt }
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 }
50 impl fmt::Display for NAVideoInfo {
51 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52 write!(f, "{}x{}", self.width, self.height)
56 #[derive(Clone,Copy,PartialEq)]
57 pub enum NACodecTypeInfo {
63 impl 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),
82 impl Drop for NABuffer {
83 fn drop(&mut self) { }
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) }
93 pub struct NACodecInfo {
95 properties: NACodecTypeInfo,
96 extradata: Option<Rc<Vec<u8>>>,
100 pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
101 let extradata = match edata {
103 Some(vec) => Some(Rc::new(vec)),
105 NACodecInfo { name: name, properties: p, extradata: extradata }
107 pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Rc<Vec<u8>>>) -> Self {
108 NACodecInfo { name: name, properties: p, extradata: edata }
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()); }
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; }
120 pub fn is_audio(&self) -> bool {
121 if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
126 impl 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()),
132 write!(f, "{}: {} {}", self.name, self.properties, edata)
136 pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
138 properties: NACodecTypeInfo::None,
141 fn 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);
152 new_size += chr.get_data_size(width, height);
153 if vinfo.is_flipped() {
154 offs.push(new_size as usize);
157 data.resize(new_size, 0);
160 fn alloc_audio_buf(ainfo: NAAudioInfo, data: &mut Vec<u8>, offs: &mut Vec<usize>) {
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);
169 offs.push(((i * ainfo.format.get_bits()) >> 3) as usize);
174 pub 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),
182 (Rc::new(NABuffer { id: 0, data: Rc::new(data) }), offs)
185 pub 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) })
191 #[derive(Debug,Clone)]
205 duration: Option<u64>,
206 buffer: Rc<NABuffer>,
207 info: Rc<NACodecInfo>,
209 options: HashMap<String, NAValue>,
212 fn 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;
222 pub fn new(pts: 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 }
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() }
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; }
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())
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),
263 pub struct NACodecContext<'a> {
264 info: &'a NACodecInfo,