Disclosure of Invention
The application provides a video coding method and a video coding device, which are used for solving the problem of poor video coding effect.
In a first aspect, the present application provides a video encoding method, including:
Acquiring an image frame in a target video, wherein the target video comprises a plurality of continuous image frames;
When the image frame is a first frame, determining a quantization parameter of the first frame according to the resolution and the code rate of the target video, and encoding the first frame according to the quantization parameter of the first frame;
When the image frame is a target frame after the first frame, calculating the content complexity of a previous frame of the target frame after encoding, and adjusting the quantization parameter of the previous frame according to the comparison result of the content complexity and a first set range to obtain a first quantization parameter;
Calculating the motion amplitude between the target frame and the frame above the target frame, and adjusting the first quantization parameter according to the comparison result of the motion amplitude and the second set range to obtain a second quantization parameter;
calculating the ratio of the decoded key frame to the forward predictive coding frame, and adjusting the second quantization parameter according to the comparison result of the ratio and a third setting range to obtain a third quantization parameter;
Calculating the peak signal-to-noise ratio of the last frame of the target frame, and adjusting the third quantization parameter according to the fact that the peak signal-to-noise ratio is smaller than a threshold value to obtain a fourth quantization parameter;
and encoding the target frame according to the fourth quantization parameter.
In some embodiments, the quantization parameter comprises a minimum quantization step size of a current frame, a maximum quantization step size of the current frame, a minimum quantization step size of the key frame, and a maximum quantization step size of the key frame.
In some embodiments, the adjusting the quantization parameter of the previous frame according to the comparison result of the content complexity and the first set range includes:
If the content complexity is larger than the maximum value of the first set range, reducing the maximum quantization step length of the key frame and the maximum quantization step length of the current frame;
And if the content complexity is smaller than the minimum value of the setting range corresponding to the first setting range, increasing the minimum quantization step length of the key frame and the minimum quantization step length of the current frame.
In some embodiments, the adjusting the first quantization parameter according to the comparison result of the motion amplitude and the second set range includes:
If the motion amplitude is larger than the maximum value of the second set range, reducing the maximum quantization step length of the key frame and the maximum quantization step length of the current frame;
and if the motion amplitude is smaller than the minimum value of the second set range, increasing the maximum quantization step length of the key frame and the minimum quantization step length of the current frame.
In some embodiments, the adjusting the second quantization parameter according to the comparison result of the ratio and the third setting range includes:
if the ratio is larger than the maximum value of the third setting range, increasing the maximum quantization step length of the key frame and the minimum quantization step length of the key frame;
and if the ratio is smaller than the minimum value of the third setting range, reducing the maximum quantization step length of the key frame and the minimum quantization step length of the key frame.
In some embodiments, the adjusting the third quantization parameter according to the peak signal-to-noise ratio being less than a threshold value includes:
And if the peak signal-to-noise ratio is smaller than a threshold value, increasing the minimum quantization step length of the current frame, the maximum quantization step length of the current frame, the minimum quantization step length of a key frame and the maximum quantization step length of the key frame.
In some embodiments, in the quantization parameter of the first frame, a minimum quantization step size of the current frame is the same as a minimum quantization step size of the key frame, and a maximum quantization step size of the current frame is the same as a maximum quantization step size of the key frame.
In a second aspect, the present application provides a video encoding apparatus comprising:
The input module is used for acquiring image frames in a target video, wherein the target video comprises a plurality of continuous image frames;
The first frame coding module is connected with the input module and is used for determining a quantization parameter of the first frame according to the resolution and the code rate of the target video when the image frame is the first frame, and coding the first frame according to the quantization parameter of the first frame;
The target frame coding module is connected with the input module and comprises:
The content complexity analysis unit is used for calculating the content complexity of the last frame of the target frame after encoding when the image frame is the target frame after the first frame, and adjusting the quantization parameter of the last frame according to the comparison result of the content complexity and the first setting range to obtain a first quantization parameter;
The motion amplitude analysis unit is connected with the content complexity unit and is used for calculating the motion amplitude between the target frame and the frame above the target frame, and adjusting the first quantization parameter according to the comparison result of the motion amplitude and the second set range to obtain a second quantization parameter;
The ratio analysis unit is connected with the motion amplitude analysis unit and is used for calculating the ratio of the decoded key frame to the forward predictive coding frame, and adjusting the second quantization parameter according to the comparison result of the ratio and a third set range to obtain a third quantization parameter;
the peak signal-to-noise ratio unit is connected with the ratio analysis unit and is used for calculating the peak signal-to-noise ratio of the last frame of the target frame, and adjusting the third quantization parameter according to the fact that the peak signal-to-noise ratio is smaller than a threshold value to obtain a fourth quantization parameter;
and the encoding unit is used for encoding the target frame according to the fourth quantization parameter.
In some embodiments, the quantization parameter comprises a minimum quantization step size of a current frame, a maximum quantization step size of the current frame, a minimum quantization step size of the key frame, and a maximum quantization step size of the key frame.
In some embodiments, in the quantization parameter of the first frame, a minimum quantization step size of the current frame is the same as a minimum quantization step size of the key frame, and a maximum quantization step size of the current frame is the same as a maximum quantization step size of the key frame.
As can be seen from the above technical content, the embodiments of the present application provide a video encoding method and apparatus. After the encoding of the first frame is completed, the embodiment of the application starts the self-adaptive adjustment process of the quantization parameter, automatically adjusts the quantization parameter according to the content complexity, the motion amplitude, the ratio of the key frame to the forward predictive encoding frame and the peak signal to noise ratio when encoding the target frame after the first frame, so that the quantization parameter can be automatically adapted to various scenes, different video contents are adapted, the flexibility is high, the code rate and the image quality of video encoding are both considered, the better video encoding effect is achieved, the user does not need to configure various quantization parameters, the complexity of the user configuration is reduced, the encoding effects such as mosaic effect, respiratory effect and encoding block effect caused by unsuitable quantization parameters configured by the user are effectively avoided, and the user experience is improved.
Detailed Description
Reference will now be made in detail to the embodiments, examples of which are illustrated in the accompanying drawings. When the following description refers to the accompanying drawings, the same numbers in different drawings refer to the same or similar elements, unless otherwise indicated. The embodiments described in the examples below do not represent all embodiments consistent with the application. Merely exemplary of systems and methods consistent with aspects of the application as set forth in the claims.
The embodiment of the application provides a video coding method and a video coding device, which are suitable for video processing intelligent equipment, wherein the video processing intelligent equipment can comprise security equipment such as IPC (Internet Protocol Camera, network camera), vehicle-mounted video processing equipment or image processing equipment and the like. The quantization parameters are adaptively adjusted through various parameters such as content complexity, motion amplitude and the like, so that the quantization parameters can be adapted to video coding under various scenes, the code rate and the image quality of the video coding can be considered, a user does not need to manually adjust the quantization parameters according to the scenes, and the video coding efficiency and the video coding effect are improved.
Referring to fig. 1, a flowchart of a video encoding method according to an embodiment of the present application is shown. As shown in fig. 1, the video encoding method may include the steps of:
step S100, obtaining an image frame in a target video, wherein the target video comprises a plurality of continuous image frames.
In some embodiments, the target video is an original video captured by a security device, the target video comprising a plurality of consecutive image frames.
After these continuous image frames are encoded, the security device may obtain a plurality of different types of image frames, such as I (intra-frame), B (predictive-frame), and P (bi-directional interpolated prediction frame, bi-predictive interpolated frame). These different types of image frames are combined into encoded video.
I frames are also referred to as key frames. The I frame contains the complete data of the picture of the frame, and has the characteristic of independent decoding, namely, the I frame can be decoded only by the data of the frame. Since the I-frame contains the complete data of the picture of the present frame, the compression rate of the I-frame is generally low.
The P-frame contains the difference data between the present frame and the previous key frame (or P-frame), and the difference defined by the present frame needs to be overlapped by the picture cached before decoding to generate the picture of the present frame. Since the P frame contains only difference data from the previous frame and does not contain the same data as the previous frame, the compression rate of the P frame is generally greater than that of the I frame.
The B frame contains difference data between the present frame and the preceding and following frames, and when decoding, not only the previous buffer picture but also the picture after the present frame is needed, and the final picture is obtained by overlapping the preceding and following frames with the present frame data. Since the B frame contains only difference data with the previous frame and difference data with the following frame, does not contain the same data as the previous frame nor the same data as the following frame, the compression rate of the B frame is generally greater than that of the P frame.
And step 200, when the image frame is a first frame, determining a quantization parameter of the first frame according to the resolution and the code rate of the target video, and encoding the first frame according to the quantization parameter of the first frame.
In the encoding process, quantization parameters are important parameters affecting the compression rate and image quality. The smaller the quantization parameter, the smaller the compression ratio and the higher the image quality, but the larger the encoded image frame volume, and the higher the quantization parameter, the higher the compression ratio and the worse the image quality, and the smaller the encoded image frame volume.
In some embodiments, the user may pre-configure the quantization parameter of the security device, i.e., configure an adjustment range for the quantization parameter. When the security device encodes the target video, the quantization parameter can be adaptively adjusted within the adjustment range. In order to facilitate the configuration of the user and ensure the adjustment space, the adjustment range can be a larger interval range. The adjustment range may be (1, 51) in bits, for example.
In some embodiments, the quantization parameters may include quantization parameters of the I frame including MaxIQp (maximum quantization step size of the I frame) and MinIQp (minimum quantization step size of the I frame) and quantization parameters of the current frame including MaxQp (maximum quantization step size) and MinQp (minimum quantization step size).
MaxIQp and MinIQp are used to define the quantization step size of the I-frame, which is between MinIQp and MaxIQp. The smaller the quantization step of the I frame is, the clearer the I frame is, and the larger the quantization step of the I frame is, the more blurred the I frame is. Since an I frame is a reference frame for P and B frames, its image quality directly affects the decoding quality of subsequent frames.
MaxQp and MinQp are used to define the quantization step size of the current frame, which is between MinQp and MaxQp. The smaller the quantization step length of the current frame is, the clearer the encoded current frame is, and the larger the quantization step length of the current frame is, the more blurred the encoded current frame is.
In some embodiments, the user may configure the 4 quantization parameters to:
MaxQp:1
MinQp:51
MaxIQp:1
MinIQp:51
After the user configuration is completed, the quantization step ranges of the I frame and the current frame are (1, 51), and the (1, 51) is the maximum range of the quantization parameter, so that the user can configure the quantization step in the range, so that the security device has sufficient adjustment space when adjusting the quantization parameter, and the security device can adapt to various scenes, such as indoor scenes, outdoor scenes, static scenes, moving scenes and the like.
In some embodiments, for a first frame of the target video, bpp (Bits Per Pixel) may be calculated according to the resolution and the code rate of the target video, the quantization parameter of the first frame may be configured according to bpp, and after the quantization parameter of the first frame is configured, the first frame may be encoded according to the quantization parameter of the first frame, so as to obtain an encoded image frame.
And step 300, when the image frame is a target frame after the first frame, calculating the content complexity of the last frame of the target frame after encoding, and adjusting the quantization parameter of the last frame according to the comparison result of the content complexity and the first set range to obtain a first quantization parameter.
In some embodiments, after the first frame is encoded, encoding of subsequent image frames of the target video is started, and when the subsequent image frames are encoded, quantization parameters can be dynamically adjusted according to the already encoded image frames so as to consider both the quality and the volume of the encoded image in real time. Wherein, to facilitate distinguishing from the first frame, an image frame located after the first frame in the target video may be referred to as a target frame.
In some embodiments, in determining the quantization parameter of the target frame, the quantization parameter of the previous frame may be adjusted according to the content complexity of the previous frame. In order to distinguish the quantization parameter from the quantization parameter of the previous frame, the quantization parameter processed by this step is referred to as a first quantization parameter.
The higher the content complexity, the more complex the current scene is represented, and the more difficult the user analyzes the picture content. If the code rate of the subsequent image frames is smaller, the image quality is poor, the analysis of the picture content is affected, if the code rate of the subsequent image frames is larger, the image quality can be ensured, and if the code rate is too high, the encoded video volume is larger. Therefore, the quantization parameter needs to be adjusted so as to adjust the code rate to a larger range, so as to achieve both the quality of the coded image and the video volume.
The lower the content complexity, the simpler the current scene is represented, and the lower the analysis difficulty of the user on the picture content is. If the code rate of the subsequent image frames is larger, the volume of the coded video is larger, if the code rate of the subsequent image frames is smaller, the analysis of the picture content by a user can be ensured because of simpler scenes, and if the code rate is too small, the image quality of the coded video is too poor, so that the user experience is influenced. Therefore, the quantization parameter needs to be adjusted so as to adjust the code rate to a smaller range, so as to achieve both the volume and the image quality of the encoded video.
In some embodiments, the content complexity may be compared with the first setting range to obtain a comparison result, and whether to adjust the quantization parameter and how to adjust the quantization parameter are determined according to the comparison result, where the adjusted step size may be configured according to the actual requirement.
For example, if the content complexity is greater than the maximum value of the first set range, the maximum quantization step size of the key frame and the maximum quantization step size of the current frame are reduced, where the first set range may be 10-14, and the unit is bit rate. When the content complexity is larger, for example, the content complexity is larger than 14, the quantization step length of the I frame and the quantization step length of the current frame can be limited in a lower range by reducing the maximum quantization step length of the I frame and the maximum quantization step length of the current frame, so that the image quality is ensured. And obtaining a first quantization parameter after the quantization parameter is adjusted.
And if the content complexity is smaller than the minimum value of the setting range corresponding to the first setting range, increasing the minimum quantization step length of the key frame and the minimum quantization step length of the current frame. When the content complexity is smaller, for example, smaller than 10, the quantization step length of the current frame and the quantization step length of the I frame can be limited in a higher range by increasing the minimum quantization step length of the I frame and the minimum quantization step length of the current frame, so as to reduce the volume of the encoded image frame. And obtaining a first quantization parameter after the quantization parameter is adjusted.
If the content complexity is within the first set range, the quantization parameter is not adjusted in this step, and the quantization parameter of the previous frame may be directly determined as the first quantization parameter.
And step 400, calculating the motion amplitude between the target frame and the frame above the target frame, and adjusting the first quantization parameter according to the comparison result of the motion amplitude and the second set range to obtain a second quantization parameter.
In some embodiments, the motion amplitude is the degree of displacement or change between two adjacent image frames. The larger the motion amplitude is, the larger the change between two adjacent frames is, at the moment, the improvement of the code rate can improve the image quality, thereby being beneficial to the analysis of the picture content, and the smaller the motion amplitude is, the smaller the change between two adjacent frames is, at the moment, the reduction of the code rate can improve the compression rate, thereby being beneficial to the reduction of the video volume.
In some embodiments, the motion amplitude may be compared with the second set range to obtain a comparison result, and whether the quantization parameter needs to be adjusted or not and how to adjust the quantization parameter are determined according to the comparison result.
For example, if the motion amplitude is greater than the maximum value of the second set range, the maximum quantization step size of the key frame and the maximum quantization step size of the current frame are reduced, where the second set range may be 120-200 pixels. When the motion amplitude is larger, for example, larger than 200 pixels, the quantization step length of the I frame and the quantization step length of the current frame can be limited in a lower range by reducing the maximum quantization step length of the I frame and the maximum quantization step length of the current frame, so that the image quality is ensured. And obtaining a second quantization parameter after the first quantization parameter is adjusted.
And if the motion amplitude is smaller than the minimum value of the setting range corresponding to the second setting range, increasing the maximum quantization step length of the key frame and the minimum quantization step length of the current frame. When the motion amplitude is smaller, for example, less than 120 pixels, the quantization step length of the I frame and the quantization step length of the current frame can be limited in a higher range by increasing the maximum quantization step length of the I frame and the minimum quantization step length of the current frame, so that the volume of the encoded image frame is reduced. And obtaining a second quantization parameter after the quantization parameter is adjusted.
If the motion amplitude is within the second set range, the first quantization parameter may be directly determined as the second quantization parameter without adjusting the quantization parameter in this step.
And S500, calculating the ratio of the decoded key frame to the forward predictive coding frame, and adjusting the second quantization parameter according to the comparison result of the ratio and a third set range to obtain a third quantization parameter.
In some embodiments, the ratio of key frames to forward predictive encoded frames, i.e., the IP ratio, is the ratio of the amount of data for I frames to the amount of data for P frames. The larger the ratio is, the larger the difference of the data amount of the I frame and the P frame is, namely the larger the difference of the image quality of the I frame and the image quality of the P frame is, the smaller the ratio is, namely the closer to 1 is, the smaller the difference of the data amount of the I frame and the P frame is, namely the smaller the difference of the image quality of the I frame and the image quality of the P frame is.
The difference between the data volumes of the I frame and the P frame is large, so that the respiratory effect is obvious. Respiratory effects refer to the periodic variation in image quality. Since the coding modes and image qualities of the I frame and the P frame are not the same, a respiratory effect easily occurs at the time of switching between the I frame and the P frame.
The ratio of the decoded key frame to the forward predictive encoded frame may be calculated prior to decoding the target frame. The decoded key frame may refer to a key frame in a gop (Group of Pictures ) in which the target frame is located, and the forward predictive-coded frame may refer to a P-frame in the gop in which the target frame is located.
In this step, the respiratory effect is optimized by automatically adjusting the IP ratio.
In some embodiments, the IP ratio may be compared with the third setting range to obtain a comparison result, and whether the quantization parameter needs to be adjusted or not and how to adjust the quantization parameter are determined according to the comparison result.
For example, if the IP ratio is greater than the maximum value of the third setting range, the maximum quantization step size and the minimum quantization step size of the key frame are increased, where the third setting range may be 60-120. The ratio at IP is larger. If the respiratory effect is more than 120, in order to reduce the respiratory effect, the quantization step length of the I frame and the quantization step length of the current frame can be limited in a higher range by increasing the maximum quantization step length of the I frame and the minimum quantization step length of the I frame, so that the code rate of the I frame is reduced, the data volume difference between the I frame and the P frame is reduced, and the respiratory effect is reduced. And obtaining a third quantization parameter after the second quantization parameter is adjusted.
And if the IP ratio is smaller than the minimum value of the setting range corresponding to the third setting range, reducing the maximum quantization step length and the minimum quantization step length of the key frame. When the IP is smaller, for example, smaller than 60, the respiratory effect is weaker, and the quantization step length of the I frame and the quantization step length of the current frame can be limited in a higher range by reducing the maximum quantization step length and the minimum quantization step length of the key frame, so that the code rate of the I frame is improved, and the image quality is improved. And obtaining a third quantization parameter after the second quantization parameter is adjusted.
If the IP ratio is within the third setting range, the second quantization parameter may be directly determined as the third quantization parameter without adjusting the quantization parameter in this step.
And S600, calculating the peak signal-to-noise ratio of the last frame of the target frame, and adjusting the third quantization parameter according to the fact that the peak signal-to-noise ratio is smaller than a threshold value to obtain a fourth quantization parameter.
In some embodiments, PSNR (PEAK SIGNAL-to-noise ratio) indicates the similarity between the image frames before and after encoding, with greater PSNR indicating higher similarity and lesser PSNR indicating lower similarity with a mosaic effect.
In some embodiments, the PSNR may be compared with a threshold to obtain a comparison result, and whether or not to adjust the quantization parameter and how to adjust the quantization parameter may be determined according to the comparison result.
The threshold may be, for example, 25 db. When the PSNR is smaller, for example, less than 25 db, the quantization step size of the I frame and the quantization step size of the current frame may be limited to a higher range by increasing the maximum quantization step size of the I frame, the minimum quantization step size of the I frame, the maximum quantization step size of the current frame, and the minimum quantization step size of the current frame, thereby improving the PSNR. And obtaining a fourth quantization parameter after the third quantization parameter is adjusted.
When the PSNR is greater than or equal to the threshold value, the third quantization parameter may be directly determined as the fourth quantization parameter without adjusting the quantization parameter in this step.
The above-mentioned steps S300-S600 show the method for adjusting the quantization parameter, wherein the adjustment steps of the quantization parameter in these steps may be configured to be the same or different, and may be specifically configured according to actual needs.
And step S700, encoding the target frame according to the fourth quantization parameter.
In some embodiments, after the fourth quantization parameter is obtained, the target frame may be encoded according to the fourth quantization parameter.
In some embodiments, after encoding is completed for one target frame, the content complexity of that target frame may be calculated for quantization parameter adjustment when encoding the next target frame.
In some embodiments, the first setting range, the second setting range, the third setting range and the threshold in the above steps may be set according to actual needs, and the embodiments of the present application are not limited in particular.
According to the video coding method, after the coding of the first frame is completed, the self-adaptive regulation process of the quantization parameter is started, and when the target frame after the first frame is coded, the quantization parameter is automatically regulated according to the content complexity, the motion amplitude, the IP ratio and the PSNR, so that the quantization parameter can be automatically adapted to various scenes, different video contents can be adapted, the flexibility is high, the code rate and the image quality of video coding are considered, a better video coding effect is achieved, a user does not need to configure various quantization parameters, the complexity of user configuration is reduced, the coding effects such as mosaic effect, respiratory effect and coding block effect caused by unsuitable quantization parameters configured by the user are effectively avoided, and the user experience is improved.
The embodiment of the application also provides a video coding device, referring to fig. 2, which may include an input module, a first frame coding module and a target frame coding module.
The input module is used for acquiring image frames in a target video, wherein the target video comprises a plurality of continuous image frames;
The first frame coding module is connected with the input module and is used for determining a quantization parameter of the first frame according to the resolution and the code rate of the target video when the image frame is the first frame, and coding the first frame according to the quantization parameter of the first frame;
The target frame coding module is connected with the input module and comprises:
The content complexity analysis unit is used for calculating the content complexity of the last frame of the target frame after encoding when the image frame is the target frame after the first frame, and adjusting the quantization parameter of the last frame according to the comparison result of the content complexity and the first setting range to obtain a first quantization parameter;
The motion amplitude analysis unit is connected with the content complexity unit and is used for calculating the motion amplitude between the target frame and the frame above the target frame, and adjusting the first quantization parameter according to the comparison result of the motion amplitude and the second set range to obtain a second quantization parameter;
The ratio analysis unit is connected with the motion amplitude analysis unit and is used for calculating the ratio of the decoded key frame to the forward predictive coding frame, and adjusting the second quantization parameter according to the comparison result of the ratio and a third set range to obtain a third quantization parameter;
the peak signal-to-noise ratio unit is connected with the ratio analysis unit and is used for calculating the peak signal-to-noise ratio of the last frame of the target frame, and adjusting the third quantization parameter according to the fact that the peak signal-to-noise ratio is smaller than a threshold value to obtain a fourth quantization parameter;
and the encoding unit is used for encoding the target frame according to the fourth quantization parameter.
In some embodiments, the working process and technical effects of each module of the video encoding apparatus may refer to the flow description in the video encoding method, which is not described herein.
The above-provided specific embodiments are only examples under the general inventive concept, and do not limit the scope of the protection set of the present application. Any other embodiments extended according to the solution of the application without inventive effort fall within the scope of the protection set-up of the application, for a person skilled in the art.