uniform sampler2DRect tex;
uniform sampler2DRect lut;

void main()
{
	vec4		now			= texture2DRect(tex, gl_TexCoord[0].st);
	vec4		nxt			= texture2DRect(tex, gl_TexCoord[1].st);
	
	now.a = 1.0;
	
	/* convert from rgb to yuv (constants from www.fourcc.org, but really from "Video Demystified") */
	const vec4	y_values	=	vec4( 0.2570,	 0.5040,	 0.0980,	16.0/255.0);
	const vec4	cb_values	=	vec4(-0.1480,	-0.2910,	 0.4390,	128.0/255.0);
	const vec4	cr_values	=	vec4( 0.4390,	-0.3680,	-0.0710,	128.0/255.0);
	
	float yy	= dot(now, y_values);
	float cb	= dot(now, cb_values);
	float cr	= dot(now, cr_values);
	
	float now_cb	= dot(nxt, cb_values);
	float now_cr	= dot(nxt, cr_values);
	
	/* avg between now and next (colors only, not YY -- it would blur with YY avg) */
	cb	= (cb + now_cb) / 2.0;
	cr	= (cr + now_cr) / 2.0;
	
	/* have to look up *256 since our color values are [0,1] */
	vec4 lookup		= texture2DRect(lut, vec2(cr,cb)*256.0);
	float alpha		= lookup.w;	/* z for big endian */
	cb				= lookup.z; /* w for big endian */
	cr				= lookup.x; /* y for big endian */
					
	gl_FragColor 	= vec4(yy,cb,cr,alpha);
/*
	gl_FragColor	= vec4(0, 1, 0, 1);
	gl_FragColor	= now;
*/
}
