shithub: libvpx

Download patch

ref: 2981cfac00342163b976f7dbcf72573448f3793b
parent: d0e5b82084fa29e49289edd0c3d860ea67d1f585
author: Angie Chiang <angiebird@google.com>
date: Wed Aug 21 11:49:13 EDT 2019

Add motion_filed_info in VP9_COMP

Call vp9_alloc_motion_field_info and
vp9_free_motion_field_info properly

Change-Id: I79fcb1fd37ee5e95bf7febb728480583ebd5a065

--- a/vp9/encoder/vp9_encoder.c
+++ b/vp9/encoder/vp9_encoder.c
@@ -7138,6 +7138,8 @@
 
   // TODO(angiebird): This probably needs further modifications to support
   // frame scaling later on.
+  vp9_alloc_motion_field_info(&cpi->motion_field_info, MAX_ARF_GOP_SIZE,
+                              mi_rows, mi_cols);
   if (cpi->feature_score_loc_alloc == 0) {
     // The smallest block size of motion field is 4x4, but the mi_unit is 8x8,
     // therefore the number of units is "mi_rows * mi_cols * 4" here.
@@ -7210,6 +7212,7 @@
 static void free_tpl_buffer(VP9_COMP *cpi) {
   int frame;
 #if CONFIG_NON_GREEDY_MV
+  vp9_free_motion_field_info(&cpi->motion_field_info);
   vpx_free(cpi->feature_score_loc_arr);
   vpx_free(cpi->feature_score_loc_sort);
   vpx_free(cpi->feature_score_loc_heap);
--- a/vp9/encoder/vp9_encoder.h
+++ b/vp9/encoder/vp9_encoder.h
@@ -576,6 +576,7 @@
   int kmeans_count_ls[MAX_KMEANS_GROUPS];
   int kmeans_ctr_num;
 #if CONFIG_NON_GREEDY_MV
+  MotionFieldInfo motion_field_info;
   int tpl_ready;
   int feature_score_loc_alloc;
   FEATURE_SCORE_LOC *feature_score_loc_arr;
--- a/vp9/encoder/vp9_non_greedy_mv.c
+++ b/vp9/encoder/vp9_non_greedy_mv.c
@@ -168,9 +168,13 @@
   return (mi_num % mi_bsize) ? mi_num / mi_bsize + 1 : mi_num / mi_bsize;
 }
 
-void vp9_alloc_motion_field_info(MotionFieldInfo *motion_field_info,
-                                 int frame_num, int mi_rows, int mi_cols) {
+Status vp9_alloc_motion_field_info(MotionFieldInfo *motion_field_info,
+                                   int frame_num, int mi_rows, int mi_cols) {
   int frame_idx, rf_idx, square_block_idx;
+  if (motion_field_info->allocated == 1) {
+    // TODO(angiebird): Avoid re-allocate buffer if possible
+    vp9_free_motion_field_info(motion_field_info);
+  }
   motion_field_info->frame_num = frame_num;
   motion_field_info->motion_field_array =
       vpx_calloc(frame_num, sizeof(*motion_field_info->motion_field_array));
@@ -186,14 +190,22 @@
         MotionField *motion_field =
             &motion_field_info
                  ->motion_field_array[frame_idx][rf_idx][square_block_idx];
-        vp9_alloc_motion_field(motion_field, bsize, block_rows, block_cols);
+        Status status =
+            vp9_alloc_motion_field(motion_field, bsize, block_rows, block_cols);
+        if (status == STATUS_FAILED) {
+          assert(0);
+          return STATUS_FAILED;
+        }
       }
     }
   }
+  motion_field_info->allocated = 1;
+  return STATUS_OK;
 }
 
