| 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 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 239 | pub enum Rotation { |
| 240 | |
| 241 | #[default] |
| 242 | None, |
| 243 | |
| 244 | Cw90, |
| 245 | |
| 246 | Cw180, |
| 247 | |
| 248 | Cw270, |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | Other { |
| 256 | |
| 257 | a: i32, |
| 258 | |
| 259 | b: i32, |
| 260 | |
| 261 | c: i32, |
| 262 | |
| 263 | d: i32, |
| 264 | }, |
| 265 | } |
| 266 | |
| 267 | impl Rotation { |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | pub fn from_matrix(a: i32, b: i32, c: i32, d: i32) -> Rotation { |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | if a == d && c.checked_neg() == Some(b) && (a != 0 || b != 0) { |
| 290 | |
| 291 | return match (a.signum(), b.signum()) { |
| 292 | (1, 0) => Rotation::None, |
| 293 | (-1, 0) => Rotation::Cw180, |
| 294 | (0, 1) => Rotation::Cw90, |
| 295 | (0, -1) => Rotation::Cw270, |
| 296 | _ => Rotation::Other { a, b, c, d }, |
| 297 | }; |
| 298 | } |
| 299 | Rotation::Other { a, b, c, d } |
| 300 | } |
| 301 | |
| 302 | |
| 303 | |
| 304 | pub fn steps(self) -> Option<u8> { |
| 305 | match self { |
| 306 | Rotation::None => Some(0), |
| 307 | Rotation::Cw90 => Some(1), |
| 308 | Rotation::Cw180 => Some(2), |
| 309 | Rotation::Cw270 => Some(3), |
| 310 | Rotation::Other { .. } => None, |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | |
| 315 | |
| 316 | pub fn swaps_axes(self) -> bool { |
| 317 | matches!(self, Rotation::Cw90 | Rotation::Cw270) |
| 318 | } |
| 319 | |
| 320 | |
| 321 | |
| 322 | pub fn is_none(self) -> bool { |
| 323 | matches!(self, Rotation::None) |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | |
| 328 | #[derive(Debug, Clone, PartialEq)] |
| 329 | pub struct StreamInfo { |
| 330 | |
| 331 | pub index: u32, |
| 332 | |
| 333 | pub time_base: TimeBase, |
| 334 | |
| 335 | pub params: CodecParameters, |
| 336 | |
| 337 | pub start_time: Option<i64>, |
| 338 | |
| 339 | pub duration: Option<i64>, |
| 340 | |
| 341 | pub language: Option<String>, |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | pub initial_padding: u32, |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | pub default: bool, |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | pub rotation: Rotation, |
| 369 | } |
| 370 | |
| 371 | impl StreamInfo { |
| 372 | |
| 373 | |
| 374 | pub fn new(index: u32, time_base: TimeBase, params: CodecParameters) -> StreamInfo { |
| 375 | StreamInfo { |
| 376 | index, |
| 377 | time_base, |
| 378 | params, |
| 379 | start_time: None, |
| 380 | duration: None, |
| 381 | language: None, |
| 382 | initial_padding: 0, |
| 383 | default: false, |
| 384 | rotation: Rotation::None, |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | |
| 390 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 391 | pub enum SeekMode { |
| 392 | |
| 393 | |
| 394 | |
| 395 | #[default] |
| 396 | SyncBefore, |
| 397 | |
| 398 | SyncAfter, |
| 399 | |
| 400 | |
| 401 | Exact, |
| 402 | } |
| 403 | |
| 404 | |
| 405 | pub trait Demuxer: Send { |
| 406 | |
| 407 | fn streams(&self) -> &[StreamInfo]; |
| 408 | |
| 409 | |
| 410 | fn next_packet(&mut self) -> Result<Packet>; |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | fn seek(&mut self, stream: u32, to: Timestamp, mode: SeekMode) -> Result<()>; |
| 417 | } |
| 418 | |
| 419 | |
| 420 | pub trait Muxer: Send { |
| 421 | |
| 422 | |
| 423 | |
| 424 | fn add_stream(&mut self, info: StreamInfo) -> Result<u32>; |
| 425 | |
| 426 | |
| 427 | |
| 428 | fn write_packet(&mut self, packet: &Packet) -> Result<()>; |
| 429 | |
| 430 | |
| 431 | |
| 432 | fn finish(&mut self) -> Result<()>; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | pub trait Decoder: Send { |
| 445 | |
| 446 | |
| 447 | fn codec_parameters(&self) -> &CodecParameters; |
| 448 | |
| 449 | |
| 450 | |
| 451 | fn send_packet(&mut self, packet: &Packet) -> Result<()>; |
| 452 | |
| 453 | |
| 454 | |
| 455 | fn receive_frame(&mut self) -> Result<Frame>; |
| 456 | |
| 457 | |
| 458 | |
| 459 | fn flush(&mut self) -> Result<()>; |
| 460 | |
| 461 | |
| 462 | |
| 463 | fn reset(&mut self); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | pub trait Encoder: Send { |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | fn codec_parameters(&self) -> &CodecParameters; |
| 473 | |
| 474 | |
| 475 | fn send_frame(&mut self, frame: &Frame) -> Result<()>; |
| 476 | |
| 477 | |
| 478 | |
| 479 | fn receive_packet(&mut self) -> Result<Packet>; |
| 480 | |
| 481 | |
| 482 | fn flush(&mut self) -> Result<()>; |
| 483 | } |
| 484 | |
| 485 | #[cfg(test)] |
| 486 | mod tests { |
| 487 | use super::*; |
| 488 | |
| 489 | #[test] |
| 490 | fn every_codec_id_has_a_media_type_and_name() { |
| 491 | |
| 492 | |
| 493 | |
| 494 | let all = [ |
| 495 | CodecId::H264, |
| 496 | CodecId::H265, |
| 497 | CodecId::Vp8, |
| 498 | CodecId::Vp9, |
| 499 | CodecId::Av1, |
| 500 | CodecId::Aac, |
| 501 | CodecId::Ac3, |
| 502 | CodecId::EAc3, |
| 503 | CodecId::TrueHd, |
| 504 | CodecId::Dts, |
| 505 | CodecId::Alac, |
| 506 | CodecId::Flac, |
| 507 | CodecId::Mp3, |
| 508 | CodecId::Opus, |
| 509 | CodecId::Vorbis, |
| 510 | CodecId::PcmU8, |
| 511 | CodecId::PcmS16Le, |
| 512 | CodecId::PcmS16Be, |
| 513 | CodecId::PcmS24Le, |
| 514 | CodecId::PcmS32Le, |
| 515 | CodecId::PcmF32Le, |
| 516 | CodecId::Srt, |
| 517 | CodecId::WebVtt, |
| 518 | CodecId::Ass, |
| 519 | CodecId::Pgs, |
| 520 | CodecId::Tx3g, |
| 521 | ]; |
| 522 | assert_eq!(all.len(), 26); |
| 523 | let mut names: Vec<&str> = all.iter().map(|c| c.name()).collect(); |
| 524 | names.sort_unstable(); |
| 525 | names.dedup(); |
| 526 | assert_eq!(names.len(), all.len(), "codec names must be unique"); |
| 527 | assert_eq!(CodecId::Av1.media_type(), MediaType::Video); |
| 528 | assert_eq!(CodecId::Opus.media_type(), MediaType::Audio); |
| 529 | assert_eq!(CodecId::Pgs.media_type(), MediaType::Subtitle); |
| 530 | } |
| 531 | |
| 532 | #[test] |
| 533 | fn parameters_default_per_media_type() { |
| 534 | let v = CodecParameters::new(CodecId::H265); |
| 535 | assert!(v.video().is_some() && v.audio().is_none()); |
| 536 | let a = CodecParameters::new(CodecId::Opus); |
| 537 | assert_eq!(a.audio().unwrap().layout, ChannelLayout::Stereo); |
| 538 | assert!(matches!( |
| 539 | CodecParameters::new(CodecId::Pgs).media, |
| 540 | MediaParameters::Subtitle |
| 541 | )); |
| 542 | |
| 543 | |
| 544 | let mut p = CodecParameters::new(CodecId::H264); |
| 545 | if let MediaParameters::Video(v) = &mut p.media { |
| 546 | v.width = 1920; |
| 547 | v.height = 1080; |
| 548 | v.frame_rate = Some(TimeBase::new(24_000, 1001)); |
| 549 | v.format = Some(PixelFormat::I420); |
| 550 | } |
| 551 | let fr = p.video().unwrap().frame_rate.unwrap(); |
| 552 | assert_eq!(fr.num(), 24_000); |
| 553 | assert_eq!(fr.inverse(), TimeBase::NTSC_FILM); |
| 554 | } |
| 555 | |
| 556 | #[test] |
| 557 | fn stream_info_carries_language_and_timing() { |
| 558 | let mut s = StreamInfo::new(2, TimeBase::MILLIS, CodecParameters::new(CodecId::Srt)); |
| 559 | s.language = Some("tur".into()); |
| 560 | s.duration = Some(3_600_000); |
| 561 | assert_eq!(s.params.codec.media_type(), MediaType::Subtitle); |
| 562 | assert_eq!(s.index, 2); |
| 563 | assert_eq!(s.language.as_deref(), Some("tur")); |
| 564 | assert_eq!(s.rotation, Rotation::None); |
| 565 | } |
| 566 | |
| 567 | #[test] |
| 568 | fn quarter_turn_matrices_read_as_typed_rotations() { |
| 569 | const ONE: i32 = 65_536; |
| 570 | let cases = [ |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | ((ONE, 0, 0, ONE), Rotation::None), |
| 576 | ((0, -ONE, ONE, 0), Rotation::Cw270), |
| 577 | ((-ONE, 0, 0, -ONE), Rotation::Cw180), |
| 578 | ((0, ONE, -ONE, 0), Rotation::Cw90), |
| 579 | |
| 580 | |
| 581 | ((2 * ONE, 0, 0, 2 * ONE), Rotation::None), |
| 582 | ]; |
| 583 | for ((a, b, c, d), want) in cases { |
| 584 | assert_eq!(Rotation::from_matrix(a, b, c, d), want, "{a} {b} {c} {d}"); |
| 585 | } |
| 586 | assert_eq!(Rotation::None.steps(), Some(0)); |
| 587 | assert_eq!(Rotation::Cw90.steps(), Some(1)); |
| 588 | assert_eq!(Rotation::Cw180.steps(), Some(2)); |
| 589 | assert_eq!(Rotation::Cw270.steps(), Some(3)); |
| 590 | assert!(Rotation::Cw90.swaps_axes() && Rotation::Cw270.swaps_axes()); |
| 591 | assert!(!Rotation::None.swaps_axes() && !Rotation::Cw180.swaps_axes()); |
| 592 | assert!(Rotation::None.is_none()); |
| 593 | } |
| 594 | |
| 595 | #[test] |
| 596 | fn a_matrix_that_is_not_a_quarter_turn_is_kept_not_rounded() { |
| 597 | const ONE: i32 = 65_536; |
| 598 | |
| 599 | assert_eq!( |
| 600 | Rotation::from_matrix(46_340, -46_340, 46_340, 46_340), |
| 601 | Rotation::Other { |
| 602 | a: 46_340, |
| 603 | b: -46_340, |
| 604 | c: 46_340, |
| 605 | d: 46_340, |
| 606 | } |
| 607 | ); |
| 608 | |
| 609 | |
| 610 | for (a, b, c, d) in [ |
| 611 | (ONE, 0, 0, -ONE), |
| 612 | (0, ONE, ONE, 0), |
| 613 | (ONE, 0, ONE, ONE), |
| 614 | (0, 0, 0, 0), |
| 615 | ] { |
| 616 | let got = Rotation::from_matrix(a, b, c, d); |
| 617 | assert!( |
| 618 | matches!(got, Rotation::Other { .. }), |
| 619 | "{a} {b} {c} {d} read as {got:?}" |
| 620 | ); |
| 621 | assert_eq!(got.steps(), None, "Other is not a quarter count"); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | #[test] |
| 626 | fn a_matrix_term_at_i32_min_is_not_a_quarter_turn() { |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | for (a, b, c, d) in [ |
| 633 | (0, i32::MIN, i32::MIN, 0), |
| 634 | (65_536, i32::MIN, i32::MIN, 65_536), |
| 635 | (i32::MIN, 0, i32::MIN, i32::MIN), |
| 636 | ] { |
| 637 | let got = Rotation::from_matrix(a, b, c, d); |
| 638 | assert!( |
| 639 | matches!(got, Rotation::Other { .. }), |
| 640 | "{a} {b} {c} {d} read as {got:?}" |
| 641 | ); |
| 642 | assert_eq!(got.steps(), None, "Other is not a quarter count"); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | #[test] |
| 647 | fn trait_objects_are_send() { |
| 648 | fn assert_send<T: Send + ?Sized>() {} |
| 649 | assert_send::<dyn Demuxer>(); |
| 650 | assert_send::<dyn Muxer>(); |
| 651 | assert_send::<dyn Decoder>(); |
| 652 | assert_send::<dyn Encoder>(); |
| 653 | assert_send::<Packet>(); |
| 654 | assert_send::<Frame>(); |
| 655 | } |
| 656 | } |