Video Codec
Principle
The principle of video coding is the elimination of redundant information and compression of video signals to reduce the amount of data and facilitate storage and transmission. The implementation of video coding usually relies on specific video coding standards, such as H.264, H.265, and so on. These standards specify specific coding algorithms and parameters for efficient video compression and transmission.
Video Encoding Principle
Taking the H.265 coding protocol as an example, video coding involves the following key steps:

- Segmentation
H.265 first divides the video into a number of sequences, and a sequence is divided into a number of Groups of Picture (GOP), each GOP represents a set of consecutive video frames.
- Spatial prediction
H.265 uses spatial prediction to remove redundant information between image blocks. As shown in the figure below, from a spatial point of view, the difference in pixel values between pixel points inside a single video frame is very small. From a temporal perspective, there are also many identical pixel points between two consecutive video frames.
Predictive coding is a method of data compression based on the statistical properties of an image that utilizes the temporal and spatial correlation of an image to predict the pixels that are currently being coded from the pixel data that has been reconstructed.
Intra-frame prediction means that both the pixels used for prediction and the pixels currently being encoded are within the same video frame and are generally in neighboring regions. Due to the strong correlation between neighboring pixels, the pixel values are generally very close to each other, and the probability of a mutation is very small, with the differences all being zero or very small numbers. Therefore, what is transmitted after intra-frame prediction coding is the difference between the predicted value and the true value, i.e., the value around 0, which is called the prediction error or residual. This enables compression to be achieved with fewer bits transmitted.
H.265 intra-frame predictive coding is done on a block basis, using the rebuilt values of neighboring blocks that have been rebuilt for the block being coded. The prediction component is divided into two, luminance and chrominance, and the corresponding prediction blocks are luminance prediction block and chrominance prediction block, respectively. In order to adapt to the content characteristics of HD video and improve the prediction accuracy, H.265 adopts richer prediction block sizes and prediction modes.
Inter-frame prediction means that the pixels used for prediction and the pixels currently being encoded are not in the same video frame, but are generally in adjacent or nearby locations. In general, inter-frame predictive coding provides better compression than intra-frame prediction, mainly because of the very strong correlation between video frames. If the rate of change of moving objects in a video frame is slow, then the pixel differences between video frames are small and the temporal redundancy is very large.
The method of inter-frame prediction to evaluate the motion condition of a moving object is motion estimation, and its main idea is to search for a matching block for the prediction block from a given range of the reference frame, and calculate the relative displacement between the matching block and the prediction block, and this relative displacement is the motion vector. After obtaining the motion vector, the prediction needs to be corrected, also known as motion compensation. Input the motion vectors into the motion compensation module and "compensate" the reference frame to get the prediction frame of the current encoded frame. The difference between the predicted frame and the current frame is the inter-frame prediction error.
If the inter-frame prediction only uses the previous frame image, it is called forward inter-frame prediction or unidirectional prediction. This predicted frame is also known as a P-frame, and the P-frame can refer to the previous I-frame or P-frame.
Inter-frame prediction is bidirectional if it uses not only the previous frame image to predict the current block, but also the subsequent frame image. This prediction frame is also known as the B-frame, which can refer to the preceding I-frame or P-frame and the following P-frame. Since P-frames need to refer to previous I-frames or P-frames, and B-frames need to refer to previous I-frames or P-frames and later P-frames, if in a video stream, the B-frame arrives first, and the dependent I-frames and P-frames haven't arrived yet, then the B-frame can't be decoded immediately, so what should be done to ensure the playback order?
Actually, PTS and DTS are generated during video encoding. Typically, after generating an I-frame, the encoder skips backward a few frames and encodes a P-frame using the preceding I-frame as a reference frame, and the frame between the I-frame and the P-frame is encoded as a B-frame. The video frame order of the push stream is already coded at the time of encoding in the dependent order of I-frames, P-frames, and B-frames, and the data is directly decoded upon receipt. Therefore, it is not possible to receive B-frames first and then dependent I-frames and P-frames.
- Transformation and quantification
In H.265, transform and quantization are used to further compress the data. By transforming the prediction error, the data can be converted from the time domain to the frequency domain for better removal of data redundancy. The transformed data is then quantized to map the data to a lower precision, thus further compressing the data. The process can be referred to the JPEG coding process.
- Entropy encoding
In the final step of the encoding process, H.265 uses entropy coding to losslessly compress the data. The main purpose of entropy coding is to minimize the redundancy of the coded data in order to improve the data compression efficiency. The process can be referred to JPEG coding process.
Video Decoding Principle
Video decoding refers to the process of converting compressed video data back to the original video format. The process of video decoding is mainly divided into entropy decoding, inverse quantization, inverse transformation, motion compensation and deconvolution, and post-processing, each of which is designed to recover as close as possible to the original video picture from the highly compressed data. Since the goal of video encoding is to minimize the file size, the decoding process must perform each step of the encoding process precisely in reverse to recover the video content:
- Entropy decoding
Entropy decoding is the process of converting compressed data back into a more manageable video format. In the process of video encoding, entropy coding techniques such as Hoffman coding or arithmetic coding are usually used to reduce the amount of data. And the purpose of entropy decoding is to recover the symbols used in the video coding process to prepare for the next step of inverse quantization.
- Inverse quantization
Inverse quantization is the process of flipping the quantization (the step in the encoding process that reduces the precision of the data in order to achieve space savings) in order to restore the precision of the original data, a step that is critical to restoring image quality.
- Inverse transform
Inverse tranform is the process of reversing the changes used in coding (e.g., the Discrete Cosine Transform, DCT) in order to restore the data from the transform domain (e.g., the frequency domain) to the spatial domain (i.e., the original image), and this step is a key step in image reconstruction.
- Motion compensation & deblocking
Motion compensation means that, for predicted frames (i.e., frames generated based on the previous/next frame), the complete frame needs to be reconstructed using the run vector data. Deconvolution, on the other hand, is the process of removing block artifacts generated during the coding process. These steps are essential to restore smooth and consistent video playback.
- Post-processing
Post-processing, as the final step, involves some techniques to enhance video quality such as denoising, sharpening, etc. This step is optional, and whether or not it is carried out depends on the requirements of video playback and whether or not the hardware capability can support it.
API Interface
Video Encoding Interface
Video Decoding Interface
For detailed interface information, please refer to hbVPVideoEncode and hbVPVideoDecode.
Usage
Video Encoding Usage
Video Decoding Usage
Additional Notes
Bit Rate Control Mode
The encoder supports both H.264 and H.265 standards, offering five bitrate control modes: CBR (Constant Bitrate), VBR (Variable Bitrate), AVBR (Adaptive Variable Bitrate), FixQp, and QpMap. These modes ensure flexibility in balancing bitrate stability, image quality, and compression efficiency. For example, CBR maintains a consistent overall bitrate, while VBR focuses on stable image quality. FixQp keeps the quantization parameter (QP) fixed for specific frames, and QpMap assigns QP values to blocks within frames, with H.265 using 32x32 blocks and H.264 using 16x16. The following explanation uses H.265 as an example for rate control settings.
- Constant Bit Rate(CBR)
CBR (Constant Bitrate) refers to maintaining a stable bitrate throughout the encoding process, ensuring that the overall bitrate of the video stream remains constant. The following are the meanings of the parameters used in CBR mode:
- Variable Bit Rate(VBR)
VBR (Variable Bitrate) dynamically adjusts the bitrate during the encoding process based on the complexity of the scene. In simple scenes, a larger QP (Quantization Parameter) is assigned to achieve higher compression. In complex scenes, a smaller QP is allocated to maintain stable image quality. The following are the meanings of the parameters used in VBR mode:
- Average Variable Bit Rate(AVBR)
AVBR (Adaptive Variable Bitrate) is a strategy that maintains a stable average bitrate throughout the encoding process. It combines the advantages of both CBR and VBR: in simple scenes, a lower bitrate is allocated, while in complex scenes, enough bitrate is provided to ensure image quality. This ensures that the bitrate is reasonably distributed across different scenes. At the same time, AVBR ensures that the average bitrate approaches the preset target bitrate over a certain period, helping to control the output file size. AVBR can be viewed as a compromise between CBR and VBR, resulting in a stream with relatively stable bitrate and image quality. The following are the meanings of the parameters used in AVBR mode:
- Fix QP
In FixQp (Fixed QP) mode, the encoder assigns a fixed Quantization Parameter (QP) value to each I-frame, P-frame, and B-frame without making any dynamic adjustments. This mode is typically used in scenarios where strict control over image quality and compression ratio is required. The following are the meanings of the parameters used in FixQp mode:
- Qp Map
The QpMap mode allows different Quantization Parameter (QP) values to be assigned to each macroblock within a frame, enabling more fine-grained control over the bitrate. For H.265 encoding, the size of each macroblock is 32x32. The following are the meanings of the parameters used in QpMap mode:
GOP Structure
H.264 and H.265 encoding support the configuration of GOP structures, allowing you to choose from preset GOP structures. Below are the descriptions of the preset GOP structures:



