How to Extract Encoding Metrics From libaom?

This article provides a practical guide on how to extract real-time encoding statistics and metrics directly from libaom, the reference software encoder for the AV1 video format. It covers the specific API functions required to retrieve frame-level data, the configuration parameters needed to enable logging, and a brief look at how these metrics can be utilized for quality analysis and bitrate control optimization.

Enabling Metric Collection in libaom

To extract statistics directly from libaom during the encoding process, you must utilize the AV1 Encoder Control interface (aom_codec_control). The encoder exposes several control IDs specifically designed to pass internal state data back to your application after a frame is processed.

Before querying metrics, ensure your encoder instance is properly initialized and that you are checking the return values of your control calls to guarantee the underlying structure is populated.

Key API Controls and Structures

The libaom API uses specific control flags to retrieve different types of encoding data. Below are the primary controls used for extracting metrics:

When handling frame-by-frame loops, you will typically call aom_codec_control immediately after a successful aom_codec_encode and aom_codec_get_cx_data cycle.

// Example snippet for extracting basic encoder stats
aom_encode_stats_t stats;
if (aom_codec_control(&codec, AV1E_GET_ENCODE_STATS, &stats) == AOM_CODEC_OK) {
    // Process your metrics here
    printf("Frame size: %zu bits\n", stats.frame_size_in_bits);
}

Extracting Pass-Specific Statistics

If you are performing multi-pass encoding, libaom relies on a dedicated statistics buffer to pass information between the first and second analysis stages.

Utilizing the Extracted Data

The metrics extracted directly from the libaom structures can be leveraged for several advanced video engineering workflows: