1# Video Encoding Configurations for Typical Scenarios 2 3 4This topic provides recommended configuration parameters for AVCodec video encoding in various scenarios. It aims to help you configure video encoders according to your specific needs. 5 6Video encoding is used in many scenarios, including video calls, video meetings, live streaming, video editing, and video sharing. Based on user experience requirements, these scenarios can be grouped into three main categories: low-latency, real-time streaming, and offline encoding. 7 8This topic offers the recommended parameter settings for video encoding in these three categories. You can select the appropriate configurations based on service requirements. 9 10 11## General Development Steps 12 13**Linking Dynamic Libraries in the CMake Script** 14 15```cmake 16target_link_libraries(sample PUBLIC libnative_media_codecbase.so) 17target_link_libraries(sample PUBLIC libnative_media_core.so) 18target_link_libraries(sample PUBLIC libnative_media_venc.so) 19``` 20 21> **NOTE** 22> 23> The word **sample** in the preceding code snippet is only an example. Use the actual project directory name. 24> 25 26**Adding Header Files** 27 28```c++ 29#include <multimedia/player_framework/native_avcodec_videoencoder.h> 30#include <multimedia/player_framework/native_avcapability.h> 31#include <multimedia/player_framework/native_avcodec_base.h> 32#include <multimedia/player_framework/native_avformat.h> 33#include <fstream> 34``` 35 36## Low-Latency Encoding Scenarios 37 38Low-latency encoding scenarios include video calls, video meetings, and interactive live streaming applications that require low end-to-end latency. 39 40**How to Develop** 41 42This section describes only the steps involved in the encoder configuration phase. You can learn the basic encoding process in [Video Encoding](video-encoding.md). 43 441. Set encoder parameters. 45 46 Configure parameters suitable for low-latency encoding scenarios. 47 48 In low-latency encoding scenarios, the recommended encoding parameters for typical resolution (using H.265 as an example) are as follows. 49 50 | Resolution | Frame Rate (fps)| Bit Rate (kbit/s)| Key Frame Interval (ms)| Bit Rate Control Mode| 51 | ------------------| -------- | -------- | ------ | ------ | 52 | 1920x1080 | 30 | 1500 | -1 | CBR | 53 | 1280x720 | 30 | 1000 | -1 | CBR | 54 | 960x540 | 30 | 700 | -1 | CBR | 55 | 640x360 | 30 | 550 | -1 | CBR | 56 | 320x180 | 20 | 200 | -1 | CBR | 57 58 In the code snippet below, the following variables are used: 59 - **videoEnc**: pointer to a video encoder instance. For details, see step 2 in [Video Encoding in Surface Mode](video-encoding.md#surface-mode). 60 61 ```c++ 62 // 1.1 Create an AVFormat parameter instance. 63 OH_AVFormat *format = OH_AVFormat_Create(); 64 65 // 1.2 Fill in the encoding parameter key-value pairs (using the 1080p@30 fps SDR input source as an example). 66 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, 1920); // (Mandatory) Video width. 67 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, 1080); // (Mandatory) Video height. 68 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12); // (Mandatory) Format of the video source data. 69 OH_AVFormat_SetIntValue(format, OH_MD_KEY_RANGE_FLAG, 0); // VUI information. The value 0 means a limited range, and 1 means a full range. 70 OH_AVFormat_SetIntValue(format, OH_MD_KEY_COLOR_PRIMARIES, OH_ColorPrimary::COLOR_PRIMARY_BT709); // VUI information, color gamut of the video source. 71 OH_AVFormat_SetIntValue(format, OH_MD_KEY_TRANSFER_CHARACTERISTICS, OH_TransferCharacteristic::TRANSFER_CHARACTERISTIC_BT709); // VUI information, OETF/EOTF curve. 72 OH_AVFormat_SetIntValue(format, OH_MD_KEY_MATRIX_COEFFICIENTS, OH_MatrixCoefficient:: MATRIX_COEFFICIENT_BT709); // VUI information, YUV and RGB conversion matrix. 73 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, OH_HEVCProfile::HEVC_PROFILE_MAIN); // Video encoder profile. 74 OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, 30.0); // (Mandatory) Video frame rate. 75 OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, -1); // (Mandatory) Key frame interval. 76 OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, OH_BitrateMode::BITRATE_MODE_CBR); // (Mandatory) Set the bit rate control mode to CBR. 77 OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, 1500000); // (Mandatory). Bit rate, in bit/s. 78 79 // 1.3 Set the encoding parameters of the video encoder. 80 int32_t ret = OH_VideoEncoder_Configure(videoEnc, format); 81 if (ret != AV_ERR_OK) { 82 // Handle exceptions. 83 } 84 // 1.4 Destroy the AVFormat instance after the configuration is complete. 85 OH_AVFormat_Destroy(format); 86 ``` 87 88 > **NOTE** 89 > 90 > A key frame interval of -1 indicates that only the first frame is a key frame. You can dynamically configure encoder parameters during running based on transmission conditions and image quality to insert new key frames (IDR). 91 922. (Optional) Dynamically configure encoder parameters during running. 93 94 For details, see step 9 in [Video Encoding in Surface Mode](video-encoding.md#surface-mode). 95 96 ```c++ 97 // 2.1 Create an AVFormat parameter instance. 98 OH_AVFormat *format = OH_AVFormat_Create(); 99 // 2.2 Fill in the encoding parameter key-value pairs (dynamically requesting IDR frames). 100 OH_AVFormat_SetIntValue(format, OH_MD_KEY_REQUEST_I_FRAME, true); 101 // 2.3 Make the encoder parameters take effect. 102 ret = OH_VideoEncoder_SetParameter(videoEnc, format); 103 if (ret != AV_ERR_OK) { 104 // Handle exceptions. 105 } 106 // 2.4 Destroy the AVFormat instance after the configuration is complete. 107 OH_AVFormat_Destroy(format); 108 ``` 109 To adapt to network fluctuations, you are advised to use the [temporally scalable video encoding](video-encoding-temporal-scalability.md) configuration. 110 111## Real-Time Streaming Encoding Scenarios 112 113Real-time streaming encoding is used in scenarios like entertainment live streaming and gaming live streaming, where the latency requirements for video are relatively low. 114 115**How to Develop** 116 117This section describes how to configure encoder parameters for real-time streaming scenarios in the encoder configuration phase. You can learn the basic encoding process in [Video Encoding](video-encoding.md). 118 119In entertainment live streaming scenarios, the recommended encoding parameters for typical resolution (using H.265 as an example) are as follows. 120 121| Resolution | Frame Rate (fps)| Bit Rate (kbit/s)| Key Frame Interval (ms)| Bit Rate Control Mode| 122| ------------------| -------- | -------- | ------ | ------ | 123| 1920x1080 | 25 | 3000 | 2000 | VBR | 124| 1080x720 | 25 | 1500 | 2000 | VBR | 125| 960x544 | 25 | 1000 | 2000 | VBR | 126| 864x480 | 25 | 800 | 2000 | VBR | 127 128In gaming live streaming scenarios, the recommended encoding parameters for typical resolution (using H.265 as an example) are as follows. 129 130| Resolution | Frame Rate (fps)| Bit Rate (kbit/s)| Key Frame Interval (ms)| Bit Rate Control Mode| 131| ------------------| -------- | -------- | ------ | ------ | 132| 1920x1080 | 60 | 6000 | 5000 | VBR | 133 134```c++ 135// 1. Create an AVFormat parameter instance. 136OH_AVFormat *format = OH_AVFormat_Create(); 137// 2. Fill in the encoding parameter key-value pair (using the 1080p@25 fps SDR input source as an example). 138OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, 1080); // (Mandatory) Video width. 139OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, 1920); // (Mandatory) Video height. 140OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12); // (Mandatory) Format of the video source data. 141OH_AVFormat_SetIntValue(format, OH_MD_KEY_RANGE_FLAG, 0); // VUI information. The value 0 means a limited range, and 1 means a full range. 142OH_AVFormat_SetIntValue(format, OH_MD_KEY_COLOR_PRIMARIES, OH_ColorPrimary::COLOR_PRIMARY_BT709); // VUI information, color gamut of the video source. 143OH_AVFormat_SetIntValue(format, OH_MD_KEY_TRANSFER_CHARACTERISTICS, OH_TransferCharacteristic::TRANSFER_CHARACTERISTIC_BT709); // VUI information, OETF/EOTF curve. 144OH_AVFormat_SetIntValue(format, OH_MD_KEY_MATRIX_COEFFICIENTS, OH_MatrixCoefficient:: MATRIX_COEFFICIENT_BT709); // VUI information, YUV and RGB conversion matrix. 145OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, OH_HEVCProfile::HEVC_PROFILE_MAIN); // Video encoder profile. 146OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, 25.0); // (Mandatory) Video frame rate. 147OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, 2000); // (Mandatory) Key frame interval, in ms. 148OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, OH_BitrateMode::BITRATE_MODE_VBR); // (Mandatory) Set the bit rate control mode to VBR. 149OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, 3000000); // (Mandatory). Bit rate, in bit/s. 150// 3. Set the encoding parameters of the video encoder. 151int32_t ret = OH_VideoEncoder_Configure(videoEnc, format); 152if (ret != AV_ERR_OK) { 153 // Handle exceptions. 154} 155// 4. Destroy the AVFormat instance after the configuration is complete. 156OH_AVFormat_Destroy(format); 157``` 158 159 160## Offline Encoding Scenarios 161 162Offline encoding is used in scenarios such as video editing and video sharing. 163 164 165**How to Develop** 166 167This section describes how to configure encoder parameters for offline editing scenarios in the encoder configuration phase. You can learn the basic encoding process in [Video Encoding](video-encoding.md). 168 169In video editing scenarios, the recommended encoding parameters for typical resolution (using H.265 as an example) are as follows. 170 171| Resolution | Frame Rate (fps)| Bit Rate (kbit/s)| Key Frame Interval (ms)| Bit Rate Control Mode| 172| ------------------| -------- | -------- | ------ | ------ | 173| 3840x2160 | 30 | 25000 | 5000 | VBR | 174| 2560x1440 | 30 | 15000 | 5000 | VBR | 175| 1920x1080 | 30 | 10000 | 5000 | VBR | 176| 1280x720 | 30 | 5000 | 5000 | VBR | 177| 854x480 | 30 | 2000 | 5000 | VBR | 178 179In video sharing scenarios, the recommended encoding parameters for typical resolution (using H.265 as an example) are as follows. 180 181| Resolution | Frame Rate (fps)| Bit Rate (kbit/s)| Key Frame Interval (ms)| Bit Rate Control Mode| 182| ------------------| -------- | -------- | ------ | ------ | 183| 3840x2160 | 30 | 5600 | 5000 | VBR | 184| 2560x1440 | 30 | 4900 | 5000 | VBR | 185| 1920x1080 | 30 | 2100 | 5000 | VBR | 186| 1280x720 | 30 | 1400 | 5000 | VBR | 187| 854x480 | 30 | 400 | 5000 | VBR | 188 189```c++ 190// 1. Create an AVFormat parameter instance. 191OH_AVFormat *format = OH_AVFormat_Create(); 192// 2. Fill in the encoding parameter key-value pair (using the 1080p@30 fps SDR input source as an example). 193OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, 1920); // (Mandatory) Video width. 194OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, 1080); // (Mandatory) Video height. 195OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12); // (Mandatory) Format of the video source data. 196OH_AVFormat_SetIntValue(format, OH_MD_KEY_RANGE_FLAG, 0); // VUI information. The value 0 means a limited range, and 1 means a full range. 197OH_AVFormat_SetIntValue(format, OH_MD_KEY_COLOR_PRIMARIES, OH_ColorPrimary::COLOR_PRIMARY_BT709); // VUI information, color gamut of the video source. 198OH_AVFormat_SetIntValue(format, OH_MD_KEY_TRANSFER_CHARACTERISTICS, OH_TransferCharacteristic::TRANSFER_CHARACTERISTIC_BT709); // VUI information, OETF/EOTF curve. 199OH_AVFormat_SetIntValue(format, OH_MD_KEY_MATRIX_COEFFICIENTS, OH_MatrixCoefficient:: MATRIX_COEFFICIENT_BT709); // YUV and RGB conversion matrix. 200OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, OH_HEVCProfile::HEVC_PROFILE_MAIN); // Video encoder profile. 201OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, 30.0); // (Mandatory) Video frame rate. 202OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, 5000); // (Mandatory) Key frame interval, in ms. 203OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, OH_BitrateMode::BITRATE_MODE_VBR); // (Mandatory) Set the bit rate control mode to VBR. 204OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, 2100000); // (Mandatory). Bit rate, in bit/s. 205// 3. Set the encoding parameters of the video encoder. 206int32_t ret = OH_VideoEncoder_Configure(videoEnc, format); 207if (ret != AV_ERR_OK) { 208 // Handle exceptions. 209} 210// 4. Destroy the AVFormat instance after the configuration is complete. 211OH_AVFormat_Destroy(format); 212``` 213