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
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use anyhow::{format_err, Result};
use bitfield::bitfield;
use fidl_fuchsia_media::*;
use std::{fs, io, path::Path};
use stream_processor_test::*;

/// Represents an SBC elementary stream.
pub struct SbcStream {
    data: Vec<u8>,
    oob_bytes: Vec<u8>,
    chunk_frames: usize,
}

impl SbcStream {
    /// Constructs an SBC elementary stream from a file with raw elementary stream data.
    pub fn from_file(
        filename: impl AsRef<Path>,
        codec_info: &[u8],
        chunk_frames: usize,
    ) -> io::Result<Self> {
        Ok(SbcStream { data: fs::read(filename)?, oob_bytes: codec_info.to_vec(), chunk_frames })
    }

    /// Returns an iterator over SBC frames that does not copy.
    fn frame_iter(&self) -> impl Iterator<Item = SbcFrame<'_>> {
        SbcFrameIter { data: &self.data, pos: 0, chunk_frames: self.chunk_frames }
    }
}

impl ElementaryStream for SbcStream {
    fn format_details(&self, version_ordinal: u64) -> FormatDetails {
        FormatDetails {
            format_details_version_ordinal: Some(version_ordinal),
            mime_type: Some(String::from("audio/sbc")),
            oob_bytes: Some(self.oob_bytes.clone()),
            ..Default::default()
        }
    }

    fn is_access_units(&self) -> bool {
        false
    }

    fn stream<'a>(&'a self) -> Box<dyn Iterator<Item = ElementaryStreamChunk> + 'a> {
        Box::new(self.frame_iter().map(|frame| ElementaryStreamChunk {
            start_access_unit: false,
            known_end_access_unit: false,
            data: frame.data.to_vec(),
            significance: Significance::Audio(AudioSignificance::Encoded),
            timestamp: None,
        }))
    }
}

#[derive(Debug, PartialEq)]
enum ChannelMode {
    Mono,
    DualChannel,
    Stereo,
    JointStereo,
}

impl From<u8> for ChannelMode {
    fn from(bits: u8) -> Self {
        match bits {
            0 => ChannelMode::Mono,
            1 => ChannelMode::DualChannel,
            2 => ChannelMode::Stereo,
            3 => ChannelMode::JointStereo,
            _ => panic!("invalid channel mode"),
        }
    }
}

bitfield! {
    pub struct SbcFrameHeader(u32);
    impl Debug;
    u8;
    syncword, _: 7, 0;
    subbands, _: 8;
    allocation_method, _: 9;
    into ChannelMode, channel_mode, _: 11, 10;
    blocks_bits, _: 13, 12;
    sampling_frequency_bits, _: 15, 14;
    bitpool_bits, _: 23, 16;
    crc_check, _: 31, 24;
}

impl SbcFrameHeader {
    /// The number of channels, based on the channel mode in the header.
    /// From Table 12.18 in the A2DP Spec.
    fn channels(&self) -> usize {
        match self.channel_mode() {
            ChannelMode::Mono => 1,
            _ => 2,
        }
    }

    fn has_syncword(&self) -> bool {
        const SBC_SYNCWORD: u8 = 0x9c;
        self.syncword() == SBC_SYNCWORD
    }

    /// The number of blocks, based on tbe bits in the header.
    /// From Table 12.17 in the A2DP Spec.
    fn blocks(&self) -> usize {
        4 * (self.blocks_bits() + 1) as usize
    }

    fn bitpool(&self) -> usize {
        self.bitpool_bits() as usize
    }

    /// Number of subbands based on the header bit.
    /// From Table 12.20 in the A2DP Spec.
    fn num_subbands(&self) -> usize {
        if self.subbands() {
            8
        } else {
            4
        }
    }

    /// Calculates the frame length.
    /// Formula from Section 12.9 of the A2DP Spec.
    fn frame_length(&self) -> Result<usize> {
        if !self.has_syncword() {
            return Err(format_err!("syncword does not match"));
        }
        let len = 4 + (4 * self.num_subbands() * self.channels()) / 8;
        let rest = (match self.channel_mode() {
            ChannelMode::Mono | ChannelMode::DualChannel => {
                self.blocks() * self.channels() * self.bitpool()
            }
            ChannelMode::Stereo => self.blocks() * self.bitpool(),
            ChannelMode::JointStereo => self.num_subbands() + (self.blocks() * self.bitpool()),
        } as f64
            / 8.0)
            .ceil() as usize;
        Ok(len + rest)
    }

    /// Given a buffer with an SBC frame at the start, find the length of the
    /// SBC frame.
    fn find_sbc_frame_len(buf: &[u8]) -> Result<usize> {
        if buf.len() < 4 {
            return Err(format_err!("Buffer too short for header"));
        }
        let hdr = u32::from_le_bytes((&buf[0..4]).try_into()?);
        SbcFrameHeader(hdr).frame_length()
    }
}

pub struct SbcFrame<'a> {
    pub data: &'a [u8],
    pub length: usize,
}

/// An iterator over frames in an SBC stream.
struct SbcFrameIter<'a> {
    data: &'a [u8],
    pos: usize,
    chunk_frames: usize,
}

impl<'a> SbcFrameIter<'a> {
    fn next_frame(&self, pos: usize) -> Option<SbcFrame<'a>> {
        SbcFrameHeader::find_sbc_frame_len(&self.data[pos..])
            .map(|len| {
                let end_pos = std::cmp::min(pos + len * self.chunk_frames, self.data.len());
                let frame_len = end_pos - pos;
                SbcFrame { data: &self.data[pos..end_pos], length: frame_len }
            })
            .ok()
    }
}

impl<'a> Iterator for SbcFrameIter<'a> {
    type Item = SbcFrame<'a>;
    fn next(&mut self) -> Option<Self::Item> {
        if self.pos >= self.data.len() {
            return None;
        }
        let frame = self.next_frame(self.pos);
        self.pos += frame.as_ref().map(|f| f.length).unwrap_or(0);
        frame
    }
}