# Configure

Source: [https://docs.qualcomm.com/doc/80-70014-20/topic/configure.html](https://docs.qualcomm.com/doc/80-70014-20/topic/configure.html)

Information on how to configure the V4L2 and GStreamer APIs for different features is
            provided here.

## Configure V4L2 APIs for different features

The following information is about V4L2 control IDs, accepted format, ranges, and
                sample code for different features:

**B-frame encode**

You can configure the VPU driver to enable B-frames in an encoder session using the
                    `VIDIOC_S_CTRL` ioctl system call. This configuration must be set
                before the `VIDIOC_STREAMON` on an output plane. The VPU driver
                supports B-frame encode only with the Hierarchical coding. The Dynamic setting is
                not supported.

| Control ID for H.264 and HEVC codec | Accepted format | Range |
| :---: | :---: | :---: |
| V4L2_CID_MPEG_VIDEO_B_FRAMESCopy to clipboard | Integer | <ul class="ul" id="concept_igj_kz3_bzb__ul_svf_x5v_m1c_eputla_02-21-24-1612-55-72"><br>                                        <li class="li">{0, 1}</li><br><br>                                        <li class="li">0: Disable B-frame</li><br><br>                                        <li class="li">1: Enable one B-frame</li><br><br>                                    </ul> |

The following code sample shows the static configuration to enable a single
                B-frame:

v4l2_control control;
    int ret = 0;
    int bframes = 1;
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_B_FRAMES;
    control.value = bframes;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    }
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_TYPE;
    control.value = 0;// V4L2_MPEG_VIDEO_HEVC_HIERARCHICAL_CODING_B
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) { 
        return -1;
    }
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_LAYER;
    control.value = 1;//Enable 1 layer
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) { 
        return -1;
    }Copy to clipboard

**Encoder initial QP override**

You can configure the VPU driver to override the initial frame Quantization Parameter
                (QP) for I, P, and B frames to an intended value of encoded frames. This is
                performed using the `VIDIOC_S_CTRL` ioctl system call. It also has
                the flexibility to set initial frame QP for I only, or P only, or B only, or I and P
                only, or P and B only and so on. This configuration must be set before the
                    `VIDIOC_STREAMON` on an output plane and is only applicable to an
                encoder session.

| Control ID for H.264 codec | Accepted format | QP range |
| --- | --- | --- |
| V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QPCopy to clipboard | Integer | Example according to the codec<br>                                        standard:<ul class="ul" id="concept_igj_kz3_bzb__ul_atj_wjk_n1c_eputla_02-23-24-1619-14-34"><br>                                        <li class="li">1 to 51 for 8‑bit</li><br><br>                                        <li class="li">1 to 63 (-12 to 51) for 10‑bit</li><br><br>                                    </ul> |
| V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QPCopy to clipboard | Integer | Example according to the codec<br>                                        standard:<ul class="ul" id="concept_igj_kz3_bzb__ul_atj_wjk_n1c_eputla_02-23-24-1619-14-34"><br>                                        <li class="li">1 to 51 for 8‑bit</li><br><br>                                        <li class="li">1 to 63 (-12 to 51) for 10‑bit</li><br><br>                                    </ul> |
| V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QPCopy to clipboard | Integer | Example according to the codec<br>                                        standard:<ul class="ul" id="concept_igj_kz3_bzb__ul_atj_wjk_n1c_eputla_02-23-24-1619-14-34"><br>                                        <li class="li">1 to 51 for 8‑bit</li><br><br>                                        <li class="li">1 to 63 (-12 to 51) for 10‑bit</li><br><br>                                    </ul> |
|  |  |  |
|  |  |  |

| Control ID for HEVC codec | Accepted format | QP range |
| --- | --- | --- |
| V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QPCopy to clipboard | Integer | Example according to the codec<br>                                        standard:<ul class="ul" id="concept_igj_kz3_bzb__ul_iwz_hlk_n1c_eputla_02-23-24-1630-15-248"><br>                                        <li class="li">1 to 51 for 8‑bit</li><br><br>                                        <li class="li">1 to 63 (-12 to 51) for 10‑bit</li><br><br>                                    </ul> |
| V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QPICopy to clipboard | Integer | Example according to the codec<br>                                        standard:<ul class="ul" id="concept_igj_kz3_bzb__ul_iwz_hlk_n1c_eputla_02-23-24-1630-15-248"><br>                                        <li class="li">1 to 51 for 8‑bit</li><br><br>                                        <li class="li">1 to 63 (-12 to 51) for 10‑bit</li><br><br>                                    </ul> |
| V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QPCopy to clipboard | Integer | Example according to the codec<br>                                        standard:<ul class="ul" id="concept_igj_kz3_bzb__ul_iwz_hlk_n1c_eputla_02-23-24-1630-15-248"><br>                                        <li class="li">1 to 51 for 8‑bit</li><br><br>                                        <li class="li">1 to 63 (-12 to 51) for 10‑bit</li><br><br>                                    </ul> |
|  |  |  |
|  |  |  |

