Statistics
| Branch: | Revision:

ffmpeg / libavfilter / libmpcodecs / vf_fspp.c @ e4852fb3

History | View | Annotate | Download (71.3 KB)

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

    
22
/*
23
 * This implementation is based on an algorithm described in
24
 * "Aria Nosratinia Embedded Post-Processing for
25
 * Enhancement of Compressed Images (1999)"
26
 * (http://citeseer.nj.nec.com/nosratinia99embedded.html)
27
 * Futher, with splitting (i)dct into hor/ver passes, one of them can be
28
 * performed once per block, not pixel. This allows for much better speed.
29
 */
30

    
31
/*
32
  Heavily optimized version of SPP filter by Nikolaj
33
 */
34

    
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#include <inttypes.h>
39
#include <math.h>
40

    
41
#include "config.h"
42

    
43
#include "mp_msg.h"
44
#include "cpudetect.h"
45
#include "img_format.h"
46
#include "mp_image.h"
47
#include "vf.h"
48
#include "vd_ffmpeg.h"
49
#include "libvo/fastmemcpy.h"
50

    
51
#include "libavutil/internal.h"
52
#include "libavutil/intreadwrite.h"
53
#include "libavutil/mem.h"
54
#include "libavcodec/avcodec.h"
55
#include "libavcodec/dsputil.h"
56

    
57
#undef free
58
#undef malloc
59

    
60
//===========================================================================//
61
#define BLOCKSZ 12
62

    
63
static const short custom_threshold[64]=
64
// values (296) can't be too high
65
// -it causes too big quant dependence
66
// or maybe overflow(check), which results in some flashing
67
{ 71, 296, 295, 237,  71,  40,  38,  19,
68
  245, 193, 185, 121, 102,  73,  53,  27,
69
  158, 129, 141, 107,  97,  73,  50,  26,
70
  102, 116, 109,  98,  82,  66,  45,  23,
71
  71,  94,  95,  81,  70,  56,  38,  20,
72
  56,  77,  74,  66,  56,  44,  30,  15,
73
  38,  53,  50,  45,  38,  30,  21,  11,
74
  20,  27,  26,  23,  20,  15,  11,   5
75
};
76

    
77
static const uint8_t  __attribute__((aligned(32))) dither[8][8]={
78
    {  0,  48,  12,  60,   3,  51,  15,  63, },
79
    { 32,  16,  44,  28,  35,  19,  47,  31, },
80
    {  8,  56,   4,  52,  11,  59,   7,  55, },
81
    { 40,  24,  36,  20,  43,  27,  39,  23, },
82
    {  2,  50,  14,  62,   1,  49,  13,  61, },
83
    { 34,  18,  46,  30,  33,  17,  45,  29, },
84
    { 10,  58,   6,  54,   9,  57,   5,  53, },
85
    { 42,  26,  38,  22,  41,  25,  37,  21, },
86
};
87

    
88
struct vf_priv_s { //align 16 !
89
    uint64_t threshold_mtx_noq[8*2];
90
    uint64_t threshold_mtx[8*2];//used in both C & MMX (& later SSE2) versions
91

    
92
    int log2_count;
93
    int temp_stride;
94
    int qp;
95
    int mpeg2;
96
    int prev_q;
97
    uint8_t *src;
98
    int16_t *temp;
99
    int bframes;
100
    char *non_b_qp;
101
};
102

    
103

    
104
#if !HAVE_MMX
105

    
106
//This func reads from 1 slice, 1 and clears 0 & 1
107
static void store_slice_c(uint8_t *dst, int16_t *src, int dst_stride, int src_stride, int width, int height, int log2_scale)
108
{int y, x;
109
#define STORE(pos)                                                        \
110
    temp= (src[x + pos] + (d[pos]>>log2_scale))>>(6-log2_scale);        \
111
    src[x + pos]=src[x + pos - 8*src_stride]=0;                                \
112
    if(temp & 0x100) temp= ~(temp>>31);                                        \
113
    dst[x + pos]= temp;
114

    
115
    for(y=0; y<height; y++){
116
        const uint8_t *d= dither[y];
117
        for(x=0; x<width; x+=8){
118
            int temp;
119
            STORE(0);
120
            STORE(1);
121
            STORE(2);
122
            STORE(3);
123
            STORE(4);
124
            STORE(5);
125
            STORE(6);
126
            STORE(7);
127
        }
128
        src+=src_stride;
129
        dst+=dst_stride;
130
    }
131
}
132

    
133
//This func reads from 2 slices, 0 & 2  and clears 2-nd
134
static void store_slice2_c(uint8_t *dst, int16_t *src, int dst_stride, int src_stride, int width, int height, int log2_scale)
135
{int y, x;
136
#define STORE2(pos)                                                        \
137
    temp= (src[x + pos] + src[x + pos + 16*src_stride] + (d[pos]>>log2_scale))>>(6-log2_scale);        \
138
    src[x + pos + 16*src_stride]=0;                                        \
139
    if(temp & 0x100) temp= ~(temp>>31);                                        \
140
    dst[x + pos]= temp;
141

    
142
    for(y=0; y<height; y++){
143
        const uint8_t *d= dither[y];
144
        for(x=0; x<width; x+=8){
145
            int temp;
146
            STORE2(0);
147
            STORE2(1);
148
            STORE2(2);
149
            STORE2(3);
150
            STORE2(4);
151
            STORE2(5);
152
            STORE2(6);
153
            STORE2(7);
154
        }
155
        src+=src_stride;
156
        dst+=dst_stride;
157
    }
158
}
159

    
160
static void mul_thrmat_c(struct vf_priv_s *p,int q)
161
{
162
    int a;
163
    for(a=0;a<64;a++)
164
        ((short*)p->threshold_mtx)[a]=q * ((short*)p->threshold_mtx_noq)[a];//ints faster in C
165
}
166

    
167
static void column_fidct_c(int16_t* thr_adr, DCTELEM *data, DCTELEM *output, int cnt);
168
static void row_idct_c(DCTELEM* workspace,
169
                       int16_t* output_adr, int output_stride, int cnt);
170
static void row_fdct_c(DCTELEM *data, const uint8_t *pixels, int line_size, int cnt);
171

    
172
//this is rather ugly, but there is no need for function pointers
173
#define store_slice_s store_slice_c
174
#define store_slice2_s store_slice2_c
175
#define mul_thrmat_s mul_thrmat_c
176
#define column_fidct_s column_fidct_c
177
#define row_idct_s row_idct_c
178
#define row_fdct_s row_fdct_c
179

    
180
#else /* HAVE_MMX */
181

    
182
//This func reads from 1 slice, 1 and clears 0 & 1
183
static void store_slice_mmx(uint8_t *dst, int16_t *src, long dst_stride, long src_stride, long width, long height, long log2_scale)
184
{
185
    const uint8_t *od=&dither[0][0];
186
    const uint8_t *end=&dither[height][0];
187
    width = (width+7)&~7;
188
    dst_stride-=width;
189
    //src_stride=(src_stride-width)*2;
190
    __asm__ volatile(
191
        "mov %5, %%"REG_d"                \n\t"
192
        "mov %6, %%"REG_S"                \n\t"
193
        "mov %7, %%"REG_D"                \n\t"
194
        "mov %1, %%"REG_a"                \n\t"
195
        "movd %%"REG_d", %%mm5             \n\t"
196
        "xor $-1, %%"REG_d"              \n\t"
197
        "mov %%"REG_a", %%"REG_c"             \n\t"
198
        "add $7, %%"REG_d"               \n\t"
199
        "neg %%"REG_a"                   \n\t"
200
        "sub %0, %%"REG_c"            \n\t"
201
        "add %%"REG_c", %%"REG_c"             \n\t"
202
        "movd %%"REG_d", %%mm2             \n\t"
203
        "mov %%"REG_c", %1       \n\t"
204
        "mov %2, %%"REG_d"               \n\t"
205
        "shl $4, %%"REG_a"               \n\t"
206

    
207
        "2:                        \n\t"
208
        "movq (%%"REG_d"), %%mm3           \n\t"
209
        "movq %%mm3, %%mm4             \n\t"
210
        "pxor %%mm7, %%mm7             \n\t"
211
        "punpcklbw %%mm7, %%mm3        \n\t"
212
        "punpckhbw %%mm7, %%mm4        \n\t"
213
        "mov %0, %%"REG_c"            \n\t"
214
        "psraw %%mm5, %%mm3            \n\t"
215
        "psraw %%mm5, %%mm4            \n\t"
216
        "1:                        \n\t"
217
        "movq %%mm7, (%%"REG_S",%%"REG_a",)     \n\t"
218
        "movq (%%"REG_S"), %%mm0           \n\t"
219
        "movq 8(%%"REG_S"), %%mm1          \n\t"
220

    
221
        "movq %%mm7, 8(%%"REG_S",%%"REG_a",)    \n\t"
222
        "paddw %%mm3, %%mm0            \n\t"
223
        "paddw %%mm4, %%mm1            \n\t"
224

    
225
        "movq %%mm7, (%%"REG_S")           \n\t"
226
        "psraw %%mm2, %%mm0            \n\t"
227
        "psraw %%mm2, %%mm1            \n\t"
228

    
229
        "movq %%mm7, 8(%%"REG_S")          \n\t"
230
        "packuswb %%mm1, %%mm0         \n\t"
231
        "add $16, %%"REG_S"              \n\t"
232

    
233
        "movq %%mm0, (%%"REG_D")           \n\t"
234
        "add $8, %%"REG_D"               \n\t"
235
        "sub $8, %%"REG_c"               \n\t"
236
        "jg 1b                      \n\t"
237
        "add %1, %%"REG_S"       \n\t"
238
        "add $8, %%"REG_d"               \n\t"
239
        "add %3, %%"REG_D"       \n\t"
240
        "cmp %4, %%"REG_d"           \n\t"
241
        "jl 2b                      \n\t"
242

    
243
        :
244
        : "m" (width), "m" (src_stride), "erm" (od), "m" (dst_stride), "erm" (end),
245
          "m" (log2_scale), "m" (src), "m" (dst) //input
246
        : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S, "%"REG_D
247
        );
248
}
249

    
250
//This func reads from 2 slices, 0 & 2  and clears 2-nd
251
static void store_slice2_mmx(uint8_t *dst, int16_t *src, long dst_stride, long src_stride, long width, long height, long log2_scale)
252
{
253
    const uint8_t *od=&dither[0][0];
254
    const uint8_t *end=&dither[height][0];
255
    width = (width+7)&~7;
256
    dst_stride-=width;
257
    //src_stride=(src_stride-width)*2;
258
    __asm__ volatile(
259
        "mov %5, %%"REG_d"                \n\t"
260
        "mov %6, %%"REG_S"                \n\t"
261
        "mov %7, %%"REG_D"                \n\t"
262
        "mov %1, %%"REG_a"            \n\t"
263
        "movd %%"REG_d", %%mm5             \n\t"
264
        "xor $-1, %%"REG_d"              \n\t"
265
        "mov %%"REG_a", %%"REG_c"             \n\t"
266
        "add $7, %%"REG_d"               \n\t"
267
        "sub %0, %%"REG_c"            \n\t"
268
        "add %%"REG_c", %%"REG_c"             \n\t"
269
        "movd %%"REG_d", %%mm2             \n\t"
270
        "mov %%"REG_c", %1       \n\t"
271
        "mov %2, %%"REG_d"               \n\t"
272
        "shl $5, %%"REG_a"               \n\t"
273

    
274
        "2:                        \n\t"
275
        "movq (%%"REG_d"), %%mm3           \n\t"
276
        "movq %%mm3, %%mm4             \n\t"
277
        "pxor %%mm7, %%mm7             \n\t"
278
        "punpcklbw %%mm7, %%mm3        \n\t"
279
        "punpckhbw %%mm7, %%mm4        \n\t"
280
        "mov %0, %%"REG_c"            \n\t"
281
        "psraw %%mm5, %%mm3            \n\t"
282
        "psraw %%mm5, %%mm4            \n\t"
283
        "1:                        \n\t"
284
        "movq (%%"REG_S"), %%mm0           \n\t"
285
        "movq 8(%%"REG_S"), %%mm1          \n\t"
286
        "paddw %%mm3, %%mm0            \n\t"
287

    
288
        "paddw (%%"REG_S",%%"REG_a",), %%mm0    \n\t"
289
        "paddw %%mm4, %%mm1            \n\t"
290
        "movq 8(%%"REG_S",%%"REG_a",), %%mm6    \n\t"
291

    
292
        "movq %%mm7, (%%"REG_S",%%"REG_a",)     \n\t"
293
        "psraw %%mm2, %%mm0            \n\t"
294
        "paddw %%mm6, %%mm1            \n\t"
295

    
296
        "movq %%mm7, 8(%%"REG_S",%%"REG_a",)    \n\t"
297
        "psraw %%mm2, %%mm1            \n\t"
298
        "packuswb %%mm1, %%mm0         \n\t"
299

    
300
        "movq %%mm0, (%%"REG_D")           \n\t"
301
        "add $16, %%"REG_S"              \n\t"
302
        "add $8, %%"REG_D"               \n\t"
303
        "sub $8, %%"REG_c"               \n\t"
304
        "jg 1b                      \n\t"
305
        "add %1, %%"REG_S"       \n\t"
306
        "add $8, %%"REG_d"               \n\t"
307
        "add %3, %%"REG_D"       \n\t"
308
        "cmp %4, %%"REG_d"           \n\t"
309
        "jl 2b                      \n\t"
310

    
311
        :
312
        : "m" (width), "m" (src_stride), "erm" (od), "m" (dst_stride), "erm" (end),
313
          "m" (log2_scale), "m" (src), "m" (dst) //input
314
        : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_D, "%"REG_S
315
        );
316
}
317

    
318
static void mul_thrmat_mmx(struct vf_priv_s *p, int q)
319
{
320
    uint64_t *adr=&p->threshold_mtx_noq[0];
321
    __asm__ volatile(
322
        "movd %0, %%mm7                \n\t"
323
        "add $8*8*2, %%"REG_D"            \n\t"
324
        "movq 0*8(%%"REG_S"), %%mm0        \n\t"
325
        "punpcklwd %%mm7, %%mm7        \n\t"
326
        "movq 1*8(%%"REG_S"), %%mm1        \n\t"
327
        "punpckldq %%mm7, %%mm7        \n\t"
328
        "pmullw %%mm7, %%mm0           \n\t"
329

    
330
        "movq 2*8(%%"REG_S"), %%mm2        \n\t"
331
        "pmullw %%mm7, %%mm1           \n\t"
332

    
333
        "movq 3*8(%%"REG_S"), %%mm3        \n\t"
334
        "pmullw %%mm7, %%mm2           \n\t"
335

    
336
        "movq %%mm0, 0*8(%%"REG_D")        \n\t"
337
        "movq 4*8(%%"REG_S"), %%mm4        \n\t"
338
        "pmullw %%mm7, %%mm3           \n\t"
339

    
340
        "movq %%mm1, 1*8(%%"REG_D")        \n\t"
341
        "movq 5*8(%%"REG_S"), %%mm5        \n\t"
342
        "pmullw %%mm7, %%mm4           \n\t"
343

    
344
        "movq %%mm2, 2*8(%%"REG_D")        \n\t"
345
        "movq 6*8(%%"REG_S"), %%mm6        \n\t"
346
        "pmullw %%mm7, %%mm5           \n\t"
347

    
348
        "movq %%mm3, 3*8(%%"REG_D")        \n\t"
349
        "movq 7*8+0*8(%%"REG_S"), %%mm0    \n\t"
350
        "pmullw %%mm7, %%mm6           \n\t"
351

    
352
        "movq %%mm4, 4*8(%%"REG_D")        \n\t"
353
        "movq 7*8+1*8(%%"REG_S"), %%mm1    \n\t"
354
        "pmullw %%mm7, %%mm0           \n\t"
355

    
356
        "movq %%mm5, 5*8(%%"REG_D")        \n\t"
357
        "movq 7*8+2*8(%%"REG_S"), %%mm2    \n\t"
358
        "pmullw %%mm7, %%mm1           \n\t"
359

    
360
        "movq %%mm6, 6*8(%%"REG_D")        \n\t"
361
        "movq 7*8+3*8(%%"REG_S"), %%mm3    \n\t"
362
        "pmullw %%mm7, %%mm2           \n\t"
363

    
364
        "movq %%mm0, 7*8+0*8(%%"REG_D")    \n\t"
365
        "movq 7*8+4*8(%%"REG_S"), %%mm4    \n\t"
366
        "pmullw %%mm7, %%mm3           \n\t"
367

    
368
        "movq %%mm1, 7*8+1*8(%%"REG_D")    \n\t"
369
        "movq 7*8+5*8(%%"REG_S"), %%mm5    \n\t"
370
        "pmullw %%mm7, %%mm4           \n\t"
371

    
372
        "movq %%mm2, 7*8+2*8(%%"REG_D")    \n\t"
373
        "movq 7*8+6*8(%%"REG_S"), %%mm6    \n\t"
374
        "pmullw %%mm7, %%mm5           \n\t"
375

    
376
        "movq %%mm3, 7*8+3*8(%%"REG_D")    \n\t"
377
        "movq 14*8+0*8(%%"REG_S"), %%mm0   \n\t"
378
        "pmullw %%mm7, %%mm6           \n\t"
379

    
380
        "movq %%mm4, 7*8+4*8(%%"REG_D")    \n\t"
381
        "movq 14*8+1*8(%%"REG_S"), %%mm1   \n\t"
382
        "pmullw %%mm7, %%mm0           \n\t"
383

    
384
        "movq %%mm5, 7*8+5*8(%%"REG_D")    \n\t"
385
        "pmullw %%mm7, %%mm1           \n\t"
386

    
387
        "movq %%mm6, 7*8+6*8(%%"REG_D")    \n\t"
388
        "movq %%mm0, 14*8+0*8(%%"REG_D")   \n\t"
389
        "movq %%mm1, 14*8+1*8(%%"REG_D")   \n\t"
390

    
391
        : "+g" (q), "+S" (adr), "+D" (adr)
392
        :
393
        );
394
}
395

    
396
static void column_fidct_mmx(int16_t* thr_adr,  DCTELEM *data,  DCTELEM *output,  int cnt);
397
static void row_idct_mmx(DCTELEM* workspace,
398
                         int16_t* output_adr,  int output_stride,  int cnt);
