mirror of https://github.com/SixLabors/ImageSharp
4 changed files with 134 additions and 50 deletions
@ -0,0 +1,76 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using System.Buffers; |
|||
using System.Runtime.InteropServices; |
|||
using SixLabors.ImageSharp.Formats.Heif.Av1.Transform; |
|||
using SixLabors.ImageSharp.Memory; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Heif.Av1.Pipeline; |
|||
|
|||
/// <summary>
|
|||
/// Owns the reusable spatial, transform, and reconstruction storage for AV1 block encoding.
|
|||
/// </summary>
|
|||
internal sealed class Av1EncoderBlockWorkspace : IDisposable |
|||
{ |
|||
/// <summary>
|
|||
/// The maximum number of spatial residual samples in one AV1 transform block.
|
|||
/// </summary>
|
|||
public const int MaximumResidualCount = Av1Constants.MaxTransformSize * Av1Constants.MaxTransformSize; |
|||
|
|||
/// <summary>
|
|||
/// The maximum number of coded coefficients after AV1 removes the uncoded half of 64-point axes.
|
|||
/// </summary>
|
|||
public const int MaximumCoefficientCount = (Av1Constants.MaxTransformSize / 2) * (Av1Constants.MaxTransformSize / 2); |
|||
|
|||
/// <summary>
|
|||
/// The complete workspace length in signed-integer storage elements.
|
|||
/// </summary>
|
|||
public const int StorageLength = ResidualStorageLength + MaximumCoefficientCount + MaximumCoefficientCount + Av1TransformWorkspace.MaximumLength; |
|||
|
|||
private const int ResidualStorageLength = MaximumResidualCount / 2; |
|||
private const int TransformCoefficientOffset = ResidualStorageLength; |
|||
private const int DequantizedCoefficientOffset = TransformCoefficientOffset + MaximumCoefficientCount; |
|||
private const int TransformWorkspaceOffset = DequantizedCoefficientOffset + MaximumCoefficientCount; |
|||
|
|||
/// <summary>
|
|||
/// Owns the complete reusable block workspace in 32-bit elements so every transform region is naturally aligned.
|
|||
/// </summary>
|
|||
private readonly IMemoryOwner<int> owner; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Av1EncoderBlockWorkspace"/> class.
|
|||
/// </summary>
|
|||
/// <param name="configuration">The configuration providing the encoder allocator.</param>
|
|||
public Av1EncoderBlockWorkspace(Configuration configuration) |
|||
=> this.owner = configuration.MemoryAllocator.Allocate<int>(StorageLength); |
|||
|
|||
/// <summary>
|
|||
/// Gets the maximum-size spatial residual workspace as a compact 16-bit view of the aligned owner.
|
|||
/// </summary>
|
|||
public Span<short> Residual |
|||
=> MemoryMarshal.Cast<int, short>(this.owner.Memory.Span[..ResidualStorageLength]); |
|||
|
|||
/// <summary>
|
|||
/// Gets the maximum-size forward-transform coefficient workspace.
|
|||
/// </summary>
|
|||
public Span<int> TransformCoefficients |
|||
=> this.owner.Memory.Span.Slice(TransformCoefficientOffset, MaximumCoefficientCount); |
|||
|
|||
/// <summary>
|
|||
/// Gets the maximum-size dequantized reconstruction coefficient workspace.
|
|||
/// </summary>
|
|||
public Span<int> DequantizedCoefficients |
|||
=> this.owner.Memory.Span.Slice(DequantizedCoefficientOffset, MaximumCoefficientCount); |
|||
|
|||
/// <summary>
|
|||
/// Gets the reusable two-dimensional transform workspace.
|
|||
/// </summary>
|
|||
public Span<int> TransformWorkspace |
|||
=> this.owner.Memory.Span.Slice(TransformWorkspaceOffset, Av1TransformWorkspace.MaximumLength); |
|||
|
|||
/// <summary>
|
|||
/// Releases the reusable block workspace.
|
|||
/// </summary>
|
|||
public void Dispose() => this.owner.Dispose(); |
|||
} |
|||
Loading…
Reference in new issue