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 fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
239 let chromaton = info.get_format().get_chromaton(idx);
240 if let None = chromaton { return (0, 0); }
241 let (hs, vs) = chromaton.unwrap().get_subsampling();
242 let w = (info.get_width() + ((1 << hs) - 1)) >> hs;
243 let h = (info.get_height() + ((1 << vs) - 1)) >> vs;
248 pub fn new(pts: Option<u64>,
250 duration: Option<u64>,
253 info: Rc<NACodecInfo>,
254 options: HashMap<String, NAValue>) -> Self {
255 let (buf, offs) = alloc_buf(&info);
256 NAFrame { pts: pts, dts: dts, duration: duration, buffer: buf, offsets: offs, info: info, ftype: ftype, key: keyframe, options: options }
258 pub fn from_copy(src: &NAFrame) -> Self {
259 let buf = copy_buf(&src.get_buffer());
260 let mut offs: Vec<usize> = Vec::new();
261 offs.clone_from(&src.offsets);
262 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() }
264 pub fn get_pts(&self) -> Option<u64> { self.pts }
265 pub fn get_dts(&self) -> Option<u64> { self.dts }
266 pub fn get_duration(&self) -> Option<u64> { self.duration }
267 pub fn get_frame_type(&self) -> FrameType { self.ftype }
268 pub fn is_keyframe(&self) -> bool { self.key }
269 pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
270 pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
271 pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
272 pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; }
273 pub fn set_keyframe(&mut self, key: bool) { self.key = key; }
275 pub fn get_offset(&self, idx: usize) -> usize { self.offsets[idx] }
276 pub fn get_buffer(&self) -> Ref<NABuffer> { self.buffer.borrow() }
277 pub fn get_buffer_mut(&mut self) -> RefMut<NABuffer> { self.buffer.borrow_mut() }
278 pub fn get_stride(&self, idx: usize) -> usize {
279 if let NACodecTypeInfo::Video(vinfo) = self.info.get_properties() {
280 if idx >= vinfo.get_format().get_num_comp() { return 0; }
281 vinfo.get_format().get_chromaton(idx).unwrap().get_linesize(vinfo.get_width())
286 pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
287 match self.info.get_properties() {
288 NACodecTypeInfo::Video(ref vinfo) => get_plane_size(vinfo, idx),
294 pub type NAFrameRef = Rc<RefCell<NAFrame>>;
296 /// Possible stream types.
297 #[derive(Debug,Clone,Copy)]
299 pub enum StreamType {
306 /// any data stream (or might be an unrecognized audio/video stream)
308 /// nonexistent stream
312 impl fmt::Display for StreamType {
313 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
315 StreamType::Video => write!(f, "Video"),
316 StreamType::Audio => write!(f, "Audio"),
317 StreamType::Subtitles => write!(f, "Subtitles"),
318 StreamType::Data => write!(f, "Data"),
319 StreamType::None => write!(f, "-"),
326 pub struct NAStream {
327 media_type: StreamType,
330 info: Rc<NACodecInfo>,
334 pub fn new(mt: StreamType, id: u32, info: NACodecInfo) -> Self {
335 NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info) }
337 pub fn get_id(&self) -> u32 { self.id }
338 pub fn get_num(&self) -> usize { self.num }
339 pub fn set_num(&mut self, num: usize) { self.num = num; }
340 pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
343 impl fmt::Display for NAStream {
344 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
345 write!(f, "({}#{} - {})", self.media_type, self.id, self.info.get_properties())
350 pub struct NAPacket {
351 stream: Rc<NAStream>,
354 duration: Option<u64>,
357 // options: HashMap<String, NAValue<'a>>,
361 pub fn new(str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, vec: Vec<u8>) -> Self {
362 // let mut vec: Vec<u8> = Vec::new();
363 // vec.resize(size, 0);
364 NAPacket { stream: str, pts: pts, dts: dts, duration: dur, keyframe: kf, buffer: Rc::new(vec) }
366 pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() }
367 pub fn get_pts(&self) -> Option<u64> { self.pts }
368 pub fn get_dts(&self) -> Option<u64> { self.dts }
369 pub fn get_duration(&self) -> Option<u64> { self.duration }
370 pub fn is_keyframe(&self) -> bool { self.keyframe }
371 pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() }
374 impl Drop for NAPacket {
375 fn drop(&mut self) {}
378 impl fmt::Display for NAPacket {
379 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
380 let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
381 if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); }
382 if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); }
383 if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); }
384 if self.keyframe { foo = format!("{} kf", foo); }
390 pub trait FrameFromPacket {
391 fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>) -> NAFrame;
392 fn fill_timestamps(&mut self, pkt: &NAPacket);
395 impl FrameFromPacket for NAFrame {
396 fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>) -> NAFrame {
397 NAFrame::new(pkt.pts, pkt.dts, pkt.duration, FrameType::Other, pkt.keyframe, info, HashMap::new())
399 fn fill_timestamps(&mut self, pkt: &NAPacket) {
400 self.set_pts(pkt.pts);
401 self.set_dts(pkt.dts);
402 self.set_duration(pkt.duration);