postprocess_template.c 92.1 KB
Newer Older
1
/*
Michael Niedermayer's avatar
Michael Niedermayer committed
2
    Copyright (C) 2001-2002 Michael Niedermayer (michaelni@gmx.at)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

    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.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

19 20 21
#undef PAVGB
#undef PMINUB
#undef PMAXUB
Arpi's avatar
Arpi committed
22 23 24 25 26 27

#ifdef HAVE_MMX2
#define PAVGB(a,b) "pavgb " #a ", " #b " \n\t"
#elif defined (HAVE_3DNOW)
#define PAVGB(a,b) "pavgusb " #a ", " #b " \n\t"
#endif
28

Michael Niedermayer's avatar
Michael Niedermayer committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#ifdef HAVE_MMX2
#define PMINUB(a,b,t) "pminub " #a ", " #b " \n\t"
#elif defined (HAVE_MMX)
#define PMINUB(b,a,t) \
	"movq " #a ", " #t " \n\t"\
	"psubusb " #b ", " #t " \n\t"\
	"psubb " #t ", " #a " \n\t"
#endif

#ifdef HAVE_MMX2
#define PMAXUB(a,b) "pmaxub " #a ", " #b " \n\t"
#elif defined (HAVE_MMX)
#define PMAXUB(a,b) \
	"psubusb " #a ", " #b " \n\t"\
	"paddb " #a ", " #b " \n\t"
#endif


47 48
//FIXME? |255-0| = 1 (shouldnt be a problem ...)
/**
49
 * Check if the middle 8x8 Block in the given 8x16 block is flat
50
 */
51
static inline int RENAME(isVertDC)(uint8_t src[], int stride){
52
	int numEq= 0;
53
#ifndef HAVE_MMX
54
	int y;
55
#endif
56
	src+= stride*4; // src points to begin of the 8x8 Block
57
#ifdef HAVE_MMX
58 59 60 61 62
asm volatile(
		"leal (%1, %2), %%eax				\n\t"
		"leal (%%eax, %2, 4), %%ebx			\n\t"
//	0	1	2	3	4	5	6	7	8	9
//	%1	eax	eax+%2	eax+2%2	%1+4%2	ebx	ebx+%2	ebx+2%2	%1+8%2	ebx+4%2
63 64
		"movq "MANGLE(mmxDCOffset)", %%mm7		\n\t" // mm7 = 0x7F
		"movq "MANGLE(mmxDCThreshold)", %%mm6		\n\t" // mm6 = 0x7D
65
		"movq (%1), %%mm0				\n\t"
66
		"movq (%%eax), %%mm1				\n\t"
67 68 69 70
		"psubb %%mm1, %%mm0				\n\t" // mm0 = differnece
		"paddb %%mm7, %%mm0				\n\t"
		"pcmpgtb %%mm6, %%mm0				\n\t"

71
		"movq (%%eax,%2), %%mm2				\n\t"
72 73 74 75 76
		"psubb %%mm2, %%mm1				\n\t"
		"paddb %%mm7, %%mm1				\n\t"
		"pcmpgtb %%mm6, %%mm1				\n\t"
		"paddb %%mm1, %%mm0				\n\t"

77
		"movq (%%eax, %2, 2), %%mm1			\n\t"
78 79 80 81 82
		"psubb %%mm1, %%mm2				\n\t"
		"paddb %%mm7, %%mm2				\n\t"
		"pcmpgtb %%mm6, %%mm2				\n\t"
		"paddb %%mm2, %%mm0				\n\t"

83
		"movq (%1, %2, 4), %%mm2			\n\t"
84 85 86 87 88
		"psubb %%mm2, %%mm1				\n\t"
		"paddb %%mm7, %%mm1				\n\t"
		"pcmpgtb %%mm6, %%mm1				\n\t"
		"paddb %%mm1, %%mm0				\n\t"

89
		"movq (%%ebx), %%mm1				\n\t"
90 91 92 93 94
		"psubb %%mm1, %%mm2				\n\t"
		"paddb %%mm7, %%mm2				\n\t"
		"pcmpgtb %%mm6, %%mm2				\n\t"
		"paddb %%mm2, %%mm0				\n\t"

95
		"movq (%%ebx, %2), %%mm2			\n\t"
96 97 98 99 100
		"psubb %%mm2, %%mm1				\n\t"
		"paddb %%mm7, %%mm1				\n\t"
		"pcmpgtb %%mm6, %%mm1				\n\t"
		"paddb %%mm1, %%mm0				\n\t"

101
		"movq (%%ebx, %2, 2), %%mm1			\n\t"
102 103 104 105 106 107
		"psubb %%mm1, %%mm2				\n\t"
		"paddb %%mm7, %%mm2				\n\t"
		"pcmpgtb %%mm6, %%mm2				\n\t"
		"paddb %%mm2, %%mm0				\n\t"

		"						\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
108 109 110 111
#ifdef HAVE_MMX2
		"pxor %%mm7, %%mm7				\n\t"
		"psadbw %%mm7, %%mm0				\n\t"
#else
112 113 114 115 116 117 118 119 120
		"movq %%mm0, %%mm1				\n\t"
		"psrlw $8, %%mm0				\n\t"
		"paddb %%mm1, %%mm0				\n\t"
		"movq %%mm0, %%mm1				\n\t"
		"psrlq $16, %%mm0				\n\t"
		"paddb %%mm1, %%mm0				\n\t"
		"movq %%mm0, %%mm1				\n\t"
		"psrlq $32, %%mm0				\n\t"
		"paddb %%mm1, %%mm0				\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
121
#endif
122 123 124
		"movd %%mm0, %0					\n\t"
		: "=r" (numEq)
		: "r" (src), "r" (stride)
Michael Niedermayer's avatar
Michael Niedermayer committed
125
		: "%ebx"
126
		);
Michael Niedermayer's avatar
Michael Niedermayer committed
127
	numEq= (-numEq) &0xFF;
128 129

#else
130
	for(y=0; y<BLOCK_SIZE-1; y++)
131
	{
132 133 134 135 136 137 138 139
		if(((src[0] - src[0+stride] + dcOffset)&0xFFFF) < dcThreshold) numEq++;
		if(((src[1] - src[1+stride] + dcOffset)&0xFFFF) < dcThreshold) numEq++;
		if(((src[2] - src[2+stride] + dcOffset)&0xFFFF) < dcThreshold) numEq++;
		if(((src[3] - src[3+stride] + dcOffset)&0xFFFF) < dcThreshold) numEq++;
		if(((src[4] - src[4+stride] + dcOffset)&0xFFFF) < dcThreshold) numEq++;
		if(((src[5] - src[5+stride] + dcOffset)&0xFFFF) < dcThreshold) numEq++;
		if(((src[6] - src[6+stride] + dcOffset)&0xFFFF) < dcThreshold) numEq++;
		if(((src[7] - src[7+stride] + dcOffset)&0xFFFF) < dcThreshold) numEq++;
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
		src+= stride;
	}
#endif
/*	if(abs(numEq - asmEq) > 0)
	{
		printf("\nasm:%d  c:%d\n", asmEq, numEq);
		for(int y=0; y<8; y++)
		{
			for(int x=0; x<8; x++)
			{
				printf("%d ", temp[x + y*stride]);
			}
			printf("\n");
		}
	}
*/
156 157
//	for(int i=0; i<numEq/8; i++) src[i]=255;
	return (numEq > vFlatnessThreshold) ? 1 : 0;
158 159
}

160
static inline int RENAME(isVertMinMaxOk)(uint8_t src[], int stride, int QP)
161 162 163
{
#ifdef HAVE_MMX
	int isOk;
164
	src+= stride*3;
165 166 167 168 169 170 171 172 173
	asm volatile(
//		"int $3 \n\t"
		"movq (%1, %2), %%mm0				\n\t"
		"movq (%1, %2, 8), %%mm1			\n\t"
		"movq %%mm0, %%mm2				\n\t"
		"psubusb %%mm1, %%mm0				\n\t"
		"psubusb %%mm2, %%mm1				\n\t"
		"por %%mm1, %%mm0				\n\t" // ABS Diff

174
		"movq "MANGLE(pQPb)", %%mm7			\n\t" // QP,..., QP
175 176
		"paddusb %%mm7, %%mm7				\n\t" // 2QP ... 2QP
		"psubusb %%mm7, %%mm0				\n\t" // Diff <= 2QP -> 0
177
		"pcmpeqd "MANGLE(b00)", %%mm0			\n\t"
178
		"psrlq $16, %%mm0				\n\t"
179
		"pcmpeqd "MANGLE(bFF)", %%mm0			\n\t"
180 181 182 183 184
//		"movd %%mm0, (%1, %2, 4)\n\t"
		"movd %%mm0, %0					\n\t"
		: "=r" (isOk)
		: "r" (src), "r" (stride)
		);
185
	return isOk;
186 187
#else

188 189
	int isOk2= 1;
	int x;
190
	src+= stride*3;
191
	for(x=0; x<BLOCK_SIZE; x++)
192
	{
193
		if(abs((int)src[x + stride] - (int)src[x + (stride<<3)]) > 2*QP) isOk2=0;
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
	}
/*	if(isOk && !isOk2 || !isOk && isOk2)
	{
		printf("\nasm:%d  c:%d QP:%d\n", isOk, isOk2, QP);
		for(int y=0; y<9; y++)
		{
			for(int x=0; x<8; x++)
			{
				printf("%d ", src[x + y*stride]);
			}
			printf("\n");
		}
	} */

	return isOk2;
#endif

}

/**
214
 * Do a vertical low pass filter on the 8x16 block (only write to the 8x8 block in the middle)
Michael Niedermayer's avatar
Michael Niedermayer committed
215
 * using the 9-Tap Filter (1,1,2,2,4,2,2,1,1)/16
216
 */
