1e41f4b71Sopenharmony_ci# Obtaining Supported Codecs 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe codecs and their capabilities that you can use for your application on a device vary according to the codec source, codec protocol, and codec capabilities deployed on that device. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciTo ensure that the encoding and decoding behavior meets your expectations, first query the audio and video codecs supported by the system and their capability parameters through a series of APIs. Then find the codecs that are suitable for the development scenario, and correctly configure the codec parameters. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## General Development 8e41f4b71Sopenharmony_ci1. Link the dynamic libraries in the CMake script. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci ``` cmake 11e41f4b71Sopenharmony_ci target_link_libraries(sample PUBLIC libnative_media_codecbase.so) 12e41f4b71Sopenharmony_ci target_link_libraries(sample PUBLIC libnative_media_core.so) 13e41f4b71Sopenharmony_ci target_link_libraries(sample PUBLIC libnative_media_venc.so) 14e41f4b71Sopenharmony_ci target_link_libraries(sample PUBLIC libnative_media_vdec.so) 15e41f4b71Sopenharmony_ci ``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci > **NOTE** 18e41f4b71Sopenharmony_ci > 19e41f4b71Sopenharmony_ci > The word **sample** in the preceding code snippet is only an example. Use the actual project directory name. 20e41f4b71Sopenharmony_ci > 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci2. Add the header files. 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci ```c++ 25e41f4b71Sopenharmony_ci #include <multimedia/player_framework/native_avcapability.h> 26e41f4b71Sopenharmony_ci #include <multimedia/player_framework/native_avcodec_base.h> 27e41f4b71Sopenharmony_ci #include <multimedia/player_framework/native_avformat.h> 28e41f4b71Sopenharmony_ci #include <multimedia/player_framework/native_avcodec_videoencoder.h> 29e41f4b71Sopenharmony_ci #include <multimedia/player_framework/native_avcodec_videodecoder.h> 30e41f4b71Sopenharmony_ci ``` 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci3. Obtain the audio/video codec capability instance. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci You can use either of the following methods to obtain the instance: 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci Method 1: Call **OH_AVCodec_GetCapability** to obtain the codec capability instance recommended by the system. The recommendation policy is the same as that of the **OH_XXX_CreateByMime** series APIs. 37e41f4b71Sopenharmony_ci ```c++ 38e41f4b71Sopenharmony_ci // Obtain the AAC decoder capability instance recommended by the system. 39e41f4b71Sopenharmony_ci OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false); 40e41f4b71Sopenharmony_ci ``` 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci Method 2: Call **OH_AVCodec_GetCapabilityByCategory** to obtain the codec capability instance of the specified software or hardware. 43e41f4b71Sopenharmony_ci ```c++ 44e41f4b71Sopenharmony_ci // Obtain the AVC encoder capability instance of the specified hardware. 45e41f4b71Sopenharmony_ci OH_AVCapability *capability = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE); 46e41f4b71Sopenharmony_ci ``` 47e41f4b71Sopenharmony_ci The system automatically recycles the instance when it is no longer needed. 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci4. Call the query APIs as required. For details, see the [API Reference](../../reference/apis-avcodec-kit/_a_v_capability.md). 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci## Scenario-specific Development 52e41f4b71Sopenharmony_ciThis section describes how to use the capability query APIs in specific scenarios. 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci### Creating a Codec with the Specified Name 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ciIf multiple encoders or decoders with the same MIME type exist, using the **OH_XXX_CreateByMime** series APIs creates only codecs recommended by the system. To create a non-recommended codec, you must first obtain the codec name and then call **OH_XXX_CreateByName** series APIs to create a codec with the given name. 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci| API | Description | 59e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 60e41f4b71Sopenharmony_ci| OH_AVCapability_GetName | Obtains the name of a codec corresponding to a capability instance.| 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ciThe code snippet below creates an H.264 software decoder when there are an H.264 software decoder and H.264 hardware decoder: 63e41f4b71Sopenharmony_ci```c++ 64e41f4b71Sopenharmony_ci// 1. Obtain an H.264 software decoder capability instance. 65e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, false, SOFTWARE); 66e41f4b71Sopenharmony_ciif (capability != nullptr) { 67e41f4b71Sopenharmony_ci // 2. Obtain the name of the H.264 software decoder. 68e41f4b71Sopenharmony_ci const char *codecName = OH_AVCapability_GetName(capability); 69e41f4b71Sopenharmony_ci // 3. Create an H.264 software decoder instance. 70e41f4b71Sopenharmony_ci OH_AVCodec *videoDec = OH_VideoDecoder_CreateByName(codecName); 71e41f4b71Sopenharmony_ci} 72e41f4b71Sopenharmony_ci``` 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci### Setting Codec Parameters by Software or Hardware 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ciA software codec and hardware codec are defined as follows: 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci* A software codec performs encoding and decoding on the CPU. Its capabilities can be flexibly iterated. It provides better compatibility and better protocol and specification extension capabilities over a hardware codec. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci* A hardware codec performs encoding and decoding on dedicated hardware. It has been hardened on the hardware platform and its capabilities are iterated with the hardware platform. Compared with a software codec, a hardware codec has better power consumption, time consumption, and throughput performance, as well as lower CPU load. 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ciA hardware codec is preferred as long as it meets your project requirements. You can configure codec parameters based on the software or hardware type. 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci| API | Description | 85e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 86e41f4b71Sopenharmony_ci| OH_AVCapability_IsHardware | Checks whether a codec capability instance describes a hardware codec.| 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ciThe code snippet below shows the differentiated frame rate configuration for software and hardware video encoders. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci```c++ 91e41f4b71Sopenharmony_ci// 1. Check whether the recommended H.264 encoder is a hardware codec. 92e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true); 93e41f4b71Sopenharmony_cibool isHardward = OH_AVCapability_IsHardware(capability); 94e41f4b71Sopenharmony_ci// 2. Carry out differentiated configuration based on the software or hardware type. 95e41f4b71Sopenharmony_ciOH_AVCodec *videoEnc = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC); 96e41f4b71Sopenharmony_ciOH_AVFormat *format = OH_AVFormat_CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, 1920, 1080); 97e41f4b71Sopenharmony_cidouble frameRate = isHardward ? 60.0 : 30.0; 98e41f4b71Sopenharmony_ciif (!OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, frameRate)) { 99e41f4b71Sopenharmony_ci // Exception handling. 100e41f4b71Sopenharmony_ci} 101e41f4b71Sopenharmony_ciif (OH_VideoEncoder_Configure(videoEnc, format) != AV_ERR_OK) { 102e41f4b71Sopenharmony_ci // Exception handling. 103e41f4b71Sopenharmony_ci} 104e41f4b71Sopenharmony_ciOH_AVFormat_Destroy(format); 105e41f4b71Sopenharmony_ci``` 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci### Creating a Multi-Channel Codec 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ciMultiple codecs are required in certain scenarios. However, the number of codec instances that can be created is limited due to the restrictions of system resources. 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci| API | Description | 112e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 113e41f4b71Sopenharmony_ci| OH_AVCapability_GetMaxSupportedInstances | Obtains the maximum number of codec instances that can be concurrently run corresponding to a capability instance. The actual number of codec instances that can be created is restricted by other system resources.| 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ciCreate hardware decoder instances first. If the hardware decoder instances cannot fully meet the project requirements, create software decoder instances. The following is an example: 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci```c++ 118e41f4b71Sopenharmony_ciconstexpr int32_t NEEDED_VDEC_NUM = 8; 119e41f4b71Sopenharmony_ci// 1. Create hardware decoder instances. 120e41f4b71Sopenharmony_ciOH_AVCapability *capHW = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, false, HARDWARE); 121e41f4b71Sopenharmony_ciint32_t vDecNumHW = min(OH_AVCapability_GetMaxSupportedInstances(capHW), NEEDED_VDEC_NUM); 122e41f4b71Sopenharmony_ciint32_t createdVDecNum = 0; 123e41f4b71Sopenharmony_cifor (int i = 0; i < vDecNumHW; i++) { 124e41f4b71Sopenharmony_ci OH_AVCodec *videoDec = OH_VideoDecoder_CreateByName(OH_AVCapability_GetName(capHW)); 125e41f4b71Sopenharmony_ci if (videoDec != nullptr) { 126e41f4b71Sopenharmony_ci // Maintained in videoDecVector. 127e41f4b71Sopenharmony_ci createdVDecNum++; 128e41f4b71Sopenharmony_ci } 129e41f4b71Sopenharmony_ci} 130e41f4b71Sopenharmony_ciif (createdVDecNum < NEEDED_VDEC_NUM) { 131e41f4b71Sopenharmony_ci // 2. If the hardware decoder instances cannot fully meet the project requirements, create software decoder instances. 132e41f4b71Sopenharmony_ci OH_AVCapability *capSW = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, false, SOFTWARE); 133e41f4b71Sopenharmony_ci int32_t vDecNumSW = min(OH_AVCapability_GetMaxSupportedInstances(capSW), NEEDED_VDEC_NUM - createdVDecNum); 134e41f4b71Sopenharmony_ci for (int i = 0; i < vDecNumSW; i++) { 135e41f4b71Sopenharmony_ci OH_AVCodec *videoDec = OH_VideoDecoder_CreateByName(OH_AVCapability_GetName(capSW)); 136e41f4b71Sopenharmony_ci if (videoDec != nullptr) { 137e41f4b71Sopenharmony_ci // Maintained in videoDecVector. 138e41f4b71Sopenharmony_ci createdVDecNum++; 139e41f4b71Sopenharmony_ci } 140e41f4b71Sopenharmony_ci } 141e41f4b71Sopenharmony_ci} 142e41f4b71Sopenharmony_ci``` 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci### Controlling the Encoding Quality 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ciThree bit rate modes are available: Constant Bit Rate (CBR), Dynamic Bit Rate (VBR), and Constant Quality (CQ). For CBR and VBR, the encoding quality is determined by the bit rate parameters. For CQ, the encoding quality is determined by the quality parameters. 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci| API | Description | 149e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 150e41f4b71Sopenharmony_ci| OH_AVCapability_IsEncoderBitrateModeSupported | Checks whether a codec supports the specified bit rate mode.| 151e41f4b71Sopenharmony_ci| OH_AVCapability_GetEncoderBitrateRange | Obtains the bit rate range supported by a codec. This API can be used in CBR or VBR mode.| 152e41f4b71Sopenharmony_ci| OH_AVCapability_GetEncoderQualityRange | Obtains the quality range supported by a codec. This API can be used in CQ mode. | 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ciThe code snippet below shows the configuration in CBR or VBR mode. 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci```c++ 157e41f4b71Sopenharmony_ciOH_BitrateMode bitrateMode = BITRATE_MODE_CBR; 158e41f4b71Sopenharmony_ciint32_t bitrate = 3000000; 159e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true); 160e41f4b71Sopenharmony_ciif (capability == nullptr) { 161e41f4b71Sopenharmony_ci // Exception handling. 162e41f4b71Sopenharmony_ci} 163e41f4b71Sopenharmony_ci// 1. Check whether a bit rate mode is supported. 164e41f4b71Sopenharmony_cibool isSupported = OH_AVCapability_IsEncoderBitrateModeSupported(capability, bitrateMode); 165e41f4b71Sopenharmony_ciif (!isSupported) { 166e41f4b71Sopenharmony_ci // Exception handling. 167e41f4b71Sopenharmony_ci} 168e41f4b71Sopenharmony_ci// 2. Obtain the bit rate range and check whether the bit rate to be configured is within the range. 169e41f4b71Sopenharmony_ciOH_AVRange bitrateRange = {-1, -1}; 170e41f4b71Sopenharmony_ciint32_t ret = OH_AVCapability_GetEncoderBitrateRange(capability, &bitrateRange); 171e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || bitrateRange.maxVal <= 0) { 172e41f4b71Sopenharmony_ci // Exception handling. 173e41f4b71Sopenharmony_ci} 174e41f4b71Sopenharmony_ciif (bitrate > bitrateRange.maxVal || bitrate < bitrateRange.minVal) { 175e41f4b71Sopenharmony_ci // 3. (Optional) Adjust the bit rate parameters to be configured. 176e41f4b71Sopenharmony_ci} 177e41f4b71Sopenharmony_ci// 4. Set the encoding parameters. 178e41f4b71Sopenharmony_ciOH_AVCodec *videoEnc = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC); 179e41f4b71Sopenharmony_ciOH_AVFormat *format = OH_AVFormat_CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, 1920, 1080); 180e41f4b71Sopenharmony_ciif (OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, bitrateMode) && 181e41f4b71Sopenharmony_ci OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, static_cast<int64_t>(bitrate)) == false) { 182e41f4b71Sopenharmony_ci // Exception handling. 183e41f4b71Sopenharmony_ci} 184e41f4b71Sopenharmony_ciif (OH_VideoEncoder_Configure(videoEnc, format) != AV_ERR_OK) { 185e41f4b71Sopenharmony_ci // Exception handling. 186e41f4b71Sopenharmony_ci} 187e41f4b71Sopenharmony_ciOH_AVFormat_Destroy(format); 188e41f4b71Sopenharmony_ci``` 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ciThe code snippet below shows the configuration in CQ mode. 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci```c++ 193e41f4b71Sopenharmony_ciOH_BitrateMode bitrateMode = BITRATE_MODE_CQ; 194e41f4b71Sopenharmony_ciint32_t quality = 0; 195e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true); 196e41f4b71Sopenharmony_ciif (capability == nullptr) { 197e41f4b71Sopenharmony_ci // Exception handling. 198e41f4b71Sopenharmony_ci} 199e41f4b71Sopenharmony_ci// 1. Check whether a bit rate mode is supported. 200e41f4b71Sopenharmony_cibool isSupported = OH_AVCapability_IsEncoderBitrateModeSupported(capability, bitrateMode); 201e41f4b71Sopenharmony_ciif (!isSupported) { 202e41f4b71Sopenharmony_ci // Exception handling. 203e41f4b71Sopenharmony_ci} 204e41f4b71Sopenharmony_ci// 2. Obtain the quality range and determine whether the quality parameters to be configured are within the range. 205e41f4b71Sopenharmony_ciOH_AVRange qualityRange = {-1, -1}; 206e41f4b71Sopenharmony_ciint32_t ret = OH_AVCapability_GetEncoderQualityRange(capability, &qualityRange); 207e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || qualityRange.maxVal < 0) { 208e41f4b71Sopenharmony_ci // Exception handling. 209e41f4b71Sopenharmony_ci} 210e41f4b71Sopenharmony_ciif (quality > qualityRange.maxVal || quality < qualityRange.minVal) { 211e41f4b71Sopenharmony_ci // 3. (Optional) Adjust the quality parameters to be configured. 212e41f4b71Sopenharmony_ci} 213e41f4b71Sopenharmony_ci// 5. Set the encoding parameters. 214e41f4b71Sopenharmony_ciOH_AVCodec *videoEnc = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC); 215e41f4b71Sopenharmony_ciOH_AVFormat *format = OH_AVFormat_CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, 1920, 1080); 216e41f4b71Sopenharmony_ciif (OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, bitrateMode) && 217e41f4b71Sopenharmony_ci OH_AVFormat_SetIntValue(format, OH_MD_KEY_QUALITY, quality) == false) { 218e41f4b71Sopenharmony_ci // Exception handling. 219e41f4b71Sopenharmony_ci} 220e41f4b71Sopenharmony_ciif (OH_VideoEncoder_Configure(videoEnc, format) != AV_ERR_OK) { 221e41f4b71Sopenharmony_ci // Exception handling. 222e41f4b71Sopenharmony_ci} 223e41f4b71Sopenharmony_ciOH_AVFormat_Destroy(format); 224e41f4b71Sopenharmony_ci``` 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci### Checking the Complexity Range Supported 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ciThe complexity range determines the number of tools used by the codec. It is supported only by some codecs. 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci| API | Description | 231e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 232e41f4b71Sopenharmony_ci| OH_AVCapability_GetEncoderComplexityRange | Obtains the complexity range supported by a codec.| 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci```c++ 235e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true); 236e41f4b71Sopenharmony_ciif (capability == nullptr) { 237e41f4b71Sopenharmony_ci // Exception handling. 238e41f4b71Sopenharmony_ci} 239e41f4b71Sopenharmony_ci// Check the supported encoding complexity range. 240e41f4b71Sopenharmony_ciOH_AVRange complexityRange = {-1, -1}; 241e41f4b71Sopenharmony_ciint32_t ret = OH_AVCapability_GetEncoderComplexityRange(capability, &complexityRange); 242e41f4b71Sopenharmony_ci``` 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci### Setting the Correct Audio Codec Parameters 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ciIn audio encoding or decoding scenarios, you need to query and set parameters such as the sampling rate, number of channels, and bit rate (required only for audio encoding). 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci| API | Description | 249e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 250e41f4b71Sopenharmony_ci| OH_AVCapability_GetAudioSupportedSampleRates | Obtains the sample rates supported by an audio codec.| 251e41f4b71Sopenharmony_ci| OH_AVCapability_GetAudioChannelCountRange | Obtains the count range of channels supported by an audio codec.| 252e41f4b71Sopenharmony_ci| OH_AVCapability_GetEncoderBitrateRange | Obtains the bit rate range supported by an encoder.| 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ciThe code snippet below shows how to correctly set the encoding parameters in the audio encoding scenario. 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci```c++ 257e41f4b71Sopenharmony_ciint32_t sampleRate = 44100; 258e41f4b71Sopenharmony_ciint32_t channelCount = 2; 259e41f4b71Sopenharmony_ciint32_t bitrate = 261000; 260e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true); 261e41f4b71Sopenharmony_ciif (capability == nullptr) { 262e41f4b71Sopenharmony_ci // Exception handling. 263e41f4b71Sopenharmony_ci} 264e41f4b71Sopenharmony_ci// 1. Check whether the sample rate to be configured is supported. 265e41f4b71Sopenharmony_ciconst int32_t *sampleRates = nullptr; 266e41f4b71Sopenharmony_ciuint32_t sampleRateNum = -1; 267e41f4b71Sopenharmony_ciint32_t ret = OH_AVCapability_GetAudioSupportedSampleRates(capability, &sampleRates, &sampleRateNum); 268e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || sampleRates == nullptr || sampleRateNum <= 0) { 269e41f4b71Sopenharmony_ci // Exception handling. 270e41f4b71Sopenharmony_ci} 271e41f4b71Sopenharmony_cibool isMatched = false; 272e41f4b71Sopenharmony_cifor (int i = 0; i < sampleRateNum; i++) { 273e41f4b71Sopenharmony_ci if (sampleRates[i] == sampleRate) { 274e41f4b71Sopenharmony_ci isMatched = true; 275e41f4b71Sopenharmony_ci } 276e41f4b71Sopenharmony_ci} 277e41f4b71Sopenharmony_ciif (!isMatched) { 278e41f4b71Sopenharmony_ci // 2. (Optional) Adjust the sample rate to be configured. 279e41f4b71Sopenharmony_ci} 280e41f4b71Sopenharmony_ci// 3. Obtain the count range of channels and check whether the number of channels to be configured is within the range. 281e41f4b71Sopenharmony_ciOH_AVRange channelRange = {-1, -1}; 282e41f4b71Sopenharmony_ciret = OH_AVCapability_GetAudioChannelCountRange(capability, &channelRange); 283e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || channelRange.maxVal <= 0) { 284e41f4b71Sopenharmony_ci // Exception handling. 285e41f4b71Sopenharmony_ci} 286e41f4b71Sopenharmony_ciif (channelCount > channelRange.maxVal || channelCount < channelRange.minVal ) { 287e41f4b71Sopenharmony_ci //4. (Optional) Adjust the number of channels to be configured. 288e41f4b71Sopenharmony_ci} 289e41f4b71Sopenharmony_ci// 5. Obtain the bit rate range and check whether the bit rate to be configured is within the range. 290e41f4b71Sopenharmony_ciOH_AVRange bitrateRange = {-1, -1}; 291e41f4b71Sopenharmony_ciret = OH_AVCapability_GetEncoderBitrateRange(capability, &bitrateRange); 292e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || bitrateRange.maxVal <= 0) { 293e41f4b71Sopenharmony_ci // Exception handling. 294e41f4b71Sopenharmony_ci} 295e41f4b71Sopenharmony_ciif (bitrate > bitrateRange.maxVal || bitrate < bitrateRange.minVal ) { 296e41f4b71Sopenharmony_ci //7. (Optional) Adjust the bit rate to be configured. 297e41f4b71Sopenharmony_ci} 298e41f4b71Sopenharmony_ci// 8. Set the encoding parameters. 299e41f4b71Sopenharmony_ciOH_AVCodec *audioEnc = OH_AudioEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC); 300e41f4b71Sopenharmony_ciOH_AVFormat *format = OH_AVFormat_Create(); 301e41f4b71Sopenharmony_ciif (OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, sampleRate) && 302e41f4b71Sopenharmony_ci OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, channelCount) && 303e41f4b71Sopenharmony_ci OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, static_cast<int64_t>(bitrate)) == false) { 304e41f4b71Sopenharmony_ci // Exception handling. 305e41f4b71Sopenharmony_ci} 306e41f4b71Sopenharmony_ciif (OH_AudioEncoder_Configure(audioEnc, format) != AV_ERR_OK) { 307e41f4b71Sopenharmony_ci // Exception handling. 308e41f4b71Sopenharmony_ci} 309e41f4b71Sopenharmony_ciOH_AVFormat_Destroy(format); 310e41f4b71Sopenharmony_ci``` 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci### Checking the Codec Profile and Level Supported 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ciThe codec standard provides lots of encoding tools to deal with various encoding scenarios. However, not all tools are required in a specific scenario. Therefore, the standard uses the codec profile to specify the enabled status of these encoding tools. For example, for H.264, there are baseline, main, and high profiles. For details, see **OH_AVCProfile**. 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ciA codec level is a division of the processing capability and storage space required by a codec. For example, for H.264, there are 20 levels ranging from 1 to 6.2. For details, see **OH_AVCLevel**. 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ci| API | Description | 319e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 320e41f4b71Sopenharmony_ci| OH_AVCapability_GetSupportedProfiles | Obtains the profiles supported by a codec.| 321e41f4b71Sopenharmony_ci| OH_AVCapability_GetSupportedLevelsForProfile | Obtains the codec levels supported by a profile.| 322e41f4b71Sopenharmony_ci| OH_AVCapability_AreProfileAndLevelSupported | Checks whether a codec supports the combination of a profile and level.| 323e41f4b71Sopenharmony_ci 324e41f4b71Sopenharmony_ciThe code snippet below checks whether a profile is supported and obtains the supported levels. 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci```c++ 327e41f4b71Sopenharmony_ciOH_AVCProfile profile = AVC_PROFILE_MAIN; 328e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true); 329e41f4b71Sopenharmony_ciif (capability == nullptr) { 330e41f4b71Sopenharmony_ci // Exception handling. 331e41f4b71Sopenharmony_ci} 332e41f4b71Sopenharmony_ci// 1. Check whether the profile to be configured is supported. 333e41f4b71Sopenharmony_ciconst int32_t *profiles = nullptr; 334e41f4b71Sopenharmony_ciuint32_t profileNum = -1; 335e41f4b71Sopenharmony_ciint32_t ret = OH_AVCapability_GetSupportedProfiles(capability, &profiles, &profileNum); 336e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || profiles == nullptr || profileNum <= 0) { 337e41f4b71Sopenharmony_ci // Exception handling. 338e41f4b71Sopenharmony_ci} 339e41f4b71Sopenharmony_cibool isMatched = false; 340e41f4b71Sopenharmony_cifor (int i = 0; i < profileNum; i++) { 341e41f4b71Sopenharmony_ci if (profiles[i] == profile) { 342e41f4b71Sopenharmony_ci isMatched = true; 343e41f4b71Sopenharmony_ci } 344e41f4b71Sopenharmony_ci} 345e41f4b71Sopenharmony_ci// 2. Obtain the codec levels supported by the profile. 346e41f4b71Sopenharmony_ciconst int32_t *levels = nullptr; 347e41f4b71Sopenharmony_ciuint32_t levelNum = -1; 348e41f4b71Sopenharmony_ciret = OH_AVCapability_GetSupportedLevelsForProfile(capability, profile, &levels, &levelNum); 349e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || levels == nullptr || levelNum <= 0) { 350e41f4b71Sopenharmony_ci // Exception handling. 351e41f4b71Sopenharmony_ci} 352e41f4b71Sopenharmony_ciOH_AVCLevel maxLevel = static_cast<OH_AVCLevel>(levels[levelNum -1]); 353e41f4b71Sopenharmony_ci// 3. (Optional) Use different service logic based on the maximum level supported. 354e41f4b71Sopenharmony_ciswitch (maxLevel) { 355e41f4b71Sopenharmony_ci case AVC_LEVEL_31: 356e41f4b71Sopenharmony_ci // ... 357e41f4b71Sopenharmony_ci break; 358e41f4b71Sopenharmony_ci case AVC_LEVEL_51: 359e41f4b71Sopenharmony_ci // ... 360e41f4b71Sopenharmony_ci break; 361e41f4b71Sopenharmony_ci default: 362e41f4b71Sopenharmony_ci // ... 363e41f4b71Sopenharmony_ci} 364e41f4b71Sopenharmony_ci// 4. Set the profile parameters. 365e41f4b71Sopenharmony_ciOH_AVCodec *videoEnc = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC); 366e41f4b71Sopenharmony_ciOH_AVFormat *format = OH_AVFormat_CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, 1920, 1080); 367e41f4b71Sopenharmony_ciif (!OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, profile)) { 368e41f4b71Sopenharmony_ci // Exception handling. 369e41f4b71Sopenharmony_ci} 370e41f4b71Sopenharmony_ciif (OH_VideoEncoder_Configure(videoEnc, format) != AV_ERR_OK) { 371e41f4b71Sopenharmony_ci // Exception handling. 372e41f4b71Sopenharmony_ci} 373e41f4b71Sopenharmony_ciOH_AVFormat_Destroy(format); 374e41f4b71Sopenharmony_ci``` 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ciIf you already know the required profile and level combination, use the code snippet below to check whether the combination is supported. 377e41f4b71Sopenharmony_ci 378e41f4b71Sopenharmony_ci```c++ 379e41f4b71Sopenharmony_ci// 1. Obtain an H.264 encoder capability instance. 380e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true); 381e41f4b71Sopenharmony_ciif (capability == nullptr) { 382e41f4b71Sopenharmony_ci // Exception handling. 383e41f4b71Sopenharmony_ci} 384e41f4b71Sopenharmony_ci// 2. Check whether the combination of the profile and level is supported. 385e41f4b71Sopenharmony_cibool isSupported = OH_AVCapability_AreProfileAndLevelSupported(capability, AVC_PROFILE_MAIN, AVC_LEVEL_51); 386e41f4b71Sopenharmony_ci``` 387e41f4b71Sopenharmony_ci 388e41f4b71Sopenharmony_ci### Setting the Correct Video Width and Height 389e41f4b71Sopenharmony_ci 390e41f4b71Sopenharmony_ciThe video codec has restrictions on width and height alignment. For example, for YUV420 series, the default codec pixel format of popular codecs, downsampling is performed on the UV component. In this case, the width and height of the video codec must be 2-pixel aligned at least. There are other factors that may lead to stricter alignment restrictions. 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ciThe width and height of a video codec are restricted by the frame-level encoding and decoding capability of the codec and the frame-level capability defined in the protocol. For example, for H.264, AVC_LEVEL_51 limits the maximum number of macroblocks per frame to 36864. 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_ciThe formula for calculating the maximum frame rate based on the image width and height is as follows, where *MaxMBsPerFrameLevelLimits* indicates the maximum number of macroblocks per frame defined in the protocol that can be supported by the codec, and *MaxMBsPerFrameSubmit* indicates the maximum number of macroblocks per frame reported by the codec. In practice, the intersection of the two values is used. 395e41f4b71Sopenharmony_ci 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci| API | Description | 399e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 400e41f4b71Sopenharmony_ci| OH_AVCapability_GetVideoWidthAlignment | Obtains the video width alignment supported by a video codec.| 401e41f4b71Sopenharmony_ci| OH_AVCapability_GetVideoHeightAlignment | Obtains the video height alignment supported by a video codec.| 402e41f4b71Sopenharmony_ci| OH_AVCapability_GetVideoWidthRange | Obtains the video width range supported by a video codec.| 403e41f4b71Sopenharmony_ci| OH_AVCapability_GetVideoHeightRange | Obtains the video height range supported by a video codec.| 404e41f4b71Sopenharmony_ci| OH_AVCapability_GetVideoWidthRangeForHeight | Obtains the video width range of a video codec based on a given height.| 405e41f4b71Sopenharmony_ci| OH_AVCapability_GetVideoHeightRangeForWidth | Obtains the video height range of a video codec based on a given width.| 406e41f4b71Sopenharmony_ci| OH_AVCapability_IsVideoSizeSupported | Checks whether a video codec supports the combination of a given width and height.| 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ciIf you already know the video height and width, use the code snippet below to check whether they are supported. 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ci```c++ 411e41f4b71Sopenharmony_ciint32_t width = 1920; 412e41f4b71Sopenharmony_ciint32_t height = 1080; 413e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true); 414e41f4b71Sopenharmony_ci// 1. Check whether the video width and height are supported. 415e41f4b71Sopenharmony_cibool isSupported = OH_AVCapability_IsVideoSizeSupported(capability, width, height); 416e41f4b71Sopenharmony_ciif (!isSupported) { 417e41f4b71Sopenharmony_ci // 2. (Optional) Query detailed restrictions based on the video height and width and adjust the video height and width to use. 418e41f4b71Sopenharmony_ci} 419e41f4b71Sopenharmony_ci``` 420e41f4b71Sopenharmony_ci 421e41f4b71Sopenharmony_ciIf the video height or width is not supported or the configuration fails, use the following methods to find the supported video width and height range. 422e41f4b71Sopenharmony_ci 423e41f4b71Sopenharmony_ciFind the supported size configuration when the video width is known. The following is an example: 424e41f4b71Sopenharmony_ci 425e41f4b71Sopenharmony_ci```c++ 426e41f4b71Sopenharmony_ciint32_t width = 1920; 427e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true); 428e41f4b71Sopenharmony_ci// 1. Check whether the video width meets the width alignment requirements. 429e41f4b71Sopenharmony_ciint32_t widthAlignment = 0; 430e41f4b71Sopenharmony_ciint32_t ret = OH_AVCapability_GetVideoWidthAlignment(capability, &widthAlignment); 431e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || widthAlignment <= 0) { 432e41f4b71Sopenharmony_ci // Exception handling. 433e41f4b71Sopenharmony_ci} else if (width % widthAlignment != 0) { 434e41f4b71Sopenharmony_ci // 2. (Optional) Align the video width. 435e41f4b71Sopenharmony_ci width = (width + widthAlignment - 1) / widthAlignment * widthAlignment; 436e41f4b71Sopenharmony_ci} 437e41f4b71Sopenharmony_ci// 3. Check whether the video width is within the supported range. 438e41f4b71Sopenharmony_ciOH_AVRange widthRange = {-1, -1}; 439e41f4b71Sopenharmony_ciret = OH_AVCapability_GetVideoWidthRange(capability, &widthRange); 440e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || widthRange.maxVal <= 0) { 441e41f4b71Sopenharmony_ci // Exception handling. 442e41f4b71Sopenharmony_ci} else if (width < widthRange.minVal || width > widthRange.maxVal) { 443e41f4b71Sopenharmony_ci // 4. (Optional) Adjust the video width. 444e41f4b71Sopenharmony_ci width = min(max(width, widthRange.minVal), widthRange.maxVal); 445e41f4b71Sopenharmony_ci} 446e41f4b71Sopenharmony_ci// 5. Obtain the range of available video heights based on the video width. 447e41f4b71Sopenharmony_ciOH_AVRange heightRange = {-1, -1}; 448e41f4b71Sopenharmony_ciret = OH_AVCapability_GetVideoHeightRangeForWidth(capability, width, &heightRange); 449e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || heightRange.maxVal <= 0) { 450e41f4b71Sopenharmony_ci // Exception handling. 451e41f4b71Sopenharmony_ci} 452e41f4b71Sopenharmony_ci// 6. Select a proper video height from the range. 453e41f4b71Sopenharmony_ci``` 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ciFind the supported size configuration when the video height is known. The following is an example: 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ci```c++ 458e41f4b71Sopenharmony_ciint32_t height = 1080; 459e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true); 460e41f4b71Sopenharmony_ci// 1. Check whether the video height meets the high alignment requirements. 461e41f4b71Sopenharmony_ciint32_t heightAlignment = 0; 462e41f4b71Sopenharmony_ciint32_t ret = OH_AVCapability_GetVideoHeightAlignment(capability, &heightAlignment); 463e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || heightAlignment <= 0) { 464e41f4b71Sopenharmony_ci // Exception handling. 465e41f4b71Sopenharmony_ci} else if (height % heightAlignment != 0) { 466e41f4b71Sopenharmony_ci // 2. (Optional) Align the video height. 467e41f4b71Sopenharmony_ci height = (height + heightAlignment - 1) / heightAlignment * heightAlignment; 468e41f4b71Sopenharmony_ci} 469e41f4b71Sopenharmony_ci// 3. Check whether the video height is within the supported range. 470e41f4b71Sopenharmony_ciOH_AVRange heightRange = {-1, -1}; 471e41f4b71Sopenharmony_ciret = OH_AVCapability_GetVideoHeightRange(capability, &heightRange); 472e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || heightRange.maxVal <= 0) { 473e41f4b71Sopenharmony_ci // Exception handling. 474e41f4b71Sopenharmony_ci} else if (height < heightRange.minVal || height > heightRange.maxVal) { 475e41f4b71Sopenharmony_ci // 4. (Optional) Adjust the video height. 476e41f4b71Sopenharmony_ci height = min(max(height, heightRange.minVal), heightRange.maxVal); 477e41f4b71Sopenharmony_ci} 478e41f4b71Sopenharmony_ci// 5. Obtain the range of available video widths based on the video height. 479e41f4b71Sopenharmony_ciOH_AVRange widthRange = {-1, -1}; 480e41f4b71Sopenharmony_ciret = OH_AVCapability_GetVideoWidthRangeForHeight(capability, height, &widthRange); 481e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || widthRange.maxVal <= 0) { 482e41f4b71Sopenharmony_ci // Exception handling. 483e41f4b71Sopenharmony_ci} 484e41f4b71Sopenharmony_ci// 6. Select a proper video width from the range. 485e41f4b71Sopenharmony_ci``` 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ci### Setting the Correct Video Frame Rate 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ciThe frame rate of a video codec is restricted by the second-level encoding and decoding capability of the codec and the second-level capability defined in the protocol. For example, for H.264, AVC_LEVEL_51 limits the maximum number of macroblocks per second to 983040. 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ciThe formula for calculating the maximum frame rate based on the image width and height is as follows, where *MaxMBsPerSecondLevelLimits* indicates the maximum number of macroblocks per second defined in the protocol that can be supported by the codec, and *MaxMBsPerSecondSubmit* indicates the maximum number of macroblocks per second reported by the codec. In practice, the intersection of the two values is used. 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ci 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ci| API | Description | 496e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 497e41f4b71Sopenharmony_ci| OH_AVCapability_GetVideoFrameRateRange | Obtains the video frame rate range supported by a video codec.| 498e41f4b71Sopenharmony_ci| OH_AVCapability_GetVideoFrameRateRangeForSize | Obtains the video frame rate range of a video codec based on a given video size.| 499e41f4b71Sopenharmony_ci| OH_AVCapability_AreVideoSizeAndFrameRateSupported | Checks whether a video codec supports the combination of a video size and frame rate.| 500e41f4b71Sopenharmony_ci 501e41f4b71Sopenharmony_ciIf a specific frame rate is required, you can use the code snippet below to check whether the frame rate is supported. 502e41f4b71Sopenharmony_ci 503e41f4b71Sopenharmony_ci```c++ 504e41f4b71Sopenharmony_ciint32_t frameRate = 120; 505e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true); 506e41f4b71Sopenharmony_ci// 1. Obtain the supported frame rate range. 507e41f4b71Sopenharmony_ciOH_AVRange frameRateRange = {-1, -1}; 508e41f4b71Sopenharmony_ciint32_t ret = OH_AVCapability_GetVideoFrameRateRange(capability, &frameRateRange); 509e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || frameRateRange.maxVal <= 0) { 510e41f4b71Sopenharmony_ci // Exception handling. 511e41f4b71Sopenharmony_ci} 512e41f4b71Sopenharmony_ci// 2. Check whether the frame rate is within the range. 513e41f4b71Sopenharmony_cibool isSupported = frameRate >= frameRateRange.minVal && frameRate <= frameRateRange.maxVal; 514e41f4b71Sopenharmony_ci``` 515e41f4b71Sopenharmony_ci 516e41f4b71Sopenharmony_ciThe code snippet below shows how to find a proper frame rate configuration based on a given video size. 517e41f4b71Sopenharmony_ci 518e41f4b71Sopenharmony_ci```c++ 519e41f4b71Sopenharmony_ciconstexpr int32_t width = 1920; 520e41f4b71Sopenharmony_ciconstexpr int32_t height = 1080; 521e41f4b71Sopenharmony_ciint32_t frameRate = 120; 522e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true); 523e41f4b71Sopenharmony_ci// 1. Check whether the video size to be configured can reach the ideal frame rate. 524e41f4b71Sopenharmony_cibool isSupported = OH_AVCapability_AreVideoSizeAndFrameRateSupported(capability, width, height, frameRate); 525e41f4b71Sopenharmony_ciif (!isSupported) { 526e41f4b71Sopenharmony_ci // 2. Query the supported frame rate range based on the video size, and adjust the frame rate to be configured based on the query result. 527e41f4b71Sopenharmony_ci OH_AVRange frameRateRange = {-1, -1}; 528e41f4b71Sopenharmony_ci int32_t ret = OH_AVCapability_GetVideoFrameRateRangeForSize(capability, width, height, &frameRateRange); 529e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK || frameRateRange.maxVal <= 0) { 530e41f4b71Sopenharmony_ci // Exception handling. 531e41f4b71Sopenharmony_ci } 532e41f4b71Sopenharmony_ci frameRate = min(max(frameRate, frameRateRange.minVal), frameRateRange.maxVal); 533e41f4b71Sopenharmony_ci} 534e41f4b71Sopenharmony_ci 535e41f4b71Sopenharmony_ci// 3. Set the video size and frame rate. 536e41f4b71Sopenharmony_ciOH_AVCodec *videoEnc = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC); 537e41f4b71Sopenharmony_ciOH_AVFormat *format = OH_AVFormat_CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, width, height); 538e41f4b71Sopenharmony_ciif (!OH_AVFormat_SetIntValue(format, OH_MD_KEY_FRAME_RATE, frameRate)) { 539e41f4b71Sopenharmony_ci // Exception handling. 540e41f4b71Sopenharmony_ci} 541e41f4b71Sopenharmony_ciif (OH_VideoEncoder_Configure(videoEnc, format) != AV_ERR_OK) { 542e41f4b71Sopenharmony_ci // Exception handling. 543e41f4b71Sopenharmony_ci} 544e41f4b71Sopenharmony_ciOH_AVFormat_Destroy(format); 545e41f4b71Sopenharmony_ci``` 546e41f4b71Sopenharmony_ci 547e41f4b71Sopenharmony_ci### Setting the Correct Video Pixel Format 548e41f4b71Sopenharmony_ci 549e41f4b71Sopenharmony_ciThe video pixel format determines the pixel arrangement mode of an input image for encoding or an output image of decoding. For details, see **OH_AVPixelFormat**. 550e41f4b71Sopenharmony_ci 551e41f4b71Sopenharmony_ci| API | Description | 552e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 553e41f4b71Sopenharmony_ci| OH_AVCapability_GetVideoSupportedPixelFormats | Obtains the video pixel formats supported by a video codec.| 554e41f4b71Sopenharmony_ci 555e41f4b71Sopenharmony_ci```c++ 556e41f4b71Sopenharmony_ciconstexpr OH_AVPixelFormat DEFAULT_PIXELFORMAT = AV_PIXEL_FORMAT_NV12; 557e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true); 558e41f4b71Sopenharmony_ciif (capability == nullptr) { 559e41f4b71Sopenharmony_ci // Exception handling. 560e41f4b71Sopenharmony_ci} 561e41f4b71Sopenharmony_ci// 1. Obtain the video pixel formats supported by the video codec. 562e41f4b71Sopenharmony_ciconst int32_t *pixFormats = nullptr; 563e41f4b71Sopenharmony_ciuint32_t pixFormatNum = -1; 564e41f4b71Sopenharmony_ciint32_t ret = OH_AVCapability_GetVideoSupportedPixelFormats(capability, &pixFormats, &pixFormatNum); 565e41f4b71Sopenharmony_ciif (ret != AV_ERR_OK || pixFormats == nullptr || pixFormatNum <= 0) { 566e41f4b71Sopenharmony_ci // Exception handling. 567e41f4b71Sopenharmony_ci} 568e41f4b71Sopenharmony_ci// 2. Check whether the pixel format to be configured is supported. 569e41f4b71Sopenharmony_cibool isMatched = false; 570e41f4b71Sopenharmony_cifor (int i = 0; i < pixFormatNum; i++) { 571e41f4b71Sopenharmony_ci if (pixFormats[i] == DEFAULT_PIXELFORMAT) { 572e41f4b71Sopenharmony_ci isMatched = true; 573e41f4b71Sopenharmony_ci } 574e41f4b71Sopenharmony_ci} 575e41f4b71Sopenharmony_ciif (!isMatched) { 576e41f4b71Sopenharmony_ci // 3. Replace an unsupported pixel format or select another codec. 577e41f4b71Sopenharmony_ci} 578e41f4b71Sopenharmony_ci``` 579e41f4b71Sopenharmony_ci 580e41f4b71Sopenharmony_ci### Checking Whether a Codec Feature Is Supported and Obtaining Its Properties 581e41f4b71Sopenharmony_ci 582e41f4b71Sopenharmony_ciA codec feature refers to an optional feature used only in specific encoding and decoding scenarios. For details, see **OH_AVCapabilityFeature**. 583e41f4b71Sopenharmony_ci 584e41f4b71Sopenharmony_ci| API | Description | 585e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 586e41f4b71Sopenharmony_ci| OH_AVCapability_IsFeatureSupported | Check whether a codec supports a given feature.| 587e41f4b71Sopenharmony_ci| OH_AVCapability_GetFeatureProperties | Obtains the properties of a feature. Only some features have property information.| 588e41f4b71Sopenharmony_ci 589e41f4b71Sopenharmony_ciThe code snippet below is an example of querying whether the H.264 encoder supports the long-term reference frame feature: 590e41f4b71Sopenharmony_ci 591e41f4b71Sopenharmony_ci```c++ 592e41f4b71Sopenharmony_ciconstexpr int32_t NEEDED_LTR_NUM = 2; 593e41f4b71Sopenharmony_ciOH_AVFormat *format = OH_AVFormat_CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, 1920, 1080); 594e41f4b71Sopenharmony_ciOH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true); 595e41f4b71Sopenharmony_ciif (capability == nullptr) { 596e41f4b71Sopenharmony_ci // Exception handling. 597e41f4b71Sopenharmony_ci} 598e41f4b71Sopenharmony_ci// 1. Check whether the long-term reference frame feature is supported. 599e41f4b71Sopenharmony_cibool isSupported = OH_AVCapability_IsFeatureSupported(capability,VIDEO_ENCODER_LONG_TERM_REFERENCE); 600e41f4b71Sopenharmony_ciif (isSupported) { 601e41f4b71Sopenharmony_ci // 2. Obtain the number of supported long-term reference frames. 602e41f4b71Sopenharmony_ci OH_AVFormat *properties = OH_AVCapability_GetFeatureProperties(capability, VIDEO_ENCODER_LONG_TERM_REFERENCE); 603e41f4b71Sopenharmony_ci int32_t maxLTRCount = -1; 604e41f4b71Sopenharmony_ci bool ret = OH_AVFormat_GetIntValue(properties, OH_FEATURE_PROPERTY_KEY_VIDEO_ENCODER_MAX_LTR_FRAME_COUNT, &maxLTRCount); 605e41f4b71Sopenharmony_ci if (ret && maxLTRCount >= NEEDED_LTR_NUM) { 606e41f4b71Sopenharmony_ci if (!OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_LTR_FRAME_COUNT, NEEDED_LTR_NUM)) { 607e41f4b71Sopenharmony_ci // Exception handling. 608e41f4b71Sopenharmony_ci } 609e41f4b71Sopenharmony_ci } 610e41f4b71Sopenharmony_ci} 611e41f4b71Sopenharmony_ci// 3. Create and configure an encoder. 612e41f4b71Sopenharmony_ciOH_AVCodec *videoEnc = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC); 613e41f4b71Sopenharmony_ciif (OH_VideoEncoder_Configure(videoEnc, format) != AV_ERR_OK) { 614e41f4b71Sopenharmony_ci // Exception handling. 615e41f4b71Sopenharmony_ci} 616e41f4b71Sopenharmony_ci``` 617