postprocess_template.c 186 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2001-2002 Michael Niedermayer (michaelni@gmx.at)
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20

Michael Niedermayer's avatar
Michael Niedermayer committed
21 22 23 24 25 26
/**
 * @file postprocess_template.c
 * mmx/mmx2/3dnow postprocess code.
 */


27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#ifdef ARCH_X86_64
#  define REGa  rax
#  define REGc  rcx
#  define REGd  rdx
#  define REG_a  "rax"
#  define REG_c  "rcx"
#  define REG_d  "rdx"
#  define REG_SP "rsp"
#  define ALIGN_MASK "$0xFFFFFFFFFFFFFFF8"
#else
#  define REGa  eax
#  define REGc  ecx
#  define REGd  edx
#  define REG_a  "eax"
#  define REG_c  "ecx"
#  define REG_d  "edx"
#  define REG_SP "esp"
#  define ALIGN_MASK "$0xFFFFFFF8"
#endif


48 49 50
#undef PAVGB
#undef PMINUB
#undef PMAXUB
Arpi's avatar
Arpi committed
51 52

#ifdef HAVE_MMX2
53
#define REAL_PAVGB(a,b) "pavgb " #a ", " #b " \n\t"
Arpi's avatar
Arpi committed
54
#elif defined (HAVE_3DNOW)
D Richard Felker III's avatar
D Richard Felker III committed
55
#define REAL_PAVGB(a,b) "pavgusb " #a ", " #b " \n\t"
Arpi's avatar
Arpi committed
56
#endif
57
#define PAVGB(a,b)  REAL_PAVGB(a,b)
58

Michael Niedermayer's avatar
Michael Niedermayer committed
59 60 61 62
#ifdef HAVE_MMX2
#define PMINUB(a,b,t) "pminub " #a ", " #b " \n\t"
#elif defined (HAVE_MMX)
#define PMINUB(b,a,t) \
63 64 65
        "movq " #a ", " #t " \n\t"\
        "psubusb " #b ", " #t " \n\t"\
        "psubb " #t ", " #a " \n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
66 67 68 69 70 71
#endif

#ifdef HAVE_MMX2
#define PMAXUB(a,b) "pmaxub " #a ", " #b " \n\t"
#elif defined (HAVE_MMX)
#define PMAXUB(a,b) \
72 73
        "psubusb " #a ", " #b " \n\t"\
        "paddb " #a ", " #b " \n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
74 75
#endif

Diego Biurrun's avatar
Diego Biurrun committed
76
//FIXME? |255-0| = 1 (should not be a problem ...)
77
#ifdef HAVE_MMX
78
/**
79
 * Check if the middle 8x8 Block in the given 8x16 block is flat
80
 */
81
static inline int RENAME(vertClassify)(uint8_t src[], int stride, PPContext *c){
82 83
        int numEq= 0, dcOk;
        src+= stride*4; // src points to begin of the 8x8 Block
84
asm volatile(
85 86
                "movq %0, %%mm7                         \n\t"
                "movq %1, %%mm6                         \n\t"
87 88
                : : "m" (c->mmxDcOffset[c->nonBQP]),  "m" (c->mmxDcThreshold[c->nonBQP])
                );
89

90
asm volatile(
91 92 93 94 95 96 97 98
                "lea (%2, %3), %%"REG_a"                \n\t"
//      0       1       2       3       4       5       6       7       8       9
//      %1      eax     eax+%2  eax+2%2 %1+4%2  ecx     ecx+%2  ecx+2%2 %1+8%2  ecx+4%2

                "movq (%2), %%mm0                       \n\t"
                "movq (%%"REG_a"), %%mm1                \n\t"
                "movq %%mm0, %%mm3                      \n\t"
                "movq %%mm0, %%mm4                      \n\t"
99 100
                PMAXUB(%%mm1, %%mm4)
                PMINUB(%%mm1, %%mm3, %%mm5)
101 102 103
                "psubb %%mm1, %%mm0                     \n\t" // mm0 = differnece
                "paddb %%mm7, %%mm0                     \n\t"
                "pcmpgtb %%mm6, %%mm0                   \n\t"
104

105
                "movq (%%"REG_a",%3), %%mm2             \n\t"
106 107
                PMAXUB(%%mm2, %%mm4)
                PMINUB(%%mm2, %%mm3, %%mm5)
108 109 110 111
                "psubb %%mm2, %%mm1                     \n\t"
                "paddb %%mm7, %%mm1                     \n\t"
                "pcmpgtb %%mm6, %%mm1                   \n\t"
                "paddb %%mm1, %%mm0                     \n\t"
112

113
                "movq (%%"REG_a", %3, 2), %%mm1         \n\t"
114 115
                PMAXUB(%%mm1, %%mm4)
                PMINUB(%%mm1, %%mm3, %%mm5)
116 117 118 119
                "psubb %%mm1, %%mm2                     \n\t"
                "paddb %%mm7, %%mm2                     \n\t"
                "pcmpgtb %%mm6, %%mm2                   \n\t"
                "paddb %%mm2, %%mm0                     \n\t"
120

121
                "lea (%%"REG_a", %3, 4), %%"REG_a"      \n\t"
122

123
                "movq (%2, %3, 4), %%mm2                \n\t"
124 125
                PMAXUB(%%mm2, %%mm4)
                PMINUB(%%mm2, %%mm3, %%mm5)
126 127 128 129
                "psubb %%mm2, %%mm1                     \n\t"
                "paddb %%mm7, %%mm1                     \n\t"
                "pcmpgtb %%mm6, %%mm1                   \n\t"
                "paddb %%mm1, %%mm0                     \n\t"
130

131
                "movq (%%"REG_a"), %%mm1                \n\t"
132 133
                PMAXUB(%%mm1, %%mm4)
                PMINUB(%%mm1, %%mm3, %%mm5)
134 135 136 137
                "psubb %%mm1, %%mm2                     \n\t"
                "paddb %%mm7, %%mm2                     \n\t"
                "pcmpgtb %%mm6, %%mm2                   \n\t"
                "paddb %%mm2, %%mm0                     \n\t"
138

139
                "movq (%%"REG_a", %3), %%mm2            \n\t"
140 141
                PMAXUB(%%mm2, %%mm4)
                PMINUB(%%mm2, %%mm3, %%mm5)
142 143 144 145
                "psubb %%mm2, %%mm1                     \n\t"
                "paddb %%mm7, %%mm1                     \n\t"
                "pcmpgtb %%mm6, %%mm1                   \n\t"
                "paddb %%mm1, %%mm0                     \n\t"
146

147
                "movq (%%"REG_a", %3, 2), %%mm1         \n\t"
148 149
                PMAXUB(%%mm1, %%mm4)
                PMINUB(%%mm1, %%mm3, %%mm5)
150 151 152 153 154
                "psubb %%mm1, %%mm2                     \n\t"
                "paddb %%mm7, %%mm2                     \n\t"
                "pcmpgtb %%mm6, %%mm2                   \n\t"
                "paddb %%mm2, %%mm0                     \n\t"
                "psubusb %%mm3, %%mm4                   \n\t"
155

156
                "                                       \n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
157
#ifdef HAVE_MMX2
158 159
                "pxor %%mm7, %%mm7                      \n\t"
                "psadbw %%mm7, %%mm0                    \n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
160
#else
161 162 163 164 165 166 167 168 169
                "movq %%mm0, %%mm1                      \n\t"
                "psrlw $8, %%mm0                        \n\t"
                "paddb %%mm1, %%mm0                     \n\t"
                "movq %%mm0, %%mm1                      \n\t"
                "psrlq $16, %%mm0                       \n\t"
                "paddb %%mm1, %%mm0                     \n\t"
                "movq %%mm0, %%mm1                      \n\t"
                "psrlq $32, %%mm0                       \n\t"
                "paddb %%mm1, %%mm0                     \n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
170
#endif
171 172 173 174 175 176 177 178 179 180 181 182 183 184
                "movq %4, %%mm7                         \n\t" // QP,..., QP
                "paddusb %%mm7, %%mm7                   \n\t" // 2QP ... 2QP
                "psubusb %%mm7, %%mm4                   \n\t" // Diff <= 2QP -> 0
                "packssdw %%mm4, %%mm4                  \n\t"
                "movd %%mm0, %0                         \n\t"
                "movd %%mm4, %1                         \n\t"

                : "=r" (numEq), "=r" (dcOk)
                : "r" (src), "r" ((long)stride), "m" (c->pQPb)
                : "%"REG_a
                );

        numEq= (-numEq) &0xFF;
        if(numEq > c->ppMode.flatnessThreshold){
185 186 187 188 189
            if(dcOk) return 0;
            else     return 1;
        }else{
            return 2;
        }
190
}
191
#endif //HAVE_MMX
192 193

/**
194
 * Do a vertical low pass filter on the 8x16 block (only write to the 8x8 block in the middle)
Michael Niedermayer's avatar
Michael Niedermayer committed
195
 * using the 9-Tap Filter (1,1,2,2,4,2,2,1,1)/16
196
 */
197
#ifndef HAVE_ALTIVEC
198
static inline void RENAME(doVertLowPass)(uint8_t *src, int stride, PPContext *c)
199
{
200
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        src+= stride*3;
        asm volatile(        //"movv %0 %1 %2\n\t"
                "movq %2, %%mm0                         \n\t"  // QP,..., QP
                "pxor %%mm4, %%mm4                      \n\t"

                "movq (%0), %%mm6                       \n\t"
                "movq (%0, %1), %%mm5                   \n\t"
                "movq %%mm5, %%mm1                      \n\t"
                "movq %%mm6, %%mm2                      \n\t"
                "psubusb %%mm6, %%mm5                   \n\t"
                "psubusb %%mm1, %%mm2                   \n\t"
                "por %%mm5, %%mm2                       \n\t" // ABS Diff of lines
                "psubusb %%mm0, %%mm2                   \n\t" // diff <= QP -> 0
                "pcmpeqb %%mm4, %%mm2                   \n\t" // diff <= QP -> FF

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

                "movq (%0, %1, 8), %%mm5                \n\t"
                "lea (%0, %1, 4), %%"REG_a"             \n\t"
                "lea (%0, %1, 8), %%"REG_c"             \n\t"
                "sub %1, %%"REG_c"                      \n\t"
                "add %1, %0                             \n\t" // %0 points to line 1 not 0
                "movq (%0, %1, 8), %%mm7                \n\t"
                "movq %%mm5, %%mm1                      \n\t"
                "movq %%mm7, %%mm2                      \n\t"
                "psubusb %%mm7, %%mm5                   \n\t"
                "psubusb %%mm1, %%mm2                   \n\t"
                "por %%mm5, %%mm2                       \n\t" // ABS Diff of lines
                "psubusb %%mm0, %%mm2                   \n\t" // diff <= QP -> 0
                "pcmpeqb %%mm4, %%mm2                   \n\t" // diff <= QP -> FF

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


                //      1       2       3       4       5       6       7       8
                //      %0      %0+%1   %0+2%1  eax     %0+4%1  eax+2%1 ecx     eax+4%1
                // 6 4 2 2 1 1
                // 6 4 4 2
                // 6 8 2

                "movq (%0, %1), %%mm0                   \n\t" //  1
                "movq %%mm0, %%mm1                      \n\t" //  1
                PAVGB(%%mm6, %%mm0)                           //1 1        /2
                PAVGB(%%mm6, %%mm0)                           //3 1        /4

                "movq (%0, %1, 4), %%mm2                \n\t" //     1
                "movq %%mm2, %%mm5                      \n\t" //     1
                PAVGB((%%REGa), %%mm2)                        //    11        /2
                PAVGB((%0, %1, 2), %%mm2)                     //   211        /4
                "movq %%mm2, %%mm3                      \n\t" //   211        /4
                "movq (%0), %%mm4                       \n\t" // 1
                PAVGB(%%mm4, %%mm3)                           // 4 211        /8
                PAVGB(%%mm0, %%mm3)                           //642211        /16
                "movq %%mm3, (%0)                       \n\t" // X
                // mm1=2 mm2=3(211) mm4=1 mm5=5 mm6=0 mm7=9
                "movq %%mm1, %%mm0                      \n\t" //  1
                PAVGB(%%mm6, %%mm0)                           //1 1        /2
                "movq %%mm4, %%mm3                      \n\t" // 1
                PAVGB((%0,%1,2), %%mm3)                       // 1 1        /2
                PAVGB((%%REGa,%1,2), %%mm5)                   //     11        /2
                PAVGB((%%REGa), %%mm5)                        //    211 /4
                PAVGB(%%mm5, %%mm3)                           // 2 2211 /8
                PAVGB(%%mm0, %%mm3)                           //4242211 /16
                "movq %%mm3, (%0,%1)                    \n\t" //  X
                // mm1=2 mm2=3(211) mm4=1 mm5=4(211) mm6=0 mm7=9
                PAVGB(%%mm4, %%mm6)                                   //11        /2
                "movq (%%"REG_c"), %%mm0                \n\t" //       1
                PAVGB((%%REGa, %1, 2), %%mm0)                 //      11/2
                "movq %%mm0, %%mm3                      \n\t" //      11/2
                PAVGB(%%mm1, %%mm0)                           //  2   11/4
                PAVGB(%%mm6, %%mm0)                           //222   11/8
                PAVGB(%%mm2, %%mm0)                           //22242211/16
                "movq (%0, %1, 2), %%mm2                \n\t" //   1
                "movq %%mm0, (%0, %1, 2)                \n\t" //   X
                // mm1=2 mm2=3 mm3=6(11) mm4=1 mm5=4(211) mm6=0(11) mm7=9
                "movq (%%"REG_a", %1, 4), %%mm0         \n\t" //        1
                PAVGB((%%REGc), %%mm0)                        //       11        /2
                PAVGB(%%mm0, %%mm6)                           //11     11        /4
                PAVGB(%%mm1, %%mm4)                           // 11                /2
                PAVGB(%%mm2, %%mm1)                           //  11                /2
                PAVGB(%%mm1, %%mm6)                           //1122   11        /8
                PAVGB(%%mm5, %%mm6)                           //112242211        /16
                "movq (%%"REG_a"), %%mm5                \n\t" //    1
                "movq %%mm6, (%%"REG_a")                \n\t" //    X
                // mm0=7(11) mm1=2(11) mm2=3 mm3=6(11) mm4=1(11) mm5=4 mm7=9
                "movq (%%"REG_a", %1, 4), %%mm6         \n\t" //        1
                PAVGB(%%mm7, %%mm6)                           //        11        /2
                PAVGB(%%mm4, %%mm6)                           // 11     11        /4
                PAVGB(%%mm3, %%mm6)                           // 11   2211        /8
                PAVGB(%%mm5, %%mm2)                           //   11                /2
                "movq (%0, %1, 4), %%mm4                \n\t" //     1
                PAVGB(%%mm4, %%mm2)                           //   112                /4
                PAVGB(%%mm2, %%mm6)                           // 112242211        /16
                "movq %%mm6, (%0, %1, 4)                \n\t" //     X
                // mm0=7(11) mm1=2(11) mm2=3(112) mm3=6(11) mm4=5 mm5=4 mm7=9
                PAVGB(%%mm7, %%mm1)                           //  11     2        /4
                PAVGB(%%mm4, %%mm5)                           //    11                /2
                PAVGB(%%mm5, %%mm0)                           //    11 11        /4
                "movq (%%"REG_a", %1, 2), %%mm6         \n\t" //      1
                PAVGB(%%mm6, %%mm1)                           //  11  4  2        /8
                PAVGB(%%mm0, %%mm1)                           //  11224222        /16
                "movq %%mm1, (%%"REG_a", %1, 2)         \n\t" //      X
                // mm2=3(112) mm3=6(11) mm4=5 mm5=4(11) mm6=6 mm7=9
                PAVGB((%%REGc), %%mm2)                        //   112 4        /8
                "movq (%%"REG_a", %1, 4), %%mm0         \n\t" //        1
                PAVGB(%%mm0, %%mm6)                           //      1 1        /2
                PAVGB(%%mm7, %%mm6)                           //      1 12        /4
                PAVGB(%%mm2, %%mm6)                           //   1122424        /4
                "movq %%mm6, (%%"REG_c")                \n\t" //       X
                // mm0=8 mm3=6(11) mm4=5 mm5=4(11) mm7=9
                PAVGB(%%mm7, %%mm5)                           //    11   2        /4
                PAVGB(%%mm7, %%mm5)                           //    11   6        /8

                PAVGB(%%mm3, %%mm0)                           //      112        /4
                PAVGB(%%mm0, %%mm5)                           //    112246        /16
                "movq %%mm5, (%%"REG_a", %1, 4)         \n\t" //        X
                "sub %1, %0                             \n\t"

                :
                : "r" (src), "r" ((long)stride), "m" (c->pQPb)
                : "%"REG_a, "%"REG_c
        );
327
#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
328 329 330 331 332 333 334 335 336 337 338 339 340
        const int l1= stride;
        const int l2= stride + l1;
        const int l3= stride + l2;
        const int l4= stride + l3;
        const int l5= stride + l4;
        const int l6= stride + l5;
        const int l7= stride + l6;
        const int l8= stride + l7;
        const int l9= stride + l8;
        int x;
        src+= stride*3;
        for(x=0; x<BLOCK_SIZE; x++)
        {
341 342
                const int first= FFABS(src[0] - src[l1]) < c->QP ? src[0] : src[l1];
                const int last= FFABS(src[l8] - src[l9]) < c->QP ? src[l9] : src[l8];
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

                int sums[10];
                sums[0] = 4*first + src[l1] + src[l2] + src[l3] + 4;
                sums[1] = sums[0] - first  + src[l4];
                sums[2] = sums[1] - first  + src[l5];
                sums[3] = sums[2] - first  + src[l6];
                sums[4] = sums[3] - first  + src[l7];
                sums[5] = sums[4] - src[l1] + src[l8];
                sums[6] = sums[5] - src[l2] + last;
                sums[7] = sums[6] - src[l3] + last;
                sums[8] = sums[7] - src[l4] + last;
                sums[9] = sums[8] - src[l5] + last;

                src[l1]= (sums[0] + sums[2] + 2*src[l1])>>4;
                src[l2]= (sums[1] + sums[3] + 2*src[l2])>>4;
                src[l3]= (sums[2] + sums[4] + 2*src[l3])>>4;
                src[l4]= (sums[3] + sums[5] + 2*src[l4])>>4;
                src[l5]= (sums[4] + sums[6] + 2*src[l5])>>4;
                src[l6]= (sums[5] + sums[7] + 2*src[l6])>>4;
                src[l7]= (sums[6] + sums[8] + 2*src[l7])>>4;
                src[l8]= (sums[7] + sums[9] + 2*src[l8])>>4;

                src++;
        }
367
#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
368
}
369
#endif //HAVE_ALTIVEC
370

371
#if 0
372 373 374 375
/**
 * Experimental implementation of the filter (Algorithm 1) described in a paper from Ramkishor & Karandikar
 * values are correctly clipped (MMX2)
 * values are wraparound (C)
Diego Biurrun's avatar
Diego Biurrun committed
376 377
 * Conclusion: It is fast, but introduces ugly horizontal patterns
 * if there is a continuous gradient.
378 379 380 381 382
        0 8 16 24
        x = 8
        x/2 = 4
        x/8 = 1
        1 12 12 23
383
 */
384
static inline void RENAME(vertRK1Filter)(uint8_t *src, int stride, int QP)
385
{
386
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
387
        src+= stride*3;
388
// FIXME rounding
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
        asm volatile(
                "pxor %%mm7, %%mm7                      \n\t" // 0
                "movq "MANGLE(b80)", %%mm6              \n\t" // MIN_SIGNED_BYTE
                "leal (%0, %1), %%"REG_a"               \n\t"
                "leal (%%"REG_a", %1, 4), %%"REG_c"     \n\t"
//      0       1       2       3       4       5       6       7       8       9
//      %0      eax     eax+%1  eax+2%1 %0+4%1  ecx     ecx+%1  ecx+2%1 %0+8%1  ecx+4%1
                "movq "MANGLE(pQPb)", %%mm0             \n\t" // QP,..., QP
                "movq %%mm0, %%mm1                      \n\t" // QP,..., QP
                "paddusb "MANGLE(b02)", %%mm0           \n\t"
                "psrlw $2, %%mm0                        \n\t"
                "pand "MANGLE(b3F)", %%mm0              \n\t" // QP/4,..., QP/4
                "paddusb %%mm1, %%mm0                   \n\t" // QP*1.25 ...
                "movq (%0, %1, 4), %%mm2                \n\t" // line 4
                "movq (%%"REG_c"), %%mm3                \n\t" // line 5
                "movq %%mm2, %%mm4                      \n\t" // line 4
                "pcmpeqb %%mm5, %%mm5                   \n\t" // -1
                "pxor %%mm2, %%mm5                      \n\t" // -line 4 - 1
                PAVGB(%%mm3, %%mm5)
                "paddb %%mm6, %%mm5                     \n\t" // (l5-l4)/2
                "psubusb %%mm3, %%mm4                   \n\t"
                "psubusb %%mm2, %%mm3                   \n\t"
                "por %%mm3, %%mm4                       \n\t" // |l4 - l5|
                "psubusb %%mm0, %%mm4                   \n\t"
                "pcmpeqb %%mm7, %%mm4                   \n\t"
                "pand %%mm4, %%mm5                      \n\t" // d/2

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

                "movq (%%"REG_c"), %%mm2                \n\t"
//                "paddb %%mm6, %%mm2                     \n\t" // line 5 + 0x80
                "psubb %%mm5, %%mm2                     \n\t"
//                "psubb %%mm6, %%mm2                     \n\t"
                "movq %%mm2, (%%"REG_c")                \n\t"

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

                "movq (%%"REG_a", %1, 2), %%mm2         \n\t"
                "paddb %%mm6, %%mm2                     \n\t" // line 3 + 0x80
                "paddsb %%mm5, %%mm2                    \n\t"
                "psubb %%mm6, %%mm2                     \n\t"
                "movq %%mm2, (%%"REG_a", %1, 2)         \n\t"

                "movq (%%"REG_c", %1), %%mm2            \n\t"
                "paddb %%mm6, %%mm2                     \n\t" // line 6 + 0x80
                "psubsb %%mm5, %%mm2                    \n\t"
                "psubb %%mm6, %%mm2                     \n\t"
                "movq %%mm2, (%%"REG_c", %1)            \n\t"

                :
                : "r" (src), "r" ((long)stride)
                : "%"REG_a, "%"REG_c
        );
448
#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
         const int l1= stride;
        const int l2= stride + l1;
        const int l3= stride + l2;
        const int l4= stride + l3;
        const int l5= stride + l4;
        const int l6= stride + l5;
//        const int l7= stride + l6;
//        const int l8= stride + l7;
//        const int l9= stride + l8;
        int x;
        const int QP15= QP + (QP>>2);
        src+= stride*3;
        for(x=0; x<BLOCK_SIZE; x++)
        {
                const int v = (src[x+l5] - src[x+l4]);
464
                if(FFABS(v) < QP15)
465 466 467 468 469 470 471 472
                {
                        src[x+l3] +=v>>3;
                        src[x+l4] +=v>>1;
                        src[x+l5] -=v>>1;
                        src[x+l6] -=v>>3;

                }
        }
473

474
#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
475
}
476
#endif //0
477 478 479

/**
 * Experimental Filter 1
480 481
 * will not damage linear gradients
 * Flat blocks should look like they where passed through the (1,1,2,2,4,2,2,1,1) 9-Tap filter
Diego Biurrun's avatar
Diego Biurrun committed
482 483
 * can only smooth blocks at the expected locations (it cannot smooth them if they did move)
 * MMX2 version does correct clipping C version does not
484
 */