-void vp9_alloc_motion_field(MotionField *motion_field, BLOCK_SIZE bsize,
-                            int block_rows, int block_cols) {
+Status vp9_alloc_motion_field(MotionField *motion_field, BLOCK_SIZE bsize,
+                              int block_rows, int block_cols) {
+  Status status = STATUS_OK;
   motion_field->ready = 0;
   motion_field->bsize = bsize;
   motion_field->block_rows = block_rows;
@@ -200,10 +212,17 @@
   motion_field->block_cols = block_cols;
   motion_field->mf =
       vpx_calloc(block_rows * block_cols, sizeof(*motion_field->mf));
-  assert(motion_field->mf != NULL);
+  if (motion_field->mf == NULL) {
+    assert(0);
+    status = STATUS_FAILED;
+  }
   motion_field->local_structure = vpx_calloc(
       block_rows * block_cols, sizeof(*motion_field->local_structure));
-  assert(motion_field->local_structure != NULL);
+  if (motion_field->local_structure == NULL) {
+    assert(0);
+    status = STATUS_FAILED;
+  }
+  return status;
 }
 
 void vp9_free_motion_field(MotionField *motion_field) {
@@ -213,21 +232,24 @@
 }
 
 void vp9_free_motion_field_info(MotionFieldInfo *motion_field_info) {
-  int frame_idx, rf_idx, square_block_idx;
-  for (frame_idx = 0; frame_idx < motion_field_info->frame_num; ++frame_idx) {
-    for (rf_idx = 0; rf_idx < 3; ++rf_idx) {
-      for (square_block_idx = 0; square_block_idx < SQUARE_BLOCK_SIZES;
-           ++square_block_idx) {
-        MotionField *motion_field =
-            &motion_field_info
-                 ->motion_field_array[frame_idx][rf_idx][square_block_idx];
-        vp9_free_motion_field(motion_field);
+  if (motion_field_info->allocated) {
+    int frame_idx, rf_idx, square_block_idx;
+    for (frame_idx = 0; frame_idx < motion_field_info->frame_num; ++frame_idx) {
+      for (rf_idx = 0; rf_idx < 3; ++rf_idx) {
+        for (square_block_idx = 0; square_block_idx < SQUARE_BLOCK_SIZES;
+             ++square_block_idx) {
+          MotionField *motion_field =
+              &motion_field_info
+                   ->motion_field_array[frame_idx][rf_idx][square_block_idx];
+          vp9_free_motion_field(motion_field);
+        }
       }
     }
+    vpx_free(motion_field_info->motion_field_array);
+    motion_field_info->motion_field_array = NULL;
+    motion_field_info->frame_num = 0;
+    motion_field_info->allocated = 0;
   }
-  vpx_free(motion_field_info->motion_field_array);
-  motion_field_info->motion_field_array = NULL;
-  motion_field_info->frame_num = 0;
 }
 
 static int64_t log2_approximation(int64_t v) {
--- a/vp9/encoder/vp9_non_greedy_mv.h
+++ b/vp9/encoder/vp9_non_greedy_mv.h
@@ -24,6 +24,8 @@
 #define MF_LOCAL_STRUCTURE_SIZE 4
 #define SQUARE_BLOCK_SIZES 4
 
+typedef enum Status { STATUS_OK = 0, STATUS_FAILED = 1 } Status;
+
 typedef struct MotionField {
   int ready;
   BLOCK_SIZE bsize;
@@ -36,6 +38,7 @@
 
 typedef struct MotionFieldInfo {
   int frame_num;
+  int allocated;
   MotionField (*motion_field_array)[3][SQUARE_BLOCK_SIZES];
 } MotionFieldInfo;
 
@@ -77,11 +80,11 @@
   return BLOCK_INVALID;
 }
 
-void vp9_alloc_motion_field_info(MotionFieldInfo *motion_field_info,
-                                 int frame_num, int mi_rows, int mi_cols);
+Status vp9_alloc_motion_field_info(MotionFieldInfo *motion_field_info,
+                                   int frame_num, int mi_rows, int mi_cols);
 
-void vp9_alloc_motion_field(MotionField *motion_field, BLOCK_SIZE bsize,
-                            int block_rows, int block_cols);
+Status vp9_alloc_motion_field(MotionField *motion_field, BLOCK_SIZE bsize,
+                              int block_rows, int block_cols);
 
 void vp9_free_motion_field(MotionField *motion_field);