r/askscience • • Jun 17 '12

Computing How does file compression work?

(like with WinRAR)

I don't really understand how a 4GB file can be compressed down into less than a gigabyte. If it could be compressed that small, why do we bother with large file sizes in the first place? Why isn't compression pushed more often?

414 Upvotes

146 comments sorted by

View all comments

Show parent comments

4

u/NEED_A_JACKET Jun 17 '12

Does video compression mainly work by 'adjusting' existing pixels? For example, 'this block of pixels moves over there' rather than giving information on the whole lot every frame?

I've always thought that's what keyframes were, a 'full' frame with the full detail, and everything in between the keyframes are just 'this part of the keyframe is now here'. Is that basically what's happening?

I've never read into it much but when you see compressed videos lose frames (bad signal / playing videos that aren't fully downloaded etc) they seem to skip the colour content of frames but still keep the movement. I always assumed that was what happens when it misses its 'key'.

6

u/Rusted_Satellites Jun 17 '12

http://en.wikipedia.org/wiki/Motion_compensation

Yes, they do move blocks of pixels around. Say you have a car driving along the street from left to right. The algorithm gets value out of saying "move these pixels x to the right" for large blocks that make up the car. However, if this is a live action car, that's not enough, since the lighting, reflections, and shadows will change, the car will not be going perfectly straight, etc... so after the motion vectors are applied, corrections are applied to get closer to the uncompressed next frame.

4

u/xNEM3S1Sx Jun 17 '12

I've always thought that's what keyframes were, a 'full' frame with the full detail, and everything in between the keyframes are just 'this part of the keyframe is now here'. Is that basically what's happening?

They are a "full" frame, but not necessarily uncompressed. These reference frames are still compressed, (like a jpeg vs bmp) and then the following frames are all information based around that frame, indicating hue, saturation, or luminance changes. (HSL)

I've never read into it much but when you see compressed videos lose frames (bad signal / playing videos that aren't fully downloaded etc) they seem to skip the colour content of frames but still keep the movement. I always assumed that was what happens when it misses its 'key'.

It will typically switch to pink/green, (I still don't know why most artifacts are pink/green, I would have to guess that it's cmky vs rgb related) and then only the parts of the picture that change regain information in their HSL values. This means that the moving part can get darker, or lighter, and more or less saturated or change hue, but doesn't have to do all of them, it could just get darker, or just get more saturated, and not change colour. (or any combination of the three)Some parts of the image, however are static, and will remain pink, until, of course, there is a new keyframe.

3

u/albatrossnecklassftw Jun 17 '12 edited Jun 17 '12

Compsci major here that works with image processing, though I've not worked on the intricacies of video compression, much of the techniques still apply. There are many different algorithms for video compression, and one method uses the JPEG compression algorithm [Removed since I am not familiar with the actual algorithm and was based on speculation] in two different stages. First stage is essentially jpeg compression of the still image, then the algorithm looks at successive frames to see if there are regions of the movie with similar color intensity values, if they are within a set threshold, then it deletes those pixels in the second frame and lets the computer know that that block of pixels are to be used in the next frame. This compression works great for videos without much movement. Say you are recording a speech where only the guy in the middle is moving, and the lighting is relatively stable throughout, a 4 gig video clip could easily be reduced to < 1 gig as only the pixels concerning the speaker are moving, if even those if he's still enough, however it results in very little compression in videos with alot of motion.

-1

u/Doctor Jun 17 '12

Dude... mp4 video uses AVC. mp3 is an audio codec, there's no reasonable way to make it work with images.

3

u/p-static Jun 17 '12

Well, to be pedantic, mp4 is a container format which can hold various types of audio and video, including MPEG audio and video streams (and AVC is an MPEG video codec). MPEG is just an industry group that standardizes these things, so it can get confusing when various parts of the stack are MPEG-something-or-other.

2

u/Doctor Jun 17 '12

Yeah, and the underlying standard is actually ISO BMFF, which derives from Quicktime... it's madness.

2

u/albatrossnecklassftw Jun 17 '12

They both rely on cosine transform for their lossy compression do they not? That is what I was referencing. I wasn't implying that an mp3 codec could have a movie run through it, merely that they use similar compression algorithms. Although I might be thinking of JPEG because that I know uses the cosine transform for compression.

2

u/Doctor Jun 17 '12

Sure, and they also rely on bits and bytes. AVC has a truckload of stuff beyond the basic transforms.

2

u/Snark_Jones Jun 17 '12

I think he is referring to MJPEG, which uses sequenced JPEG images as frames.

Alternatively, he could be referring to MPEG2 video encoding, which is what DVDs use.

1

u/[deleted] Jun 17 '12

MJPEG was my guess as well, it merely stores it as JPEGs (i.e. lossy to a degree on an image-by-image basis, but no frame-to-frame prediction). It's not used that much since CPU power is fairly cheap and you can cram a closer approximation of the video in the space with something mpeg4-based with the quality setting up real high, but it's very agile for situations where you want to edit frames (oops, that'll be quite in the way if you have to re-fix all the later frames), cut/paste, convert, etc but still somewhat space efficient compared to going whole hog and saving the frames uncompressed (or losslessly compressed).

2

u/CrasyMike Jun 17 '12 edited Jun 17 '12

I believe video compression isn't really moving blocks of pixels around, but simply explaining the difference on a block-by-block basis between the frames. So it's not moving the block, but explaining how the block has changed.

But my ExplainLikeI'm5 explanation is really the best I can give without probably leaving some inaccuracies in my response so I won't try to explain any further. Even what I said above is not really how computers do it, it's not a dictionary style of compression with Numbers representing whole words but it's an easy way to understand how compression works. Below others have given a more technical explanation of how compression works in a lower level.

2

u/NEED_A_JACKET Jun 17 '12

So it's not moving the block, but explaining how the block has changed.

Often you see specific details (eg. someone's face) 'stick' to a moving part of a scene such as the background, rather than simply being adjusted. Is that just a side effect of the pixels being adjusted? To me it looks a lot like the movement information is still there but it's 'moving' pixels based on an incorrect starting frame.

If that isn't how it works, why not? As a simplified example, if you had a panning shot of a background wouldn't it be easier to just record the fact that the whole block has moved by 2 pixels than to store the adjustment of each individual pixel? I would imagine that the majority of any video is movement based.

An example of what I mean: http://www.youtube.com/watch?v=u8TzQ8ugBIo

Every pixel that has been stored previously 'moves', and anything that hasn't been seen (the far side of his face) is 'updated'.

2

u/spiral_of_agnew Jun 17 '12

You're baically right. You've got a bunch of 3-dimensional blocks whose color information is defined by coefficients to some cosine function and whose third axis is time between keyframes. Only changes are stored, so when the data is corrupted, all subsequent frames show motion relative to the corrupted frame.