~siretart/x264/trunk

« back to all changes in this revision

Viewing changes to encoder/slicetype.c

  • Committer: Fiona Glaser
  • Author(s): Steven Walters
  • Date: 2009-09-02 04:06:20 UTC
  • Revision ID: git-v1:6940dcaef140d8a0c43c9a62db158e9d71a8fdeb
Threaded lookahead
Move lookahead into a separate thread, set to higher priority than the other threads, for optimal performance.
Reduces the amount that lookahead bottlenecks encoding, greatly increasing performance with lookahead-intensive settings (e.g. b-adapt 2) on many-core CPUs.
Buffer size can be controlled with --sync-lookahead, which defaults to auto (threads+bframes buffer size).
Note that this buffer is separate from the rc-lookahead value.
Note also that this does not split lookahead itself into multiple threads yet; this may be added in the future.
Additionally, split frames into "fdec" and "fenc" frame types and keep the two separate.
This split greatly reduces memory usage, which helps compensate for the larger lookahead size.
Extremely special thanks to Michael Kazmier and Alex Giladi of Avail Media, the original authors of this patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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] )
629
629
{
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];
668
668
    float f_bias;
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
700
700
    return res;
701
701
}
702
702
 
703
 
static void x264_slicetype_analyse( x264_t *h, int keyframe )
 
703
void x264_slicetype_analyse( x264_t *h, int keyframe )
704
704
{
705
705
    x264_mb_analysis_t a;
706
706
    x264_frame_t *frames[X264_LOOKAHEAD_MAX+3] = { NULL, };
707
 
    int num_frames;
708
 
    int keyint_limit;
709
 
    int i,j;
 
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;
712
 
    int idr_frame_type;
 
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 );
713
713
 
714
714
    assert( h->frames.b_have_lowres );
715
715
 
716
 
    if( !h->frames.last_nonb )
 
716
    if( !h->lookahead->last_nonb )
717
717
        return;
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];
721
721
 
722
722
    if( !j )
723
723
        return;
724
724
 
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 );
727
727
 
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;
730
730
 
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
765
765
        {
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 )
770
770
            {
771
771
                num_bframes = strspn( best_paths[num_frames-2], "B" );
888
888
    int bframes;
889
889
    int i;
890
890
 
891
 
    if( h->frames.next[0] == NULL )
 
891
    if( !h->lookahead->next.i_size )
892
892
        return;
893
893
 
894
894
    if( h->param.rc.b_stat_read )
895
895
    {
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 );
900
900
    }
901
901
    else if( (h->param.i_bframe && h->param.i_bframe_adaptive)
902
902
             || h->param.i_scenecut_threshold
906
906
 
907
907
    for( bframes = 0;; bframes++ )
908
908
    {
909
 
        frm = h->frames.next[bframes];
 
909
        frm = h->lookahead->next.list[bframes];
910
910
 
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 )
913
913
        {
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 )
920
920
        {
921
921
            /* Close GOP */
 
922
            h->lookahead->i_last_idr = frm->i_frame;
922
923
            if( bframes > 0 )
923
924
            {
924
925
                bframes--;
925
 
                h->frames.next[bframes]->i_type = X264_TYPE_P;
926
 
            }
927
 
            else
928
 
            {
929
 
                h->i_frame_num = 0;
 
926
                h->lookahead->next.list[bframes]->i_type = X264_TYPE_P;
930
927
            }
931
928
        }
932
929
 
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] )
935
932
        {
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" );
945
942
 
946
943
        else if( !IS_X264_TYPE_B( frm->i_type ) ) break;
947
944
    }
 
945
 
 
946
    if( bframes )
 
947
        h->lookahead->next.list[bframes-1]->b_last_minigop_bframe = 1;
 
948
    h->lookahead->next.list[bframes]->i_bframes = bframes;
 
949
 
 
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 )
 
952
    {
 
953
        x264_mb_analysis_t a;
 
954
        x264_frame_t *frames[X264_BFRAME_MAX+2] = { NULL, };
 
955
        int p0=0, p1, b;
 
956
 
 
957
        x264_lowres_context_init( h, &a );
 
958
 
 
959
        if( IS_X264_TYPE_I( h->lookahead->next.list[bframes]->i_type ) )
 
960
            p1 = b = 0;
 
961
        else // P
 
962
            p1 = b = bframes + 1;
 
963
        frames[p0] = h->lookahead->last_nonb;
 
964
        frames[b] = h->lookahead->next.list[bframes];
 
965
 
 
966
        x264_slicetype_frame_cost( h, &a, frames, p0, p1, b, 0 );
 
967
    }
948
968
}
949
969
 
950
970
int x264_rc_analyse_slice( x264_t *h )
951
971
{
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, };
954
973
    int p0=0, p1, b;
955
974
    int cost;
956
975
 
957
 
    x264_lowres_context_init( h, &a );
958
 
 
959
976
    if( IS_X264_TYPE_I(h->fenc->i_type) )
960
 
    {
961
977
        p1 = b = 0;
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) )
964
 
        {
965
 
            h->frames.last_nonb = h->fenc;
966
 
            x264_slicetype_analyse( h, 1 );
967
 
        }
968
 
    }
969
 
    else if( X264_TYPE_P == h->fenc->i_type )
970
 
    {
971
 
        p1 = 0;
972
 
        while( h->frames.current[p1] && IS_X264_TYPE_B( h->frames.current[p1]->i_type ) )
973
 
            p1++;
974
 
        p1++;
975
 
        b = p1;
976
 
    }
977
 
    else //B
978
 
    {
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];
982
 
    }
 
978
    else // P
 
979
        p1 = b = h->fenc->i_bframes + 1;
983
980
    frames[p0] = h->fref0[0];
984
981
    frames[b] = h->fenc;
985
982
 
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];
 
985
    assert( cost >= 0 );
987
986
 
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 );