1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
use nihav_core::codecs::*;
use nihav_core::io::byteio::*;
use std::str::FromStr;
const IMA_MAX_STEP: u8 = 88;
struct IMAState {
predictor: i32,
step: usize,
}
impl IMAState {
fn new() -> Self {
Self {
predictor: 0,
step: 0,
}
}
fn reset(&mut self, predictor: i16, step: u8) {
self.predictor = i32::from(predictor);
self.step = step.min(IMA_MAX_STEP) as usize;
}
fn expand_sample(&mut self, nibble: u8) -> i16 {
let istep = (self.step as isize) + (IMA_STEPS[nibble as usize] as isize);
let sign = (nibble & 8) != 0;
let diff = (i32::from(2 * (nibble & 7) + 1) * IMA_STEP_TABLE[self.step]) >> 3;
let sample = if !sign { self.predictor + diff } else { self.predictor - diff };
self.predictor = sample.max(i32::from(std::i16::MIN)).min(i32::from(std::i16::MAX));
self.step = istep.max(0).min(IMA_MAX_STEP as isize) as usize;
self.predictor as i16
}
}
struct DuckADPCMDecoder {
ainfo: NAAudioInfo,
chmap: NAChannelMap,
is_dk3: bool,
ch_state: [IMAState; 2],
block_len: usize,
}
impl DuckADPCMDecoder {
fn new(is_dk3: bool) -> Self {
Self {
ainfo: NAAudioInfo::new(0, 1, SND_S16P_FORMAT, 0),
chmap: NAChannelMap::new(),
is_dk3,
ch_state: [IMAState::new(), IMAState::new()],
block_len: 0,
}
}
}
impl NADecoder for DuckADPCMDecoder {
fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() {
validate!(ainfo.get_block_len() > 16);
self.block_len = ainfo.get_block_len();
let channels = ainfo.get_channels();
validate!(channels == 2 || (!self.is_dk3 && channels == 1));
let len = if self.is_dk3 {
((self.block_len - 16) * 2 / 3) * 2
} else {
(self.block_len - 4 * (channels as usize)) * 2 / (channels as usize)
};
self.ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), channels, SND_S16P_FORMAT, len);
self.chmap = NAChannelMap::from_str(if channels == 1 { "C" } else { "L,R" }).unwrap();
Ok(())
} else {
Err(DecoderError::InvalidData)
}
}
fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
let info = pkt.get_stream().get_info();
if let NACodecTypeInfo::Audio(_) = info.get_properties() {
let pktbuf = pkt.get_buffer();
validate!(pktbuf.len() > (if self.is_dk3 { 16 } else { 4 * self.chmap.num_channels() }));
let nblocks = pktbuf.len() / self.block_len;
let out_block_len = self.ainfo.get_block_len();
let duration = out_block_len * nblocks;
let abuf = alloc_audio_buffer(self.ainfo, duration, self.chmap.clone())?;
let mut adata = abuf.get_abuf_i16().unwrap();
let mut off0 = adata.get_offset(0);
let mut off1 = adata.get_offset(1);
let dst = adata.get_data_mut().unwrap();
for blk in pktbuf.chunks_exact(self.block_len) {
let mut mr = MemoryReader::new_read(blk);
let mut br = ByteReader::new(&mut mr);
if self.is_dk3 {
let _typeid = br.read_byte()?;
let _version = br.read_byte()?;
let _srate = br.read_u32le()?;
let samples = br.read_u32le()? as usize;
let sumpred = br.read_u16le()? as i16;
let diffpred = br.read_u16le()? as i16;
let sumstep = br.read_byte()?;
let diffstep = br.read_byte()?;
validate!(sumstep <= IMA_MAX_STEP && diffstep <= IMA_MAX_STEP);
validate!(samples <= out_block_len);
self.ch_state[0].reset(sumpred, sumstep);
self.ch_state[1].reset(diffpred, diffstep);
let mut last_nib = 0;
let mut diff_val: i32 = i32::from(diffpred);
for x in (0..out_block_len).step_by(2) {
let nib0;
let nib1;
let nib2;
if (x & 2) == 0 {
let b0 = br.read_byte()?;
let b1 = br.read_byte()?;
nib0 = b0 & 0xF;
nib1 = b0 >> 4;
nib2 = b1 & 0xF;
last_nib = b1 >> 4;
} else {
let b0 = br.read_byte()?;
nib0 = last_nib;
nib1 = b0 & 0xF;
nib2 = b0 >> 4;
}
let sum0 = i32::from(self.ch_state[0].expand_sample(nib0));
let diff = i32::from(self.ch_state[1].expand_sample(nib1));
let sum1 = i32::from(self.ch_state[0].expand_sample(nib2));
diff_val = (diff_val + diff) >> 1;
dst[off0 + x + 0] = (sum0 + diff_val) as i16;
dst[off1 + x + 0] = (sum0 - diff_val) as i16;
diff_val = (diff_val + diff) >> 1;
dst[off0 + x + 1] = (sum1 + diff_val) as i16;
dst[off1 + x + 1] = (sum1 - diff_val) as i16;
diff_val = diff;
}
} else {
let nchannels = self.chmap.num_channels();
for ch in 0..nchannels {
let pred = br.read_u16le()? as i16;
let step = br.read_byte()?;
br.read_skip(1)?;
validate!(step <= IMA_MAX_STEP);
self.ch_state[ch].reset(pred, step);
}
if nchannels == 2 {
for x in 0..out_block_len {
let b = br.read_byte()?;
dst[off0 + x] = self.ch_state[0].expand_sample(b >> 4);
dst[off1 + x] = self.ch_state[1].expand_sample(b & 0xF);
}
} else {
for x in (0..out_block_len).step_by(2) {
let b = br.read_byte()?;
dst[off0 + x + 0] = self.ch_state[0].expand_sample(b >> 4);
dst[off0 + x + 1] = self.ch_state[0].expand_sample(b & 0xF);
}
}
}
off0 += out_block_len;
off1 += out_block_len;
}
let mut frm = NAFrame::new_from_pkt(pkt, info, abuf);
frm.set_duration(Some(duration as u64));
frm.set_keyframe(false);
Ok(frm.into_ref())
} else {
Err(DecoderError::InvalidData)
}
}
fn flush(&mut self) {
}
}
pub fn get_decoder_dk3() -> Box<dyn NADecoder + Send> {
Box::new(DuckADPCMDecoder::new(true))
}
pub fn get_decoder_dk4() -> Box<dyn NADecoder + Send> {
Box::new(DuckADPCMDecoder::new(false))
}
#[cfg(test)]
mod test {
use nihav_core::codecs::RegisteredDecoders;
use nihav_core::demuxers::RegisteredDemuxers;
use nihav_codec_support::test::dec_video::*;
use crate::duck_register_all_codecs;
use nihav_commonfmt::generic_register_all_demuxers;
#[test]
fn test_dk3() {
let mut dmx_reg = RegisteredDemuxers::new();
generic_register_all_demuxers(&mut dmx_reg);
let mut dec_reg = RegisteredDecoders::new();
duck_register_all_codecs(&mut dec_reg);
//let file = "assets/Duck/AVI-DUCK-dk3.duk";
//test_decode_audio("avi", file, Some(100), None/*Some("dk3")*/, &dmx_reg, &dec_reg);
test_decoding("avi", "adpcm-dk3", "assets/Duck/AVI-DUCK-dk3.duk", None, &dmx_reg, &dec_reg,
ExpectedTestResult::MD5([0xa48fae0a, 0xa536b27f, 0x169ecc19, 0x8436fade]));
}
#[test]
fn test_dk4() {
let mut dmx_reg = RegisteredDemuxers::new();
generic_register_all_demuxers(&mut dmx_reg);
let mut dec_reg = RegisteredDecoders::new();
duck_register_all_codecs(&mut dec_reg);
// let file = "assets/Duck/virtuafighter2-opening1.avi";
// test_decode_audio("avi", file, Some(100), None/*Some("dk4")*/, &dmx_reg, &dec_reg);
test_decoding("avi", "adpcm-dk4", "assets/Duck/virtuafighter2-opening1.avi", None, &dmx_reg, &dec_reg,
ExpectedTestResult::MD5([0x04e40d15, 0xf65b3427, 0x1dd5181f, 0xf321b56f]));
}
}
const IMA_STEPS: [i8; 16] = [
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8
];
const IMA_STEP_TABLE: [i32; 89] = [
7, 8, 9, 10, 11, 12, 13, 14,
16, 17, 19, 21, 23, 25, 28, 31,
34, 37, 41, 45, 50, 55, 60, 66,
73, 80, 88, 97, 107, 118, 130, 143,
157, 173, 190, 209, 230, 253, 279, 307,
337, 371, 408, 449, 494, 544, 598, 658,
724, 796, 876, 963, 1060, 1166, 1282, 1411,
1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
];
|