Solution layout, build rules with zero-package guard, in-house test framework, localization runtime, brand mark rasteriser with ICO/PNG generation, Core contracts (imaging, video, domain model, TIFF reader), Titano.Tools (make-icon, i18n, xaml-lint, package-guard) and docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
135 lines
3.9 KiB
C#
135 lines
3.9 KiB
C#
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,
|
||
};
|
||
|
||
/// <summary>True for codecs every platform can produce without an OS encoder.</summary>
|
||
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",
|
||
};
|
||
}
|
||
|
||
/// <summary>What an encoder on this machine can do (shown in Settings › Rendering).</summary>
|
||
public sealed record EncoderCapability(
|
||
VideoCodec Codec,
|
||
string Name,
|
||
bool Hardware,
|
||
int MaxWidth,
|
||
int MaxHeight,
|
||
bool SupportsTenBit,
|
||
string? Vendor = null);
|
||
|
||
/// <summary>Complete parameters of one encode.</summary>
|
||
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;
|
||
|
||
/// <summary>Average bit rate in bits per second (VBR/CBR).</summary>
|
||
public long BitRate { get; init; }
|
||
|
||
/// <summary>0–100 quality for <see cref="RateControl.Quality"/>, JPEG/MJPEG and GIF.</summary>
|
||
public int Quality { get; init; } = 90;
|
||
|
||
/// <summary>Distance between key frames in frames; 0 = encoder default (≈ 1 s).</summary>
|
||
public int GopLength { get; init; }
|
||
|
||
public int BFrames { get; init; } = 2;
|
||
public bool PreferHardware { get; init; } = true;
|
||
|
||
/// <summary>Optional specific encoder name from <see cref="EncoderCapability.Name"/>.</summary>
|
||
public string? EncoderName { get; init; }
|
||
}
|
||
|
||
/// <summary>One open encode. Frames are linear-light buffers at the output size.</summary>
|
||
public interface IVideoEncoderSession : IDisposable
|
||
{
|
||
/// <summary>Encoder actually used ("NVIDIA NVENC H.264", "Managed MJPEG").</summary>
|
||
string EncoderName { get; }
|
||
|
||
bool IsHardware { get; }
|
||
|
||
/// <summary>Appends a frame shown for <paramref name="duration90k"/> ticks of a 90 kHz clock.</summary>
|
||
void WriteFrame(ImageBuffer frame, long duration90k);
|
||
|
||
/// <summary>Flushes and closes the output. Without it, <see cref="IDisposable.Dispose"/> deletes the partial file.</summary>
|
||
void Complete();
|
||
}
|
||
|
||
/// <summary>A family of encoders (Media Foundation on Windows, managed everywhere).</summary>
|
||
public interface IVideoEncoderProvider
|
||
{
|
||
string Name { get; }
|
||
IReadOnlyList<EncoderCapability> Capabilities { get; }
|
||
bool CanEncode(EncodeSettings settings);
|
||
IVideoEncoderSession Open(EncodeSettings settings);
|
||
}
|