Video encoding method, device, equipment and medium

By dividing video frames into image blocks to detect the foreground ratio, determining the motion level and adjusting the code rate, the problem of poor bit rate saving effect caused by misjudgment of static scenes in the prior art is solved, and the encoding method with high bit rate during movement and low bit rate during static is realized, and the accuracy of overall bit rate control is improved.

CN115955564BActive Publication Date: 2025-08-08ZHEJIANG DAHUA TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211731021.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-12-30
Publication Date
2025-08-08
Estimated Expiration
2042-12-30

AI Technical Summary

Technical Problem

Existing video encoding methods are prone to misjudgment of motion and static scenes in static scenes, resulting in poor bit rate saving effect.

Method used

By dividing the video frame into image blocks of preset size, the ratio of the number of foreground image blocks is detected, the motion level is determined based on the ratio, and the target code rate coefficient and bits are determined based on the motion level, and the code rate control model is used to obtain quantization parameters for encoding.

Benefits of technology

It realizes the use of a higher target code rate during movement, actively reduces the code rate when stationary, thereby saving the overall code rate and improving the accuracy of code rate control.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115955564B_ABST
    Figure CN115955564B_ABST
Patent Text Reader

Abstract

The present invention discloses a video encoding method, apparatus, device and medium. Since the present invention obtains the motion condition of each video frame to determine the motion level, determines the target bit rate of the current video frame based on the motion level, determines the target bit allocated to the video frame according to the target bit rate, obtains the quantization parameter of the video frame according to the target bit mapping, and encodes each video frame in the target video according to the quantization parameter of each video frame in the target video to obtain a compressed code stream, compared with the existing method of simply dividing the scenes in the video frame into motion scenes and static scenes and then using corresponding fixed encoding methods for encoding, the present invention achieves the purpose of using a higher target bit rate when moving and actively reducing the target bit rate when static, thereby saving the overall bit rate.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of video coding and decoding technology, and in particular to a video coding method, apparatus, device and medium. Background Art

[0002] Video coding and decoding technology aims to compress the captured video into data in different standard formats for transmission or storage. Video coding and decoding standards include H.264 / Advanced Video Coding (AVC), H.265 / High Efficiency Video Coding (HEVC), and H.266 / Versatile Video Coding (VVC).

[0003] Rate control is a key technology in video coding standards. By adjusting encoding parameters during the encoding process, video data can be compressed, thereby controlling the bitrate at each moment. Each image in a video is called a frame. The compressed frame size is related to the encoder's encoding parameters, including frame type, motion information, and quantization parameter (QP). The quantization parameter controls the amount of data lost during the lossy quantization process and has a direct impact on the size of the encoded frame.

[0004] Common methods for rate control based on quantization parameters in existing technologies include: Constant Bit Rate (CBR) and Variable Bit Rate (VBR). CBR has stricter rate control requirements than VBR, as CBR requires the local bit rate to remain constant, while VBR allows the local bit rate to fluctuate within a certain range while maintaining stable subjective quality. With the development of multimedia technology, the performance requirements for rate control in video streams have also increased. For CBR, accurate rate control can maximize the transmission of data without wasting bandwidth. For VBR, it can minimize bit rate while maintaining subjective quality.

[0005] Rate control generally controls the bit rate at three levels: Group of Picture (GOP) level, frame level, and macroblock level. GOP-level coding control means that the video is usually divided into multiple continuous GOPs during encoding, which is the largest unit processed in the rate control process. The GOP-level coding control process will reasonably allocate the target number of bits for each GOP based on the target bit rate and buffer status. Frame-level coding control means that each GOP contains multiple video frames. The frame-level coding control process will allocate the target number of bits for each frame image based on the total target number of bits of the GOP and the characteristics of the video frame. The macroblock-level coding process analyzes each coded macroblock in the entire frame of video separately, and obtains the bits of the macroblock based on the characteristics of each coded macroblock. When calculating the quantization parameters based on the bits at the GOP level, frame level, and macroblock level, the rate-distortion theory is used. In data compression, the rate-distortion theory provides a theoretical limit and comparison standard for the performance of data compression. The method of improving video compression quality based on the rate-distortion theory is called rate-distortion optimization. The R-λ model, a type of rate control model, uses rate-distortion optimization. It models the mathematical relationship between allocated bits (R) and the rate-distortion parameter (λ). The quantization parameter (QP) is then mapped to the rate-distortion parameter (λ) to determine the quantization parameter, thereby controlling the bitrate. The RQ model directly models the mathematical relationship between allocated bits (R) and the quantization factor (Qscale). The QP is then calculated from the Qscale.

[0006] In the video surveillance scene discrimination method and its surveillance image encoding method and device in the prior art, the previous video frame obtained by monitoring and the current video frame are subtracted to obtain a residual frame, and then the ratio of the number of non-zero pixels in the residual frame to all pixels in the residual frame is counted. If the ratio is greater than a set threshold, the current scene is considered to be a motion scene, otherwise it is a static scene. For the identified motion scene or static scene, the image encoding method of the motion scene or static scene is adopted, thereby realizing surveillance image encoding.

[0007] However, in static scenes, the existing technology still has some differences in the pixel values between the two frames. Directly using the ratio of non-zero pixels to all pixels to determine whether it is a motion scene or a static scene is prone to misjudgment. In addition, a scene is only roughly divided into motion scenes and static scenes. In actual applications, there are many situations, such as large motion scenes, medium motion scenes, small motion scenes, static scenes, etc. Therefore, the video encoding method has a poor effect in saving bit rate. Summary of the Invention

[0008] The present invention provides a video encoding method, apparatus, device and medium, which are used to solve the problem that the existing video encoding method has a poor bit rate saving effect.

[0009] The present invention provides a video encoding method, the method comprising:

[0010] Get each video frame in the target video;

[0011] For each video frame, dividing the video frame into image blocks of a preset size, determining a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determining, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length;

[0012] For each video frame, determining a target bit rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the bit rate coefficient and the motion level;

[0013] For each video frame, inputting a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame to determine an output target bit of the video frame;

[0014] For each video frame, inputting the target bit of the video frame into a rate control model to obtain an output quantization parameter of the video frame;

[0015] Each video frame in the target video is encoded according to the quantization parameter of each video frame in the target video to obtain a compressed code stream.

[0016] Furthermore, determining a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame includes:

[0017] Determine a foreground mask image of the video frame, determine a foreground image block in the video frame based on the foreground mask image, and obtain a target ratio of a first number of foreground image blocks in the video frame and a total number of image blocks in the video frame.

[0018] Furthermore, after detecting the video frame to determine the foreground and background mask images of the video frame, and before determining the foreground image blocks in the video frame according to the foreground and background mask images, the method further includes:

[0019] Marking connected domains of the foreground and background mask images, determining a first parameter value of a preset parameter of each connected domain, wherein the preset parameter is the area of the connected domain or the number of pixels in the connected domain, and determining, for each connected domain, whether the first parameter value of the connected domain is less than a first preset threshold value. If so, filling the mask values of all pixels in the connected domain with the background mask value; if not, filling the mask values of all pixels in the connected domain with the foreground mask value, thereby obtaining an updated foreground and background mask image;

[0020] An updated foreground image block in the video frame is determined according to the updated foreground background mask image.

[0021] Furthermore, before performing connected component marking on the foreground and background mask images, the method further includes:

[0022] Based on the foreground and background mask images, a first ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame is determined; based on the first ratio, a second preset threshold, and a third preset threshold, whether the first ratio is less than the second preset threshold is determined, wherein the third preset threshold is greater than the second preset threshold; if so, the foreground and background mask images are first dilated and then eroded; if not, whether the first ratio is greater than the third preset threshold is determined; if so, the foreground and background mask images are first eroded and then dilated; if not, no processing is performed.

[0023] Furthermore, inputting the target bit rate coefficient of the video frame, the maximum bit of the group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame to determine the output target bit of the video frame includes:

[0024] If the video frame is a key frame, determine whether the video frame is the first key frame; if so, determine a first product value of a maximum bit of the picture group and a preset coefficient, and determine the first product value as a target bit of the video frame; if not, determine a second ratio of the target bit rate coefficient of the video frame to the bit rate coefficient of a previous frame of the video frame, determine a second product value of the second ratio and the bit of the previous frame of the video frame, and determine the second product value as the target bit of the video frame;

[0025] If the video frame is a non-key frame, determine a first difference between the maximum bit and the bit of the first key frame, and determine a second difference between the number of video frames contained in the picture group and a preset number. Based on a third ratio of the first difference and the second difference, and the target bit rate coefficient, determine a third product value of the third ratio and the target bit rate coefficient of the video frame, and determine the third product value as the target bit of the video frame.