399
static void row_fdct_mmx(DCTELEM *data,  const uint8_t *pixels,  int line_size,  int cnt);
400

    
401
#define store_slice_s store_slice_mmx
402
#define store_slice2_s store_slice2_mmx
403
#define mul_thrmat_s mul_thrmat_mmx
404
#define column_fidct_s column_fidct_mmx
405
#define row_idct_s row_idct_mmx
406
#define row_fdct_s row_fdct_mmx
407
#endif // HAVE_MMX
408

    
409
static void filter(struct vf_priv_s *p, uint8_t *dst, uint8_t *src,
410
                   int dst_stride, int src_stride,
411
                   int width, int height,
412
                   uint8_t *qp_store, int qp_stride, int is_luma)
413
{
414
    int x, x0, y, es, qy, t;
415
    const int stride= is_luma ? p->temp_stride : (width+16);//((width+16+15)&(~15))
416
    const int step=6-p->log2_count;
417
    const int qps= 3 + is_luma;
418
    int32_t __attribute__((aligned(32))) block_align[4*8*BLOCKSZ+ 4*8*BLOCKSZ];
419
    DCTELEM *block= (DCTELEM *)block_align;
420
    DCTELEM *block3=(DCTELEM *)(block_align+4*8*BLOCKSZ);
421

    
422
    memset(block3, 0, 4*8*BLOCKSZ);
423

    
424
    //p->src=src-src_stride*8-8;//!
425
    if (!src || !dst) return; // HACK avoid crash for Y8 colourspace
426
    for(y=0; y<height; y++){
427
        int index= 8 + 8*stride + y*stride;
428
        fast_memcpy(p->src + index, src + y*src_stride, width);//this line can be avoided by using DR & user fr.buffers
429
        for(x=0; x<8; x++){
430
            p->src[index         - x - 1]= p->src[index +         x    ];
431
            p->src[index + width + x    ]= p->src[index + width - x - 1];
432
        }
433
    }
434
    for(y=0; y<8; y++){
435
        fast_memcpy(p->src + (      7-y)*stride, p->src + (      y+8)*stride, stride);
436
        fast_memcpy(p->src + (height+8+y)*stride, p->src + (height-y+7)*stride, stride);
437
    }
438
    //FIXME (try edge emu)
439

    
440
    for(y=8; y<24; y++)
441
        memset(p->temp+ 8 +y*stride, 0,width*sizeof(int16_t));
442

    
443
    for(y=step; y<height+8; y+=step){    //step= 1,2
444
        qy=y-4;
445
        if (qy>height-1) qy=height-1;
446
        if (qy<0) qy=0;
447
        qy=(qy>>qps)*qp_stride;
448
        row_fdct_s(block, p->src + y*stride +2-(y&1), stride, 2);
449
        for(x0=0; x0<width+8-8*(BLOCKSZ-1); x0+=8*(BLOCKSZ-1)){
450
            row_fdct_s(block+8*8, p->src + y*stride+8+x0 +2-(y&1), stride, 2*(BLOCKSZ-1));
451
            if(p->qp)
452
                column_fidct_s((int16_t*)(&p->threshold_mtx[0]), block+0*8, block3+0*8, 8*(BLOCKSZ-1)); //yes, this is a HOTSPOT
453
            else
454
                for (x=0; x<8*(BLOCKSZ-1); x+=8) {
455
                    t=x+x0-2; //correct t=x+x0-2-(y&1), but its the same
456
                    if (t<0) t=0;//t always < width-2
457
                    t=qp_store[qy+(t>>qps)];
458
                    t=norm_qscale(t, p->mpeg2);
459
                    if (t!=p->prev_q) p->prev_q=t, mul_thrmat_s(p, t);
460
                    column_fidct_s((int16_t*)(&p->threshold_mtx[0]), block+x*8, block3+x*8, 8); //yes, this is a HOTSPOT
461
                }
462
            row_idct_s(block3+0*8, p->temp + (y&15)*stride+x0+2-(y&1), stride, 2*(BLOCKSZ-1));
463
            memmove(block, block+(BLOCKSZ-1)*64, 8*8*sizeof(DCTELEM)); //cycling
464
            memmove(block3, block3+(BLOCKSZ-1)*64, 6*8*sizeof(DCTELEM));
465
        }
466
        //
467
        es=width+8-x0; //  8, ...
468
        if (es>8)
469
            row_fdct_s(block+8*8, p->src + y*stride+8+x0 +2-(y&1), stride, (es-4)>>2);
470
        column_fidct_s((int16_t*)(&p->threshold_mtx[0]), block, block3, es&(~1));
471
        row_idct_s(block3+0*8, p->temp + (y&15)*stride+x0+2-(y&1), stride, es>>2);
472
        {const int y1=y-8+step;//l5-7  l4-6
473
            if (!(y1&7) && y1) {
474
                if (y1&8) store_slice_s(dst + (y1-8)*dst_stride, p->temp+ 8 +8*stride,
475
                                        dst_stride, stride, width, 8, 5-p->log2_count);
476
                else store_slice2_s(dst + (y1-8)*dst_stride, p->temp+ 8 +0*stride,
477
                                    dst_stride, stride, width, 8, 5-p->log2_count);
478
            } }
479
    }
480

    
481
    if (y&7) {  // == height & 7
482
        if (y&8) store_slice_s(dst + ((y-8)&~7)*dst_stride, p->temp+ 8 +8*stride,
483
                               dst_stride, stride, width, y&7, 5-p->log2_count);
484
        else store_slice2_s(dst + ((y-8)&~7)*dst_stride, p->temp+ 8 +0*stride,
485
                            dst_stride, stride, width, y&7, 5-p->log2_count);
486
    }
487
}
488

    
489
static int config(struct vf_instance *vf,
490
                  int width, int height, int d_width, int d_height,
491
                  unsigned int flags, unsigned int outfmt)
492
{
493
    int h= (height+16+15)&(~15);
494

    
495
    vf->priv->temp_stride= (width+16+15)&(~15);
496
    vf->priv->temp= (int16_t*)av_mallocz(vf->priv->temp_stride*3*8*sizeof(int16_t));
497
    //this can also be avoided, see above
498
    vf->priv->src = (uint8_t*)av_malloc(vf->priv->temp_stride*h*sizeof(uint8_t));
499

    
500
    return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
501
}
502

    
503
static void get_image(struct vf_instance *vf, mp_image_t *mpi)
504
{
505
    if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
506
    // ok, we can do pp in-place (or pp disabled):
507
    vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
508
                          mpi->type, mpi->flags, mpi->width, mpi->height);
509
    mpi->planes[0]=vf->dmpi->planes[0];
510
    mpi->stride[0]=vf->dmpi->stride[0];
511
    mpi->width=vf->dmpi->width;
512
    if(mpi->flags&MP_IMGFLAG_PLANAR){
513
        mpi->planes[1]=vf->dmpi->planes[1];
514
        mpi->planes[2]=vf->dmpi->planes[2];
515
        mpi->stride[1]=vf->dmpi->stride[1];
516
        mpi->stride[2]=vf->dmpi->stride[2];
517
    }
518
    mpi->flags|=MP_IMGFLAG_DIRECT;
519
}
520

    
521
static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
522
{
523
    mp_image_t *dmpi;
524
    if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
525
        // no DR, so get a new image! hope we'll get DR buffer:
526
        dmpi=vf_get_image(vf->next,mpi->imgfmt,
527
                          MP_IMGTYPE_TEMP,
528
                          MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
529
                          mpi->width,mpi->height);
530
        vf_clone_mpi_attributes(dmpi, mpi);
531
    }else{
532
        dmpi=vf->dmpi;
533
    }
534

    
535
    vf->priv->mpeg2= mpi->qscale_type;
536
    if(mpi->pict_type != 3 && mpi->qscale && !vf->priv->qp){
537
        int w = mpi->qstride;
538
        int h = (mpi->h + 15) >> 4;
539
        if (!w) {
540
            w = (mpi->w + 15) >> 4;
541
            h = 1;
542
        }
543
        if(!vf->priv->non_b_qp)
544
            vf->priv->non_b_qp= malloc(w*h);
545
        fast_memcpy(vf->priv->non_b_qp, mpi->qscale, w*h);
546
    }
547
    if(vf->priv->log2_count || !(mpi->flags&MP_IMGFLAG_DIRECT)){
548
        char *qp_tab= vf->priv->non_b_qp;
549
        if(vf->priv->bframes || !qp_tab)
550
            qp_tab= mpi->qscale;
551

    
552
        if(qp_tab || vf->priv->qp){
553
            filter(vf->priv, dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0],
554
                   mpi->w, mpi->h, qp_tab, mpi->qstride, 1);
555
            filter(vf->priv, dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1],
556
                   mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, qp_tab, mpi->qstride, 0);
557
            filter(vf->priv, dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2],
558
                   mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, qp_tab, mpi->qstride, 0);
559
        }else{
560
            memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h, dmpi->stride[0], mpi->stride[0]);
561
            memcpy_pic(dmpi->planes[1], mpi->planes[1], mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, dmpi->stride[1], mpi->stride[1]);
