diff --git a/common/common.h b/common/common.h index 0498139..22e514f 100644 --- a/common/common.h +++ b/common/common.h @@ -165,9 +165,10 @@ extern const float x264_log2_lz_lut[32]; * qp to qscale. */ static ALWAYS_INLINE int x264_exp2fix8( float x ) { + int i; if( x >= 512.f/6.f ) return 0; if( x <= -512.f/6.f ) return 0xffff; - int i = x*(-64.f/6.f) + 512; + i = x*(-64.f/6.f) + 512; return (x264_exp2_lut[i&63]+256) << (i>>6) >> 8; } diff --git a/common/macroblock.c b/common/macroblock.c index 54a095d..452031d 100644 --- a/common/macroblock.c +++ b/common/macroblock.c @@ -666,6 +666,7 @@ int x264_macroblock_cache_init( x264_t *h ) { int i, j; int i_mb_count = h->mb.i_mb_count; + int buf_hpel, buf_ssim, me_range, buf_tesa, buf_mbtree; h->mb.i_mb_stride = h->sps->i_mb_width; h->mb.i_b8_stride = h->sps->i_mb_width * 2; @@ -739,12 +740,12 @@ int x264_macroblock_cache_init( x264_t *h ) h->mb.i_neighbour4[15] = h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT; - int buf_hpel = (h->param.i_width+48) * sizeof(int16_t); - int buf_ssim = h->param.analyse.b_ssim * 8 * (h->param.i_width/4+3) * sizeof(int); - int me_range = X264_MIN(h->param.analyse.i_me_range, h->param.analyse.i_mv_range); - int buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) * - ((me_range*2+18) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t)); - int buf_mbtree = h->param.rc.b_mb_tree * ((h->sps->i_mb_width+3)&~3) * sizeof(int); + buf_hpel = (h->param.i_width+48) * sizeof(int16_t); + buf_ssim = h->param.analyse.b_ssim * 8 * (h->param.i_width/4+3) * sizeof(int); + me_range = X264_MIN(h->param.analyse.i_me_range, h->param.analyse.i_mv_range); + buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) * + ((me_range*2+18) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t)); + buf_mbtree = h->param.rc.b_mb_tree * ((h->sps->i_mb_width+3)&~3) * sizeof(int); CHECKED_MALLOC( h->scratch_buffer, X264_MAX4( buf_hpel, buf_ssim, buf_tesa, buf_mbtree ) ); return 0; diff --git a/encoder/analyse.c b/encoder/analyse.c index 5fcfd99..0b3f935 100644 --- a/encoder/analyse.c +++ b/encoder/analyse.c @@ -2947,7 +2947,7 @@ void x264_macroblock_analyse( x264_t *h ) if( analysis.i_mbrd >= 2 ) { /* Don't bother with bipred or 8x8-and-below, the odds are incredibly low. */ - static const uint8_t check_mv_lists[X264_MBTYPE_MAX] = {[P_L0]=1, [B_L0_L0]=1, [B_L1_L1]=2}; + static const uint8_t check_mv_lists[X264_MBTYPE_MAX] = {0,0,0,0,1,0,0,0,1,0,0,0,2,0,0,0,0,0,0}; int list = check_mv_lists[h->mb.i_type] - 1; if( list >= 0 && h->mb.i_partition != D_16x16 && *(uint32_t*)&h->mb.cache.mv[list][x264_scan8[0]] == *(uint32_t*)&h->mb.cache.mv[list][x264_scan8[12]] && diff --git a/encoder/encoder.c b/encoder/encoder.c index 9dfe397..f6dc4ed 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -337,6 +337,7 @@ fail: static int x264_validate_parameters( x264_t *h ) { + int max_slices; #ifdef HAVE_MMX if( !(x264_cpu_detect() & X264_CPU_SSE) ) { @@ -466,7 +467,7 @@ static int x264_validate_parameters( x264_t *h ) h->param.i_width, h->param.i_height ); } - int max_slices = (h->param.i_height+((16<param.b_interlaced)-1))/(16<param.b_interlaced); + max_slices = (h->param.i_height+((16<param.b_interlaced)-1))/(16<param.b_interlaced); h->param.i_slice_count = x264_clip3( h->param.i_slice_count, 0, max_slices ); h->param.i_slice_max_size = X264_MAX( h->param.i_slice_max_size, 0 ); h->param.i_slice_max_mbs = X264_MAX( h->param.i_slice_max_mbs, 0 ); @@ -1023,6 +1024,7 @@ static int x264_nal_end( x264_t *h ) static int x264_encoder_encapsulate_nals( x264_t *h ) { int nal_size = 0, i; + uint8_t *nal_buffer; for( i = 0; i < h->out.i_nal; i++ ) nal_size += h->out.nal[i].i_payload; @@ -1036,7 +1038,7 @@ static int x264_encoder_encapsulate_nals( x264_t *h ) h->nal_buffer = buf; } - uint8_t *nal_buffer = h->nal_buffer; + nal_buffer = h->nal_buffer; for( i = 0; i < h->out.i_nal; i++ ) { @@ -1345,6 +1347,8 @@ static int x264_slice_write( x264_t *h ) while( (mb_xy = i_mb_x + i_mb_y * h->sps->i_mb_width) <= h->sh.i_last_mb ) { + int total_bits; + int mb_size; int mb_spos = bs_pos(&h->out.bs) + x264_cabac_pos(&h->cabac); if( h->param.i_slice_max_size > 0 ) { @@ -1408,8 +1412,8 @@ static int x264_slice_write( x264_t *h ) } } - int total_bits = bs_pos(&h->out.bs) + x264_cabac_pos(&h->cabac); - int mb_size = total_bits - mb_spos; + total_bits = bs_pos(&h->out.bs) + x264_cabac_pos(&h->cabac); + mb_size = total_bits - mb_spos; /* We'll just re-encode this last macroblock if we go over the max slice size. */ if( total_bits - starting_bits > slice_max_size && !h->mb.b_reencode_mb ) @@ -1593,10 +1597,12 @@ static void *x264_slices_write( x264_t *h ) h->sh.i_last_mb = h->sh.i_first_mb + h->param.i_slice_max_mbs - 1; else if( h->param.i_slice_count ) { + double height; + int width; x264_emms(); i_slice_num++; - double height = h->sps->i_mb_height >> h->param.b_interlaced; - int width = h->sps->i_mb_width << h->param.b_interlaced; + height = h->sps->i_mb_height >> h->param.b_interlaced; + width = h->sps->i_mb_width << h->param.b_interlaced; h->sh.i_last_mb = (int)(height * i_slice_num / h->param.i_slice_count + 0.5) * width - 1; } h->sh.i_last_mb = X264_MIN( h->sh.i_last_mb, h->mb.i_mb_count - 1 ); @@ -1639,6 +1645,7 @@ int x264_encoder_encode( x264_t *h, int i_nal_ref_idc; int i_global_qp; + int overhead = NALU_OVERHEAD; if( h->param.i_threads > 1) { @@ -1814,8 +1821,6 @@ int x264_encoder_encode( x264_t *h, h->i_nal_type = i_nal_type; h->i_nal_ref_idc = i_nal_ref_idc; - int overhead = NALU_OVERHEAD; - /* Write SPS and PPS */ if( i_nal_type == NAL_SLICE_IDR && h->param.b_repeat_headers ) { @@ -2206,6 +2211,8 @@ void x264_encoder_close ( x264_t *h ) int64_t i_mb_count = i_count * h->mb.i_mb_count; float fps = (float) h->param.i_fps_num / h->param.i_fps_den; float f_bitrate = fps * SUM3(h->stat.i_frame_size) / i_count / 125; + int64_t fixed_pred_modes[3][9] = {{0}}; + int64_t sum_pred_modes[3] = {0}; if( h->pps->b_transform_8x8_mode ) { @@ -2234,8 +2241,6 @@ void x264_encoder_close ( x264_t *h ) h->stat.i_mb_cbp[2] * 100.0 / (i_all_intra ), h->stat.i_mb_cbp[4] * 100.0 / (i_all_intra ), buf ); - int64_t fixed_pred_modes[3][9] = {{0}}; - int64_t sum_pred_modes[3] = {0}; for( i = 0; i <= I_PRED_16x16_DC_128; i++ ) { fixed_pred_modes[0][x264_mb_pred_mode16x16_fix[i]] += h->stat.i_mb_pred_mode[0][i]; diff --git a/encoder/lookahead.c b/encoder/lookahead.c index 968d0fc..957792a 100644 --- a/encoder/lookahead.c +++ b/encoder/lookahead.c @@ -131,8 +131,9 @@ static void x264_lookahead_thread( x264_t *h ) int x264_lookahead_init( x264_t *h, int i_slicetype_length ) { x264_lookahead_t *look; - CHECKED_MALLOCZERO( look, sizeof(x264_lookahead_t) ); int i; + x264_t *look_h; + CHECKED_MALLOCZERO( look, sizeof(x264_lookahead_t) ); for( i = 0; i < h->param.i_threads; i++ ) h->thread[i]->lookahead = look; @@ -150,7 +151,7 @@ int x264_lookahead_init( x264_t *h, int i_slicetype_length ) if( !h->param.i_sync_lookahead ) return 0; - x264_t *look_h = h->thread[h->param.i_threads]; + look_h = h->thread[h->param.i_threads]; *look_h = *h; if( x264_macroblock_cache_init( look_h ) ) goto fail; @@ -248,13 +249,13 @@ void x264_lookahead_get_frames( x264_t *h ) } else { /* We are not running a lookahead thread, so perform all the slicetype decide on the fly */ + int bframes=0; if( h->frames.current[0] || !h->lookahead->next.i_size ) return; x264_stack_align( x264_slicetype_decide, h ); - int bframes=0; while( IS_X264_TYPE_B( h->lookahead->next.list[bframes]->i_type ) ) bframes++; diff --git a/encoder/me.c b/encoder/me.c index 8510ce2..891fffe 100644 --- a/encoder/me.c +++ b/encoder/me.c @@ -197,6 +197,8 @@ void x264_me_search_ref( x264_t *h, x264_me_t *m, int16_t (*mvc)[2], int i_mvc, const uint16_t *p_cost_mvx = m->p_cost_mv - m->mvp[0]; const uint16_t *p_cost_mvy = m->p_cost_mv - m->mvp[1]; + const uint16_t *p_cost_omvx; + const uint16_t *p_cost_omvy; bmx = x264_clip3( m->mvp[0], mv_x_min*4, mv_x_max*4 ); bmy = x264_clip3( m->mvp[1], mv_y_min*4, mv_y_max*4 ); @@ -452,8 +454,8 @@ me_hex2: /* hexagon grid */ omx = bmx; omy = bmy; - const uint16_t *p_cost_omvx = p_cost_mvx + omx*4; - const uint16_t *p_cost_omvy = p_cost_mvy + omy*4; + p_cost_omvx = p_cost_mvx + omx*4; + p_cost_omvy = p_cost_mvy + omy*4; i = 1; do { diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c index b082cc7..c106fb5 100644 --- a/encoder/ratecontrol.c +++ b/encoder/ratecontrol.c @@ -1377,12 +1377,13 @@ static double predict_size( predictor_t *p, double q, double var ) static void update_predictor( predictor_t *p, double q, double var, double bits ) { const double range = 1.5; + double old_coeff, new_coeff, new_coeff_clipped, new_offset; if( var < 10 ) return; - double old_coeff = p->coeff / p->count; - double new_coeff = bits*q / var; - double new_coeff_clipped = x264_clip3f( new_coeff, old_coeff/range, old_coeff*range ); - double new_offset = bits*q - new_coeff_clipped * var; + old_coeff = p->coeff / p->count; + new_coeff = bits*q / var; + new_coeff_clipped = x264_clip3f( new_coeff, old_coeff/range, old_coeff*range ); + new_offset = bits*q - new_coeff_clipped * var; if( new_offset >= 0 ) new_coeff = new_coeff_clipped; else @@ -1499,6 +1500,8 @@ static double clip_qscale( x264_t *h, int pict_type, double q ) /* Fallback to old purely-reactive algorithm: no lookahead. */ else { + double bits; + double qf = 1.0; if( ( pict_type == SLICE_TYPE_P || ( pict_type == SLICE_TYPE_I && rcc->last_non_b_pict_type == SLICE_TYPE_I ) ) && rcc->buffer_fill/rcc->buffer_size < 0.5 ) @@ -1508,8 +1511,7 @@ static double clip_qscale( x264_t *h, int pict_type, double q ) /* Now a hard threshold to make sure the frame fits in VBV. * This one is mostly for I-frames. */ - double bits = predict_size( &rcc->pred[h->sh.i_type], q, rcc->last_satd ); - double qf = 1.0; + bits = predict_size( &rcc->pred[h->sh.i_type], q, rcc->last_satd ); if( bits > rcc->buffer_fill/2 ) qf = x264_clip3f( rcc->buffer_fill/(2*bits), 0.2, 1.0 ); q /= qf; diff --git a/encoder/slicetype.c b/encoder/slicetype.c index 4314062..f75bef0 100644 --- a/encoder/slicetype.c +++ b/encoder/slicetype.c @@ -456,12 +456,16 @@ static void x264_macroblock_tree_propagate( x264_t *h, x264_frame_t **frames, in int idx1 = idx0 + 1; int idx2 = idx0 + h->mb.i_mb_stride; int idx3 = idx0 + h->mb.i_mb_stride + 1; + int idx0weight; + int idx1weight; + int idx2weight; + int idx3weight; x &= 31; y &= 31; - int idx0weight = (32-y)*(32-x); - int idx1weight = (32-y)*x; - int idx2weight = y*(32-x); - int idx3weight = y*x; + idx0weight = (32-y)*(32-x); + idx1weight = (32-y)*x; + idx2weight = y*(32-x); + idx3weight = y*x; /* Apply bipred weighting. */ if( lists_used == 3 ) @@ -653,17 +657,19 @@ static void x264_slicetype_path( x264_t *h, x264_mb_analysis_t *a, x264_frame_t static int scenecut( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames, int p0, int p1, int print ) { x264_frame_t *frame = frames[p1]; + int icost, pcost; + int i_gop_size; + float f_thresh_max, f_thresh_min, f_bias; + int res; x264_slicetype_frame_cost( h, a, frames, p0, p1, p1, 0 ); - int icost = frame->i_cost_est[0][0]; - int pcost = frame->i_cost_est[p1-p0][0]; - float f_bias; - int i_gop_size = frame->i_frame - h->lookahead->i_last_idr; - float f_thresh_max = h->param.i_scenecut_threshold / 100.0; + icost = frame->i_cost_est[0][0]; + pcost = frame->i_cost_est[p1-p0][0]; + i_gop_size = frame->i_frame - h->lookahead->i_last_idr; + f_thresh_max = h->param.i_scenecut_threshold / 100.0; /* magic numbers pulled out of thin air */ - float f_thresh_min = f_thresh_max * h->param.i_keyint_min - / ( h->param.i_keyint_max * 4 ); - int res; + f_thresh_min = f_thresh_max * h->param.i_keyint_min + / ( h->param.i_keyint_max * 4 ); if( h->param.i_keyint_min == h->param.i_keyint_max ) f_thresh_min= f_thresh_max; @@ -700,6 +706,12 @@ void x264_slicetype_analyse( x264_t *h, int keyframe ) int i_mb_count = NUM_MBS; int cost1p0, cost2p0, cost1b1, cost2p1; int i_max_search = X264_MIN( h->lookahead->next.i_size, X264_LOOKAHEAD_MAX ); + char best_paths[X264_LOOKAHEAD_MAX][X264_LOOKAHEAD_MAX] = {"","P"}; + int n; + int num_bframes = 0; + int max_bframes; + int num_analysed_frames; + int reset_start; if( h->param.b_deterministic ) i_max_search = X264_MIN( i_max_search, h->lookahead->i_slicetype_length + !keyframe ); @@ -739,12 +751,8 @@ void x264_slicetype_analyse( x264_t *h, int keyframe ) return; } - char best_paths[X264_LOOKAHEAD_MAX][X264_LOOKAHEAD_MAX] = {"","P"}; - int n; - int num_bframes = 0; - int max_bframes = X264_MIN(num_frames-1, h->param.i_bframe); - int num_analysed_frames = num_frames; - int reset_start; + max_bframes = X264_MIN(num_frames-1, h->param.i_bframe); + num_analysed_frames = num_frames; if( h->param.i_scenecut_threshold && scenecut( h, &a, frames, 0, 1, 1 ) ) { frames[1]->i_type = idr_frame_type; diff --git a/muxers.c b/muxers.c index 7100877..c9c9555 100644 --- a/muxers.c +++ b/muxers.c @@ -343,10 +343,10 @@ typedef struct int open_file_avis( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param ) { avis_input_t *h = malloc( sizeof(avis_input_t) ); - if( !h ) - return -1; AVISTREAMINFO info; int i; + if( !h ) + return -1; *p_handle = (hnd_t)h;