[0026] Accordingly, the present invention provides a video encoding method, the method comprising:

[0027] Get each video frame in the target video;

[0028] For each video frame, dividing the video frame into image blocks of a preset size, determining a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determining, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length;

[0029] For each video frame, determining a target bit rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the bit rate coefficient and the motion level;

[0030] For each video frame, inputting a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame to determine an output target bit of the video frame, and determining each second parameter value of texture complexity of the video frame and each image block of the video frame according to a pixel value of each pixel point of the video frame, wherein the texture complexity is any one of parameters such as gradient, variance, mean absolute difference, and sum of absolute error;

[0031] For each image block of the video frame, determining a fourth ratio of the second parameter value of the image block to the second parameter value of the video frame, determining a fourth product value of the fourth ratio and the target bit of the video frame as a first bit of the image block, inputting the first bit of the image block into a rate control model, and obtaining an output candidate quantization parameter for the image block;

[0032] If the image block is a foreground image block, determining, based on a pre-stored second mapping relationship, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the second mapping relationship; if the image block is a background image block, determining, based on a pre-stored third mapping relationship, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the third mapping relationship;

[0033] Each image block of each video frame in the target video is encoded according to a target quantization parameter of each image block of each video frame in the target video.

[0034] Accordingly, the present invention provides a video encoding device, comprising:

[0035] An acquisition module, used to acquire each video frame in the target video;

[0036] a first determining module configured to divide each video frame into image blocks of a preset size, determine a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determine, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length;

[0037] a second determining module, configured to determine, for each video frame, a target rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the rate coefficient and the motion level;

[0038] a third determination module, configured to input, for each video frame, a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame, and determine an output target bit of the video frame;

[0039] a fourth determination module, configured to input, for each video frame, a target bit of the video frame into a rate control model to obtain an output quantization parameter of the video frame;

[0040] The encoding module is used to encode each video frame in the target video according to the quantization parameter of each video frame in the target video to obtain a compressed code stream.

[0041] Furthermore, the first determination module is specifically used to determine the foreground and background mask images of the video frame, determine the foreground image blocks in the video frame based on the foreground and background mask images, and obtain a target ratio of the first number to the total number based on the first number of foreground image blocks in the video frame and the total number of image blocks in the video frame.

[0042] Furthermore, the first determination module is also used to mark the connected domains of the foreground and background mask images of the video frame after detecting the video frame to determine the foreground and background mask images of the video frame, and before determining the foreground image blocks in the video frame based on the foreground and background mask images, to determine the first parameter value of the preset parameter of each connected domain, wherein the preset parameter is the area of the connected domain or the number of pixels in the connected domain. For each connected domain, it is determined whether the first parameter value of the connected domain is less than a first preset threshold value. If so, the mask values of all pixels in the connected domain are filled with the background mask values; if not, the mask values of all pixels in the connected domain are filled with the foreground mask values to obtain an updated foreground and background mask image; and based on the updated foreground and background mask images, the updated foreground image blocks in the video frame are determined.

[0043] Furthermore, the first determination module is also used to determine, before marking the connected domain of the foreground and background mask images, a first ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame based on the foreground and background mask images, and to judge, based on the first ratio, a second preset threshold and a third preset threshold, whether the first ratio is less than the second preset threshold, wherein the third preset threshold is greater than the second preset threshold. If so, the foreground and background mask images are first dilated and then eroded; if not, it is judged whether the first ratio is greater than the third preset threshold. If so, the foreground and background mask images are first eroded and then dilated. If not, no processing is performed.

[0044] Furthermore, the third determination module is specifically configured to, if the video frame is a key frame, determine whether the video frame is the first key frame; if so, determine a first product value of a maximum bit of the picture group and a preset coefficient, and determine the first product value as the target bit of the video frame; if not, determine a second ratio of the target bit rate coefficient of the video frame to the bit rate coefficient of the previous frame of the video frame, determine a second product value of the second ratio and the bits of the previous frame of the video frame, and determine the second product value as the target bit of the video frame; if the video frame is a non-key frame, determine a first difference between the maximum bit and the bits of the first key frame, and determine a second difference between the number of video frames included in the picture group and a preset number; determine a third product value of the third ratio and the target bit rate coefficient of the video frame based on a third ratio of the first difference and the second difference and the target bit rate coefficient, and determine the third product value as the target bit of the video frame.

[0045] Accordingly, the present invention provides a video encoding device, comprising:

[0046] An acquisition module, used to acquire each video frame in the target video;

[0047] a first determining module configured to divide each video frame into image blocks of a preset size, determine a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determine, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length;

[0048] a second determining module, configured to determine, for each video frame, a target rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the rate coefficient and the motion level;

[0049] a third determination module, configured to input, for each video frame, a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame, determine an output target bit of the video frame, and determine, based on a pixel value of each pixel point of the video frame, each second parameter value of texture complexity of the video frame and each image block of the video frame, wherein the texture complexity is any one of parameters such as gradient, variance, mean absolute difference, and sum of absolute error;

[0050] a fourth determination module, configured to determine, for each image block of the video frame, a fourth ratio of the second parameter value of the image block to the second parameter value of the video frame, determine a fourth product value of the fourth ratio and the target bit of the video frame as a first bit of the image block, input the first bit of the image block into a rate control model, and output a candidate quantization parameter for the image block;

[0051] a fifth determining module, configured to determine, if the image block is a foreground image block, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the second mapping relationship based on a pre-stored second mapping relationship; and, if the image block is a background image block, to determine, if the image block is a background image block, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the third mapping relationship based on a pre-stored third mapping relationship;

[0052] The encoding module is specifically configured to encode each image block of each video frame in the target video according to a target quantization parameter of each image block of each video frame in the target video to obtain a compressed code stream.

[0053] Accordingly, the present invention provides an electronic device, comprising: a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other via the communication bus;

[0054] The memory stores a computer program, and when the program is executed by the processor, the processor implements the steps of any one of the above-mentioned video encoding methods.

[0055] Accordingly, the present invention provides a computer-readable storage medium storing a computer program, wherein the computer program implements the steps of any one of the above-mentioned video encoding methods when executed by a processor.

[0056] The present invention provides a video encoding method, apparatus, device and medium. In the present invention, for each video frame, the video frame is divided into image blocks of a preset size. By detecting a target ratio of the number of foreground image blocks to the total number of image blocks in the video frame, a target motion level corresponding to a target numerical range of the target ratio is determined according to the target ratio and a correspondence between the numerical range and the motion level. The target rate coefficient of the video frame is determined according to the target motion level of the video frame and a first mapping relationship between the rate coefficient and the motion level. The target rate coefficient, the maximum bit of a picture group and the number of frames contained in the picture group are input into a bit determination function of the video frame. The target bit of the video frame is input into the maximum bit of the picture group. The target bit of the video frame is determined and input into a rate control model to obtain an output. Quantization parameters, based on the quantization parameters of each video frame in the target video, each video frame in the target video is encoded to obtain a compressed code stream. Since the present invention obtains the motion situation of each video frame to determine the motion level, and determines the target bit rate of the current video frame based on the motion level, determines the target bit allocated to the video frame according to the target bit rate, obtains the quantization parameters of the video frame according to the target bit mapping, and encodes each video frame in the target video according to the quantization parameters of each video frame in the target video to obtain a compressed code stream, compared with the existing technology that simply divides the scenes in the video frame into motion scenes and static scenes, and then adopts a corresponding fixed encoding method for encoding, the present invention achieves the purpose of using a higher target bit rate when moving and actively reducing the target bit rate when static, thereby saving the overall bit rate. BRIEF DESCRIPTION OF THE DRAWINGS

[0057] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0058] Figure 1 A schematic diagram of a video encoding method according to an embodiment of the present invention;

[0059] Figure 2 A schematic diagram of a linear relationship between a target motion level and a bit rate coefficient provided by an embodiment of the present invention;

[0060] Figure 3 A schematic structural diagram of a video encoding device provided by an embodiment of the present invention;

[0061] Figure 4 A schematic structural diagram of a video encoding device provided by an embodiment of the present invention;

[0062] Figure 5A schematic structural diagram of a video encoding device provided by an embodiment of the present invention;

[0063] Figure 6 A schematic structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0064] To make the objectives, technical solutions, and advantages of the present invention more apparent, the present invention will be further described in detail below with reference to the accompanying drawings. It is apparent that the embodiments described are only some, not all, of the present invention. All other embodiments derived by persons of ordinary skill in the art based on the embodiments of the present invention without creative effort are intended to fall within the scope of protection of the present invention.

