Reduce Memory Copy When Frame Capture Success

This commit is contained in:
copecone committed 2026-06-01 11:59:39 +09:00
1 parent 036216c006
commit 6bd4a8975e
3 files changed
+32 -11

No files matched your search

+1 -11
View File
@@ -463,17 +463,7 @@ namespace ADOFAIRenderer.Core
}
else if (frame.NativePointer != IntPtr.Zero && frame.Length > 0)
{
var buffer = FFmpegProcess.RentRawBytes(frame.Length);
unsafe
{
fixed (byte* destPtr = buffer)
{
Buffer.MemoryCopy((void*)frame.NativePointer, destPtr, buffer.Length, frame.Length);
}
}
videoFFmpeg.SendDataFromBuffer(buffer, frame.Length);
ArrayPool<byte>.Shared.Return(buffer);
videoFFmpeg.SendDataFromNativeBuffer(frame.NativePointer, frame.Length);
if (frame.ReadbackState != null)
{
+7
View File
@@ -44,6 +44,13 @@ namespace ADOFAIRenderer.Util.FFmpeg
_inputStream.Flush();
}
public void SendDataFromNativeBuffer(IntPtr nativePtr, int length)
{
if (_inputStream == null) return;
StreamUtil.WriteNativePtrToStream(nativePtr, length, _inputStream);
_inputStream.Flush();
}
public void Close()
{
if (_ffmpegProcess == null || _ffmpegProcess.HasExited || _inputStream == null) return;
+24
View File
@@ -0,0 +1,24 @@
using System;
using System.IO;
namespace ADOFAIRenderer.Util
{
public static class StreamUtil
{
public static void WriteNativePtrToStream(IntPtr nativePtr, int length, Stream destinationStream)
{
if (nativePtr == IntPtr.Zero)
throw new ArgumentNullException(nameof(nativePtr));
if (length <= 0) return;
unsafe
{
// Create an unmanaged memory stream that wraps the native pointer
// (This does not allocate new memory; it references the existing pointer)
using var unmanagedStream = new UnmanagedMemoryStream((byte*)nativePtr, length);
// Copy the data directly into your target stream
unmanagedStream.CopyTo(destinationStream);
}
}
}
}