Files
Febe/tests/Febe.Tests/Media/H264/H264PcmWriterTests.cs
T
Alby96andClaude Opus 5.5 ab0d717424 E3: API di formati e fotogrammi, conversione della libreria e demo
Porta le funzioni della tappa E3 fino all'interfaccia web, così che WP4
possa costruire le schermate su indirizzi veri.

- Impostazioni, cartelle delle copie e «Copia in…» hanno i formati
  (nomi original, jpg, png, webplossless, mp4); il riepilogo dice cosa si
  converte, cosa resta com'è e se c'è perdita. «Copia di nuovo» rifà la
  copia nella sua forma esatta.
- Fotogrammi: informazioni di un video, un fotogramma (anteprima o
  scaricamento), gli istanti di una modalità, «Scarica» di uno o di uno
  zip scritto mentre parte, con il permesso dei lavori lunghi oltre 20.
  Kestrel non vede mai scritture sincrone: il piccolo descrittore che lo
  zip di .NET 10 scrive in modo sincrono parte con la scrittura seguente.
- Conversione della libreria: stato, la domanda «Converti anche i N già
  in libreria?», riprova, pausa e ripresa; il visualizzatore mostra i
  file convertiti, e le eliminazioni li contano.
- Demo: un video H.264 scritto in casa (solo macroblocchi I_PCM) per
  ogni video, una copia in PNG, un fotogramma e due conversioni; la
  conversione non parte mai.

Due prove cambiano con il comportamento: i video della demo ora si
aprono, e un file non copiato può avere il segno «solo fotogramma».

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 01:02:37 +01:00

79 lines
3.2 KiB
C#

using Febe.Core.Codecs.H264;
using Febe.Core.Library;
namespace Febe.Tests.Codecs.H264;
/// <summary>
/// The small H.264 video the demo shows (tappa E3): PCM macroblocks only, so its frames decode to the samples written,
/// through the same reader «Fotogramma» uses.
/// </summary>
[TestClass]
public sealed class H264PcmWriterTests
{
[TestMethod]
public void TheDemoVideoIsTheSameEveryTime() =>
CollectionAssert.AreEqual(H264PcmWriter.DemoVideo(), H264PcmWriter.DemoVideo());
[TestMethod]
public void TheDemoVideoGivesItsFramesAtTheirInstants()
{
using var stream = new MemoryStream(H264PcmWriter.DemoVideo());
using var reader = VideoFrameReader.Open(stream, out var info);
Assert.IsTrue(info.Available, info.Unavailable);
Assert.AreEqual((H264PcmWriter.DemoWidth, H264PcmWriter.DemoHeight), (info.Width, info.Height));
Assert.AreEqual(H264PcmWriter.DemoPictures, info.SampleCount);
Assert.AreEqual(TimeSpan.FromSeconds(2), info.Duration);
Assert.IsFalse(info.HasAudio);
// 8 pictures per second: 1.234 s shows picture 9 (shown from 1.125 s).
var targets = reader.Locate([TimeSpan.FromMilliseconds(1234), TimeSpan.Zero]);
Assert.AreEqual(TimeSpan.FromMilliseconds(1125), targets[0].Actual);
Assert.AreEqual(9, targets[0].Sample);
var frames = reader.Decode(targets, CancellationToken.None).ToList();
try
{
Assert.HasCount(2, frames);
foreach (var frame in frames)
{
var expected = H264PcmWriter.DemoPicture(frame.Index == 0 ? 9 : 0);
var pixels = frame.Image.ToArray();
var width = H264PcmWriter.DemoWidth;
foreach (var (x, y) in new[] { (0, 0), (17, 33), (width - 1, H264PcmWriter.DemoHeight - 1), (120, 200) })
{
var (r, g, b) = Bt601(expected.Y[(y * width) + x], expected.Cb[((y / 2) * (width / 2)) + (x / 2)], expected.Cr[((y / 2) * (width / 2)) + (x / 2)]);
var at = ((y * width) + x) * 4;
Assert.IsLessThanOrEqualTo(3, Math.Abs(pixels[at] - r), $"R at {x},{y}");
Assert.IsLessThanOrEqualTo(3, Math.Abs(pixels[at + 1] - g), $"G at {x},{y}");
Assert.IsLessThanOrEqualTo(3, Math.Abs(pixels[at + 2] - b), $"B at {x},{y}");
Assert.AreEqual((byte)255, pixels[at + 3]);
}
}
}
finally
{
frames.ForEach(f => f.Dispose());
}
}
[TestMethod]
public void TheSamplesNeverContainAStartCode()
{
var nal = H264PcmWriter.Idr(H264PcmWriter.DemoPicture(3), 1);
for (var i = 0; i + 2 < nal.Length; i++)
{
Assert.IsFalse(nal[i] == 0 && nal[i + 1] == 0 && nal[i + 2] <= 3 && nal[i + 2] != 3, $"Start code at {i}");
}
}
private static (int R, int G, int B) Bt601(byte y, byte cb, byte cr)
{
var c = 1.164 * (y - 16);
var d = cb - 128;
var e = cr - 128;
static int Clamp(double v) => (int)Math.Clamp(Math.Round(v), 0, 255);
return (Clamp(c + (1.596 * e)), Clamp(c - (0.392 * d) - (0.813 * e)), Clamp(c + (2.017 * d)));
}
}