Advertisement
06_taro

OreAQ-core-Taro-r2074.diff

Apr 20th, 2011
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 40.66 KB | None | 0 0
  1. diff -uNrp a/common/common.c b/common/common.c
  2. --- a/common/common.c   2011-08-25 10:27:19 +0800
  3. +++ b/common/common.c   2011-08-25 11:29:07 +0800
  4. @@ -106,8 +106,27 @@ void x264_param_default( x264_param_t *p
  5.      param->rc.i_qp_step = 4;
  6.      param->rc.f_ip_factor = 1.4;
  7.      param->rc.f_pb_factor = 1.3;
  8. -    param->rc.i_aq_mode = X264_AQ_VARIANCE;
  9. -    param->rc.f_aq_strength = 1.0;
  10. +    param->rc.i_aq_mode = X264_AQ_ORE;
  11. +    param->rc.f_aq_strength = 0.5;
  12. +    param->rc.f_aq_strengths[0][0] = 0;
  13. +    param->rc.f_aq_strengths[0][1] = 0;
  14. +    param->rc.f_aq_strengths[0][2] = 0;
  15. +    param->rc.f_aq_strengths[0][3] = 0;
  16. +    param->rc.f_aq_strengths[1][0] = 0;
  17. +    param->rc.f_aq_strengths[1][1] = 0;
  18. +    param->rc.f_aq_strengths[1][2] = 0;
  19. +    param->rc.f_aq_strengths[1][3] = 0;
  20. +    param->rc.f_aq_sensitivity = 10;
  21. +    param->rc.f_aq_ifactor[0] = 1.0;
  22. +    param->rc.f_aq_ifactor[1] = 1.0;
  23. +    param->rc.f_aq_pfactor[0] = 1.0;
  24. +    param->rc.f_aq_pfactor[1] = 1.0;
  25. +    param->rc.f_aq_bfactor[0] = 1.0;
  26. +    param->rc.f_aq_bfactor[1] = 1.0;
  27. +    param->rc.b_aq_boundary = 0;
  28. +    param->rc.i_aq_boundary[0] = 192;
  29. +    param->rc.i_aq_boundary[1] = 64;
  30. +    param->rc.i_aq_boundary[2] = 24;
  31.      param->rc.i_lookahead = 40;
  32.  
  33.      param->rc.b_stat_write = 0;
  34. @@ -359,7 +378,7 @@ static int x264_param_apply_tune( x264_p
  35.          else if( !strncasecmp( s, "ssim", 4 ) )
  36.          {
  37.              if( psy_tuning_used++ ) goto psy_failure;
  38. -            param->rc.i_aq_mode = X264_AQ_AUTOVARIANCE;
  39. +            param->rc.i_aq_mode = X264_AQ_MIXORE;
  40.              param->analyse.b_psy = 0;
  41.          }
  42.          else if( !strncasecmp( s, "fastdecode", 10 ) )
  43. @@ -972,7 +991,61 @@ int x264_param_parse( x264_param_t *p, c
  44.      OPT("aq-mode")
  45.          p->rc.i_aq_mode = atoi(value);
  46.      OPT("aq-strength")
  47. -        p->rc.f_aq_strength = atof(value);
  48. +    {
  49. +        if( 8 == sscanf( value, "%f:%f:%f:%f:%f:%f:%f:%f",
  50. +                         &p->rc.f_aq_strengths[0][0], &p->rc.f_aq_strengths[1][0], &p->rc.f_aq_strengths[0][1], &p->rc.f_aq_strengths[1][1],
  51. +                         &p->rc.f_aq_strengths[0][2], &p->rc.f_aq_strengths[1][2], &p->rc.f_aq_strengths[0][3], &p->rc.f_aq_strengths[1][3] ) ||
  52. +            8 == sscanf( value, "%f,%f,%f,%f,%f,%f,%f,%f",
  53. +                         &p->rc.f_aq_strengths[0][0], &p->rc.f_aq_strengths[1][0], &p->rc.f_aq_strengths[0][1], &p->rc.f_aq_strengths[1][1],
  54. +                         &p->rc.f_aq_strengths[0][2], &p->rc.f_aq_strengths[1][2], &p->rc.f_aq_strengths[0][3], &p->rc.f_aq_strengths[1][3] ) )
  55. +            p->rc.f_aq_strength = 0.0;
  56. +        else if( 2 == sscanf( value, "%f:%f", &p->rc.f_aq_strengths[0][0], &p->rc.f_aq_strengths[1][0] ) ||
  57. +                 2 == sscanf( value, "%f,%f", &p->rc.f_aq_strengths[0][0], &p->rc.f_aq_strengths[1][0] ) )
  58. +        {
  59. +            p->rc.f_aq_strength = 0.0;
  60. +            for( i = 0; i < 2; i++ )
  61. +                for( int j = 1; j < 4; j++ )
  62. +                    p->rc.f_aq_strengths[i][j] = p->rc.f_aq_strengths[i][0];
  63. +        }
  64. +        else if( sscanf( value, "%f", &p->rc.f_aq_strength ) )
  65. +            for( i = 0; i < 2; i++ )
  66. +                for( int j = 0; j < 4; j++ )
  67. +                    p->rc.f_aq_strengths[i][j] = p->rc.f_aq_strength;
  68. +    }
  69. +    OPT("aq-sensitivity")
  70. +        p->rc.f_aq_sensitivity = atof(value);
  71. +    OPT("aq-ifactor")
  72. +        if( 2 == sscanf( value, "%f:%f", &p->rc.f_aq_ifactor[0], &p->rc.f_aq_ifactor[1] ) ||
  73. +            2 == sscanf( value, "%f,%f", &p->rc.f_aq_ifactor[0], &p->rc.f_aq_ifactor[1] ) )
  74. +        { }
  75. +        else if( sscanf( value, "%f", &p->rc.f_aq_ifactor[0] ) )
  76. +            p->rc.f_aq_ifactor[1] = p->rc.f_aq_ifactor[0];
  77. +        else
  78. +            p->rc.f_aq_ifactor[1] = p->rc.f_aq_ifactor[0] = 1.0;
  79. +    OPT("aq-pfactor")
  80. +        if( 2 == sscanf( value, "%f:%f", &p->rc.f_aq_pfactor[0], &p->rc.f_aq_pfactor[1] ) ||
  81. +            2 == sscanf( value, "%f,%f", &p->rc.f_aq_pfactor[0], &p->rc.f_aq_pfactor[1] ) )
  82. +        { }
  83. +        else if( sscanf( value, "%f", &p->rc.f_aq_pfactor[0] ) )
  84. +            p->rc.f_aq_pfactor[1] = p->rc.f_aq_pfactor[0];
  85. +        else
  86. +            p->rc.f_aq_pfactor[1] = p->rc.f_aq_pfactor[0] = 1.0;
  87. +    OPT("aq-bfactor")
  88. +        if( 2 == sscanf( value, "%f:%f", &p->rc.f_aq_bfactor[0], &p->rc.f_aq_bfactor[1] ) ||
  89. +            2 == sscanf( value, "%f,%f", &p->rc.f_aq_bfactor[0], &p->rc.f_aq_bfactor[1] ) )
  90. +        { }
  91. +        else if( sscanf( value, "%f", &p->rc.f_aq_bfactor[0] ) )
  92. +            p->rc.f_aq_bfactor[1] = p->rc.f_aq_bfactor[0];
  93. +        else
  94. +            p->rc.f_aq_bfactor[1] = p->rc.f_aq_bfactor[0] = 1.0;
  95. +    OPT("aq-boundary")
  96. +    {
  97. +        if( 3 == sscanf( value, "%d:%d:%d", &p->rc.i_aq_boundary[0], &p->rc.i_aq_boundary[1], &p->rc.i_aq_boundary[2] ) ||
  98. +            3 == sscanf( value, "%d,%d,%d", &p->rc.i_aq_boundary[0], &p->rc.i_aq_boundary[1], &p->rc.i_aq_boundary[2] ) )
  99. +            p->rc.b_aq_boundary = 1;
  100. +        else
  101. +            p->rc.i_aq_boundary[0] = p->rc.i_aq_boundary[1] = p->rc.i_aq_boundary[2] = 0;
  102. +    }
  103.      OPT("fgo")
  104.          p->analyse.i_fgo = atoi(value);
  105.      OPT("fade-compensate")
  106. @@ -1412,7 +1485,16 @@ char *x264_param2string( x264_param_t *p
  107.              s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
  108.          s += sprintf( s, " aq=%d", p->rc.i_aq_mode );
  109.          if( p->rc.i_aq_mode )
  110. -            s += sprintf( s, ":%.2f", p->rc.f_aq_strength );
  111. +        {
  112. +            s += sprintf( s, " aq-strength=%.2f:%.2f:%.2f:%.2f:%.2f:%.2f:%.2f:%.2f",
  113. +                          p->rc.f_aq_strengths[0][0], p->rc.f_aq_strengths[1][0], p->rc.f_aq_strengths[0][1], p->rc.f_aq_strengths[1][1],
  114. +                          p->rc.f_aq_strengths[0][2], p->rc.f_aq_strengths[1][2], p->rc.f_aq_strengths[0][3], p->rc.f_aq_strengths[1][3] );
  115. +            s += sprintf( s, " aq-sensitivity=%.2f", p->rc.f_aq_sensitivity );
  116. +            s += sprintf( s, " aq-ifactor=%.2f:%.2f", p->rc.f_aq_ifactor[0], p->rc.f_aq_ifactor[1] );
  117. +            s += sprintf( s, " aq-pfactor=%.2f:%.2f", p->rc.f_aq_pfactor[0], p->rc.f_aq_pfactor[1] );
  118. +            s += sprintf( s, " aq-bfactor=%.2f:%.2f", p->rc.f_aq_bfactor[0], p->rc.f_aq_bfactor[1] );
  119. +            s += sprintf( s, " aq-boundary=%d:%d:%d", p->rc.i_aq_boundary[0], p->rc.i_aq_boundary[1], p->rc.i_aq_boundary[2] );
  120. +        }
  121.          if( p->rc.psz_zones )
  122.              s += sprintf( s, " zones=%s", p->rc.psz_zones );
  123.          else if( p->rc.i_zones )
  124. diff -uNrp a/common/common.h b/common/common.h
  125. --- a/common/common.h   2011-08-25 10:24:33 +0800
  126. +++ b/common/common.h   2011-08-25 11:30:10 +0800
  127. @@ -840,6 +840,13 @@ struct x264_t
  128.              int64_t i_ssd[3];
  129.              double f_ssim;
  130.              int i_ssim_cnt;
  131. +
  132. +            /* AQ stats per frame */
  133. +            uint64_t i_aq_count[5];
  134. +            uint64_t i_aq_result[5][3];
  135. +            uint64_t i_aq_change[2*QP_MAX_SPEC+1];
  136. +            int i_aq_change_min;
  137. +            int i_aq_change_max;
  138.          } frame;
  139.  
  140.          /* Cumulated stats */
  141. @@ -870,6 +877,13 @@ struct x264_t
  142.          int     i_direct_frames[2];
  143.          /* num p-frames weighted */
  144.          int     i_wpred[2];
  145. +        /* */
  146. +        uint64_t i_aq_count_total;
  147. +        uint64_t i_aq_count[5];
  148. +        uint64_t i_aq_result[5][3];
  149. +        uint64_t i_aq_change[2*QP_MAX_SPEC+1];
  150. +        int i_aq_change_min;
  151. +        int i_aq_change_max;
  152.  
  153.      } stat;
  154.  
  155. diff -uNrp a/common/frame.c b/common/frame.c
  156. --- a/common/frame.c    2011-08-25 10:24:33 +0800
  157. +++ b/common/frame.c    2011-08-25 11:30:58 +0800
  158. @@ -232,6 +232,7 @@ static x264_frame_t *x264_frame_new( x26
  159.          {
  160.              CHECKED_MALLOC( frame->f_qp_offset, h->mb.i_mb_count * sizeof(float) );
  161.              CHECKED_MALLOC( frame->f_qp_offset_aq, h->mb.i_mb_count * sizeof(float) );
  162. +            CHECKED_MALLOC( frame->i_aq_type, h->mb.i_mb_count * sizeof(uint8_t) );
  163.              if( h->frames.b_have_lowres )
  164.                  /* shouldn't really be initialized, just silences a valgrind false-positive in x264_mbtree_propagate_cost_sse2 */
  165.                  CHECKED_MALLOCZERO( frame->i_inv_qscale_factor, (h->mb.i_mb_count+3) * sizeof(uint16_t) );
  166. @@ -278,6 +279,7 @@ void x264_frame_delete( x264_frame_t *fr
  167.                  x264_free( frame->lowres_costs[j][i] );
  168.          x264_free( frame->f_qp_offset );
  169.          x264_free( frame->f_qp_offset_aq );
  170. +        x264_free( frame->i_aq_type );
  171.          x264_free( frame->i_inv_qscale_factor );
  172.          x264_free( frame->i_row_bits );
  173.          x264_free( frame->f_row_qp );
  174. diff -uNrp a/common/frame.h b/common/frame.h
  175. --- a/common/frame.h    2011-08-25 10:24:33 +0800
  176. +++ b/common/frame.h    2011-08-25 11:31:21 +0800
  177. @@ -125,6 +125,7 @@ typedef struct x264_frame
  178.      float   *f_row_qscale;
  179.      float   *f_qp_offset;
  180.      float   *f_qp_offset_aq;
  181. +    uint8_t *i_aq_type;        // OreAQ mode type
  182.      int     b_intra_calculated;
  183.      uint16_t *i_intra_cost;
  184.      uint16_t *i_propagate_cost;
  185. diff -uNrp a/common/pixel.c b/common/pixel.c
  186. --- a/common/pixel.c    2011-08-25 10:27:18 +0800
  187. +++ b/common/pixel.c    2011-08-25 11:32:40 +0800
  188. @@ -70,6 +70,23 @@ PIXEL_SAD_C( x264_pixel_sad_8x4,    8,
  189.  PIXEL_SAD_C( x264_pixel_sad_4x8,    4,  8 )
  190.  PIXEL_SAD_C( x264_pixel_sad_4x4,    4,  4 )
  191.  
  192. +static int x264_pixel_count_8x8( uint8_t *pix, int i_pix, uint32_t threshold )
  193. +{
  194. +    int sum = 0;
  195. +    for( int y = 0; y < 8; y++, pix += i_pix )
  196. +        for( int x = 0; x < 8; x++ )
  197. +            sum += pix[x] > (uint8_t)threshold;
  198. +    return sum;
  199. +}
  200. +
  201. +static int x264_pixel_count_16x16( uint8_t *pix, int i_pix, uint32_t threshold )
  202. +{
  203. +    int sum = 0;
  204. +    for( int y = 0; y < 16; y++, pix += i_pix )
  205. +        for( int x = 0; x < 16; x++ )
  206. +            sum += pix[x] > (uint8_t)threshold;
  207. +    return sum;
  208. +}
  209.  
  210.  /****************************************************************************
  211.   * pixel_ssd_WxH
  212. @@ -849,6 +866,8 @@ void x264_pixel_init( int cpu, x264_pixe
  213.      pixf->ssim_end4 = ssim_end4;
  214.      pixf->var2_8x8 = pixel_var2_8x8;
  215.      pixf->vsad = pixel_vsad;
  216. +    pixf->count_8x8   = x264_pixel_count_8x8;
  217. +    pixf->count_16x16 = x264_pixel_count_16x16;
  218.  
  219.      pixf->intra_sad_x3_4x4    = x264_intra_sad_x3_4x4;
  220.      pixf->intra_satd_x3_4x4   = x264_intra_satd_x3_4x4;
  221. diff -uNrp a/common/pixel.h b/common/pixel.h
  222. --- a/common/pixel.h    2011-08-25 10:27:18 +0800
  223. +++ b/common/pixel.h    2011-08-25 11:33:25 +0800
  224. @@ -109,6 +109,9 @@ typedef struct
  225.      int (*ads[7])( int enc_dc[4], uint16_t *sums, int delta,
  226.                     uint16_t *cost_mvx, int16_t *mvs, int width, int thresh );
  227.  
  228. +    int (*count_8x8)( uint8_t *pix, int i_pix, uint32_t threshold );
  229. +    int (*count_16x16)( uint8_t *pix, int i_pix, uint32_t threshold );
  230. +
  231.      /* calculate satd or sad of V, H, and DC modes.
  232.       * may be NULL, in which case just use pred+satd instead. */
  233.      void (*intra_mbcmp_x3_16x16)( pixel *fenc, pixel *fdec  , int res[3] );
  234. diff -uNrp a/encoder/analyse.c b/encoder/analyse.c
  235. --- a/encoder/analyse.c 2011-08-25 10:27:18 +0800
  236. +++ b/encoder/analyse.c 2011-08-25 11:34:07 +0800
  237. @@ -200,7 +200,10 @@ const float x264_log2_lut[128] =
  238.  /* Avoid an int/float conversion. */
  239.  const float x264_log2_lz_lut[32] =
  240.  {
  241. -    31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
  242. +    31.00024, 30.00024, 29.00024, 28.00024, 27.00024, 26.00024, 25.00024, 24.00024,
  243. +    23.00024, 22.00024, 21.00024, 20.00024, 19.00024, 18.00024, 17.00024, 16.00024,
  244. +    15.00024, 14.00024, 13.00024, 12.00024, 11.00024, 10.00024,  9.00024,  8.00024,
  245. +     7.00024,  6.00024,  5.00024,  4.00024,  3.00024,  2.00024,  1.00024,  0.00024
  246.  };
  247.  
  248.  // should the intra and inter lambdas be different?
  249. diff -uNrp a/encoder/encoder.c b/encoder/encoder.c
  250. --- a/encoder/encoder.c 2011-08-25 10:27:19 +0800
  251. +++ b/encoder/encoder.c 2011-08-25 11:41:18 +0800
  252. @@ -772,10 +772,41 @@ static int x264_validate_parameters( x26
  253.      if( !h->param.rc.b_mb_tree )
  254.          h->param.rc.f_fade_compensate = 0;
  255.      
  256. -    h->param.rc.i_aq_mode = x264_clip3( h->param.rc.i_aq_mode, 0, 2 );
  257. -    h->param.rc.f_aq_strength = x264_clip3f( h->param.rc.f_aq_strength, 0, 3 );
  258. -    if( h->param.rc.f_aq_strength == 0 )
  259. -        h->param.rc.i_aq_mode = 0;
  260. +    h->param.rc.i_aq_mode = x264_clip3( h->param.rc.i_aq_mode, 0, 3 );
  261. +    h->param.rc.f_aq_strength = x264_clip3f( h->param.rc.f_aq_strength, -3, 3 );
  262. +    for( int i = 0; i < 2; i++ )
  263. +        for( int j = 0; j < 4; j++ )
  264. +            h->param.rc.f_aq_strengths[i][j] = x264_clip3f( h->param.rc.f_aq_strengths[i][j], -3, 3 );
  265. +    if( h->param.rc.f_aq_strengths[0][0] == 0 && h->param.rc.f_aq_strengths[1][0] == 0 &&
  266. +        h->param.rc.f_aq_strengths[0][1] == 0 && h->param.rc.f_aq_strengths[1][1] == 0 &&
  267. +        h->param.rc.f_aq_strengths[0][2] == 0 && h->param.rc.f_aq_strengths[1][2] == 0 &&
  268. +        h->param.rc.f_aq_strengths[0][3] == 0 && h->param.rc.f_aq_strengths[1][3] == 0 )
  269. +    {
  270. +        if( h->param.rc.f_aq_strength == 0 )
  271. +            h->param.rc.i_aq_mode = 0;
  272. +        else
  273. +            for( int i = 0; i < 2; i++ )
  274. +                for( int j = 0; j < 4; j++ )
  275. +                    h->param.rc.f_aq_strengths[i][j] = h->param.rc.f_aq_strength;
  276. +    }
  277. +    if( h->param.rc.f_aq_sensitivity < 0 )
  278. +        h->param.rc.f_aq_sensitivity = 0;
  279. +    for( int i = 0; i < 2; i++ )
  280. +    {
  281. +        h->param.rc.f_aq_ifactor[i] = x264_clip3f( h->param.rc.f_aq_ifactor[i], -10, 10 );
  282. +        h->param.rc.f_aq_pfactor[i] = x264_clip3f( h->param.rc.f_aq_pfactor[i], -10, 10 );
  283. +        h->param.rc.f_aq_bfactor[i] = x264_clip3f( h->param.rc.f_aq_bfactor[i], -10, 10 );
  284. +    }
  285. +    h->param.rc.i_aq_boundary[0] = x264_clip3( h->param.rc.i_aq_boundary[0], 0, 255 );
  286. +    h->param.rc.i_aq_boundary[1] = x264_clip3( h->param.rc.i_aq_boundary[1], 0, 255 );
  287. +    h->param.rc.i_aq_boundary[2] = x264_clip3( h->param.rc.i_aq_boundary[2], 0, 255 );
  288. +    if( !h->param.rc.b_aq_boundary ||
  289. +        h->param.rc.i_aq_boundary[0] <= h->param.rc.i_aq_boundary[1] || h->param.rc.i_aq_boundary[1] <= h->param.rc.i_aq_boundary[2] )
  290. +    {
  291. +        h->param.rc.i_aq_boundary[0] = h->param.vui.b_fullrange ? 205 : 192;
  292. +        h->param.rc.i_aq_boundary[1] = h->param.vui.b_fullrange ? 56 : 64;
  293. +        h->param.rc.i_aq_boundary[2] = h->param.vui.b_fullrange ? 9 : 24;
  294. +    }
  295.  
  296.      if( h->param.i_log_level < X264_LOG_INFO && (!h->param.psz_log_file || h->param.i_log_file_level < X264_LOG_INFO) )
  297.      {
  298. @@ -3216,6 +3247,22 @@ static int x264_encoder_frame_end( x264_
  299.      }
  300.      psz_message[79] = '\0';
  301.  
  302. +    if( h->param.rc.i_aq_mode )
  303. +    {
  304. +        h->stat.i_aq_count_total += h->mb.i_mb_count;
  305. +        for( int i = 0; i < 5; i++ )
  306. +        {
  307. +            h->stat.i_aq_count[i] += h->stat.frame.i_aq_count[i];
  308. +            h->stat.i_aq_result[i][0] += h->stat.frame.i_aq_result[i][0];
  309. +            h->stat.i_aq_result[i][1] += h->stat.frame.i_aq_result[i][1];
  310. +            h->stat.i_aq_result[i][2] += h->stat.frame.i_aq_result[i][2];
  311. +        }
  312. +        for( int i = h->stat.frame.i_aq_change_min+QP_MAX_SPEC; i <= h->stat.frame.i_aq_change_max+QP_MAX_SPEC; i++ )
  313. +            h->stat.i_aq_change[i] += h->stat.frame.i_aq_change[i];
  314. +        h->stat.i_aq_change_min = X264_MIN(h->stat.i_aq_change_min, h->stat.frame.i_aq_change_min);
  315. +        h->stat.i_aq_change_max = X264_MAX(h->stat.i_aq_change_max, h->stat.frame.i_aq_change_max);
  316. +    }
  317. +
  318.      x264_log( h, X264_LOG_DEBUG,
  319.                    "frame=%4d QP=%.2f NAL=%d Slice:%c Poc:%-3d I:%-4d P:%-4d SKIP:%-4d size=%d bytes%s\n",
  320.                h->i_frame,
  321. @@ -3287,7 +3334,7 @@ void    x264_encoder_close  ( x264_t *h
  322.      int chroma_size = h->param.i_width * h->param.i_height >> (!CHROMA444 * 2);
  323.      int64_t i_yuv_size = luma_size + chroma_size * 2;
  324.      int64_t i_mb_count_size[2][7] = {{0}};
  325. -    char buf[200];
  326. +    char buf[1500];
  327.      int b_print_pcm = h->stat.i_mb_count[SLICE_TYPE_I][I_PCM]
  328.                     || h->stat.i_mb_count[SLICE_TYPE_P][I_PCM]
  329.                     || h->stat.i_mb_count[SLICE_TYPE_B][I_PCM];
  330. @@ -3555,6 +3602,37 @@ void    x264_encoder_close  ( x264_t *h
  331.                  x264_log( h, X264_LOG_INFO, "ref %c L%d:%s\n", "PB"[i_slice], i_list, buf );
  332.              }
  333.  
  334. +        if( h->stat.i_aq_count_total && !h->param.rc.b_mb_tree )
  335. +        {
  336. +#define GET_AQ_RESULT(x) \
  337. +            100.0 / h->stat.i_aq_count_total * h->stat.i_aq_count[x], \
  338. +            h->stat.i_aq_count[x] ? (100.0 / h->stat.i_aq_count[x] * h->stat.i_aq_result[x][0]) : 0, \
  339. +            h->stat.i_aq_count[x] ? (100.0 / h->stat.i_aq_count[x] * h->stat.i_aq_result[x][1]) : 0, \
  340. +            h->stat.i_aq_count[x] ? (100.0 / h->stat.i_aq_count[x] * h->stat.i_aq_result[x][2]) : 0
  341. +            char *p = buf;
  342. +
  343. +            x264_log( h, X264_LOG_INFO,
  344. +                "AQ Result  Bright MB:%5.2f%%  QP Up:%5.2f%% Down:%5.2f%% Even:%5.2f%%\n",
  345. +                GET_AQ_RESULT(1));
  346. +
  347. +            x264_log( h, X264_LOG_INFO,
  348. +                "AQ Result  Middle MB:%5.2f%%  QP Up:%5.2f%% Down:%5.2f%% Even:%5.2f%%\n",
  349. +                GET_AQ_RESULT(2));
  350. +
  351. +            x264_log( h, X264_LOG_INFO,
  352. +                "AQ Result    Dark MB:%5.2f%%  QP Up:%5.2f%% Down:%5.2f%% Even:%5.2f%%\n",
  353. +                GET_AQ_RESULT(3));
  354. +
  355. +            x264_log( h, X264_LOG_INFO,
  356. +                "AQ Result  M.Dark MB:%5.2f%%  QP Up:%5.2f%% Down:%5.2f%% Even:%5.2f%%\n",
  357. +                GET_AQ_RESULT(4));
  358. +
  359. +            for( int i = h->stat.i_aq_change_min; i <= h->stat.i_aq_change_max; i++ )
  360. +                p += sprintf( p, " %d:%.2f%%", -i, 100.0 / h->stat.i_aq_count_total * h->stat.i_aq_change[i + QP_MAX_SPEC] );
  361. +            x264_log( h, X264_LOG_INFO, "AQ change value %s\n", buf );
  362. +
  363. +        }
  364. +
  365.          if( h->param.analyse.b_ssim )
  366.          {
  367.              float ssim = SUM3( h->stat.f_ssim_mean_y ) / duration;
  368. diff -uNrp a/encoder/ratecontrol.c b/encoder/ratecontrol.c
  369. --- a/encoder/ratecontrol.c 2011-08-25 10:27:19 +0800
  370. +++ b/encoder/ratecontrol.c 2011-08-25 11:53:26 +0800
  371. @@ -149,6 +149,9 @@ struct x264_ratecontrol_t
  372.      int bframes;                /* # consecutive B-frames before this P-frame */
  373.      int bframe_bits;            /* total cost of those frames */
  374.  
  375. +    /* AQ stuff */
  376. +    float aq_threshold;
  377. +
  378.      int i_zones;
  379.      x264_zone_t *zones;
  380.      x264_zone_t *prev_zone;
  381. @@ -237,7 +240,7 @@ static ALWAYS_INLINE uint32_t ac_energy_
  382.  }
  383.  
  384.  // Find the total AC energy of the block in all planes.
  385. -static NOINLINE uint32_t x264_ac_energy_mb( x264_t *h, int mb_x, int mb_y, x264_frame_t *frame )
  386. +static NOINLINE uint32_t x264_ac_energy_mb( x264_t *h, int mb_x, int mb_y, x264_frame_t *frame, uint32_t *energy )
  387.  {
  388.      /* This function contains annoying hacks because GCC has a habit of reordering emms
  389.       * and putting it after floating point ops.  As a result, we put the emms at the end of the
  390. @@ -248,44 +251,223 @@ static NOINLINE uint32_t x264_ac_energy_
  391.      {
  392.          /* We don't know the super-MB mode we're going to pick yet, so
  393.           * simply try both and pick the lower of the two. */
  394. -        uint32_t var_interlaced, var_progressive;
  395. -        var_interlaced   = ac_energy_plane( h, mb_x, mb_y, frame, 0, 0, 1, 1 );
  396. -        var_progressive  = ac_energy_plane( h, mb_x, mb_y, frame, 0, 0, 0, 0 );
  397. +        uint32_t var_interlaced_y  = ac_energy_plane( h, mb_x, mb_y, frame, 0, 0, 1, 1 );
  398. +        uint32_t var_progressive_y = ac_energy_plane( h, mb_x, mb_y, frame, 0, 0, 0, 0 );
  399. +        uint32_t var_interlaced_uv;
  400. +        uint32_t var_progressive_uv;
  401.          if( CHROMA444 )
  402.          {
  403. -            var_interlaced  += ac_energy_plane( h, mb_x, mb_y, frame, 1, 0, 1, 1 );
  404. -            var_progressive += ac_energy_plane( h, mb_x, mb_y, frame, 1, 0, 0, 0 );
  405. -            var_interlaced  += ac_energy_plane( h, mb_x, mb_y, frame, 2, 0, 1, 1 );
  406. -            var_progressive += ac_energy_plane( h, mb_x, mb_y, frame, 2, 0, 0, 0 );
  407. +            var_interlaced_uv   = ac_energy_plane( h, mb_x, mb_y, frame, 1, 0, 1, 1 );
  408. +            var_progressive_uv  = ac_energy_plane( h, mb_x, mb_y, frame, 1, 0, 0, 0 );
  409. +            var_interlaced_uv  += ac_energy_plane( h, mb_x, mb_y, frame, 2, 0, 1, 1 );
  410. +            var_progressive_uv += ac_energy_plane( h, mb_x, mb_y, frame, 2, 0, 0, 0 );
  411. +        }
  412. +        else
  413. +        {
  414. +            var_interlaced_uv   = ac_energy_plane( h, mb_x, mb_y, frame, 1, 1, 1, 1 );
  415. +            var_progressive_uv  = ac_energy_plane( h, mb_x, mb_y, frame, 1, 1, 0, 0 );
  416. +        }
  417. +        uint32_t var_interlaced  = var_interlaced_y  + var_interlaced_uv;
  418. +        uint32_t var_progressive = var_progressive_y + var_progressive_uv;
  419. +        if( var_interlaced < var_progressive )
  420. +        {
  421. +            var = var_interlaced;
  422. +            energy[0] = var_interlaced_y;
  423. +            energy[1] = var_interlaced_uv;
  424.          }
  425.          else
  426.          {
  427. -            var_interlaced  += ac_energy_plane( h, mb_x, mb_y, frame, 1, 1, 1, 1 );
  428. -            var_progressive += ac_energy_plane( h, mb_x, mb_y, frame, 1, 1, 0, 0 );
  429. +            var = var_progressive;
  430. +            energy[0] = var_progressive_y;
  431. +            energy[1] = var_progressive_uv;
  432.          }
  433. -        var = X264_MIN( var_interlaced, var_progressive );
  434.      }
  435.      else
  436.      {
  437. -        var  = ac_energy_plane( h, mb_x, mb_y, frame, 0, 0, PARAM_INTERLACED, 1 );
  438. +
  439. +        energy[0] = ac_energy_plane( h, mb_x, mb_y, frame, 0, 0, PARAM_INTERLACED, 1 );
  440.          if( CHROMA444 )
  441.          {
  442. -            var += ac_energy_plane( h, mb_x, mb_y, frame, 1, 0, PARAM_INTERLACED, 1 );
  443. -            var += ac_energy_plane( h, mb_x, mb_y, frame, 2, 0, PARAM_INTERLACED, 1 );
  444. +            energy[1]  = ac_energy_plane( h, mb_x, mb_y, frame, 1, 0, PARAM_INTERLACED, 1 );
  445. +            energy[1] += ac_energy_plane( h, mb_x, mb_y, frame, 2, 0, PARAM_INTERLACED, 1 );
  446.          }
  447.          else
  448. -            var += ac_energy_plane( h, mb_x, mb_y, frame, 1, 1, PARAM_INTERLACED, 1 );
  449. +            energy[1]  = ac_energy_plane( h, mb_x, mb_y, frame, 1, 1, PARAM_INTERLACED, 1 );
  450. +        var = energy[0] + energy[1];
  451.      }
  452.      x264_emms();
  453.      return var;
  454.  }
  455.  
  456. +static NOINLINE void get_image_mb( x264_t *h, int mb_x, int mb_y, x264_frame_t *frame, int b_field, int *luma, int *bluePoint )
  457. +{
  458. +    ALIGNED_16( static uint8_t zero[17] ) = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
  459. +    if( CHROMA444 )
  460. +    {
  461. +        int stride[3];
  462. +        int offset[3];
  463. +        for( int i = 0; i < 3; i++ )
  464. +        {
  465. +            stride[i] = frame->i_stride[i];
  466. +            offset[i] = b_field
  467. +                ? 16 * (mb_x + (mb_y&~1) * stride[i]) + (mb_y&1) * stride[i]
  468. +                : 16 * (mb_x +  mb_y * stride[i]);
  469. +            stride[i] <<= b_field;
  470. +        }
  471. +        *luma = h->pixf.sad[PIXEL_16x16]( frame->plane[0] + offset[0], stride[0], zero, 0 ) >> 8;
  472. +        *bluePoint =
  473. +            (h->pixf.count_16x16( frame->plane[1] + offset[1], stride[1], 0x81818181 ) >= 40) &&
  474. +            (h->pixf.count_16x16( frame->plane[2] + offset[2], stride[2], 0x87878787 ) <= 24);
  475. +    }
  476. +    else
  477. +    {
  478. +        int stride[2];
  479. +        int offset[2];
  480. +        for( int i = 0; i < 2; i++ )
  481. +        {
  482. +            int w = i ? 8 : 16;
  483. +            stride[i] = frame->i_stride[i];
  484. +            offset[i] = b_field
  485. +                ? 16 * mb_x + w *(mb_y&~1) * stride[i] + (mb_y&1) * stride[i]
  486. +                : 16 * mb_x + w * mb_y * stride[i];
  487. +            stride[i] <<= b_field;
  488. +        }
  489. +        *luma = h->pixf.sad[PIXEL_16x16]( frame->plane[0] + offset[0], stride[0], zero, 0 ) >> 8;
  490. +        ALIGNED_ARRAY_16( pixel, pix,[FENC_STRIDE*8] );
  491. +        h->mc.load_deinterleave_8x8x2_fenc( pix, frame->plane[1] + offset[1], stride[1] );
  492. +        *bluePoint =
  493. +            (h->pixf.count_8x8( pix,               FENC_STRIDE, 0x81818181 ) >= 40) &&
  494. +            (h->pixf.count_8x8( pix+FENC_STRIDE/2, FENC_STRIDE, 0x87878787 ) <= 24);
  495. +    }
  496. +}
  497. +
  498. +static NOINLINE float x264_adjust_OreAQ( x264_t *h, int mb_x, int mb_y, x264_frame_t *frame, uint32_t *energy )
  499. +{
  500. +    uint8_t mode;
  501. +    int bluePoint = 0;
  502. +    int luma = 0;
  503. +    float energy_y, energy_uv, f_qp_adj;
  504. +    uint32_t _energy_y, _energy_uv;
  505. +
  506. +    get_image_mb( h, mb_x, mb_y, frame, PARAM_INTERLACED, &luma, &bluePoint );
  507. +    x264_emms();
  508. +
  509. +    _energy_y  = X264_MAX( energy[0], 1 );
  510. +    _energy_uv = X264_MAX( energy[1], 1 );
  511. +
  512. +    // logf(energy) = 1.0397 * x264_log2( energy ) / 1.5
  513. +    // energy_y  = 1.2 * (logf(_energy_y ) - ((h->rc->aq_threshold + 2*(BIT_DEPTH-8)) * .91) + 0.5);
  514. +    // energy_uv = 0.8 * (logf(_energy_uv) - ((h->rc->aq_threshold + 2*(BIT_DEPTH-8)) * .91) + 0.5);
  515. +    energy_y  = 0.83176f * x264_log2( _energy_y  ) - ((h->rc->aq_threshold + 2*(BIT_DEPTH-8)) * 1.092f) + 0.5f;
  516. +    energy_uv = 0.55451f * x264_log2( _energy_uv ) - ((h->rc->aq_threshold + 2*(BIT_DEPTH-8)) * 0.728f) + 0.5f;
  517. +    f_qp_adj = 0.f;
  518. +
  519. +    if( luma > h->param.rc.i_aq_boundary[0] )
  520. +    {
  521. +        // *** Bright ***
  522. +        // Y & UV Flat      -> qp up
  523. +        // Y Flat / UV Bump -> qp y up
  524. +        // Y Bump / UV Flat -> even
  525. +        // Y & UV Bump      -> qp down
  526. +        mode = 0x00;
  527. +
  528. +        // qp up
  529. +        if( !bluePoint && energy_y < 0 && energy_uv < 0 )
  530. +            f_qp_adj = X264_MIN( energy_y, energy_uv );
  531. +        // qp y up
  532. +        else if( !bluePoint && energy_y < 0 && energy_uv >= 0 )
  533. +            f_qp_adj = energy_y;
  534. +        // qp down
  535. +        else if( energy_y >= 0 && energy_uv >= 0 )
  536. +            f_qp_adj = X264_MAX( energy_y, energy_uv ) * 0.5f;
  537. +    }
  538. +    else if( luma > h->param.rc.i_aq_boundary[1] )
  539. +    {
  540. +        // *** Middle ***
  541. +        // Y & UV Flat      -> qp up
  542. +        // Y Flat / UV Bump -> even
  543. +        // Y Bump / UV Flat -> qp mix down
  544. +        // Y & UV Bump      -> qp down
  545. +        mode = 0x01;
  546. +
  547. +        // qp up
  548. +        if( !bluePoint && energy_y < 0 && energy_uv < 0 )
  549. +            f_qp_adj = X264_MAX( energy_y, energy_uv );
  550. +        // qp mix down
  551. +        else if( energy_y >= 0 && energy_uv < 0 )
  552. +            f_qp_adj = X264_MAX( energy_y + (!bluePoint * energy_uv), 0 ) * 0.5f;
  553. +        // qp down
  554. +        else if( energy_y >= 0 && energy_uv >= 0 )
  555. +            f_qp_adj = X264_MAX( energy_y, bluePoint * energy_uv );
  556. +    }
  557. +    else if( luma > h->param.rc.i_aq_boundary[2] )
  558. +    {
  559. +        // *** Dark ***
  560. +        // Y & UV Flat      -> qp up
  561. +        // Y Flat / UV Bump -> qp uv down
  562. +        // Y Bump / UV Flat -> qp y down
  563. +        // Y & UV Bump      -> qp down
  564. +        mode = 0x02;
  565. +
  566. +        // qp up
  567. +        if( energy_y < 0 && energy_uv < 0 )
  568. +            f_qp_adj = X264_MAX( energy_y, energy_uv ) * 0.5f;
  569. +        // qp uv down
  570. +        else if( energy_y < 0 && energy_uv >= 0 )
  571. +            f_qp_adj = energy_uv * 1.25f;
  572. +        // qp y down
  573. +        else if( energy_y >= 0 && energy_uv < 0 )
  574. +            f_qp_adj = energy_y * 1.25f;
  575. +        // qp down
  576. +        else if( energy_y >= 0 && energy_uv >= 0 )
  577. +            f_qp_adj = X264_MAX( energy_y, energy_uv ) * 1.25f;
  578. +    }
  579. +    else
  580. +    {
  581. +        // *** M.Dark ***
  582. +        // Y & UV Flat      -> qp double up
  583. +        // Y Flat / UV Bump -> qp y up
  584. +        // Y Bump / UV Flat -> qp uv up
  585. +        // Y & UV Bump      -> even
  586. +        mode = 0x03;
  587. +
  588. +        // qp double up
  589. +        if( energy_y < 0 && energy_uv < 0 )
  590. +            f_qp_adj = energy_y + energy_uv;
  591. +        // qp mix up
  592. +        else if( energy_y < 0 && energy_uv >= 0 )
  593. +            f_qp_adj = energy_y;
  594. +        // qp uv down
  595. +        else if( energy_y >= 0 && energy_uv < 0 )
  596. +            f_qp_adj = energy_uv * 0.5f;
  597. +    }
  598. +
  599. +    /* If f_qp_adj is positive, then lower the qp. */
  600. +    f_qp_adj *= h->param.rc.f_aq_strengths[f_qp_adj>0][mode];
  601. +
  602. +    frame->i_aq_type[mb_x + mb_y*h->mb.i_mb_stride] = mode + 0x01;
  603. +
  604. +    if( h->param.rc.i_aq_mode == X264_AQ_ORE )
  605. +    {
  606. +        /* If current MB is frame edge, lower the qp. */
  607. +        if( mb_x == 0 || mb_y == 0 || mb_x == h->sps->i_mb_width - 1 || mb_y == h->sps->i_mb_height - 1  )
  608. +            f_qp_adj += (float)( (energy_y<0) + (energy_uv<0) + 1 ) * ( h->param.rc.f_aq_strengths[1][3] + 0.5f );
  609. +    }
  610. +
  611. +    return -f_qp_adj;
  612. +}
  613. +
  614.  void x264_adaptive_quant_frame( x264_t *h, x264_frame_t *frame, float *quant_offsets )
  615.  {
  616.      /* constants chosen to result in approximately the same overall bitrate as without AQ.
  617.       * FIXME: while they're written in 5 significant digits, they're only tuned to 2. */
  618. -    float strength;
  619. +    float strength = 0.f;
  620.      float avg_adj = 0.f;
  621. +    uint32_t energy_yuv[2] = {0, 0};
  622. +    int no_aq = h->param.rc.f_aq_strengths[0][0] == 0 && h->param.rc.f_aq_strengths[1][0] == 0 &&
  623. +                h->param.rc.f_aq_strengths[0][1] == 0 && h->param.rc.f_aq_strengths[1][1] == 0 &&
  624. +                h->param.rc.f_aq_strengths[0][2] == 0 && h->param.rc.f_aq_strengths[1][2] == 0 &&
  625. +                h->param.rc.f_aq_strengths[0][3] == 0 && h->param.rc.f_aq_strengths[1][3] == 0;
  626.      /* Initialize frame stats */
  627.      for( int i = 0; i < 3; i++ )
  628.      {
  629. @@ -294,10 +476,10 @@ void x264_adaptive_quant_frame( x264_t *
  630.      }
  631.  
  632.      /* Degenerate cases */
  633. -    if( h->param.rc.i_aq_mode == X264_AQ_NONE || h->param.rc.f_aq_strength == 0 )
  634. +    if( h->param.rc.i_aq_mode == X264_AQ_NONE || no_aq )
  635.      {
  636.          /* Need to init it anyways for MB tree */
  637. -        if( h->param.rc.i_aq_mode && h->param.rc.f_aq_strength == 0 )
  638. +        if( h->param.rc.i_aq_mode && no_aq )
  639.          {
  640.              if( quant_offsets )
  641.              {
  642. @@ -315,13 +497,14 @@ void x264_adaptive_quant_frame( x264_t *
  643.                      for( int mb_xy = 0; mb_xy < h->mb.i_mb_count; mb_xy++ )
  644.                          frame->i_inv_qscale_factor[mb_xy] = 256;
  645.              }
  646. +            memset( frame->i_aq_type, 0, h->mb.i_mb_count * sizeof(uint8_t) );
  647.          }
  648.          /* Need variance data for weighted prediction */
  649.          if( h->param.analyse.i_weighted_pred )
  650.          {
  651.              for( int mb_y = 0; mb_y < h->mb.i_mb_height; mb_y++ )
  652.                  for( int mb_x = 0; mb_x < h->mb.i_mb_width; mb_x++ )
  653. -                    x264_ac_energy_mb( h, mb_x, mb_y, frame );
  654. +                    x264_ac_energy_mb( h, mb_x, mb_y, frame, energy_yuv );
  655.          }
  656.          else
  657.              return;
  658. @@ -329,41 +512,49 @@ void x264_adaptive_quant_frame( x264_t *
  659.      /* Actual adaptive quantization */
  660.      else
  661.      {
  662. -        if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE )
  663. +        h->rc->aq_threshold = logf( powf( h->param.rc.f_aq_sensitivity, 4 ) / 2.0 );
  664. +
  665. +        if( h->param.rc.i_aq_mode == X264_AQ_MIXORE )
  666.          {
  667.              float bit_depth_correction = powf(1 << (BIT_DEPTH-8), 0.5f);
  668.              float avg_adj_pow2 = 0.f;
  669.              for( int mb_y = 0; mb_y < h->mb.i_mb_height; mb_y++ )
  670.                  for( int mb_x = 0; mb_x < h->mb.i_mb_width; mb_x++ )
  671.                  {
  672. -                    uint32_t energy = x264_ac_energy_mb( h, mb_x, mb_y, frame );
  673. +                    uint32_t energy = x264_ac_energy_mb( h, mb_x, mb_y, frame, energy_yuv );
  674.                      float qp_adj = powf( energy + 1, 0.125f );
  675. -                    frame->f_qp_offset[mb_x + mb_y*h->mb.i_mb_stride] = qp_adj;
  676.                      avg_adj += qp_adj;
  677.                      avg_adj_pow2 += qp_adj * qp_adj;
  678. +                    frame->f_qp_offset[mb_x + mb_y*h->mb.i_mb_stride] = qp_adj;
  679. +                    frame->f_qp_offset_aq[mb_x + mb_y*h->mb.i_mb_stride] = x264_adjust_OreAQ( h, mb_x, mb_y, frame, energy_yuv );
  680.                  }
  681.              avg_adj /= h->mb.i_mb_count;
  682.              avg_adj_pow2 /= h->mb.i_mb_count;
  683. -            strength = h->param.rc.f_aq_strength * avg_adj / bit_depth_correction;
  684. +            strength = h->param.rc.f_aq_strengths[1][3] * avg_adj / bit_depth_correction;
  685.              avg_adj = avg_adj - 0.5f * (avg_adj_pow2 - (14.f * bit_depth_correction)) / avg_adj;
  686.          }
  687. -        else
  688. -            strength = h->param.rc.f_aq_strength * 1.0397f;
  689.  
  690.          for( int mb_y = 0; mb_y < h->mb.i_mb_height; mb_y++ )
  691.              for( int mb_x = 0; mb_x < h->mb.i_mb_width; mb_x++ )
  692.              {
  693.                  float qp_adj;
  694.                  int mb_xy = mb_x + mb_y*h->mb.i_mb_stride;
  695. -                if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE )
  696. +                if( h->param.rc.i_aq_mode == X264_AQ_MIXORE )
  697.                  {
  698. -                    qp_adj = frame->f_qp_offset[mb_xy];
  699. -                    qp_adj = strength * (qp_adj - avg_adj);
  700. +                    if( frame->f_qp_offset_aq[mb_xy] < -1 )
  701. +                    {
  702. +                        qp_adj = strength * (frame->f_qp_offset[mb_xy] - avg_adj);
  703. +                        qp_adj = X264_MIN( qp_adj, frame->f_qp_offset_aq[mb_xy] );
  704. +                    }
  705. +                    else if( frame->f_qp_offset_aq[mb_xy] >= 1 )
  706. +                        qp_adj = frame->f_qp_offset_aq[mb_xy];
  707. +                    else
  708. +                        qp_adj = strength * (frame->f_qp_offset[mb_xy] - avg_adj);
  709.                  }
  710.                  else
  711.                  {
  712. -                    uint32_t energy = x264_ac_energy_mb( h, mb_x, mb_y, frame );
  713. -                    qp_adj = strength * (x264_log2( X264_MAX(energy, 1) ) - (14.427f + 2*(BIT_DEPTH-8)));
  714. +                    x264_ac_energy_mb( h, mb_x, mb_y, frame, energy_yuv );
  715. +                    qp_adj = x264_adjust_OreAQ( h, mb_x, mb_y, frame, energy_yuv );
  716.                  }
  717.                  if( quant_offsets )
  718.                      qp_adj += quant_offsets[mb_xy];
  719. @@ -420,6 +611,8 @@ int x264_macroblock_tree_read( x264_t *h
  720.                  frame->i_inv_qscale_factor[i] = x264_exp2fix8(frame->f_qp_offset[i]);
  721.          }
  722.          rc->qpbuf_pos--;
  723. +
  724. +        memset( frame->i_aq_type, 0, h->mb.i_mb_count * sizeof(uint8_t) );
  725.      }
  726.      else
  727.          x264_stack_align( x264_adaptive_quant_frame, h, frame, quant_offsets );
  728. @@ -1524,17 +1717,32 @@ int x264_ratecontrol_qp( x264_t *h )
  729.  int x264_ratecontrol_mb_qp( x264_t *h )
  730.  {
  731.      x264_emms();
  732. -    float qp = h->rc->qpm;
  733. -    if( h->param.rc.i_aq_mode )
  734. -    {
  735. -         /* MB-tree currently doesn't adjust quantizers in unreferenced frames. */
  736. -        float qp_offset = h->fdec->b_kept_as_ref ? h->fenc->f_qp_offset[h->mb.i_mb_xy] : h->fenc->f_qp_offset_aq[h->mb.i_mb_xy];
  737. -        /* Scale AQ's effect towards zero in emergency mode. */
  738. -        if( qp > QP_MAX_SPEC )
  739. -            qp_offset *= (QP_MAX - qp) / (QP_MAX - QP_MAX_SPEC);
  740. -        qp += qp_offset;
  741. -    }
  742. -    return x264_clip3( qp + 0.5f, h->param.rc.i_qp_min, h->param.rc.i_qp_max );
  743. +    float f_qp = h->rc->qpm;
  744. +    if( !h->param.rc.i_aq_mode )
  745. +        return x264_clip3( f_qp + 0.5f, h->param.rc.i_qp_min, h->param.rc.i_qp_max );
  746. +
  747. +    /* MB-tree currently doesn't adjust quantizers in unreferenced frames. */
  748. +    float qp_offset = h->fdec->b_kept_as_ref ? h->fenc->f_qp_offset[h->mb.i_mb_xy] : h->fenc->f_qp_offset_aq[h->mb.i_mb_xy];
  749. +
  750. +    qp_offset *= h->sh.i_type == SLICE_TYPE_I ? h->param.rc.f_aq_ifactor[qp_offset<0]
  751. +               : h->sh.i_type == SLICE_TYPE_P ? h->param.rc.f_aq_pfactor[qp_offset<0]
  752. +               : h->sh.i_type == SLICE_TYPE_B ? h->param.rc.f_aq_bfactor[qp_offset<0]
  753. +               : 1.f;
  754. +
  755. +    /* Scale AQ's effect towards zero in emergency mode. */
  756. +    if( f_qp > QP_MAX_SPEC )
  757. +        qp_offset *= (QP_MAX - f_qp) / (QP_MAX - QP_MAX_SPEC);
  758. +
  759. +    int i_qp = (int)(f_qp + qp_offset + 0.5f);
  760. +    int i_qp_adj = x264_clip3( f_qp - i_qp + 0.5f, -QP_MAX_SPEC, QP_MAX_SPEC );
  761. +
  762. +    h->stat.frame.i_aq_change[i_qp_adj + QP_MAX_SPEC] += 1;
  763. +    h->stat.frame.i_aq_count[ h->fenc->i_aq_type[h->mb.i_mb_xy] ] += 1;
  764. +    h->stat.frame.i_aq_result[ h->fenc->i_aq_type[h->mb.i_mb_xy] ][ i_qp_adj < 0 ? 0 :(i_qp_adj > 0 ? 1 : 2) ] += 1;
  765. +    h->stat.frame.i_aq_change_min = X264_MIN(h->stat.frame.i_aq_change_min, i_qp_adj);
  766. +    h->stat.frame.i_aq_change_max = X264_MAX(h->stat.frame.i_aq_change_max, i_qp_adj);
  767. +
  768. +    return x264_clip3( i_qp, h->param.rc.i_qp_min, h->param.rc.i_qp_max );
  769.  }
  770.  
  771.  /* In 2pass, force the same frame types as in the 1st pass */
  772. diff -uNrp a/x264.c b/x264.c
  773. --- a/x264.c    2011-08-25 10:27:19 +0800
  774. +++ b/x264.c    2011-08-25 11:55:13 +0800
  775. @@ -690,10 +690,25 @@ static void help( x264_param_t *defaults
  776.      H2( "      --chroma-qp-offset <integer>  QP difference between chroma and luma [%d]\n", defaults->analyse.i_chroma_qp_offset );
  777.      H2( "      --aq-mode <integer>     AQ method [%d]\n"
  778.          "                                  - 0: Disabled\n"
  779. -        "                                  - 1: Variance AQ (complexity mask)\n"
  780. -        "                                  - 2: Auto-variance AQ (experimental)\n", defaults->rc.i_aq_mode );
  781. -    H1( "      --aq-strength <float>   Reduces blocking and blurring in flat and\n"
  782. -        "                              textured areas. [%.1f]\n", defaults->rc.f_aq_strength );
  783. +        "                                  - 1: OreAQ\n"
  784. +        "                                  - 2: MixOre (experimental)\n", defaults->rc.i_aq_mode );
  785. +    H1( "      --aq-strength <float>   Reduces blocking and blurring in bump and\n"
  786. +        "                              clear-cut areas. [%.1f]\n", defaults->rc.f_aq_strength );
  787. +    H2( "                    <Up:Down> or <Up1:Down1:Up2:Down2:Up3:Down3:Up4:OtherStuff>\n"
  788. +        "                              Set QP up/down strength.\n" );
  789. +    H1( "      --aq-sensitivity <float> \"Center\" of AQ curve. [%.1f]\n"
  790. +        "                                  -  5: most QPs are raised\n"
  791. +        "                                  - 10: good general-use sensitivity\n"
  792. +        "                                  - 15: most QPs are lowered\n", defaults->rc.f_aq_sensitivity );
  793. +    H2( "      --aq-ifactor <Up:Down>  AQ strength factor of I-frames [%.1f:%.1f]\n", defaults->rc.f_aq_ifactor[0], defaults->rc.f_aq_ifactor[1] );
  794. +    H2( "      --aq-pfactor <Up:Down>  AQ strength factor of P-frames [%.1f:%.1f]\n", defaults->rc.f_aq_pfactor[0], defaults->rc.f_aq_pfactor[1] );
  795. +    H2( "      --aq-bfactor <Up:Down>  AQ strength factor of B-frames [%.1f:%.1f]\n", defaults->rc.f_aq_bfactor[0], defaults->rc.f_aq_bfactor[1] );
  796. +    H2( "      --aq-boundary <int:int:int>   AQ boundary. \n"
  797. +        "                                  fullrange=off: [192:64:24]\n"
  798. +        "                                  fullrange=on : [205:56:9]\n"
  799. +        "                                  #1: Bright-Middle\n"
  800. +        "                                  #2: Middle-Dark\n"
  801. +        "                                  #3: Dark-M.Dark\n" );
  802.      H1( "      --fade-compensate <float> Allocate more bits to fades [%.1f]\n", defaults->rc.f_fade_compensate );
  803.      H2( "                                  Approximate sane range: 0.0 - 1.0\n" );
  804.      H1( "\n" );
  805. @@ -1062,7 +1077,12 @@ static struct option long_options[] =
  806.      { "no-fast-pskip",     no_argument, NULL, 0 },
  807.      { "no-dct-decimate",   no_argument, NULL, 0 },
  808.      { "aq-strength", required_argument, NULL, 0 },
  809. +    { "aq-sensitivity", required_argument, NULL, 0 },
  810. +    { "aq-ifactor", required_argument, NULL, 0 },
  811. +    { "aq-pfactor", required_argument, NULL, 0 },
  812. +    { "aq-bfactor", required_argument, NULL, 0 },
  813.      { "aq-mode",     required_argument, NULL, 0 },
  814. +    { "aq-boundary", required_argument, NULL, 0 },
  815.      { "fgo",         required_argument, NULL, 0 },
  816.      { "fade-compensate", required_argument, NULL, 0 },
  817.      { "deadzone-inter", required_argument, NULL, 0 },
  818. diff -uNrp a/x264.h b/x264.h
  819. --- a/x264.h    2011-08-25 10:27:19 +0800
  820. +++ b/x264.h    2011-08-25 11:56:42 +0800
  821. @@ -42,6 +42,8 @@
  822.  #include "x264_config.h"
  823.  
  824.  #define X264_BUILD 116
  825. +#define X264_SUB_BUILD 2 // OreAQ
  826. +#define X264_AQ_MODE_ORE05 0x00000004
  827.  
  828.  /* x264_t:
  829.   *      opaque handler for encoder */
  830. @@ -150,8 +152,8 @@ typedef struct
  831.  #define X264_RC_ABR                  2
  832.  #define X264_QP_AUTO                 0
  833.  #define X264_AQ_NONE                 0
  834. -#define X264_AQ_VARIANCE             1
  835. -#define X264_AQ_AUTOVARIANCE         2
  836. +#define X264_AQ_ORE                  1
  837. +#define X264_AQ_MIXORE               2
  838.  #define X264_B_ADAPT_NONE            0
  839.  #define X264_B_ADAPT_FAST            1
  840.  #define X264_B_ADAPT_TRELLIS         2
  841. @@ -388,6 +390,13 @@ typedef struct x264_param_t
  842.  
  843.          int         i_aq_mode;      /* psy adaptive QP. (X264_AQ_*) */
  844.          float       f_aq_strength;
  845. +        float       f_aq_strengths[2][4];   /* Up{ Bright, Middle, Dark, M.Dark }, Down{ Bright, Middle, Dark, Other stuff } */
  846. +        float       f_aq_sensitivity;
  847. +        float       f_aq_ifactor[2]; /* { Up, Down } */
  848. +        float       f_aq_pfactor[2]; /* { Up, Down } */
  849. +        float       f_aq_bfactor[2]; /* { Up, Down } */
  850. +        int         b_aq_boundary;
  851. +        int         i_aq_boundary[3];
  852.          float       f_fade_compensate; /* Give more bits to fades. */
  853.          int         b_mb_tree;      /* Macroblock-tree ratecontrol. */
  854.          int         i_lookahead;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement