[PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Archive of historical development discussions
Discussions / Development has moved to GitHub
Forum rules
*******************************
Please be aware we are now using GitHub for issue tracking and feature requests.
- This section of the forum is now closed to new topics.

*******************************
Post Reply
jeremy33
Posts: 12
Joined: Wed Oct 19, 2011 11:01 pm

[PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by jeremy33 »

Hi,

I made a patch to make colorspace conversion (BT709 <-> BT601) from this "unofficial version" of handbrake (thanks to the author golgol7777) :

http://pastebin.com/BCqkNH0x

Colorspace conversion is very important to make HD to SD encode if you want to have the right colors.

You can use this feature like this :
BT709 -> BT601
HandBrakeCLI -i movie_source.mkv -o movie_dest.mkv -e x264 -q 20 -w 720 --colorspace 709:601

I'm not a developer but everything seems to be ok ;)

Thanks
User avatar
JohnAStebbins
HandBrake Team
Posts: 5712
Joined: Sat Feb 09, 2008 7:21 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by JohnAStebbins »

If you are encoding to h.264, the color matrix code gets written to the video stream. If you are encoding to mp4, the color matrix code gets written to the container. So this filter is not necessary if you are using the x264 encoder or the mp4 container. The player should read and honor the color matrix code it reads.

This filter would only be necessary if you are transcoding to mpeg-4 or theora and you are using the mkv container. Or if your player is broken.

Thanks for the submission. I'll have to dig into how translation between colorspaces works though before this could go into the code base. It looks more complex than it needs to be. This should be as simple as a 3x3 matrix multiplication on each pixel.
jeremy33
Posts: 12
Joined: Wed Oct 19, 2011 11:01 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by jeremy33 »

I know that the color matrix code can be written to the container or the video stream but I think there are not many player that take into account those informations unfortunately.
User avatar
JohnAStebbins
HandBrake Team
Posts: 5712
Joined: Sat Feb 09, 2008 7:21 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by JohnAStebbins »

Hmm, well, if your player doesn't respect the color matrix settings, then transforming the pixels to another colorspace really doesn't make any difference. One color space is just as wrong as the other if the player isn't doing the right thing.
jeremy33
Posts: 12
Joined: Wed Oct 19, 2011 11:01 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by jeremy33 »

If you encode your video in the right colorspace it does not matter that the player takes into account or not the color matrix info, the output will be ok I think.

Any way when you encode HD to SD you have to convert the color matrix from bt709 to bt601

http://forum.doom9.org/showthread.php?t ... ost1090068
When should you use Colormatrix? (assuming HD >= 720 vertical lines i.e. 720p/1080i/1080p/etc.) :
-for downscaling HD to SD resolutions, you should use Colormatrix (from 709 to 601)
-i.e. to convert 720p HD DVD content to regular DVD content, use the 709 to 601 conversion and then a bilinear resize (preferable for downscaling)
-for upscaling SD to HD resolutions, you should use Colormatrix (from 601 to 709)
-i.e. to convert DVD content to 720p HD DVD content, use the 709 to 601 conversion and then the upscaling resizer of your choice
-for fixing a previously rescaled source (SD->HD or HD->SD) that didn't have it's colorimetry changed at the same time
-for fixing a source that appears to have the wrong colorimetry information (as determined by eye, by calibration tools, by comparison with another original source, by
personal preference)
-for changing a source that has an unusual colorimetry-resolution combination (e.g. BT.709 colorspace video rescaled to put on a DVD may not display the right colors on
devices that make decisions about colorimetry based on resolution. Colormatrix can be used to change the colorspace to better fit the expected norms.)
-for converting a BT.601 source into BT.709 before feeding it to an encoder expecting BT.709
-for converting a BT.709 source into BT.601 before feeding it to an encoder expecting BT.601
User avatar
JohnAStebbins
HandBrake Team
Posts: 5712
Joined: Sat Feb 09, 2008 7:21 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by JohnAStebbins »

HD or SD has nothing to do with which color space must be used. It just happens that certain HD specifications state that BT.709 will be used and certain SD specifications state the BT.601 will be used. But BT.709 is not universally used for HD and BT.601 is not universally used for SD. There are many other colorspace formats in addition to these 2 that could be used. The flags in the stream are the only way for a player to definitively know which standard to use.

BT.601 was chosen in the days of analog SD video because the color primaries used most closely matched the color of the phosphors of the TVs of the time. BT.709 was selected for HD because the color primaries more closely matches the color of the phosphors in todays HD displays. So, if your player does not compensate for the color matrix and if your source was HD with BT.709 color and if you are playing back on a monitor whose primary colors match those of BT.709, then you are seeing the right colors, regardless of what resolution you changed the video to during transcode. If any one of those things is not true, you are seeing incorrect color.

Note that to really see correct colors, the player must know the primaries of the display and know the primaries of the source video and translate between them.
jeremy33
Posts: 12
Joined: Wed Oct 19, 2011 11:01 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by jeremy33 »

I agree with you but Blu-ray and HDTV are bt709 and DVD, NTSC, PAL and SD-TV/videos are bt601, so the vast majority of HD content are bt709 and the vast majority of SD content are bt601.

If threre is no color matrix info or if the player don't takes into account the color matrix info the player may check the resolution to output bt709 or bt601
Uses the information from the stream (H.264 only). Otherwise ffdshow switches between BT.601 and BT.709 when the following resolution conditions are met:
width > 1024 or height >= 600: BT.709
width <=1024 and height < 600: BT.601

ITU-R BT.601
DVD, NTSC, PAL and SD-TV/videos use this.

ITU-R BT.709
Blu-ray and HD-TV use this.
So, in this case, it is really usefull to covert bt709 to bt601 ;)
User avatar
JohnAStebbins
HandBrake Team
Posts: 5712
Joined: Sat Feb 09, 2008 7:21 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by JohnAStebbins »

That I can agree with. HandBrake does the same thing.

But except for the cases I noted, HandBrake also writes the color matrix to the output. So a player that honors color matrix will show correct colors without any transform, and a player that does not honor the color matrix will (almost) *never* show correct colors.
Deleted User 11865

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by Deleted User 11865 »

On an unrelated note, this unofficial repo features a few gems:

http://code.google.com/p/handbrake/sour ... ef09d1f204

There may be issues re:GPL and QuickTime being an optional, third-party install under Windows, but at least for people who build their own HB it's pretty cool.

There are also a bunch of minor fixes: http://code.google.com/p/handbrake/sour ... 3508309dbf and some build system fixes.
User avatar
s55
HandBrake Team
Posts: 10350
Joined: Sun Dec 24, 2006 1:05 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by s55 »

Yeh, it would definatly be worth bringing a couple of those back in.

I'm not sure, but I don't think they link against any qt libs, so I'm not sure if that would still fall foul of the GPL. I'd need to re-read it again.
jeremy33
Posts: 12
Joined: Wed Oct 19, 2011 11:01 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by jeremy33 »

JohnAStebbins, did you have some time to check my patch ?
I hope it will be commited soon ;)
User avatar
s55
HandBrake Team
Posts: 10350
Joined: Sun Dec 24, 2006 1:05 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by s55 »

