AOMedia AV1 Codec: Encoder Algorithm Interface

| AOMedia AV1 Codec

Encoder Algorithm Interface
Common Algorithm Interface

## Files
file | aom_encoder.h
| Describes the encoder algorithm interface to applications.

## Data Structures
struct | aom_fixed_buf
| Generic fixed size buffer structure. More...
struct | aom_codec_cx_pkt
| Encoder output packet. More...
struct | aom_rational
| Rational Number. More...
struct | cfg_options
| Encoder Config Options. More...
struct | aom_codec_enc_cfg
| Encoder configuration structure. More...

## Macros
#define | AOM_ENCODER_ABI_VERSION
| Current ABI version number.
#define | AOM_CODEC_CAP_PSNR 0x10000
| Encoder capabilities bitfield.
#define | AOM_CODEC_CAP_HIGHBITDEPTH 0x40000
#define | AOM_CODEC_USE_PSNR 0x10000
| Initialization-time Feature Enabling.
#define | AOM_CODEC_USE_HIGHBITDEPTH 0x40000
#define | AOM_ERROR_RESILIENT_DEFAULT 0x1
| Improve resiliency against losses of whole frames.
#define | AOM_EFLAG_FORCE_KF (1 << 0)
| Force this frame to be a keyframe.
#define | AOM_EFLAG_CALCULATE_PSNR (1 << 1)
| Calculate PSNR for this frame, requires g_lag_in_frames to be 0.
#define | AOM_EFLAG_FREEZE_INTERNAL_STATE (1 << 2)
| Freeze internal state, do not update reference buffers, entropy tables, rate control state, etc. Requires g_lag_in_frames to be 0.
#define | aom_codec_enc_init(ctx, iface, cfg, flags)
| Convenience macro for aom_codec_enc_init_ver().
#define | AOM_USAGE_GOOD_QUALITY 0u
| usage parameter analogous to AV1 GOOD QUALITY mode.
#define | AOM_USAGE_REALTIME 1u
| usage parameter analogous to AV1 REALTIME mode.
#define | AOM_USAGE_ALL_INTRA 2u
| usage parameter analogous to AV1 all intra mode.

## Typedefs
typedef struct aom_fixed_buf | aom_fixed_buf_t
| Generic fixed size buffer structure.
typedef uint32_t | aom_codec_er_flags_t
| Error Resilient flags.
typedef struct aom_codec_cx_pkt | aom_codec_cx_pkt_t
| Encoder output packet.
typedef struct aom_rational | aom_rational_t
| Rational Number.
typedef struct cfg_options | cfg_options_t
| Encoder Config Options.
typedef long | aom_enc_frame_flags_t
| Encoded Frame Flags.
typedef struct aom_codec_enc_cfg | aom_codec_enc_cfg_t
| Encoder configuration structure.

## Enumerations
enum | aom_codec_cx_pkt_kind { AOM_CODEC_CX_FRAME_PKT , AOM_CODEC_STATS_PKT , AOM_CODEC_FPMB_STATS_PKT , AOM_CODEC_PSNR_PKT , AOM_CODEC_CUSTOM_PKT = 256 }
| Encoder output packet variants. More...
enum | aom_enc_pass { AOM_RC_ONE_PASS = 0 , AOM_RC_FIRST_PASS = 1 , AOM_RC_SECOND_PASS = 2 , AOM_RC_THIRD_PASS = 3 , AOM_RC_LAST_PASS = 2 }
| Multi-pass Encoding Pass. More...
enum | aom_rc_mode { AOM_VBR , AOM_CBR , AOM_CQ , AOM_Q }
| Rate control mode. More...
enum | aom_kf_mode { AOM_KF_FIXED , AOM_KF_AUTO , AOM_KF_DISABLED = 0 }
| Keyframe placement mode. More...
enum | aom_superres_mode { AOM_SUPERRES_NONE , AOM_SUPERRES_FIXED , AOM_SUPERRES_RANDOM , AOM_SUPERRES_QTHRESH , AOM_SUPERRES_AUTO }
| Frame super-resolution mode. More...

