~ubuntu-branches/ubuntu/maverick/x264/maverick-updates

« back to all changes in this revision

Viewing changes to encoder/ratecontrol.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2010-02-18 07:39:51 UTC
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: james.westby@ubuntu.com-20100218073951-9jgsvskb976rfbvq
Tags: upstream-0.85.1442.1+git781d30
ImportĀ upstreamĀ versionĀ 0.85.1442.1+git781d30

Show diffs side-by-side

added added

removed removed

Lines of Context:
134
134
                                 * This value is the current position (0 or 1). */
135
135
 
136
136
    /* MBRC stuff */
137
 
    double frame_size_estimated;
 
137
    float frame_size_estimated; /* Access to this variable must be atomic: double is
 
138
                                 * not atomic on all arches we care about */
138
139
    double frame_size_planned;
139
140
    double slice_size_planned;
 
141
    double max_frame_error;
140
142
    predictor_t (*row_pred)[2];
141
143
    predictor_t row_preds[5][2];
142
144
    predictor_t *pred_b_from_p; /* predict B-frame size from P-frame satd */
388
390
    return output;
389
391
}
390
392
 
 
393
void x264_ratecontrol_init_reconfigurable( x264_t *h, int b_init )
 
394
{
 
395
    x264_ratecontrol_t *rc = h->rc;
 
396
    if( !b_init && rc->b_2pass )
 
397
        return;
 
398
 
 
399
    if( h->param.rc.i_vbv_max_bitrate > 0 && h->param.rc.i_vbv_buffer_size > 0 )
 
400
    {
 
401
        if( h->param.rc.i_vbv_buffer_size < (int)(h->param.rc.i_vbv_max_bitrate / rc->fps) )
 
402
        {
 
403
            h->param.rc.i_vbv_buffer_size = h->param.rc.i_vbv_max_bitrate / rc->fps;
 
404
            x264_log( h, X264_LOG_WARNING, "VBV buffer size cannot be smaller than one frame, using %d kbit\n",
 
405
                      h->param.rc.i_vbv_buffer_size );
 
406
        }
 
407
 
 
408
        /* We don't support changing the ABR bitrate right now,
 
409
           so if the stream starts as CBR, keep it CBR. */
 
410
        if( rc->b_vbv_min_rate )
 
411
            h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate;
 
412
        rc->buffer_rate = h->param.rc.i_vbv_max_bitrate * 1000. / rc->fps;
 
413
        rc->buffer_size = h->param.rc.i_vbv_buffer_size * 1000.;
 
414
        rc->single_frame_vbv = rc->buffer_rate * 1.1 > rc->buffer_size;
 
415
        rc->cbr_decay = 1.0 - rc->buffer_rate / rc->buffer_size
 
416
                      * 0.5 * X264_MAX(0, 1.5 - rc->buffer_rate * rc->fps / rc->bitrate);
 
417
        if( b_init )
 
418
        {
 
419
            if( h->param.rc.f_vbv_buffer_init > 1. )
 
420
                h->param.rc.f_vbv_buffer_init = x264_clip3f( h->param.rc.f_vbv_buffer_init / h->param.rc.i_vbv_buffer_size, 0, 1 );
 
421
            h->param.rc.f_vbv_buffer_init = X264_MAX( h->param.rc.f_vbv_buffer_init, rc->buffer_rate / rc->buffer_size );
 
422
            rc->buffer_fill_final = rc->buffer_size * h->param.rc.f_vbv_buffer_init;
 
423
            rc->b_vbv = 1;
 
424
            rc->b_vbv_min_rate = !rc->b_2pass
 
425
                          && h->param.rc.i_rc_method == X264_RC_ABR
 
426
                          && h->param.rc.i_vbv_max_bitrate <= h->param.rc.i_bitrate;
 
427
        }
 
428
    }
 
429
    if( h->param.rc.i_rc_method == X264_RC_CRF )
 
430
    {
 
431
        /* Arbitrary rescaling to make CRF somewhat similar to QP.
 
432
         * Try to compensate for MB-tree's effects as well. */
 
433
        double base_cplx = h->mb.i_mb_count * (h->param.i_bframe ? 120 : 80);
 
434
        double mbtree_offset = h->param.rc.b_mb_tree ? (1.0-h->param.rc.f_qcompress)*13.5 : 0;
 
435
        rc->rate_factor_constant = pow( base_cplx, 1 - rc->qcompress )
 
436
                                 / qp2qscale( h->param.rc.f_rf_constant + mbtree_offset );
 
437
    }
 
438
}
 
