diff --git a/LuaScript/AviUtlScriptContext.cs b/LuaScript/AviUtlScriptContext.cs index 2574a52..fde07f7 100644 --- a/LuaScript/AviUtlScriptContext.cs +++ b/LuaScript/AviUtlScriptContext.cs @@ -185,20 +185,29 @@ namespace LuaScript private bool TryWriteBuffer(string id, byte[] data, int width, int height) { + int n = width * height * 4; switch (BufferKind(id, out string key)) { case 'o': - ReplaceBuffer((byte[])data.Clone(), width, height); + if (_copyOutputBuffer is null || _copyOutputBuffer.Length != n) + _copyOutputBuffer = new byte[n]; + Buffer.BlockCopy(data, 0, _copyOutputBuffer, 0, n); + ReplaceBuffer(_copyOutputBuffer, width, height); return true; case 't': case 'c': - _buffers[key] = ((byte[])data.Clone(), width, height); + if (!_buffers.TryGetValue(key, out var existing) || existing.Data.Length != n) + existing.Data = new byte[n]; + Buffer.BlockCopy(data, 0, existing.Data, 0, n); + _buffers[key] = (existing.Data, width, height); return false; default: return false; } } + private byte[]? _copyOutputBuffer; + private static char BufferKind(string id, out string key) { key = string.Empty;