Reportedly FFmpeg fails to compile on Cygwin with vhook enabled, but FFserver
[libav.git] / libavcodec / libpostproc / postprocess_template.c
CommitLineData
3057fa66 1/*
bdd677ac 2 Copyright (C) 2001-2002 Michael Niedermayer (michaelni@gmx.at)
3057fa66
A
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
b304569a
MN
19/**
20 * @file postprocess_template.c
21 * mmx/mmx2/3dnow postprocess code.
22 */
23
24
053dea12
AJ
25#ifdef ARCH_X86_64
26# define REGa rax
27# define REGc rcx
28# define REGd rdx
29# define REG_a "rax"
30# define REG_c "rcx"
31# define REG_d "rdx"
32# define REG_SP "rsp"
33# define ALIGN_MASK "$0xFFFFFFFFFFFFFFF8"
34#else
35# define REGa eax
36# define REGc ecx
37# define REGd edx
38# define REG_a "eax"
39# define REG_c "ecx"
40# define REG_d "edx"
41# define REG_SP "esp"
42# define ALIGN_MASK "$0xFFFFFFF8"
43#endif
44
45
cc9b0679
MN
46#undef PAVGB
47#undef PMINUB
48#undef PMAXUB
e939e1c3
A
49
50#ifdef HAVE_MMX2
053dea12 51#define REAL_PAVGB(a,b) "pavgb " #a ", " #b " \n\t"
e939e1c3 52#elif defined (HAVE_3DNOW)
4471e59b 53#define REAL_PAVGB(a,b) "pavgusb " #a ", " #b " \n\t"
e939e1c3 54#endif
053dea12 55#define PAVGB(a,b) REAL_PAVGB(a,b)
3057fa66 56
2e212618
MN
57#ifdef HAVE_MMX2
58#define PMINUB(a,b,t) "pminub " #a ", " #b " \n\t"
59#elif defined (HAVE_MMX)
60#define PMINUB(b,a,t) \
61 "movq " #a ", " #t " \n\t"\
62 "psubusb " #b ", " #t " \n\t"\
63 "psubb " #t ", " #a " \n\t"
64#endif
65
66#ifdef HAVE_MMX2
67#define PMAXUB(a,b) "pmaxub " #a ", " #b " \n\t"
68#elif defined (HAVE_MMX)
69#define PMAXUB(a,b) \
70 "psubusb " #a ", " #b " \n\t"\
71 "paddb " #a ", " #b " \n\t"
72#endif
73
3057fa66 74//FIXME? |255-0| = 1 (shouldnt be a problem ...)
9c9e467d 75#ifdef HAVE_MMX
3057fa66 76/**
acced553 77 * Check if the middle 8x8 Block in the given 8x16 block is flat
3057fa66 78 */
cb482d25
MN
79static inline int RENAME(vertClassify)(uint8_t src[], int stride, PPContext *c){
80 int numEq= 0, dcOk;
acced553 81 src+= stride*4; // src points to begin of the 8x8 Block
37da00fc 82asm volatile(
115329f1
DB
83 "movq %0, %%mm7 \n\t"
84 "movq %1, %%mm6 \n\t"
1e79606d
MN
85 : : "m" (c->mmxDcOffset[c->nonBQP]), "m" (c->mmxDcThreshold[c->nonBQP])
86 );
115329f1 87
1e79606d 88asm volatile(
053dea12 89 "lea (%2, %3), %%"REG_a" \n\t"
37da00fc 90// 0 1 2 3 4 5 6 7 8 9
9c9e467d 91// %1 eax eax+%2 eax+2%2 %1+4%2 ecx ecx+%2 ecx+2%2 %1+8%2 ecx+4%2
ec487e5d 92
cb482d25 93 "movq (%2), %%mm0 \n\t"
053dea12 94 "movq (%%"REG_a"), %%mm1 \n\t"
cb482d25
MN
95 "movq %%mm0, %%mm3 \n\t"
96 "movq %%mm0, %%mm4 \n\t"
97 PMAXUB(%%mm1, %%mm4)
98 PMINUB(%%mm1, %%mm3, %%mm5)
3057fa66
A
99 "psubb %%mm1, %%mm0 \n\t" // mm0 = differnece
100 "paddb %%mm7, %%mm0 \n\t"
101 "pcmpgtb %%mm6, %%mm0 \n\t"
102
053dea12 103 "movq (%%"REG_a",%3), %%mm2 \n\t"
cb482d25
MN
104 PMAXUB(%%mm2, %%mm4)
105 PMINUB(%%mm2, %%mm3, %%mm5)
3057fa66
A
106 "psubb %%mm2, %%mm1 \n\t"
107 "paddb %%mm7, %%mm1 \n\t"
108 "pcmpgtb %%mm6, %%mm1 \n\t"
109 "paddb %%mm1, %%mm0 \n\t"
110
053dea12 111 "movq (%%"REG_a", %3, 2), %%mm1 \n\t"
cb482d25
MN
112 PMAXUB(%%mm1, %%mm4)
113 PMINUB(%%mm1, %%mm3, %%mm5)
3057fa66
A
114 "psubb %%mm1, %%mm2 \n\t"
115 "paddb %%mm7, %%mm2 \n\t"
116 "pcmpgtb %%mm6, %%mm2 \n\t"
117 "paddb %%mm2, %%mm0 \n\t"
115329f1 118
053dea12 119 "lea (%%"REG_a", %3, 4), %%"REG_a" \n\t"
3057fa66 120
cb482d25
MN
121 "movq (%2, %3, 4), %%mm2 \n\t"
122 PMAXUB(%%mm2, %%mm4)
123 PMINUB(%%mm2, %%mm3, %%mm5)
3057fa66
A
124 "psubb %%mm2, %%mm1 \n\t"
125 "paddb %%mm7, %%mm1 \n\t"
126 "pcmpgtb %%mm6, %%mm1 \n\t"
127 "paddb %%mm1, %%mm0 \n\t"
128
053dea12 129 "movq (%%"REG_a"), %%mm1 \n\t"
cb482d25
MN
130 PMAXUB(%%mm1, %%mm4)
131 PMINUB(%%mm1, %%mm3, %%mm5)
3057fa66
A
132 "psubb %%mm1, %%mm2 \n\t"
133 "paddb %%mm7, %%mm2 \n\t"
134 "pcmpgtb %%mm6, %%mm2 \n\t"
135 "paddb %%mm2, %%mm0 \n\t"
136
053dea12 137 "movq (%%"REG_a", %3), %%mm2 \n\t"
cb482d25
MN
138 PMAXUB(%%mm2, %%mm4)
139 PMINUB(%%mm2, %%mm3, %%mm5)
3057fa66
A
140 "psubb %%mm2, %%mm1 \n\t"
141 "paddb %%mm7, %%mm1 \n\t"
142 "pcmpgtb %%mm6, %%mm1 \n\t"
143 "paddb %%mm1, %%mm0 \n\t"
144
053dea12 145 "movq (%%"REG_a", %3, 2), %%mm1 \n\t"
cb482d25
MN
146 PMAXUB(%%mm1, %%mm4)
147 PMINUB(%%mm1, %%mm3, %%mm5)
3057fa66
A
148 "psubb %%mm1, %%mm2 \n\t"
149 "paddb %%mm7, %%mm2 \n\t"
150 "pcmpgtb %%mm6, %%mm2 \n\t"
151 "paddb %%mm2, %%mm0 \n\t"
cb482d25 152 "psubusb %%mm3, %%mm4 \n\t"
3057fa66
A
153
154 " \n\t"
cd38e322
MN
155#ifdef HAVE_MMX2
156 "pxor %%mm7, %%mm7 \n\t"
157 "psadbw %%mm7, %%mm0 \n\t"
158#else
3057fa66
A
159 "movq %%mm0, %%mm1 \n\t"
160 "psrlw $8, %%mm0 \n\t"
161 "paddb %%mm1, %%mm0 \n\t"
162 "movq %%mm0, %%mm1 \n\t"
163 "psrlq $16, %%mm0 \n\t"
164 "paddb %%mm1, %%mm0 \n\t"
165 "movq %%mm0, %%mm1 \n\t"
166 "psrlq $32, %%mm0 \n\t"
167 "paddb %%mm1, %%mm0 \n\t"
cd38e322 168#endif
1e79606d 169 "movq %4, %%mm7 \n\t" // QP,..., QP
cb482d25
MN
170 "paddusb %%mm7, %%mm7 \n\t" // 2QP ... 2QP
171 "psubusb %%mm7, %%mm4 \n\t" // Diff <= 2QP -> 0
172 "packssdw %%mm4, %%mm4 \n\t"
3057fa66 173 "movd %%mm0, %0 \n\t"
cb482d25
MN
174 "movd %%mm4, %1 \n\t"
175
176 : "=r" (numEq), "=r" (dcOk)
053dea12
AJ
177 : "r" (src), "r" ((long)stride), "m" (c->pQPb)
178 : "%"REG_a
3057fa66 179 );
cb482d25 180
cd38e322 181 numEq= (-numEq) &0xFF;
cb482d25
MN
182 if(numEq > c->ppMode.flatnessThreshold){
183 if(dcOk) return 0;
184 else return 1;
185 }else{
186 return 2;
187 }
3057fa66 188}
64c968da 189#endif //HAVE_MMX
3057fa66 190
3057fa66 191/**
acced553 192 * Do a vertical low pass filter on the 8x16 block (only write to the 8x8 block in the middle)
a6be8111 193 * using the 9-Tap Filter (1,1,2,2,4,2,2,1,1)/16
3057fa66 194 */
b0ac780a 195#ifndef HAVE_ALTIVEC
9c9e467d 196static inline void RENAME(doVertLowPass)(uint8_t *src, int stride, PPContext *c)
3057fa66 197{
13e00528 198#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
acced553 199 src+= stride*3;
3057fa66 200 asm volatile( //"movv %0 %1 %2\n\t"
9c9e467d
MN
201 "movq %2, %%mm0 \n\t" // QP,..., QP
202 "pxor %%mm4, %%mm4 \n\t"
3057fa66
A
203
204 "movq (%0), %%mm6 \n\t"
205 "movq (%0, %1), %%mm5 \n\t"
206 "movq %%mm5, %%mm1 \n\t"
207 "movq %%mm6, %%mm2 \n\t"
208 "psubusb %%mm6, %%mm5 \n\t"
209 "psubusb %%mm1, %%mm2 \n\t"
210 "por %%mm5, %%mm2 \n\t" // ABS Diff of lines
211 "psubusb %%mm0, %%mm2 \n\t" // diff <= QP -> 0
9c9e467d 212 "pcmpeqb %%mm4, %%mm2 \n\t" // diff <= QP -> FF
3057fa66
A
213
214 "pand %%mm2, %%mm6 \n\t"
215 "pandn %%mm1, %%mm2 \n\t"
216 "por %%mm2, %%mm6 \n\t"// First Line to Filter
217
218 "movq (%0, %1, 8), %%mm5 \n\t"
053dea12
AJ
219 "lea (%0, %1, 4), %%"REG_a" \n\t"
220 "lea (%0, %1, 8), %%"REG_c" \n\t"
221 "sub %1, %%"REG_c" \n\t"
222 "add %1, %0 \n\t" // %0 points to line 1 not 0
3057fa66
A
223 "movq (%0, %1, 8), %%mm7 \n\t"
224 "movq %%mm5, %%mm1 \n\t"
225 "movq %%mm7, %%mm2 \n\t"
226 "psubusb %%mm7, %%mm5 \n\t"
227 "psubusb %%mm1, %%mm2 \n\t"
228 "por %%mm5, %%mm2 \n\t" // ABS Diff of lines
229 "psubusb %%mm0, %%mm2 \n\t" // diff <= QP -> 0
9c9e467d 230 "pcmpeqb %%mm4, %%mm2 \n\t" // diff <= QP -> FF
3057fa66
A
231
232 "pand %%mm2, %%mm7 \n\t"
233 "pandn %%mm1, %%mm2 \n\t"
234 "por %%mm2, %%mm7 \n\t" // First Line to Filter
235
236
237 // 1 2 3 4 5 6 7 8
9c9e467d 238 // %0 %0+%1 %0+2%1 eax %0+4%1 eax+2%1 ecx eax+4%1
3057fa66
A
239 // 6 4 2 2 1 1
240 // 6 4 4 2
241 // 6 8 2
acced553 242
3057fa66
A
243 "movq (%0, %1), %%mm0 \n\t" // 1
244 "movq %%mm0, %%mm1 \n\t" // 1
13e00528
A
245 PAVGB(%%mm6, %%mm0) //1 1 /2
246 PAVGB(%%mm6, %%mm0) //3 1 /4
3057fa66
A
247
248 "movq (%0, %1, 4), %%mm2 \n\t" // 1
249 "movq %%mm2, %%mm5 \n\t" // 1
053dea12 250 PAVGB((%%REGa), %%mm2) // 11 /2
13e00528 251 PAVGB((%0, %1, 2), %%mm2) // 211 /4
3057fa66
A
252 "movq %%mm2, %%mm3 \n\t" // 211 /4
253 "movq (%0), %%mm4 \n\t" // 1
13e00528
A
254 PAVGB(%%mm4, %%mm3) // 4 211 /8
255 PAVGB(%%mm0, %%mm3) //642211 /16
3057fa66
A
256 "movq %%mm3, (%0) \n\t" // X
257 // mm1=2 mm2=3(211) mm4=1 mm5=5 mm6=0 mm7=9
258 "movq %%mm1, %%mm0 \n\t" // 1
13e00528 259 PAVGB(%%mm6, %%mm0) //1 1 /2
3057fa66 260 "movq %%mm4, %%mm3 \n\t" // 1
13e00528 261 PAVGB((%0,%1,2), %%mm3) // 1 1 /2
053dea12
AJ
262 PAVGB((%%REGa,%1,2), %%mm5) // 11 /2
263 PAVGB((%%REGa), %%mm5) // 211 /4
13e00528
A
264 PAVGB(%%mm5, %%mm3) // 2 2211 /8
265 PAVGB(%%mm0, %%mm3) //4242211 /16
3057fa66
A
266 "movq %%mm3, (%0,%1) \n\t" // X
267 // mm1=2 mm2=3(211) mm4=1 mm5=4(211) mm6=0 mm7=9
13e00528 268 PAVGB(%%mm4, %%mm6) //11 /2
053dea12
AJ
269 "movq (%%"REG_c"), %%mm0 \n\t" // 1
270 PAVGB((%%REGa, %1, 2), %%mm0) // 11/2
3057fa66 271 "movq %%mm0, %%mm3 \n\t" // 11/2
13e00528
A
272 PAVGB(%%mm1, %%mm0) // 2 11/4
273 PAVGB(%%mm6, %%mm0) //222 11/8
274 PAVGB(%%mm2, %%mm0) //22242211/16
3057fa66
A
275 "movq (%0, %1, 2), %%mm2 \n\t" // 1
276 "movq %%mm0, (%0, %1, 2) \n\t" // X
277 // mm1=2 mm2=3 mm3=6(11) mm4=1 mm5=4(211) mm6=0(11) mm7=9
053dea12
AJ
278 "movq (%%"REG_a", %1, 4), %%mm0 \n\t" // 1
279 PAVGB((%%REGc), %%mm0) // 11 /2
13e00528
A
280 PAVGB(%%mm0, %%mm6) //11 11 /4
281 PAVGB(%%mm1, %%mm4) // 11 /2
282 PAVGB(%%mm2, %%mm1) // 11 /2
283 PAVGB(%%mm1, %%mm6) //1122 11 /8
284 PAVGB(%%mm5, %%mm6) //112242211 /16
053dea12
AJ
285 "movq (%%"REG_a"), %%mm5 \n\t" // 1
286 "movq %%mm6, (%%"REG_a") \n\t" // X
3057fa66 287 // mm0=7(11) mm1=2(11) mm2=3 mm3=6(11) mm4=1(11) mm5=4 mm7=9
053dea12 288 "movq (%%"REG_a", %1, 4), %%mm6 \n\t" // 1
13e00528
A
289 PAVGB(%%mm7, %%mm6) // 11 /2
290 PAVGB(%%mm4, %%mm6) // 11 11 /4
291 PAVGB(%%mm3, %%mm6) // 11 2211 /8
292 PAVGB(%%mm5, %%mm2) // 11 /2
3057fa66 293 "movq (%0, %1, 4), %%mm4 \n\t" // 1
13e00528
A
294 PAVGB(%%mm4, %%mm2) // 112 /4
295 PAVGB(%%mm2, %%mm6) // 112242211 /16
3057fa66
A
296 "movq %%mm6, (%0, %1, 4) \n\t" // X
297 // mm0=7(11) mm1=2(11) mm2=3(112) mm3=6(11) mm4=5 mm5=4 mm7=9
13e00528
A
298 PAVGB(%%mm7, %%mm1) // 11 2 /4
299 PAVGB(%%mm4, %%mm5) // 11 /2
300 PAVGB(%%mm5, %%mm0) // 11 11 /4
053dea12 301 "movq (%%"REG_a", %1, 2), %%mm6 \n\t" // 1
13e00528
A
302 PAVGB(%%mm6, %%mm1) // 11 4 2 /8
303 PAVGB(%%mm0, %%mm1) // 11224222 /16
053dea12 304 "movq %%mm1, (%%"REG_a", %1, 2) \n\t" // X
3057fa66 305 // mm2=3(112) mm3=6(11) mm4=5 mm5=4(11) mm6=6 mm7=9
053dea12
AJ
306 PAVGB((%%REGc), %%mm2) // 112 4 /8
307 "movq (%%"REG_a", %1, 4), %%mm0 \n\t" // 1
13e00528
A
308 PAVGB(%%mm0, %%mm6) // 1 1 /2
309 PAVGB(%%mm7, %%mm6) // 1 12 /4
310 PAVGB(%%mm2, %%mm6) // 1122424 /4
053dea12 311 "movq %%mm6, (%%"REG_c") \n\t" // X
3057fa66 312 // mm0=8 mm3=6(11) mm4=5 mm5=4(11) mm7=9
13e00528
A
313 PAVGB(%%mm7, %%mm5) // 11 2 /4
314 PAVGB(%%mm7, %%mm5) // 11 6 /8
3057fa66 315
13e00528
A
316 PAVGB(%%mm3, %%mm0) // 112 /4
317 PAVGB(%%mm0, %%mm5) // 112246 /16
053dea12
AJ
318 "movq %%mm5, (%%"REG_a", %1, 4) \n\t" // X
319 "sub %1, %0 \n\t"
3057fa66
A
320
321 :
053dea12
AJ
322 : "r" (src), "r" ((long)stride), "m" (c->pQPb)
323 : "%"REG_a, "%"REG_c
3057fa66 324 );
64c968da 325#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
3057fa66
A
326 const int l1= stride;
327 const int l2= stride + l1;
328 const int l3= stride + l2;
329 const int l4= stride + l3;
330 const int l5= stride + l4;
331 const int l6= stride + l5;
332 const int l7= stride + l6;
333 const int l8= stride + l7;
334 const int l9= stride + l8;
d5a1a995 335 int x;
acced553 336 src+= stride*3;
d5a1a995 337 for(x=0; x<BLOCK_SIZE; x++)
3057fa66 338 {
9c9e467d
MN
339 const int first= ABS(src[0] - src[l1]) < c->QP ? src[0] : src[l1];
340 const int last= ABS(src[l8] - src[l9]) < c->QP ? src[l9] : src[l8];
3057fa66 341
8c8bbd10
MN
342 int sums[10];
343 sums[0] = 4*first + src[l1] + src[l2] + src[l3] + 4;
344 sums[1] = sums[0] - first + src[l4];
345 sums[2] = sums[1] - first + src[l5];
346 sums[3] = sums[2] - first + src[l6];
347 sums[4] = sums[3] - first + src[l7];
348 sums[5] = sums[4] - src[l1] + src[l8];
349 sums[6] = sums[5] - src[l2] + last;
350 sums[7] = sums[6] - src[l3] + last;
351 sums[8] = sums[7] - src[l4] + last;
352 sums[9] = sums[8] - src[l5] + last;
353
354 src[l1]= (sums[0] + sums[2] + 2*src[l1])>>4;
355 src[l2]= (sums[1] + sums[3] + 2*src[l2])>>4;
356 src[l3]= (sums[2] + sums[4] + 2*src[l3])>>4;
357 src[l4]= (sums[3] + sums[5] + 2*src[l4])>>4;
358 src[l5]= (sums[4] + sums[6] + 2*src[l5])>>4;
359 src[l6]= (sums[5] + sums[7] + 2*src[l6])>>4;
360 src[l7]= (sums[6] + sums[8] + 2*src[l7])>>4;
361 src[l8]= (sums[7] + sums[9] + 2*src[l8])>>4;
3057fa66
A
362
363 src++;
364 }
64c968da 365#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
3057fa66 366}
b0ac780a 367#endif //HAVE_ALTIVEC
3057fa66 368
9c9e467d 369#if 0
13e00528
A
370/**
371 * Experimental implementation of the filter (Algorithm 1) described in a paper from Ramkishor & Karandikar
372 * values are correctly clipped (MMX2)
373 * values are wraparound (C)
374 * conclusion: its fast, but introduces ugly horizontal patterns if there is a continious gradient
375 0 8 16 24
376 x = 8
377 x/2 = 4
378 x/8 = 1
379 1 12 12 23
380 */
cc9b0679 381static inline void RENAME(vertRK1Filter)(uint8_t *src, int stride, int QP)
13e00528 382{
d5a1a995 383#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
acced553 384 src+= stride*3;
13e00528
A
385// FIXME rounding
386 asm volatile(
387 "pxor %%mm7, %%mm7 \n\t" // 0
9b464428 388 "movq "MANGLE(b80)", %%mm6 \n\t" // MIN_SIGNED_BYTE
053dea12
AJ
389 "leal (%0, %1), %%"REG_a" \n\t"
390 "leal (%%"REG_a", %1, 4), %%"REG_c" \n\t"
13e00528 391// 0 1 2 3 4 5 6 7 8 9
9c9e467d 392// %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1 %0+8%1 ecx+4%1
9b464428 393 "movq "MANGLE(pQPb)", %%mm0 \n\t" // QP,..., QP
13e00528 394 "movq %%mm0, %%mm1 \n\t" // QP,..., QP
9b464428 395 "paddusb "MANGLE(b02)", %%mm0 \n\t"
13e00528 396 "psrlw $2, %%mm0 \n\t"
9b464428 397 "pand "MANGLE(b3F)", %%mm0 \n\t" // QP/4,..., QP/4
13e00528
A
398 "paddusb %%mm1, %%mm0 \n\t" // QP*1.25 ...
399 "movq (%0, %1, 4), %%mm2 \n\t" // line 4
053dea12 400 "movq (%%"REG_c"), %%mm3 \n\t" // line 5
13e00528
A
401 "movq %%mm2, %%mm4 \n\t" // line 4
402 "pcmpeqb %%mm5, %%mm5 \n\t" // -1
403 "pxor %%mm2, %%mm5 \n\t" // -line 4 - 1
d5a1a995 404 PAVGB(%%mm3, %%mm5)
13e00528
A
405 "paddb %%mm6, %%mm5 \n\t" // (l5-l4)/2
406 "psubusb %%mm3, %%mm4 \n\t"
407 "psubusb %%mm2, %%mm3 \n\t"
408 "por %%mm3, %%mm4 \n\t" // |l4 - l5|
409 "psubusb %%mm0, %%mm4 \n\t"
410 "pcmpeqb %%mm7, %%mm4 \n\t"
411 "pand %%mm4, %%mm5 \n\t" // d/2
412
413// "paddb %%mm6, %%mm2 \n\t" // line 4 + 0x80
414 "paddb %%mm5, %%mm2 \n\t"
415// "psubb %%mm6, %%mm2 \n\t"
416 "movq %%mm2, (%0,%1, 4) \n\t"
417
053dea12 418 "movq (%%"REG_c"), %%mm2 \n\t"
13e00528
A
419// "paddb %%mm6, %%mm2 \n\t" // line 5 + 0x80
420 "psubb %%mm5, %%mm2 \n\t"
421// "psubb %%mm6, %%mm2 \n\t"
053dea12 422 "movq %%mm2, (%%"REG_c") \n\t"
13e00528
A
423
424 "paddb %%mm6, %%mm5 \n\t"
425 "psrlw $2, %%mm5 \n\t"
9b464428
FB
426 "pand "MANGLE(b3F)", %%mm5 \n\t"
427 "psubb "MANGLE(b20)", %%mm5 \n\t" // (l5-l4)/8
13e00528 428
053dea12 429 "movq (%%"REG_a", %1, 2), %%mm2 \n\t"
13e00528
A
430 "paddb %%mm6, %%mm2 \n\t" // line 3 + 0x80
431 "paddsb %%mm5, %%mm2 \n\t"
432 "psubb %%mm6, %%mm2 \n\t"
053dea12 433 "movq %%mm2, (%%"REG_a", %1, 2) \n\t"
13e00528 434
053dea12 435 "movq (%%"REG_c", %1), %%mm2 \n\t"
13e00528
A
436 "paddb %%mm6, %%mm2 \n\t" // line 6 + 0x80
437 "psubsb %%mm5, %%mm2 \n\t"
438 "psubb %%mm6, %%mm2 \n\t"
053dea12 439 "movq %%mm2, (%%"REG_c", %1) \n\t"
13e00528
A
440
441 :
053dea12
AJ
442 : "r" (src), "r" ((long)stride)
443 : "%"REG_a, "%"REG_c
13e00528 444 );
64c968da 445#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
13e00528
A
446 const int l1= stride;
447 const int l2= stride + l1;
448 const int l3= stride + l2;
449 const int l4= stride + l3;
450 const int l5= stride + l4;
451 const int l6= stride + l5;
e5c30e06
MN
452// const int l7= stride + l6;
453// const int l8= stride + l7;
454// const int l9= stride + l8;
d5a1a995 455 int x;
3407a972 456 const int QP15= QP + (QP>>2);
acced553 457 src+= stride*3;
d5a1a995 458 for(x=0; x<BLOCK_SIZE; x++)
13e00528 459 {
3407a972
MN
460 const int v = (src[x+l5] - src[x+l4]);
461 if(ABS(v) < QP15)
13e00528 462 {
3407a972
MN
463 src[x+l3] +=v>>3;
464 src[x+l4] +=v>>1;
465 src[x+l5] -=v>>1;
466 src[x+l6] -=v>>3;
13e00528 467
13e00528 468 }
13e00528
A
469 }
470
64c968da 471#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
13e00528 472}
64c968da 473#endif //0
13e00528
A
474
475/**
476 * Experimental Filter 1
9f45d04d
MN
477 * will not damage linear gradients
478 * Flat blocks should look like they where passed through the (1,1,2,2,4,2,2,1,1) 9-Tap filter
d5a1a995
MN
479 * can only smooth blocks at the expected locations (it cant smooth them if they did move)
480 * MMX2 version does correct clipping C version doesnt
13e00528 481 */
9c9e467d 482static inline void RENAME(vertX1Filter)(uint8_t *src, int stride, PPContext *co)
13e00528 483{
d5a1a995 484#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
acced553
MN
485 src+= stride*3;
486
13e00528 487 asm volatile(
d5a1a995 488 "pxor %%mm7, %%mm7 \n\t" // 0
053dea12
AJ
489 "lea (%0, %1), %%"REG_a" \n\t"
490 "lea (%%"REG_a", %1, 4), %%"REG_c" \n\t"
d5a1a995 491// 0 1 2 3 4 5 6 7 8 9
9c9e467d 492// %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1 %0+8%1 ecx+4%1
053dea12 493 "movq (%%"REG_a", %1, 2), %%mm0 \n\t" // line 3
d5a1a995
MN
494 "movq (%0, %1, 4), %%mm1 \n\t" // line 4
495 "movq %%mm1, %%mm2 \n\t" // line 4
496 "psubusb %%mm0, %%mm1 \n\t"
497 "psubusb %%mm2, %%mm0 \n\t"
498 "por %%mm1, %%mm0 \n\t" // |l2 - l3|
053dea12
AJ
499 "movq (%%"REG_c"), %%mm3 \n\t" // line 5
500 "movq (%%"REG_c", %1), %%mm4 \n\t" // line 6
d5a1a995
MN
501 "movq %%mm3, %%mm5 \n\t" // line 5
502 "psubusb %%mm4, %%mm3 \n\t"
503 "psubusb %%mm5, %%mm4 \n\t"
504 "por %%mm4, %%mm3 \n\t" // |l5 - l6|
505 PAVGB(%%mm3, %%mm0) // (|l2 - l3| + |l5 - l6|)/2
506 "movq %%mm2, %%mm1 \n\t" // line 4
507 "psubusb %%mm5, %%mm2 \n\t"
508 "movq %%mm2, %%mm4 \n\t"
509 "pcmpeqb %%mm7, %%mm2 \n\t" // (l4 - l5) <= 0 ? -1 : 0
510 "psubusb %%mm1, %%mm5 \n\t"
511 "por %%mm5, %%mm4 \n\t" // |l4 - l5|
512 "psubusb %%mm0, %%mm4 \n\t" //d = MAX(0, |l4-l5| - (|l2-l3| + |l5-l6|)/2)
513 "movq %%mm4, %%mm3 \n\t" // d
9c9e467d 514 "movq %2, %%mm0 \n\t"
dc16b332
MN
515 "paddusb %%mm0, %%mm0 \n\t"
516 "psubusb %%mm0, %%mm4 \n\t"
d5a1a995 517 "pcmpeqb %%mm7, %%mm4 \n\t" // d <= QP ? -1 : 0
9b464428 518 "psubusb "MANGLE(b01)", %%mm3 \n\t"
d5a1a995
MN
519 "pand %%mm4, %%mm3 \n\t" // d <= QP ? d : 0
520
521 PAVGB(%%mm7, %%mm3) // d/2
9f45d04d
MN
522 "movq %%mm3, %%mm1 \n\t" // d/2
523 PAVGB(%%mm7, %%mm3) // d/4
524 PAVGB(%%mm1, %%mm3) // 3*d/8
d5a1a995
MN
525
526 "movq (%0, %1, 4), %%mm0 \n\t" // line 4
527 "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l4-1 : l4
528 "psubusb %%mm3, %%mm0 \n\t"
529 "pxor %%mm2, %%mm0 \n\t"
530 "movq %%mm0, (%0, %1, 4) \n\t" // line 4
531
053dea12 532 "movq (%%"REG_c"), %%mm0 \n\t" // line 5
d5a1a995
MN
533 "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l5-1 : l5
534 "paddusb %%mm3, %%mm0 \n\t"
535 "pxor %%mm2, %%mm0 \n\t"
053dea12 536 "movq %%mm0, (%%"REG_c") \n\t" // line 5
d5a1a995 537
9f45d04d 538 PAVGB(%%mm7, %%mm1) // d/4
d5a1a995 539
053dea12 540 "movq (%%"REG_a", %1, 2), %%mm0 \n\t" // line 3
d5a1a995 541 "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l4-1 : l4
9f45d04d 542 "psubusb %%mm1, %%mm0 \n\t"
d5a1a995 543 "pxor %%mm2, %%mm0 \n\t"
053dea12 544 "movq %%mm0, (%%"REG_a", %1, 2) \n\t" // line 3
d5a1a995 545
053dea12 546 "movq (%%"REG_c", %1), %%mm0 \n\t" // line 6
d5a1a995 547 "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l5-1 : l5
9f45d04d 548 "paddusb %%mm1, %%mm0 \n\t"
d5a1a995 549 "pxor %%mm2, %%mm0 \n\t"
053dea12 550 "movq %%mm0, (%%"REG_c", %1) \n\t" // line 6
d5a1a995 551
9f45d04d 552 PAVGB(%%mm7, %%mm1) // d/8
d5a1a995 553
053dea12 554 "movq (%%"REG_a", %1), %%mm0 \n\t" // line 2
d5a1a995 555 "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l2-1 : l2
9f45d04d 556 "psubusb %%mm1, %%mm0 \n\t"
d5a1a995 557 "pxor %%mm2, %%mm0 \n\t"
053dea12 558 "movq %%mm0, (%%"REG_a", %1) \n\t" // line 2
d5a1a995 559
053dea12 560 "movq (%%"REG_c", %1, 2), %%mm0 \n\t" // line 7
d5a1a995 561 "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l7-1 : l7
9f45d04d 562 "paddusb %%mm1, %%mm0 \n\t"
d5a1a995 563 "pxor %%mm2, %%mm0 \n\t"
053dea12 564 "movq %%mm0, (%%"REG_c", %1, 2) \n\t" // line 7
13e00528
A
565
566 :
053dea12
AJ
567 : "r" (src), "r" ((long)stride), "m" (co->pQPb)
568 : "%"REG_a, "%"REG_c
13e00528 569 );
64c968da 570#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
d5a1a995
MN
571
572 const int l1= stride;
573 const int l2= stride + l1;
574 const int l3= stride + l2;
575 const int l4= stride + l3;
576 const int l5= stride + l4;
577 const int l6= stride + l5;
578 const int l7= stride + l6;
e5c30e06
MN
579// const int l8= stride + l7;
580// const int l9= stride + l8;
d5a1a995 581 int x;
acced553
MN
582
583 src+= stride*3;
d5a1a995
MN
584 for(x=0; x<BLOCK_SIZE; x++)
585 {
586 int a= src[l3] - src[l4];
587 int b= src[l4] - src[l5];
9f45d04d 588 int c= src[l5] - src[l6];
d5a1a995 589
3407a972
MN
590 int d= ABS(b) - ((ABS(a) + ABS(c))>>1);
591 d= MAX(d, 0);
d5a1a995 592
9c9e467d 593 if(d < co->QP*2)
d5a1a995
MN
594 {
595 int v = d * SIGN(-b);
596
3407a972
MN
597 src[l2] +=v>>3;
598 src[l3] +=v>>2;
599 src[l4] +=(3*v)>>3;
600 src[l5] -=(3*v)>>3;
601 src[l6] -=v>>2;
602 src[l7] -=v>>3;
d5a1a995
MN
603
604 }
605 src++;
606 }
64c968da 607#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
13e00528
A
608}
609
b0ac780a 610#ifndef HAVE_ALTIVEC
9c9e467d 611static inline void RENAME(doVertDefFilter)(uint8_t src[], int stride, PPContext *c)
3057fa66 612{
7f16f6e6
MN
613#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
614/*
615 uint8_t tmp[16];
616 const int l1= stride;
617 const int l2= stride + l1;
618 const int l3= stride + l2;
619 const int l4= (int)tmp - (int)src - stride*3;
620 const int l5= (int)tmp - (int)src - stride*3 + 8;
621 const int l6= stride*3 + l3;
622 const int l7= stride + l6;
623 const int l8= stride + l7;
624
625 memcpy(tmp, src+stride*7, 8);
626 memcpy(tmp+8, src+stride*8, 8);
627*/
628 src+= stride*4;
629 asm volatile(
630
631#if 0 //sligtly more accurate and slightly slower
632 "pxor %%mm7, %%mm7 \n\t" // 0
053dea12
AJ
633 "lea (%0, %1), %%"REG_a" \n\t"
634 "lea (%%"REG_a", %1, 4), %%"REG_c" \n\t"
7f16f6e6 635// 0 1 2 3 4 5 6 7
9c9e467d
MN
636// %0 %0+%1 %0+2%1 eax+2%1 %0+4%1 eax+4%1 ecx+%1 ecx+2%1
637// %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1
7f16f6e6
MN
638
639
640 "movq (%0, %1, 2), %%mm0 \n\t" // l2
641 "movq (%0), %%mm1 \n\t" // l0
642 "movq %%mm0, %%mm2 \n\t" // l2
643 PAVGB(%%mm7, %%mm0) // ~l2/2
644 PAVGB(%%mm1, %%mm0) // ~(l2 + 2l0)/4
645 PAVGB(%%mm2, %%mm0) // ~(5l2 + 2l0)/8
646
053dea12
AJ
647 "movq (%%"REG_a"), %%mm1 \n\t" // l1
648 "movq (%%"REG_a", %1, 2), %%mm3 \n\t" // l3
7f16f6e6
MN
649 "movq %%mm1, %%mm4 \n\t" // l1
650 PAVGB(%%mm7, %%mm1) // ~l1/2
651 PAVGB(%%mm3, %%mm1) // ~(l1 + 2l3)/4
652 PAVGB(%%mm4, %%mm1) // ~(5l1 + 2l3)/8
653
654 "movq %%mm0, %%mm4 \n\t" // ~(5l2 + 2l0)/8
655 "psubusb %%mm1, %%mm0 \n\t"
656 "psubusb %%mm4, %%mm1 \n\t"
657 "por %%mm0, %%mm1 \n\t" // ~|2l0 - 5l1 + 5l2 - 2l3|/8
658// mm1= |lenergy|, mm2= l2, mm3= l3, mm7=0
659
660 "movq (%0, %1, 4), %%mm0 \n\t" // l4
661 "movq %%mm0, %%mm4 \n\t" // l4
662 PAVGB(%%mm7, %%mm0) // ~l4/2
663 PAVGB(%%mm2, %%mm0) // ~(l4 + 2l2)/4
664 PAVGB(%%mm4, %%mm0) // ~(5l4 + 2l2)/8
665
053dea12 666 "movq (%%"REG_c"), %%mm2 \n\t" // l5
7f16f6e6
MN
667 "movq %%mm3, %%mm5 \n\t" // l3
668 PAVGB(%%mm7, %%mm3) // ~l3/2
669 PAVGB(%%mm2, %%mm3) // ~(l3 + 2l5)/4
670 PAVGB(%%mm5, %%mm3) // ~(5l3 + 2l5)/8
671
672 "movq %%mm0, %%mm6 \n\t" // ~(5l4 + 2l2)/8
673 "psubusb %%mm3, %%mm0 \n\t"
674 "psubusb %%mm6, %%mm3 \n\t"
675 "por %%mm0, %%mm3 \n\t" // ~|2l2 - 5l3 + 5l4 - 2l5|/8
676 "pcmpeqb %%mm7, %%mm0 \n\t" // SIGN(2l2 - 5l3 + 5l4 - 2l5)
677// mm0= SIGN(menergy), mm1= |lenergy|, mm2= l5, mm3= |menergy|, mm4=l4, mm5= l3, mm7=0
678
053dea12 679 "movq (%%"REG_c", %1), %%mm6 \n\t" // l6
7f16f6e6
MN
680 "movq %%mm6, %%mm5 \n\t" // l6
681 PAVGB(%%mm7, %%mm6) // ~l6/2
682 PAVGB(%%mm4, %%mm6) // ~(l6 + 2l4)/4
683 PAVGB(%%mm5, %%mm6) // ~(5l6 + 2l4)/8
684
053dea12 685 "movq (%%"REG_c", %1, 2), %%mm5 \n\t" // l7
7f16f6e6
MN
686 "movq %%mm2, %%mm4 \n\t" // l5
687 PAVGB(%%mm7, %%mm2) // ~l5/2
688 PAVGB(%%mm5, %%mm2) // ~(l5 + 2l7)/4
689 PAVGB(%%mm4, %%mm2) // ~(5l5 + 2l7)/8
690
691 "movq %%mm6, %%mm4 \n\t" // ~(5l6 + 2l4)/8
692 "psubusb %%mm2, %%mm6 \n\t"
693 "psubusb %%mm4, %%mm2 \n\t"
694 "por %%mm6, %%mm2 \n\t" // ~|2l4 - 5l5 + 5l6 - 2l7|/8
695// mm0= SIGN(menergy), mm1= |lenergy|/8, mm2= |renergy|/8, mm3= |menergy|/8, mm7=0
696
697
698 PMINUB(%%mm2, %%mm1, %%mm4) // MIN(|lenergy|,|renergy|)/8
9c9e467d 699 "movq %2, %%mm4 \n\t" // QP //FIXME QP+1 ?
9b464428 700 "paddusb "MANGLE(b01)", %%mm4 \n\t"
7f16f6e6
MN
701 "pcmpgtb %%mm3, %%mm4 \n\t" // |menergy|/8 < QP
702 "psubusb %%mm1, %%mm3 \n\t" // d=|menergy|/8-MIN(|lenergy|,|renergy|)/8
703 "pand %%mm4, %%mm3 \n\t"
704
705 "movq %%mm3, %%mm1 \n\t"
9b464428 706// "psubusb "MANGLE(b01)", %%mm3 \n\t"
7f16f6e6
MN
707 PAVGB(%%mm7, %%mm3)
708 PAVGB(%%mm7, %%mm3)
709 "paddusb %%mm1, %%mm3 \n\t"
9b464428 710// "paddusb "MANGLE(b01)", %%mm3 \n\t"
7f16f6e6 711
053dea12 712 "movq (%%"REG_a", %1, 2), %%mm6 \n\t" //l3
7f16f6e6
MN
713 "movq (%0, %1, 4), %%mm5 \n\t" //l4
714 "movq (%0, %1, 4), %%mm4 \n\t" //l4
715 "psubusb %%mm6, %%mm5 \n\t"
716 "psubusb %%mm4, %%mm6 \n\t"
717 "por %%mm6, %%mm5 \n\t" // |l3-l4|
718 "pcmpeqb %%mm7, %%mm6 \n\t" // SIGN(l3-l4)
719 "pxor %%mm6, %%mm0 \n\t"
720 "pand %%mm0, %%mm3 \n\t"
721 PMINUB(%%mm5, %%mm3, %%mm0)
722
9b464428 723 "psubusb "MANGLE(b01)", %%mm3 \n\t"
7f16f6e6
MN
724 PAVGB(%%mm7, %%mm3)
725
053dea12 726 "movq (%%"REG_a", %1, 2), %%mm0 \n\t"
7f16f6e6
MN
727 "movq (%0, %1, 4), %%mm2 \n\t"
728 "pxor %%mm6, %%mm0 \n\t"
729 "pxor %%mm6, %%mm2 \n\t"
730 "psubb %%mm3, %%mm0 \n\t"
731 "paddb %%mm3, %%mm2 \n\t"
732 "pxor %%mm6, %%mm0 \n\t"
733 "pxor %%mm6, %%mm2 \n\t"
053dea12 734 "movq %%mm0, (%%"REG_a", %1, 2) \n\t"
7f16f6e6 735 "movq %%mm2, (%0, %1, 4) \n\t"
64c968da 736#endif //0
7f16f6e6 737
053dea12 738 "lea (%0, %1), %%"REG_a" \n\t"
7f16f6e6
MN
739 "pcmpeqb %%mm6, %%mm6 \n\t" // -1
740// 0 1 2 3 4 5 6 7
9c9e467d
MN
741// %0 %0+%1 %0+2%1 eax+2%1 %0+4%1 eax+4%1 ecx+%1 ecx+2%1
742// %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1
7f16f6e6
MN
743
744
053dea12 745 "movq (%%"REG_a", %1, 2), %%mm1 \n\t" // l3
7f16f6e6
MN
746 "movq (%0, %1, 4), %%mm0 \n\t" // l4
747 "pxor %%mm6, %%mm1 \n\t" // -l3-1
748 PAVGB(%%mm1, %%mm0) // -q+128 = (l4-l3+256)/2
749// mm1=-l3-1, mm0=128-q
750
053dea12
AJ
751 "movq (%%"REG_a", %1, 4), %%mm2 \n\t" // l5
752 "movq (%%"REG_a", %1), %%mm3 \n\t" // l2
7f16f6e6
MN
753 "pxor %%mm6, %%mm2 \n\t" // -l5-1
754 "movq %%mm2, %%mm5 \n\t" // -l5-1
9b464428 755 "movq "MANGLE(b80)", %%mm4 \n\t" // 128
053dea12 756 "lea (%%"REG_a", %1, 4), %%"REG_c" \n\t"
7f16f6e6
MN
757 PAVGB(%%mm3, %%mm2) // (l2-l5+256)/2
758 PAVGB(%%mm0, %%mm4) // ~(l4-l3)/4 + 128
759 PAVGB(%%mm2, %%mm4) // ~(l2-l5)/4 +(l4-l3)/8 + 128
760 PAVGB(%%mm0, %%mm4) // ~(l2-l5)/8 +5(l4-l3)/16 + 128
761// mm1=-l3-1, mm0=128-q, mm3=l2, mm4=menergy/16 + 128, mm5= -l5-1
762
053dea12 763 "movq (%%"REG_a"), %%mm2 \n\t" // l1
7f16f6e6
MN
764 "pxor %%mm6, %%mm2 \n\t" // -l1-1
765 PAVGB(%%mm3, %%mm2) // (l2-l1+256)/2
766 PAVGB((%0), %%mm1) // (l0-l3+256)/2
9b464428 767 "movq "MANGLE(b80)", %%mm3 \n\t" // 128
7f16f6e6
MN
768 PAVGB(%%mm2, %%mm3) // ~(l2-l1)/4 + 128
769 PAVGB(%%mm1, %%mm3) // ~(l0-l3)/4 +(l2-l1)/8 + 128
770 PAVGB(%%mm2, %%mm3) // ~(l0-l3)/8 +5(l2-l1)/16 + 128
771// mm0=128-q, mm3=lenergy/16 + 128, mm4= menergy/16 + 128, mm5= -l5-1
772
053dea12
AJ
773 PAVGB((%%REGc, %1), %%mm5) // (l6-l5+256)/2
774 "movq (%%"REG_c", %1, 2), %%mm1 \n\t" // l7
7f16f6e6
MN
775 "pxor %%mm6, %%mm1 \n\t" // -l7-1
776 PAVGB((%0, %1, 4), %%mm1) // (l4-l7+256)/2
9b464428 777 "movq "MANGLE(b80)", %%mm2 \n\t" // 128
7f16f6e6
MN
778 PAVGB(%%mm5, %%mm2) // ~(l6-l5)/4 + 128
779 PAVGB(%%mm1, %%mm2) // ~(l4-l7)/4 +(l6-l5)/8 + 128
780 PAVGB(%%mm5, %%mm2) // ~(l4-l7)/8 +5(l6-l5)/16 + 128
781// mm0=128-q, mm2=renergy/16 + 128, mm3=lenergy/16 + 128, mm4= menergy/16 + 128
782
9b464428
FB
783 "movq "MANGLE(b00)", %%mm1 \n\t" // 0
784 "movq "MANGLE(b00)", %%mm5 \n\t" // 0
7f16f6e6
MN
785 "psubb %%mm2, %%mm1 \n\t" // 128 - renergy/16
786 "psubb %%mm3, %%mm5 \n\t" // 128 - lenergy/16
787 PMAXUB(%%mm1, %%mm2) // 128 + |renergy/16|
788 PMAXUB(%%mm5, %%mm3) // 128 + |lenergy/16|
789 PMINUB(%%mm2, %%mm3, %%mm1) // 128 + MIN(|lenergy|,|renergy|)/16
790
791// mm0=128-q, mm3=128 + MIN(|lenergy|,|renergy|)/16, mm4= menergy/16 + 128
792
9b464428 793 "movq "MANGLE(b00)", %%mm7 \n\t" // 0
9c9e467d 794 "movq %2, %%mm2 \n\t" // QP
7f16f6e6
MN
795 PAVGB(%%mm6, %%mm2) // 128 + QP/2
796 "psubb %%mm6, %%mm2 \n\t"
797
798 "movq %%mm4, %%mm1 \n\t"
799 "pcmpgtb %%mm7, %%mm1 \n\t" // SIGN(menergy)
800 "pxor %%mm1, %%mm4 \n\t"
801 "psubb %%mm1, %%mm4 \n\t" // 128 + |menergy|/16
802 "pcmpgtb %%mm4, %%mm2 \n\t" // |menergy|/16 < QP/2
803 "psubusb %%mm3, %%mm4 \n\t" //d=|menergy|/16 - MIN(|lenergy|,|renergy|)/16
804// mm0=128-q, mm1= SIGN(menergy), mm2= |menergy|/16 < QP/2, mm4= d/16
805
806 "movq %%mm4, %%mm3 \n\t" // d
9b464428 807 "psubusb "MANGLE(b01)", %%mm4 \n\t"
7f16f6e6
MN
808 PAVGB(%%mm7, %%mm4) // d/32
809 PAVGB(%%mm7, %%mm4) // (d + 32)/64
810 "paddb %%mm3, %%mm4 \n\t" // 5d/64
811 "pand %%mm2, %%mm4 \n\t"
812
9b464428 813 "movq "MANGLE(b80)", %%mm5 \n\t" // 128
7f16f6e6
MN
814 "psubb %%mm0, %%mm5 \n\t" // q
815 "paddsb %%mm6, %%mm5 \n\t" // fix bad rounding
816 "pcmpgtb %%mm5, %%mm7 \n\t" // SIGN(q)
817 "pxor %%mm7, %%mm5 \n\t"
818
819 PMINUB(%%mm5, %%mm4, %%mm3) // MIN(|q|, 5d/64)
820 "pxor %%mm1, %%mm7 \n\t" // SIGN(d*q)
821
822 "pand %%mm7, %%mm4 \n\t"
053dea12 823 "movq (%%"REG_a", %1, 2), %%mm0 \n\t"
7f16f6e6
MN
824 "movq (%0, %1, 4), %%mm2 \n\t"
825 "pxor %%mm1, %%mm0 \n\t"
826 "pxor %%mm1, %%mm2 \n\t"
827 "paddb %%mm4, %%mm0 \n\t"
828 "psubb %%mm4, %%mm2 \n\t"
829 "pxor %%mm1, %%mm0 \n\t"
830 "pxor %%mm1, %%mm2 \n\t"
053dea12 831 "movq %%mm0, (%%"REG_a", %1, 2) \n\t"
7f16f6e6
MN
832 "movq %%mm2, (%0, %1, 4) \n\t"
833
834 :
053dea12
AJ
835 : "r" (src), "r" ((long)stride), "m" (c->pQPb)
836 : "%"REG_a, "%"REG_c
7f16f6e6
MN
837 );
838
839/*
840 {
841 int x;
842 src-= stride;
843 for(x=0; x<BLOCK_SIZE; x++)
844 {
845 const int middleEnergy= 5*(src[l5] - src[l4]) + 2*(src[l3] - src[l6]);
846 if(ABS(middleEnergy)< 8*QP)
847 {
848 const int q=(src[l4] - src[l5])/2;
849 const int leftEnergy= 5*(src[l3] - src[l2]) + 2*(src[l1] - src[l4]);
850 const int rightEnergy= 5*(src[l7] - src[l6]) + 2*(src[l5] - src[l8]);
851
852 int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) );
853 d= MAX(d, 0);
854
855 d= (5*d + 32) >> 6;
856 d*= SIGN(-middleEnergy);
857
858 if(q>0)
859 {
860 d= d<0 ? 0 : d;
861 d= d>q ? q : d;
862 }
863 else
864 {
865 d= d>0 ? 0 : d;
866 d= d<q ? q : d;
867 }
868
869 src[l4]-= d;
870 src[l5]+= d;
871 }
872 src++;
873 }
874src-=8;
875 for(x=0; x<8; x++)
876 {
877 int y;
878 for(y=4; y<6; y++)
879 {
880 int d= src[x+y*stride] - tmp[x+(y-4)*8];
881 int ad= ABS(d);
882 static int max=0;
883 static int sum=0;
884 static int num=0;
885 static int bias=0;
886
887 if(max<ad) max=ad;
888 sum+= ad>3 ? 1 : 0;
889 if(ad>3)
890 {
891 src[0] = src[7] = src[stride*7] = src[(stride+1)*7]=255;
892 }
893 if(y==4) bias+=d;
894 num++;
895 if(num%1000000 == 0)
896 {
897 printf(" %d %d %d %d\n", num, sum, max, bias);
898 }
899 }
900 }
901}
902*/
903#elif defined (HAVE_MMX)
acced553 904 src+= stride*4;
3057fa66
A
905 asm volatile(
906 "pxor %%mm7, %%mm7 \n\t"
053dea12
AJ
907 "lea -40(%%"REG_SP"), %%"REG_c" \n\t" // make space for 4 8-byte vars
908 "and "ALIGN_MASK", %%"REG_c" \n\t" // align
3057fa66 909// 0 1 2 3 4 5 6 7
9c9e467d
MN
910// %0 %0+%1 %0+2%1 eax+2%1 %0+4%1 eax+4%1 edx+%1 edx+2%1
911// %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1
3057fa66
A
912
913 "movq (%0), %%mm0 \n\t"
914 "movq %%mm0, %%mm1 \n\t"
915 "punpcklbw %%mm7, %%mm0 \n\t" // low part of line 0
916 "punpckhbw %%mm7, %%mm1 \n\t" // high part of line 0
917
01dc3aa4 918 "movq (%0, %1), %%mm2 \n\t"
053dea12 919 "lea (%0, %1, 2), %%"REG_a" \n\t"
3057fa66
A
920 "movq %%mm2, %%mm3 \n\t"
921 "punpcklbw %%mm7, %%mm2 \n\t" // low part of line 1
922 "punpckhbw %%mm7, %%mm3 \n\t" // high part of line 1
923
053dea12 924 "movq (%%"REG_a"), %%mm4 \n\t"
3057fa66
A
925 "movq %%mm4, %%mm5 \n\t"
926 "punpcklbw %%mm7, %%mm4 \n\t" // low part of line 2
927 "punpckhbw %%mm7, %%mm5 \n\t" // high part of line 2
928
929 "paddw %%mm0, %%mm0 \n\t" // 2L0
930 "paddw %%mm1, %%mm1 \n\t" // 2H0
931 "psubw %%mm4, %%mm2 \n\t" // L1 - L2
932 "psubw %%mm5, %%mm3 \n\t" // H1 - H2
933 "psubw %%mm2, %%mm0 \n\t" // 2L0 - L1 + L2
934 "psubw %%mm3, %%mm1 \n\t" // 2H0 - H1 + H2
935
936 "psllw $2, %%mm2 \n\t" // 4L1 - 4L2
937 "psllw $2, %%mm3 \n\t" // 4H1 - 4H2
938 "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2
939 "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2
940
053dea12 941 "movq (%%"REG_a", %1), %%mm2 \n\t"
3057fa66
A
942 "movq %%mm2, %%mm3 \n\t"
943 "punpcklbw %%mm7, %%mm2 \n\t" // L3
944 "punpckhbw %%mm7, %%mm3 \n\t" // H3
945
946 "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2 - L3
947 "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2 - H3
948 "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2 - 2L3
949 "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2 - 2H3
053dea12
AJ
950 "movq %%mm0, (%%"REG_c") \n\t" // 2L0 - 5L1 + 5L2 - 2L3
951 "movq %%mm1, 8(%%"REG_c") \n\t" // 2H0 - 5H1 + 5H2 - 2H3
3057fa66 952
053dea12 953 "movq (%%"REG_a", %1, 2), %%mm0 \n\t"
3057fa66
A
954 "movq %%mm0, %%mm1 \n\t"
955 "punpcklbw %%mm7, %%mm0 \n\t" // L4
956 "punpckhbw %%mm7, %%mm1 \n\t" // H4
957
958 "psubw %%mm0, %%mm2 \n\t" // L3 - L4
959 "psubw %%mm1, %%mm3 \n\t" // H3 - H4
053dea12
AJ
960 "movq %%mm2, 16(%%"REG_c") \n\t" // L3 - L4
961 "movq %%mm3, 24(%%"REG_c") \n\t" // H3 - H4
3057fa66
A
962 "paddw %%mm4, %%mm4 \n\t" // 2L2
963 "paddw %%mm5, %%mm5 \n\t" // 2H2
964 "psubw %%mm2, %%mm4 \n\t" // 2L2 - L3 + L4
965 "psubw %%mm3, %%mm5 \n\t" // 2H2 - H3 + H4
966
053dea12 967 "lea (%%"REG_a", %1), %0 \n\t"
3057fa66
A
968 "psllw $2, %%mm2 \n\t" // 4L3 - 4L4
969 "psllw $2, %%mm3 \n\t" // 4H3 - 4H4
970 "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4
971 "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4
972//50 opcodes so far
01dc3aa4 973 "movq (%0, %1, 2), %%mm2 \n\t"
3057fa66
A
974 "movq %%mm2, %%mm3 \n\t"
975 "punpcklbw %%mm7, %%mm2 \n\t" // L5
976 "punpckhbw %%mm7, %%mm3 \n\t" // H5
977 "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4 - L5
978 "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4 - H5
979 "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4 - 2L5
980 "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4 - 2H5
981
053dea12 982 "movq (%%"REG_a", %1, 4), %%mm6 \n\t"
3057fa66
A
983 "punpcklbw %%mm7, %%mm6 \n\t" // L6
984 "psubw %%mm6, %%mm2 \n\t" // L5 - L6
053dea12 985 "movq (%%"REG_a", %1, 4), %%mm6 \n\t"
3057fa66
A
986 "punpckhbw %%mm7, %%mm6 \n\t" // H6
987 "psubw %%mm6, %%mm3 \n\t" // H5 - H6
988
989 "paddw %%mm0, %%mm0 \n\t" // 2L4
990 "paddw %%mm1, %%mm1 \n\t" // 2H4
991 "psubw %%mm2, %%mm0 \n\t" // 2L4 - L5 + L6
992 "psubw %%mm3, %%mm1 \n\t" // 2H4 - H5 + H6
993
994 "psllw $2, %%mm2 \n\t" // 4L5 - 4L6
995 "psllw $2, %%mm3 \n\t" // 4H5 - 4H6
996 "psubw %%mm2, %%mm0 \n\t" // 2L4 - 5L5 + 5L6
997 "psubw %%mm3, %%mm1 \n\t" // 2H4 - 5H5 + 5H6
998
01dc3aa4 999 "movq (%0, %1, 4), %%mm2 \n\t"
3057fa66
A
1000 "movq %%mm2, %%mm3 \n\t"
1001 "punpcklbw %%mm7, %%mm2 \n\t" // L7
1002 "punpckhbw %%mm7, %%mm3 \n\t" // H7
1003
1004 "paddw %%mm2, %%mm2 \n\t" // 2L7
1005 "paddw %%mm3, %%mm3 \n\t" // 2H7
1006 "psubw %%mm2, %%mm0 \n\t" // 2L4 - 5L5 + 5L6 - 2L7
1007 "psubw %%mm3, %%mm1 \n\t" // 2H4 - 5H5 + 5H6 - 2H7
1008
053dea12
AJ
1009 "movq (%%"REG_c"), %%mm2 \n\t" // 2L0 - 5L1 + 5L2 - 2L3
1010 "movq 8(%%"REG_c"), %%mm3 \n\t" // 2H0 - 5H1 + 5H2 - 2H3
8405b3fd
MN
1011
1012#ifdef HAVE_MMX2
1013 "movq %%mm7, %%mm6 \n\t" // 0
1014 "psubw %%mm0, %%mm6 \n\t"
1015 "pmaxsw %%mm6, %%mm0 \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
1016 "movq %%mm7, %%mm6 \n\t" // 0
1017 "psubw %%mm1, %%mm6 \n\t"
1018 "pmaxsw %%mm6, %%mm1 \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
1019 "movq %%mm7, %%mm6 \n\t" // 0
1020 "psubw %%mm2, %%mm6 \n\t"
1021 "pmaxsw %%mm6, %%mm2 \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
1022 "movq %%mm7, %%mm6 \n\t" // 0
1023 "psubw %%mm3, %%mm6 \n\t"
1024 "pmaxsw %%mm6, %%mm3 \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
1025#else
3057fa66
A
1026 "movq %%mm7, %%mm6 \n\t" // 0
1027 "pcmpgtw %%mm0, %%mm6 \n\t"
1028 "pxor %%mm6, %%mm0 \n\t"
1029 "psubw %%mm6, %%mm0 \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
1030 "movq %%mm7, %%mm6 \n\t" // 0
1031 "pcmpgtw %%mm1, %%mm6 \n\t"
1032 "pxor %%mm6, %%mm1 \n\t"
1033 "psubw %%mm6, %%mm1 \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
3057fa66
A
1034 "movq %%mm7, %%mm6 \n\t" // 0
1035 "pcmpgtw %%mm2, %%mm6 \n\t"
1036 "pxor %%mm6, %%mm2 \n\t"
1037 "psubw %%mm6, %%mm2 \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
1038 "movq %%mm7, %%mm6 \n\t" // 0
1039 "pcmpgtw %%mm3, %%mm6 \n\t"
1040 "pxor %%mm6, %%mm3 \n\t"
1041 "psubw %%mm6, %%mm3 \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
8405b3fd 1042#endif
3057fa66
A
1043
1044#ifdef HAVE_MMX2
1045 "pminsw %%mm2, %%mm0 \n\t"
1046 "pminsw %%mm3, %%mm1 \n\t"
1047#else
1048 "movq %%mm0, %%mm6 \n\t"
1049 "psubusw %%mm2, %%mm6 \n\t"
1050 "psubw %%mm6, %%mm0 \n\t"
1051 "movq %%mm1, %%mm6 \n\t"
1052 "psubusw %%mm3, %%mm6 \n\t"
1053 "psubw %%mm6, %%mm1 \n\t"
1054#endif
1055
792a5a7c
MN
1056 "movd %2, %%mm2 \n\t" // QP
1057 "punpcklbw %%mm7, %%mm2 \n\t"
1058
3057fa66
A
1059 "movq %%mm7, %%mm6 \n\t" // 0
1060 "pcmpgtw %%mm4, %%mm6 \n\t" // sign(2L2 - 5L3 + 5L4 - 2L5)
1061 "pxor %%mm6, %%mm4 \n\t"
1062 "psubw %%mm6, %%mm4 \n\t" // |2L2 - 5L3 + 5L4 - 2L5|
1063 "pcmpgtw %%mm5, %%mm7 \n\t" // sign(2H2 - 5H3 + 5H4 - 2H5)
1064 "pxor %%mm7, %%mm5 \n\t"
1065 "psubw %%mm7, %%mm5 \n\t" // |2H2 - 5H3 + 5H4 - 2H5|
1066// 100 opcodes
3057fa66
A
1067 "psllw $3, %%mm2 \n\t" // 8QP
1068 "movq %%mm2, %%mm3 \n\t" // 8QP
1069 "pcmpgtw %%mm4, %%mm2 \n\t"
1070 "pcmpgtw %%mm5, %%mm3 \n\t"
1071 "pand %%mm2, %%mm4 \n\t"
1072 "pand %%mm3, %%mm5 \n\t"
1073
1074
1075 "psubusw %%mm0, %%mm4 \n\t" // hd
1076 "psubusw %%mm1, %%mm5 \n\t" // ld
1077
1078
bf1595c4 1079 "movq "MANGLE(w05)", %%mm2 \n\t" // 5
3057fa66
A
1080 "pmullw %%mm2, %%mm4 \n\t"
1081 "pmullw %%mm2, %%mm5 \n\t"
bf1595c4 1082 "movq "MANGLE(w20)", %%mm2 \n\t" // 32
3057fa66
A
1083 "paddw %%mm2, %%mm4 \n\t"
1084 "paddw %%mm2, %%mm5 \n\t"
1085 "psrlw $6, %%mm4 \n\t"
1086 "psrlw $6, %%mm5 \n\t"
1087
053dea12
AJ
1088 "movq 16(%%"REG_c"), %%mm0 \n\t" // L3 - L4
1089 "movq 24(%%"REG_c"), %%mm1 \n\t" // H3 - H4
3057fa66
A
1090
1091 "pxor %%mm2, %%mm2 \n\t"
1092 "pxor %%mm3, %%mm3 \n\t"
1093
3057fa66
A
1094 "pcmpgtw %%mm0, %%mm2 \n\t" // sign (L3-L4)
1095 "pcmpgtw %%mm1, %%mm3 \n\t" // sign (H3-H4)
1096 "pxor %%mm2, %%mm0 \n\t"
1097 "pxor %%mm3, %%mm1 \n\t"
1098 "psubw %%mm2, %%mm0 \n\t" // |L3-L4|
1099 "psubw %%mm3, %%mm1 \n\t" // |H3-H4|
e5c30e06
MN
1100 "psrlw $1, %%mm0 \n\t" // |L3 - L4|/2
1101 "psrlw $1, %%mm1 \n\t" // |H3 - H4|/2
3057fa66
A
1102
1103 "pxor %%mm6, %%mm2 \n\t"
1104 "pxor %%mm7, %%mm3 \n\t"
1105 "pand %%mm2, %%mm4 \n\t"
1106 "pand %%mm3, %%mm5 \n\t"
1107
1108#ifdef HAVE_MMX2
1109 "pminsw %%mm0, %%mm4 \n\t"
1110 "pminsw %%mm1, %%mm5 \n\t"
1111#else
1112 "movq %%mm4, %%mm2 \n\t"
1113 "psubusw %%mm0, %%mm2 \n\t"
1114 "psubw %%mm2, %%mm4 \n\t"
1115 "movq %%mm5, %%mm2 \n\t"
1116 "psubusw %%mm1, %%mm2 \n\t"
1117 "psubw %%mm2, %%mm5 \n\t"
1118#endif
1119 "pxor %%mm6, %%mm4 \n\t"
1120 "pxor %%mm7, %%mm5 \n\t"
1121 "psubw %%mm6, %%mm4 \n\t"
1122 "psubw %%mm7, %%mm5 \n\t"
1123 "packsswb %%mm5, %%mm4 \n\t"
01dc3aa4 1124 "movq (%0), %%mm0 \n\t"
3057fa66 1125 "paddb %%mm4, %%mm0 \n\t"
01dc3aa4
MN
1126 "movq %%mm0, (%0) \n\t"
1127 "movq (%0, %1), %%mm0 \n\t"
3057fa66 1128 "psubb %%mm4, %%mm0 \n\t"
01dc3aa4 1129 "movq %%mm0, (%0, %1) \n\t"
3057fa66 1130
01dc3aa4 1131 : "+r" (src)
053dea12
AJ
1132 : "r" ((long)stride), "m" (c->pQPb)
1133 : "%"REG_a, "%"REG_c
3057fa66 1134 );
64c968da 1135#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
3057fa66
A
1136 const int l1= stride;
1137 const int l2= stride + l1;
1138 const int l3= stride + l2;
1139 const int l4= stride + l3;
1140 const int l5= stride + l4;
1141 const int l6= stride + l5;
1142 const int l7= stride + l6;
1143 const int l8= stride + l7;
1144// const int l9= stride + l8;
d5a1a995 1145 int x;
acced553 1146 src+= stride*3;
d5a1a995 1147 for(x=0; x<BLOCK_SIZE; x++)
3057fa66
A
1148 {
1149 const int middleEnergy= 5*(src[l5] - src[l4]) + 2*(src[l3] - src[l6]);
9c9e467d 1150 if(ABS(middleEnergy) < 8*c->QP)
3057fa66
A
1151 {
1152 const int q=(src[l4] - src[l5])/2;
1153 const int leftEnergy= 5*(src[l3] - src[l2]) + 2*(src[l1] - src[l4]);
1154 const int rightEnergy= 5*(src[l7] - src[l6]) + 2*(src[l5] - src[l8]);
1155
1156 int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) );
1157 d= MAX(d, 0);
1158
1159 d= (5*d + 32) >> 6;
1160 d*= SIGN(-middleEnergy);
1161
1162 if(q>0)
1163 {
1164 d= d<0 ? 0 : d;
1165 d= d>q ? q : d;
1166 }
1167 else
1168 {
1169 d= d>0 ? 0 : d;
1170 d= d<q ? q : d;
1171 }
1172
1173 src[l4]-= d;
1174 src[l5]+= d;
1175 }
1176 src++;
1177 }
64c968da 1178#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
3057fa66 1179}
b0ac780a 1180#endif //HAVE_ALTIVEC
3057fa66 1181
b0ac780a 1182#ifndef HAVE_ALTIVEC
9c9e467d 1183static inline void RENAME(dering)(uint8_t src[], int stride, PPContext *c)
3057fa66 1184{
e0f8ffae 1185#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
3057fa66 1186 asm volatile(
9c9e467d
MN
1187 "pxor %%mm6, %%mm6 \n\t"
1188 "pcmpeqb %%mm7, %%mm7 \n\t"
1189 "movq %2, %%mm0 \n\t"
1190 "punpcklbw %%mm6, %%mm0 \n\t"
1191 "psrlw $1, %%mm0 \n\t"
1192 "psubw %%mm7, %%mm0 \n\t"
1193 "packuswb %%mm0, %%mm0 \n\t"
1194 "movq %%mm0, %3 \n\t"
70c5ae87 1195
053dea12
AJ
1196 "lea (%0, %1), %%"REG_a" \n\t"
1197 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
115329f1 1198
3057fa66 1199// 0 1 2 3 4 5 6 7 8 9
9c9e467d 1200// %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
3057fa66 1201
cc9b0679 1202#undef FIND_MIN_MAX
e0f8ffae 1203#ifdef HAVE_MMX2
053dea12 1204#define REAL_FIND_MIN_MAX(addr)\
70c5ae87 1205 "movq " #addr ", %%mm0 \n\t"\
cd38e322
MN
1206 "pminub %%mm0, %%mm7 \n\t"\
1207 "pmaxub %%mm0, %%mm6 \n\t"
e0f8ffae 1208#else
053dea12 1209#define REAL_FIND_MIN_MAX(addr)\
e0f8ffae 1210 "movq " #addr ", %%mm0 \n\t"\
cd38e322
MN
1211 "movq %%mm7, %%mm1 \n\t"\
1212 "psubusb %%mm0, %%mm6 \n\t"\
1213 "paddb %%mm0, %%mm6 \n\t"\
e0f8ffae 1214 "psubusb %%mm0, %%mm1 \n\t"\
cd38e322 1215 "psubb %%mm1, %%mm7 \n\t"
e0f8ffae 1216#endif
053dea12 1217#define FIND_MIN_MAX(addr) REAL_FIND_MIN_MAX(addr)
3057fa66 1218
053dea12
AJ
1219FIND_MIN_MAX((%%REGa))
1220FIND_MIN_MAX((%%REGa, %1))
1221FIND_MIN_MAX((%%REGa, %1, 2))
70c5ae87 1222FIND_MIN_MAX((%0, %1, 4))
053dea12
AJ
1223FIND_MIN_MAX((%%REGd))
1224FIND_MIN_MAX((%%REGd, %1))
1225FIND_MIN_MAX((%%REGd, %1, 2))
70c5ae87 1226FIND_MIN_MAX((%0, %1, 8))
3057fa66 1227
3057fa66 1228 "movq %%mm7, %%mm4 \n\t"
e5c30e06 1229 "psrlq $8, %%mm7 \n\t"
e5c30e06 1230#ifdef HAVE_MMX2
cd38e322 1231 "pminub %%mm4, %%mm7 \n\t" // min of pixels
e5c30e06 1232 "pshufw $0xF9, %%mm7, %%mm4 \n\t"
cd38e322 1233 "pminub %%mm4, %%mm7 \n\t" // min of pixels
e5c30e06 1234 "pshufw $0xFE, %%mm7, %%mm4 \n\t"
cd38e322 1235 "pminub %%mm4, %%mm7 \n\t"
e5c30e06 1236#else
cd38e322
MN
1237 "movq %%mm7, %%mm1 \n\t"
1238 "psubusb %%mm4, %%mm1 \n\t"
1239 "psubb %%mm1, %%mm7 \n\t"
3057fa66
A
1240 "movq %%mm7, %%mm4 \n\t"
1241 "psrlq $16, %%mm7 \n\t"
cd38e322
MN
1242 "movq %%mm7, %%mm1 \n\t"
1243 "psubusb %%mm4, %%mm1 \n\t"
1244 "psubb %%mm1, %%mm7 \n\t"
3057fa66 1245 "movq %%mm7, %%mm4 \n\t"
e5c30e06 1246 "psrlq $32, %%mm7 \n\t"
cd38e322
MN
1247 "movq %%mm7, %%mm1 \n\t"
1248 "psubusb %%mm4, %%mm1 \n\t"
1249 "psubb %%mm1, %%mm7 \n\t"
e5c30e06 1250#endif
cd38e322
MN
1251
1252
1253 "movq %%mm6, %%mm4 \n\t"
1254 "psrlq $8, %%mm6 \n\t"
1255#ifdef HAVE_MMX2
1256 "pmaxub %%mm4, %%mm6 \n\t" // max of pixels
1257 "pshufw $0xF9, %%mm6, %%mm4 \n\t"
1258 "pmaxub %%mm4, %%mm6 \n\t"
1259 "pshufw $0xFE, %%mm6, %%mm4 \n\t"
1260 "pmaxub %%mm4, %%mm6 \n\t"
1261#else
1262 "psubusb %%mm4, %%mm6 \n\t"
1263 "paddb %%mm4, %%mm6 \n\t"
1264 "movq %%mm6, %%mm4 \n\t"
1265 "psrlq $16, %%mm6 \n\t"
1266 "psubusb %%mm4, %%mm6 \n\t"
1267 "paddb %%mm4, %%mm6 \n\t"
1268 "movq %%mm6, %%mm4 \n\t"
1269 "psrlq $32, %%mm6 \n\t"
1270 "psubusb %%mm4, %%mm6 \n\t"
1271 "paddb %%mm4, %%mm6 \n\t"
1272#endif
1273 "movq %%mm6, %%mm0 \n\t" // max
1274 "psubb %%mm7, %%mm6 \n\t" // max - min
1275 "movd %%mm6, %%ecx \n\t"
9b464428 1276 "cmpb "MANGLE(deringThreshold)", %%cl \n\t"
cd38e322 1277 " jb 1f \n\t"
053dea12 1278 "lea -24(%%"REG_SP"), %%"REG_c" \n\t"
115329f1 1279 "and "ALIGN_MASK", %%"REG_c" \n\t"
cd38e322 1280 PAVGB(%%mm0, %%mm7) // a=(max + min)/2
e5c30e06
MN
1281 "punpcklbw %%mm7, %%mm7 \n\t"
1282 "punpcklbw %%mm7, %%mm7 \n\t"
1283 "punpcklbw %%mm7, %%mm7 \n\t"
053dea12 1284 "movq %%mm7, (%%"REG_c") \n\t"
70c5ae87
MN
1285
1286 "movq (%0), %%mm0 \n\t" // L10
1287 "movq %%mm0, %%mm1 \n\t" // L10
1288 "movq %%mm0, %%mm2 \n\t" // L10
1289 "psllq $8, %%mm1 \n\t"
1290 "psrlq $8, %%mm2 \n\t"
1291 "movd -4(%0), %%mm3 \n\t"
1292 "movd 8(%0), %%mm4 \n\t"
1293 "psrlq $24, %%mm3 \n\t"
1294 "psllq $56, %%mm4 \n\t"
1295 "por %%mm3, %%mm1 \n\t" // L00
1296 "por %%mm4, %%mm2 \n\t" // L20
1297 "movq %%mm1, %%mm3 \n\t" // L00
1298 PAVGB(%%mm2, %%mm1) // (L20 + L00)/2
1299 PAVGB(%%mm0, %%mm1) // (L20 + L00 + 2L10)/4
1300 "psubusb %%mm7, %%mm0 \n\t"
1301 "psubusb %%mm7, %%mm2 \n\t"
1302 "psubusb %%mm7, %%mm3 \n\t"
9b464428
FB
1303 "pcmpeqb "MANGLE(b00)", %%mm0 \n\t" // L10 > a ? 0 : -1
1304 "pcmpeqb "MANGLE(b00)", %%mm2 \n\t" // L20 > a ? 0 : -1
1305 "pcmpeqb "MANGLE(b00)", %%mm3 \n\t" // L00 > a ? 0 : -1
70c5ae87
MN
1306 "paddb %%mm2, %%mm0 \n\t"
1307 "paddb %%mm3, %%mm0 \n\t"
1308
053dea12 1309 "movq (%%"REG_a"), %%mm2 \n\t" // L11
70c5ae87
MN
1310 "movq %%mm2, %%mm3 \n\t" // L11
1311 "movq %%mm2, %%mm4 \n\t" // L11
1312 "psllq $8, %%mm3 \n\t"
1313 "psrlq $8, %%mm4 \n\t"
053dea12
AJ
1314 "movd -4(%%"REG_a"), %%mm5 \n\t"
1315 "movd 8(%%"REG_a"), %%mm6 \n\t"
70c5ae87
MN
1316 "psrlq $24, %%mm5 \n\t"
1317 "psllq $56, %%mm6 \n\t"
1318 "por %%mm5, %%mm3 \n\t" // L01
1319 "por %%mm6, %%mm4 \n\t" // L21
1320 "movq %%mm3, %%mm5 \n\t" // L01
1321 PAVGB(%%mm4, %%mm3) // (L21 + L01)/2
1322 PAVGB(%%mm2, %%mm3) // (L21 + L01 + 2L11)/4
1323 "psubusb %%mm7, %%mm2 \n\t"
1324 "psubusb %%mm7, %%mm4 \n\t"
1325 "psubusb %%mm7, %%mm5 \n\t"
9b464428
FB
1326 "pcmpeqb "MANGLE(b00)", %%mm2 \n\t" // L11 > a ? 0 : -1
1327 "pcmpeqb "MANGLE(b00)", %%mm4 \n\t" // L21 > a ? 0 : -1
1328 "pcmpeqb "MANGLE(b00)", %%mm5 \n\t" // L01 > a ? 0 : -1
70c5ae87
MN
1329 "paddb %%mm4, %%mm2 \n\t"
1330 "paddb %%mm5, %%mm2 \n\t"
1331// 0, 2, 3, 1
053dea12 1332#define REAL_DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1) \
70c5ae87
MN
1333 "movq " #src ", " #sx " \n\t" /* src[0] */\
1334 "movq " #sx ", " #lx " \n\t" /* src[0] */\
1335 "movq " #sx ", " #t0 " \n\t" /* src[0] */\
1336 "psllq $8, " #lx " \n\t"\
1337 "psrlq $8, " #t0 " \n\t"\
1338 "movd -4" #src ", " #t1 " \n\t"\
1339 "psrlq $24, " #t1 " \n\t"\
1340 "por " #t1 ", " #lx " \n\t" /* src[-1] */\
1341 "movd 8" #src ", " #t1 " \n\t"\
1342 "psllq $56, " #t1 " \n\t"\
1343 "por " #t1 ", " #t0 " \n\t" /* src[+1] */\
1344 "movq " #lx ", " #t1 " \n\t" /* src[-1] */\
1345 PAVGB(t0, lx) /* (src[-1] + src[+1])/2 */\
1346 PAVGB(sx, lx) /* (src[-1] + 2src[0] + src[+1])/4 */\
9927c7ee 1347 PAVGB(lx, pplx) \
053dea12
AJ
1348 "movq " #lx ", 8(%%"REG_c") \n\t"\
1349 "movq (%%"REG_c"), " #lx " \n\t"\
8405b3fd
MN
1350 "psubusb " #lx ", " #t1 " \n\t"\
1351 "psubusb " #lx ", " #t0 " \n\t"\
1352 "psubusb " #lx ", " #sx " \n\t"\
9b464428 1353 "movq "MANGLE(b00)", " #lx " \n\t"\
8405b3fd
MN
1354 "pcmpeqb " #lx ", " #t1 " \n\t" /* src[-1] > a ? 0 : -1*/\
1355 "pcmpeqb " #lx ", " #t0 " \n\t" /* src[+1] > a ? 0 : -1*/\
1356 "pcmpeqb " #lx ", " #sx " \n\t" /* src[0] > a ? 0 : -1*/\
70c5ae87
MN
1357 "paddb " #t1 ", " #t0 " \n\t"\
1358 "paddb " #t0 ", " #sx " \n\t"\
1359\
70c5ae87
MN
1360 PAVGB(plx, pplx) /* filtered */\
1361 "movq " #dst ", " #t0 " \n\t" /* dst */\
2e212618 1362 "movq " #t0 ", " #t1 " \n\t" /* dst */\
9c9e467d
MN
1363 "psubusb %3, " #t0 " \n\t"\
1364 "paddusb %3, " #t1 " \n\t"\
2e212618
MN
1365 PMAXUB(t0, pplx)\
1366 PMINUB(t1, pplx, t0)\
70c5ae87
MN
1367 "paddb " #sx ", " #ppsx " \n\t"\
1368 "paddb " #psx ", " #ppsx " \n\t"\
9b464428
FB
1369 "#paddb "MANGLE(b02)", " #ppsx " \n\t"\
1370 "pand "MANGLE(b08)", " #ppsx " \n\t"\
8405b3fd 1371 "pcmpeqb " #lx ", " #ppsx " \n\t"\
2e212618 1372 "pand " #ppsx ", " #pplx " \n\t"\
70c5ae87 1373 "pandn " #dst ", " #ppsx " \n\t"\
8405b3fd 1374 "por " #pplx ", " #ppsx " \n\t"\
9927c7ee 1375 "movq " #ppsx ", " #dst " \n\t"\
053dea12 1376 "movq 8(%%"REG_c"), " #lx " \n\t"
2e212618 1377
053dea12
AJ
1378#define DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1) \
1379 REAL_DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1)
70c5ae87
MN
1380/*
13810000000
13821111111
e5c30e06 1383
70c5ae87
MN
13841111110
13851111101
13861111100
13871111011
13881111010
13891111001
e5c30e06 1390
70c5ae87
MN
13911111000
13921110111
e5c30e06 1393
70c5ae87
MN
1394*/
1395//DERING_CORE(dst,src ,ppsx ,psx ,sx ,pplx ,plx ,lx ,t0 ,t1)
053dea12
AJ
1396DERING_CORE((%%REGa),(%%REGa, %1) ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
1397DERING_CORE((%%REGa, %1),(%%REGa, %1, 2) ,%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
1398DERING_CORE((%%REGa, %1, 2),(%0, %1, 4) ,%%mm4,%%mm0,%%mm2,%%mm5,%%mm1,%%mm3,%%mm6,%%mm7)
1399DERING_CORE((%0, %1, 4),(%%REGd) ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
1400DERING_CORE((%%REGd),(%%REGd, %1) ,%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
1401DERING_CORE((%%REGd, %1), (%%REGd, %1, 2),%%mm4,%%mm0,%%mm2,%%mm5,%%mm1,%%mm3,%%mm6,%%mm7)
1402DERING_CORE((%%REGd, %1, 2),(%0, %1, 8) ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
1403DERING_CORE((%0, %1, 8),(%%REGd, %1, 4) ,%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
3057fa66 1404
cd38e322 1405 "1: \n\t"
053dea12
AJ
1406 : : "r" (src), "r" ((long)stride), "m" (c->pQPb), "m"(c->pQPb2)
1407 : "%"REG_a, "%"REG_d, "%"REG_c
3057fa66 1408 );
64c968da 1409#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
2e212618
MN
1410 int y;
1411 int min=255;
1412 int max=0;
1413 int avg;
1414 uint8_t *p;
1415 int s[10];
9c9e467d 1416 const int QP2= c->QP/2 + 1;
2e212618
MN
1417
1418 for(y=1; y<9; y++)
1419 {
1420 int x;
1421 p= src + stride*y;
1422 for(x=1; x<9; x++)
1423 {
1424 p++;
1425 if(*p > max) max= *p;
1426 if(*p < min) min= *p;
1427 }
1428 }
9c9e467d 1429 avg= (min + max + 1)>>1;
2e212618 1430
cd38e322
MN
1431 if(max - min <deringThreshold) return;
1432
2e212618
MN
1433 for(y=0; y<10; y++)
1434 {
2e212618 1435 int t = 0;
9c9e467d
MN
1436
1437 if(src[stride*y + 0] > avg) t+= 1;
1438 if(src[stride*y + 1] > avg) t+= 2;
1439 if(src[stride*y + 2] > avg) t+= 4;
1440 if(src[stride*y + 3] > avg) t+= 8;
1441 if(src[stride*y + 4] > avg) t+= 16;
1442 if(src[stride*y + 5] > avg) t+= 32;
1443 if(src[stride*y + 6] > avg) t+= 64;
1444 if(src[stride*y + 7] > avg) t+= 128;
1445 if(src[stride*y + 8] > avg) t+= 256;
1446 if(src[stride*y + 9] > avg) t+= 512;
115329f1 1447
2e212618
MN
1448 t |= (~t)<<16;
1449 t &= (t<<1) & (t>>1);
1450 s[y] = t;
1451 }
115329f1 1452
2e212618
MN
1453 for(y=1; y<9; y++)
1454 {
2e212618
MN
1455 int t = s[y-1] & s[y] & s[y+1];
1456 t|= t>>16;
9c9e467d
MN
1457 s[y-1]= t;
1458 }
1459
1460 for(y=1; y<9; y++)
1461 {
1462 int x;
1463 int t = s[y-1];
2e212618
MN
1464
1465 p= src + stride*y;
1466 for(x=1; x<9; x++)
1467 {
1468 p++;
1469 if(t & (1<<x))
1470 {
1471 int f= (*(p-stride-1)) + 2*(*(p-stride)) + (*(p-stride+1))
1472 +2*(*(p -1)) + 4*(*p ) + 2*(*(p +1))
1473 +(*(p+stride-1)) + 2*(*(p+stride)) + (*(p+stride+1));
1474 f= (f + 8)>>4;
1475
cd38e322
MN
1476#ifdef DEBUG_DERING_THRESHOLD
1477 asm volatile("emms\n\t":);
1478 {
1479 static long long numPixels=0;
1480 if(x!=1 && x!=8 && y!=1 && y!=8) numPixels++;
1481// if((max-min)<20 || (max-min)*QP<200)
1482// if((max-min)*QP < 500)
1483// if(max-min<QP/2)
1484 if(max-min < 20)
1485 {
1486 static int numSkiped=0;
1487 static int errorSum=0;
1488 static int worstQP=0;
1489 static int worstRange=0;
1490 static int worstDiff=0;
1491 int diff= (f - *p);
1492 int absDiff= ABS(diff);
1493 int error= diff*diff;
1494
1495 if(x==1 || x==8 || y==1 || y==8) continue;
1496
1497 numSkiped++;
1498 if(absDiff > worstDiff)
1499 {
1500 worstDiff= absDiff;
1501 worstQP= QP;
1502 worstRange= max-min;
1503 }
1504 errorSum+= error;
1505
1506 if(1024LL*1024LL*1024LL % numSkiped == 0)
1507 {
1508 printf( "sum:%1.3f, skip:%d, wQP:%d, "
1509 "wRange:%d, wDiff:%d, relSkip:%1.3f\n",
1510 (float)errorSum/numSkiped, numSkiped, worstQP, worstRange,
1511 worstDiff, (float)numSkiped/numPixels);
1512 }
1513 }
1514 }
1515#endif
9c9e467d
MN
1516 if (*p + QP2 < f) *p= *p + QP2;
1517 else if(*p - QP2 > f) *p= *p - QP2;
2e212618
MN
1518 else *p=f;
1519 }
1520 }
1521 }
cd38e322
MN
1522#ifdef DEBUG_DERING_THRESHOLD
1523 if(max-min < 20)
1524 {
1525 for(y=1; y<9; y++)
1526 {
1527 int x;
1528 int t = 0;
1529 p= src + stride*y;
1530 for(x=1; x<9; x++)
1531 {
1532 p++;
1533 *p = MIN(*p + 20, 255);
1534 }
1535 }
1536// src[0] = src[7]=src[stride*7]=src[stride*7 + 7]=255;
1537 }
1538#endif
64c968da 1539#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
3057fa66 1540}
b0ac780a 1541#endif //HAVE_ALTIVEC
3057fa66 1542
3b58b885 1543/**
b304569a 1544 * Deinterlaces the given block by linearly interpolating every second line.
7fb36f6c
MN
1545 * will be called for every 8x8 block and can read & write from line 4-15
1546 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
1547 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
3b58b885 1548 */
cc9b0679 1549static inline void RENAME(deInterlaceInterpolateLinear)(uint8_t src[], int stride)
3b58b885
MN
1550{
1551#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
7fb36f6c 1552 src+= 4*stride;
3b58b885 1553 asm volatile(
053dea12
AJ
1554 "lea (%0, %1), %%"REG_a" \n\t"
1555 "lea (%%"REG_a", %1, 4), %%"REG_c" \n\t"
3b58b885 1556// 0 1 2 3 4 5 6 7 8 9
9c9e467d 1557// %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1 %0+8%1 ecx+4%1
3b58b885
MN
1558
1559 "movq (%0), %%mm0 \n\t"
053dea12 1560 "movq (%%"REG_a", %1), %%mm1 \n\t"
acced553 1561 PAVGB(%%mm1, %%mm0)
053dea12 1562 "movq %%mm0, (%%"REG_a") \n\t"
3b58b885 1563 "movq (%0, %1, 4), %%mm0 \n\t"
acced553 1564 PAVGB(%%mm0, %%mm1)
053dea12
AJ
1565 "movq %%mm1, (%%"REG_a", %1, 2) \n\t"
1566 "movq (%%"REG_c", %1), %%mm1 \n\t"
acced553 1567 PAVGB(%%mm1, %%mm0)
053dea12 1568 "movq %%mm0, (%%"REG_c") \n\t"
3b58b885 1569 "movq (%0, %1, 8), %%mm0 \n\t"
acced553 1570 PAVGB(%%mm0, %%mm1)
053dea12 1571 "movq %%mm1, (%%"REG_c", %1, 2) \n\t"
3b58b885 1572
053dea12
AJ
1573 : : "r" (src), "r" ((long)stride)
1574 : "%"REG_a, "%"REG_c
3b58b885
MN
1575 );
1576#else
99d33fa3 1577 int a, b, x;
7fb36f6c 1578 src+= 4*stride;
99d33fa3
MN
1579
1580 for(x=0; x<2; x++){
1581 a= *(uint32_t*)&src[stride*0];
1582 b= *(uint32_t*)&src[stride*2];
1583 *(uint32_t*)&src[stride*1]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
1584 a= *(uint32_t*)&src[stride*4];
1585 *(uint32_t*)&src[stride*3]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
1586 b= *(uint32_t*)&src[stride*6];
1587 *(uint32_t*)&src[stride*5]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
1588 a= *(uint32_t*)&src[stride*8];
1589 *(uint32_t*)&src[stride*7]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
1590 src += 4;
3b58b885
MN
1591 }
1592#endif
1593}
1594
1595/**
b304569a 1596 * Deinterlaces the given block by cubic interpolating every second line.
7fb36f6c
MN
1597 * will be called for every 8x8 block and can read & write from line 4-15
1598 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
1599 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1600 * this filter will read lines 3-15 and write 7-13
3b58b885 1601 */
cc9b0679 1602static inline void RENAME(deInterlaceInterpolateCubic)(uint8_t src[], int stride)
3b58b885
MN
1603{
1604#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
7fb36f6c 1605 src+= stride*3;
3b58b885 1606 asm volatile(
053dea12
AJ
1607 "lea (%0, %1), %%"REG_a" \n\t"
1608 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
1609 "lea (%%"REG_d", %1, 4), %%"REG_c" \n\t"
1610 "add %1, %%"REG_c" \n\t"
acced553
MN
1611 "pxor %%mm7, %%mm7 \n\t"
1612// 0 1 2 3 4 5 6 7 8 9 10
9c9e467d 1613// %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1 ecx
3b58b885 1614
053dea12 1615#define REAL_DEINT_CUBIC(a,b,c,d,e)\
acced553
MN
1616 "movq " #a ", %%mm0 \n\t"\
1617 "movq " #b ", %%mm1 \n\t"\
1618 "movq " #d ", %%mm2 \n\t"\
1619 "movq " #e ", %%mm3 \n\t"\
1620 PAVGB(%%mm2, %%mm1) /* (b+d) /2 */\
1621 PAVGB(%%mm3, %%mm0) /* a(a+e) /2 */\
1622 "movq %%mm0, %%mm2 \n\t"\
1623 "punpcklbw %%mm7, %%mm0 \n\t"\
1624 "punpckhbw %%mm7, %%mm2 \n\t"\
1625 "movq %%mm1, %%mm3 \n\t"\
1626 "punpcklbw %%mm7, %%mm1 \n\t"\
1627 "punpckhbw %%mm7, %%mm3 \n\t"\
1628 "psubw %%mm1, %%mm0 \n\t" /* L(a+e - (b+d))/2 */\
1629 "psubw %%mm3, %%mm2 \n\t" /* H(a+e - (b+d))/2 */\
1630 "psraw $3, %%mm0 \n\t" /* L(a+e - (b+d))/16 */\
1631 "psraw $3, %%mm2 \n\t" /* H(a+e - (b+d))/16 */\
1632 "psubw %%mm0, %%mm1 \n\t" /* L(9b + 9d - a - e)/16 */\
1633 "psubw %%mm2, %%mm3 \n\t" /* H(9b + 9d - a - e)/16 */\
1634 "packuswb %%mm3, %%mm1 \n\t"\
1635 "movq %%mm1, " #c " \n\t"
053dea12 1636#define DEINT_CUBIC(a,b,c,d,e) REAL_DEINT_CUBIC(a,b,c,d,e)
acced553 1637
053dea12
AJ
1638DEINT_CUBIC((%0), (%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4), (%%REGd, %1))
1639DEINT_CUBIC((%%REGa, %1), (%0, %1, 4), (%%REGd), (%%REGd, %1), (%0, %1, 8))
1640DEINT_CUBIC((%0, %1, 4), (%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8), (%%REGc))
1641DEINT_CUBIC((%%REGd, %1), (%0, %1, 8), (%%REGd, %1, 4), (%%REGc), (%%REGc, %1, 2))
3b58b885 1642
053dea12
AJ
1643 : : "r" (src), "r" ((long)stride)
1644 : "%"REG_a, "%"REG_d, "%"REG_c
3b58b885 1645 );
64c968da 1646#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
3b58b885 1647 int x;
7fb36f6c 1648 src+= stride*3;
3b58b885
MN
1649 for(x=0; x<8; x++)
1650 {
134eb1e5
MN
1651 src[stride*3] = CLIP((-src[0] + 9*src[stride*2] + 9*src[stride*4] - src[stride*6])>>4);
1652 src[stride*5] = CLIP((-src[stride*2] + 9*src[stride*4] + 9*src[stride*6] - src[stride*8])>>4);
1653 src[stride*7] = CLIP((-src[stride*4] + 9*src[stride*6] + 9*src[stride*8] - src[stride*10])>>4);
1654 src[stride*9] = CLIP((-src[stride*6] + 9*src[stride*8] + 9*src[stride*10] - src[stride*12])>>4);
3b58b885
MN
1655 src++;
1656 }
64c968da 1657#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
3b58b885
MN
1658}
1659
1660/**
b304569a 1661 * Deinterlaces the given block by filtering every second line with a (-1 4 2 4 -1) filter.
7fb36f6c
MN
1662 * will be called for every 8x8 block and can read & write from line 4-15
1663 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
1664 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
9c9e467d 1665 * this filter will read lines 4-13 and write 5-11
9c9e467d
MN
1666 */
1667static inline void RENAME(deInterlaceFF)(uint8_t src[], int stride, uint8_t *tmp)
1668{
1669#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1670 src+= stride*4;
1671 asm volatile(
053dea12
AJ
1672 "lea (%0, %1), %%"REG_a" \n\t"
1673 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
9c9e467d
MN
1674 "pxor %%mm7, %%mm7 \n\t"
1675 "movq (%2), %%mm0 \n\t"
1676// 0 1 2 3 4 5 6 7 8 9 10
1677// %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1 ecx
1678
053dea12 1679#define REAL_DEINT_FF(a,b,c,d)\
9c9e467d
MN
1680 "movq " #a ", %%mm1 \n\t"\
1681 "movq " #b ", %%mm2 \n\t"\
1682 "movq " #c ", %%mm3 \n\t"\
1683 "movq " #d ", %%mm4 \n\t"\
1684 PAVGB(%%mm3, %%mm1) \
1685 PAVGB(%%mm4, %%mm0) \
1686 "movq %%mm0, %%mm3 \n\t"\
1687 "punpcklbw %%mm7, %%mm0 \n\t"\
1688 "punpckhbw %%mm7, %%mm3 \n\t"\
1689 "movq %%mm1, %%mm4 \n\t"\
1690 "punpcklbw %%mm7, %%mm1 \n\t"\
1691 "punpckhbw %%mm7, %%mm4 \n\t"\
1692 "psllw $2, %%mm1 \n\t"\
1693 "psllw $2, %%mm4 \n\t"\
1694 "psubw %%mm0, %%mm1 \n\t"\
1695 "psubw %%mm3, %%mm4 \n\t"\
1696 "movq %%mm2, %%mm5 \n\t"\
1697 "movq %%mm2, %%mm0 \n\t"\
1698 "punpcklbw %%mm7, %%mm2 \n\t"\
1699 "punpckhbw %%mm7, %%mm5 \n\t"\
1700 "paddw %%mm2, %%mm1 \n\t"\
1701 "paddw %%mm5, %%mm4 \n\t"\
1702 "psraw $2, %%mm1 \n\t"\
1703 "psraw $2, %%mm4 \n\t"\
1704 "packuswb %%mm4, %%mm1 \n\t"\
1705 "movq %%mm1, " #b " \n\t"\
1706
053dea12
AJ
1707#define DEINT_FF(a,b,c,d) REAL_DEINT_FF(a,b,c,d)
1708
1709DEINT_FF((%0) , (%%REGa) , (%%REGa, %1), (%%REGa, %1, 2))
1710DEINT_FF((%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4), (%%REGd) )
1711DEINT_FF((%0, %1, 4), (%%REGd) , (%%REGd, %1), (%%REGd, %1, 2))
1712DEINT_FF((%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8), (%%REGd, %1, 4))
9c9e467d
MN
1713
1714 "movq %%mm0, (%2) \n\t"
053dea12
AJ
1715 : : "r" (src), "r" ((long)stride), "r"(tmp)
1716 : "%"REG_a, "%"REG_d
9c9e467d 1717 );
64c968da 1718#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
9c9e467d
MN
1719 int x;
1720 src+= stride*4;
1721 for(x=0; x<8; x++)
1722 {
1723 int t1= tmp[x];
1724 int t2= src[stride*1];
1725
134eb1e5 1726 src[stride*1]= CLIP((-t1 + 4*src[stride*0] + 2*t2 + 4*src[stride*2] - src[stride*3] + 4)>>3);
9c9e467d 1727 t1= src[stride*4];
134eb1e5 1728 src[stride*3]= CLIP((-t2 + 4*src[stride*2] + 2*t1 + 4*src[stride*4] - src[stride*5] + 4)>>3);
9c9e467d 1729 t2= src[stride*6];
134eb1e5 1730 src[stride*5]= CLIP((-t1 + 4*src[stride*4] + 2*t2 + 4*src[stride*6] - src[stride*7] + 4)>>3);
9c9e467d 1731 t1= src[stride*8];
134eb1e5 1732 src[stride*7]= CLIP((-t2 + 4*src[stride*6] + 2*t1 + 4*src[stride*8] - src[stride*9] + 4)>>3);
9c9e467d
MN
1733 tmp[x]= t1;
1734
1735 src++;
1736 }
64c968da 1737#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
9c9e467d
MN
1738}
1739
1740/**
134eb1e5
MN
1741 * Deinterlaces the given block by filtering every line with a (-1 2 6 2 -1) filter.
1742 * will be called for every 8x8 block and can read & write from line 4-15
1743 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
1744 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1745 * this filter will read lines 4-13 and write 4-11
1746 */
1747static inline void RENAME(deInterlaceL5)(uint8_t src[], int stride, uint8_t *tmp, uint8_t *tmp2)
1748{
1749#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1750 src+= stride*4;
1751 asm volatile(
053dea12
AJ
1752 "lea (%0, %1), %%"REG_a" \n\t"
1753 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
134eb1e5
MN
1754 "pxor %%mm7, %%mm7 \n\t"
1755 "movq (%2), %%mm0 \n\t"
1756 "movq (%3), %%mm1 \n\t"
1757// 0 1 2 3 4 5 6 7 8 9 10
1758// %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1 ecx
1759
053dea12 1760#define REAL_DEINT_L5(t1,t2,a,b,c)\
134eb1e5
MN
1761 "movq " #a ", %%mm2 \n\t"\
1762 "movq " #b ", %%mm3 \n\t"\
1763 "movq " #c ", %%mm4 \n\t"\
1764 PAVGB(t2, %%mm3) \
1765 PAVGB(t1, %%mm4) \
1766 "movq %%mm2, %%mm5 \n\t"\
1767 "movq %%mm2, " #t1 " \n\t"\
1768 "punpcklbw %%mm7, %%mm2 \n\t"\
1769 "punpckhbw %%mm7, %%mm5 \n\t"\
1770 "movq %%mm2, %%mm6 \n\t"\
1771 "paddw %%mm2, %%mm2 \n\t"\
1772 "paddw %%mm6, %%mm2 \n\t"\
1773 "movq %%mm5, %%mm6 \n\t"\
1774 "paddw %%mm5, %%mm5 \n\t"\
1775 "paddw %%mm6, %%mm5 \n\t"\
1776 "movq %%mm3, %%mm6 \n\t"\
1777 "punpcklbw %%mm7, %%mm3 \n\t"\
1778 "punpckhbw %%mm7, %%mm6 \n\t"\
1779 "paddw %%mm3, %%mm3 \n\t"\
1780 "paddw %%mm6, %%mm6 \n\t"\
1781 "paddw %%mm3, %%mm2 \n\t"\
1782 "paddw %%mm6, %%mm5 \n\t"\
1783 "movq %%mm4, %%mm6 \n\t"\
1784 "punpcklbw %%mm7, %%mm4 \n\t"\
1785 "punpckhbw %%mm7, %%mm6 \n\t"\
1786 "psubw %%mm4, %%mm2 \n\t"\
1787 "psubw %%mm6, %%mm5 \n\t"\
1788 "psraw $2, %%mm2 \n\t"\
1789 "psraw $2, %%mm5 \n\t"\
1790 "packuswb %%mm5, %%mm2 \n\t"\
1791 "movq %%mm2, " #a " \n\t"\
1792
053dea12
AJ
1793#define DEINT_L5(t1,t2,a,b,c) REAL_DEINT_L5(t1,t2,a,b,c)
1794
1795DEINT_L5(%%mm0, %%mm1, (%0) , (%%REGa) , (%%REGa, %1) )
1796DEINT_L5(%%mm1, %%mm0, (%%REGa) , (%%REGa, %1) , (%%REGa, %1, 2))
1797DEINT_L5(%%mm0, %%mm1, (%%REGa, %1) , (%%REGa, %1, 2), (%0, %1, 4) )
1798DEINT_L5(%%mm1, %%mm0, (%%REGa, %1, 2), (%0, %1, 4) , (%%REGd) )
115329f1 1799DEINT_L5(%%mm0, %%mm1, (%0, %1, 4) , (%%REGd) , (%%REGd, %1) )
053dea12
AJ
1800DEINT_L5(%%mm1, %%mm0, (%%REGd) , (%%REGd, %1) , (%%REGd, %1, 2))
1801DEINT_L5(%%mm0, %%mm1, (%%REGd, %1) , (%%REGd, %1, 2), (%0, %1, 8) )
1802DEINT_L5(%%mm1, %%mm0, (%%REGd, %1, 2), (%0, %1, 8) , (%%REGd, %1, 4))
134eb1e5
MN
1803
1804 "movq %%mm0, (%2) \n\t"
1805 "movq %%mm1, (%3) \n\t"
053dea12
AJ
1806 : : "r" (src), "r" ((long)stride), "r"(tmp), "r"(tmp2)
1807 : "%"REG_a, "%"REG_d
134eb1e5 1808 );
64c968da 1809#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
134eb1e5
MN
1810 int x;
1811 src+= stride*4;
1812 for(x=0; x<8; x++)
1813 {
1814 int t1= tmp[x];
1815 int t2= tmp2[x];
1816 int t3= src[0];
1817
1818 src[stride*0]= CLIP((-(t1 + src[stride*2]) + 2*(t2 + src[stride*1]) + 6*t3 + 4)>>3);
1819 t1= src[stride*1];
1820 src[stride*1]= CLIP((-(t2 + src[stride*3]) + 2*(t3 + src[stride*2]) + 6*t1 + 4)>>3);
1821 t2= src[stride*2];
1822 src[stride*2]= CLIP((-(t3 + src[stride*4]) + 2*(t1 + src[stride*3]) + 6*t2 + 4)>>3);
1823 t3= src[stride*3];
1824 src[stride*3]= CLIP((-(t1 + src[stride*5]) + 2*(t2 + src[stride*4]) + 6*t3 + 4)>>3);
1825 t1= src[stride*4];
1826 src[stride*4]= CLIP((-(t2 + src[stride*6]) + 2*(t3 + src[stride*5]) + 6*t1 + 4)>>3);
1827 t2= src[stride*5];
1828 src[stride*5]= CLIP((-(t3 + src[stride*7]) + 2*(t1 + src[stride*6]) + 6*t2 + 4)>>3);
1829 t3= src[stride*6];
1830 src[stride*6]= CLIP((-(t1 + src[stride*8]) + 2*(t2 + src[stride*7]) + 6*t3 + 4)>>3);
1831 t1= src[stride*7];
1832 src[stride*7]= CLIP((-(t2 + src[stride*9]) + 2*(t3 + src[stride*8]) + 6*t1 + 4)>>3);
1833
1834 tmp[x]= t3;
1835 tmp2[x]= t1;
1836
1837 src++;
1838 }
64c968da 1839#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
134eb1e5
MN
1840}
1841
1842/**
b304569a 1843 * Deinterlaces the given block by filtering all lines with a (1 2 1) filter.
9c9e467d
MN
1844 * will be called for every 8x8 block and can read & write from line 4-15
1845 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
1846 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
7fb36f6c 1847 * this filter will read lines 4-13 and write 4-11
3b58b885 1848 */
13ba9ae4 1849static inline void RENAME(deInterlaceBlendLinear)(uint8_t src[], int stride, uint8_t *tmp)
3b58b885
MN
1850{
1851#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
7fb36f6c 1852 src+= 4*stride;
3b58b885 1853 asm volatile(
053dea12
AJ
1854 "lea (%0, %1), %%"REG_a" \n\t"
1855 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
3b58b885 1856// 0 1 2 3 4 5 6 7 8 9
9c9e467d 1857// %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
3b58b885 1858
13ba9ae4 1859 "movq (%2), %%mm0 \n\t" // L0
053dea12 1860 "movq (%%"REG_a"), %%mm1 \n\t" // L2
3b58b885 1861 PAVGB(%%mm1, %%mm0) // L0+L2
13ba9ae4 1862 "movq (%0), %%mm2 \n\t" // L1
3b58b885
MN
1863 PAVGB(%%mm2, %%mm0)
1864 "movq %%mm0, (%0) \n\t"
053dea12 1865 "movq (%%"REG_a", %1), %%mm0 \n\t" // L3
3b58b885
MN
1866 PAVGB(%%mm0, %%mm2) // L1+L3
1867 PAVGB(%%mm1, %%mm2) // 2L2 + L1 + L3
053dea12
AJ
1868 "movq %%mm2, (%%"REG_a") \n\t"
1869 "movq (%%"REG_a", %1, 2), %%mm2 \n\t" // L4
3b58b885
MN
1870 PAVGB(%%mm2, %%mm1) // L2+L4
1871 PAVGB(%%mm0, %%mm1) // 2L3 + L2 + L4
053dea12 1872 "movq %%mm1, (%%"REG_a", %1) \n\t"
13ba9ae4 1873 "movq (%0, %1, 4), %%mm1 \n\t" // L5
3b58b885
MN
1874 PAVGB(%%mm1, %%mm0) // L3+L5
1875 PAVGB(%%mm2, %%mm0) // 2L4 + L3 + L5
053dea12
AJ
1876 "movq %%mm0, (%%"REG_a", %1, 2) \n\t"
1877 "movq (%%"REG_d"), %%mm0 \n\t" // L6
3b58b885
MN
1878 PAVGB(%%mm0, %%mm2) // L4+L6
1879 PAVGB(%%mm1, %%mm2) // 2L5 + L4 + L6
1880 "movq %%mm2, (%0, %1, 4) \n\t"
053dea12 1881 "movq (%%"REG_d", %1), %%mm2 \n\t" // L7
3b58b885
MN
1882 PAVGB(%%mm2, %%mm1) // L5+L7
1883 PAVGB(%%mm0, %%mm1) // 2L6 + L5 + L7
053dea12
AJ
1884 "movq %%mm1, (%%"REG_d") \n\t"
1885 "movq (%%"REG_d", %1, 2), %%mm1 \n\t" // L8
3b58b885
MN
1886 PAVGB(%%mm1, %%mm0) // L6+L8
1887 PAVGB(%%mm2, %%mm0) // 2L7 + L6 + L8
053dea12 1888 "movq %%mm0, (%%"REG_d", %1) \n\t"
13ba9ae4 1889 "movq (%0, %1, 8), %%mm0 \n\t" // L9
3b58b885
MN
1890 PAVGB(%%mm0, %%mm2) // L7+L9
1891 PAVGB(%%mm1, %%mm2) // 2L8 + L7 + L9
053dea12 1892 "movq %%mm2, (%%"REG_d", %1, 2) \n\t"
13ba9ae4 1893 "movq %%mm1, (%2) \n\t"
3b58b885 1894
053dea12
AJ
1895 : : "r" (src), "r" ((long)stride), "r" (tmp)
1896 : "%"REG_a, "%"REG_d
3b58b885 1897 );
64c968da 1898#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
99d33fa3 1899 int a, b, c, x;
7fb36f6c 1900 src+= 4*stride;
99d33fa3
MN
1901
1902 for(x=0; x<2; x++){
13ba9ae4
MN
1903 a= *(uint32_t*)&tmp[stride*0];
1904 b= *(uint32_t*)&src[stride*0];
1905 c= *(uint32_t*)&src[stride*1];
99d33fa3
MN
1906 a= (a&c) + (((a^c)&0xFEFEFEFEUL)>>1);
1907 *(uint32_t*)&src[stride*0]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
1908
13ba9ae4 1909 a= *(uint32_t*)&src[stride*2];
99d33fa3
MN
1910 b= (a&b) + (((a^b)&0xFEFEFEFEUL)>>1);
1911 *(uint32_t*)&src[stride*1]= (c|b) - (((c^b)&0xFEFEFEFEUL)>>1);
1912
13ba9ae4 1913 b= *(uint32_t*)&src[stride*3];
99d33fa3
MN
1914 c= (b&c) + (((b^c)&0xFEFEFEFEUL)>>1);
1915 *(uint32_t*)&src[stride*2]= (c|a) - (((c^a)&0xFEFEFEFEUL)>>1);
1916
13ba9ae4 1917 c= *(uint32_t*)&src[stride*4];
99d33fa3
MN
1918 a= (a&c) + (((a^c)&0xFEFEFEFEUL)>>1);
1919 *(uint32_t*)&src[stride*3]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
1920
13ba9ae4 1921 a= *(uint32_t*)&src[stride*5];
99d33fa3
MN
1922 b= (a&b) + (((a^b)&0xFEFEFEFEUL)>>1);
1923 *(uint32_t*)&src[stride*4]= (c|b) - (((c^b)&0xFEFEFEFEUL)>>1);
1924
13ba9ae4 1925 b= *(uint32_t*)&src[stride*6];
99d33fa3
MN
1926 c= (b&c) + (((b^c)&0xFEFEFEFEUL)>>1);
1927 *(uint32_t*)&src[stride*5]= (c|a) - (((c^a)&0xFEFEFEFEUL)>>1);
1928
13ba9ae4 1929 c= *(uint32_t*)&src[stride*7];
99d33fa3
MN
1930 a= (a&c) + (((a^c)&0xFEFEFEFEUL)>>1);
1931 *(uint32_t*)&src[stride*6]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
1932
13ba9ae4 1933 a= *(uint32_t*)&src[stride*8];
99d33fa3
MN
1934 b= (a&b) + (((a^b)&0xFEFEFEFEUL)>>1);
1935 *(uint32_t*)&src[stride*7]= (c|b) - (((c^b)&0xFEFEFEFEUL)>>1);
1936
13ba9ae4 1937 *(uint32_t*)&tmp[stride*0]= c;
99d33fa3 1938 src += 4;
13ba9ae4 1939 tmp += 4;
3b58b885 1940 }
64c968da 1941#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
3b58b885
MN
1942}
1943
1944/**
b304569a 1945 * Deinterlaces the given block by applying a median filter to every second line.
7fb36f6c
MN
1946 * will be called for every 8x8 block and can read & write from line 4-15,
1947 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
1948 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
3b58b885 1949 */
cc9b0679 1950static inline void RENAME(deInterlaceMedian)(uint8_t src[], int stride)
3b58b885 1951{
a6be8111 1952#ifdef HAVE_MMX
7fb36f6c 1953 src+= 4*stride;
a6be8111 1954#ifdef HAVE_MMX2
3b58b885 1955 asm volatile(
053dea12
AJ
1956 "lea (%0, %1), %%"REG_a" \n\t"
1957 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
3b58b885 1958// 0 1 2 3 4 5 6 7 8 9
9c9e467d 1959// %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
3b58b885
MN
1960
1961 "movq (%0), %%mm0 \n\t" //
053dea12
AJ
1962 "movq (%%"REG_a", %1), %%mm2 \n\t" //
1963 "movq (%%"REG_a"), %%mm1 \n\t" //
3b58b885
MN
1964 "movq %%mm0, %%mm3 \n\t"
1965 "pmaxub %%mm1, %%mm0 \n\t" //
1966 "pminub %%mm3, %%mm1 \n\t" //
1967 "pmaxub %%mm2, %%mm1 \n\t" //
1968 "pminub %%mm1, %%mm0 \n\t"
053dea12 1969 "movq %%mm0, (%%"REG_a") \n\t"
3b58b885
MN
1970
1971 "movq (%0, %1, 4), %%mm0 \n\t" //
053dea12 1972 "movq (%%"REG_a", %1, 2), %%mm1 \n\t" //
3b58b885
MN
1973 "movq %%mm2, %%mm3 \n\t"
1974 "pmaxub %%mm1, %%mm2 \n\t" //
1975 "pminub %%mm3, %%mm1 \n\t" //
1976 "pmaxub %%mm0, %%mm1 \n\t" //
1977 "pminub %%mm1, %%mm2 \n\t"
053dea12 1978 "movq %%mm2, (%%"REG_a", %1, 2) \n\t"
3b58b885 1979
053dea12
AJ
1980 "movq (%%"REG_d"), %%mm2 \n\t" //
1981 "movq (%%"REG_d", %1), %%mm1 \n\t" //
3b58b885
MN
1982 "movq %%mm2, %%mm3 \n\t"
1983 "pmaxub %%mm0, %%mm2 \n\t" //
1984 "pminub %%mm3, %%mm0 \n\t" //
1985 "pmaxub %%mm1, %%mm0 \n\t" //
1986 "pminub %%mm0, %%mm2 \n\t"
053dea12 1987 "movq %%mm2, (%%"REG_d") \n\t"
3b58b885 1988
053dea12 1989 "movq (%%"REG_d", %1, 2), %%mm2 \n\t" //
3b58b885
MN
1990 "movq (%0, %1, 8), %%mm0 \n\t" //
1991 "movq %%mm2, %%mm3 \n\t"
1992 "pmaxub %%mm0, %%mm2 \n\t" //
1993 "pminub %%mm3, %%mm0 \n\t" //
1994 "pmaxub %%mm1, %%mm0 \n\t" //
1995 "pminub %%mm0, %%mm2 \n\t"
053dea12 1996 "movq %%mm2, (%%"REG_d", %1, 2) \n\t"
3b58b885
MN
1997
1998
053dea12
AJ
1999 : : "r" (src), "r" ((long)stride)
2000 : "%"REG_a, "%"REG_d
3b58b885 2001 );
a6be8111
MN
2002
2003#else // MMX without MMX2
2004 asm volatile(
053dea12
AJ
2005 "lea (%0, %1), %%"REG_a" \n\t"
2006 "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
a6be8111 2007// 0 1 2 3 4 5 6 7 8 9
9c9e467d 2008// %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
a6be8111
MN
2009 "pxor %%mm7, %%mm7 \n\t"
2010
053dea12 2011#define REAL_MEDIAN(a,b,c)\
a6be8111
MN
2012 "movq " #a ", %%mm0 \n\t"\
2013 "movq " #b ", %%mm2 \n\t"\
2014 "movq " #c ", %%mm1 \n\t"\
2015 "movq %%mm0, %%mm3 \n\t"\
2016 "movq %%mm1, %%mm4 \n\t"\
2017 "movq %%mm2, %%mm5 \n\t"\
2018 "psubusb %%mm1, %%mm3 \n\t"\
2019 "psubusb %%mm2, %%mm4 \n\t"\
2020 "psubusb %%mm0, %%mm5 \n\t"\
2021 "pcmpeqb %%mm7, %%mm3 \n\t"\
2022 "pcmpeqb %%mm7, %%mm4 \n\t"\
2023 "pcmpeqb %%mm7, %%mm5 \n\t"\
2024 "movq %%mm3, %%mm6 \n\t"\
2025 "pxor %%mm4, %%mm3 \n\t"\
2026 "pxor %%mm5, %%mm4 \n\t"\
2027 "pxor %%mm6, %%mm5 \n\t"\
2028 "por %%mm3, %%mm1 \n\t"\
2029 "por %%mm4, %%mm2 \n\t"\
2030 "por %%mm5, %%mm0 \n\t"\
2031 "pand %%mm2, %%mm0 \n\t"\
2032 "pand %%mm1, %%mm0 \n\t"\
2033 "movq %%mm0, " #b " \n\t"
053dea12 2034#define MEDIAN(a,b,c) REAL_MEDIAN(a,b,c)
a6be8111 2035
053dea12
AJ
2036MEDIAN((%0), (%%REGa), (%%REGa, %1))
2037MEDIAN((%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4))
2038MEDIAN((%0, %1, 4), (%%REGd), (%%REGd, %1))
2039MEDIAN((%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8))
a6be8111 2040
053dea12
AJ
2041 : : "r" (src), "r" ((long)stride)
2042 : "%"REG_a, "%"REG_d
a6be8111 2043 );
64c968da
DB
2044#endif //HAVE_MMX2
2045#else //HAVE_MMX
9b1663fc 2046 int x, y;
7fb36f6c 2047 src+= 4*stride;
9b1663fc 2048 // FIXME - there should be a way to do a few columns in parallel like w/mmx
3b58b885
MN
2049 for(x=0; x<8; x++)
2050 {
9b1663fc
RFI
2051 uint8_t *colsrc = src;
2052 for (y=0; y<4; y++)
2053 {
2054 int a, b, c, d, e, f;
2055 a = colsrc[0 ];
2056 b = colsrc[stride ];
2057 c = colsrc[stride*2];
2058 d = (a-b)>>31;
2059 e = (b-c)>>31;
2060 f = (c-a)>>31;
2061 colsrc[stride ] = (a|(d^f)) & (b|(d^e)) & (c|(e^f));
2062 colsrc += stride*2;
2063 }
3b58b885
MN
2064 src++;
2065 }
64c968da 2066#endif //HAVE_MMX
3b58b885
MN
2067}
2068
e5c30e06 2069#ifdef HAVE_MMX
4e4dcbc5
MN
2070/**
2071 * transposes and shift the given 8x8 Block into dst1 and dst2
2072 */
cc9b0679 2073static inline void RENAME(transpose1)(uint8_t *dst1, uint8_t *dst2, uint8_t *src, int srcStride)
4e4dcbc5
MN
2074{
2075 asm(
053dea12 2076 "lea (%0, %1), %%"REG_a" \n\t"
4e4dcbc5 2077// 0 1 2 3 4 5 6 7 8 9
9c9e467d 2078// %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
4e4dcbc5 2079 "movq (%0), %%mm0 \n\t" // 12345678
053dea12 2080 "movq (%%"REG_a"), %%mm1 \n\t" // abcdefgh
4e4dcbc5
MN
2081 "movq %%mm0, %%mm2 \n\t" // 12345678
2082 "punpcklbw %%mm1, %%mm0 \n\t" // 1a2b3c4d
2083 "punpckhbw %%mm1, %%mm2 \n\t" // 5e6f7g8h
2084
053dea12
AJ
2085 "movq (%%"REG_a", %1), %%mm1 \n\t"
2086 "movq (%%"REG_a", %1, 2), %%mm3 \n\t"
4e4dcbc5
MN
2087 "movq %%mm1, %%mm4 \n\t"
2088 "punpcklbw %%mm3, %%mm1 \n\t"
2089 "punpckhbw %%mm3, %%mm4 \n\t"
2090
2091 "movq %%mm0, %%mm3 \n\t"
2092 "punpcklwd %%mm1, %%mm0 \n\t"
2093 "punpckhwd %%mm1, %%mm3 \n\t"
2094 "movq %%mm2, %%mm1 \n\t"
2095 "punpcklwd %%mm4, %%mm2 \n\t"
2096 "punpckhwd %%mm4, %%mm1 \n\t"
2097
2098 "movd %%mm0, 128(%2) \n\t"
2099 "psrlq $32, %%mm0 \n\t"
2100 "movd %%mm0, 144(%2) \n\t"
2101 "movd %%mm3, 160(%2) \n\t"
2102 "psrlq $32, %%mm3 \n\t"
2103 "movd %%mm3, 176(%2) \n\t"
2104 "movd %%mm3, 48(%3) \n\t"
2105 "movd %%mm2, 192(%2) \n\t"
2106 "movd %%mm2, 64(%3) \n\t"
2107 "psrlq $32, %%mm2 \n\t"
2108 "movd %%mm2, 80(%3) \n\t"
2109 "movd %%mm1, 96(%3) \n\t"
2110 "psrlq $32, %%mm1 \n\t"
2111 "movd %%mm1, 112(%3) \n\t"
2112
053dea12 2113 "lea (%%"REG_a", %1, 4), %%"REG_a" \n\t"
115329f1 2114
4e4dcbc5 2115 "movq (%0, %1, 4), %%mm0 \n\t" // 12345678
053dea12 2116 "movq (%%"REG_a"), %%mm1 \n\t" // abcdefgh
4e4dcbc5
MN
2117 "movq %%mm0, %%mm2 \n\t" // 12345678
2118 "punpcklbw %%mm1, %%mm0 \n\t" // 1a2b3c4d
2119 "punpckhbw %%mm1, %%mm2 \n\t" // 5e6f7g8h
2120
053dea12
AJ
2121 "movq (%%"REG_a", %1), %%mm1 \n\t"
2122 "movq (%%"REG_a", %1, 2), %%mm3 \n\t"
4e4dcbc5
MN
2123 "movq %%mm1, %%mm4 \n\t"
2124 "punpcklbw %%mm3, %%mm1 \n\t"
2125 "punpckhbw %%mm3, %%mm4 \n\t"
2126
2127 "movq %%mm0, %%mm3 \n\t"
2128 "punpcklwd %%mm1, %%mm0 \n\t"
2129 "punpckhwd %%mm1, %%mm3 \n\t"
2130 "movq %%mm2, %%mm1 \n\t"
2131 "punpcklwd %%mm4, %%mm2 \n\t"
2132 "punpckhwd %%mm4, %%mm1 \n\t"
2133
2134 "movd %%mm0, 132(%2) \n\t"
2135 "psrlq $32, %%mm0 \n\t"
2136 "movd %%mm0, 148(%2) \n\t"
2137 "movd %%mm3, 164(%2) \n\t"
2138 "psrlq $32, %%mm3 \n\t"
2139 "movd %%mm3, 180(%2) \n\t"
2140 "movd %%mm3, 52(%3) \n\t"
2141 "movd %%mm2, 196(%2) \n\t"
2142 "movd %%mm2, 68(%3) \n\t"
2143 "psrlq $32, %%mm2 \n\t"
2144 "movd %%mm2, 84(%3) \n\t"
2145 "movd %%mm1, 100(%3) \n\t"
2146 "psrlq $32, %%mm1 \n\t"
2147 "movd %%mm1, 116(%3) \n\t"
2148
2149
053dea12
AJ
2150 :: "r" (src), "r" ((long)srcStride), "r" (dst1), "r" (dst2)
2151 : "%"REG_a
4e4dcbc5
MN
2152 );
2153}
2154
2155/**
2156 * transposes the given 8x8 block
2157 */
cc9b0679 2158static inline void RENAME(transpose2)(uint8_t *dst, int dstStride, uint8_t *src)
4e4dcbc5
MN
2159{
2160 asm(
053dea12
AJ
2161 "lea (%0, %1), %%"REG_a" \n\t"
2162 "lea (%%"REG_a",%1,4), %%"REG_d"\n\t"
4e4dcbc5 2163// 0 1 2 3 4 5 6 7 8 9
9c9e467d 2164// %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
4e4dcbc5
MN
2165 "movq (%2), %%mm0 \n\t" // 12345678
2166 "movq 16(%2), %%mm1 \n\t" // abcdefgh
2167 "movq %%mm0, %%mm2 \n\t" // 12345678
2168 "punpcklbw %%mm1, %%mm0 \n\t" // 1a2b3c4d
2169 "punpckhbw %%mm1, %%mm2 \n\t" // 5e6f7g8h
2170
2171 "movq 32(%2), %%mm1 \n\t"
2172 "movq 48(%2), %%mm3 \n\t"
2173 "movq %%mm1, %%mm4 \n\t"
2174 "punpcklbw %%mm3, %%mm1 \n\t"
2175 "punpckhbw %%mm3, %%mm4 \n\t"
2176
2177 "movq %%mm0, %%mm3 \n\t"
2178 "punpcklwd %%mm1, %%mm0 \n\t"
2179 "punpckhwd %%mm1, %%mm3 \n\t"
2180 "movq %%mm2, %%mm1 \n\t"
2181 "punpcklwd %%mm4, %%mm2 \n\t"
2182 "punpckhwd %%mm4, %%mm1 \n\t"
2183
2184 "movd %%mm0, (%0) \n\t"
2185 "psrlq $32, %%mm0 \n\t"
053dea12
AJ
2186 "movd %%mm0, (%%"REG_a") \n\t"
2187 "movd %%mm3, (%%"REG_a", %1) \n\t"
4e4dcbc5 2188 "psrlq $32, %%mm3 \n\t"
053dea12 2189 "movd %%mm3, (%%"REG_a", %1, 2) \n\t"
4e4dcbc5
MN
2190 "movd %%mm2, (%0, %1, 4) \n\t"
2191 "psrlq $32, %%mm2 \n\t"
053dea12
AJ
2192 "movd %%mm2, (%%"REG_d") \n\t"
2193 "movd %%mm1, (%%"REG_d", %1) \n\t"
4e4dcbc5 2194 "psrlq $32, %%mm1 \n\t"
053dea12 2195 "movd %%mm1, (%%"REG_d", %1, 2) \n\t"
4e4dcbc5
MN
2196
2197
2198 "movq 64(%2), %%mm0 \n\t" // 12345678
2199 "movq 80(%2), %%mm1 \n\t" // abcdefgh
2200 "movq %%mm0, %%mm2 \n\t" // 12345678
2201 "punpcklbw %%mm1, %%mm0 \n\t" // 1a2b3c4d
2202 "punpckhbw %%mm1, %%mm2 \n\t" // 5e6f7g8h
2203
2204 "movq 96(%2), %%mm1 \n\t"
2205 "movq 112(%2), %%mm3 \n\t"
2206 "movq %%mm1, %%mm4 \n\t"
2207 "punpcklbw %%mm3, %%mm1 \n\t"
2208 "punpckhbw %%mm3, %%mm4 \n\t"
2209
2210 "movq %%mm0, %%mm3 \n\t"
2211 "punpcklwd %%mm1, %%mm0 \n\t"
2212 "punpckhwd %%mm1, %%mm3 \n\t"
2213 "movq %%mm2, %%mm1 \n\t"
2214 "punpcklwd %%mm4, %%mm2 \n\t"
2215 "punpckhwd %%mm4, %%mm1 \n\t"
2216
2217 "movd %%mm0, 4(%0) \n\t"
2218 "psrlq $32, %%mm0 \n\t"
053dea12
AJ
2219 "movd %%mm0, 4(%%"REG_a") \n\t"
2220 "movd %%mm3, 4(%%"REG_a", %1) \n\t"
4e4dcbc5 2221 "psrlq $32, %%mm3 \n\t"
053dea12 2222 "movd %%mm3, 4(%%"REG_a", %1, 2) \n\t"
4e4dcbc5
MN
2223 "movd %%mm2, 4(%0, %1, 4) \n\t"
2224 "psrlq $32, %%mm2 \n\t"
053dea12
AJ
2225 "movd %%mm2, 4(%%"REG_d") \n\t"
2226 "movd %%mm1, 4(%%"REG_d", %1) \n\t"
4e4dcbc5 2227 "psrlq $32, %%mm1 \n\t"
053dea12 2228 "movd %%mm1, 4(%%"REG_d", %1, 2) \n\t"
4e4dcbc5 2229
053dea12
AJ
2230 :: "r" (dst), "r" ((long)dstStride), "r" (src)
2231 : "%"REG_a, "%"REG_d
4e4dcbc5
MN
2232 );
2233}
64c968da 2234#endif //HAVE_MMX
053dea12 2235//static long test=0;
4e4dcbc5 2236
a7b2871c 2237#ifndef HAVE_ALTIVEC
a2596758 2238static inline void RENAME(tempNoiseReducer)(uint8_t *src, int stride,
a9c77978 2239 uint8_t *tempBlured, uint32_t *tempBluredPast, int *maxNoise)
117e45b0 2240{
9c9e467d
MN
2241 // to save a register (FIXME do this outside of the loops)
2242 tempBluredPast[127]= maxNoise[0];
2243 tempBluredPast[128]= maxNoise[1];
2244 tempBluredPast[129]= maxNoise[2];
115329f1 2245
be44a4d7
MN
2246#define FAST_L2_DIFF
2247//#define L1_DIFF //u should change the thresholds too if u try that one
2248#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
2249 asm volatile(
053dea12
AJ
2250 "lea (%2, %2, 2), %%"REG_a" \n\t" // 3*stride
2251 "lea (%2, %2, 4), %%"REG_d" \n\t" // 5*stride
2252 "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
be44a4d7 2253// 0 1 2 3 4 5 6 7 8 9
9c9e467d 2254// %x %x+%2 %x+2%2 %x+eax %x+4%2 %x+edx %x+2eax %x+ecx %x+8%2
be44a4d7
MN
2255//FIXME reorder?
2256#ifdef L1_DIFF //needs mmx2
2257 "movq (%0), %%mm0 \n\t" // L0
2258 "psadbw (%1), %%mm0 \n\t" // |L0-R0|
2259 "movq (%0, %2), %%mm1 \n\t" // L1
2260 "psadbw (%1, %2), %%mm1 \n\t" // |L1-R1|
2261 "movq (%0, %2, 2), %%mm2 \n\t" // L2
2262 "psadbw (%1, %2, 2), %%mm2 \n\t" // |L2-R2|
053dea12
AJ
2263 "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
2264 "psadbw (%1, %%"REG_a"), %%mm3 \n\t" // |L3-R3|
be44a4d7
MN
2265
2266 "movq (%0, %2, 4), %%mm4 \n\t" // L4
2267 "paddw %%mm1, %%mm0 \n\t"
2268 "psadbw (%1, %2, 4), %%mm4 \n\t" // |L4-R4|
053dea12 2269 "movq (%0, %%"REG_d"), %%mm5 \n\t" // L5
be44a4d7 2270 "paddw %%mm2, %%mm0 \n\t"
053dea12
AJ
2271 "psadbw (%1, %%"REG_d"), %%mm5 \n\t" // |L5-R5|
2272 "movq (%0, %%"REG_a", 2), %%mm6 \n\t" // L6
be44a4d7 2273 "paddw %%mm3, %%mm0 \n\t"
053dea12
AJ
2274 "psadbw (%1, %%"REG_a", 2), %%mm6 \n\t" // |L6-R6|
2275 "movq (%0, %%"REG_c"), %%mm7 \n\t" // L7
be44a4d7 2276 "paddw %%mm4, %%mm0 \n\t"
053dea12 2277 "psadbw (%1, %%"REG_c"), %%mm7 \n\t" // |L7-R7|
be44a4d7
MN
2278 "paddw %%mm5, %%mm6 \n\t"
2279 "paddw %%mm7, %%mm6 \n\t"
2280 "paddw %%mm6, %%mm0 \n\t"
64c968da 2281#else //L1_DIFF
053dea12 2282#if defined (FAST_L2_DIFF)
be44a4d7 2283 "pcmpeqb %%mm7, %%mm7 \n\t"
9b464428 2284 "movq "MANGLE(b80)", %%mm6 \n\t"
be44a4d7 2285 "pxor %%mm0, %%mm0 \n\t"
053dea12 2286#define REAL_L2_DIFF_CORE(a, b)\
be44a4d7
MN
2287 "movq " #a ", %%mm5 \n\t"\
2288 "movq " #b ", %%mm2 \n\t"\
2289 "pxor %%mm7, %%mm2 \n\t"\
2290 PAVGB(%%mm2, %%mm5)\
2291 "paddb %%mm6, %%mm5 \n\t"\
2292 "movq %%mm5, %%mm2 \n\t"\
2293 "psllw $8, %%mm5 \n\t"\
2294 "pmaddwd %%mm5, %%mm5 \n\t"\
2295 "pmaddwd %%mm2, %%mm2 \n\t"\
2296 "paddd %%mm2, %%mm5 \n\t"\
2297 "psrld $14, %%mm5 \n\t"\
2298 "paddd %%mm5, %%mm0 \n\t"
2299
64c968da 2300#else //defined (FAST_L2_DIFF)
be44a4d7
MN
2301 "pxor %%mm7, %%mm7 \n\t"
2302 "pxor %%mm0, %%mm0 \n\t"
053dea12 2303#define REAL_L2_DIFF_CORE(a, b)\
be44a4d7
MN
2304 "movq " #a ", %%mm5 \n\t"\
2305 "movq " #b ", %%mm2 \n\t"\
2306 "movq %%mm5, %%mm1 \n\t"\
2307 "movq %%mm2, %%mm3 \n\t"\
2308 "punpcklbw %%mm7, %%mm5 \n\t"\
2309 "punpckhbw %%mm7, %%mm1 \n\t"\
2310 "punpcklbw %%mm7, %%mm2 \n\t"\
2311 "punpckhbw %%mm7, %%mm3 \n\t"\
2312 "psubw %%mm2, %%mm5 \n\t"\
2313 "psubw %%mm3, %%mm1 \n\t"\
2314 "pmaddwd %%mm5, %%mm5 \n\t"\
2315 "pmaddwd %%mm1, %%mm1 \n\t"\
2316 "paddd %%mm1, %%mm5 \n\t"\
2317 "paddd %%mm5, %%mm0 \n\t"
2318
64c968da 2319#endif //defined (FAST_L2_DIFF)
053dea12
AJ
2320
2321#define L2_DIFF_CORE(a, b) REAL_L2_DIFF_CORE(a, b)
2322
be44a4d7
MN
2323L2_DIFF_CORE((%0), (%1))
2324L2_DIFF_CORE((%0, %2), (%1, %2))
2325L2_DIFF_CORE((%0, %2, 2), (%1, %2, 2))
053dea12 2326L2_DIFF_CORE((%0, %%REGa), (%1, %%REGa))
be44a4d7 2327L2_DIFF_CORE((%0, %2, 4), (%1, %2, 4))
053dea12
AJ
2328L2_DIFF_CORE((%0, %%REGd), (%1, %%REGd))
2329L2_DIFF_CORE((%0, %%REGa,2), (%1, %%REGa,2))
2330L2_DIFF_CORE((%0, %%REGc), (%1, %%REGc))
be44a4d7 2331
64c968da 2332#endif //L1_DIFF
be44a4d7
MN
2333
2334 "movq %%mm0, %%mm4 \n\t"
2335 "psrlq $32, %%mm0 \n\t"
2336 "paddd %%mm0, %%mm4 \n\t"
2337 "movd %%mm4, %%ecx \n\t"
a9c77978 2338 "shll $2, %%ecx \n\t"
053dea12
AJ
2339 "mov %3, %%"REG_d" \n\t"
2340 "addl -4(%%"REG_d"), %%ecx \n\t"
2341 "addl 4(%%"REG_d"), %%ecx \n\t"
2342 "addl -1024(%%"REG_d"), %%ecx \n\t"
a9c77978 2343 "addl $4, %%ecx \n\t"
053dea12 2344 "addl 1024(%%"REG_d"), %%ecx \n\t"
a9c77978 2345 "shrl $3, %%ecx \n\t"
053dea12 2346 "movl %%ecx, (%%"REG_d") \n\t"
a9c77978 2347
053dea12
AJ
2348// "mov %3, %%"REG_c" \n\t"
2349// "mov %%"REG_c", test \n\t"
be44a4d7 2350// "jmp 4f \n\t"
053dea12 2351 "cmpl 512(%%"REG_d"), %%ecx \n\t"
be44a4d7 2352 " jb 2f \n\t"
053dea12 2353 "cmpl 516(%%"REG_d"), %%ecx \n\t"
be44a4d7
MN
2354 " jb 1f \n\t"
2355
053dea12
AJ
2356 "lea (%%"REG_a", %2, 2), %%"REG_d" \n\t" // 5*stride
2357 "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
be44a4d7
MN
2358 "movq (%0), %%mm0 \n\t" // L0
2359 "movq (%0, %2), %%mm1 \n\t" // L1
2360 "movq (%0, %2, 2), %%mm2 \n\t" // L2
053dea12 2361 "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
be44a4d7 2362 "movq (%0, %2, 4), %%mm4 \n\t" // L4
053dea12
AJ
2363 "movq (%0, %%"REG_d"), %%mm5 \n\t" // L5
2364 "movq (%0, %%"REG_a", 2), %%mm6 \n\t" // L6
2365 "movq (%0, %%"REG_c"), %%mm7 \n\t" // L7
be44a4d7
MN
2366 "movq %%mm0, (%1) \n\t" // L0
2367 "movq %%mm1, (%1, %2) \n\t" // L1
2368 "movq %%mm2, (%1, %2, 2) \n\t" // L2
053dea12 2369 "movq %%mm3, (%1, %%"REG_a") \n\t" // L3
be44a4d7 2370 "movq %%mm4, (%1, %2, 4) \n\t" // L4
053dea12
AJ
2371 "movq %%mm5, (%1, %%"REG_d") \n\t" // L5
2372 "movq %%mm6, (%1, %%"REG_a", 2) \n\t" // L6
2373 "movq %%mm7, (%1, %%"REG_c") \n\t" // L7
be44a4d7
MN
2374 "jmp 4f \n\t"
2375
2376 "1: \n\t"
053dea12
AJ
2377 "lea (%%"REG_a", %2, 2), %%"REG_d" \n\t" // 5*stride
2378 "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
be44a4d7 2379 "movq (%0), %%mm0 \n\t" // L0
413dec62 2380 PAVGB((%1), %%mm0) // L0
be44a4d7 2381 "movq (%0, %2), %%mm1 \n\t" // L1
413dec62 2382 PAVGB((%1, %2), %%mm1) // L1
be44a4d7 2383 "movq (%0, %2, 2), %%mm2 \n\t" // L2
413dec62 2384 PAVGB((%1, %2, 2), %%mm2) // L2
053dea12
AJ
2385 "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
2386 PAVGB((%1, %%REGa), %%mm3) // L3
be44a4d7 2387 "movq (%0, %2, 4), %%mm4 \n\t" // L4
413dec62 2388 PAVGB((%1, %2, 4), %%mm4) // L4
053dea12
AJ
2389 "movq (%0, %%"REG_d"), %%mm5 \n\t" // L5
2390 PAVGB((%1, %%REGd), %%mm5) // L5
2391 "movq (%0, %%"REG_a", 2), %%mm6 \n\t" // L6
2392 PAVGB((%1, %%REGa, 2), %%mm6) // L6
2393 "movq (%0, %%"REG_c"), %%mm7 \n\t" // L7
2394 PAVGB((%1, %%REGc), %%mm7) // L7
be44a4d7
MN
2395 "movq %%mm0, (%1) \n\t" // R0
2396 "movq %%mm1, (%1, %2) \n\t" // R1
2397 "movq %%mm2, (%1, %2, 2) \n\t" // R2
053dea12 2398 "movq %%mm3, (%1, %%"REG_a") \n\t" // R3
be44a4d7 2399 "movq %%mm4, (%1, %2, 4) \n\t" // R4
053dea12
AJ
2400 "movq %%mm5, (%1, %%"REG_d") \n\t" // R5
2401 "movq %%mm6, (%1, %%"REG_a", 2) \n\t" // R6
2402 "movq %%mm7, (%1, %%"REG_c") \n\t" // R7
be44a4d7
MN
2403 "movq %%mm0, (%0) \n\t" // L0
2404 "movq %%mm1, (%0, %2) \n\t" // L1
2405 "movq %%mm2, (%0, %2, 2) \n\t" // L2
053dea12 2406 "movq %%mm3, (%0, %%"REG_a") \n\t" // L3
be44a4d7 2407 "movq %%mm4, (%0, %2, 4) \n\t" // L4
053dea12
AJ
2408 "movq %%mm5, (%0, %%"REG_d") \n\t" // L5
2409 "movq %%mm6, (%0, %%"REG_a", 2) \n\t" // L6
2410 "movq %%mm7, (%0, %%"REG_c") \n\t" // L7
be44a4d7
MN
2411 "jmp 4f \n\t"
2412
2413 "2: \n\t"
053dea12 2414 "cmpl 508(%%"REG_d"), %%ecx \n\t"
be44a4d7
MN
2415 " jb 3f \n\t"
2416
053dea12
AJ
2417 "lea (%%"REG_a", %2, 2), %%"REG_d" \n\t" // 5*stride
2418 "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
be44a4d7
MN
2419 "movq (%0), %%mm0 \n\t" // L0
2420 "movq (%0, %2), %%mm1 \n\t" // L1
2421 "movq (%0, %2, 2), %%mm2 \n\t" // L2
053dea12 2422 "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
be44a4d7
MN
2423 "movq (%1), %%mm4 \n\t" // R0
2424 "movq (%1, %2), %%mm5 \n\t" // R1
2425 "movq (%1, %2, 2), %%mm6 \n\t" // R2
053dea12 2426 "movq (%1, %%"REG_a"), %%mm7 \n\t" // R3
be44a4d7
MN
2427 PAVGB(%%mm4, %%mm0)
2428 PAVGB(%%mm5, %%mm1)
2429 PAVGB(%%mm6, %%mm2)
2430 PAVGB(%%mm7, %%mm3)
2431 PAVGB(%%mm4, %%mm0)
2432 PAVGB(%%mm5, %%mm1)
2433 PAVGB(%%mm6, %%mm2)
2434 PAVGB(%%mm7, %%mm3)
2435 "movq %%mm0, (%1) \n\t" // R0
2436 "movq %%mm1, (%1, %2) \n\t" // R1
2437 "movq %%mm2, (%1, %2, 2) \n\t" // R2
053dea12 2438 "movq %%mm3, (%1, %%"REG_a") \n\t" // R3
be44a4d7
MN
2439 "movq %%mm0, (%0) \n\t" // L0
2440 "movq %%mm1, (%0, %2) \n\t" // L1
2441 "movq %%mm2, (%0, %2, 2) \n\t" // L2
053dea12 2442 "movq %%mm3, (%0, %%"REG_a") \n\t" // L3
be44a4d7
MN
2443
2444 "movq (%0, %2, 4), %%mm0 \n\t" // L4
053dea12
AJ
2445 "movq (%0, %%"REG_d"), %%mm1 \n\t" // L5
2446 "movq (%0, %%"REG_a", 2), %%mm2 \n\t" // L6
2447 "movq (%0, %%"REG_c"), %%mm3 \n\t" // L7
be44a4d7 2448 "movq (%1, %2, 4), %%mm4 \n\t" // R4
053dea12
AJ
2449 "movq (%1, %%"REG_d"), %%mm5 \n\t" // R5
2450 "movq (%1, %%"REG_a", 2), %%mm6 \n\t" // R6
2451 "movq (%1, %%"REG_c"), %%mm7 \n\t" // R7
be44a4d7
MN
2452 PAVGB(%%mm4, %%mm0)
2453 PAVGB(%%mm5, %%mm1)
2454 PAVGB(%%mm6, %%mm2)
2455 PAVGB(%%mm7, %%mm3)
2456 PAVGB(%%mm4, %%mm0)
2457 PAVGB(%%mm5, %%mm1)
2458 PAVGB(%%mm6, %%mm2)
2459 PAVGB(%%mm7, %%mm3)
2460 "movq %%mm0, (%1, %2, 4) \n\t" // R4
053dea12
AJ
2461 "movq %%mm1, (%1, %%"REG_d") \n\t" // R5
2462 "movq %%mm2, (%1, %%"REG_a", 2) \n\t" // R6
2463 "movq %%mm3, (%1, %%"REG_c") \n\t" // R7
be44a4d7 2464 "movq %%mm0, (%0, %2, 4) \n\t" // L4
053dea12
AJ
2465 "movq %%mm1, (%0, %%"REG_d") \n\t" // L5
2466 "movq %%mm2, (%0, %%"REG_a", 2) \n\t" // L6
2467 "movq %%mm3, (%0, %%"REG_c") \n\t" // L7
be44a4d7
MN
2468 "jmp 4f \n\t"
2469
2470 "3: \n\t"
053dea12
AJ
2471 "lea (%%"REG_a", %2, 2), %%"REG_d" \n\t" // 5*stride
2472 "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
be44a4d7
MN
2473 "movq (%0), %%mm0 \n\t" // L0
2474 "movq (%0, %2), %%mm1 \n\t" // L1
2475 "movq (%0, %2, 2), %%mm2 \n\t" // L2
053dea12 2476 "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
be44a4d7
MN
2477 "movq (%1), %%mm4 \n\t" // R0
2478 "movq (%1, %2), %%mm5 \n\t" // R1
2479 "movq (%1, %2, 2), %%mm6 \n\t" // R2
053dea12 2480 "movq (%1, %%"REG_a"), %%mm7 \n\t" // R3
be44a4d7
MN
2481 PAVGB(%%mm4, %%mm0)
2482 PAVGB(%%mm5, %%mm1)
2483 PAVGB(%%mm6, %%mm2)
2484 PAVGB(%%mm7, %%mm3)
2485 PAVGB(%%mm4, %%mm0)
2486 PAVGB(%%mm5, %%mm1)
2487 PAVGB(%%mm6, %%mm2)
2488 PAVGB(%%mm7, %%mm3)
2489 PAVGB(%%mm4, %%mm0)
2490 PAVGB(%%mm5, %%mm1)
2491 PAVGB(%%mm6, %%mm2)
2492 PAVGB(%%mm7, %%mm3)
2493 "movq %%mm0, (%1) \n\t" // R0
2494 "movq %%mm1, (%1, %2) \n\t" // R1
2495 "movq %%mm2, (%1, %2, 2) \n\t" // R2
053dea12 2496 "movq %%mm3, (%1, %%"REG_a") \n\t" // R3
be44a4d7
MN
2497 "movq %%mm0, (%0) \n\t" // L0
2498 "movq %%mm1, (%0, %2) \n\t" // L1
2499 "movq %%mm2, (%0, %2, 2) \n\t" // L2
053dea12 2500 "movq %%mm3, (%0, %%"REG_a") \n\t" // L3
be44a4d7
MN
2501
2502 "movq (%0, %2, 4), %%mm0 \n\t" // L4
053dea12
AJ
2503 "movq (%0, %%"REG_d"), %%mm1 \n\t" // L5
2504 "movq (%0, %%"REG_a", 2), %%mm2 \n\t" // L6
2505 "movq (%0, %%"REG_c"), %%mm3 \n\t" // L7
be44a4d7 2506 "movq (%1, %2, 4), %%mm4 \n\t" // R4
053dea12
AJ
2507 "movq (%1, %%"REG_d"), %%mm5 \n\t" // R5
2508 "movq (%1, %%"REG_a", 2), %%mm6 \n\t" // R6
2509 "movq (%1, %%"REG_c"), %%mm7 \n\t" // R7
be44a4d7
MN
2510 PAVGB(%%mm4, %%mm0)
2511 PAVGB(%%mm5, %%mm1)
2512 PAVGB(%%mm6, %%mm2)
2513 PAVGB(%%mm7, %%mm3)
2514 PAVGB(%%mm4, %%mm0)
2515 PAVGB(%%mm5, %%mm1)
2516 PAVGB(%%mm6, %%mm2)
2517 PAVGB(%%mm7, %%mm3)
2518 PAVGB(%%mm4, %%mm0)
2519 PAVGB(%%mm5, %%mm1)
2520 PAVGB(%%mm6, %%mm2)
2521 PAVGB(%%mm7, %%mm3)
2522 "movq %%mm0, (%1, %2, 4) \n\t" // R4
053dea12
AJ
2523 "movq %%mm1, (%1, %%"REG_d") \n\t" // R5
2524 "movq %%mm2, (%1, %%"REG_a", 2) \n\t" // R6
2525 "movq %%mm3, (%1, %%"REG_c") \n\t" // R7
be44a4d7 2526 "movq %%mm0, (%0, %2, 4) \n\t" // L4
053dea12
AJ
2527 "movq %%mm1, (%0, %%"REG_d") \n\t" // L5
2528 "movq %%mm2, (%0, %%"REG_a", 2) \n\t" // L6
2529 "movq %%mm3, (%0, %%"REG_c") \n\t" // L7
be44a4d7
MN
2530
2531 "4: \n\t"
2532
053dea12
AJ
2533 :: "r" (src), "r" (tempBlured), "r"((long)stride), "m" (tempBluredPast)
2534 : "%"REG_a, "%"REG_d, "%"REG_c, "memory"
be44a4d7
MN
2535 );
2536//printf("%d\n", test);
64c968da 2537#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
5936be4c 2538{
117e45b0
MN
2539 int y;
2540 int d=0;
a7b2871c 2541// int sysd=0;
a9c77978 2542 int i;
117e45b0
MN
2543
2544 for(y=0; y<8; y++)
2545 {
2546 int x;
2547 for(x=0; x<8; x++)
2548 {
2549 int ref= tempBlured[ x + y*stride ];
2550 int cur= src[ x + y*stride ];
2551 int d1=ref - cur;
be44a4d7
MN
2552// if(x==0 || x==7) d1+= d1>>1;
2553// if(y==0 || y==7) d1+= d1>>1;
2554// d+= ABS(d1);
2555 d+= d1*d1;
a7b2871c 2556// sysd+= d1;
117e45b0
MN
2557 }
2558 }
a9c77978
MN
2559 i=d;
2560 d= (
2561 4*d
2562 +(*(tempBluredPast-256))
2563 +(*(tempBluredPast-1))+ (*(tempBluredPast+1))
2564 +(*(tempBluredPast+256))
2565 +4)>>3;
2566 *tempBluredPast=i;
2567// ((*tempBluredPast)*3 + d + 2)>>2;
2568
117e45b0
MN
2569//printf("%d %d %d\n", maxNoise[0], maxNoise[1], maxNoise[2]);
2570/*
2571Switch between
2572 1 0 0 0 0 0 0 (0)
257364 32 16 8 4 2 1 (1)
257464 48 36 27 20 15 11 (33) (approx)
257564 56 49 43 37 33 29 (200) (approx)
2576*/
2577 if(d > maxNoise[1])
2578 {
2579 if(d < maxNoise[2])
2580 {
2581 for(y=0; y<8; y++)
2582 {
2583 int x;
2584 for(x=0; x<8; x++)
2585 {
2586 int ref= tempBlured[ x + y*stride ];
2587 int cur= src[ x + y*stride ];
2588 tempBlured[ x + y*stride ]=
2589 src[ x + y*stride ]=
2590 (ref + cur + 1)>>1;
2591 }
2592 }
2593 }
2594 else
2595 {
2596 for(y=0; y<8; y++)
2597 {
2598 int x;
2599 for(x=0; x<8; x++)
2600 {
2601 tempBlured[ x + y*stride ]= src[ x + y*stride ];
2602 }
2603 }
2604 }
2605 }
2606 else
2607 {
2608 if(d < maxNoise[0])
2609 {
2610 for(y=0; y<8; y++)
2611 {
2612 int x;
2613 for(x=0; x<8; x++)
2614 {
2615 int ref= tempBlured[ x + y*stride ];
2616 int cur= src[ x + y*stride ];
2617 tempBlured[ x + y*stride ]=
2618 src[ x + y*stride ]=
2619 (ref*7 + cur + 4)>>3;
2620 }
2621 }
2622 }
2623 else
2624 {
2625 for(y=0; y<8; y++)
2626 {
2627 int x;
2628 for(x=0; x<8; x++)
2629 {
2630 int ref= tempBlured[ x + y*stride ];
2631 int cur= src[ x + y*stride ];
2632 tempBlured[ x + y*stride ]=
2633 src[ x + y*stride ]=
2634 (ref*3 + cur + 2)>>2;
2635 }
2636 }
2637 }
2638 }
5936be4c 2639}
64c968da 2640#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
117e45b0 2641}
a7b2871c 2642#endif //HAVE_ALTIVEC
117e45b0 2643
792a5a7c
MN
2644#ifdef HAVE_MMX
2645/**
2646 * accurate deblock filter
2647 */
2648static always_inline void RENAME(do_a_deblock)(uint8_t *src, int step, int stride, PPContext *c){
5fddb9b3 2649 int64_t dc_mask, eq_mask, both_masks;
39d89b69 2650 int64_t sums[10*8*2];
792a5a7c
MN
2651 src+= step*3; // src points to begin of the 8x8 Block
2652//START_TIMER
2653asm volatile(
115329f1
DB
2654 "movq %0, %%mm7 \n\t"
2655 "movq %1, %%mm6 \n\t"
792a5a7c
MN
2656 : : "m" (c->mmxDcOffset[c->nonBQP]), "m" (c->mmxDcThreshold[c->nonBQP])
2657 );
115329f1 2658
792a5a7c 2659asm volatile(
053dea12 2660 "lea (%2, %3), %%"REG_a" \n\t"
792a5a7c
MN
2661// 0 1 2 3 4 5 6 7 8 9
2662// %1 eax eax+%2 eax+2%2 %1+4%2 ecx ecx+%2 ecx+2%2 %1+8%2 ecx+4%2
2663
2664 "movq (%2), %%mm0 \n\t"
053dea12 2665 "movq (%%"REG_a"), %%mm1 \n\t"
792a5a7c
MN
2666 "movq %%mm1, %%mm3 \n\t"
2667 "movq %%mm1, %%mm4 \n\t"
2668 "psubb %%mm1, %%mm0 \n\t" // mm0 = differnece
2669 "paddb %%mm7, %%mm0 \n\t"
2670 "pcmpgtb %%mm6, %%mm0 \n\t"
2671
053dea12 2672 "movq (%%"REG_a",%3), %%mm2 \n\t"
792a5a7c
MN
2673 PMAXUB(%%mm2, %%mm4)
2674 PMINUB(%%mm2, %%mm3, %%mm5)
2675 "psubb %%mm2, %%mm1 \n\t"
2676 "paddb %%mm7, %%mm1 \n\t"
2677 "pcmpgtb %%mm6, %%mm1 \n\t"
2678 "paddb %%mm1, %%mm0 \n\t"
2679
053dea12 2680 "movq (%%"REG_a", %3, 2), %%mm1 \n\t"
792a5a7c
MN
2681 PMAXUB(%%mm1, %%mm4)
2682 PMINUB(%%mm1, %%mm3, %%mm5)
2683 "psubb %%mm1, %%mm2 \n\t"
2684 "paddb %%mm7, %%mm2 \n\t"
2685 "pcmpgtb %%mm6, %%mm2 \n\t"
2686 "paddb %%mm2, %%mm0 \n\t"
115329f1 2687
053dea12 2688 "lea (%%"REG_a", %3, 4), %%"REG_a" \n\t"
792a5a7c
MN
2689
2690 "movq (%2, %3, 4), %%mm2 \n\t"
2691 PMAXUB(%%mm2, %%mm4)
2692 PMINUB(%%mm2, %%mm3, %%mm5)
2693 "psubb %%mm2, %%mm1 \n\t"
2694 "paddb %%mm7, %%mm1 \n\t"
2695 "pcmpgtb %%mm6, %%mm1 \n\t"
2696 "paddb %%mm1, %%mm0 \n\t"
2697
053dea12 2698 "movq (%%"REG_a"), %%mm1 \n\t"
792a5a7c
MN
2699 PMAXUB(%%mm1, %%mm4)
2700 PMINUB(%%mm1, %%mm3, %%mm5)
2701 "psubb %%mm1, %%mm2 \n\t"
2702 "paddb %%mm7, %%mm2 \n\t"
2703 "pcmpgtb %%mm6, %%mm2 \n\t"
2704 "paddb %%mm2, %%mm0 \n\t"
2705
053dea12 2706 "movq (%%"REG_a", %3), %%mm2 \n\t"
792a5a7c
MN
2707 PMAXUB(%%mm2, %%mm4)
2708 PMINUB(%%mm2, %%mm3, %%mm5)
2709 "psubb %%mm2, %%mm1 \n\t"
2710 "paddb %%mm7, %%mm1 \n\t"
2711 "pcmpgtb %%mm6, %%mm1 \n\t"
2712 "paddb %%mm1, %%mm0 \n\t"
2713
053dea12 2714 "movq (%%"REG_a", %3, 2), %%mm1 \n\t"
792a5a7c
MN
2715 PMAXUB(%%mm1, %%mm4)
2716 PMINUB(%%mm1, %%mm3, %%mm5)
2717 "psubb %%mm1, %%mm2 \n\t"
2718 "paddb %%mm7, %%mm2 \n\t"
2719 "pcmpgtb %%mm6, %%mm2 \n\t"
2720 "paddb %%mm2, %%mm0 \n\t"
2721
2722 "movq (%2, %3, 8), %%mm2 \n\t"
2723 PMAXUB(%%mm2, %%mm4)
2724 PMINUB(%%mm2, %%mm3, %%mm5)
2725 "psubb %%mm2, %%mm1 \n\t"
2726 "paddb %%mm7, %%mm1 \n\t"
2727 "pcmpgtb %%mm6, %%mm1 \n\t"
2728 "paddb %%mm1, %%mm0 \n\t"
2729
053dea12 2730 "movq (%%"REG_a", %3, 4), %%mm1 \n\t"
792a5a7c
MN
2731 "psubb %%mm1, %%mm2 \n\t"
2732 "paddb %%mm7, %%mm2 \n\t"
2733 "pcmpgtb %%mm6, %%mm2 \n\t"
2734 "paddb %%mm2, %%mm0 \n\t"
2735 "psubusb %%mm3, %%mm4 \n\t"
2736
a5cd3c48 2737 "pxor %%mm6, %%mm6 \n\t"
792a5a7c
MN
2738 "movq %4, %%mm7 \n\t" // QP,..., QP
2739 "paddusb %%mm7, %%mm7 \n\t" // 2QP ... 2QP
a5cd3c48
MN
2740 "psubusb %%mm4, %%mm7 \n\t" // Diff >=2QP -> 0
2741 "pcmpeqb %%mm6, %%mm7 \n\t" // Diff < 2QP -> 0
2742 "pcmpeqb %%mm6, %%mm7 \n\t" // Diff < 2QP -> 0
792a5a7c
MN
2743 "movq %%mm7, %1 \n\t"
2744
792a5a7c
MN
2745 "movq %5, %%mm7 \n\t"
2746 "punpcklbw %%mm7, %%mm7 \n\t"
2747 "punpcklbw %%mm7, %%mm7 \n\t"
2748 "punpcklbw %%mm7, %%mm7 \n\t"
2749 "psubb %%mm0, %%mm6 \n\t"
2750 "pcmpgtb %%mm7, %%mm6 \n\t"
2751 "movq %%mm6, %0 \n\t"
2752
2753 : "=m" (eq_mask), "=m" (dc_mask)
053dea12
AJ
2754 : "r" (src), "r" ((long)step), "m" (c->pQPb), "m"(c->ppMode.flatnessThreshold)
2755 : "%"REG_a
792a5a7c
MN
2756 );
2757
5fddb9b3
MN
2758 both_masks = dc_mask & eq_mask;
2759
2760 if(both_masks){
053dea12 2761 long offset= -8*step;
39d89b69
MN
2762 int64_t *temp_sums= sums;
2763
2764 asm volatile(
2765 "movq %2, %%mm0 \n\t" // QP,..., QP
2766 "pxor %%mm4, %%mm4 \n\t"
2767
2768 "movq (%0), %%mm6 \n\t"
2769 "movq (%0, %1), %%mm5 \n\t"
2770 "movq %%mm5, %%mm1 \n\t"
2771 "movq %%mm6, %%mm2 \n\t"
2772 "psubusb %%mm6, %%mm5 \n\t"
2773 "psubusb %%mm1, %%mm2 \n\t"
2774 "por %%mm5, %%mm2 \n\t" // ABS Diff of lines
2775 "psubusb %%mm2, %%mm0 \n\t" // diff >= QP -> 0
2776 "pcmpeqb %%mm4, %%mm0 \n\t" // diff >= QP -> FF
2777
2778 "pxor %%mm6, %%mm1 \n\t"
2779 "pand %%mm0, %%mm1 \n\t"
2780 "pxor %%mm1, %%mm6 \n\t"
2781 // 0:QP 6:First
2782
2783 "movq (%0, %1, 8), %%mm5 \n\t"
053dea12 2784 "add %1, %0 \n\t" // %0 points to line 1 not 0
39d89b69
MN
2785 "movq (%0, %1, 8), %%mm7 \n\t"
2786 "movq %%mm5, %%mm1 \n\t"
2787 "movq %%mm7, %%mm2 \n\t"
2788 "psubusb %%mm7, %%mm5 \n\t"
2789 "psubusb %%mm1, %%mm2 \n\t"
2790 "por %%mm5, %%mm2 \n\t" // ABS Diff of lines
2791 "movq %2, %%mm0 \n\t" // QP,..., QP
2792 "psubusb %%mm2, %%mm0 \n\t" // diff >= QP -> 0
2793 "pcmpeqb %%mm4, %%mm0 \n\t" // diff >= QP -> FF
2794
2795 "pxor %%mm7, %%mm1 \n\t"
2796 "pand %%mm0, %%mm1 \n\t"
2797 "pxor %%mm1, %%mm7 \n\t"
115329f1 2798
39d89b69
MN
2799 "movq %%mm6, %%mm5 \n\t"
2800 "punpckhbw %%mm4, %%mm6 \n\t"
2801 "punpcklbw %%mm4, %%mm5 \n\t"
2802 // 4:0 5/6:First 7:Last
2803
2804 "movq %%mm5, %%mm0 \n\t"
2805 "movq %%mm6, %%mm1 \n\t"
2806 "psllw $2, %%mm0 \n\t"
2807 "psllw $2, %%mm1 \n\t"
2808 "paddw "MANGLE(w04)", %%mm0 \n\t"
2809 "paddw "MANGLE(w04)", %%mm1 \n\t"
2810
2811#define NEXT\
2812 "movq (%0), %%mm2 \n\t"\
2813 "movq (%0), %%mm3 \n\t"\
053dea12 2814 "add %1, %0 \n\t"\
39d89b69
MN
2815 "punpcklbw %%mm4, %%mm2 \n\t"\
2816 "punpckhbw %%mm4, %%mm3 \n\t"\
2817 "paddw %%mm2, %%mm0 \n\t"\
2818 "paddw %%mm3, %%mm1 \n\t"
2819
2820#define PREV\
2821 "movq (%0), %%mm2 \n\t"\
2822 "movq (%0), %%mm3 \n\t"\
053dea12 2823 "add %1, %0 \n\t"\
39d89b69
MN
2824 "punpcklbw %%mm4, %%mm2 \n\t"\
2825 "punpckhbw %%mm4, %%mm3 \n\t"\
2826 "psubw %%mm2, %%mm0 \n\t"\
2827 "psubw %%mm3, %%mm1 \n\t"
2828
115329f1 2829
39d89b69
MN
2830 NEXT //0
2831 NEXT //1
2832 NEXT //2
2833 "movq %%mm0, (%3) \n\t"
2834 "movq %%mm1, 8(%3) \n\t"
2835
2836 NEXT //3
2837 "psubw %%mm5, %%mm0 \n\t"
2838 "psubw %%mm6, %%mm1 \n\t"
2839 "movq %%mm0, 16(%3) \n\t"
2840 "movq %%mm1, 24(%3) \n\t"
2841
2842 NEXT //4
2843 "psubw %%mm5, %%mm0 \n\t"
2844 "psubw %%mm6, %%mm1 \n\t"
2845 "movq %%mm0, 32(%3) \n\t"
2846 "movq %%mm1, 40(%3) \n\t"
2847
2848 NEXT //5
2849 "psubw %%mm5, %%mm0 \n\t"
2850 "psubw %%mm6, %%mm1 \n\t"
2851 "movq %%mm0, 48(%3) \n\t"
2852 "movq %%mm1, 56(%3) \n\t"
2853
2854 NEXT //6
2855 "psubw %%mm5, %%mm0 \n\t"
2856 "psubw %%mm6, %%mm1 \n\t"
2857 "movq %%mm0, 64(%3) \n\t"
2858 "movq %%mm1, 72(%3) \n\t"
2859
2860 "movq %%mm7, %%mm6 \n\t"
2861 "punpckhbw %%mm4, %%mm7 \n\t"
2862 "punpcklbw %%mm4, %%mm6 \n\t"
115329f1 2863
39d89b69 2864 NEXT //7
053dea12
AJ
2865 "mov %4, %0 \n\t"
2866 "add %1, %0 \n\t"
39d89b69
MN
2867 PREV //0
2868 "movq %%mm0, 80(%3) \n\t"
2869 "movq %%mm1, 88(%3) \n\t"
2870
2871 PREV //1
2872 "paddw %%mm6, %%mm0 \n\t"
2873 "paddw %%mm7, %%mm1 \n\t"
2874 "movq %%mm0, 96(%3) \n\t"
2875 "movq %%mm1, 104(%3) \n\t"
115329f1 2876
39d89b69
MN
2877 PREV //2
2878 "paddw %%mm6, %%mm0 \n\t"
2879 "paddw %%mm7, %%mm1 \n\t"
2880 "movq %%mm0, 112(%3) \n\t"
2881 "movq %%mm1, 120(%3) \n\t"
2882
2883 PREV //3
2884 "paddw %%mm6, %%mm0 \n\t"
2885 "paddw %%mm7, %%mm1 \n\t"
2886 "movq %%mm0, 128(%3) \n\t"
2887 "movq %%mm1, 136(%3) \n\t"
2888
2889 PREV //4
2890 "paddw %%mm6, %%mm0 \n\t"
2891 "paddw %%mm7, %%mm1 \n\t"
2892 "movq %%mm0, 144(%3) \n\t"
2893 "movq %%mm1, 152(%3) \n\t"
2894
053dea12 2895 "mov %4, %0 \n\t" //FIXME
39d89b69
MN
2896
2897 : "+&r"(src)
053dea12 2898 : "r" ((long)step), "m" (c->pQPb), "r"(sums), "g"(src)
39d89b69
MN
2899 );
2900
2901 src+= step; // src points to begin of the 8x8 Block
2902
2903 asm volatile(
2904 "movq %4, %%mm6 \n\t"
2905 "pcmpeqb %%mm5, %%mm5 \n\t"
2906 "pxor %%mm6, %%mm5 \n\t"
2907 "pxor %%mm7, %%mm7 \n\t"
2908
2909 "1: \n\t"
2910 "movq (%1), %%mm0 \n\t"
2911 "movq 8(%1), %%mm1 \n\t"
2912 "paddw 32(%1), %%mm0 \n\t"
2913 "paddw 40(%1), %%mm1 \n\t"
2914 "movq (%0, %3), %%mm2 \n\t"
2915 "movq %%mm2, %%mm3 \n\t"
2916 "movq %%mm2, %%mm4 \n\t"
2917 "punpcklbw %%mm7, %%mm2 \n\t"
2918 "punpckhbw %%mm7, %%mm3 \n\t"
2919 "paddw %%mm2, %%mm0 \n\t"
2920 "paddw %%mm3, %%mm1 \n\t"
2921 "paddw %%mm2, %%mm0 \n\t"
2922 "paddw %%mm3, %%mm1 \n\t"
2923 "psrlw $4, %%mm0 \n\t"
2924 "psrlw $4, %%mm1 \n\t"
2925 "packuswb %%mm1, %%mm0 \n\t"
2926 "pand %%mm6, %%mm0 \n\t"
2927 "pand %%mm5, %%mm4 \n\t"
2928 "por %%mm4, %%mm0 \n\t"
2929 "movq %%mm0, (%0, %3) \n\t"
053dea12
AJ
2930 "add $16, %1 \n\t"
2931 "add %2, %0 \n\t"
39d89b69
MN
2932 " js 1b \n\t"
2933
2934 : "+r"(offset), "+r"(temp_sums)
5fddb9b3 2935 : "r" ((long)step), "r"(src - offset), "m"(both_masks)
39d89b69
MN
2936 );
2937 }else
2938 src+= step; // src points to begin of the 8x8 Block
792a5a7c
MN
2939
2940 if(eq_mask != -1LL){
39d89b69 2941 uint8_t *temp_src= src;
792a5a7c
MN
2942 asm volatile(
2943 "pxor %%mm7, %%mm7 \n\t"
053dea12
AJ
2944 "lea -40(%%"REG_SP"), %%"REG_c" \n\t" // make space for 4 8-byte vars
2945 "and "ALIGN_MASK", %%"REG_c" \n\t" // align
792a5a7c
MN
2946// 0 1 2 3 4 5 6 7 8 9
2947// %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1 %1+8%1 ecx+4%1
2948
2949 "movq (%0), %%mm0 \n\t"
2950 "movq %%mm0, %%mm1 \n\t"
2951 "punpcklbw %%mm7, %%mm0 \n\t" // low part of line 0
2952 "punpckhbw %%mm7, %%mm1 \n\t" // high part of line 0
2953
2954 "movq (%0, %1), %%mm2 \n\t"
053dea12 2955 "lea (%0, %1, 2), %%"REG_a" \n\t"
792a5a7c
MN
2956 "movq %%mm2, %%mm3 \n\t"
2957 "punpcklbw %%mm7, %%mm2 \n\t" // low part of line 1
2958 "punpckhbw %%mm7, %%mm3 \n\t" // high part of line 1
2959
053dea12 2960 "movq (%%"REG_a"), %%mm4 \n\t"
792a5a7c
MN
2961 "movq %%mm4, %%mm5 \n\t"
2962 "punpcklbw %%mm7, %%mm4 \n\t" // low part of line 2
2963 "punpckhbw %%mm7, %%mm5 \n\t" // high part of line 2
2964
2965 "paddw %%mm0, %%mm0 \n\t" // 2L0
2966 "paddw %%mm1, %%mm1 \n\t" // 2H0
2967 "psubw %%mm4, %%mm2 \n\t" // L1 - L2
2968 "psubw %%mm5, %%mm3 \n\t" // H1 - H2
2969 "psubw %%mm2, %%mm0 \n\t" // 2L0 - L1 + L2
2970 "psubw %%mm3, %%mm1 \n\t" // 2H0 - H1 + H2
2971
2972 "psllw $2, %%mm2 \n\t" // 4L1 - 4L2
2973 "psllw $2, %%mm3 \n\t" // 4H1 - 4H2
2974 "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2
2975 "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2
2976
053dea12 2977 "movq (%%"REG_a", %1), %%mm2 \n\t"
792a5a7c
MN
2978 "movq %%mm2, %%mm3 \n\t"
2979 "punpcklbw %%mm7, %%mm2 \n\t" // L3
2980 "punpckhbw %%mm7, %%mm3 \n\t" // H3
2981
2982 "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2 - L3
2983 "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2 - H3
2984 "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2 - 2L3
2985 "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2 - 2H3
053dea12
AJ
2986 "movq %%mm0, (%%"REG_c") \n\t" // 2L0 - 5L1 + 5L2 - 2L3
2987 "movq %%mm1, 8(%%"REG_c") \n\t" // 2H0 - 5H1 + 5H2 - 2H3
792a5a7c 2988
053dea12 2989 "movq (%%"REG_a", %1, 2), %%mm0 \n\t"
792a5a7c
MN
2990 "movq %%mm0, %%mm1 \n\t"
2991 "punpcklbw %%mm7, %%mm0 \n\t" // L4
2992 "punpckhbw %%mm7, %%mm1 \n\t" // H4
2993
2994 "psubw %%mm0, %%mm2 \n\t" // L3 - L4
2995 "psubw %%mm1, %%mm3 \n\t" // H3 - H4
053dea12
AJ
2996 "movq %%mm2, 16(%%"REG_c") \n\t" // L3 - L4
2997 "movq %%mm3, 24(%%"REG_c") \n\t" // H3 - H4
792a5a7c
MN
2998 "paddw %%mm4, %%mm4 \n\t" // 2L2
2999 "paddw %%mm5, %%mm5 \n\t" // 2H2
3000 "psubw %%mm2, %%mm4 \n\t" // 2L2 - L3 + L4
3001 "psubw %%mm3, %%mm5 \n\t" // 2H2 - H3 + H4
3002
053dea12 3003 "lea (%%"REG_a", %1), %0 \n\t"
792a5a7c
MN
3004 "psllw $2, %%mm2 \n\t" // 4L3 - 4L4
3005 "psllw $2, %%mm3 \n\t" // 4H3 - 4H4
3006 "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4
3007 "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4
3008//50 opcodes so far
3009 "movq (%0, %1, 2), %%mm2 \n\t"
3010 "movq %%mm2, %%mm3 \n\t"
3011 "punpcklbw %%mm7, %%mm2 \n\t" // L5
3012 "punpckhbw %%mm7, %%mm3 \n\t" // H5
3013 "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4 - L5
3014 "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4 - H5
3015 "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4 - 2L5
3016 "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4 - 2H5
3017
053dea12 3018 "movq (%%"REG_a", %1, 4), %%mm6 \n\t"
792a5a7c
MN
3019 "punpcklbw %%mm7, %%mm6 \n\t" // L6
3020 "psubw %%mm6, %%mm2 \n\t" // L5 - L6
053dea12 3021 "movq (%%"REG_a", %1, 4), %%mm6 \n\t"
792a5a7c
MN
3022 "punpckhbw %%mm7, %%mm6 \n\t" // H6
3023 "psubw %%mm6, %%mm3 \n\t" // H5 - H6
3024
3025 "paddw %%mm0, %%mm0 \n\t" // 2L4
3026 "paddw %%mm1, %%mm1 \n\t" // 2H4
3027 "psubw %%mm2, %%mm0 \n\t" // 2L4 - L5 + L6
3028 "psubw %%mm3, %%mm1 \n\t" // 2H4 - H5 + H6
3029
3030 "psllw $2, %%mm2 \n\t" // 4L5 - 4L6
3031 "psllw $2, %%mm3 \n\t" // 4H5 - 4H6
3032 "psubw %%mm2, %%mm0 \n\t" // 2L4 - 5L5 + 5L6
3033 "psubw %%mm3, %%mm1 \n\t" // 2H4 - 5H5 + 5H6
3034
3035 "movq (%0, %1, 4), %%mm2 \n\t"
3036 "movq %%mm2, %%mm3 \n\t"
3037 "punpcklbw %%mm7, %%mm2 \n\t" // L7
3038 "punpckhbw %%mm7, %%mm3 \n\t" // H7
3039
3040 "paddw %%mm2, %%mm2 \n\t" // 2L7
3041 "paddw %%mm3, %%mm3 \n\t" // 2H7
3042 "psubw %%mm2, %%mm0 \n\t" // 2L4 - 5L5 + 5L6 - 2L7
3043 "psubw %%mm3, %%mm1 \n\t" // 2H4 - 5H5 + 5H6 - 2H7
3044
053dea12
AJ
3045 "movq (%%"REG_c"), %%mm2 \n\t" // 2L0 - 5L1 + 5L2 - 2L3
3046 "movq 8(%%"REG_c"), %%mm3 \n\t" // 2H0 - 5H1 + 5H2 - 2H3
792a5a7c
MN
3047
3048#ifdef HAVE_MMX2
3049 "movq %%mm7, %%mm6 \n\t" // 0
3050 "psubw %%mm0, %%mm6 \n\t"
3051 "pmaxsw %%mm6, %%mm0 \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
3052 "movq %%mm7, %%mm6 \n\t" // 0
3053 "psubw %%mm1, %%mm6 \n\t"
3054 "pmaxsw %%mm6, %%mm1 \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
3055 "movq %%mm7, %%mm6 \n\t" // 0
3056 "psubw %%mm2, %%mm6 \n\t"
3057 "pmaxsw %%mm6, %%mm2 \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
3058 "movq %%mm7, %%mm6 \n\t" // 0
3059 "psubw %%mm3, %%mm6 \n\t"
3060 "pmaxsw %%mm6, %%mm3 \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
3061#else
3062 "movq %%mm7, %%mm6 \n\t" // 0
3063 "pcmpgtw %%mm0, %%mm6 \n\t"
3064 "pxor %%mm6, %%mm0 \n\t"
3065 "psubw %%mm6, %%mm0 \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
3066 "movq %%mm7, %%mm6 \n\t" // 0
3067 "pcmpgtw %%mm1, %%mm6 \n\t"
3068 "pxor %%mm6, %%mm1 \n\t"
3069 "psubw %%mm6, %%mm1 \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
3070 "movq %%mm7, %%mm6 \n\t" // 0
3071 "pcmpgtw %%mm2, %%mm6 \n\t"
3072 "pxor %%mm6, %%mm2 \n\t"
3073 "psubw %%mm6, %%mm2 \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
3074 "movq %%mm7, %%mm6 \n\t" // 0
3075 "pcmpgtw %%mm3, %%mm6 \n\t"
3076 "pxor %%mm6, %%mm3 \n\t"
3077 "psubw %%mm6, %%mm3 \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
3078#endif
3079
3080#ifdef HAVE_MMX2
3081 "pminsw %%mm2, %%mm0 \n\t"
3082 "pminsw %%mm3, %%mm1 \n\t"
3083#else
3084 "movq %%mm0, %%mm6 \n\t"
3085 "psubusw %%mm2, %%mm6 \n\t"
3086 "psubw %%mm6, %%mm0 \n\t"
3087 "movq %%mm1, %%mm6 \n\t"
3088 "psubusw %%mm3, %%mm6 \n\t"
3089 "psubw %%mm6, %%mm1 \n\t"
3090#endif
3091
3092 "movd %2, %%mm2 \n\t" // QP
3093 "punpcklbw %%mm7, %%mm2 \n\t"
3094
3095 "movq %%mm7, %%mm6 \n\t" // 0
3096 "pcmpgtw %%mm4, %%mm6 \n\t" // sign(2L2 - 5L3 + 5L4 - 2L5)
3097 "pxor %%mm6, %%mm4 \n\t"
3098 "psubw %%mm6, %%mm4 \n\t" // |2L2 - 5L3 + 5L4 - 2L5|
3099 "pcmpgtw %%mm5, %%mm7 \n\t" // sign(2H2 - 5H3 + 5H4 - 2H5)
3100 "pxor %%mm7, %%mm5 \n\t"
3101 "psubw %%mm7, %%mm5 \n\t" // |2H2 - 5H3 + 5H4 - 2H5|
3102// 100 opcodes
3103 "psllw $3, %%mm2 \n\t" // 8QP
3104 "movq %%mm2, %%mm3 \n\t" // 8QP
3105 "pcmpgtw %%mm4, %%mm2 \n\t"
3106 "pcmpgtw %%mm5, %%mm3 \n\t"
3107 "pand %%mm2, %%mm4 \n\t"
3108 "pand %%mm3, %%mm5 \n\t"
3109
3110
3111 "psubusw %%mm0, %%mm4 \n\t" // hd
3112 "psubusw %%mm1, %%mm5 \n\t" // ld
3113
3114