using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Febe.Core.Codecs.H264;
///
/// The two sample types of the decoder: for 8-bit video, above 8 bits (up to 14,
/// High 4:4:4). The planes of a picture are byte arrays of one or two bytes per sample (little-endian, as the official
/// reconstructions store them); the prediction, transform and filter code is generic over the sample type, and the JIT
/// compiles it once for each, so 8-bit video pays nothing for the wider one.
///
internal static class Samples
{
/// The value of a sample.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int Get(T sample)
where T : unmanaged =>
typeof(T) == typeof(byte) ? Unsafe.As(ref sample) : Unsafe.As(ref sample);
/// A sample of value , already within the range of the bit depth.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static T Make(int value)
where T : unmanaged
{
if (typeof(T) == typeof(byte))
{
var b = (byte)value;
return Unsafe.As(ref b);
}
var u = (ushort)value;
return Unsafe.As(ref u);
}
/// A plane stored as bytes, seen as samples of type .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Span Plane(byte[] plane)
where T : unmanaged => MemoryMarshal.Cast(plane.AsSpan());
/// Clip1 of ยง5.7: within 0...
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int Clip(int value, int max) => value < 0 ? 0 : value > max ? max : value;
}