vp9_quantize.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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 "vpx_ports/mem.h"
  13. #include "vp9/common/vp9_quant_common.h"
  14. #include "vp9/common/vp9_seg_common.h"
  15. #include "vp9/encoder/vp9_encoder.h"
  16. #include "vp9/encoder/vp9_quantize.h"
  17. #include "vp9/encoder/vp9_rd.h"
  18. void vp9_quantize_dc(const tran_low_t *coeff_ptr,
  19. int n_coeffs, int skip_block,
  20. const int16_t *round_ptr, const int16_t quant,
  21. tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
  22. const int16_t dequant_ptr, uint16_t *eob_ptr) {
  23. const int rc = 0;
  24. const int coeff = coeff_ptr[rc];
  25. const int coeff_sign = (coeff >> 31);
  26. const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  27. int tmp, eob = -1;
  28. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  29. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  30. if (!skip_block) {
  31. tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
  32. tmp = (tmp * quant) >> 16;
  33. qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
  34. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr;
  35. if (tmp)
  36. eob = 0;
  37. }
  38. *eob_ptr = eob + 1;
  39. }
  40. #if CONFIG_VP9_HIGHBITDEPTH
  41. void vp9_highbd_quantize_dc(const tran_low_t *coeff_ptr,
  42. int n_coeffs, int skip_block,
  43. const int16_t *round_ptr, const int16_t quant,
  44. tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
  45. const int16_t dequant_ptr, uint16_t *eob_ptr) {
  46. int eob = -1;
  47. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  48. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  49. if (!skip_block) {
  50. const int rc = 0;
  51. const int coeff = coeff_ptr[rc];
  52. const int coeff_sign = (coeff >> 31);
  53. const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  54. const int64_t tmp =
  55. (clamp(abs_coeff + round_ptr[rc != 0], INT32_MIN, INT32_MAX) *
  56. quant) >> 16;
  57. qcoeff_ptr[rc] = (tran_low_t)((tmp ^ coeff_sign) - coeff_sign);
  58. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr;
  59. if (tmp)
  60. eob = 0;
  61. }
  62. *eob_ptr = eob + 1;
  63. }
  64. #endif
  65. void vp9_quantize_dc_32x32(const tran_low_t *coeff_ptr, int skip_block,
  66. const int16_t *round_ptr, const int16_t quant,
  67. tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
  68. const int16_t dequant_ptr, uint16_t *eob_ptr) {
  69. const int n_coeffs = 1024;
  70. const int rc = 0;
  71. const int coeff = coeff_ptr[rc];
  72. const int coeff_sign = (coeff >> 31);
  73. const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  74. int tmp, eob = -1;
  75. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  76. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  77. if (!skip_block) {
  78. tmp = clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1),
  79. INT16_MIN, INT16_MAX);
  80. tmp = (tmp * quant) >> 15;
  81. qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
  82. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr / 2;
  83. if (tmp)
  84. eob = 0;
  85. }
  86. *eob_ptr = eob + 1;
  87. }
  88. #if CONFIG_VP9_HIGHBITDEPTH
  89. void vp9_highbd_quantize_dc_32x32(const tran_low_t *coeff_ptr,
  90. int skip_block,
  91. const int16_t *round_ptr,
  92. const int16_t quant,
  93. tran_low_t *qcoeff_ptr,
  94. tran_low_t *dqcoeff_ptr,
  95. const int16_t dequant_ptr,
  96. uint16_t *eob_ptr) {
  97. const int n_coeffs = 1024;
  98. int eob = -1;
  99. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  100. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  101. if (!skip_block) {
  102. const int rc = 0;
  103. const int coeff = coeff_ptr[rc];
  104. const int coeff_sign = (coeff >> 31);
  105. const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  106. const int64_t tmp =
  107. (clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1),
  108. INT32_MIN, INT32_MAX) * quant) >> 15;
  109. qcoeff_ptr[rc] = (tran_low_t)((tmp ^ coeff_sign) - coeff_sign);
  110. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr / 2;
  111. if (tmp)
  112. eob = 0;
  113. }
  114. *eob_ptr = eob + 1;
  115. }
  116. #endif
  117. void vp9_quantize_fp_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
  118. int skip_block,
  119. const int16_t *zbin_ptr, const int16_t *round_ptr,
  120. const int16_t *quant_ptr, const int16_t *quant_shift_ptr,
  121. tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
  122. const int16_t *dequant_ptr,
  123. uint16_t *eob_ptr,
  124. const int16_t *scan, const int16_t *iscan) {
  125. int i, eob = -1;
  126. // TODO(jingning) Decide the need of these arguments after the
  127. // quantization process is completed.
  128. (void)zbin_ptr;
  129. (void)quant_shift_ptr;
  130. (void)iscan;
  131. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  132. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  133. if (!skip_block) {
  134. // Quantization pass: All coefficients with index >= zero_flag are
  135. // skippable. Note: zero_flag can be zero.
  136. for (i = 0; i < n_coeffs; i++) {
  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. int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
  142. tmp = (tmp * quant_ptr[rc != 0]) >> 16;
  143. qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
  144. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
  145. if (tmp)
  146. eob = i;
  147. }
  148. }
  149. *eob_ptr = eob + 1;
  150. }
  151. #if CONFIG_VP9_HIGHBITDEPTH
  152. void vp9_highbd_quantize_fp_c(const tran_low_t *coeff_ptr,
  153. intptr_t count,
  154. int skip_block,
  155. const int16_t *zbin_ptr,
  156. const int16_t *round_ptr,
  157. const int16_t *quant_ptr,
  158. const int16_t *quant_shift_ptr,
  159. tran_low_t *qcoeff_ptr,
  160. tran_low_t *dqcoeff_ptr,
  161. const int16_t *dequant_ptr,
  162. uint16_t *eob_ptr,
  163. const int16_t *scan,
  164. const int16_t *iscan) {
  165. int i;
  166. int eob = -1;
  167. // TODO(jingning) Decide the need of these arguments after the
  168. // quantization process is completed.
  169. (void)zbin_ptr;
  170. (void)quant_shift_ptr;
  171. (void)iscan;
  172. memset(qcoeff_ptr, 0, count * sizeof(*qcoeff_ptr));
  173. memset(dqcoeff_ptr, 0, count * sizeof(*dqcoeff_ptr));
  174. if (!skip_block) {
  175. // Quantization pass: All coefficients with index >= zero_flag are
  176. // skippable. Note: zero_flag can be zero.
  177. for (i = 0; i < count; i++) {
  178. const int rc = scan[i];
  179. const int coeff = coeff_ptr[rc];
  180. const int coeff_sign = (coeff >> 31);
  181. const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  182. const int64_t tmp =
  183. (clamp(abs_coeff + round_ptr[rc != 0], INT32_MIN, INT32_MAX) *
  184. quant_ptr[rc != 0]) >> 16;
  185. qcoeff_ptr[rc] = (tran_low_t)((tmp ^ coeff_sign) - coeff_sign);
  186. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
  187. if (tmp)
  188. eob = i;
  189. }
  190. }
  191. *eob_ptr = eob + 1;
  192. }
  193. #endif
  194. // TODO(jingning) Refactor this file and combine functions with similar
  195. // operations.
  196. void vp9_quantize_fp_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
  197. int skip_block,
  198. const int16_t *zbin_ptr, const int16_t *round_ptr,
  199. const int16_t *quant_ptr,
  200. const int16_t *quant_shift_ptr,
  201. tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
  202. const int16_t *dequant_ptr,
  203. uint16_t *eob_ptr,
  204. const int16_t *scan, const int16_t *iscan) {
  205. int i, eob = -1;
  206. (void)zbin_ptr;
  207. (void)quant_shift_ptr;
  208. (void)iscan;
  209. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  210. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  211. if (!skip_block) {
  212. for (i = 0; i < n_coeffs; i++) {
  213. const int rc = scan[i];
  214. const int coeff = coeff_ptr[rc];
  215. const int coeff_sign = (coeff >> 31);
  216. int tmp = 0;
  217. int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  218. if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
  219. abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
  220. abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
  221. tmp = (abs_coeff * quant_ptr[rc != 0]) >> 15;
  222. qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
  223. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
  224. }
  225. if (tmp)
  226. eob = i;
  227. }
  228. }
  229. *eob_ptr = eob + 1;
  230. }
  231. #if CONFIG_VP9_HIGHBITDEPTH
  232. void vp9_highbd_quantize_fp_32x32_c(const tran_low_t *coeff_ptr,
  233. intptr_t n_coeffs, int skip_block,
  234. const int16_t *zbin_ptr,
  235. const int16_t *round_ptr,
  236. const int16_t *quant_ptr,
  237. const int16_t *quant_shift_ptr,
  238. tran_low_t *qcoeff_ptr,
  239. tran_low_t *dqcoeff_ptr,
  240. const int16_t *dequant_ptr,
  241. uint16_t *eob_ptr,
  242. const int16_t *scan, const int16_t *iscan) {
  243. int i, eob = -1;
  244. (void)zbin_ptr;
  245. (void)quant_shift_ptr;
  246. (void)iscan;
  247. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  248. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  249. if (!skip_block) {
  250. for (i = 0; i < n_coeffs; i++) {
  251. const int rc = scan[i];
  252. const int coeff = coeff_ptr[rc];
  253. const int coeff_sign = (coeff >> 31);
  254. int64_t tmp = 0;
  255. const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  256. if (abs_coeff >= (dequant_ptr[rc != 0] >> 2)) {
  257. tmp = clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1),
  258. INT32_MIN, INT32_MAX);
  259. tmp = (tmp * quant_ptr[rc != 0]) >> 15;
  260. qcoeff_ptr[rc] = (tran_low_t)((tmp ^ coeff_sign) - coeff_sign);
  261. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
  262. }
  263. if (tmp)
  264. eob = i;
  265. }
  266. }
  267. *eob_ptr = eob + 1;
  268. }
  269. #endif
  270. void vp9_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
  271. int skip_block,
  272. const int16_t *zbin_ptr, const int16_t *round_ptr,
  273. const int16_t *quant_ptr, const int16_t *quant_shift_ptr,
  274. tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
  275. const int16_t *dequant_ptr,
  276. uint16_t *eob_ptr,
  277. const int16_t *scan, const int16_t *iscan) {
  278. int i, non_zero_count = (int)n_coeffs, eob = -1;
  279. const int zbins[2] = {zbin_ptr[0], zbin_ptr[1]};
  280. const int nzbins[2] = {zbins[0] * -1, zbins[1] * -1};
  281. (void)iscan;
  282. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  283. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  284. if (!skip_block) {
  285. // Pre-scan pass
  286. for (i = (int)n_coeffs - 1; i >= 0; i--) {
  287. const int rc = scan[i];
  288. const int coeff = coeff_ptr[rc];
  289. if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0])
  290. non_zero_count--;
  291. else
  292. break;
  293. }
  294. // Quantization pass: All coefficients with index >= zero_flag are
  295. // skippable. Note: zero_flag can be zero.
  296. for (i = 0; i < non_zero_count; i++) {
  297. const int rc = scan[i];
  298. const int coeff = coeff_ptr[rc];
  299. const int coeff_sign = (coeff >> 31);
  300. const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  301. if (abs_coeff >= zbins[rc != 0]) {
  302. int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
  303. tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
  304. quant_shift_ptr[rc != 0]) >> 16; // quantization
  305. qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
  306. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
  307. if (tmp)
  308. eob = i;
  309. }
  310. }
  311. }
  312. *eob_ptr = eob + 1;
  313. }
  314. #if CONFIG_VP9_HIGHBITDEPTH
  315. void vp9_highbd_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
  316. int skip_block, const int16_t *zbin_ptr,
  317. const int16_t *round_ptr, const int16_t *quant_ptr,
  318. const int16_t *quant_shift_ptr,
  319. tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
  320. const int16_t *dequant_ptr,
  321. uint16_t *eob_ptr, const int16_t *scan,
  322. const int16_t *iscan) {
  323. int i, non_zero_count = (int)n_coeffs, eob = -1;
  324. const int zbins[2] = {zbin_ptr[0], zbin_ptr[1]};
  325. const int nzbins[2] = {zbins[0] * -1, zbins[1] * -1};
  326. (void)iscan;
  327. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  328. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  329. if (!skip_block) {
  330. // Pre-scan pass
  331. for (i = (int)n_coeffs - 1; i >= 0; i--) {
  332. const int rc = scan[i];
  333. const int coeff = coeff_ptr[rc];
  334. if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0])
  335. non_zero_count--;
  336. else
  337. break;
  338. }
  339. // Quantization pass: All coefficients with index >= zero_flag are
  340. // skippable. Note: zero_flag can be zero.
  341. for (i = 0; i < non_zero_count; i++) {
  342. const int rc = scan[i];
  343. const int coeff = coeff_ptr[rc];
  344. const int coeff_sign = (coeff >> 31);
  345. const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  346. if (abs_coeff >= zbins[rc != 0]) {
  347. int64_t tmp = clamp(abs_coeff + round_ptr[rc != 0],
  348. INT32_MIN, INT32_MAX);
  349. tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
  350. quant_shift_ptr[rc != 0]) >> 16; // quantization
  351. qcoeff_ptr[rc] = (tran_low_t)((tmp ^ coeff_sign) - coeff_sign);
  352. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];
  353. if (tmp)
  354. eob = i;
  355. }
  356. }
  357. }
  358. *eob_ptr = eob + 1;
  359. }
  360. #endif
  361. void vp9_quantize_b_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
  362. int skip_block,
  363. const int16_t *zbin_ptr, const int16_t *round_ptr,
  364. const int16_t *quant_ptr,
  365. const int16_t *quant_shift_ptr,
  366. tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
  367. const int16_t *dequant_ptr,
  368. uint16_t *eob_ptr,
  369. const int16_t *scan, const int16_t *iscan) {
  370. const int zbins[2] = {ROUND_POWER_OF_TWO(zbin_ptr[0], 1),
  371. ROUND_POWER_OF_TWO(zbin_ptr[1], 1)};
  372. const int nzbins[2] = {zbins[0] * -1, zbins[1] * -1};
  373. int idx = 0;
  374. int idx_arr[1024];
  375. int i, eob = -1;
  376. (void)iscan;
  377. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  378. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  379. if (!skip_block) {
  380. // Pre-scan pass
  381. for (i = 0; i < n_coeffs; i++) {
  382. const int rc = scan[i];
  383. const int coeff = coeff_ptr[rc];
  384. // If the coefficient is out of the base ZBIN range, keep it for
  385. // quantization.
  386. if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0])
  387. idx_arr[idx++] = i;
  388. }
  389. // Quantization pass: only process the coefficients selected in
  390. // pre-scan pass. Note: idx can be zero.
  391. for (i = 0; i < idx; i++) {
  392. const int rc = scan[idx_arr[i]];
  393. const int coeff = coeff_ptr[rc];
  394. const int coeff_sign = (coeff >> 31);
  395. int tmp;
  396. int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  397. abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1);
  398. abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX);
  399. tmp = ((((abs_coeff * quant_ptr[rc != 0]) >> 16) + abs_coeff) *
  400. quant_shift_ptr[rc != 0]) >> 15;
  401. qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
  402. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
  403. if (tmp)
  404. eob = idx_arr[i];
  405. }
  406. }
  407. *eob_ptr = eob + 1;
  408. }
  409. #if CONFIG_VP9_HIGHBITDEPTH
  410. void vp9_highbd_quantize_b_32x32_c(const tran_low_t *coeff_ptr,
  411. intptr_t n_coeffs, int skip_block,
  412. const int16_t *zbin_ptr,
  413. const int16_t *round_ptr,
  414. const int16_t *quant_ptr,
  415. const int16_t *quant_shift_ptr,
  416. tran_low_t *qcoeff_ptr,
  417. tran_low_t *dqcoeff_ptr,
  418. const int16_t *dequant_ptr,
  419. uint16_t *eob_ptr,
  420. const int16_t *scan, const int16_t *iscan) {
  421. const int zbins[2] = {ROUND_POWER_OF_TWO(zbin_ptr[0], 1),
  422. ROUND_POWER_OF_TWO(zbin_ptr[1], 1)};
  423. const int nzbins[2] = {zbins[0] * -1, zbins[1] * -1};
  424. int idx = 0;
  425. int idx_arr[1024];
  426. int i, eob = -1;
  427. (void)iscan;
  428. memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  429. memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
  430. if (!skip_block) {
  431. // Pre-scan pass
  432. for (i = 0; i < n_coeffs; i++) {
  433. const int rc = scan[i];
  434. const int coeff = coeff_ptr[rc];
  435. // If the coefficient is out of the base ZBIN range, keep it for
  436. // quantization.
  437. if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0])
  438. idx_arr[idx++] = i;
  439. }
  440. // Quantization pass: only process the coefficients selected in
  441. // pre-scan pass. Note: idx can be zero.
  442. for (i = 0; i < idx; i++) {
  443. const int rc = scan[idx_arr[i]];
  444. const int coeff = coeff_ptr[rc];
  445. const int coeff_sign = (coeff >> 31);
  446. const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
  447. int64_t tmp = clamp(abs_coeff +
  448. ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1),
  449. INT32_MIN, INT32_MAX);
  450. tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
  451. quant_shift_ptr[rc != 0]) >> 15;
  452. qcoeff_ptr[rc] = (tran_low_t)((tmp ^ coeff_sign) - coeff_sign);
  453. dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2;
  454. if (tmp)
  455. eob = idx_arr[i];
  456. }
  457. }
  458. *eob_ptr = eob + 1;
  459. }
  460. #endif
  461. void vp9_regular_quantize_b_4x4(MACROBLOCK *x, int plane, int block,
  462. const int16_t *scan, const int16_t *iscan) {
  463. MACROBLOCKD *const xd = &x->e_mbd;
  464. struct macroblock_plane *p = &x->plane[plane];
  465. struct macroblockd_plane *pd = &xd->plane[plane];
  466. #if CONFIG_VP9_HIGHBITDEPTH
  467. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  468. vp9_highbd_quantize_b(BLOCK_OFFSET(p->coeff, block),
  469. 16, x->skip_block,
  470. p->zbin, p->round, p->quant, p->quant_shift,
  471. BLOCK_OFFSET(p->qcoeff, block),
  472. BLOCK_OFFSET(pd->dqcoeff, block),
  473. pd->dequant, &p->eobs[block],
  474. scan, iscan);
  475. return;
  476. }
  477. #endif
  478. vp9_quantize_b(BLOCK_OFFSET(p->coeff, block),
  479. 16, x->skip_block,
  480. p->zbin, p->round, p->quant, p->quant_shift,
  481. BLOCK_OFFSET(p->qcoeff, block),
  482. BLOCK_OFFSET(pd->dqcoeff, block),
  483. pd->dequant, &p->eobs[block], scan, iscan);
  484. }
  485. static void invert_quant(int16_t *quant, int16_t *shift, int d) {
  486. unsigned t;
  487. int l;
  488. t = d;
  489. for (l = 0; t > 1; l++)
  490. t >>= 1;
  491. t = 1 + (1 << (16 + l)) / d;
  492. *quant = (int16_t)(t - (1 << 16));
  493. *shift = 1 << (16 - l);
  494. }
  495. static int get_qzbin_factor(int q, vpx_bit_depth_t bit_depth) {
  496. const int quant = vp9_dc_quant(q, 0, bit_depth);
  497. #if CONFIG_VP9_HIGHBITDEPTH
  498. switch (bit_depth) {
  499. case VPX_BITS_8:
  500. return q == 0 ? 64 : (quant < 148 ? 84 : 80);
  501. case VPX_BITS_10:
  502. return q == 0 ? 64 : (quant < 592 ? 84 : 80);
  503. case VPX_BITS_12:
  504. return q == 0 ? 64 : (quant < 2368 ? 84 : 80);
  505. default:
  506. assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
  507. return -1;
  508. }
  509. #else
  510. (void) bit_depth;
  511. return q == 0 ? 64 : (quant < 148 ? 84 : 80);
  512. #endif
  513. }
  514. void vp9_init_quantizer(VP9_COMP *cpi) {
  515. VP9_COMMON *const cm = &cpi->common;
  516. QUANTS *const quants = &cpi->quants;
  517. int i, q, quant;
  518. for (q = 0; q < QINDEX_RANGE; q++) {
  519. const int qzbin_factor = get_qzbin_factor(q, cm->bit_depth);
  520. const int qrounding_factor = q == 0 ? 64 : 48;
  521. for (i = 0; i < 2; ++i) {
  522. int qrounding_factor_fp = i == 0 ? 48 : 42;
  523. if (q == 0)
  524. qrounding_factor_fp = 64;
  525. // y
  526. quant = i == 0 ? vp9_dc_quant(q, cm->y_dc_delta_q, cm->bit_depth)
  527. : vp9_ac_quant(q, 0, cm->bit_depth);
  528. invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant);
  529. quants->y_quant_fp[q][i] = (1 << 16) / quant;
  530. quants->y_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
  531. quants->y_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
  532. quants->y_round[q][i] = (qrounding_factor * quant) >> 7;
  533. cpi->y_dequant[q][i] = quant;
  534. // uv
  535. quant = i == 0 ? vp9_dc_quant(q, cm->uv_dc_delta_q, cm->bit_depth)
  536. : vp9_ac_quant(q, cm->uv_ac_delta_q, cm->bit_depth);
  537. invert_quant(&quants->uv_quant[q][i],
  538. &quants->uv_quant_shift[q][i], quant);
  539. quants->uv_quant_fp[q][i] = (1 << 16) / quant;
  540. quants->uv_round_fp[q][i] = (qrounding_factor_fp * quant) >> 7;
  541. quants->uv_zbin[q][i] = ROUND_POWER_OF_TWO(qzbin_factor * quant, 7);
  542. quants->uv_round[q][i] = (qrounding_factor * quant) >> 7;
  543. cpi->uv_dequant[q][i] = quant;
  544. }
  545. for (i = 2; i < 8; i++) {
  546. quants->y_quant[q][i] = quants->y_quant[q][1];
  547. quants->y_quant_fp[q][i] = quants->y_quant_fp[q][1];
  548. quants->y_round_fp[q][i] = quants->y_round_fp[q][1];
  549. quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
  550. quants->y_zbin[q][i] = quants->y_zbin[q][1];
  551. quants->y_round[q][i] = quants->y_round[q][1];
  552. cpi->y_dequant[q][i] = cpi->y_dequant[q][1];
  553. quants->uv_quant[q][i] = quants->uv_quant[q][1];
  554. quants->uv_quant_fp[q][i] = quants->uv_quant_fp[q][1];
  555. quants->uv_round_fp[q][i] = quants->uv_round_fp[q][1];
  556. quants->uv_quant_shift[q][i] = quants->uv_quant_shift[q][1];
  557. quants->uv_zbin[q][i] = quants->uv_zbin[q][1];
  558. quants->uv_round[q][i] = quants->uv_round[q][1];
  559. cpi->uv_dequant[q][i] = cpi->uv_dequant[q][1];
  560. }
  561. }
  562. }
  563. void vp9_init_plane_quantizers(VP9_COMP *cpi, MACROBLOCK *x) {
  564. const VP9_COMMON *const cm = &cpi->common;
  565. MACROBLOCKD *const xd = &x->e_mbd;
  566. QUANTS *const quants = &cpi->quants;
  567. const int segment_id = xd->mi[0]->mbmi.segment_id;
  568. const int qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
  569. const int rdmult = vp9_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
  570. int i;
  571. // Y
  572. x->plane[0].quant = quants->y_quant[qindex];
  573. x->plane[0].quant_fp = quants->y_quant_fp[qindex];
  574. x->plane[0].round_fp = quants->y_round_fp[qindex];
  575. x->plane[0].quant_shift = quants->y_quant_shift[qindex];
  576. x->plane[0].zbin = quants->y_zbin[qindex];
  577. x->plane[0].round = quants->y_round[qindex];
  578. xd->plane[0].dequant = cpi->y_dequant[qindex];
  579. x->plane[0].quant_thred[0] = x->plane[0].zbin[0] * x->plane[0].zbin[0];
  580. x->plane[0].quant_thred[1] = x->plane[0].zbin[1] * x->plane[0].zbin[1];
  581. // UV
  582. for (i = 1; i < 3; i++) {
  583. x->plane[i].quant = quants->uv_quant[qindex];
  584. x->plane[i].quant_fp = quants->uv_quant_fp[qindex];
  585. x->plane[i].round_fp = quants->uv_round_fp[qindex];
  586. x->plane[i].quant_shift = quants->uv_quant_shift[qindex];
  587. x->plane[i].zbin = quants->uv_zbin[qindex];
  588. x->plane[i].round = quants->uv_round[qindex];
  589. xd->plane[i].dequant = cpi->uv_dequant[qindex];
  590. x->plane[i].quant_thred[0] = x->plane[i].zbin[0] * x->plane[i].zbin[0];
  591. x->plane[i].quant_thred[1] = x->plane[i].zbin[1] * x->plane[i].zbin[1];
  592. }
  593. x->skip_block = vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP);
  594. x->q_index = qindex;
  595. x->errorperbit = rdmult >> 6;
  596. x->errorperbit += (x->errorperbit == 0);
  597. vp9_initialize_me_consts(cpi, x, x->q_index);
  598. }
  599. void vp9_frame_init_quantizer(VP9_COMP *cpi) {
  600. vp9_init_plane_quantizers(cpi, &cpi->td.mb);
  601. }
  602. void vp9_set_quantizer(VP9_COMMON *cm, int q) {
  603. // quantizer has to be reinitialized with vp9_init_quantizer() if any
  604. // delta_q changes.
  605. cm->base_qindex = q;
  606. cm->y_dc_delta_q = 0;
  607. cm->uv_dc_delta_q = 0;
  608. cm->uv_ac_delta_q = 0;
  609. }
  610. // Table that converts 0-63 Q-range values passed in outside to the Qindex
  611. // range used internally.
  612. static const int quantizer_to_qindex[] = {
  613. 0, 4, 8, 12, 16, 20, 24, 28,
  614. 32, 36, 40, 44, 48, 52, 56, 60,
  615. 64, 68, 72, 76, 80, 84, 88, 92,
  616. 96, 100, 104, 108, 112, 116, 120, 124,
  617. 128, 132, 136, 140, 144, 148, 152, 156,
  618. 160, 164, 168, 172, 176, 180, 184, 188,
  619. 192, 196, 200, 204, 208, 212, 216, 220,
  620. 224, 228, 232, 236, 240, 244, 249, 255,
  621. };
  622. int vp9_quantizer_to_qindex(int quantizer) {
  623. return quantizer_to_qindex[quantizer];
  624. }
  625. int vp9_qindex_to_quantizer(int qindex) {
  626. int quantizer;
  627. for (quantizer = 0; quantizer < 64; ++quantizer)
  628. if (quantizer_to_qindex[quantizer] >= qindex)
  629. return quantizer;
  630. return 63;
  631. }