vp9_quantize.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 <assert.h>
  11. #include <math.h>
  12. #include "./vpx_dsp_rtcd.h"
  13. #include "vpx_mem/vpx_mem.h"
  14. #include "vpx_ports/mem.h"
  15. #include "vp9/common/vp9_quant_common.h"
  16. #include "vp9/common/vp9_seg_common.h"
  17. #include "vp9/encoder/vp9_encoder.h"
  18. #include "vp9/encoder/vp9_quantize.h"
  19. #include "vp9/encoder/vp9_rd.h"
  20. void vp9_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
  21. int skip_block, const int16_t *round_ptr,
  22. const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
  23. tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
  24. uint16_t *eob_ptr, const int16_t *scan,
  25. const int16_t *iscan) {
  26. int i, eob = -1;
  27. (void)iscan;
  28. (void)skip_block;
  29. assert(!skip_block);
  30. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  31. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  32. // Quantization pass: All coefficients with index >= zero_flag are
  33. // skippable. Note: zero_flag can be zero.
  34. for (i = 0; i < n_coeffs; i++) {
  35. const int rc = scan[i];
  36. const int coeff = coeff_ptr[rc];
  37. const int coeff_sign = (coeff >> 31);
  38. const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  39. int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
  40. tmp = (tmp * quant_ptr[rc != 0]) >> 16;
  41. qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
  42. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
  43. if (tmp) eob = i;
  44. }
  45. *eob_ptr = eob + 1;
  46. }
  47. #if CONFIG_VP9_HIGHBITDEPTH
  48. void vp9_highbd_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
  49. int skip_block, const int16_t *round_ptr,
  50. const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
  51. tran_low_t *dqcoeff_ptr,
  52. const int16_t *dequant_ptr, uint16_t *eob_ptr,
  53. const int16_t *scan, const int16_t *iscan) {
  54. int i;
  55. int eob = -1;
  56. (void)iscan;
  57. (void)skip_block;
  58. assert(!skip_block);
  59. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  60. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  61. // Quantization pass: All coefficients with index >= zero_flag are
  62. // skippable. Note: zero_flag can be zero.
  63. for (i = 0; i < n_coeffs; i++) {
  64. const int rc = scan[i];
  65. const int coeff = coeff_ptr[rc];
  66. const int coeff_sign = (coeff >> 31);
  67. const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  68. const int64_t tmp = abs_coeff + round_ptr[rc != 0];
  69. const int abs_qcoeff = (int)((tmp * quant_ptr[rc != 0]) >> 16);
  70. qcoeff_ptr[rc] = (tran_low_t)(abs_qcoeff ^ coeff_sign) - coeff_sign;
  71. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
  72. if (abs_qcoeff) eob = i;
  73. }
  74. *eob_ptr = eob + 1;
  75. }
  76. #endif
  77. // TODO(jingning) Refactor this file and combine functions with similar
  78. // operations.
  79. void vp9_quantize_fp_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
  80. int skip_block, const int16_t *round_ptr,
  81. const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
  82. tran_low_t *dqcoeff_ptr,
  83. const int16_t *dequant_ptr, uint16_t *eob_ptr,
  84. const int16_t *scan, const int16_t *iscan) {
  85. int i, eob = -1;
  86. (void)iscan;
  87. (void)skip_block;
  88. assert(!skip_block);
  89. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  90. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  91. for (i = 0; i < n_coeffs; i++) {
  92. const int rc = scan[i];
  93. const int coeff = coeff_ptr[rc];
  94. const int coeff_sign = (coeff >> 31);
  95. int tmp = 0;
  96. int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  97. if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
  98. abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
  99. abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
  100. tmp = (abs_coeff * quant_ptr[rc != 0]) >> 15;
  101. qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
  102. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
  103. }
  104. if (tmp) eob = i;
  105. }
  106. *eob_ptr = eob + 1;
  107. }
  108. #if CONFIG_VP9_HIGHBITDEPTH
  109. void vp9_highbd_quantize_fp_32x32_c(
  110. const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block,
  111. const int16_t *round_ptr, const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
  112. tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
  113. const int16_t *scan, const int16_t *iscan) {
  114. int i, eob = -1;
  115. (void)iscan;
  116. (void)skip_block;
  117. assert(!skip_block);
  118. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  119. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  120. for (i = 0; i < n_coeffs; i++) {
  121. int abs_qcoeff = 0;
  122. const int rc = scan[i];
  123. const int coeff = coeff_ptr[rc];
  124. const int coeff_sign = (coeff >> 31);
  125. const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  126. if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
  127. const int64_t tmp = abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
  128. abs_qcoeff = (int)((tmp * quant_ptr[rc != 0]) >> 15);
  129. qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
  130. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
  131. }
  132. if (abs_qcoeff) eob = i;
  133. }
  134. *eob_ptr = eob + 1;
  135. }
  136. #endif
  137. void vp9_regular_quantize_b_4x4(MACROBLOCK *x, int plane, int block,
  138. const int16_t *scan, const int16_t *iscan) {
  139. MACROBLOCKD *const xd = &x->e_mbd;
  140. struct macroblock_plane *p = &x->plane[plane];
  141. struct macroblockd_plane *pd = &xd->plane[plane];
  142. tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block),
  143. *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  144. const int n_coeffs = 4 * 4;
  145. if (x->skip_block) {
  146. memset(qcoeff, 0, n_coeffs * sizeof(*qcoeff));
  147. memset(dqcoeff, 0, n_coeffs * sizeof(*dqcoeff));
  148. return;
  149. }
  150. #if CONFIG_VP9_HIGHBITDEPTH
  151. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  152. vpx_highbd_quantize_b(BLOCK_OFFSET(p->coeff, block), n_coeffs,
  153. x->skip_block, p->zbin, p->round, p->quant,
  154. p->quant_shift, qcoeff, dqcoeff, pd->dequant,
  155. &p->eobs[block], scan, iscan);
  156. return;
  157. }
  158. #endif
  159. vpx_quantize_b(BLOCK_OFFSET(p->coeff, block), n_coeffs, x->skip_block,
  160. p->zbin, p->round, p->quant, p->quant_shift, qcoeff, dqcoeff,
  161. pd->dequant, &p->eobs[block], scan, iscan);
  162. }
  163. static void invert_quant(int16_t *quant, int16_t *shift, int d) {
  164. unsigned t;
  165. int l, m;
  166. t = d;
  167. for (l = 0; t > 1; l++) t >>= 1;
  168. m = 1 + (1 << (16 + l)) / d;
  169. *quant = (int16_t)(m - (1 << 16));
  170. *shift = 1 << (16 - l);
  171. }
  172. static int get_qzbin_factor(int q, vpx_bit_depth_t bit_depth) {
  173. const int quant = vp9_dc_quant(q, 0, bit_depth);
  174. #if CONFIG_VP9_HIGHBITDEPTH
  175. switch (bit_depth) {
  176. case VPX_BITS_8: return q == 0 ? 64 : (quant < 148 ? 84 : 80);
  177. case VPX_BITS_10: return q == 0 ? 64 : (quant < 592 ? 84 : 80);
  178. case VPX_BITS_12: return q == 0 ? 64 : (quant < 2368 ? 84 : 80);
  179. default:
  180. assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
  181. return -1;
  182. }
  183. #else
  184. (void)bit_depth;
  185. return q == 0 ? 64 : (quant < 148 ? 84 : 80);
  186. #endif
  187. }
  188. void vp9_init_quantizer(VP9_COMP *cpi) {
  189. VP9_COMMON *const cm = &cpi->common;
  190. QUANTS *const quants = &cpi->quants;
  191. int i, q, quant;
  192. for (q = 0; q < QINDEX_RANGE; q++) {
  193. const int qzbin_factor = get_qzbin_factor(q, cm->bit_depth);
  194. const int qrounding_factor = q == 0 ? 64 : 48;
  195. for (i = 0; i < 2; ++i) {
  196. int qrounding_factor_fp = i == 0 ? 48 : 42;
  197. if (q == 0) qrounding_factor_fp = 64;
  198. // y
  199. quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q, cm->bit_depth)
  200. : vp9_ac_quant(q, 0, cm->bit_depth);
  201. invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant);
  202. quants->y_quant_fp[q][i] = (1 << 16) / quant;
  203. quants->y_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
  204. quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
  205. quants->y_round[q][i] = (qrounding_factor * quant) >> 7;
  206. cpi->y_dequant[q][i] = quant;
  207. // uv
  208. quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q, cm->bit_depth)
  209. : vp9_ac_quant(q, cm->uv_ac_delta_q, cm->bit_depth);
  210. invert_quant(&quants->uv_quant[q][i], &quants->uv_quant_shift[q][i],
  211. quant);
  212. quants->uv_quant_fp[q][i] = (1 << 16) / quant;
  213. quants->uv_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
  214. quants->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
  215. quants->uv_round[q][i] = (qrounding_factor * quant) >> 7;
  216. cpi->uv_dequant[q][i] = quant;
  217. }
  218. for (i = 2; i < 8; i++) {
  219. quants->y_quant[q][i] = quants->y_quant[q][1];
  220. quants->y_quant_fp[q][i] = quants->y_quant_fp[q][1];
  221. quants->y_round_fp[q][i] = quants->y_round_fp[q][1];
  222. quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
  223. quants->y_zbin[q][i] = quants->y_zbin[q][1];
  224. quants->y_round[q][i] = quants->y_round[q][1];
  225. cpi->y_dequant[q][i] = cpi->y_dequant[q][1];
  226. quants->uv_quant[q][i] = quants->uv_quant[q][1];
  227. quants->uv_quant_fp[q][i] = quants->uv_quant_fp[q][1];
  228. quants->uv_round_fp[q][i] = quants->uv_round_fp[q][1];
  229. quants->uv_quant_shift[q][i] = quants->uv_quant_shift[q][1];
  230. quants->uv_zbin[q][i] = quants->uv_zbin[q][1];
  231. quants->uv_round[q][i] = quants->uv_round[q][1];
  232. cpi->uv_dequant[q][i] = cpi->uv_dequant[q][1];
  233. }
  234. }
  235. }
  236. void vp9_init_plane_quantizers(VP9_COMP *cpi, MACROBLOCK *x) {
  237. const VP9_COMMON *const cm = &cpi->common;
  238. MACROBLOCKD *const xd = &x->e_mbd;
  239. QUANTS *const quants = &cpi->quants;
  240. const int segment_id = xd->mi[0]->segment_id;
  241. const int qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
  242. const int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
  243. int i;
  244. // Y
  245. x->plane[0].quant = quants->y_quant[qindex];
  246. x->plane[0].quant_fp = quants->y_quant_fp[qindex];
  247. x->plane[0].round_fp = quants->y_round_fp[qindex];
  248. x->plane[0].quant_shift = quants->y_quant_shift[qindex];
  249. x->plane[0].zbin = quants->y_zbin[qindex];
  250. x->plane[0].round = quants->y_round[qindex];
  251. xd->plane[0].dequant = cpi->y_dequant[qindex];
  252. x->plane[0].quant_thred[0] = x->plane[0].zbin[0] * x->plane[0].zbin[0];
  253. x->plane[0].quant_thred[1] = x->plane[0].zbin[1] * x->plane[0].zbin[1];
  254. // UV
  255. for (i = 1; i < 3; i++) {
  256. x->plane[i].quant = quants->uv_quant[qindex];
  257. x->plane[i].quant_fp = quants->uv_quant_fp[qindex];
  258. x->plane[i].round_fp = quants->uv_round_fp[qindex];
  259. x->plane[i].quant_shift = quants->uv_quant_shift[qindex];
  260. x->plane[i].zbin = quants->uv_zbin[qindex];
  261. x->plane[i].round = quants->uv_round[qindex];
  262. xd->plane[i].dequant = cpi->uv_dequant[qindex];
  263. x->plane[i].quant_thred[0] = x->plane[i].zbin[0] * x->plane[i].zbin[0];
  264. x->plane[i].quant_thred[1] = x->plane[i].zbin[1] * x->plane[i].zbin[1];
  265. }
  266. x->skip_block = segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
  267. x->q_index = qindex;
  268. set_error_per_bit(x, rdmult);
  269. vp9_initialize_me_consts(cpi, x, x->q_index);
  270. }
  271. void vp9_frame_init_quantizer(VP9_COMP *cpi) {
  272. vp9_init_plane_quantizers(cpi, &cpi->td.mb);
  273. }
  274. void vp9_set_quantizer(VP9_COMMON *cm, int q) {
  275. // quantizer has to be reinitialized with vp9_init_quantizer() if any
  276. // delta_q changes.
  277. cm->base_qindex = q;
  278. cm->y_dc_delta_q = 0;
  279. cm->uv_dc_delta_q = 0;
  280. cm->uv_ac_delta_q = 0;
  281. }
  282. // Table that converts 0-63 Q-range values passed in outside to the Qindex
  283. // range used internally.
  284. static const int quantizer_to_qindex[] = {
  285. 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48,
  286. 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100,
  287. 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152,
  288. 156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204,
  289. 208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 249, 255,
  290. };
  291. int vp9_quantizer_to_qindex(int quantizer) {
  292. return quantizer_to_qindex[quantizer];
  293. }
  294. int vp9_qindex_to_quantizer(int qindex) {
  295. int quantizer;
  296. for (quantizer = 0; quantizer < 64; ++quantizer)
  297. if (quantizer_to_qindex[quantizer] >= qindex) return quantizer;
  298. return 63;
  299. }