SHA256
Compare commits
25
Commits
| Author | SHA256 | Date | |
|---|---|---|---|
|
|
27e7327c1e
|
||
|
|
2152625e45
|
||
|
|
aefa1409c1
|
||
|
|
e802b6a08a
|
||
|
|
c8050abbc9
|
||
|
|
45a7637a36
|
||
|
|
6955762641
|
||
|
|
ea8d4c91ac
|
||
|
|
5080e7ea1d
|
||
|
|
4e556a4632
|
||
|
|
f1e926f758 | ||
|
|
c62723cf5c
|
||
|
|
fee7077b16
|
||
|
|
9fbf057677
|
||
|
|
f153bf70aa
|
||
|
|
93539307b1
|
||
|
|
c0e902fc1a
|
||
|
|
dd2585ee9f
|
||
|
|
1a672cbf59
|
||
|
|
6d5880fe19
|
||
|
|
72d8d71928
|
||
|
|
413778e165
|
||
|
|
36552511f5
|
||
|
|
a1f9fa36ce
|
||
|
|
00971a6730
|
No files matched your search
@@ -48,3 +48,4 @@ desktop.ini
|
||||
|
||||
# Developer Utility Files
|
||||
/REVIEW_CHECKLIST.md
|
||||
/REVIEW_FFMPEG.md
|
||||
@@ -520,7 +520,9 @@ public class RenderSession
|
||||
}
|
||||
|
||||
RenderManager.Instance.levelPath = levelPath;
|
||||
RenderManager.Instance.Configuration = renderConfig;
|
||||
// The render uses its own copy of the configuration: values adjusted during the render
|
||||
// (e.g. the audio sample rate in SetupPlay) stay in this copy and do not change the caller's object.
|
||||
RenderManager.Instance.Configuration = renderConfig.Clone();
|
||||
|
||||
RenderManager.Instance.stage = RenderStage.Load;
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
+282
-136
@@ -5,7 +5,6 @@ using System.IO;
|
||||
using System.Threading;
|
||||
using System.Buffers;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using ADOFAIRenderer.API;
|
||||
using ADOFAIRenderer.Core.Data;
|
||||
using ADOFAIRenderer.Data;
|
||||
@@ -81,8 +80,28 @@ internal class RenderManager : MonoBehaviour
|
||||
public static bool IsActive => Instance && Instance.stage >= RenderStage.OnScene;
|
||||
public static bool IsCapturingActive => Instance && Instance.stage >= RenderStage.Render;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum number of frames in ImageQueue. The main thread waits (throttles) while the queue holds more frames.
|
||||
/// </summary>
|
||||
public const int MaxQueuedFrames = 64;
|
||||
|
||||
/// <summary>
|
||||
/// Upper bound of the readback buffer bytes of the frames in ImageQueue (4 GiB).
|
||||
/// The frame limit becomes min(MaxQueuedFrames, MaxQueuedBytes / bytes per frame) (see ComputeQueueFrameLimit),
|
||||
/// so it only lowers the frame count at resolutions where MaxQueuedFrames frames exceed this size.
|
||||
/// </summary>
|
||||
public const long MaxQueuedBytes = 4L * 1024 * 1024 * 1024;
|
||||
|
||||
/// <summary>
|
||||
/// Wait time of the throttle loop (OnPostFrame) for the render thread to take a frame from ImageQueue.
|
||||
/// When it elapses without a dequeue, AsyncGPUReadback.WaitAllRequests() is called once so the readbacks
|
||||
/// still in flight complete (the render thread may be waiting for one of them to fill a gap in ImageQueue).
|
||||
/// </summary>
|
||||
private const int ThrottleWaitMilliseconds = 10;
|
||||
|
||||
/// <summary>Frame limit of ImageQueue applied by the last throttle check (MaxQueuedFrames or lower).</summary>
|
||||
public int QueueFrameLimit { get; private set; } = MaxQueuedFrames;
|
||||
|
||||
// Statistics
|
||||
public long framesSent;
|
||||
private readonly Stopwatch _renderStopwatch = new();
|
||||
@@ -213,6 +232,8 @@ internal class RenderManager : MonoBehaviour
|
||||
// Thread and Queue State
|
||||
internal SortedList<long, CapturedFrame> ImageQueue = new();
|
||||
internal AutoResetEvent FrameAvailableEvent = new(false);
|
||||
// Set by the render thread each time it takes a frame from ImageQueue; the throttle loop waits on it.
|
||||
internal AutoResetEvent FrameDequeuedEvent = new(false);
|
||||
public Thread? RenderThread;
|
||||
public long passCount;
|
||||
public volatile bool endRequested;
|
||||
@@ -226,6 +247,11 @@ internal class RenderManager : MonoBehaviour
|
||||
private GameObject? _debugMonitorObj;
|
||||
private GameObject? _escapeHandlerObj;
|
||||
internal string? CurrentOutPath;
|
||||
|
||||
// Output file of the current render that is not finished yet.
|
||||
// Set just before the encoder opens the file, cleared by the render thread after the encoder finished the file (End).
|
||||
// Only this file is deleted when a render is cancelled, fails or the game exits (DeleteIncompleteOutputFile).
|
||||
private string? _incompleteOutputPath;
|
||||
private bool _renderSetup;
|
||||
|
||||
private void Awake()
|
||||
@@ -286,6 +312,32 @@ internal class RenderManager : MonoBehaviour
|
||||
{
|
||||
RenderThread.Join(200);
|
||||
}
|
||||
|
||||
// The encoders are disposed above, which closes the output file. An unfinished file of this render is deleted.
|
||||
DeleteIncompleteOutputFile();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the output file of the current render when the render did not finish it (_incompleteOutputPath).
|
||||
/// Files of earlier, finished renders are never deleted.
|
||||
/// Returns true when a file was deleted.
|
||||
/// </summary>
|
||||
private bool DeleteIncompleteOutputFile()
|
||||
{
|
||||
var path = Interlocked.Exchange(ref _incompleteOutputPath, null);
|
||||
if (string.IsNullOrEmpty(path) || !File.Exists(path)) return false;
|
||||
|
||||
try
|
||||
{
|
||||
File.Delete(path);
|
||||
Melon<RenderMod>.Logger.Msg($"[File Cleanup] Deleted incomplete video: {path}");
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Melon<RenderMod>.Logger.Error($"[File Cleanup] Failed to delete incomplete video: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
@@ -365,26 +417,73 @@ internal class RenderManager : MonoBehaviour
|
||||
frameCount++;
|
||||
}
|
||||
|
||||
// Floors that own a hold renderer, collected from scrLevelMaker.listFloors (see RefreshHoldFloors).
|
||||
private readonly List<scrFloor> _holdFloors = new();
|
||||
private List<scrFloor>? _holdFloorsSource;
|
||||
private int _holdFloorsSourceCount = -1;
|
||||
|
||||
public void OnLastUpdate()
|
||||
{
|
||||
if (Instance.stage < RenderStage.Load) return;
|
||||
if (!Configuration.AdvancedConfig.NonTile) return;
|
||||
foreach (var floor in scrLevelMaker.instance.listFloors
|
||||
.Where(floor => floor.holdRenderer && floor.holdRenderer.m_meshRenderer.enabled))
|
||||
|
||||
if (Configuration.AdvancedConfig.NonTile)
|
||||
{
|
||||
floor.holdRenderer.m_meshRenderer.enabled = false;
|
||||
// scrHoldRenderer.UpdateColor sets m_meshRenderer.enabled = true every time it runs
|
||||
// (scrHoldRenderer.cs, called from scrPlanet/scrPlayer), so the hold meshes are disabled again every frame.
|
||||
RefreshHoldFloors();
|
||||
foreach (var floor in _holdFloors)
|
||||
{
|
||||
if (!floor || !floor.holdRenderer) continue;
|
||||
var meshRenderer = floor.holdRenderer.m_meshRenderer;
|
||||
if (meshRenderer && meshRenderer.enabled) meshRenderer.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
TriggerOnLastUpdate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rebuilds the hold floor list when scrLevelMaker.listFloors is a different list or has a different count
|
||||
/// (hold renderers are created together with the floors in scrLevelMaker).
|
||||
/// </summary>
|
||||
private void RefreshHoldFloors()
|
||||
{
|
||||
var floors = scrLevelMaker.instance.listFloors;
|
||||
if (ReferenceEquals(floors, _holdFloorsSource) && floors.Count == _holdFloorsSourceCount) return;
|
||||
|
||||
_holdFloors.Clear();
|
||||
foreach (var floor in floors)
|
||||
{
|
||||
if (floor && floor.holdRenderer) _holdFloors.Add(floor);
|
||||
}
|
||||
_holdFloorsSource = floors;
|
||||
_holdFloorsSourceCount = floors.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Frame limit of ImageQueue for the given capture texture: MaxQueuedBytes divided by the readback bytes of
|
||||
/// one frame (YUV 4:2:0 when the GPU conversion is active, RGBA otherwise), within 4 to MaxQueuedFrames.
|
||||
/// </summary>
|
||||
private int ComputeQueueFrameLimit(RenderTexture texture)
|
||||
{
|
||||
var frameBytes = _frameReader?.GetFrameBufferBytes(texture.width, texture.height)
|
||||
?? (long)texture.width * texture.height * 4;
|
||||
if (frameBytes <= 0) return MaxQueuedFrames;
|
||||
|
||||
return (int)Math.Min(MaxQueuedFrames, Math.Max(4, MaxQueuedBytes / frameBytes));
|
||||
}
|
||||
|
||||
// ReSharper disable once Unity.IncorrectMethodSignature
|
||||
public void OnPostFrame(RenderTexture? texture)
|
||||
{
|
||||
if (Instance.stage < RenderStage.Render || !texture) return;
|
||||
|
||||
// [Backpressure Throttling]
|
||||
// The main thread waits while ImageQueue holds more than QueueFrameLimit frames. Only the render thread
|
||||
// removes frames, and it signals FrameDequeuedEvent for each one. The wait has a timeout so that the loop
|
||||
// re-checks the render thread state and pumps pending readback completions (see ThrottleWaitMilliseconds).
|
||||
isThrottling = false;
|
||||
var frameLimit = QueueFrameLimit = ComputeQueueFrameLimit(texture!);
|
||||
|
||||
while (Instance.stage >= RenderStage.Render)
|
||||
{
|
||||
@@ -400,7 +499,7 @@ internal class RenderManager : MonoBehaviour
|
||||
count = ImageQueue.Count;
|
||||
}
|
||||
|
||||
if (count <= MaxQueuedFrames) break;
|
||||
if (count <= frameLimit) break;
|
||||
|
||||
if (!isThrottling)
|
||||
{
|
||||
@@ -408,7 +507,10 @@ internal class RenderManager : MonoBehaviour
|
||||
throttleCount++;
|
||||
}
|
||||
|
||||
AsyncGPUReadback.WaitAllRequests();
|
||||
if (!FrameDequeuedEvent.WaitOne(ThrottleWaitMilliseconds))
|
||||
{
|
||||
AsyncGPUReadback.WaitAllRequests();
|
||||
}
|
||||
}
|
||||
isThrottling = false;
|
||||
|
||||
@@ -588,6 +690,7 @@ internal class RenderManager : MonoBehaviour
|
||||
endRequested = false;
|
||||
endSuccessful = false;
|
||||
FrameAvailableEvent.Reset();
|
||||
FrameDequeuedEvent.Reset();
|
||||
|
||||
var renderFPS = Configuration.RenderFPS;
|
||||
var videoFPS = Configuration.VideoFPS;
|
||||
@@ -634,6 +737,7 @@ internal class RenderManager : MonoBehaviour
|
||||
{
|
||||
_allVideoFFmpegInstances.Add(videoFFmpeg);
|
||||
}
|
||||
Volatile.Write(ref _incompleteOutputPath, CurrentOutPath);
|
||||
videoFFmpeg.Start(CurrentOutPath);
|
||||
}
|
||||
catch (Exception)
|
||||
@@ -650,123 +754,8 @@ internal class RenderManager : MonoBehaviour
|
||||
throw;
|
||||
}
|
||||
|
||||
RenderThread = new Thread(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
CapturedFrame frame = default;
|
||||
bool hasFrame = false;
|
||||
lock (ImageQueue)
|
||||
{
|
||||
if (ImageQueue.Count > 0)
|
||||
{
|
||||
var firstKey = ImageQueue.Keys[0];
|
||||
|
||||
if (firstKey > passCount && Instance.stage < RenderStage.Render)
|
||||
{
|
||||
passCount = firstKey;
|
||||
}
|
||||
|
||||
if (firstKey == passCount)
|
||||
{
|
||||
frame = ImageQueue[firstKey];
|
||||
ImageQueue.RemoveAt(0);
|
||||
passCount++;
|
||||
hasFrame = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (hasFrame)
|
||||
{
|
||||
if (frame.ManagedBuffer != null)
|
||||
{
|
||||
if (frame.ManagedBuffer.Length > 0)
|
||||
{
|
||||
videoFFmpeg.SendDataFromBuffer(frame.ManagedBuffer, frame.Length, frame.IsColorConverted);
|
||||
Interlocked.Increment(ref framesSent);
|
||||
|
||||
ArrayPool<byte>.Shared.Return(frame.ManagedBuffer);
|
||||
}
|
||||
}
|
||||
else if (frame.NativePointer != IntPtr.Zero && frame.Length > 0)
|
||||
{
|
||||
videoFFmpeg.SendDataFromNativeBuffer(frame.NativePointer, frame.Length, frame.IsColorConverted);
|
||||
Interlocked.Increment(ref framesSent);
|
||||
|
||||
if (frame.ReadbackState != null)
|
||||
{
|
||||
_frameReader?.RecycleState(frame.ReadbackState);
|
||||
}
|
||||
}
|
||||
|
||||
if (Configuration.AudioConfig.RenderAudio && SyncCalculator is { } syncCalculator)
|
||||
{
|
||||
var frameIdx = (int)(passCount - 1);
|
||||
var effectiveVideoFps = Math.Min(Configuration.VideoFPS, Configuration.RenderFPS);
|
||||
var frameTime = frameIdx / effectiveVideoFps;
|
||||
|
||||
var channels = Configuration.AudioConfig.AudioChannels;
|
||||
var samplesNeeded = syncCalculator.GetNextFrameSamples();
|
||||
|
||||
var audioBuffer = ArrayPool<float>.Shared.Rent(samplesNeeded * channels);
|
||||
try
|
||||
{
|
||||
AudioMixer?.GenerateAudioForFrame(audioBuffer, samplesNeeded, frameTime, dspTimeBase);
|
||||
videoFFmpeg.WriteAudioSamples(audioBuffer, samplesNeeded);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<float>.Shared.Return(audioBuffer);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (IsRenderAborted) break;
|
||||
|
||||
if (endRequested)
|
||||
{
|
||||
if (passCount > Volatile.Read(ref lastRequestedVideoFrameIndex)) break;
|
||||
}
|
||||
|
||||
FrameAvailableEvent.WaitOne(50);
|
||||
}
|
||||
|
||||
if (!IsRenderAborted) videoFFmpeg.End();
|
||||
videoFFmpeg.Dispose();
|
||||
|
||||
lock (_allVideoFFmpegInstances)
|
||||
{
|
||||
_allVideoFFmpegInstances.Remove(videoFFmpeg);
|
||||
}
|
||||
|
||||
_activeVideoFFmpeg = null;
|
||||
endSuccessful = !IsRenderAborted;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Melon<RenderMod>.Logger.Error($"FFmpeg rendering thread error: {ex}");
|
||||
|
||||
MarkFailed("encoder thread error. See the log for details.");
|
||||
try
|
||||
{
|
||||
videoFFmpeg.Dispose();
|
||||
}
|
||||
catch (Exception disposeEx)
|
||||
{
|
||||
Melon<RenderMod>.Logger.Error($"Encoder dispose after thread error also failed: {disposeEx.Message}");
|
||||
}
|
||||
|
||||
lock (_allVideoFFmpegInstances)
|
||||
{
|
||||
_allVideoFFmpegInstances.Remove(videoFFmpeg);
|
||||
}
|
||||
_activeVideoFFmpeg = null;
|
||||
}
|
||||
})
|
||||
var encoder = videoFFmpeg;
|
||||
RenderThread = new Thread(() => RunRenderThread(encoder))
|
||||
{
|
||||
IsBackground = true,
|
||||
Priority = ThreadPriority.AboveNormal
|
||||
@@ -775,6 +764,172 @@ internal class RenderManager : MonoBehaviour
|
||||
RenderThread.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Body of the render thread. Sends queued frames and their audio to the encoder until the render ends,
|
||||
/// finishes the output file, and releases the encoder. An exception is handled as a render failure.
|
||||
/// </summary>
|
||||
private void RunRenderThread(FFmpegNativeEncoder videoFFmpeg)
|
||||
{
|
||||
try
|
||||
{
|
||||
EncodeQueuedFrames(videoFFmpeg);
|
||||
|
||||
if (!IsRenderAborted)
|
||||
{
|
||||
// End throws when the file cannot be finished (e.g. the trailer cannot be written),
|
||||
// which is handled as a render failure by the catch block below.
|
||||
videoFFmpeg.End();
|
||||
Volatile.Write(ref _incompleteOutputPath, null);
|
||||
}
|
||||
videoFFmpeg.Dispose();
|
||||
UnregisterEncoder(videoFFmpeg);
|
||||
|
||||
endSuccessful = !IsRenderAborted;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Melon<RenderMod>.Logger.Error($"FFmpeg rendering thread error: {ex}");
|
||||
|
||||
MarkFailed("encoder thread error. See the log for details.");
|
||||
try
|
||||
{
|
||||
videoFFmpeg.Dispose();
|
||||
}
|
||||
catch (Exception disposeEx)
|
||||
{
|
||||
Melon<RenderMod>.Logger.Error($"Encoder dispose after thread error also failed: {disposeEx.Message}");
|
||||
}
|
||||
UnregisterEncoder(videoFFmpeg);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes frames from ImageQueue in passCount order and sends each one to the encoder, followed by the audio
|
||||
/// samples of that frame. Waits on FrameAvailableEvent (50ms) while no frame is ready.
|
||||
/// Returns when the render is aborted (checked before every frame, frames left in ImageQueue are released
|
||||
/// by the cleanup after the thread ends) or all requested frames have been sent after endRequested.
|
||||
/// </summary>
|
||||
private void EncodeQueuedFrames(FFmpegNativeEncoder videoFFmpeg)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (IsRenderAborted) return;
|
||||
|
||||
if (TryDequeueNextFrame(out var frame))
|
||||
{
|
||||
FrameDequeuedEvent.Set();
|
||||
SendVideoFrame(videoFFmpeg, frame);
|
||||
WriteAudioForFrame(videoFFmpeg);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ShouldStopEncodeLoop()) return;
|
||||
|
||||
FrameAvailableEvent.WaitOne(50);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the frame whose key equals passCount from ImageQueue and advances passCount.
|
||||
/// Before the Render stage, passCount jumps to the first queued key (the first captured frame index is not 0).
|
||||
/// Returns false when the queue is empty or the next frame has not arrived yet.
|
||||
/// </summary>
|
||||
private bool TryDequeueNextFrame(out CapturedFrame frame)
|
||||
{
|
||||
frame = default;
|
||||
lock (ImageQueue)
|
||||
{
|
||||
if (ImageQueue.Count == 0) return false;
|
||||
|
||||
var firstKey = ImageQueue.Keys[0];
|
||||
if (firstKey > passCount && Instance.stage < RenderStage.Render)
|
||||
{
|
||||
passCount = firstKey;
|
||||
}
|
||||
|
||||
if (firstKey != passCount) return false;
|
||||
|
||||
frame = ImageQueue[firstKey];
|
||||
ImageQueue.RemoveAt(0);
|
||||
passCount++;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends one captured frame to the encoder and releases its buffer (managed: ArrayPool, native: readback state).
|
||||
/// A frame without data is skipped.
|
||||
/// </summary>
|
||||
private void SendVideoFrame(FFmpegNativeEncoder videoFFmpeg, in CapturedFrame frame)
|
||||
{
|
||||
if (frame.ManagedBuffer != null)
|
||||
{
|
||||
if (frame.ManagedBuffer.Length == 0) return;
|
||||
|
||||
videoFFmpeg.SendDataFromBuffer(frame.ManagedBuffer, frame.Length, frame.IsColorConverted);
|
||||
Interlocked.Increment(ref framesSent);
|
||||
|
||||
ArrayPool<byte>.Shared.Return(frame.ManagedBuffer);
|
||||
return;
|
||||
}
|
||||
|
||||
if (frame.NativePointer == IntPtr.Zero || frame.Length <= 0) return;
|
||||
|
||||
videoFFmpeg.SendDataFromNativeBuffer(frame.NativePointer, frame.Length, frame.IsColorConverted);
|
||||
Interlocked.Increment(ref framesSent);
|
||||
|
||||
if (frame.ReadbackState != null)
|
||||
{
|
||||
_frameReader?.RecycleState(frame.ReadbackState);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Mixes the audio of the frame that was just sent (index passCount - 1) and writes it to the encoder.
|
||||
/// Does nothing when audio rendering is off.
|
||||
/// </summary>
|
||||
private void WriteAudioForFrame(FFmpegNativeEncoder videoFFmpeg)
|
||||
{
|
||||
if (!Configuration.AudioConfig.RenderAudio || SyncCalculator is not { } syncCalculator) return;
|
||||
|
||||
var frameIdx = (int)(passCount - 1);
|
||||
var effectiveVideoFps = Math.Min(Configuration.VideoFPS, Configuration.RenderFPS);
|
||||
var frameTime = frameIdx / effectiveVideoFps;
|
||||
|
||||
var channels = Configuration.AudioConfig.AudioChannels;
|
||||
var samplesNeeded = syncCalculator.GetNextFrameSamples();
|
||||
|
||||
var audioBuffer = ArrayPool<float>.Shared.Rent(samplesNeeded * channels);
|
||||
try
|
||||
{
|
||||
AudioMixer?.GenerateAudioForFrame(audioBuffer, samplesNeeded, frameTime, dspTimeBase);
|
||||
videoFFmpeg.WriteAudioSamples(audioBuffer, samplesNeeded);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<float>.Shared.Return(audioBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True when the render is aborted, or when the end was requested and every requested frame has been sent.
|
||||
/// </summary>
|
||||
private bool ShouldStopEncodeLoop()
|
||||
{
|
||||
if (IsRenderAborted) return true;
|
||||
|
||||
return endRequested && passCount > Volatile.Read(ref lastRequestedVideoFrameIndex);
|
||||
}
|
||||
|
||||
private void UnregisterEncoder(FFmpegNativeEncoder videoFFmpeg)
|
||||
{
|
||||
lock (_allVideoFFmpegInstances)
|
||||
{
|
||||
_allVideoFFmpegInstances.Remove(videoFFmpeg);
|
||||
}
|
||||
_activeVideoFFmpeg = null;
|
||||
}
|
||||
|
||||
public void CancelRender()
|
||||
{
|
||||
ResetRenderState(success: false);
|
||||
@@ -805,7 +960,7 @@ internal class RenderManager : MonoBehaviour
|
||||
var reason = IsRenderFailed ? $"FAILED ({_failureReason})" : "CANCELLED";
|
||||
Melon<RenderMod>.Logger.Error($"Rendering {reason}. Safely cleaning up state and resources.");
|
||||
|
||||
// 1. Wait for render background thread to finish processing remaining queued frames and terminate
|
||||
// 1. Wait (up to 2s) for the render background thread to see the abort flag and terminate
|
||||
if (RenderThread is { IsAlive: true })
|
||||
{
|
||||
RenderThread.Join(2000);
|
||||
@@ -862,18 +1017,9 @@ internal class RenderManager : MonoBehaviour
|
||||
scrController.instance.noFailInfiniteMargin = false;
|
||||
}
|
||||
|
||||
// 6. Immediately delete cancelled incomplete temporary video file
|
||||
if (!string.IsNullOrEmpty(CurrentOutPath) && File.Exists(CurrentOutPath))
|
||||
// 6. Immediately delete the incomplete video file of this render
|
||||
if (DeleteIncompleteOutputFile())
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(CurrentOutPath);
|
||||
Melon<RenderMod>.Logger.Msg($"[File Cleanup] Deleted incomplete video: {CurrentOutPath}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Melon<RenderMod>.Logger.Error($"[File Cleanup] Failed to delete temporary video: {ex.Message}");
|
||||
}
|
||||
CurrentOutPath = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
|
||||
---
|
||||
|
||||
## 1. Legal Terms (Official)
|
||||
|
||||
Linking this Program statically or dynamically with other modules is making a combined work based on this Program. Thus, the terms and conditions of the GNU General Public License cover the whole combination.
|
||||
|
||||
As a special exception, the copyright holders of this Program give you permission to dynamically link this Program with independent modules that interact with or reference this Program solely through the public application programming interfaces (APIs), types, and data structures declared within the following namespaces:
|
||||
@@ -22,21 +20,3 @@ An **"independent module"** is a separate and distinct software module that is n
|
||||
This exception does not grant any permission to modify the source code of this Program itself outside the terms of the GNU General Public License version 3.
|
||||
|
||||
In accordance with section 7 of the GNU GPL version 3, if you modify this Program, you may extend this exception to your version of the Program, but you are not obligated to do so. If you do not wish to provide this exception in your derivative work, you may remove this exception statement from your version.
|
||||
|
||||
---
|
||||
|
||||
## 2. 조항 해설 (Korean Guide)
|
||||
|
||||
본 문서는 GNU General Public License 버전 3 (GPLv3) 제7조(추가적인 허가, Additional Permissions)에 따라 제공되는 공식 예외 조항입니다.
|
||||
|
||||
### 핵심 요약
|
||||
|
||||
- **동적 링킹(Dynamic Linking)만 허용**:
|
||||
- 본 예외 조항은 **동적 링킹(런타임 참조 및 어셈블리 동적 로드)**에만 배타적으로 적용됩니다.
|
||||
- ADOFAIRenderer 또는 그 일부 코드를 대상 모드/프로그램에 직접 병합하거나 포함(ILMerge, Costura, 바이너리 직접 내장/정적 컴파일 등)하는 **정적 링킹(Static Linking)은 본 예외 대상에서 제외**되며, 순수 GPLv3의 카피레프트(전염성) 의무가 그대로 적용됩니다.
|
||||
- **외부 모드의 라이선스 자유**:
|
||||
- 제3자 모드 또는 외부 프로그램이 동적 링킹을 통해 `ADOFAIRenderer.API` 및 `ADOFAIRenderer.Data` 네임스페이스의 공개 API, 클래스, 데이터 구조만을 참조·호출하여 연동하는 경우, 해당 모드는 **GPLv3의 카피레프트 의무를 적용받지 않습니다.**
|
||||
- 외부 모드 개발자는 자신의 모드를 MIT, Apache-2.0, 독점/상용 라이선스 등 원하는 라이선스로 자유롭게 배포할 수 있습니다.
|
||||
- **ADOFAIRenderer 코어 보호**:
|
||||
- ADOFAIRenderer 자체의 소스 코드(렌더링 파이프라인, 코어 패치, 내부 유틸리티 등)를 수정하거나 재배포하는 경우에는 본 예외 조항이 적용되지 않으며, 순수 GPLv3 라이선스 조건이 그대로 적용됩니다.
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using ADOFAIRenderer.Core;
|
||||
using HarmonyLib;
|
||||
|
||||
namespace ADOFAIRenderer.Patch.Game;
|
||||
|
||||
[HarmonyPatch(typeof(DetailedResults))]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static class DetailedResultsPatch
|
||||
{
|
||||
[HarmonyPrefix]
|
||||
[HarmonyPatch("Show")]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static bool PreventShow(DetailedResults __instance)
|
||||
{
|
||||
if (!RenderManager.IsActive) return true;
|
||||
|
||||
if (__instance)
|
||||
{
|
||||
__instance.enabled = false;
|
||||
if (__instance.gameObject) __instance.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -41,4 +41,38 @@ public static class scrControllerPatch
|
||||
{
|
||||
return !RenderManager.IsActive;
|
||||
}
|
||||
|
||||
[HarmonyPostfix]
|
||||
[HarmonyPatch("OnLandOnPortal")]
|
||||
public static void PreventWinTextsPostfix(scrController __instance)
|
||||
{
|
||||
if (!RenderManager.IsActive || __instance == null) return;
|
||||
|
||||
if (__instance.txtCongrats)
|
||||
{
|
||||
__instance.txtCongrats.text = string.Empty;
|
||||
__instance.txtCongrats.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
if (__instance.txtAprilCongrats)
|
||||
{
|
||||
__instance.txtAprilCongrats.text = string.Empty;
|
||||
__instance.txtAprilCongrats.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
if (__instance.txtAllStrictClear)
|
||||
{
|
||||
__instance.txtAllStrictClear.text = string.Empty;
|
||||
__instance.txtAllStrictClear.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
if (__instance.detailedResults)
|
||||
{
|
||||
__instance.detailedResults.enabled = false;
|
||||
if (__instance.detailedResults.gameObject != null)
|
||||
{
|
||||
__instance.detailedResults.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,12 @@ public class scrPressToStartPatch
|
||||
if (!RenderManager.Instance || !RenderManager.Instance.skipPressToStart) return true;
|
||||
|
||||
RenderManager.Instance.skipPressToStart = false;
|
||||
|
||||
// scrPressToStart.ShowText ends with ShowDifficultyContainer(mode). Skipping ShowText skips that call too,
|
||||
// which leaves the difficulty selector active and clickable. DontShow deactivates difficultyContainer
|
||||
// and makes the keyboard difficulty shortcuts (difficultyUIMode != DontShow checks) do nothing.
|
||||
if (scrUIController.instance)
|
||||
scrUIController.instance.ShowDifficultyContainer(DifficultyUIMode.DontShow);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -9,7 +9,7 @@ using MelonLoader.Utils;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
[assembly: MelonInfo(typeof(RenderMod), "ADOFAI Renderer", "0.2.6-INDEV", "ADOFAI Editor Forum")]
|
||||
[assembly: MelonInfo(typeof(RenderMod), "ADOFAI Renderer", "0.4.0-BETA", "ADOFAI Editor Forum")]
|
||||
[assembly: MelonGame("7th Beat Games", "A Dance of Fire and Ice")]
|
||||
|
||||
namespace ADOFAIRenderer;
|
||||
|
||||
@@ -164,7 +164,7 @@ internal class DebugMonitorUI : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
sb.AppendLine($"• Queue: <b>{queueCount}</b> / {RenderManager.MaxQueuedFrames} frames");
|
||||
sb.AppendLine($"• Queue: <b>{queueCount}</b> / {(RenderManager.Instance ? RenderManager.Instance.QueueFrameLimit : RenderManager.MaxQueuedFrames)} frames");
|
||||
sb.AppendLine($"• Rendered Frames: <b>{(RenderManager.Instance ? RenderManager.Instance.frameCount : 0)}</b>");
|
||||
sb.AppendLine($"• Frames Sent: <b>{(RenderManager.Instance ? Interlocked.Read(ref RenderManager.Instance.framesSent) : 0)}</b>");
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ using ADOFAIRenderer.Data;
|
||||
using ADOFAIRenderer.Localization;
|
||||
using ADOFAIRenderer.Util.OS;
|
||||
using MelonLoader;
|
||||
using Newtonsoft.Json;
|
||||
using O5Kit.Control;
|
||||
using O5Kit.Core;
|
||||
using O5Kit.Factory;
|
||||
@@ -32,6 +33,14 @@ internal static class RenderConfigWindow
|
||||
private static Coroutine? _deleteResetCoroutine;
|
||||
private static string? _lastLoadedPresetName;
|
||||
|
||||
/// <summary>
|
||||
/// Configuration edited by this window. Kept for the whole game session; a render started from the window
|
||||
/// receives a copy of it (RenderSession.Start), so values adjusted during the render are not written back here.
|
||||
/// </summary>
|
||||
private static RenderConfig? _uiConfig;
|
||||
|
||||
private static RenderConfig UiConfig => _uiConfig ??= new RenderConfig();
|
||||
|
||||
// Controls cache for validation & sync
|
||||
private static O5Slider? _renderFpsSlider;
|
||||
private static O5Slider? _videoFPSSlider;
|
||||
@@ -111,8 +120,7 @@ internal static class RenderConfigWindow
|
||||
ReloadPresetDropdown(GlobalSettings.Current.LastPresetName);
|
||||
}
|
||||
|
||||
// ReSharper disable once NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract
|
||||
var config = RenderManager.Instance.Configuration ??= new RenderConfig();
|
||||
var config = UiConfig;
|
||||
ApplyConfigToUI(config);
|
||||
|
||||
SetWindowActive(true);
|
||||
@@ -125,8 +133,7 @@ internal static class RenderConfigWindow
|
||||
var (_, scrollContent, _) =
|
||||
O5Factory.ScrollView(ctx, content, spacing: 10f, padding: 8f);
|
||||
|
||||
// ReSharper disable once NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract
|
||||
var config = RenderManager.Instance.Configuration ??= new RenderConfig();
|
||||
var config = UiConfig;
|
||||
EnsureDefaultConfig(config);
|
||||
|
||||
double[] renderFpsSnap = { 23.976, 24, 29.97, 30, 59.94, 60, 144,
|
||||
@@ -744,11 +751,14 @@ internal static class RenderConfigWindow
|
||||
{
|
||||
EnsureDefaultConfig(config);
|
||||
|
||||
_renderFpsSlider?.Set((float) config.RenderFPS, false);
|
||||
_videoFPSSlider?.Set((float) config.VideoFPS, false);
|
||||
_renderWidthSlider?.Set(config.RenderWidth, false);
|
||||
_renderHeightSlider?.Set(config.RenderHeight, false);
|
||||
_delayAfterEndSlider?.Set((float) config.DelayAfterEnd, false);
|
||||
// Sliders show the config values as they are (noFilter: true).
|
||||
// O5Slider.Set applies the slider filter (snapping to the nearest snap value) unless noFilter is set;
|
||||
// the filter is meant for dragging, and typed values are not snapped either (O5Slider.SetFromInput).
|
||||
_renderFpsSlider?.Set(config.RenderFPS, false, noFilter: true);
|
||||
_videoFPSSlider?.Set(config.VideoFPS, false, noFilter: true);
|
||||
_renderWidthSlider?.Set(config.RenderWidth, false, noFilter: true);
|
||||
_renderHeightSlider?.Set(config.RenderHeight, false, noFilter: true);
|
||||
_delayAfterEndSlider?.Set(config.DelayAfterEnd, false, noFilter: true);
|
||||
_showJudgementToggle?.Set(config.ShowJudgementText, false);
|
||||
|
||||
_codecDropdown?.Set(config.VideoConfig.Codec, false);
|
||||
@@ -756,13 +766,13 @@ internal static class RenderConfigWindow
|
||||
_bitrateInput?.Set(config.VideoConfig.Bitrate.ToString(), false);
|
||||
_maxBitrateInput?.Set(config.VideoConfig.MaxBitrate.ToString(), false);
|
||||
_qpInput?.Set(config.VideoConfig.QP.ToString(), false);
|
||||
_keyframeIntervalSlider?.Set(config.VideoConfig.KeyframeInterval, false);
|
||||
_keyframeIntervalSlider?.Set(config.VideoConfig.KeyframeInterval, false, noFilter: true);
|
||||
_forceSoftwareToggle?.Set(config.VideoConfig.ForceSoftwareEncoder, false);
|
||||
|
||||
_renderAudioToggle?.Set(config.AudioConfig.RenderAudio, false);
|
||||
_sampleRateDropdown?.Set(config.AudioConfig.AudioSampleRate, false);
|
||||
_channelsDropdown?.Set(config.AudioConfig.AudioChannels, false);
|
||||
_audioBitrateSlider?.Set(config.AudioConfig.AudioBitrate / 1000.0, false);
|
||||
_audioBitrateSlider?.Set(config.AudioConfig.AudioBitrate / 1000.0, false, noFilter: true);
|
||||
|
||||
_outputDirInput?.Set(GlobalSettings.EffectiveOutputDirectory, false);
|
||||
|
||||
@@ -789,17 +799,51 @@ internal static class RenderConfigWindow
|
||||
UpdateVideoCodecUI(config.VideoConfig.Codec, config);
|
||||
UpdateRateControlUI(config.VideoConfig.RateControlMode, noAnimate: true);
|
||||
|
||||
if (!string.IsNullOrEmpty(GlobalSettings.Current.LastPresetName))
|
||||
UpdatePresetLabel(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the preset dropdown from the config values.
|
||||
/// Every value equals the last loaded preset (_lastLoadedPresetName): the preset name is shown.
|
||||
/// Any value differs (edited, or the preset file changed): the Custom label is shown (MarkConfigModified),
|
||||
/// the same state as after editing a value in the window.
|
||||
/// </summary>
|
||||
private static void UpdatePresetLabel(RenderConfig config)
|
||||
{
|
||||
if (_presetDropdown == null) return;
|
||||
|
||||
var presetName = _lastLoadedPresetName;
|
||||
var preset = string.IsNullOrEmpty(presetName)
|
||||
? null
|
||||
: PresetManager.GetPresets().FirstOrDefault(p => string.Equals(p.Name, presetName, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (preset == null || !ConfigValuesEqual(config, preset.Config))
|
||||
{
|
||||
_presetDropdown?.Set(GlobalSettings.Current.LastPresetName, false);
|
||||
_lastLoadedPresetName = GlobalSettings.Current.LastPresetName;
|
||||
UpdateDeleteButtonState();
|
||||
MarkConfigModified();
|
||||
return;
|
||||
}
|
||||
|
||||
if (_presetDropdown.Values.Contains(CustomPresetLabel))
|
||||
{
|
||||
_presetDropdown.SetValues(_presetDropdown.Values.Where(v => v != CustomPresetLabel).ToList());
|
||||
}
|
||||
_presetDropdown.Set(preset.Name, false);
|
||||
UpdateDeleteButtonState();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares two configs by their serialized JSON (the same form as the preset files), so every saved value is compared.
|
||||
/// </summary>
|
||||
private static bool ConfigValuesEqual(RenderConfig a, RenderConfig b)
|
||||
{
|
||||
var settings = new JsonSerializerSettings();
|
||||
settings.Converters.Add(new ColorJsonConverter());
|
||||
return JsonConvert.SerializeObject(a, settings) == JsonConvert.SerializeObject(b, settings);
|
||||
}
|
||||
|
||||
private static void TryGoRender()
|
||||
{
|
||||
var config = RenderManager.Instance.Configuration;
|
||||
var config = UiConfig;
|
||||
SyncUItoConfig(config);
|
||||
|
||||
// Sync Output Directory
|
||||
|
||||
+320
-136
@@ -23,7 +23,7 @@ namespace ADOFAIRenderer.Util.FFmpeg;
|
||||
/// 2. Codec & Hardware Device Discovery:
|
||||
/// a) avcodec_find_decoder: Locate codec implementation by codec ID.
|
||||
/// b) avcodec_get_hw_config: Query supported hardware configurations for the decoder.
|
||||
/// c) Prioritized HW device creation (D3D11VA → DXVA2 → QSV → CUDA → D3D12VA → VAAPI → VideoToolbox).
|
||||
/// c) HW device creation for the reported configurations, in the order of PreferredHwDeviceTypes.
|
||||
/// d) Attach hw_device_ctx and register custom get_format callback.
|
||||
/// e) If HW initialization fails or is unsupported, fall back to CPU software decoding
|
||||
/// with multi-threading enabled (thread_count = 0, FF_THREAD_FRAME).
|
||||
@@ -35,8 +35,10 @@ namespace ADOFAIRenderer.Util.FFmpeg;
|
||||
/// c) (HW Transfer) For hardware frames, av_hwframe_transfer_data downloads GPU surface data
|
||||
/// into system RAM as NV12 (av_frame_copy_props preserves timestamps).
|
||||
/// d) (Output Dispatch):
|
||||
/// - TryReadNV12Frame: Zero-copy direct memory transfer for NV12 frames, sws_scale fallback.
|
||||
/// - TryReadYUV420PFrame: Zero-copy planar copy for YUV420P/YUVJ420P, sws_scale fallback.
|
||||
/// - TryReadNV12Frame: NV12 frames are copied row by row (Buffer.MemoryCopy) into the output buffers,
|
||||
/// other formats are converted with sws_scale.
|
||||
/// - TryReadYUV420PFrame: YUV420P/YUVJ420P planes are copied row by row into the output buffers,
|
||||
/// other formats are converted with sws_scale.
|
||||
/// - TryReadFrame: sws_scale conversion to RGBA32 with optional vertical flip (vflip).
|
||||
///
|
||||
/// 4. Seeking & Scrubbing (SeekToTime / SeekToFrame):
|
||||
@@ -76,9 +78,12 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
|
||||
/// <summary>
|
||||
/// Preferred hardware device types in priority order.
|
||||
/// Modern Windows platforms prioritize D3D11VA for wide driver support and direct integration,
|
||||
/// followed by DXVA2 (DirectX 9 / legacy), Intel QSV, NVIDIA CUDA, and DirectX 12 D3D12VA.
|
||||
/// Non-Windows systems fall back to VAAPI or VideoToolbox.
|
||||
/// The candidates are the hardware configurations the selected decoder reports (avcodec_get_hw_config),
|
||||
/// sorted by this list; configurations not in the list follow in reported order.
|
||||
/// Windows: D3D11VA, then DXVA2 (DirectX 9), CUDA and D3D12VA. Other platforms: VAAPI or VideoToolbox.
|
||||
/// The QSV entry matches only decoders that report a QSV configuration. The native decoders returned by
|
||||
/// avcodec_find_decoder report DXVA2/D3D11VA/D3D12VA/CUDA/VAAPI/Vulkan in the bundled FFmpeg 8 build;
|
||||
/// the QSV decoders (h264_qsv, etc.) are separate decoders selected by name.
|
||||
/// </summary>
|
||||
private static readonly AVHWDeviceType[] PreferredHwDeviceTypes = new[]
|
||||
{
|
||||
@@ -191,6 +196,13 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
private bool _isOpened;
|
||||
private bool _disposed;
|
||||
private bool _isDraining;
|
||||
|
||||
/// <summary>Number of video packets skipped because the decoder rejected them as invalid data.</summary>
|
||||
private int _skippedPacketCount;
|
||||
|
||||
/// <summary>Number of skipped packets for which a warning is logged.</summary>
|
||||
private const int MaxLoggedSkippedPackets = 5;
|
||||
|
||||
private readonly object _lock = new();
|
||||
|
||||
// Fields to detect SwsContext updates
|
||||
@@ -199,6 +211,12 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
private int _srcWidth;
|
||||
private int _srcHeight;
|
||||
|
||||
/// <summary>YUV→RGB matrix id (<c>SWS_CS_*</c>) applied to the context. 0 when no matrix is set (non-RGBA output).</summary>
|
||||
private int _srcSwsColorspace;
|
||||
|
||||
/// <summary>Whether the source range applied to the context is the full range.</summary>
|
||||
private bool _srcFullRange;
|
||||
|
||||
// ──────────────────────────────────────────────────────────────
|
||||
// Public Properties
|
||||
// ──────────────────────────────────────────────────────────────
|
||||
@@ -289,6 +307,59 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Color matrix id used to convert the YUV frames of this stream to RGB, selected from the colorspace tag of the stream.
|
||||
/// The ids are shared with the <c>ColorMatrix</c> uniform of ColorConversion.compute:
|
||||
/// 0 = BT.709 (also used when the stream has no usable tag), 1 = BT.601, 2 = BT.2020 (non-constant luminance).
|
||||
/// </summary>
|
||||
public int ColorMatrix
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_pCodecContext != null && _pCodecContext->colorspace != AVColorSpace.AVCOL_SPC_UNSPECIFIED)
|
||||
{
|
||||
return GetColorMatrixId(_pCodecContext->colorspace);
|
||||
}
|
||||
if (_pFormatContext != null && _videoStreamIndex >= 0)
|
||||
{
|
||||
return GetColorMatrixId(_pFormatContext->streams[_videoStreamIndex]->codecpar->color_space);
|
||||
}
|
||||
return ColorMatrixBT709;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Color matrix id of BT.709.</summary>
|
||||
// ReSharper disable InconsistentNaming
|
||||
public const int ColorMatrixBT709 = 0;
|
||||
|
||||
/// <summary>Color matrix id of BT.601 (SMPTE 170M / BT.470 BG).</summary>
|
||||
public const int ColorMatrixBT601 = 1;
|
||||
|
||||
/// <summary>Color matrix id of BT.2020 (non-constant luminance).</summary>
|
||||
public const int ColorMatrixBT2020 = 2;
|
||||
// ReSharper restore InconsistentNaming
|
||||
|
||||
/// <summary>
|
||||
/// Converts a colorspace tag to a color matrix id (see <see cref="ColorMatrix"/>).
|
||||
/// Tags that have no matrix of their own (unspecified, RGB, other standards) use BT.709.
|
||||
/// </summary>
|
||||
private static int GetColorMatrixId(AVColorSpace colorspace)
|
||||
{
|
||||
switch (colorspace)
|
||||
{
|
||||
case AVColorSpace.AVCOL_SPC_SMPTE170M:
|
||||
case AVColorSpace.AVCOL_SPC_BT470BG:
|
||||
return ColorMatrixBT601;
|
||||
case AVColorSpace.AVCOL_SPC_BT2020_NCL:
|
||||
return ColorMatrixBT2020;
|
||||
default:
|
||||
return ColorMatrixBT709;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether hardware-accelerated decoding is currently active.
|
||||
/// </summary>
|
||||
@@ -371,6 +442,16 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
if (_videoStreamIndex == -1)
|
||||
throw new Exception("Failed to find video stream.");
|
||||
|
||||
// Only the video stream is used. The demuxer does not need to return the packets of the other streams
|
||||
// (audio, subtitles, ...) once they are marked as discarded.
|
||||
// Reference: AVStream.discard - "Selects which packets can be discarded at will and do not need to be demuxed."
|
||||
// https://ffmpeg.org/doxygen/trunk/structAVStream.html
|
||||
for (int i = 0; i < _pFormatContext->nb_streams; i++)
|
||||
{
|
||||
if (i != _videoStreamIndex)
|
||||
_pFormatContext->streams[i]->discard = AVDiscard.AVDISCARD_ALL;
|
||||
}
|
||||
|
||||
var stream = _pFormatContext->streams[_videoStreamIndex];
|
||||
_timeBase = stream->time_base;
|
||||
|
||||
@@ -441,9 +522,13 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
TotalFrames = 0;
|
||||
}
|
||||
|
||||
Duration = _pFormatContext->duration > 0
|
||||
? _pFormatContext->duration / (double)ffmpeg.AV_TIME_BASE
|
||||
: (stream->duration > 0 ? stream->duration * ffmpeg.av_q2d(stream->time_base) : 0);
|
||||
// Duration of the video stream (AVStream.duration, in the stream time base).
|
||||
// When the stream does not carry a duration, the container duration (AVFormatContext.duration,
|
||||
// in AV_TIME_BASE units, the longest of all streams) is used.
|
||||
// Reference: https://ffmpeg.org/doxygen/trunk/structAVStream.html, https://ffmpeg.org/doxygen/trunk/structAVFormatContext.html
|
||||
Duration = stream->duration > 0
|
||||
? stream->duration * ffmpeg.av_q2d(stream->time_base)
|
||||
: _pFormatContext->duration > 0 ? _pFormatContext->duration / (double)ffmpeg.AV_TIME_BASE : 0;
|
||||
|
||||
// 9. Allocate frame and packet buffers
|
||||
// Reference: https://ffmpeg.org/doxygen/trunk/group__lavu__frame.html
|
||||
@@ -581,9 +666,11 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
_getFormatDelegate = GetFormat;
|
||||
_pCodecContext->get_format = _getFormatDelegate;
|
||||
|
||||
// Hardware decoding requires single-threaded decoding (thread_count = 1).
|
||||
// Multi-threading (thread_count = 0 / FF_THREAD_FRAME) is incompatible with fixed-function
|
||||
// GPU decoders and causes surface pool allocation exhaustion or crashes in get_format.
|
||||
// Hardware decoding runs with one decoder thread (thread_count = 1).
|
||||
// The GPU performs the decoding; the decoder thread only parses packets and submits them.
|
||||
// With frame threading, libavcodec adds one hardware surface per thread to the frame pool
|
||||
// (avcodec_get_hw_frames_parameters). The software fallback in GetFormat keeps this thread count.
|
||||
// Reference: https://github.com/FFmpeg/FFmpeg/blob/release/8.0/libavcodec/decode.c
|
||||
_pCodecContext->thread_count = 1;
|
||||
|
||||
// Attempt opening codec context with hardware configuration
|
||||
@@ -655,6 +742,8 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
/// <summary>
|
||||
/// Reads packets from the container until a packet belonging to the video stream is found,
|
||||
/// then sends it to the decoder via <c>avcodec_send_packet</c>.
|
||||
/// A packet that the decoder rejects as invalid data (<c>AVERROR_INVALIDDATA</c>, e.g. a damaged packet)
|
||||
/// is skipped and the next packet is read. Any other decoder error is thrown.
|
||||
/// Reference: https://ffmpeg.org/doxygen/trunk/group__lavc__encdec.html
|
||||
/// </summary>
|
||||
/// <returns>True if a video packet was sent; false on EOF.</returns>
|
||||
@@ -670,6 +759,18 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
{
|
||||
ret = ffmpeg.avcodec_send_packet(_pCodecContext, _pPacket);
|
||||
ffmpeg.av_packet_unref(_pPacket);
|
||||
|
||||
if (ret == ffmpeg.AVERROR_INVALIDDATA)
|
||||
{
|
||||
// The warning is logged for the first few packets only (a damaged file can have many).
|
||||
_skippedPacketCount++;
|
||||
if (_skippedPacketCount <= MaxLoggedSkippedPackets)
|
||||
{
|
||||
Melon<RenderMod>.Logger.Warning($"[FFmpegNativeDecoder] Skipped an invalid video packet ({_skippedPacketCount}): {GetFFmpegErrorString(ret)}");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
throw new Exception($"Error sending packet to decoder: {GetFFmpegErrorString(ret)}");
|
||||
@@ -743,12 +844,67 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lazily initializes or updates the <see cref="SwsContext"/> when source/destination formats or dimensions change.
|
||||
/// Selects the YUV→RGB conversion matrix (libswscale <c>SWS_CS_*</c>) from the colorspace tag of a frame.
|
||||
/// The matrix set is the same as that of the compute shader path (ColorConversion.compute, <c>ColorMatrix</c> uniform):
|
||||
/// BT.709 (also for frames without a usable tag), BT.601 and BT.2020, so both paths give the same colors.
|
||||
/// Reference: https://ffmpeg.org/doxygen/trunk/group__libsws.html (sws_getCoefficients)
|
||||
/// </summary>
|
||||
private static int GetSwsColorspace(AVColorSpace colorspace)
|
||||
{
|
||||
switch (GetColorMatrixId(colorspace))
|
||||
{
|
||||
case ColorMatrixBT601:
|
||||
return ffmpeg.SWS_CS_ITU601;
|
||||
case ColorMatrixBT2020:
|
||||
return ffmpeg.SWS_CS_BT2020;
|
||||
default:
|
||||
return ffmpeg.SWS_CS_ITU709;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true when the Y/U/V samples of the frame use the full range (0-255) instead of the limited range (16-235 / 16-240).
|
||||
/// The range is given by the frame's color_range tag or by a deprecated full range pixel format (YUVJ*).
|
||||
/// </summary>
|
||||
private static bool IsFullRange(AVFrame* frame)
|
||||
{
|
||||
if (frame->color_range == AVColorRange.AVCOL_RANGE_JPEG) return true;
|
||||
|
||||
switch ((AVPixelFormat)frame->format)
|
||||
{
|
||||
case AVPixelFormat.AV_PIX_FMT_YUVJ420P:
|
||||
case AVPixelFormat.AV_PIX_FMT_YUVJ422P:
|
||||
case AVPixelFormat.AV_PIX_FMT_YUVJ444P:
|
||||
case AVPixelFormat.AV_PIX_FMT_YUVJ440P:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lazily initializes or updates the <see cref="SwsContext"/> when source/destination formats, dimensions,
|
||||
/// or (for RGBA output) the colorspace / range of the source frame change.
|
||||
/// For RGBA output the YUV→RGB matrix and the source range are taken from <paramref name="colorFrame"/>.
|
||||
/// Reference: https://ffmpeg.org/doxygen/trunk/group__libsws.html
|
||||
/// </summary>
|
||||
private void InitOrUpdateSwsContext(AVPixelFormat srcFormat, int srcWidth, int srcHeight, AVPixelFormat dstFormat)
|
||||
/// <param name="srcFormat">Pixel format of the source frame.</param>
|
||||
/// <param name="srcWidth">Width of the source frame in pixels.</param>
|
||||
/// <param name="srcHeight">Height of the source frame in pixels.</param>
|
||||
/// <param name="dstFormat">Pixel format of the output.</param>
|
||||
/// <param name="colorFrame">Source frame whose colorspace / range tags are applied. Required for RGBA output.</param>
|
||||
private void InitOrUpdateSwsContext(AVPixelFormat srcFormat, int srcWidth, int srcHeight, AVPixelFormat dstFormat, AVFrame* colorFrame = null)
|
||||
{
|
||||
if (_pSwsContext != null && _srcPixFmt == srcFormat && _srcWidth == srcWidth && _srcHeight == srcHeight && _dstPixFmt == dstFormat)
|
||||
var swsColorspace = 0;
|
||||
var fullRange = false;
|
||||
if (colorFrame != null && dstFormat == AVPixelFormat.AV_PIX_FMT_RGBA)
|
||||
{
|
||||
swsColorspace = GetSwsColorspace(colorFrame->colorspace);
|
||||
fullRange = IsFullRange(colorFrame);
|
||||
}
|
||||
|
||||
if (_pSwsContext != null && _srcPixFmt == srcFormat && _srcWidth == srcWidth && _srcHeight == srcHeight && _dstPixFmt == dstFormat
|
||||
&& _srcSwsColorspace == swsColorspace && _srcFullRange == fullRange)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -763,6 +919,8 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
_srcWidth = srcWidth;
|
||||
_srcHeight = srcHeight;
|
||||
_dstPixFmt = dstFormat;
|
||||
_srcSwsColorspace = swsColorspace;
|
||||
_srcFullRange = fullRange;
|
||||
|
||||
_pSwsContext = ffmpeg.sws_getContext(
|
||||
srcWidth, srcHeight, srcFormat,
|
||||
@@ -774,6 +932,44 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
{
|
||||
throw new Exception($"Failed to initialize SwsContext (src: {srcFormat} {srcWidth}x{srcHeight} -> dst: {dstFormat} {Width}x{Height}).");
|
||||
}
|
||||
|
||||
if (swsColorspace != 0)
|
||||
{
|
||||
ApplySwsColorspace(swsColorspace, fullRange);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the YUV→RGB matrix and the source range of <see cref="_pSwsContext"/>.
|
||||
/// The destination range, brightness, contrast and saturation keep the values of the context.
|
||||
/// Reference: https://ffmpeg.org/doxygen/trunk/group__libsws.html (sws_setColorspaceDetails)
|
||||
/// </summary>
|
||||
/// <param name="swsColorspace">Matrix id (<c>SWS_CS_*</c>) of the source.</param>
|
||||
/// <param name="fullRange">True when the source uses the full range.</param>
|
||||
private void ApplySwsColorspace(int swsColorspace, bool fullRange)
|
||||
{
|
||||
int* currentInvTable;
|
||||
int* currentTable;
|
||||
int srcRange, dstRange, brightness, contrast, saturation;
|
||||
|
||||
// Returns a negative value when the context cannot change its colorspace details (e.g. YUV to YUV conversion).
|
||||
if (ffmpeg.sws_getColorspaceDetails(_pSwsContext, ¤tInvTable, &srcRange, ¤tTable, &dstRange,
|
||||
&brightness, &contrast, &saturation) < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var coefficients = ffmpeg.sws_getCoefficients(swsColorspace);
|
||||
var invTable = new int_array4();
|
||||
var table = new int_array4();
|
||||
for (uint i = 0; i < 4; i++)
|
||||
{
|
||||
invTable[i] = coefficients[i];
|
||||
table[i] = currentTable[i];
|
||||
}
|
||||
|
||||
ffmpeg.sws_setColorspaceDetails(_pSwsContext, invTable, fullRange ? 1 : 0, table, dstRange,
|
||||
brightness, contrast, saturation);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -865,7 +1061,7 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
if (frame == null) return false;
|
||||
|
||||
timestamp = GetTimestampAndUpdateIndex(frame);
|
||||
InitOrUpdateSwsContext((AVPixelFormat)frame->format, frame->width, frame->height, AVPixelFormat.AV_PIX_FMT_RGBA);
|
||||
InitOrUpdateSwsContext((AVPixelFormat)frame->format, frame->width, frame->height, AVPixelFormat.AV_PIX_FMT_RGBA, frame);
|
||||
|
||||
ClearConvertedFramePointers();
|
||||
if (vflip)
|
||||
@@ -929,7 +1125,7 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
/// </summary>
|
||||
/// <param name="yBuffer">Pointer to destination buffer for Y plane (Width * Height bytes).</param>
|
||||
/// <param name="yStride">Row stride in bytes for Y plane.</param>
|
||||
/// <param name="uvBuffer">Pointer to destination buffer for interleaved UV plane (Width * (Height / 2) bytes).</param>
|
||||
/// <param name="uvBuffer">Pointer to destination buffer for interleaved UV plane (((Width + 1) / 2) * 2 * ((Height + 1) / 2) bytes).</param>
|
||||
/// <param name="uvStride">Row stride in bytes for UV plane.</param>
|
||||
/// <param name="timestamp">Timestamp of the output frame in seconds.</param>
|
||||
/// <returns>True if a frame was successfully retrieved; false on EOF.</returns>
|
||||
@@ -982,7 +1178,7 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
int w = Width;
|
||||
int h = Height;
|
||||
int uvH = (h + 1) / 2;
|
||||
int uvRowBytes = (w / 2) * 2;
|
||||
int uvRowBytes = ((w + 1) / 2) * 2;
|
||||
|
||||
// Copy Y plane (row by row to safely skip linesize GPU padding)
|
||||
for (int y = 0; y < h; y++)
|
||||
@@ -1043,7 +1239,7 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
int w = Width;
|
||||
int h = Height;
|
||||
int uvH = (h + 1) / 2;
|
||||
int uvRowBytes = (w / 2) * 2;
|
||||
int uvRowBytes = ((w + 1) / 2) * 2;
|
||||
|
||||
if (yBuffer.Length < (long)w * h || uvBuffer.Length < (long)uvRowBytes * uvH)
|
||||
{
|
||||
@@ -1212,61 +1408,94 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
// ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Decodes and discards a specified number of frames immediately without GPU-to-CPU transfer overhead.
|
||||
/// Maximum distance in frames over which <see cref="SeekToTime"/> moves forward by decoding.
|
||||
/// Decoding forward needs no keyframe seek and no decoder flush, which is cheaper than seeking for short distances.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of frames to discard.</param>
|
||||
private void SkipFrames(int count)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (!_isOpened || _disposed) return;
|
||||
if (count <= 0) return;
|
||||
private const int MaxForwardDecodeFrames = 60;
|
||||
|
||||
int skipped = 0;
|
||||
if (_hasCachedFrame)
|
||||
/// <summary>
|
||||
/// Decodes forward from the current decoder position until the first frame whose presentation time is at least
|
||||
/// <paramref name="timeSeconds"/> minus half a frame, and keeps that frame in <see cref="_pCachedFrame"/>
|
||||
/// for the next <c>TryRead*</c> call.
|
||||
/// Frames before the target are discarded without GPU-to-host memory transfer; only the target frame is transferred.
|
||||
/// When the end of the stream is reached before the target, no frame is cached.
|
||||
/// </summary>
|
||||
/// <param name="timeSeconds">Target time in seconds.</param>
|
||||
private void DecodeToTarget(double timeSeconds)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
int ret = ffmpeg.avcodec_receive_frame(_pCodecContext, _pDecodedFrame);
|
||||
if (ret == 0)
|
||||
{
|
||||
ffmpeg.av_frame_unref(_pCachedFrame);
|
||||
_hasCachedFrame = false;
|
||||
skipped++;
|
||||
double currentPts = GetFrameTimestamp(_pDecodedFrame);
|
||||
|
||||
// Check with half-frame margin to accommodate floating point rounding
|
||||
if (currentPts >= timeSeconds - (0.5 / FPS))
|
||||
{
|
||||
// Cache the first matching frame in system RAM for immediate consumption
|
||||
if (_isHwDecoding && _pDecodedFrame->format == (int)_hwPixFmt)
|
||||
{
|
||||
ffmpeg.av_frame_unref(_pCachedFrame);
|
||||
int transferRet = ffmpeg.av_hwframe_transfer_data(_pCachedFrame, _pDecodedFrame, 0);
|
||||
if (transferRet < 0)
|
||||
{
|
||||
ffmpeg.av_frame_unref(_pDecodedFrame);
|
||||
throw new Exception($"Failed to transfer cached hardware frame during seek: {GetFFmpegErrorString(transferRet)}");
|
||||
}
|
||||
|
||||
ffmpeg.av_frame_copy_props(_pCachedFrame, _pDecodedFrame);
|
||||
ffmpeg.av_frame_unref(_pDecodedFrame);
|
||||
|
||||
_downloadedPixFmt = (AVPixelFormat)_pCachedFrame->format;
|
||||
}
|
||||
else
|
||||
{
|
||||
ffmpeg.av_frame_unref(_pCachedFrame);
|
||||
ffmpeg.av_frame_move_ref(_pCachedFrame, _pDecodedFrame);
|
||||
}
|
||||
|
||||
_hasCachedFrame = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Discard non-matching intermediate frame without transferring to CPU
|
||||
GetTimestampAndUpdateIndex(_pDecodedFrame);
|
||||
ffmpeg.av_frame_unref(_pDecodedFrame);
|
||||
continue;
|
||||
}
|
||||
|
||||
while (skipped < count)
|
||||
if (ret == ffmpeg.AVERROR(ffmpeg.EAGAIN))
|
||||
{
|
||||
int ret = ffmpeg.avcodec_receive_frame(_pCodecContext, _pDecodedFrame);
|
||||
if (ret == 0)
|
||||
{
|
||||
GetTimestampAndUpdateIndex(_pDecodedFrame);
|
||||
ffmpeg.av_frame_unref(_pDecodedFrame);
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
if (_isDraining) return;
|
||||
|
||||
if (ret == ffmpeg.AVERROR(ffmpeg.EAGAIN))
|
||||
if (!ReadAndSendPacket())
|
||||
{
|
||||
if (_isDraining) break;
|
||||
|
||||
if (!ReadAndSendPacket())
|
||||
{
|
||||
_isDraining = true;
|
||||
ffmpeg.avcodec_send_packet(_pCodecContext, null);
|
||||
}
|
||||
}
|
||||
else if (ret == ffmpeg.AVERROR_EOF)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Error skipping frames: {GetFFmpegErrorString(ret)}");
|
||||
_isDraining = true;
|
||||
ffmpeg.avcodec_send_packet(_pCodecContext, null);
|
||||
}
|
||||
}
|
||||
else if (ret == ffmpeg.AVERROR_EOF)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Error during seek decoding: {GetFFmpegErrorString(ret)}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeks to a specific playback time in seconds using backward keyframe seek followed by forward decode.
|
||||
/// Intermediate frames are discarded without GPU-to-host memory transfer to maximize seek speed;
|
||||
/// only the target frame is transferred and preserved in <see cref="_pCachedFrame"/>.
|
||||
/// Moves the playback position to a time in seconds.
|
||||
/// The next <c>TryRead*</c> call returns the first frame whose presentation time is at least
|
||||
/// <paramref name="timeSeconds"/> minus half a frame (the frame nearest to the target time).
|
||||
/// Both paths below return that same frame:
|
||||
/// - Forward decode: the target is ahead of the current position by less than <see cref="MaxForwardDecodeFrames"/> frames.
|
||||
/// Frames are decoded and discarded up to the target.
|
||||
/// - Keyframe seek: all other cases (target before the current position, far ahead, or the target frame already read).
|
||||
/// A backward keyframe seek is followed by a forward decode up to the target.
|
||||
/// The target frame is transferred and preserved in <see cref="_pCachedFrame"/>; when it is already there, nothing is decoded.
|
||||
/// </summary>
|
||||
/// <param name="timeSeconds">Target time in seconds to seek to.</param>
|
||||
public void SeekToTime(double timeSeconds)
|
||||
@@ -1276,13 +1505,38 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
if (!_isOpened || _disposed) return;
|
||||
|
||||
if (timeSeconds < 0) timeSeconds = 0;
|
||||
double targetFrameIndex = timeSeconds * FPS;
|
||||
|
||||
// If already at target index or very close future, skip frames instead of seek to avoid I-frame seek latency
|
||||
bool useSkip = targetFrameIndex >= _currentFrameIndex && targetFrameIndex - _currentFrameIndex < 60;
|
||||
if (useSkip)
|
||||
// Earliest presentation time that satisfies the target (target minus half a frame).
|
||||
double threshold = timeSeconds - (0.5 / FPS);
|
||||
|
||||
// Index of the first frame that satisfies the target. The epsilon keeps a frame that lies exactly on the
|
||||
// margin from being excluded by floating point rounding.
|
||||
double targetFrameIndex = Math.Ceiling(timeSeconds * FPS - 0.5 - 1e-6);
|
||||
|
||||
if (_hasCachedFrame)
|
||||
{
|
||||
SkipFrames((int)(targetFrameIndex - _currentFrameIndex));
|
||||
// A frame is already positioned for the next read (e.g. by a previous SeekToTime call).
|
||||
double cachedPts = GetFrameTimestamp(_pCachedFrame);
|
||||
if (cachedPts >= threshold)
|
||||
{
|
||||
// The cached frame is the first frame that satisfies the target.
|
||||
// When the target lies before it, the frames in between are no longer available and a keyframe seek is required.
|
||||
if (cachedPts - threshold < 1.0 / FPS) return;
|
||||
}
|
||||
else if (targetFrameIndex - _currentFrameIndex < MaxForwardDecodeFrames)
|
||||
{
|
||||
// The cached frame lies before the target: drop it and continue decoding forward.
|
||||
GetTimestampAndUpdateIndex(_pCachedFrame);
|
||||
ffmpeg.av_frame_unref(_pCachedFrame);
|
||||
_hasCachedFrame = false;
|
||||
DecodeToTarget(timeSeconds);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (targetFrameIndex > _currentFrameIndex && targetFrameIndex - _currentFrameIndex < MaxForwardDecodeFrames)
|
||||
{
|
||||
// The target frame has not been read yet and is close: decode forward instead of seeking.
|
||||
DecodeToTarget(timeSeconds);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1310,77 +1564,7 @@ internal sealed unsafe class FFmpegNativeDecoder : IDisposable
|
||||
_isDraining = false;
|
||||
|
||||
// Decode frames until reaching the target presentation timestamp
|
||||
while (true)
|
||||
{
|
||||
ret = ffmpeg.avcodec_receive_frame(_pCodecContext, _pDecodedFrame);
|
||||
if (ret == 0)
|
||||
{
|
||||
double currentPts = GetFrameTimestamp(_pDecodedFrame);
|
||||
|
||||
// Check with half-frame margin to accommodate floating point rounding
|
||||
if (currentPts >= timeSeconds - (0.5 / FPS))
|
||||
{
|
||||
// Cache the first matching frame in system RAM for immediate consumption
|
||||
if (_isHwDecoding && _pDecodedFrame->format == (int)_hwPixFmt)
|
||||
{
|
||||
ffmpeg.av_frame_unref(_pCachedFrame);
|
||||
int transferRet = ffmpeg.av_hwframe_transfer_data(_pCachedFrame, _pDecodedFrame, 0);
|
||||
if (transferRet < 0)
|
||||
{
|
||||
ffmpeg.av_frame_unref(_pDecodedFrame);
|
||||
throw new Exception($"Failed to transfer cached hardware frame during seek: {GetFFmpegErrorString(transferRet)}");
|
||||
}
|
||||
|
||||
ffmpeg.av_frame_copy_props(_pCachedFrame, _pDecodedFrame);
|
||||
ffmpeg.av_frame_unref(_pDecodedFrame);
|
||||
|
||||
_downloadedPixFmt = (AVPixelFormat)_pCachedFrame->format;
|
||||
}
|
||||
else
|
||||
{
|
||||
ffmpeg.av_frame_unref(_pCachedFrame);
|
||||
ffmpeg.av_frame_move_ref(_pCachedFrame, _pDecodedFrame);
|
||||
}
|
||||
|
||||
_hasCachedFrame = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// Discard non-matching intermediate frame without transferring to CPU
|
||||
long ptsValue = _pDecodedFrame->best_effort_timestamp != ffmpeg.AV_NOPTS_VALUE
|
||||
? _pDecodedFrame->best_effort_timestamp
|
||||
: _pDecodedFrame->pts;
|
||||
if (ptsValue == ffmpeg.AV_NOPTS_VALUE)
|
||||
{
|
||||
_currentFrameIndex++;
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentFrameIndex = (long)Math.Round(currentPts * FPS);
|
||||
}
|
||||
ffmpeg.av_frame_unref(_pDecodedFrame);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ret == ffmpeg.AVERROR(ffmpeg.EAGAIN))
|
||||
{
|
||||
if (_isDraining) break;
|
||||
|
||||
if (!ReadAndSendPacket())
|
||||
{
|
||||
_isDraining = true;
|
||||
ffmpeg.avcodec_send_packet(_pCodecContext, null);
|
||||
}
|
||||
}
|
||||
else if (ret == ffmpeg.AVERROR_EOF)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Error during seek decoding: {GetFFmpegErrorString(ret)}");
|
||||
}
|
||||
}
|
||||
DecodeToTarget(timeSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+535
-190
File diff suppressed because it is too large.
Load diff
+42
-5
@@ -73,6 +73,16 @@ namespace ADOFAIRenderer.Util
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bytes of the readback buffer of one frame: Width x Height x 3 / 2 when the GPU conversion to NV12 / YUV420P
|
||||
/// is used, Width x Height x 4 (RGBA) otherwise. The same size as ReadbackRequestState.Setup uses.
|
||||
/// </summary>
|
||||
public long GetFrameBufferBytes(int width, int height)
|
||||
{
|
||||
var pixels = (long)width * height;
|
||||
return _useGPUConversion ? pixels * 3 / 2 : pixels * 4;
|
||||
}
|
||||
|
||||
public void RequestFrameCapture(Texture texture, long currentFrame)
|
||||
{
|
||||
if (!RenderManager.IsCapturingActive) return;
|
||||
@@ -154,14 +164,11 @@ namespace ADOFAIRenderer.Util
|
||||
}
|
||||
Melon<RenderMod>.Logger.Warning($"[OOM Prevent] GPU Readback failed for frame {frame}. Queue deadlock prevented by sending a black frame.");
|
||||
|
||||
var byteSize = state.Width * state.Height * 3 / 2;
|
||||
var blackFrame = ArrayPool<byte>.Shared.Rent(byteSize);
|
||||
Array.Clear(blackFrame, 0, byteSize);
|
||||
|
||||
var blackFrame = CreateBlackFrame(state, out var byteSize);
|
||||
var capFrame = new RenderManager.CapturedFrame
|
||||
{
|
||||
ManagedBuffer = blackFrame, Length = byteSize,
|
||||
IsColorConverted = true
|
||||
IsColorConverted = state.IsGPUConversion
|
||||
};
|
||||
PushToQueue(frame, capFrame);
|
||||
RecycleState(state);
|
||||
@@ -190,6 +197,36 @@ namespace ADOFAIRenderer.Util
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a black frame in the same format as the frames of this reader, from ArrayPool<byte>.Shared
|
||||
/// (returned to the pool by the render thread after encoding).
|
||||
///
|
||||
/// GPU conversion (NV12 / YUV420P, full range): Width × Height × 3 / 2 bytes.
|
||||
/// Y plane (Width × Height bytes) = 0, U/V data (the rest) = 128.
|
||||
/// 128 is the zero chroma value of the 8-bit full range: the U/V offset 0.5 of RGB2YUV_BT709_Full
|
||||
/// in ColorConversion.compute, which the encoder input of this path uses.
|
||||
/// Both NV12 (interleaved UV) and YUV420P (U plane, then V plane) store all chroma after the Y plane,
|
||||
/// so the same fill applies to both.
|
||||
/// No GPU conversion (RGBA input, converted by the encoder): Width × Height × 4 bytes, all 0.
|
||||
/// </summary>
|
||||
private static byte[] CreateBlackFrame(ReadbackRequestState state, out int byteSize)
|
||||
{
|
||||
var lumaSize = state.Width * state.Height;
|
||||
byteSize = state.IsGPUConversion ? lumaSize * 3 / 2 : lumaSize * 4;
|
||||
|
||||
var frame = ArrayPool<byte>.Shared.Rent(byteSize);
|
||||
if (state.IsGPUConversion)
|
||||
{
|
||||
Array.Clear(frame, 0, lumaSize);
|
||||
frame.AsSpan(lumaSize, byteSize - lumaSize).Fill(128);
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Clear(frame, 0, byteSize);
|
||||
}
|
||||
return frame;
|
||||
}
|
||||
|
||||
internal void RecycleState(ReadbackRequestState state)
|
||||
{
|
||||
lock (_poolLock)
|
||||
|
||||
@@ -9,8 +9,12 @@ namespace ADOFAIRenderer.Util.OS;
|
||||
/// <summary>
|
||||
/// Sends native desktop notifications on Windows, macOS, and Linux.
|
||||
/// <para>
|
||||
/// - <b>Windows</b>: Uses Win32 <c>Shell_NotifyIcon</c> (system tray balloon) via P/Invoke.
|
||||
/// No PowerShell, no UWP, no WinRT dependencies.
|
||||
/// - <b>Windows</b>: Toast notification shown by a hidden <c>powershell.exe</c> process
|
||||
/// (WinRT <c>ToastNotificationManager</c>, script passed with <c>-EncodedCommand</c>).
|
||||
/// With a folder path, the script also registers the <c>adofai-renderer://</c> URL protocol under
|
||||
/// <c>HKCU\Software\Classes\adofai-renderer</c> (open command: <c>explorer.exe "<folder>"</c>)
|
||||
/// so the toast button opens the folder; the key is overwritten on each notification and stays registered.
|
||||
/// When PowerShell fails, a Win32 <c>Shell_NotifyIcon</c> balloon is shown via P/Invoke.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// - <b>macOS</b>: Uses <c>osascript</c> with AppleScript's <c>display notification</c>.
|
||||
@@ -88,7 +92,9 @@ internal static class OSNotification
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to show a Windows Toast notification via PowerShell.
|
||||
/// Returns true if the process launched successfully, false otherwise.
|
||||
/// With a folder path, registers the adofai-renderer:// protocol in HKCU first (see class summary).
|
||||
/// Waits up to 5 seconds for the process. Returns true when it exits with code 0,
|
||||
/// false when it exits with another code, does not exit within 5 seconds, or fails to start.
|
||||
/// </summary>
|
||||
private static bool TrySendToast(string title, string message, string? folderPath)
|
||||
{
|
||||
|
||||
+14
-3
@@ -20,6 +20,7 @@ internal class VideoBGPlayer : MonoBehaviour
|
||||
private static readonly int UTex = Shader.PropertyToID("_UTex");
|
||||
private static readonly int VTex = Shader.PropertyToID("_VTex");
|
||||
private static readonly int ColorRange = Shader.PropertyToID("ColorRange");
|
||||
private static readonly int ColorMatrix = Shader.PropertyToID("ColorMatrix");
|
||||
|
||||
[SuppressMessage("ReSharper", "InconsistentNaming")]
|
||||
private enum GPUMode
|
||||
@@ -96,7 +97,7 @@ internal class VideoBGPlayer : MonoBehaviour
|
||||
_kernelIndex = _computeShader.FindKernel("CSMain_NV12ToRGBA");
|
||||
|
||||
_textureY = new Texture2D(w, h, TextureFormat.R8, false);
|
||||
_textureUV = new Texture2D(w / 2, h / 2, TextureFormat.RG16, false);
|
||||
_textureUV = new Texture2D((w + 1) / 2, (h + 1) / 2, TextureFormat.RG16, false);
|
||||
ConfigureTexture(_textureY);
|
||||
ConfigureTexture(_textureUV);
|
||||
|
||||
@@ -117,8 +118,8 @@ internal class VideoBGPlayer : MonoBehaviour
|
||||
_kernelIndex = _computeShader.FindKernel("CSMain_YUV420PToRGBA");
|
||||
|
||||
_textureY = new Texture2D(w, h, TextureFormat.R8, false);
|
||||
_textureU = new Texture2D(w / 2, h / 2, TextureFormat.R8, false);
|
||||
_textureV = new Texture2D(w / 2, h / 2, TextureFormat.R8, false);
|
||||
_textureU = new Texture2D((w + 1) / 2, (h + 1) / 2, TextureFormat.R8, false);
|
||||
_textureV = new Texture2D((w + 1) / 2, (h + 1) / 2, TextureFormat.R8, false);
|
||||
ConfigureTexture(_textureY);
|
||||
ConfigureTexture(_textureU);
|
||||
ConfigureTexture(_textureV);
|
||||
@@ -189,6 +190,14 @@ internal class VideoBGPlayer : MonoBehaviour
|
||||
var vidOffset = scrVfxPlus.instance ? scrVfxPlus.instance.vidOffset : 0.0;
|
||||
var targetVideoTime = songTime + vidOffset;
|
||||
|
||||
// When looping is enabled, the playback position is the elapsed time wrapped into [0, Duration).
|
||||
// When it is disabled, the position keeps increasing and the last frame stays on screen after the end.
|
||||
var videoDuration = _decoder.Duration;
|
||||
if (_isLooping && targetVideoTime >= 0 && videoDuration > 0)
|
||||
{
|
||||
targetVideoTime %= videoDuration;
|
||||
}
|
||||
|
||||
if (targetVideoTime < 0)
|
||||
{
|
||||
if (_targetTexture)
|
||||
@@ -307,6 +316,7 @@ internal class VideoBGPlayer : MonoBehaviour
|
||||
_computeShader.SetInt(Width, w);
|
||||
_computeShader.SetInt(Height, h);
|
||||
_computeShader.SetInt(ColorRange, _decoder.ColorRange == AVColorRange.AVCOL_RANGE_JPEG ? 1 : 0);
|
||||
_computeShader.SetInt(ColorMatrix, _decoder.ColorMatrix);
|
||||
|
||||
int threadGroupsX = (w + 7) / 8;
|
||||
int threadGroupsY = (h + 7) / 8;
|
||||
@@ -341,6 +351,7 @@ internal class VideoBGPlayer : MonoBehaviour
|
||||
_computeShader.SetInt(Width, w);
|
||||
_computeShader.SetInt(Height, h);
|
||||
_computeShader.SetInt(ColorRange, _decoder.ColorRange == AVColorRange.AVCOL_RANGE_JPEG ? 1 : 0);
|
||||
_computeShader.SetInt(ColorMatrix, _decoder.ColorMatrix);
|
||||
|
||||
int threadGroupsX = (w + 7) / 8;
|
||||
int threadGroupsY = (h + 7) / 8;
|
||||
|
||||
Reference in new issue
Block a user