Merge "Resolve merge issues with --enable-pvq" into nextgenv2
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index d0907ec..de40666 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -355,9 +355,8 @@
 
   od_pvq_decode(dec, ref_int32, out_int32, (int)quant[1] >> quant_shift, pli,
                 bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
-                OD_ROBUST_STREAM,
-                is_keyframe, &flags, ac_dc_coded, dec->state.qm + off,
-                dec->state.qm_inv + off);
+                OD_ROBUST_STREAM, is_keyframe, &flags, ac_dc_coded,
+                dec->state.qm + off, dec->state.qm_inv + off);
 
   // copy int32 result back to int16
   for (i = 0; i < blk_size * blk_size; i++) dqcoeff_pvq[i] = out_int32[i];
@@ -379,9 +378,9 @@
   return eob;
 }
 
-static int av1_pvq_decode_helper2(
-    MACROBLOCKD *const xd, MB_MODE_INFO *const mbmi, int plane, int row,
-    int col, TX_SIZE tx_size, TX_TYPE tx_type ) {
+static int av1_pvq_decode_helper2(MACROBLOCKD *const xd,
+                                  MB_MODE_INFO *const mbmi, int plane, int row,
+                                  int col, TX_SIZE tx_size, TX_TYPE tx_type) {
   struct macroblockd_plane *const pd = &xd->plane[plane];
   // transform block size in pixels
   int tx_blk_size = tx_size_wide[tx_size];
@@ -411,6 +410,8 @@
     int seg_id = mbmi->segment_id;
     int16_t *quant;
     FWD_TXFM_PARAM fwd_txfm_param;
+    // ToDo(yaowu): figure out how to initialize this
+    int max_scan_line = 0;
 
     for (j = 0; j < tx_blk_size; j++)
       for (i = 0; i < tx_blk_size; i++) {
@@ -436,8 +437,8 @@
     for (j = 0; j < tx_blk_size; j++)
       for (i = 0; i < tx_blk_size; i++) dst[j * pd->dst.stride + i] = 0;
 
-    inverse_transform_block(xd, plane, tx_type, tx_size, dst,
-                            pd->dst.stride, eob);
+    inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
+                            max_scan_line, eob);
   }
 
   return eob;
@@ -553,7 +554,6 @@
 #endif
                                    int segment_id, int plane, int row, int col,
                                    TX_SIZE tx_size) {
-  struct macroblockd_plane *const pd = &xd->plane[plane];
   PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
   int block_idx = (row << 1) + col;
   TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
@@ -561,6 +561,9 @@
   int eob;
   (void)cm;
   (void)r;
+  (void)segment_id;
+#else
+  struct macroblockd_plane *const pd = &xd->plane[plane];
 #endif
 
 #if !CONFIG_PVQ
@@ -577,7 +580,8 @@
                             &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
                             pd->dst.stride, max_scan_line, eob);
 #else
-  eob = av1_pvq_decode_helper2(xd, mbmi, plane, row, col, tx_size, tx_type);
+  eob = av1_pvq_decode_helper2(xd, &xd->mi[0]->mbmi, plane, row, col, tx_size,
+                               tx_type);
 #endif
   return eob;
 }
@@ -3032,7 +3036,7 @@
               : NULL;
       av1_zero(td->dqcoeff);
 #if CONFIG_PVQ
-      av1_zero(tile_data->pvq_ref_coeff);
+      av1_zero(td->pvq_ref_coeff);
 #endif
       av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
 #if !CONFIG_ANS
@@ -3394,7 +3398,7 @@
 #endif
                              twd->dqcoeff);
 #if CONFIG_PVQ
-      daala_dec_init(&twd->xd.daala_dec, &twd->bit_reader.ec);
+        daala_dec_init(&twd->xd.daala_dec, &twd->bit_reader.ec);
 #endif
 #if CONFIG_PALETTE
         twd->xd.plane[0].color_index_map = twd->color_index_map[0];
diff --git a/av1/encoder/context_tree.c b/av1/encoder/context_tree.c
index 239c365..5a26d9b 100644
--- a/av1/encoder/context_tree.c
+++ b/av1/encoder/context_tree.c
@@ -78,8 +78,8 @@
       aom_free(ctx->dqcoeff[i][k]);
       ctx->dqcoeff[i][k] = 0;
 #if CONFIG_PVQ
