A kind of automatic bit-rate control method and image compressing device
Technical field
The present invention relates to a kind of automatic bit-rate control method and image compressing device, particularly a kind of control method and image compressing device to JPEG coding automatic bit-rate.
Background technology
When the video chip hardware circuit design, consider that image JPEG compresses the back storage size and fixes, the image compression rate is more little, image fault is more little, therefore, selected fixing image compression rate keeps the peak use rate of memory space to be very important.
In jpeg coder, the image compression rate is adjusted by the Q value, and automatic bit-rate control is finished by self adaptation Q value adjuster.The purpose that the Q value is adjusted is the Q value that is used for predicting the next frame image, so that the compression ratio of next frame image can reach expection.
The JPEG contraction principle is to obtain 1 DC coefficient (DC coefficient) and 63 ac coefficients (AC coefficient) by a kind of piecemeal 8 * 8 data structures being carried out forward discrete cosine transform (FDCT), the data after obtaining compressing through quantification (Quantization), zigzag scanning (Zig-Zag Scan), huffman coding (HuffmanEncoding) again.
As shown in Figure 1, be common JPEG contraction principle schematic diagram, comprise discrete cosine transform module 101, quantization modules 102, zigzag scanning module 103, huffman coding module 104, automatic bit-rate control module 105, quantization table generation module 106 compositions.
In the JPEG condensing encoder, can control compression ratio (CR) and compression quality by changing quantization table (Q-table), the most frequently used method multiply by quantization table a coefficient exactly simultaneously, is called Q value (Q factor), and scope is generally 0~2.The Q value then reduces compression ratio less than 1, improves the picture quality after compressing, otherwise strengthens compression ratio, reduces picture quality.Different image curve difference can be very big.
Automatic bit rate controlled (auto bit rate control, ABRC) be used for the Rate Control that consecutive image compresses, (coordinate Q is [1 by fixing point with one for it, 0] in, adjustable, CR=0) straight line is similar to the CR-Q curve, and then determines linear equation (2 definite straight lines) according to former frame image used Q value and realistic compression ratio (CR), obtains the Q value prediction of the Q value of target compression correspondence as next frame again.We define CR
TargetBe target compression, Q
TargetBe corresponding target Q value.The implementation method that current Q value is adjusted is as follows: the CR-Q relation curve approximate one through a fixing point (wherein b is a negative parameter for Q=-b, straight line CR=0), just can not change in case decide usually again.We have an initial outside configurable Q
InitialAnd CR
Initial, we just can access curvilinear equation by initial value and fixing point (b, 0):
Q
TargetCan pass through formula
Calculate, Fig. 2 is the schematic diagram that concerns of compression ratio and Q value, and curvilinear equation figure as shown in Figure 2.
But in fact the CR-Q curve can't be a perfect straight line by fixing point, therefore the Q that calculates in this way
TargetCan not obtain more accurate Q value.Therefore mostly be to try to achieve the Q value of wanting now by the way of iteration repeatedly.Fig. 3 is repeatedly the schematic diagram that concerns of compression ratio under the iteration and Q value, and as shown in the figure, compression for the first time obtains Q
0And CR
0, then by No. 0 linear relationship and CR
Target(target compression) calculates Q
1, use Q
1Compression next frame image is by that analogy up to CR
nNear CR
TargetFor most of images by 2 to 3 frame iteration can be very fast force into actual curve, the speed of convergence is relevant with the starting point of iteration certainly, starting point is the closer to desired value, convergence rate is fast more.
Because in JPEG compression, all must not be after the every two field picture compression of a lot of requirements for hardware greater than certain thresholding, otherwise can overflow and abandon.But present JPEG compression can only just can learn whether overflow after the entire image compression, Joint Photographic Experts Group does not allow to revise quantization table in compression process yet simultaneously, that is to say that all pieces of 8 * 8 are all necessary with the same quantization table, i.e. Yi Yang Q value.Therefore can't adjust compression ratio in advance by adjusting the Q value.ABRC is by predicting the Q value with straight line approximate (CR-Q), because the curve approximation degree is not enough, so forecasting process is a convergence process, and the curve similarity is low more, convergence process is long more, and each frame meeting in the convergence process is excessive compression or under-voltage contracting (promptly overflowing) because the Q value is improper.Two content frames changed little before and after ABRC also required simultaneously, because the variation of CR-Q curve could be little like this, this satisfies easily for high frame per second or static scene, but for low frame per second, situations such as strenuous exercise's scene or scene switching then are difficult for satisfying, and then the prediction that makes the mistake, the Q value takes place easily excessively regulate or regulate not enough and cause the serious frame losing phenomenon of overflowing.Fig. 4 is the schematic diagram that concerns of compression ratio under big Q value and the little Q value situation and Q value; As shown in the figure, cause the following two kinds of situations of can reducing of this defective:
One, be a frame simple image after the frame complicated image: in this case, the big Q value (Q that simple image will be calculated by previous frame complicated image compressometer
Target_cmplx) compression, the target compression (CR of this simple image
Target) than realistic compression ratio (CR
Over) little a lot, may cause the result is that this simple image is compressed by transition, the serious distortion of image, and this situation may continue several frames and drop to a zone of reasonableness up to CR.
Two, be a frame complicated image after the frame simple image: in this case, the little Q value (Q that complicated image will be calculated by previous frame simple image compressometer
Target_smpl) compression, the target compression (CR of this complicated image
Target) than realistic compression ratio (CR
Lack) big a lot, may cause the result is that this complicated image compression is not enough, to such an extent as to hardware chart image storage space is overflowed, this situation may continue several frames and be raised to a zone of reasonableness up to CR.
Therefore, on original automatic bit-rate control jpeg image compression method, exist following deficiency:
Because big Q value changes and causes the undesirable of image compression between two two field pictures, the Q value of prediction next frame image that can't be correct can be compressed or compress and cause inadequately and frame losing can't provide high-quality image compression because of reducing image transition.
Summary of the invention
Technical problem solved by the invention is to provide a kind of automatic bit-rate control method and image compressing device, to reach when detecting between two two field pictures Q value and change greatly, in image compression process, the Q value of correct prediction next frame image, eliminate and reduce the image transition compression or compress the situation that causes frame losing inadequately, thereby guarantee provides high-quality image compression.
For solving the problems of the technologies described above, the invention provides a kind of automatic bit-rate control method, the Q value according to current compression is used dopes the Q value that compression next time should be used, and carries out the Rate Control of consecutive image compression in view of the above, comprises the steps:
(1) current Q value Q_curr of setting and the difference threshold Q_Threshold between the Q value Q_next next time;
(2) judge that whether difference Q_diff between the Q value next time that current Q value and prediction come out is above difference threshold;
(3) if the difference of the Q value next time of current Q value and prediction surpasses the difference threshold of described setting, then adjust the described Q value of calculating next time, difference is increased, and use the adjusted value of Q next time compression next frame image.
Wherein, described step (1) can comprise: set a plurality of difference threshold;
Described step (2) can comprise: different interval according to what described a plurality of difference threshold defined, corresponding different weights judge which interval is described difference be in, and described Q value next time is equipped with corresponding weights adjusts, and difference is increased with different amplitudes.
Wherein, described Q value to next time is equipped with the step that corresponding weights are adjusted, and the adjusted new value of Q next time equals current difference and multiply by the weights corresponding with this difference, adds and adjusts the preceding value of Q next time.
Automatic bit-rate control method of the present invention can further include:
Set the compression ratio testing conditions, this condition accounts for the ratio of entire image for the compression sizes in a two field picture compression process;
When current frame image is compressed to described ratio, whether can cause that memory space overflows after whole two field picture compression is finished according to the Q value prediction of current use;
Can overflow if predict this two field picture, then abandon the ac coefficient that obtains through discrete cosine transform, and current Q value is compensated.
The present invention and then a kind of image compressing device also is provided, comprise: forward discrete cosine transform module, quantization modules, zigzag scanning module, huffman coding module, automatic bit-rate control module, quantization table generation module, wherein, the Q value Qc that the automatic bit-rate control module is used according to current compression, dope the Q value Qn that the next frame compression should be used, the quantization table generation module generates new quantization table according to Qn, uses for quantization modules, and described compression set also comprises:
Q value correction module, be used for Qn, judge whether the difference between Qc and the Qn surpasses default thresholding, preset thresholding then Qn is adjusted if exceed according to automatic bit-rate control module prediction, difference between Qc and the Qn is further increased, and adjusted Qn input is quantized table generation module.
Wherein, described Q value correction module further surpasses the degree of presetting thresholding according to the difference between Qc and the Qn, Qn is equipped with different weights adjusts.
Image compressing device of the present invention can further include:
The Bit-Rate Control Algorithm module is used for the periodic monitoring compression ratio by force, can cause that memory space overflows if detect after the Q value Qc that uses current compression to use finishes whole two field picture compression, then abandons the ac coefficient that the process discrete cosine transform of predetermined number obtains;
Q value compensating module is used for the basis startup situation of Bit-Rate Control Algorithm module by force, and the Q value Qc that uses for the current compression of automatic bit-rate control module compensates.
After using the present invention, solved that compression ratio control is not very desirable problem in the jpeg image coding.Can be good at controlling image compression distortion and compression in the JPEG coding and cause the phenomenon of image frame losing inadequately, the high-quality that finally reaches output video image is with high smooth.
Description of drawings
Fig. 1 is the principle schematic of existing jpeg image compression set;
Fig. 2 is the schematic diagram that concerns of compression ratio and Q value;
Fig. 3 is repeatedly the schematic diagram that concerns of compression ratio under the iteration and Q value;
Fig. 4 is the schematic diagram that concerns of compression ratio under big Q value and the little Q value situation and Q value;
Fig. 5 is the principle schematic of image compressing device described in the embodiment of the invention;
Fig. 6 is to the control method implementing procedure schematic diagram of JPEG coding automatic bit-rate described in the embodiment of the invention.
Embodiment
As shown in Figure 5, be principle schematic according to the described image compressing device of the embodiment of the invention, compare with existing compression set, increased in the present embodiment: Bit-Rate Control Algorithm (RBRC) module 501, Q value compensate (Q-complement) module 502 by force, proofread and correct (Q-correction) module 503 with the Q value.All dispose some registers in described each module, store the parameter that needs separately.
Wherein, Q value correction module 503, be used to store the current Q value that sets in advance and the difference threshold between the Q value next time, in image compression process, whether the difference between the value of Q next time of judging current Q value and predicting surpasses default thresholding, if exceed default thresholding then Q value is next time adjusted, current Q value and the difference between the Q value are next time further increased, and the adjusted value of Q is next time imported quantification show the generation module.
Further, described difference threshold can be set a plurality of, and be weights of interval corresponding configuration that each difference threshold defined, Q value correction module 503 can judge which interval is described difference be in, described Q value next time is equipped with corresponding weights adjusts, difference is increased with different amplitudes.The concrete mode of adjusting can be: make the adjusted new value of Q next time equal current difference and multiply by the weights corresponding with this difference, add the value of Q next time before adjusting.Wherein said difference threshold, and parameter such as weights can be revised by the register configuration in the Q value correction module 503.
The Bit-Rate Control Algorithm module 501 by force, be used in a two field picture compression process periodic monitoring compression ratio, use current Q value can cause that memory space overflows after the two field picture compression is finished if detect, then abandon the ac coefficient that discrete cosine transform obtains that passes through of predetermined number putting in order; Described periodic monitoring can be arranged on 1/4,1/2 or 3/4 8 * 8 and detect when finishing compression, and concrete predictive mode parameters can be revised by the register configuration in the Bit-Rate Control Algorithm module 501 by force.
Q value compensating module 502 is used for according to the startup situation of Bit-Rate Control Algorithm module by force, for the Q value of the current use of automatic bit-rate control module compensates.Compensation can be to add a positive number, and concrete offset can be determined by emulation, and revise and store by the register parameters in the Q value compensating module 502.
According to image input condition levels of complexity, embodiments of the invention are divided into two stages:
One, the Q value correction during big Q variate:
This stage does not need to use RBRC and the compensation of Q value.The cardinal principle that this mode realizes is, use Q value (Q_curr) to alter a great deal when detecting the prediction next frame required Q value of image compression (Q_next) with present image, be the difference (Q_diff) of the Q value of next frame and current Q value when big, need on the basis of current Q_next slight increase or reduce obtain a new Q value (New_Q_next).Increasing or reduce numerical value is determined by the Q_diff=Q_next-Q_curr in-scope.
On specific implementation, multiple mode can be adopted, for example, 4 threshold value: Q_Threshold3<Q_Threshold4<0<Q_Threshold2<Q_Threshold1 and four weight factor weight1 can be selected, weight2, weight3, weight4.These parameters can be revised by register configuration.
So, when detecting Q_diff>Q_Threshold1, New_Q_next=(Q_diff*Q_weight1)+Q_next then;
When detecting Q_Threshold1>=Q_diff>Q_Threshold2, New_Q_next=(Q_diff*Q_weight2)+Q_next then;
When detecting Q_Threshold2>=Q_diff>0, then New_Q_next=Q_next;
When detecting Q_Threshold4<=Q_diff<0, then New_Q_next=Q_next;
When detecting Q_Threshold3<=Q_diff<=Q_Threshold4, New_Q_next==(Q_diff*Q_weight4)+Q_next then;
When detecting Q_diff<Q_Threshold3, New_Q_next==(Q_diff*Q_weight3)+Q_next then.
Because the Q value is big more, the image compression rate is big more, opposite Q value is more little, the image compression rate is more little, so, Q_diff be on the occasion of the time, can predict that the next frame image is a complicated image, we need increase a numerical value on the Q_next basis, can strengthen the Q value of compression complicated image so fast, and feasible to be unlikely to this complicated image compression ratio not enough; When Q_diff is negative value, can predict that the next frame image is a simple image, we need reduce by a numerical value on the Q_next basis, can reduce the Q value of compression simple image so fast, make to be unlikely to this simple image overvoltage.
This mode can be quickened the convergence process that the Q value is adjusted.Concrete Q value changes thresholding and regulated value can be set by the value that emulation mode obtains.The effect that this method reaches is to add rapid convergence, can effectively reduce frame losing and image excess compression phenomenon.
Two, by force BRC (Rude BRC, RBRC) and Q value compensate (Q-complement):
If after starting above-mentioned Q value correction, (foundation of frame losing is the compressed images byte number frame loss condition still often to occur, if this numerical value is greater than some particular values, then this image will be dropped), illustrate that so the effect that the Q value is proofreaied and correct is not very desirable, at this moment can further start Bit-Rate Control Algorithm and these two parts of Q value compensation by force.
Utilize present embodiment, can be in the process of piece image compression the regular monitoring compression ratio, when finishing compression such as 8 * 8 that can be made as 1/4,1/2 or 3/4, three predictive modes that just detect.If the image after current compression size surpass target sizes 1/4,1/2 or then opened AC coefficient drop policy at 3/4 o'clock, abandon by scanning sequency N AC coefficient reciprocal.The N value preestablishes, and different predictive mode N can be different.Recommendation is 32~56, but the occurrence emulation mode is determined.
Abandon the AC coefficient and can improve compression ratio, overflow, meet the requirement of JPEG compression standard simultaneously so that effective prevention is contingent.Because abandoning the AC coefficient has changed compression ratio, has destroyed the CR-Q curve, will have influence on the ABRC of back, therefore before carrying out ABRC, need current Q value is compensated, add a positive number exactly.The size of offset and predictive mode, it is relevant that current Q value and AC abandon number N, do not have clear and definite formula at present, and actual value is obtained by emulation mode, is three-dimensional table, and the distribution of value is 0~64.Can effectively prevent frame losing like this.
As shown in Figure 6, for according to the described control method schematic flow sheet of the embodiment of the invention, comprise the steps: JPEG coding automatic bit-rate
The variation thresholding of the difference between the Q value that the Q value of step 601, the current compression use of setting and the next frame that dopes use.
Step 602, after the difference size that detects the Q value surpasses Q value and changes thresholding, on the basis of the next frame Q value that detects by setting that regulated value increases or minimizing is predicted and the Q value of next frame made described difference further increase.
Step 603, setting compression ratio testing conditions, this condition accounts for the ratio of entire image for the compression sizes in a two field picture compression process.Can be made as when compression factor is 1/4,1/2,3/4 etc.
Step 604, when testing conditions satisfies, the size that the size that movement images obtains according to described compression ratio and storage object block obtain according to described compression ratio.
As us testing conditions being arranged on compression factor is 1/2 o'clock, if the total size of image is A, when the object block that can store size is B, then is that 1/2 * A and 1/2 * B are compared.
Step 605, the size that obtains according to described compression ratio when image greater than the storage object block according to described compression ratio obtain big or small the time, abandon ac coefficient.
Just, just abandon ac coefficient as 1/2 * A during greater than 1/2 * B.
Step 606, according to setting to Q value increase offset.
In sum, the present invention is directed in the design of existing video chip, is not very desirable problem to the compression ratio control of jpeg image coding, pointed proposition two kinds of concrete control devices, a kind of is by the Q value correcting method, adjusts the big Q value of two two field pictures and changes.Its basic thought is when detecting big Q value and change, and slightly changes original ABRC and calculates the Q value.This method increases very greatly constantly in the Q value, can quicken the convergence process of expecting that Q value is adjusted, and reduces under the very big situation in the Q value, can stop the too fast phenomenon that causes that image fault is serious of Q value adjustment.Another kind is BRC and the compensation method of Q value by force.This method mainly is not enough at compression ratio, causes the situation that memory space overflows.Its basic thought is in image compression process, predicts whether this two field picture can overflow, if judge this two field picture the possibility of overflowing is arranged, and then obtains bigger compression ratio by the method that abandons the AC coefficient, makes last image size can satisfy the memory space requirement.Because this method can be destroyed original Q value calculating method, therefore need compensate the calculating of Q value.Can significantly reduce because of storage space limitations by this method and to cause image frame losing phenomenon.