E4: l'analisi regge dissolvenze su nero, colori pieni e foto scure, e filmati ripetuti

Una scritta o un marchio sul nero, un cartello blu e una foto scura che si dissolvono risultavano «In movimento»: il guadagno si stima pesando i blocchi sulla luce, sul canale più forte non bruciato. Lo stesso per un'immagine scura quanto la soglia, che la grana porta sopra e sotto: un fotogramma scuro fra due si confronta, non si condanna. Gli istanti nuovi abbracciano almeno il 48 % del giro di un filmato ripetuto da 2 a 40 volte; prima, a quasi sette ripetizioni, cadevano tutti insieme e il video risultava «Fermo».

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-01 20:44:45 +01:00
1 parent 90fa8aefcc
commit cf52e0ea6a
2 files changed
+236 -91

No files matched your search

+115 -80
View File
@@ -66,11 +66,13 @@ public sealed record StillVideoResult(
/// channel and the share of it clipped at white; its sharpness is the variance of the Laplacian of its luma.</item>
/// <item>A frame with no block as bright as <see cref="DarkLevel"/> in any colour, and no detail as bright as
/// <see cref="BrightDetail"/>, is dark: dark frames at the start or the end (a fade from or to black) are set aside; a
/// dark frame between two that are not means the picture changes. A single frame that is not dark says nothing of the
/// video: more are then looked at between the dark ones around it.</item>
/// dark frame between two that are not is compared like any other (a dim picture whose grain crosses the threshold is
/// still; black between two pictures is not), and is never the one chosen. A single frame that is not dark says nothing
/// of the video: more are then looked at between the dark ones around it.</item>
/// <item>A fade is undone before frames are compared: when the brightness ratios of the blocks agree (a fade scales
/// them all alike) the picture is scaled to the frame's brightness; what clipping at white may hide is bounded, channel
/// by channel, and never taken for a change.</item>
/// them all alike), each counted by its light so that a card on black or a dark photo tells its fade too, the picture is
/// scaled to the frame's brightness; what clipping at white may hide is bounded, channel by channel, and never taken for
/// a change.</item>
/// <item>The still picture is the median of the frames, block by block, so a part that moves does not belong to it. The
/// video is still when every frame differs from it by a mean of at most <see cref="MeanThreshold"/> (each block capped
/// at <see cref="BlockThreshold"/>) and the blocks that differ by more than <see cref="BlockThreshold"/> cover at most
@@ -104,10 +106,11 @@ public static class StillVideoAnalyzer
internal const int FineCell = 4;
/// <summary>
/// Where each frame looked at sits inside its part of the video (0–1): uneven, so that no number of repetitions of a
/// short clip puts them all on the same picture of it.
/// Where each frame looked at sits inside its part of the video (0–1): the first and the last far from the ends, the
/// others uneven (found by a search), so that for a clip repeated 2 to 40 times, also not a whole number, the seven
/// frames never fall within less than 48 % of it. A picture shown for less than half of the clip can still be missed.
/// </summary>
internal static readonly double[] Offsets = [0.4173, 0.3611, 0.6237, 0.5, 0.6619, 0.3307, 0.5521];
internal static readonly double[] Offsets = [0.54, 0.865, 0.417, 0.253, 0.432, 0.45, 0.46];
/// <summary>
/// A frame none of whose blocks has a red, green or blue mean as bright as this (0–255) is dark: black or nearly
@@ -136,18 +139,24 @@ public static class StillVideoAnalyzer
/// </summary>
internal const double EarlyAreaFactor = 3;
/// <summary>Blocks darker than this in both frames do not take part in the estimate of a fade.</summary>
internal const double GainFloor = 16;
/// <summary>Blocks darker than this (their largest channel) in both frames do not take part in the estimate of a fade.</summary>
internal const double GainFloor = 8;
/// <summary>The largest spread (interquartile range of the natural logarithms) of block ratios read as a fade.</summary>
/// <summary>The fewest blocks that can tell a fade.</summary>
internal const int GainBlocks = 10;
/// <summary>A block this near (0–255) to what the fade makes of it follows it: the rounding of a dim frame.</summary>
internal const double GainNoise = 1.5;
/// <summary>The width of the band, as a share of the ratio, in which the blocks holding half of the light must be for it to be a fade.</summary>
internal const double GainAgreement = 0.1;
/// <summary>The largest brightness ratio undone as a fade.</summary>
internal const double GainRange = 8;
internal const double GainRange = 16;
/// <summary>
/// Blocks with more of their pixels clipped at white than this do not take part in the estimate of a fade, unless
/// too few are left.
/// A channel of a block with more of its pixels clipped at white than this does not take part in the estimate of a
/// fade, unless the others tell none.
/// </summary>
internal const double ClippedShare = 0.1;
@@ -282,7 +291,8 @@ public static class StillVideoAnalyzer
}
}
// In the order they are shown: dark frames only at the start or the end (a fade), never between two pictures.
// In the order they are shown: dark frames at the start or the end are a fade; between two that are not, the
// picture changes unless they are its like (a dim picture around the threshold).
// Motion proved by the frames there are stands; with a frame missing nothing else can be said.
frames.Sort((a, b) => a.Actual.CompareTo(b.Actual));
var complete = frames.Count == expected;
@@ -293,12 +303,16 @@ public static class StillVideoAnalyzer
}
var last = frames.FindLastIndex(f => !f.Dark);
if (frames.Skip(first).Take(last - first + 1).Any(f => f.Dark))
var pictures = frames.GetRange(first, last - first + 1);
foreach (var i in Enumerable.Range(0, pictures.Count).Where(i => pictures[i].Dark))
{
return Moving(frames.Count, info, 0, 0);
var (mean, area) = Compare(pictures[i], pictures.Take(i).Last(f => !f.Dark));
if (mean > MeanThreshold || area > EarlyAreaFactor * AreaThreshold)
{
return Moving(frames.Count, info, mean, area);
}
}
var pictures = frames.Where(f => !f.Dark).ToList();
if (pictures.All(f => f.Uniform))
{
// Before the comparison of the blocks, which cannot undo a fade of a colour that clips or of a dark one.
@@ -323,7 +337,7 @@ public static class StillVideoAnalyzer
// The sharpest picture (the first shown of equally sharp ones): never a dark frame, nor one dimmed by a fade.
var chosen = pictures[0];
foreach (var f in pictures.Skip(1))
foreach (var f in pictures.Skip(1).Where(f => !f.Dark))
{
if (f.Sharpness > chosen.Sharpness)
{
@@ -438,15 +452,21 @@ public static class StillVideoAnalyzer
private static StillVideoResult Moving(int sampled, VideoFrameInfo info, double difference, double area) =>
new(VideoAnalysisOutcome.Moving, null, null, sampled, info.Rotation, info.HasAudio, Math.Round(difference, 2), Math.Round(area, 4), 0);
/// <summary>Whether the pictures, each of a single colour, all show the same colour, fades undone on its largest channel.</summary>
/// <summary>
/// Whether the pictures, each of a single colour, all show the same colour, fades undone on its largest channel that
/// is not clipped at white.
/// </summary>
private static bool SameColour(List<Signature> pictures)
{
var brightest = pictures.MaxBy(p => p.Colour.Largest)!.Colour;
var brightest = pictures.MaxBy(p => p.Luma.Sum())!.Whole;
var channel = Enumerable.Range(0, Grid.Channels)
.OrderBy(c => brightest.Clipped[c][0] > ClippedShare || brightest.Mean[c][0] < GainFloor)
.ThenByDescending(c => brightest.Mean[c][0])
.First();
return pictures.All(p =>
{
var gain = p.Colour.Largest / Math.Max(1, brightest.Largest);
return Difference(p.Colour.R - (gain * brightest.R), p.Colour.G - (gain * brightest.G), p.Colour.B - (gain * brightest.B), ChromaWeight)
<= BlockThreshold;
var gain = Math.Clamp(p.Whole.Mean[channel][0] / Math.Max(1.0, brightest.Mean[channel][0]), 1 / GainRange, GainRange);
return Measure(p.Whole, brightest, gain, gain, ChromaWeight).Area == 0;
});
}
@@ -561,43 +581,72 @@ public static class StillVideoAnalyzer
private static double Bt709(double red, double green, double blue) => (0.2126 * red) + (0.7152 * green) + (0.0722 * blue);
/// <summary>
/// The brightness of <paramref name="a"/> against <paramref name="b"/> when it is a fade of it: the median ratio of
/// the blocks, provided the middle half of the ratios agree within <see cref="GainAgreement"/> (a different picture
/// gives ratios all over); 1 otherwise. Dark blocks do not count, nor blocks clipped at white, unless fewer than a
/// quarter of the blocks are left (a white card, a saturated poster): then the clipped ones count too.
/// The brightness of <paramref name="a"/> against <paramref name="b"/> when it is a fade of it, 1 otherwise: from the
/// blocks not clipped at white or, when they are too few or do not agree, from all of them (a white card).
/// </summary>
private static double Gain(Signature a, Signature b)
private static double Gain(Signature a, Signature b) => Fade(a, b, clippedToo: false) ?? Fade(a, b, clippedToo: true) ?? 1;
/// <summary>
/// The ratio between two frames that the blocks agree on, or null. Each block counts by its brightness: the median
/// of the ratios, provided at least half of the light is in blocks that follow it within <see cref="GainAgreement"/>
/// (a different picture gives ratios all over), and the blocks are at least <see cref="GainBlocks"/>; then the ratio
/// of the light of those blocks. The largest channel of a block that is not clipped is measured, so that a saturated
/// or dark picture takes part too; a block under <see cref="GainFloor"/> in both frames does not.
/// </summary>
private static double? Fade(Signature a, Signature b, bool clippedToo)
{
var ratios = Ratios(a, b, clippedToo: false);
if (ratios.Count < Signature.Blocks / 4)
{
ratios = Ratios(a, b, clippedToo: true);
}
if (ratios.Count < Signature.Blocks / 4)
{
return 1;
}
ratios.Sort();
return ratios[3 * ratios.Count / 4] - ratios[ratios.Count / 4] > GainAgreement
? 1
: Math.Clamp(Math.Exp(ratios[ratios.Count / 2]), 1 / GainRange, GainRange);
}
private static List<double> Ratios(Signature a, Signature b, bool clippedToo)
{
var ratios = new List<double>(Signature.Blocks);
var blocks = new List<(float A, float B, double Ratio)>(Signature.Blocks);
double light = 0;
for (var i = 0; i < Signature.Blocks; i++)
{
if (Math.Max(a.Luma[i], b.Luma[i]) >= GainFloor && a.Luma[i] > 0.5 && b.Luma[i] > 0.5
&& (clippedToo || (a.Clipped[i] <= ClippedShare && b.Clipped[i] <= ClippedShare)))
float largest = 0, va = 0, vb = 0;
for (var c = 0; c < Grid.Channels; c++)
{
ratios.Add(Math.Log(a.Luma[i] / b.Luma[i]));
var (ca, cb) = (a.Coarse.Mean[c][i], b.Coarse.Mean[c][i]);
if (Math.Max(ca, cb) > largest
&& (clippedToo || (a.Coarse.Clipped[c][i] <= ClippedShare && b.Coarse.Clipped[c][i] <= ClippedShare)))
{
(largest, va, vb) = (Math.Max(ca, cb), ca, cb);
}
}
if (largest >= GainFloor)
{
blocks.Add((va, vb, Math.Max(va, 0.5) / Math.Max(vb, 0.5)));
light += largest;
}
}
return ratios;
if (blocks.Count < GainBlocks)
{
return null;
}
blocks.Sort((x, y) => x.Ratio.CompareTo(y.Ratio));
double median = 1, passed = 0;
foreach (var (va, vb, ratio) in blocks)
{
(median, passed) = (ratio, passed + Math.Max(va, vb));
if (passed >= light / 2)
{
break;
}
}
double agreeing = 0, lightA = 0, lightB = 0;
foreach (var (va, vb, _) in blocks)
{
// Measured on the darker frame, where one level of rounding weighs the most.
var (darker, expected) = median <= 1 ? (va, vb * median) : (vb, va / median);
if (Math.Abs(darker - expected) <= Math.Max(GainNoise, GainAgreement / 2 * expected))
{
agreeing += Math.Max(va, vb);
(lightA, lightB) = (lightA + va, lightB + vb);
}
}
var gain = lightA / Math.Max(lightB, 0.5);
return agreeing >= light / 2 && gain is >= 1 / GainRange and <= GainRange ? gain : null;
}
/// <summary>The picture in cells: for each channel the mean of the cell and the share of it clipped at white.</summary>
@@ -628,10 +677,9 @@ public static class StillVideoAnalyzer
/// <summary>
/// The grid of an RGBA frame. Each cell covers at least one pixel, so a frame narrower than the grid (or a stream
/// that changes size) still gives a full grid. <paramref name="clippedAny"/>, when given, gets the share of the
/// pixels of each cell with any channel clipped.
/// that changes size) still gives a full grid.
/// </summary>
internal static Grid Of(RgbaImage image, int columns, int rows, double[]? clippedAny = null)
internal static Grid Of(RgbaImage image, int columns, int rows)
{
var grid = new Grid(columns, rows);
var (w, h) = (image.Width, image.Height);
@@ -646,7 +694,7 @@ public static class StillVideoAnalyzer
var x0 = gx * w / columns;
var x1 = Math.Max(x0 + 1, (gx + 1) * w / columns);
long r = 0, g = 0, b = 0;
int wr = 0, wg = 0, wb = 0, any = 0;
int wr = 0, wg = 0, wb = 0;
for (var y = y0; y < y1; y++)
{
var row = pixels.Slice((y * stride) + (x0 * RgbaImage.BytesPerPixel), (x1 - x0) * RgbaImage.BytesPerPixel);
@@ -656,13 +704,9 @@ public static class StillVideoAnalyzer
r += pr;
g += pg;
b += pb;
if (pr >= White || pg >= White || pb >= White)
{
any++;
wr += pr >= White ? 1 : 0;
wg += pg >= White ? 1 : 0;
wb += pb >= White ? 1 : 0;
}
wr += pr >= White ? 1 : 0;
wg += pg >= White ? 1 : 0;
wb += pb >= White ? 1 : 0;
}
}
@@ -670,10 +714,6 @@ public static class StillVideoAnalyzer
var cell = (gy * columns) + gx;
(grid.Mean[0][cell], grid.Mean[1][cell], grid.Mean[2][cell]) = (r / count, g / count, b / count);
(grid.Clipped[0][cell], grid.Clipped[1][cell], grid.Clipped[2][cell]) = (wr / count, wg / count, wb / count);
if (clippedAny is not null)
{
clippedAny[cell] = any / count;
}
}
}
@@ -686,9 +726,9 @@ public static class StillVideoAnalyzer
{
internal const int Blocks = GridSize * GridSize;
private Signature(TimeSpan actual, Grid coarse, Grid fine, double[] clipped)
private Signature(TimeSpan actual, Grid whole, Grid coarse, Grid fine)
{
(Actual, Coarse, Fine, Clipped) = (actual, coarse, fine, clipped);
(Actual, Whole, Coarse, Fine) = (actual, whole, coarse, fine);
}
internal TimeSpan Actual { get; }
@@ -700,11 +740,8 @@ public static class StillVideoAnalyzer
/// <summary>The luma of each block.</summary>
internal double[] Luma { get; } = new double[Blocks];
/// <summary>The share of the pixels of each block with a channel clipped at white.</summary>
internal double[] Clipped { get; }
/// <summary>The mean colour of the frame, and the largest of its channels.</summary>
internal (double R, double G, double B, double Largest) Colour { get; private set; }
/// <summary>The frame as one cell: its mean colour.</summary>
internal Grid Whole { get; }
internal bool Dark { get; private set; }
@@ -716,10 +753,10 @@ public static class StillVideoAnalyzer
internal static Signature Of(RgbaImage image, TimeSpan actual)
{
var (w, h) = (image.Width, image.Height);
var clipped = new double[Blocks];
var coarse = Grid.Of(image, GridSize, GridSize, clipped);
var coarse = Grid.Of(image, GridSize, GridSize);
var fine = Grid.Of(image, Math.Clamp(w / FineCell, 1, FineGridSize), Math.Clamp(h / FineCell, 1, FineGridSize));
var s = new Signature(actual, coarse, fine, clipped);
var whole = Grid.Of(image, 1, 1);
var s = new Signature(actual, whole, coarse, fine);
var blue = new double[Blocks];
var red = new double[Blocks];
var brightest = 0.0;
@@ -731,12 +768,10 @@ public static class StillVideoAnalyzer
brightest = Math.Max(brightest, Math.Max(r, Math.Max(g, b)));
}
var (cr, cg, cb) = (coarse.Mean[0].Average(v => (double)v), coarse.Mean[1].Average(v => (double)v), coarse.Mean[2].Average(v => (double)v));
s.Colour = (cr, cg, cb, Math.Max(cr, Math.Max(cg, cb)));
ReadOnlySpan<byte> pixels = image.Pixels;
s.Dark = brightest < DarkLevel && !HasBrightDetail(pixels, w, h, image.Stride);
s.Uniform = Spread(s.Luma) < UniformSpread && Spread(blue) < UniformSpread && Spread(red) < UniformSpread
&& !HasDetail(pixels, w, h, image.Stride, Bt709(cr, cg, cb));
&& !HasDetail(pixels, w, h, image.Stride, Bt709(whole.Mean[0][0], whole.Mean[1][0], whole.Mean[2][0]));
s.Sharpness = LaplacianVariance(pixels, w, h, image.Stride);
return s;
}