The following code sample shows the static configuration parameter to choose the
                following: 
- 40 as an initial QP for I frame
- 35 as an initial QP for P-frame
- B-frame for H.264 session

v4l2_control control;
    int ret = 0;
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP;
    control.value = 40;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    }
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP;
    control.value = 35;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP;
    control.value = 35;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    }Copy to clipboard

**Slice encode**

You can configure the VPU driver to encode a single frame to multiple slices using
                the `VIDIOC_S_CTRL ioctl system` call. A slice boundary is specified
                as the number of bits per slice or the number of macroblocks per slice. This
                configuration must be set before the `VIDIOC_STREAMON` on an output
                plane. Dynamic configuration is not supported in this feature, and this is
                applicable only to an encoder session.

| Control ID for H.264 and HEVC | Accepted format | Range |
| :---: | :---: | :---: |
| V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODECopy to clipboard | enum<br>`<br>                                v4l2_mpeg_video_multi_slice_mode` | Values according to the codec standard:<br>enum<br><br><br>`<br>                                        v4l2_mpeg_video_multi_slice_mode` |
| V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTESCopy to clipboard | Integer | 512 - MAX\_BITRATE/8<br>Unit: bits/second |
| V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MBCopy to clipboard | Integer | 1 to 36864<br>Unit: Macroblocks |

The following code sample shows the static configuration to encode frames with
                multiple slices and with the maximum number of macroblocks per slice set to 400:

v4l2_control control;
    int ret = 0;
    v4l2_mpeg_video_multi_slice_mode slicemode = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_MB;
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE;
    control.value = slicemode;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    }
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB;
    control.value = 400;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    }
    Copy to clipboard

**Intra refresh**

You can configure the VPU driver to enable the Intra refresh mode and intra refresh
                period using the `VIDIOC_S_CTRL ioctl system` call. This
                configuration must be set before the `VIDIOC_STREAMON` on an output
                plane. It is to be noted that the intra refresh period can be modified dynamically
                during the session and this feature is applicable only to an encoder session.

| Control ID for H.264 and HEVC | Accepted format | Range |
| :---: | :---: | :---: |
| V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPECopy to clipboard | enum<br>`<br>                                            v4l2_mpeg_video_intra_refresh_period_type` | `{V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE_RANDOM,<br>                                        V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE_CYCLIC}`<br>Random<br>                                        mode:<br>                                        <br><br>    V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE_RANDOMCopy to clipboard<br><br><br>Cyclic<br>                                        mode:<br>                                        <br><br>    V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE_CYCLICCopy to clipboard |
| V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIODCopy to clipboard | Integer | Set the intra refresh period to be less than an I-frame<br>                                        interval<br><br><br>                                    <br>Unit: number of frames |

The following code sample shows the static configuration to enable the random intra
                refresh for every 10 frames:

v4l2_control control;
    int ret = 0;
    v4l2_mpeg_video_intra_refresh_period_type refreshtype = V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE_RANDOM;
         
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE;
    control.value = refreshtype;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    }
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD;
    control.value = 10;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    }Copy to clipboard

**Rotation**

You can configure the VPU driver to rotate the input frame before encoding using the
                    `VIDIOC_S_CTRL` ioctl system call. This configuration must be set
                before the `VIDIOC_STREAMON` on an output plane. This feature does
                not support dynamic configuration and is applicable only to an encoder session.

| Control ID for H264 and HEVC | Accepted format | Range |
| :---: | :---: | :---: |
| V4L2_CID_ROTATECopy to clipboard | Integer | {90, 180, 270} Unit: degrees |

The following sample code shows the static configuration for rotating an input frame
                to 90 degrees:

v4l2_control control;
    int ret = 0;
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_ROTATE;
    control.value = 90;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    }Copy to clipboard

**Frame QP range**

You can configure the VPU driver to set the minimum and maximum frame QP range
                parameter for I, P, and B frames to the intended values for encoded frames using the
                    `VIDIOC_S_CTRL` ioctl system call. This configuration must be set
                before the `VIDIOC_STREAMON` on an output plane. The values are valid
                for an entire session. Dynamic configuration is not supported, and this feature is
                applicable only to an encoder session.

