[Libav-user] JPEG YUV->RGB

Steffen Ebersbach ebersbac at hs-mittweida.de
Thu May 12 09:12:14 CEST 2011

Am 12.5.2011 09:02, schrieb Dmitriy K:
> Hi all!
> I use libav to decode MJPEG frames from IP-camera and to show some 
> pixels from them on a SDL-surface.
> SDL takes only RGB pixels so I have to convert YUV from MJPEG frame to 
> RGB.
> I have AVFrame after avcodec_decode_video() and as I see, there 
> data[0] stands for Y component, data[1] - V component and data[2] - U 
> component.
> Also, U and V components arrays are twice shorter, so that 1 U or V 
> component stands for 2 Y components.
> Then I use formulas found in wikipedia to convert YUV to RGB:
Yes it's the YUV 4:2:2 format, that is often use in JPEG.

> R=Y + 1.370705*(U - 128);
> G=Y - 0.698001*(V - 128) - 0.337633*(U - 128);
> B=Y + 1.732446*(V - 128);
> But in final picture I got colors mismatch in highly white and highly 
> black regions.
> For example in pixels that should be black, I have blue or red pixels, 
> and in white regions - only green.
> So I have two questions:
> 1) Am I right in my vision of YUV components position in AVFrame->data 
> arrays ?
> 2) Is there any way to solve problems with colors mismatch?

The formulas are right, but this is not recommend. In libav you can use 
the swscale libary to do this conversion. It's an easy way for this task.


More information about the Libav-user mailing list