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
@@ -39,6 +39,7 @@ public static class IsoBmffReader
|
||||
long position = 0;
|
||||
Span<byte> header = stackalloc byte[16];
|
||||
Movie? movie = null;
|
||||
string? brand = null;
|
||||
var pending = new List<(long Start, byte[] Body)>();
|
||||
for (var i = 0; i < MaxTopLevelBoxes && position + 8 <= length; i++)
|
||||
{
|
||||
@@ -75,6 +76,12 @@ public static class IsoBmffReader
|
||||
}
|
||||
|
||||
var type = header.Slice(4, 4);
|
||||
if (type.SequenceEqual("ftyp"u8) && brand is null && size - headerSize >= 4)
|
||||
{
|
||||
stream.ReadExactly(header[..4]); // the size field is read already: its bytes can hold the brand
|
||||
brand = System.Text.Encoding.Latin1.GetString(header[..4]);
|
||||
}
|
||||
|
||||
if (type.SequenceEqual("moov"u8) && movie is null)
|
||||
{
|
||||
movie = ParseMoov(ReadBody(stream, position + headerSize, size - headerSize), now, samples, length);
|
||||
@@ -112,7 +119,7 @@ public static class IsoBmffReader
|
||||
throw new ContainerException("Manca il box moov.");
|
||||
}
|
||||
|
||||
return movie.Build();
|
||||
return movie.Build(brand);
|
||||
}
|
||||
|
||||
private static byte[] ReadBody(Stream stream, long offset, long count)
|
||||
@@ -141,7 +148,11 @@ public static class IsoBmffReader
|
||||
case "mvhd":
|
||||
movie.ParseMvhd(body, now);
|
||||
break;
|
||||
case "udta" or "meta":
|
||||
movie.HasUserData = true;
|
||||
break;
|
||||
case "trak":
|
||||
movie.TrackCount++;
|
||||
try
|
||||
{
|
||||
movie.ParseTrak(moov, start, count);
|
||||
@@ -269,6 +280,11 @@ public static class IsoBmffReader
|
||||
internal byte[]? Hvcc;
|
||||
internal byte[]? Asc;
|
||||
internal long EditOffset;
|
||||
internal bool HasEdits;
|
||||
internal long EmptyEditMovie;
|
||||
internal int EditCount;
|
||||
internal string? Language;
|
||||
internal int ObjectType;
|
||||
internal List<MediaSample> Samples = [];
|
||||
internal long FragmentTime = -1;
|
||||
}
|
||||
@@ -279,6 +295,7 @@ public static class IsoBmffReader
|
||||
internal readonly Dictionary<uint, (uint Duration, uint Size, uint Flags)> TrackDefaults = [];
|
||||
private readonly List<Track> tracks = [];
|
||||
private DateTimeOffset? created;
|
||||
private DateTimeOffset? modified;
|
||||
private long timescale;
|
||||
private long duration;
|
||||
|
||||
@@ -288,6 +305,12 @@ public static class IsoBmffReader
|
||||
/// <summary>fragment_duration of the <c>mehd</c> box, in movie units; 0 when absent.</summary>
|
||||
internal long FragmentDuration;
|
||||
|
||||
/// <summary>The <c>trak</c> boxes of the movie, of every kind.</summary>
|
||||
internal int TrackCount;
|
||||
|
||||
/// <summary>True when the movie has a <c>udta</c> or <c>meta</c> box.</summary>
|
||||
internal bool HasUserData;
|
||||
|
||||
/// <summary>A fragmented movie whose headers give no duration: only its fragments tell it.</summary>
|
||||
internal bool NeedsFragmentsForDuration =>
|
||||
Fragmented && duration <= 0 && FragmentDuration <= 0 && !tracks.Exists(t => t.Handler == "vide" && t.Duration > 0);
|
||||
@@ -295,15 +318,18 @@ public static class IsoBmffReader
|
||||
internal void ParseMvhd(ReadOnlySpan<byte> body, DateTimeOffset now)
|
||||
{
|
||||
ulong creation;
|
||||
ulong modification;
|
||||
if (body[0] == 1)
|
||||
{
|
||||
creation = BinaryPrimitives.ReadUInt64BigEndian(body[4..]);
|
||||
modification = BinaryPrimitives.ReadUInt64BigEndian(body[12..]);
|
||||
timescale = BinaryPrimitives.ReadUInt32BigEndian(body[20..]);
|
||||
duration = (long)Math.Min(BinaryPrimitives.ReadUInt64BigEndian(body[24..]), long.MaxValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
creation = BinaryPrimitives.ReadUInt32BigEndian(body[4..]);
|
||||
modification = BinaryPrimitives.ReadUInt32BigEndian(body[8..]);
|
||||
timescale = BinaryPrimitives.ReadUInt32BigEndian(body[12..]);
|
||||
duration = BinaryPrimitives.ReadUInt32BigEndian(body[16..]);
|
||||
if (duration == uint.MaxValue)
|
||||
@@ -312,14 +338,22 @@ public static class IsoBmffReader
|
||||
}
|
||||
}
|
||||
|
||||
if (creation > 0 && creation < 400UL * 366 * 86400)
|
||||
created = Plausible(creation, now);
|
||||
modified = Plausible(modification, now);
|
||||
}
|
||||
|
||||
private static DateTimeOffset? Plausible(ulong seconds, DateTimeOffset now)
|
||||
{
|
||||
if (seconds > 0 && seconds < 400UL * 366 * 86400)
|
||||
{
|
||||
var instant = Epoch1904.AddSeconds(creation);
|
||||
var instant = Epoch1904.AddSeconds(seconds);
|
||||
if (instant >= Earliest && instant <= now.AddDays(1))
|
||||
{
|
||||
created = instant;
|
||||
return instant;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
internal void ParseTrak(byte[] data, int start, int count)
|
||||
@@ -365,15 +399,33 @@ public static class IsoBmffReader
|
||||
var v1 = body[0] == 1;
|
||||
var entries = BinaryPrimitives.ReadUInt32BigEndian(body[4..]);
|
||||
var offset = 8;
|
||||
var leading = true;
|
||||
for (var i = 0; i < entries && i < 16; i++)
|
||||
{
|
||||
var segment = v1 ? (long)Math.Min(BinaryPrimitives.ReadUInt64BigEndian(body[offset..]), long.MaxValue / 4) : BinaryPrimitives.ReadUInt32BigEndian(body[offset..]);
|
||||
long mediaTime = v1 ? BinaryPrimitives.ReadInt64BigEndian(body[(offset + 8)..]) : BinaryPrimitives.ReadInt32BigEndian(body[(offset + 4)..]);
|
||||
var rate = BinaryPrimitives.ReadInt16BigEndian(body[(offset + (v1 ? 16 : 8))..]);
|
||||
offset += v1 ? 20 : 12;
|
||||
if (mediaTime >= 0)
|
||||
if (leading && mediaTime < 0)
|
||||
{
|
||||
track.EditOffset = mediaTime; // the first non-empty edit: media time shown at presentation zero
|
||||
return;
|
||||
track.EmptyEditMovie += segment; // a delayed start
|
||||
continue;
|
||||
}
|
||||
|
||||
if (leading)
|
||||
{
|
||||
leading = false;
|
||||
track.HasEdits = true;
|
||||
track.EditOffset = mediaTime; // the first non-empty edit: media time shown at presentation zero
|
||||
}
|
||||
|
||||
// A rate other than 1 (a slowed or still segment) is not an ordinary edit either.
|
||||
track.EditCount += rate == 1 ? 1 : 2;
|
||||
}
|
||||
|
||||
if (entries > 16)
|
||||
{
|
||||
track.EditCount += 2;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -397,6 +449,7 @@ public static class IsoBmffReader
|
||||
track.Duration = d == uint.MaxValue ? 0 : d;
|
||||
}
|
||||
|
||||
track.Language = Language(body[0] == 1 ? body[32..] : body[20..]);
|
||||
break;
|
||||
case "hdlr":
|
||||
track.Handler = System.Text.Encoding.Latin1.GetString(body.Slice(8, 4));
|
||||
@@ -478,6 +531,39 @@ public static class IsoBmffReader
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The ISO 639-2/T code packed in an <c>mdhd</c> (three letters of five bits each); null for «und», for a
|
||||
/// QuickTime Macintosh language code (below 0x400) or for a damaged value.
|
||||
/// </summary>
|
||||
private static string? Language(ReadOnlySpan<byte> field)
|
||||
{
|
||||
if (field.Length < 2)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var packed = BinaryPrimitives.ReadUInt16BigEndian(field) & 0x7FFF;
|
||||
if (packed < 0x400)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
Span<char> code = stackalloc char[3];
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
var letter = (packed >> (10 - (5 * i))) & 0x1F;
|
||||
if (letter is < 1 or > 26)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
code[i] = (char)('a' + letter - 1);
|
||||
}
|
||||
|
||||
var text = new string(code);
|
||||
return text == "und" ? null : text;
|
||||
}
|
||||
|
||||
private static void ParseStsd(byte[] data, int start, int count, Track track)
|
||||
{
|
||||
if (count < 16)
|
||||
@@ -524,16 +610,31 @@ public static class IsoBmffReader
|
||||
{
|
||||
if (t == "esds")
|
||||
{
|
||||
track.Asc = EsdsConfig(data.AsSpan(s, c));
|
||||
(track.ObjectType, track.Asc) = EsdsConfig(data.AsSpan(s, c));
|
||||
}
|
||||
else if (t == "wave" && track.ObjectType == 0)
|
||||
{
|
||||
// QuickTime keeps the esds of an mp4a entry inside a wave box (QTFF, sound sample description).
|
||||
foreach (var (wt, ws, wc) in Children(data, s, c))
|
||||
{
|
||||
if (wt == "esds")
|
||||
{
|
||||
(track.ObjectType, track.Asc) = EsdsConfig(data.AsSpan(ws, wc));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The DecoderSpecificInfo of an <c>esds</c> box (ISO/IEC 14496-1 descriptors).</summary>
|
||||
private static byte[]? EsdsConfig(ReadOnlySpan<byte> body)
|
||||
/// <summary>
|
||||
/// The objectTypeIndication (0 when absent) and the DecoderSpecificInfo of an <c>esds</c> box (ISO/IEC 14496-1
|
||||
/// descriptors).
|
||||
/// </summary>
|
||||
private static (int ObjectType, byte[]? Config) EsdsConfig(ReadOnlySpan<byte> body)
|
||||
{
|
||||
var objectType = 0;
|
||||
var p = 4; // version and flags
|
||||
while (p + 2 <= body.Length)
|
||||
{
|
||||
@@ -555,7 +656,7 @@ public static class IsoBmffReader
|
||||
{
|
||||
if (p + 3 > body.Length)
|
||||
{
|
||||
return null;
|
||||
return (objectType, null);
|
||||
}
|
||||
|
||||
var flags = body[p + 2];
|
||||
@@ -569,7 +670,7 @@ public static class IsoBmffReader
|
||||
{
|
||||
if (p >= body.Length)
|
||||
{
|
||||
return null;
|
||||
return (objectType, null);
|
||||
}
|
||||
|
||||
p += 1 + body[p];
|
||||
@@ -583,18 +684,23 @@ public static class IsoBmffReader
|
||||
break;
|
||||
}
|
||||
|
||||
case 0x04: // DecoderConfigDescriptor: its fixed fields, then the children
|
||||
case 0x04: // DecoderConfigDescriptor: objectTypeIndication and the other fixed fields, then the children
|
||||
if (p < body.Length)
|
||||
{
|
||||
objectType = body[p];
|
||||
}
|
||||
|
||||
p += 13;
|
||||
break;
|
||||
case 0x05:
|
||||
return p + length <= body.Length ? body.Slice(p, length).ToArray() : null;
|
||||
return (objectType, p + length <= body.Length ? body.Slice(p, length).ToArray() : null);
|
||||
default:
|
||||
p += length;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return (objectType, null);
|
||||
}
|
||||
|
||||
private void BuildSamples(Track track, byte[] stts, byte[]? ctts, byte[] stsc, byte[] stsz, bool stz2, byte[] stco, bool co64, byte[]? stss)
|
||||
@@ -903,7 +1009,7 @@ public static class IsoBmffReader
|
||||
return total;
|
||||
}
|
||||
|
||||
internal MediaContainer Build()
|
||||
internal MediaContainer Build(string? brand)
|
||||
{
|
||||
var video = tracks.Find(t => t.Handler == "vide");
|
||||
var audio = tracks.Find(t => t.Handler == "soun");
|
||||
@@ -932,6 +1038,10 @@ public static class IsoBmffReader
|
||||
AvcConfiguration = video.Avcc,
|
||||
HevcConfiguration = video.Hvcc,
|
||||
Samples = video.Samples,
|
||||
EditMediaTime = video.HasEdits ? video.EditOffset : null,
|
||||
EmptyEditDuration = EmptyEdit(video),
|
||||
EditCount = video.EditCount,
|
||||
Language = video.Language,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -942,6 +1052,8 @@ public static class IsoBmffReader
|
||||
{
|
||||
Codec = audio.Format switch
|
||||
{
|
||||
// objectTypeIndication 0x69 and 0x6B are MPEG-1/2 audio (MP3) in an mp4a entry (ISO/IEC 14496-1 Table 5).
|
||||
"mp4a" when audio.ObjectType is 0x69 or 0x6B => AudioCodec.Mp3,
|
||||
"mp4a" => AudioCodec.Aac,
|
||||
".mp3" => AudioCodec.Mp3,
|
||||
"Opus" => AudioCodec.Opus,
|
||||
@@ -951,10 +1063,15 @@ public static class IsoBmffReader
|
||||
CodecTag = audio.Format,
|
||||
Channels = audio.Channels,
|
||||
SampleRate = audio.SampleRate,
|
||||
AacConfiguration = audio.Asc,
|
||||
AacConfiguration = audio.ObjectType is 0x69 or 0x6B ? null : audio.Asc,
|
||||
AudioObjectType = audio.ObjectType is 0x69 or 0x6B ? null : AudioTrack.ObjectTypeOf(audio.Asc),
|
||||
Timescale = audio.Timescale > 0 ? audio.Timescale : 1,
|
||||
Duration = DurationOf(audio),
|
||||
Samples = audio.Samples,
|
||||
EditMediaTime = audio.HasEdits ? audio.EditOffset : null,
|
||||
EmptyEditDuration = EmptyEdit(audio),
|
||||
EditCount = audio.EditCount,
|
||||
Language = audio.Language,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -965,7 +1082,30 @@ public static class IsoBmffReader
|
||||
total = Seconds(v.Seconds(v.Duration));
|
||||
}
|
||||
|
||||
return new MediaContainer { Format = ContainerFormat.IsoBmff, CreatedAt = created, Duration = total, Video = v, Audio = a };
|
||||
return new MediaContainer
|
||||
{
|
||||
Format = ContainerFormat.IsoBmff,
|
||||
CreatedAt = created,
|
||||
ModifiedAt = modified,
|
||||
MajorBrand = brand,
|
||||
TrackCount = TrackCount,
|
||||
HasUserData = HasUserData,
|
||||
Duration = total,
|
||||
Video = v,
|
||||
Audio = a,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>The leading empty edits of a track (movie units) in track units, rounded to the nearest.</summary>
|
||||
private long EmptyEdit(Track track)
|
||||
{
|
||||
if (track.EmptyEditMovie <= 0 || timescale <= 0 || track.Timescale <= 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var value = (((Int128)track.EmptyEditMovie * track.Timescale) + (timescale / 2)) / timescale;
|
||||
return value > long.MaxValue / 4 ? 0 : (long)value;
|
||||
}
|
||||
|
||||
/// <summary>A duration in seconds; null when it is beyond what a video can last (a damaged header).</summary>
|
||||
|
||||
Reference in new issue
Block a user