1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include "av1/common/warped_motion.h"
13 #include "av1/common/thread_common.h"
14 
15 #include "av1/encoder/bitstream.h"
16 #include "av1/encoder/encodeframe.h"
17 #include "av1/encoder/encoder.h"
18 #include "av1/encoder/encoder_alloc.h"
19 #include "av1/encoder/encodeframe_utils.h"
20 #include "av1/encoder/ethread.h"
21 #if !CONFIG_REALTIME_ONLY
22 #include "av1/encoder/firstpass.h"
23 #endif
24 #include "av1/encoder/global_motion.h"
25 #include "av1/encoder/global_motion_facade.h"
26 #include "av1/encoder/rdopt.h"
27 #include "aom_dsp/aom_dsp_common.h"
28 #include "av1/encoder/temporal_filter.h"
29 #include "av1/encoder/tpl_model.h"
30 
accumulate_rd_opt(ThreadData * td,ThreadData * td_t)31 static AOM_INLINE void accumulate_rd_opt(ThreadData *td, ThreadData *td_t) {
32   for (int i = 0; i < REFERENCE_MODES; i++)
33     td->rd_counts.comp_pred_diff[i] += td_t->rd_counts.comp_pred_diff[i];
34 
35   td->rd_counts.compound_ref_used_flag |=
36       td_t->rd_counts.compound_ref_used_flag;
37   td->rd_counts.skip_mode_used_flag |= td_t->rd_counts.skip_mode_used_flag;
38 
39   for (int i = 0; i < TX_SIZES_ALL; i++) {
40     for (int j = 0; j < TX_TYPES; j++)
41       td->rd_counts.tx_type_used[i][j] += td_t->rd_counts.tx_type_used[i][j];
42   }
43 
44   for (int i = 0; i < BLOCK_SIZES_ALL; i++) {
45     for (int j = 0; j < 2; j++) {
46       td->rd_counts.obmc_used[i][j] += td_t->rd_counts.obmc_used[i][j];
47     }
48   }
49 
50   for (int i = 0; i < 2; i++) {
51     td->rd_counts.warped_used[i] += td_t->rd_counts.warped_used[i];
52   }
53 }
54 
update_delta_lf_for_row_mt(AV1_COMP * cpi)55 static AOM_INLINE void update_delta_lf_for_row_mt(AV1_COMP *cpi) {
56   AV1_COMMON *cm = &cpi->common;
57   MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
58   const int mib_size = cm->seq_params->mib_size;
59   const int frame_lf_count =
60       av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
61   for (int row = 0; row < cm->tiles.rows; row++) {
62     for (int col = 0; col < cm->tiles.cols; col++) {
63       TileDataEnc *tile_data = &cpi->tile_data[row * cm->tiles.cols + col];
64       const TileInfo *const tile_info = &tile_data->tile_info;
65       for (int mi_row = tile_info->mi_row_start; mi_row < tile_info->mi_row_end;
66            mi_row += mib_size) {
67         if (mi_row == tile_info->mi_row_start)
68           av1_reset_loop_filter_delta(xd, av1_num_planes(cm));
69         for (int mi_col = tile_info->mi_col_start;
70              mi_col < tile_info->mi_col_end; mi_col += mib_size) {
71           const int idx_str = cm->mi_params.mi_stride * mi_row + mi_col;
72           MB_MODE_INFO **mi = cm->mi_params.mi_grid_base + idx_str;
73           MB_MODE_INFO *mbmi = mi[0];
74           if (mbmi->skip_txfm == 1 &&
75               (mbmi->bsize == cm->seq_params->sb_size)) {
76             for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id)
77               mbmi->delta_lf[lf_id] = xd->delta_lf[lf_id];
78             mbmi->delta_lf_from_base = xd->delta_lf_from_base;
79           } else {
80             if (cm->delta_q_info.delta_lf_multi) {
81               for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id)
82                 xd->delta_lf[lf_id] = mbmi->delta_lf[lf_id];
83             } else {
84               xd->delta_lf_from_base = mbmi->delta_lf_from_base;
85             }
86           }
87         }
88       }
89     }
90   }
91 }
92 
av1_row_mt_sync_read_dummy(AV1EncRowMultiThreadSync * row_mt_sync,int r,int c)93 void av1_row_mt_sync_read_dummy(AV1EncRowMultiThreadSync *row_mt_sync, int r,
94                                 int c) {
95   (void)row_mt_sync;
96   (void)r;
97   (void)c;
98   return;
99 }
100 
av1_row_mt_sync_write_dummy(AV1EncRowMultiThreadSync * row_mt_sync,int r,int c,int cols)101 void av1_row_mt_sync_write_dummy(AV1EncRowMultiThreadSync *row_mt_sync, int r,
102                                  int c, int cols) {
103   (void)row_mt_sync;
104   (void)r;
105   (void)c;
106   (void)cols;
107   return;
108 }
109 
av1_row_mt_sync_read(AV1EncRowMultiThreadSync * row_mt_sync,int r,int c)110 void av1_row_mt_sync_read(AV1EncRowMultiThreadSync *row_mt_sync, int r, int c) {
111 #if CONFIG_MULTITHREAD
112   const int nsync = row_mt_sync->sync_range;
113 
114   if (r) {
115     pthread_mutex_t *const mutex = &row_mt_sync->mutex_[r - 1];
116     pthread_mutex_lock(mutex);
117 
118     while (c > row_mt_sync->num_finished_cols[r - 1] - nsync) {
119       pthread_cond_wait(&row_mt_sync->cond_[r - 1], mutex);
120     }
121     pthread_mutex_unlock(mutex);
122   }
123 #else
124   (void)row_mt_sync;
125   (void)r;
126   (void)c;
127 #endif  // CONFIG_MULTITHREAD
128 }
129 
av1_row_mt_sync_write(AV1EncRowMultiThreadSync * row_mt_sync,int r,int c,int cols)130 void av1_row_mt_sync_write(AV1EncRowMultiThreadSync *row_mt_sync, int r, int c,
131                            int cols) {
132 #if CONFIG_MULTITHREAD
133   const int nsync = row_mt_sync->sync_range;
134   int cur;
135   // Only signal when there are enough encoded blocks for next row to run.
136   int sig = 1;
137 
138   if (c < cols - 1) {
139     cur = c;
140     if (c % nsync) sig = 0;
141   } else {
142     cur = cols + nsync;
143   }
144 
145   if (sig) {
146     pthread_mutex_lock(&row_mt_sync->mutex_[r]);
147 
148     row_mt_sync->num_finished_cols[r] = cur;
149 
150     pthread_cond_signal(&row_mt_sync->cond_[r]);
151     pthread_mutex_unlock(&row_mt_sync->mutex_[r]);
152   }
153 #else
154   (void)row_mt_sync;
155   (void)r;
156   (void)c;
157   (void)cols;
158 #endif  // CONFIG_MULTITHREAD
159 }
160 
161 // Allocate memory for row synchronization
row_mt_sync_mem_alloc(AV1EncRowMultiThreadSync * row_mt_sync,AV1_COMMON * cm,int rows)162 static void row_mt_sync_mem_alloc(AV1EncRowMultiThreadSync *row_mt_sync,
163                                   AV1_COMMON *cm, int rows) {
164 #if CONFIG_MULTITHREAD
165   int i;
166 
167   CHECK_MEM_ERROR(cm, row_mt_sync->mutex_,
168                   aom_malloc(sizeof(*row_mt_sync->mutex_) * rows));
169   if (row_mt_sync->mutex_) {
170     for (i = 0; i < rows; ++i) {
171       pthread_mutex_init(&row_mt_sync->mutex_[i], NULL);
172     }
173   }
174 
175   CHECK_MEM_ERROR(cm, row_mt_sync->cond_,
176                   aom_malloc(sizeof(*row_mt_sync->cond_) * rows));
177   if (row_mt_sync->cond_) {
178     for (i = 0; i < rows; ++i) {
179       pthread_cond_init(&row_mt_sync->cond_[i], NULL);
180     }
181   }
182 #endif  // CONFIG_MULTITHREAD
183 
184   CHECK_MEM_ERROR(cm, row_mt_sync->num_finished_cols,
185                   aom_malloc(sizeof(*row_mt_sync->num_finished_cols) * rows));
186 
187   row_mt_sync->rows = rows;
188   // Set up nsync.
189   row_mt_sync->sync_range = 1;
190 }
191 
192 // Deallocate row based multi-threading synchronization related mutex and data
row_mt_sync_mem_dealloc(AV1EncRowMultiThreadSync * row_mt_sync)193 static void row_mt_sync_mem_dealloc(AV1EncRowMultiThreadSync *row_mt_sync) {
194   if (row_mt_sync != NULL) {
195 #if CONFIG_MULTITHREAD
196     int i;
197 
198     if (row_mt_sync->mutex_ != NULL) {
199       for (i = 0; i < row_mt_sync->rows; ++i) {
200         pthread_mutex_destroy(&row_mt_sync->mutex_[i]);
201       }
202       aom_free(row_mt_sync->mutex_);
203     }
204     if (row_mt_sync->cond_ != NULL) {
205       for (i = 0; i < row_mt_sync->rows; ++i) {
206         pthread_cond_destroy(&row_mt_sync->cond_[i]);
207       }
208       aom_free(row_mt_sync->cond_);
209     }
210 #endif  // CONFIG_MULTITHREAD
211     aom_free(row_mt_sync->num_finished_cols);
212 
213     // clear the structure as the source of this call may be dynamic change
214     // in tiles in which case this call will be followed by an _alloc()
215     // which may fail.
216     av1_zero(*row_mt_sync);
217   }
218 }
219 
row_mt_mem_alloc(AV1_COMP * cpi,int max_rows,int max_cols,int alloc_row_ctx)220 static void row_mt_mem_alloc(AV1_COMP *cpi, int max_rows, int max_cols,
221                              int alloc_row_ctx) {
222   struct AV1Common *cm = &cpi->common;
223   AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
224   const int tile_cols = cm->tiles.cols;
225   const int tile_rows = cm->tiles.rows;
226   int tile_col, tile_row;
227 
228   // Allocate memory for row based multi-threading
229   for (tile_row = 0; tile_row < tile_rows; tile_row++) {
230     for (tile_col = 0; tile_col < tile_cols; tile_col++) {
231       int tile_index = tile_row * tile_cols + tile_col;
232       TileDataEnc *const this_tile = &cpi->tile_data[tile_index];
233 
234       row_mt_sync_mem_alloc(&this_tile->row_mt_sync, cm, max_rows);
235 
236       this_tile->row_ctx = NULL;
237       if (alloc_row_ctx) {
238         assert(max_cols > 0);
239         const int num_row_ctx = AOMMAX(1, (max_cols - 1));
240         CHECK_MEM_ERROR(cm, this_tile->row_ctx,
241                         (FRAME_CONTEXT *)aom_memalign(
242                             16, num_row_ctx * sizeof(*this_tile->row_ctx)));
243       }
244     }
245   }
246   enc_row_mt->allocated_tile_cols = tile_cols;
247   enc_row_mt->allocated_tile_rows = tile_rows;
248   enc_row_mt->allocated_rows = max_rows;
249   enc_row_mt->allocated_cols = max_cols - 1;
250 }
251 
av1_row_mt_mem_dealloc(AV1_COMP * cpi)252 void av1_row_mt_mem_dealloc(AV1_COMP *cpi) {
253   AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
254   const int tile_cols = enc_row_mt->allocated_tile_cols;
255   const int tile_rows = enc_row_mt->allocated_tile_rows;
256   int tile_col, tile_row;
257 
258   // Free row based multi-threading sync memory
259   for (tile_row = 0; tile_row < tile_rows; tile_row++) {
260     for (tile_col = 0; tile_col < tile_cols; tile_col++) {
261       int tile_index = tile_row * tile_cols + tile_col;
262       TileDataEnc *const this_tile = &cpi->tile_data[tile_index];
263 
264       row_mt_sync_mem_dealloc(&this_tile->row_mt_sync);
265 
266       if (cpi->oxcf.algo_cfg.cdf_update_mode) aom_free(this_tile->row_ctx);
267     }
268   }
269   enc_row_mt->allocated_rows = 0;
270   enc_row_mt->allocated_cols = 0;
271   enc_row_mt->allocated_tile_cols = 0;
272   enc_row_mt->allocated_tile_rows = 0;
273 }
274 
assign_tile_to_thread(int * thread_id_to_tile_id,int num_tiles,int num_workers)275 static AOM_INLINE void assign_tile_to_thread(int *thread_id_to_tile_id,
276                                              int num_tiles, int num_workers) {
277   int tile_id = 0;
278   int i;
279 
280   for (i = 0; i < num_workers; i++) {
281     thread_id_to_tile_id[i] = tile_id++;
282     if (tile_id == num_tiles) tile_id = 0;
283   }
284 }
285 
get_next_job(TileDataEnc * const tile_data,int * current_mi_row,int mib_size)286 static AOM_INLINE int get_next_job(TileDataEnc *const tile_data,
287                                    int *current_mi_row, int mib_size) {
288   AV1EncRowMultiThreadSync *const row_mt_sync = &tile_data->row_mt_sync;
289   const int mi_row_end = tile_data->tile_info.mi_row_end;
290 
291   if (row_mt_sync->next_mi_row < mi_row_end) {
292     *current_mi_row = row_mt_sync->next_mi_row;
293     row_mt_sync->num_threads_working++;
294     row_mt_sync->next_mi_row += mib_size;
295     return 1;
296   }
297   return 0;
298 }
299 
switch_tile_and_get_next_job(AV1_COMMON * const cm,TileDataEnc * const tile_data,int * cur_tile_id,int * current_mi_row,int * end_of_frame,int is_firstpass,const BLOCK_SIZE fp_block_size)300 static AOM_INLINE void switch_tile_and_get_next_job(
301     AV1_COMMON *const cm, TileDataEnc *const tile_data, int *cur_tile_id,
302     int *current_mi_row, int *end_of_frame, int is_firstpass,
303     const BLOCK_SIZE fp_block_size) {
304   const int tile_cols = cm->tiles.cols;
305   const int tile_rows = cm->tiles.rows;
306 
307   int tile_id = -1;  // Stores the tile ID with minimum proc done
308   int max_mis_to_encode = 0;
309   int min_num_threads_working = INT_MAX;
310 
311   for (int tile_row = 0; tile_row < tile_rows; tile_row++) {
312     for (int tile_col = 0; tile_col < tile_cols; tile_col++) {
313       int tile_index = tile_row * tile_cols + tile_col;
314       TileDataEnc *const this_tile = &tile_data[tile_index];
315       AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
316 
317 #if CONFIG_REALTIME_ONLY
318       int num_b_rows_in_tile =
319           av1_get_sb_rows_in_tile(cm, this_tile->tile_info);
320       int num_b_cols_in_tile =
321           av1_get_sb_cols_in_tile(cm, this_tile->tile_info);
322 #else
323       int num_b_rows_in_tile =
324           is_firstpass
325               ? av1_get_unit_rows_in_tile(this_tile->tile_info, fp_block_size)
326               : av1_get_sb_rows_in_tile(cm, this_tile->tile_info);
327       int num_b_cols_in_tile =
328           is_firstpass
329               ? av1_get_unit_cols_in_tile(this_tile->tile_info, fp_block_size)
330               : av1_get_sb_cols_in_tile(cm, this_tile->tile_info);
331 #endif
332       int theoretical_limit_on_threads =
333           AOMMIN((num_b_cols_in_tile + 1) >> 1, num_b_rows_in_tile);
334       int num_threads_working = row_mt_sync->num_threads_working;
335 
336       if (num_threads_working < theoretical_limit_on_threads) {
337         int num_mis_to_encode =
338             this_tile->tile_info.mi_row_end - row_mt_sync->next_mi_row;
339 
340         // Tile to be processed by this thread is selected on the basis of
341         // availability of jobs:
342         // 1) If jobs are available, tile to be processed is chosen on the
343         // basis of minimum number of threads working for that tile. If two or
344         // more tiles have same number of threads working for them, then the
345         // tile with maximum number of jobs available will be chosen.
346         // 2) If no jobs are available, then end_of_frame is reached.
347         if (num_mis_to_encode > 0) {
348           if (num_threads_working < min_num_threads_working) {
349             min_num_threads_working = num_threads_working;
350             max_mis_to_encode = 0;
351           }
352           if (num_threads_working == min_num_threads_working &&
353               num_mis_to_encode > max_mis_to_encode) {
354             tile_id = tile_index;
355             max_mis_to_encode = num_mis_to_encode;
356           }
357         }
358       }
359     }
360   }
361   if (tile_id == -1) {
362     *end_of_frame = 1;
363   } else {
364     // Update the current tile id to the tile id that will be processed next,
365     // which will be the least processed tile.
366     *cur_tile_id = tile_id;
367     const int unit_height = mi_size_high[fp_block_size];
368     get_next_job(&tile_data[tile_id], current_mi_row,
369                  is_firstpass ? unit_height : cm->seq_params->mib_size);
370   }
371 }
372 
373 #if !CONFIG_REALTIME_ONLY
fp_enc_row_mt_worker_hook(void * arg1,void * unused)374 static int fp_enc_row_mt_worker_hook(void *arg1, void *unused) {
375   EncWorkerData *const thread_data = (EncWorkerData *)arg1;
376   AV1_COMP *const cpi = thread_data->cpi;
377   AV1_COMMON *const cm = &cpi->common;
378   int thread_id = thread_data->thread_id;
379   AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
380   int cur_tile_id = enc_row_mt->thread_id_to_tile_id[thread_id];
381 #if CONFIG_MULTITHREAD
382   pthread_mutex_t *enc_row_mt_mutex_ = enc_row_mt->mutex_;
383 #endif
384   (void)unused;
385 
386   assert(cur_tile_id != -1);
387 
388   const BLOCK_SIZE fp_block_size = cpi->fp_block_size;
389   const int unit_height = mi_size_high[fp_block_size];
390   int end_of_frame = 0;
391   while (1) {
392     int current_mi_row = -1;
393 #if CONFIG_MULTITHREAD
394     pthread_mutex_lock(enc_row_mt_mutex_);
395 #endif
396     if (!get_next_job(&cpi->tile_data[cur_tile_id], &current_mi_row,
397                       unit_height)) {
398       // No jobs are available for the current tile. Query for the status of
399       // other tiles and get the next job if available
400       switch_tile_and_get_next_job(cm, cpi->tile_data, &cur_tile_id,
401                                    &current_mi_row, &end_of_frame, 1,
402                                    fp_block_size);
403     }
404 #if CONFIG_MULTITHREAD
405     pthread_mutex_unlock(enc_row_mt_mutex_);
406 #endif
407     if (end_of_frame == 1) break;
408 
409     TileDataEnc *const this_tile = &cpi->tile_data[cur_tile_id];
410     AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
411     ThreadData *td = thread_data->td;
412 
413     assert(current_mi_row != -1 &&
414            current_mi_row <= this_tile->tile_info.mi_row_end);
415 
416     const int unit_height_log2 = mi_size_high_log2[fp_block_size];
417     av1_first_pass_row(cpi, td, this_tile, current_mi_row >> unit_height_log2,
418                        fp_block_size);
419 #if CONFIG_MULTITHREAD
420     pthread_mutex_lock(enc_row_mt_mutex_);
421 #endif
422     row_mt_sync->num_threads_working--;
423 #if CONFIG_MULTITHREAD
424     pthread_mutex_unlock(enc_row_mt_mutex_);
425 #endif
426   }
427 
428   return 1;
429 }
430 #endif
431 
enc_row_mt_worker_hook(void * arg1,void * unused)432 static int enc_row_mt_worker_hook(void *arg1, void *unused) {
433   EncWorkerData *const thread_data = (EncWorkerData *)arg1;
434   AV1_COMP *const cpi = thread_data->cpi;
435   AV1_COMMON *const cm = &cpi->common;
436   int thread_id = thread_data->thread_id;
437   AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
438   int cur_tile_id = enc_row_mt->thread_id_to_tile_id[thread_id];
439 #if CONFIG_MULTITHREAD
440   pthread_mutex_t *enc_row_mt_mutex_ = enc_row_mt->mutex_;
441 #endif
442   (void)unused;
443 
444   assert(cur_tile_id != -1);
445 
446   const BLOCK_SIZE fp_block_size = cpi->fp_block_size;
447   int end_of_frame = 0;
448 
449   // When master thread does not have a valid job to process, xd->tile_ctx
450   // is not set and it contains NULL pointer. This can result in NULL pointer
451   // access violation if accessed beyond the encode stage. Hence, updating
452   // thread_data->td->mb.e_mbd.tile_ctx is initialized with common frame
453   // context to avoid NULL pointer access in subsequent stages.
454   thread_data->td->mb.e_mbd.tile_ctx = cm->fc;
455   while (1) {
456     int current_mi_row = -1;
457 #if CONFIG_MULTITHREAD
458     pthread_mutex_lock(enc_row_mt_mutex_);
459 #endif
460     if (!get_next_job(&cpi->tile_data[cur_tile_id], &current_mi_row,
461                       cm->seq_params->mib_size)) {
462       // No jobs are available for the current tile. Query for the status of
463       // other tiles and get the next job if available
464       switch_tile_and_get_next_job(cm, cpi->tile_data, &cur_tile_id,
465                                    &current_mi_row, &end_of_frame, 0,
466                                    fp_block_size);
467     }
468 #if CONFIG_MULTITHREAD
469     pthread_mutex_unlock(enc_row_mt_mutex_);
470 #endif
471     if (end_of_frame == 1) break;
472 
473     TileDataEnc *const this_tile = &cpi->tile_data[cur_tile_id];
474     AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
475     const TileInfo *const tile_info = &this_tile->tile_info;
476     const int tile_row = tile_info->tile_row;
477     const int tile_col = tile_info->tile_col;
478     ThreadData *td = thread_data->td;
479 
480     assert(current_mi_row != -1 && current_mi_row <= tile_info->mi_row_end);
481 
482     td->mb.e_mbd.tile_ctx = td->tctx;
483     td->mb.tile_pb_ctx = &this_tile->tctx;
484     td->abs_sum_level = 0;
485 
486     if (this_tile->allow_update_cdf) {
487       td->mb.row_ctx = this_tile->row_ctx;
488       if (current_mi_row == tile_info->mi_row_start)
489         memcpy(td->mb.e_mbd.tile_ctx, &this_tile->tctx, sizeof(FRAME_CONTEXT));
490     } else {
491       memcpy(td->mb.e_mbd.tile_ctx, &this_tile->tctx, sizeof(FRAME_CONTEXT));
492     }
493 
494     av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), tile_row,
495                            &td->mb.e_mbd);
496 
497     cfl_init(&td->mb.e_mbd.cfl, cm->seq_params);
498     if (td->mb.txfm_search_info.txb_rd_records != NULL) {
499       av1_crc32c_calculator_init(
500           &td->mb.txfm_search_info.txb_rd_records->mb_rd_record.crc_calculator);
501     }
502 
503     av1_encode_sb_row(cpi, td, tile_row, tile_col, current_mi_row);
504 #if CONFIG_MULTITHREAD
505     pthread_mutex_lock(enc_row_mt_mutex_);
506 #endif
507     this_tile->abs_sum_level += td->abs_sum_level;
508     row_mt_sync->num_threads_working--;
509 #if CONFIG_MULTITHREAD
510     pthread_mutex_unlock(enc_row_mt_mutex_);
511 #endif
512   }
513 
514   return 1;
515 }
516 
enc_worker_hook(void * arg1,void * unused)517 static int enc_worker_hook(void *arg1, void *unused) {
518   EncWorkerData *const thread_data = (EncWorkerData *)arg1;
519   AV1_COMP *const cpi = thread_data->cpi;
520   const AV1_COMMON *const cm = &cpi->common;
521   const int tile_cols = cm->tiles.cols;
522   const int tile_rows = cm->tiles.rows;
523   int t;
524 
525   (void)unused;
526 
527   for (t = thread_data->start; t < tile_rows * tile_cols;
528        t += cpi->mt_info.num_workers) {
529     int tile_row = t / tile_cols;
530     int tile_col = t % tile_cols;
531 
532     TileDataEnc *const this_tile =
533         &cpi->tile_data[tile_row * cm->tiles.cols + tile_col];
534     thread_data->td->mb.e_mbd.tile_ctx = &this_tile->tctx;
535     thread_data->td->mb.tile_pb_ctx = &this_tile->tctx;
536     av1_encode_tile(cpi, thread_data->td, tile_row, tile_col);
537   }
538 
539   return 1;
540 }
541 
av1_init_frame_mt(AV1_PRIMARY * ppi,AV1_COMP * cpi)542 void av1_init_frame_mt(AV1_PRIMARY *ppi, AV1_COMP *cpi) {
543   cpi->mt_info.workers = ppi->p_mt_info.workers;
544   cpi->mt_info.num_workers = ppi->p_mt_info.num_workers;
545   cpi->mt_info.tile_thr_data = ppi->p_mt_info.tile_thr_data;
546   int i;
547   for (i = MOD_FP; i < NUM_MT_MODULES; i++) {
548     cpi->mt_info.num_mod_workers[i] =
549         AOMMIN(cpi->mt_info.num_workers, ppi->p_mt_info.num_mod_workers[i]);
550   }
551 }
552 
av1_init_cdef_worker(AV1_COMP * cpi)553 void av1_init_cdef_worker(AV1_COMP *cpi) {
554 #if CONFIG_FRAME_PARALLEL_ENCODE
555   // The allocation is done only for level 0 parallel frames. No change
556   // in config is supported in the middle of a parallel encode set, since the
557   // rest of the MT modules also do not support dynamic change of config.
558   if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) return;
559 #endif  // CONFIG_FRAME_PARALLEL_ENCODE
560   PrimaryMultiThreadInfo *const p_mt_info = &cpi->ppi->p_mt_info;
561   int num_cdef_workers = av1_get_num_mod_workers_for_alloc(p_mt_info, MOD_CDEF);
562 
563   av1_alloc_cdef_buffers(&cpi->common, &p_mt_info->cdef_worker,
564                          &cpi->mt_info.cdef_sync, num_cdef_workers, 1);
565   cpi->mt_info.cdef_worker = &p_mt_info->cdef_worker[0];
566 }
567 
568 #if !CONFIG_REALTIME_ONLY
av1_init_lr_mt_buffers(AV1_COMP * cpi)569 void av1_init_lr_mt_buffers(AV1_COMP *cpi) {
570   AV1_COMMON *const cm = &cpi->common;
571   AV1LrSync *lr_sync = &cpi->mt_info.lr_row_sync;
572   if (lr_sync->sync_range) {
573     int num_lr_workers =
574         av1_get_num_mod_workers_for_alloc(&cpi->ppi->p_mt_info, MOD_LR);
575 #if CONFIG_FRAME_PARALLEL_ENCODE
576     if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0)
577       return;
578 #endif  // CONFIG_FRAME_PARALLEL_ENCODE
579     lr_sync->lrworkerdata[num_lr_workers - 1].rst_tmpbuf = cm->rst_tmpbuf;
580     lr_sync->lrworkerdata[num_lr_workers - 1].rlbs = cm->rlbs;
581   }
582 }
583 #endif
584 
585 #if CONFIG_MULTITHREAD
av1_init_mt_sync(AV1_COMP * cpi,int is_first_pass)586 void av1_init_mt_sync(AV1_COMP *cpi, int is_first_pass) {
587   AV1_COMMON *const cm = &cpi->common;
588   MultiThreadInfo *const mt_info = &cpi->mt_info;
589 
590   // Initialize enc row MT object.
591   if (is_first_pass || cpi->oxcf.row_mt == 1) {
592     AV1EncRowMultiThreadInfo *enc_row_mt = &mt_info->enc_row_mt;
593     if (enc_row_mt->mutex_ == NULL) {
594       CHECK_MEM_ERROR(cm, enc_row_mt->mutex_,
595                       aom_malloc(sizeof(*(enc_row_mt->mutex_))));
596       if (enc_row_mt->mutex_) pthread_mutex_init(enc_row_mt->mutex_, NULL);
597     }
598   }
599 
600   if (!is_first_pass) {
601     // Initialize global motion MT object.
602     AV1GlobalMotionSync *gm_sync = &mt_info->gm_sync;
603     if (gm_sync->mutex_ == NULL) {
604       CHECK_MEM_ERROR(cm, gm_sync->mutex_,
605                       aom_malloc(sizeof(*(gm_sync->mutex_))));
606       if (gm_sync->mutex_) pthread_mutex_init(gm_sync->mutex_, NULL);
607     }
608 #if !CONFIG_REALTIME_ONLY
609     // Initialize temporal filtering MT object.
610     AV1TemporalFilterSync *tf_sync = &mt_info->tf_sync;
611     if (tf_sync->mutex_ == NULL) {
612       CHECK_MEM_ERROR(cm, tf_sync->mutex_,
613                       aom_malloc(sizeof(*tf_sync->mutex_)));
614       if (tf_sync->mutex_) pthread_mutex_init(tf_sync->mutex_, NULL);
615     }
616 #endif  // !CONFIG_REALTIME_ONLY
617         // Initialize CDEF MT object.
618     AV1CdefSync *cdef_sync = &mt_info->cdef_sync;
619     if (cdef_sync->mutex_ == NULL) {
620       CHECK_MEM_ERROR(cm, cdef_sync->mutex_,
621                       aom_malloc(sizeof(*(cdef_sync->mutex_))));
622       if (cdef_sync->mutex_) pthread_mutex_init(cdef_sync->mutex_, NULL);
623     }
624 
625     // Initialize loop filter MT object.
626     AV1LfSync *lf_sync = &mt_info->lf_row_sync;
627     // Number of superblock rows
628     const int sb_rows =
629         ALIGN_POWER_OF_TWO(cm->height >> MI_SIZE_LOG2, MAX_MIB_SIZE_LOG2) >>
630         MAX_MIB_SIZE_LOG2;
631     PrimaryMultiThreadInfo *const p_mt_info = &cpi->ppi->p_mt_info;
632     int num_lf_workers = av1_get_num_mod_workers_for_alloc(p_mt_info, MOD_LPF);
633 
634     if (!lf_sync->sync_range || sb_rows != lf_sync->rows ||
635         num_lf_workers > lf_sync->num_workers) {
636       av1_loop_filter_dealloc(lf_sync);
637       av1_loop_filter_alloc(lf_sync, cm, sb_rows, cm->width, num_lf_workers);
638     }
639 
640 #if !CONFIG_REALTIME_ONLY
641     // Initialize loop restoration MT object.
642     AV1LrSync *lr_sync = &mt_info->lr_row_sync;
643     int rst_unit_size;
644     if (cm->width * cm->height > 352 * 288)
645       rst_unit_size = RESTORATION_UNITSIZE_MAX;
646     else
647       rst_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
648     int num_rows_lr = av1_lr_count_units_in_tile(rst_unit_size, cm->height);
649     int num_lr_workers = av1_get_num_mod_workers_for_alloc(p_mt_info, MOD_LR);
650     if (!lr_sync->sync_range || num_rows_lr > lr_sync->rows ||
651         num_lr_workers > lr_sync->num_workers ||
652         MAX_MB_PLANE > lr_sync->num_planes) {
653       av1_loop_restoration_dealloc(lr_sync, num_lr_workers);
654       av1_loop_restoration_alloc(lr_sync, cm, num_lr_workers, num_rows_lr,
655                                  MAX_MB_PLANE, cm->width);
656     }
657 #endif
658 
659     // Initialization of pack bitstream MT object.
660     AV1EncPackBSSync *pack_bs_sync = &mt_info->pack_bs_sync;
661     if (pack_bs_sync->mutex_ == NULL) {
662       CHECK_MEM_ERROR(cm, pack_bs_sync->mutex_,
663                       aom_malloc(sizeof(*pack_bs_sync->mutex_)));
664       if (pack_bs_sync->mutex_) pthread_mutex_init(pack_bs_sync->mutex_, NULL);
665     }
666   }
667 }
668 #endif  // CONFIG_MULTITHREAD
669 
670 // Computes the number of workers to be considered while allocating memory for a
671 // multi-threaded module under FPMT.
av1_get_num_mod_workers_for_alloc(PrimaryMultiThreadInfo * const p_mt_info,MULTI_THREADED_MODULES mod_name)672 int av1_get_num_mod_workers_for_alloc(PrimaryMultiThreadInfo *const p_mt_info,
673                                       MULTI_THREADED_MODULES mod_name) {
674   int num_mod_workers = p_mt_info->num_mod_workers[mod_name];
675   if (p_mt_info->num_mod_workers[MOD_FRAME_ENC] > 1) {
676     // TODO(anyone): Change num_mod_workers to num_mod_workers[MOD_FRAME_ENC].
677     // As frame parallel jobs will only perform multi-threading for the encode
678     // stage, we can limit the allocations according to num_enc_workers per
679     // frame parallel encode(a.k.a num_mod_workers[MOD_FRAME_ENC]).
680     num_mod_workers = p_mt_info->num_workers;
681   }
682   return num_mod_workers;
683 }
684 
av1_init_tile_thread_data(AV1_PRIMARY * ppi,int is_first_pass)685 void av1_init_tile_thread_data(AV1_PRIMARY *ppi, int is_first_pass) {
686   PrimaryMultiThreadInfo *const p_mt_info = &ppi->p_mt_info;
687 
688   assert(p_mt_info->workers != NULL);
689   assert(p_mt_info->tile_thr_data != NULL);
690 
691   int num_workers = p_mt_info->num_workers;
692   int num_enc_workers = av1_get_num_mod_workers_for_alloc(p_mt_info, MOD_ENC);
693   for (int i = num_workers - 1; i >= 0; i--) {
694     EncWorkerData *const thread_data = &p_mt_info->tile_thr_data[i];
695 
696     if (i > 0) {
697       // Allocate thread data.
698       AOM_CHECK_MEM_ERROR(&ppi->error, thread_data->td,
699                           aom_memalign(32, sizeof(*thread_data->td)));
700       av1_zero(*thread_data->td);
701 #if CONFIG_FRAME_PARALLEL_ENCODE
702       thread_data->original_td = thread_data->td;
703 #endif
704 
705       // Set up shared coeff buffers.
706       av1_setup_shared_coeff_buffer(
707           &ppi->seq_params, &thread_data->td->shared_coeff_buf, &ppi->error);
708       AOM_CHECK_MEM_ERROR(
709           &ppi->error, thread_data->td->tmp_conv_dst,
710           aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE *
711                                sizeof(*thread_data->td->tmp_conv_dst)));
712 
713       if (i < p_mt_info->num_mod_workers[MOD_FP]) {
714         // Set up firstpass PICK_MODE_CONTEXT.
715         thread_data->td->firstpass_ctx = av1_alloc_pmc(
716             ppi->cpi, BLOCK_16X16, &thread_data->td->shared_coeff_buf);
717       }
718 
719       if (!is_first_pass && i < num_enc_workers) {
720         // Set up sms_tree.
721         av1_setup_sms_tree(ppi->cpi, thread_data->td);
722 
723         alloc_obmc_buffers(&thread_data->td->obmc_buffer, &ppi->error);
724 
725         for (int x = 0; x < 2; x++)
726           for (int y = 0; y < 2; y++)
727             AOM_CHECK_MEM_ERROR(
728                 &ppi->error, thread_data->td->hash_value_buffer[x][y],
729                 (uint32_t *)aom_malloc(
730                     AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
731                     sizeof(*thread_data->td->hash_value_buffer[0][0])));
732 
733         // Allocate frame counters in thread data.
734         AOM_CHECK_MEM_ERROR(&ppi->error, thread_data->td->counts,
735                             aom_calloc(1, sizeof(*thread_data->td->counts)));
736 
737         // Allocate buffers used by palette coding mode.
738         AOM_CHECK_MEM_ERROR(
739             &ppi->error, thread_data->td->palette_buffer,
740             aom_memalign(16, sizeof(*thread_data->td->palette_buffer)));
741 
742         alloc_compound_type_rd_buffers(&ppi->error,
743                                        &thread_data->td->comp_rd_buffer);
744 
745         for (int j = 0; j < 2; ++j) {
746           AOM_CHECK_MEM_ERROR(
747               &ppi->error, thread_data->td->tmp_pred_bufs[j],
748               aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
749                                    sizeof(*thread_data->td->tmp_pred_bufs[j])));
750         }
751 
752         const SPEED_FEATURES *sf = &ppi->cpi->sf;
753         if (sf->intra_sf.intra_pruning_with_hog ||
754             sf->intra_sf.chroma_intra_pruning_with_hog) {
755           const int plane_types = PLANE_TYPES >> ppi->seq_params.monochrome;
756           AOM_CHECK_MEM_ERROR(
757               &ppi->error, thread_data->td->pixel_gradient_info,
758               aom_malloc(sizeof(*thread_data->td->pixel_gradient_info) *
759                          plane_types * MAX_SB_SQUARE));
760         }
761 
762         if (ppi->cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION) {
763           const int num_64x64_blocks =
764               (ppi->seq_params.sb_size == BLOCK_64X64) ? 1 : 4;
765           AOM_CHECK_MEM_ERROR(
766               &ppi->error, thread_data->td->vt64x64,
767               aom_malloc(sizeof(*thread_data->td->vt64x64) * num_64x64_blocks));
768         }
769       }
770     }
771 
772     if (!is_first_pass && ppi->cpi->oxcf.row_mt == 1 && i < num_enc_workers) {
773       if (i == 0) {
774 #if CONFIG_FRAME_PARALLEL_ENCODE
775         for (int j = 0; j < ppi->num_fp_contexts; j++) {
776           AOM_CHECK_MEM_ERROR(&ppi->error, ppi->parallel_cpi[j]->td.tctx,
777                               (FRAME_CONTEXT *)aom_memalign(
778                                   16, sizeof(*ppi->parallel_cpi[j]->td.tctx)));
779         }
780 #else
781         AOM_CHECK_MEM_ERROR(
782             &ppi->error, ppi->cpi->td.tctx,
783             (FRAME_CONTEXT *)aom_memalign(16, sizeof(*ppi->cpi->td.tctx)));
784 #endif
785       } else {
786         AOM_CHECK_MEM_ERROR(
787             &ppi->error, thread_data->td->tctx,
788             (FRAME_CONTEXT *)aom_memalign(16, sizeof(*thread_data->td->tctx)));
789       }
790     }
791   }
792 }
793 
av1_create_workers(AV1_PRIMARY * ppi,int num_workers)794 void av1_create_workers(AV1_PRIMARY *ppi, int num_workers) {
795   PrimaryMultiThreadInfo *const p_mt_info = &ppi->p_mt_info;
796   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
797 
798   AOM_CHECK_MEM_ERROR(&ppi->error, p_mt_info->workers,
799                       aom_malloc(num_workers * sizeof(*p_mt_info->workers)));
800 
801   AOM_CHECK_MEM_ERROR(
802       &ppi->error, p_mt_info->tile_thr_data,
803       aom_calloc(num_workers, sizeof(*p_mt_info->tile_thr_data)));
804 
805   for (int i = num_workers - 1; i >= 0; i--) {
806     AVxWorker *const worker = &p_mt_info->workers[i];
807     EncWorkerData *const thread_data = &p_mt_info->tile_thr_data[i];
808 
809     winterface->init(worker);
810     worker->thread_name = "aom enc worker";
811 
812     thread_data->thread_id = i;
813     // Set the starting tile for each thread.
814     thread_data->start = i;
815 
816     if (i > 0) {
817       // Create threads
818       if (!winterface->reset(worker))
819         aom_internal_error(&ppi->error, AOM_CODEC_ERROR,
820                            "Tile encoder thread creation failed");
821     }
822     winterface->sync(worker);
823 
824     ++p_mt_info->num_workers;
825   }
826 }
827 
828 #if CONFIG_FRAME_PARALLEL_ENCODE
829 // This function returns 1 if frame parallel encode is supported for
830 // the current configuration. Returns 0 otherwise.
is_fp_config(AV1_PRIMARY * ppi,AV1EncoderConfig * oxcf)831 static AOM_INLINE int is_fp_config(AV1_PRIMARY *ppi, AV1EncoderConfig *oxcf) {
832   // FPMT is enabled for AOM_Q and AOM_VBR.
833   // TODO(Mufaddal, Aasaipriya): Test and enable multi-tile and resize config.
834   if (oxcf->rc_cfg.mode == AOM_CBR || oxcf->rc_cfg.mode == AOM_CQ) {
835     return 0;
836   }
837   if (ppi->use_svc) {
838     return 0;
839   }
840   if (oxcf->tile_cfg.tile_columns > 0 || oxcf->tile_cfg.tile_rows > 0) {
841     return 0;
842   }
843   if (oxcf->dec_model_cfg.timing_info_present) {
844     return 0;
845   }
846   if (oxcf->mode != GOOD) {
847     return 0;
848   }
849   if (oxcf->tool_cfg.error_resilient_mode) {
850     return 0;
851   }
852   if (oxcf->resize_cfg.resize_mode) {
853     return 0;
854   }
855   if (oxcf->passes == 1) {
856     return 0;
857   }
858 
859   return 1;
860 }
861 
862 // A large value for threads used to compute the max num_enc_workers
863 // possible for each resolution.
864 #define MAX_THREADS 100
865 
866 // Computes the number of frame parallel(fp) contexts to be created
867 // based on the number of max_enc_workers.
av1_compute_num_fp_contexts(AV1_PRIMARY * ppi,AV1EncoderConfig * oxcf)868 int av1_compute_num_fp_contexts(AV1_PRIMARY *ppi, AV1EncoderConfig *oxcf) {
869   ppi->p_mt_info.num_mod_workers[MOD_FRAME_ENC] = 0;
870   if (!is_fp_config(ppi, oxcf)) {
871     return 1;
872   }
873   int max_num_enc_workers =
874       av1_compute_num_enc_workers(ppi->parallel_cpi[0], MAX_THREADS);
875 
876   // A parallel frame encode must have at least 1/4th the theoretical limit of
877   // max enc workers. TODO(Remya) : Tune this value for multi-tile case.
878   int workers_per_frame = AOMMAX(1, (max_num_enc_workers + 2) / 4);
879   int max_threads = oxcf->max_threads;
880   int num_fp_contexts = max_threads / workers_per_frame;
881 
882   num_fp_contexts = AOMMAX(1, AOMMIN(num_fp_contexts, MAX_PARALLEL_FRAMES));
883   if (num_fp_contexts > 1) {
884     assert(max_threads >= 2);
885     ppi->p_mt_info.num_mod_workers[MOD_FRAME_ENC] =
886         AOMMIN(max_num_enc_workers * num_fp_contexts, oxcf->max_threads);
887   }
888   return num_fp_contexts;
889 }
890 
891 // Computes the number of workers to process each of the parallel frames.
compute_num_workers_per_frame(const int num_workers,const int parallel_frame_count)892 static AOM_INLINE int compute_num_workers_per_frame(
893     const int num_workers, const int parallel_frame_count) {
894   // Number of level 2 workers per frame context (floor division).
895   int workers_per_frame = (num_workers / parallel_frame_count);
896   return workers_per_frame;
897 }
898 
899 // Prepare level 1 workers. This function is only called for
900 // parallel_frame_count > 1. This function populates the mt_info structure of
901 // frame level contexts appropriately by dividing the total number of available
902 // workers amongst the frames as level 2 workers. It also populates the hook and
903 // data members of level 1 workers.
prepare_fpmt_workers(AV1_PRIMARY * ppi,AV1_COMP_DATA * first_cpi_data,AVxWorkerHook hook,int parallel_frame_count)904 static AOM_INLINE void prepare_fpmt_workers(AV1_PRIMARY *ppi,
905                                             AV1_COMP_DATA *first_cpi_data,
906                                             AVxWorkerHook hook,
907                                             int parallel_frame_count) {
908   assert(parallel_frame_count <= ppi->num_fp_contexts &&
909          parallel_frame_count > 1);
910 
911   PrimaryMultiThreadInfo *const p_mt_info = &ppi->p_mt_info;
912   int num_workers = p_mt_info->num_workers;
913 
914   int frame_idx = 0;
915   int i = 0;
916   while (i < num_workers) {
917     // Assign level 1 worker
918     AVxWorker *frame_worker = p_mt_info->p_workers[frame_idx] =
919         &p_mt_info->workers[i];
920     AV1_COMP *cur_cpi = ppi->parallel_cpi[frame_idx];
921     MultiThreadInfo *mt_info = &cur_cpi->mt_info;
922     const int num_planes = av1_num_planes(&cur_cpi->common);
923 
924     // Assign start of level 2 worker pool
925     mt_info->workers = &p_mt_info->workers[i];
926     mt_info->tile_thr_data = &p_mt_info->tile_thr_data[i];
927     // Assign number of workers for each frame in the parallel encode set.
928     mt_info->num_workers = compute_num_workers_per_frame(
929         num_workers - i, parallel_frame_count - frame_idx);
930     for (int j = MOD_FP; j < NUM_MT_MODULES; j++) {
931       mt_info->num_mod_workers[j] =
932           AOMMIN(mt_info->num_workers, ppi->p_mt_info.num_mod_workers[j]);
933     }
934     if (ppi->p_mt_info.cdef_worker != NULL) {
935       mt_info->cdef_worker = &ppi->p_mt_info.cdef_worker[i];
936 
937       // Back up the original cdef_worker pointers.
938       mt_info->restore_state_buf.cdef_srcbuf = mt_info->cdef_worker->srcbuf;
939       for (int plane = 0; plane < num_planes; plane++)
940         mt_info->restore_state_buf.cdef_colbuf[plane] =
941             mt_info->cdef_worker->colbuf[plane];
942     }
943 #if !CONFIG_REALTIME_ONLY
944     // Back up the original LR buffers before update.
945     int idx = i + mt_info->num_workers - 1;
946     mt_info->restore_state_buf.rst_tmpbuf =
947         mt_info->lr_row_sync.lrworkerdata[idx].rst_tmpbuf;
948     mt_info->restore_state_buf.rlbs =
949         mt_info->lr_row_sync.lrworkerdata[idx].rlbs;
950 
951     // Update LR buffers.
952     mt_info->lr_row_sync.lrworkerdata[idx].rst_tmpbuf =
953         cur_cpi->common.rst_tmpbuf;
954     mt_info->lr_row_sync.lrworkerdata[idx].rlbs = cur_cpi->common.rlbs;
955 #endif
956 
957     // At this stage, the thread specific CDEF buffers for the current frame's
958     // 'common' and 'cdef_sync' only need to be allocated. 'cdef_worker' has
959     // already been allocated across parallel frames.
960     av1_alloc_cdef_buffers(&cur_cpi->common, &p_mt_info->cdef_worker,
961                            &mt_info->cdef_sync, p_mt_info->num_workers, 0);
962 
963     frame_worker->hook = hook;
964     frame_worker->data1 = cur_cpi;
965     frame_worker->data2 = (frame_idx == 0)
966                               ? first_cpi_data
967                               : &ppi->parallel_frames_data[frame_idx - 1];
968     frame_idx++;
969     i += mt_info->num_workers;
970   }
971   p_mt_info->p_num_workers = parallel_frame_count;
972 }
973 
974 // Launch level 1 workers to perform frame parallel encode.
launch_fpmt_workers(AV1_PRIMARY * ppi)975 static AOM_INLINE void launch_fpmt_workers(AV1_PRIMARY *ppi) {
976   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
977   int num_workers = ppi->p_mt_info.p_num_workers;
978 
979   for (int i = num_workers - 1; i >= 0; i--) {
980     AVxWorker *const worker = ppi->p_mt_info.p_workers[i];
981     if (i == 0)
982       winterface->execute(worker);
983     else
984       winterface->launch(worker);
985   }
986 }
987 
988 // Synchronize level 1 workers.
sync_fpmt_workers(AV1_PRIMARY * ppi)989 static AOM_INLINE void sync_fpmt_workers(AV1_PRIMARY *ppi) {
990   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
991   int num_workers = ppi->p_mt_info.p_num_workers;
992   int had_error = 0;
993   // Points to error in the earliest display order frame in the parallel set.
994   const struct aom_internal_error_info *error;
995 
996   // Encoding ends.
997   for (int i = num_workers - 1; i >= 0; i--) {
998     AVxWorker *const worker = ppi->p_mt_info.p_workers[i];
999     if (!winterface->sync(worker)) {
1000       had_error = 1;
1001       error = ((AV1_COMP *)worker->data1)->common.error;
1002     }
1003   }
1004 
1005   if (had_error)
1006     aom_internal_error(&ppi->error, error->error_code, error->detail);
1007 }
1008 
1009 // Restore worker states after parallel encode.
restore_workers_after_fpmt(AV1_PRIMARY * ppi,int parallel_frame_count)1010 static AOM_INLINE void restore_workers_after_fpmt(AV1_PRIMARY *ppi,
1011                                                   int parallel_frame_count) {
1012   assert(parallel_frame_count <= ppi->num_fp_contexts &&
1013          parallel_frame_count > 1);
1014   (void)parallel_frame_count;
1015 
1016   PrimaryMultiThreadInfo *const p_mt_info = &ppi->p_mt_info;
1017   int num_workers = p_mt_info->num_workers;
1018 
1019   int frame_idx = 0;
1020   int i = 0;
1021   while (i < num_workers) {
1022     AV1_COMP *cur_cpi = ppi->parallel_cpi[frame_idx];
1023     MultiThreadInfo *mt_info = &cur_cpi->mt_info;
1024     const int num_planes = av1_num_planes(&cur_cpi->common);
1025 
1026     // Restore the original cdef_worker pointers.
1027     if (ppi->p_mt_info.cdef_worker != NULL) {
1028       mt_info->cdef_worker->srcbuf = mt_info->restore_state_buf.cdef_srcbuf;
1029       for (int plane = 0; plane < num_planes; plane++)
1030         mt_info->cdef_worker->colbuf[plane] =
1031             mt_info->restore_state_buf.cdef_colbuf[plane];
1032     }
1033 #if !CONFIG_REALTIME_ONLY
1034     // Restore the original LR buffers.
1035     int idx = i + mt_info->num_workers - 1;
1036     mt_info->lr_row_sync.lrworkerdata[idx].rst_tmpbuf =
1037         mt_info->restore_state_buf.rst_tmpbuf;
1038     mt_info->lr_row_sync.lrworkerdata[idx].rlbs =
1039         mt_info->restore_state_buf.rlbs;
1040 #endif
1041 
1042     frame_idx++;
1043     i += mt_info->num_workers;
1044   }
1045 }
1046 
get_compressed_data_hook(void * arg1,void * arg2)1047 static int get_compressed_data_hook(void *arg1, void *arg2) {
1048   AV1_COMP *cpi = (AV1_COMP *)arg1;
1049   AV1_COMP_DATA *cpi_data = (AV1_COMP_DATA *)arg2;
1050   int status = av1_get_compressed_data(cpi, cpi_data);
1051 
1052   // AOM_CODEC_OK(0) means no error.
1053   return !status;
1054 }
1055 
1056 // This function encodes the raw frame data for each frame in parallel encode
1057 // set, and outputs the frame bit stream to the designated buffers.
av1_compress_parallel_frames(AV1_PRIMARY * const ppi,AV1_COMP_DATA * const first_cpi_data)1058 int av1_compress_parallel_frames(AV1_PRIMARY *const ppi,
1059                                  AV1_COMP_DATA *const first_cpi_data) {
1060   // Bitmask for the frame buffers referenced by cpi->scaled_ref_buf
1061   // corresponding to frames in the current parallel encode set.
1062   int ref_buffers_used_map = 0;
1063   int frames_in_parallel_set = av1_init_parallel_frame_context(
1064       first_cpi_data, ppi, &ref_buffers_used_map);
1065   prepare_fpmt_workers(ppi, first_cpi_data, get_compressed_data_hook,
1066                        frames_in_parallel_set);
1067   launch_fpmt_workers(ppi);
1068   sync_fpmt_workers(ppi);
1069   restore_workers_after_fpmt(ppi, frames_in_parallel_set);
1070 
1071   // Release cpi->scaled_ref_buf corresponding to frames in the current parallel
1072   // encode set.
1073   for (int i = 0; i < frames_in_parallel_set; ++i) {
1074     av1_release_scaled_references_fpmt(ppi->parallel_cpi[i]);
1075   }
1076   av1_decrement_ref_counts_fpmt(ppi->cpi->common.buffer_pool,
1077                                 ref_buffers_used_map);
1078   return AOM_CODEC_OK;
1079 }
1080 #endif  // CONFIG_FRAME_PARALLEL_ENCODE
1081 
launch_workers(MultiThreadInfo * const mt_info,int num_workers)1082 static AOM_INLINE void launch_workers(MultiThreadInfo *const mt_info,
1083                                       int num_workers) {
1084   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
1085   for (int i = num_workers - 1; i >= 0; i--) {
1086     AVxWorker *const worker = &mt_info->workers[i];
1087     if (i == 0)
1088       winterface->execute(worker);
1089     else
1090       winterface->launch(worker);
1091   }
1092 }
1093 
sync_enc_workers(MultiThreadInfo * const mt_info,AV1_COMMON * const cm,int num_workers)1094 static AOM_INLINE void sync_enc_workers(MultiThreadInfo *const mt_info,
1095                                         AV1_COMMON *const cm, int num_workers) {
1096   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
1097   int had_error = 0;
1098 
1099   // Encoding ends.
1100   for (int i = num_workers - 1; i > 0; i--) {
1101     AVxWorker *const worker = &mt_info->workers[i];
1102     had_error |= !winterface->sync(worker);
1103   }
1104 
1105   if (had_error)
1106     aom_internal_error(cm->error, AOM_CODEC_ERROR,
1107                        "Failed to encode tile data");
1108 }
1109 
accumulate_counters_enc_workers(AV1_COMP * cpi,int num_workers)1110 static AOM_INLINE void accumulate_counters_enc_workers(AV1_COMP *cpi,
1111                                                        int num_workers) {
1112   for (int i = num_workers - 1; i >= 0; i--) {
1113     AVxWorker *const worker = &cpi->mt_info.workers[i];
1114     EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
1115     cpi->intrabc_used |= thread_data->td->intrabc_used;
1116     cpi->deltaq_used |= thread_data->td->deltaq_used;
1117     // Accumulate cyclic refresh params.
1118     if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
1119         !frame_is_intra_only(&cpi->common))
1120       av1_accumulate_cyclic_refresh_counters(cpi->cyclic_refresh,
1121                                              &thread_data->td->mb);
1122     if (thread_data->td != &cpi->td) {
1123       if (cpi->oxcf.cost_upd_freq.mv < COST_UPD_OFF) {
1124         aom_free(thread_data->td->mb.mv_costs);
1125       }
1126       if (cpi->oxcf.cost_upd_freq.dv < COST_UPD_OFF) {
1127         aom_free(thread_data->td->mb.dv_costs);
1128       }
1129     }
1130     av1_dealloc_mb_data(&cpi->common, &thread_data->td->mb);
1131 
1132     // Accumulate counters.
1133     if (i > 0) {
1134       av1_accumulate_frame_counts(&cpi->counts, thread_data->td->counts);
1135       accumulate_rd_opt(&cpi->td, thread_data->td);
1136       cpi->td.mb.txfm_search_info.txb_split_count +=
1137           thread_data->td->mb.txfm_search_info.txb_split_count;
1138 #if CONFIG_SPEED_STATS
1139       cpi->td.mb.txfm_search_info.tx_search_count +=
1140           thread_data->td->mb.txfm_search_info.tx_search_count;
1141 #endif  // CONFIG_SPEED_STATS
1142     }
1143   }
1144 }
1145 
prepare_enc_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)1146 static AOM_INLINE void prepare_enc_workers(AV1_COMP *cpi, AVxWorkerHook hook,
1147                                            int num_workers) {
1148   MultiThreadInfo *const mt_info = &cpi->mt_info;
1149   AV1_COMMON *const cm = &cpi->common;
1150   for (int i = num_workers - 1; i >= 0; i--) {
1151     AVxWorker *const worker = &mt_info->workers[i];
1152     EncWorkerData *const thread_data = &mt_info->tile_thr_data[i];
1153 
1154     worker->hook = hook;
1155     worker->data1 = thread_data;
1156     worker->data2 = NULL;
1157 
1158     thread_data->thread_id = i;
1159     // Set the starting tile for each thread.
1160     thread_data->start = i;
1161 
1162     thread_data->cpi = cpi;
1163     if (i == 0) {
1164       thread_data->td = &cpi->td;
1165 #if !CONFIG_FRAME_PARALLEL_ENCODE
1166     }
1167 #else
1168     } else {
1169       thread_data->td = thread_data->original_td;
1170     }
1171 #endif  // CONFIG_FRAME_PARALLEL_ENCODE
1172 
1173     thread_data->td->intrabc_used = 0;
1174     thread_data->td->deltaq_used = 0;
1175     thread_data->td->abs_sum_level = 0;
1176 
1177     // Before encoding a frame, copy the thread data from cpi.
1178     if (thread_data->td != &cpi->td) {
1179       thread_data->td->mb = cpi->td.mb;
1180       thread_data->td->rd_counts = cpi->td.rd_counts;
1181       thread_data->td->mb.obmc_buffer = thread_data->td->obmc_buffer;
1182 
1183       for (int x = 0; x < 2; x++) {
1184         for (int y = 0; y < 2; y++) {
1185           memcpy(thread_data->td->hash_value_buffer[x][y],
1186                  cpi->td.mb.intrabc_hash_info.hash_value_buffer[x][y],
1187                  AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
1188                      sizeof(*thread_data->td->hash_value_buffer[0][0]));
1189           thread_data->td->mb.intrabc_hash_info.hash_value_buffer[x][y] =
1190               thread_data->td->hash_value_buffer[x][y];
1191         }
1192       }
1193       if (cpi->oxcf.cost_upd_freq.mv < COST_UPD_OFF) {
1194         CHECK_MEM_ERROR(cm, thread_data->td->mb.mv_costs,
1195                         (MvCosts *)aom_malloc(sizeof(MvCosts)));
1196         memcpy(thread_data->td->mb.mv_costs, cpi->td.mb.mv_costs,
1197                sizeof(MvCosts));
1198       }
1199       if (cpi->oxcf.cost_upd_freq.dv < COST_UPD_OFF) {
1200         CHECK_MEM_ERROR(cm, thread_data->td->mb.dv_costs,
1201                         (IntraBCMVCosts *)aom_malloc(sizeof(IntraBCMVCosts)));
1202         memcpy(thread_data->td->mb.dv_costs, cpi->td.mb.dv_costs,
1203                sizeof(IntraBCMVCosts));
1204       }
1205     }
1206     av1_alloc_mb_data(cm, &thread_data->td->mb,
1207                       cpi->sf.rt_sf.use_nonrd_pick_mode);
1208 
1209     // Reset cyclic refresh counters.
1210     av1_init_cyclic_refresh_counters(&thread_data->td->mb);
1211 
1212     if (thread_data->td->counts != &cpi->counts) {
1213       memcpy(thread_data->td->counts, &cpi->counts, sizeof(cpi->counts));
1214     }
1215 
1216     if (i > 0) {
1217       thread_data->td->mb.palette_buffer = thread_data->td->palette_buffer;
1218       thread_data->td->mb.comp_rd_buffer = thread_data->td->comp_rd_buffer;
1219       thread_data->td->mb.tmp_conv_dst = thread_data->td->tmp_conv_dst;
1220       for (int j = 0; j < 2; ++j) {
1221         thread_data->td->mb.tmp_pred_bufs[j] =
1222             thread_data->td->tmp_pred_bufs[j];
1223       }
1224       thread_data->td->mb.pixel_gradient_info =
1225           thread_data->td->pixel_gradient_info;
1226 
1227       thread_data->td->mb.e_mbd.tmp_conv_dst = thread_data->td->mb.tmp_conv_dst;
1228       for (int j = 0; j < 2; ++j) {
1229         thread_data->td->mb.e_mbd.tmp_obmc_bufs[j] =
1230             thread_data->td->mb.tmp_pred_bufs[j];
1231       }
1232     }
1233   }
1234 }
1235 
1236 #if !CONFIG_REALTIME_ONLY
fp_prepare_enc_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)1237 static AOM_INLINE void fp_prepare_enc_workers(AV1_COMP *cpi, AVxWorkerHook hook,
1238                                               int num_workers) {
1239   AV1_COMMON *const cm = &cpi->common;
1240   MultiThreadInfo *const mt_info = &cpi->mt_info;
1241   for (int i = num_workers - 1; i >= 0; i--) {
1242     AVxWorker *const worker = &mt_info->workers[i];
1243     EncWorkerData *const thread_data = &mt_info->tile_thr_data[i];
1244 
1245     worker->hook = hook;
1246     worker->data1 = thread_data;
1247     worker->data2 = NULL;
1248 
1249     thread_data->thread_id = i;
1250     // Set the starting tile for each thread.
1251     thread_data->start = i;
1252 
1253     thread_data->cpi = cpi;
1254     if (i == 0) {
1255       thread_data->td = &cpi->td;
1256 #if !CONFIG_FRAME_PARALLEL_ENCODE
1257     }
1258 #else
1259     } else {
1260       thread_data->td = thread_data->original_td;
1261     }
1262 #endif  // CONFIG_FRAME_PARALLEL_ENCODE
1263 
1264     // Before encoding a frame, copy the thread data from cpi.
1265     if (thread_data->td != &cpi->td) {
1266       thread_data->td->mb = cpi->td.mb;
1267       if (cpi->oxcf.cost_upd_freq.mv < COST_UPD_OFF) {
1268         CHECK_MEM_ERROR(cm, thread_data->td->mb.mv_costs,
1269                         (MvCosts *)aom_malloc(sizeof(MvCosts)));
1270         memcpy(thread_data->td->mb.mv_costs, cpi->td.mb.mv_costs,
1271                sizeof(MvCosts));
1272       }
1273       if (cpi->oxcf.cost_upd_freq.dv < COST_UPD_OFF) {
1274         CHECK_MEM_ERROR(cm, thread_data->td->mb.dv_costs,
1275                         (IntraBCMVCosts *)aom_malloc(sizeof(IntraBCMVCosts)));
1276         memcpy(thread_data->td->mb.dv_costs, cpi->td.mb.dv_costs,
1277                sizeof(IntraBCMVCosts));
1278       }
1279     }
1280 
1281     av1_alloc_mb_data(cm, &thread_data->td->mb,
1282                       cpi->sf.rt_sf.use_nonrd_pick_mode);
1283   }
1284 }
1285 #endif
1286 
1287 // Computes the number of workers for row multi-threading of encoding stage
compute_num_enc_row_mt_workers(AV1_COMMON * const cm,int max_threads)1288 static AOM_INLINE int compute_num_enc_row_mt_workers(AV1_COMMON *const cm,
1289                                                      int max_threads) {
1290   TileInfo tile_info;
1291   const int tile_cols = cm->tiles.cols;
1292   const int tile_rows = cm->tiles.rows;
1293   int total_num_threads_row_mt = 0;
1294   for (int row = 0; row < tile_rows; row++) {
1295     for (int col = 0; col < tile_cols; col++) {
1296       av1_tile_init(&tile_info, cm, row, col);
1297       const int num_sb_rows_in_tile = av1_get_sb_rows_in_tile(cm, tile_info);
1298       const int num_sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile_info);
1299       total_num_threads_row_mt +=
1300           AOMMIN((num_sb_cols_in_tile + 1) >> 1, num_sb_rows_in_tile);
1301     }
1302   }
1303   return AOMMIN(max_threads, total_num_threads_row_mt);
1304 }
1305 
1306 // Computes the number of workers for tile multi-threading of encoding stage
compute_num_enc_tile_mt_workers(AV1_COMMON * const cm,int max_threads)1307 static AOM_INLINE int compute_num_enc_tile_mt_workers(AV1_COMMON *const cm,
1308                                                       int max_threads) {
1309   const int tile_cols = cm->tiles.cols;
1310   const int tile_rows = cm->tiles.rows;
1311   return AOMMIN(max_threads, tile_cols * tile_rows);
1312 }
1313 
1314 // Find max worker of all MT stages
av1_get_max_num_workers(AV1_COMP * cpi)1315 int av1_get_max_num_workers(AV1_COMP *cpi) {
1316   int max_num_workers = 0;
1317   for (int i = MOD_FP; i < NUM_MT_MODULES; i++)
1318     max_num_workers =
1319         AOMMAX(cpi->ppi->p_mt_info.num_mod_workers[i], max_num_workers);
1320   assert(max_num_workers >= 1);
1321   return AOMMIN(max_num_workers, cpi->oxcf.max_threads);
1322 }
1323 
1324 // Computes the number of workers for encoding stage (row/tile multi-threading)
av1_compute_num_enc_workers(AV1_COMP * cpi,int max_workers)1325 int av1_compute_num_enc_workers(AV1_COMP *cpi, int max_workers) {
1326   if (max_workers <= 1) return 1;
1327   if (cpi->oxcf.row_mt)
1328     return compute_num_enc_row_mt_workers(&cpi->common, max_workers);
1329   else
1330     return compute_num_enc_tile_mt_workers(&cpi->common, max_workers);
1331 }
1332 
av1_encode_tiles_mt(AV1_COMP * cpi)1333 void av1_encode_tiles_mt(AV1_COMP *cpi) {
1334   AV1_COMMON *const cm = &cpi->common;
1335   MultiThreadInfo *const mt_info = &cpi->mt_info;
1336   const int tile_cols = cm->tiles.cols;
1337   const int tile_rows = cm->tiles.rows;
1338   int num_workers = mt_info->num_mod_workers[MOD_ENC];
1339 
1340   assert(IMPLIES(cpi->tile_data == NULL,
1341                  cpi->allocated_tiles < tile_cols * tile_rows));
1342   if (cpi->allocated_tiles < tile_cols * tile_rows) av1_alloc_tile_data(cpi);
1343 
1344   av1_init_tile_data(cpi);
1345   num_workers = AOMMIN(num_workers, mt_info->num_workers);
1346 
1347   prepare_enc_workers(cpi, enc_worker_hook, num_workers);
1348   launch_workers(&cpi->mt_info, num_workers);
1349   sync_enc_workers(&cpi->mt_info, cm, num_workers);
1350   accumulate_counters_enc_workers(cpi, num_workers);
1351 }
1352 
1353 // Accumulate frame counts. FRAME_COUNTS consist solely of 'unsigned int'
1354 // members, so we treat it as an array, and sum over the whole length.
av1_accumulate_frame_counts(FRAME_COUNTS * acc_counts,const FRAME_COUNTS * counts)1355 void av1_accumulate_frame_counts(FRAME_COUNTS *acc_counts,
1356                                  const FRAME_COUNTS *counts) {
1357   unsigned int *const acc = (unsigned int *)acc_counts;
1358   const unsigned int *const cnt = (const unsigned int *)counts;
1359 
1360   const unsigned int n_counts = sizeof(FRAME_COUNTS) / sizeof(unsigned int);
1361 
1362   for (unsigned int i = 0; i < n_counts; i++) acc[i] += cnt[i];
1363 }
1364 
1365 // Computes the maximum number of sb_rows for row multi-threading of encoding
1366 // stage
compute_max_sb_rows_cols(AV1_COMP * cpi,int * max_sb_rows,int * max_sb_cols)1367 static AOM_INLINE void compute_max_sb_rows_cols(AV1_COMP *cpi, int *max_sb_rows,
1368                                                 int *max_sb_cols) {
1369   AV1_COMMON *const cm = &cpi->common;
1370   const int tile_cols = cm->tiles.cols;
1371   const int tile_rows = cm->tiles.rows;
1372   for (int row = 0; row < tile_rows; row++) {
1373     for (int col = 0; col < tile_cols; col++) {
1374       const int tile_index = row * cm->tiles.cols + col;
1375       TileInfo tile_info = cpi->tile_data[tile_index].tile_info;
1376       const int num_sb_rows_in_tile = av1_get_sb_rows_in_tile(cm, tile_info);
1377       const int num_sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile_info);
1378       *max_sb_rows = AOMMAX(*max_sb_rows, num_sb_rows_in_tile);
1379       *max_sb_cols = AOMMAX(*max_sb_cols, num_sb_cols_in_tile);
1380     }
1381   }
1382 }
1383 
1384 #if !CONFIG_REALTIME_ONLY
1385 // Computes the number of workers for firstpass stage (row/tile multi-threading)
av1_fp_compute_num_enc_workers(AV1_COMP * cpi)1386 int av1_fp_compute_num_enc_workers(AV1_COMP *cpi) {
1387   AV1_COMMON *cm = &cpi->common;
1388   const int tile_cols = cm->tiles.cols;
1389   const int tile_rows = cm->tiles.rows;
1390   int total_num_threads_row_mt = 0;
1391   TileInfo tile_info;
1392 
1393   if (cpi->oxcf.max_threads <= 1) return 1;
1394 
1395   for (int row = 0; row < tile_rows; row++) {
1396     for (int col = 0; col < tile_cols; col++) {
1397       av1_tile_init(&tile_info, cm, row, col);
1398       const int num_mb_rows_in_tile =
1399           av1_get_unit_rows_in_tile(tile_info, cpi->fp_block_size);
1400       const int num_mb_cols_in_tile =
1401           av1_get_unit_cols_in_tile(tile_info, cpi->fp_block_size);
1402       total_num_threads_row_mt +=
1403           AOMMIN((num_mb_cols_in_tile + 1) >> 1, num_mb_rows_in_tile);
1404     }
1405   }
1406   return AOMMIN(cpi->oxcf.max_threads, total_num_threads_row_mt);
1407 }
1408 
1409 // Computes the maximum number of mb_rows for row multi-threading of firstpass
1410 // stage
fp_compute_max_mb_rows(const AV1_COMMON * const cm,const TileDataEnc * const tile_data,const BLOCK_SIZE fp_block_size)1411 static AOM_INLINE int fp_compute_max_mb_rows(const AV1_COMMON *const cm,
1412                                              const TileDataEnc *const tile_data,
1413                                              const BLOCK_SIZE fp_block_size) {
1414   const int tile_cols = cm->tiles.cols;
1415   const int tile_rows = cm->tiles.rows;
1416   int max_mb_rows = 0;
1417   for (int row = 0; row < tile_rows; row++) {
1418     for (int col = 0; col < tile_cols; col++) {
1419       const int tile_index = row * cm->tiles.cols + col;
1420       TileInfo tile_info = tile_data[tile_index].tile_info;
1421       const int num_mb_rows_in_tile =
1422           av1_get_unit_rows_in_tile(tile_info, fp_block_size);
1423       max_mb_rows = AOMMAX(max_mb_rows, num_mb_rows_in_tile);
1424     }
1425   }
1426   return max_mb_rows;
1427 }
1428 #endif
1429 
av1_encode_tiles_row_mt(AV1_COMP * cpi)1430 void av1_encode_tiles_row_mt(AV1_COMP *cpi) {
1431   AV1_COMMON *const cm = &cpi->common;
1432   MultiThreadInfo *const mt_info = &cpi->mt_info;
1433   AV1EncRowMultiThreadInfo *const enc_row_mt = &mt_info->enc_row_mt;
1434   const int tile_cols = cm->tiles.cols;
1435   const int tile_rows = cm->tiles.rows;
1436   int *thread_id_to_tile_id = enc_row_mt->thread_id_to_tile_id;
1437   int max_sb_rows = 0, max_sb_cols = 0;
1438   int num_workers = mt_info->num_mod_workers[MOD_ENC];
1439 
1440   assert(IMPLIES(cpi->tile_data == NULL,
1441                  cpi->allocated_tiles < tile_cols * tile_rows));
1442   if (cpi->allocated_tiles < tile_cols * tile_rows) {
1443     av1_row_mt_mem_dealloc(cpi);
1444     av1_alloc_tile_data(cpi);
1445   }
1446 
1447   av1_init_tile_data(cpi);
1448 
1449   compute_max_sb_rows_cols(cpi, &max_sb_rows, &max_sb_cols);
1450 
1451   if (enc_row_mt->allocated_tile_cols != tile_cols ||
1452       enc_row_mt->allocated_tile_rows != tile_rows ||
1453       enc_row_mt->allocated_rows != max_sb_rows ||
1454       enc_row_mt->allocated_cols != (max_sb_cols - 1)) {
1455     av1_row_mt_mem_dealloc(cpi);
1456     row_mt_mem_alloc(cpi, max_sb_rows, max_sb_cols,
1457                      cpi->oxcf.algo_cfg.cdf_update_mode);
1458   }
1459 
1460   memset(thread_id_to_tile_id, -1,
1461          sizeof(*thread_id_to_tile_id) * MAX_NUM_THREADS);
1462 
1463   for (int tile_row = 0; tile_row < tile_rows; tile_row++) {
1464     for (int tile_col = 0; tile_col < tile_cols; tile_col++) {
1465       int tile_index = tile_row * tile_cols + tile_col;
1466       TileDataEnc *const this_tile = &cpi->tile_data[tile_index];
1467       AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
1468 
1469       // Initialize num_finished_cols to -1 for all rows.
1470       memset(row_mt_sync->num_finished_cols, -1,
1471              sizeof(*row_mt_sync->num_finished_cols) * max_sb_rows);
1472       row_mt_sync->next_mi_row = this_tile->tile_info.mi_row_start;
1473       row_mt_sync->num_threads_working = 0;
1474 
1475       av1_inter_mode_data_init(this_tile);
1476       av1_zero_above_context(cm, &cpi->td.mb.e_mbd,
1477                              this_tile->tile_info.mi_col_start,
1478                              this_tile->tile_info.mi_col_end, tile_row);
1479     }
1480   }
1481 
1482   num_workers = AOMMIN(num_workers, mt_info->num_workers);
1483 
1484   assign_tile_to_thread(thread_id_to_tile_id, tile_cols * tile_rows,
1485                         num_workers);
1486   prepare_enc_workers(cpi, enc_row_mt_worker_hook, num_workers);
1487   launch_workers(&cpi->mt_info, num_workers);
1488   sync_enc_workers(&cpi->mt_info, cm, num_workers);
1489   if (cm->delta_q_info.delta_lf_present_flag) update_delta_lf_for_row_mt(cpi);
1490   accumulate_counters_enc_workers(cpi, num_workers);
1491 }
1492 
1493 #if !CONFIG_REALTIME_ONLY
av1_fp_encode_tiles_row_mt(AV1_COMP * cpi)1494 void av1_fp_encode_tiles_row_mt(AV1_COMP *cpi) {
1495   AV1_COMMON *const cm = &cpi->common;
1496   MultiThreadInfo *const mt_info = &cpi->mt_info;
1497   AV1EncRowMultiThreadInfo *const enc_row_mt = &mt_info->enc_row_mt;
1498   const int tile_cols = cm->tiles.cols;
1499   const int tile_rows = cm->tiles.rows;
1500   int *thread_id_to_tile_id = enc_row_mt->thread_id_to_tile_id;
1501   int num_workers = 0;
1502   int max_mb_rows = 0;
1503 
1504   assert(IMPLIES(cpi->tile_data == NULL,
1505                  cpi->allocated_tiles < tile_cols * tile_rows));
1506   if (cpi->allocated_tiles < tile_cols * tile_rows) {
1507     av1_row_mt_mem_dealloc(cpi);
1508     av1_alloc_tile_data(cpi);
1509   }
1510 
1511   av1_init_tile_data(cpi);
1512 
1513   const BLOCK_SIZE fp_block_size = cpi->fp_block_size;
1514   max_mb_rows = fp_compute_max_mb_rows(cm, cpi->tile_data, fp_block_size);
1515 
1516   // For pass = 1, compute the no. of workers needed. For single-pass encode
1517   // (pass = 0), no. of workers are already computed.
1518   if (mt_info->num_mod_workers[MOD_FP] == 0)
1519     num_workers = av1_fp_compute_num_enc_workers(cpi);
1520   else
1521     num_workers = mt_info->num_mod_workers[MOD_FP];
1522 
1523   if (enc_row_mt->allocated_tile_cols != tile_cols ||
1524       enc_row_mt->allocated_tile_rows != tile_rows ||
1525       enc_row_mt->allocated_rows != max_mb_rows) {
1526     av1_row_mt_mem_dealloc(cpi);
1527     row_mt_mem_alloc(cpi, max_mb_rows, -1, 0);
1528   }
1529 
1530   memset(thread_id_to_tile_id, -1,
1531          sizeof(*thread_id_to_tile_id) * MAX_NUM_THREADS);
1532 
1533   for (int tile_row = 0; tile_row < tile_rows; tile_row++) {
1534     for (int tile_col = 0; tile_col < tile_cols; tile_col++) {
1535       int tile_index = tile_row * tile_cols + tile_col;
1536       TileDataEnc *const this_tile = &cpi->tile_data[tile_index];
1537       AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
1538 
1539       // Initialize num_finished_cols to -1 for all rows.
1540       memset(row_mt_sync->num_finished_cols, -1,
1541              sizeof(*row_mt_sync->num_finished_cols) * max_mb_rows);
1542       row_mt_sync->next_mi_row = this_tile->tile_info.mi_row_start;
1543       row_mt_sync->num_threads_working = 0;
1544     }
1545   }
1546 
1547   num_workers = AOMMIN(num_workers, mt_info->num_workers);
1548   assign_tile_to_thread(thread_id_to_tile_id, tile_cols * tile_rows,
1549                         num_workers);
1550   fp_prepare_enc_workers(cpi, fp_enc_row_mt_worker_hook, num_workers);
1551   launch_workers(&cpi->mt_info, num_workers);
1552   sync_enc_workers(&cpi->mt_info, cm, num_workers);
1553   for (int i = num_workers - 1; i >= 0; i--) {
1554     EncWorkerData *const thread_data = &cpi->mt_info.tile_thr_data[i];
1555     if (thread_data->td != &cpi->td) {
1556       if (cpi->oxcf.cost_upd_freq.mv < COST_UPD_OFF) {
1557         aom_free(thread_data->td->mb.mv_costs);
1558       }
1559       if (cpi->oxcf.cost_upd_freq.dv < COST_UPD_OFF) {
1560         aom_free(thread_data->td->mb.dv_costs);
1561       }
1562     }
1563     av1_dealloc_mb_data(cm, &thread_data->td->mb);
1564   }
1565 }
1566 
av1_tpl_row_mt_sync_read_dummy(AV1TplRowMultiThreadSync * tpl_mt_sync,int r,int c)1567 void av1_tpl_row_mt_sync_read_dummy(AV1TplRowMultiThreadSync *tpl_mt_sync,
1568                                     int r, int c) {
1569   (void)tpl_mt_sync;
1570   (void)r;
1571   (void)c;
1572   return;
1573 }
1574 
av1_tpl_row_mt_sync_write_dummy(AV1TplRowMultiThreadSync * tpl_mt_sync,int r,int c,int cols)1575 void av1_tpl_row_mt_sync_write_dummy(AV1TplRowMultiThreadSync *tpl_mt_sync,
1576                                      int r, int c, int cols) {
1577   (void)tpl_mt_sync;
1578   (void)r;
1579   (void)c;
1580   (void)cols;
1581   return;
1582 }
1583 
av1_tpl_row_mt_sync_read(AV1TplRowMultiThreadSync * tpl_row_mt_sync,int r,int c)1584 void av1_tpl_row_mt_sync_read(AV1TplRowMultiThreadSync *tpl_row_mt_sync, int r,
1585                               int c) {
1586 #if CONFIG_MULTITHREAD
1587   int nsync = tpl_row_mt_sync->sync_range;
1588 
1589   if (r) {
1590     pthread_mutex_t *const mutex = &tpl_row_mt_sync->mutex_[r - 1];
1591     pthread_mutex_lock(mutex);
1592 
1593     while (c > tpl_row_mt_sync->num_finished_cols[r - 1] - nsync)
1594       pthread_cond_wait(&tpl_row_mt_sync->cond_[r - 1], mutex);
1595     pthread_mutex_unlock(mutex);
1596   }
1597 #else
1598   (void)tpl_row_mt_sync;
1599   (void)r;
1600   (void)c;
1601 #endif  // CONFIG_MULTITHREAD
1602 }
1603 
av1_tpl_row_mt_sync_write(AV1TplRowMultiThreadSync * tpl_row_mt_sync,int r,int c,int cols)1604 void av1_tpl_row_mt_sync_write(AV1TplRowMultiThreadSync *tpl_row_mt_sync, int r,
1605                                int c, int cols) {
1606 #if CONFIG_MULTITHREAD
1607   int nsync = tpl_row_mt_sync->sync_range;
1608   int cur;
1609   // Only signal when there are enough encoded blocks for next row to run.
1610   int sig = 1;
1611 
1612   if (c < cols - 1) {
1613     cur = c;
1614     if (c % nsync) sig = 0;
1615   } else {
1616     cur = cols + nsync;
1617   }
1618 
1619   if (sig) {
1620     pthread_mutex_lock(&tpl_row_mt_sync->mutex_[r]);
1621 
1622     tpl_row_mt_sync->num_finished_cols[r] = cur;
1623 
1624     pthread_cond_signal(&tpl_row_mt_sync->cond_[r]);
1625     pthread_mutex_unlock(&tpl_row_mt_sync->mutex_[r]);
1626   }
1627 #else
1628   (void)tpl_row_mt_sync;
1629   (void)r;
1630   (void)c;
1631   (void)cols;
1632 #endif  // CONFIG_MULTITHREAD
1633 }
1634 
1635 // Each worker calls tpl_worker_hook() and computes the tpl data.
tpl_worker_hook(void * arg1,void * unused)1636 static int tpl_worker_hook(void *arg1, void *unused) {
1637   (void)unused;
1638   EncWorkerData *thread_data = (EncWorkerData *)arg1;
1639   AV1_COMP *cpi = thread_data->cpi;
1640   AV1_COMMON *cm = &cpi->common;
1641   MACROBLOCK *x = &thread_data->td->mb;
1642   MACROBLOCKD *xd = &x->e_mbd;
1643   TplTxfmStats *tpl_txfm_stats = &thread_data->td->tpl_txfm_stats;
1644   CommonModeInfoParams *mi_params = &cm->mi_params;
1645   BLOCK_SIZE bsize = convert_length_to_bsize(cpi->ppi->tpl_data.tpl_bsize_1d);
1646   TX_SIZE tx_size = max_txsize_lookup[bsize];
1647   int mi_height = mi_size_high[bsize];
1648   int num_active_workers = cpi->ppi->tpl_data.tpl_mt_sync.num_threads_working;
1649 
1650   av1_init_tpl_txfm_stats(tpl_txfm_stats);
1651 
1652   for (int mi_row = thread_data->start * mi_height; mi_row < mi_params->mi_rows;
1653        mi_row += num_active_workers * mi_height) {
1654     // Motion estimation row boundary
1655     av1_set_mv_row_limits(mi_params, &x->mv_limits, mi_row, mi_height,
1656                           cpi->oxcf.border_in_pixels);
1657     xd->mb_to_top_edge = -GET_MV_SUBPEL(mi_row * MI_SIZE);
1658     xd->mb_to_bottom_edge =
1659         GET_MV_SUBPEL((mi_params->mi_rows - mi_height - mi_row) * MI_SIZE);
1660     av1_mc_flow_dispenser_row(cpi, tpl_txfm_stats, x, mi_row, bsize, tx_size);
1661   }
1662   return 1;
1663 }
1664 
1665 // Deallocate tpl synchronization related mutex and data.
av1_tpl_dealloc(AV1TplRowMultiThreadSync * tpl_sync)1666 void av1_tpl_dealloc(AV1TplRowMultiThreadSync *tpl_sync) {
1667   assert(tpl_sync != NULL);
1668 
1669 #if CONFIG_MULTITHREAD
1670   if (tpl_sync->mutex_ != NULL) {
1671     for (int i = 0; i < tpl_sync->rows; ++i)
1672       pthread_mutex_destroy(&tpl_sync->mutex_[i]);
1673     aom_free(tpl_sync->mutex_);
1674   }
1675   if (tpl_sync->cond_ != NULL) {
1676     for (int i = 0; i < tpl_sync->rows; ++i)
1677       pthread_cond_destroy(&tpl_sync->cond_[i]);
1678     aom_free(tpl_sync->cond_);
1679   }
1680 #endif  // CONFIG_MULTITHREAD
1681 
1682   aom_free(tpl_sync->num_finished_cols);
1683   // clear the structure as the source of this call may be a resize in which
1684   // case this call will be followed by an _alloc() which may fail.
1685   av1_zero(*tpl_sync);
1686 }
1687 
1688 // Allocate memory for tpl row synchronization.
av1_tpl_alloc(AV1TplRowMultiThreadSync * tpl_sync,AV1_COMMON * cm,int mb_rows)1689 void av1_tpl_alloc(AV1TplRowMultiThreadSync *tpl_sync, AV1_COMMON *cm,
1690                    int mb_rows) {
1691   tpl_sync->rows = mb_rows;
1692 #if CONFIG_MULTITHREAD
1693   {
1694     CHECK_MEM_ERROR(cm, tpl_sync->mutex_,
1695                     aom_malloc(sizeof(*tpl_sync->mutex_) * mb_rows));
1696     if (tpl_sync->mutex_) {
1697       for (int i = 0; i < mb_rows; ++i)
1698         pthread_mutex_init(&tpl_sync->mutex_[i], NULL);
1699     }
1700 
1701     CHECK_MEM_ERROR(cm, tpl_sync->cond_,
1702                     aom_malloc(sizeof(*tpl_sync->cond_) * mb_rows));
1703     if (tpl_sync->cond_) {
1704       for (int i = 0; i < mb_rows; ++i)
1705         pthread_cond_init(&tpl_sync->cond_[i], NULL);
1706     }
1707   }
1708 #endif  // CONFIG_MULTITHREAD
1709   CHECK_MEM_ERROR(cm, tpl_sync->num_finished_cols,
1710                   aom_malloc(sizeof(*tpl_sync->num_finished_cols) * mb_rows));
1711 
1712   // Set up nsync.
1713   tpl_sync->sync_range = 1;
1714 }
1715 
1716 // Each worker is prepared by assigning the hook function and individual thread
1717 // data.
prepare_tpl_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)1718 static AOM_INLINE void prepare_tpl_workers(AV1_COMP *cpi, AVxWorkerHook hook,
1719                                            int num_workers) {
1720   MultiThreadInfo *mt_info = &cpi->mt_info;
1721   for (int i = num_workers - 1; i >= 0; i--) {
1722     AVxWorker *worker = &mt_info->workers[i];
1723     EncWorkerData *thread_data = &mt_info->tile_thr_data[i];
1724 
1725     worker->hook = hook;
1726     worker->data1 = thread_data;
1727     worker->data2 = NULL;
1728 
1729     thread_data->thread_id = i;
1730     // Set the starting tile for each thread.
1731     thread_data->start = i;
1732 
1733     thread_data->cpi = cpi;
1734     if (i == 0) {
1735       thread_data->td = &cpi->td;
1736 #if !CONFIG_FRAME_PARALLEL_ENCODE
1737     }
1738 #else
1739     } else {
1740       thread_data->td = thread_data->original_td;
1741     }
1742 #endif  // CONFIG_FRAME_PARALLEL_ENCODE
1743 
1744     // Before encoding a frame, copy the thread data from cpi.
1745     if (thread_data->td != &cpi->td) {
1746       thread_data->td->mb = cpi->td.mb;
1747       // OBMC buffers are used only to init MS params and remain unused when
1748       // called from tpl, hence set the buffers to defaults.
1749       av1_init_obmc_buffer(&thread_data->td->mb.obmc_buffer);
1750       thread_data->td->mb.tmp_conv_dst = thread_data->td->tmp_conv_dst;
1751       thread_data->td->mb.e_mbd.tmp_conv_dst = thread_data->td->mb.tmp_conv_dst;
1752     }
1753   }
1754 }
1755 
1756 // Accumulate transform stats after tpl.
tpl_accumulate_txfm_stats(ThreadData * main_td,const MultiThreadInfo * mt_info,int num_workers)1757 static void tpl_accumulate_txfm_stats(ThreadData *main_td,
1758                                       const MultiThreadInfo *mt_info,
1759                                       int num_workers) {
1760   TplTxfmStats *accumulated_stats = &main_td->tpl_txfm_stats;
1761   for (int i = num_workers - 1; i >= 0; i--) {
1762     AVxWorker *const worker = &mt_info->workers[i];
1763     EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
1764     ThreadData *td = thread_data->td;
1765     if (td != main_td) {
1766       const TplTxfmStats *tpl_txfm_stats = &td->tpl_txfm_stats;
1767       av1_accumulate_tpl_txfm_stats(tpl_txfm_stats, accumulated_stats);
1768     }
1769   }
1770 }
1771 
1772 // Implements multi-threading for tpl.
av1_mc_flow_dispenser_mt(AV1_COMP * cpi)1773 void av1_mc_flow_dispenser_mt(AV1_COMP *cpi) {
1774   AV1_COMMON *cm = &cpi->common;
1775   CommonModeInfoParams *mi_params = &cm->mi_params;
1776   MultiThreadInfo *mt_info = &cpi->mt_info;
1777   TplParams *tpl_data = &cpi->ppi->tpl_data;
1778   AV1TplRowMultiThreadSync *tpl_sync = &tpl_data->tpl_mt_sync;
1779   int mb_rows = mi_params->mb_rows;
1780   int num_workers =
1781       AOMMIN(mt_info->num_mod_workers[MOD_TPL], mt_info->num_workers);
1782 
1783   if (mb_rows != tpl_sync->rows) {
1784     av1_tpl_dealloc(tpl_sync);
1785     av1_tpl_alloc(tpl_sync, cm, mb_rows);
1786   }
1787   tpl_sync->num_threads_working = num_workers;
1788 
1789   // Initialize cur_mb_col to -1 for all MB rows.
1790   memset(tpl_sync->num_finished_cols, -1,
1791          sizeof(*tpl_sync->num_finished_cols) * mb_rows);
1792 
1793   prepare_tpl_workers(cpi, tpl_worker_hook, num_workers);
1794   launch_workers(&cpi->mt_info, num_workers);
1795   sync_enc_workers(&cpi->mt_info, cm, num_workers);
1796   tpl_accumulate_txfm_stats(&cpi->td, &cpi->mt_info, num_workers);
1797 }
1798 
1799 // Deallocate memory for temporal filter multi-thread synchronization.
av1_tf_mt_dealloc(AV1TemporalFilterSync * tf_sync)1800 void av1_tf_mt_dealloc(AV1TemporalFilterSync *tf_sync) {
1801   assert(tf_sync != NULL);
1802 #if CONFIG_MULTITHREAD
1803   if (tf_sync->mutex_ != NULL) {
1804     pthread_mutex_destroy(tf_sync->mutex_);
1805     aom_free(tf_sync->mutex_);
1806   }
1807 #endif  // CONFIG_MULTITHREAD
1808   tf_sync->next_tf_row = 0;
1809 }
1810 
1811 // Checks if a job is available. If job is available,
1812 // populates next_tf_row and returns 1, else returns 0.
tf_get_next_job(AV1TemporalFilterSync * tf_mt_sync,int * current_mb_row,int mb_rows)1813 static AOM_INLINE int tf_get_next_job(AV1TemporalFilterSync *tf_mt_sync,
1814                                       int *current_mb_row, int mb_rows) {
1815   int do_next_row = 0;
1816 #if CONFIG_MULTITHREAD
1817   pthread_mutex_t *tf_mutex_ = tf_mt_sync->mutex_;
1818   pthread_mutex_lock(tf_mutex_);
1819 #endif
1820   if (tf_mt_sync->next_tf_row < mb_rows) {
1821     *current_mb_row = tf_mt_sync->next_tf_row;
1822     tf_mt_sync->next_tf_row++;
1823     do_next_row = 1;
1824   }
1825 #if CONFIG_MULTITHREAD
1826   pthread_mutex_unlock(tf_mutex_);
1827 #endif
1828   return do_next_row;
1829 }
1830 
1831 // Hook function for each thread in temporal filter multi-threading.
tf_worker_hook(void * arg1,void * unused)1832 static int tf_worker_hook(void *arg1, void *unused) {
1833   (void)unused;
1834   EncWorkerData *thread_data = (EncWorkerData *)arg1;
1835   AV1_COMP *cpi = thread_data->cpi;
1836   ThreadData *td = thread_data->td;
1837   TemporalFilterCtx *tf_ctx = &cpi->tf_ctx;
1838   AV1TemporalFilterSync *tf_sync = &cpi->mt_info.tf_sync;
1839   const struct scale_factors *scale = &cpi->tf_ctx.sf;
1840   const int num_planes = av1_num_planes(&cpi->common);
1841   assert(num_planes >= 1 && num_planes <= MAX_MB_PLANE);
1842 
1843   MACROBLOCKD *mbd = &td->mb.e_mbd;
1844   uint8_t *input_buffer[MAX_MB_PLANE];
1845   MB_MODE_INFO **input_mb_mode_info;
1846   tf_save_state(mbd, &input_mb_mode_info, input_buffer, num_planes);
1847   tf_setup_macroblockd(mbd, &td->tf_data, scale);
1848 
1849   int current_mb_row = -1;
1850 
1851   while (tf_get_next_job(tf_sync, &current_mb_row, tf_ctx->mb_rows))
1852     av1_tf_do_filtering_row(cpi, td, current_mb_row);
1853 
1854   tf_restore_state(mbd, input_mb_mode_info, input_buffer, num_planes);
1855 
1856   return 1;
1857 }
1858 
1859 // Assigns temporal filter hook function and thread data to each worker.
prepare_tf_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers,int is_highbitdepth)1860 static void prepare_tf_workers(AV1_COMP *cpi, AVxWorkerHook hook,
1861                                int num_workers, int is_highbitdepth) {
1862   MultiThreadInfo *mt_info = &cpi->mt_info;
1863   mt_info->tf_sync.next_tf_row = 0;
1864   for (int i = num_workers - 1; i >= 0; i--) {
1865     AVxWorker *worker = &mt_info->workers[i];
1866     EncWorkerData *thread_data = &mt_info->tile_thr_data[i];
1867 
1868     worker->hook = hook;
1869     worker->data1 = thread_data;
1870     worker->data2 = NULL;
1871 
1872     thread_data->thread_id = i;
1873     // Set the starting tile for each thread.
1874     thread_data->start = i;
1875 
1876     thread_data->cpi = cpi;
1877     if (i == 0) {
1878       thread_data->td = &cpi->td;
1879 #if !CONFIG_FRAME_PARALLEL_ENCODE
1880     }
1881 #else
1882     } else {
1883       thread_data->td = thread_data->original_td;
1884     }
1885 #endif  // CONFIG_FRAME_PARALLEL_ENCODE
1886 
1887     // Before encoding a frame, copy the thread data from cpi.
1888     if (thread_data->td != &cpi->td) {
1889       thread_data->td->mb = cpi->td.mb;
1890       // OBMC buffers are used only to init MS params and remain unused when
1891       // called from tf, hence set the buffers to defaults.
1892       av1_init_obmc_buffer(&thread_data->td->mb.obmc_buffer);
1893       tf_alloc_and_reset_data(&thread_data->td->tf_data, cpi->tf_ctx.num_pels,
1894                               is_highbitdepth);
1895     }
1896   }
1897 }
1898 
1899 // Deallocate thread specific data for temporal filter.
tf_dealloc_thread_data(AV1_COMP * cpi,int num_workers,int is_highbitdepth)1900 static void tf_dealloc_thread_data(AV1_COMP *cpi, int num_workers,
1901                                    int is_highbitdepth) {
1902   MultiThreadInfo *mt_info = &cpi->mt_info;
1903   for (int i = num_workers - 1; i >= 0; i--) {
1904     EncWorkerData *thread_data = &mt_info->tile_thr_data[i];
1905     ThreadData *td = thread_data->td;
1906     if (td != &cpi->td) tf_dealloc_data(&td->tf_data, is_highbitdepth);
1907   }
1908 }
1909 
1910 // Accumulate sse and sum after temporal filtering.
tf_accumulate_frame_diff(AV1_COMP * cpi,int num_workers)1911 static void tf_accumulate_frame_diff(AV1_COMP *cpi, int num_workers) {
1912   FRAME_DIFF *total_diff = &cpi->td.tf_data.diff;
1913   for (int i = num_workers - 1; i >= 0; i--) {
1914     AVxWorker *const worker = &cpi->mt_info.workers[i];
1915     EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
1916     ThreadData *td = thread_data->td;
1917     FRAME_DIFF *diff = &td->tf_data.diff;
1918     if (td != &cpi->td) {
1919       total_diff->sse += diff->sse;
1920       total_diff->sum += diff->sum;
1921     }
1922   }
1923 }
1924 
1925 // Implements multi-threading for temporal filter.
av1_tf_do_filtering_mt(AV1_COMP * cpi)1926 void av1_tf_do_filtering_mt(AV1_COMP *cpi) {
1927   AV1_COMMON *cm = &cpi->common;
1928   MultiThreadInfo *mt_info = &cpi->mt_info;
1929   const int is_highbitdepth = cpi->tf_ctx.is_highbitdepth;
1930 
1931   int num_workers =
1932       AOMMIN(mt_info->num_mod_workers[MOD_TF], mt_info->num_workers);
1933 
1934   prepare_tf_workers(cpi, tf_worker_hook, num_workers, is_highbitdepth);
1935   launch_workers(mt_info, num_workers);
1936   sync_enc_workers(mt_info, cm, num_workers);
1937   tf_accumulate_frame_diff(cpi, num_workers);
1938   tf_dealloc_thread_data(cpi, num_workers, is_highbitdepth);
1939 }
1940 
1941 // Checks if a job is available in the current direction. If a job is available,
1942 // frame_idx will be populated and returns 1, else returns 0.
get_next_gm_job(AV1_COMP * cpi,int * frame_idx,int cur_dir)1943 static AOM_INLINE int get_next_gm_job(AV1_COMP *cpi, int *frame_idx,
1944                                       int cur_dir) {
1945   GlobalMotionInfo *gm_info = &cpi->gm_info;
1946   JobInfo *job_info = &cpi->mt_info.gm_sync.job_info;
1947 
1948   int total_refs = gm_info->num_ref_frames[cur_dir];
1949   int8_t cur_frame_to_process = job_info->next_frame_to_process[cur_dir];
1950 
1951   if (cur_frame_to_process < total_refs && !job_info->early_exit[cur_dir]) {
1952     *frame_idx = gm_info->reference_frames[cur_dir][cur_frame_to_process].frame;
1953     job_info->next_frame_to_process[cur_dir] += 1;
1954     return 1;
1955   }
1956   return 0;
1957 }
1958 
1959 // Switches the current direction and calls the function get_next_gm_job() if
1960 // the speed feature 'prune_ref_frame_for_gm_search' is not set.
switch_direction(AV1_COMP * cpi,int * frame_idx,int * cur_dir)1961 static AOM_INLINE void switch_direction(AV1_COMP *cpi, int *frame_idx,
1962                                         int *cur_dir) {
1963   if (cpi->sf.gm_sf.prune_ref_frame_for_gm_search) return;
1964   // Switch the direction and get next job
1965   *cur_dir = !(*cur_dir);
1966   get_next_gm_job(cpi, frame_idx, *(cur_dir));
1967 }
1968 
1969 // Initializes inliers, num_inliers and segment_map.
init_gm_thread_data(const GlobalMotionInfo * gm_info,GlobalMotionThreadData * thread_data)1970 static AOM_INLINE void init_gm_thread_data(
1971     const GlobalMotionInfo *gm_info, GlobalMotionThreadData *thread_data) {
1972   for (int m = 0; m < RANSAC_NUM_MOTIONS; m++) {
1973     MotionModel motion_params = thread_data->params_by_motion[m];
1974     av1_zero(motion_params.params);
1975     motion_params.num_inliers = 0;
1976   }
1977 
1978   av1_zero_array(thread_data->segment_map,
1979                  gm_info->segment_map_w * gm_info->segment_map_h);
1980 }
1981 
1982 // Hook function for each thread in global motion multi-threading.
gm_mt_worker_hook(void * arg1,void * unused)1983 static int gm_mt_worker_hook(void *arg1, void *unused) {
1984   (void)unused;
1985 
1986   EncWorkerData *thread_data = (EncWorkerData *)arg1;
1987   AV1_COMP *cpi = thread_data->cpi;
1988   GlobalMotionInfo *gm_info = &cpi->gm_info;
1989   MultiThreadInfo *mt_info = &cpi->mt_info;
1990   JobInfo *job_info = &mt_info->gm_sync.job_info;
1991   int thread_id = thread_data->thread_id;
1992   GlobalMotionThreadData *gm_thread_data =
1993       &mt_info->gm_sync.thread_data[thread_id];
1994   int cur_dir = job_info->thread_id_to_dir[thread_id];
1995 #if CONFIG_MULTITHREAD
1996   pthread_mutex_t *gm_mt_mutex_ = mt_info->gm_sync.mutex_;
1997 #endif
1998 
1999   while (1) {
2000     int ref_buf_idx = -1;
2001     int ref_frame_idx = -1;
2002 
2003 #if CONFIG_MULTITHREAD
2004     pthread_mutex_lock(gm_mt_mutex_);
2005 #endif
2006 
2007     // Populates ref_buf_idx(the reference frame type) for which global motion
2008     // estimation will be done.
2009     if (!get_next_gm_job(cpi, &ref_buf_idx, cur_dir)) {
2010       // No jobs are available for the current direction. Switch
2011       // to other direction and get the next job, if available.
2012       switch_direction(cpi, &ref_buf_idx, &cur_dir);
2013     }
2014 
2015     // 'ref_frame_idx' holds the index of the current reference frame type in
2016     // gm_info->reference_frames. job_info->next_frame_to_process will be
2017     // incremented in get_next_gm_job() and hence subtracting by 1.
2018     ref_frame_idx = job_info->next_frame_to_process[cur_dir] - 1;
2019 
2020 #if CONFIG_MULTITHREAD
2021     pthread_mutex_unlock(gm_mt_mutex_);
2022 #endif
2023 
2024     if (ref_buf_idx == -1) break;
2025 
2026     init_gm_thread_data(gm_info, gm_thread_data);
2027 
2028     // Compute global motion for the given ref_buf_idx.
2029     av1_compute_gm_for_valid_ref_frames(
2030         cpi, gm_info->ref_buf, ref_buf_idx, gm_info->num_src_corners,
2031         gm_info->src_corners, gm_info->src_buffer,
2032         gm_thread_data->params_by_motion, gm_thread_data->segment_map,
2033         gm_info->segment_map_w, gm_info->segment_map_h);
2034 
2035 #if CONFIG_MULTITHREAD
2036     pthread_mutex_lock(gm_mt_mutex_);
2037 #endif
2038     assert(ref_frame_idx != -1);
2039     // If global motion w.r.t. current ref frame is
2040     // INVALID/TRANSLATION/IDENTITY, skip the evaluation of global motion w.r.t
2041     // the remaining ref frames in that direction. The below exit is disabled
2042     // when ref frame distance w.r.t. current frame is zero. E.g.:
2043     // source_alt_ref_frame w.r.t. ARF frames.
2044     if (cpi->sf.gm_sf.prune_ref_frame_for_gm_search &&
2045         gm_info->reference_frames[cur_dir][ref_frame_idx].distance != 0 &&
2046         cpi->common.global_motion[ref_buf_idx].wmtype != ROTZOOM)
2047       job_info->early_exit[cur_dir] = 1;
2048 
2049 #if CONFIG_MULTITHREAD
2050     pthread_mutex_unlock(gm_mt_mutex_);
2051 #endif
2052   }
2053   return 1;
2054 }
2055 
2056 // Assigns global motion hook function and thread data to each worker.
prepare_gm_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)2057 static AOM_INLINE void prepare_gm_workers(AV1_COMP *cpi, AVxWorkerHook hook,
2058                                           int num_workers) {
2059   MultiThreadInfo *mt_info = &cpi->mt_info;
2060   for (int i = num_workers - 1; i >= 0; i--) {
2061     AVxWorker *worker = &mt_info->workers[i];
2062     EncWorkerData *thread_data = &mt_info->tile_thr_data[i];
2063 
2064     worker->hook = hook;
2065     worker->data1 = thread_data;
2066     worker->data2 = NULL;
2067 
2068     thread_data->thread_id = i;
2069     // Set the starting tile for each thread.
2070     thread_data->start = i;
2071 
2072     thread_data->cpi = cpi;
2073     if (i == 0) {
2074       thread_data->td = &cpi->td;
2075 #if !CONFIG_FRAME_PARALLEL_ENCODE
2076     }
2077 #else
2078     } else {
2079       thread_data->td = thread_data->original_td;
2080     }
2081 #endif  // CONFIG_FRAME_PARALLEL_ENCODE
2082   }
2083 }
2084 
2085 // Assigns available threads to past/future direction.
assign_thread_to_dir(int8_t * thread_id_to_dir,int num_workers)2086 static AOM_INLINE void assign_thread_to_dir(int8_t *thread_id_to_dir,
2087                                             int num_workers) {
2088   int8_t frame_dir_idx = 0;
2089 
2090   for (int i = 0; i < num_workers; i++) {
2091     thread_id_to_dir[i] = frame_dir_idx++;
2092     if (frame_dir_idx == MAX_DIRECTIONS) frame_dir_idx = 0;
2093   }
2094 }
2095 
2096 // Computes number of workers for global motion multi-threading.
compute_gm_workers(const AV1_COMP * cpi)2097 static AOM_INLINE int compute_gm_workers(const AV1_COMP *cpi) {
2098   int total_refs =
2099       cpi->gm_info.num_ref_frames[0] + cpi->gm_info.num_ref_frames[1];
2100   int num_gm_workers = cpi->sf.gm_sf.prune_ref_frame_for_gm_search
2101                            ? AOMMIN(MAX_DIRECTIONS, total_refs)
2102                            : total_refs;
2103   num_gm_workers = AOMMIN(num_gm_workers, cpi->mt_info.num_workers);
2104   return (num_gm_workers);
2105 }
2106 
2107 // Frees the memory allocated for each worker in global motion multi-threading.
av1_gm_dealloc(AV1GlobalMotionSync * gm_sync_data)2108 void av1_gm_dealloc(AV1GlobalMotionSync *gm_sync_data) {
2109   if (gm_sync_data->thread_data != NULL) {
2110     for (int j = 0; j < gm_sync_data->allocated_workers; j++) {
2111       GlobalMotionThreadData *thread_data = &gm_sync_data->thread_data[j];
2112       aom_free(thread_data->segment_map);
2113 
2114       for (int m = 0; m < RANSAC_NUM_MOTIONS; m++)
2115         aom_free(thread_data->params_by_motion[m].inliers);
2116     }
2117     aom_free(gm_sync_data->thread_data);
2118   }
2119 }
2120 
2121 // Allocates memory for inliers and segment_map for each worker in global motion
2122 // multi-threading.
gm_alloc(AV1_COMP * cpi,int num_workers)2123 static AOM_INLINE void gm_alloc(AV1_COMP *cpi, int num_workers) {
2124   AV1_COMMON *cm = &cpi->common;
2125   AV1GlobalMotionSync *gm_sync = &cpi->mt_info.gm_sync;
2126   GlobalMotionInfo *gm_info = &cpi->gm_info;
2127 
2128   gm_sync->allocated_workers = num_workers;
2129   gm_sync->allocated_width = cpi->source->y_width;
2130   gm_sync->allocated_height = cpi->source->y_height;
2131 
2132   CHECK_MEM_ERROR(cm, gm_sync->thread_data,
2133                   aom_malloc(sizeof(*gm_sync->thread_data) * num_workers));
2134 
2135   for (int i = 0; i < num_workers; i++) {
2136     GlobalMotionThreadData *thread_data = &gm_sync->thread_data[i];
2137     CHECK_MEM_ERROR(
2138         cm, thread_data->segment_map,
2139         aom_malloc(sizeof(*thread_data->segment_map) * gm_info->segment_map_w *
2140                    gm_info->segment_map_h));
2141 
2142     for (int m = 0; m < RANSAC_NUM_MOTIONS; m++) {
2143       CHECK_MEM_ERROR(
2144           cm, thread_data->params_by_motion[m].inliers,
2145           aom_malloc(sizeof(*thread_data->params_by_motion[m].inliers) * 2 *
2146                      MAX_CORNERS));
2147     }
2148   }
2149 }
2150 
2151 // Implements multi-threading for global motion.
av1_global_motion_estimation_mt(AV1_COMP * cpi)2152 void av1_global_motion_estimation_mt(AV1_COMP *cpi) {
2153   AV1GlobalMotionSync *gm_sync = &cpi->mt_info.gm_sync;
2154   JobInfo *job_info = &gm_sync->job_info;
2155 
2156   av1_zero(*job_info);
2157 
2158   int num_workers = compute_gm_workers(cpi);
2159 
2160   if (num_workers > gm_sync->allocated_workers ||
2161       cpi->source->y_width != gm_sync->allocated_width ||
2162       cpi->source->y_height != gm_sync->allocated_height) {
2163     av1_gm_dealloc(gm_sync);
2164     gm_alloc(cpi, num_workers);
2165   }
2166 
2167   assign_thread_to_dir(job_info->thread_id_to_dir, num_workers);
2168   prepare_gm_workers(cpi, gm_mt_worker_hook, num_workers);
2169   launch_workers(&cpi->mt_info, num_workers);
2170   sync_enc_workers(&cpi->mt_info, &cpi->common, num_workers);
2171 }
2172 #endif  // !CONFIG_REALTIME_ONLY
2173 
2174 // Compare and order tiles based on absolute sum of tx coeffs.
compare_tile_order(const void * a,const void * b)2175 static int compare_tile_order(const void *a, const void *b) {
2176   const PackBSTileOrder *const tile_a = (const PackBSTileOrder *)a;
2177   const PackBSTileOrder *const tile_b = (const PackBSTileOrder *)b;
2178 
2179   if (tile_a->abs_sum_level > tile_b->abs_sum_level)
2180     return -1;
2181   else if (tile_a->abs_sum_level == tile_b->abs_sum_level)
2182     return (tile_a->tile_idx > tile_b->tile_idx ? 1 : -1);
2183   else
2184     return 1;
2185 }
2186 
2187 // Get next tile index to be processed for pack bitstream
get_next_pack_bs_tile_idx(AV1EncPackBSSync * const pack_bs_sync,const int num_tiles)2188 static AOM_INLINE int get_next_pack_bs_tile_idx(
2189     AV1EncPackBSSync *const pack_bs_sync, const int num_tiles) {
2190   assert(pack_bs_sync->next_job_idx <= num_tiles);
2191   if (pack_bs_sync->next_job_idx == num_tiles) return -1;
2192 
2193   return pack_bs_sync->pack_bs_tile_order[pack_bs_sync->next_job_idx++]
2194       .tile_idx;
2195 }
2196 
2197 // Calculates bitstream chunk size based on total buffer size and tile or tile
2198 // group size.
get_bs_chunk_size(int tg_or_tile_size,const int frame_or_tg_size,size_t * remain_buf_size,size_t max_buf_size,int is_last_chunk)2199 static AOM_INLINE size_t get_bs_chunk_size(int tg_or_tile_size,
2200                                            const int frame_or_tg_size,
2201                                            size_t *remain_buf_size,
2202                                            size_t max_buf_size,
2203                                            int is_last_chunk) {
2204   size_t this_chunk_size;
2205   assert(*remain_buf_size > 0);
2206   if (is_last_chunk) {
2207     this_chunk_size = *remain_buf_size;
2208     *remain_buf_size = 0;
2209   } else {
2210     const uint64_t size_scale = (uint64_t)max_buf_size * tg_or_tile_size;
2211     this_chunk_size = (size_t)(size_scale / frame_or_tg_size);
2212     *remain_buf_size -= this_chunk_size;
2213     assert(*remain_buf_size > 0);
2214   }
2215   assert(this_chunk_size > 0);
2216   return this_chunk_size;
2217 }
2218 
2219 // Initializes params required for pack bitstream tile.
init_tile_pack_bs_params(AV1_COMP * const cpi,uint8_t * const dst,struct aom_write_bit_buffer * saved_wb,PackBSParams * const pack_bs_params_arr,uint8_t obu_extn_header)2220 static void init_tile_pack_bs_params(AV1_COMP *const cpi, uint8_t *const dst,
2221                                      struct aom_write_bit_buffer *saved_wb,
2222                                      PackBSParams *const pack_bs_params_arr,
2223                                      uint8_t obu_extn_header) {
2224   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
2225   AV1_COMMON *const cm = &cpi->common;
2226   const CommonTileParams *const tiles = &cm->tiles;
2227   const int num_tiles = tiles->cols * tiles->rows;
2228   // Fixed size tile groups for the moment
2229   const int num_tg_hdrs = cpi->num_tg;
2230   // Tile group size in terms of number of tiles.
2231   const int tg_size_in_tiles = (num_tiles + num_tg_hdrs - 1) / num_tg_hdrs;
2232   uint8_t *tile_dst = dst;
2233   uint8_t *tile_data_curr = dst;
2234   // Max tile group count can not be more than MAX_TILES.
2235   int tg_size_mi[MAX_TILES] = { 0 };  // Size of tile group in mi units
2236   int tile_idx;
2237   int tg_idx = 0;
2238   int tile_count_in_tg = 0;
2239   int new_tg = 1;
2240 
2241   // Populate pack bitstream params of all tiles.
2242   for (tile_idx = 0; tile_idx < num_tiles; tile_idx++) {
2243     const TileInfo *const tile_info = &cpi->tile_data[tile_idx].tile_info;
2244     PackBSParams *const pack_bs_params = &pack_bs_params_arr[tile_idx];
2245     // Calculate tile size in mi units.
2246     const int tile_size_mi = (tile_info->mi_col_end - tile_info->mi_col_start) *
2247                              (tile_info->mi_row_end - tile_info->mi_row_start);
2248     int is_last_tile_in_tg = 0;
2249     tile_count_in_tg++;
2250     if (tile_count_in_tg == tg_size_in_tiles || tile_idx == (num_tiles - 1))
2251       is_last_tile_in_tg = 1;
2252 
2253     // Populate pack bitstream params of this tile.
2254     pack_bs_params->curr_tg_hdr_size = 0;
2255     pack_bs_params->obu_extn_header = obu_extn_header;
2256     pack_bs_params->saved_wb = saved_wb;
2257     pack_bs_params->obu_header_size = 0;
2258     pack_bs_params->is_last_tile_in_tg = is_last_tile_in_tg;
2259     pack_bs_params->new_tg = new_tg;
2260     pack_bs_params->tile_col = tile_info->tile_col;
2261     pack_bs_params->tile_row = tile_info->tile_row;
2262     pack_bs_params->tile_size_mi = tile_size_mi;
2263     tg_size_mi[tg_idx] += tile_size_mi;
2264 
2265     if (new_tg) new_tg = 0;
2266     if (is_last_tile_in_tg) {
2267       tile_count_in_tg = 0;
2268       new_tg = 1;
2269       tg_idx++;
2270     }
2271   }
2272 
2273   assert(cpi->available_bs_size > 0);
2274   size_t tg_buf_size[MAX_TILES] = { 0 };
2275   size_t max_buf_size = cpi->available_bs_size;
2276   size_t remain_buf_size = max_buf_size;
2277   const int frame_size_mi = cm->mi_params.mi_rows * cm->mi_params.mi_cols;
2278 
2279   tile_idx = 0;
2280   // Prepare obu, tile group and frame header of each tile group.
2281   for (tg_idx = 0; tg_idx < cpi->num_tg; tg_idx++) {
2282     PackBSParams *const pack_bs_params = &pack_bs_params_arr[tile_idx];
2283     int is_last_tg = tg_idx == cpi->num_tg - 1;
2284     // Prorate bitstream buffer size based on tile group size and available
2285     // buffer size. This buffer will be used to store headers and tile data.
2286     tg_buf_size[tg_idx] =
2287         get_bs_chunk_size(tg_size_mi[tg_idx], frame_size_mi, &remain_buf_size,
2288                           max_buf_size, is_last_tg);
2289 
2290     pack_bs_params->dst = tile_dst;
2291     pack_bs_params->tile_data_curr = tile_dst;
2292 
2293     // Write obu, tile group and frame header at first tile in the tile
2294     // group.
2295     av1_write_obu_tg_tile_headers(cpi, xd, pack_bs_params, tile_idx);
2296     tile_dst += tg_buf_size[tg_idx];
2297 
2298     // Exclude headers from tile group buffer size.
2299     tg_buf_size[tg_idx] -= pack_bs_params->curr_tg_hdr_size;
2300     tile_idx += tg_size_in_tiles;
2301   }
2302 
2303   tg_idx = 0;
2304   // Calculate bitstream buffer size of each tile in the tile group.
2305   for (tile_idx = 0; tile_idx < num_tiles; tile_idx++) {
2306     PackBSParams *const pack_bs_params = &pack_bs_params_arr[tile_idx];
2307 
2308     if (pack_bs_params->new_tg) {
2309       max_buf_size = tg_buf_size[tg_idx];
2310       remain_buf_size = max_buf_size;
2311     }
2312 
2313     // Prorate bitstream buffer size of this tile based on tile size and
2314     // available buffer size. For this proration, header size is not accounted.
2315     const size_t tile_buf_size = get_bs_chunk_size(
2316         pack_bs_params->tile_size_mi, tg_size_mi[tg_idx], &remain_buf_size,
2317         max_buf_size, pack_bs_params->is_last_tile_in_tg);
2318     pack_bs_params->tile_buf_size = tile_buf_size;
2319 
2320     // Update base address of bitstream buffer for tile and tile group.
2321     if (pack_bs_params->new_tg) {
2322       tile_dst = pack_bs_params->dst;
2323       tile_data_curr = pack_bs_params->tile_data_curr;
2324       // Account header size in first tile of a tile group.
2325       pack_bs_params->tile_buf_size += pack_bs_params->curr_tg_hdr_size;
2326     } else {
2327       pack_bs_params->dst = tile_dst;
2328       pack_bs_params->tile_data_curr = tile_data_curr;
2329     }
2330 
2331     if (pack_bs_params->is_last_tile_in_tg) tg_idx++;
2332     tile_dst += pack_bs_params->tile_buf_size;
2333   }
2334 }
2335 
2336 // Worker hook function of pack bitsteam multithreading.
pack_bs_worker_hook(void * arg1,void * arg2)2337 static int pack_bs_worker_hook(void *arg1, void *arg2) {
2338   EncWorkerData *const thread_data = (EncWorkerData *)arg1;
2339   PackBSParams *const pack_bs_params = (PackBSParams *)arg2;
2340   AV1_COMP *const cpi = thread_data->cpi;
2341   AV1_COMMON *const cm = &cpi->common;
2342   AV1EncPackBSSync *const pack_bs_sync = &cpi->mt_info.pack_bs_sync;
2343   const CommonTileParams *const tiles = &cm->tiles;
2344   const int num_tiles = tiles->cols * tiles->rows;
2345 
2346   while (1) {
2347 #if CONFIG_MULTITHREAD
2348     pthread_mutex_lock(pack_bs_sync->mutex_);
2349 #endif
2350     const int tile_idx = get_next_pack_bs_tile_idx(pack_bs_sync, num_tiles);
2351 #if CONFIG_MULTITHREAD
2352     pthread_mutex_unlock(pack_bs_sync->mutex_);
2353 #endif
2354     if (tile_idx == -1) break;
2355     TileDataEnc *this_tile = &cpi->tile_data[tile_idx];
2356     thread_data->td->mb.e_mbd.tile_ctx = &this_tile->tctx;
2357 
2358     av1_pack_tile_info(cpi, thread_data->td, &pack_bs_params[tile_idx]);
2359   }
2360 
2361   return 1;
2362 }
2363 
2364 // Prepares thread data and workers of pack bitsteam multithreading.
prepare_pack_bs_workers(AV1_COMP * const cpi,PackBSParams * const pack_bs_params,AVxWorkerHook hook,const int num_workers)2365 static void prepare_pack_bs_workers(AV1_COMP *const cpi,
2366                                     PackBSParams *const pack_bs_params,
2367                                     AVxWorkerHook hook, const int num_workers) {
2368   MultiThreadInfo *const mt_info = &cpi->mt_info;
2369   for (int i = num_workers - 1; i >= 0; i--) {
2370     AVxWorker *worker = &mt_info->workers[i];
2371     EncWorkerData *const thread_data = &mt_info->tile_thr_data[i];
2372     if (i == 0) {
2373       thread_data->td = &cpi->td;
2374 #if !CONFIG_FRAME_PARALLEL_ENCODE
2375     }
2376 #else
2377     } else {
2378       thread_data->td = thread_data->original_td;
2379     }
2380 #endif  // CONFIG_FRAME_PARALLEL_ENCODE
2381 
2382     if (thread_data->td != &cpi->td) thread_data->td->mb = cpi->td.mb;
2383 
2384     thread_data->cpi = cpi;
2385     thread_data->start = i;
2386     thread_data->thread_id = i;
2387     av1_reset_pack_bs_thread_data(thread_data->td);
2388 
2389     worker->hook = hook;
2390     worker->data1 = thread_data;
2391     worker->data2 = pack_bs_params;
2392   }
2393 
2394   AV1_COMMON *const cm = &cpi->common;
2395   AV1EncPackBSSync *const pack_bs_sync = &mt_info->pack_bs_sync;
2396   const uint16_t num_tiles = cm->tiles.rows * cm->tiles.cols;
2397   pack_bs_sync->next_job_idx = 0;
2398 
2399   PackBSTileOrder *const pack_bs_tile_order = pack_bs_sync->pack_bs_tile_order;
2400   // Reset tile order data of pack bitstream
2401   av1_zero_array(pack_bs_tile_order, num_tiles);
2402 
2403   // Populate pack bitstream tile order structure
2404   for (uint16_t tile_idx = 0; tile_idx < num_tiles; tile_idx++) {
2405     pack_bs_tile_order[tile_idx].abs_sum_level =
2406         cpi->tile_data[tile_idx].abs_sum_level;
2407     pack_bs_tile_order[tile_idx].tile_idx = tile_idx;
2408   }
2409 
2410   // Sort tiles in descending order based on tile area.
2411   qsort(pack_bs_tile_order, num_tiles, sizeof(*pack_bs_tile_order),
2412         compare_tile_order);
2413 }
2414 
2415 // Accumulates data after pack bitsteam processing.
accumulate_pack_bs_data(AV1_COMP * const cpi,const PackBSParams * const pack_bs_params_arr,uint8_t * const dst,uint32_t * total_size,const FrameHeaderInfo * fh_info,int * const largest_tile_id,unsigned int * max_tile_size,uint32_t * const obu_header_size,uint8_t ** tile_data_start,const int num_workers)2416 static void accumulate_pack_bs_data(
2417     AV1_COMP *const cpi, const PackBSParams *const pack_bs_params_arr,
2418     uint8_t *const dst, uint32_t *total_size, const FrameHeaderInfo *fh_info,
2419     int *const largest_tile_id, unsigned int *max_tile_size,
2420     uint32_t *const obu_header_size, uint8_t **tile_data_start,
2421     const int num_workers) {
2422   const AV1_COMMON *const cm = &cpi->common;
2423   const CommonTileParams *const tiles = &cm->tiles;
2424   const int tile_count = tiles->cols * tiles->rows;
2425   // Fixed size tile groups for the moment
2426   size_t curr_tg_data_size = 0;
2427   int is_first_tg = 1;
2428   uint8_t *curr_tg_start = dst;
2429   size_t src_offset = 0;
2430   size_t dst_offset = 0;
2431 
2432   for (int tile_idx = 0; tile_idx < tile_count; tile_idx++) {
2433     // PackBSParams stores all parameters required to pack tile and header
2434     // info.
2435     const PackBSParams *const pack_bs_params = &pack_bs_params_arr[tile_idx];
2436     uint32_t tile_size = 0;
2437 
2438     if (pack_bs_params->new_tg) {
2439       curr_tg_start = dst + *total_size;
2440       curr_tg_data_size = pack_bs_params->curr_tg_hdr_size;
2441       *tile_data_start += pack_bs_params->curr_tg_hdr_size;
2442       *obu_header_size = pack_bs_params->obu_header_size;
2443     }
2444     curr_tg_data_size +=
2445         pack_bs_params->buf.size + (pack_bs_params->is_last_tile_in_tg ? 0 : 4);
2446 
2447     if (pack_bs_params->buf.size > *max_tile_size) {
2448       *largest_tile_id = tile_idx;
2449       *max_tile_size = (unsigned int)pack_bs_params->buf.size;
2450     }
2451     tile_size +=
2452         (uint32_t)pack_bs_params->buf.size + *pack_bs_params->total_size;
2453 
2454     // Pack all the chunks of tile bitstreams together
2455     if (tile_idx != 0) memmove(dst + dst_offset, dst + src_offset, tile_size);
2456 
2457     if (pack_bs_params->is_last_tile_in_tg)
2458       av1_write_last_tile_info(
2459           cpi, fh_info, pack_bs_params->saved_wb, &curr_tg_data_size,
2460           curr_tg_start, &tile_size, tile_data_start, largest_tile_id,
2461           &is_first_tg, *obu_header_size, pack_bs_params->obu_extn_header);
2462     src_offset += pack_bs_params->tile_buf_size;
2463     dst_offset += tile_size;
2464     *total_size += tile_size;
2465   }
2466 
2467   // Accumulate thread data
2468   MultiThreadInfo *const mt_info = &cpi->mt_info;
2469   for (int idx = num_workers - 1; idx >= 0; idx--) {
2470     ThreadData const *td = mt_info->tile_thr_data[idx].td;
2471     av1_accumulate_pack_bs_thread_data(cpi, td);
2472   }
2473 }
2474 
av1_write_tile_obu_mt(AV1_COMP * const cpi,uint8_t * const dst,uint32_t * total_size,struct aom_write_bit_buffer * saved_wb,uint8_t obu_extn_header,const FrameHeaderInfo * fh_info,int * const largest_tile_id,unsigned int * max_tile_size,uint32_t * const obu_header_size,uint8_t ** tile_data_start,const int num_workers)2475 void av1_write_tile_obu_mt(
2476     AV1_COMP *const cpi, uint8_t *const dst, uint32_t *total_size,
2477     struct aom_write_bit_buffer *saved_wb, uint8_t obu_extn_header,
2478     const FrameHeaderInfo *fh_info, int *const largest_tile_id,
2479     unsigned int *max_tile_size, uint32_t *const obu_header_size,
2480     uint8_t **tile_data_start, const int num_workers) {
2481   MultiThreadInfo *const mt_info = &cpi->mt_info;
2482 
2483   PackBSParams pack_bs_params[MAX_TILES];
2484   uint32_t tile_size[MAX_TILES] = { 0 };
2485 
2486   for (int tile_idx = 0; tile_idx < MAX_TILES; tile_idx++)
2487     pack_bs_params[tile_idx].total_size = &tile_size[tile_idx];
2488 
2489   init_tile_pack_bs_params(cpi, dst, saved_wb, pack_bs_params, obu_extn_header);
2490   prepare_pack_bs_workers(cpi, pack_bs_params, pack_bs_worker_hook,
2491                           num_workers);
2492   launch_workers(mt_info, num_workers);
2493   sync_enc_workers(mt_info, &cpi->common, num_workers);
2494   accumulate_pack_bs_data(cpi, pack_bs_params, dst, total_size, fh_info,
2495                           largest_tile_id, max_tile_size, obu_header_size,
2496                           tile_data_start, num_workers);
2497 }
2498 
2499 // Deallocate memory for CDEF search multi-thread synchronization.
av1_cdef_mt_dealloc(AV1CdefSync * cdef_sync)2500 void av1_cdef_mt_dealloc(AV1CdefSync *cdef_sync) {
2501   (void)cdef_sync;
2502   assert(cdef_sync != NULL);
2503 #if CONFIG_MULTITHREAD
2504   if (cdef_sync->mutex_ != NULL) {
2505     pthread_mutex_destroy(cdef_sync->mutex_);
2506     aom_free(cdef_sync->mutex_);
2507   }
2508 #endif  // CONFIG_MULTITHREAD
2509 }
2510 
2511 // Updates the row and column indices of the next job to be processed.
2512 // Also updates end_of_frame flag when the processing of all blocks is complete.
update_next_job_info(AV1CdefSync * cdef_sync,int nvfb,int nhfb)2513 static void update_next_job_info(AV1CdefSync *cdef_sync, int nvfb, int nhfb) {
2514   cdef_sync->fbc++;
2515   if (cdef_sync->fbc == nhfb) {
2516     cdef_sync->fbr++;
2517     if (cdef_sync->fbr == nvfb) {
2518       cdef_sync->end_of_frame = 1;
2519     } else {
2520       cdef_sync->fbc = 0;
2521     }
2522   }
2523 }
2524 
2525 // Initializes cdef_sync parameters.
cdef_reset_job_info(AV1CdefSync * cdef_sync)2526 static AOM_INLINE void cdef_reset_job_info(AV1CdefSync *cdef_sync) {
2527 #if CONFIG_MULTITHREAD
2528   if (cdef_sync->mutex_) pthread_mutex_init(cdef_sync->mutex_, NULL);
2529 #endif  // CONFIG_MULTITHREAD
2530   cdef_sync->end_of_frame = 0;
2531   cdef_sync->fbr = 0;
2532   cdef_sync->fbc = 0;
2533 }
2534 
2535 // Checks if a job is available. If job is available,
2536 // populates next job information and returns 1, else returns 0.
cdef_get_next_job(AV1CdefSync * cdef_sync,CdefSearchCtx * cdef_search_ctx,int * cur_fbr,int * cur_fbc,int * sb_count)2537 static AOM_INLINE int cdef_get_next_job(AV1CdefSync *cdef_sync,
2538                                         CdefSearchCtx *cdef_search_ctx,
2539                                         int *cur_fbr, int *cur_fbc,
2540                                         int *sb_count) {
2541 #if CONFIG_MULTITHREAD
2542   pthread_mutex_lock(cdef_sync->mutex_);
2543 #endif  // CONFIG_MULTITHREAD
2544   int do_next_block = 0;
2545   const int nvfb = cdef_search_ctx->nvfb;
2546   const int nhfb = cdef_search_ctx->nhfb;
2547 
2548   // If a block is skip, do not process the block and
2549   // check the skip condition for the next block.
2550   while ((!cdef_sync->end_of_frame) &&
2551          (cdef_sb_skip(cdef_search_ctx->mi_params, cdef_sync->fbr,
2552                        cdef_sync->fbc))) {
2553     update_next_job_info(cdef_sync, nvfb, nhfb);
2554   }
2555 
2556   // Populates information needed for current job and update the row,
2557   // column indices of the next block to be processed.
2558   if (cdef_sync->end_of_frame == 0) {
2559     do_next_block = 1;
2560     *cur_fbr = cdef_sync->fbr;
2561     *cur_fbc = cdef_sync->fbc;
2562     *sb_count = cdef_search_ctx->sb_count;
2563     cdef_search_ctx->sb_count++;
2564     update_next_job_info(cdef_sync, nvfb, nhfb);
2565   }
2566 #if CONFIG_MULTITHREAD
2567   pthread_mutex_unlock(cdef_sync->mutex_);
2568 #endif  // CONFIG_MULTITHREAD
2569   return do_next_block;
2570 }
2571 
2572 // Hook function for each thread in CDEF search multi-threading.
cdef_filter_block_worker_hook(void * arg1,void * arg2)2573 static int cdef_filter_block_worker_hook(void *arg1, void *arg2) {
2574   AV1CdefSync *const cdef_sync = (AV1CdefSync *)arg1;
2575   CdefSearchCtx *cdef_search_ctx = (CdefSearchCtx *)arg2;
2576   int cur_fbr, cur_fbc, sb_count;
2577   while (cdef_get_next_job(cdef_sync, cdef_search_ctx, &cur_fbr, &cur_fbc,
2578                            &sb_count)) {
2579     av1_cdef_mse_calc_block(cdef_search_ctx, cur_fbr, cur_fbc, sb_count);
2580   }
2581   return 1;
2582 }
2583 
2584 // Assigns CDEF search hook function and thread data to each worker.
prepare_cdef_workers(MultiThreadInfo * mt_info,CdefSearchCtx * cdef_search_ctx,AVxWorkerHook hook,int num_workers)2585 static void prepare_cdef_workers(MultiThreadInfo *mt_info,
2586                                  CdefSearchCtx *cdef_search_ctx,
2587                                  AVxWorkerHook hook, int num_workers) {
2588   for (int i = num_workers - 1; i >= 0; i--) {
2589     AVxWorker *worker = &mt_info->workers[i];
2590     worker->hook = hook;
2591     worker->data1 = &mt_info->cdef_sync;
2592     worker->data2 = cdef_search_ctx;
2593   }
2594 }
2595 
2596 // Implements multi-threading for CDEF search.
av1_cdef_mse_calc_frame_mt(AV1_COMMON * cm,MultiThreadInfo * mt_info,CdefSearchCtx * cdef_search_ctx)2597 void av1_cdef_mse_calc_frame_mt(AV1_COMMON *cm, MultiThreadInfo *mt_info,
2598                                 CdefSearchCtx *cdef_search_ctx) {
2599   AV1CdefSync *cdef_sync = &mt_info->cdef_sync;
2600   const int num_workers = mt_info->num_mod_workers[MOD_CDEF_SEARCH];
2601 
2602   cdef_reset_job_info(cdef_sync);
2603   prepare_cdef_workers(mt_info, cdef_search_ctx, cdef_filter_block_worker_hook,
2604                        num_workers);
2605   launch_workers(mt_info, num_workers);
2606   sync_enc_workers(mt_info, cm, num_workers);
2607 }
2608 
2609 // Computes num_workers for temporal filter multi-threading.
compute_num_tf_workers(AV1_COMP * cpi)2610 static AOM_INLINE int compute_num_tf_workers(AV1_COMP *cpi) {
2611   // For single-pass encode, using no. of workers as per tf block size was not
2612   // found to improve speed. Hence the thread assignment for single-pass encode
2613   // is kept based on compute_num_enc_workers().
2614   if (cpi->oxcf.pass < AOM_RC_SECOND_PASS)
2615     return (av1_compute_num_enc_workers(cpi, cpi->oxcf.max_threads));
2616 
2617   if (cpi->oxcf.max_threads <= 1) return 1;
2618 
2619   const int frame_height = cpi->common.height;
2620   const BLOCK_SIZE block_size = TF_BLOCK_SIZE;
2621   const int mb_height = block_size_high[block_size];
2622   const int mb_rows = get_num_blocks(frame_height, mb_height);
2623   return AOMMIN(cpi->oxcf.max_threads, mb_rows);
2624 }
2625 
2626 // Computes num_workers for tpl multi-threading.
compute_num_tpl_workers(AV1_COMP * cpi)2627 static AOM_INLINE int compute_num_tpl_workers(AV1_COMP *cpi) {
2628   return av1_compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
2629 }
2630 
2631 // Computes num_workers for loop filter multi-threading.
compute_num_lf_workers(AV1_COMP * cpi)2632 static AOM_INLINE int compute_num_lf_workers(AV1_COMP *cpi) {
2633   return av1_compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
2634 }
2635 
2636 // Computes num_workers for cdef multi-threading.
compute_num_cdef_workers(AV1_COMP * cpi)2637 static AOM_INLINE int compute_num_cdef_workers(AV1_COMP *cpi) {
2638   return av1_compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
2639 }
2640 
2641 // Computes num_workers for loop-restoration multi-threading.
compute_num_lr_workers(AV1_COMP * cpi)2642 static AOM_INLINE int compute_num_lr_workers(AV1_COMP *cpi) {
2643   return av1_compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
2644 }
2645 
2646 // Computes num_workers for pack bitstream multi-threading.
compute_num_pack_bs_workers(AV1_COMP * cpi)2647 static AOM_INLINE int compute_num_pack_bs_workers(AV1_COMP *cpi) {
2648   if (cpi->oxcf.max_threads <= 1) return 1;
2649   return compute_num_enc_tile_mt_workers(&cpi->common, cpi->oxcf.max_threads);
2650 }
2651 
compute_num_mod_workers(AV1_COMP * cpi,MULTI_THREADED_MODULES mod_name)2652 int compute_num_mod_workers(AV1_COMP *cpi, MULTI_THREADED_MODULES mod_name) {
2653   int num_mod_workers = 0;
2654   switch (mod_name) {
2655     case MOD_FP:
2656       if (cpi->oxcf.pass >= AOM_RC_SECOND_PASS)
2657         num_mod_workers = 0;
2658       else
2659         num_mod_workers =
2660             av1_compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
2661       break;
2662     case MOD_TF: num_mod_workers = compute_num_tf_workers(cpi); break;
2663     case MOD_TPL: num_mod_workers = compute_num_tpl_workers(cpi); break;
2664     case MOD_GME: num_mod_workers = 1; break;
2665     case MOD_ENC:
2666       num_mod_workers = av1_compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
2667       break;
2668     case MOD_LPF: num_mod_workers = compute_num_lf_workers(cpi); break;
2669     case MOD_CDEF_SEARCH:
2670       num_mod_workers = compute_num_cdef_workers(cpi);
2671       break;
2672     case MOD_CDEF: num_mod_workers = compute_num_cdef_workers(cpi); break;
2673     case MOD_LR: num_mod_workers = compute_num_lr_workers(cpi); break;
2674     case MOD_PACK_BS: num_mod_workers = compute_num_pack_bs_workers(cpi); break;
2675     case MOD_FRAME_ENC:
2676       num_mod_workers = cpi->ppi->p_mt_info.num_mod_workers[MOD_FRAME_ENC];
2677       break;
2678     default: assert(0); break;
2679   }
2680   return (num_mod_workers);
2681 }
2682 // Computes the number of workers for each MT modules in the encoder
av1_compute_num_workers_for_mt(AV1_COMP * cpi)2683 void av1_compute_num_workers_for_mt(AV1_COMP *cpi) {
2684   for (int i = MOD_FP; i < NUM_MT_MODULES; i++)
2685     cpi->ppi->p_mt_info.num_mod_workers[i] =
2686         compute_num_mod_workers(cpi, (MULTI_THREADED_MODULES)i);
2687 }
2688