1 /*****************************************************************************
2 * mc.c: ppc motion compensation
3 *****************************************************************************
4 * Copyright (C) 2003-2014 x264 project
6 * Authors: Eric Petit <eric.petit@lapsus.org>
7 * Guillaume Poirier <gpoirier@mplayerhq.hu>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
23 * This program is also available under a commercial proprietary license.
24 * For more information, contact us at licensing@x264.com.
25 *****************************************************************************/
34 #include "common/common.h"
35 #include "common/mc.h"
37 #include "ppccommon.h"
40 typedef void (*pf_mc_t)( uint8_t *src, intptr_t i_src,
41 uint8_t *dst, intptr_t i_dst, int i_height );
43 static inline int x264_tapfilter( uint8_t *pix, int i_pix_next )
45 return pix[-2*i_pix_next] - 5*pix[-1*i_pix_next] + 20*(pix[0] +
46 pix[1*i_pix_next]) - 5*pix[ 2*i_pix_next] +
50 static inline int x264_tapfilter1( uint8_t *pix )
52 return pix[-2] - 5*pix[-1] + 20*(pix[0] + pix[1]) - 5*pix[ 2] +
56 static inline void x264_pixel_avg2_w4_altivec( uint8_t *dst, intptr_t i_dst,
57 uint8_t *src1, intptr_t i_src1,
58 uint8_t *src2, int i_height )
60 for( int y = 0; y < i_height; y++ )
62 for( int x = 0; x < 4; x++ )
63 dst[x] = ( src1[x] + src2[x] + 1 ) >> 1;
70 static inline void x264_pixel_avg2_w8_altivec( uint8_t *dst, intptr_t i_dst,
71 uint8_t *src1, intptr_t i_src1,
72 uint8_t *src2, int i_height )
74 vec_u8_t src1v, src2v;
77 PREP_LOAD_SRC( src1 );
78 PREP_LOAD_SRC( src2 );
80 for( int y = 0; y < i_height; y++ )
82 VEC_LOAD( src1, src1v, 8, vec_u8_t, src1 );
83 VEC_LOAD( src2, src2v, 8, vec_u8_t, src2 );
84 src1v = vec_avg( src1v, src2v );
85 VEC_STORE8( src1v, dst );
93 static inline void x264_pixel_avg2_w16_altivec( uint8_t *dst, intptr_t i_dst,
94 uint8_t *src1, intptr_t i_src1,
95 uint8_t *src2, int i_height )
97 vec_u8_t src1v, src2v;
99 PREP_LOAD_SRC( src1 );
100 PREP_LOAD_SRC( src2 );
102 for( int y = 0; y < i_height; y++ )
104 VEC_LOAD( src1, src1v, 16, vec_u8_t, src1 );
105 VEC_LOAD( src2, src2v, 16, vec_u8_t, src2 );
106 src1v = vec_avg( src1v, src2v );
107 vec_st(src1v, 0, dst);
115 static inline void x264_pixel_avg2_w20_altivec( uint8_t *dst, intptr_t i_dst,
116 uint8_t *src1, intptr_t i_src1,
117 uint8_t *src2, int i_height )
119 x264_pixel_avg2_w16_altivec(dst, i_dst, src1, i_src1, src2, i_height);
120 x264_pixel_avg2_w4_altivec(dst+16, i_dst, src1+16, i_src1, src2+16, i_height);
123 /* mc_copy: plain c */
125 #define MC_COPY( name, a ) \
126 static void name( uint8_t *dst, intptr_t i_dst, \
127 uint8_t *src, intptr_t i_src, int i_height ) \
130 for( y = 0; y < i_height; y++ ) \
132 memcpy( dst, src, a ); \
137 MC_COPY( x264_mc_copy_w4_altivec, 4 )
138 MC_COPY( x264_mc_copy_w8_altivec, 8 )
140 static void x264_mc_copy_w16_altivec( uint8_t *dst, intptr_t i_dst,
141 uint8_t *src, intptr_t i_src, int i_height )
145 PREP_LOAD_SRC( src );
147 for( int y = 0; y < i_height; y++ )
149 VEC_LOAD( src, cpyV, 16, vec_u8_t, src );
150 vec_st(cpyV, 0, dst);
158 static void x264_mc_copy_w16_aligned_altivec( uint8_t *dst, intptr_t i_dst,
159 uint8_t *src, intptr_t i_src, int i_height )
161 for( int y = 0; y < i_height; ++y )
163 vec_u8_t cpyV = vec_ld( 0, src );
164 vec_st(cpyV, 0, dst);
172 static void mc_luma_altivec( uint8_t *dst, intptr_t i_dst_stride,
173 uint8_t *src[4], intptr_t i_src_stride,
175 int i_width, int i_height, const x264_weight_t *weight )
177 int qpel_idx = ((mvy&3)<<2) + (mvx&3);
178 intptr_t offset = (mvy>>2)*i_src_stride + (mvx>>2);
179 uint8_t *src1 = src[x264_hpel_ref0[qpel_idx]] + offset + ((mvy&3) == 3) * i_src_stride;
180 if( qpel_idx & 5 ) /* qpel interpolation needed */
182 uint8_t *src2 = src[x264_hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);
187 x264_pixel_avg2_w4_altivec( dst, i_dst_stride, src1, i_src_stride, src2, i_height );
190 x264_pixel_avg2_w8_altivec( dst, i_dst_stride, src1, i_src_stride, src2, i_height );
194 x264_pixel_avg2_w16_altivec( dst, i_dst_stride, src1, i_src_stride, src2, i_height );
196 if( weight->weightfn )
197 weight->weightfn[i_width>>2]( dst, i_dst_stride, dst, i_dst_stride, weight, i_height );
199 else if( weight->weightfn )
200 weight->weightfn[i_width>>2]( dst, i_dst_stride, src1, i_src_stride, weight, i_height );
206 x264_mc_copy_w4_altivec( dst, i_dst_stride, src1, i_src_stride, i_height );
209 x264_mc_copy_w8_altivec( dst, i_dst_stride, src1, i_src_stride, i_height );
212 x264_mc_copy_w16_altivec( dst, i_dst_stride, src1, i_src_stride, i_height );
220 static uint8_t *get_ref_altivec( uint8_t *dst, intptr_t *i_dst_stride,
221 uint8_t *src[4], intptr_t i_src_stride,
223 int i_width, int i_height, const x264_weight_t *weight )
225 int qpel_idx = ((mvy&3)<<2) + (mvx&3);
226 intptr_t offset = (mvy>>2)*i_src_stride + (mvx>>2);
227 uint8_t *src1 = src[x264_hpel_ref0[qpel_idx]] + offset + ((mvy&3) == 3) * i_src_stride;
228 if( qpel_idx & 5 ) /* qpel interpolation needed */
230 uint8_t *src2 = src[x264_hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);
234 x264_pixel_avg2_w4_altivec( dst, *i_dst_stride, src1, i_src_stride, src2, i_height );
237 x264_pixel_avg2_w8_altivec( dst, *i_dst_stride, src1, i_src_stride, src2, i_height );
242 x264_pixel_avg2_w16_altivec( dst, *i_dst_stride, src1, i_src_stride, src2, i_height );
245 x264_pixel_avg2_w20_altivec( dst, *i_dst_stride, src1, i_src_stride, src2, i_height );
248 if( weight->weightfn )
249 weight->weightfn[i_width>>2]( dst, *i_dst_stride, dst, *i_dst_stride, weight, i_height );
252 else if( weight->weightfn )
254 weight->weightfn[i_width>>2]( dst, *i_dst_stride, src1, i_src_stride, weight, i_height );
259 *i_dst_stride = i_src_stride;
264 static void mc_chroma_2xh( uint8_t *dstu, uint8_t *dstv, intptr_t i_dst_stride,
265 uint8_t *src, intptr_t i_src_stride,
266 int mvx, int mvy, int i_height )
272 int cA = (8-d8x)*(8-d8y);
273 int cB = d8x *(8-d8y);
274 int cC = (8-d8x)*d8y;
277 src += (mvy >> 3) * i_src_stride + (mvx >> 3)*2;
278 srcp = &src[i_src_stride];
280 for( int y = 0; y < i_height; y++ )
282 dstu[0] = ( cA*src[0] + cB*src[2] + cC*srcp[0] + cD*srcp[2] + 32 ) >> 6;
283 dstv[0] = ( cA*src[1] + cB*src[3] + cC*srcp[1] + cD*srcp[3] + 32 ) >> 6;
284 dstu[1] = ( cA*src[2] + cB*src[4] + cC*srcp[2] + cD*srcp[4] + 32 ) >> 6;
285 dstv[1] = ( cA*src[3] + cB*src[5] + cC*srcp[3] + cD*srcp[5] + 32 ) >> 6;
288 srcp += i_src_stride;
289 dstu += i_dst_stride;
290 dstv += i_dst_stride;
294 static void mc_chroma_altivec_4xh( uint8_t *dstu, uint8_t *dstv, intptr_t i_dst_stride,
295 uint8_t *src, intptr_t i_src_stride,
296 int mvx, int mvy, int i_height )
299 int d8x = mvx & 0x07;
300 int d8y = mvy & 0x07;
302 ALIGNED_16( uint16_t coeff[4] );
303 coeff[0] = (8-d8x)*(8-d8y);
304 coeff[1] = d8x *(8-d8y);
305 coeff[2] = (8-d8x)*d8y;
308 src += (mvy >> 3) * i_src_stride + (mvx >> 3)*2;
309 srcp = &src[i_src_stride];
313 PREP_LOAD_SRC( src );
314 vec_u16_t coeff0v, coeff1v, coeff2v, coeff3v;
315 vec_u8_t src2v_8, dstuv, dstvv;
316 vec_u16_t src0v_16, src1v_16, src2v_16, src3v_16, dstv16;
317 vec_u16_t shiftv, k32v;
319 static const vec_u8_t perm0v = CV(1,5,9,13,1,5,9,13,1,5,9,13,1,5,9,13);
320 static const vec_u8_t perm1v = CV(3,7,11,15,3,7,11,15,3,7,11,15,3,7,11,15);
322 coeff0v = vec_ld( 0, coeff );
323 coeff3v = vec_splat( coeff0v, 3 );
324 coeff2v = vec_splat( coeff0v, 2 );
325 coeff1v = vec_splat( coeff0v, 1 );
326 coeff0v = vec_splat( coeff0v, 0 );
327 k32v = vec_sl( vec_splat_u16( 1 ), vec_splat_u16( 5 ) );
328 shiftv = vec_splat_u16( 6 );
330 VEC_LOAD( src, src2v_8, 9, vec_u8_t, src );
331 src2v_16 = vec_u8_to_u16( src2v_8 );
332 src3v_16 = vec_u8_to_u16( vec_sld( src2v_8, src2v_8, 2 ) );
334 for( int y = 0; y < i_height; y += 2 )
338 VEC_LOAD( srcp, src2v_8, 9, vec_u8_t, src );
339 src2v_16 = vec_u8_to_u16( src2v_8 );
340 src3v_16 = vec_u8_to_u16( vec_sld( src2v_8, src2v_8, 2 ) );
342 dstv16 = vec_mladd( coeff0v, src0v_16, k32v );
343 dstv16 = vec_mladd( coeff1v, src1v_16, dstv16 );
344 dstv16 = vec_mladd( coeff2v, src2v_16, dstv16 );
345 dstv16 = vec_mladd( coeff3v, src3v_16, dstv16 );
347 dstv16 = vec_sr( dstv16, shiftv );
349 dstuv = (vec_u8_t)vec_perm( dstv16, dstv16, perm0v );
350 dstvv = (vec_u8_t)vec_perm( dstv16, dstv16, perm1v );
351 vec_ste( (vec_u32_t)dstuv, 0, (uint32_t*) dstu );
352 vec_ste( (vec_u32_t)dstvv, 0, (uint32_t*) dstv );
354 srcp += i_src_stride;
355 dstu += i_dst_stride;
356 dstv += i_dst_stride;
360 VEC_LOAD( srcp, src2v_8, 9, vec_u8_t, src );
361 src2v_16 = vec_u8_to_u16( src2v_8 );
362 src3v_16 = vec_u8_to_u16( vec_sld( src2v_8, src2v_8, 2 ) );
364 dstv16 = vec_mladd( coeff0v, src0v_16, k32v );
365 dstv16 = vec_mladd( coeff1v, src1v_16, dstv16 );
366 dstv16 = vec_mladd( coeff2v, src2v_16, dstv16 );
367 dstv16 = vec_mladd( coeff3v, src3v_16, dstv16 );
369 dstv16 = vec_sr( dstv16, shiftv );
371 dstuv = (vec_u8_t)vec_perm( dstv16, dstv16, perm0v );
372 dstvv = (vec_u8_t)vec_perm( dstv16, dstv16, perm1v );
373 vec_ste( (vec_u32_t)dstuv, 0, (uint32_t*) dstu );
374 vec_ste( (vec_u32_t)dstvv, 0, (uint32_t*) dstv );
376 srcp += i_src_stride;
377 dstu += i_dst_stride;
378 dstv += i_dst_stride;
382 static void mc_chroma_altivec_8xh( uint8_t *dstu, uint8_t *dstv, intptr_t i_dst_stride,
383 uint8_t *src, intptr_t i_src_stride,
384 int mvx, int mvy, int i_height )
387 int d8x = mvx & 0x07;
388 int d8y = mvy & 0x07;
390 ALIGNED_16( uint16_t coeff[4] );
391 coeff[0] = (8-d8x)*(8-d8y);
392 coeff[1] = d8x *(8-d8y);
393 coeff[2] = (8-d8x)*d8y;
396 src += (mvy >> 3) * i_src_stride + (mvx >> 3)*2;
397 srcp = &src[i_src_stride];
401 PREP_LOAD_SRC( src );
403 vec_u16_t coeff0v, coeff1v, coeff2v, coeff3v;
404 vec_u8_t src0v_8, src1v_8, src2v_8, src3v_8;
405 vec_u8_t dstuv, dstvv;
406 vec_u16_t src0v_16h, src1v_16h, src2v_16h, src3v_16h, dstv_16h;
407 vec_u16_t src0v_16l, src1v_16l, src2v_16l, src3v_16l, dstv_16l;
408 vec_u16_t shiftv, k32v;
410 coeff0v = vec_ld( 0, coeff );
411 coeff3v = vec_splat( coeff0v, 3 );
412 coeff2v = vec_splat( coeff0v, 2 );
413 coeff1v = vec_splat( coeff0v, 1 );
414 coeff0v = vec_splat( coeff0v, 0 );
415 k32v = vec_sl( vec_splat_u16( 1 ), vec_splat_u16( 5 ) );
416 shiftv = vec_splat_u16( 6 );
418 static const vec_u8_t perm0v = CV(1,5,9,13,17,21,25,29,0,0,0,0,0,0,0,0);
419 static const vec_u8_t perm1v = CV(3,7,11,15,19,23,27,31,0,0,0,0,0,0,0,0);
421 VEC_LOAD( src, src2v_8, 16, vec_u8_t, src );
422 VEC_LOAD( src+16, src3v_8, 2, vec_u8_t, src );
423 src3v_8 = vec_sld( src2v_8, src3v_8, 2 );
425 for( int y = 0; y < i_height; y += 2 )
429 VEC_LOAD( srcp, src2v_8, 16, vec_u8_t, src );
430 VEC_LOAD( srcp+16, src3v_8, 2, vec_u8_t, src );
432 src3v_8 = vec_sld( src2v_8, src3v_8, 2 );
434 src0v_16h = vec_u8_to_u16_h( src0v_8 );
435 src0v_16l = vec_u8_to_u16_l( src0v_8 );
436 src1v_16h = vec_u8_to_u16_h( src1v_8 );
437 src1v_16l = vec_u8_to_u16_l( src1v_8 );
438 src2v_16h = vec_u8_to_u16_h( src2v_8 );
439 src2v_16l = vec_u8_to_u16_l( src2v_8 );
440 src3v_16h = vec_u8_to_u16_h( src3v_8 );
441 src3v_16l = vec_u8_to_u16_l( src3v_8 );
443 dstv_16h = vec_mladd( coeff0v, src0v_16h, k32v );
444 dstv_16l = vec_mladd( coeff0v, src0v_16l, k32v );
445 dstv_16h = vec_mladd( coeff1v, src1v_16h, dstv_16h );
446 dstv_16l = vec_mladd( coeff1v, src1v_16l, dstv_16l );
447 dstv_16h = vec_mladd( coeff2v, src2v_16h, dstv_16h );
448 dstv_16l = vec_mladd( coeff2v, src2v_16l, dstv_16l );
449 dstv_16h = vec_mladd( coeff3v, src3v_16h, dstv_16h );
450 dstv_16l = vec_mladd( coeff3v, src3v_16l, dstv_16l );
452 dstv_16h = vec_sr( dstv_16h, shiftv );
453 dstv_16l = vec_sr( dstv_16l, shiftv );
455 dstuv = (vec_u8_t)vec_perm( dstv_16h, dstv_16l, perm0v );
456 dstvv = (vec_u8_t)vec_perm( dstv_16h, dstv_16l, perm1v );
458 VEC_STORE8( dstuv, dstu );
459 VEC_STORE8( dstvv, dstv );
461 srcp += i_src_stride;
462 dstu += i_dst_stride;
463 dstv += i_dst_stride;
467 VEC_LOAD( srcp, src2v_8, 16, vec_u8_t, src );
468 VEC_LOAD( srcp+16, src3v_8, 2, vec_u8_t, src );
470 src3v_8 = vec_sld( src2v_8, src3v_8, 2 );
472 src0v_16h = vec_u8_to_u16_h( src0v_8 );
473 src0v_16l = vec_u8_to_u16_l( src0v_8 );
474 src1v_16h = vec_u8_to_u16_h( src1v_8 );
475 src1v_16l = vec_u8_to_u16_l( src1v_8 );
476 src2v_16h = vec_u8_to_u16_h( src2v_8 );
477 src2v_16l = vec_u8_to_u16_l( src2v_8 );
478 src3v_16h = vec_u8_to_u16_h( src3v_8 );
479 src3v_16l = vec_u8_to_u16_l( src3v_8 );
481 dstv_16h = vec_mladd( coeff0v, src0v_16h, k32v );
482 dstv_16l = vec_mladd( coeff0v, src0v_16l, k32v );
483 dstv_16h = vec_mladd( coeff1v, src1v_16h, dstv_16h );
484 dstv_16l = vec_mladd( coeff1v, src1v_16l, dstv_16l );
485 dstv_16h = vec_mladd( coeff2v, src2v_16h, dstv_16h );
486 dstv_16l = vec_mladd( coeff2v, src2v_16l, dstv_16l );
487 dstv_16h = vec_mladd( coeff3v, src3v_16h, dstv_16h );
488 dstv_16l = vec_mladd( coeff3v, src3v_16l, dstv_16l );
490 dstv_16h = vec_sr( dstv_16h, shiftv );
491 dstv_16l = vec_sr( dstv_16l, shiftv );
493 dstuv = (vec_u8_t)vec_perm( dstv_16h, dstv_16l, perm0v );
494 dstvv = (vec_u8_t)vec_perm( dstv_16h, dstv_16l, perm1v );
496 VEC_STORE8( dstuv, dstu );
497 VEC_STORE8( dstvv, dstv );
499 srcp += i_src_stride;
500 dstu += i_dst_stride;
501 dstv += i_dst_stride;
505 static void mc_chroma_altivec( uint8_t *dstu, uint8_t *dstv, intptr_t i_dst_stride,
506 uint8_t *src, intptr_t i_src_stride,
507 int mvx, int mvy, int i_width, int i_height )
510 mc_chroma_altivec_8xh( dstu, dstv, i_dst_stride, src, i_src_stride,
511 mvx, mvy, i_height );
512 else if( i_width == 4 )
513 mc_chroma_altivec_4xh( dstu, dstv, i_dst_stride, src, i_src_stride,
514 mvx, mvy, i_height );
516 mc_chroma_2xh( dstu, dstv, i_dst_stride, src, i_src_stride,
517 mvx, mvy, i_height );
520 #define HPEL_FILTER_1( t1v, t2v, t3v, t4v, t5v, t6v ) \
522 t1v = vec_add( t1v, t6v ); \
523 t2v = vec_add( t2v, t5v ); \
524 t3v = vec_add( t3v, t4v ); \
526 t1v = vec_sub( t1v, t2v ); /* (a-b) */ \
527 t2v = vec_sub( t2v, t3v ); /* (b-c) */ \
528 t2v = vec_sl( t2v, twov ); /* (b-c)*4 */ \
529 t1v = vec_sub( t1v, t2v ); /* a-5*b+4*c */ \
530 t3v = vec_sl( t3v, fourv ); /* 16*c */ \
531 t1v = vec_add( t1v, t3v ); /* a-5*b+20*c */ \
534 #define HPEL_FILTER_2( t1v, t2v, t3v, t4v, t5v, t6v ) \
536 t1v = vec_add( t1v, t6v ); \
537 t2v = vec_add( t2v, t5v ); \
538 t3v = vec_add( t3v, t4v ); \
540 t1v = vec_sub( t1v, t2v ); /* (a-b) */ \
541 t1v = vec_sra( t1v, twov ); /* (a-b)/4 */ \
542 t1v = vec_sub( t1v, t2v ); /* (a-b)/4-b */ \
543 t1v = vec_add( t1v, t3v ); /* (a-b)/4-b+c */ \
544 t1v = vec_sra( t1v, twov ); /* ((a-b)/4-b+c)/4 */ \
545 t1v = vec_add( t1v, t3v ); /* ((a-b)/4-b+c)/4+c = (a-5*b+20*c)/16 */ \
548 #define HPEL_FILTER_HORIZONTAL() \
550 VEC_LOAD_G( &src[x- 2+i_stride*y], src1v, 16, vec_u8_t); \
551 VEC_LOAD_G( &src[x+14+i_stride*y], src6v, 16, vec_u8_t); \
553 src2v = vec_sld( src1v, src6v, 1 ); \
554 src3v = vec_sld( src1v, src6v, 2 ); \
555 src4v = vec_sld( src1v, src6v, 3 ); \
556 src5v = vec_sld( src1v, src6v, 4 ); \
557 src6v = vec_sld( src1v, src6v, 5 ); \
559 temp1v = vec_u8_to_s16_h( src1v ); \
560 temp2v = vec_u8_to_s16_h( src2v ); \
561 temp3v = vec_u8_to_s16_h( src3v ); \
562 temp4v = vec_u8_to_s16_h( src4v ); \
563 temp5v = vec_u8_to_s16_h( src5v ); \
564 temp6v = vec_u8_to_s16_h( src6v ); \
566 HPEL_FILTER_1( temp1v, temp2v, temp3v, \
567 temp4v, temp5v, temp6v ); \
569 dest1v = vec_add( temp1v, sixteenv ); \
570 dest1v = vec_sra( dest1v, fivev ); \
572 temp1v = vec_u8_to_s16_l( src1v ); \
573 temp2v = vec_u8_to_s16_l( src2v ); \
574 temp3v = vec_u8_to_s16_l( src3v ); \
575 temp4v = vec_u8_to_s16_l( src4v ); \
576 temp5v = vec_u8_to_s16_l( src5v ); \
577 temp6v = vec_u8_to_s16_l( src6v ); \
579 HPEL_FILTER_1( temp1v, temp2v, temp3v, \
580 temp4v, temp5v, temp6v ); \
582 dest2v = vec_add( temp1v, sixteenv ); \
583 dest2v = vec_sra( dest2v, fivev ); \
585 destv = vec_packsu( dest1v, dest2v ); \
587 VEC_STORE16( destv, &dsth[x+i_stride*y], dsth ); \
590 #define HPEL_FILTER_VERTICAL() \
592 VEC_LOAD( &src[x+i_stride*(y-2)], src1v, 16, vec_u8_t, src ); \
593 VEC_LOAD( &src[x+i_stride*(y-1)], src2v, 16, vec_u8_t, src ); \
594 VEC_LOAD( &src[x+i_stride*(y-0)], src3v, 16, vec_u8_t, src ); \
595 VEC_LOAD( &src[x+i_stride*(y+1)], src4v, 16, vec_u8_t, src ); \
596 VEC_LOAD( &src[x+i_stride*(y+2)], src5v, 16, vec_u8_t, src ); \
597 VEC_LOAD( &src[x+i_stride*(y+3)], src6v, 16, vec_u8_t, src ); \
599 temp1v = vec_u8_to_s16_h( src1v ); \
600 temp2v = vec_u8_to_s16_h( src2v ); \
601 temp3v = vec_u8_to_s16_h( src3v ); \
602 temp4v = vec_u8_to_s16_h( src4v ); \
603 temp5v = vec_u8_to_s16_h( src5v ); \
604 temp6v = vec_u8_to_s16_h( src6v ); \
606 HPEL_FILTER_1( temp1v, temp2v, temp3v, \
607 temp4v, temp5v, temp6v ); \
609 dest1v = vec_add( temp1v, sixteenv ); \
610 dest1v = vec_sra( dest1v, fivev ); \
612 temp4v = vec_u8_to_s16_l( src1v ); \
613 temp5v = vec_u8_to_s16_l( src2v ); \
614 temp6v = vec_u8_to_s16_l( src3v ); \
615 temp7v = vec_u8_to_s16_l( src4v ); \
616 temp8v = vec_u8_to_s16_l( src5v ); \
617 temp9v = vec_u8_to_s16_l( src6v ); \
619 HPEL_FILTER_1( temp4v, temp5v, temp6v, \
620 temp7v, temp8v, temp9v ); \
622 dest2v = vec_add( temp4v, sixteenv ); \
623 dest2v = vec_sra( dest2v, fivev ); \
625 destv = vec_packsu( dest1v, dest2v ); \
627 VEC_STORE16( destv, &dstv[x+i_stride*y], dsth ); \
630 #define HPEL_FILTER_CENTRAL() \
632 temp1v = vec_sld( tempav, tempbv, 12 ); \
633 temp2v = vec_sld( tempav, tempbv, 14 ); \
635 temp4v = vec_sld( tempbv, tempcv, 2 ); \
636 temp5v = vec_sld( tempbv, tempcv, 4 ); \
637 temp6v = vec_sld( tempbv, tempcv, 6 ); \
639 HPEL_FILTER_2( temp1v, temp2v, temp3v, \
640 temp4v, temp5v, temp6v ); \
642 dest1v = vec_add( temp1v, thirtytwov ); \
643 dest1v = vec_sra( dest1v, sixv ); \
645 temp1v = vec_sld( tempbv, tempcv, 12 ); \
646 temp2v = vec_sld( tempbv, tempcv, 14 ); \
648 temp4v = vec_sld( tempcv, tempdv, 2 ); \
649 temp5v = vec_sld( tempcv, tempdv, 4 ); \
650 temp6v = vec_sld( tempcv, tempdv, 6 ); \
652 HPEL_FILTER_2( temp1v, temp2v, temp3v, \
653 temp4v, temp5v, temp6v ); \
655 dest2v = vec_add( temp1v, thirtytwov ); \
656 dest2v = vec_sra( dest2v, sixv ); \
658 destv = vec_packsu( dest1v, dest2v ); \
660 VEC_STORE16( destv, &dstc[x-16+i_stride*y], dsth ); \
663 void x264_hpel_filter_altivec( uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, uint8_t *src,
664 intptr_t i_stride, int i_width, int i_height, int16_t *buf )
667 vec_u8_t src1v, src2v, src3v, src4v, src5v, src6v;
668 vec_s16_t dest1v, dest2v;
669 vec_s16_t temp1v, temp2v, temp3v, temp4v, temp5v, temp6v, temp7v, temp8v, temp9v;
670 vec_s16_t tempav, tempbv, tempcv, tempdv, tempev;
675 PREP_STORE16_DST( dsth );
678 vec_u16_t twov, fourv, fivev, sixv;
679 vec_s16_t sixteenv, thirtytwov;
683 twov = vec_splat( temp_u.v, 0 );
685 fourv = vec_splat( temp_u.v, 0 );
687 fivev = vec_splat( temp_u.v, 0 );
689 sixv = vec_splat( temp_u.v, 0 );
691 sixteenv = (vec_s16_t)vec_splat( temp_u.v, 0 );
693 thirtytwov = (vec_s16_t)vec_splat( temp_u.v, 0 );
695 for( int y = 0; y < i_height; y++ )
699 /* horizontal_filter */
700 HPEL_FILTER_HORIZONTAL();
702 /* vertical_filter */
703 HPEL_FILTER_VERTICAL();
708 tempcv = vec_splat( temp1v, 0 ); /* first only */
712 for( x = 16; x < i_width; x+=16 )
714 /* horizontal_filter */
715 HPEL_FILTER_HORIZONTAL();
717 /* vertical_filter */
718 HPEL_FILTER_VERTICAL();
727 HPEL_FILTER_CENTRAL();
730 /* Partial vertical filter */
731 VEC_LOAD_PARTIAL( &src[x+i_stride*(y-2)], src1v, 16, vec_u8_t, src );
732 VEC_LOAD_PARTIAL( &src[x+i_stride*(y-1)], src2v, 16, vec_u8_t, src );
733 VEC_LOAD_PARTIAL( &src[x+i_stride*(y-0)], src3v, 16, vec_u8_t, src );
734 VEC_LOAD_PARTIAL( &src[x+i_stride*(y+1)], src4v, 16, vec_u8_t, src );
735 VEC_LOAD_PARTIAL( &src[x+i_stride*(y+2)], src5v, 16, vec_u8_t, src );
736 VEC_LOAD_PARTIAL( &src[x+i_stride*(y+3)], src6v, 16, vec_u8_t, src );
738 temp1v = vec_u8_to_s16_h( src1v );
739 temp2v = vec_u8_to_s16_h( src2v );
740 temp3v = vec_u8_to_s16_h( src3v );
741 temp4v = vec_u8_to_s16_h( src4v );
742 temp5v = vec_u8_to_s16_h( src5v );
743 temp6v = vec_u8_to_s16_h( src6v );
745 HPEL_FILTER_1( temp1v, temp2v, temp3v, temp4v, temp5v, temp6v );
752 /* tempev is not used */
754 HPEL_FILTER_CENTRAL();
758 static void frame_init_lowres_core_altivec( uint8_t *src0, uint8_t *dst0, uint8_t *dsth, uint8_t *dstv, uint8_t *dstc,
759 intptr_t src_stride, intptr_t dst_stride, int width, int height )
762 int end = (width & 15);
763 vec_u8_t src0v, src1v, src2v;
764 vec_u8_t lv, hv, src1p1v;
765 vec_u8_t avg0v, avg1v, avghv, avghp1v, avgleftv, avgrightv;
766 static const vec_u8_t inverse_bridge_shuffle = CV(0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1A, 0x1C, 0x1E );
768 for( int y = 0; y < height; y++ )
771 uint8_t *src1 = src0+src_stride;
772 uint8_t *src2 = src1+src_stride;
774 src0v = vec_ld(0, src0);
775 src1v = vec_ld(0, src1);
776 src2v = vec_ld(0, src2);
778 avg0v = vec_avg(src0v, src1v);
779 avg1v = vec_avg(src1v, src2v);
781 for( x = 0; x < w; x++ )
783 lv = vec_ld(16*(x*2+1), src0);
784 src1v = vec_ld(16*(x*2+1), src1);
785 avghv = vec_avg(lv, src1v);
787 lv = vec_ld(16*(x*2+2), src0);
788 src1p1v = vec_ld(16*(x*2+2), src1);
789 avghp1v = vec_avg(lv, src1p1v);
791 avgleftv = vec_avg(vec_sld(avg0v, avghv, 1), avg0v);
792 avgrightv = vec_avg(vec_sld(avghv, avghp1v, 1), avghv);
794 vec_st(vec_perm(avgleftv, avgrightv, inverse_bridge_shuffle), 16*x, dst0);
795 vec_st((vec_u8_t)vec_pack((vec_u16_t)avgleftv,(vec_u16_t)avgrightv), 16*x, dsth);
799 hv = vec_ld(16*(x*2+1), src2);
800 avghv = vec_avg(src1v, hv);
802 hv = vec_ld(16*(x*2+2), src2);
803 avghp1v = vec_avg(src1p1v, hv);
805 avgleftv = vec_avg(vec_sld(avg1v, avghv, 1), avg1v);
806 avgrightv = vec_avg(vec_sld(avghv, avghp1v, 1), avghv);
808 vec_st(vec_perm(avgleftv, avgrightv, inverse_bridge_shuffle), 16*x, dstv);
809 vec_st((vec_u8_t)vec_pack((vec_u16_t)avgleftv,(vec_u16_t)avgrightv), 16*x, dstc);
816 lv = vec_ld(16*(x*2+1), src0);
817 src1v = vec_ld(16*(x*2+1), src1);
818 avghv = vec_avg(lv, src1v);
820 lv = vec_ld(16*(x*2+1), src2);
821 avghp1v = vec_avg(src1v, lv);
823 avgleftv = vec_avg(vec_sld(avg0v, avghv, 1), avg0v);
824 avgrightv = vec_avg(vec_sld(avg1v, avghp1v, 1), avg1v);
826 lv = vec_perm(avgleftv, avgrightv, inverse_bridge_shuffle);
827 hv = (vec_u8_t)vec_pack((vec_u16_t)avgleftv,(vec_u16_t)avgrightv);
829 vec_ste((vec_u32_t)lv,16*x,(uint32_t*)dst0);
830 vec_ste((vec_u32_t)lv,16*x+4,(uint32_t*)dst0);
831 vec_ste((vec_u32_t)hv,16*x,(uint32_t*)dsth);
832 vec_ste((vec_u32_t)hv,16*x+4,(uint32_t*)dsth);
834 lv = vec_sld(lv, lv, 8);
835 hv = vec_sld(hv, hv, 8);
837 vec_ste((vec_u32_t)lv,16*x,(uint32_t*)dstv);
838 vec_ste((vec_u32_t)lv,16*x+4,(uint32_t*)dstv);
839 vec_ste((vec_u32_t)hv,16*x,(uint32_t*)dstc);
840 vec_ste((vec_u32_t)hv,16*x+4,(uint32_t*)dstc);
843 src0 += src_stride*2;
851 static void mc_weight_w2_altivec( uint8_t *dst, intptr_t i_dst, uint8_t *src, intptr_t i_src,
852 const x264_weight_t *weight, int i_height )
856 PREP_LOAD_SRC( src );
859 vec_s16_t scalev, offsetv, denomv, roundv;
862 int denom = weight->i_denom;
864 loadv.s[0] = weight->i_scale;
865 scalev = vec_splat( loadv.v, 0 );
867 loadv.s[0] = weight->i_offset;
868 offsetv = vec_splat( loadv.v, 0 );
873 denomv = vec_splat( loadv.v, 0 );
875 loadv.s[0] = 1<<(denom - 1);
876 roundv = vec_splat( loadv.v, 0 );
878 for( int y = 0; y < i_height; y++, dst += i_dst, src += i_src )
880 VEC_LOAD( src, srcv, 2, vec_u8_t, src );
881 weightv = vec_u8_to_s16( srcv );
883 weightv = vec_mladd( weightv, scalev, roundv );
884 weightv = vec_sra( weightv, (vec_u16_t)denomv );
885 weightv = vec_add( weightv, offsetv );
887 srcv = vec_packsu( weightv, zero_s16v );
888 vec_ste( vec_splat( (vec_u16_t)srcv, 0 ), 0, (uint16_t*)dst );
893 for( int y = 0; y < i_height; y++, dst += i_dst, src += i_src )
895 VEC_LOAD( src, srcv, 2, vec_u8_t, src );
896 weightv = vec_u8_to_s16( srcv );
898 weightv = vec_mladd( weightv, scalev, offsetv );
900 srcv = vec_packsu( weightv, zero_s16v );
901 vec_ste( vec_splat( (vec_u16_t)srcv, 0 ), 0, (uint16_t*)dst );
905 static void mc_weight_w4_altivec( uint8_t *dst, intptr_t i_dst, uint8_t *src, intptr_t i_src,
906 const x264_weight_t *weight, int i_height )
910 PREP_LOAD_SRC( src );
913 vec_s16_t scalev, offsetv, denomv, roundv;
916 int denom = weight->i_denom;
918 loadv.s[0] = weight->i_scale;
919 scalev = vec_splat( loadv.v, 0 );
921 loadv.s[0] = weight->i_offset;
922 offsetv = vec_splat( loadv.v, 0 );
927 denomv = vec_splat( loadv.v, 0 );
929 loadv.s[0] = 1<<(denom - 1);
930 roundv = vec_splat( loadv.v, 0 );
932 for( int y = 0; y < i_height; y++, dst += i_dst, src += i_src )
934 VEC_LOAD( src, srcv, 4, vec_u8_t, src );
935 weightv = vec_u8_to_s16( srcv );
937 weightv = vec_mladd( weightv, scalev, roundv );
938 weightv = vec_sra( weightv, (vec_u16_t)denomv );
939 weightv = vec_add( weightv, offsetv );
941 srcv = vec_packsu( weightv, zero_s16v );
942 vec_ste( vec_splat( (vec_u32_t)srcv, 0 ), 0, (uint32_t*)dst );
947 for( int y = 0; y < i_height; y++, dst += i_dst, src += i_src )
949 VEC_LOAD( src, srcv, 4, vec_u8_t, src );
950 weightv = vec_u8_to_s16( srcv );
952 weightv = vec_mladd( weightv, scalev, offsetv );
954 srcv = vec_packsu( weightv, zero_s16v );
955 vec_ste( vec_splat( (vec_u32_t)srcv, 0 ), 0, (uint32_t*)dst );
959 static void mc_weight_w8_altivec( uint8_t *dst, intptr_t i_dst, uint8_t *src, intptr_t i_src,
960 const x264_weight_t *weight, int i_height )
964 PREP_LOAD_SRC( src );
968 vec_s16_t scalev, offsetv, denomv, roundv;
971 int denom = weight->i_denom;
973 loadv.s[0] = weight->i_scale;
974 scalev = vec_splat( loadv.v, 0 );
976 loadv.s[0] = weight->i_offset;
977 offsetv = vec_splat( loadv.v, 0 );
982 denomv = vec_splat( loadv.v, 0 );
984 loadv.s[0] = 1<<(denom - 1);
985 roundv = vec_splat( loadv.v, 0 );
987 for( int y = 0; y < i_height; y++, dst += i_dst, src += i_src )
989 VEC_LOAD( src, srcv, 8, vec_u8_t, src );
990 weightv = vec_u8_to_s16( srcv );
992 weightv = vec_mladd( weightv, scalev, roundv );
993 weightv = vec_sra( weightv, (vec_u16_t)denomv );
994 weightv = vec_add( weightv, offsetv );
996 srcv = vec_packsu( weightv, zero_s16v );
997 VEC_STORE8( srcv, dst );
1002 for( int y = 0; y < i_height; y++, dst += i_dst, src += i_src )
1004 VEC_LOAD( src, srcv, 8, vec_u8_t, src );
1005 weightv = vec_u8_to_s16( srcv );
1007 weightv = vec_mladd( weightv, scalev, offsetv );
1009 srcv = vec_packsu( weightv, zero_s16v );
1010 VEC_STORE8( srcv, dst );
1014 static void mc_weight_w16_altivec( uint8_t *dst, intptr_t i_dst, uint8_t *src, intptr_t i_src,
1015 const x264_weight_t *weight, int i_height )
1019 PREP_LOAD_SRC( src );
1021 vec_s16_t weight_lv, weight_hv;
1022 vec_s16_t scalev, offsetv, denomv, roundv;
1025 int denom = weight->i_denom;
1027 loadv.s[0] = weight->i_scale;
1028 scalev = vec_splat( loadv.v, 0 );
1030 loadv.s[0] = weight->i_offset;
1031 offsetv = vec_splat( loadv.v, 0 );
1036 denomv = vec_splat( loadv.v, 0 );
1038 loadv.s[0] = 1<<(denom - 1);
1039 roundv = vec_splat( loadv.v, 0 );
1041 for( int y = 0; y < i_height; y++, dst += i_dst, src += i_src )
1043 VEC_LOAD( src, srcv, 16, vec_u8_t, src );
1044 weight_hv = vec_u8_to_s16_h( srcv );
1045 weight_lv = vec_u8_to_s16_l( srcv );
1047 weight_hv = vec_mladd( weight_hv, scalev, roundv );
1048 weight_lv = vec_mladd( weight_lv, scalev, roundv );
1049 weight_hv = vec_sra( weight_hv, (vec_u16_t)denomv );
1050 weight_lv = vec_sra( weight_lv, (vec_u16_t)denomv );
1051 weight_hv = vec_add( weight_hv, offsetv );
1052 weight_lv = vec_add( weight_lv, offsetv );
1054 srcv = vec_packsu( weight_hv, weight_lv );
1055 vec_st( srcv, 0, dst );
1060 for( int y = 0; y < i_height; y++, dst += i_dst, src += i_src )
1062 VEC_LOAD( src, srcv, 16, vec_u8_t, src );
1063 weight_hv = vec_u8_to_s16_h( srcv );
1064 weight_lv = vec_u8_to_s16_l( srcv );
1066 weight_hv = vec_mladd( weight_hv, scalev, offsetv );
1067 weight_lv = vec_mladd( weight_lv, scalev, offsetv );
1069 srcv = vec_packsu( weight_hv, weight_lv );
1070 vec_st( srcv, 0, dst );
1074 static void mc_weight_w20_altivec( uint8_t *dst, intptr_t i_dst, uint8_t *src, intptr_t i_src,
1075 const x264_weight_t *weight, int i_height )
1078 PREP_LOAD_SRC( src );
1079 vec_u8_t src_1v, src_2v, src_3v;
1080 vec_s16_t weight_lv, weight_hv, weight_3v;
1081 vec_s16_t scalev, offsetv, denomv, roundv;
1084 int denom = weight->i_denom;
1086 loadv.s[0] = weight->i_scale;
1087 scalev = vec_splat( loadv.v, 0 );
1089 loadv.s[0] = weight->i_offset;
1090 offsetv = vec_splat( loadv.v, 0 );
1095 denomv = vec_splat( loadv.v, 0 );
1097 loadv.s[0] = 1<<(denom - 1);
1098 roundv = vec_splat( loadv.v, 0 );
1100 for( int y = 0; y < i_height; y++, dst += i_dst, src += i_src )
1102 src_1v = vec_ld( 0, src );
1103 src_2v = vec_ld( 16, src );
1104 src_3v = vec_ld( 19, src );
1105 src_1v = vec_perm( src_1v, src_2v, _src_ );
1106 src_3v = vec_perm( src_2v, src_3v, _src_ );
1107 weight_hv = vec_u8_to_s16_h( src_1v );
1108 weight_lv = vec_u8_to_s16_l( src_1v );
1109 weight_3v = vec_u8_to_s16_h( src_3v );
1111 weight_hv = vec_mladd( weight_hv, scalev, roundv );
1112 weight_lv = vec_mladd( weight_lv, scalev, roundv );
1113 weight_3v = vec_mladd( weight_3v, scalev, roundv );
1114 weight_hv = vec_sra( weight_hv, (vec_u16_t)denomv );
1115 weight_lv = vec_sra( weight_lv, (vec_u16_t)denomv );
1116 weight_3v = vec_sra( weight_3v, (vec_u16_t)denomv );
1117 weight_hv = vec_add( weight_hv, offsetv );
1118 weight_lv = vec_add( weight_lv, offsetv );
1119 weight_3v = vec_add( weight_3v, offsetv );
1121 src_1v = vec_packsu( weight_hv, weight_lv );
1122 src_3v = vec_packsu( weight_3v, zero_s16v );
1123 vec_st( src_1v, 0, dst );
1124 vec_ste( (vec_u32_t)src_3v, 16, (uint32_t*)dst );
1129 for( int y = 0; y < i_height; y++, dst += i_dst, src += i_src )
1131 src_1v = vec_ld( 0, src );
1132 src_2v = vec_ld( 16, src );
1133 src_3v = vec_ld( 19, src );
1134 src_1v = vec_perm( src_1v, src_2v, _src_ );
1135 src_3v = vec_perm( src_2v, src_3v, _src_ );
1136 weight_hv = vec_u8_to_s16_h( src_1v );
1137 weight_lv = vec_u8_to_s16_l( src_1v );
1138 weight_3v = vec_u8_to_s16_h( src_3v );
1140 weight_hv = vec_mladd( weight_hv, scalev, offsetv );
1141 weight_lv = vec_mladd( weight_lv, scalev, offsetv );
1142 weight_3v = vec_mladd( weight_3v, scalev, offsetv );
1144 src_1v = vec_packsu( weight_hv, weight_lv );
1145 src_3v = vec_packsu( weight_3v, zero_s16v );
1146 vec_st( src_1v, 0, dst );
1147 vec_ste( (vec_u32_t)src_3v, 16, (uint32_t*)dst );
1152 static weight_fn_t x264_mc_weight_wtab_altivec[6] =
1154 mc_weight_w2_altivec,
1155 mc_weight_w4_altivec,
1156 mc_weight_w8_altivec,
1157 mc_weight_w16_altivec,
1158 mc_weight_w16_altivec,
1159 mc_weight_w20_altivec,
1162 #endif // !HIGH_BIT_DEPTH
1164 void x264_mc_altivec_init( x264_mc_functions_t *pf )
1167 pf->mc_luma = mc_luma_altivec;
1168 pf->get_ref = get_ref_altivec;
1169 pf->mc_chroma = mc_chroma_altivec;
1171 pf->copy_16x16_unaligned = x264_mc_copy_w16_altivec;
1172 pf->copy[PIXEL_16x16] = x264_mc_copy_w16_aligned_altivec;
1174 pf->hpel_filter = x264_hpel_filter_altivec;
1175 pf->frame_init_lowres_core = frame_init_lowres_core_altivec;
1177 pf->weight = x264_mc_weight_wtab_altivec;
1178 #endif // !HIGH_BIT_DEPTH