+ pub fn get_name(&self) -> &'static str { self.name }
+ pub fn is_video(&self) -> bool {
+ if let NACodecTypeInfo::Video(_) = self.properties { return true; }
+ false
+ }
+ pub fn is_audio(&self) -> bool {
+ if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
+ false
+ }
+}
+
+impl fmt::Display for NACodecInfo {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ let edata = match self.extradata.clone() {
+ None => format!("no extradata"),
+ Some(v) => format!("{} byte(s) of extradata", v.len()),
+ };
+ write!(f, "{}: {} {}", self.name, self.properties, edata)
+ }
+}
+
+pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
+ name: "none",
+ properties: NACodecTypeInfo::None,
+ extradata: None };
+
+fn alloc_video_buf(vinfo: NAVideoInfo, data: &mut Vec<u8>, offs: &mut Vec<usize>) {
+//todo use overflow detection mul
+ let width = vinfo.width as usize;
+ let height = vinfo.height as usize;
+ let fmt = &vinfo.format;
+ let mut new_size = 0;
+ for i in 0..fmt.get_num_comp() {
+ let chr = fmt.get_chromaton(i).unwrap();
+ if !vinfo.is_flipped() {
+ offs.push(new_size as usize);
+ }
+ new_size += chr.get_data_size(width, height);
+ if vinfo.is_flipped() {
+ offs.push(new_size as usize);
+ }
+ }
+ data.resize(new_size, 0);
+}
+
+fn alloc_audio_buf(ainfo: NAAudioInfo, data: &mut Vec<u8>, offs: &mut Vec<usize>) {
+//todo better alloc
+ let length = ((ainfo.sample_rate as usize) * (ainfo.format.get_bits() as usize)) >> 3;
+ let new_size: usize = length * (ainfo.channels as usize);
+ data.resize(new_size, 0);
+ for i in 0..ainfo.channels {
+ if ainfo.format.is_planar() {
+ offs.push((i as usize) * length);
+ } else {
+ offs.push(((i * ainfo.format.get_bits()) >> 3) as usize);
+ }
+ }
+}
+
+pub fn alloc_buf(info: &NACodecInfo) -> (Rc<NABuffer>, Vec<usize>) {
+ let mut data: Vec<u8> = Vec::new();
+ let mut offs: Vec<usize> = Vec::new();
+ match info.properties {
+ NACodecTypeInfo::Audio(ainfo) => alloc_audio_buf(ainfo, &mut data, &mut offs),
+ NACodecTypeInfo::Video(vinfo) => alloc_video_buf(vinfo, &mut data, &mut offs),
+ _ => (),
+ }
+ (Rc::new(NABuffer { id: 0, data: Rc::new(data) }), offs)