vp9_encodemb.c 39 KB

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