1 use nihav_core::codecs::*;
2 use nihav_core::io::byteio::*;
3 use nihav_core::io::bitreader::*;
4 use nihav_core::io::intcode::*;
18 fn reset(&mut self, bpp: u8) {
19 const SHIFTS: [u8; 3] = [10, 9, 10];
20 self.shift = SHIFTS[(bpp - 1) as usize];
21 self.round = (1 << self.shift) >> 1;
28 #[allow(clippy::comparison_chain)]
29 fn hybrid_filt(&mut self, delta: i32) -> i32 {
31 for (qm, dx) in self.qm.iter_mut().zip(self.dx.iter()) {
34 } else if self.error > 0 {
35 for (qm, dx) in self.qm.iter_mut().zip(self.dx.iter()) {
40 let mut sum = self.round;
41 for (dl, qm) in self.dl.iter().zip(self.qm.iter()) {
42 sum = sum.wrapping_add(*dl * *qm);
45 let val = (sum >> self.shift) + delta;
48 self.dx[i] = self.dx[i + 1];
49 self.dl[i] = self.dl[i + 1];
51 self.dx[4] = (self.dl[4] >> 30) | 1;
52 self.dx[5] = ((self.dl[5] >> 30) | 2) & !1;
53 self.dx[6] = ((self.dl[6] >> 30) | 2) & !1;
54 self.dx[7] = ((self.dl[7] >> 30) | 4) & !3;
55 self.dl[4] = -self.dl[5];
56 self.dl[5] = -self.dl[6];
57 self.dl[6] = val - self.dl[7];
59 self.dl[5] += self.dl[6];
60 self.dl[4] += self.dl[5];
64 fn static_pred(&mut self, bpp: u8, mut val: i32) -> i32 {
66 0 => ((i64::from(self.predictor) * 15) >> 4) as i32,
67 1 | 2 => ((i64::from(self.predictor) * 31) >> 5) as i32,
84 k, sum: RiceDecoder::limit(k)
89 self.sum = RiceDecoder::limit(self.k);
91 fn limit(k: u8) -> u32 { 1 << (k + 4).min(31) }
92 fn update(&mut self, val: u32) {
93 self.sum -= self.sum >> 4;
95 if self.k > 0 && self.sum < Self::limit(self.k) {
97 } else if self.sum > Self::limit(self.k + 1) {
104 fn set(&mut self, val: i32);
107 impl Output for i16 {
108 fn set(&mut self, val: i32) { *self = val as i16; }
110 impl Output for i32 {
111 fn set(&mut self, val: i32) { *self = val; }
114 struct ChannelDecoder {
122 impl ChannelDecoder {
125 filt: Filter::default(),
126 rice0: RiceDecoder::new(),
127 rice1: RiceDecoder::new(),
140 ch_dec: Vec<ChannelDecoder>,
146 ainfo: NAAudioInfo::new(0, 1, SND_S16P_FORMAT, 0),
147 chmap: NAChannelMap::new(),
154 fn decode_frame<T: Output>(&mut self, br: &mut BitReader, dst: &mut [T], stride: usize) -> DecoderResult<bool> {
155 for (i, chdec) in self.ch_dec.iter_mut().enumerate() {
156 chdec.offset = i * stride;
159 chdec.filt.reset(self.bpp);
162 let channels = self.ch_dec.len();
163 let tail_len = self.nsamples % self.framelen;
165 for sample in 0..self.framelen {
166 for chdec in self.ch_dec.iter_mut() {
167 let pfx = br.read_code(UintCodeType::UnaryOnes)?;
168 let (k, pfx, level1) = if pfx == 0 {
169 (chdec.rice0.k, 0, false)
171 (chdec.rice1.k, pfx - 1, true)
173 let mut val = (pfx << k) | br.read(k)?;
175 chdec.rice1.update(val);
176 val += 1 << chdec.rice0.k;
178 chdec.rice0.update(val);
179 let delta = if (val & 1) == 0 {
182 ((val + 1) >> 1) as i32
184 let hval = chdec.filt.hybrid_filt(delta);
185 chdec.sample = chdec.filt.static_pred(self.bpp, hval);
188 self.ch_dec[channels - 1].sample += self.ch_dec[channels - 2].sample / 2;
189 let mut last = self.ch_dec[channels - 1].sample;
190 for chdec in self.ch_dec.iter_mut().rev().skip(1) {
191 chdec.sample = last - chdec.sample;
195 for chdec in self.ch_dec.iter_mut() {
196 dst[chdec.offset].set(chdec.sample);
199 if (tail_len > 0) && (sample == tail_len - 1) && (br.left() < 40) {
208 fn check_crc(buf: &[u8]) -> bool {
212 let mut crc = 0xFFFFFFFF;
213 let ref_crc = read_u32le(&buf[buf.len() - 4..]).unwrap_or(0);
214 for el in buf.iter().take(buf.len() - 4) {
215 crc = CRC32_TAB[(crc as u8 ^ *el) as usize] ^ (crc >> 8);
220 impl NADecoder for TTADecoder {
221 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
222 if let NACodecTypeInfo::Audio(_ainfo) = info.get_properties() {
223 if let Some(buf) = info.get_extradata() {
224 if !check_crc(&buf) {
225 return Err(DecoderError::ChecksumError);
227 let mut mr = MemoryReader::new_read(&buf);
228 let mut br = ByteReader::new(&mut mr);
229 let tag = br.read_tag()?;
230 validate!(&tag == b"TTA1");
231 let afmt = br.read_u16le()?;
233 return Err(DecoderError::NotImplemented);
235 let channels = br.read_u16le()?;
236 validate!(channels > 0 && channels < 256);
237 let bpp = br.read_u16le()?;
238 validate!(bpp > 0 && bpp <= 32);
239 let srate = br.read_u32le()?;
240 validate!(srate > 256 && srate < 1048576);
241 self.nsamples = br.read_u32le()?;
242 validate!(self.nsamples > 0);
244 self.framelen = srate * 256 / 245;
246 self.chmap = if channels == 1 {
247 NAChannelMap::from_str("C").unwrap()
248 } else if channels == 2 {
249 NAChannelMap::from_str("L,R").unwrap()
251 return Err(DecoderError::NotImplemented);
253 let fmt = match bpp {
254 8 | 16 => SND_S16P_FORMAT,
255 24 | 32 => SND_S32P_FORMAT,
256 _ => return Err(DecoderError::NotImplemented),
258 self.bpp = (bpp / 8) as u8;
259 self.ch_dec = Vec::with_capacity(channels as usize);
260 for _ in 0..channels {
261 self.ch_dec.push(ChannelDecoder::new());
264 self.ainfo = NAAudioInfo::new(srate, channels as u8, fmt, self.framelen as usize);
267 Err(DecoderError::InvalidData)
270 Err(DecoderError::InvalidData)
273 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
274 let info = pkt.get_stream().get_info();
275 if let NACodecTypeInfo::Audio(_) = info.get_properties() {
276 let pktbuf = pkt.get_buffer();
277 validate!(pktbuf.len() > 4);
278 if !check_crc(&pktbuf) {
279 return Err(DecoderError::ChecksumError);
282 let mut br = BitReader::new(&pktbuf, BitReaderMode::LE);
284 let mut abuf = alloc_audio_buffer(self.ainfo, self.framelen as usize, self.chmap.clone())?;
285 let duration = match abuf {
286 NABufferType::AudioI16(ref mut adata) => {
287 let stride = adata.get_stride();
288 let dst = adata.get_data_mut().unwrap();
289 let not_last = self.decode_frame(&mut br, dst, stride)?;
293 self.nsamples % self.framelen
296 NABufferType::AudioI32(ref mut adata) => {
297 let stride = adata.get_stride();
298 let dst = adata.get_data_mut().unwrap();
299 let not_last = self.decode_frame(&mut br, dst, stride)?;
303 self.nsamples % self.framelen
308 abuf.truncate_audio(duration as usize);
310 let mut frm = NAFrame::new_from_pkt(pkt, info, abuf);
311 frm.set_duration(Some(u64::from(duration)));
314 Err(DecoderError::InvalidData)
317 fn flush(&mut self) {
321 impl NAOptionHandler for TTADecoder {
322 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
323 fn set_options(&mut self, _options: &[NAOption]) { }
324 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
327 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
328 Box::new(TTADecoder::new())
333 use nihav_core::codecs::RegisteredDecoders;
334 use nihav_core::demuxers::RegisteredDemuxers;
335 use nihav_codec_support::test::dec_video::*;
336 use crate::llaudio_register_all_decoders;
337 use crate::llaudio_register_all_demuxers;
340 let mut dmx_reg = RegisteredDemuxers::new();
341 llaudio_register_all_demuxers(&mut dmx_reg);
342 let mut dec_reg = RegisteredDecoders::new();
343 llaudio_register_all_decoders(&mut dec_reg);
345 // sample: https://samples.mplayerhq.hu/A-codecs/lossless/luckynight.tta
346 test_decoding("tta", "tta", "assets/LLaudio/luckynight.tta", Some(3), &dmx_reg, &dec_reg,
347 ExpectedTestResult::MD5([0xce0fe9c4, 0xa69eefda, 0xe182008c, 0xe941db3f]));
351 const CRC32_TAB: [u32; 256] = [
352 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
353 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
354 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
355 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
356 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
357 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
358 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
359 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
360 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
361 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
362 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
363 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
364 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
365 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
366 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
367 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
368 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
369 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
370 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
371 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
372 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
373 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
374 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
375 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
376 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
377 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
378 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
379 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
380 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
381 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
382 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
383 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
384 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
385 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
386 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
387 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
388 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
389 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
390 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
391 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
392 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
393 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
394 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
395 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
396 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
397 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
398 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
399 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
400 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
401 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
402 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
403 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
404 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
405 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
406 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
407 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
408 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
409 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
410 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
411 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
412 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
413 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
414 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
415 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d