From f6886c4b93283ed9ef11f4688b5af25ebe9aca6a Mon Sep 17 00:00:00 2001
From: Yunqing Wang <yunqingwang@google.com>
Date: Wed, 10 Oct 2012 11:27:11 -0700
Subject: [PATCH] post-proc: fix 0 or negative threshold handling

If the threshold(limits) <= 0, skipped filtering and copied the
frame directly. Also, fixed memory allocation checking.

Change-Id: If3d79d5b2bcb71b9777e6eb5cba1384585131e22
---
 vp8/common/alloccommon.c | 36 ++++++++++++++++++++++++------------
 vp8/common/onyxc_int.h   |  1 +
 vp8/common/postproc.c    | 18 +++++++++---------
 vp8/common/postproc.h    |  3 ++-
 vp8/encoder/onyx_if.c    |  4 ++--
 5 files changed, 38 insertions(+), 24 deletions(-)

diff --git a/vp8/common/alloccommon.c b/vp8/common/alloccommon.c
index 86485ac78b..86a0971acd 100644
--- a/vp8/common/alloccommon.c
+++ b/vp8/common/alloccommon.c
@@ -28,6 +28,9 @@ void vp8_de_alloc_frame_buffers(VP8_COMMON *oci)
     vp8_yv12_de_alloc_frame_buffer(&oci->post_proc_buffer);
     if (oci->post_proc_buffer_int_used)
         vp8_yv12_de_alloc_frame_buffer(&oci->post_proc_buffer_int);
+
+    vpx_free(oci->pp_limits_buffer);
+    oci->pp_limits_buffer = NULL;
 #endif
 
     vpx_free(oci->above_context);
@@ -82,18 +85,6 @@ int vp8_alloc_frame_buffers(VP8_COMMON *oci, int width, int height)
         return 1;
     }
 
-#if CONFIG_POSTPROC
-    if (vp8_yv12_alloc_frame_buffer(&oci->post_proc_buffer, width, height, VP8BORDERINPIXELS) < 0)
-    {
-        vp8_de_alloc_frame_buffers(oci);
-        return 1;
-    }
-
-    oci->post_proc_buffer_int_used = 0;
-    vpx_memset(&oci->postproc_state, 0, sizeof(oci->postproc_state));
-    vpx_memset((&oci->post_proc_buffer)->buffer_alloc,128,(&oci->post_proc_buffer)->frame_size);
-#endif
-
     oci->mb_rows = height >> 4;
     oci->mb_cols = width >> 4;
     oci->MBs = oci->mb_rows * oci->mb_cols;
@@ -119,6 +110,27 @@ int vp8_alloc_frame_buffers(VP8_COMMON *oci, int width, int height)
         return 1;
     }
 
+#if CONFIG_POSTPROC
+    if (vp8_yv12_alloc_frame_buffer(&oci->post_proc_buffer, width, height, VP8BORDERINPIXELS) < 0)
+    {
+        vp8_de_alloc_frame_buffers(oci);
+        return 1;
+    }
+
+    oci->post_proc_buffer_int_used = 0;
+    vpx_memset(&oci->postproc_state, 0, sizeof(oci->postproc_state));
+    vpx_memset(oci->post_proc_buffer.buffer_alloc, 128,
+               oci->post_proc_buffer.frame_size);
+
+    /* Allocate buffer to store post-processing filter coefficients. */
+    oci->pp_limits_buffer = vpx_memalign(16, 24 * oci->mb_cols);
+    if (!oci->pp_limits_buffer)
+    {
+        vp8_de_alloc_frame_buffers(oci);
+        return 1;
+    }
+#endif
+
     return 0;
 }
 void vp8_setup_version(VP8_COMMON *cm)
diff --git a/vp8/common/onyxc_int.h b/vp8/common/onyxc_int.h
index 36d75093a9..5325bace44 100644
--- a/vp8/common/onyxc_int.h
+++ b/vp8/common/onyxc_int.h
@@ -87,6 +87,7 @@ typedef struct VP8Common
     YV12_BUFFER_CONFIG post_proc_buffer;
     YV12_BUFFER_CONFIG post_proc_buffer_int;
     int post_proc_buffer_int_used;
+    unsigned char *pp_limits_buffer;   /* post-processing filter coefficients */
 #endif
 
     FRAME_TYPE last_frame_type;  /* Save last frame's frame type for motion search. */
