dnxhdenc: x86: more sensible names for optimization file and init function
[ffmpeg.git] / libavcodec / x86 / mpegvideo_mmx_template.c
1 /*
2  * MPEG video MMX templates
3  *
4  * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
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
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #undef MMREG_WIDTH
24 #undef MM
25 #undef MOVQ
26 #undef SPREADW
27 #undef PMAXW
28 #undef PMAX
29 #undef SAVE_SIGN
30 #undef RESTORE_SIGN
31
32 #if HAVE_SSE2
33 #define MMREG_WIDTH "16"
34 #define MM "%%xmm"
35 #define MOVQ "movdqa"
36 #define SPREADW(a) \
37             "pshuflw $0, "a", "a"       \n\t"\
38             "punpcklwd "a", "a"         \n\t"
39 #define PMAXW(a,b) "pmaxsw "a", "b"     \n\t"
40 #define PMAX(a,b) \
41             "movhlps "a", "b"           \n\t"\
42             PMAXW(b, a)\
43             "pshuflw $0x0E, "a", "b"    \n\t"\
44             PMAXW(b, a)\
45             "pshuflw $0x01, "a", "b"    \n\t"\
46             PMAXW(b, a)
47 #else
48 #define MMREG_WIDTH "8"
49 #define MM "%%mm"
50 #define MOVQ "movq"
51 #if HAVE_MMXEXT
52 #define SPREADW(a) "pshufw $0, "a", "a" \n\t"
53 #define PMAXW(a,b) "pmaxsw "a", "b"     \n\t"
54 #define PMAX(a,b) \
55             "pshufw $0x0E, "a", "b"     \n\t"\
56             PMAXW(b, a)\
57             "pshufw $0x01, "a", "b"     \n\t"\
58             PMAXW(b, a)
59 #else
60 #define SPREADW(a) \
61             "punpcklwd "a", "a"         \n\t"\
62             "punpcklwd "a", "a"         \n\t"
63 #define PMAXW(a,b) \
64             "psubusw "a", "b"           \n\t"\
65             "paddw "a", "b"             \n\t"
66 #define PMAX(a,b)  \
67             "movq "a", "b"              \n\t"\
68             "psrlq $32, "a"             \n\t"\
69             PMAXW(b, a)\
70             "movq "a", "b"              \n\t"\
71             "psrlq $16, "a"             \n\t"\
72             PMAXW(b, a)
73
74 #endif
75 #endif
76
77 #if HAVE_SSSE3
78 #define SAVE_SIGN(a,b) \
79             "movdqa "b", "a"            \n\t"\
80             "pabsw  "b", "b"            \n\t"
81 #define RESTORE_SIGN(a,b) \
82             "psignw "a", "b"            \n\t"
83 #else
84 #define SAVE_SIGN(a,b) \
85             "pxor "a", "a"              \n\t"\
86             "pcmpgtw "b", "a"           \n\t" /* block[i] <= 0 ? 0xFF : 0x00 */\
87             "pxor "a", "b"              \n\t"\
88             "psubw "a", "b"             \n\t" /* ABS(block[i]) */
89 #define RESTORE_SIGN(a,b) \
90             "pxor "a", "b"              \n\t"\
91             "psubw "a", "b"             \n\t" // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
92 #endif
93
94 static int RENAME(dct_quantize)(MpegEncContext *s,
95                             DCTELEM *block, int n,
96                             int qscale, int *overflow)
97 {
98     x86_reg last_non_zero_p1;
99     int level=0, q; //=0 is because gcc says uninitialized ...
100     const uint16_t *qmat, *bias;
101     LOCAL_ALIGNED_16(int16_t, temp_block, [64]);
102
103     assert((7&(int)(&temp_block[0])) == 0); //did gcc align it correctly?
104
105     //s->fdct (block);
106     RENAMEl(ff_fdct) (block); //cannot be anything else ...
107
108     if(s->dct_error_sum)
109         s->denoise_dct(s, block);
110
111     if (s->mb_intra) {
112         int dummy;
113         if (n < 4)
114             q = s->y_dc_scale;
115         else
116             q = s->c_dc_scale;
117         /* note: block[0] is assumed to be positive */
118         if (!s->h263_aic) {
119         __asm__ volatile (
120                 "mul %%ecx                \n\t"
121                 : "=d" (level), "=a"(dummy)
122                 : "a" ((block[0]>>2) + q), "c" (ff_inverse[q<<1])
123         );
124         } else
125             /* For AIC we skip quant/dequant of INTRADC */
126             level = (block[0] + 4)>>3;
127
128         block[0]=0; //avoid fake overflow
129 //        temp_block[0] = (block[0] + (q >> 1)) / q;
130         last_non_zero_p1 = 1;
131         bias = s->q_intra_matrix16[qscale][1];
132         qmat = s->q_intra_matrix16[qscale][0];
133     } else {
134         last_non_zero_p1 = 0;
135         bias = s->q_inter_matrix16[qscale][1];
136         qmat = s->q_inter_matrix16[qscale][0];
137     }
138
139     if((s->out_format == FMT_H263 || s->out_format == FMT_H261) && s->mpeg_quant==0){
140
141         __asm__ volatile(
142             "movd %%"REG_a", "MM"3              \n\t" // last_non_zero_p1
143             SPREADW(MM"3")
144             "pxor "MM"7, "MM"7                  \n\t" // 0
145             "pxor "MM"4, "MM"4                  \n\t" // 0
146             MOVQ" (%2), "MM"5                   \n\t" // qmat[0]
147             "pxor "MM"6, "MM"6                  \n\t"
148             "psubw (%3), "MM"6                  \n\t" // -bias[0]
149             "mov $-128, %%"REG_a"               \n\t"
150             ".p2align 4                         \n\t"
151             "1:                                 \n\t"
152             MOVQ" (%1, %%"REG_a"), "MM"0        \n\t" // block[i]
153             SAVE_SIGN(MM"1", MM"0")                   // ABS(block[i])
154             "psubusw "MM"6, "MM"0               \n\t" // ABS(block[i]) + bias[0]
155             "pmulhw "MM"5, "MM"0                \n\t" // (ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16
156             "por "MM"0, "MM"4                   \n\t"
157             RESTORE_SIGN(MM"1", MM"0")                // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
158             MOVQ" "MM"0, (%5, %%"REG_a")        \n\t"
159             "pcmpeqw "MM"7, "MM"0               \n\t" // out==0 ? 0xFF : 0x00
160             MOVQ" (%4, %%"REG_a"), "MM"1        \n\t"
161             MOVQ" "MM"7, (%1, %%"REG_a")        \n\t" // 0
162             "pandn "MM"1, "MM"0                 \n\t"
163             PMAXW(MM"0", MM"3")
164             "add $"MMREG_WIDTH", %%"REG_a"      \n\t"
165             " js 1b                             \n\t"
166             PMAX(MM"3", MM"0")
167             "movd "MM"3, %%"REG_a"              \n\t"
168             "movzb %%al, %%"REG_a"              \n\t" // last_non_zero_p1
169             : "+a" (last_non_zero_p1)
170             : "r" (block+64), "r" (qmat), "r" (bias),
171               "r" (ff_inv_zigzag_direct16+64), "r" (temp_block+64)
172               XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
173                                 "%xmm4", "%xmm5", "%xmm6", "%xmm7")
174         );
175     }else{ // FMT_H263
176         __asm__ volatile(
177             "movd %%"REG_a", "MM"3              \n\t" // last_non_zero_p1
178             SPREADW(MM"3")
179             "pxor "MM"7, "MM"7                  \n\t" // 0
180             "pxor "MM"4, "MM"4                  \n\t" // 0
181             "mov $-128, %%"REG_a"               \n\t"
182             ".p2align 4                         \n\t"
183             "1:                                 \n\t"
184             MOVQ" (%1, %%"REG_a"), "MM"0        \n\t" // block[i]
185             SAVE_SIGN(MM"1", MM"0")                   // ABS(block[i])
186             MOVQ" (%3, %%"REG_a"), "MM"6        \n\t" // bias[0]
187             "paddusw "MM"6, "MM"0               \n\t" // ABS(block[i]) + bias[0]
188             MOVQ" (%2, %%"REG_a"), "MM"5        \n\t" // qmat[i]
189             "pmulhw "MM"5, "MM"0                \n\t" // (ABS(block[i])*qmat[0] + bias[0]*qmat[0])>>16
190             "por "MM"0, "MM"4                   \n\t"
191             RESTORE_SIGN(MM"1", MM"0")                // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
192             MOVQ" "MM"0, (%5, %%"REG_a")        \n\t"
193             "pcmpeqw "MM"7, "MM"0               \n\t" // out==0 ? 0xFF : 0x00
194             MOVQ" (%4, %%"REG_a"), "MM"1        \n\t"
195             MOVQ" "MM"7, (%1, %%"REG_a")        \n\t" // 0
196             "pandn "MM"1, "MM"0                 \n\t"
197             PMAXW(MM"0", MM"3")
198             "add $"MMREG_WIDTH", %%"REG_a"      \n\t"
199             " js 1b                             \n\t"
200             PMAX(MM"3", MM"0")
201             "movd "MM"3, %%"REG_a"              \n\t"
202             "movzb %%al, %%"REG_a"              \n\t" // last_non_zero_p1
203             : "+a" (last_non_zero_p1)
204             : "r" (block+64), "r" (qmat+64), "r" (bias+64),
205               "r" (ff_inv_zigzag_direct16+64), "r" (temp_block+64)
206               XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
207                                 "%xmm4", "%xmm5", "%xmm6", "%xmm7")
208         );
209     }
210     __asm__ volatile(
211         "movd %1, "MM"1                     \n\t" // max_qcoeff
212         SPREADW(MM"1")
213         "psubusw "MM"1, "MM"4               \n\t"
214         "packuswb "MM"4, "MM"4              \n\t"
215 #if HAVE_SSE2
216         "packuswb "MM"4, "MM"4              \n\t"
217 #endif
218         "movd "MM"4, %0                     \n\t" // *overflow
219         : "=g" (*overflow)
220         : "g" (s->max_qcoeff)
221     );
222
223     if(s->mb_intra) block[0]= level;
224     else            block[0]= temp_block[0];
225
226     if(s->dsp.idct_permutation_type == FF_SIMPLE_IDCT_PERM){
227         if(last_non_zero_p1 <= 1) goto end;
228         block[0x08] = temp_block[0x01]; block[0x10] = temp_block[0x08];
229         block[0x20] = temp_block[0x10];
230         if(last_non_zero_p1 <= 4) goto end;
231         block[0x18] = temp_block[0x09]; block[0x04] = temp_block[0x02];
232         block[0x09] = temp_block[0x03];
233         if(last_non_zero_p1 <= 7) goto end;
234         block[0x14] = temp_block[0x0A]; block[0x28] = temp_block[0x11];
235         block[0x12] = temp_block[0x18]; block[0x02] = temp_block[0x20];
236         if(last_non_zero_p1 <= 11) goto end;
237         block[0x1A] = temp_block[0x19]; block[0x24] = temp_block[0x12];
238         block[0x19] = temp_block[0x0B]; block[0x01] = temp_block[0x04];
239         block[0x0C] = temp_block[0x05];
240         if(last_non_zero_p1 <= 16) goto end;
241         block[0x11] = temp_block[0x0C]; block[0x29] = temp_block[0x13];
242         block[0x16] = temp_block[0x1A]; block[0x0A] = temp_block[0x21];
243         block[0x30] = temp_block[0x28]; block[0x22] = temp_block[0x30];
244         block[0x38] = temp_block[0x29]; block[0x06] = temp_block[0x22];
245         if(last_non_zero_p1 <= 24) goto end;
246         block[0x1B] = temp_block[0x1B]; block[0x21] = temp_block[0x14];
247         block[0x1C] = temp_block[0x0D]; block[0x05] = temp_block[0x06];
248         block[0x0D] = temp_block[0x07]; block[0x15] = temp_block[0x0E];
249         block[0x2C] = temp_block[0x15]; block[0x13] = temp_block[0x1C];
250         if(last_non_zero_p1 <= 32) goto end;
251         block[0x0B] = temp_block[0x23]; block[0x34] = temp_block[0x2A];
252         block[0x2A] = temp_block[0x31]; block[0x32] = temp_block[0x38];
253         block[0x3A] = temp_block[0x39]; block[0x26] = temp_block[0x32];
254         block[0x39] = temp_block[0x2B]; block[0x03] = temp_block[0x24];
255         if(last_non_zero_p1 <= 40) goto end;
256         block[0x1E] = temp_block[0x1D]; block[0x25] = temp_block[0x16];
257         block[0x1D] = temp_block[0x0F]; block[0x2D] = temp_block[0x17];
258         block[0x17] = temp_block[0x1E]; block[0x0E] = temp_block[0x25];
259         block[0x31] = temp_block[0x2C]; block[0x2B] = temp_block[0x33];
260         if(last_non_zero_p1 <= 48) goto end;
261         block[0x36] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B];
262         block[0x23] = temp_block[0x34]; block[0x3C] = temp_block[0x2D];
263         block[0x07] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
264         block[0x0F] = temp_block[0x27]; block[0x35] = temp_block[0x2E];
265         if(last_non_zero_p1 <= 56) goto end;
266         block[0x2E] = temp_block[0x35]; block[0x33] = temp_block[0x3C];
267         block[0x3E] = temp_block[0x3D]; block[0x27] = temp_block[0x36];
268         block[0x3D] = temp_block[0x2F]; block[0x2F] = temp_block[0x37];
269         block[0x37] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
270     }else if(s->dsp.idct_permutation_type == FF_LIBMPEG2_IDCT_PERM){
271         if(last_non_zero_p1 <= 1) goto end;
272         block[0x04] = temp_block[0x01];
273         block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
274         if(last_non_zero_p1 <= 4) goto end;
275         block[0x0C] = temp_block[0x09]; block[0x01] = temp_block[0x02];
276         block[0x05] = temp_block[0x03];
277         if(last_non_zero_p1 <= 7) goto end;
278         block[0x09] = temp_block[0x0A]; block[0x14] = temp_block[0x11];
279         block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
280         if(last_non_zero_p1 <= 11) goto end;
281         block[0x1C] = temp_block[0x19];
282         block[0x11] = temp_block[0x12]; block[0x0D] = temp_block[0x0B];
283         block[0x02] = temp_block[0x04]; block[0x06] = temp_block[0x05];
284         if(last_non_zero_p1 <= 16) goto end;
285         block[0x0A] = temp_block[0x0C]; block[0x15] = temp_block[0x13];
286         block[0x19] = temp_block[0x1A]; block[0x24] = temp_block[0x21];
287         block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30];
288         block[0x2C] = temp_block[0x29]; block[0x21] = temp_block[0x22];
289         if(last_non_zero_p1 <= 24) goto end;
290         block[0x1D] = temp_block[0x1B]; block[0x12] = temp_block[0x14];
291         block[0x0E] = temp_block[0x0D]; block[0x03] = temp_block[0x06];
292         block[0x07] = temp_block[0x07]; block[0x0B] = temp_block[0x0E];
293         block[0x16] = temp_block[0x15]; block[0x1A] = temp_block[0x1C];
294         if(last_non_zero_p1 <= 32) goto end;
295         block[0x25] = temp_block[0x23]; block[0x29] = temp_block[0x2A];
296         block[0x34] = temp_block[0x31]; block[0x38] = temp_block[0x38];
297         block[0x3C] = temp_block[0x39]; block[0x31] = temp_block[0x32];
298         block[0x2D] = temp_block[0x2B]; block[0x22] = temp_block[0x24];
299         if(last_non_zero_p1 <= 40) goto end;
300         block[0x1E] = temp_block[0x1D]; block[0x13] = temp_block[0x16];
301         block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17];
302         block[0x1B] = temp_block[0x1E]; block[0x26] = temp_block[0x25];
303         block[0x2A] = temp_block[0x2C]; block[0x35] = temp_block[0x33];
304         if(last_non_zero_p1 <= 48) goto end;
305         block[0x39] = temp_block[0x3A]; block[0x3D] = temp_block[0x3B];
306         block[0x32] = temp_block[0x34]; block[0x2E] = temp_block[0x2D];
307             block[0x23] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
308         block[0x27] = temp_block[0x27]; block[0x2B] = temp_block[0x2E];
309         if(last_non_zero_p1 <= 56) goto end;
310         block[0x36] = temp_block[0x35]; block[0x3A] = temp_block[0x3C];
311         block[0x3E] = temp_block[0x3D]; block[0x33] = temp_block[0x36];
312         block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37];
313         block[0x3B] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
314     }else{
315         if(last_non_zero_p1 <= 1) goto end;
316         block[0x01] = temp_block[0x01];
317         block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
318         if(last_non_zero_p1 <= 4) goto end;
319         block[0x09] = temp_block[0x09]; block[0x02] = temp_block[0x02];
320         block[0x03] = temp_block[0x03];
321         if(last_non_zero_p1 <= 7) goto end;
322         block[0x0A] = temp_block[0x0A]; block[0x11] = temp_block[0x11];
323         block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
324         if(last_non_zero_p1 <= 11) goto end;
325         block[0x19] = temp_block[0x19];
326         block[0x12] = temp_block[0x12]; block[0x0B] = temp_block[0x0B];
327         block[0x04] = temp_block[0x04]; block[0x05] = temp_block[0x05];
328         if(last_non_zero_p1 <= 16) goto end;
329         block[0x0C] = temp_block[0x0C]; block[0x13] = temp_block[0x13];
330         block[0x1A] = temp_block[0x1A]; block[0x21] = temp_block[0x21];
331         block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30];
332         block[0x29] = temp_block[0x29]; block[0x22] = temp_block[0x22];
333         if(last_non_zero_p1 <= 24) goto end;
334         block[0x1B] = temp_block[0x1B]; block[0x14] = temp_block[0x14];
335         block[0x0D] = temp_block[0x0D]; block[0x06] = temp_block[0x06];
336         block[0x07] = temp_block[0x07]; block[0x0E] = temp_block[0x0E];
337         block[0x15] = temp_block[0x15]; block[0x1C] = temp_block[0x1C];
338         if(last_non_zero_p1 <= 32) goto end;
339         block[0x23] = temp_block[0x23]; block[0x2A] = temp_block[0x2A];
340         block[0x31] = temp_block[0x31]; block[0x38] = temp_block[0x38];
341         block[0x39] = temp_block[0x39]; block[0x32] = temp_block[0x32];
342         block[0x2B] = temp_block[0x2B]; block[0x24] = temp_block[0x24];
343         if(last_non_zero_p1 <= 40) goto end;
344         block[0x1D] = temp_block[0x1D]; block[0x16] = temp_block[0x16];
345         block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17];
346         block[0x1E] = temp_block[0x1E]; block[0x25] = temp_block[0x25];
347         block[0x2C] = temp_block[0x2C]; block[0x33] = temp_block[0x33];
348         if(last_non_zero_p1 <= 48) goto end;
349         block[0x3A] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B];
350         block[0x34] = temp_block[0x34]; block[0x2D] = temp_block[0x2D];
351         block[0x26] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
352         block[0x27] = temp_block[0x27]; block[0x2E] = temp_block[0x2E];
353         if(last_non_zero_p1 <= 56) goto end;
354         block[0x35] = temp_block[0x35]; block[0x3C] = temp_block[0x3C];
355         block[0x3D] = temp_block[0x3D]; block[0x36] = temp_block[0x36];
356         block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37];
357         block[0x3E] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
358     }
359     end:
360     return last_non_zero_p1 - 1;
361 }