Skip to content

AMF Encoder Settings and Tuning in FFmpeg

Huts, Roman edited this page Nov 1, 2023 · 6 revisions

Advanced Micro Devices

AMF Encoder Settings and Tuning in FFmpeg

Usage Guide


Disclaimer

The information presented in this document is for informational purposes only and may contain technical inaccuracies, omissions, and typographical errors. The information contained herein is subject to change and may be rendered inaccurate for many reasons, including but not limited to product and roadmap changes, component and motherboard version changes, new model and/or product releases, product differences between differing manufacturers, software changes, BIOS flashes, firmware upgrades, or the like. Any computer system has risks of security vulnerabilities that cannot be completely prevented or mitigated. AMD assumes no obligation to update or otherwise correct or revise this information. However, AMD reserves the right to revise this information and to make changes from time to time to the content hereof without obligation of AMD to notify any person of such revisions or changes. THIS INFORMATION IS PROVIDED ‘AS IS.” AMD MAKES NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE CONTENTS HEREOF AND ASSUMES NO RESPONSIBILITY FOR ANY INACCURACIES, ERRORS, OR OMISSIONS THAT MAY APPEAR IN THIS INFORMATION. AMD SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR ANY PARTICULAR PURPOSE. IN NO EVENT WILL AMD BE LIABLE TO ANY PERSON FOR ANY RELIANCE, DIRECT, INDIRECT, SPECIAL, OR OTHER CONSEQUENTIAL DAMAGES ARISING FROM THE USE OF ANY INFORMATION CONTAINED HEREIN, EVEN IF AMD IS EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

AMD, the AMD Arrow logo, and combinations thereof are trademarks of Advanced Micro Devices, Inc. Other product names used in this publication are for identification purposes only and may be trademarks of their respective companies.

© 2023 Advanced Micro Devices, Inc. All rights reserved.


Copyright Notice

© 2023 Advanced Micro Devices, Inc. All rights reserved

Notice Regarding Standards. AMD does not provide a license or sublicense to any Intellectual Property Rights relating to any standards, including but not limited to any audio and/or video codec technologies such as MPEG-2, MPEG-4; AVC/H.264; HEVC/H.265; AV1; AAC decode/FFMPEG; AAC encode/FFMPEG; VC-1; and MP3 (collectively, the “Media Technologies”). For clarity, you will pay any royalties due for such third party technologies, which may include the Media Technologies that are owed as a result of AMD providing the Software to you.

MIT license

Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Table of Contents

  1. Introduction
  2. AMF Encoder Parameter Listings
  3. AMF Encoder Parameter Dependencies
  4. AMF Encoder Parameter Priorities
  5. 5 Settings of Specific Parameters
  6. Tools for Performance and Quality Tuning
  7. Acronyms

1 Introduction

The Advanced Media Framework (AMF) provides developers with optimal access to AMD GPU for multimedia processing.

AMD AMF Encoder is a professional video encoder that provides powerful video encoding capabilities and a wide range of customization options. It is designed to meet the individual needs of different users. Users can adjust the encoder's parameter settings to meet different encoding requirements, such as resolution, bit rate, frame rate, encoding quality, and more. These parameter settings can be customized based on users' needs to meet different video encoding scenarios and device requirements. Additionally, the AMD AMF Encoder supports multiple encoding formats, including H.264, H.265, and AV1, to satisfy different users' video encoding format requirements.

AMF is effectively supported by FFmpeg to significantly speed up video encoding, decoding, and transcoding on AMD GPUs.

The following user guides show how to set the FFmpeg command line to efficiently use the AMD AMF encoder for various personalized video encoding needs.

2 AMF Encoder Parameter Listings

The tables below list all parameter names, value types, available values, and descriptions for the AMF encoder H.264, HEVC and AV1 that can be set in FFmpeg:

AMF Encoder Parameter Listings for H.264