## Functions
aom_codec_err_t | aom_codec_enc_init_ver (aom_codec_ctx_t *ctx, aom_codec_iface_t *iface, const aom_codec_enc_cfg_t *cfg, aom_codec_flags_t flags, int ver)
| Initialize an encoder instance.
aom_codec_err_t | aom_codec_enc_config_default (aom_codec_iface_t *iface, aom_codec_enc_cfg_t *cfg, unsigned int usage)
| Get the default configuration for a usage.
aom_codec_err_t | aom_codec_enc_config_set (aom_codec_ctx_t *ctx, const aom_codec_enc_cfg_t *cfg)
| Set or change configuration.
aom_fixed_buf_t * | aom_codec_get_global_headers (aom_codec_ctx_t *ctx)
| Get global stream headers.
aom_codec_err_t | aom_codec_encode (aom_codec_ctx_t *ctx, const aom_image_t *img, aom_codec_pts_t pts, unsigned long duration, aom_enc_frame_flags_t flags)
| Encode a frame.
aom_codec_err_t | aom_codec_set_cx_data_buf (aom_codec_ctx_t *ctx, const aom_fixed_buf_t *buf, unsigned int pad_before, unsigned int pad_after)
| Set compressed data output buffer.
const aom_codec_cx_pkt_t * | aom_codec_get_cx_data (aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
| Encoded data iterator.
const aom_image_t * | aom_codec_get_preview_frame (aom_codec_ctx_t *ctx)
| Get Preview Frame.

## Detailed Description

This abstraction allows applications using this encoder to easily support multiple video formats with minimal code duplication. This section describes the interface common to all encoders.

## Macro Definition Documentation

## ◆ AOM_CODEC_CAP_PSNR

#define AOM_CODEC_CAP_PSNR 0x10000

Encoder capabilities bitfield.

Each encoder advertises the capabilities it supports as part of its aom_codec_iface_t interface structure. Capabilities are extra interfaces or functionality, and are not required to be supported by an encoder.

The available flags are specified by AOM_CODEC_CAP_* defines. Can issue PSNR packets

## ◆ AOM_CODEC_CAP_HIGHBITDEPTH

#define AOM_CODEC_CAP_HIGHBITDEPTH 0x40000

Can support input images at greater than 8 bitdepth.

## ◆ AOM_CODEC_USE_PSNR

#define AOM_CODEC_USE_PSNR 0x10000

Initialization-time Feature Enabling.

Certain codec features must be known at initialization time, to allow for proper memory allocation.

The available flags are specified by AOM_CODEC_USE_* defines. Calculate PSNR on each frame

## ◆ AOM_CODEC_USE_HIGHBITDEPTH

#define AOM_CODEC_USE_HIGHBITDEPTH 0x40000

Use high bitdepth

## ◆ aom_codec_enc_init

#define aom_codec_enc_init | ( |  | ctx,
|  |  | iface,
|  |  | cfg,
|  |  | flags )

Value:
aom_codec_enc_init_ver(ctx, iface, cfg, flags, AOM_ENCODER_ABI_VERSION)
aom_codec_enc_init_ver
aom_codec_err_t aom_codec_enc_init_ver(aom_codec_ctx_t *ctx, aom_codec_iface_t *iface, const aom_codec_enc_cfg_t *cfg, aom_codec_flags_t flags, int ver)
Initialize an encoder instance.
AOM_ENCODER_ABI_VERSION
#define AOM_ENCODER_ABI_VERSION
Current ABI version number.
Definition aom_encoder.h:54

Convenience macro for aom_codec_enc_init_ver().

Ensures the ABI version parameter is properly set.

## Typedef Documentation

## ◆ aom_fixed_buf_t

typedef struct aom_fixed_buf aom_fixed_buf_t

Generic fixed size buffer structure.

This structure is able to hold a reference to any fixed size buffer. alias for struct aom_fixed_buf

## ◆ aom_codec_er_flags_t

typedef uint32_t aom_codec_er_flags_t

Error Resilient flags.

These flags define which error resilient features to enable in the encoder. The flags are specified through the aom_codec_enc_cfg::g_error_resilient variable.

## ◆ aom_codec_cx_pkt_t

typedef struct aom_codec_cx_pkt aom_codec_cx_pkt_t

Encoder output packet.

This structure contains the different kinds of output data the encoder may produce while compressing a frame. alias for struct aom_codec_cx_pkt

## ◆ aom_rational_t

typedef struct aom_rational aom_rational_t

Rational Number.

This structure holds a fractional value. alias for struct aom_rational

## ◆ cfg_options_t

typedef struct cfg_options cfg_options_t

