shithub: libvpx

Download patch

ref: d887413eca3b683c2f3d110c01e7e680c7072548
parent: 5df8d048c67af0e1f5f43eba1eae45fdf16805ff
author: Angie Chiang <angiebird@google.com>
date: Thu Mar 14 08:28:25 EDT 2019

Fix race condition in wiener_var_rd_mult

Change-Id: Id5e9c2cbfe35809ac99a3bc9ba93cf462a6b1a34

--- a/vp9/encoder/vp9_encodeframe.c
+++ b/vp9/encoder/vp9_encodeframe.c
@@ -3595,9 +3595,15 @@
     for (col = mb_col_start; col < mb_col_end; ++col)
       wiener_variance += cpi->mb_wiener_variance[row * cm->mb_cols + col];
 
+#if CONFIG_MULTITHREAD
+  pthread_mutex_lock(&cpi->kmeans_mutex);
+#endif  // CONFIG_MULTITHREAD
   kmeans_data = &cpi->kmeans_data_arr[cpi->kmeans_data_size++];
   kmeans_data->value = log(1 + wiener_variance);
   kmeans_data->pos = mi_row * cpi->kmeans_data_stride + mi_col;
+#if CONFIG_MULTITHREAD
+  pthread_mutex_unlock(&cpi->kmeans_mutex);
+#endif  // CONFIG_MULTITHREAD
   if (wiener_variance)
     wiener_variance /=
         (mb_row_end - mb_row_start) * (mb_col_end - mb_col_start);
--- a/vp9/encoder/vp9_encoder.c
+++ b/vp9/encoder/vp9_encoder.c
@@ -2593,6 +2593,9 @@
 #endif
 
   if (cpi->kmeans_data_arr_alloc) {
+#if CONFIG_MULTITHREAD
+    pthread_mutex_destroy(&cpi->kmeans_mutex);
+#endif
     vpx_free(cpi->kmeans_data_arr);
   }
 
@@ -7263,6 +7266,9 @@
   if (cpi->kmeans_data_arr_alloc == 0) {
     const int mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
     const int mi_rows = mi_cols_aligned_to_sb(cm->mi_rows);
+#if CONFIG_MULTITHREAD
+    pthread_mutex_init(&cpi->kmeans_mutex, NULL);
+#endif
     CHECK_MEM_ERROR(
         cm, cpi->kmeans_data_arr,
         vpx_calloc(mi_rows * mi_cols, sizeof(*cpi->kmeans_data_arr)));
--- a/vp9/encoder/vp9_encoder.h
+++ b/vp9/encoder/vp9_encoder.h
@@ -602,6 +602,9 @@
   TplDepFrame tpl_stats[MAX_ARF_GOP_SIZE];
   YV12_BUFFER_CONFIG *tpl_recon_frames[REF_FRAMES];
   EncFrameBuf enc_frame_buf[REF_FRAMES];
+#if CONFIG_MULTITHREAD
+  pthread_mutex_t kmeans_mutex;
+#endif
   int kmeans_data_arr_alloc;
   KMEANS_DATA *kmeans_data_arr;
   int kmeans_data_size;