diff --git a/ErsatzTV.FFmpeg.Tests/Filter/OverlayWatermarkFilterTests.cs b/ErsatzTV.FFmpeg.Tests/Filter/OverlayWatermarkFilterTests.cs new file mode 100644 index 000000000..f878162aa --- /dev/null +++ b/ErsatzTV.FFmpeg.Tests/Filter/OverlayWatermarkFilterTests.cs @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using ErsatzTV.FFmpeg; +using ErsatzTV.FFmpeg.Filter; +using ErsatzTV.FFmpeg.Filter.Cuda; +using ErsatzTV.FFmpeg.Filter.Qsv; +using ErsatzTV.FFmpeg.Format; +using ErsatzTV.FFmpeg.State; +using LanguageExt; +using Microsoft.Extensions.Logging.Abstractions; +using NUnit.Framework; +using Shouldly; + +namespace ErsatzTV.FFmpeg.Tests.Filter; + +[TestFixture] +public class OverlayWatermarkFilterTests +{ + private static readonly FrameSize Resolution = new(1920, 1080); + private static readonly FrameSize SquarePixelFrameSize = new(1920, 1080); + + // a pillarboxed source: 1440x1080 of real content inside a 1920x1080 frame, so + // SourceContentMargins() adds half the 480px horizontal padding to the margin + private static readonly FrameSize PillarboxedSquarePixelFrameSize = new(1440, 1080); + + private static WatermarkState StateFor(WatermarkLocation location, bool placeWithinSourceContent = false) => + new( + Option>.None, + location, + WatermarkSize.ActualSize, + 10, + 10, + 10, + 100, + placeWithinSourceContent); + + private static readonly (WatermarkLocation Location, string Expected)[] ExpectedPositions = + [ + (WatermarkLocation.BottomRight, "x=W-w-192:y=H-h-108"), + (WatermarkLocation.BottomLeft, "x=192:y=H-h-108"), + (WatermarkLocation.TopRight, "x=W-w-192:y=108"), + (WatermarkLocation.TopLeft, "x=192:y=108"), + (WatermarkLocation.TopMiddle, "x=(W-w)/2:y=108"), + (WatermarkLocation.RightMiddle, "x=W-w-192:y=(H-h)/2"), + (WatermarkLocation.BottomMiddle, "x=(W-w)/2:y=H-h-108"), + (WatermarkLocation.LeftMiddle, "x=192:y=(H-h)/2"), + (WatermarkLocation.MiddleCenter, "x=(W-w)/2:y=(H-h)/2") + ]; + + private static IEnumerable AllLocations() => + ExpectedPositions.Select(x => new TestCaseData(x.Location, x.Expected).SetName( + $"{{m}}({x.Location})")); + + [TestCaseSource(nameof(AllLocations))] + public void Software_Overlay_Maps_Every_Location_To_Exact_Position(WatermarkLocation location, string expectedPosition) + { + var filter = new OverlayWatermarkFilter( + StateFor(location), + Resolution, + SquarePixelFrameSize, + new PixelFormatUnknown(), + NullLogger.Instance); + + filter.Filter.ShouldBe($"overlay={expectedPosition}:format=0"); + } + + [TestCaseSource(nameof(AllLocations))] + public void Cuda_Overlay_Maps_Every_Location_To_Exact_Position(WatermarkLocation location, string expectedPosition) + { + var filter = new OverlayWatermarkCudaFilter( + StateFor(location), + Resolution, + SquarePixelFrameSize, + NullLogger.Instance); + + filter.Filter.ShouldBe($"overlay_cuda={expectedPosition}"); + } + + [TestCaseSource(nameof(AllLocations))] + public void Qsv_Overlay_Maps_Every_Location_To_Exact_Position(WatermarkLocation location, string expectedPosition) + { + var filter = new OverlayWatermarkQsvFilter( + StateFor(location), + Resolution, + SquarePixelFrameSize, + NullLogger.Instance); + + filter.Filter.ShouldBe($"overlay_qsv={expectedPosition}"); + } + + // #503: the position switch has a catch-all default that silently renders bottom-right, so a + // newly added WatermarkLocation would compile, pass every case above, and reintroduce the exact + // bug this fixture exists to prevent. Fail here instead, at the point the enum grows. + [Test] + public void Every_WatermarkLocation_Has_An_Expected_Position() + { + IEnumerable covered = ExpectedPositions.Select(x => x.Location); + + Enum.GetValues().Except(covered).ShouldBeEmpty( + "every WatermarkLocation needs an explicit arm in OverlayWatermarkFilter.Position and a " + + "matching entry in ExpectedPositions; otherwise it silently renders bottom-right"); + } + + [Test] + public void Source_Content_Margins_Offset_By_Half_The_Pillarbox_Padding() + { + var filter = new OverlayWatermarkFilter( + StateFor(WatermarkLocation.BottomRight, placeWithinSourceContent: true), + Resolution, + PillarboxedSquarePixelFrameSize, + new PixelFormatUnknown(), + NullLogger.Instance); + + // horizontal: round(0.10 * 1440 + 480 / 2) = 384; vertical: round(0.10 * 1080 + 0) = 108 + filter.Filter.ShouldBe("overlay=x=W-w-384:y=H-h-108:format=0"); + } + + [Test] + public void Unmapped_Location_Falls_Back_To_BottomRight_Position() + { + var filter = new OverlayWatermarkFilter( + StateFor((WatermarkLocation)999), + Resolution, + SquarePixelFrameSize, + new PixelFormatUnknown(), + NullLogger.Instance); + + filter.Filter.ShouldBe("overlay=x=W-w-192:y=H-h-108:format=0"); + } +} diff --git a/ErsatzTV.FFmpeg/Filter/OverlayWatermarkFilter.cs b/ErsatzTV.FFmpeg/Filter/OverlayWatermarkFilter.cs index fbfb03ee4..7bcb4b071 100644 --- a/ErsatzTV.FFmpeg/Filter/OverlayWatermarkFilter.cs +++ b/ErsatzTV.FFmpeg/Filter/OverlayWatermarkFilter.cs @@ -1,4 +1,4 @@ -using ErsatzTV.FFmpeg.Format; +using ErsatzTV.FFmpeg.Format; using ErsatzTV.FFmpeg.State; using Microsoft.Extensions.Logging; @@ -36,18 +36,35 @@ public class OverlayWatermarkFilter : BaseFilter ? SourceContentMargins() : NormalMargins(); - return _watermarkState.Location switch + switch (_watermarkState.Location) { // TODO: can these be pre-calculated (and used with accelerated overlay filters) - WatermarkLocation.BottomLeft => $"x={horizontalMargin}:y=H-h-{verticalMargin}", - WatermarkLocation.TopLeft => $"x={horizontalMargin}:y={verticalMargin}", - WatermarkLocation.TopRight => $"x=W-w-{horizontalMargin}:y={verticalMargin}", - WatermarkLocation.TopMiddle => $"x=(W-w)/2:y={verticalMargin}", - WatermarkLocation.RightMiddle => $"x=W-w-{horizontalMargin}:y=(H-h)/2", - WatermarkLocation.BottomMiddle => $"x=(W-w)/2:y=H-h-{verticalMargin}", - WatermarkLocation.LeftMiddle => $"x={horizontalMargin}:y=(H-h)/2", - _ => $"x=W-w-{horizontalMargin}:y=H-h-{verticalMargin}" - }; + case WatermarkLocation.BottomLeft: + return $"x={horizontalMargin}:y=H-h-{verticalMargin}"; + case WatermarkLocation.TopLeft: + return $"x={horizontalMargin}:y={verticalMargin}"; + case WatermarkLocation.TopRight: + return $"x=W-w-{horizontalMargin}:y={verticalMargin}"; + case WatermarkLocation.TopMiddle: + return $"x=(W-w)/2:y={verticalMargin}"; + case WatermarkLocation.RightMiddle: + return $"x=W-w-{horizontalMargin}:y=(H-h)/2"; + case WatermarkLocation.BottomMiddle: + return $"x=(W-w)/2:y=H-h-{verticalMargin}"; + case WatermarkLocation.LeftMiddle: + return $"x={horizontalMargin}:y=(H-h)/2"; + case WatermarkLocation.MiddleCenter: + return $"x=(W-w)/2:y=(H-h)/2"; + case WatermarkLocation.BottomRight: + return $"x=W-w-{horizontalMargin}:y=H-h-{verticalMargin}"; + default: + // unmapped enum value; log loudly and fall back to the BottomRight position + // rather than throwing, since this runs on the playback hot path + _logger.LogWarning( + "Unrecognized watermark location {Location}; falling back to bottom-right overlay position", + _watermarkState.Location); + return $"x=W-w-{horizontalMargin}:y=H-h-{verticalMargin}"; + } } }