Skip to main content

Decoder

Struct Decoder 

Source
pub struct Decoder { /* private fields */ }

Implementations§

Source§

impl Decoder

Source

pub fn new(config: DecoderConfig) -> Result<Self>

Examples found in repository?
examples/decode_stream.rs (line 13)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let input = env::args().nth(1).ok_or_else(usage)?;
10    let path = Path::new(&input);
11    let data = fs::read(path)?;
12
13    let mut decoder = Decoder::new(DecoderConfig::default())?;
14    let mut frame_count = 0usize;
15    let mut pcm_bytes = 0usize;
16    let mut offset = 0usize;
17
18    while offset < data.len() {
19        let end = (offset + 256).min(data.len());
20        match decoder.decode_stream_chunk(&data[offset..end])? {
21            DecodeStatus::Frame(frame) => {
22                frame_count += 1;
23                pcm_bytes += frame.pcm.len();
24                println!(
25                    "frame {}: {} pcm bytes, {} Hz, {} ch",
26                    frame_count,
27                    frame.pcm.len(),
28                    frame.stream_info.sample_rate,
29                    frame.stream_info.channels
30                );
31            }
32            DecodeStatus::NeedMoreInput(progress) => {
33                if let Some(info) = progress.stream_info {
34                    println!(
35                        "need more input: initialized={}, {} Hz, {} ch",
36                        progress.initialized, info.sample_rate, info.channels
37                    );
38                } else {
39                    println!("need more input: initialized={}", progress.initialized);
40                }
41            }
42            DecodeStatus::EndOfStream => break,
43        }
44        offset = end;
45    }
46
47    loop {
48        match decoder.finish()? {
49            DecodeStatus::Frame(frame) => {
50                frame_count += 1;
51                pcm_bytes += frame.pcm.len();
52                println!(
53                    "flush frame {}: {} pcm bytes, {} Hz, {} ch",
54                    frame_count,
55                    frame.pcm.len(),
56                    frame.stream_info.sample_rate,
57                    frame.stream_info.channels
58                );
59            }
60            DecodeStatus::NeedMoreInput(_) => continue,
61            DecodeStatus::EndOfStream => break,
62        }
63    }
64
65    println!("decoded frames: {}", frame_count);
66    println!("decoded pcm bytes: {}", pcm_bytes);
67    Ok(())
68}
More examples
Hide additional examples
examples/file_info.rs (line 13)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let input = env::args().nth(1).ok_or_else(usage)?;
10    let input_path = Path::new(&input);
11    let data = fs::read(input_path)?;
12
13    let mut decoder = Decoder::new(DecoderConfig::default())?;
14    let version = decoder.version().clone();
15
16    println!("file: {}", input_path.display());
17    println!("size: {} bytes", data.len());
18    println!("decoder: {} {}", version.name, version.version);
19    println!("decoder input capacity: {} bytes", decoder.input_capacity());
20
21    if data.is_empty() {
22        println!("stream info: file is empty");
23        return Ok(());
24    }
25
26    let probe_len = data.len();
27    let probe = &data;
28
29    match decoder.probe_stream_info(probe) {
30        Ok(info) => {
31            println!("probe bytes: {}", probe_len);
32            println!("sample rate: {} Hz", info.sample_rate);
33            println!("channels: {}", info.channels);
34            println!("channel mask: 0x{:x}", info.channel_mask);
35            println!("channel mode: {:?}", info.channel_mode);
36            println!("pcm word size: {} bits", info.pcm_word_size);
37            println!("audio object type: {}", info.audio_object_type);
38            println!("sbr mode: {:?}", info.sbr_mode);
39            println!("drc active: {}", info.drc_active);
40            println!(
41                "drc target loudness: {}",
42                info.drc_target_loudness
43                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
44            );
45            println!(
46                "drc loudness norm: {}",
47                info.drc_loudness_norm
48                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
49            );
50            println!(
51                "loudness leveling: {}",
52                info.loudness_leveling
53                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
54            );
55            println!(
56                "preroll frames: {}",
57                info.preroll_frames
58                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
59            );
60            println!(
61                "gain payload bytes: {}",
62                info.gain_payload_len
63                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
64            );
65            match estimate_adts_bitrate(&data, info.sample_rate, info.sbr_mode) {
66                Some(bitrate) => println!("bit rate: {} bps", bitrate),
67                None => println!("bit rate: unavailable"),
68            }
69        }
70        Err(err) => {
71            println!("stream info: unavailable");
72            println!("decoder error: {err}");
73            println!("probe bytes: {}", probe_len);
74        }
75    }
76
77    Ok(())
78}
Source

