# Encoder Initial QP Override

Table of Contents

- [Encoder Initial QP Override](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#encoder-initial-qp-override)

    - [Encoder Initial Quantization Parameter (QP)](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#encoder-initial-quantization-parameter-qp)
    - [Encoder Initial QP Override extensions](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#encoder-initial-qp-override-extensions)
    - [Query device capabilities](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#query-device-capabilities)
    - [Using the Initial QP Override extension](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#using-the-initial-qp-override-extension)

## [Encoder Initial Quantization Parameter (QP)](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#id2)[](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#encoder-initial-quantization-parameter-qp)

Video/Image encoding involves mapping the signal levels to discrete values that are more easily compressed.
[Quantization](https://en.wikipedia.org/wiki/Quantization) is a lossy process and the levels of quantization govern the quality versus compression.
Encoders typically start off with a default QP at the beginning.
Based on the configured bitrate and scene complexity, encoders try to arrive at the right QP value by continuously monitoring the complexity and redundancy across frames.
It may take a couple of seconds to a few seconds for the encoder to arrive at a steady state to predict the right QP value to match the target bitrate (also known as rate convergence).

The initial QP is the default QP value used by the encoder. This QP value is tuned based on the complexity analysis on a large dataset. However, this may not work for cases where the first frame is either too complex or too simple to encode.
For advanced applications that are aware of scene compexity, the Initial QP Override extension allows applications to override the initial QP used by the encoder and achieve rate convergence much faster.
Applications can specify the QP override for each frame type (I/P/B).

## [Encoder Initial QP Override extensions](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#id3)[](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#encoder-initial-qp-override-extensions)

The extensions to enable and set initial QP override for I, P and B frames are:

| API KEY | Value Type | Details |
| --- | --- | --- |
| [`KEY_INIT_QP_I_FRAME_ENABLE`](https://docs.qualcomm.com/doc/80-56386-10/topic/classqti_1_1video_1_1QMediaExtensions.html#_CPPv4N3qti5video16QMediaExtensionsE) | Integer | Enables override of initial QP value for I frame ( 0 - Disable, 1 - Enable). |
| [`KEY_INIT_QP_P_FRAME_ENABLE`](https://docs.qualcomm.com/doc/80-56386-10/topic/classqti_1_1video_1_1QMediaExtensions.html#_CPPv4N3qti5video16QMediaExtensionsE) | Integer | Enables override of initial QP value for P frame( 0 - Disable, 1 - Enable). |
| [`KEY_INIT_QP_B_FRAME_ENABLE`](https://docs.qualcomm.com/doc/80-56386-10/topic/classqti_1_1video_1_1QMediaExtensions.html#_CPPv4N3qti5video16QMediaExtensionsE) | Integer | Enables override of initial QP value for B frame( 0 - Disable, 1 - Enable). |
| [`KEY_INIT_QP_I_FRAME`](https://docs.qualcomm.com/doc/80-56386-10/topic/classqti_1_1video_1_1QMediaExtensions.html#_CPPv4N3qti5video16QMediaExtensionsE) | Integer | Initial I frame QP value. See [Query device capabilities](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#qp-query-caps-section-label) for the range of allowed values. |
| [`KEY_INIT_QP_P_FRAME`](https://docs.qualcomm.com/doc/80-56386-10/topic/classqti_1_1video_1_1QMediaExtensions.html#_CPPv4N3qti5video16QMediaExtensionsE) | Integer | Initial P frame QP value. See [Query device capabilities](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#qp-query-caps-section-label) for the range of allowed values. |
| [`KEY_INIT_QP_B_FRAME`](https://docs.qualcomm.com/doc/80-56386-10/topic/classqti_1_1video_1_1QMediaExtensions.html#_CPPv4N3qti5video16QMediaExtensionsE) | Integer | Initial B frame QP value. See [Query device capabilities](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#qp-query-caps-section-label) for the range of allowed values. |

Note

It is required to enable the initial QP with KEY\_INIT\_QP\_X\_FRAME\_ENABLE to
set the initial QP value via KEY\_INIT\_QP\_X\_FRAME (where X is I, P or B).

Note

To configure the initial QP for P frame, it is required to enable and configure the initial QP for I frame.

To configure the initial QP for B frame, it is required to enable and configure the initial QP for both I and P frames.

## [Query device capabilities](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#id4)[](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#query-device-capabilities)

MediaCodec codec = MediaCodec.createByCodecName("c2.qti.hevc.encoder");
    // codec.getSupportedVendorParameters() should contain KEY_INIT_QP_I_FRAME_ENABLE, KEY_INIT_QP_P_FRAME_ENABLE,
    // KEY_INIT_QP_B_FRAME_ENABLE, KEY_INIT_QP_I_FRAME, KEY_INIT_QP_P_FRAME and KEY_INIT_QP_B_FRAME
    // codec.getParameterDescriptor(KEY_INIT_QP_I_FRAME_ENABLE).getType() -> MediaFormat.TYPE_INTEGER
    
    QMediaCodecCapabilities qCodecCaps = QMediaCodecCapabilities.CreateForCodec("c2.qti.hevc.encoder", "video/hevc");
    SupportedValues<Integer> iFrameEnableValues = qCodecCaps.getParameterRangeInteger(KEY_INIT_QP_I_FRAME_ENABLE);
    // iFrameEnableValues.getType() -> SupportedValues.TYPE_VALUES
    // iFrameEnableValues.getValues() -> {0, 1}
    
    // KEY_INIT_QP_P_FRAME_ENABLE & KEY_INIT_QP_B_FRAME_ENABLE should have same supported values
    
    // qCodecCaps.getParameterDescriptor(KEY_INIT_QP_I_FRAME).getType() -> MediaFormat.TYPE_INTEGER
    SupportedValues<Integer> iFrameQpValues = qCodecCaps.getParameterRangeInteger(KEY_INIT_QP_I_FRAME);
    // iFrameQpValues.getType() -> SupportedValues.TYPE_RANGE
    
    // KEY_INIT_QP_P_FRAME & KEY_INIT_QP_B_FRAME should have similar supported values
    Copy to clipboard

## [Using the Initial QP Override extension](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#id5)[](https://docs.qualcomm.com/doc/80-56386-10/topic/encoder_qp.html#using-the-initial-qp-override-extension)

MediaCodec codec = MediaCodec.createEncoderByType("video/hevc");
    MediaFormat format = MediaFormat.createVideoFormat("video/hevc", 1280, 720);
    
    /*
     * Enable encoder initial QP value override feature for all frame types
     */
    format.setInteger(KEY_INIT_QP_I_FRAME_ENABLE,1);
    format.setInteger(KEY_INIT_QP_P_FRAME_ENABLE,1);
    format.setInteger(KEY_INIT_QP_B_FRAME_ENABLE,1);
    
    /*
     * Set override initial QP values for I, P and B frames
     */
    format.setInteger(KEY_INIT_QP_I_FRAME, 20);
    format.setInteger(KEY_INIT_QP_P_FRAME, 30);
    format.setInteger(KEY_INIT_QP_B_FRAME, 40);
    
    codec.configure(format);
    Copy to clipboard

Last Published: Jun 13, 2023

[Previous Topic
Region Of Interest Encoding](https://docs.qualcomm.com/bundle/publicresource/80-56386-10/topics/roi.md) [Next Topic
Advanced Encoder Frame-QP Control](https://docs.qualcomm.com/bundle/publicresource/80-56386-10/topics/advance_qp_control.md)