Fix allocation of workers for enc row-mt

Workers allocated for row based multi-threading of encoder are
now evaluated as minimum of num of threads and total number of
sb rows in the frame to be encoded.

Change-Id: I07501f43514f1ee45dd6637fe56411432930396c
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index ab4687a..29d52ed 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -569,7 +569,8 @@
   const int tile_cols = cm->tile_cols;
   const int tile_rows = cm->tile_rows;
   MultiThreadHandle *multi_thread_ctxt = &cpi->multi_thread_ctxt;
-  int num_workers = cpi->oxcf.max_threads;
+  int num_workers = 0;
+  int total_num_sb_rows = 0;
   int max_sb_rows = 0;
 
   if (cpi->tile_data == NULL || cpi->allocated_tiles < tile_cols * tile_rows) {
@@ -582,10 +583,13 @@
   for (int row = 0; row < tile_rows; row++) {
     for (int col = 0; col < tile_cols; col++) {
       TileDataEnc *tile_data = &cpi->tile_data[row * cm->tile_cols + col];
-      max_sb_rows = AOMMAX(max_sb_rows,
-                           av1_get_sb_rows_in_tile(cm, tile_data->tile_info));
+      int num_sb_rows_in_tile =
+          av1_get_sb_rows_in_tile(cm, tile_data->tile_info);
+      total_num_sb_rows += num_sb_rows_in_tile;
+      max_sb_rows = AOMMAX(max_sb_rows, num_sb_rows_in_tile);
     }
   }
+  num_workers = AOMMIN(cpi->oxcf.max_threads, total_num_sb_rows);
 
   if (multi_thread_ctxt->allocated_tile_cols != tile_cols ||
       multi_thread_ctxt->allocated_tile_rows != tile_rows ||