Files
Chronos/src/Titano.Data/Processing/SequenceFrames.cs
T

105 lines
4.3 KiB
C#

using Titano.Core.Analysis;
using Titano.Core.Archiving;
using Titano.Core.Diagnostics;
using Titano.Core.Metadata;
using Titano.Core.Model;
namespace Titano.Data.Processing;
/// <summary>
/// One sequence as the processing jobs read it: the frame rows in index order and the file to decode for each (an
/// archive original or proxy, or a render node's cached copy; null when there is none).
/// </summary>
internal sealed record SequenceFrames(Sequence Sequence, IReadOnlyList<Frame> Frames, IReadOnlyList<string?> Files)
{
/// <summary>Flags of frames that never reach a render.</summary>
public const FrameFlags NotRendered = FrameFlags.Missing | FrameFlags.Corrupt | FrameFlags.Excluded | FrameFlags.Undecodable;
/// <summary>Flags of frames whose content is not analysed (absent, or not the file that was archived).</summary>
public const FrameFlags NotAnalysed = FrameFlags.Missing | FrameFlags.Corrupt;
public int Count => Frames.Count;
/// <summary>Every frame row of <paramref name="sequence"/> with its archive file (the proxy when asked and built).</summary>
public static async Task<SequenceFrames> LoadAsync(TitanoDatabase db, Archive archive, Sequence sequence, bool useProxies,
CancellationToken cancellationToken)
{
var frames = await ListAsync(db, sequence.Id, cancellationToken).ConfigureAwait(false);
var paths = new ArchivePaths(archive.RootPath);
var folder = paths.Sequence(sequence.Folder);
var files = new string?[frames.Count];
for (var i = 0; i < files.Length; i++)
{
var frame = frames[i];
if ((frame.Flags & FrameFlags.Missing) != 0) continue;
var proxy = useProxies ? folder.Proxy(frame.Index) : null;
files[i] = proxy is not null && File.Exists(proxy) ? proxy : paths.FromRelative(frame.RelativePath);
}
return new SequenceFrames(sequence, frames, files);
}
public static async Task<List<Frame>> ListAsync(TitanoDatabase db, Guid sequenceId, CancellationToken cancellationToken)
{
const int pageSize = 1000;
var frames = new List<Frame>();
while (true)
{
var after = frames.Count > 0 ? frames[^1].Index : -1;
var page = await db.Frames.ListBySequenceAsync(sequenceId, after, pageSize, cancellationToken).ConfigureAwait(false);
frames.AddRange(page);
if (page.Count < pageSize) return frames;
}
}
/// <summary>
/// Light-analysis input (BRIEF §10.1): every frame's metadata with the sequence's camera and lens. The index keeps
/// no GPS, so the first readable file's fix is read once: a time-lapse camera stays put, and the analysis applies
/// that position (and the clock offset it implies) to the whole sequence.
/// </summary>
public IReadOnlyList<LightFrame> LightFrames()
{
var s = Sequence;
var light = new LightFrame[Frames.Count];
for (var i = 0; i < light.Length; i++)
{
light[i] = LightFrame.From(Frames[i]) with
{
Index = i,
CameraMake = s.CameraMake,
CameraModel = s.CameraModel,
CameraSerial = s.CameraSerial,
Lens = s.Lens,
};
}
if (FirstGpsFix() is var (index, gps)) light[index] = light[index] with { Latitude = gps.Latitude, Longitude = gps.Longitude, GpsUtc = gps.UtcTimestamp };
return light;
}
/// <summary>Positions of the frames a render must skip besides the project's exclusions.</summary>
public IEnumerable<int> NotRenderable()
{
for (var i = 0; i < Frames.Count; i++)
{
if (Files[i] is null || (Frames[i].Flags & NotRendered) != 0) yield return i;
}
}
private (int Index, GpsPosition Gps)? FirstGpsFix()
{
for (var i = 0; i < Files.Count; i++)
{
if (Files[i] is not { } file || (Frames[i].Flags & NotAnalysed) != 0) continue;
try
{
return MetadataReader.Read(file).Gps is { } gps ? (i, gps) : null;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning("processing", $"Metadata of {file} could not be read", ex);
return null;
}
}
return null;
}
}