pub fn config(&self) -> &DecoderConfig

Source

pub fn version(&self) -> &VersionInfo

Examples found in repository?
examples/file_info.rs (line 14)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let input = env::args().nth(1).ok_or_else(usage)?;
10    let input_path = Path::new(&input);
11    let data = fs::read(input_path)?;
12
13    let mut decoder = Decoder::new(DecoderConfig::default())?;
14    let version = decoder.version().clone();
15
16    println!("file: {}", input_path.display());
17    println!("size: {} bytes", data.len());
18    println!("decoder: {} {}", version.name, version.version);
19    println!("decoder input capacity: {} bytes", decoder.input_capacity());
20
21    if data.is_empty() {
22        println!("stream info: file is empty");
23        return Ok(());
24    }
25
26    let probe_len = data.len();
27    let probe = &data;
28
29    match decoder.probe_stream_info(probe) {
30        Ok(info) => {
31            println!("probe bytes: {}", probe_len);
32            println!("sample rate: {} Hz", info.sample_rate);
33            println!("channels: {}", info.channels);
34            println!("channel mask: 0x{:x}", info.channel_mask);
35            println!("channel mode: {:?}", info.channel_mode);
36            println!("pcm word size: {} bits", info.pcm_word_size);
37            println!("audio object type: {}", info.audio_object_type);
38            println!("sbr mode: {:?}", info.sbr_mode);
39            println!("drc active: {}", info.drc_active);
40            println!(
41                "drc target loudness: {}",
42                info.drc_target_loudness
43                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
44            );
45            println!(
46                "drc loudness norm: {}",
47                info.drc_loudness_norm
48                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
49            );
50            println!(
51                "loudness leveling: {}",
52                info.loudness_leveling
53                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
54            );
55            println!(
56                "preroll frames: {}",
57                info.preroll_frames
58                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
59            );
60            println!(
61                "gain payload bytes: {}",
62                info.gain_payload_len
63                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
64            );
65            match estimate_adts_bitrate(&data, info.sample_rate, info.sbr_mode) {
66                Some(bitrate) => println!("bit rate: {} bps", bitrate),
67                None => println!("bit rate: unavailable"),
68            }
69        }
70        Err(err) => {
71            println!("stream info: unavailable");
72            println!("decoder error: {err}");
73            println!("probe bytes: {}", probe_len);
74        }
75    }
76
77    Ok(())
78}
Source

pub fn input_capacity(&self) -> usize

Examples found in repository?
examples/file_info.rs (line 19)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let input = env::args().nth(1).ok_or_else(usage)?;
10    let input_path = Path::new(&input);
11    let data = fs::read(input_path)?;
12
13    let mut decoder = Decoder::new(DecoderConfig::default())?;
14    let version = decoder.version().clone();
15
16    println!("file: {}", input_path.display());
17    println!("size: {} bytes", data.len());
18    println!("decoder: {} {}", version.name, version.version);
19    println!("decoder input capacity: {} bytes", decoder.input_capacity());
20
21    if data.is_empty() {
22        println!("stream info: file is empty");
23        return Ok(());
24    }
25
26    let probe_len = data.len();
27    let probe = &data;
28
29    match decoder.probe_stream_info(probe) {
30        Ok(info) => {
31            println!("probe bytes: {}", probe_len);
32            println!("sample rate: {} Hz", info.sample_rate);
33            println!("channels: {}", info.channels);
34            println!("channel mask: 0x{:x}", info.channel_mask);
35            println!("channel mode: {:?}", info.channel_mode);
36            println!("pcm word size: {} bits", info.pcm_word_size);
37            println!("audio object type: {}", info.audio_object_type);
38            println!("sbr mode: {:?}", info.sbr_mode);
39            println!("drc active: {}", info.drc_active);
40            println!(
41                "drc target loudness: {}",
42                info.drc_target_loudness
43                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
44            );
45            println!(
46                "drc loudness norm: {}",
47                info.drc_loudness_norm
48                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
49            );
50            println!(
51                "loudness leveling: {}",
52                info.loudness_leveling
53                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
54            );
55            println!(
56                "preroll frames: {}",
57                info.preroll_frames
58                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
59            );
60            println!(
61                "gain payload bytes: {}",
62                info.gain_payload_len
63                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
64            );
65            match estimate_adts_bitrate(&data, info.sample_rate, info.sbr_mode) {
66                Some(bitrate) => println!("bit rate: {} bps", bitrate),
67                None => println!("bit rate: unavailable"),
68            }
69        }
70        Err(err) => {
71            println!("stream info: unavailable");
72            println!("decoder error: {err}");
73            println!("probe bytes: {}", probe_len);
74        }
75    }
76
77    Ok(())
78}
Source

