]>
Commit | Line | Data |
---|---|---|
6a494288 KS |
1 | use nihav_core::frame::*; |
2 | use nihav_core::codecs::*; | |
3 | use nihav_core::io::byteio::read_u16le; | |
4 | use nihav_core::io::bitreader::*; | |
5 | use nihav_core::io::codebook::*; | |
6 | use nihav_core::io::intcode::*; | |
7 | use nihav_core::dsp::fft::*; | |
8 | use nihav_core::dsp::mdct::IMDCT; | |
9 | ||
10 | use std::str::FromStr; | |
11 | ||
12 | use super::on2avcdata::*; | |
13 | ||
14 | const COEFFS: usize = 1024; | |
15 | const MAX_BANDS: usize = 14 * 8; | |
16 | const QTAB_SIZE: usize = 10000; | |
17 | const SHORT_WIN_POINT0: usize = 512 - 64; | |
18 | const SHORT_WIN_POINT1: usize = 512 + 64; | |
19 | ||
20 | struct WinInfo { | |
21 | is_long: bool, | |
22 | bands: &'static [usize], | |
23 | } | |
24 | ||
25 | impl WinInfo { | |
26 | fn get_tot_bands(&self) -> usize { | |
27 | if self.is_long { | |
28 | self.bands.len() | |
29 | } else { | |
30 | self.bands.len() * 8 | |
31 | } | |
32 | } | |
33 | } | |
34 | ||
35 | struct Codebooks { | |
36 | scale_cb: Codebook<i8>, | |
37 | spec_cb: [Codebook<u16>; 15], | |
38 | } | |
39 | ||
40 | fn scale_map(idx: usize) -> i8 { (idx as i8) - 60 } | |
41 | fn cb_map1(idx: usize) -> u16 { AVC_SPEC_CB1_SYMS[idx] } | |
42 | fn cb_map2(idx: usize) -> u16 { AVC_SPEC_CB2_SYMS[idx] } | |
43 | fn cb_map3(idx: usize) -> u16 { AVC_SPEC_CB3_SYMS[idx] } | |
44 | fn cb_map4(idx: usize) -> u16 { AVC_SPEC_CB4_SYMS[idx] } | |
45 | fn cb_map5(idx: usize) -> u16 { AVC_SPEC_CB5_SYMS[idx] } | |
46 | fn cb_map6(idx: usize) -> u16 { AVC_SPEC_CB6_SYMS[idx] } | |
47 | fn cb_map7(idx: usize) -> u16 { AVC_SPEC_CB7_SYMS[idx] } | |
48 | fn cb_map8(idx: usize) -> u16 { AVC_SPEC_CB8_SYMS[idx] } | |
49 | fn cb_map9(idx: usize) -> u16 { AVC_SPEC_CB9_SYMS[idx] } | |
50 | fn cb_map10(idx: usize) -> u16 { AVC_SPEC_CB10_SYMS[idx] } | |
51 | fn cb_map11(idx: usize) -> u16 { AVC_SPEC_CB11_SYMS[idx] } | |
52 | fn cb_map12(idx: usize) -> u16 { AVC_SPEC_CB12_SYMS[idx] } | |
53 | fn cb_map13(idx: usize) -> u16 { AVC_SPEC_CB13_SYMS[idx] } | |
54 | fn cb_map14(idx: usize) -> u16 { AVC_SPEC_CB14_SYMS[idx] } | |
55 | fn cb_map15(idx: usize) -> u16 { AVC_SPEC_CB15_SYMS[idx] } | |
56 | ||
57 | impl Codebooks { | |
58 | fn new() -> Self { | |
59 | let mut coderead = TableCodebookDescReader::new(AVC_SCF_CODEBOOK_CODES, AVC_SCF_CODEBOOK_BITS, scale_map); | |
60 | let scale_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
61 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB1_CODES, AVC_SPEC_CB1_BITS, cb_map1); | |
62 | let cb1 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
63 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB2_CODES, AVC_SPEC_CB2_BITS, cb_map2); | |
64 | let cb2 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
65 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB3_CODES, AVC_SPEC_CB3_BITS, cb_map3); | |
66 | let cb3 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
67 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB4_CODES, AVC_SPEC_CB4_BITS, cb_map4); | |
68 | let cb4 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
69 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB5_CODES, AVC_SPEC_CB5_BITS, cb_map5); | |
70 | let cb5 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
71 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB6_CODES, AVC_SPEC_CB6_BITS, cb_map6); | |
72 | let cb6 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
73 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB7_CODES, AVC_SPEC_CB7_BITS, cb_map7); | |
74 | let cb7 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
75 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB8_CODES, AVC_SPEC_CB8_BITS, cb_map8); | |
76 | let cb8 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
77 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB9_CODES, AVC_SPEC_CB9_BITS, cb_map9); | |
78 | let cb9 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
79 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB10_CODES, AVC_SPEC_CB10_BITS, cb_map10); | |
80 | let cb10 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
81 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB11_CODES, AVC_SPEC_CB11_BITS, cb_map11); | |
82 | let cb11 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
83 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB12_CODES, AVC_SPEC_CB12_BITS, cb_map12); | |
84 | let cb12 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
85 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB13_CODES, AVC_SPEC_CB13_BITS, cb_map13); | |
86 | let cb13 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
87 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB14_CODES, AVC_SPEC_CB14_BITS, cb_map14); | |
88 | let cb14 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
89 | let mut coderead = TableCodebookDescReader::new(AVC_SPEC_CB15_CODES, AVC_SPEC_CB15_BITS, cb_map15); | |
90 | let cb15 = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
91 | let spec_cb = [cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9, cb10, cb11, cb12, cb13, cb14, cb15]; | |
92 | Self { scale_cb, spec_cb } | |
93 | } | |
94 | } | |
95 | ||
96 | struct SynthDSP { | |
97 | rdft512: RDFT, | |
98 | rdft256: RDFT, | |
99 | irdft128: RDFT, | |
100 | irdft64: RDFT, | |
101 | synth_tmp: [FFTComplex; 512], | |
102 | synth_out: [FFTComplex; 512], | |
103 | } | |
104 | ||
105 | impl SynthDSP { | |
106 | fn new() -> Self { | |
107 | let rdft512 = RDFTBuilder::new_rdft(512, true, true); | |
108 | let rdft256 = RDFTBuilder::new_rdft(256, true, true); | |
109 | let irdft128 = RDFTBuilder::new_rdft(128, false, false); | |
110 | let irdft64 = RDFTBuilder::new_rdft(64, false, false); | |
111 | Self { | |
112 | rdft512, rdft256, irdft128, irdft64, | |
113 | synth_tmp: [FFTC_ZERO; 512], | |
114 | synth_out: [FFTC_ZERO; 512], | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | struct AVCDecoder { | |
120 | chmap: NAChannelMap, | |
121 | ainfo: NAAudioInfo, | |
122 | info: NACodecInfoRef, | |
123 | dsp: SynthDSP, | |
124 | version: i32, | |
125 | codebooks: Codebooks, | |
126 | channels: usize, | |
127 | is_40khz: bool, | |
128 | win_long: &'static [f32; 1024], | |
129 | ||
130 | prev_win: u8, | |
131 | cur_win: u8, | |
132 | winfo: &'static WinInfo, | |
133 | windows: usize, | |
134 | win_grp: [bool; 8], | |
135 | ms_present: bool, | |
136 | ms_info: [bool; MAX_BANDS], | |
137 | cbs: [u8; MAX_BANDS], | |
138 | scales: [u8; MAX_BANDS], | |
139 | ||
140 | imdct_long: IMDCT, | |
141 | imdct_mid: IMDCT, | |
142 | imdct_short: IMDCT, | |
143 | coeffs: [[f32; COEFFS]; 2], | |
144 | delay: [[f32; COEFFS]; 2], | |
145 | tmp: [f32; COEFFS * 2], | |
146 | ew_buf: [f32; 1152], | |
147 | ||
148 | qtab: [f32; QTAB_SIZE], | |
149 | scale_tab: [f32; 128], | |
150 | } | |
151 | ||
152 | impl AVCDecoder { | |
153 | fn new(version: i32) -> Self { | |
154 | let mut qtab = [0.0f32; QTAB_SIZE]; | |
155 | for (i, el) in qtab.iter_mut().enumerate() { | |
156 | *el = (i as f32) * (i as f32).sqrt(); | |
157 | } | |
158 | let mut scale_tab = [0.0f32; 128]; | |
159 | for (i, el) in scale_tab.iter_mut().enumerate() { | |
160 | let pow = 10.0f32.powf((i as f32) * 0.1); | |
161 | if i < 20 { | |
162 | *el = (pow * 16.0).ceil() / 32.0; | |
163 | } else { | |
164 | *el = (pow * 0.5).ceil(); | |
165 | } | |
166 | *el /= 32768.0; | |
167 | } | |
168 | ||
169 | AVCDecoder { | |
170 | chmap: NAChannelMap::new(), | |
171 | ainfo: NAAudioInfo::new(0, 0, SND_F32P_FORMAT, 0), | |
172 | info: NACodecInfo::new_dummy(), | |
173 | dsp: SynthDSP::new(), | |
174 | version, | |
175 | codebooks: Codebooks::new(), | |
176 | channels: 0, | |
177 | is_40khz: false, | |
178 | win_long: AVC_WIN_LONG_32K, | |
179 | ||
180 | prev_win: 0, | |
181 | cur_win: 0, | |
182 | winfo: &AVC_WINFO_LONG, | |
183 | windows: 0, | |
184 | win_grp: [false; 8], | |
185 | ms_present: false, | |
186 | ms_info: [false; MAX_BANDS], | |
187 | cbs: [0; MAX_BANDS], | |
188 | scales: [0; MAX_BANDS], | |
189 | ||
190 | imdct_long: IMDCT::new(1024 * 2, true), | |
191 | imdct_mid: IMDCT::new(512 * 2, true), | |
192 | imdct_short: IMDCT::new(128 * 2, true), | |
193 | coeffs: [[0.0; COEFFS]; 2], | |
194 | delay: [[0.0; COEFFS]; 2], | |
195 | tmp: [0.0; COEFFS * 2], | |
196 | ew_buf: [0.0; 1152], | |
197 | ||
198 | qtab, | |
199 | scale_tab, | |
200 | } | |
201 | } | |
202 | fn decode_channel(&mut self, br: &mut BitReader, chno: usize) -> DecoderResult<()> { | |
203 | let coeffs = &mut self.coeffs[chno]; | |
204 | ||
205 | decode_band_types(br, &mut self.cbs, self.winfo)?; | |
206 | // band scales | |
207 | let bands = self.winfo.bands.len(); | |
208 | let mut cur_band = 0; | |
209 | let mut scale = 0; | |
210 | let mut first = true; | |
211 | for wg in self.win_grp.iter().take(self.windows) { | |
212 | if *wg { | |
213 | for _ in 0..bands { | |
214 | if self.cbs[cur_band] == 0 { | |
215 | self.scales[cur_band] = 0; | |
216 | } else { | |
217 | if first { | |
218 | scale = br.read(7)? as i16; | |
219 | first = false | |
220 | } else { | |
221 | scale += br.read_cb(&self.codebooks.scale_cb)? as i16; | |
222 | validate!((scale >= 0) && (scale < 128)); | |
223 | } | |
224 | self.scales[cur_band] = scale as u8; | |
225 | } | |
226 | cur_band += 1; | |
227 | } | |
228 | } else { | |
229 | for _ in 0..bands { | |
230 | self.scales[cur_band] = self.scales[cur_band - bands]; | |
231 | cur_band += 1; | |
232 | } | |
233 | } | |
234 | } | |
235 | // coefficients | |
236 | let mut cur_band = 0; | |
237 | let mut idx = 0; | |
238 | *coeffs = [0.0; COEFFS]; | |
239 | for _ in 0..self.windows { | |
240 | let mut band_start = 0; | |
241 | for band_end in self.winfo.bands.iter() { | |
242 | let band_size = *band_end - band_start; | |
243 | let cb_idx = self.cbs[cur_band]; | |
244 | if cb_idx > 0 { | |
245 | let cb = &self.codebooks.spec_cb[(cb_idx - 1) as usize]; | |
246 | let scale = self.scale_tab[self.scales[cur_band] as usize]; | |
247 | let dst = &mut coeffs[idx..][..band_size]; | |
248 | if cb_idx < 9 { | |
249 | decode_quads(br, cb, &self.qtab, scale, dst)?; | |
250 | } else { | |
251 | decode_pairs(br, cb, &self.qtab, scale, dst, cb_idx == 15)?; | |
252 | } | |
253 | } | |
254 | ||
255 | band_start = *band_end; | |
256 | idx += band_size; | |
257 | cur_band += 1; | |
258 | } | |
259 | } | |
260 | Ok(()) | |
261 | } | |
262 | fn decode_frame(&mut self, br: &mut BitReader, adata: &mut NAAudioBuffer<f32>, offset: usize) -> DecoderResult<()> { | |
263 | let enh_flag = br.read_bool()?; | |
264 | validate!(!enh_flag); | |
265 | self.prev_win = self.cur_win; | |
266 | self.cur_win = br.read(3)? as u8; | |
267 | self.winfo = match self.cur_win { | |
268 | 3 => &AVC_WINFO_SHORT, | |
269 | 4 => if self.is_40khz { &AVC_WINFO_40K_MODE4 } else { &AVC_WINFO_44K_MODE4 }, | |
270 | 5 => if self.is_40khz { &AVC_WINFO_40K_MODE5 } else { &AVC_WINFO_44K_MODE5 }, | |
271 | 6 => if self.is_40khz { &AVC_WINFO_40K_MODE6 } else { &AVC_WINFO_44K_MODE6 }, | |
272 | _ => &AVC_WINFO_LONG, | |
273 | }; | |
274 | self.windows = if self.winfo.is_long { 1 } else { 8 }; | |
275 | let bands = self.winfo.bands.len(); | |
276 | self.win_grp[0] = true; | |
277 | for el in self.win_grp.iter_mut().skip(1).take(self.windows - 1) { | |
278 | *el = !br.read_bool()?; | |
279 | } | |
280 | ||
281 | self.ms_present = br.read_bool()?; | |
282 | if self.ms_present { | |
283 | validate!(self.channels == 2); | |
284 | let mut cur_band = 0; | |
285 | for wg in self.win_grp.iter().take(self.windows) { | |
286 | if *wg { | |
287 | for _ in 0..bands { | |
288 | self.ms_info[cur_band] = br.read_bool()?; | |
289 | cur_band += 1; | |
290 | } | |
291 | } else { | |
292 | for _ in 0..bands { | |
293 | self.ms_info[cur_band] = self.ms_info[cur_band - bands]; | |
294 | cur_band += 1; | |
295 | } | |
296 | } | |
297 | } | |
298 | } | |
299 | for ch in 0..self.channels { | |
300 | self.decode_channel(br, ch)?; | |
301 | } | |
302 | ||
303 | if (self.channels == 2) && self.ms_present { | |
304 | let mut idx = 0; | |
305 | let mut cur_band = 0; | |
306 | for _ in 0..self.windows { | |
307 | let mut band_start = 0; | |
308 | for band_end in self.winfo.bands.iter() { | |
309 | let band_size = *band_end - band_start; | |
310 | if self.ms_info[cur_band] { | |
311 | for i in 0..band_size { | |
312 | let l = self.coeffs[0][idx + i]; | |
313 | let r = self.coeffs[1][idx + i]; | |
314 | self.coeffs[0][idx + i] = l + r; | |
315 | self.coeffs[1][idx + i] = l - r; | |
316 | } | |
317 | } | |
318 | cur_band += 1; | |
319 | idx += band_size; | |
320 | band_start = *band_end; | |
321 | } | |
322 | } | |
323 | } | |
324 | ||
325 | for ch in 0..self.channels { | |
326 | let off = adata.get_offset(ch) + offset; | |
327 | let output = adata.get_data_mut().unwrap(); | |
328 | self.synth_channel(ch, &mut output[off..][..COEFFS]); | |
329 | } | |
330 | Ok(()) | |
331 | } | |
332 | fn synth_channel(&mut self, chno: usize, dst: &mut [f32]) { | |
333 | let coeffs = &mut self.coeffs[chno]; | |
334 | let delay = &mut self.delay[chno]; | |
335 | ||
336 | match self.cur_win { | |
337 | 0 | 1 => { | |
338 | self.imdct_long.imdct(coeffs, &mut self.tmp); | |
339 | overlap(dst, &self.tmp, delay, self.win_long); | |
340 | delay.copy_from_slice(&self.tmp[COEFFS..]); | |
341 | }, | |
342 | 2 => { | |
343 | self.imdct_long.imdct(coeffs, &mut self.tmp); | |
344 | for i in 0..SHORT_WIN_POINT0 { | |
345 | dst[i] = delay[i]; | |
346 | } | |
347 | overlap(&mut dst[SHORT_WIN_POINT0..SHORT_WIN_POINT1], | |
348 | &self.tmp[SHORT_WIN_POINT0..SHORT_WIN_POINT1], | |
349 | &delay[SHORT_WIN_POINT0..SHORT_WIN_POINT1], AVC_WIN_SHORT); | |
350 | for i in SHORT_WIN_POINT1..COEFFS { | |
351 | dst[i] = self.tmp[i]; | |
352 | } | |
353 | delay.copy_from_slice(&self.tmp[COEFFS..]); | |
354 | }, | |
355 | 3 => { | |
356 | for (ain, aout) in coeffs.chunks(128).zip(self.tmp.chunks_mut(256)) { | |
357 | self.imdct_short.imdct(ain, aout); | |
358 | } | |
359 | self.ew_buf = [0.0; 1152]; | |
360 | for i in 0..128 { | |
361 | self.ew_buf[i] = self.tmp[i]; | |
362 | } | |
363 | for w in 0..7 { | |
364 | overlap(&mut self.ew_buf[(w + 1) * 128..][..128], | |
365 | &self.tmp[(w + 1) * 256..][..128], | |
366 | &self.tmp[w * 256 + 128..][..128], | |
367 | AVC_WIN_SHORT); | |
368 | } | |
369 | for i in 0..128 { | |
370 | self.ew_buf[1024 + i] = self.tmp[7 * 256 + 128 + i]; | |
371 | } | |
372 | for i in 0..SHORT_WIN_POINT0 { | |
373 | dst[i] = delay[i]; | |
374 | } | |
375 | overlap(&mut dst[SHORT_WIN_POINT0..SHORT_WIN_POINT1], | |
376 | &self.ew_buf[0..128], | |
377 | &delay[SHORT_WIN_POINT0..SHORT_WIN_POINT1], AVC_WIN_SHORT); | |
378 | for i in SHORT_WIN_POINT1..COEFFS { | |
379 | dst[i] = self.ew_buf[i - SHORT_WIN_POINT1 + 128]; | |
380 | } | |
381 | for i in 0..SHORT_WIN_POINT1 { | |
382 | delay[i] = self.ew_buf[SHORT_WIN_POINT1 + i]; | |
383 | } | |
384 | for i in SHORT_WIN_POINT1..COEFFS { | |
385 | delay[i] = 0.0; | |
386 | } | |
387 | }, | |
388 | 4 => { | |
389 | for i in 0..SHORT_WIN_POINT0 { | |
390 | dst[i] = delay[i]; | |
391 | } | |
392 | synth1024(&mut self.dsp, coeffs, &mut self.ew_buf, &mut self.tmp, self.is_40khz); | |
393 | let tmp = &mut self.ew_buf[0..COEFFS]; | |
394 | tmp.reverse(); | |
395 | overlap(&mut dst[SHORT_WIN_POINT0..SHORT_WIN_POINT1], | |
396 | &self.ew_buf[SHORT_WIN_POINT0..SHORT_WIN_POINT1], | |
397 | &delay[SHORT_WIN_POINT0..SHORT_WIN_POINT1], AVC_WIN_SHORT); | |
398 | for i in SHORT_WIN_POINT1..COEFFS { | |
399 | dst[i] = self.ew_buf[i]; | |
400 | } | |
401 | *delay = [0.0; COEFFS]; | |
402 | for i in 0..SHORT_WIN_POINT1 { | |
403 | delay[i] = self.ew_buf[i]; | |
404 | } | |
405 | }, | |
406 | 5 => { | |
407 | for i in 0..SHORT_WIN_POINT0 { | |
408 | dst[i] = delay[i]; | |
409 | } | |
410 | synth512(&mut self.dsp, coeffs, &mut self.ew_buf, &mut self.tmp, self.is_40khz); | |
411 | self.imdct_mid.imdct(&coeffs[512..], &mut self.ew_buf[512..]); | |
412 | let tmp = &mut self.ew_buf[512..1024]; | |
413 | tmp.reverse(); | |
414 | let tmp = &mut self.ew_buf[0..1024]; | |
415 | tmp.reverse(); | |
416 | overlap(&mut dst[SHORT_WIN_POINT0..SHORT_WIN_POINT1], | |
417 | &self.ew_buf[SHORT_WIN_POINT0..SHORT_WIN_POINT1], | |
418 | &delay[SHORT_WIN_POINT0..SHORT_WIN_POINT1], AVC_WIN_SHORT); | |
419 | for i in SHORT_WIN_POINT1..COEFFS { | |
420 | dst[i] = self.ew_buf[i]; | |
421 | } | |
422 | *delay = [0.0; COEFFS]; | |
423 | for i in 0..SHORT_WIN_POINT1 { | |
424 | delay[i] = self.ew_buf[i]; | |
425 | } | |
426 | }, | |
427 | 6 => { | |
428 | for i in 0..SHORT_WIN_POINT0 { | |
429 | dst[i] = delay[i]; | |
430 | } | |
431 | self.imdct_mid.imdct(coeffs, &mut self.ew_buf); | |
432 | let tmp = &mut self.ew_buf[0..512]; | |
433 | tmp.reverse(); | |
434 | synth512(&mut self.dsp, &coeffs[512..], &mut self.ew_buf[512..], &mut self.tmp, self.is_40khz); | |
435 | let tmp = &mut self.ew_buf[0..1024]; | |
436 | tmp.reverse(); | |
437 | overlap(&mut dst[SHORT_WIN_POINT0..SHORT_WIN_POINT1], | |
438 | &self.ew_buf[SHORT_WIN_POINT0..SHORT_WIN_POINT1], | |
439 | &delay[SHORT_WIN_POINT0..SHORT_WIN_POINT1], AVC_WIN_SHORT); | |
440 | for i in SHORT_WIN_POINT1..COEFFS { | |
441 | dst[i] = self.ew_buf[i]; | |
442 | } | |
443 | *delay = [0.0; COEFFS]; | |
444 | for i in 0..SHORT_WIN_POINT1 { | |
445 | delay[i] = self.ew_buf[i]; | |
446 | } | |
447 | }, | |
448 | 7 => { | |
449 | for i in 0..SHORT_WIN_POINT0 { | |
450 | dst[i] = delay[i]; | |
451 | } | |
452 | self.imdct_long.imdct(coeffs, &mut self.tmp); | |
453 | overlap(&mut dst[SHORT_WIN_POINT0..SHORT_WIN_POINT1], | |
454 | &self.tmp[SHORT_WIN_POINT0..SHORT_WIN_POINT1], | |
455 | &delay[SHORT_WIN_POINT0..SHORT_WIN_POINT1], AVC_WIN_SHORT); | |
456 | for i in SHORT_WIN_POINT1..COEFFS { | |
457 | dst[i] = self.tmp[i]; | |
458 | } | |
459 | *delay = [0.0; COEFFS]; | |
460 | for i in 0..SHORT_WIN_POINT1 { | |
461 | delay[i] = self.ew_buf[i]; | |
462 | } | |
463 | }, | |
464 | _ => unreachable!(), | |
465 | }; | |
466 | } | |
467 | } | |
468 | ||
469 | fn overlap(dst: &mut [f32], src: &[f32], delay: &[f32], win: &[f32]) { | |
470 | for (i, ((out, s), d)) in dst.iter_mut().zip(src.iter()).zip(delay.iter()).enumerate() { | |
471 | *out = *s * win[i] + *d * win[win.len() - 1 - i]; | |
472 | } | |
473 | } | |
474 | ||
475 | #[derive(Clone,Copy)] | |
476 | struct SynthParams { | |
477 | p0: usize, | |
478 | p1: usize, | |
479 | idx: usize | |
480 | } | |
481 | ||
482 | macro_rules! synth_step0_template { | |
483 | ($name:ident, $tab: ident, $size: expr) => { | |
484 | fn $name(src: &[f32], dst: &mut [f32], step: usize, off: usize, sp: &SynthParams) { | |
485 | for i in 0..step { | |
486 | for j in 0..sp.p0 { | |
487 | dst[i] += ((src[j] as f64) * $tab[sp.idx][j][i]) as f32; | |
488 | } | |
489 | } | |
490 | for i in 0..step { | |
491 | for j in 0..sp.p1 { | |
492 | dst[$size - step + i] += ((src[sp.p0 + off + j] as f64) * $tab[sp.idx][sp.p0 + j][i]) as f32; | |
493 | } | |
494 | } | |
495 | } | |
496 | } | |
497 | } | |
498 | ||
499 | synth_step0_template!(synth_step0_10, AVC_SYNTH_TABS10, 10); | |
500 | synth_step0_template!(synth_step0_20, AVC_SYNTH_TABS20, 20); | |
501 | synth_step0_template!(synth_step0_40, AVC_SYNTH_TABS40, 40); | |
502 | synth_step0_template!(synth_step0_84, AVC_SYNTH_TABS84, 84); | |
503 | ||
504 | fn synth_step1(src: &[f32], dst: &mut [f32], size: usize, stride: usize, step: usize, off: usize, mut p0: usize, win: &[f64]) | |
505 | { | |
506 | let mut pos = step - 1; | |
507 | for _ in 0..off { | |
508 | let scale = src[p0] as f64; | |
509 | p0 += 1; | |
510 | pos &= size - 1; | |
511 | for i in 0..pos.min(step) { | |
512 | dst[pos - i] += (scale * win[i]) as f32; | |
513 | } | |
514 | pos += stride; | |
515 | } | |
516 | } | |
517 | ||
518 | fn zero_some(buf: &mut [FFTComplex], p0: usize, p1: usize) | |
519 | { | |
520 | for el in buf.iter_mut().take(p0/2) { | |
521 | *el = FFTC_ZERO; | |
522 | } | |
523 | if (p0 & 1) != 0 { | |
524 | buf[p0 / 2].re = 0.0; | |
525 | } | |
526 | let len = buf.len(); | |
527 | let dst = &mut buf[len - p1/2..]; | |
528 | for el in dst.iter_mut() { | |
529 | *el = FFTC_ZERO; | |
530 | } | |
531 | if (p1 & 1) != 0 { | |
532 | buf[len - p1/2 - 1].im = 0.0; | |
533 | } | |
534 | } | |
535 | ||
536 | fn merge_bands(src: &[FFTComplex], dst: &mut [FFTComplex], size: usize) { | |
537 | let step = if size == 512 { 2 } else { 4 }; | |
538 | let (s01, s23) = src.split_at(size / 2); | |
539 | let (src0, src1) = s01.split_at(size / 4); | |
540 | let (src2, src3) = s23.split_at(size / 4); | |
541 | let (t00, t01) = AVC_SYNTH_TAB0.split_at(512); | |
542 | let (t10, t11) = AVC_SYNTH_TAB1.split_at(512); | |
543 | let (t20, t21) = AVC_SYNTH_TAB2.split_at(512); | |
544 | let (t30, t31) = AVC_SYNTH_TAB3.split_at(512); | |
545 | let hsize = size / 2; | |
546 | let qsize = size / 4; | |
547 | let osize = size / 8; | |
548 | dst[0].re = src0[0].re * t00[0] + src1[0].re * t10[0] + src2[0].re * t20[0] + src3[0].re * t30[0]; | |
549 | dst[0].im = src0[0].re * t00[1] + src1[0].re * t10[1] + src2[0].re * t20[1] + src3[0].re * t30[1]; | |
550 | dst[qsize].re = src0[0].im * t00[256] + src1[0].im * t10[256] + | |
551 | src2[0].im * t20[256] + src3[0].im * t30[256]; | |
552 | dst[qsize].im = src0[0].im * t00[257] + src1[0].im * t10[257] + | |
553 | src2[0].im * t20[257] + src3[0].im * t30[257]; | |
554 | for i in 1..qsize { | |
555 | let s0 = src0[i]; | |
556 | let s1 = src1[i]; | |
557 | let s2 = src2[i]; | |
558 | let s3 = src3[i]; | |
559 | let t0 = s0 * FFTComplex { re: t00[i * step + 0], im: t00[i * step + 1] } + | |
560 | s1 * FFTComplex { re: t10[i * step + 0], im: t10[i * step + 1] } + | |
561 | s2 * FFTComplex { re: t20[i * step + 0], im: t20[i * step + 1] } + | |
562 | s3 * FFTComplex { re: t30[i * step + 0], im: t30[i * step + 1] }; | |
563 | let t1 = s0 * FFTComplex { re: t01[i * step + 0], im: t01[i * step + 1] } + | |
564 | s1 * FFTComplex { re: t11[i * step + 0], im: t11[i * step + 1] } + | |
565 | s2 * FFTComplex { re: t21[i * step + 0], im: t21[i * step + 1] } + | |
566 | s3 * FFTComplex { re: t31[i * step + 0], im: t31[i * step + 1] }; | |
567 | dst[i] = t0; | |
568 | dst[qsize + i] = t1; | |
569 | } | |
570 | ||
571 | dst[hsize].re = src0[0].im * t01[256] + src1[0].im * t11[256] + | |
572 | src2[0].im * t21[256] + src3[0].im * t31[256]; | |
573 | dst[hsize].im = src0[0].im * t01[257] + src1[0].im * t11[257] + | |
574 | src2[0].im * t21[257] + src3[0].im * t31[257]; | |
575 | dst[hsize + qsize].re = src0[0].re * t01[256] + src1[0].re * t11[256] + | |
576 | src2[0].re * t21[256] + src3[0].re * t31[256]; | |
577 | dst[hsize + qsize].im = src0[0].re * t01[257] + src1[0].re * t11[257] + | |
578 | src2[0].re * t21[257] + src3[0].re * t31[257]; | |
579 | for i in 1..osize { | |
580 | let s0 = !src0[qsize - i]; | |
581 | let s1 = !src1[qsize - i]; | |
582 | let s2 = !src2[qsize - i]; | |
583 | let s3 = !src3[qsize - i]; | |
584 | let t0 = s0 * FFTComplex { re: t00[256 + i * step + 0], im: t00[256 + i * step + 1] } + | |
585 | s1 * FFTComplex { re: t10[256 + i * step + 0], im: t10[256 + i * step + 1] } + | |
586 | s2 * FFTComplex { re: t20[256 + i * step + 0], im: t20[256 + i * step + 1] } + | |
587 | s3 * FFTComplex { re: t30[256 + i * step + 0], im: t30[256 + i * step + 1] }; | |
588 | let t1 = s0 * FFTComplex { re: t01[256 + i * step + 0], im: t01[256 + i * step + 1] } + | |
589 | s1 * FFTComplex { re: t11[256 + i * step + 0], im: t11[256 + i * step + 1] } + | |
590 | s2 * FFTComplex { re: t21[256 + i * step + 0], im: t21[256 + i * step + 1] } + | |
591 | s3 * FFTComplex { re: t31[256 + i * step + 0], im: t31[256 + i * step + 1] }; | |
592 | dst[hsize + i] = !t0; | |
593 | dst[hsize + qsize + i] = !t1; | |
594 | } | |
595 | } | |
596 | ||
597 | const SPARAMS84: [SynthParams; 4] = [ | |
598 | SynthParams { p0: 16, p1: 4, idx: 0 }, | |
599 | SynthParams { p0: 16, p1: 4, idx: 1 }, | |
600 | SynthParams { p0: 13, p1: 7, idx: 2 }, | |
601 | SynthParams { p0: 15, p1: 5, idx: 3 } ]; | |
602 | ||
603 | fn synth1024(dsp: &mut SynthDSP, src: &[f32], dst: &mut [f32], tmpbuf: &mut [f32; COEFFS * 2], is_40khz: bool) { | |
604 | for el in tmpbuf.iter_mut() { | |
605 | *el = 0.0; | |
606 | } | |
607 | let (tmp, tmp2) = tmpbuf.split_at_mut(COEFFS); | |
608 | for el in dsp.synth_tmp.iter_mut() { *el = FFTC_ZERO; } | |
609 | for el in dsp.synth_out.iter_mut() { *el = FFTC_ZERO; } | |
610 | ||
611 | let (size, step, stride) = (32, 20, 2); | |
612 | let off = (size - step) / stride + 1; | |
613 | let mut sparam0 = SynthParams { p0: 5, p1: 4, idx: 0 }; | |
614 | let mut sparam1 = SynthParams { p0: 4, p1: 5, idx: 1 }; | |
615 | for (i, dst) in tmp.chunks_mut(size).take(8).enumerate() { | |
616 | let coeffs = &src[i * 32..]; | |
617 | synth_step0_20(coeffs, dst, step, off, &sparam0); | |
618 | synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, &AVC_TAB20[sparam0.idx]); | |
619 | let coeffs = &src[i * 32 + 16..]; | |
620 | synth_step0_20(coeffs, dst, step, off, &sparam1); | |
621 | synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, &AVC_TAB20[sparam1.idx]); | |
622 | std::mem::swap(&mut sparam0, &mut sparam1); | |
623 | } | |
624 | ||
625 | let (size, step, stride) = (64, 40, 2); | |
626 | let off = (size - step) / stride + 1; | |
627 | let mut sparam0 = SynthParams { p0: 11, p1: 8, idx: 0 }; | |
628 | let mut sparam1 = SynthParams { p0: 8, p1: 11, idx: 1 }; | |
629 | let nchunks = if !is_40khz { 8 } else { 12 }; | |
630 | for (i, dst) in tmp2.chunks_mut(size).take(nchunks).enumerate() { | |
631 | let coeffs = if i < 4 { &tmp[i * 64..] } else { &src[i * 64 + 32..] }; | |
632 | synth_step0_40(coeffs, dst, step, off, &sparam0); | |
633 | synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, &AVC_TAB40[sparam0.idx]); | |
634 | let coeffs = if i < 4 { &tmp[i * 64 + 32..] } else { &src[i * 64 + 32..] }; | |
635 | synth_step0_40(coeffs, dst, step, off, &sparam1); | |
636 | synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, &AVC_TAB40[sparam1.idx]); | |
637 | std::mem::swap(&mut sparam0, &mut sparam1); | |
638 | } | |
639 | ||
640 | for el in tmp.iter_mut().take(128) { | |
641 | *el = 0.0; | |
642 | } | |
643 | let (size, step, stride) = (256, 84, 4); | |
644 | let off = (size - step) / stride + 1; | |
645 | for i in 0..4 { | |
646 | let coeffs = &tmp2[i * 64..]; | |
647 | synth_step0_84(coeffs, tmp, step, off, &SPARAMS84[i]); | |
648 | synth_step1(coeffs, tmp, size, stride, step, off, SPARAMS84[i].p0, &AVC_TAB84[i]); | |
649 | } | |
650 | for i in 0..4 { | |
651 | let coeffs = if i < 2 && is_40khz { &tmp2[256 + i * 64..] } else { &src[256 + i * 64..] }; | |
652 | let dst = &mut tmp[256..]; | |
653 | synth_step0_84(coeffs, dst, step, off, &SPARAMS84[3 - i]); | |
654 | synth_step1(coeffs, dst, size, stride, step, off, SPARAMS84[3 - i].p0, &AVC_TAB84[3 - i]); | |
655 | } | |
656 | ||
657 | if !is_40khz { | |
658 | let (size, step, stride) = (256, 40, 2); | |
659 | let off = (size - step) / stride + 1; | |
660 | let sparams = [ SynthParams { p0: 11, p1: 8, idx: 0 }, | |
661 | SynthParams { p0: 8, p1: 11, idx: 1 } ]; | |
662 | for i in 0..2 { | |
663 | let coeffs = &src[512 + i * 128..]; | |
664 | let dst = &mut tmp[512..]; | |
665 | synth_step0_40(coeffs, dst, step, off, &sparams[i]); | |
666 | synth_step1(coeffs, dst, size, stride, step, off, sparams[i].p0, &AVC_TAB40[i]); | |
667 | } | |
668 | } else { | |
669 | let (size, step, stride) = (256, 84, 4); | |
670 | let off = (size - step) / stride + 1; | |
671 | for i in 0..4 { | |
672 | let coeffs = &src[512 + i * 64..]; | |
673 | let dst = &mut tmp[512..]; | |
674 | synth_step0_84(coeffs, dst, step, off, &SPARAMS84[i]); | |
675 | synth_step1(coeffs, dst, size, stride, step, off, SPARAMS84[i].p0, &AVC_TAB84[i]); | |
676 | } | |
677 | } | |
678 | ||
679 | for (i, s) in tmp.chunks(2).take(768/2).enumerate() { | |
680 | dsp.synth_tmp[i] = FFTComplex { re: s[0], im: s[1] }; | |
681 | } | |
682 | for i in (768..1024).step_by(2) { | |
683 | dsp.synth_tmp[i >> 1] = FFTComplex { re: src[i], im: src[i + 1] }; | |
684 | } | |
685 | { | |
686 | let dst = &mut tmp[768..1024]; | |
687 | dst.copy_from_slice(&src[768..1024]); | |
688 | } | |
689 | for (i, chunk) in dsp.synth_tmp.chunks_exact_mut(128).enumerate() { | |
690 | zero_some(chunk, SPARAMS84[i].p0, SPARAMS84[i].p1); | |
691 | } | |
692 | for chunk in dsp.synth_tmp.chunks_exact_mut(128) { | |
693 | dsp.irdft128.do_rdft_inplace(chunk); | |
694 | for el in chunk.iter_mut() { *el = el.scale(1.0/128.0f32); } | |
695 | } | |
696 | merge_bands(&dsp.synth_tmp, &mut dsp.synth_out, 512); | |
697 | dsp.rdft512.do_rdft_inplace(&mut dsp.synth_out); | |
698 | for (out, src) in dst.chunks_exact_mut(2).zip(dsp.synth_out.iter()) { | |
699 | out[0] = src.re * (1.0 / 1024.0f32.sqrt()); | |
700 | out[1] = src.im * (1.0 / 1024.0f32.sqrt()); | |
701 | } | |
702 | let (size, step, stride) = (1024, 84, 4); | |
703 | let off = (size - step) / stride + 1; | |
704 | for i in 0..4 { | |
705 | let src = &tmp[256 * i..]; | |
706 | synth_step0_84(src, dst, step, off, &SPARAMS84[i]); | |
707 | } | |
708 | } | |
709 | fn synth512(dsp: &mut SynthDSP, src: &[f32], dst: &mut [f32], tmpbuf: &mut [f32; COEFFS * 2], is_40khz: bool) { | |
710 | for el in tmpbuf.iter_mut() { | |
711 | *el = 0.0; | |
712 | } | |
713 | let (tmp, tmp2) = tmpbuf.split_at_mut(COEFFS); | |
714 | ||
715 | let (size, step, stride) = (16, 10, 2); | |
716 | let off = (size - step) / stride + 1; | |
717 | let mut sparam0 = SynthParams { p0: 1, p1: 3, idx: 0 }; | |
718 | let mut sparam1 = SynthParams { p0: 3, p1: 1, idx: 1 }; | |
719 | let mut tab_ptr0: &[f64] = AVC_TAB10_0; | |
720 | let mut tab_ptr1: &[f64] = AVC_TAB10_1; | |
721 | for (i, dst) in tmp.chunks_mut(size).take(8).enumerate() { | |
722 | let coeffs = &src[i * 16..]; | |
723 | synth_step0_10(coeffs, dst, step, off, &sparam0); | |
724 | synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, tab_ptr0); | |
725 | let coeffs = &src[i * 16 + 8..]; | |
726 | synth_step0_10(coeffs, dst, step, off, &sparam1); | |
727 | synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, tab_ptr1); | |
728 | std::mem::swap(&mut sparam0, &mut sparam1); | |
729 | std::mem::swap(&mut tab_ptr0, &mut tab_ptr1); | |
730 | } | |
731 | ||
732 | let (size, step, stride) = (32, 20, 2); | |
733 | let off = (size - step) / stride + 1; | |
734 | let mut sparam0 = SynthParams { p0: 5, p1: 4, idx: 0 }; | |
735 | let mut sparam1 = SynthParams { p0: 4, p1: 5, idx: 1 }; | |
736 | let nchunks = if !is_40khz { 8 } else { 12 }; | |
737 | for (i, dst) in tmp2.chunks_mut(size).take(nchunks).enumerate() { | |
738 | let coeffs = if i < 4 { &tmp[i * 32..] } else { &src[i * 32..] }; | |
739 | synth_step0_20(coeffs, dst, step, off, &sparam0); | |
740 | synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, &AVC_TAB20[sparam0.idx]); | |
741 | let coeffs = if i < 4 { &tmp[i * 32 + 16..] } else { &src[i * 32 + 16..] }; | |
742 | synth_step0_20(coeffs, dst, step, off, &sparam1); | |
743 | synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, &AVC_TAB20[sparam1.idx]); | |
744 | std::mem::swap(&mut sparam0, &mut sparam1); | |
745 | } | |
746 | ||
747 | for el in tmp.iter_mut().take(64) { | |
748 | *el = 0.0; | |
749 | } | |
750 | let (size, step, stride) = (128, 84, 4); | |
751 | let off = (size - step) / stride + 1; | |
752 | for i in 0..4 { | |
753 | let coeffs = &tmp2[i * 32..]; | |
754 | synth_step0_84(coeffs, tmp, step, off, &SPARAMS84[i]); | |
755 | synth_step1(coeffs, tmp, size, stride, step, off, SPARAMS84[i].p0, &AVC_TAB84[i]); | |
756 | } | |
757 | for i in 0..4 { | |
758 | let coeffs = if i < 2 && is_40khz { &tmp2[128 + i * 32..] } else { &src[128 + i * 32..] }; | |
759 | let dst = &mut tmp[128..]; | |
760 | synth_step0_84(coeffs, dst, step, off, &SPARAMS84[3 - i]); | |
761 | synth_step1(coeffs, dst, size, stride, step, off, SPARAMS84[3 - i].p0, &AVC_TAB84[3 - i]); | |
762 | } | |
763 | ||
764 | if !is_40khz { | |
765 | let (size, step, stride) = (128, 40, 2); | |
766 | let off = (size - step) / stride + 1; | |
767 | let sparams = [ SynthParams { p0: 11, p1: 8, idx: 0 }, | |
768 | SynthParams { p0: 8, p1: 11, idx: 1 } ]; | |
769 | for i in 0..2 { | |
770 | let coeffs = &src[256 + i * 64..]; | |
771 | let dst = &mut tmp[256..]; | |
772 | synth_step0_40(coeffs, dst, step, off, &sparams[i]); | |
773 | synth_step1(coeffs, dst, size, stride, step, off, sparams[i].p0, &AVC_TAB40[i]); | |
774 | } | |
775 | } else { | |
776 | let (size, step, stride) = (128, 84, 4); | |
777 | let off = (size - step) / stride + 1; | |
778 | for i in 0..4 { | |
779 | let coeffs = &src[256 + i * 32..]; | |
780 | let dst = &mut tmp[256..]; | |
781 | synth_step0_84(coeffs, dst, step, off, &SPARAMS84[i]); | |
782 | synth_step1(coeffs, dst, size, stride, step, off, SPARAMS84[i].p0, &AVC_TAB84[i]); | |
783 | } | |
784 | } | |
785 | ||
786 | for (i, s) in tmp.chunks(2).take(384/2).enumerate() { | |
787 | dsp.synth_tmp[i] = FFTComplex { re: s[0], im: s[1] }; | |
788 | } | |
789 | for i in (384..512).step_by(2) { | |
790 | dsp.synth_tmp[i >> 1] = FFTComplex { re: src[i], im: src[i + 1] }; | |
791 | } | |
792 | { | |
793 | let dst = &mut tmp[384..512]; | |
794 | dst.copy_from_slice(&src[384..512]); | |
795 | } | |
796 | for (i, chunk) in dsp.synth_tmp.chunks_exact_mut(128).enumerate() { | |
797 | zero_some(chunk, SPARAMS84[i].p0, SPARAMS84[i].p1); | |
798 | } | |
799 | for chunk in dsp.synth_tmp.chunks_exact_mut(64) { | |
800 | dsp.irdft64.do_rdft_inplace(chunk); | |
801 | } | |
802 | merge_bands(&dsp.synth_tmp, &mut dsp.synth_out, 256); | |
803 | dsp.rdft256.do_rdft_inplace(&mut dsp.synth_out); | |
804 | for (out, src) in dst.chunks_exact_mut(2).zip(dsp.synth_out.iter()).take(256) { | |
805 | out[0] = src.re * (1.0 / 512.0f32 / 1024.0f32); | |
806 | out[1] = src.im * (1.0 / 512.0f32 / 1024.0f32); | |
807 | } | |
808 | let (size, step, stride) = (512, 84, 4); | |
809 | let off = (size - step) / stride + 1; | |
810 | for i in 0..4 { | |
811 | let src = &tmp[128 * i..]; | |
812 | synth_step0_84(src, dst, step, off, &SPARAMS84[i]); | |
813 | } | |
814 | } | |
815 | ||
816 | fn decode_band_types(br: &mut BitReader, cbs: &mut [u8; MAX_BANDS], winfo: &WinInfo) -> DecoderResult<()> { | |
817 | let bits_per_sect = if winfo.is_long { 5 } else { 3 }; | |
818 | let esc_val = (1 << bits_per_sect) - 1; | |
819 | let tot_bands = winfo.get_tot_bands(); | |
820 | ||
821 | let mut cur_band = 0; | |
822 | while cur_band < tot_bands { | |
823 | let codebook = br.read(4)? as u8; | |
824 | let mut run = 1; | |
825 | loop { | |
826 | let run_add = br.read(bits_per_sect)? as usize; | |
827 | run += run_add; | |
828 | if run_add != esc_val { break; } | |
829 | } | |
830 | validate!(cur_band + run <= tot_bands); | |
831 | for _ in 0..run { | |
832 | cbs[cur_band] = codebook; | |
833 | cur_band += 1; | |
834 | } | |
835 | } | |
836 | Ok(()) | |
837 | } | |
838 | ||
839 | fn dequant(val: i16, qtab: &[f32; QTAB_SIZE], scale: f32) -> f32 { | |
840 | if val >= 0 { | |
841 | qtab[val as usize] * scale | |
842 | } else { | |
843 | -qtab[val.abs() as usize] * scale | |
844 | } | |
845 | } | |
846 | ||
847 | fn decode_quads(br: &mut BitReader, cb: &Codebook<u16>, qtab: &[f32; QTAB_SIZE], scale: f32, dst: &mut [f32]) -> DecoderResult<()> { | |
848 | for quad in dst.chunks_exact_mut(4) { | |
849 | let mut idx = br.read_cb(cb)? as i16; | |
850 | for el in quad.iter_mut() { | |
851 | *el = dequant(idx >> 12, qtab, scale); | |
852 | idx <<= 4; | |
853 | } | |
854 | } | |
855 | Ok(()) | |
856 | } | |
857 | ||
858 | fn decode_esc_val(br: &mut BitReader, qtab: &[f32; QTAB_SIZE], scale: f32, sign: bool) -> DecoderResult<f32> { | |
859 | let pfx = br.read_code(UintCodeType::LimitedOnes(24))? + 4; | |
860 | let add = br.read(pfx as u8)? as usize; | |
861 | let val = (1 << pfx) + add; | |
862 | let fval = scale * if val < qtab.len() { | |
863 | qtab[val] | |
864 | } else { | |
865 | (val as f32) * (val as f32).sqrt() | |
866 | }; | |
867 | if !sign { | |
868 | Ok(fval) | |
869 | } else { | |
870 | Ok(-fval) | |
871 | } | |
872 | } | |
873 | ||
874 | fn decode_pairs(br: &mut BitReader, cb: &Codebook<u16>, qtab: &[f32; QTAB_SIZE], scale: f32, dst: &mut [f32], is_esc: bool) -> DecoderResult<()> { | |
875 | for pair in dst.chunks_exact_mut(2) { | |
876 | let idx = br.read_cb(cb)? as i16; | |
877 | let idx0 = idx >> 8; | |
878 | let idx1 = (idx << 8) >> 8; | |
879 | if !is_esc || (idx0.abs() < 16) { | |
880 | pair[0] = dequant(idx0, qtab, scale); | |
881 | } else { | |
882 | pair[0] = decode_esc_val(br, qtab, scale, idx0 < 0)?; | |
883 | } | |
884 | if !is_esc || (idx1.abs() < 16) { | |
885 | pair[1] = dequant(idx1, qtab, scale); | |
886 | } else { | |
887 | pair[1] = decode_esc_val(br, qtab, scale, idx1 < 0)?; | |
888 | } | |
889 | } | |
890 | Ok(()) | |
891 | } | |
892 | ||
893 | impl NADecoder for AVCDecoder { | |
894 | fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { | |
895 | if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() { | |
896 | self.chmap = match ainfo.get_channels() { | |
897 | 1 => { NAChannelMap::from_str("C").unwrap() }, | |
898 | 2 => { NAChannelMap::from_str("L,R").unwrap() }, | |
899 | _ => { return Err(DecoderError::InvalidData); }, | |
900 | }; | |
901 | let srate = ainfo.get_sample_rate(); | |
902 | self.ainfo = NAAudioInfo::new(srate, ainfo.get_channels(), | |
903 | SND_F32P_FORMAT, COEFFS); | |
904 | self.info = info.replace_info(NACodecTypeInfo::Audio(self.ainfo)); | |
905 | self.channels = ainfo.get_channels() as usize; | |
906 | self.is_40khz = srate <= 40000; | |
907 | ||
908 | self.win_long = if (srate > 24000) && ((srate > 32000) || (self.channels == 2)) { | |
909 | AVC_WIN_LONG_32K | |
910 | } else { | |
911 | AVC_WIN_LONG_24K | |
912 | }; | |
913 | ||
914 | Ok(()) | |
915 | } else { | |
916 | Err(DecoderError::InvalidData) | |
917 | } | |
918 | } | |
919 | fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> { | |
920 | let info = pkt.get_stream().get_info(); | |
921 | validate!(info.get_properties().is_audio()); | |
922 | let src = pkt.get_buffer(); | |
923 | ||
924 | let abuf; | |
925 | ||
926 | if self.version == 500 { | |
927 | abuf = alloc_audio_buffer(self.ainfo, COEFFS, self.chmap.clone())?; | |
928 | let mut adata = abuf.get_abuf_f32().unwrap(); | |
929 | let mut br = BitReader::new(src.as_slice(), src.len(), BitReaderMode::BE); | |
930 | self.decode_frame(&mut br, &mut adata, 0)?; | |
931 | } else { | |
932 | let mut offsets: Vec<usize> = Vec::new(); | |
933 | let mut sizes: Vec<usize> = Vec::new(); | |
934 | ||
935 | let mut cur_off = 0; | |
936 | while cur_off + 2 < src.len() { | |
937 | let sz = read_u16le(&src[cur_off..])? as usize; | |
938 | validate!(sz > 0); | |
939 | validate!(cur_off + sz + 2 <= src.len()); | |
940 | offsets.push(cur_off + 2); | |
941 | sizes.push(sz); | |
942 | cur_off += sz + 2; | |
943 | } | |
944 | ||
945 | validate!(!sizes.is_empty()); | |
946 | ||
947 | abuf = alloc_audio_buffer(self.ainfo, COEFFS * sizes.len(), self.chmap.clone())?; | |
948 | let mut adata = abuf.get_abuf_f32().unwrap(); | |
949 | let mut aoffset = 0; | |
950 | for (o, s) in offsets.iter().zip(sizes.iter()) { | |
951 | let mut br = BitReader::new(&src[*o..], *s, BitReaderMode::BE); | |
952 | self.decode_frame(&mut br, &mut adata, aoffset)?; | |
953 | aoffset += COEFFS; | |
954 | } | |
955 | } | |
956 | ||
957 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), abuf); | |
958 | frm.set_keyframe(true); | |
959 | Ok(frm.into_ref()) | |
960 | } | |
961 | } | |
962 | ||
963 | pub fn get_decoder_500() -> Box<dyn NADecoder> { | |
964 | Box::new(AVCDecoder::new(500)) | |
965 | } | |
966 | ||
967 | pub fn get_decoder_501() -> Box<dyn NADecoder> { | |
968 | Box::new(AVCDecoder::new(501)) | |
969 | } | |
970 | ||
971 | #[cfg(test)] | |
972 | mod test { | |
973 | use nihav_core::codecs::RegisteredDecoders; | |
974 | use nihav_core::demuxers::RegisteredDemuxers; | |
975 | use nihav_core::test::dec_video::*; | |
976 | use crate::codecs::duck_register_all_codecs; | |
977 | use nihav_commonfmt::demuxers::generic_register_all_demuxers; | |
978 | #[test] | |
979 | fn test_avc() { | |
980 | let mut dmx_reg = RegisteredDemuxers::new(); | |
981 | generic_register_all_demuxers(&mut dmx_reg); | |
982 | let mut dec_reg = RegisteredDecoders::new(); | |
983 | duck_register_all_codecs(&mut dec_reg); | |
984 | ||
985 | //let file = "assets/Duck/Cell-140.vp5"; | |
986 | //let file = "assets/Duck/Chocolat-500.vp5"; | |
987 | let file = "assets/Duck/potter-500.vp7"; | |
988 | test_decode_audio("avi", file, Some(1500), "avc", &dmx_reg, &dec_reg); | |
989 | panic!("end"); | |
990 | } | |
991 | } | |
992 | ||
993 | const AVC_WINFO_LONG: WinInfo = WinInfo { | |
994 | is_long: true, | |
995 | bands: &[ | |
996 | 4, 8, 12, 16, 20, 24, 28, 32, | |
997 | 36, 40, 48, 56, 64, 72, 80, 88, | |
998 | 96, 108, 120, 132, 144, 156, 172, 188, | |
999 | 204, 224, 244, 264, 288, 312, 340, 368, | |
1000 | 400, 432, 464, 496, 528, 560, 592, 624, | |
1001 | 656, 688, 720, 752, 784, 816, 848, 880, 1024 | |
1002 | ], | |
1003 | }; | |
1004 | ||
1005 | const AVC_WINFO_SHORT: WinInfo = WinInfo { | |
1006 | is_long: false, | |
1007 | bands: &[ 4, 8, 12, 16, 20, 24, 32, 40, 48, 56, 68, 80, 108, 128 ], | |
1008 | }; | |
1009 | ||
1010 | const AVC_WINFO_40K_MODE4: WinInfo = WinInfo { | |
1011 | is_long: true, | |
1012 | bands: &[ | |
1013 | 8, 16, 24, 32, 40, 48, 56, 64, | |
1014 | 72, 80, 88, 96, 104, 112, 120, 128, | |
1015 | 144, 160, 176, 192, 208, 224, 240, 256, | |
1016 | 264, 272, 280, 288, 296, 304, 312, 320, | |
1017 | 328, 336, 344, 352, 360, 368, 376, 384, | |
1018 | 400, 416, 432, 448, 464, 480, 496, 512, | |
1019 | 520, 528, 536, 544, 552, 560, 568, 576, | |
1020 | 584, 592, 600, 608, 616, 624, 632, 640, | |
1021 | 648, 656, 664, 672, 680, 688, 696, 704, | |
1022 | 712, 720, 728, 736, 744, 752, 760, 768, | |
1023 | 800, 832, 864, 896, 928, 960, 992, 1024 | |
1024 | ], | |
1025 | }; | |
1026 | const AVC_WINFO_40K_MODE5: WinInfo = WinInfo { | |
1027 | is_long: true, | |
1028 | bands: &[ | |
1029 | 8, 16, 24, 32, 40, 48, 56, 64, | |
1030 | 80, 96, 112, 128, 136, 144, 152, 160, | |
1031 | 168, 176, 184, 192, 208, 224, 240, 256, | |
1032 | 264, 272, 280, 288, 296, 304, 312, 320, | |
1033 | 328, 336, 344, 352, 360, 368, 376, 384, | |
1034 | 416, 448, 480, 512, 516, 520, 524, 528, | |
1035 | 532, 536, 540, 548, 556, 568, 580, 592, | |
1036 | 608, 624, 640, 656, 672, 688, 704, 720, | |
1037 | 736, 752, 768, 784, 800, 816, 832, 848, | |
1038 | 864, 880, 896, 912, 928, 944, 1024 | |
1039 | ], | |
1040 | }; | |
1041 | const AVC_WINFO_40K_MODE6: WinInfo = WinInfo { | |
1042 | is_long: true, | |
1043 | bands: &[ | |
1044 | 4, 8, 12, 16, 20, 24, 28, 36, | |
1045 | 44, 56, 68, 80, 96, 112, 128, 144, | |
1046 | 160, 176, 192, 208, 224, 240, 256, 272, | |
1047 | 288, 304, 320, 336, 352, 368, 384, 400, | |
1048 | 416, 432, 512, 520, 528, 536, 544, 552, | |
1049 | 560, 568, 576, 592, 608, 624, 640, 648, | |
1050 | 656, 664, 672, 680, 688, 696, 704, 720, | |
1051 | 736, 752, 768, 776, 784, 792, 800, 808, | |
1052 | 816, 824, 832, 840, 848, 856, 864, 872, | |
1053 | 880, 888, 896, 928, 960, 992, 1024 | |
1054 | ], | |
1055 | }; | |
1056 | ||
1057 | const AVC_WINFO_44K_MODE4: WinInfo = WinInfo { | |
1058 | is_long: true, | |
1059 | bands: &[ | |
1060 | 8, 16, 24, 32, 40, 48, 56, 64, | |
1061 | 72, 80, 88, 96, 104, 112, 120, 128, | |
1062 | 136, 144, 152, 160, 168, 176, 184, 192, | |
1063 | 200, 208, 216, 224, 232, 240, 248, 256, | |
1064 | 264, 272, 280, 288, 296, 304, 312, 320, | |
1065 | 328, 336, 344, 352, 360, 368, 376, 384, | |
1066 | 392, 400, 408, 416, 424, 432, 440, 448, | |
1067 | 456, 464, 472, 480, 488, 496, 504, 512, | |
1068 | 528, 544, 560, 576, 592, 608, 624, 640, | |
1069 | 672, 704, 736, 768, 832, 896, 960, 1024 | |
1070 | ], | |
1071 | }; | |
1072 | const AVC_WINFO_44K_MODE5: WinInfo = WinInfo { | |
1073 | is_long: true, | |
1074 | bands: &[ | |
1075 | 8, 16, 24, 32, 40, 48, 56, 64, | |
1076 | 72, 80, 88, 96, 104, 112, 120, 128, | |
1077 | 136, 144, 152, 160, 168, 176, 184, 192, | |
1078 | 200, 208, 216, 224, 232, 240, 248, 256, | |
1079 | 272, 288, 304, 320, 352, 384, 448, 512, | |
1080 | 516, 520, 524, 528, 532, 536, 540, 548, | |
1081 | 556, 568, 580, 592, 608, 624, 640, 656, | |
1082 | 672, 688, 704, 720, 736, 752, 768, 784, | |
1083 | 800, 816, 832, 848, 864, 880, 896, 912, | |
1084 | 928, 944, 1024 | |
1085 | ], | |
1086 | }; | |
1087 | const AVC_WINFO_44K_MODE6: WinInfo = WinInfo { | |
1088 | is_long: true, | |
1089 | bands: &[ | |
1090 | 4, 8, 12, 16, 20, 24, 28, 36, | |
1091 | 44, 56, 68, 80, 96, 112, 128, 144, | |
1092 | 160, 176, 192, 208, 224, 240, 256, 272, | |
1093 | 288, 304, 320, 336, 352, 368, 384, 400, | |
1094 | 416, 432, 512, 520, 528, 536, 544, 552, | |
1095 | 560, 568, 576, 584, 592, 600, 608, 616, | |
1096 | 624, 632, 640, 648, 656, 664, 672, 680, | |
1097 | 688, 696, 704, 712, 720, 728, 736, 744, | |
1098 | 752, 760, 768, 784, 800, 816, 832, 864, | |
1099 | 896, 960, 1024 | |
1100 | ], | |
1101 | }; |