Assert bsize is allowed for 4-way partitions

In prepare_features_after_part_ab(), assert bsize is allowed for 4-way
partitions. av1_ml_prune_4_partition() already has such an assertion, so
just add a comment for that. However, these two assertions did not fix
the Coverity defects listed below.

Add a comment for the assertion in prune_4_way_partition_search() added
in commit c9f3d68

The variables ab_min_bsize_allowed and part4_min_bsize_allowed are
renamed ab_bsize_thresh and part4_bsize_thresh, respectively, because
these two variables are actually one less than the minimum bsize
allowed for ab and 4-way partitions.

Assert that horz_4_bs and vert_4_bs are not equal to BLOCK_INVALID in
av1_ml_prune_4_partition() and prepare_features_after_part_ab(). These
assertions actually fixed the following Coverity defects:
* CID 323648: In av1_ml_prune_4_partition: Out-of-bounds read from a
  buffer
* CID 323757: In av1_ml_prune_4_partition: Out-of-bounds read from a
  buffer (apparently a duplicate of the above)
* CID 323761: In prepare_features_after_part_ab: Out-of-bounds read from
  a buffer
* CID 323828: In prepare_features_after_part_ab: Out-of-bounds read from
  a buffer (apparently a duplicate of the above)

Bug: aomedia:3499
Change-Id: Idd4ff8cf3e7e6a3656885472af426b6f98489197
diff --git a/av1/encoder/partition_search.c b/av1/encoder/partition_search.c
index 1664d4e..9f2d3d4 100644
--- a/av1/encoder/partition_search.c
+++ b/av1/encoder/partition_search.c
@@ -3958,14 +3958,19 @@
   // Do not prune if there is no valid partition
   if (best_rdcost == INT64_MAX) return 1;
 
-  // Determine min bsize to evaluate ab partitions
-  BLOCK_SIZE ab_min_bsize_allowed = part_sf->ext_partition_eval_thresh;
+  // Determine bsize threshold to evaluate ab partitions
+  BLOCK_SIZE ab_bsize_thresh = part_sf->ext_partition_eval_thresh;
   if (part_sf->ext_part_eval_based_on_cur_best && !must_find_valid_partition &&
       !(curr_best_part == PARTITION_HORZ || curr_best_part == PARTITION_VERT))
-    ab_min_bsize_allowed = BLOCK_128X128;
+    ab_bsize_thresh = BLOCK_128X128;
+
+  // ab partitions are only allowed for square block sizes BLOCK_16X16 or
+  // higher, so ab_bsize_thresh must be large enough to exclude BLOCK_4X4 and
+  // BLOCK_8X8.
+  assert(ab_bsize_thresh >= BLOCK_8X8);
 
   int ab_partition_allowed =
-      part_search_state->do_rectangular_split && bsize > ab_min_bsize_allowed &&
+      part_search_state->do_rectangular_split && bsize > ab_bsize_thresh &&
       av1_blk_has_rows_and_cols(&blk_params) && !prune_ext_part_state;
 
   return ab_partition_allowed;
@@ -4012,18 +4017,20 @@
   // Do not prune if there is no valid partition
   if (best_rdc->rdcost == INT64_MAX) return;
 
-  // Determine min bsize to evaluate 4-way partitions
-  BLOCK_SIZE part4_min_bsize_allowed =
-      cpi->sf.part_sf.ext_partition_eval_thresh;
+  // Determine bsize threshold to evaluate 4-way partitions
+  BLOCK_SIZE part4_bsize_thresh = cpi->sf.part_sf.ext_partition_eval_thresh;
   if (cpi->sf.part_sf.ext_part_eval_based_on_cur_best &&
       !x->must_find_valid_partition && pc_tree->partitioning == PARTITION_NONE)
-    part4_min_bsize_allowed = BLOCK_128X128;
-  assert(part4_min_bsize_allowed >= BLOCK_8X8);
+    part4_bsize_thresh = BLOCK_128X128;
 
-  bool partition4_allowed = part_search_state->do_rectangular_split &&
-                            bsize > part4_min_bsize_allowed &&
-                            av1_blk_has_rows_and_cols(&blk_params) &&
-                            !prune_ext_part_state;
+  // 4-way partitions are only allowed for BLOCK_16X16, BLOCK_32X32, and
+  // BLOCK_64X64, so part4_bsize_thresh must be large enough to exclude
+  // BLOCK_4X4 and BLOCK_8X8.
+  assert(part4_bsize_thresh >= BLOCK_8X8);
+
+  bool partition4_allowed =
+      part_search_state->do_rectangular_split && bsize > part4_bsize_thresh &&
+      av1_blk_has_rows_and_cols(&blk_params) && !prune_ext_part_state;
 
   // Disable 4-way partition search flags for width less than a multiple of the
   // minimum partition width.
diff --git a/av1/encoder/partition_strategy.c b/av1/encoder/partition_strategy.c
index a00cc35..ce06313 100644
--- a/av1/encoder/partition_strategy.c
+++ b/av1/encoder/partition_strategy.c
@@ -1330,6 +1330,7 @@
   int64_t *horz_rd = rect_part_rd[HORZ4];
   int64_t *vert_rd = rect_part_rd[VERT4];
   const NN_CONFIG *nn_config = NULL;
+  // 4-way partitions are only allowed for these three square block sizes.
   switch (bsize) {
     case BLOCK_16X16: nn_config = &av1_4_partition_nnconfig_16; break;
     case BLOCK_32X32: nn_config = &av1_4_partition_nnconfig_32; break;
@@ -1376,6 +1377,10 @@
   {
     BLOCK_SIZE horz_4_bs = get_partition_subsize(bsize, PARTITION_HORZ_4);
     BLOCK_SIZE vert_4_bs = get_partition_subsize(bsize, PARTITION_VERT_4);
+
+    assert(horz_4_bs != BLOCK_INVALID);
+    assert(vert_4_bs != BLOCK_INVALID);
+
     av1_setup_src_planes(x, cpi->source, mi_row, mi_col,
                          av1_num_planes(&cpi->common), bsize);
     const int src_stride = x->plane[0].src.stride;
@@ -1959,12 +1964,19 @@
     features->after_part_ab.f[feature_index++] = rd_ratio;
   }
 
+  // 4-way partitions are only allowed for these three square block sizes.
+  assert(bsize == BLOCK_16X16 || bsize == BLOCK_32X32 || bsize == BLOCK_64X64);
+
   // Get variance of the 1:4 and 4:1 sub-blocks.
   unsigned int horz_4_source_var[SUB_PARTITIONS_PART4] = { 0 };
   unsigned int vert_4_source_var[SUB_PARTITIONS_PART4] = { 0 };
   {
     BLOCK_SIZE horz_4_bs = get_partition_subsize(bsize, PARTITION_HORZ_4);
     BLOCK_SIZE vert_4_bs = get_partition_subsize(bsize, PARTITION_VERT_4);
+
+    assert(horz_4_bs != BLOCK_INVALID);
+    assert(vert_4_bs != BLOCK_INVALID);
+
     av1_setup_src_planes(x, cpi->source, mi_row, mi_col,
                          av1_num_planes(&cpi->common), bsize);
     const int src_stride = x->plane[0].src.stride;