2
0

vp9_quantize.c 13 KB

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