📄 mc.c
字号:
/***************************************************************************** * mc.c: h264 encoder library (Motion Compensation) ***************************************************************************** * Copyright (C) 2003-2008 x264 project * * Authors: Eric Petit <eric.petit@lapsus.org> * Guillaume Poirier <gpoirier@mplayerhq.hu> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. *****************************************************************************/#include <stdlib.h>#include <stdio.h>#include <string.h>#include <stdint.h>#include <stdarg.h>#include "x264.h"#include "common/common.h"#include "common/mc.h"#include "mc.h"#include "ppccommon.h"typedef void (*pf_mc_t)( uint8_t *src, int i_src, uint8_t *dst, int i_dst, int i_height );static const int hpel_ref0[16] = {0,1,1,1,0,1,1,1,2,3,3,3,0,1,1,1};static const int hpel_ref1[16] = {0,0,0,0,2,2,3,2,2,2,3,2,2,2,3,2};static inline int x264_tapfilter( uint8_t *pix, int i_pix_next ){ return pix[-2*i_pix_next] - 5*pix[-1*i_pix_next] + 20*(pix[0] + pix[1*i_pix_next]) - 5*pix[ 2*i_pix_next] + pix[ 3*i_pix_next];}static inline int x264_tapfilter1( uint8_t *pix ){ return pix[-2] - 5*pix[-1] + 20*(pix[0] + pix[1]) - 5*pix[ 2] + pix[ 3];}static inline void x264_pixel_avg2_w4_altivec( uint8_t *dst, int i_dst, uint8_t *src1, int i_src1, uint8_t *src2, int i_height ){ int x, y; for( y = 0; y < i_height; y++ ) { for( x = 0; x < 4; x++ ) { dst[x] = ( src1[x] + src2[x] + 1 ) >> 1; } dst += i_dst; src1 += i_src1; src2 += i_src1; }}static inline void x264_pixel_avg2_w8_altivec( uint8_t *dst, int i_dst, uint8_t *src1, int i_src1, uint8_t *src2, int i_height ){ int y; vec_u8_t src1v, src2v; PREP_LOAD; PREP_STORE8; PREP_LOAD_SRC( src1 ); PREP_LOAD_SRC( src2 ); for( y = 0; y < i_height; y++ ) { VEC_LOAD( src1, src1v, 8, vec_u8_t, src1 ); VEC_LOAD( src2, src2v, 8, vec_u8_t, src2 ); src1v = vec_avg( src1v, src2v ); VEC_STORE8( src1v, dst ); dst += i_dst; src1 += i_src1; src2 += i_src1; }}static inline void x264_pixel_avg2_w16_altivec( uint8_t *dst, int i_dst, uint8_t *src1, int i_src1, uint8_t *src2, int i_height ){ int y; vec_u8_t src1v, src2v; PREP_LOAD; PREP_LOAD_SRC( src1 ); PREP_LOAD_SRC( src2 ); for( y = 0; y < i_height; y++ ) { VEC_LOAD( src1, src1v, 16, vec_u8_t, src1 ); VEC_LOAD( src2, src2v, 16, vec_u8_t, src2 ); src1v = vec_avg( src1v, src2v ); vec_st(src1v, 0, dst); dst += i_dst; src1 += i_src1; src2 += i_src1; }}static inline void x264_pixel_avg2_w20_altivec( uint8_t *dst, int i_dst, uint8_t *src1, int i_src1, uint8_t *src2, int i_height ){ x264_pixel_avg2_w16_altivec(dst, i_dst, src1, i_src1, src2, i_height); x264_pixel_avg2_w4_altivec(dst+16, i_dst, src1+16, i_src1, src2+16, i_height);}/* mc_copy: plain c */#define MC_COPY( name, a ) \static void name( uint8_t *dst, int i_dst, \ uint8_t *src, int i_src, int i_height ) \{ \ int y; \ for( y = 0; y < i_height; y++ ) \ { \ memcpy( dst, src, a ); \ src += i_src; \ dst += i_dst; \ } \}MC_COPY( x264_mc_copy_w4_altivec, 4 )MC_COPY( x264_mc_copy_w8_altivec, 8 )static void x264_mc_copy_w16_altivec( uint8_t *dst, int i_dst, uint8_t *src, int i_src, int i_height ){ int y; vec_u8_t cpyV; PREP_LOAD; PREP_LOAD_SRC( src ); for( y = 0; y < i_height; y++) { VEC_LOAD( src, cpyV, 16, vec_u8_t, src ); vec_st(cpyV, 0, dst); src += i_src; dst += i_dst; }}static void x264_mc_copy_w16_aligned_altivec( uint8_t *dst, int i_dst, uint8_t *src, int i_src, int i_height ){ int y; for( y = 0; y < i_height; ++y) { vec_u8_t cpyV = vec_ld( 0, src); vec_st(cpyV, 0, dst); src += i_src; dst += i_dst; }}static void mc_luma_altivec( uint8_t *dst, int i_dst_stride, uint8_t *src[4], int i_src_stride, int mvx, int mvy, int i_width, int i_height ){ int qpel_idx = ((mvy&3)<<2) + (mvx&3); int offset = (mvy>>2)*i_src_stride + (mvx>>2); uint8_t *src1 = src[hpel_ref0[qpel_idx]] + offset + ((mvy&3) == 3) * i_src_stride; if( qpel_idx & 5 ) /* qpel interpolation needed */ { uint8_t *src2 = src[hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3); switch(i_width) { case 4: x264_pixel_avg2_w4_altivec( dst, i_dst_stride, src1, i_src_stride, src2, i_height ); break; case 8: x264_pixel_avg2_w8_altivec( dst, i_dst_stride, src1, i_src_stride, src2, i_height ); break; case 16: default: x264_pixel_avg2_w16_altivec( dst, i_dst_stride, src1, i_src_stride, src2, i_height ); } } else { switch(i_width) { case 4: x264_mc_copy_w4_altivec( dst, i_dst_stride, src1, i_src_stride, i_height ); break; case 8: x264_mc_copy_w8_altivec( dst, i_dst_stride, src1, i_src_stride, i_height ); break; case 16: x264_mc_copy_w16_altivec( dst, i_dst_stride, src1, i_src_stride, i_height ); break; } }}static uint8_t *get_ref_altivec( uint8_t *dst, int *i_dst_stride, uint8_t *src[4], int i_src_stride, int mvx, int mvy, int i_width, int i_height ){ int qpel_idx = ((mvy&3)<<2) + (mvx&3); int offset = (mvy>>2)*i_src_stride + (mvx>>2); uint8_t *src1 = src[hpel_ref0[qpel_idx]] + offset + ((mvy&3) == 3) * i_src_stride; if( qpel_idx & 5 ) /* qpel interpolation needed */ { uint8_t *src2 = src[hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3); switch(i_width) { case 4: x264_pixel_avg2_w4_altivec( dst, *i_dst_stride, src1, i_src_stride, src2, i_height ); break; case 8: x264_pixel_avg2_w8_altivec( dst, *i_dst_stride, src1, i_src_stride, src2, i_height ); break; case 12: case 16: default: x264_pixel_avg2_w16_altivec( dst, *i_dst_stride, src1, i_src_stride, src2, i_height ); break; case 20: x264_pixel_avg2_w20_altivec( dst, *i_dst_stride, src1, i_src_stride, src2, i_height ); break; } return dst; } else { *i_dst_stride = i_src_stride; return src1; }}static void mc_chroma_2xh( uint8_t *dst, int i_dst_stride, uint8_t *src, int i_src_stride, int mvx, int mvy, int i_height ){ uint8_t *srcp; int y; int d8x = mvx&0x07; int d8y = mvy&0x07; const int cA = (8-d8x)*(8-d8y); const int cB = d8x *(8-d8y); const int cC = (8-d8x)*d8y; const int cD = d8x *d8y; src += (mvy >> 3) * i_src_stride + (mvx >> 3); srcp = &src[i_src_stride]; for( y = 0; y < i_height; y++ ) { dst[0] = ( cA*src[0] + cB*src[0+1] + cC*srcp[0] + cD*srcp[0+1] + 32 ) >> 6; dst[1] = ( cA*src[1] + cB*src[1+1] + cC*srcp[1] + cD*srcp[1+1] + 32 ) >> 6; src += i_src_stride; srcp += i_src_stride; dst += i_dst_stride; } }#define DO_PROCESS_W4( a ) \ dstv_16A = vec_mladd( src##a##v_16A, coeff##a##v, dstv_16A ); \ dstv_16B = vec_mladd( src##a##v_16B, coeff##a##v, dstv_16B )static void mc_chroma_altivec_4xh( uint8_t *dst, int i_dst_stride, uint8_t *src, int i_src_stride, int mvx, int mvy, int i_height ){ uint8_t *srcp; int y; int d8x = mvx & 0x07; int d8y = mvy & 0x07; DECLARE_ALIGNED_16( uint16_t coeff[4] ); coeff[0] = (8-d8x)*(8-d8y); coeff[1] = d8x *(8-d8y); coeff[2] = (8-d8x)*d8y; coeff[3] = d8x *d8y; src += (mvy >> 3) * i_src_stride + (mvx >> 3); srcp = &src[i_src_stride]; LOAD_ZERO; PREP_LOAD; PREP_LOAD_SRC( src ); vec_u16_t coeff0v, coeff1v, coeff2v, coeff3v; vec_u8_t src2v_8A, dstv_8A; vec_u8_t src2v_8B, dstv_8B; vec_u16_t src0v_16A, src1v_16A, src2v_16A, src3v_16A, dstv_16A; vec_u16_t src0v_16B, src1v_16B, src2v_16B, src3v_16B, dstv_16B; vec_u16_t shiftv, k32v; coeff0v = vec_ld( 0, coeff ); coeff3v = vec_splat( coeff0v, 3 ); coeff2v = vec_splat( coeff0v, 2 ); coeff1v = vec_splat( coeff0v, 1 ); coeff0v = vec_splat( coeff0v, 0 ); k32v = vec_sl( vec_splat_u16( 1 ), vec_splat_u16( 5 ) ); shiftv = vec_splat_u16( 6 ); VEC_LOAD( src, src2v_8B, 5, vec_u8_t, src ); src2v_16B = vec_u8_to_u16( src2v_8B ); src3v_16B = vec_sld( src2v_16B, src2v_16B, 2 ); for( y = 0; y < i_height; y+=2 ) { src0v_16A = src2v_16B; src1v_16A = src3v_16B; VEC_LOAD_G( srcp, src2v_8A, 5, vec_u8_t ); srcp += i_src_stride; VEC_LOAD_G( srcp, src2v_8B, 5, vec_u8_t ); srcp += i_src_stride; src2v_16A = vec_u8_to_u16( src2v_8A ); src2v_16B = vec_u8_to_u16( src2v_8B ); src3v_16A = vec_sld( src2v_16A, src2v_16A, 2 ); src3v_16B = vec_sld( src2v_16B, src2v_16B, 2 ); src0v_16B = src2v_16A; src1v_16B = src3v_16A; dstv_16A = dstv_16B = k32v; DO_PROCESS_W4( 0 ); DO_PROCESS_W4( 1 ); DO_PROCESS_W4( 2 ); DO_PROCESS_W4( 3 ); dstv_16A = vec_sr( dstv_16A, shiftv ); dstv_16B = vec_sr( dstv_16B, shiftv ); dstv_8A = vec_u16_to_u8( dstv_16A );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -