slicetype.c
上传用户:lctgjx
上传日期:2022-06-04
资源大小:8887k
文件大小:40k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /*****************************************************************************
  2.  * slicetype.c: h264 encoder library
  3.  *****************************************************************************
  4.  * Copyright (C) 2005-2008 x264 project
  5.  *
  6.  * Authors: Loren Merritt <lorenm@u.washington.edu>
  7.  *          Jason Garrett-Glaser <darkshikari@gmail.com>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. #include <math.h>
  24. #include "common/common.h"
  25. #include "common/cpu.h"
  26. #include "macroblock.h"
  27. #include "me.h"
  28. static void x264_lowres_context_init( x264_t *h, x264_mb_analysis_t *a )
  29. {
  30.     a->i_qp = X264_LOOKAHEAD_QP;
  31.     a->i_lambda = x264_lambda_tab[ a->i_qp ];
  32.     x264_mb_analyse_load_costs( h, a );
  33.     h->mb.i_me_method = X264_MIN( X264_ME_HEX, h->param.analyse.i_me_method ); // maybe dia?
  34.     h->mb.i_subpel_refine = 4; // 3 should be enough, but not tweaking for speed now
  35.     h->mb.b_chroma_me = 0;
  36. }
  37. static int x264_slicetype_mb_cost( x264_t *h, x264_mb_analysis_t *a,
  38.                             x264_frame_t **frames, int p0, int p1, int b,
  39.                             int dist_scale_factor, int do_search[2] )
  40. {
  41.     x264_frame_t *fref0 = frames[p0];
  42.     x264_frame_t *fref1 = frames[p1];
  43.     x264_frame_t *fenc  = frames[b];
  44.     const int b_bidir = (b < p1);
  45.     const int i_mb_x = h->mb.i_mb_x;
  46.     const int i_mb_y = h->mb.i_mb_y;
  47.     const int i_mb_stride = h->sps->i_mb_width;
  48.     const int i_mb_xy = i_mb_x + i_mb_y * i_mb_stride;
  49.     const int i_stride = fenc->i_stride_lowres;
  50.     const int i_pel_offset = 8 * ( i_mb_x + i_mb_y * i_stride );
  51.     const int i_bipred_weight = h->param.analyse.b_weighted_bipred ? 64 - (dist_scale_factor>>2) : 32;
  52.     int16_t (*fenc_mvs[2])[2] = { &frames[b]->lowres_mvs[0][b-p0-1][i_mb_xy], &frames[b]->lowres_mvs[1][p1-b-1][i_mb_xy] };
  53.     int (*fenc_costs[2]) = { &frames[b]->lowres_mv_costs[0][b-p0-1][i_mb_xy], &frames[b]->lowres_mv_costs[1][p1-b-1][i_mb_xy] };
  54.     ALIGNED_8( uint8_t pix1[9*FDEC_STRIDE] );
  55.     uint8_t *pix2 = pix1+8;
  56.     x264_me_t m[2];
  57.     int i_bcost = COST_MAX;
  58.     int l, i;
  59.     int list_used = 0;
  60.     h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
  61.     h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fenc[0], FENC_STRIDE, &fenc->lowres[0][i_pel_offset], i_stride, 8 );
  62.     if( p0 == p1 )
  63.         goto lowres_intra_mb;
  64.     // no need for h->mb.mv_min[]
  65.     h->mb.mv_min_fpel[0] = -8*h->mb.i_mb_x - 4;
  66.     h->mb.mv_max_fpel[0] = 8*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 4;
  67.     h->mb.mv_min_spel[0] = 4*( h->mb.mv_min_fpel[0] - 8 );
  68.     h->mb.mv_max_spel[0] = 4*( h->mb.mv_max_fpel[0] + 8 );
  69.     if( h->mb.i_mb_x >= h->sps->i_mb_width - 2 )
  70.     {
  71.         h->mb.mv_min_fpel[1] = -8*h->mb.i_mb_y - 4;
  72.         h->mb.mv_max_fpel[1] = 8*( h->sps->i_mb_height - h->mb.i_mb_y - 1 ) + 4;
  73.         h->mb.mv_min_spel[1] = 4*( h->mb.mv_min_fpel[1] - 8 );
  74.         h->mb.mv_max_spel[1] = 4*( h->mb.mv_max_fpel[1] + 8 );
  75.     }
  76. #define LOAD_HPELS_LUMA(dst, src) 
  77.     { 
  78.         (dst)[0] = &(src)[0][i_pel_offset]; 
  79.         (dst)[1] = &(src)[1][i_pel_offset]; 
  80.         (dst)[2] = &(src)[2][i_pel_offset]; 
  81.         (dst)[3] = &(src)[3][i_pel_offset]; 
  82.     }
  83. #define CLIP_MV( mv ) 
  84.     { 
  85.         mv[0] = x264_clip3( mv[0], h->mb.mv_min_spel[0], h->mb.mv_max_spel[0] ); 
  86.         mv[1] = x264_clip3( mv[1], h->mb.mv_min_spel[1], h->mb.mv_max_spel[1] ); 
  87.     }
  88. #define TRY_BIDIR( mv0, mv1, penalty ) 
  89.     { 
  90.         int stride1 = 16, stride2 = 16; 
  91.         uint8_t *src1, *src2; 
  92.         int i_cost; 
  93.         src1 = h->mc.get_ref( pix1, &stride1, m[0].p_fref, m[0].i_stride[0], 
  94.                               (mv0)[0], (mv0)[1], 8, 8 ); 
  95.         src2 = h->mc.get_ref( pix2, &stride2, m[1].p_fref, m[1].i_stride[0], 
  96.                               (mv1)[0], (mv1)[1], 8, 8 ); 
  97.         h->mc.avg[PIXEL_8x8]( pix1, 16, src1, stride1, src2, stride2, i_bipred_weight ); 
  98.         i_cost = penalty + h->pixf.mbcmp[PIXEL_8x8]( 
  99.                            m[0].p_fenc[0], FENC_STRIDE, pix1, 16 ); 
  100.         COPY2_IF_LT( i_bcost, i_cost, list_used, 3 ); 
  101.     }
  102.     m[0].i_pixel = PIXEL_8x8;
  103.     m[0].p_cost_mv = a->p_cost_mv;
  104.     m[0].i_stride[0] = i_stride;
  105.     m[0].p_fenc[0] = h->mb.pic.p_fenc[0];
  106.     LOAD_HPELS_LUMA( m[0].p_fref, fref0->lowres );
  107.     if( b_bidir )
  108.     {
  109.         int16_t *mvr = fref1->lowres_mvs[0][p1-p0-1][i_mb_xy];
  110.         int dmv[2][2];
  111.         h->mc.memcpy_aligned( &m[1], &m[0], sizeof(x264_me_t) );
  112.         LOAD_HPELS_LUMA( m[1].p_fref, fref1->lowres );
  113.         dmv[0][0] = ( mvr[0] * dist_scale_factor + 128 ) >> 8;
  114.         dmv[0][1] = ( mvr[1] * dist_scale_factor + 128 ) >> 8;
  115.         dmv[1][0] = dmv[0][0] - mvr[0];
  116.         dmv[1][1] = dmv[0][1] - mvr[1];
  117.         CLIP_MV( dmv[0] );
  118.         CLIP_MV( dmv[1] );
  119.         TRY_BIDIR( dmv[0], dmv[1], 0 );
  120.         if( dmv[0][0] | dmv[0][1] | dmv[1][0] | dmv[1][1] )
  121.         {
  122.             int i_cost;
  123.             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 );
  124.             i_cost = h->pixf.mbcmp[PIXEL_8x8]( m[0].p_fenc[0], FENC_STRIDE, pix1, 16 );
  125.             COPY2_IF_LT( i_bcost, i_cost, list_used, 3 );
  126.         }
  127.     }
  128.     for( l = 0; l < 1 + b_bidir; l++ )
  129.     {
  130.         if( do_search[l] )
  131.         {
  132.             int i_mvc = 0;
  133.             int16_t (*fenc_mv)[2] = fenc_mvs[l];
  134.             ALIGNED_4( int16_t mvc[4][2] );
  135.             /* Reverse-order MV prediction. */
  136.             *(uint32_t*)mvc[0] = 0;
  137.             *(uint32_t*)mvc[1] = 0;
  138.             *(uint32_t*)mvc[2] = 0;
  139. #define MVC(mv) { *(uint32_t*)mvc[i_mvc] = *(uint32_t*)mv; i_mvc++; }
  140.             if( i_mb_x < h->sps->i_mb_width - 1 )
  141.                 MVC(fenc_mv[1]);
  142.             if( i_mb_y < h->sps->i_mb_height - 1 )
  143.             {
  144.                 MVC(fenc_mv[i_mb_stride]);
  145.                 if( i_mb_x > 0 )
  146.                     MVC(fenc_mv[i_mb_stride-1]);
  147.                 if( i_mb_x < h->sps->i_mb_width - 1 )
  148.                     MVC(fenc_mv[i_mb_stride+1]);
  149.             }
  150. #undef MVC
  151.             x264_median_mv( m[l].mvp, mvc[0], mvc[1], mvc[2] );
  152.             x264_me_search( h, &m[l], mvc, i_mvc );
  153.             m[l].cost -= 2; // remove mvcost from skip mbs
  154.             if( *(uint32_t*)m[l].mv )
  155.                 m[l].cost += 5;
  156.             *(uint32_t*)fenc_mvs[l] = *(uint32_t*)m[l].mv;
  157.             *fenc_costs[l] = m[l].cost;
  158.         }
  159.         else
  160.         {
  161.             *(uint32_t*)m[l].mv = *(uint32_t*)fenc_mvs[l];
  162.             m[l].cost = *fenc_costs[l];
  163.         }
  164.         COPY2_IF_LT( i_bcost, m[l].cost, list_used, l+1 );
  165.     }
  166.     if( b_bidir && ( *(uint32_t*)m[0].mv || *(uint32_t*)m[1].mv ) )
  167.         TRY_BIDIR( m[0].mv, m[1].mv, 5 );
  168.     /* Store to width-2 bitfield. */
  169.     frames[b]->lowres_inter_types[b-p0][p1-b][i_mb_xy>>2] &= ~(3<<((i_mb_xy&3)*2));
  170.     frames[b]->lowres_inter_types[b-p0][p1-b][i_mb_xy>>2] |= list_used<<((i_mb_xy&3)*2);
  171. lowres_intra_mb:
  172.     /* forbid intra-mbs in B-frames, because it's rare and not worth checking */
  173.     /* FIXME: Should we still forbid them now that we cache intra scores? */
  174.     if( !b_bidir || h->param.rc.b_mb_tree )
  175.     {
  176.         int i_icost, b_intra;
  177.         if( !fenc->b_intra_calculated )
  178.         {
  179.             ALIGNED_ARRAY_16( uint8_t, edge,[33] );
  180.             uint8_t *pix = &pix1[8+FDEC_STRIDE - 1];
  181.             uint8_t *src = &fenc->lowres[0][i_pel_offset - 1];
  182.             const int intra_penalty = 5;
  183.             int satds[4];
  184.             memcpy( pix-FDEC_STRIDE, src-i_stride, 17 );
  185.             for( i=0; i<8; i++ )
  186.                 pix[i*FDEC_STRIDE] = src[i*i_stride];
  187.             pix++;
  188.             if( h->pixf.intra_mbcmp_x3_8x8c )
  189.             {
  190.                 h->pixf.intra_mbcmp_x3_8x8c( h->mb.pic.p_fenc[0], pix, satds );
  191.                 h->predict_8x8c[I_PRED_CHROMA_P]( pix );
  192.                 satds[I_PRED_CHROMA_P] =
  193.                     h->pixf.mbcmp[PIXEL_8x8]( pix, FDEC_STRIDE, h->mb.pic.p_fenc[0], FENC_STRIDE );
  194.             }
  195.             else
  196.             {
  197.                 for( i=0; i<4; i++ )
  198.                 {
  199.                     h->predict_8x8c[i]( pix );
  200.                     satds[i] = h->pixf.mbcmp[PIXEL_8x8]( pix, FDEC_STRIDE, h->mb.pic.p_fenc[0], FENC_STRIDE );
  201.                 }
  202.             }
  203.             i_icost = X264_MIN4( satds[0], satds[1], satds[2], satds[3] );
  204.             h->predict_8x8_filter( pix, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
  205.             for( i=3; i<9; i++ )
  206.             {
  207.                 int satd;
  208.                 h->predict_8x8[i]( pix, edge );
  209.                 satd = h->pixf.mbcmp[PIXEL_8x8]( pix, FDEC_STRIDE, h->mb.pic.p_fenc[0], FENC_STRIDE );
  210.                 i_icost = X264_MIN( i_icost, satd );
  211.             }
  212.             i_icost += intra_penalty;
  213.             fenc->i_intra_cost[i_mb_xy] = i_icost;
  214.         }
  215.         else
  216.             i_icost = fenc->i_intra_cost[i_mb_xy];
  217.         if( !b_bidir )
  218.         {
  219.             b_intra = i_icost < i_bcost;
  220.             if( b_intra )
  221.                 i_bcost = i_icost;
  222.             if(   (i_mb_x > 0 && i_mb_x < h->sps->i_mb_width - 1
  223.                 && i_mb_y > 0 && i_mb_y < h->sps->i_mb_height - 1)
  224.                 || h->sps->i_mb_width <= 2 || h->sps->i_mb_height <= 2 )
  225.             {
  226.                 fenc->i_intra_mbs[b-p0] += b_intra;
  227.                 fenc->i_cost_est[0][0] += i_icost;
  228.                 if( h->param.rc.i_aq_mode )
  229.                     fenc->i_cost_est_aq[0][0] += (i_icost * fenc->i_inv_qscale_factor[i_mb_xy] + 128) >> 8;
  230.             }
  231.         }
  232.     }
  233.     fenc->lowres_costs[b-p0][p1-b][i_mb_xy] = i_bcost;
  234.     return i_bcost;
  235. }
  236. #undef TRY_BIDIR
  237. #define NUM_MBS
  238.    (h->sps->i_mb_width > 2 && h->sps->i_mb_height > 2 ?
  239.    (h->sps->i_mb_width - 2) * (h->sps->i_mb_height - 2) :
  240.     h->sps->i_mb_width * h->sps->i_mb_height)
  241. static int x264_slicetype_frame_cost( x264_t *h, x264_mb_analysis_t *a,
  242.                                x264_frame_t **frames, int p0, int p1, int b,
  243.                                int b_intra_penalty )
  244. {
  245.     int i_score = 0;
  246.     /* Don't use the AQ'd scores for slicetype decision. */
  247.     int i_score_aq = 0;
  248.     int do_search[2];
  249.     /* Check whether we already evaluated this frame
  250.      * If we have tried this frame as P, then we have also tried
  251.      * the preceding frames as B. (is this still true?) */
  252.     /* Also check that we already calculated the row SATDs for the current frame. */
  253.     if( frames[b]->i_cost_est[b-p0][p1-b] >= 0 && (!h->param.rc.i_vbv_buffer_size || frames[b]->i_row_satds[b-p0][p1-b][0] != -1) )
  254.     {
  255.         i_score = frames[b]->i_cost_est[b-p0][p1-b];
  256.     }
  257.     else
  258.     {
  259.         int dist_scale_factor = 128;
  260.         int *row_satd = frames[b]->i_row_satds[b-p0][p1-b];
  261.         /* For each list, check to see whether we have lowres motion-searched this reference frame before. */
  262.         do_search[0] = b != p0 && frames[b]->lowres_mvs[0][b-p0-1][0][0] == 0x7FFF;
  263.         do_search[1] = b != p1 && frames[b]->lowres_mvs[1][p1-b-1][0][0] == 0x7FFF;
  264.         if( do_search[0] ) frames[b]->lowres_mvs[0][b-p0-1][0][0] = 0;
  265.         if( do_search[1] ) frames[b]->lowres_mvs[1][p1-b-1][0][0] = 0;
  266.         if( b == p1 )
  267.         {
  268.             frames[b]->i_intra_mbs[b-p0] = 0;
  269.             frames[b]->i_cost_est[0][0] = 0;
  270.             frames[b]->i_cost_est_aq[0][0] = 0;
  271.         }
  272.         if( p1 != p0 )
  273.             dist_scale_factor = ( ((b-p0) << 8) + ((p1-p0) >> 1) ) / (p1-p0);
  274.         /* Lowres lookahead goes backwards because the MVs are used as predictors in the main encode.
  275.          * This considerably improves MV prediction overall. */
  276.         /* the edge mbs seem to reduce the predictive quality of the
  277.          * whole frame's score, but are needed for a spatial distribution. */
  278.         if( h->param.rc.b_mb_tree || h->param.rc.i_vbv_buffer_size ||
  279.             h->sps->i_mb_width <= 2 || h->sps->i_mb_height <= 2 )
  280.         {
  281.             for( h->mb.i_mb_y = h->sps->i_mb_height - 1; h->mb.i_mb_y >= 0; h->mb.i_mb_y-- )
  282.             {
  283.                 row_satd[ h->mb.i_mb_y ] = 0;
  284.                 for( h->mb.i_mb_x = h->sps->i_mb_width - 1; h->mb.i_mb_x >= 0; h->mb.i_mb_x-- )
  285.                 {
  286.                     int i_mb_cost = x264_slicetype_mb_cost( h, a, frames, p0, p1, b, dist_scale_factor, do_search );
  287.                     int i_mb_cost_aq = i_mb_cost;
  288.                     if( h->param.rc.i_aq_mode )
  289.                         i_mb_cost_aq = (i_mb_cost_aq * frames[b]->i_inv_qscale_factor[h->mb.i_mb_x + h->mb.i_mb_y*h->mb.i_mb_stride] + 128) >> 8;
  290.                     row_satd[ h->mb.i_mb_y ] += i_mb_cost_aq;
  291.                     if( (h->mb.i_mb_y > 0 && h->mb.i_mb_y < h->sps->i_mb_height - 1 &&
  292.                          h->mb.i_mb_x > 0 && h->mb.i_mb_x < h->sps->i_mb_width - 1) ||
  293.                          h->sps->i_mb_width <= 2 || h->sps->i_mb_height <= 2 )
  294.                     {
  295.                         /* Don't use AQ-weighted costs for slicetype decision, only for ratecontrol. */
  296.                         i_score += i_mb_cost;
  297.                         i_score_aq += i_mb_cost_aq;
  298.                     }
  299.                 }
  300.             }
  301.         }
  302.         else
  303.         {
  304.             for( h->mb.i_mb_y = h->sps->i_mb_height - 2; h->mb.i_mb_y > 0; h->mb.i_mb_y-- )
  305.                 for( h->mb.i_mb_x = h->sps->i_mb_width - 2; h->mb.i_mb_x > 0; h->mb.i_mb_x-- )
  306.                 {
  307.                     int i_mb_cost = x264_slicetype_mb_cost( h, a, frames, p0, p1, b, dist_scale_factor, do_search );
  308.                     int i_mb_cost_aq = i_mb_cost;
  309.                     if( h->param.rc.i_aq_mode )
  310.                         i_mb_cost_aq = (i_mb_cost_aq * frames[b]->i_inv_qscale_factor[h->mb.i_mb_x + h->mb.i_mb_y*h->mb.i_mb_stride] + 128) >> 8;
  311.                     i_score += i_mb_cost;
  312.                     i_score_aq += i_mb_cost_aq;
  313.                 }
  314.         }
  315.         if( b != p1 )
  316.             i_score = i_score * 100 / (120 + h->param.i_bframe_bias);
  317.         else
  318.             frames[b]->b_intra_calculated = 1;
  319.         frames[b]->i_cost_est[b-p0][p1-b] = i_score;
  320.         frames[b]->i_cost_est_aq[b-p0][p1-b] = i_score_aq;
  321.         x264_emms();
  322.     }
  323.     if( b_intra_penalty )
  324.     {
  325.         // arbitrary penalty for I-blocks after B-frames
  326.         int nmb = NUM_MBS;
  327.         i_score += i_score * frames[b]->i_intra_mbs[b-p0] / (nmb * 8);
  328.     }
  329.     return i_score;
  330. }
  331. /* If MB-tree changes the quantizers, we need to recalculate the frame cost without
  332.  * re-running lookahead. */
  333. static int x264_slicetype_frame_cost_recalculate( x264_t *h, x264_frame_t **frames, int p0, int p1, int b )
  334. {
  335.     int i_score = 0;
  336.     int *row_satd = frames[b]->i_row_satds[b-p0][p1-b];
  337.     x264_emms();
  338.     for( h->mb.i_mb_y = h->sps->i_mb_height - 1; h->mb.i_mb_y >= 0; h->mb.i_mb_y-- )
  339.     {
  340.         row_satd[ h->mb.i_mb_y ] = 0;
  341.         for( h->mb.i_mb_x = h->sps->i_mb_width - 1; h->mb.i_mb_x >= 0; h->mb.i_mb_x-- )
  342.         {
  343.             int i_mb_xy = h->mb.i_mb_x + h->mb.i_mb_y*h->mb.i_mb_stride;
  344.             int i_mb_cost = frames[b]->lowres_costs[b-p0][p1-b][i_mb_xy];
  345.             float qp_adj = frames[b]->f_qp_offset[i_mb_xy];
  346.             i_mb_cost = (i_mb_cost * x264_exp2fix8(qp_adj) + 128) >> 8;
  347.             row_satd[ h->mb.i_mb_y ] += i_mb_cost;
  348.             if( (h->mb.i_mb_y > 0 && h->mb.i_mb_y < h->sps->i_mb_height - 1 &&
  349.                  h->mb.i_mb_x > 0 && h->mb.i_mb_x < h->sps->i_mb_width - 1) ||
  350.                  h->sps->i_mb_width <= 2 || h->sps->i_mb_height <= 2 )
  351.             {
  352.                 i_score += i_mb_cost;
  353.             }
  354.         }
  355.     }
  356.     return i_score;
  357. }
  358. static void x264_macroblock_tree_finish( x264_t *h, x264_frame_t *frame, int b_bidir )
  359. {
  360.     int mb_index;
  361.     x264_emms();
  362.     if( b_bidir )
  363.         memcpy( frame->f_qp_offset, frame->f_qp_offset_aq, sizeof( frame->f_qp_offset ) );
  364.     else
  365.     {
  366.         /* Allow the strength to be adjusted via qcompress, since the two
  367.          * concepts are very similar. */
  368.         float strength = 5.0f * (1.0f - h->param.rc.f_qcompress);
  369.         for( mb_index = 0; mb_index < h->mb.i_mb_count; mb_index++ )
  370.         {
  371.             int intra_cost = (frame->i_intra_cost[mb_index] * frame->i_inv_qscale_factor[mb_index]+128)>>8;
  372.             if( intra_cost )
  373.             {
  374.                 int propagate_cost = frame->i_propagate_cost[mb_index];
  375.                 float log2_ratio = x264_log2(intra_cost + propagate_cost) - x264_log2(intra_cost);
  376.                 frame->f_qp_offset[mb_index] = frame->f_qp_offset_aq[mb_index] - strength * log2_ratio;
  377.             }
  378.         }
  379.     }
  380. }
  381. static void x264_macroblock_tree_propagate( x264_t *h, x264_frame_t **frames, int p0, int p1, int b )
  382. {
  383.     uint16_t *ref_costs[2] = {frames[p0]->i_propagate_cost,frames[p1]->i_propagate_cost};
  384.     int dist_scale_factor = ( ((b-p0) << 8) + ((p1-p0) >> 1) ) / (p1-p0);
  385.     int i_bipred_weight = h->param.analyse.b_weighted_bipred ? 64 - (dist_scale_factor>>2) : 32;
  386.     int16_t (*mvs[2])[2] = { frames[b]->lowres_mvs[0][b-p0-1], frames[b]->lowres_mvs[1][p1-b-1] };
  387.     int bipred_weights[2] = {i_bipred_weight, 64 - i_bipred_weight};
  388.     int *buf = h->scratch_buffer;
  389.     for( h->mb.i_mb_y = 0; h->mb.i_mb_y < h->sps->i_mb_height; h->mb.i_mb_y++ )
  390.     {
  391.         int mb_index = h->mb.i_mb_y*h->mb.i_mb_stride;
  392.         h->mc.mbtree_propagate_cost( buf, frames[b]->i_propagate_cost+mb_index,
  393.             frames[b]->i_intra_cost+mb_index, frames[b]->lowres_costs[b-p0][p1-b]+mb_index,
  394.             frames[b]->i_inv_qscale_factor+mb_index, h->sps->i_mb_width );
  395.         for( h->mb.i_mb_x = 0; h->mb.i_mb_x < h->sps->i_mb_width; h->mb.i_mb_x++, mb_index++ )
  396.         {
  397.             int propagate_amount = buf[h->mb.i_mb_x];
  398.             /* Don't propagate for an intra block. */
  399.             if( propagate_amount > 0 )
  400.             {
  401.                 /* Access width-2 bitfield. */
  402.                 int lists_used = (frames[b]->lowres_inter_types[b-p0][p1-b][mb_index>>2] >> ((mb_index&3)*2))&3;
  403.                 int list;
  404.                 /* Follow the MVs to the previous frame(s). */
  405.                 for( list = 0; list < 2; list++ )
  406.                     if( (lists_used >> list)&1 )
  407.                     {
  408.                         int x = mvs[list][mb_index][0];
  409.                         int y = mvs[list][mb_index][1];
  410.                         int listamount = propagate_amount;
  411.                         int mbx = (x>>5)+h->mb.i_mb_x;
  412.                         int mby = (y>>5)+h->mb.i_mb_y;
  413.                         int idx0 = mbx + mby*h->mb.i_mb_stride;
  414.                         int idx1 = idx0 + 1;
  415.                         int idx2 = idx0 + h->mb.i_mb_stride;
  416.                         int idx3 = idx0 + h->mb.i_mb_stride + 1;
  417. //C程序不能中间定义变量 --@lia
  418. int idx0weight,idx1weight,idx2weight,idx3weight;
  419.                         x &= 31;
  420.                         y &= 31;
  421. //C程序不能中间定义变量 --@lia
  422.                         //int 
  423. idx0weight = (32-y)*(32-x);
  424.                         //int 
  425. idx1weight = (32-y)*x;
  426.                         //int 
  427. idx2weight = y*(32-x);
  428.                         //int 
  429. idx3weight = y*x;
  430.                         /* Apply bipred weighting. */
  431.                         if( lists_used == 3 )
  432.                             listamount = (listamount * bipred_weights[list] + 32) >> 6;
  433. #define CLIP_ADD(s,x) (s) = X264_MIN((s)+(x),(1<<16)-1)
  434.                         /* We could just clip the MVs, but pixels that lie outside the frame probably shouldn't
  435.                          * be counted. */
  436.                         if( mbx < h->sps->i_mb_width-1 && mby < h->sps->i_mb_height-1 && mbx >= 0 && mby >= 0 )
  437.                         {
  438.                             CLIP_ADD( ref_costs[list][idx0], (listamount*idx0weight+512)>>10 );
  439.                             CLIP_ADD( ref_costs[list][idx1], (listamount*idx1weight+512)>>10 );
  440.                             CLIP_ADD( ref_costs[list][idx2], (listamount*idx2weight+512)>>10 );
  441.                             CLIP_ADD( ref_costs[list][idx3], (listamount*idx3weight+512)>>10 );
  442.                         }
  443.                         else /* Check offsets individually */
  444.                         {
  445.                             if( mbx < h->sps->i_mb_width && mby < h->sps->i_mb_height && mbx >= 0 && mby >= 0 )
  446.                                 CLIP_ADD( ref_costs[list][idx0], (listamount*idx0weight+512)>>10 );
  447.                             if( mbx+1 < h->sps->i_mb_width && mby < h->sps->i_mb_height && mbx+1 >= 0 && mby >= 0 )
  448.                                 CLIP_ADD( ref_costs[list][idx1], (listamount*idx1weight+512)>>10 );
  449.                             if( mbx < h->sps->i_mb_width && mby+1 < h->sps->i_mb_height && mbx >= 0 && mby+1 >= 0 )
  450.                                 CLIP_ADD( ref_costs[list][idx2], (listamount*idx2weight+512)>>10 );
  451.                             if( mbx+1 < h->sps->i_mb_width && mby+1 < h->sps->i_mb_height && mbx+1 >= 0 && mby+1 >= 0 )
  452.                                 CLIP_ADD( ref_costs[list][idx3], (listamount*idx3weight+512)>>10 );
  453.                         }
  454.                     }
  455.             }
  456.         }
  457.     }
  458.     if( h->param.rc.i_vbv_buffer_size )
  459.         x264_macroblock_tree_finish( h, frames[b], b != p1 );
  460. }
  461. static void x264_macroblock_tree( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames, int num_frames, int b_intra )
  462. {
  463.     int i, idx = !b_intra;
  464.     int last_nonb, cur_nonb = 1;
  465.     if( b_intra )
  466.         x264_slicetype_frame_cost( h, a, frames, 0, 0, 0, 0 );
  467.     i = num_frames-1;
  468.     while( i > 0 && frames[i]->i_type == X264_TYPE_B )
  469.         i--;
  470.     last_nonb = i;
  471.     if( last_nonb < 0 )
  472.         return;
  473.     memset( frames[last_nonb]->i_propagate_cost, 0, h->mb.i_mb_count * sizeof(uint16_t) );
  474.     while( i-- > idx )
  475.     {
  476.         cur_nonb = i;
  477.         while( frames[cur_nonb]->i_type == X264_TYPE_B && cur_nonb > 0 )
  478.             cur_nonb--;
  479.         if( cur_nonb < idx )
  480.             break;
  481.         x264_slicetype_frame_cost( h, a, frames, cur_nonb, last_nonb, last_nonb, 0 );
  482.         memset( frames[cur_nonb]->i_propagate_cost, 0, h->mb.i_mb_count * sizeof(uint16_t) );
  483.         x264_macroblock_tree_propagate( h, frames, cur_nonb, last_nonb, last_nonb );
  484.         while( frames[i]->i_type == X264_TYPE_B && i > 0 )
  485.         {
  486.             x264_slicetype_frame_cost( h, a, frames, cur_nonb, last_nonb, i, 0 );
  487.             memset( frames[i]->i_propagate_cost, 0, h->mb.i_mb_count * sizeof(uint16_t) );
  488.             x264_macroblock_tree_propagate( h, frames, cur_nonb, last_nonb, i );
  489.             i--;
  490.         }
  491.         last_nonb = cur_nonb;
  492.     }
  493.     x264_macroblock_tree_finish( h, frames[last_nonb], 0 );
  494. }
  495. static int x264_vbv_frame_cost( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames, int p0, int p1, int b )
  496. {
  497.     int cost = x264_slicetype_frame_cost( h, a, frames, p0, p1, b, 0 );
  498.     if( h->param.rc.i_aq_mode )
  499.     {
  500.         if( h->param.rc.b_mb_tree )
  501.             return x264_slicetype_frame_cost_recalculate( h, frames, p0, p1, b );
  502.         else
  503.             return frames[b]->i_cost_est_aq[b-p0][p1-b];
  504.     }
  505.     return cost;
  506. }
  507. static void x264_vbv_lookahead( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames, int num_frames, int keyframe )
  508. {
  509.     int last_nonb = 0, cur_nonb = 1, next_nonb, i, idx = 0;
  510.     while( cur_nonb < num_frames && frames[cur_nonb]->i_type == X264_TYPE_B )
  511.         cur_nonb++;
  512.     next_nonb = keyframe ? last_nonb : cur_nonb;
  513.     while( cur_nonb <= num_frames )
  514.     {
  515.         /* P/I cost: This shouldn't include the cost of next_nonb */
  516.         if( next_nonb != cur_nonb )
  517.         {
  518.             int p0 = IS_X264_TYPE_I( frames[cur_nonb]->i_type ) ? cur_nonb : last_nonb;
  519.             frames[next_nonb]->i_planned_satd[idx] = x264_vbv_frame_cost( h, a, frames, p0, cur_nonb, cur_nonb );
  520.             frames[next_nonb]->i_planned_type[idx] = frames[cur_nonb]->i_type;
  521.             idx++;
  522.         }
  523.         /* Handle the B-frames: coded order */
  524.         for( i = last_nonb+1; i < cur_nonb; i++, idx++ )
  525.         {
  526.             frames[next_nonb]->i_planned_satd[idx] = x264_vbv_frame_cost( h, a, frames, last_nonb, cur_nonb, i );
  527.             frames[next_nonb]->i_planned_type[idx] = X264_TYPE_B;
  528.         }
  529.         last_nonb = cur_nonb;
  530.         cur_nonb++;
  531.         while( cur_nonb <= num_frames && frames[cur_nonb]->i_type == X264_TYPE_B )
  532.             cur_nonb++;
  533.     }
  534.     frames[next_nonb]->i_planned_type[idx] = X264_TYPE_AUTO;
  535. }
  536. static int x264_slicetype_path_cost( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames, char *path, int threshold )
  537. {
  538.     int loc = 1;
  539.     int cost = 0;
  540.     int cur_p = 0;
  541.     path--; /* Since the 1st path element is really the second frame */
  542.     while( path[loc] )
  543.     {
  544.         int next_p = loc;
  545.         int next_b;
  546.         /* Find the location of the next P-frame. */
  547.         while( path[next_p] && path[next_p] != 'P' )
  548.             next_p++;
  549.         /* Return if the path doesn't end on a P-frame. */
  550.         if( path[next_p] != 'P' )
  551.             return cost;
  552.         /* Add the cost of the P-frame found above */
  553.         cost += x264_slicetype_frame_cost( h, a, frames, cur_p, next_p, next_p, 0 );
  554.         /* Early terminate if the cost we have found is larger than the best path cost so far */
  555.         if( cost > threshold )
  556.             break;
  557.         for( next_b = loc; next_b < next_p && cost < threshold; next_b++ )
  558.             cost += x264_slicetype_frame_cost( h, a, frames, cur_p, next_p, next_b, 0 );
  559.         loc = next_p + 1;
  560.         cur_p = next_p;
  561.     }
  562.     return cost;
  563. }
  564. /* Viterbi/trellis slicetype decision algorithm. */
  565. /* Uses strings due to the fact that the speed of the control functions is
  566.    negligable compared to the cost of running slicetype_frame_cost, and because
  567.    it makes debugging easier. */
  568. 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] )
  569. {
  570.     char paths[X264_BFRAME_MAX+2][X264_LOOKAHEAD_MAX] = {{0}};
  571.     int num_paths = X264_MIN(max_bframes+1, length);
  572.     int suffix_size, loc, path;
  573.     int best_cost = COST_MAX;
  574.     int best_path_index = 0;
  575.     length = X264_MIN(length,X264_LOOKAHEAD_MAX);
  576.     /* Iterate over all currently possible paths and add suffixes to each one */
  577.     for( suffix_size = 0; suffix_size < num_paths; suffix_size++ )
  578.     {
  579.         memcpy( paths[suffix_size], best_paths[length - (suffix_size + 1)], length - (suffix_size + 1) );
  580.         for( loc = 0; loc < suffix_size; loc++ )
  581.             strcat( paths[suffix_size], "B" );
  582.         strcat( paths[suffix_size], "P" );
  583.     }
  584.     /* Calculate the actual cost of each of the current paths */
  585.     for( path = 0; path < num_paths; path++ )
  586.     {
  587.         int cost = x264_slicetype_path_cost( h, a, frames, paths[path], best_cost );
  588.         if( cost < best_cost )
  589.         {
  590.             best_cost = cost;
  591.             best_path_index = path;
  592.         }
  593.     }
  594.     /* Store the best path. */
  595.     memcpy( best_paths[length], paths[best_path_index], length );
  596. }
  597. static int scenecut( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames, int p0, int p1, int print )
  598. {
  599.     x264_frame_t *frame = frames[p1];
  600. //C程序不能中间定义变量 --@lia
  601. int icost,pcost,i_gop_size,res;
  602. float f_bias,f_thresh_max,f_thresh_min;
  603.     x264_slicetype_frame_cost( h, a, frames, p0, p1, p1, 0 );
  604. //C程序不能中间定义变量 --@lia
  605.     //int 
  606. icost = frame->i_cost_est[0][0];
  607.     //int 
  608. pcost = frame->i_cost_est[p1-p0][0];
  609.     //float f_bias;
  610.     //int 
  611. i_gop_size = frame->i_frame - h->lookahead->i_last_idr;
  612.     //float 
  613. f_thresh_max = h->param.i_scenecut_threshold / 100.0;
  614.     /* magic numbers pulled out of thin air */
  615.     //float 
  616. f_thresh_min = f_thresh_max * h->param.i_keyint_min
  617.                          / ( h->param.i_keyint_max * 4 );
  618.     //int res;
  619.     if( h->param.i_keyint_min == h->param.i_keyint_max )
  620.         f_thresh_min= f_thresh_max;
  621.     if( i_gop_size < h->param.i_keyint_min / 4 )
  622.         f_bias = f_thresh_min / 4;
  623.     else if( i_gop_size <= h->param.i_keyint_min )
  624.         f_bias = f_thresh_min * i_gop_size / h->param.i_keyint_min;
  625.     else
  626.     {
  627.         f_bias = f_thresh_min
  628.                  + ( f_thresh_max - f_thresh_min )
  629.                     * ( i_gop_size - h->param.i_keyint_min )
  630.                    / ( h->param.i_keyint_max - h->param.i_keyint_min ) ;
  631.     }
  632.     res = pcost >= (1.0 - f_bias) * icost;
  633.     if( res && print )
  634.     {
  635.         int imb = frame->i_intra_mbs[p1-p0];
  636.         int pmb = NUM_MBS - imb;
  637.         x264_log( h, X264_LOG_DEBUG, "scene cut at %d Icost:%d Pcost:%d ratio:%.4f bias:%.4f gop:%d (imb:%d pmb:%d)n",
  638.                   frame->i_frame,
  639.                   icost, pcost, 1. - (double)pcost / icost,
  640.                   f_bias, i_gop_size, imb, pmb );
  641.     }
  642.     return res;
  643. }
  644. void x264_slicetype_analyse( x264_t *h, int keyframe )
  645. {
  646.     x264_mb_analysis_t a;
  647.     x264_frame_t *frames[X264_LOOKAHEAD_MAX+3] = { NULL, };
  648.     int num_frames, keyint_limit, idr_frame_type, i, j;
  649.     int i_mb_count = NUM_MBS;
  650.     int cost1p0, cost2p0, cost1b1, cost2p1;
  651.     int i_max_search = X264_MIN( h->lookahead->next.i_size, X264_LOOKAHEAD_MAX );
  652. //C程序不能在中间定义变量 --@lia
  653.     char best_paths[X264_LOOKAHEAD_MAX][X264_LOOKAHEAD_MAX] = {"","P"};
  654.     int n;
  655.     int num_bframes = 0;
  656.     int max_bframes  ;//= X264_MIN(num_frames-1, h->param.i_bframe);
  657.     int num_analysed_frames ;//= num_frames;
  658.     int reset_start;
  659.     if( h->param.b_deterministic )
  660.         i_max_search = X264_MIN( i_max_search, h->lookahead->i_slicetype_length + !keyframe );
  661.     assert( h->frames.b_have_lowres );
  662.     if( !h->lookahead->last_nonb )
  663.         return;
  664.     frames[0] = h->lookahead->last_nonb;
  665.     for( j = 0; j < i_max_search && h->lookahead->next.list[j]->i_type == X264_TYPE_AUTO; j++ )
  666.         frames[j+1] = h->lookahead->next.list[j];
  667.     if( !j )
  668.         return;
  669.     keyint_limit = h->param.i_keyint_max - frames[0]->i_frame + h->lookahead->i_last_idr - 1;
  670.     num_frames = X264_MIN( j, keyint_limit );
  671.     x264_lowres_context_init( h, &a );
  672.     idr_frame_type = frames[1]->i_frame - h->lookahead->i_last_idr >= h->param.i_keyint_min ? X264_TYPE_IDR : X264_TYPE_I;
  673.     /* This is important psy-wise: if we have a non-scenecut keyframe,
  674.      * there will be significant visual artifacts if the frames just before
  675.      * go down in quality due to being referenced less, despite it being
  676.      * more RD-optimal. */
  677.     if( (h->param.analyse.b_psy && h->param.rc.b_mb_tree) || h->param.rc.i_vbv_buffer_size )
  678.         num_frames = j;
  679.     else if( num_frames == 1 )
  680.     {
  681.         frames[1]->i_type = X264_TYPE_P;
  682.         if( h->param.i_scenecut_threshold && scenecut( h, &a, frames, 0, 1, 1 ) )
  683.             frames[1]->i_type = idr_frame_type;
  684.         return;
  685.     }
  686.     else if( num_frames == 0 )
  687.     {
  688.         frames[1]->i_type = idr_frame_type;
  689.         return;
  690.     }
  691. //C程序不能在中间定义变量 --@lia
  692.     //char best_paths[X264_LOOKAHEAD_MAX][X264_LOOKAHEAD_MAX] = {"","P"};
  693.     //int n;
  694.     //int num_bframes = 0;
  695.     //int 
  696. max_bframes = X264_MIN(num_frames-1, h->param.i_bframe);
  697.     //int 
  698. num_analysed_frames = num_frames;
  699.     //int reset_start;
  700.     if( h->param.i_scenecut_threshold && scenecut( h, &a, frames, 0, 1, 1 ) )
  701.     {
  702.         frames[1]->i_type = idr_frame_type;
  703.         return;
  704.     }
  705.     if( h->param.i_bframe )
  706.     {
  707.         if( h->param.i_bframe_adaptive == X264_B_ADAPT_TRELLIS )
  708.         {
  709.             /* Perform the frametype analysis. */
  710.             for( n = 2; n < num_frames-1; n++ )
  711.                 x264_slicetype_path( h, &a, frames, n, max_bframes, best_paths );
  712.             if( num_frames > 1 )
  713.             {
  714.                 num_bframes = strspn( best_paths[num_frames-2], "B" );
  715.                 /* Load the results of the analysis into the frame types. */
  716.                 for( j = 1; j < num_frames; j++ )
  717.                     frames[j]->i_type = best_paths[num_frames-2][j-1] == 'B' ? X264_TYPE_B : X264_TYPE_P;
  718.             }
  719.             frames[num_frames]->i_type = X264_TYPE_P;
  720.         }
  721.         else if( h->param.i_bframe_adaptive == X264_B_ADAPT_FAST )
  722.         {
  723.             for( i = 0; i < num_frames-(2-!i); )
  724.             {
  725.                 cost2p1 = x264_slicetype_frame_cost( h, &a, frames, i+0, i+2, i+2, 1 );
  726.                 if( frames[i+2]->i_intra_mbs[2] > i_mb_count / 2 )
  727.                 {
  728.                     frames[i+1]->i_type = X264_TYPE_P;
  729.                     frames[i+2]->i_type = X264_TYPE_P;
  730.                     i += 2;
  731.                     continue;
  732.                 }
  733.                 cost1b1 = x264_slicetype_frame_cost( h, &a, frames, i+0, i+2, i+1, 0 );
  734.                 cost1p0 = x264_slicetype_frame_cost( h, &a, frames, i+0, i+1, i+1, 0 );
  735.                 cost2p0 = x264_slicetype_frame_cost( h, &a, frames, i+1, i+2, i+2, 0 );
  736.                 if( cost1p0 + cost2p0 < cost1b1 + cost2p1 )
  737.                 {
  738.                     frames[i+1]->i_type = X264_TYPE_P;
  739.                     frames[i+2]->i_type = X264_TYPE_P;
  740.                     i += 2;
  741.                     continue;
  742.                 }
  743.                 // arbitrary and untuned
  744.                 #define INTER_THRESH 300
  745.                 #define P_SENS_BIAS (50 - h->param.i_bframe_bias)
  746.                 frames[i+1]->i_type = X264_TYPE_B;
  747.                 frames[i+2]->i_type = X264_TYPE_P;
  748.                 for( j = i+2; j <= X264_MIN( h->param.i_bframe, num_frames-1 ); j++ )
  749.                 {
  750.                     int pthresh = X264_MAX(INTER_THRESH - P_SENS_BIAS * (j-i-1), INTER_THRESH/10);
  751.                     int pcost = x264_slicetype_frame_cost( h, &a, frames, i+0, j+1, j+1, 1 );
  752.                     if( pcost > pthresh*i_mb_count || frames[j+1]->i_intra_mbs[j-i+1] > i_mb_count/3 )
  753.                     {
  754.                         frames[j]->i_type = X264_TYPE_P;
  755.                         break;
  756.                     }
  757.                     else
  758.                         frames[j]->i_type = X264_TYPE_B;
  759.                 }
  760.                 i = j;
  761.             }
  762.             frames[i+!i]->i_type = X264_TYPE_P;
  763.             num_bframes = 0;
  764.             while( num_bframes < num_frames && frames[num_bframes+1]->i_type == X264_TYPE_B )
  765.                 num_bframes++;
  766.         }
  767.         else
  768.         {
  769.             num_bframes = X264_MIN(num_frames-1, h->param.i_bframe);
  770.             for( j = 1; j < num_frames; j++ )
  771.                 frames[j]->i_type = (j%(num_bframes+1)) ? X264_TYPE_B : X264_TYPE_P;
  772.             frames[num_frames]->i_type = X264_TYPE_P;
  773.         }
  774.         /* Check scenecut on the first minigop. */
  775.         for( j = 1; j < num_bframes+1; j++ )
  776.             if( h->param.i_scenecut_threshold && scenecut( h, &a, frames, j, j+1, 0 ) )
  777.             {
  778.                 frames[j]->i_type = X264_TYPE_P;
  779.                 num_analysed_frames = j;
  780.                 break;
  781.             }
  782.         reset_start = keyframe ? 1 : X264_MIN( num_bframes+2, num_analysed_frames+1 );
  783.     }
  784.     else
  785.     {
  786.         for( j = 1; j < num_frames; j++ )
  787.             frames[j]->i_type = X264_TYPE_P;
  788.         reset_start = !keyframe + 1;
  789.         num_bframes = 0;
  790.     }
  791.     for( j = 1; j <= num_frames; j++ )
  792.         if( frames[j]->i_type == X264_TYPE_AUTO )
  793.             frames[j]->i_type = X264_TYPE_P;
  794.     /* Perform the actual macroblock tree analysis.
  795.      * Don't go farther than the maximum keyframe interval; this helps in short GOPs. */
  796.     if( h->param.rc.b_mb_tree )
  797.         x264_macroblock_tree( h, &a, frames, X264_MIN(num_frames, h->param.i_keyint_max), keyframe );
  798.     /* Enforce keyframe limit. */
  799.     for( j = 0; j < num_frames; j++ )
  800.     {
  801.         if( ((j-keyint_limit) % h->param.i_keyint_max) == 0 )
  802.         {
  803.             if( j && h->param.i_keyint_max > 1 )
  804.                 frames[j]->i_type = X264_TYPE_P;
  805.             frames[j+1]->i_type = X264_TYPE_IDR;
  806.             reset_start = X264_MIN( reset_start, j+2 );
  807.         }
  808.     }
  809.     if( h->param.rc.i_vbv_buffer_size )
  810.         x264_vbv_lookahead( h, &a, frames, num_frames, keyframe );
  811.     /* Restore frametypes for all frames that haven't actually been decided yet. */
  812.     for( j = reset_start; j <= num_frames; j++ )
  813.         frames[j]->i_type = X264_TYPE_AUTO;
  814. }
  815. void x264_slicetype_decide( x264_t *h )
  816. {
  817.     x264_frame_t *frm;
  818.     int bframes;
  819.     int i;
  820.     if( !h->lookahead->next.i_size )
  821.         return;
  822.     if( h->param.rc.b_stat_read )
  823.     {
  824.         /* Use the frame types from the first pass */
  825.         for( i = 0; i < h->lookahead->next.i_size; i++ )
  826.             h->lookahead->next.list[i]->i_type =
  827.                 x264_ratecontrol_slice_type( h, h->lookahead->next.list[i]->i_frame );
  828.     }
  829.     else if( (h->param.i_bframe && h->param.i_bframe_adaptive)
  830.              || h->param.i_scenecut_threshold
  831.              || h->param.rc.b_mb_tree
  832.              || (h->param.rc.i_vbv_buffer_size && h->param.rc.i_lookahead) )
  833.         x264_slicetype_analyse( h, 0 );
  834.     for( bframes = 0;; bframes++ )
  835.     {
  836.         frm = h->lookahead->next.list[bframes];
  837.         /* Limit GOP size */
  838.         if( frm->i_frame - h->lookahead->i_last_idr >= h->param.i_keyint_max )
  839.         {
  840.             if( frm->i_type == X264_TYPE_AUTO )
  841.                 frm->i_type = X264_TYPE_IDR;
  842.             if( frm->i_type != X264_TYPE_IDR )
  843.                 x264_log( h, X264_LOG_WARNING, "specified frame type (%d) is not compatible with keyframe intervaln", frm->i_type );
  844.         }
  845.         if( frm->i_type == X264_TYPE_IDR )
  846.         {
  847.             /* Close GOP */
  848.             h->lookahead->i_last_idr = frm->i_frame;
  849.             if( bframes > 0 )
  850.             {
  851.                 bframes--;
  852.                 h->lookahead->next.list[bframes]->i_type = X264_TYPE_P;
  853.             }
  854.         }
  855.         if( bframes == h->param.i_bframe ||
  856.             !h->lookahead->next.list[bframes+1] )
  857.         {
  858.             if( IS_X264_TYPE_B( frm->i_type ) )
  859.                 x264_log( h, X264_LOG_WARNING, "specified frame type is not compatible with max B-framesn" );
  860.             if( frm->i_type == X264_TYPE_AUTO
  861.                 || IS_X264_TYPE_B( frm->i_type ) )
  862.                 frm->i_type = X264_TYPE_P;
  863.         }
  864.         if( frm->i_type == X264_TYPE_AUTO )
  865.             frm->i_type = X264_TYPE_B;
  866.         else if( !IS_X264_TYPE_B( frm->i_type ) ) break;
  867.     }
  868.     if( bframes )
  869.         h->lookahead->next.list[bframes-1]->b_last_minigop_bframe = 1;
  870.     h->lookahead->next.list[bframes]->i_bframes = bframes;
  871.     /* calculate the frame costs ahead of time for x264_rc_analyse_slice while we still have lowres */
  872.     if( h->param.rc.i_rc_method != X264_RC_CQP )
  873.     {
  874.         x264_mb_analysis_t a;
  875.         x264_frame_t *frames[X264_BFRAME_MAX+2] = { NULL, };
  876.         int p0=0, p1, b;
  877.         x264_lowres_context_init( h, &a );
  878.         if( IS_X264_TYPE_I( h->lookahead->next.list[bframes]->i_type ) )
  879.             p1 = b = 0;
  880.         else // P
  881.             p1 = b = bframes + 1;
  882.         frames[p0] = h->lookahead->last_nonb;
  883.         frames[b] = h->lookahead->next.list[bframes];
  884.         x264_slicetype_frame_cost( h, &a, frames, p0, p1, b, 0 );
  885.         /* We need the intra costs for row SATDs. */
  886.         if( b && h->param.rc.i_vbv_buffer_size )
  887.             x264_slicetype_frame_cost( h, &a, frames, b, b, b, 0 );
  888.     }
  889. }
  890. int x264_rc_analyse_slice( x264_t *h )
  891. {
  892.     x264_frame_t *frames[X264_BFRAME_MAX+2] = { NULL, };
  893.     int p0=0, p1, b;
  894.     int cost;
  895.     if( IS_X264_TYPE_I(h->fenc->i_type) )
  896.         p1 = b = 0;
  897.     else // P
  898.         p1 = b = h->fenc->i_bframes + 1;
  899.     frames[p0] = h->fref0[0];
  900.     frames[b] = h->fenc;
  901.     /* cost should have been already calculated by x264_slicetype_decide */
  902.     cost = frames[b]->i_cost_est[b-p0][p1-b];
  903.     assert( cost >= 0 );
  904.     if( h->param.rc.b_mb_tree && !h->param.rc.b_stat_read )
  905.     {
  906.         cost = x264_slicetype_frame_cost_recalculate( h, frames, p0, p1, b );
  907.         if( b && h->param.rc.i_vbv_buffer_size )
  908.             x264_slicetype_frame_cost_recalculate( h, frames, b, b, b );
  909.     }
  910.     /* In AQ, use the weighted score instead. */
  911.     else if( h->param.rc.i_aq_mode )
  912.         cost = frames[b]->i_cost_est_aq[b-p0][p1-b];
  913.     h->fenc->i_row_satd = h->fenc->i_row_satds[b-p0][p1-b];
  914.     h->fdec->i_row_satd = h->fdec->i_row_satds[b-p0][p1-b];
  915.     h->fdec->i_satd = cost;
  916.     memcpy( h->fdec->i_row_satd, h->fenc->i_row_satd, h->sps->i_mb_height * sizeof(int) );
  917.     if( !IS_X264_TYPE_I(h->fenc->i_type) )
  918.         memcpy( h->fdec->i_row_satds[0][0], h->fenc->i_row_satds[0][0], h->sps->i_mb_height * sizeof(int) );
  919.     return cost;
  920. }