- x264_slices_write( h );
-
- /* restore CPU state (before using float again) */
- x264_emms();
-
- if( h->sh.i_type == SLICE_TYPE_P && !h->param.rc.b_stat_read
- && h->param.i_scenecut_threshold >= 0
- && !h->param.b_pre_scenecut )
- {
- 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;
-
- float f_bias;
- int i_gop_size = h->fenc->i_frame - h->frames.i_last_idr;
- float 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 );
- if( h->param.i_keyint_min == h->param.i_keyint_max )
- f_thresh_min= f_thresh_max;
-
- /* macroblock_analyse() doesn't further analyse skipped mbs,
- * so we have to guess their cost */
- if( h->stat.frame.i_mbs_analysed > 0 )
- i_intra_cost = i_intra_cost * i_mb / h->stat.frame.i_mbs_analysed;
-
- if( i_gop_size < h->param.i_keyint_min / 4 )
- f_bias = f_thresh_min / 4;
- else if( i_gop_size <= h->param.i_keyint_min )
- f_bias = f_thresh_min * i_gop_size / h->param.i_keyint_min;
- else
- {
- f_bias = f_thresh_min
- + ( f_thresh_max - f_thresh_min )
- * ( i_gop_size - h->param.i_keyint_min )
- / ( h->param.i_keyint_max - h->param.i_keyint_min );
- }
- f_bias = X264_MIN( f_bias, 1.0 );
-
- /* Bad P will be reencoded as I */
- if( h->stat.frame.i_mbs_analysed > 0 &&
- i_inter_cost >= (1.0 - f_bias) * i_intra_cost )
- {
- int b;
-
- x264_log( h, X264_LOG_DEBUG, "scene cut at %d Icost:%.0f Pcost:%.0f ratio:%.4f bias:%.4f gop:%d (imb:%d pmb:%d smb:%d)\n",
- h->fenc->i_frame,
- (double)i_intra_cost, (double)i_inter_cost,
- 1. - (double)i_inter_cost / i_intra_cost,
- f_bias, i_gop_size,
- i_mb_i, i_mb_p, i_mb_s );
-
- /* Restore frame num */
- h->i_frame_num--;
-
- for( b = 0; h->frames.current[b] && IS_X264_TYPE_B( h->frames.current[b]->i_type ); b++ );
- if( b > 0 )
- {
- /* If using B-frames, force GOP to be closed.
- * Even if this frame is going to be I and not IDR, forcing a
- * P-frame before the scenecut will probably help compression.
- *
- * We don't yet know exactly which frame is the scene cut, so
- * we can't assign an I-frame. Instead, change the previous
- * B-frame to P, and rearrange coding order. */
-
- if( h->param.b_bframe_adaptive || b > 1 )
- h->fenc->i_type = X264_TYPE_AUTO;
- x264_frame_sort_pts( h->frames.current );
- x264_frame_unshift( h->frames.next, h->fenc );
- h->fenc = h->frames.current[b-1];
- h->frames.current[b-1] = NULL;
- h->fenc->i_type = X264_TYPE_P;
- x264_frame_sort_dts( h->frames.current );
- }
- /* Do IDR if needed */
- else if( i_gop_size >= h->param.i_keyint_min )
- {
- /* Reset */
- h->i_frame_num = 0;
-
- /* Reinit field of fenc */
- h->fenc->i_type = X264_TYPE_IDR;
- h->fenc->i_poc = 0;
-
- /* Put enqueued frames back in the pool */
- while( h->frames.current[0] )
- x264_frame_push( h->frames.next, x264_frame_shift( h->frames.current ) );
- x264_frame_sort_pts( h->frames.next );
- }
- else
- {
- h->fenc->i_type = X264_TYPE_I;
- }
- goto do_encode;
- }
- }