[FFmpeg-user] How to get JPEG Quality factor

Gregory J Wolfe gregory.wolfe at kodakalaris.com
Mon Nov 2 18:13:58 CET 2015


> -----Original Message-----
> From: ffmpeg-user [mailto:ffmpeg-user-bounces at ffmpeg.org] On Behalf Of
> Carl Eugen Hoyos
> Sent: Sunday, November 01, 2015 5:11 AM
> To: ffmpeg-user at ffmpeg.org
> Subject: Re: [FFmpeg-user] How to get JPEG Quality factor
> 
> Przemysław Sobala <przemyslaw.sobala <at> grupawp.pl> writes:
> 
> > It turns out that it's some kind of 1/x relationship.
> 
> Definitely.
> 
> > Do you maybe have a final formula?
> 
> No.
> I would have expected that "JPEG Quality" is not a strictly defined term but I
> may of course be wrong.
> (I never read the standard.)
> 
> Carl Eugen
> _______________________________________________
> ffmpeg-user mailing list
> ffmpeg-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-user

When writing a single JPEG image, the JPEG quality level supplied by the client (1 to 100, 100 being the best) is used as follows.  This simplified example is based on the IJG library source code (see jcparam.c):
 
Step 1:  map client quality value to "scaled" quality value

if ( quality < 50 )
  scaled_quality = 5000 / quality;
else
  scaled_quality = 200 - 2 * quality;

Step 2:  apply scaled quality value to unscaled quantization table

for ( i = 0; i < DCT_TABLE_SIZE; i++ )
{
  int temp = (unscaled_qtable[i] * scaled_quality + 50) / 100;
  scaled_qtable[i] = (temp < 1) ? 1 : temp;
}

Step 3:  apply scaled quantization table to DCT coefficients

for ( i = 0; i < DCT_TABLE_SIZE; i++ )
   quantized_dct_values[i] = dct_values[i] / scaled_qtable[i];


Briefly:
- A high quality value becomes a low percentage value (and vice-versa).
- Quantization table values are scaled using the percentage value, so a high quality value becomes a low quantization table value.
- DCT coefficients are in turn divided by the quantization values, so a high quality level leads to less (or zero) quantization.
- Note that a quality level of 50 leaves unscaled_qtable[] unchanged, and a quality level of 100 results in scaled_qtable[i] == 1.

Only the final quantization tables (i.e. after scaling) are stored in the JPEG file.  The client's quality level is not stored, so you can only guess as to the quality value actually used.  If you know that the JPEG image was written using the default IJG unscaled quantization tables, you can directly back calculate the quality level.  However, if the client supplied his own unscaled_qtable[], then the best you can do is probably to back calculate using a least-squares fit to the default IJG tables.  If you do this, you should exclude 1s and 255s from the least squares calculation, since these values may have resulted from clipping out of range values when scaling the quantization tables.

Good luck!

Greg Wolfe, Software Engineer, Kodak Alaris Inc.


More information about the ffmpeg-user mailing list