🐛 Draw the self-check stream's cells in colours RGB keeps
A guest shows a decoded picture in 8-bit RGB, and a screen recording takes it back to 4:2:0 from RGB, as does a QMP screendump read as video. Fifteen of the 36 combinations of the flat cells' levels lie outside RGB (Y 40 with Cb or Cr at 80 or 176, Y 190 with Cb 176): such a cell comes back clipped, up to 32 code values off. On the 780M a QuickTime playback judged from its recording called WRONG in nearly every frame (1169 of 1241), each off cell an out-of-gamut one, every in-gamut cell within 12 of its levels. content_cell now draws each cell from CELL_COLOURS, the 18 combinations of the same levels whose colour lies 8 or more inside 8-bit RGB under both the BT.709 and the BT.601 matrix, so a player that takes either matrix leaves a cell within the judge's slack: a matrix the other way round moves a cell at most 15.5 luma and 6.4 chroma code values. The slacks and every other threshold are unchanged. The streams are tagged BT.709 at limited range, so the guest's player converts by the stated matrix. Tests: the colours are exactly the combinations inside RGB; each comes back from 8-bit RGB within the slack, by either matrix either way; pictures shown in RGB and taken back pass; a stale row of cells is still WRONG after that round trip. selfcheck.sh selftest adds the same through ffmpeg: the H.264 decode to rgb24 by BT.709 and by BT.601, back to 4:2:0 by BT.709 and re-encoded, judged clean, and with frame 60's row 2 cells frame 68's, judged WRONG.
This commit is contained in:
1 parent
cc1f47b30b
commit
d6ab1eaed7
4 files changed
+221
-18
No files matched your search
@@ -12,7 +12,11 @@
|
||||
//! - two rows of flat cells (2 and 5), each cell's luma and chroma levels drawn from its index
|
||||
//! and the frame's number over eight ([`content_cell`]), at a phase of its own, so a cell keeps
|
||||
//! its levels eight frames and about four of the 32 change each frame: most of a picture is
|
||||
//! its references', and a picture built from a wrong or stale reference shows it;
|
||||
//! its references', and a picture built from a wrong or stale reference shows it. A cell's
|
||||
//! levels are one of [`CELL_COLOURS`], the combinations of the levels whose colour lies inside
|
||||
//! 8-bit RGB under both the BT.709 and the BT.601 matrix: a player shows a picture in RGB and a
|
||||
//! screen recording takes it back from RGB, and a colour outside would come back clipped, far
|
||||
//! from its levels;
|
||||
//! - a row of stripes (6), [`STRIPES`] across the picture, each of a luma level of its own,
|
||||
//! moving a fifteenth of a stripe a frame ([`stripe_at`]), for the encoders' motion vectors.
|
||||
//!
|
||||
@@ -55,6 +59,34 @@ const WHITE: u8 = 235;
|
||||
const LUMA_LEVELS: [u8; 4] = [40, 90, 140, 190];
|
||||
/// A flat cell's chroma levels, 48 apart.
|
||||
const CHROMA_LEVELS: [u8; 3] = [80, 128, 176];
|
||||
/// A flat cell's luma, Cb and Cr: every combination of [`LUMA_LEVELS`] and [`CHROMA_LEVELS`]
|
||||
/// whose colour, at limited range, lies [`RGB_MARGIN`] or more inside 8-bit RGB's 0 to 255 under
|
||||
/// both the BT.709 and the BT.601 matrix (`tests::cell_colours_are_the_combinations_inside_rgb`
|
||||
/// keeps the list equal to that set), so that a player that shows the picture in RGB by either
|
||||
/// matrix, and a recording that takes it back, leave each cell's levels within the judge's slack.
|
||||
pub const CELL_COLOURS: [(u8, u8, u8); 18] = [
|
||||
(40, 128, 128),
|
||||
(40, 176, 128),
|
||||
(90, 128, 128),
|
||||
(90, 128, 176),
|
||||
(90, 176, 128),
|
||||
(90, 176, 176),
|
||||
(140, 80, 80),
|
||||
(140, 80, 128),
|
||||
(140, 80, 176),
|
||||
(140, 128, 80),
|
||||
(140, 128, 128),
|
||||
(140, 128, 176),
|
||||
(140, 176, 80),
|
||||
(140, 176, 128),
|
||||
(140, 176, 176),
|
||||
(190, 80, 128),
|
||||
(190, 128, 80),
|
||||
(190, 128, 128),
|
||||
];
|
||||
/// How far inside 8-bit RGB's 0 to 255 each of [`CELL_COLOURS`] lies: room for a lossy codec's
|
||||
/// noise before a channel clips.
|
||||
pub const RGB_MARGIN: f64 = 8.0;
|
||||
/// How far a cell's luma may lie from its level, on the scale the references set, and its
|
||||
/// chroma from its level: half the levels' spacing.
|
||||
const LUMA_SLACK: f64 = 25.0;
|
||||
@@ -149,16 +181,13 @@ fn mix(a: u64, b: u64) -> u64 {
|
||||
}
|
||||
|
||||
/// The luma, Cb and Cr of flat cell `cell` (0 to 31, row 2's then row 5's, left to right) in
|
||||
/// frame `n`: its levels change every [`CELL_EPOCH`] frames, at a phase of its own.
|
||||
/// frame `n`, one of [`CELL_COLOURS`]: its levels change every [`CELL_EPOCH`] frames, at a phase
|
||||
/// of its own.
|
||||
pub fn content_cell(n: u64, cell: u64) -> (u8, u8, u8) {
|
||||
let n = n % PERIOD;
|
||||
let epoch = (n + 3 * cell) / CELL_EPOCH;
|
||||
let h = mix(cell + 1, epoch);
|
||||
(
|
||||
LUMA_LEVELS[(h % 4) as usize],
|
||||
CHROMA_LEVELS[(h / 4 % 3) as usize],
|
||||
CHROMA_LEVELS[(h / 12 % 3) as usize],
|
||||
)
|
||||
CELL_COLOURS[(h % CELL_COLOURS.len() as u64) as usize]
|
||||
}
|
||||
|
||||
/// The stripe at `x` of a picture `width` wide in frame `n`, and where in it `x` lies, as a
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! The judge on pictures drawn here, each fault class made by hand: frames dropped, repeated and
|
||||
//! out of order, pictures of zeros, torn, stale and with their chroma swapped, and a recording's
|
||||
//! frames, the picture scaled into a screen and held.
|
||||
//! out of order, pictures of zeros, torn, stale and with their chroma swapped, a recording's
|
||||
//! frames, the picture scaled into a screen and held, and pictures shown in 8-bit RGB and taken
|
||||
//! back, as a player shows them and a screen recording takes them.
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -94,6 +95,61 @@ fn on_screen(picture: Option<&Frame>, width: u32, height: u32, region: Region) -
|
||||
screen
|
||||
}
|
||||
|
||||
/// A matrix's Kr and Kb: BT.709's and BT.601's.
|
||||
const BT709: (f64, f64) = (0.2126, 0.0722);
|
||||
const BT601: (f64, f64) = (0.299, 0.114);
|
||||
|
||||
/// The 8-bit RGB, unrounded and unclipped, of limited-range luma, Cb and Cr by the matrix
|
||||
/// `(kr, kb)`.
|
||||
fn to_rgb((y, cb, cr): (u8, u8, u8), (kr, kb): (f64, f64)) -> [f64; 3] {
|
||||
let y = (f64::from(y) - 16.0) * 255.0 / 219.0;
|
||||
let u = (f64::from(cb) - 128.0) * 255.0 / 224.0;
|
||||
let v = (f64::from(cr) - 128.0) * 255.0 / 224.0;
|
||||
let r = y + 2.0 * (1.0 - kr) * v;
|
||||
let b = y + 2.0 * (1.0 - kb) * u;
|
||||
let g = (y - kr * r - kb * b) / (1.0 - kr - kb);
|
||||
[r, g, b]
|
||||
}
|
||||
|
||||
/// `rgb` as 8-bit samples: rounded, and clipped to 0 to 255 as a display's are.
|
||||
fn rgb8(rgb: [f64; 3]) -> [u8; 3] {
|
||||
rgb.map(|c| c.round().clamp(0.0, 255.0) as u8)
|
||||
}
|
||||
|
||||
/// 8-bit RGB back to limited-range luma, Cb and Cr by the matrix `(kr, kb)`, rounded.
|
||||
fn from_rgb([r, g, b]: [u8; 3], (kr, kb): (f64, f64)) -> (u8, u8, u8) {
|
||||
let (r, g, b) = (f64::from(r), f64::from(g), f64::from(b));
|
||||
let y = kr * r + (1.0 - kr - kb) * g + kb * b;
|
||||
let u = (b - y) / (2.0 * (1.0 - kb));
|
||||
let v = (r - y) / (2.0 * (1.0 - kr));
|
||||
let q = |x: f64| x.round().clamp(0.0, 255.0) as u8;
|
||||
(
|
||||
q(16.0 + y * 219.0 / 255.0),
|
||||
q(128.0 + u * 224.0 / 255.0),
|
||||
q(128.0 + v * 224.0 / 255.0),
|
||||
)
|
||||
}
|
||||
|
||||
/// `frame` shown in 8-bit RGB by the matrix `shown` and taken back by `captured`, as a player
|
||||
/// shows a picture and a screen recording takes it: each pixel's luma with its 2x2 block's
|
||||
/// chroma, and each block's chroma its top left pixel's.
|
||||
fn through_rgb(frame: &Frame, shown: (f64, f64), captured: (f64, f64)) -> Frame {
|
||||
let mut out = frame.clone();
|
||||
for y in 0..frame.height {
|
||||
for x in 0..frame.width {
|
||||
let (cb, cr) = frame.chroma(x, y);
|
||||
let (ly, lu, lv) = from_rgb(rgb8(to_rgb((frame.luma(x, y), cb, cr), shown)), captured);
|
||||
out.y[(y * frame.width + x) as usize] = ly;
|
||||
if x % 2 == 0 && y % 2 == 0 {
|
||||
let at = ((y / 2) * (frame.width / 2) + x / 2) as usize;
|
||||
out.u[at] = lu;
|
||||
out.v[at] = lv;
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_picture_reads_back_as_its_number_with_nothing_wrong() {
|
||||
for n in [0, 1, 2, 7, 8, 777, 8191, 16383] {
|
||||
@@ -294,6 +350,90 @@ fn swapped_chroma_is_wrong() {
|
||||
assert_eq!(verdict.count("WRONG"), 1, "{}", verdict.line);
|
||||
}
|
||||
|
||||
/// [`CELL_COLOURS`] is every combination of the levels whose colour lies [`RGB_MARGIN`] or more
|
||||
/// inside 8-bit RGB under both BT.709 and BT.601, in the levels' order, and no other.
|
||||
#[test]
|
||||
fn cell_colours_are_the_combinations_inside_rgb() {
|
||||
let mut inside = Vec::new();
|
||||
for y in LUMA_LEVELS {
|
||||
for cb in CHROMA_LEVELS {
|
||||
for cr in CHROMA_LEVELS {
|
||||
let fits = [BT709, BT601].iter().all(|&matrix| {
|
||||
(to_rgb((y, cb, cr), matrix).iter())
|
||||
.all(|&c| (RGB_MARGIN..=255.0 - RGB_MARGIN).contains(&c))
|
||||
});
|
||||
if fits {
|
||||
inside.push((y, cb, cr));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(inside, CELL_COLOURS.to_vec());
|
||||
let lumas: std::collections::BTreeSet<u8> = CELL_COLOURS.iter().map(|c| c.0).collect();
|
||||
assert_eq!(
|
||||
lumas.len(),
|
||||
LUMA_LEVELS.len(),
|
||||
"every luma level has a colour"
|
||||
);
|
||||
}
|
||||
|
||||
/// Each cell colour comes back from 8-bit RGB within the judge's slack: shown and taken back by
|
||||
/// one matrix, or by BT.709 and BT.601 one each way.
|
||||
#[test]
|
||||
fn each_cell_colour_comes_back_from_8_bit_rgb_within_slack() {
|
||||
for colour in CELL_COLOURS {
|
||||
for shown in [BT709, BT601] {
|
||||
for captured in [BT709, BT601] {
|
||||
let (y, cb, cr) = from_rgb(rgb8(to_rgb(colour, shown)), captured);
|
||||
assert!(
|
||||
f64::from(y.abs_diff(colour.0)) <= LUMA_SLACK
|
||||
&& f64::from(cb.abs_diff(colour.1)) <= CHROMA_SLACK
|
||||
&& f64::from(cr.abs_diff(colour.2)) <= CHROMA_SLACK,
|
||||
"{colour:?} shown by {shown:?}, taken back by {captured:?}: {:?}",
|
||||
(y, cb, cr)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A stream shown in 8-bit RGB and taken back, by one matrix or by the two one each way, passes:
|
||||
/// every picture reads as its number with nothing wrong.
|
||||
#[test]
|
||||
fn pictures_shown_in_rgb_and_taken_back_pass() {
|
||||
for (shown, captured) in [(BT709, BT709), (BT601, BT601), (BT601, BT709), (BT709, BT601)] {
|
||||
let frames: Vec<Frame> = (pictures(0..48).iter())
|
||||
.map(|frame| through_rgb(frame, shown, captured))
|
||||
.collect();
|
||||
let (_, verdict) = judged(Mode::File, None, Some(48), &frames);
|
||||
assert!(
|
||||
verdict.pass,
|
||||
"shown by {shown:?}, taken back by {captured:?}: {}",
|
||||
verdict.line
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A picture whose flat cells are an earlier frame's is still wrong once shown in RGB and taken
|
||||
/// back: the colours' round trip leaves a stale cell showing.
|
||||
#[test]
|
||||
fn stale_cells_are_wrong_after_rgb() {
|
||||
let mut frames = pictures(0..40);
|
||||
let (top, bottom) = (edge(2, ROWS, H), edge(3, ROWS, H));
|
||||
frames[30] = rows_from(&frames[30], &picture(14, W, H), top, bottom);
|
||||
let frames: Vec<Frame> = (frames.iter())
|
||||
.map(|frame| through_rgb(frame, BT709, BT709))
|
||||
.collect();
|
||||
let (calls, verdict) = judged(Mode::File, None, Some(40), &frames);
|
||||
assert_eq!(calls[30].number, Some(30));
|
||||
assert!(
|
||||
matches!(calls[30].faults.as_slice(), [Fault::Wrong { .. }]),
|
||||
"{:?}",
|
||||
calls[30].faults
|
||||
);
|
||||
assert_eq!(verdict.count("WRONG"), 1, "{}", verdict.line);
|
||||
}
|
||||
|
||||
/// A frame of no picture, all mid grey, does not read.
|
||||
#[test]
|
||||
fn a_frame_of_no_picture_is_unreadable() {
|
||||
|
||||
@@ -16,7 +16,8 @@
|
||||
# encoder the host lacks is skipped, saying so. The streams are made from
|
||||
# scratch, by the crate's selfcheck-frames example, on our hosts; x264, x265,
|
||||
# libvpx, SVT-AV1, aom and ffmpeg are test oracles alone, and nothing they write
|
||||
# is committed.
|
||||
# is committed. Each stream is tagged BT.709 (primaries, transfer and matrix) at
|
||||
# limited range, so that a player converts it to RGB by the matrix it states.
|
||||
#
|
||||
# judge decodes each FILE with ffmpeg in software, each stored frame once
|
||||
# (-fps_mode passthrough), into 8-bit 4:2:0, and judges its frames: a line for
|
||||
@@ -36,9 +37,13 @@
|
||||
# class made from the H.264 stream by an ffmpeg filter on its decode, each of
|
||||
# which must be called: frames dropped, repeated and reordered, a picture of
|
||||
# zeros, one partly of zeros, one torn of two frames, one whose flat cells are
|
||||
# a later frame's, one whose chroma planes are swapped; and a recording of its
|
||||
# a later frame's, one whose chroma planes are swapped; a recording of its
|
||||
# playback (60 fps, scaled into a larger screen, re-encoded) judged clean with
|
||||
# its crop. It needs no GPU.
|
||||
# its crop; and the H.264 stream's decode shown in 8-bit RGB and taken back to
|
||||
# 4:2:0 by BT.709, as a screen recording takes a player's picture, re-encoded
|
||||
# and judged clean, shown by BT.709 and, as a player that takes the other
|
||||
# matrix, by BT.601, and once with frame 60's flat cells of row 2 frame 68's,
|
||||
# which must still be called WRONG. It needs no GPU.
|
||||
set -euo pipefail
|
||||
source "$(dirname "$0")/lib.sh"
|
||||
|
||||
@@ -60,6 +65,10 @@ encoders() {
|
||||
return 0
|
||||
}
|
||||
|
||||
# The streams' colour: BT.709 primaries, transfer and matrix, limited range,
|
||||
# said of the raw pictures and of each stream.
|
||||
tags=(-color_primaries bt709 -color_trc bt709 -colorspace bt709 -color_range tv)
|
||||
|
||||
# encode NAME OUT SIZE RATE FRAMES: the stream of codec NAME (encoders'), its
|
||||
# pictures from selfcheck-frames, into OUT.
|
||||
encode() {
|
||||
@@ -79,7 +88,7 @@ encode() {
|
||||
esac
|
||||
"$examples/selfcheck-frames" "$width" "$height" "$frames" |
|
||||
ffmpeg -nostdin -hide_banner -loglevel error -y -f rawvideo -pix_fmt yuv420p \
|
||||
-video_size "$size" -framerate "$rate" -i - "${args[@]}" "$out"
|
||||
-video_size "$size" -framerate "$rate" "${tags[@]}" -i - "${args[@]}" "${tags[@]}" "$out"
|
||||
}
|
||||
|
||||
# file_of NAME: the stream's file name.
|
||||
@@ -214,6 +223,27 @@ selftest() {
|
||||
--recording --crop 130,76,480,270 --frames "$frames" || true)
|
||||
expect "the recording's frame of zeros" GREEN "$out"
|
||||
expect "the recording's stretch out of order" OUT-OF-ORDER "$out"
|
||||
# The decode in 8-bit RGB by MATRIX and back to 4:2:0 by BT.709, re-encoded.
|
||||
local rgb=$dir/rgb-h264.mp4 matrix
|
||||
through_rgb() {
|
||||
ffmpeg -nostdin -hide_banner -loglevel error -y -i "$h264" \
|
||||
-vf "${2:+$2,}scale=in_color_matrix=$1:in_range=tv,format=rgb24,scale=out_color_matrix=bt709:out_range=tv,format=yuv420p" \
|
||||
-c:v libx264 -crf 16 -pix_fmt yuv420p "${tags[@]}" "$rgb"
|
||||
}
|
||||
for matrix in bt709 bt601; do
|
||||
through_rgb "$matrix"
|
||||
out=$(judge_file "$rgb" "" --frames "$frames" || true)
|
||||
if [[ $(tail -1 <<< "$out") == *"; PASS" ]]; then
|
||||
echo "PASS shown in RGB by $matrix, taken back by bt709: $(tail -1 <<< "$out")"
|
||||
else
|
||||
echo "FAIL shown in RGB by $matrix, taken back by bt709:"
|
||||
echo "$out" | tail -20
|
||||
failed=1
|
||||
fi
|
||||
done
|
||||
through_rgb bt709 "split[a][b];[b]trim=start_frame=8,setpts=PTS-STARTPTS,crop=iw:ih/9:0:ih*2/9[row];[a][row]overlay=x=0:y=main_h*2/9:enable='eq(n,60)':eof_action=pass"
|
||||
out=$(judge_file "$rgb" "" --frames "$frames" || true)
|
||||
expect "frame 60's flat cells of row 2 frame 68's, shown in RGB and taken back" WRONG "$out"
|
||||
}
|
||||
|
||||
command=${1:-}
|
||||
|
||||
@@ -131,9 +131,11 @@ surface's pages, against VideoToolbox's own read back of each picture
|
||||
recording that no reference decode can be laid beside. Each picture carries its own frame number,
|
||||
in three bands of 14 bits and their complement, and what the rest of it should look like: flat
|
||||
cells whose levels change every eight frames at phases of their own, and stripes that move
|
||||
(the conformance crate's `selfcheck` module). `selfcheck.sh make` encodes them with the host's
|
||||
ffmpeg: H.264 High, HEVC Main and Main 10, VP9 and AV1, each whose encoder the host has, at
|
||||
1920x1080, 30 fps and 40 s by default. `selfcheck.sh judge` decodes a file with ffmpeg in
|
||||
(the conformance crate's `selfcheck` module). A flat cell's colour is one whose RGB lies inside
|
||||
8-bit RGB under both the BT.709 and the BT.601 matrix, as a player shows the picture and a screen
|
||||
recording takes it back, so that neither clips it. `selfcheck.sh make` encodes them with the
|
||||
host's ffmpeg: H.264 High, HEVC Main and Main 10, VP9 and AV1, each whose encoder the host has, at
|
||||
1920x1080, 30 fps and 40 s by default, each tagged BT.709 at limited range. `selfcheck.sh judge` decodes a file with ffmpeg in
|
||||
software and judges each frame from its pixels: repeated, out of order, torn (its bands
|
||||
disagree), wrong (its cells are not its number's picture's: a stale reference, swapped chroma),
|
||||
green or partly green (a picture of zeros, as `rec-judge.py` measures it), unreadable, and the
|
||||
@@ -141,8 +143,10 @@ runs of numbers no frame showed (dropped). A decode is judged as a file, every f
|
||||
order; a recording with `--recording`, the picture in its `--crop` region, where a picture held
|
||||
over several frames, and the frames before and after the stream, are no fault. `selfcheck.sh
|
||||
selftest`, in a job with no GPU, judges each codec's decode clean and each fault class called,
|
||||
each made from the H.264 stream by an ffmpeg filter, and a recording made from it (60 fps, scaled
|
||||
into a larger screen, re-encoded) clean.
|
||||
each made from the H.264 stream by an ffmpeg filter, a recording made from it (60 fps, scaled
|
||||
into a larger screen, re-encoded) clean, and its decode shown in 8-bit RGB by BT.709 and by
|
||||
BT.601, taken back to 4:2:0 by BT.709 and re-encoded, clean, with a stale row of cells still
|
||||
called WRONG through that round trip.
|
||||
|
||||
The encoders have no such suite, as no software reference gives a hardware encoder's bytes:
|
||||
`videotoolbox::encoder`'s ignored round trip encodes 30 frames at 1280x720 of each profile the
|
||||
|
||||
Reference in new issue
Block a user