~siretart/x264/trunk

« back to all changes in this revision

Viewing changes to encoder/slicetype.c

  • Committer: Fiona Glaser
  • Date: 2009-08-07 05:57:40 UTC
  • Revision ID: git-v1:835ccc3cec908b1febfd31613d3e6583628116b3
Macroblock-tree ratecontrol
On by default; can be turned off with --no-mbtree.
Uses a large lookahead to track temporal propagation of data and weight quality accordingly.
Requires a very large separate statsfile (2 bytes per macroblock) in multi-pass mode.
Doesn't work with b-pyramid yet.
Note that MB-tree inherently measures quality different from the standard qcomp method, so bitrates produced by CRF may change somewhat.
This makes the "medium" preset a bit slower.  Accordingly, make "fast" slower as well, and introduce a new preset "faster" between "fast" and "veryfast".
All presets "fast" and above will have MB-tree on.
Add a new option, --rc-lookahead, to control the distance MB tree looks ahead to perform propagation analysis.
Default is 40; larger values will be slower and require more memory but give more accurate results.
This value will be used in the future to control ratecontrol lookahead (VBV).
Add a new option, --no-psy, to disable all psy optimizations that don't improve PSNR or SSIM.
This disables psy-RD/trellis, but also other more subtle internal psy optimizations that can't be controlled directly via external parameters.
Quality improvement from MB-tree is about 2-70% depending on content.
Strength of MB-tree adjustments can be tweaked using qcompress; higher values mean lower MB-tree strength.
Note that MB-tree may perform slightly suboptimally on fades; this will be fixed by weighted prediction, which is coming soon.

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
    x264_me_t m[2];
64
64
    int i_bcost = COST_MAX;
65
65
    int l, i;
 
66
    int list_used = 0;
66
67
 
67
68
    h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
68
69
    h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fenc[0], FENC_STRIDE, &fenc->lowres[0][i_pel_offset], i_stride, 8 );
107
108
        h->mc.avg[PIXEL_8x8]( pix1, 16, src1, stride1, src2, stride2, i_bipred_weight ); \
108
109
        i_cost = penalty + h->pixf.mbcmp[PIXEL_8x8]( \
109
110
                           m[0].p_fenc[0], FENC_STRIDE, pix1, 16 ); \
110
 
        if( i_bcost > i_cost ) \
111
 
            i_bcost = i_cost; \
 
111
        COPY2_IF_LT( i_bcost, i_cost, list_used, 3 ); \
112
112
    }
113
113
 
114
114
    m[0].i_pixel = PIXEL_8x8;
138
138
            int i_cost;
139
139
            h->mc.avg[PIXEL_8x8]( pix1, 16, m[0].p_fref[0], m[0].i_stride[0], m[1].p_fref[0], m[1].i_stride[0], i_bipred_weight );
140
140
            i_cost = h->pixf.mbcmp[PIXEL_8x8]( m[0].p_fenc[0], FENC_STRIDE, pix1, 16 );
141
 
            if( i_bcost > i_cost )
142
 
                i_bcost = i_cost;
 
141
            COPY2_IF_LT( i_bcost, i_cost, list_used, 3 );
143
142
        }
144
143
    }
145
144
 
181
180
            *(uint32_t*)m[l].mv = *(uint32_t*)fenc_mvs[l];
182
181
            m[l].cost = *fenc_costs[l];
183
182
        }
184
 
        i_bcost = X264_MIN( i_bcost, m[l].cost );
 
183
        COPY2_IF_LT( i_bcost, m[l].cost, list_used, l+1 );
185
184
    }
186
185
 
187
186
    if( b_bidir && ( *(uint32_t*)m[0].mv || *(uint32_t*)m[1].mv ) )
188
187
        TRY_BIDIR( m[0].mv, m[1].mv, 5 );
189
188
 
 
189
    frames[b]->lowres_inter_types[b-p0][p1-b][i_mb_xy] = list_used;
 
190
 
190
191
lowres_intra_mb:
191
192
    /* forbid intra-mbs in B-frames, because it's rare and not worth checking */
192
193
    /* FIXME: Should we still forbid them now that we cache intra scores? */
193
 
    if( !b_bidir )
 
194
    if( !b_bidir || h->param.rc.b_mb_tree )
194
195
    {
195
196
        int i_icost, b_intra;
196
197
        if( !fenc->b_intra_calculated )
237
238
        }
238
239
        else
239
240
            i_icost = fenc->i_intra_cost[i_mb_xy];
240
 
        b_intra = i_icost < i_bcost;