485
static inline void RENAME(vertX1Filter)(uint8_t *src, int stride, PPContext *co)
486
{
487
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
        src+= stride*3;

        asm volatile(
                "pxor %%mm7, %%mm7                      \n\t" // 0
                "lea (%0, %1), %%"REG_a"                \n\t"
                "lea (%%"REG_a", %1, 4), %%"REG_c"      \n\t"
//      0       1       2       3       4       5       6       7       8       9
//      %0      eax     eax+%1  eax+2%1 %0+4%1  ecx     ecx+%1  ecx+2%1 %0+8%1  ecx+4%1
                "movq (%%"REG_a", %1, 2), %%mm0         \n\t" // line 3
                "movq (%0, %1, 4), %%mm1                \n\t" // line 4
                "movq %%mm1, %%mm2                      \n\t" // line 4
                "psubusb %%mm0, %%mm1                   \n\t"
                "psubusb %%mm2, %%mm0                   \n\t"
                "por %%mm1, %%mm0                       \n\t" // |l2 - l3|
                "movq (%%"REG_c"), %%mm3                \n\t" // line 5
                "movq (%%"REG_c", %1), %%mm4            \n\t" // line 6
                "movq %%mm3, %%mm5                      \n\t" // line 5
                "psubusb %%mm4, %%mm3                   \n\t"
                "psubusb %%mm5, %%mm4                   \n\t"
                "por %%mm4, %%mm3                       \n\t" // |l5 - l6|
                PAVGB(%%mm3, %%mm0)                           // (|l2 - l3| + |l5 - l6|)/2
                "movq %%mm2, %%mm1                      \n\t" // line 4
                "psubusb %%mm5, %%mm2                   \n\t"
                "movq %%mm2, %%mm4                      \n\t"
                "pcmpeqb %%mm7, %%mm2                   \n\t" // (l4 - l5) <= 0 ? -1 : 0
                "psubusb %%mm1, %%mm5                   \n\t"
                "por %%mm5, %%mm4                       \n\t" // |l4 - l5|
                "psubusb %%mm0, %%mm4                   \n\t" //d = MAX(0, |l4-l5| - (|l2-l3| + |l5-l6|)/2)
                "movq %%mm4, %%mm3                      \n\t" // d
                "movq %2, %%mm0                         \n\t"
                "paddusb %%mm0, %%mm0                   \n\t"
                "psubusb %%mm0, %%mm4                   \n\t"
                "pcmpeqb %%mm7, %%mm4                   \n\t" // d <= QP ? -1 : 0
                "psubusb "MANGLE(b01)", %%mm3           \n\t"
                "pand %%mm4, %%mm3                      \n\t" // d <= QP ? d : 0

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

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

                "movq (%%"REG_c"), %%mm0                \n\t" // line 5
                "pxor %%mm2, %%mm0                      \n\t" //(l4 - l5) <= 0 ? -l5-1 : l5
                "paddusb %%mm3, %%mm0                   \n\t"
                "pxor %%mm2, %%mm0                      \n\t"
                "movq %%mm0, (%%"REG_c")                \n\t" // line 5

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

                "movq (%%"REG_a", %1, 2), %%mm0         \n\t" // line 3
                "pxor %%mm2, %%mm0                      \n\t" //(l4 - l5) <= 0 ? -l4-1 : l4
                "psubusb %%mm1, %%mm0                   \n\t"
                "pxor %%mm2, %%mm0                      \n\t"
                "movq %%mm0, (%%"REG_a", %1, 2)         \n\t" // line 3

                "movq (%%"REG_c", %1), %%mm0            \n\t" // line 6
                "pxor %%mm2, %%mm0                      \n\t" //(l4 - l5) <= 0 ? -l5-1 : l5
                "paddusb %%mm1, %%mm0                   \n\t"
                "pxor %%mm2, %%mm0                      \n\t"
                "movq %%mm0, (%%"REG_c", %1)            \n\t" // line 6

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

                "movq (%%"REG_a", %1), %%mm0            \n\t" // line 2
                "pxor %%mm2, %%mm0                      \n\t" //(l4 - l5) <= 0 ? -l2-1 : l2
                "psubusb %%mm1, %%mm0                   \n\t"
                "pxor %%mm2, %%mm0                      \n\t"
                "movq %%mm0, (%%"REG_a", %1)            \n\t" // line 2

                "movq (%%"REG_c", %1, 2), %%mm0         \n\t" // line 7
                "pxor %%mm2, %%mm0                      \n\t" //(l4 - l5) <= 0 ? -l7-1 : l7
                "paddusb %%mm1, %%mm0                   \n\t"
                "pxor %%mm2, %%mm0                      \n\t"
                "movq %%mm0, (%%"REG_c", %1, 2)         \n\t" // line 7

                :
                : "r" (src), "r" ((long)stride), "m" (co->pQPb)
                : "%"REG_a, "%"REG_c
        );
573
#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
574

575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
        const int l1= stride;
        const int l2= stride + l1;
        const int l3= stride + l2;
        const int l4= stride + l3;
        const int l5= stride + l4;
        const int l6= stride + l5;
        const int l7= stride + l6;
//        const int l8= stride + l7;
//        const int l9= stride + l8;
        int x;

        src+= stride*3;
        for(x=0; x<BLOCK_SIZE; x++)
        {
                int a= src[l3] - src[l4];
                int b= src[l4] - src[l5];
                int c= src[l5] - src[l6];

593
                int d= FFABS(b) - ((FFABS(a) + FFABS(c))>>1);
594
                d= FFMAX(d, 0);
595 596 597

                if(d < co->QP*2)
                {
598
                        int v = d * FFSIGN(-b);
599 600 601 602 603 604 605 606 607 608 609

                        src[l2] +=v>>3;
                        src[l3] +=v>>2;
                        src[l4] +=(3*v)>>3;
                        src[l5] -=(3*v)>>3;
                        src[l6] -=v>>2;
                        src[l7] -=v>>3;

                }
                src++;
        }
610
#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
611 612
}

613
#ifndef HAVE_ALTIVEC
614
static inline void RENAME(doVertDefFilter)(uint8_t src[], int stride, PPContext *c)
615
{
616 617
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
/*
618 619 620 621 622 623 624 625 626 627 628 629
        uint8_t tmp[16];
        const int l1= stride;
        const int l2= stride + l1;
        const int l3= stride + l2;
        const int l4= (int)tmp - (int)src - stride*3;
        const int l5= (int)tmp - (int)src - stride*3 + 8;
        const int l6= stride*3 + l3;
        const int l7= stride + l6;
        const int l8= stride + l7;

        memcpy(tmp, src+stride*7, 8);
        memcpy(tmp+8, src+stride*8, 8);
630
*/
631 632
        src+= stride*4;
        asm volatile(
633 634

#if 0 //sligtly more accurate and slightly slower
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
                "pxor %%mm7, %%mm7                      \n\t" // 0
                "lea (%0, %1), %%"REG_a"                \n\t"
                "lea (%%"REG_a", %1, 4), %%"REG_c"      \n\t"
//      0       1       2       3       4       5       6       7
//      %0      %0+%1   %0+2%1  eax+2%1 %0+4%1  eax+4%1 ecx+%1  ecx+2%1
//      %0      eax     eax+%1  eax+2%1 %0+4%1  ecx     ecx+%1  ecx+2%1


                "movq (%0, %1, 2), %%mm0                \n\t" // l2
                "movq (%0), %%mm1                       \n\t" // l0
                "movq %%mm0, %%mm2                      \n\t" // l2
                PAVGB(%%mm7, %%mm0)                           // ~l2/2
                PAVGB(%%mm1, %%mm0)                           // ~(l2 + 2l0)/4
                PAVGB(%%mm2, %%mm0)                           // ~(5l2 + 2l0)/8

                "movq (%%"REG_a"), %%mm1                \n\t" // l1
                "movq (%%"REG_a", %1, 2), %%mm3         \n\t" // l3
                "movq %%mm1, %%mm4                      \n\t" // l1
                PAVGB(%%mm7, %%mm1)                           // ~l1/2
                PAVGB(%%mm3, %%mm1)                           // ~(l1 + 2l3)/4
                PAVGB(%%mm4, %%mm1)                           // ~(5l1 + 2l3)/8

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

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
                "movq (%0, %1, 4), %%mm0                \n\t" // l4
                "movq %%mm0, %%mm4                      \n\t" // l4
                PAVGB(%%mm7, %%mm0)                           // ~l4/2
                PAVGB(%%mm2, %%mm0)                           // ~(l4 + 2l2)/4
                PAVGB(%%mm4, %%mm0)                           // ~(5l4 + 2l2)/8

                "movq (%%"REG_c"), %%mm2                \n\t" // l5
                "movq %%mm3, %%mm5                      \n\t" // l3
                PAVGB(%%mm7, %%mm3)                           // ~l3/2
                PAVGB(%%mm2, %%mm3)                           // ~(l3 + 2l5)/4
                PAVGB(%%mm5, %%mm3)                           // ~(5l3 + 2l5)/8

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

682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
                "movq (%%"REG_c", %1), %%mm6            \n\t" // l6
                "movq %%mm6, %%mm5                      \n\t" // l6
                PAVGB(%%mm7, %%mm6)                           // ~l6/2
                PAVGB(%%mm4, %%mm6)                           // ~(l6 + 2l4)/4
                PAVGB(%%mm5, %%mm6)                           // ~(5l6 + 2l4)/8

                "movq (%%"REG_c", %1, 2), %%mm5         \n\t" // l7
                "movq %%mm2, %%mm4                      \n\t" // l5
                PAVGB(%%mm7, %%mm2)                           // ~l5/2
                PAVGB(%%mm5, %%mm2)                           // ~(l5 + 2l7)/4
                PAVGB(%%mm4, %%mm2)                           // ~(5l5 + 2l7)/8

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


701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
                PMINUB(%%mm2, %%mm1, %%mm4)                   // MIN(|lenergy|,|renergy|)/8
                "movq %2, %%mm4                         \n\t" // QP //FIXME QP+1 ?
                "paddusb "MANGLE(b01)", %%mm4           \n\t"
                "pcmpgtb %%mm3, %%mm4                   \n\t" // |menergy|/8 < QP
                "psubusb %%mm1, %%mm3                   \n\t" // d=|menergy|/8-MIN(|lenergy|,|renergy|)/8
                "pand %%mm4, %%mm3                      \n\t"

                "movq %%mm3, %%mm1                      \n\t"
//                "psubusb "MANGLE(b01)", %%mm3           \n\t"
                PAVGB(%%mm7, %%mm3)
                PAVGB(%%mm7, %%mm3)
                "paddusb %%mm1, %%mm3                   \n\t"
//                "paddusb "MANGLE(b01)", %%mm3           \n\t"

                "movq (%%"REG_a", %1, 2), %%mm6         \n\t" //l3
                "movq (%0, %1, 4), %%mm5                \n\t" //l4
                "movq (%0, %1, 4), %%mm4                \n\t" //l4
                "psubusb %%mm6, %%mm5                   \n\t"
                "psubusb %%mm4, %%mm6                   \n\t"
                "por %%mm6, %%mm5                       \n\t" // |l3-l4|
                "pcmpeqb %%mm7, %%mm6                   \n\t" // SIGN(l3-l4)
                "pxor %%mm6, %%mm0                      \n\t"
                "pand %%mm0, %%mm3                      \n\t"
                PMINUB(%%mm5, %%mm3, %%mm0)

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

                "movq (%%"REG_a", %1, 2), %%mm0         \n\t"
                "movq (%0, %1, 4), %%mm2                \n\t"
                "pxor %%mm6, %%mm0                      \n\t"
                "pxor %%mm6, %%mm2                      \n\t"
                "psubb %%mm3, %%mm0                     \n\t"
                "paddb %%mm3, %%mm2                     \n\t"
                "pxor %%mm6, %%mm0                      \n\t"
                "pxor %%mm6, %%mm2                      \n\t"
                "movq %%mm0, (%%"REG_a", %1, 2)         \n\t"
                "movq %%mm2, (%0, %1, 4)                \n\t"
739
#endif //0
740

741 742 743 744 745
                "lea (%0, %1), %%"REG_a"                \n\t"
                "pcmpeqb %%mm6, %%mm6                   \n\t" // -1
//      0       1       2       3       4       5       6       7
//      %0      %0+%1   %0+2%1  eax+2%1 %0+4%1  eax+4%1 ecx+%1  ecx+2%1
//      %0      eax     eax+%1  eax+2%1 %0+4%1  ecx     ecx+%1  ecx+2%1
746 747


748 749 750 751
                "movq (%%"REG_a", %1, 2), %%mm1         \n\t" // l3
                "movq (%0, %1, 4), %%mm0                \n\t" // l4
                "pxor %%mm6, %%mm1                      \n\t" // -l3-1
                PAVGB(%%mm1, %%mm0)                           // -q+128 = (l4-l3+256)/2
752 753
// mm1=-l3-1, mm0=128-q

754 755 756 757 758 759 760 761 762 763
                "movq (%%"REG_a", %1, 4), %%mm2         \n\t" // l5
                "movq (%%"REG_a", %1), %%mm3            \n\t" // l2
                "pxor %%mm6, %%mm2                      \n\t" // -l5-1
                "movq %%mm2, %%mm5                      \n\t" // -l5-1
                "movq "MANGLE(b80)", %%mm4              \n\t" // 128
                "lea (%%"REG_a", %1, 4), %%"REG_c"      \n\t"
                PAVGB(%%mm3, %%mm2)                           // (l2-l5+256)/2
                PAVGB(%%mm0, %%mm4)                           // ~(l4-l3)/4 + 128
                PAVGB(%%mm2, %%mm4)                           // ~(l2-l5)/4 +(l4-l3)/8 + 128
                PAVGB(%%mm0, %%mm4)                           // ~(l2-l5)/8 +5(l4-l3)/16 + 128
764 765
// mm1=-l3-1, mm0=128-q, mm3=l2, mm4=menergy/16 + 128, mm5= -l5-1

766 767 768 769 770 771 772 773
                "movq (%%"REG_a"), %%mm2                \n\t" // l1
                "pxor %%mm6, %%mm2                      \n\t" // -l1-1
                PAVGB(%%mm3, %%mm2)                           // (l2-l1+256)/2
                PAVGB((%0), %%mm1)                            // (l0-l3+256)/2
                "movq "MANGLE(b80)", %%mm3              \n\t" // 128
                PAVGB(%%mm2, %%mm3)                           // ~(l2-l1)/4 + 128
                PAVGB(%%mm1, %%mm3)                           // ~(l0-l3)/4 +(l2-l1)/8 + 128
                PAVGB(%%mm2, %%mm3)                           // ~(l0-l3)/8 +5(l2-l1)/16 + 128
774 775
// mm0=128-q, mm3=lenergy/16 + 128, mm4= menergy/16 + 128, mm5= -l5-1

776 777 778 779 780 781 782 783
                PAVGB((%%REGc, %1), %%mm5)                    // (l6-l5+256)/2
                "movq (%%"REG_c", %1, 2), %%mm1         \n\t" // l7
                "pxor %%mm6, %%mm1                      \n\t" // -l7-1
                PAVGB((%0, %1, 4), %%mm1)                     // (l4-l7+256)/2
                "movq "MANGLE(b80)", %%mm2              \n\t" // 128
                PAVGB(%%mm5, %%mm2)                           // ~(l6-l5)/4 + 128
                PAVGB(%%mm1, %%mm2)                           // ~(l4-l7)/4 +(l6-l5)/8 + 128
                PAVGB(%%mm5, %%mm2)                           // ~(l4-l7)/8 +5(l6-l5)/16 + 128
784 785
// mm0=128-q, mm2=renergy/16 + 128, mm3=lenergy/16 + 128, mm4= menergy/16 + 128

786 787 788 789 790 791 792
                "movq "MANGLE(b00)", %%mm1              \n\t" // 0
                "movq "MANGLE(b00)", %%mm5              \n\t" // 0
                "psubb %%mm2, %%mm1                     \n\t" // 128 - renergy/16
                "psubb %%mm3, %%mm5                     \n\t" // 128 - lenergy/16
                PMAXUB(%%mm1, %%mm2)                          // 128 + |renergy/16|
                 PMAXUB(%%mm5, %%mm3)                         // 128 + |lenergy/16|
                PMINUB(%%mm2, %%mm3, %%mm1)                   // 128 + MIN(|lenergy|,|renergy|)/16
793 794 795

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

796 797 798 799 800 801 802 803 804 805 806
                "movq "MANGLE(b00)", %%mm7              \n\t" // 0
                "movq %2, %%mm2                         \n\t" // QP
                PAVGB(%%mm6, %%mm2)                           // 128 + QP/2
                "psubb %%mm6, %%mm2                     \n\t"

                "movq %%mm4, %%mm1                      \n\t"
                "pcmpgtb %%mm7, %%mm1                   \n\t" // SIGN(menergy)
                "pxor %%mm1, %%mm4                      \n\t"
                "psubb %%mm1, %%mm4                     \n\t" // 128 + |menergy|/16
                "pcmpgtb %%mm4, %%mm2                   \n\t" // |menergy|/16 < QP/2
                "psubusb %%mm3, %%mm4                   \n\t" //d=|menergy|/16 - MIN(|lenergy|,|renergy|)/16
807 808
// mm0=128-q, mm1= SIGN(menergy), mm2= |menergy|/16 < QP/2, mm4= d/16

809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
                "movq %%mm4, %%mm3                      \n\t" // d
                "psubusb "MANGLE(b01)", %%mm4           \n\t"
                PAVGB(%%mm7, %%mm4)                           // d/32
                PAVGB(%%mm7, %%mm4)                           // (d + 32)/64
                "paddb %%mm3, %%mm4                     \n\t" // 5d/64
                "pand %%mm2, %%mm4                      \n\t"

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

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

                "pand %%mm7, %%mm4                      \n\t"
                "movq (%%"REG_a", %1, 2), %%mm0         \n\t"
                "movq (%0, %1, 4), %%mm2                \n\t"
                "pxor %%mm1, %%mm0                      \n\t"
                "pxor %%mm1, %%mm2                      \n\t"
                "paddb %%mm4, %%mm0                     \n\t"
                "psubb %%mm4, %%mm2                     \n\t"
                "pxor %%mm1, %%mm0                      \n\t"
                "pxor %%mm1, %%mm2                      \n\t"
                "movq %%mm0, (%%"REG_a", %1, 2)         \n\t"
                "movq %%mm2, (%0, %1, 4)                \n\t"

                :
                : "r" (src), "r" ((long)stride), "m" (c->pQPb)
                : "%"REG_a, "%"REG_c
        );
841 842

/*
843 844 845 846 847 848
        {
        int x;
        src-= stride;
        for(x=0; x<BLOCK_SIZE; x++)
        {
                const int middleEnergy= 5*(src[l5] - src[l4]) + 2*(src[l3] - src[l6]);
849
                if(FFABS(middleEnergy)< 8*QP)
850 851 852 853 854
                {
                        const int q=(src[l4] - src[l5])/2;
                        const int leftEnergy=  5*(src[l3] - src[l2]) + 2*(src[l1] - src[l4]);
                        const int rightEnergy= 5*(src[l7] - src[l6]) + 2*(src[l5] - src[l8]);

855
                        int d= FFABS(middleEnergy) - FFMIN( FFABS(leftEnergy), FFABS(rightEnergy) );
856
                        d= FFMAX(d, 0);
857 858

                        d= (5*d + 32) >> 6;
859
                        d*= FFSIGN(-middleEnergy);
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876

                        if(q>0)
                        {
                                d= d<0 ? 0 : d;
                                d= d>q ? q : d;
                        }
                        else
                        {
                                d= d>0 ? 0 : d;
                                d= d<q ? q : d;
                        }

                        src[l4]-= d;
                        src[l5]+= d;
                }
                src++;
        }
877
src-=8;
878 879 880 881 882 883
        for(x=0; x<8; x++)
        {
                int y;
                for(y=4; y<6; y++)
                {
                        int d= src[x+y*stride] - tmp[x+(y-4)*8];
884
                        int ad= FFABS(d);
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
                        static int max=0;
                        static int sum=0;
                        static int num=0;
                        static int bias=0;

                        if(max<ad) max=ad;
                        sum+= ad>3 ? 1 : 0;
                        if(ad>3)
                        {
                                src[0] = src[7] = src[stride*7] = src[(stride+1)*7]=255;
                        }
                        if(y==4) bias+=d;
                        num++;
                        if(num%1000000 == 0)
                        {
900
                                av_log(c, AV_LOG_INFO, " %d %d %d %d\n", num, sum, max, bias);
901 902 903
                        }
                }
        }
904 905 906
}
*/
#elif defined (HAVE_MMX)
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
        src+= stride*4;
        asm volatile(
                "pxor %%mm7, %%mm7                      \n\t"
                "lea -40(%%"REG_SP"), %%"REG_c"         \n\t" // make space for 4 8-byte vars
                "and "ALIGN_MASK", %%"REG_c"            \n\t" // align
//      0       1       2       3       4       5       6       7
//      %0      %0+%1   %0+2%1  eax+2%1 %0+4%1  eax+4%1 edx+%1  edx+2%1
//      %0      eax     eax+%1  eax+2%1 %0+4%1  edx     edx+%1  edx+2%1

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

                "movq (%0, %1), %%mm2                   \n\t"
                "lea (%0, %1, 2), %%"REG_a"             \n\t"
                "movq %%mm2, %%mm3                      \n\t"
                "punpcklbw %%mm7, %%mm2                 \n\t" // low part of line 1
                "punpckhbw %%mm7, %%mm3                 \n\t" // high part of line 1

                "movq (%%"REG_a"), %%mm4                \n\t"
                "movq %%mm4, %%mm5                      \n\t"
                "punpcklbw %%mm7, %%mm4                 \n\t" // low part of line 2
                "punpckhbw %%mm7, %%mm5                 \n\t" // high part of line 2

                "paddw %%mm0, %%mm0                     \n\t" // 2L0
                "paddw %%mm1, %%mm1                     \n\t" // 2H0
                "psubw %%mm4, %%mm2                     \n\t" // L1 - L2
                "psubw %%mm5, %%mm3                     \n\t" // H1 - H2
                "psubw %%mm2, %%mm0                     \n\t" // 2L0 - L1 + L2
                "psubw %%mm3, %%mm1                     \n\t" // 2H0 - H1 + H2

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

                "movq (%%"REG_a", %1), %%mm2            \n\t"
                "movq %%mm2, %%mm3                      \n\t"
                "punpcklbw %%mm7, %%mm2                 \n\t" // L3
                "punpckhbw %%mm7, %%mm3                 \n\t" // H3

                "psubw %%mm2, %%mm0                     \n\t" // 2L0 - 5L1 + 5L2 - L3
                "psubw %%mm3, %%mm1                     \n\t" // 2H0 - 5H1 + 5H2 - H3
                "psubw %%mm2, %%mm0                     \n\t" // 2L0 - 5L1 + 5L2 - 2L3
                "psubw %%mm3, %%mm1                     \n\t" // 2H0 - 5H1 + 5H2 - 2H3
                "movq %%mm0, (%%"REG_c")                \n\t" // 2L0 - 5L1 + 5L2 - 2L3
                "movq %%mm1, 8(%%"REG_c")               \n\t" // 2H0 - 5H1 + 5H2 - 2H3

                "movq (%%"REG_a", %1, 2), %%mm0         \n\t"
                "movq %%mm0, %%mm1                      \n\t"
                "punpcklbw %%mm7, %%mm0                 \n\t" // L4
                "punpckhbw %%mm7, %%mm1                 \n\t" // H4

                "psubw %%mm0, %%mm2                     \n\t" // L3 - L4
                "psubw %%mm1, %%mm3                     \n\t" // H3 - H4
                "movq %%mm2, 16(%%"REG_c")              \n\t" // L3 - L4
                "movq %%mm3, 24(%%"REG_c")              \n\t" // H3 - H4
                "paddw %%mm4, %%mm4                     \n\t" // 2L2
                "paddw %%mm5, %%mm5                     \n\t" // 2H2
                "psubw %%mm2, %%mm4                     \n\t" // 2L2 - L3 + L4
                "psubw %%mm3, %%mm5                     \n\t" // 2H2 - H3 + H4

                "lea (%%"REG_a", %1), %0                \n\t"
                "psllw $2, %%mm2                        \n\t" // 4L3 - 4L4
                "psllw $2, %%mm3                        \n\t" // 4H3 - 4H4
                "psubw %%mm2, %%mm4                     \n\t" // 2L2 - 5L3 + 5L4
                "psubw %%mm3, %%mm5                     \n\t" // 2H2 - 5H3 + 5H4
975
//50 opcodes so far
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
                "movq (%0, %1, 2), %%mm2                \n\t"
                "movq %%mm2, %%mm3                      \n\t"
                "punpcklbw %%mm7, %%mm2                 \n\t" // L5
                "punpckhbw %%mm7, %%mm3                 \n\t" // H5
                "psubw %%mm2, %%mm4                     \n\t" // 2L2 - 5L3 + 5L4 - L5
                "psubw %%mm3, %%mm5                     \n\t" // 2H2 - 5H3 + 5H4 - H5
                "psubw %%mm2, %%mm4                     \n\t" // 2L2 - 5L3 + 5L4 - 2L5
                "psubw %%mm3, %%mm5                     \n\t" // 2H2 - 5H3 + 5H4 - 2H5

                "movq (%%"REG_a", %1, 4), %%mm6         \n\t"
                "punpcklbw %%mm7, %%mm6                 \n\t" // L6
                "psubw %%mm6, %%mm2                     \n\t" // L5 - L6
                "movq (%%"REG_a", %1, 4), %%mm6         \n\t"
                "punpckhbw %%mm7, %%mm6                 \n\t" // H6
                "psubw %%mm6, %%mm3                     \n\t" // H5 - H6

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

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

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

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

                "movq (%%"REG_c"), %%mm2                \n\t" // 2L0 - 5L1 + 5L2 - 2L3
                "movq 8(%%"REG_c"), %%mm3               \n\t" // 2H0 - 5H1 + 5H2 - 2H3
Michael Niedermayer's avatar
Michael Niedermayer committed
1014 1015

#ifdef HAVE_MMX2
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
                "movq %%mm7, %%mm6                      \n\t" // 0
                "psubw %%mm0, %%mm6                     \n\t"
                "pmaxsw %%mm6, %%mm0                    \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
                "movq %%mm7, %%mm6                      \n\t" // 0
                "psubw %%mm1, %%mm6                     \n\t"
                "pmaxsw %%mm6, %%mm1                    \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
                "movq %%mm7, %%mm6                      \n\t" // 0
                "psubw %%mm2, %%mm6                     \n\t"
                "pmaxsw %%mm6, %%mm2                    \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
                "movq %%mm7, %%mm6                      \n\t" // 0
                "psubw %%mm3, %%mm6                     \n\t"
                "pmaxsw %%mm6, %%mm3                    \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
Michael Niedermayer's avatar
Michael Niedermayer committed
1028
#else
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
                "movq %%mm7, %%mm6                      \n\t" // 0
                "pcmpgtw %%mm0, %%mm6                   \n\t"
                "pxor %%mm6, %%mm0                      \n\t"
                "psubw %%mm6, %%mm0                     \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
                "movq %%mm7, %%mm6                      \n\t" // 0
                "pcmpgtw %%mm1, %%mm6                   \n\t"
                "pxor %%mm6, %%mm1                      \n\t"
                "psubw %%mm6, %%mm1                     \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
                "movq %%mm7, %%mm6                      \n\t" // 0
                "pcmpgtw %%mm2, %%mm6                   \n\t"
                "pxor %%mm6, %%mm2                      \n\t"
                "psubw %%mm6, %%mm2                     \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
                "movq %%mm7, %%mm6                      \n\t" // 0
                "pcmpgtw %%mm3, %%mm6                   \n\t"
                "pxor %%mm6, %%mm3                      \n\t"
                "psubw %%mm6, %%mm3                     \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
Michael Niedermayer's avatar
Michael Niedermayer committed
1045
#endif
1046 1047

#ifdef HAVE_MMX2
1048 1049
                "pminsw %%mm2, %%mm0                    \n\t"
                "pminsw %%mm3, %%mm1                    \n\t"
1050
#else
1051 1052 1053 1054 1055 1056
                "movq %%mm0, %%mm6                      \n\t"
                "psubusw %%mm2, %%mm6                   \n\t"
                "psubw %%mm6, %%mm0                     \n\t"
                "movq %%mm1, %%mm6                      \n\t"
                "psubusw %%mm3, %%mm6                   \n\t"
                "psubw %%mm6, %%mm1                     \n\t"
1057 1058
#endif

1059 1060
                "movd %2, %%mm2                         \n\t" // QP
                "punpcklbw %%mm7, %%mm2                 \n\t"
1061

1062 1063 1064 1065 1066 1067 1068
                "movq %%mm7, %%mm6                      \n\t" // 0
                "pcmpgtw %%mm4, %%mm6                   \n\t" // sign(2L2 - 5L3 + 5L4 - 2L5)
                "pxor %%mm6, %%mm4                      \n\t"
                "psubw %%mm6, %%mm4                     \n\t" // |2L2 - 5L3 + 5L4 - 2L5|
                "pcmpgtw %%mm5, %%mm7                   \n\t" // sign(2H2 - 5H3 + 5H4 - 2H5)
                "pxor %%mm7, %%mm5                      \n\t"
                "psubw %%mm7, %%mm5                     \n\t" // |2H2 - 5H3 + 5H4 - 2H5|
1069
// 100 opcodes
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
                "psllw $3, %%mm2                        \n\t" // 8QP
                "movq %%mm2, %%mm3                      \n\t" // 8QP
                "pcmpgtw %%mm4, %%mm2                   \n\t"
                "pcmpgtw %%mm5, %%mm3                   \n\t"
                "pand %%mm2, %%mm4                      \n\t"
                "pand %%mm3, %%mm5                      \n\t"


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


                "movq "MANGLE(w05)", %%mm2              \n\t" // 5
                "pmullw %%mm2, %%mm4                    \n\t"
                "pmullw %%mm2, %%mm5                    \n\t"
                "movq "MANGLE(w20)", %%mm2              \n\t" // 32
                "paddw %%mm2, %%mm4                     \n\t"
                "paddw %%mm2, %%mm5                     \n\t"
                "psrlw $6, %%mm4                        \n\t"
                "psrlw $6, %%mm5                        \n\t"

                "movq 16(%%"REG_c"), %%mm0              \n\t" // L3 - L4
                "movq 24(%%"REG_c"), %%mm1              \n\t" // H3 - H4

                "pxor %%mm2, %%mm2                      \n\t"
                "pxor %%mm3, %%mm3                      \n\t"

                "pcmpgtw %%mm0, %%mm2                   \n\t" // sign (L3-L4)
                "pcmpgtw %%mm1, %%mm3                   \n\t" // sign (H3-H4)
                "pxor %%mm2, %%mm0                      \n\t"
                "pxor %%mm3, %%mm1                      \n\t"
                "psubw %%mm2, %%mm0                     \n\t" // |L3-L4|
                "psubw %%mm3, %%mm1                     \n\t" // |H3-H4|
                "psrlw $1, %%mm0                        \n\t" // |L3 - L4|/2
                "psrlw $1, %%mm1                        \n\t" // |H3 - H4|/2

                "pxor %%mm6, %%mm2                      \n\t"
                "pxor %%mm7, %%mm3                      \n\t"
                "pand %%mm2, %%mm4                      \n\t"
                "pand %%mm3, %%mm5                      \n\t"
1110 1111

#ifdef HAVE_MMX2
1112 1113
                "pminsw %%mm0, %%mm4                    \n\t"
                "pminsw %%mm1, %%mm5                    \n\t"
1114
#else
1115 1116 1117 1118 1119 1120
                "movq %%mm4, %%mm2                      \n\t"
                "psubusw %%mm0, %%mm2                   \n\t"
                "psubw %%mm2, %%mm4                     \n\t"
                "movq %%mm5, %%mm2                      \n\t"
                "psubusw %%mm1, %%mm2                   \n\t"
                "psubw %%mm2, %%mm5                     \n\t"
1121
#endif
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
                "pxor %%mm6, %%mm4                      \n\t"
                "pxor %%mm7, %%mm5                      \n\t"
                "psubw %%mm6, %%mm4                     \n\t"
                "psubw %%mm7, %%mm5                     \n\t"
                "packsswb %%mm5, %%mm4                  \n\t"
                "movq (%0), %%mm0                       \n\t"
                "paddb   %%mm4, %%mm0                   \n\t"
                "movq %%mm0, (%0)                       \n\t"
                "movq (%0, %1), %%mm0                   \n\t"
                "psubb %%mm4, %%mm0                     \n\t"
                "movq %%mm0, (%0, %1)                   \n\t"

                : "+r" (src)
                : "r" ((long)stride), "m" (c->pQPb)
                : "%"REG_a, "%"REG_c
        );
1138
#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
        const int l1= stride;
        const int l2= stride + l1;
        const int l3= stride + l2;
        const int l4= stride + l3;
        const int l5= stride + l4;
        const int l6= stride + l5;
        const int l7= stride + l6;
        const int l8= stride + l7;
//        const int l9= stride + l8;
        int x;
        src+= stride*3;
        for(x=0; x<BLOCK_SIZE; x++)
        {
                const int middleEnergy= 5*(src[l5] - src[l4]) + 2*(src[l3] - src[l6]);
1153
                if(FFABS(middleEnergy) < 8*c->QP)
1154 1155 1156 1157 1158
                {
                        const int q=(src[l4] - src[l5])/2;
                        const int leftEnergy=  5*(src[l3] - src[l2]) + 2*(src[l1] - src[l4]);
                        const int rightEnergy= 5*(src[l7] - src[l6]) + 2*(src[l5] - src[l8]);

1159
                        int d= FFABS(middleEnergy) - FFMIN( FFABS(leftEnergy), FFABS(rightEnergy) );
1160
                        d= FFMAX(d, 0);
1161 1162

                        d= (5*d + 32) >> 6;
1163
                        d*= FFSIGN(-middleEnergy);
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

                        if(q>0)
                        {
                                d= d<0 ? 0 : d;
                                d= d>q ? q : d;
                        }
                        else
                        {
                                d= d>0 ? 0 : d;
                                d= d<q ? q : d;
                        }

                        src[l4]-= d;
                        src[l5]+= d;
                }
                src++;
        }
1181
#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1182
}
1183
#endif //HAVE_ALTIVEC
1184