217
static inline void RENAME(doVertLowPass)(uint8_t *src, int stride, int QP)
218
{
219
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
220
	src+= stride*3;
221
	asm volatile(	//"movv %0 %1 %2\n\t"
222
		"movq "MANGLE(pQPb)", %%mm0			\n\t"  // QP,..., QP
223 224 225 226 227 228 229 230 231

		"movq (%0), %%mm6				\n\t"
		"movq (%0, %1), %%mm5				\n\t"
		"movq %%mm5, %%mm1				\n\t"
		"movq %%mm6, %%mm2				\n\t"
		"psubusb %%mm6, %%mm5				\n\t"
		"psubusb %%mm1, %%mm2				\n\t"
		"por %%mm5, %%mm2				\n\t" // ABS Diff of lines
		"psubusb %%mm0, %%mm2				\n\t" // diff <= QP -> 0
232
		"pcmpeqb "MANGLE(b00)", %%mm2			\n\t" // diff <= QP -> FF
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

		"pand %%mm2, %%mm6				\n\t"
		"pandn %%mm1, %%mm2				\n\t"
		"por %%mm2, %%mm6				\n\t"// First Line to Filter

		"movq (%0, %1, 8), %%mm5			\n\t"
		"leal (%0, %1, 4), %%eax			\n\t"
		"leal (%0, %1, 8), %%ebx			\n\t"
		"subl %1, %%ebx					\n\t"
		"addl %1, %0					\n\t" // %0 points to line 1 not 0
		"movq (%0, %1, 8), %%mm7			\n\t"
		"movq %%mm5, %%mm1				\n\t"
		"movq %%mm7, %%mm2				\n\t"
		"psubusb %%mm7, %%mm5				\n\t"
		"psubusb %%mm1, %%mm2				\n\t"
		"por %%mm5, %%mm2				\n\t" // ABS Diff of lines
		"psubusb %%mm0, %%mm2				\n\t" // diff <= QP -> 0
250
		"pcmpeqb "MANGLE(b00)", %%mm2			\n\t" // diff <= QP -> FF
251 252 253 254 255 256 257 258 259 260 261

		"pand %%mm2, %%mm7				\n\t"
		"pandn %%mm1, %%mm2				\n\t"
		"por %%mm2, %%mm7				\n\t" // First Line to Filter


		// 	1	2	3	4	5	6	7	8
		//	%0	%0+%1	%0+2%1	eax	%0+4%1	eax+2%1	ebx	eax+4%1
		// 6 4 2 2 1 1
		// 6 4 4 2
		// 6 8 2
262

263 264
		"movq (%0, %1), %%mm0				\n\t" //  1
		"movq %%mm0, %%mm1				\n\t" //  1
265 266
		PAVGB(%%mm6, %%mm0)				      //1 1	/2
		PAVGB(%%mm6, %%mm0)				      //3 1	/4
267 268 269

		"movq (%0, %1, 4), %%mm2			\n\t" //     1
		"movq %%mm2, %%mm5				\n\t" //     1
270 271
		PAVGB((%%eax), %%mm2)				      //    11	/2
		PAVGB((%0, %1, 2), %%mm2)			      //   211	/4
272 273
		"movq %%mm2, %%mm3				\n\t" //   211	/4
		"movq (%0), %%mm4				\n\t" // 1
274 275
		PAVGB(%%mm4, %%mm3)				      // 4 211	/8
		PAVGB(%%mm0, %%mm3)				      //642211	/16
276 277 278
		"movq %%mm3, (%0)				\n\t" // X
		// mm1=2 mm2=3(211) mm4=1 mm5=5 mm6=0 mm7=9
		"movq %%mm1, %%mm0				\n\t" //  1
279
		PAVGB(%%mm6, %%mm0)				      //1 1	/2
280
		"movq %%mm4, %%mm3				\n\t" // 1
281 282 283 284 285
		PAVGB((%0,%1,2), %%mm3)				      // 1 1	/2
		PAVGB((%%eax,%1,2), %%mm5)			      //     11	/2
		PAVGB((%%eax), %%mm5)				      //    211 /4
		PAVGB(%%mm5, %%mm3)				      // 2 2211 /8
		PAVGB(%%mm0, %%mm3)				      //4242211 /16
286 287
		"movq %%mm3, (%0,%1)				\n\t" //  X
		// mm1=2 mm2=3(211) mm4=1 mm5=4(211) mm6=0 mm7=9
288
		PAVGB(%%mm4, %%mm6)				      //11	/2
289
		"movq (%%ebx), %%mm0				\n\t" //       1
290
		PAVGB((%%eax, %1, 2), %%mm0)			      //      11/2
291
		"movq %%mm0, %%mm3				\n\t" //      11/2
292 293 294
		PAVGB(%%mm1, %%mm0)				      //  2   11/4
		PAVGB(%%mm6, %%mm0)				      //222   11/8
		PAVGB(%%mm2, %%mm0)				      //22242211/16
295 296 297 298
		"movq (%0, %1, 2), %%mm2			\n\t" //   1
		"movq %%mm0, (%0, %1, 2)			\n\t" //   X
		// mm1=2 mm2=3 mm3=6(11) mm4=1 mm5=4(211) mm6=0(11) mm7=9
		"movq (%%eax, %1, 4), %%mm0			\n\t" //        1
299 300 301 302 303 304
		PAVGB((%%ebx), %%mm0)				      //       11	/2
		PAVGB(%%mm0, %%mm6)				      //11     11	/4
		PAVGB(%%mm1, %%mm4)				      // 11		/2
		PAVGB(%%mm2, %%mm1)				      //  11		/2
		PAVGB(%%mm1, %%mm6)				      //1122   11	/8
		PAVGB(%%mm5, %%mm6)				      //112242211	/16
305 306 307 308
		"movq (%%eax), %%mm5				\n\t" //    1
		"movq %%mm6, (%%eax)				\n\t" //    X
		// mm0=7(11) mm1=2(11) mm2=3 mm3=6(11) mm4=1(11) mm5=4 mm7=9
		"movq (%%eax, %1, 4), %%mm6			\n\t" //        1
309 310 311 312
		PAVGB(%%mm7, %%mm6)				      //        11	/2
		PAVGB(%%mm4, %%mm6)				      // 11     11	/4
		PAVGB(%%mm3, %%mm6)				      // 11   2211	/8
		PAVGB(%%mm5, %%mm2)				      //   11		/2
313
		"movq (%0, %1, 4), %%mm4			\n\t" //     1
314 315
		PAVGB(%%mm4, %%mm2)				      //   112		/4
		PAVGB(%%mm2, %%mm6)				      // 112242211	/16
316 317
		"movq %%mm6, (%0, %1, 4)			\n\t" //     X
		// mm0=7(11) mm1=2(11) mm2=3(112) mm3=6(11) mm4=5 mm5=4 mm7=9
318 319 320
		PAVGB(%%mm7, %%mm1)				      //  11     2	/4
		PAVGB(%%mm4, %%mm5)				      //    11		/2
		PAVGB(%%mm5, %%mm0)				      //    11 11	/4
321
		"movq (%%eax, %1, 2), %%mm6			\n\t" //      1
322 323
		PAVGB(%%mm6, %%mm1)				      //  11  4  2	/8
		PAVGB(%%mm0, %%mm1)				      //  11224222	/16
324 325
		"movq %%mm1, (%%eax, %1, 2)			\n\t" //      X
		// mm2=3(112) mm3=6(11) mm4=5 mm5=4(11) mm6=6 mm7=9
326
		PAVGB((%%ebx), %%mm2)				      //   112 4	/8
327
		"movq (%%eax, %1, 4), %%mm0			\n\t" //        1
328 329 330
		PAVGB(%%mm0, %%mm6)				      //      1 1	/2
		PAVGB(%%mm7, %%mm6)				      //      1 12	/4
		PAVGB(%%mm2, %%mm6)				      //   1122424	/4
331 332
		"movq %%mm6, (%%ebx)				\n\t" //       X
		// mm0=8 mm3=6(11) mm4=5 mm5=4(11) mm7=9
333 334
		PAVGB(%%mm7, %%mm5)				      //    11   2	/4
		PAVGB(%%mm7, %%mm5)				      //    11   6	/8
335

336 337
		PAVGB(%%mm3, %%mm0)				      //      112	/4
		PAVGB(%%mm0, %%mm5)				      //    112246	/16
338
		"movq %%mm5, (%%eax, %1, 4)			\n\t" //        X
Michael Niedermayer's avatar
Michael Niedermayer committed
339
		"subl %1, %0					\n\t"
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

		:
		: "r" (src), "r" (stride)
		: "%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;
355
	int x;
356
	src+= stride*3;
357
	for(x=0; x<BLOCK_SIZE; x++)
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
	{
		const int first= ABS(src[0] - src[l1]) < QP ? src[0] : src[l1];
		const int last= ABS(src[l8] - src[l9]) < QP ? src[l9] : src[l8];

		int sums[9];
		sums[0] = first + src[l1];
		sums[1] = src[l1] + src[l2];
		sums[2] = src[l2] + src[l3];
		sums[3] = src[l3] + src[l4];
		sums[4] = src[l4] + src[l5];
		sums[5] = src[l5] + src[l6];
		sums[6] = src[l6] + src[l7];
		sums[7] = src[l7] + src[l8];
		sums[8] = src[l8] + last;

		src[l1]= ((sums[0]<<2) + ((first + sums[2])<<1) + sums[4] + 8)>>4;
374 375 376 377 378 379 380
		src[l2]= ((src[l2]<<2) + ((first + sums[0] + sums[3])<<1) + sums[5] + 8)>>4;
		src[l3]= ((src[l3]<<2) + ((first + sums[1] + sums[4])<<1) + sums[6] + 8)>>4;
		src[l4]= ((src[l4]<<2) + ((sums[2] + sums[5])<<1) + sums[0] + sums[7] + 8)>>4;
		src[l5]= ((src[l5]<<2) + ((sums[3] + sums[6])<<1) + sums[1] + sums[8] + 8)>>4;
		src[l6]= ((src[l6]<<2) + ((last + sums[7] + sums[4])<<1) + sums[2] + 8)>>4;
		src[l7]= (((last + src[l7])<<2) + ((src[l8] + sums[5])<<1) + sums[3] + 8)>>4;
		src[l8]= ((sums[8]<<2) + ((last + sums[6])<<1) + sums[4] + 8)>>4;
381 382 383 384 385 386 387

		src++;
	}

#endif
}

388 389 390 391 392 393 394 395 396 397 398
/**
 * Experimental implementation of the filter (Algorithm 1) described in a paper from Ramkishor & Karandikar
 * values are correctly clipped (MMX2)
 * values are wraparound (C)
 * conclusion: its fast, but introduces ugly horizontal patterns if there is a continious gradient
	0 8 16 24
	x = 8
	x/2 = 4
	x/8 = 1
	1 12 12 23
 */
399
static inline void RENAME(vertRK1Filter)(uint8_t *src, int stride, int QP)
400
{
401
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
402
	src+= stride*3;
403 404 405
// FIXME rounding
	asm volatile(
		"pxor %%mm7, %%mm7				\n\t" // 0
406
		"movq "MANGLE(b80)", %%mm6			\n\t" // MIN_SIGNED_BYTE
407 408 409 410
		"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
411
		"movq "MANGLE(pQPb)", %%mm0			\n\t" // QP,..., QP
412
		"movq %%mm0, %%mm1				\n\t" // QP,..., QP
413
		"paddusb "MANGLE(b02)", %%mm0			\n\t"
414
		"psrlw $2, %%mm0				\n\t"
415
		"pand "MANGLE(b3F)", %%mm0			\n\t" // QP/4,..., QP/4
416 417 418 419 420 421
		"paddusb %%mm1, %%mm0				\n\t" // QP*1.25 ...
		"movq (%0, %1, 4), %%mm2			\n\t" // line 4
		"movq (%%ebx), %%mm3				\n\t" // line 5
		"movq %%mm2, %%mm4				\n\t" // line 4
		"pcmpeqb %%mm5, %%mm5				\n\t" // -1
		"pxor %%mm2, %%mm5				\n\t" // -line 4 - 1
422
		PAVGB(%%mm3, %%mm5)
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
		"paddb %%mm6, %%mm5				\n\t" // (l5-l4)/2
		"psubusb %%mm3, %%mm4				\n\t"
		"psubusb %%mm2, %%mm3				\n\t"
		"por %%mm3, %%mm4				\n\t" // |l4 - l5|
		"psubusb %%mm0, %%mm4				\n\t"
		"pcmpeqb %%mm7, %%mm4				\n\t"
		"pand %%mm4, %%mm5				\n\t" // d/2

//		"paddb %%mm6, %%mm2				\n\t" // line 4 + 0x80
		"paddb %%mm5, %%mm2				\n\t"
//		"psubb %%mm6, %%mm2				\n\t"
		"movq %%mm2, (%0,%1, 4)				\n\t"

		"movq (%%ebx), %%mm2				\n\t"
//		"paddb %%mm6, %%mm2				\n\t" // line 5 + 0x80
		"psubb %%mm5, %%mm2				\n\t"
//		"psubb %%mm6, %%mm2				\n\t"
		"movq %%mm2, (%%ebx)				\n\t"

		"paddb %%mm6, %%mm5				\n\t"
		"psrlw $2, %%mm5				\n\t"
444 445
		"pand "MANGLE(b3F)", %%mm5			\n\t"
		"psubb "MANGLE(b20)", %%mm5			\n\t" // (l5-l4)/8
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

		"movq (%%eax, %1, 2), %%mm2			\n\t"
		"paddb %%mm6, %%mm2				\n\t" // line 3 + 0x80
		"paddsb %%mm5, %%mm2				\n\t"
		"psubb %%mm6, %%mm2				\n\t"
		"movq %%mm2, (%%eax, %1, 2)			\n\t"

		"movq (%%ebx, %1), %%mm2			\n\t"
		"paddb %%mm6, %%mm2				\n\t" // line 6 + 0x80
		"psubsb %%mm5, %%mm2				\n\t"
		"psubb %%mm6, %%mm2				\n\t"
		"movq %%mm2, (%%ebx, %1)			\n\t"

		:
		: "r" (src), "r" (stride)
		: "%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;
470 471 472
//	const int l7= stride + l6;
//	const int l8= stride + l7;
//	const int l9= stride + l8;
473
	int x;
474
	const int QP15= QP + (QP>>2);
475
	src+= stride*3;
476
	for(x=0; x<BLOCK_SIZE; x++)
477
	{
478 479
		const int v = (src[x+l5] - src[x+l4]);
		if(ABS(v) < QP15)
480
		{
481 482 483 484
			src[x+l3] +=v>>3;
			src[x+l4] +=v>>1;
			src[x+l5] -=v>>1;
			src[x+l6] -=v>>3;
485 486 487 488 489 490 491 492 493

		}
	}

#endif
}

/**
 * Experimental Filter 1
494 495
 * will not damage linear gradients
 * Flat blocks should look like they where passed through the (1,1,2,2,4,2,2,1,1) 9-Tap filter
496 497
 * can only smooth blocks at the expected locations (it cant smooth them if they did move)
 * MMX2 version does correct clipping C version doesnt
498
 */
499
static inline void RENAME(vertX1Filter)(uint8_t *src, int stride, int QP)
500
{
501
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
502 503
	src+= stride*3;

504
	asm volatile(
505 506 507 508 509 510 511 512 513 514 515 516
		"pxor %%mm7, %%mm7				\n\t" // 0
		"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 (%%eax, %1, 2), %%mm0			\n\t" // line 3
		"movq (%0, %1, 4), %%mm1			\n\t" // line 4
		"movq %%mm1, %%mm2				\n\t" // line 4
		"psubusb %%mm0, %%mm1				\n\t"
		"psubusb %%mm2, %%mm0				\n\t"
		"por %%mm1, %%mm0				\n\t" // |l2 - l3|
		"movq (%%ebx), %%mm3				\n\t" // line 5
517
		"movq (%%ebx, %1), %%mm4			\n\t" // line 6
518 519 520 521 522 523 524 525 526 527 528 529 530
		"movq %%mm3, %%mm5				\n\t" // line 5
		"psubusb %%mm4, %%mm3				\n\t"
		"psubusb %%mm5, %%mm4				\n\t"
		"por %%mm4, %%mm3				\n\t" // |l5 - l6|
		PAVGB(%%mm3, %%mm0)				      // (|l2 - l3| + |l5 - l6|)/2
		"movq %%mm2, %%mm1				\n\t" // line 4
		"psubusb %%mm5, %%mm2				\n\t"
		"movq %%mm2, %%mm4				\n\t"
		"pcmpeqb %%mm7, %%mm2				\n\t" // (l4 - l5) <= 0 ? -1 : 0
		"psubusb %%mm1, %%mm5				\n\t"
		"por %%mm5, %%mm4				\n\t" // |l4 - l5|
		"psubusb %%mm0, %%mm4		\n\t" //d = MAX(0, |l4-l5| - (|l2-l3| + |l5-l6|)/2)
		"movq %%mm4, %%mm3				\n\t" // d
531 532 533
		"movq "MANGLE(pQPb)", %%mm0			\n\t"
                "paddusb %%mm0, %%mm0				\n\t"
		"psubusb %%mm0, %%mm4				\n\t"
534
		"pcmpeqb %%mm7, %%mm4				\n\t" // d <= QP ? -1 : 0
535
		"psubusb "MANGLE(b01)", %%mm3			\n\t"
536 537 538
		"pand %%mm4, %%mm3				\n\t" // d <= QP ? d : 0

		PAVGB(%%mm7, %%mm3)				      // d/2
539 540 541
		"movq %%mm3, %%mm1				\n\t" // d/2
		PAVGB(%%mm7, %%mm3)				      // d/4
		PAVGB(%%mm1, %%mm3)				      // 3*d/8
542 543 544 545 546 547 548 549 550 551 552 553 554

		"movq (%0, %1, 4), %%mm0			\n\t" // line 4
		"pxor %%mm2, %%mm0				\n\t" //(l4 - l5) <= 0 ? -l4-1 : l4
		"psubusb %%mm3, %%mm0				\n\t"
		"pxor %%mm2, %%mm0				\n\t"
		"movq %%mm0, (%0, %1, 4)			\n\t" // line 4

		"movq (%%ebx), %%mm0				\n\t" // line 5
		"pxor %%mm2, %%mm0				\n\t" //(l4 - l5) <= 0 ? -l5-1 : l5
		"paddusb %%mm3, %%mm0				\n\t"
		"pxor %%mm2, %%mm0				\n\t"
		"movq %%mm0, (%%ebx)				\n\t" // line 5

555
		PAVGB(%%mm7, %%mm1)				      // d/4
556 557 558

		"movq (%%eax, %1, 2), %%mm0			\n\t" // line 3
		"pxor %%mm2, %%mm0				\n\t" //(l4 - l5) <= 0 ? -l4-1 : l4
559
		"psubusb %%mm1, %%mm0				\n\t"
560 561 562 563 564
		"pxor %%mm2, %%mm0				\n\t"
		"movq %%mm0, (%%eax, %1, 2)			\n\t" // line 3

		"movq (%%ebx, %1), %%mm0			\n\t" // line 6
		"pxor %%mm2, %%mm0				\n\t" //(l4 - l5) <= 0 ? -l5-1 : l5
565
		"paddusb %%mm1, %%mm0				\n\t"
566 567 568
		"pxor %%mm2, %%mm0				\n\t"
		"movq %%mm0, (%%ebx, %1)			\n\t" // line 6

569
		PAVGB(%%mm7, %%mm1)				      // d/8
570 571 572

		"movq (%%eax, %1), %%mm0			\n\t" // line 2
		"pxor %%mm2, %%mm0				\n\t" //(l4 - l5) <= 0 ? -l2-1 : l2
573
		"psubusb %%mm1, %%mm0				\n\t"
574 575 576 577 578
		"pxor %%mm2, %%mm0				\n\t"
		"movq %%mm0, (%%eax, %1)			\n\t" // line 2

		"movq (%%ebx, %1, 2), %%mm0			\n\t" // line 7
		"pxor %%mm2, %%mm0				\n\t" //(l4 - l5) <= 0 ? -l7-1 : l7
579
		"paddusb %%mm1, %%mm0				\n\t"
580 581
		"pxor %%mm2, %%mm0				\n\t"
		"movq %%mm0, (%%ebx, %1, 2)			\n\t" // line 7
582 583 584 585 586 587

		:
		: "r" (src), "r" (stride)
		: "%eax", "%ebx"
	);
#else
588 589 590 591 592 593 594 595

 	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;
596 597
//	const int l8= stride + l7;
//	const int l9= stride + l8;
598
	int x;
599 600

	src+= stride*3;
601 602 603 604
	for(x=0; x<BLOCK_SIZE; x++)
	{
		int a= src[l3] - src[l4];
		int b= src[l4] - src[l5];
605
		int c= src[l5] - src[l6];
606

607 608
		int d= ABS(b) - ((ABS(a) + ABS(c))>>1);
		d= MAX(d, 0);
609

610
		if(d < QP*2)
611 612 613
		{
			int v = d * SIGN(-b);

614 615 616 617 618 619
			src[l2] +=v>>3;
			src[l3] +=v>>2;
			src[l4] +=(3*v)>>3;
			src[l5] -=(3*v)>>3;
			src[l6] -=v>>2;
			src[l7] -=v>>3;
620 621 622 623 624

		}
		src++;
	}
	/*
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
 	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;
	for(int x=0; x<BLOCK_SIZE; x++)
	{
		int v2= src[l2];
		int v3= src[l3];
		int v4= src[l4];
		int v5= src[l5];
		int v6= src[l6];
		int v7= src[l7];

		if(ABS(v4-v5)<QP &&  ABS(v4-v5) - (ABS(v3-v4) + ABS(v5-v6))>0 )
		{
			src[l3] = (6*v2 + 4*v3 + 3*v4 + 2*v5 + v6         )/16;
			src[l4] = (3*v2 + 3*v3 + 4*v4 + 3*v5 + 2*v6 + v7  )/16;
			src[l5] = (1*v2 + 2*v3 + 3*v4 + 4*v5 + 3*v6 + 3*v7)/16;
			src[l6] = (       1*v3 + 2*v4 + 3*v5 + 4*v6 + 6*v7)/16;
		}
		src++;
	}
652
*/
653 654 655
#endif
}

656
static inline void RENAME(doVertDefFilter)(uint8_t src[], int stride, int QP)
657
{
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
/*
	uint8_t tmp[16];
	const int l1= stride;
	const int l2= stride + l1;
	const int l3= stride + l2;
	const int l4= (int)tmp - (int)src - stride*3;
	const int l5= (int)tmp - (int)src - stride*3 + 8;
	const int l6= stride*3 + l3;
	const int l7= stride + l6;
	const int l8= stride + l7;

	memcpy(tmp, src+stride*7, 8);
	memcpy(tmp+8, src+stride*8, 8);
*/
	src+= stride*4;
	asm volatile(

#if 0 //sligtly more accurate and slightly slower
		"pxor %%mm7, %%mm7				\n\t" // 0
		"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, %1, 2), %%mm0			\n\t" // l2
		"movq (%0), %%mm1				\n\t" // l0
		"movq %%mm0, %%mm2				\n\t" // l2
		PAVGB(%%mm7, %%mm0)				      // ~l2/2
		PAVGB(%%mm1, %%mm0)				      // ~(l2 + 2l0)/4
		PAVGB(%%mm2, %%mm0)				      // ~(5l2 + 2l0)/8

		"movq (%%eax), %%mm1				\n\t" // l1
		"movq (%%eax, %1, 2), %%mm3			\n\t" // l3
		"movq %%mm1, %%mm4				\n\t" // l1
		PAVGB(%%mm7, %%mm1)				      // ~l1/2
		PAVGB(%%mm3, %%mm1)				      // ~(l1 + 2l3)/4
		PAVGB(%%mm4, %%mm1)				      // ~(5l1 + 2l3)/8

		"movq %%mm0, %%mm4				\n\t" // ~(5l2 + 2l0)/8
		"psubusb %%mm1, %%mm0				\n\t"
		"psubusb %%mm4, %%mm1				\n\t"
		"por %%mm0, %%mm1				\n\t" // ~|2l0 - 5l1 + 5l2 - 2l3|/8
// mm1= |lenergy|, mm2= l2, mm3= l3, mm7=0

		"movq (%0, %1, 4), %%mm0			\n\t" // l4
		"movq %%mm0, %%mm4				\n\t" // l4
		PAVGB(%%mm7, %%mm0)				      // ~l4/2
		PAVGB(%%mm2, %%mm0)				      // ~(l4 + 2l2)/4
		PAVGB(%%mm4, %%mm0)				      // ~(5l4 + 2l2)/8

		"movq (%%ebx), %%mm2				\n\t" // l5
		"movq %%mm3, %%mm5				\n\t" // l3
		PAVGB(%%mm7, %%mm3)				      // ~l3/2
		PAVGB(%%mm2, %%mm3)				      // ~(l3 + 2l5)/4
		PAVGB(%%mm5, %%mm3)				      // ~(5l3 + 2l5)/8

		"movq %%mm0, %%mm6				\n\t" // ~(5l4 + 2l2)/8
		"psubusb %%mm3, %%mm0				\n\t"
		"psubusb %%mm6, %%mm3				\n\t"
		"por %%mm0, %%mm3				\n\t" // ~|2l2 - 5l3 + 5l4 - 2l5|/8
		"pcmpeqb %%mm7, %%mm0				\n\t" // SIGN(2l2 - 5l3 + 5l4 - 2l5)
// mm0= SIGN(menergy), mm1= |lenergy|, mm2= l5, mm3= |menergy|, mm4=l4, mm5= l3, mm7=0

		"movq (%%ebx, %1), %%mm6			\n\t" // l6
		"movq %%mm6, %%mm5				\n\t" // l6
		PAVGB(%%mm7, %%mm6)				      // ~l6/2
		PAVGB(%%mm4, %%mm6)				      // ~(l6 + 2l4)/4
		PAVGB(%%mm5, %%mm6)				      // ~(5l6 + 2l4)/8

		"movq (%%ebx, %1, 2), %%mm5			\n\t" // l7
		"movq %%mm2, %%mm4				\n\t" // l5
		PAVGB(%%mm7, %%mm2)				      // ~l5/2
		PAVGB(%%mm5, %%mm2)				      // ~(l5 + 2l7)/4
		PAVGB(%%mm4, %%mm2)				      // ~(5l5 + 2l7)/8

		"movq %%mm6, %%mm4				\n\t" // ~(5l6 + 2l4)/8
		"psubusb %%mm2, %%mm6				\n\t"
		"psubusb %%mm4, %%mm2				\n\t"
		"por %%mm6, %%mm2				\n\t" // ~|2l4 - 5l5 + 5l6 - 2l7|/8
// mm0= SIGN(menergy), mm1= |lenergy|/8, mm2= |renergy|/8, mm3= |menergy|/8, mm7=0


		PMINUB(%%mm2, %%mm1, %%mm4)			      // MIN(|lenergy|,|renergy|)/8
744 745
		"movq "MANGLE(pQPb)", %%mm4			\n\t" // QP //FIXME QP+1 ?
		"paddusb "MANGLE(b01)", %%mm4			\n\t"
746 747 748 749 750
		"pcmpgtb %%mm3, %%mm4				\n\t" // |menergy|/8 < QP
		"psubusb %%mm1, %%mm3				\n\t" // d=|menergy|/8-MIN(|lenergy|,|renergy|)/8
		"pand %%mm4, %%mm3				\n\t"

		"movq %%mm3, %%mm1				\n\t"
751
//		"psubusb "MANGLE(b01)", %%mm3			\n\t"
752 753 754
		PAVGB(%%mm7, %%mm3)
		PAVGB(%%mm7, %%mm3)
		"paddusb %%mm1, %%mm3				\n\t"
755
//		"paddusb "MANGLE(b01)", %%mm3			\n\t"
756 757 758 759 760 761 762 763 764 765 766 767

		"movq (%%eax, %1, 2), %%mm6			\n\t" //l3
		"movq (%0, %1, 4), %%mm5			\n\t" //l4
		"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)

768
		"psubusb "MANGLE(b01)", %%mm3			\n\t"
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
		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
800
		"movq "MANGLE(b80)", %%mm4			\n\t" // 128
801 802 803 804 805 806 807 808 809 810 811
		"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
812
		"movq "MANGLE(b80)", %%mm3			\n\t" // 128
813 814 815 816 817 818 819 820 821
		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
822
		"movq "MANGLE(b80)", %%mm2			\n\t" // 128
823 824 825 826 827
		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

828 829
		"movq "MANGLE(b00)", %%mm1			\n\t" // 0
		"movq "MANGLE(b00)", %%mm5			\n\t" // 0
830 831 832 833 834 835 836 837
		"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

838 839
		"movq "MANGLE(b00)", %%mm7			\n\t" // 0
		"movq "MANGLE(pQPb)", %%mm2			\n\t" // QP
840 841 842 843 844 845 846 847 848 849 850 851
		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
852
		"psubusb "MANGLE(b01)", %%mm4			\n\t"
853 854 855 856 857
		PAVGB(%%mm7, %%mm4)				      // d/32
		PAVGB(%%mm7, %%mm4)				      // (d + 32)/64
		"paddb %%mm3, %%mm4				\n\t" // 5d/64
		"pand %%mm2, %%mm4				\n\t"

858
		"movq "MANGLE(b80)", %%mm5			\n\t" // 128
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
		"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)
949
	src+= stride*4;
950

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
	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
995 996
		"movq %%mm0, "MANGLE(temp0)"			\n\t" // 2L0 - 5L1 + 5L2 - 2L3
		"movq %%mm1, "MANGLE(temp1)"			\n\t" // 2H0 - 5H1 + 5H2 - 2H3
997 998 999 1000 1001 1002 1003 1004

		"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
1005 1006
		"movq %%mm2, "MANGLE(temp2)"			\n\t" // L3 - L4
		"movq %%mm3, "MANGLE(temp3)"			\n\t" // H3 - H4
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
		"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

1053 1054
		"movq "MANGLE(temp0)", %%mm2			\n\t" // 2L0 - 5L1 + 5L2 - 2L3
		"movq "MANGLE(temp1)", %%mm3			\n\t" // 2H0 - 5H1 + 5H2 - 2H3
Michael Niedermayer's avatar
Michael Niedermayer committed
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069

#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
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
		"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
1086
#endif
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122

#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


1123
		"movq "MANGLE(w05)", %%mm2			\n\t" // 5
1124 1125
		"pmullw %%mm2, %%mm4				\n\t"
		"pmullw %%mm2, %%mm5				\n\t"
1126
		"movq "MANGLE(w20)", %%mm2			\n\t" // 32
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
		"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
*/

1142 1143
		"movq "MANGLE(temp2)", %%mm0			\n\t" // L3 - L4
		"movq "MANGLE(temp3)", %%mm1			\n\t" // H3 - H4
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153

		"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|
1154 1155
		"psrlw $1, %%mm0				\n\t" // |L3 - L4|/2
		"psrlw $1, %%mm1				\n\t" // |H3 - H4|/2
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198

		"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;
1199
	int x;
1200
	src+= stride*3;
1201
	for(x=0; x<BLOCK_SIZE; x++)
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
	{
		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
}

1235
static inline void RENAME(dering)(uint8_t src[], int stride, int QP)
1236
{
Michael Niedermayer's avatar
Michael Niedermayer committed
1237
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1238
	asm volatile(
1239
		"movq "MANGLE(pQPb)", %%mm0			\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1240
		"paddusb %%mm0, %%mm0				\n\t"
1241
		"movq %%mm0, "MANGLE(pQPb2)"			\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1242

1243 1244 1245 1246 1247
		"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
1248 1249
		"pcmpeqb %%mm7, %%mm7				\n\t"
		"pxor %%mm6, %%mm6				\n\t"
1250
#undef FIND_MIN_MAX
Michael Niedermayer's avatar
Michael Niedermayer committed
1251
#ifdef HAVE_MMX2
1252
#define FIND_MIN_MAX(addr)\
Michael Niedermayer's avatar
Michael Niedermayer committed
1253
		"movq " #addr ", %%mm0				\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
1254 1255
		"pminub %%mm0, %%mm7				\n\t"\
		"pmaxub %%mm0, %%mm6				\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1256 1257 1258
#else
#define FIND_MIN_MAX(addr)\
		"movq " #addr ", %%mm0				\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
1259 1260 1261
		"movq %%mm7, %%mm1				\n\t"\
		"psubusb %%mm0, %%mm6				\n\t"\
		"paddb %%mm0, %%mm6				\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
1262
		"psubusb %%mm0, %%mm1				\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
1263
		"psubb %%mm1, %%mm7				\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1264
#endif
1265

Michael Niedermayer's avatar
Michael Niedermayer committed
1266 1267 1268 1269 1270 1271 1272 1273
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))
1274 1275

		"movq %%mm7, %%mm4				\n\t"
1276 1277
		"psrlq $8, %%mm7				\n\t"
#ifdef HAVE_MMX2
Michael Niedermayer's avatar
Michael Niedermayer committed
1278
		"pminub %%mm4, %%mm7				\n\t" // min of pixels
1279
		"pshufw $0xF9, %%mm7, %%mm4			\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1280
		"pminub %%mm4, %%mm7				\n\t" // min of pixels
1281
		"pshufw $0xFE, %%mm7, %%mm4			\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1282
		"pminub %%mm4, %%mm7				\n\t"
1283
#else
Michael Niedermayer's avatar
Michael Niedermayer committed
1284 1285 1286
		"movq %%mm7, %%mm1				\n\t"
		"psubusb %%mm4, %%mm1				\n\t"
		"psubb %%mm1, %%mm7				\n\t"
1287 1288
		"movq %%mm7, %%mm4				\n\t"
		"psrlq $16, %%mm7				\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1289 1290 1291
		"movq %%mm7, %%mm1				\n\t"
		"psubusb %%mm4, %%mm1				\n\t"
		"psubb %%mm1, %%mm7				\n\t"
1292
		"movq %%mm7, %%mm4				\n\t"
1293
		"psrlq $32, %%mm7				\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1294 1295 1296
		"movq %%mm7, %%mm1				\n\t"
		"psubusb %%mm4, %%mm1				\n\t"
		"psubb %%mm1, %%mm7				\n\t"
1297
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322


		"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"
1323
		"cmpb "MANGLE(deringThreshold)", %%cl		\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1324 1325
		" jb 1f						\n\t"
		PAVGB(%%mm0, %%mm7)				      // a=(max + min)/2
1326 1327 1328
		"punpcklbw %%mm7, %%mm7				\n\t"
		"punpcklbw %%mm7, %%mm7				\n\t"
		"punpcklbw %%mm7, %%mm7				\n\t"
1329
		"movq %%mm7, "MANGLE(temp0)"			\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347

		"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"
1348 1349 1350
		"pcmpeqb "MANGLE(b00)", %%mm0			\n\t" // L10 > a ? 0 : -1
		"pcmpeqb "MANGLE(b00)", %%mm2			\n\t" // L20 > a ? 0 : -1
		"pcmpeqb "MANGLE(b00)", %%mm3			\n\t" // L00 > a ? 0 : -1
Michael Niedermayer's avatar
Michael Niedermayer committed
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
		"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"
1371 1372 1373
		"pcmpeqb "MANGLE(b00)", %%mm2			\n\t" // L11 > a ? 0 : -1
		"pcmpeqb "MANGLE(b00)", %%mm4			\n\t" // L21 > a ? 0 : -1
		"pcmpeqb "MANGLE(b00)", %%mm5			\n\t" // L01 > a ? 0 : -1
Michael Niedermayer's avatar
Michael Niedermayer committed
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
		"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
1392
		PAVGB(lx, pplx)					     \
1393 1394
		"movq " #lx ", "MANGLE(temp1)"			\n\t"\
		"movq "MANGLE(temp0)", " #lx "			\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
1395 1396 1397
		"psubusb " #lx ", " #t1 "			\n\t"\
		"psubusb " #lx ", " #t0 "			\n\t"\
		"psubusb " #lx ", " #sx "			\n\t"\
1398
		"movq "MANGLE(b00)", " #lx "			\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
1399 1400 1401
		"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
1402 1403 1404 1405 1406
		"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
1407
		"movq " #t0 ", " #t1 "				\n\t" /* dst */\
1408 1409
		"psubusb "MANGLE(pQPb2)", " #t0 "		\n\t"\
		"paddusb "MANGLE(pQPb2)", " #t1 "		\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
1410 1411
		PMAXUB(t0, pplx)\
		PMINUB(t1, pplx, t0)\
Michael Niedermayer's avatar
Michael Niedermayer committed
1412 1413
		"paddb " #sx ", " #ppsx "			\n\t"\
		"paddb " #psx ", " #ppsx "			\n\t"\
1414 1415
		"#paddb "MANGLE(b02)", " #ppsx "		\n\t"\
		"pand "MANGLE(b08)", " #ppsx "			\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
1416
		"pcmpeqb " #lx ", " #ppsx "			\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
1417
		"pand " #ppsx ", " #pplx "			\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
1418
		"pandn " #dst ", " #ppsx "			\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
1419
		"por " #pplx ", " #ppsx "			\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
1420
		"movq " #ppsx ", " #dst "			\n\t"\
1421
		"movq "MANGLE(temp1)", " #lx "			\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1422

Michael Niedermayer's avatar
Michael Niedermayer committed
1423 1424 1425
/*
0000000
1111111
1426

Michael Niedermayer's avatar
Michael Niedermayer committed
1427 1428 1429 1430 1431 1432
1111110
1111101
1111100
1111011
1111010
1111001
1433

Michael Niedermayer's avatar
Michael Niedermayer committed
1434 1435
1111000
1110111
1436

Michael Niedermayer's avatar
Michael Niedermayer committed
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
*/
//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)
1447

Michael Niedermayer's avatar
Michael Niedermayer committed
1448
		"1:			\n\t"
1449
		: : "r" (src), "r" (stride), "r" (QP)
Michael Niedermayer's avatar
Michael Niedermayer committed
1450
		: "%eax", "%ebx", "%ecx"
1451 1452
	);
#else
Michael Niedermayer's avatar
Michael Niedermayer committed
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
	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
1473 1474
	if(max - min <deringThreshold) return;

Michael Niedermayer's avatar
Michael Niedermayer committed
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
	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
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
#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
1547 1548 1549 1550 1551 1552
				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
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
#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
1570 1571 1572
#endif
}

1573 1574
/**
 * Deinterlaces the given block
Michael Niedermayer's avatar
Michael Niedermayer committed
1575 1576 1577
 * 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
1578
 */
1579
static inline void RENAME(deInterlaceInterpolateLinear)(uint8_t src[], int stride)
1580 1581
{
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
Michael Niedermayer's avatar
Michael Niedermayer committed
1582
	src+= 4*stride;
1583 1584 1585 1586 1587 1588 1589 1590
	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"
1591
		PAVGB(%%mm1, %%mm0)
1592 1593
		"movq %%mm0, (%%eax)				\n\t"
		"movq (%0, %1, 4), %%mm0			\n\t"
1594
		PAVGB(%%mm0, %%mm1)
1595 1596
		"movq %%mm1, (%%eax, %1, 2)			\n\t"
		"movq (%%ebx, %1), %%mm1			\n\t"
1597
		PAVGB(%%mm1, %%mm0)
1598 1599
		"movq %%mm0, (%%ebx)				\n\t"
		"movq (%0, %1, 8), %%mm0			\n\t"
1600
		PAVGB(%%mm0, %%mm1)
1601 1602 1603 1604 1605 1606 1607
		"movq %%mm1, (%%ebx, %1, 2)			\n\t"

		: : "r" (src), "r" (stride)
		: "%eax", "%ebx"
	);
#else
	int x;
Michael Niedermayer's avatar
Michael Niedermayer committed
1608
	src+= 4*stride;
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
	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
1622 1623 1624 1625
 * 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
1626
 * no cliping in C version
1627
 */
1628
static inline void RENAME(deInterlaceInterpolateCubic)(uint8_t src[], int stride)
1629 1630
{
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
Michael Niedermayer's avatar
Michael Niedermayer committed
1631
	src+= stride*3;
1632 1633 1634
	asm volatile(
		"leal (%0, %1), %%eax				\n\t"
		"leal (%%eax, %1, 4), %%ebx			\n\t"
1635 1636 1637 1638 1639
		"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
1640

1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
#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 */\
		"psraw $3, %%mm2				\n\t"	/* H(a+e - (b+d))/16 */\
		"psubw %%mm0, %%mm1				\n\t"	/* L(9b + 9d - a - e)/16 */\
		"psubw %%mm2, %%mm3				\n\t"	/* H(9b + 9d - a - e)/16 */\
		"packuswb %%mm3, %%mm1				\n\t"\
		"movq %%mm1, " #c "				\n\t"

DEINT_CUBIC((%0), (%%eax, %1), (%%eax, %1, 2), (%0, %1, 4), (%%ebx, %1))
DEINT_CUBIC((%%eax, %1), (%0, %1, 4), (%%ebx), (%%ebx, %1), (%0, %1, 8))
DEINT_CUBIC((%0, %1, 4), (%%ebx, %1), (%%ebx, %1, 2), (%0, %1, 8), (%%ecx))
DEINT_CUBIC((%%ebx, %1), (%0, %1, 8), (%%ebx, %1, 4), (%%ecx), (%%ecx, %1, 2))
1667 1668

		: : "r" (src), "r" (stride)
1669
		: "%eax", "%ebx", "ecx"
1670 1671 1672
	);
#else
	int x;
Michael Niedermayer's avatar
Michael Niedermayer committed
1673
	src+= stride*3;
1674 1675
	for(x=0; x<8; x++)
	{
1676 1677 1678 1679
		src[stride*3] = (-src[0]        + 9*src[stride*2] + 9*src[stride*4] - src[stride*6])>>4;
		src[stride*5] = (-src[stride*2] + 9*src[stride*4] + 9*src[stride*6] - src[stride*8])>>4;
		src[stride*7] = (-src[stride*4] + 9*src[stride*6] + 9*src[stride*8] - src[stride*10])>>4;
		src[stride*9] = (-src[stride*6] + 9*src[stride*8] + 9*src[stride*10] - src[stride*12])>>4;
1680 1681 1682 1683 1684 1685 1686
		src++;
	}
#endif
}

/**
 * Deinterlaces the given block
Michael Niedermayer's avatar
Michael Niedermayer committed
1687 1688 1689
 * 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
1690
 * will shift the image up by 1 line (FIXME if this is a problem)
Michael Niedermayer's avatar
Michael Niedermayer committed
1691
 * this filter will read lines 4-13 and write 4-11
1692
 */
1693
static inline void RENAME(deInterlaceBlendLinear)(uint8_t src[], int stride)
1694 1695
{
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
Michael Niedermayer's avatar
Michael Niedermayer committed
1696
	src+= 4*stride;
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
	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" // L0
		"movq (%%eax, %1), %%mm1			\n\t" // L2
		PAVGB(%%mm1, %%mm0)				      // L0+L2
		"movq (%%eax), %%mm2				\n\t" // L1
		PAVGB(%%mm2, %%mm0)
		"movq %%mm0, (%0)				\n\t"
		"movq (%%eax, %1, 2), %%mm0			\n\t" // L3
		PAVGB(%%mm0, %%mm2)				      // L1+L3
		PAVGB(%%mm1, %%mm2)				      // 2L2 + L1 + L3
		"movq %%mm2, (%%eax)				\n\t"
		"movq (%0, %1, 4), %%mm2			\n\t" // L4
		PAVGB(%%mm2, %%mm1)				      // L2+L4
		PAVGB(%%mm0, %%mm1)				      // 2L3 + L2 + L4
		"movq %%mm1, (%%eax, %1)			\n\t"
		"movq (%%ebx), %%mm1				\n\t" // L5
		PAVGB(%%mm1, %%mm0)				      // L3+L5
		PAVGB(%%mm2, %%mm0)				      // 2L4 + L3 + L5
		"movq %%mm0, (%%eax, %1, 2)			\n\t"
		"movq (%%ebx, %1), %%mm0			\n\t" // L6
		PAVGB(%%mm0, %%mm2)				      // L4+L6
		PAVGB(%%mm1, %%mm2)				      // 2L5 + L4 + L6
		"movq %%mm2, (%0, %1, 4)			\n\t"
		"movq (%%ebx, %1, 2), %%mm2			\n\t" // L7
		PAVGB(%%mm2, %%mm1)				      // L5+L7
		PAVGB(%%mm0, %%mm1)				      // 2L6 + L5 + L7
		"movq %%mm1, (%%ebx)				\n\t"
		"movq (%0, %1, 8), %%mm1			\n\t" // L8
		PAVGB(%%mm1, %%mm0)				      // L6+L8
		PAVGB(%%mm2, %%mm0)				      // 2L7 + L6 + L8
		"movq %%mm0, (%%ebx, %1)			\n\t"
		"movq (%%ebx, %1, 4), %%mm0			\n\t" // L9
		PAVGB(%%mm0, %%mm2)				      // L7+L9
		PAVGB(%%mm1, %%mm2)				      // 2L8 + L7 + L9
		"movq %%mm2, (%%ebx, %1, 2)			\n\t"


		: : "r" (src), "r" (stride)
		: "%eax", "%ebx"
	);
#else
	int x;
Michael Niedermayer's avatar
Michael Niedermayer committed
1744
	src+= 4*stride;
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
	for(x=0; x<8; x++)
	{
		src[0       ] = (src[0       ] + 2*src[stride  ] + src[stride*2])>>2;
		src[stride  ] = (src[stride  ] + 2*src[stride*2] + src[stride*3])>>2;
		src[stride*2] = (src[stride*2] + 2*src[stride*3] + src[stride*4])>>2;
		src[stride*3] = (src[stride*3] + 2*src[stride*4] + src[stride*5])>>2;
		src[stride*4] = (src[stride*4] + 2*src[stride*5] + src[stride*6])>>2;
		src[stride*5] = (src[stride*5] + 2*src[stride*6] + src[stride*7])>>2;
		src[stride*6] = (src[stride*6] + 2*src[stride*7] + src[stride*8])>>2;
		src[stride*7] = (src[stride*7] + 2*src[stride*8] + src[stride*9])>>2;
		src++;
	}
#endif
}

/**
 * Deinterlaces the given block
Michael Niedermayer's avatar
Michael Niedermayer committed
1762 1763 1764
 * 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
1765
 */
1766
static inline void RENAME(deInterlaceMedian)(uint8_t src[], int stride)
1767
{
Michael Niedermayer's avatar
Michael Niedermayer committed
1768
#ifdef HAVE_MMX
Michael Niedermayer's avatar
Michael Niedermayer committed
1769
	src+= 4*stride;
Michael Niedermayer's avatar
Michael Niedermayer committed
1770
#ifdef HAVE_MMX2
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
	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), %%mm2			\n\t" //
		"movq (%%eax), %%mm1				\n\t" //
		"movq %%mm0, %%mm3				\n\t"
		"pmaxub %%mm1, %%mm0				\n\t" //
		"pminub %%mm3, %%mm1				\n\t" //
		"pmaxub %%mm2, %%mm1				\n\t" //
		"pminub %%mm1, %%mm0				\n\t"
		"movq %%mm0, (%%eax)				\n\t"

		"movq (%0, %1, 4), %%mm0			\n\t" //
		"movq (%%eax, %1, 2), %%mm1			\n\t" //
		"movq %%mm2, %%mm3				\n\t"
		"pmaxub %%mm1, %%mm2				\n\t" //
		"pminub %%mm3, %%mm1				\n\t" //
		"pmaxub %%mm0, %%mm1				\n\t" //
		"pminub %%mm1, %%mm2				\n\t"
		"movq %%mm2, (%%eax, %1, 2)			\n\t"

		"movq (%%ebx), %%mm2				\n\t" //
		"movq (%%ebx, %1), %%mm1			\n\t" //
		"movq %%mm2, %%mm3				\n\t"
		"pmaxub %%mm0, %%mm2				\n\t" //
		"pminub %%mm3, %%mm0				\n\t" //
		"pmaxub %%mm1, %%mm0				\n\t" //
		"pminub %%mm0, %%mm2				\n\t"
		"movq %%mm2, (%%ebx)				\n\t"

		"movq (%%ebx, %1, 2), %%mm2			\n\t" //
		"movq (%0, %1, 8), %%mm0			\n\t" //
		"movq %%mm2, %%mm3				\n\t"
		"pmaxub %%mm0, %%mm2				\n\t" //
		"pminub %%mm3, %%mm0				\n\t" //
		"pmaxub %%mm1, %%mm0				\n\t" //
		"pminub %%mm0, %%mm2				\n\t"
		"movq %%mm2, (%%ebx, %1, 2)			\n\t"


		: : "r" (src), "r" (stride)
		: "%eax", "%ebx"
	);
