/**************************************************************************
 *																							*
 * This software is an implementation of a part of one or more MPEG-4 Video tools as        *
 * specified in ISO/IEC 14496-2 standard.  Those intending to use this						*
 * software module in hardware or software products are advised that its					*
 * use may infringe existing patents or copyrights, and any such use						*
 * would be at such party's own risk.  The original developer of this						*
 * software module and his/her company, and subsequent editors and their					*
 * companies, will have no liability for use of												*
 * this software or modifications or derivatives thereof.									*
 *																							*
 /***************************************************************************************
 *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						*
 *																						*																						*	
 *																							*
 *																							*
 * Authors:																					*
 *          Marc Dukette      
 *			Pedro Mateu
 **************************************************************************************/

#include <memory.h>

#include "portab.h"

#include "yuv2rgb.h"
//unsigned char LimitValues[768]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
//255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255};
#if defined(GAPI)||(defined(_USEVIDBUFF))
#ifdef MIPS
#ifdef HPC
	#define DisplayWidth 640
	#define DisplayHeight 240
	#define VisibleWidth 640
	#define VisibleHeight 240
#else
	#define DisplayWidth 256
	#define DisplayHeight 320
	#define VisibleWidth 240
	#define VisibleHeight 320
#endif
#else
#ifdef IPAQ
	#define DisplayWidth 320
	#define DisplayHeight 240
	#define VisibleWidth 320
	#define VisibleHeight 240
#else
#ifdef HPC
	#define VisibleWidth 640
	#define DisplayWidth 640
	#define VisibleHeight 240
	#define DisplayHeight 240
#else
	#define VisibleWidth 240
	#define DisplayWidth 240
	#define VisibleHeight 320
	#define DisplayHeight 320
#endif
#endif
#endif
#else
#ifdef HPC
	#define VisibleWidth 640
	#define DisplayWidth 640
	#define VisibleHeight 240
	#define DisplayHeight 240
#else
	#define VisibleWidth 240
	#define DisplayWidth 240
	#define VisibleHeight 320
	#define DisplayHeight 320
#endif
#endif


// 16 greyscale level - aero 1530,Monopaq
#ifdef _GREYSCALE_16
#define _S(a) (a)>255 ? 255 : a 

void yuv2rgb_565(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int stride_dest, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, __int8 slowopt, int invert) 
{
	register int x,y; 
	unsigned char *pus_out; 
	register unsigned int l,l2;					//luminance (brightness) value
	register unsigned int quant_error_l;		
	register unsigned int mask=0;
	if (stride_dest<0) stride_dest=0;
	if (Dither==1){mask=0x0F;}
	if ((height_y)>VisibleHeight) height_y=VisibleHeight;
	if ((width_y)>VisibleWidth) width_y=VisibleWidth;
	pus_out = (unsigned char *) puc_out; 
	quant_error_l=0;
	for (y=0; y<height_y; y++) 
	{ 
		for (x=0;x<width_y;x+=2)
		{ 
			l = _S(puc_y[x]+quant_error_l+Brightness); 
			quant_error_l=l&mask;
			l2 = _S(puc_y[x+1]+quant_error_l+Brightness);
			quant_error_l=l2&mask;
			pus_out[0] = (unsigned char) (l&0xF0)|((l2&0xF0)>>4);
			pus_out++; 
		} 
		puc_y += stride_y; 
		pus_out+=(stride_dest>>1); 
	} 
} 

void yuv2rgb_565Z(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int nExtra, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, __int8 slowopt, int invert)
{ 
	register int x,y; 
	unsigned int l,l2;
	unsigned int quant_error_l;
	register unsigned int mask=0;
	unsigned char *pus_out; 
	int we;
	if (nExtra<0) nExtra=0;
	if (Dither==1){mask=0x0F;}
	if ((height_y<<1)>VisibleHeight) height_y=VisibleHeight>>1;
	if ((width_y<<1)>VisibleWidth) width_y=VisibleWidth>>1;
	we=width_y+(nExtra>>1);
	pus_out = (unsigned char *) puc_out;
	quant_error_l=0;	 
	for (y=0; y<height_y; y++) 
	{ 
		for (x=0;x<width_y; x++)
		{ 
			l = _S(puc_y[x]+quant_error_l+Brightness); 
			quant_error_l=l&mask;
			l2 = _S(puc_y[x]+quant_error_l+Brightness);
			quant_error_l=l2&mask;
			pus_out[0] = (unsigned char) (l&0xF0)|((l2&0xF0)>>4);
			pus_out[we] = (unsigned char) (l2&0xF0)|((l&0xF0)>>4);
			pus_out++; 
		} 
		pus_out+=we;
		pus_out+=(nExtra>>1);
		puc_y += stride_y; 
	} 
} 

void yuv2rgb_565R(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int Extra, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, __int8 slowopt, int invert)
{ 
	register int x,y; 
	unsigned char *pus_out; 
	register unsigned int l,l2; 
	register unsigned int mask=0;
	register unsigned int quant_error_l;	
	if (Extra<0) Extra=0;
	if (Dither==1){mask=0x0F;}
	pus_out = (unsigned char *) puc_out; 
	quant_error_l=0;
	for (y=0; y<(height_y>>1); y++) 
	{ 
		pus_out = (unsigned char *) puc_out;
		pus_out+=y;
		for (x=width_y-1;x!=0;x--)
		{ 
			l = _S(puc_y[x]+quant_error_l+Brightness); 
			quant_error_l=l&mask;		
			l2 = _S(puc_y[x+stride_y]+quant_error_l+Brightness);
			quant_error_l=l2&mask;	
			pus_out[0] = (unsigned char)(l&0xF0)|((l2&0xF0)>>4); 
			pus_out+=(height_y+Extra)>>1;
		} 
		puc_y += (stride_y<<1); 
	} 
} 

void yuv2rgb_565RZ(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int nExtra, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, __int8 slowopt, int invert)
{ 

	register int x,y; 
	unsigned char *pus_out; 
	register unsigned int l; 
	register unsigned int quant_error_l;
	register unsigned int mask=0;
	if (nExtra<0) nExtra=0;
	if (Dither==1){mask=0x0F;}
	if ((width_y<<1)>VisibleHeight) width_y=VisibleHeight>>1;
	if ((height_y<<1)>VisibleWidth) height_y=VisibleWidth>>1;
	pus_out = (unsigned char *) puc_out;
	quant_error_l=0;	 
	for (y=0; y<height_y; y++) 
	{ 
		pus_out = (unsigned char *) puc_out;
		pus_out+=y;
		for (x=width_y-1;x!=0;x--)
		{ 
			l= _S(puc_y[x]+quant_error_l+Brightness); 
			quant_error_l=l&0x0F;
			pus_out[0] = pus_out[(height_y+nExtra)] =(unsigned char)(l&0xF0)|((l&0xF0)>>4); 
			pus_out+=(height_y+nExtra)<<1;
		} 
		puc_y += stride_y; 
	} 
} 

// 4 greyscale level- NINO300,...

#elif _GREYSCALE_4

#ifdef MIPS
#define _S(a) (a)>255 ? 255 : a 

void yuv2rgb_565( 
uint8_t *puc_y, int stride_y, 
uint8_t *puc_out, 
int width_y, int height_y,int stride_dest, int Dither) 
{ 

//YUV2RGB MIPS ASM v1.1 
// 
//v1.1=>pequeño aumento de velocidad
//SE => yuv2rgb para unidades de 4 grises (NINO 300...)
	
__asm (	//creamos nueva pila y salvamos registros 
		"addiu	sp,sp,-36;" 
		"sw		$18,20(sp);" 
		"sw		$17,16(sp);" 
		"sw		$16,12(sp);" 

		//calculamos stride_uv 

		"srl	$14,$5,1;" 

		//cargamos todas las variables necesarias 
		"lw		$18,56(sp);"//cargamos stride_dest 
		"lw		$2,52(sp);" 
//		"lw		$13,48(sp);" 
//		"lw		$25,44(sp);" 
		"lbu	$10,0($4);" 

		"srl	$18,$18,2;");//para poder usar stride_dest como pixels 
__asm(
		//diferencias de strides 

		"sub	$16,$5,$7;" 
		"srl	$8,$7,1;" 
		"sub	$14,$14,$8;" 

		// 
		"addi	$11,$0,0;"
		"addi	$12,$0,0;"

		"srl	$17,$7,2;" 
		"add	$17,$6,$17;" 

		//dividimos Y por 2 y comenzamos bucle 

		"srl	$2,$2,1;" 

		"bucle_y:add	$15,$4,$5;" 
		"srl	$3,$7,2;" 

		//bucle x 
		
		"add	$17,$17,$18;");//dst_buf2+ 

		//luminancia 1
__asm(	"bucle_x:add	$10,$10,$11;"
		"addi	$10,$10,-16;"
		"srl	$9,$10,16;"
		"srlv	$10,$10,$9;"
		"sll	$9,$10,23;"
		"sra	$9,$9,31;"
		"or		$10,$10,$9;"
		"andi	$11,$10,0x003F;"
		"andi	$24,$10,0x00C0;" 
		"lbu	$10,1($4);"
		//"srl	$24,$9,6;" 
			
		//primer punto ,ahora procesamos segundo. 

		//luminancia 2 
	
		"add	$10,$10,$11;"
		"addi	$10,$10,-16;"
		"srl	$9,$10,16;"
		"srlv	$10,$10,$9;"
		"sll	$9,$10,23;"
		"sra	$9,$9,31;"
		"or		$10,$10,$9;"
		"andi	$11,$10,0x003F;"
		"andi	$9,$10,0x00C0;" 
		"srl	$9,$9,2;" 
		"lbu	$10,2($4);"
		"or		$24,$24,$9;" 
 
); 
		//luminancia 3
__asm(	"add	$10,$10,$11;"
		"addi	$10,$10,-16;"
		"srl	$9,$10,16;"
		"srlv	$10,$10,$9;"
		"sll	$9,$10,23;"
		"sra	$9,$9,31;"
		"or		$10,$10,$9;"
		"andi	$11,$10,0x003F;"
		"andi	$9,$10,0x00C0;" 
		"lbu	$10,3($4);"
		"srl	$9,$9,4;" 
		"or		$24,$24,$9;"
			
		//primer punto ,ahora procesamos segundo. 

		//luminancia 4 

		//cargamos aqui para no tener interbloqueo 
		"add	$10,$10,$11;"
		"addi	$10,$10,-16;"
		"srl	$9,$10,16;"
		"srlv	$10,$10,$9;"
		"sll	$9,$10,23;"
		"sra	$9,$9,31;"
		"or		$10,$10,$9;"
		"andi	$11,$10,0x003F;"
		"andi	$9,$10,0x00C0;" 
		"lbu	$10,0($15);"
		"srl	$9,$9,6;"
		"or		$24,$24,$9;" 
	
		
		//presentamos el punto 

		"sb		$24,0($6);" 
); 
__asm( 	//f2-luminacia 1 
		 
		"add	$10,$10,$12;"
		"addi	$10,$10,-16;"
		"srl	$9,$10,16;"
		"srlv	$10,$10,$9;"
		"sll	$9,$10,23;"
		"sra	$9,$9,31;"
		"or		$10,$10,$9;"
		"andi	$12,$10,0x003F;"
		"andi	$24,$10,0x00C0;" 
		//"srl	$24,$9,6;"
		"lbu	$10,1($15);"//cargamos aqui para no tener interbloqueo 
		 
		

		//primer punto ,ahora procesamos segundo. 

		//f2-luminacia 2
		
		"add	$10,$10,$12;"
		"addi	$10,$10,-16;"
		"srl	$9,$10,16;"
		"srlv	$10,$10,$9;"
		"sll	$9,$10,23;"
		"sra	$9,$9,31;"
		"or		$10,$10,$9;"
		"andi	$12,$10,0x003F;"
		"andi	$9,$10,0x00C0;" 
		"srl	$9,$9,2;" 
		"lbu	$10,2($15);" 
		"or		$24,$24,$9;" 
 
		//presentamos el punto 
		 );

__asm( 	//f2-luminacia 3 
		 
		"add	$10,$10,$12;"
		"addi	$10,$10,-16;"
		"srl	$9,$10,16;"
		"srlv	$10,$10,$9;"
		"sll	$9,$10,23;"
		"sra	$9,$9,31;"
		"or		$10,$10,$9;"
		"andi	$12,$10,0x003F;"
		"andi	$9,$10,0x00C0;" 
		"lbu	$10,3($15);"//cargamos aqui para no tener interbloqueo 
		"srl	$9,$9,4;" 
		"or		$24,$24,$9;"
		

		//primer punto ,ahora procesamos segundo. 

		//f2-luminacia 4
		
		"add	$10,$10,$12;"
		"addi	$10,$10,-16;"
		"srl	$9,$10,16;"
		"srlv	$10,$10,$9;"
		"sll	$9,$10,23;"
		"sra	$9,$9,31;"
		"or		$10,$10,$9;"
		"andi	$12,$10,0x003F;"
		"andi	$9,$10,0x00C0;" 
		"lbu	$10,4($4);" 
		"srl	$9,$9,6;"
		"or		$24,$24,$9;" 
 
		//presentamos el punto 

		"sb		$24,0($17);" );


		//incrementamos todos los punteros y volvemos al bucle 
__asm(
		"addi	$4,$4,+4;" 
		"addi	$15,$15,+4;" 
		"addi	$3,$3,-1;" 
		"addi	$6,$6,+1;" 
		"addi	$17,$17,+1;" 

		"bgtz	$3,bucle_x;" 
	
		"add	$4,$15,$16;" 

		//cargamos aqui para no tener interbloqueo 
		"lbu	$10,0($4);" 
		
		"add	$6,$6,$18;"//dest+stride_dest 
		"add	$6,$6,$18;" 
		"add	$17,$17,$18;" 

		"addi	$2,$2,-1;" 
		"srl	$8,$7,2;" 
		"add	$6,$6,$8;" 
		"add	$17,$17,$8;" 

		"bgtz	$2,bucle_y;" 

		//restauramos registros, la pila y finalizamos 
		"lw		$16,12(sp);" 
		"lw		$17,16(sp);" 
		"lw		$18,20(sp);" 
		"addiu	sp,sp,+36;" 

);} 

#else
#define _S(a) (a)>255 ? 255 : a 
void yuv2rgb_565(uint8_t *puc_y, int stride_y, 
uint8_t *puc_out, int width_y, int height_y,int stride_dest, int Dither,int Brightness) 
 {
	register int x,y; 
	unsigned char *pus_out; 
	register unsigned int l,l2,l3,l4; 
	unsigned int quant_error_l;
	register unsigned int mask=0;
	if (stride_dest<0) stride_dest=0;
	if (Dither==1){mask=0x3F;}
	pus_out = (unsigned char *) puc_out; 
	quant_error_l=0;
	for (y=0; y<height_y; y++) 
	{ 
		for (x=0;x<width_y;x+=4)
		{ 
			l = _S(puc_y[x]+quant_error_l+Brightness); 
			quant_error_l=l&mask;
			l2 = _S(puc_y[x+1]+quant_error_l+Brightness);
			quant_error_l=l2&mask;
			l3 = _S(puc_y[x+2]+quant_error_l+Brightness); 
			quant_error_l=l3&mask;
			l4 = _S(puc_y[x+3]+quant_error_l+Brightness);
			quant_error_l=l4&mask;
			pus_out[0] = (unsigned char) (l&0xC0)|((l2&0xC0)>>2)|((l3&0xC0)>>4)|((l4&0xC0)>>6);
			pus_out++; 
		} 
		puc_y += stride_y; 
		pus_out+=(stride_dest>>2); 
	} 
} 
#endif
void yuv2rgb_565Z(uint8_t *puc_y, int stride_y, uint8_t *puc_out, int width_y, int height_y, int nExtra, int Dither,int Brightness) 
{ 
	register int x,y; 
	unsigned int l,l2,l3,l4;
	unsigned int quant_error_l;
	unsigned char *pus_out; 
	register unsigned int mask=0;
	int we;
	if (nExtra<0) nExtra=0;
	if (Dither==1){mask=0x3F;}
	we=(width_y>>1)+(nExtra>>2);
	pus_out = (unsigned char *) puc_out;
	quant_error_l=0;	 
	for (y=0; y<height_y; y++) 
	{ 
		for (x=0;x<width_y;x+=2)
		{ 
			l = _S(puc_y[x]+quant_error_l+Brightness); 
			quant_error_l=l&mask;
			l2 = _S(puc_y[x]+quant_error_l+Brightness); 
			quant_error_l=l2&mask;
			l3 = _S(puc_y[x+1]+quant_error_l+Brightness); 
			quant_error_l=l3&mask;
			l4 = _S(puc_y[x+1]+quant_error_l+Brightness); 
			quant_error_l=l4&mask;
			pus_out[0] = (unsigned char) (l&0xC0)|((l2&0xC0)>>2)|((l3&0xC0)>>4)|((l4&0xC0)>>6);
			pus_out[we] = (unsigned char) (l2&0xC0)|((l&0xC0)>>2)|((l4&0xC0)>>4)|((l3&0xC0)>>6);
			pus_out++; 
		} 
		pus_out+=we;
		pus_out+=(nExtra>>2);
		puc_y += stride_y; 
	} 
} 

