Files
Chronos/src/Titano.Core/Project/ProjectSettings.cs
T

229 lines
8.4 KiB
C#

using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
namespace Titano.Core.Project;
public enum TimingMode
{
/// <summary>Every source frame lasts one output frame.</summary>
Constant,
/// <summary>Output time follows capture time (deliberate intervalometer pauses are kept short, gaps preserved).</summary>
Adaptive,
/// <summary>Missing frames in sparse gaps are synthesised from motion.</summary>
Interpolated,
/// <summary>A monotone curve maps output time to source position (speed ramps).</summary>
Remapped,
}
public enum RegionMode
{
Off,
Auto,
Horizon,
Luminance,
}
public enum FlowQualitySetting
{
Draft,
Balanced,
Best,
}
public enum StackSetting
{
None,
Median,
Mean,
StarTrails,
}
/// <summary>A control point of the time-remap curve: output fraction → source fraction (both 0..1).</summary>
public sealed record CurvePoint(double X, double Y);
/// <summary>Virtual-camera keyframe in normalised coordinates.</summary>
public sealed record CameraKey(int Frame, double CenterX, double CenterY, double Zoom, double Rotation, string Easing = "EaseInOut");
public sealed record TimingSettings
{
public Auto<TimingMode> Mode { get; init; } = new();
/// <summary>Output frame rate as "num/den" or an integer (e.g. "24000/1001", "30").</summary>
public Auto<string> FrameRate { get; init; } = new();
/// <summary>When set, the plan is scaled so the video lasts this many seconds.</summary>
public double? FitToSeconds { get; init; }
public IReadOnlyList<CurvePoint> RemapCurve { get; init; } = [new(0, 0), new(1, 1)];
}
public sealed record ExposureSettings
{
public Auto<bool> Deflicker { get; init; } = new();
/// <summary>Local-regression window in frames (odd).</summary>
public Auto<int> DeflickerWindow { get; init; } = new();
/// <summary>0..1: share of the flicker removed.</summary>
public Auto<double> DeflickerStrength { get; init; } = new();
public Auto<RegionMode> Regions { get; init; } = new();
public Auto<bool> HolyGrail { get; init; } = new();
/// <summary>Transition length in frames for redistributed exposure steps.</summary>
public Auto<int> TransitionLength { get; init; } = new();
public Auto<bool> WhiteBalanceSmoothing { get; init; } = new();
/// <summary>Global exposure offset in EV applied after correction.</summary>
public double ExposureOffsetEv { get; init; }
/// <summary>Soft highlight knee (linear), null = hard clip at the encoder.</summary>
public double? HighlightKnee { get; init; } = 0.9;
}
public sealed record MotionSettings
{
public Auto<bool> MotionBlur { get; init; } = new();
/// <summary>Target shutter angle in degrees (180 = film look).</summary>
public Auto<double> TargetShutterAngle { get; init; } = new();
public Auto<FlowQualitySetting> FlowQuality { get; init; } = new();
public Auto<bool> Stabilize { get; init; } = new();
/// <summary>Path smoothing in frames (Gaussian sigma).</summary>
public Auto<double> StabilizeSmoothing { get; init; } = new();
}
public sealed record CameraSettings
{
/// <summary>Normalised crop (0..1) or null for the full frame.</summary>
public double[]? Crop { get; init; }
/// <summary>"source", "16:9", "21:9", "4:3", "3:2", "1:1", "9:16", "4:5" or "w:h".</summary>
public Auto<string> Aspect { get; init; } = new();
public double OffsetX { get; init; }
public double OffsetY { get; init; }
public IReadOnlyList<CameraKey> Keyframes { get; init; } = [];
}
public sealed record StackSettings
{
public StackSetting Mode { get; init; } = StackSetting.None;
/// <summary>Window (median/mean) or trail length (star trails) in frames.</summary>
public int Length { get; init; } = 9;
}
/// <summary>Values of an export; null fields take the preset's value (BRIEF §11.4: editing any value makes it "Custom").</summary>
public sealed record ExportOverrides
{
public string? Codec { get; init; }
public string? Container { get; init; }
public string? Profile { get; init; }
public int? BitDepth { get; init; }
public int? Width { get; init; }
public int? Height { get; init; }
public string? FrameRate { get; init; }
public long? BitRate { get; init; }
public int? GopFrames { get; init; }
public int? BFrames { get; init; }
public int? Quality { get; init; }
public string? RateControl { get; init; }
public string? Encoder { get; init; }
}
public sealed record ExportSettings
{
/// <summary>Id of the selected preset (built-in or user); "custom" when values differ from every preset.</summary>
public Auto<string> Preset { get; init; } = new();
public ExportOverrides Overrides { get; init; } = new();
/// <summary>Output file name template (tokens: date, title, preset, width, height, fps, codec).</summary>
public string NameTemplate { get; init; } = "{date} {title} - {preset}";
/// <summary>Output folder; null = the sequence's exports folder.</summary>
public string? Folder { get; init; }
/// <summary>Presets queued together by "Render another preset" / batch.</summary>
public IReadOnlyList<string> Batch { get; init; } = [];
}
/// <summary>
/// The "project" of a sequence (<c>.titano/project.json</c>, BRIEF §7): every render decision. Originals are
/// never modified; all edits live here.
/// </summary>
public sealed record ProjectSettings
{
public const int CurrentVersion = 1;
public int Version { get; init; } = CurrentVersion;
public TimingSettings Timing { get; init; } = new();
public ExposureSettings Exposure { get; init; } = new();
public MotionSettings Motion { get; init; } = new();
public CameraSettings Camera { get; init; } = new();
public StackSettings Stack { get; init; } = new();
public ExportSettings Export { get; init; } = new();
/// <summary>Frame indices excluded from renders.</summary>
public IReadOnlyList<int> ExcludedFrames { get; init; } = [];
/// <summary>Index of the hero frame (sequence thumbnail).</summary>
public int? HeroFrame { get; init; }
public string ToJson() => JsonSerializer.Serialize(this, ProjectJson.Default.ProjectSettings);
/// <summary>
/// Reads a project; members missing from <paramref name="json"/> (an API client's partial settings, an older file)
/// keep their defaults. The JSON is laid over the default project first, because generated deserialisation leaves
/// missing init-only members null or false rather than at their initial values.
/// </summary>
/// <exception cref="JsonException">The text is not a JSON object.</exception>
/// <exception cref="InvalidDataException">The project was written by a newer Titano.</exception>
public static ProjectSettings FromJson(string json)
{
var options = new JsonNodeOptions { PropertyNameCaseInsensitive = true };
var merged = JsonNode.Parse(new ProjectSettings().ToJson(), options)!.AsObject();
switch (JsonNode.Parse(json, options))
{
case JsonObject given:
Overlay(merged, given);
break;
case not null:
throw new JsonException("A project must be a JSON object.");
}
var settings = JsonSerializer.Deserialize(merged, ProjectJson.Default.ProjectSettings) ?? new ProjectSettings();
if (settings.Version > CurrentVersion)
throw new InvalidDataException($"project.json version {settings.Version} is newer than this Titano ({CurrentVersion}).");
return settings;
}
/// <summary>Copies <paramref name="source"/> into <paramref name="target"/>, merging nested objects; a null never replaces an object.</summary>
private static void Overlay(JsonObject target, JsonObject source)
{
foreach (var (name, value) in source)
{
if (value is JsonObject child && target[name] is JsonObject existing) Overlay(existing, child);
else if (value is not null || target[name] is not JsonObject) target[name] = value?.DeepClone();
}
}
}
[JsonSourceGenerationOptions(
WriteIndented = true,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
UseStringEnumConverter = true,
PropertyNameCaseInsensitive = true)]
[JsonSerializable(typeof(ProjectSettings))]
internal sealed partial class ProjectJson : JsonSerializerContext;