Michael Niedermayer's avatar
Michael Niedermayer committed
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859

#else // MMX without MMX2
	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
		"pxor %%mm7, %%mm7				\n\t"

#define MEDIAN(a,b,c)\
		"movq " #a ", %%mm0				\n\t"\
		"movq " #b ", %%mm2				\n\t"\
		"movq " #c ", %%mm1				\n\t"\
		"movq %%mm0, %%mm3				\n\t"\
		"movq %%mm1, %%mm4				\n\t"\
		"movq %%mm2, %%mm5				\n\t"\
		"psubusb %%mm1, %%mm3				\n\t"\
		"psubusb %%mm2, %%mm4				\n\t"\
		"psubusb %%mm0, %%mm5				\n\t"\
		"pcmpeqb %%mm7, %%mm3				\n\t"\
		"pcmpeqb %%mm7, %%mm4				\n\t"\
		"pcmpeqb %%mm7, %%mm5				\n\t"\
		"movq %%mm3, %%mm6				\n\t"\
		"pxor %%mm4, %%mm3				\n\t"\
		"pxor %%mm5, %%mm4				\n\t"\
		"pxor %%mm6, %%mm5				\n\t"\
		"por %%mm3, %%mm1				\n\t"\
		"por %%mm4, %%mm2				\n\t"\
		"por %%mm5, %%mm0				\n\t"\
		"pand %%mm2, %%mm0				\n\t"\
		"pand %%mm1, %%mm0				\n\t"\
		"movq %%mm0, " #b "				\n\t"

