forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'ec-hw-10bit-path'

Browse files at this commit

e7509e88e45e01c67f53afe7bc51631824d11354Ahmet Kaan GümüşParent: 5f50798a6922c1
crates/ec-hw/tests/gpu.rsmodified+205 -0
@@ -12,6 +12,7 @@ use std::io::Read;
12 use std::path::{Path, PathBuf};12 use std::path::{Path, PathBuf};
13 use std::process::{Command, Stdio};13 use std::process::{Command, Stdio};
14 use std::sync::Arc;14 use std::sync::Arc;
15+use std::time::{Duration, Instant};
15 16
16 use ec_hw::{17 use ec_hw::{
17 Codec, Decoder, EncCodec, Encoder, EncoderConfig, Frame, FrameMetadata, I420, RateControlMode,18 Codec, Decoder, EncCodec, Encoder, EncoderConfig, Frame, FrameMetadata, I420, RateControlMode,
@@ -1193,6 +1194,210 @@ fn short(path: &Path) -> String {
1193 .unwrap_or_default()1194 .unwrap_or_default()
1194 }1195 }
1195 1196
1197+/// PSNR of one plane, in dB (`f64::INFINITY` for an exact match).
1198+fn plane_psnr(a: &[u16], b: &[u16], peak: f64) -> f64 {
1199+ let n = a.len().min(b.len());
1200+ let mse: f64 = a[..n]
1201+ .iter()
1202+ .zip(&b[..n])
1203+ .map(|(&p, &q)| {
1204+ let d = f64::from(p) - f64::from(q);
1205+ d * d
1206+ })
1207+ .sum::<f64>()
1208+ / n as f64;
1209+ if mse == 0.0 {
1210+ f64::INFINITY
1211+ } else {
1212+ 10.0 * (peak * peak / mse).log10()
1213+ }
1214+}
1215+
1216+/// P010 correctness on the real 4K HDR film, plane by plane.
1217+///
1218+/// `real_library_spot_check` already pins the whole-frame PSNR at >= 40 dB
1219+/// for a mix of files; this isolates Y from U/V on this specific 10-bit film,
1220+/// against both `yuv420p10le` (the lossless comparison) and `yuv420p` (the
1221+/// truncating `to_i420` path, which rounds ffmpeg's dithered 8-bit output
1222+/// against this crate's un-dithered right-shift -- a real but small gap, not
1223+/// a bug). A collapsed U/V PSNR here (a grey frame) would mean the chroma
1224+/// plane is being read at the wrong stride/offset; it is not, on this file.
1225+#[test]
1226+fn ten_bit_4k_chroma_is_not_grey() {
1227+ let Some(display) = display() else { return };
1228+ let path = Path::new(
1229+ "/home/user/Downloads/reeld\
1230+ /reeld.mkv",
1231+ );
1232+ if !path.exists() {
1233+ eprintln!("skipped: film not present");
1234+ return;
1235+ }
1236+ let frames = 3usize;
1237+ let Some(data) = elementary(path, Codec::H265, frames) else {
1238+ eprintln!("skipped: no elementary stream");
1239+ return;
1240+ };
1241+ let mut decoder = Decoder::new(&display, Codec::H265).expect("decoder opens");
1242+ let mut ref10: Option<Reference> = None;
1243+ let mut ref8: Option<Reference> = None;
1244+ let mut compared = 0usize;
1245+ decode_stream(&mut decoder, Codec::H265, &data, frames, |frame| {
1246+ let (w, h) = frame.display_size;
1247+ let ref10 = ref10.get_or_insert_with(|| Reference::open(path, w, h, true).expect("ffmpeg runs"));
1248+ let want10 = ref10.next(w, h).expect("ffmpeg has a frame");
1249+ let got16 = Planes::from_16bit(&frame.to_i420_16().expect("readback"));
1250+ let y_db = plane_psnr(&got16.y, &want10.y, 1023.0);
1251+ let uv_db = plane_psnr(
1252+ &[got16.u.as_slice(), got16.v.as_slice()].concat(),
1253+ &[want10.u.as_slice(), want10.v.as_slice()].concat(),
1254+ 1023.0,
1255+ );
1256+ println!("frame {compared} to_i420_16 vs yuv420p10le: Y {y_db:.1} dB, UV {uv_db:.1} dB");
1257+ assert!(y_db >= 50.0, "frame {compared}: Y PSNR collapsed to {y_db:.1} dB");
1258+ assert!(
1259+ uv_db >= 50.0,
1260+ "frame {compared}: chroma PSNR collapsed to {uv_db:.1} dB (grey frame)"
1261+ );
1262+
1263+ let ref8 = ref8.get_or_insert_with(|| Reference::open(path, w, h, false).expect("ffmpeg runs"));
1264+ let want8 = ref8.next(w, h).expect("ffmpeg has a frame");
1265+ let got8 = Planes::from_8bit(&frame.to_i420().expect("readback"));
1266+ let y_db8 = plane_psnr(&got8.y, &want8.y, 255.0);
1267+ let uv_db8 = plane_psnr(
1268+ &[got8.u.as_slice(), got8.v.as_slice()].concat(),
1269+ &[want8.u.as_slice(), want8.v.as_slice()].concat(),
1270+ 255.0,
1271+ );
1272+ println!("frame {compared} to_i420 vs yuv420p: Y {y_db8:.1} dB, UV {uv_db8:.1} dB");
1273+ // ffmpeg dithers its 8-bit downconversion; this crate truncates. Both
1274+ // are legitimate 10-to-8 roundings, so the floor here is the
1275+ // dithering noise floor, not the lossless bound above.
1276+ assert!(y_db8 >= 30.0, "frame {compared}: 8-bit Y PSNR {y_db8:.1} dB");
1277+ assert!(
1278+ uv_db8 >= 30.0,
1279+ "frame {compared}: 8-bit chroma PSNR collapsed to {uv_db8:.1} dB (grey frame)"
1280+ );
1281+ compared += 1;
1282+ })
1283+ .expect("decode");
1284+ assert_eq!(compared, frames, "not all frames were compared");
1285+}
1286+
1287+/// Per-frame cost of the 4K 10-bit read-back path, isolated from decode
1288+/// submission itself.
1289+///
1290+/// The consumer's `hw_decode::the_hdr_film_renders_tone_mapped` measured
1291+/// 1352.5 ms/frame against a 39 ms (24 fps) budget on this film. Decode
1292+/// correctness on it is already pinned by `real_library_spot_check` (bit-exact
1293+/// against ffmpeg), so this isolates which stage of the frame path -- decode,
1294+/// 8-bit readback, 10-bit readback or DRM PRIME export -- the time goes to.
1295+#[test]
1296+fn ten_bit_4k_frame_path_is_realtime() {
1297+ let Some(display) = display() else { return };
1298+ let path = Path::new(
1299+ "/home/user/Downloads/reeld\
1300+ /reeld.mkv",
1301+ );
1302+ if !path.exists() {
1303+ eprintln!("skipped: film not present");
1304+ return;
1305+ }
1306+ let limit = 120usize;
1307+ let Some(data) = elementary(path, Codec::H265, limit) else {
1308+ eprintln!("skipped: no elementary stream");
1309+ return;
1310+ };
1311+ let units = split_access_units(&data, Codec::H265);
1312+ assert!(units.len() > limit, "film clip too short for {limit} frames");
1313+
1314+ #[derive(Clone, Copy)]
1315+ enum Op {
1316+ DecodeOnly,
1317+ ToI420,
1318+ ToI420_16,
1319+ ExportPrime,
1320+ }
1321+ fn apply(op: Op, frame: Frame) {
1322+ match op {
1323+ Op::DecodeOnly => drop(frame),
1324+ Op::ToI420 => {
1325+ frame.to_i420().expect("readback");
1326+ }
1327+ Op::ToI420_16 => {
1328+ frame.to_i420_16().expect("readback");
1329+ }
1330+ Op::ExportPrime => {
1331+ frame.export_prime().expect("export");
1332+ }
1333+ }
1334+ }
1335+
1336+ println!(
1337+ "{:<22} {:>10} {:>10} {:>10}",
1338+ "stage", "median ms", "min ms", "max ms"
1339+ );
1340+ let mut got_to_i420_16 = None;
1341+ for (name, op) in [
1342+ ("decode-only", Op::DecodeOnly),
1343+ ("decode+to_i420", Op::ToI420),
1344+ ("decode+to_i420_16", Op::ToI420_16),
1345+ ("decode+export_prime", Op::ExportPrime),
1346+ ] {
1347+ let mut decoder = Decoder::new(&display, Codec::H265).expect("decoder opens");
1348+ let mut durations: Vec<Duration> = Vec::new();
1349+ // Hold a few frames before reading, as the resident-set test does:
1350+ // reading one frame per submission measures latency, not throughput,
1351+ // and lets decode run ahead of read-back the way a real player does.
1352+ let mut pending: Vec<Frame> = Vec::new();
1353+ let mut t_prev = Instant::now();
1354+ 'outer: for (i, unit) in units.iter().enumerate() {
1355+ decoder.decode(unit, i as i64).expect("decode");
1356+ while let Some(frame) = decoder.next_frame() {
1357+ pending.push(frame);
1358+ if pending.len() < 4 {
1359+ continue;
1360+ }
1361+ for frame in pending.drain(..) {
1362+ apply(op, frame);
1363+ let now = Instant::now();
1364+ durations.push(now.duration_since(t_prev));
1365+ t_prev = now;
1366+ }
1367+ if durations.len() >= limit {
1368+ break 'outer;
1369+ }
1370+ }
1371+ }
1372+ decoder.flush().expect("flush");
1373+ while durations.len() < limit {
1374+ let Some(frame) = decoder.next_frame() else {
1375+ break;
1376+ };
1377+ apply(op, frame);
1378+ let now = Instant::now();
1379+ durations.push(now.duration_since(t_prev));
1380+ t_prev = now;
1381+ }
1382+ assert!(
1383+ durations.len() >= 24,
1384+ "{name}: only {} frames decoded",
1385+ durations.len()
1386+ );
1387+ durations.sort();
1388+ let ms = |d: Duration| d.as_secs_f64() * 1000.0;
1389+ let median = ms(durations[durations.len() / 2]);
1390+ let min = ms(durations[0]);
1391+ let max = ms(durations[durations.len() - 1]);
1392+ println!("{name:<22} {median:>10.2} {min:>10.2} {max:>10.2}");
1393+ if name == "decode+to_i420_16" {
1394+ got_to_i420_16 = Some(median);
1395+ }
1396+ }
1397+ let got = got_to_i420_16.expect("to_i420_16 phase ran");
1398+ println!("target: decode+to_i420_16 <= 39.00 ms/frame median (24 fps budget); got {got:.2}");
1399+}
1400+
1196 /// A CRA GOP boundary stores its RASL leading pictures *after* the CRA in1401 /// A CRA GOP boundary stores its RASL leading pictures *after* the CRA in
1197 /// decode order, even though they display before it (they predict from1402 /// decode order, even though they display before it (they predict from
1198 /// pictures the CRA's own random-access point does not have). A decoder1403 /// pictures the CRA's own random-access point does not have). A decoder
crates/ec-matroska/tests/matroska.rsmodified+48 -0
@@ -809,3 +809,51 @@ fn same_class_sweep(path: &str, wants: &[f64]) {
809 );809 );
810 }810 }
811 }811 }
812+
813+/// `ffprobe` reports MaxCLL 1230 / MaxFALL 419 for this real HDR10 film, but
814+/// that reading comes from `[SIDE_DATA]` on the first *frame* (a
815+/// `content_light_level_information` HEVC SEI message inside the bitstream),
816+/// not from the container's own `Colour` element. This file's `Colour`
817+/// element genuinely carries no `MaxCLL`/`MaxFALL`/`MasteringMetadata`
818+/// children (checked with `ffprobe -show_entries stream=side_data_list`,
819+/// which comes back empty) — so a demuxer-only reader is correct to return
820+/// `None` here, and the consumer's brightness-metadata gap is downstream, in
821+/// whether anything reads the HEVC SEI (`ec_core::color::hevc_sei_light`
822+/// exists and round-trips in `ec-h265-syntax`, but nothing in this workspace
823+/// calls it from a real decode path yet).
824+#[test]
825+#[ignore = "reads a real local file, not a CI fixture"]
826+fn real_hdr_film_container_states_no_light_metadata() {
827+ use ec_core::{Demuxer, MediaParameters};
828+ use ec_matroska::MatroskaDemuxer;
829+ let path = "/home/user/Downloads/reeld/reeld.mkv";
830+ if !std::path::Path::new(path).exists() {
831+ eprintln!("skipped: film not present");
832+ return;
833+ }
834+ let f = std::fs::File::open(path).expect("open");
835+ let demux = MatroskaDemuxer::new(f).expect("demux opens");
836+ let mut checked = false;
837+ for s in demux.streams() {
838+ if let MediaParameters::Video(v) = &s.params.media {
839+ println!("color = {:?}", v.color);
840+ println!("light = {:?}", v.light);
841+ // BT.2020 non-constant luminance, PQ transfer, BT.2020 primaries —
842+ // this much the Colour element does carry, and it must match
843+ // ffprobe's color_space/color_primaries/color_transfer.
844+ assert_eq!(v.color.matrix, 9, "expected bt2020nc");
845+ assert_eq!(v.color.transfer, 16, "expected smpte2084 (PQ)");
846+ assert_eq!(v.color.primaries, 9, "expected bt2020");
847+ assert_eq!(
848+ v.light,
849+ ec_core::color::ContentLight::default(),
850+ "the Colour element has no light metadata for this file — a \
851+ non-default reading here would mean either ffprobe or this \
852+ reader started finding it in the container and the doc \
853+ comment above is stale"
854+ );
855+ checked = true;
856+ }
857+ }
858+ assert!(checked, "no video stream found");
859+}