Parameter Name Value Type Values Description
usage enum transcoding
ultralowlatency
lowlatency
webcam
high_quality
Encoder Usage (default transcoding)
profile enum main
high
constrained_baseline
constrained_high
Profile (default main)
level enum auto
1.0
1.1
1.2
1.3
2.0
2.1
2.2
3.0
3.1
3.2
4.0
4.1
4.2
5.0
5.1
5.2
6.0
6.1
6.2
Profile Level (default auto)
quality enum speed
balanced
quality
Quality Preference (default speed)
rc enum cqp
cbr
vbr_peak
vbr_latency
qvbr
hqvbr
hqcbr
Rate Control Method (default -1, reserved for internal use)
qvbr_quality_level int Sets the QVBR quality level (from -1 to 51) (default -1, reserved for internal use)
enforce_hrd boolean true, false Enforce HRD (default false)
filler_data boolean true, false Filler Data Enable (default false)
refs int Reference frame number (default -1, reserved for internal use)
aspect int Frame aspect (default 0)
qmin int Minimum QP value (from -1 to 51) (default -1, reserved for internal use)
qmax int Maximum QP value (from -1 to 51) (default -1, reserved for internal use)
maxrate int Maximum bitrate (in bits/s). Used for VBV together with bufsize.
r decimal Set frame rate (Hz value, fraction or abbreviation).
bufsize int Set ratecontrol buffer size (in bits).
b int Target bitrate (default 2M)
g int Frame number in Group of Pictures (default 250)
slices int Slice number within a frame (default 1)
vbaq boolean true, false Enable VBAQ (default false)
frame_skipping boolean true, false Rate Control Based Frame Skip (default false)
qp_i int Quantization Parameter for I-Frame (from -1 to 51) (default -1, reserved for internal use)
qp_p int Quantization Parameter for P-Frame (from -1 to 51) (default -1, reserved for internal use)
qp_b int Quantization Parameter for B-Frame (from -1 to 51) (default -1, reserved for internal use)
preencode boolean true, false Pre-encode assisted rate control (default false)
max_au_size int Maximum Access Unit Size for rate control (in bits) (from 0 to INT_MAX) (default 0)
header_spacing int Header Insertion Spacing (from -1 to 1000) (default -1, reserved for internal use)
bf_delta_qp int B-Picture Delta QP (from -10 to 10) (default 4)
bf_ref boolean true, false Enable Reference to B-Frames (default true)
bf_ref_delta_qp int Reference B-Picture Delta QP (from -10 to 10) (default 4)
max_b_frames int Maximum number of consecutive B Pictures (from -1 to 3) (default -1, reserved for internal use)
bf int B Picture Pattern (from -1 to 3) (default -1, reserved for internal use)
intra_refresh_mb int Intra Refresh MBs Number Per Slot in Macroblocks (from 0 to INT_MAX) (default 0)
coder enum auto
cavlc
cabac
Coding Type (from 0 to 2) (default auto)
high_motion_quality_boost_enable boolean true, false Enable High motion quality boost mode (default auto)
me_half_pel boolean true, false Enable ME Half Pixel (default true)
me_quarter_pel boolean true, false Enable ME Quarter Pixel (default true)
aud boolean true, false Inserts AU Delimiter NAL unit (default false)
log_to_dbg boolean true, false Enable AMF logging to debug output (default false)
preanalysis boolean true, false Enable preanalysis (default auto)
pa_activity_type enum y
yuv
Set the type of activity analysis (default -1, reserved for internal use)
pa_scene_change_detection_enable boolean true, false Enable scene change detection (default auto)
pa_scene_change_detection_sensitivity enum low
medium
high
Set the sensitivity of scene change detection (default -1, reserved for internal use)
pa_static_scene_detection_enable boolean true, false Enable static scene detection (default auto)
pa_static_scene_detection_sensitivity enum low
medium
high
Set the sensitivity of static scene detection (default -1, reserved for internal use)
pa_initial_qp_after_scene_change int The QP value that is used immediately after a scene change (from -1 to51) (default -1, reserved for internal use)
pa_max_qp_before_force_skip int The QP threshold to allow a skip frame (from -1 to 51) (default -1, reserved for internal use)
pa_caq_strength enum low
medium
high
Content Adaptive Quantization strength (default -1, reserved for internal use)
pa_frame_sad_enable boolean true, false Enable Frame SAD algorithm (default auto)
pa_ltr_enable boolean true, false Enable long term reference frame management (default auto)
pa_lookahead_buffer_depth int Sets the PA lookahead buffer size (from -1 to 41) (default -1, reserved for internal use)
pa_paq_mode enum none
caq
Sets the perceptual adaptive quantization mode (default -1, reserved for internal use)
pa_taq_mode enum none
1
2
Sets the temporal adaptive quantization mode (default -1, reserved for internal use)
pa_high_motion_quality_boost_mode enum none
auto
Sets the PA high motion quality boost mode (default -1, reserved for internal use)
pa_adaptive_mini_gop boolean true, false Enable Adaptive MiniGOP (default auto)

AMF Encoder Parameter Listings for HEVC