MEDIAN((%0), (%%eax), (%%eax, %1))
MEDIAN((%%eax, %1), (%%eax, %1, 2), (%0, %1, 4))
MEDIAN((%0, %1, 4), (%%ebx), (%%ebx, %1))
MEDIAN((%%ebx, %1), (%%ebx, %1, 2), (%0, %1, 8))

		: : "r" (src), "r" (stride)
		: "%eax", "%ebx"
	);
#endif // MMX
1860 1861 1862
#else
	//FIXME
	int x;
Michael Niedermayer's avatar
Michael Niedermayer committed
1863
	src+= 4*stride;
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
	for(x=0; x<8; x++)
	{
		src[0       ] = (src[0       ] + 2*src[stride  ] + src[stride*2])>>2;
		src[stride  ] = (src[stride  ] + 2*src[stride*2] + src[stride*3])>>2;
		src[stride*2] = (src[stride*2] + 2*src[stride*3] + src[stride*4])>>2;
		src[stride*3] = (src[stride*3] + 2*src[stride*4] + src[stride*5])>>2;
		src[stride*4] = (src[stride*4] + 2*src[stride*5] + src[stride*6])>>2;
		src[stride*5] = (src[stride*5] + 2*src[stride*6] + src[stride*7])>>2;
		src[stride*6] = (src[stride*6] + 2*src[stride*7] + src[stride*8])>>2;
		src[stride*7] = (src[stride*7] + 2*src[stride*8] + src[stride*9])>>2;
		src++;
	}
#endif
}

1879
#ifdef HAVE_MMX
1880 1881 1882
/**
 * transposes and shift the given 8x8 Block into dst1 and dst2
 */
