H.264 Is Magic (2016)

2 years ago (sidbala.com)

AV1 is more magic with better licensing.

For their use case Meta is gradually getting to a baseline of VP9 and AV1 streams for their video streaming: https://www.streamingmedia.com/Producer/Articles/Editorial/F...

And AV1 for video calls: https://engineering.fb.com/2024/03/20/video-engineering/mobi...

Microsoft is starting to use AV1 in Teams. AV1 has video coding tools that are particularly useful for screen sharing: https://techcommunity.microsoft.com/t5/microsoft-teams-blog/...

Most of the video I see on YouTube these days is in VP9 or AV1. Occasionally some video is still H.264.

H.264 will still be around for quite some time, but AV1 looks set to become the new baseline for internet video.

  • Well sure, but the hardware encode and decode isn’t completely widespread yet. I’ve been patiently waiting for what has felt like an eternity. From the developer perspective everyone needs to have access to it or I’m just sitting on my hands waiting for the capabilities to trickle down to the majority of users. Hopefully more users will purchase hardware if it features AV1 encode/decode. They need a logo that says “AV1 inside” or something. So, for example only the iPhone 15 pro offers hardware decode so far in the iPhone lineup.

    • You inevitably have to do multiple encodes. An H.264 encode as the "plays anywhere" option and an AV1 encode as the "less bandwidth or better image quality at the same bandwidth" option.

      YouTube does H.264, VP9, and AV1 video encodes with AAC and Opus audio encodes.

      This video for example has encodes of all those options: https://www.youtube.com/watch?v=ArmDp-zijuc

      You can watch it in multiple resolutions and formats from 4K in AV1 down to 144p in H.264.

      2 replies →

  • > AV1 is more magic with better licensing.

    AV1 is bloody awful to encode in software.

    I was recording videos of lectures (a slowly moving person against a whiteboard with thin sharp lines on it, in a questionably well-lit room) and needed to encode them to smallish 720p files to be posted somewhere (ended up uploading them to the Internet Archive). This is not something that x264’s default encoding profiles do well on, but with a day or two of fiddling with the settings I had something I could run on my 2014-era iGPU-only laptop the night after the lecture and have the result up the next day.

    By contrast, libaom promised me something like a week of rendering time for the three hours of video. Perhaps this could be brought down (my x264 fiddling got me veryslow-comparable results several times faster), but the defaults were so bad I couldn’t afford to experiment. (This was some four years ago. Things have probably gotten better since then, but I don’t expect miracles.)

    • Not at all my experience, 673 movies and counting. I encode at around 10 to 13x with my 12900K using svt-av1.

      My command:

      ffmpeg.exe -i file.mp4 -r 23.976 -vf scale=1280:720 -c:v libsvtav1 -pix_fmt yuv420p10le -crf 30 -preset 10 -g 600 -c:a libopus -b:a 96k -ac 2 -c:s copy -map 0 out.mp4

      Try it.

      5 replies →

    • > libaom promised me

      The reference AOM was never a practically useful encoder even 4 years ago.

      > I couldn’t afford to experiment

      Pity as you'd discover a better encoder...

  • In an 8-bit colorspace, h.264/5 suffer from virtually no block artifacts. AV1 can't get rid of them without upping to 10-bit. Not that it's necessarily a problem.

    The real problem with AV1 is how darn computationally intensive it is to compress.

    • > In an 8-bit colorspace, h.264/5 suffer from virtually no block artifacts. AV1 can't get rid of them without upping to 10-bit. Not that it's necessarily a problem.

      H.264 gets enough of a compression boost from 10-bit math that you should be using it anyway.

      I don't know if this affects H.265

      > The real problem with AV1 is how darn computationally intensive it is to compress.

      How are you measuring that?

      It's true that SVT-AV1 is slower than x264 at preset 6 or below. And it's slower than x265 at preset 3 or below. But those aren't the presets you use if you want to be fast.

      SVT-AV1 can match the quality of x264 veryslow while running at the speed of x264 veryfast.

      It can match the quality of x265 veryslow while running at the speed of x265 medium.

      It can match the quality of x265 medium while running at the speed of x265 ultrafast.

      https://engineering.fb.com/wp-content/uploads/2023/02/AV1-Co...

    • > how darn computationally intensive it is to compress.

      which i think is fine, as most decompression is on user side, but compression is on server side (and mostly only ever done once!).

      3 replies →

  • > Most of the video I see on YouTube these days is in VP9 or AV1. Occasionally some video is still H.264.

    can't shake the feeling that most videos at 720p took a hard hit to quality within the last years (or my eyes are getting better)

    any deep dives on the topic appreciated

    • I've compressed tens of thousands of videos in recent years and critically looked at the results.

      My gut feeling is that implementations of hardware acceleration are just simply not as good as software implementations, the cause perhaps relating to localised resource constraints in hardware when recursing deeper or somesuch.

      The algorithms are the same, the chip versions are more resource constrained as the stack goes deeper if you will.

      Hardware compressors are much much faster, no doubt, by an order of magnitude and more .. but software compressors left to run slow, with two passes, just produce a much better result.

      Standard hardware compressor settings tend to leave moire patterns when the camera pans outside a chain link fence onto a player inside a court (for example), or leaves a blocky halo around the significant figure point of interest moving in an otherwise stillish scene - it's subltle small things that grate when you see them.

      The root cause of perceived downgrade in quality is bulk pushing of high quality originals through hardware accelerated compression in order to save time and reduce cost when generating lower bitrate versions to stream.

      3 replies →

    • I don't have a deep dive, but I can confirm youtube has made things worse. I came across a copy of 『the TV show』 that I downloaded in 2009. 720p, H.264, 2460kbps. Looking again a couple weeks ago, it was 720p, VP9, 1300kbps. VP9 is more efficient but at half the bitrate it looks much worse.

      Poking more, there's an AV1 version that's a little less bad at about the same bitrate, but it's still significantly worse than the old encode. There's also a new version of H.264 that's 2/3 the size of the old one. It's about as bad as the VP9 version but the artifacts focus on different parts of the image.

      You'd think after 15 years of storage prices dropping, bandwidth prices cratering, and screens getting bigger, youtube might decide to maintain bitrates and let new codecs increase the quality of videos. Or split the difference between quality and savings. But nah, worse.

      7 replies →

    • During the pandemic everyone was online. There wasn’t enough bandwidth. So all major platforms slashed bitrates. YouTube, Netflix, Zoom.

      2 replies →

    • I thought it was well known that YouTube cut bitrates during the pandemic? In any case, I think you're quite right, I only watch YouTube in 720p and some videos seem comparable to stuff I downloaded from Kazaa back in the day. It's ok, though. I don't pay for YouTube and I think it's good for them to offer higher quality options for a charge rather than hound every single viewer for payment.

      1 reply →

  • AV1 doesn't have the fast encode/decode speed that H.264 has.

    • Do you have numbers for that?

      I don't have a comparison for decode speed, but software encoding of AV1 is able to match the performance of x264 veryfast while looking vastly better.

