shithub: libvpx

Download patch

ref: b758ba795a04d551caa3c1af2806cdf0c075803a
parent: 1fa6e2912ca6f7bd7a902e0aff46439e3e117e05
author: Jingning Han <jingning@google.com>
date: Fri Apr 12 06:25:19 EDT 2019

Use qsort to find median value

The list is short enough to use qsort.

Change-Id: I5bb1f2c43eec508bafaf4d1ad7c8a92441f066ce

--- a/vp9/encoder/vp9_encoder.c
+++ b/vp9/encoder/vp9_encoder.c
@@ -8,9 +8,10 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
+#include <limits.h>
 #include <math.h>
 #include <stdio.h>
-#include <limits.h>
+#include <stdlib.h>
 
 #include "./vp9_rtcd.h"
 #include "./vpx_config.h"
@@ -4718,6 +4719,14 @@
 }
 
 // Process the wiener variance in 16x16 block basis.
+static int qsort_comp(const void *elem1, const void *elem2) {
+  int a = *((const int *)elem1);
+  int b = *((const int *)elem2);
+  if (a > b) return 1;
+  if (a < b) return -1;
+  return 0;
+}
+
 static void set_mb_wiener_variance(VP9_COMP *cpi) {
   VP9_COMMON *cm = &cpi->common;
   uint8_t *buffer = cpi->Source->y_buffer;
@@ -4762,7 +4771,7 @@
 
   for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
     for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
-      int idx, hist_count = 0;
+      int idx;
       int16_t median_val = 0;
       uint8_t *mb_buffer =
           buffer + mb_row * block_size * buf_stride + mb_col * block_size;
@@ -4785,18 +4794,13 @@
       wht_fwd_txfm(src_diff, block_size, coeff, tx_size);
 #endif  // CONFIG_VP9_HIGHBITDEPTH
 
-      for (idx = 0; idx < UINT16_MAX; ++idx) cpi->stack_rank_buffer[idx] = 0;
+      coeff[0] = 0;
+      for (idx = 1; idx < coeff_count; ++idx) coeff[idx] = abs(coeff[idx]);
 
-      for (idx = 1; idx < coeff_count; ++idx)
-        ++cpi->stack_rank_buffer[abs(coeff[idx])];
+      qsort(coeff, coeff_count, sizeof(*coeff), qsort_comp);
 
-      for (idx = 0; idx < UINT16_MAX; ++idx) {
-        hist_count += cpi->stack_rank_buffer[idx];
-        if (hist_count >= coeff_count / 2) break;
-      }
-
       // Noise level estimation
-      median_val = idx;
+      median_val = coeff[coeff_count / 2];
 
       // Wiener filter
       for (idx = 1; idx < coeff_count; ++idx) {