forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBbede77eTahinli2 months ago3534 commits

syncinfo.rs96 lines, 3.2 KiBHistoryRawDownload
1//! `syncinfo()` — the five bytes an AC-3 syncframe opens with (A/52 §5.3.1).
2
3use ec_core::{BitReader, Error, Result};
4
5use crate::tables;
6
7/// The AC-3 sync word, `0x0B77`, in the order it appears on the wire.
8pub const SYNCWORD: u16 = 0x0B77;
9
10/// Everything `syncinfo()` states, plus the two values every caller derives
11/// from it (sample rate and frame size).
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct SyncInfo {
14 /// CRC over the first 5/8 of the frame (§7.10.1). Carried, not checked.
15 pub crc1: u16,
16 /// Sample rate code (Table 5.6).
17 pub fscod: u8,
18 /// Frame size code (Table 5.18).
19 pub frmsizecod: u8,
20 /// Sample rate in Hz, from `fscod`.
21 pub sample_rate: u32,
22 /// Whole frame length in bytes, from `frmsizecod` and `fscod`.
23 pub frame_size: usize,
24 /// Nominal bit rate in kbit/s.
25 pub bit_rate_kbps: u32,
26}
27
28/// Parse `syncinfo()` from the start of a syncframe.
29///
30/// `data` starts at the sync word. Fewer than 5 bytes is [`Error::NeedMore`];
31/// a wrong sync word or a reserved `fscod`/`frmsizecod` is [`Error::Corrupt`],
32/// which is what makes this usable as a "is this AC-3?" probe.
33pub fn parse(data: &[u8]) -> Result<SyncInfo> {
34 parse_from(&mut BitReader::new(data))
35}
36
37/// [`parse`] against a reader the caller keeps, so a decoder can carry on into
38/// `bsi()` on the same bit position.
39pub fn parse_from(r: &mut BitReader<'_>) -> Result<SyncInfo> {
40 let syncword = r.read_bits(16)? as u16;
41 if syncword != SYNCWORD {
42 return Err(Error::corrupt(format!(
43 "AC-3 syncinfo: syncword {syncword:#06x}, expected {SYNCWORD:#06x}"
44 )));
45 }
46 let crc1 = r.read_bits(16)? as u16;
47 let fscod = r.read_bits(2)? as u8;
48 let frmsizecod = r.read_bits(6)? as u8;
49 let sample_rate = tables::SAMPLE_RATE[fscod as usize];
50 if sample_rate == 0 {
51 return Err(Error::corrupt("AC-3 syncinfo: fscod = 3 (reserved)"));
52 }
53 let Some(words) = tables::FRAME_SIZE_WORDS.get(frmsizecod as usize) else {
54 return Err(Error::corrupt(format!(
55 "AC-3 syncinfo: frmsizecod = {frmsizecod} (reserved)"
56 )));
57 };
58 Ok(SyncInfo {
59 crc1,
60 fscod,
61 frmsizecod,
62 sample_rate,
63 frame_size: usize::from(words[fscod as usize]) * 2,
64 bit_rate_kbps: tables::BIT_RATE_KBPS[frmsizecod as usize >> 1],
65 })
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn parses_a_48k_448k_header() {
74 // syncword, crc1 = 0x1234, fscod = 0 (48 kHz), frmsizecod = 30 (448k).
75 let data = [0x0B, 0x77, 0x12, 0x34, 0b0001_1110];
76 let si = parse(&data).unwrap();
77 assert_eq!(si.crc1, 0x1234);
78 assert_eq!(si.sample_rate, 48_000);
79 assert_eq!(si.bit_rate_kbps, 448);
80 assert_eq!(si.frame_size, 896 * 2);
81 }
82
83 #[test]
84 fn refuses_a_bad_syncword_and_short_input() {
85 assert!(matches!(
86 parse(&[0x0B, 0x78, 0, 0, 0]),
87 Err(Error::Corrupt { .. })
88 ));
89 assert!(matches!(parse(&[0x0B, 0x77, 0]), Err(Error::NeedMore)));
90 // fscod = 3 is reserved.
91 assert!(matches!(
92 parse(&[0x0B, 0x77, 0, 0, 0b1100_0000]),
93 Err(Error::Corrupt { .. })
94 ));
95 }
96}