439
 
391
440
int x264_ratecontrol_new( x264_t *h )
392
441
{
393
442
    x264_ratecontrol_t *rc;
426
475
        x264_log(h, X264_LOG_ERROR, "constant rate-factor is incompatible with 2pass.\n");
427
476
        return -1;
428
477
    }
429
 
    if( h->param.rc.i_vbv_buffer_size )
430
 
    {
431
 
        if( h->param.rc.i_rc_method == X264_RC_CQP )
432
 
        {
433
 
            x264_log(h, X264_LOG_WARNING, "VBV is incompatible with constant QP, ignored.\n");
434
 
            h->param.rc.i_vbv_max_bitrate = 0;
435
 
            h->param.rc.i_vbv_buffer_size = 0;
436
 
        }
437
 
        else if( h->param.rc.i_vbv_max_bitrate == 0 )
438
 
        {
439
 
            if( h->param.rc.i_rc_method == X264_RC_ABR )
440
 
            {
441
 
                x264_log( h, X264_LOG_INFO, "VBV maxrate unspecified, assuming CBR\n" );
442
 
                h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate;
443
 
            }
444
 
            else
445
 
            {
446
 
                x264_log( h, X264_LOG_INFO, "VBV bufsize set but maxrate unspecified, ignored\n" );
447
 
                h->param.rc.i_vbv_buffer_size = 0;
448
 
            }
449
 
        }
450
 
    }
451
 
    if( h->param.rc.i_vbv_max_bitrate < h->param.rc.i_bitrate &&
452
 
        h->param.rc.i_vbv_max_bitrate > 0)
453
 
        x264_log(h, X264_LOG_WARNING, "max bitrate less than average bitrate, ignored.\n");
454
 
    else if( h->param.rc.i_vbv_max_bitrate > 0 &&
455
 
             h->param.rc.i_vbv_buffer_size > 0 )
456
 
    {
457
 
        if( h->param.rc.i_vbv_buffer_size < (int)(h->param.rc.i_vbv_max_bitrate / rc->fps) )
458
 
        {
459
 
            h->param.rc.i_vbv_buffer_size = h->param.rc.i_vbv_max_bitrate / rc->fps;
460
 
            x264_log( h, X264_LOG_WARNING, "VBV buffer size cannot be smaller than one frame, using %d kbit\n",
461
 
                      h->param.rc.i_vbv_buffer_size );
462
 
        }
463
 
        if( h->param.rc.f_vbv_buffer_init > 1. )
464
 
            h->param.rc.f_vbv_buffer_init = x264_clip3f( h->param.rc.f_vbv_buffer_init / h->param.rc.i_vbv_buffer_size, 0, 1 );
465
 
        rc->buffer_rate = h->param.rc.i_vbv_max_bitrate * 1000. / rc->fps;
466
 
        rc->buffer_size = h->param.rc.i_vbv_buffer_size * 1000.;
467
 
        rc->single_frame_vbv = rc->buffer_rate * 1.1 > rc->buffer_size;
468
 
        h->param.rc.f_vbv_buffer_init = X264_MAX( h->param.rc.f_vbv_buffer_init, rc->buffer_rate / rc->buffer_size );
469
 
        rc->buffer_fill_final = rc->buffer_size * h->param.rc.f_vbv_buffer_init;
470
 
        rc->cbr_decay = 1.0 - rc->buffer_rate / rc->buffer_size
471
 
                      * 0.5 * X264_MAX(0, 1.5 - rc->buffer_rate * rc->fps / rc->bitrate);
472
 
        rc->b_vbv = 1;
473
 
        rc->b_vbv_min_rate = !rc->b_2pass
474
 
                          && h->param.rc.i_rc_method == X264_RC_ABR
475
 
                          && h->param.rc.i_vbv_max_bitrate <= h->param.rc.i_bitrate;
476
 
    }