-    aom_free(ctx->pvq_ref_coeff[i]);
-    ctx->pvq_ref_coeff[i] = 0;
+      aom_free(ctx->pvq_ref_coeff[i]);
+      ctx->pvq_ref_coeff[i] = 0;
 #endif
       aom_free(ctx->eobs[i][k]);
       ctx->eobs[i][k] = 0;
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index bb1c151..0a30186 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -2084,11 +2084,11 @@
 
 static void restore_context(MACROBLOCK *x,
                             const RD_SEARCH_MACROBLOCK_CONTEXT *ctx, int mi_row,
-                            int mi_col, 
+                            int mi_col,
 #if CONFIG_PVQ
                             od_rollback_buffer *rdo_buf,
-#endif														
-							BLOCK_SIZE bsize) {
+#endif
+                            BLOCK_SIZE bsize) {
   MACROBLOCKD *xd = &x->e_mbd;
   int p;
   const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
@@ -2124,11 +2124,11 @@
 }
 
 static void save_context(const MACROBLOCK *x, RD_SEARCH_MACROBLOCK_CONTEXT *ctx,
-                         int mi_row, int mi_col, 
+                         int mi_row, int mi_col,
 #if CONFIG_PVQ
                          od_rollback_buffer *rdo_buf,
 #endif
-						 BLOCK_SIZE bsize) {
+                         BLOCK_SIZE bsize) {
   const MACROBLOCKD *xd = &x->e_mbd;
   int p;
   const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
@@ -4499,8 +4499,9 @@
 #if CONFIG_PVQ
         // This will be dynamically increased as more pvq block is encoded.
         tile_data->pvq_q.buf_len = 1000;
-        CHECK_MEM_ERROR(cm, tile_data->pvq_q.buf,
-                        aom_malloc(tile_data->pvq_q.buf_len * sizeof(PVQ_INFO)));
+        CHECK_MEM_ERROR(
+            cm, tile_data->pvq_q.buf,
+            aom_malloc(tile_data->pvq_q.buf_len * sizeof(PVQ_INFO)));
         tile_data->pvq_q.curr_pos = 0;
 #endif
       }
diff --git a/av1/encoder/encodemb.c b/av1/encoder/encodemb.c
index 83d463b..4d035ae 100644
--- a/av1/encoder/encodemb.c
+++ b/av1/encoder/encodemb.c
@@ -62,7 +62,6 @@
   tran_low_t dqc;
 } av1_token_state;
 
-#if !CONFIG_PVQ
 // These numbers are empirically obtained.
 static const int plane_rd_mult[REF_TYPES][PLANE_TYPES] = {
   { 10, 6 }, { 8, 5 },
@@ -404,7 +403,6 @@
   assert(final_eob <= default_eob);
   return final_eob;
 }
