Statistics
| Branch: | Revision:

ffmpeg / libavcodec / libpostproc / postprocess_template.c @ b304569a

History | View | Annotate | Download (91.1 KB)

1
/*
2
    Copyright (C) 2001-2002 Michael Niedermayer (michaelni@gmx.at)
3

4
    This program is free software; you can redistribute it and/or modify
5
    it under the terms of the GNU General Public License as published by
6
    the Free Software Foundation; either version 2 of the License, or
7
    (at your option) any later version.
8

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

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

    
19
/**
20
 * @file postprocess_template.c
21
 * mmx/mmx2/3dnow postprocess code.
22
 */
23

    
24

    
25
#undef PAVGB
26
#undef PMINUB
27
#undef PMAXUB
28

    
29
#ifdef HAVE_MMX2
30
#define PAVGB(a,b) "pavgb " #a ", " #b " \n\t"
31
#elif defined (HAVE_3DNOW)
32
#define PAVGB(a,b) "pavgusb " #a ", " #b " \n\t"
33
#endif
34

    
35
#ifdef HAVE_MMX2
36
#define PMINUB(a,b,t) "pminub " #a ", " #b " \n\t"
37
#elif defined (HAVE_MMX)
38
#define PMINUB(b,a,t) \
39
        "movq " #a ", " #t " \n\t"\
40
        "psubusb " #b ", " #t " \n\t"\
41
        "psubb " #t ", " #a " \n\t"
42
#endif
43

    
44
#ifdef HAVE_MMX2
45
#define PMAXUB(a,b) "pmaxub " #a ", " #b " \n\t"
46
#elif defined (HAVE_MMX)
47
#define PMAXUB(a,b) \
48
        "psubusb " #a ", " #b " \n\t"\
49
        "paddb " #a ", " #b " \n\t"
50
#endif
51

    
52

    
53
//FIXME? |255-0| = 1 (shouldnt be a problem ...)
54
#ifdef HAVE_MMX
55
/**
56
 * Check if the middle 8x8 Block in the given 8x16 block is flat
57
 */
58
static inline int RENAME(isVertDC)(uint8_t src[], int stride, PPContext *c){
59
        int numEq= 0;
60
        src+= stride*4; // src points to begin of the 8x8 Block
61
asm volatile(
62
                "leal (%1, %2), %%eax                                \n\t"
63
//        0        1        2        3        4        5        6        7        8        9
64
//        %1        eax        eax+%2        eax+2%2        %1+4%2        ecx        ecx+%2        ecx+2%2        %1+8%2        ecx+4%2
65
                "movq %3, %%mm7                                        \n\t" 
66
                "movq %4, %%mm6                                        \n\t" 
67

    
68
                "movq (%1), %%mm0                                \n\t"
69
                "movq (%%eax), %%mm1                                \n\t"
70
                "psubb %%mm1, %%mm0                                \n\t" // mm0 = differnece
71
                "paddb %%mm7, %%mm0                                \n\t"
72
                "pcmpgtb %%mm6, %%mm0                                \n\t"
73

    
74
                "movq (%%eax,%2), %%mm2                                \n\t"
75
                "psubb %%mm2, %%mm1                                \n\t"
76
                "paddb %%mm7, %%mm1                                \n\t"
77
                "pcmpgtb %%mm6, %%mm1                                \n\t"
78
                "paddb %%mm1, %%mm0                                \n\t"
79

    
80
                "movq (%%eax, %2, 2), %%mm1                        \n\t"
81
                "psubb %%mm1, %%mm2                                \n\t"
82
                "paddb %%mm7, %%mm2                                \n\t"
83
                "pcmpgtb %%mm6, %%mm2                                \n\t"
84
                "paddb %%mm2, %%mm0                                \n\t"
85
                
86
                "leal (%%eax, %2, 4), %%eax                        \n\t"
87

    
88
                "movq (%1, %2, 4), %%mm2                        \n\t"
89
                "psubb %%mm2, %%mm1                                \n\t"
90
                "paddb %%mm7, %%mm1                                \n\t"
91
                "pcmpgtb %%mm6, %%mm1                                \n\t"
92
                "paddb %%mm1, %%mm0                                \n\t"
93

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

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

    
106
                "movq (%%eax, %2, 2), %%mm1                        \n\t"
107
                "psubb %%mm1, %%mm2                                \n\t"
108
                "paddb %%mm7, %%mm2                                \n\t"
109
                "pcmpgtb %%mm6, %%mm2                                \n\t"
110
                "paddb %%mm2, %%mm0                                \n\t"
111

    
112
                "                                                \n\t"
113
#ifdef HAVE_MMX2
114
                "pxor %%mm7, %%mm7                                \n\t"
115
                "psadbw %%mm7, %%mm0                                \n\t"
116
#else
117
                "movq %%mm0, %%mm1                                \n\t"
118
                "psrlw $8, %%mm0                                \n\t"
119
                "paddb %%mm1, %%mm0                                \n\t"
120
                "movq %%mm0, %%mm1                                \n\t"
121
                "psrlq $16, %%mm0                                \n\t"
122
                "paddb %%mm1, %%mm0                                \n\t"
123
                "movq %%mm0, %%mm1                                \n\t"
124
                "psrlq $32, %%mm0                                \n\t"
125
                "paddb %%mm1, %%mm0                                \n\t"
126
#endif
127
                "movd %%mm0, %0                                        \n\t"
128
                : "=r" (numEq)
129
                : "r" (src), "r" (stride), "m" (c->mmxDcOffset[c->nonBQP]),  "m" (c->mmxDcThreshold[c->nonBQP])
130
                : "%eax"
131
                );
132
        numEq= (-numEq) &0xFF;
133
        return numEq > c->ppMode.flatnessThreshold;
134
}
135
#endif
136

    
137
static inline int RENAME(isVertMinMaxOk)(uint8_t src[], int stride, PPContext *c)
138
{
139
#ifdef HAVE_MMX
140
        int isOk;
141
        src+= stride*3;
142
        asm volatile(
143
                "movq (%1, %2), %%mm0                                \n\t"
144
                "movq (%1, %2, 8), %%mm1                        \n\t"
145
                "movq %%mm0, %%mm2                                \n\t"
146
                "psubusb %%mm1, %%mm0                                \n\t"
147
                "psubusb %%mm2, %%mm1                                \n\t"
148
                "por %%mm1, %%mm0                                \n\t" // ABS Diff
149

    
150
                "movq %3, %%mm7                                        \n\t" // QP,..., QP
151
                "paddusb %%mm7, %%mm7                                \n\t" // 2QP ... 2QP
152
                "psubusb %%mm7, %%mm0                                \n\t" // Diff <= 2QP -> 0
153
                "packssdw %%mm0, %%mm0                                \n\t"
154
                "movd %%mm0, %0                                        \n\t"
155
                : "=r" (isOk)
156
                : "r" (src), "r" (stride), "m" (c->pQPb)
157
                );
158
        return isOk==0;
159
#else
160
#if 1
161
        int x;
162
        const int QP= c->QP;
163
        src+= stride*3;
164
        for(x=0; x<BLOCK_SIZE; x++)
165
        {
166
                if((unsigned)(src[x + stride] - src[x + (stride<<3)] + 2*QP) > 4*QP) return 0;
167
        }
168

    
169
        return 1;
170
#else
171
        int x;
172
        const int QP= c->QP;
173
        src+= stride*4;
174
        for(x=0; x<BLOCK_SIZE; x++)
175
        {
176
                int min=255;
177
                int max=0;
178
                int y;
179
                for(y=0; y<8; y++){
180
                        int v= src[x + y*stride];
181
                        if(v>max) max=v;
182
                        if(v<min) min=v;
183
                }
184
                if(max-min > 2*QP) return 0;
185
        }
186
        return 1;
187
#endif
188
#endif
189
}
190

    
191
/**
192
 * Do a vertical low pass filter on the 8x16 block (only write to the 8x8 block in the middle)
193
 * using the 9-Tap Filter (1,1,2,2,4,2,2,1,1)/16
194
 */
195
static inline void RENAME(doVertLowPass)(uint8_t *src, int stride, PPContext *c)
196
{
197
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
198
        src+= stride*3;
199
        asm volatile(        //"movv %0 %1 %2\n\t"
200
                "movq %2, %%mm0                        \n\t"  // QP,..., QP
201
                "pxor %%mm4, %%mm4                                \n\t"
202

    
203
                "movq (%0), %%mm6                                \n\t"
204
                "movq (%0, %1), %%mm5                                \n\t"
205
                "movq %%mm5, %%mm1                                \n\t"
206
                "movq %%mm6, %%mm2                                \n\t"
207
                "psubusb %%mm6, %%mm5                                \n\t"
208
                "psubusb %%mm1, %%mm2                                \n\t"
209
                "por %%mm5, %%mm2                                \n\t" // ABS Diff of lines
210
                "psubusb %%mm0, %%mm2                                \n\t" // diff <= QP -> 0
211
                "pcmpeqb %%mm4, %%mm2                        \n\t" // diff <= QP -> FF
212

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

    
217
                "movq (%0, %1, 8), %%mm5                        \n\t"
218
                "leal (%0, %1, 4), %%eax                        \n\t"
219
                "leal (%0, %1, 8), %%ecx                        \n\t"
220
                "subl %1, %%ecx                                        \n\t"
221
                "addl %1, %0                                        \n\t" // %0 points to line 1 not 0
222
                "movq (%0, %1, 8), %%mm7                        \n\t"
223
                "movq %%mm5, %%mm1                                \n\t"
224
                "movq %%mm7, %%mm2                                \n\t"
225
                "psubusb %%mm7, %%mm5                                \n\t"
226
                "psubusb %%mm1, %%mm2                                \n\t"
227
                "por %%mm5, %%mm2                                \n\t" // ABS Diff of lines
228
                "psubusb %%mm0, %%mm2                                \n\t" // diff <= QP -> 0
229
                "pcmpeqb %%mm4, %%mm2                        \n\t" // diff <= QP -> FF
230

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

    
235

    
236
                //         1        2        3        4        5        6        7        8
237
                //        %0        %0+%1        %0+2%1        eax        %0+4%1        eax+2%1        ecx        eax+4%1
238
                // 6 4 2 2 1 1
239
                // 6 4 4 2
240
                // 6 8 2
241

    
242
                "movq (%0, %1), %%mm0                                \n\t" //  1
243
                "movq %%mm0, %%mm1                                \n\t" //  1
244
                PAVGB(%%mm6, %%mm0)                                      //1 1        /2
245
                PAVGB(%%mm6, %%mm0)                                      //3 1        /4
246

    
247
                "movq (%0, %1, 4), %%mm2                        \n\t" //     1
248
                "movq %%mm2, %%mm5                                \n\t" //     1
249
                PAVGB((%%eax), %%mm2)                                      //    11        /2
250
                PAVGB((%0, %1, 2), %%mm2)                              //   211        /4
251
                "movq %%mm2, %%mm3                                \n\t" //   211        /4
252
                "movq (%0), %%mm4                                \n\t" // 1
253
                PAVGB(%%mm4, %%mm3)                                      // 4 211        /8
254
                PAVGB(%%mm0, %%mm3)                                      //642211        /16
255
                "movq %%mm3, (%0)                                \n\t" // X
256
                // mm1=2 mm2=3(211) mm4=1 mm5=5 mm6=0 mm7=9
257
                "movq %%mm1, %%mm0                                \n\t" //  1
258
                PAVGB(%%mm6, %%mm0)                                      //1 1        /2
259
                "movq %%mm4, %%mm3                                \n\t" // 1
260
                PAVGB((%0,%1,2), %%mm3)                                      // 1 1        /2
261
                PAVGB((%%eax,%1,2), %%mm5)                              //     11        /2
262
                PAVGB((%%eax), %%mm5)                                      //    211 /4
263
                PAVGB(%%mm5, %%mm3)                                      // 2 2211 /8
264
                PAVGB(%%mm0, %%mm3)                                      //4242211 /16
265
                "movq %%mm3, (%0,%1)                                \n\t" //  X
266
                // mm1=2 mm2=3(211) mm4=1 mm5=4(211) mm6=0 mm7=9
267
                PAVGB(%%mm4, %%mm6)                                      //11        /2
268
                "movq (%%ecx), %%mm0                                \n\t" //       1
269
                PAVGB((%%eax, %1, 2), %%mm0)                              //      11/2
270
                "movq %%mm0, %%mm3                                \n\t" //      11/2
271
                PAVGB(%%mm1, %%mm0)                                      //  2   11/4
272
                PAVGB(%%mm6, %%mm0)                                      //222   11/8
273
                PAVGB(%%mm2, %%mm0)                                      //22242211/16
274
                "movq (%0, %1, 2), %%mm2                        \n\t" //   1
275
                "movq %%mm0, (%0, %1, 2)                        \n\t" //   X
276
                // mm1=2 mm2=3 mm3=6(11) mm4=1 mm5=4(211) mm6=0(11) mm7=9
277
                "movq (%%eax, %1, 4), %%mm0                        \n\t" //        1
278
                PAVGB((%%ecx), %%mm0)                                      //       11        /2
279
                PAVGB(%%mm0, %%mm6)                                      //11     11        /4
280
                PAVGB(%%mm1, %%mm4)                                      // 11                /2
281
                PAVGB(%%mm2, %%mm1)                                      //  11                /2
282
                PAVGB(%%mm1, %%mm6)                                      //1122   11        /8
283
                PAVGB(%%mm5, %%mm6)                                      //112242211        /16
284
                "movq (%%eax), %%mm5                                \n\t" //    1
285
                "movq %%mm6, (%%eax)                                \n\t" //    X
286
                // mm0=7(11) mm1=2(11) mm2=3 mm3=6(11) mm4=1(11) mm5=4 mm7=9
287
                "movq (%%eax, %1, 4), %%mm6                        \n\t" //        1
288
                PAVGB(%%mm7, %%mm6)                                      //        11        /2
289
                PAVGB(%%mm4, %%mm6)                                      // 11     11        /4
290
                PAVGB(%%mm3, %%mm6)                                      // 11   2211        /8
291
                PAVGB(%%mm5, %%mm2)                                      //   11                /2
292
                "movq (%0, %1, 4), %%mm4                        \n\t" //     1
293
                PAVGB(%%mm4, %%mm2)                                      //   112                /4
294
                PAVGB(%%mm2, %%mm6)                                      // 112242211        /16
295
                "movq %%mm6, (%0, %1, 4)                        \n\t" //     X
296
                // mm0=7(11) mm1=2(11) mm2=3(112) mm3=6(11) mm4=5 mm5=4 mm7=9
297
                PAVGB(%%mm7, %%mm1)                                      //  11     2        /4
298
                PAVGB(%%mm4, %%mm5)                                      //    11                /2
299
                PAVGB(%%mm5, %%mm0)                                      //    11 11        /4
300
                "movq (%%eax, %1, 2), %%mm6                        \n\t" //      1
301
                PAVGB(%%mm6, %%mm1)                                      //  11  4  2        /8
302
                PAVGB(%%mm0, %%mm1)                                      //  11224222        /16
303
                "movq %%mm1, (%%eax, %1, 2)                        \n\t" //      X
304
                // mm2=3(112) mm3=6(11) mm4=5 mm5=4(11) mm6=6 mm7=9
305
                PAVGB((%%ecx), %%mm2)                                      //   112 4        /8
306
                "movq (%%eax, %1, 4), %%mm0                        \n\t" //        1
307
                PAVGB(%%mm0, %%mm6)                                      //      1 1        /2
308
                PAVGB(%%mm7, %%mm6)                                      //      1 12        /4
309
                PAVGB(%%mm2, %%mm6)                                      //   1122424        /4
310
                "movq %%mm6, (%%ecx)                                \n\t" //       X
311
                // mm0=8 mm3=6(11) mm4=5 mm5=4(11) mm7=9
312
                PAVGB(%%mm7, %%mm5)                                      //    11   2        /4
313
                PAVGB(%%mm7, %%mm5)                                      //    11   6        /8
314

    
315
                PAVGB(%%mm3, %%mm0)                                      //      112        /4
316
                PAVGB(%%mm0, %%mm5)                                      //    112246        /16
317
                "movq %%mm5, (%%eax, %1, 4)                        \n\t" //        X
318
                "subl %1, %0                                        \n\t"
319

    
320
                :
321
                : "r" (src), "r" (stride), "m" (c->pQPb)
322
                : "%eax", "%ecx"
323
        );
324
#else
325
        const int l1= stride;
326
        const int l2= stride + l1;
327
        const int l3= stride + l2;
328
        const int l4= stride + l3;
329
        const int l5= stride + l4;
330
        const int l6= stride + l5;
331
        const int l7= stride + l6;
332
        const int l8= stride + l7;
333
        const int l9= stride + l8;
334
        int x;
335
        src+= stride*3;
336
        for(x=0; x<BLOCK_SIZE; x++)
337
        {
338
                const int first= ABS(src[0] - src[l1]) < c->QP ? src[0] : src[l1];
339
                const int last= ABS(src[l8] - src[l9]) < c->QP ? src[l9] : src[l8];
340

    
341
                int sums[9];
342
                sums[0] = first + src[l1];
343
                sums[1] = src[l1] + src[l2];
344
                sums[2] = src[l2] + src[l3];
345
                sums[3] = src[l3] + src[l4];
346
                sums[4] = src[l4] + src[l5];
347
                sums[5] = src[l5] + src[l6];
348
                sums[6] = src[l6] + src[l7];
349
                sums[7] = src[l7] + src[l8];
350
                sums[8] = src[l8] + last;
351

    
352
                src[l1]= ((sums[0]<<2) + ((first + sums[2])<<1) + sums[4] + 8)>>4;
353
                src[l2]= ((src[l2]<<2) + ((first + sums[0] + sums[3])<<1) + sums[5] + 8)>>4;
354
                src[l3]= ((src[l3]<<2) + ((first + sums[1] + sums[4])<<1) + sums[6] + 8)>>4;
355
                src[l4]= ((src[l4]<<2) + ((sums[2] + sums[5])<<1) + sums[0] + sums[7] + 8)>>4;
356
                src[l5]= ((src[l5]<<2) + ((sums[3] + sums[6])<<1) + sums[1] + sums[8] + 8)>>4;
357
                src[l6]= ((src[l6]<<2) + ((last + sums[7] + sums[4])<<1) + sums[2] + 8)>>4;
358
                src[l7]= (((last + src[l7])<<2) + ((src[l8] + sums[5])<<1) + sums[3] + 8)>>4;
359
                src[l8]= ((sums[8]<<2) + ((last + sums[6])<<1) + sums[4] + 8)>>4;
360

    
361
                src++;
362
        }
363
#endif
364
}
365

    
366
#if 0
367
/**
368
 * Experimental implementation of the filter (Algorithm 1) described in a paper from Ramkishor & Karandikar
369
 * values are correctly clipped (MMX2)
370
 * values are wraparound (C)
371
 * conclusion: its fast, but introduces ugly horizontal patterns if there is a continious gradient
372
        0 8 16 24
373
        x = 8
374
        x/2 = 4
375
        x/8 = 1
376
        1 12 12 23
377
 */
