oxideav-g711
Pure-Rust ITU-T G.711 codec — both µ-law and A-law variants,
decoder + encoder. Spec-exact lookup tables, deterministic per-sample
companding, bit-exact against the reference formulas in G.711 §2 / §3.
Zero C dependencies, no FFI, no *-sys crates.
Part of the oxideav framework but usable standalone.
Installation
[]
= "0.1"
= "0.1"
= "0.0"
Quick use
G.711 is 1 byte per input sample, stateless. Every encoded byte decodes to exactly one S16 PCM sample and vice versa. The spec defines it at 8 kHz mono (PSTN) but the implementation is rate- and channel-independent — interleaved S16 input with any channel count round-trips through the same byte-per-sample mapping.
use CodecRegistry;
use ;
let mut reg = new;
register;
let mut params = audio;
params.sample_rate = Some;
params.channels = Some;
let mut dec = reg.make_decoder?;
dec.send_packet?;
let Audio = dec.receive_frame? else ;
// `a.data[0]` is interleaved S16 PCM.
# Ok::
Encoder is symmetric — build with reg.make_encoder(¶ms), feed
Frame::Audio with S16 PCM, get companded Packets back. One output
byte per input S16 sample, preserving interleave order across channels.
Codec IDs
- µ-law:
"pcm_mulaw"(aliases:"g711u","ulaw") - A-law:
"pcm_alaw"(aliases:"g711a","alaw")
Channels and sample rate
Both variants accept any channel count ≥ 1 and any sample rate. The
packet-to-frame mapping assumes interleaved bytes in the same order as
the S16 PCM: ch0 ch1 … chN ch0 ch1 …. Decoder packets whose length
is not a multiple of the channel count are rejected.
Going deeper — bypassing the registry
If you just want the byte-to-sample mapping, skip the trait surface entirely:
use ;
let pcm: i16 = decode_sample;
let byte: u8 = encode_sample;
The UlawDecoder / UlawEncoder / AlawDecoder / AlawEncoder
structs are also publicly constructible via mulaw::make_decoder /
alaw::make_decoder / etc. for cases where you want full control over
construction without the registry lookup.
Properties verified by the test suite
- Decode tables match the ITU-T G.711 §2 / §3 reference formulas for all 256 bytes of both laws.
- Encode is bit-exact against the reference Sun/ANSI formulas for all 65 536 S16 inputs of both laws.
- Encode / decode / encode is idempotent for every S16 sample (the quantiser is a projection).
- Decode / encode is a round-trip identity for every byte, except µ-law bytes 0x7F (negative zero) which the encoder canonicalises to 0xFF (positive zero) — both decode to linear 0 per the spec.
- Sign symmetry: byte
band byteb ^ 0x80decode to exact negatives. - Monotonicity: the companded transfer function is monotonic across the whole S16 range.
- Multichannel round-trip (1, 2, 6, 8 channels) through the trait surface returns the same per-sample quantisation as direct calls.
License
MIT — see LICENSE.