vp9_encodemb.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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 "./vp9_rtcd.h"
  11. #include "./vpx_config.h"
  12. #include "./vpx_dsp_rtcd.h"
  13. #include "vpx_dsp/quantize.h"
  14. #include "vpx_mem/vpx_mem.h"
  15. #include "vpx_ports/mem.h"
  16. #if CONFIG_MISMATCH_DEBUG
  17. #include "vpx_util/vpx_debug_util.h"
  18. #endif
  19. #include "vp9/common/vp9_idct.h"
  20. #include "vp9/common/vp9_reconinter.h"
  21. #include "vp9/common/vp9_reconintra.h"
  22. #include "vp9/common/vp9_scan.h"
  23. #include "vp9/encoder/vp9_encodemb.h"
  24. #include "vp9/encoder/vp9_rd.h"
  25. #include "vp9/encoder/vp9_tokenize.h"
  26. struct optimize_ctx {
  27. ENTROPY_CONTEXT ta[MAX_MB_PLANE][16];
  28. ENTROPY_CONTEXT tl[MAX_MB_PLANE][16];
  29. };
  30. void vp9_subtract_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
  31. struct macroblock_plane *const p = &x->plane[plane];
  32. const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane];
  33. const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
  34. const int bw = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  35. const int bh = 4 * num_4x4_blocks_high_lookup[plane_bsize];
  36. #if CONFIG_VP9_HIGHBITDEPTH
  37. if (x->e_mbd.cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  38. vpx_highbd_subtract_block(bh, bw, p->src_diff, bw, p->src.buf,
  39. p->src.stride, pd->dst.buf, pd->dst.stride,
  40. x->e_mbd.bd);
  41. return;
  42. }
  43. #endif // CONFIG_VP9_HIGHBITDEPTH
  44. vpx_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
  45. pd->dst.buf, pd->dst.stride);
  46. }
  47. static const int plane_rd_mult[REF_TYPES][PLANE_TYPES] = {
  48. { 10, 6 },
  49. { 8, 5 },
  50. };
  51. // 'num' can be negative, but 'shift' must be non-negative.
  52. #define RIGHT_SHIFT_POSSIBLY_NEGATIVE(num, shift) \
  53. (((num) >= 0) ? (num) >> (shift) : -((-(num)) >> (shift)))
  54. int vp9_optimize_b(MACROBLOCK *mb, int plane, int block, TX_SIZE tx_size,
  55. int ctx) {
  56. MACROBLOCKD *const xd = &mb->e_mbd;
  57. struct macroblock_plane *const p = &mb->plane[plane];
  58. struct macroblockd_plane *const pd = &xd->plane[plane];
  59. const int ref = is_inter_block(xd->mi[0]);
  60. uint8_t token_cache[1024];
  61. const tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  62. tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  63. tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  64. const int eob = p->eobs[block];
  65. const PLANE_TYPE plane_type = get_plane_type(plane);
  66. const int default_eob = 16 << (tx_size << 1);
  67. const int shift = (tx_size == TX_32X32);
  68. const int16_t *const dequant_ptr = pd->dequant;
  69. const uint8_t *const band_translate = get_band_translate(tx_size);
  70. const scan_order *const so = get_scan(xd, tx_size, plane_type, block);
  71. const int16_t *const scan = so->scan;
  72. const int16_t *const nb = so->neighbors;
  73. const MODE_INFO *mbmi = xd->mi[0];
  74. const int sharpness = mb->sharpness;
  75. const int64_t rdadj = (int64_t)mb->rdmult * plane_rd_mult[ref][plane_type];
  76. const int64_t rdmult =
  77. (sharpness == 0 ? rdadj >> 1
  78. : (rdadj * (8 - sharpness + mbmi->segment_id)) >> 4);
  79. const int64_t rddiv = mb->rddiv;
  80. int64_t rd_cost0, rd_cost1;
  81. int64_t rate0, rate1;
  82. int16_t t0, t1;
  83. int i, final_eob;
  84. int count_high_values_after_eob = 0;
  85. #if CONFIG_VP9_HIGHBITDEPTH
  86. const uint16_t *cat6_high_cost = vp9_get_high_cost_table(xd->bd);
  87. #else
  88. const uint16_t *cat6_high_cost = vp9_get_high_cost_table(8);
  89. #endif
  90. unsigned int(*const token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
  91. mb->token_costs[tx_size][plane_type][ref];
  92. unsigned int(*token_costs_cur)[2][COEFF_CONTEXTS][ENTROPY_TOKENS];
  93. int64_t eob_cost0, eob_cost1;
  94. const int ctx0 = ctx;
  95. int64_t accu_rate = 0;
  96. // Initialized to the worst possible error for the largest transform size.
  97. // This ensures that it never goes negative.
  98. int64_t accu_error = ((int64_t)1) << 50;
  99. int64_t best_block_rd_cost = INT64_MAX;
  100. int x_prev = 1;
  101. tran_low_t before_best_eob_qc = 0;
  102. tran_low_t before_best_eob_dqc = 0;
  103. assert((!plane_type && !plane) || (plane_type && plane));
  104. assert(eob <= default_eob);
  105. for (i = 0; i < eob; i++) {
  106. const int rc = scan[i];
  107. token_cache[rc] = vp9_pt_energy_class[vp9_get_token(qcoeff[rc])];
  108. }
  109. final_eob = 0;
  110. // Initial RD cost.
  111. token_costs_cur = token_costs + band_translate[0];
  112. rate0 = (*token_costs_cur)[0][ctx0][EOB_TOKEN];
  113. best_block_rd_cost = RDCOST(rdmult, rddiv, rate0, accu_error);
  114. // For each token, pick one of two choices greedily:
  115. // (i) First candidate: Keep current quantized value, OR
  116. // (ii) Second candidate: Reduce quantized value by 1.
  117. for (i = 0; i < eob; i++) {
  118. const int rc = scan[i];
  119. const int x = qcoeff[rc];
  120. const int band_cur = band_translate[i];
  121. const int ctx_cur = (i == 0) ? ctx : get_coef_context(nb, token_cache, i);
  122. const int token_tree_sel_cur = (x_prev == 0);
  123. token_costs_cur = token_costs + band_cur;
  124. if (x == 0) { // No need to search
  125. const int token = vp9_get_token(x);
  126. rate0 = (*token_costs_cur)[token_tree_sel_cur][ctx_cur][token];
  127. accu_rate += rate0;
  128. x_prev = 0;
  129. // Note: accu_error does not change.
  130. } else {
  131. const int dqv = dequant_ptr[rc != 0];
  132. // Compute the distortion for quantizing to 0.
  133. const int diff_for_zero_raw = (0 - coeff[rc]) * (1 << shift);
  134. const int diff_for_zero =
  135. #if CONFIG_VP9_HIGHBITDEPTH
  136. (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
  137. ? RIGHT_SHIFT_POSSIBLY_NEGATIVE(diff_for_zero_raw, xd->bd - 8)
  138. :
  139. #endif
  140. diff_for_zero_raw;
  141. const int64_t distortion_for_zero =
  142. (int64_t)diff_for_zero * diff_for_zero;
  143. // Compute the distortion for the first candidate
  144. const int diff0_raw = (dqcoeff[rc] - coeff[rc]) * (1 << shift);
  145. const int diff0 =
  146. #if CONFIG_VP9_HIGHBITDEPTH
  147. (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
  148. ? RIGHT_SHIFT_POSSIBLY_NEGATIVE(diff0_raw, xd->bd - 8)
  149. :
  150. #endif // CONFIG_VP9_HIGHBITDEPTH
  151. diff0_raw;
  152. const int64_t distortion0 = (int64_t)diff0 * diff0;
  153. // Compute the distortion for the second candidate
  154. const int sign = -(x < 0); // -1 if x is negative and 0 otherwise.
  155. const int x1 = x - 2 * sign - 1; // abs(x1) = abs(x) - 1.
  156. int64_t distortion1;
  157. if (x1 != 0) {
  158. const int dqv_step =
  159. #if CONFIG_VP9_HIGHBITDEPTH
  160. (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? dqv >> (xd->bd - 8)
  161. :
  162. #endif // CONFIG_VP9_HIGHBITDEPTH
  163. dqv;
  164. const int diff_step = (dqv_step + sign) ^ sign;
  165. const int diff1 = diff0 - diff_step;
  166. assert(dqv > 0); // We aren't right shifting a negative number above.
  167. distortion1 = (int64_t)diff1 * diff1;
  168. } else {
  169. distortion1 = distortion_for_zero;
  170. }
  171. {
  172. // Calculate RDCost for current coeff for the two candidates.
  173. const int64_t base_bits0 = vp9_get_token_cost(x, &t0, cat6_high_cost);
  174. const int64_t base_bits1 = vp9_get_token_cost(x1, &t1, cat6_high_cost);
  175. rate0 =
  176. base_bits0 + (*token_costs_cur)[token_tree_sel_cur][ctx_cur][t0];
  177. rate1 =
  178. base_bits1 + (*token_costs_cur)[token_tree_sel_cur][ctx_cur][t1];
  179. }
  180. {
  181. int rdcost_better_for_x1, eob_rdcost_better_for_x1;
  182. int dqc0, dqc1;
  183. int64_t best_eob_cost_cur;
  184. int use_x1;
  185. // Calculate RD Cost effect on the next coeff for the two candidates.
  186. int64_t next_bits0 = 0;
  187. int64_t next_bits1 = 0;
  188. int64_t next_eob_bits0 = 0;
  189. int64_t next_eob_bits1 = 0;
  190. if (i < default_eob - 1) {
  191. int ctx_next, token_tree_sel_next;
  192. const int band_next = band_translate[i + 1];
  193. const int token_next =
  194. (i + 1 != eob) ? vp9_get_token(qcoeff[scan[i + 1]]) : EOB_TOKEN;
  195. unsigned int(*const token_costs_next)[2][COEFF_CONTEXTS]
  196. [ENTROPY_TOKENS] =
  197. token_costs + band_next;
  198. token_cache[rc] = vp9_pt_energy_class[t0];
  199. ctx_next = get_coef_context(nb, token_cache, i + 1);
  200. token_tree_sel_next = (x == 0);
  201. next_bits0 =
  202. (*token_costs_next)[token_tree_sel_next][ctx_next][token_next];
  203. next_eob_bits0 =
  204. (*token_costs_next)[token_tree_sel_next][ctx_next][EOB_TOKEN];
  205. token_cache[rc] = vp9_pt_energy_class[t1];
  206. ctx_next = get_coef_context(nb, token_cache, i + 1);
  207. token_tree_sel_next = (x1 == 0);
  208. next_bits1 =
  209. (*token_costs_next)[token_tree_sel_next][ctx_next][token_next];
  210. if (x1 != 0) {
  211. next_eob_bits1 =
  212. (*token_costs_next)[token_tree_sel_next][ctx_next][EOB_TOKEN];
  213. }
  214. }
  215. // Compare the total RD costs for two candidates.
  216. rd_cost0 = RDCOST(rdmult, rddiv, (rate0 + next_bits0), distortion0);
  217. rd_cost1 = RDCOST(rdmult, rddiv, (rate1 + next_bits1), distortion1);
  218. rdcost_better_for_x1 = (rd_cost1 < rd_cost0);
  219. eob_cost0 = RDCOST(rdmult, rddiv, (accu_rate + rate0 + next_eob_bits0),
  220. (accu_error + distortion0 - distortion_for_zero));
  221. eob_cost1 = eob_cost0;
  222. if (x1 != 0) {
  223. eob_cost1 =
  224. RDCOST(rdmult, rddiv, (accu_rate + rate1 + next_eob_bits1),
  225. (accu_error + distortion1 - distortion_for_zero));
  226. eob_rdcost_better_for_x1 = (eob_cost1 < eob_cost0);
  227. } else {
  228. eob_rdcost_better_for_x1 = 0;
  229. }
  230. // Calculate the two candidate de-quantized values.
  231. dqc0 = dqcoeff[rc];
  232. dqc1 = 0;
  233. if (rdcost_better_for_x1 + eob_rdcost_better_for_x1) {
  234. if (x1 != 0) {
  235. dqc1 = RIGHT_SHIFT_POSSIBLY_NEGATIVE(x1 * dqv, shift);
  236. } else {
  237. dqc1 = 0;
  238. }
  239. }
  240. // Pick and record the better quantized and de-quantized values.
  241. if (rdcost_better_for_x1) {
  242. qcoeff[rc] = x1;
  243. dqcoeff[rc] = dqc1;
  244. accu_rate += rate1;
  245. accu_error += distortion1 - distortion_for_zero;
  246. assert(distortion1 <= distortion_for_zero);
  247. token_cache[rc] = vp9_pt_energy_class[t1];
  248. } else {
  249. accu_rate += rate0;
  250. accu_error += distortion0 - distortion_for_zero;
  251. assert(distortion0 <= distortion_for_zero);
  252. token_cache[rc] = vp9_pt_energy_class[t0];
  253. }
  254. if (sharpness > 0 && abs(qcoeff[rc]) > 1) count_high_values_after_eob++;
  255. assert(accu_error >= 0);
  256. x_prev = qcoeff[rc]; // Update based on selected quantized value.
  257. use_x1 = (x1 != 0) && eob_rdcost_better_for_x1;
  258. best_eob_cost_cur = use_x1 ? eob_cost1 : eob_cost0;
  259. // Determine whether to move the eob position to i+1
  260. if (best_eob_cost_cur < best_block_rd_cost) {
  261. best_block_rd_cost = best_eob_cost_cur;
  262. final_eob = i + 1;
  263. count_high_values_after_eob = 0;
  264. if (use_x1) {
  265. before_best_eob_qc = x1;
  266. before_best_eob_dqc = dqc1;
  267. } else {
  268. before_best_eob_qc = x;
  269. before_best_eob_dqc = dqc0;
  270. }
  271. }
  272. }
  273. }
  274. }
  275. if (count_high_values_after_eob > 0) {
  276. final_eob = eob - 1;
  277. for (; final_eob >= 0; final_eob--) {
  278. const int rc = scan[final_eob];
  279. const int x = qcoeff[rc];
  280. if (x) {
  281. break;
  282. }
  283. }
  284. final_eob++;
  285. } else {
  286. assert(final_eob <= eob);
  287. if (final_eob > 0) {
  288. int rc;
  289. assert(before_best_eob_qc != 0);
  290. i = final_eob - 1;
  291. rc = scan[i];
  292. qcoeff[rc] = before_best_eob_qc;
  293. dqcoeff[rc] = before_best_eob_dqc;
  294. }
  295. for (i = final_eob; i < eob; i++) {
  296. int rc = scan[i];
  297. qcoeff[rc] = 0;
  298. dqcoeff[rc] = 0;
  299. }
  300. }
  301. mb->plane[plane].eobs[block] = final_eob;
  302. return final_eob;
  303. }
  304. #undef RIGHT_SHIFT_POSSIBLY_NEGATIVE
  305. static INLINE void fdct32x32(int rd_transform, const int16_t *src,
  306. tran_low_t *dst, int src_stride) {
  307. if (rd_transform)
  308. vpx_fdct32x32_rd(src, dst, src_stride);
  309. else
  310. vpx_fdct32x32(src, dst, src_stride);
  311. }
  312. #if CONFIG_VP9_HIGHBITDEPTH
  313. static INLINE void highbd_fdct32x32(int rd_transform, const int16_t *src,
  314. tran_low_t *dst, int src_stride) {
  315. if (rd_transform)
  316. vpx_highbd_fdct32x32_rd(src, dst, src_stride);
  317. else
  318. vpx_highbd_fdct32x32(src, dst, src_stride);
  319. }
  320. #endif // CONFIG_VP9_HIGHBITDEPTH
  321. void vp9_xform_quant_fp(MACROBLOCK *x, int plane, int block, int row, int col,
  322. BLOCK_SIZE plane_bsize, TX_SIZE tx_size) {
  323. MACROBLOCKD *const xd = &x->e_mbd;
  324. const struct macroblock_plane *const p = &x->plane[plane];
  325. const struct macroblockd_plane *const pd = &xd->plane[plane];
  326. const scan_order *const scan_order = &vp9_default_scan_orders[tx_size];
  327. tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  328. tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  329. tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  330. uint16_t *const eob = &p->eobs[block];
  331. const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  332. const int16_t *src_diff;
  333. src_diff = &p->src_diff[4 * (row * diff_stride + col)];
  334. // skip block condition should be handled before this is called.
  335. assert(!x->skip_block);
  336. #if CONFIG_VP9_HIGHBITDEPTH
  337. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  338. switch (tx_size) {
  339. case TX_32X32:
  340. highbd_fdct32x32(x->use_lp32x32fdct, src_diff, coeff, diff_stride);
  341. vp9_highbd_quantize_fp_32x32(coeff, 1024, x->skip_block, p->round_fp,
  342. p->quant_fp, qcoeff, dqcoeff, pd->dequant,
  343. eob, scan_order->scan, scan_order->iscan);
  344. break;
  345. case TX_16X16:
  346. vpx_highbd_fdct16x16(src_diff, coeff, diff_stride);
  347. vp9_highbd_quantize_fp(coeff, 256, x->skip_block, p->round_fp,
  348. p->quant_fp, qcoeff, dqcoeff, pd->dequant, eob,
  349. scan_order->scan, scan_order->iscan);
  350. break;
  351. case TX_8X8:
  352. vpx_highbd_fdct8x8(src_diff, coeff, diff_stride);
  353. vp9_highbd_quantize_fp(coeff, 64, x->skip_block, p->round_fp,
  354. p->quant_fp, qcoeff, dqcoeff, pd->dequant, eob,
  355. scan_order->scan, scan_order->iscan);
  356. break;
  357. default:
  358. assert(tx_size == TX_4X4);
  359. x->fwd_txfm4x4(src_diff, coeff, diff_stride);
  360. vp9_highbd_quantize_fp(coeff, 16, x->skip_block, p->round_fp,
  361. p->quant_fp, qcoeff, dqcoeff, pd->dequant, eob,
  362. scan_order->scan, scan_order->iscan);
  363. break;
  364. }
  365. return;
  366. }
  367. #endif // CONFIG_VP9_HIGHBITDEPTH
  368. switch (tx_size) {
  369. case TX_32X32:
  370. fdct32x32(x->use_lp32x32fdct, src_diff, coeff, diff_stride);
  371. vp9_quantize_fp_32x32(coeff, 1024, x->skip_block, p->round_fp,
  372. p->quant_fp, qcoeff, dqcoeff, pd->dequant, eob,
  373. scan_order->scan, scan_order->iscan);
  374. break;
  375. case TX_16X16:
  376. vpx_fdct16x16(src_diff, coeff, diff_stride);
  377. vp9_quantize_fp(coeff, 256, x->skip_block, p->round_fp, p->quant_fp,
  378. qcoeff, dqcoeff, pd->dequant, eob, scan_order->scan,
  379. scan_order->iscan);
  380. break;
  381. case TX_8X8:
  382. vpx_fdct8x8(src_diff, coeff, diff_stride);
  383. vp9_quantize_fp(coeff, 64, x->skip_block, p->round_fp, p->quant_fp,
  384. qcoeff, dqcoeff, pd->dequant, eob, scan_order->scan,
  385. scan_order->iscan);
  386. break;
  387. default:
  388. assert(tx_size == TX_4X4);
  389. x->fwd_txfm4x4(src_diff, coeff, diff_stride);
  390. vp9_quantize_fp(coeff, 16, x->skip_block, p->round_fp, p->quant_fp,
  391. qcoeff, dqcoeff, pd->dequant, eob, scan_order->scan,
  392. scan_order->iscan);
  393. break;
  394. }
  395. }
  396. void vp9_xform_quant_dc(MACROBLOCK *x, int plane, int block, int row, int col,
  397. BLOCK_SIZE plane_bsize, TX_SIZE tx_size) {
  398. MACROBLOCKD *const xd = &x->e_mbd;
  399. const struct macroblock_plane *const p = &x->plane[plane];
  400. const struct macroblockd_plane *const pd = &xd->plane[plane];
  401. tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  402. tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  403. tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  404. uint16_t *const eob = &p->eobs[block];
  405. const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  406. const int16_t *src_diff;
  407. src_diff = &p->src_diff[4 * (row * diff_stride + col)];
  408. // skip block condition should be handled before this is called.
  409. assert(!x->skip_block);
  410. #if CONFIG_VP9_HIGHBITDEPTH
  411. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  412. switch (tx_size) {
  413. case TX_32X32:
  414. vpx_highbd_fdct32x32_1(src_diff, coeff, diff_stride);
  415. vpx_highbd_quantize_dc_32x32(coeff, x->skip_block, p->round,
  416. p->quant_fp[0], qcoeff, dqcoeff,
  417. pd->dequant[0], eob);
  418. break;
  419. case TX_16X16:
  420. vpx_highbd_fdct16x16_1(src_diff, coeff, diff_stride);
  421. vpx_highbd_quantize_dc(coeff, 256, x->skip_block, p->round,
  422. p->quant_fp[0], qcoeff, dqcoeff, pd->dequant[0],
  423. eob);
  424. break;
  425. case TX_8X8:
  426. vpx_highbd_fdct8x8_1(src_diff, coeff, diff_stride);
  427. vpx_highbd_quantize_dc(coeff, 64, x->skip_block, p->round,
  428. p->quant_fp[0], qcoeff, dqcoeff, pd->dequant[0],
  429. eob);
  430. break;
  431. default:
  432. assert(tx_size == TX_4X4);
  433. x->fwd_txfm4x4(src_diff, coeff, diff_stride);
  434. vpx_highbd_quantize_dc(coeff, 16, x->skip_block, p->round,
  435. p->quant_fp[0], qcoeff, dqcoeff, pd->dequant[0],
  436. eob);
  437. break;
  438. }
  439. return;
  440. }
  441. #endif // CONFIG_VP9_HIGHBITDEPTH
  442. switch (tx_size) {
  443. case TX_32X32:
  444. vpx_fdct32x32_1(src_diff, coeff, diff_stride);
  445. vpx_quantize_dc_32x32(coeff, x->skip_block, p->round, p->quant_fp[0],
  446. qcoeff, dqcoeff, pd->dequant[0], eob);
  447. break;
  448. case TX_16X16:
  449. vpx_fdct16x16_1(src_diff, coeff, diff_stride);
  450. vpx_quantize_dc(coeff, 256, x->skip_block, p->round, p->quant_fp[0],
  451. qcoeff, dqcoeff, pd->dequant[0], eob);
  452. break;
  453. case TX_8X8:
  454. vpx_fdct8x8_1(src_diff, coeff, diff_stride);
  455. vpx_quantize_dc(coeff, 64, x->skip_block, p->round, p->quant_fp[0],
  456. qcoeff, dqcoeff, pd->dequant[0], eob);
  457. break;
  458. default:
  459. assert(tx_size == TX_4X4);
  460. x->fwd_txfm4x4(src_diff, coeff, diff_stride);
  461. vpx_quantize_dc(coeff, 16, x->skip_block, p->round, p->quant_fp[0],
  462. qcoeff, dqcoeff, pd->dequant[0], eob);
  463. break;
  464. }
  465. }
  466. void vp9_xform_quant(MACROBLOCK *x, int plane, int block, int row, int col,
  467. BLOCK_SIZE plane_bsize, TX_SIZE tx_size) {
  468. MACROBLOCKD *const xd = &x->e_mbd;
  469. const struct macroblock_plane *const p = &x->plane[plane];
  470. const struct macroblockd_plane *const pd = &xd->plane[plane];
  471. const scan_order *const scan_order = &vp9_default_scan_orders[tx_size];
  472. tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  473. tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  474. tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  475. uint16_t *const eob = &p->eobs[block];
  476. const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  477. const int16_t *src_diff;
  478. src_diff = &p->src_diff[4 * (row * diff_stride + col)];
  479. // skip block condition should be handled before this is called.
  480. assert(!x->skip_block);
  481. #if CONFIG_VP9_HIGHBITDEPTH
  482. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  483. switch (tx_size) {
  484. case TX_32X32:
  485. highbd_fdct32x32(x->use_lp32x32fdct, src_diff, coeff, diff_stride);
  486. vpx_highbd_quantize_b_32x32(coeff, 1024, x->skip_block, p->zbin,
  487. p->round, p->quant, p->quant_shift, qcoeff,
  488. dqcoeff, pd->dequant, eob, scan_order->scan,
  489. scan_order->iscan);
  490. break;
  491. case TX_16X16:
  492. vpx_highbd_fdct16x16(src_diff, coeff, diff_stride);
  493. vpx_highbd_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round,
  494. p->quant, p->quant_shift, qcoeff, dqcoeff,
  495. pd->dequant, eob, scan_order->scan,
  496. scan_order->iscan);
  497. break;
  498. case TX_8X8:
  499. vpx_highbd_fdct8x8(src_diff, coeff, diff_stride);
  500. vpx_highbd_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round,
  501. p->quant, p->quant_shift, qcoeff, dqcoeff,
  502. pd->dequant, eob, scan_order->scan,
  503. scan_order->iscan);
  504. break;
  505. default:
  506. assert(tx_size == TX_4X4);
  507. x->fwd_txfm4x4(src_diff, coeff, diff_stride);
  508. vpx_highbd_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round,
  509. p->quant, p->quant_shift, qcoeff, dqcoeff,
  510. pd->dequant, eob, scan_order->scan,
  511. scan_order->iscan);
  512. break;
  513. }
  514. return;
  515. }
  516. #endif // CONFIG_VP9_HIGHBITDEPTH
  517. switch (tx_size) {
  518. case TX_32X32:
  519. fdct32x32(x->use_lp32x32fdct, src_diff, coeff, diff_stride);
  520. vpx_quantize_b_32x32(coeff, 1024, x->skip_block, p->zbin, p->round,
  521. p->quant, p->quant_shift, qcoeff, dqcoeff,
  522. pd->dequant, eob, scan_order->scan,
  523. scan_order->iscan);
  524. break;
  525. case TX_16X16:
  526. vpx_fdct16x16(src_diff, coeff, diff_stride);
  527. vpx_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round, p->quant,
  528. p->quant_shift, qcoeff, dqcoeff, pd->dequant, eob,
  529. scan_order->scan, scan_order->iscan);
  530. break;
  531. case TX_8X8:
  532. vpx_fdct8x8(src_diff, coeff, diff_stride);
  533. vpx_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round, p->quant,
  534. p->quant_shift, qcoeff, dqcoeff, pd->dequant, eob,
  535. scan_order->scan, scan_order->iscan);
  536. break;
  537. default:
  538. assert(tx_size == TX_4X4);
  539. x->fwd_txfm4x4(src_diff, coeff, diff_stride);
  540. vpx_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round, p->quant,
  541. p->quant_shift, qcoeff, dqcoeff, pd->dequant, eob,
  542. scan_order->scan, scan_order->iscan);
  543. break;
  544. }
  545. }
  546. static void encode_block(int plane, int block, int row, int col,
  547. BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
  548. struct encode_b_args *const args = arg;
  549. #if CONFIG_MISMATCH_DEBUG
  550. int mi_row = args->mi_row;
  551. int mi_col = args->mi_col;
  552. int output_enabled = args->output_enabled;
  553. #endif
  554. MACROBLOCK *const x = args->x;
  555. MACROBLOCKD *const xd = &x->e_mbd;
  556. struct macroblock_plane *const p = &x->plane[plane];
  557. struct macroblockd_plane *const pd = &xd->plane[plane];
  558. tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  559. uint8_t *dst;
  560. ENTROPY_CONTEXT *a, *l;
  561. dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
  562. a = &args->ta[col];
  563. l = &args->tl[row];
  564. // TODO(jingning): per transformed block zero forcing only enabled for
  565. // luma component. will integrate chroma components as well.
  566. if (x->zcoeff_blk[tx_size][block] && plane == 0) {
  567. p->eobs[block] = 0;
  568. *a = *l = 0;
  569. #if CONFIG_MISMATCH_DEBUG
  570. goto encode_block_end;
  571. #else
  572. return;
  573. #endif
  574. }
  575. if (!x->skip_recode) {
  576. if (x->quant_fp) {
  577. // Encoding process for rtc mode
  578. if (x->skip_txfm[0] == SKIP_TXFM_AC_DC && plane == 0) {
  579. // skip forward transform
  580. p->eobs[block] = 0;
  581. *a = *l = 0;
  582. #if CONFIG_MISMATCH_DEBUG
  583. goto encode_block_end;
  584. #else
  585. return;
  586. #endif
  587. } else {
  588. vp9_xform_quant_fp(x, plane, block, row, col, plane_bsize, tx_size);
  589. }
  590. } else {
  591. if (max_txsize_lookup[plane_bsize] == tx_size) {
  592. int txfm_blk_index = (plane << 2) + (block >> (tx_size << 1));
  593. if (x->skip_txfm[txfm_blk_index] == SKIP_TXFM_NONE) {
  594. // full forward transform and quantization
  595. vp9_xform_quant(x, plane, block, row, col, plane_bsize, tx_size);
  596. } else if (x->skip_txfm[txfm_blk_index] == SKIP_TXFM_AC_ONLY) {
  597. // fast path forward transform and quantization
  598. vp9_xform_quant_dc(x, plane, block, row, col, plane_bsize, tx_size);
  599. } else {
  600. // skip forward transform
  601. p->eobs[block] = 0;
  602. *a = *l = 0;
  603. #if CONFIG_MISMATCH_DEBUG
  604. goto encode_block_end;
  605. #else
  606. return;
  607. #endif
  608. }
  609. } else {
  610. vp9_xform_quant(x, plane, block, row, col, plane_bsize, tx_size);
  611. }
  612. }
  613. }
  614. if (x->optimize && (!x->skip_recode || !x->skip_optimize)) {
  615. const int ctx = combine_entropy_contexts(*a, *l);
  616. *a = *l = vp9_optimize_b(x, plane, block, tx_size, ctx) > 0;
  617. } else {
  618. *a = *l = p->eobs[block] > 0;
  619. }
  620. if (p->eobs[block]) *(args->skip) = 0;
  621. if (x->skip_encode || p->eobs[block] == 0) {
  622. #if CONFIG_MISMATCH_DEBUG
  623. goto encode_block_end;
  624. #else
  625. return;
  626. #endif
  627. }
  628. #if CONFIG_VP9_HIGHBITDEPTH
  629. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  630. uint16_t *const dst16 = CONVERT_TO_SHORTPTR(dst);
  631. switch (tx_size) {
  632. case TX_32X32:
  633. vp9_highbd_idct32x32_add(dqcoeff, dst16, pd->dst.stride, p->eobs[block],
  634. xd->bd);
  635. break;
  636. case TX_16X16:
  637. vp9_highbd_idct16x16_add(dqcoeff, dst16, pd->dst.stride, p->eobs[block],
  638. xd->bd);
  639. break;
  640. case TX_8X8:
  641. vp9_highbd_idct8x8_add(dqcoeff, dst16, pd->dst.stride, p->eobs[block],
  642. xd->bd);
  643. break;
  644. default:
  645. assert(tx_size == TX_4X4);
  646. // this is like vp9_short_idct4x4 but has a special case around eob<=1
  647. // which is significant (not just an optimization) for the lossless
  648. // case.
  649. x->highbd_inv_txfm_add(dqcoeff, dst16, pd->dst.stride, p->eobs[block],
  650. xd->bd);
  651. break;
  652. }
  653. #if CONFIG_MISMATCH_DEBUG
  654. goto encode_block_end;
  655. #else
  656. return;
  657. #endif
  658. }
  659. #endif // CONFIG_VP9_HIGHBITDEPTH
  660. switch (tx_size) {
  661. case TX_32X32:
  662. vp9_idct32x32_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
  663. break;
  664. case TX_16X16:
  665. vp9_idct16x16_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
  666. break;
  667. case TX_8X8:
  668. vp9_idct8x8_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
  669. break;
  670. default:
  671. assert(tx_size == TX_4X4);
  672. // this is like vp9_short_idct4x4 but has a special case around eob<=1
  673. // which is significant (not just an optimization) for the lossless
  674. // case.
  675. x->inv_txfm_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
  676. break;
  677. }
  678. #if CONFIG_MISMATCH_DEBUG
  679. encode_block_end:
  680. if (output_enabled) {
  681. int pixel_c, pixel_r;
  682. int blk_w = 1 << (tx_size + TX_UNIT_SIZE_LOG2);
  683. int blk_h = 1 << (tx_size + TX_UNIT_SIZE_LOG2);
  684. mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, col, row,
  685. pd->subsampling_x, pd->subsampling_y);
  686. mismatch_record_block_tx(dst, pd->dst.stride, plane, pixel_c, pixel_r,
  687. blk_w, blk_h,
  688. xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
  689. }
  690. #endif
  691. }
  692. static void encode_block_pass1(int plane, int block, int row, int col,
  693. BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
  694. void *arg) {
  695. MACROBLOCK *const x = (MACROBLOCK *)arg;
  696. MACROBLOCKD *const xd = &x->e_mbd;
  697. struct macroblock_plane *const p = &x->plane[plane];
  698. struct macroblockd_plane *const pd = &xd->plane[plane];
  699. tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  700. uint8_t *dst;
  701. dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
  702. vp9_xform_quant(x, plane, block, row, col, plane_bsize, tx_size);
  703. if (p->eobs[block] > 0) {
  704. #if CONFIG_VP9_HIGHBITDEPTH
  705. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  706. x->highbd_inv_txfm_add(dqcoeff, CONVERT_TO_SHORTPTR(dst), pd->dst.stride,
  707. p->eobs[block], xd->bd);
  708. return;
  709. }
  710. #endif // CONFIG_VP9_HIGHBITDEPTH
  711. x->inv_txfm_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
  712. }
  713. }
  714. void vp9_encode_sby_pass1(MACROBLOCK *x, BLOCK_SIZE bsize) {
  715. vp9_subtract_plane(x, bsize, 0);
  716. vp9_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
  717. encode_block_pass1, x);
  718. }
  719. void vp9_encode_sb(MACROBLOCK *x, BLOCK_SIZE bsize, int mi_row, int mi_col,
  720. int output_enabled) {
  721. MACROBLOCKD *const xd = &x->e_mbd;
  722. struct optimize_ctx ctx;
  723. MODE_INFO *mi = xd->mi[0];
  724. int plane;
  725. #if CONFIG_MISMATCH_DEBUG
  726. struct encode_b_args arg = { x, 1, NULL, NULL,
  727. &mi->skip, mi_row, mi_col, output_enabled };
  728. #else
  729. struct encode_b_args arg = { x, 1, NULL, NULL, &mi->skip };
  730. (void)mi_row;
  731. (void)mi_col;
  732. (void)output_enabled;
  733. #endif
  734. mi->skip = 1;
  735. if (x->skip) return;
  736. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  737. if (!x->skip_recode) vp9_subtract_plane(x, bsize, plane);
  738. if (x->optimize && (!x->skip_recode || !x->skip_optimize)) {
  739. const struct macroblockd_plane *const pd = &xd->plane[plane];
  740. const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
  741. vp9_get_entropy_contexts(bsize, tx_size, pd, ctx.ta[plane],
  742. ctx.tl[plane]);
  743. arg.enable_coeff_opt = 1;
  744. } else {
  745. arg.enable_coeff_opt = 0;
  746. }
  747. arg.ta = ctx.ta[plane];
  748. arg.tl = ctx.tl[plane];
  749. vp9_foreach_transformed_block_in_plane(xd, bsize, plane, encode_block,
  750. &arg);
  751. }
  752. }
  753. void vp9_encode_block_intra(int plane, int block, int row, int col,
  754. BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
  755. void *arg) {
  756. struct encode_b_args *const args = arg;
  757. MACROBLOCK *const x = args->x;
  758. MACROBLOCKD *const xd = &x->e_mbd;
  759. MODE_INFO *mi = xd->mi[0];
  760. struct macroblock_plane *const p = &x->plane[plane];
  761. struct macroblockd_plane *const pd = &xd->plane[plane];
  762. tran_low_t *coeff = BLOCK_OFFSET(p->coeff, block);
  763. tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  764. tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  765. const scan_order *scan_order;
  766. TX_TYPE tx_type = DCT_DCT;
  767. PREDICTION_MODE mode;
  768. const int bwl = b_width_log2_lookup[plane_bsize];
  769. const int diff_stride = 4 * (1 << bwl);
  770. uint8_t *src, *dst;
  771. int16_t *src_diff;
  772. uint16_t *eob = &p->eobs[block];
  773. const int src_stride = p->src.stride;
  774. const int dst_stride = pd->dst.stride;
  775. ENTROPY_CONTEXT *a = NULL;
  776. ENTROPY_CONTEXT *l = NULL;
  777. int entropy_ctx = 0;
  778. dst = &pd->dst.buf[4 * (row * dst_stride + col)];
  779. src = &p->src.buf[4 * (row * src_stride + col)];
  780. src_diff = &p->src_diff[4 * (row * diff_stride + col)];
  781. if (args->enable_coeff_opt) {
  782. a = &args->ta[col];
  783. l = &args->tl[row];
  784. entropy_ctx = combine_entropy_contexts(*a, *l);
  785. }
  786. if (tx_size == TX_4X4) {
  787. tx_type = get_tx_type_4x4(get_plane_type(plane), xd, block);
  788. scan_order = &vp9_scan_orders[TX_4X4][tx_type];
  789. mode = plane == 0 ? get_y_mode(xd->mi[0], block) : mi->uv_mode;
  790. } else {
  791. mode = plane == 0 ? mi->mode : mi->uv_mode;
  792. if (tx_size == TX_32X32) {
  793. scan_order = &vp9_default_scan_orders[TX_32X32];
  794. } else {
  795. tx_type = get_tx_type(get_plane_type(plane), xd);
  796. scan_order = &vp9_scan_orders[tx_size][tx_type];
  797. }
  798. }
  799. vp9_predict_intra_block(
  800. xd, bwl, tx_size, mode, (x->skip_encode || x->fp_src_pred) ? src : dst,
  801. (x->skip_encode || x->fp_src_pred) ? src_stride : dst_stride, dst,
  802. dst_stride, col, row, plane);
  803. // skip block condition should be handled before this is called.
  804. assert(!x->skip_block);
  805. #if CONFIG_VP9_HIGHBITDEPTH
  806. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  807. uint16_t *const dst16 = CONVERT_TO_SHORTPTR(dst);
  808. switch (tx_size) {
  809. case TX_32X32:
  810. if (!x->skip_recode) {
  811. vpx_highbd_subtract_block(32, 32, src_diff, diff_stride, src,
  812. src_stride, dst, dst_stride, xd->bd);
  813. highbd_fdct32x32(x->use_lp32x32fdct, src_diff, coeff, diff_stride);
  814. vpx_highbd_quantize_b_32x32(coeff, 1024, x->skip_block, p->zbin,
  815. p->round, p->quant, p->quant_shift,
  816. qcoeff, dqcoeff, pd->dequant, eob,
  817. scan_order->scan, scan_order->iscan);
  818. }
  819. if (args->enable_coeff_opt && !x->skip_recode) {
  820. *a = *l = vp9_optimize_b(x, plane, block, tx_size, entropy_ctx) > 0;
  821. }
  822. if (!x->skip_encode && *eob) {
  823. vp9_highbd_idct32x32_add(dqcoeff, dst16, dst_stride, *eob, xd->bd);
  824. }
  825. break;
  826. case TX_16X16:
  827. if (!x->skip_recode) {
  828. vpx_highbd_subtract_block(16, 16, src_diff, diff_stride, src,
  829. src_stride, dst, dst_stride, xd->bd);
  830. if (tx_type == DCT_DCT)
  831. vpx_highbd_fdct16x16(src_diff, coeff, diff_stride);
  832. else
  833. vp9_highbd_fht16x16(src_diff, coeff, diff_stride, tx_type);
  834. vpx_highbd_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round,
  835. p->quant, p->quant_shift, qcoeff, dqcoeff,
  836. pd->dequant, eob, scan_order->scan,
  837. scan_order->iscan);
  838. }
  839. if (args->enable_coeff_opt && !x->skip_recode) {
  840. *a = *l = vp9_optimize_b(x, plane, block, tx_size, entropy_ctx) > 0;
  841. }
  842. if (!x->skip_encode && *eob) {
  843. vp9_highbd_iht16x16_add(tx_type, dqcoeff, dst16, dst_stride, *eob,
  844. xd->bd);
  845. }
  846. break;
  847. case TX_8X8:
  848. if (!x->skip_recode) {
  849. vpx_highbd_subtract_block(8, 8, src_diff, diff_stride, src,
  850. src_stride, dst, dst_stride, xd->bd);
  851. if (tx_type == DCT_DCT)
  852. vpx_highbd_fdct8x8(src_diff, coeff, diff_stride);
  853. else
  854. vp9_highbd_fht8x8(src_diff, coeff, diff_stride, tx_type);
  855. vpx_highbd_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round,
  856. p->quant, p->quant_shift, qcoeff, dqcoeff,
  857. pd->dequant, eob, scan_order->scan,
  858. scan_order->iscan);
  859. }
  860. if (args->enable_coeff_opt && !x->skip_recode) {
  861. *a = *l = vp9_optimize_b(x, plane, block, tx_size, entropy_ctx) > 0;
  862. }
  863. if (!x->skip_encode && *eob) {
  864. vp9_highbd_iht8x8_add(tx_type, dqcoeff, dst16, dst_stride, *eob,
  865. xd->bd);
  866. }
  867. break;
  868. default:
  869. assert(tx_size == TX_4X4);
  870. if (!x->skip_recode) {
  871. vpx_highbd_subtract_block(4, 4, src_diff, diff_stride, src,
  872. src_stride, dst, dst_stride, xd->bd);
  873. if (tx_type != DCT_DCT)
  874. vp9_highbd_fht4x4(src_diff, coeff, diff_stride, tx_type);
  875. else
  876. x->fwd_txfm4x4(src_diff, coeff, diff_stride);
  877. vpx_highbd_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round,
  878. p->quant, p->quant_shift, qcoeff, dqcoeff,
  879. pd->dequant, eob, scan_order->scan,
  880. scan_order->iscan);
  881. }
  882. if (args->enable_coeff_opt && !x->skip_recode) {
  883. *a = *l = vp9_optimize_b(x, plane, block, tx_size, entropy_ctx) > 0;
  884. }
  885. if (!x->skip_encode && *eob) {
  886. if (tx_type == DCT_DCT) {
  887. // this is like vp9_short_idct4x4 but has a special case around
  888. // eob<=1 which is significant (not just an optimization) for the
  889. // lossless case.
  890. x->highbd_inv_txfm_add(dqcoeff, dst16, dst_stride, *eob, xd->bd);
  891. } else {
  892. vp9_highbd_iht4x4_16_add(dqcoeff, dst16, dst_stride, tx_type,
  893. xd->bd);
  894. }
  895. }
  896. break;
  897. }
  898. if (*eob) *(args->skip) = 0;
  899. return;
  900. }
  901. #endif // CONFIG_VP9_HIGHBITDEPTH
  902. switch (tx_size) {
  903. case TX_32X32:
  904. if (!x->skip_recode) {
  905. vpx_subtract_block(32, 32, src_diff, diff_stride, src, src_stride, dst,
  906. dst_stride);
  907. fdct32x32(x->use_lp32x32fdct, src_diff, coeff, diff_stride);
  908. vpx_quantize_b_32x32(coeff, 1024, x->skip_block, p->zbin, p->round,
  909. p->quant, p->quant_shift, qcoeff, dqcoeff,
  910. pd->dequant, eob, scan_order->scan,
  911. scan_order->iscan);
  912. }
  913. if (args->enable_coeff_opt && !x->skip_recode) {
  914. *a = *l = vp9_optimize_b(x, plane, block, tx_size, entropy_ctx) > 0;
  915. }
  916. if (!x->skip_encode && *eob)
  917. vp9_idct32x32_add(dqcoeff, dst, dst_stride, *eob);
  918. break;
  919. case TX_16X16:
  920. if (!x->skip_recode) {
  921. vpx_subtract_block(16, 16, src_diff, diff_stride, src, src_stride, dst,
  922. dst_stride);
  923. vp9_fht16x16(src_diff, coeff, diff_stride, tx_type);
  924. vpx_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round, p->quant,
  925. p->quant_shift, qcoeff, dqcoeff, pd->dequant, eob,
  926. scan_order->scan, scan_order->iscan);
  927. }
  928. if (args->enable_coeff_opt && !x->skip_recode) {
  929. *a = *l = vp9_optimize_b(x, plane, block, tx_size, entropy_ctx) > 0;
  930. }
  931. if (!x->skip_encode && *eob)
  932. vp9_iht16x16_add(tx_type, dqcoeff, dst, dst_stride, *eob);
  933. break;
  934. case TX_8X8:
  935. if (!x->skip_recode) {
  936. vpx_subtract_block(8, 8, src_diff, diff_stride, src, src_stride, dst,
  937. dst_stride);
  938. vp9_fht8x8(src_diff, coeff, diff_stride, tx_type);
  939. vpx_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round, p->quant,
  940. p->quant_shift, qcoeff, dqcoeff, pd->dequant, eob,
  941. scan_order->scan, scan_order->iscan);
  942. }
  943. if (args->enable_coeff_opt && !x->skip_recode) {
  944. *a = *l = vp9_optimize_b(x, plane, block, tx_size, entropy_ctx) > 0;
  945. }
  946. if (!x->skip_encode && *eob)
  947. vp9_iht8x8_add(tx_type, dqcoeff, dst, dst_stride, *eob);
  948. break;
  949. default:
  950. assert(tx_size == TX_4X4);
  951. if (!x->skip_recode) {
  952. vpx_subtract_block(4, 4, src_diff, diff_stride, src, src_stride, dst,
  953. dst_stride);
  954. if (tx_type != DCT_DCT)
  955. vp9_fht4x4(src_diff, coeff, diff_stride, tx_type);
  956. else
  957. x->fwd_txfm4x4(src_diff, coeff, diff_stride);
  958. vpx_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round, p->quant,
  959. p->quant_shift, qcoeff, dqcoeff, pd->dequant, eob,
  960. scan_order->scan, scan_order->iscan);
  961. }
  962. if (args->enable_coeff_opt && !x->skip_recode) {
  963. *a = *l = vp9_optimize_b(x, plane, block, tx_size, entropy_ctx) > 0;
  964. }
  965. if (!x->skip_encode && *eob) {
  966. if (tx_type == DCT_DCT)
  967. // this is like vp9_short_idct4x4 but has a special case around eob<=1
  968. // which is significant (not just an optimization) for the lossless
  969. // case.
  970. x->inv_txfm_add(dqcoeff, dst, dst_stride, *eob);
  971. else
  972. vp9_iht4x4_16_add(dqcoeff, dst, dst_stride, tx_type);
  973. }
  974. break;
  975. }
  976. if (*eob) *(args->skip) = 0;
  977. }
  978. void vp9_encode_intra_block_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane,
  979. int enable_optimize_b) {
  980. const MACROBLOCKD *const xd = &x->e_mbd;
  981. struct optimize_ctx ctx;
  982. #if CONFIG_MISMATCH_DEBUG
  983. // TODO(angiebird): make mismatch_debug support intra mode
  984. struct encode_b_args arg = {
  985. x, enable_optimize_b, ctx.ta[plane], ctx.tl[plane], &xd->mi[0]->skip, 0, 0,
  986. 0
  987. };
  988. #else
  989. struct encode_b_args arg = { x, enable_optimize_b, ctx.ta[plane],
  990. ctx.tl[plane], &xd->mi[0]->skip };
  991. #endif
  992. if (enable_optimize_b && x->optimize &&
  993. (!x->skip_recode || !x->skip_optimize)) {
  994. const struct macroblockd_plane *const pd = &xd->plane[plane];
  995. const TX_SIZE tx_size =
  996. plane ? get_uv_tx_size(xd->mi[0], pd) : xd->mi[0]->tx_size;
  997. vp9_get_entropy_contexts(bsize, tx_size, pd, ctx.ta[plane], ctx.tl[plane]);
  998. } else {
  999. arg.enable_coeff_opt = 0;
  1000. }
  1001. vp9_foreach_transformed_block_in_plane(xd, bsize, plane,
  1002. vp9_encode_block_intra, &arg);
  1003. }