378
static inline void RENAME(vertRK1Filter)(uint8_t *src, int stride, int QP)
379
{
380
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
381
        src+= stride*3;
382
// FIXME rounding
383
        asm volatile(
384
                "pxor %%mm7, %%mm7                                \n\t" // 0
385
                "movq "MANGLE(b80)", %%mm6                        \n\t" // MIN_SIGNED_BYTE
386
                "leal (%0, %1), %%eax                                \n\t"
387
                "leal (%%eax, %1, 4), %%ecx                        \n\t"
388
//        0        1        2        3        4        5        6        7        8        9
389
//        %0        eax        eax+%1        eax+2%1        %0+4%1        ecx        ecx+%1        ecx+2%1        %0+8%1        ecx+4%1
390
                "movq "MANGLE(pQPb)", %%mm0                        \n\t" // QP,..., QP
391
                "movq %%mm0, %%mm1                                \n\t" // QP,..., QP
392
                "paddusb "MANGLE(b02)", %%mm0                        \n\t"
393
                "psrlw $2, %%mm0                                \n\t"
394
                "pand "MANGLE(b3F)", %%mm0                        \n\t" // QP/4,..., QP/4
395
                "paddusb %%mm1, %%mm0                                \n\t" // QP*1.25 ...
396
                "movq (%0, %1, 4), %%mm2                        \n\t" // line 4
397
                "movq (%%ecx), %%mm3                                \n\t" // line 5
398
                "movq %%mm2, %%mm4                                \n\t" // line 4
399
                "pcmpeqb %%mm5, %%mm5                                \n\t" // -1
400
                "pxor %%mm2, %%mm5                                \n\t" // -line 4 - 1
401
                PAVGB(%%mm3, %%mm5)
402
                "paddb %%mm6, %%mm5                                \n\t" // (l5-l4)/2
403
                "psubusb %%mm3, %%mm4                                \n\t"
404
                "psubusb %%mm2, %%mm3                                \n\t"
405
                "por %%mm3, %%mm4                                \n\t" // |l4 - l5|
406
                "psubusb %%mm0, %%mm4                                \n\t"
407
                "pcmpeqb %%mm7, %%mm4                                \n\t"
408
                "pand %%mm4, %%mm5                                \n\t" // d/2
409

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

415
                "movq (%%ecx), %%mm2                                \n\t"
416
//                "paddb %%mm6, %%mm2                                \n\t" // line 5 + 0x80
417
                "psubb %%mm5, %%mm2                                \n\t"
418
//                "psubb %%mm6, %%mm2                                \n\t"
419
                "movq %%mm2, (%%ecx)                                \n\t"
420

421
                "paddb %%mm6, %%mm5                                \n\t"
422
                "psrlw $2, %%mm5                                \n\t"
423
                "pand "MANGLE(b3F)", %%mm5                        \n\t"
424
                "psubb "MANGLE(b20)", %%mm5                        \n\t" // (l5-l4)/8
425

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

432
                "movq (%%ecx, %1), %%mm2                        \n\t"
433
                "paddb %%mm6, %%mm2                                \n\t" // line 6 + 0x80
434
                "psubsb %%mm5, %%mm2                                \n\t"
435
                "psubb %%mm6, %%mm2                                \n\t"
436
                "movq %%mm2, (%%ecx, %1)                        \n\t"
437

438
                :
439
                : "r" (src), "r" (stride)
440
                : "%eax", "%ecx"
441
        );
442
#else
443
         const int l1= stride;
444
        const int l2= stride + l1;
445
        const int l3= stride + l2;
446
        const int l4= stride + l3;
447
        const int l5= stride + l4;
448
        const int l6= stride + l5;
449
//        const int l7= stride + l6;
450
//        const int l8= stride + l7;
451
//        const int l9= stride + l8;
452
        int x;
453
        const int QP15= QP + (QP>>2);
454
        src+= stride*3;
455
        for(x=0; x<BLOCK_SIZE; x++)
456
        {
457
                const int v = (src[x+l5] - src[x+l4]);
458
                if(ABS(v) < QP15)
459
                {
460
                        src[x+l3] +=v>>3;
461
                        src[x+l4] +=v>>1;
462
                        src[x+l5] -=v>>1;
463
                        src[x+l6] -=v>>3;
464

    
465
                }
466
        }
467

    
468
#endif
469
}
470
#endif
471

    
472
/**
473
 * Experimental Filter 1
474
 * will not damage linear gradients
475
 * Flat blocks should look like they where passed through the (1,1,2,2,4,2,2,1,1) 9-Tap filter
476
 * can only smooth blocks at the expected locations (it cant smooth them if they did move)
477
 * MMX2 version does correct clipping C version doesnt
478
 */