8 years after that article was written, many of the patents on H.264 are actually approaching expiry soon (i.e. within a year or two):

https://meta.wikimedia.org/wiki/Have_the_patents_for_H.264_M...

This is not surprising, given that the first version of the H.264 standard was published in 2003 and patents are usually valid for 20 years.

Its predecessors, H.263 and MPEG-4 ASP, have already patent-expired and are in public domain.

  • I bet that the successor algorithms will be implemented everywhere in hardware, and we're stuck with the patent problem once again.

Well OK, but what about H.265? It's one louder, isn't it?

https://en.wikipedia.org/wiki/High_Efficiency_Video_Coding

  • I do a lot of video compression for hobby projects, and I stick with h264 for the most part because h265 encoding requires far too much extra compute relative to the space savings. I can spend an hour compressing a file down to 1gb with h264, or I can spend 12 hours compressing the same file to 850mb with h265. Depending on the use-case, I might still need the h264 version anyway since it's far more widely supported by clients. If I had a data center worth of compute to throw at encoding, or I were running a streaming service where the extra 150mb per video started to add up, then I'd definitely on-board with h265 but it's really hard to justify for a lot of practical use-cases.

    • > I stick with h264 for the most part because h265 encoding requires far too much extra compute relative to the space savings. I can spend an hour compressing a file down to 1gb with h264, or I can spend 12 hours compressing the same file to 850mb with h265.

      FWIW, as someone who is a complete layman on this topic but has spent much of the past 6 months converting age-old videos to h265...

      i have a pi5 where i queue up old videos into a RAM disk, it converts them to h265, and moves them back to the machine which uploaded them. It can take up to 2 days to compress any given video (depending mostly on the input format - old AVIs tend to compress in a few hours), but the space savings is _much_ higher than what you're reporting. The average size of my h265-encodes is anywhere from 1/3rd to 1/2 of the original, and 1/4th of the original is not terribly uncommon. That said: the input formats vary wildly, with possibly only a minority fraction being h264.

      i've tried the same thing using a pi4 but it needs roughly 4-5x as long as the pi5. An 8-core intel-based laptop can do it in some 25% of the time of the pi5 but pulls a lot more electricity while doing so. So my otherwise idle pi5 gets the assignment.

      My Firefly collection (14 tv episodes plus the movie) dropped from 8gb to 2.1gb. My Bob Ross collection (141 videos) went from 38gb to 7.4gb. 128 Futurama episodes/movies was cut from roughly 50gb to 21gb.

      i was once asked why i didn't choose AV1 and the answer is simple: when i googled along the lines of "how to get the best video compression with ffmpeg" the most common answer was h265. Whether that's correct or not, i can't say, but it works for me and i've freed up the better part of a terabyte of space with it.

      1 reply →

    • What about when you’re recording (say your iPhone / android / standalone camera) - are you choosing h264 or h265 (or something else like an intra-frame only codec for easier editing).

      1 reply →

  • I really like HEVC/h265. It's pretty much on par with VP9. but licensing trouble has made it difficult to get adopted everywhere even now. VVC/h266 seems to be having the same issues; AV1 is pretty much just as good and already seeing much more adoption.

  • I've always felt like H.264 hit a great sweet spot of complexity vs compression. Newer codecs compress better, but they're increasingly complex in a nonlinear way.

    • It also benefits from the extremely optimized encoder x264, which has many easily approachable tunings and speed/compression presets.

      I’m not sure I’d trust x265 to preserve film grain better than x264 —tune film unless I gave x265 much, much more time as well as pretty similar bitrate.

      2 replies →

    • MPEG 5 EVC Baseline, arguably the improved / refined version of H.264, actually compress better while having the same complexity, Both in terms of mental model and computational cost.

      Somewhat unfortunate that never became anything.

  • > Well OK, but what about H.265? It's one louder, isn't it?

    I had some old, gigantic, video footage (in a variety of old, inefficient, formats at a super high quality).

    So I did some testing and, well, ended up re-encoding/transcoding everything to H.265. It makes for much smaller files than H.264. The standard is also ten years younger than H.264 (2013 for H.265 vs 2003 for H.264).

    • Years ago I was photographing a party in silicon valley (had just graduated college and was taking any job I could get). I take a nice photo of a couple and the wife looks at me and says " You know my husband invented H264? Do you know what that is?".

      Ended up having a long conversation with the husband (the engineer) and he hinted many times that they were working currently on something even better. Years later now of course we have H265. Everytime I play something in H264 or H265 I think of that moment and how much work that guy and his colleagues must have put into coding such a widely used codec (and how proud his wife was!)

      4 replies →

  • What about VVC (H.266)?

    https://en.wikipedia.org/wiki/Versatile_Video_Coding

    • VVC has a lot of the same issues that plagued HEVC adoption. There really isn't much reason to not use AV1 if you want the best codec currently.

      5 replies →

    • Best in terms of compression efficiency. Although x266 has been delayed by 6 months and counting.

      But it is currently being used in India and China already. So its usage is much bigger than a lot of people would imagine.

  • HEVC and AV1 is twice more magic. It keep the same quality to human eyes while reducing the bitrate by 50%.

    HEVC is patent encumbered. Users who use old hardware don't have hardware decoder. So both requires more time to be adopted.

  • Maybe I need to change some options, but I find whatever encoder I get when I ask ffmpeg to encode h265 takes a very long time. Then decoding 4k h265 is very slow on most of my PCs.

    It sure gets the file size down though.