241
 
        if( b_intra )
242
 
            i_bcost = i_icost;
243
 
        if(   (i_mb_x > 0 && i_mb_x < h->sps->i_mb_width - 1
244
 
            && i_mb_y > 0 && i_mb_y < h->sps->i_mb_height - 1)
245
 
            || h->sps->i_mb_width <= 2 || h->sps->i_mb_height <= 2 )
 
241
        if( !b_bidir )
246
242
        {
247
 
            fenc->i_intra_mbs[b-p0] += b_intra;
248
 
            fenc->i_cost_est[0][0] += i_icost;
 
243
            b_intra = i_icost < i_bcost;
 
244
            if( b_intra )
 
245
                i_bcost = i_icost;
 
246
            if(   (i_mb_x > 0 && i_mb_x < h->sps->i_mb_width - 1
 
247
                && i_mb_y > 0 && i_mb_y < h->sps->i_mb_height - 1)
 
248
                || h->sps->i_mb_width <= 2 || h->sps->i_mb_height <= 2 )
 
249
            {
 
250
                fenc->i_intra_mbs[b-p0] += b_intra;
 
251
                fenc->i_cost_est[0][0] += i_icost;
 
252
            }
249
253
        }
250
254
    }
251
255
 
 
256
    frames[b]->lowres_costs[b-p0][p1-b][i_mb_xy] = i_bcost;
 
257
 
252
258
    return i_bcost;
253
259
}
254
260
#undef TRY_BIDIR
262
268
                               x264_frame_t **frames, int p0, int p1, int b,
263
269
                               int b_intra_penalty )
