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

+80
View File
@@ -2,6 +2,19 @@ using Febe.Core.Codecs.H264;
namespace Febe.Core.Codecs.H265;
/// <summary>What an access unit offers to start decoding from (see <see cref="H265Decoder.AccessPoint"/>).</summary>
public enum H265AccessPoint
{
/// <summary>A picture that is not IRAP, or no picture: decoding cannot start here.</summary>
None,
/// <summary>A CRA picture: decoding starts here, but its RASL pictures (shown before it) are skipped.</summary>
Cra,
/// <summary>An IDR or BLA picture: nothing decoded after it refers to anything before.</summary>
Idr,
}
/// <summary>
/// An HEVC (ITU-T H.265 | ISO/IEC 23008-2) decoder written from the Recommendation, for the Main, Main 10 and Main Still
/// Picture profiles and the format range extensions up to 12 bits: 4:0:0, 4:2:0, 4:2:2 and 4:4:4, tiles, wavefronts,
@@ -50,6 +63,16 @@ public sealed partial class H265Decoder
/// <summary>Raised for each frame in output order (the bumping process of C.5.2.4), cropped.</summary>
public event Action<H265Frame>? FrameOutput;
/// <summary>
/// Raised for each picture once decoded and filtered, in decoding order and cropped, when <see cref="Wanted"/> accepts
/// its decoding index: a caller that maps pictures to the samples of a container takes them here, without waiting for
/// the output order. Pictures with pic_output_flag 0 are raised too.
/// </summary>
public event Action<H265Frame>? FrameDecoded;
/// <summary>Which pictures <see cref="FrameDecoded"/> hands out, by decoding index; null hands out all of them.</summary>
public Func<long, bool>? Wanted { get; set; }
/// <summary>Raised for each picture once decoded and filtered, in decoding order (for the decoded picture hash).</summary>
internal event Action<Picture>? PictureDecoded;
@@ -125,6 +148,58 @@ public sealed partial class H265Decoder
FinishPicture();
}
/// <summary>Decodes one access unit in Annex B form (an AVI chunk, a PES packet) and finishes its picture.</summary>
public void DecodeAccessUnitAnnexB(ReadOnlySpan<byte> accessUnit)
{
var position = 0;
while (NalUnits.NextAnnexB(accessUnit, ref position, out var unit))
{
Decode(accessUnit[unit]);
}
FinishPicture();
}
/// <summary>
/// Whether decoding can start at this access unit (§8.1.3): exactly at an IDR or BLA picture; at a CRA picture too,
/// but then its RASL pictures, which follow it in decoding order and refer to pictures before it, are not decoded.
/// <paramref name="lengthSize"/> is the size of the NAL length fields, 0 for Annex B.
/// </summary>
public static H265AccessPoint AccessPoint(ReadOnlySpan<byte> accessUnit, int lengthSize)
{
var position = 0;
while (true)
{
Range unit;
var more = lengthSize == 0
? NalUnits.NextAnnexB(accessUnit, ref position, out unit)
: NalUnits.NextLengthPrefixed(accessUnit, lengthSize, ref position, out unit);
if (!more)
{
return H265AccessPoint.None;
}
var nal = accessUnit[unit];
if (nal.Length < 3 || (nal[0] & 0x80) != 0 || (((nal[0] & 1) << 5) | (nal[1] >> 3)) != 0)
{
continue; // too short, forbidden bit set, or a layer above 0
}
var type = (nal[0] >> 1) & 63;
if (type > 31)
{
continue; // parameter sets, SEI, delimiters: the picture comes after them
}
return type switch
{
>= 16 and <= 20 => H265AccessPoint.Idr,
21 => H265AccessPoint.Cra,
_ => H265AccessPoint.None,
};
}
}
/// <summary>Finishes the picture being decoded and outputs every picture still waiting, in output order.</summary>
public void Flush()
{
@@ -560,6 +635,11 @@ public sealed partial class H265Decoder
}
PictureDecoded?.Invoke(picture);
if (FrameDecoded is { } decoded && (Wanted?.Invoke(picture.DecodeIndex) ?? true))
{
decoded(picture.ToFrame());
}
foreach (var other in dpb)
{
if (other.NeededForOutput)