configure check for whether we can compile ssse3
[libav.git] / libavcodec / i386 / mpegvideo_mmx_template.c
CommitLineData
2f349de2 1/*
ff4ec49e
FB
2 * MPEG video MMX templates
3 *
4 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
5 *
b78e7197
DB
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
ff4ec49e
FB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
ff4ec49e 12 *
b78e7197 13 * FFmpeg is distributed in the hope that it will be useful,
ff4ec49e
FB
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
b78e7197 19 * License along with FFmpeg; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
ff4ec49e 21 */
2f349de2
MN
22#undef SPREADW
23#undef PMAXW
8fb0d073 24#undef PMAX
2f349de2
MN
25#ifdef HAVE_MMX2
26#define SPREADW(a) "pshufw $0, " #a ", " #a " \n\t"
bb270c08 27#define PMAXW(a,b) "pmaxsw " #a ", " #b " \n\t"
12e96681 28#define PMAX(a,b) \
bb270c08
DB
29 "pshufw $0x0E," #a ", " #b " \n\t"\
30 PMAXW(b, a)\
31 "pshufw $0x01," #a ", " #b " \n\t"\
32 PMAXW(b, a)
2f349de2
MN
33#else
34#define SPREADW(a) \
bb270c08
DB
35 "punpcklwd " #a ", " #a " \n\t"\
36 "punpcklwd " #a ", " #a " \n\t"
2f349de2 37#define PMAXW(a,b) \
bb270c08
DB
38 "psubusw " #a ", " #b " \n\t"\
39 "paddw " #a ", " #b " \n\t"
12e96681 40#define PMAX(a,b) \
bb270c08
DB
41 "movq " #a ", " #b " \n\t"\
42 "psrlq $32, " #a " \n\t"\
43 PMAXW(b, a)\
44 "movq " #a ", " #b " \n\t"\
45 "psrlq $16, " #a " \n\t"\
46 PMAXW(b, a)
12e96681 47
2f349de2
MN
48#endif
49
50static int RENAME(dct_quantize)(MpegEncContext *s,
51 DCTELEM *block, int n,
d7e9533a 52 int qscale, int *overflow)
2f349de2 53{
053dea12
AJ
54 long last_non_zero_p1;
55 int level=0, q; //=0 is cuz gcc says uninitalized ...
0c1a9eda 56 const uint16_t *qmat, *bias;
68b51e58 57 DECLARE_ALIGNED_8(int16_t, temp_block[64]);
115329f1 58
f8188626 59 assert((7&(int)(&temp_block[0])) == 0); //did gcc align it correctly?
2f349de2 60
28db7fce 61 //s->fdct (block);
cf3bf5bb 62 RENAMEl(ff_fdct) (block); //cant be anything else ...
d7e9533a 63
821cb11f 64 if(s->dct_error_sum)
783df5f3 65 s->denoise_dct(s, block);
821cb11f 66
2f349de2
MN
67 if (s->mb_intra) {
68 int dummy;
69 if (n < 4)
1a684b3d 70 q = s->y_dc_scale;
2f349de2 71 else
1a684b3d 72 q = s->c_dc_scale;
2f349de2 73 /* note: block[0] is assumed to be positive */
ef5b1b5a 74 if (!s->h263_aic) {
2f349de2 75#if 1
d7e9533a 76 asm volatile (
bb270c08
DB
77 "mul %%ecx \n\t"
78 : "=d" (level), "=a"(dummy)
36cd3069 79 : "a" ((block[0]>>2) + q), "c" (ff_inverse[q<<1])
d7e9533a 80 );
2f349de2 81#else
d7e9533a 82 asm volatile (
bb270c08
DB
83 "xorl %%edx, %%edx \n\t"
84 "divw %%cx \n\t"
85 "movzwl %%ax, %%eax \n\t"
86 : "=a" (level)
87 : "a" ((block[0]>>2) + q), "c" (q<<1)
88 : "%edx"
d7e9533a 89 );
2f349de2 90#endif
ef5b1b5a
J
91 } else
92 /* For AIC we skip quant/dequant of INTRADC */
1a684b3d 93 level = (block[0] + 4)>>3;
115329f1 94
d7e9533a 95 block[0]=0; //avoid fake overflow
2f349de2 96// temp_block[0] = (block[0] + (q >> 1)) / q;
2f349de2 97 last_non_zero_p1 = 1;
642ccefb
MN
98 bias = s->q_intra_matrix16[qscale][1];
99 qmat = s->q_intra_matrix16[qscale][0];
2f349de2 100 } else {
2f349de2 101 last_non_zero_p1 = 0;
642ccefb
MN
102 bias = s->q_inter_matrix16[qscale][1];
103 qmat = s->q_inter_matrix16[qscale][0];
2f349de2
MN
104 }
105
1c3990db 106 if((s->out_format == FMT_H263 || s->out_format == FMT_H261) && s->mpeg_quant==0){
115329f1 107
d7e9533a 108 asm volatile(
bb270c08 109 "movd %%"REG_a", %%mm3 \n\t" // last_non_zero_p1
d7e9533a 110 SPREADW(%%mm3)
bb270c08
DB
111 "pxor %%mm7, %%mm7 \n\t" // 0
112 "pxor %%mm4, %%mm4 \n\t" // 0
113 "movq (%2), %%mm5 \n\t" // qmat[0]
114 "pxor %%mm6, %%mm6 \n\t"
115 "psubw (%3), %%mm6 \n\t" // -bias[0]
116 "mov $-128, %%"REG_a" \n\t"
4454dc1b 117 ASMALIGN(4)
bb270c08
DB
118 "1: \n\t"
119 "pxor %%mm1, %%mm1 \n\t" // 0
120 "movq (%1, %%"REG_a"), %%mm0 \n\t" // block[i]
121 "pcmpgtw %%mm0, %%mm1 \n\t" // block[i] <= 0 ? 0xFF : 0x00
122 "pxor %%mm1, %%mm0 \n\t"
123 "psubw %%mm1, %%mm0 \n\t" // ABS(block[i])
124 "psubusw %%mm6, %%mm0 \n\t" // ABS(block[i]) + bias[0]
125 "pmulhw %%mm5, %%mm0 \n\t" // (ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16
126 "por %%mm0, %%mm4 \n\t"
127 "pxor %%mm1, %%mm0 \n\t"
128 "psubw %%mm1, %%mm0 \n\t" // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
129 "movq %%mm0, (%5, %%"REG_a") \n\t"
130 "pcmpeqw %%mm7, %%mm0 \n\t" // out==0 ? 0xFF : 0x00
131 "movq (%4, %%"REG_a"), %%mm1 \n\t"
132 "movq %%mm7, (%1, %%"REG_a") \n\t" // 0
133 "pandn %%mm1, %%mm0 \n\t"
134 PMAXW(%%mm0, %%mm3)
135 "add $8, %%"REG_a" \n\t"
136 " js 1b \n\t"
137 PMAX(%%mm3, %%mm0)
138 "movd %%mm3, %%"REG_a" \n\t"
139 "movzb %%al, %%"REG_a" \n\t" // last_non_zero_p1
140 : "+a" (last_non_zero_p1)
d04fdff1 141 : "r" (block+64), "r" (qmat), "r" (bias),
486497e0 142 "r" (inv_zigzag_direct16+64), "r" (temp_block+64)
d04fdff1
ZK
143 );
144 // note the asm is split cuz gcc doesnt like that many operands ...
145 asm volatile(
bb270c08
DB
146 "movd %1, %%mm1 \n\t" // max_qcoeff
147 SPREADW(%%mm1)
148 "psubusw %%mm1, %%mm4 \n\t"
149 "packuswb %%mm4, %%mm4 \n\t"
150 "movd %%mm4, %0 \n\t" // *overflow
d04fdff1
ZK
151 : "=g" (*overflow)
152 : "g" (s->max_qcoeff)
153 );
d7e9533a
MN
154 }else{ // FMT_H263
155 asm volatile(
bb270c08 156 "movd %%"REG_a", %%mm3 \n\t" // last_non_zero_p1
d7e9533a 157 SPREADW(%%mm3)
bb270c08
DB
158 "pxor %%mm7, %%mm7 \n\t" // 0
159 "pxor %%mm4, %%mm4 \n\t" // 0
160 "mov $-128, %%"REG_a" \n\t"
4454dc1b 161 ASMALIGN(4)
bb270c08
DB
162 "1: \n\t"
163 "pxor %%mm1, %%mm1 \n\t" // 0
164 "movq (%1, %%"REG_a"), %%mm0 \n\t" // block[i]
165 "pcmpgtw %%mm0, %%mm1 \n\t" // block[i] <= 0 ? 0xFF : 0x00
166 "pxor %%mm1, %%mm0 \n\t"
167 "psubw %%mm1, %%mm0 \n\t" // ABS(block[i])
168 "movq (%3, %%"REG_a"), %%mm6 \n\t" // bias[0]
169 "paddusw %%mm6, %%mm0 \n\t" // ABS(block[i]) + bias[0]
170 "movq (%2, %%"REG_a"), %%mm5 \n\t" // qmat[i]
171 "pmulhw %%mm5, %%mm0 \n\t" // (ABS(block[i])*qmat[0] + bias[0]*qmat[0])>>16
172 "por %%mm0, %%mm4 \n\t"
173 "pxor %%mm1, %%mm0 \n\t"
174 "psubw %%mm1, %%mm0 \n\t" // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
175 "movq %%mm0, (%5, %%"REG_a") \n\t"
176 "pcmpeqw %%mm7, %%mm0 \n\t" // out==0 ? 0xFF : 0x00
177 "movq (%4, %%"REG_a"), %%mm1 \n\t"
178 "movq %%mm7, (%1, %%"REG_a") \n\t" // 0
179 "pandn %%mm1, %%mm0 \n\t"
180 PMAXW(%%mm0, %%mm3)
181 "add $8, %%"REG_a" \n\t"
182 " js 1b \n\t"
183 PMAX(%%mm3, %%mm0)
184 "movd %%mm3, %%"REG_a" \n\t"
185 "movzb %%al, %%"REG_a" \n\t" // last_non_zero_p1
186 : "+a" (last_non_zero_p1)
d04fdff1 187 : "r" (block+64), "r" (qmat+64), "r" (bias+64),
486497e0 188 "r" (inv_zigzag_direct16+64), "r" (temp_block+64)
d7e9533a
MN
189 );
190 // note the asm is split cuz gcc doesnt like that many operands ...
191 asm volatile(
bb270c08
DB
192 "movd %1, %%mm1 \n\t" // max_qcoeff
193 SPREADW(%%mm1)
194 "psubusw %%mm1, %%mm4 \n\t"
195 "packuswb %%mm4, %%mm4 \n\t"
196 "movd %%mm4, %0 \n\t" // *overflow
d04fdff1
ZK
197 : "=g" (*overflow)
198 : "g" (s->max_qcoeff)
d7e9533a
MN
199 );
200 }
201
2ad1516a
MN
202 if(s->mb_intra) block[0]= level;
203 else block[0]= temp_block[0];
204
b0368839 205 if(s->dsp.idct_permutation_type == FF_SIMPLE_IDCT_PERM){
2ad1516a 206 if(last_non_zero_p1 <= 1) goto end;
115329f1
DB
207 block[0x08] = temp_block[0x01]; block[0x10] = temp_block[0x08];
208 block[0x20] = temp_block[0x10];
2ad1516a 209 if(last_non_zero_p1 <= 4) goto end;
115329f1
DB
210 block[0x18] = temp_block[0x09]; block[0x04] = temp_block[0x02];
211 block[0x09] = temp_block[0x03];
2ad1516a 212 if(last_non_zero_p1 <= 7) goto end;
115329f1
DB
213 block[0x14] = temp_block[0x0A]; block[0x28] = temp_block[0x11];
214 block[0x12] = temp_block[0x18]; block[0x02] = temp_block[0x20];
2ad1516a 215 if(last_non_zero_p1 <= 11) goto end;
115329f1
DB
216 block[0x1A] = temp_block[0x19]; block[0x24] = temp_block[0x12];
217 block[0x19] = temp_block[0x0B]; block[0x01] = temp_block[0x04];
218 block[0x0C] = temp_block[0x05];
2ad1516a 219 if(last_non_zero_p1 <= 16) goto end;
115329f1
DB
220 block[0x11] = temp_block[0x0C]; block[0x29] = temp_block[0x13];
221 block[0x16] = temp_block[0x1A]; block[0x0A] = temp_block[0x21];
222 block[0x30] = temp_block[0x28]; block[0x22] = temp_block[0x30];
223 block[0x38] = temp_block[0x29]; block[0x06] = temp_block[0x22];
2ad1516a 224 if(last_non_zero_p1 <= 24) goto end;
115329f1
DB
225 block[0x1B] = temp_block[0x1B]; block[0x21] = temp_block[0x14];
226 block[0x1C] = temp_block[0x0D]; block[0x05] = temp_block[0x06];
227 block[0x0D] = temp_block[0x07]; block[0x15] = temp_block[0x0E];
228 block[0x2C] = temp_block[0x15]; block[0x13] = temp_block[0x1C];
2ad1516a 229 if(last_non_zero_p1 <= 32) goto end;
115329f1
DB
230 block[0x0B] = temp_block[0x23]; block[0x34] = temp_block[0x2A];
231 block[0x2A] = temp_block[0x31]; block[0x32] = temp_block[0x38];
232 block[0x3A] = temp_block[0x39]; block[0x26] = temp_block[0x32];
233 block[0x39] = temp_block[0x2B]; block[0x03] = temp_block[0x24];
2ad1516a 234 if(last_non_zero_p1 <= 40) goto end;
115329f1
DB
235 block[0x1E] = temp_block[0x1D]; block[0x25] = temp_block[0x16];
236 block[0x1D] = temp_block[0x0F]; block[0x2D] = temp_block[0x17];
237 block[0x17] = temp_block[0x1E]; block[0x0E] = temp_block[0x25];
238 block[0x31] = temp_block[0x2C]; block[0x2B] = temp_block[0x33];
2ad1516a 239 if(last_non_zero_p1 <= 48) goto end;
115329f1
DB
240 block[0x36] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B];
241 block[0x23] = temp_block[0x34]; block[0x3C] = temp_block[0x2D];
242 block[0x07] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
243 block[0x0F] = temp_block[0x27]; block[0x35] = temp_block[0x2E];
2ad1516a 244 if(last_non_zero_p1 <= 56) goto end;
115329f1
DB
245 block[0x2E] = temp_block[0x35]; block[0x33] = temp_block[0x3C];
246 block[0x3E] = temp_block[0x3D]; block[0x27] = temp_block[0x36];
247 block[0x3D] = temp_block[0x2F]; block[0x2F] = temp_block[0x37];
2ad1516a 248 block[0x37] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
b0368839 249 }else if(s->dsp.idct_permutation_type == FF_LIBMPEG2_IDCT_PERM){
2ad1516a 250 if(last_non_zero_p1 <= 1) goto end;
115329f1
DB
251 block[0x04] = temp_block[0x01];
252 block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
2ad1516a 253 if(last_non_zero_p1 <= 4) goto end;
115329f1
DB
254 block[0x0C] = temp_block[0x09]; block[0x01] = temp_block[0x02];
255 block[0x05] = temp_block[0x03];
2ad1516a 256 if(last_non_zero_p1 <= 7) goto end;
115329f1
DB
257 block[0x09] = temp_block[0x0A]; block[0x14] = temp_block[0x11];
258 block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
2ad1516a 259 if(last_non_zero_p1 <= 11) goto end;
115329f1
DB
260 block[0x1C] = temp_block[0x19];
261 block[0x11] = temp_block[0x12]; block[0x0D] = temp_block[0x0B];
262 block[0x02] = temp_block[0x04]; block[0x06] = temp_block[0x05];
2ad1516a 263 if(last_non_zero_p1 <= 16) goto end;
115329f1
DB
264 block[0x0A] = temp_block[0x0C]; block[0x15] = temp_block[0x13];
265 block[0x19] = temp_block[0x1A]; block[0x24] = temp_block[0x21];
266 block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30];
267 block[0x2C] = temp_block[0x29]; block[0x21] = temp_block[0x22];
2ad1516a 268 if(last_non_zero_p1 <= 24) goto end;
115329f1
DB
269 block[0x1D] = temp_block[0x1B]; block[0x12] = temp_block[0x14];
270 block[0x0E] = temp_block[0x0D]; block[0x03] = temp_block[0x06];
271 block[0x07] = temp_block[0x07]; block[0x0B] = temp_block[0x0E];
272 block[0x16] = temp_block[0x15]; block[0x1A] = temp_block[0x1C];
2ad1516a 273 if(last_non_zero_p1 <= 32) goto end;
115329f1
DB
274 block[0x25] = temp_block[0x23]; block[0x29] = temp_block[0x2A];
275 block[0x34] = temp_block[0x31]; block[0x38] = temp_block[0x38];
276 block[0x3C] = temp_block[0x39]; block[0x31] = temp_block[0x32];
277 block[0x2D] = temp_block[0x2B]; block[0x22] = temp_block[0x24];
2ad1516a 278 if(last_non_zero_p1 <= 40) goto end;
115329f1
DB
279 block[0x1E] = temp_block[0x1D]; block[0x13] = temp_block[0x16];
280 block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17];
281 block[0x1B] = temp_block[0x1E]; block[0x26] = temp_block[0x25];
282 block[0x2A] = temp_block[0x2C]; block[0x35] = temp_block[0x33];
2ad1516a 283 if(last_non_zero_p1 <= 48) goto end;
115329f1
DB
284 block[0x39] = temp_block[0x3A]; block[0x3D] = temp_block[0x3B];
285 block[0x32] = temp_block[0x34]; block[0x2E] = temp_block[0x2D];
286 block[0x23] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
287 block[0x27] = temp_block[0x27]; block[0x2B] = temp_block[0x2E];
2ad1516a 288 if(last_non_zero_p1 <= 56) goto end;
115329f1
DB
289 block[0x36] = temp_block[0x35]; block[0x3A] = temp_block[0x3C];
290 block[0x3E] = temp_block[0x3D]; block[0x33] = temp_block[0x36];
291 block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37];
2ad1516a
MN
292 block[0x3B] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
293 }else{
294 if(last_non_zero_p1 <= 1) goto end;
115329f1
DB
295 block[0x01] = temp_block[0x01];
296 block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
2ad1516a 297 if(last_non_zero_p1 <= 4) goto end;
115329f1
DB
298 block[0x09] = temp_block[0x09]; block[0x02] = temp_block[0x02];
299 block[0x03] = temp_block[0x03];
2ad1516a 300 if(last_non_zero_p1 <= 7) goto end;
115329f1
DB
301 block[0x0A] = temp_block[0x0A]; block[0x11] = temp_block[0x11];
302 block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
2ad1516a 303 if(last_non_zero_p1 <= 11) goto end;
115329f1
DB
304 block[0x19] = temp_block[0x19];
305 block[0x12] = temp_block[0x12]; block[0x0B] = temp_block[0x0B];
306 block[0x04] = temp_block[0x04]; block[0x05] = temp_block[0x05];
2ad1516a 307 if(last_non_zero_p1 <= 16) goto end;
115329f1
DB
308 block[0x0C] = temp_block[0x0C]; block[0x13] = temp_block[0x13];
309 block[0x1A] = temp_block[0x1A]; block[0x21] = temp_block[0x21];
310 block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30];
311 block[0x29] = temp_block[0x29]; block[0x22] = temp_block[0x22];
2ad1516a 312 if(last_non_zero_p1 <= 24) goto end;
115329f1
DB
313 block[0x1B] = temp_block[0x1B]; block[0x14] = temp_block[0x14];
314 block[0x0D] = temp_block[0x0D]; block[0x06] = temp_block[0x06];
315 block[0x07] = temp_block[0x07]; block[0x0E] = temp_block[0x0E];
316 block[0x15] = temp_block[0x15]; block[0x1C] = temp_block[0x1C];
2ad1516a 317 if(last_non_zero_p1 <= 32) goto end;
115329f1
DB
318 block[0x23] = temp_block[0x23]; block[0x2A] = temp_block[0x2A];
319 block[0x31] = temp_block[0x31]; block[0x38] = temp_block[0x38];
320 block[0x39] = temp_block[0x39]; block[0x32] = temp_block[0x32];
321 block[0x2B] = temp_block[0x2B]; block[0x24] = temp_block[0x24];
2ad1516a 322 if(last_non_zero_p1 <= 40) goto end;
115329f1
DB
323 block[0x1D] = temp_block[0x1D]; block[0x16] = temp_block[0x16];
324 block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17];
325 block[0x1E] = temp_block[0x1E]; block[0x25] = temp_block[0x25];
326 block[0x2C] = temp_block[0x2C]; block[0x33] = temp_block[0x33];
2ad1516a 327 if(last_non_zero_p1 <= 48) goto end;
115329f1
DB
328 block[0x3A] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B];
329 block[0x34] = temp_block[0x34]; block[0x2D] = temp_block[0x2D];
330 block[0x26] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
331 block[0x27] = temp_block[0x27]; block[0x2E] = temp_block[0x2E];
2ad1516a 332 if(last_non_zero_p1 <= 56) goto end;
115329f1
DB
333 block[0x35] = temp_block[0x35]; block[0x3C] = temp_block[0x3C];
334 block[0x3D] = temp_block[0x3D]; block[0x36] = temp_block[0x36];
335 block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37];
2ad1516a
MN
336 block[0x3E] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
337 }
338 end:
2f349de2
MN
339/*
340 for(i=0; i<last_non_zero_p1; i++)
341 {
342 int j= zigzag_direct_noperm[i];
343 block[block_permute_op(j)]= temp_block[j];
344 }
345*/
d7e9533a 346
2f349de2
MN
347 return last_non_zero_p1 - 1;
348}