#define RDO_SKIP_BS
+static int cabac_prefix_transition[15][128];
+static int cabac_prefix_size[15][128];
+
/* CAVLC: produces exactly the same bit count as a normal encode */
/* this probably still leaves some unnecessary computations */
#define bs_write1(s,v) ((s)->i_bits_encoded += 1)
/* CABAC: not exactly the same. x264_cabac_size_decision() keeps track of
* fractional bits, but only finite precision. */
#define x264_cabac_encode_decision(c,x,v) x264_cabac_size_decision(c,x,v)
-#define x264_cabac_encode_terminal(c,v) x264_cabac_size_decision(c,276,v)
+#define x264_cabac_encode_terminal(c) x264_cabac_size_decision(c,276,0)
#define x264_cabac_encode_bypass(c,v) ((c)->f8_bits_encoded += 256)
-#define x264_cabac_encode_flush(c)
+#define x264_cabac_encode_flush(h,c)
#define x264_macroblock_write_cabac x264_macroblock_size_cabac
#define x264_cabac_mb_skip x264_cabac_mb_size_skip_unused
#include "cabac.c"
+static int ssd_mb( x264_t *h )
+{
+ return h->pixf.ssd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
+ h->mb.pic.p_fdec[0], FDEC_STRIDE )
+ + h->pixf.ssd[PIXEL_8x8]( h->mb.pic.p_fenc[1], FENC_STRIDE,
+ h->mb.pic.p_fdec[1], FDEC_STRIDE )
+ + h->pixf.ssd[PIXEL_8x8]( h->mb.pic.p_fenc[2], FENC_STRIDE,
+ h->mb.pic.p_fdec[2], FDEC_STRIDE );
+}
+
+static int ssd_plane( x264_t *h, int size, int p, int x, int y )
+{
+ return h->pixf.ssd[size]( h->mb.pic.p_fenc[p] + x+y*FENC_STRIDE, FENC_STRIDE,
+ h->mb.pic.p_fdec[p] + x+y*FDEC_STRIDE, FDEC_STRIDE );
+}
+
static int x264_rd_cost_mb( x264_t *h, int i_lambda2 )
{
int b_transform_bak = h->mb.b_transform_8x8;
x264_macroblock_encode( h );
- i_ssd = h->pixf.ssd[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->pixf.ssd[PIXEL_8x8]( h->mb.pic.p_fenc[1], h->mb.pic.i_stride[1],
- h->mb.pic.p_fdec[1], h->mb.pic.i_stride[1] )
- + h->pixf.ssd[PIXEL_8x8]( h->mb.pic.p_fenc[2], h->mb.pic.i_stride[2],
- h->mb.pic.p_fdec[2], h->mb.pic.i_stride[2] );
+ i_ssd = ssd_mb( h );
if( IS_SKIP( h->mb.i_type ) )
{
return i_ssd + i_bits;
}
+int x264_rd_cost_part( x264_t *h, int i_lambda2, int i8, int i_pixel )
+{
+ int i_ssd, i_bits;
+
+ if( i_pixel == PIXEL_16x16 )
+ {
+ int type_bak = h->mb.i_type;
+ int i_cost = x264_rd_cost_mb( h, i_lambda2 );
+ h->mb.i_type = type_bak;
+ return i_cost;
+ }
+
+ x264_macroblock_encode_p8x8( h, i8 );
+ if( i_pixel == PIXEL_16x8 )
+ x264_macroblock_encode_p8x8( h, i8+1 );
+ if( i_pixel == PIXEL_8x16 )
+ x264_macroblock_encode_p8x8( h, i8+2 );
+
+ i_ssd = ssd_plane( h, i_pixel, 0, (i8&1)*8, (i8>>1)*8 )
+ + ssd_plane( h, i_pixel+3, 1, (i8&1)*4, (i8>>1)*4 )
+ + ssd_plane( h, i_pixel+3, 2, (i8&1)*4, (i8>>1)*4 );
+
+ if( h->param.b_cabac )
+ {
+ x264_cabac_t cabac_tmp = h->cabac;
+ cabac_tmp.f8_bits_encoded = 0;
+ x264_partition_size_cabac( h, &cabac_tmp, i8, i_pixel );
+ i_bits = ( cabac_tmp.f8_bits_encoded * i_lambda2 + 128 ) >> 8;
+ }
+ else
+ {
+ i_bits = x264_partition_size_cavlc( h, i8, i_pixel ) * i_lambda2;
+ }
+
+ return i_ssd + i_bits;
+}
+
+int x264_rd_cost_i8x8( x264_t *h, int i_lambda2, int i8, int i_mode )
+{
+ int i_ssd, i_bits;
+
+ x264_mb_encode_i8x8( h, i8, h->mb.i_qp );
+ i_ssd = ssd_plane( h, PIXEL_8x8, 0, (i8&1)*8, (i8>>1)*8 );
+
+ if( h->param.b_cabac )
+ {
+ x264_cabac_t cabac_tmp = h->cabac;
+ cabac_tmp.f8_bits_encoded = 0;
+ x264_partition_i8x8_size_cabac( h, &cabac_tmp, i8, i_mode );
+ i_bits = ( cabac_tmp.f8_bits_encoded * i_lambda2 + 128 ) >> 8;
+ }
+ else
+ {
+ i_bits = x264_partition_i8x8_size_cavlc( h, i8, i_mode ) * i_lambda2;
+ }
+
+ return i_ssd + i_bits;
+}
+
+int x264_rd_cost_i4x4( x264_t *h, int i_lambda2, int i4, int i_mode )
+{
+ int i_ssd, i_bits;
+
+ x264_mb_encode_i4x4( h, i4, h->mb.i_qp );
+ i_ssd = ssd_plane( h, PIXEL_4x4, 0, block_idx_x[i4]*4, block_idx_y[i4]*4 );
+
+ if( h->param.b_cabac )
+ {
+ x264_cabac_t cabac_tmp = h->cabac;
+ cabac_tmp.f8_bits_encoded = 0;
+ x264_partition_i4x4_size_cabac( h, &cabac_tmp, i4, i_mode );
+ i_bits = ( cabac_tmp.f8_bits_encoded * i_lambda2 + 128 ) >> 8;
+ }
+ else
+ {
+ i_bits = x264_partition_i4x4_size_cavlc( h, i4, i_mode ) * i_lambda2;
+ }
+
+ return i_ssd + i_bits;
+}
+
+int x264_rd_cost_i8x8_chroma( x264_t *h, int i_lambda2, int i_mode, int b_dct )
+{
+ int i_ssd, i_bits;
+
+ if( b_dct )
+ x264_mb_encode_8x8_chroma( h, 0, h->mb.i_chroma_qp );
+ i_ssd = ssd_plane( h, PIXEL_8x8, 1, 0, 0 ) +
+ ssd_plane( h, PIXEL_8x8, 2, 0, 0 );
+ h->mb.i_chroma_pred_mode = i_mode;
+
+ if( h->param.b_cabac )
+ {
+ x264_cabac_t cabac_tmp = h->cabac;
+ cabac_tmp.f8_bits_encoded = 0;
+ x264_i8x8_chroma_size_cabac( h, &cabac_tmp );
+ i_bits = ( cabac_tmp.f8_bits_encoded * i_lambda2 + 128 ) >> 8;
+ }
+ else
+ {
+ i_bits = x264_i8x8_chroma_size_cavlc( h ) * i_lambda2;
+ }
+
+ return i_ssd + i_bits;
+}
/****************************************************************************
* Trellis RD quantization
****************************************************************************/
-#define TRELLIS_SCORE_MAX (1ULL<<50)
+#define TRELLIS_SCORE_MAX ((uint64_t)1<<50)
#define CABAC_SIZE_BITS 8
#define SSD_WEIGHT_BITS 5
#define LAMBDA_BITS 4
/* precalculate the cost of coding abs_level_m1 */
-static int cabac_prefix_transition[15][128];
-static int cabac_prefix_size[15][128];
void x264_rdo_init( )
{
int i_prefix;
{ 4, 4, 4, 4, 5, 6, 7, 7 }
};
-static const int lambda2_tab[6] = { 1024, 1290, 1625, 2048, 2580, 3251 };
+// should the intra and inter lambdas be different?
+// I'm just matching the behaviour of deadzone quant.
+static const int lambda2_tab[2][52] = {
+ // inter lambda = .85 * .85 * 2**(qp/3. + 10 - LAMBDA_BITS)
+ { 46, 58, 73, 92, 117, 147,
+ 185, 233, 294, 370, 466, 587,
+ 740, 932, 1174, 1480, 1864, 2349,
+ 2959, 3728, 4697, 5918, 7457, 9395,
+ 11837, 14914, 18790, 23674, 29828, 37581,
+ 47349, 59656, 75163, 94699, 119313, 150326,
+ 189399, 238627, 300652, 378798, 477255, 601304,
+ 757596, 954511, 1202608, 1515192, 1909022, 2405217,
+ 3030384, 3818045, 4810435, 6060769 },
+ // intra lambda = .65 * .65 * 2**(qp/3. + 10 - LAMBDA_BITS)
+ { 27, 34, 43, 54, 68, 86,
+ 108, 136, 172, 216, 273, 343,
+ 433, 545, 687, 865, 1090, 1374,
+ 1731, 2180, 2747, 3461, 4361, 5494,
+ 6922, 8721, 10988, 13844, 17442, 21976,
+ 27688, 34885, 43953, 55377, 69771, 87906,
+ 110755, 139543, 175813, 221511, 279087, 351627,
+ 443023, 558174, 703255, 886046, 1116348, 1406511,
+ 1772093, 2232697, 2813022, 3544186 }
+};
typedef struct {
uint64_t score;
// and uses the dct scaling factors, not the idct ones.
static void quant_trellis_cabac( x264_t *h, int16_t *dct,
- const int *quant_mf, const int *unquant_mf,
- const int *coef_weight, const int *zigzag,
- int i_ctxBlockCat, int i_qbits, int i_lambda2, int b_ac, int i_coefs )
+ const uint16_t *quant_mf, const int *unquant_mf,
+ const int *coef_weight, const uint8_t *zigzag,
+ int i_ctxBlockCat, int i_lambda2, int b_ac, int i_coefs )
{
int abs_coefs[64], signs[64];
trellis_node_t nodes[2][8];
trellis_node_t *bnode;
uint8_t cabac_state_sig[64];
uint8_t cabac_state_last[64];
- const int f = 1 << (i_qbits-1); // no deadzone
- int i_last_nnz = -1;
+ const int b_interlaced = h->mb.b_interlaced;
+ const int f = 1 << 15; // no deadzone
+ int i_last_nnz;
int i, j;
// (# of coefs) * (# of ctx) * (# of levels tried) = 1024
int i_levels_used = 1;
/* init coefs */
- for( i = b_ac; i < i_coefs; i++ )
- {
- int coef = dct[zigzag[i]];
- abs_coefs[i] = abs(coef);
- signs[i] = coef < 0 ? -1 : 1;
- if( f <= abs_coefs[i] * quant_mf[zigzag[i]] )
- i_last_nnz = i;
- }
+ for( i = i_coefs-1; i >= b_ac; i-- )
+ if( (unsigned)(dct[zigzag[i]] * quant_mf[zigzag[i]] + f-1) >= 2*f )
+ break;
- if( i_last_nnz == -1 )
+ if( i < b_ac )
{
memset( dct, 0, i_coefs * sizeof(*dct) );
return;
}
+ i_last_nnz = i;
+
+ for( ; i >= b_ac; i-- )
+ {
+ int coef = dct[zigzag[i]];
+ abs_coefs[i] = abs(coef);
+ signs[i] = coef < 0 ? -1 : 1;
+ }
+
/* init trellis */
for( i = 1; i < 8; i++ )
nodes_cur[i].score = TRELLIS_SCORE_MAX;
if( i_coefs == 64 )
{
- const uint8_t *ctx_sig = &h->cabac.state[ significant_coeff_flag_offset[i_ctxBlockCat] ];
- const uint8_t *ctx_last = &h->cabac.state[ last_coeff_flag_offset[i_ctxBlockCat] ];
+ const uint8_t *ctx_sig = &h->cabac.state[ significant_coeff_flag_offset[b_interlaced][i_ctxBlockCat] ];
+ const uint8_t *ctx_last = &h->cabac.state[ last_coeff_flag_offset[b_interlaced][i_ctxBlockCat] ];
for( i = 0; i < 63; i++ )
{
- cabac_state_sig[i] = ctx_sig[ significant_coeff_flag_offset_8x8[i] ];
+ cabac_state_sig[i] = ctx_sig[ significant_coeff_flag_offset_8x8[b_interlaced][i] ];
cabac_state_last[i] = ctx_last[ last_coeff_flag_offset_8x8[i] ];
}
}
else
{
- memcpy( cabac_state_sig, &h->cabac.state[ significant_coeff_flag_offset[i_ctxBlockCat] ], 15 );
- memcpy( cabac_state_last, &h->cabac.state[ last_coeff_flag_offset[i_ctxBlockCat] ], 15 );
+ memcpy( cabac_state_sig, &h->cabac.state[ significant_coeff_flag_offset[b_interlaced][i_ctxBlockCat] ], 15 );
+ memcpy( cabac_state_last, &h->cabac.state[ last_coeff_flag_offset[b_interlaced][i_ctxBlockCat] ], 15 );
}
memcpy( nodes_cur[0].cabac_state, &h->cabac.state[ coeff_abs_level_m1_offset[i_ctxBlockCat] ], 10 );
for( i = i_last_nnz; i >= b_ac; i-- )
{
int i_coef = abs_coefs[i];
- int q = ( f + i_coef * quant_mf[zigzag[i]] ) >> i_qbits;
+ int q = ( f + i_coef * quant_mf[zigzag[i]] ) >> 16;
int abs_level;
int cost_sig[2], cost_last[2];
trellis_node_t n;
{
// no need to calculate ssd of 0s: it's the same in all nodes.
// no need to modify level_tree for ctx=0: it starts with an infinite loop of 0s.
- const int cost_sig0 = x264_cabac_size_decision_noup( &cabac_state_sig[i], 0 )
- * i_lambda2 >> ( CABAC_SIZE_BITS - LAMBDA_BITS );
+ const uint32_t cost_sig0 = x264_cabac_size_decision_noup( &cabac_state_sig[i], 0 )
+ * (uint64_t)i_lambda2 >> ( CABAC_SIZE_BITS - LAMBDA_BITS );
for( j = 1; j < 8; j++ )
{
if( nodes_cur[j].score != TRELLIS_SCORE_MAX )
void x264_quant_4x4_trellis( x264_t *h, int16_t dct[4][4], int i_quant_cat,
int i_qp, int i_ctxBlockCat, int b_intra )
{
- const int i_qbits = i_qp / 6;
- const int i_mf = i_qp % 6;
- const int b_ac = (i_ctxBlockCat == DCT_LUMA_AC);
- /* should the lambdas be different? I'm just matching the behaviour of deadzone quant. */
- const int i_lambda_mult = b_intra ? 65 : 85;
- const int i_lambda2 = ((lambda2_tab[i_mf] * i_lambda_mult*i_lambda_mult / 10000)
- << (2*i_qbits)) >> LAMBDA_BITS;
-
+ int b_ac = (i_ctxBlockCat == DCT_LUMA_AC);
quant_trellis_cabac( h, (int16_t*)dct,
- (int*)h->quant4_mf[i_quant_cat][i_mf], h->unquant4_mf[i_quant_cat][i_qp],
- x264_dct4_weight2_zigzag, x264_zigzag_scan4,
- i_ctxBlockCat, 15+i_qbits, i_lambda2, b_ac, 16 );
+ h->quant4_mf[i_quant_cat][i_qp], h->unquant4_mf[i_quant_cat][i_qp],
+ x264_dct4_weight2_zigzag[h->mb.b_interlaced],
+ x264_zigzag_scan4[h->mb.b_interlaced],
+ i_ctxBlockCat, lambda2_tab[b_intra][i_qp], b_ac, 16 );
}
void x264_quant_8x8_trellis( x264_t *h, int16_t dct[8][8], int i_quant_cat,
int i_qp, int b_intra )
{
- const int i_qbits = i_qp / 6;
- const int i_mf = i_qp % 6;
- const int i_lambda_mult = b_intra ? 65 : 85;
- const int i_lambda2 = ((lambda2_tab[i_mf] * i_lambda_mult*i_lambda_mult / 10000)
- << (2*i_qbits)) >> LAMBDA_BITS;
-
quant_trellis_cabac( h, (int16_t*)dct,
- (int*)h->quant8_mf[i_quant_cat][i_mf], h->unquant8_mf[i_quant_cat][i_qp],
- x264_dct8_weight2_zigzag, x264_zigzag_scan8,
- DCT_LUMA_8x8, 16+i_qbits, i_lambda2, 0, 64 );
+ h->quant8_mf[i_quant_cat][i_qp], h->unquant8_mf[i_quant_cat][i_qp],
+ x264_dct8_weight2_zigzag[h->mb.b_interlaced],
+ x264_zigzag_scan8[h->mb.b_interlaced],
+ DCT_LUMA_8x8, lambda2_tab[b_intra][i_qp], 0, 64 );
}