Files
Febe/src/Febe.Core/Codecs/Containers/MediaContainer.cs
T
Alby96andClaude Opus 5.5 ac85d1baed 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>
2026-11-18 17:38:09 +01:00

130 lines
4.4 KiB
C#

namespace Febe.Core.Codecs.Containers;
/// <summary>The container formats Febe reads without external programs.</summary>
public enum ContainerFormat
{
/// <summary>ISO base media file format: MP4, MOV, 3GP, M4V (also fragmented).</summary>
IsoBmff,
/// <summary>Matroska and WebM.</summary>
Matroska,
/// <summary>RIFF AVI (also OpenDML).</summary>
Avi,
}
/// <summary>Video codecs recognised in a container; <see cref="H264"/> and <see cref="Hevc"/> can be decoded.</summary>
public enum VideoCodec
{
Unknown,
H264,
Hevc,
Vp8,
Vp9,
Av1,
Mpeg4Part2,
Mjpeg,
}
/// <summary>Audio codecs recognised in a container.</summary>
public enum AudioCodec
{
Unknown,
Aac,
Mp3,
Opus,
Vorbis,
Pcm,
}
/// <summary>
/// One sample (an access unit) of a track: where its bytes are in the file and when it is decoded and shown, in units of
/// the track's <see cref="MediaTrack.Timescale"/>. Samples are listed in decoding order.
/// </summary>
public readonly record struct MediaSample(long Offset, int Size, long DecodeTime, long PresentationTime, bool Key);
/// <summary>A track of a container.</summary>
public abstract class MediaTrack
{
/// <summary>Ticks per second of the sample times.</summary>
public long Timescale { get; init; } = 1000;
/// <summary>Duration of the track in <see cref="Timescale"/> units; 0 when unknown.</summary>
public long Duration { get; init; }
/// <summary>Samples in decoding order (empty when the container was read for its metadata only).</summary>
public IReadOnlyList<MediaSample> Samples { get; init; } = [];
/// <summary>The four-character code or codec identifier of the container, for messages.</summary>
public string CodecTag { get; init; } = string.Empty;
/// <summary>Time in seconds of a value in track units.</summary>
public double Seconds(long ticks) => Timescale > 0 ? (double)ticks / Timescale : 0;
}
/// <summary>The video track of a container.</summary>
public sealed class VideoTrack : MediaTrack
{
public VideoCodec Codec { get; init; }
/// <summary>Coded width in pixels (before rotation).</summary>
public int Width { get; init; }
/// <summary>Coded height in pixels (before rotation).</summary>
public int Height { get; init; }
/// <summary>Clockwise rotation to apply when showing the frames: 0, 90, 180 or 270.</summary>
public int Rotation { get; init; }
/// <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; }
/// <summary>Width as shown (rotation applied).</summary>
public int DisplayWidth => Rotation % 180 == 0 ? Width : Height;
/// <summary>Height as shown (rotation applied).</summary>
public int DisplayHeight => Rotation % 180 == 0 ? Height : Width;
}
/// <summary>The audio track of a container.</summary>
public sealed class AudioTrack : MediaTrack
{
public AudioCodec Codec { get; init; }
public int SampleRate { get; init; }
public int Channels { get; init; }
/// <summary>The AudioSpecificConfig of an AAC track.</summary>
public byte[]? AacConfiguration { get; init; }
}
/// <summary>What a container says: its creation time, duration and first video and audio tracks.</summary>
public sealed class MediaContainer
{
public ContainerFormat Format { get; init; }
/// <summary>Creation time written in the container, when plausible.</summary>
public DateTimeOffset? CreatedAt { get; init; }
/// <summary>Duration of the whole presentation.</summary>
public TimeSpan? Duration { get; init; }
public VideoTrack? Video { get; init; }
public AudioTrack? Audio { get; init; }
}
/// <summary>A container that cannot be read: damaged, truncated or of a shape Febe does not handle.</summary>
public sealed class ContainerException(string message) : Exception(message);