oarust 0.13.0

GPU-first semantic computing engine written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
// Ported from veloce-ac3 0.1.7 (5c7020d62a869ace14af43aa5bfe9c49499c6bea), MIT.
// See NOTICE.md for attribution and license text.

//! E-AC-3 (ATSC A/52:2012 Annex E): the syncframe header and bit stream information (§E2.2.2),
//! the audio frame (§E2.2.3) and spectral extension (§E2.2.4, §E3.6). The audio blocks share the
//! AC-3 decoder (`Decoder::audio_block`), which reads the E-AC-3 differences from [`FrameInfo`].

use super::{Bits, Error, Header, NFCHANS, Result};

/// Sample rates by fscod, and by fscod2 when fscod is 3 (Tables E2.2, E2.3).
const RATES: [u32; 3] = [48_000, 44_100, 32_000];
const REDUCED_RATES: [u32; 3] = [24_000, 22_050, 16_000];
/// Audio blocks per syncframe by numblkscod (Table E2.4).
const BLOCKS: [u8; 4] = [1, 2, 3, 6];

/// Exponent strategies of the six blocks for each frame-based strategy code (Table E2.10):
/// 0 reuse, 1 D15, 2 D25, 3 D45.
const FRAME_EXPSTR: [[u8; 6]; 32] = [
	[1, 0, 0, 0, 0, 0],
	[1, 0, 0, 0, 0, 3],
	[1, 0, 0, 0, 2, 0],
	[1, 0, 0, 0, 3, 3],
	[2, 0, 0, 2, 0, 0],
	[2, 0, 0, 2, 0, 3],
	[2, 0, 0, 3, 2, 0],
	[2, 0, 0, 3, 3, 3],
	[2, 0, 1, 0, 0, 0],
	[2, 0, 2, 0, 0, 3],
	[2, 0, 2, 0, 2, 0],
	[2, 0, 2, 0, 3, 3],
	[2, 0, 3, 2, 0, 0],
	[2, 0, 3, 2, 0, 3],
	[2, 0, 3, 3, 2, 0],
	[2, 0, 3, 3, 3, 3],
	[3, 1, 0, 0, 0, 0],
	[3, 1, 0, 0, 0, 3],
	[3, 2, 0, 0, 2, 0],
	[3, 2, 0, 0, 3, 3],
	[3, 2, 0, 2, 0, 0],
	[3, 2, 0, 2, 0, 3],
	[3, 2, 0, 3, 2, 0],
	[3, 2, 0, 3, 3, 3],
	[3, 3, 1, 0, 0, 0],
	[3, 3, 2, 0, 0, 3],
	[3, 3, 2, 0, 2, 0],
	[3, 3, 2, 0, 3, 3],
	[3, 3, 3, 2, 0, 0],
	[3, 3, 3, 2, 0, 3],
	[3, 3, 3, 3, 2, 0],
	[3, 3, 3, 3, 3, 3],
];

/// Default coupling band structure by coupling sub-band (Table E2.12, "Default Coupling Banding
/// Structure"; sub-band 0 never starts a combined band).
const DEFAULT_CPLBNDSTRC: [bool; 18] = {
	let mut t = [false; 18];
	let ones = [8, 10, 11, 13, 14, 15, 16, 17];
	let mut i = 0;
	while i < ones.len() {
		t[ones[i]] = true;
		i += 1;
	}
	t
};

/// Default spectral extension band structure by sub-band (Table E2.11).
const DEFAULT_SPXBNDSTRC: [bool; 17] = {
	let mut t = [false; 17];
	let mut i = 8;
	while i < 17 {
		t[i] = true;
		i += 2;
	}
	t
};

/// First transform coefficient of spectral extension sub-band `k` (Table E3.13, k ≤ 17).
const fn spx_band_bin(k: usize) -> usize {
	25 + 12 * k
}

/// Spectral extension attenuation (Table E3.14): spxattentab[code][binindex] = 2^(-(code + 1) ·
/// (binindex + 1) / 15), which the table lists to nine decimals.
fn spx_atten(code: usize, binindex: usize) -> f32 {
	let e = (code as f64 + 1.0) * (binindex as f64 + 1.0) / 15.0;
	(-e).exp2() as f32
}