void yuv2rgb_565R(uint8_t *puc_y, int stride_y, 
 
uint8_t *puc_out, int width_y, int height_y, int Extra, int Dither,int Brightness) 
{ 
	register int x,y; 
	unsigned char *pus_out; 
	register unsigned int l,l2,l3,l4; 
	register unsigned int quant_error_l;	
	register unsigned int mask=0;
	if (Extra<0) Extra=0;
	if (Dither==1){mask=0x3F;}
	pus_out = (unsigned char *) puc_out; 
	quant_error_l=0;
	for (y=0; y<(height_y>>2); y++) 
	{ 
		pus_out = (unsigned char *) puc_out;
		pus_out+=y;
		for (x=width_y-1;x!=0;x--)
		{ 
			l = _S(puc_y[x]+quant_error_l+Brightness); 
			quant_error_l=l&mask;		
			l2 = _S(puc_y[x+stride_y]+quant_error_l+Brightness);
			quant_error_l=l2&mask;	
			l3 = _S(puc_y[x+(stride_y<<1)]+quant_error_l+Brightness); 
			quant_error_l=l3&mask;		
			l4 = _S(puc_y[x+(stride_y<<2)]+quant_error_l+Brightness);
			quant_error_l=l4&mask;
			pus_out[0] = (unsigned char)(l&0xC0)|((l2&0xC0)>>2)|((l3&0xC0)>>4)|((l4&0xC0)>>6); 
			pus_out+=(height_y+Extra)>>2;
		} 
		puc_y += (stride_y<<2); 
	} 
} 

void yuv2rgb_565RZ(uint8_t *puc_y, int stride_y, 
 
uint8_t *puc_out, int width_y, int height_y, int nExtra, int Dither,int Brightness) 
{ 

	register int x,y; 
	unsigned char *pus_out; 
	register unsigned int l,l2; 
	register unsigned int quant_error_l;
	register unsigned int mask=0;
	if (nExtra<0) nExtra=0;
	if (Dither==1){mask=0x3F;}
	pus_out = (unsigned char *) puc_out;
	quant_error_l=0;	 
	for (y=0; y<(height_y>>1); y++) 
	{ 
		pus_out = (unsigned char *) puc_out;
		pus_out+=y;
		for (x=width_y-1;x!=0;x--)
		{ 
			l= _S(puc_y[x]+quant_error_l+Brightness); 
			quant_error_l=l&mask;
			l2= _S(puc_y[x+stride_y]+quant_error_l+Brightness); 
			quant_error_l=l2&mask;
			pus_out[0] = pus_out[(height_y>>1)+(nExtra>>1)] =(unsigned char)(l&0xC0)|((l&0xC0)>>2)|((l2&0xC0)>>4)|((l2&0xC0)>>6); 
			pus_out+=height_y+nExtra;

		} 
		puc_y += (stride_y<<1); 
	} 
	
} 

#elif emul
#define _S(a) (a)>255 ? 255 : a 

void yuv2rgb_565(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int stride_dest, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, __int8 slowopt, int invert)
 
{
	register int x,y; 
	unsigned short *pus_out; 
	register unsigned int l; 
	unsigned int quant_error_l;
	if (stride_dest<0) stride_dest=0;
	pus_out = (unsigned short *) puc_out; 
	quant_error_l=0;
	for (y=0; y<height_y; y+=2) 
	{ 
		for (x=0;x<width_y;x++)
		{ 
			l = _S(puc_y[x]+quant_error_l+Brightness); 
			quant_error_l=l&0x0F;
			pus_out[0] = (unsigned short) (l&0xF0)<<8|((l&0xF0))<<3|((l&0xF0)>>3);
			pus_out++; 
		} 
		puc_y += stride_y; 
		pus_out+=stride_dest; 
		for (x=0;x<width_y;x++)
		{ 
			l = _S(puc_y[x]+quant_error_l+Brightness); 
			quant_error_l=l&0x0F;
			pus_out[0] = (unsigned short) (l&0xF0)<<8|((l&0xF0))<<3|((l&0xF0)>>3);
			pus_out++; 
		} 
		puc_y += stride_y; 
		pus_out+=stride_dest; 
	} 
} 

void yuv2rgb_565Z(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int stride_dest, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, __int8 slowopt, int invert)
{ }

void yuv2rgb_565R(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int stride_dest, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, __int8 slowopt, int invert)
{ }

void yuv2rgb_565RZ(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int stride_dest, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, __int8 slowopt, int invert)
{ }


#else


#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) ( ((r & 0xF8) >> 3) | (((g & 0xFC) << 3)) | (((b & 0xF8) << 8)))
#define _Ps5652(r,g,b) ( ((r & 0xF8) << 13) | (((g & 0xFC) << 19)) | (((b & 0xF8) << 24)))
//#define _Ps565(R,G,B)	((B << 8) & 0xf800) | ((G << 3) & 0x07e0) |((R >> 3) & 0x001f)
//#define _Ps565(r,g,b) (((((b >> 3)<<6) | (g >> 2)) << 5) | (r >> 3))
//#define _Ps565(r,g,b) ((b>>3)<<11) + ((g>>2)<<5) + (r>>3)

#ifdef MIPS

void yuv2rgb_565( 
uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, 
uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, 
int width_y, int height_y,int stride_dest, int Dither) 
{ 
//YUV2RGB MIPS ASM v1.1 
//v1.2->Crop support added 
//v1.1->pequeño aumento de velocidad
// 
__asm ( //creamos nueva pila y salvamos registros 
  "addiu sp,sp,-36;" 
  "sw  $18,20(sp);" 
  "sw  $17,16(sp);" 
  "sw  $16,12(sp);" 
 
  //calculamos stride_uv 
 
  "srl $14,$5,1;" 
 
  //cargamos todas las variables necesarias 
  "lw  $18,68(sp);"//cargamos stride_dest 
  "lw  $2,64(sp);" 
  "lw  $13,60(sp);" 
  "lw  $25,56(sp);" 
  "lbu $11,0($6);" 
  "lbu $12,0($7);" 
  "lbu $10,0($4);" 
 
  "sll $18,$18,1;");//para poder usar stride_dest como pixels 
__asm( 
	 
	// crop x
 
  "addi $8,$13,-640;"
  "blez $8,no_x_crop;"
  
  "li	$13,640;"
  "srl	$8,$8,1;"
  "add	$4,$4,$8;"
  "srl	$8,$8,1;"
  "add	$6,$6,$8;"
  "add	$7,$7,$8;"

  // crop y
  "no_x_crop:addi $8,$2,-240;"
  "blez $8,no_y_crop;"
  
  "li	$2,240;"
    
  "srl	$8,$8,1;"
  "mul	$8,$8,$5;"
  "add	$4,$4,$8;"
  "srl	$8,$8,2;"
  "add	$6,$6,$8;"
  "add	$7,$7,$8;"

  //diferencias de strides 
 
  "no_y_crop:sub $16,$5,$13;" 
  "srl $8,$13,1;" 
  "sub $14,$14,$8;" 
 
  // 
 
  "sll $17,$13,1;" 
  "add $17,$25,$17;" 
 
  //dividimos Y por 2 y comenzamos bucle 
 
  "srl $2,$2,1;" 
 
  "bucle_y:add $15,$4,$5;" 
  "srl $3,$13,1;" 
 
  //bucle x 
 
  "add $17,$17,$18;"//dst_buf2+ 
 
  "bucle_x:addi $11,$11,-144;" 
  "addi $12,$12,-144;" 
);
__asm(
  //r1 
 
  "add $9,$10,$11;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "srl $24,$9,3;" 
 
  //g1 
 
  "addi $9,$10,-16;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00FC;" 
  "sll $9,$9,3;" 
  "or  $24,$24,$9;" 
 
  //b1 
 
  "add $9,$10,$12;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;"); 
 
__asm( "andi $9,$9,0x00F8;" 
  "lbu $10,1($4);"//cargamos aqui para no tener interbloqueo 
 
  "sll $9,$9,8;" 
  "or  $24,$24,$9;" 
 
  //primer punto ,ahora procesamos segundo. 
 
  //r2 
 
  "add $9,$10,$11;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;"
  "andi $9,$9,0x00F8;"
  "sll $9,$9,13;" 
  "or  $24,$24,$9;" 
 
  //g2 
 
  "addi $9,$10,-16;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00FC;" 
  "sll $9,$9,19;" 
  "or  $24,$24,$9;" 
 
  //b2 
 
  "add $9,$10,$12;" 
  "lbu $10,0($15);"//cargamos aqui para no tener interbloqueo 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00F8;" 
  "sll $9,$9,24;" 
  "or  $24,$24,$9;" 
 
  //presentamos el punto 
 
  "sw  $24,0($25);" 
); 
 
__asm( 
  //ahora procesamos la segunda línea 
 
  //f2r1 
 
  "add $9,$10,$11;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "srl $24,$9,3;" 
 
  //f2g1 
 
  "addi $9,$10,-16;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00FC;" 
  "sll $9,$9,3;" 
  "or  $24,$24,$9;" 
 
  //f2b1 
 
  "add $9,$10,$12;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00F8;" 
  "lbu $10,1($15);"//cargamos aqui para no tener interbloqueo 
 
  "sll $9,$9,8;" 
  "or  $24,$24,$9;"); 
 
  //primer punto ,ahora procesamos segundo. 
 
  //f2r2 
 
__asm( "add $9,$10,$11;" 
  "lbu $11,1($6);" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;"
  "andi $9,$9,0x00F8;"
  "sll $9,$9,13;" 
  "or  $24,$24,$9;" 
 
  //f2g2 
 
  "addi $9,$10,-16;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00FC;" 
  "sll $9,$9,19;" 
  "or  $24,$24,$9;" 
 
  //f2b2 
 
  "add $9,$10,$12;" 
  "lbu $12,1($7);" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00F8;" 
 
  "sll $9,$9,24;" 
  "lbu $10,2($4);" 
  "or  $24,$24,$9;" 
 
  //presentamos el punto 
 
  "sw  $24,0($17);" );
 
  //incrementamos todos los punteros y volvemos al bucle 
__asm(
  "addi $4,$4,+2;" 
  "addi $15,$15,+2;" 
  "addi $6,$6,+1;" 
  "addi $7,$7,+1;" 
  "addi $3,$3,-1;" 
  "addi $25,$25,+4;" 
  "addi $17,$17,+4;" 
 
  "bgtz $3,bucle_x;" 
 
  "add $4,$15,$16;" 
 
  "addu $6,$6,$14;" 
  "addu $7,$7,$14;" 
 
  //cargamos aqui para no tener interbloqueo 
  "lbu $11,0($6);" 
  "lbu $10,0($4);" 
  "lbu $12,0($7);" 
 
  "add $25,$25,$18;"//dest+stride_dest 
  "add $25,$25,$18;" 
  "add $17,$17,$18;" 
 
  "addi $2,$2,-1;" 
  "sll $8,$13,1;" 
  "add $25,$25,$8;" 
  "add $17,$17,$8;" 
 
  "bgtz $2,bucle_y;" 
 
  //restauramos registros, la pila y finalizamos 
  "lw  $16,12(sp);" 
  "lw  $17,16(sp);" 
  "lw  $18,20(sp);" 
  "addiu sp,sp,+36;" 
 
);} 