477
 
    else if( h->param.rc.i_vbv_max_bitrate )
478
 
    {
479
 
        x264_log(h, X264_LOG_WARNING, "VBV maxrate specified, but no bufsize.\n");
480
 
        h->param.rc.i_vbv_max_bitrate = 0;
481
 
    }
482
 
    if(rc->rate_tolerance < 0.01)
 
478
 
 
479
    x264_ratecontrol_init_reconfigurable( h, 1 );
 
480
 
 
481
    if( rc->rate_tolerance < 0.01 )
483
482
    {
484
483
        x264_log(h, X264_LOG_WARNING, "bitrate tolerance too small, using .01\n");
485
484
        rc->rate_tolerance = 0.01;
499
498
        rc->last_non_b_pict_type = SLICE_TYPE_I;
500
499
    }
501
500
 
502
 
    if( h->param.rc.i_rc_method == X264_RC_CRF )
503
 
    {
504
 
        /* Arbitrary rescaling to make CRF somewhat similar to QP.
505
 
         * Try to compensate for MB-tree's effects as well. */
506
 
        double base_cplx = h->mb.i_mb_count * (h->param.i_bframe ? 120 : 80);
507
 
        double mbtree_offset = h->param.rc.b_mb_tree ? (1.0-h->param.rc.f_qcompress)*13.5 : 0;
508
 
        rc->rate_factor_constant = pow( base_cplx, 1 - rc->qcompress )
509
 
                                 / qp2qscale( h->param.rc.f_rf_constant + mbtree_offset );
510
 
    }
511
 
 
512
501
    rc->ip_offset = 6.0 * log(h->param.rc.f_ip_factor) / log(2.0);
513
502
    rc->pb_offset = 6.0 * log(h->param.rc.f_pb_factor) / log(2.0);
514
503
    rc->qp_constant[SLICE_TYPE_P] = h->param.rc.i_qp_constant;
518
507
 
519
508
    rc->lstep = pow( 2, h->param.rc.i_qp_step / 6.0 );
520
509
    rc->last_qscale = qp2qscale(26);
521
 
    CHECKED_MALLOC( rc->pred, 5*sizeof(predictor_t) );
 
510
    int num_preds = h->param.b_sliced_threads * h->param.i_threads + 1;
 
511
    CHECKED_MALLOC( rc->pred, 5 * sizeof(predictor_t) * num_preds );
522
512
    CHECKED_MALLOC( rc->pred_b_from_p, sizeof(predictor_t) );
523
513
    for( i = 0; i < 5; i++ )