/// Parse the header of an E-AC-3 syncframe (bsid 11-16) at the start of `b` (at least 7 bytes).
pub(crate) fn parse_header(b: &[u8], bsid: u8) -> Result<Header> {
	let mut r = Bits::new(b.get(2..).unwrap_or_default());
	let strmtyp = r.read(2) as u8;
	let substreamid = r.read(3) as u8;
	let words = r.read(11) as usize + 1;
	let fscod = r.read(2) as usize;
	let (sample_rate, blocks) = match RATES.get(fscod) {
		Some(&rate) => (rate, BLOCKS[r.read(2) as usize & 3]),
		None => (
			*REDUCED_RATES
				.get(r.read(2) as usize)
				.ok_or(Error::Invalid("reserved fscod2"))?,
			6,
		),
	};
	let acmod = r.read(3) as u8;
	let lfeon = r.bit();
	if strmtyp == 3 {
		return Err(Error::Unsupported("reserved E-AC-3 stream type 3".into()));
	}
	let frame_bytes = words * 2;
	if frame_bytes < 8 {
		return Err(Error::Invalid("E-AC-3 syncframe too short"));
	}
	// frame bits per second of audio
	let bits = frame_bytes as u64 * 8 * u64::from(sample_rate) / (256 * u64::from(blocks));
	let bitrate_kbps = u32::try_from(bits / 1000).unwrap_or(u32::MAX);
	Ok(Header {
		sample_rate,
		bitrate_kbps,
		frame_bytes,
		bsid,
		acmod,
		lfeon,
		blocks,
		strmtyp,
		substreamid,
	})
}

/// What the audio frame and blocks need from the bit stream information.
pub(crate) struct Bsi {
	pub strmtyp: u8,
	/// Sample rate code for the bit allocation's hearing threshold: fscod, or fscod2 at the
	/// reduced rates.
	pub fscod: usize,
	pub numblkscod: usize,
}

/// Read syncinfo and bsi (§E2.2.1-2), leaving `r` at the audio frame.
pub(crate) fn read_bsi(r: &mut Bits) -> Result<Bsi> {
	r.skip(16); // syncword
	let strmtyp = r.read(2) as u8;
	r.skip(3 + 11); // substreamid, frmsiz
	let fscod = r.read(2) as usize;
	let (rate_code, numblkscod) = if fscod == 3 {
		let fscod2 = r.read(2) as usize;
		if fscod2 == 3 {
			return Err(Error::Invalid("reserved fscod2"));
		}
		(fscod2, 3)
	} else {
		(fscod, r.read(2) as usize)
	};
	let nblocks = BLOCKS[numblkscod & 3] as usize;
	let acmod = r.read(3) as usize;
	let lfeon = r.bit();
	r.skip(5 + 5); // bsid, dialnorm
	if r.bit() {
		r.skip(8); // compr
	}
	if acmod == 0 {
		r.skip(5); // dialnorm2
		if r.bit() {
			r.skip(8); // compr2
		}
	}
	if strmtyp == 1 && r.bit() {
		r.skip(16); // chanmap
	}
	if r.bit() {
		// mixing metadata
		if acmod > 2 {
			r.skip(2); // dmixmod
		}
		if acmod & 1 != 0 && acmod > 2 {
			r.skip(6); // ltrtcmixlev, lorocmixlev
		}
		if acmod & 4 != 0 {
			r.skip(6); // ltrtsurmixlev, lorosurmixlev
		}
		if lfeon && r.bit() {
			r.skip(5); // lfemixlevcod
		}
		if strmtyp == 0 {
			if r.bit() {
				r.skip(6); // pgmscl
			}
			if acmod == 0 && r.bit() {
				r.skip(6); // pgmscl2
			}
			if r.bit() {
				r.skip(6); // extpgmscl
			}
			match r.read(2) {
				1 => r.skip(5), // premixcmpsel, drcsrc, premixcmpscl
				2 => r.skip(12),
				// mixdeflen: the mixdata field (through mixdatafill) is mixdeflen + 2 bytes
				3 => {
					let len = r.read(5) as usize;
					r.skip(8 * (len + 2));
				}
				_ => {}
			}
			if acmod < 2 {
				if r.bit() {
					r.skip(14); // panmean, paninfo
				}
				if acmod == 0 && r.bit() {
					r.skip(14); // panmean2, paninfo2
				}
			}
			if r.bit() {
				// frame mixing configuration information
				if numblkscod == 0 {
					r.skip(5);
				} else {
					for _ in 0..nblocks {
						if r.bit() {
							r.skip(5);
						}
					}
				}
			}
		}
	}
	if r.bit() {
		// informational metadata
		r.skip(5); // bsmod, copyrightb, origbs
		if acmod == 2 {
			r.skip(4); // dsurmod, dheadphonmod
		}
		if acmod >= 6 {
			r.skip(2); // dsurexmod
		}
		if r.bit() {
			r.skip(8); // mixlevel, roomtyp, adconvtyp
		}
		if acmod == 0 && r.bit() {
			r.skip(8); // mixlevel2, roomtyp2, adconvtyp2
		}
		if fscod < 3 {
			r.skip(1); // sourcefscod
		}
	}
	if strmtyp == 0 && numblkscod != 3 {
		r.skip(1); // convsync
	}
	if strmtyp == 2 && (numblkscod == 3 || r.bit()) {
		r.skip(6); // blkid → frmsizecod
	}
	if r.bit() {
		let l = r.read(6) as usize;
		r.skip((l + 1) * 8); // addbsi
	}
	Ok(Bsi {
		strmtyp,
		fscod: rate_code,
		numblkscod,
	})
}