void yuv2rgb_565R( 
uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, 
uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, 
int width_y, int height_y,int stride_dest, int Dither) 
{ 
//YUV2RGB MIPS ASM v1.1 
//v1.2->Crop support added 
//v1.1->pequeño aumento de velocidad
// 
__asm ( //creamos nueva pila y salvamos registros 
  "addiu sp,sp,-36;" 
  "sw  $19,24(sp);"
  "sw  $18,20(sp);" 
  "sw  $17,16(sp);" 
  "sw  $16,12(sp);" 
 
  //calculamos stride_uv 
 
  "srl $14,$5,1;" 
 
  //cargamos todas las variables necesarias 
  "lw  $18,68(sp);"//cargamos stride_dest 
  "lw  $2,64(sp);" 
  "lw  $13,60(sp);" 
  "lw  $25,56(sp);" 
  "lbu $11,0($6);" 
  "lbu $12,0($7);" 
  "lbu $10,0($4);" 
 
  "sll $18,$18,1;");//para poder usar stride_dest como pixels 
__asm( // crop x
 
  "addi $8,$13,-240;"
  "blez $8,no_x_cropR;"
  
  "li	$13,240;"
  "srl	$8,$8,1;"
  "add	$4,$4,$8;"
  "srl	$8,$8,1;"
  "add	$6,$6,$8;"
  "add	$7,$7,$8;"

  // crop y
  "no_x_cropR:addi $8,$2,-640;"
  "blez $8,no_y_cropR;"
  
  "li	$2,640;"
  "srl	$8,$8,1;"
  "mul	$8,$8,$5;"
  "add	$4,$4,$8;"
  "srl	$8,$8,2;"
  "add	$6,$6,$8;"
  "add	$7,$7,$8;"
   
  //diferencias de strides 
 
  "no_y_cropR:sub $16,$5,$13;" 
  "srl $8,$13,1;" 
  "sub $14,$14,$8;" 

  "sll $8,$2,1;"
  "add $17,$18,$8;" //(height+stride)*2 (short)
 
  "addi $8,$13,-1;"
  "mul $8,$8,$17;"
  "addu $25,$25,$8;" //puntero_video+desplazamiento 
  
  //dividimos Y por 2 y comenzamos bucle 
 
  "srl $2,$2,1;" 
 
  "bucle_yR:add $15,$4,$5;" 
  "srl $3,$13,1;" 
 
  //bucle x 
 
  "bucle_xR:addi $11,$11,-144;" 
  "addi $12,$12,-144;" 
);
__asm(//r1 
 
  "add $9,$10,$11;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "srl $24,$9,3;" 
 
  //g1 
 
  "addi $9,$10,-16;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00FC;" 
  "sll $9,$9,3;" 
  "or  $24,$24,$9;" 
 
  //b1 
 
  "add $9,$10,$12;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;"); 
 
__asm( "andi $9,$9,0x00F8;" 
  "lbu $10,1($4);"//cargamos aqui para no tener interbloqueo 
 
  "sll $9,$9,8;" 
  "or  $24,$24,$9;" 
 
  //primer punto ,ahora procesamos segundo. 
 
  //r2 
 
  "add $9,$10,$11;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;"
  "andi $9,$9,0x00F8;"
  "srl $19,$9,3;" 
 
  //g2 
 
  "addi $9,$10,-16;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00FC;" 
  "sll $9,$9,3;" 
  "or  $19,$19,$9;" 
 
  //b2 
 
  "add $9,$10,$12;" 
  "lbu $10,0($15);"//cargamos aqui para no tener interbloqueo 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00F8;" 
  "sll $9,$9,8;" 
  "or  $19,$19,$9;" 
 
  //presentamos el punto 
 
); 
 
__asm(//ahora procesamos la segunda línea 
 
  //f2r1 
 
  "add $9,$10,$11;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00F8;" 
  "sll $9,$9,13;" 
  "or  $24,$24,$9;" 
 
  //f2g1 
 
  "addi $9,$10,-16;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00FC;" 
  "sll $9,$9,19;" 
  "or  $24,$24,$9;" 

  //f2b1 
 
  "add $9,$10,$12;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00F8;" 
  "lbu $10,1($15);"//cargamos aqui para no tener interbloqueo 
 
  "sll $9,$9,24;" 
  "or  $24,$24,$9;" 
); 
 
  //primer punto ,ahora procesamos segundo. 
 
  //f2r2 
 
__asm( "add $9,$10,$11;" 
  "lbu $11,1($6);" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;"
  "andi $9,$9,0x00F8;"
  "sll $9,$9,13;" 
  "or  $19,$19,$9;" 
 
  //f2g2 
 
  "addi $9,$10,-16;" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00FC;" 
  "sll $9,$9,19;" 
  "or  $19,$19,$9;" 
 
  //f2b2 
 
  "add $9,$10,$12;" 
  "lbu $12,1($7);" 
  "sra $8,$9,31;" 
  "srlv $9,$9,$8;" 
  "andi $9,$9,0x00F8;" 
 
  "sll $9,$9,24;" 
  "lbu $10,2($4);" 
  "or  $19,$19,$9;" 
 
  //presentamos el punto 
 
  "sw $24,0($25);"
  "sub $8,$25,$17;" 
  "sw $19,0($8);" 
);
 
  //incrementamos todos los punteros y volvemos al bucle 
__asm(
  "addi $4,$4,+2;" 
  "addi $15,$15,+2;" 
  "addi $6,$6,+1;" 
  "addi $7,$7,+1;" 
  "addi $3,$3,-1;" 
  "sub $25,$25,$17;" 
  "sub $25,$25,$17;" 

  "bgtz $3,bucle_xR;" 
 
  "add $4,$15,$16;" 
 
  "addu $6,$6,$14;" 
  "addu $7,$7,$14;" 
 
  //cargamos aqui para no tener interbloqueo 
  "lbu $11,0($6);" 
  "lbu $10,0($4);" 
  "lbu $12,0($7);" 
 
  "mul $8,$13,$17;"
  "addu $25,$25,$8;" //puntero_video+desplazamiento 
  "addiu $25,$25,+4;"

  "addi $2,$2,-1;" 

  "bgtz $2,bucle_yR;" 
 
  //restauramos registros, la pila y finalizamos 
  "lw  $16,12(sp);" 
  "lw  $17,16(sp);" 
  "lw  $18,20(sp);" 
  "lw $19,24(sp);"

  "addiu sp,sp,+36;" 
 
);} 

 
void yuv2rgb_565Z(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int stride_dest, int Dither) 
{ 
// yuv2rgb 4xzoom.32bits 
// v0.2->some optimizations+horizontal crop 
// v0.3->precise bilinear+some optimizations
// v0.4a->removed precise filtering+save word opt.
// v0.5a->fixed big bug. Precise bilinear only on VR4122.
 
__asm ( "addiu sp,sp,-8;" 
  "sw  $16,0(sp);" 
  "sw  $17,4(sp);"
 
  //cargamos todas las variables 
 
  "lw  $9,40(sp);" 
  "lw  $2,36(sp);" 
  "lw  $3,32(sp);" 
  "lw  $25,28(sp);" 
 
  //recortamos video x_crop
  
  "addi $14,$3,-320;" 
  "blez $14,no_recorte_x;" 
  
  "li $3,320;" 
  "srl	$14,$14,1;"
  "add	$4,$4,$14;"
  "srl	$14,$14,1;"
  "add	$6,$6,$14;"
  "add	$7,$7,$14;"

  "no_recorte_x:"
  
  //recortamos video y_crop
  
  "addi $14,$2,-120;" 
  "blez $14,no_recorte_y;" 
  
  "li	$2,120;" 
  "srl	$14,$14,1;"
  "mul	$14,$14,$5;"
  "add	$4,$4,$14;"
  "srl	$14,$14,2;"
  "add	$6,$6,$14;"
  "add	$7,$7,$14;"
    
  //diferencias de strides: 
  
  "no_recorte_y:srl $8,$5,1;" 
  "sub $5,$5,$3;" 
  
  //
 
  "sll $9,$9,1;" 
  
  //preparando segunda linea
 
  "sll $14,$3,1;"
  "add $17,$25,$9;"
  "add $17,$17,$14;"
 
  //
 
  "bucle_y1:lbu $12,0($7);" 
  "lbu $11,0($6);" 
  "lbu $10,0($4);" 
  "add $16,$3,$0;" 
  "addi $11,$11,-128;" 
  "addi $12,$12,-128;"
  "addi $10,$10,-16;"
  
  //r1 
  
  "add $14,$10,$11;" 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;"
  "srl $13,$15,3;" 
  
  //g1 
  
  "sra $15,$14,31;"
  "srlv $15,$14,$15;"
  "andi $14,$15,0x00FC;" 
  "sll $14,$14,3;" 
  "or  $13,$13,$14;" 
  
  //b1 
  
  "add $14,$10,$12;" 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;"
  "andi $14,$15,0x00F8;" 
  "sll $14,$14,8;" 
  "or  $13,$13,$14;" 
  
  "lbu $10,1($4);" 
  "addi $4,$4,+1;" 
//  "addi $16,$16,-1;"
  "addi $10,$10,-16;"
  ); 
 
__asm( "bucle_x1:add $14,$10,$11;"  
  "sra $15,$14,31;"
  "srlv $24,$14,$15;"
  "srl $24,$24,3;" 
 
  //g3 
 
  "sra $15,$10,31;" 
  "srlv $15,$10,$15;"
  "andi $15,$15,0x00FC;" 
  "sll $14,$15,3;" 
  "or  $24,$24,$14;" 
 
  //b3 
 
  "add $14,$10,$12;" 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;"
  "andi $15,$15,0x00F8;" 
  "sll $14,$15,8;" 
  "or  $24,$24,$14;" 
#if (_WIN32_WCE < 300) 
  //calculamos el punto interpolado 
 
  "srl $14,$13,1;"  //div por 2 p1 
  "srl $15,$24,1;"  //y p2 
 
  "andi $14,$14,0x7BEF;" //los enmascaramos 
  "andi $15,$15,0x7BEF;" 
 
  "addu $13,$14,$15;"  //y los sumamos 
 
#else
  //calculamos el punto interpolado 
 
  "andi $10,$13,0x0821;"//
  "and $10,$10,$24;"//calcula un valor medio perfecto
 
  "srl $14,$13,1;"//div por 2 p1 
  "srl $15,$24,1;"//y p2 
 
  "andi $14,$14,0x7BEF;"//los enmascaramos 
  "andi $15,$15,0x7BEF;" 
 
  "addu $13,$14,$15;"//y los sumamos 
  "addu $13,$13,$10;"
#endif  
  //guardamos puntos 
    
  "sll $15,$24,16;"
  "or  $13,$13,$15;"
 
  "sw  $13,0($25);"
  "sw  $13,0($17);"
     
  //cambiamos de lugar ultimo punto 
  
  "add $13,$24,$0;" 
  
  //incrementamos punteros 
  
  "lbu $10,0($4);"  //cargamos nuevo puc_y 
  "addi $4,$4,+1;" 
  "addi $16,$16,-1;" 
  "sll $14,$16,31;" 
  "addi $25,$25,+4;" 
  "addi $17,$17,+4;"
 
  "addi $10,$10,-16;" //*
     
  "beq $14,$0,par_x;" 
  "addi $6,$6,+1;" 
  "addi $7,$7,+1;" 
  "lbu $12,0($7);" 
  "lbu $11,0($6);" 
  "addi $12,$12,-128;" 
  "addi $11,$11,-128;"
  "par_x:bgtz $16,bucle_x1;"); 
 
__asm( //aumentamos strides: 
  "sll $14,$2,31;" 
  "add $4,$4,$5;" 
  "addi $4,$4,-1;"
  "srl $15,$3,1;" 
  "sub $6,$6,$15;" 
  "sub $7,$7,$15;" 
    
  "beq $14,$0,par_y;" 
  "add $6,$6,$8;" 
  "add $7,$7,$8;" 
 
  //aumentamos stride_dest 
 
  "par_y:addi  $2,$2,-1;" 
  
  "sll $14,$3,1;"
  "sub $17,$17,$14;"
  "add $25,$17,$9;"
 
  "add $17,$25,$9;"
  "add $17,$17,$14;"
  
  "bgtz $2,bucle_y1;" 
 
  "lw  $16,0(sp);" 
  "lw  $17,4(sp);"
 
  "addiu sp,sp,+8;" ); 
} 
 
 
 

void yuv2rgb_565ZPP(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int stride_dest, int Dither) 
{ 
//yuv2rgb 4xzoom BILINEAR FILTERING. 32bit access 
// v0.1=>base code of 0.2 linear zoom asm +bilinear filtering 
// v0.2=>finally 32bits code
 
__asm ( "addiu sp,sp,-36;" 
  "sw  $16,0(sp);" 
  "sw  $17,4(sp);" 
  "sw  $18,8(sp);" 
  "sw  $19,12(sp);" 
  "sw  $20,16(sp);" 
  "sw  $21,20(sp);"
 
  //cargamos todas las variables 
 
  "lw  $9,68(sp);" 
  "lw  $2,64(sp);" 
  "lw  $3,60(sp);" 
  "lw  $25,56(sp);" 
 
  //recortamos video x_crop
  
  "addi $14,$3,-320;" 
  "blez $14,no_recorte_xzpp;" 
  
  "li $3,320;" 
  "srl	$14,$14,1;"
  "add	$4,$4,$14;"
  "srl	$14,$14,1;"
  "add	$6,$6,$14;"
  "add	$7,$7,$14;"

  "no_recorte_xzpp:"
  
  //recortamos video y_crop
  
  "addi $14,$2,-120;" 
  "blez $14,no_recorte_yzpp;" 
  
  "li	$2,120;" 
  "srl	$14,$14,1;"
  "mul	$14,$14,$5;"
  "add	$4,$4,$14;"
  "srl	$14,$14,2;"
  "add	$6,$6,$14;"
  "add	$7,$7,$14;"
 
  //diferencias de strides: 
 
  "no_recorte_yzpp:srl $8,$5,1;" 
 
  "add $17,$5,$4;"//y tomamos el primer puntero 
 
  "sub $5,$5,$3;" 
  
  //
 
  "sll $9,$9,1;" 
  
  //preparando segunda linea
 
  "sll $14,$3,1;"
  "add $21,$25,$9;"
  "add $21,$21,$14;"
   
  //
 
  "bucle_y2:lbu $12,0($7);" 
  "lbu $11,0($6);" 
  "lbu $10,0($4);" 
  "lbu $18,0($17);"//tomamos puc_y de la siguiente linea 
  "add $16,$3,$0;" 
  "addi $11,$11,-144;" 
  "addi $12,$12,-144;"); 
 
  //r1 
 
__asm( "add $14,$10,$11;" 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;"
  "srl $13,$15,3;" 
  
  //g1 
 
  "addi $14,$10,-16;" 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;"
  "andi $14,$15,0x00FC;" 
  "sll $14,$14,3;" 
  "or  $13,$13,$14;" 
 
  //b1 
  
  "add $14,$10,$12;" 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;"
  "andi $14,$15,0x00F8;" 
  "sll $14,$14,8;" 
  "or  $13,$13,$14;" 
 
  //r2 
 
  "srl $10,$10,1;" 
  "srl $18,$18,1;" 
  
  "add $18,$18,$10;" 
 
  "add $14,$18,$11;" 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;"
  "srl $19,$15,3;" 
 
  //g2 
 
  "addi $14,$18,-16;" 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;" 
  "andi $14,$15,0x00FC;" 
  "sll $14,$14,3;" 
  "or  $19,$19,$14;" 
 
  //b2
  
  "add $14,$18,$12;" 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;" 
  "andi $14,$15,0x00F8;" 
  "sll $14,$14,8;" 
  "or  $19,$19,$14;" 
 
  "lbu $10,1($4);" 
  "lbu $18,1($17);" 
  "addi $4,$4,+1;" 
  "addi $17,$17,+1;"//sumamos al interpolado 
  "addi $16,$16,-1;"); 
 
__asm( "bucle_x2:add $14,$10,$11;" //r3 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;"
  "add $24,$15,$0;" 
  "srl $24,$24,3;" 
 
  //g3 
 
  "addi $14,$10,-16;" 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;"
  "andi $15,$15,0x00FC;" 
  "sll $14,$15,3;" 
  "or  $24,$24,$14;" 
 
  //b3 
 
  "add $14,$10,$12;" 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;"
  "andi $15,$15,0x00F8;" 
  "sll $14,$15,8;" 
  "or  $24,$24,$14;" 
 
  //calculamos el punto interpolado 
 
  "srl $14,$13,1;"//div por 2 p1 
  "srl $15,$24,1;"//y p2 
 
  "andi $14,$14,0x7BEF;"//los enmascaramos 
  "andi $15,$15,0x7BEF;" 
 
  "addu $13,$14,$15;"
  "sll $15,$24,16;"
  "or  $13,$13,$15;"
  "sw  $13,0($25);" 
  );//y los sumamos 
 
  //re2 
__asm( "srl $10,$10,1;" 
  "srl $18,$18,1;" 
 
  "add $18,$18,$10;" 
 
  "add $14,$18,$11;" 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;" 
  "srl $20,$15,3;" 
  
  //ge2 
  
  "addi $14,$18,-16;" 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;" 
  "andi $14,$15,0x00FC;" 
  "sll $14,$14,3;" 
  "or  $20,$20,$14;" 
  
  //be2 
  
  "add $14,$18,$12;" 
  "sra $15,$14,31;"
  "srlv $15,$14,$15;" 
  "andi $14,$15,0x00F8;" 
  "sll $14,$14,8;" 
  "or  $20,$20,$14;" 
 
/// 
 
  "srl $14,$19,1;"//div por 2 p1 
  "srl $15,$20,1;"//y p2 
 
  "andi $14,$14,0x7BEF;"//los enmascaramos 
  "andi $15,$15,0x7BEF;" 
 
  "addu $19,$14,$15;"//y los sumamos 
 

  //guardamos puntos 
  
  "sll $15,$20,16;"
  "or  $19,$19,$15;"
 
  "sw  $19,0($21);" 
 
 
  //cambiamos de lugar ultimo punto 
 
  "add $13,$24,$0;" 
  "add $19,$20,$0;" 
); 
 
  //incrementamos punteros 
 
__asm( "lbu $10,0($4);"//cargamos nuevo puc_y 
  "lbu $18,0($17);" 
  "addi $4,$4,+1;" 
  "sll $14,$16,31;" 
  "addi $17,$17,+1;" 
  "addi $16,$16,-1;" 
  "addi $25,$25,+4;" 
  "addi $21,$21,+4;"
  "beq $14,$0,par_x1;" 
  "addi $6,$6,+1;" 
  "addi $7,$7,+1;" 
  "lbu $12,0($7);" 
  "lbu $11,0($6);" 
  "addi $12,$12,-144;" 
  "addi $11,$11,-144;" 
  "par_x1:bgtz $16,bucle_x2;"); 
 
__asm( //aumentamos strides: 
  "sll $14,$2,31;" 
  "add $4,$4,$5;" 
  "add $17,$17,$5;" 
  "srl $15,$3,1;" 
  "sub $6,$6,$15;" 
  "sub $7,$7,$15;" 
  "beq $14,$0,par_1;" 
  "add $6,$6,$8;" 
  "add $7,$7,$8;" 
  
  //aumentamos stride_dest 
  
  "par_1:addi $2,$2,-1;" 
  "sll $14,$3,1;"
  "sub $21,$21,$14;"
  "add $25,$21,$9;"
  "addi $25,$25,4;"
 
  "add $21,$25,$9;"
  "add $21,$21,$14;"
  "bgtz $2,bucle_y2;" 
 
  "lw  $16,0(sp);" 
  "lw  $17,4(sp);" 
  "lw  $18,8(sp);" 
  "lw  $19,12(sp);" 
  "lw  $20,16(sp);" 
  "lw  $21,20(sp);"
  "addiu sp,sp,+36;" ); 
} 

#else