479
static inline void RENAME(vertX1Filter)(uint8_t *src, int stride, PPContext *co)
480
{
481
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
482
        src+= stride*3;
483

    
484
        asm volatile(
485
                "pxor %%mm7, %%mm7                                \n\t" // 0
486
                "leal (%0, %1), %%eax                                \n\t"
487
                "leal (%%eax, %1, 4), %%ecx                        \n\t"
488
//        0        1        2        3        4        5        6        7        8        9
489
//        %0        eax        eax+%1        eax+2%1        %0+4%1        ecx        ecx+%1        ecx+2%1        %0+8%1        ecx+4%1
490
                "movq (%%eax, %1, 2), %%mm0                        \n\t" // line 3
491
                "movq (%0, %1, 4), %%mm1                        \n\t" // line 4
492
                "movq %%mm1, %%mm2                                \n\t" // line 4
493
                "psubusb %%mm0, %%mm1                                \n\t"
494
                "psubusb %%mm2, %%mm0                                \n\t"
495
                "por %%mm1, %%mm0                                \n\t" // |l2 - l3|
496
                "movq (%%ecx), %%mm3                                \n\t" // line 5
497
                "movq (%%ecx, %1), %%mm4                        \n\t" // line 6
498
                "movq %%mm3, %%mm5                                \n\t" // line 5
499
                "psubusb %%mm4, %%mm3                                \n\t"
500
                "psubusb %%mm5, %%mm4                                \n\t"
501
                "por %%mm4, %%mm3                                \n\t" // |l5 - l6|
502
                PAVGB(%%mm3, %%mm0)                                      // (|l2 - l3| + |l5 - l6|)/2
503
                "movq %%mm2, %%mm1                                \n\t" // line 4
504
                "psubusb %%mm5, %%mm2                                \n\t"
505
                "movq %%mm2, %%mm4                                \n\t"
506
                "pcmpeqb %%mm7, %%mm2                                \n\t" // (l4 - l5) <= 0 ? -1 : 0
507
                "psubusb %%mm1, %%mm5                                \n\t"
508
                "por %%mm5, %%mm4                                \n\t" // |l4 - l5|
509
                "psubusb %%mm0, %%mm4                \n\t" //d = MAX(0, |l4-l5| - (|l2-l3| + |l5-l6|)/2)
510
                "movq %%mm4, %%mm3                                \n\t" // d
511
                "movq %2, %%mm0                        \n\t"
512
                "paddusb %%mm0, %%mm0                                \n\t"
513
                "psubusb %%mm0, %%mm4                                \n\t"
514
                "pcmpeqb %%mm7, %%mm4                                \n\t" // d <= QP ? -1 : 0
515
                "psubusb "MANGLE(b01)", %%mm3                        \n\t"
516
                "pand %%mm4, %%mm3                                \n\t" // d <= QP ? d : 0
517

    
518
                PAVGB(%%mm7, %%mm3)                                      // d/2
519
                "movq %%mm3, %%mm1                                \n\t" // d/2
520
                PAVGB(%%mm7, %%mm3)                                      // d/4
521
                PAVGB(%%mm1, %%mm3)                                      // 3*d/8
522

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

    
529
                "movq (%%ecx), %%mm0                                \n\t" // line 5
530
                "pxor %%mm2, %%mm0                                \n\t" //(l4 - l5) <= 0 ? -l5-1 : l5
531
                "paddusb %%mm3, %%mm0                                \n\t"
532
                "pxor %%mm2, %%mm0                                \n\t"
533
                "movq %%mm0, (%%ecx)                                \n\t" // line 5
534

    
535
                PAVGB(%%mm7, %%mm1)                                      // d/4
536

    
537
                "movq (%%eax, %1, 2), %%mm0                        \n\t" // line 3
538
                "pxor %%mm2, %%mm0                                \n\t" //(l4 - l5) <= 0 ? -l4-1 : l4
539
                "psubusb %%mm1, %%mm0                                \n\t"
540
                "pxor %%mm2, %%mm0                                \n\t"
541
                "movq %%mm0, (%%eax, %1, 2)                        \n\t" // line 3
542

    
543
                "movq (%%ecx, %1), %%mm0                        \n\t" // line 6
544
                "pxor %%mm2, %%mm0                                \n\t" //(l4 - l5) <= 0 ? -l5-1 : l5
545
                "paddusb %%mm1, %%mm0                                \n\t"
546
                "pxor %%mm2, %%mm0                                \n\t"
547
                "movq %%mm0, (%%ecx, %1)                        \n\t" // line 6
548

    
549
                PAVGB(%%mm7, %%mm1)                                      // d/8
550

    
551
                "movq (%%eax, %1), %%mm0                        \n\t" // line 2
552
                "pxor %%mm2, %%mm0                                \n\t" //(l4 - l5) <= 0 ? -l2-1 : l2
553
                "psubusb %%mm1, %%mm0                                \n\t"
554
                "pxor %%mm2, %%mm0                                \n\t"
555
                "movq %%mm0, (%%eax, %1)                        \n\t" // line 2
556

    
557
                "movq (%%ecx, %1, 2), %%mm0                        \n\t" // line 7
558
                "pxor %%mm2, %%mm0                                \n\t" //(l4 - l5) <= 0 ? -l7-1 : l7
559
                "paddusb %%mm1, %%mm0                                \n\t"
560
                "pxor %%mm2, %%mm0                                \n\t"
561
                "movq %%mm0, (%%ecx, %1, 2)                        \n\t" // line 7
562

    
563
                :
564
                : "r" (src), "r" (stride), "m" (co->pQPb)
565
                : "%eax", "%ecx"
566
        );
567
#else
568

    
569
         const int l1= stride;
570
        const int l2= stride + l1;
571
        const int l3= stride + l2;
572
        const int l4= stride + l3;
573
        const int l5= stride + l4;
574
        const int l6= stride + l5;
575
        const int l7= stride + l6;
576
//        const int l8= stride + l7;
577
//        const int l9= stride + l8;
578
        int x;
579

    
580
        src+= stride*3;
581
        for(x=0; x<BLOCK_SIZE; x++)
582
        {
583
                int a= src[l3] - src[l4];
584
                int b= src[l4] - src[l5];
585
                int c= src[l5] - src[l6];
586

    
587
                int d= ABS(b) - ((ABS(a) + ABS(c))>>1);
588
                d= MAX(d, 0);
589

    
590
                if(d < co->QP*2)
591
                {
592
                        int v = d * SIGN(-b);
593

    
594
                        src[l2] +=v>>3;
595
                        src[l3] +=v>>2;
596
                        src[l4] +=(3*v)>>3;
597
                        src[l5] -=(3*v)>>3;
598
                        src[l6] -=v>>2;
599
                        src[l7] -=v>>3;
600

    
601
                }
602
                src++;
603
        }
604
#endif
605
}
606

    
607
static inline void RENAME(doVertDefFilter)(uint8_t src[], int stride, PPContext *c)
608
{
609
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
610
/*
611
        uint8_t tmp[16];
612
        const int l1= stride;
613
        const int l2= stride + l1;
614
        const int l3= stride + l2;
615
        const int l4= (int)tmp - (int)src - stride*3;
616
        const int l5= (int)tmp - (int)src - stride*3 + 8;
617
        const int l6= stride*3 + l3;
618
        const int l7= stride + l6;
619
        const int l8= stride + l7;
620

621
        memcpy(tmp, src+stride*7, 8);
622
        memcpy(tmp+8, src+stride*8, 8);
623
*/
624
        src+= stride*4;
625
        asm volatile(
626

    
627
#if 0 //sligtly more accurate and slightly slower
628
                "pxor %%mm7, %%mm7                                \n\t" // 0
629
                "leal (%0, %1), %%eax                                \n\t"
630
                "leal (%%eax, %1, 4), %%ecx                        \n\t"
631
//        0        1        2        3        4        5        6        7
632
//        %0        %0+%1        %0+2%1        eax+2%1        %0+4%1        eax+4%1        ecx+%1        ecx+2%1
633
//        %0        eax        eax+%1        eax+2%1        %0+4%1        ecx        ecx+%1        ecx+2%1
634

635

636
                "movq (%0, %1, 2), %%mm0                        \n\t" // l2
637
                "movq (%0), %%mm1                                \n\t" // l0
638
                "movq %%mm0, %%mm2                                \n\t" // l2
639
                PAVGB(%%mm7, %%mm0)                                      // ~l2/2
640
                PAVGB(%%mm1, %%mm0)                                      // ~(l2 + 2l0)/4
641
                PAVGB(%%mm2, %%mm0)                                      // ~(5l2 + 2l0)/8
642

643
                "movq (%%eax), %%mm1                                \n\t" // l1
644
                "movq (%%eax, %1, 2), %%mm3                        \n\t" // l3
645
                "movq %%mm1, %%mm4                                \n\t" // l1
646
                PAVGB(%%mm7, %%mm1)                                      // ~l1/2
647
                PAVGB(%%mm3, %%mm1)                                      // ~(l1 + 2l3)/4
648
                PAVGB(%%mm4, %%mm1)                                      // ~(5l1 + 2l3)/8
649

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

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

662
                "movq (%%ecx), %%mm2                                \n\t" // l5
663
                "movq %%mm3, %%mm5                                \n\t" // l3
664
                PAVGB(%%mm7, %%mm3)                                      // ~l3/2
665
                PAVGB(%%mm2, %%mm3)                                      // ~(l3 + 2l5)/4
666
                PAVGB(%%mm5, %%mm3)                                      // ~(5l3 + 2l5)/8
667

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

675
                "movq (%%ecx, %1), %%mm6                        \n\t" // l6
676
                "movq %%mm6, %%mm5                                \n\t" // l6
677
                PAVGB(%%mm7, %%mm6)                                      // ~l6/2
678
                PAVGB(%%mm4, %%mm6)                                      // ~(l6 + 2l4)/4
679
                PAVGB(%%mm5, %%mm6)                                      // ~(5l6 + 2l4)/8
680

681
                "movq (%%ecx, %1, 2), %%mm5                        \n\t" // l7
682
                "movq %%mm2, %%mm4                                \n\t" // l5
683
                PAVGB(%%mm7, %%mm2)                                      // ~l5/2
684
                PAVGB(%%mm5, %%mm2)                                      // ~(l5 + 2l7)/4
685
                PAVGB(%%mm4, %%mm2)                                      // ~(5l5 + 2l7)/8
686

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

693

694
                PMINUB(%%mm2, %%mm1, %%mm4)                              // MIN(|lenergy|,|renergy|)/8
695
                "movq %2, %%mm4                                        \n\t" // QP //FIXME QP+1 ?
696
                "paddusb "MANGLE(b01)", %%mm4                        \n\t"
697
                "pcmpgtb %%mm3, %%mm4                                \n\t" // |menergy|/8 < QP
698
                "psubusb %%mm1, %%mm3                                \n\t" // d=|menergy|/8-MIN(|lenergy|,|renergy|)/8
699
                "pand %%mm4, %%mm3                                \n\t"
700

701
                "movq %%mm3, %%mm1                                \n\t"
702
//                "psubusb "MANGLE(b01)", %%mm3                        \n\t"
703
                PAVGB(%%mm7, %%mm3)
704
                PAVGB(%%mm7, %%mm3)
705
                "paddusb %%mm1, %%mm3                                \n\t"
706
//                "paddusb "MANGLE(b01)", %%mm3                        \n\t"
707

708
                "movq (%%eax, %1, 2), %%mm6                        \n\t" //l3
709
                "movq (%0, %1, 4), %%mm5                        \n\t" //l4
710
                "movq (%0, %1, 4), %%mm4                        \n\t" //l4
711
                "psubusb %%mm6, %%mm5                                \n\t"
712
                "psubusb %%mm4, %%mm6                                \n\t"
713
                "por %%mm6, %%mm5                                \n\t" // |l3-l4|
714
                "pcmpeqb %%mm7, %%mm6                                \n\t" // SIGN(l3-l4)
715
                "pxor %%mm6, %%mm0                                \n\t"
716
                "pand %%mm0, %%mm3                                \n\t"
717
                PMINUB(%%mm5, %%mm3, %%mm0)
718

719
                "psubusb "MANGLE(b01)", %%mm3                        \n\t"
720
                PAVGB(%%mm7, %%mm3)
721

722
                "movq (%%eax, %1, 2), %%mm0                        \n\t"
723
                "movq (%0, %1, 4), %%mm2                        \n\t"
724
                "pxor %%mm6, %%mm0                                \n\t"
725
                "pxor %%mm6, %%mm2                                \n\t"
726
                "psubb %%mm3, %%mm0                                \n\t"
727
                "paddb %%mm3, %%mm2                                \n\t"
728
                "pxor %%mm6, %%mm0                                \n\t"
729
                "pxor %%mm6, %%mm2                                \n\t"
730
                "movq %%mm0, (%%eax, %1, 2)                        \n\t"
731
                "movq %%mm2, (%0, %1, 4)                        \n\t"
732
#endif
733

    
734
                "leal (%0, %1), %%eax                                \n\t"
735
                "pcmpeqb %%mm6, %%mm6                                \n\t" // -1
736
//        0        1        2        3        4        5        6        7
737
//        %0        %0+%1        %0+2%1        eax+2%1        %0+4%1        eax+4%1        ecx+%1        ecx+2%1
738
//        %0        eax        eax+%1        eax+2%1        %0+4%1        ecx        ecx+%1        ecx+2%1
739

    
740

    
741
                "movq (%%eax, %1, 2), %%mm1                        \n\t" // l3
742
                "movq (%0, %1, 4), %%mm0                        \n\t" // l4
743
                "pxor %%mm6, %%mm1                                \n\t" // -l3-1
744
                PAVGB(%%mm1, %%mm0)                                      // -q+128 = (l4-l3+256)/2
745
// mm1=-l3-1, mm0=128-q
746

    
747
                "movq (%%eax, %1, 4), %%mm2                        \n\t" // l5
748
                "movq (%%eax, %1), %%mm3                        \n\t" // l2
749
                "pxor %%mm6, %%mm2                                \n\t" // -l5-1
750
                "movq %%mm2, %%mm5                                \n\t" // -l5-1
751
                "movq "MANGLE(b80)", %%mm4                        \n\t" // 128
752
                "leal (%%eax, %1, 4), %%ecx                        \n\t"
753
                PAVGB(%%mm3, %%mm2)                                      // (l2-l5+256)/2
754
                PAVGB(%%mm0, %%mm4)                                      // ~(l4-l3)/4 + 128
755
                PAVGB(%%mm2, %%mm4)                                      // ~(l2-l5)/4 +(l4-l3)/8 + 128
756
                PAVGB(%%mm0, %%mm4)                                      // ~(l2-l5)/8 +5(l4-l3)/16 + 128
757
// mm1=-l3-1, mm0=128-q, mm3=l2, mm4=menergy/16 + 128, mm5= -l5-1
758

    
759
                "movq (%%eax), %%mm2                                \n\t" // l1
760
                "pxor %%mm6, %%mm2                                \n\t" // -l1-1
761
                PAVGB(%%mm3, %%mm2)                                      // (l2-l1+256)/2
762
                PAVGB((%0), %%mm1)                                      // (l0-l3+256)/2
763
                "movq "MANGLE(b80)", %%mm3                        \n\t" // 128
764
                PAVGB(%%mm2, %%mm3)                                      // ~(l2-l1)/4 + 128
765
                PAVGB(%%mm1, %%mm3)                                      // ~(l0-l3)/4 +(l2-l1)/8 + 128
766
                PAVGB(%%mm2, %%mm3)                                      // ~(l0-l3)/8 +5(l2-l1)/16 + 128
767
// mm0=128-q, mm3=lenergy/16 + 128, mm4= menergy/16 + 128, mm5= -l5-1
768

    
769
                PAVGB((%%ecx, %1), %%mm5)                              // (l6-l5+256)/2
770
                "movq (%%ecx, %1, 2), %%mm1                        \n\t" // l7
771
                "pxor %%mm6, %%mm1                                \n\t" // -l7-1
772
                PAVGB((%0, %1, 4), %%mm1)                              // (l4-l7+256)/2
773
                "movq "MANGLE(b80)", %%mm2                        \n\t" // 128
774
                PAVGB(%%mm5, %%mm2)                                      // ~(l6-l5)/4 + 128
775
                PAVGB(%%mm1, %%mm2)                                      // ~(l4-l7)/4 +(l6-l5)/8 + 128
776
                PAVGB(%%mm5, %%mm2)                                      // ~(l4-l7)/8 +5(l6-l5)/16 + 128
777
// mm0=128-q, mm2=renergy/16 + 128, mm3=lenergy/16 + 128, mm4= menergy/16 + 128
778

    
779
                "movq "MANGLE(b00)", %%mm1                        \n\t" // 0
780
                "movq "MANGLE(b00)", %%mm5                        \n\t" // 0
781
                "psubb %%mm2, %%mm1                                \n\t" // 128 - renergy/16
782
                "psubb %%mm3, %%mm5                                \n\t" // 128 - lenergy/16
783
                PMAXUB(%%mm1, %%mm2)                                      // 128 + |renergy/16|
784
                 PMAXUB(%%mm5, %%mm3)                                      // 128 + |lenergy/16|
785
                PMINUB(%%mm2, %%mm3, %%mm1)                              // 128 + MIN(|lenergy|,|renergy|)/16
786

    
787
// mm0=128-q, mm3=128 + MIN(|lenergy|,|renergy|)/16, mm4= menergy/16 + 128
788

    
789
                "movq "MANGLE(b00)", %%mm7                        \n\t" // 0
790
                "movq %2, %%mm2                                        \n\t" // QP
791
                PAVGB(%%mm6, %%mm2)                                      // 128 + QP/2
792
                "psubb %%mm6, %%mm2                                \n\t"
793

    
794
                "movq %%mm4, %%mm1                                \n\t"
795
                "pcmpgtb %%mm7, %%mm1                                \n\t" // SIGN(menergy)
796
                "pxor %%mm1, %%mm4                                \n\t"
797
                "psubb %%mm1, %%mm4                                \n\t" // 128 + |menergy|/16
798
                "pcmpgtb %%mm4, %%mm2                                \n\t" // |menergy|/16 < QP/2
799
                "psubusb %%mm3, %%mm4                                \n\t" //d=|menergy|/16 - MIN(|lenergy|,|renergy|)/16
800
// mm0=128-q, mm1= SIGN(menergy), mm2= |menergy|/16 < QP/2, mm4= d/16
801

    
802
                "movq %%mm4, %%mm3                                \n\t" // d
803
                "psubusb "MANGLE(b01)", %%mm4                        \n\t"
804
                PAVGB(%%mm7, %%mm4)                                      // d/32
805
                PAVGB(%%mm7, %%mm4)                                      // (d + 32)/64
806
                "paddb %%mm3, %%mm4                                \n\t" // 5d/64
807
                "pand %%mm2, %%mm4                                \n\t"
808

    
809
                "movq "MANGLE(b80)", %%mm5                        \n\t" // 128
810
                "psubb %%mm0, %%mm5                                \n\t" // q
811
                "paddsb %%mm6, %%mm5                                \n\t" // fix bad rounding
812
                "pcmpgtb %%mm5, %%mm7                                \n\t" // SIGN(q)
813
                "pxor %%mm7, %%mm5                                \n\t"
814

    
815
                PMINUB(%%mm5, %%mm4, %%mm3)                              // MIN(|q|, 5d/64)
816
                "pxor %%mm1, %%mm7                                \n\t" // SIGN(d*q)
817

    
818
                "pand %%mm7, %%mm4                                \n\t"
819
                "movq (%%eax, %1, 2), %%mm0                        \n\t"
820
                "movq (%0, %1, 4), %%mm2                        \n\t"
821
                "pxor %%mm1, %%mm0                                \n\t"
822
                "pxor %%mm1, %%mm2                                \n\t"
823
                "paddb %%mm4, %%mm0                                \n\t"
824
                "psubb %%mm4, %%mm2                                \n\t"
825
                "pxor %%mm1, %%mm0                                \n\t"
826
                "pxor %%mm1, %%mm2                                \n\t"
827
                "movq %%mm0, (%%eax, %1, 2)                        \n\t"
828
                "movq %%mm2, (%0, %1, 4)                        \n\t"
829

    
830
                :
831
                : "r" (src), "r" (stride), "m" (c->pQPb)
832
                : "%eax", "%ecx"
833
        );
834

    
835
/*
836
        {
837
        int x;
838
        src-= stride;
839
        for(x=0; x<BLOCK_SIZE; x++)
840
        {
841
                const int middleEnergy= 5*(src[l5] - src[l4]) + 2*(src[l3] - src[l6]);
842
                if(ABS(middleEnergy)< 8*QP)
843
                {
844
                        const int q=(src[l4] - src[l5])/2;
845
                        const int leftEnergy=  5*(src[l3] - src[l2]) + 2*(src[l1] - src[l4]);
846
                        const int rightEnergy= 5*(src[l7] - src[l6]) + 2*(src[l5] - src[l8]);
847

848
                        int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) );
849
                        d= MAX(d, 0);
850

851
                        d= (5*d + 32) >> 6;
852
                        d*= SIGN(-middleEnergy);
853

854
                        if(q>0)
855
                        {
856
                                d= d<0 ? 0 : d;
857
                                d= d>q ? q : d;
858
                        }
859
                        else
860
                        {
861
                                d= d>0 ? 0 : d;
862
                                d= d<q ? q : d;
863
                        }
864

865
                        src[l4]-= d;
866
                        src[l5]+= d;
867
                }
868
                src++;
869
        }
870
src-=8;
871
        for(x=0; x<8; x++)
872
        {
873
                int y;
874
                for(y=4; y<6; y++)
875
                {
876
                        int d= src[x+y*stride] - tmp[x+(y-4)*8];
877
                        int ad= ABS(d);
878
                        static int max=0;
879
                        static int sum=0;
880
                        static int num=0;
881
                        static int bias=0;
882

883
                        if(max<ad) max=ad;
884
                        sum+= ad>3 ? 1 : 0;
885
                        if(ad>3)
886
                        {
887
                                src[0] = src[7] = src[stride*7] = src[(stride+1)*7]=255;
888
                        }
889
                        if(y==4) bias+=d;
890
                        num++;
891
                        if(num%1000000 == 0)
892
                        {
893
                                printf(" %d %d %d %d\n", num, sum, max, bias);
894
                        }
895
                }
896
        }
897
}
898
*/
899
#elif defined (HAVE_MMX)
900
        src+= stride*4;
901
        asm volatile(
902
                "pxor %%mm7, %%mm7                                \n\t"
903
                "leal -40(%%esp), %%ecx                                \n\t" // make space for 4 8-byte vars
904
                "andl $0xFFFFFFF8, %%ecx                        \n\t" // align
905
//        0        1        2        3        4        5        6        7
906
//        %0        %0+%1        %0+2%1        eax+2%1        %0+4%1        eax+4%1        edx+%1        edx+2%1
907
//        %0        eax        eax+%1        eax+2%1        %0+4%1        edx        edx+%1        edx+2%1
908

    
909
                "movq (%0), %%mm0                                \n\t"
910
                "movq %%mm0, %%mm1                                \n\t"
911
                "punpcklbw %%mm7, %%mm0                                \n\t" // low part of line 0
912
                "punpckhbw %%mm7, %%mm1                                \n\t" // high part of line 0
913

    
914
                "movq (%0, %1), %%mm2                                \n\t"
915
                "leal (%0, %1, 2), %%eax                        \n\t"
916
                "movq %%mm2, %%mm3                                \n\t"
917
                "punpcklbw %%mm7, %%mm2                                \n\t" // low part of line 1
918
                "punpckhbw %%mm7, %%mm3                                \n\t" // high part of line 1
919

    
920
                "movq (%%eax), %%mm4                                \n\t"
921
                "movq %%mm4, %%mm5                                \n\t"
922
                "punpcklbw %%mm7, %%mm4                                \n\t" // low part of line 2
923
                "punpckhbw %%mm7, %%mm5                                \n\t" // high part of line 2
924

    
925
                "paddw %%mm0, %%mm0                                \n\t" // 2L0
926
                "paddw %%mm1, %%mm1                                \n\t" // 2H0
927
                "psubw %%mm4, %%mm2                                \n\t" // L1 - L2
928
                "psubw %%mm5, %%mm3                                \n\t" // H1 - H2
929
                "psubw %%mm2, %%mm0                                \n\t" // 2L0 - L1 + L2
930
                "psubw %%mm3, %%mm1                                \n\t" // 2H0 - H1 + H2
931

    
932
                "psllw $2, %%mm2                                \n\t" // 4L1 - 4L2
933
                "psllw $2, %%mm3                                \n\t" // 4H1 - 4H2
934
                "psubw %%mm2, %%mm0                                \n\t" // 2L0 - 5L1 + 5L2
935
                "psubw %%mm3, %%mm1                                \n\t" // 2H0 - 5H1 + 5H2
936

    
937
                "movq (%%eax, %1), %%mm2                        \n\t"
938
                "movq %%mm2, %%mm3                                \n\t"
939
                "punpcklbw %%mm7, %%mm2                                \n\t" // L3
940
                "punpckhbw %%mm7, %%mm3                                \n\t" // H3
941

    
942
                "psubw %%mm2, %%mm0                                \n\t" // 2L0 - 5L1 + 5L2 - L3
943
                "psubw %%mm3, %%mm1                                \n\t" // 2H0 - 5H1 + 5H2 - H3
944
                "psubw %%mm2, %%mm0                                \n\t" // 2L0 - 5L1 + 5L2 - 2L3
945
                "psubw %%mm3, %%mm1                                \n\t" // 2H0 - 5H1 + 5H2 - 2H3
946
                "movq %%mm0, (%%ecx)                                \n\t" // 2L0 - 5L1 + 5L2 - 2L3
947
                "movq %%mm1, 8(%%ecx)                                \n\t" // 2H0 - 5H1 + 5H2 - 2H3
948

    
949
                "movq (%%eax, %1, 2), %%mm0                        \n\t"
950
                "movq %%mm0, %%mm1                                \n\t"
951
                "punpcklbw %%mm7, %%mm0                                \n\t" // L4
952
                "punpckhbw %%mm7, %%mm1                                \n\t" // H4
953

    
954
                "psubw %%mm0, %%mm2                                \n\t" // L3 - L4
955
                "psubw %%mm1, %%mm3                                \n\t" // H3 - H4
956
                "movq %%mm2, 16(%%ecx)                                \n\t" // L3 - L4
957
                "movq %%mm3, 24(%%ecx)                                \n\t" // H3 - H4
958
                "paddw %%mm4, %%mm4                                \n\t" // 2L2
959
                "paddw %%mm5, %%mm5                                \n\t" // 2H2
960
                "psubw %%mm2, %%mm4                                \n\t" // 2L2 - L3 + L4
961
                "psubw %%mm3, %%mm5                                \n\t" // 2H2 - H3 + H4
962

    
963
                "leal (%%eax, %1), %0                                \n\t"
964
                "psllw $2, %%mm2                                \n\t" // 4L3 - 4L4
965
                "psllw $2, %%mm3                                \n\t" // 4H3 - 4H4
966
                "psubw %%mm2, %%mm4                                \n\t" // 2L2 - 5L3 + 5L4
967
                "psubw %%mm3, %%mm5                                \n\t" // 2H2 - 5H3 + 5H4
968
//50 opcodes so far
969
                "movq (%0, %1, 2), %%mm2                        \n\t"
970
                "movq %%mm2, %%mm3                                \n\t"
971
                "punpcklbw %%mm7, %%mm2                                \n\t" // L5
972
                "punpckhbw %%mm7, %%mm3                                \n\t" // H5
973
                "psubw %%mm2, %%mm4                                \n\t" // 2L2 - 5L3 + 5L4 - L5
974
                "psubw %%mm3, %%mm5                                \n\t" // 2H2 - 5H3 + 5H4 - H5
975
                "psubw %%mm2, %%mm4                                \n\t" // 2L2 - 5L3 + 5L4 - 2L5
976
                "psubw %%mm3, %%mm5                                \n\t" // 2H2 - 5H3 + 5H4 - 2H5
977

    
978
                "movq (%%eax, %1, 4), %%mm6                        \n\t"
979
                "punpcklbw %%mm7, %%mm6                                \n\t" // L6
980
                "psubw %%mm6, %%mm2                                \n\t" // L5 - L6
981
                "movq (%%eax, %1, 4), %%mm6                        \n\t"
982
                "punpckhbw %%mm7, %%mm6                                \n\t" // H6
983
                "psubw %%mm6, %%mm3                                \n\t" // H5 - H6
984

    
985
                "paddw %%mm0, %%mm0                                \n\t" // 2L4
986
                "paddw %%mm1, %%mm1                                \n\t" // 2H4
987
                "psubw %%mm2, %%mm0                                \n\t" // 2L4 - L5 + L6
988
                "psubw %%mm3, %%mm1                                \n\t" // 2H4 - H5 + H6
989

    
990
                "psllw $2, %%mm2                                \n\t" // 4L5 - 4L6
991
                "psllw $2, %%mm3                                \n\t" // 4H5 - 4H6
992
                "psubw %%mm2, %%mm0                                \n\t" // 2L4 - 5L5 + 5L6
993
                "psubw %%mm3, %%mm1                                \n\t" // 2H4 - 5H5 + 5H6
994

    
995
                "movq (%0, %1, 4), %%mm2                        \n\t"
996
                "movq %%mm2, %%mm3                                \n\t"
997
                "punpcklbw %%mm7, %%mm2                                \n\t" // L7
998
                "punpckhbw %%mm7, %%mm3                                \n\t" // H7
999

    
1000
                "paddw %%mm2, %%mm2                                \n\t" // 2L7
1001
                "paddw %%mm3, %%mm3                                \n\t" // 2H7
1002
                "psubw %%mm2, %%mm0                                \n\t" // 2L4 - 5L5 + 5L6 - 2L7
1003
                "psubw %%mm3, %%mm1                                \n\t" // 2H4 - 5H5 + 5H6 - 2H7
1004

    
1005
                "movq (%%ecx), %%mm2                                \n\t" // 2L0 - 5L1 + 5L2 - 2L3
1006
                "movq 8(%%ecx), %%mm3                                \n\t" // 2H0 - 5H1 + 5H2 - 2H3
1007

    
1008
#ifdef HAVE_MMX2
1009
                "movq %%mm7, %%mm6                                \n\t" // 0
1010
                "psubw %%mm0, %%mm6                                \n\t"
1011
                "pmaxsw %%mm6, %%mm0                                \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
1012
                "movq %%mm7, %%mm6                                \n\t" // 0
1013
                "psubw %%mm1, %%mm6                                \n\t"
1014
                "pmaxsw %%mm6, %%mm1                                \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
1015
                "movq %%mm7, %%mm6                                \n\t" // 0
1016
                "psubw %%mm2, %%mm6                                \n\t"
1017
                "pmaxsw %%mm6, %%mm2                                \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
1018
                "movq %%mm7, %%mm6                                \n\t" // 0
1019
                "psubw %%mm3, %%mm6                                \n\t"
1020
                "pmaxsw %%mm6, %%mm3                                \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
1021
#else
1022
                "movq %%mm7, %%mm6                                \n\t" // 0
1023
                "pcmpgtw %%mm0, %%mm6                                \n\t"
1024
                "pxor %%mm6, %%mm0                                \n\t"
1025
                "psubw %%mm6, %%mm0                                \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
1026
                "movq %%mm7, %%mm6                                \n\t" // 0
1027
                "pcmpgtw %%mm1, %%mm6                                \n\t"
1028
                "pxor %%mm6, %%mm1                                \n\t"
1029
                "psubw %%mm6, %%mm1                                \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
1030
                "movq %%mm7, %%mm6                                \n\t" // 0
1031
                "pcmpgtw %%mm2, %%mm6                                \n\t"
1032
                "pxor %%mm6, %%mm2                                \n\t"
1033
                "psubw %%mm6, %%mm2                                \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
1034
                "movq %%mm7, %%mm6                                \n\t" // 0
1035
                "pcmpgtw %%mm3, %%mm6                                \n\t"
1036
                "pxor %%mm6, %%mm3                                \n\t"
1037
                "psubw %%mm6, %%mm3                                \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
1038
#endif
1039

    
1040
#ifdef HAVE_MMX2
1041
                "pminsw %%mm2, %%mm0                                \n\t"
1042
                "pminsw %%mm3, %%mm1                                \n\t"
1043
#else
1044
                "movq %%mm0, %%mm6                                \n\t"
1045
                "psubusw %%mm2, %%mm6                                \n\t"
1046
                "psubw %%mm6, %%mm0                                \n\t"
1047
                "movq %%mm1, %%mm6                                \n\t"
1048
                "psubusw %%mm3, %%mm6                                \n\t"
1049
                "psubw %%mm6, %%mm1                                \n\t"
1050
#endif
1051

    
1052
                "movq %%mm7, %%mm6                                \n\t" // 0
1053
                "pcmpgtw %%mm4, %%mm6                                \n\t" // sign(2L2 - 5L3 + 5L4 - 2L5)
1054
                "pxor %%mm6, %%mm4                                \n\t"
1055
                "psubw %%mm6, %%mm4                                \n\t" // |2L2 - 5L3 + 5L4 - 2L5|
1056
                "pcmpgtw %%mm5, %%mm7                                \n\t" // sign(2H2 - 5H3 + 5H4 - 2H5)
1057
                "pxor %%mm7, %%mm5                                \n\t"
1058
                "psubw %%mm7, %%mm5                                \n\t" // |2H2 - 5H3 + 5H4 - 2H5|
1059
// 100 opcodes
1060
                "movd %2, %%mm2                                        \n\t" // QP
1061
                "psllw $3, %%mm2                                \n\t" // 8QP
1062
                "movq %%mm2, %%mm3                                \n\t" // 8QP
1063
                "pcmpgtw %%mm4, %%mm2                                \n\t"
1064
                "pcmpgtw %%mm5, %%mm3                                \n\t"
1065
                "pand %%mm2, %%mm4                                \n\t"
1066
                "pand %%mm3, %%mm5                                \n\t"
1067

    
1068

    
1069
                "psubusw %%mm0, %%mm4                                \n\t" // hd
1070
                "psubusw %%mm1, %%mm5                                \n\t" // ld
1071

    
1072

    
1073
                "movq "MANGLE(w05)", %%mm2                        \n\t" // 5
1074
                "pmullw %%mm2, %%mm4                                \n\t"
1075
                "pmullw %%mm2, %%mm5                                \n\t"
1076
                "movq "MANGLE(w20)", %%mm2                        \n\t" // 32
1077
                "paddw %%mm2, %%mm4                                \n\t"
1078
                "paddw %%mm2, %%mm5                                \n\t"
1079
                "psrlw $6, %%mm4                                \n\t"
1080
                "psrlw $6, %%mm5                                \n\t"
1081

    
1082
                "movq 16(%%ecx), %%mm0                                \n\t" // L3 - L4
1083
                "movq 24(%%ecx), %%mm1                                \n\t" // H3 - H4
1084

    
1085
                "pxor %%mm2, %%mm2                                \n\t"
1086
                "pxor %%mm3, %%mm3                                \n\t"
1087

    
1088
                "pcmpgtw %%mm0, %%mm2                                \n\t" // sign (L3-L4)
1089
                "pcmpgtw %%mm1, %%mm3                                \n\t" // sign (H3-H4)
1090
                "pxor %%mm2, %%mm0                                \n\t"
1091
                "pxor %%mm3, %%mm1                                \n\t"
1092
                "psubw %%mm2, %%mm0                                \n\t" // |L3-L4|
1093
                "psubw %%mm3, %%mm1                                \n\t" // |H3-H4|
1094
                "psrlw $1, %%mm0                                \n\t" // |L3 - L4|/2
1095
                "psrlw $1, %%mm1                                \n\t" // |H3 - H4|/2
1096

    
1097
                "pxor %%mm6, %%mm2                                \n\t"
1098
                "pxor %%mm7, %%mm3                                \n\t"
1099
                "pand %%mm2, %%mm4                                \n\t"
1100
                "pand %%mm3, %%mm5                                \n\t"
1101

    
1102
#ifdef HAVE_MMX2
1103
                "pminsw %%mm0, %%mm4                                \n\t"
1104
                "pminsw %%mm1, %%mm5                                \n\t"
1105
#else
1106
                "movq %%mm4, %%mm2                                \n\t"
1107
                "psubusw %%mm0, %%mm2                                \n\t"
1108
                "psubw %%mm2, %%mm4                                \n\t"
1109
                "movq %%mm5, %%mm2                                \n\t"
1110
                "psubusw %%mm1, %%mm2                                \n\t"
1111
                "psubw %%mm2, %%mm5                                \n\t"
1112
#endif
1113
                "pxor %%mm6, %%mm4                                \n\t"
1114
                "pxor %%mm7, %%mm5                                \n\t"
1115
                "psubw %%mm6, %%mm4                                \n\t"
1116
                "psubw %%mm7, %%mm5                                \n\t"
1117
                "packsswb %%mm5, %%mm4                                \n\t"
1118
                "movq (%0), %%mm0                                \n\t"
1119
                "paddb   %%mm4, %%mm0                                \n\t"
1120
                "movq %%mm0, (%0)                                \n\t"
1121
                "movq (%0, %1), %%mm0                                \n\t"
1122
                "psubb %%mm4, %%mm0                                \n\t"
1123
                "movq %%mm0, (%0, %1)                                \n\t"
1124

    
1125
                : "+r" (src)
1126
                : "r" (stride), "m" (c->pQPb)
1127
                : "%eax", "%ecx"
1128
        );
1129
#else
1130
        const int l1= stride;
1131
        const int l2= stride + l1;
1132
        const int l3= stride + l2;
1133
        const int l4= stride + l3;
1134
        const int l5= stride + l4;
1135
        const int l6= stride + l5;
1136
        const int l7= stride + l6;
1137
        const int l8= stride + l7;
1138
//        const int l9= stride + l8;
1139
        int x;
1140
        src+= stride*3;
1141
        for(x=0; x<BLOCK_SIZE; x++)
1142
        {
1143
                const int middleEnergy= 5*(src[l5] - src[l4]) + 2*(src[l3] - src[l6]);
1144
                if(ABS(middleEnergy) < 8*c->QP)
1145
                {
1146
                        const int q=(src[l4] - src[l5])/2;
1147
                        const int leftEnergy=  5*(src[l3] - src[l2]) + 2*(src[l1] - src[l4]);
1148
                        const int rightEnergy= 5*(src[l7] - src[l6]) + 2*(src[l5] - src[l8]);
1149

    
1150
                        int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) );
1151
                        d= MAX(d, 0);
1152

    
1153
                        d= (5*d + 32) >> 6;
1154
                        d*= SIGN(-middleEnergy);
1155

    
1156
                        if(q>0)
1157
                        {
1158
                                d= d<0 ? 0 : d;
1159
                                d= d>q ? q : d;
1160
                        }
1161
                        else
1162
                        {
1163
                                d= d>0 ? 0 : d;
1164
                                d= d<q ? q : d;
1165
                        }
1166

    
1167
                        src[l4]-= d;
1168
                        src[l5]+= d;
1169
                }
