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