#ifdef ARM
#ifndef ATI
void yuv2rgb_565D(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y,int stride_dest, int Dither,int Brightness, uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int xStart, int xEnd, int xStep) 
{ 
//Floyd-Steinberg dithered version of the yuv2rgbHQ
	register int x,y; 
	register int temp_y,temp_Guv,temp_Guv1,temp_Guv2; 
	unsigned int *pus_out; 
	register unsigned int r,g,b; 
	register int quant_error_r=0,quant_error_g=0,quant_error_b=0;
//	unsigned char* LimitValue=&LimitValues[256];
	int r_adjust=(0x0F);
	int g_adjust=(0x0F);
	int b_adjust=(0x0F);
	if (Dither==1)
	{
		r_adjust=(0x07);
		g_adjust=(0x03);
		b_adjust=(0x07);
	}
	Brightness-=16;
	pus_out = (unsigned int *) (puc_out); 
	for (y=0; y<height_y; y+=2)
	{ 		
		//if (y==lastpel) width_y-=2;
		for (x=xStart;x!=xEnd;x+=(xStep<<1))
		{ 
			if ((!slowopt)||(puc_y[x]-puc_yp[x]||(puc_y[x+stride_y]-puc_yp[x+stride_y])||(puc_u[x>>1]-puc_up[x>>1])||(puc_v[x>>1]-puc_vp[x>>1])))
			{
				unsigned int hvalue;
				temp_y=9576*(puc_y[x]+Brightness);
				temp_Guv=-3218*(puc_u[x>>1]-128)-6686*(puc_v[x>>1]-128);
				temp_Guv2=16591*(puc_v[x>>1]-128);
				temp_Guv1=13123*(puc_u[x>>1]-128);
				r = _S(((temp_y+temp_Guv1)>>13)+ quant_error_r); 
				g = _S(((temp_y+temp_Guv)>>13)+quant_error_g); 
				b = _S(((temp_y+temp_Guv2)>>13)+ quant_error_b); 
				hvalue = (unsigned int) _Ps565(r,g,b); 
				quant_error_r=(r&r_adjust);
				quant_error_g=(g&g_adjust);
				quant_error_b=(b&b_adjust);
				temp_y=9576*(puc_y[x+xStep]+Brightness);
				r =  _S(((temp_y+temp_Guv1)>>13)+ quant_error_r); 
				g =  _S(((temp_y+temp_Guv)>>13)+quant_error_g); 
				b =  _S(((temp_y+temp_Guv2)>>13)+ quant_error_b); 

				quant_error_r=(r&r_adjust);
				quant_error_g=(g&g_adjust);
				quant_error_b=(b&b_adjust);
				pus_out[0] = hvalue|(((unsigned int) _Ps5652(r,g,b))); 
				temp_y=9576*(puc_y[x+stride_y]+Brightness);
				r =  _S(((temp_y+temp_Guv1)>>13)+ quant_error_r); 
				g =  _S(((temp_y+temp_Guv)>>13)+quant_error_g); 
				b =  _S(((temp_y+temp_Guv2)>>13)+ quant_error_b); 
				hvalue = (unsigned int) _Ps565(r,g,b); 
				quant_error_r=(r&r_adjust);
				quant_error_g=(g&g_adjust);
				quant_error_b=(b&b_adjust);
				temp_y=9576*(puc_y[x+stride_y+xStep]+Brightness);
				r =  _S(((temp_y+temp_Guv1)>>13)+ quant_error_r); 
				g =  _S(((temp_y+temp_Guv)>>13)+quant_error_g); 
				b =  _S(((temp_y+temp_Guv2)>>13)+ quant_error_b); 
				quant_error_r=(r&r_adjust);
				quant_error_g=(g&g_adjust);
				quant_error_b=(b&b_adjust);
				pus_out[(width_y+stride_dest)>>1] = hvalue|(((unsigned int) _Ps5652(r,g,b))); 
			}
			pus_out++; 
		} 
		puc_y += stride_y<<1; 
		puc_yp+=stride_y<<1;
		pus_out+=(width_y>>1)+stride_dest;
		puc_u += stride_uv; 
		puc_v += stride_uv; 
		puc_up += stride_uv; 
		puc_vp += stride_uv; 
	} 
} 


void yuv2rgb_565ZD(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int nExtra, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int xStart, int xEnd, int xStep) 
{ 
//Floyd-Steinberg dithered version of the yuv2rgbHQ
	register int x,y; 
	register signed int temp_y,temp_Guv,next_y,next_Guv; 
	unsigned int *pus_out; 
	register unsigned int r,g,b; 
	register int quant_error_r,quant_error_g,quant_error_b;
	register int quant_error_r2,quant_error_g2,quant_error_b2;
	int we;
	int r_adjust=(0x0F);
	int g_adjust=(0x0F);
	int b_adjust=(0x0F);
	//unsigned char* LimitValue=&LimitValues[256];
	if (Dither==1)
	{
		r_adjust=(0x07);
		g_adjust=(0x03);
		b_adjust=(0x07);
	}
	Brightness-=16;
//	if ((height_y<<1)>VisibleHeight) height_y=VisibleHeight>>1;
//	if ((width_y<<1)>VisibleWidth) width_y=VisibleWidth>>1;
//	if (nExtra<0) nExtra=0;
	we=width_y+nExtra;
	pus_out = (unsigned int *) puc_out;	//I can´t get it working with hold 
	quant_error_r=quant_error_g=quant_error_b=0;
	quant_error_r2=quant_error_g2=quant_error_b2=0;
	for (y=0; y<height_y; y++) 
	{ 
		quant_error_r2=quant_error_g2=quant_error_b2=6;
		//if (y==119) width_y-=1; //don't want to go outside the border.
		for (x=xStart;x!=xEnd;x+=xStep)
		{ 
			int x2=x>>1;//add tolerance of 1 
			if ((!slowopt)||(puc_y[x]!=puc_yp[x]||(puc_u[x2]!=puc_up[x2])||(puc_v[x2]!=puc_vp[x2])))
			{
				unsigned int hvalue,hvalue2,hvalue3,hvalue4;
				 
				temp_y=9576*(puc_y[x]+Brightness);
				temp_Guv=-3218*(puc_u[x2]-128)-6686*(puc_v[x2]-128);
				r = _S(((temp_y+13123*(puc_u[x2]-128))>>13)+ quant_error_r); 
				g = _S(((temp_y+temp_Guv)>>13)+quant_error_g); 
				b = _S(((temp_y+16591*(puc_v[x2]-128))>>13)+ quant_error_b); 
				quant_error_r=(r&r_adjust);
				quant_error_g=(g&g_adjust);
				quant_error_b=(b&b_adjust);
				hvalue=(unsigned short) _Ps565(r,g,b);
				
				temp_y=9576*(puc_y[x+stride_y]+Brightness);

				if (y<<31) 
				{ 
					temp_Guv=-3218*(puc_u[stride_uv+x2]-128)-6686*(puc_v[stride_uv+x2]-128);
					r+= _S(((temp_y+13123*(puc_u[stride_uv+x2]-128))>>13)+ quant_error_r2); 
					g+= _S(((temp_y+temp_Guv)>>13)+quant_error_g2); 
					b+= _S(((temp_y+16591*(puc_v[stride_uv+x2]-128))>>13)+ quant_error_b2); 
				}
				else
				{
					r+= _S(((temp_y+13123*(puc_u[x2]-128))>>13)+ quant_error_r2); 
					g+= _S(((temp_y+temp_Guv)>>13)+quant_error_g2); 
					b+= _S(((temp_y+16591*(puc_v[x2]-128))>>13)+ quant_error_b2); 
				}
				r = (r>>1);
				g = (g>>1);
				b = (b>>1);
				quant_error_r=(r&r_adjust);
				quant_error_g=(g&g_adjust);
				quant_error_b=(b&b_adjust);
				hvalue2=(unsigned short) _Ps565(r,g,b);

				if (x+xStep!=width_y)
				{
					temp_y=9576*(puc_y[x+xStep]+Brightness);
					temp_Guv=-3218*(puc_u[(x+xStep)>>1]-128)-6686*(puc_v[(x+xStep)>>1]-128);
					r += _S(((temp_y+13123*(puc_u[(x+xStep)>>1]-128))>>13)+ quant_error_r); 
					g += _S(((temp_y+temp_Guv)>>13)+quant_error_g); 
					b += _S(((temp_y+16591*(puc_v[(x+xStep)>>1]-128))>>13)+ quant_error_b); 
					r = (r>>1);
					g = (g>>1);
					b = (b>>1);
					quant_error_r=(r&r_adjust);
					quant_error_g=(g&g_adjust);
					quant_error_b=(b&b_adjust);
					hvalue3=(unsigned short)_Ps565(r,g,b);
					next_y=9576*(puc_y[x+xStep+stride_y]-16);

					if (y<<31) 
					{ 
						next_Guv=-3218*(puc_v[stride_uv+((x+xStep)>>1)]-128)-6686*(puc_u[stride_uv+((x+xStep)>>1)]-128);
						r += _S(((next_y+13123*(puc_u[stride_uv+((x+xStep)>>1)]-128))>>13)+ quant_error_r2); 
						g += _S(((next_y+next_Guv)>>13)+quant_error_g2); 
						b += _S(((next_y+16591*(puc_v[stride_uv+((x+xStep)>>1)]-128))>>13)+ quant_error_b2); 
					}
					else
					{
						r += _S(((next_y+13123*(puc_u[((x+xStep)>>1)]-128))>>13)+ quant_error_r2); 
						g += _S(((next_y+temp_Guv)>>13)+quant_error_g2); 
						b += _S(((next_y+16591*(puc_v[((x+xStep)>>1)]-128))>>13)+ quant_error_b2); 
					}
					r = (r>>1);
					g = (g>>1);
					b = (b>>1);
					quant_error_r=(r&r_adjust);
					quant_error_g=(g&g_adjust);
					quant_error_b=(b&b_adjust);
					hvalue4=(unsigned short) _Ps565(r,g,b);
				}
				else
				{
					hvalue2=hvalue;
				}
				pus_out[0] =hvalue|(hvalue3<<16);
				pus_out[we>>1]=hvalue2|(hvalue4<<16);
			}
			pus_out++;
		} 
		pus_out+=nExtra;
		puc_y += stride_y; 
		puc_yp += stride_y; 
		if (y<<31) 
		{ 
			puc_u += stride_uv; 
			puc_v += stride_uv; 
			puc_up += stride_uv; 
			puc_vp += stride_uv; 
		} 
	}
} 
#endif
#ifdef IPAQ //||(defined(MIPS))

void yuv2rgb_565(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y,int stride_dest, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int invert) 
{ 
//Floyd-Steinberg dithered version of the yuv2rgbHQ
	register int x,y; 
	unsigned int *pus_out; 
	register unsigned int r,g,b; 
	//unsigned char* LimitValue=&LimitValues[256];
	int xStart=0,xEnd=width_y,xStep=1;
	if (stride_dest>=0)
	{
		if (height_y>VisibleHeight)
		{
			puc_y+=(height_y-VisibleHeight)*(stride_y>>1);
			puc_u+=(height_y-VisibleHeight)*(stride_uv>>2);
			puc_v+=(height_y-VisibleHeight)*(stride_uv>>2);
			puc_yp+=(height_y-VisibleHeight)*(stride_y>>1);
			puc_up+=(height_y-VisibleHeight)*(stride_uv>>2);
			puc_vp+=(height_y-VisibleHeight)*(stride_uv>>2);
			height_y=VisibleHeight;
		}
		if (width_y>VisibleWidth)
		{
			puc_y+=(width_y-VisibleWidth)>>1;
			puc_u+=(width_y-VisibleWidth)>>2;
			puc_v+=(width_y-VisibleWidth)>>2;
			puc_yp+=(width_y-VisibleWidth)>>1;
			puc_up+=(width_y-VisibleWidth)>>2;
			puc_vp+=(width_y-VisibleWidth)>>2;
			width_y=VisibleWidth;
		}
		if (invert)
		{
			puc_y += (height_y - 1) * stride_y ; 
			puc_u += (height_y/2 - 1) * stride_uv; 
			puc_v += (height_y/2 - 1) * stride_uv; 
			puc_yp += (height_y - 1) * stride_y ; 
			puc_up += (height_y/2 - 1) * stride_uv; 
			puc_vp += (height_y/2 - 1) * stride_uv; 
			stride_y = -stride_y; 
			stride_uv = -stride_uv; 
			xStart=width_y-1;
			xEnd=-1;
			//lastpel=238;
			xStep=-1;
		}
		else
		{
			xEnd=width_y;
		}
	}
	else
	{
		if (width_y>VisibleHeight)
		{
			puc_y+=(width_y-VisibleHeight)>>1;
			puc_u+=(width_y-VisibleHeight)>>2;
			puc_v+=(width_y-VisibleHeight)>>2;
			width_y=VisibleHeight;
		}
		if (height_y>VisibleWidth)
		{
			puc_y+=(height_y-VisibleWidth)*(stride_y>>1);
			puc_u+=(height_y-VisibleWidth)*(stride_uv>>2);
			puc_v+=(height_y-VisibleWidth)*(stride_uv>>2);
			height_y=VisibleWidth;
		}
		xEnd=width_y;
		stride_dest=0;
//		lastpel=318;
	}
	if (Dither) 
	{
		yuv2rgb_565D(puc_y, stride_y, puc_u, puc_v, stride_uv, puc_out, width_y, height_y,stride_dest, Dither,Brightness,puc_yp,puc_up,puc_vp,slowopt,xStart,xEnd,xStep);
		return;
	}
	Brightness-=16;
	pus_out = (unsigned int *) (puc_out); 
	for (y=height_y; y!=0; y-=2)
	{ 		
//		if (y==lastpel) width_y-=2;
		for (x=xStart;x!=xEnd;x+=(xStep<<1))
		{ 
			int x2=x>>1;
			if ((!slowopt)||(puc_y[x]-puc_yp[x]||(puc_y[x+stride_y]-puc_yp[x+stride_y])||(puc_u[x>>1]-puc_up[x>>1])||(puc_v[x>>1]-puc_vp[x>>1])))
			{
				unsigned int hvalue;
				int y,u,v;
				y = (puc_y[x] + Brightness)*0x2568;
				u = puc_u[x2] - 128;
				v = puc_v[x2] - 128;
				r = _S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));
				hvalue = (unsigned int) _Ps565(r,g,b); 
				y = (puc_y[x+xStep] + Brightness)*0x2568;
				r =_S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));
				pus_out[0] = hvalue|(((unsigned int) _Ps5652(r,g,b))); 
				y = (puc_y[x+stride_y] + Brightness)*0x2568;
				r =_S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));
				hvalue = (unsigned int) _Ps565(r,g,b); 
				y = (puc_y[x+stride_y+xStep] + Brightness)*0x2568;
				r = _S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));
				pus_out[(width_y+stride_dest)>>1] = hvalue|(((unsigned int) _Ps5652(r,g,b))); 
			}
			pus_out++; 
		} 
		puc_y += stride_y<<1; 
		puc_yp+=stride_y<<1;
		pus_out+=(width_y>>1)+stride_dest;
		puc_u += stride_uv; 
		puc_v += stride_uv; 
		puc_up += stride_uv; 
		puc_vp += stride_uv; 
	} 
} 


