]>
Commit | Line | Data |
---|---|---|
38953fb5 KS |
1 | use nihav_core::formats::*; |
2 | use nihav_core::frame::*; | |
3 | use nihav_core::codecs::*; | |
4 | use nihav_core::io::bitreader::*; | |
5 | use nihav_core::io::byteio::*; | |
6 | use nihav_core::io::codebook::*; | |
38953fb5 | 7 | use nihav_core::dsp::mdct::IMDCT; |
aab478c1 KS |
8 | use std::f32::consts; |
9 | ||
10 | #[derive(Clone,Copy,PartialEq)] | |
11 | enum Mode { | |
12 | Mono, | |
13 | Stereo, | |
14 | JointStereo, | |
15 | } | |
16 | ||
17 | const ATRAC3_FRAME_SIZE: usize = 1024; | |
18 | ||
19 | #[derive(Clone,Copy)] | |
20 | struct Tone { | |
21 | pos: usize, | |
22 | ncoeffs: usize, | |
23 | coeffs: [f32; 8], | |
24 | } | |
25 | ||
26 | struct GainData { | |
27 | ngains: [usize; 4], | |
28 | gain_lev: [[usize; 8]; 4], | |
29 | gain_loc: [[usize; 8]; 4], | |
30 | } | |
31 | ||
32 | impl GainData { | |
33 | fn new() -> Self { | |
34 | Self { | |
35 | ngains: [0; 4], | |
36 | gain_lev: [[0; 8]; 4], | |
37 | gain_loc: [[0; 8]; 4], | |
38 | } | |
39 | } | |
40 | fn read(&mut self, br: &mut BitReader, bands: usize) -> DecoderResult<()> { | |
41 | self.ngains = [0; 4]; | |
42 | for band in 0..bands { | |
43 | self.ngains[band] = br.read(3)? as usize; | |
44 | for i in 0..self.ngains[band] { | |
45 | self.gain_lev[band][i] = br.read(4)? as usize; | |
46 | self.gain_loc[band][i] = br.read(5)? as usize; | |
47 | } | |
48 | } | |
49 | Ok(()) | |
50 | } | |
51 | } | |
52 | ||
53 | struct Channel { | |
54 | data: [f32; ATRAC3_FRAME_SIZE], | |
55 | delay: [f32; ATRAC3_FRAME_SIZE], | |
56 | qmf_delay: [f32; 64 * 3], | |
57 | ||
58 | bands: usize, | |
59 | block_no: usize, | |
60 | ||
61 | gain_data: [GainData; 2], | |
62 | ||
63 | subbands: usize, | |
64 | components: usize, | |
65 | ntones: usize, | |
66 | tones: [Tone; 64], | |
67 | } | |
68 | ||
69 | impl Channel { | |
70 | fn new() -> Self { | |
71 | Self { | |
72 | data: [0.0; ATRAC3_FRAME_SIZE], | |
73 | delay: [0.0; ATRAC3_FRAME_SIZE], | |
74 | qmf_delay: [0.0; 64 * 3], | |
75 | ||
76 | bands: 0, | |
77 | block_no: 0, | |
78 | ||
79 | gain_data: [GainData::new(), GainData::new()], | |
80 | ||
81 | subbands: 0, | |
82 | components: 0, | |
83 | ntones: 0, | |
84 | tones: [Tone {pos: 0, ncoeffs: 0, coeffs: [0.0; 8]}; 64], | |
85 | } | |
86 | } | |
87 | fn decode_unit(&mut self, br: &mut BitReader, codebooks: &[Codebook<u8>; 7], scalefactors: &[f32; 64]) -> DecoderResult<()> { | |
88 | self.bands = (br.read(2)? as usize) + 1; | |
89 | self.gain_data[self.block_no].read(br, self.bands)?; | |
90 | self.data = [0.0; ATRAC3_FRAME_SIZE]; | |
91 | ||
92 | self.ntones = 0; | |
93 | self.components = br.read(5)? as usize; | |
94 | if self.components > 0 { | |
95 | let selector = br.read(2)?; | |
96 | validate!(selector != 2); | |
97 | let mut mode0 = (selector & 1) != 0; | |
98 | ||
99 | for _ in 0..self.components { | |
100 | let mut flags: [bool; 4] = [false; 4]; | |
101 | for band in 0..self.bands { | |
102 | flags[band] = br.read_bool()?; | |
103 | } | |
104 | let nvals = br.read(3)? as usize; | |
105 | let quant = br.read(3)? as usize; | |
106 | validate!(quant > 1); | |
107 | if selector == 3 { | |
108 | mode0 = br.read_bool()?; | |
109 | } | |
110 | for j in 0..self.bands*4 { | |
111 | if !flags[j >> 2] { continue; } | |
112 | let count = br.read(3)? as usize; | |
113 | validate!(self.ntones + count < 64); | |
114 | for _ in 0..count { | |
115 | let scale_idx = br.read(6)? as usize; | |
116 | let tone = &mut self.tones[self.ntones]; | |
117 | tone.pos = (br.read(6)? as usize) + j * 64; | |
118 | tone.ncoeffs = (ATRAC3_FRAME_SIZE - tone.pos).min(nvals + 1); | |
119 | let scale = scalefactors[scale_idx] * ATRAC3_MAX_QUANT[quant]; | |
120 | read_coeffs(br, codebooks, &mut tone.coeffs[..tone.ncoeffs], quant, scale, mode0)?; | |
121 | self.ntones += 1; | |
122 | } | |
123 | } | |
124 | } | |
125 | } | |
126 | ||
127 | // spectrum | |
128 | let mut quants: [usize; 32] = [0; 32]; | |
129 | let mut scf: [usize; 32] = [0; 32]; | |
130 | self.subbands = (br.read(5)? as usize) + 1; | |
131 | let mode0 = br.read_bool()?; | |
132 | for i in 0..self.subbands { | |
133 | quants[i] = br.read(3)? as usize; | |
134 | } | |
135 | for i in 0..self.subbands { | |
136 | if quants[i] == 0 { continue; } | |
137 | scf[i] = br.read(6)? as usize; | |
138 | } | |
139 | ||
140 | self.data = [0.0; ATRAC3_FRAME_SIZE]; | |
141 | for i in 0..self.subbands { | |
142 | if quants[i] == 0 { continue; } | |
143 | let start = ATRAC3_SUBBANDS[i]; | |
144 | let end = ATRAC3_SUBBANDS[i + 1]; | |
145 | let scale = scalefactors[scf[i]] * ATRAC3_MAX_QUANT[quants[i]]; | |
146 | read_coeffs(br, codebooks, &mut self.data[start..end], quants[i], scale, mode0)?; | |
147 | } | |
148 | ||
149 | for i in 0..self.ntones { | |
150 | let tone = &self.tones[i]; | |
151 | for j in 0..tone.ncoeffs { | |
152 | self.data[tone.pos + j] += tone.coeffs[j]; | |
153 | } | |
154 | } | |
155 | Ok(()) | |
156 | } | |
157 | fn synth(&mut self, dsp: &mut DSP) { | |
158 | let mut flag = 0; | |
c83013a1 | 159 | for (band, (data, delay)) in self.data.chunks_mut(256).zip(self.delay.chunks_mut(256)).enumerate() { |
aab478c1 KS |
160 | if (flag & 1) != 0 { |
161 | for i in 0..128 { | |
162 | let t0 = data[i]; | |
163 | let t1 = data[255 - i]; | |
164 | data[i] = t1; | |
165 | data[255 - i] = t0; | |
166 | } | |
167 | } | |
168 | dsp.synth_band(data); | |
169 | dsp.apply_gains(data, delay, &mut self.gain_data, self.block_no, band); | |
170 | delay.copy_from_slice(&dsp.tmp[256..512]); | |
171 | flag ^= 1; | |
aab478c1 KS |
172 | } |
173 | self.block_no ^= 1; | |
174 | } | |
175 | fn do_qmf(&mut self, dsp: &mut DSP, dst: &mut [f32]) { | |
176 | let mut qchunks = self.qmf_delay.chunks_mut(64); | |
177 | let qmf0 = qchunks.next().unwrap(); | |
178 | let qmf1 = qchunks.next().unwrap(); | |
179 | let qmf2 = qchunks.next().unwrap(); | |
180 | { | |
181 | let mut tchunks = self.data.chunks_mut(512); | |
182 | let tmp0 = tchunks.next().unwrap(); | |
183 | let tmp1 = tchunks.next().unwrap(); | |
184 | dsp.do_qmf(tmp0, qmf0, false); | |
185 | dsp.do_qmf(tmp1, qmf1, true); | |
186 | } | |
187 | dsp.do_qmf_out(dst, &self.data, qmf2); | |
188 | } | |
189 | } | |
190 | ||
191 | const ATRAC3_DEFAULT_DELAY: usize = 2190; | |
192 | const ATRAC3_WEIGHTING_DELAYS: [u8; 6] = [ 0, 7, 0, 7, 0, 7 ]; | |
193 | ||
194 | fn read_coeffs(br: &mut BitReader, cb: &[Codebook<u8>; 7], dst: &mut [f32], quant: usize, scale: f32, mode0: bool) -> DecoderResult<()> { | |
195 | if mode0 { | |
196 | read_coeffs_mode0(br, dst, quant, scale) | |
197 | } else if quant == 1 { | |
198 | read_coeffs_mode1(br, &cb[0], dst, scale) | |
199 | } else { | |
200 | read_coeffs_other(br, &cb[quant - 1], dst, scale) | |
201 | } | |
202 | } | |
203 | ||
204 | const ATRAC3_MODE0_CB: [f32; 4] = [ 0.0, 1.0, -2.0, -1.0 ]; | |
205 | const ATRAC3_MODE0_BITS: [u8; 8] = [ 0, 4, 3, 3, 4, 4, 5, 6 ]; | |
206 | ||
207 | fn read_coeffs_mode0(br: &mut BitReader, dst: &mut [f32], quant: usize, scale: f32) -> DecoderResult<()> { | |
208 | let bits = ATRAC3_MODE0_BITS[quant]; | |
209 | if bits > 0 { | |
c83013a1 | 210 | for el in dst.iter_mut() { |
aab478c1 | 211 | let val = br.read_s(bits)? as f32; |
c83013a1 | 212 | *el = val * scale; |
aab478c1 KS |
213 | } |
214 | } else { | |
c83013a1 | 215 | for out in dst.chunks_mut(2) { |
aab478c1 | 216 | let val = br.read(4)? as usize; |
c83013a1 KS |
217 | out[0] = ATRAC3_MODE0_CB[val >> 2] * scale; |
218 | out[1] = ATRAC3_MODE0_CB[val & 3] * scale; | |
aab478c1 KS |
219 | } |
220 | } | |
221 | Ok(()) | |
222 | } | |
223 | ||
224 | const ATRAC3_MODE1_CB: [f32; 9 * 2] = [ | |
225 | 0.0, 0.0, | |
226 | 0.0, 1.0, | |
227 | 0.0, -1.0, | |
228 | 1.0, 0.0, | |
229 | -1.0, 0.0, | |
230 | 1.0, 1.0, | |
231 | 1.0, -1.0, | |
232 | -1.0, 1.0, | |
233 | -1.0, -1.0 | |
234 | ]; | |
235 | ||
236 | fn read_coeffs_mode1(br: &mut BitReader, cb: &Codebook<u8>, dst: &mut [f32], scale: f32) -> DecoderResult<()> { | |
c83013a1 | 237 | for out in dst.chunks_mut(2) { |
aab478c1 | 238 | let val = br.read_cb(cb)? as usize; |
c83013a1 KS |
239 | out[0] = ATRAC3_MODE1_CB[val * 2 + 0] * scale; |
240 | out[1] = ATRAC3_MODE1_CB[val * 2 + 1] * scale; | |
aab478c1 KS |
241 | } |
242 | Ok(()) | |
243 | } | |
244 | ||
245 | fn read_coeffs_other(br: &mut BitReader, cb: &Codebook<u8>, dst: &mut [f32], scale: f32) -> DecoderResult<()> { | |
c83013a1 | 246 | for el in dst.iter_mut() { |
aab478c1 KS |
247 | let val = (br.read_cb(cb)? as i8) + 1; |
248 | let sign = (val & 1) != 0; | |
c83013a1 KS |
249 | let coef = f32::from(if sign { -(val >> 1) } else { val >> 1 }); |
250 | *el = coef * scale; | |
aab478c1 KS |
251 | } |
252 | Ok(()) | |
253 | } | |
254 | ||
255 | struct DSP { | |
256 | imdct: IMDCT, | |
257 | window: [f32; 512], | |
258 | gain_tab: [f32; 16], | |
259 | gain_tab2: [f32; 32], | |
260 | tmp: [f32; ATRAC3_FRAME_SIZE + 64], | |
261 | } | |
262 | ||
263 | impl DSP { | |
264 | fn new() -> Self { | |
265 | let mut gain_tab: [f32; 16] = [0.0; 16]; | |
266 | let mut gain_tab2: [f32; 32] = [0.0; 32]; | |
267 | ||
268 | for i in 0..16 { | |
269 | gain_tab[i] = 2.0f32.powf(4.0 - (i as f32)); | |
270 | } | |
271 | for i in 0..32 { | |
272 | gain_tab2[i] = 2.0f32.powf(((i as f32) - 15.0) * -0.125); | |
273 | } | |
274 | ||
275 | let mut tmpw: [f32; 256] = [0.0; 256]; | |
276 | for i in 0..tmpw.len() { | |
277 | tmpw[i] = (((((i as f32) + 0.5) / 256.0 - 0.5) * consts::PI).sin() + 1.0) * 0.5; | |
278 | } | |
279 | ||
280 | let mut window: [f32; 512] = [0.0; 512]; | |
281 | for i in 0..tmpw.len() { | |
282 | let w = tmpw[i] / (tmpw[i] * tmpw[i] + tmpw[255 - i] * tmpw[255 - i]); | |
283 | window[i] = w; | |
284 | window[512 - 1 - i] = w; | |
285 | } | |
1a151e53 | 286 | |
aab478c1 | 287 | Self { |
9e78289c | 288 | imdct: IMDCT::new(512, false), |
aab478c1 KS |
289 | tmp: [0.0; ATRAC3_FRAME_SIZE + 64], |
290 | gain_tab, gain_tab2, window, | |
291 | } | |
292 | } | |
293 | fn synth_band(&mut self, src: &[f32]) { | |
294 | let dst = &mut self.tmp; | |
295 | self.imdct.imdct(src, dst); | |
296 | for i in 0..512 { | |
297 | dst[i] *= self.window[i]; | |
298 | } | |
299 | } | |
300 | fn apply_gains(&mut self, dst: &mut [f32], delay: &[f32], gain_data: &mut [GainData; 2], block_no: usize, band: usize) { | |
301 | let prev_ngains = gain_data[block_no ^ 1].ngains[band]; | |
302 | let gain1 = if gain_data[block_no].ngains[band] > 0 { | |
303 | self.gain_tab[gain_data[block_no].gain_lev[band][0]] | |
304 | } else { 1.0 }; | |
305 | ||
306 | if prev_ngains == 0 { | |
307 | for i in 0..256 { | |
308 | dst[i] = self.tmp[i] * gain1 + delay[i]; | |
309 | } | |
310 | return; | |
311 | } | |
312 | ||
313 | gain_data[block_no ^ 1].gain_lev[band][prev_ngains] = 4; | |
314 | gain_data[block_no ^ 1].gain_loc[band][prev_ngains] = 32; | |
315 | ||
316 | let mut off = 0; | |
317 | for i in 0..prev_ngains { | |
318 | let start = gain_data[block_no ^ 1].gain_loc[band][i] * 8; | |
319 | let end = start + 8; | |
320 | ||
321 | let mut gain2 = self.gain_tab [gain_data[block_no ^ 1].gain_lev[band][i]]; | |
322 | let gain_inc = self.gain_tab2[gain_data[block_no ^ 1].gain_lev[band][i + 1] + 15 - | |
323 | gain_data[block_no ^ 1].gain_lev[band][i]]; | |
324 | ||
325 | while off < start { | |
326 | dst[off] = (self.tmp[off] * gain1 + delay[off]) * gain2; | |
327 | off += 1; | |
328 | } | |
329 | while off < end { | |
330 | dst[off] = (self.tmp[off] * gain1 + delay[off]) * gain2; | |
331 | gain2 *= gain_inc; | |
332 | off += 1; | |
333 | } | |
334 | } | |
335 | for i in off..256 { | |
336 | dst[i] = self.tmp[i] * gain1 + delay[i]; | |
337 | } | |
338 | } | |
339 | fn qmf_prepare(&mut self, src: &[f32], delay: &[f32], size: usize, swap: bool) { | |
340 | for i in 0..46 { | |
341 | self.tmp[i] = delay[i]; | |
342 | } | |
343 | let (s0, s1) = if !swap { | |
344 | (&src[0..], &src[size/2..]) | |
345 | } else { | |
346 | (&src[size/2..], &src[0..]) | |
347 | }; | |
348 | for i in (0..size).step_by(4) { | |
349 | self.tmp[46 + i + 0] = s0[i / 2 + 0] + s1[i / 2 + 0]; | |
350 | self.tmp[46 + i + 1] = s0[i / 2 + 0] - s1[i / 2 + 0]; | |
351 | self.tmp[46 + i + 2] = s0[i / 2 + 1] + s1[i / 2 + 1]; | |
352 | self.tmp[46 + i + 3] = s0[i / 2 + 1] - s1[i / 2 + 1]; | |
353 | } | |
354 | } | |
355 | fn qmf_synth(&mut self, dst: &mut [f32], size: usize) { | |
356 | for i in (0..size).step_by(2) { | |
357 | let mut acc0 = 0.0; | |
358 | let mut acc1 = 0.0; | |
359 | ||
360 | for j in (0..ATRAC3_QMF_FILTER.len()).step_by(2) { | |
361 | acc0 += self.tmp[i + j + 0] * ATRAC3_QMF_FILTER[j + 0]; | |
362 | acc1 += self.tmp[i + j + 1] * ATRAC3_QMF_FILTER[j + 1]; | |
363 | } | |
364 | dst[i + 0] = acc1 * consts::SQRT_2 / 256.0; | |
365 | dst[i + 1] = acc0 * consts::SQRT_2 / 256.0; | |
366 | } | |
367 | } | |
368 | fn do_qmf(&mut self, dst: &mut [f32], delay: &mut [f32], swap: bool) { | |
369 | self.qmf_prepare(dst, delay, 512, swap); | |
370 | self.qmf_synth(dst, 512); | |
371 | for i in 0..46 { | |
372 | delay[i] = self.tmp[512 + i]; | |
373 | } | |
374 | } | |
375 | fn do_qmf_out(&mut self, dst: &mut [f32], src: &[f32], delay: &mut [f32]) { | |
376 | self.qmf_prepare(src, delay, 1024, false); | |
377 | self.qmf_synth(dst, 1024); | |
378 | for i in 0..46 { | |
379 | delay[i] = self.tmp[1024 + i]; | |
380 | } | |
381 | } | |
382 | } | |
383 | ||
384 | struct Atrac3Decoder { | |
2422d969 | 385 | info: NACodecInfoRef, |
aab478c1 KS |
386 | channels: usize, |
387 | chmap: NAChannelMap, | |
388 | samples: usize, | |
389 | mode: Mode, | |
390 | scrambled: bool, | |
391 | ||
392 | codebooks: [Codebook<u8>; 7], | |
393 | dsp: DSP, | |
394 | ch_data: [Channel; 2], | |
395 | scalefactors: [f32; 64], | |
396 | ||
397 | mci_prev: [usize; 4], | |
398 | mci_cur: [usize; 4], | |
399 | mci_next: [usize; 4], | |
400 | ||
401 | weighting_delay: [u8; 6], | |
402 | ||
403 | pkt_buf: Vec<u8>, | |
404 | } | |
405 | ||
406 | struct Atrac3CodebookReader { | |
407 | bits: &'static [u8], | |
408 | codes: &'static [u8], | |
409 | } | |
410 | impl CodebookDescReader<u8> for Atrac3CodebookReader { | |
411 | fn bits(&mut self, idx: usize) -> u8 { self.bits[idx] } | |
412 | fn code(&mut self, idx: usize) -> u32 { self.codes[idx] as u32 } | |
413 | fn sym (&mut self, idx: usize) -> u8 { idx as u8 } | |
414 | fn len(&mut self) -> usize { self.bits.len() } | |
415 | } | |
416 | ||
417 | impl Atrac3Decoder { | |
418 | fn new() -> Self { | |
419 | let mut scalefactors: [f32; 64] = [0.0; 64]; | |
420 | for i in 0..scalefactors.len() { | |
421 | scalefactors[i] = 2.0f32.powf(((i as f32) - 15.0) / 3.0); | |
422 | } | |
423 | ||
424 | let mut cb0 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[0], bits: ATRAC3_HUFF_BITS[0] }; | |
425 | let mut cb1 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[1], bits: ATRAC3_HUFF_BITS[1] }; | |
426 | let mut cb2 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[2], bits: ATRAC3_HUFF_BITS[2] }; | |
427 | let mut cb3 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[3], bits: ATRAC3_HUFF_BITS[3] }; | |
428 | let mut cb4 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[4], bits: ATRAC3_HUFF_BITS[4] }; | |
429 | let mut cb5 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[5], bits: ATRAC3_HUFF_BITS[5] }; | |
430 | let mut cb6 = Atrac3CodebookReader { codes: ATRAC3_HUFF_CODES[6], bits: ATRAC3_HUFF_BITS[6] }; | |
431 | Self { | |
432 | info: NACodecInfo::new_dummy(), | |
433 | chmap: NAChannelMap::new(), | |
434 | channels: 0, | |
435 | samples: 0, | |
436 | mode: Mode::Mono, | |
437 | scrambled: false, | |
438 | ||
439 | dsp: DSP::new(), | |
440 | ch_data: [Channel::new(), Channel::new()], | |
441 | codebooks: [Codebook::new(&mut cb0, CodebookMode::MSB).unwrap(), | |
442 | Codebook::new(&mut cb1, CodebookMode::MSB).unwrap(), | |
443 | Codebook::new(&mut cb2, CodebookMode::MSB).unwrap(), | |
444 | Codebook::new(&mut cb3, CodebookMode::MSB).unwrap(), | |
445 | Codebook::new(&mut cb4, CodebookMode::MSB).unwrap(), | |
446 | Codebook::new(&mut cb5, CodebookMode::MSB).unwrap(), | |
447 | Codebook::new(&mut cb6, CodebookMode::MSB).unwrap() ], | |
448 | ||
449 | mci_prev: [3; 4], | |
450 | mci_cur: [3; 4], | |
451 | mci_next: [3; 4], | |
452 | ||
453 | weighting_delay: ATRAC3_WEIGHTING_DELAYS, | |
454 | pkt_buf: Vec::with_capacity(65536), | |
455 | scalefactors, | |
456 | } | |
457 | } | |
458 | fn rev_matrix(&mut self) { | |
459 | for i in 0..4 { | |
460 | let c0 = self.mci_prev[i]; | |
461 | let c1 = self.mci_cur[i]; | |
462 | let off; | |
463 | if c0 != c1 { | |
464 | let l0 = ATRAC3_MATRIX_COEFFS[c0 * 2 + 0]; | |
465 | let r0 = ATRAC3_MATRIX_COEFFS[c1 * 2 + 0]; | |
466 | let l1 = ATRAC3_MATRIX_COEFFS[c0 * 2 + 1]; | |
467 | let r1 = ATRAC3_MATRIX_COEFFS[c1 * 2 + 1]; | |
468 | for idx in 0..8 { | |
469 | let t0 = self.ch_data[0].data[idx + i * 256]; | |
470 | let t1 = self.ch_data[1].data[idx + i * 256]; | |
471 | let n1 = t0 * interp(l0, r0, idx) + t1 * interp(l1, r1, idx); | |
472 | self.ch_data[0].data[idx + i * 256] = n1; | |
473 | self.ch_data[1].data[idx + i * 256] = t0 * 2.0 - n1; | |
474 | } | |
475 | off = i * 256 + 8; | |
476 | } else { | |
477 | off = i * 256; | |
478 | } | |
479 | match c1 { | |
480 | 0 => { | |
481 | for i in off..256 { | |
482 | let t0 = self.ch_data[0].data[i]; | |
483 | let t1 = self.ch_data[1].data[i]; | |
484 | self.ch_data[0].data[i] = t1 * 2.0; | |
485 | self.ch_data[1].data[i] = (t0 - t1) * 2.0; | |
486 | } | |
487 | }, | |
488 | 1 => { | |
489 | for i in off..256 { | |
490 | let t0 = self.ch_data[0].data[i]; | |
491 | let t1 = self.ch_data[1].data[i]; | |
492 | self.ch_data[0].data[i] = (t0 + t1) * 2.0; | |
493 | self.ch_data[1].data[i] = t1 * -2.0; | |
494 | } | |
495 | }, | |
496 | _ => { | |
497 | for i in off..256 { | |
498 | let t0 = self.ch_data[0].data[i]; | |
499 | let t1 = self.ch_data[1].data[i]; | |
500 | self.ch_data[0].data[i] = t0 + t1; | |
501 | self.ch_data[1].data[i] = t0 - t1; | |
502 | } | |
503 | }, | |
504 | }; | |
505 | } | |
506 | } | |
507 | fn weigh_channels(&mut self) { | |
508 | if (self.weighting_delay[1] == 7) && (self.weighting_delay[3] == 7) { return; } | |
509 | let pw: [f32; 2]; | |
510 | if self.weighting_delay[1] == 7 { | |
511 | pw = [1.0; 2]; | |
512 | } else { | |
c83013a1 | 513 | let w = f32::from(self.weighting_delay[1]) / 7.0; |
aab478c1 KS |
514 | let iw = (2.0 - w * w).sqrt(); |
515 | if self.weighting_delay[0] == 0 { | |
516 | pw = [w, iw]; | |
517 | } else { | |
518 | pw = [iw, w]; | |
519 | } | |
520 | } | |
521 | let cw: [f32; 2]; | |
522 | if self.weighting_delay[3] == 7 { | |
523 | cw = [1.0; 2]; | |
524 | } else { | |
c83013a1 | 525 | let w = f32::from(self.weighting_delay[3]) / 7.0; |
aab478c1 KS |
526 | let iw = (2.0 - w * w).sqrt(); |
527 | if self.weighting_delay[2] == 0 { | |
528 | cw = [w, iw]; | |
529 | } else { | |
530 | cw = [iw, w]; | |
531 | } | |
532 | } | |
533 | ||
534 | for i in 0..4 { | |
535 | let off = i * 256; | |
536 | for j in 0..8 { | |
537 | self.ch_data[0].data[off + j] *= interp(pw[0], pw[1], j); | |
538 | self.ch_data[1].data[off + j] *= interp(pw[0], pw[1], j); | |
539 | } | |
540 | for j in 8..256 { | |
541 | self.ch_data[0].data[off + j] *= cw[0]; | |
542 | self.ch_data[1].data[off + j] *= cw[1]; | |
543 | } | |
544 | } | |
545 | } | |
546 | } | |
547 | ||
548 | fn interp(a: f32, b: f32, pos: usize) -> f32 { | |
549 | a + ((pos as f32) / 8.0) * (b - a) | |
550 | } | |
551 | ||
552 | impl NADecoder for Atrac3Decoder { | |
01613464 | 553 | fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { |
aab478c1 KS |
554 | if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() { |
555 | self.info = info.clone(); | |
556 | let edata = info.get_extradata().unwrap(); | |
557 | validate!(edata.len() >= 4); | |
558 | ||
559 | let mut mr = MemoryReader::new_read(&edata); | |
560 | let mut br = ByteReader::new(&mut mr); | |
561 | ||
562 | match edata.len() { | |
563 | 10 => { | |
564 | let version = br.read_u32be()?; | |
565 | validate!(version == 4); | |
566 | self.samples = br.read_u16be()? as usize; | |
567 | let delay = br.read_u16be()? as usize; | |
568 | validate!(delay == ATRAC3_DEFAULT_DELAY); | |
569 | let mode = br.read_u16be()?; | |
570 | self.mode = match mode { | |
571 | 0 => Mode::Mono, | |
572 | 2 => Mode::Stereo, | |
573 | 0x12 => Mode::JointStereo, | |
574 | _ => return Err(DecoderError::InvalidData), | |
575 | }; | |
576 | self.channels = if self.mode == Mode::Mono { 1 } else { 2 }; | |
577 | self.scrambled = true; | |
578 | }, | |
579 | 14 | 16 => { | |
580 | if edata.len() == 16 { br.read_skip(2)?; } | |
581 | self.samples = br.read_u32be()? as usize; | |
582 | let mode = br.read_u16be()?; | |
583 | self.mode = if mode != 0 { Mode::JointStereo } else { Mode::Stereo }; | |
584 | self.channels = 2; | |
585 | br.read_skip(2)?; | |
586 | let ffactor = br.read_u16be()? as usize; | |
587 | validate!((ffactor > 0) && (ffactor <= 16)); | |
588 | // calculate block_align / channels / ffactor, validate it's 96/152/192 | |
589 | }, | |
590 | _ => { return Err(DecoderError::InvalidData) } | |
591 | }; | |
592 | validate!(self.samples == ATRAC3_FRAME_SIZE * self.channels); | |
593 | if self.mode == Mode::Mono { | |
594 | self.chmap.add_channel(NAChannelType::C); | |
595 | } else { | |
596 | self.chmap.add_channel(NAChannelType::L); | |
597 | self.chmap.add_channel(NAChannelType::R); | |
598 | } | |
599 | let ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), self.channels as u8, | |
600 | SND_F32P_FORMAT, self.samples); | |
601 | self.info = info.replace_info(NACodecTypeInfo::Audio(ainfo)); | |
602 | Ok(()) | |
603 | } else { | |
604 | Err(DecoderError::InvalidData) | |
605 | } | |
606 | } | |
01613464 | 607 | fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> { |
aab478c1 KS |
608 | let info = pkt.get_stream().get_info(); |
609 | validate!(info.get_properties().is_audio()); | |
610 | let pktbuf = pkt.get_buffer(); | |
611 | validate!(pktbuf.len() > 5); | |
612 | ||
613 | let frame_size = pktbuf.len(); | |
614 | self.pkt_buf.resize(frame_size, 0); | |
615 | if self.scrambled { | |
616 | for (i, s) in pktbuf.iter().enumerate() { | |
617 | self.pkt_buf[i] = s ^ ATRAC3_XOR_KEY[i & 3]; | |
618 | } | |
619 | } else { | |
620 | let dst = &mut self.pkt_buf[0..frame_size]; | |
621 | dst.copy_from_slice(pktbuf.as_slice()); | |
622 | } | |
623 | ||
624 | { | |
625 | let mut br = BitReader::new(self.pkt_buf.as_slice(), frame_size, BitReaderMode::BE); | |
626 | let id = br.read(6)?; | |
627 | validate!(id == 0x28); | |
628 | self.ch_data[0].decode_unit(&mut br, &self.codebooks, &self.scalefactors)?; | |
629 | self.ch_data[0].synth(&mut self.dsp); | |
630 | } | |
631 | if self.channels == 2 { | |
632 | let off; | |
633 | if self.mode == Mode::JointStereo { | |
634 | for i in 0..frame_size / 2 { | |
635 | let b0 = self.pkt_buf[i]; | |
636 | let b1 = self.pkt_buf[frame_size - i - 1]; | |
637 | self.pkt_buf[i] = b1; | |
638 | self.pkt_buf[frame_size - i - 1] = b0; | |
639 | } | |
640 | let mut i = 0; | |
641 | while (i < frame_size) && (self.pkt_buf[i] == 0xF8) { i += 1; } | |
642 | validate!(frame_size - i > 4); | |
643 | off = i; | |
644 | } else { | |
645 | off = frame_size / 2; | |
646 | } | |
647 | let mut br = BitReader::new(&self.pkt_buf[off..], frame_size - off, BitReaderMode::BE); | |
648 | if self.mode == Mode::JointStereo { | |
649 | let id = br.read(2)?; | |
650 | validate!(id == 0x3); | |
651 | } else { | |
652 | let id = br.read(6)?; | |
653 | validate!(id == 0x28); | |
654 | } | |
655 | if self.mode == Mode::JointStereo { | |
656 | for i in 0..self.weighting_delay.len() - 2 { | |
657 | self.weighting_delay[i] = self.weighting_delay[i + 2]; | |
658 | } | |
659 | self.weighting_delay[4] = br.read(1)? as u8; | |
660 | self.weighting_delay[5] = br.read(3)? as u8; | |
661 | self.mci_prev = self.mci_cur; | |
662 | self.mci_cur = self.mci_next; | |
663 | for i in 0..4 { | |
664 | self.mci_next[i] = br.read(2)? as usize; | |
665 | } | |
666 | } | |
667 | self.ch_data[1].decode_unit(&mut br, &self.codebooks, &self.scalefactors)?; | |
668 | self.ch_data[1].synth(&mut self.dsp); | |
669 | } | |
670 | if self.mode == Mode::JointStereo { | |
671 | self.rev_matrix(); | |
672 | self.weigh_channels(); | |
673 | } | |
674 | ||
675 | let ainfo = self.info.get_properties().get_audio_info().unwrap(); | |
676 | ||
b70cc006 | 677 | let abuf = alloc_audio_buffer(ainfo, ATRAC3_FRAME_SIZE, self.chmap.clone())?; |
aab478c1 | 678 | let mut adata = abuf.get_abuf_f32().unwrap(); |
1a967e6b | 679 | let output = adata.get_data_mut().unwrap(); |
aab478c1 KS |
680 | |
681 | for ch in 0..self.channels { | |
682 | let dpos = abuf.get_offset(ch); | |
683 | self.ch_data[ch].do_qmf(&mut self.dsp, &mut output[dpos..][..ATRAC3_FRAME_SIZE]); | |
684 | } | |
685 | ||
686 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.replace_info(NACodecTypeInfo::Audio(ainfo)), abuf); | |
687 | frm.set_keyframe(true); | |
171860fc | 688 | Ok(frm.into_ref()) |
aab478c1 | 689 | } |
f9be4e75 KS |
690 | fn flush(&mut self) { |
691 | for ch_data in self.ch_data.iter_mut() { | |
692 | ch_data.delay = [0.0; 1024]; | |
693 | ch_data.qmf_delay = [0.0; 64 * 3]; | |
694 | } | |
695 | } | |
aab478c1 KS |
696 | } |
697 | ||
6011e201 | 698 | pub fn get_decoder() -> Box<dyn NADecoder> { |
aab478c1 KS |
699 | Box::new(Atrac3Decoder::new()) |
700 | } | |
701 | ||
702 | #[cfg(test)] | |
703 | mod test { | |
4f6124ac KS |
704 | use nihav_core::codecs::RegisteredDecoders; |
705 | use nihav_core::demuxers::RegisteredDemuxers; | |
706 | use nihav_core::test::dec_video::test_decode_audio; | |
707 | use crate::codecs::generic_register_all_codecs; | |
708 | use nihav_realmedia::demuxers::realmedia_register_all_demuxers; | |
aab478c1 KS |
709 | #[test] |
710 | fn test_atrac3() { | |
4f6124ac KS |
711 | let mut dmx_reg = RegisteredDemuxers::new(); |
712 | realmedia_register_all_demuxers(&mut dmx_reg); | |
713 | let mut dec_reg = RegisteredDecoders::new(); | |
714 | generic_register_all_codecs(&mut dec_reg); | |
715 | ||
aab478c1 KS |
716 | let file = "assets/RV/rv30_atrc_384x208_realproducer_plus_8.51.rm"; |
717 | // let file = "assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm"; | |
4f6124ac | 718 | test_decode_audio("realmedia", file, Some(12000), "atrac3", &dmx_reg, &dec_reg); |
aab478c1 KS |
719 | } |
720 | } | |
721 | ||
722 | const ATRAC3_XOR_KEY: [u8; 4] = [ 0x53, 0x7F, 0x61, 0x03 ]; | |
723 | ||
724 | const ATRAC3_HUFF_CODES: [&[u8]; 7] = [ | |
725 | &[ 0x00, 0x04, 0x05, 0x0C, 0x0D, 0x1C, 0x1D, 0x1E, 0x1F ], | |
726 | &[ 0x00, 0x04, 0x05, 0x06, 0x07 ], | |
727 | &[ 0x00, 0x04, 0x05, 0x0C, 0x0D, 0x0E, 0x0F ], | |
728 | &[ 0x00, 0x04, 0x05, 0x0C, 0x0D, 0x1C, 0x1D, 0x1E, 0x1F ], // same as 0 | |
729 | &[ 0x00, 0x02, 0x03, 0x08, 0x09, 0x0A, 0x0B, 0x1C, 0x1D, 0x3C, 0x3D, 0x3E, 0x3F, 0x0C, 0x0D ], | |
730 | &[ 0x00, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x14, | |
731 | 0x15, 0x16, 0x17, 0x18, 0x19, 0x34, 0x35, 0x36, | |
732 | 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x78, 0x79, 0x7A, | |
733 | 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x08, 0x09 ], | |
734 | &[ 0x00, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, | |
735 | 0x0F, 0x10, 0x11, 0x24, 0x25, 0x26, 0x27, 0x28, | |
736 | 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, | |
737 | 0x31, 0x32, 0x33, 0x68, 0x69, 0x6A, 0x6B, 0x6C, | |
738 | 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, | |
739 | 0x75, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, | |
740 | 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, | |
741 | 0xFB, 0xFC, 0xFD, 0xFE, 0xFF, 0x02, 0x03 ] | |
742 | ]; | |
743 | const ATRAC3_HUFF_BITS: [&[u8]; 7] = [ | |
744 | &[ 1, 3, 3, 4, 4, 5, 5, 5, 5 ], | |
745 | &[ 1, 3, 3, 3, 3 ], | |
746 | &[ 1, 3, 3, 4, 4, 4, 4 ], | |
747 | &[ 1, 3, 3, 4, 4, 5, 5, 5, 5 ], | |
748 | &[ 2, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 4, 4 ], | |
749 | &[ 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 4, 4 ], | |
750 | &[ 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, | |
751 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4 ], | |
752 | ]; | |
753 | ||
754 | const ATRAC3_MAX_QUANT: [f32; 8] = [ 0.0, 1.0/1.5, 1.0/2.5, 1.0/3.5, 1.0/4.5, 1.0/7.5, 1.0/15.5, 1.0/31.5 ]; | |
755 | ||
756 | const ATRAC3_SUBBANDS: [usize; 32 + 1] = [ | |
757 | 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, | |
758 | 192, 224, 256, 288, 320, 352, 384, 416, 448, 480, 512, 576, 640, 704, 768, 896, | |
759 | 1024 | |
760 | ]; | |
761 | ||
762 | const ATRAC3_MATRIX_COEFFS: [f32; 8] = [ 0.0, 2.0, 2.0, 2.0, 0.0, 0.0, 1.0, 1.0 ]; | |
763 | ||
764 | const ATRAC3_QMF_FILTER: [f32; 48] = [ | |
765 | -0.000029238139, -0.000184109580, -0.000112315138, 0.000602345390, | |
766 | 0.000484503806, -0.001705877949, -0.001041114796, 0.004068033770, | |
767 | 0.001566677820, -0.008430772461, -0.001512299757, 0.015680588782, | |
768 | -0.000122339843, -0.026883240789, 0.004925364163, 0.043472178280, | |
769 | -0.015603342094, -0.068180441856, 0.037618979812, 0.108652018011, | |
770 | -0.087192758918, -0.198768734932, 0.264158189297, 0.928483188152, | |
771 | 0.928483188152, 0.264158189297, -0.198768734932, -0.087192758918, | |
772 | 0.108652018011, 0.037618979812, -0.068180441856, -0.015603342094, | |
773 | 0.043472178280, 0.004925364163, -0.026883240789, -0.000122339843, | |
774 | 0.015680588782, -0.001512299757, -0.008430772461, 0.001566677820, | |
775 | 0.004068033770, -0.001041114796, -0.001705877949, 0.000484503806, | |
776 | 0.000602345390, -0.000112315138, -0.000184109580, -0.000029238139 | |
777 | ]; |