~siretart/x264/trunk

« back to all changes in this revision

Viewing changes to encoder/slicetype.c

  • Committer: Fiona Glaser
  • Date: 2008-09-14 00:27:03 UTC
  • Revision ID: git-v1:80458ffcd62f0852e7092176b7b155bdfd3d5a82
Move adaptive quantization to before ratecontrol, eliminate qcomp bias
This change improves VBV accuracy and improves bit distribution in CRF and 2pass.
Instead of being applied after ratecontrol, AQ becomes part of the complexity measure that ratecontrol uses.
This allows for modularity for changes to AQ; a new AQ algorithm can be introduced simply by introducing a new aq_mode and a corresponding if in adaptive_quant_frame.
This also allows quantizer field smoothing, since quantizers are calculated beofrehand rather during encoding.
Since there is no more reason for it, aq_mode 1 is removed.  The new mode 1 is in a sense a merger of the old modes 1 and 2.
WARNING: This change redefines CRF when using AQ, so output bitrate for a given CRF may be significantly different from before this change!

Show diffs side-by-side

added added

removed removed

Lines of Context:
248
248
                               int b_intra_penalty )
249
249
{
250
250
    int i_score = 0;
 
251
    /* Don't use the AQ'd scores for slicetype decision. */
 
252
    int i_score_aq = 0;
251
253
 
252
254
    /* Check whether we already evaluated this frame
253
255
     * If we have tried this frame as P, then we have also tried
276
278
        if( p1 != p0 )
277
279
            dist_scale_factor = ( ((b-p0) << 8) + ((p1-p0) >> 1) ) / (p1-p0);
278
280
 
 
281
        if( h->sps->i_mb_width <= 2 || h->sps->i_mb_height <= 2 )
 
282
        {
 
283
            for( h->mb.i_mb_y = 0; h->mb.i_mb_y < h->sps->i_mb_height; h->mb.i_mb_y++ )
 
284
                for( h->mb.i_mb_x = 0; h->mb.i_mb_x < h->sps->i_mb_width; h->mb.i_mb_x++ )
 
285
                    i_score += x264_slicetype_mb_cost( h, a, frames, p0, p1, b, dist_scale_factor );
 
286
        }
279
287
        /* the edge mbs seem to reduce the predictive quality of the
280
288
         * whole frame's score, but are needed for a spatial distribution. */
281
 
        if( h->param.rc.i_vbv_buffer_size )
 
289
        else if( h->param.rc.i_vbv_buffer_size )
282
290
        {
283
291
            for( h->mb.i_mb_y = 0; h->mb.i_mb_y < h->sps->i_mb_height; h->mb.i_mb_y++ )
284
292
            {
286
294
                for( h->mb.i_mb_x = 0; h->mb.i_mb_x < h->sps->i_mb_width; h->mb.i_mb_x++ )
287
295
                {
288
296
                    int i_mb_cost = x264_slicetype_mb_cost( h, a, frames, p0, p1, b, dist_scale_factor );
289
 
                    row_satd[ h->mb.i_mb_y ] += i_mb_cost;
 
297
                    int i_mb_cost_aq = i_mb_cost;
 
298
                    if( h->param.rc.i_aq_mode )
 
299
                    {
 
300
                        x264_emms();
 
301
                        i_mb_cost_aq *= pow(2.0,-(frames[b]->f_qp_offset[h->mb.i_mb_x + h->mb.i_mb_y*h->mb.i_mb_stride])/6.0);
 
302
                    }
 
303
                    row_satd[ h->mb.i_mb_y ] += i_mb_cost_aq;
290
304
                    if( h->mb.i_mb_y > 0 && h->mb.i_mb_y < h->sps->i_mb_height - 1 &&
291
305
                        h->mb.i_mb_x > 0 && h->mb.i_mb_x < h->sps->i_mb_width - 1 )
292
306
                    {
 
307
                        /* Don't use AQ-weighted costs for slicetype decision, only for ratecontrol. */
293
308
                        i_score += i_mb_cost;
 
309
                        i_score_aq += i_mb_cost_aq;
294
310
                    }
295
311
                }
296
312
            }
297
313
        }
298
 
        else if( h->sps->i_mb_width > 2 && h->sps->i_mb_height > 2 )
 
314
        else
299
315
        {
300
316
            for( h->mb.i_mb_y = 1; h->mb.i_mb_y < h->sps->i_mb_height - 1; h->mb.i_mb_y++ )
301
317
                for( h->mb.i_mb_x = 1; h->mb.i_mb_x < h->sps->i_mb_width - 1; h->mb.i_mb_x++ )
302
 
                    i_score += x264_slicetype_mb_cost( h, a, frames, p0, p1, b, dist_scale_factor );
303
 
        }
304
 
        else
305
 
        {
306
 
            for( h->mb.i_mb_y = 0; h->mb.i_mb_y < h->sps->i_mb_height; h->mb.i_mb_y++ )
307
 
                for( h->mb.i_mb_x = 0; h->mb.i_mb_x < h->sps->i_mb_width; h->mb.i_mb_x++ )
308
 
                    i_score += x264_slicetype_mb_cost( h, a, frames, p0, p1, b, dist_scale_factor );
309
 
        }
310
 
 
 
318
                {
 
319
                    int i_mb_cost = x264_slicetype_mb_cost( h, a, frames, p0, p1, b, dist_scale_factor );
 
320
                    int i_mb_cost_aq = i_mb_cost;
 
321
                    if( h->param.rc.i_aq_mode )
 
322
                    {
 
323
                        x264_emms();
 
324
                        i_mb_cost_aq *= pow(2.0,-(frames[b]->f_qp_offset[h->mb.i_mb_x + h->mb.i_mb_y*h->mb.i_mb_stride])/6.0);
 
325
                    }
 
326
                    i_score += i_mb_cost;
 
327
                    i_score_aq += i_mb_cost_aq;
 
328
                }
 
329
        }
311
330
 
312
331
        if( b != p1 )
313
332
            i_score = i_score * 100 / (120 + h->param.i_bframe_bias);
314
333
 
315
334
        frames[b]->i_cost_est[b-p0][p1-b] = i_score;
 
335
        frames[b]->i_cost_est_aq[b-p0][p1-b] = i_score_aq;
316
336
//      fprintf( stderr, "frm %d %c(%d,%d): %6d %6d imb:%d  \n", frames[b]->i_frame,
317
337
//               (p1==0?'I':b<p1?'B':'P'), b-p0, p1-b, i_score, frames[b]->i_cost_est[0][0], frames[b]->i_intra_mbs[b-p0] );
318
338
        x264_emms();
538
558
    frames[b] = h->fenc;
539
559
 
540
560
    cost = x264_slicetype_frame_cost( h, &a, frames, p0, p1, b, 0 );
 
561
 
 
562
    /* In AQ, use the weighted score instead. */
 
563
    if( h->param.rc.i_aq_mode )
 
564
        cost = frames[b]->i_cost_est[b-p0][p1-b];
 
565
 
541
566
    h->fenc->i_row_satd = h->fenc->i_row_satds[b-p0][p1-b];
542
567
    h->fdec->i_row_satd = h->fdec->i_row_satds[b-p0][p1-b];
543
568
    h->fdec->i_satd = cost;