Parameter Name Value Type Values Description
usage enum transcoding
ultralowlatency
lowlatency
webcam
high_quality
Encoder Usage (default transcoding)
profile enum main Profile (default main)
profile_tier enum main
high
Tier (default main)
level enum auto
1.0<
2.0
2.1
3.0
3.1
4.0
4.1
5.0
5.1
5.2
6.0
6.1
6.2
Set the encoding level (default auto)
quality enum speed
balanced
quality
Quality Preference (default speed)
rc enum cqp
cbr
vbr_peak
vbr_latency
qvbr
hqvbr
hqcbr
Rate Control Method (default -1, reserved for internal use)
qvbr_quality_level int Sets the QVBR quality level (from -1 to 51) (default -1, reserved for internal use)
header_insertion_mode enum none
gop
idr
Sets the header insertion mode (default none)
high_motion_quality_boost_enable boolean true, false Enable High motion quality boost mode (default auto)
gops_per_idr int GOPs per IDR 0-no IDR will be inserted (from 0 to INT_MAX) (default 1)
preencode boolean true, false Pre-encode assisted rate control (default false)
vbaq boolean true, false Enable VBAQ (default false)
enforce_hrd boolean true, false Enforce HRD (default false)
filler_data boolean true, false Filler Data Enable (default false)
max_au_size int Maximum Access Unit Size for rate control (in bits) (from 0 to INT_MAX) (default 0)
min_qp_i int Min quantization parameter for I-frame (from -1 to 51) (default -1)
max_qp_i int Max quantization parameter for I-frame (from -1 to 51) (default -1)
min_qp_p int Min quantization parameter for P-frame (from -1 to 51) (default -1)
max_qp_p int Max quantization parameter for P-frame (from -1 to 51) (default -1)
qp_i int Quantization Parameter for I-Frame (from -1 to 51) (default -1, reserved for internal use)
qp_p int Quantization Parameter for P-Frame (from -1 to 51) (default -1, reserved for internal use)
maxrate int Maximum bitrate (in bits/s). Used for VBV together with bufsize.
r decimal Set frame rate (Hz value, fraction or abbreviation).
bufsize int Set ratecontrol buffer size (in bits).
b int Target bitrate (default 2M)
frame_skipping boolean true, false Rate Control Based Frame Skip (default false)
refs int Reference frame number (default -1, reserved for internal use)
aspect int Frame aspect (default 0)
qmin int Minimum QP value (from -1 to 51) (default -1, reserved for internal use)
qmax int Maximum QP value (from -1 to 51) (default -1, reserved for internal use)
b int Target bitrate (default 2M)
g int Frame number in Group of Pictures (default 250)
slices int Slice number within a frame (default 1)
me_half_pel boolean true, false Enable ME Half Pixel (default true)
me_quarter_pel boolean true, false Enable ME Quarter Pixel (default true)
aud boolean true, false Inserts AU Delimiter NAL unit (default false)
log_to_dbg boolean true, false Enable AMF logging to debug output (default false)
preanalysis boolean true, false Enable preanalysis (default auto)
pa_activity_type enum y
yuv
Set the type of activity analysis (default -1, reserved for internal use)
pa_scene_change_detection_enable boolean true, false Enable scene change detection (default auto)
pa_scene_change_detection_sensitivity enum low
medium
high
Set the sensitivity of scene change detection (default -1, reserved for internal use)
pa_static_scene_detection_enable boolean true, false Enable static scene detection (default auto)
pa_static_scene_detection_sensitivity enum low
medium
high
Set the sensitivity of static scene detection (default -1, reserved for internal use)
pa_initial_qp_after_scene_change int The QP value that is used immediately after a scene change (from -1 to51) (default -1, reserved for internal use)
pa_max_qp_before_force_skip int The QP threshold to allow a skip frame (from -1 to 51) (default -1, reserved for internal use)
pa_caq_strength enum low
medium
high
Content Adaptive Quantization strength (default -1, reserved for internal use)
pa_frame_sad_enable boolean true, false Enable Frame SAD algorithm (default auto)
pa_ltr_enable boolean true, false Enable long term reference frame management (default auto)
pa_lookahead_buffer_depth int Sets the PA lookahead buffer size (from -1 to 41) (default -1, reserved for internal use)
pa_paq_mode enum none
caq
Sets the perceptual adaptive quantization mode (default -1, reserved for internal use)
pa_taq_mode enum none
1
2
Sets the temporal adaptive quantization mode (default -1, reserved for internal use)
pa_high_motion_quality_boost_mode enum none
auto
Sets the PA high motion quality boost mode (default -1, reserved for internal use)
pa_adaptive_mini_gop boolean true, false Enable Adaptive MiniGOP (default auto)

AMF Encoder Parameter Listings for AV1

