What is the maximum and minimum of 'q' value in ffmpeg?

5

2

When extract a image from a Video via ffmpeg, can get message like below.

frame=    1 fps=0.0 q=2.5 Lsize=N/A time=00:00:00.03 bitrate=N/A 

and that 'q' value means "quality of coded frames". I found this by searching code ffmpeg.c.(reference Link : http://www.ffmpeg.org/doxygen/trunk/frame_8h_source.html#l00205)

And normally it has value between 0.0 to 6.0. But the problem is it can get much higher value by reference.
The code says,

 fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality / (float)FF_QP2LAMBDA);

and the range of quality value is 1 to 32,767. and FF_QP2LAMBDA is 118. So we can get maximum value of %2.1f is over 277. And it is very suspicious (because of "%2.1f").

Juneyoung Oh

Posted 2013-08-29T06:21:16.057

Reputation: 547

Answers

7

Without going too much in the details of video encoding, you can look at both Quantization Parameters (QP) or the Lambda parameter to express the quality of an encoded frame.

This figure was taken from Xiaoyin, Cheng – Subjectively Optimized HDTV Video Coding, 2009. You can think of Lambda as a the Lagrangian multiplier in the Rate-Distortion Optimization process.

If you take a macroblock and encode it with a low QP, the distortion (D) in comparison to the original image will be low, but the bit rate (R) will be high. Likewise, if you choose a high QP, the distortion will be high, but the bit rate will be low. So, the cost of compressing a frame is calculated by J = D + λR – and this cost needs to be minimized.

In your specific case, I'm fairly certain that enc->coded_frame->quality is assigned Lambda values from the encoder. With the FF_QP2LAMBDA constant you can convert to Lambda units, e.g. let QP = 21, then λ = 21 × FF_QP2LAMBDA. The other way round, you get the QP by QP = λ / FF_QP2LAMBDA, which is what you're seeing in the output.

For example, in H.264, the basic relation is λ = 0.85 × 2(QP-12)/3. Since QP values range from 0 to 51, you get Lambda values between 0.053 and 6,963.2 – but FFmpeg will output the QP values anyway, by converting Lambda back into QP.

Now, when you say "range of quality value is 1 to 32,767", you mean the range of possible Lambda values. Since the Lagrangian function only comes into play after the QP is applied to the coded macroblock, there is a practical limit to the maximum number that coded_picture->quality can get. For example, in MPEG-4 Part 2, QP values go from 1–31, and like I said above, in MPEG-4 Part 10, it's from 0–51. In return, the q= will output the correct QP again, because the possible Lambda values are limited by what QP you chose before.

slhck

Posted 2013-08-29T06:21:16.057

Reputation: 182 472

Thanks for such an explanation. Are you one of ffmpeg contributor?:D I have an additional question, if you do not mind. Now I understand basic concept of QP(Quantization Parameter), Lambda, D(Distortion), But what about 'J'? Thanks again. – Juneyoung Oh – 2013-08-30T06:09:15.743

I'm not really a contributor in code, just a contributor of documentation and user support, if you will :) J is just the cost of the Lagrangian function which you want to minimize. This is, in essence, basic optimization mathematics. It is explained in the paper I linked you to in Section 1.5.3, but you will find generalizations about this in almost any good mathematics text book. – slhck – 2013-08-30T06:20:42.197