Advertisement
06_taro

OreAQ-core-Taro-r2085

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