1883
static inline void RENAME(transpose1)(uint8_t *dst1, uint8_t *dst2, uint8_t *src, int srcStride)
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
{
	asm(
		"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" // 12345678
		"movq (%%eax), %%mm1		\n\t" // abcdefgh
		"movq %%mm0, %%mm2		\n\t" // 12345678
		"punpcklbw %%mm1, %%mm0		\n\t" // 1a2b3c4d
		"punpckhbw %%mm1, %%mm2		\n\t" // 5e6f7g8h

		"movq (%%eax, %1), %%mm1	\n\t"
		"movq (%%eax, %1, 2), %%mm3	\n\t"
		"movq %%mm1, %%mm4		\n\t"
		"punpcklbw %%mm3, %%mm1		\n\t"
		"punpckhbw %%mm3, %%mm4		\n\t"

		"movq %%mm0, %%mm3		\n\t"
		"punpcklwd %%mm1, %%mm0		\n\t"
		"punpckhwd %%mm1, %%mm3		\n\t"
		"movq %%mm2, %%mm1		\n\t"
		"punpcklwd %%mm4, %%mm2		\n\t"
		"punpckhwd %%mm4, %%mm1		\n\t"

		"movd %%mm0, 128(%2)		\n\t"
		"psrlq $32, %%mm0		\n\t"
		"movd %%mm0, 144(%2)		\n\t"
		"movd %%mm3, 160(%2)		\n\t"
		"psrlq $32, %%mm3		\n\t"
		"movd %%mm3, 176(%2)		\n\t"
		"movd %%mm3, 48(%3)		\n\t"
		"movd %%mm2, 192(%2)		\n\t"
		"movd %%mm2, 64(%3)		\n\t"
		"psrlq $32, %%mm2		\n\t"
		"movd %%mm2, 80(%3)		\n\t"
		"movd %%mm1, 96(%3)		\n\t"
		"psrlq $32, %%mm1		\n\t"
		"movd %%mm1, 112(%3)		\n\t"

		"movq (%0, %1, 4), %%mm0	\n\t" // 12345678
		"movq (%%ebx), %%mm1		\n\t" // abcdefgh
		"movq %%mm0, %%mm2		\n\t" // 12345678
		"punpcklbw %%mm1, %%mm0		\n\t" // 1a2b3c4d
		"punpckhbw %%mm1, %%mm2		\n\t" // 5e6f7g8h

		"movq (%%ebx, %1), %%mm1	\n\t"
		"movq (%%ebx, %1, 2), %%mm3	\n\t"
		"movq %%mm1, %%mm4		\n\t"
		"punpcklbw %%mm3, %%mm1		\n\t"
		"punpckhbw %%mm3, %%mm4		\n\t"

		"movq %%mm0, %%mm3		\n\t"
		"punpcklwd %%mm1, %%mm0		\n\t"
		"punpckhwd %%mm1, %%mm3		\n\t"
		"movq %%mm2, %%mm1		\n\t"
		"punpcklwd %%mm4, %%mm2		\n\t"
		"punpckhwd %%mm4, %%mm1		\n\t"

		"movd %%mm0, 132(%2)		\n\t"
		"psrlq $32, %%mm0		\n\t"
		"movd %%mm0, 148(%2)		\n\t"
		"movd %%mm3, 164(%2)		\n\t"
		"psrlq $32, %%mm3		\n\t"
		"movd %%mm3, 180(%2)		\n\t"
		"movd %%mm3, 52(%3)		\n\t"
		"movd %%mm2, 196(%2)		\n\t"
		"movd %%mm2, 68(%3)		\n\t"
		"psrlq $32, %%mm2		\n\t"
		"movd %%mm2, 84(%3)		\n\t"
		"movd %%mm1, 100(%3)		\n\t"
		"psrlq $32, %%mm1		\n\t"
		"movd %%mm1, 116(%3)		\n\t"


	:: "r" (src), "r" (srcStride), "r" (dst1), "r" (dst2)
	: "%eax", "%ebx"
	);
}

/**
 * transposes the given 8x8 block
 */
1967
static inline void RENAME(transpose2)(uint8_t *dst, int dstStride, uint8_t *src)
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
{
	asm(
		"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 (%2), %%mm0		\n\t" // 12345678
		"movq 16(%2), %%mm1		\n\t" // abcdefgh
		"movq %%mm0, %%mm2		\n\t" // 12345678
		"punpcklbw %%mm1, %%mm0		\n\t" // 1a2b3c4d
		"punpckhbw %%mm1, %%mm2		\n\t" // 5e6f7g8h

		"movq 32(%2), %%mm1		\n\t"
		"movq 48(%2), %%mm3		\n\t"
		"movq %%mm1, %%mm4		\n\t"
		"punpcklbw %%mm3, %%mm1		\n\t"
		"punpckhbw %%mm3, %%mm4		\n\t"

		"movq %%mm0, %%mm3		\n\t"
		"punpcklwd %%mm1, %%mm0		\n\t"
		"punpckhwd %%mm1, %%mm3		\n\t"
		"movq %%mm2, %%mm1		\n\t"
		"punpcklwd %%mm4, %%mm2		\n\t"
		"punpckhwd %%mm4, %%mm1		\n\t"

		"movd %%mm0, (%0)		\n\t"
		"psrlq $32, %%mm0		\n\t"
		"movd %%mm0, (%%eax)		\n\t"
		"movd %%mm3, (%%eax, %1)	\n\t"
		"psrlq $32, %%mm3		\n\t"
		"movd %%mm3, (%%eax, %1, 2)	\n\t"
		"movd %%mm2, (%0, %1, 4)	\n\t"
		"psrlq $32, %%mm2		\n\t"
		"movd %%mm2, (%%ebx)		\n\t"
		"movd %%mm1, (%%ebx, %1)	\n\t"
		"psrlq $32, %%mm1		\n\t"
		"movd %%mm1, (%%ebx, %1, 2)	\n\t"


		"movq 64(%2), %%mm0		\n\t" // 12345678
		"movq 80(%2), %%mm1		\n\t" // abcdefgh
		"movq %%mm0, %%mm2		\n\t" // 12345678
		"punpcklbw %%mm1, %%mm0		\n\t" // 1a2b3c4d
		"punpckhbw %%mm1, %%mm2		\n\t" // 5e6f7g8h

		"movq 96(%2), %%mm1		\n\t"
		"movq 112(%2), %%mm3		\n\t"
		"movq %%mm1, %%mm4		\n\t"
		"punpcklbw %%mm3, %%mm1		\n\t"
		"punpckhbw %%mm3, %%mm4		\n\t"

		"movq %%mm0, %%mm3		\n\t"
		"punpcklwd %%mm1, %%mm0		\n\t"
		"punpckhwd %%mm1, %%mm3		\n\t"
		"movq %%mm2, %%mm1		\n\t"
		"punpcklwd %%mm4, %%mm2		\n\t"
		"punpckhwd %%mm4, %%mm1		\n\t"

		"movd %%mm0, 4(%0)		\n\t"
		"psrlq $32, %%mm0		\n\t"
		"movd %%mm0, 4(%%eax)		\n\t"
		"movd %%mm3, 4(%%eax, %1)	\n\t"
		"psrlq $32, %%mm3		\n\t"
		"movd %%mm3, 4(%%eax, %1, 2)	\n\t"
		"movd %%mm2, 4(%0, %1, 4)	\n\t"
		"psrlq $32, %%mm2		\n\t"
		"movd %%mm2, 4(%%ebx)		\n\t"
		"movd %%mm1, 4(%%ebx, %1)	\n\t"
		"psrlq $32, %%mm1		\n\t"
		"movd %%mm1, 4(%%ebx, %1, 2)	\n\t"

	:: "r" (dst), "r" (dstStride), "r" (src)
	: "%eax", "%ebx"
	);
}
2043
#endif
2044
//static int test=0;
2045

2046
static void inline RENAME(tempNoiseReducer)(uint8_t *src, int stride,
Michael Niedermayer's avatar
Michael Niedermayer committed
2047
				    uint8_t *tempBlured, uint32_t *tempBluredPast, int *maxNoise)
