Skip to main content

codec_core/codecs/
mod.rs

1//! # Audio Codec Implementations
2//!
3//! This module contains G.711 audio codec implementation for `VoIP` applications.
4//!
5//! ## Available Codecs
6//!
7//! ### G.711 (PCMU/PCMA) - [`g711`]
8//! - **Standard**: ITU-T G.711
9//! - **Sample Rate**: 8 kHz
10//! - **Bitrate**: 64 kbps
11//! - **Quality**: ~37 dB SNR
12//! - **Use Case**: Standard telephony
13//! - **Variants**: μ-law (PCMU), A-law (PCMA)
14//!
15//! ## Testing
16//!
17//! G.711 is validated with real speech samples through WAV roundtrip tests:
18//! - Downloads reference audio samples
19//! - Round-trip encoding and decoding validation
20//! - Signal-to-Noise Ratio (SNR) measurement
21//!
22//! ## Usage Examples
23//!
24//! ### Using the Codec Factory
25//! ```rust
26//! # #[cfg(feature = "g711")]
27//! # {
28//! use codec_core::codecs::CodecFactory;
29//! use codec_core::types::{CodecConfig, CodecType, SampleRate};
30//!
31//! // Create any codec through the factory
32//! let config = CodecConfig::new(CodecType::G711Pcmu)
33//!     .with_sample_rate(SampleRate::Rate8000);
34//! let mut codec = CodecFactory::create(config)?;
35//!
36//! // Use unified interface
37//! let samples = vec![0i16; 160];
38//! let encoded = codec.encode(&samples)?;
39//! let decoded = codec.decode(&encoded)?;
40//! # }
41//! # Ok::<(), Box<dyn std::error::Error>>(())
42//! ```
43//!
44//! ### Direct Codec Access
45//! ```rust
46//! # #[cfg(feature = "g711")]
47//! # {
48//! use codec_core::codecs::g711::{G711Codec, G711Variant};
49//!
50//! // Direct instantiation
51//! let mut g711_ulaw = G711Codec::new(G711Variant::MuLaw);
52//! let mut g711_alaw = G711Codec::new(G711Variant::ALaw);
53//! # }
54//! # Ok::<(), Box<dyn std::error::Error>>(())
55//! ```
56//!
57//! ## Testing & Validation
58//!
59//! All codecs include comprehensive test suites:
60//! - ITU-T compliance validation
61//! - Real audio roundtrip tests
62//! - Performance benchmarks
63//! - Quality measurements (SNR)
64//!
65//! ```bash
66//! # Test all codecs
67//! cargo test
68//!
69//! # Test with real audio (downloads speech samples)
70//! cargo test wav_roundtrip_test -- --nocapture
71//! ```
72
73use crate::error::{CodecError, Result};
74use crate::types::{AudioCodec, CodecConfig, CodecInfo, CodecType};
75use std::collections::HashMap;
76
77// Codec implementations
78#[cfg(feature = "g711")]
79pub mod g711;
80
81#[cfg(feature = "g729")]
82pub mod g729;
83
84#[cfg(feature = "opus")]
85pub mod opus;
86
87#[cfg(any(feature = "amr-nb", feature = "amr-wb"))]
88pub mod amr;
89
90/// Codec factory for creating codec instances
91pub struct CodecFactory;
92
93impl CodecFactory {
94    /// Create a codec instance from configuration
95    ///
96    /// # Errors
97    ///
98    /// Returns an error when the configuration is invalid, the codec feature
99    /// is disabled, or codec construction fails.
100    // Preserve the public by-value constructor in a build where every codec
101    // branch is compiled out; feature-enabled branches transfer ownership to
102    // their concrete codec constructors.
103    #[cfg_attr(
104        not(any(feature = "g711", feature = "g729", feature = "opus")),
105        allow(clippy::needless_pass_by_value)
106    )]
107    pub fn create(config: CodecConfig) -> Result<Box<dyn AudioCodec>> {
108        // Validate configuration first
109        config.validate()?;
110
111        match config.codec_type {
112            #[cfg(feature = "g711")]
113            CodecType::G711Pcmu => {
114                let codec = g711::G711Codec::new_pcmu(config)?;
115                Ok(Box::new(codec))
116            }
117
118            #[cfg(feature = "g711")]
119            CodecType::G711Pcma => {
120                let codec = g711::G711Codec::new_pcma(config)?;
121                Ok(Box::new(codec))
122            }
123
124            #[cfg(feature = "g729")]
125            CodecType::G729 | CodecType::G729A | CodecType::G729BA => {
126                let codec = g729::G729Codec::new(config)?;
127                Ok(Box::new(codec))
128            }
129
130            #[cfg(feature = "opus")]
131            CodecType::Opus => {
132                let codec = opus::OpusCodec::new(config)?;
133                Ok(Box::new(codec))
134            }
135
136            #[cfg(feature = "amr-nb")]
137            CodecType::AmrNb => {
138                let codec = amr::AmrCodec::new(&config)?;
139                Ok(Box::new(codec))
140            }
141
142            #[cfg(feature = "amr-wb")]
143            CodecType::AmrWb => {
144                let codec = amr::AmrCodec::new(&config)?;
145                Ok(Box::new(codec))
146            }
147
148            codec_type => Err(CodecError::feature_not_enabled(format!(
149                "Codec {codec_type} not enabled in build features"
150            ))),
151        }
152    }
153
154    /// Create a codec by name
155    ///
156    /// # Errors
157    ///
158    /// Returns an error when `name` is unknown, its feature is disabled, or
159    /// the configuration is invalid.
160    pub fn create_by_name(name: &str, config: CodecConfig) -> Result<Box<dyn AudioCodec>> {
161        let codec_type = match normalize_codec_name(name).as_str() {
162            "PCMU" => CodecType::G711Pcmu,
163            "PCMA" => CodecType::G711Pcma,
164            "G729" => CodecType::G729,
165            "G729A" => CodecType::G729A,
166            "G729AB" | "G729BA" => CodecType::G729BA,
167            "OPUS" => CodecType::Opus,
168            "AMR" => CodecType::AmrNb,
169            "AMR-WB" | "AMRWB" => CodecType::AmrWb,
170            _ => return Err(CodecError::unsupported_codec(name)),
171        };
172
173        let config = CodecConfig {
174            codec_type,
175            ..config
176        };
177
178        Self::create(config)
179    }
180
181    /// Create a codec by RTP payload type
182    ///
183    /// # Errors
184    ///
185    /// Returns an error when the payload type is unknown, its codec feature is
186    /// disabled, or the configuration is invalid.
187    pub fn create_by_payload_type(
188        payload_type: u8,
189        config: CodecConfig,
190    ) -> Result<Box<dyn AudioCodec>> {
191        let codec_type = match payload_type {
192            0 => CodecType::G711Pcmu,
193            8 => CodecType::G711Pcma,
194            18 => CodecType::G729,
195
196            _ => return Err(CodecError::unsupported_codec(format!("PT{payload_type}"))),
197        };
198
199        let config = CodecConfig {
200            codec_type,
201            ..config
202        };
203
204        Self::create(config)
205    }
206
207    /// Get all supported codec names
208    #[must_use]
209    pub fn supported_codecs() -> Vec<&'static str> {
210        vec![
211            #[cfg(feature = "g711")]
212            "PCMU",
213            #[cfg(feature = "g711")]
214            "PCMA",
215            #[cfg(feature = "g729")]
216            "G729",
217            #[cfg(feature = "g729")]
218            "G729A",
219            #[cfg(feature = "g729")]
220            "G729BA",
221            #[cfg(feature = "opus")]
222            "OPUS",
223            #[cfg(feature = "amr-nb")]
224            "AMR",
225            #[cfg(feature = "amr-wb")]
226            "AMR-WB",
227        ]
228    }
229
230    /// Check if a codec is supported
231    #[must_use]
232    pub fn is_supported(name: &str) -> bool {
233        let normalized = normalize_codec_name(name);
234        match normalized.as_str() {
235            #[cfg(feature = "g711")]
236            "PCMU" | "PCMA" => true,
237            #[cfg(feature = "g729")]
238            "G729" | "G729A" | "G729AB" | "G729BA" => true,
239            #[cfg(feature = "opus")]
240            "OPUS" => true,
241            #[cfg(feature = "amr-nb")]
242            "AMR" => true,
243            #[cfg(feature = "amr-wb")]
244            "AMR-WB" | "AMRWB" => true,
245            _ => false,
246        }
247    }
248}
249
250fn normalize_codec_name(name: &str) -> String {
251    name.to_ascii_uppercase().replace('.', "")
252}
253
254/// Codec registry for managing multiple codec instances
255pub struct CodecRegistry {
256    codecs: HashMap<String, Box<dyn AudioCodec>>,
257}
258
259impl CodecRegistry {
260    /// Create a new empty registry
261    #[must_use]
262    pub fn new() -> Self {
263        Self {
264            codecs: HashMap::new(),
265        }
266    }
267
268    /// Register a codec with a name
269    pub fn register(&mut self, name: String, codec: Box<dyn AudioCodec>) {
270        self.codecs.insert(name, codec);
271    }
272
273    /// Get a codec by name
274    #[must_use]
275    pub fn get(&self, name: &str) -> Option<&dyn AudioCodec> {
276        self.codecs.get(name).map(std::convert::AsRef::as_ref)
277    }
278
279    /// Get a mutable codec by name
280    pub fn get_mut(&mut self, name: &str) -> Option<&mut Box<dyn AudioCodec>> {
281        self.codecs.get_mut(name)
282    }
283
284    /// Remove a codec by name
285    pub fn remove(&mut self, name: &str) -> Option<Box<dyn AudioCodec>> {
286        self.codecs.remove(name)
287    }
288
289    /// List all registered codec names
290    #[must_use]
291    pub fn list_codecs(&self) -> Vec<&String> {
292        self.codecs.keys().collect()
293    }
294
295    /// Get the count of registered codecs
296    #[must_use]
297    pub fn len(&self) -> usize {
298        self.codecs.len()
299    }
300
301    /// Check if the registry is empty
302    #[must_use]
303    pub fn is_empty(&self) -> bool {
304        self.codecs.is_empty()
305    }
306
307    /// Clear all registered codecs
308    pub fn clear(&mut self) {
309        self.codecs.clear();
310    }
311}
312
313impl Default for CodecRegistry {
314    fn default() -> Self {
315        Self::new()
316    }
317}
318
319/// Codec capability information
320#[derive(Debug, Clone)]
321pub struct CodecCapabilities {
322    /// Available codec types
323    pub codec_types: Vec<CodecType>,
324    /// Codec information
325    pub codec_info: HashMap<CodecType, CodecInfo>,
326}
327
328/// Register the AMR variants enabled by feature flags.
329///
330/// Split out of [`CodecCapabilities::get_all`] to keep that function within the
331/// workspace line limit as codecs accumulate.
332#[cfg(any(feature = "amr-nb", feature = "amr-wb"))]
333fn add_amr_capabilities(
334    codec_types: &mut Vec<CodecType>,
335    codec_info: &mut HashMap<CodecType, CodecInfo>,
336) {
337    #[cfg(feature = "amr-nb")]
338    {
339        codec_types.push(CodecType::AmrNb);
340        codec_info.insert(
341            CodecType::AmrNb,
342            CodecInfo {
343                name: "AMR",
344                sample_rate: 8000,
345                channels: 1,
346                bitrate: CodecType::AmrNb.default_bitrate(),
347                frame_size: 160,
348                payload_type: None,
349            },
350        );
351    }
352
353    #[cfg(feature = "amr-wb")]
354    {
355        codec_types.push(CodecType::AmrWb);
356        codec_info.insert(
357            CodecType::AmrWb,
358            CodecInfo {
359                name: "AMR-WB",
360                sample_rate: 16000,
361                channels: 1,
362                bitrate: CodecType::AmrWb.default_bitrate(),
363                frame_size: 320,
364                payload_type: None,
365            },
366        );
367    }
368}
369
370impl CodecCapabilities {
371    /// Get capabilities for all supported codecs
372    #[must_use]
373    pub fn get_all() -> Self {
374        // Both values are populated by feature-gated blocks. In a deliberately
375        // codec-free build they remain empty and therefore need no mutation.
376        #[allow(unused_mut)]
377        let mut codec_types = Vec::new();
378        #[allow(unused_mut)]
379        let mut codec_info = HashMap::new();
380
381        #[cfg(feature = "g711")]
382        {
383            codec_types.push(CodecType::G711Pcmu);
384            codec_types.push(CodecType::G711Pcma);
385
386            codec_info.insert(
387                CodecType::G711Pcmu,
388                CodecInfo {
389                    name: "PCMU",
390                    sample_rate: 8000,
391                    channels: 1,
392                    bitrate: 64000,
393                    frame_size: 160,
394                    payload_type: Some(0),
395                },
396            );
397
398            codec_info.insert(
399                CodecType::G711Pcma,
400                CodecInfo {
401                    name: "PCMA",
402                    sample_rate: 8000,
403                    channels: 1,
404                    bitrate: 64000,
405                    frame_size: 160,
406                    payload_type: Some(8),
407                },
408            );
409        }
410
411        #[cfg(feature = "opus")]
412        {
413            codec_types.push(CodecType::Opus);
414            codec_info.insert(
415                CodecType::Opus,
416                CodecInfo {
417                    name: "opus",
418                    sample_rate: 48000,
419                    channels: 1,
420                    bitrate: 64000,
421                    frame_size: 960,
422                    payload_type: None,
423                },
424            );
425        }
426
427        #[cfg(any(feature = "amr-nb", feature = "amr-wb"))]
428        add_amr_capabilities(&mut codec_types, &mut codec_info);
429
430        #[cfg(feature = "g729")]
431        {
432            codec_types.push(CodecType::G729);
433            codec_types.push(CodecType::G729A);
434            codec_types.push(CodecType::G729BA);
435
436            codec_info.insert(
437                CodecType::G729,
438                CodecInfo {
439                    name: "G729",
440                    sample_rate: 8000,
441                    channels: 1,
442                    bitrate: 8000,
443                    frame_size: 80,
444                    payload_type: Some(18),
445                },
446            );
447            codec_info.insert(
448                CodecType::G729A,
449                CodecInfo {
450                    name: "G729A",
451                    sample_rate: 8000,
452                    channels: 1,
453                    bitrate: 8000,
454                    frame_size: 80,
455                    payload_type: Some(18),
456                },
457            );
458            codec_info.insert(
459                CodecType::G729BA,
460                CodecInfo {
461                    name: "G729BA",
462                    sample_rate: 8000,
463                    channels: 1,
464                    bitrate: 8000,
465                    frame_size: 80,
466                    payload_type: Some(18),
467                },
468            );
469        }
470
471        Self {
472            codec_types,
473            codec_info,
474        }
475    }
476
477    /// Check if a codec type is supported
478    #[must_use]
479    pub fn is_supported(&self, codec_type: CodecType) -> bool {
480        self.codec_types.contains(&codec_type)
481    }
482
483    /// Get information for a specific codec type
484    #[must_use]
485    pub fn get_info(&self, codec_type: CodecType) -> Option<&CodecInfo> {
486        self.codec_info.get(&codec_type)
487    }
488}
489
490#[cfg(test)]
491mod tests {
492    use super::*;
493
494    #[test]
495    fn test_codec_factory_supported_codecs() {
496        let supported = CodecFactory::supported_codecs();
497
498        #[cfg(any(
499            feature = "g711",
500            feature = "g729",
501            feature = "opus",
502            feature = "amr-nb",
503            feature = "amr-wb"
504        ))]
505        assert!(!supported.is_empty());
506
507        #[cfg(not(any(
508            feature = "g711",
509            feature = "g729",
510            feature = "opus",
511            feature = "amr-nb",
512            feature = "amr-wb"
513        )))]
514        assert!(supported.is_empty());
515
516        #[cfg(feature = "g711")]
517        {
518            assert!(supported.contains(&"PCMU"));
519            assert!(supported.contains(&"PCMA"));
520        }
521
522        #[cfg(feature = "amr-nb")]
523        assert!(supported.contains(&"AMR"));
524
525        #[cfg(feature = "amr-wb")]
526        assert!(supported.contains(&"AMR-WB"));
527    }
528
529    #[test]
530    fn test_codec_factory_is_supported() {
531        #[cfg(feature = "g711")]
532        {
533            assert!(CodecFactory::is_supported("PCMU"));
534            assert!(CodecFactory::is_supported("pcmu"));
535            assert!(CodecFactory::is_supported("PCMA"));
536        }
537
538        assert!(!CodecFactory::is_supported("UNSUPPORTED"));
539        assert!(!CodecFactory::is_supported("G722"));
540
541        #[cfg(feature = "opus")]
542        for name in ["opus", "Opus", "OPUS"] {
543            assert!(CodecFactory::is_supported(name));
544        }
545        #[cfg(not(feature = "opus"))]
546        for name in ["opus", "Opus", "OPUS"] {
547            assert!(!CodecFactory::is_supported(name));
548        }
549    }
550
551    #[test]
552    fn test_codec_registry() {
553        let mut registry = CodecRegistry::new();
554        assert!(registry.is_empty());
555        assert_eq!(registry.len(), 0);
556
557        #[cfg(feature = "g711")]
558        {
559            let config = CodecConfig::g711_pcmu();
560            let codec = CodecFactory::create(config).unwrap();
561            registry.register("test_pcmu".to_string(), codec);
562
563            assert_eq!(registry.len(), 1);
564            assert!(!registry.is_empty());
565            assert!(registry.get("test_pcmu").is_some());
566        }
567
568        registry.clear();
569        assert!(registry.is_empty());
570    }
571
572    #[test]
573    fn test_codec_capabilities() {
574        let caps = CodecCapabilities::get_all();
575
576        #[cfg(any(
577            feature = "g711",
578            feature = "g729",
579            feature = "opus",
580            feature = "amr-nb",
581            feature = "amr-wb"
582        ))]
583        {
584            assert!(!caps.codec_types.is_empty());
585            assert!(!caps.codec_info.is_empty());
586        }
587
588        #[cfg(not(any(
589            feature = "g711",
590            feature = "g729",
591            feature = "opus",
592            feature = "amr-nb",
593            feature = "amr-wb"
594        )))]
595        {
596            assert!(caps.codec_types.is_empty());
597            assert!(caps.codec_info.is_empty());
598        }
599
600        #[cfg(feature = "g711")]
601        {
602            assert!(caps.is_supported(CodecType::G711Pcmu));
603            assert!(caps.get_info(CodecType::G711Pcmu).is_some());
604        }
605
606        #[cfg(feature = "amr-nb")]
607        {
608            assert!(caps.is_supported(CodecType::AmrNb));
609            assert_eq!(caps.get_info(CodecType::AmrNb).unwrap().name, "AMR");
610        }
611
612        #[cfg(feature = "amr-wb")]
613        {
614            assert!(caps.is_supported(CodecType::AmrWb));
615            assert_eq!(caps.get_info(CodecType::AmrWb).unwrap().name, "AMR-WB");
616        }
617    }
618
619    #[test]
620    #[cfg(feature = "g711")]
621    fn test_codec_creation() {
622        let config = CodecConfig::g711_pcmu();
623        let codec = CodecFactory::create(config);
624        assert!(codec.is_ok());
625
626        let codec = codec.unwrap();
627        let info = codec.info();
628        assert_eq!(info.name, "PCMU");
629        assert_eq!(info.sample_rate, 8000);
630    }
631
632    #[test]
633    #[cfg(feature = "g711")]
634    fn test_codec_creation_by_name() {
635        let config = CodecConfig::new(CodecType::G711Pcmu);
636        let codec = CodecFactory::create_by_name("PCMU", config.clone());
637        assert!(codec.is_ok());
638
639        let codec = CodecFactory::create_by_name("UNKNOWN", config);
640        assert!(codec.is_err());
641    }
642
643    #[test]
644    #[cfg(feature = "g711")]
645    fn test_codec_creation_by_payload_type() {
646        let config = CodecConfig::new(CodecType::G711Pcmu);
647        let codec = CodecFactory::create_by_payload_type(0, config.clone());
648        assert!(codec.is_ok());
649
650        let codec = CodecFactory::create_by_payload_type(255, config);
651        assert!(codec.is_err());
652    }
653}