diff --git a/vp8/common/postproc.c b/vp8/common/postproc.c
index 752292eff8..e3820b4843 100644
--- a/vp8/common/postproc.c
+++ b/vp8/common/postproc.c
@@ -338,8 +338,8 @@ void vp8_deblock(VP8_COMMON                 *cm,
 
     /* The pixel thresholds are adjusted according to if or not the macroblock
      * is a skipped block.  */
-    unsigned char *ylimits = (unsigned char *)vpx_memalign(16, 16 * cm->mb_cols);
-    unsigned char *uvlimits = (unsigned char *)vpx_memalign(16, 8 * cm->mb_cols);
+    unsigned char *ylimits = cm->pp_limits_buffer;
+    unsigned char *uvlimits = cm->pp_limits_buffer + 16 * cm->mb_cols;
     (void) low_var_thresh;
     (void) flag;
 
@@ -381,13 +381,15 @@ void vp8_deblock(VP8_COMMON                 *cm,
                 post->v_buffer + 8 * mbr * post->uv_stride, source->uv_stride,
                 post->uv_stride, source->uv_width, uvlimits, 8);
         }
+    } else
+    {
+        vp8_yv12_copy_frame(source, post);
     }
-    vpx_free(ylimits);
-    vpx_free(uvlimits);
 }
 
 #if !(CONFIG_TEMPORAL_DENOISING)
-void vp8_de_noise(YV12_BUFFER_CONFIG         *source,
+void vp8_de_noise(VP8_COMMON                 *cm,
+                  YV12_BUFFER_CONFIG         *source,
                   YV12_BUFFER_CONFIG         *post,
                   int                         q,
                   int                         low_var_thresh,
@@ -397,15 +399,15 @@ void vp8_de_noise(YV12_BUFFER_CONFIG         *source,
     int ppl = (int)(level + .5);
     int mb_rows = source->y_width >> 4;
     int mb_cols = source->y_height >> 4;
-    unsigned char *limits = (unsigned char *)vpx_memalign(16, 16 * mb_cols);
+    unsigned char *limits = cm->pp_limits_buffer;;
     int mbr, mbc;
     (void) post;
     (void) low_var_thresh;
     (void) flag;
 
-    /* TODO: The original code don't filter the 2 outer rows and columns. */
     vpx_memset(limits, (unsigned char)ppl, 16 * mb_cols);
 
+    /* TODO: The original code don't filter the 2 outer rows and columns. */
     for (mbr = 0; mbr < mb_rows; mbr++)
     {
         vp8_post_proc_down_and_across_mb_row(
@@ -422,8 +424,6 @@ void vp8_de_noise(YV12_BUFFER_CONFIG         *source,
             source->v_buffer + 8 * mbr * source->uv_stride,
             source->uv_stride, source->uv_stride, source->uv_width, limits, 8);
     }
-
-    vpx_free(limits);
 }
 #endif
 
diff --git a/vp8/common/postproc.h b/vp8/common/postproc.h
index a156398d2b..495a2c906f 100644
--- a/vp8/common/postproc.h
+++ b/vp8/common/postproc.h
@@ -30,7 +30,8 @@ int vp8_post_proc_frame(struct VP8Common *oci, YV12_BUFFER_CONFIG *dest,
                         vp8_ppflags_t *flags);
 
 
-void vp8_de_noise(YV12_BUFFER_CONFIG         *source,
+void vp8_de_noise(struct VP8Common           *oci,
+                  YV12_BUFFER_CONFIG         *source,
                   YV12_BUFFER_CONFIG         *post,
                   int                         q,
                   int                         low_var_thresh,
diff --git a/vp8/encoder/onyx_if.c b/vp8/encoder/onyx_if.c
index d81bd4323e..4235662f12 100644
--- a/vp8/encoder/onyx_if.c
+++ b/vp8/encoder/onyx_if.c
@@ -3810,11 +3810,11 @@ static void encode_frame_to_data_rate
 
         if (cm->frame_type == KEY_FRAME)
         {
-            vp8_de_noise(cpi->Source, cpi->Source, l , 1,  0);
+            vp8_de_noise(cm, cpi->Source, cpi->Source, l , 1,  0);
         }
         else
         {
-            vp8_de_noise(cpi->Source, cpi->Source, l , 1,  0);
+            vp8_de_noise(cm, cpi->Source, cpi->Source, l , 1,  0);
 
             src = cpi->Source->y_buffer;
 
-- 
GitLab