2048
{
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085
#define FAST_L2_DIFF
//#define L1_DIFF //u should change the thresholds too if u try that one
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
	asm volatile(
		"leal (%2, %2, 2), %%eax			\n\t" // 3*stride
		"leal (%2, %2, 4), %%ebx			\n\t" // 5*stride
		"leal (%%ebx, %2, 2), %%ecx			\n\t" // 7*stride
//	0	1	2	3	4	5	6	7	8	9
//	%x	%x+%2	%x+2%2	%x+eax	%x+4%2	%x+ebx	%x+2eax	%x+ecx	%x+8%2
//FIXME reorder?
#ifdef L1_DIFF //needs mmx2
		"movq (%0), %%mm0				\n\t" // L0
		"psadbw (%1), %%mm0				\n\t" // |L0-R0|
		"movq (%0, %2), %%mm1				\n\t" // L1
		"psadbw (%1, %2), %%mm1				\n\t" // |L1-R1|
		"movq (%0, %2, 2), %%mm2			\n\t" // L2
		"psadbw (%1, %2, 2), %%mm2			\n\t" // |L2-R2|
		"movq (%0, %%eax), %%mm3			\n\t" // L3
		"psadbw (%1, %%eax), %%mm3			\n\t" // |L3-R3|

		"movq (%0, %2, 4), %%mm4			\n\t" // L4
		"paddw %%mm1, %%mm0				\n\t"
		"psadbw (%1, %2, 4), %%mm4			\n\t" // |L4-R4|
		"movq (%0, %%ebx), %%mm5			\n\t" // L5
		"paddw %%mm2, %%mm0				\n\t"
		"psadbw (%1, %%ebx), %%mm5			\n\t" // |L5-R5|
		"movq (%0, %%eax, 2), %%mm6			\n\t" // L6
		"paddw %%mm3, %%mm0				\n\t"
		"psadbw (%1, %%eax, 2), %%mm6			\n\t" // |L6-R6|
		"movq (%0, %%ecx), %%mm7			\n\t" // L7
		"paddw %%mm4, %%mm0				\n\t"
		"psadbw (%1, %%ecx), %%mm7			\n\t" // |L7-R7|
		"paddw %%mm5, %%mm6				\n\t"
		"paddw %%mm7, %%mm6				\n\t"
		"paddw %%mm6, %%mm0				\n\t"
#elif defined (FAST_L2_DIFF)
		"pcmpeqb %%mm7, %%mm7				\n\t"
2086
		"movq "MANGLE(b80)", %%mm6			\n\t"
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
		"pxor %%mm0, %%mm0				\n\t"
#define L2_DIFF_CORE(a, b)\
		"movq " #a ", %%mm5				\n\t"\
		"movq " #b ", %%mm2				\n\t"\
		"pxor %%mm7, %%mm2				\n\t"\
		PAVGB(%%mm2, %%mm5)\
		"paddb %%mm6, %%mm5				\n\t"\
		"movq %%mm5, %%mm2				\n\t"\
		"psllw $8, %%mm5				\n\t"\
		"pmaddwd %%mm5, %%mm5				\n\t"\
		"pmaddwd %%mm2, %%mm2				\n\t"\
		"paddd %%mm2, %%mm5				\n\t"\
		"psrld $14, %%mm5				\n\t"\
		"paddd %%mm5, %%mm0				\n\t"

L2_DIFF_CORE((%0), (%1))
L2_DIFF_CORE((%0, %2), (%1, %2))
L2_DIFF_CORE((%0, %2, 2), (%1, %2, 2))
L2_DIFF_CORE((%0, %%eax), (%1, %%eax))
L2_DIFF_CORE((%0, %2, 4), (%1, %2, 4))
L2_DIFF_CORE((%0, %%ebx), (%1, %%ebx))
L2_DIFF_CORE((%0, %%eax,2), (%1, %%eax,2))
L2_DIFF_CORE((%0, %%ecx), (%1, %%ecx))

#else
		"pxor %%mm7, %%mm7				\n\t"
		"pxor %%mm0, %%mm0				\n\t"
#define L2_DIFF_CORE(a, b)\
		"movq " #a ", %%mm5				\n\t"\
		"movq " #b ", %%mm2				\n\t"\
		"movq %%mm5, %%mm1				\n\t"\
		"movq %%mm2, %%mm3				\n\t"\
		"punpcklbw %%mm7, %%mm5				\n\t"\
		"punpckhbw %%mm7, %%mm1				\n\t"\
		"punpcklbw %%mm7, %%mm2				\n\t"\
		"punpckhbw %%mm7, %%mm3				\n\t"\
		"psubw %%mm2, %%mm5				\n\t"\
		"psubw %%mm3, %%mm1				\n\t"\
		"pmaddwd %%mm5, %%mm5				\n\t"\
		"pmaddwd %%mm1, %%mm1				\n\t"\
		"paddd %%mm1, %%mm5				\n\t"\
		"paddd %%mm5, %%mm0				\n\t"

L2_DIFF_CORE((%0), (%1))
L2_DIFF_CORE((%0, %2), (%1, %2))
L2_DIFF_CORE((%0, %2, 2), (%1, %2, 2))
L2_DIFF_CORE((%0, %%eax), (%1, %%eax))
L2_DIFF_CORE((%0, %2, 4), (%1, %2, 4))
L2_DIFF_CORE((%0, %%ebx), (%1, %%ebx))
L2_DIFF_CORE((%0, %%eax,2), (%1, %%eax,2))
L2_DIFF_CORE((%0, %%ecx), (%1, %%ecx))

#endif

		"movq %%mm0, %%mm4				\n\t"
		"psrlq $32, %%mm0				\n\t"
		"paddd %%mm0, %%mm4				\n\t"
		"movd %%mm4, %%ecx				\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
		"shll $2, %%ecx					\n\t"
		"movl %3, %%ebx					\n\t"
		"addl -4(%%ebx), %%ecx				\n\t"
		"addl 4(%%ebx), %%ecx				\n\t"
		"addl -1024(%%ebx), %%ecx			\n\t"
		"addl $4, %%ecx					\n\t"
		"addl 1024(%%ebx), %%ecx			\n\t"
		"shrl $3, %%ecx					\n\t"
		"movl %%ecx, (%%ebx)				\n\t"
		"leal (%%eax, %2, 2), %%ebx			\n\t" // 5*stride

2156
//		"movl %3, %%ecx					\n\t"
2157 2158
//		"movl %%ecx, test				\n\t"
//		"jmp 4f \n\t"
2159
		"cmpl 4+"MANGLE(maxTmpNoise)", %%ecx		\n\t"
2160
		" jb 2f						\n\t"
2161
		"cmpl 8+"MANGLE(maxTmpNoise)", %%ecx		\n\t"
2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
		" jb 1f						\n\t"

		"leal (%%ebx, %2, 2), %%ecx			\n\t" // 7*stride
		"movq (%0), %%mm0				\n\t" // L0
		"movq (%0, %2), %%mm1				\n\t" // L1
		"movq (%0, %2, 2), %%mm2			\n\t" // L2
		"movq (%0, %%eax), %%mm3			\n\t" // L3
		"movq (%0, %2, 4), %%mm4			\n\t" // L4
		"movq (%0, %%ebx), %%mm5			\n\t" // L5
		"movq (%0, %%eax, 2), %%mm6			\n\t" // L6
		"movq (%0, %%ecx), %%mm7			\n\t" // L7
		"movq %%mm0, (%1)				\n\t" // L0
		"movq %%mm1, (%1, %2)				\n\t" // L1
		"movq %%mm2, (%1, %2, 2)			\n\t" // L2
		"movq %%mm3, (%1, %%eax)			\n\t" // L3
		"movq %%mm4, (%1, %2, 4)			\n\t" // L4
		"movq %%mm5, (%1, %%ebx)			\n\t" // L5
		"movq %%mm6, (%1, %%eax, 2)			\n\t" // L6
		"movq %%mm7, (%1, %%ecx)			\n\t" // L7
		"jmp 4f						\n\t"

		"1:						\n\t"
		"leal (%%ebx, %2, 2), %%ecx			\n\t" // 7*stride
		"movq (%0), %%mm0				\n\t" // L0
2186
		PAVGB((%1), %%mm0)				      // L0
2187
		"movq (%0, %2), %%mm1				\n\t" // L1
2188
		PAVGB((%1, %2), %%mm1)				      // L1
2189
		"movq (%0, %2, 2), %%mm2			\n\t" // L2
2190
		PAVGB((%1, %2, 2), %%mm2)			      // L2
2191
		"movq (%0, %%eax), %%mm3			\n\t" // L3
2192
		PAVGB((%1, %%eax), %%mm3)			      // L3
2193
		"movq (%0, %2, 4), %%mm4			\n\t" // L4
2194
		PAVGB((%1, %2, 4), %%mm4)			      // L4
2195
		"movq (%0, %%ebx), %%mm5			\n\t" // L5
2196
		PAVGB((%1, %%ebx), %%mm5)			      // L5
2197
		"movq (%0, %%eax, 2), %%mm6			\n\t" // L6
2198
		PAVGB((%1, %%eax, 2), %%mm6)			      // L6
2199
		"movq (%0, %%ecx), %%mm7			\n\t" // L7
2200
		PAVGB((%1, %%ecx), %%mm7)			      // L7
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219
		"movq %%mm0, (%1)				\n\t" // R0
		"movq %%mm1, (%1, %2)				\n\t" // R1
		"movq %%mm2, (%1, %2, 2)			\n\t" // R2
		"movq %%mm3, (%1, %%eax)			\n\t" // R3
		"movq %%mm4, (%1, %2, 4)			\n\t" // R4
		"movq %%mm5, (%1, %%ebx)			\n\t" // R5
		"movq %%mm6, (%1, %%eax, 2)			\n\t" // R6
		"movq %%mm7, (%1, %%ecx)			\n\t" // R7
		"movq %%mm0, (%0)				\n\t" // L0
		"movq %%mm1, (%0, %2)				\n\t" // L1
		"movq %%mm2, (%0, %2, 2)			\n\t" // L2
		"movq %%mm3, (%0, %%eax)			\n\t" // L3
		"movq %%mm4, (%0, %2, 4)			\n\t" // L4
		"movq %%mm5, (%0, %%ebx)			\n\t" // L5
		"movq %%mm6, (%0, %%eax, 2)			\n\t" // L6
		"movq %%mm7, (%0, %%ecx)			\n\t" // L7
		"jmp 4f						\n\t"

		"2:						\n\t"
2220
		"cmpl "MANGLE(maxTmpNoise)", %%ecx		\n\t"
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
		" jb 3f						\n\t"

		"leal (%%ebx, %2, 2), %%ecx			\n\t" // 7*stride
		"movq (%0), %%mm0				\n\t" // L0
		"movq (%0, %2), %%mm1				\n\t" // L1
		"movq (%0, %2, 2), %%mm2			\n\t" // L2
		"movq (%0, %%eax), %%mm3			\n\t" // L3
		"movq (%1), %%mm4				\n\t" // R0
		"movq (%1, %2), %%mm5				\n\t" // R1
		"movq (%1, %2, 2), %%mm6			\n\t" // R2
		"movq (%1, %%eax), %%mm7			\n\t" // R3
		PAVGB(%%mm4, %%mm0)
		PAVGB(%%mm5, %%mm1)
		PAVGB(%%mm6, %%mm2)
		PAVGB(%%mm7, %%mm3)
		PAVGB(%%mm4, %%mm0)
		PAVGB(%%mm5, %%mm1)
		PAVGB(%%mm6, %%mm2)
		PAVGB(%%mm7, %%mm3)
		"movq %%mm0, (%1)				\n\t" // R0
		"movq %%mm1, (%1, %2)				\n\t" // R1
		"movq %%mm2, (%1, %2, 2)			\n\t" // R2
		"movq %%mm3, (%1, %%eax)			\n\t" // R3
		"movq %%mm0, (%0)				\n\t" // L0
		"movq %%mm1, (%0, %2)				\n\t" // L1
		"movq %%mm2, (%0, %2, 2)			\n\t" // L2
		"movq %%mm3, (%0, %%eax)			\n\t" // L3

		"movq (%0, %2, 4), %%mm0			\n\t" // L4
		"movq (%0, %%ebx), %%mm1			\n\t" // L5
		"movq (%0, %%eax, 2), %%mm2			\n\t" // L6
		"movq (%0, %%ecx), %%mm3			\n\t" // L7
		"movq (%1, %2, 4), %%mm4			\n\t" // R4
		"movq (%1, %%ebx), %%mm5			\n\t" // R5
		"movq (%1, %%eax, 2), %%mm6			\n\t" // R6
		"movq (%1, %%ecx), %%mm7			\n\t" // R7
		PAVGB(%%mm4, %%mm0)
		PAVGB(%%mm5, %%mm1)
		PAVGB(%%mm6, %%mm2)
		PAVGB(%%mm7, %%mm3)
		PAVGB(%%mm4, %%mm0)
		PAVGB(%%mm5, %%mm1)
		PAVGB(%%mm6, %%mm2)
		PAVGB(%%mm7, %%mm3)
		"movq %%mm0, (%1, %2, 4)			\n\t" // R4
		"movq %%mm1, (%1, %%ebx)			\n\t" // R5
		"movq %%mm2, (%1, %%eax, 2)			\n\t" // R6
		"movq %%mm3, (%1, %%ecx)			\n\t" // R7
		"movq %%mm0, (%0, %2, 4)			\n\t" // L4
		"movq %%mm1, (%0, %%ebx)			\n\t" // L5
		"movq %%mm2, (%0, %%eax, 2)			\n\t" // L6
		"movq %%mm3, (%0, %%ecx)			\n\t" // L7
		"jmp 4f						\n\t"

		"3:						\n\t"
		"leal (%%ebx, %2, 2), %%ecx			\n\t" // 7*stride
		"movq (%0), %%mm0				\n\t" // L0
		"movq (%0, %2), %%mm1				\n\t" // L1
		"movq (%0, %2, 2), %%mm2			\n\t" // L2
		"movq (%0, %%eax), %%mm3			\n\t" // L3
		"movq (%1), %%mm4				\n\t" // R0
		"movq (%1, %2), %%mm5				\n\t" // R1
		"movq (%1, %2, 2), %%mm6			\n\t" // R2
		"movq (%1, %%eax), %%mm7			\n\t" // R3
		PAVGB(%%mm4, %%mm0)
		PAVGB(%%mm5, %%mm1)
		PAVGB(%%mm6, %%mm2)
		PAVGB(%%mm7, %%mm3)
		PAVGB(%%mm4, %%mm0)
		PAVGB(%%mm5, %%mm1)
		PAVGB(%%mm6, %%mm2)
		PAVGB(%%mm7, %%mm3)
		PAVGB(%%mm4, %%mm0)
		PAVGB(%%mm5, %%mm1)
		PAVGB(%%mm6, %%mm2)
		PAVGB(%%mm7, %%mm3)
		"movq %%mm0, (%1)				\n\t" // R0
		"movq %%mm1, (%1, %2)				\n\t" // R1
		"movq %%mm2, (%1, %2, 2)			\n\t" // R2
		"movq %%mm3, (%1, %%eax)			\n\t" // R3
		"movq %%mm0, (%0)				\n\t" // L0
		"movq %%mm1, (%0, %2)				\n\t" // L1
		"movq %%mm2, (%0, %2, 2)			\n\t" // L2
		"movq %%mm3, (%0, %%eax)			\n\t" // L3

		"movq (%0, %2, 4), %%mm0			\n\t" // L4
		"movq (%0, %%ebx), %%mm1			\n\t" // L5
		"movq (%0, %%eax, 2), %%mm2			\n\t" // L6
		"movq (%0, %%ecx), %%mm3			\n\t" // L7
		"movq (%1, %2, 4), %%mm4			\n\t" // R4
		"movq (%1, %%ebx), %%mm5			\n\t" // R5
		"movq (%1, %%eax, 2), %%mm6			\n\t" // R6
		"movq (%1, %%ecx), %%mm7			\n\t" // R7
		PAVGB(%%mm4, %%mm0)
		PAVGB(%%mm5, %%mm1)
		PAVGB(%%mm6, %%mm2)
		PAVGB(%%mm7, %%mm3)
		PAVGB(%%mm4, %%mm0)
		PAVGB(%%mm5, %%mm1)
		PAVGB(%%mm6, %%mm2)
		PAVGB(%%mm7, %%mm3)
		PAVGB(%%mm4, %%mm0)
		PAVGB(%%mm5, %%mm1)
		PAVGB(%%mm6, %%mm2)
		PAVGB(%%mm7, %%mm3)
		"movq %%mm0, (%1, %2, 4)			\n\t" // R4
		"movq %%mm1, (%1, %%ebx)			\n\t" // R5
		"movq %%mm2, (%1, %%eax, 2)			\n\t" // R6
		"movq %%mm3, (%1, %%ecx)			\n\t" // R7
		"movq %%mm0, (%0, %2, 4)			\n\t" // L4
		"movq %%mm1, (%0, %%ebx)			\n\t" // L5
		"movq %%mm2, (%0, %%eax, 2)			\n\t" // L6
		"movq %%mm3, (%0, %%ecx)			\n\t" // L7

		"4:						\n\t"

Michael Niedermayer's avatar
Michael Niedermayer committed
2337
		:: "r" (src), "r" (tempBlured), "r"(stride), "m" (tempBluredPast)
2338 2339 2340 2341
		: "%eax", "%ebx", "%ecx", "memory"
		);
//printf("%d\n", test);
#else
2342 2343 2344
	int y;
	int d=0;
	int sysd=0;
Michael Niedermayer's avatar
Michael Niedermayer committed
2345
	int i;
2346 2347 2348 2349 2350 2351 2352 2353 2354

	for(y=0; y<8; y++)
	{
		int x;
		for(x=0; x<8; x++)
		{
			int ref= tempBlured[ x + y*stride ];
			int cur= src[ x + y*stride ];
			int d1=ref - cur;
2355 2356 2357 2358
//			if(x==0 || x==7) d1+= d1>>1;
//			if(y==0 || y==7) d1+= d1>>1;
//			d+= ABS(d1);
			d+= d1*d1;
2359 2360 2361
			sysd+= d1;
		}
	}
Michael Niedermayer's avatar
Michael Niedermayer committed
2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
	i=d;
	d= 	(
		4*d
		+(*(tempBluredPast-256))
		+(*(tempBluredPast-1))+ (*(tempBluredPast+1))
		+(*(tempBluredPast+256))
		+4)>>3;
	*tempBluredPast=i;
//	((*tempBluredPast)*3 + d + 2)>>2;

2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441
//printf("%d %d %d\n", maxNoise[0], maxNoise[1], maxNoise[2]);
/*
Switch between
 1  0  0  0  0  0  0  (0)
64 32 16  8  4  2  1  (1)
64 48 36 27 20 15 11 (33) (approx)
64 56 49 43 37 33 29 (200) (approx)
*/
	if(d > maxNoise[1])
	{
		if(d < maxNoise[2])
		{
			for(y=0; y<8; y++)
			{
				int x;
				for(x=0; x<8; x++)
				{
					int ref= tempBlured[ x + y*stride ];
					int cur= src[ x + y*stride ];
					tempBlured[ x + y*stride ]=
					src[ x + y*stride ]=
						(ref + cur + 1)>>1;
				}
			}
		}
		else
		{
			for(y=0; y<8; y++)
			{
				int x;
				for(x=0; x<8; x++)
				{
					tempBlured[ x + y*stride ]= src[ x + y*stride ];
				}
			}
		}
	}
	else
	{
		if(d < maxNoise[0])
		{
			for(y=0; y<8; y++)
			{
				int x;
				for(x=0; x<8; x++)
				{
					int ref= tempBlured[ x + y*stride ];
					int cur= src[ x + y*stride ];
					tempBlured[ x + y*stride ]=
					src[ x + y*stride ]=
						(ref*7 + cur + 4)>>3;
				}
			}
		}
		else
		{
			for(y=0; y<8; y++)
			{
				int x;
				for(x=0; x<8; x++)
				{
					int ref= tempBlured[ x + y*stride ];
					int cur= src[ x + y*stride ];
					tempBlured[ x + y*stride ]=
					src[ x + y*stride ]=
						(ref*3 + cur + 2)>>2;
				}
			}
		}
	}
2442
#endif
2443 2444
}

2445
static void RENAME(postProcess)(uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
2446
	QP_STORE_T QPs[], int QPStride, int isColor, struct PPMode *ppMode);
2447

2448 2449
/**
 * Copies a block from src to dst and fixes the blacklevel
2450
 * levelFix == 0 -> dont touch the brighness & contrast
2451
 */
2452
static inline void RENAME(blockCopy)(uint8_t dst[], int dstStride, uint8_t src[], int srcStride,
Michael Niedermayer's avatar
Michael Niedermayer committed
2453
	int levelFix)
2454
{
2455
#ifndef HAVE_MMX
2456
	int i;
2457
#endif
2458 2459
	if(levelFix)
	{
2460 2461
#ifdef HAVE_MMX
					asm volatile(
Michael Niedermayer's avatar
Michael Niedermayer committed
2462 2463
						"leal (%0,%2), %%eax	\n\t"
						"leal (%1,%3), %%ebx	\n\t"
2464 2465
						"movq "MANGLE(packedYOffset)", %%mm2\n\t"
						"movq "MANGLE(packedYScale)", %%mm3\n\t"
2466
						"pxor %%mm4, %%mm4	\n\t"
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
#ifdef HAVE_MMX2
#define SCALED_CPY(src1, src2, dst1, dst2)					\
						"movq " #src1 ", %%mm0	\n\t"\
						"movq " #src1 ", %%mm5	\n\t"\
						"movq " #src2 ", %%mm1	\n\t"\
						"movq " #src2 ", %%mm6	\n\t"\
						"punpcklbw %%mm0, %%mm0 \n\t"\
						"punpckhbw %%mm5, %%mm5 \n\t"\
						"punpcklbw %%mm1, %%mm1 \n\t"\
						"punpckhbw %%mm6, %%mm6 \n\t"\
						"pmulhuw %%mm3, %%mm0	\n\t"\
						"pmulhuw %%mm3, %%mm5	\n\t"\
						"pmulhuw %%mm3, %%mm1	\n\t"\
						"pmulhuw %%mm3, %%mm6	\n\t"\
						"psubw %%mm2, %%mm0	\n\t"\
						"psubw %%mm2, %%mm5	\n\t"\
						"psubw %%mm2, %%mm1	\n\t"\
						"psubw %%mm2, %%mm6	\n\t"\
						"packuswb %%mm5, %%mm0	\n\t"\
						"packuswb %%mm6, %%mm1	\n\t"\
						"movq %%mm0, " #dst1 "	\n\t"\
						"movq %%mm1, " #dst2 "	\n\t"\
2489

2490
#else //HAVE_MMX2
Michael Niedermayer's avatar
Michael Niedermayer committed
2491 2492 2493
#define SCALED_CPY(src1, src2, dst1, dst2)					\
						"movq " #src1 ", %%mm0	\n\t"\
						"movq " #src1 ", %%mm5	\n\t"\
2494 2495
						"punpcklbw %%mm4, %%mm0 \n\t"\
						"punpckhbw %%mm4, %%mm5 \n\t"\
2496 2497
						"psubw %%mm2, %%mm0	\n\t"\
						"psubw %%mm2, %%mm5	\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
2498
						"movq " #src2 ", %%mm1	\n\t"\
2499 2500
						"psllw $6, %%mm0	\n\t"\
						"psllw $6, %%mm5	\n\t"\
2501
						"pmulhw %%mm3, %%mm0	\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
2502
						"movq " #src2 ", %%mm6	\n\t"\
2503 2504
						"pmulhw %%mm3, %%mm5	\n\t"\
						"punpcklbw %%mm4, %%mm1 \n\t"\
2505
						"punpckhbw %%mm4, %%mm6 \n\t"\
2506
						"psubw %%mm2, %%mm1	\n\t"\
2507
						"psubw %%mm2, %%mm6	\n\t"\
2508
						"psllw $6, %%mm1	\n\t"\
2509
						"psllw $6, %%mm6	\n\t"\
2510
						"pmulhw %%mm3, %%mm1	\n\t"\
2511 2512 2513
						"pmulhw %%mm3, %%mm6	\n\t"\
						"packuswb %%mm5, %%mm0	\n\t"\
						"packuswb %%mm6, %%mm1	\n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
2514 2515 2516
						"movq %%mm0, " #dst1 "	\n\t"\
						"movq %%mm1, " #dst2 "	\n\t"\

2517 2518
#endif //!HAVE_MMX2

Michael Niedermayer's avatar
Michael Niedermayer committed
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529
SCALED_CPY((%0)       , (%0, %2)      , (%1)       , (%1, %3))
SCALED_CPY((%0, %2, 2), (%%eax, %2, 2), (%1, %3, 2), (%%ebx, %3, 2))
SCALED_CPY((%0, %2, 4), (%%eax, %2, 4), (%1, %3, 4), (%%ebx, %3, 4))
						"leal (%%eax,%2,4), %%eax	\n\t"
						"leal (%%ebx,%3,4), %%ebx	\n\t"
SCALED_CPY((%%eax, %2), (%%eax, %2, 2), (%%ebx, %3), (%%ebx, %3, 2))


						: : "r"(src),
						"r"(dst),
						"r" (srcStride),
2530
						"r" (dstStride)
2531 2532 2533
						: "%eax", "%ebx"
					);
#else
Michael Niedermayer's avatar
Michael Niedermayer committed
2534
				for(i=0; i<8; i++)
2535 2536 2537 2538 2539 2540 2541 2542
					memcpy(	&(dst[dstStride*i]),
						&(src[srcStride*i]), BLOCK_SIZE);
#endif
	}
	else
	{
#ifdef HAVE_MMX
					asm volatile(
Michael Niedermayer's avatar
Michael Niedermayer committed
2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558
						"leal (%0,%2), %%eax	\n\t"
						"leal (%1,%3), %%ebx	\n\t"

#define SIMPLE_CPY(src1, src2, dst1, dst2)				\
						"movq " #src1 ", %%mm0	\n\t"\
						"movq " #src2 ", %%mm1	\n\t"\
						"movq %%mm0, " #dst1 "	\n\t"\
						"movq %%mm1, " #dst2 "	\n\t"\

SIMPLE_CPY((%0)       , (%0, %2)      , (%1)       , (%1, %3))
SIMPLE_CPY((%0, %2, 2), (%%eax, %2, 2), (%1, %3, 2), (%%ebx, %3, 2))
SIMPLE_CPY((%0, %2, 4), (%%eax, %2, 4), (%1, %3, 4), (%%ebx, %3, 4))
						"leal (%%eax,%2,4), %%eax	\n\t"
						"leal (%%ebx,%3,4), %%ebx	\n\t"
SIMPLE_CPY((%%eax, %2), (%%eax, %2, 2), (%%ebx, %3), (%%ebx, %3, 2))

2559 2560 2561
						: : "r" (src),
						"r" (dst),
						"r" (srcStride),
Michael Niedermayer's avatar
Michael Niedermayer committed
2562
						"r" (dstStride)
2563 2564 2565
						: "%eax", "%ebx"
					);
#else
Michael Niedermayer's avatar
Michael Niedermayer committed
2566
				for(i=0; i<8; i++)
2567 2568 2569
					memcpy(	&(dst[dstStride*i]),
						&(src[srcStride*i]), BLOCK_SIZE);
#endif
2570
	}
2571 2572
}

