[Gridflow-cvs] [svn] commit: r5385 - /trunk/src/libv4l.cxx

svn-gridflow at artengine.ca svn-gridflow at artengine.ca
Wed Apr 7 21:57:06 EDT 2010


Author: matju
Date: Wed Apr  7 21:57:05 2010
New Revision: 5385

Log:
copied new pixel converters from videodev.cxx

Modified:
    trunk/src/libv4l.cxx

Modified: trunk/src/libv4l.cxx
==============================================================================
--- trunk/src/libv4l.cxx (original)
+++ trunk/src/libv4l.cxx Wed Apr  7 21:57:05 2010
@@ -22,6 +22,7 @@
 */
 
 #include "gridflow.hxx.fcs"
+#include "colorspace.hxx"
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
@@ -211,78 +212,78 @@
 
 static uint8 clip(int x) {return x<0?0 : x>255?255 : x;}
 
-void FormatLibV4L::frame_finished (uint8 *buf) {
+void FormatVideoDev::frame_finished (uint8 *buf) {
 	string cs = colorspace->s_name;
 	int downscale = cs=="magic";
 	/* picture is converted here. */
-	int sy = dim->get(0)>>downscale;
-	int sx = dim->get(1)>>downscale;
-	int bs = dim->prod(1)>>downscale;
+	int sy = dim->get(0);
+	int sx = dim->get(1);
+	int bs = dim->prod(1); if (downscale) bs/=2;
 	uint8 b2[bs];
-	post("sy=%d sx=%d bs=%d",sy,sx,bs);
-	post("frame_finished, vp.palette = %d; colorspace = %s",vp.palette,cs.data());
-	if (vp.palette==V4L2_PIX_FMT_YVU420) {
-		GridOutlet out(this,0,cs=="magic"?new Dim(sy,sx,3):(Dim *)dim,cast);
+	//post("frame_finished sy=%d sx=%d bs=%d, vp.palette = %d; colorspace = %s",sy,sx,bs,vp.palette,cs.data());
+	if (vp.palette==V4L2_PIX_FMT_YUYV) {
+		uint8 *bufy = buf;
+		GridOutlet out(this,0,cs=="magic"?new Dim(sy>>downscale,sx>>downscale,3):(Dim *)dim,cast);
 		if (cs=="y") {
-			out.send(sy*sx,buf);
+		    for(int y=0; y<sy; bufy+=sx, y++) {
+			for (int x=0,xx=0; x<sx; x+=2,xx+=4) {
+				b2[xx+0]=YUV2Y(bufy[x+0],0,0);
+				b2[xx+1]=YUV2Y(bufy[x+2],0,0);
+			}
+			out.send(bs,b2);
+		    }
 		} else if (cs=="rgb") {
-			for(int y=0; y<sy; y++) {
-				uint8 *bufy = buf+sx* y;
-				uint8 *bufu = buf+sx*sy    +(sx/2)*(y/2);
-				uint8 *bufv = buf+sx*sy*5/4+(sx/2)*(y/2);
-				int Y1,Y2,U,V;
-				for (int x=0,xx=0; x<sx; x+=2,xx+=6) {
-					Y1=bufy[x]   - 16;
-					Y2=bufy[x+1] - 16;
-					U=bufu[x/2] - 128;
-					V=bufv[x/2] - 128;
-					b2[xx+0]=clip((298*Y1         + 409*V)>>8);
-					b2[xx+1]=clip((298*Y1 - 100*U - 208*V)>>8);
-					b2[xx+2]=clip((298*Y1 + 516*U        )>>8);
-					b2[xx+3]=clip((298*Y2         + 409*V)>>8);
-					b2[xx+4]=clip((298*Y2 - 100*U - 208*V)>>8);
-					b2[xx+5]=clip((298*Y2 + 516*U        )>>8);
-				}
-				out.send(bs,b2);
-			}
+		    for(int y=0; y<sy; bufy+=2*sx, y++) {int Y1,Y2,U,V;
+			for (int x=0,xx=0; x<sx*2; x+=4,xx+=6) {GETYUYV(x); YUV2RGB(b2+xx,Y1,U,V); YUV2RGB(b2+xx+3,Y2,U,V);}
+			out.send(bs,b2);}
+		} else if (cs=="rgba") {
+		    for(int y=0; y<sy; bufy+=2*sx, y++) {int Y1,Y2,U,V;
+			for (int x=0,xx=0; x<sx*2; x+=4,xx+=8) {GETYUYV(x); YUV2RGB(b2+xx,Y1,U,V); YUV2RGB(b2+xx+4,Y2,U,V);
+				b2[xx+3]=255; b2[xx+7]=255;}
+			out.send(bs,b2);}
 		} else if (cs=="yuv") {
-			for(int y=0; y<sy; y++) {
-				uint8 *bufy = buf+sx* y;
-				uint8 *bufu = buf+sx*sy    +(sx/2)*(y/2);
-				uint8 *bufv = buf+sx*sy*5/4+(sx/2)*(y/2);
-				int U,V;
-				for (int x=0,xx=0; x<sx; x+=2,xx+=6) {
-					U=bufu[x/2];
-					V=bufv[x/2];
-					b2[xx+0]=clip(((bufy[x+0]-16)*298)>>8);
-					b2[xx+1]=clip(128+(((U-128)*293)>>8));
-					b2[xx+2]=clip(128+(((V-128)*293)>>8));
-					b2[xx+3]=clip(((bufy[x+1]-16)*298)>>8);
-					b2[xx+4]=clip(128+(((U-128)*293)>>8));
-					b2[xx+5]=clip(128+(((V-128)*293)>>8));
-				}
-				out.send(bs,b2);
-			}
+		    for(int y=0; y<sy; bufy+=2*sx, y++) {int Y1,Y2,U,V;
+			for (int x=0,xx=0; x<sx*2; x+=4,xx+=6) {GETYUYV(x); YUV2YUV(b2+xx,Y1,U,V); YUV2YUV(b2+xx+3,Y2,U,V);}
+			out.send(bs,b2);}
 		} else if (cs=="magic") {
-			for(int y=0; y<sy; y++) {
-				uint8 *bufy = buf        +4*sx*y;
-				uint8 *bufu = buf+4*sx*sy+  sx*y;
-				uint8 *bufv = buf+5*sx*sy+  sx*y;
-				for (int x=0,xx=0; x<sx; x++,xx+=3) {
-					b2[xx+0]=bufy[x+x];
-					b2[xx+1]=bufu[x];
-					b2[xx+2]=bufv[x];
-				}
-				out.send(bs,b2);
-			}
+		    int sx2 = sx/2;
+		    for(int y=0; y<sy; bufy+=2*sx, y+=2) {
+ 			for (int x=0,xx=0; x<sx2; x+=3,xx+=3) {b2[xx+0]=bufy[x+0]; b2[xx+1]=bufy[x+1]; b2[xx+2]=bufy[x+3];}
+			out.send(bs,b2);
+		    }
+		}
+	} else if (vp.palette==V4L2_PIX_FMT_YVU420) {
+		uint8 *bufy = buf, *bufu = buf+sx*sy, *bufv = buf+sx*sy*5/4;
+		GridOutlet out(this,0,cs=="magic"?new Dim(sy>>downscale,sx>>downscale,3):(Dim *)dim,cast);
+		if (cs=="y") {
+		    out.send(sy*sx,buf);
+		} else if (cs=="rgb") {
+		    for(int y=0; y<sy; bufy+=sx, bufu+=(sx/2)*(y&1), bufv+=(sx/2)*(y&1), y++) {int Y1,Y2,U,V;
+			for (int x=0,xx=0; x<sx; x+=2,xx+=6) {GET420P(x); YUV2RGB(b2+xx,Y1,U,V); YUV2RGB(b2+xx+3,Y2,U,V);}
+			out.send(bs,b2);}
+		} else if (cs=="rgba") {
+		    for(int y=0; y<sy; bufy+=sx, bufu+=(sx/2)*(y&1), bufv+=(sx/2)*(y&1), y++) {int Y1,Y2,U,V;
+			for (int x=0,xx=0; x<sx; x+=2,xx+=8) {GET420P(x); YUV2RGB(b2+xx,Y1,U,V); YUV2RGB(b2+xx+4,Y2,U,V);
+			b2[xx+3]=255; b2[xx+7]=255;}
+			out.send(bs,b2);}
+		} else if (cs=="yuv") {
+		    for(int y=0; y<sy; bufy+=sx, bufu+=(sx/2)*(y&1), bufv+=(sx/2)*(y&1), y++) {int Y1,Y2,U,V;
+			for (int x=0,xx=0; x<sx; x+=2,xx+=6) {GET420P(x); YUV2YUV(b2+xx,Y1,U,V); YUV2YUV(b2+xx+3,Y2,U,V);}
+			out.send(bs,b2);}
+		} else if (cs=="magic") {
+		    int sx2 = sx/2;
+		    for(int y=0; y<sy/2; bufy+=2*sx, bufu+=sx2, bufv+=sx2, y++) {
+ 			for (int x=0,xx=0; x<sx2; x++,xx+=3) {b2[xx+0]=bufy[x+x]; b2[xx+1]=bufu[x]; b2[xx+2]=bufv[x];}
+			out.send(bs,b2);
+		    }
 		}
 	} else if (vp.palette==V4L2_PIX_FMT_RGB24) {
 		GridOutlet out(this,0,dim,cast);
-		uint8 rgb[sx*3];
+		uint8 rgb[sx*4];
 		uint8 b2[sx*3];
 		if (cs=="y") {
 			for(int y=0; y<sy; y++) {
-			        bit_packing->unpack(sx,buf+y*sx*bit_packing->bytes,rgb);
+			        bit_packing3->unpack(sx,buf+y*sx*bit_packing3->bytes,rgb);
 				for (int x=0,xx=0; x<sx; x+=2,xx+=6) {
 					b2[x+0] = (76*rgb[xx+0]+150*rgb[xx+1]+29*rgb[xx+2])>>8;
 					b2[x+1] = (76*rgb[xx+3]+150*rgb[xx+4]+29*rgb[xx+5])>>8;
@@ -291,25 +292,27 @@
 			}
 		} else if (cs=="rgb") {
 			for(int y=0; y<sy; y++) {
-			        bit_packing->unpack(sx,buf+y*sx*bit_packing->bytes,rgb);
-				out.send(bs,rgb);
+			        bit_packing3->unpack(sx,buf+y*sx*bit_packing3->bytes,rgb);
+			 	out.send(bs,rgb);
+			}
+		} else if (cs=="rgba") {
+			for(int y=0; y<sy; y++) {
+				uint8 *buf2 = buf+y*sx*bit_packing4->bytes;
+			        bit_packing4->unpack(sx,buf2,rgb);
+			        for (int x=0; x<sx; x++) buf2[4*x+3]=255; /* i hope this is correct. not tested. */
+			 	out.send(bs,rgb);
 			}
 		} else if (cs=="yuv") {
 			for(int y=0; y<sy; y++) {
-				bit_packing->unpack(sx,buf+y*sx*bit_packing->bytes,rgb);
-				for (int x=0,xx=0; x<sx; x+=2,xx+=6) {
-					b2[xx+0] = clip(    ((  76*rgb[xx+0] + 150*rgb[xx+1] +  29*rgb[xx+2])>>8));
-					b2[xx+1] = clip(128+((- 44*rgb[xx+0] -  85*rgb[xx+1] + 108*rgb[xx+2])>>8));
-					b2[xx+2] = clip(128+(( 128*rgb[xx+0] - 108*rgb[xx+1] -  21*rgb[xx+2])>>8));
-					b2[xx+3] = clip(    ((  76*rgb[xx+3] + 150*rgb[xx+4] +  29*rgb[xx+5])>>8));
-					b2[xx+4] = clip(128+((- 44*rgb[xx+3] -  85*rgb[xx+4] + 108*rgb[xx+5])>>8));
-					b2[xx+5] = clip(128+(( 128*rgb[xx+3] - 108*rgb[xx+4] -  21*rgb[xx+5])>>8));
+				bit_packing3->unpack(sx,buf+y*sx*bit_packing3->bytes,rgb);
+				for (int x=0,xx=0; x<sx; x++,xx+=3) {
+					b2[xx+0] = RGB2Y(rgb[xx+0],rgb[xx+1],rgb[xx+2]);
+					b2[xx+1] = RGB2U(rgb[xx+0],rgb[xx+1],rgb[xx+2]);
+					b2[xx+2] = RGB2V(rgb[xx+0],rgb[xx+1],rgb[xx+2]);
 				}
 				out.send(bs,b2);
 			}
-		} else if (cs=="magic") {
-			RAISE("magic colorspace not supported with a RGB palette");
-		}
+		} else if (cs=="magic") RAISE("magic colorspace not supported with a RGB palette");
 	} else {
 		RAISE("unsupported palette %d",vp.palette);
 	}



More information about the Gridflow-cvs mailing list