Factorize in ppccommon.h the conditional inclusion of altivec.h on Linux systems.
[x262.git] / common / ppc / quant.c
1 /*****************************************************************************
2 * quant.c: h264 encoder
3 *****************************************************************************
4 * Copyright (C) 2007 Guillaume Poirier <gpoirier@mplayerhq.hu>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
19 *****************************************************************************/
20
21 #include "common/common.h"
22 #include "ppccommon.h"
23 #include "quant.h"            
24
25 // quant of a whole 4x4 block, unrolled 2x and "pre-scheduled"
26 #define QUANT_16_U( idx0, idx1 )                                             \
27 temp1v = vec_ld((idx0), *dct);                                               \
28 temp2v = vec_ld((idx1), *dct);                                               \
29 mfvA = vec_ld((idx0), mf);                                                   \
30 mfvB = vec_ld((idx1), mf);                                                   \
31 biasvA = vec_ld((idx0), bias);                                               \
32 biasvB = vec_ld((idx1), bias);                                               \
33 mskA = vec_cmplt(temp1v, zerov);                                             \
34 mskB = vec_cmplt(temp2v, zerov);                                             \
35 coefvA = (vec_u16_t)vec_max(vec_sub(zerov, temp1v), temp1v);                 \
36 coefvB = (vec_u16_t)vec_max(vec_sub(zerov, temp2v), temp2v);                 \
37 coefvA = vec_adds(coefvA, biasvA);                                           \
38 coefvB = vec_adds(coefvB, biasvB);                                           \
39 multEvenvA = vec_mule(coefvA, mfvA);                                         \
40 multOddvA = vec_mulo(coefvA, mfvA);                                          \
41 multEvenvB = vec_mule(coefvB, mfvB);                                         \
42 multOddvB = vec_mulo(coefvB, mfvB);                                          \
43 multEvenvA = vec_sr(multEvenvA, i_qbitsv);                                   \
44 multOddvA = vec_sr(multOddvA, i_qbitsv);                                     \
45 multEvenvB = vec_sr(multEvenvB, i_qbitsv);                                   \
46 multOddvB = vec_sr(multOddvB, i_qbitsv);                                     \
47 temp1v = (vec_s16_t) vec_packs(vec_mergeh(multEvenvA, multOddvA), vec_mergel(multEvenvA, multOddvA)); \
48 temp2v = (vec_s16_t) vec_packs(vec_mergeh(multEvenvB, multOddvB), vec_mergel(multEvenvB, multOddvB)); \
49 temp1v = vec_xor(temp1v, mskA);                                              \
50 temp2v = vec_xor(temp2v, mskB);                                              \
51 temp1v = vec_adds(temp1v, vec_and(mskA, one));                               \
52 vec_st(temp1v, (idx0), (int16_t*)dct);                                       \
53 temp2v = vec_adds(temp2v, vec_and(mskB, one));                               \
54 vec_st(temp2v, (idx1), (int16_t*)dct);
55                 
56 void x264_quant_4x4_altivec( int16_t dct[4][4], uint16_t mf[16], uint16_t bias[16] )
57 {
58     vector bool short mskA;
59     vec_u32_t i_qbitsv;
60     vec_u16_t coefvA;
61     vec_u32_t multEvenvA, multOddvA;
62     vec_u16_t mfvA;
63     vec_u16_t biasvA;
64     vec_s16_t zerov, one;
65
66     vector bool short mskB;
67     vec_u16_t coefvB;
68     vec_u32_t multEvenvB, multOddvB;
69     vec_u16_t mfvB;
70     vec_u16_t biasvB;
71
72     vec_s16_t temp1v, temp2v;
73
74     vect_int_u qbits_u;
75     qbits_u.s[0]=16;
76     i_qbitsv = vec_splat(qbits_u.v, 0);
77
78     zerov = vec_splat_s16(0);
79     one = vec_splat_s16(1);
80
81     QUANT_16_U( 0, 16 );
82 }
83
84 // DC quant of a whole 4x4 block, unrolled 2x and "pre-scheduled"
85 #define QUANT_16_U_DC( idx0, idx1 )                             \
86 temp1v = vec_ld((idx0), *dct);                                  \
87 temp2v = vec_ld((idx1), *dct);                                  \
88 mskA = vec_cmplt(temp1v, zerov);                                \
89 mskB = vec_cmplt(temp2v, zerov);                                \
90 coefvA = (vec_u16_t) vec_max(vec_sub(zerov, temp1v), temp1v);   \
91 coefvB = (vec_u16_t) vec_max(vec_sub(zerov, temp2v), temp2v);   \
92 coefvA = vec_add(coefvA, biasv);                                \
93 coefvB = vec_add(coefvB, biasv);                                \
94 multEvenvA = vec_mule(coefvA, mfv);                             \
95 multOddvA = vec_mulo(coefvA, mfv);                              \
96 multEvenvB = vec_mule(coefvB, mfv);                             \
97 multOddvB = vec_mulo(coefvB, mfv);                              \
98 multEvenvA = vec_sr(multEvenvA, i_qbitsv);                      \
99 multOddvA = vec_sr(multOddvA, i_qbitsv);                        \
100 multEvenvB = vec_sr(multEvenvB, i_qbitsv);                      \
101 multOddvB = vec_sr(multOddvB, i_qbitsv);                        \
102 temp1v = (vec_s16_t) vec_packs(vec_mergeh(multEvenvA, multOddvA), vec_mergel(multEvenvA, multOddvA)); \
103 temp2v = (vec_s16_t) vec_packs(vec_mergeh(multEvenvB, multOddvB), vec_mergel(multEvenvB, multOddvB)); \
104 temp1v = vec_xor(temp1v, mskA);                                 \
105 temp2v = vec_xor(temp2v, mskB);                                 \
106 temp1v = vec_add(temp1v, vec_and(mskA, one));                   \
107 vec_st(temp1v, (idx0), (int16_t*)dct);                          \
108 temp2v = vec_add(temp2v, vec_and(mskB, one));                   \
109 vec_st(temp2v, (idx1), (int16_t*)dct);
110
111 void x264_quant_4x4_dc_altivec( int16_t dct[4][4], int mf, int bias )
112 {
113     vector bool short mskA;
114     vec_u32_t i_qbitsv;
115     vec_u16_t coefvA;
116     vec_u32_t multEvenvA, multOddvA;
117     vec_s16_t zerov, one;
118
119     vector bool short mskB;
120     vec_u16_t coefvB;
121     vec_u32_t multEvenvB, multOddvB;
122
123     vec_s16_t temp1v, temp2v;
124
125     vec_u16_t mfv;
126     vec_u16_t biasv;
127
128     vect_ushort_u mf_u;
129     mf_u.s[0]=mf;
130     mfv = vec_splat( mf_u.v, 0 );
131
132     vect_int_u qbits_u;
133     qbits_u.s[0]=16;
134     i_qbitsv = vec_splat(qbits_u.v, 0);
135
136     vect_ushort_u bias_u;
137     bias_u.s[0]=bias;
138     biasv = vec_splat(bias_u.v, 0);
139
140     zerov = vec_splat_s16(0);
141     one = vec_splat_s16(1);
142
143     QUANT_16_U_DC( 0, 16 );
144 }
145
146 // DC quant of a whole 2x2 block
147 #define QUANT_4_U_DC( idx0 )                                    \
148 const vec_u16_t sel = (vec_u16_t) CV(-1,-1,-1,-1,0,0,0,0);      \
149 temp1v = vec_ld((idx0), *dct);                                  \
150 mskA = vec_cmplt(temp1v, zerov);                                \
151 coefvA = (vec_u16_t) vec_max(vec_sub(zerov, temp1v), temp1v);   \
152 coefvA = vec_add(coefvA, biasv);                                \
153 multEvenvA = vec_mule(coefvA, mfv);                             \
154 multOddvA = vec_mulo(coefvA, mfv);                              \
155 multEvenvA = vec_sr(multEvenvA, i_qbitsv);                      \
156 multOddvA = vec_sr(multOddvA, i_qbitsv);                        \
157 temp2v = (vec_s16_t) vec_packs(vec_mergeh(multEvenvA, multOddvA), vec_mergel(multEvenvA, multOddvA)); \
158 temp2v = vec_xor(temp2v, mskA);                                 \
159 temp2v = vec_add(temp2v, vec_and(mskA, one));                   \
160 temp1v = vec_sel(temp1v, temp2v, sel);                          \
161 vec_st(temp1v, (idx0), (int16_t*)dct);
162
163 void x264_quant_2x2_dc_altivec( int16_t dct[2][2], int mf, int bias )
164 {
165     vector bool short mskA;
166     vec_u32_t i_qbitsv;
167     vec_u16_t coefvA;
168     vec_u32_t multEvenvA, multOddvA;
169     vec_s16_t zerov, one;
170
171     vec_s16_t temp1v, temp2v;
172
173     vec_u16_t mfv;
174     vec_u16_t biasv;
175
176     vect_ushort_u mf_u;
177     mf_u.s[0]=mf;
178     mfv = vec_splat( mf_u.v, 0 );
179
180     vect_int_u qbits_u;
181     qbits_u.s[0]=16;
182     i_qbitsv = vec_splat(qbits_u.v, 0);
183
184     vect_ushort_u bias_u;
185     bias_u.s[0]=bias;
186     biasv = vec_splat(bias_u.v, 0);
187
188     zerov = vec_splat_s16(0);
189     one = vec_splat_s16(1);
190
191     QUANT_4_U_DC(0);
192 }
193
194 void x264_quant_8x8_altivec( int16_t dct[8][8], uint16_t mf[64], uint16_t bias[64] )
195 {
196     vector bool short mskA;
197     vec_u32_t i_qbitsv;
198     vec_u16_t coefvA;
199     vec_u32_t multEvenvA, multOddvA;
200     vec_u16_t mfvA;
201     vec_u16_t biasvA;
202     vec_s16_t zerov, one;
203     
204     vector bool short mskB;
205     vec_u16_t coefvB;
206     vec_u32_t multEvenvB, multOddvB;
207     vec_u16_t mfvB;
208     vec_u16_t biasvB;
209     
210     vec_s16_t temp1v, temp2v;
211     
212     vect_int_u qbits_u;
213     qbits_u.s[0]=16;
214     i_qbitsv = vec_splat(qbits_u.v, 0);
215
216     zerov = vec_splat_s16(0);
217     one = vec_splat_s16(1);
218     
219     int i;
220
221     for ( i=0; i<4; i++ ) {
222       QUANT_16_U( i*2*16, i*2*16+16 );
223     }
224 }
225
226 #define DEQUANT_SHL()                                                \
227 {                                                                    \
228     dctv = vec_ld(0, dct[y]);                                        \
229     mf1v = vec_ld(0, dequant_mf[i_mf][y]);                           \
230     mf2v = vec_ld(16, dequant_mf[i_mf][y]);                          \
231     mfv  = vec_packs(mf1v, mf2v);                                    \
232                                                                      \
233     multEvenvA = vec_mule(dctv, mfv);                                \
234     multOddvA = vec_mulo(dctv, mfv);                                 \
235     dctv = (vec_s16_t) vec_packs(vec_mergeh(multEvenvA, multOddvA),  \
236                                  vec_mergel(multEvenvA, multOddvA)); \
237     dctv = vec_sl(dctv, i_qbitsv);                                   \
238     vec_st(dctv, 0, dct[y]);                                         \
239 }
240
241 #define DEQUANT_SHR()                                          \
242 {                                                              \
243     dctv = vec_ld(0, dct[y]);                                  \
244     dct1v = vec_mergeh(dctv, dctv);                            \
245     dct2v = vec_mergel(dctv, dctv);                            \
246     mf1v = vec_ld(0, dequant_mf[i_mf][y]);                     \
247     mf2v = vec_ld(16, dequant_mf[i_mf][y]);                    \
248                                                                \
249     multEvenvA = vec_mule(dct1v, (vec_s16_t)mf1v);             \
250     multOddvA = vec_mulo(dct1v, (vec_s16_t)mf1v);              \
251     temp1v = vec_add(vec_sl(multEvenvA, sixteenv), multOddvA); \
252     temp1v = vec_add(temp1v, fv);                              \
253     temp1v = vec_sra(temp1v, i_qbitsv);                        \
254                                                                \
255     multEvenvA = vec_mule(dct2v, (vec_s16_t)mf2v);             \
256     multOddvA = vec_mulo(dct2v, (vec_s16_t)mf2v);              \
257     temp2v = vec_add(vec_sl(multEvenvA, sixteenv), multOddvA); \
258     temp2v = vec_add(temp2v, fv);                              \
259     temp2v = vec_sra(temp2v, i_qbitsv);                        \
260                                                                \
261     dctv = (vec_s16_t)vec_packs(temp1v, temp2v);               \
262     vec_st(dctv, 0, dct[y]);                                   \
263 }
264
265 void x264_dequant_4x4_altivec( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
266 {
267     const int i_mf = i_qp%6;
268     const int i_qbits = i_qp/6 - 4;
269     int y;
270
271     vec_s16_t dctv;
272     vec_s16_t dct1v, dct2v;
273     vec_s32_t mf1v, mf2v;
274     vec_s16_t mfv;
275     vec_s32_t multEvenvA, multOddvA;
276     vec_s32_t temp1v, temp2v;
277
278     if( i_qbits >= 0 )
279     {
280         vec_u16_t i_qbitsv;
281         vect_ushort_u qbits_u;
282         qbits_u.s[0]=i_qbits;
283         i_qbitsv = vec_splat(qbits_u.v, 0);
284
285         for( y = 0; y < 4; y+=2 )
286             DEQUANT_SHL();
287     }
288     else
289     {
290         const int f = 1 << (-i_qbits-1);
291
292         vec_s32_t fv;
293         vect_int_u f_u;
294         f_u.s[0]=f;
295         fv = (vec_s32_t)vec_splat(f_u.v, 0);
296
297         vec_u32_t i_qbitsv;
298         vect_int_u qbits_u;
299         qbits_u.s[0]=-i_qbits;
300         i_qbitsv = vec_splat(qbits_u.v, 0);
301
302         vec_u32_t sixteenv;
303         vect_int_u sixteen_u;
304         sixteen_u.s[0]=16;
305         sixteenv = vec_splat(sixteen_u.v, 0);
306
307         for( y = 0; y < 4; y+=2 )
308             DEQUANT_SHR();
309     }
310 }
311
312 void x264_dequant_8x8_altivec( int16_t dct[8][8], int dequant_mf[6][8][8], int i_qp )
313 {
314     const int i_mf = i_qp%6;
315     const int i_qbits = i_qp/6 - 6;
316     int y;
317
318     vec_s16_t dctv;
319     vec_s16_t dct1v, dct2v;
320     vec_s32_t mf1v, mf2v;
321     vec_s16_t mfv;
322     vec_s32_t multEvenvA, multOddvA;
323     vec_s32_t temp1v, temp2v;
324
325     if( i_qbits >= 0 )
326     {
327         vec_u16_t i_qbitsv;
328         vect_ushort_u qbits_u;
329         qbits_u.s[0]=i_qbits;
330         i_qbitsv = vec_splat(qbits_u.v, 0);
331
332         for( y = 0; y < 8; y++ )
333             DEQUANT_SHL();
334     }
335     else
336     {
337         const int f = 1 << (-i_qbits-1);
338
339         vec_s32_t fv;
340         vect_int_u f_u;
341         f_u.s[0]=f;
342         fv = (vec_s32_t)vec_splat(f_u.v, 0);
343
344         vec_u32_t i_qbitsv;
345         vect_int_u qbits_u;
346         qbits_u.s[0]=-i_qbits;
347         i_qbitsv = vec_splat(qbits_u.v, 0);
348
349         vec_u32_t sixteenv;
350         vect_int_u sixteen_u;
351         sixteen_u.s[0]=16;
352         sixteenv = vec_splat(sixteen_u.v, 0);
353
354         for( y = 0; y < 8; y++ )
355             DEQUANT_SHR();
356     }
357 }
358