slicetype_decision.c
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:14k
源码类别:

Audio

开发平台:

Visual C++

  1. /*****************************************************************************
  2.  * slicetype_decision.c: h264 encoder library
  3.  *****************************************************************************
  4.  * Copyright (C) 2005 Loren Merritt
  5.  *
  6.  * Authors: Loren Merritt <lorenm@u.washington.edu>
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  21.  *****************************************************************************/
  22. #include <string.h>
  23. #include <math.h>
  24. #include <limits.h>
  25. #include "common/common.h"
  26. #include "common/cpu.h"
  27. #include "macroblock.h"
  28. #include "me.h"
  29. static void x264_lowres_context_init( x264_t *h, x264_mb_analysis_t *a )
  30. {
  31.     a->i_qp = 12; // arbitrary, but low because SATD scores are 1/4 normal
  32.     a->i_lambda = i_qp0_cost_table[ a->i_qp ];
  33.     x264_mb_analyse_load_costs( h, a );
  34.     h->mb.i_me_method = X264_MIN( X264_ME_HEX, h->param.analyse.i_me_method ); // maybe dia?
  35.     h->mb.i_subpel_refine = 4; // 3 should be enough, but not tweaking for speed now
  36.     h->mb.b_chroma_me = 0;
  37. }
  38. int x264_slicetype_mb_cost( x264_t *h, x264_mb_analysis_t *a,
  39.                             x264_frame_t **frames, int p0, int p1, int b,
  40.                             int dist_scale_factor )
  41. {
  42.     x264_frame_t *fref0 = frames[p0];
  43.     x264_frame_t *fref1 = frames[p1];
  44.     x264_frame_t *fenc  = frames[b];
  45.     const int b_bidir = (b < p1);
  46.     const int i_mb_x = h->mb.i_mb_x;
  47.     const int i_mb_y = h->mb.i_mb_y;
  48.     const int i_mb_stride = h->sps->i_mb_width;
  49.     const int i_mb_xy = i_mb_x + i_mb_y * i_mb_stride;
  50.     const int i_stride = fenc->i_stride_lowres;
  51.     const int i_pel_offset = 8 * ( i_mb_x + i_mb_y * i_stride );
  52.     uint8_t pix1[9*9], pix2[8*8];
  53.     x264_me_t m[2];
  54.     int mvc[4][2], i_mvc;
  55.     int i_bcost = COST_MAX;
  56.     int i_cost_bak;
  57.     int l, i;
  58.     h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
  59.     h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fenc[0], FENC_STRIDE, &fenc->lowres[0][i_pel_offset], i_stride, 8 );
  60.     if( !p0 && !p1 && !b )
  61.         goto lowres_intra_mb;
  62.     // no need for h->mb.mv_min[]
  63.     h->mb.mv_min_fpel[0] = -8*h->mb.i_mb_x - 4;
  64.     h->mb.mv_max_fpel[0] = 8*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 4;
  65.     h->mb.mv_min_spel[0] = 4*( h->mb.mv_min_fpel[0] - 8 );
  66.     h->mb.mv_max_spel[0] = 4*( h->mb.mv_max_fpel[0] + 8 );
  67.     if( h->mb.i_mb_x <= 1)
  68.     {
  69.         h->mb.mv_min_fpel[1] = -8*h->mb.i_mb_y - 4;
  70.         h->mb.mv_max_fpel[1] = 8*( h->sps->i_mb_height - h->mb.i_mb_y - 1 ) + 4;
  71.         h->mb.mv_min_spel[1] = 4*( h->mb.mv_min_fpel[1] - 8 );
  72.         h->mb.mv_max_spel[1] = 4*( h->mb.mv_max_fpel[1] + 8 );
  73.     }
  74. #define LOAD_HPELS_LUMA(dst, src) 
  75.     { 
  76.         (dst)[0] = &(src)[0][i_pel_offset]; 
  77.         (dst)[1] = &(src)[1][i_pel_offset]; 
  78.         (dst)[2] = &(src)[2][i_pel_offset]; 
  79.         (dst)[3] = &(src)[3][i_pel_offset]; 
  80.     }
  81. #define SAVE_MVS( mv0, mv1 ) 
  82.     { 
  83.         fenc->mv[0][i_mb_xy][0] = mv0[0]; 
  84.         fenc->mv[0][i_mb_xy][1] = mv0[1]; 
  85.         if( b_bidir ) 
  86.         { 
  87.             fenc->mv[1][i_mb_xy][0] = mv1[0]; 
  88.             fenc->mv[1][i_mb_xy][1] = mv1[1]; 
  89.         } 
  90.     }
  91. #define CLIP_MV( mv ) 
  92.     { 
  93.         mv[0] = x264_clip3( mv[0], h->mb.mv_min_spel[0], h->mb.mv_max_spel[0] ); 
  94.         mv[1] = x264_clip3( mv[1], h->mb.mv_min_spel[1], h->mb.mv_max_spel[1] ); 
  95.     }
  96. #define TRY_BIDIR( mv0, mv1, penalty ) 
  97.     { 
  98.         int stride2 = 8; 
  99.         uint8_t *src2; 
  100.         int i_cost; 
  101.         h->mc.mc_luma( m[0].p_fref, m[0].i_stride[0], pix1, 8, 
  102.                        (mv0)[0], (mv0)[1], 8, 8 ); 
  103.         src2 = h->mc.get_ref( m[1].p_fref, m[1].i_stride[0], pix2, &stride2, 
  104.                        (mv1)[0], (mv1)[1], 8, 8 ); 
  105.         h->mc.avg[PIXEL_8x8]( pix1, 8, src2, stride2 ); 
  106.         i_cost = penalty + h->pixf.mbcmp[PIXEL_8x8]( 
  107.                            m[0].p_fenc[0], FENC_STRIDE, pix1, 8 ); 
  108.         if( i_bcost > i_cost ) 
  109.         { 
  110.             i_bcost = i_cost; 
  111.             SAVE_MVS( mv0, mv1 ); 
  112.         } 
  113.     }
  114.     m[0].i_pixel = PIXEL_8x8;
  115.     m[0].p_cost_mv = a->p_cost_mv;
  116.     m[0].i_stride[0] = i_stride;
  117.     m[0].p_fenc[0] = h->mb.pic.p_fenc[0];
  118.     LOAD_HPELS_LUMA( m[0].p_fref, fref0->lowres );
  119.     if( b_bidir )
  120.     {
  121.         int16_t *mvr = fref1->mv[0][i_mb_xy];
  122.         int dmv[2][2];
  123.         int mv0[2] = {0,0};
  124.         m[1] = m[0];
  125.         LOAD_HPELS_LUMA( m[1].p_fref, fref1->lowres );
  126.         dmv[0][0] = ( mvr[0] * dist_scale_factor + 128 ) >> 8;
  127.         dmv[0][1] = ( mvr[1] * dist_scale_factor + 128 ) >> 8;
  128.         dmv[1][0] = dmv[0][0] - mvr[0];
  129.         dmv[1][1] = dmv[0][1] - mvr[1];
  130.         CLIP_MV( dmv[0] );
  131.         CLIP_MV( dmv[1] );
  132.         TRY_BIDIR( dmv[0], dmv[1], 0 );
  133.         if( dmv[0][0] || dmv[0][1] || dmv[1][0] || dmv[1][1] )
  134.            TRY_BIDIR( mv0, mv0, 0 );
  135. //      if( i_bcost < 60 ) // arbitrary threshold
  136. //          return i_bcost;
  137.     }
  138.     i_cost_bak = i_bcost;
  139.     for( l = 0; l < 1 + b_bidir; l++ )
  140.     {
  141.         int16_t (*fenc_mv)[2] = &fenc->mv[l][i_mb_xy];
  142.         mvc[0][0] = fenc_mv[-1][0];
  143.         mvc[0][1] = fenc_mv[-1][1];
  144.         mvc[1][0] = fenc_mv[-i_mb_stride][0];
  145.         mvc[1][1] = fenc_mv[-i_mb_stride][1];
  146.         mvc[2][0] = fenc_mv[-i_mb_stride+1][0];
  147.         mvc[2][1] = fenc_mv[-i_mb_stride+1][1];
  148.         mvc[3][0] = fenc_mv[-i_mb_stride-1][0];
  149.         mvc[3][1] = fenc_mv[-i_mb_stride-1][1];
  150.         m[l].mvp[0] = x264_median( mvc[0][0], mvc[1][0], mvc[2][0] );
  151.         m[l].mvp[1] = x264_median( mvc[0][1], mvc[1][1], mvc[2][1] );
  152.         i_mvc = 4;
  153.         x264_me_search( h, &m[l], mvc, i_mvc );
  154.         i_bcost = X264_MIN( i_bcost, m[l].cost + 3 );
  155.     }
  156.     if( b_bidir && (m[0].mv[0] || m[0].mv[1] || m[1].mv[0] || m[1].mv[1]) )
  157.         TRY_BIDIR( m[0].mv, m[1].mv, 5 );
  158.     if( i_bcost < i_cost_bak )
  159.         SAVE_MVS( m[0].mv, m[1].mv );
  160. lowres_intra_mb:
  161.     {
  162.         uint8_t pix_buf[9*FDEC_STRIDE];
  163.         uint8_t *pix = &pix_buf[8+FDEC_STRIDE - 1];
  164.         uint8_t *src = &fenc->lowres[0][i_pel_offset - 1];
  165.         int intra_penalty = 5 + 10 * b_bidir;
  166.         i_cost_bak = i_bcost;
  167.         memcpy( pix-FDEC_STRIDE, src-i_stride, 9 );
  168.         for( i=0; i<8; i++ )
  169.             pix[i*FDEC_STRIDE] = src[i*i_stride];
  170.         pix++;
  171.         for( i = I_PRED_CHROMA_DC; i <= I_PRED_CHROMA_P; i++ )
  172.         {
  173.             int i_cost;
  174.             h->predict_8x8c[i]( pix );
  175.             i_cost = h->pixf.mbcmp[PIXEL_8x8]( pix, FDEC_STRIDE, h->mb.pic.p_fenc[0], FENC_STRIDE ) + intra_penalty;
  176.             i_bcost = X264_MIN( i_bcost, i_cost );
  177.         }
  178.         if( i_bcost != i_cost_bak )
  179.         {
  180.             if( !b_bidir )
  181.                 fenc->i_intra_mbs[b-p0]++;
  182.             if( p1 > p0+1 )
  183.                 i_bcost = i_bcost * 9 / 8; // arbitray penalty for I-blocks in and after B-frames
  184.         }
  185.     }
  186.     return i_bcost;
  187. }
  188. #undef TRY_BIDIR
  189. #undef SAVE_MVS
  190. int x264_slicetype_frame_cost( x264_t *h, x264_mb_analysis_t *a,
  191.                                x264_frame_t **frames, int p0, int p1, int b )
  192. {
  193.     int i_score = 0;
  194.     int dist_scale_factor = 128;
  195.     /* Check whether we already evaluated this frame
  196.      * If we have tried this frame as P, then we have also tried
  197.      * the preceding frames as B. (is this still true?) */
  198.     if( frames[b]->i_cost_est[b-p0][p1-b] >= 0 )
  199.         return frames[b]->i_cost_est[b-p0][p1-b];
  200.     /* Init MVs so that we don't have to check edge conditions when loading predictors. */
  201.     /* FIXME: not needed every time */
  202.     memset( frames[b]->mv[0], 0, h->sps->i_mb_height * h->sps->i_mb_width * 2*sizeof(int16_t) );
  203.     if( b != p1 )
  204.         memset( frames[b]->mv[1], 0, h->sps->i_mb_height * h->sps->i_mb_width * 2*sizeof(int16_t) );
  205.     if( b == p1 )
  206.         frames[b]->i_intra_mbs[b-p0] = 0;
  207.     if( p1 != p0 )
  208.         dist_scale_factor = ( ((b-p0) << 8) + ((p1-p0) >> 1) ) / (p1-p0);
  209.     /* Skip the outermost ring of macroblocks, to simplify mv range and intra prediction. */
  210.     for( h->mb.i_mb_y = 1; h->mb.i_mb_y < h->sps->i_mb_height - 1; h->mb.i_mb_y++ )
  211.         for( h->mb.i_mb_x = 1; h->mb.i_mb_x < h->sps->i_mb_width - 1; h->mb.i_mb_x++ )
  212.             i_score += x264_slicetype_mb_cost( h, a, frames, p0, p1, b, dist_scale_factor );
  213.     if( b != p1 )
  214.         i_score = i_score * 100 / (120 + h->param.i_bframe_bias);
  215.     frames[b]->i_cost_est[b-p0][p1-b] = i_score;
  216. //  fprintf( stderr, "frm %d %c(%d,%d): %6d I:%d  n", frames[b]->i_frame,
  217. //           (p1==0?'I':b<p1?'B':'P'), b-p0, p1-b, i_score, frames[b]->i_intra_mbs[b-p0] );
  218.     x264_cpu_restore( h->param.cpu );
  219.     return i_score;
  220. }
  221. void x264_slicetype_analyse( x264_t *h )
  222. {
  223.     x264_mb_analysis_t a;
  224.     x264_frame_t *frames[X264_BFRAME_MAX+3] = { NULL, };
  225.     int num_frames;
  226.     int keyint_limit;
  227.     int j;
  228.     int i_mb_count = (h->sps->i_mb_width - 2) * (h->sps->i_mb_height - 2);
  229.     int cost1p0, cost2p0, cost1b1, cost2p1;
  230.     if( !h->frames.last_nonb )
  231.         return;
  232.     frames[0] = h->frames.last_nonb;
  233.     for( j = 0; h->frames.next[j]; j++ )
  234.         frames[j+1] = h->frames.next[j];
  235.     keyint_limit = h->param.i_keyint_max - frames[0]->i_frame + h->frames.i_last_idr - 1;
  236.     num_frames = X264_MIN( j, keyint_limit );
  237.     if( num_frames == 0 )
  238.         return;
  239.     if( num_frames == 1 )
  240.     {
  241. no_b_frames:
  242.         frames[1]->i_type = X264_TYPE_P;
  243.         return;
  244.     }
  245.     x264_lowres_context_init( h, &a );
  246.     cost2p1 = x264_slicetype_frame_cost( h, &a, frames, 0, 2, 2 );
  247.     if( frames[2]->i_intra_mbs[2] > i_mb_count / 2 )
  248.         goto no_b_frames;
  249.     cost1b1 = x264_slicetype_frame_cost( h, &a, frames, 0, 2, 1 );
  250.     cost1p0 = x264_slicetype_frame_cost( h, &a, frames, 0, 1, 1 );
  251.     cost2p0 = x264_slicetype_frame_cost( h, &a, frames, 1, 2, 2 );
  252. //  fprintf( stderr, "PP: %d + %d <=> BP: %d + %d n",
  253. //           cost1p0, cost2p0, cost1b1, cost2p1 );
  254.     if( cost1p0 + cost2p0 < cost1b1 + cost2p1 )
  255.         goto no_b_frames;
  256. // arbitrary and untuned
  257. #define INTER_THRESH 300
  258. #define P_SENS_BIAS (50 - h->param.i_bframe_bias)
  259.     frames[1]->i_type = X264_TYPE_B;
  260.     for( j = 2; j <= X264_MIN( h->param.i_bframe, num_frames-1 ); j++ )
  261.     {
  262.         int pthresh = X264_MAX(INTER_THRESH - P_SENS_BIAS * (j-1), INTER_THRESH/10);
  263.         int pcost = x264_slicetype_frame_cost( h, &a, frames, 0, j+1, j+1 );
  264. //      fprintf( stderr, "frm%d+%d: %d <=> %d, I:%d/%d n",
  265. //               frames[0]->i_frame, j-1, pthresh, pcost/i_mb_count,
  266. //               frames[j+1]->i_intra_mbs[j+1], i_mb_count );
  267.         if( pcost > pthresh*i_mb_count || frames[j+1]->i_intra_mbs[j+1] > i_mb_count/3 )
  268.         {
  269.             frames[j]->i_type = X264_TYPE_P;
  270.             break;
  271.         }
  272.         else
  273.             frames[j]->i_type = X264_TYPE_B;
  274.     }
  275. }
  276. void x264_slicetype_decide( x264_t *h )
  277. {
  278.     x264_frame_t *frm;
  279.     int bframes;
  280.     int i;
  281.     if( h->frames.next[0] == NULL )
  282.         return;
  283.     if( h->param.rc.b_stat_read )
  284.     {
  285.         /* Use the frame types from the first pass */
  286.         for( i = 0; h->frames.next[i] != NULL; i++ )
  287.             h->frames.next[i]->i_type =
  288.                 x264_ratecontrol_slice_type( h, h->frames.next[i]->i_frame );
  289.     }
  290.     else if( h->param.i_bframe && h->param.b_bframe_adaptive )
  291.         x264_slicetype_analyse( h );
  292.     for( bframes = 0;; bframes++ )
  293.     {
  294.         frm = h->frames.next[bframes];
  295.         /* Limit GOP size */
  296.         if( frm->i_frame - h->frames.i_last_idr >= h->param.i_keyint_max )
  297.         {
  298.             if( frm->i_type == X264_TYPE_AUTO )
  299.                 frm->i_type = X264_TYPE_IDR;
  300.             if( frm->i_type != X264_TYPE_IDR )
  301.                 x264_log( h, X264_LOG_ERROR, "specified frame type (%d) is not compatible with keyframe intervaln", frm->i_type );
  302.         }
  303.         if( frm->i_type == X264_TYPE_IDR )
  304.         {
  305.             /* Close GOP */
  306.             if( bframes > 0 )
  307.             {
  308.                 bframes--;
  309.                 h->frames.next[bframes]->i_type = X264_TYPE_P;
  310.             }
  311.             else
  312.             {
  313.                 h->i_frame_num = 0;
  314.             }
  315.         }
  316.         if( bframes == h->param.i_bframe
  317.             || h->frames.next[bframes+1] == NULL )
  318.         {
  319.             if( IS_X264_TYPE_B( frm->i_type ) )
  320.                 x264_log( h, X264_LOG_ERROR, "specified frame type is not compatible with max B-framesn" );
  321.             if( frm->i_type == X264_TYPE_AUTO
  322.                 || IS_X264_TYPE_B( frm->i_type ) )
  323.                 frm->i_type = X264_TYPE_P;
  324.         }
  325.         if( frm->i_type != X264_TYPE_AUTO && frm->i_type != X264_TYPE_B && frm->i_type != X264_TYPE_BREF )
  326.             break;
  327.         frm->i_type = X264_TYPE_B;
  328.     }
  329. }
  330. int x264_rc_analyse_slice( x264_t *h )
  331. {
  332.     int p1 = 0;
  333.     x264_mb_analysis_t a;
  334.     x264_frame_t *frames[X264_BFRAME_MAX+2] = { NULL, };
  335.     if( IS_X264_TYPE_I(h->fenc->i_type) )
  336.         return x264_slicetype_frame_cost( h, &a, &h->fenc, 0, 0, 0 );
  337.     while( h->frames.current[p1] && IS_X264_TYPE_B( h->frames.current[p1]->i_type ) )
  338.         p1++;
  339.     p1++;
  340.     if( h->fenc->i_cost_est[p1][0] >= 0 )
  341.         return h->fenc->i_cost_est[p1][0];
  342.     frames[0] = h->fref0[0];
  343.     frames[p1] = h->fenc;
  344.     x264_lowres_context_init( h, &a );
  345.     return x264_slicetype_frame_cost( h, &a, frames, 0, p1, p1 );
  346. }