1185
#ifndef HAVE_ALTIVEC
1186
static inline void RENAME(dering)(uint8_t src[], int stride, PPContext *c)
1187
{
Michael Niedermayer's avatar
Michael Niedermayer committed
1188
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
        asm volatile(
                "pxor %%mm6, %%mm6                      \n\t"
                "pcmpeqb %%mm7, %%mm7                   \n\t"
                "movq %2, %%mm0                         \n\t"
                "punpcklbw %%mm6, %%mm0                 \n\t"
                "psrlw $1, %%mm0                        \n\t"
                "psubw %%mm7, %%mm0                     \n\t"
                "packuswb %%mm0, %%mm0                  \n\t"
                "movq %%mm0, %3                         \n\t"

                "lea (%0, %1), %%"REG_a"                \n\t"
                "lea (%%"REG_a", %1, 4), %%"REG_d"      \n\t"

//        0        1        2        3        4        5        6        7        8        9
//        %0        eax        eax+%1        eax+2%1        %0+4%1        edx        edx+%1        edx+2%1        %0+8%1        edx+4%1
1204

1205
#undef FIND_MIN_MAX
Michael Niedermayer's avatar
Michael Niedermayer committed
1206
#ifdef HAVE_MMX2
1207
#define REAL_FIND_MIN_MAX(addr)\
1208 1209 1210
                "movq " #addr ", %%mm0                  \n\t"\
                "pminub %%mm0, %%mm7                    \n\t"\
                "pmaxub %%mm0, %%mm6                    \n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1211
#else
1212
#define REAL_FIND_MIN_MAX(addr)\
1213 1214 1215 1216 1217 1218
                "movq " #addr ", %%mm0                  \n\t"\
                "movq %%mm7, %%mm1                      \n\t"\
                "psubusb %%mm0, %%mm6                   \n\t"\
                "paddb %%mm0, %%mm6                     \n\t"\
                "psubusb %%mm0, %%mm1                   \n\t"\
                "psubb %%mm1, %%mm7                     \n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1219
#endif
1220
#define FIND_MIN_MAX(addr)  REAL_FIND_MIN_MAX(addr)
1221

1222 1223 1224
FIND_MIN_MAX((%%REGa))
FIND_MIN_MAX((%%REGa, %1))
FIND_MIN_MAX((%%REGa, %1, 2))
Michael Niedermayer's avatar
Michael Niedermayer committed
1225
FIND_MIN_MAX((%0, %1, 4))
1226 1227 1228
FIND_MIN_MAX((%%REGd))
FIND_MIN_MAX((%%REGd, %1))
FIND_MIN_MAX((%%REGd, %1, 2))
Michael Niedermayer's avatar
Michael Niedermayer committed
1229
FIND_MIN_MAX((%0, %1, 8))
1230

1231 1232
                "movq %%mm7, %%mm4                      \n\t"
                "psrlq $8, %%mm7                        \n\t"
1233
#ifdef HAVE_MMX2
1234 1235 1236 1237 1238
                "pminub %%mm4, %%mm7                    \n\t" // min of pixels
                "pshufw $0xF9, %%mm7, %%mm4             \n\t"
                "pminub %%mm4, %%mm7                    \n\t" // min of pixels
                "pshufw $0xFE, %%mm7, %%mm4             \n\t"
                "pminub %%mm4, %%mm7                    \n\t"
1239
#else
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
                "movq %%mm7, %%mm1                      \n\t"
                "psubusb %%mm4, %%mm1                   \n\t"
                "psubb %%mm1, %%mm7                     \n\t"
                "movq %%mm7, %%mm4                      \n\t"
                "psrlq $16, %%mm7                       \n\t"
                "movq %%mm7, %%mm1                      \n\t"
                "psubusb %%mm4, %%mm1                   \n\t"
                "psubb %%mm1, %%mm7                     \n\t"
                "movq %%mm7, %%mm4                      \n\t"
                "psrlq $32, %%mm7                       \n\t"
                "movq %%mm7, %%mm1                      \n\t"
                "psubusb %%mm4, %%mm1                   \n\t"
                "psubb %%mm1, %%mm7                     \n\t"
1253
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
1254 1255


1256 1257
                "movq %%mm6, %%mm4                      \n\t"
                "psrlq $8, %%mm6                        \n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1258
#ifdef HAVE_MMX2
1259 1260 1261 1262 1263
                "pmaxub %%mm4, %%mm6                    \n\t" // max of pixels
                "pshufw $0xF9, %%mm6, %%mm4             \n\t"
                "pmaxub %%mm4, %%mm6                    \n\t"
                "pshufw $0xFE, %%mm6, %%mm4             \n\t"
                "pmaxub %%mm4, %%mm6                    \n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1264
#else
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
                "psubusb %%mm4, %%mm6                   \n\t"
                "paddb %%mm4, %%mm6                     \n\t"
                "movq %%mm6, %%mm4                      \n\t"
                "psrlq $16, %%mm6                       \n\t"
                "psubusb %%mm4, %%mm6                   \n\t"
                "paddb %%mm4, %%mm6                     \n\t"
                "movq %%mm6, %%mm4                      \n\t"
                "psrlq $32, %%mm6                       \n\t"
                "psubusb %%mm4, %%mm6                   \n\t"
                "paddb %%mm4, %%mm6                     \n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1275
#endif
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
                "movq %%mm6, %%mm0                      \n\t" // max
                "psubb %%mm7, %%mm6                     \n\t" // max - min
                "movd %%mm6, %%ecx                      \n\t"
                "cmpb "MANGLE(deringThreshold)", %%cl   \n\t"
                " jb 1f                                 \n\t"
                "lea -24(%%"REG_SP"), %%"REG_c"         \n\t"
                "and "ALIGN_MASK", %%"REG_c"            \n\t"
                PAVGB(%%mm0, %%mm7)                           // a=(max + min)/2
                "punpcklbw %%mm7, %%mm7                 \n\t"
                "punpcklbw %%mm7, %%mm7                 \n\t"
                "punpcklbw %%mm7, %%mm7                 \n\t"
                "movq %%mm7, (%%"REG_c")                \n\t"

                "movq (%0), %%mm0                       \n\t" // L10
                "movq %%mm0, %%mm1                      \n\t" // L10
                "movq %%mm0, %%mm2                      \n\t" // L10
                "psllq $8, %%mm1                        \n\t"
                "psrlq $8, %%mm2                        \n\t"
                "movd -4(%0), %%mm3                     \n\t"
                "movd 8(%0), %%mm4                      \n\t"
                "psrlq $24, %%mm3                       \n\t"
                "psllq $56, %%mm4                       \n\t"
                "por %%mm3, %%mm1                       \n\t" // L00
                "por %%mm4, %%mm2                       \n\t" // L20
                "movq %%mm1, %%mm3                      \n\t" // L00
                PAVGB(%%mm2, %%mm1)                           // (L20 + L00)/2
                PAVGB(%%mm0, %%mm1)                           // (L20 + L00 + 2L10)/4
                "psubusb %%mm7, %%mm0                   \n\t"
                "psubusb %%mm7, %%mm2                   \n\t"
                "psubusb %%mm7, %%mm3                   \n\t"
                "pcmpeqb "MANGLE(b00)", %%mm0           \n\t" // L10 > a ? 0 : -1
                "pcmpeqb "MANGLE(b00)", %%mm2           \n\t" // L20 > a ? 0 : -1
                "pcmpeqb "MANGLE(b00)", %%mm3           \n\t" // L00 > a ? 0 : -1
                "paddb %%mm2, %%mm0                     \n\t"
                "paddb %%mm3, %%mm0                     \n\t"

                "movq (%%"REG_a"), %%mm2                \n\t" // L11
                "movq %%mm2, %%mm3                      \n\t" // L11
                "movq %%mm2, %%mm4                      \n\t" // L11
                "psllq $8, %%mm3                        \n\t"
                "psrlq $8, %%mm4                        \n\t"
                "movd -4(%%"REG_a"), %%mm5              \n\t"
                "movd 8(%%"REG_a"), %%mm6               \n\t"
                "psrlq $24, %%mm5                       \n\t"
                "psllq $56, %%mm6                       \n\t"
                "por %%mm5, %%mm3                       \n\t" // L01
                "por %%mm6, %%mm4                       \n\t" // L21
                "movq %%mm3, %%mm5                      \n\t" // L01
                PAVGB(%%mm4, %%mm3)                           // (L21 + L01)/2
                PAVGB(%%mm2, %%mm3)                           // (L21 + L01 + 2L11)/4
                "psubusb %%mm7, %%mm2                   \n\t"
                "psubusb %%mm7, %%mm4                   \n\t"
                "psubusb %%mm7, %%mm5                   \n\t"
                "pcmpeqb "MANGLE(b00)", %%mm2           \n\t" // L11 > a ? 0 : -1
                "pcmpeqb "MANGLE(b00)", %%mm4           \n\t" // L21 > a ? 0 : -1
                "pcmpeqb "MANGLE(b00)", %%mm5           \n\t" // L01 > a ? 0 : -1
                "paddb %%mm4, %%mm2                     \n\t"
                "paddb %%mm5, %%mm2                     \n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1334
// 0, 2, 3, 1
1335
#define REAL_DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1) \
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
                "movq " #src ", " #sx "                 \n\t" /* src[0] */\
                "movq " #sx ", " #lx "                  \n\t" /* src[0] */\
                "movq " #sx ", " #t0 "                  \n\t" /* src[0] */\
                "psllq $8, " #lx "                      \n\t"\
                "psrlq $8, " #t0 "                      \n\t"\
                "movd -4" #src ", " #t1 "               \n\t"\
                "psrlq $24, " #t1 "                     \n\t"\
                "por " #t1 ", " #lx "                   \n\t" /* src[-1] */\
                "movd 8" #src ", " #t1 "                \n\t"\
                "psllq $56, " #t1 "                     \n\t"\
                "por " #t1 ", " #t0 "                   \n\t" /* src[+1] */\
                "movq " #lx ", " #t1 "                  \n\t" /* src[-1] */\
                PAVGB(t0, lx)                                 /* (src[-1] + src[+1])/2 */\
                PAVGB(sx, lx)                                 /* (src[-1] + 2src[0] + src[+1])/4 */\
                PAVGB(lx, pplx)                                     \
                "movq " #lx ", 8(%%"REG_c")             \n\t"\
                "movq (%%"REG_c"), " #lx "              \n\t"\
                "psubusb " #lx ", " #t1 "               \n\t"\
                "psubusb " #lx ", " #t0 "               \n\t"\
                "psubusb " #lx ", " #sx "               \n\t"\
                "movq "MANGLE(b00)", " #lx "            \n\t"\
                "pcmpeqb " #lx ", " #t1 "               \n\t" /* src[-1] > a ? 0 : -1*/\
                "pcmpeqb " #lx ", " #t0 "               \n\t" /* src[+1] > a ? 0 : -1*/\
                "pcmpeqb " #lx ", " #sx "               \n\t" /* src[0]  > a ? 0 : -1*/\
                "paddb " #t1 ", " #t0 "                 \n\t"\
                "paddb " #t0 ", " #sx "                 \n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
1362
\
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
                PAVGB(plx, pplx)                              /* filtered */\
                "movq " #dst ", " #t0 "                 \n\t" /* dst */\
                "movq " #t0 ", " #t1 "                  \n\t" /* dst */\
                "psubusb %3, " #t0 "                    \n\t"\
                "paddusb %3, " #t1 "                    \n\t"\
                PMAXUB(t0, pplx)\
                PMINUB(t1, pplx, t0)\
                "paddb " #sx ", " #ppsx "               \n\t"\
                "paddb " #psx ", " #ppsx "              \n\t"\
                "#paddb "MANGLE(b02)", " #ppsx "        \n\t"\
                "pand "MANGLE(b08)", " #ppsx "          \n\t"\
                "pcmpeqb " #lx ", " #ppsx "             \n\t"\
                "pand " #ppsx ", " #pplx "              \n\t"\
                "pandn " #dst ", " #ppsx "              \n\t"\
                "por " #pplx ", " #ppsx "               \n\t"\
                "movq " #ppsx ", " #dst "               \n\t"\
                "movq 8(%%"REG_c"), " #lx "             \n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
1380

1381 1382
#define DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1) \
   REAL_DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1)
Michael Niedermayer's avatar
Michael Niedermayer committed
1383 1384 1385
/*
0000000
1111111
1386

Michael Niedermayer's avatar
Michael Niedermayer committed
1387 1388 1389 1390 1391 1392
1111110
1111101
1111100
1111011
1111010
1111001
1393

Michael Niedermayer's avatar
Michael Niedermayer committed
1394 1395
1111000
1110111
1396

Michael Niedermayer's avatar
Michael Niedermayer committed
1397
*/
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
//DERING_CORE(dst          ,src            ,ppsx ,psx  ,sx   ,pplx ,plx  ,lx   ,t0   ,t1)
DERING_CORE((%%REGa)       ,(%%REGa, %1)   ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
DERING_CORE((%%REGa, %1)   ,(%%REGa, %1, 2),%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
DERING_CORE((%%REGa, %1, 2),(%0, %1, 4)    ,%%mm4,%%mm0,%%mm2,%%mm5,%%mm1,%%mm3,%%mm6,%%mm7)
DERING_CORE((%0, %1, 4)    ,(%%REGd)       ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
DERING_CORE((%%REGd)       ,(%%REGd, %1)   ,%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
DERING_CORE((%%REGd, %1)   ,(%%REGd, %1, 2),%%mm4,%%mm0,%%mm2,%%mm5,%%mm1,%%mm3,%%mm6,%%mm7)
DERING_CORE((%%REGd, %1, 2),(%0, %1, 8)    ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
DERING_CORE((%0, %1, 8)    ,(%%REGd, %1, 4),%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)

                "1:                        \n\t"
                : : "r" (src), "r" ((long)stride), "m" (c->pQPb), "m"(c->pQPb2)
                : "%"REG_a, "%"REG_d, "%"REG_c
        );
1412
#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
        int y;
        int min=255;
        int max=0;
        int avg;
        uint8_t *p;
        int s[10];
        const int QP2= c->QP/2 + 1;

        for(y=1; y<9; y++)
        {
                int x;
                p= src + stride*y;
                for(x=1; x<9; x++)
                {
                        p++;
                        if(*p > max) max= *p;
                        if(*p < min) min= *p;
                }
        }
        avg= (min + max + 1)>>1;

        if(max - min <deringThreshold) return;

        for(y=0; y<10; y++)
        {
                int t = 0;

                if(src[stride*y + 0] > avg) t+= 1;
                if(src[stride*y + 1] > avg) t+= 2;
                if(src[stride*y + 2] > avg) t+= 4;
                if(src[stride*y + 3] > avg) t+= 8;
                if(src[stride*y + 4] > avg) t+= 16;
                if(src[stride*y + 5] > avg) t+= 32;
                if(src[stride*y + 6] > avg) t+= 64;
                if(src[stride*y + 7] > avg) t+= 128;
                if(src[stride*y + 8] > avg) t+= 256;
                if(src[stride*y + 9] > avg) t+= 512;

                t |= (~t)<<16;
                t &= (t<<1) & (t>>1);
                s[y] = t;
        }

        for(y=1; y<9; y++)
        {
                int t = s[y-1] & s[y] & s[y+1];
                t|= t>>16;
                s[y-1]= t;
        }

        for(y=1; y<9; y++)
        {
                int x;
                int t = s[y-1];

                p= src + stride*y;
                for(x=1; x<9; x++)
                {
                        p++;
                        if(t & (1<<x))
                        {
                                int f= (*(p-stride-1)) + 2*(*(p-stride)) + (*(p-stride+1))
                                      +2*(*(p     -1)) + 4*(*p         ) + 2*(*(p     +1))
                                      +(*(p+stride-1)) + 2*(*(p+stride)) + (*(p+stride+1));
                                f= (f + 8)>>4;
Michael Niedermayer's avatar
Michael Niedermayer committed
1478

Michael Niedermayer's avatar
Michael Niedermayer committed
1479
#ifdef DEBUG_DERING_THRESHOLD
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
                                asm volatile("emms\n\t":);
                                {
                                static long long numPixels=0;
                                if(x!=1 && x!=8 && y!=1 && y!=8) numPixels++;
//                                if((max-min)<20 || (max-min)*QP<200)
//                                if((max-min)*QP < 500)
//                                if(max-min<QP/2)
                                if(max-min < 20)
                                {
                                        static int numSkiped=0;
                                        static int errorSum=0;
                                        static int worstQP=0;
                                        static int worstRange=0;
                                        static int worstDiff=0;
                                        int diff= (f - *p);
1495
                                        int absDiff= FFABS(diff);
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
                                        int error= diff*diff;

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

                                        numSkiped++;
                                        if(absDiff > worstDiff)
                                        {
                                                worstDiff= absDiff;
                                                worstQP= QP;
                                                worstRange= max-min;
                                        }
                                        errorSum+= error;

                                        if(1024LL*1024LL*1024LL % numSkiped == 0)
                                        {
1511
                                                av_log(c, AV_LOG_INFO, "sum:%1.3f, skip:%d, wQP:%d, "
1512 1513 1514 1515 1516 1517
                                                        "wRange:%d, wDiff:%d, relSkip:%1.3f\n",
                                                        (float)errorSum/numSkiped, numSkiped, worstQP, worstRange,
                                                        worstDiff, (float)numSkiped/numPixels);
                                        }
                                }
                                }
Michael Niedermayer's avatar
Michael Niedermayer committed
1518
#endif
1519 1520 1521 1522 1523 1524
                                if     (*p + QP2 < f) *p= *p + QP2;
                                else if(*p - QP2 > f) *p= *p - QP2;
                                else *p=f;
                        }
                }
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
1525
#ifdef DEBUG_DERING_THRESHOLD
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
        if(max-min < 20)
        {
                for(y=1; y<9; y++)
                {
                        int x;
                        int t = 0;
                        p= src + stride*y;
                        for(x=1; x<9; x++)
                        {
                                p++;
1536
                                *p = FFMIN(*p + 20, 255);
1537 1538 1539 1540
                        }
                }
//                src[0] = src[7]=src[stride*7]=src[stride*7 + 7]=255;
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
1541
#endif
1542
#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1543
}
1544
#endif //HAVE_ALTIVEC
1545

1546
/**
Michael Niedermayer's avatar
Michael Niedermayer committed
1547
 * Deinterlaces the given block by linearly interpolating every second line.
Michael Niedermayer's avatar
Michael Niedermayer committed
1548 1549 1550
 * will be called for every 8x8 block and can read & write from line 4-15
 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1551
 */
1552
static inline void RENAME(deInterlaceInterpolateLinear)(uint8_t src[], int stride)
1553 1554
{
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
        src+= 4*stride;
        asm volatile(
                "lea (%0, %1), %%"REG_a"                \n\t"
                "lea (%%"REG_a", %1, 4), %%"REG_c"      \n\t"
//      0       1       2       3       4       5       6       7       8       9
//      %0      eax     eax+%1  eax+2%1 %0+4%1  ecx     ecx+%1  ecx+2%1 %0+8%1  ecx+4%1

                "movq (%0), %%mm0                       \n\t"
                "movq (%%"REG_a", %1), %%mm1            \n\t"
                PAVGB(%%mm1, %%mm0)
                "movq %%mm0, (%%"REG_a")                \n\t"
                "movq (%0, %1, 4), %%mm0                \n\t"
                PAVGB(%%mm0, %%mm1)
                "movq %%mm1, (%%"REG_a", %1, 2)         \n\t"
                "movq (%%"REG_c", %1), %%mm1            \n\t"
                PAVGB(%%mm1, %%mm0)
                "movq %%mm0, (%%"REG_c")                \n\t"
                "movq (%0, %1, 8), %%mm0                \n\t"
                PAVGB(%%mm0, %%mm1)
                "movq %%mm1, (%%"REG_c", %1, 2)         \n\t"

                : : "r" (src), "r" ((long)stride)
                : "%"REG_a, "%"REG_c
        );
1579
#else
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
        int a, b, x;
        src+= 4*stride;

        for(x=0; x<2; x++){
                a= *(uint32_t*)&src[stride*0];
                b= *(uint32_t*)&src[stride*2];
                *(uint32_t*)&src[stride*1]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
                a= *(uint32_t*)&src[stride*4];
                *(uint32_t*)&src[stride*3]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
                b= *(uint32_t*)&src[stride*6];
                *(uint32_t*)&src[stride*5]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
                a= *(uint32_t*)&src[stride*8];
                *(uint32_t*)&src[stride*7]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
                src += 4;
        }
1595 1596 1597 1598
#endif
}

/**
Michael Niedermayer's avatar
Michael Niedermayer committed
1599
 * Deinterlaces the given block by cubic interpolating every second line.
Michael Niedermayer's avatar
Michael Niedermayer committed
1600 1601 1602 1603
 * will be called for every 8x8 block and can read & write from line 4-15
 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
 * this filter will read lines 3-15 and write 7-13
1604
 */
1605
static inline void RENAME(deInterlaceInterpolateCubic)(uint8_t src[], int stride)
1606 1607
{
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1608 1609 1610 1611 1612 1613 1614 1615 1616
        src+= stride*3;
        asm volatile(
                "lea (%0, %1), %%"REG_a"                \n\t"
                "lea (%%"REG_a", %1, 4), %%"REG_d"      \n\t"
                "lea (%%"REG_d", %1, 4), %%"REG_c"      \n\t"
                "add %1, %%"REG_c"                      \n\t"
                "pxor %%mm7, %%mm7                      \n\t"
//      0       1       2       3       4       5       6       7       8       9       10
//      %0      eax     eax+%1  eax+2%1 %0+4%1  edx     edx+%1  edx+2%1 %0+8%1  edx+4%1 ecx
1617

1618
#define REAL_DEINT_CUBIC(a,b,c,d,e)\
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
                "movq " #a ", %%mm0                     \n\t"\
                "movq " #b ", %%mm1                     \n\t"\
                "movq " #d ", %%mm2                     \n\t"\
                "movq " #e ", %%mm3                     \n\t"\
                PAVGB(%%mm2, %%mm1)                             /* (b+d) /2 */\
                PAVGB(%%mm3, %%mm0)                             /* a(a+e) /2 */\
                "movq %%mm0, %%mm2                      \n\t"\
                "punpcklbw %%mm7, %%mm0                 \n\t"\
                "punpckhbw %%mm7, %%mm2                 \n\t"\
                "movq %%mm1, %%mm3                      \n\t"\
                "punpcklbw %%mm7, %%mm1                 \n\t"\
                "punpckhbw %%mm7, %%mm3                 \n\t"\
                "psubw %%mm1, %%mm0                     \n\t"   /* L(a+e - (b+d))/2 */\
                "psubw %%mm3, %%mm2                     \n\t"   /* H(a+e - (b+d))/2 */\
                "psraw $3, %%mm0                        \n\t"   /* L(a+e - (b+d))/16 */\
                "psraw $3, %%mm2                        \n\t"   /* H(a+e - (b+d))/16 */\
                "psubw %%mm0, %%mm1                     \n\t"   /* L(9b + 9d - a - e)/16 */\
                "psubw %%mm2, %%mm3                     \n\t"   /* H(9b + 9d - a - e)/16 */\
                "packuswb %%mm3, %%mm1                  \n\t"\
                "movq %%mm1, " #c "                     \n\t"
1639
#define DEINT_CUBIC(a,b,c,d,e)  REAL_DEINT_CUBIC(a,b,c,d,e)
1640

1641 1642 1643 1644
DEINT_CUBIC((%0)        , (%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4) , (%%REGd, %1))
DEINT_CUBIC((%%REGa, %1), (%0, %1, 4) , (%%REGd)       , (%%REGd, %1), (%0, %1, 8))
DEINT_CUBIC((%0, %1, 4) , (%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8) , (%%REGc))
DEINT_CUBIC((%%REGd, %1), (%0, %1, 8) , (%%REGd, %1, 4), (%%REGc)    , (%%REGc, %1, 2))
1645

1646 1647 1648
                : : "r" (src), "r" ((long)stride)
                : "%"REG_a, "%"REG_d, "%"REG_c
        );
1649
#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
        int x;
        src+= stride*3;
        for(x=0; x<8; x++)
        {
                src[stride*3] = CLIP((-src[0]        + 9*src[stride*2] + 9*src[stride*4] - src[stride*6])>>4);
                src[stride*5] = CLIP((-src[stride*2] + 9*src[stride*4] + 9*src[stride*6] - src[stride*8])>>4);
                src[stride*7] = CLIP((-src[stride*4] + 9*src[stride*6] + 9*src[stride*8] - src[stride*10])>>4);
                src[stride*9] = CLIP((-src[stride*6] + 9*src[stride*8] + 9*src[stride*10] - src[stride*12])>>4);
                src++;
        }
1660
#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1661 1662
}

1663
/**
Michael Niedermayer's avatar
Michael Niedermayer committed
1664
 * Deinterlaces the given block by filtering every second line with a (-1 4 2 4 -1) filter.
1665 1666 1667 1668 1669 1670 1671 1672
 * will be called for every 8x8 block and can read & write from line 4-15
 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
 * this filter will read lines 4-13 and write 5-11
 */
static inline void RENAME(deInterlaceFF)(uint8_t src[], int stride, uint8_t *tmp)
{
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1673 1674 1675 1676 1677 1678 1679 1680
        src+= stride*4;
        asm volatile(
                "lea (%0, %1), %%"REG_a"                \n\t"
                "lea (%%"REG_a", %1, 4), %%"REG_d"      \n\t"
                "pxor %%mm7, %%mm7                      \n\t"
                "movq (%2), %%mm0                       \n\t"
//      0       1       2       3       4       5       6       7       8       9       10
//      %0      eax     eax+%1  eax+2%1 %0+4%1  edx     edx+%1  edx+2%1 %0+8%1  edx+4%1 ecx
1681

1682
#define REAL_DEINT_FF(a,b,c,d)\
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
                "movq " #a ", %%mm1                     \n\t"\
                "movq " #b ", %%mm2                     \n\t"\
                "movq " #c ", %%mm3                     \n\t"\
                "movq " #d ", %%mm4                     \n\t"\
                PAVGB(%%mm3, %%mm1)                          \
                PAVGB(%%mm4, %%mm0)                          \
                "movq %%mm0, %%mm3                      \n\t"\
                "punpcklbw %%mm7, %%mm0                 \n\t"\
                "punpckhbw %%mm7, %%mm3                 \n\t"\
                "movq %%mm1, %%mm4                      \n\t"\
                "punpcklbw %%mm7, %%mm1                 \n\t"\
                "punpckhbw %%mm7, %%mm4                 \n\t"\
                "psllw $2, %%mm1                        \n\t"\
                "psllw $2, %%mm4                        \n\t"\
                "psubw %%mm0, %%mm1                     \n\t"\
                "psubw %%mm3, %%mm4                     \n\t"\
                "movq %%mm2, %%mm5                      \n\t"\
                "movq %%mm2, %%mm0                      \n\t"\
                "punpcklbw %%mm7, %%mm2                 \n\t"\
                "punpckhbw %%mm7, %%mm5                 \n\t"\
                "paddw %%mm2, %%mm1                     \n\t"\
                "paddw %%mm5, %%mm4                     \n\t"\
                "psraw $2, %%mm1                        \n\t"\
                "psraw $2, %%mm4                        \n\t"\
                "packuswb %%mm4, %%mm1                  \n\t"\
                "movq %%mm1, " #b "                     \n\t"\
1709

1710 1711
#define DEINT_FF(a,b,c,d)  REAL_DEINT_FF(a,b,c,d)

1712 1713 1714 1715
DEINT_FF((%0)        , (%%REGa)       , (%%REGa, %1), (%%REGa, %1, 2))
DEINT_FF((%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4) , (%%REGd)       )
DEINT_FF((%0, %1, 4) , (%%REGd)       , (%%REGd, %1), (%%REGd, %1, 2))
DEINT_FF((%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8) , (%%REGd, %1, 4))
1716

1717 1718 1719 1720
                "movq %%mm0, (%2)                       \n\t"
                : : "r" (src), "r" ((long)stride), "r"(tmp)
                : "%"REG_a, "%"REG_d
        );
1721
#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
        int x;
        src+= stride*4;
        for(x=0; x<8; x++)
        {
                int t1= tmp[x];
                int t2= src[stride*1];

                src[stride*1]= CLIP((-t1 + 4*src[stride*0] + 2*t2 + 4*src[stride*2] - src[stride*3] + 4)>>3);
                t1= src[stride*4];
                src[stride*3]= CLIP((-t2 + 4*src[stride*2] + 2*t1 + 4*src[stride*4] - src[stride*5] + 4)>>3);
                t2= src[stride*6];
                src[stride*5]= CLIP((-t1 + 4*src[stride*4] + 2*t2 + 4*src[stride*6] - src[stride*7] + 4)>>3);
                t1= src[stride*8];
                src[stride*7]= CLIP((-t2 + 4*src[stride*6] + 2*t1 + 4*src[stride*8] - src[stride*9] + 4)>>3);
                tmp[x]= t1;

                src++;
        }
1740
#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1741 1742
}

1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
/**
 * Deinterlaces the given block by filtering every line with a (-1 2 6 2 -1) filter.
 * will be called for every 8x8 block and can read & write from line 4-15
 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
 * this filter will read lines 4-13 and write 4-11
 */
static inline void RENAME(deInterlaceL5)(uint8_t src[], int stride, uint8_t *tmp, uint8_t *tmp2)
{
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1753 1754 1755 1756 1757 1758 1759 1760 1761
        src+= stride*4;
        asm volatile(
                "lea (%0, %1), %%"REG_a"                \n\t"
                "lea (%%"REG_a", %1, 4), %%"REG_d"      \n\t"
                "pxor %%mm7, %%mm7                      \n\t"
                "movq (%2), %%mm0                       \n\t"
                "movq (%3), %%mm1                       \n\t"
//      0       1       2       3       4       5       6       7       8       9       10
//      %0      eax     eax+%1  eax+2%1 %0+4%1  edx     edx+%1  edx+2%1 %0+8%1  edx+4%1 ecx
1762

1763
#define REAL_DEINT_L5(t1,t2,a,b,c)\
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
                "movq " #a ", %%mm2                     \n\t"\
                "movq " #b ", %%mm3                     \n\t"\
                "movq " #c ", %%mm4                     \n\t"\
                PAVGB(t2, %%mm3)                             \
                PAVGB(t1, %%mm4)                             \
                "movq %%mm2, %%mm5                      \n\t"\
                "movq %%mm2, " #t1 "                    \n\t"\
                "punpcklbw %%mm7, %%mm2                 \n\t"\
                "punpckhbw %%mm7, %%mm5                 \n\t"\
                "movq %%mm2, %%mm6                      \n\t"\
                "paddw %%mm2, %%mm2                     \n\t"\
                "paddw %%mm6, %%mm2                     \n\t"\
                "movq %%mm5, %%mm6                      \n\t"\
                "paddw %%mm5, %%mm5                     \n\t"\
                "paddw %%mm6, %%mm5                     \n\t"\
                "movq %%mm3, %%mm6                      \n\t"\
                "punpcklbw %%mm7, %%mm3                 \n\t"\
                "punpckhbw %%mm7, %%mm6                 \n\t"\
                "paddw %%mm3, %%mm3                     \n\t"\
                "paddw %%mm6, %%mm6                     \n\t"\
                "paddw %%mm3, %%mm2                     \n\t"\
                "paddw %%mm6, %%mm5                     \n\t"\
                "movq %%mm4, %%mm6                      \n\t"\
                "punpcklbw %%mm7, %%mm4                 \n\t"\
                "punpckhbw %%mm7, %%mm6                 \n\t"\
                "psubw %%mm4, %%mm2                     \n\t"\
                "psubw %%mm6, %%mm5                     \n\t"\
                "psraw $2, %%mm2                        \n\t"\
                "psraw $2, %%mm5                        \n\t"\
                "packuswb %%mm5, %%mm2                  \n\t"\
                "movq %%mm2, " #a "                     \n\t"\
1795

1796 1797 1798 1799 1800 1801
#define DEINT_L5(t1,t2,a,b,c)  REAL_DEINT_L5(t1,t2,a,b,c)

DEINT_L5(%%mm0, %%mm1, (%0)           , (%%REGa)       , (%%REGa, %1)   )
DEINT_L5(%%mm1, %%mm0, (%%REGa)       , (%%REGa, %1)   , (%%REGa, %1, 2))
DEINT_L5(%%mm0, %%mm1, (%%REGa, %1)   , (%%REGa, %1, 2), (%0, %1, 4)   )
DEINT_L5(%%mm1, %%mm0, (%%REGa, %1, 2), (%0, %1, 4)    , (%%REGd)       )
1802
DEINT_L5(%%mm0, %%mm1, (%0, %1, 4)    , (%%REGd)       , (%%REGd, %1)   )
1803 1804 1805
DEINT_L5(%%mm1, %%mm0, (%%REGd)       , (%%REGd, %1)   , (%%REGd, %1, 2))
DEINT_L5(%%mm0, %%mm1, (%%REGd, %1)   , (%%REGd, %1, 2), (%0, %1, 8)   )
DEINT_L5(%%mm1, %%mm0, (%%REGd, %1, 2), (%0, %1, 8)    , (%%REGd, %1, 4))
1806

1807 1808 1809 1810 1811
                "movq %%mm0, (%2)                       \n\t"
                "movq %%mm1, (%3)                       \n\t"
                : : "r" (src), "r" ((long)stride), "r"(tmp), "r"(tmp2)
                : "%"REG_a, "%"REG_d
        );
1812
#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
        int x;
        src+= stride*4;
        for(x=0; x<8; x++)
        {
                int t1= tmp[x];
                int t2= tmp2[x];
                int t3= src[0];

                src[stride*0]= CLIP((-(t1 + src[stride*2]) + 2*(t2 + src[stride*1]) + 6*t3 + 4)>>3);
                t1= src[stride*1];
                src[stride*1]= CLIP((-(t2 + src[stride*3]) + 2*(t3 + src[stride*2]) + 6*t1 + 4)>>3);
                t2= src[stride*2];
                src[stride*2]= CLIP((-(t3 + src[stride*4]) + 2*(t1 + src[stride*3]) + 6*t2 + 4)>>3);
                t3= src[stride*3];
                src[stride*3]= CLIP((-(t1 + src[stride*5]) + 2*(t2 + src[stride*4]) + 6*t3 + 4)>>3);
                t1= src[stride*4];
                src[stride*4]= CLIP((-(t2 + src[stride*6]) + 2*(t3 + src[stride*5]) + 6*t1 + 4)>>3);
                t2= src[stride*5];
                src[stride*5]= CLIP((-(t3 + src[stride*7]) + 2*(t1 + src[stride*6]) + 6*t2 + 4)>>3);
                t3= src[stride*6];
                src[stride*6]= CLIP((-(t1 + src[stride*8]) + 2*(t2 + src[stride*7]) + 6*t3 + 4)>>3);
                t1= src[stride*7];
                src[stride*7]= CLIP((-(t2 + src[stride*9]) + 2*(t3 + src[stride*8]) + 6*t1 + 4)>>3);

                tmp[x]= t3;
                tmp2[x]= t1;

                src++;
        }
1842
#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1843 1844
}

1845
/**
Michael Niedermayer's avatar
Michael Niedermayer committed
1846
 * Deinterlaces the given block by filtering all lines with a (1 2 1) filter.
Michael Niedermayer's avatar
Michael Niedermayer committed
1847 1848 1849 1850
 * will be called for every 8x8 block and can read & write from line 4-15
 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
 * this filter will read lines 4-13 and write 4-11
1851
 */
1852
static inline void RENAME(deInterlaceBlendLinear)(uint8_t src[], int stride, uint8_t *tmp)
1853 1854
{
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
        src+= 4*stride;
        asm volatile(
                "lea (%0, %1), %%"REG_a"                \n\t"
                "lea (%%"REG_a", %1, 4), %%"REG_d"      \n\t"
//      0       1       2       3       4       5       6       7       8       9
//      %0      eax     eax+%1  eax+2%1 %0+4%1  edx     edx+%1  edx+2%1 %0+8%1  edx+4%1

                "movq (%2), %%mm0                       \n\t" // L0
                "movq (%%"REG_a"), %%mm1                \n\t" // L2
                PAVGB(%%mm1, %%mm0)                           // L0+L2
                "movq (%0), %%mm2                       \n\t" // L1
                PAVGB(%%mm2, %%mm0)
                "movq %%mm0, (%0)                       \n\t"
                "movq (%%"REG_a", %1), %%mm0            \n\t" // L3
                PAVGB(%%mm0, %%mm2)                           // L1+L3
                PAVGB(%%mm1, %%mm2)                           // 2L2 + L1 + L3
                "movq %%mm2, (%%"REG_a")                \n\t"
                "movq (%%"REG_a", %1, 2), %%mm2         \n\t" // L4
                PAVGB(%%mm2, %%mm1)                           // L2+L4
                PAVGB(%%mm0, %%mm1)                           // 2L3 + L2 + L4
                "movq %%mm1, (%%"REG_a", %1)            \n\t"
                "movq (%0, %1, 4), %%mm1                \n\t" // L5
                PAVGB(%%mm1, %%mm0)                           // L3+L5
                PAVGB(%%mm2, %%mm0)                           // 2L4 + L3 + L5
                "movq %%mm0, (%%"REG_a", %1, 2)         \n\t"
                "movq (%%"REG_d"), %%mm0                \n\t" // L6
                PAVGB(%%mm0, %%mm2)                           // L4+L6
                PAVGB(%%mm1, %%mm2)                           // 2L5 + L4 + L6
                "movq %%mm2, (%0, %1, 4)                \n\t"
                "movq (%%"REG_d", %1), %%mm2            \n\t" // L7
                PAVGB(%%mm2, %%mm1)                           // L5+L7
                PAVGB(%%mm0, %%mm1)                           // 2L6 + L5 + L7
                "movq %%mm1, (%%"REG_d")                \n\t"
                "movq (%%"REG_d", %1, 2), %%mm1         \n\t" // L8
                PAVGB(%%mm1, %%mm0)                           // L6+L8
                PAVGB(%%mm2, %%mm0)                           // 2L7 + L6 + L8
                "movq %%mm0, (%%"REG_d", %1)            \n\t"
                "movq (%0, %1, 8), %%mm0                \n\t" // L9
                PAVGB(%%mm0, %%mm2)                           // L7+L9
                PAVGB(%%mm1, %%mm2)                           // 2L8 + L7 + L9
                "movq %%mm2, (%%"REG_d", %1, 2)         \n\t"
                "movq %%mm1, (%2)                       \n\t"

                : : "r" (src), "r" ((long)stride), "r" (tmp)
                : "%"REG_a, "%"REG_d
        );
1901
#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
        int a, b, c, x;
        src+= 4*stride;

        for(x=0; x<2; x++){
                a= *(uint32_t*)&tmp[stride*0];
                b= *(uint32_t*)&src[stride*0];
                c= *(uint32_t*)&src[stride*1];
                a= (a&c) + (((a^c)&0xFEFEFEFEUL)>>1);
                *(uint32_t*)&src[stride*0]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);

                a= *(uint32_t*)&src[stride*2];
                b= (a&b) + (((a^b)&0xFEFEFEFEUL)>>1);
                *(uint32_t*)&src[stride*1]= (c|b) - (((c^b)&0xFEFEFEFEUL)>>1);

                b= *(uint32_t*)&src[stride*3];
                c= (b&c) + (((b^c)&0xFEFEFEFEUL)>>1);
                *(uint32_t*)&src[stride*2]= (c|a) - (((c^a)&0xFEFEFEFEUL)>>1);

                c= *(uint32_t*)&src[stride*4];
                a= (a&c) + (((a^c)&0xFEFEFEFEUL)>>1);
                *(uint32_t*)&src[stride*3]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);

                a= *(uint32_t*)&src[stride*5];
                b= (a&b) + (((a^b)&0xFEFEFEFEUL)>>1);
                *(uint32_t*)&src[stride*4]= (c|b) - (((c^b)&0xFEFEFEFEUL)>>1);

                b= *(uint32_t*)&src[stride*6];
                c= (b&c) + (((b^c)&0xFEFEFEFEUL)>>1);
                *(uint32_t*)&src[stride*5]= (c|a) - (((c^a)&0xFEFEFEFEUL)>>1);

                c= *(uint32_t*)&src[stride*7];
                a= (a&c) + (((a^c)&0xFEFEFEFEUL)>>1);
                *(uint32_t*)&src[stride*6]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);

                a= *(uint32_t*)&src[stride*8];
                b= (a&b) + (((a^b)&0xFEFEFEFEUL)>>1);
                *(uint32_t*)&src[stride*7]= (c|b) - (((c^b)&0xFEFEFEFEUL)>>1);

                *(uint32_t*)&tmp[stride*0]= c;
                src += 4;
                tmp += 4;
        }
1944
#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
1945 1946 1947
}

/**
Michael Niedermayer's avatar
Michael Niedermayer committed
1948
 * Deinterlaces the given block by applying a median filter to every second line.
Michael Niedermayer's avatar
Michael Niedermayer committed
1949 1950 1951
 * will be called for every 8x8 block and can read & write from line 4-15,
 * lines 0-3 have been passed through the deblock / dering filters allready, but can be read too
 * lines 4-12 will be read into the deblocking filter and should be deinterlaced
1952
 */
1953
static inline void RENAME(deInterlaceMedian)(uint8_t src[], int stride)
1954
{
Michael Niedermayer's avatar
Michael Niedermayer committed
1955
#ifdef HAVE_MMX
1956
        src+= 4*stride;
Michael Niedermayer's avatar
Michael Niedermayer committed
1957
#ifdef HAVE_MMX2
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
        asm volatile(
                "lea (%0, %1), %%"REG_a"                \n\t"
                "lea (%%"REG_a", %1, 4), %%"REG_d"      \n\t"
//      0       1       2       3       4       5       6       7       8       9
//      %0      eax     eax+%1  eax+2%1 %0+4%1  edx     edx+%1  edx+2%1 %0+8%1  edx+4%1

                "movq (%0), %%mm0                       \n\t" //
                "movq (%%"REG_a", %1), %%mm2            \n\t" //
                "movq (%%"REG_a"), %%mm1                \n\t" //
                "movq %%mm0, %%mm3                      \n\t"
                "pmaxub %%mm1, %%mm0                    \n\t" //
                "pminub %%mm3, %%mm1                    \n\t" //
                "pmaxub %%mm2, %%mm1                    \n\t" //
                "pminub %%mm1, %%mm0                    \n\t"
                "movq %%mm0, (%%"REG_a")                \n\t"

                "movq (%0, %1, 4), %%mm0                \n\t" //
                "movq (%%"REG_a", %1, 2), %%mm1         \n\t" //
                "movq %%mm2, %%mm3                      \n\t"
                "pmaxub %%mm1, %%mm2                    \n\t" //
                "pminub %%mm3, %%mm1                    \n\t" //
                "pmaxub %%mm0, %%mm1                    \n\t" //
                "pminub %%mm1, %%mm2                    \n\t"
                "movq %%mm2, (%%"REG_a", %1, 2)         \n\t"

                "movq (%%"REG_d"), %%mm2                \n\t" //
                "movq (%%"REG_d", %1), %%mm1            \n\t" //
                "movq %%mm2, %%mm3                      \n\t"
                "pmaxub %%mm0, %%mm2                    \n\t" //
                "pminub %%mm3, %%mm0                    \n\t" //
                "pmaxub %%mm1, %%mm0                    \n\t" //
                "pminub %%mm0, %%mm2                    \n\t"
                "movq %%mm2, (%%"REG_d")                \n\t"

                "movq (%%"REG_d", %1, 2), %%mm2         \n\t" //
                "movq (%0, %1, 8), %%mm0                \n\t" //
                "movq %%mm2, %%mm3                      \n\t"
                "pmaxub %%mm0, %%mm2                    \n\t" //
                "pminub %%mm3, %%mm0                    \n\t" //
                "pmaxub %%mm1, %%mm0                    \n\t" //
                "pminub %%mm0, %%mm2                    \n\t"
                "movq %%mm2, (%%"REG_d", %1, 2)         \n\t"


                : : "r" (src), "r" ((long)stride)
                : "%"REG_a, "%"REG_d
        );
Michael Niedermayer's avatar
Michael Niedermayer committed
2005 2006

#else // MMX without MMX2
2007 2008 2009 2010 2011 2012
        asm volatile(
                "lea (%0, %1), %%"REG_a"                \n\t"
                "lea (%%"REG_a", %1, 4), %%"REG_d"      \n\t"
//      0       1       2       3       4       5       6       7       8       9
//      %0      eax     eax+%1  eax+2%1 %0+4%1  edx     edx+%1  edx+2%1 %0+8%1  edx+4%1
                "pxor %%mm7, %%mm7                      \n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
2013

2014
#define REAL_MEDIAN(a,b,c)\
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
                "movq " #a ", %%mm0                     \n\t"\
                "movq " #b ", %%mm2                     \n\t"\
                "movq " #c ", %%mm1                     \n\t"\
                "movq %%mm0, %%mm3                      \n\t"\
                "movq %%mm1, %%mm4                      \n\t"\
                "movq %%mm2, %%mm5                      \n\t"\
                "psubusb %%mm1, %%mm3                   \n\t"\
                "psubusb %%mm2, %%mm4                   \n\t"\
                "psubusb %%mm0, %%mm5                   \n\t"\
                "pcmpeqb %%mm7, %%mm3                   \n\t"\
                "pcmpeqb %%mm7, %%mm4                   \n\t"\
                "pcmpeqb %%mm7, %%mm5                   \n\t"\
                "movq %%mm3, %%mm6                      \n\t"\
                "pxor %%mm4, %%mm3                      \n\t"\
                "pxor %%mm5, %%mm4                      \n\t"\
                "pxor %%mm6, %%mm5                      \n\t"\
                "por %%mm3, %%mm1                       \n\t"\
                "por %%mm4, %%mm2                       \n\t"\
                "por %%mm5, %%mm0                       \n\t"\
                "pand %%mm2, %%mm0                      \n\t"\
                "pand %%mm1, %%mm0                      \n\t"\
                "movq %%mm0, " #b "                     \n\t"
2037
#define MEDIAN(a,b,c)  REAL_MEDIAN(a,b,c)
Michael Niedermayer's avatar
Michael Niedermayer committed
2038

2039
MEDIAN((%0)        , (%%REGa)       , (%%REGa, %1))
2040
MEDIAN((%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4))
2041
MEDIAN((%0, %1, 4) , (%%REGd)       , (%%REGd, %1))
2042
MEDIAN((%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8))
Michael Niedermayer's avatar
Michael Niedermayer committed
2043

2044 2045 2046
                : : "r" (src), "r" ((long)stride)
                : "%"REG_a, "%"REG_d
        );
2047 2048
#endif //HAVE_MMX2
#else //HAVE_MMX
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
        int x, y;
        src+= 4*stride;
        // FIXME - there should be a way to do a few columns in parallel like w/mmx
        for(x=0; x<8; x++)
        {
                uint8_t *colsrc = src;
                for (y=0; y<4; y++)
                {
                        int a, b, c, d, e, f;
                        a = colsrc[0       ];
                        b = colsrc[stride  ];
                        c = colsrc[stride*2];
                        d = (a-b)>>31;
                        e = (b-c)>>31;
                        f = (c-a)>>31;
                        colsrc[stride  ] = (a|(d^f)) & (b|(d^e)) & (c|(e^f));
                        colsrc += stride*2;
                }
                src++;
        }
2069
#endif //HAVE_MMX
2070 2071
}

2072
#ifdef HAVE_MMX
2073 2074 2075
/**
 * transposes and shift the given 8x8 Block into dst1 and dst2
 */
2076
static inline void RENAME(transpose1)(uint8_t *dst1, uint8_t *dst2, uint8_t *src, int srcStride)
2077
{
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
        asm(
                "lea (%0, %1), %%"REG_a"                \n\t"
//      0       1       2       3       4       5       6       7       8       9
//      %0      eax     eax+%1  eax+2%1 %0+4%1  edx     edx+%1  edx+2%1 %0+8%1  edx+4%1
                "movq (%0), %%mm0                       \n\t" // 12345678
                "movq (%%"REG_a"), %%mm1                \n\t" // abcdefgh
                "movq %%mm0, %%mm2                      \n\t" // 12345678
                "punpcklbw %%mm1, %%mm0                 \n\t" // 1a2b3c4d
                "punpckhbw %%mm1, %%mm2                 \n\t" // 5e6f7g8h

                "movq (%%"REG_a", %1), %%mm1            \n\t"
                "movq (%%"REG_a", %1, 2), %%mm3         \n\t"
                "movq %%mm1, %%mm4                      \n\t"
                "punpcklbw %%mm3, %%mm1                 \n\t"
                "punpckhbw %%mm3, %%mm4                 \n\t"

                "movq %%mm0, %%mm3                      \n\t"
                "punpcklwd %%mm1, %%mm0                 \n\t"
                "punpckhwd %%mm1, %%mm3                 \n\t"
                "movq %%mm2, %%mm1                      \n\t"
                "punpcklwd %%mm4, %%mm2                 \n\t"
                "punpckhwd %%mm4, %%mm1                 \n\t"

                "movd %%mm0, 128(%2)                    \n\t"
                "psrlq $32, %%mm0                       \n\t"
                "movd %%mm0, 144(%2)                    \n\t"
                "movd %%mm3, 160(%2)                    \n\t"
                "psrlq $32, %%mm3                       \n\t"
                "movd %%mm3, 176(%2)                    \n\t"
                "movd %%mm3, 48(%3)                     \n\t"
                "movd %%mm2, 192(%2)                    \n\t"
                "movd %%mm2, 64(%3)                     \n\t"
                "psrlq $32, %%mm2                       \n\t"
                "movd %%mm2, 80(%3)                     \n\t"
                "movd %%mm1, 96(%3)                     \n\t"
                "psrlq $32, %%mm1                       \n\t"
                "movd %%mm1, 112(%3)                    \n\t"

                "lea (%%"REG_a", %1, 4), %%"REG_a"      \n\t"

                "movq (%0, %1, 4), %%mm0                \n\t" // 12345678
                "movq (%%"REG_a"), %%mm1                \n\t" // abcdefgh
                "movq %%mm0, %%mm2                      \n\t" // 12345678
                "punpcklbw %%mm1, %%mm0                 \n\t" // 1a2b3c4d
                "punpckhbw %%mm1, %%mm2                 \n\t" // 5e6f7g8h

                "movq (%%"REG_a", %1), %%mm1            \n\t"
                "movq (%%"REG_a", %1, 2), %%mm3         \n\t"
                "movq %%mm1, %%mm4                      \n\t"
                "punpcklbw %%mm3, %%mm1                 \n\t"
                "punpckhbw %%mm3, %%mm4                 \n\t"

                "movq %%mm0, %%mm3                      \n\t"
                "punpcklwd %%mm1, %%mm0                 \n\t"
                "punpckhwd %%mm1, %%mm3                 \n\t"
                "movq %%mm2, %%mm1                      \n\t"
                "punpcklwd %%mm4, %%mm2                 \n\t"
                "punpckhwd %%mm4, %%mm1                 \n\t"

                "movd %%mm0, 132(%2)                    \n\t"
                "psrlq $32, %%mm0                       \n\t"
                "movd %%mm0, 148(%2)                    \n\t"
                "movd %%mm3, 164(%2)                    \n\t"
                "psrlq $32, %%mm3                       \n\t"
                "movd %%mm3, 180(%2)                    \n\t"
                "movd %%mm3, 52(%3)                     \n\t"
                "movd %%mm2, 196(%2)                    \n\t"
                "movd %%mm2, 68(%3)                     \n\t"
                "psrlq $32, %%mm2                       \n\t"
                "movd %%mm2, 84(%3)                     \n\t"
                "movd %%mm1, 100(%3)                    \n\t"
                "psrlq $32, %%mm1                       \n\t"
                "movd %%mm1, 116(%3)                    \n\t"


        :: "r" (src), "r" ((long)srcStride), "r" (dst1), "r" (dst2)
        : "%"REG_a
        );
2156 2157 2158 2159 2160
}

/**
 * transposes the given 8x8 block
 */
2161
static inline void RENAME(transpose2)(uint8_t *dst, int dstStride, uint8_t *src)
2162
{
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235
        asm(
                "lea (%0, %1), %%"REG_a"                \n\t"
                "lea (%%"REG_a",%1,4), %%"REG_d"        \n\t"
//      0       1       2       3       4       5       6       7       8       9
//      %0      eax     eax+%1  eax+2%1 %0+4%1  edx     edx+%1  edx+2%1 %0+8%1  edx+4%1
                "movq (%2), %%mm0                       \n\t" // 12345678
                "movq 16(%2), %%mm1                     \n\t" // abcdefgh
                "movq %%mm0, %%mm2                      \n\t" // 12345678
                "punpcklbw %%mm1, %%mm0                 \n\t" // 1a2b3c4d
                "punpckhbw %%mm1, %%mm2                 \n\t" // 5e6f7g8h

                "movq 32(%2), %%mm1                     \n\t"
                "movq 48(%2), %%mm3                     \n\t"
                "movq %%mm1, %%mm4                      \n\t"
                "punpcklbw %%mm3, %%mm1                 \n\t"
                "punpckhbw %%mm3, %%mm4                 \n\t"

                "movq %%mm0, %%mm3                      \n\t"
                "punpcklwd %%mm1, %%mm0                 \n\t"
                "punpckhwd %%mm1, %%mm3                 \n\t"
                "movq %%mm2, %%mm1                      \n\t"
                "punpcklwd %%mm4, %%mm2                 \n\t"
                "punpckhwd %%mm4, %%mm1                 \n\t"

                "movd %%mm0, (%0)                       \n\t"
                "psrlq $32, %%mm0                       \n\t"
                "movd %%mm0, (%%"REG_a")                \n\t"
                "movd %%mm3, (%%"REG_a", %1)            \n\t"
                "psrlq $32, %%mm3                       \n\t"
                "movd %%mm3, (%%"REG_a", %1, 2)         \n\t"
                "movd %%mm2, (%0, %1, 4)                \n\t"
                "psrlq $32, %%mm2                       \n\t"
                "movd %%mm2, (%%"REG_d")                \n\t"
                "movd %%mm1, (%%"REG_d", %1)            \n\t"
                "psrlq $32, %%mm1                       \n\t"
                "movd %%mm1, (%%"REG_d", %1, 2)         \n\t"


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

                "movq 96(%2), %%mm1                     \n\t"
                "movq 112(%2), %%mm3                    \n\t"
                "movq %%mm1, %%mm4                      \n\t"
                "punpcklbw %%mm3, %%mm1                 \n\t"
                "punpckhbw %%mm3, %%mm4                 \n\t"

                "movq %%mm0, %%mm3                      \n\t"
                "punpcklwd %%mm1, %%mm0                 \n\t"
                "punpckhwd %%mm1, %%mm3                 \n\t"
                "movq %%mm2, %%mm1                      \n\t"
                "punpcklwd %%mm4, %%mm2                 \n\t"
                "punpckhwd %%mm4, %%mm1                 \n\t"

                "movd %%mm0, 4(%0)                      \n\t"
                "psrlq $32, %%mm0                       \n\t"
                "movd %%mm0, 4(%%"REG_a")               \n\t"
                "movd %%mm3, 4(%%"REG_a", %1)           \n\t"
                "psrlq $32, %%mm3                       \n\t"
                "movd %%mm3, 4(%%"REG_a", %1, 2)        \n\t"
                "movd %%mm2, 4(%0, %1, 4)               \n\t"
                "psrlq $32, %%mm2                       \n\t"
                "movd %%mm2, 4(%%"REG_d")               \n\t"
                "movd %%mm1, 4(%%"REG_d", %1)           \n\t"
                "psrlq $32, %%mm1                       \n\t"
                "movd %%mm1, 4(%%"REG_d", %1, 2)        \n\t"

        :: "r" (dst), "r" ((long)dstStride), "r" (src)
        : "%"REG_a, "%"REG_d
        );
2236
}
2237
#endif //HAVE_MMX
2238
//static long test=0;
2239

2240
#ifndef HAVE_ALTIVEC
Michael Niedermayer's avatar
Michael Niedermayer committed
2241
static inline void RENAME(tempNoiseReducer)(uint8_t *src, int stride,
2242
                                    uint8_t *tempBlured, uint32_t *tempBluredPast, int *maxNoise)
2243
{
2244 2245 2246 2247
        // to save a register (FIXME do this outside of the loops)
        tempBluredPast[127]= maxNoise[0];
        tempBluredPast[128]= maxNoise[1];
        tempBluredPast[129]= maxNoise[2];
2248

2249 2250 2251
#define FAST_L2_DIFF
//#define L1_DIFF //u should change the thresholds too if u try that one
#if defined (HAVE_MMX2) || defined (HAVE_3DNOW)
2252 2253 2254 2255 2256 2257
        asm volatile(
                "lea (%2, %2, 2), %%"REG_a"             \n\t" // 3*stride
                "lea (%2, %2, 4), %%"REG_d"             \n\t" // 5*stride
                "lea (%%"REG_d", %2, 2), %%"REG_c"      \n\t" // 7*stride
//      0       1       2       3       4       5       6       7       8       9
//      %x      %x+%2   %x+2%2  %x+eax  %x+4%2  %x+edx  %x+2eax %x+ecx  %x+8%2
2258 2259
//FIXME reorder?
#ifdef L1_DIFF //needs mmx2
2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283
                "movq (%0), %%mm0                       \n\t" // L0
                "psadbw (%1), %%mm0                     \n\t" // |L0-R0|
                "movq (%0, %2), %%mm1                   \n\t" // L1
                "psadbw (%1, %2), %%mm1                 \n\t" // |L1-R1|
                "movq (%0, %2, 2), %%mm2                \n\t" // L2
                "psadbw (%1, %2, 2), %%mm2              \n\t" // |L2-R2|
                "movq (%0, %%"REG_a"), %%mm3            \n\t" // L3
                "psadbw (%1, %%"REG_a"), %%mm3          \n\t" // |L3-R3|

                "movq (%0, %2, 4), %%mm4                \n\t" // L4
                "paddw %%mm1, %%mm0                     \n\t"
                "psadbw (%1, %2, 4), %%mm4              \n\t" // |L4-R4|
                "movq (%0, %%"REG_d"), %%mm5            \n\t" // L5
                "paddw %%mm2, %%mm0                     \n\t"
                "psadbw (%1, %%"REG_d"), %%mm5          \n\t" // |L5-R5|
                "movq (%0, %%"REG_a", 2), %%mm6         \n\t" // L6
                "paddw %%mm3, %%mm0                     \n\t"
                "psadbw (%1, %%"REG_a", 2), %%mm6       \n\t" // |L6-R6|
                "movq (%0, %%"REG_c"), %%mm7            \n\t" // L7
                "paddw %%mm4, %%mm0                     \n\t"
                "psadbw (%1, %%"REG_c"), %%mm7          \n\t" // |L7-R7|
                "paddw %%mm5, %%mm6                     \n\t"
                "paddw %%mm7, %%mm6                     \n\t"
                "paddw %%mm6, %%mm0                     \n\t"
2284
#else //L1_DIFF
2285
#if defined (FAST_L2_DIFF)
2286 2287 2288
                "pcmpeqb %%mm7, %%mm7                   \n\t"
                "movq "MANGLE(b80)", %%mm6              \n\t"
                "pxor %%mm0, %%mm0                      \n\t"
2289
#define REAL_L2_DIFF_CORE(a, b)\
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301
                "movq " #a ", %%mm5                     \n\t"\
                "movq " #b ", %%mm2                     \n\t"\
                "pxor %%mm7, %%mm2                      \n\t"\
                PAVGB(%%mm2, %%mm5)\
                "paddb %%mm6, %%mm5                     \n\t"\
                "movq %%mm5, %%mm2                      \n\t"\
                "psllw $8, %%mm5                        \n\t"\
                "pmaddwd %%mm5, %%mm5                   \n\t"\
                "pmaddwd %%mm2, %%mm2                   \n\t"\
                "paddd %%mm2, %%mm5                     \n\t"\
                "psrld $14, %%mm5                       \n\t"\
                "paddd %%mm5, %%mm0                     \n\t"
2302

2303
#else //defined (FAST_L2_DIFF)
2304 2305
                "pxor %%mm7, %%mm7                      \n\t"
                "pxor %%mm0, %%mm0                      \n\t"
2306
#define REAL_L2_DIFF_CORE(a, b)\
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320
                "movq " #a ", %%mm5                     \n\t"\
                "movq " #b ", %%mm2                     \n\t"\
                "movq %%mm5, %%mm1                      \n\t"\
                "movq %%mm2, %%mm3                      \n\t"\
                "punpcklbw %%mm7, %%mm5                 \n\t"\
                "punpckhbw %%mm7, %%mm1                 \n\t"\
                "punpcklbw %%mm7, %%mm2                 \n\t"\
                "punpckhbw %%mm7, %%mm3                 \n\t"\
                "psubw %%mm2, %%mm5                     \n\t"\
                "psubw %%mm3, %%mm1                     \n\t"\
                "pmaddwd %%mm5, %%mm5                   \n\t"\
                "pmaddwd %%mm1, %%mm1                   \n\t"\
                "paddd %%mm1, %%mm5                     \n\t"\
                "paddd %%mm5, %%mm0                     \n\t"
2321

2322
#endif //defined (FAST_L2_DIFF)
2323 2324 2325

#define L2_DIFF_CORE(a, b)  REAL_L2_DIFF_CORE(a, b)

2326 2327 2328 2329 2330 2331
L2_DIFF_CORE((%0)          , (%1))
L2_DIFF_CORE((%0, %2)      , (%1, %2))
L2_DIFF_CORE((%0, %2, 2)   , (%1, %2, 2))
L2_DIFF_CORE((%0, %%REGa)  , (%1, %%REGa))
L2_DIFF_CORE((%0, %2, 4)   , (%1, %2, 4))
L2_DIFF_CORE((%0, %%REGd)  , (%1, %%REGd))
2332
L2_DIFF_CORE((%0, %%REGa,2), (%1, %%REGa,2))
2333
L2_DIFF_CORE((%0, %%REGc)  , (%1, %%REGc))
2334

2335
#endif //L1_DIFF
2336

2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
                "movq %%mm0, %%mm4                      \n\t"
                "psrlq $32, %%mm0                       \n\t"
                "paddd %%mm0, %%mm4                     \n\t"
                "movd %%mm4, %%ecx                      \n\t"
                "shll $2, %%ecx                         \n\t"
                "mov %3, %%"REG_d"                      \n\t"
                "addl -4(%%"REG_d"), %%ecx              \n\t"
                "addl 4(%%"REG_d"), %%ecx               \n\t"
                "addl -1024(%%"REG_d"), %%ecx           \n\t"
                "addl $4, %%ecx                         \n\t"
                "addl 1024(%%"REG_d"), %%ecx            \n\t"
                "shrl $3, %%ecx                         \n\t"
                "movl %%ecx, (%%"REG_d")                \n\t"

//                "mov %3, %%"REG_c"                      \n\t"
//                "mov %%"REG_c", test                    \n\t"
//                "jmp 4f                                 \n\t"
                "cmpl 512(%%"REG_d"), %%ecx             \n\t"
                " jb 2f                                 \n\t"
                "cmpl 516(%%"REG_d"), %%ecx             \n\t"
                " jb 1f                                 \n\t"

                "lea (%%"REG_a", %2, 2), %%"REG_d"      \n\t" // 5*stride
                "lea (%%"REG_d", %2, 2), %%"REG_c"      \n\t" // 7*stride
                "movq (%0), %%mm0                       \n\t" // L0
                "movq (%0, %2), %%mm1                   \n\t" // L1
                "movq (%0, %2, 2), %%mm2                \n\t" // L2
                "movq (%0, %%"REG_a"), %%mm3            \n\t" // L3
                "movq (%0, %2, 4), %%mm4                \n\t" // L4
                "movq (%0, %%"REG_d"), %%mm5            \n\t" // L5
                "movq (%0, %%"REG_a", 2), %%mm6         \n\t" // L6
                "movq (%0, %%"REG_c"), %%mm7            \n\t" // L7
                "movq %%mm0, (%1)                       \n\t" // L0
                "movq %%mm1, (%1, %2)                   \n\t" // L1
                "movq %%mm2, (%1, %2, 2)                \n\t" // L2
                "movq %%mm3, (%1, %%"REG_a")            \n\t" // L3
                "movq %%mm4, (%1, %2, 4)                \n\t" // L4
                "movq %%mm5, (%1, %%"REG_d")            \n\t" // L5
                "movq %%mm6, (%1, %%"REG_a", 2)         \n\t" // L6
                "movq %%mm7, (%1, %%"REG_c")            \n\t" // L7
                "jmp 4f                                 \n\t"

                "1:                                     \n\t"
                "lea (%%"REG_a", %2, 2), %%"REG_d"      \n\t" // 5*stride
                "lea (%%"REG_d", %2, 2), %%"REG_c"      \n\t" // 7*stride
                "movq (%0), %%mm0                       \n\t" // L0
                PAVGB((%1), %%mm0)                            // L0
                "movq (%0, %2), %%mm1                   \n\t" // L1
                PAVGB((%1, %2), %%mm1)                        // L1
                "movq (%0, %2, 2), %%mm2                \n\t" // L2
                PAVGB((%1, %2, 2), %%mm2)                     // L2
                "movq (%0, %%"REG_a"), %%mm3            \n\t" // L3
                PAVGB((%1, %%REGa), %%mm3)                    // L3
                "movq (%0, %2, 4), %%mm4                \n\t" // L4
                PAVGB((%1, %2, 4), %%mm4)                     // L4
                "movq (%0, %%"REG_d"), %%mm5            \n\t" // L5
                PAVGB((%1, %%REGd), %%mm5)                    // L5
                "movq (%0, %%"REG_a", 2), %%mm6         \n\t" // L6
                PAVGB((%1, %%REGa, 2), %%mm6)                 // L6
                "movq (%0, %%"REG_c"), %%mm7            \n\t" // L7
                PAVGB((%1, %%REGc), %%mm7)                    // L7
                "movq %%mm0, (%1)                       \n\t" // R0
                "movq %%mm1, (%1, %2)                   \n\t" // R1
                "movq %%mm2, (%1, %2, 2)                \n\t" // R2
                "movq %%mm3, (%1, %%"REG_a")            \n\t" // R3
                "movq %%mm4, (%1, %2, 4)                \n\t" // R4
                "movq %%mm5, (%1, %%"REG_d")            \n\t" // R5
                "movq %%mm6, (%1, %%"REG_a", 2)         \n\t" // R6
                "movq %%mm7, (%1, %%"REG_c")            \n\t" // R7
                "movq %%mm0, (%0)                       \n\t" // L0
                "movq %%mm1, (%0, %2)                   \n\t" // L1
                "movq %%mm2, (%0, %2, 2)                \n\t" // L2
                "movq %%mm3, (%0, %%"REG_a")            \n\t" // L3
                "movq %%mm4, (%0, %2, 4)                \n\t" // L4
                "movq %%mm5, (%0, %%"REG_d")            \n\t" // L5
                "movq %%mm6, (%0, %%"REG_a", 2)         \n\t" // L6
                "movq %%mm7, (%0, %%"REG_c")            \n\t" // L7
                "jmp 4f                                 \n\t"

                "2:                                     \n\t"
                "cmpl 508(%%"REG_d"), %%ecx             \n\t"
                " jb 3f                                 \n\t"

                "lea (%%"REG_a", %2, 2), %%"REG_d"      \n\t" // 5*stride
                "lea (%%"REG_d", %2, 2), %%"REG_c"      \n\t" // 7*stride
                "movq (%0), %%mm0                       \n\t" // L0
                "movq (%0, %2), %%mm1                   \n\t" // L1
                "movq (%0, %2, 2), %%mm2                \n\t" // L2
                "movq (%0, %%"REG_a"), %%mm3            \n\t" // L3
                "movq (%1), %%mm4                       \n\t" // R0
                "movq (%1, %2), %%mm5                   \n\t" // R1
                "movq (%1, %2, 2), %%mm6                \n\t" // R2
                "movq (%1, %%"REG_a"), %%mm7            \n\t" // R3
                PAVGB(%%mm4, %%mm0)
                PAVGB(%%mm5, %%mm1)
                PAVGB(%%mm6, %%mm2)
                PAVGB(%%mm7, %%mm3)
                PAVGB(%%mm4, %%mm0)
                PAVGB(%%mm5, %%mm1)
                PAVGB(%%mm6, %%mm2)
                PAVGB(%%mm7, %%mm3)
                "movq %%mm0, (%1)                       \n\t" // R0
                "movq %%mm1, (%1, %2)                   \n\t" // R1
                "movq %%mm2, (%1, %2, 2)                \n\t" // R2
                "movq %%mm3, (%1, %%"REG_a")            \n\t" // R3
                "movq %%mm0, (%0)                       \n\t" // L0
                "movq %%mm1, (%0, %2)                   \n\t" // L1
                "movq %%mm2, (%0, %2, 2)                \n\t" // L2
                "movq %%mm3, (%0, %%"REG_a")            \n\t" // L3

                "movq (%0, %2, 4), %%mm0                \n\t" // L4
                "movq (%0, %%"REG_d"), %%mm1            \n\t" // L5
                "movq (%0, %%"REG_a", 2), %%mm2         \n\t" // L6
                "movq (%0, %%"REG_c"), %%mm3            \n\t" // L7
                "movq (%1, %2, 4), %%mm4                \n\t" // R4
                "movq (%1, %%"REG_d"), %%mm5            \n\t" // R5
                "movq (%1, %%"REG_a", 2), %%mm6         \n\t" // R6
                "movq (%1, %%"REG_c"), %%mm7            \n\t" // R7
                PAVGB(%%mm4, %%mm0)
                PAVGB(%%mm5, %%mm1)
                PAVGB(%%mm6, %%mm2)
                PAVGB(%%mm7, %%mm3)
                PAVGB(%%mm4, %%mm0)
                PAVGB(%%mm5, %%mm1)
                PAVGB(%%mm6, %%mm2)
                PAVGB(%%mm7, %%mm3)
                "movq %%mm0, (%1, %2, 4)                \n\t" // R4
                "movq %%mm1, (%1, %%"REG_d")            \n\t" // R5
                "movq %%mm2, (%1, %%"REG_a", 2)         \n\t" // R6
                "movq %%mm3, (%1, %%"REG_c")            \n\t" // R7
                "movq %%mm0, (%0, %2, 4)                \n\t" // L4
                "movq %%mm1, (%0, %%"REG_d")            \n\t" // L5
                "movq %%mm2, (%0, %%"REG_a", 2)         \n\t" // L6
                "movq %%mm3, (%0, %%"REG_c")            \n\t" // L7
                "jmp 4f                                 \n\t"

                "3:                                     \n\t"
                "lea (%%"REG_a", %2, 2), %%"REG_d"      \n\t" // 5*stride
                "lea (%%"REG_d", %2, 2), %%"REG_c"      \n\t" // 7*stride
                "movq (%0), %%mm0                       \n\t" // L0
                "movq (%0, %2), %%mm1                   \n\t" // L1
                "movq (%0, %2, 2), %%mm2                \n\t" // L2
                "movq (%0, %%"REG_a"), %%mm3            \n\t" // L3
                "movq (%1), %%mm4                       \n\t" // R0
                "movq (%1, %2), %%mm5                   \n\t" // R1
                "movq (%1, %2, 2), %%mm6                \n\t" // R2
                "movq (%1, %%"REG_a"), %%mm7            \n\t" // R3
                PAVGB(%%mm4, %%mm0)
                PAVGB(%%mm5, %%mm1)
                PAVGB(%%mm6, %%mm2)
                PAVGB(%%mm7, %%mm3)
                PAVGB(%%mm4, %%mm0)
                PAVGB(%%mm5, %%mm1)
                PAVGB(%%mm6, %%mm2)
                PAVGB(%%mm7, %%mm3)
                PAVGB(%%mm4, %%mm0)
                PAVGB(%%mm5, %%mm1)
                PAVGB(%%mm6, %%mm2)
                PAVGB(%%mm7, %%mm3)
                "movq %%mm0, (%1)                       \n\t" // R0
                "movq %%mm1, (%1, %2)                   \n\t" // R1
                "movq %%mm2, (%1, %2, 2)                \n\t" // R2
                "movq %%mm3, (%1, %%"REG_a")            \n\t" // R3
                "movq %%mm0, (%0)                       \n\t" // L0
                "movq %%mm1, (%0, %2)                   \n\t" // L1
                "movq %%mm2, (%0, %2, 2)                \n\t" // L2
                "movq %%mm3, (%0, %%"REG_a")            \n\t" // L3

                "movq (%0, %2, 4), %%mm0                \n\t" // L4
                "movq (%0, %%"REG_d"), %%mm1            \n\t" // L5
                "movq (%0, %%"REG_a", 2), %%mm2         \n\t" // L6
                "movq (%0, %%"REG_c"), %%mm3            \n\t" // L7
                "movq (%1, %2, 4), %%mm4                \n\t" // R4
                "movq (%1, %%"REG_d"), %%mm5            \n\t" // R5
                "movq (%1, %%"REG_a", 2), %%mm6         \n\t" // R6
                "movq (%1, %%"REG_c"), %%mm7            \n\t" // R7
                PAVGB(%%mm4, %%mm0)
                PAVGB(%%mm5, %%mm1)
                PAVGB(%%mm6, %%mm2)
                PAVGB(%%mm7, %%mm3)
                PAVGB(%%mm4, %%mm0)
                PAVGB(%%mm5, %%mm1)
                PAVGB(%%mm6, %%mm2)
                PAVGB(%%mm7, %%mm3)
                PAVGB(%%mm4, %%mm0)
                PAVGB(%%mm5, %%mm1)
                PAVGB(%%mm6, %%mm2)
                PAVGB(%%mm7, %%mm3)
                "movq %%mm0, (%1, %2, 4)                \n\t" // R4
                "movq %%mm1, (%1, %%"REG_d")            \n\t" // R5
                "movq %%mm2, (%1, %%"REG_a", 2)         \n\t" // R6
                "movq %%mm3, (%1, %%"REG_c")            \n\t" // R7
                "movq %%mm0, (%0, %2, 4)                \n\t" // L4
                "movq %%mm1, (%0, %%"REG_d")            \n\t" // L5
                "movq %%mm2, (%0, %%"REG_a", 2)         \n\t" // L6
                "movq %%mm3, (%0, %%"REG_c")            \n\t" // L7

                "4:                                     \n\t"

                :: "r" (src), "r" (tempBlured), "r"((long)stride), "m" (tempBluredPast)
                : "%"REG_a, "%"REG_d, "%"REG_c, "memory"
                );
2539
#else //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
2540
{
2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555
        int y;
        int d=0;
//        int sysd=0;
        int i;

        for(y=0; y<8; y++)
        {
                int x;
                for(x=0; x<8; x++)
                {
                        int ref= tempBlured[ x + y*stride ];
                        int cur= src[ x + y*stride ];
                        int d1=ref - cur;
//                        if(x==0 || x==7) d1+= d1>>1;
//                        if(y==0 || y==7) d1+= d1>>1;
2556
//                        d+= FFABS(d1);
2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569
                        d+= d1*d1;
//                        sysd+= d1;
                }
        }
        i=d;
        d=         (
                4*d
                +(*(tempBluredPast-256))
                +(*(tempBluredPast-1))+ (*(tempBluredPast+1))
                +(*(tempBluredPast+256))
                +4)>>3;
        *tempBluredPast=i;
//        ((*tempBluredPast)*3 + d + 2)>>2;
Michael Niedermayer's avatar
Michael Niedermayer committed
2570

2571 2572 2573 2574 2575 2576 2577
/*
Switch between
 1  0  0  0  0  0  0  (0)
64 32 16  8  4  2  1  (1)
64 48 36 27 20 15 11 (33) (approx)
64 56 49 43 37 33 29 (200) (approx)
*/
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639
        if(d > maxNoise[1])
        {
                if(d < maxNoise[2])
                {
                        for(y=0; y<8; y++)
                        {
                                int x;
                                for(x=0; x<8; x++)
                                {
                                        int ref= tempBlured[ x + y*stride ];
                                        int cur= src[ x + y*stride ];
                                        tempBlured[ x + y*stride ]=
                                        src[ x + y*stride ]=
                                                (ref + cur + 1)>>1;
                                }
                        }
                }
                else
                {
                        for(y=0; y<8; y++)
                        {
                                int x;
                                for(x=0; x<8; x++)
                                {
                                        tempBlured[ x + y*stride ]= src[ x + y*stride ];
                                }
                        }
                }
        }
        else
        {
                if(d < maxNoise[0])
                {
                        for(y=0; y<8; y++)
                        {
                                int x;
                                for(x=0; x<8; x++)
                                {
                                        int ref= tempBlured[ x + y*stride ];
                                        int cur= src[ x + y*stride ];
                                        tempBlured[ x + y*stride ]=
                                        src[ x + y*stride ]=
                                                (ref*7 + cur + 4)>>3;
                                }
                        }
                }
                else
                {
                        for(y=0; y<8; y++)
                        {
                                int x;
                                for(x=0; x<8; x++)
                                {
                                        int ref= tempBlured[ x + y*stride ];
                                        int cur= src[ x + y*stride ];
                                        tempBlured[ x + y*stride ]=
                                        src[ x + y*stride ]=
                                                (ref*3 + cur + 2)>>2;
                                }
                        }
                }
        }
2640
}
2641
#endif //defined (HAVE_MMX2) || defined (HAVE_3DNOW)
2642
}
2643
#endif //HAVE_ALTIVEC
2644

2645 2646 2647 2648
#ifdef HAVE_MMX
/**
 * accurate deblock filter
 */
2649
static av_always_inline void RENAME(do_a_deblock)(uint8_t *src, int step, int stride, PPContext *c){
2650 2651 2652
        int64_t dc_mask, eq_mask, both_masks;
        int64_t sums[10*8*2];
        src+= step*3; // src points to begin of the 8x8 Block
2653 2654
//START_TIMER
asm volatile(
2655 2656
                "movq %0, %%mm7                         \n\t"
                "movq %1, %%mm6                         \n\t"
2657 2658
                : : "m" (c->mmxDcOffset[c->nonBQP]),  "m" (c->mmxDcThreshold[c->nonBQP])
                );
2659

2660
asm volatile(
2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673
                "lea (%2, %3), %%"REG_a"                \n\t"
//      0       1       2       3       4       5       6       7       8       9
//      %1      eax     eax+%2  eax+2%2 %1+4%2  ecx     ecx+%2  ecx+2%2 %1+8%2  ecx+4%2

                "movq (%2), %%mm0                       \n\t"
                "movq (%%"REG_a"), %%mm1                \n\t"
                "movq %%mm1, %%mm3                      \n\t"
                "movq %%mm1, %%mm4                      \n\t"
                "psubb %%mm1, %%mm0                     \n\t" // mm0 = differnece
                "paddb %%mm7, %%mm0                     \n\t"
                "pcmpgtb %%mm6, %%mm0                   \n\t"

                "movq (%%"REG_a",%3), %%mm2             \n\t"
2674 2675
                PMAXUB(%%mm2, %%mm4)
                PMINUB(%%mm2, %%mm3, %%mm5)
2676 2677 2678 2679
                "psubb %%mm2, %%mm1                     \n\t"
                "paddb %%mm7, %%mm1                     \n\t"
                "pcmpgtb %%mm6, %%mm1                   \n\t"
                "paddb %%mm1, %%mm0                     \n\t"
2680

2681
                "movq (%%"REG_a", %3, 2), %%mm1         \n\t"
2682 2683
                PMAXUB(%%mm1, %%mm4)
                PMINUB(%%mm1, %%mm3, %%mm5)
2684 2685 2686 2687
                "psubb %%mm1, %%mm2                     \n\t"
                "paddb %%mm7, %%mm2                     \n\t"
                "pcmpgtb %%mm6, %%mm2                   \n\t"
                "paddb %%mm2, %%mm0                     \n\t"
2688

2689
                "lea (%%"REG_a", %3, 4), %%"REG_a"      \n\t"
2690

2691
                "movq (%2, %3, 4), %%mm2                \n\t"
2692 2693
                PMAXUB(%%mm2, %%mm4)
                PMINUB(%%mm2, %%mm3, %%mm5)
2694 2695 2696 2697
                "psubb %%mm2, %%mm1                     \n\t"
                "paddb %%mm7, %%mm1                     \n\t"
                "pcmpgtb %%mm6, %%mm1                   \n\t"
                "paddb %%mm1, %%mm0                     \n\t"
2698

2699
                "movq (%%"REG_a"), %%mm1                \n\t"
2700 2701
                PMAXUB(%%mm1, %%mm4)
                PMINUB(%%mm1, %%mm3, %%mm5)
2702 2703 2704 2705
                "psubb %%mm1, %%mm2                     \n\t"
                "paddb %%mm7, %%mm2                     \n\t"
                "pcmpgtb %%mm6, %%mm2                   \n\t"
                "paddb %%mm2, %%mm0                     \n\t"
2706

2707
                "movq (%%"REG_a", %3), %%mm2            \n\t"
2708 2709
                PMAXUB(%%mm2, %%mm4)
                PMINUB(%%mm2, %%mm3, %%mm5)
2710 2711 2712 2713
                "psubb %%mm2, %%mm1                     \n\t"
                "paddb %%mm7, %%mm1                     \n\t"
                "pcmpgtb %%mm6, %%mm1                   \n\t"
                "paddb %%mm1, %%mm0                     \n\t"
2714

2715
                "movq (%%"REG_a", %3, 2), %%mm1         \n\t"
2716 2717
                PMAXUB(%%mm1, %%mm4)
                PMINUB(%%mm1, %%mm3, %%mm5)
2718 2719 2720 2721
                "psubb %%mm1, %%mm2                     \n\t"
                "paddb %%mm7, %%mm2                     \n\t"
                "pcmpgtb %%mm6, %%mm2                   \n\t"
                "paddb %%mm2, %%mm0                     \n\t"
2722

2723
                "movq (%2, %3, 8), %%mm2                \n\t"
2724 2725
                PMAXUB(%%mm2, %%mm4)
                PMINUB(%%mm2, %%mm3, %%mm5)
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810
                "psubb %%mm2, %%mm1                     \n\t"
                "paddb %%mm7, %%mm1                     \n\t"
                "pcmpgtb %%mm6, %%mm1                   \n\t"
                "paddb %%mm1, %%mm0                     \n\t"

                "movq (%%"REG_a", %3, 4), %%mm1         \n\t"
                "psubb %%mm1, %%mm2                     \n\t"
                "paddb %%mm7, %%mm2                     \n\t"
                "pcmpgtb %%mm6, %%mm2                   \n\t"
                "paddb %%mm2, %%mm0                     \n\t"
                "psubusb %%mm3, %%mm4                   \n\t"

                "pxor %%mm6, %%mm6                      \n\t"
                "movq %4, %%mm7                         \n\t" // QP,..., QP
                "paddusb %%mm7, %%mm7                   \n\t" // 2QP ... 2QP
                "psubusb %%mm4, %%mm7                   \n\t" // Diff >=2QP -> 0
                "pcmpeqb %%mm6, %%mm7                   \n\t" // Diff < 2QP -> 0
                "pcmpeqb %%mm6, %%mm7                   \n\t" // Diff < 2QP -> 0
                "movq %%mm7, %1                         \n\t"

                "movq %5, %%mm7                         \n\t"
                "punpcklbw %%mm7, %%mm7                 \n\t"
                "punpcklbw %%mm7, %%mm7                 \n\t"
                "punpcklbw %%mm7, %%mm7                 \n\t"
                "psubb %%mm0, %%mm6                     \n\t"
                "pcmpgtb %%mm7, %%mm6                   \n\t"
                "movq %%mm6, %0                         \n\t"

                : "=m" (eq_mask), "=m" (dc_mask)
                : "r" (src), "r" ((long)step), "m" (c->pQPb), "m"(c->ppMode.flatnessThreshold)
                : "%"REG_a
                );

        both_masks = dc_mask & eq_mask;

        if(both_masks){
                long offset= -8*step;
                int64_t *temp_sums= sums;

                asm volatile(
                "movq %2, %%mm0                         \n\t"  // QP,..., QP
                "pxor %%mm4, %%mm4                      \n\t"

                "movq (%0), %%mm6                       \n\t"
                "movq (%0, %1), %%mm5                   \n\t"
                "movq %%mm5, %%mm1                      \n\t"
                "movq %%mm6, %%mm2                      \n\t"
                "psubusb %%mm6, %%mm5                   \n\t"
                "psubusb %%mm1, %%mm2                   \n\t"
                "por %%mm5, %%mm2                       \n\t" // ABS Diff of lines
                "psubusb %%mm2, %%mm0                   \n\t" // diff >= QP -> 0
                "pcmpeqb %%mm4, %%mm0                   \n\t" // diff >= QP -> FF

                "pxor %%mm6, %%mm1                      \n\t"
                "pand %%mm0, %%mm1                      \n\t"
                "pxor %%mm1, %%mm6                      \n\t"
                // 0:QP  6:First

                "movq (%0, %1, 8), %%mm5                \n\t"
                "add %1, %0                             \n\t" // %0 points to line 1 not 0
                "movq (%0, %1, 8), %%mm7                \n\t"
                "movq %%mm5, %%mm1                      \n\t"
                "movq %%mm7, %%mm2                      \n\t"
                "psubusb %%mm7, %%mm5                   \n\t"
                "psubusb %%mm1, %%mm2                   \n\t"
                "por %%mm5, %%mm2                       \n\t" // ABS Diff of lines
                "movq %2, %%mm0                         \n\t"  // QP,..., QP
                "psubusb %%mm2, %%mm0                   \n\t" // diff >= QP -> 0
                "pcmpeqb %%mm4, %%mm0                   \n\t" // diff >= QP -> FF

                "pxor %%mm7, %%mm1                      \n\t"
                "pand %%mm0, %%mm1                      \n\t"
                "pxor %%mm1, %%mm7                      \n\t"

                "movq %%mm6, %%mm5                      \n\t"
                "punpckhbw %%mm4, %%mm6                 \n\t"
                "punpcklbw %%mm4, %%mm5                 \n\t"
                // 4:0 5/6:First 7:Last

                "movq %%mm5, %%mm0                      \n\t"
                "movq %%mm6, %%mm1                      \n\t"
                "psllw $2, %%mm0                        \n\t"
                "psllw $2, %%mm1                        \n\t"
                "paddw "MANGLE(w04)", %%mm0             \n\t"
                "paddw "MANGLE(w04)", %%mm1             \n\t"
2811 2812

#define NEXT\
2813 2814 2815 2816 2817 2818 2819
                "movq (%0), %%mm2                       \n\t"\
                "movq (%0), %%mm3                       \n\t"\
                "add %1, %0                             \n\t"\
                "punpcklbw %%mm4, %%mm2                 \n\t"\
                "punpckhbw %%mm4, %%mm3                 \n\t"\
                "paddw %%mm2, %%mm0                     \n\t"\
                "paddw %%mm3, %%mm1                     \n\t"
2820 2821

#define PREV\
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008
                "movq (%0), %%mm2                       \n\t"\
                "movq (%0), %%mm3                       \n\t"\
                "add %1, %0                             \n\t"\
                "punpcklbw %%mm4, %%mm2                 \n\t"\
                "punpckhbw %%mm4, %%mm3                 \n\t"\
                "psubw %%mm2, %%mm0                     \n\t"\
                "psubw %%mm3, %%mm1                     \n\t"


                NEXT //0
                NEXT //1
                NEXT //2
                "movq %%mm0, (%3)                       \n\t"
                "movq %%mm1, 8(%3)                      \n\t"

                NEXT //3
                "psubw %%mm5, %%mm0                     \n\t"
                "psubw %%mm6, %%mm1                     \n\t"
                "movq %%mm0, 16(%3)                     \n\t"
                "movq %%mm1, 24(%3)                     \n\t"

                NEXT //4
                "psubw %%mm5, %%mm0                     \n\t"
                "psubw %%mm6, %%mm1                     \n\t"
                "movq %%mm0, 32(%3)                     \n\t"
                "movq %%mm1, 40(%3)                     \n\t"

                NEXT //5
                "psubw %%mm5, %%mm0                     \n\t"
                "psubw %%mm6, %%mm1                     \n\t"
                "movq %%mm0, 48(%3)                     \n\t"
                "movq %%mm1, 56(%3)                     \n\t"

                NEXT //6
                "psubw %%mm5, %%mm0                     \n\t"
                "psubw %%mm6, %%mm1                     \n\t"
                "movq %%mm0, 64(%3)                     \n\t"
                "movq %%mm1, 72(%3)                     \n\t"

                "movq %%mm7, %%mm6                      \n\t"
                "punpckhbw %%mm4, %%mm7                 \n\t"
                "punpcklbw %%mm4, %%mm6                 \n\t"

                NEXT //7
                "mov %4, %0                             \n\t"
                "add %1, %0                             \n\t"
                PREV //0
                "movq %%mm0, 80(%3)                     \n\t"
                "movq %%mm1, 88(%3)                     \n\t"

                PREV //1
                "paddw %%mm6, %%mm0                     \n\t"
                "paddw %%mm7, %%mm1                     \n\t"
                "movq %%mm0, 96(%3)                     \n\t"
                "movq %%mm1, 104(%3)                    \n\t"

                PREV //2
                "paddw %%mm6, %%mm0                     \n\t"
                "paddw %%mm7, %%mm1                     \n\t"
                "movq %%mm0, 112(%3)                    \n\t"
                "movq %%mm1, 120(%3)                    \n\t"

                PREV //3
                "paddw %%mm6, %%mm0                     \n\t"
                "paddw %%mm7, %%mm1                     \n\t"
                "movq %%mm0, 128(%3)                    \n\t"
                "movq %%mm1, 136(%3)                    \n\t"

                PREV //4
                "paddw %%mm6, %%mm0                     \n\t"
                "paddw %%mm7, %%mm1                     \n\t"
                "movq %%mm0, 144(%3)                    \n\t"
                "movq %%mm1, 152(%3)                    \n\t"

                "mov %4, %0                             \n\t" //FIXME

                : "+&r"(src)
                : "r" ((long)step), "m" (c->pQPb), "r"(sums), "g"(src)
                );

                src+= step; // src points to begin of the 8x8 Block

                asm volatile(
                "movq %4, %%mm6                         \n\t"
                "pcmpeqb %%mm5, %%mm5                   \n\t"
                "pxor %%mm6, %%mm5                      \n\t"
                "pxor %%mm7, %%mm7                      \n\t"

                "1:                                     \n\t"
                "movq (%1), %%mm0                       \n\t"
                "movq 8(%1), %%mm1                      \n\t"
                "paddw 32(%1), %%mm0                    \n\t"
                "paddw 40(%1), %%mm1                    \n\t"
                "movq (%0, %3), %%mm2                   \n\t"
                "movq %%mm2, %%mm3                      \n\t"
                "movq %%mm2, %%mm4                      \n\t"
                "punpcklbw %%mm7, %%mm2                 \n\t"
                "punpckhbw %%mm7, %%mm3                 \n\t"
                "paddw %%mm2, %%mm0                     \n\t"
                "paddw %%mm3, %%mm1                     \n\t"
                "paddw %%mm2, %%mm0                     \n\t"
                "paddw %%mm3, %%mm1                     \n\t"
                "psrlw $4, %%mm0                        \n\t"
                "psrlw $4, %%mm1                        \n\t"
                "packuswb %%mm1, %%mm0                  \n\t"
                "pand %%mm6, %%mm0                      \n\t"
                "pand %%mm5, %%mm4                      \n\t"
                "por %%mm4, %%mm0                       \n\t"
                "movq %%mm0, (%0, %3)                   \n\t"
                "add $16, %1                            \n\t"
                "add %2, %0                             \n\t"
                " js 1b                                 \n\t"

                : "+r"(offset), "+r"(temp_sums)
                : "r" ((long)step), "r"(src - offset), "m"(both_masks)
                );
        }else
                src+= step; // src points to begin of the 8x8 Block

        if(eq_mask != -1LL){
                uint8_t *temp_src= src;
                asm volatile(
                "pxor %%mm7, %%mm7                      \n\t"
                "lea -40(%%"REG_SP"), %%"REG_c"         \n\t" // make space for 4 8-byte vars
                "and "ALIGN_MASK", %%"REG_c"            \n\t" // align
//      0       1       2       3       4       5       6       7       8       9
//      %0      eax     eax+%1  eax+2%1 %0+4%1  ecx     ecx+%1  ecx+2%1 %1+8%1  ecx+4%1

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

                "movq (%0, %1), %%mm2                   \n\t"
                "lea (%0, %1, 2), %%"REG_a"             \n\t"
                "movq %%mm2, %%mm3                      \n\t"
                "punpcklbw %%mm7, %%mm2                 \n\t" // low part of line 1
                "punpckhbw %%mm7, %%mm3                 \n\t" // high part of line 1

                "movq (%%"REG_a"), %%mm4                \n\t"
                "movq %%mm4, %%mm5                      \n\t"
                "punpcklbw %%mm7, %%mm4                 \n\t" // low part of line 2
                "punpckhbw %%mm7, %%mm5                 \n\t" // high part of line 2

                "paddw %%mm0, %%mm0                     \n\t" // 2L0
                "paddw %%mm1, %%mm1                     \n\t" // 2H0
                "psubw %%mm4, %%mm2                     \n\t" // L1 - L2
                "psubw %%mm5, %%mm3                     \n\t" // H1 - H2
                "psubw %%mm2, %%mm0                     \n\t" // 2L0 - L1 + L2
                "psubw %%mm3, %%mm1                     \n\t" // 2H0 - H1 + H2

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

                "movq (%%"REG_a", %1), %%mm2            \n\t"
                "movq %%mm2, %%mm3                      \n\t"
                "punpcklbw %%mm7, %%mm2                 \n\t" // L3
                "punpckhbw %%mm7, %%mm3                 \n\t" // H3

                "psubw %%mm2, %%mm0                     \n\t" // 2L0 - 5L1 + 5L2 - L3
                "psubw %%mm3, %%mm1                     \n\t" // 2H0 - 5H1 + 5H2 - H3
                "psubw %%mm2, %%mm0                     \n\t" // 2L0 - 5L1 + 5L2 - 2L3
                "psubw %%mm3, %%mm1                     \n\t" // 2H0 - 5H1 + 5H2 - 2H3
                "movq %%mm0, (%%"REG_c")                \n\t" // 2L0 - 5L1 + 5L2 - 2L3
                "movq %%mm1, 8(%%"REG_c")               \n\t" // 2H0 - 5H1 + 5H2 - 2H3

                "movq (%%"REG_a", %1, 2), %%mm0         \n\t"
                "movq %%mm0, %%mm1                      \n\t"
                "punpcklbw %%mm7, %%mm0                 \n\t" // L4
                "punpckhbw %%mm7, %%mm1                 \n\t" // H4

                "psubw %%mm0, %%mm2                     \n\t" // L3 - L4
                "psubw %%mm1, %%mm3                     \n\t" // H3 - H4
                "movq %%mm2, 16(%%"REG_c")              \n\t" // L3 - L4
                "movq %%mm3, 24(%%"REG_c")              \n\t" // H3 - H4
                "paddw %%mm4, %%mm4                     \n\t" // 2L2
                "paddw %%mm5, %%mm5                     \n\t" // 2H2
                "psubw %%mm2, %%mm4                     \n\t" // 2L2 - L3 + L4
                "psubw %%mm3, %%mm5                     \n\t" // 2H2 - H3 + H4

                "lea (%%"REG_a", %1), %0                \n\t"
                "psllw $2, %%mm2                        \n\t" // 4L3 - 4L4
                "psllw $2, %%mm3                        \n\t" // 4H3 - 4H4
                "psubw %%mm2, %%mm4                     \n\t" // 2L2 - 5L3 + 5L4
                "psubw %%mm3, %%mm5                     \n\t" // 2H2 - 5H3 + 5H4
3009
//50 opcodes so far
3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047
                "movq (%0, %1, 2), %%mm2                \n\t"
                "movq %%mm2, %%mm3                      \n\t"
                "punpcklbw %%mm7, %%mm2                 \n\t" // L5
                "punpckhbw %%mm7, %%mm3                 \n\t" // H5
                "psubw %%mm2, %%mm4                     \n\t" // 2L2 - 5L3 + 5L4 - L5
                "psubw %%mm3, %%mm5                     \n\t" // 2H2 - 5H3 + 5H4 - H5
                "psubw %%mm2, %%mm4                     \n\t" // 2L2 - 5L3 + 5L4 - 2L5
                "psubw %%mm3, %%mm5                     \n\t" // 2H2 - 5H3 + 5H4 - 2H5

                "movq (%%"REG_a", %1, 4), %%mm6         \n\t"
                "punpcklbw %%mm7, %%mm6                 \n\t" // L6
                "psubw %%mm6, %%mm2                     \n\t" // L5 - L6
                "movq (%%"REG_a", %1, 4), %%mm6         \n\t"
                "punpckhbw %%mm7, %%mm6                 \n\t" // H6
                "psubw %%mm6, %%mm3                     \n\t" // H5 - H6

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

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

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

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

                "movq (%%"REG_c"), %%mm2                \n\t" // 2L0 - 5L1 + 5L2 - 2L3
                "movq 8(%%"REG_c"), %%mm3               \n\t" // 2H0 - 5H1 + 5H2 - 2H3
3048 3049

#ifdef HAVE_MMX2
3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061
                "movq %%mm7, %%mm6                      \n\t" // 0
                "psubw %%mm0, %%mm6                     \n\t"
                "pmaxsw %%mm6, %%mm0                    \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
                "movq %%mm7, %%mm6                      \n\t" // 0
                "psubw %%mm1, %%mm6                     \n\t"
                "pmaxsw %%mm6, %%mm1                    \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
                "movq %%mm7, %%mm6                      \n\t" // 0
                "psubw %%mm2, %%mm6                     \n\t"
                "pmaxsw %%mm6, %%mm2                    \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
                "movq %%mm7, %%mm6                      \n\t" // 0
                "psubw %%mm3, %%mm6                     \n\t"
                "pmaxsw %%mm6, %%mm3                    \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
3062
#else
3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078
                "movq %%mm7, %%mm6                      \n\t" // 0
                "pcmpgtw %%mm0, %%mm6                   \n\t"
                "pxor %%mm6, %%mm0                      \n\t"
                "psubw %%mm6, %%mm0                     \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
                "movq %%mm7, %%mm6                      \n\t" // 0
                "pcmpgtw %%mm1, %%mm6                   \n\t"
                "pxor %%mm6, %%mm1                      \n\t"
                "psubw %%mm6, %%mm1                     \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
                "movq %%mm7, %%mm6                      \n\t" // 0
                "pcmpgtw %%mm2, %%mm6                   \n\t"
                "pxor %%mm6, %%mm2                      \n\t"
                "psubw %%mm6, %%mm2                     \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
                "movq %%mm7, %%mm6                      \n\t" // 0
                "pcmpgtw %%mm3, %%mm6                   \n\t"
                "pxor %%mm6, %%mm3                      \n\t"
                "psubw %%mm6, %%mm3                     \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
3079 3080 3081
#endif

#ifdef HAVE_MMX2
3082 3083
                "pminsw %%mm2, %%mm0                    \n\t"
                "pminsw %%mm3, %%mm1                    \n\t"
3084
#else
3085 3086 3087 3088 3089 3090
                "movq %%mm0, %%mm6                      \n\t"
                "psubusw %%mm2, %%mm6                   \n\t"
                "psubw %%mm6, %%mm0                     \n\t"
                "movq %%mm1, %%mm6                      \n\t"
                "psubusw %%mm3, %%mm6                   \n\t"
                "psubw %%mm6, %%mm1                     \n\t"
3091 3092
#endif

3093 3094
                "movd %2, %%mm2                         \n\t" // QP
                "punpcklbw %%mm7, %%mm2                 \n\t"
3095

3096 3097 3098 3099 3100 3101 3102
                "movq %%mm7, %%mm6                      \n\t" // 0
                "pcmpgtw %%mm4, %%mm6                   \n\t" // sign(2L2 - 5L3 + 5L4 - 2L5)
                "pxor %%mm6, %%mm4                      \n\t"
                "psubw %%mm6, %%mm4                     \n\t" // |2L2 - 5L3 + 5L4 - 2L5|
                "pcmpgtw %%mm5, %%mm7                   \n\t" // sign(2H2 - 5H3 + 5H4 - 2H5)
                "pxor %%mm7, %%mm5                      \n\t"
                "psubw %%mm7, %%mm5                     \n\t" // |2H2 - 5H3 + 5H4 - 2H5|
3103
// 100 opcodes
3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
                "psllw $3, %%mm2                        \n\t" // 8QP
                "movq %%mm2, %%mm3                      \n\t" // 8QP
                "pcmpgtw %%mm4, %%mm2                   \n\t"
                "pcmpgtw %%mm5, %%mm3                   \n\t"
                "pand %%mm2, %%mm4                      \n\t"
                "pand %%mm3, %%mm5                      \n\t"


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


                "movq "MANGLE(w05)", %%mm2              \n\t" // 5
                "pmullw %%mm2, %%mm4                    \n\t"
                "pmullw %%mm2, %%mm5                    \n\t"
                "movq "MANGLE(w20)", %%mm2              \n\t" // 32
                "paddw %%mm2, %%mm4                     \n\t"
                "paddw %%mm2, %%mm5                     \n\t"
                "psrlw $6, %%mm4                        \n\t"
                "psrlw $6, %%mm5                        \n\t"

                "movq 16(%%"REG_c"), %%mm0              \n\t" // L3 - L4
                "movq 24(%%"REG_c"), %%mm1              \n\t" // H3 - H4

                "pxor %%mm2, %%mm2                      \n\t"
                "pxor %%mm3, %%mm3                      \n\t"

                "pcmpgtw %%mm0, %%mm2                   \n\t" // sign (L3-L4)
                "pcmpgtw %%mm1, %%mm3                   \n\t" // sign (H3-H4)
                "pxor %%mm2, %%mm0                      \n\t"
                "pxor %%mm3, %%mm1                      \n\t"
                "psubw %%mm2, %%mm0                     \n\t" // |L3-L4|
                "psubw %%mm3, %%mm1                     \n\t" // |H3-H4|
                "psrlw $1, %%mm0                        \n\t" // |L3 - L4|/2
                "psrlw $1, %%mm1                        \n\t" // |H3 - H4|/2

                "pxor %%mm6, %%mm2                      \n\t"
                "pxor %%mm7, %%mm3                      \n\t"
                "pand %%mm2, %%mm4                      \n\t"
                "pand %%mm3, %%mm5                      \n\t"
3144 3145

#ifdef HAVE_MMX2
3146 3147
                "pminsw %%mm0, %%mm4                    \n\t"
                "pminsw %%mm1, %%mm5                    \n\t"
3148
#else
3149 3150 3151 3152 3153 3154
                "movq %%mm4, %%mm2                      \n\t"
                "psubusw %%mm0, %%mm2                   \n\t"
                "psubw %%mm2, %%mm4                     \n\t"
                "movq %%mm5, %%mm2                      \n\t"
                "psubusw %%mm1, %%mm2                   \n\t"
                "psubw %%mm2, %%mm5                     \n\t"
3155
#endif
3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174
                "pxor %%mm6, %%mm4                      \n\t"
                "pxor %%mm7, %%mm5                      \n\t"
                "psubw %%mm6, %%mm4                     \n\t"
                "psubw %%mm7, %%mm5                     \n\t"
                "packsswb %%mm5, %%mm4                  \n\t"
                "movq %3, %%mm1                         \n\t"
                "pandn %%mm4, %%mm1                     \n\t"
                "movq (%0), %%mm0                       \n\t"
                "paddb   %%mm1, %%mm0                   \n\t"
                "movq %%mm0, (%0)                       \n\t"
                "movq (%0, %1), %%mm0                   \n\t"
                "psubb %%mm1, %%mm0                     \n\t"
                "movq %%mm0, (%0, %1)                   \n\t"

                : "+r" (temp_src)
                : "r" ((long)step), "m" (c->pQPb), "m"(eq_mask)
                : "%"REG_a, "%"REG_c
                );
        }
3175 3176 3177 3178 3179 3180 3181 3182
/*if(step==16){
    STOP_TIMER("step16")
}else{
    STOP_TIMER("stepX")
}*/
}
#endif //HAVE_MMX

Michael Niedermayer's avatar
Michael Niedermayer committed
3183 3184
static void RENAME(postProcess)(const uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
        const QP_STORE_T QPs[], int QPStride, int isColor, PPContext *c);
3185

3186
/**
Diego Biurrun's avatar
Diego Biurrun committed
3187 3188
 * Copies a block from src to dst and fixes the blacklevel.
 * levelFix == 0 -> do not touch the brighness & contrast
3189
 */
3190 3191
#undef SCALED_CPY

Michael Niedermayer's avatar
Michael Niedermayer committed
3192
static inline void RENAME(blockCopy)(uint8_t dst[], int dstStride, const uint8_t src[], int srcStride,
3193
        int levelFix, int64_t *packedOffsetAndScale)
3194
{
3195
#ifndef HAVE_MMX
3196
        int i;
3197
#endif
3198 3199
        if(levelFix)
        {
3200
#ifdef HAVE_MMX
3201 3202 3203 3204 3205 3206
                asm volatile(
                        "movq (%%"REG_a"), %%mm2        \n\t" // packedYOffset
                        "movq 8(%%"REG_a"), %%mm3       \n\t" // packedYScale
                        "lea (%2,%4), %%"REG_a"         \n\t"
                        "lea (%3,%5), %%"REG_d"         \n\t"
                        "pxor %%mm4, %%mm4              \n\t"
3207
#ifdef HAVE_MMX2
3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228
#define REAL_SCALED_CPY(src1, src2, dst1, dst2)                                                \
                        "movq " #src1 ", %%mm0          \n\t"\
                        "movq " #src1 ", %%mm5          \n\t"\
                        "movq " #src2 ", %%mm1          \n\t"\
                        "movq " #src2 ", %%mm6          \n\t"\
                        "punpcklbw %%mm0, %%mm0         \n\t"\
                        "punpckhbw %%mm5, %%mm5         \n\t"\
                        "punpcklbw %%mm1, %%mm1         \n\t"\
                        "punpckhbw %%mm6, %%mm6         \n\t"\
                        "pmulhuw %%mm3, %%mm0           \n\t"\
                        "pmulhuw %%mm3, %%mm5           \n\t"\
                        "pmulhuw %%mm3, %%mm1           \n\t"\
                        "pmulhuw %%mm3, %%mm6           \n\t"\
                        "psubw %%mm2, %%mm0             \n\t"\
                        "psubw %%mm2, %%mm5             \n\t"\
                        "psubw %%mm2, %%mm1             \n\t"\
                        "psubw %%mm2, %%mm6             \n\t"\
                        "packuswb %%mm5, %%mm0          \n\t"\
                        "packuswb %%mm6, %%mm1          \n\t"\
                        "movq %%mm0, " #dst1 "          \n\t"\
                        "movq %%mm1, " #dst2 "          \n\t"\
3229

3230
#else //HAVE_MMX2
3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255
#define REAL_SCALED_CPY(src1, src2, dst1, dst2)                                        \
                        "movq " #src1 ", %%mm0          \n\t"\
                        "movq " #src1 ", %%mm5          \n\t"\
                        "punpcklbw %%mm4, %%mm0         \n\t"\
                        "punpckhbw %%mm4, %%mm5         \n\t"\
                        "psubw %%mm2, %%mm0             \n\t"\
                        "psubw %%mm2, %%mm5             \n\t"\
                        "movq " #src2 ", %%mm1          \n\t"\
                        "psllw $6, %%mm0                \n\t"\
                        "psllw $6, %%mm5                \n\t"\
                        "pmulhw %%mm3, %%mm0            \n\t"\
                        "movq " #src2 ", %%mm6          \n\t"\
                        "pmulhw %%mm3, %%mm5            \n\t"\
                        "punpcklbw %%mm4, %%mm1         \n\t"\
                        "punpckhbw %%mm4, %%mm6         \n\t"\
                        "psubw %%mm2, %%mm1             \n\t"\
                        "psubw %%mm2, %%mm6             \n\t"\
                        "psllw $6, %%mm1                \n\t"\
                        "psllw $6, %%mm6                \n\t"\
                        "pmulhw %%mm3, %%mm1            \n\t"\
                        "pmulhw %%mm3, %%mm6            \n\t"\
                        "packuswb %%mm5, %%mm0          \n\t"\
                        "packuswb %%mm6, %%mm1          \n\t"\
                        "movq %%mm0, " #dst1 "          \n\t"\
                        "movq %%mm1, " #dst2 "          \n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
3256

3257
#endif //HAVE_MMX2
3258 3259
#define SCALED_CPY(src1, src2, dst1, dst2)\
   REAL_SCALED_CPY(src1, src2, dst1, dst2)
3260

3261
SCALED_CPY((%2)       , (%2, %4)      , (%3)       , (%3, %5))
3262 3263
SCALED_CPY((%2, %4, 2), (%%REGa, %4, 2), (%3, %5, 2), (%%REGd, %5, 2))
SCALED_CPY((%2, %4, 4), (%%REGa, %4, 4), (%3, %5, 4), (%%REGd, %5, 4))
3264 3265
                        "lea (%%"REG_a",%4,4), %%"REG_a"        \n\t"
                        "lea (%%"REG_d",%5,4), %%"REG_d"        \n\t"
3266
SCALED_CPY((%%REGa, %4), (%%REGa, %4, 2), (%%REGd, %5), (%%REGd, %5, 2))
Michael Niedermayer's avatar
Michael Niedermayer committed
3267 3268


3269 3270 3271 3272 3273 3274 3275 3276
                        : "=&a" (packedOffsetAndScale)
                        : "0" (packedOffsetAndScale),
                        "r"(src),
                        "r"(dst),
                        "r" ((long)srcStride),
                        "r" ((long)dstStride)
                        : "%"REG_d
                                        );
3277
#else //HAVE_MMX
3278 3279 3280
        for(i=0; i<8; i++)
                memcpy( &(dst[dstStride*i]),
                        &(src[srcStride*i]), BLOCK_SIZE);
3281
#endif //HAVE_MMX
3282 3283 3284
        }
        else
        {
3285
#ifdef HAVE_MMX
3286 3287 3288
        asm volatile(
                "lea (%0,%2), %%"REG_a"                 \n\t"
                "lea (%1,%3), %%"REG_d"                 \n\t"
Michael Niedermayer's avatar
Michael Niedermayer committed
3289

3290 3291 3292 3293 3294
#define REAL_SIMPLE_CPY(src1, src2, dst1, dst2)                              \
                "movq " #src1 ", %%mm0          \n\t"\
                "movq " #src2 ", %%mm1          \n\t"\
                "movq %%mm0, " #dst1 "          \n\t"\
                "movq %%mm1, " #dst2 "          \n\t"\
Michael Niedermayer's avatar
Michael Niedermayer committed
3295

3296 3297 3298
#define SIMPLE_CPY(src1, src2, dst1, dst2)\
   REAL_SIMPLE_CPY(src1, src2, dst1, dst2)

3299
SIMPLE_CPY((%0)       , (%0, %2)       , (%1)       , (%1, %3))
3300 3301
SIMPLE_CPY((%0, %2, 2), (%%REGa, %2, 2), (%1, %3, 2), (%%REGd, %3, 2))
SIMPLE_CPY((%0, %2, 4), (%%REGa, %2, 4), (%1, %3, 4), (%%REGd, %3, 4))
3302 3303
                "lea (%%"REG_a",%2,4), %%"REG_a"        \n\t"
                "lea (%%"REG_d",%3,4), %%"REG_d"        \n\t"
3304
SIMPLE_CPY((%%REGa, %2), (%%REGa, %2, 2), (%%REGd, %3), (%%REGd, %3, 2))
Michael Niedermayer's avatar
Michael Niedermayer committed
3305

3306 3307 3308 3309 3310 3311
                : : "r" (src),
                "r" (dst),
                "r" ((long)srcStride),
                "r" ((long)dstStride)
                : "%"REG_a, "%"REG_d
        );
3312
#else //HAVE_MMX
3313 3314 3315
        for(i=0; i<8; i++)
                memcpy( &(dst[dstStride*i]),
                        &(src[srcStride*i]), BLOCK_SIZE);
3316
#endif //HAVE_MMX
3317
        }
3318 3319
}

Michael Niedermayer's avatar
Michael Niedermayer committed
3320 3321 3322 3323 3324 3325
/**
 * Duplicates the given 8 src pixels ? times upward
 */
static inline void RENAME(duplicate)(uint8_t src[], int stride)
{
#ifdef HAVE_MMX
3326 3327 3328 3329 3330 3331 3332 3333 3334
        asm volatile(
                "movq (%0), %%mm0               \n\t"
                "add %1, %0                     \n\t"
                "movq %%mm0, (%0)               \n\t"
                "movq %%mm0, (%0, %1)           \n\t"
                "movq %%mm0, (%0, %1, 2)        \n\t"
                : "+r" (src)
                : "r" ((long)-stride)
        );
Michael Niedermayer's avatar
Michael Niedermayer committed
3335
#else
3336 3337 3338 3339 3340 3341 3342
        int i;
        uint8_t *p=src;
        for(i=0; i<3; i++)
        {
                p-= stride;
                memcpy(p, src, 8);
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
3343 3344
#endif
}
3345 3346 3347 3348

/**
 * Filters array of bytes (Y or U or V values)
 */
Michael Niedermayer's avatar
Michael Niedermayer committed
3349 3350
static void RENAME(postProcess)(const uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
        const QP_STORE_T QPs[], int QPStride, int isColor, PPContext *c2)
3351
{
3352
        DECLARE_ALIGNED(8, PPContext, c)= *c2; //copy to stack for faster access
3353
        int x,y;
3354
#ifdef COMPILE_TIME_MODE
3355
        const int mode= COMPILE_TIME_MODE;
3356
#else
3357
        const int mode= isColor ? c.ppMode.chromMode : c.ppMode.lumMode;
3358
#endif
3359 3360
        int black=0, white=255; // blackest black and whitest white in the picture
        int QPCorrecture= 256*256;
3361

3362
        int copyAhead;
3363
#ifdef HAVE_MMX
3364
        int i;
3365
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
3366

3367 3368
        const int qpHShift= isColor ? 4-c.hChromaSubSample : 4;
        const int qpVShift= isColor ? 4-c.vChromaSubSample : 4;
3369

3370 3371 3372 3373 3374
        //FIXME remove
        uint64_t * const yHistogram= c.yHistogram;
        uint8_t * const tempSrc= srcStride > 0 ? c.tempSrc : c.tempSrc - 23*srcStride;
        uint8_t * const tempDst= dstStride > 0 ? c.tempDst : c.tempDst - 23*dstStride;
        //const int mbWidth= isColor ? (width+7)>>3 : (width+15)>>4;
3375

Michael Niedermayer's avatar
Michael Niedermayer committed
3376
#ifdef HAVE_MMX
3377 3378 3379 3380 3381 3382 3383 3384
        for(i=0; i<57; i++){
                int offset= ((i*c.ppMode.baseDcDiff)>>8) + 1;
                int threshold= offset*2 + 1;
                c.mmxDcOffset[i]= 0x7F - offset;
                c.mmxDcThreshold[i]= 0x7F - threshold;
                c.mmxDcOffset[i]*= 0x0101010101010101LL;
                c.mmxDcThreshold[i]*= 0x0101010101010101LL;
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
3385
#endif
3386

3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418
        if(mode & CUBIC_IPOL_DEINT_FILTER) copyAhead=16;
        else if(   (mode & LINEAR_BLEND_DEINT_FILTER)
                || (mode & FFMPEG_DEINT_FILTER)
                || (mode & LOWPASS5_DEINT_FILTER)) copyAhead=14;
        else if(   (mode & V_DEBLOCK)
                || (mode & LINEAR_IPOL_DEINT_FILTER)
                || (mode & MEDIAN_DEINT_FILTER)
                || (mode & V_A_DEBLOCK)) copyAhead=13;
        else if(mode & V_X1_FILTER) copyAhead=11;
//        else if(mode & V_RK1_FILTER) copyAhead=10;
        else if(mode & DERING) copyAhead=9;
        else copyAhead=8;

        copyAhead-= 8;

        if(!isColor)
        {
                uint64_t sum= 0;
                int i;
                uint64_t maxClipped;
                uint64_t clipped;
                double scale;

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

                for(i=0; i<256; i++)
                {
                        sum+= yHistogram[i];
                }

Diego Biurrun's avatar
Diego Biurrun committed
3419
                /* We always get a completely black picture first. */
3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436
                maxClipped= (uint64_t)(sum * c.ppMode.maxClippedThreshold);

                clipped= sum;
                for(black=255; black>0; black--)
                {
                        if(clipped < maxClipped) break;
                        clipped-= yHistogram[black];
                }

                clipped= sum;
                for(white=0; white<256; white++)
                {
                        if(clipped < maxClipped) break;
                        clipped-= yHistogram[white];
                }

                scale= (double)(c.ppMode.maxAllowedY - c.ppMode.minAllowedY) / (double)(white-black);
3437 3438

#ifdef HAVE_MMX2
3439 3440
                c.packedYScale= (uint16_t)(scale*256.0 + 0.5);
                c.packedYOffset= (((black*c.packedYScale)>>8) - c.ppMode.minAllowedY) & 0xFFFF;
3441
#else
3442 3443
                c.packedYScale= (uint16_t)(scale*1024.0 + 0.5);
                c.packedYOffset= (black - c.ppMode.minAllowedY) & 0xFFFF;
3444 3445
#endif

3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464
                c.packedYOffset|= c.packedYOffset<<32;
                c.packedYOffset|= c.packedYOffset<<16;

                c.packedYScale|= c.packedYScale<<32;
                c.packedYScale|= c.packedYScale<<16;

                if(mode & LEVEL_FIX)        QPCorrecture= (int)(scale*256*256 + 0.5);
                else                        QPCorrecture= 256*256;
        }
        else
        {
                c.packedYScale= 0x0100010001000100LL;
                c.packedYOffset= 0;
                QPCorrecture= 256*256;
        }

        /* copy & deinterlace first row of blocks */
        y=-BLOCK_SIZE;
        {
Michael Niedermayer's avatar
Michael Niedermayer committed
3465
                const uint8_t *srcBlock= &(src[y*srcStride]);
3466 3467
                uint8_t *dstBlock= tempDst + dstStride;

3468
                // From this point on it is guaranteed that we can read and write 16 lines downward
3469 3470 3471 3472
                // finish 1 block before the next otherwise we might have a problem
                // with the L1 Cache of the P4 ... or only a few blocks at a time or soemthing
                for(x=0; x<width; x+=BLOCK_SIZE)
                {
Michael Niedermayer's avatar
Michael Niedermayer committed
3473 3474 3475

#ifdef HAVE_MMX2
/*
3476 3477 3478 3479
                        prefetchnta(srcBlock + (((x>>2)&6) + 5)*srcStride + 32);
                        prefetchnta(srcBlock + (((x>>2)&6) + 6)*srcStride + 32);
                        prefetcht0(dstBlock + (((x>>2)&6) + 5)*dstStride + 32);
                        prefetcht0(dstBlock + (((x>>2)&6) + 6)*dstStride + 32);
Michael Niedermayer's avatar
Michael Niedermayer committed
3480 3481
*/

3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499
                        asm(
                                "mov %4, %%"REG_a"              \n\t"
                                "shr $2, %%"REG_a"              \n\t"
                                "and $6, %%"REG_a"              \n\t"
                                "add %5, %%"REG_a"              \n\t"
                                "mov %%"REG_a", %%"REG_d"       \n\t"
                                "imul %1, %%"REG_a"             \n\t"
                                "imul %3, %%"REG_d"             \n\t"
                                "prefetchnta 32(%%"REG_a", %0)  \n\t"
                                "prefetcht0 32(%%"REG_d", %2)   \n\t"
                                "add %1, %%"REG_a"              \n\t"
                                "add %3, %%"REG_d"              \n\t"
                                "prefetchnta 32(%%"REG_a", %0)  \n\t"
                                "prefetcht0 32(%%"REG_d", %2)   \n\t"
                        :: "r" (srcBlock), "r" ((long)srcStride), "r" (dstBlock), "r" ((long)dstStride),
                        "g" ((long)x), "g" ((long)copyAhead)
                        : "%"REG_a, "%"REG_d
                        );
Michael Niedermayer's avatar
Michael Niedermayer committed
3500 3501

#elif defined(HAVE_3DNOW)
Diego Biurrun's avatar
Diego Biurrun committed
3502
//FIXME check if this is faster on an 3dnow chip or if it is faster without the prefetch or ...
3503 3504 3505 3506
/*                        prefetch(srcBlock + (((x>>3)&3) + 5)*srcStride + 32);
                        prefetch(srcBlock + (((x>>3)&3) + 9)*srcStride + 32);
                        prefetchw(dstBlock + (((x>>3)&3) + 5)*dstStride + 32);
                        prefetchw(dstBlock + (((x>>3)&3) + 9)*dstStride + 32);
Michael Niedermayer's avatar
Michael Niedermayer committed
3507 3508 3509
*/
#endif

3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528
                        RENAME(blockCopy)(dstBlock + dstStride*8, dstStride,
                                srcBlock + srcStride*8, srcStride, mode & LEVEL_FIX, &c.packedYOffset);

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

                        if(mode & LINEAR_IPOL_DEINT_FILTER)
                                RENAME(deInterlaceInterpolateLinear)(dstBlock, dstStride);
                        else if(mode & LINEAR_BLEND_DEINT_FILTER)
                                RENAME(deInterlaceBlendLinear)(dstBlock, dstStride, c.deintTemp + x);
                        else if(mode & MEDIAN_DEINT_FILTER)
                                RENAME(deInterlaceMedian)(dstBlock, dstStride);
                        else if(mode & CUBIC_IPOL_DEINT_FILTER)
                                RENAME(deInterlaceInterpolateCubic)(dstBlock, dstStride);
                        else if(mode & FFMPEG_DEINT_FILTER)
                                RENAME(deInterlaceFF)(dstBlock, dstStride, c.deintTemp + x);
                        else if(mode & LOWPASS5_DEINT_FILTER)
                                RENAME(deInterlaceL5)(dstBlock, dstStride, c.deintTemp + x, c.deintTemp + width + x);
/*                        else if(mode & CUBIC_BLEND_DEINT_FILTER)
                                RENAME(deInterlaceBlendCubic)(dstBlock, dstStride);
Michael Niedermayer's avatar
Michael Niedermayer committed
3529
*/
3530 3531 3532
                        dstBlock+=8;
                        srcBlock+=8;
                }
3533
                if(width==FFABS(dstStride))
3534 3535 3536 3537 3538 3539 3540 3541 3542 3543
                        linecpy(dst, tempDst + 9*dstStride, copyAhead, dstStride);
                else
                {
                        int i;
                        for(i=0; i<copyAhead; i++)
                        {
                                memcpy(dst + i*dstStride, tempDst + (9+i)*dstStride, width);
                        }
                }
        }
3544

3545 3546 3547
        for(y=0; y<height; y+=BLOCK_SIZE)
        {
                //1% speedup if these are here instead of the inner loop
Michael Niedermayer's avatar
Michael Niedermayer committed
3548
                const uint8_t *srcBlock= &(src[y*srcStride]);
3549
                uint8_t *dstBlock= &(dst[y*dstStride]);
3550
#ifdef HAVE_MMX
3551 3552
                uint8_t *tempBlock1= c.tempBlocks;
                uint8_t *tempBlock2= c.tempBlocks + 8;
3553
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
3554
                const int8_t *QPptr= &QPs[(y>>qpVShift)*QPStride];
3555
                int8_t *nonBQPptr= &c.nonBQPTable[(y>>qpVShift)*FFABS(QPStride)];
3556 3557 3558 3559 3560 3561 3562 3563 3564
                int QP=0;
                /* can we mess with a 8x16 block from srcBlock/dstBlock downwards and 1 line upwards
                   if not than use a temporary buffer */
                if(y+15 >= height)
                {
                        int i;
                        /* copy from line (copyAhead) to (copyAhead+7) of src, these will be copied with
                           blockcopy to dst later */
                        linecpy(tempSrc + srcStride*copyAhead, srcBlock + srcStride*copyAhead,
3565
                                FFMAX(height-y-copyAhead, 0), srcStride);
3566 3567

                        /* duplicate last line of src to fill the void upto line (copyAhead+7) */
3568
                        for(i=FFMAX(height-y, 8); i<copyAhead+8; i++)
3569
                                memcpy(tempSrc + srcStride*i, src + srcStride*(height-1), FFABS(srcStride));
3570 3571

                        /* copy up to (copyAhead+1) lines of dst (line -1 to (copyAhead-1))*/
3572
                        linecpy(tempDst, dstBlock - dstStride, FFMIN(height-y+1, copyAhead+1), dstStride);
3573 3574 3575

                        /* duplicate last line of dst to fill the void upto line (copyAhead) */
                        for(i=height-y+1; i<=copyAhead; i++)
3576
                                memcpy(tempDst + dstStride*i, dst + dstStride*(height-1), FFABS(dstStride));
3577 3578 3579 3580

                        dstBlock= tempDst + dstStride;
                        srcBlock= tempSrc;
                }
3581

3582
                // From this point on it is guaranteed that we can read and write 16 lines downward
3583 3584 3585 3586 3587
                // finish 1 block before the next otherwise we might have a problem
                // with the L1 Cache of the P4 ... or only a few blocks at a time or soemthing
                for(x=0; x<width; x+=BLOCK_SIZE)
                {
                        const int stride= dstStride;
3588
#ifdef HAVE_MMX
3589
                        uint8_t *tmpXchg;
3590
#endif
3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604
                        if(isColor)
                        {
                                QP= QPptr[x>>qpHShift];
                                c.nonBQP= nonBQPptr[x>>qpHShift];
                        }
                        else
                        {
                                QP= QPptr[x>>4];
                                QP= (QP* QPCorrecture + 256*128)>>16;
                                c.nonBQP= nonBQPptr[x>>4];
                                c.nonBQP= (c.nonBQP* QPCorrecture + 256*128)>>16;
                                yHistogram[ srcBlock[srcStride*12 + 4] ]++;
                        }
                        c.QP= QP;
3605
#ifdef HAVE_MMX
3606 3607 3608 3609 3610 3611 3612 3613 3614
                        asm volatile(
                                "movd %1, %%mm7         \n\t"
                                "packuswb %%mm7, %%mm7  \n\t" // 0, 0, 0, QP, 0, 0, 0, QP
                                "packuswb %%mm7, %%mm7  \n\t" // 0,QP, 0, QP, 0,QP, 0, QP
                                "packuswb %%mm7, %%mm7  \n\t" // QP,..., QP
                                "movq %%mm7, %0         \n\t"
                                : "=m" (c.pQPb)
                                : "r" (QP)
                        );
3615 3616 3617
#endif


3618
#ifdef HAVE_MMX2
Michael Niedermayer's avatar
Michael Niedermayer committed
3619
/*
3620 3621 3622 3623
                        prefetchnta(srcBlock + (((x>>2)&6) + 5)*srcStride + 32);
                        prefetchnta(srcBlock + (((x>>2)&6) + 6)*srcStride + 32);
                        prefetcht0(dstBlock + (((x>>2)&6) + 5)*dstStride + 32);
                        prefetcht0(dstBlock + (((x>>2)&6) + 6)*dstStride + 32);
Michael Niedermayer's avatar
Michael Niedermayer committed
3624 3625
*/

3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643
                        asm(
                                "mov %4, %%"REG_a"              \n\t"
                                "shr $2, %%"REG_a"              \n\t"
                                "and $6, %%"REG_a"              \n\t"
                                "add %5, %%"REG_a"              \n\t"
                                "mov %%"REG_a", %%"REG_d"       \n\t"
                                "imul %1, %%"REG_a"             \n\t"
                                "imul %3, %%"REG_d"             \n\t"
                                "prefetchnta 32(%%"REG_a", %0)  \n\t"
                                "prefetcht0 32(%%"REG_d", %2)   \n\t"
                                "add %1, %%"REG_a"              \n\t"
                                "add %3, %%"REG_d"              \n\t"
                                "prefetchnta 32(%%"REG_a", %0)  \n\t"
                                "prefetcht0 32(%%"REG_d", %2)   \n\t"
                        :: "r" (srcBlock), "r" ((long)srcStride), "r" (dstBlock), "r" ((long)dstStride),
                         "g" ((long)x), "g" ((long)copyAhead)
                        : "%"REG_a, "%"REG_d
                        );
Michael Niedermayer's avatar
Michael Niedermayer committed
3644

3645
#elif defined(HAVE_3DNOW)
Diego Biurrun's avatar
Diego Biurrun committed
3646
//FIXME check if this is faster on an 3dnow chip or if it is faster without the prefetch or ...
3647 3648 3649 3650
/*                        prefetch(srcBlock + (((x>>3)&3) + 5)*srcStride + 32);
                        prefetch(srcBlock + (((x>>3)&3) + 9)*srcStride + 32);
                        prefetchw(dstBlock + (((x>>3)&3) + 5)*dstStride + 32);
                        prefetchw(dstBlock + (((x>>3)&3) + 9)*dstStride + 32);
3651
*/
3652
#endif
3653

3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670
                        RENAME(blockCopy)(dstBlock + dstStride*copyAhead, dstStride,
                                srcBlock + srcStride*copyAhead, srcStride, mode & LEVEL_FIX, &c.packedYOffset);

                        if(mode & LINEAR_IPOL_DEINT_FILTER)
                                RENAME(deInterlaceInterpolateLinear)(dstBlock, dstStride);
                        else if(mode & LINEAR_BLEND_DEINT_FILTER)
                                RENAME(deInterlaceBlendLinear)(dstBlock, dstStride, c.deintTemp + x);
                        else if(mode & MEDIAN_DEINT_FILTER)
                                RENAME(deInterlaceMedian)(dstBlock, dstStride);
                        else if(mode & CUBIC_IPOL_DEINT_FILTER)
                                RENAME(deInterlaceInterpolateCubic)(dstBlock, dstStride);
                        else if(mode & FFMPEG_DEINT_FILTER)
                                RENAME(deInterlaceFF)(dstBlock, dstStride, c.deintTemp + x);
                        else if(mode & LOWPASS5_DEINT_FILTER)
                                RENAME(deInterlaceL5)(dstBlock, dstStride, c.deintTemp + x, c.deintTemp + width + x);
/*                        else if(mode & CUBIC_BLEND_DEINT_FILTER)
                                RENAME(deInterlaceBlendCubic)(dstBlock, dstStride);
3671
*/
3672

3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689
                        /* only deblock if we have 2 blocks */
                        if(y + 8 < height)
                        {
                                if(mode & V_X1_FILTER)
                                        RENAME(vertX1Filter)(dstBlock, stride, &c);
                                else if(mode & V_DEBLOCK)
                                {
                                        const int t= RENAME(vertClassify)(dstBlock, stride, &c);

                                        if(t==1)
                                                RENAME(doVertLowPass)(dstBlock, stride, &c);
                                        else if(t==2)
                                                RENAME(doVertDefFilter)(dstBlock, stride, &c);
                                }else if(mode & V_A_DEBLOCK){
                                        RENAME(do_a_deblock)(dstBlock, stride, 1, &c);
                                }
                        }
Michael Niedermayer's avatar
Michael Niedermayer committed
3690

3691
#ifdef HAVE_MMX
3692
                        RENAME(transpose1)(tempBlock1, tempBlock2, dstBlock, dstStride);
3693
#endif
3694 3695 3696
                        /* check if we have a previous block to deblock it with dstBlock */
                        if(x - 8 >= 0)
                        {
3697
#ifdef HAVE_MMX
3698 3699 3700 3701
                                if(mode & H_X1_FILTER)
                                        RENAME(vertX1Filter)(tempBlock1, 16, &c);
                                else if(mode & H_DEBLOCK)
                                {
3702
//START_TIMER
3703
                                        const int t= RENAME(vertClassify)(tempBlock1, 16, &c);
3704 3705
//STOP_TIMER("dc & minmax")
                                        if(t==1)
3706 3707 3708 3709 3710 3711
                                                RENAME(doVertLowPass)(tempBlock1, 16, &c);
                                        else if(t==2)
                                                RENAME(doVertDefFilter)(tempBlock1, 16, &c);
                                }else if(mode & H_A_DEBLOCK){
                                        RENAME(do_a_deblock)(tempBlock1, 16, 1, &c);
                                }
3712

3713
                                RENAME(transpose2)(dstBlock-4, dstStride, tempBlock1 + 4*16);
3714 3715

#else
3716 3717 3718 3719
                                if(mode & H_X1_FILTER)
                                        horizX1Filter(dstBlock-4, stride, QP);
                                else if(mode & H_DEBLOCK)
                                {
3720
#ifdef HAVE_ALTIVEC
3721
                                        DECLARE_ALIGNED(16, unsigned char, tempBlock[272]);
3722
                                        transpose_16x8_char_toPackedAlign_altivec(tempBlock, dstBlock - (4 + 1), stride);
3723

3724 3725 3726
                                        const int t=vertClassify_altivec(tempBlock-48, 16, &c);
                                        if(t==1) {
                                                doVertLowPass_altivec(tempBlock-48, 16, &c);
3727 3728
                                                transpose_8x16_char_fromPackedAlign_altivec(dstBlock - (4 + 1), tempBlock, stride);
                                        }
3729 3730
                                        else if(t==2) {
                                                doVertDefFilter_altivec(tempBlock-48, 16, &c);
3731 3732 3733
                                                transpose_8x16_char_fromPackedAlign_altivec(dstBlock - (4 + 1), tempBlock, stride);
                                        }
#else
3734
                                        const int t= RENAME(horizClassify)(dstBlock-4, stride, &c);
3735

3736 3737 3738 3739
                                        if(t==1)
                                                RENAME(doHorizLowPass)(dstBlock-4, stride, &c);
                                        else if(t==2)
                                                RENAME(doHorizDefFilter)(dstBlock-4, stride, &c);
3740
#endif
3741 3742 3743
                                }else if(mode & H_A_DEBLOCK){
                                        RENAME(do_a_deblock)(dstBlock-8, 1, stride, &c);
                                }
3744
#endif //HAVE_MMX
3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761
                                if(mode & DERING)
                                {
                                //FIXME filter first line
                                        if(y>0) RENAME(dering)(dstBlock - stride - 8, stride, &c);
                                }

                                if(mode & TEMP_NOISE_FILTER)
                                {
                                        RENAME(tempNoiseReducer)(dstBlock-8, stride,
                                                c.tempBlured[isColor] + y*dstStride + x,
                                                c.tempBluredPast[isColor] + (y>>3)*256 + (x>>3),
                                                c.ppMode.maxTmpNoise);
                                }
                        }

                        dstBlock+=8;
                        srcBlock+=8;
3762

3763
#ifdef HAVE_MMX
3764 3765 3766
                        tmpXchg= tempBlock1;
                        tempBlock1= tempBlock2;
                        tempBlock2 = tmpXchg;
3767
#endif
3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786
                }

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

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

                /* did we use a tmp buffer for the last lines*/
                if(y+15 >= height)
                {
                        uint8_t *dstBlock= &(dst[y*dstStride]);
3787
                        if(width==FFABS(dstStride))
3788 3789 3790 3791 3792 3793 3794 3795 3796 3797
                                linecpy(dstBlock, tempDst + dstStride, height-y, dstStride);
                        else
                        {
                                int i;
                                for(i=0; i<height-y; i++)
                                {
                                        memcpy(dstBlock + i*dstStride, tempDst + (i+1)*dstStride, width);
                                }
                        }
                }
3798
/*
3799 3800 3801 3802 3803 3804 3805 3806 3807 3808
                for(x=0; x<width; x+=32)
                {
                        volatile int i;
                        i+=        + dstBlock[x + 7*dstStride] + dstBlock[x + 8*dstStride]
                                + dstBlock[x + 9*dstStride] + dstBlock[x +10*dstStride]
                                + dstBlock[x +11*dstStride] + dstBlock[x +12*dstStride];
//                                + dstBlock[x +13*dstStride]
//                                + dstBlock[x +14*dstStride] + dstBlock[x +15*dstStride];
                }*/
        }
3809
#ifdef HAVE_3DNOW
3810
        asm volatile("femms");
3811
#elif defined (HAVE_MMX)
3812
        asm volatile("emms");
3813 3814
#endif

3815
#ifdef DEBUG_BRIGHTNESS
3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839
        if(!isColor)
        {
                int max=1;
                int i;
                for(i=0; i<256; i++)
                        if(yHistogram[i] > max) max=yHistogram[i];

                for(i=1; i<256; i++)
                {
                        int x;
                        int start=yHistogram[i-1]/(max/256+1);
                        int end=yHistogram[i]/(max/256+1);
                        int inc= end > start ? 1 : -1;
                        for(x=start; x!=end+inc; x+=inc)
                                dst[ i*dstStride + x]+=128;
                }

                for(i=0; i<100; i+=2)
                {
                        dst[ (white)*dstStride + i]+=128;
                        dst[ (black)*dstStride + i]+=128;
                }

        }
3840 3841
#endif

3842
        *c2= c; //copy local context back
3843

3844
}