Live data from Hacker News

Adventures with ffmpeg and color ranges

facebook.com

21–30 of 94 posts

Re: Adventures with ffmpeg and color ranges

#21
post #7

> This is usually visible as ugly banding or blocking in dark scenes. I was just complaining about this a couple weeks ago with some friends after the “Dark” episode of GoT. I was saying the codecs don’t seem to have caught up or they are just being fed at too low a bit rate to really excel at dark scenes. It’s hard to imagine this is just a scaling issue with the color ranges, and if so, how could it possibly not ha…

It's not; full-range YCbCr doesn't exist anywhere in the "pro" or broadcast pipelines that would have been used for GoT.

I think he meant to say that the limited dynamic range of 8-bit video manifests most obviously in dark scenes, since that's where standard gamma curves are the most imprecise relative to our vision (which was fine back when TVs couldn't reproduce that range anyway.) The 14% from full-range doesn't help that much; really you need the 4x finer precision you get from 10bit.

Re: Adventures with ffmpeg and color ranges

#22
post #8
post #4

Earlier quoted context omitted.

I suspect it’s not his decision to make.

You think someone is telling Carmack where he can and can't post? Why one earth would they do that? And why on earth would he put up with it?

It doesn't have to be coercive. Some of us would like to know what the people who hire us are up to even if we don't personally have any interest in it.

Re: Adventures with ffmpeg and color ranges

#24
John Carmack 7 hrs ·

Adventures with ffmpeg and color ranges.

A video legacy issue that can cause a lot of problems is the issue of “limited” versus “full” component range. For arcane reasons related to TV broadcast limitations, many video formats restrict the YUV color components to be in the 16..235 or 16..240 range instead of the full 0..255 range. Losing 14% of the already-barely-enough 8 bit dynamic range is bad enough, but it also often results in the black range starting at a quite visible grey value because most players don’t rescale the range. This is usually visible as ugly banding or blocking in dark scenes.

For ffmpeg, the trick to avoid this is to use ‘-pix_fmt yuvj420p’, which says to use the j-for-jpeg full range in a 420 YUV subsampled p-for-planar format.

If you are starting with either RGB images, a 10/12 bit format, or a yuvj420p format video as input, then with the libx264 codec, you would get a full range output. Note that any video processing tools used along the way could also limit the range, and once it is gone, there is no getting it back, so you must be very careful and check your entire pipeline!

When you us this format, ffmpeg complains about ‘deprecated pixel format used, make sure you did set range correctly’, but you should ignore this warning.

Ffmpeg would like the world to move to specifying the range independently from the YUV channel subsampling and layout:

Setting ‘-color_range 0 -pix_fmt yuv420p’ makes the output format yuv420p Setting ‘-color_range 1 -pix_fmt yuv420p’ makes the output format yuv420p(tv) Setting ‘-color_range 2 -pix_fmt yuv420p’ makes the output format yuv420p(pc)

Unfortunately, this isn’t yet uniformly handled throughout all the internal format tests.

The libx265 integration in ffmpeg didn’t support the deprecated yuvj420p pixel format, only the basic yuv420p one, and no matter what I did, my test videos were always coming out limited range. It didn’t matter if you add a ’-color_range 2’, or ‘-x265-params range=full’. Those will change the settings in the VUI (Video Usability Information) section of the output, but the values are still compressed to the limited range.

Regardless of the input data, any 8 bit h265 video coming out of ffmpeg was limited range!

I walked through the libx265 code looking for range compression, but it turned out that all the damage was being done by ffmpeg before it got to x265. Ffmpeg will automatically convert formats when the output differs from the input, and since x265 only supported yuv420p, any full range input will be processed.

Adding ‘-v 48’ to ffmpeg will dump more information, including this auto_scaler invocation, which is what is killing the full color range: [auto_scaler_0 @ 000001f55ecd1a40] w:2048 h:2048 fmt:bgr24 sar:0/1 -> w:2048 h:2048 fmt:yuv420p sar:0/1 flags:0x4

I was about to start hacking the code to at least do what I wanted for my use case, but I tried an appeal to Twitter:

https://twitter.com/ID_AA_Carmack/status/1131715388067274753

My suspicions were confirmed, but Derek Buitenhuis went ahead and submitted an official patch to get yuvj420p accepted by libx265, and windows builds are already available at https://ffmpeg.zeranoe.com/builds/.

I suspect there was probably some way of working around this involving explicit format conversion filters with -src_range and -dst_range overrides, but this is now working as you would expect it:

ffmpeg -i source.mp4 -c:v libx265 -pix_fmt yuvj420p dest.mp4