1170
                src++;
1171
        }
1172
#endif
1173
}
1174

    
1175
static inline void RENAME(dering)(uint8_t src[], int stride, PPContext *c)
1176
{
1177
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1178
        asm volatile(
1179
                "pxor %%mm6, %%mm6                                \n\t"
1180
                "pcmpeqb %%mm7, %%mm7                                \n\t"
1181
                "movq %2, %%mm0                                        \n\t"
1182
                "punpcklbw %%mm6, %%mm0                                \n\t"
1183
                "psrlw $1, %%mm0                                \n\t"
1184
                "psubw %%mm7, %%mm0                                \n\t"
1185
                "packuswb %%mm0, %%mm0                                \n\t"
1186
                "movq %%mm0, %3                                        \n\t"
1187

    
1188
                "leal (%0, %1), %%eax                                \n\t"
1189
                "leal (%%eax, %1, 4), %%edx                        \n\t"
1190
                
1191
//        0        1        2        3        4        5        6        7        8        9
1192
//        %0        eax        eax+%1        eax+2%1        %0+4%1        edx        edx+%1        edx+2%1        %0+8%1        edx+4%1
1193

    
1194
#undef FIND_MIN_MAX
1195
#ifdef HAVE_MMX2
1196
#define FIND_MIN_MAX(addr)\
1197
                "movq " #addr ", %%mm0                                \n\t"\
1198
                "pminub %%mm0, %%mm7                                \n\t"\
1199
                "pmaxub %%mm0, %%mm6                                \n\t"
1200
#else
1201
#define FIND_MIN_MAX(addr)\
1202
                "movq " #addr ", %%mm0                                \n\t"\
1203
                "movq %%mm7, %%mm1                                \n\t"\
1204
                "psubusb %%mm0, %%mm6                                \n\t"\
1205
                "paddb %%mm0, %%mm6                                \n\t"\
1206
                "psubusb %%mm0, %%mm1                                \n\t"\
1207
                "psubb %%mm1, %%mm7                                \n\t"
1208
#endif
1209

    
1210
FIND_MIN_MAX((%%eax))
1211
FIND_MIN_MAX((%%eax, %1))
1212
FIND_MIN_MAX((%%eax, %1, 2))
1213
FIND_MIN_MAX((%0, %1, 4))
1214
FIND_MIN_MAX((%%edx))
1215
FIND_MIN_MAX((%%edx, %1))
1216
FIND_MIN_MAX((%%edx, %1, 2))
1217
FIND_MIN_MAX((%0, %1, 8))
1218

    
1219
                "movq %%mm7, %%mm4                                \n\t"
1220
                "psrlq $8, %%mm7                                \n\t"
1221
#ifdef HAVE_MMX2
1222
                "pminub %%mm4, %%mm7                                \n\t" // min of pixels
1223
                "pshufw $0xF9, %%mm7, %%mm4                        \n\t"
1224
                "pminub %%mm4, %%mm7                                \n\t" // min of pixels
1225
                "pshufw $0xFE, %%mm7, %%mm4                        \n\t"
1226
                "pminub %%mm4, %%mm7                                \n\t"
1227
#else
1228
                "movq %%mm7, %%mm1                                \n\t"
1229
                "psubusb %%mm4, %%mm1                                \n\t"
1230
                "psubb %%mm1, %%mm7                                \n\t"
1231
                "movq %%mm7, %%mm4                                \n\t"
1232
                "psrlq $16, %%mm7                                \n\t"
1233
                "movq %%mm7, %%mm1                                \n\t"
1234
                "psubusb %%mm4, %%mm1                                \n\t"
1235
                "psubb %%mm1, %%mm7                                \n\t"
1236
                "movq %%mm7, %%mm4                                \n\t"
1237
                "psrlq $32, %%mm7                                \n\t"
1238
                "movq %%mm7, %%mm1                                \n\t"
1239
                "psubusb %%mm4, %%mm1                                \n\t"
1240
                "psubb %%mm1, %%mm7                                \n\t"
1241
#endif
1242

    
1243

    
1244
                "movq %%mm6, %%mm4                                \n\t"
1245
                "psrlq $8, %%mm6                                \n\t"
1246
#ifdef HAVE_MMX2
1247
                "pmaxub %%mm4, %%mm6                                \n\t" // max of pixels
1248
                "pshufw $0xF9, %%mm6, %%mm4                        \n\t"
1249
                "pmaxub %%mm4, %%mm6                                \n\t"
1250
                "pshufw $0xFE, %%mm6, %%mm4                        \n\t"
1251
                "pmaxub %%mm4, %%mm6                                \n\t"
1252
#else
1253
                "psubusb %%mm4, %%mm6                                \n\t"
1254
                "paddb %%mm4, %%mm6                                \n\t"
1255
                "movq %%mm6, %%mm4                                \n\t"
1256
                "psrlq $16, %%mm6                                \n\t"
1257
                "psubusb %%mm4, %%mm6                                \n\t"
1258
                "paddb %%mm4, %%mm6                                \n\t"
1259
                "movq %%mm6, %%mm4                                \n\t"
1260
                "psrlq $32, %%mm6                                \n\t"
1261
                "psubusb %%mm4, %%mm6                                \n\t"
1262
                "paddb %%mm4, %%mm6                                \n\t"
1263
#endif
1264
                "movq %%mm6, %%mm0                                \n\t" // max
1265
                "psubb %%mm7, %%mm6                                \n\t" // max - min
1266
                "movd %%mm6, %%ecx                                \n\t"
1267
                "cmpb "MANGLE(deringThreshold)", %%cl                \n\t"
1268
                " jb 1f                                                \n\t"
1269
                "leal -24(%%esp), %%ecx                                \n\t"
1270
                "andl $0xFFFFFFF8, %%ecx                        \n\t" 
1271
                PAVGB(%%mm0, %%mm7)                                      // a=(max + min)/2
1272
                "punpcklbw %%mm7, %%mm7                                \n\t"
1273
                "punpcklbw %%mm7, %%mm7                                \n\t"
1274
                "punpcklbw %%mm7, %%mm7                                \n\t"
1275
                "movq %%mm7, (%%ecx)                                \n\t"
1276

    
1277
                "movq (%0), %%mm0                                \n\t" // L10
1278
                "movq %%mm0, %%mm1                                \n\t" // L10
1279
                "movq %%mm0, %%mm2                                \n\t" // L10
1280
                "psllq $8, %%mm1                                \n\t"
1281
                "psrlq $8, %%mm2                                \n\t"
1282
                "movd -4(%0), %%mm3                                \n\t"
1283
                "movd 8(%0), %%mm4                                \n\t"
1284
                "psrlq $24, %%mm3                                \n\t"
1285
                "psllq $56, %%mm4                                \n\t"
1286
                "por %%mm3, %%mm1                                \n\t" // L00
1287
                "por %%mm4, %%mm2                                \n\t" // L20
1288
                "movq %%mm1, %%mm3                                \n\t" // L00
1289
                PAVGB(%%mm2, %%mm1)                                      // (L20 + L00)/2
1290
                PAVGB(%%mm0, %%mm1)                                      // (L20 + L00 + 2L10)/4
1291
                "psubusb %%mm7, %%mm0                                \n\t"
1292
                "psubusb %%mm7, %%mm2                                \n\t"
1293
                "psubusb %%mm7, %%mm3                                \n\t"
1294
                "pcmpeqb "MANGLE(b00)", %%mm0                        \n\t" // L10 > a ? 0 : -1
1295
                "pcmpeqb "MANGLE(b00)", %%mm2                        \n\t" // L20 > a ? 0 : -1
1296
                "pcmpeqb "MANGLE(b00)", %%mm3                        \n\t" // L00 > a ? 0 : -1
1297
                "paddb %%mm2, %%mm0                                \n\t"
1298
                "paddb %%mm3, %%mm0                                \n\t"
1299

    
1300
                "movq (%%eax), %%mm2                                \n\t" // L11
1301
                "movq %%mm2, %%mm3                                \n\t" // L11
1302
                "movq %%mm2, %%mm4                                \n\t" // L11
1303
                "psllq $8, %%mm3                                \n\t"
1304
                "psrlq $8, %%mm4                                \n\t"
1305
                "movd -4(%%eax), %%mm5                                \n\t"
1306
                "movd 8(%%eax), %%mm6                                \n\t"
1307
                "psrlq $24, %%mm5                                \n\t"
1308
                "psllq $56, %%mm6                                \n\t"
1309
                "por %%mm5, %%mm3                                \n\t" // L01
1310
                "por %%mm6, %%mm4                                \n\t" // L21
1311
                "movq %%mm3, %%mm5                                \n\t" // L01
1312
                PAVGB(%%mm4, %%mm3)                                      // (L21 + L01)/2
1313
                PAVGB(%%mm2, %%mm3)                                      // (L21 + L01 + 2L11)/4
1314
                "psubusb %%mm7, %%mm2                                \n\t"
1315
                "psubusb %%mm7, %%mm4                                \n\t"
1316
                "psubusb %%mm7, %%mm5                                \n\t"
1317
                "pcmpeqb "MANGLE(b00)", %%mm2                        \n\t" // L11 > a ? 0 : -1
1318
                "pcmpeqb "MANGLE(b00)", %%mm4                        \n\t" // L21 > a ? 0 : -1
1319
                "pcmpeqb "MANGLE(b00)", %%mm5                        \n\t" // L01 > a ? 0 : -1
1320
                "paddb %%mm4, %%mm2                                \n\t"
1321
                "paddb %%mm5, %%mm2                                \n\t"
1322
// 0, 2, 3, 1
1323
#define DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1) \
1324
                "movq " #src ", " #sx "                                \n\t" /* src[0] */\
1325
                "movq " #sx ", " #lx "                                \n\t" /* src[0] */\
1326
                "movq " #sx ", " #t0 "                                \n\t" /* src[0] */\
1327
                "psllq $8, " #lx "                                \n\t"\
1328
                "psrlq $8, " #t0 "                                \n\t"\
1329
                "movd -4" #src ", " #t1 "                        \n\t"\
1330
                "psrlq $24, " #t1 "                                \n\t"\
1331
                "por " #t1 ", " #lx "                                \n\t" /* src[-1] */\
1332
                "movd 8" #src ", " #t1 "                        \n\t"\
1333
                "psllq $56, " #t1 "                                \n\t"\
1334
                "por " #t1 ", " #t0 "                                \n\t" /* src[+1] */\
1335
                "movq " #lx ", " #t1 "                                \n\t" /* src[-1] */\
1336
                PAVGB(t0, lx)                                              /* (src[-1] + src[+1])/2 */\
1337
                PAVGB(sx, lx)                                      /* (src[-1] + 2src[0] + src[+1])/4 */\
1338
                PAVGB(lx, pplx)                                             \
1339
                "movq " #lx ", 8(%%ecx)                                \n\t"\
1340
                "movq (%%ecx), " #lx "                                \n\t"\
1341
                "psubusb " #lx ", " #t1 "                        \n\t"\
1342
                "psubusb " #lx ", " #t0 "                        \n\t"\
1343
                "psubusb " #lx ", " #sx "                        \n\t"\
1344
                "movq "MANGLE(b00)", " #lx "                        \n\t"\
1345
                "pcmpeqb " #lx ", " #t1 "                        \n\t" /* src[-1] > a ? 0 : -1*/\
1346
                "pcmpeqb " #lx ", " #t0 "                        \n\t" /* src[+1] > a ? 0 : -1*/\
1347
                "pcmpeqb " #lx ", " #sx "                        \n\t" /* src[0]  > a ? 0 : -1*/\
1348
                "paddb " #t1 ", " #t0 "                                \n\t"\
1349
                "paddb " #t0 ", " #sx "                                \n\t"\
1350
\
1351
                PAVGB(plx, pplx)                                      /* filtered */\
1352
                "movq " #dst ", " #t0 "                                \n\t" /* dst */\
1353
                "movq " #t0 ", " #t1 "                                \n\t" /* dst */\
1354
                "psubusb %3, " #t0 "                                \n\t"\
1355
                "paddusb %3, " #t1 "                                \n\t"\
1356
                PMAXUB(t0, pplx)\
1357
                PMINUB(t1, pplx, t0)\
1358
                "paddb " #sx ", " #ppsx "                        \n\t"\
1359
                "paddb " #psx ", " #ppsx "                        \n\t"\
1360
                "#paddb "MANGLE(b02)", " #ppsx "                \n\t"\
1361
                "pand "MANGLE(b08)", " #ppsx "                        \n\t"\
1362
                "pcmpeqb " #lx ", " #ppsx "                        \n\t"\
1363
                "pand " #ppsx ", " #pplx "                        \n\t"\
1364
                "pandn " #dst ", " #ppsx "                        \n\t"\
1365
                "por " #pplx ", " #ppsx "                        \n\t"\
1366
                "movq " #ppsx ", " #dst "                        \n\t"\
1367
                "movq 8(%%ecx), " #lx "                                \n\t"
1368

    
1369
/*
1370
0000000
1371
1111111
1372

1373
1111110
1374
1111101
1375
1111100
1376
1111011
1377
1111010
1378
1111001
1379

1380
1111000
1381
1110111
1382

1383
*/
1384
//DERING_CORE(dst,src                  ,ppsx ,psx  ,sx   ,pplx ,plx  ,lx   ,t0   ,t1)
1385
DERING_CORE((%%eax),(%%eax, %1)        ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
1386
DERING_CORE((%%eax, %1),(%%eax, %1, 2) ,%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
1387
DERING_CORE((%%eax, %1, 2),(%0, %1, 4) ,%%mm4,%%mm0,%%mm2,%%mm5,%%mm1,%%mm3,%%mm6,%%mm7)
1388
DERING_CORE((%0, %1, 4),(%%edx)        ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
1389
DERING_CORE((%%edx),(%%edx, %1)        ,%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
1390
DERING_CORE((%%edx, %1), (%%edx, %1, 2),%%mm4,%%mm0,%%mm2,%%mm5,%%mm1,%%mm3,%%mm6,%%mm7)
1391
DERING_CORE((%%edx, %1, 2),(%0, %1, 8) ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
1392
DERING_CORE((%0, %1, 8),(%%edx, %1, 4) ,%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
1393

    
1394
                "1:                        \n\t"
1395
                : : "r" (src), "r" (stride), "m" (c->pQPb), "m"(c->pQPb2)
1396
                : "%eax", "%edx", "%ecx"
1397
        );
1398
#else
1399
        int y;
1400
        int min=255;
1401
        int max=0;
1402
        int avg;
1403
        uint8_t *p;
1404
        int s[10];
1405
        const int QP2= c->QP/2 + 1;
1406

    
1407
        for(y=1; y<9; y++)
1408
        {
1409
                int x;
1410
                p= src + stride*y;
1411
                for(x=1; x<9; x++)
1412
                {
1413
                        p++;
1414
                        if(*p > max) max= *p;
1415
                        if(*p < min) min= *p;
1416
                }
1417
        }
1418
        avg= (min + max + 1)>>1;
1419

    
1420
        if(max - min <deringThreshold) return;
1421

    
1422
        for(y=0; y<10; y++)
1423
        {
1424
                int t = 0;
1425

    
1426
                if(src[stride*y + 0] > avg) t+= 1;
1427
                if(src[stride*y + 1] > avg) t+= 2;
1428
                if(src[stride*y + 2] > avg) t+= 4;
1429
                if(src[stride*y + 3] > avg) t+= 8;
1430
                if(src[stride*y + 4] > avg) t+= 16;
1431
                if(src[stride*y + 5] > avg) t+= 32;
1432
                if(src[stride*y + 6] > avg) t+= 64;
1433
                if(src[stride*y + 7] > avg) t+= 128;
1434
                if(src[stride*y + 8] > avg) t+= 256;
1435
                if(src[stride*y + 9] > avg) t+= 512;
1436
                
1437
                t |= (~t)<<16;
1438
                t &= (t<<1) & (t>>1);
1439
                s[y] = t;
1440
        }
1441
        
1442
        for(y=1; y<9; y++)
1443
        {
1444
                int t = s[y-1] & s[y] & s[y+1];
1445
                t|= t>>16;
1446
                s[y-1]= t;
1447
        }
1448

    
1449
        for(y=1; y<9; y++)
1450
        {
1451
                int x;
1452
                int t = s[y-1];
1453

    
1454
                p= src + stride*y;
1455
                for(x=1; x<9; x++)
1456
                {
1457
                        p++;
1458
                        if(t & (1<<x))
1459
                        {
1460
                                int f= (*(p-stride-1)) + 2*(*(p-stride)) + (*(p-stride+1))
1461
                                      +2*(*(p     -1)) + 4*(*p         ) + 2*(*(p     +1))
1462
                                      +(*(p+stride-1)) + 2*(*(p+stride)) + (*(p+stride+1));
1463
                                f= (f + 8)>>4;
1464

    
1465
#ifdef DEBUG_DERING_THRESHOLD
1466
                                asm volatile("emms\n\t":);
1467
                                {
1468
                                static long long numPixels=0;
1469
                                if(x!=1 && x!=8 && y!=1 && y!=8) numPixels++;
1470
//                                if((max-min)<20 || (max-min)*QP<200)
1471
//                                if((max-min)*QP < 500)
1472
//                                if(max-min<QP/2)
1473
                                if(max-min < 20)
1474
                                {
1475
                                        static int numSkiped=0;
1476
                                        static int errorSum=0;
1477
                                        static int worstQP=0;
1478
                                        static int worstRange=0;
1479
                                        static int worstDiff=0;
1480
                                        int diff= (f - *p);
1481
                                        int absDiff= ABS(diff);
1482
                                        int error= diff*diff;
1483

    
1484
                                        if(x==1 || x==8 || y==1 || y==8) continue;
1485

    
1486
                                        numSkiped++;
1487
                                        if(absDiff > worstDiff)
1488
                                        {
1489
                                                worstDiff= absDiff;
1490
                                                worstQP= QP;
1491
                                                worstRange= max-min;
1492
                                        }
1493
                                        errorSum+= error;
1494

    
1495
                                        if(1024LL*1024LL*1024LL % numSkiped == 0)
1496
                                        {
1497
                                                printf( "sum:%1.3f, skip:%d, wQP:%d, "
1498
                                                        "wRange:%d, wDiff:%d, relSkip:%1.3f\n",
1499
                                                        (float)errorSum/numSkiped, numSkiped, worstQP, worstRange,
1500
                                                        worstDiff, (float)numSkiped/numPixels);
1501
                                        }
1502
                                }
1503
                                }
1504
#endif
1505
                                if     (*p + QP2 < f) *p= *p + QP2;
1506
                                else if(*p - QP2 > f) *p= *p - QP2;
1507
                                else *p=f;
1508
                        }
1509
                }
1510
        }
1511
#ifdef DEBUG_DERING_THRESHOLD
1512
        if(max-min < 20)
1513
        {
1514
                for(y=1; y<9; y++)
1515
                {
1516
                        int x;
1517
                        int t = 0;
1518
                        p= src + stride*y;
1519
                        for(x=1; x<9; x++)
1520
                        {
1521
                                p++;
1522
                                *p = MIN(*p + 20, 255);
1523
                        }
1524
                }
1525
//                src[0] = src[7]=src[stride*7]=src[stride*7 + 7]=255;
1526
        }
1527
#endif
1528
#endif
1529
}
1530

    
1531
/**
1532
 * Deinterlaces the given block by linearly interpolating every second line.
1533
 * will be called for every 8x8 block and can read & write from line 4-15
1534
 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
1535
 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1536
 */
1537
static inline void RENAME(deInterlaceInterpolateLinear)(uint8_t src[], int stride)
1538
{
1539
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1540
        src+= 4*stride;
1541
        asm volatile(
1542
                "leal (%0, %1), %%eax                                \n\t"
1543
                "leal (%%eax, %1, 4), %%ecx                        \n\t"
1544
//        0        1        2        3        4        5        6        7        8        9
1545
//        %0        eax        eax+%1        eax+2%1        %0+4%1        ecx        ecx+%1        ecx+2%1        %0+8%1        ecx+4%1
1546

    
1547
                "movq (%0), %%mm0                                \n\t"
1548
                "movq (%%eax, %1), %%mm1                        \n\t"
1549
                PAVGB(%%mm1, %%mm0)
1550
                "movq %%mm0, (%%eax)                                \n\t"
1551
                "movq (%0, %1, 4), %%mm0                        \n\t"
1552
                PAVGB(%%mm0, %%mm1)
1553
                "movq %%mm1, (%%eax, %1, 2)                        \n\t"
1554
                "movq (%%ecx, %1), %%mm1                        \n\t"
1555
                PAVGB(%%mm1, %%mm0)
1556
                "movq %%mm0, (%%ecx)                                \n\t"
1557
                "movq (%0, %1, 8), %%mm0                        \n\t"
1558
                PAVGB(%%mm0, %%mm1)
1559
                "movq %%mm1, (%%ecx, %1, 2)                        \n\t"
1560

    
1561
                : : "r" (src), "r" (stride)
1562
                : "%eax", "%ecx"
1563
        );
1564
#else
1565
        int x;
1566
        src+= 4*stride;
1567
        for(x=0; x<8; x++)
1568
        {
1569
                src[stride]   = (src[0]        + src[stride*2])>>1;
1570
                src[stride*3] = (src[stride*2] + src[stride*4])>>1;
1571
                src[stride*5] = (src[stride*4] + src[stride*6])>>1;
1572
                src[stride*7] = (src[stride*6] + src[stride*8])>>1;
1573
                src++;
1574
        }
1575
#endif
1576
}
1577

    
1578
/**
1579
 * Deinterlaces the given block by cubic interpolating every second line.
1580
 * will be called for every 8x8 block and can read & write from line 4-15
1581
 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
1582
 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1583
 * this filter will read lines 3-15 and write 7-13
1584
 * no cliping in C version
1585
 */
1586
static inline void RENAME(deInterlaceInterpolateCubic)(uint8_t src[], int stride)
1587
{
1588
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1589
        src+= stride*3;
1590
        asm volatile(
1591
                "leal (%0, %1), %%eax                                \n\t"
1592
                "leal (%%eax, %1, 4), %%edx                        \n\t"
1593
                "leal (%%edx, %1, 4), %%ecx                        \n\t"
1594
                "addl %1, %%ecx                                        \n\t"
1595
                "pxor %%mm7, %%mm7                                \n\t"
1596
//        0        1        2        3        4        5        6        7        8        9        10
1597
//        %0        eax        eax+%1        eax+2%1        %0+4%1        edx        edx+%1        edx+2%1        %0+8%1        edx+4%1 ecx
1598

    
1599
#define DEINT_CUBIC(a,b,c,d,e)\
1600
                "movq " #a ", %%mm0                                \n\t"\
1601
                "movq " #b ", %%mm1                                \n\t"\
1602
                "movq " #d ", %%mm2                                \n\t"\
1603
                "movq " #e ", %%mm3                                \n\t"\
1604
                PAVGB(%%mm2, %%mm1)                                        /* (b+d) /2 */\
1605
                PAVGB(%%mm3, %%mm0)                                        /* a(a+e) /2 */\
1606
                "movq %%mm0, %%mm2                                \n\t"\
1607
                "punpcklbw %%mm7, %%mm0                                \n\t"\
1608
                "punpckhbw %%mm7, %%mm2                                \n\t"\
1609
                "movq %%mm1, %%mm3                                \n\t"\
1610
                "punpcklbw %%mm7, %%mm1                                \n\t"\
1611
                "punpckhbw %%mm7, %%mm3                                \n\t"\
1612
                "psubw %%mm1, %%mm0                                \n\t"        /* L(a+e - (b+d))/2 */\
1613
                "psubw %%mm3, %%mm2                                \n\t"        /* H(a+e - (b+d))/2 */\
1614
                "psraw $3, %%mm0                                \n\t"        /* L(a+e - (b+d))/16 */\
1615
                "psraw $3, %%mm2                                \n\t"        /* H(a+e - (b+d))/16 */\
1616
                "psubw %%mm0, %%mm1                                \n\t"        /* L(9b + 9d - a - e)/16 */\
1617
                "psubw %%mm2, %%mm3                                \n\t"        /* H(9b + 9d - a - e)/16 */\
1618
                "packuswb %%mm3, %%mm1                                \n\t"\
1619
                "movq %%mm1, " #c "                                \n\t"
1620

    
1621
DEINT_CUBIC((%0), (%%eax, %1), (%%eax, %1, 2), (%0, %1, 4), (%%edx, %1))
1622
DEINT_CUBIC((%%eax, %1), (%0, %1, 4), (%%edx), (%%edx, %1), (%0, %1, 8))
1623
DEINT_CUBIC((%0, %1, 4), (%%edx, %1), (%%edx, %1, 2), (%0, %1, 8), (%%ecx))
1624
DEINT_CUBIC((%%edx, %1), (%0, %1, 8), (%%edx, %1, 4), (%%ecx), (%%ecx, %1, 2))
1625

    
1626
                : : "r" (src), "r" (stride)
1627
                : "%eax", "%edx", "ecx"
1628
        );
1629
#else
1630
        int x;
1631
        src+= stride*3;
1632
        for(x=0; x<8; x++)
1633
        {
1634
                src[stride*3] = (-src[0]        + 9*src[stride*2] + 9*src[stride*4] - src[stride*6])>>4;
1635
                src[stride*5] = (-src[stride*2] + 9*src[stride*4] + 9*src[stride*6] - src[stride*8])>>4;
1636
                src[stride*7] = (-src[stride*4] + 9*src[stride*6] + 9*src[stride*8] - src[stride*10])>>4;
1637
                src[stride*9] = (-src[stride*6] + 9*src[stride*8] + 9*src[stride*10] - src[stride*12])>>4;
1638
                src++;
1639
        }
1640
#endif
1641
}
1642

    
1643
/**
1644
 * Deinterlaces the given block by filtering every second line with a (-1 4 2 4 -1) filter.
1645
 * will be called for every 8x8 block and can read & write from line 4-15
1646
 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
1647
 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1648
 * this filter will read lines 4-13 and write 5-11
1649
 * no cliping in C version
1650
 */
1651
static inline void RENAME(deInterlaceFF)(uint8_t src[], int stride, uint8_t *tmp)
1652
{
1653
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1654
        src+= stride*4;
1655
        asm volatile(
1656
                "leal (%0, %1), %%eax                                \n\t"
1657
                "leal (%%eax, %1, 4), %%edx                        \n\t"
1658
                "pxor %%mm7, %%mm7                                \n\t"
1659
                "movq (%2), %%mm0                                \n\t"
1660
//        0        1        2        3        4        5        6        7        8        9        10
1661
//        %0        eax        eax+%1        eax+2%1        %0+4%1        edx        edx+%1        edx+2%1        %0+8%1        edx+4%1 ecx
1662

    
1663
#define DEINT_FF(a,b,c,d)\
1664
                "movq " #a ", %%mm1                                \n\t"\
1665
                "movq " #b ", %%mm2                                \n\t"\
1666
                "movq " #c ", %%mm3                                \n\t"\
1667
                "movq " #d ", %%mm4                                \n\t"\
1668
                PAVGB(%%mm3, %%mm1)                                        \
1669
                PAVGB(%%mm4, %%mm0)                                        \
1670
                "movq %%mm0, %%mm3                                \n\t"\
1671
                "punpcklbw %%mm7, %%mm0                                \n\t"\
1672
                "punpckhbw %%mm7, %%mm3                                \n\t"\
1673
                "movq %%mm1, %%mm4                                \n\t"\
1674
                "punpcklbw %%mm7, %%mm1                                \n\t"\
1675
                "punpckhbw %%mm7, %%mm4                                \n\t"\
1676
                "psllw $2, %%mm1                                \n\t"\
1677
                "psllw $2, %%mm4                                \n\t"\
1678
                "psubw %%mm0, %%mm1                                \n\t"\
1679
                "psubw %%mm3, %%mm4                                \n\t"\
1680
                "movq %%mm2, %%mm5                                \n\t"\
1681
                "movq %%mm2, %%mm0                                \n\t"\
1682
                "punpcklbw %%mm7, %%mm2                                \n\t"\
1683
                "punpckhbw %%mm7, %%mm5                                \n\t"\
1684
                "paddw %%mm2, %%mm1                                \n\t"\
1685
                "paddw %%mm5, %%mm4                                \n\t"\
1686
                "psraw $2, %%mm1                                \n\t"\
1687
                "psraw $2, %%mm4                                \n\t"\
1688
                "packuswb %%mm4, %%mm1                                \n\t"\
1689
                "movq %%mm1, " #b "                                \n\t"\
1690

    
1691
DEINT_FF((%0)       , (%%eax)       , (%%eax, %1), (%%eax, %1, 2))
1692
DEINT_FF((%%eax, %1), (%%eax, %1, 2), (%0, %1, 4), (%%edx)       )
1693
DEINT_FF((%0, %1, 4), (%%edx)       , (%%edx, %1), (%%edx, %1, 2))
1694
DEINT_FF((%%edx, %1), (%%edx, %1, 2), (%0, %1, 8), (%%edx, %1, 4))
1695

    
1696
                "movq %%mm0, (%2)                                \n\t"
1697
                : : "r" (src), "r" (stride), "r"(tmp)
1698
                : "%eax", "%edx"
1699
        );
1700
#else
1701
        int x;
1702
        src+= stride*4;
1703
        for(x=0; x<8; x++)
1704
        {
1705
                int t1= tmp[x];
1706
                int t2= src[stride*1];
1707

    
1708
                src[stride*1]= (-t1 + 4*src[stride*0] + 2*t2 + 4*src[stride*2] - src[stride*3] + 4)>>3;
1709
                t1= src[stride*4];
1710
                src[stride*3]= (-t2 + 4*src[stride*2] + 2*t1 + 4*src[stride*4] - src[stride*5] + 4)>>3;
1711
                t2= src[stride*6];
1712
                src[stride*5]= (-t1 + 4*src[stride*4] + 2*t2 + 4*src[stride*6] - src[stride*7] + 4)>>3;
1713
                t1= src[stride*8];
1714
                src[stride*7]= (-t2 + 4*src[stride*6] + 2*t1 + 4*src[stride*8] - src[stride*9] + 4)>>3;
1715
                tmp[x]= t1;
1716

    
1717
                src++;
1718
        }
1719
#endif
1720
}
1721

    
1722
/**
1723
 * Deinterlaces the given block by filtering all lines with a (1 2 1) filter.
1724
 * will be called for every 8x8 block and can read & write from line 4-15
1725
 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
1726
 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1727
 * will shift the image up by 1 line (FIXME if this is a problem)
1728
 * this filter will read lines 4-13 and write 4-11
1729
 */
1730
static inline void RENAME(deInterlaceBlendLinear)(uint8_t src[], int stride)
1731
{
1732
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1733
        src+= 4*stride;
1734
        asm volatile(
1735
                "leal (%0, %1), %%eax                                \n\t"
1736
                "leal (%%eax, %1, 4), %%edx                        \n\t"
1737
//        0        1        2        3        4        5        6        7        8        9
1738
//        %0        eax        eax+%1        eax+2%1        %0+4%1        edx        edx+%1        edx+2%1        %0+8%1        edx+4%1
1739

    
1740
                "movq (%0), %%mm0                                \n\t" // L0
1741
                "movq (%%eax, %1), %%mm1                        \n\t" // L2
1742
                PAVGB(%%mm1, %%mm0)                                      // L0+L2
1743
                "movq (%%eax), %%mm2                                \n\t" // L1
1744
                PAVGB(%%mm2, %%mm0)
1745
                "movq %%mm0, (%0)                                \n\t"
1746
                "movq (%%eax, %1, 2), %%mm0                        \n\t" // L3
1747
                PAVGB(%%mm0, %%mm2)                                      // L1+L3
1748
                PAVGB(%%mm1, %%mm2)                                      // 2L2 + L1 + L3
1749
                "movq %%mm2, (%%eax)                                \n\t"
1750
                "movq (%0, %1, 4), %%mm2                        \n\t" // L4
1751
                PAVGB(%%mm2, %%mm1)                                      // L2+L4
1752
                PAVGB(%%mm0, %%mm1)                                      // 2L3 + L2 + L4
1753
                "movq %%mm1, (%%eax, %1)                        \n\t"
1754
                "movq (%%edx), %%mm1                                \n\t" // L5
1755
                PAVGB(%%mm1, %%mm0)                                      // L3+L5
1756
                PAVGB(%%mm2, %%mm0)                                      // 2L4 + L3 + L5
1757
                "movq %%mm0, (%%eax, %1, 2)                        \n\t"
1758
                "movq (%%edx, %1), %%mm0                        \n\t" // L6
1759
                PAVGB(%%mm0, %%mm2)                                      // L4+L6
1760
                PAVGB(%%mm1, %%mm2)                                      // 2L5 + L4 + L6
1761
                "movq %%mm2, (%0, %1, 4)                        \n\t"
1762
                "movq (%%edx, %1, 2), %%mm2                        \n\t" // L7
1763
                PAVGB(%%mm2, %%mm1)                                      // L5+L7
1764
                PAVGB(%%mm0, %%mm1)                                      // 2L6 + L5 + L7
1765
                "movq %%mm1, (%%edx)                                \n\t"
1766
                "movq (%0, %1, 8), %%mm1                        \n\t" // L8
1767
                PAVGB(%%mm1, %%mm0)                                      // L6+L8
1768
                PAVGB(%%mm2, %%mm0)                                      // 2L7 + L6 + L8
1769
                "movq %%mm0, (%%edx, %1)                        \n\t"
1770
                "movq (%%edx, %1, 4), %%mm0                        \n\t" // L9
1771
                PAVGB(%%mm0, %%mm2)                                      // L7+L9
1772
                PAVGB(%%mm1, %%mm2)                                      // 2L8 + L7 + L9
1773
                "movq %%mm2, (%%edx, %1, 2)                        \n\t"
1774

    
1775

    
1776
                : : "r" (src), "r" (stride)
1777
                : "%eax", "%edx"
1778
        );
1779
#else
1780
        int x;
1781
        src+= 4*stride;
1782
        for(x=0; x<8; x++)
1783
        {
1784
                src[0       ] = (src[0       ] + 2*src[stride  ] + src[stride*2])>>2;
1785
                src[stride  ] = (src[stride  ] + 2*src[stride*2] + src[stride*3])>>2;
1786
                src[stride*2] = (src[stride*2] + 2*src[stride*3] + src[stride*4])>>2;
1787
                src[stride*3] = (src[stride*3] + 2*src[stride*4] + src[stride*5])>>2;
1788
                src[stride*4] = (src[stride*4] + 2*src[stride*5] + src[stride*6])>>2;
1789
                src[stride*5] = (src[stride*5] + 2*src[stride*6] + src[stride*7])>>2;
1790
                src[stride*6] = (src[stride*6] + 2*src[stride*7] + src[stride*8])>>2;
1791
                src[stride*7] = (src[stride*7] + 2*src[stride*8] + src[stride*9])>>2;
1792
                src++;
1793
        }
1794
#endif
1795
}
1796

    
1797
/**
1798
 * Deinterlaces the given block by applying a median filter to every second line.
1799
 * will be called for every 8x8 block and can read & write from line 4-15,
1800
 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
1801
 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1802
 */
1803
static inline void RENAME(deInterlaceMedian)(uint8_t src[], int stride)
1804
{
1805
#ifdef HAVE_MMX
1806
        src+= 4*stride;
1807
#ifdef HAVE_MMX2
1808
        asm volatile(
1809
                "leal (%0, %1), %%eax                                \n\t"
1810
                "leal (%%eax, %1, 4), %%edx                        \n\t"
1811
//        0        1        2        3        4        5        6        7        8        9
1812
//        %0        eax        eax+%1        eax+2%1        %0+4%1        edx        edx+%1        edx+2%1        %0+8%1        edx+4%1
1813

    
1814
                "movq (%0), %%mm0                                \n\t" //
1815
                "movq (%%eax, %1), %%mm2                        \n\t" //
1816
                "movq (%%eax), %%mm1                                \n\t" //
1817
                "movq %%mm0, %%mm3                                \n\t"
1818
                "pmaxub %%mm1, %%mm0                                \n\t" //
1819
                "pminub %%mm3, %%mm1                                \n\t" //
1820
                "pmaxub %%mm2, %%mm1                                \n\t" //
1821
                "pminub %%mm1, %%mm0                                \n\t"
1822
                "movq %%mm0, (%%eax)                                \n\t"
1823

    
1824
                "movq (%0, %1, 4), %%mm0                        \n\t" //
1825
                "movq (%%eax, %1, 2), %%mm1                        \n\t" //
1826
                "movq %%mm2, %%mm3                                \n\t"
1827
                "pmaxub %%mm1, %%mm2                                \n\t" //
1828
                "pminub %%mm3, %%mm1                                \n\t" //
1829
                "pmaxub %%mm0, %%mm1                                \n\t" //
1830
                "pminub %%mm1, %%mm2                                \n\t"
1831
                "movq %%mm2, (%%eax, %1, 2)                        \n\t"
1832

    
1833
                "movq (%%edx), %%mm2                                \n\t" //
1834
                "movq (%%edx, %1), %%mm1                        \n\t" //
1835
                "movq %%mm2, %%mm3                                \n\t"
1836
                "pmaxub %%mm0, %%mm2                                \n\t" //
1837
                "pminub %%mm3, %%mm0                                \n\t" //
1838
                "pmaxub %%mm1, %%mm0                                \n\t" //
1839
                "pminub %%mm0, %%mm2                                \n\t"
1840
                "movq %%mm2, (%%edx)                                \n\t"
1841

    
1842
                "movq (%%edx, %1, 2), %%mm2                        \n\t" //
1843
                "movq (%0, %1, 8), %%mm0                        \n\t" //
1844
                "movq %%mm2, %%mm3                                \n\t"
1845
                "pmaxub %%mm0, %%mm2                                \n\t" //
1846
                "pminub %%mm3, %%mm0                                \n\t" //
1847
                "pmaxub %%mm1, %%mm0                                \n\t" //
1848
                "pminub %%mm0, %%mm2                                \n\t"
1849
                "movq %%mm2, (%%edx, %1, 2)                        \n\t"
1850

    
1851

    
1852
                : : "r" (src), "r" (stride)
1853
                : "%eax", "%edx"
1854
        );
1855

    
1856
#else // MMX without MMX2
1857
        asm volatile(
1858
                "leal (%0, %1), %%eax                                \n\t"
1859
                "leal (%%eax, %1, 4), %%edx                        \n\t"
1860
//        0        1        2        3        4        5        6        7        8        9
1861
//        %0        eax        eax+%1        eax+2%1        %0+4%1        edx        edx+%1        edx+2%1        %0+8%1        edx+4%1
1862
                "pxor %%mm7, %%mm7                                \n\t"
1863

    
1864
#define MEDIAN(a,b,c)\
1865
                "movq " #a ", %%mm0                                \n\t"\
1866
                "movq " #b ", %%mm2                                \n\t"\
1867
                "movq " #c ", %%mm1                                \n\t"\
1868
                "movq %%mm0, %%mm3                                \n\t"\
1869
                "movq %%mm1, %%mm4                                \n\t"\
1870
                "movq %%mm2, %%mm5                                \n\t"\
1871
                "psubusb %%mm1, %%mm3                                \n\t"\
1872
                "psubusb %%mm2, %%mm4                                \n\t"\
1873
                "psubusb %%mm0, %%mm5                                \n\t"\
1874
                "pcmpeqb %%mm7, %%mm3                                \n\t"\
1875
                "pcmpeqb %%mm7, %%mm4                                \n\t"\
1876
                "pcmpeqb %%mm7, %%mm5                                \n\t"\
1877
                "movq %%mm3, %%mm6                                \n\t"\
1878
                "pxor %%mm4, %%mm3                                \n\t"\
1879
                "pxor %%mm5, %%mm4                                \n\t"\
1880
                "pxor %%mm6, %%mm5                                \n\t"\
1881
                "por %%mm3, %%mm1                                \n\t"\
1882
                "por %%mm4, %%mm2                                \n\t"\
1883
                "por %%mm5, %%mm0                                \n\t"\
1884
                "pand %%mm2, %%mm0                                \n\t"\
1885
                "pand %%mm1, %%mm0                                \n\t"\
1886
                "movq %%mm0, " #b "                                \n\t"
1887

    
1888
MEDIAN((%0), (%%eax), (%%eax, %1))
1889
MEDIAN((%%eax, %1), (%%eax, %1, 2), (%0, %1, 4))
1890
MEDIAN((%0, %1, 4), (%%edx), (%%edx, %1))
1891
MEDIAN((%%edx, %1), (%%edx, %1, 2), (%0, %1, 8))
1892

    
1893
                : : "r" (src), "r" (stride)
1894
                : "%eax", "%edx"
1895
        );
1896
#endif // MMX
1897
#else
1898
        int x, y;
1899
        src+= 4*stride;
1900
        // FIXME - there should be a way to do a few columns in parallel like w/mmx
1901
        for(x=0; x<8; x++)
1902
        {
1903
                uint8_t *colsrc = src;
1904
                for (y=0; y<4; y++)
1905
                {
1906
                        int a, b, c, d, e, f;
1907
                        a = colsrc[0       ];
1908
                        b = colsrc[stride  ];
1909
                        c = colsrc[stride*2];
1910
                        d = (a-b)>>31;
1911
                        e = (b-c)>>31;
1912
                        f = (c-a)>>31;
1913
                        colsrc[stride  ] = (a|(d^f)) & (b|(d^e)) & (c|(e^f));
1914
                        colsrc += stride*2;
1915
                }
1916
                src++;
1917
        }
1918
#endif
1919
}
1920

    
1921
#ifdef HAVE_MMX
1922
/**
1923
 * transposes and shift the given 8x8 Block into dst1 and dst2
1924
 */
1925
static inline void RENAME(transpose1)(uint8_t *dst1, uint8_t *dst2, uint8_t *src, int srcStride)
1926
{
1927
        asm(
1928
                "leal (%0, %1), %%eax                                \n\t"
1929
//        0        1        2        3        4        5        6        7        8        9
1930
//        %0        eax        eax+%1        eax+2%1        %0+4%1        edx        edx+%1        edx+2%1        %0+8%1        edx+4%1
1931
                "movq (%0), %%mm0                \n\t" // 12345678
1932
                "movq (%%eax), %%mm1                \n\t" // abcdefgh
1933
                "movq %%mm0, %%mm2                \n\t" // 12345678
1934
                "punpcklbw %%mm1, %%mm0                \n\t" // 1a2b3c4d
1935
                "punpckhbw %%mm1, %%mm2                \n\t" // 5e6f7g8h
1936

    
1937
                "movq (%%eax, %1), %%mm1        \n\t"
1938
                "movq (%%eax, %1, 2), %%mm3        \n\t"
1939
                "movq %%mm1, %%mm4                \n\t"
1940
                "punpcklbw %%mm3, %%mm1                \n\t"
1941
                "punpckhbw %%mm3, %%mm4                \n\t"
1942

    
1943
                "movq %%mm0, %%mm3                \n\t"
1944
                "punpcklwd %%mm1, %%mm0                \n\t"
1945
                "punpckhwd %%mm1, %%mm3                \n\t"
1946
                "movq %%mm2, %%mm1                \n\t"
1947
                "punpcklwd %%mm4, %%mm2                \n\t"
1948
                "punpckhwd %%mm4, %%mm1                \n\t"
1949

    
1950
                "movd %%mm0, 128(%2)                \n\t"
1951
                "psrlq $32, %%mm0                \n\t"
1952
                "movd %%mm0, 144(%2)                \n\t"
1953
                "movd %%mm3, 160(%2)                \n\t"
1954
                "psrlq $32, %%mm3                \n\t"
1955
                "movd %%mm3, 176(%2)                \n\t"
1956
                "movd %%mm3, 48(%3)                \n\t"
1957
                "movd %%mm2, 192(%2)                \n\t"
1958
                "movd %%mm2, 64(%3)                \n\t"
1959
                "psrlq $32, %%mm2                \n\t"
1960
                "movd %%mm2, 80(%3)                \n\t"
1961
                "movd %%mm1, 96(%3)                \n\t"
1962
                "psrlq $32, %%mm1                \n\t"
1963
                "movd %%mm1, 112(%3)                \n\t"
1964

    
1965
                "leal (%%eax, %1, 4), %%eax        \n\t"
1966
                
1967
                "movq (%0, %1, 4), %%mm0        \n\t" // 12345678
1968
                "movq (%%eax), %%mm1                \n\t" // abcdefgh
1969
                "movq %%mm0, %%mm2                \n\t" // 12345678
1970
                "punpcklbw %%mm1, %%mm0                \n\t" // 1a2b3c4d
1971
                "punpckhbw %%mm1, %%mm2                \n\t" // 5e6f7g8h
1972

    
1973
                "movq (%%eax, %1), %%mm1        \n\t"
1974
                "movq (%%eax, %1, 2), %%mm3        \n\t"
1975
                "movq %%mm1, %%mm4                \n\t"
1976
                "punpcklbw %%mm3, %%mm1                \n\t"
1977
                "punpckhbw %%mm3, %%mm4                \n\t"
1978

    
1979
                "movq %%mm0, %%mm3                \n\t"
1980
                "punpcklwd %%mm1, %%mm0                \n\t"
1981
                "punpckhwd %%mm1, %%mm3                \n\t"
1982
                "movq %%mm2, %%mm1                \n\t"
1983
                "punpcklwd %%mm4, %%mm2                \n\t"
1984
                "punpckhwd %%mm4, %%mm1                \n\t"
1985

    
1986
                "movd %%mm0, 132(%2)                \n\t"
1987
                "psrlq $32, %%mm0                \n\t"
1988
                "movd %%mm0, 148(%2)                \n\t"
1989
                "movd %%mm3, 164(%2)                \n\t"
1990
                "psrlq $32, %%mm3                \n\t"
1991
                "movd %%mm3, 180(%2)                \n\t"
1992
                "movd %%mm3, 52(%3)                \n\t"
1993
                "movd %%mm2, 196(%2)                \n\t"
1994
                "movd %%mm2, 68(%3)                \n\t"
1995
                "psrlq $32, %%mm2                \n\t"
1996
                "movd %%mm2, 84(%3)                \n\t"
1997
                "movd %%mm1, 100(%3)                \n\t"
1998
                "psrlq $32, %%mm1                \n\t"
1999
                "movd %%mm1, 116(%3)                \n\t"
2000

    
2001

    
2002
        :: "r" (src), "r" (srcStride), "r" (dst1), "r" (dst2)
2003
        : "%eax"
2004
        );
2005
}
2006

    
2007
/**
2008
 * transposes the given 8x8 block
2009
 */
2010
static inline void RENAME(transpose2)(uint8_t *dst, int dstStride, uint8_t *src)
2011
{
2012
        asm(
2013
                "leal (%0, %1), %%eax                                \n\t"
2014
                "leal (%%eax, %1, 4), %%edx                        \n\t"
2015
//        0        1        2        3        4        5        6        7        8        9
2016
//        %0        eax        eax+%1        eax+2%1        %0+4%1        edx        edx+%1        edx+2%1        %0+8%1        edx+4%1
2017
                "movq (%2), %%mm0                \n\t" // 12345678
2018
                "movq 16(%2), %%mm1                \n\t" // abcdefgh
2019
                "movq %%mm0, %%mm2                \n\t" // 12345678
2020
                "punpcklbw %%mm1, %%mm0                \n\t" // 1a2b3c4d
2021
                "punpckhbw %%mm1, %%mm2                \n\t" // 5e6f7g8h
2022

    
2023
                "movq 32(%2), %%mm1                \n\t"
2024
                "movq 48(%2), %%mm3                \n\t"
2025
                "movq %%mm1, %%mm4                \n\t"
2026
                "punpcklbw %%mm3, %%mm1                \n\t"
2027
                "punpckhbw %%mm3, %%mm4                \n\t"
2028

    
2029
                "movq %%mm0, %%mm3                \n\t"
2030
                "punpcklwd %%mm1, %%mm0                \n\t"
2031
                "punpckhwd %%mm1, %%mm3                \n\t"
2032
                "movq %%mm2, %%mm1                \n\t"
2033
                "punpcklwd %%mm4, %%mm2                \n\t"
2034
                "punpckhwd %%mm4, %%mm1                \n\t"
2035

    
2036
                "movd %%mm0, (%0)                \n\t"
2037
                "psrlq $32, %%mm0                \n\t"
2038
                "movd %%mm0, (%%eax)                \n\t"
2039
                "movd %%mm3, (%%eax, %1)        \n\t"
2040
                "psrlq $32, %%mm3                \n\t"
2041
                "movd %%mm3, (%%eax, %1, 2)        \n\t"
2042
                "movd %%mm2, (%0, %1, 4)        \n\t"
2043
                "psrlq $32, %%mm2                \n\t"
2044
                "movd %%mm2, (%%edx)                \n\t"
2045
                "movd %%mm1, (%%edx, %1)        \n\t"
2046
                "psrlq $32, %%mm1                \n\t"
2047
                "movd %%mm1, (%%edx, %1, 2)        \n\t"
2048

    
2049

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

    
2056
                "movq 96(%2), %%mm1                \n\t"
2057
                "movq 112(%2), %%mm3                \n\t"
2058
                "movq %%mm1, %%mm4                \n\t"
2059
                "punpcklbw %%mm3, %%mm1                \n\t"
2060
                "punpckhbw %%mm3, %%mm4                \n\t"
2061

    
2062
                "movq %%mm0, %%mm3                \n\t"
2063
                "punpcklwd %%mm1, %%mm0                \n\t"
2064
                "punpckhwd %%mm1, %%mm3                \n\t"
2065
                "movq %%mm2, %%mm1                \n\t"
2066
                "punpcklwd %%mm4, %%mm2                \n\t"
2067
                "punpckhwd %%mm4, %%mm1                \n\t"
2068

    
2069
                "movd %%mm0, 4(%0)                \n\t"
2070
                "psrlq $32, %%mm0                \n\t"
2071
                "movd %%mm0, 4(%%eax)                \n\t"
2072
                "movd %%mm3, 4(%%eax, %1)        \n\t"
2073
                "psrlq $32, %%mm3                \n\t"
2074
                "movd %%mm3, 4(%%eax, %1, 2)        \n\t"
2075
                "movd %%mm2, 4(%0, %1, 4)        \n\t"
2076
                "psrlq $32, %%mm2                \n\t"
2077
                "movd %%mm2, 4(%%edx)                \n\t"
2078
                "movd %%mm1, 4(%%edx, %1)        \n\t"
2079
                "psrlq $32, %%mm1                \n\t"
2080
                "movd %%mm1, 4(%%edx, %1, 2)        \n\t"
2081

    
2082
        :: "r" (dst), "r" (dstStride), "r" (src)
2083
        : "%eax", "%edx"
2084
        );
2085
}
2086
#endif
2087
//static int test=0;
2088

    
2089
static inline void RENAME(tempNoiseReducer)(uint8_t *src, int stride,
2090
                                    uint8_t *tempBlured, uint32_t *tempBluredPast, int *maxNoise)
2091
{
2092
        // to save a register (FIXME do this outside of the loops)
2093
        tempBluredPast[127]= maxNoise[0];
2094
        tempBluredPast[128]= maxNoise[1];
2095
        tempBluredPast[129]= maxNoise[2];
2096
        
2097
#define FAST_L2_DIFF
2098
//#define L1_DIFF //u should change the thresholds too if u try that one
2099
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
2100
        asm volatile(
2101
                "leal (%2, %2, 2), %%eax                        \n\t" // 3*stride
2102
                "leal (%2, %2, 4), %%edx                        \n\t" // 5*stride
2103
                "leal (%%edx, %2, 2), %%ecx                        \n\t" // 7*stride
2104
//        0        1        2        3        4        5        6        7        8        9
2105
//        %x        %x+%2        %x+2%2        %x+eax        %x+4%2        %x+edx        %x+2eax        %x+ecx        %x+8%2
2106
//FIXME reorder?
2107
#ifdef L1_DIFF //needs mmx2
2108
                "movq (%0), %%mm0                                \n\t" // L0
2109
                "psadbw (%1), %%mm0                                \n\t" // |L0-R0|
2110
                "movq (%0, %2), %%mm1                                \n\t" // L1
2111
                "psadbw (%1, %2), %%mm1                                \n\t" // |L1-R1|
2112
                "movq (%0, %2, 2), %%mm2                        \n\t" // L2
2113
                "psadbw (%1, %2, 2), %%mm2                        \n\t" // |L2-R2|
2114
                "movq (%0, %%eax), %%mm3                        \n\t" // L3
2115
                "psadbw (%1, %%eax), %%mm3                        \n\t" // |L3-R3|
2116

    
2117
                "movq (%0, %2, 4), %%mm4                        \n\t" // L4
2118
                "paddw %%mm1, %%mm0                                \n\t"
2119
                "psadbw (%1, %2, 4), %%mm4                        \n\t" // |L4-R4|
2120
                "movq (%0, %%edx), %%mm5                        \n\t" // L5
2121
                "paddw %%mm2, %%mm0                                \n\t"
2122
                "psadbw (%1, %%edx), %%mm5                        \n\t" // |L5-R5|
2123
                "movq (%0, %%eax, 2), %%mm6                        \n\t" // L6
2124
                "paddw %%mm3, %%mm0                                \n\t"
2125
                "psadbw (%1, %%eax, 2), %%mm6                        \n\t" // |L6-R6|
2126
                "movq (%0, %%ecx), %%mm7                        \n\t" // L7
2127
                "paddw %%mm4, %%mm0                                \n\t"
2128
                "psadbw (%1, %%ecx), %%mm7                        \n\t" // |L7-R7|
2129
                "paddw %%mm5, %%mm6                                \n\t"
2130
                "paddw %%mm7, %%mm6                                \n\t"
2131
                "paddw %%mm6, %%mm0                                \n\t"
2132
#elif defined (FAST_L2_DIFF)
2133
                "pcmpeqb %%mm7, %%mm7                                \n\t"
2134
                "movq "MANGLE(b80)", %%mm6                        \n\t"
2135
                "pxor %%mm0, %%mm0                                \n\t"
2136
#define L2_DIFF_CORE(a, b)\
2137
                "movq " #a ", %%mm5                                \n\t"\
2138
                "movq " #b ", %%mm2                                \n\t"\
2139
                "pxor %%mm7, %%mm2                                \n\t"\
2140
                PAVGB(%%mm2, %%mm5)\
2141
                "paddb %%mm6, %%mm5                                \n\t"\
2142
                "movq %%mm5, %%mm2                                \n\t"\
2143
                "psllw $8, %%mm5                                \n\t"\
2144
                "pmaddwd %%mm5, %%mm5                                \n\t"\
2145
                "pmaddwd %%mm2, %%mm2                                \n\t"\
2146
                "paddd %%mm2, %%mm5                                \n\t"\
2147
                "psrld $14, %%mm5                                \n\t"\
2148
                "paddd %%mm5, %%mm0                                \n\t"
2149

    
2150
L2_DIFF_CORE((%0), (%1))
2151
L2_DIFF_CORE((%0, %2), (%1, %2))
2152
L2_DIFF_CORE((%0, %2, 2), (%1, %2, 2))
2153
L2_DIFF_CORE((%0, %%eax), (%1, %%eax))
2154
L2_DIFF_CORE((%0, %2, 4), (%1, %2, 4))
2155
L2_DIFF_CORE((%0, %%edx), (%1, %%edx))
2156
L2_DIFF_CORE((%0, %%eax,2), (%1, %%eax,2))
2157
L2_DIFF_CORE((%0, %%ecx), (%1, %%ecx))
2158

    
2159
#else
2160
                "pxor %%mm7, %%mm7                                \n\t"
2161
                "pxor %%mm0, %%mm0                                \n\t"
2162
#define L2_DIFF_CORE(a, b)\
2163
                "movq " #a ", %%mm5                                \n\t"\
2164
                "movq " #b ", %%mm2                                \n\t"\
2165
                "movq %%mm5, %%mm1                                \n\t"\
2166
                "movq %%mm2, %%mm3                                \n\t"\
2167
                "punpcklbw %%mm7, %%mm5                                \n\t"\
2168
                "punpckhbw %%mm7, %%mm1                                \n\t"\
2169
                "punpcklbw %%mm7, %%mm2                                \n\t"\
2170
                "punpckhbw %%mm7, %%mm3                                \n\t"\
2171
                "psubw %%mm2, %%mm5                                \n\t"\
2172
                "psubw %%mm3, %%mm1                                \n\t"\
2173
                "pmaddwd %%mm5, %%mm5                                \n\t"\
2174
                "pmaddwd %%mm1, %%mm1                                \n\t"\
2175
                "paddd %%mm1, %%mm5                                \n\t"\
2176
                "paddd %%mm5, %%mm0                                \n\t"
2177

    
2178
L2_DIFF_CORE((%0), (%1))
2179
L2_DIFF_CORE((%0, %2), (%1, %2))
2180
L2_DIFF_CORE((%0, %2, 2), (%1, %2, 2))
2181
L2_DIFF_CORE((%0, %%eax), (%1, %%eax))
2182
L2_DIFF_CORE((%0, %2, 4), (%1, %2, 4))
2183
L2_DIFF_CORE((%0, %%edx), (%1, %%edx))
2184
L2_DIFF_CORE((%0, %%eax,2), (%1, %%eax,2))
2185
L2_DIFF_CORE((%0, %%ecx), (%1, %%ecx))
2186

    
2187
#endif
2188

    
2189
                "movq %%mm0, %%mm4                                \n\t"
2190
                "psrlq $32, %%mm0                                \n\t"
2191
                "paddd %%mm0, %%mm4                                \n\t"
2192
                "movd %%mm4, %%ecx                                \n\t"
2193
                "shll $2, %%ecx                                        \n\t"
2194
                "movl %3, %%edx                                        \n\t"
2195
                "addl -4(%%edx), %%ecx                                \n\t"
2196
                "addl 4(%%edx), %%ecx                                \n\t"
2197
                "addl -1024(%%edx), %%ecx                        \n\t"
2198
                "addl $4, %%ecx                                        \n\t"
2199
                "addl 1024(%%edx), %%ecx                        \n\t"
2200
                "shrl $3, %%ecx                                        \n\t"
2201
                "movl %%ecx, (%%edx)                                \n\t"
2202

    
2203
//                "movl %3, %%ecx                                        \n\t"
2204
//                "movl %%ecx, test                                \n\t"
2205
//                "jmp 4f \n\t"
2206
                "cmpl 512(%%edx), %%ecx                                \n\t"
2207
                " jb 2f                                                \n\t"
2208
                "cmpl 516(%%edx), %%ecx                                \n\t"
2209
                " jb 1f                                                \n\t"
2210

    
2211
                "leal (%%eax, %2, 2), %%edx                        \n\t" // 5*stride
2212
                "leal (%%edx, %2, 2), %%ecx                        \n\t" // 7*stride
2213
                "movq (%0), %%mm0                                \n\t" // L0
2214
                "movq (%0, %2), %%mm1                                \n\t" // L1
2215
                "movq (%0, %2, 2), %%mm2                        \n\t" // L2
2216
                "movq (%0, %%eax), %%mm3                        \n\t" // L3
2217
                "movq (%0, %2, 4), %%mm4                        \n\t" // L4
2218
                "movq (%0, %%edx), %%mm5                        \n\t" // L5
2219
                "movq (%0, %%eax, 2), %%mm6                        \n\t" // L6
2220
                "movq (%0, %%ecx), %%mm7                        \n\t" // L7
2221
                "movq %%mm0, (%1)                                \n\t" // L0
2222
                "movq %%mm1, (%1, %2)                                \n\t" // L1
2223
                "movq %%mm2, (%1, %2, 2)                        \n\t" // L2
2224
                "movq %%mm3, (%1, %%eax)                        \n\t" // L3
2225
                "movq %%mm4, (%1, %2, 4)                        \n\t" // L4
2226
                "movq %%mm5, (%1, %%edx)                        \n\t" // L5
2227
                "movq %%mm6, (%1, %%eax, 2)                        \n\t" // L6
2228
                "movq %%mm7, (%1, %%ecx)                        \n\t" // L7
2229
                "jmp 4f                                                \n\t"
2230

    
2231
                "1:                                                \n\t"
2232
                "leal (%%eax, %2, 2), %%edx                        \n\t" // 5*stride
2233
                "leal (%%edx, %2, 2), %%ecx                        \n\t" // 7*stride
2234
                "movq (%0), %%mm0                                \n\t" // L0
2235
                PAVGB((%1), %%mm0)                                      // L0
2236
                "movq (%0, %2), %%mm1                                \n\t" // L1
2237
                PAVGB((%1, %2), %%mm1)                                      // L1
2238
                "movq (%0, %2, 2), %%mm2                        \n\t" // L2
2239
                PAVGB((%1, %2, 2), %%mm2)                              // L2
2240
                "movq (%0, %%eax), %%mm3                        \n\t" // L3
2241
                PAVGB((%1, %%eax), %%mm3)                              // L3
2242
                "movq (%0, %2, 4), %%mm4                        \n\t" // L4
2243
                PAVGB((%1, %2, 4), %%mm4)                              // L4
2244
                "movq (%0, %%edx), %%mm5                        \n\t" // L5
2245
                PAVGB((%1, %%edx), %%mm5)                              // L5
2246
                "movq (%0, %%eax, 2), %%mm6                        \n\t" // L6
2247
                PAVGB((%1, %%eax, 2), %%mm6)                              // L6
2248
                "movq (%0, %%ecx), %%mm7                        \n\t" // L7
2249
                PAVGB((%1, %%ecx), %%mm7)                              // L7
2250
                "movq %%mm0, (%1)                                \n\t" // R0
2251
                "movq %%mm1, (%1, %2)                                \n\t" // R1
2252
                "movq %%mm2, (%1, %2, 2)                        \n\t" // R2
2253
                "movq %%mm3, (%1, %%eax)                        \n\t" // R3
2254
                "movq %%mm4, (%1, %2, 4)                        \n\t" // R4
2255
                "movq %%mm5, (%1, %%edx)                        \n\t" // R5
2256
                "movq %%mm6, (%1, %%eax, 2)                        \n\t" // R6
2257
                "movq %%mm7, (%1, %%ecx)                        \n\t" // R7
2258
                "movq %%mm0, (%0)                                \n\t" // L0
2259
                "movq %%mm1, (%0, %2)                                \n\t" // L1
2260
                "movq %%mm2, (%0, %2, 2)                        \n\t" // L2
2261
                "movq %%mm3, (%0, %%eax)                        \n\t" // L3
2262
                "movq %%mm4, (%0, %2, 4)                        \n\t" // L4
2263
                "movq %%mm5, (%0, %%edx)                        \n\t" // L5
2264
                "movq %%mm6, (%0, %%eax, 2)                        \n\t" // L6
2265
                "movq %%mm7, (%0, %%ecx)                        \n\t" // L7
2266
                "jmp 4f                                                \n\t"
2267

    
2268
                "2:                                                \n\t"
2269
                "cmpl 508(%%edx), %%ecx                                \n\t"
2270
                " jb 3f                                                \n\t"
2271

    
2272
                "leal (%%eax, %2, 2), %%edx                        \n\t" // 5*stride
2273
                "leal (%%edx, %2, 2), %%ecx                        \n\t" // 7*stride
2274
                "movq (%0), %%mm0                                \n\t" // L0
2275
                "movq (%0, %2), %%mm1                                \n\t" // L1
2276
                "movq (%0, %2, 2), %%mm2                        \n\t" // L2
2277
                "movq (%0, %%eax), %%mm3                        \n\t" // L3
2278
                "movq (%1), %%mm4                                \n\t" // R0
2279
                "movq (%1, %2), %%mm5                                \n\t" // R1
2280
                "movq (%1, %2, 2), %%mm6                        \n\t" // R2
2281
                "movq (%1, %%eax), %%mm7                        \n\t" // R3
2282
                PAVGB(%%mm4, %%mm0)
2283
                PAVGB(%%mm5, %%mm1)
2284
                PAVGB(%%mm6, %%mm2)
2285
                PAVGB(%%mm7, %%mm3)
2286
                PAVGB(%%mm4, %%mm0)
2287
                PAVGB(%%mm5, %%mm1)
2288
                PAVGB(%%mm6, %%mm2)
2289
                PAVGB(%%mm7, %%mm3)
2290
                "movq %%mm0, (%1)                                \n\t" // R0
2291
                "movq %%mm1, (%1, %2)                                \n\t" // R1
2292
                "movq %%mm2, (%1, %2, 2)                        \n\t" // R2
2293
                "movq %%mm3, (%1, %%eax)                        \n\t" // R3
2294
                "movq %%mm0, (%0)                                \n\t" // L0
2295
                "movq %%mm1, (%0, %2)                                \n\t" // L1
2296
                "movq %%mm2, (%0, %2, 2)                        \n\t" // L2
2297
                "movq %%mm3, (%0, %%eax)                        \n\t" // L3
2298

    
2299
                "movq (%0, %2, 4), %%mm0                        \n\t" // L4
2300
                "movq (%0, %%edx), %%mm1                        \n\t" // L5
2301
                "movq (%0, %%eax, 2), %%mm2                        \n\t" // L6
2302
                "movq (%0, %%ecx), %%mm3                        \n\t" // L7
2303
                "movq (%1, %2, 4), %%mm4                        \n\t" // R4
2304
                "movq (%1, %%edx), %%mm5                        \n\t" // R5
2305
                "movq (%1, %%eax, 2), %%mm6                        \n\t" // R6
2306
                "movq (%1, %%ecx), %%mm7                        \n\t" // R7
2307
                PAVGB(%%mm4, %%mm0)
2308
                PAVGB(%%mm5, %%mm1)
2309
                PAVGB(%%mm6, %%mm2)
2310
                PAVGB(%%mm7, %%mm3)
2311
                PAVGB(%%mm4, %%mm0)
2312
                PAVGB(%%mm5, %%mm1)
2313
                PAVGB(%%mm6, %%mm2)
2314
                PAVGB(%%mm7, %%mm3)
2315
                "movq %%mm0, (%1, %2, 4)                        \n\t" // R4
2316
                "movq %%mm1, (%1, %%edx)                        \n\t" // R5
2317
                "movq %%mm2, (%1, %%eax, 2)                        \n\t" // R6
2318
                "movq %%mm3, (%1, %%ecx)                        \n\t" // R7
2319
                "movq %%mm0, (%0, %2, 4)                        \n\t" // L4
2320
                "movq %%mm1, (%0, %%edx)                        \n\t" // L5
2321
                "movq %%mm2, (%0, %%eax, 2)                        \n\t" // L6
2322
                "movq %%mm3, (%0, %%ecx)                        \n\t" // L7
2323
                "jmp 4f                                                \n\t"
2324

    
2325
                "3:                                                \n\t"
2326
                "leal (%%eax, %2, 2), %%edx                        \n\t" // 5*stride
2327
                "leal (%%edx, %2, 2), %%ecx                        \n\t" // 7*stride
2328
                "movq (%0), %%mm0                                \n\t" // L0
2329
                "movq (%0, %2), %%mm1                                \n\t" // L1
2330
                "movq (%0, %2, 2), %%mm2                        \n\t" // L2
2331
                "movq (%0, %%eax), %%mm3                        \n\t" // L3
2332
                "movq (%1), %%mm4                                \n\t" // R0
2333
                "movq (%1, %2), %%mm5                                \n\t" // R1
2334
                "movq (%1, %2, 2), %%mm6                        \n\t" // R2
2335
                "movq (%1, %%eax), %%mm7                        \n\t" // R3
2336
                PAVGB(%%mm4, %%mm0)
2337
                PAVGB(%%mm5, %%mm1)
2338
                PAVGB(%%mm6, %%mm2)
2339
                PAVGB(%%mm7, %%mm3)
2340
                PAVGB(%%mm4, %%mm0)
2341
                PAVGB(%%mm5, %%mm1)
2342
                PAVGB(%%mm6, %%mm2)
2343
                PAVGB(%%mm7, %%mm3)
2344
                PAVGB(%%mm4, %%mm0)
2345
                PAVGB(%%mm5, %%mm1)
2346
                PAVGB(%%mm6, %%mm2)
2347
                PAVGB(%%mm7, %%mm3)
2348
                "movq %%mm0, (%1)                                \n\t" // R0
2349
                "movq %%mm1, (%1, %2)                                \n\t" // R1
2350
                "movq %%mm2, (%1, %2, 2)                        \n\t" // R2
2351
                "movq %%mm3, (%1, %%eax)                        \n\t" // R3
2352
                "movq %%mm0, (%0)                                \n\t" // L0
2353
                "movq %%mm1, (%0, %2)                                \n\t" // L1
2354
                "movq %%mm2, (%0, %2, 2)                        \n\t" // L2
2355
                "movq %%mm3, (%0, %%eax)                        \n\t" // L3
2356

    
2357
                "movq (%0, %2, 4), %%mm0                        \n\t" // L4
2358
                "movq (%0, %%edx), %%mm1                        \n\t" // L5
2359
                "movq (%0, %%eax, 2), %%mm2                        \n\t" // L6
2360
                "movq (%0, %%ecx), %%mm3                        \n\t" // L7
2361
                "movq (%1, %2, 4), %%mm4                        \n\t" // R4
2362
                "movq (%1, %%edx), %%mm5                        \n\t" // R5
2363
                "movq (%1, %%eax, 2), %%mm6                        \n\t" // R6
2364
                "movq (%1, %%ecx), %%mm7                        \n\t" // R7
2365
                PAVGB(%%mm4, %%mm0)
2366
                PAVGB(%%mm5, %%mm1)
2367
                PAVGB(%%mm6, %%mm2)
2368
                PAVGB(%%mm7, %%mm3)
2369
                PAVGB(%%mm4, %%mm0)
2370
                PAVGB(%%mm5, %%mm1)
2371
                PAVGB(%%mm6, %%mm2)
2372
                PAVGB(%%mm7, %%mm3)
2373
                PAVGB(%%mm4, %%mm0)
2374
                PAVGB(%%mm5, %%mm1)
2375
                PAVGB(%%mm6, %%mm2)
2376
                PAVGB(%%mm7, %%mm3)
2377
                "movq %%mm0, (%1, %2, 4)                        \n\t" // R4
2378
                "movq %%mm1, (%1, %%edx)                        \n\t" // R5
2379
                "movq %%mm2, (%1, %%eax, 2)                        \n\t" // R6
2380
                "movq %%mm3, (%1, %%ecx)                        \n\t" // R7
2381
                "movq %%mm0, (%0, %2, 4)                        \n\t" // L4
2382
                "movq %%mm1, (%0, %%edx)                        \n\t" // L5
2383
                "movq %%mm2, (%0, %%eax, 2)                        \n\t" // L6
2384
                "movq %%mm3, (%0, %%ecx)                        \n\t" // L7
2385

    
2386
                "4:                                                \n\t"
2387

    
2388
                :: "r" (src), "r" (tempBlured), "r"(stride), "m" (tempBluredPast)
2389
                : "%eax", "%edx", "%ecx", "memory"
2390
                );
2391
//printf("%d\n", test);
2392
#else
2393
{
2394
        int y;
2395
        int d=0;
2396
        int sysd=0;
2397
        int i;
2398

    
2399
        for(y=0; y<8; y++)
2400
        {
2401
                int x;
2402
                for(x=0; x<8; x++)
2403
                {
2404
                        int ref= tempBlured[ x + y*stride ];
2405
                        int cur= src[ x + y*stride ];
2406
                        int d1=ref - cur;
2407
//                        if(x==0 || x==7) d1+= d1>>1;
2408
//                        if(y==0 || y==7) d1+= d1>>1;
2409
//                        d+= ABS(d1);
2410
                        d+= d1*d1;
2411
                        sysd+= d1;
2412
                }
2413
        }
2414
        i=d;
2415
        d=         (
2416
                4*d
2417
                +(*(tempBluredPast-256))
2418
                +(*(tempBluredPast-1))+ (*(tempBluredPast+1))
2419
                +(*(tempBluredPast+256))
2420
                +4)>>3;
2421
        *tempBluredPast=i;
2422
//        ((*tempBluredPast)*3 + d + 2)>>2;
2423

    
2424
//printf("%d %d %d\n", maxNoise[0], maxNoise[1], maxNoise[2]);
2425
/*
2426
Switch between
2427
 1  0  0  0  0  0  0  (0)
2428
64 32 16  8  4  2  1  (1)
2429
64 48 36 27 20 15 11 (33) (approx)
2430
64 56 49 43 37 33 29 (200) (approx)
2431
*/
2432
        if(d > maxNoise[1])
2433
        {
2434
                if(d < maxNoise[2])
2435
                {
2436
                        for(y=0; y<8; y++)
2437
                        {
2438
                                int x;
2439
                                for(x=0; x<8; x++)
2440
                                {
2441
                                        int ref= tempBlured[ x + y*stride ];
2442
                                        int cur= src[ x + y*stride ];
2443
                                        tempBlured[ x + y*stride ]=
2444
                                        src[ x + y*stride ]=
2445
                                                (ref + cur + 1)>>1;
2446
                                }
2447
                        }
2448
                }
2449
                else
2450
                {
2451
                        for(y=0; y<8; y++)
2452
                        {
2453
                                int x;
2454
                                for(x=0; x<8; x++)
2455
                                {
2456
                                        tempBlured[ x + y*stride ]= src[ x + y*stride ];
2457
                                }
2458
                        }
2459
                }
2460
        }
2461
        else
2462
        {
2463
                if(d < maxNoise[0])
2464
                {
2465
                        for(y=0; y<8; y++)
2466
                        {
2467
                                int x;
2468
                                for(x=0; x<8; x++)
2469
                                {
2470
                                        int ref= tempBlured[ x + y*stride ];
2471
                                        int cur= src[ x + y*stride ];
2472
                                        tempBlured[ x + y*stride ]=
2473
                                        src[ x + y*stride ]=
2474
                                                (ref*7 + cur + 4)>>3;
2475
                                }
2476
                        }
2477
                }
2478
                else
2479
                {
2480
                        for(y=0; y<8; y++)
2481
                        {
2482
                                int x;
2483
                                for(x=0; x<8; x++)
2484
                                {
2485
                                        int ref= tempBlured[ x + y*stride ];
2486
                                        int cur= src[ x + y*stride ];
2487
                                        tempBlured[ x + y*stride ]=
2488
                                        src[ x + y*stride ]=
2489
                                                (ref*3 + cur + 2)>>2;
2490
                                }
2491
                        }
2492
                }
2493
        }
2494
}
2495
#endif
2496
}
2497

    
2498
static void RENAME(postProcess)(uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
2499
        QP_STORE_T QPs[], int QPStride, int isColor, PPContext *c);
2500

    
2501
/**
2502
 * Copies a block from src to dst and fixes the blacklevel
2503
 * levelFix == 0 -> dont touch the brighness & contrast
2504
 */
2505
#undef SCALED_CPY
2506

    
2507
static inline void RENAME(blockCopy)(uint8_t dst[], int dstStride, uint8_t src[], int srcStride,
2508
        int levelFix, int64_t *packedOffsetAndScale)
2509
{
2510
#ifndef HAVE_MMX
2511
        int i;
2512
#endif
2513
        if(levelFix)
2514
        {
2515
#ifdef HAVE_MMX
2516
                                        asm volatile(
2517
                                                "movq (%%eax), %%mm2        \n\t" // packedYOffset
2518
                                                "movq 8(%%eax), %%mm3        \n\t" // packedYScale
2519
                                                "leal (%2,%4), %%eax        \n\t"
2520
                                                "leal (%3,%5), %%edx        \n\t"
2521
                                                "pxor %%mm4, %%mm4        \n\t"
2522
#ifdef HAVE_MMX2
2523
#define SCALED_CPY(src1, src2, dst1, dst2)                                        \
2524
                                                "movq " #src1 ", %%mm0        \n\t"\
2525
                                                "movq " #src1 ", %%mm5        \n\t"\
2526
                                                "movq " #src2 ", %%mm1        \n\t"\
2527
                                                "movq " #src2 ", %%mm6        \n\t"\
2528
                                                "punpcklbw %%mm0, %%mm0 \n\t"\
2529
                                                "punpckhbw %%mm5, %%mm5 \n\t"\
2530
                                                "punpcklbw %%mm1, %%mm1 \n\t"\
2531
                                                "punpckhbw %%mm6, %%mm6 \n\t"\
2532
                                                "pmulhuw %%mm3, %%mm0        \n\t"\
2533
                                                "pmulhuw %%mm3, %%mm5        \n\t"\
2534
                                                "pmulhuw %%mm3, %%mm1        \n\t"\
2535
                                                "pmulhuw %%mm3, %%mm6        \n\t"\
2536
                                                "psubw %%mm2, %%mm0        \n\t"\
2537
                                                "psubw %%mm2, %%mm5        \n\t"\
2538
                                                "psubw %%mm2, %%mm1        \n\t"\
2539
                                                "psubw %%mm2, %%mm6        \n\t"\
2540
                                                "packuswb %%mm5, %%mm0        \n\t"\
2541
                                                "packuswb %%mm6, %%mm1        \n\t"\
2542
                                                "movq %%mm0, " #dst1 "        \n\t"\
2543
                                                "movq %%mm1, " #dst2 "        \n\t"\
2544

    
2545
#else //HAVE_MMX2
2546
#define SCALED_CPY(src1, src2, dst1, dst2)                                        \
2547
                                                "movq " #src1 ", %%mm0        \n\t"\
2548
                                                "movq " #src1 ", %%mm5        \n\t"\
2549
                                                "punpcklbw %%mm4, %%mm0 \n\t"\
2550
                                                "punpckhbw %%mm4, %%mm5 \n\t"\
2551
                                                "psubw %%mm2, %%mm0        \n\t"\
2552
                                                "psubw %%mm2, %%mm5        \n\t"\
2553
                                                "movq " #src2 ", %%mm1        \n\t"\
2554
                                                "psllw $6, %%mm0        \n\t"\
2555
                                                "psllw $6, %%mm5        \n\t"\
2556
                                                "pmulhw %%mm3, %%mm0        \n\t"\
2557
                                                "movq " #src2 ", %%mm6        \n\t"\
2558
                                                "pmulhw %%mm3, %%mm5        \n\t"\
2559
                                                "punpcklbw %%mm4, %%mm1 \n\t"\
2560
                                                "punpckhbw %%mm4, %%mm6 \n\t"\
2561
                                                "psubw %%mm2, %%mm1        \n\t"\
2562
                                                "psubw %%mm2, %%mm6        \n\t"\
2563
                                                "psllw $6, %%mm1        \n\t"\
2564
                                                "psllw $6, %%mm6        \n\t"\
2565
                                                "pmulhw %%mm3, %%mm1        \n\t"\
2566
                                                "pmulhw %%mm3, %%mm6        \n\t"\
2567
                                                "packuswb %%mm5, %%mm0        \n\t"\
2568
                                                "packuswb %%mm6, %%mm1        \n\t"\
2569
                                                "movq %%mm0, " #dst1 "        \n\t"\
2570
                                                "movq %%mm1, " #dst2 "        \n\t"\
2571

    
2572
#endif //!HAVE_MMX2
2573

    
2574
SCALED_CPY((%2)       , (%2, %4)      , (%3)       , (%3, %5))
2575
SCALED_CPY((%2, %4, 2), (%%eax, %4, 2), (%3, %5, 2), (%%edx, %5, 2))
2576
SCALED_CPY((%2, %4, 4), (%%eax, %4, 4), (%3, %5, 4), (%%edx, %5, 4))
2577
                                                "leal (%%eax,%4,4), %%eax        \n\t"
2578
                                                "leal (%%edx,%5,4), %%edx        \n\t"
2579
SCALED_CPY((%%eax, %4), (%%eax, %4, 2), (%%edx, %5), (%%edx, %5, 2))
2580

    
2581

    
2582
                                                : "=&a" (packedOffsetAndScale)
2583
                                                : "0" (packedOffsetAndScale),
2584
                                                "r"(src),
2585
                                                "r"(dst),
2586
                                                "r" (srcStride),
2587
                                                "r" (dstStride)
2588
                                                : "%edx"
2589
                                        );
2590
#else
2591
                                for(i=0; i<8; i++)
2592
                                        memcpy(        &(dst[dstStride*i]),
2593
                                                &(src[srcStride*i]), BLOCK_SIZE);
2594
#endif
2595
        }
2596
        else
2597
        {
2598
#ifdef HAVE_MMX
2599
                                        asm volatile(
2600
                                                "leal (%0,%2), %%eax        \n\t"
2601
                                                "leal (%1,%3), %%edx        \n\t"
2602

    
2603
#define SIMPLE_CPY(src1, src2, dst1, dst2)                                \
2604
                                                "movq " #src1 ", %%mm0        \n\t"\
2605
                                                "movq " #src2 ", %%mm1        \n\t"\
2606
                                                "movq %%mm0, " #dst1 "        \n\t"\
2607
                                                "movq %%mm1, " #dst2 "        \n\t"\
2608

    
2609
SIMPLE_CPY((%0)       , (%0, %2)      , (%1)       , (%1, %3))
2610
SIMPLE_CPY((%0, %2, 2), (%%eax, %2, 2), (%1, %3, 2), (%%edx, %3, 2))
2611
SIMPLE_CPY((%0, %2, 4), (%%eax, %2, 4), (%1, %3, 4), (%%edx, %3, 4))
2612
                                                "leal (%%eax,%2,4), %%eax        \n\t"
2613
                                                "leal (%%edx,%3,4), %%edx        \n\t"
2614
SIMPLE_CPY((%%eax, %2), (%%eax, %2, 2), (%%edx, %3), (%%edx, %3, 2))
2615

    
2616
                                                : : "r" (src),
2617
                                                "r" (dst),
2618
                                                "r" (srcStride),
2619
                                                "r" (dstStride)
2620
                                                : "%eax", "%edx"
2621
                                        );
2622
#else
2623
                                for(i=0; i<8; i++)
2624
                                        memcpy(        &(dst[dstStride*i]),
2625
                                                &(src[srcStride*i]), BLOCK_SIZE);
2626
#endif
2627
        }
2628
}
2629

    
2630
/**
2631
 * Duplicates the given 8 src pixels ? times upward
2632
 */
2633
static inline void RENAME(duplicate)(uint8_t src[], int stride)
2634
{
2635
#ifdef HAVE_MMX
2636
        asm volatile(
2637
                "movq (%0), %%mm0                \n\t"
2638
                "addl %1, %0                        \n\t"
2639
                "movq %%mm0, (%0)                \n\t"
2640
                "movq %%mm0, (%0, %1)                \n\t"
2641
                "movq %%mm0, (%0, %1, 2)        \n\t"
2642
                : "+r" (src)
2643
                : "r" (-stride)
2644
        );
2645
#else
2646
        int i;
2647
        uint8_t *p=src;
2648
        for(i=0; i<3; i++)
2649
        {
2650
                p-= stride;
2651
                memcpy(p, src, 8);
2652
        }
2653
#endif
2654
}
2655

    
2656
/**
2657
 * Filters array of bytes (Y or U or V values)
2658
 */
2659
static void RENAME(postProcess)(uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
2660
        QP_STORE_T QPs[], int QPStride, int isColor, PPContext *c2)
2661
{
2662
        PPContext __attribute__((aligned(8))) c= *c2; //copy to stack for faster access
2663
        int x,y;
2664
#ifdef COMPILE_TIME_MODE
2665
        const int mode= COMPILE_TIME_MODE;
2666
#else
2667
        const int mode= isColor ? c.ppMode.chromMode : c.ppMode.lumMode;
2668
#endif
2669
        int black=0, white=255; // blackest black and whitest white in the picture
2670
        int QPCorrecture= 256*256;
2671

    
2672
        int copyAhead;
2673
#ifdef HAVE_MMX
2674
        int i;
2675
#endif
2676

    
2677
        const int qpHShift= isColor ? 4-c.hChromaSubSample : 4;
2678
        const int qpVShift= isColor ? 4-c.vChromaSubSample : 4;
2679

    
2680
        //FIXME remove
2681
        uint64_t * const yHistogram= c.yHistogram;
2682
        uint8_t * const tempSrc= c.tempSrc;
2683
        uint8_t * const tempDst= c.tempDst;
2684
        const int mbWidth= isColor ? (width+7)>>3 : (width+15)>>4;
2685

    
2686
#ifdef HAVE_MMX
2687
        for(i=0; i<32; i++){
2688
                int offset= ((i*c.ppMode.baseDcDiff)>>8) + 1;
2689
                int threshold= offset*2 + 1;
2690
                c.mmxDcOffset[i]= 0x7F - offset;
2691
                c.mmxDcThreshold[i]= 0x7F - threshold;
2692
                c.mmxDcOffset[i]*= 0x0101010101010101LL;
2693
                c.mmxDcThreshold[i]*= 0x0101010101010101LL;
2694
        }
2695
#endif
2696

    
2697
        if(mode & CUBIC_IPOL_DEINT_FILTER) copyAhead=16;
2698
        else if(   (mode & LINEAR_BLEND_DEINT_FILTER)
2699
                || (mode & FFMPEG_DEINT_FILTER)) copyAhead=14;
2700
        else if(   (mode & V_DEBLOCK)
2701
                || (mode & LINEAR_IPOL_DEINT_FILTER)
2702
                || (mode & MEDIAN_DEINT_FILTER)) copyAhead=13;
2703
        else if(mode & V_X1_FILTER) copyAhead=11;
2704
//        else if(mode & V_RK1_FILTER) copyAhead=10;
2705
        else if(mode & DERING) copyAhead=9;
2706
        else copyAhead=8;
2707

    
2708
        copyAhead-= 8;
2709

    
2710
        if(!isColor)
2711
        {
2712
                uint64_t sum= 0;
2713
                int i;
2714
                uint64_t maxClipped;
2715
                uint64_t clipped;
2716
                double scale;
2717

    
2718
                c.frameNum++;
2719
                // first frame is fscked so we ignore it
2720
                if(c.frameNum == 1) yHistogram[0]= width*height/64*15/256;
2721

    
2722
                for(i=0; i<256; i++)
2723
                {
2724
                        sum+= yHistogram[i];
2725
//                        printf("%d ", yHistogram[i]);
2726
                }
2727
//                printf("\n\n");
2728

    
2729
                /* we allways get a completly black picture first */
2730
                maxClipped= (uint64_t)(sum * c.ppMode.maxClippedThreshold);
2731

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

    
2739
                clipped= sum;
2740
                for(white=0; white<256; white++)
2741
                {
2742
                        if(clipped < maxClipped) break;
2743
                        clipped-= yHistogram[white];
2744
                }
2745

    
2746
                scale= (double)(c.ppMode.maxAllowedY - c.ppMode.minAllowedY) / (double)(white-black);
2747

    
2748
#ifdef HAVE_MMX2
2749
                c.packedYScale= (uint16_t)(scale*256.0 + 0.5);
2750
                c.packedYOffset= (((black*c.packedYScale)>>8) - c.ppMode.minAllowedY) & 0xFFFF;
2751
#else
2752
                c.packedYScale= (uint16_t)(scale*1024.0 + 0.5);
2753
                c.packedYOffset= (black - c.ppMode.minAllowedY) & 0xFFFF;
2754
#endif
2755

    
2756
                c.packedYOffset|= c.packedYOffset<<32;
2757
                c.packedYOffset|= c.packedYOffset<<16;
2758

    
2759
                c.packedYScale|= c.packedYScale<<32;
2760
                c.packedYScale|= c.packedYScale<<16;
2761
                
2762
                if(mode & LEVEL_FIX)        QPCorrecture= (int)(scale*256*256 + 0.5);
2763
                else                        QPCorrecture= 256*256;
2764
        }
2765
        else
2766
        {
2767
                c.packedYScale= 0x0100010001000100LL;
2768
                c.packedYOffset= 0;
2769
                QPCorrecture= 256*256;
2770
        }
2771

    
2772
        /* copy & deinterlace first row of blocks */
2773
        y=-BLOCK_SIZE;
2774
        {
2775
                uint8_t *srcBlock= &(src[y*srcStride]);
2776
                uint8_t *dstBlock= tempDst + dstStride;
2777

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

    
2784
#ifdef HAVE_MMX2
2785
/*
2786
                        prefetchnta(srcBlock + (((x>>2)&6) + 5)*srcStride + 32);
2787
                        prefetchnta(srcBlock + (((x>>2)&6) + 6)*srcStride + 32);
2788
                        prefetcht0(dstBlock + (((x>>2)&6) + 5)*dstStride + 32);
2789
                        prefetcht0(dstBlock + (((x>>2)&6) + 6)*dstStride + 32);
2790
*/
2791

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

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

    
2820
                        RENAME(blockCopy)(dstBlock + dstStride*8, dstStride,
2821
                                srcBlock + srcStride*8, srcStride, mode & LEVEL_FIX, &c.packedYOffset);
2822

    
2823
                        RENAME(duplicate)(dstBlock + dstStride*8, dstStride);
2824

    
2825
                        if(mode & LINEAR_IPOL_DEINT_FILTER)
2826
                                RENAME(deInterlaceInterpolateLinear)(dstBlock, dstStride);
2827
                        else if(mode & LINEAR_BLEND_DEINT_FILTER)
2828
                                RENAME(deInterlaceBlendLinear)(dstBlock, dstStride);
2829
                        else if(mode & MEDIAN_DEINT_FILTER)
2830
                                RENAME(deInterlaceMedian)(dstBlock, dstStride);
2831
                        else if(mode & CUBIC_IPOL_DEINT_FILTER)
2832
                                RENAME(deInterlaceInterpolateCubic)(dstBlock, dstStride);
2833
                        else if(mode & FFMPEG_DEINT_FILTER)
2834
                                RENAME(deInterlaceFF)(dstBlock, dstStride, c.deintTemp + x);
2835
/*                        else if(mode & CUBIC_BLEND_DEINT_FILTER)
2836
                                RENAME(deInterlaceBlendCubic)(dstBlock, dstStride);
2837
*/
2838
                        dstBlock+=8;
2839
                        srcBlock+=8;
2840
                }
2841
                if(width==dstStride)
2842
                        memcpy(dst, tempDst + 9*dstStride, copyAhead*dstStride);
2843
                else
2844
                {
2845
                        int i;
2846
                        for(i=0; i<copyAhead; i++)
2847
                        {
2848
                                memcpy(dst + i*dstStride, tempDst + (9+i)*dstStride, width);
2849
                        }
2850
                }
2851
        }
2852

    
2853
//printf("\n");
2854
        for(y=0; y<height; y+=BLOCK_SIZE)
2855
        {
2856
                //1% speedup if these are here instead of the inner loop
2857
                uint8_t *srcBlock= &(src[y*srcStride]);
2858
                uint8_t *dstBlock= &(dst[y*dstStride]);
2859
#ifdef HAVE_MMX
2860
                uint8_t *tempBlock1= c.tempBlocks;
2861
                uint8_t *tempBlock2= c.tempBlocks + 8;
2862
#endif
2863
                int8_t *QPptr= &QPs[(y>>qpVShift)*QPStride];
2864
                int8_t *nonBQPptr= &c.nonBQPTable[(y>>qpVShift)*mbWidth];
2865
                int QP=0;
2866
                /* can we mess with a 8x16 block from srcBlock/dstBlock downwards and 1 line upwards
2867
                   if not than use a temporary buffer */
2868
                if(y+15 >= height)
2869
                {
2870
                        int i;
2871
                        /* copy from line (copyAhead) to (copyAhead+7) of src, these will be copied with
2872
                           blockcopy to dst later */
2873
                        memcpy(tempSrc + srcStride*copyAhead, srcBlock + srcStride*copyAhead,
2874
                                srcStride*MAX(height-y-copyAhead, 0) );
2875

    
2876
                        /* duplicate last line of src to fill the void upto line (copyAhead+7) */
2877
                        for(i=MAX(height-y, 8); i<copyAhead+8; i++)
2878
                                memcpy(tempSrc + srcStride*i, src + srcStride*(height-1), srcStride);
2879

    
2880
                        /* copy up to (copyAhead+1) lines of dst (line -1 to (copyAhead-1))*/
2881
                        memcpy(tempDst, dstBlock - dstStride, dstStride*MIN(height-y+1, copyAhead+1) );
2882

    
2883
                        /* duplicate last line of dst to fill the void upto line (copyAhead) */
2884
                        for(i=height-y+1; i<=copyAhead; i++)
2885
                                memcpy(tempDst + dstStride*i, dst + dstStride*(height-1), dstStride);
2886

    
2887
                        dstBlock= tempDst + dstStride;
2888
                        srcBlock= tempSrc;
2889
                }
2890
//printf("\n");
2891

    
2892
                // From this point on it is guranteed that we can read and write 16 lines downward
2893
                // finish 1 block before the next otherwise we?ll might have a problem
2894
                // with the L1 Cache of the P4 ... or only a few blocks at a time or soemthing
2895
                for(x=0; x<width; x+=BLOCK_SIZE)
2896
                {
2897
                        const int stride= dstStride;
2898
#ifdef HAVE_MMX
2899
                        uint8_t *tmpXchg;
2900
#endif
2901
                        if(isColor)
2902
                        {
2903
                                QP= QPptr[x>>qpHShift];
2904
                                c.nonBQP= nonBQPptr[x>>qpHShift];
2905
                        }
2906
                        else
2907
                        {
2908
                                QP= QPptr[x>>4];
2909
                                QP= (QP* QPCorrecture + 256*128)>>16;
2910
                                c.nonBQP= nonBQPptr[x>>4];
2911
                                c.nonBQP= (c.nonBQP* QPCorrecture + 256*128)>>16;
2912
                                yHistogram[ srcBlock[srcStride*12 + 4] ]++;
2913
                        }
2914
                        c.QP= QP;
2915
#ifdef HAVE_MMX
2916
                        asm volatile(
2917
                                "movd %1, %%mm7                                        \n\t"
2918
                                "packuswb %%mm7, %%mm7                                \n\t" // 0, 0, 0, QP, 0, 0, 0, QP
2919
                                "packuswb %%mm7, %%mm7                                \n\t" // 0,QP, 0, QP, 0,QP, 0, QP
2920
                                "packuswb %%mm7, %%mm7                                \n\t" // QP,..., QP
2921
                                "movq %%mm7, %0                        \n\t"
2922
                                : "=m" (c.pQPb) 
2923
                                : "r" (QP)
2924
                        );
2925
#endif
2926

    
2927

    
2928
#ifdef HAVE_MMX2
2929
/*
2930
                        prefetchnta(srcBlock + (((x>>2)&6) + 5)*srcStride + 32);
2931
                        prefetchnta(srcBlock + (((x>>2)&6) + 6)*srcStride + 32);
2932
                        prefetcht0(dstBlock + (((x>>2)&6) + 5)*dstStride + 32);
2933
                        prefetcht0(dstBlock + (((x>>2)&6) + 6)*dstStride + 32);
2934
*/
2935

    
2936
                        asm(
2937
                                "movl %4, %%eax                        \n\t"
2938
                                "shrl $2, %%eax                        \n\t"
2939
                                "andl $6, %%eax                        \n\t"
2940
                                "addl %5, %%eax                        \n\t"
2941
                                "movl %%eax, %%edx                \n\t"
2942
                                "imul %1, %%eax                        \n\t"
2943
                                "imul %3, %%edx                        \n\t"
2944
                                "prefetchnta 32(%%eax, %0)        \n\t"
2945
                                "prefetcht0 32(%%edx, %2)        \n\t"
2946
                                "addl %1, %%eax                        \n\t"
2947
                                "addl %3, %%edx                        \n\t"
2948
                                "prefetchnta 32(%%eax, %0)        \n\t"
2949
                                "prefetcht0 32(%%edx, %2)        \n\t"
2950
                        :: "r" (srcBlock), "r" (srcStride), "r" (dstBlock), "r" (dstStride),
2951
                        "m" (x), "m" (copyAhead)
2952
                        : "%eax", "%edx"
2953
                        );
2954

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

    
2964
                        RENAME(blockCopy)(dstBlock + dstStride*copyAhead, dstStride,
2965
                                srcBlock + srcStride*copyAhead, srcStride, mode & LEVEL_FIX, &c.packedYOffset);
2966

    
2967
                        if(mode & LINEAR_IPOL_DEINT_FILTER)
2968
                                RENAME(deInterlaceInterpolateLinear)(dstBlock, dstStride);
2969
                        else if(mode & LINEAR_BLEND_DEINT_FILTER)
2970
                                RENAME(deInterlaceBlendLinear)(dstBlock, dstStride);
2971
                        else if(mode & MEDIAN_DEINT_FILTER)
2972
                                RENAME(deInterlaceMedian)(dstBlock, dstStride);
2973
                        else if(mode & CUBIC_IPOL_DEINT_FILTER)
2974
                                RENAME(deInterlaceInterpolateCubic)(dstBlock, dstStride);
2975
                        else if(mode & FFMPEG_DEINT_FILTER)
2976
                                RENAME(deInterlaceFF)(dstBlock, dstStride, c.deintTemp + x);
2977
/*                        else if(mode & CUBIC_BLEND_DEINT_FILTER)
2978
                                RENAME(deInterlaceBlendCubic)(dstBlock, dstStride);
2979
*/
2980

    
2981
                        /* only deblock if we have 2 blocks */
2982
                        if(y + 8 < height)
2983
                        {
2984
                                if(mode & V_X1_FILTER)
2985
                                        RENAME(vertX1Filter)(dstBlock, stride, &c);
2986
                                else if(mode & V_DEBLOCK)
2987
                                {
2988
                                        if( RENAME(isVertDC)(dstBlock, stride, &c))
2989
                                        {
2990
                                                if(RENAME(isVertMinMaxOk)(dstBlock, stride, &c))
2991
                                                        RENAME(doVertLowPass)(dstBlock, stride, &c);
2992
                                        }
2993
                                        else
2994
                                                RENAME(doVertDefFilter)(dstBlock, stride, &c);
2995
                                }
2996
                        }
2997

    
2998
#ifdef HAVE_MMX
2999
                        RENAME(transpose1)(tempBlock1, tempBlock2, dstBlock, dstStride);
3000
#endif
3001
                        /* check if we have a previous block to deblock it with dstBlock */
3002
                        if(x - 8 >= 0)
3003
                        {
3004
#ifdef HAVE_MMX
3005
                                if(mode & H_X1_FILTER)
3006
                                        RENAME(vertX1Filter)(tempBlock1, 16, &c);
3007
                                else if(mode & H_DEBLOCK)
3008
                                {
3009
                                        if( RENAME(isVertDC)(tempBlock1, 16, &c))
3010
                                        {
3011
                                                if(RENAME(isVertMinMaxOk)(tempBlock1, 16, &c))
3012
                                                        RENAME(doVertLowPass)(tempBlock1, 16, &c);
3013
                                        }
3014
                                        else
3015
                                                RENAME(doVertDefFilter)(tempBlock1, 16, &c);
3016
                                }
3017

    
3018
                                RENAME(transpose2)(dstBlock-4, dstStride, tempBlock1 + 4*16);
3019

    
3020
#else
3021
                                if(mode & H_X1_FILTER)
3022
                                        horizX1Filter(dstBlock-4, stride, QP);
3023
                                else if(mode & H_DEBLOCK)
3024
                                {
3025
                                        if( isHorizDC(dstBlock-4, stride, &c))
3026
                                        {
3027
                                                if(isHorizMinMaxOk(dstBlock-4, stride, QP))
3028
                                                        doHorizLowPass(dstBlock-4, stride, QP);
3029
                                        }
3030
                                        else
3031
                                                doHorizDefFilter(dstBlock-4, stride, QP);
3032
                                }
3033
#endif
3034
                                if(mode & DERING)
3035
                                {
3036
                                //FIXME filter first line
3037
                                        if(y>0) RENAME(dering)(dstBlock - stride - 8, stride, &c);
3038
                                }
3039

    
3040
                                if(mode & TEMP_NOISE_FILTER)
3041
                                {
3042
                                        RENAME(tempNoiseReducer)(dstBlock-8, stride,
3043
                                                c.tempBlured[isColor] + y*dstStride + x,
3044
                                                c.tempBluredPast[isColor] + (y>>3)*256 + (x>>3),
3045
                                                c.ppMode.maxTmpNoise);
3046
                                }
3047
                        }
3048

    
3049
                        dstBlock+=8;
3050
                        srcBlock+=8;
3051

    
3052
#ifdef HAVE_MMX
3053
                        tmpXchg= tempBlock1;
3054
                        tempBlock1= tempBlock2;
3055
                        tempBlock2 = tmpXchg;
3056
#endif
3057
                }
3058

    
3059
                if(mode & DERING)
3060
                {
3061
                                if(y > 0) RENAME(dering)(dstBlock - dstStride - 8, dstStride, &c);
3062
                }
3063

    
3064
                if((mode & TEMP_NOISE_FILTER))
3065
                {
3066
                        RENAME(tempNoiseReducer)(dstBlock-8, dstStride,
3067
                                c.tempBlured[isColor] + y*dstStride + x,
3068
                                c.tempBluredPast[isColor] + (y>>3)*256 + (x>>3),
3069
                                c.ppMode.maxTmpNoise);
3070
                }
3071

    
3072
                /* did we use a tmp buffer for the last lines*/
3073
                if(y+15 >= height)
3074
                {
3075
                        uint8_t *dstBlock= &(dst[y*dstStride]);
3076
                        if(width==dstStride)
3077
                                memcpy(dstBlock, tempDst + dstStride, dstStride*(height-y));
3078
                        else
3079
                        {
3080
                                int i;
3081
                                for(i=0; i<height-y; i++)
3082
                                {
3083
                                        memcpy(dstBlock + i*dstStride, tempDst + (i+1)*dstStride, width);
3084
                                }
3085
                        }
3086
                }
3087
/*
3088
                for(x=0; x<width; x+=32)
3089
                {
3090
                        volatile int i;
3091
                        i+=        + dstBlock[x + 7*dstStride] + dstBlock[x + 8*dstStride]
3092
                                + dstBlock[x + 9*dstStride] + dstBlock[x +10*dstStride]
3093
                                + dstBlock[x +11*dstStride] + dstBlock[x +12*dstStride];
3094
//                                + dstBlock[x +13*dstStride]
3095
//                                + dstBlock[x +14*dstStride] + dstBlock[x +15*dstStride];
3096
                }*/
3097
        }
3098
#ifdef HAVE_3DNOW
3099
        asm volatile("femms");
3100
#elif defined (HAVE_MMX)
3101
        asm volatile("emms");
3102
#endif
3103

    
3104
#ifdef DEBUG_BRIGHTNESS
3105
        if(!isColor)
3106
        {
3107
                int max=1;
3108
                int i;
3109
                for(i=0; i<256; i++)
3110
                        if(yHistogram[i] > max) max=yHistogram[i];
3111

    
3112
                for(i=1; i<256; i++)
3113
                {
3114
                        int x;
3115
                        int start=yHistogram[i-1]/(max/256+1);
3116
                        int end=yHistogram[i]/(max/256+1);
3117
                        int inc= end > start ? 1 : -1;
3118
                        for(x=start; x!=end+inc; x+=inc)
3119
                                dst[ i*dstStride + x]+=128;
3120
                }
3121

    
3122
                for(i=0; i<100; i+=2)
3123
                {
3124
                        dst[ (white)*dstStride + i]+=128;
3125
                        dst[ (black)*dstStride + i]+=128;
3126
                }
3127

    
3128
        }
3129
#endif
3130

    
3131
        *c2= c; //copy local context back
3132

    
3133
}