625
625
/* Uses strings due to the fact that the speed of the control functions is
626
626
negligable compared to the cost of running slicetype_frame_cost, and because
627
627
it makes debugging easier. */
628
static void x264_slicetype_path( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames, int length, int max_bframes, int buffer_size, char (*best_paths)[X264_LOOKAHEAD_MAX] )
628
static void x264_slicetype_path( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames, int length, int max_bframes, char (*best_paths)[X264_LOOKAHEAD_MAX] )
630
630
char paths[X264_BFRAME_MAX+2][X264_LOOKAHEAD_MAX] = {{0}};
631
631
int num_paths = X264_MIN(max_bframes+1, length);
666
666
int icost = frame->i_cost_est[0][0];
667
667
int pcost = frame->i_cost_est[p1-p0][0];
669
int i_gop_size = frame->i_frame - h->frames.i_last_idr;
669
int i_gop_size = frame->i_frame - h->lookahead->i_last_idr;
670
670
float f_thresh_max = h->param.i_scenecut_threshold / 100.0;
671
671
/* magic numbers pulled out of thin air */
672
672
float f_thresh_min = f_thresh_max * h->param.i_keyint_min
703
static void x264_slicetype_analyse( x264_t *h, int keyframe )
703
void x264_slicetype_analyse( x264_t *h, int keyframe )
705
705
x264_mb_analysis_t a;
706
706
x264_frame_t *frames[X264_LOOKAHEAD_MAX+3] = { NULL, };
707
int num_frames, keyint_limit, idr_frame_type, i, j;
710
708
int i_mb_count = NUM_MBS;
711
709
int cost1p0, cost2p0, cost1b1, cost2p1;
710
int i_max_search = X264_MIN( h->lookahead->next.i_size, X264_LOOKAHEAD_MAX );
711
if( h->param.b_deterministic )
712
i_max_search = X264_MIN( i_max_search, h->lookahead->i_slicetype_length + !keyframe );
714
714
assert( h->frames.b_have_lowres );
716
if( !h->frames.last_nonb )
716
if( !h->lookahead->last_nonb )
718
frames[0] = h->frames.last_nonb;
719
for( j = 0; h->frames.next[j] && h->frames.next[j]->i_type == X264_TYPE_AUTO; j++ )
720
frames[j+1] = h->frames.next[j];
718
frames[0] = h->lookahead->last_nonb;
719
for( j = 0; j < i_max_search && h->lookahead->next.list[j]->i_type == X264_TYPE_AUTO; j++ )
720
frames[j+1] = h->lookahead->next.list[j];
725
keyint_limit = h->param.i_keyint_max - frames[0]->i_frame + h->frames.i_last_idr - 1;
725
keyint_limit = h->param.i_keyint_max - frames[0]->i_frame + h->lookahead->i_last_idr - 1;
726
726
num_frames = X264_MIN( j, keyint_limit );
728
728
x264_lowres_context_init( h, &a );
729
idr_frame_type = frames[1]->i_frame - h->frames.i_last_idr >= h->param.i_keyint_min ? X264_TYPE_IDR : X264_TYPE_I;
729
idr_frame_type = frames[1]->i_frame - h->lookahead->i_last_idr >= h->param.i_keyint_min ? X264_TYPE_IDR : X264_TYPE_I;
731
731
/* This is important psy-wise: if we have a non-scenecut keyframe,
732
732
* there will be significant visual artifacts if the frames just before
766
766
/* Perform the frametype analysis. */
767
767
for( n = 2; n < num_frames-1; n++ )
768
x264_slicetype_path( h, &a, frames, n, max_bframes, num_frames-max_bframes, best_paths );
768
x264_slicetype_path( h, &a, frames, n, max_bframes, best_paths );
769
769
if( num_frames > 1 )
771
771
num_bframes = strspn( best_paths[num_frames-2], "B" );
891
if( h->frames.next[0] == NULL )
891
if( !h->lookahead->next.i_size )
894
894
if( h->param.rc.b_stat_read )
896
896
/* Use the frame types from the first pass */
897
for( i = 0; h->frames.next[i] != NULL; i++ )
898
h->frames.next[i]->i_type =
899
x264_ratecontrol_slice_type( h, h->frames.next[i]->i_frame );
897
for( i = 0; i < h->lookahead->next.i_size; i++ )
898
h->lookahead->next.list[i]->i_type =
899
x264_ratecontrol_slice_type( h, h->lookahead->next.list[i]->i_frame );
901
901
else if( (h->param.i_bframe && h->param.i_bframe_adaptive)
902
902
|| h->param.i_scenecut_threshold
907
907
for( bframes = 0;; bframes++ )
909
frm = h->frames.next[bframes];
909
frm = h->lookahead->next.list[bframes];
911
911
/* Limit GOP size */
912
if( frm->i_frame - h->frames.i_last_idr >= h->param.i_keyint_max )
912
if( frm->i_frame - h->lookahead->i_last_idr >= h->param.i_keyint_max )
914
914
if( frm->i_type == X264_TYPE_AUTO )
915
915
frm->i_type = X264_TYPE_IDR;
919
919
if( frm->i_type == X264_TYPE_IDR )
922
h->lookahead->i_last_idr = frm->i_frame;
922
923
if( bframes > 0 )
925
h->frames.next[bframes]->i_type = X264_TYPE_P;
926
h->lookahead->next.list[bframes]->i_type = X264_TYPE_P;
933
if( bframes == h->param.i_bframe
934
|| h->frames.next[bframes+1] == NULL )
930
if( bframes == h->param.i_bframe ||
931
!h->lookahead->next.list[bframes+1] )
936
933
if( IS_X264_TYPE_B( frm->i_type ) )
937
934
x264_log( h, X264_LOG_WARNING, "specified frame type is not compatible with max B-frames\n" );
946
943
else if( !IS_X264_TYPE_B( frm->i_type ) ) break;
947
h->lookahead->next.list[bframes-1]->b_last_minigop_bframe = 1;
948
h->lookahead->next.list[bframes]->i_bframes = bframes;
950
/* calculate the frame costs ahead of time for x264_rc_analyse_slice while we still have lowres */
951
if( h->param.rc.i_rc_method != X264_RC_CQP )
953
x264_mb_analysis_t a;
954
x264_frame_t *frames[X264_BFRAME_MAX+2] = { NULL, };
957
x264_lowres_context_init( h, &a );
959
if( IS_X264_TYPE_I( h->lookahead->next.list[bframes]->i_type ) )
962
p1 = b = bframes + 1;
963
frames[p0] = h->lookahead->last_nonb;
964
frames[b] = h->lookahead->next.list[bframes];
966
x264_slicetype_frame_cost( h, &a, frames, p0, p1, b, 0 );
950
970
int x264_rc_analyse_slice( x264_t *h )
952
x264_mb_analysis_t a;
953
x264_frame_t *frames[X264_LOOKAHEAD_MAX+2] = { NULL, };
972
x264_frame_t *frames[X264_BFRAME_MAX+2] = { NULL, };
957
x264_lowres_context_init( h, &a );
959
976
if( IS_X264_TYPE_I(h->fenc->i_type) )
962
/* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
963
if( h->param.rc.b_mb_tree || (h->param.rc.i_vbv_buffer_size && h->param.rc.i_lookahead) )
965
h->frames.last_nonb = h->fenc;
966
x264_slicetype_analyse( h, 1 );
969
else if( X264_TYPE_P == h->fenc->i_type )
972
while( h->frames.current[p1] && IS_X264_TYPE_B( h->frames.current[p1]->i_type ) )
979
p1 = (h->fref1[0]->i_poc - h->fref0[0]->i_poc)/2;
980
b = (h->fref1[0]->i_poc - h->fenc->i_poc)/2;
981
frames[p1] = h->fref1[0];
979
p1 = b = h->fenc->i_bframes + 1;
983
980
frames[p0] = h->fref0[0];
984
981
frames[b] = h->fenc;
986
cost = x264_slicetype_frame_cost( h, &a, frames, p0, p1, b, 0 );
983
/* cost should have been already calculated by x264_slicetype_decide */
984
cost = frames[b]->i_cost_est[b-p0][p1-b];
988
987
if( h->param.rc.b_mb_tree && !h->param.rc.b_stat_read )
989
988
cost = x264_slicetype_frame_cost_recalculate( h, frames, p0, p1, b );