LCOV - code coverage report
Current view: top level - third_party/aom/av1/encoder - encodemb.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 430 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 17 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
       3             :  *
       4             :  * This source code is subject to the terms of the BSD 2 Clause License and
       5             :  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
       6             :  * was not distributed with this source code in the LICENSE file, you can
       7             :  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
       8             :  * Media Patent License 1.0 was not distributed with this source code in the
       9             :  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
      10             :  */
      11             : 
      12             : #include "./av1_rtcd.h"
      13             : #include "./aom_config.h"
      14             : #include "./aom_dsp_rtcd.h"
      15             : 
      16             : #include "aom_dsp/bitwriter.h"
      17             : #include "aom_dsp/quantize.h"
      18             : #include "aom_mem/aom_mem.h"
      19             : #include "aom_ports/mem.h"
      20             : 
      21             : #include "av1/common/idct.h"
      22             : #include "av1/common/reconinter.h"
      23             : #include "av1/common/reconintra.h"
      24             : #include "av1/common/scan.h"
      25             : 
      26             : #include "av1/encoder/av1_quantize.h"
      27             : #include "av1/encoder/encodemb.h"
      28             : #if CONFIG_LV_MAP
      29             : #include "av1/encoder/encodetxb.h"
      30             : #endif
      31             : #include "av1/encoder/hybrid_fwd_txfm.h"
      32             : #include "av1/encoder/rd.h"
      33             : #include "av1/encoder/tokenize.h"
      34             : 
      35             : #if CONFIG_PVQ
      36             : #include "av1/encoder/encint.h"
      37             : #include "av1/common/partition.h"
      38             : #include "av1/encoder/pvq_encoder.h"
      39             : #endif
      40             : 
      41             : #if CONFIG_CFL
      42             : #include "av1/common/cfl.h"
      43             : #endif
      44             : 
      45             : // Check if one needs to use c version subtraction.
      46           0 : static int check_subtract_block_size(int w, int h) { return w < 4 || h < 4; }
      47             : 
      48           0 : static void subtract_block(const MACROBLOCKD *xd, int rows, int cols,
      49             :                            int16_t *diff, ptrdiff_t diff_stride,
      50             :                            const uint8_t *src8, ptrdiff_t src_stride,
      51             :                            const uint8_t *pred8, ptrdiff_t pred_stride) {
      52             : #if !CONFIG_HIGHBITDEPTH
      53             :   (void)xd;
      54             : #endif
      55             : 
      56           0 :   if (check_subtract_block_size(rows, cols)) {
      57             : #if CONFIG_HIGHBITDEPTH
      58           0 :     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
      59           0 :       aom_highbd_subtract_block_c(rows, cols, diff, diff_stride, src8,
      60             :                                   src_stride, pred8, pred_stride, xd->bd);
      61           0 :       return;
      62             :     }
      63             : #endif  // CONFIG_HIGHBITDEPTH
      64           0 :     aom_subtract_block_c(rows, cols, diff, diff_stride, src8, src_stride, pred8,
      65             :                          pred_stride);
      66             : 
      67           0 :     return;
      68             :   }
      69             : 
      70             : #if CONFIG_HIGHBITDEPTH
      71           0 :   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
      72           0 :     aom_highbd_subtract_block(rows, cols, diff, diff_stride, src8, src_stride,
      73             :                               pred8, pred_stride, xd->bd);
      74           0 :     return;
      75             :   }
      76             : #endif  // CONFIG_HIGHBITDEPTH
      77           0 :   aom_subtract_block(rows, cols, diff, diff_stride, src8, src_stride, pred8,
      78             :                      pred_stride);
      79             : }
      80             : 
      81           0 : void av1_subtract_txb(MACROBLOCK *x, int plane, BLOCK_SIZE plane_bsize,
      82             :                       int blk_col, int blk_row, TX_SIZE tx_size) {
      83           0 :   MACROBLOCKD *const xd = &x->e_mbd;
      84           0 :   struct macroblock_plane *const p = &x->plane[plane];
      85           0 :   const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane];
      86           0 :   const int diff_stride = block_size_wide[plane_bsize];
      87           0 :   const int src_stride = p->src.stride;
      88           0 :   const int dst_stride = pd->dst.stride;
      89           0 :   const int tx1d_width = tx_size_wide[tx_size];
      90           0 :   const int tx1d_height = tx_size_high[tx_size];
      91           0 :   uint8_t *dst =
      92           0 :       &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
      93           0 :   uint8_t *src =
      94           0 :       &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]];
      95           0 :   int16_t *src_diff =
      96           0 :       &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
      97           0 :   subtract_block(xd, tx1d_height, tx1d_width, src_diff, diff_stride, src,
      98             :                  src_stride, dst, dst_stride);
      99           0 : }
     100             : 
     101           0 : void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
     102           0 :   struct macroblock_plane *const p = &x->plane[plane];
     103           0 :   const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane];
     104           0 :   const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
     105           0 :   const int bw = block_size_wide[plane_bsize];
     106           0 :   const int bh = block_size_high[plane_bsize];
     107           0 :   const MACROBLOCKD *xd = &x->e_mbd;
     108             : 
     109           0 :   subtract_block(xd, bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
     110           0 :                  pd->dst.buf, pd->dst.stride);
     111           0 : }
     112             : 
     113             : // These numbers are empirically obtained.
     114             : static const int plane_rd_mult[REF_TYPES][PLANE_TYPES] = {
     115             : #if CONFIG_EC_ADAPT
     116             :   { 10, 7 }, { 8, 5 },
     117             : #else
     118             :   { 10, 6 }, { 8, 6 },
     119             : #endif
     120             : };
     121             : 
     122             : #define UPDATE_RD_COST()                             \
     123             :   {                                                  \
     124             :     rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0); \
     125             :     rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1); \
     126             :   }
     127             : 
     128           0 : static INLINE unsigned int get_token_bit_costs(
     129             :     unsigned int token_costs[2][COEFF_CONTEXTS][ENTROPY_TOKENS], int skip_eob,
     130             :     int ctx, int token) {
     131             :   (void)skip_eob;
     132           0 :   return token_costs[token == ZERO_TOKEN || token == EOB_TOKEN][ctx][token];
     133             : }
     134             : 
     135             : #if !CONFIG_LV_MAP
     136             : #define USE_GREEDY_OPTIMIZE_B 0
     137             : 
     138             : #if USE_GREEDY_OPTIMIZE_B
     139             : 
     140             : typedef struct av1_token_state_greedy {
     141             :   int16_t token;
     142             :   tran_low_t qc;
     143             :   tran_low_t dqc;
     144             : } av1_token_state_greedy;
     145             : 
     146             : static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
     147             :                              int block, TX_SIZE tx_size, int ctx) {
     148             :   MACROBLOCKD *const xd = &mb->e_mbd;
     149             :   struct macroblock_plane *const p = &mb->plane[plane];
     150             :   struct macroblockd_plane *const pd = &xd->plane[plane];
     151             :   const int ref = is_inter_block(&xd->mi[0]->mbmi);
     152             :   av1_token_state_greedy tokens[MAX_TX_SQUARE + 1][2];
     153             :   uint8_t token_cache[MAX_TX_SQUARE];
     154             :   const tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
     155             :   tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
     156             :   tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
     157             :   const int eob = p->eobs[block];
     158             :   const PLANE_TYPE plane_type = pd->plane_type;
     159             :   const int16_t *const dequant_ptr = pd->dequant;
     160             :   const uint8_t *const band_translate = get_band_translate(tx_size);
     161             :   TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
     162             :   const SCAN_ORDER *const scan_order =
     163             :       get_scan(cm, tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi));
     164             :   const int16_t *const scan = scan_order->scan;
     165             :   const int16_t *const nb = scan_order->neighbors;
     166             :   int dqv;
     167             :   const int shift = av1_get_tx_scale(tx_size);
     168             : #if CONFIG_AOM_QM
     169             :   int seg_id = xd->mi[0]->mbmi.segment_id;
     170             :   const qm_val_t *iqmatrix = pd->seg_iqmatrix[seg_id][!ref][tx_size];
     171             : #endif
     172             : #if CONFIG_NEW_QUANT
     173             :   int dq = get_dq_profile_from_ctx(mb->qindex, ctx, ref, plane_type);
     174             :   const dequant_val_type_nuq *dequant_val = pd->dequant_val_nuq[dq];
     175             : #endif  // CONFIG_NEW_QUANT
     176             :   int sz = 0;
     177             :   const int64_t rddiv = mb->rddiv;
     178             :   int64_t rd_cost0, rd_cost1;
     179             :   int16_t t0, t1;
     180             :   int i, final_eob;
     181             :   const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, xd->bd);
     182             :   unsigned int(*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
     183             :       mb->token_costs[txsize_sqr_map[tx_size]][plane_type][ref];
     184             :   const int default_eob = tx_size_2d[tx_size];
     185             : 
     186             :   assert(mb->qindex > 0);
     187             : 
     188             :   assert((!plane_type && !plane) || (plane_type && plane));
     189             :   assert(eob <= default_eob);
     190             : 
     191             :   int64_t rdmult = (mb->rdmult * plane_rd_mult[ref][plane_type]) >> 1;
     192             : 
     193             :   int64_t rate0, rate1;
     194             :   for (i = 0; i < eob; i++) {
     195             :     const int rc = scan[i];
     196             :     int x = qcoeff[rc];
     197             :     t0 = av1_get_token(x);
     198             : 
     199             :     tokens[i][0].qc = x;
     200             :     tokens[i][0].token = t0;
     201             :     tokens[i][0].dqc = dqcoeff[rc];
     202             : 
     203             :     token_cache[rc] = av1_pt_energy_class[t0];
     204             :   }
     205             :   tokens[eob][0].token = EOB_TOKEN;
     206             :   tokens[eob][0].qc = 0;
     207             :   tokens[eob][0].dqc = 0;
     208             :   tokens[eob][1] = tokens[eob][0];
     209             : 
     210             :   unsigned int(*token_costs_ptr)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
     211             :       token_costs;
     212             : 
     213             :   final_eob = 0;
     214             : 
     215             :   int64_t eob_cost0, eob_cost1;
     216             : 
     217             :   const int ctx0 = ctx;
     218             :   /* Record the r-d cost */
     219             :   int64_t accu_rate = 0;
     220             :   int64_t accu_error = 0;
     221             : 
     222             :   rate0 = get_token_bit_costs(*(token_costs_ptr + band_translate[0]), 0, ctx0,
     223             :                               EOB_TOKEN);
     224             :   int64_t best_block_rd_cost = RDCOST(rdmult, rddiv, rate0, accu_error);
     225             : 
     226             :   // int64_t best_block_rd_cost_all0 = best_block_rd_cost;
     227             : 
     228             :   int x_prev = 1;
     229             : 
     230             :   for (i = 0; i < eob; i++) {
     231             :     const int rc = scan[i];
     232             :     int x = qcoeff[rc];
     233             :     sz = -(x < 0);
     234             : 
     235             :     int band_cur = band_translate[i];
     236             :     int ctx_cur = (i == 0) ? ctx : get_coef_context(nb, token_cache, i);
     237             :     int token_tree_sel_cur = (x_prev == 0);
     238             : 
     239             :     if (x == 0) {
     240             :       // no need to search when x == 0
     241             :       rate0 =
     242             :           get_token_bit_costs(*(token_costs_ptr + band_cur), token_tree_sel_cur,
     243             :                               ctx_cur, tokens[i][0].token);
     244             :       accu_rate += rate0;
     245             :       x_prev = 0;
     246             :       // accu_error does not change when x==0
     247             :     } else {
     248             :       /*  Computing distortion
     249             :        */
     250             :       // compute the distortion for the first candidate
     251             :       // and the distortion for quantizing to 0.
     252             :       int dx0 = (-coeff[rc]) * (1 << shift);
     253             : #if CONFIG_HIGHBITDEPTH
     254             :       if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     255             :         dx0 >>= xd->bd - 8;
     256             :       }
     257             : #endif
     258             :       int64_t d0 = (int64_t)dx0 * dx0;
     259             : 
     260             :       int x_a = x - 2 * sz - 1;
     261             :       int64_t d2, d2_a;
     262             : 
     263             :       int dx;
     264             : 
     265             : #if CONFIG_AOM_QM
     266             :       int iwt = iqmatrix[rc];
     267             :       dqv = dequant_ptr[rc != 0];
     268             :       dqv = ((iwt * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
     269             : #else
     270             :       dqv = dequant_ptr[rc != 0];
     271             : #endif
     272             : 
     273             :       dx = (dqcoeff[rc] - coeff[rc]) * (1 << shift);
     274             : #if CONFIG_HIGHBITDEPTH
     275             :       if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     276             :         dx >>= xd->bd - 8;
     277             :       }
     278             : #endif  // CONFIG_HIGHBITDEPTH
     279             :       d2 = (int64_t)dx * dx;
     280             : 
     281             :       /* compute the distortion for the second candidate
     282             :        * x_a = x - 2 * sz + 1;
     283             :        */
     284             :       if (x_a != 0) {
     285             : #if CONFIG_NEW_QUANT
     286             :         dx = av1_dequant_coeff_nuq(x, dqv, dequant_val[band_translate[i]]) -
     287             :              (coeff[rc] << shift);
     288             : #if CONFIG_HIGHBITDEPTH
     289             :         if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     290             :           dx >>= xd->bd - 8;
     291             :         }
     292             : #endif  // CONFIG_HIGHBITDEPTH
     293             : #else   // CONFIG_NEW_QUANT
     294             : #if CONFIG_HIGHBITDEPTH
     295             :         if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     296             :           dx -= ((dqv >> (xd->bd - 8)) + sz) ^ sz;
     297             :         } else {
     298             :           dx -= (dqv + sz) ^ sz;
     299             :         }
     300             : #else
     301             :         dx -= (dqv + sz) ^ sz;
     302             : #endif  // CONFIG_HIGHBITDEPTH
     303             : #endif  // CONFIG_NEW_QUANT
     304             :         d2_a = (int64_t)dx * dx;
     305             :       } else {
     306             :         d2_a = d0;
     307             :       }
     308             :       /*  Computing rates and r-d cost
     309             :        */
     310             : 
     311             :       int best_x, best_eob_x;
     312             :       int64_t base_bits, next_bits0, next_bits1;
     313             :       int64_t next_eob_bits0, next_eob_bits1;
     314             : 
     315             :       // rate cost of x
     316             :       base_bits = av1_get_token_cost(x, &t0, cat6_bits);
     317             :       rate0 = base_bits + get_token_bit_costs(*(token_costs_ptr + band_cur),
     318             :                                               token_tree_sel_cur, ctx_cur, t0);
     319             : 
     320             :       base_bits = av1_get_token_cost(x_a, &t1, cat6_bits);
     321             :       rate1 = base_bits + get_token_bit_costs(*(token_costs_ptr + band_cur),
     322             :                                               token_tree_sel_cur, ctx_cur, t1);
     323             : 
     324             :       next_bits0 = 0;
     325             :       next_bits1 = 0;
     326             :       next_eob_bits0 = 0;
     327             :       next_eob_bits1 = 0;
     328             : 
     329             :       if (i < default_eob - 1) {
     330             :         int ctx_next, token_tree_sel_next;
     331             :         int band_next = band_translate[i + 1];
     332             : 
     333             :         token_cache[rc] = av1_pt_energy_class[t0];
     334             :         ctx_next = get_coef_context(nb, token_cache, i + 1);
     335             :         token_tree_sel_next = (x == 0);
     336             : 
     337             :         next_bits0 = get_token_bit_costs(*(token_costs_ptr + band_next),
     338             :                                          token_tree_sel_next, ctx_next,
     339             :                                          tokens[i + 1][0].token);
     340             :         next_eob_bits0 =
     341             :             get_token_bit_costs(*(token_costs_ptr + band_next),
     342             :                                 token_tree_sel_next, ctx_next, EOB_TOKEN);
     343             : 
     344             :         token_cache[rc] = av1_pt_energy_class[t1];
     345             :         ctx_next = get_coef_context(nb, token_cache, i + 1);
     346             :         token_tree_sel_next = (x_a == 0);
     347             : 
     348             :         next_bits1 = get_token_bit_costs(*(token_costs_ptr + band_next),
     349             :                                          token_tree_sel_next, ctx_next,
     350             :                                          tokens[i + 1][0].token);
     351             : 
     352             :         if (x_a != 0) {
     353             :           next_eob_bits1 =
     354             :               get_token_bit_costs(*(token_costs_ptr + band_next),
     355             :                                   token_tree_sel_next, ctx_next, EOB_TOKEN);
     356             :         }
     357             :       }
     358             : 
     359             :       rd_cost0 = RDCOST(rdmult, rddiv, (rate0 + next_bits0), d2);
     360             :       rd_cost1 = RDCOST(rdmult, rddiv, (rate1 + next_bits1), d2_a);
     361             : 
     362             :       best_x = (rd_cost1 < rd_cost0);
     363             : 
     364             :       eob_cost0 = RDCOST(rdmult, rddiv, (accu_rate + rate0 + next_eob_bits0),
     365             :                          (accu_error + d2 - d0));
     366             :       eob_cost1 = eob_cost0;
     367             :       if (x_a != 0) {
     368             :         eob_cost1 = RDCOST(rdmult, rddiv, (accu_rate + rate1 + next_eob_bits1),
     369             :                            (accu_error + d2_a - d0));
     370             :         best_eob_x = (eob_cost1 < eob_cost0);
     371             :       } else {
     372             :         best_eob_x = 0;
     373             :       }
     374             : 
     375             :       int dqc, dqc_a = 0;
     376             : 
     377             :       dqc = dqcoeff[rc];
     378             :       if (best_x + best_eob_x) {
     379             :         if (x_a != 0) {
     380             : #if CONFIG_NEW_QUANT
     381             :           dqc_a = av1_dequant_abscoeff_nuq(abs(x_a), dqv,
     382             :                                            dequant_val[band_translate[i]]);
     383             :           dqc_a = shift ? ROUND_POWER_OF_TWO(dqc_a, shift) : dqc_a;
     384             :           if (sz) dqc_a = -dqc_a;
     385             : #else
     386             :           if (x_a < 0)
     387             :             dqc_a = -((-x_a * dqv) >> shift);
     388             :           else
     389             :             dqc_a = (x_a * dqv) >> shift;
     390             : #endif  // CONFIG_NEW_QUANT
     391             :         } else {
     392             :           dqc_a = 0;
     393             :         }  // if (x_a != 0)
     394             :       }
     395             : 
     396             :       // record the better quantized value
     397             :       if (best_x) {
     398             :         qcoeff[rc] = x_a;
     399             :         dqcoeff[rc] = dqc_a;
     400             : 
     401             :         accu_rate += rate1;
     402             :         accu_error += d2_a - d0;
     403             :         assert(d2_a <= d0);
     404             : 
     405             :         token_cache[rc] = av1_pt_energy_class[t1];
     406             :       } else {
     407             :         accu_rate += rate0;
     408             :         accu_error += d2 - d0;
     409             :         assert(d2 <= d0);
     410             : 
     411             :         token_cache[rc] = av1_pt_energy_class[t0];
     412             :       }
     413             : 
     414             :       x_prev = qcoeff[rc];
     415             : 
     416             :       // determine whether to move the eob position to i+1
     417             :       int64_t best_eob_cost_i = eob_cost0;
     418             : 
     419             :       tokens[i][1].token = t0;
     420             :       tokens[i][1].qc = x;
     421             :       tokens[i][1].dqc = dqc;
     422             : 
     423             :       if ((x_a != 0) && (best_eob_x)) {
     424             :         best_eob_cost_i = eob_cost1;
     425             : 
     426             :         tokens[i][1].token = t1;
     427             :         tokens[i][1].qc = x_a;
     428             :         tokens[i][1].dqc = dqc_a;
     429             :       }
     430             : 
     431             :       if (best_eob_cost_i < best_block_rd_cost) {
     432             :         best_block_rd_cost = best_eob_cost_i;
     433             :         final_eob = i + 1;
     434             :       }
     435             :     }  // if (x==0)
     436             :   }    // for (i)
     437             : 
     438             :   assert(final_eob <= eob);
     439             :   if (final_eob > 0) {
     440             :     assert(tokens[final_eob - 1][1].qc != 0);
     441             :     i = final_eob - 1;
     442             :     int rc = scan[i];
     443             :     qcoeff[rc] = tokens[i][1].qc;
     444             :     dqcoeff[rc] = tokens[i][1].dqc;
     445             :   }
     446             : 
     447             :   for (i = final_eob; i < eob; i++) {
     448             :     int rc = scan[i];
     449             :     qcoeff[rc] = 0;
     450             :     dqcoeff[rc] = 0;
     451             :   }
     452             : 
     453             :   mb->plane[plane].eobs[block] = final_eob;
     454             :   return final_eob;
     455             : }
     456             : 
     457             : #else  // USE_GREEDY_OPTIMIZE_B
     458             : 
     459             : typedef struct av1_token_state_org {
     460             :   int64_t error;
     461             :   int rate;
     462             :   int16_t next;
     463             :   int16_t token;
     464             :   tran_low_t qc;
     465             :   tran_low_t dqc;
     466             :   uint8_t best_index;
     467             : } av1_token_state_org;
     468             : 
     469           0 : static int optimize_b_org(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
     470             :                           int block, TX_SIZE tx_size, int ctx) {
     471           0 :   MACROBLOCKD *const xd = &mb->e_mbd;
     472           0 :   struct macroblock_plane *const p = &mb->plane[plane];
     473           0 :   struct macroblockd_plane *const pd = &xd->plane[plane];
     474           0 :   const int ref = is_inter_block(&xd->mi[0]->mbmi);
     475             :   av1_token_state_org tokens[MAX_TX_SQUARE + 1][2];
     476             :   uint8_t token_cache[MAX_TX_SQUARE];
     477           0 :   const tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
     478           0 :   tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
     479           0 :   tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
     480           0 :   const int eob = p->eobs[block];
     481           0 :   const PLANE_TYPE plane_type = pd->plane_type;
     482           0 :   const int default_eob = tx_size_2d[tx_size];
     483           0 :   const int16_t *const dequant_ptr = pd->dequant;
     484           0 :   const uint8_t *const band_translate = get_band_translate(tx_size);
     485           0 :   TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
     486           0 :   const SCAN_ORDER *const scan_order =
     487           0 :       get_scan(cm, tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi));
     488           0 :   const int16_t *const scan = scan_order->scan;
     489           0 :   const int16_t *const nb = scan_order->neighbors;
     490             :   int dqv;
     491           0 :   const int shift = av1_get_tx_scale(tx_size);
     492             : #if CONFIG_AOM_QM
     493             :   int seg_id = xd->mi[0]->mbmi.segment_id;
     494             :   const qm_val_t *iqmatrix = pd->seg_iqmatrix[seg_id][!ref][tx_size];
     495             : #endif
     496             : #if CONFIG_NEW_QUANT
     497             :   int dq = get_dq_profile_from_ctx(mb->qindex, ctx, ref, plane_type);
     498             :   const dequant_val_type_nuq *dequant_val = pd->dequant_val_nuq[dq];
     499             : #endif  // CONFIG_NEW_QUANT
     500           0 :   int next = eob, sz = 0;
     501           0 :   const int64_t rdmult = (mb->rdmult * plane_rd_mult[ref][plane_type]) >> 1;
     502           0 :   const int64_t rddiv = mb->rddiv;
     503             :   int64_t rd_cost0, rd_cost1;
     504             :   int rate0, rate1;
     505             :   int64_t error0, error1;
     506             :   int16_t t0, t1;
     507           0 :   int best, band = (eob < default_eob) ? band_translate[eob]
     508           0 :                                        : band_translate[eob - 1];
     509             :   int pt, i, final_eob;
     510           0 :   const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, xd->bd);
     511           0 :   unsigned int(*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
     512           0 :       mb->token_costs[txsize_sqr_map[tx_size]][plane_type][ref];
     513           0 :   const uint16_t *band_counts = &band_count_table[tx_size][band];
     514           0 :   uint16_t band_left = eob - band_cum_count_table[tx_size][band] + 1;
     515           0 :   int shortcut = 0;
     516           0 :   int next_shortcut = 0;
     517             : 
     518             : #if CONFIG_EXT_DELTA_Q
     519           0 :   const int qindex = cm->seg.enabled
     520           0 :                          ? av1_get_qindex(&cm->seg, xd->mi[0]->mbmi.segment_id,
     521             :                                           cm->base_qindex)
     522           0 :                          : cm->base_qindex;
     523           0 :   assert(qindex > 0);
     524             :   (void)qindex;
     525             : #else
     526             :   assert(mb->qindex > 0);
     527             : #endif
     528             : 
     529           0 :   token_costs += band;
     530             : 
     531           0 :   assert((!plane_type && !plane) || (plane_type && plane));
     532           0 :   assert(eob <= default_eob);
     533             : 
     534             :   /* Now set up a Viterbi trellis to evaluate alternative roundings. */
     535             :   /* Initialize the sentinel node of the trellis. */
     536           0 :   tokens[eob][0].rate = 0;
     537           0 :   tokens[eob][0].error = 0;
     538           0 :   tokens[eob][0].next = default_eob;
     539           0 :   tokens[eob][0].token = EOB_TOKEN;
     540           0 :   tokens[eob][0].qc = 0;
     541           0 :   tokens[eob][1] = tokens[eob][0];
     542             : 
     543           0 :   for (i = 0; i < eob; i++) {
     544           0 :     const int rc = scan[i];
     545           0 :     tokens[i][0].rate = av1_get_token_cost(qcoeff[rc], &t0, cat6_bits);
     546           0 :     tokens[i][0].token = t0;
     547           0 :     token_cache[rc] = av1_pt_energy_class[t0];
     548             :   }
     549             : 
     550           0 :   for (i = eob; i-- > 0;) {
     551             :     int base_bits, dx;
     552             :     int64_t d2;
     553           0 :     const int rc = scan[i];
     554           0 :     int x = qcoeff[rc];
     555             : #if CONFIG_AOM_QM
     556             :     int iwt = iqmatrix[rc];
     557             :     dqv = dequant_ptr[rc != 0];
     558             :     dqv = ((iwt * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
     559             : #else
     560           0 :     dqv = dequant_ptr[rc != 0];
     561             : #endif
     562           0 :     next_shortcut = shortcut;
     563             : 
     564             :     /* Only add a trellis state for non-zero coefficients. */
     565           0 :     if (UNLIKELY(x)) {
     566           0 :       error0 = tokens[next][0].error;
     567           0 :       error1 = tokens[next][1].error;
     568             :       /* Evaluate the first possibility for this state. */
     569           0 :       rate0 = tokens[next][0].rate;
     570           0 :       rate1 = tokens[next][1].rate;
     571             : 
     572           0 :       if (next_shortcut) {
     573             :         /* Consider both possible successor states. */
     574           0 :         if (next < default_eob) {
     575           0 :           pt = get_coef_context(nb, token_cache, i + 1);
     576           0 :           rate0 +=
     577           0 :               get_token_bit_costs(*token_costs, 0, pt, tokens[next][0].token);
     578           0 :           rate1 +=
     579           0 :               get_token_bit_costs(*token_costs, 0, pt, tokens[next][1].token);
     580             :         }
     581           0 :         UPDATE_RD_COST();
     582             :         /* And pick the best. */
     583           0 :         best = rd_cost1 < rd_cost0;
     584             :       } else {
     585           0 :         if (next < default_eob) {
     586           0 :           pt = get_coef_context(nb, token_cache, i + 1);
     587           0 :           rate0 +=
     588           0 :               get_token_bit_costs(*token_costs, 0, pt, tokens[next][0].token);
     589             :         }
     590           0 :         best = 0;
     591             :       }
     592             : 
     593           0 :       dx = (dqcoeff[rc] - coeff[rc]) * (1 << shift);
     594             : #if CONFIG_HIGHBITDEPTH
     595           0 :       if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     596           0 :         dx >>= xd->bd - 8;
     597             :       }
     598             : #endif  // CONFIG_HIGHBITDEPTH
     599           0 :       d2 = (int64_t)dx * dx;
     600           0 :       tokens[i][0].rate += (best ? rate1 : rate0);
     601           0 :       tokens[i][0].error = d2 + (best ? error1 : error0);
     602           0 :       tokens[i][0].next = next;
     603           0 :       tokens[i][0].qc = x;
     604           0 :       tokens[i][0].dqc = dqcoeff[rc];
     605           0 :       tokens[i][0].best_index = best;
     606             : 
     607             :       /* Evaluate the second possibility for this state. */
     608           0 :       rate0 = tokens[next][0].rate;
     609           0 :       rate1 = tokens[next][1].rate;
     610             : 
     611             :       // The threshold of 3 is empirically obtained.
     612           0 :       if (UNLIKELY(abs(x) > 3)) {
     613           0 :         shortcut = 0;
     614             :       } else {
     615             : #if CONFIG_NEW_QUANT
     616             :         shortcut = ((av1_dequant_abscoeff_nuq(abs(x), dqv,
     617             :                                               dequant_val[band_translate[i]]) >
     618             :                      (abs(coeff[rc]) << shift)) &&
     619             :                     (av1_dequant_abscoeff_nuq(abs(x) - 1, dqv,
     620             :                                               dequant_val[band_translate[i]]) <
     621             :                      (abs(coeff[rc]) << shift)));
     622             : #else  // CONFIG_NEW_QUANT
     623             : #if CONFIG_AOM_QM
     624             :         if ((abs(x) * dequant_ptr[rc != 0] * iwt >
     625             :              ((abs(coeff[rc]) << shift) << AOM_QM_BITS)) &&
     626             :             (abs(x) * dequant_ptr[rc != 0] * iwt <
     627             :              (((abs(coeff[rc]) << shift) + dequant_ptr[rc != 0])
     628             :               << AOM_QM_BITS)))
     629             : #else
     630           0 :         if ((abs(x) * dequant_ptr[rc != 0] > (abs(coeff[rc]) << shift)) &&
     631           0 :             (abs(x) * dequant_ptr[rc != 0] <
     632           0 :              (abs(coeff[rc]) << shift) + dequant_ptr[rc != 0]))
     633             : #endif  // CONFIG_AOM_QM
     634           0 :           shortcut = 1;
     635             :         else
     636           0 :           shortcut = 0;
     637             : #endif  // CONFIG_NEW_QUANT
     638             :       }
     639             : 
     640           0 :       if (shortcut) {
     641           0 :         sz = -(x < 0);
     642           0 :         x -= 2 * sz + 1;
     643             :       } else {
     644           0 :         tokens[i][1] = tokens[i][0];
     645           0 :         next = i;
     646             : 
     647           0 :         if (UNLIKELY(!(--band_left))) {
     648           0 :           --band_counts;
     649           0 :           band_left = *band_counts;
     650           0 :           --token_costs;
     651             :         }
     652           0 :         continue;
     653             :       }
     654             : 
     655             :       /* Consider both possible successor states. */
     656           0 :       if (!x) {
     657             :         /* If we reduced this coefficient to zero, check to see if
     658             :          *  we need to move the EOB back here.
     659             :          */
     660           0 :         t0 = tokens[next][0].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
     661           0 :         t1 = tokens[next][1].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
     662           0 :         base_bits = 0;
     663             :       } else {
     664           0 :         base_bits = av1_get_token_cost(x, &t0, cat6_bits);
     665           0 :         t1 = t0;
     666             :       }
     667             : 
     668           0 :       if (next_shortcut) {
     669           0 :         if (LIKELY(next < default_eob)) {
     670           0 :           if (t0 != EOB_TOKEN) {
     671           0 :             token_cache[rc] = av1_pt_energy_class[t0];
     672           0 :             pt = get_coef_context(nb, token_cache, i + 1);
     673           0 :             rate0 += get_token_bit_costs(*token_costs, !x, pt,
     674           0 :                                          tokens[next][0].token);
     675             :           }
     676           0 :           if (t1 != EOB_TOKEN) {
     677           0 :             token_cache[rc] = av1_pt_energy_class[t1];
     678           0 :             pt = get_coef_context(nb, token_cache, i + 1);
     679           0 :             rate1 += get_token_bit_costs(*token_costs, !x, pt,
     680           0 :                                          tokens[next][1].token);
     681             :           }
     682             :         }
     683             : 
     684           0 :         UPDATE_RD_COST();
     685             :         /* And pick the best. */
     686           0 :         best = rd_cost1 < rd_cost0;
     687             :       } else {
     688             :         // The two states in next stage are identical.
     689           0 :         if (next < default_eob && t0 != EOB_TOKEN) {
     690           0 :           token_cache[rc] = av1_pt_energy_class[t0];
     691           0 :           pt = get_coef_context(nb, token_cache, i + 1);
     692           0 :           rate0 +=
     693           0 :               get_token_bit_costs(*token_costs, !x, pt, tokens[next][0].token);
     694             :         }
     695           0 :         best = 0;
     696             :       }
     697             : 
     698             : #if CONFIG_NEW_QUANT
     699             :       dx = av1_dequant_coeff_nuq(x, dqv, dequant_val[band_translate[i]]) -
     700             :            (coeff[rc] << shift);
     701             : #if CONFIG_HIGHBITDEPTH
     702             :       if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     703             :         dx >>= xd->bd - 8;
     704             :       }
     705             : #endif  // CONFIG_HIGHBITDEPTH
     706             : #else   // CONFIG_NEW_QUANT
     707             : #if CONFIG_HIGHBITDEPTH
     708           0 :       if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     709           0 :         dx -= ((dqv >> (xd->bd - 8)) + sz) ^ sz;
     710             :       } else {
     711           0 :         dx -= (dqv + sz) ^ sz;
     712             :       }
     713             : #else
     714             :       dx -= (dqv + sz) ^ sz;
     715             : #endif  // CONFIG_HIGHBITDEPTH
     716             : #endif  // CONFIG_NEW_QUANT
     717           0 :       d2 = (int64_t)dx * dx;
     718             : 
     719           0 :       tokens[i][1].rate = base_bits + (best ? rate1 : rate0);
     720           0 :       tokens[i][1].error = d2 + (best ? error1 : error0);
     721           0 :       tokens[i][1].next = next;
     722           0 :       tokens[i][1].token = best ? t1 : t0;
     723           0 :       tokens[i][1].qc = x;
     724             : 
     725           0 :       if (x) {
     726             : #if CONFIG_NEW_QUANT
     727             :         tokens[i][1].dqc = av1_dequant_abscoeff_nuq(
     728             :             abs(x), dqv, dequant_val[band_translate[i]]);
     729             :         tokens[i][1].dqc = shift ? ROUND_POWER_OF_TWO(tokens[i][1].dqc, shift)
     730             :                                  : tokens[i][1].dqc;
     731             :         if (sz) tokens[i][1].dqc = -tokens[i][1].dqc;
     732             : #else
     733           0 :         if (x < 0)
     734           0 :           tokens[i][1].dqc = -((-x * dqv) >> shift);
     735             :         else
     736           0 :           tokens[i][1].dqc = (x * dqv) >> shift;
     737             : #endif  // CONFIG_NEW_QUANT
     738             :       } else {
     739           0 :         tokens[i][1].dqc = 0;
     740             :       }
     741             : 
     742           0 :       tokens[i][1].best_index = best;
     743             :       /* Finally, make this the new head of the trellis. */
     744           0 :       next = i;
     745             :     } else {
     746             :       /* There's no choice to make for a zero coefficient, so we don't
     747             :        *  add a new trellis node, but we do need to update the costs.
     748             :        */
     749           0 :       t0 = tokens[next][0].token;
     750           0 :       t1 = tokens[next][1].token;
     751           0 :       pt = get_coef_context(nb, token_cache, i + 1);
     752             :       /* Update the cost of each path if we're past the EOB token. */
     753           0 :       if (t0 != EOB_TOKEN) {
     754           0 :         tokens[next][0].rate += get_token_bit_costs(*token_costs, 1, pt, t0);
     755           0 :         tokens[next][0].token = ZERO_TOKEN;
     756             :       }
     757           0 :       if (t1 != EOB_TOKEN) {
     758           0 :         tokens[next][1].rate += get_token_bit_costs(*token_costs, 1, pt, t1);
     759           0 :         tokens[next][1].token = ZERO_TOKEN;
     760             :       }
     761           0 :       tokens[i][0].best_index = tokens[i][1].best_index = 0;
     762           0 :       shortcut = (tokens[next][0].rate != tokens[next][1].rate);
     763             :       /* Don't update next, because we didn't add a new node. */
     764             :     }
     765             : 
     766           0 :     if (UNLIKELY(!(--band_left))) {
     767           0 :       --band_counts;
     768           0 :       band_left = *band_counts;
     769           0 :       --token_costs;
     770             :     }
     771             :   }
     772             : 
     773             :   /* Now pick the best path through the whole trellis. */
     774           0 :   rate0 = tokens[next][0].rate;
     775           0 :   rate1 = tokens[next][1].rate;
     776           0 :   error0 = tokens[next][0].error;
     777           0 :   error1 = tokens[next][1].error;
     778           0 :   t0 = tokens[next][0].token;
     779           0 :   t1 = tokens[next][1].token;
     780           0 :   rate0 += get_token_bit_costs(*token_costs, 0, ctx, t0);
     781           0 :   rate1 += get_token_bit_costs(*token_costs, 0, ctx, t1);
     782           0 :   UPDATE_RD_COST();
     783           0 :   best = rd_cost1 < rd_cost0;
     784             : 
     785           0 :   final_eob = -1;
     786             : 
     787           0 :   for (i = next; i < eob; i = next) {
     788           0 :     const int x = tokens[i][best].qc;
     789           0 :     const int rc = scan[i];
     790           0 :     if (x) final_eob = i;
     791           0 :     qcoeff[rc] = x;
     792           0 :     dqcoeff[rc] = tokens[i][best].dqc;
     793             : 
     794           0 :     next = tokens[i][best].next;
     795           0 :     best = tokens[i][best].best_index;
     796             :   }
     797           0 :   final_eob++;
     798             : 
     799           0 :   mb->plane[plane].eobs[block] = final_eob;
     800           0 :   assert(final_eob <= default_eob);
     801           0 :   return final_eob;
     802             : }
     803             : 
     804             : #endif  // USE_GREEDY_OPTIMIZE_B
     805             : #endif  // !CONFIG_LV_MAP
     806             : 
     807           0 : int av1_optimize_b(const AV1_COMMON *cm, MACROBLOCK *mb, int plane, int block,
     808             :                    BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
     809             :                    const ENTROPY_CONTEXT *a, const ENTROPY_CONTEXT *l) {
     810           0 :   MACROBLOCKD *const xd = &mb->e_mbd;
     811           0 :   struct macroblock_plane *const p = &mb->plane[plane];
     812           0 :   const int eob = p->eobs[block];
     813           0 :   assert((mb->qindex == 0) ^ (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
     814           0 :   if (eob == 0) return eob;
     815           0 :   if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return eob;
     816             : #if CONFIG_PVQ
     817             :   (void)cm;
     818             :   (void)tx_size;
     819             :   (void)a;
     820             :   (void)l;
     821             :   return eob;
     822             : #endif
     823             : 
     824             : #if !CONFIG_LV_MAP
     825             :   (void)plane_bsize;
     826             : #if CONFIG_VAR_TX
     827           0 :   int ctx = get_entropy_context(tx_size, a, l);
     828             : #else
     829             :   int ctx = combine_entropy_contexts(*a, *l);
     830             : #endif
     831             : 
     832             : #if USE_GREEDY_OPTIMIZE_B
     833             :   return optimize_b_greedy(cm, mb, plane, block, tx_size, ctx);
     834             : #else   // USE_GREEDY_OPTIMIZE_B
     835           0 :   return optimize_b_org(cm, mb, plane, block, tx_size, ctx);
     836             : #endif  // USE_GREEDY_OPTIMIZE_B
     837             : #else   // !CONFIG_LV_MAP
     838             :   TXB_CTX txb_ctx;
     839             :   get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
     840             :   return av1_optimize_txb(cm, mb, plane, block, tx_size, &txb_ctx);
     841             : #endif  // !CONFIG_LV_MAP
     842             : }
     843             : 
     844             : #if !CONFIG_PVQ
     845             : #if CONFIG_HIGHBITDEPTH
     846             : typedef enum QUANT_FUNC {
     847             :   QUANT_FUNC_LOWBD = 0,
     848             :   QUANT_FUNC_HIGHBD = 1,
     849             :   QUANT_FUNC_TYPES = 2
     850             : } QUANT_FUNC;
     851             : 
     852             : static AV1_QUANT_FACADE
     853             :     quant_func_list[AV1_XFORM_QUANT_TYPES][QUANT_FUNC_TYPES] = {
     854             : #if !CONFIG_NEW_QUANT
     855             :       { av1_quantize_fp_facade, av1_highbd_quantize_fp_facade },
     856             :       { av1_quantize_b_facade, av1_highbd_quantize_b_facade },
     857             :       { av1_quantize_dc_facade, av1_highbd_quantize_dc_facade },
     858             : #else   // !CONFIG_NEW_QUANT
     859             :       { av1_quantize_fp_nuq_facade, av1_highbd_quantize_fp_nuq_facade },
     860             :       { av1_quantize_b_nuq_facade, av1_highbd_quantize_b_nuq_facade },
     861             :       { av1_quantize_dc_nuq_facade, av1_highbd_quantize_dc_nuq_facade },
     862             : #endif  // !CONFIG_NEW_QUANT
     863             :       { NULL, NULL }
     864             :     };
     865             : 
     866             : #else
     867             : 
     868             : typedef enum QUANT_FUNC {
     869             :   QUANT_FUNC_LOWBD = 0,
     870             :   QUANT_FUNC_TYPES = 1
     871             : } QUANT_FUNC;
     872             : 
     873             : static AV1_QUANT_FACADE quant_func_list[AV1_XFORM_QUANT_TYPES]
     874             :                                        [QUANT_FUNC_TYPES] = {
     875             : #if !CONFIG_NEW_QUANT
     876             :                                          { av1_quantize_fp_facade },
     877             :                                          { av1_quantize_b_facade },
     878             :                                          { av1_quantize_dc_facade },
     879             : #else   // !CONFIG_NEW_QUANT
     880             :                                          { av1_quantize_fp_nuq_facade },
     881             :                                          { av1_quantize_b_nuq_facade },
     882             :                                          { av1_quantize_dc_nuq_facade },
     883             : #endif  // !CONFIG_NEW_QUANT
     884             :                                          { NULL }
     885             :                                        };
     886             : #endif  // CONFIG_HIGHBITDEPTH
     887             : #endif  // CONFIG_PVQ
     888             : 
     889           0 : void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
     890             :                      int blk_row, int blk_col, BLOCK_SIZE plane_bsize,
     891             :                      TX_SIZE tx_size, int ctx,
     892             :                      AV1_XFORM_QUANT xform_quant_idx) {
     893           0 :   MACROBLOCKD *const xd = &x->e_mbd;
     894           0 :   MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
     895             : #if !(CONFIG_PVQ || CONFIG_DAALA_DIST)
     896           0 :   const struct macroblock_plane *const p = &x->plane[plane];
     897           0 :   const struct macroblockd_plane *const pd = &xd->plane[plane];
     898             : #else
     899             :   struct macroblock_plane *const p = &x->plane[plane];
     900             :   struct macroblockd_plane *const pd = &xd->plane[plane];
     901             : #endif
     902           0 :   PLANE_TYPE plane_type = get_plane_type(plane);
     903           0 :   TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
     904           0 :   const int is_inter = is_inter_block(mbmi);
     905           0 :   const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, is_inter);
     906           0 :   tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
     907           0 :   tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
     908           0 :   tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
     909           0 :   uint16_t *const eob = &p->eobs[block];
     910           0 :   const int diff_stride = block_size_wide[plane_bsize];
     911             : #if CONFIG_AOM_QM
     912             :   int seg_id = mbmi->segment_id;
     913             :   const qm_val_t *qmatrix = pd->seg_qmatrix[seg_id][!is_inter][tx_size];
     914             :   const qm_val_t *iqmatrix = pd->seg_iqmatrix[seg_id][!is_inter][tx_size];
     915             : #endif
     916             : 
     917             :   FWD_TXFM_PARAM fwd_txfm_param;
     918             : 
     919             : #if CONFIG_PVQ || CONFIG_DAALA_DIST
     920             :   uint8_t *dst;
     921             :   int16_t *pred;
     922             :   const int dst_stride = pd->dst.stride;
     923             :   int tx_blk_size;
     924             :   int i, j;
     925             : #endif
     926             : 
     927             : #if !CONFIG_PVQ
     928           0 :   const int tx2d_size = tx_size_2d[tx_size];
     929             :   QUANT_PARAM qparam;
     930             :   const int16_t *src_diff;
     931             : 
     932           0 :   src_diff =
     933           0 :       &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
     934           0 :   qparam.log_scale = av1_get_tx_scale(tx_size);
     935             : #if CONFIG_NEW_QUANT
     936             :   qparam.tx_size = tx_size;
     937             :   qparam.dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
     938             : #endif  // CONFIG_NEW_QUANT
     939             : #if CONFIG_AOM_QM
     940             :   qparam.qmatrix = qmatrix;
     941             :   qparam.iqmatrix = iqmatrix;
     942             : #endif  // CONFIG_AOM_QM
     943             : #else
     944             :   tran_low_t *ref_coeff = BLOCK_OFFSET(pd->pvq_ref_coeff, block);
     945             :   int skip = 1;
     946             :   PVQ_INFO *pvq_info = NULL;
     947             :   uint8_t *src;
     948             :   int16_t *src_int16;
     949             :   const int src_stride = p->src.stride;
     950             : 
     951             :   (void)ctx;
     952             :   (void)scan_order;
     953             :   (void)qcoeff;
     954             : 
     955             :   if (x->pvq_coded) {
     956             :     assert(block < MAX_PVQ_BLOCKS_IN_SB);
     957             :     pvq_info = &x->pvq[block][plane];
     958             :   }
     959             :   src = &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]];
     960             :   src_int16 =
     961             :       &p->src_int16[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
     962             : 
     963             :   // transform block size in pixels
     964             :   tx_blk_size = tx_size_wide[tx_size];
     965             : #if CONFIG_HIGHBITDEPTH
     966             :   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     967             :     for (j = 0; j < tx_blk_size; j++)
     968             :       for (i = 0; i < tx_blk_size; i++)
     969             :         src_int16[diff_stride * j + i] =
     970             :             CONVERT_TO_SHORTPTR(src)[src_stride * j + i];
     971             :   } else {
     972             : #endif  // CONFIG_HIGHBITDEPTH
     973             :     for (j = 0; j < tx_blk_size; j++)
     974             :       for (i = 0; i < tx_blk_size; i++)
     975             :         src_int16[diff_stride * j + i] = src[src_stride * j + i];
     976             : #if CONFIG_HIGHBITDEPTH
     977             :   }
     978             : #endif  // CONFIG_HIGHBITDEPTH
     979             : #endif
     980             : 
     981             : #if CONFIG_PVQ || CONFIG_DAALA_DIST
     982             :   dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
     983             :   pred = &pd->pred[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
     984             : 
     985             :   // transform block size in pixels
     986             :   tx_blk_size = tx_size_wide[tx_size];
     987             : 
     988             : // copy uint8 orig and predicted block to int16 buffer
     989             : // in order to use existing VP10 transform functions
     990             : #if CONFIG_HIGHBITDEPTH
     991             :   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     992             :     for (j = 0; j < tx_blk_size; j++)
     993             :       for (i = 0; i < tx_blk_size; i++)
     994             :         pred[diff_stride * j + i] =
     995             :             CONVERT_TO_SHORTPTR(dst)[dst_stride * j + i];
     996             :   } else {
     997             : #endif  // CONFIG_HIGHBITDEPTH
     998             :     for (j = 0; j < tx_blk_size; j++)
     999             :       for (i = 0; i < tx_blk_size; i++)
    1000             :         pred[diff_stride * j + i] = dst[dst_stride * j + i];
    1001             : #if CONFIG_HIGHBITDEPTH
    1002             :   }
    1003             : #endif  // CONFIG_HIGHBITDEPTH
    1004             : #endif
    1005             : 
    1006             :   (void)ctx;
    1007             : 
    1008           0 :   fwd_txfm_param.tx_type = tx_type;
    1009           0 :   fwd_txfm_param.tx_size = tx_size;
    1010           0 :   fwd_txfm_param.lossless = xd->lossless[mbmi->segment_id];
    1011             : 
    1012             : #if !CONFIG_PVQ
    1013             : #if CONFIG_HIGHBITDEPTH
    1014           0 :   fwd_txfm_param.bd = xd->bd;
    1015           0 :   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    1016           0 :     av1_highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
    1017           0 :     if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
    1018           0 :       if (LIKELY(!x->skip_block)) {
    1019           0 :         quant_func_list[xform_quant_idx][QUANT_FUNC_HIGHBD](
    1020             :             coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam);
    1021             :       } else {
    1022           0 :         av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
    1023             :       }
    1024             :     }
    1025             : #if CONFIG_LV_MAP
    1026             :     p->txb_entropy_ctx[block] =
    1027             :         (uint8_t)av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
    1028             : #endif  // CONFIG_LV_MAP
    1029           0 :     return;
    1030             :   }
    1031             : #endif  // CONFIG_HIGHBITDEPTH
    1032           0 :   av1_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
    1033           0 :   if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
    1034           0 :     if (LIKELY(!x->skip_block)) {
    1035           0 :       quant_func_list[xform_quant_idx][QUANT_FUNC_LOWBD](
    1036             :           coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam);
    1037             :     } else {
    1038           0 :       av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
    1039             :     }
    1040             :   }
    1041             : #if CONFIG_LV_MAP
    1042             :   p->txb_entropy_ctx[block] =
    1043             :       (uint8_t)av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
    1044             : #endif  // CONFIG_LV_MAP
    1045             : #else   // #if !CONFIG_PVQ
    1046             :   (void)xform_quant_idx;
    1047             : #if CONFIG_HIGHBITDEPTH
    1048             :   fwd_txfm_param.bd = xd->bd;
    1049             :   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    1050             :     av1_highbd_fwd_txfm(src_int16, coeff, diff_stride, &fwd_txfm_param);
    1051             :     av1_highbd_fwd_txfm(pred, ref_coeff, diff_stride, &fwd_txfm_param);
    1052             :   } else {
    1053             : #endif
    1054             :     av1_fwd_txfm(src_int16, coeff, diff_stride, &fwd_txfm_param);
    1055             :     av1_fwd_txfm(pred, ref_coeff, diff_stride, &fwd_txfm_param);
    1056             : #if CONFIG_HIGHBITDEPTH
    1057             :   }
    1058             : #endif
    1059             : 
    1060             :   // PVQ for inter mode block
    1061             :   if (!x->skip_block) {
    1062             :     PVQ_SKIP_TYPE ac_dc_coded =
    1063             :         av1_pvq_encode_helper(x,
    1064             :                               coeff,        // target original vector
    1065             :                               ref_coeff,    // reference vector
    1066             :                               dqcoeff,      // de-quantized vector
    1067             :                               eob,          // End of Block marker
    1068             :                               pd->dequant,  // aom's quantizers
    1069             :                               plane,        // image plane
    1070             :                               tx_size,      // block size in log_2 - 2
    1071             :                               tx_type,
    1072             :                               &x->rate,  // rate measured
    1073             :                               x->pvq_speed,
    1074             :                               pvq_info);  // PVQ info for a block
    1075             :     skip = ac_dc_coded == PVQ_SKIP;
    1076             :   }
    1077             :   x->pvq_skip[plane] = skip;
    1078             : 
    1079             :   if (!skip) mbmi->skip = 0;
    1080             : #endif  // #if !CONFIG_PVQ
    1081             : }
    1082             : 
    1083           0 : static void encode_block(int plane, int block, int blk_row, int blk_col,
    1084             :                          BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
    1085           0 :   struct encode_b_args *const args = arg;
    1086           0 :   AV1_COMMON *cm = args->cm;
    1087           0 :   MACROBLOCK *const x = args->x;
    1088           0 :   MACROBLOCKD *const xd = &x->e_mbd;
    1089             :   int ctx;
    1090           0 :   struct macroblock_plane *const p = &x->plane[plane];
    1091           0 :   struct macroblockd_plane *const pd = &xd->plane[plane];
    1092           0 :   tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
    1093             :   uint8_t *dst;
    1094             : #if !CONFIG_PVQ
    1095             :   ENTROPY_CONTEXT *a, *l;
    1096             : #endif
    1097             : #if CONFIG_VAR_TX
    1098           0 :   int bw = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
    1099             : #endif
    1100           0 :   dst = &pd->dst
    1101           0 :              .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
    1102             : 
    1103             : #if !CONFIG_PVQ
    1104           0 :   a = &args->ta[blk_col];
    1105           0 :   l = &args->tl[blk_row];
    1106             : #if CONFIG_VAR_TX
    1107           0 :   ctx = get_entropy_context(tx_size, a, l);
    1108             : #else
    1109             :   ctx = combine_entropy_contexts(*a, *l);
    1110             : #endif
    1111             : #else
    1112             :   ctx = 0;
    1113             : #endif  // CONFIG_PVQ
    1114             : 
    1115             : #if CONFIG_VAR_TX
    1116             :   // Assert not magic number (uninitialized).
    1117           0 :   assert(x->blk_skip[plane][blk_row * bw + blk_col] != 234);
    1118             : 
    1119           0 :   if (x->blk_skip[plane][blk_row * bw + blk_col] == 0) {
    1120             : #else
    1121             :   {
    1122             : #endif
    1123           0 :     av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
    1124             :                     ctx, AV1_XFORM_QUANT_FP);
    1125             :   }
    1126             : #if CONFIG_VAR_TX
    1127             :   else {
    1128           0 :     p->eobs[block] = 0;
    1129             :   }
    1130             : #endif
    1131             : 
    1132             : #if !CONFIG_PVQ
    1133           0 :   av1_optimize_b(cm, x, plane, block, plane_bsize, tx_size, a, l);
    1134             : 
    1135           0 :   av1_set_txb_context(x, plane, block, tx_size, a, l);
    1136             : 
    1137           0 :   if (p->eobs[block]) *(args->skip) = 0;
    1138             : 
    1139           0 :   if (p->eobs[block] == 0) return;
    1140             : #else
    1141             :   (void)ctx;
    1142             :   if (!x->pvq_skip[plane]) *(args->skip) = 0;
    1143             : 
    1144             :   if (x->pvq_skip[plane]) return;
    1145             : #endif
    1146           0 :   TX_TYPE tx_type = get_tx_type(pd->plane_type, xd, block, tx_size);
    1147           0 :   av1_inverse_transform_block(xd, dqcoeff, tx_type, tx_size, dst,
    1148           0 :                               pd->dst.stride, p->eobs[block]);
    1149             : }
    1150             : 
    1151             : #if CONFIG_VAR_TX
    1152           0 : static void encode_block_inter(int plane, int block, int blk_row, int blk_col,
    1153             :                                BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
    1154             :                                void *arg) {
    1155           0 :   struct encode_b_args *const args = arg;
    1156           0 :   MACROBLOCK *const x = args->x;
    1157           0 :   MACROBLOCKD *const xd = &x->e_mbd;
    1158           0 :   MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
    1159           0 :   const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
    1160           0 :   const struct macroblockd_plane *const pd = &xd->plane[plane];
    1161           0 :   const int tx_row = blk_row >> (1 - pd->subsampling_y);
    1162           0 :   const int tx_col = blk_col >> (1 - pd->subsampling_x);
    1163             :   TX_SIZE plane_tx_size;
    1164           0 :   const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
    1165           0 :   const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
    1166             : 
    1167           0 :   if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
    1168             : 
    1169           0 :   plane_tx_size =
    1170           0 :       plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
    1171           0 :             : mbmi->inter_tx_size[tx_row][tx_col];
    1172             : 
    1173           0 :   if (tx_size == plane_tx_size) {
    1174           0 :     encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
    1175             :   } else {
    1176           0 :     assert(tx_size < TX_SIZES_ALL);
    1177           0 :     const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
    1178           0 :     assert(sub_txs < tx_size);
    1179             :     // This is the square transform block partition entry point.
    1180           0 :     int bsl = tx_size_wide_unit[sub_txs];
    1181             :     int i;
    1182           0 :     assert(bsl > 0);
    1183             : 
    1184           0 :     for (i = 0; i < 4; ++i) {
    1185           0 :       const int offsetr = blk_row + ((i >> 1) * bsl);
    1186           0 :       const int offsetc = blk_col + ((i & 0x01) * bsl);
    1187           0 :       int step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs];
    1188             : 
    1189           0 :       if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
    1190             : 
    1191           0 :       encode_block_inter(plane, block, offsetr, offsetc, plane_bsize, sub_txs,
    1192             :                          arg);
    1193           0 :       block += step;
    1194             :     }
    1195             :   }
    1196             : }
    1197             : #endif
    1198             : 
    1199             : typedef struct encode_block_pass1_args {
    1200             :   AV1_COMMON *cm;
    1201             :   MACROBLOCK *x;
    1202             : } encode_block_pass1_args;
    1203             : 
    1204           0 : static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
    1205             :                                BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
    1206             :                                void *arg) {
    1207           0 :   encode_block_pass1_args *args = (encode_block_pass1_args *)arg;
    1208           0 :   AV1_COMMON *cm = args->cm;
    1209           0 :   MACROBLOCK *const x = args->x;
    1210           0 :   MACROBLOCKD *const xd = &x->e_mbd;
    1211           0 :   struct macroblock_plane *const p = &x->plane[plane];
    1212           0 :   struct macroblockd_plane *const pd = &xd->plane[plane];
    1213           0 :   tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
    1214             :   uint8_t *dst;
    1215           0 :   int ctx = 0;
    1216           0 :   dst = &pd->dst
    1217           0 :              .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
    1218             : 
    1219           0 :   av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
    1220             :                   ctx, AV1_XFORM_QUANT_B);
    1221             : #if !CONFIG_PVQ
    1222           0 :   if (p->eobs[block] > 0) {
    1223             : #else
    1224             :   if (!x->pvq_skip[plane]) {
    1225             :     {
    1226             :       int tx_blk_size;
    1227             :       int i, j;
    1228             :       // transform block size in pixels
    1229             :       tx_blk_size = tx_size_wide[tx_size];
    1230             : 
    1231             : // Since av1 does not have separate function which does inverse transform
    1232             : // but av1_inv_txfm_add_*x*() also does addition of predicted image to
    1233             : // inverse transformed image,
    1234             : // pass blank dummy image to av1_inv_txfm_add_*x*(), i.e. set dst as zeros
    1235             : #if CONFIG_HIGHBITDEPTH
    1236             :       if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    1237             :         for (j = 0; j < tx_blk_size; j++)
    1238             :           for (i = 0; i < tx_blk_size; i++)
    1239             :             CONVERT_TO_SHORTPTR(dst)[j * pd->dst.stride + i] = 0;
    1240             :       } else {
    1241             : #endif  // CONFIG_HIGHBITDEPTH
    1242             :         for (j = 0; j < tx_blk_size; j++)
    1243             :           for (i = 0; i < tx_blk_size; i++) dst[j * pd->dst.stride + i] = 0;
    1244             : #if CONFIG_HIGHBITDEPTH
    1245             :       }
    1246             : #endif  // CONFIG_HIGHBITDEPTH
    1247             :     }
    1248             : #endif  // !CONFIG_PVQ
    1249             : #if CONFIG_HIGHBITDEPTH
    1250           0 :     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    1251           0 :       if (xd->lossless[xd->mi[0]->mbmi.segment_id]) {
    1252           0 :         av1_highbd_iwht4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block],
    1253             :                                xd->bd);
    1254             :       } else {
    1255           0 :         av1_highbd_idct4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block],
    1256             :                                xd->bd);
    1257             :       }
    1258           0 :       return;
    1259             :     }
    1260             : #endif  //  CONFIG_HIGHBITDEPTH
    1261           0 :     if (xd->lossless[xd->mi[0]->mbmi.segment_id]) {
    1262           0 :       av1_iwht4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
    1263             :     } else {
    1264           0 :       av1_idct4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
    1265             :     }
    1266             :   }
    1267             : }
    1268             : 
    1269           0 : void av1_encode_sby_pass1(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize) {
    1270           0 :   encode_block_pass1_args args = { cm, x };
    1271           0 :   av1_subtract_plane(x, bsize, 0);
    1272           0 :   av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
    1273             :                                          encode_block_pass1, &args);
    1274           0 : }
    1275             : 
    1276           0 : void av1_encode_sb(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize, int mi_row,
    1277             :                    int mi_col) {
    1278           0 :   MACROBLOCKD *const xd = &x->e_mbd;
    1279             :   struct optimize_ctx ctx;
    1280           0 :   MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
    1281           0 :   struct encode_b_args arg = { cm, x, &ctx, &mbmi->skip, NULL, NULL, 1 };
    1282             :   int plane;
    1283             : 
    1284           0 :   mbmi->skip = 1;
    1285             : 
    1286           0 :   if (x->skip) return;
    1287             : 
    1288           0 :   for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
    1289             : #if CONFIG_CB4X4 && !CONFIG_CHROMA_2X2
    1290           0 :     const int subsampling_x = xd->plane[plane].subsampling_x;
    1291           0 :     const int subsampling_y = xd->plane[plane].subsampling_y;
    1292             : 
    1293           0 :     if (!is_chroma_reference(mi_row, mi_col, bsize, subsampling_x,
    1294             :                              subsampling_y))
    1295           0 :       continue;
    1296             : 
    1297           0 :     bsize = scale_chroma_bsize(bsize, subsampling_x, subsampling_y);
    1298             : #else
    1299             :     (void)mi_row;
    1300             :     (void)mi_col;
    1301             : #endif
    1302             : 
    1303             : #if CONFIG_VAR_TX
    1304             :     // TODO(jingning): Clean this up.
    1305           0 :     const struct macroblockd_plane *const pd = &xd->plane[plane];
    1306           0 :     const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
    1307           0 :     const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
    1308           0 :     const int mi_height = block_size_high[plane_bsize] >> tx_size_wide_log2[0];
    1309           0 :     const TX_SIZE max_tx_size = get_vartx_max_txsize(mbmi, plane_bsize);
    1310           0 :     const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
    1311           0 :     const int bw = block_size_wide[txb_size] >> tx_size_wide_log2[0];
    1312           0 :     const int bh = block_size_high[txb_size] >> tx_size_wide_log2[0];
    1313             :     int idx, idy;
    1314           0 :     int block = 0;
    1315           0 :     int step = tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
    1316           0 :     av1_get_entropy_contexts(bsize, 0, pd, ctx.ta[plane], ctx.tl[plane]);
    1317             : #else
    1318             :     const struct macroblockd_plane *const pd = &xd->plane[plane];
    1319             :     const TX_SIZE tx_size = get_tx_size(plane, xd);
    1320             :     av1_get_entropy_contexts(bsize, tx_size, pd, ctx.ta[plane], ctx.tl[plane]);
    1321             : #endif
    1322             : 
    1323             : #if !CONFIG_PVQ
    1324           0 :     av1_subtract_plane(x, bsize, plane);
    1325             : #endif
    1326           0 :     arg.ta = ctx.ta[plane];
    1327           0 :     arg.tl = ctx.tl[plane];
    1328             : 
    1329             : #if CONFIG_VAR_TX
    1330           0 :     for (idy = 0; idy < mi_height; idy += bh) {
    1331           0 :       for (idx = 0; idx < mi_width; idx += bw) {
    1332           0 :         encode_block_inter(plane, block, idy, idx, plane_bsize, max_tx_size,
    1333             :                            &arg);
    1334           0 :         block += step;
    1335             :       }
    1336             :     }
    1337             : #else
    1338             :     av1_foreach_transformed_block_in_plane(xd, bsize, plane, encode_block,
    1339             :                                            &arg);
    1340             : #endif
    1341             :   }
    1342             : }
    1343             : 
    1344             : #if CONFIG_SUPERTX
    1345             : void av1_encode_sb_supertx(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize) {
    1346             :   MACROBLOCKD *const xd = &x->e_mbd;
    1347             :   struct optimize_ctx ctx;
    1348             :   MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
    1349             :   struct encode_b_args arg = { cm, x, &ctx, &mbmi->skip, NULL, NULL, 1 };
    1350             :   int plane;
    1351             : 
    1352             :   mbmi->skip = 1;
    1353             :   if (x->skip) return;
    1354             : 
    1355             :   for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
    1356             :     const struct macroblockd_plane *const pd = &xd->plane[plane];
    1357             : #if CONFIG_VAR_TX
    1358             :     const TX_SIZE tx_size = TX_4X4;
    1359             : #else
    1360             :     const TX_SIZE tx_size = get_tx_size(plane, xd);
    1361             : #endif
    1362             :     av1_subtract_plane(x, bsize, plane);
    1363             :     av1_get_entropy_contexts(bsize, tx_size, pd, ctx.ta[plane], ctx.tl[plane]);
    1364             :     arg.ta = ctx.ta[plane];
    1365             :     arg.tl = ctx.tl[plane];
    1366             :     av1_foreach_transformed_block_in_plane(xd, bsize, plane, encode_block,
    1367             :                                            &arg);
    1368             :   }
    1369             : }
    1370             : #endif  // CONFIG_SUPERTX
    1371             : 
    1372             : #if !CONFIG_PVQ
    1373           0 : void av1_set_txb_context(MACROBLOCK *x, int plane, int block, TX_SIZE tx_size,
    1374             :                          ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l) {
    1375             :   (void)tx_size;
    1376           0 :   struct macroblock_plane *p = &x->plane[plane];
    1377             : 
    1378             : #if !CONFIG_LV_MAP
    1379           0 :   *a = *l = p->eobs[block] > 0;
    1380             : #else   // !CONFIG_LV_MAP
    1381             :   *a = *l = p->txb_entropy_ctx[block];
    1382             : #endif  // !CONFIG_LV_MAP
    1383             : 
    1384             : #if CONFIG_VAR_TX || CONFIG_LV_MAP
    1385             :   int i;
    1386           0 :   for (i = 0; i < tx_size_wide_unit[tx_size]; ++i) a[i] = a[0];
    1387             : 
    1388           0 :   for (i = 0; i < tx_size_high_unit[tx_size]; ++i) l[i] = l[0];
    1389             : #endif
    1390           0 : }
    1391             : #endif
    1392             : 
    1393           0 : static void encode_block_intra_and_set_context(int plane, int block,
    1394             :                                                int blk_row, int blk_col,
    1395             :                                                BLOCK_SIZE plane_bsize,
    1396             :                                                TX_SIZE tx_size, void *arg) {
    1397           0 :   av1_encode_block_intra(plane, block, blk_row, blk_col, plane_bsize, tx_size,
    1398             :                          arg);
    1399             : #if !CONFIG_PVQ
    1400           0 :   struct encode_b_args *const args = arg;
    1401           0 :   MACROBLOCK *x = args->x;
    1402           0 :   ENTROPY_CONTEXT *a = &args->ta[blk_col];
    1403           0 :   ENTROPY_CONTEXT *l = &args->tl[blk_row];
    1404           0 :   av1_set_txb_context(x, plane, block, tx_size, a, l);
    1405             : #endif
    1406           0 : }
    1407             : 
    1408             : #if CONFIG_DPCM_INTRA
    1409             : static int get_eob(const tran_low_t *qcoeff, intptr_t n_coeffs,
    1410             :                    const int16_t *scan) {
    1411             :   int eob = -1;
    1412             :   for (int i = (int)n_coeffs - 1; i >= 0; i--) {
    1413             :     const int rc = scan[i];
    1414             :     if (qcoeff[rc]) {
    1415             :       eob = i;
    1416             :       break;
    1417             :     }
    1418             :   }
    1419             :   return eob + 1;
    1420             : }
    1421             : 
    1422             : static void quantize_scaler(int coeff, int16_t zbin, int16_t round_value,
    1423             :                             int16_t quant, int16_t quant_shift, int16_t dequant,
    1424             :                             int log_scale, tran_low_t *const qcoeff,
    1425             :                             tran_low_t *const dqcoeff) {
    1426             :   zbin = ROUND_POWER_OF_TWO(zbin, log_scale);
    1427             :   round_value = ROUND_POWER_OF_TWO(round_value, log_scale);
    1428             :   const int coeff_sign = (coeff >> 31);
    1429             :   const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
    1430             :   if (abs_coeff >= zbin) {
    1431             :     int tmp = clamp(abs_coeff + round_value, INT16_MIN, INT16_MAX);
    1432             :     tmp = ((((tmp * quant) >> 16) + tmp) * quant_shift) >> (16 - log_scale);
    1433             :     *qcoeff = (tmp ^ coeff_sign) - coeff_sign;
    1434             :     *dqcoeff = (*qcoeff * dequant) / (1 << log_scale);
    1435             :   }
    1436             : }
    1437             : 
    1438             : typedef void (*dpcm_fwd_tx_func)(const int16_t *input, int stride,
    1439             :                                  TX_TYPE_1D tx_type, tran_low_t *output);
    1440             : 
    1441             : static dpcm_fwd_tx_func get_dpcm_fwd_tx_func(int tx_length) {
    1442             :   switch (tx_length) {
    1443             :     case 4: return av1_dpcm_ft4_c;
    1444             :     case 8: return av1_dpcm_ft8_c;
    1445             :     case 16: return av1_dpcm_ft16_c;
    1446             :     case 32:
    1447             :       return av1_dpcm_ft32_c;
    1448             :     // TODO(huisu): add support for TX_64X64.
    1449             :     default: assert(0); return NULL;
    1450             :   }
    1451             : }
    1452             : 
    1453             : static void process_block_dpcm_vert(TX_SIZE tx_size, TX_TYPE_1D tx_type_1d,
    1454             :                                     struct macroblockd_plane *const pd,
    1455             :                                     struct macroblock_plane *const p,
    1456             :                                     uint8_t *src, int src_stride, uint8_t *dst,
    1457             :                                     int dst_stride, int16_t *src_diff,
    1458             :                                     int diff_stride, tran_low_t *coeff,
    1459             :                                     tran_low_t *qcoeff, tran_low_t *dqcoeff) {
    1460             :   const int tx1d_width = tx_size_wide[tx_size];
    1461             :   dpcm_fwd_tx_func forward_tx = get_dpcm_fwd_tx_func(tx1d_width);
    1462             :   dpcm_inv_txfm_add_func inverse_tx =
    1463             :       av1_get_dpcm_inv_txfm_add_func(tx1d_width);
    1464             :   const int tx1d_height = tx_size_high[tx_size];
    1465             :   const int log_scale = av1_get_tx_scale(tx_size);
    1466             :   int q_idx = 0;
    1467             :   for (int r = 0; r < tx1d_height; ++r) {
    1468             :     // Update prediction.
    1469             :     if (r > 0) memcpy(dst, dst - dst_stride, tx1d_width * sizeof(dst[0]));
    1470             :     // Subtraction.
    1471             :     for (int c = 0; c < tx1d_width; ++c) src_diff[c] = src[c] - dst[c];
    1472             :     // Forward transform.
    1473             :     forward_tx(src_diff, 1, tx_type_1d, coeff);
    1474             :     // Quantization.
    1475             :     for (int c = 0; c < tx1d_width; ++c) {
    1476             :       quantize_scaler(coeff[c], p->zbin[q_idx], p->round[q_idx],
    1477             :                       p->quant[q_idx], p->quant_shift[q_idx],
    1478             :                       pd->dequant[q_idx], log_scale, &qcoeff[c], &dqcoeff[c]);
    1479             :       q_idx = 1;
    1480             :     }
    1481             :     // Inverse transform.
    1482             :     inverse_tx(dqcoeff, 1, tx_type_1d, dst);
    1483             :     // Move to the next row.
    1484             :     coeff += tx1d_width;
    1485             :     qcoeff += tx1d_width;
    1486             :     dqcoeff += tx1d_width;
    1487             :     src_diff += diff_stride;
    1488             :     dst += dst_stride;
    1489             :     src += src_stride;
    1490             :   }
    1491             : }
    1492             : 
    1493             : static void process_block_dpcm_horz(TX_SIZE tx_size, TX_TYPE_1D tx_type_1d,
    1494             :                                     struct macroblockd_plane *const pd,
    1495             :                                     struct macroblock_plane *const p,
    1496             :                                     uint8_t *src, int src_stride, uint8_t *dst,
    1497             :                                     int dst_stride, int16_t *src_diff,
    1498             :                                     int diff_stride, tran_low_t *coeff,
    1499             :                                     tran_low_t *qcoeff, tran_low_t *dqcoeff) {
    1500             :   const int tx1d_height = tx_size_high[tx_size];
    1501             :   dpcm_fwd_tx_func forward_tx = get_dpcm_fwd_tx_func(tx1d_height);
    1502             :   dpcm_inv_txfm_add_func inverse_tx =
    1503             :       av1_get_dpcm_inv_txfm_add_func(tx1d_height);
    1504             :   const int tx1d_width = tx_size_wide[tx_size];
    1505             :   const int log_scale = av1_get_tx_scale(tx_size);
    1506             :   int q_idx = 0;
    1507             :   for (int c = 0; c < tx1d_width; ++c) {
    1508             :     for (int r = 0; r < tx1d_height; ++r) {
    1509             :       // Update prediction.
    1510             :       if (c > 0) dst[r * dst_stride] = dst[r * dst_stride - 1];
    1511             :       // Subtraction.
    1512             :       src_diff[r * diff_stride] = src[r * src_stride] - dst[r * dst_stride];
    1513             :     }
    1514             :     // Forward transform.
    1515             :     tran_low_t tx_buff[64];
    1516             :     forward_tx(src_diff, diff_stride, tx_type_1d, tx_buff);
    1517             :     for (int r = 0; r < tx1d_height; ++r) coeff[r * tx1d_width] = tx_buff[r];
    1518             :     // Quantization.
    1519             :     for (int r = 0; r < tx1d_height; ++r) {
    1520             :       quantize_scaler(coeff[r * tx1d_width], p->zbin[q_idx], p->round[q_idx],
    1521             :                       p->quant[q_idx], p->quant_shift[q_idx],
    1522             :                       pd->dequant[q_idx], log_scale, &qcoeff[r * tx1d_width],
    1523             :                       &dqcoeff[r * tx1d_width]);
    1524             :       q_idx = 1;
    1525             :     }
    1526             :     // Inverse transform.
    1527             :     for (int r = 0; r < tx1d_height; ++r) tx_buff[r] = dqcoeff[r * tx1d_width];
    1528             :     inverse_tx(tx_buff, dst_stride, tx_type_1d, dst);
    1529             :     // Move to the next column.
    1530             :     ++coeff, ++qcoeff, ++dqcoeff, ++src_diff, ++dst, ++src;
    1531             :   }
    1532             : }
    1533             : 
    1534             : #if CONFIG_HIGHBITDEPTH
    1535             : static void hbd_process_block_dpcm_vert(
    1536             :     TX_SIZE tx_size, TX_TYPE_1D tx_type_1d, int bd,
    1537             :     struct macroblockd_plane *const pd, struct macroblock_plane *const p,
    1538             :     uint8_t *src8, int src_stride, uint8_t *dst8, int dst_stride,
    1539             :     int16_t *src_diff, int diff_stride, tran_low_t *coeff, tran_low_t *qcoeff,
    1540             :     tran_low_t *dqcoeff) {
    1541             :   const int tx1d_width = tx_size_wide[tx_size];
    1542             :   dpcm_fwd_tx_func forward_tx = get_dpcm_fwd_tx_func(tx1d_width);
    1543             :   hbd_dpcm_inv_txfm_add_func inverse_tx =
    1544             :       av1_get_hbd_dpcm_inv_txfm_add_func(tx1d_width);
    1545             :   uint16_t *src = CONVERT_TO_SHORTPTR(src8);
    1546             :   uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
    1547             :   const int tx1d_height = tx_size_high[tx_size];
    1548             :   const int log_scale = av1_get_tx_scale(tx_size);
    1549             :   int q_idx = 0;
    1550             :   for (int r = 0; r < tx1d_height; ++r) {
    1551             :     // Update prediction.
    1552             :     if (r > 0) memcpy(dst, dst - dst_stride, tx1d_width * sizeof(dst[0]));
    1553             :     // Subtraction.
    1554             :     for (int c = 0; c < tx1d_width; ++c) src_diff[c] = src[c] - dst[c];
    1555             :     // Forward transform.
    1556             :     forward_tx(src_diff, 1, tx_type_1d, coeff);
    1557             :     // Quantization.
    1558             :     for (int c = 0; c < tx1d_width; ++c) {
    1559             :       quantize_scaler(coeff[c], p->zbin[q_idx], p->round[q_idx],
    1560             :                       p->quant[q_idx], p->quant_shift[q_idx],
    1561             :                       pd->dequant[q_idx], log_scale, &qcoeff[c], &dqcoeff[c]);
    1562             :       q_idx = 1;
    1563             :     }
    1564             :     // Inverse transform.
    1565             :     inverse_tx(dqcoeff, 1, tx_type_1d, bd, dst);
    1566             :     // Move to the next row.
    1567             :     coeff += tx1d_width;
    1568             :     qcoeff += tx1d_width;
    1569             :     dqcoeff += tx1d_width;
    1570             :     src_diff += diff_stride;
    1571             :     dst += dst_stride;
    1572             :     src += src_stride;
    1573             :   }
    1574             : }
    1575             : 
    1576             : static void hbd_process_block_dpcm_horz(
    1577             :     TX_SIZE tx_size, TX_TYPE_1D tx_type_1d, int bd,
    1578             :     struct macroblockd_plane *const pd, struct macroblock_plane *const p,
    1579             :     uint8_t *src8, int src_stride, uint8_t *dst8, int dst_stride,
    1580             :     int16_t *src_diff, int diff_stride, tran_low_t *coeff, tran_low_t *qcoeff,
    1581             :     tran_low_t *dqcoeff) {
    1582             :   const int tx1d_height = tx_size_high[tx_size];
    1583             :   dpcm_fwd_tx_func forward_tx = get_dpcm_fwd_tx_func(tx1d_height);
    1584             :   hbd_dpcm_inv_txfm_add_func inverse_tx =
    1585             :       av1_get_hbd_dpcm_inv_txfm_add_func(tx1d_height);
    1586             :   uint16_t *src = CONVERT_TO_SHORTPTR(src8);
    1587             :   uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
    1588             :   const int tx1d_width = tx_size_wide[tx_size];
    1589             :   const int log_scale = av1_get_tx_scale(tx_size);
    1590             :   int q_idx = 0;
    1591             :   for (int c = 0; c < tx1d_width; ++c) {
    1592             :     for (int r = 0; r < tx1d_height; ++r) {
    1593             :       // Update prediction.
    1594             :       if (c > 0) dst[r * dst_stride] = dst[r * dst_stride - 1];
    1595             :       // Subtraction.
    1596             :       src_diff[r * diff_stride] = src[r * src_stride] - dst[r * dst_stride];
    1597             :     }
    1598             :     // Forward transform.
    1599             :     tran_low_t tx_buff[64];
    1600             :     forward_tx(src_diff, diff_stride, tx_type_1d, tx_buff);
    1601             :     for (int r = 0; r < tx1d_height; ++r) coeff[r * tx1d_width] = tx_buff[r];
    1602             :     // Quantization.
    1603             :     for (int r = 0; r < tx1d_height; ++r) {
    1604             :       quantize_scaler(coeff[r * tx1d_width], p->zbin[q_idx], p->round[q_idx],
    1605             :                       p->quant[q_idx], p->quant_shift[q_idx],
    1606             :                       pd->dequant[q_idx], log_scale, &qcoeff[r * tx1d_width],
    1607             :                       &dqcoeff[r * tx1d_width]);
    1608             :       q_idx = 1;
    1609             :     }
    1610             :     // Inverse transform.
    1611             :     for (int r = 0; r < tx1d_height; ++r) tx_buff[r] = dqcoeff[r * tx1d_width];
    1612             :     inverse_tx(tx_buff, dst_stride, tx_type_1d, bd, dst);
    1613             :     // Move to the next column.
    1614             :     ++coeff, ++qcoeff, ++dqcoeff, ++src_diff, ++dst, ++src;
    1615             :   }
    1616             : }
    1617             : #endif  // CONFIG_HIGHBITDEPTH
    1618             : 
    1619             : void av1_encode_block_intra_dpcm(const AV1_COMMON *cm, MACROBLOCK *x,
    1620             :                                  PREDICTION_MODE mode, int plane, int block,
    1621             :                                  int blk_row, int blk_col,
    1622             :                                  BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
    1623             :                                  TX_TYPE tx_type, ENTROPY_CONTEXT *ta,
    1624             :                                  ENTROPY_CONTEXT *tl, int8_t *skip) {
    1625             :   MACROBLOCKD *const xd = &x->e_mbd;
    1626             :   struct macroblock_plane *const p = &x->plane[plane];
    1627             :   struct macroblockd_plane *const pd = &xd->plane[plane];
    1628             :   tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
    1629             :   const int diff_stride = block_size_wide[plane_bsize];
    1630             :   const int src_stride = p->src.stride;
    1631             :   const int dst_stride = pd->dst.stride;
    1632             :   const int tx1d_width = tx_size_wide[tx_size];
    1633             :   const int tx1d_height = tx_size_high[tx_size];
    1634             :   const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, 0);
    1635             :   tran_low_t *coeff = BLOCK_OFFSET(p->coeff, block);
    1636             :   tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
    1637             :   uint8_t *dst =
    1638             :       &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
    1639             :   uint8_t *src =
    1640             :       &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]];
    1641             :   int16_t *src_diff =
    1642             :       &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
    1643             :   uint16_t *eob = &p->eobs[block];
    1644             :   *eob = 0;
    1645             :   memset(qcoeff, 0, tx1d_height * tx1d_width * sizeof(*qcoeff));
    1646             :   memset(dqcoeff, 0, tx1d_height * tx1d_width * sizeof(*dqcoeff));
    1647             : 
    1648             :   if (LIKELY(!x->skip_block)) {
    1649             :     TX_TYPE_1D tx_type_1d = DCT_1D;
    1650             :     switch (tx_type) {
    1651             :       case IDTX: tx_type_1d = IDTX_1D; break;
    1652             :       case V_DCT:
    1653             :         assert(mode == H_PRED);
    1654             :         tx_type_1d = DCT_1D;
    1655             :         break;
    1656             :       case H_DCT:
    1657             :         assert(mode == V_PRED);
    1658             :         tx_type_1d = DCT_1D;
    1659             :         break;
    1660             :       default: assert(0);
    1661             :     }
    1662             :     switch (mode) {
    1663             :       case V_PRED:
    1664             : #if CONFIG_HIGHBITDEPTH
    1665             :         if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    1666             :           hbd_process_block_dpcm_vert(tx_size, tx_type_1d, xd->bd, pd, p, src,
    1667             :                                       src_stride, dst, dst_stride, src_diff,
    1668             :                                       diff_stride, coeff, qcoeff, dqcoeff);
    1669             :         } else {
    1670             : #endif  // CONFIG_HIGHBITDEPTH
    1671             :           process_block_dpcm_vert(tx_size, tx_type_1d, pd, p, src, src_stride,
    1672             :                                   dst, dst_stride, src_diff, diff_stride, coeff,
    1673             :                                   qcoeff, dqcoeff);
    1674             : #if CONFIG_HIGHBITDEPTH
    1675             :         }
    1676             : #endif  // CONFIG_HIGHBITDEPTH
    1677             :         break;
    1678             :       case H_PRED:
    1679             : #if CONFIG_HIGHBITDEPTH
    1680             :         if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    1681             :           hbd_process_block_dpcm_horz(tx_size, tx_type_1d, xd->bd, pd, p, src,
    1682             :                                       src_stride, dst, dst_stride, src_diff,
    1683             :                                       diff_stride, coeff, qcoeff, dqcoeff);
    1684             :         } else {
    1685             : #endif  // CONFIG_HIGHBITDEPTH
    1686             :           process_block_dpcm_horz(tx_size, tx_type_1d, pd, p, src, src_stride,
    1687             :                                   dst, dst_stride, src_diff, diff_stride, coeff,
    1688             :                                   qcoeff, dqcoeff);
    1689             : #if CONFIG_HIGHBITDEPTH
    1690             :         }
    1691             : #endif  // CONFIG_HIGHBITDEPTH
    1692             :         break;
    1693             :       default: assert(0);
    1694             :     }
    1695             :     *eob = get_eob(qcoeff, tx1d_height * tx1d_width, scan_order->scan);
    1696             :   }
    1697             : 
    1698             :   ta[blk_col] = tl[blk_row] = *eob > 0;
    1699             :   if (*eob) *skip = 0;
    1700             : }
    1701             : #endif  // CONFIG_DPCM_INTRA
    1702             : 
    1703           0 : void av1_encode_block_intra(int plane, int block, int blk_row, int blk_col,
    1704             :                             BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
    1705             :                             void *arg) {
    1706           0 :   struct encode_b_args *const args = arg;
    1707           0 :   AV1_COMMON *cm = args->cm;
    1708           0 :   MACROBLOCK *const x = args->x;
    1709           0 :   MACROBLOCKD *const xd = &x->e_mbd;
    1710           0 :   struct macroblock_plane *const p = &x->plane[plane];
    1711           0 :   struct macroblockd_plane *const pd = &xd->plane[plane];
    1712           0 :   tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
    1713           0 :   PLANE_TYPE plane_type = get_plane_type(plane);
    1714           0 :   const TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
    1715           0 :   uint16_t *eob = &p->eobs[block];
    1716           0 :   const int dst_stride = pd->dst.stride;
    1717           0 :   uint8_t *dst =
    1718           0 :       &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
    1719             : #if CONFIG_CFL
    1720             : 
    1721             : #if CONFIG_EC_ADAPT
    1722             :   FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
    1723             : #else
    1724             :   FRAME_CONTEXT *const ec_ctx = cm->fc;
    1725             : #endif  // CONFIG_EC_ADAPT
    1726             : 
    1727             :   av1_predict_intra_block_encoder_facade(x, ec_ctx, plane, block, blk_col,
    1728             :                                          blk_row, tx_size, plane_bsize);
    1729             : #else
    1730           0 :   av1_predict_intra_block_facade(xd, plane, block, blk_col, blk_row, tx_size);
    1731             : #endif
    1732             : 
    1733             : #if CONFIG_DPCM_INTRA
    1734             :   const int block_raster_idx = av1_block_index_to_raster_order(tx_size, block);
    1735             :   const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
    1736             :   const PREDICTION_MODE mode =
    1737             :       (plane == 0) ? get_y_mode(xd->mi[0], block_raster_idx) : mbmi->uv_mode;
    1738             :   if (av1_use_dpcm_intra(plane, mode, tx_type, mbmi)) {
    1739             :     av1_encode_block_intra_dpcm(cm, x, mode, plane, block, blk_row, blk_col,
    1740             :                                 plane_bsize, tx_size, tx_type, args->ta,
    1741             :                                 args->tl, args->skip);
    1742             :     return;
    1743             :   }
    1744             : #endif  // CONFIG_DPCM_INTRA
    1745             : 
    1746           0 :   av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size);
    1747             : 
    1748           0 :   const ENTROPY_CONTEXT *a = &args->ta[blk_col];
    1749           0 :   const ENTROPY_CONTEXT *l = &args->tl[blk_row];
    1750           0 :   int ctx = combine_entropy_contexts(*a, *l);
    1751           0 :   if (args->enable_optimize_b) {
    1752           0 :     av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
    1753             :                     ctx, AV1_XFORM_QUANT_FP);
    1754           0 :     av1_optimize_b(cm, x, plane, block, plane_bsize, tx_size, a, l);
    1755             :   } else {
    1756           0 :     av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
    1757             :                     ctx, AV1_XFORM_QUANT_B);
    1758             :   }
    1759             : 
    1760             : #if CONFIG_PVQ
    1761             :   // *(args->skip) == mbmi->skip
    1762             :   if (!x->pvq_skip[plane]) *(args->skip) = 0;
    1763             : 
    1764             :   if (x->pvq_skip[plane]) return;
    1765             : #endif  // CONFIG_PVQ
    1766           0 :   av1_inverse_transform_block(xd, dqcoeff, tx_type, tx_size, dst, dst_stride,
    1767           0 :                               *eob);
    1768             : #if !CONFIG_PVQ
    1769           0 :   if (*eob) *(args->skip) = 0;
    1770             : #else
    1771             : // Note : *(args->skip) == mbmi->skip
    1772             : #endif
    1773             : #if CONFIG_CFL
    1774             :   MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
    1775             :   if (plane == AOM_PLANE_Y && x->cfl_store_y) {
    1776             :     cfl_store(xd->cfl, dst, dst_stride, blk_row, blk_col, tx_size);
    1777             :   }
    1778             : 
    1779             :   if (mbmi->uv_mode == DC_PRED) {
    1780             :     // TODO(ltrudeau) find a cleaner way to detect last transform block
    1781             :     if (plane == AOM_PLANE_U) {
    1782             :       xd->cfl->num_tx_blk[CFL_PRED_U] =
    1783             :           (blk_row == 0 && blk_col == 0) ? 1
    1784             :                                          : xd->cfl->num_tx_blk[CFL_PRED_U] + 1;
    1785             :     }
    1786             : 
    1787             :     if (plane == AOM_PLANE_V) {
    1788             :       xd->cfl->num_tx_blk[CFL_PRED_V] =
    1789             :           (blk_row == 0 && blk_col == 0) ? 1
    1790             :                                          : xd->cfl->num_tx_blk[CFL_PRED_V] + 1;
    1791             : 
    1792             :       if (mbmi->skip &&
    1793             :           xd->cfl->num_tx_blk[CFL_PRED_U] == xd->cfl->num_tx_blk[CFL_PRED_V]) {
    1794             :         assert(plane_bsize != BLOCK_INVALID);
    1795             :         const int block_width = block_size_wide[plane_bsize];
    1796             :         const int block_height = block_size_high[plane_bsize];
    1797             : 
    1798             :         // if SKIP is chosen at the block level, and ind != 0, we must change
    1799             :         // the prediction
    1800             :         if (mbmi->cfl_alpha_idx != 0) {
    1801             :           const struct macroblockd_plane *const pd_cb = &xd->plane[AOM_PLANE_U];
    1802             :           uint8_t *const dst_cb = pd_cb->dst.buf;
    1803             :           const int dst_stride_cb = pd_cb->dst.stride;
    1804             :           uint8_t *const dst_cr = pd->dst.buf;
    1805             :           const int dst_stride_cr = pd->dst.stride;
    1806             :           for (int j = 0; j < block_height; j++) {
    1807             :             for (int i = 0; i < block_width; i++) {
    1808             :               dst_cb[dst_stride_cb * j + i] =
    1809             :                   (uint8_t)(xd->cfl->dc_pred[CFL_PRED_U] + 0.5);
    1810             :               dst_cr[dst_stride_cr * j + i] =
    1811             :                   (uint8_t)(xd->cfl->dc_pred[CFL_PRED_V] + 0.5);
    1812             :             }
    1813             :           }
    1814             :           mbmi->cfl_alpha_idx = 0;
    1815             :           mbmi->cfl_alpha_signs[CFL_PRED_U] = CFL_SIGN_POS;
    1816             :           mbmi->cfl_alpha_signs[CFL_PRED_V] = CFL_SIGN_POS;
    1817             :         }
    1818             :       }
    1819             :     }
    1820             :   }
    1821             : #endif
    1822           0 : }
    1823             : 
    1824             : #if CONFIG_CFL
    1825             : static int cfl_alpha_dist(const uint8_t *y_pix, int y_stride, double y_avg,
    1826             :                           const uint8_t *src, int src_stride, int blk_width,
    1827             :                           int blk_height, double dc_pred, double alpha,
    1828             :                           int *dist_neg_out) {
    1829             :   const double dc_pred_bias = dc_pred + 0.5;
    1830             :   int dist = 0;
    1831             :   int diff;
    1832             : 
    1833             :   if (alpha == 0.0) {
    1834             :     const int dc_pred_i = (int)dc_pred_bias;
    1835             :     for (int j = 0; j < blk_height; j++) {
    1836             :       for (int i = 0; i < blk_width; i++) {
    1837             :         diff = src[i] - dc_pred_i;
    1838             :         dist += diff * diff;
    1839             :       }
    1840             :       src += src_stride;
    1841             :     }
    1842             : 
    1843             :     if (dist_neg_out) *dist_neg_out = dist;
    1844             : 
    1845             :     return dist;
    1846             :   }
    1847             : 
    1848             :   int dist_neg = 0;
    1849             :   for (int j = 0; j < blk_height; j++) {
    1850             :     for (int i = 0; i < blk_width; i++) {
    1851             :       const double scaled_luma = alpha * (y_pix[i] - y_avg);
    1852             :       const int uv = src[i];
    1853             :       diff = uv - (int)(scaled_luma + dc_pred_bias);
    1854             :       dist += diff * diff;
    1855             :       diff = uv + (int)(scaled_luma - dc_pred_bias);
    1856             :       dist_neg += diff * diff;
    1857             :     }
    1858             :     y_pix += y_stride;
    1859             :     src += src_stride;
    1860             :   }
    1861             : 
    1862             :   if (dist_neg_out) *dist_neg_out = dist_neg;
    1863             : 
    1864             :   return dist;
    1865             : }
    1866             : 
    1867             : static int cfl_compute_alpha_ind(MACROBLOCK *const x, const CFL_CTX *const cfl,
    1868             :                                  BLOCK_SIZE bsize,
    1869             :                                  CFL_SIGN_TYPE signs_out[CFL_SIGNS]) {
    1870             :   const struct macroblock_plane *const p_u = &x->plane[AOM_PLANE_U];
    1871             :   const struct macroblock_plane *const p_v = &x->plane[AOM_PLANE_V];
    1872             :   const uint8_t *const src_u = p_u->src.buf;
    1873             :   const uint8_t *const src_v = p_v->src.buf;
    1874             :   const int src_stride_u = p_u->src.stride;
    1875             :   const int src_stride_v = p_v->src.stride;
    1876             :   const int block_width = block_size_wide[bsize];
    1877             :   const int block_height = block_size_high[bsize];
    1878             :   const double dc_pred_u = cfl->dc_pred[CFL_PRED_U];
    1879             :   const double dc_pred_v = cfl->dc_pred[CFL_PRED_V];
    1880             : 
    1881             :   // Temporary pixel buffer used to store the CfL prediction when we compute the
    1882             :   // alpha index.
    1883             :   uint8_t tmp_pix[MAX_SB_SQUARE];
    1884             :   // Load CfL Prediction over the entire block
    1885             :   const double y_avg =
    1886             :       cfl_load(cfl, tmp_pix, MAX_SB_SIZE, 0, 0, block_width, block_height);
    1887             : 
    1888             :   int sse[CFL_PRED_PLANES][CFL_MAGS_SIZE];
    1889             :   sse[CFL_PRED_U][0] =
    1890             :       cfl_alpha_dist(tmp_pix, MAX_SB_SIZE, y_avg, src_u, src_stride_u,
    1891             :                      block_width, block_height, dc_pred_u, 0, NULL);
    1892             :   sse[CFL_PRED_V][0] =
    1893             :       cfl_alpha_dist(tmp_pix, MAX_SB_SIZE, y_avg, src_v, src_stride_v,
    1894             :                      block_width, block_height, dc_pred_v, 0, NULL);
    1895             :   for (int m = 1; m < CFL_MAGS_SIZE; m += 2) {
    1896             :     assert(cfl_alpha_mags[m + 1] == -cfl_alpha_mags[m]);
    1897             :     sse[CFL_PRED_U][m] = cfl_alpha_dist(
    1898             :         tmp_pix, MAX_SB_SIZE, y_avg, src_u, src_stride_u, block_width,
    1899             :         block_height, dc_pred_u, cfl_alpha_mags[m], &sse[CFL_PRED_U][m + 1]);
    1900             :     sse[CFL_PRED_V][m] = cfl_alpha_dist(
    1901             :         tmp_pix, MAX_SB_SIZE, y_avg, src_v, src_stride_v, block_width,
    1902             :         block_height, dc_pred_v, cfl_alpha_mags[m], &sse[CFL_PRED_V][m + 1]);
    1903             :   }
    1904             : 
    1905             :   int dist;
    1906             :   int64_t cost;
    1907             :   int64_t best_cost;
    1908             : 
    1909             :   // Compute least squares parameter of the entire block
    1910             :   // IMPORTANT: We assume that the first code is 0,0
    1911             :   int ind = 0;
    1912             :   signs_out[CFL_PRED_U] = CFL_SIGN_POS;
    1913             :   signs_out[CFL_PRED_V] = CFL_SIGN_POS;
    1914             : 
    1915             :   dist = sse[CFL_PRED_U][0] + sse[CFL_PRED_V][0];
    1916             :   dist *= 16;
    1917             :   best_cost = RDCOST(x->rdmult, x->rddiv, cfl->costs[0], dist);
    1918             : 
    1919             :   for (int c = 1; c < CFL_ALPHABET_SIZE; c++) {
    1920             :     const int idx_u = cfl_alpha_codes[c][CFL_PRED_U];
    1921             :     const int idx_v = cfl_alpha_codes[c][CFL_PRED_V];
    1922             :     for (CFL_SIGN_TYPE sign_u = idx_u == 0; sign_u < CFL_SIGNS; sign_u++) {
    1923             :       for (CFL_SIGN_TYPE sign_v = idx_v == 0; sign_v < CFL_SIGNS; sign_v++) {
    1924             :         dist = sse[CFL_PRED_U][idx_u + (sign_u == CFL_SIGN_NEG)] +
    1925             :                sse[CFL_PRED_V][idx_v + (sign_v == CFL_SIGN_NEG)];
    1926             :         dist *= 16;
    1927             :         cost = RDCOST(x->rdmult, x->rddiv, cfl->costs[c], dist);
    1928             :         if (cost < best_cost) {
    1929             :           best_cost = cost;
    1930             :           ind = c;
    1931             :           signs_out[CFL_PRED_U] = sign_u;
    1932             :           signs_out[CFL_PRED_V] = sign_v;
    1933             :         }
    1934             :       }
    1935             :     }
    1936             :   }
    1937             : 
    1938             :   return ind;
    1939             : }
    1940             : 
    1941             : static inline void cfl_update_costs(CFL_CTX *cfl, FRAME_CONTEXT *ec_ctx) {
    1942             :   assert(ec_ctx->cfl_alpha_cdf[CFL_ALPHABET_SIZE - 1] ==
    1943             :          AOM_ICDF(CDF_PROB_TOP));
    1944             :   const int prob_den = CDF_PROB_TOP;
    1945             : 
    1946             :   int prob_num = AOM_ICDF(ec_ctx->cfl_alpha_cdf[0]);
    1947             :   cfl->costs[0] = av1_cost_zero(get_prob(prob_num, prob_den));
    1948             : 
    1949             :   for (int c = 1; c < CFL_ALPHABET_SIZE; c++) {
    1950             :     int sign_bit_cost = (cfl_alpha_codes[c][CFL_PRED_U] != 0) +
    1951             :                         (cfl_alpha_codes[c][CFL_PRED_V] != 0);
    1952             :     prob_num = AOM_ICDF(ec_ctx->cfl_alpha_cdf[c]) -
    1953             :                AOM_ICDF(ec_ctx->cfl_alpha_cdf[c - 1]);
    1954             :     cfl->costs[c] = av1_cost_zero(get_prob(prob_num, prob_den)) +
    1955             :                     av1_cost_literal(sign_bit_cost);
    1956             :   }
    1957             : }
    1958             : 
    1959             : void av1_predict_intra_block_encoder_facade(MACROBLOCK *x,
    1960             :                                             FRAME_CONTEXT *ec_ctx, int plane,
    1961             :                                             int block_idx, int blk_col,
    1962             :                                             int blk_row, TX_SIZE tx_size,
    1963             :                                             BLOCK_SIZE plane_bsize) {
    1964             :   MACROBLOCKD *const xd = &x->e_mbd;
    1965             :   MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
    1966             :   if (plane != AOM_PLANE_Y && mbmi->uv_mode == DC_PRED) {
    1967             :     if (blk_col == 0 && blk_row == 0 && plane == AOM_PLANE_U) {
    1968             :       CFL_CTX *const cfl = xd->cfl;
    1969             :       cfl_update_costs(cfl, ec_ctx);
    1970             :       cfl_dc_pred(xd, plane_bsize, tx_size);
    1971             :       mbmi->cfl_alpha_idx =
    1972             :           cfl_compute_alpha_ind(x, cfl, plane_bsize, mbmi->cfl_alpha_signs);
    1973             :     }
    1974             :   }
    1975             :   av1_predict_intra_block_facade(xd, plane, block_idx, blk_col, blk_row,
    1976             :                                  tx_size);
    1977             : }
    1978             : #endif
    1979             : 
    1980           0 : void av1_encode_intra_block_plane(AV1_COMMON *cm, MACROBLOCK *x,
    1981             :                                   BLOCK_SIZE bsize, int plane,
    1982             :                                   int enable_optimize_b, int mi_row,
    1983             :                                   int mi_col) {
    1984           0 :   const MACROBLOCKD *const xd = &x->e_mbd;
    1985           0 :   ENTROPY_CONTEXT ta[2 * MAX_MIB_SIZE] = { 0 };
    1986           0 :   ENTROPY_CONTEXT tl[2 * MAX_MIB_SIZE] = { 0 };
    1987             : 
    1988           0 :   struct encode_b_args arg = {
    1989           0 :     cm, x, NULL, &xd->mi[0]->mbmi.skip, ta, tl, enable_optimize_b
    1990             :   };
    1991             : 
    1992             : #if CONFIG_CB4X4
    1993           0 :   if (!is_chroma_reference(mi_row, mi_col, bsize,
    1994             :                            xd->plane[plane].subsampling_x,
    1995             :                            xd->plane[plane].subsampling_y))
    1996           0 :     return;
    1997             : #else
    1998             :   (void)mi_row;
    1999             :   (void)mi_col;
    2000             : #endif
    2001             : 
    2002           0 :   if (enable_optimize_b) {
    2003           0 :     const struct macroblockd_plane *const pd = &xd->plane[plane];
    2004           0 :     const TX_SIZE tx_size = get_tx_size(plane, xd);
    2005           0 :     av1_get_entropy_contexts(bsize, tx_size, pd, ta, tl);
    2006             :   }
    2007           0 :   av1_foreach_transformed_block_in_plane(
    2008             :       xd, bsize, plane, encode_block_intra_and_set_context, &arg);
    2009             : }
    2010             : 
    2011             : #if CONFIG_PVQ
    2012             : PVQ_SKIP_TYPE av1_pvq_encode_helper(MACROBLOCK *x, tran_low_t *const coeff,
    2013             :                                     tran_low_t *ref_coeff,
    2014             :                                     tran_low_t *const dqcoeff, uint16_t *eob,
    2015             :                                     const int16_t *quant, int plane,
    2016             :                                     int tx_size, TX_TYPE tx_type, int *rate,
    2017             :                                     int speed, PVQ_INFO *pvq_info) {
    2018             :   const int tx_blk_size = tx_size_wide[tx_size];
    2019             :   daala_enc_ctx *daala_enc = &x->daala_enc;
    2020             :   PVQ_SKIP_TYPE ac_dc_coded;
    2021             :   int coeff_shift = 3 - av1_get_tx_scale(tx_size);
    2022             :   int hbd_downshift = 0;
    2023             :   int rounding_mask;
    2024             :   int pvq_dc_quant;
    2025             :   int use_activity_masking = daala_enc->use_activity_masking;
    2026             :   int tell;
    2027             :   int has_dc_skip = 1;
    2028             :   int i;
    2029             :   int off = od_qm_offset(tx_size, plane ? 1 : 0);
    2030             : 
    2031             :   DECLARE_ALIGNED(16, tran_low_t, coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
    2032             :   DECLARE_ALIGNED(16, tran_low_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
    2033             :   DECLARE_ALIGNED(16, tran_low_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
    2034             : 
    2035             :   DECLARE_ALIGNED(16, int32_t, in_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
    2036             :   DECLARE_ALIGNED(16, int32_t, ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
    2037             :   DECLARE_ALIGNED(16, int32_t, out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
    2038             : 
    2039             :   hbd_downshift = x->e_mbd.bd - 8;
    2040             : 
    2041             :   assert(OD_COEFF_SHIFT >= 4);
    2042             :   // DC quantizer for PVQ
    2043             :   if (use_activity_masking)
    2044             :     pvq_dc_quant =
    2045             :         OD_MAXI(1, (quant[0] << (OD_COEFF_SHIFT - 3) >> hbd_downshift) *
    2046             :                            daala_enc->state
    2047             :                                .pvq_qm_q4[plane][od_qm_get_index(tx_size, 0)] >>
    2048             :                        4);
    2049             :   else
    2050             :     pvq_dc_quant =
    2051             :         OD_MAXI(1, quant[0] << (OD_COEFF_SHIFT - 3) >> hbd_downshift);
    2052             : 
    2053             :   *eob = 0;
    2054             : 
    2055             : #if !CONFIG_ANS
    2056             :   tell = od_ec_enc_tell_frac(&daala_enc->w.ec);
    2057             : #else
    2058             : #error "CONFIG_PVQ currently requires !CONFIG_ANS."
    2059             : #endif
    2060             : 
    2061             :   // Change coefficient ordering for pvq encoding.
    2062             :   od_raster_to_coding_order(coeff_pvq, tx_blk_size, tx_type, coeff,
    2063             :                             tx_blk_size);
    2064             :   od_raster_to_coding_order(ref_coeff_pvq, tx_blk_size, tx_type, ref_coeff,
    2065             :                             tx_blk_size);
    2066             : 
    2067             :   // copy int16 inputs to int32
    2068             :   for (i = 0; i < tx_blk_size * tx_blk_size; i++) {
    2069             :     ref_int32[i] =
    2070             :         AOM_SIGNED_SHL(ref_coeff_pvq[i], OD_COEFF_SHIFT - coeff_shift) >>
    2071             :         hbd_downshift;
    2072             :     in_int32[i] = AOM_SIGNED_SHL(coeff_pvq[i], OD_COEFF_SHIFT - coeff_shift) >>
    2073             :                   hbd_downshift;
    2074             :   }
    2075             : 
    2076             :   if (abs(in_int32[0] - ref_int32[0]) < pvq_dc_quant * 141 / 256) { /* 0.55 */
    2077             :     out_int32[0] = 0;
    2078             :   } else {
    2079             :     out_int32[0] = OD_DIV_R0(in_int32[0] - ref_int32[0], pvq_dc_quant);
    2080             :   }
    2081             : 
    2082             :   ac_dc_coded =
    2083             :       od_pvq_encode(daala_enc, ref_int32, in_int32, out_int32,
    2084             :                     OD_MAXI(1, quant[0] << (OD_COEFF_SHIFT - 3) >>
    2085             :                                    hbd_downshift),  // scale/quantizer
    2086             :                     OD_MAXI(1, quant[1] << (OD_COEFF_SHIFT - 3) >>
    2087             :                                    hbd_downshift),  // scale/quantizer
    2088             :                     plane,
    2089             :                     tx_size, OD_PVQ_BETA[use_activity_masking][plane][tx_size],
    2090             :                     0,  // is_keyframe,
    2091             :                     daala_enc->state.qm + off, daala_enc->state.qm_inv + off,
    2092             :                     speed,  // speed
    2093             :                     pvq_info);
    2094             : 
    2095             :   // Encode residue of DC coeff, if required.
    2096             :   if (!has_dc_skip || out_int32[0]) {
    2097             :     generic_encode(&daala_enc->w, &daala_enc->state.adapt->model_dc[plane],
    2098             :                    abs(out_int32[0]) - has_dc_skip,
    2099             :                    &daala_enc->state.adapt->ex_dc[plane][tx_size][0], 2);
    2100             :   }
    2101             :   if (out_int32[0]) {
    2102             :     aom_write_bit(&daala_enc->w, out_int32[0] < 0);
    2103             :   }
    2104             : 
    2105             :   // need to save quantized residue of DC coeff
    2106             :   // so that final pvq bitstream writing can know whether DC is coded.
    2107             :   if (pvq_info) pvq_info->dq_dc_residue = out_int32[0];
    2108             : 
    2109             :   out_int32[0] = out_int32[0] * pvq_dc_quant;
    2110             :   out_int32[0] += ref_int32[0];
    2111             : 
    2112             :   // copy int32 result back to int16
    2113             :   assert(OD_COEFF_SHIFT > coeff_shift);
    2114             :   rounding_mask = (1 << (OD_COEFF_SHIFT - coeff_shift - 1)) - 1;
    2115             :   for (i = 0; i < tx_blk_size * tx_blk_size; i++) {
    2116             :     out_int32[i] = AOM_SIGNED_SHL(out_int32[i], hbd_downshift);
    2117             :     dqcoeff_pvq[i] = (out_int32[i] + (out_int32[i] < 0) + rounding_mask) >>
    2118             :                      (OD_COEFF_SHIFT - coeff_shift);
    2119             :   }
    2120             : 
    2121             :   // Back to original coefficient order
    2122             :   od_coding_order_to_raster(dqcoeff, tx_blk_size, tx_type, dqcoeff_pvq,
    2123             :                             tx_blk_size);
    2124             : 
    2125             :   *eob = tx_blk_size * tx_blk_size;
    2126             : 
    2127             : #if !CONFIG_ANS
    2128             :   *rate = (od_ec_enc_tell_frac(&daala_enc->w.ec) - tell)
    2129             :           << (AV1_PROB_COST_SHIFT - OD_BITRES);
    2130             : #else
    2131             : #error "CONFIG_PVQ currently requires !CONFIG_ANS."
    2132             : #endif
    2133             :   assert(*rate >= 0);
    2134             : 
    2135             :   return ac_dc_coded;
    2136             : }
    2137             : 
    2138             : void av1_store_pvq_enc_info(PVQ_INFO *pvq_info, int *qg, int *theta, int *k,
    2139             :                             od_coeff *y, int nb_bands, const int *off,
    2140             :                             int *size, int skip_rest, int skip_dir,
    2141             :                             int bs) {  // block size in log_2 -2
    2142             :   int i;
    2143             :   const int tx_blk_size = tx_size_wide[bs];
    2144             : 
    2145             :   for (i = 0; i < nb_bands; i++) {
    2146             :     pvq_info->qg[i] = qg[i];
    2147             :     pvq_info->theta[i] = theta[i];
    2148             :     pvq_info->k[i] = k[i];
    2149             :     pvq_info->off[i] = off[i];
    2150             :     pvq_info->size[i] = size[i];
    2151             :   }
    2152             : 
    2153             :   memcpy(pvq_info->y, y, tx_blk_size * tx_blk_size * sizeof(od_coeff));
    2154             : 
    2155             :   pvq_info->nb_bands = nb_bands;
    2156             :   pvq_info->skip_rest = skip_rest;
    2157             :   pvq_info->skip_dir = skip_dir;
    2158             :   pvq_info->bs = bs;
    2159             : }
    2160             : #endif

Generated by: LCOV version 1.13