Lettori ISO BMFF (anche frammentato), Matroska/WebM e AVI; ContainerVideoMetadataProbe è la sonda predefinita; VideoFrameDecoder (H.264 in casa, BMP ruotato, riconoscimento dei video fermi) sostituisce VideoFrameExtractor; Mp4Writer con moov in testa. Tolti ffmpeg e ffprobe da Dockerfile, CI, script e prove. WIP perché la corsa completa di tests/Febe.Tests non era finita al momento del commit e l'ultima correzione (data qualsiasi nella lettura dei campioni) non è stata ricompilata né riprovata: la compilazione durante la corsa fallisce per DLL bloccate dalle prove in esecuzione. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
123 lines
4.0 KiB
C#
123 lines
4.0 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; only <see cref="H264"/> 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>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);
|