Encoder Config Options.

This type allows to enumerate and control flags defined for encoder control via config file at runtime.

## ◆ aom_enc_frame_flags_t

typedef long aom_enc_frame_flags_t

Encoded Frame Flags.

This type indicates a bitfield to be passed to aom_codec_encode(), defining per-frame boolean values. By convention, bits common to all codecs will be named AOM_EFLAG_*, and bits specific to an algorithm will be named /algo/_eflag_*. The lower order 16 bits are reserved for common use.

## ◆ aom_codec_enc_cfg_t

typedef struct aom_codec_enc_cfg aom_codec_enc_cfg_t

Encoder configuration structure.

This structure contains the encoder settings that have common representations across all codecs. This doesn't imply that all codecs support all features, however. alias for struct aom_codec_enc_cfg

## Enumeration Type Documentation

## ◆ aom_codec_cx_pkt_kind

enum aom_codec_cx_pkt_kind

Encoder output packet variants.

This enumeration lists the different kinds of data packets that can be returned by calls to aom_codec_get_cx_data(). Algorithms MAY extend this list to provide additional functionality.

Enumerator
AOM_CODEC_CX_FRAME_PKT | Compressed video frame
AOM_CODEC_STATS_PKT | Two-pass statistics for this frame
AOM_CODEC_FPMB_STATS_PKT | first pass mb statistics for this frame
AOM_CODEC_PSNR_PKT | PSNR statistics for this frame
AOM_CODEC_CUSTOM_PKT | Algorithm extensions

## ◆ aom_enc_pass

enum aom_enc_pass

Multi-pass Encoding Pass.

AOM_RC_LAST_PASS is kept for backward compatibility. If passes is not given and pass==2, the codec will assume passes=2. For new code, it is recommended to use AOM_RC_SECOND_PASS and set the "passes" member to 2 via the key & val API for two-pass encoding.

Enumerator
AOM_RC_ONE_PASS | Single pass mode
AOM_RC_FIRST_PASS | First pass of multi-pass mode
AOM_RC_SECOND_PASS | Second pass of multi-pass mode
AOM_RC_THIRD_PASS | Third pass of multi-pass mode
AOM_RC_LAST_PASS | Final pass of two-pass mode

## ◆ aom_rc_mode

enum aom_rc_mode

Rate control mode.

Enumerator
AOM_VBR | Variable Bit Rate (VBR) mode
AOM_CBR | Constant Bit Rate (CBR) mode
AOM_CQ | Constrained Quality (CQ) mode
AOM_Q | Constant Quality (Q) mode

## ◆ aom_kf_mode

enum aom_kf_mode

Keyframe placement mode.

This enumeration determines whether keyframes are placed automatically by the encoder or whether this behavior is disabled. Older releases of this SDK were implemented such that AOM_KF_FIXED meant keyframes were disabled. This name is confusing for this behavior, so the new symbols to be used are AOM_KF_AUTO and AOM_KF_DISABLED.

Enumerator
AOM_KF_FIXED | deprecated, implies AOM_KF_DISABLED
AOM_KF_AUTO | Encoder determines optimal placement automatically
AOM_KF_DISABLED | Encoder does not place keyframes.

## ◆ aom_superres_mode

enum aom_superres_mode

Frame super-resolution mode.

Enumerator
AOM_SUPERRES_NONE | Frame super-resolution is disabled for all frames. All frames are coded at the specified scale and super-resolved.
AOM_SUPERRES_FIXED | All frames are coded at a random scale and super-resolved.
AOM_SUPERRES_RANDOM | Super-resolution scale for each frame is determined based on the q index of that frame.
AOM_SUPERRES_QTHRESH | Full-resolution or super-resolution and the scale (in case of super-resolution) are automatically selected for each frame.

## Function Documentation

## ◆ aom_codec_enc_init_ver()

aom_codec_err_t aom_codec_enc_init_ver | ( | aom_codec_ctx_t * | ctx,
|  | aom_codec_iface_t * | iface,
|  | const aom_codec_enc_cfg_t * | cfg,
|  | aom_codec_flags_t | flags,
|  | int | ver )

Initialize an encoder instance.

Initializes an encoder context using the given interface. Applications should call the aom_codec_enc_init convenience macro instead of this function directly, to ensure that the ABI version number parameter is properly initialized.

