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:
Alby96andClaude Opus 5.5 committed 2026-11-27 22:30:23 +01:00
1 parent 7faf92e667
commit 704c1ed9cf
28 files changed
+2830 -144

No files matched your search

@@ -107,6 +107,9 @@ public static class MatroskaReader
case TracksId:
ParseTracks(reader, bodyEnd, state);
break;
case TagsId:
state.HasTags = true;
break;
case ClusterId:
if (!samples)
{
@@ -214,6 +217,13 @@ public static class MatroskaReader
case 0x23E383:
track.DefaultDuration = (long)reader.Unsigned(tsize);
break;
case 0x22B59C:
track.Language = reader.String(tsize);
break;
case 0x6D80: // ContentEncodings: the frames are compressed, stripped or encrypted
track.Encoded = true;
reader.Skip(tsize);
break;
case 0x55EE: // MaxBlockAdditionID, and others: ignored
reader.Skip(tsize);
break;
@@ -289,6 +299,7 @@ public static class MatroskaReader
}
stream.Position = Math.Min(entryEnd, stream.Length);
state.TrackCount++;
if (track.Type == 1 && state.Video is null)
{
state.Video = track;
@@ -498,6 +509,8 @@ public static class MatroskaReader
internal double Roll;
internal int SampleRate;
internal int Channels;
internal string? Language;
internal bool Encoded;
internal readonly List<MediaSample> Samples = [];
}
@@ -509,6 +522,12 @@ public static class MatroskaReader
internal TrackEntry? Video;
internal TrackEntry? Audio;
/// <summary>Every TrackEntry of the file.</summary>
internal int TrackCount;
/// <summary>True when the segment has a Tags element.</summary>
internal bool HasTags;
/// <summary>Nanoseconds of one unit of the sample times; fixed by the first block, 0 before.</summary>
private long unit;
@@ -599,12 +618,15 @@ public static class MatroskaReader
Timescale = timescale,
Duration = trackUnits,
Samples = samples,
Language = Language(v.Language),
Encoded = v.Encoded,
};
}
AudioTrack? audio = null;
if (Audio is { } a)
{
var asc = a.CodecId == "A_AAC" ? a.CodecPrivate : a.CodecId.StartsWith("A_AAC/", StringComparison.Ordinal) ? LegacyAacConfig(a) : null;
audio = new AudioTrack
{
Codec = a.CodecId switch
@@ -619,16 +641,91 @@ public static class MatroskaReader
CodecTag = a.CodecId,
SampleRate = a.SampleRate,
Channels = a.Channels,
AacConfiguration = a.CodecId == "A_AAC" ? a.CodecPrivate : null,
AacConfiguration = asc,
AudioObjectType = AudioTrack.ObjectTypeOf(asc),
Timescale = timescale,
Duration = trackUnits,
Samples = a.Samples,
Language = Language(a.Language),
Encoded = a.Encoded,
};
}
var seconds = DurationTicks * TimecodeScale / 1e9;
TimeSpan? duration = seconds is > 0 and < 1e9 ? TimeSpan.FromSeconds(seconds) : null;
return new MediaContainer { Format = ContainerFormat.Matroska, CreatedAt = Created, Duration = duration, Video = video, Audio = audio };
return new MediaContainer
{
Format = ContainerFormat.Matroska,
CreatedAt = Created,
Duration = duration,
Video = video,
Audio = audio,
TrackCount = TrackCount,
HasUserData = HasTags,
};
}
/// <summary>A three-letter ISO 639-2 language; null for «und», absent or malformed.</summary>
private static string? Language(string? code) =>
code is { Length: 3 } && code != "und" && code.All(char.IsAsciiLetterLower) ? code : null;
/// <summary>
/// The AudioSpecificConfig (ISO/IEC 14496-3 §1.6.2.1) of a track with an old codec ID that names the profile
/// («A_AAC/MPEG4/LC», «A_AAC/MPEG2/MAIN», «…/SBR»...) instead of carrying the configuration in CodecPrivate:
/// object type, sampling frequency index (or the explicit frequency), channel configuration, then a
/// GASpecificConfig of zeros (1024-sample frames, no core coder, no extension).
/// </summary>
private static byte[]? LegacyAacConfig(TrackEntry track)
{
if (track.CodecPrivate is { Length: >= 2 })
{
return track.CodecPrivate;
}
var profile = track.CodecId[(track.CodecId.LastIndexOf('/') + 1)..];
var objectType = profile switch
{
"MAIN" => 1,
"LC" or "SBR" => 2, // SBR is signalled implicitly: the core rate in SamplingFrequency
"SSR" => 3,
"LTP" => 4,
_ => 0,
};
if (objectType == 0 || track.SampleRate <= 0 || track.Channels is < 1 or > 7)
{
return null;
}
int[] rates = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
var index = Array.IndexOf(rates, track.SampleRate);
var bits = new List<(int Value, int Count)> { (objectType, 5) };
if (index >= 0)
{
bits.Add((index, 4));
}
else
{
bits.Add((15, 4));
bits.Add((track.SampleRate & 0xFFFFFF, 24));
}
bits.Add((track.Channels, 4));
bits.Add((0, 3));
var total = bits.Sum(b => b.Count);
var config = new byte[(total + 7) / 8];
var position = 0;
foreach (var (value, count) in bits)
{
for (var i = count - 1; i >= 0; i--, position++)
{
if (((value >> i) & 1) != 0)
{
config[position / 8] |= (byte)(0x80 >> (position % 8));
}
}
}
return config;
}
}