Fix issues for palette content in enc row-mt

For palette contents, color_map_cdf holds the context pointer
which would be used during pack_bitstream. In case of row-mt,
each thread has its own context memory. So, pointer assignment
in cost_and_tokenize_map would result in incorrect memory access
while packing the bitstream. Updating the color_map_cdf pointer
based on accurate tile context that would be used during packing
of bitstream will help to overcome this issue.

Change-Id: I139bfa764054781a1ca39c2e97a89caa6f38733a
diff --git a/av1/common/blockd.h b/av1/common/blockd.h
index 4bb2b92..cdfb3c9 100644
--- a/av1/common/blockd.h
+++ b/av1/common/blockd.h
@@ -1140,6 +1140,7 @@
   int plane_height;
   uint8_t *color_map;
   MapCdf map_cdf;
+  MapCdf map_pb_cdf;
   ColorCost color_cost;
 } Av1ColorMapParam;
 
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index 9049c25..aa6bb18 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -280,6 +280,12 @@
   uint8_t *tmp_obmc_bufs[2];
 
   FRAME_CONTEXT *backup_tile_ctx;
+  // This context will be used to update color_map_cdf pointer which would be
+  // used during pack bitstream. For single thread and tile-multithreading case
+  // this ponter will be same as xd->tile_ctx, but for the case of row-mt:
+  // xd->tile_ctx will point to a temporary context while tile_pb_ctx will point
+  // to the accurate tile context.
+  FRAME_CONTEXT *tile_pb_ctx;
 
 #if CONFIG_COLLECT_INTER_MODE_RD_STATS
   struct inter_modes_info *inter_modes_info;
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 2b3dde6..2e9a922 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -5121,6 +5121,7 @@
           &cpi->tile_data[tile_row * cm->tile_cols + tile_col];
       cpi->td.intrabc_used = 0;
       cpi->td.mb.e_mbd.tile_ctx = &this_tile->tctx;
+      cpi->td.mb.tile_pb_ctx = &this_tile->tctx;
       cpi->td.mb.backup_tile_ctx = &this_tile->backup_tctx;
       av1_encode_tile(cpi, &cpi->td, tile_row, tile_col);
       cpi->intrabc_used |= cpi->td.intrabc_used;
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index 3b0e536..6e963f0 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -275,6 +275,7 @@
     ThreadData *td = thread_data->td;
 
     td->mb.e_mbd.tile_ctx = td->tctx;
+    td->mb.tile_pb_ctx = &this_tile->tctx;
     td->mb.backup_tile_ctx = &this_tile->backup_tctx;
     if (current_mi_row == this_tile->tile_info.mi_row_start)
       memcpy(td->mb.e_mbd.tile_ctx, &this_tile->tctx, sizeof(FRAME_CONTEXT));
@@ -320,6 +321,7 @@
         &cpi->tile_data[tile_row * cm->tile_cols + tile_col];
     thread_data->td->tctx = &this_tile->tctx;
     thread_data->td->mb.e_mbd.tile_ctx = thread_data->td->tctx;
+    thread_data->td->mb.tile_pb_ctx = thread_data->td->tctx;
     thread_data->td->mb.backup_tile_ctx = &this_tile->backup_tctx;
     av1_encode_tile(cpi, thread_data->td, tile_row, tile_col);
   }
diff --git a/av1/encoder/tokenize.c b/av1/encoder/tokenize.c
index af4cc70..4c12e68d 100644
--- a/av1/encoder/tokenize.c
+++ b/av1/encoder/tokenize.c
@@ -32,6 +32,7 @@
                                  FRAME_COUNTS *counts) {
   const uint8_t *const color_map = param->color_map;
   MapCdf map_cdf = param->map_cdf;
+  MapCdf map_pb_cdf = param->map_pb_cdf;
   ColorCost color_cost = param->color_cost;
   const int plane_block_width = param->plane_width;
   const int rows = param->rows;
@@ -55,7 +56,7 @@
         this_rate += (*color_cost)[palette_size_idx][color_ctx][color_new_idx];
       } else {
         (*t)->token = color_new_idx;
-        (*t)->color_map_cdf = map_cdf[palette_size_idx][color_ctx];
+        (*t)->color_map_cdf = map_pb_cdf[palette_size_idx][color_ctx];
         ++(*t);
         if (allow_update_cdf)
           update_cdf(map_cdf[palette_size_idx][color_ctx], color_new_idx, n);
@@ -83,6 +84,8 @@
   params->color_map = xd->plane[plane].color_index_map;
   params->map_cdf = plane ? xd->tile_ctx->palette_uv_color_index_cdf
                           : xd->tile_ctx->palette_y_color_index_cdf;
+  params->map_pb_cdf = plane ? x->tile_pb_ctx->palette_uv_color_index_cdf
+                             : x->tile_pb_ctx->palette_y_color_index_cdf;
   params->color_cost =
       plane ? &x->palette_uv_color_cost : &x->palette_y_color_cost;
   params->n_colors = pmi->palette_size[plane];