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