264
270
{
 
271
 
265
272
    int i_score = 0;
266
273
    /* Don't use the AQ'd scores for slicetype decision. */
267
274
    int i_score_aq = 0;
299
306
 
300
307
        /* the edge mbs seem to reduce the predictive quality of the
301
308
         * whole frame's score, but are needed for a spatial distribution. */
302
 
        if( h->param.rc.i_vbv_buffer_size || h->sps->i_mb_width <= 2 || h->sps->i_mb_height <= 2 )
 
309
        if( h->param.rc.b_mb_tree || h->param.rc.i_vbv_buffer_size ||
 
310
            h->sps->i_mb_width <= 2 || h->sps->i_mb_height <= 2 )
303
311
        {
304
312
            for( h->mb.i_mb_y = h->sps->i_mb_height - 1; h->mb.i_mb_y >= 0; h->mb.i_mb_y-- )
305
313
            {
355
363
    return i_score;
356
364
}
357
365
 
358
 
#define MAX_LENGTH (X264_BFRAME_MAX*4)
 
366
/* If MB-tree changes the quantizers, we need to recalculate the frame cost without
 
367
 * re-running lookahead. */
 
368
static int x264_slicetype_frame_cost_recalculate( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames,
 
369
                                                  int p0, int p1, int b )
 
370
{
 
371
    int i_score = 0;
 
372
    int *row_satd = frames[b]->i_row_satds[b-p0][p1-b];
 
373
    x264_emms();
 
374
    for( h->mb.i_mb_y = h->sps->i_mb_height - 1; h->mb.i_mb_y >= 0; h->mb.i_mb_y-- )
 
375
    {
 
376
        row_satd[ h->mb.i_mb_y ] = 0;
 
377
        for( h->mb.i_mb_x = h->sps->i_mb_width - 1; h->mb.i_mb_x >= 0; h->mb.i_mb_x-- )
 
378
        {
 
379
            int i_mb_xy = h->mb.i_mb_x + h->mb.i_mb_y*h->mb.i_mb_stride;
 
380
            int i_mb_cost = frames[b]->lowres_costs[b-p0][p1-b][i_mb_xy];
 
381
            float qp_adj = frames[b]->f_qp_offset[i_mb_xy];
 
382
            i_mb_cost = (i_mb_cost * x264_exp2fix8(qp_adj*(-1.f/6.f)) + 128) >> 8;
 
383
            row_satd[ h->mb.i_mb_y ] += i_mb_cost;
 
384
            if( (h->mb.i_mb_y > 0 && h->mb.i_mb_y < h->sps->i_mb_height - 1 &&
 
385
                 h->mb.i_mb_x > 0 && h->mb.i_mb_x < h->sps->i_mb_width - 1) ||
 
386
                 h->sps->i_mb_width <= 2 || h->sps->i_mb_height <= 2 )
 
387
            {
 
388
                i_score += i_mb_cost;
 
389
            }
 
390
        }
 
391
    }
 
392
    return i_score;
 
393
}
 
394
 
 
395
static void x264_macroblock_tree_propagate( x264_t *h, x264_frame_t **frames, int p0, int p1, int b )
 
396
{
 
397
    x264_frame_t *refs[2] = {frames[p0],frames[p1]};
 
398
    int dist_scale_factor = p1 != p0 ? 128 : ( ((b-p0) << 8) + ((p1-p0) >> 1) ) / (p1-p0);
 
399
    int i_bipred_weight = h->param.analyse.b_weighted_bipred ? 64 - (dist_scale_factor>>2) : 32;
 
400
 
 
401
    for( h->mb.i_mb_y = 0; h->mb.i_mb_y < h->sps->i_mb_height; h->mb.i_mb_y++ )
 
402
    {
 
403
        for( h->mb.i_mb_x = 0; h->mb.i_mb_x < h->sps->i_mb_width; h->mb.i_mb_x++ )
 
404
        {
 
405
            int mb_index = h->mb.i_mb_x + h->mb.i_mb_y*h->mb.i_mb_stride;
 
406
            int inter_cost = frames[b]->lowres_costs[b-p0][p1-b][mb_index];
 
407
            int intra_cost = (frames[b]->i_intra_cost[mb_index] * frames[b]->i_inv_qscale_factor[mb_index]+128)>>8;
 
408
            int lists_used = frames[b]->lowres_inter_types[b-p0][p1-b][mb_index];
 
409
            /* The approximate amount of data that this block contains. */
 
410
            int propagate_amount = intra_cost + frames[b]->i_propagate_cost[mb_index];
 
411
 
 
412
            /* Divide by 64 for per-pixel summing. */
 
413
            propagate_amount = (((uint64_t)propagate_amount*(intra_cost-inter_cost)) / intra_cost + 32) >> 6;
 
414
 
 
415
            /* Don't propagate for an intra block. */
 
416
            if( inter_cost < intra_cost )
 
417
            {
 
418
                int mv[2][2], list;
 
419
                mv[0][0] = frames[b]->lowres_mvs[0][b-p0-1][mb_index][0];
 
420
                mv[0][1] = frames[b]->lowres_mvs[0][b-p0-1][mb_index][1];
 
421
                if( b != p1 )
 
422
                {
 
423
                    mv[1][0] = frames[b]->lowres_mvs[1][p1-b-1][mb_index][0];
 
424
                    mv[1][1] = frames[b]->lowres_mvs[1][p1-b-1][mb_index][1];
 
425
                }
 
426
 
 
427
                /* Follow the MVs to the previous frame(s). */
 
428
                for( list = 0; list < 2; list++ )
 
429
                    if( (lists_used >> list)&1 )
 
430
                    {
 
431
                        int x = mv[list][0];
 
432
                        int y = mv[list][1];
 
433
                        int listamount = propagate_amount;
 
434
                        int mbx = (x>>5)+h->mb.i_mb_x;
 
435
                        int mby = ((y>>5)+h->mb.i_mb_y);
 
436
                        int idx0 = mbx + mby*h->mb.i_mb_stride;
 
437
                        int idx1 = idx0 + 1;
 
438
                        int idx2 = idx0 + h->mb.i_mb_stride;
 
439
                        int idx3 = idx0 + h->mb.i_mb_stride + 1;
 
440
                        int idx0weight = (32-(y&31))*(32-(x&31));
 
441
                        int idx1weight = (32-(y&31))*(x&31);
 
442
                        int idx2weight = (y&31)*(32-(x&31));
 
443
                        int idx3weight = (y&31)*(x&31);
 
444
 
 
445
                        /* Apply bipred weighting. */
 
446
                        if( lists_used == 3 )
 
447
                            listamount = (listamount * (list?(64-i_bipred_weight):i_bipred_weight) + 32) >> 6;
 
448
 
 
449
#define CLIP_ADD(s,x) (s) = X264_MIN((s)+(x),(1<<16)-1)
 
450
 
 
451
                        /* We could just clip the MVs, but pixels that lie outside the frame probably shouldn't
 
452
                         * be counted. */
 
453
                        if( mbx < h->sps->i_mb_width-1 && mby < h->sps->i_mb_height-1 && mbx >= 0 && mby >= 0 )
 
454
                        {
 
455
                            CLIP_ADD( refs[list]->i_propagate_cost[idx0], (listamount*idx0weight+8)>>4 );
 
456
                            CLIP_ADD( refs[list]->i_propagate_cost[idx1], (listamount*idx1weight+8)>>4 );
 
457
                            CLIP_ADD( refs[list]->i_propagate_cost[idx2], (listamount*idx2weight+8)>>4 );
 
458
                            CLIP_ADD( refs[list]->i_propagate_cost[idx3], (listamount*idx3weight+8)>>4 );
 
459
                        }
 
460
                        else /* Check offsets individually */
 
461
                        {
 
462
                            if( mbx < h->sps->i_mb_width && mby < h->sps->i_mb_height && mbx >= 0 && mby >= 0 )
 
463
                                CLIP_ADD( refs[list]->i_propagate_cost[idx0], (listamount*idx0weight+8)>>4 );
 
464
                            if( mbx+1 < h->sps->i_mb_width && mby < h->sps->i_mb_height && mbx+1 >= 0 && mby >= 0 )
 
465
                                CLIP_ADD( refs[list]->i_propagate_cost[idx1], (listamount*idx1weight+8)>>4 );
 
466
                            if( mbx < h->sps->i_mb_width && mby+1 < h->sps->i_mb_height && mbx >= 0 && mby+1 >= 0 )
 
467
                                CLIP_ADD( refs[list]->i_propagate_cost[idx2], (listamount*idx2weight+8)>>4 );
 
468
                            if( mbx+1 < h->sps->i_mb_width && mby+1 < h->sps->i_mb_height && mbx+1 >= 0 && mby+1 >= 0 )
 
469
                                CLIP_ADD( refs[list]->i_propagate_cost[idx3], (listamount*idx3weight+8)>>4 );
 
470
                        }
 
471
                    }
 
472
            }
 
473
        }
 
474
    }
 
475
}
 
476
 
 
477
static void x264_macroblock_tree( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames, int num_frames, int b_intra )
 
478
{
 
479
    int i, idx = !b_intra;
 
480
    int last_nonb, cur_nonb = 1;
 
481
    if( b_intra )
 
482
       x264_slicetype_frame_cost( h, a, frames, 0, 0, 0, 0 );
 
483
 
 
484
    i = num_frames-1;
 
485
    while( i > 0 && frames[i]->i_type == X264_TYPE_B )
 
486
        i--;
 
487
    last_nonb = i;
 
488
 
 
489
    if( last_nonb < 0 )
 
490
        return;
 
491
 
 
492
    memset( frames[last_nonb]->i_propagate_cost, 0, h->mb.i_mb_count * sizeof(uint32_t) );
 
493
    while( i-- > idx )
 
494
    {
 
495
        cur_nonb = i;
 
496
        while( frames[cur_nonb]->i_type == X264_TYPE_B && cur_nonb > 0 )
 
497
            cur_nonb--;
 
498
        if( cur_nonb < idx )
 
499
            break;
 
500
        x264_slicetype_frame_cost( h, a, frames, cur_nonb, last_nonb, last_nonb, 0 );
 
501
        memset( frames[cur_nonb]->i_propagate_cost, 0, h->mb.i_mb_count * sizeof(uint32_t) );
 
502
        x264_macroblock_tree_propagate( h, frames, cur_nonb, last_nonb, last_nonb );
 
503
        while( frames[i]->i_type == X264_TYPE_B && i > 0 )
 
504
        {
 
505
            x264_slicetype_frame_cost( h, a, frames, cur_nonb, last_nonb, i, 0 );
 
506
            memset( frames[i]->i_propagate_cost, 0, h->mb.i_mb_count * sizeof(uint32_t) );
 
507
            x264_macroblock_tree_propagate( h, frames, cur_nonb, last_nonb, i );
 
508
            i--;
 
509
        }
 
510
        last_nonb = cur_nonb;
 
511
    }
 
512
    x264_emms();
 
513
 
 
514
    for( h->mb.i_mb_y = 0; h->mb.i_mb_y < h->sps->i_mb_height; h->mb.i_mb_y++ )
 
515
    {
 
516
        for( h->mb.i_mb_x = 0; h->mb.i_mb_x < h->sps->i_mb_width; h->mb.i_mb_x++ )
 
517
        {
 
518
            int mb_index = h->mb.i_mb_x + h->mb.i_mb_y*h->mb.i_mb_stride;
 
519
            int intra_cost = (frames[last_nonb]->i_intra_cost[mb_index] * frames[last_nonb]->i_inv_qscale_factor[mb_index]+128)>>8;
 
520
 
 
521
            if( intra_cost )
 
522
            {
 
523
                int propagate_cost = frames[last_nonb]->i_propagate_cost[mb_index];
 
524
                float log2_ratio = x264_log2(intra_cost + propagate_cost) - x264_log2(intra_cost);
 
525
                /* Allow the constant to be adjusted via qcompress, since the two
 
526
                 * concepts are very similar. */
 
527
                frames[last_nonb]->f_qp_offset[mb_index] -= 5.0 * (1.0 - h->param.rc.f_qcompress) * log2_ratio;
 
528
            }
 
529
        }
 
530
    }
 
531
}
359
532
 
360
533
static int x264_slicetype_path_cost( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames, char *path, int threshold )
361
534
{
393
566
/* Uses strings due to the fact that the speed of the control functions is
394
567
   negligable compared to the cost of running slicetype_frame_cost, and because
395
568
   it makes debugging easier. */
396
 
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)[MAX_LENGTH] )
 
569
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] )
397
570
{
398
 
    char paths[X264_BFRAME_MAX+2][MAX_LENGTH] = {{0}};
 
571
    char paths[X264_BFRAME_MAX+2][X264_LOOKAHEAD_MAX] = {{0}};
399
572
    int num_paths = X264_MIN(max_bframes+1, length);
400
573
    int suffix_size, loc, path;
401
574
    int best_cost = COST_MAX;
402
575
    int best_path_index = 0;
403
 
    length = X264_MIN(length,MAX_LENGTH);
 
576
    length = X264_MIN(length,X264_LOOKAHEAD_MAX);
404
577
 
405
578
    /* Iterate over all currently possible paths and add suffixes to each one */
406
579
    for( suffix_size = 0; suffix_size < num_paths; suffix_size++ )
426
599
    memcpy( best_paths[length], paths[best_path_index], length );
427
600
}
428
601
 
