Files
Chronos/tests/Chronos.Core.Tests/Video/Mp4MuxerTests.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

257 lines
11 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 System.Buffers.Binary;
using Chronos.Core.Video;
using Chronos.Core.Video.Bitstream;
using Chronos.Core.Video.Mp4;
using Chronos.Testing;
namespace Chronos.Core.Tests.Video;
public sealed class Mp4MuxerTests(TestContext context)
{
private static long Duration(int i) => 3000 + i % 3 * 1500;
[Test]
public void H264WithVariableDurationsInspectsClean()
{
using var ms = new MemoryStream();
var muxer = new Mp4Muxer(ms, new VideoTrackConfig { Codec = VideoCodec.H264, Width = 1920, Height = 1080 });
var expected = new List<byte[]>();
long total = 0;
for (var i = 0; i < 30; i++)
{
var idr = i % 10 == 0;
var slice = SyntheticStreams.H264Slice(idr, 200 + i * 7, i);
List<byte[]> nals = [SyntheticStreams.H264AccessUnitDelimiter()];
if (idr) nals.AddRange([SyntheticStreams.H264Sps(), SyntheticStreams.H264Pps()]);
nals.Add(slice);
var annexB = AnnexBParser.ToAnnexB(nals);
// Alternate input formats: the muxer accepts both.
muxer.WriteSample(i % 2 == 0 ? annexB : AnnexBParser.ToLengthPrefixed(annexB), Duration(i), idr);
expected.Add(AnnexBParser.ToLengthPrefixed(AnnexBParser.ToAnnexB([slice])));
total += Duration(i);
}
muxer.Complete();
var report = Mp4Inspector.Inspect(ms);
AssertClean(report);
Assert.Equal("avc1", report.SampleEntry);
Assert.Equal("avc1.640029", report.CodecString);
Assert.Equal(1920, report.Width);
Assert.Equal(1080, report.Height);
Assert.Equal(30, report.FrameCount);
Assert.Equal(3, report.SyncSampleCount);
Assert.Equal(90000u, report.TimeScale);
Assert.Equal(total, report.DurationTicks);
Assert.False(report.FastStart);
Assert.False(report.HasEditList);
for (var i = 0; i < 30; i++)
{
Assert.Equal(Duration(i), (long)report.Samples[i].Duration);
Assert.BytesEqual(expected[i], ReadSample(ms, report.Samples[i]));
}
}
[Test]
public void HevcMain10InspectsClean()
{
using var ms = new MemoryStream();
var muxer = new Mp4Muxer(ms, new VideoTrackConfig { Codec = VideoCodec.Hevc, Width = 3840, Height = 2160 });
for (var i = 0; i < 12; i++)
{
var idr = i % 6 == 0;
List<byte[]> nals = idr ? [SyntheticStreams.HevcVps(), SyntheticStreams.HevcSps(3840, 2160), SyntheticStreams.HevcPps()] : [];
nals.Add(SyntheticStreams.HevcSlice(idr, 500, i));
muxer.WriteSample(AnnexBParser.ToAnnexB(nals), 1500, idr);
}
muxer.Complete();
var report = Mp4Inspector.Inspect(ms);
AssertClean(report);
Assert.Equal("hvc1", report.SampleEntry);
Assert.Equal(VideoCodec.Hevc, report.Codec);
Assert.Equal("hvc1.2.4.L153.90", report.CodecString);
Assert.Equal(3840, report.Width);
Assert.Equal(2160, report.Height);
Assert.Equal(10, report.BitDepth);
Assert.Equal(12, report.FrameCount);
Assert.Equal(18000L, report.DurationTicks);
Assert.Approximately(0.2, report.Duration.TotalSeconds, 1e-9);
}
[Test]
public void Av1InspectsCleanAndGetsSizeFields()
{
using var ms = new MemoryStream();
var muxer = new Mp4Muxer(ms, new VideoTrackConfig { Codec = VideoCodec.Av1, Width = 1280, Height = 720 });
for (var i = 0; i < 10; i++)
{
var key = i == 0;
var frame = SyntheticStreams.Random(300, i);
// Last OBU of the temporal unit without obu_size, as encoders may emit it.
byte[] lastWithoutSize = [(byte)Av1ObuType.Frame << 3, .. frame];
var tu = key
? SyntheticStreams.Concat(SyntheticStreams.Av1TemporalDelimiter(), SyntheticStreams.Av1SequenceHeaderObu(1280, 720, 5), lastWithoutSize)
: SyntheticStreams.Concat(SyntheticStreams.Av1TemporalDelimiter(), lastWithoutSize);
muxer.WriteSample(tu, 3003, key);
}
muxer.Complete();
var report = Mp4Inspector.Inspect(ms);
AssertClean(report);
Assert.Equal("av01", report.SampleEntry);
Assert.Equal("av01.0.05M.08", report.CodecString);
Assert.Equal(1280, report.Width);
Assert.Equal(720, report.Height);
Assert.Equal(10, report.FrameCount);
Assert.Equal(1, report.SyncSampleCount);
var second = ReadSample(ms, report.Samples[1]);
Assert.True(Av1ObuParser.TryReadObu(second, 0, out var obu));
Assert.True(obu.HasSizeField);
Assert.Equal(Av1ObuType.Frame, obu.Type);
Assert.Equal(second.Length, obu.TotalLength);
}
[Test]
public void FastStartMovesMoovAndKeepsOffsetsValid()
{
var path = Path.Combine(context.CreateTempDirectory(), "fast.mp4");
var expected = new List<byte[]>();
using (var fs = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite))
{
var muxer = new Mp4Muxer(fs, new VideoTrackConfig { Codec = VideoCodec.H264, Width = 1280, Height = 720, FastStart = true });
for (var i = 0; i < 90; i++)
{
var idr = i % 30 == 0;
var slice = SyntheticStreams.H264Slice(idr, 4000 + i, i);
List<byte[]> nals = idr ? [SyntheticStreams.H264Sps(1280, 720, 77, 31), SyntheticStreams.H264Pps(), slice] : [slice];
muxer.WriteSample(AnnexBParser.ToAnnexB(nals), 3000, idr);
expected.Add(AnnexBParser.ToLengthPrefixed(AnnexBParser.ToAnnexB([slice])));
}
muxer.Complete();
}
var report = Mp4Inspector.Inspect(path);
AssertClean(report);
Assert.True(report.FastStart);
Assert.SequenceEqual(["ftyp", "moov", "free", "mdat"], report.Boxes.Select(b => b.Type));
Assert.Equal(new FileInfo(path).Length, report.Boxes.Sum(b => b.Size));
using var read = File.OpenRead(path);
for (var i = 0; i < expected.Count; i++) Assert.BytesEqual(expected[i], ReadSample(read, report.Samples[i]));
}
[Test]
public void CompositionOffsetsProduceCttsAndEditList()
{
// Decode order I0 P3 B1 B2 P6 B4 B5 with unit durations of 3000 ticks; encoder gives PTS − DTS.
int[] pts = [0, 3, 1, 2, 6, 4, 5];
using var ms = new MemoryStream();
var muxer = new Mp4Muxer(ms, new VideoTrackConfig { Codec = VideoCodec.H264, Width = 1920, Height = 1080 });
for (var i = 0; i < pts.Length; i++)
{
List<byte[]> nals = i == 0 ? [SyntheticStreams.H264Sps(), SyntheticStreams.H264Pps()] : [];
nals.Add(SyntheticStreams.H264Slice(i == 0, 100, i));
muxer.WriteSample(AnnexBParser.ToAnnexB(nals), 3000, i == 0, (pts[i] - i) * 3000L);
}
muxer.Complete();
var report = Mp4Inspector.Inspect(ms);
AssertClean(report);
Assert.True(report.HasCompositionOffsets);
Assert.True(report.HasEditList);
Assert.Equal(7 * 3000L, report.DurationTicks);
// After the shift every composition time is non-negative and I0 presents first.
var cts = report.Samples.Select(s => s.DecodeTime + s.CompositionOffset).ToArray();
Assert.Equal(cts.Min(), cts[0]);
Assert.True(report.Samples.All(s => s.CompositionOffset >= 0));
}
[Test]
public void LargeFilesUseCo64AndLargeMdat()
{
const int sampleSize = 1 << 20;
const int samples = 4200; // ≈ 4.4 GB of payload, dropped by the sparse stream
var slice = new byte[sampleSize];
BinaryPrimitives.WriteInt32BigEndian(slice, sampleSize - 4);
slice[4] = 0x41; // non-IDR slice header
var first = SyntheticStreams.Concat(
AnnexBParser.ToLengthPrefixed(AnnexBParser.ToAnnexB([SyntheticStreams.H264Sps(), SyntheticStreams.H264Pps()])), slice);
first[first.Length - sampleSize + 4] = 0x65;
using var sparse = new SparseStream();
var muxer = new Mp4Muxer(sparse, new VideoTrackConfig { Codec = VideoCodec.H264, Width = 1920, Height = 1080 });
muxer.WriteSample(first, 3000, true);
for (var i = 1; i < samples; i++) muxer.WriteSample(slice, 3000, i % 60 == 0);
muxer.Complete();
var report = Mp4Inspector.Inspect(sparse);
AssertClean(report);
Assert.True(report.UsesCo64);
Assert.True(report.UsesLargeMdat);
Assert.Equal(samples, report.FrameCount);
Assert.Greater(report.Samples[^1].Offset, uint.MaxValue);
Assert.Equal((long)sampleSize * samples, report.MediaBytes);
}
[Test]
public void CompleteWithoutParameterSetsThrows()
{
using var ms = new MemoryStream();
var muxer = new Mp4Muxer(ms, new VideoTrackConfig { Codec = VideoCodec.H264, Width = 64, Height = 64 });
muxer.WriteSample(AnnexBParser.ToAnnexB([SyntheticStreams.H264Slice(true, 10, 1)]), 3000, true);
Assert.Throws<InvalidOperationException>(muxer.Complete);
Assert.Throws<ArgumentOutOfRangeException>(() => muxer.WriteSample([0, 0, 1, 0x65, 1], 0, true));
}
[Test]
public void ParameterSetOnlyUnitsAddNoSampleAndSeiOnlyUnitsAreRejected()
{
using var ms = new MemoryStream();
var muxer = new Mp4Muxer(ms, new VideoTrackConfig { Codec = VideoCodec.H264, Width = 1920, Height = 1080 });
muxer.WriteSample(AnnexBParser.ToAnnexB([SyntheticStreams.H264Sps(), SyntheticStreams.H264Pps()]), 3000, true);
Assert.Equal(0, muxer.SampleCount);
Assert.Throws<ArgumentException>(() => muxer.WriteSample(AnnexBParser.ToAnnexB([[0x06, 0x05, 0x01, 0x00, 0x80]]), 3000, false));
muxer.WriteSample(AnnexBParser.ToAnnexB([SyntheticStreams.H264Slice(true, 64, 1)]), 3000, true);
muxer.Complete();
var report = Mp4Inspector.Inspect(ms);
AssertClean(report);
Assert.Equal(1, report.FrameCount);
Assert.Equal(0, report.Warnings.Count);
}
[Test]
public void InspectorSurvivesTruncationAndCorruption()
{
using var ms = new MemoryStream();
var muxer = new Mp4Muxer(ms, new VideoTrackConfig { Codec = VideoCodec.H264, Width = 1920, Height = 1080 });
for (var i = 0; i < 5; i++)
{
List<byte[]> nals = i == 0 ? [SyntheticStreams.H264Sps(), SyntheticStreams.H264Pps()] : [];
nals.Add(SyntheticStreams.H264Slice(i == 0, 50, i));
muxer.WriteSample(AnnexBParser.ToAnnexB(nals), 3000, i == 0);
}
muxer.Complete();
var valid = ms.ToArray();
for (var cut = 0; cut < valid.Length; cut += 7)
{
var report = Mp4Inspector.Inspect(new MemoryStream(valid, 0, cut));
Assert.False(report.IsValid, $"truncated at {cut} should not validate");
}
var rng = new Random(11);
for (var trial = 0; trial < 500; trial++)
{
var corrupt = (byte[])valid.Clone();
for (var k = 0; k < 3; k++) corrupt[rng.Next(corrupt.Length)] = (byte)rng.Next(256);
Mp4Inspector.Inspect(new MemoryStream(corrupt)).ToString();
}
}
private static void AssertClean(Mp4Report report)
{
if (!report.IsValid) Assert.Fail("Inspector errors: " + string.Join("; ", report.Errors));
}
private static byte[] ReadSample(Stream stream, Mp4SampleInfo sample)
{
var data = new byte[sample.Size];
stream.Position = sample.Offset;
stream.ReadExactly(data);
return data;
}
}