E3: conversione delle foto, rimux in MP4 e fotogrammi esatti nel nucleo
Prima parte della tappa E3 (formati e fotogrammi), solo nel nucleo: il pianificatore, il copiatore e l'archivio delle esportazioni restano alla parte successiva. - Foto: ImageConverter.Check dice prima che cosa succede (copia identica, conversione, copia così com'è con il motivo: HEIC, AVIF, raw, animazioni, file danneggiati, CMYK e oltre 8 bit verso PNG/WEBP, oltre il limite di pixel); la conversione riporta come note ciò che non può portare invece di fermarsi, e la trasparenza verso JPG diventa bianco. I lettori segnalano animazioni, CMYK e segmenti non letti. VP8L usa un logaritmo in virgola fissa, così lo stesso file dà gli stessi byte su ogni sistema. - Video: i lettori ISO e Matroska leggono elenco dei montaggi, lingue, tipo di AAC, MP3 e i vecchi codici AAC di Matroska; Mp4Writer prepara la disposizione e ne conosce la lunghezza prima di scrivere; il nuovo Mp4Remuxer porta in MP4 senza ricodificare, sempre con gli stessi byte. - Fotogrammi: VideoFrameReader trova il campione mostrato a un istante come lo mostra il lettore, lo decodifica esatto a piena risoluzione e lo raddrizza; ExportFrames calcola istanti, suffissi e etichette. - Nomi: il suffisso del fotogramma non si accorcia mai e la numerazione dei nomi liberi viene dopo di lui; SafeFileWriter scrive anche contenuti prodotti mentre li scrive. - MediaConverter e ConversionThrottle preparano una conversione senza sapere dove andrà, una alla volta in tutto il processo. Nuove prove per determinismo, istanti, rotazione, rimux e nomi; tutte le prove di Febe.Tests passano. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7faf92e667
commit
704c1ed9cf
28 files changed
+2830
-144
No files matched your search
@@ -0,0 +1,153 @@
|
||||
using System.Security.Cryptography;
|
||||
using Febe.Core.Codecs.Containers;
|
||||
using Febe.Core.Codecs.H264;
|
||||
using Febe.Core.Export;
|
||||
using Febe.Core.Library;
|
||||
|
||||
namespace Febe.Tests.Codecs.Containers;
|
||||
|
||||
/// <summary>
|
||||
/// Frames at chosen instants (tappa E3, D12): the sample shown at an instant, exact decoding at full size, rotation,
|
||||
/// and the remux into MP4 (D9: the same bytes every time).
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public sealed class VideoFramesTests
|
||||
{
|
||||
[TestMethod]
|
||||
[DataRow("mp4")]
|
||||
[DataRow("mkv")]
|
||||
public void TheFrameAtAnInstantIsTheOneShown(string kind)
|
||||
{
|
||||
var es = ElementaryStream.Load("CABA3_SVA_B");
|
||||
var data = kind == "mkv" ? TestVideos.Matroska(es) : TestVideos.Mp4(es);
|
||||
using var stream = new MemoryStream(data);
|
||||
using var reader = VideoFrameReader.Open(stream, out var info);
|
||||
Assert.IsTrue(info.Available);
|
||||
Assert.AreEqual((es.Width, es.Height), (info.Width, info.Height));
|
||||
|
||||
// 30 frames per second: 0.51 s shows frame 15 (shown at 0.5 s), 0.499 s still frame 14. The Matroska file starts
|
||||
// showing later by the reordering delay of its B frames (its block times are presentation times as written).
|
||||
var shift = kind == "mkv" ? Enumerable.Range(0, es.Count).Max(i => i - es.Rank[i]) : 0;
|
||||
var targets = reader.Locate([TimeSpan.FromMilliseconds(510), TimeSpan.FromMilliseconds(499)]);
|
||||
Assert.AreEqual(TimeSpan.FromMilliseconds(500), targets[0].Actual);
|
||||
Assert.AreEqual(500L, ExportFrames.ActualMs(targets[0].Actual));
|
||||
Assert.IsLessThan(TimeSpan.FromMilliseconds(500), targets[1].Actual);
|
||||
|
||||
var frames = reader.Decode(targets, CancellationToken.None).ToList();
|
||||
try
|
||||
{
|
||||
Assert.HasCount(2, frames);
|
||||
foreach (var (frame, rank) in new[] { (frames.Single(f => f.Index == 0), 15 - shift), (frames.Single(f => f.Index == 1), 14 - shift) })
|
||||
{
|
||||
var expected = VideoFrameDecoder.ToImage(ShownFrame(es, rank), 0, 0);
|
||||
Assert.AreEqual((expected.Width, expected.Height), (frame.Image.Width, frame.Image.Height));
|
||||
var pixels = frame.Image.ToArray();
|
||||
for (var i = 0; i < expected.Width * expected.Height; i++)
|
||||
{
|
||||
Assert.AreEqual(expected.Rgb[i * 3], pixels[i * 4]);
|
||||
Assert.AreEqual(expected.Rgb[(i * 3) + 1], pixels[(i * 4) + 1]);
|
||||
Assert.AreEqual(expected.Rgb[(i * 3) + 2], pixels[(i * 4) + 2]);
|
||||
Assert.AreEqual((byte)255, pixels[(i * 4) + 3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
frames.ForEach(f => f.Dispose());
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void BeforeTheStartAndPastTheEndGiveTheFirstAndLastFrames()
|
||||
{
|
||||
var es = ElementaryStream.Load("CABA3_SVA_B");
|
||||
using var stream = new MemoryStream(TestVideos.Mp4(es));
|
||||
using var reader = VideoFrameReader.Open(stream, out var info);
|
||||
|
||||
var targets = reader.Locate([TimeSpan.Zero, TimeSpan.FromHours(1), info.Duration]);
|
||||
|
||||
Assert.AreEqual(Array.IndexOf(es.Rank, 0), targets[0].Sample);
|
||||
Assert.AreEqual(Array.IndexOf(es.Rank, es.Count - 1), targets[1].Sample);
|
||||
Assert.AreEqual(targets[1].Sample, targets[2].Sample);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ReRequestingTheActualMsGivesTheSameFrame()
|
||||
{
|
||||
var es = ElementaryStream.Load("CABA3_SVA_B");
|
||||
using var stream = new MemoryStream(TestVideos.Matroska(es));
|
||||
using var reader = VideoFrameReader.Open(stream, out var info);
|
||||
var instants = ExportFrames.Spread(info.Duration, 7);
|
||||
|
||||
var first = reader.Locate(instants);
|
||||
var again = reader.Locate([.. first.Select(t => TimeSpan.FromMilliseconds(ExportFrames.ActualMs(t.Actual)))]);
|
||||
|
||||
CollectionAssert.AreEqual(first.Select(t => t.Sample).ToArray(), again.Select(t => t.Sample).ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void RotationIsApplied()
|
||||
{
|
||||
var es = ElementaryStream.Load("BA1_Sony_D");
|
||||
using var stream = new MemoryStream(TestVideos.Mp4(es, rotation: 90));
|
||||
using var reader = VideoFrameReader.Open(stream, out var info);
|
||||
Assert.AreEqual((es.Height, es.Width), (info.Width, info.Height));
|
||||
|
||||
using var frame = reader.Decode(reader.Locate([TimeSpan.Zero]), CancellationToken.None).Single();
|
||||
|
||||
Assert.AreEqual((es.Height, es.Width), (frame.Image.Width, frame.Image.Height));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AnUnreadableVideoSaysNotAvailable()
|
||||
{
|
||||
using var stream = new MemoryStream(new byte[4096]);
|
||||
using var reader = VideoFrameReader.Open(stream, out var info);
|
||||
|
||||
Assert.IsFalse(info.Available);
|
||||
Assert.AreEqual(VideoFrameReader.NotAvailableMessage, info.Unavailable);
|
||||
Assert.IsEmpty(reader.Locate([TimeSpan.Zero]));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void RemuxingTwiceGivesTheSameBytesOfThePlannedLength()
|
||||
{
|
||||
var es = ElementaryStream.Load("CABA3_SVA_B");
|
||||
var mkv = TestVideos.Matroska(es, new DateTimeOffset(2024, 1, 2, 3, 4, 5, TimeSpan.Zero));
|
||||
using var source = new MemoryStream(mkv);
|
||||
|
||||
Assert.AreEqual(RemuxRefusal.None, Mp4Remuxer.Check(source, "video.mkv", out var plan, CancellationToken.None));
|
||||
using var first = new MemoryStream();
|
||||
using var second = new MemoryStream();
|
||||
Mp4Remuxer.Write(plan!, source, first, CancellationToken.None);
|
||||
Mp4Remuxer.Write(plan!, source, second, CancellationToken.None);
|
||||
|
||||
Assert.AreEqual(plan!.OutputLength, first.Length);
|
||||
CollectionAssert.AreEqual(SHA256.HashData(first.ToArray()), SHA256.HashData(second.ToArray()));
|
||||
|
||||
// The MP4 shows the same frames at the same instants as the Matroska file.
|
||||
first.Position = 0;
|
||||
using var remuxed = VideoFrameReader.Open(first, out var info);
|
||||
Assert.IsTrue(info.Available);
|
||||
using var original = VideoFrameReader.Open(source, out _);
|
||||
var instants = ExportFrames.Every(info.Duration, TimeSpan.FromMilliseconds(250));
|
||||
CollectionAssert.AreEqual(
|
||||
original.Locate(instants).Select(t => t.Actual).ToArray(),
|
||||
remuxed.Locate(instants).Select(t => t.Actual).ToArray());
|
||||
|
||||
// An MP4 named .mp4 is already what was asked.
|
||||
using var mp4 = new MemoryStream(TestVideos.Mp4(es));
|
||||
Assert.AreEqual(RemuxRefusal.AlreadyMp4, Mp4Remuxer.Check(mp4, "video.mp4", out _, CancellationToken.None));
|
||||
}
|
||||
|
||||
/// <summary>The frame shown at position <paramref name="rank"/>, decoded straight from the stream.</summary>
|
||||
private static H264Frame ShownFrame(ElementaryStream es, int rank)
|
||||
{
|
||||
var decoder = new H264Decoder();
|
||||
var frames = new List<H264Frame>();
|
||||
decoder.FrameDecoded += frames.Add;
|
||||
decoder.DecodeAnnexB(ElementaryStream.AnnexB(es.Name));
|
||||
var index = Array.IndexOf(es.Rank, rank);
|
||||
return frames.Single(f => f.DecodeIndex == index);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user