[0065] In order to improve the accuracy of rate control and save bit rate when performing video encoding, embodiments of the present invention provide a video encoding method, apparatus, device and medium.

[0066] Example 1:

[0067] Figure 1 A schematic diagram of a video encoding method according to an embodiment of the present invention is provided, wherein the process includes the following steps:

[0068] S101: Acquire each video frame in the target video.

[0069] In order to improve the accuracy of bit rate control and save bit rate when performing video encoding, an embodiment of the present invention provides a video encoding method applied to an electronic device. The video encoding method is a frame-level encoding method for video, that is, by determining the quantization parameters of each video frame in the video, the bit stream after video compression is determined; the electronic device can be a host, tablet computer, laptop computer, smart phone and other intelligent terminal devices, or a server. The server can be a local server or a cloud server, and the embodiment of the present invention does not impose any restrictions on this.

[0070] The electronic device obtains the target video, specifically by capturing the target video through the electronic device's own image capture device, or by receiving the target video through another device connected to the electronic device, or by obtaining the target video pre-saved by the electronic device itself. This is not limited in the embodiments of the present invention. After obtaining the target video, the electronic device obtains each video frame from the target video frame.

[0071] S102: For each video frame, divide the video frame into image blocks of a preset size, determine a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and based on the target ratio and the correspondence between the numerical range and the motion level, determine that the motion level corresponding to the target numerical range within which the target ratio lies is the target motion level of the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length.

[0072] For each video frame, the electronic device divides the video frame into image blocks of a preset size, where the preset size is pre-set by the user, and the preset size is the size of a single pixel or the size of a rectangle with a preset side length. If the accuracy of bit rate control is to be improved, the preset side length can be set smaller. If the efficiency of image encoding is to be improved, the preset side length can be set smaller; for example, a video frame with a width of W and a height of H is divided into non-overlapping image blocks, and the size of the image blocks is MxN.

[0073] The electronic device detects the video frame and determines a foreground image block in each image block. The foreground refers to a person or scenery in front of the subject or close to the lens position. The foreground image block can also be called a motion image block. Based on the number of foreground image blocks and the total number of image blocks in the video frame, a target ratio of the number of foreground image blocks to the total number of image blocks is determined.

[0074] In order to determine the motion level of the video frame, the electronic device pre-saves the correspondence between the numerical range and the motion level, and determines the target numerical range where the target ratio is located based on the determined target ratio; based on the target numerical range and the correspondence between the numerical range and the motion level, the motion level corresponding to the target numerical range is determined as the target motion level; the smaller the target motion level, the closer the video frame is to a static scene, and fewer bits can be allocated; the higher the target motion level, the closer the video frame is to a motion scene, and more bits need to be allocated.

[0075] For example, the correspondence between the value range and the motion level can be expressed as obj ratio <0, motion_grade=0; obj ratio <1.0, motion_grade=1; obj ratio <2.0, motion_grade=2; obj ratio <4.0, motion_grade=3; obj ratio <8.0, motion_grade=4; obj ratio <12, motion_grade=5; obj ratio<16, motion_grade=6; obj ratio <20, motion_grade=7; obj ratio <24, motion_grade=8; obj ratio When ≥24, motion_grade=9; where motion_grade indicates the motion grade, obj_ratio indicates the ratio of foreground image blocks to the total number of image blocks. When the ratio is less than 0.5, the corresponding motion grade is 0; when the ratio is not less than 0.5 and less than 1.0, the corresponding motion grade is 1; when the ratio is not less than 1.0 and less than 2.0, the corresponding motion grade is 2; when the ratio is not less than 2.0 and less than 4.0, the corresponding motion grade is 3; when the ratio is not less than 4.0 and less than 8.0, the corresponding motion grade is 4; when the ratio is not less than 8.0 and less than 12, the corresponding motion grade is 5; when the ratio is not less than 12 and less than 16, the corresponding motion grade is 6; when the ratio is not less than 16 and less than 20, the corresponding motion grade is 7; when the ratio is not less than 20 and less than 24, the corresponding motion grade is 8; when the ratio is not less than 24, the corresponding motion grade is 9.

[0076] S103: For each video frame, determine a target rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the rate coefficient and the motion level.

[0077] In order to determine the bit rate coefficient assigned to the video frame, the electronic device pre-saves a first mapping relationship between the bit rate coefficient and the motion level, and according to the determined target motion level, determines the bit rate coefficient corresponding to the target motion level in the first mapping relationship as the target bit rate coefficient of the video frame.

[0078] The method for determining the target bit of a video frame in the present invention is described below through a specific embodiment. The first mapping relationship for calculating the rate coefficient rate_grade assigned to the video frame according to the target motion grade motion_grade is exemplified by a linear relationship. If the video frame is a key frame: rate_grade = (a + b * motion_grade) α1 If the video frame is a non-keyframe: rate_grade = (a + b * motion_grade) α2 Generally, a here can be taken as 0.05, which represents the lower limit of the bit rate, and b can be taken as 0.1.

