Files
Febe/tests/Febe.Tests/Media/Containers/PcmVideos.cs
T
Alby96andClaude Opus 5.5 9277e0a18c E4: l'analisi non dice più «Fermo» di ciò che non ha visto
Filmati ripetuti, video tagliati o rovinati, neve e scritte piccole risultavano fermi; una scheda bianca in
dissolvenza in movimento; una scritta sul nero «tutto nero». Istanti a punti diversi, griglia fitta, bianco bruciato
per canale, fotogrammi mancanti e un solo fotogramma chiaro trattati a parte, errori del disco rilanciati.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 18:57:56 +01:00

285 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Febe.Core.Codecs.H264;
namespace Febe.Tests.Codecs.Containers;
/// <summary>
/// Test videos built from pictures whose every pixel is known (I_PCM H.264, <see cref="H264PcmWriter"/>), for the
/// still-video analysis of tappa E4: a photo-like picture, grain, fades, an animated sticker, a moving caption, black and
/// single-colour pictures. 144 × 256 (9:16, as phones film) at 30 pictures a second through <see cref="TestVideos"/>, so
/// audio and rotation come along; nothing comes from Instagram or from a real file.
/// </summary>
internal static class PcmVideos
{
public const int Width = 144;
public const int Height = 256;
/// <summary>Pictures a second (<see cref="TestVideos.Timescale"/> / <see cref="TestVideos.FrameTicks"/>).</summary>
public const int Fps = 30;
/// <summary>An elementary stream of <paramref name="count"/> IDR pictures, picture <c>i</c> given by <paramref name="picture"/>.</summary>
public static ElementaryStream Stream(string name, int count, Func<int, H264PcmPicture> picture)
{
using var annexB = new MemoryStream();
var first = picture(0);
foreach (var nal in new[] { H264PcmWriter.Sps(first.Width, first.Height), H264PcmWriter.Pps() })
{
annexB.Write([0, 0, 0, 1]);
annexB.Write(nal);
}
for (var i = 0; i < count; i++)
{
annexB.Write([0, 0, 0, 1]);
annexB.Write(H264PcmWriter.Idr(i == 0 ? first : picture(i), i % 2));
}
return ElementaryStream.FromAnnexB(name, annexB.ToArray());
}
/// <summary>An MP4 of <paramref name="count"/> pictures at <see cref="Fps"/>, optionally rotated and with an audio track.</summary>
public static byte[] Mp4(int count, Func<int, H264PcmPicture> picture, int rotation = 0, bool audio = false) =>
TestVideos.Mp4(Stream("pcm", count, picture), rotation: rotation, audio: audio);
/// <summary>
/// An MP4 of <paramref name="count"/> pictures of <paramref name="width"/> × <paramref name="height"/> (multiples of
/// 16), one a second: for what depends on the size of the picture.
/// </summary>
public static byte[] Large(int width, int height, int count, Func<int, H264PcmPicture> picture)
{
using var output = new MemoryStream();
H264PcmWriter.WriteMp4(output, width, height, count, 1, picture);
return output.ToArray();
}
/// <summary>A grey picture of any size (multiples of 16) whose luma at (x, y) is given, clamped to 16–235.</summary>
public static H264PcmPicture Grey(int width, int height, Func<int, int, double> luma)
{
var p = new H264PcmPicture(width, height);
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
p.Y[(y * width) + x] = Luma((int)Math.Round(luma(x, y)));
}
}
Array.Fill(p.Cb, (byte)128);
Array.Fill(p.Cr, (byte)128);
return p;
}
/// <summary>A poster: one colour, and a photo on the bottom <paramref name="share"/> of it.</summary>
public static H264PcmPicture Poster(byte luma, byte cb, byte cr, double share)
{
var p = Flat(luma, cb, cr);
var photo = Photo(4);
var top = Height - ((int)(Height * share) / 2 * 2);
for (var i = top * Width; i < p.Y.Length; i++)
{
p.Y[i] = (byte)(photo.Y[i] * 0.8);
}
for (var i = top / 2 * (Width / 2); i < p.Cb.Length; i++)
{
(p.Cb[i], p.Cr[i]) = (photo.Cb[i], photo.Cr[i]);
}
return p;
}
/// <summary>
/// A photo-like picture: a diagonal gradient, a few coloured shapes and a fine texture (so it has detail to be sharp
/// with). Luma 16–235, chroma 16–240.
/// </summary>
public static H264PcmPicture Photo(int seed = 1)
{
var random = new Random(seed);
var p = new H264PcmPicture(Width, Height);
for (var y = 0; y < Height; y++)
{
for (var x = 0; x < Width; x++)
{
var texture = ((x / 2) + (y / 2)) % 2 == 0 ? 6 : -6;
p.Y[(y * Width) + x] = Luma(40 + (x * 100 / Width) + (y * 60 / Height) + texture);
}
}
for (var y = 0; y < Height / 2; y++)
{
for (var x = 0; x < Width / 2; x++)
{
p.Cb[(y * Width / 2) + x] = (byte)(100 + (y * 50 / (Height / 2)));
p.Cr[(y * Width / 2) + x] = (byte)(110 + (x * 40 / (Width / 2)));
}
}
for (var shape = 0; shape < 6; shape++)
{
var (cx, cy, r) = (random.Next(Width), random.Next(Height), 12 + random.Next(24));
var (luma, cb, cr) = (random.Next(30, 220), random.Next(40, 220), random.Next(40, 220));
var round = shape % 2 == 0;
Paint(p, cx - r, cy - r, 2 * r, 2 * r, (x, y) => !round || ((x - cx) * (x - cx)) + ((y - cy) * (y - cy)) <= r * r ? (luma, cb, cr) : null);
}
return p;
}
/// <summary>A picture of one colour.</summary>
public static H264PcmPicture Flat(byte luma, byte cb = 128, byte cr = 128)
{
var p = new H264PcmPicture(Width, Height);
Array.Fill(p.Y, luma);
Array.Fill(p.Cb, cb);
Array.Fill(p.Cr, cr);
return p;
}
/// <summary>Black (limited range).</summary>
public static H264PcmPicture Black() => Flat(16);
/// <summary>A copy of <paramref name="source"/>.</summary>
public static H264PcmPicture Copy(H264PcmPicture source)
{
var p = new H264PcmPicture(source.Width, source.Height);
source.Y.CopyTo(p.Y, 0);
source.Cb.CopyTo(p.Cb, 0);
source.Cr.CopyTo(p.Cr, 0);
return p;
}
/// <summary>Grain: every luma sample moved by up to ± <paramref name="amplitude"/>, differently for each seed.</summary>
public static H264PcmPicture Noise(H264PcmPicture source, int amplitude, int seed)
{
var random = new Random(seed);
var p = Copy(source);
for (var i = 0; i < p.Y.Length; i++)
{
p.Y[i] = Luma(p.Y[i] + random.Next(-amplitude, amplitude + 1));
}
return p;
}
/// <summary>
/// The picture faded towards black by <paramref name="factor"/> (1: as it is, 0: black), as an editor fades: luma
/// towards 16, chroma towards 128.
/// </summary>
public static H264PcmPicture Fade(H264PcmPicture source, double factor)
{
var p = Copy(source);
for (var i = 0; i < p.Y.Length; i++)
{
p.Y[i] = (byte)Math.Round(16 + (factor * (p.Y[i] - 16)));
}
for (var i = 0; i < p.Cb.Length; i++)
{
p.Cb[i] = (byte)Math.Round(128 + (factor * (p.Cb[i] - 128)));
p.Cr[i] = (byte)Math.Round(128 + (factor * (p.Cr[i] - 128)));
}
return p;
}
/// <summary>
/// The picture with a sticker of <paramref name="width"/> × <paramref name="height"/> pixels at (x, y): a white and
/// magenta checkerboard whose phase <paramref name="phase"/> animates it in place.
/// </summary>
public static H264PcmPicture Sticker(H264PcmPicture source, int x, int y, int width, int height, int phase = 0)
{
var p = Copy(source);
Paint(p, x, y, width, height, (px, py) => (((px - x) / 4) + ((py - y) / 4) + phase) % 2 == 0 ? (235, 128, 128) : (90, 200, 220));
return p;
}
/// <summary>
/// The picture with a caption band <paramref name="height"/> pixels high from row <paramref name="top"/>: a dark band
/// with light «letters» that scroll left by <paramref name="offset"/> pixels.
/// </summary>
public static H264PcmPicture Caption(H264PcmPicture source, int top, int height, int offset)
{
var p = Copy(source);
Paint(p, 0, top, Width, height, (px, py) =>
{
var column = (px + offset) % 12;
var letter = column < 7 && (py - top) > 1 && (py - top) < height - 2 && ((px + offset) / 12 % 5) != 4;
return letter ? (230, 128, 128) : (30, 128, 128);
});
return p;
}
/// <summary>The picture moved right by <paramref name="dx"/> pixels (a pan), the left edge repeated.</summary>
public static H264PcmPicture Shift(H264PcmPicture source, int dx)
{
var p = new H264PcmPicture(source.Width, source.Height);
for (var y = 0; y < Height; y++)
{
for (var x = 0; x < Width; x++)
{
p.Y[(y * Width) + x] = source.Y[(y * Width) + Math.Clamp(x - dx, 0, Width - 1)];
}
}
for (var y = 0; y < Height / 2; y++)
{
for (var x = 0; x < Width / 2; x++)
{
var from = (y * Width / 2) + Math.Clamp(x - (dx / 2), 0, (Width / 2) - 1);
p.Cb[(y * Width / 2) + x] = source.Cb[from];
p.Cr[(y * Width / 2) + x] = source.Cr[from];
}
}
return p;
}
/// <summary>The picture with its luma blurred by a 3 × 3 box, as a strong compression softens it.</summary>
public static H264PcmPicture Blur(H264PcmPicture source)
{
var p = Copy(source);
for (var y = 0; y < Height; y++)
{
for (var x = 0; x < Width; x++)
{
var sum = 0;
for (var dy = -1; dy <= 1; dy++)
{
for (var dx = -1; dx <= 1; dx++)
{
sum += source.Y[(Math.Clamp(y + dy, 0, Height - 1) * Width) + Math.Clamp(x + dx, 0, Width - 1)];
}
}
p.Y[(y * Width) + x] = (byte)((sum + 4) / 9);
}
}
return p;
}
/// <summary>Paints the pixels of a rectangle (clipped to the picture) for which <paramref name="colour"/> gives a colour.</summary>
private static void Paint(H264PcmPicture p, int x, int y, int width, int height, Func<int, int, (int Luma, int Cb, int Cr)?> colour)
{
for (var py = Math.Max(0, y); py < Math.Min(Height, y + height); py++)
{
for (var px = Math.Max(0, x); px < Math.Min(Width, x + width); px++)
{
if (colour(px, py) is not { } c)
{
continue;
}
p.Y[(py * Width) + px] = Luma(c.Luma);
if (px % 2 == 0 && py % 2 == 0)
{
p.Cb[(py / 2 * Width / 2) + (px / 2)] = (byte)Math.Clamp(c.Cb, 16, 240);
p.Cr[(py / 2 * Width / 2) + (px / 2)] = (byte)Math.Clamp(c.Cr, 16, 240);
}
}
}
}
private static byte Luma(int value) => (byte)Math.Clamp(value, 16, 235);
}