Correct the error handling of buffer allocations in encodeframe.c

This CL uses the thread-specific 'x->e_mbd.error_info' for error
reporting when allocating the buffer 'sb_org_stats' in encode_rd_sb().

Also has the below changes w.r.t. hash buffers in
encode_frame_internal().
- Replaced CHECK_MEM_ERROR() with aom_malloc() for local buffer
  allocations.
- Added call to av1_hash_table_destroy() in encoder_destroy().
- Fixed the allocation check for 'p_hash_table->p_lookup_table'.
- Handled the return status of the function calls within
  hash_table_add_to_table().

Bug: aomedia:3276
Change-Id: I9e4f223e670898d5151a6ccf07e8f4ea2f01c6e8
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 9abaf6e..53d3905 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -812,8 +812,8 @@
     SB_FIRST_PASS_STATS *sb_org_stats = NULL;
 
     if (cpi->oxcf.sb_qp_sweep) {
-      CHECK_MEM_ERROR(
-          cm, sb_org_stats,
+      AOM_CHECK_MEM_ERROR(
+          x->e_mbd.error_info, sb_org_stats,
           (SB_FIRST_PASS_STATS *)aom_malloc(sizeof(SB_FIRST_PASS_STATS)));
       av1_backup_sb_state(sb_org_stats, cpi, td, tile_data, mi_row, mi_col);
     }
@@ -1684,6 +1684,19 @@
   }
 }
 
+static void free_block_hash_buffers(uint32_t *block_hash_values[2][2],
+                                    int8_t *is_block_same[2][3]) {
+  for (int k = 0; k < 2; ++k) {
+    for (int j = 0; j < 2; ++j) {
+      aom_free(block_hash_values[k][j]);
+    }
+
+    for (int j = 0; j < 3; ++j) {
+      aom_free(is_block_same[k][j]);
+    }
+  }
+}
+
 /*!\brief Encoder setup(only for the current frame), encoding, and recontruction
  * for a single frame
  *
@@ -1754,26 +1767,34 @@
     // add to hash table
     const int pic_width = cpi->source->y_crop_width;
     const int pic_height = cpi->source->y_crop_height;
-    uint32_t *block_hash_values[2][2];
-    int8_t *is_block_same[2][3];
+    uint32_t *block_hash_values[2][2] = { { NULL } };
+    int8_t *is_block_same[2][3] = { { NULL } };
     int k, j;
+    bool error = false;
 
-    for (k = 0; k < 2; k++) {
-      for (j = 0; j < 2; j++) {
-        CHECK_MEM_ERROR(cm, block_hash_values[k][j],
-                        aom_malloc(sizeof(uint32_t) * pic_width * pic_height));
+    for (k = 0; k < 2 && !error; ++k) {
+      for (j = 0; j < 2; ++j) {
+        block_hash_values[k][j] = (uint32_t *)aom_malloc(
+            sizeof(*block_hash_values[0][0]) * pic_width * pic_height);
+        if (!block_hash_values[k][j]) {
+          error = true;
+          break;
+        }
       }
 
-      for (j = 0; j < 3; j++) {
-        CHECK_MEM_ERROR(cm, is_block_same[k][j],
-                        aom_malloc(sizeof(int8_t) * pic_width * pic_height));
+      for (j = 0; j < 3 && !error; ++j) {
+        is_block_same[k][j] = (int8_t *)aom_malloc(
+            sizeof(*is_block_same[0][0]) * pic_width * pic_height);
+        if (!is_block_same[k][j]) error = true;
       }
     }
 
     av1_hash_table_init(intrabc_hash_info);
-    if (!av1_hash_table_create(&intrabc_hash_info->intrabc_hash_table)) {
+    if (error ||
+        !av1_hash_table_create(&intrabc_hash_info->intrabc_hash_table)) {
+      free_block_hash_buffers(block_hash_values, is_block_same);
       aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
-                         "Error allocating intrabc_hash_table");
+                         "Error allocating intrabc_hash_table and buffers");
     }
     hash_table_created = 1;
     av1_generate_block_2x2_hash_value(intrabc_hash_info, cpi->source,
@@ -1783,7 +1804,6 @@
     const int max_sb_size =
         (1 << (cm->seq_params->mib_size_log2 + MI_SIZE_LOG2));
     int src_idx = 0;
-    bool error = false;
     for (int size = 4; size <= max_sb_size; size *= 2, src_idx = !src_idx) {
       const int dst_idx = !src_idx;
       av1_generate_block_hash_value(
@@ -1801,15 +1821,7 @@
       }
     }
 
-    for (k = 0; k < 2; k++) {
-      for (j = 0; j < 2; j++) {
-        aom_free(block_hash_values[k][j]);
-      }
-
-      for (j = 0; j < 3; j++) {
-        aom_free(is_block_same[k][j]);
-      }
-    }
+    free_block_hash_buffers(block_hash_values, is_block_same);
 
     if (error) {
       aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
diff --git a/av1/encoder/encoder_alloc.h b/av1/encoder/encoder_alloc.h
index 07ac72a..73c1b5b 100644
--- a/av1/encoder/encoder_alloc.h
+++ b/av1/encoder/encoder_alloc.h
@@ -242,6 +242,8 @@
       cpi->td.mb.intrabc_hash_info.hash_value_buffer[i][j] = NULL;
     }
 
+  av1_hash_table_destroy(&cpi->td.mb.intrabc_hash_info.intrabc_hash_table);
+
   aom_free(cm->tpl_mvs);
   cm->tpl_mvs = NULL;
 
diff --git a/av1/encoder/hash_motion.c b/av1/encoder/hash_motion.c
index 164aa09..8b04e22 100644
--- a/av1/encoder/hash_motion.c
+++ b/av1/encoder/hash_motion.c
@@ -128,7 +128,7 @@
   }
   p_hash_table->p_lookup_table =
       (Vector **)aom_calloc(kMaxAddr, sizeof(p_hash_table->p_lookup_table[0]));
-  if (!p_hash_table) return false;
+  if (!p_hash_table->p_lookup_table) return false;
   return true;
 }
 
@@ -141,13 +141,16 @@
     if (p_hash_table->p_lookup_table[hash_value] == NULL) {
       return false;
     }
-    aom_vector_setup(p_hash_table->p_lookup_table[hash_value], 10,
-                     sizeof(curr_block_hash[0]));
-    aom_vector_push_back(p_hash_table->p_lookup_table[hash_value],
-                         curr_block_hash);
+    if (aom_vector_setup(p_hash_table->p_lookup_table[hash_value], 10,
+                         sizeof(curr_block_hash[0])) == VECTOR_ERROR)
+      return false;
+    if (aom_vector_push_back(p_hash_table->p_lookup_table[hash_value],
+                             curr_block_hash) == VECTOR_ERROR)
+      return false;
   } else {
-    aom_vector_push_back(p_hash_table->p_lookup_table[hash_value],
-                         curr_block_hash);
+    if (aom_vector_push_back(p_hash_table->p_lookup_table[hash_value],
+                             curr_block_hash) == VECTOR_ERROR)
+      return false;
   }
   return true;
 }