E4: video fermi nella dimostrazione

La dimostrazione ha ora due video fermi veri: il video più recente di
@marta.demo e la storia video più recente di un altro account sono la
loro copertina ferma per 2 secondi dopo una breve dissolvenza dal nero
(H264PcmWriter.DemoStillVideo e FromRgba, scritti in casa). Ognuno ha il
suo file, registrato con la sua impronta vera, e un'analisi «Fermo» data
dall'analizzatore vero: così il filtro «Video fermi», «Salva il
fotogramma» con «Scarica» e «Nella cartella delle copie» (dal motore
vero) funzionano. Il video dopo di @marta.demo è «In movimento», quello
dopo ancora «Non analizzabile» (troppo lento); gli altri non sono mai
analizzati, così «È un video fermo?» e «Cerca i video fermi» lavorano
davvero. Nella sola dimostrazione il servizio non controlla l'impronta
prima di analizzare (VerifySource), perché gli altri video condividono
un file che si muove. Prove in DemoStillVideoTests; riga in LEGGIMI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-29 16:15:41 +01:00
1 parent 78502e9c14
commit 5eec9cc22a
8 files changed
+517 -3

No files matched your search

@@ -1,4 +1,5 @@
using Febe.Core.Codecs.Containers;
using Febe.Core.Codecs.Images;
namespace Febe.Core.Codecs.H264;
@@ -213,6 +214,83 @@ public static class H264PcmWriter
return output.ToArray();
}
/// <summary>
/// The demo's still video (tappa E4): <paramref name="picture"/> alone for two seconds, <see cref="DemoFps"/> pictures per
/// second, after a short fade from black (the first picture nearly black, the second at half light), as a photo
/// shared as a video. «È un video fermo?» finds it «Fermo» and «Salva il fotogramma» takes a frame after the fade.
/// </summary>
/// <param name="picture">The picture, <see cref="DemoWidth"/>×<see cref="DemoHeight"/> (see <see cref="FromRgba"/>).</param>
public static byte[] DemoStillVideo(H264PcmPicture picture)
{
ArgumentNullException.ThrowIfNull(picture);
if (picture.Width != DemoWidth || picture.Height != DemoHeight)
{
throw new ArgumentException("Il fotogramma deve avere le dimensioni del video dimostrativo.", nameof(picture));
}
var dark = Faded(picture, 0.05);
var half = Faded(picture, 0.5);
using var output = new MemoryStream();
WriteMp4(output, DemoWidth, DemoHeight, DemoPictures, DemoFps, i => i switch { 0 => dark, 1 => half, _ => picture }, new DateTimeOffset(2026, 1, 1, 12, 0, 0, TimeSpan.Zero));
return output.ToArray();
}
/// <summary>
/// A picture of the samples of <paramref name="image"/> (alpha ignored), with the coefficients the decoder uses for a
/// stream that says nothing about its colours under 720 lines: BT.601, limited range (luma 16-235, chroma 16-240).
/// The chroma of each 2×2 block is the mean of its four pixels. Width and height must be multiples of 16.
/// </summary>
public static H264PcmPicture FromRgba(RgbaImage image)
{
ArgumentNullException.ThrowIfNull(image);
var p = new H264PcmPicture(image.Width, image.Height);
var pixels = image.Pixels;
var chromaWidth = image.Width / 2;
for (var y = 0; y < image.Height; y += 2)
{
for (var x = 0; x < image.Width; x += 2)
{
double cb = 0, cr = 0;
for (var dy = 0; dy < 2; dy++)
{
for (var dx = 0; dx < 2; dx++)
{
var at = (((y + dy) * image.Width) + x + dx) * RgbaImage.BytesPerPixel;
double r = pixels[at], g = pixels[at + 1], b = pixels[at + 2];
p.Y[((y + dy) * image.Width) + x + dx] = Clamp(16 + (((65.481 * r) + (128.553 * g) + (24.966 * b)) / 255), 16, 235);
cb += 128 + (((-37.797 * r) - (74.203 * g) + (112.0 * b)) / 255);
cr += 128 + (((112.0 * r) - (93.786 * g) - (18.214 * b)) / 255);
}
}
p.Cb[((y / 2) * chromaWidth) + (x / 2)] = Clamp(cb / 4, 16, 240);
p.Cr[((y / 2) * chromaWidth) + (x / 2)] = Clamp(cr / 4, 16, 240);
}
}
return p;
}
/// <summary>The picture with its light scaled by <paramref name="factor"/> toward black (luma 16, chroma 128).</summary>
private static H264PcmPicture Faded(H264PcmPicture picture, double factor)
{
var p = new H264PcmPicture(picture.Width, picture.Height);
for (var i = 0; i < p.Y.Length; i++)
{
p.Y[i] = Clamp(16 + ((picture.Y[i] - 16) * factor), 16, 235);
}
for (var i = 0; i < p.Cb.Length; i++)
{
p.Cb[i] = Clamp(128 + ((picture.Cb[i] - 128) * factor), 16, 240);
p.Cr[i] = Clamp(128 + ((picture.Cr[i] - 128) * factor), 16, 240);
}
return p;
}
private static byte Clamp(double value, int low, int high) => (byte)Math.Clamp((int)Math.Round(value), low, high);
/// <summary>Picture <paramref name="index"/> of the demo video (luma 16-235, chroma 16-240: no zero byte).</summary>
public static H264PcmPicture DemoPicture(int index)
{