1 use std::collections::HashMap;
8 #[derive(Clone,Copy,PartialEq)]
9 pub struct NAAudioInfo {
17 pub fn new(sr: u32, ch: u8, fmt: NASoniton, bl: usize) -> Self {
18 NAAudioInfo { sample_rate: sr, channels: ch, format: fmt, block_len: bl }
20 pub fn get_sample_rate(&self) -> u32 { self.sample_rate }
21 pub fn get_channels(&self) -> u8 { self.channels }
22 pub fn get_format(&self) -> NASoniton { self.format }
23 pub fn get_block_len(&self) -> usize { self.block_len }
26 impl fmt::Display for NAAudioInfo {
27 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28 write!(f, "{} Hz, {} ch", self.sample_rate, self.channels)
33 #[derive(Clone,Copy,PartialEq)]
34 pub struct NAVideoInfo {
38 format: NAPixelFormaton,
42 pub fn new(w: usize, h: usize, flip: bool, fmt: NAPixelFormaton) -> Self {
43 NAVideoInfo { width: w, height: h, flipped: flip, format: fmt }
45 pub fn get_width(&self) -> usize { self.width as usize }
46 pub fn get_height(&self) -> usize { self.height as usize }
47 pub fn is_flipped(&self) -> bool { self.flipped }
48 pub fn get_format(&self) -> NAPixelFormaton { self.format }
51 impl fmt::Display for NAVideoInfo {
52 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53 write!(f, "{}x{}", self.width, self.height)
57 #[derive(Clone,Copy,PartialEq)]
58 pub enum NACodecTypeInfo {
64 impl fmt::Display for NACodecTypeInfo {
65 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66 let ret = match *self {
67 NACodecTypeInfo::None => format!(""),
68 NACodecTypeInfo::Audio(fmt) => format!("{}", fmt),
69 NACodecTypeInfo::Video(fmt) => format!("{}", fmt),
75 pub type BufferRef = Rc<RefCell<Vec<u8>>>;
84 impl Drop for NABuffer {
85 fn drop(&mut self) { }
89 pub fn get_data(&self) -> Ref<Vec<u8>> { self.data.borrow() }
90 pub fn get_data_mut(&mut self) -> RefMut<Vec<u8>> { self.data.borrow_mut() }
93 pub type NABufferRef = Rc<RefCell<NABuffer>>;
97 pub struct NACodecInfo {
99 properties: NACodecTypeInfo,
100 extradata: Option<Rc<Vec<u8>>>,
104 pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
105 let extradata = match edata {
107 Some(vec) => Some(Rc::new(vec)),
109 NACodecInfo { name: name, properties: p, extradata: extradata }
111 pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Rc<Vec<u8>>>) -> Self {
112 NACodecInfo { name: name, properties: p, extradata: edata }
114 pub fn get_properties(&self) -> NACodecTypeInfo { self.properties }
115 pub fn get_extradata(&self) -> Option<Rc<Vec<u8>>> {
116 if let Some(ref vec) = self.extradata { return Some(vec.clone()); }
119 pub fn get_name(&self) -> &'static str { self.name }
120 pub fn is_video(&self) -> bool {
121 if let NACodecTypeInfo::Video(_) = self.properties { return true; }
124 pub fn is_audio(&self) -> bool {
125 if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
130 impl fmt::Display for NACodecInfo {
131 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
132 let edata = match self.extradata.clone() {
133 None => format!("no extradata"),
134 Some(v) => format!("{} byte(s) of extradata", v.len()),
136 write!(f, "{}: {} {}", self.name, self.properties, edata)
140 pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
142 properties: NACodecTypeInfo::None,
145 fn alloc_video_buf(vinfo: NAVideoInfo, data: &mut Vec<u8>, offs: &mut Vec<usize>) {
146 //todo use overflow detection mul
147 let width = vinfo.width as usize;
148 let height = vinfo.height as usize;
149 let fmt = &vinfo.format;
150 let mut new_size = 0;
151 for i in 0..fmt.get_num_comp() {
152 let chr = fmt.get_chromaton(i).unwrap();
153 if !vinfo.is_flipped() {
154 offs.push(new_size as usize);
156 new_size += chr.get_data_size(width, height);
157 if vinfo.is_flipped() {
158 offs.push(new_size as usize);
161 data.resize(new_size, 0);
164 fn alloc_audio_buf(ainfo: NAAudioInfo, data: &mut Vec<u8>, offs: &mut Vec<usize>) {
166 let length = ((ainfo.sample_rate as usize) * (ainfo.format.get_bits() as usize)) >> 3;
167 let new_size: usize = length * (ainfo.channels as usize);
168 data.resize(new_size, 0);
169 for i in 0..ainfo.channels {
170 if ainfo.format.is_planar() {
171 offs.push((i as usize) * length);
173 offs.push(((i * ainfo.format.get_bits()) >> 3) as usize);
178 pub fn alloc_buf(info: &NACodecInfo) -> (NABufferRef, Vec<usize>) {
179 let mut data: Vec<u8> = Vec::new();
180 let mut offs: Vec<usize> = Vec::new();
181 match info.properties {
182 NACodecTypeInfo::Audio(ainfo) => alloc_audio_buf(ainfo, &mut data, &mut offs),
183 NACodecTypeInfo::Video(vinfo) => alloc_video_buf(vinfo, &mut data, &mut offs),
186 (Rc::new(RefCell::new(NABuffer { id: 0, data: Rc::new(RefCell::new(data)) })), offs)
189 pub fn copy_buf(buf: &NABuffer) -> NABufferRef {
190 let mut data: Vec<u8> = Vec::new();
191 data.clone_from(buf.get_data().as_ref());
192 Rc::new(RefCell::new(NABuffer { id: 0, data: Rc::new(RefCell::new(data)) }))
195 #[derive(Debug,Clone)]
204 #[derive(Debug,Clone,Copy,PartialEq)]
213 impl fmt::Display for FrameType {
214 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
216 FrameType::I => write!(f, "I"),
217 FrameType::P => write!(f, "P"),
218 FrameType::B => write!(f, "B"),
219 FrameType::Other => write!(f, "x"),
229 duration: Option<u64>,
231 info: Rc<NACodecInfo>,
235 options: HashMap<String, NAValue>,
238 pub type NAFrameRef = Rc<RefCell<NAFrame>>;
240 fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
241 let chromaton = info.get_format().get_chromaton(idx);
242 if let None = chromaton { return (0, 0); }
243 let (hs, vs) = chromaton.unwrap().get_subsampling();
244 let w = (info.get_width() + ((1 << hs) - 1)) >> hs;
245 let h = (info.get_height() + ((1 << vs) - 1)) >> vs;
250 pub fn new(pts: Option<u64>,
252 duration: Option<u64>,
255 info: Rc<NACodecInfo>,
256 options: HashMap<String, NAValue>) -> Self {
257 let (buf, offs) = alloc_buf(&info);
258 NAFrame { pts: pts, dts: dts, duration: duration, buffer: buf, offsets: offs, info: info, ftype: ftype, key: keyframe, options: options }
260 pub fn from_copy(src: &NAFrame) -> Self {
261 let buf = copy_buf(&src.get_buffer());
262 let mut offs: Vec<usize> = Vec::new();
263 offs.clone_from(&src.offsets);
264 NAFrame { pts: None, dts: None, duration: None, buffer: buf, offsets: offs, info: src.info.clone(), ftype: src.ftype, key: src.key, options: src.options.clone() }
266 pub fn from_ref(srcref: NAFrameRef) -> Self {
267 let src = srcref.borrow();
268 let buf = copy_buf(&src.get_buffer());
269 let mut offs: Vec<usize> = Vec::new();
270 offs.clone_from(&src.offsets);
271 NAFrame { pts: None, dts: None, duration: None, buffer: buf, offsets: offs, info: src.info.clone(), ftype: src.ftype, key: src.key, options: src.options.clone() }
273 pub fn get_pts(&self) -> Option<u64> { self.pts }
274 pub fn get_dts(&self) -> Option<u64> { self.dts }
275 pub fn get_duration(&self) -> Option<u64> { self.duration }
276 pub fn get_frame_type(&self) -> FrameType { self.ftype }
277 pub fn is_keyframe(&self) -> bool { self.key }
278 pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
279 pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
280 pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
281 pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; }
282 pub fn set_keyframe(&mut self, key: bool) { self.key = key; }
284 pub fn get_offset(&self, idx: usize) -> usize { self.offsets[idx] }
285 pub fn get_buffer(&self) -> Ref<NABuffer> { self.buffer.borrow() }
286 pub fn get_buffer_mut(&mut self) -> RefMut<NABuffer> { self.buffer.borrow_mut() }
287 pub fn get_stride(&self, idx: usize) -> usize {
288 if let NACodecTypeInfo::Video(vinfo) = self.info.get_properties() {
289 if idx >= vinfo.get_format().get_num_comp() { return 0; }
290 vinfo.get_format().get_chromaton(idx).unwrap().get_linesize(vinfo.get_width())
295 pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
296 match self.info.get_properties() {
297 NACodecTypeInfo::Video(ref vinfo) => get_plane_size(vinfo, idx),
303 impl fmt::Display for NAFrame {
304 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
305 let (w, h) = self.get_dimensions(0);
306 let mut foo = format!("frame type {} size {}x{}", self.ftype, w, h);
307 if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); }
308 if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); }
309 if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); }
310 if self.key { foo = format!("{} kf", foo); }
311 write!(f, "[{}]", foo)
315 /// Possible stream types.
316 #[derive(Debug,Clone,Copy)]
318 pub enum StreamType {
325 /// any data stream (or might be an unrecognized audio/video stream)
327 /// nonexistent stream
331 impl fmt::Display for StreamType {
332 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
334 StreamType::Video => write!(f, "Video"),
335 StreamType::Audio => write!(f, "Audio"),
336 StreamType::Subtitles => write!(f, "Subtitles"),
337 StreamType::Data => write!(f, "Data"),
338 StreamType::None => write!(f, "-"),
345 pub struct NAStream {
346 media_type: StreamType,
349 info: Rc<NACodecInfo>,
353 pub fn new(mt: StreamType, id: u32, info: NACodecInfo) -> Self {
354 NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info) }
356 pub fn get_id(&self) -> u32 { self.id }
357 pub fn get_num(&self) -> usize { self.num }
358 pub fn set_num(&mut self, num: usize) { self.num = num; }
359 pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
362 impl fmt::Display for NAStream {
363 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
364 write!(f, "({}#{} - {})", self.media_type, self.id, self.info.get_properties())
369 pub struct NAPacket {
370 stream: Rc<NAStream>,
373 duration: Option<u64>,
376 // options: HashMap<String, NAValue<'a>>,
380 pub fn new(str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, vec: Vec<u8>) -> Self {
381 // let mut vec: Vec<u8> = Vec::new();
382 // vec.resize(size, 0);
383 NAPacket { stream: str, pts: pts, dts: dts, duration: dur, keyframe: kf, buffer: Rc::new(vec) }
385 pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() }
386 pub fn get_pts(&self) -> Option<u64> { self.pts }
387 pub fn get_dts(&self) -> Option<u64> { self.dts }
388 pub fn get_duration(&self) -> Option<u64> { self.duration }
389 pub fn is_keyframe(&self) -> bool { self.keyframe }
390 pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() }
393 impl Drop for NAPacket {
394 fn drop(&mut self) {}
397 impl fmt::Display for NAPacket {
398 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
399 let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
400 if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); }
401 if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); }
402 if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); }
403 if self.keyframe { foo = format!("{} kf", foo); }
409 pub trait FrameFromPacket {
410 fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>) -> NAFrame;
411 fn fill_timestamps(&mut self, pkt: &NAPacket);
414 impl FrameFromPacket for NAFrame {
415 fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>) -> NAFrame {
416 NAFrame::new(pkt.pts, pkt.dts, pkt.duration, FrameType::Other, pkt.keyframe, info, HashMap::new())
418 fn fill_timestamps(&mut self, pkt: &NAPacket) {
419 self.set_pts(pkt.pts);
420 self.set_dts(pkt.dts);
421 self.set_duration(pkt.duration);