> Suppose you have some strange coin - you've tossed it 10 times, and every time it lands on heads. How would you describe this information to someone? You wouldn't say HHHHHHHHH. You would just say "10 tosses, all heads" - bam! You've just compressed some data!

There appears to be some lossy compression on that string of "H"s.

I remember when h264 appeared.

At that time, I was obsessed with mplayer and would download and build the latest releases on a regular basis. The first time I got an h264 file, mplayer wouldn’t read it so I had to get the dev version and build it.

It worked, and I realized two things : the quality was incredible, and my athlon 1800+ couldn’t cope with it. Subsequent mplayer versions ( or libavcodec ?) vastly improved performance, but I still remember that day.

  • Yep. Same here. Man, I have not used mplayer in a very long time. But, it was the bee's knees.

    I used to work for a company that was developing some video based products. And this other company in Vegas sold our bosses a "revolutionary video codec" and a player. We had to sign NDAs to use it.

    I used it and observed that it worked like mplayer. Too much like it. 5 more minutes of investigation and the jig was up. Our execs who paid a lot of money to the Vegas company had an egg on their face.

    It is shockingly easy to scam non-tech decisions makers in the tech field. They are too worried about being left behind. Smart ones will pull in a "good" engineer for evaluation. Dunning Kruger subjects will line up with their wallets out.

