| 1 | |
| 2 | |
| 3 | use crate::error::{Error, Result}; |
| 4 | use crate::packet::Buf; |
| 5 | use crate::timebase::Timestamp; |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 13 | pub enum PixelFormat { |
| 14 | |
| 15 | I420, |
| 16 | |
| 17 | I422, |
| 18 | |
| 19 | I444, |
| 20 | |
| 21 | I010, |
| 22 | |
| 23 | I210, |
| 24 | |
| 25 | I410, |
| 26 | |
| 27 | I412, |
| 28 | |
| 29 | Nv12, |
| 30 | |
| 31 | Nv21, |
| 32 | |
| 33 | P010, |
| 34 | |
| 35 | P016, |
| 36 | |
| 37 | Gray8, |
| 38 | |
| 39 | Gray16, |
| 40 | |
| 41 | Rgb8, |
| 42 | |
| 43 | Bgr8, |
| 44 | |
| 45 | Rgba8, |
| 46 | |
| 47 | Bgra8, |
| 48 | } |
| 49 | |
| 50 | impl PixelFormat { |
| 51 | |
| 52 | pub fn plane_count(&self) -> usize { |
| 53 | use PixelFormat::*; |
| 54 | match self { |
| 55 | I420 | I422 | I444 | I010 | I210 | I410 | I412 => 3, |
| 56 | Nv12 | Nv21 | P010 | P016 => 2, |
| 57 | Gray8 | Gray16 | Rgb8 | Bgr8 | Rgba8 | Bgra8 => 1, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | pub fn is_planar(&self) -> bool { |
| 63 | self.plane_count() > 1 |
| 64 | } |
| 65 | |
| 66 | |
| 67 | pub fn is_yuv(&self) -> bool { |
| 68 | use PixelFormat::*; |
| 69 | matches!( |
| 70 | self, |
| 71 | I420 | I422 | I444 | I010 | I210 | I410 | I412 | Nv12 | Nv21 | P010 | P016 |
| 72 | ) |
| 73 | } |
| 74 | |
| 75 | |
| 76 | pub fn has_alpha(&self) -> bool { |
| 77 | matches!(self, PixelFormat::Rgba8 | PixelFormat::Bgra8) |
| 78 | } |
| 79 | |
| 80 | |
| 81 | pub fn bits_per_component(&self) -> u32 { |
| 82 | use PixelFormat::*; |
| 83 | match self { |
| 84 | I010 | I210 | I410 | P010 => 10, |
| 85 | I412 => 12, |
| 86 | Gray16 | P016 => 16, |
| 87 | _ => 8, |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | pub fn bytes_per_component(&self) -> usize { |
| 93 | if self.bits_per_component() > 8 { 2 } else { 1 } |
| 94 | } |
| 95 | |
| 96 | |
| 97 | |
| 98 | pub fn chroma_shift(&self) -> (u32, u32) { |
| 99 | use PixelFormat::*; |
| 100 | match self { |
| 101 | I420 | I010 | Nv12 | Nv21 | P010 | P016 => (1, 1), |
| 102 | I422 | I210 => (1, 0), |
| 103 | _ => (0, 0), |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |
| 108 | pub fn plane_geometry(&self, width: u32, height: u32) -> Vec<(usize, usize)> { |
| 109 | use PixelFormat::*; |
| 110 | let (w, h) = (width as usize, height as usize); |
| 111 | let bpc = self.bytes_per_component(); |
| 112 | let (sx, sy) = self.chroma_shift(); |
| 113 | let cw = w.div_ceil(1 << sx); |
| 114 | let ch = h.div_ceil(1 << sy); |
| 115 | match self { |
| 116 | I420 | I422 | I444 | I010 | I210 | I410 | I412 => { |
| 117 | vec![(w * bpc, h), (cw * bpc, ch), (cw * bpc, ch)] |
| 118 | } |
| 119 | Nv12 | Nv21 | P010 | P016 => vec![(w * bpc, h), (cw * 2 * bpc, ch)], |
| 120 | Gray8 | Gray16 => vec![(w * bpc, h)], |
| 121 | Rgb8 | Bgr8 => vec![(w * 3, h)], |
| 122 | Rgba8 | Bgra8 => vec![(w * 4, h)], |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | |
| 128 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 129 | pub struct Plane { |
| 130 | |
| 131 | pub data: Buf, |
| 132 | |
| 133 | pub stride: usize, |
| 134 | } |
| 135 | |
| 136 | impl Plane { |
| 137 | |
| 138 | pub fn new(data: impl Into<Buf>, stride: usize) -> Plane { |
| 139 | Plane { |
| 140 | data: data.into(), |
| 141 | stride, |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | |
| 146 | pub fn row(&self, y: usize, len: usize) -> Option<&[u8]> { |
| 147 | let start = y.checked_mul(self.stride)?; |
| 148 | self.data.get(start..start.checked_add(len)?) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 158 | pub struct ColorInfo { |
| 159 | |
| 160 | pub primaries: u8, |
| 161 | |
| 162 | pub transfer: u8, |
| 163 | |
| 164 | pub matrix: u8, |
| 165 | |
| 166 | pub full_range: bool, |
| 167 | } |
| 168 | |
| 169 | impl Default for ColorInfo { |
| 170 | |
| 171 | fn default() -> Self { |
| 172 | ColorInfo { |
| 173 | primaries: 2, |
| 174 | transfer: 2, |
| 175 | matrix: 2, |
| 176 | full_range: false, |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] |
| 187 | pub enum Interlace { |
| 188 | |
| 189 | #[default] |
| 190 | Progressive, |
| 191 | |
| 192 | TopFieldFirst, |
| 193 | |
| 194 | BottomFieldFirst, |
| 195 | } |
| 196 | |
| 197 | impl Interlace { |
| 198 | |
| 199 | pub fn is_interlaced(self) -> bool { |
| 200 | self != Interlace::Progressive |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 206 | pub struct VideoFrame { |
| 207 | |
| 208 | pub format: PixelFormat, |
| 209 | |
| 210 | pub width: u32, |
| 211 | |
| 212 | pub height: u32, |
| 213 | |
| 214 | pub planes: Vec<Plane>, |
| 215 | |
| 216 | pub color: ColorInfo, |
| 217 | |
| 218 | pub pts: Option<Timestamp>, |
| 219 | |
| 220 | pub interlace: Interlace, |
| 221 | } |
| 222 | |
| 223 | impl VideoFrame { |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | pub fn try_new( |
| 229 | format: PixelFormat, |
| 230 | width: u32, |
| 231 | height: u32, |
| 232 | planes: Vec<Plane>, |
| 233 | ) -> Result<VideoFrame> { |
| 234 | let geometry = format.plane_geometry(width, height); |
| 235 | if planes.len() != geometry.len() { |
| 236 | return Err(Error::corrupt(format!( |
| 237 | "{format:?} needs {} planes, got {}", |
| 238 | geometry.len(), |
| 239 | planes.len() |
| 240 | ))); |
| 241 | } |
| 242 | for (i, (plane, (row_bytes, rows))) in planes.iter().zip(geometry).enumerate() { |
| 243 | if plane.stride < row_bytes { |
| 244 | return Err(Error::corrupt(format!( |
| 245 | "plane {i}: stride {} < {row_bytes} bytes per row", |
| 246 | plane.stride |
| 247 | ))); |
| 248 | } |
| 249 | |
| 250 | let needed = rows.saturating_sub(1) * plane.stride + row_bytes; |
| 251 | if plane.data.len() < needed { |
| 252 | return Err(Error::corrupt(format!( |
| 253 | "plane {i}: {} bytes for {rows} rows of stride {}", |
| 254 | plane.data.len(), |
| 255 | plane.stride |
| 256 | ))); |
| 257 | } |
| 258 | } |
| 259 | Ok(VideoFrame { |
| 260 | format, |
| 261 | width, |
| 262 | height, |
| 263 | planes, |
| 264 | color: ColorInfo::default(), |
| 265 | pts: None, |
| 266 | interlace: Interlace::Progressive, |
| 267 | }) |
| 268 | } |
| 269 | |
| 270 | |
| 271 | pub fn alloc(format: PixelFormat, width: u32, height: u32) -> VideoFrame { |
| 272 | let planes = format |
| 273 | .plane_geometry(width, height) |
| 274 | .into_iter() |
| 275 | .map(|(row_bytes, rows)| Plane::new(vec![0u8; row_bytes * rows], row_bytes)) |
| 276 | .collect(); |
| 277 | VideoFrame { |
| 278 | format, |
| 279 | width, |
| 280 | height, |
| 281 | planes, |
| 282 | color: ColorInfo::default(), |
| 283 | pts: None, |
| 284 | interlace: Interlace::Progressive, |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | |
| 290 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 291 | pub enum SampleFormat { |
| 292 | |
| 293 | U8, |
| 294 | |
| 295 | S16, |
| 296 | |
| 297 | S32, |
| 298 | |
| 299 | F32, |
| 300 | |
| 301 | F64, |
| 302 | } |
| 303 | |
| 304 | impl SampleFormat { |
| 305 | |
| 306 | pub fn bytes_per_sample(&self) -> usize { |
| 307 | match self { |
| 308 | SampleFormat::U8 => 1, |
| 309 | SampleFormat::S16 => 2, |
| 310 | SampleFormat::S32 | SampleFormat::F32 => 4, |
| 311 | SampleFormat::F64 => 8, |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | |
| 316 | pub fn is_float(&self) -> bool { |
| 317 | matches!(self, SampleFormat::F32 | SampleFormat::F64) |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | |
| 322 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 323 | pub enum ChannelPosition { |
| 324 | |
| 325 | FrontLeft, |
| 326 | |
| 327 | FrontRight, |
| 328 | |
| 329 | FrontCenter, |
| 330 | |
| 331 | Lfe, |
| 332 | |
| 333 | BackLeft, |
| 334 | |
| 335 | BackRight, |
| 336 | |
| 337 | SideLeft, |
| 338 | |
| 339 | SideRight, |
| 340 | |
| 341 | BackCenter, |
| 342 | |
| 343 | FrontLeftOfCenter, |
| 344 | |
| 345 | FrontRightOfCenter, |
| 346 | } |
| 347 | |
| 348 | use ChannelPosition::*; |
| 349 | |
| 350 | const MONO: &[ChannelPosition] = &[FrontCenter]; |
| 351 | const STEREO: &[ChannelPosition] = &[FrontLeft, FrontRight]; |
| 352 | const SURROUND_5_1: &[ChannelPosition] = |
| 353 | &[FrontLeft, FrontRight, FrontCenter, Lfe, BackLeft, BackRight]; |
| 354 | const SURROUND_7_1: &[ChannelPosition] = &[ |
| 355 | FrontLeft, |
| 356 | FrontRight, |
| 357 | FrontCenter, |
| 358 | Lfe, |
| 359 | BackLeft, |
| 360 | BackRight, |
| 361 | SideLeft, |
| 362 | SideRight, |
| 363 | ]; |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 370 | pub enum ChannelLayout { |
| 371 | |
| 372 | Mono, |
| 373 | |
| 374 | Stereo, |
| 375 | |
| 376 | Surround5_1, |
| 377 | |
| 378 | Surround7_1, |
| 379 | |
| 380 | Custom(Vec<ChannelPosition>), |
| 381 | } |
| 382 | |
| 383 | impl ChannelLayout { |
| 384 | |
| 385 | pub fn positions(&self) -> &[ChannelPosition] { |
| 386 | match self { |
| 387 | ChannelLayout::Mono => MONO, |
| 388 | ChannelLayout::Stereo => STEREO, |
| 389 | ChannelLayout::Surround5_1 => SURROUND_5_1, |
| 390 | ChannelLayout::Surround7_1 => SURROUND_7_1, |
| 391 | ChannelLayout::Custom(v) => v, |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | |
| 396 | pub fn channel_count(&self) -> usize { |
| 397 | self.positions().len() |
| 398 | } |
| 399 | |
| 400 | |
| 401 | |
| 402 | pub fn from_count(n: usize) -> ChannelLayout { |
| 403 | match n { |
| 404 | 1 => ChannelLayout::Mono, |
| 405 | 2 => ChannelLayout::Stereo, |
| 406 | 6 => ChannelLayout::Surround5_1, |
| 407 | 8 => ChannelLayout::Surround7_1, |
| 408 | _ => ChannelLayout::Custom(vec![FrontCenter; n]), |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | |
| 414 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 415 | pub struct AudioFrame { |
| 416 | |
| 417 | pub format: SampleFormat, |
| 418 | |
| 419 | pub planar: bool, |
| 420 | |
| 421 | pub layout: ChannelLayout, |
| 422 | |
| 423 | pub rate: u32, |
| 424 | |
| 425 | pub samples: usize, |
| 426 | |
| 427 | pub data: Vec<Buf>, |
| 428 | |
| 429 | pub pts: Option<Timestamp>, |
| 430 | } |
| 431 | |
| 432 | impl AudioFrame { |
| 433 | |
| 434 | pub fn try_new( |
| 435 | format: SampleFormat, |
| 436 | planar: bool, |
| 437 | layout: ChannelLayout, |
| 438 | rate: u32, |
| 439 | samples: usize, |
| 440 | data: Vec<Buf>, |
| 441 | ) -> Result<AudioFrame> { |
| 442 | let channels = layout.channel_count(); |
| 443 | let wanted_planes = if planar { channels } else { 1 }; |
| 444 | if data.len() != wanted_planes { |
| 445 | return Err(Error::corrupt(format!( |
| 446 | "{layout:?} {} needs {wanted_planes} planes, got {}", |
| 447 | if planar { "planar" } else { "interleaved" }, |
| 448 | data.len() |
| 449 | ))); |
| 450 | } |
| 451 | let per_plane = if planar { 1 } else { channels }; |
| 452 | let needed = samples * per_plane * format.bytes_per_sample(); |
| 453 | for (i, plane) in data.iter().enumerate() { |
| 454 | if plane.len() < needed { |
| 455 | return Err(Error::corrupt(format!( |
| 456 | "audio plane {i}: {} bytes, {needed} needed for {samples} samples", |
| 457 | plane.len() |
| 458 | ))); |
| 459 | } |
| 460 | } |
| 461 | Ok(AudioFrame { |
| 462 | format, |
| 463 | planar, |
| 464 | layout, |
| 465 | rate, |
| 466 | samples, |
| 467 | data, |
| 468 | pts: None, |
| 469 | }) |
| 470 | } |
| 471 | |
| 472 | |
| 473 | pub fn channels(&self) -> usize { |
| 474 | self.layout.channel_count() |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 485 | pub enum Frame { |
| 486 | |
| 487 | Video(VideoFrame), |
| 488 | |
| 489 | Audio(AudioFrame), |
| 490 | } |
| 491 | |
| 492 | impl Frame { |
| 493 | |
| 494 | pub fn pts(&self) -> Option<Timestamp> { |
| 495 | match self { |
| 496 | Frame::Video(v) => v.pts, |
| 497 | Frame::Audio(a) => a.pts, |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | #[cfg(test)] |
| 503 | mod tests { |
| 504 | use super::*; |
| 505 | use crate::timebase::TimeBase; |
| 506 | |
| 507 | #[test] |
| 508 | fn plane_geometry_matches_formats() { |
| 509 | |
| 510 | assert_eq!( |
| 511 | PixelFormat::I420.plane_geometry(1921, 1081), |
| 512 | vec![(1921, 1081), (961, 541), (961, 541)] |
| 513 | ); |
| 514 | assert_eq!( |
| 515 | PixelFormat::Nv12.plane_geometry(1920, 1080), |
| 516 | vec![(1920, 1080), (1920, 540)] |
| 517 | ); |
| 518 | |
| 519 | assert_eq!( |
| 520 | PixelFormat::P010.plane_geometry(1920, 1080), |
| 521 | vec![(3840, 1080), (3840, 540)] |
| 522 | ); |
| 523 | assert_eq!(PixelFormat::Bgra8.plane_geometry(64, 4), vec![(256, 4)]); |
| 524 | assert_eq!(PixelFormat::I444.plane_geometry(16, 16).len(), 3); |
| 525 | assert_eq!(PixelFormat::P010.bits_per_component(), 10); |
| 526 | assert_eq!(PixelFormat::P010.bytes_per_component(), 2); |
| 527 | assert!(PixelFormat::Bgra8.has_alpha()); |
| 528 | assert!(!PixelFormat::Nv12.has_alpha()); |
| 529 | assert!(PixelFormat::Nv12.is_yuv()); |
| 530 | } |
| 531 | |
| 532 | #[test] |
| 533 | fn video_frame_validates_planes() { |
| 534 | let f = VideoFrame::alloc(PixelFormat::I420, 64, 32); |
| 535 | assert_eq!(f.planes.len(), 3); |
| 536 | assert_eq!(f.planes[0].stride, 64); |
| 537 | assert_eq!(f.planes[1].data.len(), 32 * 16); |
| 538 | assert_eq!(f.planes[0].row(31, 64).unwrap().len(), 64); |
| 539 | assert!(f.planes[0].row(32, 64).is_none()); |
| 540 | |
| 541 | |
| 542 | let padded = VideoFrame::try_new( |
| 543 | PixelFormat::Gray8, |
| 544 | 4, |
| 545 | 2, |
| 546 | vec![Plane::new(vec![0u8; 8 + 4], 8)], |
| 547 | ); |
| 548 | assert!(padded.is_ok()); |
| 549 | assert!( |
| 550 | VideoFrame::try_new(PixelFormat::Gray8, 4, 2, vec![Plane::new(vec![0u8; 8], 2)]) |
| 551 | .is_err() |
| 552 | ); |
| 553 | assert!( |
| 554 | VideoFrame::try_new(PixelFormat::I420, 4, 2, vec![Plane::new(vec![0u8; 8], 4)]) |
| 555 | .is_err() |
| 556 | ); |
| 557 | } |
| 558 | |
| 559 | #[test] |
| 560 | fn channel_layouts_are_named_and_ordered() { |
| 561 | assert_eq!( |
| 562 | ChannelLayout::Surround5_1.positions(), |
| 563 | &[ |
| 564 | ChannelPosition::FrontLeft, |
| 565 | ChannelPosition::FrontRight, |
| 566 | ChannelPosition::FrontCenter, |
| 567 | ChannelPosition::Lfe, |
| 568 | ChannelPosition::BackLeft, |
| 569 | ChannelPosition::BackRight |
| 570 | ] |
| 571 | ); |
| 572 | assert_eq!(ChannelLayout::Surround7_1.channel_count(), 8); |
| 573 | assert_eq!(ChannelLayout::from_count(6), ChannelLayout::Surround5_1); |
| 574 | assert_eq!(ChannelLayout::from_count(3).channel_count(), 3); |
| 575 | } |
| 576 | |
| 577 | #[test] |
| 578 | fn audio_frame_validates_planes() { |
| 579 | let interleaved = AudioFrame::try_new( |
| 580 | SampleFormat::F32, |
| 581 | false, |
| 582 | ChannelLayout::Surround5_1, |
| 583 | 48_000, |
| 584 | 1024, |
| 585 | vec![Buf::from_vec(vec![0u8; 1024 * 6 * 4])], |
| 586 | ) |
| 587 | .unwrap(); |
| 588 | assert_eq!(interleaved.channels(), 6); |
| 589 | |
| 590 | let planar = AudioFrame::try_new( |
| 591 | SampleFormat::S16, |
| 592 | true, |
| 593 | ChannelLayout::Stereo, |
| 594 | 44_100, |
| 595 | 512, |
| 596 | vec![Buf::from_vec(vec![0u8; 1024]); 2], |
| 597 | ) |
| 598 | .unwrap(); |
| 599 | assert_eq!(planar.data.len(), 2); |
| 600 | |
| 601 | |
| 602 | assert!( |
| 603 | AudioFrame::try_new( |
| 604 | SampleFormat::F32, |
| 605 | true, |
| 606 | ChannelLayout::Stereo, |
| 607 | 48_000, |
| 608 | 64, |
| 609 | vec![Buf::from_vec(vec![0u8; 256])] |
| 610 | ) |
| 611 | .is_err() |
| 612 | ); |
| 613 | assert!( |
| 614 | AudioFrame::try_new( |
| 615 | SampleFormat::F32, |
| 616 | false, |
| 617 | ChannelLayout::Stereo, |
| 618 | 48_000, |
| 619 | 64, |
| 620 | vec![Buf::from_vec(vec![0u8; 4])] |
| 621 | ) |
| 622 | .is_err() |
| 623 | ); |
| 624 | } |
| 625 | |
| 626 | #[test] |
| 627 | fn frame_pts_passthrough() { |
| 628 | let mut v = VideoFrame::alloc(PixelFormat::Nv12, 16, 16); |
| 629 | v.pts = Some(Timestamp::new(1001, TimeBase::new(1, 24_000))); |
| 630 | let f = Frame::Video(v); |
| 631 | assert_eq!(f.pts().unwrap().ticks, 1001); |
| 632 | } |
| 633 | } |