Advertisement
Guest User

Dark Shikari

a guest
Mar 31st, 2008
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.63 KB | None | 0 0
  1. diff --git a/common/common.c b/common/common.c
  2. index 44d9113..5b9b2c4 100644
  3. --- a/common/common.c
  4. +++ b/common/common.c
  5. @@ -123,6 +123,9 @@ void x264_param_default( x264_param_t *param )
  6. param->analyse.i_chroma_qp_offset = 0;
  7. param->analyse.b_fast_pskip = 1;
  8. param->analyse.b_dct_decimate = 1;
  9. + param->analyse.f_aq_strength = 1.0;
  10. + param->analyse.i_aq_mode = 2;
  11. + param->analyse.i_aq_metric = 3;
  12. param->analyse.i_luma_deadzone[0] = 21;
  13. param->analyse.i_luma_deadzone[1] = 11;
  14. param->analyse.b_psnr = 1;
  15. @@ -455,6 +458,12 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value )
  16. p->analyse.b_fast_pskip = atobool(value);
  17. OPT("dct-decimate")
  18. p->analyse.b_dct_decimate = atobool(value);
  19. + OPT("aq-strength")
  20. + p->analyse.f_aq_strength = atof(value);
  21. + OPT("aq-mode")
  22. + p->analyse.i_aq_mode = atoi(value);
  23. + OPT("aq-metric")
  24. + p->analyse.i_aq_metric = atoi(value);
  25. OPT("deadzone-inter")
  26. p->analyse.i_luma_deadzone[0] = atoi(value);
  27. OPT("deadzone-intra")
  28. @@ -883,6 +892,10 @@ char *x264_param2string( x264_param_t *p, int b_res )
  29. s += sprintf( s, " ip_ratio=%.2f", p->rc.f_ip_factor );
  30. if( p->i_bframe )
  31. s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
  32. + if( p->analyse.i_aq_mode )
  33. + s += sprintf( s, " aq=%d:%.1f", p->analyse.i_aq_mode, p->analyse.f_aq_strength );
  34. + else
  35. + s += sprintf( s, " aq=0" );
  36. if( p->rc.psz_zones )
  37. s += sprintf( s, " zones=%s", p->rc.psz_zones );
  38. else if( p->rc.i_zones )
  39. diff --git a/common/pixel.c b/common/pixel.c
  40. index 1d5567b..a86932a 100644
  41. --- a/common/pixel.c
  42. +++ b/common/pixel.c
  43. @@ -95,6 +95,39 @@ PIXEL_SSD_C( x264_pixel_ssd_8x4, 8, 4 )
  44. PIXEL_SSD_C( x264_pixel_ssd_4x8, 4, 8 )
  45. PIXEL_SSD_C( x264_pixel_ssd_4x4, 4, 4 )
  46.  
  47. +#define PIXEL_NSSD_C( name, lx, ly) \
  48. +static int name( uint8_t *pix1, int i_stride_pix1, \
  49. + uint8_t *pix2, int i_stride_pix2 ) \
  50. +{\
  51. + int score1=0;\
  52. + int score2=0;\
  53. + int x,y;\
  54. + for(y=0; y<ly; y++){\
  55. + for(x=0; x<lx; x++){\
  56. + score1 += abs(pix1[x] - pix2[x]);\
  57. + }\
  58. + if(y+1<ly){\
  59. + for(x=0; x<lx-1; x++){\
  60. + score2 += abs( pix1[x] - pix1[x+i_stride_pix1]\
  61. + - pix1[x+1] + pix1[x+1+i_stride_pix1])\
  62. + -abs( pix2[x] - pix2[x+i_stride_pix2]\
  63. + - pix2[x+1] + pix2[x+1+i_stride_pix2]);\
  64. + }\
  65. + }\
  66. + pix1 += i_stride_pix1;\
  67. + pix2 += i_stride_pix2;\
  68. + }\
  69. + return score1 + abs(score2)*8;\
  70. +}
  71. +
  72. +PIXEL_NSSD_C( x264_pixel_nssd_16x16, 16, 16 )
  73. +PIXEL_NSSD_C( x264_pixel_nssd_16x8, 16, 8 )
  74. +PIXEL_NSSD_C( x264_pixel_nssd_8x16, 8, 16 )
  75. +PIXEL_NSSD_C( x264_pixel_nssd_8x8, 8, 8 )
  76. +PIXEL_NSSD_C( x264_pixel_nssd_8x4, 8, 4 )
  77. +PIXEL_NSSD_C( x264_pixel_nssd_4x8, 4, 8 )
  78. +PIXEL_NSSD_C( x264_pixel_nssd_4x4, 4, 4 )
  79. +
  80. int64_t x264_pixel_ssd_wxh( x264_pixel_function_t *pf, uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2, int i_width, int i_height )
  81. {
  82. int64_t i_ssd = 0;
  83. @@ -530,6 +563,7 @@ void x264_pixel_init( int cpu, x264_pixel_function_t *pixf )
  84. INIT7( satd, );
  85. INIT7( satd_x3, );
  86. INIT7( satd_x4, );
  87. + INIT7( nssd, );
  88. INIT4( sa8d, );
  89. INIT_ADS( );
  90.  
  91. diff --git a/common/pixel.h b/common/pixel.h
  92. index d533620..5a5ddec 100644
  93. --- a/common/pixel.h
  94. +++ b/common/pixel.h
  95. @@ -74,6 +74,7 @@ typedef struct
  96. x264_pixel_cmp_t fpelcmp[7]; /* either satd or sad for fullpel motion search */
  97. x264_pixel_cmp_x3_t fpelcmp_x3[7];
  98. x264_pixel_cmp_x4_t fpelcmp_x4[7];
  99. + x264_pixel_cmp_t nssd[7];
  100.  
  101. void (*ssim_4x4x2_core)( const uint8_t *pix1, int stride1,
  102. const uint8_t *pix2, int stride2, int sums[2][4] );
  103. diff --git a/encoder/analyse.c b/encoder/analyse.c
  104. index 0264621..b89724d 100644
  105. --- a/encoder/analyse.c
  106. +++ b/encoder/analyse.c
  107. @@ -218,7 +218,7 @@ static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
  108. h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
  109. h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
  110. && h->mb.i_subpel_refine >= 5;
  111. - h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd;
  112. + h->mb.b_trellis = (h->param.analyse.i_trellis > 1 && a->b_mbrd);
  113. h->mb.b_transform_8x8 = 0;
  114. h->mb.b_noise_reduction = 0;
  115.  
  116. @@ -2064,8 +2064,13 @@ void x264_macroblock_analyse( x264_t *h )
  117. int i_cost = COST_MAX;
  118. int i;
  119.  
  120. - /* init analysis */
  121. - x264_mb_analyse_init( h, &analysis, x264_ratecontrol_qp( h ) );
  122. + h->mb.i_qp = x264_ratecontrol_qp( h );
  123. +
  124. + if( h->param.analyse.i_aq_mode )
  125. + x264_adaptive_quant( h );
  126. +
  127. + /* init analysis */
  128. + x264_mb_analyse_init( h, &analysis, h->mb.i_qp );
  129.  
  130. /*--------------------------- Do the analysis ---------------------------*/
  131. if( h->sh.i_type == SLICE_TYPE_I )
  132. diff --git a/encoder/encoder.c b/encoder/encoder.c
  133. index 3dadb02..73f289c 100644
  134. --- a/encoder/encoder.c
  135. +++ b/encoder/encoder.c
  136. @@ -401,6 +401,7 @@ static int x264_validate_parameters( x264_t *h )
  137. h->param.analyse.b_fast_pskip = 0;
  138. h->param.analyse.i_noise_reduction = 0;
  139. h->param.analyse.i_subpel_refine = x264_clip3( h->param.analyse.i_subpel_refine, 1, 6 );
  140. + h->param.analyse.i_aq_mode = 0;
  141. }
  142. if( h->param.rc.i_rc_method == X264_RC_CQP )
  143. {
  144. @@ -475,6 +476,17 @@ static int x264_validate_parameters( x264_t *h )
  145. if( !h->param.b_cabac )
  146. h->param.analyse.i_trellis = 0;
  147. h->param.analyse.i_trellis = x264_clip3( h->param.analyse.i_trellis, 0, 2 );
  148. + h->param.analyse.i_aq_mode = x264_clip3(h->param.analyse.i_aq_mode, 0, 2);
  149. + if(h->param.analyse.f_aq_strength <= 0) h->param.analyse.i_aq_mode = 0;
  150. + /* VAQ on mode 1 effectively replaces qcomp, so qcomp is raised towards 1 to compensate. */
  151. + if(h->param.analyse.i_aq_mode == 2)
  152. + h->param.rc.f_qcompress = x264_clip3f(h->param.rc.f_qcompress + h->param.analyse.f_aq_strength * 0.4 / 0.28, 0, 1);
  153. + h->param.analyse.i_aq_metric = x264_clip3(h->param.analyse.i_aq_metric, 0, 3);
  154. + if(h->param.analyse.i_aq_metric > 0)
  155. + {
  156. + x264_log( h, X264_LOG_WARNING, "AQ METRIC %d IS AN EXPERIMENTAL ADAPTIVE QUANTIZATION MODE.\n", h->param.analyse.i_aq_metric );
  157. + x264_log( h, X264_LOG_WARNING, "USE IT AT YOUR OWN RISK!\n" );
  158. + }
  159. h->param.analyse.i_noise_reduction = x264_clip3( h->param.analyse.i_noise_reduction, 0, 1<<16 );
  160.  
  161. {
  162. diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c
  163. index 0c8a6d7..ea9aafc 100644
  164. --- a/encoder/ratecontrol.c
  165. +++ b/encoder/ratecontrol.c
  166. @@ -127,6 +127,10 @@ struct x264_ratecontrol_t
  167. predictor_t *pred_b_from_p; /* predict B-frame size from P-frame satd */
  168. int bframes; /* # consecutive B-frames before this P-frame */
  169. int bframe_bits; /* total cost of those frames */
  170. +
  171. + /* AQ stuff */
  172. + float aq_threshold;
  173. + int *ac_energy;
  174.  
  175. int i_zones;
  176. x264_zone_t *zones;
  177. @@ -169,6 +173,247 @@ static inline double qscale2bits(ratecontrol_entry_t *rce, double qscale)
  178. + rce->misc_bits;
  179. }
  180.  
  181. +static const int window_weights[7][7] =
  182. +{{41,68,94,104,94,68,41},
  183. +{68,115,155,171,155,115,68},
  184. +{94,155,209,230,209,155,94},
  185. +{104,171,230,256,230,171,104},
  186. +{94,155,209,230,209,155,94},
  187. +{68,115,155,171,155,115,68},
  188. +{41,68,94,104,94,68,41}};
  189. +
  190. +static inline int windowed_variance( x264_t *h, uint8_t *plane, int stride, int window_x, int window_y, int blocksize, int mb_x, int mb_y, int step )
  191. +{
  192. + int x,y,locx,locy,n=0;
  193. + uint64_t total = 0;
  194. + int shiftx = (window_x - 1) / 2; int shifty = (window_y - 1) / 2;
  195. + int startx = shiftx; int starty = shifty;
  196. + int endx = blocksize - shiftx; int endy = blocksize - shifty;
  197. + plane -= (shiftx + shifty * stride);
  198. + if(mb_x == 0) startx += shiftx;
  199. + if(mb_y == 0) starty += shifty;
  200. + if(mb_x == h->sps->i_mb_width - 1) endx -= shiftx;
  201. + if(mb_y == h->sps->i_mb_height - 1) endy -= shifty;
  202. + plane += starty * stride;
  203. + for(locy = starty; locy < endy; locy+=step)
  204. + {
  205. + for(locx = startx; locx < endx; locx+=step)
  206. + {
  207. + int sum = 0;
  208. + for(y = 0; y < window_y; y++)
  209. + for(x = 0; x < window_x; x++)
  210. + sum += window_weights[y][x] * plane[x+y*stride+locx];
  211. + sum = (sum + 64) >> 7;
  212. + sum = (sum + (window_x*window_y+1)/2)/(window_x*window_y);
  213. + int ssd = 0;
  214. + for(y = 0; y < window_y; y++)
  215. + for(x = 0; x < window_x; x++)
  216. + {
  217. + int val = plane[x+y*stride+locx] - sum;
  218. + ssd += (window_weights[y][x] * val * val + 64) >> 7;
  219. + }
  220. + total += ssd;
  221. + n++;
  222. + }
  223. + plane += stride*step;
  224. + }
  225. + return (total * 256) / n;
  226. +}
  227. +
  228. +static inline int fast_windowed_variance( x264_t *h, uint8_t *plane, int stride, int blocksize, int mb_x, int mb_y, int step )
  229. +{
  230. + DECLARE_ALIGNED( static uint8_t, zero[8], 16 ) = {0,0,0,0,0,0,0,0};
  231. + int locx,locy,n=0;
  232. + uint64_t total = 0;
  233. + int shiftx = 4; int shifty = 4;
  234. + int startx = shiftx; int starty = shifty;
  235. + int endx = blocksize - shiftx; int endy = blocksize - shifty;
  236. + plane -= (shiftx + shifty * stride);
  237. + if(mb_x == 0) startx += shiftx;
  238. + if(mb_y == 0) starty += shifty;
  239. + if(mb_x == h->sps->i_mb_width - 1) endx -= shiftx;
  240. + if(mb_y == h->sps->i_mb_height - 1) endy -= shifty;
  241. + plane += starty * stride;
  242. + for(locy = starty; locy < endy; locy+=step)
  243. + {
  244. + for(locx = startx; locx < endx; locx+=step)
  245. + {
  246. + int sad = h->pixf.sad[PIXEL_8x8](plane+locx,stride,zero,0);
  247. + int ssd = h->pixf.ssd[PIXEL_8x8](plane+locx,stride,zero,0);
  248. + total += ssd - ((sad * sad) >> 6);
  249. + n++;
  250. + }
  251. + plane += stride*step;
  252. + }
  253. + return (total * 196) / n;
  254. +}
  255. +
  256. +// Find the total AC energy of the block in all planes.
  257. +static int aq_metric_3( x264_t *h, int mb_x, int mb_y, int *satd )
  258. +{
  259. + DECLARE_ALIGNED( static uint8_t, zero[16], 16 ) = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  260. + unsigned int var=0, i;
  261. + for( i=0; i<1; i++ )
  262. + {
  263. + int w = i ? 8 : 16;
  264. + int stride = h->fenc->i_stride[i];
  265. + int pix = i ? PIXEL_8x8 : PIXEL_16x16;
  266. + int offset = h->mb.b_interlaced
  267. + ? w * (mb_x + (mb_y&~1) * stride) + (mb_y&1) * stride
  268. + : w * (mb_x + mb_y * stride);
  269. + stride <<= h->mb.b_interlaced;
  270. + var += windowed_variance(h, h->fenc->plane[i]+offset, stride, 7, 7, w, mb_x, mb_y, 1);
  271. + if( var && satd )
  272. + *satd += h->pixf.satd[pix](zero, 0, h->fenc->plane[i]+offset, stride);
  273. + }
  274. + return (var+16) >> 5;
  275. +}
  276. +
  277. +static int aq_metric_2( x264_t *h, int mb_x, int mb_y, int *satd )
  278. +{
  279. + DECLARE_ALIGNED( static uint8_t, zero[16], 16 ) = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  280. + unsigned int var=0, i;
  281. + for( i=0; i<1; i++ )
  282. + {
  283. + int w = i ? 8 : 16;
  284. + int stride = h->fenc->i_stride[i];
  285. + int pix = i ? PIXEL_8x8 : PIXEL_16x16;
  286. + int offset = h->mb.b_interlaced
  287. + ? w * (mb_x + (mb_y&~1) * stride) + (mb_y&1) * stride
  288. + : w * (mb_x + mb_y * stride);
  289. + stride <<= h->mb.b_interlaced;
  290. + var += windowed_variance(h, h->fenc->plane[i]+offset, stride, 7, 7, w, mb_x, mb_y, 2);
  291. + if( var && satd )
  292. + *satd += h->pixf.satd[pix](zero, 0, h->fenc->plane[i]+offset, stride);
  293. + }
  294. + return (var+16) >> 5;
  295. +}
  296. +
  297. +static int aq_metric_1( x264_t *h, int mb_x, int mb_y, int *satd )
  298. +{
  299. + DECLARE_ALIGNED( static uint8_t, zero[16], 16 ) = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  300. + unsigned int var=0, i;
  301. + for( i=0; i<1; i++ )
  302. + {
  303. + int w = i ? 8 : 16;
  304. + int stride = h->fenc->i_stride[i];
  305. + int pix = i ? PIXEL_8x8 : PIXEL_16x16;
  306. + int offset = h->mb.b_interlaced
  307. + ? w * (mb_x + (mb_y&~1) * stride) + (mb_y&1) * stride
  308. + : w * (mb_x + mb_y * stride);
  309. + stride <<= h->mb.b_interlaced;
  310. + var += fast_windowed_variance(h, h->fenc->plane[i]+offset, stride, w, mb_x, mb_y, 2);
  311. + if( var && satd )
  312. + *satd += h->pixf.satd[pix](zero, 0, h->fenc->plane[i]+offset, stride);
  313. + }
  314. + return (var+16) >> 5;
  315. +}
  316. +
  317. +static int aq_metric_0( x264_t *h, int mb_x, int mb_y, int *satd )
  318. +{
  319. + DECLARE_ALIGNED( static uint8_t, flat[16], 16 ) = {128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128};
  320. + unsigned int var=0, sad, ssd, i;
  321. + for( i=0; i<3; i++ )
  322. + {
  323. + int w = i ? 8 : 16;
  324. + int stride = h->fenc->i_stride[i];
  325. + int offset = h->mb.b_interlaced
  326. + ? w * (mb_x + (mb_y&~1) * stride) + (mb_y&1) * stride
  327. + : w * (mb_x + mb_y * stride);
  328. + int pix = i ? PIXEL_8x8 : PIXEL_16x16;
  329. + stride <<= h->mb.b_interlaced;
  330. + sad = h->pixf.sad[pix](flat, 0, h->fenc->plane[i]+offset, stride);
  331. + ssd = h->pixf.ssd[pix](flat, 0, h->fenc->plane[i]+offset, stride);
  332. + var += ssd - (sad * sad >> (i?6:8));
  333. + // SATD to represent the block's overall complexity (bit cost) for intra encoding.
  334. + // exclude the DC coef, because nothing short of an actual intra prediction will estimate DC cost.
  335. + if( var && satd )
  336. + *satd += h->pixf.satd[pix](flat, 0, h->fenc->plane[i]+offset, stride) - sad/2;
  337. + }
  338. + return var;
  339. +}
  340. +
  341. +void x264_autosense_aq( x264_t *h )
  342. +{
  343. + double total = 0;
  344. + double n = 0;
  345. + int mb_x, mb_y;
  346. + /* FIXME: Some of the SATDs might be already calculated elsewhere (ratecontrol?). Can we reuse them? */
  347. + /* FIXME: Is chroma SATD necessary? */
  348. + for( mb_y=0; mb_y<h->sps->i_mb_height; mb_y++ )
  349. + for( mb_x=0; mb_x<h->sps->i_mb_width; mb_x++ )
  350. + {
  351. + int energy, satd=0;
  352. + if(h->param.analyse.i_aq_metric == 0)
  353. + energy = aq_metric_0( h, h->mb.i_mb_x, h->mb.i_mb_y, &satd );
  354. + else if(h->param.analyse.i_aq_metric == 1)
  355. + energy = aq_metric_1( h, h->mb.i_mb_x, h->mb.i_mb_y, &satd );
  356. + else if(h->param.analyse.i_aq_metric == 2)
  357. + energy = aq_metric_2( h, h->mb.i_mb_x, h->mb.i_mb_y, &satd );
  358. + else
  359. + energy = aq_metric_3( h, h->mb.i_mb_x, h->mb.i_mb_y, &satd );
  360. + h->rc->ac_energy[mb_x + mb_y * h->sps->i_mb_width] = energy;
  361. + /* Weight the energy value by the SATD value of the MB. This represents the fact that
  362. + the more complex blocks in a frame should be weighted more when calculating the optimal threshold.
  363. + This also helps diminish the negative effect of large numbers of simple blocks in a frame, such as in the case
  364. + of a letterboxed film. */
  365. + if( energy )
  366. + {
  367. + x264_cpu_restore(h->param.cpu);
  368. + total += logf(energy) * satd;
  369. + n += satd;
  370. + }
  371. + }
  372. + x264_cpu_restore(h->param.cpu);
  373. + /* Calculate and store the threshold. */
  374. + h->rc->aq_threshold = n ? total/n : 15;
  375. +}
  376. +
  377. +/*****************************************************************************
  378. +* x264_adaptive_quant:
  379. + * adjust macroblock QP based on variance (AC energy) of the MB.
  380. + * high variance = higher QP
  381. + * low variance = lower QP
  382. + * This generally increases SSIM and lowers PSNR.
  383. +*****************************************************************************/
  384. +void x264_adaptive_quant( x264_t *h )
  385. +{
  386. + int qp = h->mb.i_qp;
  387. + int energy;
  388. + if(h->param.analyse.i_aq_mode == 2)
  389. + {
  390. + if(h->param.analyse.i_aq_metric == 0)
  391. + energy = aq_metric_0( h, h->mb.i_mb_x, h->mb.i_mb_y, NULL );
  392. + //printf("%d ",energy);
  393. + else if(h->param.analyse.i_aq_metric == 1)
  394. + energy = aq_metric_1( h, h->mb.i_mb_x, h->mb.i_mb_y, NULL );
  395. + //printf("%d ",energy);
  396. + else if(h->param.analyse.i_aq_metric == 2)
  397. + energy = aq_metric_2( h, h->mb.i_mb_x, h->mb.i_mb_y, NULL );
  398. + //printf("%d ",energy);
  399. + else
  400. + energy = aq_metric_3( h, h->mb.i_mb_x, h->mb.i_mb_y, NULL );
  401. + //printf("%d\n",energy);
  402. + }
  403. + else
  404. + energy = h->rc->ac_energy[h->mb.i_mb_xy];
  405. + if(energy == 0)
  406. + h->mb.i_qp = h->mb.i_last_qp;
  407. + else
  408. + {
  409. + x264_cpu_restore(h->param.cpu);
  410. + float result = energy;
  411. + /* Adjust the QP based on the AC energy of the macroblock. */
  412. + float qp_adj = 1.5 * (logf(result) - h->rc->aq_threshold);
  413. + if(h->param.analyse.i_aq_mode == 1) qp_adj = x264_clip3f(qp_adj, -5, 5);
  414. + int new_qp = x264_clip3(qp + qp_adj * h->param.analyse.f_aq_strength + .5, h->param.rc.i_qp_min, h->param.rc.i_qp_max);
  415. + /* If the QP of this MB is within 1 of the previous MB, code the same QP as the previous MB,
  416. + * to lower the bit cost of the qp_delta. */
  417. + //if(abs(new_qp - h->mb.i_last_qp) == 1) new_qp = h->mb.i_last_qp;
  418. + h->mb.i_qp = new_qp;
  419. + }
  420. + h->mb.i_chroma_qp = i_chroma_qp_table[x264_clip3( h->mb.i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )];
  421. +}
  422.  
  423. int x264_ratecontrol_new( x264_t *h )
  424. {
  425. @@ -244,7 +489,7 @@ int x264_ratecontrol_new( x264_t *h )
  426. rc->rate_tolerance = 0.01;
  427. }
  428.  
  429. - h->mb.b_variable_qp = rc->b_vbv && !rc->b_2pass;
  430. + h->mb.b_variable_qp = (rc->b_vbv && !rc->b_2pass) || h->param.analyse.i_aq_mode;
  431.  
  432. if( rc->b_abr )
  433. {
  434. @@ -458,10 +703,13 @@ int x264_ratecontrol_new( x264_t *h )
  435. x264_free( p );
  436. }
  437.  
  438. - for( i=1; i<h->param.i_threads; i++ )
  439. + for( i=0; i<h->param.i_threads; i++ )
  440. {
  441. h->thread[i]->rc = rc+i;
  442. - rc[i] = rc[0];
  443. + if( i )
  444. + rc[i] = rc[0];
  445. + if( h->param.analyse.i_aq_mode == 1 )
  446. + rc[i].ac_energy = x264_malloc( h->mb.i_mb_count * sizeof(int) );
  447. }
  448.  
  449. return 0;
  450. @@ -623,6 +871,8 @@ void x264_ratecontrol_delete( x264_t *h )
  451. x264_free( rc->zones[i].param );
  452. x264_free( rc->zones );
  453. }
  454. + for( i=0; i<h->param.i_threads; i++ )
  455. + x264_free( rc[i].ac_energy );
  456. x264_free( rc );
  457. }
  458.  
  459. @@ -729,6 +979,12 @@ void x264_ratecontrol_start( x264_t *h, int i_force_qp )
  460.  
  461. if( h->sh.i_type != SLICE_TYPE_B )
  462. rc->last_non_b_pict_type = h->sh.i_type;
  463. +
  464. + /* Adaptive AQ thresholding algorithm. */
  465. + if( h->param.analyse.i_aq_mode == 2 )
  466. + h->rc->aq_threshold = logf(5000.0); /* Arbitrary value for "center" of AQ curve. */
  467. + else if( h->param.analyse.i_aq_mode == 1 )
  468. + x264_autosense_aq(h);
  469. }
  470.  
  471. double predict_row_size( x264_t *h, int y, int qp )
  472. diff --git a/encoder/ratecontrol.h b/encoder/ratecontrol.h
  473. index d4af2c0..e8b2ea1 100644
  474. --- a/encoder/ratecontrol.h
  475. +++ b/encoder/ratecontrol.h
  476. @@ -34,6 +34,7 @@ void x264_ratecontrol_mb( x264_t *, int bits );
  477. int x264_ratecontrol_qp( x264_t * );
  478. void x264_ratecontrol_end( x264_t *, int bits );
  479. void x264_ratecontrol_summary( x264_t * );
  480. +void x264_adaptive_quant ( x264_t * );
  481.  
  482. #endif
  483.  
  484. diff --git a/x264.c b/x264.c
  485. index f68755d..37618fe 100644
  486. --- a/x264.c
  487. +++ b/x264.c
  488. @@ -244,6 +244,19 @@ static void Help( x264_param_t *defaults, int b_longhelp )
  489. " - 2: enabled on all mode decisions\n", defaults->analyse.i_trellis );
  490. H0( " --no-fast-pskip Disables early SKIP detection on P-frames\n" );
  491. H0( " --no-dct-decimate Disables coefficient thresholding on P-frames\n" );
  492. + H0( " --aq-strength <float> Reduces blocking and blurring in flat and\n"
  493. + " textured areas. [%.1f]\n"
  494. + " - 0.2: weak AQ\n"
  495. + " - 1.0: very strong AQ\n", defaults->analyse.f_aq_strength );
  496. + H0( " --aq-mode <integer> How AQ distributes bits [%d]\n"
  497. + " - 0: Disabled\n"
  498. + " - 1: Avoid moving bits between frames\n"
  499. + " - 2: Move bits between frames\n", defaults->analyse.i_aq_mode );
  500. + H0( " --aq-metric <integer> The metric used for AQ [%d]\n"
  501. + " - 0: Whole-macroblock variance (fastest)\n"
  502. + " - 1: Partial overlapped block variance\n"
  503. + " - 2: Partial overlapped gaussian variance\n"
  504. + " - 3: Full overlapped gaussian variance (slowest)", defaults->analyse.i_aq_metric );
  505. H0( " --nr <integer> Noise reduction [%d]\n", defaults->analyse.i_noise_reduction );
  506. H1( "\n" );
  507. H1( " --deadzone-inter <int> Set the size of the inter luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[0] );
  508. @@ -407,6 +420,9 @@ static int Parse( int argc, char **argv,
  509. { "trellis", required_argument, NULL, 't' },
  510. { "no-fast-pskip", no_argument, NULL, 0 },
  511. { "no-dct-decimate", no_argument, NULL, 0 },
  512. + { "aq-strength", required_argument, NULL, 0 },
  513. + { "aq-mode", required_argument, NULL, 0 },
  514. + { "aq-metric", required_argument, NULL, 0 },
  515. { "deadzone-inter", required_argument, NULL, '0' },
  516. { "deadzone-intra", required_argument, NULL, '0' },
  517. { "level", required_argument, NULL, 0 },
  518. diff --git a/x264.h b/x264.h
  519. index 70c9eaf..63712d4 100644
  520. --- a/x264.h
  521. +++ b/x264.h
  522. @@ -232,6 +232,10 @@ typedef struct x264_param_t
  523. int i_trellis; /* trellis RD quantization */
  524. int b_fast_pskip; /* early SKIP detection on P-frames */
  525. int b_dct_decimate; /* transform coefficient thresholding on P-frames */
  526. + float f_aq_strength; /* psy adaptive QP */
  527. + int i_aq_mode; /* 0 = off, 1 = auto, 2 = static sensitivity */
  528. + int i_aq_metric; /* 0 = macroblock variance, 1 = partial overlapped 8x8 block variance */
  529. + /* 2 = partial overlapped 7x7 gaussian window variance, 3 = full overlapped 7x7 gaussian window variance. */
  530. int i_noise_reduction; /* adaptive pseudo-deadzone */
  531.  
  532. /* the deadzone size that will be used in luma quantization */
  533.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement