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:
- Profile 0: 8-bit color depth, 4:2:0 chroma subsampling (most widely compatible).
- Profile 1: 8-bit color depth, 4:2:2, 4:4:0, or 4:4:4 chroma subsampling.
- Profile 2: 10-bit or 12-bit color depth, 4:2:0 chroma subsampling.
- Profile 3: 10-bit or 12-bit color depth, 4:2:2, 4:4:0, or 4:4:4 chroma subsampling.
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.webmImportant Considerations
- Pixel Format Alignment: The pixel format
(
-pix_fmt) must match the profile’s capabilities. If you set-profile:v 2but pass an 8-bit pixel format (likeyuv420p), the encoder initialization will fail. - Profile 0 (Standard 8-bit):
-profile:v 0paired with-pix_fmt yuv420p. - Profile 2 (Standard 10-bit):
-profile:v 2paired with-pix_fmt yuv420p10le.
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
- Define the Configuration Structure: Instantiate the encoder configuration structure.
- Set the Profile: Assign the profile integer (0, 1,
2, or 3) to the
g_profilemember variable. - 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
}
}