perf(video): encode image-sequence frames concurrently

Frames are independent, so up to one per core is encoded at once with
single-threaded codecs; PNG sequences no longer run Deflate on one core
(1080p PNG export 1.0 to 2.5 fps on 4 cores). Failures surface on a later
frame or on Complete, and discarding waits for every write first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 11:27:36 +00:00
1 parent 6a1eca0d6c
commit 0bd19ebeb1
2 files changed
+65 -12

No files matched your search

@@ -203,7 +203,10 @@ internal sealed class ApngSession : SingleFileSession
/// <summary>
/// Numbered image files (<c>&lt;folder name&gt;_000001.ext</c>, 1-based, six digits) in the output folder. Files are written
/// (and flushed) into a hidden temporary sibling folder that is renamed to the output folder on completion.
/// (and flushed) into a hidden temporary sibling folder that is renamed to the output folder on completion. Frames are
/// independent, so several are encoded at once (each single-threaded, at most one per core): PNG's Deflate would
/// otherwise run on one core. A failed encode surfaces on a later <see cref="IVideoEncoderSession.WriteFrame"/> or on
/// <see cref="IVideoEncoderSession.Complete"/>.
/// </summary>
internal sealed class ImageSequenceSession : ManagedSession
{
@@ -212,6 +215,8 @@ internal sealed class ImageSequenceSession : ManagedSession
private readonly string _stem;
private readonly string _extension;
private readonly ConstantRateClock _clock;
private readonly Queue<Task> _inFlight = new();
private readonly int _maxInFlight = int.Clamp(Environment.ProcessorCount, 1, 8);
private int _index;
public ImageSequenceSession(EncodeSettings settings, string encoderName) : base(settings, encoderName)
@@ -235,20 +240,43 @@ internal sealed class ImageSequenceSession : ManagedSession
{
var repeats = _clock.Advance(duration90k);
if (repeats == 0) return;
var bytes = Encode(frame);
for (var i = 0; i < repeats; i++)
while (_inFlight.Count >= _maxInFlight) _inFlight.Dequeue().GetAwaiter().GetResult();
var first = _index + 1;
_index += repeats;
var copy = frame.Clone();
_inFlight.Enqueue(Task.Run(() =>
{
_index++;
using var file = new FileStream(Path.Combine(_temp, FrameName(_stem, _index, _extension)), FileMode.CreateNew, FileAccess.Write, FileShare.None, 1 << 16);
file.Write(bytes);
file.Flush(flushToDisk: true);
}
byte[] bytes;
using (copy) bytes = Encode(copy);
for (var number = first; number < first + repeats; number++)
{
using var file = new FileStream(Path.Combine(_temp, FrameName(_stem, number, _extension)), FileMode.CreateNew, FileAccess.Write, FileShare.None, 1 << 16);
file.Write(bytes);
file.Flush(flushToDisk: true);
}
}));
}
protected override void Commit() => Directory.Move(_temp, _folder);
protected override void Commit()
{
while (_inFlight.Count > 0) _inFlight.Dequeue().GetAwaiter().GetResult();
Directory.Move(_temp, _folder);
}
protected override void Discard()
{
// Every encode must have stopped writing before the folder goes; their failures are already superseded.
while (_inFlight.Count > 0)
{
try
{
_inFlight.Dequeue().GetAwaiter().GetResult();
}
catch (Exception ex)
{
Log.Warning(LogCategory, "A sequence frame failed while the encode was being discarded", ex);
}
}
try
{
Directory.Delete(_temp, recursive: true);
@@ -259,13 +287,15 @@ internal sealed class ImageSequenceSession : ManagedSession
}
}
/// <summary>One frame, single-threaded (frames are encoded concurrently).</summary>
private byte[] Encode(ImageBuffer frame) => Settings.Codec switch
{
VideoCodec.JpegSequence => JpegEncoder.Encode(frame, JpegOptions(Settings.Quality)),
VideoCodec.JpegSequence => JpegEncoder.Encode(frame, JpegOptions(Settings.Quality) with { MaxDegreeOfParallelism = 1 }),
VideoCodec.PngSequence => PngEncoder.Encode(frame, Settings.BitDepth == 16 ? 16 : 8, CompressionLevel.Optimal),
VideoCodec.Tiff16Sequence => TiffWriter.Write(frame, new TiffWriteOptions { BitsPerSample = Settings.BitDepth == 8 ? 8 : 16 }),
VideoCodec.Tiff16Sequence => TiffWriter.Write(frame, new TiffWriteOptions { BitsPerSample = Settings.BitDepth == 8 ? 8 : 16, MaxDegreeOfParallelism = 1 }),
// Tiles no larger than the frame: edge padding of a 256-pixel tile would dominate small outputs.
VideoCodec.LinearDngSequence => LinearDngWriter.Write(frame, new LinearDngOptions { TileSize = int.Clamp((int.Max(frame.Width, frame.Height) + 15) / 16 * 16, 16, 256) }),
VideoCodec.LinearDngSequence => LinearDngWriter.Write(frame,
new LinearDngOptions { TileSize = int.Clamp((int.Max(frame.Width, frame.Height) + 15) / 16 * 16, 16, 256), MaxDegreeOfParallelism = 1 }),
_ => throw new NotSupportedException($"{Settings.Codec} is not an image sequence"),
};
}
@@ -165,6 +165,29 @@ public sealed class ManagedEncoderTests
Assert.Empty(Directory.GetFileSystemEntries(dir));
}
[Test]
public void SequenceWriteFailuresSurfaceAndLeaveNothing()
{
var dir = _context.CreateTempDirectory();
var session = new ManagedEncoderProvider().Open(Settings(Path.Combine(dir, "seq"), VideoCodec.PngSequence));
using var frame = Frame(0);
session.WriteFrame(frame, 3000);
Directory.Delete(Directory.GetDirectories(dir, ".seq.*.tmp").Single(), recursive: true);
Exception? error = null;
try
{
for (var i = 0; i < 20; i++) session.WriteFrame(frame, 3000);
session.Complete();
}
catch (Exception ex)
{
error = ex;
}
session.Dispose();
Assert.True(error is IOException, $"unexpected {error?.GetType().Name}");
Assert.Empty(Directory.GetFileSystemEntries(dir));
}
[Test]
public void SessionsValidateTheirInput()
{