/// The audio frame (§E2.2.3) and the state that runs from block to block within a syncframe.
pub(crate) struct FrameInfo {
	pub strmtyp: u8,
	pub blkswe: bool,
	pub dithflage: bool,
	pub bamode: bool,
	pub frmfgaincode: bool,
	pub dbaflde: bool,
	pub skipflde: bool,
	pub snroffststr: u8,
	pub frmcsnroffst: i32,
	pub frmfsnroffst: i32,
	pub cplstre: [bool; 6],
	pub cplinu: [bool; 6],
	pub cplexpstr: [u8; 6],
	pub chexpstr: [[u8; 5]; 6],
	pub lfeexpstr: [u8; 6],
	/// Spectral extension attenuation code per channel (spxattencod), when in use.
	pub spxatten: [Option<usize>; 5],
	pub firstcplcos: [bool; 5],
	pub firstcplleak: bool,
	/// Coupling band structure by absolute coupling sub-band.
	pub cplbndstrc: [bool; 18],
	pub spx: Spx,
}

impl FrameInfo {
	/// Read the audio frame. `words` is the syncframe size in 16-bit words.
	pub(crate) fn read(
		r: &mut Bits,
		bsi: &Bsi,
		acmod: usize,
		lfeon: bool,
		nblocks: usize,
		words: usize,
	) -> Result<FrameInfo> {
		let nf = NFCHANS[acmod & 7];
		let six = bsi.numblkscod == 3;
		let (expstre, ahte) = if six {
			(r.bit(), r.bit())
		} else {
			(true, false)
		};
		let snroffststr = r.read(2) as u8;
		if snroffststr == 3 {
			return Err(Error::Invalid("reserved SNR offset strategy"));
		}
		let transproce = r.bit();
		let mut e = FrameInfo {
			strmtyp: bsi.strmtyp,
			blkswe: r.bit(),
			dithflage: r.bit(),
			bamode: r.bit(),
			frmfgaincode: r.bit(),
			dbaflde: r.bit(),
			skipflde: r.bit(),
			snroffststr,
			frmcsnroffst: 0,
			frmfsnroffst: 0,
			cplstre: [false; 6],
			cplinu: [false; 6],
			cplexpstr: [0; 6],
			chexpstr: [[0; 5]; 6],
			lfeexpstr: [0; 6],
			spxatten: [None; 5],
			firstcplcos: [true; 5],
			firstcplleak: true,
			cplbndstrc: DEFAULT_CPLBNDSTRC,
			spx: Spx::new(),
		};
		let spxattene = r.bit();
		if acmod > 1 {
			e.cplstre[0] = true;
			e.cplinu[0] = r.bit();
			for blk in 1..nblocks {
				e.cplstre[blk] = r.bit();
				e.cplinu[blk] = if e.cplstre[blk] {
					r.bit()
				} else {
					e.cplinu[blk - 1]
				};
			}
		}
		if expstre {
			for blk in 0..nblocks {
				if e.cplinu[blk] {
					e.cplexpstr[blk] = r.read(2) as u8;
				}
				for ch in 0..nf {
					e.chexpstr[blk][ch] = r.read(2) as u8;
				}
			}
		} else {
			// frame-based strategies (six blocks only)
			if acmod > 1 && e.cplinu.iter().any(|&c| c) {
				e.cplexpstr = FRAME_EXPSTR[r.read(5) as usize & 31];
			}
			for ch in 0..nf {
				let s = FRAME_EXPSTR[r.read(5) as usize & 31];
				for (blk, &v) in s.iter().enumerate() {
					e.chexpstr[blk][ch] = v;
				}
			}
		}
		if lfeon {
			for blk in 0..nblocks {
				e.lfeexpstr[blk] = r.read(1) as u8;
			}
		}
		if bsi.strmtyp == 0 && (six || r.bit()) {
			r.skip(5 * nf); // convexpstr
		}
		if ahte {
			// AHT flags exist for channels whose exponents are sent once in the frame (§E3.4.2)
			let unsupported = || {
				Err(Error::Unsupported(
					"E-AC-3 adaptive hybrid transform".into(),
				))
			};
			let ncplblks = e.cplinu.iter().filter(|&&c| c).count();
			let ncplregs = (0..6)
				.filter(|&b| e.cplstre[b] || e.cplexpstr[b] != 0)
				.count();
			if ncplblks == 6 && ncplregs == 1 && r.bit() {
				return unsupported();
			}
			for ch in 0..nf {
				if (0..6).filter(|&b| e.chexpstr[b][ch] != 0).count() == 1 && r.bit() {
					return unsupported();
				}
			}
			if lfeon && e.lfeexpstr.iter().filter(|&&s| s != 0).count() == 1 && r.bit() {
				return unsupported();
			}
		}
		if snroffststr == 0 {
			e.frmcsnroffst = r.read(6) as i32;
			e.frmfsnroffst = r.read(4) as i32;
		}
		if transproce {
			// transient pre-noise processing (§E3.7) is not applied
			for _ in 0..nf {
				if r.bit() {
					r.skip(10 + 8); // transprocloc, transproclen
				}
			}
		}
		if spxattene {
			for ch in 0..nf {
				if r.bit() {
					e.spxatten[ch] = Some(r.read(5) as usize);
				}
			}
		}
		if bsi.numblkscod != 0 && r.bit() {
			// blkstrtinfo: (blocks - 1) × (4 + ceil(log2(words per frame))) bits (§E2.3.2.27)
			let log2 = (usize::BITS - words.saturating_sub(1).leading_zeros()) as usize;
			r.skip((nblocks - 1) * (4 + log2));
		}
		Ok(e)
	}
}