@jeremy33, we are in code-freeze right now for the upcoming release, so as soon as that's out, the backlog of patches and get looked into.

You've not been forgotten ;)
jeremy33
Posts: 12
Joined: Wed Oct 19, 2011 11:01 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by jeremy33 »

Ok good news ;)

Thank you
Deleted User 13735

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by Deleted User 13735 »

Not to rain on your parade, but the end differences between 601 and 709 are so small as to be insignificant for all but the most technical applications. There is a tiny (politically inflicted) shift in the greens, nothing else. It is so small that DSLR manufacturers and most broadcasters ignore it completely, rather than make a conversion, although doing so would be "correct."

That being said, a CLI option to convert colorspace RGB<->601/709 would be most useful, since x264 by itself gets it wrong with many RGB sources, and mangles the luminance range. There are a couple of ways to achieve this in AviSynth, and the ability to do so in Handbrake would open the door to more source video options than is currently practical, if one wishes to maintain colorspace integrity throughout the delivery chain.

viewtopic.php?f=5&t=22556&p=104089&hilit=pc+709#p104089
jeremy33
Posts: 12
Joined: Wed Oct 19, 2011 11:01 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by jeremy33 »

I propose this feature 6 months ago and it's still not integrated :cry:
User avatar
s55
HandBrake Team
Posts: 10350
Joined: Sun Dec 24, 2006 1:05 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by s55 »

Pushed this onto our reviewboard so it can have a better chance at not getting forgotten about
https://reviews.handbrake.fr/r/305/
User avatar
JohnAStebbins
HandBrake Team
Posts: 5712
Joined: Sat Feb 09, 2008 7:21 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by JohnAStebbins »

heh, I have the forum post bookmarked in my handbrake todo folder. But putting it on review board is a much better solution.
jeremy33
Posts: 12
Joined: Wed Oct 19, 2011 11:01 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by jeremy33 »

It's been almost 18 months since I posted this patch, which seems to me essential for encoding software, and you don't have included it.
Why ?
Deleted User 11865

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by Deleted User 11865 »

We have limited time and this isn't high-priority, at least not to me.
jeremy33
Posts: 12
Joined: Wed Oct 19, 2011 11:01 pm

Re: [PATCH] Colorspace conversion BT709 <-> BT601 (HD->SD)

Post by jeremy33 »

OK but the patch works (still works ?) as is.
That was not a lot of work to check it and apply it. I didn't code that patch, just adapt it, but I took me some time to adapt it for nothing.
That's not cool :(
Post Reply