[0079] Figure 2 A schematic diagram of the linear relationship between a target motion level and a bit rate coefficient provided by an embodiment of the present invention is shown in FIG. Figure 2As shown, the horizontal axis x represents (0.05+0.1*motion_grade), and the vertical axis y represents the rate coefficient rate_grade. Figure 2 From top to bottom ( Figure 2 The corresponding α values for each line (upper and lower in the figure) are 1 / 8, 1 / 4, 1 / 2, 1, 3 / 2, 2, 4, and 8, respectively. For keyframes, to maximize bit allocation in scenes with large motion, for example, when x is between 0.7 and 0.9, the coefficient α1 can be 0.5. For non-keyframes, to minimize bit allocation in static scenes, for example, when x is between 0.1 and 0.5, the coefficient α2 is 1.5.

[0080] If the current frame is a non-key frame and motion_grade is 3, base_ratio+0.1*motion_grade is 0.35, so according to Figure 2 The rate_grade is 0.21.

[0081] S104: For each video frame, input the target bit rate coefficient of the video frame, the maximum bit of the picture group, and the number of frames included in the picture group into a bit determination function of the video frame to determine the output target bit of the video frame.

[0082] For each video frame, based on the target bit rate coefficient of the video frame, the maximum bit of the picture group and the number of frames contained in the picture group, the target bit rate coefficient, the maximum bit and the number of frames are input into the bit determination function of the video frame to determine the target bits allocated to the output video frame.

[0083] S105: For each video frame, input the target bit of the video frame into a rate control model to obtain an output quantization parameter of the video frame.

[0084] For each video frame, the target bit of the video frame is input into the rate control model to obtain the output quantization parameter of the video frame; the rate control model can be an R-λ model or an RQ model, which is not limited in the embodiment of the present invention.

[0085] For example, when the rate control model can be an R-λ model, the specific formula is λ=α·R β , QP=4.2005·ln(λ)+13.7122; where R represents the target bit of the video frame, λ represents the rate-distortion parameter, and the parameters α and β are mapping-related coefficients and are updated every frame.

[0086] S106: Encode each video frame in the target video according to the quantization parameter of each video frame in the target video to obtain a compressed code stream.

[0087] After determining the quantization parameters of each video frame in the target video, each video frame in the target video is encoded according to each quantization parameter to obtain a compressed code stream composed of each compressed video frame; the specific method of encoding the video frame according to the quantization parameter is in the prior art and will not be described in detail in the embodiment of the present invention.

[0088] Since in the embodiment of the present invention, for each video frame, the video frame is divided into each image block of a preset size, by detecting the target ratio of the number of foreground image blocks in the video frame to the total number of image blocks, the target motion level corresponding to the target numerical range of the target ratio is determined according to the corresponding relationship between the target ratio and the numerical range and the motion level, and the target bit rate coefficient of the video frame is determined according to the target motion level of the video frame and the first mapping relationship between the bit rate coefficient and the motion level, so that a higher target bit rate coefficient is used when the motion level is large, and the target bit rate coefficient is actively reduced when it is still, and the target bit rate coefficient, the maximum bit of the picture group and the number of frames contained in the picture group are input into the bit determination function of the video frame, the target bit of the video frame is input into the maximum bit of the picture group, and the target bit of the video frame is input into the bit control function. The invention obtains the output quantization parameter, encodes each video frame in the target video based on the quantization parameter of each video frame in the target video, and obtains the compressed code stream. Since the present invention obtains the motion condition of each video frame to determine the motion level, and determines the target bit rate of the current video frame based on the motion level, determines the target bit allocated to the video frame according to the target bit rate, obtains the quantization parameter of the video frame according to the target bit mapping, and encodes each video frame in the target video according to the quantization parameter of each video frame in the target video to obtain the compressed code stream, compared with the prior art method of simply dividing the scenes in the video frame into motion scenes and static scenes, and then adopting the corresponding fixed encoding method for encoding, the present invention achieves the purpose of using a higher target bit rate when moving and actively reducing the target bit rate when static, thereby saving the overall bit rate.

[0089] Example 2:

[0090] In order to determine the target ratio of the number of foreground image blocks to the total number of image blocks, based on the above embodiment, in an embodiment of the present invention, determining the target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame includes:

[0091] Determine a foreground mask image of the video frame, determine a foreground image block in the video frame based on the foreground mask image, and obtain a target ratio of a first number of foreground image blocks in the video frame and a total number of image blocks in the video frame.

[0092] In order to determine the target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, the electronic device detects the video frame to determine the foreground and background mask images of the video frame. Specifically, the detection can be performed using a Gaussian mixture model, a frame difference method, or an optical flow method, which is not limited in the embodiments of the present invention.

[0093] When the preset size is the size of a single pixel, the size of the foreground and background mask images detected is the same as the size of the video frame. When the preset size is the size of a rectangle with a preset side length, for example, the preset size is a rectangle with an MxN size, when the size of the video frame is WxH, the length of the foreground and background mask images is W / M and the width is H / N.

[0094] For example, when the electronic device uses the frame difference method to detect the video frame, the size of the video frame is 1920x1080. After dividing it into 8x8 non-overlapping blocks, the difference diff value between each image block and the image block at the corresponding position of the previous frame is calculated, where M represents the row size of the image block, N represents the width of the image block, both of which are 8 here, Cur represents the pixel value of the current image block, and Pre represents the pixel value of the image block at the corresponding position in the previous frame.

[0095] If the current block diff is greater than the set threshold T1, the current block is considered to be a foreground image block, which can also be recorded as a moving image block. Otherwise, the current image block is a background image block, which can also be recorded as a still image block. Generally, T1 here can take the value of 20. The same processing is performed on all image blocks of the video frame to obtain the foreground and background conditions of the entire frame. The mask value of the pixel point of the foreground image block at the corresponding position of the foreground background mask image is set to the preset foreground mask value, and the mask value of the pixel point of the background image block at the corresponding position of the foreground background mask image is set to the preset background mask value, thereby obtaining the foreground background mask image of the video frame; the size of the foreground background mask image of the video frame is 240x135.

[0096] After determining the foreground and background mask images of the video frame, an image block corresponding to a pixel point having a mask value equal to a preset foreground mask value is determined as a foreground image block based on the mask value of each pixel point in the foreground and background mask images; and a target ratio of the first number to the total number of image blocks is determined based on the first number of foreground image blocks and the total number of image blocks.

[0097] Example 3:

[0098] In order to improve the accuracy of determining the foreground image block, based on the above embodiments, in an embodiment of the present invention, after detecting the video frame to determine the foreground and background mask images of the video frame, and before determining the foreground image blocks in the video frame based on the foreground and background mask images, the method further includes:

[0099] Marking connected domains of the foreground and background mask images, determining a first parameter value of a preset parameter of each connected domain, wherein the preset parameter is the area of the connected domain or the number of pixels in the connected domain, and determining, for each connected domain, whether the first parameter value of the connected domain is less than a first preset threshold value. If so, filling the mask values of all pixels in the connected domain with the background mask value; if not, filling the mask values of all pixels in the connected domain with the foreground mask value, thereby obtaining an updated foreground and background mask image;

[0100] An updated foreground image block in the video frame is determined according to the updated foreground background mask image.

[0101] The electronic device performs connected domain marking on the foreground and background mask images, determines each connected domain in the foreground and background mask images, and determines a first parameter value of a preset parameter based on each determined connected domain, wherein the preset parameter is the area of the connected domain or the number of pixels in the connected domain.

[0102] For each connected domain, the electronic device compares the first parameter of the connected domain to determine whether the first parameter value of the connected domain is less than a first preset threshold value. If the first parameter value of the connected domain is less than the first preset threshold value, it is considered that the connected domain belongs to the edge of the background part or a connected domain formed by noise, so the mask values of all pixels in the connected domain are filled with the background mask value; if the first parameter value of the connected domain is not less than the first preset threshold value, the mask values of all pixels in the connected domain are filled with the foreground mask value, thereby obtaining a more complete foreground image block and improving the integrity and accuracy of the foreground and background mask detection.

[0103] In order to further improve the integrity and accuracy of foreground and background mask detection, in an embodiment of the present invention, before performing connected component marking on the foreground and background mask image, the method further includes:

[0104] Based on the foreground and background mask images, a first ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame is determined; based on the first ratio, a second preset threshold, and a third preset threshold, whether the first ratio is less than the second preset threshold is determined, wherein the third preset threshold is greater than the second preset threshold; if so, the foreground and background mask images are first dilated and then eroded; if not, whether the first ratio is greater than the third preset threshold is determined; if so, the foreground and background mask images are first eroded and then dilated; if not, no processing is performed.

[0105] The electronic device determines the number of foreground image blocks in the video frame and the total number of image blocks in the video frame based on the foreground-background mask image, and determines a first ratio of the number of foreground image blocks to the total number of image blocks. According to the first ratio, a second preset threshold, and a third preset threshold, it is judged whether the first ratio is less than the second preset threshold. If the first ratio is less than the second preset threshold, it is considered that the foreground detection result is not complete enough, and there may be holes in the foreground, etc. Therefore, morphological processing of first dilating and then eroding the foreground-background mask image is performed; first dilating and then eroding is used to fill small holes in the object, connect adjacent objects, and smooth its boundary.

[0106] If the first ratio is not less than the second preset threshold, it is judged whether the first ratio is greater than the third preset threshold. If the first ratio is greater than the third preset threshold, it is considered that the foreground detection result is relatively complete, and there may still be a part of background interference. Therefore, morphological processing of first eroding and then dilating the foreground-background mask image is performed to eliminate the influence of isolated points in the background. First eroding and then dilating is usually used to eliminate small objects, separate objects at thin points, and smooth the boundaries of larger objects; if the first ratio is not greater than the third preset threshold, it is considered that the foreground detection result is basically complete at this time, and the interference of the background can also be ignored. Therefore, no morphological processing is performed on the foreground-background mask image.

[0107] Among them, the erosion algorithm uses an NxN matrix all of 1s to scan each pixel point in the foreground-background mask image, that is, the central element of the matrix corresponds to the pixel point position in the foreground-background mask image, and a logical "AND" operation is performed on each pixel in the matrix and the pixel value of the pixel point it covers. If the operation results are all 1, the pixel value of this pixel point is 1, otherwise it is 0; the dilation operation uses an NxN matrix all of 1s to scan each pixel point in the foreground-background mask image, that is, the central element of the matrix corresponds to the pixel point position in the foreground-background mask image, and a logical "OR" operation is performed on each pixel in the matrix and the pixel value of the pixel point it covers. If the operation results are all 0, this pixel is 0, otherwise it is 1.

[0108] For example, the electronic device processes the foreground-background mask image as follows: If P 前景 < T2, then first dilate and then erode. If P 前景 > T3, then first erode and then dilate. If T2 < P 前景 < T3, then no processing is performed; where P 前景 represents the proportion of foreground image blocks in a frame, which is between 0 and 1. Specifically, the values of T2 and T3 are taken as 0.15 and 0.20 respectively, and a 3x3 matrix all of 1s is used for erosion and dilation.

[0109] Since, in the embodiment of the present invention, adaptive morphological processing and connected domain calculation are performed on the foreground and background mask images, isolated noise points can be removed, and the integrity and accuracy of motion foreground detection can be improved. Compared with the prior art method of using preprocessed frames to determine whether the current scene is a motion scene or a static scene, the method is simpler and does not have a delay problem.

[0110] Example 4:

[0111] To determine the target bit of a video frame, based on the above embodiments, in this embodiment of the present invention, the target bit rate coefficient of the video frame, the maximum bit of a group of pictures, and the number of frames included in the group of pictures are input into a bit determination function of the video frame to determine the target bit of the video frame as an output, including:

[0112] If the video frame is a key frame, determine whether the video frame is the first key frame; if so, determine a first product value of a maximum bit of the picture group and a preset coefficient, and determine the first product value as a target bit of the video frame; if not, determine a second ratio of the target bit rate coefficient of the video frame to the bit rate coefficient of a previous frame of the video frame, determine a second product value of the second ratio and the bit of the previous frame of the video frame, and determine the second product value as the target bit of the video frame;

[0113] If the video frame is a non-key frame, determine a first difference between the maximum bit and the bit of the first key frame, and determine a second difference between the number of video frames contained in the picture group and a preset number. Based on a third ratio of the first difference and the second difference, and the target bit rate coefficient, determine a third product value of the third ratio and the target bit rate coefficient of the video frame, and determine the third product value as the target bit of the video frame.

[0114] When determining the target bit of the video frame, the electronic device determines whether the video frame is the first key frame if the video frame is a key frame. If the video frame is the first key frame, the electronic device determines a first product value of the maximum bit and the preset coefficient based on the maximum bit of the picture group and the preset coefficient, and determines the first product value as the target bit of the video frame; wherein the maximum bit of the picture group is The calculation method is Duration represents the time to encode a frame, bitrate represents the target bit rate, so the maximum bit of a frame calculated by duration*bitrate is Size. Gop Indicates the number of frames contained in a GOP.

[0115] If the video frame is divided into the first key frame, determine the second ratio of the target bit rate coefficient of the video frame and the bit rate coefficient of the previous frame of the video frame, and based on the second ratio and the bits of the previous frame of the video frame, determine the second product value of the second ratio and the bits of the previous frame, and determine the second product value as the target bit of the video frame.

[0116] For example, when the current video frame is a key frame, for the first frame: For non-first frames: Among them, α3 represents the proportional coefficient, ratio_Irate represents the ratio of the rate coefficient rate_grade of the current key frame to the rate coefficient rate_grade of the previous key frame, It represents the target bit of the previous key frame.

[0117] If the video frame is a non-key frame, a first difference between the maximum bit and the bit of the first key frame is determined based on the maximum bit of the picture group and the calculated bit of the first key frame. A second difference between the number of video frames and the preset number is determined based on the number of video frames included in the picture group and a preset number, where the preset number is 1. A third ratio of the first difference and the second difference is determined based on the first difference and the second difference, where the third ratio represents the average bit of the remaining video frames after the picture group removes the first key frame. A third product value of the third ratio and the target bit rate coefficient is determined based on the third ratio and the target bit rate coefficient, and the third product value is determined as the target bit of the video frame.

[0118] For example, if the current video frame is a non-key frame, first calculate the average bit rate of the remaining video frames after removing the first key frame in the picture group. in Indicates the maximum bit of the picture group, Indicates the bits of the first key frame, Size Gop Indicates the number of frames contained in GOP; the average bit Multiply it by the rate coefficient rate_grade of the current video frame to get the target bit rate of the current video frame. in

[0119] Since the embodiments of the present invention use relatively easy-to-obtain parameters and features for adjustment, the parameters are simple to calculate and obtain, which can well meet the use of software video encoding and decoding devices and can be well adapted to different video encoding standards.

[0120] Example 5:

[0121] In order to save bit rate, an embodiment of the present invention provides a video encoding method, which includes:

[0122] Get each video frame in the target video;

[0123] For each video frame, dividing the video frame into image blocks of a preset size, determining a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determining, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length;

[0124] For each video frame, determining a target bit rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the bit rate coefficient and the motion level;

[0125] For each video frame, inputting a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame to determine an output target bit of the video frame, and determining each second parameter value of texture complexity of the video frame and each image block of the video frame according to a pixel value of each pixel point of the video frame, wherein the texture complexity is any one of parameters such as gradient, variance, mean absolute difference, and sum of absolute error;

[0126] For each image block of the video frame, determining a fourth ratio of the second parameter value of the image block to the second parameter value of the video frame, determining a fourth product value of the fourth ratio and the target bit of the video frame as a first bit of the image block, inputting the first bit of the image block into a rate control model, and obtaining an output candidate quantization parameter for the image block;

[0127] If the image block is a foreground image block, determining, based on a pre-stored second mapping relationship, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the second mapping relationship; if the image block is a background image block, determining, based on a pre-stored third mapping relationship, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the third mapping relationship;

[0128] Each image block of each video frame in the target video is encoded according to the target quantization parameter of each image block of each video frame in the target video to obtain a compressed code stream.

[0129] In order to improve the accuracy of bit rate control and save bit rate when performing video encoding, an embodiment of the present invention provides a video encoding method applied to an electronic device. The video encoding method is a macroblock-level encoding method for video, that is, by determining the quantization parameters of each image block of each video frame in the video, the bit stream after video compression is determined; the electronic device can be a host, tablet computer, laptop computer, smart phone and other intelligent terminal devices, or a server. The server can be a local server or a cloud server, and the embodiment of the present invention does not limit this; the electronic device can be the same electronic device as the electronic device that performs video encoding in the above embodiments 1-5, or it can be a different electronic device.

[0130] The electronic device obtains the target video, specifically by capturing the target video through the electronic device's own image capture device, or by receiving the target video through another device connected to the electronic device, or by obtaining the target video pre-saved by the electronic device itself. This is not limited in the embodiments of the present invention. After obtaining the target video, the electronic device obtains each video frame from the target video frame.

[0131] For each video frame, the electronic device divides the video frame into image blocks of a preset size, where the preset size is pre-set by the user, and the preset size is the size of a single pixel or the size of a rectangle with a preset side length. The video frame is detected to determine the foreground image blocks in each image block, and based on the number of foreground image blocks and the total number of image blocks in the video frame, a target ratio of the number of foreground image blocks to the total number of image blocks is determined.

[0132] In order to determine the motion level of the video frame, the electronic device pre-saves the correspondence between the numerical range and the motion level, and determines the target numerical range where the target ratio is located based on the determined target ratio; based on the target numerical range and the correspondence between the numerical range and the motion level, the motion level corresponding to the target numerical range is determined as the target motion level; the smaller the target motion level, the closer the video frame is to a static scene, and fewer bits can be allocated; the higher the target motion level, the closer the video frame is to a motion scene, and more bits need to be allocated.

[0133] In order to determine the bit rate coefficient assigned to the video frame, the electronic device pre-saves a first mapping relationship between the bit rate coefficient and the motion level, and according to the determined target motion level, determines the bit rate coefficient corresponding to the target motion level in the first mapping relationship as the target bit rate coefficient of the video frame.

[0134] For each video frame, according to the target bit rate coefficient of the video frame, the maximum bit of the picture group and the number of frames contained in the picture group, the target bit rate coefficient, the maximum bit and the number of frames are input into the bit determination function of the video frame to determine the target bits allocated to the output video frame, and according to the pixel value of each pixel point of the video frame, the second parameter value of the texture complexity of the video frame is determined; according to the pixel value of each pixel point in each image block, the second parameter value of the texture complexity of each image block is determined, wherein the texture complexity is any one of the parameters such as gradient, variance, mean absolute difference and absolute error. For example, when the texture complexity is gradient, the second parameter value is the gradient value.

[0135] In order to achieve macroblock-level encoding of the target video, the electronic device determines the bits allocated to each image block of the video frame. Specifically, the electronic device determines a fourth ratio of the second parameter value of the image block to the second parameter value of the video frame, and determines a fourth product value of the fourth ratio and the target bit based on the fourth ratio and the target bit of the video frame, and determines the fourth product value as the first bit of the image block.

[0136] In order to achieve macroblock-level encoding of the target video, the electronic device inputs the first bit of the image block into a rate control model to obtain the output candidate quantization parameter of the image block; if the image block is a foreground image block, the target quantization parameter corresponding to the candidate quantization parameter of the image block in the second mapping relationship is determined based on the second mapping relationship between the candidate quantization parameter of the foreground image block and the target quantization parameter; if the image block is a background image block, the target quantization parameter corresponding to the candidate quantization parameter of the image block in the third mapping relationship is determined based on the third mapping relationship between the candidate quantization parameter of the background image block and the target quantization parameter.

[0137] For example, according to the target bit rate of the video frame And the second parameter value G of the texture complexity of each image block mb The bits allocated to each image block in Among them G mb The second parameter value, G, represents the texture complexity of each image block. fra The second parameter value representing the texture complexity of the video frame is obtained based on the bit number of each image block. Input the R-λ model to calculate the candidate quantization parameter QP for each image block pre .

[0138] According to the foreground and background mask images of the current video frame, each image block is judged to be a still image block or a moving image block, thereby adjusting the QP of each image block. When the current image block is a still image block, QP=f1(QP pre), when the current image block is a motion image block, QP=f2(QP pre ), f1() represents the second mapping relationship, and f2() represents the third mapping relationship, which can be a linear relationship or a nonlinear relationship. For example, when f1() and f2() are in a linear relationship, and the current image block is a static image block, QP=QP pre +α4, when the current image block is a moving image block, QP=QP pre -α5, where α4 and α5 can both be set to 2. For motion blocks, the quantization QP can be appropriately lowered to protect the quality of the motion blocks. For static blocks, the quantization QP can be appropriately increased to reduce the bit rate.

[0139] After determining the target quantization parameter of each image block of each video frame, each image block of each video frame in the target video is encoded to obtain a compressed code stream composed of each image block of each compressed video frame.

[0140] Example 6:

[0141] Figure 3 A schematic diagram of the structure of a video encoding device provided by an embodiment of the present invention is shown in FIG. Figure 3 As shown, the device includes:

[0142] An acquisition module 301 is configured to acquire each video frame in a target video;

[0143] A first determining module 302 is configured to divide each video frame into image blocks of a preset size, determine a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determine, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, where the preset size is the size of a single pixel or the size of a rectangle with a preset side length;

[0144] A second determining module 303 is configured to determine, for each video frame, a target rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the rate coefficient and the motion level;

[0145] A third determination module 304 is configured to input, for each video frame, a target bit rate coefficient of the video frame, a maximum bit rate of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame, and determine an output target bit rate of the video frame;

[0146] A fourth determination module 305 is configured to input the target bit of each video frame into a rate control model to obtain a quantization parameter of the video frame as an output;

[0147] The encoding module 306 is configured to encode each video frame in the target video according to the quantization parameter of each video frame in the target video to obtain a compressed code stream.

[0148] Furthermore, the first determination module 302 is specifically used to determine the foreground and background mask images of the video frame, determine the foreground image blocks in the video frame based on the foreground and background mask images, and obtain a target ratio of the first number to the total number based on the first number of foreground image blocks in the video frame and the total number of image blocks in the video frame.

[0149] Furthermore, the first determination module 302 is also used to mark the connected domains of the foreground and background mask images of the video frame after detecting the video frame to determine the foreground and background mask images of the video frame, and before determining the foreground image blocks in the video frame based on the foreground and background mask images, to determine the first parameter value of the preset parameter of each connected domain, wherein the preset parameter is the area of the connected domain or the number of pixels in the connected domain. For each connected domain, it is determined whether the first parameter value of the connected domain is less than a first preset threshold value. If so, the mask values of all the pixels in the connected domain are filled with the background mask values; if not, the mask values of all the pixels in the connected domain are filled with the foreground mask values to obtain an updated foreground and background mask image; and based on the updated foreground and background mask images, the updated foreground image blocks in the video frame are determined.

[0150] Furthermore, the first determination module 302 is also used to determine, before marking the connected domain of the foreground and background mask images, a first ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame based on the foreground and background mask images, and to judge whether the first ratio is less than the second preset threshold based on the first ratio, a second preset threshold and a third preset threshold, wherein the third preset threshold is greater than the second preset threshold. If so, the foreground and background mask images are first dilated and then eroded; if not, it is judged whether the first ratio is greater than the third preset threshold. If so, the foreground and background mask images are first eroded and then dilated. If not, no processing is performed.

[0151] Furthermore, the third determination module 304 is specifically configured to, if the video frame is a key frame, determine whether the video frame is the first key frame; if so, determine a first product value of a maximum bit of the picture group and a preset coefficient, and determine the first product value as the target bit of the video frame; if not, determine a second ratio of the target bit rate coefficient of the video frame to the bit rate coefficient of the previous frame of the video frame, determine a second product value of the second ratio and the bits of the previous frame of the video frame, and determine the second product value as the target bit of the video frame; if the video frame is a non-key frame, determine a first difference between the maximum bit and the bits of the first key frame, and determine a second difference between the number of video frames included in the picture group and a preset number, determine a third product value of the third ratio and the target bit rate coefficient of the video frame based on a third ratio of the first difference and the second difference and the target bit rate coefficient, and determine the third product value as the target bit of the video frame.

[0152] Figure 4 A schematic diagram of the structure of a video encoding device provided by an embodiment of the present invention is shown in FIG. Figure 4 As shown, the device includes: a foreground and background segmentation module 401, a morphological processing module 402, a connected domain calculation module 403, a motion level analysis module 404, a frame level QP calculation module 405, a macroblock level QP calculation module 406, and an encoding module 407.

[0153] The foreground and background segmentation module 401 is used to obtain each video frame of the target video and detect the foreground and background of the video frame through a motion detection method for subsequent motion level analysis; it is equivalent to the acquisition module 301 and the first determination module 302 in the above embodiment.

[0154] The morphological processing module 402 is used to process the foreground and background mask images of the extracted video frames after the foreground and background segmentation module 401 to eliminate noise and holes; it is equivalent to the first determination module 302 in the above embodiment.

[0155] The connected component calculation module 403 is used to mark the connected components of the foreground and background masks after the morphological processing module 402 ; it is equivalent to the first determination module 302 in the above embodiment.

[0156] The motion level analysis module 404 is used to calculate the motion level of the current video frame; it is equivalent to the first determination module 302 in the above embodiment.

[0157] The frame-level QP calculation module 405 is used to calculate the quantization parameter of the video frame; it is equivalent to the second determination module 303, the third determination module 304 and the fourth determination module 305 in the above embodiment.

[0158] The macroblock-level QP calculation module 406 is used to calculate the quantization parameter of each image block of the video frame; it is equivalent to the fourth determination module 305 in the above embodiment.

[0159] The encoding module 407 is used to encode each video frame in the target video to obtain a compressed code stream, which is equivalent to the encoding module 306 in the above embodiment.

[0160] Example 7:

[0161] Figure 5 A schematic diagram of the structure of a video encoding device provided by an embodiment of the present invention is shown in FIG. Figure 5 As shown, the device includes:

[0162] An acquisition module 501 is used to acquire each video frame in a target video;

[0163] A first determining module 502 is configured to divide each video frame into image blocks of a preset size, determine a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determine, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length;

[0164] A second determining module 503 is configured to determine, for each video frame, a target rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the rate coefficient and the motion level;

[0165] a third determining module 504 configured to input, for each video frame, a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame, determine an output target bit of the video frame, and determine, based on a pixel value of each pixel point of the video frame, each second parameter value of texture complexity of the video frame and each image block of the video frame, wherein the texture complexity is any one of parameters such as gradient, variance, mean absolute difference, and sum of absolute error;

[0166] a fourth determining module 505 configured to determine, for each image block of the video frame, a fourth ratio of the second parameter value of the image block to the second parameter value of the video frame, determine a fourth product value of the fourth ratio and the target bit of the video frame as a first bit of the image block, input the first bit of the image block into a rate control model, and output a candidate quantization parameter for the image block;

[0167] a fifth determining module 506 configured to determine, if the image block is a foreground image block, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the second mapping relationship based on a pre-stored second mapping relationship; and, if the image block is a background image block, to determine, if the image block is a background image block, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the third mapping relationship based on a pre-stored third mapping relationship;

[0168] The encoding module 507 is configured to encode each image block of each video frame in the target video according to a target quantization parameter of each image block of each video frame in the target video to obtain a compressed code stream.

[0169] Example 8:

[0170] Figure 6 This is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. Based on the above embodiments, this application also provides an electronic device, such as Figure 6 As shown, it includes: a processor 601 , a communication interface 602 , a memory 603 and a communication bus 604 , wherein the processor 601 , the communication interface 602 and the memory 603 communicate with each other via the communication bus 604 .

[0171] The memory 603 stores a computer program. When the program is executed by the processor 601, the processor 601 performs the following steps:

[0172] Get each video frame in the target video;

[0173] For each video frame, dividing the video frame into image blocks of a preset size, determining a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determining, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length;

[0174] For each video frame, determining a target bit rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the bit rate coefficient and the motion level;

[0175] For each video frame, inputting a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame to determine an output target bit of the video frame;

[0176] For each video frame, inputting the target bit of the video frame into a rate control model to obtain an output quantization parameter of the video frame;

[0177] Each video frame in the target video is encoded according to the quantization parameter of each video frame in the target video to obtain a compressed code stream.

[0178] Furthermore, the processor 601 is specifically configured to determine a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, including:

[0179] Determine a foreground mask image of the video frame, determine a foreground image block in the video frame based on the foreground mask image, and obtain a target ratio of a first number of foreground image blocks in the video frame and a total number of image blocks in the video frame.

[0180] Furthermore, the processor 601 is further configured to, after detecting the video frame to determine the foreground and background mask images of the video frame, and before determining the foreground image blocks in the video frame according to the foreground and background mask images, further comprise:

[0181] Marking connected domains of the foreground and background mask images, determining a first parameter value of a preset parameter of each connected domain, wherein the preset parameter is the area of the connected domain or the number of pixels in the connected domain, and determining, for each connected domain, whether the first parameter value of the connected domain is less than a first preset threshold value. If so, filling the mask values of all pixels in the connected domain with the background mask value; if not, filling the mask values of all pixels in the connected domain with the foreground mask value, thereby obtaining an updated foreground and background mask image;

[0182] An updated foreground image block in the video frame is determined according to the updated foreground background mask image.

[0183] Furthermore, the processor 601 is further configured to, before performing connected component labeling on the foreground and background mask images, further include:

[0184] Based on the foreground and background mask images, a first ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame is determined; based on the first ratio, a second preset threshold, and a third preset threshold, whether the first ratio is less than the second preset threshold is determined, wherein the third preset threshold is greater than the second preset threshold; if so, the foreground and background mask images are first dilated and then eroded; if not, whether the first ratio is greater than the third preset threshold is determined; if so, the foreground and background mask images are first eroded and then dilated; if not, no processing is performed.

[0185] Furthermore, the processor 601 is specifically configured to input the target bit rate coefficient of the video frame, the maximum bit of the group of pictures, and the number of frames included in the group of pictures into the bit determination function of the video frame, and determine the output target bit of the video frame, including:

[0186] If the video frame is a key frame, determine whether the video frame is the first key frame; if so, determine a first product value of a maximum bit of the picture group and a preset coefficient, and determine the first product value as a target bit of the video frame; if not, determine a second ratio of the target bit rate coefficient of the video frame to the bit rate coefficient of a previous frame of the video frame, determine a second product value of the second ratio and the bit of the previous frame of the video frame, and determine the second product value as the target bit of the video frame;

[0187] If the video frame is a non-key frame, determine a first difference between the maximum bit and the bit of the first key frame, and determine a second difference between the number of video frames contained in the picture group and a preset number. Based on a third ratio of the first difference and the second difference, and the target bit rate coefficient, determine a third product value of the third ratio and the target bit rate coefficient of the video frame, and determine the third product value as the target bit of the video frame.

[0188] Alternatively, the processor 601 is configured to obtain each video frame in the target video;

[0189] For each video frame, dividing the video frame into image blocks of a preset size, determining a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determining, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length;

[0190] For each video frame, determining a target bit rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the bit rate coefficient and the motion level;

[0191] For each video frame, inputting a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame to determine an output target bit of the video frame, and determining each second parameter value of texture complexity of the video frame and each image block of the video frame according to a pixel value of each pixel point of the video frame, wherein the texture complexity is any one of parameters such as gradient, variance, mean absolute difference, and sum of absolute error;

[0192] For each image block of the video frame, determining a fourth ratio of the second parameter value of the image block to the second parameter value of the video frame, determining a fourth product value of the fourth ratio and the target bit of the video frame as a first bit of the image block, inputting the first bit of the image block into a rate control model, and obtaining an output candidate quantization parameter for the image block;

[0193] If the image block is a foreground image block, determining, based on a pre-stored second mapping relationship, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the second mapping relationship; if the image block is a background image block, determining, based on a pre-stored third mapping relationship, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the third mapping relationship;

[0194] Each image block of each video frame in the target video is encoded according to the target quantization parameter of each image block of each video frame in the target video to obtain a compressed code stream.

[0195] The communication bus mentioned in the electronic device mentioned above may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. This communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one thick line is used in the figure, but this does not mean that there is only one bus or only one type of bus.

[0196] The communication interface 602 is used for communication between the electronic device and other devices.

[0197] The memory may include random access memory (RAM) or non-volatile memory (NVM), such as at least one disk memory. Alternatively, the memory may be at least one storage device located away from the processor.

[0198] The above-mentioned processor can be a general-purpose processor, including a central processing unit, a network processor (NP), etc.; it can also be a digital signal processing processor (DSP), an application-specific integrated circuit, a field programmable gate array or other programmable logic device, a discrete gate or transistor logic device, a discrete hardware component, etc.

[0199] Example 8:

[0200] Based on the above embodiments, the present application further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program executable by a processor. When the program is executed on the processor, the processor implements the following steps:

[0201] Get each video frame in the target video;

[0202] For each video frame, dividing the video frame into image blocks of a preset size, determining a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determining, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length;

[0203] For each video frame, determining a target bit rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the bit rate coefficient and the motion level;

[0204] For each video frame, inputting a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame to determine an output target bit of the video frame;

[0205] For each video frame, inputting the target bit of the video frame into a rate control model to obtain an output quantization parameter of the video frame;

[0206] Each video frame in the target video is encoded according to the quantization parameter of each video frame in the target video to obtain a compressed code stream.

[0207] Furthermore, determining a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame includes:

[0208] Determine a foreground mask image of the video frame, determine a foreground image block in the video frame based on the foreground mask image, and obtain a target ratio of a first number of foreground image blocks in the video frame and a total number of image blocks in the video frame.

[0209] Furthermore, after detecting the video frame to determine the foreground and background mask images of the video frame, and before determining the foreground image blocks in the video frame according to the foreground and background mask images, the method further includes:

[0210] Marking connected domains of the foreground and background mask images, determining a first parameter value of a preset parameter of each connected domain, wherein the preset parameter is the area of the connected domain or the number of pixels in the connected domain, and determining, for each connected domain, whether the first parameter value of the connected domain is less than a first preset threshold value. If so, filling the mask values of all pixels in the connected domain with the background mask value; if not, filling the mask values of all pixels in the connected domain with the foreground mask value, thereby obtaining an updated foreground and background mask image;

[0211] An updated foreground image block in the video frame is determined according to the updated foreground background mask image.

[0212] Furthermore, before performing connected component marking on the foreground and background mask images, the method further includes:

[0213] Based on the foreground and background mask images, a first ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame is determined; based on the first ratio, a second preset threshold, and a third preset threshold, whether the first ratio is less than the second preset threshold is determined, wherein the third preset threshold is greater than the second preset threshold; if so, the foreground and background mask images are first dilated and then eroded; if not, whether the first ratio is greater than the third preset threshold is determined; if so, the foreground and background mask images are first eroded and then dilated; if not, no processing is performed.

[0214] Furthermore, inputting the target bit rate coefficient of the video frame, the maximum bit of the group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame to determine the output target bit of the video frame includes:

[0215] If the video frame is a key frame, determine whether the video frame is the first key frame; if so, determine a first product value of a maximum bit of the picture group and a preset coefficient, and determine the first product value as a target bit of the video frame; if not, determine a second ratio of the target bit rate coefficient of the video frame to the bit rate coefficient of a previous frame of the video frame, determine a second product value of the second ratio and the bit of the previous frame of the video frame, and determine the second product value as the target bit of the video frame;

[0216] If the video frame is a non-key frame, determine a first difference between the maximum bit and the bit of the first key frame, and determine a second difference between the number of video frames contained in the picture group and a preset number. Based on a third ratio of the first difference and the second difference, and the target bit rate coefficient, determine a third product value of the third ratio and the target bit rate coefficient of the video frame, and determine the third product value as the target bit of the video frame.

[0217] Alternatively, the processor implements the following steps when executing:

[0218] Get each video frame in the target video;

[0219] For each video frame, dividing the video frame into image blocks of a preset size, determining a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determining, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length;

[0220] For each video frame, determining a target bit rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the bit rate coefficient and the motion level;

[0221] For each video frame, inputting a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame to determine an output target bit of the video frame, and determining each second parameter value of texture complexity of the video frame and each image block of the video frame according to a pixel value of each pixel point of the video frame, wherein the texture complexity is any one of parameters such as gradient, variance, mean absolute difference, and sum of absolute error;

[0222] For each image block of the video frame, determining a fourth ratio of the second parameter value of the image block to the second parameter value of the video frame, determining a fourth product value of the fourth ratio and the target bit of the video frame as a first bit of the image block, inputting the first bit of the image block into a rate control model, and obtaining an output candidate quantization parameter for the image block;

[0223] If the image block is a foreground image block, determining, based on a pre-stored second mapping relationship, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the second mapping relationship; if the image block is a background image block, determining, based on a pre-stored third mapping relationship, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the third mapping relationship;

[0224] The encoding of each video frame in the target video according to the quantization parameter of each video frame in the target video includes:

[0225] Each image block of each video frame in the target video is encoded according to a target quantization parameter of each image block of each video frame in the target video.

[0226] Those skilled in the art will appreciate that the embodiments of the present application can be provided as methods, systems, or computer program products. Therefore, the present application can adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application can adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.

[0227] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the present application. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 a process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0228] These computer program instructions may also be stored in a computer readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 a process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.

