using System.Numerics; using Vortice.DCommon; using Vortice.Direct2D1; using Vortice.Direct2D1.Effects; using Vortice.DXGI; using Vortice.Mathematics; using YukkuriMovieMaker.Commons; using YukkuriMovieMaker.Plugin.FileSource; namespace ProxyForge.Tests; public enum TestCentering { Floor, Exact, } internal sealed class TestVideoSource : IVideoFileSource { const float ReferenceDpi = 96f; readonly ID2D1Bitmap1 bitmap; readonly AffineTransform2D centering; readonly ID2D1Image output; readonly int frameRateNumerator; readonly int frameRateDenominator; readonly int width; readonly int height; readonly Func? framePixelOf; int uploadedFrame = -1; public int UpdateCount { get; private set; } public List RequestedFrames { get; } = []; public bool IsDisposed { get; private set; } public TimeSpan LastUpdateTime { get; private set; } public TimeSpan Duration { get; } public ID2D1Image Output => output; public TestVideoSource( IGraphicsDevicesAndContext devices, int width, int height, int frameRateNumerator, int frameRateDenominator, int frameCount, Func pixelOf, TestCentering centering = TestCentering.Floor, Func? framePixelOf = null) { this.frameRateNumerator = frameRateNumerator; this.frameRateDenominator = frameRateDenominator; this.width = width; this.height = height; this.framePixelOf = framePixelOf; Duration = TimeSpan.FromTicks(frameCount * TimeSpan.TicksPerSecond * frameRateDenominator / frameRateNumerator); var pixelFormat = new PixelFormat(Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied); bitmap = devices.DeviceContext.CreateBitmap( new SizeI(width, height), nint.Zero, width * 4, new BitmapProperties1(pixelFormat, ReferenceDpi, ReferenceDpi, BitmapOptions.None)); Upload(pixelOf); this.centering = new AffineTransform2D(devices.DeviceContext); this.centering.SetInput(0, bitmap, true); this.centering.TransformMatrix = centering == TestCentering.Floor ? Matrix3x2.CreateTranslation(MathF.Floor(-width / 2f), MathF.Floor(-height / 2f)) : Matrix3x2.CreateTranslation(-width / 2f, -height / 2f); output = this.centering.Output; } public static TestVideoSource Solid( IGraphicsDevicesAndContext devices, int width, int height, int frameRateNumerator, int frameRateDenominator, int frameCount, byte blue = 64, byte green = 64, byte red = 64) => new(devices, width, height, frameRateNumerator, frameRateDenominator, frameCount, (_, _) => (blue, green, red, byte.MaxValue)); public void Update(TimeSpan time) { UpdateCount++; LastUpdateTime = time; var frame = time <= TimeSpan.Zero ? 0 : (int)((Int128)time.Ticks * frameRateNumerator / ((Int128)frameRateDenominator * TimeSpan.TicksPerSecond)); RequestedFrames.Add(frame); if (framePixelOf is null || frame == uploadedFrame) return; uploadedFrame = frame; Upload((x, y) => framePixelOf(frame, x, y)); } void Upload(Func pixelOf) { var stride = width * 4; var pixels = new byte[stride * height]; for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { var (blue, green, red, alpha) = pixelOf(x, y); var offset = y * stride + x * 4; pixels[offset] = Premultiply(blue, alpha); pixels[offset + 1] = Premultiply(green, alpha); pixels[offset + 2] = Premultiply(red, alpha); pixels[offset + 3] = alpha; } } bitmap.CopyFromMemory(pixels, stride); } public int GetFrameIndex(TimeSpan time) => FrameTime.TimeToFrame(time, frameRateNumerator, frameRateDenominator); public void Dispose() { if (IsDisposed) return; IsDisposed = true; output.Dispose(); centering.SetInput(0, null, true); centering.Dispose(); bitmap.Dispose(); } static byte Premultiply(byte value, byte alpha) => (byte)Math.Round(value * (double)alpha / byte.MaxValue, MidpointRounding.AwayFromZero); }