void yuv2rgb_565Z(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int nExtra, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int invert) 
{ 

	register int x,y; 
	register signed int temp_y,temp_Guv,next_y,next_Guv; 
	register signed int temp,tempnextx,tempnexty,tempnextxy; 
	unsigned int *pus_out; 
	register unsigned int r,g,b; 
	int we;
	//unsigned char* LimitValue=&LimitValues[256];
	int xStart=0,xEnd=width_y,xStep=1;//,lastpel=318;
	if ((height_y<<1)>VisibleHeight) height_y=VisibleHeight>>1;
	if ((width_y<<1)>VisibleWidth) width_y=VisibleWidth>>1;

	if (nExtra<0)
	{
		nExtra=0;
	}
	else
	{
		if (invert)
		{
			puc_y += (height_y - 1) * stride_y ; 
			puc_u += (height_y/2 - 1) * stride_uv; 
			puc_v += (height_y/2 - 1) * stride_uv; 
			puc_yp += (height_y - 1) * stride_y ; 
			puc_up += (height_y/2 - 1) * stride_uv; 
			puc_vp += (height_y/2 - 1) * stride_uv; 
			stride_y = -stride_y; 
			stride_uv = -stride_uv; 
			xStart=width_y;
			xEnd=0;
			//lastpel=238;
			xStep=-1;
		}
		else
		{
			xEnd=width_y;
		}
	}
	if (Dither)
	{
		yuv2rgb_565ZD(puc_y, stride_y, puc_u, puc_v, stride_uv, puc_out, width_y, height_y,nExtra, Dither,Brightness, puc_yp,puc_up,puc_vp,slowopt,xStart,xEnd,xStep);
		return;
	}
	Brightness-=16;
	we=width_y+nExtra;
	pus_out = (unsigned int *) puc_out; 
	for (y=0; y<height_y; y++) 
	{ 
		for (x=xStart;x!=xEnd;x+=xStep)
		{ 
			int x2=x>>1;//add tolerance of 1 
			if ((!slowopt)||(puc_y[x]!=puc_yp[x]||(puc_u[x2]!=puc_up[x2])||(puc_v[x2]!=puc_vp[x2])))
			{
				unsigned int hvalue,hvalue2,hvalue3,hvalue4;
				 
				temp_y=9576*(puc_y[x]+Brightness);
				temp_Guv=-3218*(puc_u[x2]-128)-6686*(puc_v[x2]-128);
				r = _S(((temp_y+13123*(puc_u[x2]-128))>>13)); 
				g = _S(((temp_y+temp_Guv)>>13)); 
				b = _S(((temp_y+16591*(puc_v[x2]-128))>>13)); 
				hvalue=(unsigned short) _Ps565(r,g,b);
				
				temp_y=9576*(puc_y[x+stride_y]+Brightness);

				if (y<<31) 
				{ 
					temp_Guv=-3218*(puc_u[stride_uv+x2]-128)-6686*(puc_v[stride_uv+x2]-128);
					r+= _S(((temp_y+13123*(puc_u[stride_uv+x2]-128))>>13)); 
					g+= _S(((temp_y+temp_Guv)>>13)); 
					b+= _S(((temp_y+16591*(puc_v[stride_uv+x2]-128))>>13)); 
				}
				else
				{
					r+= _S(((temp_y+13123*(puc_u[x2]-128))>>13)); 
					g+= _S(((temp_y+temp_Guv)>>13)); 
					b+= _S(((temp_y+16591*(puc_v[x2]-128))>>13)); 
				}
				r = (r>>1);
				g = (g>>1);
				b = (b>>1);
				hvalue2=(unsigned short) _Ps565(r,g,b);

				if (x+xStep!=width_y)
				{
					temp_y=9576*(puc_y[x+xStep]+Brightness);
					temp_Guv=-3218*(puc_u[(x+xStep)>>1]-128)-6686*(puc_v[(x+xStep)>>1]-128);
					r += _S(((temp_y+13123*(puc_u[(x+xStep)>>1]-128))>>13)); 
					g += _S(((temp_y+temp_Guv)>>13)); 
					b += _S(((temp_y+16591*(puc_v[(x+xStep)>>1]-128))>>13)); 
					r = (r>>1);
					g = (g>>1);
					b = (b>>1);
					hvalue3=(unsigned short)_Ps565(r,g,b);
					next_y=9576*(puc_y[x+xStep+stride_y]-16);

					if (y<<31) 
					{ 
						next_Guv=-3218*(puc_v[stride_uv+((x+xStep)>>1)]-128)-6686*(puc_u[stride_uv+((x+xStep)>>1)]-128);
						r += _S(((next_y+13123*(puc_u[stride_uv+((x+xStep)>>1)]-128))>>13)); 
						g += _S(((next_y+next_Guv)>>13)); 
						b += _S(((next_y+16591*(puc_v[stride_uv+((x+xStep)>>1)]-128))>>13)); 
					}
					else
					{
						r += _S(((next_y+13123*(puc_u[((x+xStep)>>1)]-128))>>13)); 
						g += _S(((next_y+temp_Guv)>>13)); 
						b += _S(((next_y+16591*(puc_v[((x+xStep)>>1)]-128))>>13)); 
					}
					r = (r>>1);
					g = (g>>1);
					b = (b>>1);
					hvalue4=(unsigned short) _Ps565(r,g,b);
				}
				else
				{
					hvalue2=hvalue;
				}
				pus_out[0] =hvalue|(hvalue3<<16);
				pus_out[we>>1]=hvalue2|(hvalue4<<16);
			}
			pus_out++;
		} 
		pus_out+=nExtra;
		puc_y += stride_y; 
		puc_yp += stride_y; 
		if (y<<31) 
		{ 
			puc_u += stride_uv; 
			puc_v += stride_uv; 
			puc_up += stride_uv; 
			puc_vp += stride_uv; 
		} 
	}
/*	for (y=0; y<height_y; y++) 
	{ 
//		if (y==119) width_y-=1;
		for (x=xStart;x!=xEnd;x+=xStep)
		{ 
			unsigned int hvalue,hvalue2,hvalue1,hvalue3;
			int x2=x>>1;
			if ((!slowopt)||(puc_y[x]!=puc_yp[x]||(puc_u[x2]!=puc_up[x2])||(puc_v[x2]!=puc_vp[x2])))
			{
			 
				temp=puc_y[x]-144+Brightness; 
				r = _S(temp+puc_u[x2]); 
				g = _S(temp+128); 
				b = _S(temp+puc_v[x2]); 
				hvalue=(unsigned short) _Ps565(r,g,b);
				tempnexty=puc_y[x+stride_y]+Brightness; 
				if (y<<31) 
				{ 
					r = _S((temp+puc_u[x2]+tempnexty+puc_u[stride_uv+x2])/2); 
					g = _S((temp+tempnexty+256)/2); 
					b = _S((temp+puc_v[x2]+tempnexty+puc_v[stride_uv+x2])/2); 
				}
				else
				{
					r = _S((temp+puc_u[x2]+tempnexty+puc_u[x2])/2); 
					g = _S((temp+tempnexty+256)/2); 
					b = _S((temp+puc_v[x2]+tempnexty+puc_v[x2])/2); 
				}
				hvalue1=(unsigned short) _Ps565(r,g,b);
				if ((x+xStep!=width_y))
				{
					tempnextx=puc_y[x+xStep]+Brightness; 
					r = _S((temp+puc_u[x2]+tempnextx+puc_u[x2])/2); 
					g = _S((temp+tempnextx+256)/2); 
					b = _S((temp+puc_v[x2]+tempnextx+puc_v[x2])/2); 
					hvalue2=(unsigned short) _Ps565(r,g,b);
					tempnextxy=puc_y[x+xStep+stride_y]+Brightness; 

					if (y<<31) 
					{ 
						r = _S((temp+puc_u[x2]+tempnextxy+puc_u[stride_uv+x2])/2); 
						g = _S((temp+tempnextxy+256)/2); 
						b = _S((temp+puc_v[x2]+tempnextxy+puc_v[stride_uv+x2])/2); 
					}
					else
					{
						r = _S((temp+puc_u[x2]+tempnextxy+puc_u[x2])/2); 
						g = _S((temp+tempnextxy+256)/2); 
						b = _S((temp+puc_v[x2]+tempnextxy+puc_v[x2])/2); 
					}
					hvalue3=(unsigned short) _Ps565(r,g,b);
				}
				else
				{
					hvalue2=hvalue3=hvalue;
				}
				pus_out[0] = hvalue|(hvalue2<<16);
				pus_out[we>>1]=hvalue1|(hvalue3<<16);
			}
			pus_out++;
		} 
		pus_out+=nExtra;
		puc_y += stride_y; 
		puc_yp += stride_y; 

		if (y<<31) 
		{ 
			puc_u += stride_uv; 
			puc_v += stride_uv; 
			puc_up += stride_uv; 
			puc_vp += stride_uv; 
		} 
	}*/
//	memcpy(puc_out,hold,320*(height_y<<2));
} 
#else
void yuv2rgb_420(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y,int stride_out, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int invert) 
{ 
	int i;
	unsigned char * pauc_out_y,*pauc_out_u,*pauc_out_v;

	if (!invert)
	{
		if (width_y>VisibleWidth)
		{
			puc_y+=(width_y-VisibleWidth)>>1;
			puc_u+=(width_y-VisibleWidth)>>2;
			puc_v+=(width_y-VisibleWidth)>>2;
			stride_out=width_y=VisibleWidth;
		}
		if (height_y>VisibleHeight)
		{
			puc_y+=(height_y-VisibleHeight)*(stride_y>>1);
			puc_u+=(height_y-VisibleHeight)*(stride_uv>>2);
			puc_v+=(height_y-VisibleHeight)*(stride_uv>>2);
			height_y=VisibleHeight;
		}

	}

	pauc_out_y = (unsigned char *)puc_out;
	if (!invert)
	{
		pauc_out_u = (unsigned char *)(puc_out + stride_out * height_y);
		pauc_out_v = (unsigned char *)(puc_out + stride_out * height_y * 5 / 4);
	}
	else if (invert==2)
	{
		pauc_out_u = (unsigned char *)(puc_out);
		pauc_out_v = (unsigned char *)(puc_out + (stride_out>>1) * (height_y>>1));
	}

	if (invert!=2)
	{

		for (i=0; i<height_y; i++) 
		{
			if (!Brightness)
				memcpy(pauc_out_y, puc_y, width_y);
			else
			{
				int y;
				for (y=0;y<width_y;y+=4)
				{
					unsigned int c0,c1,c2,c3;
					c0=_S(puc_y[y]+Brightness);
					c1=_S(puc_y[y+1]+Brightness);
					c2=_S(puc_y[y+2]+Brightness);
					c3=_S(puc_y[y+3]+Brightness);
					((unsigned int*)pauc_out_y)[y>>2]=(unsigned int)(c0|(c1<<8)|(c2<<16)|(c3<<24));
					//((unsigned int*)pauc_out_y)[y>>2]=(unsigned int)((((unsigned int)_S(puc_y[y]+Brightness)))|(((unsigned int)_S(puc_y[y+1]+Brightness))<<8)|(((unsigned int)_S(puc_y[y+2]+Brightness))<<16)|(((unsigned int)_S(puc_y[y+3]+Brightness))<<24));
				}
			}

			pauc_out_y += stride_out;
			puc_y += stride_y;
				
		}
		if (invert==1) return;
	}
	for (i=0; i<height_y>>1; i++) {
			int y;
			for (y=0;y<width_y>>1;y+=4)
			{
//			memcpy(pauc_out_u, puc_u, width_y>>1); // U and V buffer must be inverted
				((unsigned int*)pauc_out_u)[y>>2]=(unsigned int)(puc_u[y]|(puc_u[y+1]<<8)|(puc_u[y+2]<<16)|(puc_u[y+3]<<24));
			}
			pauc_out_u += stride_out>>1;
			puc_u += stride_uv;
	}
	for (i=0; i<height_y>>1; i++) {
			int y;
			for (y=0;y<width_y>>1;y+=4)
			{
				((unsigned int*)pauc_out_v)[y>>2]=(unsigned int)(puc_v[y]|(puc_v[y+1]<<8)|(puc_v[y+2]<<16)|(puc_v[y+3]<<24));
			}
//			memcpy(pauc_out_v, puc_v, width_y>>1);
			pauc_out_v += stride_out>>1;
			puc_v += stride_uv;
	}
}

#ifndef ATI

void yuv2rgb_565Z(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int nExtra, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int invert) 
{ 
	register int x,y; 
	register signed int temp,tempnextx,tempnexty,tempnextxy; 
	unsigned short *pus_out; 
	register unsigned int r,g,b; 
//	unsigned char* LimitValue=&LimitValues[256];
	int we;
	if ((height_y<<1)>VisibleHeight) height_y=VisibleHeight>>1;
	if ((width_y<<1)>VisibleWidth) width_y=VisibleWidth>>1;
	if (nExtra<0) nExtra=0;
	if (Dither) 
	{
		yuv2rgb_565ZD(puc_y, stride_y, puc_u, puc_v, stride_uv, puc_out, width_y, height_y,nExtra, Dither,Brightness,puc_yp,puc_up,puc_vp,slowopt,0,width_y,1);
		return;
	}
	Brightness-=144;
	we=width_y+nExtra;
	pus_out = (unsigned short *) puc_out; 
	for (y=0; y<height_y; y++) 
	{ 
		for (x=0;x<width_y;x++)
		{ 
			unsigned short hvalue,hvalue2,hvalue1,hvalue3;
			int x2=x>>1;
			if ((!slowopt)||(puc_y[x]!=puc_yp[x]||(puc_u[x2]!=puc_up[x2])||(puc_v[x2]!=puc_vp[x2])))
			{
			 
				temp=puc_y[x]+Brightness; 
				r = _S(temp+puc_u[x2]); 
				g = _S(temp+128); 
				b = _S(temp+puc_v[x2]); 
				hvalue=(unsigned short) _Ps565(r,g,b);
				tempnexty=puc_y[x+stride_y]+Brightness; 
				if (y<<31) 
				{ 
					r = _S((temp+puc_u[x2]+tempnexty+puc_u[stride_uv+x2])/2); 
					g = _S((temp+tempnexty+256)/2); 
					b = _S((temp+puc_v[x2]+tempnexty+puc_v[stride_uv+x2])/2); 
				}
				else
				{
					r = _S((temp+puc_u[x2]+tempnexty+puc_u[x2])/2); 
					g = _S((temp+tempnexty+256)/2); 
					b = _S((temp+puc_v[x2]+tempnexty+puc_v[x2])/2); 
				}
				hvalue1=(unsigned short) _Ps565(r,g,b);
				if ((x+1!=width_y))
				{
					tempnextx=puc_y[x+1]+Brightness; 
					r = _S((temp+puc_u[x2]+tempnextx+puc_u[x2])/2); 
					g = _S((temp+tempnextx+256)/2); 
					b = _S((temp+puc_v[x2]+tempnextx+puc_v[x2])/2); 
					hvalue2=(unsigned short) _Ps565(r,g,b);
					tempnextxy=puc_y[x+1+stride_y]+Brightness; 

					if (y<<31) 
					{ 
						r = _S((temp+puc_u[x2]+tempnextxy+puc_u[stride_uv+x2])/2); 
						g = _S((temp+tempnextxy+256)/2); 
						b = _S((temp+puc_v[x2]+tempnextxy+puc_v[stride_uv+x2])/2); 
					}
					else
					{
						r = _S((temp+puc_u[x2]+tempnextxy+puc_u[x2])/2); 
						g = _S((temp+tempnextxy+256)/2); 
						b = _S((temp+puc_v[x2]+tempnextxy+puc_v[x2])/2); 
					}
					hvalue3=(unsigned short) _Ps565(r,g,b);
				}
				else
				{
					hvalue2=hvalue3=hvalue;
				}
				pus_out[0] = hvalue;
				pus_out[1] = hvalue2;
				pus_out[we]=hvalue1;
				pus_out[we+1]=hvalue3;
			}
			pus_out+=2;
		} 
		pus_out+=nExtra<<1;
		puc_y += stride_y; 
		puc_yp += stride_y; 

		if (y<<31) 
		{ 
			puc_u += stride_uv; 
			puc_v += stride_uv; 
			puc_up += stride_uv; 
			puc_vp += stride_uv; 
		} 
	}
} 
#endif
/*
void yuv2rgb_565(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y,int stride_dest, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt) 
{ 
	register int x,y; 
	register signed int temp; 
	unsigned short *pus_out; 
	register unsigned int r,g,b; 
	if (height_y>VisibleHeight)
	{
		puc_y+=(height_y-VisibleHeight)*(stride_y>>1);
		puc_u+=(height_y-VisibleHeight)*(stride_uv>>2);
		puc_v+=(height_y-VisibleHeight)*(stride_uv>>2);
		puc_yp+=(height_y-VisibleHeight)*(stride_y>>1);
		puc_up+=(height_y-VisibleHeight)*(stride_uv>>2);
		puc_vp+=(height_y-VisibleHeight)*(stride_uv>>2);
		height_y=VisibleHeight;
	}
	if (width_y>VisibleWidth)
	{
		puc_y+=(width_y-VisibleWidth)>>1;
		puc_u+=(width_y-VisibleWidth)>>2;
		puc_v+=(width_y-VisibleWidth)>>2;
		puc_yp+=(width_y-VisibleWidth)>>1;
		puc_up+=(width_y-VisibleWidth)>>2;
		puc_vp+=(width_y-VisibleWidth)>>2;
		width_y=VisibleWidth;
	}
	pus_out = (unsigned short *) (puc_out); 

	for (y=0; y<height_y; y++)
	{ 
		for (x=0;x<width_y;x++)
		{ 
			int x2=x>>1;
			if ((!slowopt)||(puc_y[x]!=puc_yp[x]||(puc_u[x2]!=puc_up[x2])||(puc_v[x2]!=puc_vp[x2])))
			{
				temp=puc_y[x]-144; 
				r = _S(temp+puc_u[x2]); 
				g = _S(temp+128); 
				b = _S(temp+puc_v[x2]); 
				pus_out[0] = (unsigned short) _Ps565(r,g,b); 
			}
			pus_out++; 
		} 
		puc_y += stride_y; 
		puc_yp+=stride_y;
		pus_out+=stride_dest;
		if (y<<31)
		{
			puc_u += stride_uv; 
			puc_v += stride_uv; 
			puc_up += stride_uv; 
			puc_vp += stride_uv; 
		}
	} 
} 
*/