Story time.

Back in 1999, I was leaving a startup that had been acquired, but I didn't want to stick around. I had been doing in mpeg encoding; this is relevant later.

One of the companies I interviewed with had come up with a new video compression scheme. They were very tight lipped but after signing NDA paperwork, they showed me some short clips they had encoded/decoded via a non-realtime software codec. I was interviewing to create an ASIC version of the algorithm. Even seeing just a minute or two of their codec output, I guessed what they were doing. I suggested that their examples were playing to the strengths of their algorithm and suggested some scenarios that would be more challenging. I also described what I thought they were doing. They neither confirmed nor denied, but they had me come back for a second round.

In the second round I talked with the founders, a husband/wife CEO/CTO (IIRC) team. That is when I learned their plan wasn't to sell ASICs, but they wanted to keep their codec secret and instead create DSL-based cable network using the ASICs for video distribution. I said something like, it sounds like you have invented a better carburetor and instead of selling carburetors you are planning on building a car factory to compete with GM. It was cheeky, and they didn't take it well.

Getting to the point of how this story relates to H.264, their claim was: conventional compression has reached the limit, and so their codec would allow them to do something nobody else can do: send high-def video over DSL lines. I replied: I think compressors will continue to get better, and even if not, higher speed internet service will eventually come to houses and remove the particular threshhold they think only they can cross. Oh, no, they replied, physics dictates the speed bits can be sent on a wire and we are at that limit.

I didn't get (and didn't want) a job offer. The company did get some VC money but folded a few years later, much more efficient codecs were developed by others, and 2 Mbps internet connections were not a limit. I'm sure the actual algorithm (which I describe later at a high level) had a lot of clever math and algorithmic muscle to make it work -- they weren't stupid technically, just stupid from a business sense.

This retelling makes me sound like a smug know-it-all, but it is one of two times in my life where I looked at something and in seconds figured out their secret sauce. There are far more examples where I am an idiot.

What was their algorithm? They never confirmed it, but based on silhouette artifacts, it seemed pretty clear what they were doing. MPEG (like jpeg) works by compressing images in small blocks (8x8, 16x16, and a few others). That limits how much spatial redundancy can be used to compress the image, but it also limits the computational costs of finding that redundancy. Their codec was similar to what Microsoft had proposed for their Talisman graphics architecture in the late 90s.

From what I could tell, they would analyze a sequence of frames and rather than segmenting the image into fixed blocks like mpeg, they would find regions with semi-arbitrary boundaries that were structurally coherent -- eg, if the scene was a tennis match, they could tell that the background was pretty "rigid" -- if a pixel appeared to move (the camera panned) then the nearby pixels were highly likely to make the same spatial transformation. Although each player changed frame to frame, that blob had some kind of correlation in lighting and position. Once identified, they would isolate a given region and compress that image using whatever (probably similar to jpeg). In subsequent frames they'd analyze the affine (or perhaps more general) transformation from a region from one frame to the next, then encode that transformation via a few parameters. That would be the basis of the next frame prediction, and if done well, not many bits need to be sent to fix up the misprediction errors.

  • I couldn't remember the name of the company, nor the founders, but google found it for me:

    https://www.zdnet.com/article/high-hopes-for-video-compressi...

    It says they raised $32M from VCs, came out of stealth mode in 2002. I was unable to find what happened after that.

    • I'm always amused when I read an article from years ago that uses perfectly-fine-but-quaint terminology:

      > ...that hope to deliver broadcast quality programming over the Internet--the holy grail for nascent video-on-demand (VOD) services

      What year did we all suddenly stop using the term "video-on-demand" and start saying "streaming"? I don't remember it happening, but obviously it did.

      Well at least "broadcast quality" still means the same thing. An ancient antenna on my roof can still kick Youtube's ass when it comes to clarity and resolution.

      3 replies →

  • > they weren't stupid technically, just stupid from a business sense

    I wouldn’t call them “stupid technically”, but assuming that there was a hard limit to codec efficiency and to internet bandwidth would at least make them technically naive in my opinion.

