using Febe.Core.Codecs.H264;
namespace Febe.Tests.Codecs.Containers;
///
/// Test videos built from pictures whose every pixel is known (I_PCM H.264, ), 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 , so
/// audio and rotation come along; nothing comes from Instagram or from a real file.
///
internal static class PcmVideos
{
public const int Width = 144;
public const int Height = 256;
/// Pictures a second ( / ).
public const int Fps = 30;
/// An elementary stream of IDR pictures, picture i given by .
public static ElementaryStream Stream(string name, int count, Func 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());
}
/// An MP4 of pictures at , optionally rotated and with an audio track.
public static byte[] Mp4(int count, Func picture, int rotation = 0, bool audio = false) =>
TestVideos.Mp4(Stream("pcm", count, picture), rotation: rotation, audio: audio);
///
/// An MP4 of pictures of × (multiples of
/// 16), one a second: for what depends on the size of the picture.
///
public static byte[] Large(int width, int height, int count, Func picture)
{
using var output = new MemoryStream();
H264PcmWriter.WriteMp4(output, width, height, count, 1, picture);
return output.ToArray();
}
/// A grey picture of any size (multiples of 16) whose luma at (x, y) is given, clamped to 16–235.
public static H264PcmPicture Grey(int width, int height, Func 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;
}
/// A poster: one colour, and a photo on the bottom of it.
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;
}
///
/// 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.
///
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;
}
/// A picture of one colour.
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;
}
/// Black (limited range).
public static H264PcmPicture Black() => Flat(16);
/// A copy of .
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;
}
/// Grain: every luma sample moved by up to ± , differently for each seed.
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;
}
///
/// The picture faded towards black by (1: as it is, 0: black), as an editor fades: luma
/// towards 16, chroma towards 128.
///
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;
}
///
/// The picture with a sticker of × pixels at (x, y): a white and
/// magenta checkerboard whose phase animates it in place.
///
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;
}
///
/// The picture with a caption band pixels high from row : a dark band
/// with light «letters» that scroll left by pixels.
///
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;
}
/// The picture moved right by pixels (a pan), the left edge repeated.
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;
}
/// The picture with its luma blurred by a 3 × 3 box, as a strong compression softens it.
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;
}
/// Paints the pixels of a rectangle (clipped to the picture) for which gives a colour.
private static void Paint(H264PcmPicture p, int x, int y, int width, int height, Func 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);
}