/media/libvpx/vp8/encoder/quantize.c

http://github.com/zpao/v8monkey · C · 734 lines · 579 code · 112 blank · 43 comment · 40 complexity · 38b045f868dd87f25dbb74cfd0f23ffd MD5 · raw file

  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <math.h>
  11. #include "vpx_mem/vpx_mem.h"
  12. #include "onyx_int.h"
  13. #include "quantize.h"
  14. #include "vp8/common/quant_common.h"
  15. #define EXACT_QUANT
  16. #ifdef EXACT_FASTQUANT
  17. void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
  18. {
  19. int i, rc, eob;
  20. int zbin;
  21. int x, y, z, sz;
  22. short *coeff_ptr = b->coeff;
  23. short *zbin_ptr = b->zbin;
  24. short *round_ptr = b->round;
  25. short *quant_ptr = b->quant_fast;
  26. unsigned char *quant_shift_ptr = b->quant_shift;
  27. short *qcoeff_ptr = d->qcoeff;
  28. short *dqcoeff_ptr = d->dqcoeff;
  29. short *dequant_ptr = d->dequant;
  30. vpx_memset(qcoeff_ptr, 0, 32);
  31. vpx_memset(dqcoeff_ptr, 0, 32);
  32. eob = -1;
  33. for (i = 0; i < 16; i++)
  34. {
  35. rc = vp8_default_zig_zag1d[i];
  36. z = coeff_ptr[rc];
  37. zbin = zbin_ptr[rc] ;
  38. sz = (z >> 31); // sign of z
  39. x = (z ^ sz) - sz; // x = abs(z)
  40. if (x >= zbin)
  41. {
  42. x += round_ptr[rc];
  43. y = (((x * quant_ptr[rc]) >> 16) + x)
  44. >> quant_shift_ptr[rc]; // quantize (x)
  45. x = (y ^ sz) - sz; // get the sign back
  46. qcoeff_ptr[rc] = x; // write to destination
  47. dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value
  48. if (y)
  49. {
  50. eob = i; // last nonzero coeffs
  51. }
  52. }
  53. }
  54. d->eob = eob + 1;
  55. }
  56. #else
  57. void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
  58. {
  59. int i, rc, eob;
  60. int x, y, z, sz;
  61. short *coeff_ptr = b->coeff;
  62. short *round_ptr = b->round;
  63. short *quant_ptr = b->quant_fast;
  64. short *qcoeff_ptr = d->qcoeff;
  65. short *dqcoeff_ptr = d->dqcoeff;
  66. short *dequant_ptr = d->dequant;
  67. eob = -1;
  68. for (i = 0; i < 16; i++)
  69. {
  70. rc = vp8_default_zig_zag1d[i];
  71. z = coeff_ptr[rc];
  72. sz = (z >> 31); // sign of z
  73. x = (z ^ sz) - sz; // x = abs(z)
  74. y = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; // quantize (x)
  75. x = (y ^ sz) - sz; // get the sign back
  76. qcoeff_ptr[rc] = x; // write to destination
  77. dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value
  78. if (y)
  79. {
  80. eob = i; // last nonzero coeffs
  81. }
  82. }
  83. d->eob = eob + 1;
  84. }
  85. #endif
  86. #ifdef EXACT_QUANT
  87. void vp8_regular_quantize_b(BLOCK *b, BLOCKD *d)
  88. {
  89. int i, rc, eob;
  90. int zbin;
  91. int x, y, z, sz;
  92. short *zbin_boost_ptr = b->zrun_zbin_boost;
  93. short *coeff_ptr = b->coeff;
  94. short *zbin_ptr = b->zbin;
  95. short *round_ptr = b->round;
  96. short *quant_ptr = b->quant;
  97. unsigned char *quant_shift_ptr = b->quant_shift;
  98. short *qcoeff_ptr = d->qcoeff;
  99. short *dqcoeff_ptr = d->dqcoeff;
  100. short *dequant_ptr = d->dequant;
  101. short zbin_oq_value = b->zbin_extra;
  102. vpx_memset(qcoeff_ptr, 0, 32);
  103. vpx_memset(dqcoeff_ptr, 0, 32);
  104. eob = -1;
  105. for (i = 0; i < 16; i++)
  106. {
  107. rc = vp8_default_zig_zag1d[i];
  108. z = coeff_ptr[rc];
  109. zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
  110. zbin_boost_ptr ++;
  111. sz = (z >> 31); // sign of z
  112. x = (z ^ sz) - sz; // x = abs(z)
  113. if (x >= zbin)
  114. {
  115. x += round_ptr[rc];
  116. y = (((x * quant_ptr[rc]) >> 16) + x)
  117. >> quant_shift_ptr[rc]; // quantize (x)
  118. x = (y ^ sz) - sz; // get the sign back
  119. qcoeff_ptr[rc] = x; // write to destination
  120. dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value
  121. if (y)
  122. {
  123. eob = i; // last nonzero coeffs
  124. zbin_boost_ptr = b->zrun_zbin_boost; // reset zero runlength
  125. }
  126. }
  127. }
  128. d->eob = eob + 1;
  129. }
  130. /* Perform regular quantization, with unbiased rounding and no zero bin. */
  131. void vp8_strict_quantize_b(BLOCK *b, BLOCKD *d)
  132. {
  133. int i;
  134. int rc;
  135. int eob;
  136. int x;
  137. int y;
  138. int z;
  139. int sz;
  140. short *coeff_ptr;
  141. short *quant_ptr;
  142. unsigned char *quant_shift_ptr;
  143. short *qcoeff_ptr;
  144. short *dqcoeff_ptr;
  145. short *dequant_ptr;
  146. coeff_ptr = b->coeff;
  147. quant_ptr = b->quant;
  148. quant_shift_ptr = b->quant_shift;
  149. qcoeff_ptr = d->qcoeff;
  150. dqcoeff_ptr = d->dqcoeff;
  151. dequant_ptr = d->dequant;
  152. eob = - 1;
  153. vpx_memset(qcoeff_ptr, 0, 32);
  154. vpx_memset(dqcoeff_ptr, 0, 32);
  155. for (i = 0; i < 16; i++)
  156. {
  157. int dq;
  158. int round;
  159. /*TODO: These arrays should be stored in zig-zag order.*/
  160. rc = vp8_default_zig_zag1d[i];
  161. z = coeff_ptr[rc];
  162. dq = dequant_ptr[rc];
  163. round = dq >> 1;
  164. /* Sign of z. */
  165. sz = -(z < 0);
  166. x = (z + sz) ^ sz;
  167. x += round;
  168. if (x >= dq)
  169. {
  170. /* Quantize x. */
  171. y = (((x * quant_ptr[rc]) >> 16) + x) >> quant_shift_ptr[rc];
  172. /* Put the sign back. */
  173. x = (y + sz) ^ sz;
  174. /* Save the coefficient and its dequantized value. */
  175. qcoeff_ptr[rc] = x;
  176. dqcoeff_ptr[rc] = x * dq;
  177. /* Remember the last non-zero coefficient. */
  178. if (y)
  179. eob = i;
  180. }
  181. }
  182. d->eob = eob + 1;
  183. }
  184. #else
  185. void vp8_regular_quantize_b(BLOCK *b, BLOCKD *d)
  186. {
  187. int i, rc, eob;
  188. int zbin;
  189. int x, y, z, sz;
  190. short *zbin_boost_ptr = b->zrun_zbin_boost;
  191. short *coeff_ptr = b->coeff;
  192. short *zbin_ptr = b->zbin;
  193. short *round_ptr = b->round;
  194. short *quant_ptr = b->quant;
  195. short *qcoeff_ptr = d->qcoeff;
  196. short *dqcoeff_ptr = d->dqcoeff;
  197. short *dequant_ptr = d->dequant;
  198. short zbin_oq_value = b->zbin_extra;
  199. vpx_memset(qcoeff_ptr, 0, 32);
  200. vpx_memset(dqcoeff_ptr, 0, 32);
  201. eob = -1;
  202. for (i = 0; i < 16; i++)
  203. {
  204. rc = vp8_default_zig_zag1d[i];
  205. z = coeff_ptr[rc];
  206. //if ( i == 0 )
  207. // zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value/2;
  208. //else
  209. zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
  210. zbin_boost_ptr ++;
  211. sz = (z >> 31); // sign of z
  212. x = (z ^ sz) - sz; // x = abs(z)
  213. if (x >= zbin)
  214. {
  215. y = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; // quantize (x)
  216. x = (y ^ sz) - sz; // get the sign back
  217. qcoeff_ptr[rc] = x; // write to destination
  218. dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value
  219. if (y)
  220. {
  221. eob = i; // last nonzero coeffs
  222. zbin_boost_ptr = &b->zrun_zbin_boost[0]; // reset zero runlength
  223. }
  224. }
  225. }
  226. d->eob = eob + 1;
  227. }
  228. #endif
  229. void vp8_quantize_mby_c(MACROBLOCK *x)
  230. {
  231. int i;
  232. int has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
  233. && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
  234. for (i = 0; i < 16; i++)
  235. x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
  236. if(has_2nd_order)
  237. x->quantize_b(&x->block[24], &x->e_mbd.block[24]);
  238. }
  239. void vp8_quantize_mb_c(MACROBLOCK *x)
  240. {
  241. int i;
  242. int has_2nd_order=(x->e_mbd.mode_info_context->mbmi.mode != B_PRED
  243. && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
  244. for (i = 0; i < 24+has_2nd_order; i++)
  245. x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
  246. }
  247. void vp8_quantize_mbuv_c(MACROBLOCK *x)
  248. {
  249. int i;
  250. for (i = 16; i < 24; i++)
  251. x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
  252. }
  253. /* quantize_b_pair function pointer in MACROBLOCK structure is set to one of
  254. * these two C functions if corresponding optimized routine is not available.
  255. * NEON optimized version implements currently the fast quantization for pair
  256. * of blocks. */
  257. void vp8_regular_quantize_b_pair(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2)
  258. {
  259. vp8_regular_quantize_b(b1, d1);
  260. vp8_regular_quantize_b(b2, d2);
  261. }
  262. void vp8_fast_quantize_b_pair_c(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2)
  263. {
  264. vp8_fast_quantize_b_c(b1, d1);
  265. vp8_fast_quantize_b_c(b2, d2);
  266. }
  267. static const int qrounding_factors[129] =
  268. {
  269. 48, 48, 48, 48, 48, 48, 48, 48,
  270. 48, 48, 48, 48, 48, 48, 48, 48,
  271. 48, 48, 48, 48, 48, 48, 48, 48,
  272. 48, 48, 48, 48, 48, 48, 48, 48,
  273. 48, 48, 48, 48, 48, 48, 48, 48,
  274. 48, 48, 48, 48, 48, 48, 48, 48,
  275. 48, 48, 48, 48, 48, 48, 48, 48,
  276. 48, 48, 48, 48, 48, 48, 48, 48,
  277. 48, 48, 48, 48, 48, 48, 48, 48,
  278. 48, 48, 48, 48, 48, 48, 48, 48,
  279. 48, 48, 48, 48, 48, 48, 48, 48,
  280. 48, 48, 48, 48, 48, 48, 48, 48,
  281. 48, 48, 48, 48, 48, 48, 48, 48,
  282. 48, 48, 48, 48, 48, 48, 48, 48,
  283. 48, 48, 48, 48, 48, 48, 48, 48,
  284. 48, 48, 48, 48, 48, 48, 48, 48,
  285. 48
  286. };
  287. static const int qzbin_factors[129] =
  288. {
  289. 84, 84, 84, 84, 84, 84, 84, 84,
  290. 84, 84, 84, 84, 84, 84, 84, 84,
  291. 84, 84, 84, 84, 84, 84, 84, 84,
  292. 84, 84, 84, 84, 84, 84, 84, 84,
  293. 84, 84, 84, 84, 84, 84, 84, 84,
  294. 84, 84, 84, 84, 84, 84, 84, 84,
  295. 80, 80, 80, 80, 80, 80, 80, 80,
  296. 80, 80, 80, 80, 80, 80, 80, 80,
  297. 80, 80, 80, 80, 80, 80, 80, 80,
  298. 80, 80, 80, 80, 80, 80, 80, 80,
  299. 80, 80, 80, 80, 80, 80, 80, 80,
  300. 80, 80, 80, 80, 80, 80, 80, 80,
  301. 80, 80, 80, 80, 80, 80, 80, 80,
  302. 80, 80, 80, 80, 80, 80, 80, 80,
  303. 80, 80, 80, 80, 80, 80, 80, 80,
  304. 80, 80, 80, 80, 80, 80, 80, 80,
  305. 80
  306. };
  307. static const int qrounding_factors_y2[129] =
  308. {
  309. 48, 48, 48, 48, 48, 48, 48, 48,
  310. 48, 48, 48, 48, 48, 48, 48, 48,
  311. 48, 48, 48, 48, 48, 48, 48, 48,
  312. 48, 48, 48, 48, 48, 48, 48, 48,
  313. 48, 48, 48, 48, 48, 48, 48, 48,
  314. 48, 48, 48, 48, 48, 48, 48, 48,
  315. 48, 48, 48, 48, 48, 48, 48, 48,
  316. 48, 48, 48, 48, 48, 48, 48, 48,
  317. 48, 48, 48, 48, 48, 48, 48, 48,
  318. 48, 48, 48, 48, 48, 48, 48, 48,
  319. 48, 48, 48, 48, 48, 48, 48, 48,
  320. 48, 48, 48, 48, 48, 48, 48, 48,
  321. 48, 48, 48, 48, 48, 48, 48, 48,
  322. 48, 48, 48, 48, 48, 48, 48, 48,
  323. 48, 48, 48, 48, 48, 48, 48, 48,
  324. 48, 48, 48, 48, 48, 48, 48, 48,
  325. 48
  326. };
  327. static const int qzbin_factors_y2[129] =
  328. {
  329. 84, 84, 84, 84, 84, 84, 84, 84,
  330. 84, 84, 84, 84, 84, 84, 84, 84,
  331. 84, 84, 84, 84, 84, 84, 84, 84,
  332. 84, 84, 84, 84, 84, 84, 84, 84,
  333. 84, 84, 84, 84, 84, 84, 84, 84,
  334. 84, 84, 84, 84, 84, 84, 84, 84,
  335. 80, 80, 80, 80, 80, 80, 80, 80,
  336. 80, 80, 80, 80, 80, 80, 80, 80,
  337. 80, 80, 80, 80, 80, 80, 80, 80,
  338. 80, 80, 80, 80, 80, 80, 80, 80,
  339. 80, 80, 80, 80, 80, 80, 80, 80,
  340. 80, 80, 80, 80, 80, 80, 80, 80,
  341. 80, 80, 80, 80, 80, 80, 80, 80,
  342. 80, 80, 80, 80, 80, 80, 80, 80,
  343. 80, 80, 80, 80, 80, 80, 80, 80,
  344. 80, 80, 80, 80, 80, 80, 80, 80,
  345. 80
  346. };
  347. #define EXACT_QUANT
  348. #ifdef EXACT_QUANT
  349. static void invert_quant(int improved_quant, short *quant,
  350. unsigned char *shift, short d)
  351. {
  352. if(improved_quant)
  353. {
  354. unsigned t;
  355. int l;
  356. t = d;
  357. for(l = 0; t > 1; l++)
  358. t>>=1;
  359. t = 1 + (1<<(16+l))/d;
  360. *quant = (short)(t - (1<<16));
  361. *shift = l;
  362. }
  363. else
  364. {
  365. *quant = (1 << 16) / d;
  366. *shift = 0;
  367. }
  368. }
  369. void vp8cx_init_quantizer(VP8_COMP *cpi)
  370. {
  371. int i;
  372. int quant_val;
  373. int Q;
  374. int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44, 44, 44};
  375. for (Q = 0; Q < QINDEX_RANGE; Q++)
  376. {
  377. // dc values
  378. quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
  379. cpi->Y1quant_fast[Q][0] = (1 << 16) / quant_val;
  380. invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 0,
  381. cpi->Y1quant_shift[Q] + 0, quant_val);
  382. cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
  383. cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
  384. cpi->common.Y1dequant[Q][0] = quant_val;
  385. cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;
  386. quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
  387. cpi->Y2quant_fast[Q][0] = (1 << 16) / quant_val;
  388. invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 0,
  389. cpi->Y2quant_shift[Q] + 0, quant_val);
  390. cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
  391. cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
  392. cpi->common.Y2dequant[Q][0] = quant_val;
  393. cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;
  394. quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
  395. cpi->UVquant_fast[Q][0] = (1 << 16) / quant_val;
  396. invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 0,
  397. cpi->UVquant_shift[Q] + 0, quant_val);
  398. cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
  399. cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
  400. cpi->common.UVdequant[Q][0] = quant_val;
  401. cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;
  402. // all the ac values = ;
  403. for (i = 1; i < 16; i++)
  404. {
  405. int rc = vp8_default_zig_zag1d[i];
  406. quant_val = vp8_ac_yquant(Q);
  407. cpi->Y1quant_fast[Q][rc] = (1 << 16) / quant_val;
  408. invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + rc,
  409. cpi->Y1quant_shift[Q] + rc, quant_val);
  410. cpi->Y1zbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
  411. cpi->Y1round[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
  412. cpi->common.Y1dequant[Q][rc] = quant_val;
  413. cpi->zrun_zbin_boost_y1[Q][i] = (quant_val * zbin_boost[i]) >> 7;
  414. quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
  415. cpi->Y2quant_fast[Q][rc] = (1 << 16) / quant_val;
  416. invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + rc,
  417. cpi->Y2quant_shift[Q] + rc, quant_val);
  418. cpi->Y2zbin[Q][rc] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
  419. cpi->Y2round[Q][rc] = (qrounding_factors_y2[Q] * quant_val) >> 7;
  420. cpi->common.Y2dequant[Q][rc] = quant_val;
  421. cpi->zrun_zbin_boost_y2[Q][i] = (quant_val * zbin_boost[i]) >> 7;
  422. quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
  423. cpi->UVquant_fast[Q][rc] = (1 << 16) / quant_val;
  424. invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + rc,
  425. cpi->UVquant_shift[Q] + rc, quant_val);
  426. cpi->UVzbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
  427. cpi->UVround[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
  428. cpi->common.UVdequant[Q][rc] = quant_val;
  429. cpi->zrun_zbin_boost_uv[Q][i] = (quant_val * zbin_boost[i]) >> 7;
  430. }
  431. }
  432. }
  433. #else
  434. void vp8cx_init_quantizer(VP8_COMP *cpi)
  435. {
  436. int i;
  437. int quant_val;
  438. int Q;
  439. int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44, 44, 44};
  440. for (Q = 0; Q < QINDEX_RANGE; Q++)
  441. {
  442. // dc values
  443. quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
  444. cpi->Y1quant[Q][0] = (1 << 16) / quant_val;
  445. cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
  446. cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
  447. cpi->common.Y1dequant[Q][0] = quant_val;
  448. cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;
  449. quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
  450. cpi->Y2quant[Q][0] = (1 << 16) / quant_val;
  451. cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
  452. cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
  453. cpi->common.Y2dequant[Q][0] = quant_val;
  454. cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;
  455. quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
  456. cpi->UVquant[Q][0] = (1 << 16) / quant_val;
  457. cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
  458. cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
  459. cpi->common.UVdequant[Q][0] = quant_val;
  460. cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;
  461. // all the ac values = ;
  462. for (i = 1; i < 16; i++)
  463. {
  464. int rc = vp8_default_zig_zag1d[i];
  465. quant_val = vp8_ac_yquant(Q);
  466. cpi->Y1quant[Q][rc] = (1 << 16) / quant_val;
  467. cpi->Y1zbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
  468. cpi->Y1round[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
  469. cpi->common.Y1dequant[Q][rc] = quant_val;
  470. cpi->zrun_zbin_boost_y1[Q][i] = (quant_val * zbin_boost[i]) >> 7;
  471. quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
  472. cpi->Y2quant[Q][rc] = (1 << 16) / quant_val;
  473. cpi->Y2zbin[Q][rc] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
  474. cpi->Y2round[Q][rc] = (qrounding_factors_y2[Q] * quant_val) >> 7;
  475. cpi->common.Y2dequant[Q][rc] = quant_val;
  476. cpi->zrun_zbin_boost_y2[Q][i] = (quant_val * zbin_boost[i]) >> 7;
  477. quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
  478. cpi->UVquant[Q][rc] = (1 << 16) / quant_val;
  479. cpi->UVzbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
  480. cpi->UVround[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
  481. cpi->common.UVdequant[Q][rc] = quant_val;
  482. cpi->zrun_zbin_boost_uv[Q][i] = (quant_val * zbin_boost[i]) >> 7;
  483. }
  484. }
  485. }
  486. #endif
  487. void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x)
  488. {
  489. int i;
  490. int QIndex;
  491. MACROBLOCKD *xd = &x->e_mbd;
  492. int zbin_extra;
  493. // Select the baseline MB Q index.
  494. if (xd->segmentation_enabled)
  495. {
  496. // Abs Value
  497. if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA)
  498. QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
  499. // Delta Value
  500. else
  501. {
  502. QIndex = cpi->common.base_qindex + xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
  503. QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0; // Clamp to valid range
  504. }
  505. }
  506. else
  507. QIndex = cpi->common.base_qindex;
  508. // Y
  509. zbin_extra = ( cpi->common.Y1dequant[QIndex][1] *
  510. ( cpi->zbin_over_quant +
  511. cpi->zbin_mode_boost +
  512. x->act_zbin_adj ) ) >> 7;
  513. for (i = 0; i < 16; i++)
  514. {
  515. x->block[i].quant = cpi->Y1quant[QIndex];
  516. x->block[i].quant_fast = cpi->Y1quant_fast[QIndex];
  517. x->block[i].quant_shift = cpi->Y1quant_shift[QIndex];
  518. x->block[i].zbin = cpi->Y1zbin[QIndex];
  519. x->block[i].round = cpi->Y1round[QIndex];
  520. x->e_mbd.block[i].dequant = cpi->common.Y1dequant[QIndex];
  521. x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_y1[QIndex];
  522. x->block[i].zbin_extra = (short)zbin_extra;
  523. }
  524. // UV
  525. zbin_extra = ( cpi->common.UVdequant[QIndex][1] *
  526. ( cpi->zbin_over_quant +
  527. cpi->zbin_mode_boost +
  528. x->act_zbin_adj ) ) >> 7;
  529. for (i = 16; i < 24; i++)
  530. {
  531. x->block[i].quant = cpi->UVquant[QIndex];
  532. x->block[i].quant_fast = cpi->UVquant_fast[QIndex];
  533. x->block[i].quant_shift = cpi->UVquant_shift[QIndex];
  534. x->block[i].zbin = cpi->UVzbin[QIndex];
  535. x->block[i].round = cpi->UVround[QIndex];
  536. x->e_mbd.block[i].dequant = cpi->common.UVdequant[QIndex];
  537. x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_uv[QIndex];
  538. x->block[i].zbin_extra = (short)zbin_extra;
  539. }
  540. // Y2
  541. zbin_extra = ( cpi->common.Y2dequant[QIndex][1] *
  542. ( (cpi->zbin_over_quant / 2) +
  543. cpi->zbin_mode_boost +
  544. x->act_zbin_adj ) ) >> 7;
  545. x->block[24].quant_fast = cpi->Y2quant_fast[QIndex];
  546. x->block[24].quant = cpi->Y2quant[QIndex];
  547. x->block[24].quant_shift = cpi->Y2quant_shift[QIndex];
  548. x->block[24].zbin = cpi->Y2zbin[QIndex];
  549. x->block[24].round = cpi->Y2round[QIndex];
  550. x->e_mbd.block[24].dequant = cpi->common.Y2dequant[QIndex];
  551. x->block[24].zrun_zbin_boost = cpi->zrun_zbin_boost_y2[QIndex];
  552. x->block[24].zbin_extra = (short)zbin_extra;
  553. /* save this macroblock QIndex for vp8_update_zbin_extra() */
  554. x->q_index = QIndex;
  555. }
  556. void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x)
  557. {
  558. int i;
  559. int QIndex = x->q_index;
  560. int zbin_extra;
  561. // Y
  562. zbin_extra = ( cpi->common.Y1dequant[QIndex][1] *
  563. ( cpi->zbin_over_quant +
  564. cpi->zbin_mode_boost +
  565. x->act_zbin_adj ) ) >> 7;
  566. for (i = 0; i < 16; i++)
  567. {
  568. x->block[i].zbin_extra = (short)zbin_extra;
  569. }
  570. // UV
  571. zbin_extra = ( cpi->common.UVdequant[QIndex][1] *
  572. ( cpi->zbin_over_quant +
  573. cpi->zbin_mode_boost +
  574. x->act_zbin_adj ) ) >> 7;
  575. for (i = 16; i < 24; i++)
  576. {
  577. x->block[i].zbin_extra = (short)zbin_extra;
  578. }
  579. // Y2
  580. zbin_extra = ( cpi->common.Y2dequant[QIndex][1] *
  581. ( (cpi->zbin_over_quant / 2) +
  582. cpi->zbin_mode_boost +
  583. x->act_zbin_adj ) ) >> 7;
  584. x->block[24].zbin_extra = (short)zbin_extra;
  585. }
  586. void vp8cx_frame_init_quantizer(VP8_COMP *cpi)
  587. {
  588. // Clear Zbin mode boost for default case
  589. cpi->zbin_mode_boost = 0;
  590. // MB level quantizer setup
  591. vp8cx_mb_init_quantizer(cpi, &cpi->mb);
  592. }
  593. void vp8_set_quantizer(struct VP8_COMP *cpi, int Q)
  594. {
  595. VP8_COMMON *cm = &cpi->common;
  596. MACROBLOCKD *mbd = &cpi->mb.e_mbd;
  597. int update = 0;
  598. int new_delta_q;
  599. cm->base_qindex = Q;
  600. /* if any of the delta_q values are changing update flag has to be set */
  601. /* currently only y2dc_delta_q may change */
  602. cm->y1dc_delta_q = 0;
  603. cm->y2ac_delta_q = 0;
  604. cm->uvdc_delta_q = 0;
  605. cm->uvac_delta_q = 0;
  606. if (Q < 4)
  607. {
  608. new_delta_q = 4-Q;
  609. }
  610. else
  611. new_delta_q = 0;
  612. update |= cm->y2dc_delta_q != new_delta_q;
  613. cm->y2dc_delta_q = new_delta_q;
  614. // Set Segment specific quatizers
  615. mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
  616. mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
  617. mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
  618. mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
  619. /* quantizer has to be reinitialized for any delta_q changes */
  620. if(update)
  621. vp8cx_init_quantizer(cpi);
  622. }