Unione di funzione/codec-immagini in funzione/decoder-h264 (catena della 0.3.0)

Porta nel decodificatore H.264 le regole senza componenti esterni, la sessione S4b e i codec
di immagini di casa (C1), così la richiesta di unione di questo ramo contiene già tutta la
catena. Conflitti risolti tenendo le due parti: in decisioni.md tutte le righe, prima quelle
della catena e poi quelle del decodificatore; in docker.md, processo.md e Sviluppo.md né
ffmpeg (uscito con questo ramo) né SkiaSharp (uscito con C1), con l'eccezione «da fare» delle
prove di conformità; nelle prove di Febe.Tests entrambe le cartelle di file copiati e, nelle
prove delle miniature, sia i video sia le immagini di riferimento.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-25 11:16:26 +01:00
commit 8bcc86e496
279 files changed
+25617 -816

No files matched your search

+129 -10
View File
@@ -1,20 +1,25 @@
using System.Buffers.Binary;
using Febe.Core.Codecs.Images;
using Febe.Core.Library;
using Febe.Tests.Codecs.Containers;
using Febe.Tests.Imaging;
using Febe.Tests.Support;
using SkiaSharp;
using TestImages = Febe.Tests.Support.TestImages;
namespace Febe.Tests.Library;
[TestClass]
public sealed class ThumbnailStoreTests
{
private static SKImageInfo InfoOf(string path)
private static ImageInfo InfoOf(string path)
{
using var codec = SKCodec.Create(path) ?? throw new AssertFailedException("Miniatura non leggibile.");
Assert.AreEqual(SKEncodedImageFormat.Jpeg, codec.EncodedFormat);
return codec.Info;
var info = ImageDecoder.Identify(File.ReadAllBytes(path));
Assert.AreEqual(ImageFormat.Jpeg, info.Format);
return info;
}
private static RgbaImage Read(string path) => ImageDecoder.Decode(File.ReadAllBytes(path));
[TestMethod]
public void ThumbnailsAre480PixelsOnTheLongSideStoredOnceAndSharded()
{
@@ -69,17 +74,90 @@ public sealed class ThumbnailStoreTests
Assert.AreEqual(360, info.Height);
}
[TestMethod]
public void UprightAndTaggedSidewaysGiveTheSameThumbnail()
{
using var temp = new TempFolder();
var store = new ThumbnailStore(temp.Path);
using var scene = TestImages.Scene(4, 1200, 900);
using var sideways = TestImages.RotateCounterClockwise(scene);
Assert.IsTrue(store.Create(Make.Sha(9), new MemoryStream(TestImages.Png(scene))));
Assert.IsTrue(store.Create(Make.Sha(10), new MemoryStream(TestImages.WithExifOrientation(TestImages.Jpeg(sideways, 95), 6))));
using var upright = Read(store.PathFor(Make.Sha(9)));
using var turned = Read(store.PathFor(Make.Sha(10)));
var quality = ImageQuality.Compare(upright, turned);
Assert.IsGreaterThan(30.0, quality.MinPsnr, quality.ToString());
}
[TestMethod]
public void TransparentImagesBecomeJpegOnWhite()
{
using var temp = new TempFolder();
var store = new ThumbnailStore(temp.Path);
using var transparent = new SKBitmap(new SKImageInfo(600, 600, SKColorType.Rgba8888, SKAlphaType.Premul));
transparent.Erase(SKColors.Transparent);
using var transparent = new RgbaImage(600, 600);
Assert.IsTrue(store.Create(Make.Sha(4), new MemoryStream(TestImages.Png(transparent))));
using var thumbnail = SKBitmap.Decode(store.PathFor(Make.Sha(4)));
using var thumbnail = Read(store.PathFor(Make.Sha(4)));
var pixel = thumbnail.GetPixel(10, 10);
Assert.IsGreaterThan((byte)240, pixel.Red);
Assert.IsGreaterThan((byte)240, pixel.R);
}
[TestMethod]
public void TheThumbnailShowsThePictureReducedFaithfully()
{
// Compared with the source reduced by area averaging (the most neutral reduction), what differs is only the
// JPEG compression and the filter.
using var temp = new TempFolder();
var store = new ThumbnailStore(temp.Path);
using var scene = TestImages.Scene(8, 2400, 1800);
Assert.IsTrue(store.Create(Make.Sha(7), new MemoryStream(TestImages.Png(scene))));
using var thumbnail = Read(store.PathFor(Make.Sha(7)));
using var expected = ImageResampler.Resize(scene, 480, 360, new ResampleOptions { Filter = ResampleFilter.Area });
var quality = ImageQuality.Compare(expected, thumbnail);
Assert.IsGreaterThan(28.0, quality.MinPsnr, quality.ToString());
}
[TestMethod]
public void AJpegShrunkWhileDecodingGivesTheSameThumbnailAsAFullDecode()
{
// 4000 × 3000 is decoded at a quarter (1000 × 750) and then reduced to 480: compared with the full-size path.
using var temp = new TempFolder();
var store = new ThumbnailStore(temp.Path);
using var scene = TestImages.Scene(9, 4000, 3000);
var jpeg = TestImages.Jpeg(scene, 92);
Assert.IsTrue(store.Create(Make.Sha(8), new MemoryStream(jpeg)));
using var thumbnail = Read(store.PathFor(Make.Sha(8)));
Assert.AreEqual((480, 360), (thumbnail.Width, thumbnail.Height));
using var full = ImageDecoder.Decode(jpeg);
using var reference = ImageResampler.Resize(full, 480, 360);
var quality = ImageQuality.Compare(reference, thumbnail);
Assert.IsGreaterThan(30.0, quality.MinPsnr, quality.ToString());
}
[TestMethod]
public void VideoFramesAsBmpBecomeThumbnails()
{
// The in-house video decoder (branch funzione/decoder-h264) hands frames over as 24-bit BMP, rows bottom up.
using var temp = new TempFolder();
var store = new ThumbnailStore(temp.Path);
using var frame = TestImages.Scene(11, 1280, 720);
Assert.IsTrue(store.Create(Make.Sha(11), new MemoryStream(Bmp24(frame))));
var info = InfoOf(store.PathFor(Make.Sha(11)));
Assert.AreEqual((480, 270), (info.Width, info.Height));
using var thumbnail = Read(store.PathFor(Make.Sha(11)));
using var expected = ImageResampler.Resize(frame, 480, 270);
var quality = ImageQuality.Compare(expected, thumbnail);
Assert.IsGreaterThan(30.0, quality.MinPsnr, quality.ToString());
}
[TestMethod]
public void ThePhotoOfASearchIsASmallJpeg()
{
using var scene = TestImages.Scene(12, 1080, 1080);
var small = ThumbnailStore.EncodeSmallJpeg(new MemoryStream(TestImages.Jpeg(scene)), 160);
Assert.IsNotNull(small);
var info = ImageDecoder.Identify(small);
Assert.AreEqual((ImageFormat.Jpeg, 160, 160), (info.Format, info.Width, info.Height));
Assert.IsNull(ThumbnailStore.EncodeSmallJpeg(new MemoryStream([1, 2, 3]), 160));
}
[TestMethod]
@@ -101,8 +179,14 @@ public sealed class ThumbnailStoreTests
var tile = store.NeutralVideoTile();
Assert.AreEqual(Path.Combine(temp.Path, ThumbnailStore.NeutralVideoTileName), tile);
var info = InfoOf(tile);
Assert.AreEqual(480, info.Height);
Assert.AreEqual((270, 480), (info.Width, info.Height));
Assert.AreEqual(tile, store.NeutralVideoTile());
// Light grey, a darker disc in the middle, the play sign in the light grey again.
using var image = Read(tile);
AssertGrey(image.GetPixel(10, 10), 0xE4, "sfondo");
AssertGrey(image.GetPixel(135 - 25, 240), 0x9A, "disco");
AssertGrey(image.GetPixel(135 + 2, 240), 0xE4, "segno di riproduzione");
}
[TestMethod]
@@ -121,4 +205,39 @@ public sealed class ThumbnailStoreTests
Assert.IsNull(await VideoFrameDecoder.Instance.ExtractImageAsync(temp.Combine("non-esiste.mp4")));
}
private static void AssertGrey((byte R, byte G, byte B, byte A) pixel, int level, string what) =>
Assert.IsTrue(Math.Abs(pixel.R - level) <= 4 && Math.Abs(pixel.G - level) <= 4 && Math.Abs(pixel.B - level) <= 4, $"{what}: {pixel}");
/// <summary>A 24-bit BI_RGB BMP, rows bottom up, the way VideoImage.ToBmp writes it.</summary>
private static byte[] Bmp24(RgbaImage image)
{
var stride = ((image.Width * 3) + 3) & ~3;
var size = 54 + (stride * image.Height);
var bmp = new byte[size];
var span = bmp.AsSpan();
span[0] = (byte)'B';
span[1] = (byte)'M';
BinaryPrimitives.WriteInt32LittleEndian(span[2..], size);
BinaryPrimitives.WriteInt32LittleEndian(span[10..], 54);
BinaryPrimitives.WriteInt32LittleEndian(span[14..], 40);
BinaryPrimitives.WriteInt32LittleEndian(span[18..], image.Width);
BinaryPrimitives.WriteInt32LittleEndian(span[22..], image.Height);
BinaryPrimitives.WriteInt16LittleEndian(span[26..], 1);
BinaryPrimitives.WriteInt16LittleEndian(span[28..], 24);
BinaryPrimitives.WriteInt32LittleEndian(span[34..], stride * image.Height);
for (var y = 0; y < image.Height; y++)
{
var source = image.GetRow(y);
var target = span.Slice(54 + ((image.Height - 1 - y) * stride), image.Width * 3);
for (var x = 0; x < image.Width; x++)
{
target[x * 3] = source[(x * 4) + 2];
target[(x * 3) + 1] = source[(x * 4) + 1];
target[(x * 3) + 2] = source[x * 4];
}
}
return bmp;
}
}