void yuv2rgb_565(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y,int stride_dest, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int invert) 
{ 
	register int x,y; 
	unsigned int *pus_out; 
	register unsigned int r,g,b; 
//	unsigned char* LimitValue=&LimitValues[256];
	if (height_y>VisibleHeight)
	{
		puc_y+=(height_y-VisibleHeight)*(stride_y>>1);
		puc_u+=(height_y-VisibleHeight)*(stride_uv>>2);
		puc_v+=(height_y-VisibleHeight)*(stride_uv>>2);
		puc_yp+=(height_y-VisibleHeight)*(stride_y>>1);
		puc_up+=(height_y-VisibleHeight)*(stride_uv>>2);
		puc_vp+=(height_y-VisibleHeight)*(stride_uv>>2);
		height_y=VisibleHeight;
	}
	if (width_y>VisibleWidth)
	{
		puc_y+=(width_y-VisibleWidth)>>1;
		puc_u+=(width_y-VisibleWidth)>>2;
		puc_v+=(width_y-VisibleWidth)>>2;
		puc_yp+=(width_y-VisibleWidth)>>1;
		puc_up+=(width_y-VisibleWidth)>>2;
		puc_vp+=(width_y-VisibleWidth)>>2;
		width_y=VisibleWidth;
	}
#ifndef ATI
	if (Dither) 
	{
		yuv2rgb_565D(puc_y, stride_y, puc_u, puc_v, stride_uv, puc_out, width_y, height_y,stride_dest, Dither,Brightness,puc_yp,puc_up,puc_vp,slowopt,0,width_y,1);
		return;
	}
#endif
	pus_out = (unsigned int *) (puc_out); 
	Brightness-=16;
	for (y=0; y<height_y; y+=2)
	{ 		
//		if (y==lastpel) width_y-=2;
		for (x=0;x!=width_y;x+=2)
		{ 
			int x2=x>>1;
			if ((!slowopt)||(puc_y[x]-puc_yp[x]||(puc_y[x+stride_y]-puc_yp[x+stride_y])||(puc_u[x>>1]-puc_up[x>>1])||(puc_v[x>>1]-puc_vp[x>>1])))
			{
				unsigned int hvalue;
				int y,u,v;
				y = (puc_y[x] + Brightness)*0x2568;
				u = puc_u[x2] - 128;
				v = puc_v[x2] - 128;
				r = _S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));

				hvalue = (unsigned int) _Ps565(r,g,b); 
				y = (puc_y[x+1] + Brightness)*0x2568;
				r = _S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));
				pus_out[0] = hvalue|(((unsigned int) _Ps5652(r,g,b))); 
				y = (puc_y[x+stride_y] + Brightness)*0x2568;
				r = _S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));
				hvalue = (unsigned int) _Ps565(r,g,b); 
				y = (puc_y[x+stride_y+1] + Brightness)*0x2568;
				r = _S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));
				pus_out[(width_y+stride_dest)>>1] = hvalue|(((unsigned int) _Ps5652(r,g,b))); 
			}
			pus_out++; 
		} 
		puc_y += stride_y<<1; 
		puc_yp+=stride_y<<1;
		pus_out+=(width_y>>1)+stride_dest;
		puc_u += stride_uv; 
		puc_v += stride_uv; 
		puc_up += stride_uv; 
		puc_vp += stride_uv; 
	} 
} 
#endif
#else

void yuv2rgb_565(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y,int stride_dest, int Dither) 
{ 

	register int x,y; 
	register signed int temp; 
	unsigned short *pus_out; 
	register unsigned int r,g,b; 
	if (stride_dest<0) stride_dest=0;
	if (height_y>VisibleHeight)
	{
		puc_y+=(height_y-VisibleHeight)*(stride_y>>1);
		puc_u+=(height_y-VisibleHeight)*(stride_uv>>2);
		puc_v+=(height_y-VisibleHeight)*(stride_uv>>2);
		height_y=VisibleHeight;
	}
	if (width_y>VisibleWidth)
	{
		puc_y+=(width_y-VisibleWidth)>>1;
		puc_u+=(width_y-VisibleWidth)>>2;
		puc_v+=(width_y-VisibleWidth)>>2;
		width_y=VisibleWidth;
	}
	pus_out = (unsigned short *) puc_out; 
	for (y=0; y<height_y; y+=2) 
	{ 
		for (x=0;x<width_y;x++)
		{ 
			int y,u,v;
			y = (puc_y[x] - 16)*0x2568;
			u = puc_u[x>>1] - 128;
			v = puc_v[x>>1] - 128;
			r = _S(_R(y,u,v));
			g= _S(_G(y,u,v));
			b=_S(_B(y,u,v));
//			temp=puc_y[x]-144; 
//			r = _S(temp+puc_u[x>>1]); 
//			g = _S(temp+128); 
//			b = _S(temp+puc_v[x>>1]); 
			pus_out[0] = (unsigned short) _Ps565(r,g,b); 
			pus_out++; 
		} 
		puc_y += stride_y; 
		pus_out+=stride_dest; 
		for (x=0;x<width_y;x++)
		{ 
			int y,u,v;
			y = (puc_y[x] - 16)*0x2568;
			u = puc_u[x>>1] - 128;
			v = puc_v[x>>1] - 128;
			r = _S(_R(y,u,v));
			g= _S(_G(y,u,v));
			b=_S(_B(y,u,v));
////			temp=puc_y[x]-144; 
//			r = _S(temp+puc_u[x>>1]); 
//			g = _S(temp+128); 
//			b = _S(temp+puc_v[x>>1]); 
			pus_out[0] = (unsigned short) _Ps565(r,g,b); 
			pus_out++; 
		} 
		puc_y += stride_y; 
		pus_out+=stride_dest; 
		puc_u += stride_uv; 
		puc_v += stride_uv; 
	} 
} 

void yuv2rgb_565Z(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int nExtra, int Dither) 
{ 

	register int x,y; 
	register signed int temp; 
	unsigned short *pus_out; 
	register unsigned int r,g,b; 
	int we;
	if (nExtra<0) nExtra=0;
	if ((height_y<<1)>VisibleHeight) height_y=VisibleHeight>>1;
	if ((width_y<<1)>VisibleWidth) width_y=VisibleWidth>>1;
	we=width_y+nExtra;
	pus_out = (unsigned short *) puc_out; 
	for (y=0; y<height_y; y++) 
	{ 
		for (x=0;x<width_y;x++)
		{ 
			temp=puc_y[x]-144; 
			r = _S(temp+puc_u[x>>1]); 
			g = _S(temp+128); 
			b = _S(temp+puc_v[x>>1]); 
			pus_out[0] = pus_out[1]= pus_out[we] = pus_out[we+1]=(unsigned short) _Ps565(r,g,b); 
			pus_out+=2;

		} 
		pus_out+=nExtra<<1;
		puc_y += stride_y; 
		if (y<<31) 
		{ 
			puc_u += stride_uv; 
			puc_v += stride_uv; 
		} 
	} 
} 
#endif
#endif

#ifdef ARM
#ifndef ATI
void yuv2rgb_565RD(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int Extra, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int xStart, int xEnd, int xStep)
{ 
//Floyd-Steinberg dithered version of the yuv2rgbHQ
	register int x,y; 
	register int temp_y,temp_Guv,temp_Guv2,temp_Guv1; 
	register int quant_error_r,quant_error_g,quant_error_b;
	unsigned int *pus_out; 
	register unsigned int r,g,b; 
//	unsigned char* LimitValue=&LimitValues[256];
	int hvalue,hvalue1;
	int r_adjust=(0x0F);
	int g_adjust=(0x0F);
	int b_adjust=(0x0F);
	if (Dither==1)
	{
		r_adjust=(0x07);
		g_adjust=(0x03);
		b_adjust=(0x07);
	}
	quant_error_r=quant_error_g=quant_error_b=0;
	Brightness-=16;
	for (y=0; y<height_y; y+=2) 
	{ 
		pus_out = (unsigned int *) puc_out;
		pus_out+=y>>1;
//		if (y==lastpel) width_y-=2;
		for (x=xStart;x!=xEnd;x+=(xStep<<1))
		{ 
			if ((!slowopt)||(puc_y[x]-puc_yp[x]||(puc_y[x+xStep]-puc_yp[x+xStep])||(puc_u[x>>1]-puc_up[x>>1])||(puc_v[x>>1]-puc_vp[x>>1])))
			{
				temp_y=9576*(puc_y[x]+Brightness);
				temp_Guv=-3218*(puc_u[x>>1]-128)-6686*(puc_v[x>>1]-128);
				temp_Guv2=16591*(puc_v[x>>1]-128);
				temp_Guv1=13123*(puc_u[x>>1]-128);
				r = _S(((temp_y+temp_Guv1)>>13)+ quant_error_r); 
				g = _S(((temp_y+temp_Guv)>>13)+quant_error_g); 
				b = _S(((temp_y+temp_Guv2)>>13)+ quant_error_b);  
				quant_error_r=(r&r_adjust);
				quant_error_g=(g&g_adjust);
				quant_error_b=(b&b_adjust);
				hvalue=(unsigned int) _Ps565(r,g,b);

				
				temp_y=9576*(puc_y[x+xStep]+Brightness);

				r =  _S(((temp_y+temp_Guv1)>>13)+ quant_error_r); 
				g =  _S(((temp_y+temp_Guv)>>13)+quant_error_g); 
				b =  _S(((temp_y+temp_Guv2)>>13)+ quant_error_b); 
				quant_error_r=(r&r_adjust);
				quant_error_g=(g&g_adjust);
				quant_error_b=(b&b_adjust);
				hvalue1=(unsigned int) _Ps565(r,g,b);
				
				temp_y=9576*(puc_y[x+stride_y]+Brightness);
				r =  _S(((temp_y+temp_Guv1)>>13)+ quant_error_r); 
				g =  _S(((temp_y+temp_Guv)>>13)+quant_error_g); 
				b =  _S(((temp_y+temp_Guv2)>>13)+ quant_error_b); 
				quant_error_r=(r&r_adjust);
				quant_error_g=(g&g_adjust);
				quant_error_b=(b&b_adjust);

				pus_out[0] = hvalue|((unsigned int) _Ps5652(r,g,b)); 

				temp_y=9576*(puc_y[x+stride_y+xStep]+Brightness);
				r =  _S(((temp_y+temp_Guv1)>>13)+ quant_error_r); 
				g =  _S(((temp_y+temp_Guv)>>13)+quant_error_g); 
				b =  _S(((temp_y+temp_Guv2)>>13)+ quant_error_b); 
				quant_error_r=(r&r_adjust);
				quant_error_g=(g&g_adjust);
				quant_error_b=(b&b_adjust);

				pus_out[(height_y+Extra)>>1] = hvalue1|(((unsigned int) _Ps5652(r,g,b))); 



			}
			pus_out+=(height_y+Extra);
		} 
		puc_y += stride_y<<1; 
		puc_yp += stride_y<<1; 
		puc_u += stride_uv; 
		puc_v += stride_uv; 

		puc_up += stride_uv; 
		puc_vp += stride_uv; 
	} 
} 


void yuv2rgb_565RZD(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int nExtra, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int xStart, int xEnd, int xStep) 
{ 

	register int x,y; 
	register signed int temp_y,temp_Guv; 
	unsigned int *pus_out; 
	register unsigned int r,g,b; 
	unsigned int hvalue,hvalue2,hvalue3,hvalue4;
//	unsigned char* LimitValue=&LimitValues[256];
	int quant_error_r,quant_error_g,quant_error_b,quant_error_r2,quant_error_g2,quant_error_b2=0;
	int r_adjust=(0x0F);
	int g_adjust=(0x0F);
	int b_adjust=(0x0F);
	if (Dither==1)
	{
		r_adjust=(0x07);
		g_adjust=(0x03);
		b_adjust=(0x07);
	}
	Brightness-=16;
	pus_out = (unsigned int *) puc_out; 
	for (y=0; y<height_y; y++) 
	{ 
		pus_out = (unsigned int *) puc_out;
		pus_out+=y;
		quant_error_b=quant_error_g=quant_error_r=quant_error_r2=quant_error_g2=quant_error_b2=6;
		for (x=xStart;x!=xEnd;x+=xStep)
		{ 
			temp_y=9576*(puc_y[x]+Brightness);
			temp_Guv=-3218*(puc_u[x>>1]-128)-6686*(puc_v[x>>1]-128);
			r = _S(((temp_y+13123*(puc_u[x>>1]-128))>>13)+ quant_error_r); 
			g = _S(((temp_y+temp_Guv)>>13)+quant_error_g); 
			b = _S(((temp_y+16591*(puc_v[x>>1]-128))>>13)+ quant_error_b); 
			quant_error_r=(r&r_adjust);
			quant_error_g=(g&g_adjust);
			quant_error_b=(b&b_adjust);
			hvalue=(unsigned int) _Ps565(r,g,b);
			temp_y=9576*(puc_y[x]+Brightness);
			temp_Guv=-3218*(puc_u[x>>1]-128)-6686*(puc_v[x>>1]-128);
			r = _S(((temp_y+13123*(puc_u[x>>1]-128))>>13)+ quant_error_r); 
			g = _S(((temp_y+temp_Guv)>>13)+quant_error_g); 
			b = _S(((temp_y+16591*(puc_v[x>>1]-128))>>13)+ quant_error_b); 
			quant_error_r=(r&r_adjust);
			quant_error_g=(g&g_adjust);
			quant_error_b=(b&b_adjust);
			hvalue2=(unsigned int) _Ps565(r,g,b);
			if (/*Dither&&*/(x+xStep!=xEnd))
			{
				temp_y=9576*(puc_y[x+xStep]+Brightness);
				temp_Guv=-3218*(puc_u[(x+xStep)>>1]-128)-6686*(puc_v[(x+xStep)>>1]-128);
				r += _S(((temp_y+13123*(puc_u[(x+xStep)>>1]-128))>>13)+ quant_error_r2); 
				g += _S(((temp_y+temp_Guv)>>13)+quant_error_g2); 
				b += _S(((temp_y+16591*(puc_v[(x+xStep)>>1]-128))>>13)+ quant_error_b2); 
				r = (r>>1);
				g = (g>>1);
				b = (b>>1);
				quant_error_r=(r&r_adjust);
				quant_error_g=(g&g_adjust);
				quant_error_b=(b&b_adjust);
				hvalue3=(unsigned int) _Ps565(r,g,b);
				r += _S(((temp_y+13123*(puc_u[(x+xStep)>>1]-128))>>13)+ quant_error_r2); 
				g += _S(((temp_y+temp_Guv)>>13)+quant_error_g2); 
				b += _S(((temp_y+16591*(puc_v[(x+xStep)>>1]-128))>>13)+ quant_error_b2); 
				r = (r>>1);
				g = (g>>1);
				b = (b>>1);
				quant_error_r=(r&r_adjust);
				quant_error_g=(g&g_adjust);
				quant_error_b=(b&b_adjust);
				hvalue4=(unsigned int) _Ps565(r,g,b);
			}
			else
			{
				hvalue2=hvalue;
			}
			pus_out[0] =hvalue|(hvalue2<<16); 
			pus_out[(height_y+nExtra)>>1] = hvalue3|(hvalue4<<16); 
			pus_out+=((height_y+nExtra));

		} 
		puc_y += stride_y; 
		if (y<<31) 
		{ 
			puc_u += stride_uv; 
			puc_v += stride_uv; 
		} 
	} 
} 

#endif
#ifdef IPAQ

