forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB214cdf2Tahinli3 months ago821 commits

component.rs112 lines, 3.6 KiBHistoryRawDownload
1//! `flacenc::component`: the encoded stream and its `STREAMINFO`.
2//!
3//! The incumbent models every FLAC component as an addressable value and
4//! writes them out on demand. Ours holds the encoded bytes and the header
5//! fields a caller is allowed to correct afterwards — which is the whole of
6//! what the replica does with the tree.
7
8use crate::bitsink::BitSink;
9use crate::error::{OutputError, VerifyError};
10
11/// Something that can state its size and write itself to a [`BitSink`].
12pub trait BitRepr {
13 /// Bits this component occupies.
14 fn count_bits(&self) -> usize;
15
16 /// Write the component out.
17 ///
18 /// # Errors
19 /// When the sink fails, or the component holds a value FLAC cannot express.
20 fn write<S: BitSink>(&self, dest: &mut S) -> Result<(), OutputError<S>>;
21}
22
23/// A complete encoded FLAC stream.
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct Stream {
26 bytes: Vec<u8>,
27 stream_info: StreamInfo,
28}
29
30/// Offset of the `STREAMINFO` payload: `fLaC` plus the 4-byte block header.
31const STREAM_INFO_AT: usize = 8;
32
33impl Stream {
34 pub(crate) fn from_bytes(bytes: Vec<u8>) -> Stream {
35 let min = u16::from_be_bytes([bytes[STREAM_INFO_AT], bytes[STREAM_INFO_AT + 1]]);
36 let max = u16::from_be_bytes([bytes[STREAM_INFO_AT + 2], bytes[STREAM_INFO_AT + 3]]);
37 Stream {
38 bytes,
39 stream_info: StreamInfo {
40 min_block_size: min,
41 max_block_size: max,
42 },
43 }
44 }
45
46 /// The stream's `STREAMINFO`, mutable.
47 pub fn stream_info_mut(&mut self) -> &mut StreamInfo {
48 &mut self.stream_info
49 }
50
51 /// The stream's `STREAMINFO`.
52 pub fn stream_info(&self) -> &StreamInfo {
53 &self.stream_info
54 }
55}
56
57impl BitRepr for Stream {
58 fn count_bits(&self) -> usize {
59 self.bytes.len() * 8
60 }
61
62 fn write<S: BitSink>(&self, dest: &mut S) -> Result<(), OutputError<S>> {
63 // The declared block sizes are the one field a caller may have changed
64 // after encoding, so they are patched in on the way out.
65 let mut bytes = self.bytes.clone();
66 bytes[STREAM_INFO_AT..STREAM_INFO_AT + 2]
67 .copy_from_slice(&self.stream_info.min_block_size.to_be_bytes());
68 bytes[STREAM_INFO_AT + 2..STREAM_INFO_AT + 4]
69 .copy_from_slice(&self.stream_info.max_block_size.to_be_bytes());
70 dest.write_bytes(&bytes)
71 .map_err(|e| OutputError::new(e.to_string()))
72 }
73}
74
75/// The `STREAMINFO` fields a caller may correct after encoding.
76#[derive(Debug, Clone, Copy, PartialEq, Eq)]
77pub struct StreamInfo {
78 min_block_size: u16,
79 max_block_size: u16,
80}
81
82impl StreamInfo {
83 /// State the stream's block sizes.
84 ///
85 /// # Errors
86 /// When either value is not a legal block size.
87 pub fn set_block_sizes(
88 &mut self,
89 min_value: usize,
90 max_value: usize,
91 ) -> Result<(), VerifyError> {
92 self.min_block_size = u16::try_from(min_value)
93 .ok()
94 .filter(|&n| n >= 16)
95 .ok_or_else(|| VerifyError::new("min_block_size", "must be a valid block size."))?;
96 self.max_block_size = u16::try_from(max_value)
97 .ok()
98 .filter(|&n| n >= 16)
99 .ok_or_else(|| VerifyError::new("max_block_size", "must be a valid block size."))?;
100 Ok(())
101 }
102
103 /// Smallest block size stated by the stream.
104 pub fn min_block_size(&self) -> usize {
105 usize::from(self.min_block_size)
106 }
107
108 /// Largest block size stated by the stream.
109 pub fn max_block_size(&self) -> usize {
110 usize::from(self.max_block_size)
111 }
112}