This is very cool but the use of the terms "Information Entropy" together as if they were a separate thing is maybe the furthest that any "ATM-machine"-type phrase has rustled my jimmies.

It is a cool article, just, wowzers, what a phrase.

in 02016 it was patented magic in many countries. now the patents have all expired, or will in a few months, since the standard was released in august 02004 after a year of public standardization work, patents only last 20 years from filing, and you can't file a patent on something that's already public (except that, in the usa, there's a one-year grace period if you're the one that published it). if there are any exceptions, i'd be very interested to hear of them

userbinator points at https://meta.m.wikimedia.org/wiki/Have_the_patents_for_H.264..., but most of the patents there have a precedence date after the h.264 standard was finalized, and therefore can't be necessary to implement h.264 itself (unless the argument is that, at the time it was standardized, it was not known to be implementable, a rather implausible argument)

what's surprising is that the last 20 years have produced a few things that are arguably a little better, but nothing that's much better, at least according to my tests of the implementations in ffmpeg

it seems likely that its guaranteed patent-free status will entrench it as the standard codec for the foreseeable future, for better or worse. av1 has slightly better visual quality at the same bandwidth, but is much slower (possibly this is fixable by a darktangent), but it's vulnerable to patents filed by criminals as late as 02018

  • but it's vulnerable to patents filed by criminals as late as 02018

    Ah, if we get through year 2038 we get latent free AV1!

  • why do you put an extra zero in front of the year?

    • Because he's a true believer in the Long Now religion. But not enough of a true believer that he uses six or seven digits ;-)

      Also, he uses that (and no capital letters) as a "bozo filter" - anyone who replies about that rather than the substance of the comment, he defines as someone he doesn't want to talk to anyway.

      Personally, I think it's rather an obnoxious move. He's deliberately derailing discussion about the content, and he has decent content. But he insists on playing this game instead of just talking about the substance.

      4 replies →

> Because you've asked the decoder to jump to some arbitrary frame, the decoder has to redo all the calculations - starting from the nearest I-frames and adding up the motion vector deltas to the frame you're on - and this is computationally expensive, and hence the brief pause.

That was 2016. Today, it's because Youtube knows you're on Firefox.

My favorite part of these codecs is how the discrete cosine transform plays so well together with quantization, zig-zag scanning and entropy coding.

The core of any lossy visual compressor is approximately the same theme. Those I-frames are essentially jpegs.

Lossy compression is possible in PNG to a certain degree. This is the "same PNG", but 94 kB instead of 1015 kB:

1015 kB: https://sidbala.com/content/images/2016/11/outputFrame.png

94 kB: https://www.photopea.com/keynote.png

  • Thats not PNG being lossy, the image was preprocessed to lose information by reducing color count before being compressed with PNG.

    • That is what a "lossy compression" means. Instead of encoding the original image, you look for a similar image, that can be losslessly encoded into a smaller file.

      In PNG, you look for an image with 100 - 200 colors (so that colors repeat more often) and losslessly encode it with DEFLATE.

      In JPG, you look for an image with with a reduced description through DCT, and losslessly encode it with Huffmann trees.

      1 reply →

V-Nova has developed a very interesting video compression algorithm, now in use at some major media companies [0]. It is software but it will also be built-in in certain hardware for Smart TVs, etc.

Disclosure: I'm an investor.

[0]: https://www.v-nova.com/

One of my surprises when I started to investigate video encoders was that, because the details in a scene comes at the high frequency, you can drop the bandwith requirements on the fly, by just not sending the details for a frame.

I think VVC is even better, MXPlayer streaming already has shows streaming in VVC in India! I dont know how they're doing it but its live. VVC is supposedly 20-30% more efficient than av1

To nitpick, comparing with PNG is misleading, because it's comparing a lossless format with lossy. a JPEG would be around the same size has H.264.