]> git.nihav.org Git - nihav.git/blame_incremental - nihav-mpeg/src/codecs/aac/mod.rs
aac: do not add 16 to large coefficients
[nihav.git] / nihav-mpeg / src / codecs / aac / mod.rs
... / ...
CommitLineData
1use nihav_core::formats::*;
2use nihav_core::frame::*;
3use nihav_core::codecs::*;
4use nihav_codec_support::dsp::mdct::IMDCT;
5use nihav_codec_support::dsp::window::*;
6use nihav_core::io::bitreader::*;
7use nihav_core::io::byteio::*;
8use std::str::FromStr;
9
10mod coeff_read;
11use coeff_read::*;
12mod info;
13use info::*;
14mod tables;
15use tables::*;
16mod tools;
17use tools::*;
18
19const MAX_WINDOWS: usize = 8;
20const MAX_SFBS: usize = 64;
21
22#[derive(Clone,Copy)]
23pub struct ICSInfo {
24 window_sequence: u8,
25 prev_window_sequence: u8,
26 window_shape: bool,
27 prev_window_shape: bool,
28 scale_factor_grouping: [bool; MAX_WINDOWS],
29 group_start: [usize; MAX_WINDOWS],
30 window_groups: usize,
31 num_windows: usize,
32 max_sfb: usize,
33 predictor_data: Option<LTPData>,
34 long_win: bool,
35}
36
37const ONLY_LONG_SEQUENCE: u8 = 0;
38const LONG_START_SEQUENCE: u8 = 1;
39const EIGHT_SHORT_SEQUENCE: u8 = 2;
40const LONG_STOP_SEQUENCE: u8 = 3;
41
42impl ICSInfo {
43 fn new() -> Self {
44 Self {
45 window_sequence: 0,
46 prev_window_sequence: 0,
47 window_shape: false,
48 prev_window_shape: false,
49 scale_factor_grouping: [false; MAX_WINDOWS],
50 group_start: [0; MAX_WINDOWS],
51 num_windows: 0,
52 window_groups: 0,
53 max_sfb: 0,
54 predictor_data: None,
55 long_win: true,
56 }
57 }
58 fn decode_ics_info(&mut self, br: &mut BitReader) -> DecoderResult<()> {
59 self.prev_window_sequence = self.window_sequence;
60 self.prev_window_shape = self.window_shape;
61 let ics_reserved_bit = br.read(1)?;
62 validate!(ics_reserved_bit == 0);
63 self.window_sequence = br.read(2)? as u8;
64 match self.prev_window_sequence {
65 ONLY_LONG_SEQUENCE | LONG_STOP_SEQUENCE => {
66 if (self.window_sequence != ONLY_LONG_SEQUENCE) &&
67 (self.window_sequence != LONG_START_SEQUENCE) {
68 println!("incorrect previous window");
69 }
70 },
71 LONG_START_SEQUENCE | EIGHT_SHORT_SEQUENCE => {
72 if (self.window_sequence != EIGHT_SHORT_SEQUENCE) &&
73 (self.window_sequence != LONG_STOP_SEQUENCE) {
74 println!("incorrect previous window");
75 }
76 },
77 _ => {},
78 };
79 self.window_shape = br.read_bool()?;
80 self.window_groups = 1;
81 if self.window_sequence == EIGHT_SHORT_SEQUENCE {
82 self.long_win = false;
83 self.num_windows = 8;
84 self.max_sfb = br.read(4)? as usize;
85 for i in 0..MAX_WINDOWS-1 {
86 self.scale_factor_grouping[i] = br.read_bool()?;
87 if !self.scale_factor_grouping[i] {
88 self.group_start[self.window_groups] = i + 1;
89 self.window_groups += 1;
90 }
91 }
92 } else {
93 self.long_win = true;
94 self.num_windows = 1;
95 self.max_sfb = br.read(6)? as usize;
96 self.predictor_data = LTPData::read(br)?;
97 }
98 Ok(())
99 }
100 fn get_group_start(&self, g: usize) -> usize {
101 if g == 0 {
102 0
103 } else if g >= self.window_groups {
104 if self.long_win { 1 } else { 8 }
105 } else {
106 self.group_start[g]
107 }
108 }
109}
110
111#[derive(Clone)]
112struct ICS {
113 global_gain: u8,
114 info: ICSInfo,
115 pulse_data: Option<PulseData>,
116 tns_data: Option<TNSData>,
117 gain_control: Option<GainControlData>,
118 sect_cb: [[u8; MAX_SFBS]; MAX_WINDOWS],
119 sect_len: [[usize; MAX_SFBS]; MAX_WINDOWS],
120 sfb_cb: [[u8; MAX_SFBS]; MAX_WINDOWS],
121 num_sec: [usize; MAX_WINDOWS],
122 scales: [[u8; MAX_SFBS]; MAX_WINDOWS],
123 sbinfo: GASubbandInfo,
124 coeffs: [f32; 1024],
125 delay: [f32; 1024],
126}
127
128const INTENSITY_SCALE_MIN: i16 = -155;
129impl ICS {
130 fn new(sbinfo: GASubbandInfo) -> Self {
131 Self {
132 global_gain: 0,
133 info: ICSInfo::new(),
134 pulse_data: None,
135 tns_data: None,
136 gain_control: None,
137 sect_cb: [[0; MAX_SFBS]; MAX_WINDOWS],
138 sect_len: [[0; MAX_SFBS]; MAX_WINDOWS],
139 sfb_cb: [[0; MAX_SFBS]; MAX_WINDOWS],
140 scales: [[0; MAX_SFBS]; MAX_WINDOWS],
141 num_sec: [0; MAX_WINDOWS],
142 sbinfo,
143 coeffs: [0.0; 1024],
144 delay: [0.0; 1024],
145 }
146 }
147 fn decode_section_data(&mut self, br: &mut BitReader, may_have_intensity: bool) -> DecoderResult<()> {
148 let sect_bits = if self.info.long_win { 5 } else { 3 };
149 let sect_esc_val = (1 << sect_bits) - 1;
150
151 for g in 0..self.info.window_groups {
152 let mut k = 0;
153 let mut l = 0;
154 while k < self.info.max_sfb {
155 self.sect_cb[g][l] = br.read(4)? as u8;
156 self.sect_len[g][l] = 0;
157 validate!(self.sect_cb[g][l] != RESERVED_HCB);
158 if ((self.sect_cb[g][l] == INTENSITY_HCB) || (self.sect_cb[g][l] == INTENSITY_HCB2)) && !may_have_intensity {
159 return Err(DecoderError::InvalidData);
160 }
161 loop {
162 let sect_len_incr = br.read(sect_bits)? as usize;
163 self.sect_len[g][l] += sect_len_incr;
164 if sect_len_incr < sect_esc_val { break; }
165 }
166 validate!(k + self.sect_len[g][l] <= self.info.max_sfb);
167 for _ in 0..self.sect_len[g][l] {
168 self.sfb_cb[g][k] = self.sect_cb[g][l];
169 k += 1;
170 }
171 l += 1;
172 }
173 self.num_sec[g] = l;
174 }
175 Ok(())
176 }
177 fn is_intensity(&self, g: usize, sfb: usize) -> bool {
178 (self.sfb_cb[g][sfb] == INTENSITY_HCB) || (self.sfb_cb[g][sfb] == INTENSITY_HCB2)
179 }
180 fn get_intensity_dir(&self, g: usize, sfb: usize) -> bool {
181 self.sfb_cb[g][sfb] == INTENSITY_HCB
182 }
183 fn decode_scale_factor_data(&mut self, br: &mut BitReader, codebooks: &Codebooks) -> DecoderResult<()> {
184 decode_scale_factor_data(br, &mut self.scales, self.global_gain, &self.info, &self.sfb_cb, codebooks)
185 }
186 fn get_band_start(&self, swb: usize) -> usize {
187 if self.info.long_win {
188 self.sbinfo.long_bands[swb]
189 } else {
190 self.sbinfo.short_bands[swb]
191 }
192 }
193 fn get_num_bands(&self) -> usize {
194 if self.info.long_win {
195 self.sbinfo.long_bands.len() - 1
196 } else {
197 self.sbinfo.short_bands.len() - 1
198 }
199 }
200 fn decode_spectrum(&mut self, br: &mut BitReader, codebooks: &Codebooks) -> DecoderResult<()> {
201 self.coeffs = [0.0; 1024];
202 decode_spectrum(br, &mut self.coeffs, &self.scales, &self.info, &self.sbinfo, &self.sfb_cb, codebooks)
203 }
204 fn place_pulses(&mut self) {
205 if let Some(ref pdata) = self.pulse_data {
206 if pdata.pulse_start_sfb >= self.sbinfo.long_bands.len() - 1 { return; }
207 let mut k = self.get_band_start(pdata.pulse_start_sfb);
208 let mut band = pdata.pulse_start_sfb;
209 for pno in 0..pdata.number_pulse {
210 k += pdata.pulse_offset[pno] as usize;
211 if k >= 1024 { return; }
212 while self.get_band_start(band + 1) <= k { band += 1; }
213 let scale = get_scale(self.scales[0][band]);
214 let mut base = self.coeffs[k];
215 if base != 0.0 {
216 base = requant(self.coeffs[k], scale);
217 }
218 if base > 0.0 {
219 base += f32::from(pdata.pulse_amp[pno]);
220 } else {
221 base -= f32::from(pdata.pulse_amp[pno]);
222 }
223 self.coeffs[k] = iquant(base) * scale;
224 }
225 }
226 }
227 fn decode_ics(&mut self, br: &mut BitReader, codebooks: &Codebooks, m4atype: M4AType, common_window: bool, may_have_intensity: bool) -> DecoderResult<()> {
228 self.global_gain = br.read(8)? as u8;
229 if !common_window {
230 self.info.decode_ics_info(br)?;
231 }
232 self.decode_section_data(br, may_have_intensity)?;
233 self.decode_scale_factor_data(br, codebooks)?;
234 self.pulse_data = PulseData::read(br)?;
235 validate!(self.pulse_data.is_none() || self.info.long_win);
236 let tns_max_order;
237 if !self.info.long_win {
238 tns_max_order = 7;
239 } else if m4atype == M4AType::LC {
240 tns_max_order = 12;
241 } else {
242 tns_max_order = TNS_MAX_ORDER;
243 }
244 self.tns_data = TNSData::read(br, self.info.long_win, self.info.num_windows, tns_max_order)?;
245 if m4atype == M4AType::SSR {
246 self.gain_control = GainControlData::read(br)?;
247 } else {
248 let gain_control_data_present = br.read_bool()?;
249 validate!(!gain_control_data_present);
250 }
251 self.decode_spectrum(br, codebooks)?;
252 Ok(())
253 }
254 fn synth_channel(&mut self, dsp: &mut DSP, dst: &mut [f32], srate_idx: usize) {
255 self.place_pulses();
256 if let Some(ref tns_data) = self.tns_data {
257 let tns_max_bands = TNSData::get_max_bands(self.info.long_win, srate_idx).min(self.info.max_sfb);
258 for w in 0..self.info.num_windows {
259 let mut bottom = self.get_num_bands();
260 for f in 0..tns_data.n_filt[w] {
261 let top = bottom;
262 bottom = if top >= tns_data.coeffs[w][f].length { top - tns_data.coeffs[w][f].length } else { 0 };
263 let order = tns_data.coeffs[w][f].order;
264 if order == 0 { continue; }
265 let start = w * 128 + self.get_band_start(tns_max_bands.min(bottom));
266 let end = w * 128 + self.get_band_start(tns_max_bands.min(top));
267 tns_data.apply(&mut self.coeffs, w, f, start, end);
268 }
269 }
270 }
271 dsp.synth(&self.coeffs, &mut self.delay, self.info.window_sequence, self.info.window_shape, self.info.prev_window_shape, dst);
272 }
273}
274
275#[derive(Clone)]
276struct ChannelPair {
277 pair: bool,
278 channel: usize,
279 common_window: bool,
280 ms_mask_present: u8,
281 ms_used: [[bool; MAX_SFBS]; MAX_WINDOWS],
282 ics: [ICS; 2],
283}
284
285impl ChannelPair {
286 fn new(pair: bool, channel: usize, sbinfo: GASubbandInfo) -> Self {
287 Self {
288 pair, channel,
289 common_window: false,
290 ms_mask_present: 0,
291 ms_used: [[false; MAX_SFBS]; MAX_WINDOWS],
292 ics: [ICS::new(sbinfo), ICS::new(sbinfo)],
293 }
294 }
295 fn decode_ga_sce(&mut self, br: &mut BitReader, codebooks: &Codebooks, m4atype: M4AType) -> DecoderResult<()> {
296 self.ics[0].decode_ics(br, codebooks, m4atype, false, false)?;
297 Ok(())
298 }
299 fn decode_ga_cpe(&mut self, br: &mut BitReader, codebooks: &Codebooks, m4atype: M4AType) -> DecoderResult<()> {
300 let common_window = br.read_bool()?;
301 self.common_window = common_window;
302 if common_window {
303 self.ics[0].info.decode_ics_info(br)?;
304 self.ms_mask_present = br.read(2)? as u8;
305 validate!(self.ms_mask_present != 3);
306 if self.ms_mask_present == 1 {
307 for g in 0..self.ics[0].info.window_groups {
308 for sfb in 0..self.ics[0].info.max_sfb {
309 self.ms_used[g][sfb] = br.read_bool()?;
310 }
311 }
312 }
313 self.ics[1].info = self.ics[0].info;
314 }
315 self.ics[0].decode_ics(br, codebooks, m4atype, common_window, true)?;
316 self.ics[1].decode_ics(br, codebooks, m4atype, common_window, true)?;
317 if common_window && self.ms_mask_present != 0 {
318 let mut g = 0;
319 for w in 0..self.ics[0].info.num_windows {
320 if w > 0 && !self.ics[0].info.scale_factor_grouping[w - 1] {
321 g += 1;
322 }
323 for sfb in 0..self.ics[0].info.max_sfb {
324 let start = w * 128 + self.ics[0].get_band_start(sfb);
325 let end = w * 128 + self.ics[0].get_band_start(sfb + 1);
326 if self.ics[0].is_intensity(g, sfb) {
327 let invert = (self.ms_mask_present == 1) && self.ms_used[g][sfb];
328 let dir = self.ics[0].get_intensity_dir(g, sfb) ^ invert;
329 let scale = 0.5f32.powf(0.25 * (f32::from(self.ics[0].scales[g][sfb]) + f32::from(INTENSITY_SCALE_MIN)));
330 if !dir {
331 for i in start..end {
332 self.ics[1].coeffs[i] = scale * self.ics[0].coeffs[i];
333 }
334 } else {
335 for i in start..end {
336 self.ics[1].coeffs[i] = -scale * self.ics[0].coeffs[i];
337 }
338 }
339 } else if (self.ms_mask_present == 2) || self.ms_used[g][sfb] {
340 for i in start..end {
341 let tmp = self.ics[0].coeffs[i] - self.ics[1].coeffs[i];
342 self.ics[0].coeffs[i] += self.ics[1].coeffs[i];
343 self.ics[1].coeffs[i] = tmp;
344 }
345 }
346 }
347 }
348 }
349 Ok(())
350 }
351 fn synth_audio(&mut self, dsp: &mut DSP, abuf: &mut NABufferType, srate_idx: usize) {
352 let mut adata = abuf.get_abuf_f32().unwrap();
353 let output = adata.get_data_mut().unwrap();
354 let off0 = abuf.get_offset(self.channel);
355 let off1 = abuf.get_offset(self.channel + 1);
356 self.ics[0].synth_channel(dsp, &mut output[off0..], srate_idx);
357 if self.pair {
358 self.ics[1].synth_channel(dsp, &mut output[off1..], srate_idx);
359 }
360 }
361}
362
363struct DSP {
364 kbd_long_win: [f32; 1024],
365 kbd_short_win: [f32; 128],
366 sine_long_win: [f32; 1024],
367 sine_short_win: [f32; 128],
368 imdct_long: IMDCT,
369 imdct_short: IMDCT,
370 tmp: [f32; 2048],
371 ew_buf: [f32; 1152],
372}
373
374const SHORT_WIN_POINT0: usize = 512 - 64;
375const SHORT_WIN_POINT1: usize = 512 + 64;
376
377impl DSP {
378 fn new() -> Self {
379 let mut kbd_long_win: [f32; 1024] = [0.0; 1024];
380 let mut kbd_short_win: [f32; 128] = [0.0; 128];
381 generate_window(WindowType::KaiserBessel(4.0), 1.0, 1024, true, &mut kbd_long_win);
382 generate_window(WindowType::KaiserBessel(6.0), 1.0, 128, true, &mut kbd_short_win);
383 let mut sine_long_win: [f32; 1024] = [0.0; 1024];
384 let mut sine_short_win: [f32; 128] = [0.0; 128];
385 generate_window(WindowType::Sine, 1.0, 1024, true, &mut sine_long_win);
386 generate_window(WindowType::Sine, 1.0, 128, true, &mut sine_short_win);
387 Self {
388 kbd_long_win, kbd_short_win,
389 sine_long_win, sine_short_win,
390 imdct_long: IMDCT::new(1024 * 2, true),
391 imdct_short: IMDCT::new(128 * 2, true),
392 tmp: [0.0; 2048], ew_buf: [0.0; 1152],
393 }
394 }
395 #[allow(clippy::cognitive_complexity)]
396 fn synth(&mut self, coeffs: &[f32; 1024], delay: &mut [f32; 1024], seq: u8, window_shape: bool, prev_window_shape: bool, dst: &mut [f32]) {
397 let long_win = if window_shape { &self.kbd_long_win } else { &self.sine_long_win };
398 let short_win = if window_shape { &self.kbd_short_win } else { &self.sine_short_win };
399 let left_long_win = if prev_window_shape { &self.kbd_long_win } else { &self.sine_long_win };
400 let left_short_win = if prev_window_shape { &self.kbd_short_win } else { &self.sine_short_win };
401 if seq != EIGHT_SHORT_SEQUENCE {
402 self.imdct_long.imdct(coeffs, &mut self.tmp);
403 } else {
404 for (ain, aout) in coeffs.chunks(128).zip(self.tmp.chunks_mut(256)) {
405 self.imdct_short.imdct(ain, aout);
406 }
407 self.ew_buf = [0.0; 1152];
408 for (w, src) in self.tmp.chunks(256).enumerate() {
409 if w > 0 {
410 for i in 0..128 {
411 self.ew_buf[w * 128 + i] += src[i] * short_win[i];
412 }
413 } else { // to be left-windowed
414 for i in 0..128 {
415 self.ew_buf[i] = src[i];
416 }
417 }
418 for i in 0..128 {
419 self.ew_buf[w * 128 + i + 128] += src[i + 128] * short_win[127 - i];
420 }
421 }
422 }
423 if seq == ONLY_LONG_SEQUENCE { // should be the most common case
424 for i in 0..1024 {
425 dst[i] = delay[i] + self.tmp[i] * left_long_win[i];
426 delay[i] = self.tmp[i + 1024] * long_win[1023 - i];
427 }
428 return;
429 }
430 // output new data
431 match seq {
432 ONLY_LONG_SEQUENCE | LONG_START_SEQUENCE => {
433 for i in 0..1024 {
434 dst[i] = self.tmp[i] * left_long_win[i] + delay[i];
435 }
436 },
437 EIGHT_SHORT_SEQUENCE => {
438 for i in 0..SHORT_WIN_POINT0 {
439 dst[i] = delay[i];
440 }
441 for i in SHORT_WIN_POINT0..SHORT_WIN_POINT1 {
442 let j = i - SHORT_WIN_POINT0;
443 dst[i] = delay[i] + self.ew_buf[j] * left_short_win[j];
444 }
445 for i in SHORT_WIN_POINT1..1024 {
446 let j = i - SHORT_WIN_POINT0;
447 dst[i] = self.ew_buf[j];
448 }
449 },
450 LONG_STOP_SEQUENCE => {
451 for i in 0..SHORT_WIN_POINT0 {
452 dst[i] = delay[i];
453 }
454 for i in SHORT_WIN_POINT0..SHORT_WIN_POINT1 {
455 dst[i] = delay[i] + self.tmp[i] * left_short_win[i - SHORT_WIN_POINT0];
456 }
457 for i in SHORT_WIN_POINT1..1024 {
458 dst[i] = self.tmp[i];
459 }
460 },
461 _ => unreachable!(""),
462 };
463 // save delay
464 match seq {
465 ONLY_LONG_SEQUENCE | LONG_STOP_SEQUENCE => {
466 for i in 0..1024 {
467 delay[i] = self.tmp[i + 1024] * long_win[1023 - i];
468 }
469 },
470 EIGHT_SHORT_SEQUENCE => {
471 for i in 0..SHORT_WIN_POINT1 { // last part is already windowed
472 delay[i] = self.ew_buf[i + 512+64];
473 }
474 for i in SHORT_WIN_POINT1..1024 {
475 delay[i] = 0.0;
476 }
477 },
478 LONG_START_SEQUENCE => {
479 for i in 0..SHORT_WIN_POINT0 {
480 delay[i] = self.tmp[i + 1024];
481 }
482 for i in SHORT_WIN_POINT0..SHORT_WIN_POINT1 {
483 delay[i] = self.tmp[i + 1024] * short_win[127 - (i - SHORT_WIN_POINT0)];
484 }
485 for i in SHORT_WIN_POINT1..1024 {
486 delay[i] = 0.0;
487 }
488 },
489 _ => unreachable!(""),
490 };
491 }
492}
493
494struct AACDecoder {
495 info: NACodecInfoRef,
496 chmap: NAChannelMap,
497 m4ainfo: M4AInfo,
498 pairs: Vec<ChannelPair>,
499 codebooks: Codebooks,
500 dsp: DSP,
501 sbinfo: GASubbandInfo,
502}
503
504impl AACDecoder {
505 fn new() -> Self {
506 AACDecoder {
507 info: NACodecInfo::new_dummy(),
508 chmap: NAChannelMap::new(),
509 m4ainfo: M4AInfo::new(),
510 pairs: Vec::new(),
511 codebooks: Codebooks::new(),
512 dsp: DSP::new(),
513 sbinfo: AAC_SUBBAND_INFO[0],
514 }
515 }
516 fn set_pair(&mut self, pair_no: usize, channel: usize, pair: bool) -> DecoderResult<()> {
517 if self.pairs.len() <= pair_no {
518 self.pairs.push(ChannelPair::new(pair, channel, self.sbinfo));
519 } else {
520 validate!(self.pairs[pair_no].channel == channel);
521 validate!(self.pairs[pair_no].pair == pair);
522 }
523 validate!(if pair { channel + 1 } else { channel } < self.m4ainfo.channels);
524 Ok(())
525 }
526 fn decode_ga(&mut self, br: &mut BitReader, abuf: &mut NABufferType) -> DecoderResult<()> {
527 let mut cur_pair = 0;
528 let mut cur_ch = 0;
529 while br.left() > 3 {
530 let id = br.read(3)?;
531 match id {
532 0 => { // ID_SCE
533 let _tag = br.read(4)?;
534 self.set_pair(cur_pair, cur_ch, false)?;
535 self.pairs[cur_pair].decode_ga_sce(br, &self.codebooks, self.m4ainfo.otype)?;
536 cur_pair += 1;
537 cur_ch += 1;
538 },
539 1 => { // ID_CPE
540 let _tag = br.read(4)?;
541 self.set_pair(cur_pair, cur_ch, true)?;
542 self.pairs[cur_pair].decode_ga_cpe(br, &self.codebooks, self.m4ainfo.otype)?;
543 cur_pair += 1;
544 cur_ch += 2;
545 },
546 2 => { // ID_CCE
547 unimplemented!("coupling channel element");
548 },
549 3 => { // ID_LFE
550 let _tag = br.read(4)?;
551 self.set_pair(cur_pair, cur_ch, false)?;
552 self.pairs[cur_pair].decode_ga_sce(br, &self.codebooks, self.m4ainfo.otype)?;
553 cur_pair += 1;
554 cur_ch += 1;
555 },
556 4 => { // ID_DSE
557 let _id = br.read(4)?;
558 let align = br.read_bool()?;
559 let mut count = br.read(8)? as u32;
560 if count == 255 { count += br.read(8)? as u32; }
561 if align { br.align(); }
562 br.skip(count * 8)?; // no SBR payload or such
563 },
564 5 => { // ID_PCE
565 unimplemented!("program config");
566 },
567 6 => { // ID_FIL
568 let mut count = br.read(4)? as usize;
569 if count == 15 {
570 count += br.read(8)? as usize;
571 count -= 1;
572 }
573 for _ in 0..count {
574 // ext payload
575 br.skip(8)?;
576 }
577 },
578 7 => { // ID_TERM
579 break;
580 },
581 _ => { unreachable!(); },
582 };
583 }
584 let srate_idx = GASubbandInfo::find_idx(self.m4ainfo.srate);
585 for pair in 0..cur_pair {
586 self.pairs[pair].synth_audio(&mut self.dsp, abuf, srate_idx);
587 }
588 Ok(())
589 }
590}
591
592impl NADecoder for AACDecoder {
593 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
594 if let NACodecTypeInfo::Audio(_) = info.get_properties() {
595 let edata = info.get_extradata().unwrap();
596 validate!(edata.len() >= 2);
597
598//print!("edata:"); for s in edata.iter() { print!(" {:02X}", *s);}println!("");
599 if (edata.len() > 12) && (&edata[4..8] == b"esds") {
600 let mut mr = MemoryReader::new_read(edata.as_slice());
601 let mut br = ByteReader::new(&mut mr);
602 let esds_size = br.read_u32be()? as usize;
603 validate!(esds_size <= edata.len());
604 br.read_skip(8)?;
605 let mut info_start = 0;
606 let mut info_size = 0;
607 while br.tell() < (esds_size as u64) {
608 let tag = br.read_byte()?;
609 let mut size = 0;
610 loop {
611 let b = br.read_byte()?;
612 size = (size << 7) | u64::from(b & 0x7F);
613 validate!(br.tell() + size <= (esds_size as u64));
614 if (b & 0x80) == 0 {
615 break;
616 }
617 }
618 match tag {
619 3 => {
620 br.read_u16be()?;
621 let flags = br.read_byte()?;
622 if (flags & 0x80) != 0 {
623 br.read_u16be()?;
624 }
625 if (flags & 0x40) != 0 {
626 let len = br.read_byte()?;
627 br.read_skip(len as usize)?;
628 }
629 if (flags & 0x20) != 0 {
630 br.read_u16be()?;
631 }
632 },
633 4 => {
634 let _otype = br.read_byte()?;
635 let _stype = br.read_byte()?;
636 let _flags = br.read_u24be()?;
637 let _max_br = br.read_u32be()?;
638 let _min_br = br.read_u32be()?;
639 },
640 5 => {
641 info_start = br.tell() as usize;
642 info_size = size as usize;
643 break;
644 },
645 _ => br.read_skip(size as usize)?,
646 }
647 }
648 validate!(info_start > 0 && info_size > 0);
649 self.m4ainfo.read(&edata[info_start..][..info_size])?;
650 } else {
651 self.m4ainfo.read(&edata)?;
652 }
653
654 //println!("{}", self.m4ainfo);
655 if (self.m4ainfo.otype != M4AType::LC) || (self.m4ainfo.channels > 2) || (self.m4ainfo.samples != 1024) {
656 return Err(DecoderError::NotImplemented);
657 }
658 self.sbinfo = GASubbandInfo::find(self.m4ainfo.srate);
659
660 let ainfo = NAAudioInfo::new(self.m4ainfo.srate, self.m4ainfo.channels as u8,
661 SND_F32P_FORMAT, self.m4ainfo.samples);
662 self.info = info.replace_info(NACodecTypeInfo::Audio(ainfo));
663
664 if self.m4ainfo.channels >= DEFAULT_CHANNEL_MAP.len() {
665 return Err(DecoderError::NotImplemented);
666 }
667 let chmap_str = DEFAULT_CHANNEL_MAP[self.m4ainfo.channels];
668 if chmap_str.is_empty() { return Err(DecoderError::NotImplemented); }
669 self.chmap = NAChannelMap::from_str(chmap_str).unwrap();
670
671 Ok(())
672 } else {
673 Err(DecoderError::InvalidData)
674 }
675 }
676 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
677 let info = pkt.get_stream().get_info();
678 validate!(info.get_properties().is_audio());
679 let pktbuf = pkt.get_buffer();
680
681 let ainfo = self.info.get_properties().get_audio_info().unwrap();
682 let mut abuf = alloc_audio_buffer(ainfo, self.m4ainfo.samples, self.chmap.clone())?;
683
684 let mut br = BitReader::new(&pktbuf, BitReaderMode::BE);
685 match self.m4ainfo.otype {
686 M4AType::LC => {
687 self.decode_ga(&mut br, &mut abuf)?;
688 },
689 _ => { unimplemented!(""); }
690 }
691
692 let mut frm = NAFrame::new_from_pkt(pkt, self.info.replace_info(NACodecTypeInfo::Audio(ainfo)), abuf);
693 frm.set_keyframe(true);
694 Ok(frm.into_ref())
695 }
696 fn flush(&mut self) {
697 for pair in self.pairs.iter_mut() {
698 pair.ics[0].delay = [0.0; 1024];
699 pair.ics[1].delay = [0.0; 1024];
700 }
701 }
702}
703
704impl NAOptionHandler for AACDecoder {
705 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
706 fn set_options(&mut self, _options: &[NAOption]) { }
707 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
708}
709
710pub fn get_decoder() -> Box<dyn NADecoder + Send> {
711 Box::new(AACDecoder::new())
712}
713
714#[cfg(test)]
715mod test {
716 use nihav_core::codecs::RegisteredDecoders;
717 use nihav_core::demuxers::RegisteredDemuxers;
718 use nihav_codec_support::test::dec_video::test_decode_audio;
719 use crate::mpeg_register_all_decoders;
720 use nihav_realmedia::realmedia_register_all_demuxers;
721 #[test]
722 fn test_aac() {
723 let mut dmx_reg = RegisteredDemuxers::new();
724 realmedia_register_all_demuxers(&mut dmx_reg);
725 let mut dec_reg = RegisteredDecoders::new();
726 mpeg_register_all_decoders(&mut dec_reg);
727
728// let file = "assets/RV/rv40_weighted_mc.rmvb";
729 let file = "assets/RV/rv40_weighted_mc_2.rmvb";
730 test_decode_audio("realmedia", file, Some(12000), None/*Some("aac")*/, &dmx_reg, &dec_reg);
731 }
732}
733
734const DEFAULT_CHANNEL_MAP: [&str; 9] = [
735 "",
736 "C",
737 "L,R",
738 "C,L,R",
739 "C,L,R,Cs",
740 "C,L,R,Ls,Rs",
741 "C,L,R,Ls,Rs,LFE",
742 "",
743 "C,L,R,Ls,Rs,Lss,Rss,LFE",
744];