Parameter Name Value Type Values Description
usage enum transcoding
lowlatency
Encoder Usage (default transcoding)
profile enum main Profile (default main)
level enum auto
2.0
2.1
2.2
2.3
3.0
3.1
3.2
3.3
4.0
4.1
4.2
4.3
5.0
5.1
5.2
5.3
6.0
6.1
6.2
6.3
7.0
7.1
7.2
7.3
Profile Level (default auto)
quality enum speed
balanced
quality
high_quality
Quality Preference (default speed)
rc enum cqp
cbr
vbr_peak
vbr_latency
qvbr
hqvbr
hqcbr
Rate Control Method (default -1, reserved for internal use)
qvbr_quality_level int Sets the QVBR quality level (from -1 to 51) (default -1, reserved for internal use)
header_insertion_mode enum none
gop
idr
Sets the header insertion mode (default none)
high_motion_quality_boost_enable boolean true, false Enable High motion quality boost mode (default auto)
preencode boolean true, false Pre-encode assisted rate control (default false)
enforce_hrd boolean true, false Enforce HRD (default false)
filler_data boolean true, false Filler Data Enable (default false)
min_qp_i int Min quantization parameter for I-frame (from -1 to 255, 0 is not allowed) (default -1)
max_qp_i int Max quantization parameter for I-frame (from -1 to 255, 0 is not allowed) (default -1)
min_qp_p int Min quantization parameter for P-frame (from -1 to 255, 0 is not allowed) (default -1)
max_qp_p int Max quantization parameter for P-frame (from -1 to 255, 0 is not allowed) (default -1)
qp_i int Quantization Parameter for I-Frame (from -1 to 255, 0 is not allowed) (default -1, reserved for internal use)
qp_p int Quantization Parameter for P-Frame (from -1 to 255, 0 is not allowed) (default -1, reserved for internal use)
maxrate int Maximum bitrate (in bits/s). Used for VBV together with bufsize.
r decimal Set frame rate (Hz value, fraction or abbreviation).
bufsize int Set ratecontrol buffer size (in bits).
b int Target bitrate (default 2M)
frame_skipping boolean true, false Rate Control Based Frame Skip (default false)
align enum 64x16
1080p
none
alignment mode (default none)
refs int Reference frame number (default -1, reserved for internal use)
aspect int Frame aspect (default 0)
qmin int Minimum QP value (from -1 to 255, 0 is not allowed) (default -1, reserved for internal use)
qmax int Maximum QP value (from -1 to 255, 0 is not allowed) (default -1, reserved for internal use)
b int Target bitrate (default 2M)
g int Frame number in Group of Pictures (default 250)
slices int Slice number within a frame (default 1)
preanalysis boolean true, false Enable preanalysis (default auto)
pa_activity_type enum y
yuv
Set the type of activity analysis (default -1, reserved for internal use)
pa_scene_change_detection_enable boolean true, false Enable scene change detection (default auto)
pa_scene_change_detection_sensitivity enum low
medium
high
Set the sensitivity of scene change detection (default -1, reserved for internal use)
pa_static_scene_detection_enable boolean true, false Enable static scene detection (default auto)
pa_static_scene_detection_sensitivity enum low
medium
high
Set the sensitivity of static scene detection (default -1, reserved for internal use)
pa_initial_qp_after_scene_change int The QP value that is used immediately after a scene change (from -1 to51) (default -1, reserved for internal use)
pa_max_qp_before_force_skip int The QP threshold to allow a skip frame (from -1 to 51) (default -1, reserved for internal use)
pa_caq_strength enum low
medium
high
Content Adaptive Quantization strength (default -1, reserved for internal use)
pa_frame_sad_enable boolean true, false Enable Frame SAD algorithm (default auto)
pa_ltr_enable boolean true, false Enable long term reference frame management (default auto)
pa_lookahead_buffer_depth int Sets the PA lookahead buffer size (from -1 to 41) (default -1, reserved for internal use)
pa_paq_mode enum none
caq
Sets the perceptual adaptive quantization mode (default -1, reserved for internal use)
pa_taq_mode enum none
1
2
Sets the temporal adaptive quantization mode (default -1, reserved for internal use)
pa_high_motion_quality_boost_mode enum none
auto
Sets the PA high motion quality boost mode (default -1, reserved for internal use)
pa_adaptive_mini_gop boolean true, false Enable Adaptive MiniGOP (default auto)

In FFmpeg all AMF encoders use _amf as postfix for encoder name such as h264_amf, hevc_amf and av1_amf.

Based on the above AMF Encoder Parameters List, it's easy to get started with your first AMF encoder setup.

ffmpeg -s 1920x1080 -pix_fmt yuv420p -i input.yuv -c:v h264_amf -usage  transcoding output.mp4

This will use the AMF H.264 encoder to encode a yuv file with an input resolution of 1920x1080 into an H.264 format video stream with an intended usage scenario of "high_quality". The output stream will be packaged in an MP4 container.

Please note that all FFmpeg command lines in this document use the AMF H.264 encoder h.264_amf as an example. For the vast majority of examples, you can replace the encoder to the AMF HEVC encoder hevc_amf or the AV1 encoder av1_amf. Before replacing to hevc_amf or av1_amf, please refer to the table of AMF Encoder Parameter Listings - HEVC and AMF Encoder Parameter Listings - AV1 to ensure that these parameters are applicable.

3 AMF Encoder Parameter Dependencies

In the parameter list, some parameters can be set independently, while others cannot, which we call "non-independent parameters".

The "non-independent parameters" have dependencies on other parameters which we call “dependent parameters”. Only when the “dependent parameters” are correctly set can these "non-independent parameters" be set.

The table below lists the dependencies between parameters.

