Files
Chronos/tests/Chronos.Core.Tests/Video/Nv12ConverterTests.cs
T
Alby96andClaude Opus 5.5 5f27a86406
CI / Build and test (pull_request) Failing after 2m3s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / Docker image (pull_request) Skipped
CI / Windows tests and installer (pull_request) Skipped
CI / YAML check (pull_request) Successful in 4s
refactor!: rename Titano to Chronos and move CI and releases to Gitea
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout. The release cross-compiles the Windows programs, checks their
  icon, version and DPI manifest, packs installers and zips, publishes the
  Gitea release and pushes the server image (REGISTRY_TOKEN). Windows-only
  tests run in an opt-in job on a Windows runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow) and
  pe-check.

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 11:34:48 +02:00

182 lines
7.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Chronos.Core.Imaging;
using Chronos.Core.Video.Yuv;
using Chronos.Testing;
namespace Chronos.Core.Tests.Video;
public sealed class Nv12ConverterTests
{
[Theory]
[Case(1f, 1f, 1f, 235, 128, 128)]
[Case(0f, 0f, 0f, 16, 128, 128)]
[Case(1f, 0f, 0f, 63, 102, 240)]
[Case(0f, 1f, 0f, 173, 42, 26)]
[Case(0f, 0f, 1f, 32, 240, 118)]
public void FlatColoursHitBt709LimitedRangeCodes(float r, float g, float b, int y, int u, int v)
{
using var image = Flat(8, 4, r, g, b);
var nv12 = new byte[Nv12Converter.FrameLength(8, 4)];
Nv12Converter.ToNv12(image, nv12);
Assert.True(nv12.AsSpan(0, 32).ToArray().All(c => c == y), $"luma {nv12[0]} expected {y}");
Assert.Equal((byte)u, nv12[32]);
Assert.Equal((byte)v, nv12[33]);
var p010 = new ushort[Nv12Converter.FrameLength(8, 4)];
Nv12Converter.ToP010(image, p010);
Assert.InRange(p010[0] >> 6, y * 4 - 2, y * 4 + 2);
Assert.Equal(0, p010[0] & 0x3F);
Assert.InRange(p010[32] >> 6, u * 4 - 2, u * 4 + 2);
}
[Test]
public void TenBitWhiteAndBlackAreExact()
{
using var white = Flat(4, 2, 1, 1, 1);
var p010 = new ushort[Nv12Converter.FrameLength(4, 2)];
Nv12Converter.ToP010(white, p010);
Assert.Equal((ushort)(940 << 6), p010[0]);
Assert.Equal((ushort)(512 << 6), p010[8]);
using var black = Flat(4, 2, 0, 0, 0);
Nv12Converter.ToP010(black, p010);
Assert.Equal((ushort)(64 << 6), p010[3]);
}
[Theory]
[Case(70, 34)]
[Case(512, 256)]
public void LumaMatchesScalarReferenceIncludingRemainders(int width, int height)
{
using var image = Gradient(width, height);
var nv12 = new byte[Nv12Converter.FrameLength(width, height)];
var p010 = new ushort[Nv12Converter.FrameLength(width, height)];
Nv12Converter.ToNv12(image, nv12);
Nv12Converter.ToP010(image, p010);
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
var luma = 0.2126f * ColorSpace.EncodeFast(image[0, x, y]) + 0.7152f * ColorSpace.EncodeFast(image[1, x, y]) + 0.0722f * ColorSpace.EncodeFast(image[2, x, y]);
Assert.InRange(nv12[y * width + x], luma * 219 + 16 - 1, luma * 219 + 17);
Assert.InRange(p010[y * width + x] >> 6, luma * 876 + 64 - 1, luma * 876 + 65);
}
}
}
[Theory]
[Case(ChromaSiting.Left)]
[Case(ChromaSiting.Center)]
public void RoundTripStaysWithinQuantisationError(ChromaSiting siting)
{
const int w = 256, h = 128;
using var image = Gradient(w, h);
using var back = new ImageBuffer(w, h);
var nv12 = new byte[Nv12Converter.FrameLength(w, h)];
Nv12Converter.ToNv12(image, nv12, siting);
Nv12Converter.FromNv12(nv12, back, siting);
var maxNv12 = MaxEncodedError(image, back);
var p010 = new ushort[Nv12Converter.FrameLength(w, h)];
Nv12Converter.ToP010(image, p010, siting);
Nv12Converter.FromP010(p010, back, siting);
var maxP010 = MaxEncodedError(image, back);
Assert.LessOrEqual(maxNv12, 3.5 / 255);
Assert.LessOrEqual(maxP010, 2.0 / 255);
Assert.Less(maxP010, maxNv12);
}
[Test]
public void GreyRampRoundTripOnlyCarriesLumaQuantisation()
{
const int w = 256, h = 4;
using var image = new ImageBuffer(w, h, 1);
for (var y = 0; y < h; y++)
{
for (var x = 0; x < w; x++) image[0, x, y] = ColorSpace.Linear8((byte)x);
}
using var back = new ImageBuffer(w, h, 1);
var nv12 = new byte[Nv12Converter.FrameLength(w, h)];
Nv12Converter.ToNv12(image, nv12);
Assert.True(nv12.AsSpan(w * h).ToArray().All(c => c == 128), "grey has neutral chroma");
Nv12Converter.FromNv12(nv12, back);
double max = 0;
for (var i = 0; i < w * h; i++) max = Math.Max(max, Math.Abs(ColorSpace.LinearToSrgb(image.Plane(0)[i]) - ColorSpace.LinearToSrgb(back.Plane(0)[i])));
// One limited-range step is 255/219 full-range levels; rounding keeps half of it.
Assert.LessOrEqual(max, 0.6 / 219);
}
[Test]
public void SitingChangesTheChromaFilterFootprint()
{
// Columns: red, red | blue, blue. Centre siting keeps the second chroma pure blue,
// left siting (co-sited with column 2, taps 1:2:1) mixes in a quarter of red.
using var image = new ImageBuffer(4, 2);
for (var y = 0; y < 2; y++)
{
image[0, 0, y] = image[0, 1, y] = 1;
image[2, 2, y] = image[2, 3, y] = 1;
}
var centre = new byte[Nv12Converter.FrameLength(4, 2)];
var left = new byte[Nv12Converter.FrameLength(4, 2)];
Nv12Converter.ToNv12(image, centre, ChromaSiting.Center);
Nv12Converter.ToNv12(image, left, ChromaSiting.Left);
Assert.Equal((byte)240, centre[8 + 2]); // Cb of pure blue
Assert.Equal((byte)118, centre[8 + 3]); // Cr of pure blue
Assert.Equal((byte)240, left[8 + 1]); // Cr of pure red at column 0
// R'G'B' = (0.25, 0, 0.75): Cb = (0.75 − 0.1073) / 1.8556 → 206, Cr = (0.25 − 0.1073) / 1.5748 → 148.
Assert.Equal((byte)206, left[8 + 2]);
Assert.Equal((byte)148, left[8 + 3]);
}
[Test]
public void RespectsStrideAndRejectsOddSizes()
{
using var image = Flat(6, 2, 1, 1, 1);
var nv12 = new byte[Nv12Converter.FrameLength(16, 2)];
nv12.AsSpan().Fill(7);
Nv12Converter.ToNv12(image, nv12, stride: 16);
Assert.Equal((byte)235, nv12[5]);
Assert.Equal((byte)7, nv12[6]); // padding untouched
Assert.Equal((byte)128, nv12[32]);
using var odd = new ImageBuffer(5, 2);
Assert.Throws<ArgumentException>(() => Nv12Converter.ToNv12(odd, new byte[64]));
}
private static double MaxEncodedError(ImageBuffer a, ImageBuffer b)
{
double max = 0;
for (var c = 0; c < 3; c++)
{
var pa = a.Plane(c);
var pb = b.Plane(c);
for (var i = 0; i < pa.Length; i++) max = Math.Max(max, Math.Abs(ColorSpace.LinearToSrgb(pa[i]) - ColorSpace.LinearToSrgb(pb[i])));
}
return max;
}
private static ImageBuffer Flat(int w, int h, float r, float g, float b)
{
var image = new ImageBuffer(w, h);
image.Plane(0).Fill(r);
image.Plane(1).Fill(g);
image.Plane(2).Fill(b);
return image;
}
/// <summary>Smooth colour ramps in encoded space (linearised), so 4:2:0 loss stays small.</summary>
private static ImageBuffer Gradient(int w, int h)
{
var image = new ImageBuffer(w, h);
for (var y = 0; y < h; y++)
{
for (var x = 0; x < w; x++)
{
image[0, x, y] = (float)ColorSpace.SrgbToLinear(0.1 + 0.8 * x / w);
image[1, x, y] = (float)ColorSpace.SrgbToLinear(0.2 + 0.6 * y / h);
image[2, x, y] = (float)ColorSpace.SrgbToLinear(0.5 + 0.2 * Math.Sin((x + y) * 0.02));
}
}
return image;
}
}