562
            memcpy_pic(dmpi->planes[2], mpi->planes[2], mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, dmpi->stride[2], mpi->stride[2]);
563
        }
564
    }
565

    
566
#if HAVE_MMX
567
    if(gCpuCaps.hasMMX) __asm__ volatile ("emms\n\t");
568
#endif
569
#if HAVE_MMX2
570
    if(gCpuCaps.hasMMX2) __asm__ volatile ("sfence\n\t");
571
#endif
572
    return vf_next_put_image(vf,dmpi, pts);
573
}
574

    
575
static void uninit(struct vf_instance *vf)
576
{
577
    if(!vf->priv) return;
578

    
579
    av_free(vf->priv->temp);
580
    vf->priv->temp= NULL;
581
    av_free(vf->priv->src);
582
    vf->priv->src= NULL;
583
    //free(vf->priv->avctx);
584
    //vf->priv->avctx= NULL;
585
    free(vf->priv->non_b_qp);
586
    vf->priv->non_b_qp= NULL;
587

    
588
    av_free(vf->priv);
589
    vf->priv=NULL;
590
}
591

    
592
//===========================================================================//
593

    
594
static int query_format(struct vf_instance *vf, unsigned int fmt)
595
{
596
    switch(fmt){
597
    case IMGFMT_YVU9:
598
    case IMGFMT_IF09:
599
    case IMGFMT_YV12:
600
    case IMGFMT_I420:
601
    case IMGFMT_IYUV:
602
    case IMGFMT_CLPL:
603
    case IMGFMT_Y800:
604
    case IMGFMT_Y8:
605
    case IMGFMT_444P:
606
    case IMGFMT_422P:
607
    case IMGFMT_411P:
608
        return vf_next_query_format(vf,fmt);
609
    }
610
    return 0;
611
}
612

    
613
static int control(struct vf_instance *vf, int request, void* data)
614
{
615
    switch(request){
616
    case VFCTRL_QUERY_MAX_PP_LEVEL:
617
        return 5;
618
    case VFCTRL_SET_PP_LEVEL:
619
        vf->priv->log2_count= *((unsigned int*)data);
620
        if (vf->priv->log2_count < 4) vf->priv->log2_count=4;
621
        return CONTROL_TRUE;
622
    }
623
    return vf_next_control(vf,request,data);
624
}
625

    
626
static int vf_open(vf_instance_t *vf, char *args)
627
{
628
    int i=0, bias;
629
    int custom_threshold_m[64];
630
    int log2c=-1;
631

    
632
    vf->config=config;
633
    vf->put_image=put_image;
634
    vf->get_image=get_image;
635
    vf->query_format=query_format;
636
    vf->uninit=uninit;
637
    vf->control= control;
638
    vf->priv=av_mallocz(sizeof(struct vf_priv_s));//assumes align 16 !
639

    
640
    init_avcodec();
641

    
642
    //vf->priv->avctx= avcodec_alloc_context();
643
    //dsputil_init(&vf->priv->dsp, vf->priv->avctx);
644

    
645
    vf->priv->log2_count= 4;
646
    vf->priv->bframes = 0;
647

    
648
    if (args) sscanf(args, "%d:%d:%d:%d", &log2c, &vf->priv->qp, &i, &vf->priv->bframes);
649

    
650
    if( log2c >=4 && log2c <=5 )
651
        vf->priv->log2_count = log2c;
652
    else if( log2c >= 6 )
653
        vf->priv->log2_count = 5;
654

    
655
    if(vf->priv->qp < 0)
656
        vf->priv->qp = 0;
657

    
658
    if (i < -15) i = -15;
659
    if (i > 32) i = 32;
660

    
661
    bias= (1<<4)+i; //regulable
662
    vf->priv->prev_q=0;
663
    //
664
    for(i=0;i<64;i++) //FIXME: tune custom_threshold[] and remove this !
665
        custom_threshold_m[i]=(int)(custom_threshold[i]*(bias/71.)+ 0.5);
666
    for(i=0;i<8;i++){
667
        vf->priv->threshold_mtx_noq[2*i]=(uint64_t)custom_threshold_m[i*8+2]
668
            |(((uint64_t)custom_threshold_m[i*8+6])<<16)
669
            |(((uint64_t)custom_threshold_m[i*8+0])<<32)
670
            |(((uint64_t)custom_threshold_m[i*8+4])<<48);
671
        vf->priv->threshold_mtx_noq[2*i+1]=(uint64_t)custom_threshold_m[i*8+5]
672
            |(((uint64_t)custom_threshold_m[i*8+3])<<16)
673
            |(((uint64_t)custom_threshold_m[i*8+1])<<32)
674
            |(((uint64_t)custom_threshold_m[i*8+7])<<48);
675
    }
676

    
677
    if (vf->priv->qp) vf->priv->prev_q=vf->priv->qp, mul_thrmat_s(vf->priv, vf->priv->qp);
678

    
679
    return 1;
680
}
681

    
682
const vf_info_t vf_info_fspp = {
683
    "fast simple postprocess",
684
    "fspp",
685
    "Michael Niedermayer, Nikolaj Poroshin",
686
    "",
687
    vf_open,
688
    NULL
689
};
690

    
691
//====================================================================
692
//Specific spp's dct, idct and threshold functions
693
//I'd prefer to have them in the separate file.
694

    
695
//#define MANGLE(a) #a
696

    
697
//typedef int16_t DCTELEM; //! only int16_t
698

    
699
#define DCTSIZE 8
700
#define DCTSIZE_S "8"
701

    
702
#define FIX(x,s)  ((int) ((x) * (1<<s) + 0.5)&0xffff)
703
#define C64(x)    ((uint64_t)((x)|(x)<<16))<<32 | (uint64_t)(x) | (uint64_t)(x)<<16
704
#define FIX64(x,s)  C64(FIX(x,s))
705

    
706
#define MULTIPLY16H(x,k)   (((x)*(k))>>16)
707
#define THRESHOLD(r,x,t) if(((unsigned)((x)+t))>t*2) r=(x);else r=0;
708
#define DESCALE(x,n)  (((x) + (1 << ((n)-1))) >> n)
709

    
710
#if HAVE_MMX
711

    
712
DECLARE_ASM_CONST(8, uint64_t, MM_FIX_0_382683433)=FIX64(0.382683433, 14);
713
DECLARE_ASM_CONST(8, uint64_t, MM_FIX_0_541196100)=FIX64(0.541196100, 14);
714
DECLARE_ASM_CONST(8, uint64_t, MM_FIX_0_707106781)=FIX64(0.707106781, 14);
715
DECLARE_ASM_CONST(8, uint64_t, MM_FIX_1_306562965)=FIX64(1.306562965, 14);
716

    
717
DECLARE_ASM_CONST(8, uint64_t, MM_FIX_1_414213562_A)=FIX64(1.414213562, 14);
718

    
719
DECLARE_ASM_CONST(8, uint64_t, MM_FIX_1_847759065)=FIX64(1.847759065, 13);
720
DECLARE_ASM_CONST(8, uint64_t, MM_FIX_2_613125930)=FIX64(-2.613125930, 13); //-
721
DECLARE_ASM_CONST(8, uint64_t, MM_FIX_1_414213562)=FIX64(1.414213562, 13);
722
DECLARE_ASM_CONST(8, uint64_t, MM_FIX_1_082392200)=FIX64(1.082392200, 13);
723
//for t3,t5,t7 == 0 shortcut
724
DECLARE_ASM_CONST(8, uint64_t, MM_FIX_0_847759065)=FIX64(0.847759065, 14);
725
DECLARE_ASM_CONST(8, uint64_t, MM_FIX_0_566454497)=FIX64(0.566454497, 14);
726
DECLARE_ASM_CONST(8, uint64_t, MM_FIX_0_198912367)=FIX64(0.198912367, 14);
727

    
728
DECLARE_ASM_CONST(8, uint64_t, MM_DESCALE_RND)=C64(4);
729
DECLARE_ASM_CONST(8, uint64_t, MM_2)=C64(2);
730

    
731
#else /* !HAVE_MMX */
732

    
733
typedef int32_t int_simd16_t;
734
static const int16_t FIX_0_382683433=FIX(0.382683433, 14);
735
static const int16_t FIX_0_541196100=FIX(0.541196100, 14);
736
static const int16_t FIX_0_707106781=FIX(0.707106781, 14);
737
static const int16_t FIX_1_306562965=FIX(1.306562965, 14);
738
static const int16_t FIX_1_414213562_A=FIX(1.414213562, 14);
739
static const int16_t FIX_1_847759065=FIX(1.847759065, 13);
740
static const int16_t FIX_2_613125930=FIX(-2.613125930, 13); //-
741
static const int16_t FIX_1_414213562=FIX(1.414213562, 13);
742
static const int16_t FIX_1_082392200=FIX(1.082392200, 13);
743

    
744
#endif
745

    
746
#if !HAVE_MMX
747

    
748
static void column_fidct_c(int16_t* thr_adr, DCTELEM *data, DCTELEM *output, int cnt)
749
{
750
    int_simd16_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
751
    int_simd16_t tmp10, tmp11, tmp12, tmp13;
752
    int_simd16_t z1,z2,z3,z4,z5, z10, z11, z12, z13;
753
    int_simd16_t d0, d1, d2, d3, d4, d5, d6, d7;
754

    
755
    DCTELEM* dataptr;
756
    DCTELEM* wsptr;
757
    int16_t *threshold;
758
    int ctr;
759

    
760
    dataptr = data;
761
    wsptr = output;
762

    
763
    for (; cnt > 0; cnt-=2) { //start positions
764
        threshold=(int16_t*)thr_adr;//threshold_mtx
765
        for (ctr = DCTSIZE; ctr > 0; ctr--) {
766
            // Process columns from input, add to output.
767
            tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
768
            tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
769

    
770
            tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
771
            tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
772

    
773
            tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
774
            tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
775

    
776
            tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
777
            tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
778

    
779
            // Even part of FDCT
780

    
781
            tmp10 = tmp0 + tmp3;
782
            tmp13 = tmp0 - tmp3;
783
            tmp11 = tmp1 + tmp2;
784
            tmp12 = tmp1 - tmp2;
785

    
786
            d0 = tmp10 + tmp11;
787
            d4 = tmp10 - tmp11;
788

    
789
            z1 = MULTIPLY16H((tmp12 + tmp13) <<2, FIX_0_707106781);
790
            d2 = tmp13 + z1;
791
            d6 = tmp13 - z1;
792

    
793
            // Even part of IDCT
794

    
795
            THRESHOLD(tmp0, d0, threshold[0*8]);
796
            THRESHOLD(tmp1, d2, threshold[2*8]);
797
            THRESHOLD(tmp2, d4, threshold[4*8]);
798
            THRESHOLD(tmp3, d6, threshold[6*8]);
799
            tmp0+=2;
800
            tmp10 = (tmp0 + tmp2)>>2;
801
            tmp11 = (tmp0 - tmp2)>>2;
802

    
803
            tmp13 = (tmp1 + tmp3)>>2; //+2 !  (psnr decides)
804
            tmp12 = MULTIPLY16H((tmp1 - tmp3), FIX_1_414213562_A) - tmp13; //<<2
805

    
806
            tmp0 = tmp10 + tmp13; //->temps
807
            tmp3 = tmp10 - tmp13; //->temps
808
            tmp1 = tmp11 + tmp12; //->temps
809
            tmp2 = tmp11 - tmp12; //->temps
810

    
811
            // Odd part of FDCT
812

    
813
            tmp10 = tmp4 + tmp5;
814
            tmp11 = tmp5 + tmp6;
815
            tmp12 = tmp6 + tmp7;
816

    
817
            z5 = MULTIPLY16H((tmp10 - tmp12)<<2, FIX_0_382683433);
818
            z2 = MULTIPLY16H(tmp10 <<2, FIX_0_541196100) + z5;
819
            z4 = MULTIPLY16H(tmp12 <<2, FIX_1_306562965) + z5;
820
            z3 = MULTIPLY16H(tmp11 <<2, FIX_0_707106781);
821

    
822
            z11 = tmp7 + z3;
823
            z13 = tmp7 - z3;
824

    
825
            d5 = z13 + z2;
826
            d3 = z13 - z2;
827
            d1 = z11 + z4;
828
            d7 = z11 - z4;
829

    
830
            // Odd part of IDCT
831

    
832
            THRESHOLD(tmp4, d1, threshold[1*8]);
833
            THRESHOLD(tmp5, d3, threshold[3*8]);
834
            THRESHOLD(tmp6, d5, threshold[5*8]);
835
            THRESHOLD(tmp7, d7, threshold[7*8]);
836

    
837
            //Simd version uses here a shortcut for the tmp5,tmp6,tmp7 == 0
838
            z13 = tmp6 + tmp5;
839
            z10 = (tmp6 - tmp5)<<1;
840
            z11 = tmp4 + tmp7;
841
            z12 = (tmp4 - tmp7)<<1;
842

    
843
            tmp7 = (z11 + z13)>>2; //+2 !
844
            tmp11 = MULTIPLY16H((z11 - z13)<<1, FIX_1_414213562);
845
            z5 =    MULTIPLY16H(z10 + z12, FIX_1_847759065);
846
            tmp10 = MULTIPLY16H(z12, FIX_1_082392200) - z5;
847
            tmp12 = MULTIPLY16H(z10, FIX_2_613125930) + z5; // - !!
848

    
849
            tmp6 = tmp12 - tmp7;
850
            tmp5 = tmp11 - tmp6;
851
            tmp4 = tmp10 + tmp5;
852

    
853
            wsptr[DCTSIZE*0]+=  (tmp0 + tmp7);
854
            wsptr[DCTSIZE*1]+=  (tmp1 + tmp6);
855
            wsptr[DCTSIZE*2]+=  (tmp2 + tmp5);
856
            wsptr[DCTSIZE*3]+=  (tmp3 - tmp4);
857
            wsptr[DCTSIZE*4]+=  (tmp3 + tmp4);
858
            wsptr[DCTSIZE*5]+=  (tmp2 - tmp5);
859
            wsptr[DCTSIZE*6]=  (tmp1 - tmp6);
860
            wsptr[DCTSIZE*7]=  (tmp0 - tmp7);
861
            //
862
            dataptr++; //next column
863
            wsptr++;
864
            threshold++;
865
        }
866
        dataptr+=8; //skip each second start pos
867
        wsptr  +=8;
868
    }
869
}
870

    
871
#else /* HAVE_MMX */
872

    
873
static void column_fidct_mmx(int16_t* thr_adr,  DCTELEM *data,  DCTELEM *output,  int cnt)
874
{
875
    uint64_t __attribute__((aligned(8))) temps[4];
876
    __asm__ volatile(
877
        ASMALIGN(4)
878
        "1:                   \n\t"
879
        "movq "DCTSIZE_S"*0*2(%%"REG_S"), %%mm1 \n\t"
880
        //
881
        "movq "DCTSIZE_S"*3*2(%%"REG_S"), %%mm7 \n\t"
882
        "movq %%mm1, %%mm0             \n\t"
883

    
884
        "paddw "DCTSIZE_S"*7*2(%%"REG_S"), %%mm1 \n\t" //t0
885
        "movq %%mm7, %%mm3             \n\t"
886

    
887
        "paddw "DCTSIZE_S"*4*2(%%"REG_S"), %%mm7 \n\t" //t3
888
        "movq %%mm1, %%mm5             \n\t"
889

    
890
        "movq "DCTSIZE_S"*1*2(%%"REG_S"), %%mm6 \n\t"
891
        "psubw %%mm7, %%mm1            \n\t" //t13
892

    
893
        "movq "DCTSIZE_S"*2*2(%%"REG_S"), %%mm2 \n\t"
894
        "movq %%mm6, %%mm4             \n\t"
895

    
896
        "paddw "DCTSIZE_S"*6*2(%%"REG_S"), %%mm6 \n\t" //t1
897
        "paddw %%mm7, %%mm5            \n\t" //t10
898

    
899
        "paddw "DCTSIZE_S"*5*2(%%"REG_S"), %%mm2 \n\t" //t2
900
        "movq %%mm6, %%mm7             \n\t"
901

    
902
        "paddw %%mm2, %%mm6            \n\t" //t11
903
        "psubw %%mm2, %%mm7            \n\t" //t12
904

    
905
        "movq %%mm5, %%mm2             \n\t"
906
        "paddw %%mm6, %%mm5            \n\t" //d0
907
        // i0 t13 t12 i3 i1 d0 - d4
908
        "psubw %%mm6, %%mm2            \n\t" //d4
909
        "paddw %%mm1, %%mm7            \n\t"
910

    
911
        "movq  4*16(%%"REG_d"), %%mm6      \n\t"
912
        "psllw $2, %%mm7              \n\t"
913

    
914
        "psubw 0*16(%%"REG_d"), %%mm5      \n\t"
915
        "psubw %%mm6, %%mm2            \n\t"
916

    
917
        "paddusw 0*16(%%"REG_d"), %%mm5    \n\t"
918
        "paddusw %%mm6, %%mm2          \n\t"
919

    
920
        "pmulhw "MANGLE(MM_FIX_0_707106781)", %%mm7 \n\t"
921
        //
922
        "paddw 0*16(%%"REG_d"), %%mm5      \n\t"
923
        "paddw %%mm6, %%mm2            \n\t"
924

    
925
        "psubusw 0*16(%%"REG_d"), %%mm5    \n\t"
926
        "psubusw %%mm6, %%mm2          \n\t"
927

    
928
//This func is totally compute-bound,  operates at huge speed. So,  DC shortcut
929
// at this place isn't worthwhile due to BTB miss penalty (checked on Pent. 3).
930
//However,  typical numbers: nondc - 29%%,  dc - 46%%,  zero - 25%%. All <> 0 case is very rare.
931
        "paddw "MANGLE(MM_2)", %%mm5            \n\t"
932
        "movq %%mm2, %%mm6             \n\t"
933

    
934
        "paddw %%mm5, %%mm2            \n\t"
935
        "psubw %%mm6, %%mm5            \n\t"
936

    
937
        "movq %%mm1, %%mm6             \n\t"
938
        "paddw %%mm7, %%mm1            \n\t" //d2
939

    
940
        "psubw 2*16(%%"REG_d"), %%mm1      \n\t"
941
        "psubw %%mm7, %%mm6            \n\t" //d6
942

    
943
        "movq 6*16(%%"REG_d"), %%mm7       \n\t"
944
        "psraw $2, %%mm5              \n\t"
945

    
946
        "paddusw 2*16(%%"REG_d"), %%mm1    \n\t"
947
        "psubw %%mm7, %%mm6            \n\t"
948
        // t7 d2 /t11 t4 t6 - d6 /t10
949

    
950
        "paddw 2*16(%%"REG_d"), %%mm1      \n\t"
951
        "paddusw %%mm7, %%mm6          \n\t"
952

    
953
        "psubusw 2*16(%%"REG_d"), %%mm1    \n\t"
954
        "paddw %%mm7, %%mm6            \n\t"
955

    
956
        "psubw "DCTSIZE_S"*4*2(%%"REG_S"), %%mm3 \n\t"
957
        "psubusw %%mm7, %%mm6          \n\t"
958

    
959
        //movq [edi+"DCTSIZE_S"*2*2], mm1
960
        //movq [edi+"DCTSIZE_S"*6*2], mm6
961
        "movq %%mm1, %%mm7             \n\t"
962
        "psraw $2, %%mm2              \n\t"
963

    
964
        "psubw "DCTSIZE_S"*6*2(%%"REG_S"), %%mm4 \n\t"
965
        "psubw %%mm6, %%mm1            \n\t"
966

    
967
        "psubw "DCTSIZE_S"*7*2(%%"REG_S"), %%mm0 \n\t"
968
        "paddw %%mm7, %%mm6            \n\t" //'t13
969

    
970
        "psraw $2, %%mm6              \n\t" //paddw mm6, MM_2 !!    ---
971
        "movq %%mm2, %%mm7             \n\t"
972

    
973
        "pmulhw "MANGLE(MM_FIX_1_414213562_A)", %%mm1 \n\t"
974
        "paddw %%mm6, %%mm2            \n\t" //'t0
975

    
976
        "movq %%mm2, 0*8+%3            \n\t" //!
977
        "psubw %%mm6, %%mm7            \n\t" //'t3
978

    
979
        "movq "DCTSIZE_S"*2*2(%%"REG_S"), %%mm2 \n\t"
980
        "psubw %%mm6, %%mm1            \n\t" //'t12
981

    
982
        "psubw "DCTSIZE_S"*5*2(%%"REG_S"), %%mm2 \n\t" //t5
983
        "movq %%mm5, %%mm6             \n\t"
984

    
985
        "movq %%mm7, 3*8+%3            \n\t"
986
        "paddw %%mm2, %%mm3            \n\t" //t10
987

    
988
        "paddw %%mm4, %%mm2            \n\t" //t11
989
        "paddw %%mm0, %%mm4            \n\t" //t12
990

    
991
        "movq %%mm3, %%mm7             \n\t"
992
        "psubw %%mm4, %%mm3            \n\t"
993

    
994
        "psllw $2, %%mm3              \n\t"
995
        "psllw $2, %%mm7              \n\t" //opt for P6
996

    
997
        "pmulhw "MANGLE(MM_FIX_0_382683433)", %%mm3 \n\t"
998
        "psllw $2, %%mm4              \n\t"
999

    
1000
        "pmulhw "MANGLE(MM_FIX_0_541196100)", %%mm7 \n\t"
1001
        "psllw $2, %%mm2              \n\t"
1002

    
1003
        "pmulhw "MANGLE(MM_FIX_1_306562965)", %%mm4 \n\t"
1004
        "paddw %%mm1, %%mm5            \n\t" //'t1
1005

    
1006
        "pmulhw "MANGLE(MM_FIX_0_707106781)", %%mm2 \n\t"
1007
        "psubw %%mm1, %%mm6            \n\t" //'t2
1008
        // t7 't12 't11 t4 t6 - 't13 't10   ---
1009

    
1010
        "paddw %%mm3, %%mm7            \n\t" //z2
1011

    
1012
        "movq %%mm5, 1*8+%3            \n\t"
1013
        "paddw %%mm3, %%mm4            \n\t" //z4
1014

    
1015
        "movq 3*16(%%"REG_d"), %%mm3       \n\t"
1016
        "movq %%mm0, %%mm1             \n\t"
1017

    
1018
        "movq %%mm6, 2*8+%3            \n\t"
1019
        "psubw %%mm2, %%mm1            \n\t" //z13
1020

    
1021
//===
1022
        "paddw %%mm2, %%mm0            \n\t" //z11
1023
        "movq %%mm1, %%mm5             \n\t"
1024

    
1025
        "movq 5*16(%%"REG_d"), %%mm2       \n\t"
1026
        "psubw %%mm7, %%mm1            \n\t" //d3
1027

    
1028
        "paddw %%mm7, %%mm5            \n\t" //d5
1029
        "psubw %%mm3, %%mm1            \n\t"
1030

    
1031
        "movq 1*16(%%"REG_d"), %%mm7       \n\t"
1032
        "psubw %%mm2, %%mm5            \n\t"
1033

    
1034
        "movq %%mm0, %%mm6             \n\t"
1035
        "paddw %%mm4, %%mm0            \n\t" //d1
1036

    
1037
        "paddusw %%mm3, %%mm1          \n\t"
1038
        "psubw %%mm4, %%mm6            \n\t" //d7
1039

    
1040
        // d1 d3 - - - d5 d7 -
1041
        "movq 7*16(%%"REG_d"), %%mm4       \n\t"
1042
        "psubw %%mm7, %%mm0            \n\t"
1043

    
1044
        "psubw %%mm4, %%mm6            \n\t"
1045
        "paddusw %%mm2, %%mm5          \n\t"
1046

    
1047
        "paddusw %%mm4, %%mm6          \n\t"
1048
        "paddw %%mm3, %%mm1            \n\t"
1049

    
1050
        "paddw %%mm2, %%mm5            \n\t"
1051
        "paddw %%mm4, %%mm6            \n\t"
1052

    
1053
        "psubusw %%mm3, %%mm1          \n\t"
1054
        "psubusw %%mm2, %%mm5          \n\t"
1055

    
1056
        "psubusw %%mm4, %%mm6          \n\t"
1057
        "movq %%mm1, %%mm4             \n\t"
1058

    
1059
        "por %%mm5, %%mm4              \n\t"
1060
        "paddusw %%mm7, %%mm0          \n\t"
1061

    
1062
        "por %%mm6, %%mm4              \n\t"
1063
        "paddw %%mm7, %%mm0            \n\t"
1064

    
1065
        "packssdw %%mm4, %%mm4         \n\t"
1066
        "psubusw %%mm7, %%mm0          \n\t"
1067

    
1068
        "movd %%mm4, %%"REG_a"             \n\t"
1069
        "or %%"REG_a", %%"REG_a"              \n\t"
1070
        "jnz 2f                 \n\t"
1071
        //movq [edi+"DCTSIZE_S"*3*2], mm1
1072
        //movq [edi+"DCTSIZE_S"*5*2], mm5
1073
        //movq [edi+"DCTSIZE_S"*1*2], mm0
1074
        //movq [edi+"DCTSIZE_S"*7*2], mm6
1075
        // t4 t5 - - - t6 t7 -
1076
        //--- t4 (mm0) may be <>0; mm1, mm5, mm6 == 0
1077
//Typical numbers: nondc - 19%%,  dc - 26%%,  zero - 55%%. zero case alone isn't worthwhile
1078
        "movq 0*8+%3, %%mm4            \n\t"
1079
        "movq %%mm0, %%mm1             \n\t"
1080

    
1081
        "pmulhw "MANGLE(MM_FIX_0_847759065)", %%mm0 \n\t" //tmp6
1082
        "movq %%mm1, %%mm2             \n\t"
1083

    
1084
        "movq "DCTSIZE_S"*0*2(%%"REG_D"), %%mm5 \n\t"
1085
        "movq %%mm2, %%mm3             \n\t"
1086

    
1087
        "pmulhw "MANGLE(MM_FIX_0_566454497)", %%mm1 \n\t" //tmp5
1088
        "paddw %%mm4, %%mm5            \n\t"
1089

    
1090
        "movq 1*8+%3, %%mm6            \n\t"
1091
        //paddw mm3, MM_2
1092
        "psraw $2, %%mm3              \n\t" //tmp7
1093

    
1094
        "pmulhw "MANGLE(MM_FIX_0_198912367)", %%mm2 \n\t" //-tmp4
1095
        "psubw %%mm3, %%mm4            \n\t"
1096

    
1097
        "movq "DCTSIZE_S"*1*2(%%"REG_D"), %%mm7 \n\t"
1098
        "paddw %%mm3, %%mm5            \n\t"
1099

    
1100
        "movq %%mm4, "DCTSIZE_S"*7*2(%%"REG_D") \n\t"
1101
        "paddw %%mm6, %%mm7            \n\t"
1102

    
1103
        "movq 2*8+%3, %%mm3            \n\t"
1104
        "psubw %%mm0, %%mm6            \n\t"
1105

    
1106
        "movq "DCTSIZE_S"*2*2(%%"REG_D"), %%mm4 \n\t"
1107
        "paddw %%mm0, %%mm7            \n\t"
1108

    
1109
        "movq %%mm5, "DCTSIZE_S"*0*2(%%"REG_D") \n\t"
1110
        "paddw %%mm3, %%mm4            \n\t"
1111

    
1112
        "movq %%mm6, "DCTSIZE_S"*6*2(%%"REG_D") \n\t"
1113
        "psubw %%mm1, %%mm3            \n\t"
1114

    
1115
        "movq "DCTSIZE_S"*5*2(%%"REG_D"), %%mm5 \n\t"
1116
        "paddw %%mm1, %%mm4            \n\t"
1117

    
1118
        "movq "DCTSIZE_S"*3*2(%%"REG_D"), %%mm6 \n\t"
1119
        "paddw %%mm3, %%mm5            \n\t"
1120

    
1121
        "movq 3*8+%3, %%mm0            \n\t"
1122
        "add $8, %%"REG_S"               \n\t"
1123

    
1124
        "movq %%mm7, "DCTSIZE_S"*1*2(%%"REG_D") \n\t"
1125
        "paddw %%mm0, %%mm6            \n\t"
1126

    
1127
        "movq %%mm4, "DCTSIZE_S"*2*2(%%"REG_D") \n\t"
1128
        "psubw %%mm2, %%mm0            \n\t"
1129

    
1130
        "movq "DCTSIZE_S"*4*2(%%"REG_D"), %%mm7 \n\t"
1131
        "paddw %%mm2, %%mm6            \n\t"
1132

    
1133
        "movq %%mm5, "DCTSIZE_S"*5*2(%%"REG_D") \n\t"
1134
        "paddw %%mm0, %%mm7            \n\t"
1135

    
1136
        "movq %%mm6, "DCTSIZE_S"*3*2(%%"REG_D") \n\t"
1137

    
1138
        "movq %%mm7, "DCTSIZE_S"*4*2(%%"REG_D") \n\t"
1139
        "add $8, %%"REG_D"               \n\t"
1140
        "jmp 4f                  \n\t"
1141

    
1142
        "2:                    \n\t"
1143
        //--- non DC2
1144
        //psraw mm1, 2 w/o it -> offset. thr1, thr1, thr1  (actually thr1, thr1, thr1-1)
1145
        //psraw mm5, 2
1146
        //psraw mm0, 2
1147
        //psraw mm6, 2
1148
        "movq %%mm5, %%mm3             \n\t"
1149
        "psubw %%mm1, %%mm5            \n\t"
1150

    
1151
        "psllw $1, %%mm5              \n\t" //'z10
1152
        "paddw %%mm1, %%mm3            \n\t" //'z13
1153

    
1154
        "movq %%mm0, %%mm2             \n\t"
1155
        "psubw %%mm6, %%mm0            \n\t"
1156

    
1157
        "movq %%mm5, %%mm1             \n\t"
1158
        "psllw $1, %%mm0              \n\t" //'z12
1159

    
1160
        "pmulhw "MANGLE(MM_FIX_2_613125930)", %%mm1 \n\t" //-
1161
        "paddw %%mm0, %%mm5            \n\t"
1162

    
1163
        "pmulhw "MANGLE(MM_FIX_1_847759065)", %%mm5 \n\t" //'z5
1164
        "paddw %%mm6, %%mm2            \n\t" //'z11
1165

    
1166
        "pmulhw "MANGLE(MM_FIX_1_082392200)", %%mm0 \n\t"
1167
        "movq %%mm2, %%mm7             \n\t"
1168

    
1169
        //---
1170
        "movq 0*8+%3, %%mm4            \n\t"
1171
        "psubw %%mm3, %%mm2            \n\t"
1172

    
1173
        "psllw $1, %%mm2              \n\t"
1174
        "paddw %%mm3, %%mm7            \n\t" //'t7
1175

    
1176
        "pmulhw "MANGLE(MM_FIX_1_414213562)", %%mm2 \n\t" //'t11
1177
        "movq %%mm4, %%mm6             \n\t"
1178
        //paddw mm7, MM_2
1179
        "psraw $2, %%mm7              \n\t"
1180

    
1181
        "paddw "DCTSIZE_S"*0*2(%%"REG_D"), %%mm4 \n\t"
1182
        "psubw %%mm7, %%mm6            \n\t"
1183

    
1184
        "movq 1*8+%3, %%mm3            \n\t"
1185
        "paddw %%mm7, %%mm4            \n\t"
1186

    
1187
        "movq %%mm6, "DCTSIZE_S"*7*2(%%"REG_D") \n\t"
1188
        "paddw %%mm5, %%mm1            \n\t" //'t12
1189

    
1190
        "movq %%mm4, "DCTSIZE_S"*0*2(%%"REG_D") \n\t"
1191
        "psubw %%mm7, %%mm1            \n\t" //'t6
1192

    
1193
        "movq 2*8+%3, %%mm7            \n\t"
1194
        "psubw %%mm5, %%mm0            \n\t" //'t10
1195

    
1196
        "movq 3*8+%3, %%mm6            \n\t"
1197
        "movq %%mm3, %%mm5             \n\t"
1198

    
1199
        "paddw "DCTSIZE_S"*1*2(%%"REG_D"), %%mm3 \n\t"
1200
        "psubw %%mm1, %%mm5            \n\t"
1201

    
1202
        "psubw %%mm1, %%mm2            \n\t" //'t5
1203
        "paddw %%mm1, %%mm3            \n\t"
1204

    
1205
        "movq %%mm5, "DCTSIZE_S"*6*2(%%"REG_D") \n\t"
1206
        "movq %%mm7, %%mm4             \n\t"
1207

    
1208
        "paddw "DCTSIZE_S"*2*2(%%"REG_D"), %%mm7 \n\t"
1209
        "psubw %%mm2, %%mm4            \n\t"
1210

    
1211
        "paddw "DCTSIZE_S"*5*2(%%"REG_D"), %%mm4 \n\t"
1212
        "paddw %%mm2, %%mm7            \n\t"
1213

    
1214
        "movq %%mm3, "DCTSIZE_S"*1*2(%%"REG_D") \n\t"
1215
        "paddw %%mm2, %%mm0            \n\t" //'t4
1216

    
1217
        // 't4 't6 't5 - - - - 't7
1218
        "movq %%mm7, "DCTSIZE_S"*2*2(%%"REG_D") \n\t"
1219
        "movq %%mm6, %%mm1             \n\t"
1220

    
1221
        "paddw "DCTSIZE_S"*4*2(%%"REG_D"), %%mm6 \n\t"
1222
        "psubw %%mm0, %%mm1            \n\t"
1223

    
1224
        "paddw "DCTSIZE_S"*3*2(%%"REG_D"), %%mm1 \n\t"
1225
        "paddw %%mm0, %%mm6            \n\t"
1226

    
1227
        "movq %%mm4, "DCTSIZE_S"*5*2(%%"REG_D") \n\t"
1228
        "add $8, %%"REG_S"               \n\t"
1229

    
1230
        "movq %%mm6, "DCTSIZE_S"*4*2(%%"REG_D") \n\t"
1231

    
1232
        "movq %%mm1, "DCTSIZE_S"*3*2(%%"REG_D") \n\t"
1233
        "add $8, %%"REG_D"               \n\t"
1234

    
1235
        "4:                     \n\t"
1236
//=part 2 (the same)===========================================================
1237
        "movq "DCTSIZE_S"*0*2(%%"REG_S"), %%mm1 \n\t"
1238
        //
1239
        "movq "DCTSIZE_S"*3*2(%%"REG_S"), %%mm7 \n\t"
1240
        "movq %%mm1, %%mm0             \n\t"
1241

    
1242
        "paddw "DCTSIZE_S"*7*2(%%"REG_S"), %%mm1 \n\t" //t0
1243
        "movq %%mm7, %%mm3             \n\t"
1244

    
1245
        "paddw "DCTSIZE_S"*4*2(%%"REG_S"), %%mm7 \n\t" //t3
1246
        "movq %%mm1, %%mm5             \n\t"
1247

    
1248
        "movq "DCTSIZE_S"*1*2(%%"REG_S"), %%mm6 \n\t"
1249
        "psubw %%mm7, %%mm1            \n\t" //t13
1250

    
1251
        "movq "DCTSIZE_S"*2*2(%%"REG_S"), %%mm2 \n\t"
1252
        "movq %%mm6, %%mm4             \n\t"
1253

    
1254
        "paddw "DCTSIZE_S"*6*2(%%"REG_S"), %%mm6 \n\t" //t1
1255
        "paddw %%mm7, %%mm5            \n\t" //t10
1256

    
1257
        "paddw "DCTSIZE_S"*5*2(%%"REG_S"), %%mm2 \n\t" //t2
1258
        "movq %%mm6, %%mm7             \n\t"
1259

    
1260
        "paddw %%mm2, %%mm6            \n\t" //t11
1261
        "psubw %%mm2, %%mm7            \n\t" //t12
1262

    
1263
        "movq %%mm5, %%mm2             \n\t"
1264
        "paddw %%mm6, %%mm5            \n\t" //d0
1265
        // i0 t13 t12 i3 i1 d0 - d4
1266
        "psubw %%mm6, %%mm2            \n\t" //d4
1267
        "paddw %%mm1, %%mm7            \n\t"
1268

    
1269
        "movq  1*8+4*16(%%"REG_d"), %%mm6  \n\t"
1270
        "psllw $2, %%mm7              \n\t"
1271

    
1272
        "psubw 1*8+0*16(%%"REG_d"), %%mm5  \n\t"
1273
        "psubw %%mm6, %%mm2            \n\t"
1274

    
1275
        "paddusw 1*8+0*16(%%"REG_d"), %%mm5 \n\t"
1276
        "paddusw %%mm6, %%mm2          \n\t"
1277

    
1278
        "pmulhw "MANGLE(MM_FIX_0_707106781)", %%mm7 \n\t"
1279
        //
1280
        "paddw 1*8+0*16(%%"REG_d"), %%mm5  \n\t"
1281
        "paddw %%mm6, %%mm2            \n\t"
1282

    
1283
        "psubusw 1*8+0*16(%%"REG_d"), %%mm5 \n\t"
1284
        "psubusw %%mm6, %%mm2          \n\t"
1285

    
1286
//This func is totally compute-bound,  operates at huge speed. So,  DC shortcut
1287
// at this place isn't worthwhile due to BTB miss penalty (checked on Pent. 3).
1288
//However,  typical numbers: nondc - 29%%,  dc - 46%%,  zero - 25%%. All <> 0 case is very rare.
1289
        "paddw "MANGLE(MM_2)", %%mm5            \n\t"
1290
        "movq %%mm2, %%mm6             \n\t"
1291

    
1292
        "paddw %%mm5, %%mm2            \n\t"
1293
        "psubw %%mm6, %%mm5            \n\t"
1294

    
1295
        "movq %%mm1, %%mm6             \n\t"
1296
        "paddw %%mm7, %%mm1            \n\t" //d2
1297

    
1298
        "psubw 1*8+2*16(%%"REG_d"), %%mm1  \n\t"
1299
        "psubw %%mm7, %%mm6            \n\t" //d6
1300

    
1301
        "movq 1*8+6*16(%%"REG_d"), %%mm7   \n\t"
1302
        "psraw $2, %%mm5              \n\t"
1303

    
1304
        "paddusw 1*8+2*16(%%"REG_d"), %%mm1 \n\t"
1305
        "psubw %%mm7, %%mm6            \n\t"
1306
        // t7 d2 /t11 t4 t6 - d6 /t10
1307

    
1308
        "paddw 1*8+2*16(%%"REG_d"), %%mm1  \n\t"
1309
        "paddusw %%mm7, %%mm6          \n\t"
1310

    
1311
        "psubusw 1*8+2*16(%%"REG_d"), %%mm1 \n\t"
1312
        "paddw %%mm7, %%mm6            \n\t"
1313

    
1314
        "psubw "DCTSIZE_S"*4*2(%%"REG_S"), %%mm3 \n\t"
1315
        "psubusw %%mm7, %%mm6          \n\t"
1316

    
1317
        //movq [edi+"DCTSIZE_S"*2*2], mm1
1318
        //movq [edi+"DCTSIZE_S"*6*2], mm6
1319
        "movq %%mm1, %%mm7             \n\t"
1320
        "psraw $2, %%mm2              \n\t"
1321

    
1322
        "psubw "DCTSIZE_S"*6*2(%%"REG_S"), %%mm4 \n\t"
1323
        "psubw %%mm6, %%mm1            \n\t"
1324

    
1325
        "psubw "DCTSIZE_S"*7*2(%%"REG_S"), %%mm0 \n\t"
1326
        "paddw %%mm7, %%mm6            \n\t" //'t13
1327

    
1328
        "psraw $2, %%mm6              \n\t" //paddw mm6, MM_2 !!    ---
1329
        "movq %%mm2, %%mm7             \n\t"
1330

    
1331
        "pmulhw "MANGLE(MM_FIX_1_414213562_A)", %%mm1 \n\t"
1332
        "paddw %%mm6, %%mm2            \n\t" //'t0
1333

    
1334
        "movq %%mm2, 0*8+%3            \n\t" //!
1335
        "psubw %%mm6, %%mm7            \n\t" //'t3
1336

    
1337
        "movq "DCTSIZE_S"*2*2(%%"REG_S"), %%mm2 \n\t"
1338
        "psubw %%mm6, %%mm1            \n\t" //'t12
1339

    
1340
        "psubw "DCTSIZE_S"*5*2(%%"REG_S"), %%mm2 \n\t" //t5
1341
        "movq %%mm5, %%mm6             \n\t"
1342

    
1343
        "movq %%mm7, 3*8+%3            \n\t"
1344
        "paddw %%mm2, %%mm3            \n\t" //t10
1345

    
1346
        "paddw %%mm4, %%mm2            \n\t" //t11
1347
        "paddw %%mm0, %%mm4            \n\t" //t12
1348

    
1349
        "movq %%mm3, %%mm7             \n\t"
1350
        "psubw %%mm4, %%mm3            \n\t"
1351

    
1352
        "psllw $2, %%mm3              \n\t"
1353
        "psllw $2, %%mm7              \n\t" //opt for P6
1354

    
1355
        "pmulhw "MANGLE(MM_FIX_0_382683433)", %%mm3 \n\t"
1356
        "psllw $2, %%mm4              \n\t"
1357

    
1358
        "pmulhw "MANGLE(MM_FIX_0_541196100)", %%mm7 \n\t"
1359
        "psllw $2, %%mm2              \n\t"
1360

    
1361
        "pmulhw "MANGLE(MM_FIX_1_306562965)", %%mm4 \n\t"
1362
        "paddw %%mm1, %%mm5            \n\t" //'t1
1363

    
1364
        "pmulhw "MANGLE(MM_FIX_0_707106781)", %%mm2 \n\t"
1365
        "psubw %%mm1, %%mm6            \n\t" //'t2
1366
        // t7 't12 't11 t4 t6 - 't13 't10   ---
1367

    
1368
        "paddw %%mm3, %%mm7            \n\t" //z2
1369

    
1370
        "movq %%mm5, 1*8+%3            \n\t"
1371
        "paddw %%mm3, %%mm4            \n\t" //z4
1372

    
1373
        "movq 1*8+3*16(%%"REG_d"), %%mm3   \n\t"
1374
        "movq %%mm0, %%mm1             \n\t"
1375

    
1376
        "movq %%mm6, 2*8+%3            \n\t"
1377
        "psubw %%mm2, %%mm1            \n\t" //z13
1378

    
1379
//===
1380
        "paddw %%mm2, %%mm0            \n\t" //z11
1381
        "movq %%mm1, %%mm5             \n\t"
1382

    
1383
        "movq 1*8+5*16(%%"REG_d"), %%mm2   \n\t"
1384
        "psubw %%mm7, %%mm1            \n\t" //d3
1385

    
1386
        "paddw %%mm7, %%mm5            \n\t" //d5
1387
        "psubw %%mm3, %%mm1            \n\t"
1388

    
1389
        "movq 1*8+1*16(%%"REG_d"), %%mm7   \n\t"
1390
        "psubw %%mm2, %%mm5            \n\t"
1391

    
1392
        "movq %%mm0, %%mm6             \n\t"
1393
        "paddw %%mm4, %%mm0            \n\t" //d1
1394

    
1395
        "paddusw %%mm3, %%mm1          \n\t"
1396
        "psubw %%mm4, %%mm6            \n\t" //d7
1397

    
1398
        // d1 d3 - - - d5 d7 -
1399
        "movq 1*8+7*16(%%"REG_d"), %%mm4   \n\t"
1400
        "psubw %%mm7, %%mm0            \n\t"
1401

    
1402
        "psubw %%mm4, %%mm6            \n\t"
1403
        "paddusw %%mm2, %%mm5          \n\t"
1404

    
1405
        "paddusw %%mm4, %%mm6          \n\t"
1406
        "paddw %%mm3, %%mm1            \n\t"
1407

    
1408
        "paddw %%mm2, %%mm5            \n\t"
1409
        "paddw %%mm4, %%mm6            \n\t"
1410

    
1411
        "psubusw %%mm3, %%mm1          \n\t"
1412
        "psubusw %%mm2, %%mm5          \n\t"
1413

    
1414
        "psubusw %%mm4, %%mm6          \n\t"
1415
        "movq %%mm1, %%mm4             \n\t"
1416

    
1417
        "por %%mm5, %%mm4              \n\t"
1418
        "paddusw %%mm7, %%mm0          \n\t"
1419

    
1420
        "por %%mm6, %%mm4              \n\t"
1421
        "paddw %%mm7, %%mm0            \n\t"
1422

    
1423
        "packssdw %%mm4, %%mm4         \n\t"
1424
        "psubusw %%mm7, %%mm0          \n\t"
1425

    
1426
        "movd %%mm4, %%"REG_a"             \n\t"
1427
        "or %%"REG_a", %%"REG_a"              \n\t"
1428
        "jnz 3f                 \n\t"
1429
        //movq [edi+"DCTSIZE_S"*3*2], mm1
1430
        //movq [edi+"DCTSIZE_S"*5*2], mm5
1431
        //movq [edi+"DCTSIZE_S"*1*2], mm0
1432
        //movq [edi+"DCTSIZE_S"*7*2], mm6
1433
        // t4 t5 - - - t6 t7 -
1434
        //--- t4 (mm0) may be <>0; mm1, mm5, mm6 == 0
1435
//Typical numbers: nondc - 19%%,  dc - 26%%,  zero - 55%%. zero case alone isn't worthwhile
1436
        "movq 0*8+%3, %%mm4            \n\t"
1437
        "movq %%mm0, %%mm1             \n\t"
1438

    
1439
        "pmulhw "MANGLE(MM_FIX_0_847759065)", %%mm0 \n\t" //tmp6
1440
        "movq %%mm1, %%mm2             \n\t"
1441

    
1442
        "movq "DCTSIZE_S"*0*2(%%"REG_D"), %%mm5 \n\t"
1443
        "movq %%mm2, %%mm3             \n\t"
1444

    
1445
        "pmulhw "MANGLE(MM_FIX_0_566454497)", %%mm1 \n\t" //tmp5
1446
        "paddw %%mm4, %%mm5            \n\t"
1447

    
1448
        "movq 1*8+%3, %%mm6            \n\t"
1449
        //paddw mm3, MM_2
1450
        "psraw $2, %%mm3              \n\t" //tmp7
1451

    
1452
        "pmulhw "MANGLE(MM_FIX_0_198912367)", %%mm2 \n\t" //-tmp4
1453
        "psubw %%mm3, %%mm4            \n\t"
1454

    
1455
        "movq "DCTSIZE_S"*1*2(%%"REG_D"), %%mm7 \n\t"
1456
        "paddw %%mm3, %%mm5            \n\t"
1457

    
1458
        "movq %%mm4, "DCTSIZE_S"*7*2(%%"REG_D") \n\t"
1459
        "paddw %%mm6, %%mm7            \n\t"
1460

    
1461
        "movq 2*8+%3, %%mm3            \n\t"
1462
        "psubw %%mm0, %%mm6            \n\t"
1463

    
1464
        "movq "DCTSIZE_S"*2*2(%%"REG_D"), %%mm4 \n\t"
1465
        "paddw %%mm0, %%mm7            \n\t"
1466

    
1467
        "movq %%mm5, "DCTSIZE_S"*0*2(%%"REG_D") \n\t"
1468
        "paddw %%mm3, %%mm4            \n\t"
1469

    
1470
        "movq %%mm6, "DCTSIZE_S"*6*2(%%"REG_D") \n\t"
1471
        "psubw %%mm1, %%mm3            \n\t"
1472

    
1473
        "movq "DCTSIZE_S"*5*2(%%"REG_D"), %%mm5 \n\t"
1474
        "paddw %%mm1, %%mm4            \n\t"
1475

    
1476
        "movq "DCTSIZE_S"*3*2(%%"REG_D"), %%mm6 \n\t"
1477
        "paddw %%mm3, %%mm5            \n\t"
1478

    
1479
        "movq 3*8+%3, %%mm0            \n\t"
1480
        "add $24, %%"REG_S"              \n\t"
1481

    
1482
        "movq %%mm7, "DCTSIZE_S"*1*2(%%"REG_D") \n\t"
1483
        "paddw %%mm0, %%mm6            \n\t"
1484

    
1485
        "movq %%mm4, "DCTSIZE_S"*2*2(%%"REG_D") \n\t"
1486
        "psubw %%mm2, %%mm0            \n\t"
1487

    
1488
        "movq "DCTSIZE_S"*4*2(%%"REG_D"), %%mm7 \n\t"
1489
        "paddw %%mm2, %%mm6            \n\t"
1490

    
1491
        "movq %%mm5, "DCTSIZE_S"*5*2(%%"REG_D") \n\t"
1492
        "paddw %%mm0, %%mm7            \n\t"
1493

    
1494
        "movq %%mm6, "DCTSIZE_S"*3*2(%%"REG_D") \n\t"
1495

    
1496
        "movq %%mm7, "DCTSIZE_S"*4*2(%%"REG_D") \n\t"
1497
        "add $24, %%"REG_D"              \n\t"
1498
        "sub $2, %%"REG_c"               \n\t"
1499
        "jnz 1b                \n\t"
1500
        "jmp 5f                   \n\t"
1501

    
1502
        "3:                    \n\t"
1503
        //--- non DC2
1504
        //psraw mm1, 2 w/o it -> offset. thr1, thr1, thr1  (actually thr1, thr1, thr1-1)
1505
        //psraw mm5, 2
1506
        //psraw mm0, 2
1507
        //psraw mm6, 2
1508
        "movq %%mm5, %%mm3             \n\t"
1509
        "psubw %%mm1, %%mm5            \n\t"
1510

    
1511
        "psllw $1, %%mm5              \n\t" //'z10
1512
        "paddw %%mm1, %%mm3            \n\t" //'z13
1513

    
1514
        "movq %%mm0, %%mm2             \n\t"
1515
        "psubw %%mm6, %%mm0            \n\t"
1516

    
1517
        "movq %%mm5, %%mm1             \n\t"
1518
        "psllw $1, %%mm0              \n\t" //'z12
1519

    
1520
        "pmulhw "MANGLE(MM_FIX_2_613125930)", %%mm1 \n\t" //-
1521
        "paddw %%mm0, %%mm5            \n\t"
1522

    
1523
        "pmulhw "MANGLE(MM_FIX_1_847759065)", %%mm5 \n\t" //'z5
1524
        "paddw %%mm6, %%mm2            \n\t" //'z11
1525

    
1526
        "pmulhw "MANGLE(MM_FIX_1_082392200)", %%mm0 \n\t"
1527
        "movq %%mm2, %%mm7             \n\t"
1528

    
1529
        //---
1530
        "movq 0*8+%3, %%mm4            \n\t"
1531
        "psubw %%mm3, %%mm2            \n\t"
1532

    
1533
        "psllw $1, %%mm2              \n\t"
1534
        "paddw %%mm3, %%mm7            \n\t" //'t7
1535

    
1536
        "pmulhw "MANGLE(MM_FIX_1_414213562)", %%mm2 \n\t" //'t11
1537
        "movq %%mm4, %%mm6             \n\t"
1538
        //paddw mm7, MM_2
1539
        "psraw $2, %%mm7              \n\t"
1540

    
1541
        "paddw "DCTSIZE_S"*0*2(%%"REG_D"), %%mm4 \n\t"
1542
        "psubw %%mm7, %%mm6            \n\t"
1543

    
1544
        "movq 1*8+%3, %%mm3            \n\t"
1545
        "paddw %%mm7, %%mm4            \n\t"
1546

    
1547
        "movq %%mm6, "DCTSIZE_S"*7*2(%%"REG_D") \n\t"
1548
        "paddw %%mm5, %%mm1            \n\t" //'t12
1549

    
1550
        "movq %%mm4, "DCTSIZE_S"*0*2(%%"REG_D") \n\t"
1551
        "psubw %%mm7, %%mm1            \n\t" //'t6
1552

    
1553
        "movq 2*8+%3, %%mm7            \n\t"
1554
        "psubw %%mm5, %%mm0            \n\t" //'t10
1555

    
1556
        "movq 3*8+%3, %%mm6            \n\t"
1557
        "movq %%mm3, %%mm5             \n\t"
1558

    
1559
        "paddw "DCTSIZE_S"*1*2(%%"REG_D"), %%mm3 \n\t"
1560
        "psubw %%mm1, %%mm5            \n\t"
1561

    
1562
        "psubw %%mm1, %%mm2            \n\t" //'t5
1563
        "paddw %%mm1, %%mm3            \n\t"
1564

    
1565
        "movq %%mm5, "DCTSIZE_S"*6*2(%%"REG_D") \n\t"
1566
        "movq %%mm7, %%mm4             \n\t"
1567

    
1568
        "paddw "DCTSIZE_S"*2*2(%%"REG_D"), %%mm7 \n\t"
1569
        "psubw %%mm2, %%mm4            \n\t"
1570

    
1571
        "paddw "DCTSIZE_S"*5*2(%%"REG_D"), %%mm4 \n\t"
1572
        "paddw %%mm2, %%mm7            \n\t"
1573

    
1574
        "movq %%mm3, "DCTSIZE_S"*1*2(%%"REG_D") \n\t"
1575
        "paddw %%mm2, %%mm0            \n\t" //'t4
1576

    
1577
        // 't4 't6 't5 - - - - 't7
1578
        "movq %%mm7, "DCTSIZE_S"*2*2(%%"REG_D") \n\t"
1579
        "movq %%mm6, %%mm1             \n\t"
1580

    
1581
        "paddw "DCTSIZE_S"*4*2(%%"REG_D"), %%mm6 \n\t"
1582
        "psubw %%mm0, %%mm1            \n\t"
1583

    
1584
        "paddw "DCTSIZE_S"*3*2(%%"REG_D"), %%mm1 \n\t"
1585
        "paddw %%mm0, %%mm6            \n\t"
1586

    
1587
        "movq %%mm4, "DCTSIZE_S"*5*2(%%"REG_D") \n\t"
1588
        "add $24, %%"REG_S"              \n\t"
1589

    
1590
        "movq %%mm6, "DCTSIZE_S"*4*2(%%"REG_D") \n\t"
1591

    
1592
        "movq %%mm1, "DCTSIZE_S"*3*2(%%"REG_D") \n\t"
1593
        "add $24, %%"REG_D"              \n\t"
1594
        "sub $2, %%"REG_c"               \n\t"
1595
        "jnz 1b                \n\t"
1596
        "5:                      \n\t"
1597

    
1598
        : "+S"(data), "+D"(output), "+c"(cnt), "=o"(temps)
1599
        : "d"(thr_adr)
1600
        : "%"REG_a
1601
        );
1602
}
1603

    
1604
#endif // HAVE_MMX
1605

    
1606
#if !HAVE_MMX
1607

    
1608
static void row_idct_c(DCTELEM* workspace,
1609
                       int16_t* output_adr, int output_stride, int cnt)
