Set VP9 Profile in libvpx-vp9 Encoder

This article explains how to explicitly define the VP9 profile when initializing the libvpx-vp9 encoder. You will learn how to target specific profiles using both FFmpeg command-line flags and the native libvpx C API, ensuring your output video matches your precise color depth and chroma subsampling requirements.

VP9 Profiles Overview

The VP9 codec supports four profiles, each defining different bit depths and chroma subsampling configurations:


Method 1: Using the FFmpeg Command Line

When using FFmpeg, the libvpx-vp9 encoder automatically selects a profile based on the input pixel format. However, you can explicitly force a profile using the -profile:v flag.

Command Example

To encode a video explicitly using Profile 2 (for 10-bit video):

ffmpeg -i input.mp4 -c:v libvpx-vp9 -profile:v 2 -pix_fmt yuv420p10le output.webm

Important Considerations


Method 2: Using the Native libvpx C API

If you are developing an application directly with the libvpx library, you configure the encoder profile by modifying the vpx_codec_enc_cfg_t configuration structure before initialization.

Step-by-Step Implementation

  1. Define the Configuration Structure: Instantiate the encoder configuration structure.
  2. Set the Profile: Assign the profile integer (0, 1, 2, or 3) to the g_profile member variable.
  3. Initialize the Encoder: Pass the configuration to vpx_codec_enc_init.

Code Example

#include <vpx/vpx_encoder.h>
#include <vpx/vp8cx.h>

void initialize_vp9_encoder() {
    vpx_codec_ctx_t codec;
    vpx_codec_enc_cfg_t cfg;
    vpx_codec_iface_t *interface = vpx_codec_vp9_cx();

    // Populate configuration with default values
    vpx_codec_enc_config_default(interface, &cfg, 0);

    // Explicitly define the VP9 profile
    // Use 0 for Profile 0, 1 for Profile 1, 2 for Profile 2, 3 for Profile 3
    cfg.g_profile = 2; 

    // Set other configuration properties to match the profile requirements
    cfg.g_w = 1920;
    cfg.g_h = 1080;
    cfg.g_bit_depth = VPX_BITS_10; // Must align with Profile 2 (10-bit)
    cfg.g_input_bit_depth = 10;

    // Initialize the encoder context
    vpx_codec_err_t res = vpx_codec_enc_init(&codec, interface, &cfg, VPX_CODEC_USE_HIGHBITDEPTH);
    if (res != VPX_CODEC_OK) {
        // Handle initialization error
    }
}