# Difference between jpg compression and mp3 compression

My understanding of jpg compression is that, if done repeatedly, any image will eventually be reduced to a single flat color, due to the way it deals with neighboring pixels. However, years ago I was told that mp3 does not have this problem, a 192kbps song will stay at the same audio quality with little file size variation no matter how many times it is compressed at the same setting.

Why is that? What's the differentiating factor?

Thanks

-

Actually, it's kind of the other way around. If you reuse the same JPEG encoder at the same quality level (without any smoothing steps as built-in prepcosessing) and a decoder which faithfully decompresses the images, I expect the image quality not to degrade from generation to generation. This is because quantization (the lossy part) is done the same way during the 2nd compression. You can imagine scalar quantization like this: given some value v and a quantizer step s, the quantized value is round(v/s)*s. Example:

v = 6.3
s = 2
q = round(v/s)*s = 6


So, the first "compression" includes an error of 0.3 in this case. But if you feed this into the encoder a 2nd time

v = 6
s = 2
q = round(v/s)*s = 6


it does not change anymore. It stays 6. If you change the quality level from one compression to the next, you also change the quantizer step s. This would add a new rounding error.

Keep in mind that this is just a simple example. JPEG Quantization may include a "dead zone quantizer" which changes the threshold a bit near 0. For example, you might want to round any value v to zero in case |v|<0.7. Something like this might give you a slightly higher quality-per-bit ratio because zeros can be much more compactly represented. But this should not change what I described above.

With MP3 the "quantization steps" are computed dynamically and depend on the actual signal. This is what the "psychoacoustic model" is for. It analyzes the audio data and tells us how strongly we can quantize certain portions of the signal without us noticing a difference. But this quantization adds "quantization noise" which could make the psychoacoustic model compute other quanzitation steps during the 2nd compression even in the case where you told the encoder to use the same "quality level". In the case the 2nd compression uses different quantization steps, you'll introduce new rounding errors.

Actually, something like this has been done before. Some artist did such an experiment where he compressed some audio file over and over again (like 1000 times) and the result had little to do with the original. Unfortunately, I don't remember any names or references.

There are other issues which make lossy audio compression more complicated. For example, a decoder might add or remove some leading samples (encoder+decoder delay mismatch) which would make the encoder work on different blocks for the 2nd pass. This would also add new rounding errors. But if I remember correctly, such a "block position mismatch" was shown to be beneficial w.r.t. generation loss by some experiments done by the Hydrogenaudio crowd a couple of years ago. So, while there was still a generation loss, the loss was less annoying than with matching block positions.

-
Wow, my perception of two things was completely turned around within one question, super informative. Given that this post addressed both, I'll go with this as the answer. Thank you very much @sellibitze! That experiment sounds interesting, I've seen a similar one done by introducing glitches to each generation of an image, will definitely do a search on that. –  qwalter Mar 24 at 15:46
What happens if the decoded image is edited in a small way and then resaved at the same quality? Obviously the result will be different in some way, but what effect does the modification have on the artifacts introduced by the initial lossy compression? Will any additional loss of data be restricted to the local area of the edit, or does the modification cascade? (I feel the answer is the former, but I'm not knowledgeable enough on the JFIF standard to know exactly how the compression of one region of an image is affected by neighboring regions). –  JAB Mar 24 at 18:05
@JAB: JPEG divides the image into "macro blocks" that are independent with respect to the lossy part of the compression. So, the macro blocks that intersect with the area you modified will change. A macro block is typically 16x16 pixels (with chroma subsampling enabled). –  sellibitze Mar 25 at 14:46
@qwalter: I heard about the "compressed 1000 times"-experiment in some SGU podcast. I couldn't find which episode it was. But it might have referred to this: mashable.com/2010/06/03/youtube-i-am-sitting –  sellibitze Mar 25 at 14:58
Apparently, we also have generation loss with lossy video compression. I guess this is due to motion compensation and deblocking filters. –  sellibitze Mar 25 at 15:04

I don't think that repeated jpg compression reduces to a single flat color. I tried compressing-decompressing an image 3 times. (Using GIMP 2.8.2, at quality level "10%" with progressive, exif, thumbnail and xmp all turned off, 4:2:2 vertical subsampling and integer DCT.) All three images are identical (Linux cmp turns up no differences at all between the files.)

jpg and mp3 have similar philosophies: given some model of human perception, estimate how well a human could perceive each feature in the input. If it can't be perceived (according to the model) then leave it out of the output. I don't know much about mp3, but in jpg, once you've removed certain features and kept others, the file is no longer compressible unless you use a different perception model for the next round of compression.

In jpg the human perception model is relatively simple: (1) we see gradations of intensity with much more detail than we see changes in color and (2) we see medium frequency gradations in more detail than we see high-frequency gradations.

Jpg makes use of (1) (usually) by converting the picture from sRGB to YCbCr. Then the Cb and Cr planes are reduced in resolution (usually by a factor of 2 in each direction, so you throw out 3/4 of the info.) Once the resolution loss is done once it is reversible.

Jpg makes use of (2) by transforming each 8x8 block of the picture into the frequency domain, and then quantizing the high frequencies (as described in @sellibitze's post). Again once the quantization is done it is reversible.

-
Thanks for elaborating! So would that mean, if we limit ourselves to standard jpg compression, each image would have a theoretical "bottom point" at which the quality can not get any worse? I was thinking about the possibility of alternating between high and low quality levels but according to what you described that would make no difference either yes? –  qwalter Mar 24 at 15:51
This is really interesting, because I just started a script to recompress files for a thousand times, and I did see that they're bit-identical! –  Alan Shutko Mar 24 at 16:58
@qwalter: I'm not sure about various roundoff error problems if you are switching back and forth between two models. For example, what if the high-quality profile were reducing the color resolution by a factor of 2 and the low-quality profile were reducing color resolution by a factor of 3. Now the chunks don't overlap as you switch back and forth between profiles. I think my reversibility argument depends on all the 8x8 blocks being independent. –  Wandering Logic Mar 24 at 18:21
True. The transform (DCT) actually serves two purposes. One is that it allows to get rid of things we don't perceive as easily (which you explained) and the other is that it decorrelates the data, that is, it puts most of the "energy" into very few "DCT bins". This is a more efficient representation. –  sellibitze Mar 25 at 14:52