The parameters listed in the first column of the table are "non-independent parameters". Only when the "dependent parameters" are set to the values shown in the table, can the "non-independent parameters" be set.

In the table below, we have used abbreviations for the values of "rc" to keep the table concise:

cqp: CQP
cbr: CBR
vbr_peak: PCVBR
vbr_latency: LCVBR
qvbr: QVBR
hqvbr: HQVBR
hqcbr: HQCBR

AMF Encoder Parameter Dependencies

AMF Encoder Parameter Dependencies

In these "dependent parameters", the two most important parameters are "rc" and "preanalysis".

AMF encoder provides a rich set of rate control methods. These rate control methods work together with other parameters to provide optimal video quality. Many encoder parameters are related to rate control and only work when “rc” is set to a specific value.

For example, when using the non-independent parameters "qvbr_quality_level", "rc" must be set to "qvbr".

ffmpeg -s 1920x1080 -pix_fmt yuv420p -i input.yuv -c:v h264_amf -rc qvbr -qvbr_quality_level 18 output.mp4

All parameters in the non-independent parameters list starting with "pa_" depend on “preanalysis” being set to “true” to take effect. For example:

ffmpeg -i input.mp4 -c:v h264_amf -quality balanced -preanalysis true -pa_taq_mode 2 output.mp4

4 AMF Encoder Parameter Priorities

In the parameter list, some parameters have higher priority than others. When both high-priority and low-priority parameters are set in the command line, the high-priority parameters take effect and the low-priority parameters are ignored.

The table below lists the priorities between parameters.

The first column of the table represents the currently observed parameter, while the first row represents the comparative parameters. The value in the intersection cell of the currently observed parameter and comparative parameters represents the value of the corresponding comparative parameter. There is a priority issue between the currently observed parameter and the comparative parameter when the comparative parameter is set to the value in the cell. An upward-pointing arrow in the cell indicates that the comparative parameter has higher priority, while a left-pointing arrow indicates that the currently observed parameter has higher priority.

AMF Encoder Parameter Priorities

AMF Encoder Parameter Priorities

Next, we will use several examples to explain the priority of encoding parameters.

In the following parameter settings, both the "vbaq" and "pa_taq_mode" parameters are set. However, because "pa_taq_mode" has a higher priority than "vbaq" when "pa_taq_mode" is set to 1 or 2, "vbaq" is ignored. The two command lines below have the same effect in the encoder.

ffmpeg -s 1920x1080 -pix_fmt yuv420p -i input.yuv -c:v h264_amf -vbaq true -preanalysis true -pa_taq_mode 2 output.mp4

ffmpeg -s 1920x1080 -pix_fmt yuv420p -i input.yuv -c:v h264_amf -preanalysis true -pa_taq_mode 2 output.mp4

In the following parameter settings, both the "bf" and "pa_adaptive_mini_gop" parameters are set. However, because "pa_adaptive_mini_gop" has a higher priority than "bf" when "pa_adaptive_mini_gop" is set to true, "bf" is ignored. The two command lines below have the same effect in the encoder.

ffmpeg -s 1920x1080 -pix_fmt yuv420p -i input.yuv -c:v h264_amf -max_b_frames 3 -bf 2 -preanalysis true -pa_adaptive_mini_gop true output.mp4

ffmpeg -s 1920x1080 -pix_fmt yuv420p -i input.yuv -c:v h264_amf -max_b_frames 3 -preanalysis true -pa_adaptive_mini_gop true output.mp4

5 Settings of Specific Parameters

Next, we will introduce the usage methods of some important parameters or settings that require more attention.

-usage

The video encoder balances factors such as speed, quality, and latency. Therefore, it is important to have a deep understanding of your specific application scenario and to prioritize the importance of speed, quality, and latency for your business. Based on this understanding, setting up the encoder can ensure that the video stream output by the encoder best meets the needs of your business.

AMD has integrated several typical user scenario presets into the AMF encoder. Users can use these presets by setting the “usage” parameter.

Usage parameter supports typical application scenarios, including:

  • transcoding: Convert high-resolution or high-bitrate videos to low-resolution or low-bitrate videos for transmission or storage in bandwidth-limited network environments.
  • ultralowlatency: For real-time video applications, such as online gaming or video conferences, it is necessary to minimize the latency caused by the encoder to ensure the fastest response time.
  • lowlatency: For video streaming live applications, lower latency and higher video quality are required.
  • webcam: Suitable for desktop applications and video chat applications, which require a balance between video quality and encoding efficiency.
  • high_quality: Suitable for scenarios that require outputting high-quality videos, such as film and television production.
  • lowlatency_high_quality: This scenario usually occurs in applications that require high real-time requirements for video streams, such as live broadcasting, sports events, online games, and so on.

