From a971ceff2c3e60cd11ba5d8bdb7daa3b8e984f75 Mon Sep 17 00:00:00 2001 From: unclshura Date: Sat, 19 Sep 2026 10:15:04 +0100 Subject: [PATCH] Fixed camera initial position and gravitate center. Fixed bug with reusable buffers. --- Splitter-UI/Services/BufferPool.cs | 64 +++++++------------- Splitter-UI/Services/MatToBitmapConverter.cs | 4 +- Splitter-UI/Services/ThumbnailService.cs | 2 +- splitter-cli/algo/CameraController.cs | 5 +- 4 files changed, 28 insertions(+), 47 deletions(-) diff --git a/Splitter-UI/Services/BufferPool.cs b/Splitter-UI/Services/BufferPool.cs index 3f75e71..d8e61a4 100644 --- a/Splitter-UI/Services/BufferPool.cs +++ b/Splitter-UI/Services/BufferPool.cs @@ -1,62 +1,42 @@ -namespace Splitter_UI.Services; +using System.Buffers; + +namespace Splitter_UI.Services; public sealed class BufferPool : IBufferPool { - - public sealed class Entry + public sealed class Entry : IDisposable { + private bool _disposed; + public readonly int Width; public readonly int Height; - public readonly byte[] Bgr; - public readonly byte[] Bgra; - public Entry(int w, int h) + public byte[] Bgr; + public byte[] Bgra; + + internal Entry(int w, int h) { Width = w; Height = h; - Bgr = new byte[w * h * 3]; - Bgra = new byte[w * h * 4]; + + Bgr = ArrayPool.Shared.Rent(w * h * 3); + Bgra = ArrayPool.Shared.Rent(w * h * 4); } - override public string ToString() => $"Entry({Width}x{Height})"; - } + public void Dispose() + { + if (_disposed) return; + _disposed = true; - private readonly Dictionary<(int w, int h), LinkedListNode> _map; - private readonly LinkedList _lru; - private readonly int _capacity; - private readonly Lock _lock = new(); + ArrayPool.Shared.Return(Bgr); + ArrayPool.Shared.Return(Bgra); + } - public BufferPool() - { - _capacity = 8; - _map = new Dictionary<(int w, int h), LinkedListNode>(_capacity); - _lru = new LinkedList(); + public override string ToString() => $"Entry({Width}x{Height})"; } public Entry Get(int w, int h) { - var key = (w, h); - - if (_map.TryGetValue(key, out var node)) - { - _lru.Remove(node); - _lru.AddLast(node); - return node.Value; - } - - var created = new Entry(w, h); - var newNode = new LinkedListNode(created); - - _lru.AddLast(newNode); - _map[key] = newNode; - - if (_lru.Count > _capacity) - { - var first = _lru.First!; - _lru.RemoveFirst(); - _map.Remove((first.Value.Width, first.Value.Height)); - } - - return created; + return new Entry(w, h); } } diff --git a/Splitter-UI/Services/MatToBitmapConverter.cs b/Splitter-UI/Services/MatToBitmapConverter.cs index 660758b..c651290 100644 --- a/Splitter-UI/Services/MatToBitmapConverter.cs +++ b/Splitter-UI/Services/MatToBitmapConverter.cs @@ -22,7 +22,7 @@ public sealed class MatToBitmapConverter(IBufferPool _pool) : IMatToBitmapConver lock (_sync) { - var entry = _pool.Get(w, h); + using var entry = _pool.Get(w, h); var src = mat; if (!src.IsContinuous()) @@ -65,7 +65,7 @@ public sealed class MatToBitmapConverter(IBufferPool _pool) : IMatToBitmapConver public Bitmap Convert(byte[] bgr, int width, int height, Bitmap? existing = null) { - var entry = _pool.Get(width, height); + using var entry = _pool.Get(width, height); ConvertBgrToBgra(bgr, entry.Bgra, width, height); if (existing is WriteableBitmap wb && diff --git a/Splitter-UI/Services/ThumbnailService.cs b/Splitter-UI/Services/ThumbnailService.cs index 7ffaa60..7be4014 100644 --- a/Splitter-UI/Services/ThumbnailService.cs +++ b/Splitter-UI/Services/ThumbnailService.cs @@ -59,7 +59,7 @@ public sealed class ThumbnailService : IThumbnailService height ??= ThumbHeight; skip ??= TimeSpan.Zero; - var entry = _pool.Get(width.Value, height.Value); + using var entry = _pool.Get(width.Value, height.Value); var ok = await DecodeFrameAsync( entry.Bgr, diff --git a/splitter-cli/algo/CameraController.cs b/splitter-cli/algo/CameraController.cs index 4b92ae2..c874a89 100644 --- a/splitter-cli/algo/CameraController.cs +++ b/splitter-cli/algo/CameraController.cs @@ -47,6 +47,7 @@ public sealed class CameraController _cropHeight = cropHeight; _kalman = kalman; _cmd = cmd; + DefaultCenter = new Point2f(videoWidth * _cmd.GravitateTo.X, videoHeight * _cmd.GravitateTo.Y); _cameraCenter = DefaultCenter; _state = TrackState.Tracking; @@ -58,7 +59,7 @@ public sealed class CameraController _kalman.Reset(_cameraCenter); } - private Point2f DefaultCenter => _cmd.GravitateTo; + private Point2f DefaultCenter { get; set; } public int LostFrames => _lostFrames; public Point2f CameraCenter => _cameraCenter; @@ -148,7 +149,7 @@ public sealed class CameraController smoothedCenter = _kalman.Update(objectCenter); var driftEasing = 0.01f; - var fallbackCenter = new Point2f(_videoWidth / 2f, _videoHeight / 2f); + var fallbackCenter = DefaultCenter; _cameraCenter = new Point2f( _cameraCenter.X + (fallbackCenter.X - _cameraCenter.X) * driftEasing,