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