vp9_quantize.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. default:
  179. assert(bit_depth == VPX_BITS_12);
  180. return q == 0 ? 64 : (quant < 2368 ? 84 : 80);
  181. }
  182. #else
  183. (void)bit_depth;
  184. return q == 0 ? 64 : (quant < 148 ? 84 : 80);
  185. #endif
  186. }
  187. void vp9_init_quantizer(VP9_COMP *cpi) {
  188. VP9_COMMON *const cm = &cpi->common;
  189. QUANTS *const quants = &cpi->quants;
  190. int i, q, quant;
  191. for (q = 0; q < QINDEX_RANGE; q++) {
  192. int qzbin_factor = get_qzbin_factor(q, cm->bit_depth);
  193. int qrounding_factor = q == 0 ? 64 : 48;
  194. const int sharpness_adjustment = 16 * (7 - cpi->oxcf.sharpness) / 7;
  195. if (cpi->oxcf.sharpness > 0 && q > 0) {
  196. qzbin_factor = 64 + sharpness_adjustment;
  197. qrounding_factor = 64 - sharpness_adjustment;
  198. }
  199. for (i = 0; i < 2; ++i) {
  200. int qrounding_factor_fp = i == 0 ? 48 : 42;
  201. if (q == 0) qrounding_factor_fp = 64;
  202. if (cpi->oxcf.sharpness > 0)
  203. qrounding_factor_fp = 64 - sharpness_adjustment;
  204. // y
  205. quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q, cm->bit_depth)
  206. : vp9_ac_quant(q, 0, cm->bit_depth);
  207. invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant);
  208. quants->y_quant_fp[q][i] = (1 << 16) / quant;
  209. quants->y_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
  210. quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
  211. quants->y_round[q][i] = (qrounding_factor * quant) >> 7;
  212. cpi->y_dequant[q][i] = quant;
  213. // uv
  214. quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q, cm->bit_depth)
  215. : vp9_ac_quant(q, cm->uv_ac_delta_q, cm->bit_depth);
  216. invert_quant(&quants->uv_quant[q][i], &quants->uv_quant_shift[q][i],
  217. quant);
  218. quants->uv_quant_fp[q][i] = (1 << 16) / quant;
  219. quants->uv_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
  220. quants->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
  221. quants->uv_round[q][i] = (qrounding_factor * quant) >> 7;
  222. cpi->uv_dequant[q][i] = quant;
  223. }
  224. for (i = 2; i < 8; i++) {
  225. quants->y_quant[q][i] = quants->y_quant[q][1];
  226. quants->y_quant_fp[q][i] = quants->y_quant_fp[q][1];
  227. quants->y_round_fp[q][i] = quants->y_round_fp[q][1];
  228. quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
  229. quants->y_zbin[q][i] = quants->y_zbin[q][1];
  230. quants->y_round[q][i] = quants->y_round[q][1];
  231. cpi->y_dequant[q][i] = cpi->y_dequant[q][1];
  232. quants->uv_quant[q][i] = quants->uv_quant[q][1];
  233. quants->uv_quant_fp[q][i] = quants->uv_quant_fp[q][1];
  234. quants->uv_round_fp[q][i] = quants->uv_round_fp[q][1];
  235. quants->uv_quant_shift[q][i] = quants->uv_quant_shift[q][1];
  236. quants->uv_zbin[q][i] = quants->uv_zbin[q][1];
  237. quants->uv_round[q][i] = quants->uv_round[q][1];
  238. cpi->uv_dequant[q][i] = cpi->uv_dequant[q][1];
  239. }
  240. }
  241. }
  242. void vp9_init_plane_quantizers(VP9_COMP *cpi, MACROBLOCK *x) {
  243. const VP9_COMMON *const cm = &cpi->common;
  244. MACROBLOCKD *const xd = &x->e_mbd;
  245. QUANTS *const quants = &cpi->quants;
  246. const int segment_id = xd->mi[0]->segment_id;
  247. const int qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
  248. const int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
  249. int i;
  250. // Y
  251. x->plane[0].quant = quants->y_quant[qindex];
  252. x->plane[0].quant_fp = quants->y_quant_fp[qindex];
  253. memcpy(x->plane[0].round_fp, quants->y_round_fp[qindex],
  254. 8 * sizeof(*(x->plane[0].round_fp)));
  255. x->plane[0].quant_shift = quants->y_quant_shift[qindex];
  256. x->plane[0].zbin = quants->y_zbin[qindex];
  257. x->plane[0].round = quants->y_round[qindex];
  258. xd->plane[0].dequant = cpi->y_dequant[qindex];
  259. x->plane[0].quant_thred[0] = x->plane[0].zbin[0] * x->plane[0].zbin[0];
  260. x->plane[0].quant_thred[1] = x->plane[0].zbin[1] * x->plane[0].zbin[1];
  261. // UV
  262. for (i = 1; i < 3; i++) {
  263. x->plane[i].quant = quants->uv_quant[qindex];
  264. x->plane[i].quant_fp = quants->uv_quant_fp[qindex];
  265. memcpy(x->plane[i].round_fp, quants->uv_round_fp[qindex],
  266. 8 * sizeof(*(x->plane[i].round_fp)));
  267. x->plane[i].quant_shift = quants->uv_quant_shift[qindex];
  268. x->plane[i].zbin = quants->uv_zbin[qindex];
  269. x->plane[i].round = quants->uv_round[qindex];
  270. xd->plane[i].dequant = cpi->uv_dequant[qindex];
  271. x->plane[i].quant_thred[0] = x->plane[i].zbin[0] * x->plane[i].zbin[0];
  272. x->plane[i].quant_thred[1] = x->plane[i].zbin[1] * x->plane[i].zbin[1];
  273. }
  274. x->skip_block = segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
  275. x->q_index = qindex;
  276. set_error_per_bit(x, rdmult);
  277. vp9_initialize_me_consts(cpi, x, x->q_index);
  278. }
  279. void vp9_frame_init_quantizer(VP9_COMP *cpi) {
  280. vp9_init_plane_quantizers(cpi, &cpi->td.mb);
  281. }
  282. void vp9_set_quantizer(VP9_COMMON *cm, int q) {
  283. // quantizer has to be reinitialized with vp9_init_quantizer() if any
  284. // delta_q changes.
  285. cm->base_qindex = q;
  286. cm->y_dc_delta_q = 0;
  287. cm->uv_dc_delta_q = 0;
  288. cm->uv_ac_delta_q = 0;
  289. }
  290. // Table that converts 0-63 Q-range values passed in outside to the Qindex
  291. // range used internally.
  292. static const int quantizer_to_qindex[] = {
  293. 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48,
  294. 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100,
  295. 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152,
  296. 156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204,
  297. 208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 249, 255,
  298. };
  299. int vp9_quantizer_to_qindex(int quantizer) {
  300. return quantizer_to_qindex[quantizer];
  301. }
  302. int vp9_qindex_to_quantizer(int qindex) {
  303. int quantizer;
  304. for (quantizer = 0; quantizer < 64; ++quantizer)
  305. if (quantizer_to_qindex[quantizer] >= qindex) return quantizer;
  306. return 63;
  307. }