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