If the library was configured with -DCONFIG_MULTITHREAD=0, this call is not thread safe and should be guarded with a lock if being used in a multithreaded context.

On success, aom_codec_destroy() must be used to free resources allocated for the encoder context. If aom_codec_enc_init_ver() fails, it is not necessary to call aom_codec_destroy() on the encoder context.

Parameters

  [in] | ctx | Pointer to this instance's context.
  [in] | iface | Pointer to the algorithm interface to use.
  [in] | cfg | Configuration to use, if known.
  [in] | flags | Bitfield of AOM_CODEC_USE_* flags
  [in] | ver | ABI version number. Must be set to AOM_ENCODER_ABI_VERSION

Return values

  AOM_CODEC_OK | The encoder algorithm has been initialized.
  AOM_CODEC_MEM_ERROR | Memory allocation failed.

## ◆ aom_codec_enc_config_default()

aom_codec_err_t aom_codec_enc_config_default | ( | aom_codec_iface_t * | iface,
|  | aom_codec_enc_cfg_t * | cfg,
|  | unsigned int | usage )

Get the default configuration for a usage.

Initializes an encoder configuration structure with default values. Supports the notion of "usages" so that an algorithm may offer different default settings depending on the user's intended goal. This function SHOULD be called by all applications to initialize the configuration structure before specializing the configuration with application specific values.

Parameters

  [in] | iface | Pointer to the algorithm interface to use.
  [out] | cfg | Configuration buffer to populate.
  [in] | usage | Algorithm specific usage value. For AV1, must be set to AOM_USAGE_GOOD_QUALITY (0), AOM_USAGE_REALTIME (1), or AOM_USAGE_ALL_INTRA (2).

Return values

  AOM_CODEC_OK | The configuration was populated.
  AOM_CODEC_INCAPABLE | Interface is not an encoder interface.
  AOM_CODEC_INVALID_PARAM | A parameter was NULL, or the usage value was not recognized.

## ◆ aom_codec_enc_config_set()

aom_codec_err_t aom_codec_enc_config_set | ( | aom_codec_ctx_t * | ctx,
|  | const aom_codec_enc_cfg_t * | cfg )

Set or change configuration.

Reconfigures an encoder instance according to the given configuration.

Parameters

  [in] | ctx | Pointer to this instance's context
  [in] | cfg | Configuration buffer to use

Return values

  AOM_CODEC_OK | The configuration was populated.
  AOM_CODEC_INCAPABLE | Interface is not an encoder interface.
  AOM_CODEC_INVALID_PARAM | A parameter was NULL, or the usage value was not recognized.

## ◆ aom_codec_get_global_headers()

aom_fixed_buf_t * aom_codec_get_global_headers | ( | aom_codec_ctx_t * | ctx | ) |

Get global stream headers.

Retrieves a stream level global header packet, if supported by the codec. Calls to this function should be deferred until all configuration information has been passed to libaom. Otherwise the global header data may be invalidated by additional configuration changes.

The AV1 implementation of this function returns an OBU. The OBU returned is in Low Overhead Bitstream Format. Specifically, the obu_has_size_field bit is set, and the buffer contains the obu_size field for the returned OBU.

Parameters

  [in] | ctx | Pointer to this instance's context

Return values

  NULL | Encoder does not support global header, or an error occurred while generating the global header.
  Non-NULL | Pointer to buffer containing global header packet. The caller owns the memory associated with this buffer, and must free the 'buf' member of the aom_fixed_buf_t as well as the aom_fixed_buf_t pointer. Memory returned must be freed via call to free().

## ◆ aom_codec_encode()

aom_codec_err_t aom_codec_encode | ( | aom_codec_ctx_t * | ctx,
|  | const aom_image_t * | img,
|  | aom_codec_pts_t | pts,
|  | unsigned long | duration,
|  | aom_enc_frame_flags_t | flags )

Encode a frame.

Encodes a video frame at the given "presentation time." The presentation time stamp (PTS) MUST be strictly increasing.

When the last frame has been passed to the encoder, this function should continue to be called in a loop, with the img parameter set to NULL. This will signal the end-of-stream condition to the encoder and allow it to encode any held buffers. Encoding is complete when aom_codec_encode() is called with img set to NULL and aom_codec_get_cx_data() returns no data.

