HEVC nella catena dei media e foto HEIC/HEIF decodificate in casa (#58)

Le miniature e i fotogrammi dei video HEVC (i video dei telefoni) e le foto
HEIC non avevano un decodificatore: ora passano dal decodificatore HEVC
scritto in casa, senza componenti esterni.

- Contenitori: hvcC dai sample entry hvc1/hev1 dei file ISO e dalla
  CodecPrivate di Matroska V_MPEGH/ISO/HEVC (VideoTrack.HevcConfiguration);
  HEVC in AVI come Annex B. Lo scrittore MP4 sa scrivere anche hvc1.
- H265Decoder: fotogrammi in ordine di decodifica (FrameDecoded con Wanted),
  unità di accesso Annex B, punto d'accesso di un campione (IDR/BLA esatti,
  CRA vicino).
- VideoFrameDecoder sceglie H.264 o HEVC dal codec della traccia; il
  fotogramma da cui parte la corsa vale per un campione senza immagine
  (RASL di una CRA). Una sola conversione Y'CbCr->RGB (Codecs/YuvToRgb)
  per H.264, HEVC 8-12 bit 4:0:0/4:2:0/4:2:2/4:4:4 e HEIF.
- Febe.Core.Codecs.Heif (ISO/IEC 23008-12): meta, iinf, iloc (file e idat),
  iref, iprp/ipco/ipma; elemento hvc1 o griglia di tessere decodificate in
  parallelo, colr nclx/ICC, piano alfa, clap, irot/imir come orientamento
  EXIF, elemento Exif. Uscita HeifImage, con la stessa forma di RgbaImage
  del ramo funzione/codec-immagini (da unire).
- ImageDecoding manda gli HEIF al decodificatore di casa: miniature,
  impronte, dimensioni e anteprime valgono anche per gli HEIC;
  ExifDateReader legge la data dall'elemento Exif nei primi 512 KB.
- Prove su file costruiti dai flussi H.265.1 (MP4, frammentato, Matroska,
  AVI con e senza indice, Main 10, griglie, rotazioni, clap, alfa, nclx,
  Exif, file rovinati con semi fissi). Documenti: docker.md, LEGGIMI
  dell'importazione, una riga in decisioni.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-18 17:38:09 +01:00
1 parent 3cabd87316
commit ac85d1baed
21 files changed
+2695 -148

No files matched your search

@@ -266,6 +266,7 @@ public static class IsoBmffReader
internal int Channels;
internal int SampleRate;
internal byte[]? Avcc;
internal byte[]? Hvcc;
internal byte[]? Asc;
internal long EditOffset;
internal List<MediaSample> Samples = [];
@@ -505,6 +506,10 @@ public static class IsoBmffReader
{
track.Avcc = data[s..(s + c)];
}
else if (t == "hvcC")
{
track.Hvcc = data[s..(s + c)];
}
}
}
else if (track.Handler == "soun" && size >= 8 + 28)
@@ -925,6 +930,7 @@ public static class IsoBmffReader
Timescale = video.Timescale > 0 ? video.Timescale : 1,
Duration = DurationOf(video),
AvcConfiguration = video.Avcc,
HevcConfiguration = video.Hvcc,
Samples = video.Samples,
};
}
@@ -595,6 +595,7 @@ public static class MatroskaReader
Height = v.Height,
Rotation = roll is 90 or 180 or 270 ? roll : 0,
AvcConfiguration = v.CodecId == "V_MPEG4/ISO/AVC" ? v.CodecPrivate : null,
HevcConfiguration = v.CodecId == "V_MPEGH/ISO/HEVC" ? v.CodecPrivate : null,
Timescale = timescale,
Duration = trackUnits,
Samples = samples,
@@ -13,7 +13,7 @@ public enum ContainerFormat
Avi,
}
/// <summary>Video codecs recognised in a container; only <see cref="H264"/> can be decoded.</summary>
/// <summary>Video codecs recognised in a container; <see cref="H264"/> and <see cref="Hevc"/> can be decoded.</summary>
public enum VideoCodec
{
Unknown,
@@ -79,6 +79,13 @@ public sealed class VideoTrack : MediaTrack
/// <summary>The AVCDecoderConfigurationRecord (<c>avcC</c>) of an H.264 track stored with length prefixes.</summary>
public byte[]? AvcConfiguration { get; init; }
/// <summary>
/// The HEVCDecoderConfigurationRecord (<c>hvcC</c>, ISO/IEC 14496-15 §8.3.3) of an HEVC track stored with length
/// prefixes: the <c>hvcC</c> box of an <c>hvc1</c> or <c>hev1</c> sample entry, the CodecPrivate of a Matroska
/// <c>V_MPEGH/ISO/HEVC</c> track.
/// </summary>
public byte[]? HevcConfiguration { get; init; }
/// <summary>True when the samples are an Annex B byte stream (start codes), as in AVI.</summary>
public bool AnnexB { get; init; }
+12 -5
View File
@@ -6,7 +6,10 @@ namespace Febe.Core.Codecs.Containers;
/// <summary>A sample to write: its size, its times in track units, whether it is a sync sample.</summary>
public readonly record struct Mp4OutputSample(int Size, long DecodeTime, long PresentationTime, bool Key);
/// <summary>A track to write: H.264 video (with its <c>avcC</c>) or AAC audio (with its AudioSpecificConfig).</summary>
/// <summary>
/// A track to write: H.264 video (with its <c>avcC</c>), HEVC video (with its <c>hvcC</c>, as an <c>hvc1</c> sample entry) or
/// AAC audio (with its AudioSpecificConfig).
/// </summary>
public sealed class Mp4OutputTrack
{
public required bool IsVideo { get; init; }
@@ -27,6 +30,9 @@ public sealed class Mp4OutputTrack
public byte[]? AvcConfiguration { get; init; }
/// <summary>The <c>hvcC</c> of an HEVC video track (then <see cref="AvcConfiguration"/> stays null).</summary>
public byte[]? HevcConfiguration { get; init; }
public byte[]? AacConfiguration { get; init; }
public int SampleRate { get; init; }
@@ -59,7 +65,7 @@ public static class Mp4Writer
foreach (var track in tracks)
{
if (track.IsVideo ? track.AvcConfiguration is null : track.AacConfiguration is null)
if (track.IsVideo ? (track.AvcConfiguration is null) == (track.HevcConfiguration is null) : track.AacConfiguration is null)
{
throw new ArgumentException("Manca la configurazione del codificatore.", nameof(tracks));
}
@@ -533,7 +539,8 @@ public static class Mp4Writer
private static Box Avc1(Mp4OutputTrack track)
{
var entry = new Box("avc1");
var hevc = track.HevcConfiguration is not null;
var entry = new Box(hevc ? "hvc1" : "avc1");
entry.Zero(6);
entry.U16(1);
entry.Zero(16);
@@ -546,8 +553,8 @@ public static class Mp4Writer
entry.Zero(32);
entry.U16(0x0018);
entry.U16(0xFFFF);
var avcC = new Box("avcC");
avcC.Bytes(track.AvcConfiguration);
var avcC = new Box(hevc ? "hvcC" : "avcC");
avcC.Bytes(hevc ? track.HevcConfiguration : track.AvcConfiguration);
entry.Child(avcC);
return entry;
}