Skip to content
Snippets Groups Projects
postprocess.c 111 KiB
Newer Older
  • Learn to ignore specific revisions
  • 		"movq (%0, %1, 4), %%mm4			\n\t" //l4
    		"psubusb %%mm6, %%mm5				\n\t"
    		"psubusb %%mm4, %%mm6				\n\t"
    		"por %%mm6, %%mm5				\n\t" // |l3-l4|
    		"pcmpeqb %%mm7, %%mm6				\n\t" // SIGN(l3-l4)
    		"pxor %%mm6, %%mm0				\n\t"
    		"pand %%mm0, %%mm3				\n\t"
    		PMINUB(%%mm5, %%mm3, %%mm0)
    
    		"psubusb b01, %%mm3				\n\t"
    		PAVGB(%%mm7, %%mm3)
    
    		"movq (%%eax, %1, 2), %%mm0			\n\t"
    		"movq (%0, %1, 4), %%mm2			\n\t"
    		"pxor %%mm6, %%mm0				\n\t"
    		"pxor %%mm6, %%mm2				\n\t"
    		"psubb %%mm3, %%mm0				\n\t"
    		"paddb %%mm3, %%mm2				\n\t"
    		"pxor %%mm6, %%mm0				\n\t"
    		"pxor %%mm6, %%mm2				\n\t"
    		"movq %%mm0, (%%eax, %1, 2)			\n\t"
    		"movq %%mm2, (%0, %1, 4)			\n\t"
    #endif
    
    		"leal (%0, %1), %%eax				\n\t"
    		"pcmpeqb %%mm6, %%mm6				\n\t" // -1
    //	0	1	2	3	4	5	6	7
    //	%0	%0+%1	%0+2%1	eax+2%1	%0+4%1	eax+4%1	ebx+%1	ebx+2%1
    //	%0	eax	eax+%1	eax+2%1	%0+4%1	ebx	ebx+%1	ebx+2%1
    
    
    		"movq (%%eax, %1, 2), %%mm1			\n\t" // l3
    		"movq (%0, %1, 4), %%mm0			\n\t" // l4
    		"pxor %%mm6, %%mm1				\n\t" // -l3-1
    		PAVGB(%%mm1, %%mm0)				      // -q+128 = (l4-l3+256)/2
    // mm1=-l3-1, mm0=128-q
    
    		"movq (%%eax, %1, 4), %%mm2			\n\t" // l5
    		"movq (%%eax, %1), %%mm3			\n\t" // l2
    		"pxor %%mm6, %%mm2				\n\t" // -l5-1
    		"movq %%mm2, %%mm5				\n\t" // -l5-1
    		"movq b80, %%mm4				\n\t" // 128
    		"leal (%%eax, %1, 4), %%ebx			\n\t"
    		PAVGB(%%mm3, %%mm2)				      // (l2-l5+256)/2
    		PAVGB(%%mm0, %%mm4)				      // ~(l4-l3)/4 + 128
    		PAVGB(%%mm2, %%mm4)				      // ~(l2-l5)/4 +(l4-l3)/8 + 128
    		PAVGB(%%mm0, %%mm4)				      // ~(l2-l5)/8 +5(l4-l3)/16 + 128
    // mm1=-l3-1, mm0=128-q, mm3=l2, mm4=menergy/16 + 128, mm5= -l5-1
    
    		"movq (%%eax), %%mm2				\n\t" // l1
    		"pxor %%mm6, %%mm2				\n\t" // -l1-1
    		PAVGB(%%mm3, %%mm2)				      // (l2-l1+256)/2
    		PAVGB((%0), %%mm1)				      // (l0-l3+256)/2
    		"movq b80, %%mm3				\n\t" // 128
    		PAVGB(%%mm2, %%mm3)				      // ~(l2-l1)/4 + 128
    		PAVGB(%%mm1, %%mm3)				      // ~(l0-l3)/4 +(l2-l1)/8 + 128
    		PAVGB(%%mm2, %%mm3)				      // ~(l0-l3)/8 +5(l2-l1)/16 + 128
    // mm0=128-q, mm3=lenergy/16 + 128, mm4= menergy/16 + 128, mm5= -l5-1
    
    		PAVGB((%%ebx, %1), %%mm5)			      // (l6-l5+256)/2
    		"movq (%%ebx, %1, 2), %%mm1			\n\t" // l7
    		"pxor %%mm6, %%mm1				\n\t" // -l7-1
    		PAVGB((%0, %1, 4), %%mm1)			      // (l4-l7+256)/2
    		"movq b80, %%mm2				\n\t" // 128
    		PAVGB(%%mm5, %%mm2)				      // ~(l6-l5)/4 + 128
    		PAVGB(%%mm1, %%mm2)				      // ~(l4-l7)/4 +(l6-l5)/8 + 128
    		PAVGB(%%mm5, %%mm2)				      // ~(l4-l7)/8 +5(l6-l5)/16 + 128
    // mm0=128-q, mm2=renergy/16 + 128, mm3=lenergy/16 + 128, mm4= menergy/16 + 128
    
    		"movq b00, %%mm1				\n\t" // 0
    		"movq b00, %%mm5				\n\t" // 0
    		"psubb %%mm2, %%mm1				\n\t" // 128 - renergy/16
    		"psubb %%mm3, %%mm5				\n\t" // 128 - lenergy/16
    		PMAXUB(%%mm1, %%mm2)				      // 128 + |renergy/16|
     		PMAXUB(%%mm5, %%mm3)				      // 128 + |lenergy/16|
    		PMINUB(%%mm2, %%mm3, %%mm1)			      // 128 + MIN(|lenergy|,|renergy|)/16
    
    // mm0=128-q, mm3=128 + MIN(|lenergy|,|renergy|)/16, mm4= menergy/16 + 128
    
    		"movq b00, %%mm7				\n\t" // 0
    		"movq pQPb, %%mm2				\n\t" // QP
    		PAVGB(%%mm6, %%mm2)				      // 128 + QP/2
    		"psubb %%mm6, %%mm2				\n\t"
    
    		"movq %%mm4, %%mm1				\n\t"
    		"pcmpgtb %%mm7, %%mm1				\n\t" // SIGN(menergy)
    		"pxor %%mm1, %%mm4				\n\t"
    		"psubb %%mm1, %%mm4				\n\t" // 128 + |menergy|/16
    		"pcmpgtb %%mm4, %%mm2				\n\t" // |menergy|/16 < QP/2
    		"psubusb %%mm3, %%mm4				\n\t" //d=|menergy|/16 - MIN(|lenergy|,|renergy|)/16
    // mm0=128-q, mm1= SIGN(menergy), mm2= |menergy|/16 < QP/2, mm4= d/16
    
    		"movq %%mm4, %%mm3				\n\t" // d
    		"psubusb b01, %%mm4				\n\t"
    		PAVGB(%%mm7, %%mm4)				      // d/32
    		PAVGB(%%mm7, %%mm4)				      // (d + 32)/64
    		"paddb %%mm3, %%mm4				\n\t" // 5d/64
    		"pand %%mm2, %%mm4				\n\t"
    
    		"movq b80, %%mm5				\n\t" // 128
    		"psubb %%mm0, %%mm5				\n\t" // q
    		"paddsb %%mm6, %%mm5				\n\t" // fix bad rounding
    		"pcmpgtb %%mm5, %%mm7				\n\t" // SIGN(q)
    		"pxor %%mm7, %%mm5				\n\t"
    
    		PMINUB(%%mm5, %%mm4, %%mm3)			      // MIN(|q|, 5d/64)
    		"pxor %%mm1, %%mm7				\n\t" // SIGN(d*q)
    
    		"pand %%mm7, %%mm4				\n\t"
    		"movq (%%eax, %1, 2), %%mm0			\n\t"
    		"movq (%0, %1, 4), %%mm2			\n\t"
    		"pxor %%mm1, %%mm0				\n\t"
    		"pxor %%mm1, %%mm2				\n\t"
    		"paddb %%mm4, %%mm0				\n\t"
    		"psubb %%mm4, %%mm2				\n\t"
    		"pxor %%mm1, %%mm0				\n\t"
    		"pxor %%mm1, %%mm2				\n\t"
    		"movq %%mm0, (%%eax, %1, 2)			\n\t"
    		"movq %%mm2, (%0, %1, 4)			\n\t"
    
    		:
    		: "r" (src), "r" (stride)
    		: "%eax", "%ebx"
    	);
    
    /*
    	{
    	int x;
    	src-= stride;
    	for(x=0; x<BLOCK_SIZE; x++)
    	{
    		const int middleEnergy= 5*(src[l5] - src[l4]) + 2*(src[l3] - src[l6]);
    		if(ABS(middleEnergy)< 8*QP)
    		{
    			const int q=(src[l4] - src[l5])/2;
    			const int leftEnergy=  5*(src[l3] - src[l2]) + 2*(src[l1] - src[l4]);
    			const int rightEnergy= 5*(src[l7] - src[l6]) + 2*(src[l5] - src[l8]);
    
    			int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) );
    			d= MAX(d, 0);
    
    			d= (5*d + 32) >> 6;
    			d*= SIGN(-middleEnergy);
    
    			if(q>0)
    			{
    				d= d<0 ? 0 : d;
    				d= d>q ? q : d;
    			}
    			else
    			{
    				d= d>0 ? 0 : d;
    				d= d<q ? q : d;
    			}
    
            		src[l4]-= d;
    	        	src[l5]+= d;
    		}
    		src++;
    	}
    src-=8;
    	for(x=0; x<8; x++)
    	{
    		int y;
    		for(y=4; y<6; y++)
    		{
    			int d= src[x+y*stride] - tmp[x+(y-4)*8];
    			int ad= ABS(d);
    			static int max=0;
    			static int sum=0;
    			static int num=0;
    			static int bias=0;
    
    			if(max<ad) max=ad;
    			sum+= ad>3 ? 1 : 0;
    			if(ad>3)
    			{
    				src[0] = src[7] = src[stride*7] = src[(stride+1)*7]=255;
    			}
    			if(y==4) bias+=d;
    			num++;
    			if(num%1000000 == 0)
    			{
    				printf(" %d %d %d %d\n", num, sum, max, bias);
    			}
    		}
    	}
    }
    */
    #elif defined (HAVE_MMX)
    
    	src+= stride*4;
    
    	asm volatile(
    		"pxor %%mm7, %%mm7				\n\t"
    		"leal (%0, %1), %%eax				\n\t"
    		"leal (%%eax, %1, 4), %%ebx			\n\t"
    //	0	1	2	3	4	5	6	7
    //	%0	%0+%1	%0+2%1	eax+2%1	%0+4%1	eax+4%1	ebx+%1	ebx+2%1
    //	%0	eax	eax+%1	eax+2%1	%0+4%1	ebx	ebx+%1	ebx+2%1
    
    		"movq (%0), %%mm0				\n\t"
    		"movq %%mm0, %%mm1				\n\t"
    		"punpcklbw %%mm7, %%mm0				\n\t" // low part of line 0
    		"punpckhbw %%mm7, %%mm1				\n\t" // high part of line 0
    
    		"movq (%%eax), %%mm2				\n\t"
    		"movq %%mm2, %%mm3				\n\t"
    		"punpcklbw %%mm7, %%mm2				\n\t" // low part of line 1
    		"punpckhbw %%mm7, %%mm3				\n\t" // high part of line 1
    
    		"movq (%%eax, %1), %%mm4			\n\t"
    		"movq %%mm4, %%mm5				\n\t"
    		"punpcklbw %%mm7, %%mm4				\n\t" // low part of line 2
    		"punpckhbw %%mm7, %%mm5				\n\t" // high part of line 2
    
    		"paddw %%mm0, %%mm0				\n\t" // 2L0
    		"paddw %%mm1, %%mm1				\n\t" // 2H0
    		"psubw %%mm4, %%mm2				\n\t" // L1 - L2
    		"psubw %%mm5, %%mm3				\n\t" // H1 - H2
    		"psubw %%mm2, %%mm0				\n\t" // 2L0 - L1 + L2
    		"psubw %%mm3, %%mm1				\n\t" // 2H0 - H1 + H2
    
    		"psllw $2, %%mm2				\n\t" // 4L1 - 4L2
    		"psllw $2, %%mm3				\n\t" // 4H1 - 4H2
    		"psubw %%mm2, %%mm0				\n\t" // 2L0 - 5L1 + 5L2
    		"psubw %%mm3, %%mm1				\n\t" // 2H0 - 5H1 + 5H2
    
    		"movq (%%eax, %1, 2), %%mm2			\n\t"
    		"movq %%mm2, %%mm3				\n\t"
    		"punpcklbw %%mm7, %%mm2				\n\t" // L3
    		"punpckhbw %%mm7, %%mm3				\n\t" // H3
    
    		"psubw %%mm2, %%mm0				\n\t" // 2L0 - 5L1 + 5L2 - L3
    		"psubw %%mm3, %%mm1				\n\t" // 2H0 - 5H1 + 5H2 - H3
    		"psubw %%mm2, %%mm0				\n\t" // 2L0 - 5L1 + 5L2 - 2L3
    		"psubw %%mm3, %%mm1				\n\t" // 2H0 - 5H1 + 5H2 - 2H3
    		"movq %%mm0, temp0				\n\t" // 2L0 - 5L1 + 5L2 - 2L3
    		"movq %%mm1, temp1				\n\t" // 2H0 - 5H1 + 5H2 - 2H3
    
    		"movq (%0, %1, 4), %%mm0			\n\t"
    		"movq %%mm0, %%mm1				\n\t"
    		"punpcklbw %%mm7, %%mm0				\n\t" // L4
    		"punpckhbw %%mm7, %%mm1				\n\t" // H4
    
    		"psubw %%mm0, %%mm2				\n\t" // L3 - L4
    		"psubw %%mm1, %%mm3				\n\t" // H3 - H4
    		"movq %%mm2, temp2				\n\t" // L3 - L4
    		"movq %%mm3, temp3				\n\t" // H3 - H4
    		"paddw %%mm4, %%mm4				\n\t" // 2L2
    		"paddw %%mm5, %%mm5				\n\t" // 2H2
    		"psubw %%mm2, %%mm4				\n\t" // 2L2 - L3 + L4
    		"psubw %%mm3, %%mm5				\n\t" // 2H2 - H3 + H4
    
    		"psllw $2, %%mm2				\n\t" // 4L3 - 4L4
    		"psllw $2, %%mm3				\n\t" // 4H3 - 4H4
    		"psubw %%mm2, %%mm4				\n\t" // 2L2 - 5L3 + 5L4
    		"psubw %%mm3, %%mm5				\n\t" // 2H2 - 5H3 + 5H4
    //50 opcodes so far
    		"movq (%%ebx), %%mm2				\n\t"
    		"movq %%mm2, %%mm3				\n\t"
    		"punpcklbw %%mm7, %%mm2				\n\t" // L5
    		"punpckhbw %%mm7, %%mm3				\n\t" // H5
    		"psubw %%mm2, %%mm4				\n\t" // 2L2 - 5L3 + 5L4 - L5
    		"psubw %%mm3, %%mm5				\n\t" // 2H2 - 5H3 + 5H4 - H5
    		"psubw %%mm2, %%mm4				\n\t" // 2L2 - 5L3 + 5L4 - 2L5
    		"psubw %%mm3, %%mm5				\n\t" // 2H2 - 5H3 + 5H4 - 2H5
    
    		"movq (%%ebx, %1), %%mm6			\n\t"
    		"punpcklbw %%mm7, %%mm6				\n\t" // L6
    		"psubw %%mm6, %%mm2				\n\t" // L5 - L6
    		"movq (%%ebx, %1), %%mm6			\n\t"
    		"punpckhbw %%mm7, %%mm6				\n\t" // H6
    		"psubw %%mm6, %%mm3				\n\t" // H5 - H6
    
    		"paddw %%mm0, %%mm0				\n\t" // 2L4
    		"paddw %%mm1, %%mm1				\n\t" // 2H4
    		"psubw %%mm2, %%mm0				\n\t" // 2L4 - L5 + L6
    		"psubw %%mm3, %%mm1				\n\t" // 2H4 - H5 + H6
    
    		"psllw $2, %%mm2				\n\t" // 4L5 - 4L6
    		"psllw $2, %%mm3				\n\t" // 4H5 - 4H6
    		"psubw %%mm2, %%mm0				\n\t" // 2L4 - 5L5 + 5L6
    		"psubw %%mm3, %%mm1				\n\t" // 2H4 - 5H5 + 5H6
    
    		"movq (%%ebx, %1, 2), %%mm2			\n\t"
    		"movq %%mm2, %%mm3				\n\t"
    		"punpcklbw %%mm7, %%mm2				\n\t" // L7
    		"punpckhbw %%mm7, %%mm3				\n\t" // H7
    
    		"paddw %%mm2, %%mm2				\n\t" // 2L7
    		"paddw %%mm3, %%mm3				\n\t" // 2H7
    		"psubw %%mm2, %%mm0				\n\t" // 2L4 - 5L5 + 5L6 - 2L7
    		"psubw %%mm3, %%mm1				\n\t" // 2H4 - 5H5 + 5H6 - 2H7
    
    		"movq temp0, %%mm2				\n\t" // 2L0 - 5L1 + 5L2 - 2L3
    		"movq temp1, %%mm3				\n\t" // 2H0 - 5H1 + 5H2 - 2H3
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    
    #ifdef HAVE_MMX2
    		"movq %%mm7, %%mm6				\n\t" // 0
    		"psubw %%mm0, %%mm6				\n\t"
    		"pmaxsw %%mm6, %%mm0				\n\t" // |2L4 - 5L5 + 5L6 - 2L7|
    		"movq %%mm7, %%mm6				\n\t" // 0
    		"psubw %%mm1, %%mm6				\n\t"
    		"pmaxsw %%mm6, %%mm1				\n\t" // |2H4 - 5H5 + 5H6 - 2H7|
    		"movq %%mm7, %%mm6				\n\t" // 0
    		"psubw %%mm2, %%mm6				\n\t"
    		"pmaxsw %%mm6, %%mm2				\n\t" // |2L0 - 5L1 + 5L2 - 2L3|
    		"movq %%mm7, %%mm6				\n\t" // 0
    		"psubw %%mm3, %%mm6				\n\t"
    		"pmaxsw %%mm6, %%mm3				\n\t" // |2H0 - 5H1 + 5H2 - 2H3|
    #else
    
    		"movq %%mm7, %%mm6				\n\t" // 0
    		"pcmpgtw %%mm0, %%mm6				\n\t"
    		"pxor %%mm6, %%mm0				\n\t"
    		"psubw %%mm6, %%mm0				\n\t" // |2L4 - 5L5 + 5L6 - 2L7|
    		"movq %%mm7, %%mm6				\n\t" // 0
    		"pcmpgtw %%mm1, %%mm6				\n\t"
    		"pxor %%mm6, %%mm1				\n\t"
    		"psubw %%mm6, %%mm1				\n\t" // |2H4 - 5H5 + 5H6 - 2H7|
    		"movq %%mm7, %%mm6				\n\t" // 0
    		"pcmpgtw %%mm2, %%mm6				\n\t"
    		"pxor %%mm6, %%mm2				\n\t"
    		"psubw %%mm6, %%mm2				\n\t" // |2L0 - 5L1 + 5L2 - 2L3|
    		"movq %%mm7, %%mm6				\n\t" // 0
    		"pcmpgtw %%mm3, %%mm6				\n\t"
    		"pxor %%mm6, %%mm3				\n\t"
    		"psubw %%mm6, %%mm3				\n\t" // |2H0 - 5H1 + 5H2 - 2H3|
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    #endif
    
    
    #ifdef HAVE_MMX2
    		"pminsw %%mm2, %%mm0				\n\t"
    		"pminsw %%mm3, %%mm1				\n\t"
    #else
    		"movq %%mm0, %%mm6				\n\t"
    		"psubusw %%mm2, %%mm6				\n\t"
    		"psubw %%mm6, %%mm0				\n\t"
    		"movq %%mm1, %%mm6				\n\t"
    		"psubusw %%mm3, %%mm6				\n\t"
    		"psubw %%mm6, %%mm1				\n\t"
    #endif
    
    		"movq %%mm7, %%mm6				\n\t" // 0
    		"pcmpgtw %%mm4, %%mm6				\n\t" // sign(2L2 - 5L3 + 5L4 - 2L5)
    		"pxor %%mm6, %%mm4				\n\t"
    		"psubw %%mm6, %%mm4				\n\t" // |2L2 - 5L3 + 5L4 - 2L5|
    		"pcmpgtw %%mm5, %%mm7				\n\t" // sign(2H2 - 5H3 + 5H4 - 2H5)
    		"pxor %%mm7, %%mm5				\n\t"
    		"psubw %%mm7, %%mm5				\n\t" // |2H2 - 5H3 + 5H4 - 2H5|
    // 100 opcodes
    		"movd %2, %%mm2					\n\t" // QP
    		"punpcklwd %%mm2, %%mm2				\n\t"
    		"punpcklwd %%mm2, %%mm2				\n\t"
    		"psllw $3, %%mm2				\n\t" // 8QP
    		"movq %%mm2, %%mm3				\n\t" // 8QP
    		"pcmpgtw %%mm4, %%mm2				\n\t"
    		"pcmpgtw %%mm5, %%mm3				\n\t"
    		"pand %%mm2, %%mm4				\n\t"
    		"pand %%mm3, %%mm5				\n\t"
    
    
    		"psubusw %%mm0, %%mm4				\n\t" // hd
    		"psubusw %%mm1, %%mm5				\n\t" // ld
    
    
    		"movq w05, %%mm2				\n\t" // 5
    		"pmullw %%mm2, %%mm4				\n\t"
    		"pmullw %%mm2, %%mm5				\n\t"
    		"movq w20, %%mm2				\n\t" // 32
    		"paddw %%mm2, %%mm4				\n\t"
    		"paddw %%mm2, %%mm5				\n\t"
    		"psrlw $6, %%mm4				\n\t"
    		"psrlw $6, %%mm5				\n\t"
    
    /*
    		"movq w06, %%mm2				\n\t" // 6
    		"paddw %%mm2, %%mm4				\n\t"
    		"paddw %%mm2, %%mm5				\n\t"
    		"movq w1400, %%mm2				\n\t" // 1400h = 5120 = 5/64*2^16
    //FIXME if *5/64 is supposed to be /13 then we should use 5041 instead of 5120
    		"pmulhw %%mm2, %%mm4				\n\t" // hd/13
    		"pmulhw %%mm2, %%mm5				\n\t" // ld/13
    */
    
    		"movq temp2, %%mm0				\n\t" // L3 - L4
    		"movq temp3, %%mm1				\n\t" // H3 - H4
    
    		"pxor %%mm2, %%mm2				\n\t"
    		"pxor %%mm3, %%mm3				\n\t"
    
    		"pcmpgtw %%mm0, %%mm2				\n\t" // sign (L3-L4)
    		"pcmpgtw %%mm1, %%mm3				\n\t" // sign (H3-H4)
    		"pxor %%mm2, %%mm0				\n\t"
    		"pxor %%mm3, %%mm1				\n\t"
    		"psubw %%mm2, %%mm0				\n\t" // |L3-L4|
    		"psubw %%mm3, %%mm1				\n\t" // |H3-H4|
    
    		"psrlw $1, %%mm0				\n\t" // |L3 - L4|/2
    		"psrlw $1, %%mm1				\n\t" // |H3 - H4|/2
    
    
    		"pxor %%mm6, %%mm2				\n\t"
    		"pxor %%mm7, %%mm3				\n\t"
    		"pand %%mm2, %%mm4				\n\t"
    		"pand %%mm3, %%mm5				\n\t"
    
    #ifdef HAVE_MMX2
    		"pminsw %%mm0, %%mm4				\n\t"
    		"pminsw %%mm1, %%mm5				\n\t"
    #else
    		"movq %%mm4, %%mm2				\n\t"
    		"psubusw %%mm0, %%mm2				\n\t"
    		"psubw %%mm2, %%mm4				\n\t"
    		"movq %%mm5, %%mm2				\n\t"
    		"psubusw %%mm1, %%mm2				\n\t"
    		"psubw %%mm2, %%mm5				\n\t"
    #endif
    		"pxor %%mm6, %%mm4				\n\t"
    		"pxor %%mm7, %%mm5				\n\t"
    		"psubw %%mm6, %%mm4				\n\t"
    		"psubw %%mm7, %%mm5				\n\t"
    		"packsswb %%mm5, %%mm4				\n\t"
    		"movq (%%eax, %1, 2), %%mm0			\n\t"
    		"paddb   %%mm4, %%mm0				\n\t"
    		"movq %%mm0, (%%eax, %1, 2) 			\n\t"
    		"movq (%0, %1, 4), %%mm0			\n\t"
    		"psubb %%mm4, %%mm0				\n\t"
    		"movq %%mm0, (%0, %1, 4) 			\n\t"
    
    		:
    		: "r" (src), "r" (stride), "r" (QP)
    		: "%eax", "%ebx"
    	);
    #else
    	const int l1= stride;
    	const int l2= stride + l1;
    	const int l3= stride + l2;
    	const int l4= stride + l3;
    	const int l5= stride + l4;
    	const int l6= stride + l5;
    	const int l7= stride + l6;
    	const int l8= stride + l7;
    //	const int l9= stride + l8;
    
    	src+= stride*3;
    
    	{
    		const int middleEnergy= 5*(src[l5] - src[l4]) + 2*(src[l3] - src[l6]);
    		if(ABS(middleEnergy) < 8*QP)
    		{
    			const int q=(src[l4] - src[l5])/2;
    			const int leftEnergy=  5*(src[l3] - src[l2]) + 2*(src[l1] - src[l4]);
    			const int rightEnergy= 5*(src[l7] - src[l6]) + 2*(src[l5] - src[l8]);
    
    			int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) );
    			d= MAX(d, 0);
    
    			d= (5*d + 32) >> 6;
    			d*= SIGN(-middleEnergy);
    
    			if(q>0)
    			{
    				d= d<0 ? 0 : d;
    				d= d>q ? q : d;
    			}
    			else
    			{
    				d= d>0 ? 0 : d;
    				d= d<q ? q : d;
    			}
    
            		src[l4]-= d;
    	        	src[l5]+= d;
    		}
    		src++;
    	}
    #endif
    }
    
    
    /**
     * Check if the given 8x8 Block is mostly "flat"
     */
    static inline int isHorizDC(uint8_t src[], int stride)
    {
    	int numEq= 0;
    	int y;
    	for(y=0; y<BLOCK_SIZE; y++)
    	{
    		if(((src[0] - src[1] + 1) & 0xFFFF) < 3) numEq++;
    		if(((src[1] - src[2] + 1) & 0xFFFF) < 3) numEq++;
    		if(((src[2] - src[3] + 1) & 0xFFFF) < 3) numEq++;
    		if(((src[3] - src[4] + 1) & 0xFFFF) < 3) numEq++;
    		if(((src[4] - src[5] + 1) & 0xFFFF) < 3) numEq++;
    		if(((src[5] - src[6] + 1) & 0xFFFF) < 3) numEq++;
    		if(((src[6] - src[7] + 1) & 0xFFFF) < 3) numEq++;
    		src+= stride;
    	}
    	return numEq > hFlatnessThreshold;
    }
    
    static inline int isHorizMinMaxOk(uint8_t src[], int stride, int QP)
    {
    	if(abs(src[0] - src[7]) > 2*QP) return 0;
    
    	return 1;
    }
    
    static inline void doHorizDefFilter(uint8_t dst[], int stride, int QP)
    {
    	int y;
    	for(y=0; y<BLOCK_SIZE; y++)
    	{
    		const int middleEnergy= 5*(dst[4] - dst[5]) + 2*(dst[2] - dst[5]);
    
    		if(ABS(middleEnergy) < 8*QP)
    		{
    			const int q=(dst[3] - dst[4])/2;
    			const int leftEnergy=  5*(dst[2] - dst[1]) + 2*(dst[0] - dst[3]);
    			const int rightEnergy= 5*(dst[6] - dst[5]) + 2*(dst[4] - dst[7]);
    
    			int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) );
    			d= MAX(d, 0);
    
    			d= (5*d + 32) >> 6;
    			d*= SIGN(-middleEnergy);
    
    			if(q>0)
    			{
    				d= d<0 ? 0 : d;
    				d= d>q ? q : d;
    			}
    			else
    			{
    				d= d>0 ? 0 : d;
    				d= d<q ? q : d;
    			}
    
            		dst[3]-= d;
    	        	dst[4]+= d;
    		}
    		dst+= stride;
    	}
    }
    
    /**
     * Do a horizontal low pass filter on the 10x8 block (dst points to middle 8x8 Block)
     * using the 9-Tap Filter (1,1,2,2,4,2,2,1,1)/16 (C version)
     */
    static inline void doHorizLowPass(uint8_t dst[], int stride, int QP)
    {
    
    	int y;
    	for(y=0; y<BLOCK_SIZE; y++)
    	{
    		const int first= ABS(dst[-1] - dst[0]) < QP ? dst[-1] : dst[0];
    		const int last= ABS(dst[8] - dst[7]) < QP ? dst[8] : dst[7];
    
    		int sums[9];
    		sums[0] = first + dst[0];
    		sums[1] = dst[0] + dst[1];
    		sums[2] = dst[1] + dst[2];
    		sums[3] = dst[2] + dst[3];
    		sums[4] = dst[3] + dst[4];
    		sums[5] = dst[4] + dst[5];
    		sums[6] = dst[5] + dst[6];
    		sums[7] = dst[6] + dst[7];
    		sums[8] = dst[7] + last;
    
    		dst[0]= ((sums[0]<<2) + ((first + sums[2])<<1) + sums[4] + 8)>>4;
    		dst[1]= ((dst[1]<<2) + ((first + sums[0] + sums[3])<<1) + sums[5] + 8)>>4;
    		dst[2]= ((dst[2]<<2) + ((first + sums[1] + sums[4])<<1) + sums[6] + 8)>>4;
    		dst[3]= ((dst[3]<<2) + ((sums[2] + sums[5])<<1) + sums[0] + sums[7] + 8)>>4;
    		dst[4]= ((dst[4]<<2) + ((sums[3] + sums[6])<<1) + sums[1] + sums[8] + 8)>>4;
    		dst[5]= ((dst[5]<<2) + ((last + sums[7] + sums[4])<<1) + sums[2] + 8)>>4;
    		dst[6]= (((last + dst[6])<<2) + ((dst[7] + sums[5])<<1) + sums[3] + 8)>>4;
    		dst[7]= ((sums[8]<<2) + ((last + sums[6])<<1) + sums[4] + 8)>>4;
    
    		dst+= stride;
    	}
    }
    
    
    
    static inline void dering(uint8_t src[], int stride, int QP)
    {
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    #if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"movq pQPb, %%mm0				\n\t"
    		"paddusb %%mm0, %%mm0				\n\t"
    		"movq %%mm0, pQPb2				\n\t"
    
    
    		"leal (%0, %1), %%eax				\n\t"
    		"leal (%%eax, %1, 4), %%ebx			\n\t"
    //	0	1	2	3	4	5	6	7	8	9
    //	%0	eax	eax+%1	eax+2%1	%0+4%1	ebx	ebx+%1	ebx+2%1	%0+8%1	ebx+4%1
    
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"pcmpeqb %%mm7, %%mm7				\n\t"
    		"pxor %%mm6, %%mm6				\n\t"
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    #ifdef HAVE_MMX2
    
    #define FIND_MIN_MAX(addr)\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"movq " #addr ", %%mm0				\n\t"\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"pminub %%mm0, %%mm7				\n\t"\
    		"pmaxub %%mm0, %%mm6				\n\t"
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    #else
    #define FIND_MIN_MAX(addr)\
    		"movq " #addr ", %%mm0				\n\t"\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"movq %%mm7, %%mm1				\n\t"\
    		"psubusb %%mm0, %%mm6				\n\t"\
    		"paddb %%mm0, %%mm6				\n\t"\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"psubusb %%mm0, %%mm1				\n\t"\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"psubb %%mm1, %%mm7				\n\t"
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    #endif
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    FIND_MIN_MAX((%%eax))
    FIND_MIN_MAX((%%eax, %1))
    FIND_MIN_MAX((%%eax, %1, 2))
    FIND_MIN_MAX((%0, %1, 4))
    FIND_MIN_MAX((%%ebx))
    FIND_MIN_MAX((%%ebx, %1))
    FIND_MIN_MAX((%%ebx, %1, 2))
    FIND_MIN_MAX((%0, %1, 8))
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"pminub %%mm4, %%mm7				\n\t" // min of pixels
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"pminub %%mm4, %%mm7				\n\t" // min of pixels
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"pminub %%mm4, %%mm7				\n\t"
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"movq %%mm7, %%mm1				\n\t"
    		"psubusb %%mm4, %%mm1				\n\t"
    		"psubb %%mm1, %%mm7				\n\t"
    
    		"movq %%mm7, %%mm4				\n\t"
    		"psrlq $16, %%mm7				\n\t"
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"movq %%mm7, %%mm1				\n\t"
    		"psubusb %%mm4, %%mm1				\n\t"
    		"psubb %%mm1, %%mm7				\n\t"
    
    		"movq %%mm7, %%mm4				\n\t"
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"movq %%mm7, %%mm1				\n\t"
    		"psubusb %%mm4, %%mm1				\n\t"
    		"psubb %%mm1, %%mm7				\n\t"
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    
    
    		"movq %%mm6, %%mm4				\n\t"
    		"psrlq $8, %%mm6				\n\t"
    #ifdef HAVE_MMX2
    		"pmaxub %%mm4, %%mm6				\n\t" // max of pixels
    		"pshufw $0xF9, %%mm6, %%mm4			\n\t"
    		"pmaxub %%mm4, %%mm6				\n\t"
    		"pshufw $0xFE, %%mm6, %%mm4			\n\t"
    		"pmaxub %%mm4, %%mm6				\n\t"
    #else
    		"psubusb %%mm4, %%mm6				\n\t"
    		"paddb %%mm4, %%mm6				\n\t"
    		"movq %%mm6, %%mm4				\n\t"
    		"psrlq $16, %%mm6				\n\t"
    		"psubusb %%mm4, %%mm6				\n\t"
    		"paddb %%mm4, %%mm6				\n\t"
    		"movq %%mm6, %%mm4				\n\t"
    		"psrlq $32, %%mm6				\n\t"
    		"psubusb %%mm4, %%mm6				\n\t"
    		"paddb %%mm4, %%mm6				\n\t"
    #endif
    		"movq %%mm6, %%mm0				\n\t" // max
    		"psubb %%mm7, %%mm6				\n\t" // max - min
    		"movd %%mm6, %%ecx				\n\t"
    		"cmpb deringThreshold, %%cl			\n\t"
    		" jb 1f						\n\t"
    		PAVGB(%%mm0, %%mm7)				      // a=(max + min)/2
    
    		"punpcklbw %%mm7, %%mm7				\n\t"
    		"punpcklbw %%mm7, %%mm7				\n\t"
    		"punpcklbw %%mm7, %%mm7				\n\t"
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"movq %%mm7, temp0				\n\t"
    
    		"movq (%0), %%mm0				\n\t" // L10
    		"movq %%mm0, %%mm1				\n\t" // L10
    		"movq %%mm0, %%mm2				\n\t" // L10
    		"psllq $8, %%mm1				\n\t"
    		"psrlq $8, %%mm2				\n\t"
    		"movd -4(%0), %%mm3				\n\t"
    		"movd 8(%0), %%mm4				\n\t"
    		"psrlq $24, %%mm3				\n\t"
    		"psllq $56, %%mm4				\n\t"
    		"por %%mm3, %%mm1				\n\t" // L00
    		"por %%mm4, %%mm2				\n\t" // L20
    		"movq %%mm1, %%mm3				\n\t" // L00
    		PAVGB(%%mm2, %%mm1)				      // (L20 + L00)/2
    		PAVGB(%%mm0, %%mm1)				      // (L20 + L00 + 2L10)/4
    		"psubusb %%mm7, %%mm0				\n\t"
    		"psubusb %%mm7, %%mm2				\n\t"
    		"psubusb %%mm7, %%mm3				\n\t"
    		"pcmpeqb b00, %%mm0				\n\t" // L10 > a ? 0 : -1
    		"pcmpeqb b00, %%mm2				\n\t" // L20 > a ? 0 : -1
    		"pcmpeqb b00, %%mm3				\n\t" // L00 > a ? 0 : -1
    		"paddb %%mm2, %%mm0				\n\t"
    		"paddb %%mm3, %%mm0				\n\t"
    
    		"movq (%%eax), %%mm2				\n\t" // L11
    		"movq %%mm2, %%mm3				\n\t" // L11
    		"movq %%mm2, %%mm4				\n\t" // L11
    		"psllq $8, %%mm3				\n\t"
    		"psrlq $8, %%mm4				\n\t"
    		"movd -4(%%eax), %%mm5				\n\t"
    		"movd 8(%%eax), %%mm6				\n\t"
    		"psrlq $24, %%mm5				\n\t"
    		"psllq $56, %%mm6				\n\t"
    		"por %%mm5, %%mm3				\n\t" // L01
    		"por %%mm6, %%mm4				\n\t" // L21
    		"movq %%mm3, %%mm5				\n\t" // L01
    		PAVGB(%%mm4, %%mm3)				      // (L21 + L01)/2
    		PAVGB(%%mm2, %%mm3)				      // (L21 + L01 + 2L11)/4
    		"psubusb %%mm7, %%mm2				\n\t"
    		"psubusb %%mm7, %%mm4				\n\t"
    		"psubusb %%mm7, %%mm5				\n\t"
    		"pcmpeqb b00, %%mm2				\n\t" // L11 > a ? 0 : -1
    		"pcmpeqb b00, %%mm4				\n\t" // L21 > a ? 0 : -1
    		"pcmpeqb b00, %%mm5				\n\t" // L01 > a ? 0 : -1
    		"paddb %%mm4, %%mm2				\n\t"
    		"paddb %%mm5, %%mm2				\n\t"
    // 0, 2, 3, 1
    #define DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1) \
    		"movq " #src ", " #sx "				\n\t" /* src[0] */\
    		"movq " #sx ", " #lx "				\n\t" /* src[0] */\
    		"movq " #sx ", " #t0 "				\n\t" /* src[0] */\
    		"psllq $8, " #lx "				\n\t"\
    		"psrlq $8, " #t0 "				\n\t"\
    		"movd -4" #src ", " #t1 "			\n\t"\
    		"psrlq $24, " #t1 "				\n\t"\
    		"por " #t1 ", " #lx "				\n\t" /* src[-1] */\
    		"movd 8" #src ", " #t1 "			\n\t"\
    		"psllq $56, " #t1 "				\n\t"\
    		"por " #t1 ", " #t0 "				\n\t" /* src[+1] */\
    		"movq " #lx ", " #t1 "				\n\t" /* src[-1] */\
    		PAVGB(t0, lx)				              /* (src[-1] + src[+1])/2 */\
    		PAVGB(sx, lx)				      /* (src[-1] + 2src[0] + src[+1])/4 */\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		PAVGB(lx, pplx)					     \
    		"movq " #lx ", temp1				\n\t"\
    		"movq temp0, " #lx "				\n\t"\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"psubusb " #lx ", " #t1 "			\n\t"\
    		"psubusb " #lx ", " #t0 "			\n\t"\
    		"psubusb " #lx ", " #sx "			\n\t"\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"movq b00, " #lx "				\n\t"\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"pcmpeqb " #lx ", " #t1 "			\n\t" /* src[-1] > a ? 0 : -1*/\
    		"pcmpeqb " #lx ", " #t0 "			\n\t" /* src[+1] > a ? 0 : -1*/\
    		"pcmpeqb " #lx ", " #sx "			\n\t" /* src[0]  > a ? 0 : -1*/\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"paddb " #t1 ", " #t0 "				\n\t"\
    		"paddb " #t0 ", " #sx "				\n\t"\
    \
    		PAVGB(plx, pplx)				      /* filtered */\
    		"movq " #dst ", " #t0 "				\n\t" /* dst */\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"movq " #t0 ", " #t1 "				\n\t" /* dst */\
    		"psubusb pQPb2, " #t0 "				\n\t"\
    		"paddusb pQPb2, " #t1 "				\n\t"\
    		PMAXUB(t0, pplx)\
    		PMINUB(t1, pplx, t0)\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"paddb " #sx ", " #ppsx "			\n\t"\
    		"paddb " #psx ", " #ppsx "			\n\t"\
    	"#paddb b02, " #ppsx "				\n\t"\
    		"pand b08, " #ppsx "				\n\t"\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"pcmpeqb " #lx ", " #ppsx "			\n\t"\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"pand " #ppsx ", " #pplx "			\n\t"\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"pandn " #dst ", " #ppsx "			\n\t"\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"por " #pplx ", " #ppsx "			\n\t"\
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"movq " #ppsx ", " #dst "			\n\t"\
    		"movq temp1, " #lx "				\n\t"
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    /*
    0000000
    1111111
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    1111110
    1111101
    1111100
    1111011
    1111010
    1111001
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    1111000
    1110111
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    */
    //DERING_CORE(dst,src                  ,ppsx ,psx  ,sx   ,pplx ,plx  ,lx   ,t0   ,t1)
    DERING_CORE((%%eax),(%%eax, %1)        ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
    DERING_CORE((%%eax, %1),(%%eax, %1, 2) ,%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
    DERING_CORE((%%eax, %1, 2),(%0, %1, 4) ,%%mm4,%%mm0,%%mm2,%%mm5,%%mm1,%%mm3,%%mm6,%%mm7)
    DERING_CORE((%0, %1, 4),(%%ebx)        ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
    DERING_CORE((%%ebx),(%%ebx, %1)        ,%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
    DERING_CORE((%%ebx, %1), (%%ebx, %1, 2),%%mm4,%%mm0,%%mm2,%%mm5,%%mm1,%%mm3,%%mm6,%%mm7)
    DERING_CORE((%%ebx, %1, 2),(%0, %1, 8) ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
    DERING_CORE((%0, %1, 8),(%%ebx, %1, 4) ,%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		"1:			\n\t"
    
    		: : "r" (src), "r" (stride), "r" (QP)
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    		: "%eax", "%ebx", "%ecx"
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    	int y;
    	int min=255;
    	int max=0;
    	int avg;
    	uint8_t *p;
    	int s[10];
    
    	for(y=1; y<9; y++)
    	{
    		int x;
    		p= src + stride*y;
    		for(x=1; x<9; x++)
    		{
    			p++;
    			if(*p > max) max= *p;
    			if(*p < min) min= *p;
    		}
    	}
    	avg= (min + max + 1)/2;
    
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    	if(max - min <deringThreshold) return;
    
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    	for(y=0; y<10; y++)
    	{
    		int x;
    		int t = 0;
    		p= src + stride*y;
    		for(x=0; x<10; x++)
    		{
    			if(*p > avg) t |= (1<<x);
    			p++;
    		}
    		t |= (~t)<<16;
    		t &= (t<<1) & (t>>1);
    		s[y] = t;
    	}
    
    	for(y=1; y<9; y++)
    	{
    		int x;
    		int t = s[y-1] & s[y] & s[y+1];
    		t|= t>>16;
    
    		p= src + stride*y;
    		for(x=1; x<9; x++)
    		{
    			p++;
    			if(t & (1<<x))
    			{
    				int f= (*(p-stride-1)) + 2*(*(p-stride)) + (*(p-stride+1))
    				      +2*(*(p     -1)) + 4*(*p         ) + 2*(*(p     +1))
    				      +(*(p+stride-1)) + 2*(*(p+stride)) + (*(p+stride+1));
    				f= (f + 8)>>4;
    
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    #ifdef DEBUG_DERING_THRESHOLD
    				asm volatile("emms\n\t":);
    				{
    				static long long numPixels=0;
    				if(x!=1 && x!=8 && y!=1 && y!=8) numPixels++;
    //				if((max-min)<20 || (max-min)*QP<200)
    //				if((max-min)*QP < 500)
    //				if(max-min<QP/2)
    				if(max-min < 20)
    				{
    					static int numSkiped=0;
    					static int errorSum=0;
    					static int worstQP=0;
    					static int worstRange=0;
    					static int worstDiff=0;
    					int diff= (f - *p);
    					int absDiff= ABS(diff);
    					int error= diff*diff;
    
    					if(x==1 || x==8 || y==1 || y==8) continue;
    
    					numSkiped++;
    					if(absDiff > worstDiff)
    					{
    						worstDiff= absDiff;
    						worstQP= QP;
    						worstRange= max-min;
    					}
    					errorSum+= error;
    
    					if(1024LL*1024LL*1024LL % numSkiped == 0)
    					{
    						printf( "sum:%1.3f, skip:%d, wQP:%d, "
    							"wRange:%d, wDiff:%d, relSkip:%1.3f\n",
    							(float)errorSum/numSkiped, numSkiped, worstQP, worstRange,
    							worstDiff, (float)numSkiped/numPixels);
    					}
    				}
    				}
    #endif
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    				if     (*p + 2*QP < f) *p= *p + 2*QP;
    				else if(*p - 2*QP > f) *p= *p - 2*QP;
    				else *p=f;
    			}
    		}
    	}
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    #ifdef DEBUG_DERING_THRESHOLD
    	if(max-min < 20)
    	{
    		for(y=1; y<9; y++)
    		{
    			int x;
    			int t = 0;
    			p= src + stride*y;
    			for(x=1; x<9; x++)
    			{
    				p++;
    				*p = MIN(*p + 20, 255);
    			}
    		}
    //		src[0] = src[7]=src[stride*7]=src[stride*7 + 7]=255;
    	}
    #endif
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
     * will be called for every 8x8 block and can read & write from line 4-15
     * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
     * lines 4-12 will be read into the deblocking filter and should be deinterlaced
    
     */
    static inline void deInterlaceInterpolateLinear(uint8_t src[], int stride)
    {
    #if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    	src+= 4*stride;
    
    	asm volatile(
    		"leal (%0, %1), %%eax				\n\t"
    		"leal (%%eax, %1, 4), %%ebx			\n\t"
    //	0	1	2	3	4	5	6	7	8	9
    //	%0	eax	eax+%1	eax+2%1	%0+4%1	ebx	ebx+%1	ebx+2%1	%0+8%1	ebx+4%1
    
    		"movq (%0), %%mm0				\n\t"
    		"movq (%%eax, %1), %%mm1			\n\t"
    
    		PAVGB(%%mm1, %%mm0)
    
    		"movq %%mm0, (%%eax)				\n\t"
    		"movq (%0, %1, 4), %%mm0			\n\t"
    
    		PAVGB(%%mm0, %%mm1)
    
    		"movq %%mm1, (%%eax, %1, 2)			\n\t"
    		"movq (%%ebx, %1), %%mm1			\n\t"
    
    		PAVGB(%%mm1, %%mm0)
    
    		"movq %%mm0, (%%ebx)				\n\t"
    		"movq (%0, %1, 8), %%mm0			\n\t"
    
    		PAVGB(%%mm0, %%mm1)
    
    		"movq %%mm1, (%%ebx, %1, 2)			\n\t"
    
    		: : "r" (src), "r" (stride)
    		: "%eax", "%ebx"
    	);
    #else
    	int x;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    	src+= 4*stride;
    
    	for(x=0; x<8; x++)
    	{
    		src[stride]   = (src[0]        + src[stride*2])>>1;
    		src[stride*3] = (src[stride*2] + src[stride*4])>>1;
    		src[stride*5] = (src[stride*4] + src[stride*6])>>1;
    		src[stride*7] = (src[stride*6] + src[stride*8])>>1;
    		src++;
    	}
    #endif
    }
    
    /**
     * Deinterlaces the given block
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
     * will be called for every 8x8 block and can read & write from line 4-15
     * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
     * lines 4-12 will be read into the deblocking filter and should be deinterlaced
     * this filter will read lines 3-15 and write 7-13
    
     * no cliping in C version
    
    static inline void deInterlaceInterpolateCubic(uint8_t src[], int stride)
    
    {
    #if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    	src+= stride*3;
    
    	asm volatile(
    		"leal (%0, %1), %%eax				\n\t"
    		"leal (%%eax, %1, 4), %%ebx			\n\t"
    
    		"leal (%%ebx, %1, 4), %%ecx			\n\t"
    		"addl %1, %%ecx					\n\t"
    		"pxor %%mm7, %%mm7				\n\t"
    //	0	1	2	3	4	5	6	7	8	9	10
    //	%0	eax	eax+%1	eax+2%1	%0+4%1	ebx	ebx+%1	ebx+2%1	%0+8%1	ebx+4%1 ecx
    
    #define DEINT_CUBIC(a,b,c,d,e)\
    		"movq " #a ", %%mm0				\n\t"\
    		"movq " #b ", %%mm1				\n\t"\
    		"movq " #d ", %%mm2				\n\t"\
    		"movq " #e ", %%mm3				\n\t"\
    		PAVGB(%%mm2, %%mm1)					/* (b+d) /2 */\
    		PAVGB(%%mm3, %%mm0)					/* a(a+e) /2 */\
    		"movq %%mm0, %%mm2				\n\t"\
    		"punpcklbw %%mm7, %%mm0				\n\t"\
    		"punpckhbw %%mm7, %%mm2				\n\t"\
    		"movq %%mm1, %%mm3				\n\t"\
    		"punpcklbw %%mm7, %%mm1				\n\t"\
    		"punpckhbw %%mm7, %%mm3				\n\t"\
    		"psubw %%mm1, %%mm0				\n\t"	/* L(a+e - (b+d))/2 */\
    		"psubw %%mm3, %%mm2				\n\t"	/* H(a+e - (b+d))/2 */\
    		"psraw $3, %%mm0				\n\t"	/* L(a+e - (b+d))/16 */\