From 97bc346539dd028d42ac0d01c773a52932cfdaf4 Mon Sep 17 00:00:00 2001 From: Timothy Date: Sat, 25 Jul 2026 14:09:51 +0200 Subject: [PATCH 1/2] fix(503): map WatermarkLocation.MiddleCenter to a centered overlay position OverlayWatermarkFilter.Position had no switch arm for MiddleCenter, so it silently fell into the BottomRight default and rendered bottom-right. OverlayWatermarkCudaFilter and OverlayWatermarkQsvFilter both inherit this Position property without overriding it, so they were affected too (the whole ErsatzTV.FFmpeg project has only this one WatermarkLocation switch). - Add an explicit MiddleCenter arm: x=(W-w)/2:y=(H-h)/2 - Make BottomRight an explicit arm instead of the fallthrough default - The default case now logs a warning (not throw - this runs on the playback hot path constructing ffmpeg args, and sibling filters in this project already use safe string fallbacks rather than throwing for an unmapped enum) and falls back to the BottomRight position - Strip the pre-existing UTF-8 BOM from OverlayWatermarkFilter.cs (#311 formatting gate: touching a legacy BOM'd file makes removing it ours) - Add ErsatzTV.FFmpeg.Tests/Filter/OverlayWatermarkFilterTests.cs asserting the exact position expression for every WatermarkLocation value across the software, CUDA, and QSV overlay filters, plus the unmapped-value fallback Co-Authored-By: Claude Sonnet 5 --- .../Filter/OverlayWatermarkFilterTests.cs | 99 +++++++++++++++++++ .../Filter/OverlayWatermarkFilter.cs | 39 +++++--- 2 files changed, 127 insertions(+), 11 deletions(-) create mode 100644 ErsatzTV.FFmpeg.Tests/Filter/OverlayWatermarkFilterTests.cs diff --git a/ErsatzTV.FFmpeg.Tests/Filter/OverlayWatermarkFilterTests.cs b/ErsatzTV.FFmpeg.Tests/Filter/OverlayWatermarkFilterTests.cs new file mode 100644 index 000000000..0d37c62ed --- /dev/null +++ b/ErsatzTV.FFmpeg.Tests/Filter/OverlayWatermarkFilterTests.cs @@ -0,0 +1,99 @@ +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); + + private static WatermarkState StateFor(WatermarkLocation location) => + new( + Option>.None, + location, + WatermarkSize.ActualSize, + 10, + 10, + 10, + 100, + false); + + 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}"); + } + + [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}"; + } } } -- 2.47.3 From 39873811da1f9fdc6adcb0ea48225045ef12788e Mon Sep 17 00:00:00 2001 From: Timothy Date: Sat, 25 Jul 2026 14:20:33 +0200 Subject: [PATCH 2/2] test(503): guard WatermarkLocation exhaustiveness and cover source-content margins Review follow-up. The 9-element ExpectedPositions table meant a future enum member would pass every case and silently render bottom-right again -- the exact bug #503 fixes. Assert the table covers Enum.GetValues instead, and exercise the previously untested SourceContentMargins() branch. --- .../Filter/OverlayWatermarkFilterTests.cs | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/ErsatzTV.FFmpeg.Tests/Filter/OverlayWatermarkFilterTests.cs b/ErsatzTV.FFmpeg.Tests/Filter/OverlayWatermarkFilterTests.cs index 0d37c62ed..f878162aa 100644 --- a/ErsatzTV.FFmpeg.Tests/Filter/OverlayWatermarkFilterTests.cs +++ b/ErsatzTV.FFmpeg.Tests/Filter/OverlayWatermarkFilterTests.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using ErsatzTV.FFmpeg; @@ -19,7 +20,11 @@ public class OverlayWatermarkFilterTests private static readonly FrameSize Resolution = new(1920, 1080); private static readonly FrameSize SquarePixelFrameSize = new(1920, 1080); - private static WatermarkState StateFor(WatermarkLocation location) => + // 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, @@ -28,7 +33,7 @@ public class OverlayWatermarkFilterTests 10, 10, 100, - false); + placeWithinSourceContent); private static readonly (WatermarkLocation Location, string Expected)[] ExpectedPositions = [ @@ -84,6 +89,33 @@ public class OverlayWatermarkFilterTests 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() { -- 2.47.3