16

The following is mentioned in the ffmpeg documentation.

ffmpeg calls the libavformat library (containing demuxers) to read input files and get packets containing encoded data from them... Encoded packets are then passed to the decoder... The decoder produces uncompressed frames (raw video/PCM audio/...) which can be processed further by filtering...

Does that mean that you can always extract the original large size videos from all the compressed formats/codecs like h.264?

  • 18
    uncompressed != original – Bergi Apr 2 at 20:00
  • Yeah, and the question is about reversing the uncompressed to it's original; which has been explained and discussed in the answers. – thebrokencoder Apr 3 at 3:36
  • 2
    Aside from the technically oriented answers, a legally oriented person (a lawyer or judge or possibly the owner of the intellectual property) will, most probably, insist that the decompressed video is, in a nutshell, still the original work (even if it's copied at a much lower resolution/quality, for example). – Klaws Apr 3 at 16:01
  • @Klaws It logically makes sense. But anyway, one cannot legitimately redistribute the video/audio in an opensource format unless the original allows it to be converted or redistributed; as far as I know. – thebrokencoder Apr 3 at 18:30
48

Video compression is typically "lossy", in contrast to "lossless".

This means that by compressing the data, and subsequently decompressing it, you end up with an approximation of the original that is (hopefully) acceptable for the purpose.

Storing images and video using lossy compression techniques is generally fine, because humans aren't very good at perceiving the detail in an image - for example, our brain can fill in lots of details from context alone (as our eyes only have a small area in the center that is of "high resolution"), and our eyes are more sensitive to the intensity of green than red or blue (which explains the allocation in RGB565).

So, when you say:

Does that mean that you can always extract the original large size videos from all the compressed formats like mp4?

You're asking one question, but leaving lots of openings for misinterpretation.

  • Can we extract the original, pixel-perfect image from a video compressed using lossy techniques?
    • No
  • Can we extract a single frame of a video compressed using lossy techniques that has high quality, and could be used as a static image?
    • Probably not - movement in a scene will cause a higher number of artifacts, while a static scene will typically look better and better over time.
  • Can we extract an image of the original size, that looks "reasonable" to a human?
    • Yes - for a given definition of "reasonable"

This imperfect resulting image will have "Compression Artifacts". As shown in the exaggerated examples below:

good / 100% JPEG

bad / 5% JPEG


As the comments have also focused heavily on Audio quality, the exact same thing occurs. However, audio is a significantly lower bandwidth medium, which allows for lossless codecs to be practical (for example FLAC).

Converting from PCM / WAV → MP3 → PCM will result in an output PCM file that has the artifacts introduced by the MP3 encode/decode process - these cannot be removed.

Additionally, concatenating codecs will allow each to introduce their own artifacts, and the order that the codecs are applied can have a significant effect on the output.

Converting PCM → MP3 → OGG vorbis → Opus is not the same as PCM → Opus, because each of the intermediary codecs are lossy.

I've made an example audio file which presents this as another exaggerated example. This is the first ~30 seconds of "Tonight" by "The 69 Eyes", alternating every 5 seconds between the following chains:

  • CD → FLAC → Opus
  • CD → FLAC → MP3 → Vorbis → Opus

The effect here is dramatic, with the higher frequencies (i.e: high-hat) being almost completely removed from the audio, and with significant channel (left/right) interaction that is especially noticeable at the beginning.

compare

  • Comments are not for extended discussion; this conversation has been moved to chat. – DavidPostill Apr 2 at 22:17
  • 4
    re: chaining lossy codecs: The easiest way to understand this is that artefacts introduced by the first lossy codec become part of the "details" that the 2nd codec has to spend bits accurately reproducing. Any post-processing (like h.264's in-loop deblocking filter), or feeding one codec to another code, will smear out the ringing / blocking so it doesn't just become easy-to-quantize input for the next codec. (Like might happen with simple jpg -> jpg -> jpg or other simple codecs where stability might be reached.) – Peter Cordes Apr 3 at 4:11
  • 4
    @opa US vs UK spelling differences do not merit editing. – mbrig Apr 3 at 20:14
15

A short answer for codecs in general:
Any lossless codec like FLAC or ZIP can be reversed exactly.
Any lossy codec like JPEG or MP3 cannot be reversed exactly.

This principle is also valid for video codecs.
Here HuffYUV is an example for a lossless video-codec.
And x264 is an example for a lossy video-codec.

  • 1
    Very nicely put. – thebrokencoder Apr 3 at 12:53
  • 1
    To nitpick, you are mixing up some codecs (an encoder/decoder) and formats (what a codec outputs, or more specifically what an encoder outputs). To add to the confusion sometimes they can share the same name, and people use the term "codec" loosely. JPEG and MP3 are formats. LAME is an encoder that outputs MP3. FLAC is a format, but ffmpeg has a FLAC encoder/decoder (codec) named flac. x264 is an encoder that produces the H.264 video format (and to top it off x264 can output both lossy and lossless H.264). – llogan Apr 3 at 19:05
  • @llogan: Thank you for your clarification, it is surely helpful. – zx485 Apr 3 at 20:42
  • There is an interesting note to be made: h.264 does have a true lossless encoding setting[2][3]. It is of course not very common to see it in use in the real world, though, as it doesn't make much sense for the vast majority of applications. – Marc.2377 Apr 4 at 3:21
  • @Marc.2377: That's interesting, but as you said, it's an exception. – zx485 Apr 4 at 21:37
4

Does that mean that you can always extract the original large size videos from all the compressed formats like mp4?

No. You're misinterpreting that sentence.

You can't display compressed image, just as you can't read compressed text or read file compressed with ZIP without unpacking it first. To display any image, you have to somehow represent it uncompressed in memory, pixel-for-pixel.

This doesn't imply that the uncompressed image will be identical to original - this depends on codecs used. As far as I know there are no commonly used lossless codecs. Lossy codecs aim to represent original closely enough to make the uncompressed video indistinguishable under normal circumstances, ie. when played at predicted resolution and framerate. When looking at compressed stills (ie. framerate=0) you will certainly see artifacts, unless bitrate during compression is set impractically high (ie. producing too large output files) (this answer of mine is somehow related).


By the way, MP4 is only a file format, ie. it tells you how compressed data is stored in a file, but doesn't determine how it was compressed. MP4 can work with a variety of codecs. H.264 is probably the most popular video codec used with MP4, but don't quote me on that. Audio is usually compressed with AAC, I guess.

  • @Attie Now bold "original" – Lightness Races with Monica Apr 3 at 9:41
  • 1
    @Attie I'm interpreting "original", "large size" in OP's question as "lossless" – gronostaj Apr 3 at 9:45
  • Well, I get to understand that if the compression is lossless and is the first transcoding from the original (not concatenated), you can always decode and encode it back to the original pixel by pixel video, but not from a lossy format. – thebrokencoder Apr 3 at 12:30
  • @mjustin Exactly. What they mean is that decoder produces some uncompressed frames, not necessarily identical to original. – gronostaj Apr 3 at 12:37
  • @gronostaj But if the decoder is decoding from an uncompressed lossless format, then the result should be identical to the original ones, pixel by pixel. That's what I understood. – thebrokencoder Apr 3 at 12:43
1

This is a very common blunder.

You can convert from a lossy format to a lossless format, but the damage is done.

The lossless output file will show all the damage (compromised details, added artifacts) that are the reason you don't like the lossy format. You can't get that back.

The only option is to contact the publisher and see if he still has the original lossless files, and would be willing to somehow upload them to you.

There's no such thing as an Enhance button. warning: TVtropes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.