symphonia_core/codecs/mod.rs
1// Symphonia
2// Copyright (c) 2019-2026 The Project Symphonia Developers.
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at https://mozilla.org/MPL/2.0/.
7
8//! The `codec` module and its sub-modules provides traits and supporting infrastructure to
9//! implement audio, video, and subtitle decoders.
10//!
11//! # Nomenclature
12//!
13//! * A codec ID refers to a unique identifier for a specific codec.
14//! * A codec type refers to the type of media the codec is encoding: audio, video, subtitles,
15//! etc.
16//! * Codec parameters refers to a set of parameters common to a particular codec type (e.g.,
17//! audio sample rate).
18
19use std::hash::Hash;
20
21pub mod audio;
22pub mod registry;
23pub mod subtitle;
24pub mod video;
25
26use crate::codecs::audio::{AudioCodecId, AudioCodecParameters};
27use crate::codecs::subtitle::{SubtitleCodecId, SubtitleCodecParameters};
28use crate::codecs::video::{VideoCodecId, VideoCodecParameters};
29
30/// A codec-specific identification code for a profile.
31///
32/// In general, codec profiles are designed to target specific applications, and define a set of
33/// minimum capabilities a decoder must implement to successfully decode a bitstream. For an
34/// encoder, a profile imposes a set of constraints upon the bitstream it produces.
35#[repr(transparent)]
36#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
37pub struct CodecProfile(u32);
38
39impl CodecProfile {
40 /// Create a new codec profile from a profile code.
41 pub const fn new(code: u32) -> Self {
42 Self(code)
43 }
44
45 /// Get the profile code.
46 pub const fn get(&self) -> u32 {
47 self.0
48 }
49}
50
51impl From<u32> for CodecProfile {
52 fn from(value: u32) -> Self {
53 Self(value)
54 }
55}
56
57/// Basic information about a codec profile.
58#[derive(Copy, Clone, Debug)]
59pub struct CodecProfileInfo {
60 /// The codec profile.
61 pub profile: CodecProfile,
62 /// A short ASCII-only string identifying the codec profile.
63 pub short_name: &'static str,
64 /// A longer, more descriptive, string identifying the codec profile
65 pub long_name: &'static str,
66}
67
68/// Basic information about a codec.
69#[derive(Copy, Clone, Debug)]
70pub struct CodecInfo {
71 /// A short ASCII-only string identifying the codec.
72 pub short_name: &'static str,
73 /// A longer, more descriptive, string identifying the codec.
74 pub long_name: &'static str,
75 /// A list of codec profiles.
76 pub profiles: &'static [CodecProfileInfo],
77}
78
79/// Generic wrapper around type-specific codec parameters.
80#[non_exhaustive]
81#[derive(Clone, Debug)]
82pub enum CodecParameters {
83 /// Codec parameters for an audio codec.
84 Audio(AudioCodecParameters),
85 /// Codec parameters for a video codec.
86 Video(VideoCodecParameters),
87 /// Codec parameters for a subtitle codec.
88 Subtitle(SubtitleCodecParameters),
89}
90
91impl CodecParameters {
92 /// Returns `true` if the codec parameters are for an audio codec.
93 pub fn is_audio(&self) -> bool {
94 matches!(self, CodecParameters::Audio(_))
95 }
96
97 /// If the codec parameters are for an audio codec, returns an immutable reference to the
98 /// contained audio codec parameters. Otherwise, returns `None`.
99 pub fn audio(&self) -> Option<&AudioCodecParameters> {
100 match self {
101 CodecParameters::Audio(params) => Some(params),
102 _ => None,
103 }
104 }
105
106 /// If the codec parameters are for an audio codec, returns a mutable reference to the
107 /// contained audio codec parameters. Otherwise, returns `None`.
108 pub fn audio_mut(&mut self) -> Option<&mut AudioCodecParameters> {
109 match self {
110 CodecParameters::Audio(params) => Some(params),
111 _ => None,
112 }
113 }
114
115 /// Returns `true` if the codec parameters are for a video codec.
116 pub fn is_video(&self) -> bool {
117 matches!(self, CodecParameters::Video(_))
118 }
119
120 /// If the codec parameters are for an video codec, returns an immutable reference to the
121 /// contained video codec parameters. Otherwise, returns `None`.
122 pub fn video(&self) -> Option<&VideoCodecParameters> {
123 match self {
124 CodecParameters::Video(params) => Some(params),
125 _ => None,
126 }
127 }
128
129 /// If the codec parameters are for an video codec, returns a mutable reference to the
130 /// contained video codec parameters. Otherwise, returns `None`.
131 pub fn video_mut(&mut self) -> Option<&mut VideoCodecParameters> {
132 match self {
133 CodecParameters::Video(params) => Some(params),
134 _ => None,
135 }
136 }
137
138 /// Returns `true` if the codec parameters are for an subtitle codec.
139 pub fn is_subtitle(&self) -> bool {
140 matches!(self, CodecParameters::Subtitle(_))
141 }
142
143 /// If the codec parameters are for an subtitle codec, returns an immutable reference to the
144 /// contained subtitle codec parameters. Otherwise, returns `None`.
145 pub fn subtitle(&self) -> Option<&SubtitleCodecParameters> {
146 match self {
147 CodecParameters::Subtitle(params) => Some(params),
148 _ => None,
149 }
150 }
151
152 /// If the codec parameters are for an subtitle codec, returns a mutable reference to the
153 /// contained subtitle codec parameters. Otherwise, returns `None`.
154 pub fn subtitle_mut(&mut self) -> Option<&mut SubtitleCodecParameters> {
155 match self {
156 CodecParameters::Subtitle(params) => Some(params),
157 _ => None,
158 }
159 }
160}
161
162impl From<AudioCodecParameters> for CodecParameters {
163 fn from(value: AudioCodecParameters) -> Self {
164 CodecParameters::Audio(value)
165 }
166}
167
168impl From<VideoCodecParameters> for CodecParameters {
169 fn from(value: VideoCodecParameters) -> Self {
170 CodecParameters::Video(value)
171 }
172}
173
174impl From<SubtitleCodecParameters> for CodecParameters {
175 fn from(value: SubtitleCodecParameters) -> Self {
176 CodecParameters::Subtitle(value)
177 }
178}
179
180/// Generic wrapper around type-specific codec IDs.
181#[non_exhaustive]
182#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
183pub enum CodecId {
184 /// Codec ID for an audio codec.
185 Audio(AudioCodecId),
186 /// Codec ID for a video codec.
187 Video(VideoCodecId),
188 /// Codec ID for a subtitle codec.
189 Subtitle(SubtitleCodecId),
190}
191
192impl From<AudioCodecId> for CodecId {
193 fn from(value: AudioCodecId) -> Self {
194 CodecId::Audio(value)
195 }
196}
197
198impl From<VideoCodecId> for CodecId {
199 fn from(value: VideoCodecId) -> Self {
200 CodecId::Video(value)
201 }
202}
203
204impl From<SubtitleCodecId> for CodecId {
205 fn from(value: SubtitleCodecId) -> Self {
206 CodecId::Subtitle(value)
207 }
208}