| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | use std::sync::mpsc::SyncSender; |
| 28 | |
| 29 | use ec_h265::decode::DecodedPicture; |
| 30 | use ec_h265::{Decoder, Frame as Decoded}; |
| 31 | use ec_h265_syntax::ConformanceWindow; |
| 32 | |
| 33 | use super::{Abort, DecodeFailure, Frame, I420, LATE_RUN, Render, Wide, skip_for_speed}; |
| 34 | use crate::demux::Demuxer; |
| 35 | use crate::project::Speed; |
| 36 | |
| 37 | |
| 38 | |
| 39 | pub(crate) struct Stream { |
| 40 | decoder: Decoder, |
| 41 | |
| 42 | |
| 43 | index: i64, |
| 44 | |
| 45 | |
| 46 | landed: bool, |
| 47 | |
| 48 | flushed: bool, |
| 49 | |
| 50 | wide: [Vec<u16>; 3], |
| 51 | } |
| 52 | |
| 53 | impl Stream { |
| 54 | |
| 55 | pub(crate) fn open(demuxer: &mut Demuxer, start_frame: u32) -> Self { |
| 56 | Self { |
| 57 | decoder: Decoder::new(), |
| 58 | index: demuxer.seek_to_sync_at_or_before(start_frame), |
| 59 | landed: false, |
| 60 | flushed: false, |
| 61 | wide: Default::default(), |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | pub(crate) fn next( |
| 69 | &mut self, |
| 70 | demuxer: &mut Demuxer, |
| 71 | out: &mut I420, |
| 72 | ) -> Result<Option<i64>, String> { |
| 73 | self.next_with(demuxer, |decoded, due, wide| fill(decoded, due, wide, out)) |
| 74 | } |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | pub(crate) fn next_wide( |
| 81 | &mut self, |
| 82 | demuxer: &mut Demuxer, |
| 83 | out: &mut Wide, |
| 84 | ) -> Result<Option<i64>, String> { |
| 85 | self.next_with(demuxer, |decoded, due, _| fill_wide(decoded, due, out)) |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | fn next_with( |
| 91 | &mut self, |
| 92 | demuxer: &mut Demuxer, |
| 93 | mut sink: impl FnMut(&Decoded, i64, &mut [Vec<u16>; 3]) -> Result<(), String>, |
| 94 | ) -> Result<Option<i64>, String> { |
| 95 | loop { |
| 96 | if let Some(decoded) = self.decoder.next_frame() { |
| 97 | if !self.landed { |
| 98 | if decoded.au_index != 0 { |
| 99 | continue; |
| 100 | } |
| 101 | self.landed = true; |
| 102 | } |
| 103 | if !carried(&decoded.picture) { |
| 104 | return Err(format!( |
| 105 | "software HEVC frame {} is {}/{}-bit luma/chroma, and only 8, 10 and 12 bits are carried", |
| 106 | self.index, decoded.picture.bit_depth, decoded.picture.bit_depth_c, |
| 107 | )); |
| 108 | } |
| 109 | let due = self.index; |
| 110 | self.index += 1; |
| 111 | sink(&decoded, due, &mut self.wide)?; |
| 112 | return Ok(Some(due)); |
| 113 | } |
| 114 | if self.flushed { |
| 115 | return Ok(None); |
| 116 | } |
| 117 | match demuxer.next_access_unit() { |
| 118 | Ok(Some(au)) => { |
| 119 | if let Err(e) = self.decoder.decode_annex_b(&au) { |
| 120 | return Err(format!( |
| 121 | "software HEVC decode failed at frame {}: {e}", |
| 122 | self.index |
| 123 | )); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | |
| 128 | Ok(None) => { |
| 129 | self.flushed = true; |
| 130 | if let Err(e) = self.decoder.flush() { |
| 131 | return Err(format!("software HEVC decode failed at end of stream: {e}")); |
| 132 | } |
| 133 | } |
| 134 | Err(e) => return Err(format!("demux error: {e}")), |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | #[allow(clippy::too_many_arguments)] |
| 150 | pub(super) fn run_hevc( |
| 151 | demuxer: &mut Demuxer, |
| 152 | tx: &SyncSender<Frame>, |
| 153 | start_frame: u32, |
| 154 | end_frame: u32, |
| 155 | render: &mut Render, |
| 156 | abort: &Abort, |
| 157 | speed: Speed, |
| 158 | failure: &DecodeFailure, |
| 159 | ) { |
| 160 | let mut stream = Stream::open(demuxer, start_frame); |
| 161 | let mut out = I420::default(); |
| 162 | let mut skipped = 0; |
| 163 | while !abort.hit() { |
| 164 | let due = match stream.next(demuxer, &mut out) { |
| 165 | Ok(Some(due)) => due, |
| 166 | Ok(None) => break, |
| 167 | Err(e) => { |
| 168 | failure.set(e); |
| 169 | break; |
| 170 | } |
| 171 | }; |
| 172 | if due < i64::from(start_frame) { |
| 173 | continue; |
| 174 | } |
| 175 | let due = due as u32; |
| 176 | |
| 177 | |
| 178 | |
| 179 | let sent = if abort.late(due) { |
| 180 | if skipped < LATE_RUN { |
| 181 | skipped += 1; |
| 182 | false |
| 183 | } else { |
| 184 | skipped = 0; |
| 185 | send(&out, due, render, tx) |
| 186 | } |
| 187 | } else if skip_for_speed(speed, start_frame, due, end_frame) { |
| 188 | |
| 189 | false |
| 190 | } else { |
| 191 | skipped = 0; |
| 192 | send(&out, due, render, tx) |
| 193 | }; |
| 194 | |
| 195 | if sent || i64::from(due) + 1 >= i64::from(end_frame) { |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | |
| 202 | |
| 203 | fn send(out: &I420, due: u32, render: &mut Render, tx: &SyncSender<Frame>) -> bool { |
| 204 | let frame = render.frame(due, &out.y, &out.u, &out.v, out.width, out.height); |
| 205 | tx.send(frame).is_err() |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | fn carried(picture: &DecodedPicture) -> bool { |
| 213 | picture.chroma_format_idc <= 3 |
| 214 | && matches!(picture.bit_depth, 8 | 10 | 12) |
| 215 | && matches!(chroma_depth(picture), 8 | 10 | 12) |
| 216 | } |
| 217 | |
| 218 | |
| 219 | |
| 220 | fn chroma_depth(picture: &DecodedPicture) -> u32 { |
| 221 | if picture.chroma_format_idc == 0 { |
| 222 | picture.bit_depth |
| 223 | } else { |
| 224 | picture.bit_depth_c |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | fn crop( |
| 236 | picture: &DecodedPicture, |
| 237 | window: &ConformanceWindow, |
| 238 | y: &mut Vec<u16>, |
| 239 | u: &mut Vec<u16>, |
| 240 | v: &mut Vec<u16>, |
| 241 | ) -> Option<(u32, u32)> { |
| 242 | let (left, right) = (window.left as usize, window.right as usize); |
| 243 | let (top, bottom) = (window.top as usize, window.bottom as usize); |
| 244 | let (width, height) = (picture.width, picture.height); |
| 245 | let idc = picture.chroma_format_idc; |
| 246 | let sub_w = if matches!(idc, 1 | 2) { 2 } else { 1 }; |
| 247 | let sub_h = if idc == 1 { 2 } else { 1 }; |
| 248 | let out_w = width.checked_sub(sub_w * (left + right))?; |
| 249 | let out_h = height.checked_sub(sub_h * (top + bottom))?; |
| 250 | if out_w == 0 || out_h == 0 { |
| 251 | return None; |
| 252 | } |
| 253 | region( |
| 254 | &picture.y, |
| 255 | width, |
| 256 | (sub_w * left, sub_h * top), |
| 257 | (out_w, out_h), |
| 258 | y, |
| 259 | )?; |
| 260 | let (ow, oh) = (out_w.div_ceil(2), out_h.div_ceil(2)); |
| 261 | if idc == 0 { |
| 262 | let neutral = 1u16 << (picture.bit_depth - 1); |
| 263 | for plane in [&mut *u, &mut *v] { |
| 264 | plane.clear(); |
| 265 | plane.resize(ow * oh, neutral); |
| 266 | } |
| 267 | return Some((out_w as u32, out_h as u32)); |
| 268 | } |
| 269 | let chroma_stride = width.div_ceil(sub_w); |
| 270 | let (cw, ch) = (out_w.div_ceil(sub_w), out_h.div_ceil(sub_h)); |
| 271 | for (src, dst) in [(&picture.cb, &mut *u), (&picture.cr, &mut *v)] { |
| 272 | region(src, chroma_stride, (left, top), (cw, ch), dst)?; |
| 273 | if (cw, ch) != (ow, oh) { |
| 274 | let narrowed = crate::export::chroma_420(dst, cw, ch, ow, oh); |
| 275 | dst.clear(); |
| 276 | dst.extend(narrowed.into_iter().map(|s| s as u16)); |
| 277 | } |
| 278 | } |
| 279 | Some((out_w as u32, out_h as u32)) |
| 280 | } |
| 281 | |
| 282 | |
| 283 | |
| 284 | fn region( |
| 285 | src: &[u16], |
| 286 | stride: usize, |
| 287 | (x0, y0): (usize, usize), |
| 288 | (w, h): (usize, usize), |
| 289 | dst: &mut Vec<u16>, |
| 290 | ) -> Option<()> { |
| 291 | dst.clear(); |
| 292 | for row in y0..y0.checked_add(h)? { |
| 293 | let start = row.checked_mul(stride)?.checked_add(x0)?; |
| 294 | dst.extend_from_slice(src.get(start..start.checked_add(w)?)?); |
| 295 | } |
| 296 | Some(()) |
| 297 | } |
| 298 | |
| 299 | |
| 300 | fn to_8bit(src: &[u16], shift: u32, dst: &mut Vec<u8>) { |
| 301 | dst.clear(); |
| 302 | dst.extend(src.iter().map(|&sample| (sample >> shift) as u8)); |
| 303 | } |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | fn fill_wide(decoded: &Decoded, due: i64, out: &mut Wide) -> Result<(), String> { |
| 310 | let picture = &decoded.picture; |
| 311 | let Some((w, h)) = crop(picture, &decoded.window, &mut out.y, &mut out.u, &mut out.v) else { |
| 312 | return Err(format!( |
| 313 | "software HEVC frame {due}: its conformance window does not fit the {}x{} picture", |
| 314 | picture.width, picture.height, |
| 315 | )); |
| 316 | }; |
| 317 | |
| 318 | let to_10 = |s: &mut u16, depth: u32| { |
| 319 | *s = if depth <= 10 { *s << (10 - depth) } else { *s >> (depth - 10) }; |
| 320 | }; |
| 321 | for s in &mut out.y { |
| 322 | to_10(s, picture.bit_depth); |
| 323 | } |
| 324 | for s in out.u.iter_mut().chain(out.v.iter_mut()) { |
| 325 | to_10(s, chroma_depth(picture)); |
| 326 | } |
| 327 | out.width = w; |
| 328 | out.height = h; |
| 329 | Ok(()) |
| 330 | } |
| 331 | |
| 332 | fn fill( |
| 333 | decoded: &Decoded, |
| 334 | due: i64, |
| 335 | wide: &mut [Vec<u16>; 3], |
| 336 | out: &mut I420, |
| 337 | ) -> Result<(), String> { |
| 338 | let picture = &decoded.picture; |
| 339 | let [wy, wu, wv] = wide; |
| 340 | let Some((w, h)) = crop(picture, &decoded.window, wy, wu, wv) else { |
| 341 | return Err(format!( |
| 342 | "software HEVC frame {due}: its conformance window does not fit the {}x{} picture", |
| 343 | picture.width, picture.height, |
| 344 | )); |
| 345 | }; |
| 346 | let shift_luma = picture.bit_depth.saturating_sub(8); |
| 347 | let shift_chroma = chroma_depth(picture).saturating_sub(8); |
| 348 | to_8bit(wy, shift_luma, &mut out.y); |
| 349 | to_8bit(wu, shift_chroma, &mut out.u); |
| 350 | to_8bit(wv, shift_chroma, &mut out.v); |
| 351 | out.width = w; |
| 352 | out.height = h; |
| 353 | Ok(()) |
| 354 | } |
| 355 | |
| 356 | #[cfg(test)] |
| 357 | mod tests { |
| 358 | use std::path::PathBuf; |
| 359 | use std::process::Command; |
| 360 | |
| 361 | use super::*; |
| 362 | |
| 363 | fn asset(name: &str) -> PathBuf { |
| 364 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 365 | .join("../../assets") |
| 366 | .join(name) |
| 367 | } |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | #[test] |
| 373 | fn crop_takes_the_conformance_window_out_of_the_coded_picture() { |
| 374 | |
| 375 | let luma: Vec<u16> = (0..8u16) |
| 376 | .flat_map(|y| (0..16u16).map(move |x| x + 16 * y)) |
| 377 | .collect(); |
| 378 | let picture = DecodedPicture { |
| 379 | width: 16, |
| 380 | height: 8, |
| 381 | bit_depth: 8, |
| 382 | bit_depth_c: 8, |
| 383 | chroma_format_idc: 1, |
| 384 | y: luma, |
| 385 | cb: vec![7; 8 * 4], |
| 386 | cr: vec![9; 8 * 4], |
| 387 | }; |
| 388 | |
| 389 | |
| 390 | let window = ConformanceWindow { |
| 391 | left: 1, |
| 392 | right: 1, |
| 393 | top: 0, |
| 394 | bottom: 1, |
| 395 | }; |
| 396 | let (mut y, mut u, mut v) = (Vec::new(), Vec::new(), Vec::new()); |
| 397 | assert_eq!( |
| 398 | crop(&picture, &window, &mut y, &mut u, &mut v), |
| 399 | Some((12, 6)) |
| 400 | ); |
| 401 | assert_eq!(y.len(), 12 * 6); |
| 402 | assert_eq!(&y[..12], &[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); |
| 403 | assert_eq!(y[12], 18, "the second row starts two samples in, at y = 1"); |
| 404 | assert_eq!( |
| 405 | (u.len(), v.len()), |
| 406 | (6 * 3, 6 * 3), |
| 407 | "chroma crops by the same units" |
| 408 | ); |
| 409 | assert!(u.iter().all(|&s| s == 7) && v.iter().all(|&s| s == 9)); |
| 410 | |
| 411 | |
| 412 | let all = ConformanceWindow { |
| 413 | left: 4, |
| 414 | right: 4, |
| 415 | top: 0, |
| 416 | bottom: 0, |
| 417 | }; |
| 418 | assert_eq!(crop(&picture, &all, &mut y, &mut u, &mut v), None); |
| 419 | } |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | #[test] |
| 425 | fn decoded_planes_match_ffmpeg_sample_for_sample() { |
| 426 | let cases = [ |
| 427 | ("test_hevcsw_8.mp4", 352, 288, "yuv420p", 1), |
| 428 | ("test_hevcsw_8.mkv", 352, 288, "yuv420p", 1), |
| 429 | ("test_hevcsw_10.mp4", 352, 288, "yuv420p10le", 2), |
| 430 | ("test_hevcsw_10.mkv", 352, 288, "yuv420p10le", 2), |
| 431 | ("test_hevcsw_odd.mp4", 350, 286, "yuv420p", 1), |
| 432 | ("test_hevcsw_cra.mp4", 352, 288, "yuv420p", 1), |
| 433 | ]; |
| 434 | for (name, w, h, pix_fmt, bytes) in cases { |
| 435 | let path = asset(name); |
| 436 | let want = ffmpeg_planes(&path, w, h, pix_fmt, bytes); |
| 437 | let got = ec_planes(&path); |
| 438 | assert_eq!(got.len(), want.len(), "{name}: picture count"); |
| 439 | for (i, (g, e)) in got.iter().zip(&want).enumerate() { |
| 440 | assert!(g == e, "{name}: picture {i} differs from ffmpeg"); |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | #[test] |
| 449 | fn crop_takes_422_to_420_by_averaging_chroma_row_pairs() { |
| 450 | let (w, h) = (16usize, 8usize); |
| 451 | |
| 452 | let chroma: Vec<u16> = (0..8u16) |
| 453 | .flat_map(|r| std::iter::repeat_n(10 * r, 8)) |
| 454 | .collect(); |
| 455 | let picture = DecodedPicture { |
| 456 | width: w, |
| 457 | height: h, |
| 458 | bit_depth: 8, |
| 459 | bit_depth_c: 8, |
| 460 | chroma_format_idc: 2, |
| 461 | y: vec![3; w * h], |
| 462 | cb: chroma.clone(), |
| 463 | cr: chroma, |
| 464 | }; |
| 465 | assert!(carried(&picture)); |
| 466 | |
| 467 | |
| 468 | let window = ConformanceWindow { |
| 469 | left: 1, |
| 470 | right: 1, |
| 471 | top: 0, |
| 472 | bottom: 1, |
| 473 | }; |
| 474 | let (mut y, mut u, mut v) = (Vec::new(), Vec::new(), Vec::new()); |
| 475 | assert_eq!( |
| 476 | crop(&picture, &window, &mut y, &mut u, &mut v), |
| 477 | Some((12, 7)) |
| 478 | ); |
| 479 | assert_eq!(y.len(), 12 * 7); |
| 480 | |
| 481 | |
| 482 | let want: Vec<u16> = [5u16, 25, 45, 60] |
| 483 | .iter() |
| 484 | .flat_map(|&r| std::iter::repeat_n(r, 6)) |
| 485 | .collect(); |
| 486 | assert_eq!(u, want); |
| 487 | assert_eq!(v, want); |
| 488 | } |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | fn seat_planes(path: &std::path::Path, bits: u32) -> Vec<[Vec<u16>; 3]> { |
| 494 | let (_, mut demuxer) = crate::demux::Demuxer::open(path).expect("open the clip"); |
| 495 | let mut stream = Stream::open(&mut demuxer, 0); |
| 496 | let mut out = Vec::new(); |
| 497 | if bits == 8 { |
| 498 | let mut picture = I420::default(); |
| 499 | while stream |
| 500 | .next(&mut demuxer, &mut picture) |
| 501 | .expect("decode") |
| 502 | .is_some() |
| 503 | { |
| 504 | out.push([ |
| 505 | picture.y.iter().map(|&s| u16::from(s)).collect(), |
| 506 | picture.u.iter().map(|&s| u16::from(s)).collect(), |
| 507 | picture.v.iter().map(|&s| u16::from(s)).collect(), |
| 508 | ]); |
| 509 | } |
| 510 | } else { |
| 511 | let mut picture = Wide::default(); |
| 512 | while stream |
| 513 | .next_wide(&mut demuxer, &mut picture) |
| 514 | .expect("decode") |
| 515 | .is_some() |
| 516 | { |
| 517 | out.push([picture.y.clone(), picture.u.clone(), picture.v.clone()]); |
| 518 | } |
| 519 | } |
| 520 | out |
| 521 | } |
| 522 | |
| 523 | |
| 524 | fn psnr(got: &[[Vec<u16>; 3]], want: &[[Vec<u16>; 3]], peak: f64) -> f64 { |
| 525 | let (mut squared, mut n) = (0f64, 0usize); |
| 526 | for (g, w) in got.iter().zip(want) { |
| 527 | for (gp, wp) in g.iter().zip(w) { |
| 528 | for (&a, &b) in gp.iter().zip(wp) { |
| 529 | let d = f64::from(a) - f64::from(b); |
| 530 | squared += d * d; |
| 531 | n += 1; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | 10.0 * (peak * peak * n as f64 / squared).log10() |
| 536 | } |
| 537 | |
| 538 | |
| 539 | |
| 540 | fn gen_422(name: &str, pix_fmt: &str) -> PathBuf { |
| 541 | let path = std::env::temp_dir().join(format!("{name}-{}.mp4", std::process::id())); |
| 542 | let out = Command::new("ffmpeg") |
| 543 | .args(["-v", "error", "-y", "-f", "lavfi", "-i"]) |
| 544 | .arg("testsrc2=size=352x288:rate=30:duration=1") |
| 545 | .args(["-frames:v", "30", "-c:v", "libx265", "-pix_fmt", pix_fmt]) |
| 546 | .args(["-x265-params", "log-level=error"]) |
| 547 | .arg(&path) |
| 548 | .output() |
| 549 | .expect("run ffmpeg: the clips are encoded with it"); |
| 550 | assert!( |
| 551 | out.status.success(), |
| 552 | "ffmpeg: {}", |
| 553 | String::from_utf8_lossy(&out.stderr) |
| 554 | ); |
| 555 | path |
| 556 | } |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | #[test] |
| 564 | fn rext_streams_decode_to_420_within_psnr_of_ffmpeg() { |
| 565 | for (src, dst, bits, bytes, floor) in [ |
| 566 | ("yuv422p", "yuv420p", 8, 1, 45.0), |
| 567 | ("yuv422p10le", "yuv420p10le", 10, 2, 45.0), |
| 568 | ("yuv422p12le", "yuv420p10le", 10, 2, 45.0), |
| 569 | ("yuv444p", "yuv420p", 8, 1, 38.0), |
| 570 | ("yuv444p10le", "yuv420p10le", 10, 2, 38.0), |
| 571 | ("yuv444p12le", "yuv420p10le", 10, 2, 38.0), |
| 572 | ("gray", "gray", 8, 1, 45.0), |
| 573 | ("gray10le", "gray10le", 10, 2, 45.0), |
| 574 | ("gray12le", "gray10le", 10, 2, 45.0), |
| 575 | ] { |
| 576 | let clip = gen_422(&format!("hevcrext-unit-{src}"), src); |
| 577 | let got = seat_planes(&clip, bits); |
| 578 | let want = ffmpeg_planes(&clip, 352, 288, dst, bytes); |
| 579 | let _ = std::fs::remove_file(&clip); |
| 580 | assert_eq!(got.len(), 30, "{src}: every frame comes out"); |
| 581 | assert_eq!(got.len(), want.len(), "{src}: picture count"); |
| 582 | let db = psnr(&got, &want, f64::from((1u32 << bits) - 1)); |
| 583 | eprintln!("{src} -> {dst} against ffmpeg: {db:.2} dB"); |
| 584 | assert!(db > floor, "{src} is {db:.2} dB from ffmpeg, want above {floor}"); |
| 585 | if src.starts_with("gray") { |
| 586 | let neutral = 1u16 << (bits - 1); |
| 587 | assert!( |
| 588 | got.iter().all(|f| f[1].iter().chain(&f[2]).all(|&s| s == neutral)), |
| 589 | "{src}: chroma is neutral" |
| 590 | ); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | |
| 596 | fn ec_planes(path: &std::path::Path) -> Vec<[Vec<u16>; 3]> { |
| 597 | let (_, mut demuxer) = crate::demux::Demuxer::open(path).expect("open the fixture"); |
| 598 | let mut decoder = Decoder::new(); |
| 599 | let mut out = Vec::new(); |
| 600 | let (mut y, mut u, mut v) = (Vec::new(), Vec::new(), Vec::new()); |
| 601 | let mut take = |decoder: &mut Decoder, out: &mut Vec<[Vec<u16>; 3]>| { |
| 602 | while let Some(decoded) = decoder.next_frame() { |
| 603 | assert!(carried(&decoded.picture), "fixture outside 8/10-bit 4:2:0"); |
| 604 | crop(&decoded.picture, &decoded.window, &mut y, &mut u, &mut v) |
| 605 | .expect("the window fits the picture"); |
| 606 | out.push([y.clone(), u.clone(), v.clone()]); |
| 607 | } |
| 608 | }; |
| 609 | while let Some(au) = demuxer.next_access_unit().expect("read") { |
| 610 | decoder.decode_annex_b(&au).expect("decode"); |
| 611 | take(&mut decoder, &mut out); |
| 612 | } |
| 613 | decoder.flush().expect("flush"); |
| 614 | take(&mut decoder, &mut out); |
| 615 | out |
| 616 | } |
| 617 | |
| 618 | |
| 619 | |
| 620 | fn ffmpeg_planes( |
| 621 | path: &std::path::Path, |
| 622 | w: usize, |
| 623 | h: usize, |
| 624 | pix_fmt: &str, |
| 625 | bytes: usize, |
| 626 | ) -> Vec<[Vec<u16>; 3]> { |
| 627 | let out = Command::new("ffmpeg") |
| 628 | .args(["-v", "error", "-i"]) |
| 629 | .arg(path) |
| 630 | .args([ |
| 631 | "-f", |
| 632 | "rawvideo", |
| 633 | "-pix_fmt", |
| 634 | pix_fmt, |
| 635 | "-fps_mode", |
| 636 | "passthrough", |
| 637 | "-", |
| 638 | ]) |
| 639 | .output() |
| 640 | .expect("run ffmpeg: the fixtures are made with it, see scripts/gen_fixtures.sh"); |
| 641 | assert!(out.status.success(), "ffmpeg failed on {}", path.display()); |
| 642 | |
| 643 | |
| 644 | let gray = pix_fmt.starts_with("gray"); |
| 645 | let (cw, ch) = if gray { (0, 0) } else { (w.div_ceil(2), h.div_ceil(2)) }; |
| 646 | let frame = (w * h + 2 * cw * ch) * bytes; |
| 647 | assert_eq!( |
| 648 | out.stdout.len() % frame, |
| 649 | 0, |
| 650 | "ffmpeg handed back a partial frame" |
| 651 | ); |
| 652 | out.stdout |
| 653 | .chunks_exact(frame) |
| 654 | .map(|f| { |
| 655 | let wide: Vec<u16> = if bytes == 1 { |
| 656 | f.iter().map(|&s| u16::from(s)).collect() |
| 657 | } else { |
| 658 | f.as_chunks::<2>() |
| 659 | .0 |
| 660 | .iter() |
| 661 | .map(|p| u16::from_le_bytes(*p)) |
| 662 | .collect() |
| 663 | }; |
| 664 | let (y, uv) = wide.split_at(w * h); |
| 665 | let (u, v) = uv.split_at(cw * ch); |
| 666 | [y.to_vec(), u.to_vec(), v.to_vec()] |
| 667 | }) |
| 668 | .collect() |
| 669 | } |
| 670 | } |