63d2ed
From 263682c9a29395055f3b3afe2d97be1828a6223f Mon Sep 17 00:00:00 2001
63d2ed
From: Jerome Jiang <jianj@google.com>
63d2ed
Date: Thu, 30 Jun 2022 13:48:56 -0400
63d2ed
Subject: [PATCH] Fix bug with smaller width bigger size
63d2ed
63d2ed
Fixed previous patch that clusterfuzz failed on.
63d2ed
63d2ed
Bug: webm:1642
63d2ed
Change-Id: If0e08e72abd2e042efe4dcfac21e4cc51afdfdb9
63d2ed
---
63d2ed
 test/resize_test.cc          | 11 +++--------
63d2ed
 vp9/common/vp9_alloccommon.c | 13 ++++++-------
63d2ed
 vp9/encoder/vp9_encoder.c    | 27 +++++++++++++++++++++++++--
63d2ed
 3 files changed, 34 insertions(+), 17 deletions(-)
63d2ed
63d2ed
diff --git a/test/resize_test.cc b/test/resize_test.cc
63d2ed
index fd1c2a92de6..20ad2229b46 100644
63d2ed
--- a/test/resize_test.cc
63d2ed
+++ b/test/resize_test.cc
63d2ed
@@ -102,11 +102,8 @@ void ScaleForFrameNumber(unsigned int frame, unsigned int initial_w,
63d2ed
     if (frame < 30) {
63d2ed
       return;
63d2ed
     }
63d2ed
-    if (frame < 100) {
63d2ed
-      *w = initial_w * 7 / 10;
63d2ed
-      *h = initial_h * 16 / 10;
63d2ed
-      return;
63d2ed
-    }
63d2ed
+    *w = initial_w * 7 / 10;
63d2ed
+    *h = initial_h * 16 / 10;
63d2ed
     return;
63d2ed
   }
63d2ed
   if (frame < 10) {
63d2ed
@@ -559,9 +556,7 @@ TEST_P(ResizeRealtimeTest, TestExternalResizeWorks) {
63d2ed
   }
63d2ed
 }
63d2ed
 
63d2ed
-// TODO(https://crbug.com/webm/1642): This causes a segfault in
63d2ed
-// init_encode_frame_mb_context().
63d2ed
-TEST_P(ResizeRealtimeTest, DISABLED_TestExternalResizeSmallerWidthBiggerSize) {
63d2ed
+TEST_P(ResizeRealtimeTest, TestExternalResizeSmallerWidthBiggerSize) {
63d2ed
   ResizingVideoSource video;
63d2ed
   video.flag_codec_ = true;
63d2ed
   video.smaller_width_larger_size_ = true;
63d2ed
diff --git a/vp9/common/vp9_alloccommon.c b/vp9/common/vp9_alloccommon.c
63d2ed
index e53883f621d..9e73e40ea09 100644
63d2ed
--- a/vp9/common/vp9_alloccommon.c
63d2ed
+++ b/vp9/common/vp9_alloccommon.c
63d2ed
@@ -135,13 +135,6 @@ int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
63d2ed
     cm->free_mi(cm);
63d2ed
     if (cm->alloc_mi(cm, new_mi_size)) goto fail;
63d2ed
   }
63d2ed
-
63d2ed
-  if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
63d2ed
-    // Create the segmentation map structure and set to 0.
63d2ed
-    free_seg_map(cm);
63d2ed
-    if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail;
63d2ed
-  }
63d2ed
-
63d2ed
   if (cm->above_context_alloc_cols < cm->mi_cols) {
63d2ed
     vpx_free(cm->above_context);
63d2ed
     cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
63d2ed
@@ -156,6 +149,12 @@ int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
63d2ed
     cm->above_context_alloc_cols = cm->mi_cols;
63d2ed
   }
63d2ed
 
63d2ed
+  if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
63d2ed
+    // Create the segmentation map structure and set to 0.
63d2ed
+    free_seg_map(cm);
63d2ed
+    if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail;
63d2ed
+  }
63d2ed
+
63d2ed
   if (vp9_alloc_loop_filter(cm)) goto fail;
63d2ed
 
63d2ed
   return 0;
63d2ed
diff --git a/vp9/encoder/vp9_encoder.c b/vp9/encoder/vp9_encoder.c
63d2ed
index 69a4e3c314f..e3ba294c32f 100644
63d2ed
--- a/vp9/encoder/vp9_encoder.c
63d2ed
+++ b/vp9/encoder/vp9_encoder.c
63d2ed
@@ -2047,6 +2047,17 @@ static void alloc_copy_partition_data(VP9_COMP *cpi) {
63d2ed
   }
63d2ed
 }
63d2ed
 
63d2ed
+static void free_copy_partition_data(VP9_COMP *cpi) {
63d2ed
+  vpx_free(cpi->prev_partition);
63d2ed
+  cpi->prev_partition = NULL;
63d2ed
+  vpx_free(cpi->prev_segment_id);
63d2ed
+  cpi->prev_segment_id = NULL;
63d2ed
+  vpx_free(cpi->prev_variance_low);
63d2ed
+  cpi->prev_variance_low = NULL;
63d2ed
+  vpx_free(cpi->copied_frame_cnt);
63d2ed
+  cpi->copied_frame_cnt = NULL;
63d2ed
+}
63d2ed
+
63d2ed
 void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
63d2ed
   VP9_COMMON *const cm = &cpi->common;
63d2ed
   RATE_CONTROL *const rc = &cpi->rc;
63d2ed
@@ -2126,6 +2137,8 @@ void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
63d2ed
     new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
63d2ed
     if (cm->mi_alloc_size < new_mi_size) {
63d2ed
       vp9_free_context_buffers(cm);
63d2ed
+      vp9_free_pc_tree(&cpi->td);
63d2ed
+      vpx_free(cpi->mbmi_ext_base);
63d2ed
       alloc_compressor_data(cpi);
63d2ed
       realloc_segmentation_maps(cpi);
63d2ed
       cpi->initial_width = cpi->initial_height = 0;
63d2ed
@@ -2144,8 +2157,18 @@ void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
63d2ed
     update_frame_size(cpi);
63d2ed
 
63d2ed
   if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
63d2ed
-    memset(cpi->consec_zero_mv, 0,
63d2ed
-           cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
63d2ed
+    vpx_free(cpi->consec_zero_mv);
63d2ed
+    CHECK_MEM_ERROR(
63d2ed
+        &cm->error, cpi->consec_zero_mv,
63d2ed
+        vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->consec_zero_mv)));
63d2ed
+
63d2ed
+    vpx_free(cpi->skin_map);
63d2ed
+    CHECK_MEM_ERROR(
63d2ed
+        &cm->error, cpi->skin_map,
63d2ed
+        vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(cpi->skin_map[0])));
63d2ed
+
63d2ed
+    free_copy_partition_data(cpi);
63d2ed
+    alloc_copy_partition_data(cpi);
63d2ed
     if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
63d2ed
       vp9_cyclic_refresh_reset_resize(cpi);
63d2ed
     rc->rc_1_frame = 0;