anni_flac/
frames.rs

1#[derive(Debug)]
2pub enum Frames {
3    Parsed(Vec<Frame>),
4    Unparsed(Vec<u8>),
5    Skip,
6}
7
8#[derive(Debug)]
9pub struct Frame {
10    pub header: FrameHeader,
11
12    /// One SUBFRAME per channel.
13    pub subframes: Vec<SubFrame>,
14
15    /// FRAME_FOOTER
16    /// <16> CRC-16 (polynomial = x^16 + x^15 + x^2 + x^0, initialized with 0) of everything before the crc, back to and including the frame header sync code
17    pub crc: u16,
18}
19
20#[derive(Debug)]
21pub struct FrameHeader {
22    // <14> Sync code '11111111111110'
23    /// This bit must remain reserved for 0 in order for a FLAC frame's initial 15 bits to be distinguishable from the start of an MPEG audio frame (see also).
24    pub reserved: bool,
25    /// <1> Blocking strategy:
26    /// - 0 : fixed-blocksize stream; frame header encodes the frame number
27    /// - 1 : variable-blocksize stream; frame header encodes the sample number
28    ///
29    /// The "blocking strategy" bit must be the same throughout the entire stream.
30    /// The "blocking strategy" bit determines how to calculate the sample number of the first sample in the frame.
31    /// If the bit is 0 (fixed-blocksize), the frame header encodes the frame number as above, and the frame's starting sample number will be the frame number times the blocksize.
32    /// If it is 1 (variable-blocksize), the frame header encodes the frame's starting sample number itself.
33    /// (In the case of a fixed-blocksize stream, only the last block may be shorter than the stream blocksize;
34    ///  its starting sample number will be calculated as the frame number times the previous frame's blocksize, or zero if it is the first frame).
35    pub block_strategy: BlockStrategy,
36    /// <4> Block size in inter-channel samples:
37    /// - `0000` : reserved
38    /// - `0001` : 192 samples
39    /// - `0010-0101` : 576 * (2^(n-2)) samples, i.e. 576/1152/2304/4608
40    /// - `0110` : get 8 bit (blocksize-1) from end of header
41    /// - `0111` : get 16 bit (blocksize-1) from end of header
42    /// - `1000-1111` : 256 * (2^(n-8)) samples, i.e. 256/512/1024/2048/4096/8192/16384/32768
43    pub block_size: u16,
44    /// <4> Sample rate:
45    /// - `0000` : get from STREAMINFO metadata block
46    /// - `0001` : 88.2kHz
47    /// - `0010` : 176.4kHz
48    /// - `0011` : 192kHz
49    /// - `0100` : 8kHz
50    /// - `0101` : 16kHz
51    /// - `0110` : 22.05kHz
52    /// - `0111` : 24kHz
53    /// - `1000` : 32kHz
54    /// - `1001` : 44.1kHz
55    /// - `1010` : 48kHz
56    /// - `1011` : 96kHz
57    /// - `1100` : get 8 bit sample rate (in kHz) from end of header
58    /// - `1101` : get 16 bit sample rate (in Hz) from end of header
59    /// - `1110` : get 16 bit sample rate (in tens of Hz) from end of header
60    /// - `1111` : invalid, to prevent sync-fooling string of 1s
61    pub sample_rate: SampleRate,
62    /// <4> Channel assignment
63    /// - `0000-0111` : (number of independent channels)-1. Where defined, the channel order follows SMPTE/ITU-R recommendations. The assignments are as follows:
64    ///   - 1 channel: mono
65    ///   - 2 channels: left, right
66    ///   - 3 channels: left, right, center
67    ///   - 4 channels: front left, front right, back left, back right
68    ///   - 5 channels: front left, front right, front center, back/surround left, back/surround right
69    ///   - 6 channels: front left, front right, front center, LFE, back/surround left, back/surround right
70    ///   - 7 channels: front left, front right, front center, LFE, back center, side left, side right
71    ///   - 8 channels: front left, front right, front center, LFE, back left, back right, side left, side right
72    /// - `1000` : left/side stereo: channel 0 is the left channel, channel 1 is the side(difference) channel
73    /// - `1001` : right/side stereo: channel 0 is the side(difference) channel, channel 1 is the right channel
74    /// - `1010` : mid/side stereo: channel 0 is the mid(average) channel, channel 1 is the side(difference) channel
75    /// - `1011-1111` : reserved
76    pub channel_assignment: ChannelAssignment,
77    /// <3> Sample size in bits:
78    /// `000` : get from STREAMINFO metadata block
79    /// `001` : 8 bits per sample
80    /// `010` : 12 bits per sample
81    /// `011` : reserved
82    /// `100` : 16 bits per sample
83    /// `101` : 20 bits per sample
84    /// `110` : 24 bits per sample
85    /// `111` : reserved
86    pub sample_size: Option<u8>,
87    // <?> if(blocksize bits == 011x) 8/16 bit (blocksize-1)
88    // <?> if(sample rate bits == 11xx) 8/16 bit sample rate
89    /// <8> CRC-8 (polynomial = x^8 + x^2 + x^1 + x^0, initialized with 0) of everything before the crc, including the sync code
90    pub crc: u8,
91}
92
93#[derive(Debug)]
94pub enum BlockStrategy {
95    /// <8-48>:"UTF-8" coded frame number (decoded number is 31 bits)
96    Fixed(u32),
97    /// <8-56>:"UTF-8" coded sample number (decoded number is 36 bits)
98    Variable(u64),
99}
100
101#[derive(Debug)]
102pub enum SampleRate {
103    Inherit,
104    Rate88200,
105    Rate176400,
106    Rate192000,
107    Rate8000,
108    Rate16000,
109    Rate22050,
110    Rate24000,
111    Rate32000,
112    Rate44100,
113    Rate48000,
114    Rate96000,
115    Custom(u64),
116}
117
118#[derive(Debug)]
119pub enum ChannelAssignment {
120    Independent(u8),
121    LeftSide,
122    RightSide,
123    MidSide,
124    Reserved(u8),
125}
126
127#[derive(Debug)]
128pub struct SubFrame {
129    // <1> Zero bit padding, to prevent sync-fooling string of 1s
130    /// <6> Subframe type:
131    /// `000000` : SUBFRAME_CONSTANT
132    /// `000001` : SUBFRAME_VERBATIM
133    /// `00001x` : reserved
134    /// `0001xx` : reserved
135    /// `001xxx` : if(xxx <= 4) SUBFRAME_FIXED, xxx=order ; else reserved
136    /// `01xxxx` : reserved
137    /// `1xxxxx` : SUBFRAME_LPC, xxxxx=order-1
138    pub content: SubframeType,
139    /// <1+k> 'Wasted bits-per-sample' flag:
140    /// - `0` : no wasted bits-per-sample in source subblock, k=0
141    /// - `1` : k wasted bits-per-sample in source subblock, k-1 follows, unary coded; e.g. k=3 => 001 follows, k=7 => 0000001 follows.
142    pub wasted_bits: u32,
143}
144
145#[derive(Debug)]
146pub enum SubframeType {
147    /// <n> Unencoded constant value of the subblock, n = frame's bits-per-sample.
148    Constant(i32),
149    Fixed(SubFrameFixed),
150    LPC(SubFrameLPC),
151    /// <n*i> Unencoded subblock; n = frame's bits-per-sample, i = frame's blocksize.
152    Verbatim(Vec<u8>),
153}
154
155#[derive(Debug)]
156pub struct SubFrameFixed {
157    // TODO: check type
158    /// <n> Unencoded warm-up samples (n = frame's bits-per-sample * predictor order).
159    pub warm_up: Vec<i32>,
160    /// Encoded residual
161    pub residual: Residual,
162}
163
164#[derive(Debug)]
165pub struct SubFrameLPC {
166    /// <n> Unencoded warm-up samples (n = frame's bits-per-sample * lpc order).
167    pub warm_up: Vec<i32>,
168    /// <4> (Quantized linear predictor coefficients' precision in bits)-1 (1111 = invalid).
169    pub qlp_coeff_prediction: u8,
170    /// <5> Quantized linear predictor coefficient shift needed in bits (NOTE: this number is signed two's-complement).
171    pub qlp_shift: u8,
172    /// <n> Unencoded predictor coefficients (n = qlp coeff precision * lpc order) (NOTE: the coefficients are signed two's-complement).
173    pub qlp_coeff: Vec<u8>,
174    /// Encoded residual
175    pub residual: Residual,
176}
177
178/// <2> Residual coding method:
179/// `00` : partitioned Rice coding with 4-bit Rice parameter;
180///      RESIDUAL_CODING_METHOD_PARTITIONED_RICE follows
181/// `01` : partitioned Rice coding with 5-bit Rice parameter;
182///      RESIDUAL_CODING_METHOD_PARTITIONED_RICE2 follows
183/// `10-11` : reserved
184#[derive(Debug)]
185pub enum Residual {
186    Rice(ResidualCodingMethodPartitionedRice),
187    Rice2(ResidualCodingMethodPartitionedRice),
188    /// false: 10
189    /// true: 11
190    Reserved(bool),
191}
192
193#[derive(Debug)]
194pub struct ResidualCodingMethodPartitionedRice {
195    /// <4> Partition order.
196    pub order: u8,
197    /// <RICE_PARTITION+> There will be 2^order partitions.
198    pub partitons: Vec<RicePartition>,
199}
200
201#[derive(Debug)]
202pub struct RicePartition {
203    /// Encoding parameter:
204    pub parameter: RiceParameter,
205    /// Encoded residual. The number of samples (n) in the partition is determined as follows:
206    /// - if the partition order is zero, n = frame's blocksize - predictor order
207    /// - else if this is not the first partition of the subframe, n = (frame's blocksize / (2^partition order))
208    /// - else n = (frame's blocksize / (2^partition order)) - predictor order
209    pub encoded_residual: Vec<u8>,
210}
211
212#[derive(Debug)]
213pub enum RiceParameter {
214    /// Rice:
215    /// <4> `0000-1110` : Rice parameter.
216    /// Rice2:
217    /// <5> `00000-11110` : Rice parameter.
218    Parameter(u8),
219    /// Rice:
220    /// <4+5> `1111` : Escape code, meaning the partition is in unencoded binary form using n bits per sample; n follows as a 5-bit number.
221    /// Rice2:
222    /// <5+5> `11111` : Escape code, meaning the partition is in unencoded binary form using n bits per sample; n follows as a 5-bit number.
223    /// n is stored
224    Escape(u8),
225}