use std::cmp::max;
use std::collections::HashMap;
use std::fmt;
-pub use std::rc::Rc;
-pub use std::cell::*;
+use std::sync::Arc;
pub use crate::formats::*;
+pub use crate::refs::*;
#[allow(dead_code)]
#[derive(Clone,Copy,PartialEq)]
impl fmt::Display for NACodecTypeInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let ret = match *self {
- NACodecTypeInfo::None => format!(""),
+ NACodecTypeInfo::None => "".to_string(),
NACodecTypeInfo::Audio(fmt) => format!("{}", fmt),
NACodecTypeInfo::Video(fmt) => format!("{}", fmt),
};
}
}
-pub type NABufferRefT<T> = Rc<RefCell<Vec<T>>>;
-
#[derive(Clone)]
pub struct NAVideoBuffer<T> {
info: NAVideoInfo,
- data: NABufferRefT<T>,
+ data: NABufferRef<Vec<T>>,
offs: Vec<usize>,
strides: Vec<usize>,
}
else { self.offs[idx] }
}
pub fn get_info(&self) -> NAVideoInfo { self.info }
- pub fn get_data(&self) -> Ref<Vec<T>> { self.data.borrow() }
- pub fn get_data_mut(&mut self) -> RefMut<Vec<T>> { self.data.borrow_mut() }
+ pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
+ pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
pub fn copy_buffer(&mut self) -> Self {
- let mut data: Vec<T> = Vec::with_capacity(self.data.borrow().len());
- data.clone_from(self.data.borrow().as_ref());
+ let mut data: Vec<T> = Vec::with_capacity(self.data.len());
+ data.clone_from(self.data.as_ref());
let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
offs.clone_from(&self.offs);
let mut strides: Vec<usize> = Vec::with_capacity(self.strides.len());
strides.clone_from(&self.strides);
- NAVideoBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, strides: strides }
+ NAVideoBuffer { info: self.info, data: NABufferRef::new(data), offs, strides }
}
pub fn get_stride(&self, idx: usize) -> usize {
if idx >= self.strides.len() { return 0; }
pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
get_plane_size(&self.info, idx)
}
+ pub fn into_ref(self) -> NABufferRef<Self> {
+ NABufferRef::new(self)
+ }
}
+pub type NAVideoBufferRef<T> = NABufferRef<NAVideoBuffer<T>>;
+
#[derive(Clone)]
pub struct NAAudioBuffer<T> {
info: NAAudioInfo,
- data: NABufferRefT<T>,
+ data: NABufferRef<Vec<T>>,
offs: Vec<usize>,
chmap: NAChannelMap,
len: usize,
}
pub fn get_info(&self) -> NAAudioInfo { self.info }
pub fn get_chmap(&self) -> NAChannelMap { self.chmap.clone() }
- pub fn get_data(&self) -> Ref<Vec<T>> { self.data.borrow() }
- pub fn get_data_mut(&mut self) -> RefMut<Vec<T>> { self.data.borrow_mut() }
+ pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
+ pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
pub fn copy_buffer(&mut self) -> Self {
- let mut data: Vec<T> = Vec::with_capacity(self.data.borrow().len());
- data.clone_from(self.data.borrow().as_ref());
+ let mut data: Vec<T> = Vec::with_capacity(self.data.len());
+ data.clone_from(self.data.as_ref());
let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
offs.clone_from(&self.offs);
- NAAudioBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, chmap: self.get_chmap(), len: self.len }
+ NAAudioBuffer { info: self.info, data: NABufferRef::new(data), offs, chmap: self.get_chmap(), len: self.len }
}
pub fn get_length(&self) -> usize { self.len }
}
impl NAAudioBuffer<u8> {
- pub fn new_from_buf(info: NAAudioInfo, data: NABufferRefT<u8>, chmap: NAChannelMap) -> Self {
- let len = data.borrow().len();
- NAAudioBuffer { info: info, data: data, chmap: chmap, offs: Vec::new(), len: len }
+ pub fn new_from_buf(info: NAAudioInfo, data: NABufferRef<Vec<u8>>, chmap: NAChannelMap) -> Self {
+ let len = data.len();
+ NAAudioBuffer { info, data, chmap, offs: Vec::new(), len }
}
}
#[derive(Clone)]
pub enum NABufferType {
- Video (NAVideoBuffer<u8>),
- Video16 (NAVideoBuffer<u16>),
- Video32 (NAVideoBuffer<u32>),
- VideoPacked(NAVideoBuffer<u8>),
+ Video (NAVideoBufferRef<u8>),
+ Video16 (NAVideoBufferRef<u16>),
+ Video32 (NAVideoBufferRef<u32>),
+ VideoPacked(NAVideoBufferRef<u8>),
AudioU8 (NAAudioBuffer<u8>),
AudioI16 (NAAudioBuffer<i16>),
AudioI32 (NAAudioBuffer<i32>),
AudioF32 (NAAudioBuffer<f32>),
AudioPacked(NAAudioBuffer<u8>),
- Data (NABufferRefT<u8>),
+ Data (NABufferRef<Vec<u8>>),
None,
}
_ => None,
}
}
- pub fn get_vbuf(&mut self) -> Option<NAVideoBuffer<u8>> {
+ pub fn get_vbuf(&self) -> Option<NAVideoBufferRef<u8>> {
match *self {
NABufferType::Video(ref vb) => Some(vb.clone()),
NABufferType::VideoPacked(ref vb) => Some(vb.clone()),
_ => None,
}
}
- pub fn get_vbuf16(&mut self) -> Option<NAVideoBuffer<u16>> {
+ pub fn get_vbuf16(&self) -> Option<NAVideoBufferRef<u16>> {
match *self {
NABufferType::Video16(ref vb) => Some(vb.clone()),
_ => None,
}
}
- pub fn get_vbuf32(&mut self) -> Option<NAVideoBuffer<u32>> {
+ pub fn get_vbuf32(&self) -> Option<NAVideoBufferRef<u32>> {
match *self {
NABufferType::Video32(ref vb) => Some(vb.clone()),
_ => None,
}
}
- pub fn get_abuf_u8(&mut self) -> Option<NAAudioBuffer<u8>> {
+ pub fn get_abuf_u8(&self) -> Option<NAAudioBuffer<u8>> {
match *self {
NABufferType::AudioU8(ref ab) => Some(ab.clone()),
NABufferType::AudioPacked(ref ab) => Some(ab.clone()),
_ => None,
}
}
- pub fn get_abuf_i16(&mut self) -> Option<NAAudioBuffer<i16>> {
+ pub fn get_abuf_i16(&self) -> Option<NAAudioBuffer<i16>> {
match *self {
NABufferType::AudioI16(ref ab) => Some(ab.clone()),
_ => None,
}
}
- pub fn get_abuf_i32(&mut self) -> Option<NAAudioBuffer<i32>> {
+ pub fn get_abuf_i32(&self) -> Option<NAAudioBuffer<i32>> {
match *self {
NABufferType::AudioI32(ref ab) => Some(ab.clone()),
_ => None,
}
}
- pub fn get_abuf_f32(&mut self) -> Option<NAAudioBuffer<f32>> {
+ pub fn get_abuf_f32(&self) -> Option<NAAudioBuffer<f32>> {
match *self {
NABufferType::AudioF32(ref ab) => Some(ab.clone()),
_ => None,
}
}
+const NA_SIMPLE_VFRAME_COMPONENTS: usize = 4;
+pub struct NASimpleVideoFrame<'a, T: Copy> {
+ pub width: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
+ pub height: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
+ pub flip: bool,
+ pub stride: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
+ pub offset: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
+ pub components: usize,
+ pub data: &'a mut [T],
+}
+
+impl<'a, T:Copy> NASimpleVideoFrame<'a, T> {
+ pub fn from_video_buf(vbuf: &'a mut NAVideoBuffer<T>) -> Option<Self> {
+ let vinfo = vbuf.get_info();
+ let components = vinfo.format.components as usize;
+ if components > NA_SIMPLE_VFRAME_COMPONENTS {
+ return None;
+ }
+ let mut w: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
+ let mut h: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
+ let mut s: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
+ let mut o: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
+ for comp in 0..components {
+ let (width, height) = vbuf.get_dimensions(comp);
+ w[comp] = width;
+ h[comp] = height;
+ s[comp] = vbuf.get_stride(comp);
+ o[comp] = vbuf.get_offset(comp);
+ }
+ let flip = vinfo.flipped;
+ Some(NASimpleVideoFrame {
+ width: w,
+ height: h,
+ flip,
+ stride: s,
+ offset: o,
+ components,
+ data: vbuf.data.as_mut_slice(),
+ })
+ }
+}
+
#[derive(Debug,Clone,Copy,PartialEq)]
pub enum AllocatorError {
TooLargeDimensions,
let mut all_bytealigned = true;
for i in 0..fmt.get_num_comp() {
let ochr = fmt.get_chromaton(i);
- if let None = ochr { continue; }
+ if ochr.is_none() { continue; }
let chr = ochr.unwrap();
if !chr.is_packed() {
all_packed = false;
offs.push(0);
offs.push(stride * height);
strides.push(stride);
- let mut data: Vec<u8> = Vec::with_capacity(new_size.unwrap());
- data.resize(new_size.unwrap(), 0);
- let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
- Ok(NABufferType::Video(buf))
+ let data: Vec<u8> = vec![0; new_size.unwrap()];
+ let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
+ Ok(NABufferType::Video(buf.into_ref()))
} else if !all_packed {
for i in 0..fmt.get_num_comp() {
let ochr = fmt.get_chromaton(i);
- if let None = ochr { continue; }
+ if ochr.is_none() { continue; }
let chr = ochr.unwrap();
- if !vinfo.is_flipped() {
- offs.push(new_size as usize);
- }
+ offs.push(new_size as usize);
let stride = chr.get_linesize(width);
let cur_h = chr.get_height(height);
let cur_sz = stride.checked_mul(cur_h);
let new_sz = new_size.checked_add(cur_sz.unwrap());
if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
new_size = new_sz.unwrap();
- if vinfo.is_flipped() {
- offs.push(new_size as usize);
- }
strides.push(stride);
}
if max_depth <= 8 {
- let mut data: Vec<u8> = Vec::with_capacity(new_size);
- data.resize(new_size, 0);
- let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
- Ok(NABufferType::Video(buf))
+ let data: Vec<u8> = vec![0; new_size];
+ let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
+ Ok(NABufferType::Video(buf.into_ref()))
} else if max_depth <= 16 {
- let mut data: Vec<u16> = Vec::with_capacity(new_size);
- data.resize(new_size, 0);
- let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
- Ok(NABufferType::Video16(buf))
+ let data: Vec<u16> = vec![0; new_size];
+ let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
+ Ok(NABufferType::Video16(buf.into_ref()))
} else {
- let mut data: Vec<u32> = Vec::with_capacity(new_size);
- data.resize(new_size, 0);
- let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
- Ok(NABufferType::Video32(buf))
+ let data: Vec<u32> = vec![0; new_size];
+ let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
+ Ok(NABufferType::Video32(buf.into_ref()))
}
} else if all_bytealigned || unfit_elem_size {
let elem_sz = fmt.get_elem_size();
let new_sz = line_sz.unwrap().checked_mul(height);
if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
new_size = new_sz.unwrap();
- let mut data: Vec<u8> = Vec::with_capacity(new_size);
- data.resize(new_size, 0);
+ let data: Vec<u8> = vec![0; new_size];
strides.push(line_sz.unwrap());
- let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
- Ok(NABufferType::VideoPacked(buf))
+ let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
+ Ok(NABufferType::VideoPacked(buf.into_ref()))
} else {
let elem_sz = fmt.get_elem_size();
let new_sz = width.checked_mul(height);
new_size = new_sz.unwrap();
match elem_sz {
2 => {
- let mut data: Vec<u16> = Vec::with_capacity(new_size);
- data.resize(new_size, 0);
+ let data: Vec<u16> = vec![0; new_size];
strides.push(width);
- let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
- Ok(NABufferType::Video16(buf))
+ let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
+ Ok(NABufferType::Video16(buf.into_ref()))
},
4 => {
- let mut data: Vec<u32> = Vec::with_capacity(new_size);
- data.resize(new_size, 0);
+ let data: Vec<u32> = vec![0; new_size];
strides.push(width);
- let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
- Ok(NABufferType::Video32(buf))
+ let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
+ Ok(NABufferType::Video32(buf.into_ref()))
},
_ => unreachable!(),
}
}
}
+#[allow(clippy::collapsible_if)]
pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> {
let mut offs: Vec<usize> = Vec::new();
if ainfo.format.is_planar() || (ainfo.channels == 1 && (ainfo.format.get_bits() % 8) == 0) {
}
if ainfo.format.is_float() {
if ainfo.format.get_bits() == 32 {
- let mut data: Vec<f32> = Vec::with_capacity(length);
- data.resize(length, 0.0);
- let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
+ let data: Vec<f32> = vec![0.0; length];
+ let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples };
Ok(NABufferType::AudioF32(buf))
} else {
Err(AllocatorError::TooLargeDimensions)
}
} else {
if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() {
- let mut data: Vec<u8> = Vec::with_capacity(length);
- data.resize(length, 0);
- let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
+ let data: Vec<u8> = vec![0; length];
+ let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples };
Ok(NABufferType::AudioU8(buf))
} else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() {
- let mut data: Vec<i16> = Vec::with_capacity(length);
- data.resize(length, 0);
- let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
+ let data: Vec<i16> = vec![0; length];
+ let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples };
Ok(NABufferType::AudioI16(buf))
} else {
Err(AllocatorError::TooLargeDimensions)
let len = nsamples.checked_mul(ainfo.channels as usize);
if len == None { return Err(AllocatorError::TooLargeDimensions); }
let length = ainfo.format.get_audio_size(len.unwrap() as u64);
- let mut data: Vec<u8> = Vec::with_capacity(length);
- data.resize(length, 0);
- let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
+ let data: Vec<u8> = vec![0; length];
+ let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples };
Ok(NABufferType::AudioPacked(buf))
}
}
pub fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> {
- let mut data: Vec<u8> = Vec::with_capacity(size);
- data.resize(size, 0);
- let buf: NABufferRefT<u8> = Rc::new(RefCell::new(data));
+ let data: Vec<u8> = vec![0; size];
+ let buf: NABufferRef<Vec<u8>> = NABufferRef::new(data);
Ok(NABufferType::Data(buf))
}
buf.clone()
}
+pub struct NAVideoBufferPool<T:Copy> {
+ pool: Vec<NAVideoBufferRef<T>>,
+ max_len: usize,
+ add_len: usize,
+}
+
+impl<T:Copy> NAVideoBufferPool<T> {
+ pub fn new(max_len: usize) -> Self {
+ Self {
+ pool: Vec::with_capacity(max_len),
+ max_len,
+ add_len: 0,
+ }
+ }
+ pub fn set_dec_bufs(&mut self, add_len: usize) {
+ self.add_len = add_len;
+ }
+ pub fn get_free(&mut self) -> Option<NAVideoBufferRef<T>> {
+ for e in self.pool.iter() {
+ if e.get_num_refs() == 1 {
+ return Some(e.clone());
+ }
+ }
+ None
+ }
+ pub fn get_copy(&mut self, rbuf: &NAVideoBufferRef<T>) -> Option<NAVideoBufferRef<T>> {
+ let mut dbuf = self.get_free()?;
+ dbuf.data.copy_from_slice(&rbuf.data);
+ Some(dbuf)
+ }
+ pub fn reset(&mut self) {
+ self.pool.truncate(0);
+ }
+}
+
+impl NAVideoBufferPool<u8> {
+ pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
+ let nbufs = self.max_len + self.add_len - self.pool.len();
+ for _ in 0..nbufs {
+ let vbuf = alloc_video_buffer(vinfo, align)?;
+ if let NABufferType::Video(buf) = vbuf {
+ self.pool.push(buf);
+ } else if let NABufferType::VideoPacked(buf) = vbuf {
+ self.pool.push(buf);
+ } else {
+ return Err(AllocatorError::FormatError);
+ }
+ }
+ Ok(())
+ }
+}
+
+impl NAVideoBufferPool<u16> {
+ pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
+ let nbufs = self.max_len + self.add_len - self.pool.len();
+ for _ in 0..nbufs {
+ let vbuf = alloc_video_buffer(vinfo, align)?;
+ if let NABufferType::Video16(buf) = vbuf {
+ self.pool.push(buf);
+ } else {
+ return Err(AllocatorError::FormatError);
+ }
+ }
+ Ok(())
+ }
+}
+
+impl NAVideoBufferPool<u32> {
+ pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
+ let nbufs = self.max_len + self.add_len - self.pool.len();
+ for _ in 0..nbufs {
+ let vbuf = alloc_video_buffer(vinfo, align)?;
+ if let NABufferType::Video32(buf) = vbuf {
+ self.pool.push(buf);
+ } else {
+ return Err(AllocatorError::FormatError);
+ }
+ }
+ Ok(())
+ }
+}
+
#[allow(dead_code)]
#[derive(Clone)]
pub struct NACodecInfo {
name: &'static str,
properties: NACodecTypeInfo,
- extradata: Option<Rc<Vec<u8>>>,
+ extradata: Option<Arc<Vec<u8>>>,
}
+pub type NACodecInfoRef = Arc<NACodecInfo>;
+
impl NACodecInfo {
pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
let extradata = match edata {
None => None,
- Some(vec) => Some(Rc::new(vec)),
+ Some(vec) => Some(Arc::new(vec)),
};
- NACodecInfo { name: name, properties: p, extradata: extradata }
+ NACodecInfo { name, properties: p, extradata }
}
- pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Rc<Vec<u8>>>) -> Self {
- NACodecInfo { name: name, properties: p, extradata: edata }
+ pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Arc<Vec<u8>>>) -> Self {
+ NACodecInfo { name, properties: p, extradata: edata }
}
+ pub fn into_ref(self) -> NACodecInfoRef { Arc::new(self) }
pub fn get_properties(&self) -> NACodecTypeInfo { self.properties }
- pub fn get_extradata(&self) -> Option<Rc<Vec<u8>>> {
+ pub fn get_extradata(&self) -> Option<Arc<Vec<u8>>> {
if let Some(ref vec) = self.extradata { return Some(vec.clone()); }
None
}
if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
false
}
- pub fn new_dummy() -> Rc<Self> {
- Rc::new(DUMMY_CODEC_INFO)
+ pub fn new_dummy() -> Arc<Self> {
+ Arc::new(DUMMY_CODEC_INFO)
}
- pub fn replace_info(&self, p: NACodecTypeInfo) -> Rc<Self> {
- Rc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() })
+ pub fn replace_info(&self, p: NACodecTypeInfo) -> Arc<Self> {
+ Arc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() })
}
}
impl fmt::Display for NACodecInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let edata = match self.extradata.clone() {
- None => format!("no extradata"),
+ None => "no extradata".to_string(),
Some(v) => format!("{} byte(s) of extradata", v.len()),
};
write!(f, "{}: {} {}", self.name, self.properties, edata)
Int(i32),
Long(i64),
String(String),
- Data(Rc<Vec<u8>>),
+ Data(Arc<Vec<u8>>),
}
#[derive(Debug,Clone,Copy,PartialEq)]
impl NATimeInfo {
pub fn new(pts: Option<u64>, dts: Option<u64>, duration: Option<u64>, tb_num: u32, tb_den: u32) -> Self {
- NATimeInfo { pts: pts, dts: dts, duration: duration, tb_num: tb_num, tb_den: tb_den }
+ NATimeInfo { pts, dts, duration, tb_num, tb_den }
}
pub fn get_pts(&self) -> Option<u64> { self.pts }
pub fn get_dts(&self) -> Option<u64> { self.dts }
pub struct NAFrame {
ts: NATimeInfo,
buffer: NABufferType,
- info: Rc<NACodecInfo>,
+ info: NACodecInfoRef,
ftype: FrameType,
key: bool,
options: HashMap<String, NAValue>,
}
-pub type NAFrameRef = Rc<RefCell<NAFrame>>;
+pub type NAFrameRef = Arc<NAFrame>;
fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
let chromaton = info.get_format().get_chromaton(idx);
- if let None = chromaton { return (0, 0); }
+ if chromaton.is_none() { return (0, 0); }
let (hs, vs) = chromaton.unwrap().get_subsampling();
let w = (info.get_width() + ((1 << hs) - 1)) >> hs;
let h = (info.get_height() + ((1 << vs) - 1)) >> vs;
pub fn new(ts: NATimeInfo,
ftype: FrameType,
keyframe: bool,
- info: Rc<NACodecInfo>,
+ info: NACodecInfoRef,
options: HashMap<String, NAValue>,
buffer: NABufferType) -> Self {
- NAFrame { ts: ts, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options }
+ NAFrame { ts, buffer, info, ftype, key: keyframe, options }
}
- pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
+ pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
pub fn get_frame_type(&self) -> FrameType { self.ftype }
pub fn is_keyframe(&self) -> bool { self.key }
pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; }
pub fn set_duration(&mut self, dur: Option<u64>) { self.ts.set_duration(dur); }
pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() }
+
+ pub fn into_ref(self) -> NAFrameRef { Arc::new(self) }
}
impl fmt::Display for NAFrame {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let mut foo = format!("frame type {}", self.ftype);
- if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); }
- if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); }
- if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); }
- if self.key { foo = format!("{} kf", foo); }
- write!(f, "[{}]", foo)
+ let mut ostr = format!("frame type {}", self.ftype);
+ if let Some(pts) = self.ts.pts { ostr = format!("{} pts {}", ostr, pts); }
+ if let Some(dts) = self.ts.dts { ostr = format!("{} dts {}", ostr, dts); }
+ if let Some(dur) = self.ts.duration { ostr = format!("{} duration {}", ostr, dur); }
+ if self.key { ostr = format!("{} kf", ostr); }
+ write!(f, "[{}]", ostr)
}
}
media_type: StreamType,
id: u32,
num: usize,
- info: Rc<NACodecInfo>,
+ info: NACodecInfoRef,
tb_num: u32,
tb_den: u32,
}
+pub type NAStreamRef = Arc<NAStream>;
+
pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) {
if tb_num == 0 { return (tb_num, tb_den); }
if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); }
impl NAStream {
pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32) -> Self {
let (n, d) = reduce_timebase(tb_num, tb_den);
- NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info), tb_num: n, tb_den: d }
+ NAStream { media_type: mt, id, num: 0, info: info.into_ref(), tb_num: n, tb_den: d }
}
pub fn get_id(&self) -> u32 { self.id }
pub fn get_num(&self) -> usize { self.num }
pub fn set_num(&mut self, num: usize) { self.num = num; }
- pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
+ pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) }
pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) {
let (n, d) = reduce_timebase(tb_num, tb_den);
self.tb_num = n;
self.tb_den = d;
}
+ pub fn into_ref(self) -> NAStreamRef { Arc::new(self) }
}
impl fmt::Display for NAStream {
#[allow(dead_code)]
pub struct NAPacket {
- stream: Rc<NAStream>,
+ stream: NAStreamRef,
ts: NATimeInfo,
- buffer: Rc<Vec<u8>>,
+ buffer: NABufferRef<Vec<u8>>,
keyframe: bool,
// options: HashMap<String, NAValue<'a>>,
}
impl NAPacket {
- pub fn new(str: Rc<NAStream>, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
+ pub fn new(str: NAStreamRef, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
// let mut vec: Vec<u8> = Vec::new();
// vec.resize(size, 0);
- NAPacket { stream: str, ts: ts, keyframe: kf, buffer: Rc::new(vec) }
+ NAPacket { stream: str, ts, keyframe: kf, buffer: NABufferRef::new(vec) }
}
- pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() }
+ pub fn get_stream(&self) -> NAStreamRef { self.stream.clone() }
pub fn get_time_information(&self) -> NATimeInfo { self.ts }
pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
pub fn is_keyframe(&self) -> bool { self.keyframe }
- pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() }
+ pub fn get_buffer(&self) -> NABufferRef<Vec<u8>> { self.buffer.clone() }
}
impl Drop for NAPacket {
impl fmt::Display for NAPacket {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
- if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); }
- if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); }
- if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); }
- if self.keyframe { foo = format!("{} kf", foo); }
- foo = foo + "]";
- write!(f, "{}", foo)
+ let mut ostr = format!("[pkt for {} size {}", self.stream, self.buffer.len());
+ if let Some(pts) = self.ts.pts { ostr = format!("{} pts {}", ostr, pts); }
+ if let Some(dts) = self.ts.dts { ostr = format!("{} dts {}", ostr, dts); }
+ if let Some(dur) = self.ts.duration { ostr = format!("{} duration {}", ostr, dur); }
+ if self.keyframe { ostr = format!("{} kf", ostr); }
+ ostr += "]";
+ write!(f, "{}", ostr)
}
}
pub trait FrameFromPacket {
- fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame;
+ fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame;
fn fill_timestamps(&mut self, pkt: &NAPacket);
}
impl FrameFromPacket for NAFrame {
- fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame {
+ fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame {
NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf)
}
fn fill_timestamps(&mut self, pkt: &NAPacket) {