/// Spectral extension parameters (§E2.2.4, §E3.6), carried from block to block of a syncframe.
pub(crate) struct Spx {
	pub inu: bool,
	pub chinspx: [bool; 5],
	pub begf: usize,
	strtf: usize,
	/// First sub-band and one past the last (spx_begin_subbnd, spx_end_subbnd).
	begin: usize,
	end: usize,
	bndstrc: [bool; 17],
	/// Band sizes in coefficients (spxbndsztab) and their count (nspxbnds).
	sizes: [usize; 17],
	nbands: usize,
	first: [bool; 5],
	co: [[f32; 17]; 5],
	nblend: [[f32; 17]; 5],
	sblend: [[f32; 17]; 5],
}

impl Spx {
	fn new() -> Spx {
		Spx {
			inu: false,
			chinspx: [false; 5],
			begf: 0,
			strtf: 0,
			begin: 0,
			end: 0,
			bndstrc: DEFAULT_SPXBNDSTRC,
			sizes: [0; 17],
			nbands: 0,
			first: [true; 5],
			co: [[0.0; 17]; 5],
			nblend: [[0.0; 17]; 5],
			sblend: [[1.0; 17]; 5],
		}
	}

	/// First coefficient of the extension region.
	pub fn begin_bin(&self) -> usize {
		spx_band_bin(self.begin)
	}