pub fn stream_info(&self) -> Option<&StreamInfo>

Source

pub fn probe_stream_info(&mut self, data: &[u8]) -> Result<StreamInfo>

Examples found in repository?
examples/file_info.rs (line 29)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let input = env::args().nth(1).ok_or_else(usage)?;
10    let input_path = Path::new(&input);
11    let data = fs::read(input_path)?;
12
13    let mut decoder = Decoder::new(DecoderConfig::default())?;
14    let version = decoder.version().clone();
15
16    println!("file: {}", input_path.display());
17    println!("size: {} bytes", data.len());
18    println!("decoder: {} {}", version.name, version.version);
19    println!("decoder input capacity: {} bytes", decoder.input_capacity());
20
21    if data.is_empty() {
22        println!("stream info: file is empty");
23        return Ok(());
24    }
25
26    let probe_len = data.len();
27    let probe = &data;
28
29    match decoder.probe_stream_info(probe) {
30        Ok(info) => {
31            println!("probe bytes: {}", probe_len);
32            println!("sample rate: {} Hz", info.sample_rate);
33            println!("channels: {}", info.channels);
34            println!("channel mask: 0x{:x}", info.channel_mask);
35            println!("channel mode: {:?}", info.channel_mode);
36            println!("pcm word size: {} bits", info.pcm_word_size);
37            println!("audio object type: {}", info.audio_object_type);
38            println!("sbr mode: {:?}", info.sbr_mode);
39            println!("drc active: {}", info.drc_active);
40            println!(
41                "drc target loudness: {}",
42                info.drc_target_loudness
43                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
44            );
45            println!(
46                "drc loudness norm: {}",
47                info.drc_loudness_norm
48                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
49            );
50            println!(
51                "loudness leveling: {}",
52                info.loudness_leveling
53                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
54            );
55            println!(
56                "preroll frames: {}",
57                info.preroll_frames
58                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
59            );
60            println!(
61                "gain payload bytes: {}",
62                info.gain_payload_len
63                    .map_or_else(|| "unavailable".to_string(), |value| value.to_string())
64            );
65            match estimate_adts_bitrate(&data, info.sample_rate, info.sbr_mode) {
66                Some(bitrate) => println!("bit rate: {} bps", bitrate),
67                None => println!("bit rate: unavailable"),
68            }
69        }
70        Err(err) => {
71            println!("stream info: unavailable");
72            println!("decoder error: {err}");
73            println!("probe bytes: {}", probe_len);
74        }
75    }
76
77    Ok(())
78}
Source

pub fn decode(&mut self, input: &[u8]) -> Result<DecodedFrame>

Source

pub fn decode_stream_chunk(&mut self, input: &[u8]) -> Result<DecodeStatus>

