Skip to content

Commit

Permalink
optimized yuv to rgb conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jamezilla committed Jun 6, 2011
1 parent 1957d52 commit 97117bc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -21,3 +21,4 @@ ipch/
bin/
libs/*.dll
ignore/
*.bak
2 changes: 1 addition & 1 deletion examples/blackmagicExample.vcproj
Expand Up @@ -144,7 +144,7 @@
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="openframeworksLib.lib OpenGL32.lib GLu32.lib kernel32.lib setupapi.lib glut32.lib rtAudio.lib videoInput.lib libfreetype.lib FreeImage.lib qtmlClient.lib dsound.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib GLee.lib fmodex_vc.lib glu32.lib PocoFoundationmt.lib PocoNetmt.lib PocoUtilmt.lib PocoXMLmt.lib"
AdditionalDependencies="boost_thread-vc90-mt-1_44.lib boost_date_time-vc90-mt-1_44.lib comsuppw.lib cv110.lib cxcore110.lib openframeworksLib.lib OpenGL32.lib GLu32.lib kernel32.lib setupapi.lib glut32.lib rtAudio.lib videoInput.lib libfreetype.lib FreeImage.lib qtmlClient.lib dsound.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib GLee.lib fmodex_vc.lib glu32.lib PocoFoundationmt.lib PocoNetmt.lib PocoUtilmt.lib PocoXMLmt.lib"
LinkIncremental="1"
AdditionalLibraryDirectories="..\..\..\addons\ofxOpenCv\libs\opencv\lib\vs2008;&quot;C:\Program Files (x86)\boost\boost_1_44\lib&quot;;..\..\..\libs\glut\lib\vs2008;..\..\..\libs\rtAudio\lib\vs2008;..\..\..\libs\FreeImage\lib\vs2008;..\..\..\libs\freetype\lib\vs2008;..\..\..\libs\quicktime\lib\vs2008;..\..\..\libs\fmodex\lib\vs2008;..\..\..\libs\videoInput\lib\vs2008;..\..\..\libs\glee\lib\vs2008;..\..\..\libs\glu\lib\vs2008;..\..\..\libs\Poco\lib\vs2008;..\..\..\libs\openFrameworksCompiled\lib\vs2008"
IgnoreAllDefaultLibraries="false"
Expand Down
52 changes: 34 additions & 18 deletions src/DLCapture.cpp
Expand Up @@ -15,6 +15,7 @@ DLCapture::DLCapture()
mPreviewHeight = -1;
capture_workers.size_controller().resize(3);
conversion_workers.size_controller().resize(mNumCores);
CreateLookupTables();
}

DLCapture::~DLCapture()
Expand Down Expand Up @@ -177,38 +178,53 @@ void
DLCapture::YuvToRgbChunk(BYTE *yuv, boost::shared_ptr<DLFrame> rgb, unsigned int offset, unsigned int chunk_size)
{
// convert 4 YUV macropixels to 6 RGB pixels
BYTE u, y1, v, y2;
// unsigned int j = 0;
unsigned int i, j;
unsigned int boundry = offset + chunk_size;
double val = 0;
int yy, uu, vv, ug_plus_vg, ub, vr;
int r,g,b;
for(i=offset, j=(offset/4)*6; i<boundry; i+=4, j+=6){
u = yuv[i];
y1 = yuv[i+1];
v = yuv[i+2];
y2 = yuv[i+3];

// bytes are reversed BGR
rgb->pixels[j] = Clamp(1.164 * (y1 - 16) + 2.115 * (v - 128));
rgb->pixels[j+1] = Clamp(1.164 * (y1 - 16) - 0.534 * (u - 128) - 0.213 * (v - 128));
rgb->pixels[j+2] = Clamp(1.164 * (y1 - 16) + 1.793 * (u - 128));

rgb->pixels[j+3] = Clamp(1.164 * (y2 - 16) + 2.115 * (v - 128));
rgb->pixels[j+4] = Clamp(1.164 * (y2 - 16) - 0.534 * (u - 128) - 0.213 * (v - 128));
rgb->pixels[j+5] = Clamp(1.164 * (y2 - 16) + 1.793 * (u - 128));
// j+=6;
yy = yuv[i+1] << 8;
uu = yuv[i] - 128;
vv = yuv[i+2] - 128;
ug_plus_vg = uu * 88 + vv * 183;
ub = uu * 454;
vr = vv * 359;
r = (yy + vr) >> 8;
g = (yy - ug_plus_vg) >> 8;
b = (yy + ub) >> 8;
rgb->pixels[j] = r < 0 ? 0 : (r > 255 ? 255 : (unsigned char)r);
rgb->pixels[j+1] = g < 0 ? 0 : (g > 255 ? 255 : (unsigned char)g);
rgb->pixels[j+2] = b < 0 ? 0 : (b > 255 ? 255 : (unsigned char)b);
yy = yuv[i+3] << 8;
r = (yy + vr) >> 8;
g = (yy - ug_plus_vg) >> 8;
b = (yy + ub) >> 8;
rgb->pixels[j+3] = r < 0 ? 0 : (r > 255 ? 255 : (unsigned char)r);
rgb->pixels[j+4] = g < 0 ? 0 : (g > 255 ? 255 : (unsigned char)g);
rgb->pixels[j+5] = b < 0 ? 0 : (b > 255 ? 255 : (unsigned char)b);
}
}


unsigned int
inline unsigned int
DLCapture::Clamp(double value)
{
if(value > 255.0) return 255;
if(value < 0.0) return 0;
return (unsigned int) value;
}

void
DLCapture::CreateLookupTables(void){
for(int i=0; i<256; i++) {
mYLookup[i] = 1.164 * (i - 16);
mU1Lookup[i] = 0.534 * (i - 128);
mU2Lookup[i] = 1.793 * (i - 128);
mV1Lookup[i] = 2.115 * (i - 128);
mV2Lookup[i] = 0.213 * (i - 128);
}
}

shared_ptr<DLFrame>
DLCapture::Resize(shared_ptr<DLFrame> src, int targetWidth, int targetHeight)
{
Expand Down
7 changes: 7 additions & 0 deletions src/DLCapture.h
Expand Up @@ -55,6 +55,7 @@ class DLCapture : public IDeckLinkInputCallback
void YuvToRgbChunk(BYTE *yuv, boost::shared_ptr<DLFrame> rgb, unsigned int offset, unsigned int chunk_size);
boost::shared_ptr<DLFrame> Resize(boost::shared_ptr<DLFrame> src, int targetWidth, int targetHeight);
void InitialiseDimensions(IDeckLinkVideoInputFrame* pArrivedFrame);
void CreateLookupTables(void);
unsigned int Clamp(double value);

int mDropNumFrames;
Expand All @@ -68,6 +69,12 @@ class DLCapture : public IDeckLinkInputCallback
// BMDFrameFlags mFlags;
bool mDimensionsInitialized;
int mNumCores;

double mYLookup[256];
double mU1Lookup[256];
double mU2Lookup[256];
double mV1Lookup[256];
double mV2Lookup[256];

boost::threadpool::pool capture_workers;
boost::threadpool::pool conversion_workers;
Expand Down

0 comments on commit 97117bc

Please sign in to comment.