]>
Commit | Line | Data |
---|---|---|
4c1582cf KS |
1 | use nihav_core::codecs::*; |
2 | use nihav_core::io::byteio::*; | |
3 | use nihav_codec_support::codecs::imaadpcm::*; | |
4 | use std::str::FromStr; | |
5 | ||
6 | const PACKET_LEN: usize = 34; | |
7 | const PACKET_SAMPLES: usize = 64; | |
8 | ||
9 | struct IMAADPCMDecoder { | |
10 | ainfo: NAAudioInfo, | |
11 | chmap: NAChannelMap, | |
12 | ch_state: [IMAState; 2], | |
13 | } | |
14 | ||
15 | impl IMAADPCMDecoder { | |
16 | fn new() -> Self { | |
17 | Self { | |
18 | ainfo: NAAudioInfo::new(0, 1, SND_S16P_FORMAT, PACKET_SAMPLES), | |
19 | chmap: NAChannelMap::new(), | |
20 | ch_state: [IMAState::new(), IMAState::new()], | |
21 | } | |
22 | } | |
23 | } | |
24 | ||
25 | impl NADecoder for IMAADPCMDecoder { | |
26 | fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { | |
27 | if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() { | |
28 | let channels = ainfo.get_channels() as usize; | |
29 | validate!(channels == 2 || channels == 1); | |
30 | self.ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), channels as u8, SND_S16P_FORMAT, PACKET_SAMPLES); | |
31 | self.chmap = NAChannelMap::from_str(if channels == 1 { "C" } else { "L,R" }).unwrap(); | |
32 | Ok(()) | |
33 | } else { | |
34 | Err(DecoderError::InvalidData) | |
35 | } | |
36 | } | |
37 | fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> { | |
38 | let info = pkt.get_stream().get_info(); | |
39 | if let NACodecTypeInfo::Audio(_) = info.get_properties() { | |
40 | let pktbuf = pkt.get_buffer(); | |
41 | let channels = self.chmap.num_channels(); | |
42 | validate!(pktbuf.len() % (PACKET_LEN * channels) == 0); | |
43 | let nblocks = pktbuf.len() / channels / PACKET_LEN; | |
44 | let nsamples = nblocks * PACKET_SAMPLES; | |
45 | let abuf = alloc_audio_buffer(self.ainfo, nsamples, self.chmap.clone())?; | |
46 | let mut adata = abuf.get_abuf_i16().unwrap(); | |
47 | let mut off = [adata.get_offset(0), adata.get_offset(1)]; | |
48 | let dst = adata.get_data_mut().unwrap(); | |
49 | ||
50 | for data in pktbuf.chunks(PACKET_LEN * channels) { | |
51 | for ch in 0..channels { | |
52 | let mut mr = MemoryReader::new_read(&data[PACKET_LEN * ch..][..PACKET_LEN]); | |
53 | let mut br = ByteReader::new(&mut mr); | |
54 | ||
55 | let init = br.read_u16be()?; | |
56 | let pred = (init as i16) & !0x7F; | |
57 | let step = (init & 0x7F) as u8; | |
58 | validate!(step <= IMA_MAX_STEP); | |
59 | self.ch_state[ch].reset(pred, step); | |
60 | ||
61 | for i in (0..PACKET_SAMPLES).step_by(2) { | |
62 | let byte = br.read_byte()?; | |
63 | dst[off[ch] + i] = self.ch_state[ch].expand_sample(byte & 0xF); | |
64 | dst[off[ch] + i + 1] = self.ch_state[ch].expand_sample(byte >> 4); | |
65 | } | |
66 | off[ch] += PACKET_SAMPLES; | |
67 | } | |
68 | } | |
69 | ||
70 | let mut frm = NAFrame::new_from_pkt(pkt, info.replace_info(NACodecTypeInfo::Audio(self.ainfo)), abuf); | |
71 | frm.set_duration(Some(nsamples as u64)); | |
72 | frm.set_keyframe(false); | |
73 | Ok(frm.into_ref()) | |
74 | } else { | |
75 | Err(DecoderError::InvalidData) | |
76 | } | |
77 | } | |
78 | fn flush(&mut self) { | |
79 | } | |
80 | } | |
81 | ||
82 | impl NAOptionHandler for IMAADPCMDecoder { | |
83 | fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } | |
84 | fn set_options(&mut self, _options: &[NAOption]) { } | |
85 | fn query_option_value(&self, _name: &str) -> Option<NAValue> { None } | |
86 | } | |
87 | ||
88 | pub fn get_decoder() -> Box<dyn NADecoder + Send> { | |
89 | Box::new(IMAADPCMDecoder::new()) | |
90 | } | |
91 | ||
92 | #[cfg(test)] | |
93 | mod test { | |
94 | use nihav_core::codecs::RegisteredDecoders; | |
95 | use nihav_core::demuxers::RegisteredDemuxers; | |
96 | use nihav_codec_support::test::dec_video::*; | |
78fb6560 | 97 | use crate::qt_register_all_decoders; |
4c1582cf KS |
98 | use nihav_commonfmt::generic_register_all_demuxers; |
99 | #[test] | |
100 | fn test_ima_adpcm_qt() { | |
101 | let mut dmx_reg = RegisteredDemuxers::new(); | |
102 | generic_register_all_demuxers(&mut dmx_reg); | |
103 | let mut dec_reg = RegisteredDecoders::new(); | |
78fb6560 | 104 | qt_register_all_decoders(&mut dec_reg); |
4c1582cf KS |
105 | |
106 | test_decoding("mov", "ima-adpcm-qt", "assets/QT/shuffle-ima41.mov", None, &dmx_reg, &dec_reg, | |
107 | ExpectedTestResult::MD5([0xba2ad472, 0xd6aee026, 0xb915dd7d, 0xac51314c])); | |
108 | } | |
109 | } |