Michael Niedermayer's avatar
Michael Niedermayer committed
2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597
/**
 * Duplicates the given 8 src pixels ? times upward
 */
static inline void RENAME(duplicate)(uint8_t src[], int stride)
{
#ifdef HAVE_MMX
	asm volatile(
		"movq (%0), %%mm0		\n\t"
		"addl %1, %0			\n\t"
		"movq %%mm0, (%0)		\n\t"
		"movq %%mm0, (%0, %1)		\n\t"
		"movq %%mm0, (%0, %1, 2)	\n\t"
		: "+r" (src)
		: "r" (-stride)
	);
#else
	int i;
	uint8_t *p=src;
	for(i=0; i<3; i++)
	{
		p-= stride;
		memcpy(p, src, 8);
	}
#endif
}
2598 2599 2600 2601

/**
 * Filters array of bytes (Y or U or V values)
 */
2602
static void RENAME(postProcess)(uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
2603
	QP_STORE_T QPs[], int QPStride, int isColor, struct PPMode *ppMode)
2604
{
2605
	int x,y;
2606 2607 2608
#ifdef COMPILE_TIME_MODE
	const int mode= COMPILE_TIME_MODE;
#else
2609
	const int mode= isColor ? ppMode->chromMode : ppMode->lumMode;
2610
#endif
2611 2612 2613 2614
	/* we need 64bit here otherwise well going to have a problem
	   after watching a black picture for 5 hours*/
	static uint64_t *yHistogram= NULL;
	int black=0, white=255; // blackest black and whitest white in the picture
Michael Niedermayer's avatar
Michael Niedermayer committed
2615
	int QPCorrecture= 256*256;
2616

2617 2618 2619 2620
	/* Temporary buffers for handling the last row(s) */
	static uint8_t *tempDst= NULL;
	static uint8_t *tempSrc= NULL;

2621 2622 2623 2624
	/* Temporary buffers for handling the last block */
	static uint8_t *tempDstBlock= NULL;
	static uint8_t *tempSrcBlock= NULL;

2625 2626
	/* Temporal noise reducing buffers */
	static uint8_t *tempBlured[3]= {NULL,NULL,NULL};
Michael Niedermayer's avatar
Michael Niedermayer committed
2627
	static uint32_t *tempBluredPast[3]= {NULL,NULL,NULL};
2628

Michael Niedermayer's avatar
Michael Niedermayer committed
2629 2630
	int copyAhead;

2631
#ifdef PP_FUNNY_STRIDE
2632 2633
	uint8_t *dstBlockPtrBackup;
	uint8_t *srcBlockPtrBackup;
2634
#endif
2635

2636 2637 2638
#ifdef MORE_TIMING
	long long T0, T1, diffTime=0;
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
2639
#ifdef TIMING
2640
	long long memcpyTime=0, vertTime=0, horizTime=0, sumTime;
2641 2642
	sumTime= rdtsc();
#endif
2643 2644 2645
	dcOffset= ppMode->maxDcDiff;
	dcThreshold= ppMode->maxDcDiff*2 + 1;

Michael Niedermayer's avatar
Michael Niedermayer committed
2646 2647 2648 2649
#ifdef HAVE_MMX
	maxTmpNoise[0]= ppMode->maxTmpNoise[0];
	maxTmpNoise[1]= ppMode->maxTmpNoise[1];
	maxTmpNoise[2]= ppMode->maxTmpNoise[2];
2650 2651 2652 2653 2654 2655
	
	mmxDCOffset= 0x7F - dcOffset;
	mmxDCThreshold= 0x7F - dcThreshold;

	mmxDCOffset*= 0x0101010101010101LL;
	mmxDCThreshold*= 0x0101010101010101LL;
Michael Niedermayer's avatar
Michael Niedermayer committed
2656
#endif
2657

Michael Niedermayer's avatar
Michael Niedermayer committed
2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669
	if(mode & CUBIC_IPOL_DEINT_FILTER) copyAhead=16;
	else if(mode & LINEAR_BLEND_DEINT_FILTER) copyAhead=14;
	else if(   (mode & V_DEBLOCK)
		|| (mode & LINEAR_IPOL_DEINT_FILTER)
		|| (mode & MEDIAN_DEINT_FILTER)) copyAhead=13;
	else if(mode & V_X1_FILTER) copyAhead=11;
	else if(mode & V_RK1_FILTER) copyAhead=10;
	else if(mode & DERING) copyAhead=9;
	else copyAhead=8;

	copyAhead-= 8;

2670 2671 2672 2673
	if(tempDst==NULL)
	{
		tempDst= (uint8_t*)memalign(8, 1024*24);
		tempSrc= (uint8_t*)memalign(8, 1024*24);
2674 2675
		tempDstBlock= (uint8_t*)memalign(8, 1024*24);
		tempSrcBlock= (uint8_t*)memalign(8, 1024*24);
2676 2677
	}

2678 2679 2680 2681 2682 2683
	if(tempBlured[isColor]==NULL && (mode & TEMP_NOISE_FILTER))
	{
//		printf("%d %d %d\n", isColor, dstStride, height);
		//FIXME works only as long as the size doesnt increase
		//Note:the +17*1024 is just there so i dont have to worry about r/w over te end
		tempBlured[isColor]= (uint8_t*)memalign(8, dstStride*((height+7)&(~7)) + 17*1024);
Michael Niedermayer's avatar
Michael Niedermayer committed
2684
		tempBluredPast[isColor]= (uint32_t*)memalign(8, 256*((height+7)&(~7))/2 + 17*1024);
2685 2686

		memset(tempBlured[isColor], 0, dstStride*((height+7)&(~7)) + 17*1024);
Michael Niedermayer's avatar
Michael Niedermayer committed
2687
		memset(tempBluredPast[isColor], 0, 256*((height+7)&(~7))/2 + 17*1024);
2688 2689
	}

2690 2691
	if(!yHistogram)
	{
2692 2693 2694
		int i;
		yHistogram= (uint64_t*)malloc(8*256);
		for(i=0; i<256; i++) yHistogram[i]= width*height/64*15/256;
2695 2696 2697

		if(mode & FULL_Y_RANGE)
		{
2698 2699
			ppMode->maxAllowedY=255;
			ppMode->minAllowedY=0;
2700
		}
2701 2702 2703 2704 2705
	}

	if(!isColor)
	{
		uint64_t sum= 0;
2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716
		int i;
		static int framenum= -1;
		uint64_t maxClipped;
		uint64_t clipped;
		double scale;

		framenum++;
		if(framenum == 1) yHistogram[0]= width*height/64*15/256;

		for(i=0; i<256; i++)
		{
2717
			sum+= yHistogram[i];
2718 2719 2720 2721 2722 2723
//			printf("%d ", yHistogram[i]);
		}
//		printf("\n\n");

		/* we allways get a completly black picture first */
		maxClipped= (uint64_t)(sum * maxClippedThreshold);
2724

2725
		clipped= sum;
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738
		for(black=255; black>0; black--)
		{
			if(clipped < maxClipped) break;
			clipped-= yHistogram[black];
		}

		clipped= sum;
		for(white=0; white<256; white++)
		{
			if(clipped < maxClipped) break;
			clipped-= yHistogram[white];
		}

2739
		scale= (double)(ppMode->maxAllowedY - ppMode->minAllowedY) / (double)(white-black);
2740 2741 2742

#ifdef HAVE_MMX2
		packedYScale= (uint16_t)(scale*256.0 + 0.5);
2743
		packedYOffset= (((black*packedYScale)>>8) - ppMode->minAllowedY) & 0xFFFF;
2744 2745
#else
		packedYScale= (uint16_t)(scale*1024.0 + 0.5);
2746
		packedYOffset= (black - ppMode->minAllowedY) & 0xFFFF;
2747 2748
#endif

2749 2750 2751 2752 2753
		packedYOffset|= packedYOffset<<32;
		packedYOffset|= packedYOffset<<16;

		packedYScale|= packedYScale<<32;
		packedYScale|= packedYScale<<16;
Michael Niedermayer's avatar
Michael Niedermayer committed
2754 2755 2756
		
		if(mode & LEVEL_FIX)	QPCorrecture= (int)(scale*256*256 + 0.5);
		else			QPCorrecture= 256*256;
2757 2758 2759 2760 2761
	}
	else
	{
		packedYScale= 0x0100010001000100LL;
		packedYOffset= 0;
Michael Niedermayer's avatar
Michael Niedermayer committed
2762
		QPCorrecture= 256*256;
2763 2764
	}

2765
	/* copy & deinterlace first row of blocks */
Michael Niedermayer's avatar
Michael Niedermayer committed
2766 2767 2768
	y=-BLOCK_SIZE;
	{
		uint8_t *srcBlock= &(src[y*srcStride]);
Michael Niedermayer's avatar
Michael Niedermayer committed
2769
		uint8_t *dstBlock= tempDst + dstStride;
Michael Niedermayer's avatar
Michael Niedermayer committed
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788

		// From this point on it is guranteed that we can read and write 16 lines downward
		// finish 1 block before the next otherwise well might have a problem
		// with the L1 Cache of the P4 ... or only a few blocks at a time or soemthing
		for(x=0; x<width; x+=BLOCK_SIZE)
		{

#ifdef HAVE_MMX2
/*
			prefetchnta(srcBlock + (((x>>2)&6) + 5)*srcStride + 32);
			prefetchnta(srcBlock + (((x>>2)&6) + 6)*srcStride + 32);
			prefetcht0(dstBlock + (((x>>2)&6) + 5)*dstStride + 32);
			prefetcht0(dstBlock + (((x>>2)&6) + 6)*dstStride + 32);
*/

			asm(
				"movl %4, %%eax			\n\t"
				"shrl $2, %%eax			\n\t"
				"andl $6, %%eax			\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
2789
				"addl %5, %%eax			\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
2790 2791 2792 2793 2794 2795 2796 2797 2798 2799
				"movl %%eax, %%ebx		\n\t"
				"imul %1, %%eax			\n\t"
				"imul %3, %%ebx			\n\t"
				"prefetchnta 32(%%eax, %0)	\n\t"
				"prefetcht0 32(%%ebx, %2)	\n\t"
				"addl %1, %%eax			\n\t"
				"addl %3, %%ebx			\n\t"
				"prefetchnta 32(%%eax, %0)	\n\t"
				"prefetcht0 32(%%ebx, %2)	\n\t"
			:: "r" (srcBlock), "r" (srcStride), "r" (dstBlock), "r" (dstStride),
Michael Niedermayer's avatar
Michael Niedermayer committed
2800
			"m" (x), "m" (copyAhead)
Michael Niedermayer's avatar
Michael Niedermayer committed
2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812
			: "%eax", "%ebx"
			);

#elif defined(HAVE_3DNOW)
//FIXME check if this is faster on an 3dnow chip or if its faster without the prefetch or ...
/*			prefetch(srcBlock + (((x>>3)&3) + 5)*srcStride + 32);
			prefetch(srcBlock + (((x>>3)&3) + 9)*srcStride + 32);
			prefetchw(dstBlock + (((x>>3)&3) + 5)*dstStride + 32);
			prefetchw(dstBlock + (((x>>3)&3) + 9)*dstStride + 32);
*/
#endif

Michael Niedermayer's avatar
Michael Niedermayer committed
2813 2814 2815 2816
			RENAME(blockCopy)(dstBlock + dstStride*8, dstStride,
				srcBlock + srcStride*8, srcStride, mode & LEVEL_FIX);

			RENAME(duplicate)(dstBlock + dstStride*8, dstStride);
Michael Niedermayer's avatar
Michael Niedermayer committed
2817 2818

			if(mode & LINEAR_IPOL_DEINT_FILTER)
2819
				RENAME(deInterlaceInterpolateLinear)(dstBlock, dstStride);
Michael Niedermayer's avatar
Michael Niedermayer committed
2820
			else if(mode & LINEAR_BLEND_DEINT_FILTER)
2821
				RENAME(deInterlaceBlendLinear)(dstBlock, dstStride);
Michael Niedermayer's avatar
Michael Niedermayer committed
2822
			else if(mode & MEDIAN_DEINT_FILTER)
2823
				RENAME(deInterlaceMedian)(dstBlock, dstStride);
Michael Niedermayer's avatar
Michael Niedermayer committed
2824
			else if(mode & CUBIC_IPOL_DEINT_FILTER)
2825
				RENAME(deInterlaceInterpolateCubic)(dstBlock, dstStride);
Michael Niedermayer's avatar
Michael Niedermayer committed
2826
/*			else if(mode & CUBIC_BLEND_DEINT_FILTER)
2827
				RENAME(deInterlaceBlendCubic)(dstBlock, dstStride);
Michael Niedermayer's avatar
Michael Niedermayer committed
2828 2829 2830 2831
*/
			dstBlock+=8;
			srcBlock+=8;
		}
Michael Niedermayer's avatar
Michael Niedermayer committed
2832
		memcpy(dst, tempDst + 9*dstStride, copyAhead*dstStride );
Michael Niedermayer's avatar
Michael Niedermayer committed
2833
	}
2834

2835
	for(y=0; y<height; y+=BLOCK_SIZE)
2836 2837 2838 2839
	{
		//1% speedup if these are here instead of the inner loop
		uint8_t *srcBlock= &(src[y*srcStride]);
		uint8_t *dstBlock= &(dst[y*dstStride]);
2840 2841 2842 2843
#ifdef HAVE_MMX
		uint8_t *tempBlock1= tempBlocks;
		uint8_t *tempBlock2= tempBlocks + 8;
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
2844 2845
#ifdef ARCH_X86
		int *QPptr= isColor ? &QPs[(y>>3)*QPStride] :&QPs[(y>>4)*QPStride];
Michael Niedermayer's avatar
Michael Niedermayer committed
2846 2847
		int QPDelta= isColor ? (-1) : 1<<31;
		int QPFrac= 1<<30;
Michael Niedermayer's avatar
Michael Niedermayer committed
2848
#endif
2849
		int QP=0;
Michael Niedermayer's avatar
Michael Niedermayer committed
2850 2851
		/* can we mess with a 8x16 block from srcBlock/dstBlock downwards and 1 line upwards
		   if not than use a temporary buffer */
2852 2853
		if(y+15 >= height)
		{
2854
			int i;
Michael Niedermayer's avatar
Michael Niedermayer committed
2855
			/* copy from line (copyAhead) to (copyAhead+7) of src, these will be copied with
2856
			   blockcopy to dst later */
Michael Niedermayer's avatar
Michael Niedermayer committed
2857 2858
			memcpy(tempSrc + srcStride*copyAhead, srcBlock + srcStride*copyAhead,
				srcStride*MAX(height-y-copyAhead, 0) );
2859

Michael Niedermayer's avatar
Michael Niedermayer committed
2860 2861
			/* duplicate last line of src to fill the void upto line (copyAhead+7) */
			for(i=MAX(height-y, 8); i<copyAhead+8; i++)
2862
				memcpy(tempSrc + srcStride*i, src + srcStride*(height-1), srcStride);
2863

Michael Niedermayer's avatar
Michael Niedermayer committed
2864 2865
			/* copy up to (copyAhead+1) lines of dst (line -1 to (copyAhead-1))*/
			memcpy(tempDst, dstBlock - dstStride, dstStride*MIN(height-y+1, copyAhead+1) );
2866

Michael Niedermayer's avatar
Michael Niedermayer committed
2867 2868
			/* duplicate last line of dst to fill the void upto line (copyAhead) */
			for(i=height-y+1; i<=copyAhead; i++)
2869 2870
				memcpy(tempDst + dstStride*i, dst + dstStride*(height-1), dstStride);

Michael Niedermayer's avatar
Michael Niedermayer committed
2871
			dstBlock= tempDst + dstStride;
2872 2873
			srcBlock= tempSrc;
		}
2874

2875
		// From this point on it is guranteed that we can read and write 16 lines downward
2876 2877
		// finish 1 block before the next otherwise well might have a problem
		// with the L1 Cache of the P4 ... or only a few blocks at a time or soemthing
2878
		for(x=0; x<width; x+=BLOCK_SIZE)
2879
		{
2880
			const int stride= dstStride;
2881
#ifdef HAVE_MMX
2882
			uint8_t *tmpXchg;
2883
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
2884
#ifdef ARCH_X86
2885
			QP= *QPptr;
Michael Niedermayer's avatar
Michael Niedermayer committed
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895
			asm volatile(
				"addl %2, %1		\n\t"
				"sbbl %%eax, %%eax	\n\t"
				"shll $2, %%eax		\n\t"
				"subl %%eax, %0		\n\t"
				: "+r" (QPptr), "+m" (QPFrac)
				: "r" (QPDelta)
				: "%eax"
			);
#else
2896
			QP= isColor ?
Michael Niedermayer's avatar
Michael Niedermayer committed
2897 2898 2899 2900
                                QPs[(y>>3)*QPStride + (x>>3)]:
                                QPs[(y>>4)*QPStride + (x>>4)];
#endif
			if(!isColor)
Michael Niedermayer's avatar
Michael Niedermayer committed
2901
			{
Michael Niedermayer's avatar
Michael Niedermayer committed
2902
				QP= (QP* QPCorrecture + 256*128)>>16;
2903
				yHistogram[ srcBlock[srcStride*12 + 4] ]++;
Michael Niedermayer's avatar
Michael Niedermayer committed
2904
			}
2905
#ifdef HAVE_MMX
2906 2907 2908 2909 2910
			asm volatile(
				"movd %0, %%mm7					\n\t"
				"packuswb %%mm7, %%mm7				\n\t" // 0, 0, 0, QP, 0, 0, 0, QP
				"packuswb %%mm7, %%mm7				\n\t" // 0,QP, 0, QP, 0,QP, 0, QP
				"packuswb %%mm7, %%mm7				\n\t" // QP,..., QP
2911
				"movq %%mm7, "MANGLE(pQPb)"			\n\t"
2912 2913
				: : "r" (QP)
			);
2914 2915
#endif

Michael Niedermayer's avatar
Michael Niedermayer committed
2916
#ifdef MORE_TIMING
2917
			T0= rdtsc();
2918 2919
#endif

2920
#ifdef HAVE_MMX2
Michael Niedermayer's avatar
Michael Niedermayer committed
2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931
/*
			prefetchnta(srcBlock + (((x>>2)&6) + 5)*srcStride + 32);
			prefetchnta(srcBlock + (((x>>2)&6) + 6)*srcStride + 32);
			prefetcht0(dstBlock + (((x>>2)&6) + 5)*dstStride + 32);
			prefetcht0(dstBlock + (((x>>2)&6) + 6)*dstStride + 32);
*/

			asm(
				"movl %4, %%eax			\n\t"
				"shrl $2, %%eax			\n\t"
				"andl $6, %%eax			\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
2932
				"addl %5, %%eax			\n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
2933 2934 2935 2936 2937 2938 2939 2940 2941 2942
				"movl %%eax, %%ebx		\n\t"
				"imul %1, %%eax			\n\t"
				"imul %3, %%ebx			\n\t"
				"prefetchnta 32(%%eax, %0)	\n\t"
				"prefetcht0 32(%%ebx, %2)	\n\t"
				"addl %1, %%eax			\n\t"
				"addl %3, %%ebx			\n\t"
				"prefetchnta 32(%%eax, %0)	\n\t"
				"prefetcht0 32(%%ebx, %2)	\n\t"
			:: "r" (srcBlock), "r" (srcStride), "r" (dstBlock), "r" (dstStride),
Michael Niedermayer's avatar
Michael Niedermayer committed
2943
			"m" (x), "m" (copyAhead)
Michael Niedermayer's avatar
Michael Niedermayer committed
2944 2945 2946
			: "%eax", "%ebx"
			);

2947 2948
#elif defined(HAVE_3DNOW)
//FIXME check if this is faster on an 3dnow chip or if its faster without the prefetch or ...
2949 2950 2951 2952
/*			prefetch(srcBlock + (((x>>3)&3) + 5)*srcStride + 32);
			prefetch(srcBlock + (((x>>3)&3) + 9)*srcStride + 32);
			prefetchw(dstBlock + (((x>>3)&3) + 5)*dstStride + 32);
			prefetchw(dstBlock + (((x>>3)&3) + 9)*dstStride + 32);
2953
*/
2954
#endif
2955

2956
#ifdef PP_FUNNY_STRIDE
2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
			//can we mess with a 8x16 block, if not use a temp buffer, yes again
			if(x+7 >= width)
			{
				int i;
				dstBlockPtrBackup= dstBlock;
				srcBlockPtrBackup= srcBlock;

				for(i=0;i<BLOCK_SIZE*2; i++)
				{
					memcpy(tempSrcBlock+i*srcStride, srcBlock+i*srcStride, width-x);
					memcpy(tempDstBlock+i*dstStride, dstBlock+i*dstStride, width-x);
				}

				dstBlock= tempDstBlock;
				srcBlock= tempSrcBlock;
			}
2973
#endif
2974

2975
			RENAME(blockCopy)(dstBlock + dstStride*copyAhead, dstStride,
Michael Niedermayer's avatar
Michael Niedermayer committed
2976
				srcBlock + srcStride*copyAhead, srcStride, mode & LEVEL_FIX);
2977 2978

			if(mode & LINEAR_IPOL_DEINT_FILTER)
2979
				RENAME(deInterlaceInterpolateLinear)(dstBlock, dstStride);
2980
			else if(mode & LINEAR_BLEND_DEINT_FILTER)
2981
				RENAME(deInterlaceBlendLinear)(dstBlock, dstStride);
2982
			else if(mode & MEDIAN_DEINT_FILTER)
2983
				RENAME(deInterlaceMedian)(dstBlock, dstStride);
2984
			else if(mode & CUBIC_IPOL_DEINT_FILTER)
2985
				RENAME(deInterlaceInterpolateCubic)(dstBlock, dstStride);
2986
/*			else if(mode & CUBIC_BLEND_DEINT_FILTER)
2987
				RENAME(deInterlaceBlendCubic)(dstBlock, dstStride);
2988
*/
2989

2990 2991 2992
			/* only deblock if we have 2 blocks */
			if(y + 8 < height)
			{
Michael Niedermayer's avatar
Michael Niedermayer committed
2993
#ifdef MORE_TIMING
2994 2995 2996 2997
				T1= rdtsc();
				memcpyTime+= T1-T0;
				T0=T1;
#endif
2998
				if(mode & V_RK1_FILTER)
2999
					RENAME(vertRK1Filter)(dstBlock, stride, QP);
3000
				else if(mode & V_X1_FILTER)
3001
					RENAME(vertX1Filter)(dstBlock, stride, QP);
3002
				else if(mode & V_DEBLOCK)
3003
				{
3004
					if( RENAME(isVertDC)(dstBlock, stride))
3005
					{
3006 3007
						if(RENAME(isVertMinMaxOk)(dstBlock, stride, QP))
							RENAME(doVertLowPass)(dstBlock, stride, QP);
3008
					}
3009
					else
3010
						RENAME(doVertDefFilter)(dstBlock, stride, QP);
3011
				}
Michael Niedermayer's avatar
Michael Niedermayer committed
3012
#ifdef MORE_TIMING
3013 3014 3015 3016 3017
				T1= rdtsc();
				vertTime+= T1-T0;
				T0=T1;
#endif
			}
Michael Niedermayer's avatar
Michael Niedermayer committed
3018

3019
#ifdef HAVE_MMX
3020
			RENAME(transpose1)(tempBlock1, tempBlock2, dstBlock, dstStride);
3021
#endif
3022
			/* check if we have a previous block to deblock it with dstBlock */
3023
			if(x - 8 >= 0)
3024
			{
Michael Niedermayer's avatar
Michael Niedermayer committed
3025
#ifdef MORE_TIMING
3026 3027
				T0= rdtsc();
#endif
3028 3029
#ifdef HAVE_MMX
				if(mode & H_RK1_FILTER)
3030
					RENAME(vertRK1Filter)(tempBlock1, 16, QP);
3031
				else if(mode & H_X1_FILTER)
3032
					RENAME(vertX1Filter)(tempBlock1, 16, QP);
3033 3034
				else if(mode & H_DEBLOCK)
				{
3035
					if( RENAME(isVertDC)(tempBlock1, 16) )
3036
					{
3037 3038
						if(RENAME(isVertMinMaxOk)(tempBlock1, 16, QP))
							RENAME(doVertLowPass)(tempBlock1, 16, QP);
3039 3040
					}
					else
3041
						RENAME(doVertDefFilter)(tempBlock1, 16, QP);
3042 3043
				}

3044
				RENAME(transpose2)(dstBlock-4, dstStride, tempBlock1 + 4*16);
3045 3046

#else
3047 3048 3049
				if(mode & H_X1_FILTER)
					horizX1Filter(dstBlock-4, stride, QP);
				else if(mode & H_DEBLOCK)
3050
				{
3051
					if( isHorizDC(dstBlock-4, stride))
3052
					{
3053 3054
						if(isHorizMinMaxOk(dstBlock-4, stride, QP))
							doHorizLowPass(dstBlock-4, stride, QP);
3055
					}
3056
					else
3057
						doHorizDefFilter(dstBlock-4, stride, QP);
3058
				}
3059
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
3060
#ifdef MORE_TIMING
3061 3062 3063 3064
				T1= rdtsc();
				horizTime+= T1-T0;
				T0=T1;
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
3065 3066 3067
				if(mode & DERING)
				{
				//FIXME filter first line
3068
					if(y>0) RENAME(dering)(dstBlock - stride - 8, stride, QP);
Michael Niedermayer's avatar
Michael Niedermayer committed
3069 3070
				}

3071 3072
				if(mode & TEMP_NOISE_FILTER)
				{
3073
					RENAME(tempNoiseReducer)(dstBlock-8, stride,
3074
						tempBlured[isColor] + y*dstStride + x,
Michael Niedermayer's avatar
Michael Niedermayer committed
3075
						tempBluredPast[isColor] + (y>>3)*256 + (x>>3),
3076 3077 3078
						ppMode->maxTmpNoise);
				}
			}
3079

3080
#ifdef PP_FUNNY_STRIDE
3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092
			/* did we use a tmp-block buffer */
			if(x+7 >= width)
			{
				int i;
				dstBlock= dstBlockPtrBackup;
				srcBlock= srcBlockPtrBackup;

				for(i=0;i<BLOCK_SIZE*2; i++)
				{
					memcpy(dstBlock+i*dstStride, tempDstBlock+i*dstStride, width-x);
				}
			}
3093
#endif
3094

3095 3096
			dstBlock+=8;
			srcBlock+=8;
3097

3098
#ifdef HAVE_MMX
3099 3100 3101
			tmpXchg= tempBlock1;
			tempBlock1= tempBlock2;
			tempBlock2 = tmpXchg;
3102
#endif
3103 3104
		}

3105 3106
		if(mode & DERING)
		{
3107
				if(y > 0) RENAME(dering)(dstBlock - dstStride - 8, dstStride, QP);
3108 3109 3110 3111
		}

		if((mode & TEMP_NOISE_FILTER))
		{
3112
			RENAME(tempNoiseReducer)(dstBlock-8, dstStride,
3113
				tempBlured[isColor] + y*dstStride + x,
Michael Niedermayer's avatar
Michael Niedermayer committed
3114
				tempBluredPast[isColor] + (y>>3)*256 + (x>>3),
3115 3116 3117
				ppMode->maxTmpNoise);
		}

Michael Niedermayer's avatar
Michael Niedermayer committed
3118
		/* did we use a tmp buffer for the last lines*/
3119
		if(y+15 >= height)
3120 3121
		{
			uint8_t *dstBlock= &(dst[y*dstStride]);
Michael Niedermayer's avatar
Michael Niedermayer committed
3122
			memcpy(dstBlock, tempDst + dstStride, dstStride*(height-y) );
3123
		}
3124 3125 3126
/*
		for(x=0; x<width; x+=32)
		{
Michael Niedermayer's avatar
Michael Niedermayer committed
3127
			volatile int i;
3128 3129
			i+=	+ dstBlock[x + 7*dstStride] + dstBlock[x + 8*dstStride]
				+ dstBlock[x + 9*dstStride] + dstBlock[x +10*dstStride]
Michael Niedermayer's avatar
Michael Niedermayer committed
3130 3131 3132 3133 3134
				+ dstBlock[x +11*dstStride] + dstBlock[x +12*dstStride];
//				+ dstBlock[x +13*dstStride]
//				+ dstBlock[x +14*dstStride] + dstBlock[x +15*dstStride];
		}*/
	}
3135 3136 3137
#ifdef HAVE_3DNOW
	asm volatile("femms");
#elif defined (HAVE_MMX)
3138 3139 3140
	asm volatile("emms");
#endif

Michael Niedermayer's avatar
Michael Niedermayer committed
3141
#ifdef TIMING
3142 3143 3144 3145
	// FIXME diff is mostly the time spent for rdtsc (should subtract that but ...)
	sumTime= rdtsc() - sumTime;
	if(!isColor)
		printf("cpy:%4dk, vert:%4dk, horiz:%4dk, sum:%4dk, diff:%4dk, color: %d/%d    \r",
3146 3147
			(int)(memcpyTime/1000), (int)(vertTime/1000), (int)(horizTime/1000),
			(int)(sumTime/1000), (int)((sumTime-memcpyTime-vertTime-horizTime)/1000)
3148 3149
			, black, white);
#endif
3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176
#ifdef DEBUG_BRIGHTNESS
	if(!isColor)
	{
		int max=1;
		int i;
		for(i=0; i<256; i++)
			if(yHistogram[i] > max) max=yHistogram[i];

		for(i=1; i<256; i++)
		{
			int x;
			int start=yHistogram[i-1]/(max/256+1);
			int end=yHistogram[i]/(max/256+1);
			int inc= end > start ? 1 : -1;
			for(x=start; x!=end+inc; x+=inc)
				dst[ i*dstStride + x]+=128;
		}

		for(i=0; i<100; i+=2)
		{
			dst[ (white)*dstStride + i]+=128;
			dst[ (black)*dstStride + i]+=128;
		}

	}
#endif

3177
}