]> git.nihav.org Git - nihav.git/blame_incremental - nihav-ms/src/codecs/msadpcm.rs
aacsbr: fix high<->low resolution envelope mapping
[nihav.git] / nihav-ms / src / codecs / msadpcm.rs
... / ...
CommitLineData
1use nihav_core::codecs::*;
2use nihav_core::io::byteio::*;
3use std::str::FromStr;
4
5const ADAPT_TABLE: [i32; 16] = [
6 230, 230, 230, 230, 307, 409, 512, 614,
7 768, 614, 512, 409, 307, 230, 230, 230
8];
9const ADAPT_COEFFS: [[i32; 2]; 7] = [
10 [ 256, 0 ], [ 512, -256 ], [ 0, 0 ], [ 192, 64 ],
11 [ 240, 0 ], [ 460, -208 ], [ 392, -232 ]
12];
13
14#[derive(Default)]
15struct Predictor {
16 sample1: i32,
17 sample2: i32,
18 delta: i32,
19 coef1: i32,
20 coef2: i32,
21}
22
23impl Predictor {
24 fn expand_nibble(&mut self, nibble: u8) -> i16 {
25 let mul = if (nibble & 8) == 0 { i32::from(nibble) } else { i32::from(nibble) - 16 };
26 let pred = self.calc_pred() + self.delta.wrapping_mul(mul);
27 self.update(pred.max(-0x8000).min(0x7FFF));
28 self.delta = (ADAPT_TABLE[nibble as usize].wrapping_mul(self.delta) >> 8).max(16);
29 self.sample1 as i16
30 }
31 fn calc_pred(&self) -> i32 {
32 self.sample1.wrapping_mul(self.coef1).wrapping_add(self.sample2.wrapping_mul(self.coef2)) >> 8
33 }
34 fn update(&mut self, new_samp: i32) {
35 self.sample2 = self.sample1;
36 self.sample1 = new_samp;
37 }
38}
39
40#[cfg(feature="decoder_ms_adpcm")]
41struct MSADPCMDecoder {
42 ainfo: NAAudioInfo,
43 chmap: NAChannelMap,
44 adapt_coeffs: Vec<[i32; 2]>,
45 block_len: usize,
46 block_samps: usize,
47}
48
49#[cfg(feature="decoder_ms_adpcm")]
50impl MSADPCMDecoder {
51 fn new() -> Self {
52 Self {
53 ainfo: NAAudioInfo::new(0, 1, SND_S16P_FORMAT, 0),
54 chmap: NAChannelMap::new(),
55 adapt_coeffs: Vec::with_capacity(7),
56 block_len: 0,
57 block_samps: 0,
58 }
59 }
60}
61
62#[cfg(feature="decoder_ms_adpcm")]
63impl NADecoder for MSADPCMDecoder {
64 #[allow(clippy::int_plus_one)]
65 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
66 if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() {
67 self.block_len = ainfo.get_block_len();
68 let channels = ainfo.get_channels() as usize;
69 validate!(channels == 2 || channels == 1);
70 validate!(self.block_len >= 7 * channels + 1);
71 self.block_samps = (self.block_len / channels - 7) * 2 + 2;
72 self.ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), channels as u8, SND_S16P_FORMAT, self.block_samps);
73 self.chmap = NAChannelMap::from_str(if channels == 1 { "C" } else { "L,R" }).unwrap();
74 self.adapt_coeffs.clear();
75 if let Some(ref buf) = info.get_extradata() {
76 validate!(buf.len() >= 6);
77 validate!((buf.len() & 3) == 0);
78 let mut mr = MemoryReader::new_read(buf.as_slice());
79 let mut br = ByteReader::new(&mut mr);
80 let _smth = br.read_u16le()?;
81 let ncoeffs = br.read_u16le()? as usize;
82 validate!(buf.len() == ncoeffs * 4 + 4);
83
84 for _ in 0..ncoeffs {
85 let pair = [
86 i32::from(br.read_u16le()? as i16),
87 i32::from(br.read_u16le()? as i16)];
88 self.adapt_coeffs.push(pair);
89 }
90 } else {
91 self.adapt_coeffs.extend_from_slice(&ADAPT_COEFFS);
92 }
93 Ok(())
94 } else {
95 Err(DecoderError::InvalidData)
96 }
97 }
98 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
99 let info = pkt.get_stream().get_info();
100 if let NACodecTypeInfo::Audio(_) = info.get_properties() {
101 let pktbuf = pkt.get_buffer();
102 let channels = self.chmap.num_channels();
103 validate!(!pktbuf.is_empty() && (pktbuf.len() % self.block_len) == 0);
104 let nblocks = pktbuf.len() / self.block_len;
105 let nsamples = nblocks * self.block_samps;
106 let abuf = alloc_audio_buffer(self.ainfo, nsamples, self.chmap.clone())?;
107 let mut adata = abuf.get_abuf_i16().unwrap();
108 let mut off = [adata.get_offset(0), adata.get_offset(1)];
109 let dst = adata.get_data_mut().unwrap();
110
111 let mut pred = [Predictor::default(), Predictor::default()];
112
113 for blk in pktbuf.chunks(self.block_len) {
114 let mut mr = MemoryReader::new_read(blk);
115 let mut br = ByteReader::new(&mut mr);
116 for ch in 0..channels {
117 let coef_idx = br.read_byte()? as usize;
118 validate!(coef_idx < self.adapt_coeffs.len());
119 pred[ch].coef1 = self.adapt_coeffs[coef_idx][0];
120 pred[ch].coef2 = self.adapt_coeffs[coef_idx][1];
121 }
122 for ch in 0..channels {
123 pred[ch].delta = i32::from(br.read_u16le()?);
124 }
125 for ch in 0..channels {
126 let samp = br.read_u16le()? as i16;
127 pred[ch].sample2 = i32::from(samp);
128 }
129 for ch in 0..channels {
130 let samp = br.read_u16le()? as i16;
131 pred[ch].sample1 = i32::from(samp);
132 }
133 for ch in 0..channels {
134 dst[off[ch]] = pred[ch].sample2 as i16;
135 dst[off[ch] + 1] = pred[ch].sample1 as i16;
136 off[ch] += 2;
137 }
138 if channels == 1 {
139 while br.left() > 0 {
140 let idx = br.read_byte()?;
141 dst[off[0]] = pred[0].expand_nibble(idx >> 4);
142 off[0] += 1;
143 dst[off[0]] = pred[0].expand_nibble(idx & 0xF);
144 off[0] += 1;
145 }
146 } else {
147 while br.left() > 0 {
148 let idx = br.read_byte()?;
149 dst[off[0]] = pred[0].expand_nibble(idx >> 4);
150 off[0] += 1;
151 dst[off[1]] = pred[1].expand_nibble(idx & 0xF);
152 off[1] += 1;
153 }
154 }
155 }
156 let mut frm = NAFrame::new_from_pkt(pkt, info.replace_info(NACodecTypeInfo::Audio(self.ainfo)), abuf);
157 frm.set_duration(Some(nsamples as u64));
158 frm.set_keyframe(false);
159 Ok(frm.into_ref())
160 } else {
161 Err(DecoderError::InvalidData)
162 }
163 }
164 fn flush(&mut self) {
165 }
166}
167
168#[cfg(feature="decoder_ms_adpcm")]
169impl NAOptionHandler for MSADPCMDecoder {
170 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
171 fn set_options(&mut self, _options: &[NAOption]) { }
172 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
173}
174
175#[cfg(feature="decoder_ms_adpcm")]
176pub fn get_decoder() -> Box<dyn NADecoder + Send> {
177 Box::new(MSADPCMDecoder::new())
178}
179
180#[derive(Default)]
181#[cfg(feature="encoder_ms_adpcm")]
182struct MSADPCMEncoder {
183 stream: Option<NAStreamRef>,
184 samples: Vec<i16>,
185 block_len: usize,
186 channels: usize,
187 flush: bool,
188 srate: u32,
189}
190
191#[cfg(feature="encoder_ms_adpcm")]
192const DEFAULT_BLOCK_LEN: usize = 256;
193
194#[cfg(feature="encoder_ms_adpcm")]
195impl MSADPCMEncoder {
196 fn new() -> Self { Self::default() }
197 fn encode_packet(&mut self) -> EncoderResult<NAPacket> {
198 if self.samples.is_empty() {
199 return Err(EncoderError::TryAgain);
200 }
201 let len = (self.samples.len() / self.channels).min(self.block_len);
202 if len < self.block_len && !self.flush {
203 return Err(EncoderError::TryAgain);
204 }
205 if len < 2 {
206 self.flush = false;
207 return Err(EncoderError::TryAgain);
208 }
209
210 let mut dbuf = vec![0u8; Self::calc_block_size(len, self.channels)];
211 let mut mw = MemoryWriter::new_write(dbuf.as_mut_slice());
212 let mut bw = ByteWriter::new(&mut mw);
213
214 let mut best_idx = [0usize; 2];
215 for ch in 0..self.channels {
216 let mut best_dist = std::i64::MAX;
217 for i in 0..ADAPT_COEFFS.len() {
218 let dist = self.calc_dist(ch, i, len);
219 if dist < best_dist {
220 best_dist = dist;
221 best_idx[ch] = i;
222 }
223 }
224 bw.write_byte(best_idx[ch] as u8)?;
225 }
226 let mut dec = [Predictor::default(), Predictor::default()];
227 for ch in 0..self.channels {
228 dec[ch].sample1 = i32::from(self.samples[ch + self.channels]);
229 dec[ch].sample2 = i32::from(self.samples[ch]);
230 dec[ch].coef1 = ADAPT_COEFFS[best_idx[ch]][0];
231 dec[ch].coef2 = ADAPT_COEFFS[best_idx[ch]][1];
232 if len > 2 {
233 let pred = dec[ch].calc_pred();
234 dec[ch].delta = ((i32::from(self.samples[ch + self.channels * 2]) - pred).abs() / 4).max(16);
235 } else {
236 dec[ch].delta = 16;
237 }
238 }
239 for ch in 0..self.channels {
240 bw.write_u16le(dec[ch].delta as u16)?;
241 }
242 for ch in 0..self.channels {
243 bw.write_u16le(dec[ch].sample1 as u16)?;
244 }
245 for ch in 0..self.channels {
246 bw.write_u16le(dec[ch].sample2 as u16)?;
247 }
248 if self.channels == 1 {
249 for samps in self.samples.chunks(2).skip(1).take(len/2 - 1) {
250 let diff = i32::from(samps[0]) - dec[0].calc_pred();
251 let nib0 = Self::calculate_mul(dec[0].delta, diff);
252 dec[0].expand_nibble(nib0);
253 let diff = i32::from(samps[1]) - dec[0].calc_pred();
254 let nib1 = Self::calculate_mul(dec[0].delta, diff);
255 dec[0].expand_nibble(nib1);
256 bw.write_byte(nib0 * 16 + nib1)?;
257 }
258 } else {
259 for samps in self.samples.chunks(2).skip(2).take(len - 2) {
260 let diff = i32::from(samps[0]) - dec[0].calc_pred();
261 let nib0 = Self::calculate_mul(dec[0].delta, diff);
262 dec[0].expand_nibble(nib0);
263 let diff = i32::from(samps[1]) - dec[1].calc_pred();
264 let nib1 = Self::calculate_mul(dec[1].delta, diff);
265 dec[1].expand_nibble(nib1);
266 bw.write_byte(nib0 * 16 + nib1)?;
267 }
268 }
269 self.samples.drain(..len * self.channels);
270 drop(bw);
271 let ts = NATimeInfo::new(None, None, Some(1), 1, self.srate);
272 Ok(NAPacket::new(self.stream.clone().unwrap(), ts, true, dbuf))
273 }
274 fn calc_dist(&self, ch: usize, idx: usize, len: usize) -> i64 {
275 let mut dist = 0;
276 let mut dec = Predictor {
277 sample2: i32::from(self.samples[ch]),
278 sample1: i32::from(self.samples[ch + self.channels]),
279 coef1: ADAPT_COEFFS[idx][0],
280 coef2: ADAPT_COEFFS[idx][1],
281 delta: 16,
282 };
283 if self.channels == 1 {
284 for samp in self.samples.iter().skip(2).take(len - 2) {
285 let pred = dec.calc_pred();
286 dec.update(pred);
287 let diff = i64::from(*samp) - i64::from(pred);
288 dist += diff * diff;
289 }
290 } else {
291 for samp in self.samples.chunks(2).skip(2).take(len - 2) {
292 let pred = dec.calc_pred();
293 dec.update(pred);
294 let diff = i64::from(samp[ch]) - i64::from(pred);
295 dist += diff * diff;
296 }
297 }
298 dist
299 }
300 fn calculate_mul(delta: i32, diff: i32) -> u8 {
301 ((diff / delta).max(-8).min(7) & 0xF) as u8
302 }
303 fn calc_block_size(nsamps: usize, channels: usize) -> usize {
304 (nsamps - 2) * channels / 2 + 7 * channels
305 }
306}
307
308#[cfg(feature="encoder_ms_adpcm")]
309impl NAEncoder for MSADPCMEncoder {
310 fn negotiate_format(&self, encinfo: &EncodeParameters) -> EncoderResult<EncodeParameters> {
311 match encinfo.format {
312 NACodecTypeInfo::None => {
313 Ok(EncodeParameters {
314 format: NACodecTypeInfo::Audio(NAAudioInfo::new(0, 1, SND_S16_FORMAT, DEFAULT_BLOCK_LEN)),
315 ..Default::default() })
316 },
317 NACodecTypeInfo::Video(_) => Err(EncoderError::FormatError),
318 NACodecTypeInfo::Audio(ainfo) => {
319 let mut outinfo = ainfo;
320 outinfo.channels = outinfo.channels.min(2);
321 if outinfo.format != SND_S16P_FORMAT && outinfo.format != SND_S16_FORMAT {
322 outinfo.format = SND_S16_FORMAT;
323 }
324 if outinfo.block_len == 0 {
325 outinfo.block_len = DEFAULT_BLOCK_LEN;
326 }
327 if outinfo.block_len < 2 {
328 outinfo.block_len = 2;
329 }
330 if (outinfo.channels == 1) && ((outinfo.block_len & 1) == 1) {
331 outinfo.block_len += 1;
332 }
333 let mut ofmt = *encinfo;
334 ofmt.format = NACodecTypeInfo::Audio(outinfo);
335 Ok(ofmt)
336 }
337 }
338 }
339 fn init(&mut self, stream_id: u32, encinfo: EncodeParameters) -> EncoderResult<NAStreamRef> {
340 match encinfo.format {
341 NACodecTypeInfo::None => Err(EncoderError::FormatError),
342 NACodecTypeInfo::Video(_) => Err(EncoderError::FormatError),
343 NACodecTypeInfo::Audio(ainfo) => {
344 if ainfo.format != SND_S16P_FORMAT && ainfo.format != SND_S16_FORMAT {
345 return Err(EncoderError::FormatError);
346 }
347 if ainfo.channels != 1 && ainfo.channels != 2 {
348 return Err(EncoderError::FormatError);
349 }
350 if ainfo.block_len < 2 || ((ainfo.block_len * (ainfo.channels as usize)) & 1) != 0 {
351 return Err(EncoderError::FormatError);
352 }
353 self.channels = ainfo.channels as usize;
354 self.block_len = ainfo.block_len;
355
356 let soniton = NASoniton::new(4, 0);
357 let out_ainfo = NAAudioInfo::new(ainfo.sample_rate, ainfo.channels, soniton, Self::calc_block_size(self.block_len, self.channels));
358 let info = NACodecInfo::new("ms-adpcm", NACodecTypeInfo::Audio(out_ainfo), None);
359 let mut stream = NAStream::new(StreamType::Audio, stream_id, info, self.block_len as u32, ainfo.sample_rate, 0);
360 stream.set_num(stream_id as usize);
361 let stream = stream.into_ref();
362
363 self.stream = Some(stream.clone());
364 self.samples = Vec::with_capacity(self.block_len * self.channels);
365 self.srate = ainfo.sample_rate;
366 self.flush = false;
367
368 Ok(stream)
369 },
370 }
371 }
372 fn encode(&mut self, frm: &NAFrame) -> EncoderResult<()> {
373 let buf = frm.get_buffer();
374 if let Some(ref abuf) = buf.get_abuf_i16() {
375 let src = abuf.get_data();
376 let len = abuf.get_length();
377 let ch = abuf.get_chmap().num_channels();
378 if abuf.get_step() > 1 || ch == 1 {
379 self.samples.extend(src.iter().take(len * ch));
380 } else {
381 let (src0, src1) = src.split_at(abuf.get_stride());
382 self.samples.reserve(len * 2);
383 for (s0, s1) in src0.iter().take(len).zip(src1.iter()) {
384 self.samples.push(*s0);
385 self.samples.push(*s1);
386 }
387 }
388 Ok(())
389 } else {
390 Err(EncoderError::InvalidParameters)
391 }
392 }
393 fn get_packet(&mut self) -> EncoderResult<Option<NAPacket>> {
394 if let Ok(pkt) = self.encode_packet() {
395 Ok(Some(pkt))
396 } else {
397 Ok(None)
398 }
399 }
400 fn flush(&mut self) -> EncoderResult<()> {
401 self.flush = true;
402 Ok(())
403 }
404}
405
406#[cfg(feature="encoder_ms_adpcm")]
407impl NAOptionHandler for MSADPCMEncoder {
408 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
409 fn set_options(&mut self, _options: &[NAOption]) { }
410 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
411}
412
413#[cfg(feature="encoder_ms_adpcm")]
414pub fn get_encoder() -> Box<dyn NAEncoder + Send> {
415 Box::new(MSADPCMEncoder::new())
416}
417
418#[cfg(test)]
419mod test {
420 use nihav_core::codecs::*;
421 use nihav_core::demuxers::*;
422 use nihav_core::muxers::*;
423 use nihav_codec_support::test::dec_video::*;
424 use nihav_codec_support::test::enc_video::*;
425 use crate::*;
426 use nihav_commonfmt::*;
427 #[cfg(feature="decoder_ms_adpcm")]
428 #[test]
429 fn test_ms_adpcm_decoder() {
430 let mut dmx_reg = RegisteredDemuxers::new();
431 generic_register_all_demuxers(&mut dmx_reg);
432 let mut dec_reg = RegisteredDecoders::new();
433 ms_register_all_decoders(&mut dec_reg);
434
435 test_decoding("avi", "ms-adpcm", "assets/MS/dance.avi", None, &dmx_reg, &dec_reg,
436 ExpectedTestResult::MD5([0xf5e3fc84, 0xbcabc11c, 0x33c6874e, 0xe05ecd14]));
437 }
438 #[cfg(feature="encoder_ms_adpcm")]
439 #[test]
440 fn test_ms_adpcm_encoder() {
441 let mut dmx_reg = RegisteredDemuxers::new();
442 generic_register_all_demuxers(&mut dmx_reg);
443 let mut dec_reg = RegisteredDecoders::new();
444 generic_register_all_decoders(&mut dec_reg);
445 ms_register_all_decoders(&mut dec_reg);
446 let mut mux_reg = RegisteredMuxers::new();
447 generic_register_all_muxers(&mut mux_reg);
448 let mut enc_reg = RegisteredEncoders::new();
449 ms_register_all_encoders(&mut enc_reg);
450
451 // sample: https://samples.mplayerhq.hu/V-codecs/RT21/320x240/laser05.avi
452 let dec_config = DecoderTestParams {
453 demuxer: "avi",
454 in_name: "assets/Indeo/laser05.avi",
455 stream_type: StreamType::Audio,
456 limit: None,
457 dmx_reg, dec_reg,
458 };
459 let enc_config = EncoderTestParams {
460 muxer: "wav",
461 enc_name: "ms-adpcm",
462 out_name: "msadpcm.wav",
463 mux_reg, enc_reg,
464 };
465 let dst_ainfo = NAAudioInfo {
466 sample_rate: 0,
467 channels: 0,
468 format: SND_S16_FORMAT,
469 block_len: 128,
470 };
471 let enc_params = EncodeParameters {
472 format: NACodecTypeInfo::Audio(dst_ainfo),
473 quality: 0,
474 bitrate: 0,
475 tb_num: 0,
476 tb_den: 0,
477 flags: 0,
478 };
479 test_encoding_md5(&dec_config, &enc_config, enc_params, &[],
480 &[0x82259f45, 0xba7b984a, 0xc03c94e5, 0x00b4312b]);
481 }
482}