x86: xvid: K&R formatting cosmetics
[libav.git] / libavcodec / x86 / xvididct_mmx.c
CommitLineData
7b94177e
DB
1/*
2 * XVID MPEG-4 VIDEO CODEC
3 * - MMX and XMM forward discrete cosine transform -
4 *
5 * Copyright(C) 2001 Peter Ross <pross@xvid.org>
6 *
7 * Originally provided by Intel at AP-922
8 * http://developer.intel.com/vtune/cbts/strmsimd/922down.htm
9 * (See more app notes at http://developer.intel.com/vtune/cbts/strmsimd/appnotes.htm)
10 * but in a limited edition.
11 * New macro implements a column part for precise iDCT
12 * The routine precision now satisfies IEEE standard 1180-1990.
13 *
14 * Copyright(C) 2000-2001 Peter Gubanov <peter@elecard.net.ru>
15 * Rounding trick Copyright(C) 2000 Michel Lespinasse <walken@zoy.org>
16 *
17 * http://www.elecard.com/peter/idct.html
18 * http://www.linuxvideo.org/mpeg2dec/
19 *
20 * These examples contain code fragments for first stage iDCT 8x8
21 * (for rows) and first stage DCT 8x8 (for columns)
22 *
23 * conversion to gcc syntax by Michael Niedermayer
24 *
2912e87a 25 * This file is part of Libav.
7b94177e 26 *
2912e87a 27 * Libav is free software; you can redistribute it and/or
7b94177e
DB
28 * modify it under the terms of the GNU Lesser General Public
29 * License as published by the Free Software Foundation; either
30 * version 2.1 of the License, or (at your option) any later version.
31 *
2912e87a 32 * Libav is distributed in the hope that it will be useful,
7b94177e
DB
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 * Lesser General Public License for more details.
36 *
37 * You should have received a copy of the GNU Lesser General Public License
2912e87a 38 * along with Libav; if not, write to the Free Software Foundation,
7b94177e
DB
39 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
40 */
84740d59
MN
41
42#include <inttypes.h>
1d9c2dc8
MS
43
44#include "config.h"
8d27bf1c 45
1d9c2dc8 46#include "libavutil/mem.h"
dcb7c868 47
8d27bf1c
DB
48#include "libavcodec/avcodec.h"
49
e3fcb143 50#include "idctdsp.h"
dcb7c868 51#include "xvididct.h"
84740d59 52
0b8b2ae5 53#if HAVE_MMX_INLINE
79195ce5 54
8d27bf1c 55// -----------------------------------------------------------------------------
84740d59 56// Various memory constants (trigonometric values or rounding values)
8d27bf1c
DB
57// -----------------------------------------------------------------------------
58
59DECLARE_ALIGNED(8, static const int16_t, tg_1_16)[4 * 4] = {
60 13036, 13036, 13036, 13036, // tg * (2 << 16) + 0.5
61 27146, 27146, 27146, 27146, // tg * (2 << 16) + 0.5
62 -21746, -21746, -21746, -21746, // tg * (2 << 16) + 0.5
63 23170, 23170, 23170, 23170
64}; // cos * (2 << 15) + 0.5
65
66DECLARE_ALIGNED(8, static const int32_t, rounder_0)[2 * 8] = {
67 65536, 65536,
68 3597, 3597,
69 2260, 2260,
70 1203, 1203,
71 0, 0,
72 120, 120,
73 512, 512,
74 512, 512
75};
76
77// -----------------------------------------------------------------------------
84740d59
MN
78//
79// The first stage iDCT 8x8 - inverse DCTs of rows
80//
8d27bf1c 81// -----------------------------------------------------------------------------
84740d59 82// The 8-point inverse DCT direct algorithm
8d27bf1c 83// -----------------------------------------------------------------------------
84740d59
MN
84//
85// static const short w[32] = {
8d27bf1c
DB
86// FIX(cos_4_16), FIX(cos_2_16), FIX(cos_4_16), FIX(cos_6_16),
87// FIX(cos_4_16), FIX(cos_6_16), -FIX(cos_4_16), -FIX(cos_2_16),
88// FIX(cos_4_16), -FIX(cos_6_16), -FIX(cos_4_16), FIX(cos_2_16),
89// FIX(cos_4_16), -FIX(cos_2_16), FIX(cos_4_16), -FIX(cos_6_16),
90// FIX(cos_1_16), FIX(cos_3_16), FIX(cos_5_16), FIX(cos_7_16),
91// FIX(cos_3_16), -FIX(cos_7_16), -FIX(cos_1_16), -FIX(cos_5_16),
92// FIX(cos_5_16), -FIX(cos_1_16), FIX(cos_7_16), FIX(cos_3_16),
93// FIX(cos_7_16), -FIX(cos_5_16), FIX(cos_3_16), -FIX(cos_1_16) };
84740d59
MN
94//
95// #define DCT_8_INV_ROW(x, y)
96// {
8d27bf1c 97// int a0, a1, a2, a3, b0, b1, b2, b3;
84740d59 98//
8d27bf1c
DB
99// a0 = x[0] * w[0] + x[2] * w[1] + x[4] * w[2] + x[6] * w[3];
100// a1 = x[0] * w[4] + x[2] * w[5] + x[4] * w[6] + x[6] * w[7];
101// a2 = x[0] * w[8] + x[2] * w[9] + x[4] * w[10] + x[6] * w[11];
102// a3 = x[0] * w[12] + x[2] * w[13] + x[4] * w[14] + x[6] * w[15];
103// b0 = x[1] * w[16] + x[3] * w[17] + x[5] * w[18] + x[7] * w[19];
104// b1 = x[1] * w[20] + x[3] * w[21] + x[5] * w[22] + x[7] * w[23];
105// b2 = x[1] * w[24] + x[3] * w[25] + x[5] * w[26] + x[7] * w[27];
106// b3 = x[1] * w[28] + x[3] * w[29] + x[5] * w[30] + x[7] * w[31];
84740d59 107//
8d27bf1c
DB
108// y[0] = SHIFT_ROUND(a0 + b0);
109// y[1] = SHIFT_ROUND(a1 + b1);
110// y[2] = SHIFT_ROUND(a2 + b2);
111// y[3] = SHIFT_ROUND(a3 + b3);
112// y[4] = SHIFT_ROUND(a3 - b3);
113// y[5] = SHIFT_ROUND(a2 - b2);
114// y[6] = SHIFT_ROUND(a1 - b1);
115// y[7] = SHIFT_ROUND(a0 - b0);
84740d59
MN
116// }
117//
8d27bf1c 118// -----------------------------------------------------------------------------
84740d59
MN
119//
120// In this implementation the outputs of the iDCT-1D are multiplied
8d27bf1c
DB
121// for rows 0,4 - by cos_4_16,
122// for rows 1,7 - by cos_1_16,
123// for rows 2,6 - by cos_2_16,
124// for rows 3,5 - by cos_3_16
125// and are shifted to the left for better accuracy.
84740d59
MN
126//
127// For the constants used,
8d27bf1c 128// FIX(float_const) = (short) (float_const * (1 << 15) + 0.5)
84740d59 129//
8d27bf1c 130// -----------------------------------------------------------------------------
84740d59 131
8d27bf1c 132// -----------------------------------------------------------------------------
84740d59 133// Tables for mmx processors
8d27bf1c 134// -----------------------------------------------------------------------------
84740d59
MN
135
136// Table for rows 0,4 - constants are multiplied by cos_4_16
8d27bf1c
DB
137DECLARE_ALIGNED(8, static const int16_t, tab_i_04_mmx)[32 * 4] = {
138 16384, 16384, 16384, -16384, // movq-> w06 w04 w02 w00
139 21407, 8867, 8867, -21407, // w07 w05 w03 w01
140 16384, -16384, 16384, 16384, // w14 w12 w10 w08
141 -8867, 21407, -21407, -8867, // w15 w13 w11 w09
142 22725, 12873, 19266, -22725, // w22 w20 w18 w16
143 19266, 4520, -4520, -12873, // w23 w21 w19 w17
144 12873, 4520, 4520, 19266, // w30 w28 w26 w24
145 -22725, 19266, -12873, -22725, // w31 w29 w27 w25
84740d59 146// Table for rows 1,7 - constants are multiplied by cos_1_16
8d27bf1c
DB
147 22725, 22725, 22725, -22725, // movq-> w06 w04 w02 w00
148 29692, 12299, 12299, -29692, // w07 w05 w03 w01
149 22725, -22725, 22725, 22725, // w14 w12 w10 w08
150 -12299, 29692, -29692, -12299, // w15 w13 w11 w09
151 31521, 17855, 26722, -31521, // w22 w20 w18 w16
152 26722, 6270, -6270, -17855, // w23 w21 w19 w17
153 17855, 6270, 6270, 26722, // w30 w28 w26 w24
154 -31521, 26722, -17855, -31521, // w31 w29 w27 w25
84740d59 155// Table for rows 2,6 - constants are multiplied by cos_2_16
8d27bf1c
DB
156 21407, 21407, 21407, -21407, // movq-> w06 w04 w02 w00
157 27969, 11585, 11585, -27969, // w07 w05 w03 w01
158 21407, -21407, 21407, 21407, // w14 w12 w10 w08
159 -11585, 27969, -27969, -11585, // w15 w13 w11 w09
160 29692, 16819, 25172, -29692, // w22 w20 w18 w16
161 25172, 5906, -5906, -16819, // w23 w21 w19 w17
162 16819, 5906, 5906, 25172, // w30 w28 w26 w24
163 -29692, 25172, -16819, -29692, // w31 w29 w27 w25
84740d59 164// Table for rows 3,5 - constants are multiplied by cos_3_16
8d27bf1c
DB
165 19266, 19266, 19266, -19266, // movq-> w06 w04 w02 w00
166 25172, 10426, 10426, -25172, // w07 w05 w03 w01
167 19266, -19266, 19266, 19266, // w14 w12 w10 w08
168 -10426, 25172, -25172, -10426, // w15 w13 w11 w09
169 26722, 15137, 22654, -26722, // w22 w20 w18 w16
170 22654, 5315, -5315, -15137, // w23 w21 w19 w17
171 15137, 5315, 5315, 22654, // w30 w28 w26 w24
172 -26722, 22654, -15137, -26722, // w31 w29 w27 w25
84740d59 173};
8d27bf1c 174// -----------------------------------------------------------------------------
84740d59 175// Tables for xmm processors
8d27bf1c 176// -----------------------------------------------------------------------------
84740d59
MN
177
178// %3 for rows 0,4 - constants are multiplied by cos_4_16
8d27bf1c
DB
179DECLARE_ALIGNED(8, static const int16_t, tab_i_04_xmm)[32 * 4] = {
180 16384, 21407, 16384, 8867, // movq-> w05 w04 w01 w00
181 16384, 8867, -16384, -21407, // w07 w06 w03 w02
182 16384, -8867, 16384, -21407, // w13 w12 w09 w08
183 -16384, 21407, 16384, -8867, // w15 w14 w11 w10
184 22725, 19266, 19266, -4520, // w21 w20 w17 w16
185 12873, 4520, -22725, -12873, // w23 w22 w19 w18
186 12873, -22725, 4520, -12873, // w29 w28 w25 w24
187 4520, 19266, 19266, -22725, // w31 w30 w27 w26
84740d59 188// %3 for rows 1,7 - constants are multiplied by cos_1_16
8d27bf1c
DB
189 22725, 29692, 22725, 12299, // movq-> w05 w04 w01 w00
190 22725, 12299, -22725, -29692, // w07 w06 w03 w02
191 22725, -12299, 22725, -29692, // w13 w12 w09 w08
192 -22725, 29692, 22725, -12299, // w15 w14 w11 w10
193 31521, 26722, 26722, -6270, // w21 w20 w17 w16
194 17855, 6270, -31521, -17855, // w23 w22 w19 w18
195 17855, -31521, 6270, -17855, // w29 w28 w25 w24
196 6270, 26722, 26722, -31521, // w31 w30 w27 w26
84740d59 197// %3 for rows 2,6 - constants are multiplied by cos_2_16
8d27bf1c
DB
198 21407, 27969, 21407, 11585, // movq-> w05 w04 w01 w00
199 21407, 11585, -21407, -27969, // w07 w06 w03 w02
200 21407, -11585, 21407, -27969, // w13 w12 w09 w08
201 -21407, 27969, 21407, -11585, // w15 w14 w11 w10
202 29692, 25172, 25172, -5906, // w21 w20 w17 w16
203 16819, 5906, -29692, -16819, // w23 w22 w19 w18
204 16819, -29692, 5906, -16819, // w29 w28 w25 w24
205 5906, 25172, 25172, -29692, // w31 w30 w27 w26
84740d59 206// %3 for rows 3,5 - constants are multiplied by cos_3_16
8d27bf1c
DB
207 19266, 25172, 19266, 10426, // movq-> w05 w04 w01 w00
208 19266, 10426, -19266, -25172, // w07 w06 w03 w02
209 19266, -10426, 19266, -25172, // w13 w12 w09 w08
210 -19266, 25172, 19266, -10426, // w15 w14 w11 w10
211 26722, 22654, 22654, -5315, // w21 w20 w17 w16
212 15137, 5315, -26722, -15137, // w23 w22 w19 w18
213 15137, -26722, 5315, -15137, // w29 w28 w25 w24
214 5315, 22654, 22654, -26722, // w31 w30 w27 w26
84740d59 215};
8d27bf1c 216// =============================================================================
84740d59 217// Helper macros for the code
8d27bf1c 218// =============================================================================
84740d59 219
8d27bf1c 220// -----------------------------------------------------------------------------
84740d59 221// DCT_8_INV_ROW_MMX( INP, OUT, TABLE, ROUNDER
8d27bf1c
DB
222// -----------------------------------------------------------------------------
223
224#define DCT_8_INV_ROW_MMX(A1, A2, A3, A4) \
225 "movq "#A1", %%mm0 \n\t" /* 0 ; x3 x2 x1 x0 */ \
226 "movq 8+"#A1", %%mm1 \n\t" /* 1 ; x7 x6 x5 x4 */ \
227 "movq %%mm0, %%mm2 \n\t" /* 2 ; x3 x2 x1 x0 */ \
228 "movq "#A3", %%mm3 \n\t" /* 3 ; w06 w04 w02 w00 */ \
229 "punpcklwd %%mm1, %%mm0 \n\t" /* x5 x1 x4 x0 */ \
230 "movq %%mm0, %%mm5 \n\t" /* 5 ; x5 x1 x4 x0 */ \
231 "punpckldq %%mm0, %%mm0 \n\t" /* x4 x0 x4 x0 */ \
232 "movq 8+"#A3", %%mm4 \n\t" /* 4 ; w07 w05 w03 w01 */ \
233 "punpckhwd %%mm1, %%mm2 \n\t" /* 1 ; x7 x3 x6 x2 */ \
234 "pmaddwd %%mm0, %%mm3 \n\t" /* x4*w06+x0*w04 x4*w02+x0*w00 */ \
235 "movq %%mm2, %%mm6 \n\t" /* 6 ; x7 x3 x6 x2 */ \
236 "movq 32+"#A3", %%mm1 \n\t" /* 1 ; w22 w20 w18 w16 */ \
237 "punpckldq %%mm2, %%mm2 \n\t" /* x6 x2 x6 x2 */ \
238 "pmaddwd %%mm2, %%mm4 \n\t" /* x6*w07+x2*w05 x6*w03+x2*w01 */ \
239 "punpckhdq %%mm5, %%mm5 \n\t" /* x5 x1 x5 x1 */ \
240 "pmaddwd 16+"#A3", %%mm0 \n\t" /* x4*w14+x0*w12 x4*w10+x0*w08 */ \
241 "punpckhdq %%mm6, %%mm6 \n\t" /* x7 x3 x7 x3 */ \
242 "movq 40+ "#A3", %%mm7 \n\t" /* 7 ; w23 w21 w19 w17 */ \
243 "pmaddwd %%mm5, %%mm1 \n\t" /* x5*w22+x1*w20 x5*w18+x1*w16 */ \
244 "paddd "#A4", %%mm3 \n\t" /* +%4 */ \
245 "pmaddwd %%mm6, %%mm7 \n\t" /* x7*w23+x3*w21 x7*w19+x3*w17 */ \
246 "pmaddwd 24+"#A3", %%mm2 \n\t" /* x6*w15+x2*w13 x6*w11+x2*w09 */ \
247 "paddd %%mm4, %%mm3 \n\t" /* 4 ; a1=sum(even1) a0=sum(even0) */ \
248 "pmaddwd 48+"#A3", %%mm5 \n\t" /* x5*w30+x1*w28 x5*w26+x1*w24 */ \
249 "movq %%mm3, %%mm4 \n\t" /* 4 ; a1 a0 */ \
250 "pmaddwd 56+"#A3", %%mm6 \n\t" /* x7*w31+x3*w29 x7*w27+x3*w25 */ \
251 "paddd %%mm7, %%mm1 \n\t" /* 7 ; b1=sum(odd1) b0=sum(odd0) */ \
252 "paddd "#A4", %%mm0 \n\t" /* +%4 */ \
253 "psubd %%mm1, %%mm3 \n\t" /* a1-b1 a0-b0 */ \
254 "psrad $11, %%mm3 \n\t" /* y6=a1-b1 y7=a0-b0 */ \
255 "paddd %%mm4, %%mm1 \n\t" /* 4 ; a1+b1 a0+b0 */ \
256 "paddd %%mm2, %%mm0 \n\t" /* 2 ; a3=sum(even3) a2=sum(even2) */ \
257 "psrad $11, %%mm1 \n\t" /* y1=a1+b1 y0=a0+b0 */ \
258 "paddd %%mm6, %%mm5 \n\t" /* 6 ; b3=sum(odd3) b2=sum(odd2) */ \
259 "movq %%mm0, %%mm4 \n\t" /* 4 ; a3 a2 */ \
260 "paddd %%mm5, %%mm0 \n\t" /* a3+b3 a2+b2 */ \
261 "psubd %%mm5, %%mm4 \n\t" /* 5 ; a3-b3 a2-b2 */ \
262 "psrad $11, %%mm0 \n\t" /* y3=a3+b3 y2=a2+b2 */ \
263 "psrad $11, %%mm4 \n\t" /* y4=a3-b3 y5=a2-b2 */ \
264 "packssdw %%mm0, %%mm1 \n\t" /* 0 ; y3 y2 y1 y0 */ \
265 "packssdw %%mm3, %%mm4 \n\t" /* 3 ; y6 y7 y4 y5 */ \
266 "movq %%mm4, %%mm7 \n\t" /* 7 ; y6 y7 y4 y5 */ \
267 "psrld $16, %%mm4 \n\t" /* 0 y6 0 y4 */ \
268 "pslld $16, %%mm7 \n\t" /* y7 0 y5 0 */ \
269 "movq %%mm1, "#A2" \n\t" /* 1 ; save y3 y2 y1 y0 */ \
270 "por %%mm4, %%mm7 \n\t" /* 4 ; y7 y6 y5 y4 */ \
271 "movq %%mm7, 8+"#A2" \n\t" /* 7 ; save y7 y6 y5 y4 */ \
272
273
274// -----------------------------------------------------------------------------
84740d59 275// DCT_8_INV_ROW_XMM( INP, OUT, TABLE, ROUNDER
8d27bf1c
DB
276// -----------------------------------------------------------------------------
277
278#define DCT_8_INV_ROW_XMM(A1, A2, A3, A4) \
279 "movq "#A1", %%mm0 \n\t" /* 0 ; x3 x2 x1 x0 */ \
280 "movq 8+"#A1", %%mm1 \n\t" /* 1 ; x7 x6 x5 x4 */ \
281 "movq %%mm0, %%mm2 \n\t" /* 2 ; x3 x2 x1 x0 */ \
282 "movq "#A3", %%mm3 \n\t" /* 3 ; w05 w04 w01 w00 */ \
283 "pshufw $0x88, %%mm0, %%mm0 \n\t" /* x2 x0 x2 x0 */ \
284 "movq 8+"#A3", %%mm4 \n\t" /* 4 ; w07 w06 w03 w02 */ \
285 "movq %%mm1, %%mm5 \n\t" /* 5 ; x7 x6 x5 x4 */ \
286 "pmaddwd %%mm0, %%mm3 \n\t" /* x2*w05+x0*w04 x2*w01+x0*w00 */ \
287 "movq 32+"#A3", %%mm6 \n\t" /* 6 ; w21 w20 w17 w16 */ \
288 "pshufw $0x88, %%mm1, %%mm1 \n\t" /* x6 x4 x6 x4 */ \
289 "pmaddwd %%mm1, %%mm4 \n\t" /* x6*w07+x4*w06 x6*w03+x4*w02 */ \
290 "movq 40+"#A3", %%mm7 \n\t" /* 7; w23 w22 w19 w18 */ \
291 "pshufw $0xdd, %%mm2, %%mm2 \n\t" /* x3 x1 x3 x1 */ \
292 "pmaddwd %%mm2, %%mm6 \n\t" /* x3*w21+x1*w20 x3*w17+x1*w16 */ \
293 "pshufw $0xdd, %%mm5, %%mm5 \n\t" /* x7 x5 x7 x5 */ \
294 "pmaddwd %%mm5, %%mm7 \n\t" /* x7*w23+x5*w22 x7*w19+x5*w18 */ \
295 "paddd "#A4", %%mm3 \n\t" /* +%4 */ \
296 "pmaddwd 16+"#A3", %%mm0 \n\t" /* x2*w13+x0*w12 x2*w09+x0*w08 */ \
297 "paddd %%mm4, %%mm3 \n\t" /* 4 ; a1=sum(even1) a0=sum(even0) */ \
298 "pmaddwd 24+"#A3", %%mm1 \n\t" /* x6*w15+x4*w14 x6*w11+x4*w10 */ \
299 "movq %%mm3, %%mm4 \n\t" /* 4 ; a1 a0 */ \
300 "pmaddwd 48+"#A3", %%mm2 \n\t" /* x3*w29+x1*w28 x3*w25+x1*w24 */ \
301 "paddd %%mm7, %%mm6 \n\t" /* 7 ; b1=sum(odd1) b0=sum(odd0) */ \
302 "pmaddwd 56+"#A3", %%mm5 \n\t" /* x7*w31+x5*w30 x7*w27+x5*w26 */ \
303 "paddd %%mm6, %%mm3 \n\t" /* a1+b1 a0+b0 */ \
304 "paddd "#A4", %%mm0 \n\t" /* +%4 */ \
305 "psrad $11, %%mm3 \n\t" /* y1=a1+b1 y0=a0+b0 */ \
306 "paddd %%mm1, %%mm0 \n\t" /* 1 ; a3=sum(even3) a2=sum(even2) */ \
307 "psubd %%mm6, %%mm4 \n\t" /* 6 ; a1-b1 a0-b0 */ \
308 "movq %%mm0, %%mm7 \n\t" /* 7 ; a3 a2 */ \
309 "paddd %%mm5, %%mm2 \n\t" /* 5 ; b3=sum(odd3) b2=sum(odd2) */ \
310 "paddd %%mm2, %%mm0 \n\t" /* a3+b3 a2+b2 */ \
311 "psrad $11, %%mm4 \n\t" /* y6=a1-b1 y7=a0-b0 */ \
312 "psubd %%mm2, %%mm7 \n\t" /* 2 ; a3-b3 a2-b2 */ \
313 "psrad $11, %%mm0 \n\t" /* y3=a3+b3 y2=a2+b2 */ \
314 "psrad $11, %%mm7 \n\t" /* y4=a3-b3 y5=a2-b2 */ \
315 "packssdw %%mm0, %%mm3 \n\t" /* 0 ; y3 y2 y1 y0 */ \
316 "packssdw %%mm4, %%mm7 \n\t" /* 4 ; y6 y7 y4 y5 */ \
317 "movq %%mm3, "#A2" \n\t" /* 3 ; save y3 y2 y1 y0 */ \
318 "pshufw $0xb1, %%mm7, %%mm7 \n\t" /* y7 y6 y5 y4 */ \
319 "movq %%mm7, 8+"#A2" \n\t" /* 7 ; save y7 y6 y5 y4 */ \
320
321
322// -----------------------------------------------------------------------------
84740d59
MN
323//
324// The first stage DCT 8x8 - forward DCTs of columns
325//
326// The %2puts are multiplied
327// for rows 0,4 - on cos_4_16,
328// for rows 1,7 - on cos_1_16,
329// for rows 2,6 - on cos_2_16,
330// for rows 3,5 - on cos_3_16
331// and are shifted to the left for rise of accuracy
332//
8d27bf1c 333// -----------------------------------------------------------------------------
84740d59
MN
334//
335// The 8-point scaled forward DCT algorithm (26a8m)
336//
8d27bf1c 337// -----------------------------------------------------------------------------
84740d59 338//
8d27bf1c
DB
339//#define DCT_8_FRW_COL(x, y)
340// {
341// short t0, t1, t2, t3, t4, t5, t6, t7;
342// short tp03, tm03, tp12, tm12, tp65, tm65;
343// short tp465, tm465, tp765, tm765;
84740d59 344//
8d27bf1c
DB
345// t0 = LEFT_SHIFT(x[0] + x[7]);
346// t1 = LEFT_SHIFT(x[1] + x[6]);
347// t2 = LEFT_SHIFT(x[2] + x[5]);
348// t3 = LEFT_SHIFT(x[3] + x[4]);
349// t4 = LEFT_SHIFT(x[3] - x[4]);
350// t5 = LEFT_SHIFT(x[2] - x[5]);
351// t6 = LEFT_SHIFT(x[1] - x[6]);
352// t7 = LEFT_SHIFT(x[0] - x[7]);
84740d59 353//
8d27bf1c
DB
354// tp03 = t0 + t3;
355// tm03 = t0 - t3;
356// tp12 = t1 + t2;
357// tm12 = t1 - t2;
84740d59 358//
8d27bf1c
DB
359// y[0] = tp03 + tp12;
360// y[4] = tp03 - tp12;
84740d59 361//
8d27bf1c
DB
362// y[2] = tm03 + tm12 * tg_2_16;
363// y[6] = tm03 * tg_2_16 - tm12;
84740d59 364//
8d27bf1c
DB
365// tp65 = (t6 + t5) * cos_4_16;
366// tm65 = (t6 - t5) * cos_4_16;
84740d59 367//
8d27bf1c
DB
368// tp765 = t7 + tp65;
369// tm765 = t7 - tp65;
370// tp465 = t4 + tm65;
371// tm465 = t4 - tm65;
84740d59 372//
8d27bf1c
DB
373// y[1] = tp765 + tp465 * tg_1_16;
374// y[7] = tp765 * tg_1_16 - tp465;
375// y[5] = tm765 * tg_3_16 + tm465;
376// y[3] = tm765 - tm465 * tg_3_16;
377// }
84740d59 378//
8d27bf1c 379// -----------------------------------------------------------------------------
84740d59 380
8d27bf1c 381// -----------------------------------------------------------------------------
84740d59 382// DCT_8_INV_COL_4 INP,OUT
8d27bf1c
DB
383// -----------------------------------------------------------------------------
384
385#define DCT_8_INV_COL(A1, A2) \
386 "movq 2*8(%3), %%mm0 \n\t" \
387 "movq 16*3+"#A1", %%mm3 \n\t" \
388 "movq %%mm0, %%mm1 \n\t" /* tg_3_16 */ \
389 "movq 16*5+"#A1", %%mm5 \n\t" \
390 "pmulhw %%mm3, %%mm0 \n\t" /* x3*(tg_3_16-1) */ \
391 "movq (%3), %%mm4 \n\t" \
392 "pmulhw %%mm5, %%mm1 \n\t" /* x5*(tg_3_16-1) */ \
393 "movq 16*7+"#A1", %%mm7 \n\t" \
394 "movq %%mm4, %%mm2 \n\t" /* tg_1_16 */ \
395 "movq 16*1+"#A1", %%mm6 \n\t" \
396 "pmulhw %%mm7, %%mm4 \n\t" /* x7*tg_1_16 */ \
397 "paddsw %%mm3, %%mm0 \n\t" /* x3*tg_3_16 */ \
398 "pmulhw %%mm6, %%mm2 \n\t" /* x1*tg_1_16 */ \
399 "paddsw %%mm3, %%mm1 \n\t" /* x3+x5*(tg_3_16-1) */ \
400 "psubsw %%mm5, %%mm0 \n\t" /* x3*tg_3_16-x5 = tm35 */ \
401 "movq 3*8(%3), %%mm3 \n\t" \
402 "paddsw %%mm5, %%mm1 \n\t" /* x3+x5*tg_3_16 = tp35 */ \
403 "paddsw %%mm6, %%mm4 \n\t" /* x1+tg_1_16*x7 = tp17 */ \
404 "psubsw %%mm7, %%mm2 \n\t" /* x1*tg_1_16-x7 = tm17 */ \
405 "movq %%mm4, %%mm5 \n\t" /* tp17 */ \
406 "movq %%mm2, %%mm6 \n\t" /* tm17 */ \
407 "paddsw %%mm1, %%mm5 \n\t" /* tp17+tp35 = b0 */ \
408 "psubsw %%mm0, %%mm6 \n\t" /* tm17-tm35 = b3 */ \
409 "psubsw %%mm1, %%mm4 \n\t" /* tp17-tp35 = t1 */ \
410 "paddsw %%mm0, %%mm2 \n\t" /* tm17+tm35 = t2 */ \
411 "movq 1*8(%3), %%mm7 \n\t" \
412 "movq %%mm4, %%mm1 \n\t" /* t1 */ \
413 "movq %%mm5, 3*16+"#A2" \n\t" /* save b0 */ \
414 "paddsw %%mm2, %%mm1 \n\t" /* t1+t2 */ \
415 "movq %%mm6, 5*16+"#A2" \n\t" /* save b3 */ \
416 "psubsw %%mm2, %%mm4 \n\t" /* t1-t2 */ \
417 "movq 2*16+"#A1", %%mm5 \n\t" \
418 "movq %%mm7, %%mm0 \n\t" /* tg_2_16 */ \
419 "movq 6*16+"#A1", %%mm6 \n\t" \
420 "pmulhw %%mm5, %%mm0 \n\t" /* x2*tg_2_16 */ \
421 "pmulhw %%mm6, %%mm7 \n\t" /* x6*tg_2_16 */ \
422 "pmulhw %%mm3, %%mm1 \n\t" /* ocos_4_16*(t1+t2) = b1/2 */ \
423 "movq 0*16+"#A1", %%mm2 \n\t" \
424 "pmulhw %%mm3, %%mm4 \n\t" /* ocos_4_16*(t1-t2) = b2/2 */ \
425 "psubsw %%mm6, %%mm0 \n\t" /* t2*tg_2_16-x6 = tm26 */ \
426 "movq %%mm2, %%mm3 \n\t" /* x0 */ \
427 "movq 4*16+"#A1", %%mm6 \n\t" \
428 "paddsw %%mm5, %%mm7 \n\t" /* x2+x6*tg_2_16 = tp26 */ \
429 "paddsw %%mm6, %%mm2 \n\t" /* x0+x4 = tp04 */ \
430 "psubsw %%mm6, %%mm3 \n\t" /* x0-x4 = tm04 */ \
431 "movq %%mm2, %%mm5 \n\t" /* tp04 */ \
432 "movq %%mm3, %%mm6 \n\t" /* tm04 */ \
433 "psubsw %%mm7, %%mm2 \n\t" /* tp04-tp26 = a3 */ \
434 "paddsw %%mm0, %%mm3 \n\t" /* tm04+tm26 = a1 */ \
435 "paddsw %%mm1, %%mm1 \n\t" /* b1 */ \
436 "paddsw %%mm4, %%mm4 \n\t" /* b2 */ \
437 "paddsw %%mm7, %%mm5 \n\t" /* tp04+tp26 = a0 */ \
438 "psubsw %%mm0, %%mm6 \n\t" /* tm04-tm26 = a2 */ \
439 "movq %%mm3, %%mm7 \n\t" /* a1 */ \
440 "movq %%mm6, %%mm0 \n\t" /* a2 */ \
441 "paddsw %%mm1, %%mm3 \n\t" /* a1+b1 */ \
442 "paddsw %%mm4, %%mm6 \n\t" /* a2+b2 */ \
443 "psraw $6, %%mm3 \n\t" /* dst1 */ \
444 "psubsw %%mm1, %%mm7 \n\t" /* a1-b1 */ \
445 "psraw $6, %%mm6 \n\t" /* dst2 */ \
446 "psubsw %%mm4, %%mm0 \n\t" /* a2-b2 */ \
447 "movq 3*16+"#A2", %%mm1 \n\t" /* load b0 */ \
448 "psraw $6, %%mm7 \n\t" /* dst6 */ \
449 "movq %%mm5, %%mm4 \n\t" /* a0 */ \
450 "psraw $6, %%mm0 \n\t" /* dst5 */ \
451 "movq %%mm3, 1*16+"#A2" \n\t" \
452 "paddsw %%mm1, %%mm5 \n\t" /* a0+b0 */ \
453 "movq %%mm6, 2*16+"#A2" \n\t" \
454 "psubsw %%mm1, %%mm4 \n\t" /* a0-b0 */ \
455 "movq 5*16+"#A2", %%mm3 \n\t" /* load b3 */ \
456 "psraw $6, %%mm5 \n\t" /* dst0 */ \
457 "movq %%mm2, %%mm6 \n\t" /* a3 */ \
458 "psraw $6, %%mm4 \n\t" /* dst7 */ \
459 "movq %%mm0, 5*16+"#A2" \n\t" \
460 "paddsw %%mm3, %%mm2 \n\t" /* a3+b3 */ \
461 "movq %%mm7, 6*16+"#A2" \n\t" \
462 "psubsw %%mm3, %%mm6 \n\t" /* a3-b3 */ \
463 "movq %%mm5, 0*16+"#A2" \n\t" \
464 "psraw $6, %%mm2 \n\t" /* dst3 */ \
465 "movq %%mm4, 7*16+"#A2" \n\t" \
466 "psraw $6, %%mm6 \n\t" /* dst4 */ \
467 "movq %%mm2, 3*16+"#A2" \n\t" \
468 "movq %%mm6, 4*16+"#A2" \n\t" \
469
470// =============================================================================
84740d59 471// Code
8d27bf1c 472// =============================================================================
84740d59 473
8d27bf1c 474// -----------------------------------------------------------------------------
84740d59 475// void idct_mmx(uint16_t block[64]);
8d27bf1c 476// -----------------------------------------------------------------------------
84740d59 477
dcb7c868
DB
478void ff_xvid_idct_mmx(short *block)
479{
8d27bf1c
DB
480 __asm__ volatile (
481 // # Process each row
482 DCT_8_INV_ROW_MMX(0 * 16(%0), 0 * 16(%0), 64 * 0(%2), 8 * 0(%1))
483 DCT_8_INV_ROW_MMX(1 * 16(%0), 1 * 16(%0), 64 * 1(%2), 8 * 1(%1))
484 DCT_8_INV_ROW_MMX(2 * 16(%0), 2 * 16(%0), 64 * 2(%2), 8 * 2(%1))
485 DCT_8_INV_ROW_MMX(3 * 16(%0), 3 * 16(%0), 64 * 3(%2), 8 * 3(%1))
486 DCT_8_INV_ROW_MMX(4 * 16(%0), 4 * 16(%0), 64 * 0(%2), 8 * 4(%1))
487 DCT_8_INV_ROW_MMX(5 * 16(%0), 5 * 16(%0), 64 * 3(%2), 8 * 5(%1))
488 DCT_8_INV_ROW_MMX(6 * 16(%0), 6 * 16(%0), 64 * 2(%2), 8 * 6(%1))
489 DCT_8_INV_ROW_MMX(7 * 16(%0), 7 * 16(%0), 64 * 1(%2), 8 * 7(%1))
490
491 // # Process the columns (4 at a time)
492 DCT_8_INV_COL(0(%0), 0(%0))
493 DCT_8_INV_COL(8(%0), 8(%0))
494 :: "r" (block), "r" (rounder_0), "r" (tab_i_04_mmx), "r" (tg_1_16));
84740d59
MN
495}
496
dcb7c868 497void ff_xvid_idct_mmx_put(uint8_t *dest, int line_size, int16_t *block)
0b8b2ae5 498{
dcb7c868 499 ff_xvid_idct_mmx(block);
0b8b2ae5
DB
500 ff_put_pixels_clamped_mmx(block, dest, line_size);
501}
502
dcb7c868 503void ff_xvid_idct_mmx_add(uint8_t *dest, int line_size, int16_t *block)
0b8b2ae5 504{
dcb7c868 505 ff_xvid_idct_mmx(block);
0b8b2ae5
DB
506 ff_add_pixels_clamped_mmx(block, dest, line_size);
507}
508
509#endif /* HAVE_MMX_INLINE */
510
511#if HAVE_MMXEXT_INLINE
512
8d27bf1c 513// -----------------------------------------------------------------------------
84740d59 514// void idct_xmm(uint16_t block[64]);
8d27bf1c 515// -----------------------------------------------------------------------------
84740d59 516
dcb7c868 517void ff_xvid_idct_mmxext(short *block)
d8eda370 518{
8d27bf1c
DB
519 __asm__ volatile (
520 // # Process each row
521 DCT_8_INV_ROW_XMM(0 * 16(%0), 0 * 16(%0), 64 * 0(%2), 8 * 0(%1))
522 DCT_8_INV_ROW_XMM(1 * 16(%0), 1 * 16(%0), 64 * 1(%2), 8 * 1(%1))
523 DCT_8_INV_ROW_XMM(2 * 16(%0), 2 * 16(%0), 64 * 2(%2), 8 * 2(%1))
524 DCT_8_INV_ROW_XMM(3 * 16(%0), 3 * 16(%0), 64 * 3(%2), 8 * 3(%1))
525 DCT_8_INV_ROW_XMM(4 * 16(%0), 4 * 16(%0), 64 * 0(%2), 8 * 4(%1))
526 DCT_8_INV_ROW_XMM(5 * 16(%0), 5 * 16(%0), 64 * 3(%2), 8 * 5(%1))
527 DCT_8_INV_ROW_XMM(6 * 16(%0), 6 * 16(%0), 64 * 2(%2), 8 * 6(%1))
528 DCT_8_INV_ROW_XMM(7 * 16(%0), 7 * 16(%0), 64 * 1(%2), 8 * 7(%1))
529
530 // # Process the columns (4 at a time)
531 DCT_8_INV_COL(0(%0), 0(%0))
532 DCT_8_INV_COL(8(%0), 8(%0))
533 :: "r" (block), "r" (rounder_0), "r" (tab_i_04_xmm), "r" (tg_1_16));
84740d59 534}
79195ce5 535
dcb7c868 536void ff_xvid_idct_mmxext_put(uint8_t *dest, int line_size, int16_t *block)
58139e14 537{
dcb7c868 538 ff_xvid_idct_mmxext(block);
58139e14
DB
539 ff_put_pixels_clamped_mmx(block, dest, line_size);
540}
541
dcb7c868 542void ff_xvid_idct_mmxext_add(uint8_t *dest, int line_size, int16_t *block)
58139e14 543{
dcb7c868 544 ff_xvid_idct_mmxext(block);
58139e14
DB
545 ff_add_pixels_clamped_mmx(block, dest, line_size);
546}
547
0b8b2ae5 548#endif /* HAVE_MMXEXT_INLINE */