524
514
    {
525
515
        rc->last_qscale_for[i] = qp2qscale( ABR_INIT_QP );
526
516
        rc->lmin[i] = qp2qscale( h->param.rc.i_qp_min );
527
517
        rc->lmax[i] = qp2qscale( h->param.rc.i_qp_max );
528
 
        rc->pred[i].coeff= 2.0;
529
 
        rc->pred[i].count= 1.0;
530
 
        rc->pred[i].decay= 0.5;
531
 
        rc->pred[i].offset= 0.0;
 
518
        for( j = 0; j < num_preds; j++ )
 
519
        {
 
520
            rc->pred[i+j*5].coeff= 2.0;
 
521
            rc->pred[i+j*5].count= 1.0;
 
522
            rc->pred[i+j*5].decay= 0.5;
 
523
            rc->pred[i+j*5].offset= 0.0;
 
524
        }
532
525
        for( j = 0; j < 2; j++ )
533
526
        {
534
527
            rc->row_preds[i][j].coeff= .25;
999
992
    x264_free( rc );
1000
993
}
1001
994
 
1002
 
void x264_ratecontrol_set_estimated_size( x264_t *h, int bits )
1003
 
{
1004
 
    x264_pthread_mutex_lock( &h->fenc->mutex );
1005
 
    h->rc->frame_size_estimated = bits;
1006
 
    x264_pthread_mutex_unlock( &h->fenc->mutex );
1007
 
}
1008
 
 
1009
 
int x264_ratecontrol_get_estimated_size( x264_t const *h)
1010
 
{
1011
 
    int size;
1012
 
    x264_pthread_mutex_lock( &h->fenc->mutex );
1013
 
    size = h->rc->frame_size_estimated;
1014
 
    x264_pthread_mutex_unlock( &h->fenc->mutex );
1015
 
    return size;
1016
 
}
1017
 
 
1018
995
static void accum_p_qp_update( x264_t *h, float qp )
1019
996
{
1020
997
    x264_ratecontrol_t *rc = h->rc;
1186
1163
    /* tweak quality based on difference from predicted size */
1187
1164
    if( y < h->i_threadslice_end-1 )
1188
1165
    {
 
1166
        int i;
1189
1167
        int prev_row_qp = h->fdec->i_row_qp[y];
1190
1168
        int i_qp_max = X264_MIN( prev_row_qp + h->param.rc.i_qp_step, h->param.rc.i_qp_max );
1191
1169
        int i_qp_min = X264_MAX( prev_row_qp - h->param.rc.i_qp_step, h->param.rc.i_qp_min );
1199
1177
 
1200
1178
        float buffer_left_planned = rc->buffer_fill - rc->frame_size_planned;
1201
1179
        float slice_size_planned = h->param.b_sliced_threads ? rc->slice_size_planned : rc->frame_size_planned;
1202
 
        float size_of_other_slices = rc->frame_size_planned - slice_size_planned;
 
1180
        float size_of_other_slices = 0;
 
1181
        if( h->param.b_sliced_threads )
 
1182
        {
 
1183
            for( i = 0; i < h->param.i_threads; i++ )
 
1184
                if( h != h->thread[i] )
 
1185
                    size_of_other_slices += h->thread[i]->rc->frame_size_estimated;
 
1186
        }
 
1187
        else
 
1188
            rc->max_frame_error = X264_MAX( 0.05, 1.0 / (h->sps->i_mb_width) );
 
1189
 
1203
1190
        /* More threads means we have to be more cautious in letting ratecontrol use up extra bits. */
1204
1191
        float rc_tol = buffer_left_planned / h->param.i_threads * rc->rate_tolerance;
1205
 
        float max_frame_error = X264_MAX( 0.05, 1.0 / h->sps->i_mb_height );
1206
 
        int b1 = predict_row_size_sum( h, y, rc->qpm );
1207
 
 
1208
 
        /* Assume that if this slice has become larger than expected,
1209
 
         * the other slices will have gotten equally larger. */
1210
 
        b1 += X264_MAX( size_of_other_slices * b1 / slice_size_planned, size_of_other_slices );
 
1192
        int b1 = predict_row_size_sum( h, y, rc->qpm ) + size_of_other_slices;
1211
1193
 
1212
1194
        /* Don't modify the row QPs until a sufficent amount of the bits of the frame have been processed, in case a flat */
1213
1195
        /* area at the top of the frame was measured inaccurately. */
1214
 
        if( row_bits_so_far(h,y) < 0.05 * (rc->frame_size_planned-size_of_other_slices) )
 
1196
        if( row_bits_so_far( h, y ) < 0.05 * slice_size_planned )
1215
1197
            return;
1216
1198
 
1217
1199
        if( h->sh.i_type != SLICE_TYPE_I )
1226
1208
                   (b1 > rc->frame_size_planned && rc->qpm < rc->qp_novbv)) )
1227
1209
        {
1228
1210
            rc->qpm ++;
1229
 
            b1 = predict_row_size_sum( h, y, rc->qpm );
1230
 
            b1 += X264_MAX( size_of_other_slices * b1 / slice_size_planned, size_of_other_slices );
 
1211
            b1 = predict_row_size_sum( h, y, rc->qpm ) + size_of_other_slices;
1231
1212
        }
1232
1213
 
1233
1214
        while( rc->qpm > i_qp_min
1236
1217
               || b1 < (rc->buffer_fill - rc->buffer_size + rc->buffer_rate) * 1.1) )