For each usage, AMF has optimized and preset the encoder's parameters based on the corresponding scenario. These parameter optimizations and presets cover the majority of the parameters, including but not limited to:

  • Encoding profile and level
  • GOP size and structure
  • Rate control mode and strategy
  • Motion estimation method and precision
  • Multi-Pass encoding
  • Deblocking filter strength
  • Adaptive quantization and rate distortion optimization
  • Bitrate and resolution constraints

By using these presets, users can easily and efficiently select the appropriate encoding settings for their specific usage scenario without the need for in-depth knowledge of the encoder's parameters and their impact on video quality and performance.

The usage scenario for webcam

ffmpeg -s 1920x1080 -pix_fmt yuv420p -i input.yuv -c:v h264_amf -usage webcam output.mp4

The usage scenario for ultralowlatency

ffmpeg -s 1920x1080 -pix_fmt yuv420p -i input.yuv -c:v h264_amf -usage ultralowlatency output.mp4

The usage scenario for high_quality

ffmpeg -s 1920x1080 -pix_fmt yuv420p -i input.yuv -c:v h264_amf -usage high_quality output.mp4

In some cases, users may want to customize specific parameters themselves. Any user-set parameter will have a higher priority and override the default setting for that parameter based on the usage preset in the encoder.

For example, in the following settings, the user explicitly sets the GOP size to 50. The encoder will use the user's GOP size setting instead of the one preset in the high_quality usage.

ffmpeg -s 1920x1080 -pix_fmt yuv420p -i input.yuv -c:v h264_amf -usage high_quality -g 50 output.mp4

-quality

This parameter is used to select between video quality and speed. This parameter has a significant impact on encoding speed.

It has three valid values:

  • quality: This preset is optimized for high-quality video output, suitable for applications such as video production, broadcasting, and live streaming.
  • balanced: This preset balances the trade-off between quality and speed, making it suitable for a variety of applications that require a balance between the two, such as video conferencing and online gaming.
  • speed: This preset prioritizes speed over quality, making it suitable for applications that require real-time video encoding with low latency, such as online gaming and remote desktop applications.
ffmpeg -i input.mp4 -c:v h264_amf -quality balanced output.mp4

ffmpeg -i input.mp4 -c:v h264_amf -quality quality output.mp4

ffmpeg -i input.mp4 -c:v h264_amf -quality speed output.mp4

-enforce_hrd

The Hypothetical Reference Decoder (HRD) helps to prevent buffer overflow and underflow, which can cause issues such as stuttering or freezing in the video playback. HRD may sacrifice a certain level of image quality. "enforce_hrd" parameter is not always necessary or appropriate for all types of scenario. It should be used selectively and with careful consideration of the specific characteristics of the video content being encoded.

ffmpeg -i input.mp4 -c:v h264_amf -enforce_hrd true output.mp4

-vbaq

VBAQ is a technique used to improve the visual quality of the encoded video. It achieves this by adapting the quantization parameters for blocks based on the visual complexity of the content. It is particularly effective for encoding video with complex visual content, such as high-motion or high-detail scenes.

ffmpeg -i input.mp4 -c:v h264_amf -vbaq true output.mp4

-preencode

Preencode is a process that occurs before the actual encoding takes place. During preencode, the video encoder analyzes the input video to determine its visual characteristics and complexity. This information is then used to optimize the encoding process for the specific video content.

Preencode can incur additional overhead, which is more noticeable in low-resolution videos.

ffmpeg -i input.mp4 -c:v h264_amf -preencode true output.mp4

-bf

Set the number of B-frames between two reference frames (I- or P-frames). B-frames have higher encoding efficiency than I- and P-frames, but they require a greater encoding overhead. The compression efficiency of B-frames decreases as the number of B-frames increases. It is recommended to set the number of B-frames to 1, and not to exceed 2 if possible.

Note currently this parameter is only applicable for H.264.

ffmpeg -i input.mp4 -c:v h264_amf -max_b_frames 3 -bf 1 output.mp4

-align

AV1 bitstream specification does not contain the cropping information for decoders to display the specific, pixel accurate resolution. It is expected that the proper cropping information should be presented in the container instead.

AMF AV1 Encoder introduces parameter “align” to address the hardware alignment requirement such that the encoded bitstream could be decoded and presented properly.

Values for setting “align”:

  • 64x16: Input videos whose resolution is aligned to 64x16 will be coded; input videos whose resolution is not aligned to 64x16 will not be coded; All other resolution videos will not be supported.
  • 1080p: Input videos whose resolution is aligned to 64x16, as well as 1920x1080 video, will be coded; All other resolution videos will not be supported. Note that for resolution of 1920x1080, the output video would have a resolution of 1920x1082. Two extra lines are padded at the bottom of the frame, filled with black pixels.
  • None: Videos with any resolution can be coded. However, for those videos whose resolution is not 64x16 aligned, their output resolution will be extrapolated to be 64x16 aligned and padded with black pixels. The exception is for resolution of 1080p, which will be padded to 1082p, as in the case of value “1080p”.

-high_motion_quality_boost_enable

