Allocate PC_TREE_SHARED_BUFFERS structure based on max sb size

Modify the memory allocation of PC_TREE_SHARED_BUFFERS structure
based on max sb size and number of planes.

This CL is bit-exact with no impact on encode-time.

Avg memory savings for RTC configuration, cpu 5.
                        Memory Reduction (%)
                        Single      Multi
Resolution    Tile      thread      thread
640x360       2x1        1.59       2.75 (2 threads)
832x480       2x1        1.23       1.89 (2 threads)
1280x720      2x2        0.43       0.99 (4 threads)
1920x1080     4x2        0.19       1.05 (8 threads)

Change-Id: I83febf0d9bc3ae4c02c8ff754ad2a531172a4b87
diff --git a/av1/encoder/context_tree.c b/av1/encoder/context_tree.c
index 230540a..ccd6b9d 100644
--- a/av1/encoder/context_tree.c
+++ b/av1/encoder/context_tree.c
@@ -38,9 +38,15 @@
 }
 
 void av1_setup_shared_coeff_buffer(struct aom_internal_error_info *error,
-                                   PC_TREE_SHARED_BUFFERS *shared_bufs) {
-  for (int i = 0; i < 3; i++) {
-    const int max_num_pix = MAX_SB_SIZE * MAX_SB_SIZE;
+                                   PC_TREE_SHARED_BUFFERS *shared_bufs,
+                                   const SequenceHeader *const seq_params,
+                                   int num_planes) {
+  const int max_sb_square_y = 1 << num_pels_log2_lookup[seq_params->sb_size];
+  const int max_sb_square_uv = max_sb_square_y >> (seq_params->subsampling_x +
+                                                   seq_params->subsampling_y);
+  for (int i = 0; i < num_planes; i++) {
+    const int max_num_pix =
+        (i == AOM_PLANE_Y) ? max_sb_square_y : max_sb_square_uv;
     AOM_CHECK_MEM_ERROR(error, shared_bufs->coeff_buf[i],
                         aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
     AOM_CHECK_MEM_ERROR(error, shared_bufs->qcoeff_buf[i],
diff --git a/av1/encoder/context_tree.h b/av1/encoder/context_tree.h
index 966ebfe..14c433b 100644
--- a/av1/encoder/context_tree.h
+++ b/av1/encoder/context_tree.h
@@ -103,7 +103,9 @@
 } SIMPLE_MOTION_DATA_TREE;
 
 void av1_setup_shared_coeff_buffer(struct aom_internal_error_info *error,
-                                   PC_TREE_SHARED_BUFFERS *shared_bufs);
+                                   PC_TREE_SHARED_BUFFERS *shared_bufs,
+                                   const SequenceHeader *const seq_params,
+                                   int num_planes);
 void av1_free_shared_coeff_buffer(PC_TREE_SHARED_BUFFERS *shared_bufs);
 
 PC_TREE *av1_alloc_pc_tree_node(BLOCK_SIZE bsize);
diff --git a/av1/encoder/encoder_alloc.h b/av1/encoder/encoder_alloc.h
index dd3dd53..5daf50b 100644
--- a/av1/encoder/encoder_alloc.h
+++ b/av1/encoder/encoder_alloc.h
@@ -85,7 +85,8 @@
   CHECK_MEM_ERROR(cm, cpi->td.mb.dv_costs,
                   (IntraBCMVCosts *)aom_malloc(sizeof(*cpi->td.mb.dv_costs)));
 
-  av1_setup_shared_coeff_buffer(cm->error, &cpi->td.shared_coeff_buf);
+  av1_setup_shared_coeff_buffer(cm->error, &cpi->td.shared_coeff_buf,
+                                cm->seq_params, av1_num_planes(cm));
   av1_setup_sms_tree(cpi, &cpi->td);
   cpi->td.firstpass_ctx =
       av1_alloc_pmc(cpi, BLOCK_16X16, &cpi->td.shared_coeff_buf);
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index 48df376..baf9c59 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -616,8 +616,9 @@
 #endif
 
       // Set up shared coeff buffers.
-      av1_setup_shared_coeff_buffer(&ppi->error,
-                                    &thread_data->td->shared_coeff_buf);
+      av1_setup_shared_coeff_buffer(
+          &ppi->error, &thread_data->td->shared_coeff_buf, &ppi->seq_params,
+          av1_num_planes(&ppi->cpi->common));
       AOM_CHECK_MEM_ERROR(
           &ppi->error, thread_data->td->tmp_conv_dst,
           aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE *