1 /*****************************************************************************
2 * mc.c: h264 encoder library (Motion Compensation)
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Eric Petit <titer@m0k.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.
22 *****************************************************************************/
35 #include "common/common.h"
36 #include "common/mc.h"
38 #include "ppccommon.h"
40 typedef void (*pf_mc_t)( uint8_t *src, int i_src,
41 uint8_t *dst, int 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] +
49 static inline int x264_tapfilter1( uint8_t *pix )
51 return pix[-2] - 5*pix[-1] + 20*(pix[0] + pix[1]) - 5*pix[ 2] +
56 static inline void pixel_avg_w4( uint8_t *dst, int i_dst,
57 uint8_t *src1, int i_src1,
58 uint8_t *src2, int i_src2,
62 for( y = 0; y < i_height; y++ )
64 for( x = 0; x < 4; x++ )
66 dst[x] = ( src1[x] + src2[x] + 1 ) >> 1;
73 static inline void pixel_avg_w8( uint8_t *dst, int i_dst,
74 uint8_t *src1, int i_src1,
75 uint8_t *src2, int i_src2,
79 vec_u8_t src1v, src2v;
83 for( y = 0; y < i_height; y++ )
85 VEC_LOAD( src1, src1v, 8, vec_u8_t );
86 VEC_LOAD( src2, src2v, 8, vec_u8_t );
87 src1v = vec_avg( src1v, src2v );
88 VEC_STORE8( src1v, dst );
95 static inline void pixel_avg_w16( uint8_t *dst, int i_dst,
96 uint8_t *src1, int i_src1,
97 uint8_t *src2, int i_src2,
101 vec_u8_t src1v, src2v;
104 for( y = 0; y < i_height; y++ )
106 VEC_LOAD( src1, src1v, 16, vec_u8_t );
107 VEC_LOAD( src2, src2v, 16, vec_u8_t );
108 src1v = vec_avg( src1v, src2v );
109 VEC_STORE16( src1v, dst );
117 /* mc_copy: plain c */
118 #define MC_COPY( name, a ) \
119 static void name( uint8_t *src, int i_src, \
120 uint8_t *dst, int i_dst, int i_height ) \
123 for( y = 0; y < i_height; y++ ) \
125 memcpy( dst, src, a ); \
130 MC_COPY( mc_copy_w4, 4 )
131 MC_COPY( mc_copy_w8, 8 )
132 MC_COPY( mc_copy_w16, 16 )
134 void mc_luma_altivec( uint8_t *dst, int i_dst_stride,
135 uint8_t *src[4], int i_src_stride,
137 int i_width, int i_height )
139 uint8_t *src1, *src2;
141 /* todo : fixme... */
142 int correction = (((mvx&3) == 3 && (mvy&3) == 1) || ((mvx&3) == 1 && (mvy&3) == 3)) ? 1:0;
145 int hpel1y = (mvy+1-correction)>>1;
146 int filter1 = (hpel1x & 1) + ( (hpel1y & 1) << 1 );
149 src1 = src[filter1] + (hpel1y >> 1) * i_src_stride + (hpel1x >> 1);
151 if ( (mvx|mvy) & 1 ) /* qpel interpolation needed */
153 int hpel2x = (mvx+1)>>1;
154 int hpel2y = (mvy+correction)>>1;
155 int filter2 = (hpel2x & 1) + ( (hpel2y & 1) <<1 );
157 src2 = src[filter2] + (hpel2y >> 1) * i_src_stride + (hpel2x >> 1);
161 pixel_avg_w4( dst, i_dst_stride, src1, i_src_stride,
162 src2, i_src_stride, i_height );
165 pixel_avg_w8( dst, i_dst_stride, src1, i_src_stride,
166 src2, i_src_stride, i_height );
170 pixel_avg_w16( dst, i_dst_stride, src1, i_src_stride,
171 src2, i_src_stride, i_height );
179 mc_copy_w4( src1, i_src_stride, dst, i_dst_stride, i_height );
182 mc_copy_w8( src1, i_src_stride, dst, i_dst_stride, i_height );
185 mc_copy_w16( src1, i_src_stride, dst, i_dst_stride, i_height );
192 uint8_t *get_ref_altivec( uint8_t *dst, int * i_dst_stride,
193 uint8_t *src[4], int i_src_stride,
195 int i_width, int i_height )
197 uint8_t *src1, *src2;
199 /* todo : fixme... */
200 int correction = (((mvx&3) == 3 && (mvy&3) == 1) || ((mvx&3) == 1 && (mvy&3) == 3)) ? 1:0;
203 int hpel1y = (mvy+1-correction)>>1;
204 int filter1 = (hpel1x & 1) + ( (hpel1y & 1) << 1 );
207 src1 = src[filter1] + (hpel1y >> 1) * i_src_stride + (hpel1x >> 1);
209 if ( (mvx|mvy) & 1 ) /* qpel interpolation needed */
211 int hpel2x = (mvx+1)>>1;
212 int hpel2y = (mvy+correction)>>1;
213 int filter2 = (hpel2x & 1) + ( (hpel2y & 1) <<1 );
215 src2 = src[filter2] + (hpel2y >> 1) * i_src_stride + (hpel2x >> 1);
219 pixel_avg_w4( dst, *i_dst_stride, src1, i_src_stride,
220 src2, i_src_stride, i_height );
223 pixel_avg_w8( dst, *i_dst_stride, src1, i_src_stride,
224 src2, i_src_stride, i_height );
229 pixel_avg_w16( dst, *i_dst_stride, src1, i_src_stride,
230 src2, i_src_stride, i_height );
234 pixel_avg_w16( dst, *i_dst_stride, src1, i_src_stride,
235 src2, i_src_stride, i_height );
236 pixel_avg_w4( dst+16, *i_dst_stride, src1+16, i_src_stride,
237 src2+16, i_src_stride, i_height );
245 *i_dst_stride = i_src_stride;
250 #define DO_PROCESS(a) \
251 src##a##v_16 = vec_u8_to_u16( src##a##v_8 ); \
252 src##a##v_16 = vec_mladd( coeff##a##v, src##a##v_16, zero_u16v ); \
253 dstv_16 = vec_add( dstv_16, src##a##v_16 )
255 static void mc_chroma_altivec_4xh( uint8_t *dst, int i_dst_stride,
256 uint8_t *src, int i_src_stride,
262 int d8x = mvx & 0x07;
263 int d8y = mvy & 0x07;
265 DECLARE_ALIGNED_16( uint16_t coeff[4] );
266 coeff[0] = (8-d8x)*(8-d8y);
267 coeff[1] = d8x *(8-d8y);
268 coeff[2] = (8-d8x)*d8y;
271 src += (mvy >> 3) * i_src_stride + (mvx >> 3);
272 srcp = &src[i_src_stride];
277 vec_u16_t coeff0v, coeff1v, coeff2v, coeff3v;
278 vec_u8_t src0v_8, src1v_8, src2v_8, src3v_8;
279 vec_u16_t src0v_16, src1v_16, src2v_16, src3v_16;
286 coeff0v = vec_ld( 0, coeff );
287 coeff3v = vec_splat( coeff0v, 3 );
288 coeff2v = vec_splat( coeff0v, 2 );
289 coeff1v = vec_splat( coeff0v, 1 );
290 coeff0v = vec_splat( coeff0v, 0 );
291 k32v = vec_sl( vec_splat_u16( 1 ), vec_splat_u16( 5 ) );
292 permv = vec_lvsl( 0, (uint8_t *) 1 );
293 shiftv = vec_splat_u16( 6 );
295 VEC_LOAD( src, src2v_8, 5, vec_u8_t );
296 src3v_8 = vec_perm( src2v_8, src2v_8, permv );
298 for( y = 0; y < i_height; y++ )
302 VEC_LOAD( srcp, src2v_8, 5, vec_u8_t );
303 src3v_8 = vec_perm( src2v_8, src2v_8, permv );
312 dstv_16 = vec_sr( dstv_16, shiftv );
313 dstv_8 = vec_u16_to_u8( dstv_16 );
314 VEC_STORE4( dstv_8, dst );
317 srcp += i_src_stride;
321 static void mc_chroma_altivec_8xh( uint8_t *dst, int i_dst_stride,
322 uint8_t *src, int i_src_stride,
328 int d8x = mvx & 0x07;
329 int d8y = mvy & 0x07;
331 DECLARE_ALIGNED_16( uint16_t coeff[4] );
332 coeff[0] = (8-d8x)*(8-d8y);
333 coeff[1] = d8x *(8-d8y);
334 coeff[2] = (8-d8x)*d8y;
337 src += (mvy >> 3) * i_src_stride + (mvx >> 3);
338 srcp = &src[i_src_stride];
343 vec_u16_t coeff0v, coeff1v, coeff2v, coeff3v;
344 vec_u8_t src0v_8, src1v_8, src2v_8, src3v_8;
345 vec_u16_t src0v_16, src1v_16, src2v_16, src3v_16;
352 coeff0v = vec_ld( 0, coeff );
353 coeff3v = vec_splat( coeff0v, 3 );
354 coeff2v = vec_splat( coeff0v, 2 );
355 coeff1v = vec_splat( coeff0v, 1 );
356 coeff0v = vec_splat( coeff0v, 0 );
357 k32v = vec_sl( vec_splat_u16( 1 ), vec_splat_u16( 5 ) );
358 permv = vec_lvsl( 0, (uint8_t *) 1 );
359 shiftv = vec_splat_u16( 6 );
361 VEC_LOAD( src, src2v_8, 9, vec_u8_t );
362 src3v_8 = vec_perm( src2v_8, src2v_8, permv );
364 for( y = 0; y < i_height; y++ )
368 VEC_LOAD( srcp, src2v_8, 9, vec_u8_t );
369 src3v_8 = vec_perm( src2v_8, src2v_8, permv );
378 dstv_16 = vec_sr( dstv_16, shiftv );
379 dstv_8 = vec_u16_to_u8( dstv_16 );
380 VEC_STORE8( dstv_8, dst );
383 srcp += i_src_stride;
387 static void mc_chroma_altivec( uint8_t *dst, int i_dst_stride,
388 uint8_t *src, int i_src_stride,
390 int i_width, int i_height )
394 mc_chroma_altivec_8xh( dst, i_dst_stride, src, i_src_stride,
395 mvx, mvy, i_height );
399 mc_chroma_altivec_4xh( dst, i_dst_stride, src, i_src_stride,
400 mvx, mvy, i_height );
404 #define HPEL_FILTER_1( t1v, t2v, t3v, t4v, t5v, t6v ) \
406 t1v = vec_add( t1v, t6v ); \
407 t2v = vec_add( t2v, t5v ); \
408 t3v = vec_add( t3v, t4v ); \
410 t1v = vec_sub( t1v, t2v ); /* (a-b) */ \
411 t2v = vec_sub( t2v, t3v ); /* (b-c) */ \
412 t2v = vec_sl( t2v, twov ); /* (b-c)*4 */ \
413 t1v = vec_sub( t1v, t2v ); /* a-5*b+4*c */ \
414 t3v = vec_sl( t3v, fourv ); /* 16*c */ \
415 t1v = vec_add( t1v, t3v ); /* a-5*b+20*c */ \
418 #define HPEL_FILTER_2( t1v, t2v, t3v, t4v, t5v, t6v ) \
420 t1v = vec_add( t1v, t6v ); \
421 t2v = vec_add( t2v, t5v ); \
422 t3v = vec_add( t3v, t4v ); \
424 t1v = vec_sub( t1v, t2v ); /* (a-b) */ \
425 t1v = vec_sra( t1v, twov ); /* (a-b)/4 */ \
426 t1v = vec_sub( t1v, t2v ); /* (a-b)/4-b */ \
427 t1v = vec_add( t1v, t3v ); /* (a-b)/4-b+c */ \
428 t1v = vec_sra( t1v, twov ); /* ((a-b)/4-b+c)/4 */ \
429 t1v = vec_add( t1v, t3v ); /* ((a-b)/4-b+c)/4+c = (a-5*b+20*c)/16 */ \
432 #define HPEL_FILTER_HORIZONTAL() \
434 VEC_LOAD( &src[x- 2+i_stride*y], src1v, 16, vec_u8_t ); \
435 VEC_LOAD( &src[x+14+i_stride*y], src6v, 16, vec_u8_t ); \
437 src2v = vec_sld( src1v, src6v, 1 ); \
438 src3v = vec_sld( src1v, src6v, 2 ); \
439 src4v = vec_sld( src1v, src6v, 3 ); \
440 src5v = vec_sld( src1v, src6v, 4 ); \
441 src6v = vec_sld( src1v, src6v, 5 ); \
443 temp1v = vec_u8_to_s16_h( src1v ); \
444 temp2v = vec_u8_to_s16_h( src2v ); \
445 temp3v = vec_u8_to_s16_h( src3v ); \
446 temp4v = vec_u8_to_s16_h( src4v ); \
447 temp5v = vec_u8_to_s16_h( src5v ); \
448 temp6v = vec_u8_to_s16_h( src6v ); \
450 HPEL_FILTER_1( temp1v, temp2v, temp3v, \
451 temp4v, temp5v, temp6v ); \
453 dest1v = vec_add( temp1v, sixteenv ); \
454 dest1v = vec_sra( dest1v, fivev ); \
456 temp1v = vec_u8_to_s16_l( src1v ); \
457 temp2v = vec_u8_to_s16_l( src2v ); \
458 temp3v = vec_u8_to_s16_l( src3v ); \
459 temp4v = vec_u8_to_s16_l( src4v ); \
460 temp5v = vec_u8_to_s16_l( src5v ); \
461 temp6v = vec_u8_to_s16_l( src6v ); \
463 HPEL_FILTER_1( temp1v, temp2v, temp3v, \
464 temp4v, temp5v, temp6v ); \
466 dest2v = vec_add( temp1v, sixteenv ); \
467 dest2v = vec_sra( dest2v, fivev ); \
469 destv = vec_packsu( dest1v, dest2v ); \
471 VEC_STORE16( destv, &dsth[x+i_stride*y] ); \
474 #define HPEL_FILTER_VERTICAL() \
476 VEC_LOAD( &src[x+i_stride*(y-2)], src1v, 16, vec_u8_t ); \
477 VEC_LOAD( &src[x+i_stride*(y-1)], src2v, 16, vec_u8_t ); \
478 VEC_LOAD( &src[x+i_stride*(y-0)], src3v, 16, vec_u8_t ); \
479 VEC_LOAD( &src[x+i_stride*(y+1)], src4v, 16, vec_u8_t ); \
480 VEC_LOAD( &src[x+i_stride*(y+2)], src5v, 16, vec_u8_t ); \
481 VEC_LOAD( &src[x+i_stride*(y+3)], src6v, 16, vec_u8_t ); \
483 temp1v = vec_u8_to_s16_h( src1v ); \
484 temp2v = vec_u8_to_s16_h( src2v ); \
485 temp3v = vec_u8_to_s16_h( src3v ); \
486 temp4v = vec_u8_to_s16_h( src4v ); \
487 temp5v = vec_u8_to_s16_h( src5v ); \
488 temp6v = vec_u8_to_s16_h( src6v ); \
490 HPEL_FILTER_1( temp1v, temp2v, temp3v, \
491 temp4v, temp5v, temp6v ); \
493 dest1v = vec_add( temp1v, sixteenv ); \
494 dest1v = vec_sra( dest1v, fivev ); \
496 temp4v = vec_u8_to_s16_l( src1v ); \
497 temp5v = vec_u8_to_s16_l( src2v ); \
498 temp6v = vec_u8_to_s16_l( src3v ); \
499 temp7v = vec_u8_to_s16_l( src4v ); \
500 temp8v = vec_u8_to_s16_l( src5v ); \
501 temp9v = vec_u8_to_s16_l( src6v ); \
503 HPEL_FILTER_1( temp4v, temp5v, temp6v, \
504 temp7v, temp8v, temp9v ); \
506 dest2v = vec_add( temp4v, sixteenv ); \
507 dest2v = vec_sra( dest2v, fivev ); \
509 destv = vec_packsu( dest1v, dest2v ); \
511 VEC_STORE16( destv, &dstv[x+i_stride*y] ); \
514 #define HPEL_FILTER_CENTRAL() \
516 temp1v = vec_sld( tempav, tempbv, 12 ); \
517 temp2v = vec_sld( tempav, tempbv, 14 ); \
519 temp4v = vec_sld( tempbv, tempcv, 2 ); \
520 temp5v = vec_sld( tempbv, tempcv, 4 ); \
521 temp6v = vec_sld( tempbv, tempcv, 6 ); \
523 HPEL_FILTER_2( temp1v, temp2v, temp3v, \
524 temp4v, temp5v, temp6v ); \
526 dest1v = vec_add( temp1v, thirtytwov ); \
527 dest1v = vec_sra( dest1v, sixv ); \
529 temp1v = vec_sld( tempbv, tempcv, 12 ); \
530 temp2v = vec_sld( tempbv, tempcv, 14 ); \
532 temp4v = vec_sld( tempcv, tempdv, 2 ); \
533 temp5v = vec_sld( tempcv, tempdv, 4 ); \
534 temp6v = vec_sld( tempcv, tempdv, 6 ); \
536 HPEL_FILTER_2( temp1v, temp2v, temp3v, \
537 temp4v, temp5v, temp6v ); \
539 dest2v = vec_add( temp1v, thirtytwov ); \
540 dest2v = vec_sra( dest2v, sixv ); \
542 destv = vec_packsu( dest1v, dest2v ); \
544 VEC_STORE16( destv, &dstc[x-16+i_stride*y] ); \
547 void x264_hpel_filter_altivec( uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, uint8_t *src,
548 int i_stride, int i_width, int i_height )
553 vec_u8_t src1v, src2v, src3v, src4v, src5v, src6v;
554 vec_s16_t dest1v, dest2v;
555 vec_s16_t temp1v, temp2v, temp3v, temp4v, temp5v, temp6v, temp7v, temp8v, temp9v;
556 vec_s16_t tempav, tempbv, tempcv, tempdv, tempev;
562 vec_u16_t twov, fourv, fivev, sixv;
563 vec_s16_t sixteenv, thirtytwov;
564 vect_ushort_u temp_u;
567 twov = vec_splat( temp_u.v, 0 );
569 fourv = vec_splat( temp_u.v, 0 );
571 fivev = vec_splat( temp_u.v, 0 );
573 sixv = vec_splat( temp_u.v, 0 );
575 sixteenv = (vec_s16_t)vec_splat( temp_u.v, 0 );
577 thirtytwov = (vec_s16_t)vec_splat( temp_u.v, 0 );
579 for( y = 0; y < i_height; y++ )
583 /* horizontal_filter */
584 HPEL_FILTER_HORIZONTAL();
586 /* vertical_filter */
587 HPEL_FILTER_VERTICAL();
592 tempcv = vec_splat( temp1v, 0 ); /* first only */
596 for( x = 16; x < i_width; x+=16 )
598 /* horizontal_filter */
599 HPEL_FILTER_HORIZONTAL();
601 /* vertical_filter */
602 HPEL_FILTER_VERTICAL();
611 HPEL_FILTER_CENTRAL();
614 /* Partial vertical filter */
615 VEC_LOAD( &src[x+i_stride*(y-2)], src1v, 16, vec_u8_t );
616 VEC_LOAD( &src[x+i_stride*(y-1)], src2v, 16, vec_u8_t );
617 VEC_LOAD( &src[x+i_stride*(y-0)], src3v, 16, vec_u8_t );
618 VEC_LOAD( &src[x+i_stride*(y+1)], src4v, 16, vec_u8_t );
619 VEC_LOAD( &src[x+i_stride*(y+2)], src5v, 16, vec_u8_t );
620 VEC_LOAD( &src[x+i_stride*(y+3)], src6v, 16, vec_u8_t );
622 temp1v = vec_u8_to_s16_h( src1v );
623 temp2v = vec_u8_to_s16_h( src2v );
624 temp3v = vec_u8_to_s16_h( src3v );
625 temp4v = vec_u8_to_s16_h( src4v );
626 temp5v = vec_u8_to_s16_h( src5v );
627 temp6v = vec_u8_to_s16_h( src6v );
629 HPEL_FILTER_1( temp1v, temp2v, temp3v,
630 temp4v, temp5v, temp6v );
637 /* tempev is not used */
639 HPEL_FILTER_CENTRAL();
643 void x264_mc_altivec_init( x264_mc_functions_t *pf )
645 pf->mc_luma = mc_luma_altivec;
646 pf->get_ref = get_ref_altivec;
647 pf->mc_chroma = mc_chroma_altivec;
649 pf->hpel_filter = x264_hpel_filter_altivec;