[FFmpeg-devel] colorspace.h

Chris c319chris at aol.com
Thu Jun 4 11:14:43 CEST 2015

The existing code is such a jumble, I'm unable to figure out why it is coded the way it is. When I try to test it I get wacky results. I do get some inaccurate colors in files encoded by ffmpeg. I'm guessing this code may be the source of the inaccuracies. I have tested this fairly thoroughly by creating and directly encoding .bmp files to .webm. To check colors I use a test pattern containing the primary and secondary colors (e.g. cyan = 00ffff) and several levels of white, black and gray. The colors that come out of ffmpeg should be the same as those in the original .bmp image. The webm files are checked with Firefox and with VLC player.

The specific code I'm referring to is at the end of colorspace.h where it looks like they're trying to encode RGB to BT.601.

#define RGB_TO_Y(r1, g1, b1, shift)
#define RGB_TO_U(r1, g1, b1, shift)
#define RGB_TO_V(r1, g1, b1, shift)

Here is a demo program showing how I would code it. Note: it is very important to round the R,G and B values rather than letting C truncate them. I do this by adding 0.5. This code has been compiled and tested with the Pelles C compiler.
#include "..\include\stdio.h"

#define R601 0.299
#define G601 0.587
#define B601 0.114

#define R709 0.2126
#define G709 0.7152
#define B709 0.0722

float Cr,Cb,y;
int r, g, b;

void encode(int r, int g, int b, int colorspace) {

if(colorspace == 601){ //use BT.601 color space
y = (r*R601) + (g*G601) + (b*B601);
Cr = (r * R601) - y;
Cb = (b * B601) - y;

r = ((Cr + y) / R601) + 0.5;
g = -((Cr + y + Cb) / G601) + 0.5;
b = ((y + Cb) / B601) + 0.5;
  else { //use BT.709 color space
y = (r*R709) + (g*G709) + (b*B709);
Cr = (r * R709) - y;
Cb = (b * B709) - y;

r = ((Cr + y) / R709) + 0.5;
g = -((Cr + y + Cb) / G709) + 0.5;
b = ((y + Cb) / B709) + 0.5;

printf("%i\n", r);
printf("%i\n", g);
printf("%i\n", b);

int main(void){
// encode rgb colors
encode(255,128,0,709); //use 601 or 709 as last argument

-----Original Message-----
From: Michael Niedermayer <michaelni at gmx.at>
To: FFmpeg development discussions and patches <ffmpeg-devel at ffmpeg.org>
Sent: Wed, Jun 3, 2015 11:52 am
Subject: Re: [FFmpeg-devel] colorspace.h

On Tue, Jun 02, 2015 at 02:28:59AM -0400, Chris wrote:
> Hello -
> I have
found what I believe to be some questionable code in colorspace.h. I have
modified the code to what I believe is correct but am unable to compile ffmpeg
myself on Windows to test my modifications, despite spending many hours trying
to do so and reading several how-to's.
> We can discuss the code changes
here if people would like, but someone who is better equipped to compile ffmpeg
would have to compile it.

can you post a patch that shows the modifications
and explain what is
wrong before and how its improved


More information about the ffmpeg-devel mailing list