1 /*****************************************************************************
2 * ppccommon.h: h264 encoder
3 *****************************************************************************
4 * Copyright (C) 2003 Eric Petit <eric.petit@lapsus.org>
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.
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.
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 *****************************************************************************/
25 /***********************************************************************
26 * For constant vectors, use parentheses on OS X and braces on Linux
27 **********************************************************************/
28 #if defined(__APPLE__) && __GNUC__ < 4
34 /***********************************************************************
36 **********************************************************************/
37 #define vec_u8_t vector unsigned char
38 #define vec_s8_t vector signed char
39 #define vec_u16_t vector unsigned short
40 #define vec_s16_t vector signed short
41 #define vec_u32_t vector unsigned int
42 #define vec_s32_t vector signed int
46 vector unsigned int v;
51 vector unsigned short v;
56 vector signed short v;
61 vector unsigned char v;
64 /***********************************************************************
66 **********************************************************************/
67 #define LOAD_ZERO const vec_u8_t zerov = vec_splat_u8( 0 )
69 #define zero_u8v (vec_u8_t) zerov
70 #define zero_s8v (vec_s8_t) zerov
71 #define zero_u16v (vec_u16_t) zerov
72 #define zero_s16v (vec_s16_t) zerov
73 #define zero_u32v (vec_u32_t) zerov
74 #define zero_s32v (vec_s32_t) zerov
76 /***********************************************************************
77 * 8 <-> 16 bits conversions
78 **********************************************************************/
79 #define vec_u8_to_u16_h(v) (vec_u16_t) vec_mergeh( zero_u8v, (vec_u8_t) v )
80 #define vec_u8_to_u16_l(v) (vec_u16_t) vec_mergel( zero_u8v, (vec_u8_t) v )
81 #define vec_u8_to_s16_h(v) (vec_s16_t) vec_mergeh( zero_u8v, (vec_u8_t) v )
82 #define vec_u8_to_s16_l(v) (vec_s16_t) vec_mergel( zero_u8v, (vec_u8_t) v )
84 #define vec_u8_to_u16(v) vec_u8_to_u16_h(v)
85 #define vec_u8_to_s16(v) vec_u8_to_s16_h(v)
87 #define vec_u16_to_u8(v) vec_pack( v, zero_u16v )
88 #define vec_s16_to_u8(v) vec_packsu( v, zero_s16v )
90 /***********************************************************************
91 * PREP_LOAD: declares two vectors required to perform unaligned loads
92 * VEC_LOAD: loads n bytes from u8 * p into vector v of type t where o is from original src offset
93 * VEC_LOAD:_G: loads n bytes from u8 * p into vectory v of type t - use when offset is not known
94 * VEC_LOAD_OFFSET: as above, but with offset vector known in advance
95 **********************************************************************/
99 #define PREP_LOAD_SRC( src ) \
100 vec_u8_t _##src##_ = vec_lvsl(0, src)
102 #define VEC_LOAD_G( p, v, n, t ) \
103 _hv = vec_ld( 0, p ); \
104 v = (t) vec_lvsl( 0, p ); \
105 _lv = vec_ld( n - 1, p ); \
106 v = (t) vec_perm( _hv, _lv, (vec_u8_t) v )
108 #define VEC_LOAD( p, v, n, t, g ) \
109 _hv = vec_ld( 0, p ); \
110 _lv = vec_ld( n - 1, p ); \
111 v = (t) vec_perm( _hv, _lv, (vec_u8_t) _##g##_ )
113 #define VEC_LOAD_OFFSET( p, v, n, t, o ) \
114 _hv = vec_ld( 0, p); \
115 _lv = vec_ld( n - 1, p ); \
116 v = (t) vec_perm( _hv, _lv, (vec_u8_t) o )
118 #define VEC_LOAD_PARTIAL( p, v, n, t, g) \
119 _hv = vec_ld( 0, p); \
120 v = (t) vec_perm( _hv, _hv, (vec_u8_t) _##g##_ )
123 /***********************************************************************
124 * PREP_STORE##n: declares required vectors to store n bytes to a
125 * potentially unaligned address
126 * VEC_STORE##n: stores n bytes from vector v to address p
127 **********************************************************************/
128 #define PREP_STORE16 \
131 #define PREP_STORE16_DST( dst ) \
132 vec_u8_t _##dst##l_ = vec_lvsl(0, dst); \
133 vec_u8_t _##dst##r_ = vec_lvsr(0, dst);
135 #define VEC_STORE16( v, p, o ) \
136 _hv = vec_ld( 0, p ); \
137 _lv = vec_ld( 15, p ); \
138 _tmp1v = vec_perm( _lv, _hv, _##o##l_ ); \
139 _lv = vec_perm( (vec_u8_t) v, _tmp1v, _##o##r_ ); \
140 vec_st( _lv, 15, (uint8_t *) p ); \
141 _hv = vec_perm( _tmp1v, (vec_u8_t) v, _##o##r_ ); \
142 vec_st( _hv, 0, (uint8_t *) p )
145 #define PREP_STORE8 \
148 #define VEC_STORE8( v, p ) \
149 _tmp3v = vec_lvsl(0, p); \
150 v = vec_perm(v, v, _tmp3v); \
151 vec_ste((vec_u32_t)v,0,(uint32_t*)p); \
152 vec_ste((vec_u32_t)v,4,(uint32_t*)p)
155 #define PREP_STORE4 \
157 vec_u8_t _tmp2v, _tmp3v; \
158 const vec_u8_t sel = \
159 (vec_u8_t) CV(-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0)
161 #define VEC_STORE4( v, p ) \
162 _tmp3v = vec_lvsr( 0, p ); \
163 v = vec_perm( v, v, _tmp3v ); \
164 _lv = vec_ld( 3, p ); \
165 _tmp1v = vec_perm( sel, zero_u8v, _tmp3v ); \
166 _lv = vec_sel( _lv, v, _tmp1v ); \
167 vec_st( _lv, 3, p ); \
168 _hv = vec_ld( 0, p ); \
169 _tmp2v = vec_perm( zero_u8v, sel, _tmp3v ); \
170 _hv = vec_sel( _hv, v, _tmp2v ); \
173 /***********************************************************************
175 ***********************************************************************
176 * Transposes a 8x8 matrix of s16 vectors
177 **********************************************************************/
178 #define VEC_TRANSPOSE_8(a0,a1,a2,a3,a4,a5,a6,a7,b0,b1,b2,b3,b4,b5,b6,b7) \
179 b0 = vec_mergeh( a0, a4 ); \
180 b1 = vec_mergel( a0, a4 ); \
181 b2 = vec_mergeh( a1, a5 ); \
182 b3 = vec_mergel( a1, a5 ); \
183 b4 = vec_mergeh( a2, a6 ); \
184 b5 = vec_mergel( a2, a6 ); \
185 b6 = vec_mergeh( a3, a7 ); \
186 b7 = vec_mergel( a3, a7 ); \
187 a0 = vec_mergeh( b0, b4 ); \
188 a1 = vec_mergel( b0, b4 ); \
189 a2 = vec_mergeh( b1, b5 ); \
190 a3 = vec_mergel( b1, b5 ); \
191 a4 = vec_mergeh( b2, b6 ); \
192 a5 = vec_mergel( b2, b6 ); \
193 a6 = vec_mergeh( b3, b7 ); \
194 a7 = vec_mergel( b3, b7 ); \
195 b0 = vec_mergeh( a0, a4 ); \
196 b1 = vec_mergel( a0, a4 ); \
197 b2 = vec_mergeh( a1, a5 ); \
198 b3 = vec_mergel( a1, a5 ); \
199 b4 = vec_mergeh( a2, a6 ); \
200 b5 = vec_mergel( a2, a6 ); \
201 b6 = vec_mergeh( a3, a7 ); \
202 b7 = vec_mergel( a3, a7 )
204 /***********************************************************************
206 ***********************************************************************
207 * Transposes a 4x4 matrix of s16 vectors.
208 * Actually source and destination are 8x4. The low elements of the
209 * source are discarded and the low elements of the destination mustn't
211 **********************************************************************/
212 #define VEC_TRANSPOSE_4(a0,a1,a2,a3,b0,b1,b2,b3) \
213 b0 = vec_mergeh( a0, a0 ); \
214 b1 = vec_mergeh( a1, a0 ); \
215 b2 = vec_mergeh( a2, a0 ); \
216 b3 = vec_mergeh( a3, a0 ); \
217 a0 = vec_mergeh( b0, b2 ); \
218 a1 = vec_mergel( b0, b2 ); \
219 a2 = vec_mergeh( b1, b3 ); \
220 a3 = vec_mergel( b1, b3 ); \
221 b0 = vec_mergeh( a0, a2 ); \
222 b1 = vec_mergel( a0, a2 ); \
223 b2 = vec_mergeh( a1, a3 ); \
224 b3 = vec_mergel( a1, a3 )
226 /***********************************************************************
228 ***********************************************************************
233 * Loads n bytes from p1 and p2, do the diff of the high elements into
234 * d, increments p1 and p2 by i1 and i2 into known offset g
235 **********************************************************************/
239 vec_s16_t pix1v, pix2v;
242 #define VEC_DIFF_H(p1,i1,p2,i2,n,d,g) \
243 VEC_LOAD_PARTIAL( p1, pix1v, n, vec_s16_t, p1); \
244 pix1v = vec_u8_to_s16( pix1v ); \
245 VEC_LOAD( p2, pix2v, n, vec_s16_t, g); \
246 pix2v = vec_u8_to_s16( pix2v ); \
247 d = vec_sub( pix1v, pix2v ); \
251 /***********************************************************************
253 ***********************************************************************
258 * Loads 16 bytes from p1 and p2, do the diff of the high elements into
259 * dh, the diff of the low elements into dl, increments p1 and p2 by i1
261 **********************************************************************/
262 #define VEC_DIFF_HL(p1,i1,p2,i2,dh,dl) \
263 pix1v = (vec_s16_t)vec_ld(0, p1); \
264 temp0v = vec_u8_to_s16_h( pix1v ); \
265 temp1v = vec_u8_to_s16_l( pix1v ); \
266 VEC_LOAD( p2, pix2v, 16, vec_s16_t, p2); \
267 temp2v = vec_u8_to_s16_h( pix2v ); \
268 temp3v = vec_u8_to_s16_l( pix2v ); \
269 dh = vec_sub( temp0v, temp2v ); \
270 dl = vec_sub( temp1v, temp3v ); \
274 /***********************************************************************
275 * VEC_DIFF_H_8BYTE_ALIGNED
276 ***********************************************************************
281 * Loads n bytes from p1 and p2, do the diff of the high elements into
282 * d, increments p1 and p2 by i1 and i2
283 * Slightly faster when we know we are loading/diffing 8bytes which
284 * are 8 byte aligned. Reduces need for two loads and two vec_lvsl()'s
285 **********************************************************************/
286 #define PREP_DIFF_8BYTEALIGNED \
288 vec_s16_t pix1v, pix2v; \
289 vec_u8_t pix1v8, pix2v8; \
290 vec_u8_t permPix1, permPix2; \
291 permPix1 = vec_lvsl(0, pix1); \
292 permPix2 = vec_lvsl(0, pix2); \
294 #define VEC_DIFF_H_8BYTE_ALIGNED(p1,i1,p2,i2,n,d) \
295 pix1v8 = vec_perm(vec_ld(0,p1), zero_u8v, permPix1); \
296 pix2v8 = vec_perm(vec_ld(0, p2), zero_u8v, permPix2); \
297 pix1v = vec_u8_to_s16( pix1v8 ); \
298 pix2v = vec_u8_to_s16( pix2v8 ); \
299 d = vec_sub( pix1v, pix2v); \