From 8614594835ce25879c0d01ca88625ea444d577f2 Mon Sep 17 00:00:00 2001 From: Loren Merritt Date: Sun, 5 Jun 2005 11:07:28 +0000 Subject: [PATCH] SATD-based decision for 8x8 transform in inter-MBs. Enable 8x8 intra. CLI options: --8x8dct, --analyse i8x8. git-svn-id: svn://svn.videolan.org/x264/trunk@251 df754926-b1dd-0310-bc7b-ec298dee348c --- common/common.c | 3 +- common/common.h | 6 ++- common/dct.c | 34 +++++++-------- common/macroblock.c | 21 +++++----- common/macroblock.h | 7 +++- common/pixel.c | 97 ++++++++++++++++++++++++++++++++++++++++--- common/pixel.h | 11 +++-- common/predict.c | 38 ++++++++--------- encoder/analyse.c | 39 ++++++++++++++++- encoder/cabac.c | 10 ++--- encoder/cavlc.c | 58 ++++++++++++-------------- encoder/encoder.c | 15 ++++--- encoder/macroblock.c | 23 +++++----- encoder/me.c | 2 +- encoder/ratecontrol.c | 10 +++-- x264.c | 17 +++++--- 16 files changed, 259 insertions(+), 132 deletions(-) diff --git a/common/common.c b/common/common.c index 20b1c2bb..58cf2feb 100644 --- a/common/common.c +++ b/common/common.c @@ -105,7 +105,8 @@ void x264_param_default( x264_param_t *param ) /* */ param->analyse.intra = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8; - param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_BSUB16x16; + param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8 + | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_BSUB16x16; param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL; param->analyse.i_me_method = X264_ME_HEX; param->analyse.i_me_range = 16; diff --git a/common/common.h b/common/common.h index 790bf747..e748116c 100644 --- a/common/common.h +++ b/common/common.h @@ -398,8 +398,9 @@ struct x264_t int16_t direct_mv[2][X264_SCAN8_SIZE][2]; int8_t direct_ref[2][X264_SCAN8_SIZE]; - /* top and left neighbors. 1=>8x8, 0=>4x4 */ - int8_t transform_size[2]; + /* number of neighbors (top and left) that used 8x8 dct */ + int i_neighbour_transform_size; + int b_transform_8x8_allowed; } cache; /* */ @@ -437,6 +438,7 @@ struct x264_t int i_misc_bits; /* MB type counts */ int i_mb_count[19]; + int i_mb_count_i; int i_mb_count_p; int i_mb_count_skip; /* Estimated (SATD) cost as Intra/Predicted frame */ diff --git a/common/dct.c b/common/dct.c index b5bc95b7..27d3a2d0 100644 --- a/common/dct.c +++ b/common/dct.c @@ -135,22 +135,28 @@ static void idct4x4dc( int16_t d[4][4] ) } } -static void sub4x4_dct( int16_t dct[4][4], uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2 ) +static inline void pixel_sub_wxh( int16_t *diff, int i_size, + uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2 ) { - int16_t d[4][4]; - int16_t tmp[4][4]; int y, x; - int i; - - for( y = 0; y < 4; y++ ) + for( y = 0; y < i_size; y++ ) { - for( x = 0; x < 4; x++ ) + for( x = 0; x < i_size; x++ ) { - d[y][x] = pix1[x] - pix2[x]; + diff[x + y*i_size] = pix1[x] - pix2[x]; } pix1 += i_pix1; pix2 += i_pix2; } +} + +static void sub4x4_dct( int16_t dct[4][4], uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2 ) +{ + int16_t d[4][4]; + int16_t tmp[4][4]; + int i; + + pixel_sub_wxh( (int16_t*)d, 4, pix1, i_pix1, pix2, i_pix2 ); for( i = 0; i < 4; i++ ) { @@ -289,17 +295,9 @@ static void add16x16_idct( uint8_t *p_dst, int i_dst, int16_t dct[16][4][4] ) static void sub8x8_dct8( int16_t dct[8][8], uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2 ) { - int y, x, i; + int i; - for( y = 0; y < 8; y++ ) - { - for( x = 0; x < 8; x++ ) - { - dct[y][x] = pix1[x] - pix2[x]; - } - pix1 += i_pix1; - pix2 += i_pix2; - } + pixel_sub_wxh( (int16_t*)dct, 8, pix1, i_pix1, pix2, i_pix2 ); #define SRC(x) dct[i][x] for( i = 0; i < 8; i++ ) diff --git a/common/macroblock.c b/common/macroblock.c index 49804e58..03cf373b 100644 --- a/common/macroblock.c +++ b/common/macroblock.c @@ -140,11 +140,13 @@ int x264_mb_predict_non_zero_code( x264_t *h, int idx ) return i_ret & 0x7f; } -int x264_mb_transform_8x8_allowed( x264_t *h, int i_mb_type ) +int x264_mb_transform_8x8_allowed( x264_t *h ) { - int i; - if( i_mb_type == P_8x8 || i_mb_type == B_8x8 ) + if( IS_SKIP( h->mb.i_type ) ) + return 0; + if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 ) { + int i; for( i = 0; i < 4; i++ ) if( !IS_SUB8x8(h->mb.i_sub_partition[i]) || ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 && !h->sps->b_direct8x8_inference ) ) @@ -152,7 +154,7 @@ int x264_mb_transform_8x8_allowed( x264_t *h, int i_mb_type ) return 0; } } - if( i_mb_type == B_DIRECT && !h->sps->b_direct8x8_inference ) + if( h->mb.i_type == B_DIRECT && !h->sps->b_direct8x8_inference ) return 0; return 1; @@ -1178,10 +1180,9 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y ) if( h->param.analyse.b_transform_8x8 ) { - h->mb.cache.transform_size[0] = (h->mb.i_neighbour&MB_LEFT) - && h->mb.mb_transform_size[i_left_xy]; - h->mb.cache.transform_size[1] = (h->mb.i_neighbour&MB_TOP) - && h->mb.mb_transform_size[i_top_xy]; + h->mb.cache.i_neighbour_transform_size = + ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] ) + + ( i_top_type >= 0 && h->mb.mb_transform_size[i_top_xy] ); } /* load ref/mv/mvd */ @@ -1352,8 +1353,6 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y ) } } - // FIXME skip this if I_4x4 and I_8x8 are disabled? - // assumes MB_TOPRIGHT = MB_TOP<<1 h->mb.i_neighbour4[0] = h->mb.i_neighbour8[0] = (h->mb.i_neighbour & (MB_TOP|MB_LEFT|MB_TOPLEFT)) | ((h->mb.i_neighbour & MB_TOP) ? MB_TOPRIGHT : 0); @@ -1389,6 +1388,8 @@ void x264_macroblock_cache_save( x264_t *h ) int i; + h->mb.type[i_mb_xy] = i_mb_type; + if( IS_SKIP( h->mb.i_type ) ) h->mb.qp[i_mb_xy] = h->mb.i_last_qp; diff --git a/common/macroblock.h b/common/macroblock.h index a3b55271..bbbed8f3 100644 --- a/common/macroblock.h +++ b/common/macroblock.h @@ -201,7 +201,12 @@ void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[5][2 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx ); int x264_mb_predict_non_zero_code( x264_t *h, int idx ); -int x264_mb_transform_8x8_allowed( x264_t *h, int i_mb_type ); + +/* x264_mb_transform_8x8_allowed: + * check whether any partition is smaller than 8x8 (or at least + * might be, according to just partition type.) + * doesn't check for intra or cbp */ +int x264_mb_transform_8x8_allowed( x264_t *h ); void x264_mb_encode_i4x4( x264_t *h, int idx, int i_qscale ); void x264_mb_encode_i8x8( x264_t *h, int idx, int i_qscale ); diff --git a/common/pixel.c b/common/pixel.c index fa3fbac4..ff38a73f 100644 --- a/common/pixel.c +++ b/common/pixel.c @@ -104,20 +104,25 @@ PIXEL_SSD_C( pixel_ssd_4x8, 4, 8 ) PIXEL_SSD_C( pixel_ssd_4x4, 4, 4 ) -static void pixel_sub_4x4( int16_t diff[4][4], uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2 ) +static inline void pixel_sub_wxh( int16_t *diff, int i_size, + uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2 ) { int y, x; - for( y = 0; y < 4; y++ ) + for( y = 0; y < i_size; y++ ) { - for( x = 0; x < 4; x++ ) + for( x = 0; x < i_size; x++ ) { - diff[y][x] = pix1[x] - pix2[x]; + diff[x + y*i_size] = pix1[x] - pix2[x]; } pix1 += i_pix1; pix2 += i_pix2; } } + +/**************************************************************************** + * pixel_satd_WxH: sum of 4x4 Hadamard transformed differences + ****************************************************************************/ static int pixel_satd_wxh( uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2, int i_width, int i_height ) { int16_t tmp[4][4]; @@ -131,7 +136,7 @@ static int pixel_satd_wxh( uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2, { int d; - pixel_sub_4x4( diff, &pix1[x], i_pix1, &pix2[x], i_pix2 ); + pixel_sub_wxh( (int16_t*)diff, 4, &pix1[x], i_pix1, &pix2[x], i_pix2 ); for( d = 0; d < 4; d++ ) { @@ -179,6 +184,83 @@ PIXEL_SATD_C( pixel_satd_4x8, 4, 8 ) PIXEL_SATD_C( pixel_satd_4x4, 4, 4 ) +/**************************************************************************** + * pixel_sa8d_WxH: sum of 8x8 Hadamard transformed differences + ****************************************************************************/ +#define SA8D_1D {\ + const int a0 = SRC(0) + SRC(4);\ + const int a4 = SRC(0) - SRC(4);\ + const int a1 = SRC(1) + SRC(5);\ + const int a5 = SRC(1) - SRC(5);\ + const int a2 = SRC(2) + SRC(6);\ + const int a6 = SRC(2) - SRC(6);\ + const int a3 = SRC(3) + SRC(7);\ + const int a7 = SRC(3) - SRC(7);\ + const int b0 = a0 + a2;\ + const int b2 = a0 - a2;\ + const int b1 = a1 + a3;\ + const int b3 = a1 - a3;\ + const int b4 = a4 + a6;\ + const int b6 = a4 - a6;\ + const int b5 = a5 + a7;\ + const int b7 = a5 - a7;\ + DST(0, b0 + b1);\ + DST(1, b0 - b1);\ + DST(2, b2 + b3);\ + DST(3, b2 - b3);\ + DST(4, b4 + b5);\ + DST(5, b4 - b5);\ + DST(6, b6 + b7);\ + DST(7, b6 - b7);\ +} + +static inline int pixel_sa8d_wxh( uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2, + int i_width, int i_height ) +{ + int16_t diff[8][8]; + int i_satd = 0; + int x, y; + + for( y = 0; y < i_height; y += 8 ) + { + for( x = 0; x < i_width; x += 8 ) + { + int i; + pixel_sub_wxh( (int16_t*)diff, 8, pix1+x, i_pix1, pix2+x, i_pix2 ); + +#define SRC(x) diff[i][x] +#define DST(x,rhs) diff[i][x] = (rhs) + for( i = 0; i < 8; i++ ) + SA8D_1D +#undef SRC +#undef DST + +#define SRC(x) diff[x][i] +#define DST(x,rhs) i_satd += abs(rhs) + for( i = 0; i < 8; i++ ) + SA8D_1D +#undef SRC +#undef DST + } + pix1 += 8 * i_pix1; + pix2 += 8 * i_pix2; + } + + return i_satd; +} + +#define PIXEL_SA8D_C( width, height ) \ +static int pixel_sa8d_##width##x##height( uint8_t *pix1, int i_stride_pix1, \ + uint8_t *pix2, int i_stride_pix2 ) \ +{ \ + return ( pixel_sa8d_wxh( pix1, i_stride_pix1, pix2, i_stride_pix2, width, height ) + 2 ) >> 2; \ +} +PIXEL_SA8D_C( 16, 16 ) +PIXEL_SA8D_C( 16, 8 ) +PIXEL_SA8D_C( 8, 16 ) +PIXEL_SA8D_C( 8, 8 ) + + static inline void pixel_avg_wxh( uint8_t *dst, int i_dst, uint8_t *src, int i_src, int width, int height ) { int x, y; @@ -291,6 +373,11 @@ void x264_pixel_init( int cpu, x264_pixel_function_t *pixf ) pixf->satd[PIXEL_4x8] = pixel_satd_4x8; pixf->satd[PIXEL_4x4] = pixel_satd_4x4; + pixf->sa8d[PIXEL_16x16]= pixel_sa8d_16x16; + pixf->sa8d[PIXEL_16x8] = pixel_sa8d_16x8; + pixf->sa8d[PIXEL_8x16] = pixel_sa8d_8x16; + pixf->sa8d[PIXEL_8x8] = pixel_sa8d_8x8; + pixf->avg[PIXEL_16x16]= pixel_avg_16x16; pixf->avg[PIXEL_16x8] = pixel_avg_16x8; pixf->avg[PIXEL_8x16] = pixel_avg_8x16; diff --git a/common/pixel.h b/common/pixel.h index 20166969..87be5cd2 100644 --- a/common/pixel.h +++ b/common/pixel.h @@ -24,9 +24,7 @@ #ifndef _PIXEL_H #define _PIXEL_H 1 -typedef int (*x264_pixel_sad_t) ( uint8_t *, int, uint8_t *, int ); -typedef int (*x264_pixel_ssd_t) ( uint8_t *, int, uint8_t *, int ); -typedef int (*x264_pixel_satd_t)( uint8_t *, int, uint8_t *, int ); +typedef int (*x264_pixel_cmp_t) ( uint8_t *, int, uint8_t *, int ); typedef void (*x264_pixel_avg_t) ( uint8_t *, int, uint8_t *, int ); typedef void (*x264_pixel_avg_weight_t) ( uint8_t *, int, uint8_t *, int, int ); @@ -65,9 +63,10 @@ static const int x264_size2pixel[5][5] = { typedef struct { - x264_pixel_sad_t sad[7]; - x264_pixel_ssd_t ssd[7]; - x264_pixel_satd_t satd[7]; + x264_pixel_cmp_t sad[7]; + x264_pixel_cmp_t ssd[7]; + x264_pixel_cmp_t satd[7]; + x264_pixel_cmp_t sa8d[4]; x264_pixel_avg_t avg[10]; x264_pixel_avg_weight_t avg_weight[10]; } x264_pixel_function_t; diff --git a/common/predict.c b/common/predict.c index 5ce34e27..9dc3115e 100644 --- a/common/predict.c +++ b/common/predict.c @@ -686,27 +686,27 @@ static void predict_8x8_dc_128( uint8_t *src, int i_stride, int i_neighbor ) } static void predict_8x8_dc_left( uint8_t *src, int i_stride, int i_neighbor ) { - PREDICT_8x8_LOAD_LEFT; + PREDICT_8x8_LOAD_LEFT const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101; PREDICT_8x8_DC(dc); } static void predict_8x8_dc_top( uint8_t *src, int i_stride, int i_neighbor ) { - PREDICT_8x8_LOAD_TOP; + PREDICT_8x8_LOAD_TOP const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101; PREDICT_8x8_DC(dc); } static void predict_8x8_dc( uint8_t *src, int i_stride, int i_neighbor ) { - PREDICT_8x8_LOAD_LEFT; - PREDICT_8x8_LOAD_TOP; + PREDICT_8x8_LOAD_LEFT + PREDICT_8x8_LOAD_TOP const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7 +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101; PREDICT_8x8_DC(dc); } static void predict_8x8_h( uint8_t *src, int i_stride, int i_neighbor ) { - PREDICT_8x8_LOAD_LEFT; + PREDICT_8x8_LOAD_LEFT #define ROW(y) ((uint32_t*)(src+y*i_stride))[0] =\ ((uint32_t*)(src+y*i_stride))[1] = 0x01010101U * l##y ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7); @@ -729,8 +729,8 @@ static void predict_8x8_v( uint8_t *src, int i_stride, int i_neighbor ) } static void predict_8x8_ddl( uint8_t *src, int i_stride, int i_neighbor ) { - PREDICT_8x8_LOAD_TOP; - PREDICT_8x8_LOAD_TOPRIGHT; + PREDICT_8x8_LOAD_TOP + PREDICT_8x8_LOAD_TOPRIGHT SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2; SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2; SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2; @@ -749,9 +749,9 @@ static void predict_8x8_ddl( uint8_t *src, int i_stride, int i_neighbor ) } static void predict_8x8_ddr( uint8_t *src, int i_stride, int i_neighbor ) { - PREDICT_8x8_LOAD_TOP; - PREDICT_8x8_LOAD_LEFT; - PREDICT_8x8_LOAD_TOPLEFT; + PREDICT_8x8_LOAD_TOP + PREDICT_8x8_LOAD_LEFT + PREDICT_8x8_LOAD_TOPLEFT SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2; SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2; SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2; @@ -771,9 +771,9 @@ static void predict_8x8_ddr( uint8_t *src, int i_stride, int i_neighbor ) } static void predict_8x8_vr( uint8_t *src, int i_stride, int i_neighbor ) { - PREDICT_8x8_LOAD_TOP; - PREDICT_8x8_LOAD_LEFT; - PREDICT_8x8_LOAD_TOPLEFT; + PREDICT_8x8_LOAD_TOP + PREDICT_8x8_LOAD_LEFT + PREDICT_8x8_LOAD_TOPLEFT /* produce warning as l7 is unused */ SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2; SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2; @@ -800,9 +800,9 @@ static void predict_8x8_vr( uint8_t *src, int i_stride, int i_neighbor ) } static void predict_8x8_hd( uint8_t *src, int i_stride, int i_neighbor ) { - PREDICT_8x8_LOAD_TOP; - PREDICT_8x8_LOAD_LEFT; - PREDICT_8x8_LOAD_TOPLEFT; + PREDICT_8x8_LOAD_TOP + PREDICT_8x8_LOAD_LEFT + PREDICT_8x8_LOAD_TOPLEFT /* produce warning as t7 is unused */ SRC(0,7)= (l6 + l7 + 1) >> 1; SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2; @@ -829,8 +829,8 @@ static void predict_8x8_hd( uint8_t *src, int i_stride, int i_neighbor ) } static void predict_8x8_vl( uint8_t *src, int i_stride, int i_neighbor ) { - PREDICT_8x8_LOAD_TOP; - PREDICT_8x8_LOAD_TOPRIGHT; + PREDICT_8x8_LOAD_TOP + PREDICT_8x8_LOAD_TOPRIGHT SRC(0,0)= (t0 + t1 + 1) >> 1; SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2; SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1; @@ -856,7 +856,7 @@ static void predict_8x8_vl( uint8_t *src, int i_stride, int i_neighbor ) } static void predict_8x8_hu( uint8_t *src, int i_stride, int i_neighbor ) { - PREDICT_8x8_LOAD_LEFT; + PREDICT_8x8_LOAD_LEFT SRC(0,0)= (l0 + l1 + 1) >> 1; SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2; SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1; diff --git a/encoder/analyse.c b/encoder/analyse.c index 99238651..b3be49dc 100644 --- a/encoder/analyse.c +++ b/encoder/analyse.c @@ -514,6 +514,7 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *res, int i_cos i_mode = predict_mode[i]; h->predict_8x8[i_mode]( p_dst_by, i_stride, h->mb.i_neighbour ); + /* could use sa8d, but it doesn't seem worth the speed cost (without mmx at least) */ i_sad = h->pixf.satd[PIXEL_8x8]( p_dst_by, i_stride, p_src_by, i_stride ); @@ -533,7 +534,7 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *res, int i_cos x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, res->i_predict4x4[x][y] ); } -// res->i_sad_i8x8 += res->i_lambda * something; // FIXME + // FIXME some bias like in i4x4? if( h->sh.i_type == SLICE_TYPE_B ) res->i_sad_i8x8 += res->i_lambda * i_mb_b_cost_table[I_8x8]; } @@ -1342,6 +1343,29 @@ static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a ) a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16]; } +static inline void x264_mb_analyse_transform( x264_t *h ) +{ + if( h->param.analyse.b_transform_8x8 + && !IS_INTRA( h->mb.i_type ) + && x264_mb_transform_8x8_allowed( h ) ) + { + int i_cost4, i_cost8; + + /* FIXME only luma mc is needed */ + x264_mb_mc( h ); + + i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0], + h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] ); + i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0], + h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] ); + h->mb.b_transform_8x8 = i_cost8 < i_cost4; + h->mb.cache.b_transform_8x8_allowed = 1; + } + else + h->mb.cache.b_transform_8x8_allowed = 0; +} + + /***************************************************************************** * x264_macroblock_analyse: *****************************************************************************/ @@ -1552,6 +1576,11 @@ void x264_macroblock_analyse( x264_t *h ) i_intra_type = I_16x16; i_intra_cost = analysis.i_sad_i16x16; + if( analysis.i_sad_i8x8 < i_intra_cost ) + { + i_intra_type = I_8x8; + i_intra_cost = analysis.i_sad_i8x8; + } if( analysis.i_sad_i4x4 < i_intra_cost ) { i_intra_type = I_4x4; @@ -1740,6 +1769,11 @@ void x264_macroblock_analyse( x264_t *h ) h->mb.i_type = I_16x16; i_cost = analysis.i_sad_i16x16; } + if( analysis.i_sad_i8x8 < i_cost ) + { + h->mb.i_type = I_8x8; + i_cost = analysis.i_sad_i8x8; + } if( analysis.i_sad_i4x4 < i_cost ) { h->mb.i_type = I_4x4; @@ -1749,7 +1783,6 @@ void x264_macroblock_analyse( x264_t *h ) } /*-------------------- Update MB from the analysis ----------------------*/ - h->mb.type[h->mb.i_mb_xy] = x264_mb_type_fix[h->mb.i_type]; switch( h->mb.i_type ) { case I_4x4: @@ -1902,6 +1935,8 @@ void x264_macroblock_analyse( x264_t *h ) break; } } + + x264_mb_analyse_transform( h ); } #include "slicetype_decision.c" diff --git a/encoder/cabac.c b/encoder/cabac.c index 257191f0..d3a0c180 100644 --- a/encoder/cabac.c +++ b/encoder/cabac.c @@ -78,7 +78,7 @@ static inline void x264_cabac_mb_type_intra( x264_t *h, int i_mb_type, static void x264_cabac_mb_type( x264_t *h ) { - const int i_mb_type = x264_mb_type_fix[h->mb.i_type]; + const int i_mb_type = h->mb.i_type; if( h->sh.i_type == SLICE_TYPE_I ) { @@ -557,9 +557,8 @@ static inline void x264_cabac_mb_sub_b_partition( x264_t *h, int i_sub ) static inline void x264_cabac_mb_transform_size( x264_t *h ) { - int ctx = ( h->mb.cache.transform_size[0] == 1 ) - + ( h->mb.cache.transform_size[1] == 1 ); - x264_cabac_encode_decision( &h->cabac, 399 + ctx, h->mb.b_transform_8x8 ); + int ctx = 399 + h->mb.cache.i_neighbour_transform_size; + x264_cabac_encode_decision( &h->cabac, ctx, h->mb.b_transform_8x8 ); } static inline void x264_cabac_mb_ref( x264_t *h, int i_list, int idx ) @@ -1154,8 +1153,7 @@ void x264_macroblock_write_cabac( x264_t *h, bs_t *s ) x264_cabac_mb_cbp_chroma( h ); } - if( h->pps->b_transform_8x8_mode && h->mb.i_cbp_luma && !IS_INTRA(i_mb_type) - && x264_mb_transform_8x8_allowed( h, i_mb_type ) ) + if( h->mb.cache.b_transform_8x8_allowed && h->mb.i_cbp_luma && !IS_INTRA(i_mb_type) ) { x264_cabac_mb_transform_size( h ); } diff --git a/encoder/cavlc.c b/encoder/cavlc.c index 11a542eb..31cbd906 100644 --- a/encoder/cavlc.c +++ b/encoder/cavlc.c @@ -337,6 +337,29 @@ static void x264_sub_mb_mv_write_cavlc( x264_t *h, bs_t *s, int i_list ) } } +void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s ) +{ + int i8, i4, i; + if( h->mb.b_transform_8x8 ) + { + /* shuffle 8x8 dct coeffs into 4x4 lists */ + for( i8 = 0; i8 < 4; i8++ ) + if( h->mb.i_cbp_luma & (1 << i8) ) + for( i4 = 0; i4 < 4; i4++ ) + { + for( i = 0; i < 16; i++ ) + h->dct.block[i4+i8*4].luma4x4[i] = h->dct.luma8x8[i8][i4+i*4]; + h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] = + array_non_zero_count( h->dct.block[i4+i8*4].luma4x4, 16 ); + } + } + + for( i8 = 0; i8 < 4; i8++ ) + if( h->mb.i_cbp_luma & (1 << i8) ) + for( i4 = 0; i4 < 4; i4++ ) + block_residual_write_cavlc( h, s, i4+i8*4, h->dct.block[i4+i8*4].luma4x4, 16 ); +} + /***************************************************************************** * x264_macroblock_write: *****************************************************************************/ @@ -653,8 +676,7 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) } /* transform size 8x8 flag */ - if( h->pps->b_transform_8x8_mode && h->mb.i_cbp_luma && !IS_INTRA(i_mb_type) - && x264_mb_transform_8x8_allowed( h, i_mb_type ) ) + if( h->mb.cache.b_transform_8x8_allowed && h->mb.i_cbp_luma && !IS_INTRA(i_mb_type) ) { bs_write1( s, h->mb.b_transform_8x8 ); } @@ -667,39 +689,15 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) /* DC Luma */ block_residual_write_cavlc( h, s, BLOCK_INDEX_LUMA_DC , h->dct.luma16x16_dc, 16 ); + /* AC Luma */ if( h->mb.i_cbp_luma != 0 ) - { - /* AC Luma */ for( i = 0; i < 16; i++ ) - { block_residual_write_cavlc( h, s, i, h->dct.block[i].residual_ac, 15 ); - } - } } else if( h->mb.i_cbp_luma != 0 || h->mb.i_cbp_chroma != 0 ) { bs_write_se( s, h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp ); - - /* shuffle 8x8 dct coeffs into 4x4 lists */ - if( h->mb.b_transform_8x8 ) - { - int i4; - for( i4 = 0; i4 < 16; i4++ ) - { - for( i = 0; i < 16; i++ ) - h->dct.block[i4].luma4x4[i] = h->dct.luma8x8[i4>>2][(i4&3)+i*4]; - h->mb.cache.non_zero_count[x264_scan8[i4]] = - array_non_zero_count( h->dct.block[i4].luma4x4, 16 ); - } - } - - for( i = 0; i < 16; i++ ) - { - if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) ) - { - block_residual_write_cavlc( h, s, i, h->dct.block[i].luma4x4, 16 ); - } - } + x264_macroblock_luma_write_cavlc( h, s ); } if( h->mb.i_cbp_chroma != 0 ) { @@ -707,12 +705,8 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 ); block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 ); if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */ - { for( i = 0; i < 8; i++ ) - { block_residual_write_cavlc( h, s, 16 + i, h->dct.block[16+i].residual_ac, 15 ); - } - } } if( IS_INTRA( i_mb_type ) ) diff --git a/encoder/encoder.c b/encoder/encoder.c index d997c3c9..f1e0373f 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -1321,9 +1321,10 @@ do_encode: if( i_slice_type == SLICE_TYPE_P && !h->param.rc.b_stat_read && h->param.i_scenecut_threshold >= 0 ) { - int i_mb_i = h->stat.frame.i_mb_count[I_4x4] + h->stat.frame.i_mb_count[I_16x16]; - int i_mb_p = h->stat.frame.i_mb_count[P_L0] + h->stat.frame.i_mb_count[P_8x8]; - int i_mb_s = h->stat.frame.i_mb_count[P_SKIP]; + const int *mbs = h->stat.frame.i_mb_count; + int i_mb_i = mbs[I_16x16] + mbs[I_8x8] + mbs[I_4x4]; + int i_mb_p = mbs[P_L0] + mbs[P_8x8]; + int i_mb_s = mbs[P_SKIP]; int i_mb = h->sps->i_mb_width * h->sps->i_mb_height; int64_t i_inter_cost = h->stat.frame.i_inter_cost; int64_t i_intra_cost = h->stat.frame.i_intra_cost; @@ -1361,7 +1362,7 @@ do_encode: { int b; - x264_log( h, X264_LOG_DEBUG, "scene cut at %d size=%d Icost:%.0f Pcost:%.0f ratio:%.3f bias=%.3f lastIDR:%d (I:%d P:%d Skip:%d)\n", + x264_log( h, X264_LOG_DEBUG, "scene cut at %d Icost:%.0f Pcost:%.0f ratio:%.3f bias=%.3f lastIDR:%d (I:%d P:%d S:%d)\n", h->fenc->i_frame, i_frame_size, (double)i_intra_cost, (double)i_inter_cost, (double)i_inter_cost / i_intra_cost, @@ -1500,15 +1501,13 @@ do_encode: } x264_log( h, X264_LOG_DEBUG, - "frame=%4d QP=%i NAL=%d Slice:%c Poc:%-3d I4:%-4d I8:%-4d I16:%-4d P:%-4d SKIP:%-4d size=%d bytes%s\n", + "frame=%4d QP=%i NAL=%d Slice:%c Poc:%-3d I:%-4d P:%-4d SKIP:%-4d size=%d bytes%s\n", h->i_frame - 1, i_global_qp, i_nal_ref_idc, i_slice_type == SLICE_TYPE_I ? 'I' : (i_slice_type == SLICE_TYPE_P ? 'P' : 'B' ), frame_psnr->i_poc, - h->stat.frame.i_mb_count[I_4x4], - h->stat.frame.i_mb_count[I_8x8], - h->stat.frame.i_mb_count[I_16x16], + h->stat.frame.i_mb_count_i, h->stat.frame.i_mb_count_p, h->stat.frame.i_mb_count_skip, i_frame_size, diff --git a/encoder/macroblock.c b/encoder/macroblock.c index d0d2d2cc..6768dac4 100644 --- a/encoder/macroblock.c +++ b/encoder/macroblock.c @@ -830,19 +830,18 @@ void x264_macroblock_encode( x264_t *h ) * XXX: in the me perhaps we should take x264_mb_predict_mv_pskip into account * (if multiple mv give same result)*/ if( h->mb.i_type == P_L0 && h->mb.i_partition == D_16x16 && - h->mb.i_cbp_luma == 0x00 && h->mb.i_cbp_chroma== 0x00 ) + h->mb.i_cbp_luma == 0x00 && h->mb.i_cbp_chroma== 0x00 && + h->mb.cache.ref[0][x264_scan8[0]] == 0 ) { - if( h->mb.cache.ref[0][x264_scan8[0]] == 0 ) - { - int mvp[2]; + int mvp[2]; - x264_mb_predict_mv_pskip( h, mvp ); - if( h->mb.cache.mv[0][x264_scan8[0]][0] == mvp[0] && - h->mb.cache.mv[0][x264_scan8[0]][1] == mvp[1] ) - { - h->mb.type[h->mb.i_mb_xy] = h->mb.i_type = P_SKIP; - h->mb.qp[h->mb.i_mb_xy] = h->mb.i_last_qp; /* Needed */ - } + x264_mb_predict_mv_pskip( h, mvp ); + if( h->mb.cache.mv[0][x264_scan8[0]][0] == mvp[0] && + h->mb.cache.mv[0][x264_scan8[0]][1] == mvp[1] ) + { + h->mb.i_type = P_SKIP; + h->mb.qp[h->mb.i_mb_xy] = h->mb.i_last_qp; /* Needed */ + /* XXX qp reset may have issues when used in RD instead of the real encode*/ } } @@ -850,7 +849,7 @@ void x264_macroblock_encode( x264_t *h ) if( h->mb.i_type == B_DIRECT && h->mb.i_cbp_luma == 0x00 && h->mb.i_cbp_chroma== 0x00 ) { - h->mb.type[h->mb.i_mb_xy] = h->mb.i_type = B_SKIP; + h->mb.i_type = B_SKIP; h->mb.qp[h->mb.i_mb_xy] = h->mb.i_last_qp; /* Needed */ } diff --git a/encoder/me.c b/encoder/me.c index 901f3ec0..147dd159 100644 --- a/encoder/me.c +++ b/encoder/me.c @@ -60,7 +60,7 @@ static void refine_subpel( x264_t *h, x264_me_t *m, int hpel_iters, int qpel_ite void x264_me_search_ref( x264_t *h, x264_me_t *m, int (*mvc)[2], int i_mvc, int *p_fullpel_thresh ) { const int i_pixel = m->i_pixel; - const unsigned int i_me_range = h->param.analyse.i_me_range; + const int i_me_range = h->param.analyse.i_me_range; const int b_chroma_me = h->mb.b_chroma_me && i_pixel <= PIXEL_8x8; int bmx, bmy, bcost; int omx, omy, pmx, pmy; diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c index 3983feda..03606549 100644 --- a/encoder/ratecontrol.c +++ b/encoder/ratecontrol.c @@ -540,14 +540,16 @@ int x264_ratecontrol_slice_type( x264_t *h, int frame_num ) void x264_ratecontrol_end( x264_t *h, int bits ) { x264_ratecontrol_t *rc = h->rc; + const int *mbs = h->stat.frame.i_mb_count; int i; x264_cpu_restore( h->param.cpu ); - h->stat.frame.i_mb_count_skip = h->stat.frame.i_mb_count[P_SKIP] + h->stat.frame.i_mb_count[B_SKIP]; - h->stat.frame.i_mb_count_p = h->stat.frame.i_mb_count[P_L0] + h->stat.frame.i_mb_count[P_8x8]; + h->stat.frame.i_mb_count_skip = mbs[P_SKIP] + mbs[B_SKIP]; + h->stat.frame.i_mb_count_i = mbs[I_16x16] + mbs[I_8x8] + mbs[I_4x4]; + h->stat.frame.i_mb_count_p = mbs[P_L0] + mbs[P_8x8]; for( i = B_DIRECT; i < B_8x8; i++ ) - h->stat.frame.i_mb_count_p += h->stat.frame.i_mb_count[i]; + h->stat.frame.i_mb_count_p += mbs[i]; if( h->param.rc.b_stat_write ) { @@ -560,7 +562,7 @@ void x264_ratecontrol_end( x264_t *h, int bits ) c_type, rc->qpa, h->stat.frame.i_itex_bits, h->stat.frame.i_ptex_bits, h->stat.frame.i_hdr_bits, h->stat.frame.i_misc_bits, - h->stat.frame.i_mb_count[I_4x4] + h->stat.frame.i_mb_count[I_16x16], + h->stat.frame.i_mb_count_i, h->stat.frame.i_mb_count_p, h->stat.frame.i_mb_count_skip); } diff --git a/x264.c b/x264.c index 8494cb71..89e22470 100644 --- a/x264.c +++ b/x264.c @@ -214,9 +214,10 @@ static void Help( x264_param_t *defaults ) "\n" "Analysis:\n" "\n" - " -A, --analyse Analyse options: [\"i4x4,p8x8,b8x8\"]\n" - " - i4x4, p8x8, p4x4, b8x8\n" + " -A, --analyse Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n" + " - p8x8, p4x4, b8x8, i8x8, i4x4\n" " - none, all\n" + " (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n" " --direct Direct MV prediction mode [\"temporal\"]\n" " - none, spatial, temporal\n" " -w, --weightb Weighted prediction for B-frames\n" @@ -228,6 +229,7 @@ static void Help( x264_param_t *defaults ) " --merange Maximum motion vector search range [%d]\n" " -m, --subme Subpixel motion estimation quality: 1=fast, 5=best. [%d]\n" " --no-chroma-me Ignore chroma in motion estimation\n" + " -8, --8x8dct Adaptive spatial transform size\n" "\n" "Input/Output:\n" "\n" @@ -390,6 +392,7 @@ static int Parse( int argc, char **argv, { "merange", required_argument, NULL, OPT_MERANGE }, { "subme", required_argument, NULL, 'm' }, { "no-chroma-me", no_argument, NULL, OPT_NO_CHROMA_ME }, + { "8x8dct", no_argument, NULL, '8' }, { "level", required_argument, NULL, OPT_LEVEL }, { "ratetol", required_argument, NULL, OPT_RATETOL }, { "vbv-maxrate", required_argument, NULL, OPT_VBVMAXRATE }, @@ -417,7 +420,7 @@ static int Parse( int argc, char **argv, int c; - c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:f:o:A:m:p:vw", + c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:f:o:A:m:p:vw8", long_options, &long_options_index); if( c == -1 ) @@ -549,10 +552,11 @@ static int Parse( int argc, char **argv, } case 'A': param->analyse.inter = 0; - if( strstr( optarg, "none" ) ) param->analyse.inter = 0x000000; - if( strstr( optarg, "all" ) ) param->analyse.inter = X264_ANALYSE_I4x4|X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8|X264_ANALYSE_BSUB16x16; + if( strstr( optarg, "none" ) ) param->analyse.inter = 0; + if( strstr( optarg, "all" ) ) param->analyse.inter = ~0; if( strstr( optarg, "i4x4" ) ) param->analyse.inter |= X264_ANALYSE_I4x4; + if( strstr( optarg, "i8x8" ) ) param->analyse.inter |= X264_ANALYSE_I8x8; if( strstr( optarg, "p8x8" ) ) param->analyse.inter |= X264_ANALYSE_PSUB16x16; if( strstr( optarg, "p4x4" ) ) param->analyse.inter |= X264_ANALYSE_PSUB8x8; if( strstr( optarg, "b8x8" ) ) param->analyse.inter |= X264_ANALYSE_BSUB16x16; @@ -591,6 +595,9 @@ static int Parse( int argc, char **argv, case OPT_NO_CHROMA_ME: param->analyse.b_chroma_me = 0; break; + case '8': + param->analyse.b_transform_8x8 = 1; + break; case OPT_LEVEL: param->i_level_idc = atoi(optarg); break; -- 2.47.3