"high_motion_quality_boost" is a parameter in video encoding that adjusts the level of detail in high-motion scenes to improve the visual quality of the encoded video. It is typically used to address issues such as motion blur, pixelation, and artifacting that can occur in videos with fast-moving objects or high levels of motion.

ffmpeg -i input.mp4 -c:v h264_amf -high_motion_quality_boost_enable true output.mp4

-preanalysis

Preanalysis is the process of analyzing the input video prior to the actual encoding process. Preanalysis can involve several different operations, such as scene detection, motion analysis, and quantization parameter decision. By analyzing the input video before encoding, the AMF video encoder can make better decisions about how to allocate bitrate and other encoding parameters to different parts of the video. This can help to improve the overall quality of the encoded video and reduce the overall bitrate required to achieve a specific level of quality.

All parameters related to preanalysis begin with "pa_". Before using these parameters, make sure that preanalysis is set to "true".

-pa_activity_type

Select the color components to be used for analyzing the visual characteristics and complexity of the input video. Only the Y component is used or all three YUV components. In general, using only the Y component is sufficient for obtaining usable results for most video sequences.

ffmpeg -i input.mp4 -c:v h264_amf -preanalysis true  -pa_activity_type y  output.mp4

-pa_scene_change_detection_enable

Whether to use scene change detection. If enabled, a scene change will be detected and a new GOP will be started at that point. This can affect the size of GOPs. The parameter 'pa_scene_change_detection_sensitivity' is used to control the sensitivity of the scene change detection.

ffmpeg -i input.mp4 -c:v h264_amf -preanalysis true -pa_scene_change_detection_enable true -pa_scene_change_detection_sensitivity medium output.mp4

-pa_ltr_enable

Whether to use LTR. LTR allows P-frames to reference a wider range of frames, including reference an earlier P-frame or an I-frame within a short time window, which can help to reduce these artifacts and improve overall visual quality.

However, the use of LTR requires additional encoding resources and can increase the complexity of the encoding process. Therefore, it is typically only used in scenarios where visual quality is a top priority and the additional encoding cost can be justified.

LTR and B-frames cannot be used simultaneously.

ffmpeg -i input.mp4 -c:v h264_amf -preanalysis true -pa_ltr_enable true output.mp4

-pa_lookahead_buffer_depth

With lookahead, the encoder analyzes a number of future frames and uses that information to optimize the current frame's encoding. “pa_lookahead_buffer_depth” will set the number of future frames to be buffered.

However, lookahead can cause encoding latency. In scenarios with high demands for low latency, lookahead depth should be carefully adjusted.

ffmpeg -i input.mp4 -c:v h264_amf -preanalysis true -pa_lookahead_buffer_depth 40 output.mp4

-pa_paq_mode

pa_paq_mode adjusts quantization parameters using a perceptual model to achieve better image quality.

ffmpeg -i input.mp4 -c:v h264_amf -preanalysis true -pa_paq_mode caq -pa_caq_strength medium output.mp4

-pa_taq_mode

pa_taq_mode adjusts quantization parameters using temporal correlation to achieve better image quality. For most video sequences, pa_taq_mode can achieve significant quality improvement.

ffmpeg -i input.mp4 -c:v h264_amf -quality balanced -preanalysis true -pa_taq_mode 2 output.mp4

Values for setting “pa_taq_mode”:

  • 1: Use shader resource to calculate the temporal correlation. Using a value of “1” generally improves the image quality, but at the cost of a decrease in frame rate when playing games simultaneously.
  • 2: Use hardware resource to calculate the temporal correlation. Using a value of “2” generally improves the image quality, but at the cost of a decrease in encoding speed.

-pa_high_motion_quality_boost_mode

"pa_high_motion_quality_boost_mode" is a parameter in video encoding that adjusts the level of detail in high-motion scenes to improve the visual quality of the encoded video in preanalysis process. It provides a noticeable improvement for videos with high resolution and intense motion.

ffmpeg -i input.mp4 -c:v h264_amf -quality balanced  -preanalysis true -pa_high_motion_quality_boost_mode auto output.mp4

-pa_adaptive_mini_gop

Adaptive mini-GOP allows the encoder to decide whether or not to use B-frames, and if so, how many, depending on the complexity of the video content. This helps to reduce the overall bit rate and maintain high video quality.

ffmpeg -i input.mp4 -c:v h264_amf -preanalysis true -max_b_frames 3 -pa_adaptive_mini_gop true output.mp4

6 Tools for Performance and Quality Tuning

After setting the parameters of the encoder, users typically want a quantitative method to measure the effectiveness of the encoder settings. This measure typically includes two aspects: encoding speed and video quality. Effective measurement tools can help users find the easy parameter settings during repeated tuning.

FFmpeg provides exactly such measurement tools. They are the benchmark performance measurement tool and the libvmaf quality measurement tool.

Performance measurement tool: benchmark

In FFmpeg, benchmark is a performance measurement tool used to test the speed and efficiency of FFmpeg's multimedia processing capabilities. It provides a way to measure the time it takes for FFmpeg to perform specific tasks, such as decoding or encoding a video, or transcoding audio from one format to another.

When the "-benchmark" option is enabled in command, FFmpeg will output a summary of the encoding or decoding speed, along with other relevant statistics, after the process is completed. This information can be useful for comparing the performance of different systems or for optimizing FFmpeg settings.

Here is an example of how to use the "-benchmark" option in FFmpeg:

ffmpeg -s 1920x1080 -pix_fmt yuv420p -i input.yuv -c:v h264_amf -quality balanced -benchmark output.mp4

In this example, FFmpeg will encode the input yuv file "input.yuv" using the AMF encoder with balanced quality. The "-benchmark" option is also included, which will output a performance summary after the encoding process is completed.

Here's an example output of benchmark:

bench: utime=8.953s stime=1.278s rtime=9.704s

bench: maxrss=59412kB

bench: IO reads=4023kB writes=2025kB

bench: speed=  9.4x

Each line of the output represents a different performance metric. Here's what each metric means:

  • utime: The amount of CPU time used by the process in user mode, in seconds.
  • stime: The amount of CPU time used by the process in kernel mode, in seconds.
  • rtime: The real time elapsed during the process, in seconds.
  • maxrss: The maximum resident set size (memory usage) of the process, in kilobytes.
  • IO reads: The total number of kilobytes read from disk by the process
  • IO writes: The total number of kilobytes written to disk by the process.
  • speed: The encoding or decoding speed, represented as a multiplier of real-time. For example, "100x" means the process was 100 times faster than real-time.

By analyzing these metrics, you can get an idea of the CPU and memory usage, I/O performance, and overall speed of the encoding or decoding process. You can use this information to compare the performance of different codecs, settings, or systems, and to optimize your FFmpeg workflows for maximum efficiency.

Quality measurement tool: libvmaf

You can use the libvmaf library in FFmpeg to calculate the Video Multi-Method Assessment Fusion (VMAF) score for a video. libvmaf is a video quality assessment algorithm developed by Netflix. It is designed to estimate the subjective quality of video content by comparing the original video with a distorted version of that video. It can be used to evaluate the quality of video codecs, compression settings, and other video processing algorithms.

In FFmpeg, libvmaf is implemented as a filter that can be applied to a video stream. When the filter is applied, it analyzes each frame of the video and generates a report of quality metrics such as the VMAF score, PSNR (Peak Signal-to-Noise Ratio) score, and MS-SSIM (Multi-Scale Structural Similarity Index Measure) score. These metrics can help to evaluate the quality of the video and optimize encoding settings for a desired level of quality.

Encoding quality evaluation case using libvmaf

ffmpeg -i output.mp4 -s 1920x1080 -i reference.yuv -lavfi libvmaf -f null -

In this example, FFmpeg will compare the encoded video file "output.mp4" with the reference yuv file "reference.yuv" using the libvmaf filter. Usually the reference yuv file is the input source for encoded video file during encoding process. The "-f null -" option is used to discard the output, as we are only interested in the VMAF score.

Transcoding quality evaluation case using libvmaf

ffmpeg -i output.mp4 -i reference.mp4 -lavfi libvmaf -f null -

In this example, FFmpeg will compare the encoded video file "output.mp4" with the reference video file "reference.mp4" using the libvmaf filter. Usually the reference video file is the input source for encoded video file. The "-f null -" option is used to discard the output, as we are only interested in the VMAF score.

An example output of libvmaf is

[Parsed_vmaf_0 @ 0x7f8b0c100000] VMAF score: 87.372121

In this example output, you can see the VMAF score (87.372121). This is the overall quality score for the video. It's a single number that summarizes the quality of the entire video. The higher the score, the better the quality.

Annex A: Glossary of Acronyms

Acronym Definition
AMD Advanced Micro Devices
AMF Advanced Media Framework
CBR Constant Bit Rate
CPU Central Processing Unit
CQP Constant Quantization Parameter
GOP Group of Picture
GPU Graphics Processing Unit
HEVC High Efficiency Video Coding
HQCBR Hight Quality Constant Bit Rate
HQVBR High Quality Variable Bit Rate
HRD Hypothetical Reference Decoder
LCVBR Latency Constrained Variable Bit Rate
LTR Long Term Reference
ME Motion Estimation
MKV Matroska Video files
MP4 MPEG-4 Part 14
PA Pre-Analysis
PCVBR Peak Constrained Variable Bit Rate
PSNR Peak Signal-to-Noise Ratio
QP Quantization Parameter
QVBR Quality Variable Bit Rate
SSIM Multi-Scale Structural Similarity Index Measure
VBR Variable Bit Rate
VMAF Multi-Method Assessment Fusion