void yuv2rgb_565R(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int Extra, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int invert) 
{ 
//Floyd-Steinberg dithered version of the yuv2rgbHQ
 register int x,y;
	unsigned int *pus_out; 
	register unsigned int r,g,b; 
	int hvalue,hvalue1;
	int xStart=0,xEnd=-1,xStep=-1;//,lastpel=318;
//	unsigned char* LimitValue=&LimitValues[256];
	if (Extra<0)
	{
		if (height_y>VisibleHeight)
		{
			puc_y+=(height_y-VisibleHeight)*(stride_y>>1);
			puc_u+=(height_y-VisibleHeight)*(stride_uv>>2);
			puc_v+=(height_y-VisibleHeight)*(stride_uv>>2);
			height_y=VisibleHeight;
		}
		if (width_y>VisibleWidth)
		{
			puc_y+=(width_y-VisibleWidth)>>1;
			puc_u+=(width_y-VisibleWidth)>>2;
			puc_v+=(width_y-VisibleWidth)>>2;
			width_y=VisibleWidth;
		}
		xStart=width_y-1;
		Extra=0;
	}
	else
	{
		if (width_y>VisibleHeight)
		{
			puc_y+=(width_y-VisibleHeight)>>1;
			puc_u+=(width_y-VisibleHeight)>>2;
			puc_v+=(width_y-VisibleHeight)>>2;
			puc_yp+=(width_y-VisibleHeight)>>1;
			puc_up+=(width_y-VisibleHeight)>>2;
			puc_vp+=(width_y-VisibleHeight)>>2;
			width_y=VisibleHeight;
		}
		if (height_y>VisibleWidth)
		{
			puc_y+=(height_y-VisibleWidth)*(stride_y>>1);
			puc_u+=(height_y-VisibleWidth)*(stride_uv>>2);
			puc_v+=(height_y-VisibleWidth)*(stride_uv>>2);
			puc_yp+=(height_y-VisibleWidth)*(stride_y>>1);
			puc_up+=(height_y-VisibleWidth)*(stride_uv>>2);
			puc_vp+=(height_y-VisibleWidth)*(stride_uv>>2);
			height_y=VisibleWidth;
		}
		if (!invert)
		{
			puc_y += (height_y - 1) * stride_y ; 
			puc_u += (height_y/2 - 1) * stride_uv; 
			puc_v += (height_y/2 - 1) * stride_uv; 
			puc_yp += (height_y - 1) * stride_y ; 
			puc_up += (height_y/2 - 1) * stride_uv; 
			puc_vp += (height_y/2 - 1) * stride_uv; 
			stride_y = -stride_y; 
			stride_uv = -stride_uv; 
			xStart=0;
			xEnd=width_y;
			//lastpel=238;
			xStep=1;
		}
		else
		{
			xStart=width_y-1;
		}
	}
	if (Dither)
	{
		yuv2rgb_565RD(puc_y, stride_y, puc_u, puc_v, stride_uv, puc_out, width_y, height_y,Extra, Dither, Brightness, puc_yp,puc_up,puc_vp,slowopt,xStart,xEnd,xStep);
		return;
	}

	
	
	Brightness-=16;
	
 
 
	for (y=0; y<height_y; y+=2) 
	{ 
		pus_out = (unsigned int *) puc_out;
		pus_out+=y>>1;
		for (x=xStart;x!=xEnd;x+=(xStep<<1))
		{ 
			if ((!slowopt)||(puc_y[x]-puc_yp[x]||(puc_y[x+xStep]-puc_yp[x+xStep])||(puc_u[x>>1]-puc_up[x>>1])||(puc_v[x>>1]-puc_vp[x>>1])))
			{
				int y,u,v;
				y = (puc_y[x] + Brightness)*0x2568;
				u = puc_u[x>>1] - 128;
				v = puc_v[x>>1] - 128;
				r = _S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));
//				temp=puc_y[x]+Brightness; 
//				r = _S(temp+puc_u[x>>1]); 
//				g = _S(temp+128); 
//				b = _S(temp+puc_v[x>>1]); 
				hvalue=(unsigned int) _Ps565(r,g,b);
				y = (puc_y[x+xStep] + Brightness)*0x2568;
				r = _S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));
//				
//				temp=puc_y[x+xStep]+Brightness; 
//				r = _S(temp+puc_u[x>>1]); 
//				g = _S(temp+128); 
//				b = _S(temp+puc_v[x>>1]); 
				hvalue1=(unsigned int) _Ps565(r,g,b);

				y = (puc_y[x+stride_y] + Brightness)*0x2568;
				r = _S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));

//				temp=puc_y[x+stride_y]+Brightness; 
//				r = _S(temp+puc_u[x>>1]); 
//				g = _S(temp+128); 
//				b = _S(temp+puc_v[x>>1]); 

				pus_out[0] = hvalue|((unsigned int) _Ps5652(r,g,b)); 

//				temp=puc_y[x+stride_y+xStep]+Brightness; 
//				r = _S(temp+puc_u[x>>1]); 
//				g = _S(temp+128); 
//				b = _S(temp+puc_v[x>>1]); 
				y = (puc_y[x+stride_y+xStep] + Brightness)*0x2568;
				r = _S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));

				pus_out[(height_y+Extra)>>1] = hvalue1|(((unsigned int) _Ps5652(r,g,b))); 

			}
			pus_out+=(height_y+Extra);
		} 
		puc_y += stride_y<<1; 
		puc_yp += stride_y<<1; 
		puc_u += stride_uv; 
		puc_v += stride_uv; 

		puc_up += stride_uv; 
		puc_vp += stride_uv; 
	} 
} 

void yuv2rgb_565RZ(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int nExtra, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int invert) 
{ 

	register int x,y; 
	register signed int temp,tempnext; 
	register signed int temp_y,temp_Guv; 
	unsigned int *pus_out; 
	register unsigned int r,g,b; 
	unsigned int hvalue,hvalue2,hvalue3,hvalue4;
	//unsigned char* LimitValue=&LimitValues[256];
	int xStart,xEnd=0,xStep=-1;
	if ((width_y<<1)>VisibleHeight) width_y=VisibleHeight>>1;
	if ((height_y<<1)>VisibleWidth) height_y=VisibleWidth>>1;
	xStart=width_y-1;
	if (nExtra>=0&&(!invert)) 
	{ 
		puc_y += (height_y - 1) * stride_y ; 
		puc_u += (height_y/2 - 1) * stride_uv; 
		puc_v += (height_y/2 - 1) * stride_uv; 
		puc_yp += (height_y - 1) * stride_y ; 
		puc_up += (height_y/2 - 1) * stride_uv; 
		puc_vp += (height_y/2 - 1) * stride_uv; 
		stride_y = -stride_y; 
		stride_uv = -stride_uv; 
		xStart=0;
		xEnd=width_y;
		xStep=1;
	} 
	else if (nExtra<0)
	{
		nExtra=0;
	}
	if (Dither)
	{
		yuv2rgb_565RZD(puc_y, stride_y, puc_u, puc_v, stride_uv, puc_out, width_y, height_y,nExtra, Dither,Brightness,puc_yp,puc_up,puc_vp,slowopt,xStart,xEnd,xStep);
		return;
	}
/*	Brightness-=144;
	pus_out = (unsigned int *) puc_out; 
	for (y=0; y<height_y; y++) 
	{ 
		pus_out = (unsigned int *) puc_out;
		pus_out+=y;
		for (x=xStart;x!=xEnd;x+=xStep)
		{ 
			temp=puc_y[x]+Brightness; 
			r = _S(temp+puc_u[x>>1]); 
			g = _S(temp+128); 
			b = _S(temp+puc_v[x>>1]); 
			hvalue=(unsigned int) _Ps565(r,g,b);
			if ((x+xStep!=xEnd))
			{
				tempnext=puc_y[x+xStep]+Brightness; 
				r = _S((temp+puc_u[x>>1]+tempnext+puc_u[(x+xStep)>>1])/2); 
				g = _S((temp+tempnext+256)/2); 
				b = _S((temp+puc_v[x>>1]+tempnext+puc_v[(x+xStep)>>1])/2); 
				hvalue2=(unsigned int) _Ps565(r,g,b);
			}
			else
			{
				hvalue2=hvalue;
			}
			pus_out[0]=hvalue|(hvalue<<16); 
			pus_out[(height_y+nExtra)>>1] =hvalue2|(hvalue2<<16); 
			pus_out+=((height_y+nExtra));

		} 
		puc_y += stride_y; 
		if (y<<31) 
		{ 
			puc_u += stride_uv; 
			puc_v += stride_uv; 
		} 
	} */

	Brightness-=16;
	pus_out = (unsigned int *) puc_out; 
	for (y=0; y<height_y; y++) 
	{ 
		pus_out = (unsigned int *) puc_out;
		pus_out+=y;
		for (x=xStart;x!=xEnd;x+=xStep)
		{ 
			temp_y=9576*(puc_y[x]+Brightness);
			temp_Guv=-3218*(puc_u[x>>1]-128)-6686*(puc_v[x>>1]-128);
			r = _S(((temp_y+13123*(puc_u[x>>1]-128))>>13)); 
			g = _S(((temp_y+temp_Guv)>>13)); 
			b = _S(((temp_y+16591*(puc_v[x>>1]-128))>>13)); 
			hvalue=(unsigned int) _Ps565(r,g,b);
			temp_y=9576*(puc_y[x]+Brightness);
			temp_Guv=-3218*(puc_u[x>>1]-128)-6686*(puc_v[x>>1]-128);
			r = _S(((temp_y+13123*(puc_u[x>>1]-128))>>13)); 
			g = _S(((temp_y+temp_Guv)>>13)); 
			b = _S(((temp_y+16591*(puc_v[x>>1]-128))>>13)); 
			hvalue2=(unsigned int) _Ps565(r,g,b);
			if (/*Dither&&*/(x+xStep!=xEnd))
			{
				temp_y=9576*(puc_y[x+xStep]+Brightness);
				temp_Guv=-3218*(puc_u[(x+xStep)>>1]-128)-6686*(puc_v[(x+xStep)>>1]-128);
				r += _S(((temp_y+13123*(puc_u[(x+xStep)>>1]-128))>>13)); 
				g += _S(((temp_y+temp_Guv)>>13)); 
				b += _S(((temp_y+16591*(puc_v[(x+xStep)>>1]-128))>>13)); 
				r = (r>>1);
				g = (g>>1);
				b = (b>>1);
				hvalue3=(unsigned int) _Ps565(r,g,b);
				r += _S(((temp_y+13123*(puc_u[(x+xStep)>>1]-128))>>13)); 
				g += _S(((temp_y+temp_Guv)>>13)); 
				b += _S(((temp_y+16591*(puc_v[(x+xStep)>>1]-128))>>13)); 
				r = (r>>1);
				g = (g>>1);
				b = (b>>1);
				hvalue4=(unsigned int) _Ps565(r,g,b);
			}
			else
			{
				hvalue2=hvalue;
			}
			pus_out[0] =hvalue|(hvalue2<<16); 
			pus_out[(height_y+nExtra)>>1] = hvalue3|(hvalue4<<16); 
			pus_out+=((height_y+nExtra));

		} 
		puc_y += stride_y; 
		if (y<<31) 
		{ 
			puc_u += stride_uv; 
			puc_v += stride_uv; 
		} 
	} 

} 

#else

void yuv2rgb_565R(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int Extra, int Dither,int Brightness ,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int invert)
{ 

	register int x,y; 
	unsigned int *pus_out; 
	int hvalue;
	register unsigned int r,g,b; 
	//unsigned char* LimitValue=&LimitValues[256];
	if (Extra<0) Extra=0;
	if (width_y>VisibleHeight)
	{
		puc_y+=(width_y-VisibleHeight)>>1;
		puc_u+=(width_y-VisibleHeight)>>2;
		puc_v+=(width_y-VisibleHeight)>>2;
		puc_yp+=(width_y-VisibleHeight)>>1;
		puc_up+=(width_y-VisibleHeight)>>2;
		puc_vp+=(width_y-VisibleHeight)>>2;
		width_y=VisibleHeight;
	}
	if (height_y>VisibleWidth)
	{
		puc_y+=(height_y-VisibleWidth)*(stride_y>>1);
		puc_u+=(height_y-VisibleWidth)*(stride_uv>>2);
		puc_v+=(height_y-VisibleWidth)*(stride_uv>>2);
		puc_yp+=(height_y-VisibleWidth)*(stride_y>>1);
		puc_up+=(height_y-VisibleWidth)*(stride_uv>>2);
		puc_vp+=(height_y-VisibleWidth)*(stride_uv>>2);
		height_y=VisibleWidth;
	}
#ifndef ATI
	if (Dither)
	{
		yuv2rgb_565RD(puc_y, stride_y, puc_u, puc_v, stride_uv, puc_out, width_y, height_y,Extra, Dither, Brightness, puc_yp,puc_up,puc_vp,slowopt,width_y-1,-1,-1);
		return;
	}
#endif
	Brightness-=16;
	for (y=0; y<height_y; y+=2) 
	{ 
		pus_out = (unsigned int *) puc_out;
		pus_out+=y>>1;
		for (x=width_y-1;x>0;x-=2)
		{ 
			if ((!slowopt)||(puc_y[x]-puc_yp[x]||(puc_y[x-1]-puc_yp[x-1])||(puc_u[x>>1]-puc_up[x>>1])||(puc_v[x>>1]-puc_vp[x>>1])))
			{
				int y,u,v;
				y = (puc_y[x] + Brightness)*0x2568;
				u = puc_u[x>>1] - 128;
				v = puc_v[x>>1] - 128;
				r = _S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));
//				temp=puc_y[x]+Brightness; 
//				r = _S(temp+puc_u[x>>1]); 
//				g = _S(temp+128); 
//				b = _S(temp+puc_v[x>>1]); 
				hvalue=(unsigned int) _Ps565(r,g,b);
				y = (puc_y[x+stride_y] + Brightness)*0x2568;
				r = _S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));
//				temp=puc_y[x+stride_y]+Brightness; 
//				r = _S(temp+puc_u[x>>1]); 
//				g = _S(temp+128); 
//				b = _S(temp+puc_v[x>>1]); 

				pus_out[0] = hvalue|((unsigned int) _Ps5652(r,g,b)); 

				y = (puc_y[x-1] + Brightness)*0x2568;
				r = _S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));
//				temp=puc_y[x-1]+Brightness; 
//				r = _S(temp+puc_u[x>>1]); 
//				g = _S(temp+128); 
//				b = _S(temp+puc_v[x>>1]); 
				hvalue=(unsigned int) _Ps565(r,g,b);
				y = (puc_y[x+stride_y-1] + Brightness)*0x2568;
				r = _S(_R(y,u,v));
				g= _S(_G(y,u,v));
				b=_S(_B(y,u,v));
//				temp=puc_y[x+stride_y-1]+Brightness; 
//				r = _S(temp+puc_u[x>>1]); 
//				g = _S(temp+128); 
//				b = _S(temp+puc_v[x>>1]); 

				pus_out[(height_y+Extra)>>1] = hvalue|(((unsigned int) _Ps5652(r,g,b))); 

			}
			pus_out+=(height_y+Extra);
		} 
		puc_y += stride_y<<1; 
		puc_yp += stride_y<<1; 
		puc_u += stride_uv; 
		puc_v += stride_uv; 

		puc_up += stride_uv; 
		puc_vp += stride_uv; 
	} 
} 

