using Titano.Core.Imaging;
namespace Titano.Core.Video;
public enum VideoCodec
{
H264,
Hevc,
Av1,
Mjpeg,
JpegSequence,
PngSequence,
Tiff16Sequence,
LinearDngSequence,
Gif,
Apng,
}
public enum ContainerFormat
{
Mp4,
Avi,
ImageSequence,
Gif,
Apng,
}
public enum RateControl
{
Vbr,
Cbr,
Quality,
}
public enum CodecProfile
{
Auto,
H264Baseline,
H264Main,
H264High,
HevcMain,
HevcMain10,
Av1Main,
}
public static class VideoCodecInfo
{
public static ContainerFormat DefaultContainer(VideoCodec codec) => codec switch
{
VideoCodec.H264 or VideoCodec.Hevc or VideoCodec.Av1 => ContainerFormat.Mp4,
VideoCodec.Mjpeg => ContainerFormat.Avi,
VideoCodec.Gif => ContainerFormat.Gif,
VideoCodec.Apng => ContainerFormat.Apng,
_ => ContainerFormat.ImageSequence,
};
/// True for codecs every platform can produce without an OS encoder.
public static bool IsManaged(VideoCodec codec) => codec is not (VideoCodec.H264 or VideoCodec.Hevc or VideoCodec.Av1);
public static string FileExtension(VideoCodec codec) => codec switch
{
VideoCodec.H264 or VideoCodec.Hevc or VideoCodec.Av1 => ".mp4",
VideoCodec.Mjpeg => ".avi",
VideoCodec.Gif => ".gif",
VideoCodec.Apng => ".png",
VideoCodec.JpegSequence => ".jpg",
VideoCodec.PngSequence => ".png",
VideoCodec.Tiff16Sequence => ".tif",
VideoCodec.LinearDngSequence => ".dng",
_ => ".bin",
};
}
/// What an encoder on this machine can do (shown in Settings › Rendering).
public sealed record EncoderCapability(
VideoCodec Codec,
string Name,
bool Hardware,
int MaxWidth,
int MaxHeight,
bool SupportsTenBit,
string? Vendor = null);
/// Complete parameters of one encode.
public sealed record EncodeSettings
{
public required string OutputPath { get; init; }
public required VideoCodec Codec { get; init; }
public required int Width { get; init; }
public required int Height { get; init; }
public required Rational FrameRate { get; init; }
public CodecProfile Profile { get; init; } = CodecProfile.Auto;
public int BitDepth { get; init; } = 8;
public RateControl RateControl { get; init; } = RateControl.Vbr;
/// Average bit rate in bits per second (VBR/CBR).
public long BitRate { get; init; }
/// 0–100 quality for , JPEG/MJPEG and GIF.
public int Quality { get; init; } = 90;
/// Distance between key frames in frames; 0 = encoder default (≈ 1 s).
public int GopLength { get; init; }
public int BFrames { get; init; } = 2;
public bool PreferHardware { get; init; } = true;
/// Optional specific encoder name from .
public string? EncoderName { get; init; }
}
/// One open encode. Frames are linear-light buffers at the output size.
public interface IVideoEncoderSession : IDisposable
{
/// Encoder actually used ("NVIDIA NVENC H.264", "Managed MJPEG").
string EncoderName { get; }
bool IsHardware { get; }
/// Appends a frame shown for ticks of a 90 kHz clock.
void WriteFrame(ImageBuffer frame, long duration90k);
/// Flushes and closes the output. Without it, deletes the partial file.
void Complete();
}
/// A family of encoders (Media Foundation on Windows, managed everywhere).
public interface IVideoEncoderProvider
{
string Name { get; }
IReadOnlyList Capabilities { get; }
bool CanEncode(EncodeSettings settings);
IVideoEncoderSession Open(EncodeSettings settings);
}