[0229] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing the instructions executed on the computer or other programmable device for implementing the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.

[0230] Obviously, those skilled in the art may make various changes and modifications to this application without departing from the spirit and scope of this application. Thus, if these modifications and variations of this application fall within the scope of the claims of this application and their equivalents, this application is intended to include these modifications and variations.

Claims

1. A video encoding method, characterized in that: The method comprises: Get each video frame in the target video; For each video frame, dividing the video frame into image blocks of a preset size, determining a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determining, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length; For each video frame, determining a target bit rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the bit rate coefficient and the motion level; For each video frame, inputting a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame to determine an output target bit of the video frame; For each video frame, inputting the target bit of the video frame into a rate control model to obtain an output quantization parameter of the video frame; Encoding each video frame in the target video according to the quantization parameter of each video frame in the target video to obtain a compressed code stream; The step of inputting a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and a number of frames included in the group of pictures into a bit determination function of the video frame to determine the target bit of the output video frame includes: If the video frame is a key frame, determine whether the video frame is the first key frame; if so, determine a first product value of a maximum bit of the picture group and a preset coefficient, and determine the first product value as a target bit of the video frame; if not, determine a second ratio of the target bit rate coefficient of the video frame to the bit rate coefficient of a previous frame of the video frame, determine a second product value of the second ratio and the bit of the previous frame of the video frame, and determine the second product value as the target bit of the video frame; If the video frame is a non-key frame, determine a first difference between the maximum bit and the bit of the first key frame, and determine a second difference between the number of video frames contained in the picture group and a preset number. Based on a third ratio of the first difference and the second difference, and the target bit rate coefficient, determine a third product value of the third ratio and the target bit rate coefficient of the video frame, and determine the third product value as the target bit of the video frame.