	/// Read the strategy and coordinates of block `blk`.
	pub(crate) fn read(&mut self, r: &mut Bits, blk: usize, acmod: usize, nf: usize) -> Result<()> {
		if blk == 0 || r.bit() {
			self.inu = r.bit();
			if self.inu {
				if acmod == 1 {
					self.chinspx = [true, false, false, false, false];
				} else {
					for ch in 0..nf {
						self.chinspx[ch] = r.bit();
					}
				}
				self.strtf = r.read(2) as usize;
				self.begf = r.read(3) as usize;
				let endf = r.read(3) as usize;
				self.begin = if self.begf < 6 {
					self.begf + 2
				} else {
					self.begf * 2 - 3
				};
				self.end = if endf < 3 { endf + 5 } else { endf * 2 + 3 };
				if r.bit() {
					for b in self.begin + 1..self.end {
						self.bndstrc[b] = r.bit();
					}
				}
				if self.begin >= self.end {
					return Err(Error::Invalid("spectral extension ends before it begins"));
				}
				if self.strtf >= self.begin {
					return Err(Error::Invalid(
						"spectral extension copies from above its start",
					));
				}
				self.nbands = 1;
				self.sizes = [0; 17];
				self.sizes[0] = 12;
				for b in self.begin + 1..self.end {
					if self.bndstrc[b] {
						self.sizes[self.nbands - 1] += 12;
					} else {
						self.sizes[self.nbands] = 12;
						self.nbands += 1;
					}
				}
			} else {
				self.chinspx = [false; 5];
				self.first = [true; 5];
			}
		}
		if !self.inu {
			return Ok(());
		}
		for ch in 0..nf {
			if !self.chinspx[ch] {
				self.first[ch] = true;
				continue;
			}
			let coe = std::mem::replace(&mut self.first[ch], false) || r.bit();
			if !coe {
				continue;
			}
			let blend = r.read(5) as f32 / 32.0;
			let mstr = r.read(2) as i32;
			// coordinates (§E3.6.3) and blending factors (§E3.6.4.2.1)
			let mut mant = self.begin_bin() as f32;
			let end = spx_band_bin(self.end) as f32;
			for b in 0..self.nbands {
				let e = r.read(4) as i32;
				let m = r.read(2) as f32;
				let v = if e == 15 { m / 4.0 } else { (m + 4.0) / 8.0 };
				self.co[ch][b] = v * 2f32.powi(-(e + 3 * mstr));
				let size = self.sizes[b] as f32;
				let nratio = ((mant + 0.5 * size) / end - blend).clamp(0.0, 1.0);
				self.nblend[ch][b] = nratio.sqrt();
				self.sblend[ch][b] = (1.0 - nratio).sqrt();
				mant += size;
			}
		}
		Ok(())
	}

	/// Synthesize the extension region of one channel's coefficients (§E3.6.4): translate the
	/// low band up, notch-filter the borders when attenuation is on, blend with `noise` (zero
	/// mean, unit variance) scaled to the band's energy, and scale by the coordinates.
	pub(crate) fn synthesize(
		&self,
		ch: usize,
		atten: Option<usize>,
		tc: &mut [f32; 256],
		mut noise: impl FnMut() -> f32,
	) {
		let copystart = spx_band_bin(self.strtf);
		let copyend = self.begin_bin();
		let sizes = &self.sizes[..self.nbands];
		// translation
		let mut wrap = [false; 17];
		let (mut copy, mut insert) = (copystart, copyend);
		for (b, &size) in sizes.iter().enumerate() {
			if copy + size > copyend {
				copy = copystart;
				wrap[b] = true;
			}
			for _ in 0..size {
				if copy == copyend {
					copy = copystart;
				}
				if let (Some(&v), true) = (tc.get(copy), insert < tc.len()) {
					tc[insert] = v;
				}
				insert += 1;
				copy += 1;
			}
		}
		// banded RMS energy of the translated coefficients
		let mut rms = [0f32; 17];
		let mut at = copyend;
		for (b, &size) in sizes.iter().enumerate() {
			let band = tc.get(at..at + size).unwrap_or_default();
			rms[b] = (band.iter().map(|v| v * v).sum::<f32>() / size as f32).sqrt();
			at += size;
		}
		// notch filter around the baseband / extension border and each wrap point
		if let Some(code) = atten {
			let g = [spx_atten(code, 0), spx_atten(code, 1), spx_atten(code, 2)];
			let mut notch = |center: usize| {
				for (k, gain) in [g[0], g[1], g[2], g[1], g[0]].into_iter().enumerate() {
					if let Some(v) = (center + k).checked_sub(2).and_then(|i| tc.get_mut(i)) {
						*v *= gain;
					}
				}
			};
			notch(copyend);
			let mut start = copyend;
			for (b, &size) in sizes.iter().enumerate() {
				if b > 0 && wrap[b] {
					notch(start);
				}
				start += size;
			}
		}
		// noise blending and scaling
		let mut at = copyend;
		for (b, &size) in sizes.iter().enumerate() {
			let nscale = rms[b] * self.nblend[ch][b];
			let sscale = self.sblend[ch][b];
			let co = self.co[ch][b] * 32.0;
			for _ in 0..size {
				let n = noise();
				if let Some(v) = tc.get_mut(at) {
					*v = (*v * sscale + n * nscale) * co;
				}
				at += 1;
			}
		}
	}
}