1# B-Frame Video Encoding 2 3<!--Kit: AVCodec Kit--> 4<!--Subsystem: Multimedia--> 5<!--Owner: @jiangfan56--> 6<!--Designer: @dpy2650---> 7<!--Tester: @cyakee--> 8<!--Adviser: @zengyawen--> 9 10Starting from API version 20, B-frame video encoding is supported. 11 12B-frames, short for Bidirectional Predictive Frames, are one of the primary frame types defined in video encoding standards. The main difference between B-frames and P-frames is that while P-frames support only forward prediction, B-frames support bidirectional prediction. 13 14The prediction process in B-frame encoding utilizes information from both preceding and succeeding frames, which can significantly reduce temporal redundancy in the signal and achieve higher compression efficiency. 15 16Enabling B-frames has the following impacts: 17- Enabling B-frames increases the distance between reference frames for P-frame encoding, which may affect the compression ratio and visual quality of P-frames. Generally, in scenarios with complex temporal dynamics, the combined impact of P-frames and B-frames should be considered. 18- Enabling B-frames also increases the end-to-end latency for both encoding and decoding. 19 20## When to Use 21 22- Video recording 23- Editing and exporting 24- Video transcoding 25- Non-low-latency live streaming 26 27## Constraints 28 29- **Supported platforms**: This feature is platform-dependent. You can query the support using the [OH_AVCapability_IsFeatureSupported](../../reference/apis-avcodec-kit/_a_v_capability.md#oh_avcapability_isfeaturesupported) API. 30- **Supported API versions**: API version 20 and later. 31- **Supported encoders**: This feature is encoder-dependent. You can query the support using the [OH_AVCodec_GetCapabilityByCategory](../../reference/apis-avcodec-kit/_a_v_capability.md#oh_avcodec_getcapabilitybycategory) API. 32- **Supported bit rate control modes**: VBR, CBR, SQR, and CQ. 33- B-frame encoding is not supported alongside temporally scalable video encoding. 34- B-frame encoding is not supported alongside long-term reference frames. 35 36## Available APIs 37 38B-frame encoding must be configured at the initialization phase and takes effect throughout the process. Dynamic modification is not supported. 39 40The configuration parameters are as follows: 41 42|Parameter|Description|Format| 43|------- |------- |------- | 44|OH_MD_KEY_VIDEO_ENCODER_ENABLE_B_FRAME |Enabled status of B-frame video recording.<br>B-frame encoding can be enabled only when it is supported.|int | 45|OH_MD_KEY_VIDEO_ENCODER_MAX_B_FRAMES |Maximum number of B-frames supported by video encoding.<br>It is used only for query.|int | 46 47## How to Develop 48 49For details about basic encoding functionality, see [Video Encoding](video-encoding.md). The following are specific instructions for B-frame encoding. 50 511. When creating an encoder instance, check whether the video encoder supports B-frame encoding. 52 53 ```c++ 54 // 1.1 Obtain the video encoder capability instance. The following uses H.265 as an example. 55 OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_HEVC, true, HARDWARE); 56 if (cap != nullptr) { 57 OH_LOG_INFO("Get codec Capability sucess!"); 58 // 1.2 Check whether B-frame encoding is supported. 59 bool isSupported = OH_AVCapability_IsFeatureSupported(cap, VIDEO_ENCODER_B_FRAME); 60 int32_t supportedMaxBFrameCount = 0; 61 if (isSupported) { 62 OH_LOG_INFO("Feature VIDEO_ENCODER_B_FRAME is Supported!"); 63 // 1.3 Query the maximum number of B-frames that can be supported. 64 OH_AVFormat *property = OH_AVCapability_GetFeatureProperties(cap, VIDEO_ENCODER_B_FRAME); 65 OH_AVFormat_GetIntValue(property, OH_MD_KEY_VIDEO_ENCODER_MAX_B_FRAMES, &supportedMaxBFrameCount); 66 OH_LOG_INFO("Get supportedMaxBFrameCount,value is %{public}d!", supportedMaxBFrameCount); 67 // 1.4 Destroy the temporary AVFormat. 68 OH_AVFormat_Destroy(property); 69 } else { 70 OH_LOG_ERROR("Feature VIDEO_ENCODER_B_FRAME is not Supported!"); 71 } 72 } else { 73 OH_LOG_ERROR("codec Capability is null!"); 74 } 75 ``` 76 77 If B-frame encoding is supported, you can enable it. 78 792. Configure B-frame encoding during the configuration phase. 80 81 ```c++ 82 // 2.1 Create a temporary AVFormat used for configuration. 83 OH_AVFormat *format = OH_AVFormat_Create(); 84 // 2.2 Fill in the key-value pair of the parameter used to enable the feature. 85 OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_ENABLE_B_FRAME, 1); 86 // 2.3 Set parameters. 87 int32_t ret = OH_VideoEncoder_Configure(videoEnc, format); 88 if (ret != AV_ERR_OK) { 89 // Handle exceptions. 90 } 91 // 2.4 Destroy the temporary AVFormat after the configuration is complete. 92 OH_AVFormat_Destroy(format); 93 ``` 94 95 >**NOTE** 96 > - Enabling B-frame encoding is weakly checked during the configure phase. If the parameter configuration is incorrect or the encoder does not support the feature, **AV_ERR_OK** is returned, with a warning message printed. Check whether the feature is truly enabled based on the encoded stream and runtime logs. 97 > - When B-frame encoding is enabled, the actual B-frame structure and number of B-frames in the encoded stream depend on the platform's capabilities. 98 > 99