Documentation
¶
Overview ¶
Package avif implements an AVIF image decoder/encoder based on libavif shared library used via purego (CGo-free).
Index ¶
- Constants
- Variables
- func Decode(r io.Reader) (image.Image, error)
- func DecodeConfig(r io.Reader) (image.Config, error)
- func Dynamic() error
- func Encode(w io.Writer, m image.Image, o *Options) error
- func EncodeAll(w io.Writer, a *AVIF, o *Options) error
- type AVIF
- type CICP
- type CleanAperture
- type ContentLightLevel
- type Grid
- type Options
- type PixelAspectRatio
- type PixelFormat
- type Range
- type ScalingMode
Constants ¶
const ( // DefaultQuality is the default color quality (avifenc -q). Maps to a quantizer (QP) of 25. DefaultQuality = 60 // DefaultSpeed is the default encoder speed (avifenc -s). DefaultSpeed = 6 // DefaultTimescale is the default timescale of image sequences (avifenc --timescale). DefaultTimescale = 30 )
Defaults of the encoding parameters. They are the same as the defaults of avifenc.
const ( // QualityWorst is the worst quality. QualityWorst = 0 // QualityBest is the best quality. QualityBest = 100 // QualityLossless is the quality that implies lossless encoding. QualityLossless = 100 // SpeedSlowest is the slowest encoder speed (best compression). SpeedSlowest = 0 // SpeedFastest is the fastest encoder speed. SpeedFastest = 10 // SpeedCodecDefault leaves the AV1 codec to its own default speed settings (avifenc -s default). SpeedCodecDefault = -1 // RepetitionCountInfinite repeats an image sequence infinitely (avifenc --repetition-count infinite). RepetitionCountInfinite = -1 )
Special values of the encoding parameters.
const ( ColorPrimariesBT709 = 1 ColorPrimariesSRGB = 1 ColorPrimariesUnspecified = 2 ColorPrimariesBT2020 = 9 ColorPrimariesSMPTE432 = 12 // Display P3 )
Color primaries (ITU-T H.273). Only the most common values are listed.
const ( TransferCharacteristicsBT709 = 1 TransferCharacteristicsUnspecified = 2 TransferCharacteristicsLinear = 8 TransferCharacteristicsSRGB = 13 TransferCharacteristicsPQ = 16 TransferCharacteristicsHLG = 18 )
Transfer characteristics (ITU-T H.273). Only the most common values are listed.
const ( MatrixCoefficientsIdentity = 0 MatrixCoefficientsBT709 = 1 MatrixCoefficientsUnspecified = 2 MatrixCoefficientsBT601 = 6 MatrixCoefficientsBT2020NCL = 9 MatrixCoefficientsYCgCo = 8 MatrixCoefficientsYCgCoRe = 16 MatrixCoefficientsYCgCoRo = 17 )
Matrix coefficients (ITU-T H.273). Only the most common values are listed.
Variables ¶
var ( ErrMemRead = errors.New("avif: mem read failed") ErrMemWrite = errors.New("avif: mem write failed") ErrDecode = errors.New("avif: decode failed") ErrEncode = errors.New("avif: encode failed") ErrOptions = errors.New("avif: invalid options") )
Errors returned by this package, wrapped with details.
Functions ¶
func DecodeConfig ¶
DecodeConfig returns the color model and dimensions of a AVIF image without decoding the entire image.
func Dynamic ¶
func Dynamic() error
Dynamic returns the error, if any, that makes the libavif shared library unusable.
func Encode ¶
Encode writes the image m to w with the given options; nil options mean the defaults. With the same options, the output is the same as the one of avifenc given m as a PNG file.
func EncodeAll ¶
EncodeAll writes the images in a to w as an image sequence (animated AVIF) with the given options; nil options mean the defaults. The display duration of each frame is a.Delay (in seconds) converted to Options.Timescale units; frames without a positive delay last one timescale unit. A single frame is encoded as a still image, like avifenc does with a single input.
Types ¶
type AVIF ¶
type AVIF struct {
// Images, one per frame. Decoded images are *image.RGBA, or *image.RGBA64 when the bit depth
// of the AVIF is greater than 8.
Image []image.Image
// Delay times, one per frame, in seconds.
Delay []float64
}
AVIF represents the possibly multiple images stored in a AVIF file.
type CICP ¶
CICP holds the color primaries, transfer characteristics and matrix coefficients (avifenc --cicp P/T/M).
type CleanAperture ¶
type CleanAperture struct {
WidthN, WidthD uint32
HeightN, HeightD uint32
HorizOffN, HorizOffD uint32
VertOffN, VertOffD uint32
}
CleanAperture is the clap property (avifenc --clap WN,WD,HN,HD,HON,HOD,VON,VOD).
type ContentLightLevel ¶
ContentLightLevel is the clli property (avifenc --clli MaxCLL,MaxPALL).
type Options ¶
type Options struct {
// Quality for color in [0,100] where 100 is lossless (avifenc -q). Default: 60.
Quality *int
// Quality for alpha in [0,100] where 100 is lossless (avifenc --qalpha). Default: same as Quality.
QualityAlpha *int
// Encoder speed in [0,10] where 0 is the slowest and 10 the fastest, or SpeedCodecDefault
// (avifenc -s). Default: 6.
Speed *int
// Number of worker threads (avifenc -j). 0 uses as many as possible (default).
Jobs int
// Encode losslessly (avifenc -l): sets quality to 100 and the matrix coefficients to identity
// unless CICP is given.
Lossless bool
// Output bit depth per channel: 8, 10 or 12 (avifenc -d). 0 means 8 for 8-bit images and 12
// for 16-bit images (default).
Depth int
// Bit depth of a hidden image stored next to the primary one to reach 16-bit depth at decoding
// (avifenc -d D,Dextension): 4 (with Depth 12) or 8 (with Depth 8 or 12). Requires libavif 1.4.0.
DepthExtension int
// YUV format (avifenc -y). Default: auto.
YUV PixelFormat
// Premultiply color by the alpha channel and signal this in the AVIF (avifenc -p).
Premultiply bool
// Use sharp RGB to YUV420 conversion (avifenc --sharpyuv). Requires libavif built with libsharpyuv.
SharpYUV bool
// CICP values (avifenc --cicp). nil means: 1/13/6 (sRGB) if no ICC profile is given,
// 2/2/6 otherwise; matrix coefficients are 0 (identity) when Lossless is set.
CICP *CICP
// YUV range (avifenc -r). Default: full.
Range Range
// Target file size in bytes (avifenc --target-size). The qualities that are not explicitly set are
// searched to get the closest output size. 0 disables it.
TargetSize int
// Encode a simple layered image supporting progressive rendering (avifenc --progressive).
Progressive bool
// Encode a single-image grid AVIF by splitting the image into Cols x Rows cells (avifenc -g).
Grid *Grid
// AV1 codec to use, such as "aom", "rav1e" or "svt" (avifenc -c). Default: chosen by libavif.
Codec string
// Exif metadata payload (avifenc --exif).
Exif []byte
// XMP metadata payload (avifenc --xmp).
XMP []byte
// ICC profile payload (avifenc --icc).
ICC []byte
// Timescale for image sequences (avifenc --timescale). Default: 30.
Timescale uint64
// Creation time for image sequences in seconds since the Unix epoch (avifenc --creation-time).
// 0 uses the modification time. Requires libavif 1.4.0 when not 0.
CreationTime uint64
// Modification time for image sequences in seconds since the Unix epoch
// (avifenc --modification-time). 0 uses the current time. Requires libavif 1.4.0 when not 0.
ModificationTime uint64
// Maximum keyframe interval for image sequences (avifenc -k). 0 disables it (default).
KeyframeInterval int
// Number of times an image sequence is repeated, or RepetitionCountInfinite
// (avifenc --repetition-count). Default: infinite.
RepetitionCount *int
// Pixel aspect ratio property (avifenc --pasp).
PixelAspectRatio *PixelAspectRatio
// Clean aperture property calculated from a crop rectangle (avifenc --crop).
Crop *image.Rectangle
// Clean aperture property (avifenc --clap).
CleanAperture *CleanAperture
// Rotation property in [0,3]: makes (90 * Rotation) degree rotation anti-clockwise (avifenc --irot).
Rotation *int
// Mirroring property: 0 = top-to-bottom, 1 = left-to-right (avifenc --imir).
Mirror *int
// Content light level information property (avifenc --clli).
ContentLightLevel *ContentLightLevel
// Log2 of the number of tile rows in [0,6] (avifenc --tilerowslog2). Setting it switches to manual tiling.
TileRowsLog2 int
// Log2 of the number of tile columns in [0,6] (avifenc --tilecolslog2). Setting it switches to manual tiling.
TileColsLog2 int
// Use TileRowsLog2 and TileColsLog2 even when both are 0, instead of automatic tiling (default).
ManualTiling bool
// Frame scaling mode (avifenc --scaling-mode). Default: 1/1.
ScalingMode *ScalingMode
// Advanced codec-specific key/value options (avifenc -a KEY=VALUE),
// e.g. {"tune": "ssim", "c:sharpness": "2"}.
Advanced map[string]string
}
Options are the encoding parameters. They mirror the options of avifenc. The zero value of every field means "use the avifenc default". Fields that accept 0 as a meaningful value are pointers, e.g. Quality: new(80).
type PixelAspectRatio ¶
PixelAspectRatio is the pasp property (avifenc --pasp H,V).
type PixelFormat ¶
type PixelFormat int
PixelFormat is the YUV format of the encoded image (avifenc -y).
const ( // PixelFormatAuto uses YUV400 for grayscale images (*image.Gray, *image.Gray16) and YUV444 // for all other images, like avifenc does for PNG input. PixelFormatAuto PixelFormat = iota PixelFormat444 PixelFormat422 PixelFormat420 PixelFormat400 )
Pixel formats.
type ScalingMode ¶
ScalingMode is the frame (layer) scaling mode N/D (avifenc --scaling-mode N[/D]).