diff --git a/src/Titano.Core/Video/Managed/ManagedSessions.cs b/src/Titano.Core/Video/Managed/ManagedSessions.cs index d46a8e5d..36efb605 100644 --- a/src/Titano.Core/Video/Managed/ManagedSessions.cs +++ b/src/Titano.Core/Video/Managed/ManagedSessions.cs @@ -203,7 +203,10 @@ internal sealed class ApngSession : SingleFileSession /// /// Numbered image files (<folder name>_000001.ext, 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 or on +/// . /// 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 _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 } } + /// One frame, single-threaded (frames are encoded concurrently). 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"), }; } diff --git a/tests/Titano.Core.Tests/Export/ManagedEncoderTests.cs b/tests/Titano.Core.Tests/Export/ManagedEncoderTests.cs index 5e901acc..7e293f58 100644 --- a/tests/Titano.Core.Tests/Export/ManagedEncoderTests.cs +++ b/tests/Titano.Core.Tests/Export/ManagedEncoderTests.cs @@ -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() {