2. The method according to claim 1, characterized in that Determining the target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame includes: Determine a foreground mask image of the video frame, determine a foreground image block in the video frame based on the foreground mask image, and obtain a target ratio of a first number of foreground image blocks in the video frame and a total number of image blocks in the video frame.

3. The method according to claim 2, characterized in that After detecting the video frame to determine the foreground and background mask images of the video frame, and before determining the foreground image blocks in the video frame according to the foreground and background mask images, the method further includes: Marking connected domains of the foreground and background mask images, determining a first parameter value of a preset parameter of each connected domain, wherein the preset parameter is the area of the connected domain or the number of pixels in the connected domain, and determining, for each connected domain, whether the first parameter value of the connected domain is less than a first preset threshold value. If so, filling the mask values of all pixels in the connected domain with the background mask value; if not, filling the mask values of all pixels in the connected domain with the foreground mask value, thereby obtaining an updated foreground and background mask image; An updated foreground image block in the video frame is determined according to the updated foreground background mask image.

4. The method according to claim 3, characterized in that Before performing connected domain marking on the foreground and background mask images, the method further includes: Based on the foreground and background mask images, a first ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame is determined; based on the first ratio, a second preset threshold, and a third preset threshold, whether the first ratio is less than the second preset threshold is determined, wherein the third preset threshold is greater than the second preset threshold; if so, the foreground and background mask images are first dilated and then eroded; if not, whether the first ratio is greater than the third preset threshold is determined; if so, the foreground and background mask images are first eroded and then dilated; if not, no processing is performed.