1237
1218
        {
1238
1219
            rc->qpm --;
1239
 
            b1 = predict_row_size_sum( h, y, rc->qpm );
1240
 
            b1 += X264_MAX( size_of_other_slices * b1 / slice_size_planned, size_of_other_slices );
 
1220
            b1 = predict_row_size_sum( h, y, rc->qpm ) + size_of_other_slices;
1241
1221
        }
1242
1222
 
1243
1223
        /* avoid VBV underflow */
1244
1224
        while( (rc->qpm < h->param.rc.i_qp_max)
1245
 
               && (rc->buffer_fill - b1 < rc->buffer_rate * max_frame_error) )
 
1225
               && (rc->buffer_fill - b1 < rc->buffer_rate * rc->max_frame_error) )
1246
1226
        {
1247
1227
            rc->qpm ++;
1248
 
            b1 = predict_row_size_sum( h, y, rc->qpm );
1249
 
            b1 += X264_MAX( size_of_other_slices * b1 / slice_size_planned, size_of_other_slices );
 
1228
            b1 = predict_row_size_sum( h, y, rc->qpm ) + size_of_other_slices;
1250
1229
        }
1251
1230
 
1252
 
        x264_ratecontrol_set_estimated_size(h, b1);
 
1231
        h->rc->frame_size_estimated = predict_row_size_sum( h, y, rc->qpm );
1253
1232
    }
1254
1233
 
1255
1234
    /* loses the fractional part of the frame-wise qp */
1293
1272
                h->thread[i]->param.rc.b_stat_read = 0;
1294
1273
                h->thread[i]->param.i_bframe_adaptive = 0;
1295
1274
                h->thread[i]->param.i_scenecut_threshold = 0;
 
1275
                h->thread[i]->param.rc.b_mb_tree = 0;
1296
1276
                if( h->thread[i]->param.i_bframe > 1 )
1297
1277
                    h->thread[i]->param.i_bframe = 1;
1298
1278
            }
1577
1557
    if( rct->buffer_fill_final < 0 )
1578
1558
        x264_log( h, X264_LOG_WARNING, "VBV underflow (frame %d, %.0f bits)\n", h->i_frame, rct->buffer_fill_final );
1579
1559
    rct->buffer_fill_final = X264_MAX( rct->buffer_fill_final, 0 );
1580
 
    rct->buffer_fill_final += rct->buffer_rate;
1581
 
    rct->buffer_fill_final = X264_MIN( rct->buffer_fill_final, rct->buffer_size );
 
1560
    rct->buffer_fill_final += rcc->buffer_rate;
 
1561
    rct->buffer_fill_final = X264_MIN( rct->buffer_fill_final, rcc->buffer_size );
1582
1562
}
1583
1563
 
