Rules for timing, frame rate, motion blur, deflicker, regions, holy-grail transitions, white balance, stabilisation, aspect, output size and export preset, each with a localisable reason and confidence; Apply fills Auto values without touching manual ones; Optimise takes per-section module decisions and reports changes; content-driven presets round-trip as JSON. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
508 lines
27 KiB
C#
508 lines
27 KiB
C#
using System.Globalization;
|
||
using Titano.Core.Analysis;
|
||
using Titano.Core.Analysis.Deep;
|
||
using Titano.Core.Export;
|
||
using Titano.Core.Imaging.Geometry;
|
||
using Titano.Core.Project;
|
||
using Titano.Core.Video;
|
||
|
||
namespace Titano.Core.Advisor;
|
||
|
||
/// <summary>
|
||
/// The recommendation rules of BRIEF §10.3, one method per parameter group. Output size is decided first because
|
||
/// shake and motion are judged in output pixels: deep-analysis pixels (source resolution) are scaled by output long
|
||
/// edge / source long edge. Every rule states its thresholds; missing inputs yield a null value with a
|
||
/// <see cref="AdvisorKeys.MissingPrefix"/> reason.
|
||
/// </summary>
|
||
internal sealed class AdvisorRules
|
||
{
|
||
/// <summary>Visible shake threshold in output pixels (BRIEF §10.3).</summary>
|
||
public const double ShakeThresholdPx = 0.3;
|
||
|
||
/// <summary>Film-look target shutter angle in degrees.</summary>
|
||
public const double TargetShutterAngle = 180;
|
||
|
||
/// <summary>Path smoothing σ in frames (as <c>StabilizerOptions.SmoothingSigma</c>).</summary>
|
||
public const double StabilizeSigma = 12;
|
||
|
||
private const double RegionConfidence = 0.6;
|
||
private const double StrongFlickerEv = 0.08;
|
||
private const double CropCostLimit = 0.15;
|
||
|
||
private static readonly (int Width, int Height)[] Landscape = [(3840, 2160), (2560, 1440), (1920, 1080), (1280, 720)];
|
||
private static readonly (int Width, int Height)[] Portrait = [(1080, 1920), (720, 1280)];
|
||
|
||
private readonly LightAnalysisReport _light;
|
||
private readonly DeepAnalysisReport? _deep;
|
||
private readonly SequenceContext _context;
|
||
private readonly List<Recommendation> _result = [];
|
||
private readonly int _frames;
|
||
private readonly (int Width, int Height) _source;
|
||
private (int Width, int Height) _output;
|
||
private TimingMode? _timing;
|
||
|
||
public AdvisorRules(LightAnalysisReport light, DeepAnalysisReport? deep, SequenceContext context)
|
||
{
|
||
_light = light;
|
||
_deep = deep;
|
||
_context = context;
|
||
_frames = context.FrameCount > 0 ? context.FrameCount : light.FrameCount;
|
||
_source = context.SourceWidth > 0 && context.SourceHeight > 0
|
||
? (context.SourceWidth, context.SourceHeight)
|
||
: light.DominantSize ?? (deep is { SourceWidth: > 0, SourceHeight: > 0 } ? (deep.SourceWidth, deep.SourceHeight) : (0, 0));
|
||
}
|
||
|
||
public List<Recommendation> Run()
|
||
{
|
||
Export();
|
||
Frames();
|
||
Timing();
|
||
Exposure();
|
||
Motion();
|
||
Camera();
|
||
return [.. _result.OrderBy(r => r.Section)];
|
||
}
|
||
|
||
/// <summary>Converts deep-analysis pixels (source resolution) to output pixels.</summary>
|
||
private double ToOutput(double sourcePixels)
|
||
{
|
||
var deepLong = _deep is { SourceWidth: > 0, SourceHeight: > 0 } d ? int.Max(d.SourceWidth, d.SourceHeight) : _deep?.AnalysisLongEdge ?? 0;
|
||
var outputLong = int.Max(_output.Width, _output.Height);
|
||
return deepLong > 0 && outputLong > 0 ? sourcePixels * outputLong / deepLong : sourcePixels;
|
||
}
|
||
|
||
private void Add(AdvisorSection section, string parameter, object? value, double confidence, string reason, params object[] args) =>
|
||
_result.Add(new Recommendation(section, parameter, value, value is null ? 0 : double.Round(confidence, 2), reason, args));
|
||
|
||
private void Missing(AdvisorSection section, string parameter, string reason, params object[] args) => Add(section, parameter, null, 0, reason, args);
|
||
|
||
/// <summary>
|
||
/// Output size and preset: standard 16:9 sizes (9:16 for portrait sources; 4K UHD at most) are tried from the
|
||
/// largest that fits inside the source cropped to that aspect — or inside the embedded previews when frames decode
|
||
/// only as previews — downwards, and the first whose preset the encoders can produce wins. Preset per size: 10-bit
|
||
/// HEVC archive for star fields and night skies (dark gradients band in 8 bit), HEVC, then H.264 at 4K; the H.264
|
||
/// preset of the size below 4K; vertical social for portrait sources.
|
||
/// </summary>
|
||
private void Export()
|
||
{
|
||
if (_source.Width <= 0 || _source.Height <= 0)
|
||
{
|
||
Missing(AdvisorSection.Export, AdvisorParameters.Resolution, AdvisorKeys.MissingSourceSize);
|
||
Missing(AdvisorSection.Export, AdvisorParameters.ExportPreset, AdvisorKeys.MissingSourceSize);
|
||
return;
|
||
}
|
||
var portrait = _source.Height > _source.Width;
|
||
var ratio = portrait ? 9.0 / 16 : 16.0 / 9;
|
||
var cap = Fit(_source.Width, _source.Height, ratio);
|
||
var reason = AdvisorKeys.ResolutionSource;
|
||
object[] args = [_source.Width, _source.Height];
|
||
if (_context is { PreviewOnlyCount: > 0, PreviewWidth: > 0, PreviewHeight: > 0 })
|
||
{
|
||
var preview = Fit(_context.PreviewWidth, _context.PreviewHeight, ratio);
|
||
if (preview.Width < cap.Width)
|
||
{
|
||
cap = preview;
|
||
reason = AdvisorKeys.ResolutionPreview;
|
||
args = [_context.PreviewOnlyCount, _context.PreviewWidth, _context.PreviewHeight];
|
||
}
|
||
}
|
||
|
||
var sizes = (portrait ? Portrait : Landscape).Where(s => s.Width <= cap.Width && s.Height <= cap.Height).ToList();
|
||
if (sizes.Count == 0) sizes.Add((int.Max(2, cap.Width & ~1), int.Max(2, cap.Height & ~1)));
|
||
EncoderCapability? limiting = null;
|
||
foreach (var size in sizes)
|
||
{
|
||
foreach (var (preset, key) in Candidates(size, portrait))
|
||
{
|
||
var codec = ExportPresets.Find(preset)!.Codec;
|
||
if (!Fits(codec, size, out var capability))
|
||
{
|
||
limiting ??= capability;
|
||
continue;
|
||
}
|
||
_output = size;
|
||
if (limiting is not null)
|
||
{
|
||
reason = AdvisorKeys.ResolutionCodec;
|
||
args = [limiting.Name, limiting.MaxWidth, limiting.MaxHeight];
|
||
}
|
||
Add(AdvisorSection.Export, AdvisorParameters.Resolution, ParameterBindings.Size(size.Width, size.Height), 0.9, reason, args);
|
||
if (key == AdvisorKeys.ExportStandard) Add(AdvisorSection.Export, AdvisorParameters.ExportPreset, preset, 0.7, key, size.Width, size.Height);
|
||
else Add(AdvisorSection.Export, AdvisorParameters.ExportPreset, preset, key == AdvisorKeys.ExportNight ? 0.8 : 0.7, key);
|
||
return;
|
||
}
|
||
}
|
||
// No listed encoder can produce any standard size: keep the source-derived size and leave the preset alone.
|
||
_output = sizes[0];
|
||
Add(AdvisorSection.Export, AdvisorParameters.Resolution, ParameterBindings.Size(_output.Width, _output.Height), 0.5, reason, args);
|
||
}
|
||
|
||
/// <summary>Presets for one output size in order of preference, with the reason each one carries.</summary>
|
||
private IEnumerable<(string Preset, string Reason)> Candidates((int Width, int Height) size, bool portrait)
|
||
{
|
||
if (portrait)
|
||
{
|
||
yield return ("vertical-1080x1920", AdvisorKeys.ExportVertical);
|
||
yield break;
|
||
}
|
||
if (size.Width >= 3840)
|
||
{
|
||
if (IsNightSky() && HasTenBitHevc()) yield return ("4k-hevc10-archive", AdvisorKeys.ExportNight);
|
||
yield return ("4k-hevc", AdvisorKeys.ExportStandard);
|
||
yield return ("4k-h264", AdvisorKeys.ExportStandard);
|
||
}
|
||
else if (size.Width >= 2560) yield return ("1440p-h264", AdvisorKeys.ExportStandard);
|
||
else if (size.Width >= 1920) yield return ("1080p-h264", AdvisorKeys.ExportStandard);
|
||
else yield return ("720p-preview", AdvisorKeys.ExportStandard);
|
||
}
|
||
|
||
/// <summary>Whether an encoder for <paramref name="codec"/> handles the size (either orientation); with no
|
||
/// capability list everything fits. <paramref name="largest"/> is the codec's largest capability, if any.</summary>
|
||
private bool Fits(VideoCodec codec, (int Width, int Height) size, out EncoderCapability? largest)
|
||
{
|
||
largest = null;
|
||
if (_context.Codecs.Count == 0) return true;
|
||
var available = _context.Codecs.Where(c => c.Codec == codec).ToList();
|
||
largest = available.MaxBy(c => (long)c.MaxWidth * c.MaxHeight);
|
||
return available.Any(c => c.MaxWidth <= 0 || c.MaxHeight <= 0
|
||
|| (size.Width <= c.MaxWidth && size.Height <= c.MaxHeight)
|
||
|| (size.Height <= c.MaxWidth && size.Width <= c.MaxHeight));
|
||
}
|
||
|
||
/// <summary>Hero frame from the deep analysis; exclusion of undecodable, black, white and duplicate frames.</summary>
|
||
private void Frames()
|
||
{
|
||
if (_deep is null)
|
||
{
|
||
Missing(AdvisorSection.Frames, AdvisorParameters.Hero, AdvisorKeys.MissingDeep);
|
||
Missing(AdvisorSection.Frames, AdvisorParameters.Excluded, AdvisorKeys.MissingDeep);
|
||
return;
|
||
}
|
||
if (_deep.HeroIndex is { } hero) Add(AdvisorSection.Frames, AdvisorParameters.Hero, hero, 0.6, AdvisorKeys.Hero, hero + 1);
|
||
var excluded = _deep.Issues.Excludable();
|
||
if (excluded.Length > 0) Add(AdvisorSection.Frames, AdvisorParameters.Excluded, excluded, 0.9, AdvisorKeys.Exclude, excluded.Length);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Timing mode, in order: several capture sessions (pauses ≥ the session gap) → Adaptive; jitter above 10 % of the
|
||
/// median interval → Adaptive; gaps that are sparse (≤ 2 % of intervals, each ≤ 10 × median) with smooth measured
|
||
/// motion (95th percentile ≤ 1 % of the long edge per frame) → Interpolated, or Constant when motion is unknown or
|
||
/// fast; denser gaps → Adaptive; otherwise Constant. Frame rate, in order: "fit to N s" target → the standard rate
|
||
/// nearest frames/N; under 240 frames → 24 (clip ≥ 10 s); over 10 min at 30 fps → 60; several sessions → 30; fast
|
||
/// motion (≥ 1 % of the long edge per frame) → 24; measured moderate motion (≥ 0.2 %) → 24; otherwise 30.
|
||
/// </summary>
|
||
private void Timing()
|
||
{
|
||
if (_light.MedianIntervalSeconds is not { } median)
|
||
{
|
||
Missing(AdvisorSection.Timing, AdvisorParameters.TimingMode, AdvisorKeys.MissingTimestamps, _light.TimedFrameCount, _light.FrameCount);
|
||
_timing = null;
|
||
}
|
||
else
|
||
{
|
||
ChooseTimingMode(median);
|
||
}
|
||
FrameRate();
|
||
}
|
||
|
||
private void ChooseTimingMode(double median)
|
||
{
|
||
var jitter = _light.JitterSeconds ?? 0;
|
||
var breaks = _light.Intervals.Where(i => i.Kind == IntervalKind.SessionBreak).ToList();
|
||
var gaps = _light.Intervals.Where(i => i.Kind == IntervalKind.Gap).ToList();
|
||
var section = AdvisorSection.Timing;
|
||
var parameter = AdvisorParameters.TimingMode;
|
||
if (_light.Sessions.Count >= 2 && breaks.Count > 0)
|
||
{
|
||
_timing = TimingMode.Adaptive;
|
||
Add(section, parameter, _timing, 0.85, AdvisorKeys.TimingSessions, _light.Sessions.Count, R(breaks.Max(b => b.Seconds) / 3600, 1));
|
||
}
|
||
else if (jitter > 0.1 * median)
|
||
{
|
||
_timing = TimingMode.Adaptive;
|
||
Add(section, parameter, _timing, 0.7, AdvisorKeys.TimingIrregular, R(jitter, 2), R(median, 2));
|
||
}
|
||
else if (gaps.Count > 0)
|
||
{
|
||
var sparse = gaps.Count <= double.Max(1, 0.02 * _light.Intervals.Count) && gaps.All(g => g.Seconds <= 10 * median);
|
||
var motion = MotionFraction();
|
||
if (sparse && motion is <= 0.01)
|
||
{
|
||
_timing = TimingMode.Interpolated;
|
||
Add(section, parameter, _timing, 0.65, AdvisorKeys.TimingSparseGapsSmooth, gaps.Count, R(ToOutput(_deep!.Motion.P95Px!.Value), 1));
|
||
}
|
||
else if (sparse)
|
||
{
|
||
_timing = TimingMode.Constant;
|
||
Add(section, parameter, _timing, 0.6, AdvisorKeys.TimingSparseGaps, gaps.Count);
|
||
}
|
||
else
|
||
{
|
||
_timing = TimingMode.Adaptive;
|
||
Add(section, parameter, _timing, 0.7, AdvisorKeys.TimingManyGaps, gaps.Count);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_timing = TimingMode.Constant;
|
||
Add(section, parameter, _timing, 0.9, AdvisorKeys.TimingRegular, R(median, 2), R(jitter, 2));
|
||
}
|
||
}
|
||
|
||
private void FrameRate()
|
||
{
|
||
var section = AdvisorSection.Timing;
|
||
var parameter = AdvisorParameters.FrameRate;
|
||
if (_frames <= 0) return;
|
||
double Seconds(int fps) => R((double)_frames / fps, 1);
|
||
var motion = MotionFraction();
|
||
if (_context.TargetDurationSeconds is > 0 and var target)
|
||
{
|
||
var wanted = _frames / target;
|
||
var fps = new[] { 24, 25, 30, 60 }.MinBy(f => double.Abs(double.Log(f / wanted)));
|
||
Add(section, parameter, Rate(fps), 0.8, AdvisorKeys.FrameRateFit, _frames, R(target, 1), fps);
|
||
Add(section, AdvisorParameters.FitToSeconds, target, 0.8, AdvisorKeys.FitToSeconds, _frames, R(target, 1), fps);
|
||
}
|
||
else if (_frames < 240) Add(section, parameter, Rate(24), 0.7, AdvisorKeys.FrameRateShort, _frames, Seconds(24));
|
||
else if (_frames > 30 * 600) Add(section, parameter, Rate(60), 0.6, AdvisorKeys.FrameRateLong, _frames, Seconds(60));
|
||
else if (_light.Sessions.Count >= 2) Add(section, parameter, Rate(30), 0.7, AdvisorKeys.FrameRateSessions, Seconds(30));
|
||
else if (motion >= 0.01) Add(section, parameter, Rate(24), 0.6, AdvisorKeys.FrameRateFastMotion, R(ToOutput(_deep!.Motion.P95Px!.Value), 1), Seconds(24));
|
||
else if (motion >= 0.002) Add(section, parameter, Rate(24), 0.5, AdvisorKeys.FrameRateCinematic, R(ToOutput(_deep!.Motion.P95Px!.Value), 1), Seconds(24));
|
||
else Add(section, parameter, Rate(30), 0.5, AdvisorKeys.FrameRateDefault, Seconds(30));
|
||
}
|
||
|
||
/// <summary>
|
||
/// Deflicker on when the step-free luminance deviates from its smooth curve by ≥ 0.02 EV RMS; window = the
|
||
/// spectrum's 4 × flicker period (or correlation length), odd, clamped to 15–151 (the reason says when it was
|
||
/// clamped); strength 1 for ≥ 0.08 EV, else 0.8.
|
||
/// Regions follow the mask method when its confidence is ≥ 0.6. Holy grail on when metadata exposure steps reached
|
||
/// the pixels (all metadata steps when pixels were not analysed); transition = half the median spacing of the
|
||
/// received steps (12 frames for a single step). White-balance smoothing on when the colour ratios jitter by
|
||
/// ≥ 0.01 log2 RMS around their smoothed course.
|
||
/// </summary>
|
||
private void Exposure()
|
||
{
|
||
HolyGrail();
|
||
var section = AdvisorSection.Exposure;
|
||
if (_deep is null)
|
||
{
|
||
foreach (var p in new[] { AdvisorParameters.Deflicker, AdvisorParameters.DeflickerWindow, AdvisorParameters.DeflickerStrength, AdvisorParameters.Regions, AdvisorParameters.WhiteBalanceSmoothing })
|
||
Missing(section, p, AdvisorKeys.MissingDeep);
|
||
return;
|
||
}
|
||
var flicker = _deep.Flicker;
|
||
var amplitude = R(flicker.AmplitudeEv, 3);
|
||
if (flicker.AmplitudeEv >= DeepSequenceAnalysis.VisibleFlickerEv)
|
||
Add(section, AdvisorParameters.Deflicker, true, double.Min(0.95, 0.5 + 10 * flicker.AmplitudeEv), AdvisorKeys.DeflickerOn, amplitude);
|
||
else Add(section, AdvisorParameters.Deflicker, false, 0.7, AdvisorKeys.DeflickerOff, amplitude);
|
||
var scale = flicker.PeriodFrames ?? flicker.CorrelationLength;
|
||
var window = flicker.RecommendedWindow;
|
||
if (4 * scale < FlickerSpectrumOptions.Default.MinimumWindow) Add(section, AdvisorParameters.DeflickerWindow, window, 0.6, AdvisorKeys.WindowMinimum, window);
|
||
else if (4 * scale > FlickerSpectrumOptions.Default.MaximumWindow) Add(section, AdvisorParameters.DeflickerWindow, window, 0.6, AdvisorKeys.WindowMaximum, R(scale, 1), window);
|
||
else if (flicker.PeriodFrames is not null) Add(section, AdvisorParameters.DeflickerWindow, window, 0.75, AdvisorKeys.WindowPeriod, R(scale, 1), window);
|
||
else Add(section, AdvisorParameters.DeflickerWindow, window, 0.6, AdvisorKeys.WindowCorrelation, R(scale, 1), window);
|
||
if (flicker.AmplitudeEv >= StrongFlickerEv) Add(section, AdvisorParameters.DeflickerStrength, 1.0, 0.6, AdvisorKeys.StrengthFull, amplitude);
|
||
else Add(section, AdvisorParameters.DeflickerStrength, 0.8, 0.6, AdvisorKeys.StrengthPartial, amplitude);
|
||
|
||
if (_deep.Regions is not { } regions) Missing(section, AdvisorParameters.Regions, AdvisorKeys.MissingDeep);
|
||
else
|
||
{
|
||
var percent = R(100 * regions.Confidence, 0);
|
||
if (regions.Confidence < RegionConfidence) Add(section, AdvisorParameters.Regions, RegionMode.Off, 0.7, AdvisorKeys.RegionsOff, percent);
|
||
else if (regions.Method == RegionMethod.Horizon) Add(section, AdvisorParameters.Regions, RegionMode.Horizon, regions.Confidence, AdvisorKeys.RegionsHorizon, percent);
|
||
else Add(section, AdvisorParameters.Regions, RegionMode.Luminance, regions.Confidence, AdvisorKeys.RegionsLuminance, percent);
|
||
}
|
||
|
||
var jitter = _deep.WhiteBalance.JitterLog2;
|
||
var jitterPercent = R(100 * (double.Exp2(jitter) - 1), 1);
|
||
if (jitter >= DeepSequenceAnalysis.VisibleWhiteBalanceJitter) Add(section, AdvisorParameters.WhiteBalanceSmoothing, true, 0.7, AdvisorKeys.WhiteBalanceOn, jitterPercent);
|
||
else Add(section, AdvisorParameters.WhiteBalanceSmoothing, false, 0.7, AdvisorKeys.WhiteBalanceOff, jitterPercent);
|
||
}
|
||
|
||
private void HolyGrail()
|
||
{
|
||
var section = AdvisorSection.Exposure;
|
||
if (!_light.ExposureValues.Any(v => v is not null))
|
||
{
|
||
Missing(section, AdvisorParameters.HolyGrail, AdvisorKeys.MissingExposure);
|
||
Missing(section, AdvisorParameters.TransitionLength, AdvisorKeys.MissingExposure);
|
||
return;
|
||
}
|
||
if (_deep is { Exposure: var exposure })
|
||
{
|
||
if (exposure.MetadataSteps == 0) Add(section, AdvisorParameters.HolyGrail, false, 0.9, AdvisorKeys.HolyGrailNone);
|
||
else if (exposure.ReceivedSteps == 0) Add(section, AdvisorParameters.HolyGrail, false, 0.8, AdvisorKeys.HolyGrailNotReceived, exposure.MetadataSteps);
|
||
else
|
||
{
|
||
Add(section, AdvisorParameters.HolyGrail, true, 0.85, AdvisorKeys.HolyGrailReceived, exposure.ReceivedSteps);
|
||
Transition(exposure.TypicalSpacing, exposure.TransitionLength!.Value, 0.8);
|
||
}
|
||
return;
|
||
}
|
||
if (_light.ExposureSteps.Count == 0)
|
||
{
|
||
Add(section, AdvisorParameters.HolyGrail, false, 0.9, AdvisorKeys.HolyGrailNone);
|
||
return;
|
||
}
|
||
Add(section, AdvisorParameters.HolyGrail, true, 0.6, AdvisorKeys.HolyGrailMetadata, _light.ExposureSteps.Count);
|
||
var spacing = _light.TypicalStepDistance;
|
||
var length = spacing is { } s ? int.Max(2, (int)double.Round(s / 2)) : HolyGrailOptions.Default.SingleStepTransitionFrames;
|
||
Transition(spacing, length, 0.5);
|
||
}
|
||
|
||
private void Transition(double? spacing, int length, double confidence)
|
||
{
|
||
if (spacing is { } s) Add(AdvisorSection.Exposure, AdvisorParameters.TransitionLength, length, confidence, AdvisorKeys.TransitionSpacing, R(s, 0), length);
|
||
else Add(AdvisorSection.Exposure, AdvisorParameters.TransitionLength, length, confidence, AdvisorKeys.TransitionSingle, length);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Target shutter angle 180° (film look); real angle = 360 × median exposure / median interval. Motion blur off when
|
||
/// the real angle already reaches the target or the moving content (95th-percentile flow, output pixels) moves
|
||
/// under 1 px per frame; otherwise on, adding √(180² − real²)/360 × motion px of trail. Flow quality Best for
|
||
/// interpolated timing (per-pixel vectors), else Balanced. Stabilisation on when the shake RMS exceeds 0.3 output
|
||
/// pixels (lower confidence when hiding it costs over 15 % of the frame); path smoothing σ = 12 frames.
|
||
/// </summary>
|
||
private void Motion()
|
||
{
|
||
var section = AdvisorSection.Motion;
|
||
var interval = _light.MedianIntervalSeconds;
|
||
var exposure = _context.MedianExposureSeconds;
|
||
if (interval is null)
|
||
{
|
||
Missing(section, AdvisorParameters.TargetShutterAngle, AdvisorKeys.MissingTimestamps, _light.TimedFrameCount, _light.FrameCount);
|
||
Missing(section, AdvisorParameters.MotionBlur, AdvisorKeys.MissingTimestamps, _light.TimedFrameCount, _light.FrameCount);
|
||
}
|
||
else if (exposure is null)
|
||
{
|
||
Missing(section, AdvisorParameters.TargetShutterAngle, AdvisorKeys.MissingExposure);
|
||
Missing(section, AdvisorParameters.MotionBlur, AdvisorKeys.MissingExposure);
|
||
}
|
||
else
|
||
{
|
||
var real = double.Min(360, 360 * exposure.Value / interval.Value);
|
||
Add(section, AdvisorParameters.TargetShutterAngle, TargetShutterAngle, 0.8, AdvisorKeys.ShutterAngle, R(real, 0), Significant(exposure.Value), R(interval.Value, 2));
|
||
MotionBlur(real);
|
||
}
|
||
|
||
var best = _timing == TimingMode.Interpolated;
|
||
Add(section, AdvisorParameters.FlowQuality, best ? FlowQualitySetting.Best : FlowQualitySetting.Balanced, 0.6, best ? AdvisorKeys.FlowBest : AdvisorKeys.FlowBalanced);
|
||
Stabilize();
|
||
}
|
||
|
||
private void MotionBlur(double real)
|
||
{
|
||
var section = AdvisorSection.Motion;
|
||
if (real >= TargetShutterAngle)
|
||
{
|
||
Add(section, AdvisorParameters.MotionBlur, false, 0.9, AdvisorKeys.MotionBlurAlready, R(real, 0), TargetShutterAngle);
|
||
return;
|
||
}
|
||
if (_deep is null)
|
||
{
|
||
Missing(section, AdvisorParameters.MotionBlur, AdvisorKeys.MissingDeep);
|
||
return;
|
||
}
|
||
if (_deep.Motion.P95Px is not { } p95)
|
||
{
|
||
Missing(section, AdvisorParameters.MotionBlur, AdvisorKeys.MissingMotion);
|
||
return;
|
||
}
|
||
var motion = ToOutput(p95);
|
||
if (motion < 1)
|
||
{
|
||
Add(section, AdvisorParameters.MotionBlur, false, 0.8, AdvisorKeys.MotionBlurLittle, R(motion, 1));
|
||
return;
|
||
}
|
||
var trail = motion * double.Sqrt(TargetShutterAngle * TargetShutterAngle - real * real) / 360;
|
||
Add(section, AdvisorParameters.MotionBlur, true, 0.75, AdvisorKeys.MotionBlurOn, R(real, 0), R(motion, 1), TargetShutterAngle, R(trail, 1));
|
||
}
|
||
|
||
private void Stabilize()
|
||
{
|
||
var section = AdvisorSection.Motion;
|
||
if (_deep is null)
|
||
{
|
||
Missing(section, AdvisorParameters.Stabilize, AdvisorKeys.MissingDeep);
|
||
Missing(section, AdvisorParameters.StabilizeSmoothing, AdvisorKeys.MissingDeep);
|
||
return;
|
||
}
|
||
var shake = _deep.Shake;
|
||
if (shake.MeasuredFraction < 0.5)
|
||
{
|
||
Missing(section, AdvisorParameters.Stabilize, AdvisorKeys.MissingShake, R(100 * shake.MeasuredFraction, 0));
|
||
return;
|
||
}
|
||
var rms = ToOutput(shake.RmsPx);
|
||
var crop = R(100 * shake.CropCost, 1);
|
||
if (rms <= ShakeThresholdPx) Add(section, AdvisorParameters.Stabilize, false, 0.8, AdvisorKeys.StabilizeOff, R(rms, 2));
|
||
else if (shake.CropCost <= CropCostLimit) Add(section, AdvisorParameters.Stabilize, true, 0.8, AdvisorKeys.StabilizeOn, R(rms, 2), crop);
|
||
else Add(section, AdvisorParameters.Stabilize, true, 0.5, AdvisorKeys.StabilizeHighCrop, R(rms, 2), crop);
|
||
Add(section, AdvisorParameters.StabilizeSmoothing, StabilizeSigma, 0.6, AdvisorKeys.StabilizeSmoothing, StabilizeSigma);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Aspect 16:9 for landscape sources (the reason says how much height or width the crop keeps), 9:16 for portrait
|
||
/// ones. When a confident horizon exists and the crop gives up
|
||
/// height, the crop is placed so the horizon sits on the nearest third of the output frame (recommended only when the
|
||
/// placement moves at least a fifth of the slack away from centred).
|
||
/// </summary>
|
||
private void Camera()
|
||
{
|
||
var section = AdvisorSection.Camera;
|
||
if (_source.Width <= 0 || _source.Height <= 0)
|
||
{
|
||
Missing(section, AdvisorParameters.Aspect, AdvisorKeys.MissingSourceSize);
|
||
return;
|
||
}
|
||
if (_source.Height > _source.Width)
|
||
{
|
||
Add(section, AdvisorParameters.Aspect, "9:16", 0.7, AdvisorKeys.AspectPortrait);
|
||
return;
|
||
}
|
||
var ratio = (double)_source.Width / _source.Height / (16.0 / 9);
|
||
var kept = double.Min(1, ratio);
|
||
if (double.Abs(ratio - 1) < 0.01) Add(section, AdvisorParameters.Aspect, "16:9", 0.9, AdvisorKeys.AspectNative);
|
||
else if (ratio < 1) Add(section, AdvisorParameters.Aspect, "16:9", 0.7, AdvisorKeys.AspectLandscape, _source.Width, _source.Height, R(100 * ratio, 0));
|
||
else Add(section, AdvisorParameters.Aspect, "16:9", 0.6, AdvisorKeys.AspectWide, _source.Width, _source.Height, R(100 / ratio, 0));
|
||
|
||
if (kept >= 0.99 || _deep?.Regions is not { Method: RegionMethod.Horizon, Confidence: >= RegionConfidence, Horizon: { Length: > 0 } horizon }) return;
|
||
var row = horizon.Average();
|
||
var slack = 1 - kept;
|
||
var third = double.Abs(row - (slack / 2 + kept / 3)) <= double.Abs(row - (slack / 2 + 2 * kept / 3)) ? 1.0 / 3 : 2.0 / 3;
|
||
var top = double.Clamp(row - third * kept, 0, slack);
|
||
var offset = R(2 * top / slack - 1, 2);
|
||
if (double.Abs(offset) >= 0.2) Add(section, AdvisorParameters.OffsetY, offset, 0.5, AdvisorKeys.FramingHorizon, R(100 * row, 0));
|
||
}
|
||
|
||
/// <summary>Night content: a star field, or a sequence the deep analysis or GPS phases call night.</summary>
|
||
private bool IsNightSky() => _deep is { } d && (d.Sky.StarField || d.Sky.Light == DayNight.Night);
|
||
|
||
/// <summary>Speed of moving content as a fraction of the frame's long edge per frame; null without motion data.</summary>
|
||
private double? MotionFraction()
|
||
{
|
||
if (_deep?.Motion.P95Px is not { } p95) return null;
|
||
var deepLong = _deep.SourceWidth > 0 ? int.Max(_deep.SourceWidth, _deep.SourceHeight) : _deep.AnalysisLongEdge;
|
||
return deepLong > 0 ? p95 / deepLong : null;
|
||
}
|
||
|
||
private bool HasTenBitHevc() => _context.Codecs.Count == 0 || _context.Codecs.Any(c => c.Codec == VideoCodec.Hevc && c.SupportsTenBit);
|
||
|
||
private static (int Width, int Height) Fit(int width, int height, double ratio)
|
||
{
|
||
var crop = AspectRatio.FitCrop(RectD.FromSize(width, height), ratio);
|
||
return ((int)double.Floor(crop.Width + 1e-9), (int)double.Floor(crop.Height + 1e-9));
|
||
}
|
||
|
||
private static string Rate(int fps) => fps.ToString(CultureInfo.InvariantCulture);
|
||
|
||
private static double R(double value, int decimals) => double.Round(value, decimals, MidpointRounding.AwayFromZero);
|
||
|
||
private static double Significant(double value)
|
||
{
|
||
if (!(value > 0)) return value;
|
||
var decimals = 2 - (int)double.Floor(double.Log10(value));
|
||
return decimals is >= 0 and <= 15 ? R(value, decimals) : value;
|
||
}
|
||
}
|