| Control ID for H.264 codec | Accepted format | Range |
| --- | --- | --- |
| V4L2_CID_MPEG_VIDEO_H264_I_FRAME_MIN_QPCopy to clipboard | Integer | Example according to the codec<br>                                    standard: 1 to 51 for 8‑bit |
| V4L2_CID_MPEG_VIDEO_H264_I_FRAME_MAX_QPCopy to clipboard | Integer | Example according to the codec<br>                                    standard: 1 to 51 for 8‑bit |
| V4L2_CID_MPEG_VIDEO_H264_P_FRAME_MIN_QPCopy to clipboard | Integer | Example according to the codec<br>                                    standard: 1 to 51 for 8‑bit |
| V4L2_CID_MPEG_VIDEO_H264_P_FRAME_MAX_QPCopy to clipboard | Integer | Example according to the codec<br>                                    standard: 1 to 51 for 8‑bit |
| V4L2_CID_MPEG_VIDEO_H264_B_FRAME_MIN_QPCopy to clipboard | Integer | Example according to the codec<br>                                    standard: 1 to 51 for 8‑bit |
| V4L2_CID_MPEG_VIDEO_H264_B_FRAME_MAX_QPCopy to clipboard | Integer | Example according to the codec<br>                                    standard: 1 to 51 for 8‑bit |
| V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MIN_QPCopy to clipboard | Integer | Example according to the codec<br>                                    standard: 1 to 51 for 8‑bit |
| V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MAX_QPCopy to clipboard | Integer | Example according to the codec<br>                                    standard: 1 to 51 for 8‑bit |
| V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_MIN_QPCopy to clipboard | Integer | Example according to the codec<br>                                    standard: 1 to 51 for 8‑bit |
| V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_MAX_QPCopy to clipboard | Integer | Example according to the codec<br>                                    standard: 1 to 51 for 8‑bit |
| V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_MIN_QPCopy to clipboard | Integer | Example according to the codec<br>                                    standard: 1 to 51 for 8‑bit |
| V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_MAX_QPCopy to clipboard | Integer | Example according to the codec<br>                                    standard: 1 to 51 for 8‑bit |
|  |  |  |
|  |  |  |
|  |  |  |
|  |  |  |
|  |  |  |
|  |  |  |
|  |  |  |
|  |  |  |
|  |  |  |
|  |  |  |
|  |  |  |

The following code sample shows the static configuration parameter to choose 40 as
                the maximum QP and 10 as the minimum QP for the I, P, and B-frames in a H.264
                session:

v4l2_control control;
    int ret = 0;
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_H264_I_FRAME_MIN_QP;
    control.value = 10;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    }
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_H264_P_FRAME_MIN_QP;
    control.value = 10;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    }
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_H264_B_FRAME_MIN_QP;
    control.value = 10;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    }
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_H264_I_FRAME_MAX_QP;
    control.value = 40;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    }
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_H264_P_FRAME_MAX_QP;
    control.value = 40;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    }
    memset(&control, 0, sizeof(control));
    control.id = V4L2_CID_MPEG_VIDEO_H264_B_FRAME_MAX_QP;
    control.value = 40;
    ret = ioctl(mDriverFd, VIDIOC_S_CTRL, &control);// mDriverFd returned from open() system call
    if (ret) {
        return -1;
    }
    Copy to clipboard

Alternatively, you can configure the following controls if you wish to have the same
                minimum and maximum QP value for I, P, and B-frames:

| Control ID for H.264 codec | Accepted format | Range |
| --- | --- | --- |
| `V4L2_CID_MPEG_VIDEO_H264_MIN_QP` | Integer | Example according to the code<br>                                    standard: 1 to 51 for 8‑bit |
| `V4L2_CID_MPEG_VIDEO_H264_MAX_QP` | Integer | Example according to the code<br>                                    standard: 1 to 51 for 8‑bit |
|  |  |  |

| Control ID for HEVC codec | Accepted format | Range |
| --- | --- | --- |
| `V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP` | Integer | Example according to the code<br>                                    standard: 1 to 51 for 8‑bit |
| `V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP` | Integer | Example according to the code<br>                                    standard: 1 to 51 for 8‑bit |
|  |  |  |

## Configure GStreamer APIs for different features

For more information on the GStreamer element property for different features and
                respective plugins, see [Qualcomm GST plugins](https://docs.qualcomm.com/bundle/publicresource/topics/80-70014-50/qim-sdk-plugins.html).

Last Published: Jul 12, 2024

[Previous Topic
Verify](https://docs.qualcomm.com/bundle/publicresource/80-70014-20/topics/bring-up.md) [Next Topic
Debug](https://docs.qualcomm.com/bundle/publicresource/80-70014-20/topics/debug.md)