Esportazione, tappa E4: video fermi #80

Merged
Alby96 merged 22 commits from funzione/video-fermi into main 2026-12-01 21:56:37 +01:00
2 changed files with 236 additions and 91 deletions
Showing only changes of commit cf52e0ea6a - Show all commits

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;
}
@@ -34,7 +34,7 @@ public sealed class StillVideoAnalyzerTests
Assert.AreEqual(StillVideoAnalyzer.Samples, result.Sampled);
Assert.AreEqual(1, result.Algorithm);
// Seven equal parts of one second: the first one looked at is picture 2 (the nearest to 72 ms), never the very
// Seven equal parts of one second: the first one looked at is picture 2 (the nearest to 77 ms), never the very
// first.
Assert.AreEqual(Ms(2), result.FrameAtMs, "a parità di nitidezza il primo mostrato fra quelli guardati");
Assert.AreEqual(new VideoAnalysis("abc", 1, VideoAnalysisOutcome.Still, Make.T0, result.FrameAt, null), result.ToAnalysis("abc", Make.T0));
@@ -52,13 +52,13 @@ public sealed class StillVideoAnalyzerTests
[TestMethod]
public void AFadeFromBlackIsStillAndTheFrameComesAfterIt()
{
// The first nine pictures fade in from black (picture 2, the first one looked at, at 22 %; picture 6 at 67 %);
// the third one looked at is picture 11, the one nearest to 358 ms.
// The first nine pictures fade in from black (picture 2, the first one looked at, at 22 %; picture 7 at 78 %);
// the third one looked at is picture 10, the one nearest to 345 ms.
var photo = PcmVideos.Photo(3);
var result = Analyse(PcmVideos.Mp4(30, i => i < 9 ? PcmVideos.Fade(photo, i / 9.0) : photo));
Assert.AreEqual(VideoAnalysisOutcome.Still, result.Outcome, Describe(result));
Assert.AreEqual(Ms(11), result.FrameAtMs, "il più nitido: il primo a piena luce, non uno della dissolvenza");
Assert.AreEqual(Ms(10), result.FrameAtMs, "il più nitido: il primo a piena luce, non uno della dissolvenza");
}
[TestMethod]
@@ -217,12 +217,12 @@ public sealed class StillVideoAnalyzerTests
[TestMethod]
public void TheSharpestFrameIsChosen()
{
// Every picture softened but picture 15 (the fourth looked at): the cells are the same, the detail is not.
// Every picture softened but picture 13 (the fourth looked at): the cells are the same, the detail is not.
var photo = PcmVideos.Photo(14);
var soft = PcmVideos.Blur(photo);
var result = Analyse(PcmVideos.Mp4(30, i => i == 15 ? photo : soft));
var result = Analyse(PcmVideos.Mp4(30, i => i == 13 ? photo : soft));
Assert.AreEqual(VideoAnalysisOutcome.Still, result.Outcome, Describe(result));
Assert.AreEqual(Ms(15), result.FrameAtMs);
Assert.AreEqual(Ms(13), result.FrameAtMs);
Assert.IsGreaterThan(0, result.Sharpness);
}
@@ -509,14 +509,17 @@ public sealed class StillVideoAnalyzerTests
(StillVideoAnalyzer.Algorithm, StillVideoAnalyzer.Samples, StillVideoAnalyzer.GridSize, StillVideoAnalyzer.DarkLevel,
StillVideoAnalyzer.UniformSpread, StillVideoAnalyzer.BlockThreshold, StillVideoAnalyzer.MeanThreshold, StillVideoAnalyzer.AreaThreshold));
Assert.AreEqual(
(128, 4, 3.0, 16.0, 0.1, 8.0, 0.1),
(StillVideoAnalyzer.FineGridSize, StillVideoAnalyzer.FineCell, StillVideoAnalyzer.EarlyAreaFactor, StillVideoAnalyzer.GainFloor,
StillVideoAnalyzer.GainAgreement, StillVideoAnalyzer.GainRange, StillVideoAnalyzer.ClippedShare));
(128, 4, 3.0, 0.1),
(StillVideoAnalyzer.FineGridSize, StillVideoAnalyzer.FineCell, StillVideoAnalyzer.EarlyAreaFactor, StillVideoAnalyzer.ClippedShare));
Assert.AreEqual(
(8.0, 10, 1.5, 0.1, 16.0),
(StillVideoAnalyzer.GainFloor, StillVideoAnalyzer.GainBlocks, StillVideoAnalyzer.GainNoise, StillVideoAnalyzer.GainAgreement,
StillVideoAnalyzer.GainRange));
Assert.AreEqual(
(250, 0.25, 96, 64.0, 32.0, 0.0002, TimeSpan.FromSeconds(1e9)),
(StillVideoAnalyzer.White, StillVideoAnalyzer.ChromaWeight, StillVideoAnalyzer.BrightDetail, StillVideoAnalyzer.UniformVariance,
StillVideoAnalyzer.UniformDetail, StillVideoAnalyzer.DetailShare, StillVideoAnalyzer.LatestInstant));
CollectionAssert.AreEqual(new[] { 0.4173, 0.3611, 0.6237, 0.5, 0.6619, 0.3307, 0.5521 }, StillVideoAnalyzer.Offsets);
CollectionAssert.AreEqual(new[] { 0.54, 0.865, 0.417, 0.253, 0.432, 0.45, 0.46 }, StillVideoAnalyzer.Offsets);
}
[TestMethod]
@@ -551,6 +554,40 @@ public sealed class StillVideoAnalyzerTests
Assert.AreEqual(VideoAnalysisOutcome.Moving, boomerang.Outcome, Describe(boomerang));
}
[TestMethod]
[DataRow(210, 30, 9, 21)]
[DataRow(300, 45, 0, 23)]
[DataRow(450, 68, 0, 34)]
[DataRow(60, 9, 0, 5)]
[DataRow(20, 3, 0, 2)]
public void TwoPicturesOfARepeatedClipAreMotion(int pictures, int period, int from, int to)
{
// Two pictures, each for a good part of the clip, with whole and broken numbers of repetitions: the frames looked
// at must not all fall on the same one.
var a = PcmVideos.Photo(1);
var b = PcmVideos.Photo(2);
var result = Analyse(PcmVideos.Mp4(pictures, i => i % period >= from && i % period < to ? b : a));
Assert.AreEqual(VideoAnalysisOutcome.Moving, result.Outcome, Describe(result));
}
[TestMethod]
public void TheInstantsNeverFallOnMuchLessThanHalfOfARepeatedClip()
{
var at = StillVideoAnalyzer.Offsets.Select((o, i) => (i + o) / StillVideoAnalyzer.Samples).ToArray();
for (var repetitions = 2.0; repetitions <= 40; repetitions += 0.01)
{
// The positions inside the clip, around it: the largest stretch with none of them.
var phases = at.Select(t => t * repetitions % 1).Order().ToArray();
var empty = phases.Select((p, i) => (i + 1 < phases.Length ? phases[i + 1] : phases[0] + 1) - p).Max();
Assert.IsGreaterThanOrEqualTo(0.48, 1 - empty, $"{repetitions:0.00} ripetizioni");
}
// Far from the ends, and no long stretch of the video not looked at.
Assert.IsGreaterThan(0.3, StillVideoAnalyzer.Offsets[0]);
Assert.IsLessThan(0.7, StillVideoAnalyzer.Offsets[^1]);
Assert.IsLessThan(1.4 / StillVideoAnalyzer.Samples, at.Skip(1).Zip(at, (next, t) => next - t).Max());
}
[TestMethod]
[DataRow(0.0)]
[DataRow(0.015)]
@@ -600,6 +637,79 @@ public sealed class StillVideoAnalyzerTests
Assert.AreEqual(VideoAnalysisOutcome.Still, text.Outcome, Describe(text));
}
[TestMethod]
public void ACardOnBlackABlueOneOrADarkPhotoWithAFadeIsStill()
{
// Few blocks with light, or little luma in all of them: the fade is told by those there are.
var text = PcmVideos.Grey(PcmVideos.Width, PcmVideos.Height, (x, y) =>
y is > 80 and < 170 && y / 6 % 2 == 0 && x is > 20 and < 124 && x / 5 % 2 == 0 ? 235 : 16);
var logo = PcmVideos.Grey(PcmVideos.Width, PcmVideos.Height, (x, y) => x is >= 48 and < 96 && y is >= 100 and < 156 ? 200 : 16);
var night = PcmVideos.Fade(PcmVideos.Photo(5), 0.1);
foreach (var at in new[] { 20, 70, 110 })
{
night = PcmVideos.Sticker(night, at, 2 * at, 4, 4);
}
foreach (var (name, picture) in new[]
{
("scritta su nero", text), ("marchio su nero", logo), ("foto scura", night), ("cartello blu", PcmVideos.Poster(41, 240, 110, 0.15)),
("foto in basso su nero, 5 %", PcmVideos.Poster(16, 128, 128, 0.05)), ("10 %", PcmVideos.Poster(16, 128, 128, 0.1)),
})
{
foreach (var (pictures, fade) in new[] { (30, 4.0), (30, 22), (90, 30), (150, 15) })
{
var result = Analyse(PcmVideos.Mp4(pictures, i => PcmVideos.Noise(PcmVideos.Fade(picture, Math.Min(1, i / fade)), 1, i)));
Assert.AreEqual((VideoAnalysisOutcome.Still, null), (result.Outcome, result.Note), $"{name}, {pictures}, {fade}: {Describe(result)}");
}
// At the end too.
var last = Analyse(PcmVideos.Mp4(30, i => PcmVideos.Fade(picture, Math.Min(1, (29 - i) / 12.0))));
Assert.AreEqual(VideoAnalysisOutcome.Still, last.Outcome, $"{name}: {Describe(last)}");
}
// What moves on black, or a card that moves, is still motion while it fades.
H264PcmPicture Scroll(int i) => PcmVideos.Grey(PcmVideos.Width, PcmVideos.Height, (x, y) =>
y is > 60 and < 190 && (y + (2 * i)) / 6 % 2 == 0 && x is > 20 and < 124 && x / 5 % 2 == 0 ? 235 : 16);
H264PcmPicture Pass(int i) => PcmVideos.Grey(PcmVideos.Width, PcmVideos.Height, (x, y) =>
x >= 10 + (2 * i) && x < 58 + (2 * i) && y is >= 100 and < 156 ? 200 : 16);
foreach (var (name, picture) in new (string, Func<int, H264PcmPicture>)[]
{
("scritta che scorre", Scroll), ("marchio che passa", Pass),
("cartello blu", i => PcmVideos.Sticker(PcmVideos.Flat(41, 240, 110), 5 + i, 60, 90, 120)),
})
{
var moving = Analyse(PcmVideos.Mp4(30, i => PcmVideos.Fade(picture(i), Math.Min(1, i / 9.0))));
Assert.AreEqual(VideoAnalysisOutcome.Moving, moving.Outcome, $"{name}: {Describe(moving)}");
}
}
[TestMethod]
public void ADimPictureAroundTheDarkThresholdIsNotMotion()
{
// The same dim picture in every frame: the grain takes some over the threshold and leaves others under it.
H264PcmPicture Gradient(double light) => PcmVideos.Grey(PcmVideos.Width, PcmVideos.Height, (x, y) => 16 + ((x + y) * light / 400 * 219 / 255));
H264PcmPicture Text(double light) => PcmVideos.Grey(PcmVideos.Width, PcmVideos.Height, (x, y) =>
y is >= 120 and < 126 && x is >= 60 and < 66 && (x + y) % 2 == 0 ? 16 + (light * 219 / 255) : 20);
foreach (var (name, picture, grain) in new[]
{
("sfumatura 31,5", Gradient(31.5), 8), ("sfumatura 32", Gradient(32), 4), ("sfumatura 32", Gradient(32), 8),
("sfumatura 32,5", Gradient(32.5), 2), ("sfumatura 32,5", Gradient(32.5), 4), ("scritta 92", Text(92), 10),
("scritta 94", Text(94), 4), ("scritta 94", Text(94), 6), ("scritta 94", Text(94), 10), ("scritta 96", Text(96), 2), ("scritta 96", Text(96), 4),
})
{
var result = Analyse(PcmVideos.Mp4(60, i => PcmVideos.Noise(picture, grain, i)));
Assert.AreNotEqual(VideoAnalysisOutcome.Moving, result.Outcome, $"{name} ±{grain}: {Describe(result)}");
}
// Black between two pictures is, dark as they are.
var night = PcmVideos.Fade(PcmVideos.Photo(5), 0.25);
foreach (var (name, picture) in new[] { ("foto scura", night), ("grigio", PcmVideos.Flat(60)), ("bianco", PcmVideos.Flat(235)) })
{
var between = Analyse(PcmVideos.Mp4(30, i => i is >= 12 and < 18 ? PcmVideos.Black() : picture));
Assert.AreEqual(VideoAnalysisOutcome.Moving, between.Outcome, $"{name}: {Describe(between)}");
}
}
[TestMethod]
public void OnePictureAmongDarkFramesSaysNothingOfTheVideo()
{