using Chronos.Core.Imaging; using Chronos.Core.Project; using Chronos.Core.Render; using Chronos.Core.Video; using Chronos.Testing; namespace Chronos.Core.Tests.Render; /// The output program of the playback settings (owner review 1, R8): order, loops, hold and fades. public sealed class OutputProgramPlaybackTests { private static readonly Rational Fps25 = new(25, 1); [Theory] [Case("forward", 1, new[] { 0, 1, 2, 3 })] [Case("forward", 2, new[] { 0, 1, 2, 3, 0, 1, 2, 3 })] [Case("forward", 3, new[] { 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 })] [Case("reverse", 1, new[] { 3, 2, 1, 0 })] [Case("reverse", 2, new[] { 3, 2, 1, 0, 3, 2, 1, 0 })] [Case("reverse", 3, new[] { 3, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 0 })] [Case("boomerang", 1, new[] { 0, 1, 2, 3, 2, 1, 0 })] [Case("boomerang", 2, new[] { 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, 0 })] [Case("boomerang", 3, new[] { 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, 0 })] public void DirectionsAndLoopsGiveThePlanIndicesInOrder(string direction, int loops, int[] expected) { var frames = Frames(3600, 3600, 3600, 7200); var program = OutputProgram.Build(frames, new PlaybackSettings { Direction = direction, Loops = loops }, Fps25); Assert.SequenceEqual(expected, program.Entries.Select(e => e.PlanIndex)); // No frame is emitted twice in a row: neither the turn of a boomerang nor the joint of two passes. for (var i = 1; i < program.Count; i++) Assert.NotEqual(program.Entries[i - 1].PlanIndex, program.Entries[i].PlanIndex); Assert.True(program.Entries.All(e => e.Gain == 1 && e.Duration90k == frames[e.PlanIndex].Duration90k)); Assert.Equal(expected.Sum(i => frames[i].Duration90k), program.Duration90k); Assert.Equal(0, program.Warnings.Count); } [Test] public void ASingleFrameBoomerangPlaysOncePerLoop() { var program = OutputProgram.Build(Frames(3600), new PlaybackSettings { Direction = PlaybackDirection.Boomerang, Loops = 3 }, Fps25); Assert.SequenceEqual([0, 0, 0], program.Entries.Select(e => e.PlanIndex)); } [Test] public void ValuesOutOfRangeAreClamped() { var program = OutputProgram.Build(Frames(3600, 3600), new PlaybackSettings { Loops = 50 }, Fps25); Assert.Equal(2 * PlaybackSettings.MaxLoops, program.Count); var negative = OutputProgram.Build(Frames(3600, 3600), new PlaybackSettings { Loops = -3, FadeIn = -1, HoldEnd = double.NaN }, Fps25); Assert.Equal((2, 7200L), (negative.Count, negative.Duration90k)); } [Test] public void TheHoldExtendsTheLastFrameByWholePeriods() { var program = OutputProgram.Build(Frames(3600, 3600, 3600), new PlaybackSettings { Direction = PlaybackDirection.Reverse, HoldEnd = 2 }, Fps25); Assert.SequenceEqual([2, 1, 0], program.Entries.Select(e => e.PlanIndex)); Assert.SequenceEqual([3600L, 3600, 3600 + 180000], program.Entries.Select(e => e.Duration90k)); Assert.Approximately(2.12, program.DurationSeconds, 1e-12); // A hold shorter than a period still shows one more period. var brief = OutputProgram.Build(Frames(3600), new PlaybackSettings { HoldEnd = 0.001 }, Fps25); Assert.Equal(7200L, brief.Duration90k); } [Test] public void AHeldFrameInsideAFadeIsSplitPerPeriod() { // The first frame is held for ten periods (0.4 s) and the fade in lasts 0.2 s (five periods). var program = OutputProgram.Build(Frames(36000, 3600, 3600), new PlaybackSettings { FadeIn = 0.2 }, Fps25); Assert.Equal(12, program.Count); Assert.True(program.Entries.Take(10).All(e => e.PlanIndex == 0 && e.Duration90k == 3600)); Assert.SequenceEqual([1, 2], program.Entries.Skip(10).Select(e => e.PlanIndex)); for (var k = 0; k < 10; k++) Assert.Approximately(ColorSpace.SrgbToLinear(double.Min(1, k / 5.0)), program.Entries[k].Gain, 1e-12, $"period {k}"); Assert.Equal(0.0, program.Entries[0].Gain); Assert.Equal(43200L, program.Duration90k); // The fade out reaches into the hold at the end: the hold is split as well, the last period black. var fadeOut = OutputProgram.Build(Frames(3600, 3600), new PlaybackSettings { FadeOut = 0.2, HoldEnd = 0.4 }, Fps25); Assert.Equal(1 + 11, fadeOut.Count); Assert.Equal(1.0, fadeOut.Entries[0].Gain); Assert.Equal(0.0, fadeOut.Entries[^1].Gain); Assert.Monotonic(fadeOut.Entries.Select(e => e.Gain), decreasing: true); Assert.Equal(7200L + 36000, fadeOut.Duration90k); } [Test] public void FadesLongerThanHalfTheProgramAreShortenedWithAWarning() { // 0.4 s of video with two 1 s fades: together they get half of it, 0.1 s each. var program = OutputProgram.Build(Frames(Enumerable.Repeat(3600L, 10).ToArray()), new PlaybackSettings { FadeIn = 1, FadeOut = 1 }, Fps25); Assert.Count(1, program.Warnings); Assert.Equal(OutputProgram.FadesShortenedWarning, program.Warnings[0].Key); Assert.Equal(10, program.Count); Assert.Equal(0.0, program.Entries[0].Gain); Assert.Equal(0.0, program.Entries[^1].Gain); // 0.1 s is two and a half periods: the ramps take three frames each, the four between play as they are. Assert.Approximately(ColorSpace.SrgbToLinear(0.4), program.Entries[1].Gain, 1e-12); Assert.Approximately(ColorSpace.SrgbToLinear(0.8), program.Entries[2].Gain, 1e-12); Assert.SequenceEqual([1.0, 1, 1, 1], program.Entries.Skip(3).Take(4).Select(e => e.Gain)); Assert.Approximately(ColorSpace.SrgbToLinear(0.8), program.Entries[7].Gain, 1e-12); // Fades that fit leave no warning. Assert.Equal(0, OutputProgram.Build(Frames(Enumerable.Repeat(3600L, 10).ToArray()), new PlaybackSettings { FadeIn = 0.08, FadeOut = 0.12 }, Fps25).Warnings.Count); } [Test] public void TheTotalDurationIsExactOnTheNinetyKilohertzClock() { var rate = Rational.Fps2997; var frames = Frames(Enumerable.Repeat(3003L, 7).ToArray()); var playback = new PlaybackSettings { Direction = PlaybackDirection.Boomerang, Loops = 2, HoldEnd = 0.5 }; var plain = OutputProgram.Build(frames, playback, rate); // (7 + 6) + (6 + 6) frames of 3003 ticks, then 15 periods of hold at 30000/1001 (45045 ticks). Assert.Equal(25, plain.Count); Assert.Equal(25 * 3003L + RenderPlan.TicksAt(rate, 15), plain.Duration90k); Assert.Equal(120120L, plain.Duration90k); var faded = OutputProgram.Build(frames, playback with { FadeIn = 0.5, FadeOut = 0.5 }, rate); Assert.Equal(plain.Duration90k, faded.Duration90k); Assert.Equal(faded.Duration90k, faded.Entries.Sum(e => e.Duration90k)); Assert.Greater(faded.Count, plain.Count); Assert.True(faded.Entries.All(e => e.Duration90k > 0)); } [Test] public void TheProgramOfAPlanUsesItsFrames() { var times = Enumerable.Range(0, 5).Select(i => (DateTime?)new DateTime(2026, 1, 1).AddSeconds(10 * i)).ToArray(); var plan = RenderPlan.Build(times, new ProjectSettings(), Fps25); var program = OutputProgram.Build(plan, new PlaybackSettings { Direction = PlaybackDirection.Reverse }, Fps25); Assert.Equal(plan.Frames.Count, program.Count); Assert.Equal(plan.Duration90k, program.Duration90k); Assert.Equal(plan.Frames.Count - 1, program.Entries[0].PlanIndex); } private static PlannedFrame[] Frames(params long[] durations) { var frames = new PlannedFrame[durations.Length]; long start = 0; for (var i = 0; i < durations.Length; i++) { frames[i] = new PlannedFrame(i, i, i, 0, start, durations[i], i, (int)(durations[i] / 3600), 1, PlannedFrameFlags.None); start += durations[i]; } return frames; } }