Video coding method, electronic equipment and computer readable storage medium
By adjusting the quantization parameters of the video frame sequence periodically, combining the actual motion conditions and the encoded bit rate, and dynamically adjusting the quantization parameters of key frames and non-key frames, the problem of balancing bit rate and image quality in the existing technology is solved, and efficient compression and image quality assurance of video encoding are achieved.
Patent Information
- Application Number
- CN202510644822.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-19
- Publication Date
- 2025-09-05
AI Technical Summary
In the existing technology, it is difficult for video encoding methods to reduce the bit rate while ensuring image quality, and it is impossible to strike a balance between the two.
By adjusting the quantization parameters of the video frame sequence cycle by cycle, based on the bit rate cap and basic quantization parameters of the previous cycle, combined with the actual motion of the current frame and the encoded bit rate, the quantization parameters of key frames and non-key frames are dynamically adjusted to achieve higher-precision encoding.
During the encoding process, the video frames can be compressed as much as possible while ensuring the image quality, the bit rate can be reduced, and a balance between bit rate and image quality can be achieved.
Smart Images

Figure CN120602649A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of video encoding technology, and in particular to a video encoding method, an electronic device, and a computer-readable storage medium. Background Art
[0002] The raw data of a video frame sequence is relatively large, and encoding is typically required to compress the video frames, thereby reducing the bitrate of the encoded video frame sequence. Conventional techniques typically set uniform encoding parameters for the video frame sequence. However, attempting to compress the video frames as much as possible to reduce the bitrate does not guarantee image quality, while maintaining image quality requires a higher bitrate. Therefore, achieving a balanced bitrate and image quality is difficult. Therefore, how to reduce the bitrate while maintaining image quality has become a pressing issue. Summary of the Invention
[0003] The main technical problem solved by this application is to provide a video encoding method, electronic device and computer-readable storage medium, which can reduce the bit rate and ensure the image quality.
[0004] To solve the above technical problems, the first aspect of the present application provides a video encoding method, including: obtaining a current period of a video frame sequence, and determining a previous period matching the current period; wherein the previous period corresponds to a bit rate cap and a basic quantization parameter, and the basic quantization parameter is the quantization parameter of the first non-key frame in the corresponding period; based on the bit rate cap and the basic quantization parameter of the previous period, determining the basic quantization parameter of the current period, and traversing the video frames in the current period; in response to the current frame being a key frame, obtaining the actual motion of the video frame in the previous period and the quantization parameter of the previous key frame, and determining the quantization parameter of the current frame based on the actual motion and the quantization parameter of the previous key frame and encoding it; in response to the current frame being a non-key frame, obtaining the bit rate cap and the encoded bit rate of the current period, and determining the quantization parameter of the current frame based on the bit rate cap and the encoded bit rate of the current period and encoding it.
[0005] To solve the above technical problems, the second aspect of the present application provides an electronic device, which includes: a memory and a processor coupled to each other, wherein the memory stores program data, and the processor calls the program data to execute the method described in the first aspect above.
[0006] In order to solve the above technical problems, the third aspect of the present application provides a computer-readable storage medium on which program data is stored. When the program data is executed by a processor, the method described in the first aspect is implemented.
[0007] The above scheme obtains the current cycle of the video frame sequence, and determines that the cycle adjacent to the current cycle is the previous cycle, wherein the previous cycle corresponds to the bit rate upper limit and the first non-critical quantization parameter in the cycle, and the quantization parameter of the first non-critical frame corresponds to the basic quantization parameter. Based on the bit rate upper limit and the basic quantization parameter of the previous cycle, the basic quantization parameter of the current cycle is adjusted to obtain the basic quantization parameter of the current cycle, thereby adjusting the basic quantization parameter cycle by cycle, and then traversing the video frames in the current cycle starting from the first video frame in the current cycle. When the current frame is a key frame, the actual motion of the video frame in the previous cycle and the quantization parameter of the previous key frame are obtained, and based on the actual motion of the video frame in the previous cycle and the quantization parameter of the previous key frame, the quantization parameter of the current key frame is adjusted to obtain the quantization parameter of the current frame and encode it, thereby realizing the quantization parameter adjustment between the frame levels corresponding to the key frames. When the current frame is a non-key frame, the bit rate upper limit and the encoded bit rate of the current cycle are obtained, and the quantization parameters of the current non-key frame are adjusted based on the bit rate upper limit and the encoded bit rate of the current cycle, and the quantization parameters of the current frame are obtained and encoded, thereby realizing the quantization parameter adjustment between the frame levels corresponding to the non-key frames. Therefore, the present application can adjust the basic quantization parameters cycle by cycle, and realize the quantization parameter adjustment between the frame levels corresponding to the key frames and non-key frames, so that the quantization parameters of the current frame are adjusted with higher precision based on the encoded video frames during the encoding process, ensuring the image quality of the video frames while compressing the video frames as much as possible, thereby reducing the bit rate and ensuring the image quality. BRIEF DESCRIPTION OF THE DRAWINGS
[0008] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for describing the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without inventive efforts. Among them:
[0009] Figure 1 This is a flow chart of an embodiment of the video encoding method of the present application;
[0010] Figure 2 This is a flowchart of another embodiment of the video encoding method of the present application;
[0011] Figure 3 This is a schematic structural diagram of an embodiment of the electronic device of the present application;
[0012] Figure 4 It is a structural diagram of an embodiment of a computer-readable storage medium of the present application. DETAILED DESCRIPTION
[0013] The following will be combined with the drawings in the embodiments of this application to clearly and completely describe the technical solutions in the embodiments of this application. Obviously, the embodiments described are only part of the embodiments of this application, not all of them, and different implementation methods can be adaptively combined. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.
[0014] In this article, the terms "system" and "network" are often used interchangeably. The term "and / or" in this article is simply a description of the association relationship between related objects, indicating that three relationships can exist. For example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / " in this article generally indicates that the related objects before and after are in an "or" relationship. In addition, "many" in this article means two or more than two, and "positive correlation" and "negative correlation" in this article represent the overall trend of the change process. During the change process, there may be some intervals that do not change or are opposite to the overall trend.
[0015] The video encoding method provided in the present application is used to encode a video frame sequence, thereby reducing the bit rate of the encoded video frame sequence and ensuring image quality. Its corresponding execution subject is a processing unit capable of performing data processing.
[0016] The following is an explanation of some of the technical terms used in this article to facilitate understanding by those skilled in the art. Bit rate refers to the number of data bits transmitted per unit time during data transmission. The quantization parameter (QP) controls the amount of data lost during lossy quantization processing. The quantization parameter reflects the compression of spatial details. The smaller the quantization parameter, the finer the quantization and the higher the image quality, but the higher the bit rate. The larger the quantization parameter, the coarser the quantization and the lower the image quality, but the bit rate. Motion refers to the changes between two adjacent video frames. Changes are motion, and no changes are still.
[0017] See also Figure 1 , Figure 1 1 is a flow chart of an embodiment of a video encoding method of the present application, the method comprising:
[0018] S101: Obtain a current cycle of a video frame sequence and determine a previous cycle that matches the current cycle; wherein the previous cycle corresponds to a bit rate cap and a basic quantization parameter, and the basic quantization parameter is a quantization parameter of the first non-key frame in the corresponding cycle.
[0019] Specifically, the current cycle of the video frame sequence is obtained, and the cycle adjacent to the current cycle is determined to be the previous cycle, wherein the previous cycle corresponds to the bit rate upper limit and the first non-critical quantization parameter in the cycle, and the quantization parameter of the first non-critical frame corresponds to the basic quantization parameter.
[0020] It should be noted that a period in a video frame sequence includes at least one Group of Pictures (GOP), that is, a single period must include key frames and non-key frames, and the basic quantization parameter corresponding to each period corresponds to the quantization parameter of the first non-key frame in the period.
[0021] In one embodiment, the video frame sequence is divided according to Group of Pictures (GOP), and when a current period in the video frame sequence is obtained, the GOP before the current period is used as a period before the current period.
[0022] In one embodiment, the video frame sequence is pre-configured with an encoding period, wherein the number of video frames included in the period is adapted to the processing capability of the terminal, and when the current period in the video frame sequence is obtained, the period whose timing has been encoded before the current period is used as the previous period.
[0023] It is understandable that the video frame sequence is initialized before encoding, and after initialization, a bit rate upper limit and basic quantization parameters are set for the first cycle.
[0024] S102: Based on the bit rate upper limit and the basic quantization parameter of the previous cycle, determine the basic quantization parameter of the current cycle, and traverse the video frames in the current cycle.
[0025] Specifically, based on the bit rate upper limit and the basic quantization parameter of the previous cycle, the basic quantization parameter of the current cycle is adjusted to obtain the basic quantization parameter of the current cycle.
[0026] It is understandable that the basic quantization parameter is adjusted period by period, and then the video frames in the current period are traversed starting from the first video frame in the current period.
[0027] In one embodiment, an actual encoding bit rate of a previous cycle is obtained, and a basic quantization parameter of the previous cycle is adjusted based on the bit rate cap and the actual encoding bit rate of the previous cycle to obtain the basic quantization parameter of the current cycle. When the actual encoding bit rate is less than the bit rate cap, the basic quantization parameter of the current cycle is less than the basic quantization parameter of the previous cycle; and when the actual encoding bit rate is greater than or equal to the bit rate cap, the basic quantization parameter of the current cycle is greater than the basic quantization parameter of the previous cycle.
[0028] In one embodiment, the encoding bit rates of all key frames in the previous cycle and the difference between the encoding bit rates of every two adjacent non-key frames are obtained. Based on the bit rate cap of the previous cycle, the encoding bit rates of all key frames, and the difference between the encoding bit rates of every two adjacent non-key frames, the basic quantization parameter of the previous cycle is adjusted to obtain the basic quantization parameter of the current cycle. The subtraction of the encoding bit rates of all key frames in the previous cycle from the bit rate cap corresponds to a statistical difference. The smaller the statistical difference and the encoding bit rate difference, the smaller the basic quantization parameter of the current frame cycle. The larger the statistical difference and the encoding bit rate difference, the larger the basic quantization parameter of the current frame cycle.
[0029] Optionally, when the terminal supports bit rate adjustment, the bit rate cap of the current period can be updated after adjustment based on the bit rate cap of the previous period. The bit rate cap of the current period can be obtained after adjustment based on the actual encoding bit rate and the bit rate cap of the previous period.
[0030] S103: In response to the current frame being a key frame, obtaining the actual motion of the video frame in the previous period and the quantization parameter of the previous key frame, determining the quantization parameter of the current frame based on the actual motion and the quantization parameter of the previous key frame, and encoding the current frame.
[0031] Specifically, when the current frame is a key frame, the actual motion of the video frame in the previous period and the quantization parameters of the previous key frame are obtained. Based on the actual motion of the video frame in the previous period and the quantization parameters of the previous key frame, the quantization parameters of the current key frame are adjusted to obtain the quantization parameters of the current frame and encode them, thereby realizing quantization parameter adjustment between frame levels corresponding to key frames.
[0032] It should be noted that the video frame sequence is initialized before encoding. After initialization, a quantization parameter is set for the first key frame, and each period corresponds to the total number of frames in the period that all video frames match.
[0033] In one embodiment, the actual motion of a video frame in a previous cycle and the quantization parameter of a previous key frame are obtained, a statistical number of instances in which the actual motion in the previous cycle is motion is determined, and the quantization parameter of the previous key frame is adjusted based on the statistical number to obtain the quantization parameter of the first key frame in the current cycle. When traversing to other key frames in the current cycle, the statistical number of instances in which the actual motion in the total number of frames in the cycle before the key frame is motion is determined, and the quantization parameter of the previous key frame is adjusted based on the statistical number to obtain the quantization parameter of the corresponding key frame. The larger the statistical number, the larger the quantization parameter of the key frame, and the smaller the statistical number, the smaller the quantization parameter of the key frame.
[0034] In one embodiment, the actual motion of a video frame in a previous cycle and the quantization parameter of the previous key frame are obtained, the number of still frames corresponding to video frames with actual still motion and continuous motion in the previous cycle is determined, and the quantization parameter of the previous key frame is adjusted based on the number of still frames to obtain the quantization parameter of the first key frame in the current cycle. When traversing to other key frames in the current cycle, the number of still frames corresponding to video frames with actual still motion in the total number of frames in the cycle before the key frame is determined, and the quantization parameter of the previous key frame is adjusted based on the number of still frames to obtain the quantization parameter of the corresponding key frame. The greater the number of still frames, the smaller the quantization parameter of the key frame, and the smaller the number of still frames, the larger the quantization parameter of the key frame.
[0035] In one embodiment, the actual motion of a video frame in a previous cycle and the quantization parameter of a previous key frame are obtained to determine the actual motion of the current frame. Based on the actual motion of the current frame and the actual motion of the video frame in the previous cycle, as well as the motion difference between the actual motion of the current frame and the actual motion of the previous key frame, the quantization parameter of the previous key frame is adjusted to obtain the quantization parameter of the current frame. The greater the number of frames with actual motion and the motion difference in the previous cycle, the greater the quantization parameter, and the smaller the number of frames with actual motion and the motion difference in the previous cycle, the smaller the quantization parameter.
[0036] S104: In response to the current frame being a non-key frame, obtaining a bit rate upper limit and an encoded bit rate of the current period, determining a quantization parameter of the current frame based on the bit rate upper limit and the encoded bit rate of the current period, and encoding the current frame.
[0037] Specifically, when the current frame is a non-key frame, the bit rate upper limit and the encoded bit rate of the current period are obtained, and the quantization parameters of the current non-key frame are adjusted based on the bit rate upper limit and the encoded bit rate of the current period. The quantization parameters of the current frame are obtained and encoded, thereby realizing quantization parameter adjustment between frame levels corresponding to non-key frames.
[0038] It can be understood that the quantization parameter corresponding to the first non-key frame of the current period is the basic quantization parameter of the current period, and the adjustment of the quantization parameter of the non-key frame starts from the second non-key frame of the current period.
[0039] In one embodiment, the bit rate upper limit and the encoded bit rate of the current period are obtained, and the bit rate threshold of the current period is determined based on the bit rate upper limit and the encoded bit rate of the current period. When the encoded bit rate of the current period exceeds the bit rate threshold, the quantization parameter is increased based on the quantization parameter of the previous non-key frame to obtain the quantization parameter of the current frame. When the encoded bit rate of the current period does not exceed the bit rate threshold, the quantization parameter is decreased based on the quantization parameter of the previous non-key frame to obtain the quantization parameter of the current frame.
[0040] In one embodiment, the bit rate upper limit and the encoded bit rate of the current period are obtained, and the remaining bit rate of the current period is determined based on the bit rate upper limit and the encoded bit rate of the current period. Based on the actual motion of the current frame and the remaining bit rate, the quantization parameter of the previous key frame is adjusted to obtain the quantization parameter of the current frame.
[0041] It can be understood that each video frame is traversed in turn in the current cycle until the quantization parameters of the key frames and non-key frames in the current cycle are adjusted, so that the basic quantization parameters are adjusted cycle by cycle, and the quantization parameter adjustment is realized between the frame levels corresponding to the key frames and non-key frames, so that the quantization parameters of the current frame are adjusted with higher precision based on the encoded video frames during the encoding process, ensuring the image quality of the video frames while compressing the video frames as much as possible.
[0042] The above scheme obtains the current cycle of the video frame sequence, and determines that the cycle adjacent to the current cycle is the previous cycle, wherein the previous cycle corresponds to the bit rate upper limit and the first non-critical quantization parameter in the cycle, and the quantization parameter of the first non-critical frame corresponds to the basic quantization parameter. Based on the bit rate upper limit and the basic quantization parameter of the previous cycle, the basic quantization parameter of the current cycle is adjusted to obtain the basic quantization parameter of the current cycle, thereby adjusting the basic quantization parameter cycle by cycle, and then traversing the video frames in the current cycle starting from the first video frame in the current cycle. When the current frame is a key frame, the actual motion of the video frame in the previous cycle and the quantization parameter of the previous key frame are obtained, and based on the actual motion of the video frame in the previous cycle and the quantization parameter of the previous key frame, the quantization parameter of the current key frame is adjusted to obtain the quantization parameter of the current frame and encode it, thereby realizing the quantization parameter adjustment between the frame levels corresponding to the key frames. When the current frame is a non-key frame, the bit rate upper limit and the encoded bit rate of the current cycle are obtained, and the quantization parameters of the current non-key frame are adjusted based on the bit rate upper limit and the encoded bit rate of the current cycle, and the quantization parameters of the current frame are obtained and encoded, thereby realizing the quantization parameter adjustment between the frame levels corresponding to the non-key frames. Therefore, the present application can adjust the basic quantization parameters cycle by cycle, and realize the quantization parameter adjustment between the frame levels corresponding to the key frames and non-key frames, so that the quantization parameters of the current frame are adjusted with higher precision based on the encoded video frames during the encoding process, ensuring the image quality of the video frames while compressing the video frames as much as possible, thereby reducing the bit rate and ensuring the image quality.
[0043] See also Figure 2 , Figure 2 1 is a flow chart of another embodiment of the video encoding method of the present application, the method comprising:
[0044] S201: Obtain a current cycle of a video frame sequence and determine a previous cycle that matches the current cycle; wherein the previous cycle corresponds to a bit rate cap and a basic quantization parameter, and the basic quantization parameter is a quantization parameter of the first non-key frame in the corresponding cycle.
[0045] Specifically, the current cycle of the video frame sequence is obtained, and the cycle immediately preceding the current cycle is determined as the previous cycle. The video frame sequence is initialized to obtain the bitrate cap, quantization parameter, and motion gain of the first cycle. The previous cycle corresponds to the bitrate cap and the first non-critical quantization parameter in the cycle.
[0046] It should be noted that the initialization process of the video frame sequence is based on the following steps: obtaining the quality level and bit rate preset upper limit matching the video frame sequence, and determining the bit rate upper limit of the first period based on the quality level and bit rate preset upper limit; determining the pixel depth corresponding to the video frame sequence based on the bit rate upper limit of the first period and the video frame sequence and its corresponding frame rate, and determining the basic quantization parameter and key frame quantization parameter of the first period based on the pixel depth; determining the motion gain corresponding to the video frame sequence based on the quality level and pixel depth; wherein the motion gain is used to adjust the motion between adjacent video frames.
[0047] Specifically, the quality level and bit rate preset upper limit of the video frame sequence matching are obtained, and based on the quality level and bit rate preset upper limit, the bit rate upper limit set for the first period is mapped to determine the bit rate upper limit of the first period, so as to set an adaptive bit rate upper limit for the first period according to the pre-set parameter settings.
[0048] Furthermore, based on the bitrate cap of the first cycle, the image parameters of the video frame sequence, and the frame rate corresponding to the video frame sequence, the pixel depth corresponding to the video frame sequence is solved to obtain the pixel depth corresponding to the video frame sequence. The image parameters include image width and image height. Based on the pixel depth, the quantization parameters of the first cycle are mapped to obtain the base quantization parameters of the first cycle and the quantization parameters of the key frames, thereby determining the initial quantization parameters that match the video frame sequence.
[0049] Furthermore, based on the quality level and pixel depth, the motion gain of the video frame sequence is mapped to obtain the motion gain corresponding to the video frame sequence, so that the motion gain can compensate for the motion between adjacent video frames, so that the compensated result has a higher adaptability to the required quality level and pixel depth according to the video frame sequence.
[0050] It is understandable that the quality level Level of the video frame sequence encoding and the preset upper limit of the bit rate R are obtained. Max , calculate the bit rate cap for the first cycle The calculation formula is:
[0051]
[0052] Among them, f1(x) is the Max , Level) to The mapping function f1(x) can be a linear relationship or a nonlinear relationship.
[0053] Furthermore, through the bit rate cap The pixel depth BPP of the video frame sequence is calculated with the image width W, image height H, and frame rate fps. The calculation formula is:
[0054]
[0055] Calculate the initial quantization parameter Q of the current sequence by pixel depth BPP Initial , the calculation formula is:
[0056] Q Initial =f2(BPP)(3)
[0057] Where f2(x) is the distance from (BPP) to Q Initial The mapping function f2(x) can be a linear or nonlinear relationship. Furthermore, the initial quantization parameter can be obtained based on other factors, including but not limited to the quality level and the coding standard. The initial quantization parameter includes the base quantization parameter of the first cycle and the quantization parameter of the key frame.
[0058] Furthermore, the initialization motion gain parameter Gain of the current sequence is calculated by the quality level Level and pixel depth BPP Motion , the calculation formula is:
[0059] Gain Motion =f3(BPP, Level)(4)
[0060] Among them, f3(x) is from (BPP, Level) to Gain Motion The mapping function f3(x) can be a linear relationship or a nonlinear relationship.
[0061] When calculating the motion of a video frame, the motion gain parameter Gain is required. Motion Motion New The update formula is:
[0062] Motion New =f4(Motion, Gain Motion )(5)
[0063] Among them, f4(x) is the value of (Motion, GainMotion ) to Motion New The mapping function f4(x) can be a linear relationship or a nonlinear relationship.
[0064] For the sake of convenience, this application provides a specific implementation scenario as a conventional case to assist in understanding the settings of various parameters, but all parameters involved in this application can be customized based on actual scenarios, and this application does not impose specific restrictions on this. Max Calculate the current encoding bitrate upper limit The calculation formula can be, but is not limited to, the following forms:
[0065] Among them, Level Max The maximum value of Level.
[0066] Furthermore, the initial quantization parameter Q of the current sequence is calculated by the pixel depth BPP and other factors including but not limited to: quality level Level, coding standard Initial , the calculation formula can be but not limited to the following forms:
[0067] The initial quantization parameter can provide guidance for the basic quantization parameter of the first cycle and the quantization parameter of the key frame.
[0068] Furthermore, the initialization motion gain parameter Gain of the current sequence is calculated by the quality level Level and pixel depth BPP Motion , the calculation formula can be but not limited to the following forms:
[0069] Among them, the motion gain parameter is used to compensate for the motion. Therefore, when calculating the motion of the current frame, it is necessary to use the motion gain parameter Gain Motiom Motion New The calculation formula can be, but is not limited to, the following forms:
[0070] Motion New =Motion+Gain Motion , among which, Motion New The updated movement status.
[0071] S202: Determine a basic quantization parameter of a current cycle based on the bit rate upper limit and the basic quantization parameter of a previous cycle.
[0072] Specifically, based on the bit rate upper limit and the basic quantization parameter of the previous cycle, the basic quantization parameter of the current cycle is adjusted to obtain the basic quantization parameter of the current cycle.
[0073] In one embodiment, determining the basic quantization parameter of the current cycle based on the bit rate upper limit and the basic quantization parameter of the previous cycle includes: obtaining a quality level for matching the video frame sequence, and determining the bit rate threshold of the previous cycle based on the quality level and the bit rate upper limit of the previous cycle; in response to the actual encoding bit rate of the previous cycle exceeding the bit rate threshold of the previous cycle, determining the quantization parameter offset value of the current cycle based on the actual encoding bit rate of the previous cycle and the bit rate threshold of the previous cycle; in response to the actual encoding bit rate of the previous cycle not exceeding the bit rate threshold of the previous cycle, determining the predicted motion of the video frame in the current cycle based on the actual motion of the video frame in the previous cycle, and determining the quantization parameter offset value of the current cycle based on the actual motion and the predicted motion; and determining the basic quantization parameter of the current cycle based on the basic quantization parameter of the previous cycle and the quantization parameter offset value of the current cycle.
[0074] Specifically, the quality level of the video frame sequence match is obtained, and based on the quality level and the bit rate upper limit of the previous period, the bit rate threshold of the previous period is mapped to obtain the bit rate threshold of the previous period, the actual encoding bit rate of the previous period is obtained, and it is determined whether the actual encoding bit rate of the previous period exceeds the bit rate threshold of the previous period.
[0075] It should be noted that when the actual encoding rate of the previous period exceeds the rate threshold of the previous period, the quantization parameter offset value of the current period is mapped based on the actual encoding rate of the previous period and the rate threshold of the previous period, and the quantization parameter offset value of the current period is determined. Therefore, when the rate consumed in the previous period exceeds the rate threshold, the quantization parameter of the current period is adjusted by the quantization parameter offset value.
[0076] In addition, when the actual encoding bit rate of the previous cycle does not exceed the bit rate threshold of the previous cycle, the actual motion of the video frame in the previous cycle is obtained, and based on the actual motion of the video frame in the previous cycle, the motion of the video frame in the current cycle is predicted to obtain the predicted motion situation. The quantization parameter offset value of the current cycle is mapped based on the actual motion situation and the predicted motion situation to determine the quantization parameter offset value of the current cycle, so that when the bit rate consumed in the previous cycle does not exceed the bit rate threshold, the quantization parameter of the current cycle is adjusted by the quantization parameter offset value.
[0077] Furthermore, the basic quantization parameter of the previous cycle is adjusted using the quantization parameter offset value of the current cycle to obtain the basic quantization parameter of the current cycle.
[0078] It can be understood that when the actual encoding rate of the current period does not exceed the rate threshold of the previous period, the quantization parameter offset value of the current period is usually used to reduce the basic quantization parameter of the current period. When the actual encoding rate of the current period exceeds the rate threshold of the previous period, the quantization parameter offset value of the current period is usually used to increase the basic quantization parameter of the current period.
[0079] Optionally, after determining the basic quantization parameters of the current cycle based on the bit rate cap and basic quantization parameters of the previous cycle, it also includes: obtaining the actual motion conditions of the video frames in the previous cycle, and the predicted motion conditions of the video frames in the current cycle predicted based on the actual motion conditions; determining the bit rate cap of the current cycle based on the actual motion conditions, the predicted motion conditions and the bit rate cap of the previous cycle.
[0080] It is understandable that according to the quality level Level and the upper limit of the bit rate of the previous cycle Adjust and calculate the bit rate threshold T1 of the previous cycle. The calculation formula is:
[0081]
[0082] Among them, f5(x) is The mapping function to T1, f5(x), can be a linear relationship or a nonlinear relationship.
[0083] Furthermore, if the actual encoding rate of the previous cycle is Based on the actual encoding rate of the previous cycle and the rate threshold of the previous cycle, the quantization parameter offset value of the current cycle is determined. According to the upper threshold T1, the quantization parameter offset value of the current cycle is calculated. The calculation formula is:
[0084]
[0085] Among them, f6(x) is arrive The mapping function f6(x) can be a linear relationship or a nonlinear relationship.
[0086] In addition, if the actual encoding rate of the previous cycle Based on the actual motion of the video frame in the previous cycle, the predicted motion of the video frame in the current cycle is determined, and based on the actual motion and the predicted motion, the quantization parameter offset value of the current cycle is determined. According to the motion of each frame in the previous cycle, the larger the value, the more intense the motion. The actual motion of the previous cycle is calculated. The calculation formula is:
[0087]
[0088] Among them, Framenum Short is the total number of all video frames in the previous cycle, and i represents the i-th frame.
[0089] Furthermore, the predicted motion of the current cycle is predicted based on the motion of part or all of the frames in the previous cycle. The calculation formula is:
[0090]
[0091] Among them, f7(x) is the distance from Motion to The mapping function f7(x) can be a linear relationship or a nonlinear relationship.
[0092] Furthermore, according to the predicted motion of the current cycle Compared with the actual movement of the previous cycle Calculate the quantization parameter offset value of the current cycle The calculation formula is:
[0093]
[0094] Among them, f8(x) is arrive The mapping function f8(x) can be a linear relationship or a nonlinear relationship.
[0095] It is understandable that according to the calculated quantization parameter offset value The quantization parameter of the previous cycle Adjust the basic quantitative parameters of the current cycle The calculation formula is:
[0096]
[0097] Among them, f9(x) is arrive The mapping function f9(x) can be a linear relationship or a nonlinear relationship.
[0098] For the sake of convenience, this application provides a specific implementation scenario as a conventional case to assist in understanding the setting of various parameters, but all parameters involved in this application can be customized based on actual scenarios, and this application does not impose specific restrictions on this. Adjust the calculation upper threshold T1. The calculation formula can be, but is not limited to, the following:
[0099] The value range of Level can be [0, 12]. Then make adjustment in case 1. If the actual encoding rate of the previous cycle is Then make adjustments according to situation 2.
[0100] Case 1: According to the upper threshold T1, the actual encoding rate of the previous cycle Calculate the quantization parameter offset value of the current cycle The calculation formula can be, but is not limited to, the following forms: Among them, 10 is the set coefficient, which can be other values.
[0101] Case 2: Based on the motion of each frame in the previous cycle, predict the motion of the current cycle The calculation formula can be, but is not limited to, the following forms:
[0102] in, The frame weight of the i-th frame. The larger the interval between the i-th frame and the current frame, the smaller the value. i Indicates the motion of the i-th frame, Framenum Short is the total number of all video frames in the previous cycle, Indicates the average motion.
[0103] Furthermore, according to the predicted motion of the current cycle Compared with the actual movement of the previous cycle Calculate the quantization parameter offset value of the current cycle The calculation formula can be, but is not limited to, the following forms:
[0104] Among them, n is divided into the following cases based on the motion of two cycles:
[0105] Where abs represents absolute value.
[0106] It is understandable that according to the calculated quantization parameter offset value The quantization parameter of the previous cycle Adjust the basic quantitative parameters of the current cycle The calculation formula can be, but is not limited to, the following forms:
[0107] Among them, the basic quantitative parameters of the current cycle Get adjusted.
[0108] S203: Acquire the actual motion conditions of the video frames in the previous cycle and the predicted motion conditions of the video frames in the current cycle predicted based on the actual motion conditions.
[0109] Specifically, the actual motion situation of the video frame in the previous cycle and the predicted motion situation of the video frame in the current cycle are obtained, wherein the predicted motion situation is predicted based on the actual motion situation of the video frame in the previous cycle.
[0110] It can be understood that the predicted motion situation is determined at least based on the motion situation of the video frames in the previous cycle. In some implementation scenarios, it can also be determined based on the motion situation of the video frames in multiple cycles before the current cycle. The bit rate to be consumed by the video frames in the current cycle can be estimated by predicting the motion situation, so as to upper limit the bit rate of the current cycle.
[0111] In some implementation scenarios, the predicted motion situation is obtained based on the following steps: obtaining the frame weight corresponding to each video frame in the previous cycle and the average motion situation corresponding to all video frames; wherein the frame weight is negatively correlated with the frame interval between the corresponding video frame and the current cycle; based on the actual motion situation and frame weight corresponding to each video frame in the previous cycle, as well as the average motion situation, determining the predicted motion situation of the video frame in the current cycle.
[0112] Specifically, the frame weight corresponding to each video frame in the previous cycle and the average motion of all video frames in the previous cycle are obtained. The frame weight increases as the frame interval of the video frame relative to the current cycle becomes smaller, thereby focusing on video frames close to the current cycle. Based on the actual motion and frame weight corresponding to each video frame in the previous cycle, as well as the average motion, the motion of the current cycle is predicted to obtain the predicted motion of the video frames in the current cycle. The predicted motion focuses on referring to similar video frames and integrates the average motion of the previous cycle, thereby improving the accuracy of the predicted motion.
[0113] S204: Based on the actual motion situation, the predicted motion situation and the bit rate upper limit of the previous period, determine the bit rate upper limit of the current period, and traverse the video frames in the current period.
[0114] Specifically, based on the actual motion situation, the predicted motion situation and the bit rate cap of the previous period, the bit rate cap of the current period is adjusted to obtain the updated bit rate cap of the current period, so that during the encoding process, the bit rate cap of the period to be encoded can be adjusted in time according to the motion situation of the adjacent periods, thereby improving the encoding accuracy.
[0115] In some implementation scenarios, the motion change difference between the predicted motion situation of the current period and the actual motion situation of the previous period is obtained, and the bit rate adjustment value is determined based on the motion change difference. The bit rate adjustment value is superimposed on the bit rate upper limit of the previous period to obtain the bit rate upper limit of the current period.
[0116] In some implementation scenarios, quantized values corresponding to the predicted motion conditions and the actual motion conditions are obtained, and a rate adjustment ratio is determined based on the ratio between the quantized values corresponding to the predicted motion conditions and the actual motion conditions. The rate adjustment ratio is used to adjust the rate cap of the previous period to obtain the rate cap of the current period.
[0117] Optionally, based on the predicted motion of the current cycle Actual movement in the previous cycle The upper limit of the bit rate in the previous cycle Adjust the bitrate upper limit of the current period The calculation formula is:
[0118]
[0119] Among them, f 10 (x) is from arrive The mapping function, f 10 (x) can be a linear relationship or a nonlinear relationship.
[0120] For the sake of convenience, this application provides a specific implementation scenario as a conventional case to assist in understanding the setting of various parameters, but all parameters involved in this application can be customized based on actual scenarios, and this application does not impose specific restrictions on this. Actual movement in the previous cycle The upper limit of the bit rate in the previous cycle Adjust the bitrate upper limit of the current period The calculation formula can be, but is not limited to, the following forms:
[0121] Among them, the bit rate upper limit of the current cycle is adjusted.
[0122] S205: In response to the current frame being a key frame, obtaining the actual motion of the video frame in the previous period and the quantization parameter of the previous key frame, determining the quantization parameter of the current frame based on the actual motion and the quantization parameter of the previous key frame, and encoding the current frame.
[0123] Specifically, when the current frame is a key frame, the actual motion of the video frame in the previous cycle and the quantization parameters of the previous key frame are obtained. Based on the actual motion of the video frame in the previous cycle and the quantization parameters of the previous key frame, the quantization parameters of the current key frame are adjusted to obtain the quantization parameters of the current frame and encode them.
[0124] In one embodiment, the quantization parameter of the current frame is determined and encoded based on the actual motion situation and the quantization parameter of the previous key frame, including: in response to a video frame in the previous period including a video frame in which the actual motion situation is motion, based on the current frame, determining the quantization parameter offset value of the current frame; in response to the actual motion situation of all video frames in the previous period being still, based on at least part of the video frames before the current frame, determining the quantization parameter offset value of the current frame; based on the quantization parameter of the previous key frame and the quantization parameter offset value, determining and encoding the quantization parameter of the current frame.
[0125] Specifically, when the previous cycle includes a video frame whose actual motion condition is that there is motion, the quantization parameter offset value of the current frame is mapped based on the current frame itself to obtain the quantization parameter offset value of the current frame, so that when the previous cycle includes a video frame whose actual motion condition is that there is motion, a matching quantization parameter offset value can be generated based on the condition of the current frame to adapt to the scene with motion. When the actual motion conditions of all video frames in the previous cycle are static, the quantization parameter offset value of the current frame is mapped based on at least a portion of the video frames before the current frame to obtain the quantization parameter offset value of the current frame, so that when the actual motion conditions of all video frames in the previous cycle are static, a matching quantization parameter offset value can be generated based on at least a portion of the video frames before the current frame to adapt to the scene when the picture is static, thereby improving the accuracy of key frame encoding.
[0126] Furthermore, the quantization parameter of the previous key frame is adjusted using the quantization parameter offset value of the current frame to obtain the quantization parameter of the current frame, thereby encoding the current key frame based on the quantization parameter of the current frame.
[0127] It can be understood that when the current period includes a video frame in which actual motion exists, the quantization parameter offset value of the current frame is usually used to increase the quantization parameter of the current frame. When the actual motion conditions of all video frames in the current period are still, the quantization parameter offset value of the current frame is usually used to decrease the quantization parameter of the current frame.
[0128] In some implementation scenarios, based on the current frame, determining the quantization parameter offset value of the current frame includes: obtaining the actual motion of the current frame, and determining a proportional threshold based on the actual motion of the current frame; and determining the quantization parameter offset value of the current frame based on the proportional threshold and the ratio between the actual encoding bit rate of the previous period and the bit rate upper limit.
[0129] Specifically, the actual motion of the current frame is obtained, and based on the actual motion of the current frame, a proportional threshold is set for the ratio of the actual encoding rate of the previous period to the bit rate upper limit, and based on the proportional threshold and the ratio between the actual encoding rate of the previous period and the bit rate upper limit, the quantization parameter offset value of the current frame is mapped to obtain the quantization parameter offset value of the current frame, thereby improving the accuracy of the quantization parameter offset value of the current frame based on the actual motion of the current frame.
[0130] In some implementation scenarios, determining a quantization parameter offset value of the current frame based on the current frame includes: obtaining actual motion conditions of the current frame, and determining a quantization parameter offset value of the current frame based on the actual motion conditions of the current frame.
[0131] Specifically, the actual motion situation of the current frame is obtained, and the quantization parameters of the current frame are mapped based on the actual motion situation of the current frame, so as to efficiently determine the quantization parameter offset value of the current frame, so that when the actual motion situation of the video frame in the previous cycle is included as the presence of motion, a matching quantization parameter offset value can be generated based on the actual motion situation of the current frame to adapt to the scenario where motion exists.
[0132] In some implementation scenarios, a quantization parameter offset value of the current frame is determined based on at least a portion of the video frames before the current frame, including: determining the pixel depth of the previous key frame based on the frame rate corresponding to the previous key frame and the video frame sequence; obtaining the pixel depth corresponding to the video frame sequence, and determining a pixel depth threshold based on the pixel depth corresponding to the video frame sequence; and determining the quantization parameter offset value of the current frame based on the pixel depth of the previous key frame and the pixel depth threshold.
[0133] Specifically, the image parameters of the previous key frame and the frame rate corresponding to the video frame sequence are obtained, wherein the image parameters include image width and image height. Based on the frame rates corresponding to the previous key frame and the video frame sequence, the pixel depth of the previous key frame is mapped to obtain the pixel depth of the previous key frame, the pixel depth corresponding to the video frame sequence is obtained, and based on the pixel depth corresponding to the video frame sequence, a pixel depth threshold that matches the entire video frame sequence is determined. Based on the pixel depth of the previous key frame and the pixel depth threshold, the quantization parameter offset value of the current frame is mapped to obtain the quantization parameter offset value of the current frame, thereby performing high-precision calculations on the quantization parameter offset value based on the pixel depth of the key frame.
[0134] In some implementation scenarios, the quantization parameter offset value of the current frame is determined based on at least part of the video frames before the current frame, including: determining the number of continuous still periods before the current frame based on the actual motion conditions of the video frames in the previous period and other periods before that; and determining the quantization parameter offset value of the current frame based on the number of still periods.
[0135] Specifically, based on the actual motion of the video frames in the previous cycle and other cycles before it, the number of periods of continuous stillness of the video frames before the current cycle is counted to obtain the number of still periods. The quantization parameter offset value of the current frame is mapped based on the number of still periods to obtain the quantization parameter offset value of the current frame, thereby performing high-precision calculations on the quantization parameter offset value based on the number of continuous still periods.
[0136] It is understandable that the quantization parameter offset value of the current key frame (I frame) is adjusted according to the actual motion and encoding conditions of the video frame in the previous cycle. If the previous cycle was during motion, then adjust to situation 1. If the previous cycle was during stillness, then adjust to situation 2, as follows:
[0137] Case 1: When the previous cycle was exercise, you can use either of the following two solutions to adjust:
[0138] Solution 1: Motion based on the current frame I Calculate the threshold (T2, T3) using the following formula:
[0139] (T2, T3) = f 11 (Motion I )(13)
[0140] Among them, f 11 (x) is from Motion I Mapping function to (T2, T3), f 11 (x) can be a linear relationship or a nonlinear relationship.
[0141] Furthermore, according to the actual encoding rate of the previous cycle The upper limit of the bit rate of the previous periodizer Threshold (T2, T3), calculate the quantization parameter offset value of the current frame The calculation formula is:
[0142]
[0143] Among them, f 12 (x) is from arrive The mapping function, f 12 (x) can be a linear relationship or a nonlinear relationship.
[0144] Solution 2: Motion based on the current frame I , calculate the quantization parameter offset value of the current frame The calculation formula is:
[0145]
[0146] Among them, f 13 (x) is from Motion I arrive The mapping function, f 13 (x) can be a linear relationship or a nonlinear relationship.
[0147] Case 2: When the previous cycle was static, you can use either of the following two solutions for adjustment:
[0148] Option 1:
[0149] Based on the I frame size of the previous cycle I The pixel depth IPP of the I frame is calculated with the image width W, image height H, and frame rate fps. The calculation formula is:
[0150]
[0151] According to the obtained current sequence pixel depth BPP, the threshold T4 is calculated using the following formula:
[0152] T4=f 14 (BPP)(17)
[0153] Among them, f 14 (x) is the mapping function from BPP to T4, f 14 (x) can be a linear relationship or a nonlinear relationship.
[0154] Calculate the quantization parameter offset value of the I frame based on the pixel depth IPP of the I frame and the threshold T4 The calculation formula is:
[0155]
[0156] Among them, f 15 (x) is the time from (IPP, T4) to The mapping function, f 15 (x) can be a linear relationship or a nonlinear relationship.
[0157] Solution 2: Calculate the quantization parameter of the I frame based on the number of short-term static durations Stillnum The calculation formula is:
[0158]
[0159] Among them, f 16 (x) is the distance from Stillnum to The mapping function, f 16 (x) can be a linear relationship or a nonlinear relationship.
[0160] Furthermore, according to the quantization parameter offset value of the current I frame The quantization parameter of the previous I frame Adjust the quantization parameters of the current I frame The calculation formula is:
[0161]
[0162] Among them, f 17 (x) is from arrive The mapping function, f 17 (x) can be a linear relationship or a nonlinear relationship.
[0163] For ease of explanation, this application provides a specific implementation scenario as a general example to assist in understanding the settings of various parameters. However, all parameters involved in this application can be customized based on actual scenarios, and this application does not impose specific restrictions on this. If the previous cycle was during motion, then the first adjustment is performed. If the previous cycle was during stillness, then the second adjustment is performed.
[0164] Among them, situation 1 can be adjusted by taking any of the following two solutions:
[0165] Option 1:
[0166] Motion based on the current frame I Calculate the threshold (T2, T3). The calculation formula can be, but is not limited to, the following form:
[0167] Among them, T2 = T3-25%, Motion Max Indicates the maximum picture change between two consecutive frames, where the range of the maximum picture change is less than or equal to the entire video frame.
[0168] Furthermore, according to the actual encoding rate of the previous cycle The upper limit of the bit rate of the previous periodizer Threshold (T2, T3), calculate the quantization parameter offset value of the current frame The calculation formula can be, but is not limited to, the following forms:
[0169] Here, the present application schematically provides a case of two thresholds. In different implementation scenarios, more or fewer thresholds may be set.
[0170] Solution 2: Motion based on the current frame I , calculate the quantization parameter offset value of the current frame The calculation formula can be, but is not limited to, the following forms:
[0171] Among them, 6 is a preset fixed value, which can be customized in different scenarios.
[0172] Case 2: When the previous cycle was prohibited, you can use either of the following two solutions to adjust:
[0173] Solution 1: Calculate the threshold T4 based on the current sequence pixel depth BPP. The calculation formula can be, but is not limited to, the following form:
[0174] Among them, the threshold T4 can be customized to set different ranges.
[0175] Furthermore, the quantization parameter offset value of the I frame is calculated based on the pixel depth IPP of the I frame and the threshold T4. The calculation formula can be, but is not limited to, the following forms:
[0176] Among them, the magnification can be customized to different values.
[0177] Solution 2: Calculate the quantization parameter offset value of the I frame based on the number of short-term static durations, that is, the static period number Stillnum The calculation formula is:
[0178] When there is at least one static period before the current period, the quantization parameter offset value is used to reduce the quantization parameter of the current frame.
[0179] Furthermore, according to the quantization parameter offset value of the current I frame The quantization parameter of the previous I frame Adjust the quantization parameters of the current I frame The calculation formula can be, but is not limited to, the following forms:
[0180] Among them, the quantization parameter corresponding to the current key frame Get adjusted.
[0181] S206: In response to the current frame being a non-key frame, obtaining a bit rate upper limit and an encoded bit rate of the current period, determining a quantization parameter of the current frame based on the bit rate upper limit and the encoded bit rate of the current period, and encoding the current frame.
[0182] Specifically, when the current frame is a non-key frame, the bitrate cap and the encoded bitrate of the current period are obtained. The quantization parameter of the current non-key frame is adjusted based on the bitrate cap and the encoded bitrate of the current period to obtain the quantization parameter of the current frame and encode it. The adjustment of the quantization parameter of the non-key frame begins from the second non-key frame in the current period.
[0183] In one embodiment, a quantization parameter of a current frame is determined and encoded based on a bit rate cap and an encoded bit rate of a current period, including: determining a bit rate threshold of the current period based on the bit rate cap, key frames, encoded video frames, and the total number of video frames of the current period; in response to the encoded bit rate of the current period exceeding the bit rate threshold of the current period, determining a quantization parameter offset value of the current frame based on the encoded bit rate of the current period and the bit rate threshold; in response to the encoded bit rate of the current period not exceeding the bit rate threshold of the current period, obtaining a quality level of a video frame sequence match, determining a quantization parameter offset value of the current frame based on the quality level, the bit rate cap of the current period, and actual motion of a previous non-key frame; and determining a quantization parameter of the current frame based on the quantization parameter of the previous non-key frame and the quantization parameter offset value, and encoding the frame.
[0184] Specifically, based on the current cycle's bitrate cap, the current cycle's key frames, the encoded video frames, and the total number of video frames, the current cycle's bitrate threshold is adjusted to determine the current cycle's bitrate threshold. The difference between the current cycle's bitrate cap and the bitrate occupied by key frames and the total number of video frames can be converted to the bitrate that can be allocated to non-key frames. The current cycle's bitrate threshold is adapted to the bitrate occupied by key frames, the bitrate that can be allocated to non-key frames, and the number of encoded frames.
[0185] Furthermore, the consumed encoded bit rate of the current period is compared with the bit rate threshold of the current period. When the encoded bit rate of the current period exceeds the bit rate threshold of the current period, the quantization parameter offset value of the current frame is mapped based on the encoded bit rate of the current period and the bit rate threshold to obtain the quantization parameter offset value of the current frame, so that when the bit rate of the current period is insufficient, the quantization parameter can be adjusted in time based on the remaining bit rate.
[0186] In addition, when the encoded bit rate of the current period does not exceed the bit rate threshold of the current period, the quality level of the video frame sequence match is obtained, and the quantization parameter of the current frame is mapped based on the quality level, the bit rate upper limit of the current period and the actual motion of the previous non-key frame to obtain the quantization parameter offset value of the current frame, so that when the bit rate of the current period is sufficient, the quantization parameter offset value that matches the quality level and the actual motion situation can be generated based on the quality level, the bit rate upper limit of the current period and the actual motion situation of the previous non-key frame, so as to adapt to the scenario when the bit rate is sufficient and improve the accuracy of non-key frame encoding.
[0187] Furthermore, the quantization parameter of the previous non-key frame is adjusted using the quantization parameter offset value of the current frame to obtain the quantization parameter of the current frame, thereby encoding the current non-key frame based on the quantization parameter of the current frame.
[0188] It can be understood that when the encoded bit rate of the current period exceeds the bit rate threshold of the current period, the quantization parameter offset value of the current frame is usually used to increase the quantization parameter of the current frame; when the encoded bit rate of the current period does not exceed the bit rate threshold of the current period, the quantization parameter offset value of the current frame is usually used to decrease the quantization parameter of the current frame.
[0189] In some implementation scenarios, the quantization parameter offset value of the current frame is determined based on the quality level, the bit rate cap of the current period, and the actual motion of the previous non-key frame, including: determining the coding bit threshold of the previous non-key frame based on the quality level, the bit rate cap of the current period, and the actual motion of the previous non-key frame; in response to the coding bits of the previous non-key frame not exceeding the coding bit threshold, determining the quantization parameter offset value of the current frame based on the coding bits of the previous non-key frame and the coding bit threshold; in response to the coding bits of the previous non-key frame exceeding the coding bit threshold, determining the quantization parameter offset value of the current frame based on the actual motion of the current frame and the actual motion of the previous non-key frame.
[0190] Specifically, the coding bit threshold for the current frame is mapped based on the quality level, the bit rate threshold for the current period, and the actual motion of the previous non-key frame to obtain the coding bit threshold for the previous non-key frame. The coding bit threshold is positively correlated with the quality level and the bit rate threshold for the current period, and negatively correlated with the actual motion of the previous non-key frame. Therefore, the coding bit threshold enables frame-level coding bit consumption comparison between non-key frames.
[0191] Furthermore, the coding bits of the previous non-key frame are compared with the coding bit threshold. When the coding bits of the previous non-key frame do not exceed the coding bit threshold, the quantization parameter offset value of the current frame is mapped based on the coding bits of the previous non-key frame and the coding bit threshold to obtain the quantization parameter offset value of the current frame. When the coding bits of the previous non-key frame exceed the coding bit threshold, the change trend of the motion between the video frames is determined based on the actual motion of the current frame and the actual motion of the previous non-key frame, and the quantization parameter offset value of the current frame is mapped based on the change trend of the motion to obtain the quantization parameter offset value of the current frame.
[0192] It should be noted that, based on the actual motion situation of the current frame and the actual motion situation of the previous non-key frame, the quantization parameter offset value of the current frame is determined, including: in response to the actual motion situation of the current frame and the actual motion situation of the previous non-key frame being different from each other, the motion difference between the current frame and the previous non-key frame is obtained, and the quantization parameter offset value of the current frame is determined based on the motion difference; in response to the actual motion situation of the current frame being the same as the actual motion situation of the previous non-key frame, the number of consecutive frames of non-key frames with the same actual motion situation is obtained, and the quantization parameter offset value of the current frame is determined based on the number of consecutive frames.
[0193] Specifically, it is determined whether the actual motion of the current frame is the same as the actual motion of the previous non-key frame. If the actual motion of the current frame is different from the actual motion of the previous non-key frame, the motion difference between the current frame and the previous non-key frame is obtained, and the quantization parameter of the current frame is mapped based on the motion difference to obtain the quantization parameter offset value of the current frame. Therefore, when there is a difference in the motion, a quantization parameter offset value that matches the change in the motion is obtained. If the actual motion of the current frame is the same as the actual motion of the previous non-key frame, the number of consecutive non-key frames with the same actual motion is obtained, and the quantization parameter offset value of the current frame is mapped based on the number of consecutive frames to obtain the quantization parameter offset value of the current frame. Therefore, when multiple video frames are continuously still in the current period, a quantization parameter offset value that matches the still situation is obtained.
[0194] It is understandable that the current period corresponds to the size of all key frames, that is, the I frame size The encoded video frame corresponds to the number of encoded frames Based on the coded I frame size in the current cycle The upper limit of the bit rate in the current cycle Number of encoded frames in the current cycle Total number of video frames FrameNum Short , calculate the bit rate threshold T5 of the current cycle, the calculation formula is:
[0195]
[0196] Among them, f 18 It can be a linear relationship or a nonlinear relationship, f 18 (x) is from Mapping function to T5.
[0197] Furthermore, the encoding rate in the current period is compared With the current period's code rate threshold T5, if the current period has encoded code rate Then make adjustment in case 1. If the encoding rate in the current cycle is Then make adjustments according to situation 2.
[0198] Case 1: Based on the encoded bit rate in the current period Calculate the quantization parameter offset value with the threshold T5 The calculation formula is:
[0199]
[0200] Among them, f 19 (x) is from arrive The mapping function, f 19 (x) can be a linear relationship or a nonlinear relationship.
[0201] Case 2: Based on the quality level and the current period's bitrate cap The motion of the previous frame Calculate the coding bit threshold T6 using the following formula:
[0202]
[0203] Among them, f 20 (x) is from Mapping function to T6, f 20 (x) can be a linear relationship or a nonlinear relationship.
[0204] Furthermore, the previous non-key frame (P frame) is compared with the coding bit threshold T6. If the coding bit of the previous P frame is Then perform the third adjustment. If the coding bits of the previous P frame Based on the coded bits of the previous P frame Threshold T6, calculate the quantization parameter offset value The calculation formula is:
[0205]
[0206] Among them, f 21 (x) is from arrive The mapping function, f 21 (x) can be a linear relationship or a nonlinear relationship.
[0207] Case 3: Motion of the current frame Motion with the current frame If they are different, calculate the difference Diff, and the calculation formula is:
[0208]
[0209] Calculate the quantization parameter offset value based on the difference Diff The calculation formula is:
[0210]
[0211] Among them, f 22 (x) is the distance from Diff to The mapping function, f 22 (x) can be a linear relationship or a nonlinear relationship.
[0212] Furthermore, the motion of the current frame Motion with the current frame If the same, statistics of the movement The number of times Num appears continuously. Based on the number of times Num appears continuously and the motion of the current frame Calculate quantization parameter offset value The calculation formula is:
[0213]
[0214] Among them, f 23 (x) is from arrive The mapping function, f 23 (x) can be a linear relationship or a nonlinear relationship.
[0215] Furthermore, according to the quantization parameter offset value of the current P frame The quantization parameter of the previous P frame Adjust the quantization parameters of the current P frame The calculation formula is:
[0216]
[0217] Among them, f 24 (x) is from arrive The mapping function, f 24 (x) can be a linear relationship or a nonlinear relationship.
[0218] For ease of explanation, this application provides a specific implementation scenario as a common case to assist in understanding the settings of various parameters, but all parameters involved in this application can be customized based on actual scenarios, and this application does not impose specific restrictions on this.
[0219] Based on the coded I frame size in the current cycle The upper limit of the bit rate in the current cycle Number of encoded frames in the current cycle Total number of video frames FrameNum Short , calculate the bitrate threshold T5 of the current cycle. Taking one key frame in the cycle as an example, the calculation formula is: Among them, Short Time is the cycle time.
[0220] Furthermore, if the encoding rate in the current cycle is Then make adjustment in case 1. If the encoding rate in the current cycle is Then make adjustments according to situation 2.
[0221] Case 1: Based on the encoded bit rate in the current period Calculate the quantization parameter offset value with the threshold T5 The calculation formula is:
[0222] Among them, when the encoded bit rate in the current cycle , the quantization parameter offset value is used to increase the quantization parameter of the current frame.
[0223] Case 2: Based on the quality level and the current period's bitrate cap The motion of the previous frame The threshold T6 is calculated using a formula that may be, but is not limited to, the following:
[0224] Among them, Motion Max Indicates the maximum picture change between two consecutive frames.
[0225] Furthermore, if the coded bits of the previous P frame Then perform situation three adjustment, otherwise it is based on the coding bits of the previous P frame and threshold T6, calculate the quantization parameter offset value The calculation formula can be, but is not limited to, the following forms:
[0226] Among them, at this time,
[0227] Case 3: Motion of the current frame Motion with the current frame If different, calculate the quantization parameter offset value based on the difference Diff The calculation formula can be, but is not limited to, the following forms:
[0228] Among them, the quantization parameter offset value Adapts to the difference Diff.
[0229] Furthermore, the motion of the current frame Motion with the current frame If the same, according to the number of times Num, the motion of the current frame Calculate quantization parameter offset value The calculation formula can be, but is not limited to, the following forms:
[0230] Among them, the quantization parameter offset value The number of times Num is continued and the motion of the current frame Suitable.
[0231] Furthermore, according to the quantization parameter offset value of the current P frame The quantization parameter of the previous P frame Adjust the quantization parameters of the current P frame The calculation formula can be, but is not limited to, the following forms:
[0232] Among them, the quantization parameter corresponding to the current non-key frame Get adjusted.
[0233] In this embodiment, the actual motion of the video frames in the previous cycle and the predicted motion of the video frames in the current cycle are obtained, wherein the predicted motion is predicted based on the actual motion of the video frames in the previous cycle. Based on the actual motion, the predicted motion, and the bitrate cap of the previous cycle, the bitrate cap of the current cycle is adjusted to obtain an updated bitrate cap for the current cycle. This allows the bitrate cap of the cycle to be encoded to be adjusted in a timely manner based on the motion of adjacent cycles during the encoding process, thereby improving encoding accuracy. The video frames in the current cycle are traversed, and quantization parameters are adjusted between frame levels based on various conditions for key frames and non-key frames to reduce the bitrate while ensuring image quality.
[0234] See also Figure 3 , Figure 3 This is a structural diagram of an embodiment of an electronic device of the present application. The electronic device 30 includes a memory 301 and a processor 302 coupled to each other, wherein the memory 301 stores program data (not shown in the figure), and the processor 302 calls the program data to implement the method in any of the above embodiments. For an explanation of the relevant content, please refer to the detailed description of the above method embodiments, which will not be repeated here.
[0235] See also Figure 4 , Figure 4 This is a structural diagram of an embodiment of a computer-readable storage medium of the present application. The computer-readable storage medium 40 stores program data 400. When the program data 400 is executed by the processor, the method in any of the above embodiments is implemented. For an explanation of the relevant content, please refer to the detailed description of the above method embodiments, which will not be repeated here.
[0236] It should be noted that the units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of this embodiment.
[0237] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0238] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) or a processor to execute all or part of the steps of each embodiment method of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0239] The above description is merely an implementation method of the present application and does not limit the scope of protection of the present application. Any equivalent structure or equivalent process transformation made using the contents of the present application specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the scope of protection of the present application.
Claims
1. A video encoding method, characterized in that: The method comprises: Obtaining a current period of a video frame sequence, and determining a previous period that matches the current period; wherein the previous period corresponds to a bit rate cap and a basic quantization parameter, and the basic quantization parameter is a quantization parameter of the first non-key frame in the corresponding period; Determine the basic quantization parameter of the current cycle based on the bit rate upper limit and the basic quantization parameter of the previous cycle, and traverse the video frames in the current cycle; In response to the current frame being a key frame, obtaining actual motion conditions of the video frame in the previous period and a quantization parameter of the previous key frame, determining a quantization parameter of the current frame based on the actual motion conditions and the quantization parameter of the previous key frame, and encoding the current frame; In response to the current frame being a non-key frame, a bit rate upper limit and an encoded bit rate of a current period are obtained, and a quantization parameter of the current frame is determined and encoded based on the bit rate upper limit and the encoded bit rate of the current period.
2. The video encoding method according to claim 1, wherein: After the video frame sequence is initialized, the bit rate upper limit, quantization parameter and motion gain of the first cycle are obtained. The initialization process of the video frame sequence is performed based on the following steps: Obtaining a quality level and a preset upper limit of a bit rate for the video frame sequence match, and determining a bit rate upper limit for the first period based on the quality level and the preset upper limit of the bit rate; Determining a pixel depth corresponding to the video frame sequence based on a bit rate cap of the first period and the video frame sequence and its corresponding frame rate, and determining a basic quantization parameter and a key frame quantization parameter of the first period based on the pixel depth; Based on the quality level and the pixel depth, a motion gain corresponding to the video frame sequence is determined; wherein the motion gain is used to adjust motion between adjacent video frames.
3. The video encoding method according to claim 1, wherein: The determining the basic quantization parameter of the current cycle based on the bit rate upper limit and the basic quantization parameter of the previous cycle includes: Obtaining a quality level of the video frame sequence match, and determining a bit rate threshold for the previous period based on the quality level and a bit rate upper limit for the previous period; In response to the actual encoding rate of the previous cycle exceeding the rate threshold of the previous cycle, determining a quantization parameter offset value of the current cycle based on the actual encoding rate of the previous cycle and the rate threshold of the previous cycle; In response to the actual encoding bit rate of the previous cycle not exceeding the bit rate threshold of the previous cycle, determining a predicted motion condition of the video frame in the current cycle based on an actual motion condition of the video frame in the previous cycle, and determining a quantization parameter offset value for the current cycle based on the actual motion condition and the predicted motion condition; The basic quantization parameter of the current cycle is determined based on the basic quantization parameter of the previous cycle and the quantization parameter offset value of the current cycle.
4. The video encoding method according to any one of claims 1 to 3, characterized in that: After determining the basic quantization parameter of the current cycle based on the bit rate upper limit and the basic quantization parameter of the previous cycle, the method further includes: Acquire the actual motion conditions of the video frames in the previous cycle, and the predicted motion conditions of the video frames in the current cycle predicted based on the actual motion conditions; The bit rate cap of the current cycle is determined based on the actual motion situation, the predicted motion situation, and the bit rate cap of the previous cycle.
5. The video encoding method according to claim 4, wherein: The predicted motion situation is obtained based on the following steps: Obtaining a frame weight corresponding to each video frame in the previous cycle and an average motion condition corresponding to all video frames; wherein the frame weight is negatively correlated with a frame interval between the corresponding video frame and the current cycle; Based on the actual motion situation and frame weight corresponding to each video frame in the previous period and the average motion situation, the predicted motion situation of the video frame in the current period is determined.
6. The video encoding method according to claim 1, wherein: The step of determining and encoding the quantization parameter of the current frame based on the actual motion condition and the quantization parameter of the previous key frame includes: In response to a video frame in the previous period including a video frame in which an actual motion condition indicates motion exists, determining a quantization parameter offset value of the current frame based on the current frame; In response to the actual motion conditions of all video frames in the previous period being stationary, determining a quantization parameter offset value of the current frame based on at least a portion of video frames preceding the current frame; Based on the quantization parameter and the quantization parameter offset value of the previous key frame, the quantization parameter of the current frame is determined and encoded.
7. The video encoding method according to claim 6, wherein: The determining, based on the current frame, a quantization parameter offset value of the current frame includes: Obtaining an actual motion condition of the current frame, and determining a ratio threshold based on the actual motion condition of the current frame; The quantization parameter offset value of the current frame is determined based on the ratio threshold and the ratio between the actual encoding bit rate of the previous cycle and the bit rate upper limit.
8. The video encoding method according to claim 6, wherein: The determining, based on the current frame, a quantization parameter offset value of the current frame includes: An actual motion condition of the current frame is obtained, and a quantization parameter offset value of the current frame is determined based on the actual motion condition of the current frame.
9. The video encoding method according to claim 6, wherein: The determining the quantization parameter offset value of the current frame based on at least a portion of video frames preceding the current frame includes: Determining a pixel depth of a previous key frame based on a frame rate corresponding to the previous key frame and the sequence of video frames; Obtaining a pixel depth corresponding to the video frame sequence, and determining a pixel depth threshold based on the pixel depth corresponding to the video frame sequence; A quantization parameter offset value of the current frame is determined based on the pixel depth of the previous key frame and the pixel depth threshold.
10. The video encoding method according to claim 6, wherein: The determining the quantization parameter offset value of the current frame based on at least a portion of video frames preceding the current frame includes: Determining the number of still periods preceding the current frame based on actual motion conditions of the video frames in the previous period and other periods before that; Based on the number of static periods, a quantization parameter offset value of the current frame is determined.
11. The video encoding method according to claim 1, wherein: The determining and encoding of the quantization parameter of the current frame based on the bit rate upper limit of the current period and the encoded bit rate includes: Determining a bit rate threshold for the current period based on a bit rate cap for the current period, key frames, encoded video frames, and a total number of video frames for the current period; In response to the encoded bit rate of the current period exceeding the bit rate threshold of the current period, determining a quantization parameter offset value of the current frame based on the encoded bit rate of the current period and the bit rate threshold; In response to the encoded bit rate of the current period not exceeding the bit rate threshold of the current period, obtaining a quality level of the video frame sequence match, and determining a quantization parameter offset value of the current frame based on the quality level, the bit rate upper limit of the current period, and actual motion of a previous non-key frame; Based on the quantization parameter and the quantization parameter offset value of the previous non-key frame, the quantization parameter of the current frame is determined and encoded.
12. The video encoding method according to claim 11, wherein: The determining the quantization parameter offset value of the current frame based on the quality level, the bit rate upper limit of the current period, and the actual motion of the previous non-key frame includes: Determining a coding bit threshold for a previous non-key frame based on the quality level, the bit rate upper limit of the current period, and actual motion of a previous non-key frame; In response to the coding bits of the previous non-key frame not exceeding the coding bit threshold, determining a quantization parameter offset value of the current frame based on the coding bits of the previous non-key frame and the coding bit threshold; In response to the coding bits of the previous non-key frame exceeding the coding bit threshold, a quantization parameter offset value of the current frame is determined based on actual motion conditions of the current frame and actual motion conditions of the previous non-key frame.
13. The video encoding method according to claim 12, wherein: The determining the quantization parameter offset value of the current frame based on the actual motion condition of the current frame and the actual motion condition of the previous non-key frame includes: In response to the actual motion of the current frame being different from the actual motion of the previous non-key frame, obtaining a motion difference between the current frame and the previous non-key frame, and determining a quantization parameter offset value of the current frame based on the motion difference; In response to the actual motion condition of the current frame being the same as the actual motion condition of the previous non-key frame, the number of consecutive non-key frames having the same actual motion condition is obtained, and the quantization parameter offset value of the current frame is determined based on the consecutive number of frames.
14. An electronic device, characterized in that: include: A memory and a processor coupled to each other, wherein the memory stores program data, and the processor calls the program data to execute the method according to any one of claims 1 to 13.
15. A computer-readable storage medium having program data stored thereon, characterized in that: When the program data is executed by a processor, the method according to any one of claims 1 to 13 is implemented.