using Titano.Core.Analysis; using Titano.Core.Archiving; using Titano.Core.Diagnostics; using Titano.Core.Metadata; using Titano.Core.Model; namespace Titano.Data.Processing; /// /// 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). /// internal sealed record SequenceFrames(Sequence Sequence, IReadOnlyList Frames, IReadOnlyList Files) { /// Flags of frames that never reach a render. public const FrameFlags NotRendered = FrameFlags.Missing | FrameFlags.Corrupt | FrameFlags.Excluded | FrameFlags.Undecodable; /// Flags of frames whose content is not analysed (absent, or not the file that was archived). public const FrameFlags NotAnalysed = FrameFlags.Missing | FrameFlags.Corrupt; public int Count => Frames.Count; /// Every frame row of with its archive file (the proxy when asked and built). public static async Task 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> ListAsync(TitanoDatabase db, Guid sequenceId, CancellationToken cancellationToken) { const int pageSize = 1000; var frames = new List(); 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; } } /// /// 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. /// public IReadOnlyList 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; } /// Positions of the frames a render must skip besides the project's exclusions. public IEnumerable 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; } }