| 1 | |
| 2 | |
| 3 | |
| 4 | use crate::color::ContentLight; |
| 5 | use crate::error::Result; |
| 6 | use crate::frame::{ChannelLayout, ColorInfo, Frame, PixelFormat, SampleFormat}; |
| 7 | use crate::packet::{Buf, Packet}; |
| 8 | use crate::timebase::{TimeBase, Timestamp}; |
| 9 | |
| 10 | |
| 11 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 12 | pub enum MediaType { |
| 13 | |
| 14 | Video, |
| 15 | |
| 16 | Audio, |
| 17 | |
| 18 | Subtitle, |
| 19 | } |
| 20 | |
| 21 | |
| 22 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 23 | #[non_exhaustive] |
| 24 | pub enum CodecId { |
| 25 | |
| 26 | H264, |
| 27 | |
| 28 | H265, |
| 29 | |
| 30 | Vp8, |
| 31 | |
| 32 | Vp9, |
| 33 | |
| 34 | Av1, |
| 35 | |
| 36 | Aac, |
| 37 | |
| 38 | Ac3, |
| 39 | |
| 40 | EAc3, |
| 41 | |
| 42 | TrueHd, |
| 43 | |
| 44 | Dts, |
| 45 | |
| 46 | Alac, |
| 47 | |
| 48 | Flac, |
| 49 | |
| 50 | Mp3, |
| 51 | |
| 52 | Opus, |
| 53 | |
| 54 | Vorbis, |
| 55 | |
| 56 | PcmU8, |
| 57 | |
| 58 | PcmS16Le, |
| 59 | |
| 60 | PcmS16Be, |
| 61 | |
| 62 | PcmS24Le, |
| 63 | |
| 64 | PcmS32Le, |
| 65 | |
| 66 | PcmF32Le, |
| 67 | |
| 68 | Srt, |
| 69 | |
| 70 | WebVtt, |
| 71 | |
| 72 | Ass, |
| 73 | |
| 74 | Pgs, |
| 75 | |
| 76 | Tx3g, |
| 77 | } |
| 78 | |
| 79 | impl CodecId { |
| 80 | |
| 81 | pub fn media_type(&self) -> MediaType { |
| 82 | use CodecId::*; |
| 83 | match self { |
| 84 | H264 | H265 | Vp8 | Vp9 | Av1 => MediaType::Video, |
| 85 | Aac | Ac3 | EAc3 | TrueHd | Dts | Alac | Flac | Mp3 | Opus | Vorbis | PcmU8 |
| 86 | | PcmS16Le | PcmS16Be | PcmS24Le | PcmS32Le | PcmF32Le => MediaType::Audio, |
| 87 | Srt | WebVtt | Ass | Pgs | Tx3g => MediaType::Subtitle, |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | pub fn name(&self) -> &'static str { |
| 93 | use CodecId::*; |
| 94 | match self { |
| 95 | H264 => "h264", |
| 96 | H265 => "h265", |
| 97 | Vp8 => "vp8", |
| 98 | Vp9 => "vp9", |
| 99 | Av1 => "av1", |
| 100 | Aac => "aac", |
| 101 | Ac3 => "ac3", |
| 102 | EAc3 => "eac3", |
| 103 | TrueHd => "truehd", |
| 104 | Dts => "dts", |
| 105 | Alac => "alac", |
| 106 | Flac => "flac", |
| 107 | Mp3 => "mp3", |
| 108 | Opus => "opus", |
| 109 | Vorbis => "vorbis", |
| 110 | PcmU8 => "pcm_u8", |
| 111 | PcmS16Le => "pcm_s16le", |
| 112 | PcmS16Be => "pcm_s16be", |
| 113 | PcmS24Le => "pcm_s24le", |
| 114 | PcmS32Le => "pcm_s32le", |
| 115 | PcmF32Le => "pcm_f32le", |
| 116 | Srt => "srt", |
| 117 | WebVtt => "webvtt", |
| 118 | Ass => "ass", |
| 119 | Pgs => "pgs", |
| 120 | Tx3g => "tx3g", |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | #[derive(Debug, Clone, PartialEq, Default)] |
| 127 | pub struct VideoParameters { |
| 128 | |
| 129 | pub width: u32, |
| 130 | |
| 131 | pub height: u32, |
| 132 | |
| 133 | pub format: Option<PixelFormat>, |
| 134 | |
| 135 | |
| 136 | pub frame_rate: Option<TimeBase>, |
| 137 | |
| 138 | pub sample_aspect_ratio: Option<TimeBase>, |
| 139 | |
| 140 | pub color: ColorInfo, |
| 141 | |
| 142 | |
| 143 | |
| 144 | pub light: ContentLight, |
| 145 | } |
| 146 | |
| 147 | |
| 148 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 149 | pub struct AudioParameters { |
| 150 | |
| 151 | pub sample_rate: u32, |
| 152 | |
| 153 | pub layout: ChannelLayout, |
| 154 | |
| 155 | pub format: Option<SampleFormat>, |
| 156 | |
| 157 | pub bits_per_sample: Option<u32>, |
| 158 | } |
| 159 | |
| 160 | impl Default for AudioParameters { |
| 161 | fn default() -> Self { |
| 162 | AudioParameters { |
| 163 | sample_rate: 0, |
| 164 | layout: ChannelLayout::Stereo, |
| 165 | format: None, |
| 166 | bits_per_sample: None, |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | |
| 172 | #[derive(Debug, Clone, PartialEq)] |
| 173 | pub enum MediaParameters { |
| 174 | |
| 175 | Video(VideoParameters), |
| 176 | |
| 177 | Audio(AudioParameters), |
| 178 | |
| 179 | |
| 180 | Subtitle, |
| 181 | } |
| 182 | |
| 183 | |
| 184 | #[derive(Debug, Clone, PartialEq)] |
| 185 | pub struct CodecParameters { |
| 186 | |
| 187 | pub codec: CodecId, |
| 188 | |
| 189 | |
| 190 | pub extradata: Option<Buf>, |
| 191 | |
| 192 | pub media: MediaParameters, |
| 193 | } |
| 194 | |
| 195 | impl CodecParameters { |
| 196 | |
| 197 | pub fn new(codec: CodecId) -> CodecParameters { |
| 198 | let media = match codec.media_type() { |
| 199 | MediaType::Video => MediaParameters::Video(VideoParameters::default()), |
| 200 | MediaType::Audio => MediaParameters::Audio(AudioParameters::default()), |
| 201 | MediaType::Subtitle => MediaParameters::Subtitle, |
| 202 | }; |
| 203 | CodecParameters { |
| 204 | codec, |
| 205 | extradata: None, |
| 206 | media, |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | |
| 211 | pub fn video(&self) -> Option<&VideoParameters> { |
| 212 | match &self.media { |
| 213 | MediaParameters::Video(v) => Some(v), |
| 214 | _ => None, |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | |
| 219 | pub fn audio(&self) -> Option<&AudioParameters> { |
| 220 | match &self.media { |
| 221 | MediaParameters::Audio(a) => Some(a), |
| 222 | _ => None, |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | |
| 228 | #[derive(Debug, Clone, PartialEq)] |
| 229 | pub struct StreamInfo { |
| 230 | |
| 231 | pub index: u32, |
| 232 | |
| 233 | pub time_base: TimeBase, |
| 234 | |
| 235 | pub params: CodecParameters, |
| 236 | |
| 237 | pub start_time: Option<i64>, |
| 238 | |
| 239 | pub duration: Option<i64>, |
| 240 | |
| 241 | pub language: Option<String>, |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | pub initial_padding: u32, |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | pub default: bool, |
| 261 | } |
| 262 | |
| 263 | impl StreamInfo { |
| 264 | |
| 265 | |
| 266 | pub fn new(index: u32, time_base: TimeBase, params: CodecParameters) -> StreamInfo { |
| 267 | StreamInfo { |
| 268 | index, |
| 269 | time_base, |
| 270 | params, |
| 271 | start_time: None, |
| 272 | duration: None, |
| 273 | language: None, |
| 274 | initial_padding: 0, |
| 275 | default: false, |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | |
| 281 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 282 | pub enum SeekMode { |
| 283 | |
| 284 | |
| 285 | |
| 286 | #[default] |
| 287 | SyncBefore, |
| 288 | |
| 289 | SyncAfter, |
| 290 | |
| 291 | |
| 292 | Exact, |
| 293 | } |
| 294 | |
| 295 | |
| 296 | pub trait Demuxer: Send { |
| 297 | |
| 298 | fn streams(&self) -> &[StreamInfo]; |
| 299 | |
| 300 | |
| 301 | fn next_packet(&mut self) -> Result<Packet>; |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | fn seek(&mut self, stream: u32, to: Timestamp, mode: SeekMode) -> Result<()>; |
| 308 | } |
| 309 | |
| 310 | |
| 311 | pub trait Muxer: Send { |
| 312 | |
| 313 | |
| 314 | |
| 315 | fn add_stream(&mut self, info: StreamInfo) -> Result<u32>; |
| 316 | |
| 317 | |
| 318 | |
| 319 | fn write_packet(&mut self, packet: &Packet) -> Result<()>; |
| 320 | |
| 321 | |
| 322 | |
| 323 | fn finish(&mut self) -> Result<()>; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | pub trait Decoder: Send { |
| 336 | |
| 337 | |
| 338 | fn codec_parameters(&self) -> &CodecParameters; |
| 339 | |
| 340 | |
| 341 | |
| 342 | fn send_packet(&mut self, packet: &Packet) -> Result<()>; |
| 343 | |
| 344 | |
| 345 | |
| 346 | fn receive_frame(&mut self) -> Result<Frame>; |
| 347 | |
| 348 | |
| 349 | |
| 350 | fn flush(&mut self) -> Result<()>; |
| 351 | |
| 352 | |
| 353 | |
| 354 | fn reset(&mut self); |
| 355 | } |
| 356 | |
| 357 | |
| 358 | pub trait Encoder: Send { |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | fn codec_parameters(&self) -> &CodecParameters; |
| 364 | |
| 365 | |
| 366 | fn send_frame(&mut self, frame: &Frame) -> Result<()>; |
| 367 | |
| 368 | |
| 369 | |
| 370 | fn receive_packet(&mut self) -> Result<Packet>; |
| 371 | |
| 372 | |
| 373 | fn flush(&mut self) -> Result<()>; |
| 374 | } |
| 375 | |
| 376 | #[cfg(test)] |
| 377 | mod tests { |
| 378 | use super::*; |
| 379 | |
| 380 | #[test] |
| 381 | fn every_codec_id_has_a_media_type_and_name() { |
| 382 | |
| 383 | |
| 384 | |
| 385 | let all = [ |
| 386 | CodecId::H264, |
| 387 | CodecId::H265, |
| 388 | CodecId::Vp8, |
| 389 | CodecId::Vp9, |
| 390 | CodecId::Av1, |
| 391 | CodecId::Aac, |
| 392 | CodecId::Ac3, |
| 393 | CodecId::EAc3, |
| 394 | CodecId::TrueHd, |
| 395 | CodecId::Dts, |
| 396 | CodecId::Alac, |
| 397 | CodecId::Flac, |
| 398 | CodecId::Mp3, |
| 399 | CodecId::Opus, |
| 400 | CodecId::Vorbis, |
| 401 | CodecId::PcmU8, |
| 402 | CodecId::PcmS16Le, |
| 403 | CodecId::PcmS16Be, |
| 404 | CodecId::PcmS24Le, |
| 405 | CodecId::PcmS32Le, |
| 406 | CodecId::PcmF32Le, |
| 407 | CodecId::Srt, |
| 408 | CodecId::WebVtt, |
| 409 | CodecId::Ass, |
| 410 | CodecId::Pgs, |
| 411 | CodecId::Tx3g, |
| 412 | ]; |
| 413 | assert_eq!(all.len(), 26); |
| 414 | let mut names: Vec<&str> = all.iter().map(|c| c.name()).collect(); |
| 415 | names.sort_unstable(); |
| 416 | names.dedup(); |
| 417 | assert_eq!(names.len(), all.len(), "codec names must be unique"); |
| 418 | assert_eq!(CodecId::Av1.media_type(), MediaType::Video); |
| 419 | assert_eq!(CodecId::Opus.media_type(), MediaType::Audio); |
| 420 | assert_eq!(CodecId::Pgs.media_type(), MediaType::Subtitle); |
| 421 | } |
| 422 | |
| 423 | #[test] |
| 424 | fn parameters_default_per_media_type() { |
| 425 | let v = CodecParameters::new(CodecId::H265); |
| 426 | assert!(v.video().is_some() && v.audio().is_none()); |
| 427 | let a = CodecParameters::new(CodecId::Opus); |
| 428 | assert_eq!(a.audio().unwrap().layout, ChannelLayout::Stereo); |
| 429 | assert!(matches!( |
| 430 | CodecParameters::new(CodecId::Pgs).media, |
| 431 | MediaParameters::Subtitle |
| 432 | )); |
| 433 | |
| 434 | |
| 435 | let mut p = CodecParameters::new(CodecId::H264); |
| 436 | if let MediaParameters::Video(v) = &mut p.media { |
| 437 | v.width = 1920; |
| 438 | v.height = 1080; |
| 439 | v.frame_rate = Some(TimeBase::new(24_000, 1001)); |
| 440 | v.format = Some(PixelFormat::I420); |
| 441 | } |
| 442 | let fr = p.video().unwrap().frame_rate.unwrap(); |
| 443 | assert_eq!(fr.num(), 24_000); |
| 444 | assert_eq!(fr.inverse(), TimeBase::NTSC_FILM); |
| 445 | } |
| 446 | |
| 447 | #[test] |
| 448 | fn stream_info_carries_language_and_timing() { |
| 449 | let mut s = StreamInfo::new(2, TimeBase::MILLIS, CodecParameters::new(CodecId::Srt)); |
| 450 | s.language = Some("tur".into()); |
| 451 | s.duration = Some(3_600_000); |
| 452 | assert_eq!(s.params.codec.media_type(), MediaType::Subtitle); |
| 453 | assert_eq!(s.index, 2); |
| 454 | assert_eq!(s.language.as_deref(), Some("tur")); |
| 455 | } |
| 456 | |
| 457 | #[test] |
| 458 | fn trait_objects_are_send() { |
| 459 | fn assert_send<T: Send + ?Sized>() {} |
| 460 | assert_send::<dyn Demuxer>(); |
| 461 | assert_send::<dyn Muxer>(); |
| 462 | assert_send::<dyn Decoder>(); |
| 463 | assert_send::<dyn Encoder>(); |
| 464 | assert_send::<Packet>(); |
| 465 | assert_send::<Frame>(); |
| 466 | } |
| 467 | } |