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>
107 lines
4.2 KiB
C#
107 lines
4.2 KiB
C#
namespace Febe.Core.Codecs.Containers;
|
|
|
|
/// <summary>
|
|
/// Recognises the container of a video by its first bytes (MP4 and MOV, Matroska and WebM, AVI, ASF and WMV, FLV, MPEG
|
|
/// program stream) and reads it with the matching reader.
|
|
/// </summary>
|
|
public static class ContainerReader
|
|
{
|
|
/// <summary>
|
|
/// A «now» far in the future, for readers that must not depend on the clock (frames, remux): every plausible date
|
|
/// in the file is accepted.
|
|
/// </summary>
|
|
internal static readonly DateTimeOffset AnyDate = new(9000, 1, 1, 0, 0, 0, TimeSpan.Zero);
|
|
|
|
/// <summary>The largest sample read into memory: an 8K frame of H.264 at its highest level stays far below.</summary>
|
|
public const int MaxSampleSize = 64 * 1024 * 1024;
|
|
|
|
/// <summary>The format of a file from its first bytes; null when it is none of those read here.</summary>
|
|
public static ContainerFormat? Detect(ReadOnlySpan<byte> start)
|
|
{
|
|
if (MatroskaReader.Looks(start))
|
|
{
|
|
return ContainerFormat.Matroska;
|
|
}
|
|
|
|
if (AviReader.Looks(start))
|
|
{
|
|
return ContainerFormat.Avi;
|
|
}
|
|
|
|
if (AsfReader.Looks(start))
|
|
{
|
|
return ContainerFormat.Asf;
|
|
}
|
|
|
|
if (FlvReader.Looks(start))
|
|
{
|
|
return ContainerFormat.Flv;
|
|
}
|
|
|
|
if (MpegPsReader.Looks(start))
|
|
{
|
|
return ContainerFormat.MpegPs;
|
|
}
|
|
|
|
return IsoBmffReader.Looks(start) ? ContainerFormat.IsoBmff : null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads a seekable stream; null when its format is not recognised. Damaged files raise
|
|
/// <see cref="ContainerException"/> (or <see cref="EndOfStreamException"/> when truncated); <paramref name="ct"/> is
|
|
/// checked between the top-level boxes, elements and fragments.
|
|
/// </summary>
|
|
public static MediaContainer? Read(Stream stream, DateTimeOffset now, bool samples = true, CancellationToken ct = default)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(stream);
|
|
Span<byte> start = stackalloc byte[16];
|
|
stream.Position = 0;
|
|
var read = stream.ReadAtLeast(start, start.Length, throwOnEndOfStream: false);
|
|
stream.Position = 0;
|
|
return Detect(start[..read]) switch
|
|
{
|
|
ContainerFormat.Matroska => MatroskaReader.Read(stream, now, samples, ct),
|
|
ContainerFormat.Avi => AviReader.Read(stream, now, samples, ct),
|
|
ContainerFormat.Asf => AsfReader.Read(stream, now, samples, ct),
|
|
ContainerFormat.Flv => FlvReader.Read(stream, now, samples, ct),
|
|
ContainerFormat.MpegPs => MpegPsReader.Read(stream, now, samples, ct),
|
|
ContainerFormat.IsoBmff => IsoBmffReader.Read(stream, now, samples, ct),
|
|
_ => null,
|
|
};
|
|
}
|
|
|
|
/// <summary>Reads a file (read-only, shared); null when its format is not recognised.</summary>
|
|
public static MediaContainer? ReadFile(string path, DateTimeOffset now, bool samples = true, CancellationToken ct = default)
|
|
{
|
|
using var stream = OpenRead(path);
|
|
return Read(stream, now, samples, ct);
|
|
}
|
|
|
|
/// <summary>Opens a file for reading without blocking other readers or writers.</summary>
|
|
public static FileStream OpenRead(string path) =>
|
|
new(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, 64 * 1024, FileOptions.RandomAccess);
|
|
|
|
/// <summary>The bytes of a sample, read into <paramref name="buffer"/> (grown when needed).</summary>
|
|
public static ReadOnlySpan<byte> ReadSample(Stream stream, in MediaSample sample, ref byte[] buffer)
|
|
{
|
|
if (sample.Size < 0 || sample.Offset < 0 || sample.Offset + sample.Size > stream.Length)
|
|
{
|
|
throw new ContainerException("Campione fuori dal file.");
|
|
}
|
|
|
|
if (sample.Size > MaxSampleSize)
|
|
{
|
|
throw new ContainerException("Campione troppo grande."); // no real frame comes near: a damaged table
|
|
}
|
|
|
|
if (buffer.Length < sample.Size)
|
|
{
|
|
buffer = new byte[Math.Max(sample.Size, buffer.Length * 2)];
|
|
}
|
|
|
|
stream.Position = sample.Offset;
|
|
stream.ReadExactly(buffer, 0, sample.Size);
|
|
return buffer.AsSpan(0, sample.Size);
|
|
}
|
|
}
|