Examples found in repository?
examples/decode_stream.rs (line 20)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let input = env::args().nth(1).ok_or_else(usage)?;
10    let path = Path::new(&input);
11    let data = fs::read(path)?;
12
13    let mut decoder = Decoder::new(DecoderConfig::default())?;
14    let mut frame_count = 0usize;
15    let mut pcm_bytes = 0usize;
16    let mut offset = 0usize;
17
18    while offset < data.len() {
19        let end = (offset + 256).min(data.len());
20        match decoder.decode_stream_chunk(&data[offset..end])? {
21            DecodeStatus::Frame(frame) => {
22                frame_count += 1;
23                pcm_bytes += frame.pcm.len();
24                println!(
25                    "frame {}: {} pcm bytes, {} Hz, {} ch",
26                    frame_count,
27                    frame.pcm.len(),
28                    frame.stream_info.sample_rate,
29                    frame.stream_info.channels
30                );
31            }
32            DecodeStatus::NeedMoreInput(progress) => {
33                if let Some(info) = progress.stream_info {
34                    println!(
35                        "need more input: initialized={}, {} Hz, {} ch",
36                        progress.initialized, info.sample_rate, info.channels
37                    );
38                } else {
39                    println!("need more input: initialized={}", progress.initialized);
40                }
41            }
42            DecodeStatus::EndOfStream => break,
43        }
44        offset = end;
45    }
46
47    loop {
48        match decoder.finish()? {
49            DecodeStatus::Frame(frame) => {
50                frame_count += 1;
51                pcm_bytes += frame.pcm.len();
52                println!(
53                    "flush frame {}: {} pcm bytes, {} Hz, {} ch",
54                    frame_count,
55                    frame.pcm.len(),
56                    frame.stream_info.sample_rate,
57                    frame.stream_info.channels
58                );
59            }
60            DecodeStatus::NeedMoreInput(_) => continue,
61            DecodeStatus::EndOfStream => break,
62        }
63    }
64
65    println!("decoded frames: {}", frame_count);
66    println!("decoded pcm bytes: {}", pcm_bytes);
67    Ok(())
68}
Source

pub fn finish(&mut self) -> Result<DecodeStatus>

Examples found in repository?
examples/decode_stream.rs (line 48)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let input = env::args().nth(1).ok_or_else(usage)?;
10    let path = Path::new(&input);
11    let data = fs::read(path)?;
12
13    let mut decoder = Decoder::new(DecoderConfig::default())?;
14    let mut frame_count = 0usize;
15    let mut pcm_bytes = 0usize;
16    let mut offset = 0usize;
17
18    while offset < data.len() {
19        let end = (offset + 256).min(data.len());
20        match decoder.decode_stream_chunk(&data[offset..end])? {
21            DecodeStatus::Frame(frame) => {
22                frame_count += 1;
23                pcm_bytes += frame.pcm.len();
24                println!(
25                    "frame {}: {} pcm bytes, {} Hz, {} ch",
26                    frame_count,
27                    frame.pcm.len(),
28                    frame.stream_info.sample_rate,
29                    frame.stream_info.channels
30                );
31            }
32            DecodeStatus::NeedMoreInput(progress) => {
33                if let Some(info) = progress.stream_info {
34                    println!(
35                        "need more input: initialized={}, {} Hz, {} ch",
36                        progress.initialized, info.sample_rate, info.channels
37                    );
38                } else {
39                    println!("need more input: initialized={}", progress.initialized);
40                }
41            }
42            DecodeStatus::EndOfStream => break,
43        }
44        offset = end;
45    }
46
47    loop {
48        match decoder.finish()? {
49            DecodeStatus::Frame(frame) => {
50                frame_count += 1;
51                pcm_bytes += frame.pcm.len();
52                println!(
53                    "flush frame {}: {} pcm bytes, {} Hz, {} ch",
54                    frame_count,
55                    frame.pcm.len(),
56                    frame.stream_info.sample_rate,
57                    frame.stream_info.channels
58                );
59            }
60            DecodeStatus::NeedMoreInput(_) => continue,
61            DecodeStatus::EndOfStream => break,
62        }
63    }
64
65    println!("decoded frames: {}", frame_count);
66    println!("decoded pcm bytes: {}", pcm_bytes);
67    Ok(())
68}
Source

pub fn signal_end_of_input(&mut self) -> Result<()>

Trait Implementations§

Source§

impl Debug for Decoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.