1584
1564
// provisionally update VBV according to the planned size of all frames currently in progress
1585
1565
static void update_vbv_plan( x264_t *h, int overhead )
1586
1566
{
1587
1567
    x264_ratecontrol_t *rcc = h->rc;
1588
 
    rcc->buffer_fill = h->thread[0]->rc->buffer_fill_final - overhead;
 
1568
    rcc->buffer_fill = h->thread[0]->rc->buffer_fill_final;
1589
1569
    if( h->i_thread_frames > 1 )
1590
1570
    {
1591
1571
        int j = h->rc - h->thread[0]->rc;
1596
1576
            double bits = t->rc->frame_size_planned;
1597
1577
            if( !t->b_thread_active )
1598
1578
                continue;
1599
 
            bits  = X264_MAX(bits, x264_ratecontrol_get_estimated_size(t));
 
1579
            bits  = X264_MAX(bits, t->rc->frame_size_estimated);
1600
1580
            rcc->buffer_fill -= bits;
1601
1581
            rcc->buffer_fill = X264_MAX( rcc->buffer_fill, 0 );
1602
1582
            rcc->buffer_fill += rcc->buffer_rate;
1603
1583
            rcc->buffer_fill = X264_MIN( rcc->buffer_fill, rcc->buffer_size );
1604
1584
        }
1605
1585
    }
 
1586
    rcc->buffer_fill = X264_MIN( rcc->buffer_fill, rcc->buffer_size );
 
1587
    rcc->buffer_fill -= overhead;
1606
1588
}
1607
1589
 
1608
1590
// apply VBV constraints and clip qscale to between lmin and lmax
1793
1775
            rcc->frame_size_planned = qscale2bits( &rce, q );
1794
1776
        else
1795
1777
            rcc->frame_size_planned = predict_size( rcc->pred_b_from_p, q, h->fref1[h->i_ref1-1]->i_satd );
1796
 
        x264_ratecontrol_set_estimated_size(h, rcc->frame_size_planned);
 
1778
        h->rc->frame_size_estimated = rcc->frame_size_planned;
1797
1779
 
1798
1780
        /* For row SATDs */
1799
1781
        if( rcc->b_vbv )
1802
1784
    }
1803
1785
    else
1804
1786
    {
1805
 
        double abr_buffer = 2 * rcc->rate_tolerance * rcc->bitrate * h->i_thread_frames;
 
1787
        double abr_buffer = 2 * rcc->rate_tolerance * rcc->bitrate;
1806
1788
 
1807
1789
        if( rcc->b_2pass )
1808
1790
        {
1809
 
            //FIXME adjust abr_buffer based on distance to the end of the video
1810
1791
            int64_t diff;
1811
1792
            int64_t predicted_bits = total_bits;
 
1793
            /* Adjust ABR buffer based on distance to the end of the video. */
 
1794
            if( rcc->num_entries > h->fenc->i_frame )
 
1795
                abr_buffer *= 0.5 * sqrt( rcc->num_entries - h->fenc->i_frame );
1812
1796
 
1813
1797
            if( rcc->b_vbv )
1814
1798
            {
1822
1806
                        double bits = t->rc->frame_size_planned;
1823
1807
                        if( !t->b_thread_active )
1824
1808
                            continue;
1825
 
                        bits  = X264_MAX(bits, x264_ratecontrol_get_estimated_size(t));
 
1809
                        bits  = X264_MAX(bits, t->rc->frame_size_estimated);
1826
1810
                        predicted_bits += (int64_t)bits;
1827
1811
                    }
1828
1812
                }
1963
1947
        /* Always use up the whole VBV in this case. */
1964
1948
        if( rcc->single_frame_vbv )
1965
1949
            rcc->frame_size_planned = rcc->buffer_rate;
1966
 
        x264_ratecontrol_set_estimated_size(h, rcc->frame_size_planned);
 
1950
        h->rc->frame_size_estimated = rcc->frame_size_planned;
1967
1951
        return q;
1968
1952
    }
1969
1953
}
1970
1954
 
 
1955
void x264_threads_normalize_predictors( x264_t *h )
 
1956
{
 
1957
    int i;
 
1958
    double totalsize = 0;
 
1959
    for( i = 0; i < h->param.i_threads; i++ )
 
1960
        totalsize += h->thread[i]->rc->slice_size_planned;
 
1961
    double factor = h->rc->frame_size_planned / totalsize;
 
1962
    for( i = 0; i < h->param.i_threads; i++ )
 
1963
        h->thread[i]->rc->slice_size_planned *= factor;
 
1964
}
 