429
 
static int x264_slicetype_path_search( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames, int length, int bframes, int buffer )
430
 
{
431
 
    char best_paths[MAX_LENGTH][MAX_LENGTH] = {"","P"};
432
 
    int n;
433
 
    for( n = 2; n < length-1; n++ )
434
 
        x264_slicetype_path( h, a, frames, n, bframes, buffer, best_paths );
435
 
    return strspn( best_paths[length-2], "B" );
436
 
}
437
 
 
438
602
static int scenecut( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames, int p0, int p1 )
439
603
{
440
604
    x264_frame_t *frame = frames[p1];
477
641
    return res;
478
642
}
479
643
 
480
 
static void x264_slicetype_analyse( x264_t *h )
 
644
static void x264_slicetype_analyse( x264_t *h, int keyframe )
481
645
{
482
646
    x264_mb_analysis_t a;
483
 
    x264_frame_t *frames[X264_BFRAME_MAX*4+3] = { NULL, };
 
647
    x264_frame_t *frames[X264_LOOKAHEAD_MAX+3] = { NULL, };
484
648
    int num_frames;
485
649
    int keyint_limit;
486
 
    int j;
 
650
    int i,j;
487
651
    int i_mb_count = NUM_MBS;
488
652
    int cost1p0, cost2p0, cost1b1, cost2p1;
489
653
    int idr_frame_type;
497
661
        frames[j+1] = h->frames.next[j];
498
662
    keyint_limit = h->param.i_keyint_max - frames[0]->i_frame + h->frames.i_last_idr - 1;
499
663
    num_frames = X264_MIN( j, keyint_limit );
500
 
    if( num_frames == 0 )
 
664
 
 
665
    if( num_frames == 0 && (!j || !h->param.rc.b_mb_tree) )
501
666
        return;
502
667
 
503
668
    x264_lowres_context_init( h, &a );
504
669
    idr_frame_type = frames[1]->i_frame - h->frames.i_last_idr >= h->param.i_keyint_min ? X264_TYPE_IDR : X264_TYPE_I;
505
670
 
506
 
    if( num_frames == 1 )
 
671
    if( num_frames == 1 && !h->param.rc.b_mb_tree )
507
672
    {
508
 
no_b_frames:
509
673
        frames[1]->i_type = X264_TYPE_P;
510
674
        if( h->param.i_scenecut_threshold && scenecut( h, &a, frames, 0, 1 ) )
511
675
            frames[1]->i_type = idr_frame_type;
512
676
        return;
513
677
    }
514
678
 
515
 
    if( h->param.i_bframe_adaptive == X264_B_ADAPT_TRELLIS )
516
 
    {
517
 
        int num_bframes;
518
 
        int max_bframes = X264_MIN(num_frames-1, h->param.i_bframe);
519
 
        if( h->param.i_scenecut_threshold && scenecut( h, &a, frames, 0, 1 ) )
520
 
        {
521
 
            frames[1]->i_type = idr_frame_type;
522
 
            return;
523
 
        }
524
 
        num_bframes = x264_slicetype_path_search( h, &a, frames, num_frames, max_bframes, num_frames-max_bframes );
525
 
        assert(num_bframes < num_frames);
526
 
 
 
679
    /* This is important psy-wise: if we have a non-scenecut keyframe,
 
680
     * there will be significant visual artifacts if the frames just before
 
681
     * go down in quality due to being referenced less, despite it being
 
682
     * more RD-optimal. */
 
683
    if( h->param.analyse.b_psy && h->param.rc.b_mb_tree )
 
684
        num_frames = j;
 
685
 
 
686
    char best_paths[X264_LOOKAHEAD_MAX][X264_LOOKAHEAD_MAX] = {"","P"};
 
687
    int n;
 
688
    int num_bframes = 0;
 
689
    int max_bframes = X264_MIN(num_frames-1, h->param.i_bframe);
 
690
    int num_analysed_frames = num_frames;
 
691
    int reset_start;
 
692
    if( h->param.i_scenecut_threshold && scenecut( h, &a, frames, 0, 1 ) )
 
693
    {
 
694
        frames[1]->i_type = idr_frame_type;
 
695
        return;
 
696
    }
 
697
 
 
698
    if( h->param.i_bframe )
 
699
    {
 
700
        if( h->param.i_bframe_adaptive == X264_B_ADAPT_TRELLIS )
 
701
        {
 
702
            /* Perform the frametype analysis. */
 
703
            for( n = 2; n < num_frames-1; n++ )
 
704
                x264_slicetype_path( h, &a, frames, n, max_bframes, num_frames-max_bframes, best_paths );
 
705
            num_bframes = strspn( best_paths[num_frames-2], "B" );
 
706
            /* Load the results of the analysis into the frame types. */
 
707
            for( j = 1; j < num_frames; j++ )
 
708
                frames[j]->i_type = best_paths[num_frames-2][j-1] == 'B' ? X264_TYPE_B : X264_TYPE_P;
 
709
            frames[num_frames]->i_type = X264_TYPE_P;
 
710
        }
 
711
        else if( h->param.i_bframe_adaptive == X264_B_ADAPT_FAST )
 
712
        {
 
713
            for( i = 0; i < num_frames-(2-!i); )
 
714
            {
 
715
                cost2p1 = x264_slicetype_frame_cost( h, &a, frames, i+0, i+2, i+2, 1 );
 
716
                if( frames[i+2]->i_intra_mbs[2] > i_mb_count / 2 )
 
717
                {
 
718
                    frames[i+1]->i_type = X264_TYPE_P;
 
719
                    frames[i+2]->i_type = X264_TYPE_P;
 
720
                    i += 2;
 
721
                    continue;
 
722
                }
 
723
 
 
724
                cost1b1 = x264_slicetype_frame_cost( h, &a, frames, i+0, i+2, i+1, 0 );
 
725
                cost1p0 = x264_slicetype_frame_cost( h, &a, frames, i+0, i+1, i+1, 0 );
 
726
                cost2p0 = x264_slicetype_frame_cost( h, &a, frames, i+1, i+2, i+2, 0 );
 
727
 
 
728
                if( cost1p0 + cost2p0 < cost1b1 + cost2p1 )
 
729
                {
 
730
                    frames[i+1]->i_type = X264_TYPE_P;
 
731
                    frames[i+2]->i_type = X264_TYPE_P;
 
732
                    i += 2;
 
733
                    continue;
 
734
                }
 
735
 
 
736
                // arbitrary and untuned
 
737
                #define INTER_THRESH 300
 
738
                #define P_SENS_BIAS (50 - h->param.i_bframe_bias)
 
739
                frames[i+1]->i_type = X264_TYPE_B;
 
740
                frames[i+2]->i_type = X264_TYPE_P;
 
741
 
 
742
                for( j = i+2; j <= X264_MIN( h->param.i_bframe, num_frames-2 ); j++ )
 
743
                {
 
744
                    int pthresh = X264_MAX(INTER_THRESH - P_SENS_BIAS * (j-i-1), INTER_THRESH/10);
 
745
                    int pcost = x264_slicetype_frame_cost( h, &a, frames, i+0, j+1, j+1, 1 );
 
746
 
 
747
                    if( pcost > pthresh*i_mb_count || frames[j+1]->i_intra_mbs[j-i+1] > i_mb_count/3 )
 
748
                    {
 
749
                        frames[j]->i_type = X264_TYPE_P;
 
750
                        break;
 
751
                    }
 
752
                    else
 
753
                        frames[j]->i_type = X264_TYPE_B;
 
754
                }
 
755
                i = j;
 
756
            }
 
757
            frames[i+!i]->i_type = X264_TYPE_P;
 
758
            num_bframes = 0;
 
759
            while( num_bframes < num_frames && frames[num_bframes+1]->i_type == X264_TYPE_B )
 
760
                num_bframes++;
 
761
        }
 
762
        else
 
763
        {
 
764
            num_bframes = X264_MIN(num_frames-1, h->param.i_bframe);
 
765
            for( j = 1; j < num_frames; j++ )
 
766
                frames[j]->i_type = (j%(num_bframes+1)) ? X264_TYPE_B : X264_TYPE_P;
 
767
            frames[num_frames]->i_type = X264_TYPE_P;
 
768
        }
 
769
 
 
770
        /* Check scenecut on the first minigop. */
527
771
        for( j = 1; j < num_bframes+1; j++ )
528
 
        {
529
772
            if( h->param.i_scenecut_threshold && scenecut( h, &a, frames, j, j+1 ) )
530
773
            {
531
774
                frames[j]->i_type = X264_TYPE_P;
532
 
                return;
533
 
            }
534
 
            frames[j]->i_type = X264_TYPE_B;
535
 
        }
536
 
        frames[num_bframes+1]->i_type = X264_TYPE_P;
537
 
    }
538
 
    else if( h->param.i_bframe_adaptive == X264_B_ADAPT_FAST )
539
 
    {
540
 
        cost2p1 = x264_slicetype_frame_cost( h, &a, frames, 0, 2, 2, 1 );
541
 
        if( frames[2]->i_intra_mbs[2] > i_mb_count / 2 )
542
 
            goto no_b_frames;
543
 
 
544
 
        cost1b1 = x264_slicetype_frame_cost( h, &a, frames, 0, 2, 1, 0 );
545
 
        cost1p0 = x264_slicetype_frame_cost( h, &a, frames, 0, 1, 1, 0 );
546
 
        cost2p0 = x264_slicetype_frame_cost( h, &a, frames, 1, 2, 2, 0 );
547
 
 
548
 
        if( cost1p0 + cost2p0 < cost1b1 + cost2p1 )
549
 
            goto no_b_frames;
550
 
 
551
 
        // arbitrary and untuned
552
 
        #define INTER_THRESH 300
553
 
        #define P_SENS_BIAS (50 - h->param.i_bframe_bias)
554
 
        frames[1]->i_type = X264_TYPE_B;
555
 
 
556
 
        for( j = 2; j <= X264_MIN( h->param.i_bframe, num_frames-1 ); j++ )
557
 
        {
558
 
            int pthresh = X264_MAX(INTER_THRESH - P_SENS_BIAS * (j-1), INTER_THRESH/10);
559
 
            int pcost = x264_slicetype_frame_cost( h, &a, frames, 0, j+1, j+1, 1 );
560
 
 
561
 
            if( pcost > pthresh*i_mb_count || frames[j+1]->i_intra_mbs[j+1] > i_mb_count/3 )
562
 
            {
563
 
                frames[j]->i_type = X264_TYPE_P;
 
775
                num_analysed_frames = j;
564
776
                break;
565
777
            }
566
 
            else
567
 
                frames[j]->i_type = X264_TYPE_B;
568
 
        }
 
778
 
 
779
        reset_start = keyframe ? 1 : X264_MIN( num_bframes+2, num_analysed_frames+1 );
569
780
    }
570
781
    else
571
782
    {
572
 
        int max_bframes = X264_MIN(num_frames-1, h->param.i_bframe);
573
 
        if( h->param.i_scenecut_threshold && scenecut( h, &a, frames, 0, 1 ) )
574
 
        {
575
 
            frames[1]->i_type = idr_frame_type;
576
 
            return;
577
 
        }
578
 
 
579
 
        for( j = 1; j < max_bframes+1; j++ )
580
 
        {
581
 
            if( h->param.i_scenecut_threshold && scenecut( h, &a, frames, j, j+1 ) )
 
783
        for( j = 1; j < num_frames; j++ )
 
784
            frames[j]->i_type = X264_TYPE_P;
 
785
        reset_start = !keyframe + 1;
 
786
    }
 
787
 
 
788
    /* Perform the actual macroblock tree analysis.
 
789
     * Don't go farther than the lookahead parameter; this helps in short GOPs. */
 
790
    if( h->param.rc.b_mb_tree )
 
791
        x264_macroblock_tree( h, &a, frames, X264_MIN(num_analysed_frames, h->param.rc.i_lookahead), keyframe );
 
792
 
 
793
    /* Enforce keyframe limit. */
 
794
    if( h->param.i_bframe )
 
795
        for( j = 0; j <= num_bframes; j++ )
 
796
            if( j+1 > keyint_limit )
582
797
            {
583
 
                frames[j]->i_type = X264_TYPE_P;
584
 
                return;
 
798
                if( j )
 
799
                    frames[j]->i_type = X264_TYPE_P;
 
800
                frames[j+1]->i_type = idr_frame_type;
 
801
                reset_start = j+2;
 
802
                break;
585
803
            }
586
 
            frames[j]->i_type = X264_TYPE_B;
587
 
        }
588
 
        frames[max_bframes+1]->i_type = X264_TYPE_P;
589
 
    }
 
804
 
 
805
    /* Restore frametypes for all frames that haven't actually been decided yet. */
 
806
    for( j = reset_start; j <= num_frames; j++ )
 
807
        frames[j]->i_type = X264_TYPE_AUTO;
590
808
}
591
809
 
