1 use nihav_core::codecs::*;
2 use nihav_core::io::byteio::*;
3 use nihav_codec_support::codecs::imaadpcm::*;
6 struct DuckADPCMDecoder {
10 ch_state: [IMAState; 2],
14 impl DuckADPCMDecoder {
15 fn new(is_dk3: bool) -> Self {
17 ainfo: NAAudioInfo::new(0, 1, SND_S16P_FORMAT, 0),
18 chmap: NAChannelMap::new(),
20 ch_state: [IMAState::new(), IMAState::new()],
26 impl NADecoder for DuckADPCMDecoder {
27 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
28 if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() {
29 validate!(ainfo.get_block_len() > 16);
30 self.block_len = ainfo.get_block_len();
31 let channels = ainfo.get_channels();
32 validate!(channels == 2 || (!self.is_dk3 && channels == 1));
33 let len = if self.is_dk3 {
34 ((self.block_len - 16) * 2 / 3) * 2
36 (self.block_len - 4 * (channels as usize)) * 2 / (channels as usize)
38 self.ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), channels, SND_S16P_FORMAT, len);
39 self.chmap = NAChannelMap::from_str(if channels == 1 { "C" } else { "L,R" }).unwrap();
42 Err(DecoderError::InvalidData)
45 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
46 let info = pkt.get_stream().get_info();
47 if let NACodecTypeInfo::Audio(_) = info.get_properties() {
48 let pktbuf = pkt.get_buffer();
49 validate!(pktbuf.len() > (if self.is_dk3 { 16 } else { 4 * self.chmap.num_channels() }));
50 let nblocks = pktbuf.len() / self.block_len;
51 let out_block_len = self.ainfo.get_block_len();
52 let duration = out_block_len * nblocks;
53 let abuf = alloc_audio_buffer(self.ainfo, duration, self.chmap.clone())?;
54 let mut adata = abuf.get_abuf_i16().unwrap();
55 let mut off0 = adata.get_offset(0);
56 let mut off1 = adata.get_offset(1);
57 let dst = adata.get_data_mut().unwrap();
59 for blk in pktbuf.chunks_exact(self.block_len) {
60 let mut mr = MemoryReader::new_read(blk);
61 let mut br = ByteReader::new(&mut mr);
63 let _typeid = br.read_byte()?;
64 let _version = br.read_byte()?;
65 let _srate = br.read_u32le()?;
66 let samples = br.read_u32le()? as usize;
67 let sumpred = br.read_u16le()? as i16;
68 let diffpred = br.read_u16le()? as i16;
69 let sumstep = br.read_byte()?;
70 let diffstep = br.read_byte()?;
71 validate!(sumstep <= IMA_MAX_STEP && diffstep <= IMA_MAX_STEP);
72 validate!(samples <= out_block_len);
73 self.ch_state[0].reset(sumpred, sumstep);
74 self.ch_state[1].reset(diffpred, diffstep);
76 let mut diff_val: i32 = i32::from(diffpred);
77 for x in (0..out_block_len).step_by(2) {
82 let b0 = br.read_byte()?;
83 let b1 = br.read_byte()?;
89 let b0 = br.read_byte()?;
94 let sum0 = i32::from(self.ch_state[0].expand_sample(nib0));
95 let diff = i32::from(self.ch_state[1].expand_sample(nib1));
96 let sum1 = i32::from(self.ch_state[0].expand_sample(nib2));
97 diff_val = (diff_val + diff) >> 1;
98 dst[off0 + x + 0] = (sum0 + diff_val) as i16;
99 dst[off1 + x + 0] = (sum0 - diff_val) as i16;
100 diff_val = (diff_val + diff) >> 1;
101 dst[off0 + x + 1] = (sum1 + diff_val) as i16;
102 dst[off1 + x + 1] = (sum1 - diff_val) as i16;
106 let nchannels = self.chmap.num_channels();
107 for ch in 0..nchannels {
108 let pred = br.read_u16le()? as i16;
109 let step = br.read_byte()?;
111 validate!(step <= IMA_MAX_STEP);
112 self.ch_state[ch].reset(pred, step);
115 for x in 0..out_block_len {
116 let b = br.read_byte()?;
117 dst[off0 + x] = self.ch_state[0].expand_sample(b >> 4);
118 dst[off1 + x] = self.ch_state[1].expand_sample(b & 0xF);
121 for x in (0..out_block_len).step_by(2) {
122 let b = br.read_byte()?;
123 dst[off0 + x + 0] = self.ch_state[0].expand_sample(b >> 4);
124 dst[off0 + x + 1] = self.ch_state[0].expand_sample(b & 0xF);
128 off0 += out_block_len;
129 off1 += out_block_len;
131 let mut frm = NAFrame::new_from_pkt(pkt, info, abuf);
132 frm.set_duration(Some(duration as u64));
133 frm.set_keyframe(false);
136 Err(DecoderError::InvalidData)
139 fn flush(&mut self) {
143 impl NAOptionHandler for DuckADPCMDecoder {
144 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
145 fn set_options(&mut self, _options: &[NAOption]) { }
146 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
149 pub fn get_decoder_dk3() -> Box<dyn NADecoder + Send> {
150 Box::new(DuckADPCMDecoder::new(true))
153 pub fn get_decoder_dk4() -> Box<dyn NADecoder + Send> {
154 Box::new(DuckADPCMDecoder::new(false))
159 use nihav_core::codecs::RegisteredDecoders;
160 use nihav_core::demuxers::RegisteredDemuxers;
161 use nihav_codec_support::test::dec_video::*;
162 use crate::duck_register_all_decoders;
163 use nihav_commonfmt::generic_register_all_demuxers;
166 let mut dmx_reg = RegisteredDemuxers::new();
167 generic_register_all_demuxers(&mut dmx_reg);
168 let mut dec_reg = RegisteredDecoders::new();
169 duck_register_all_decoders(&mut dec_reg);
171 //let file = "assets/Duck/AVI-DUCK-dk3.duk";
172 //test_decode_audio("avi", file, Some(100), None/*Some("dk3")*/, &dmx_reg, &dec_reg);
173 test_decoding("avi", "adpcm-dk3", "assets/Duck/AVI-DUCK-dk3.duk", None, &dmx_reg, &dec_reg,
174 ExpectedTestResult::MD5([0xa48fae0a, 0xa536b27f, 0x169ecc19, 0x8436fade]));
178 let mut dmx_reg = RegisteredDemuxers::new();
179 generic_register_all_demuxers(&mut dmx_reg);
180 let mut dec_reg = RegisteredDecoders::new();
181 duck_register_all_decoders(&mut dec_reg);
183 // let file = "assets/Duck/virtuafighter2-opening1.avi";
184 // test_decode_audio("avi", file, Some(100), None/*Some("dk4")*/, &dmx_reg, &dec_reg);
185 test_decoding("avi", "adpcm-dk4", "assets/Duck/virtuafighter2-opening1.avi", None, &dmx_reg, &dec_reg,
186 ExpectedTestResult::MD5([0x04e40d15, 0xf65b3427, 0x1dd5181f, 0xf321b56f]));