1965
 
1971
1966
void x264_threads_distribute_ratecontrol( x264_t *h )
1972
1967
{
1973
 
    int i, row, totalsize = 0;
1974
 
    if( h->rc->b_vbv )
1975
 
        for( row = 0; row < h->sps->i_mb_height; row++ )
1976
 
            totalsize += h->fdec->i_row_satd[row];
 
1968
    int i, row;
 
1969
    x264_ratecontrol_t *rc = h->rc;
 
1970
 
 
1971
    /* Initialize row predictors */
 
1972
    if( h->i_frame == 0 )
 
1973
        for( i = 0; i < h->param.i_threads; i++ )
 
1974
        {
 
1975
            x264_ratecontrol_t *t = h->thread[i]->rc;
 
1976
            memcpy( t->row_preds, rc->row_preds, sizeof(rc->row_preds) );
 
1977
        }
 
1978
 
1977
1979
    for( i = 0; i < h->param.i_threads; i++ )
1978
1980
    {
1979
1981
        x264_t *t = h->thread[i];
1980
 
        x264_ratecontrol_t *rc = h->rc;
1981
 
        memcpy( t->rc, rc, sizeof(x264_ratecontrol_t) );
 
1982
        memcpy( t->rc, rc, offsetof(x264_ratecontrol_t, row_pred) );
 
1983
        t->rc->row_pred = &t->rc->row_preds[h->sh.i_type];
1982
1984
        /* Calculate the planned slice size. */
1983
 
        if( h->rc->b_vbv && rc->frame_size_planned )
 
1985
        if( rc->b_vbv && rc->frame_size_planned )
1984
1986
        {
1985
1987
            int size = 0;
1986
1988
            for( row = t->i_threadslice_start; row < t->i_threadslice_end; row++ )
1987
1989
                size += h->fdec->i_row_satd[row];
1988
 
            t->rc->slice_size_planned = size * rc->frame_size_planned / totalsize;
 
1990
            t->rc->slice_size_planned = predict_size( &rc->pred[h->sh.i_type + (i+1)*5], rc->qpm, size );
1989
1991
        }
1990
1992
        else
1991
1993
            t->rc->slice_size_planned = 0;
1992
1994
    }
 
1995
    if( rc->b_vbv && rc->frame_size_planned )
 
1996
    {
 
1997
        x264_threads_normalize_predictors( h );
 
1998
 
 
1999
        if( rc->single_frame_vbv )
 
2000
        {
 
2001
            /* Compensate for our max frame error threshold: give more bits (proportionally) to smaller slices. */
 
2002
            for( i = 0; i < h->param.i_threads; i++ )
 
2003
            {
 
2004
                x264_t *t = h->thread[i];
 
2005
                t->rc->max_frame_error = X264_MAX( 0.05, 1.0 / (t->i_threadslice_end - t->i_threadslice_start) );
 
2006
                t->rc->slice_size_planned += 2 * t->rc->max_frame_error * rc->frame_size_planned;
 
2007
            }
 
2008
            x264_threads_normalize_predictors( h );
 
2009
        }
 
2010
 
 
2011
        for( i = 0; i < h->param.i_threads; i++ )
 
2012
            h->thread[i]->rc->frame_size_estimated = h->thread[i]->rc->slice_size_planned;
 
2013
    }
1993
2014
}
1994
2015
 
