opus_codec/
lib.rs

1//! Safe, ergonomic wrappers around libopus for encoding/decoding Opus audio.
2#![warn(missing_docs)]
3#![warn(clippy::all)]
4#![warn(clippy::pedantic)]
5#![warn(clippy::cargo)]
6#![allow(clippy::cast_possible_wrap)]
7#![allow(clippy::cast_possible_truncation)]
8
9// Include the generated bindings
10#[allow(warnings)]
11#[allow(clippy::all)]
12mod bindings {
13    include!("bindings.rs");
14}
15
16mod alloc;
17pub mod constants;
18pub mod decoder;
19#[cfg(feature = "dred")]
20/// Deep Redundancy (DRED) decoder support.
21pub mod dred;
22pub mod encoder;
23pub mod error;
24pub mod multistream;
25pub mod packet;
26pub mod projection;
27mod raw;
28pub mod repacketizer;
29pub mod types;
30
31pub use alloc::AlignedBuffer;
32pub use constants::{MAX_FRAME_SAMPLES_48KHZ, MAX_PACKET_DURATION_MS, max_frame_samples_for};
33pub use decoder::Decoder;
34#[cfg(feature = "dred")]
35pub use dred::{DredDecoder, DredState};
36pub use encoder::Encoder;
37pub use error::{Error, Result};
38pub use multistream::{Mapping, MultistreamDecoder, MultistreamEncoder};
39pub use packet::{
40    packet_bandwidth, packet_channels, packet_frame_count, packet_has_lbrr, packet_parse,
41    packet_sample_count, packet_samples_per_frame, soft_clip,
42};
43pub use projection::{ProjectionDecoder, ProjectionEncoder};
44pub use repacketizer::Repacketizer;
45pub use types::{
46    Application, Bandwidth, Bitrate, Channels, Complexity, ExpertFrameDuration, FrameSize,
47    SampleRate, Signal,
48};
49
50#[doc(hidden)]
51pub use bindings::*;
52
53pub(crate) use raw::RawHandle;
54
55#[derive(Clone, Copy, Debug, PartialEq, Eq)]
56pub(crate) enum Ownership {
57    Owned,
58    Borrowed,
59}
60
61#[inline]
62pub(crate) fn opus_ptr_is_aligned(ptr: *const u8) -> bool {
63    // libopus aligns internal state to pointer-sized alignment (opus_private.h align()).
64    (ptr as usize).is_multiple_of(std::mem::align_of::<usize>())
65}
66
67/// Returns the bundled libopus version string of this crate.
68#[must_use]
69pub fn version() -> &'static str {
70    "1.5.2"
71}
72
73/// Returns the runtime libopus version string from the linked C library.
74#[must_use]
75pub fn runtime_version() -> &'static str {
76    unsafe {
77        let ptr = crate::bindings::opus_get_version_string();
78        if ptr.is_null() {
79            return "";
80        }
81        std::ffi::CStr::from_ptr(ptr).to_str().unwrap_or("")
82    }
83}
84
85/// Returns a human-readable string for a libopus error code (via runtime library).
86#[must_use]
87pub fn strerror(code: i32) -> &'static str {
88    unsafe {
89        let ptr = crate::bindings::opus_strerror(code);
90        if ptr.is_null() {
91            return "";
92        }
93        std::ffi::CStr::from_ptr(ptr).to_str().unwrap_or("")
94    }
95}