1use crate::{
2 bits::BitReader,
3 error::{Error, InvalidData, Result},
4 input::{Input, SliceInput},
5 quantization::{self, SubbandSamples},
6 synthesis::Synthesis,
7 SAMPLES_PER_FRAME,
8};
9
10pub(crate) const FRAME_HEADER: [u8; 4] = [0xff, 0xf5, 0xe0, 0xc0];
11
12#[derive(Debug)]
14pub struct DecodedPacket<'a> {
15 pcm: &'a [i16; SAMPLES_PER_FRAME],
16 consumed: usize,
17}
18
19impl<'a> DecodedPacket<'a> {
20 pub fn pcm(&self) -> &'a [i16; SAMPLES_PER_FRAME] {
22 self.pcm
23 }
24
25 pub const fn consumed_bytes(&self) -> usize {
28 self.consumed
29 }
30}
31
32pub struct PacketDecoder {
40 synthesis: Synthesis,
41}
42
43impl core::fmt::Debug for PacketDecoder {
44 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
45 f.debug_struct("PacketDecoder").finish_non_exhaustive()
46 }
47}
48
49impl Default for PacketDecoder {
50 fn default() -> Self {
51 Self::new()
52 }
53}
54
55impl PacketDecoder {
56 pub fn new() -> Self {
58 Self {
59 synthesis: Synthesis::new(),
60 }
61 }
62
63 pub fn decode_into<'a>(
69 &mut self,
70 input: &[u8],
71 pcm: &'a mut [i16],
72 ) -> Result<DecodedPacket<'a>> {
73 let pcm = frame_buffer(pcm)?;
74 let consumed = self.decode_from(&mut SliceInput(input), pcm)?;
75 Ok(DecodedPacket { pcm, consumed })
76 }
77
78 pub(crate) fn decode_from(
79 &mut self,
80 input: &mut impl Input,
81 pcm: &mut [i16; SAMPLES_PER_FRAME],
82 ) -> Result<usize> {
83 let frame = ParsedFrame::read(input)?;
84 frame.synthesize(&mut self.synthesis, pcm);
87 Ok(frame.consumed)
88 }
89}
90
91pub(crate) fn frame_buffer(pcm: &mut [i16]) -> Result<&mut [i16; SAMPLES_PER_FRAME]> {
92 let provided = pcm.len();
93 pcm.first_chunk_mut().ok_or(Error::BufferTooSmall {
94 required: SAMPLES_PER_FRAME,
95 provided,
96 })
97}
98
99struct ParsedFrame {
100 bands: SubbandSamples,
101 consumed: usize,
102}
103
104impl ParsedFrame {
105 fn read(input: &mut impl Input) -> Result<Self> {
106 let mut header = [0; FRAME_HEADER.len()];
107 input.read_exact(&mut header)?;
108 if header != FRAME_HEADER {
109 return Err(Error::Invalid(InvalidData::FrameHeader));
110 }
111 let mut bits = BitReader::new(input);
112 let bands = quantization::read_subbands(&mut bits)?;
113 Ok(Self {
114 bands,
115 consumed: bits.consumed_bytes(),
116 })
117 }
118
119 fn synthesize(&self, synthesis: &mut Synthesis, pcm: &mut [i16; SAMPLES_PER_FRAME]) {
120 let (blocks, _) = pcm.as_chunks_mut::<32>();
121 for (band, output) in self.bands.iter().zip(blocks) {
122 synthesis.decode(band, output);
123 }
124 }
125}