void yuv2rgb_420R(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y,int stride_out, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int invert) 
{ 
	int i;
	unsigned char * pauc_out_y,*pauc_out_u,*pauc_out_v;
	if (invert<2)
	{
		if (width_y>VisibleHeight)
		{
			puc_y+=(width_y-VisibleHeight)>>1;
			puc_u+=(width_y-VisibleHeight)>>2;
			puc_v+=(width_y-VisibleHeight)>>2;
			stride_out=width_y=VisibleHeight;
		}
		if (height_y>VisibleWidth)
		{
			puc_y+=(height_y-VisibleWidth)*(stride_y>>1);
			puc_u+=(height_y-VisibleWidth)*(stride_uv>>2);
			puc_v+=(height_y-VisibleWidth)*(stride_uv>>2);
			height_y=VisibleWidth;
		}
	}


	if (invert==0||(invert==2)) 
	{
		pauc_out_y = (unsigned char *)puc_out;
		for (i=0; i<height_y; i++) 
		{
			if (!Brightness)
				memcpy(pauc_out_y, puc_y, width_y);
			else
			{
				int y;
				for (y=0;y<width_y;y+=4)
				{
					unsigned int c0,c1,c2,c3;
					c0=_S(puc_y[y]+Brightness);
					c1=_S(puc_y[y+1]+Brightness);
					c2=_S(puc_y[y+2]+Brightness);
					c3=_S(puc_y[y+3]+Brightness);
					((unsigned int*)pauc_out_y)[y>>2]=(unsigned int)(c0|(c1<<8)|(c2<<16)|(c3<<24));
					//((unsigned int*)pauc_out_y)[y>>2]=(unsigned int)((((unsigned int)_S(puc_y[y]+Brightness)))|(((unsigned int)_S(puc_y[y+1]+Brightness))<<8)|(((unsigned int)_S(puc_y[y+2]+Brightness))<<16)|(((unsigned int)_S(puc_y[y+3]+Brightness))<<24));
				}
			}

			pauc_out_y += stride_out;
			puc_y += stride_y;
		}
	}
	else
	{
		pauc_out_u = (unsigned char *)(puc_out);
		pauc_out_v = (unsigned char *)(puc_out + (stride_out>>1)); //((stride_out * height_y)>>2));
		for (i=0; i<height_y>>1; i++) 
		{
			int y;
			for (y=0;y<width_y>>1;y+=4)
			{
				((unsigned int*)pauc_out_u)[y>>2]=(unsigned int)(puc_u[y]|(puc_u[y+1]<<8)|(puc_u[y+2]<<16)|(puc_u[y+3]<<24));
				((unsigned int*)pauc_out_v)[y>>2]=(unsigned int)(puc_v[y]|(puc_v[y+1]<<8)|(puc_v[y+2]<<16)|(puc_v[y+3]<<24));
			}
//			memcpy(pauc_out_u, puc_u, width_y>>1); // U and V buffer must be inverted
//			memcpy(pauc_out_v, puc_v, width_y>>1);

			pauc_out_u += stride_out;
			pauc_out_v += stride_out;
			puc_u += stride_uv;
			puc_v += stride_uv;
		}
	}
}

#ifndef ATI

#define WRITE_WIDTH_MAX 64

void yuv2rgb_565RD_e740(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int Extra, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int xStart, int xEnd, int xStep)
{

	register int x,y,z,writeWidth;
	register unsigned int r,g,b;
	register unsigned int *pus_out;
	register uint8_t *tempPuc_y, *tempPuc_u, *tempPuc_v;
	register int temp_y,temp_Guv,temp_Guv2,temp_Guv1; 
	register int quant_error_r,quant_error_g,quant_error_b;
	unsigned int hvalue;
	//unsigned char* LimitValue=&LimitValues[256];
	quant_error_r=quant_error_g=quant_error_b=0;

	pus_out = (unsigned int *) puc_out;
	Brightness-=16;

	for (y = width_y - 1, writeWidth = WRITE_WIDTH_MAX; y >= (writeWidth - 1 ); )
	{
		tempPuc_y = puc_y;
		tempPuc_u = puc_u;
		tempPuc_v = puc_v;

		for (x=0; x < (height_y-2);x+=2)
		{
			for(z = 0; z < writeWidth; z++ )
			{

				temp_y=9576*(tempPuc_y[y - z]+Brightness);
				temp_Guv=-3218*(tempPuc_u[(y - z)>>1]-128)-6686*(tempPuc_v[(y - z)>>1]-128);
				temp_Guv2=16591*(tempPuc_v[(y - z)>>1]-128);
				temp_Guv1=13123*(tempPuc_u[(y - z)>>1]-128);
				r = _S(((temp_y+temp_Guv1)>>13)+ quant_error_r); 
				g = _S(((temp_y+temp_Guv)>>13)+quant_error_g); 
				b = _S(((temp_y+temp_Guv2)>>13)+ quant_error_b);  
				hvalue=(unsigned int) _Ps565(r,g,b);
				quant_error_r=(r&0x07);
				quant_error_g=(g&0x03);
				quant_error_b=(b&0x07);
				

				temp_y=9576*((tempPuc_y + stride_y)[y - z]+Brightness);
				temp_Guv=-3218*((tempPuc_u + stride_uv)[(y - z)>>1]-128)-6686*((tempPuc_v + stride_uv)[(y - z)>>1]-128);
				temp_Guv2=16591*((tempPuc_v + stride_uv)[(y - z)>>1]-128);
				temp_Guv1=13123*((tempPuc_u + stride_uv)[(y - z)>>1]-128);
				r = _S(((temp_y+temp_Guv1)>>13)+ quant_error_r); 
				g = _S(((temp_y+temp_Guv)>>13)+quant_error_g); 
				b = _S(((temp_y+temp_Guv2)>>13)+ quant_error_b);  
				quant_error_r=(r&0x07);
				quant_error_g=(g&0x03);
				quant_error_b=(b&0x07);

				pus_out[(z * (height_y + Extra)) >> 1] = hvalue|(((unsigned int) _Ps5652(r,g,b)));

			}

			tempPuc_u += (stride_uv);
			tempPuc_v += (stride_uv);

			pus_out++;
			tempPuc_y += (stride_y << 1);

		}
		pus_out +=  ((height_y * (writeWidth - 1)) + (Extra   * writeWidth) + 2) >> 1;
		y -= writeWidth;
		if((y < WRITE_WIDTH_MAX) && (y > 0))
		{
			writeWidth = y + 1;

		}
	}
}

void yuv2rgb_565R_e740(uint8_t *puc_y, int stride_y,
uint8_t *puc_u, uint8_t *puc_v, int stride_uv,
uint8_t *puc_out, int width_y, int height_y, int Extra, int Dither,int Brightness ,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int invert)
{

	register int x,y,z,writeWidth;
	register unsigned int *pus_out;
	register uint8_t *tempPuc_y, *tempPuc_u, *tempPuc_v;
	//unsigned char* LimitValue=&LimitValues[256];

	if (Extra<0) Extra=0;
	if (width_y>VisibleHeight)
	{
		puc_y+=(width_y-VisibleHeight)>>1;
		puc_u+=(width_y-VisibleHeight)>>2;
		puc_v+=(width_y-VisibleHeight)>>2;
		puc_yp+=(width_y-VisibleHeight)>>1;
		puc_up+=(width_y-VisibleHeight)>>2;
		puc_vp+=(width_y-VisibleHeight)>>2;
		width_y=VisibleHeight;
	}
	if (height_y>VisibleWidth)
	{
		puc_y+=(height_y-VisibleWidth)*(stride_y>>1);
		puc_u+=(height_y-VisibleWidth)*(stride_uv>>2);
		puc_v+=(height_y-VisibleWidth)*(stride_uv>>2);
		puc_yp+=(height_y-VisibleWidth)*(stride_y>>1);
		puc_up+=(height_y-VisibleWidth)*(stride_uv>>2);
		puc_vp+=(height_y-VisibleWidth)*(stride_uv>>2);
		height_y=VisibleWidth;
	}
	if (Dither)
	{
		yuv2rgb_565RD_e740(puc_y, stride_y, puc_u, puc_v, stride_uv, puc_out, width_y, height_y,Extra, Dither, Brightness, puc_yp,puc_up,puc_vp,slowopt,width_y-1,-1,-1);
		return;
	}

	pus_out = (unsigned int *) puc_out;

	Brightness-=16;
	for (y = width_y - 1, writeWidth = WRITE_WIDTH_MAX; y >= (writeWidth - 1 ); )
	{
		tempPuc_y = puc_y;
		tempPuc_u = puc_u;
		tempPuc_v = puc_v;

		for (x=0; x < (height_y-2);x+=2)
		{
			for(z = 0; z < writeWidth; z++ )
			{

			int ty,u,v,r,g,b;
			unsigned int hvalue;
				ty = (tempPuc_y[y - z]+Brightness )*0x2568;
				u = tempPuc_u[(y - z)>>1] - 128;
				v = tempPuc_v[(y - z)>>1] - 128;
				r= _S(_R(ty,u,v));
				g= _S(_G(ty,u,v));
				b=_S(_B(ty,u,v));
				hvalue=_Ps565(r,g,b);
				ty = ((tempPuc_y + stride_y)[y - z]+Brightness )*0x2568;
				u = (tempPuc_u + stride_uv)[(y - z)>>1] - 128;
				v = (tempPuc_v + stride_uv)[(y - z)>>1] - 128;
				r = _S(_R(ty,u,v));
				g= _S(_G(ty,u,v));
				b=_S(_B(ty,u,v));
/*				temp=(tempPuc_y + stride_y)[y - z]+ Brightness;
				rgb =  ((_S(temp+(tempPuc_u + stride_uv)[(y - z)>>1]) & 0xF8) << 13);
				rgb |= ((_S(temp+128) & 0xFC) << 19);
				rgb |= ((_S(temp+(tempPuc_v + stride_uv)[(y - z)>>1]) & 0xF8) << 24);

				temp=tempPuc_y[y - z]+ Brightness;
				rgb |=  ((_S(temp+tempPuc_u[(y - z)>>1]) & 0xF8) >> 3);
				rgb |= ((_S(temp+128) & 0xFC) << 3);
				rgb |= ((_S(temp+tempPuc_v[(y - z)>>1]) & 0xF8) << 8);
*/
				pus_out[(z * (height_y + Extra)) >> 1] = hvalue|(unsigned int)_Ps5652(r,g,b);

			}

			tempPuc_u += (stride_uv);
			tempPuc_v += (stride_uv);

			pus_out++;
			tempPuc_y += (stride_y << 1);

		}
		pus_out +=  ((height_y * (writeWidth - 1)) + (Extra   * writeWidth) + 2) >> 1;
		y -= writeWidth;
		if((y < WRITE_WIDTH_MAX) && (y > 0))
		{
			writeWidth = y + 1;

		}
	}
}

#endif

/*
void yuv2rgb_565R(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int Extra, int Dither,int Brightness ,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt)
{ 

	register int x,y; 
	register signed int temp; 
	unsigned short *pus_out; 
	register unsigned int r,g,b; 
	if (Extra<0) Extra=0;
	if (width_y>VisibleHeight)
	{
		puc_y+=(width_y-VisibleHeight)>>1;
		puc_u+=(width_y-VisibleHeight)>>2;
		puc_v+=(width_y-VisibleHeight)>>2;
		width_y=VisibleHeight;
	}
	if (height_y>VisibleWidth)
	{
		puc_y+=(height_y-VisibleWidth)*(stride_y>>1);
		puc_u+=(height_y-VisibleWidth)*(stride_uv>>2);
		puc_v+=(height_y-VisibleWidth)*(stride_uv>>2);
		height_y=VisibleWidth;
	}
	pus_out = (unsigned short *) puc_out; 
	for (y=0; y<height_y; y++) 
	{ 
		pus_out = (unsigned short *) puc_out;
		pus_out+=y;
		for (x=width_y-1;x!=0;x--)
		{ 
			temp=puc_y[x]-144; 
			r = _S(temp+puc_u[x>>1]); 
			g = _S(temp+128); 
			b = _S(temp+puc_v[x>>1]); 
			pus_out[0] = (unsigned short) _Ps565(r,g,b); 
			pus_out+=height_y+Extra;
		} 
		puc_y += stride_y; 
		if (y<<31) 
		{ 
			puc_u += stride_uv; 
			puc_v += stride_uv; 
		} 
	} 
} 
*/
#ifndef ATI
void yuv2rgb_565RZ(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int nExtra, int Dither,int Brightness,uint8_t *puc_yp,uint8_t *puc_up,uint8_t *puc_vp, int slowopt, int invert) 
{ 

	register int x,y; 
	register signed int temp; 
	unsigned short *pus_out; 
	register unsigned int r,g,b; 
	//unsigned char* LimitValue=&LimitValues[256];
	if (nExtra<0) nExtra=0;
	if ((width_y<<1)>VisibleHeight) width_y=VisibleHeight>>1;
	if ((height_y<<1)>VisibleWidth) height_y=VisibleWidth>>1;

	if (Dither)
	{
		yuv2rgb_565RZD(puc_y, stride_y, puc_u, puc_v, stride_uv, puc_out, width_y, height_y,nExtra, Dither, Brightness, puc_yp,puc_up,puc_vp,slowopt,width_y-1,0,-1);
		return;
	}
	Brightness-=144;

	pus_out = (unsigned short *) puc_out; 
	for (y=0; y<height_y; y++) 
	{ 
		pus_out = (unsigned short *) puc_out;
		pus_out+=y<<1;
		for (x=width_y-1;x!=0;x--)
		{ 
			temp=puc_y[x]+Brightness; 
			r = _S(temp+puc_u[x>>1]); 
			g = _S(temp+128); 
			b = _S(temp+puc_v[x>>1]); 
			pus_out[0] = pus_out[1]= pus_out[(height_y+nExtra)] = pus_out[((height_y+nExtra))+1]=(unsigned short) _Ps565(r,g,b); 
			pus_out+=((height_y+nExtra)<<1);

		} 
		puc_y += stride_y; 
		if (y<<31) 
		{ 
			puc_u += stride_uv; 
			puc_v += stride_uv; 
		} 
	} 
} 
#endif
#endif

#else
#ifndef MIPS
void yuv2rgb_565R(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int Extra, int Dither )
{ 

	register int x,y; 
	register signed int temp; 
	unsigned short *pus_out; 
	register unsigned int r,g,b; 
	if (Extra<0) Extra=0;
	if (width_y>VisibleHeight)
	{
		puc_y+=(width_y-VisibleHeight)>>1;
		puc_u+=(width_y-VisibleHeight)>>2;
		puc_v+=(width_y-VisibleHeight)>>2;
		width_y=VisibleHeight;
	}
	if (height_y>VisibleWidth)
	{
		puc_y+=(height_y-VisibleWidth)*(stride_y>>1);
		puc_u+=(height_y-VisibleWidth)*(stride_uv>>2);
		puc_v+=(height_y-VisibleWidth)*(stride_uv>>2);
		height_y=VisibleWidth;
	}
	pus_out = (unsigned short *) puc_out; 
	for (y=0; y<height_y; y++) 
	{ 
		pus_out = (unsigned short *) puc_out;
		pus_out+=y;
		for (x=width_y-1;x!=0;x--)
		{ 
			int y,u,v;
			y = (puc_y[x] -16 )*0x2568;
			u = puc_u[x>>1] - 128;
			v = puc_v[x>>1] - 128;
			r = _S(_R(y,u,v));
			g= _S(_G(y,u,v));
			b=_S(_B(y,u,v));
//			temp=puc_y[x]-144; 
//			r = _S(temp+puc_u[x>>1]); 
//			g = _S(temp+128); 
//			b = _S(temp+puc_v[x>>1]); 
			pus_out[0] = (unsigned short) _Ps565(r,g,b); 
			pus_out+=height_y+Extra;
		} 
		puc_y += stride_y; 
		if (y<<31) 
		{ 
			puc_u += stride_uv; 
			puc_v += stride_uv; 
		} 
	} 
} 

#endif
void yuv2rgb_565RZ(uint8_t *puc_y, int stride_y, 
uint8_t *puc_u, uint8_t *puc_v, int stride_uv, 
uint8_t *puc_out, int width_y, int height_y, int nExtra, int Dither) 
{ 

	register int x,y; 
	register signed int temp; 
	unsigned short *pus_out; 
	register unsigned int r,g,b; 
	if (nExtra<0) nExtra=0;
	if ((width_y<<1)>VisibleHeight) width_y=VisibleHeight>>1;
	if ((height_y<<1)>VisibleWidth) height_y=VisibleWidth>>1;
	pus_out = (unsigned short *) puc_out; 
	for (y=0; y<height_y; y++) 
	{ 
		pus_out = (unsigned short *) puc_out;
		pus_out+=y<<1;
		for (x=width_y-1;x!=0;x--)
		{ 
			temp=puc_y[x]-144; 
			r = _S(temp+puc_u[x>>1]); 
			g = _S(temp+128); 
			b = _S(temp+puc_v[x>>1]); 
			pus_out[0] = pus_out[1]= pus_out[(height_y+nExtra)] = pus_out[((height_y+nExtra))+1]=(unsigned short) _Ps565(r,g,b); 
			pus_out+=((height_y+nExtra)<<1);

		} 
		puc_y += stride_y; 
		if (y<<31) 
		{ 
			puc_u += stride_uv; 
			puc_v += stride_uv; 
		} 
	} 
} 


#endif
#endif