5. A video encoding method, characterized in that: The method comprises: Get each video frame in the target video; For each video frame, dividing the video frame into image blocks of a preset size, determining a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determining, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length; For each video frame, determining a target bit rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the bit rate coefficient and the motion level; For each video frame, inputting a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame to determine an output target bit of the video frame, and determining each second parameter value of texture complexity of the video frame and each image block of the video frame according to a pixel value of each pixel point of the video frame, wherein the texture complexity is any one of parameters such as gradient, variance, mean absolute difference, and sum of absolute error; For each image block of the video frame, determining a fourth ratio of the second parameter value of the image block to the second parameter value of the video frame, determining a fourth product value of the fourth ratio and the target bit of the video frame as a first bit of the image block, inputting the first bit of the image block into a rate control model, and obtaining an output candidate quantization parameter for the image block; If the image block is a foreground image block, determining, based on a pre-stored second mapping relationship, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the second mapping relationship; if the image block is a background image block, determining, based on a pre-stored third mapping relationship, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the third mapping relationship; Each image block of each video frame in the target video is encoded according to the target quantization parameter of each image block of each video frame in the target video to obtain a compressed code stream.

6. A video encoding device, characterized in that The device comprises: An acquisition module, used to acquire each video frame in the target video; a first determining module configured to divide each video frame into image blocks of a preset size, determine a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determine, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length; a second determining module, configured to determine, for each video frame, a target rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the rate coefficient and the motion level; a third determination module, configured to input, for each video frame, a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame, and determine an output target bit of the video frame; a fourth determination module, configured to input, for each video frame, a target bit of the video frame into a rate control model to obtain an output quantization parameter of the video frame; An encoding module, configured to encode each video frame in the target video according to a quantization parameter of each video frame in the target video to obtain a compressed bit stream; The third determination module is specifically configured to, if the video frame is a key frame, determine whether the video frame is the first key frame; if so, determine a first product value of a maximum bit of a picture group and a preset coefficient, and determine the first product value as the target bit of the video frame; if not, determine a second ratio of the target bit rate coefficient of the video frame to the bit rate coefficient of a previous frame of the video frame, determine a second product value of the second ratio and the bits of the previous frame of the video frame, and determine the second product value as the target bit of the video frame; if the video frame is a non-key frame, determine a first difference between the maximum bit and the bits of the first key frame, and determine a second difference between the number of video frames included in the picture group and a preset number; determine a third product value of the third ratio and the target bit rate coefficient of the video frame based on a third ratio of the first difference and the second difference and the target bit rate coefficient, and determine the third product value as the target bit of the video frame.