1995
2016
void x264_threads_merge_ratecontrol( x264_t *h )
1996
2017
{
1997
 
    int i, j, k;
 
2018
    int i, row;
1998
2019
    x264_ratecontrol_t *rc = h->rc;
1999
2020
    x264_emms();
2000
2021
 
2001
 
    for( i = 1; i < h->param.i_threads; i++ )
 
2022
    for( i = 0; i < h->param.i_threads; i++ )
2002
2023
    {
2003
 
        x264_ratecontrol_t *t = h->thread[i]->rc;
2004
 
        rc->qpa_rc += t->qpa_rc;
2005
 
        rc->qpa_aq += t->qpa_aq;
2006
 
        for( j = 0; j < 5; j++ )
2007
 
            for( k = 0; k < 2; k++ )
2008
 
            {
2009
 
                rc->row_preds[j][k].coeff += t->row_preds[j][k].coeff;
2010
 
                rc->row_preds[j][k].offset += t->row_preds[j][k].offset;
2011
 
                rc->row_preds[j][k].count += t->row_preds[j][k].count;
2012
 
            }
2013
 
    }
2014
 
    for( j = 0; j < 5; j++ )
2015
 
        for( k = 0; k < 2; k++ )
 
2024
        x264_t *t = h->thread[i];
 
2025
        x264_ratecontrol_t *rct = h->thread[i]->rc;
 
2026
        if( h->param.rc.i_vbv_buffer_size )
2016
2027
        {
2017
 
            rc->row_preds[j][k].coeff /= h->param.i_threads;
2018
 
            rc->row_preds[j][k].offset /= h->param.i_threads;
2019
 
            rc->row_preds[j][k].count /= h->param.i_threads;
 
2028
            int size = 0;
 
2029
            for( row = t->i_threadslice_start; row < t->i_threadslice_end; row++ )
 
2030
                size += h->fdec->i_row_satd[row];
 
2031
            int bits = t->stat.frame.i_mv_bits + t->stat.frame.i_tex_bits + t->stat.frame.i_misc_bits;
 
2032
            int mb_count = (t->i_threadslice_end - t->i_threadslice_start) * h->sps->i_mb_width;
 
2033
            update_predictor( &rc->pred[h->sh.i_type+5*i], qp2qscale(rct->qpa_rc/mb_count), size, bits );
2020
2034
        }
 
2035
        if( !i )
 
2036
            continue;
 
2037
        rc->qpa_rc += rct->qpa_rc;
 
2038
        rc->qpa_aq += rct->qpa_aq;
 
2039
    }
2021
2040
}
2022
2041
 
2023
2042
void x264_thread_sync_ratecontrol( x264_t *cur, x264_t *prev, x264_t *next )
2027
2046
#define COPY(var) memcpy(&cur->rc->var, &prev->rc->var, sizeof(cur->rc->var))
2028
2047
        /* these vars are updated in x264_ratecontrol_start()
2029
2048
         * so copy them from the context that most recently started (prev)
2030
 
         * to the context that's about to start (cur).
2031
 
         */
 
2049
         * to the context that's about to start (cur). */
2032
2050
        COPY(accum_p_qp);
2033
2051
        COPY(accum_p_norm);
2034
2052
        COPY(last_satd);
2040
2058
        COPY(bframes);
2041
2059
        COPY(prev_zone);
2042
2060
        COPY(qpbuf_pos);
 
2061
        /* these vars can be updated by x264_ratecontrol_init_reconfigurable */
 
2062
        COPY(buffer_rate);
 
2063
        COPY(buffer_size);
 
2064
        COPY(single_frame_vbv);
 
2065
        COPY(cbr_decay);
 
2066
        COPY(b_vbv_min_rate);
 
2067
        COPY(rate_factor_constant);
 
2068
        COPY(bitrate);
2043
2069
#undef COPY
2044
2070
    }
2045
2071
    if( cur != next )
2047
2073
#define COPY(var) next->rc->var = cur->rc->var
2048
2074
        /* these vars are updated in x264_ratecontrol_end()
2049
2075
         * so copy them from the context that most recently ended (cur)
2050
 
         * to the context that's about to end (next)
2051
 
         */
 
2076
         * to the context that's about to end (next) */
2052
2077
        COPY(cplxr_sum);
2053
2078
        COPY(expected_bits_sum);
2054
2079
        COPY(wanted_bits_window);