Files
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
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 and the system SQLite. 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; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

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>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

206 lines
8.4 KiB
C#

using System.Buffers.Binary;
using System.Text;
using Chronos.Core.Video;
using Chronos.Core.Video.Avi;
using Chronos.Testing;
namespace Chronos.Core.Tests.Video;
public sealed class AviMjpegWriterTests
{
[Test]
public void SingleRiffHasHeadersIdx1AndStandardIndex()
{
var frames = Frames(10, 1);
var bytes = Write(frames, Rational.Fps2997, AviMjpegWriter.DefaultRiffSizeLimit);
var file = Riff.Parse(bytes);
Assert.Equal(bytes.Length, file.Sum(r => r.Size + 8));
Assert.Count(1, file);
var riff = file[0];
Assert.Equal("RIFF", riff.Id);
Assert.Equal("AVI ", riff.Type);
var hdrl = riff.List("hdrl");
var avih = hdrl.Chunk("avih").Data;
Assert.Equal(33367u, U32(avih, 0));
Assert.Equal(10u, U32(avih, 16));
Assert.Equal(1u, U32(avih, 24));
Assert.Equal(64u, U32(avih, 32));
Assert.Equal(48u, U32(avih, 36));
var strl = hdrl.List("strl");
var strh = strl.Chunk("strh").Data;
Assert.Equal("vids", Encoding.ASCII.GetString(strh, 0, 4));
Assert.Equal("MJPG", Encoding.ASCII.GetString(strh, 4, 4));
Assert.Equal(1001u, U32(strh, 20));
Assert.Equal(30000u, U32(strh, 24));
Assert.Equal(10u, U32(strh, 32));
Assert.Equal("MJPG", Encoding.ASCII.GetString(strl.Chunk("strf").Data, 16, 4));
Assert.Equal(10u, U32(hdrl.List("odml").Chunk("dmlh").Data, 0));
var movi = riff.List("movi");
var chunks = movi.Children.Where(c => c.Id == "00dc").ToList();
Assert.Equal(10, chunks.Count);
for (var i = 0; i < 10; i++)
{
Assert.BytesEqual(frames[i], chunks[i].Data);
Assert.Equal(0L, chunks[i].Offset % 2, "chunks stay word aligned after odd frames");
}
var idx1 = riff.Chunk("idx1").Data;
Assert.Equal(160, idx1.Length);
for (var i = 0; i < 10; i++)
{
Assert.Equal("00dc", Encoding.ASCII.GetString(idx1, i * 16, 4));
Assert.Equal(0x10u, U32(idx1, i * 16 + 4));
Assert.Equal(chunks[i].Offset, movi.TypeOffset + U32(idx1, i * 16 + 8));
Assert.Equal((uint)frames[i].Length, U32(idx1, i * 16 + 12));
}
AssertOpenDmlIndex(bytes, file, frames);
}
[Test]
public void LargeOutputsContinueInAvixLists()
{
var frames = Frames(40, 2);
const long limit = 16 * 1024;
var bytes = Write(frames, new Rational(25, 1), limit);
var file = Riff.Parse(bytes);
Assert.Equal(bytes.Length, file.Sum(r => r.Size + 8));
Assert.Greater(file.Count, 3);
Assert.Equal("AVI ", file[0].Type);
Assert.True(file.Skip(1).All(r => r.Type == "AVIX" && r.Children.All(c => c.Id != "idx1")));
Assert.True(file.All(r => r.Size + 8 <= limit), "every RIFF respects the limit");
var first = file[0].List("movi").Children.Count(c => c.Id == "00dc");
Assert.Equal((uint)first, U32(file[0].List("hdrl").Chunk("avih").Data, 16));
Assert.Equal(first * 16, file[0].Chunk("idx1").Data.Length);
Assert.Equal(40u, U32(file[0].List("hdrl").List("strl").Chunk("strh").Data, 32));
Assert.Equal(40u, U32(file[0].List("hdrl").List("odml").Chunk("dmlh").Data, 0));
AssertOpenDmlIndex(bytes, file, frames);
}
[Theory]
[Case(24, 1, 41667u)]
[Case(25, 1, 40000u)]
[Case(60000, 1001, 16683u)]
[Case(48, 2, 41667u)]
public void FrameRateFieldsAreExact(int numerator, int denominator, uint microseconds)
{
var bytes = Write(Frames(2, 3), new Rational(numerator, denominator), AviMjpegWriter.DefaultRiffSizeLimit);
var hdrl = Riff.Parse(bytes)[0].List("hdrl");
Assert.Equal(microseconds, U32(hdrl.Chunk("avih").Data, 0));
var strh = hdrl.List("strl").Chunk("strh").Data;
var g = (uint)System.Numerics.BigInteger.GreatestCommonDivisor(numerator, denominator);
Assert.Equal((uint)denominator / g, U32(strh, 20));
Assert.Equal((uint)numerator / g, U32(strh, 24));
}
[Test]
public void RejectsNonJpegFrames()
{
using var ms = new MemoryStream();
var writer = new AviMjpegWriter(ms, 64, 48, new Rational(30, 1));
Assert.Throws<ArgumentException>(() => writer.WriteFrame([0x89, 0x50, 0x4E, 0x47]));
}
private static void AssertOpenDmlIndex(byte[] bytes, List<Riff.Node> file, List<byte[]> frames)
{
var indx = file[0].List("hdrl").List("strl").Chunk("indx").Data;
Assert.Equal(4, BinaryPrimitives.ReadUInt16LittleEndian(indx));
Assert.Equal((byte)0, indx[3]); // AVI_INDEX_OF_INDEXES
var entries = U32(indx, 4);
Assert.Equal((uint)file.Count, entries);
var frame = 0;
for (var e = 0; e < entries; e++)
{
var offset = BinaryPrimitives.ReadInt64LittleEndian(indx.AsSpan(24 + e * 16));
var size = U32(indx, 32 + e * 16);
var duration = U32(indx, 36 + e * 16);
Assert.Equal("ix00", Encoding.ASCII.GetString(bytes, (int)offset, 4));
Assert.Equal(size, U32(bytes, (int)offset + 4) + 8);
var ix = bytes.AsSpan((int)offset + 8);
Assert.Equal((byte)1, ix[3]); // AVI_INDEX_OF_CHUNKS
Assert.Equal(duration, U32(bytes, (int)offset + 12));
var baseOffset = BinaryPrimitives.ReadInt64LittleEndian(ix[12..]);
for (var i = 0; i < duration; i++, frame++)
{
var dataOffset = baseOffset + U32(bytes, (int)offset + 32 + i * 8);
var length = U32(bytes, (int)offset + 36 + i * 8);
Assert.Equal(0u, length & 0x8000_0000u, "every MJPEG frame is a key frame");
Assert.BytesEqual(frames[frame], bytes.AsSpan((int)dataOffset, (int)length));
}
}
Assert.Equal(frames.Count, frame);
}
private static byte[] Write(List<byte[]> frames, Rational rate, long limit)
{
using var ms = new MemoryStream();
var writer = new AviMjpegWriter(ms, 64, 48, rate, limit);
foreach (var f in frames) writer.WriteFrame(f);
writer.Complete();
Assert.Equal(frames.Count, writer.FrameCount);
return ms.ToArray();
}
/// <summary>JPEG-shaped payloads (SOI … EOI) of odd and even lengths.</summary>
private static List<byte[]> Frames(int count, int seed)
{
var rng = new Random(seed);
var list = new List<byte[]>();
for (var i = 0; i < count; i++)
{
var f = new byte[1500 + rng.Next(2000)];
rng.NextBytes(f);
f[0] = 0xFF;
f[1] = 0xD8;
f[^2] = 0xFF;
f[^1] = 0xD9;
list.Add(f);
}
return list;
}
private static uint U32(byte[] data, int offset) => BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(offset));
/// <summary>Minimal RIFF tree reader for verification.</summary>
private static class Riff
{
public sealed record Node(string Id, string? Type, long Offset, uint Size, byte[] Data, List<Node> Children)
{
/// <summary>File offset of the list type four-cc (the base of idx1 offsets).</summary>
public long TypeOffset => Offset + 8;
public Node List(string type) => Children.Single(c => c.Type == type);
public Node Chunk(string id) => Children.Single(c => c.Id == id);
}
public static List<Node> Parse(byte[] bytes) => ParseRange(bytes, 0, bytes.Length);
private static List<Node> ParseRange(byte[] bytes, long start, long end)
{
var nodes = new List<Node>();
var pos = start;
while (pos + 8 <= end)
{
var id = Encoding.ASCII.GetString(bytes, (int)pos, 4);
var size = U32(bytes, (int)pos + 4);
Assert.True(pos + 8 + size <= end, $"chunk {id} at {pos} overruns its parent");
if (id is "RIFF" or "LIST")
{
var type = Encoding.ASCII.GetString(bytes, (int)pos + 8, 4);
nodes.Add(new Node(id, type, pos, size, [], ParseRange(bytes, pos + 12, pos + 8 + size)));
}
else
{
nodes.Add(new Node(id, null, pos, size, bytes.AsSpan((int)pos + 8, (int)size).ToArray(), []));
}
pos += 8 + size + (size & 1);
}
Assert.Equal(end, pos);
return nodes;
}
}
}