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