Parameters

  [in] | ctx | Pointer to this instance's context
  [in] | img | Image data to encode, NULL to flush. Encoding sample values outside the range [0..(1<<img->bit_depth)-1] is undefined behavior. Note: Although img is declared as a const pointer, if AV1E_SET_DENOISE_NOISE_LEVEL is set to a nonzero value aom_codec_encode() modifies (denoises) the samples in img->planes[i] .
  [in] | pts | Presentation time stamp, in timebase units. If img is NULL, pts is ignored.
  [in] | duration | Duration to show frame, in timebase units. If img is not NULL, duration must be nonzero. If img is NULL, duration is ignored.
  [in] | flags | Flags to use for encoding this frame.

Return values

  AOM_CODEC_OK | The configuration was populated.
  AOM_CODEC_INCAPABLE | Interface is not an encoder interface.
  AOM_CODEC_INVALID_PARAM | A parameter was NULL, the image format is unsupported, etc.

Note
  duration is of the unsigned long type, which can be 32 or 64 bits. duration must be less than or equal to UINT32_MAX so that its range is independent of the size of unsigned long.

## ◆ aom_codec_set_cx_data_buf()

aom_codec_err_t aom_codec_set_cx_data_buf | ( | aom_codec_ctx_t * | ctx,
|  | const aom_fixed_buf_t * | buf,
|  | unsigned int | pad_before,
|  | unsigned int | pad_after )

Set compressed data output buffer.

Sets the buffer that the codec should output the compressed data into. This call effectively sets the buffer pointer returned in the next AOM_CODEC_CX_FRAME_PKT packet. Subsequent packets will be appended into this buffer. The buffer is preserved across frames, so applications must periodically call this function after flushing the accumulated compressed data to disk or to the network to reset the pointer to the buffer's head.

pad_before bytes will be skipped before writing the compressed data, and pad_after bytes will be appended to the packet. The size of the packet will be the sum of the size of the actual compressed data, pad_before, and pad_after. The padding bytes will be preserved (not overwritten).

Note that calling this function does not guarantee that the returned compressed data will be placed into the specified buffer. In the event that the encoded data will not fit into the buffer provided, the returned packet MAY point to an internal buffer, as it would if this call were never used. In this event, the output packet will NOT have any padding, and the application must free space and copy it to the proper place. This is of particular note in configurations that may output multiple packets for a single encoded frame (e.g., lagged encoding) or if the application does not reset the buffer periodically.

Applications may restore the default behavior of the codec providing the compressed data buffer by calling this function with a NULL buffer.

Applications MUST NOT call this function during iteration of aom_codec_get_cx_data().

Parameters

  [in] | ctx | Pointer to this instance's context
  [in] | buf | Buffer to store compressed data into
  [in] | pad_before | Bytes to skip before writing compressed data
  [in] | pad_after | Bytes to skip after writing compressed data

Return values

  AOM_CODEC_OK | The buffer was set successfully.
  AOM_CODEC_INVALID_PARAM | A parameter was NULL, the image format is unsupported, etc.

## ◆ aom_codec_get_cx_data()

const aom_codec_cx_pkt_t * aom_codec_get_cx_data | ( | aom_codec_ctx_t * | ctx,
|  | aom_codec_iter_t * | iter )

Encoded data iterator.

Iterates over a list of data packets to be passed from the encoder to the application. The different kinds of packets available are enumerated in aom_codec_cx_pkt_kind.

AOM_CODEC_CX_FRAME_PKT packets should be passed to the application's muxer. Multiple compressed frames may be in the list. AOM_CODEC_STATS_PKT packets should be appended to a global buffer.

The application MUST silently ignore any packet kinds that it does not recognize or support.

The data buffers returned from this function are only guaranteed to be valid until the application makes another call to any aom_codec_* function.

Parameters

  [in] | ctx | Pointer to this instance's context
  [in,out] | iter | Iterator storage, initialized to NULL

Returns
  Returns a pointer to an output data packet (compressed frame data, two-pass statistics, etc.) or NULL to signal end-of-list.

## ◆ aom_codec_get_preview_frame()

const aom_image_t * aom_codec_get_preview_frame | ( | aom_codec_ctx_t * | ctx | ) |

Get Preview Frame.

Returns an image that can be used as a preview. Shows the image as it would exist at the decompressor. The application MUST NOT write into this image buffer.

Parameters

  [in] | ctx | Pointer to this instance's context

Returns
  Returns a pointer to a preview image, or NULL if no image is available.