7. A video encoding device, characterized in that The device comprises: An acquisition module, used to acquire each video frame in the target video; a first determining module configured to divide each video frame into image blocks of a preset size, determine a target ratio of the number of foreground image blocks in the video frame to the total number of image blocks in the video frame, and determine, based on the target ratio and a correspondence between a numerical range and a motion level, a motion level corresponding to a target numerical range within which the target ratio falls as a target motion level for the video frame, wherein the preset size is the size of a single pixel or the size of a rectangle with a preset side length; a second determining module, configured to determine, for each video frame, a target rate coefficient of the video frame according to the target motion level of the video frame and a first mapping relationship between the rate coefficient and the motion level; a third determination module, configured to input, for each video frame, a target bit rate coefficient of the video frame, a maximum bit of a group of pictures, and the number of frames included in the group of pictures into a bit determination function of the video frame, determine an output target bit of the video frame, and determine, based on a pixel value of each pixel point of the video frame, each second parameter value of texture complexity of the video frame and each image block of the video frame, wherein the texture complexity is any one of parameters such as gradient, variance, mean absolute difference, and sum of absolute error; a fourth determination module, configured to determine, for each image block of the video frame, a fourth ratio of the second parameter value of the image block to the second parameter value of the video frame, determine a fourth product value of the fourth ratio and the target bit of the video frame as a first bit of the image block, input the first bit of the image block into a rate control model, and output a candidate quantization parameter for the image block; a fifth determining module, configured to determine, if the image block is a foreground image block, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the second mapping relationship based on a pre-stored second mapping relationship; and, if the image block is a background image block, to determine, if the image block is a background image block, a target quantization parameter corresponding to the candidate quantization parameter of the image block in the third mapping relationship based on a pre-stored third mapping relationship; The encoding module is used to encode each image block of each video frame in the target video according to the target quantization parameter of each image block of each video frame in the target video to obtain a compressed code stream.

8. An electronic device, characterized in that: include: A processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other via the communication bus; A computer program is stored in the memory, and when the program is executed by the processor, the processor executes the steps of the video encoding method according to any one of claims 1 to 4, or the steps of the video encoding method according to claim 5.

9. A computer-readable storage medium, characterized in that It stores a computer program executable by a processor. When the program runs on the processor, the processor executes the steps of the video encoding method according to any one of claims 1 to 4 or the steps of the video encoding method according to claim 5.

Citation Information

Patent Citations

  • Video encoding method and device and encoder

    CN109951706A

  • Method for control video frequency coding rate

    CN1988659A