]> git.nihav.org Git - nihav.git/blob - nihav-duck/src/codecs/on2avc.rs
29ead41d3c1ec9ebf7dd3106fef30495d0e6bf23
[nihav.git] / nihav-duck / src / codecs / on2avc.rs
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_codec_support::dsp::fft::*;
8 use nihav_codec_support::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 use_generic: bool,
148
149 qtab: [f32; QTAB_SIZE],
150 scale_tab: [f32; 128],
151 }
152
153 impl AVCDecoder {
154 fn new(version: i32) -> Self {
155 let mut qtab = [0.0f32; QTAB_SIZE];
156 for (i, el) in qtab.iter_mut().enumerate() {
157 *el = (i as f32) * (i as f32).sqrt();
158 }
159 let mut scale_tab = [0.0f32; 128];
160 for (i, el) in scale_tab.iter_mut().enumerate() {
161 let pow = 10.0f32.powf((i as f32) * 0.1);
162 if i < 20 {
163 *el = (pow * 16.0).ceil() / 32.0;
164 } else {
165 *el = (pow * 0.5).ceil();
166 }
167 *el /= 32768.0;
168 }
169
170 AVCDecoder {
171 chmap: NAChannelMap::new(),
172 ainfo: NAAudioInfo::new(0, 0, SND_F32P_FORMAT, 0),
173 info: NACodecInfo::new_dummy(),
174 dsp: SynthDSP::new(),
175 version,
176 codebooks: Codebooks::new(),
177 channels: 0,
178 is_40khz: false,
179 win_long: AVC_WIN_LONG_32K,
180
181 prev_win: 0,
182 cur_win: 0,
183 winfo: &AVC_WINFO_LONG,
184 windows: 0,
185 win_grp: [false; 8],
186 ms_present: false,
187 ms_info: [false; MAX_BANDS],
188 cbs: [0; MAX_BANDS],
189 scales: [0; MAX_BANDS],
190
191 imdct_long: IMDCT::new(1024 * 2, true),
192 imdct_mid: IMDCT::new(512 * 2, true),
193 imdct_short: IMDCT::new(128 * 2, true),
194 coeffs: [[0.0; COEFFS]; 2],
195 delay: [[0.0; COEFFS]; 2],
196 tmp: [0.0; COEFFS * 2],
197 ew_buf: [0.0; 1152],
198 use_generic: true,
199
200 qtab,
201 scale_tab,
202 }
203 }
204 fn decode_channel(&mut self, br: &mut BitReader, chno: usize) -> DecoderResult<()> {
205 let coeffs = &mut self.coeffs[chno];
206
207 decode_band_types(br, &mut self.cbs, self.winfo)?;
208 // band scales
209 let bands = self.winfo.bands.len();
210 let mut cur_band = 0;
211 let mut scale = 0;
212 let mut first = true;
213 for wg in self.win_grp.iter().take(self.windows) {
214 if *wg {
215 for _ in 0..bands {
216 if self.cbs[cur_band] == 0 {
217 self.scales[cur_band] = 0;
218 } else {
219 if first {
220 scale = br.read(7)? as i16;
221 first = false
222 } else {
223 scale += i16::from(br.read_cb(&self.codebooks.scale_cb)?);
224 validate!((scale >= 0) && (scale < 128));
225 }
226 self.scales[cur_band] = scale as u8;
227 }
228 cur_band += 1;
229 }
230 } else {
231 for _ in 0..bands {
232 self.scales[cur_band] = self.scales[cur_band - bands];
233 cur_band += 1;
234 }
235 }
236 }
237 // coefficients
238 let mut cur_band = 0;
239 let mut idx = 0;
240 *coeffs = [0.0; COEFFS];
241 for _ in 0..self.windows {
242 let mut band_start = 0;
243 for band_end in self.winfo.bands.iter() {
244 let band_size = *band_end - band_start;
245 let cb_idx = self.cbs[cur_band];
246 if cb_idx > 0 {
247 let cb = &self.codebooks.spec_cb[(cb_idx - 1) as usize];
248 let scale = self.scale_tab[self.scales[cur_band] as usize];
249 let dst = &mut coeffs[idx..][..band_size];
250 if cb_idx < 9 {
251 decode_quads(br, cb, &self.qtab, scale, dst)?;
252 } else {
253 decode_pairs(br, cb, &self.qtab, scale, dst, cb_idx == 15)?;
254 }
255 }
256
257 band_start = *band_end;
258 idx += band_size;
259 cur_band += 1;
260 }
261 }
262 Ok(())
263 }
264 fn decode_frame(&mut self, br: &mut BitReader, adata: &mut NAAudioBuffer<f32>, offset: usize) -> DecoderResult<()> {
265 let enh_flag = br.read_bool()?;
266 validate!(!enh_flag);
267 self.prev_win = self.cur_win;
268 self.cur_win = br.read(3)? as u8;
269 self.winfo = match self.cur_win {
270 3 => &AVC_WINFO_SHORT,
271 4 => if self.is_40khz { &AVC_WINFO_40K_MODE4 } else { &AVC_WINFO_44K_MODE4 },
272 5 => if self.is_40khz { &AVC_WINFO_40K_MODE5 } else { &AVC_WINFO_44K_MODE5 },
273 6 => if self.is_40khz { &AVC_WINFO_40K_MODE6 } else { &AVC_WINFO_44K_MODE6 },
274 _ => &AVC_WINFO_LONG,
275 };
276 self.windows = if self.winfo.is_long { 1 } else { 8 };
277 let bands = self.winfo.bands.len();
278 self.win_grp[0] = true;
279 for el in self.win_grp.iter_mut().skip(1).take(self.windows - 1) {
280 *el = !br.read_bool()?;
281 }
282
283 self.ms_present = br.read_bool()?;
284 if self.ms_present {
285 validate!(self.channels == 2);
286 let mut cur_band = 0;
287 for wg in self.win_grp.iter().take(self.windows) {
288 if *wg {
289 for _ in 0..bands {
290 self.ms_info[cur_band] = br.read_bool()?;
291 cur_band += 1;
292 }
293 } else {
294 for _ in 0..bands {
295 self.ms_info[cur_band] = self.ms_info[cur_band - bands];
296 cur_band += 1;
297 }
298 }
299 }
300 }
301 for ch in 0..self.channels {
302 self.decode_channel(br, ch)?;
303 }
304
305 if (self.channels == 2) && self.ms_present {
306 let mut idx = 0;
307 let mut cur_band = 0;
308 for _ in 0..self.windows {
309 let mut band_start = 0;
310 for band_end in self.winfo.bands.iter() {
311 let band_size = *band_end - band_start;
312 if self.ms_info[cur_band] {
313 for i in 0..band_size {
314 let l = self.coeffs[0][idx + i];
315 let r = self.coeffs[1][idx + i];
316 self.coeffs[0][idx + i] = l + r;
317 self.coeffs[1][idx + i] = l - r;
318 }
319 }
320 cur_band += 1;
321 idx += band_size;
322 band_start = *band_end;
323 }
324 }
325 }
326
327 for ch in 0..self.channels {
328 let off = adata.get_offset(ch) + offset;
329 let output = adata.get_data_mut().unwrap();
330 self.synth_channel(ch, &mut output[off..][..COEFFS]);
331 }
332 Ok(())
333 }
334 #[allow(clippy::cyclomatic_complexity)]
335 fn synth_channel(&mut self, chno: usize, dst: &mut [f32]) {
336 let coeffs = &mut self.coeffs[chno];
337 let delay = &mut self.delay[chno];
338
339 match self.cur_win {
340 0 | 1 => {
341 self.imdct_long.imdct_half(coeffs, &mut self.tmp);
342 overlap_half(dst, &self.tmp, delay, self.win_long);
343 (&mut delay[0..COEFFS/2]).copy_from_slice(&self.tmp[COEFFS/2..COEFFS]);
344 for i in COEFFS/2..COEFFS {
345 delay[i] = delay[COEFFS - 1 - i];
346 }
347 },
348 2 | 7 => {
349 self.imdct_long.imdct(coeffs, &mut self.tmp);
350 for i in 0..SHORT_WIN_POINT0 {
351 dst[i] = delay[i];
352 }
353 overlap(&mut dst[SHORT_WIN_POINT0..SHORT_WIN_POINT1],
354 &self.tmp[SHORT_WIN_POINT0..SHORT_WIN_POINT1],
355 &delay[SHORT_WIN_POINT0..SHORT_WIN_POINT1], AVC_WIN_SHORT);
356 for i in SHORT_WIN_POINT1..COEFFS {
357 dst[i] = self.tmp[i - SHORT_WIN_POINT1 + 128];
358 }
359 delay.copy_from_slice(&self.tmp[COEFFS..]);
360 },
361 3 => {
362 for (ain, aout) in coeffs.chunks(128).zip(self.tmp.chunks_mut(256)) {
363 self.imdct_short.imdct(ain, aout);
364 }
365 self.ew_buf = [0.0; 1152];
366 for i in 0..128 {
367 self.ew_buf[i] = self.tmp[i];
368 }
369 for w in 0..7 {
370 overlap(&mut self.ew_buf[(w + 1) * 128..][..128],
371 &self.tmp[(w + 1) * 256..][..128],
372 &self.tmp[w * 256 + 128..][..128],
373 AVC_WIN_SHORT);
374 }
375 for i in 0..128 {
376 self.ew_buf[1024 + i] = self.tmp[7 * 256 + 128 + i];
377 }
378 for i in 0..SHORT_WIN_POINT0 {
379 dst[i] = delay[i];
380 }
381 overlap(&mut dst[SHORT_WIN_POINT0..SHORT_WIN_POINT1],
382 &self.ew_buf[0..128],
383 &delay[SHORT_WIN_POINT0..SHORT_WIN_POINT1], AVC_WIN_SHORT);
384 for i in SHORT_WIN_POINT1..COEFFS {
385 dst[i] = self.ew_buf[i - SHORT_WIN_POINT1 + 128];
386 }
387 for i in 0..SHORT_WIN_POINT1 {
388 delay[i] = self.ew_buf[SHORT_WIN_POINT1 + i];
389 }
390 for i in SHORT_WIN_POINT1..COEFFS {
391 delay[i] = 0.0;
392 }
393 },
394 4 => {
395 for i in 0..SHORT_WIN_POINT0 {
396 dst[i] = delay[i];
397 }
398 if !self.use_generic {
399 synth1024(&mut self.dsp, coeffs, &mut self.ew_buf, &mut self.tmp, self.is_40khz);
400 } else {
401 synth_generic(coeffs, &mut self.ew_buf, &mut self.tmp, self.is_40khz, 1024);
402 }
403 overlap_half(&mut dst[SHORT_WIN_POINT0..SHORT_WIN_POINT1],
404 &self.ew_buf[0..64],
405 &delay[SHORT_WIN_POINT0..SHORT_WIN_POINT1], AVC_WIN_SHORT);
406 for i in SHORT_WIN_POINT1..COEFFS {
407 dst[i] = self.ew_buf[i - SHORT_WIN_POINT1 + 64];
408 }
409 for i in 0..COEFFS/2 {
410 delay[i] = self.ew_buf[COEFFS/2 + i];
411 }
412 for i in COEFFS/2..COEFFS {
413 delay[i] = delay[COEFFS - 1 - i];
414 }
415 },
416 5 => {
417 for i in 0..SHORT_WIN_POINT0 {
418 dst[i] = delay[i];
419 }
420 if !self.use_generic {
421 synth512(&mut self.dsp, coeffs, &mut self.ew_buf, &mut self.tmp, self.is_40khz);
422 } else {
423 synth_generic(coeffs, &mut self.ew_buf, &mut self.tmp, self.is_40khz, 512);
424 }
425 self.imdct_mid.imdct(&coeffs[512..], &mut self.ew_buf[512..]);
426 overlap(&mut dst[SHORT_WIN_POINT0..SHORT_WIN_POINT1],
427 &self.ew_buf[SHORT_WIN_POINT0..SHORT_WIN_POINT1],
428 &delay[SHORT_WIN_POINT0..SHORT_WIN_POINT1], AVC_WIN_SHORT);
429 for i in SHORT_WIN_POINT1..COEFFS {
430 dst[i] = self.ew_buf[i];
431 }
432 *delay = [0.0; COEFFS];
433 for i in 0..SHORT_WIN_POINT1 {
434 delay[i] = self.ew_buf[i];
435 }
436 },
437 6 => {
438 for i in 0..SHORT_WIN_POINT0 {
439 dst[i] = delay[i];
440 }
441 self.imdct_mid.imdct(coeffs, &mut self.ew_buf);
442 if !self.use_generic {
443 synth512(&mut self.dsp, &coeffs[512..], &mut self.ew_buf[512..], &mut self.tmp, self.is_40khz);
444 } else {
445 synth_generic(&coeffs[512..], &mut self.ew_buf[512..], &mut self.tmp, self.is_40khz, 512);
446 }
447 overlap(&mut dst[SHORT_WIN_POINT0..SHORT_WIN_POINT1],
448 &self.ew_buf[SHORT_WIN_POINT0..SHORT_WIN_POINT1],
449 &delay[SHORT_WIN_POINT0..SHORT_WIN_POINT1], AVC_WIN_SHORT);
450 for i in SHORT_WIN_POINT1..COEFFS {
451 dst[i] = self.ew_buf[i];
452 }
453 *delay = [0.0; COEFFS];
454 for i in 0..SHORT_WIN_POINT1 {
455 delay[i] = self.ew_buf[i];
456 }
457 },
458 _ => unreachable!(),
459 };
460 }
461 }
462
463 fn overlap(dst: &mut [f32], src: &[f32], delay: &[f32], win: &[f32]) {
464 for (i, ((out, s), d)) in dst.iter_mut().zip(src.iter()).zip(delay.iter()).enumerate() {
465 *out = *s * win[i] + *d * win[win.len() - 1 - i];
466 }
467 }
468
469 fn overlap_half(dst: &mut [f32], src: &[f32], delay: &[f32], win: &[f32]) {
470 let n = win.len();
471 for i in 0..n/2 {
472 let ii = n - 1 - i;
473 let c = src [n/2 - 1 - i];
474 let d = delay[i];
475 let w0 = win[i];
476 let w1 = win[ii];
477 dst[i] = d * w1 - c * w0;
478 dst[ii] = d * w0 + c * w1;
479 }
480 }
481
482 #[derive(Clone,Copy)]
483 struct SynthParams {
484 p0: usize,
485 p1: usize,
486 idx: usize
487 }
488
489 macro_rules! synth_step0_template {
490 ($name:ident, $tab: ident, $size: expr) => {
491 fn $name(src: &[f32], dst: &mut [f32], step: usize, off: usize, sp: &SynthParams) {
492 for i in 0..step {
493 for j in 0..sp.p0 {
494 dst[i] += (f64::from(src[j]) * $tab[sp.idx][j][i]) as f32;
495 }
496 }
497 for i in 0..step {
498 for j in 0..sp.p1 {
499 dst[$size - step + i] += (f64::from(src[sp.p0 + off + j]) * $tab[sp.idx][sp.p0 + j][i]) as f32;
500 }
501 }
502 }
503 }
504 }
505
506 synth_step0_template!(synth_step0_10, AVC_SYNTH_TABS10, 10);
507 synth_step0_template!(synth_step0_20, AVC_SYNTH_TABS20, 20);
508 synth_step0_template!(synth_step0_40, AVC_SYNTH_TABS40, 40);
509 synth_step0_template!(synth_step0_84, AVC_SYNTH_TABS84, 84);
510
511 fn synth_step1(src: &[f32], dst: &mut [f32], size: usize, stride: usize, step: usize, off: usize, mut p0: usize, win: &[f64])
512 {
513 let mut pos = step - 1;
514 for _ in 0..off {
515 let scale = f64::from(src[p0]);
516 p0 += 1;
517 pos &= size - 1;
518 for i in 0..pos.min(step) {
519 dst[pos - i] += (scale * win[i]) as f32;
520 }
521 pos += stride;
522 }
523 }
524
525 fn zero_some(buf: &mut [FFTComplex], p0: usize, p1: usize)
526 {
527 for el in buf.iter_mut().take(p0/2) {
528 *el = FFTC_ZERO;
529 }
530 if (p0 & 1) != 0 {
531 buf[p0 / 2].re = 0.0;
532 }
533 let len = buf.len();
534 let dst = &mut buf[len - p1/2..];
535 for el in dst.iter_mut() {
536 *el = FFTC_ZERO;
537 }
538 if (p1 & 1) != 0 {
539 buf[len - p1/2 - 1].im = 0.0;
540 }
541 }
542
543 fn merge_bands(src: &[FFTComplex], dst: &mut [FFTComplex], size: usize) {
544 let step = if size == 512 { 2 } else { 4 };
545 let (s01, s23) = src.split_at(size / 2);
546 let (src0, src1) = s01.split_at(size / 4);
547 let (src2, src3) = s23.split_at(size / 4);
548 let (t00, t01) = AVC_SYNTH_TAB0.split_at(512);
549 let (t10, t11) = AVC_SYNTH_TAB1.split_at(512);
550 let (t20, t21) = AVC_SYNTH_TAB2.split_at(512);
551 let (t30, t31) = AVC_SYNTH_TAB3.split_at(512);
552 let hsize = size / 2;
553 let qsize = size / 4;
554 let osize = size / 8;
555 dst[0].re = src0[0].re * t00[0] + src1[0].re * t10[0] + src2[0].re * t20[0] + src3[0].re * t30[0];
556 dst[0].im = src0[0].re * t00[1] + src1[0].re * t10[1] + src2[0].re * t20[1] + src3[0].re * t30[1];
557 dst[qsize].re = src0[0].im * t00[256] + src1[0].im * t10[256] +
558 src2[0].im * t20[256] + src3[0].im * t30[256];
559 dst[qsize].im = src0[0].im * t00[257] + src1[0].im * t10[257] +
560 src2[0].im * t20[257] + src3[0].im * t30[257];
561 for i in 1..qsize {
562 let s0 = src0[i];
563 let s1 = src1[i];
564 let s2 = src2[i];
565 let s3 = src3[i];
566 let t0 = s0 * FFTComplex { re: t00[i * step + 0], im: t00[i * step + 1] } +
567 s1 * FFTComplex { re: t10[i * step + 0], im: t10[i * step + 1] } +
568 s2 * FFTComplex { re: t20[i * step + 0], im: t20[i * step + 1] } +
569 s3 * FFTComplex { re: t30[i * step + 0], im: t30[i * step + 1] };
570 let t1 = s0 * FFTComplex { re: t01[i * step + 0], im: t01[i * step + 1] } +
571 s1 * FFTComplex { re: t11[i * step + 0], im: t11[i * step + 1] } +
572 s2 * FFTComplex { re: t21[i * step + 0], im: t21[i * step + 1] } +
573 s3 * FFTComplex { re: t31[i * step + 0], im: t31[i * step + 1] };
574 dst[i] = t0;
575 dst[qsize + i] = t1;
576 }
577
578 dst[hsize].re = src0[0].im * t01[256] + src1[0].im * t11[256] +
579 src2[0].im * t21[256] + src3[0].im * t31[256];
580 dst[hsize].im = src0[0].im * t01[257] + src1[0].im * t11[257] +
581 src2[0].im * t21[257] + src3[0].im * t31[257];
582 dst[hsize + qsize].re = src0[0].re * t01[256] + src1[0].re * t11[256] +
583 src2[0].re * t21[256] + src3[0].re * t31[256];
584 dst[hsize + qsize].im = src0[0].re * t01[257] + src1[0].re * t11[257] +
585 src2[0].re * t21[257] + src3[0].re * t31[257];
586 for i in 1..osize {
587 let s0 = !src0[qsize - i];
588 let s1 = !src1[qsize - i];
589 let s2 = !src2[qsize - i];
590 let s3 = !src3[qsize - i];
591 let t0 = s0 * FFTComplex { re: t00[256 + i * step + 0], im: t00[256 + i * step + 1] } +
592 s1 * FFTComplex { re: t10[256 + i * step + 0], im: t10[256 + i * step + 1] } +
593 s2 * FFTComplex { re: t20[256 + i * step + 0], im: t20[256 + i * step + 1] } +
594 s3 * FFTComplex { re: t30[256 + i * step + 0], im: t30[256 + i * step + 1] };
595 let t1 = s0 * FFTComplex { re: t01[256 + i * step + 0], im: t01[256 + i * step + 1] } +
596 s1 * FFTComplex { re: t11[256 + i * step + 0], im: t11[256 + i * step + 1] } +
597 s2 * FFTComplex { re: t21[256 + i * step + 0], im: t21[256 + i * step + 1] } +
598 s3 * FFTComplex { re: t31[256 + i * step + 0], im: t31[256 + i * step + 1] };
599 dst[hsize + i] = !t0;
600 dst[hsize + qsize + i] = !t1;
601 }
602 }
603
604 const SPARAMS10: [SynthParams; 2] = [
605 SynthParams { p0: 1, p1: 3, idx: 0 },
606 SynthParams { p0: 3, p1: 1, idx: 1 } ];
607 const SPARAMS20: [SynthParams; 2] = [
608 SynthParams { p0: 5, p1: 4, idx: 0 },
609 SynthParams { p0: 4, p1: 5, idx: 1 } ];
610 const SPARAMS40: [SynthParams; 2] = [
611 SynthParams { p0: 11, p1: 8, idx: 0 },
612 SynthParams { p0: 8, p1: 11, idx: 1 } ];
613 const SPARAMS84: [SynthParams; 4] = [
614 SynthParams { p0: 16, p1: 4, idx: 0 },
615 SynthParams { p0: 16, p1: 4, idx: 1 },
616 SynthParams { p0: 13, p1: 7, idx: 2 },
617 SynthParams { p0: 15, p1: 5, idx: 3 } ];
618
619 const MERGE_ORDER_44K: &[u8] = &[
620 4, 4, 2, 2, 0, 0, 2, 0, 0, 2, 2, 0, 0, 2, 0, 0, 2, 0, 0,
621 2, 0, 0, 4, 0, 0, 0, 0, 2, 0, 0, 0
622 ];
623 const MERGE_ORDER_40K: &[u8] = &[
624 4, 4, 2, 2, 0, 0, 2, 0, 0, 2, 2, 0, 0, 2, 0, 0, 2, 0, 0,
625 2, 0, 0, 4, 2, 0, 0, 2, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0
626 ];
627
628 fn synth_filter(src: &[f32], dst: &mut [f32], len: usize, step: usize, bands: usize, idx: usize)
629 {
630 let off = (len - step) / bands + 1;
631 let params = match step {
632 10 => &SPARAMS10[idx],
633 20 => &SPARAMS20[idx],
634 40 => &SPARAMS40[idx],
635 _ => &SPARAMS84[idx],
636 };
637
638 if step == 10 {
639 synth_step0_10(src, dst, step, off, params);
640 synth_step1(src, dst, len, bands, step, off, params.p0, &AVC_TAB10[idx]);
641 } else if step == 20 {
642 synth_step0_20(src, dst, step, off, params);
643 synth_step1(src, dst, len, bands, step, off, params.p0, &AVC_TAB20[idx]);
644 } else if step == 40 {
645 synth_step0_40(src, dst, step, off, params);
646 synth_step1(src, dst, len, bands, step, off, params.p0, &AVC_TAB40[idx]);
647 } else {
648 synth_step0_84(src, dst, step, off, params);
649 synth_step1(src, dst, len, bands, step, off, params.p0, &AVC_TAB84[idx]);
650 }
651 }
652
653 fn synth_recursive(dst: &mut [f32], tmp: &mut [f32], size: usize, order: &[u8], order_idx: &mut usize, dir: bool) {
654 let bands = order[*order_idx] as usize;
655 *order_idx += 1;
656 if bands == 0 { return; }
657 let sub_size = size / bands;
658 let mut sub_dir = false;
659 for (dst, tmp) in dst.chunks_mut(sub_size).take(bands).zip(tmp.chunks_mut(sub_size)) {
660 synth_recursive(dst, tmp, sub_size, order, order_idx, sub_dir);
661 sub_dir = !sub_dir;
662 }
663 for el in tmp.iter_mut().take(size) { *el = 0.0; }
664 let step = if bands == 2 {
665 if sub_size <= 20 {
666 10
667 } else if sub_size <= 40 {
668 20
669 } else {
670 40
671 }
672 } else {
673 84
674 };
675 for (i, src) in dst.chunks_mut(sub_size).take(bands).enumerate() {
676 let idx = if !dir { i } else { bands - 1 - i };
677 synth_filter(src, tmp, size, step, bands, idx);
678 }
679 (&mut dst[..size]).copy_from_slice(&tmp[..size]);
680 }
681
682 fn synth_generic(src: &[f32], dst: &mut [f32], tmpbuf: &mut [f32; COEFFS * 2], is_40khz: bool, size: usize) {
683 let order = if is_40khz { MERGE_ORDER_40K } else { MERGE_ORDER_44K };
684 (&mut dst[..size]).copy_from_slice(&src[..size]);
685 let mut order_idx = 0;
686 synth_recursive(dst, tmpbuf, size, order, &mut order_idx, false);
687 for el in dst.iter_mut().take(COEFFS) { *el *= 0.125; }
688 }
689
690 fn synth1024(dsp: &mut SynthDSP, src: &[f32], dst: &mut [f32], tmpbuf: &mut [f32; COEFFS * 2], is_40khz: bool) {
691 for el in tmpbuf.iter_mut() {
692 *el = 0.0;
693 }
694 let (tmp, tmp2) = tmpbuf.split_at_mut(COEFFS);
695 for el in dsp.synth_tmp.iter_mut() { *el = FFTC_ZERO; }
696 for el in dsp.synth_out.iter_mut() { *el = FFTC_ZERO; }
697
698 let (size, step, stride) = (32, 20, 2);
699 let off = (size - step) / stride + 1;
700 let mut sparam0 = SynthParams { p0: 5, p1: 4, idx: 0 };
701 let mut sparam1 = SynthParams { p0: 4, p1: 5, idx: 1 };
702 for (i, dst) in tmp.chunks_mut(size).take(8).enumerate() {
703 let coeffs = &src[i * 32..];
704 synth_step0_20(coeffs, dst, step, off, &sparam0);
705 synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, &AVC_TAB20[sparam0.idx]);
706 let coeffs = &src[i * 32 + 16..];
707 synth_step0_20(coeffs, dst, step, off, &sparam1);
708 synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, &AVC_TAB20[sparam1.idx]);
709 std::mem::swap(&mut sparam0, &mut sparam1);
710 }
711
712 let (size, step, stride) = (64, 40, 2);
713 let off = (size - step) / stride + 1;
714 let mut sparam0 = SynthParams { p0: 11, p1: 8, idx: 0 };
715 let mut sparam1 = SynthParams { p0: 8, p1: 11, idx: 1 };
716 let nchunks = if !is_40khz { 8 } else { 12 };
717 for (i, dst) in tmp2.chunks_mut(size).take(nchunks).enumerate() {
718 let coeffs = if i < 4 { &tmp[i * 64..] } else { &src[i * 64 + 32..] };
719 synth_step0_40(coeffs, dst, step, off, &sparam0);
720 synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, &AVC_TAB40[sparam0.idx]);
721 let coeffs = if i < 4 { &tmp[i * 64 + 32..] } else { &src[i * 64 + 32..] };
722 synth_step0_40(coeffs, dst, step, off, &sparam1);
723 synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, &AVC_TAB40[sparam1.idx]);
724 std::mem::swap(&mut sparam0, &mut sparam1);
725 }
726
727 for el in tmp.iter_mut().take(128) {
728 *el = 0.0;
729 }
730 let (size, step, stride) = (256, 84, 4);
731 let off = (size - step) / stride + 1;
732 for i in 0..4 {
733 let coeffs = &tmp2[i * 64..];
734 synth_step0_84(coeffs, tmp, step, off, &SPARAMS84[i]);
735 synth_step1(coeffs, tmp, size, stride, step, off, SPARAMS84[i].p0, &AVC_TAB84[i]);
736 }
737 for i in 0..4 {
738 let coeffs = if i < 2 && is_40khz { &tmp2[256 + i * 64..] } else { &src[256 + i * 64..] };
739 let dst = &mut tmp[256..];
740 synth_step0_84(coeffs, dst, step, off, &SPARAMS84[3 - i]);
741 synth_step1(coeffs, dst, size, stride, step, off, SPARAMS84[3 - i].p0, &AVC_TAB84[3 - i]);
742 }
743
744 if !is_40khz {
745 let (size, step, stride) = (256, 40, 2);
746 let off = (size - step) / stride + 1;
747 let sparams = [ SynthParams { p0: 11, p1: 8, idx: 0 },
748 SynthParams { p0: 8, p1: 11, idx: 1 } ];
749 for i in 0..2 {
750 let coeffs = &src[512 + i * 128..];
751 let dst = &mut tmp[512..];
752 synth_step0_40(coeffs, dst, step, off, &sparams[i]);
753 synth_step1(coeffs, dst, size, stride, step, off, sparams[i].p0, &AVC_TAB40[i]);
754 }
755 } else {
756 let (size, step, stride) = (256, 84, 4);
757 let off = (size - step) / stride + 1;
758 for i in 0..4 {
759 let coeffs = &src[512 + i * 64..];
760 let dst = &mut tmp[512..];
761 synth_step0_84(coeffs, dst, step, off, &SPARAMS84[i]);
762 synth_step1(coeffs, dst, size, stride, step, off, SPARAMS84[i].p0, &AVC_TAB84[i]);
763 }
764 }
765
766 for (i, s) in tmp.chunks(2).take(768/2).enumerate() {
767 dsp.synth_tmp[i] = FFTComplex { re: s[0], im: s[1] };
768 }
769 for i in (768..1024).step_by(2) {
770 dsp.synth_tmp[i >> 1] = FFTComplex { re: src[i], im: src[i + 1] };
771 }
772 {
773 let dst = &mut tmp[768..1024];
774 dst.copy_from_slice(&src[768..1024]);
775 }
776 for (i, chunk) in dsp.synth_tmp.chunks_exact_mut(128).enumerate() {
777 zero_some(chunk, SPARAMS84[i].p0, SPARAMS84[i].p1);
778 }
779 for chunk in dsp.synth_tmp.chunks_exact_mut(128) {
780 dsp.irdft128.do_rdft_inplace(chunk);
781 for el in chunk.iter_mut() { *el = el.scale(1.0/128.0f32); }
782 }
783 merge_bands(&dsp.synth_tmp, &mut dsp.synth_out, 512);
784 dsp.rdft512.do_rdft_inplace(&mut dsp.synth_out);
785 for (out, src) in dst.chunks_exact_mut(2).zip(dsp.synth_out.iter()) {
786 out[0] = src.re * (1.0 / 1024.0f32.sqrt());
787 out[1] = src.im * (1.0 / 1024.0f32.sqrt());
788 }
789 let (size, step, stride) = (1024, 84, 4);
790 let off = (size - step) / stride + 1;
791 for i in 0..4 {
792 let src = &tmp[256 * i..];
793 synth_step0_84(src, dst, step, off, &SPARAMS84[i]);
794 }
795 }
796 fn synth512(dsp: &mut SynthDSP, src: &[f32], dst: &mut [f32], tmpbuf: &mut [f32; COEFFS * 2], is_40khz: bool) {
797 for el in tmpbuf.iter_mut() {
798 *el = 0.0;
799 }
800 let (tmp, tmp2) = tmpbuf.split_at_mut(COEFFS);
801
802 let (size, step, stride) = (16, 10, 2);
803 let off = (size - step) / stride + 1;
804 let mut sparam0 = SynthParams { p0: 1, p1: 3, idx: 0 };
805 let mut sparam1 = SynthParams { p0: 3, p1: 1, idx: 1 };
806 let mut tab_ptr0: &[f64] = AVC_TAB10_0;
807 let mut tab_ptr1: &[f64] = AVC_TAB10_1;
808 for (i, dst) in tmp.chunks_mut(size).take(8).enumerate() {
809 let coeffs = &src[i * 16..];
810 synth_step0_10(coeffs, dst, step, off, &sparam0);
811 synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, tab_ptr0);
812 let coeffs = &src[i * 16 + 8..];
813 synth_step0_10(coeffs, dst, step, off, &sparam1);
814 synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, tab_ptr1);
815 std::mem::swap(&mut sparam0, &mut sparam1);
816 std::mem::swap(&mut tab_ptr0, &mut tab_ptr1);
817 }
818
819 let (size, step, stride) = (32, 20, 2);
820 let off = (size - step) / stride + 1;
821 let mut sparam0 = SynthParams { p0: 5, p1: 4, idx: 0 };
822 let mut sparam1 = SynthParams { p0: 4, p1: 5, idx: 1 };
823 let nchunks = if !is_40khz { 8 } else { 12 };
824 for (i, dst) in tmp2.chunks_mut(size).take(nchunks).enumerate() {
825 let coeffs = if i < 4 { &tmp[i * 32..] } else { &src[i * 32..] };
826 synth_step0_20(coeffs, dst, step, off, &sparam0);
827 synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, &AVC_TAB20[sparam0.idx]);
828 let coeffs = if i < 4 { &tmp[i * 32 + 16..] } else { &src[i * 32 + 16..] };
829 synth_step0_20(coeffs, dst, step, off, &sparam1);
830 synth_step1(coeffs, dst, size, stride, step, off, sparam0.p0, &AVC_TAB20[sparam1.idx]);
831 std::mem::swap(&mut sparam0, &mut sparam1);
832 }
833
834 for el in tmp.iter_mut().take(64) {
835 *el = 0.0;
836 }
837 let (size, step, stride) = (128, 84, 4);
838 let off = (size - step) / stride + 1;
839 for i in 0..4 {
840 let coeffs = &tmp2[i * 32..];
841 synth_step0_84(coeffs, tmp, step, off, &SPARAMS84[i]);
842 synth_step1(coeffs, tmp, size, stride, step, off, SPARAMS84[i].p0, &AVC_TAB84[i]);
843 }
844 for i in 0..4 {
845 let coeffs = if i < 2 && is_40khz { &tmp2[128 + i * 32..] } else { &src[128 + i * 32..] };
846 let dst = &mut tmp[128..];
847 synth_step0_84(coeffs, dst, step, off, &SPARAMS84[3 - i]);
848 synth_step1(coeffs, dst, size, stride, step, off, SPARAMS84[3 - i].p0, &AVC_TAB84[3 - i]);
849 }
850
851 if !is_40khz {
852 let (size, step, stride) = (128, 40, 2);
853 let off = (size - step) / stride + 1;
854 let sparams = [ SynthParams { p0: 11, p1: 8, idx: 0 },
855 SynthParams { p0: 8, p1: 11, idx: 1 } ];
856 for i in 0..2 {
857 let coeffs = &src[256 + i * 64..];
858 let dst = &mut tmp[256..];
859 synth_step0_40(coeffs, dst, step, off, &sparams[i]);
860 synth_step1(coeffs, dst, size, stride, step, off, sparams[i].p0, &AVC_TAB40[i]);
861 }
862 } else {
863 let (size, step, stride) = (128, 84, 4);
864 let off = (size - step) / stride + 1;
865 for i in 0..4 {
866 let coeffs = &src[256 + i * 32..];
867 let dst = &mut tmp[256..];
868 synth_step0_84(coeffs, dst, step, off, &SPARAMS84[i]);
869 synth_step1(coeffs, dst, size, stride, step, off, SPARAMS84[i].p0, &AVC_TAB84[i]);
870 }
871 }
872
873 for (i, s) in tmp.chunks(2).take(384/2).enumerate() {
874 dsp.synth_tmp[i] = FFTComplex { re: s[0], im: s[1] };
875 }
876 for i in (384..512).step_by(2) {
877 dsp.synth_tmp[i >> 1] = FFTComplex { re: src[i], im: src[i + 1] };
878 }
879 {
880 let dst = &mut tmp[384..512];
881 dst.copy_from_slice(&src[384..512]);
882 }
883 for (i, chunk) in dsp.synth_tmp.chunks_exact_mut(128).enumerate() {
884 zero_some(chunk, SPARAMS84[i].p0, SPARAMS84[i].p1);
885 }
886 for chunk in dsp.synth_tmp.chunks_exact_mut(64) {
887 dsp.irdft64.do_rdft_inplace(chunk);
888 }
889 merge_bands(&dsp.synth_tmp, &mut dsp.synth_out, 256);
890 dsp.rdft256.do_rdft_inplace(&mut dsp.synth_out);
891 for (out, src) in dst.chunks_exact_mut(2).zip(dsp.synth_out.iter()).take(256) {
892 out[0] = src.re * (1.0 / 512.0f32 / 1024.0f32);
893 out[1] = src.im * (1.0 / 512.0f32 / 1024.0f32);
894 }
895 let (size, step, stride) = (512, 84, 4);
896 let off = (size - step) / stride + 1;
897 for i in 0..4 {
898 let src = &tmp[128 * i..];
899 synth_step0_84(src, dst, step, off, &SPARAMS84[i]);
900 }
901 }
902
903 fn decode_band_types(br: &mut BitReader, cbs: &mut [u8; MAX_BANDS], winfo: &WinInfo) -> DecoderResult<()> {
904 let bits_per_sect = if winfo.is_long { 5 } else { 3 };
905 let esc_val = (1 << bits_per_sect) - 1;
906 let tot_bands = winfo.get_tot_bands();
907
908 let mut cur_band = 0;
909 while cur_band < tot_bands {
910 let codebook = br.read(4)? as u8;
911 let mut run = 1;
912 loop {
913 let run_add = br.read(bits_per_sect)? as usize;
914 run += run_add;
915 if run_add != esc_val { break; }
916 }
917 validate!(cur_band + run <= tot_bands);
918 for _ in 0..run {
919 cbs[cur_band] = codebook;
920 cur_band += 1;
921 }
922 }
923 Ok(())
924 }
925
926 fn dequant(val: i16, qtab: &[f32; QTAB_SIZE], scale: f32) -> f32 {
927 if val >= 0 {
928 qtab[val as usize] * scale
929 } else {
930 -qtab[val.abs() as usize] * scale
931 }
932 }
933
934 fn decode_quads(br: &mut BitReader, cb: &Codebook<u16>, qtab: &[f32; QTAB_SIZE], scale: f32, dst: &mut [f32]) -> DecoderResult<()> {
935 for quad in dst.chunks_exact_mut(4) {
936 let mut idx = br.read_cb(cb)? as i16;
937 for el in quad.iter_mut() {
938 *el = dequant(idx >> 12, qtab, scale);
939 idx <<= 4;
940 }
941 }
942 Ok(())
943 }
944
945 fn decode_esc_val(br: &mut BitReader, qtab: &[f32; QTAB_SIZE], scale: f32, sign: bool) -> DecoderResult<f32> {
946 let pfx = br.read_code(UintCodeType::LimitedOnes(24))? + 4;
947 let add = br.read(pfx as u8)? as usize;
948 let val = (1 << pfx) + add;
949 let fval = scale * if val < qtab.len() {
950 qtab[val]
951 } else {
952 (val as f32) * (val as f32).sqrt()
953 };
954 if !sign {
955 Ok(fval)
956 } else {
957 Ok(-fval)
958 }
959 }
960
961 fn decode_pairs(br: &mut BitReader, cb: &Codebook<u16>, qtab: &[f32; QTAB_SIZE], scale: f32, dst: &mut [f32], is_esc: bool) -> DecoderResult<()> {
962 for pair in dst.chunks_exact_mut(2) {
963 let idx = br.read_cb(cb)? as i16;
964 let idx0 = idx >> 8;
965 let idx1 = (idx << 8) >> 8;
966 if !is_esc || (idx0.abs() < 16) {
967 pair[0] = dequant(idx0, qtab, scale);
968 } else {
969 pair[0] = decode_esc_val(br, qtab, scale, idx0 < 0)?;
970 }
971 if !is_esc || (idx1.abs() < 16) {
972 pair[1] = dequant(idx1, qtab, scale);
973 } else {
974 pair[1] = decode_esc_val(br, qtab, scale, idx1 < 0)?;
975 }
976 }
977 Ok(())
978 }
979
980 impl NADecoder for AVCDecoder {
981 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
982 if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() {
983 self.chmap = match ainfo.get_channels() {
984 1 => { NAChannelMap::from_str("C").unwrap() },
985 2 => { NAChannelMap::from_str("L,R").unwrap() },
986 _ => { return Err(DecoderError::InvalidData); },
987 };
988 let srate = ainfo.get_sample_rate();
989 self.ainfo = NAAudioInfo::new(srate, ainfo.get_channels(),
990 SND_F32P_FORMAT, COEFFS);
991 self.info = info.replace_info(NACodecTypeInfo::Audio(self.ainfo));
992 self.channels = ainfo.get_channels() as usize;
993 self.is_40khz = srate <= 40000;
994
995 self.win_long = if (srate > 24000) && ((srate > 32000) || (self.channels == 2)) {
996 AVC_WIN_LONG_32K
997 } else {
998 AVC_WIN_LONG_24K
999 };
1000
1001 Ok(())
1002 } else {
1003 Err(DecoderError::InvalidData)
1004 }
1005 }
1006 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
1007 let info = pkt.get_stream().get_info();
1008 validate!(info.get_properties().is_audio());
1009 let src = pkt.get_buffer();
1010
1011 let abuf;
1012
1013 if self.version == 500 {
1014 abuf = alloc_audio_buffer(self.ainfo, COEFFS, self.chmap.clone())?;
1015 let mut adata = abuf.get_abuf_f32().unwrap();
1016 let mut br = BitReader::new(src.as_slice(), BitReaderMode::BE);
1017 self.decode_frame(&mut br, &mut adata, 0)?;
1018 } else {
1019 let mut offsets: Vec<usize> = Vec::new();
1020 let mut sizes: Vec<usize> = Vec::new();
1021
1022 let mut cur_off = 0;
1023 while cur_off + 2 < src.len() {
1024 let sz = read_u16le(&src[cur_off..])? as usize;
1025 validate!(sz > 0);
1026 validate!(cur_off + sz + 2 <= src.len());
1027 offsets.push(cur_off + 2);
1028 sizes.push(sz);
1029 cur_off += sz + 2;
1030 }
1031
1032 validate!(!sizes.is_empty());
1033
1034 abuf = alloc_audio_buffer(self.ainfo, COEFFS * sizes.len(), self.chmap.clone())?;
1035 let mut adata = abuf.get_abuf_f32().unwrap();
1036 let mut aoffset = 0;
1037 for (o, s) in offsets.iter().zip(sizes.iter()) {
1038 let mut br = BitReader::new(&src[*o..][..*s], BitReaderMode::BE);
1039 self.decode_frame(&mut br, &mut adata, aoffset)?;
1040 aoffset += COEFFS;
1041 }
1042 }
1043
1044 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), abuf);
1045 frm.set_keyframe(true);
1046 Ok(frm.into_ref())
1047 }
1048 fn flush(&mut self) {
1049 }
1050 }
1051
1052 impl NAOptionHandler for AVCDecoder {
1053 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
1054 fn set_options(&mut self, _options: &[NAOption]) { }
1055 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
1056 }
1057
1058 pub fn get_decoder_500() -> Box<dyn NADecoder + Send> {
1059 Box::new(AVCDecoder::new(500))
1060 }
1061
1062 pub fn get_decoder_501() -> Box<dyn NADecoder + Send> {
1063 Box::new(AVCDecoder::new(501))
1064 }
1065
1066 #[cfg(test)]
1067 mod test {
1068 use nihav_core::codecs::RegisteredDecoders;
1069 use nihav_core::demuxers::RegisteredDemuxers;
1070 use nihav_codec_support::test::dec_video::*;
1071 use crate::duck_register_all_decoders;
1072 use nihav_commonfmt::generic_register_all_demuxers;
1073 #[test]
1074 fn test_avc() {
1075 let mut dmx_reg = RegisteredDemuxers::new();
1076 generic_register_all_demuxers(&mut dmx_reg);
1077 let mut dec_reg = RegisteredDecoders::new();
1078 duck_register_all_decoders(&mut dec_reg);
1079
1080 //let file = "assets/Duck/Cell-140.vp5";
1081 //let file = "assets/Duck/Chocolat-500.vp5";
1082 let file = "assets/Duck/potter-500.vp7";
1083 test_decode_audio("avi", file, Some(1500), None/*Some("avc")*/, &dmx_reg, &dec_reg);
1084 }
1085 }
1086
1087 const AVC_WINFO_LONG: WinInfo = WinInfo {
1088 is_long: true,
1089 bands: &[
1090 4, 8, 12, 16, 20, 24, 28, 32,
1091 36, 40, 48, 56, 64, 72, 80, 88,
1092 96, 108, 120, 132, 144, 156, 172, 188,
1093 204, 224, 244, 264, 288, 312, 340, 368,
1094 400, 432, 464, 496, 528, 560, 592, 624,
1095 656, 688, 720, 752, 784, 816, 848, 880, 1024
1096 ],
1097 };
1098
1099 const AVC_WINFO_SHORT: WinInfo = WinInfo {
1100 is_long: false,
1101 bands: &[ 4, 8, 12, 16, 20, 24, 32, 40, 48, 56, 68, 80, 108, 128 ],
1102 };
1103
1104 const AVC_WINFO_40K_MODE4: WinInfo = WinInfo {
1105 is_long: true,
1106 bands: &[
1107 8, 16, 24, 32, 40, 48, 56, 64,
1108 72, 80, 88, 96, 104, 112, 120, 128,
1109 144, 160, 176, 192, 208, 224, 240, 256,
1110 264, 272, 280, 288, 296, 304, 312, 320,
1111 328, 336, 344, 352, 360, 368, 376, 384,
1112 400, 416, 432, 448, 464, 480, 496, 512,
1113 520, 528, 536, 544, 552, 560, 568, 576,
1114 584, 592, 600, 608, 616, 624, 632, 640,
1115 648, 656, 664, 672, 680, 688, 696, 704,
1116 712, 720, 728, 736, 744, 752, 760, 768,
1117 800, 832, 864, 896, 928, 960, 992, 1024
1118 ],
1119 };
1120 const AVC_WINFO_40K_MODE5: WinInfo = WinInfo {
1121 is_long: true,
1122 bands: &[
1123 8, 16, 24, 32, 40, 48, 56, 64,
1124 80, 96, 112, 128, 136, 144, 152, 160,
1125 168, 176, 184, 192, 208, 224, 240, 256,
1126 264, 272, 280, 288, 296, 304, 312, 320,
1127 328, 336, 344, 352, 360, 368, 376, 384,
1128 416, 448, 480, 512, 516, 520, 524, 528,
1129 532, 536, 540, 548, 556, 568, 580, 592,
1130 608, 624, 640, 656, 672, 688, 704, 720,
1131 736, 752, 768, 784, 800, 816, 832, 848,
1132 864, 880, 896, 912, 928, 944, 1024
1133 ],
1134 };
1135 const AVC_WINFO_40K_MODE6: WinInfo = WinInfo {
1136 is_long: true,
1137 bands: &[
1138 4, 8, 12, 16, 20, 24, 28, 36,
1139 44, 56, 68, 80, 96, 112, 128, 144,
1140 160, 176, 192, 208, 224, 240, 256, 272,
1141 288, 304, 320, 336, 352, 368, 384, 400,
1142 416, 432, 512, 520, 528, 536, 544, 552,
1143 560, 568, 576, 592, 608, 624, 640, 648,
1144 656, 664, 672, 680, 688, 696, 704, 720,
1145 736, 752, 768, 776, 784, 792, 800, 808,
1146 816, 824, 832, 840, 848, 856, 864, 872,
1147 880, 888, 896, 928, 960, 992, 1024
1148 ],
1149 };
1150
1151 const AVC_WINFO_44K_MODE4: WinInfo = WinInfo {
1152 is_long: true,
1153 bands: &[
1154 8, 16, 24, 32, 40, 48, 56, 64,
1155 72, 80, 88, 96, 104, 112, 120, 128,
1156 136, 144, 152, 160, 168, 176, 184, 192,
1157 200, 208, 216, 224, 232, 240, 248, 256,
1158 264, 272, 280, 288, 296, 304, 312, 320,
1159 328, 336, 344, 352, 360, 368, 376, 384,
1160 392, 400, 408, 416, 424, 432, 440, 448,
1161 456, 464, 472, 480, 488, 496, 504, 512,
1162 528, 544, 560, 576, 592, 608, 624, 640,
1163 672, 704, 736, 768, 832, 896, 960, 1024
1164 ],
1165 };
1166 const AVC_WINFO_44K_MODE5: WinInfo = WinInfo {
1167 is_long: true,
1168 bands: &[
1169 8, 16, 24, 32, 40, 48, 56, 64,
1170 72, 80, 88, 96, 104, 112, 120, 128,
1171 136, 144, 152, 160, 168, 176, 184, 192,
1172 200, 208, 216, 224, 232, 240, 248, 256,
1173 272, 288, 304, 320, 352, 384, 448, 512,
1174 516, 520, 524, 528, 532, 536, 540, 548,
1175 556, 568, 580, 592, 608, 624, 640, 656,
1176 672, 688, 704, 720, 736, 752, 768, 784,
1177 800, 816, 832, 848, 864, 880, 896, 912,
1178 928, 944, 1024
1179 ],
1180 };
1181 const AVC_WINFO_44K_MODE6: WinInfo = WinInfo {
1182 is_long: true,
1183 bands: &[
1184 4, 8, 12, 16, 20, 24, 28, 36,
1185 44, 56, 68, 80, 96, 112, 128, 144,
1186 160, 176, 192, 208, 224, 240, 256, 272,
1187 288, 304, 320, 336, 352, 368, 384, 400,
1188 416, 432, 512, 520, 528, 536, 544, 552,
1189 560, 568, 576, 584, 592, 600, 608, 616,
1190 624, 632, 640, 648, 656, 664, 672, 680,
1191 688, 696, 704, 712, 720, 728, 736, 744,
1192 752, 760, 768, 784, 800, 816, 832, 864,
1193 896, 960, 1024
1194 ],
1195 };