]> git.nihav.org Git - nihav.git/blame - src/codecs/pcm.rs
h263: make is_gob a part of init
[nihav.git] / src / codecs / pcm.rs
CommitLineData
3234da61
KS
1use formats::*;
2use super::*;
3
4struct PCMDecoder { chmap: NAChannelMap }
5
6impl PCMDecoder {
7 fn new() -> Self {
8 PCMDecoder { chmap: NAChannelMap::new() }
9 }
10}
11
12const CHMAP_MONO: [NAChannelType; 1] = [NAChannelType::C];
13const CHMAP_STEREO: [NAChannelType; 2] = [NAChannelType::L, NAChannelType::R];
14
15fn get_default_chmap(nch: u8) -> NAChannelMap {
16 let mut chmap = NAChannelMap::new();
17 match nch {
18 1 => chmap.add_channels(&CHMAP_MONO),
19 2 => chmap.add_channels(&CHMAP_STEREO),
20 _ => (),
21 }
22 chmap
23}
24
12ccce74 25fn get_duration(ainfo: &NAAudioInfo, duration: Option<u64>, data_size: usize) -> u64 {
3234da61 26 if duration == None {
12ccce74
KS
27 let size_bits = (data_size as u64) * 8;
28 let blk_size = (ainfo.get_channels() as u64) * (ainfo.get_format().get_bits() as u64);
3234da61
KS
29 size_bits / blk_size
30 } else {
12ccce74 31 duration.unwrap() as u64
3234da61
KS
32 }
33}
34
35impl NADecoder for PCMDecoder {
36 fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()> {
37 if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() {
3234da61
KS
38 self.chmap = get_default_chmap(ainfo.get_channels());
39 if self.chmap.num_channels() == 0 { return Err(DecoderError::InvalidData); }
40 Ok(())
41 } else {
42 Err(DecoderError::InvalidData)
43 }
44 }
45 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
46 let info = pkt.get_stream().get_info();
47 if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() {
48 let duration = get_duration(&ainfo, pkt.get_duration(), pkt.get_buffer().len());
3234da61
KS
49 let pktbuf = pkt.get_buffer();
50 let mut buf: Vec<u8> = Vec::with_capacity(pktbuf.len());
51 buf.clone_from(&pktbuf);
52 let abuf = NAAudioBuffer::new_from_buf(ainfo, Rc::new(RefCell::new(buf)), self.chmap.clone());
53 let mut frm = NAFrame::new_from_pkt(pkt, info, NABufferType::AudioPacked(abuf));
12ccce74 54 frm.set_duration(Some(duration));
3234da61
KS
55 frm.set_keyframe(true);
56 Ok(Rc::new(RefCell::new(frm)))
57 } else {
58 Err(DecoderError::InvalidData)
59 }
60 }
61}
62
63pub fn get_decoder() -> Box<NADecoder> {
64 Box::new(PCMDecoder::new())
65}