1610
{
1611
    int_simd16_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
1612
    int_simd16_t tmp10, tmp11, tmp12, tmp13;
1613
    int_simd16_t z5, z10, z11, z12, z13;
1614
    int16_t* outptr;
1615
    DCTELEM* wsptr;
1616

    
1617
    cnt*=4;
1618
    wsptr = workspace;
1619
    outptr = output_adr;
1620
    for (; cnt > 0; cnt--) {
1621
        // Even part
1622
        //Simd version reads 4x4 block and transposes it
1623
        tmp10 = ( wsptr[2] +  wsptr[3]);
1624
        tmp11 = ( wsptr[2] -  wsptr[3]);
1625

    
1626
        tmp13 = ( wsptr[0] +  wsptr[1]);
1627
        tmp12 = (MULTIPLY16H( wsptr[0] - wsptr[1], FIX_1_414213562_A)<<2) - tmp13;//this shift order to avoid overflow
1628

    
1629
        tmp0 = tmp10 + tmp13; //->temps
1630
        tmp3 = tmp10 - tmp13; //->temps
1631
        tmp1 = tmp11 + tmp12;
1632
        tmp2 = tmp11 - tmp12;
1633

    
1634
        // Odd part
1635
        //Also transpose, with previous:
1636
        // ---- ----      ||||
1637
        // ---- ---- idct ||||
1638
        // ---- ---- ---> ||||
1639
        // ---- ----      ||||
1640
        z13 = wsptr[4] + wsptr[5];
1641
        z10 = wsptr[4] - wsptr[5];
1642
        z11 = wsptr[6] + wsptr[7];
1643
        z12 = wsptr[6] - wsptr[7];
1644

    
1645
        tmp7 = z11 + z13;
1646
        tmp11 = MULTIPLY16H(z11 - z13, FIX_1_414213562);
1647

    
1648
        z5 =    MULTIPLY16H(z10 + z12, FIX_1_847759065);
1649
        tmp10 = MULTIPLY16H(z12, FIX_1_082392200) - z5;
1650
        tmp12 = MULTIPLY16H(z10, FIX_2_613125930) + z5; // - FIX_
1651

    
1652
        tmp6 = (tmp12<<3) - tmp7;
1653
        tmp5 = (tmp11<<3) - tmp6;
1654
        tmp4 = (tmp10<<3) + tmp5;
1655

    
1656
        // Final output stage: descale and write column
1657
        outptr[0*output_stride]+= DESCALE(tmp0 + tmp7, 3);
1658
        outptr[1*output_stride]+= DESCALE(tmp1 + tmp6, 3);
1659
        outptr[2*output_stride]+= DESCALE(tmp2 + tmp5, 3);
1660
        outptr[3*output_stride]+= DESCALE(tmp3 - tmp4, 3);
1661
        outptr[4*output_stride]+= DESCALE(tmp3 + tmp4, 3);
1662
        outptr[5*output_stride]+= DESCALE(tmp2 - tmp5, 3);
1663
        outptr[6*output_stride]+= DESCALE(tmp1 - tmp6, 3); //no += ?
1664
        outptr[7*output_stride]+= DESCALE(tmp0 - tmp7, 3); //no += ?
1665
        outptr++;
1666

    
1667
        wsptr += DCTSIZE;       // advance pointer to next row
1668
    }
1669
}
1670

    
1671
#else /* HAVE_MMX */
1672

    
1673
static void row_idct_mmx (DCTELEM* workspace,
1674
                          int16_t* output_adr,  int output_stride,  int cnt)