-#endif  // !CONFIG_PVG
 
 #if CONFIG_AOM_HIGHBITDEPTH
 typedef enum QUANT_FUNC {
@@ -1166,7 +1164,7 @@
                             BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                             void *arg) {
   struct encode_b_args *const args = arg;
-#if !CONFIG_PVG
+#if !CONFIG_PVQ
   AV1_COMMON *cm = args->cm;
 #endif
   MACROBLOCK *const x = args->x;
@@ -1431,8 +1429,7 @@
       (int)quant[1] >> quant_shift,  // scale/quantizer
       // TODO(yushin): Instead of 0,
       //   use daala_enc->use_activity_masking for activity masking.
-      plane, tx_size, OD_PVQ_BETA[0][plane][tx_size],
-      OD_ROBUST_STREAM,
+      plane, tx_size, OD_PVQ_BETA[0][plane][tx_size], OD_ROBUST_STREAM,
       0,        // is_keyframe,
       0, 0, 0,  // q_scaling, bx, by,
       daala_enc->state.qm + off, daala_enc->state.qm_inv + off,
diff --git a/av1/encoder/encodemb.h b/av1/encoder/encodemb.h
index 3c496d9..2f2b93b 100644
--- a/av1/encoder/encodemb.h
+++ b/av1/encoder/encodemb.h
@@ -13,6 +13,7 @@
 #define AV1_ENCODER_ENCODEMB_H_
 
 #include "./aom_config.h"
+#include "av1/common/onyxc_int.h"
 #include "av1/encoder/block.h"
 
 #ifdef __cplusplus
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index 2f6731e..52a1cce 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -536,7 +536,8 @@
     od_adapt_ctx *adapt;
 
     pvq_q.buf_len = 5000;
-    CHECK_MEM_ERROR(cm, pvq_q.buf, aom_malloc(pvq_q.buf_len * sizeof(PVQ_INFO)));
+    CHECK_MEM_ERROR(cm, pvq_q.buf,
+                    aom_malloc(pvq_q.buf_len * sizeof(PVQ_INFO)));
     pvq_q.curr_pos = 0;
     x->pvq_coded = 0;
 
diff --git a/av1/encoder/pvq_encoder.c b/av1/encoder/pvq_encoder.c
index b0ee102..611f0b6 100644
--- a/av1/encoder/pvq_encoder.c
+++ b/av1/encoder/pvq_encoder.c
@@ -25,7 +25,7 @@
 #include "av1/common/partition.h"
 #include "av1/common/pvq_state.h"
 #include "av1/encoder/encodemb.h"
-#include "pvq_encoder.h"
+#include "av1/encoder/pvq_encoder.h"
 
 #define OD_PVQ_RATE_APPROX (0)
 /*Shift to ensure that the upper bound (i.e. for the max blocksize) of the
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index c0abb68..9e5419f 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -68,7 +68,6 @@
 #endif
 #endif
 
-
 #if CONFIG_EXT_REFS
 
 #define LAST_FRAME_MODE_MASK                                      \
@@ -1031,7 +1030,7 @@
     tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
     tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
 #if CONFIG_PVQ
-  tran_low_t *ref_coeff = BLOCK_OFFSET(pd->pvq_ref_coeff, block);
+    tran_low_t *ref_coeff = BLOCK_OFFSET(pd->pvq_ref_coeff, block);
 #endif
 #if CONFIG_AOM_HIGHBITDEPTH
     const int bd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd : 8;
diff --git a/av1/encoder/tokenize.c b/av1/encoder/tokenize.c
index 50735ad..5aafa79 100644
--- a/av1/encoder/tokenize.c
+++ b/av1/encoder/tokenize.c
@@ -320,6 +320,7 @@
   int this_rate;
 };
 
+#if !CONFIG_PVQ
 static void cost_coeffs_b(int plane, int block, int blk_row, int blk_col,
                           BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
   struct tokenize_b_args *const args = arg;
@@ -343,7 +344,6 @@
   av1_set_contexts(xd, pd, tx_size, p->eobs[block] > 0, blk_col, blk_row);
 }
 
-#if !CONFIG_PVQ
 static void set_entropy_context_b(int plane, int block, int blk_row,
                                   int blk_col, BLOCK_SIZE plane_bsize,
                                   TX_SIZE tx_size, void *arg) {
@@ -567,9 +567,10 @@
   PVQ_QUEUE *q = x->pvq_q;
   if (q->curr_pos >= q->buf_len) {
     q->buf_len = 2 * q->buf_len + 1;
-    CHECK_MEM_ERROR(cm, q->buf, aom_realloc(q->buf, q->buf_len * sizeof(PVQ_INFO)));
+    CHECK_MEM_ERROR(cm, q->buf,
+                    aom_realloc(q->buf, q->buf_len * sizeof(PVQ_INFO)));
   }
-  //memcpy(q->buf + q->curr_pos, pvq, sizeof(PVQ_INFO));
+  // memcpy(q->buf + q->curr_pos, pvq, sizeof(PVQ_INFO));
   OD_COPY(q->buf + q->curr_pos, pvq, 1);
   ++q->curr_pos;
 }
@@ -594,7 +595,7 @@
 
   assert(block < MAX_PVQ_BLOCKS_IN_SB);
   pvq_info = &x->pvq[block][plane];
-  add_pvq_block((AV1_COMMON *const)cm, x, pvq_info);
+  add_pvq_block((AV1_COMMON * const)cm, x, pvq_info);
 }
 #endif
 #if CONFIG_VAR_TX