Bravo to ffmpeg!

Re: Adventures with ffmpeg and color ranges

#25
post #7

> This is usually visible as ugly banding or blocking in dark scenes. I was just complaining about this a couple weeks ago with some friends after the “Dark” episode of GoT. I was saying the codecs don’t seem to have caught up or they are just being fed at too low a bit rate to really excel at dark scenes. It’s hard to imagine this is just a scaling issue with the color ranges, and if so, how could it possibly not ha…

>I have a high end OLED screen in a very dark room, and I was very disappointed by the picture quality.

Unfortunately for you, high end OLED in a very dark room is literally the most likely conditions under which you'd be annoyed by these problems... :-)

HDR is the way to go.

Re: Adventures with ffmpeg and color ranges

#26
post #4

Earlier quoted context omitted.

I suspect it’s not his decision to make.

That's totally absurd. Luminaries like Carmack are anything but prisoners; any firm in SV would pay big bucks to say "John Carmack works here". Facebook may be a big conglomerate these days, and they may not be the cool kid on the block anymore, but that doesn't mean that they're idiots. If anything, they treasure the presence of someone like Carmack all the more, because it's no longer reputionally-free for people l…

I don’t mean to say he’s “held prisoner”, more that the company culture is to post things using the company’s platform, and he’d rather not stick his head out too far from that path. (Which I guess could be seen as “him making a decision”. But it’s not a decision made in a vacuum.)

Re: Adventures with ffmpeg and color ranges

#27
post #8
post #4

Earlier quoted context omitted.

I suspect it’s not his decision to make.

You think someone is telling Carmack where he can and can't post? Why one earth would they do that? And why on earth would he put up with it?

More that the company’s culture says you should post things using the company’s platform, and to not do so might be making a statement he doesn’t want to make.

Otherwise, why would he move away from .plan?

Re: Adventures with ffmpeg and color ranges

#28
post #4

Earlier quoted context omitted.

I suspect it’s not his decision to make.

John Carmack can blog about ffmpeg bugs wherever he wants. Like there is no way stopping him from doing that is even remotely legal. He probably just posts on facebook because it's convenient and reaches a lot of people.

I don’t mean that he is legally bound to the platform. Just that the company culture is to use the platform, and he may see not using the platform as making a statement he does not want to make.

Re: Adventures with ffmpeg and color ranges

#29

Earlier quoted context omitted.

That's totally absurd. Luminaries like Carmack are anything but prisoners; any firm in SV would pay big bucks to say "John Carmack works here". Facebook may be a big conglomerate these days, and they may not be the cool kid on the block anymore, but that doesn't mean that they're idiots. If anything, they treasure the presence of someone like Carmack all the more, because it's no longer reputionally-free for people l…

Hero worship aside, in the grand scheme of things Facebook, Carmack is of almost 0 importance.

Sure, but in the grand scheme of things Facebook, whose market cap currently sits at 526 billion dollars, the correspondingly inconsequential sum of something-something-millions of dollars is worth the potential benefit.

In exchange for their patronage, employers to the world's technical stars get to use their name in recruitment and other types of marketing, and they're well-positioned to profit from the star's next great achievement, if indeed one ever occurs. This is very similar to the benefit to a record label or a movie studio in signing a deal with the current one-hit wonder or starlet of the hour.

As in any employment transaction, there can be no true guarantees. None of us can promise that we'll be here tomorrow. But the value of investments in strong personal brands has proven profitable enough over the last several decades that many companies are eager and willing to make them. Entire industries are built around them. I don't think it's going to stop any time soon. cf. the Lindy Effect [0]

There is surely some limit to Facebook's tolerance of a single man's foibles, regardless of star power. I would bet my bottom dollar that "blogged off-Facebook" is nowhere near it.

[0] https://en.wikipedia.org/wiki/Lindy_effect

Re: Adventures with ffmpeg and color ranges

#30
post #2

Interesting find. I have encoded video for over a decade, and I don't think a two month period has gone by where I haven't learned something that convinces me to completely change how or what flags I'm using. This is way beyond that but still.

At the risk of telling you something you know, there's a lot more things to trip on with proper color space conversion than just going from scaled to full range color in 601. It's something of rat's nest to cover all the cases.

After ProRes became a thing, there was a lot of confusion where the metadata saved within the MOV started to include the HD vs SD color primaries. Some programs were able to correctly read this data. Most did not. Most programs did not include the data including Final Cut. The iTunes team started rejecting files exported from FCP because they claimed they were not made correctly and not a valid file. The iTunes team were just reading the spec and built their automation based on that, but not what the real world output from FCP was doing.
Post reply on HN