1675
{
1676
    uint64_t __attribute__((aligned(8))) temps[4];
1677
    __asm__ volatile(
1678
        "lea (%%"REG_a",%%"REG_a",2), %%"REG_d"    \n\t"
1679
        "1:                     \n\t"
1680
        "movq "DCTSIZE_S"*0*2(%%"REG_S"), %%mm0 \n\t"
1681
        //
1682

    
1683
        "movq "DCTSIZE_S"*1*2(%%"REG_S"), %%mm1 \n\t"
1684
        "movq %%mm0, %%mm4             \n\t"
1685

    
1686
        "movq "DCTSIZE_S"*2*2(%%"REG_S"), %%mm2 \n\t"
1687
        "punpcklwd %%mm1, %%mm0        \n\t"
1688

    
1689
        "movq "DCTSIZE_S"*3*2(%%"REG_S"), %%mm3 \n\t"
1690
        "punpckhwd %%mm1, %%mm4        \n\t"
1691

    
1692
        //transpose 4x4
1693
        "movq %%mm2, %%mm7             \n\t"
1694
        "punpcklwd %%mm3, %%mm2        \n\t"
1695

    
1696
        "movq %%mm0, %%mm6             \n\t"
1697
        "punpckldq %%mm2, %%mm0        \n\t" //0
1698

    
1699
        "punpckhdq %%mm2, %%mm6        \n\t" //1
1700
        "movq %%mm0, %%mm5             \n\t"
1701

    
1702
        "punpckhwd %%mm3, %%mm7        \n\t"
1703
        "psubw %%mm6, %%mm0            \n\t"
1704

    
1705
        "pmulhw "MANGLE(MM_FIX_1_414213562_A)", %%mm0 \n\t"
1706
        "movq %%mm4, %%mm2             \n\t"
1707

    
1708
        "punpckldq %%mm7, %%mm4        \n\t" //2
1709
        "paddw %%mm6, %%mm5            \n\t"
1710

    
1711
        "punpckhdq %%mm7, %%mm2        \n\t" //3
1712
        "movq %%mm4, %%mm1             \n\t"
1713

    
1714
        "psllw $2, %%mm0              \n\t"
1715
        "paddw %%mm2, %%mm4            \n\t" //t10
1716

    
1717
        "movq "DCTSIZE_S"*0*2+"DCTSIZE_S"(%%"REG_S"), %%mm3 \n\t"
1718
        "psubw %%mm2, %%mm1            \n\t" //t11
1719

    
1720
        "movq "DCTSIZE_S"*1*2+"DCTSIZE_S"(%%"REG_S"), %%mm2 \n\t"
1721
        "psubw %%mm5, %%mm0            \n\t"
1722

    
1723
        "movq %%mm4, %%mm6             \n\t"
1724
        "paddw %%mm5, %%mm4            \n\t" //t0
1725

    
1726
        "psubw %%mm5, %%mm6            \n\t" //t3
1727
        "movq %%mm1, %%mm7             \n\t"
1728

    
1729
        "movq "DCTSIZE_S"*2*2+"DCTSIZE_S"(%%"REG_S"), %%mm5 \n\t"
1730
        "paddw %%mm0, %%mm1            \n\t" //t1
1731

    
1732
        "movq %%mm4, 0*8+%3            \n\t" //t0
1733
        "movq %%mm3, %%mm4             \n\t"
1734

    
1735
        "movq %%mm6, 1*8+%3            \n\t" //t3
1736
        "punpcklwd %%mm2, %%mm3        \n\t"
1737

    
1738
        //transpose 4x4
1739
        "movq "DCTSIZE_S"*3*2+"DCTSIZE_S"(%%"REG_S"), %%mm6 \n\t"
1740
        "punpckhwd %%mm2, %%mm4        \n\t"
1741

    
1742
        "movq %%mm5, %%mm2             \n\t"
1743
        "punpcklwd %%mm6, %%mm5        \n\t"
1744

    
1745
        "psubw %%mm0, %%mm7            \n\t" //t2
1746
        "punpckhwd %%mm6, %%mm2        \n\t"
1747

    
1748
        "movq %%mm3, %%mm0             \n\t"
1749
        "punpckldq %%mm5, %%mm3        \n\t" //4
1750

    
1751
        "punpckhdq %%mm5, %%mm0        \n\t" //5
1752
        "movq %%mm4, %%mm5             \n\t"
1753

    
1754
        //
1755
        "movq %%mm3, %%mm6             \n\t"
1756
        "punpckldq %%mm2, %%mm4        \n\t" //6
1757

    
1758
        "psubw %%mm0, %%mm3            \n\t" //z10
1759
        "punpckhdq %%mm2, %%mm5        \n\t" //7
1760

    
1761
        "paddw %%mm0, %%mm6            \n\t" //z13
1762
        "movq %%mm4, %%mm2             \n\t"
1763

    
1764
        "movq %%mm3, %%mm0             \n\t"
1765
        "psubw %%mm5, %%mm4            \n\t" //z12
1766

    
1767
        "pmulhw "MANGLE(MM_FIX_2_613125930)", %%mm0 \n\t" //-
1768
        "paddw %%mm4, %%mm3            \n\t"
1769

    
1770
        "pmulhw "MANGLE(MM_FIX_1_847759065)", %%mm3 \n\t" //z5
1771
        "paddw %%mm5, %%mm2            \n\t" //z11  >
1772

    
1773
        "pmulhw "MANGLE(MM_FIX_1_082392200)", %%mm4 \n\t"
1774
        "movq %%mm2, %%mm5             \n\t"
1775

    
1776
        "psubw %%mm6, %%mm2            \n\t"
1777
        "paddw %%mm6, %%mm5            \n\t" //t7
1778

    
1779
        "pmulhw "MANGLE(MM_FIX_1_414213562)", %%mm2 \n\t" //t11
1780
        "paddw %%mm3, %%mm0            \n\t" //t12
1781

    
1782
        "psllw $3, %%mm0              \n\t"
1783
        "psubw %%mm3, %%mm4            \n\t" //t10
1784

    
1785
        "movq 0*8+%3, %%mm6            \n\t"
1786
        "movq %%mm1, %%mm3             \n\t"
1787

    
1788
        "psllw $3, %%mm4              \n\t"
1789
        "psubw %%mm5, %%mm0            \n\t" //t6
1790

    
1791
        "psllw $3, %%mm2              \n\t"
1792
        "paddw %%mm0, %%mm1            \n\t" //d1
1793

    
1794
        "psubw %%mm0, %%mm2            \n\t" //t5
1795
        "psubw %%mm0, %%mm3            \n\t" //d6
1796

    
1797
        "paddw %%mm2, %%mm4            \n\t" //t4
1798
        "movq %%mm7, %%mm0             \n\t"
1799

    
1800
        "paddw %%mm2, %%mm7            \n\t" //d2
1801
        "psubw %%mm2, %%mm0            \n\t" //d5
1802

    
1803
        "movq "MANGLE(MM_DESCALE_RND)", %%mm2   \n\t" //4
1804
        "psubw %%mm5, %%mm6            \n\t" //d7
1805

    
1806
        "paddw 0*8+%3, %%mm5           \n\t" //d0
1807
        "paddw %%mm2, %%mm1            \n\t"
1808

    
1809
        "paddw %%mm2, %%mm5            \n\t"
1810
        "psraw $3, %%mm1              \n\t"
1811

    
1812
        "paddw %%mm2, %%mm7            \n\t"
1813
        "psraw $3, %%mm5              \n\t"
1814

    
1815
        "paddw (%%"REG_D"), %%mm5          \n\t"
1816
        "psraw $3, %%mm7              \n\t"
1817

    
1818
        "paddw (%%"REG_D",%%"REG_a",), %%mm1    \n\t"
1819
        "paddw %%mm2, %%mm0            \n\t"
1820

    
1821
        "paddw (%%"REG_D",%%"REG_a",2), %%mm7   \n\t"
1822
        "paddw %%mm2, %%mm3            \n\t"
1823

    
1824
        "movq %%mm5, (%%"REG_D")           \n\t"
1825
        "paddw %%mm2, %%mm6            \n\t"
1826

    
1827
        "movq %%mm1, (%%"REG_D",%%"REG_a",)     \n\t"
1828
        "psraw $3, %%mm0              \n\t"
1829

    
1830
        "movq %%mm7, (%%"REG_D",%%"REG_a",2)    \n\t"
1831
        "add %%"REG_d", %%"REG_D"             \n\t" //3*ls
1832

    
1833
        "movq 1*8+%3, %%mm5           \n\t" //t3
1834
        "psraw $3, %%mm3              \n\t"
1835

    
1836
        "paddw (%%"REG_D",%%"REG_a",2), %%mm0   \n\t"
1837
        "psubw %%mm4, %%mm5            \n\t" //d3
1838

    
1839
        "paddw (%%"REG_D",%%"REG_d",), %%mm3    \n\t"
1840
        "psraw $3, %%mm6              \n\t"
1841

    
1842
        "paddw 1*8+%3, %%mm4           \n\t" //d4
1843
        "paddw %%mm2, %%mm5            \n\t"
1844

    
1845
        "paddw (%%"REG_D",%%"REG_a",4), %%mm6   \n\t"
1846
        "paddw %%mm2, %%mm4            \n\t"
1847

    
1848
        "movq %%mm0, (%%"REG_D",%%"REG_a",2)    \n\t"
1849
        "psraw $3, %%mm5              \n\t"
1850

    
1851
        "paddw (%%"REG_D"), %%mm5          \n\t"
1852
        "psraw $3, %%mm4              \n\t"
1853

    
1854
        "paddw (%%"REG_D",%%"REG_a",), %%mm4    \n\t"
1855
        "add $"DCTSIZE_S"*2*4, %%"REG_S"      \n\t" //4 rows
1856

    
1857
        "movq %%mm3, (%%"REG_D",%%"REG_d",)     \n\t"
1858
        "movq %%mm6, (%%"REG_D",%%"REG_a",4)    \n\t"
1859
        "movq %%mm5, (%%"REG_D")           \n\t"
1860
        "movq %%mm4, (%%"REG_D",%%"REG_a",)     \n\t"
1861

    
1862
        "sub %%"REG_d", %%"REG_D"             \n\t"
1863
        "add $8, %%"REG_D"               \n\t"
1864
        "dec %%"REG_c"                   \n\t"
1865
        "jnz 1b                  \n\t"
1866

    
1867
        : "+S"(workspace), "+D"(output_adr), "+c"(cnt), "=o"(temps)
1868
        : "a"(output_stride*sizeof(short))
1869
        : "%"REG_d
1870
        );
1871
}
1872

    
1873
#endif // HAVE_MMX
1874

    
1875
#if !HAVE_MMX
1876

    
1877
static void row_fdct_c(DCTELEM *data, const uint8_t *pixels, int line_size, int cnt)
1878
{
1879
    int_simd16_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
1880
    int_simd16_t tmp10, tmp11, tmp12, tmp13;
1881
    int_simd16_t z1, z2, z3, z4, z5, z11, z13;
1882
    DCTELEM *dataptr;
1883

    
1884
    cnt*=4;
1885
    // Pass 1: process rows.
1886

    
1887
    dataptr = data;
1888
    for (; cnt > 0; cnt--) {
1889
        tmp0 = pixels[line_size*0] + pixels[line_size*7];
1890
        tmp7 = pixels[line_size*0] - pixels[line_size*7];
1891
        tmp1 = pixels[line_size*1] + pixels[line_size*6];
1892
        tmp6 = pixels[line_size*1] - pixels[line_size*6];
1893
        tmp2 = pixels[line_size*2] + pixels[line_size*5];
1894
        tmp5 = pixels[line_size*2] - pixels[line_size*5];
1895
        tmp3 = pixels[line_size*3] + pixels[line_size*4];
1896
        tmp4 = pixels[line_size*3] - pixels[line_size*4];
1897

    
1898
        // Even part
1899

    
1900
        tmp10 = tmp0 + tmp3;
1901
        tmp13 = tmp0 - tmp3;
1902
        tmp11 = tmp1 + tmp2;
1903
        tmp12 = tmp1 - tmp2;
1904
        //Even columns are written first, this leads to different order of columns
1905
        //in column_fidct(), but they are processed independently, so all ok.
1906
        //Later in the row_idct() columns readed at the same order.
1907
        dataptr[2] = tmp10 + tmp11;
1908
        dataptr[3] = tmp10 - tmp11;
1909

    
1910
        z1 = MULTIPLY16H((tmp12 + tmp13)<<2, FIX_0_707106781);
1911
        dataptr[0] = tmp13 + z1;
1912
        dataptr[1] = tmp13 - z1;
1913

    
1914
        // Odd part
1915

    
1916
        tmp10 = (tmp4 + tmp5) <<2;
1917
        tmp11 = (tmp5 + tmp6) <<2;
1918
        tmp12 = (tmp6 + tmp7) <<2;
1919

    
1920
        z5 = MULTIPLY16H(tmp10 - tmp12, FIX_0_382683433);
1921
        z2 = MULTIPLY16H(tmp10, FIX_0_541196100) + z5;
1922
        z4 = MULTIPLY16H(tmp12, FIX_1_306562965) + z5;
1923
        z3 = MULTIPLY16H(tmp11, FIX_0_707106781);
1924

    
1925
        z11 = tmp7 + z3;
1926
        z13 = tmp7 - z3;
1927

    
1928
        dataptr[4] = z13 + z2;
1929
        dataptr[5] = z13 - z2;
1930
        dataptr[6] = z11 + z4;
1931
        dataptr[7] = z11 - z4;
1932

    
1933
        pixels++;               // advance pointer to next column
1934
        dataptr += DCTSIZE;
1935
    }
1936
}
1937

    
1938
#else /* HAVE_MMX */
1939

    
1940
static void row_fdct_mmx(DCTELEM *data,  const uint8_t *pixels,  int line_size,  int cnt)
1941
{
1942
    uint64_t __attribute__((aligned(8))) temps[4];
1943
    __asm__ volatile(
1944
        "lea (%%"REG_a",%%"REG_a",2), %%"REG_d"    \n\t"
1945
        "6:                     \n\t"
1946
        "movd (%%"REG_S"), %%mm0           \n\t"
1947
        "pxor %%mm7, %%mm7             \n\t"
1948

    
1949
        "movd (%%"REG_S",%%"REG_a",), %%mm1     \n\t"
1950
        "punpcklbw %%mm7, %%mm0        \n\t"
1951

    
1952
        "movd (%%"REG_S",%%"REG_a",2), %%mm2    \n\t"
1953
        "punpcklbw %%mm7, %%mm1        \n\t"
1954

    
1955
        "punpcklbw %%mm7, %%mm2        \n\t"
1956
        "add %%"REG_d", %%"REG_S"             \n\t"
1957

    
1958
        "movq %%mm0, %%mm5             \n\t"
1959
        //
1960

    
1961
        "movd (%%"REG_S",%%"REG_a",4), %%mm3    \n\t" //7  ;prefetch!
1962
        "movq %%mm1, %%mm6             \n\t"
1963

    
1964
        "movd (%%"REG_S",%%"REG_d",), %%mm4     \n\t" //6
1965
        "punpcklbw %%mm7, %%mm3        \n\t"
1966

    
1967
        "psubw %%mm3, %%mm5            \n\t"
1968
        "punpcklbw %%mm7, %%mm4        \n\t"
1969

    
1970
        "paddw %%mm3, %%mm0            \n\t"
1971
        "psubw %%mm4, %%mm6            \n\t"
1972

    
1973
        "movd (%%"REG_S",%%"REG_a",2), %%mm3    \n\t" //5
1974
        "paddw %%mm4, %%mm1            \n\t"
1975

    
1976
        "movq %%mm5, 0*8+%3            \n\t" //t7
1977
        "punpcklbw %%mm7, %%mm3        \n\t"
1978

    
1979
        "movq %%mm6, 1*8+%3            \n\t" //t6
1980
        "movq %%mm2, %%mm4             \n\t"
1981

    
1982
        "movd (%%"REG_S"), %%mm5           \n\t" //3
1983
        "paddw %%mm3, %%mm2            \n\t"
1984

    
1985
        "movd (%%"REG_S",%%"REG_a",), %%mm6     \n\t" //4
1986
        "punpcklbw %%mm7, %%mm5        \n\t"
1987

    
1988
        "psubw %%mm3, %%mm4            \n\t"
1989
        "punpcklbw %%mm7, %%mm6        \n\t"
1990

    
1991
        "movq %%mm5, %%mm3             \n\t"
1992
        "paddw %%mm6, %%mm5            \n\t" //t3
1993

    
1994
        "psubw %%mm6, %%mm3            \n\t" //t4  ; t0 t1 t2 t4 t5 t3 - -
1995
        "movq %%mm0, %%mm6             \n\t"
1996

    
1997
        "movq %%mm1, %%mm7             \n\t"
1998
        "psubw %%mm5, %%mm0            \n\t" //t13
1999

    
2000
        "psubw %%mm2, %%mm1            \n\t"
2001
        "paddw %%mm2, %%mm7            \n\t" //t11
2002

    
2003
        "paddw %%mm0, %%mm1            \n\t"
2004
        "movq %%mm7, %%mm2             \n\t"
2005

    
2006
        "psllw $2, %%mm1              \n\t"
2007
        "paddw %%mm5, %%mm6            \n\t" //t10
2008

    
2009
        "pmulhw "MANGLE(MM_FIX_0_707106781)", %%mm1 \n\t"
2010
        "paddw %%mm6, %%mm7            \n\t" //d2
2011

    
2012
        "psubw %%mm2, %%mm6            \n\t" //d3
2013
        "movq %%mm0, %%mm5             \n\t"
2014

    
2015
        //transpose 4x4
2016
        "movq %%mm7, %%mm2             \n\t"
2017
        "punpcklwd %%mm6, %%mm7        \n\t"
2018

    
2019
        "paddw %%mm1, %%mm0            \n\t" //d0
2020
        "punpckhwd %%mm6, %%mm2        \n\t"
2021

    
2022
        "psubw %%mm1, %%mm5            \n\t" //d1
2023
        "movq %%mm0, %%mm6             \n\t"
2024

    
2025
        "movq 1*8+%3, %%mm1            \n\t"
2026
        "punpcklwd %%mm5, %%mm0        \n\t"
2027

    
2028
        "punpckhwd %%mm5, %%mm6        \n\t"
2029
        "movq %%mm0, %%mm5             \n\t"
2030

    
2031
        "punpckldq %%mm7, %%mm0        \n\t" //0
2032
        "paddw %%mm4, %%mm3            \n\t"
2033

    
2034
        "punpckhdq %%mm7, %%mm5        \n\t" //1
2035
        "movq %%mm6, %%mm7             \n\t"
2036

    
2037
        "movq %%mm0, "DCTSIZE_S"*0*2(%%"REG_D") \n\t"
2038
        "punpckldq %%mm2, %%mm6        \n\t" //2
2039

    
2040
        "movq %%mm5, "DCTSIZE_S"*1*2(%%"REG_D") \n\t"
2041
        "punpckhdq %%mm2, %%mm7        \n\t" //3
2042

    
2043
        "movq %%mm6, "DCTSIZE_S"*2*2(%%"REG_D") \n\t"
2044
        "paddw %%mm1, %%mm4            \n\t"
2045

    
2046
        "movq %%mm7, "DCTSIZE_S"*3*2(%%"REG_D") \n\t"
2047
        "psllw $2, %%mm3              \n\t" //t10
2048

    
2049
        "movq 0*8+%3, %%mm2           \n\t"
2050
        "psllw $2, %%mm4              \n\t" //t11
2051

    
2052
        "pmulhw "MANGLE(MM_FIX_0_707106781)", %%mm4 \n\t" //z3
2053
        "paddw %%mm2, %%mm1            \n\t"
2054

    
2055
        "psllw $2, %%mm1              \n\t" //t12
2056
        "movq %%mm3, %%mm0             \n\t"
2057

    
2058
        "pmulhw "MANGLE(MM_FIX_0_541196100)", %%mm0 \n\t"
2059
        "psubw %%mm1, %%mm3            \n\t"
2060

    
2061
        "pmulhw "MANGLE(MM_FIX_0_382683433)", %%mm3 \n\t" //z5
2062
        "movq %%mm2, %%mm5             \n\t"
2063

    
2064
        "pmulhw "MANGLE(MM_FIX_1_306562965)", %%mm1 \n\t"
2065
        "psubw %%mm4, %%mm2            \n\t" //z13
2066

    
2067
        "paddw %%mm4, %%mm5            \n\t" //z11
2068
        "movq %%mm2, %%mm6             \n\t"
2069

    
2070
        "paddw %%mm3, %%mm0            \n\t" //z2
2071
        "movq %%mm5, %%mm7             \n\t"
2072

    
2073
        "paddw %%mm0, %%mm2            \n\t" //d4
2074
        "psubw %%mm0, %%mm6            \n\t" //d5
2075

    
2076
        "movq %%mm2, %%mm4             \n\t"
2077
        "paddw %%mm3, %%mm1            \n\t" //z4
2078

    
2079
        //transpose 4x4
2080
        "punpcklwd %%mm6, %%mm2        \n\t"
2081
        "paddw %%mm1, %%mm5            \n\t" //d6
2082

    
2083
        "punpckhwd %%mm6, %%mm4        \n\t"
2084
        "psubw %%mm1, %%mm7            \n\t" //d7
2085

    
2086
        "movq %%mm5, %%mm6             \n\t"
2087
        "punpcklwd %%mm7, %%mm5        \n\t"
2088

    
2089
        "punpckhwd %%mm7, %%mm6        \n\t"
2090
        "movq %%mm2, %%mm7             \n\t"
2091

    
2092
        "punpckldq %%mm5, %%mm2        \n\t" //4
2093
        "sub %%"REG_d", %%"REG_S"             \n\t"
2094

    
2095
        "punpckhdq %%mm5, %%mm7        \n\t" //5
2096
        "movq %%mm4, %%mm5             \n\t"
2097

    
2098
        "movq %%mm2, "DCTSIZE_S"*0*2+"DCTSIZE_S"(%%"REG_D") \n\t"
2099
        "punpckldq %%mm6, %%mm4        \n\t" //6
2100

    
2101
        "movq %%mm7, "DCTSIZE_S"*1*2+"DCTSIZE_S"(%%"REG_D") \n\t"
2102
        "punpckhdq %%mm6, %%mm5        \n\t" //7
2103

    
2104
        "movq %%mm4, "DCTSIZE_S"*2*2+"DCTSIZE_S"(%%"REG_D") \n\t"
2105
        "add $4, %%"REG_S"               \n\t"
2106

    
2107
        "movq %%mm5, "DCTSIZE_S"*3*2+"DCTSIZE_S"(%%"REG_D") \n\t"
2108
        "add $"DCTSIZE_S"*2*4, %%"REG_D"      \n\t" //4 rows
2109
        "dec %%"REG_c"                   \n\t"
2110
        "jnz 6b                  \n\t"
2111

    
2112
        : "+S"(pixels), "+D"(data), "+c"(cnt), "=o"(temps)
2113
        : "a"(line_size)
2114
        : "%"REG_d);
2115
}
2116

    
2117
#endif // HAVE_MMX