/********************************************************************************
 *																				*	
 * This program is free software; you can redistribute it and/or modify			*
 * it under the terms of the GNU General Public License as published by			*
 * the Free Software Foundation; either version 2 of the License, or			*
 * (at your option) any later version.											*
 *																				*
 * The GPL can be found at: http://www.gnu.org/copyleft/gpl.html				*
 *																				*
 * Author: 																		*
 *																				*	 
 *	Pedro Mateu		(Pm):														*
 *																				*
 ********************************************************************************/

// Not ready yet

#ifdef Direct_YUV
#include "global.h"

#define _S(a) (a)<0 ? 0 : (a)>255? 255:(a) 
//#define _S(a) LimitValue[a];
#define _R(y,u,v) ((y)  			       + 0x3343*(u)) /0x2000
#define _G(y,u,v) ((y) - 0x0c92*(v) - 0x1a1e*(u)) /0x2000
#define _B(y,u,v) ((y) + 0x40cf*(v))					     /0x2000

//#define _Ps565(r,g,b) (unsigned short)((r >> 3) | (((g & 0xFC) << 3)) | (((b & 0xF8) << 8)))
#define _Ps565(r,g,b) ( ((r & 0xF8) >> 3) | (((g & 0xFC) << 3)) | (((b & 0xF8) << 8)))
#define _Ps5652(r,g,b) ( ((r & 0xF8) << 13) | (((g & 0xFC) << 19)) | (((b & 0xF8) << 24)))

static void yuv_to_rgb16(unsigned char *Output, unsigned int xStep, unsigned int yStep, unsigned char *Y, unsigned char *U, unsigned char *V, unsigned int Stride_Y) {
	unsigned int i;
	unsigned int j;
	int r,g,b,first_pix,temp_y,temp_Guv,temp_Guv2,temp_Guv1;
	int y,u,v;
	for (j=0;j<8;j++)
	{
		for (i=0;i<16;i++)
		{
			temp_y=9576*(Y[i]-16);
			temp_Guv=-3218*(U[i>>1]-128)-6686*(V[i>>1]-128);
			temp_Guv2=16591*(V[i>>1]-128);
			temp_Guv1=13123*(U[i>>1]-128);
			r = _S(((temp_y+temp_Guv1)>>13)); 
			g = _S(((temp_y+temp_Guv)>>13)); 
			b = _S(((temp_y+temp_Guv2)>>13)); 
			first_pix = _Ps565(r,g,b);
			i++;
			temp_y=9576*(Y[i]-16);
			r = _S(((temp_y+temp_Guv1)>>13)); 
			g = _S(((temp_y+temp_Guv)>>13)); 
			b = _S(((temp_y+temp_Guv2)>>13)); 
			*((unsigned int *)Output) = first_pix | (_Ps5652(r,g,b)); 
			i--;
			temp_y=9576*(Y[i+Stride_Y]-16);
			r = _S(((temp_y+temp_Guv1)>>13)); 
			g = _S(((temp_y+temp_Guv)>>13)); 
			b = _S(((temp_y+temp_Guv2)>>13)); 
			first_pix = _Ps565(r,g,b);
			i++;
			temp_y=9576*(Y[i+Stride_Y]-16);
			r = _S(((temp_y+temp_Guv1)>>13)); 
			g = _S(((temp_y+temp_Guv)>>13)); 
			b = _S(((temp_y+temp_Guv2)>>13)); 
			*((unsigned int *)(Output+yStep)) = first_pix | (_Ps5652(r,g,b)); 

			Output += (xStep<<1);
		}
		Output += ((yStep<<1) - (16*xStep));
		Y+=Stride_Y<<1;
		//if (j&1)
		//{
			U+=(Stride_Y>>1);
			V+=(Stride_Y>>1);
		//}
	}
}

void Direct_YUV_to_RGB(int bx, int by)
{
	unsigned char *rfp,*rfp2,*rfp3;
	unsigned int stride_dest;
	bx <<= 4;
	by <<= 4;
	// frame DCT coding
	rfp = frame_ref[0] + mp4_hdr.coded_picture_width * by + bx;
	stride_dest=640 * by + (bx<<1);
	// pixel coordinates
	bx >>= 1;
	by >>= 2;  //chrominance  
	// frame DCT coding
	rfp2 = frame_ref[1] + mp4_hdr.coded_picture_width * by + bx;
	rfp3 = frame_ref[2] + mp4_hdr.coded_picture_width * by + bx;
	if ((bx<<1)<320)
	yuv_to_rgb16(((uint8_t *)0xAC050020)+stride_dest, 2,640,rfp,rfp2,rfp3,mp4_hdr.coded_picture_width);
}


#endif