592
810
void x264_slicetype_decide( x264_t *h )
606
824
                x264_ratecontrol_slice_type( h, h->frames.next[i]->i_frame );
607
825
    }
608
826
    else if( (h->param.i_bframe && h->param.i_bframe_adaptive)
609
 
             || h->param.i_scenecut_threshold )
610
 
        x264_slicetype_analyse( h );
 
827
             || h->param.i_scenecut_threshold
 
828
             || h->param.rc.b_mb_tree )
 
829
        x264_slicetype_analyse( h, 0 );
611
830
 
612
831
    for( bframes = 0;; bframes++ )
613
832
    {
645
864
                frm->i_type = X264_TYPE_P;
646
865
        }
647
866
 
648
 
        if( frm->i_type == X264_TYPE_AUTO ) frm->i_type = X264_TYPE_B;
 
867
        if( frm->i_type == X264_TYPE_AUTO )
 
868
            frm->i_type = X264_TYPE_B;
 
869
 
649
870
        else if( !IS_X264_TYPE_B( frm->i_type ) ) break;
650
871
    }
651
872
}
653
874
int x264_rc_analyse_slice( x264_t *h )
654
875
{
655
876
    x264_mb_analysis_t a;
656
 
    x264_frame_t *frames[X264_BFRAME_MAX*4+2] = { NULL, };
 
877
    x264_frame_t *frames[X264_LOOKAHEAD_MAX+2] = { NULL, };
657
878
    int p0=0, p1, b;
658
879
    int cost;
659
880
 
662
883
    if( IS_X264_TYPE_I(h->fenc->i_type) )
663
884
    {
664
885
        p1 = b = 0;
 
886
        /* For MB-tree, we have to perform propagation analysis on I-frames too. */
 
887
        if( h->param.rc.b_mb_tree )
 
888
        {
 
889
            h->frames.last_nonb = h->fenc;
 
890
            x264_slicetype_analyse( h, 1 );
 
891
        }
665
892
    }
666
893
    else if( X264_TYPE_P == h->fenc->i_type )
667
894
    {
680
907
    frames[p0] = h->fref0[0];
681
908
    frames[b] = h->fenc;
682
909
 
683
 
    cost = x264_slicetype_frame_cost( h, &a, frames, p0, p1, b, 0 );
 
910
    if( h->param.rc.b_mb_tree )
 
911
        cost = x264_slicetype_frame_cost_recalculate( h, &a, frames, p0, p1, b );
 
912
    else
 
913
    {
 
914
        cost = x264_slicetype_frame_cost( h, &a, frames, p0, p1, b, 0 );
684
915
 
685
 
    /* In AQ, use the weighted score instead. */
686
 
    if( h->param.rc.i_aq_mode )
687
 
        cost = frames[b]->i_cost_est[b-p0][p1-b];
 
916
        /* In AQ, use the weighted score instead. */
 
917
        if( h->param.rc.i_aq_mode )
 
918
            cost = frames[b]->i_cost_est[b-p0][p1-b];
 
919
    }
688
920
 
689
921
    h->fenc->i_row_satd = h->fenc->i_row_satds[b-p0][p1-b];
690
922
    h->fdec->i_row_satd = h->fdec->i_row_satds[b-p0][p1-b];