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