xref: /aosp_15_r20/external/libaom/av1/encoder/ethread.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <stdbool.h>
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "aom_util/aom_pthread.h"
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/warped_motion.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/thread_common.h"
19*77c1e3ccSAndroid Build Coastguard Worker 
20*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/allintra_vis.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/bitstream.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/enc_enums.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodeframe.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodeframe_utils.h"
25*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder.h"
26*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder_alloc.h"
27*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/ethread.h"
28*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
29*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/firstpass.h"
30*77c1e3ccSAndroid Build Coastguard Worker #endif
31*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/global_motion.h"
32*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/global_motion_facade.h"
33*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/intra_mode_search_utils.h"
34*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/picklpf.h"
35*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/rdopt.h"
36*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_dsp_common.h"
37*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/temporal_filter.h"
38*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/tpl_model.h"
39*77c1e3ccSAndroid Build Coastguard Worker 
accumulate_rd_opt(ThreadData * td,ThreadData * td_t)40*77c1e3ccSAndroid Build Coastguard Worker static inline void accumulate_rd_opt(ThreadData *td, ThreadData *td_t) {
41*77c1e3ccSAndroid Build Coastguard Worker   td->rd_counts.compound_ref_used_flag |=
42*77c1e3ccSAndroid Build Coastguard Worker       td_t->rd_counts.compound_ref_used_flag;
43*77c1e3ccSAndroid Build Coastguard Worker   td->rd_counts.skip_mode_used_flag |= td_t->rd_counts.skip_mode_used_flag;
44*77c1e3ccSAndroid Build Coastguard Worker 
45*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < TX_SIZES_ALL; i++) {
46*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < TX_TYPES; j++)
47*77c1e3ccSAndroid Build Coastguard Worker       td->rd_counts.tx_type_used[i][j] += td_t->rd_counts.tx_type_used[i][j];
48*77c1e3ccSAndroid Build Coastguard Worker   }
49*77c1e3ccSAndroid Build Coastguard Worker 
50*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < BLOCK_SIZES_ALL; i++) {
51*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < 2; j++) {
52*77c1e3ccSAndroid Build Coastguard Worker       td->rd_counts.obmc_used[i][j] += td_t->rd_counts.obmc_used[i][j];
53*77c1e3ccSAndroid Build Coastguard Worker     }
54*77c1e3ccSAndroid Build Coastguard Worker   }
55*77c1e3ccSAndroid Build Coastguard Worker 
56*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < 2; i++) {
57*77c1e3ccSAndroid Build Coastguard Worker     td->rd_counts.warped_used[i] += td_t->rd_counts.warped_used[i];
58*77c1e3ccSAndroid Build Coastguard Worker   }
59*77c1e3ccSAndroid Build Coastguard Worker 
60*77c1e3ccSAndroid Build Coastguard Worker   td->rd_counts.seg_tmp_pred_cost[0] += td_t->rd_counts.seg_tmp_pred_cost[0];
61*77c1e3ccSAndroid Build Coastguard Worker   td->rd_counts.seg_tmp_pred_cost[1] += td_t->rd_counts.seg_tmp_pred_cost[1];
62*77c1e3ccSAndroid Build Coastguard Worker 
63*77c1e3ccSAndroid Build Coastguard Worker   td->rd_counts.newmv_or_intra_blocks += td_t->rd_counts.newmv_or_intra_blocks;
64*77c1e3ccSAndroid Build Coastguard Worker }
65*77c1e3ccSAndroid Build Coastguard Worker 
update_delta_lf_for_row_mt(AV1_COMP * cpi)66*77c1e3ccSAndroid Build Coastguard Worker static inline void update_delta_lf_for_row_mt(AV1_COMP *cpi) {
67*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *cm = &cpi->common;
68*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
69*77c1e3ccSAndroid Build Coastguard Worker   const int mib_size = cm->seq_params->mib_size;
70*77c1e3ccSAndroid Build Coastguard Worker   const int frame_lf_count =
71*77c1e3ccSAndroid Build Coastguard Worker       av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
72*77c1e3ccSAndroid Build Coastguard Worker   for (int row = 0; row < cm->tiles.rows; row++) {
73*77c1e3ccSAndroid Build Coastguard Worker     for (int col = 0; col < cm->tiles.cols; col++) {
74*77c1e3ccSAndroid Build Coastguard Worker       TileDataEnc *tile_data = &cpi->tile_data[row * cm->tiles.cols + col];
75*77c1e3ccSAndroid Build Coastguard Worker       const TileInfo *const tile_info = &tile_data->tile_info;
76*77c1e3ccSAndroid Build Coastguard Worker       for (int mi_row = tile_info->mi_row_start; mi_row < tile_info->mi_row_end;
77*77c1e3ccSAndroid Build Coastguard Worker            mi_row += mib_size) {
78*77c1e3ccSAndroid Build Coastguard Worker         if (mi_row == tile_info->mi_row_start)
79*77c1e3ccSAndroid Build Coastguard Worker           av1_reset_loop_filter_delta(xd, av1_num_planes(cm));
80*77c1e3ccSAndroid Build Coastguard Worker         for (int mi_col = tile_info->mi_col_start;
81*77c1e3ccSAndroid Build Coastguard Worker              mi_col < tile_info->mi_col_end; mi_col += mib_size) {
82*77c1e3ccSAndroid Build Coastguard Worker           const int idx_str = cm->mi_params.mi_stride * mi_row + mi_col;
83*77c1e3ccSAndroid Build Coastguard Worker           MB_MODE_INFO **mi = cm->mi_params.mi_grid_base + idx_str;
84*77c1e3ccSAndroid Build Coastguard Worker           MB_MODE_INFO *mbmi = mi[0];
85*77c1e3ccSAndroid Build Coastguard Worker           if (mbmi->skip_txfm == 1 &&
86*77c1e3ccSAndroid Build Coastguard Worker               (mbmi->bsize == cm->seq_params->sb_size)) {
87*77c1e3ccSAndroid Build Coastguard Worker             for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id)
88*77c1e3ccSAndroid Build Coastguard Worker               mbmi->delta_lf[lf_id] = xd->delta_lf[lf_id];
89*77c1e3ccSAndroid Build Coastguard Worker             mbmi->delta_lf_from_base = xd->delta_lf_from_base;
90*77c1e3ccSAndroid Build Coastguard Worker           } else {
91*77c1e3ccSAndroid Build Coastguard Worker             if (cm->delta_q_info.delta_lf_multi) {
92*77c1e3ccSAndroid Build Coastguard Worker               for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id)
93*77c1e3ccSAndroid Build Coastguard Worker                 xd->delta_lf[lf_id] = mbmi->delta_lf[lf_id];
94*77c1e3ccSAndroid Build Coastguard Worker             } else {
95*77c1e3ccSAndroid Build Coastguard Worker               xd->delta_lf_from_base = mbmi->delta_lf_from_base;
96*77c1e3ccSAndroid Build Coastguard Worker             }
97*77c1e3ccSAndroid Build Coastguard Worker           }
98*77c1e3ccSAndroid Build Coastguard Worker         }
99*77c1e3ccSAndroid Build Coastguard Worker       }
100*77c1e3ccSAndroid Build Coastguard Worker     }
101*77c1e3ccSAndroid Build Coastguard Worker   }
102*77c1e3ccSAndroid Build Coastguard Worker }
103*77c1e3ccSAndroid Build Coastguard Worker 
av1_row_mt_sync_read_dummy(AV1EncRowMultiThreadSync * row_mt_sync,int r,int c)104*77c1e3ccSAndroid Build Coastguard Worker void av1_row_mt_sync_read_dummy(AV1EncRowMultiThreadSync *row_mt_sync, int r,
105*77c1e3ccSAndroid Build Coastguard Worker                                 int c) {
106*77c1e3ccSAndroid Build Coastguard Worker   (void)row_mt_sync;
107*77c1e3ccSAndroid Build Coastguard Worker   (void)r;
108*77c1e3ccSAndroid Build Coastguard Worker   (void)c;
109*77c1e3ccSAndroid Build Coastguard Worker }
110*77c1e3ccSAndroid Build Coastguard Worker 
av1_row_mt_sync_write_dummy(AV1EncRowMultiThreadSync * row_mt_sync,int r,int c,int cols)111*77c1e3ccSAndroid Build Coastguard Worker void av1_row_mt_sync_write_dummy(AV1EncRowMultiThreadSync *row_mt_sync, int r,
112*77c1e3ccSAndroid Build Coastguard Worker                                  int c, int cols) {
113*77c1e3ccSAndroid Build Coastguard Worker   (void)row_mt_sync;
114*77c1e3ccSAndroid Build Coastguard Worker   (void)r;
115*77c1e3ccSAndroid Build Coastguard Worker   (void)c;
116*77c1e3ccSAndroid Build Coastguard Worker   (void)cols;
117*77c1e3ccSAndroid Build Coastguard Worker }
118*77c1e3ccSAndroid Build Coastguard Worker 
av1_row_mt_sync_read(AV1EncRowMultiThreadSync * row_mt_sync,int r,int c)119*77c1e3ccSAndroid Build Coastguard Worker void av1_row_mt_sync_read(AV1EncRowMultiThreadSync *row_mt_sync, int r, int c) {
120*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
121*77c1e3ccSAndroid Build Coastguard Worker   const int nsync = row_mt_sync->sync_range;
122*77c1e3ccSAndroid Build Coastguard Worker 
123*77c1e3ccSAndroid Build Coastguard Worker   if (r) {
124*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_t *const mutex = &row_mt_sync->mutex_[r - 1];
125*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(mutex);
126*77c1e3ccSAndroid Build Coastguard Worker 
127*77c1e3ccSAndroid Build Coastguard Worker     while (c > row_mt_sync->num_finished_cols[r - 1] - nsync -
128*77c1e3ccSAndroid Build Coastguard Worker                    row_mt_sync->intrabc_extra_top_right_sb_delay) {
129*77c1e3ccSAndroid Build Coastguard Worker       pthread_cond_wait(&row_mt_sync->cond_[r - 1], mutex);
130*77c1e3ccSAndroid Build Coastguard Worker     }
131*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(mutex);
132*77c1e3ccSAndroid Build Coastguard Worker   }
133*77c1e3ccSAndroid Build Coastguard Worker #else
134*77c1e3ccSAndroid Build Coastguard Worker   (void)row_mt_sync;
135*77c1e3ccSAndroid Build Coastguard Worker   (void)r;
136*77c1e3ccSAndroid Build Coastguard Worker   (void)c;
137*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
138*77c1e3ccSAndroid Build Coastguard Worker }
139*77c1e3ccSAndroid Build Coastguard Worker 
av1_row_mt_sync_write(AV1EncRowMultiThreadSync * row_mt_sync,int r,int c,int cols)140*77c1e3ccSAndroid Build Coastguard Worker void av1_row_mt_sync_write(AV1EncRowMultiThreadSync *row_mt_sync, int r, int c,
141*77c1e3ccSAndroid Build Coastguard Worker                            int cols) {
142*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
143*77c1e3ccSAndroid Build Coastguard Worker   const int nsync = row_mt_sync->sync_range;
144*77c1e3ccSAndroid Build Coastguard Worker   int cur;
145*77c1e3ccSAndroid Build Coastguard Worker   // Only signal when there are enough encoded blocks for next row to run.
146*77c1e3ccSAndroid Build Coastguard Worker   int sig = 1;
147*77c1e3ccSAndroid Build Coastguard Worker 
148*77c1e3ccSAndroid Build Coastguard Worker   if (c < cols - 1) {
149*77c1e3ccSAndroid Build Coastguard Worker     cur = c;
150*77c1e3ccSAndroid Build Coastguard Worker     if (c % nsync) sig = 0;
151*77c1e3ccSAndroid Build Coastguard Worker   } else {
152*77c1e3ccSAndroid Build Coastguard Worker     cur = cols + nsync + row_mt_sync->intrabc_extra_top_right_sb_delay;
153*77c1e3ccSAndroid Build Coastguard Worker   }
154*77c1e3ccSAndroid Build Coastguard Worker 
155*77c1e3ccSAndroid Build Coastguard Worker   if (sig) {
156*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(&row_mt_sync->mutex_[r]);
157*77c1e3ccSAndroid Build Coastguard Worker 
158*77c1e3ccSAndroid Build Coastguard Worker     // When a thread encounters an error, num_finished_cols[r] is set to maximum
159*77c1e3ccSAndroid Build Coastguard Worker     // column number. In this case, the AOMMAX operation here ensures that
160*77c1e3ccSAndroid Build Coastguard Worker     // num_finished_cols[r] is not overwritten with a smaller value thus
161*77c1e3ccSAndroid Build Coastguard Worker     // preventing the infinite waiting of threads in the relevant sync_read()
162*77c1e3ccSAndroid Build Coastguard Worker     // function.
163*77c1e3ccSAndroid Build Coastguard Worker     row_mt_sync->num_finished_cols[r] =
164*77c1e3ccSAndroid Build Coastguard Worker         AOMMAX(row_mt_sync->num_finished_cols[r], cur);
165*77c1e3ccSAndroid Build Coastguard Worker 
166*77c1e3ccSAndroid Build Coastguard Worker     pthread_cond_signal(&row_mt_sync->cond_[r]);
167*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(&row_mt_sync->mutex_[r]);
168*77c1e3ccSAndroid Build Coastguard Worker   }
169*77c1e3ccSAndroid Build Coastguard Worker #else
170*77c1e3ccSAndroid Build Coastguard Worker   (void)row_mt_sync;
171*77c1e3ccSAndroid Build Coastguard Worker   (void)r;
172*77c1e3ccSAndroid Build Coastguard Worker   (void)c;
173*77c1e3ccSAndroid Build Coastguard Worker   (void)cols;
174*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
175*77c1e3ccSAndroid Build Coastguard Worker }
176*77c1e3ccSAndroid Build Coastguard Worker 
177*77c1e3ccSAndroid Build Coastguard Worker // Allocate memory for row synchronization
row_mt_sync_mem_alloc(AV1EncRowMultiThreadSync * row_mt_sync,AV1_COMMON * cm,int rows)178*77c1e3ccSAndroid Build Coastguard Worker static void row_mt_sync_mem_alloc(AV1EncRowMultiThreadSync *row_mt_sync,
179*77c1e3ccSAndroid Build Coastguard Worker                                   AV1_COMMON *cm, int rows) {
180*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
181*77c1e3ccSAndroid Build Coastguard Worker   int i;
182*77c1e3ccSAndroid Build Coastguard Worker 
183*77c1e3ccSAndroid Build Coastguard Worker   CHECK_MEM_ERROR(cm, row_mt_sync->mutex_,
184*77c1e3ccSAndroid Build Coastguard Worker                   aom_malloc(sizeof(*row_mt_sync->mutex_) * rows));
185*77c1e3ccSAndroid Build Coastguard Worker   if (row_mt_sync->mutex_) {
186*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < rows; ++i) {
187*77c1e3ccSAndroid Build Coastguard Worker       pthread_mutex_init(&row_mt_sync->mutex_[i], NULL);
188*77c1e3ccSAndroid Build Coastguard Worker     }
189*77c1e3ccSAndroid Build Coastguard Worker   }
190*77c1e3ccSAndroid Build Coastguard Worker 
191*77c1e3ccSAndroid Build Coastguard Worker   CHECK_MEM_ERROR(cm, row_mt_sync->cond_,
192*77c1e3ccSAndroid Build Coastguard Worker                   aom_malloc(sizeof(*row_mt_sync->cond_) * rows));
193*77c1e3ccSAndroid Build Coastguard Worker   if (row_mt_sync->cond_) {
194*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < rows; ++i) {
195*77c1e3ccSAndroid Build Coastguard Worker       pthread_cond_init(&row_mt_sync->cond_[i], NULL);
196*77c1e3ccSAndroid Build Coastguard Worker     }
197*77c1e3ccSAndroid Build Coastguard Worker   }
198*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
199*77c1e3ccSAndroid Build Coastguard Worker 
200*77c1e3ccSAndroid Build Coastguard Worker   CHECK_MEM_ERROR(cm, row_mt_sync->num_finished_cols,
201*77c1e3ccSAndroid Build Coastguard Worker                   aom_malloc(sizeof(*row_mt_sync->num_finished_cols) * rows));
202*77c1e3ccSAndroid Build Coastguard Worker 
203*77c1e3ccSAndroid Build Coastguard Worker   row_mt_sync->rows = rows;
204*77c1e3ccSAndroid Build Coastguard Worker   // Set up nsync.
205*77c1e3ccSAndroid Build Coastguard Worker   row_mt_sync->sync_range = 1;
206*77c1e3ccSAndroid Build Coastguard Worker }
207*77c1e3ccSAndroid Build Coastguard Worker 
208*77c1e3ccSAndroid Build Coastguard Worker // Deallocate row based multi-threading synchronization related mutex and data
av1_row_mt_sync_mem_dealloc(AV1EncRowMultiThreadSync * row_mt_sync)209*77c1e3ccSAndroid Build Coastguard Worker void av1_row_mt_sync_mem_dealloc(AV1EncRowMultiThreadSync *row_mt_sync) {
210*77c1e3ccSAndroid Build Coastguard Worker   if (row_mt_sync != NULL) {
211*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
212*77c1e3ccSAndroid Build Coastguard Worker     int i;
213*77c1e3ccSAndroid Build Coastguard Worker 
214*77c1e3ccSAndroid Build Coastguard Worker     if (row_mt_sync->mutex_ != NULL) {
215*77c1e3ccSAndroid Build Coastguard Worker       for (i = 0; i < row_mt_sync->rows; ++i) {
216*77c1e3ccSAndroid Build Coastguard Worker         pthread_mutex_destroy(&row_mt_sync->mutex_[i]);
217*77c1e3ccSAndroid Build Coastguard Worker       }
218*77c1e3ccSAndroid Build Coastguard Worker       aom_free(row_mt_sync->mutex_);
219*77c1e3ccSAndroid Build Coastguard Worker     }
220*77c1e3ccSAndroid Build Coastguard Worker     if (row_mt_sync->cond_ != NULL) {
221*77c1e3ccSAndroid Build Coastguard Worker       for (i = 0; i < row_mt_sync->rows; ++i) {
222*77c1e3ccSAndroid Build Coastguard Worker         pthread_cond_destroy(&row_mt_sync->cond_[i]);
223*77c1e3ccSAndroid Build Coastguard Worker       }
224*77c1e3ccSAndroid Build Coastguard Worker       aom_free(row_mt_sync->cond_);
225*77c1e3ccSAndroid Build Coastguard Worker     }
226*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
227*77c1e3ccSAndroid Build Coastguard Worker     aom_free(row_mt_sync->num_finished_cols);
228*77c1e3ccSAndroid Build Coastguard Worker 
229*77c1e3ccSAndroid Build Coastguard Worker     // clear the structure as the source of this call may be dynamic change
230*77c1e3ccSAndroid Build Coastguard Worker     // in tiles in which case this call will be followed by an _alloc()
231*77c1e3ccSAndroid Build Coastguard Worker     // which may fail.
232*77c1e3ccSAndroid Build Coastguard Worker     av1_zero(*row_mt_sync);
233*77c1e3ccSAndroid Build Coastguard Worker   }
234*77c1e3ccSAndroid Build Coastguard Worker }
235*77c1e3ccSAndroid Build Coastguard Worker 
get_sb_rows_in_frame(AV1_COMMON * cm)236*77c1e3ccSAndroid Build Coastguard Worker static inline int get_sb_rows_in_frame(AV1_COMMON *cm) {
237*77c1e3ccSAndroid Build Coastguard Worker   return CEIL_POWER_OF_TWO(cm->mi_params.mi_rows,
238*77c1e3ccSAndroid Build Coastguard Worker                            cm->seq_params->mib_size_log2);
239*77c1e3ccSAndroid Build Coastguard Worker }
240*77c1e3ccSAndroid Build Coastguard Worker 
row_mt_mem_alloc(AV1_COMP * cpi,int max_rows,int max_cols,int alloc_row_ctx)241*77c1e3ccSAndroid Build Coastguard Worker static void row_mt_mem_alloc(AV1_COMP *cpi, int max_rows, int max_cols,
242*77c1e3ccSAndroid Build Coastguard Worker                              int alloc_row_ctx) {
243*77c1e3ccSAndroid Build Coastguard Worker   struct AV1Common *cm = &cpi->common;
244*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
245*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
246*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
247*77c1e3ccSAndroid Build Coastguard Worker   int tile_col, tile_row;
248*77c1e3ccSAndroid Build Coastguard Worker 
249*77c1e3ccSAndroid Build Coastguard Worker   av1_row_mt_mem_dealloc(cpi);
250*77c1e3ccSAndroid Build Coastguard Worker 
251*77c1e3ccSAndroid Build Coastguard Worker   // Allocate memory for row based multi-threading
252*77c1e3ccSAndroid Build Coastguard Worker   for (tile_row = 0; tile_row < tile_rows; tile_row++) {
253*77c1e3ccSAndroid Build Coastguard Worker     for (tile_col = 0; tile_col < tile_cols; tile_col++) {
254*77c1e3ccSAndroid Build Coastguard Worker       int tile_index = tile_row * tile_cols + tile_col;
255*77c1e3ccSAndroid Build Coastguard Worker       TileDataEnc *const this_tile = &cpi->tile_data[tile_index];
256*77c1e3ccSAndroid Build Coastguard Worker 
257*77c1e3ccSAndroid Build Coastguard Worker       row_mt_sync_mem_alloc(&this_tile->row_mt_sync, cm, max_rows);
258*77c1e3ccSAndroid Build Coastguard Worker 
259*77c1e3ccSAndroid Build Coastguard Worker       if (alloc_row_ctx) {
260*77c1e3ccSAndroid Build Coastguard Worker         assert(max_cols > 0);
261*77c1e3ccSAndroid Build Coastguard Worker         const int num_row_ctx = AOMMAX(1, (max_cols - 1));
262*77c1e3ccSAndroid Build Coastguard Worker         CHECK_MEM_ERROR(cm, this_tile->row_ctx,
263*77c1e3ccSAndroid Build Coastguard Worker                         (FRAME_CONTEXT *)aom_memalign(
264*77c1e3ccSAndroid Build Coastguard Worker                             16, num_row_ctx * sizeof(*this_tile->row_ctx)));
265*77c1e3ccSAndroid Build Coastguard Worker       }
266*77c1e3ccSAndroid Build Coastguard Worker     }
267*77c1e3ccSAndroid Build Coastguard Worker   }
268*77c1e3ccSAndroid Build Coastguard Worker   const int sb_rows = get_sb_rows_in_frame(cm);
269*77c1e3ccSAndroid Build Coastguard Worker   CHECK_MEM_ERROR(
270*77c1e3ccSAndroid Build Coastguard Worker       cm, enc_row_mt->num_tile_cols_done,
271*77c1e3ccSAndroid Build Coastguard Worker       aom_malloc(sizeof(*enc_row_mt->num_tile_cols_done) * sb_rows));
272*77c1e3ccSAndroid Build Coastguard Worker 
273*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->allocated_rows = max_rows;
274*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->allocated_cols = max_cols - 1;
275*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->allocated_sb_rows = sb_rows;
276*77c1e3ccSAndroid Build Coastguard Worker }
277*77c1e3ccSAndroid Build Coastguard Worker 
av1_row_mt_mem_dealloc(AV1_COMP * cpi)278*77c1e3ccSAndroid Build Coastguard Worker void av1_row_mt_mem_dealloc(AV1_COMP *cpi) {
279*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
280*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = enc_row_mt->allocated_tile_cols;
281*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = enc_row_mt->allocated_tile_rows;
282*77c1e3ccSAndroid Build Coastguard Worker   int tile_col, tile_row;
283*77c1e3ccSAndroid Build Coastguard Worker 
284*77c1e3ccSAndroid Build Coastguard Worker   // Free row based multi-threading sync memory
285*77c1e3ccSAndroid Build Coastguard Worker   for (tile_row = 0; tile_row < tile_rows; tile_row++) {
286*77c1e3ccSAndroid Build Coastguard Worker     for (tile_col = 0; tile_col < tile_cols; tile_col++) {
287*77c1e3ccSAndroid Build Coastguard Worker       int tile_index = tile_row * tile_cols + tile_col;
288*77c1e3ccSAndroid Build Coastguard Worker       TileDataEnc *const this_tile = &cpi->tile_data[tile_index];
289*77c1e3ccSAndroid Build Coastguard Worker 
290*77c1e3ccSAndroid Build Coastguard Worker       av1_row_mt_sync_mem_dealloc(&this_tile->row_mt_sync);
291*77c1e3ccSAndroid Build Coastguard Worker 
292*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->oxcf.algo_cfg.cdf_update_mode) {
293*77c1e3ccSAndroid Build Coastguard Worker         aom_free(this_tile->row_ctx);
294*77c1e3ccSAndroid Build Coastguard Worker         this_tile->row_ctx = NULL;
295*77c1e3ccSAndroid Build Coastguard Worker       }
296*77c1e3ccSAndroid Build Coastguard Worker     }
297*77c1e3ccSAndroid Build Coastguard Worker   }
298*77c1e3ccSAndroid Build Coastguard Worker   aom_free(enc_row_mt->num_tile_cols_done);
299*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->num_tile_cols_done = NULL;
300*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->allocated_rows = 0;
301*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->allocated_cols = 0;
302*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->allocated_sb_rows = 0;
303*77c1e3ccSAndroid Build Coastguard Worker }
304*77c1e3ccSAndroid Build Coastguard Worker 
assign_tile_to_thread(int * thread_id_to_tile_id,int num_tiles,int num_workers)305*77c1e3ccSAndroid Build Coastguard Worker static inline void assign_tile_to_thread(int *thread_id_to_tile_id,
306*77c1e3ccSAndroid Build Coastguard Worker                                          int num_tiles, int num_workers) {
307*77c1e3ccSAndroid Build Coastguard Worker   int tile_id = 0;
308*77c1e3ccSAndroid Build Coastguard Worker   int i;
309*77c1e3ccSAndroid Build Coastguard Worker 
310*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < num_workers; i++) {
311*77c1e3ccSAndroid Build Coastguard Worker     thread_id_to_tile_id[i] = tile_id++;
312*77c1e3ccSAndroid Build Coastguard Worker     if (tile_id == num_tiles) tile_id = 0;
313*77c1e3ccSAndroid Build Coastguard Worker   }
314*77c1e3ccSAndroid Build Coastguard Worker }
315*77c1e3ccSAndroid Build Coastguard Worker 
get_next_job(TileDataEnc * const tile_data,int * current_mi_row,int mib_size)316*77c1e3ccSAndroid Build Coastguard Worker static inline int get_next_job(TileDataEnc *const tile_data,
317*77c1e3ccSAndroid Build Coastguard Worker                                int *current_mi_row, int mib_size) {
318*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadSync *const row_mt_sync = &tile_data->row_mt_sync;
319*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row_end = tile_data->tile_info.mi_row_end;
320*77c1e3ccSAndroid Build Coastguard Worker 
321*77c1e3ccSAndroid Build Coastguard Worker   if (row_mt_sync->next_mi_row < mi_row_end) {
322*77c1e3ccSAndroid Build Coastguard Worker     *current_mi_row = row_mt_sync->next_mi_row;
323*77c1e3ccSAndroid Build Coastguard Worker     row_mt_sync->num_threads_working++;
324*77c1e3ccSAndroid Build Coastguard Worker     row_mt_sync->next_mi_row += mib_size;
325*77c1e3ccSAndroid Build Coastguard Worker     return 1;
326*77c1e3ccSAndroid Build Coastguard Worker   }
327*77c1e3ccSAndroid Build Coastguard Worker   return 0;
328*77c1e3ccSAndroid Build Coastguard Worker }
329*77c1e3ccSAndroid Build Coastguard Worker 
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)330*77c1e3ccSAndroid Build Coastguard Worker static inline void switch_tile_and_get_next_job(
331*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMMON *const cm, TileDataEnc *const tile_data, int *cur_tile_id,
332*77c1e3ccSAndroid Build Coastguard Worker     int *current_mi_row, int *end_of_frame, int is_firstpass,
333*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE fp_block_size) {
334*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
335*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
336*77c1e3ccSAndroid Build Coastguard Worker 
337*77c1e3ccSAndroid Build Coastguard Worker   int tile_id = -1;  // Stores the tile ID with minimum proc done
338*77c1e3ccSAndroid Build Coastguard Worker   int max_mis_to_encode = 0;
339*77c1e3ccSAndroid Build Coastguard Worker   int min_num_threads_working = INT_MAX;
340*77c1e3ccSAndroid Build Coastguard Worker 
341*77c1e3ccSAndroid Build Coastguard Worker   for (int tile_row = 0; tile_row < tile_rows; tile_row++) {
342*77c1e3ccSAndroid Build Coastguard Worker     for (int tile_col = 0; tile_col < tile_cols; tile_col++) {
343*77c1e3ccSAndroid Build Coastguard Worker       int tile_index = tile_row * tile_cols + tile_col;
344*77c1e3ccSAndroid Build Coastguard Worker       TileDataEnc *const this_tile = &tile_data[tile_index];
345*77c1e3ccSAndroid Build Coastguard Worker       AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
346*77c1e3ccSAndroid Build Coastguard Worker 
347*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_REALTIME_ONLY
348*77c1e3ccSAndroid Build Coastguard Worker       int num_b_rows_in_tile =
349*77c1e3ccSAndroid Build Coastguard Worker           av1_get_sb_rows_in_tile(cm, &this_tile->tile_info);
350*77c1e3ccSAndroid Build Coastguard Worker       int num_b_cols_in_tile =
351*77c1e3ccSAndroid Build Coastguard Worker           av1_get_sb_cols_in_tile(cm, &this_tile->tile_info);
352*77c1e3ccSAndroid Build Coastguard Worker #else
353*77c1e3ccSAndroid Build Coastguard Worker       int num_b_rows_in_tile =
354*77c1e3ccSAndroid Build Coastguard Worker           is_firstpass
355*77c1e3ccSAndroid Build Coastguard Worker               ? av1_get_unit_rows_in_tile(&this_tile->tile_info, fp_block_size)
356*77c1e3ccSAndroid Build Coastguard Worker               : av1_get_sb_rows_in_tile(cm, &this_tile->tile_info);
357*77c1e3ccSAndroid Build Coastguard Worker       int num_b_cols_in_tile =
358*77c1e3ccSAndroid Build Coastguard Worker           is_firstpass
359*77c1e3ccSAndroid Build Coastguard Worker               ? av1_get_unit_cols_in_tile(&this_tile->tile_info, fp_block_size)
360*77c1e3ccSAndroid Build Coastguard Worker               : av1_get_sb_cols_in_tile(cm, &this_tile->tile_info);
361*77c1e3ccSAndroid Build Coastguard Worker #endif
362*77c1e3ccSAndroid Build Coastguard Worker       int theoretical_limit_on_threads =
363*77c1e3ccSAndroid Build Coastguard Worker           AOMMIN((num_b_cols_in_tile + 1) >> 1, num_b_rows_in_tile);
364*77c1e3ccSAndroid Build Coastguard Worker       int num_threads_working = row_mt_sync->num_threads_working;
365*77c1e3ccSAndroid Build Coastguard Worker 
366*77c1e3ccSAndroid Build Coastguard Worker       if (num_threads_working < theoretical_limit_on_threads) {
367*77c1e3ccSAndroid Build Coastguard Worker         int num_mis_to_encode =
368*77c1e3ccSAndroid Build Coastguard Worker             this_tile->tile_info.mi_row_end - row_mt_sync->next_mi_row;
369*77c1e3ccSAndroid Build Coastguard Worker 
370*77c1e3ccSAndroid Build Coastguard Worker         // Tile to be processed by this thread is selected on the basis of
371*77c1e3ccSAndroid Build Coastguard Worker         // availability of jobs:
372*77c1e3ccSAndroid Build Coastguard Worker         // 1) If jobs are available, tile to be processed is chosen on the
373*77c1e3ccSAndroid Build Coastguard Worker         // basis of minimum number of threads working for that tile. If two or
374*77c1e3ccSAndroid Build Coastguard Worker         // more tiles have same number of threads working for them, then the
375*77c1e3ccSAndroid Build Coastguard Worker         // tile with maximum number of jobs available will be chosen.
376*77c1e3ccSAndroid Build Coastguard Worker         // 2) If no jobs are available, then end_of_frame is reached.
377*77c1e3ccSAndroid Build Coastguard Worker         if (num_mis_to_encode > 0) {
378*77c1e3ccSAndroid Build Coastguard Worker           if (num_threads_working < min_num_threads_working) {
379*77c1e3ccSAndroid Build Coastguard Worker             min_num_threads_working = num_threads_working;
380*77c1e3ccSAndroid Build Coastguard Worker             max_mis_to_encode = 0;
381*77c1e3ccSAndroid Build Coastguard Worker           }
382*77c1e3ccSAndroid Build Coastguard Worker           if (num_threads_working == min_num_threads_working &&
383*77c1e3ccSAndroid Build Coastguard Worker               num_mis_to_encode > max_mis_to_encode) {
384*77c1e3ccSAndroid Build Coastguard Worker             tile_id = tile_index;
385*77c1e3ccSAndroid Build Coastguard Worker             max_mis_to_encode = num_mis_to_encode;
386*77c1e3ccSAndroid Build Coastguard Worker           }
387*77c1e3ccSAndroid Build Coastguard Worker         }
388*77c1e3ccSAndroid Build Coastguard Worker       }
389*77c1e3ccSAndroid Build Coastguard Worker     }
390*77c1e3ccSAndroid Build Coastguard Worker   }
391*77c1e3ccSAndroid Build Coastguard Worker   if (tile_id == -1) {
392*77c1e3ccSAndroid Build Coastguard Worker     *end_of_frame = 1;
393*77c1e3ccSAndroid Build Coastguard Worker   } else {
394*77c1e3ccSAndroid Build Coastguard Worker     // Update the current tile id to the tile id that will be processed next,
395*77c1e3ccSAndroid Build Coastguard Worker     // which will be the least processed tile.
396*77c1e3ccSAndroid Build Coastguard Worker     *cur_tile_id = tile_id;
397*77c1e3ccSAndroid Build Coastguard Worker     const int unit_height = mi_size_high[fp_block_size];
398*77c1e3ccSAndroid Build Coastguard Worker     get_next_job(&tile_data[tile_id], current_mi_row,
399*77c1e3ccSAndroid Build Coastguard Worker                  is_firstpass ? unit_height : cm->seq_params->mib_size);
400*77c1e3ccSAndroid Build Coastguard Worker   }
401*77c1e3ccSAndroid Build Coastguard Worker }
402*77c1e3ccSAndroid Build Coastguard Worker 
403*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
set_firstpass_encode_done(AV1_COMP * cpi)404*77c1e3ccSAndroid Build Coastguard Worker static void set_firstpass_encode_done(AV1_COMP *cpi) {
405*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
406*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
407*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
408*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
409*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE fp_block_size = cpi->fp_block_size;
410*77c1e3ccSAndroid Build Coastguard Worker   const int unit_height = mi_size_high[fp_block_size];
411*77c1e3ccSAndroid Build Coastguard Worker 
412*77c1e3ccSAndroid Build Coastguard Worker   // In case of multithreading of firstpass encode, due to top-right
413*77c1e3ccSAndroid Build Coastguard Worker   // dependency, the worker on a firstpass row waits for the completion of the
414*77c1e3ccSAndroid Build Coastguard Worker   // firstpass processing of the top and top-right fp_blocks. Hence, in case a
415*77c1e3ccSAndroid Build Coastguard Worker   // thread (main/worker) encounters an error, update the firstpass processing
416*77c1e3ccSAndroid Build Coastguard Worker   // of every row in the frame to indicate that it is complete in order to avoid
417*77c1e3ccSAndroid Build Coastguard Worker   // dependent workers waiting indefinitely.
418*77c1e3ccSAndroid Build Coastguard Worker   for (int tile_row = 0; tile_row < tile_rows; ++tile_row) {
419*77c1e3ccSAndroid Build Coastguard Worker     for (int tile_col = 0; tile_col < tile_cols; ++tile_col) {
420*77c1e3ccSAndroid Build Coastguard Worker       TileDataEnc *const tile_data =
421*77c1e3ccSAndroid Build Coastguard Worker           &cpi->tile_data[tile_row * tile_cols + tile_col];
422*77c1e3ccSAndroid Build Coastguard Worker       TileInfo *tile = &tile_data->tile_info;
423*77c1e3ccSAndroid Build Coastguard Worker       AV1EncRowMultiThreadSync *const row_mt_sync = &tile_data->row_mt_sync;
424*77c1e3ccSAndroid Build Coastguard Worker       const int unit_cols_in_tile =
425*77c1e3ccSAndroid Build Coastguard Worker           av1_get_unit_cols_in_tile(tile, fp_block_size);
426*77c1e3ccSAndroid Build Coastguard Worker       for (int mi_row = tile->mi_row_start, unit_row_in_tile = 0;
427*77c1e3ccSAndroid Build Coastguard Worker            mi_row < tile->mi_row_end;
428*77c1e3ccSAndroid Build Coastguard Worker            mi_row += unit_height, unit_row_in_tile++) {
429*77c1e3ccSAndroid Build Coastguard Worker         enc_row_mt->sync_write_ptr(row_mt_sync, unit_row_in_tile,
430*77c1e3ccSAndroid Build Coastguard Worker                                    unit_cols_in_tile - 1, unit_cols_in_tile);
431*77c1e3ccSAndroid Build Coastguard Worker       }
432*77c1e3ccSAndroid Build Coastguard Worker     }
433*77c1e3ccSAndroid Build Coastguard Worker   }
434*77c1e3ccSAndroid Build Coastguard Worker }
435*77c1e3ccSAndroid Build Coastguard Worker 
fp_enc_row_mt_worker_hook(void * arg1,void * unused)436*77c1e3ccSAndroid Build Coastguard Worker static int fp_enc_row_mt_worker_hook(void *arg1, void *unused) {
437*77c1e3ccSAndroid Build Coastguard Worker   EncWorkerData *const thread_data = (EncWorkerData *)arg1;
438*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMP *const cpi = thread_data->cpi;
439*77c1e3ccSAndroid Build Coastguard Worker   int thread_id = thread_data->thread_id;
440*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
441*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
442*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *enc_row_mt_mutex_ = enc_row_mt->mutex_;
443*77c1e3ccSAndroid Build Coastguard Worker #endif
444*77c1e3ccSAndroid Build Coastguard Worker   (void)unused;
445*77c1e3ccSAndroid Build Coastguard Worker   struct aom_internal_error_info *const error_info = &thread_data->error_info;
446*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &thread_data->td->mb.e_mbd;
447*77c1e3ccSAndroid Build Coastguard Worker   xd->error_info = error_info;
448*77c1e3ccSAndroid Build Coastguard Worker 
449*77c1e3ccSAndroid Build Coastguard Worker   // The jmp_buf is valid only for the duration of the function that calls
450*77c1e3ccSAndroid Build Coastguard Worker   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
451*77c1e3ccSAndroid Build Coastguard Worker   // before it returns.
452*77c1e3ccSAndroid Build Coastguard Worker   if (setjmp(error_info->jmp)) {
453*77c1e3ccSAndroid Build Coastguard Worker     error_info->setjmp = 0;
454*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
455*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(enc_row_mt_mutex_);
456*77c1e3ccSAndroid Build Coastguard Worker     enc_row_mt->firstpass_mt_exit = true;
457*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(enc_row_mt_mutex_);
458*77c1e3ccSAndroid Build Coastguard Worker #endif
459*77c1e3ccSAndroid Build Coastguard Worker     set_firstpass_encode_done(cpi);
460*77c1e3ccSAndroid Build Coastguard Worker     return 0;
461*77c1e3ccSAndroid Build Coastguard Worker   }
462*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 1;
463*77c1e3ccSAndroid Build Coastguard Worker 
464*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
465*77c1e3ccSAndroid Build Coastguard Worker   int cur_tile_id = enc_row_mt->thread_id_to_tile_id[thread_id];
466*77c1e3ccSAndroid Build Coastguard Worker   assert(cur_tile_id != -1);
467*77c1e3ccSAndroid Build Coastguard Worker 
468*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE fp_block_size = cpi->fp_block_size;
469*77c1e3ccSAndroid Build Coastguard Worker   const int unit_height = mi_size_high[fp_block_size];
470*77c1e3ccSAndroid Build Coastguard Worker   int end_of_frame = 0;
471*77c1e3ccSAndroid Build Coastguard Worker   while (1) {
472*77c1e3ccSAndroid Build Coastguard Worker     int current_mi_row = -1;
473*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
474*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(enc_row_mt_mutex_);
475*77c1e3ccSAndroid Build Coastguard Worker #endif
476*77c1e3ccSAndroid Build Coastguard Worker     bool firstpass_mt_exit = enc_row_mt->firstpass_mt_exit;
477*77c1e3ccSAndroid Build Coastguard Worker     if (!firstpass_mt_exit && !get_next_job(&cpi->tile_data[cur_tile_id],
478*77c1e3ccSAndroid Build Coastguard Worker                                             &current_mi_row, unit_height)) {
479*77c1e3ccSAndroid Build Coastguard Worker       // No jobs are available for the current tile. Query for the status of
480*77c1e3ccSAndroid Build Coastguard Worker       // other tiles and get the next job if available
481*77c1e3ccSAndroid Build Coastguard Worker       switch_tile_and_get_next_job(cm, cpi->tile_data, &cur_tile_id,
482*77c1e3ccSAndroid Build Coastguard Worker                                    &current_mi_row, &end_of_frame, 1,
483*77c1e3ccSAndroid Build Coastguard Worker                                    fp_block_size);
484*77c1e3ccSAndroid Build Coastguard Worker     }
485*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
486*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(enc_row_mt_mutex_);
487*77c1e3ccSAndroid Build Coastguard Worker #endif
488*77c1e3ccSAndroid Build Coastguard Worker     // When firstpass_mt_exit is set to true, other workers need not pursue any
489*77c1e3ccSAndroid Build Coastguard Worker     // further jobs.
490*77c1e3ccSAndroid Build Coastguard Worker     if (firstpass_mt_exit || end_of_frame) break;
491*77c1e3ccSAndroid Build Coastguard Worker 
492*77c1e3ccSAndroid Build Coastguard Worker     TileDataEnc *const this_tile = &cpi->tile_data[cur_tile_id];
493*77c1e3ccSAndroid Build Coastguard Worker     AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
494*77c1e3ccSAndroid Build Coastguard Worker     ThreadData *td = thread_data->td;
495*77c1e3ccSAndroid Build Coastguard Worker 
496*77c1e3ccSAndroid Build Coastguard Worker     assert(current_mi_row != -1 &&
497*77c1e3ccSAndroid Build Coastguard Worker            current_mi_row < this_tile->tile_info.mi_row_end);
498*77c1e3ccSAndroid Build Coastguard Worker 
499*77c1e3ccSAndroid Build Coastguard Worker     const int unit_height_log2 = mi_size_high_log2[fp_block_size];
500*77c1e3ccSAndroid Build Coastguard Worker     av1_first_pass_row(cpi, td, this_tile, current_mi_row >> unit_height_log2,
501*77c1e3ccSAndroid Build Coastguard Worker                        fp_block_size);
502*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
503*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(enc_row_mt_mutex_);
504*77c1e3ccSAndroid Build Coastguard Worker #endif
505*77c1e3ccSAndroid Build Coastguard Worker     row_mt_sync->num_threads_working--;
506*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
507*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(enc_row_mt_mutex_);
508*77c1e3ccSAndroid Build Coastguard Worker #endif
509*77c1e3ccSAndroid Build Coastguard Worker   }
510*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 0;
511*77c1e3ccSAndroid Build Coastguard Worker   return 1;
512*77c1e3ccSAndroid Build Coastguard Worker }
513*77c1e3ccSAndroid Build Coastguard Worker #endif
514*77c1e3ccSAndroid Build Coastguard Worker 
launch_loop_filter_rows(AV1_COMMON * cm,EncWorkerData * thread_data,AV1EncRowMultiThreadInfo * enc_row_mt,int mib_size_log2)515*77c1e3ccSAndroid Build Coastguard Worker static void launch_loop_filter_rows(AV1_COMMON *cm, EncWorkerData *thread_data,
516*77c1e3ccSAndroid Build Coastguard Worker                                     AV1EncRowMultiThreadInfo *enc_row_mt,
517*77c1e3ccSAndroid Build Coastguard Worker                                     int mib_size_log2) {
518*77c1e3ccSAndroid Build Coastguard Worker   AV1LfSync *const lf_sync = (AV1LfSync *)thread_data->lf_sync;
519*77c1e3ccSAndroid Build Coastguard Worker   const int sb_rows = get_sb_rows_in_frame(cm);
520*77c1e3ccSAndroid Build Coastguard Worker   AV1LfMTInfo *cur_job_info;
521*77c1e3ccSAndroid Build Coastguard Worker   bool row_mt_exit = false;
522*77c1e3ccSAndroid Build Coastguard Worker   (void)enc_row_mt;
523*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
524*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *enc_row_mt_mutex_ = enc_row_mt->mutex_;
525*77c1e3ccSAndroid Build Coastguard Worker #endif
526*77c1e3ccSAndroid Build Coastguard Worker 
527*77c1e3ccSAndroid Build Coastguard Worker   while ((cur_job_info = get_lf_job_info(lf_sync)) != NULL) {
528*77c1e3ccSAndroid Build Coastguard Worker     LFWorkerData *const lf_data = (LFWorkerData *)thread_data->lf_data;
529*77c1e3ccSAndroid Build Coastguard Worker     const int lpf_opt_level = cur_job_info->lpf_opt_level;
530*77c1e3ccSAndroid Build Coastguard Worker     (void)sb_rows;
531*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
532*77c1e3ccSAndroid Build Coastguard Worker     const int cur_sb_row = cur_job_info->mi_row >> mib_size_log2;
533*77c1e3ccSAndroid Build Coastguard Worker     const int next_sb_row = AOMMIN(sb_rows - 1, cur_sb_row + 1);
534*77c1e3ccSAndroid Build Coastguard Worker     // Wait for current and next superblock row to finish encoding.
535*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(enc_row_mt_mutex_);
536*77c1e3ccSAndroid Build Coastguard Worker     while (!enc_row_mt->row_mt_exit &&
537*77c1e3ccSAndroid Build Coastguard Worker            (enc_row_mt->num_tile_cols_done[cur_sb_row] < cm->tiles.cols ||
538*77c1e3ccSAndroid Build Coastguard Worker             enc_row_mt->num_tile_cols_done[next_sb_row] < cm->tiles.cols)) {
539*77c1e3ccSAndroid Build Coastguard Worker       pthread_cond_wait(enc_row_mt->cond_, enc_row_mt_mutex_);
540*77c1e3ccSAndroid Build Coastguard Worker     }
541*77c1e3ccSAndroid Build Coastguard Worker     row_mt_exit = enc_row_mt->row_mt_exit;
542*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(enc_row_mt_mutex_);
543*77c1e3ccSAndroid Build Coastguard Worker #endif
544*77c1e3ccSAndroid Build Coastguard Worker     if (row_mt_exit) return;
545*77c1e3ccSAndroid Build Coastguard Worker 
546*77c1e3ccSAndroid Build Coastguard Worker     av1_thread_loop_filter_rows(
547*77c1e3ccSAndroid Build Coastguard Worker         lf_data->frame_buffer, lf_data->cm, lf_data->planes, lf_data->xd,
548*77c1e3ccSAndroid Build Coastguard Worker         cur_job_info->mi_row, cur_job_info->plane, cur_job_info->dir,
549*77c1e3ccSAndroid Build Coastguard Worker         lpf_opt_level, lf_sync, &thread_data->error_info, lf_data->params_buf,
550*77c1e3ccSAndroid Build Coastguard Worker         lf_data->tx_buf, mib_size_log2);
551*77c1e3ccSAndroid Build Coastguard Worker   }
552*77c1e3ccSAndroid Build Coastguard Worker }
553*77c1e3ccSAndroid Build Coastguard Worker 
set_encoding_done(AV1_COMP * cpi)554*77c1e3ccSAndroid Build Coastguard Worker static void set_encoding_done(AV1_COMP *cpi) {
555*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
556*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
557*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
558*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
559*77c1e3ccSAndroid Build Coastguard Worker   const int mib_size = cm->seq_params->mib_size;
560*77c1e3ccSAndroid Build Coastguard Worker 
561*77c1e3ccSAndroid Build Coastguard Worker   // In case of row-multithreading, due to top-right dependency, the worker on
562*77c1e3ccSAndroid Build Coastguard Worker   // an SB row waits for the completion of the encode of the top and top-right
563*77c1e3ccSAndroid Build Coastguard Worker   // SBs. Hence, in case a thread (main/worker) encounters an error, update that
564*77c1e3ccSAndroid Build Coastguard Worker   // encoding of every SB row in the frame is complete in order to avoid the
565*77c1e3ccSAndroid Build Coastguard Worker   // dependent workers of every tile from waiting indefinitely.
566*77c1e3ccSAndroid Build Coastguard Worker   for (int tile_row = 0; tile_row < tile_rows; tile_row++) {
567*77c1e3ccSAndroid Build Coastguard Worker     for (int tile_col = 0; tile_col < tile_cols; tile_col++) {
568*77c1e3ccSAndroid Build Coastguard Worker       TileDataEnc *const this_tile =
569*77c1e3ccSAndroid Build Coastguard Worker           &cpi->tile_data[tile_row * tile_cols + tile_col];
570*77c1e3ccSAndroid Build Coastguard Worker       const TileInfo *const tile_info = &this_tile->tile_info;
571*77c1e3ccSAndroid Build Coastguard Worker       AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
572*77c1e3ccSAndroid Build Coastguard Worker       const int sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile_info);
573*77c1e3ccSAndroid Build Coastguard Worker       for (int mi_row = tile_info->mi_row_start, sb_row_in_tile = 0;
574*77c1e3ccSAndroid Build Coastguard Worker            mi_row < tile_info->mi_row_end;
575*77c1e3ccSAndroid Build Coastguard Worker            mi_row += mib_size, sb_row_in_tile++) {
576*77c1e3ccSAndroid Build Coastguard Worker         enc_row_mt->sync_write_ptr(row_mt_sync, sb_row_in_tile,
577*77c1e3ccSAndroid Build Coastguard Worker                                    sb_cols_in_tile - 1, sb_cols_in_tile);
578*77c1e3ccSAndroid Build Coastguard Worker       }
579*77c1e3ccSAndroid Build Coastguard Worker     }
580*77c1e3ccSAndroid Build Coastguard Worker   }
581*77c1e3ccSAndroid Build Coastguard Worker }
582*77c1e3ccSAndroid Build Coastguard Worker 
lpf_mt_with_enc_enabled(int pipeline_lpf_mt_with_enc,const int filter_level[2])583*77c1e3ccSAndroid Build Coastguard Worker static bool lpf_mt_with_enc_enabled(int pipeline_lpf_mt_with_enc,
584*77c1e3ccSAndroid Build Coastguard Worker                                     const int filter_level[2]) {
585*77c1e3ccSAndroid Build Coastguard Worker   return pipeline_lpf_mt_with_enc && (filter_level[0] || filter_level[1]);
586*77c1e3ccSAndroid Build Coastguard Worker }
587*77c1e3ccSAndroid Build Coastguard Worker 
enc_row_mt_worker_hook(void * arg1,void * unused)588*77c1e3ccSAndroid Build Coastguard Worker static int enc_row_mt_worker_hook(void *arg1, void *unused) {
589*77c1e3ccSAndroid Build Coastguard Worker   EncWorkerData *const thread_data = (EncWorkerData *)arg1;
590*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMP *const cpi = thread_data->cpi;
591*77c1e3ccSAndroid Build Coastguard Worker   int thread_id = thread_data->thread_id;
592*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
593*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
594*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *enc_row_mt_mutex_ = enc_row_mt->mutex_;
595*77c1e3ccSAndroid Build Coastguard Worker #endif
596*77c1e3ccSAndroid Build Coastguard Worker   (void)unused;
597*77c1e3ccSAndroid Build Coastguard Worker 
598*77c1e3ccSAndroid Build Coastguard Worker   struct aom_internal_error_info *const error_info = &thread_data->error_info;
599*77c1e3ccSAndroid Build Coastguard Worker   AV1LfSync *const lf_sync = thread_data->lf_sync;
600*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &thread_data->td->mb.e_mbd;
601*77c1e3ccSAndroid Build Coastguard Worker   xd->error_info = error_info;
602*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *volatile const cm = &cpi->common;
603*77c1e3ccSAndroid Build Coastguard Worker   volatile const bool do_pipelined_lpf_mt_with_enc = lpf_mt_with_enc_enabled(
604*77c1e3ccSAndroid Build Coastguard Worker       cpi->mt_info.pipeline_lpf_mt_with_enc, cm->lf.filter_level);
605*77c1e3ccSAndroid Build Coastguard Worker 
606*77c1e3ccSAndroid Build Coastguard Worker   // The jmp_buf is valid only for the duration of the function that calls
607*77c1e3ccSAndroid Build Coastguard Worker   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
608*77c1e3ccSAndroid Build Coastguard Worker   // before it returns.
609*77c1e3ccSAndroid Build Coastguard Worker   if (setjmp(error_info->jmp)) {
610*77c1e3ccSAndroid Build Coastguard Worker     error_info->setjmp = 0;
611*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
612*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(enc_row_mt_mutex_);
613*77c1e3ccSAndroid Build Coastguard Worker     enc_row_mt->row_mt_exit = true;
614*77c1e3ccSAndroid Build Coastguard Worker     // Wake up all the workers waiting in launch_loop_filter_rows() to exit in
615*77c1e3ccSAndroid Build Coastguard Worker     // case of an error.
616*77c1e3ccSAndroid Build Coastguard Worker     pthread_cond_broadcast(enc_row_mt->cond_);
617*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(enc_row_mt_mutex_);
618*77c1e3ccSAndroid Build Coastguard Worker #endif
619*77c1e3ccSAndroid Build Coastguard Worker     set_encoding_done(cpi);
620*77c1e3ccSAndroid Build Coastguard Worker 
621*77c1e3ccSAndroid Build Coastguard Worker     if (do_pipelined_lpf_mt_with_enc) {
622*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
623*77c1e3ccSAndroid Build Coastguard Worker       pthread_mutex_lock(lf_sync->job_mutex);
624*77c1e3ccSAndroid Build Coastguard Worker       lf_sync->lf_mt_exit = true;
625*77c1e3ccSAndroid Build Coastguard Worker       pthread_mutex_unlock(lf_sync->job_mutex);
626*77c1e3ccSAndroid Build Coastguard Worker #endif
627*77c1e3ccSAndroid Build Coastguard Worker       av1_set_vert_loop_filter_done(&cpi->common, lf_sync,
628*77c1e3ccSAndroid Build Coastguard Worker                                     cpi->common.seq_params->mib_size_log2);
629*77c1e3ccSAndroid Build Coastguard Worker     }
630*77c1e3ccSAndroid Build Coastguard Worker     return 0;
631*77c1e3ccSAndroid Build Coastguard Worker   }
632*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 1;
633*77c1e3ccSAndroid Build Coastguard Worker 
634*77c1e3ccSAndroid Build Coastguard Worker   const int mib_size_log2 = cm->seq_params->mib_size_log2;
635*77c1e3ccSAndroid Build Coastguard Worker   int cur_tile_id = enc_row_mt->thread_id_to_tile_id[thread_id];
636*77c1e3ccSAndroid Build Coastguard Worker 
637*77c1e3ccSAndroid Build Coastguard Worker   // Preallocate the pc_tree for realtime coding to reduce the cost of memory
638*77c1e3ccSAndroid Build Coastguard Worker   // allocation.
639*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.rt_sf.use_nonrd_pick_mode) {
640*77c1e3ccSAndroid Build Coastguard Worker     thread_data->td->pc_root = av1_alloc_pc_tree_node(cm->seq_params->sb_size);
641*77c1e3ccSAndroid Build Coastguard Worker     if (!thread_data->td->pc_root)
642*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
643*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate PC_TREE");
644*77c1e3ccSAndroid Build Coastguard Worker   } else {
645*77c1e3ccSAndroid Build Coastguard Worker     thread_data->td->pc_root = NULL;
646*77c1e3ccSAndroid Build Coastguard Worker   }
647*77c1e3ccSAndroid Build Coastguard Worker 
648*77c1e3ccSAndroid Build Coastguard Worker   assert(cur_tile_id != -1);
649*77c1e3ccSAndroid Build Coastguard Worker 
650*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE fp_block_size = cpi->fp_block_size;
651*77c1e3ccSAndroid Build Coastguard Worker   int end_of_frame = 0;
652*77c1e3ccSAndroid Build Coastguard Worker   bool row_mt_exit = false;
653*77c1e3ccSAndroid Build Coastguard Worker 
654*77c1e3ccSAndroid Build Coastguard Worker   // When master thread does not have a valid job to process, xd->tile_ctx
655*77c1e3ccSAndroid Build Coastguard Worker   // is not set and it contains NULL pointer. This can result in NULL pointer
656*77c1e3ccSAndroid Build Coastguard Worker   // access violation if accessed beyond the encode stage. Hence, updating
657*77c1e3ccSAndroid Build Coastguard Worker   // thread_data->td->mb.e_mbd.tile_ctx is initialized with common frame
658*77c1e3ccSAndroid Build Coastguard Worker   // context to avoid NULL pointer access in subsequent stages.
659*77c1e3ccSAndroid Build Coastguard Worker   thread_data->td->mb.e_mbd.tile_ctx = cm->fc;
660*77c1e3ccSAndroid Build Coastguard Worker   while (1) {
661*77c1e3ccSAndroid Build Coastguard Worker     int current_mi_row = -1;
662*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
663*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(enc_row_mt_mutex_);
664*77c1e3ccSAndroid Build Coastguard Worker #endif
665*77c1e3ccSAndroid Build Coastguard Worker     row_mt_exit = enc_row_mt->row_mt_exit;
666*77c1e3ccSAndroid Build Coastguard Worker     // row_mt_exit check here can be avoided as it is checked after
667*77c1e3ccSAndroid Build Coastguard Worker     // sync_read_ptr() in encode_sb_row(). However, checking row_mt_exit here,
668*77c1e3ccSAndroid Build Coastguard Worker     // tries to return before calling the function get_next_job().
669*77c1e3ccSAndroid Build Coastguard Worker     if (!row_mt_exit &&
670*77c1e3ccSAndroid Build Coastguard Worker         !get_next_job(&cpi->tile_data[cur_tile_id], &current_mi_row,
671*77c1e3ccSAndroid Build Coastguard Worker                       cm->seq_params->mib_size)) {
672*77c1e3ccSAndroid Build Coastguard Worker       // No jobs are available for the current tile. Query for the status of
673*77c1e3ccSAndroid Build Coastguard Worker       // other tiles and get the next job if available
674*77c1e3ccSAndroid Build Coastguard Worker       switch_tile_and_get_next_job(cm, cpi->tile_data, &cur_tile_id,
675*77c1e3ccSAndroid Build Coastguard Worker                                    &current_mi_row, &end_of_frame, 0,
676*77c1e3ccSAndroid Build Coastguard Worker                                    fp_block_size);
677*77c1e3ccSAndroid Build Coastguard Worker     }
678*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
679*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(enc_row_mt_mutex_);
680*77c1e3ccSAndroid Build Coastguard Worker #endif
681*77c1e3ccSAndroid Build Coastguard Worker     // When row_mt_exit is set to true, other workers need not pursue any
682*77c1e3ccSAndroid Build Coastguard Worker     // further jobs.
683*77c1e3ccSAndroid Build Coastguard Worker     if (row_mt_exit) {
684*77c1e3ccSAndroid Build Coastguard Worker       error_info->setjmp = 0;
685*77c1e3ccSAndroid Build Coastguard Worker       return 1;
686*77c1e3ccSAndroid Build Coastguard Worker     }
687*77c1e3ccSAndroid Build Coastguard Worker 
688*77c1e3ccSAndroid Build Coastguard Worker     if (end_of_frame) break;
689*77c1e3ccSAndroid Build Coastguard Worker 
690*77c1e3ccSAndroid Build Coastguard Worker     TileDataEnc *const this_tile = &cpi->tile_data[cur_tile_id];
691*77c1e3ccSAndroid Build Coastguard Worker     AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
692*77c1e3ccSAndroid Build Coastguard Worker     const TileInfo *const tile_info = &this_tile->tile_info;
693*77c1e3ccSAndroid Build Coastguard Worker     const int tile_row = tile_info->tile_row;
694*77c1e3ccSAndroid Build Coastguard Worker     const int tile_col = tile_info->tile_col;
695*77c1e3ccSAndroid Build Coastguard Worker     ThreadData *td = thread_data->td;
696*77c1e3ccSAndroid Build Coastguard Worker     const int sb_row = current_mi_row >> mib_size_log2;
697*77c1e3ccSAndroid Build Coastguard Worker 
698*77c1e3ccSAndroid Build Coastguard Worker     assert(current_mi_row != -1 && current_mi_row <= tile_info->mi_row_end);
699*77c1e3ccSAndroid Build Coastguard Worker 
700*77c1e3ccSAndroid Build Coastguard Worker     td->mb.e_mbd.tile_ctx = td->tctx;
701*77c1e3ccSAndroid Build Coastguard Worker     td->mb.tile_pb_ctx = &this_tile->tctx;
702*77c1e3ccSAndroid Build Coastguard Worker     td->abs_sum_level = 0;
703*77c1e3ccSAndroid Build Coastguard Worker 
704*77c1e3ccSAndroid Build Coastguard Worker     if (this_tile->allow_update_cdf) {
705*77c1e3ccSAndroid Build Coastguard Worker       td->mb.row_ctx = this_tile->row_ctx;
706*77c1e3ccSAndroid Build Coastguard Worker       if (current_mi_row == tile_info->mi_row_start)
707*77c1e3ccSAndroid Build Coastguard Worker         memcpy(td->mb.e_mbd.tile_ctx, &this_tile->tctx, sizeof(FRAME_CONTEXT));
708*77c1e3ccSAndroid Build Coastguard Worker     } else {
709*77c1e3ccSAndroid Build Coastguard Worker       memcpy(td->mb.e_mbd.tile_ctx, &this_tile->tctx, sizeof(FRAME_CONTEXT));
710*77c1e3ccSAndroid Build Coastguard Worker     }
711*77c1e3ccSAndroid Build Coastguard Worker 
712*77c1e3ccSAndroid Build Coastguard Worker     av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), tile_row,
713*77c1e3ccSAndroid Build Coastguard Worker                            &td->mb.e_mbd);
714*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
715*77c1e3ccSAndroid Build Coastguard Worker     cfl_init(&td->mb.e_mbd.cfl, cm->seq_params);
716*77c1e3ccSAndroid Build Coastguard Worker #endif
717*77c1e3ccSAndroid Build Coastguard Worker     if (td->mb.txfm_search_info.mb_rd_record != NULL) {
718*77c1e3ccSAndroid Build Coastguard Worker       av1_crc32c_calculator_init(
719*77c1e3ccSAndroid Build Coastguard Worker           &td->mb.txfm_search_info.mb_rd_record->crc_calculator);
720*77c1e3ccSAndroid Build Coastguard Worker     }
721*77c1e3ccSAndroid Build Coastguard Worker 
722*77c1e3ccSAndroid Build Coastguard Worker     av1_encode_sb_row(cpi, td, tile_row, tile_col, current_mi_row);
723*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
724*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(enc_row_mt_mutex_);
725*77c1e3ccSAndroid Build Coastguard Worker #endif
726*77c1e3ccSAndroid Build Coastguard Worker     this_tile->abs_sum_level += td->abs_sum_level;
727*77c1e3ccSAndroid Build Coastguard Worker     row_mt_sync->num_threads_working--;
728*77c1e3ccSAndroid Build Coastguard Worker     enc_row_mt->num_tile_cols_done[sb_row]++;
729*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
730*77c1e3ccSAndroid Build Coastguard Worker     pthread_cond_broadcast(enc_row_mt->cond_);
731*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(enc_row_mt_mutex_);
732*77c1e3ccSAndroid Build Coastguard Worker #endif
733*77c1e3ccSAndroid Build Coastguard Worker   }
734*77c1e3ccSAndroid Build Coastguard Worker   if (do_pipelined_lpf_mt_with_enc) {
735*77c1e3ccSAndroid Build Coastguard Worker     // Loop-filter a superblock row if encoding of the current and next
736*77c1e3ccSAndroid Build Coastguard Worker     // superblock row is complete.
737*77c1e3ccSAndroid Build Coastguard Worker     // TODO(deepa.kg @ittiam.com) Evaluate encoder speed by interleaving
738*77c1e3ccSAndroid Build Coastguard Worker     // encoding and loop filter stage.
739*77c1e3ccSAndroid Build Coastguard Worker     launch_loop_filter_rows(cm, thread_data, enc_row_mt, mib_size_log2);
740*77c1e3ccSAndroid Build Coastguard Worker   }
741*77c1e3ccSAndroid Build Coastguard Worker   av1_free_pc_tree_recursive(thread_data->td->pc_root, av1_num_planes(cm), 0, 0,
742*77c1e3ccSAndroid Build Coastguard Worker                              cpi->sf.part_sf.partition_search_type);
743*77c1e3ccSAndroid Build Coastguard Worker   thread_data->td->pc_root = NULL;
744*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 0;
745*77c1e3ccSAndroid Build Coastguard Worker   return 1;
746*77c1e3ccSAndroid Build Coastguard Worker }
747*77c1e3ccSAndroid Build Coastguard Worker 
enc_worker_hook(void * arg1,void * unused)748*77c1e3ccSAndroid Build Coastguard Worker static int enc_worker_hook(void *arg1, void *unused) {
749*77c1e3ccSAndroid Build Coastguard Worker   EncWorkerData *const thread_data = (EncWorkerData *)arg1;
750*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMP *const cpi = thread_data->cpi;
751*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &thread_data->td->mb.e_mbd;
752*77c1e3ccSAndroid Build Coastguard Worker   struct aom_internal_error_info *const error_info = &thread_data->error_info;
753*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
754*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
755*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
756*77c1e3ccSAndroid Build Coastguard Worker   int t;
757*77c1e3ccSAndroid Build Coastguard Worker 
758*77c1e3ccSAndroid Build Coastguard Worker   (void)unused;
759*77c1e3ccSAndroid Build Coastguard Worker 
760*77c1e3ccSAndroid Build Coastguard Worker   xd->error_info = error_info;
761*77c1e3ccSAndroid Build Coastguard Worker 
762*77c1e3ccSAndroid Build Coastguard Worker   // The jmp_buf is valid only for the duration of the function that calls
763*77c1e3ccSAndroid Build Coastguard Worker   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
764*77c1e3ccSAndroid Build Coastguard Worker   // before it returns.
765*77c1e3ccSAndroid Build Coastguard Worker   if (setjmp(error_info->jmp)) {
766*77c1e3ccSAndroid Build Coastguard Worker     error_info->setjmp = 0;
767*77c1e3ccSAndroid Build Coastguard Worker     return 0;
768*77c1e3ccSAndroid Build Coastguard Worker   }
769*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 1;
770*77c1e3ccSAndroid Build Coastguard Worker 
771*77c1e3ccSAndroid Build Coastguard Worker   // Preallocate the pc_tree for realtime coding to reduce the cost of memory
772*77c1e3ccSAndroid Build Coastguard Worker   // allocation.
773*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.rt_sf.use_nonrd_pick_mode) {
774*77c1e3ccSAndroid Build Coastguard Worker     thread_data->td->pc_root = av1_alloc_pc_tree_node(cm->seq_params->sb_size);
775*77c1e3ccSAndroid Build Coastguard Worker     if (!thread_data->td->pc_root)
776*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
777*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate PC_TREE");
778*77c1e3ccSAndroid Build Coastguard Worker   } else {
779*77c1e3ccSAndroid Build Coastguard Worker     thread_data->td->pc_root = NULL;
780*77c1e3ccSAndroid Build Coastguard Worker   }
781*77c1e3ccSAndroid Build Coastguard Worker 
782*77c1e3ccSAndroid Build Coastguard Worker   for (t = thread_data->start; t < tile_rows * tile_cols;
783*77c1e3ccSAndroid Build Coastguard Worker        t += cpi->mt_info.num_workers) {
784*77c1e3ccSAndroid Build Coastguard Worker     int tile_row = t / tile_cols;
785*77c1e3ccSAndroid Build Coastguard Worker     int tile_col = t % tile_cols;
786*77c1e3ccSAndroid Build Coastguard Worker 
787*77c1e3ccSAndroid Build Coastguard Worker     TileDataEnc *const this_tile =
788*77c1e3ccSAndroid Build Coastguard Worker         &cpi->tile_data[tile_row * cm->tiles.cols + tile_col];
789*77c1e3ccSAndroid Build Coastguard Worker     thread_data->td->mb.e_mbd.tile_ctx = &this_tile->tctx;
790*77c1e3ccSAndroid Build Coastguard Worker     thread_data->td->mb.tile_pb_ctx = &this_tile->tctx;
791*77c1e3ccSAndroid Build Coastguard Worker     av1_encode_tile(cpi, thread_data->td, tile_row, tile_col);
792*77c1e3ccSAndroid Build Coastguard Worker   }
793*77c1e3ccSAndroid Build Coastguard Worker 
794*77c1e3ccSAndroid Build Coastguard Worker   av1_free_pc_tree_recursive(thread_data->td->pc_root, av1_num_planes(cm), 0, 0,
795*77c1e3ccSAndroid Build Coastguard Worker                              cpi->sf.part_sf.partition_search_type);
796*77c1e3ccSAndroid Build Coastguard Worker   thread_data->td->pc_root = NULL;
797*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 0;
798*77c1e3ccSAndroid Build Coastguard Worker   return 1;
799*77c1e3ccSAndroid Build Coastguard Worker }
800*77c1e3ccSAndroid Build Coastguard Worker 
av1_init_frame_mt(AV1_PRIMARY * ppi,AV1_COMP * cpi)801*77c1e3ccSAndroid Build Coastguard Worker void av1_init_frame_mt(AV1_PRIMARY *ppi, AV1_COMP *cpi) {
802*77c1e3ccSAndroid Build Coastguard Worker   cpi->mt_info.workers = ppi->p_mt_info.workers;
803*77c1e3ccSAndroid Build Coastguard Worker   cpi->mt_info.num_workers = ppi->p_mt_info.num_workers;
804*77c1e3ccSAndroid Build Coastguard Worker   cpi->mt_info.tile_thr_data = ppi->p_mt_info.tile_thr_data;
805*77c1e3ccSAndroid Build Coastguard Worker   int i;
806*77c1e3ccSAndroid Build Coastguard Worker   for (i = MOD_FP; i < NUM_MT_MODULES; i++) {
807*77c1e3ccSAndroid Build Coastguard Worker     cpi->mt_info.num_mod_workers[i] =
808*77c1e3ccSAndroid Build Coastguard Worker         AOMMIN(cpi->mt_info.num_workers, ppi->p_mt_info.num_mod_workers[i]);
809*77c1e3ccSAndroid Build Coastguard Worker   }
810*77c1e3ccSAndroid Build Coastguard Worker }
811*77c1e3ccSAndroid Build Coastguard Worker 
av1_init_cdef_worker(AV1_COMP * cpi)812*77c1e3ccSAndroid Build Coastguard Worker void av1_init_cdef_worker(AV1_COMP *cpi) {
813*77c1e3ccSAndroid Build Coastguard Worker   // The allocation is done only for level 0 parallel frames. No change
814*77c1e3ccSAndroid Build Coastguard Worker   // in config is supported in the middle of a parallel encode set, since the
815*77c1e3ccSAndroid Build Coastguard Worker   // rest of the MT modules also do not support dynamic change of config.
816*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) return;
817*77c1e3ccSAndroid Build Coastguard Worker   PrimaryMultiThreadInfo *const p_mt_info = &cpi->ppi->p_mt_info;
818*77c1e3ccSAndroid Build Coastguard Worker   int num_cdef_workers = av1_get_num_mod_workers_for_alloc(p_mt_info, MOD_CDEF);
819*77c1e3ccSAndroid Build Coastguard Worker 
820*77c1e3ccSAndroid Build Coastguard Worker   av1_alloc_cdef_buffers(&cpi->common, &p_mt_info->cdef_worker,
821*77c1e3ccSAndroid Build Coastguard Worker                          &cpi->mt_info.cdef_sync, num_cdef_workers, 1);
822*77c1e3ccSAndroid Build Coastguard Worker   cpi->mt_info.cdef_worker = p_mt_info->cdef_worker;
823*77c1e3ccSAndroid Build Coastguard Worker }
824*77c1e3ccSAndroid Build Coastguard Worker 
825*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
av1_init_lr_mt_buffers(AV1_COMP * cpi)826*77c1e3ccSAndroid Build Coastguard Worker void av1_init_lr_mt_buffers(AV1_COMP *cpi) {
827*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
828*77c1e3ccSAndroid Build Coastguard Worker   AV1LrSync *lr_sync = &cpi->mt_info.lr_row_sync;
829*77c1e3ccSAndroid Build Coastguard Worker   if (lr_sync->sync_range) {
830*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0)
831*77c1e3ccSAndroid Build Coastguard Worker       return;
832*77c1e3ccSAndroid Build Coastguard Worker     int num_lr_workers =
833*77c1e3ccSAndroid Build Coastguard Worker         av1_get_num_mod_workers_for_alloc(&cpi->ppi->p_mt_info, MOD_LR);
834*77c1e3ccSAndroid Build Coastguard Worker     assert(num_lr_workers <= lr_sync->num_workers);
835*77c1e3ccSAndroid Build Coastguard Worker     lr_sync->lrworkerdata[num_lr_workers - 1].rst_tmpbuf = cm->rst_tmpbuf;
836*77c1e3ccSAndroid Build Coastguard Worker     lr_sync->lrworkerdata[num_lr_workers - 1].rlbs = cm->rlbs;
837*77c1e3ccSAndroid Build Coastguard Worker   }
838*77c1e3ccSAndroid Build Coastguard Worker }
839*77c1e3ccSAndroid Build Coastguard Worker #endif
840*77c1e3ccSAndroid Build Coastguard Worker 
841*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
av1_init_mt_sync(AV1_COMP * cpi,int is_first_pass)842*77c1e3ccSAndroid Build Coastguard Worker void av1_init_mt_sync(AV1_COMP *cpi, int is_first_pass) {
843*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
844*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
845*77c1e3ccSAndroid Build Coastguard Worker 
846*77c1e3ccSAndroid Build Coastguard Worker   if (setjmp(cm->error->jmp)) {
847*77c1e3ccSAndroid Build Coastguard Worker     cm->error->setjmp = 0;
848*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error_copy(&cpi->ppi->error, cm->error);
849*77c1e3ccSAndroid Build Coastguard Worker   }
850*77c1e3ccSAndroid Build Coastguard Worker   cm->error->setjmp = 1;
851*77c1e3ccSAndroid Build Coastguard Worker   // Initialize enc row MT object.
852*77c1e3ccSAndroid Build Coastguard Worker   if (is_first_pass || cpi->oxcf.row_mt == 1) {
853*77c1e3ccSAndroid Build Coastguard Worker     AV1EncRowMultiThreadInfo *enc_row_mt = &mt_info->enc_row_mt;
854*77c1e3ccSAndroid Build Coastguard Worker     if (enc_row_mt->mutex_ == NULL) {
855*77c1e3ccSAndroid Build Coastguard Worker       CHECK_MEM_ERROR(cm, enc_row_mt->mutex_,
856*77c1e3ccSAndroid Build Coastguard Worker                       aom_malloc(sizeof(*(enc_row_mt->mutex_))));
857*77c1e3ccSAndroid Build Coastguard Worker       if (enc_row_mt->mutex_) pthread_mutex_init(enc_row_mt->mutex_, NULL);
858*77c1e3ccSAndroid Build Coastguard Worker     }
859*77c1e3ccSAndroid Build Coastguard Worker     if (enc_row_mt->cond_ == NULL) {
860*77c1e3ccSAndroid Build Coastguard Worker       CHECK_MEM_ERROR(cm, enc_row_mt->cond_,
861*77c1e3ccSAndroid Build Coastguard Worker                       aom_malloc(sizeof(*(enc_row_mt->cond_))));
862*77c1e3ccSAndroid Build Coastguard Worker       if (enc_row_mt->cond_) pthread_cond_init(enc_row_mt->cond_, NULL);
863*77c1e3ccSAndroid Build Coastguard Worker     }
864*77c1e3ccSAndroid Build Coastguard Worker   }
865*77c1e3ccSAndroid Build Coastguard Worker 
866*77c1e3ccSAndroid Build Coastguard Worker   if (!is_first_pass) {
867*77c1e3ccSAndroid Build Coastguard Worker     // Initialize global motion MT object.
868*77c1e3ccSAndroid Build Coastguard Worker     AV1GlobalMotionSync *gm_sync = &mt_info->gm_sync;
869*77c1e3ccSAndroid Build Coastguard Worker     if (gm_sync->mutex_ == NULL) {
870*77c1e3ccSAndroid Build Coastguard Worker       CHECK_MEM_ERROR(cm, gm_sync->mutex_,
871*77c1e3ccSAndroid Build Coastguard Worker                       aom_malloc(sizeof(*(gm_sync->mutex_))));
872*77c1e3ccSAndroid Build Coastguard Worker       if (gm_sync->mutex_) pthread_mutex_init(gm_sync->mutex_, NULL);
873*77c1e3ccSAndroid Build Coastguard Worker     }
874*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
875*77c1e3ccSAndroid Build Coastguard Worker     // Initialize temporal filtering MT object.
876*77c1e3ccSAndroid Build Coastguard Worker     AV1TemporalFilterSync *tf_sync = &mt_info->tf_sync;
877*77c1e3ccSAndroid Build Coastguard Worker     if (tf_sync->mutex_ == NULL) {
878*77c1e3ccSAndroid Build Coastguard Worker       CHECK_MEM_ERROR(cm, tf_sync->mutex_,
879*77c1e3ccSAndroid Build Coastguard Worker                       aom_malloc(sizeof(*tf_sync->mutex_)));
880*77c1e3ccSAndroid Build Coastguard Worker       if (tf_sync->mutex_) pthread_mutex_init(tf_sync->mutex_, NULL);
881*77c1e3ccSAndroid Build Coastguard Worker     }
882*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
883*77c1e3ccSAndroid Build Coastguard Worker         // Initialize CDEF MT object.
884*77c1e3ccSAndroid Build Coastguard Worker     AV1CdefSync *cdef_sync = &mt_info->cdef_sync;
885*77c1e3ccSAndroid Build Coastguard Worker     if (cdef_sync->mutex_ == NULL) {
886*77c1e3ccSAndroid Build Coastguard Worker       CHECK_MEM_ERROR(cm, cdef_sync->mutex_,
887*77c1e3ccSAndroid Build Coastguard Worker                       aom_malloc(sizeof(*(cdef_sync->mutex_))));
888*77c1e3ccSAndroid Build Coastguard Worker       if (cdef_sync->mutex_) pthread_mutex_init(cdef_sync->mutex_, NULL);
889*77c1e3ccSAndroid Build Coastguard Worker     }
890*77c1e3ccSAndroid Build Coastguard Worker 
891*77c1e3ccSAndroid Build Coastguard Worker     // Initialize loop filter MT object.
892*77c1e3ccSAndroid Build Coastguard Worker     AV1LfSync *lf_sync = &mt_info->lf_row_sync;
893*77c1e3ccSAndroid Build Coastguard Worker     // Number of superblock rows
894*77c1e3ccSAndroid Build Coastguard Worker     const int sb_rows =
895*77c1e3ccSAndroid Build Coastguard Worker         CEIL_POWER_OF_TWO(cm->height >> MI_SIZE_LOG2, MAX_MIB_SIZE_LOG2);
896*77c1e3ccSAndroid Build Coastguard Worker     PrimaryMultiThreadInfo *const p_mt_info = &cpi->ppi->p_mt_info;
897*77c1e3ccSAndroid Build Coastguard Worker     int num_lf_workers = av1_get_num_mod_workers_for_alloc(p_mt_info, MOD_LPF);
898*77c1e3ccSAndroid Build Coastguard Worker 
899*77c1e3ccSAndroid Build Coastguard Worker     if (!lf_sync->sync_range || sb_rows != lf_sync->rows ||
900*77c1e3ccSAndroid Build Coastguard Worker         num_lf_workers > lf_sync->num_workers) {
901*77c1e3ccSAndroid Build Coastguard Worker       av1_loop_filter_dealloc(lf_sync);
902*77c1e3ccSAndroid Build Coastguard Worker       av1_loop_filter_alloc(lf_sync, cm, sb_rows, cm->width, num_lf_workers);
903*77c1e3ccSAndroid Build Coastguard Worker     }
904*77c1e3ccSAndroid Build Coastguard Worker 
905*77c1e3ccSAndroid Build Coastguard Worker     // Initialize tpl MT object.
906*77c1e3ccSAndroid Build Coastguard Worker     AV1TplRowMultiThreadInfo *tpl_row_mt = &mt_info->tpl_row_mt;
907*77c1e3ccSAndroid Build Coastguard Worker     if (tpl_row_mt->mutex_ == NULL) {
908*77c1e3ccSAndroid Build Coastguard Worker       CHECK_MEM_ERROR(cm, tpl_row_mt->mutex_,
909*77c1e3ccSAndroid Build Coastguard Worker                       aom_malloc(sizeof(*(tpl_row_mt->mutex_))));
910*77c1e3ccSAndroid Build Coastguard Worker       if (tpl_row_mt->mutex_) pthread_mutex_init(tpl_row_mt->mutex_, NULL);
911*77c1e3ccSAndroid Build Coastguard Worker     }
912*77c1e3ccSAndroid Build Coastguard Worker 
913*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
914*77c1e3ccSAndroid Build Coastguard Worker     if (is_restoration_used(cm)) {
915*77c1e3ccSAndroid Build Coastguard Worker       // Initialize loop restoration MT object.
916*77c1e3ccSAndroid Build Coastguard Worker       AV1LrSync *lr_sync = &mt_info->lr_row_sync;
917*77c1e3ccSAndroid Build Coastguard Worker       int rst_unit_size = cpi->sf.lpf_sf.min_lr_unit_size;
918*77c1e3ccSAndroid Build Coastguard Worker       int num_rows_lr = av1_lr_count_units(rst_unit_size, cm->height);
919*77c1e3ccSAndroid Build Coastguard Worker       int num_lr_workers = av1_get_num_mod_workers_for_alloc(p_mt_info, MOD_LR);
920*77c1e3ccSAndroid Build Coastguard Worker       if (!lr_sync->sync_range || num_rows_lr > lr_sync->rows ||
921*77c1e3ccSAndroid Build Coastguard Worker           num_lr_workers > lr_sync->num_workers ||
922*77c1e3ccSAndroid Build Coastguard Worker           MAX_MB_PLANE > lr_sync->num_planes) {
923*77c1e3ccSAndroid Build Coastguard Worker         av1_loop_restoration_dealloc(lr_sync);
924*77c1e3ccSAndroid Build Coastguard Worker         av1_loop_restoration_alloc(lr_sync, cm, num_lr_workers, num_rows_lr,
925*77c1e3ccSAndroid Build Coastguard Worker                                    MAX_MB_PLANE, cm->width);
926*77c1e3ccSAndroid Build Coastguard Worker       }
927*77c1e3ccSAndroid Build Coastguard Worker     }
928*77c1e3ccSAndroid Build Coastguard Worker #endif
929*77c1e3ccSAndroid Build Coastguard Worker 
930*77c1e3ccSAndroid Build Coastguard Worker     // Initialization of pack bitstream MT object.
931*77c1e3ccSAndroid Build Coastguard Worker     AV1EncPackBSSync *pack_bs_sync = &mt_info->pack_bs_sync;
932*77c1e3ccSAndroid Build Coastguard Worker     if (pack_bs_sync->mutex_ == NULL) {
933*77c1e3ccSAndroid Build Coastguard Worker       CHECK_MEM_ERROR(cm, pack_bs_sync->mutex_,
934*77c1e3ccSAndroid Build Coastguard Worker                       aom_malloc(sizeof(*pack_bs_sync->mutex_)));
935*77c1e3ccSAndroid Build Coastguard Worker       if (pack_bs_sync->mutex_) pthread_mutex_init(pack_bs_sync->mutex_, NULL);
936*77c1e3ccSAndroid Build Coastguard Worker     }
937*77c1e3ccSAndroid Build Coastguard Worker   }
938*77c1e3ccSAndroid Build Coastguard Worker   cm->error->setjmp = 0;
939*77c1e3ccSAndroid Build Coastguard Worker }
940*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
941*77c1e3ccSAndroid Build Coastguard Worker 
942*77c1e3ccSAndroid Build Coastguard Worker // Computes the number of workers to be considered while allocating memory for a
943*77c1e3ccSAndroid Build Coastguard Worker // multi-threaded module under FPMT.
av1_get_num_mod_workers_for_alloc(const PrimaryMultiThreadInfo * p_mt_info,MULTI_THREADED_MODULES mod_name)944*77c1e3ccSAndroid Build Coastguard Worker int av1_get_num_mod_workers_for_alloc(const PrimaryMultiThreadInfo *p_mt_info,
945*77c1e3ccSAndroid Build Coastguard Worker                                       MULTI_THREADED_MODULES mod_name) {
946*77c1e3ccSAndroid Build Coastguard Worker   int num_mod_workers = p_mt_info->num_mod_workers[mod_name];
947*77c1e3ccSAndroid Build Coastguard Worker   if (p_mt_info->num_mod_workers[MOD_FRAME_ENC] > 1) {
948*77c1e3ccSAndroid Build Coastguard Worker     // TODO(anyone): Change num_mod_workers to num_mod_workers[MOD_FRAME_ENC].
949*77c1e3ccSAndroid Build Coastguard Worker     // As frame parallel jobs will only perform multi-threading for the encode
950*77c1e3ccSAndroid Build Coastguard Worker     // stage, we can limit the allocations according to num_enc_workers per
951*77c1e3ccSAndroid Build Coastguard Worker     // frame parallel encode(a.k.a num_mod_workers[MOD_FRAME_ENC]).
952*77c1e3ccSAndroid Build Coastguard Worker     num_mod_workers = p_mt_info->num_workers;
953*77c1e3ccSAndroid Build Coastguard Worker   }
954*77c1e3ccSAndroid Build Coastguard Worker   return num_mod_workers;
955*77c1e3ccSAndroid Build Coastguard Worker }
956*77c1e3ccSAndroid Build Coastguard Worker 
av1_init_tile_thread_data(AV1_PRIMARY * ppi,int is_first_pass)957*77c1e3ccSAndroid Build Coastguard Worker void av1_init_tile_thread_data(AV1_PRIMARY *ppi, int is_first_pass) {
958*77c1e3ccSAndroid Build Coastguard Worker   PrimaryMultiThreadInfo *const p_mt_info = &ppi->p_mt_info;
959*77c1e3ccSAndroid Build Coastguard Worker 
960*77c1e3ccSAndroid Build Coastguard Worker   assert(p_mt_info->workers != NULL);
961*77c1e3ccSAndroid Build Coastguard Worker   assert(p_mt_info->tile_thr_data != NULL);
962*77c1e3ccSAndroid Build Coastguard Worker 
963*77c1e3ccSAndroid Build Coastguard Worker   int num_workers = p_mt_info->num_workers;
964*77c1e3ccSAndroid Build Coastguard Worker   int num_enc_workers = av1_get_num_mod_workers_for_alloc(p_mt_info, MOD_ENC);
965*77c1e3ccSAndroid Build Coastguard Worker   assert(num_enc_workers <= num_workers);
966*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
967*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *const thread_data = &p_mt_info->tile_thr_data[i];
968*77c1e3ccSAndroid Build Coastguard Worker 
969*77c1e3ccSAndroid Build Coastguard Worker     if (i > 0) {
970*77c1e3ccSAndroid Build Coastguard Worker       // Allocate thread data.
971*77c1e3ccSAndroid Build Coastguard Worker       ThreadData *td;
972*77c1e3ccSAndroid Build Coastguard Worker       AOM_CHECK_MEM_ERROR(&ppi->error, td, aom_memalign(32, sizeof(*td)));
973*77c1e3ccSAndroid Build Coastguard Worker       av1_zero(*td);
974*77c1e3ccSAndroid Build Coastguard Worker       thread_data->original_td = thread_data->td = td;
975*77c1e3ccSAndroid Build Coastguard Worker 
976*77c1e3ccSAndroid Build Coastguard Worker       // Set up shared coeff buffers.
977*77c1e3ccSAndroid Build Coastguard Worker       av1_setup_shared_coeff_buffer(&ppi->seq_params, &td->shared_coeff_buf,
978*77c1e3ccSAndroid Build Coastguard Worker                                     &ppi->error);
979*77c1e3ccSAndroid Build Coastguard Worker       AOM_CHECK_MEM_ERROR(&ppi->error, td->tmp_conv_dst,
980*77c1e3ccSAndroid Build Coastguard Worker                           aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE *
981*77c1e3ccSAndroid Build Coastguard Worker                                                sizeof(*td->tmp_conv_dst)));
982*77c1e3ccSAndroid Build Coastguard Worker 
983*77c1e3ccSAndroid Build Coastguard Worker       if (i < p_mt_info->num_mod_workers[MOD_FP]) {
984*77c1e3ccSAndroid Build Coastguard Worker         // Set up firstpass PICK_MODE_CONTEXT.
985*77c1e3ccSAndroid Build Coastguard Worker         td->firstpass_ctx =
986*77c1e3ccSAndroid Build Coastguard Worker             av1_alloc_pmc(ppi->cpi, BLOCK_16X16, &td->shared_coeff_buf);
987*77c1e3ccSAndroid Build Coastguard Worker         if (!td->firstpass_ctx)
988*77c1e3ccSAndroid Build Coastguard Worker           aom_internal_error(&ppi->error, AOM_CODEC_MEM_ERROR,
989*77c1e3ccSAndroid Build Coastguard Worker                              "Failed to allocate PICK_MODE_CONTEXT");
990*77c1e3ccSAndroid Build Coastguard Worker       }
991*77c1e3ccSAndroid Build Coastguard Worker 
992*77c1e3ccSAndroid Build Coastguard Worker       if (!is_first_pass && i < num_enc_workers) {
993*77c1e3ccSAndroid Build Coastguard Worker         // Set up sms_tree.
994*77c1e3ccSAndroid Build Coastguard Worker         if (av1_setup_sms_tree(ppi->cpi, td)) {
995*77c1e3ccSAndroid Build Coastguard Worker           aom_internal_error(&ppi->error, AOM_CODEC_MEM_ERROR,
996*77c1e3ccSAndroid Build Coastguard Worker                              "Failed to allocate SMS tree");
997*77c1e3ccSAndroid Build Coastguard Worker         }
998*77c1e3ccSAndroid Build Coastguard Worker 
999*77c1e3ccSAndroid Build Coastguard Worker         for (int x = 0; x < 2; x++)
1000*77c1e3ccSAndroid Build Coastguard Worker           for (int y = 0; y < 2; y++)
1001*77c1e3ccSAndroid Build Coastguard Worker             AOM_CHECK_MEM_ERROR(
1002*77c1e3ccSAndroid Build Coastguard Worker                 &ppi->error, td->hash_value_buffer[x][y],
1003*77c1e3ccSAndroid Build Coastguard Worker                 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
1004*77c1e3ccSAndroid Build Coastguard Worker                                        sizeof(*td->hash_value_buffer[0][0])));
1005*77c1e3ccSAndroid Build Coastguard Worker 
1006*77c1e3ccSAndroid Build Coastguard Worker         // Allocate frame counters in thread data.
1007*77c1e3ccSAndroid Build Coastguard Worker         AOM_CHECK_MEM_ERROR(&ppi->error, td->counts,
1008*77c1e3ccSAndroid Build Coastguard Worker                             aom_calloc(1, sizeof(*td->counts)));
1009*77c1e3ccSAndroid Build Coastguard Worker 
1010*77c1e3ccSAndroid Build Coastguard Worker         // Allocate buffers used by palette coding mode.
1011*77c1e3ccSAndroid Build Coastguard Worker         AOM_CHECK_MEM_ERROR(&ppi->error, td->palette_buffer,
1012*77c1e3ccSAndroid Build Coastguard Worker                             aom_memalign(16, sizeof(*td->palette_buffer)));
1013*77c1e3ccSAndroid Build Coastguard Worker 
1014*77c1e3ccSAndroid Build Coastguard Worker         // The buffers 'tmp_pred_bufs[]', 'comp_rd_buffer' and 'obmc_buffer' are
1015*77c1e3ccSAndroid Build Coastguard Worker         // used in inter frames to store intermediate inter mode prediction
1016*77c1e3ccSAndroid Build Coastguard Worker         // results and are not required for allintra encoding mode. Hence, the
1017*77c1e3ccSAndroid Build Coastguard Worker         // memory allocations for these buffers are avoided for allintra
1018*77c1e3ccSAndroid Build Coastguard Worker         // encoding mode.
1019*77c1e3ccSAndroid Build Coastguard Worker         if (ppi->cpi->oxcf.kf_cfg.key_freq_max != 0) {
1020*77c1e3ccSAndroid Build Coastguard Worker           alloc_obmc_buffers(&td->obmc_buffer, &ppi->error);
1021*77c1e3ccSAndroid Build Coastguard Worker 
1022*77c1e3ccSAndroid Build Coastguard Worker           alloc_compound_type_rd_buffers(&ppi->error, &td->comp_rd_buffer);
1023*77c1e3ccSAndroid Build Coastguard Worker 
1024*77c1e3ccSAndroid Build Coastguard Worker           for (int j = 0; j < 2; ++j) {
1025*77c1e3ccSAndroid Build Coastguard Worker             AOM_CHECK_MEM_ERROR(
1026*77c1e3ccSAndroid Build Coastguard Worker                 &ppi->error, td->tmp_pred_bufs[j],
1027*77c1e3ccSAndroid Build Coastguard Worker                 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
1028*77c1e3ccSAndroid Build Coastguard Worker                                      sizeof(*td->tmp_pred_bufs[j])));
1029*77c1e3ccSAndroid Build Coastguard Worker           }
1030*77c1e3ccSAndroid Build Coastguard Worker         }
1031*77c1e3ccSAndroid Build Coastguard Worker 
1032*77c1e3ccSAndroid Build Coastguard Worker         if (is_gradient_caching_for_hog_enabled(ppi->cpi)) {
1033*77c1e3ccSAndroid Build Coastguard Worker           const int plane_types = PLANE_TYPES >> ppi->seq_params.monochrome;
1034*77c1e3ccSAndroid Build Coastguard Worker           AOM_CHECK_MEM_ERROR(&ppi->error, td->pixel_gradient_info,
1035*77c1e3ccSAndroid Build Coastguard Worker                               aom_malloc(sizeof(*td->pixel_gradient_info) *
1036*77c1e3ccSAndroid Build Coastguard Worker                                          plane_types * MAX_SB_SQUARE));
1037*77c1e3ccSAndroid Build Coastguard Worker         }
1038*77c1e3ccSAndroid Build Coastguard Worker 
1039*77c1e3ccSAndroid Build Coastguard Worker         if (is_src_var_for_4x4_sub_blocks_caching_enabled(ppi->cpi)) {
1040*77c1e3ccSAndroid Build Coastguard Worker           const BLOCK_SIZE sb_size = ppi->cpi->common.seq_params->sb_size;
1041*77c1e3ccSAndroid Build Coastguard Worker           const int mi_count_in_sb =
1042*77c1e3ccSAndroid Build Coastguard Worker               mi_size_wide[sb_size] * mi_size_high[sb_size];
1043*77c1e3ccSAndroid Build Coastguard Worker 
1044*77c1e3ccSAndroid Build Coastguard Worker           AOM_CHECK_MEM_ERROR(
1045*77c1e3ccSAndroid Build Coastguard Worker               &ppi->error, td->src_var_info_of_4x4_sub_blocks,
1046*77c1e3ccSAndroid Build Coastguard Worker               aom_malloc(sizeof(*td->src_var_info_of_4x4_sub_blocks) *
1047*77c1e3ccSAndroid Build Coastguard Worker                          mi_count_in_sb));
1048*77c1e3ccSAndroid Build Coastguard Worker         }
1049*77c1e3ccSAndroid Build Coastguard Worker 
1050*77c1e3ccSAndroid Build Coastguard Worker         if (ppi->cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION) {
1051*77c1e3ccSAndroid Build Coastguard Worker           const int num_64x64_blocks =
1052*77c1e3ccSAndroid Build Coastguard Worker               (ppi->seq_params.sb_size == BLOCK_64X64) ? 1 : 4;
1053*77c1e3ccSAndroid Build Coastguard Worker           AOM_CHECK_MEM_ERROR(
1054*77c1e3ccSAndroid Build Coastguard Worker               &ppi->error, td->vt64x64,
1055*77c1e3ccSAndroid Build Coastguard Worker               aom_malloc(sizeof(*td->vt64x64) * num_64x64_blocks));
1056*77c1e3ccSAndroid Build Coastguard Worker         }
1057*77c1e3ccSAndroid Build Coastguard Worker       }
1058*77c1e3ccSAndroid Build Coastguard Worker     }
1059*77c1e3ccSAndroid Build Coastguard Worker 
1060*77c1e3ccSAndroid Build Coastguard Worker     if (!is_first_pass && ppi->cpi->oxcf.row_mt == 1 && i < num_enc_workers) {
1061*77c1e3ccSAndroid Build Coastguard Worker       if (i == 0) {
1062*77c1e3ccSAndroid Build Coastguard Worker         for (int j = 0; j < ppi->num_fp_contexts; j++) {
1063*77c1e3ccSAndroid Build Coastguard Worker           AOM_CHECK_MEM_ERROR(&ppi->error, ppi->parallel_cpi[j]->td.tctx,
1064*77c1e3ccSAndroid Build Coastguard Worker                               (FRAME_CONTEXT *)aom_memalign(
1065*77c1e3ccSAndroid Build Coastguard Worker                                   16, sizeof(*ppi->parallel_cpi[j]->td.tctx)));
1066*77c1e3ccSAndroid Build Coastguard Worker         }
1067*77c1e3ccSAndroid Build Coastguard Worker       } else {
1068*77c1e3ccSAndroid Build Coastguard Worker         AOM_CHECK_MEM_ERROR(
1069*77c1e3ccSAndroid Build Coastguard Worker             &ppi->error, thread_data->td->tctx,
1070*77c1e3ccSAndroid Build Coastguard Worker             (FRAME_CONTEXT *)aom_memalign(16, sizeof(*thread_data->td->tctx)));
1071*77c1e3ccSAndroid Build Coastguard Worker       }
1072*77c1e3ccSAndroid Build Coastguard Worker     }
1073*77c1e3ccSAndroid Build Coastguard Worker   }
1074*77c1e3ccSAndroid Build Coastguard Worker 
1075*77c1e3ccSAndroid Build Coastguard Worker   // Record the number of workers in encode stage multi-threading for which
1076*77c1e3ccSAndroid Build Coastguard Worker   // allocation is done.
1077*77c1e3ccSAndroid Build Coastguard Worker   p_mt_info->prev_num_enc_workers = num_enc_workers;
1078*77c1e3ccSAndroid Build Coastguard Worker }
1079*77c1e3ccSAndroid Build Coastguard Worker 
av1_create_workers(AV1_PRIMARY * ppi,int num_workers)1080*77c1e3ccSAndroid Build Coastguard Worker void av1_create_workers(AV1_PRIMARY *ppi, int num_workers) {
1081*77c1e3ccSAndroid Build Coastguard Worker   PrimaryMultiThreadInfo *const p_mt_info = &ppi->p_mt_info;
1082*77c1e3ccSAndroid Build Coastguard Worker   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
1083*77c1e3ccSAndroid Build Coastguard Worker   assert(p_mt_info->num_workers == 0);
1084*77c1e3ccSAndroid Build Coastguard Worker 
1085*77c1e3ccSAndroid Build Coastguard Worker   AOM_CHECK_MEM_ERROR(&ppi->error, p_mt_info->workers,
1086*77c1e3ccSAndroid Build Coastguard Worker                       aom_malloc(num_workers * sizeof(*p_mt_info->workers)));
1087*77c1e3ccSAndroid Build Coastguard Worker 
1088*77c1e3ccSAndroid Build Coastguard Worker   AOM_CHECK_MEM_ERROR(
1089*77c1e3ccSAndroid Build Coastguard Worker       &ppi->error, p_mt_info->tile_thr_data,
1090*77c1e3ccSAndroid Build Coastguard Worker       aom_calloc(num_workers, sizeof(*p_mt_info->tile_thr_data)));
1091*77c1e3ccSAndroid Build Coastguard Worker 
1092*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < num_workers; ++i) {
1093*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *const worker = &p_mt_info->workers[i];
1094*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *const thread_data = &p_mt_info->tile_thr_data[i];
1095*77c1e3ccSAndroid Build Coastguard Worker 
1096*77c1e3ccSAndroid Build Coastguard Worker     winterface->init(worker);
1097*77c1e3ccSAndroid Build Coastguard Worker     worker->thread_name = "aom enc worker";
1098*77c1e3ccSAndroid Build Coastguard Worker 
1099*77c1e3ccSAndroid Build Coastguard Worker     thread_data->thread_id = i;
1100*77c1e3ccSAndroid Build Coastguard Worker     // Set the starting tile for each thread.
1101*77c1e3ccSAndroid Build Coastguard Worker     thread_data->start = i;
1102*77c1e3ccSAndroid Build Coastguard Worker 
1103*77c1e3ccSAndroid Build Coastguard Worker     if (i > 0) {
1104*77c1e3ccSAndroid Build Coastguard Worker       // Create threads
1105*77c1e3ccSAndroid Build Coastguard Worker       if (!winterface->reset(worker))
1106*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(&ppi->error, AOM_CODEC_ERROR,
1107*77c1e3ccSAndroid Build Coastguard Worker                            "Tile encoder thread creation failed");
1108*77c1e3ccSAndroid Build Coastguard Worker     }
1109*77c1e3ccSAndroid Build Coastguard Worker     winterface->sync(worker);
1110*77c1e3ccSAndroid Build Coastguard Worker 
1111*77c1e3ccSAndroid Build Coastguard Worker     ++p_mt_info->num_workers;
1112*77c1e3ccSAndroid Build Coastguard Worker   }
1113*77c1e3ccSAndroid Build Coastguard Worker }
1114*77c1e3ccSAndroid Build Coastguard Worker 
1115*77c1e3ccSAndroid Build Coastguard Worker // This function will change the state and free the mutex of corresponding
1116*77c1e3ccSAndroid Build Coastguard Worker // workers and terminate the object. The object can not be re-used unless a call
1117*77c1e3ccSAndroid Build Coastguard Worker // to reset() is made.
av1_terminate_workers(AV1_PRIMARY * ppi)1118*77c1e3ccSAndroid Build Coastguard Worker void av1_terminate_workers(AV1_PRIMARY *ppi) {
1119*77c1e3ccSAndroid Build Coastguard Worker   PrimaryMultiThreadInfo *const p_mt_info = &ppi->p_mt_info;
1120*77c1e3ccSAndroid Build Coastguard Worker   for (int t = 0; t < p_mt_info->num_workers; ++t) {
1121*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *const worker = &p_mt_info->workers[t];
1122*77c1e3ccSAndroid Build Coastguard Worker     aom_get_worker_interface()->end(worker);
1123*77c1e3ccSAndroid Build Coastguard Worker   }
1124*77c1e3ccSAndroid Build Coastguard Worker }
1125*77c1e3ccSAndroid Build Coastguard Worker 
1126*77c1e3ccSAndroid Build Coastguard Worker // This function returns 1 if frame parallel encode is supported for
1127*77c1e3ccSAndroid Build Coastguard Worker // the current configuration. Returns 0 otherwise.
is_fpmt_config(const AV1_PRIMARY * ppi,const AV1EncoderConfig * oxcf)1128*77c1e3ccSAndroid Build Coastguard Worker static inline int is_fpmt_config(const AV1_PRIMARY *ppi,
1129*77c1e3ccSAndroid Build Coastguard Worker                                  const AV1EncoderConfig *oxcf) {
1130*77c1e3ccSAndroid Build Coastguard Worker   // FPMT is enabled for AOM_Q and AOM_VBR.
1131*77c1e3ccSAndroid Build Coastguard Worker   // TODO(Tarun): Test and enable resize config.
1132*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->rc_cfg.mode == AOM_CBR || oxcf->rc_cfg.mode == AOM_CQ) {
1133*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1134*77c1e3ccSAndroid Build Coastguard Worker   }
1135*77c1e3ccSAndroid Build Coastguard Worker   if (ppi->use_svc) {
1136*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1137*77c1e3ccSAndroid Build Coastguard Worker   }
1138*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->tile_cfg.enable_large_scale_tile) {
1139*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1140*77c1e3ccSAndroid Build Coastguard Worker   }
1141*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->dec_model_cfg.timing_info_present) {
1142*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1143*77c1e3ccSAndroid Build Coastguard Worker   }
1144*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->mode != GOOD) {
1145*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1146*77c1e3ccSAndroid Build Coastguard Worker   }
1147*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->tool_cfg.error_resilient_mode) {
1148*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1149*77c1e3ccSAndroid Build Coastguard Worker   }
1150*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->resize_cfg.resize_mode) {
1151*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1152*77c1e3ccSAndroid Build Coastguard Worker   }
1153*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->pass != AOM_RC_SECOND_PASS) {
1154*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1155*77c1e3ccSAndroid Build Coastguard Worker   }
1156*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->max_threads < 2) {
1157*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1158*77c1e3ccSAndroid Build Coastguard Worker   }
1159*77c1e3ccSAndroid Build Coastguard Worker   if (!oxcf->fp_mt) {
1160*77c1e3ccSAndroid Build Coastguard Worker     return 0;
1161*77c1e3ccSAndroid Build Coastguard Worker   }
1162*77c1e3ccSAndroid Build Coastguard Worker 
1163*77c1e3ccSAndroid Build Coastguard Worker   return 1;
1164*77c1e3ccSAndroid Build Coastguard Worker }
1165*77c1e3ccSAndroid Build Coastguard Worker 
av1_check_fpmt_config(AV1_PRIMARY * const ppi,const AV1EncoderConfig * const oxcf)1166*77c1e3ccSAndroid Build Coastguard Worker int av1_check_fpmt_config(AV1_PRIMARY *const ppi,
1167*77c1e3ccSAndroid Build Coastguard Worker                           const AV1EncoderConfig *const oxcf) {
1168*77c1e3ccSAndroid Build Coastguard Worker   if (is_fpmt_config(ppi, oxcf)) return 1;
1169*77c1e3ccSAndroid Build Coastguard Worker   // Reset frame parallel configuration for unsupported config
1170*77c1e3ccSAndroid Build Coastguard Worker   if (ppi->num_fp_contexts > 1) {
1171*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 1; i < ppi->num_fp_contexts; i++) {
1172*77c1e3ccSAndroid Build Coastguard Worker       // Release the previously-used frame-buffer
1173*77c1e3ccSAndroid Build Coastguard Worker       if (ppi->parallel_cpi[i]->common.cur_frame != NULL) {
1174*77c1e3ccSAndroid Build Coastguard Worker         --ppi->parallel_cpi[i]->common.cur_frame->ref_count;
1175*77c1e3ccSAndroid Build Coastguard Worker         ppi->parallel_cpi[i]->common.cur_frame = NULL;
1176*77c1e3ccSAndroid Build Coastguard Worker       }
1177*77c1e3ccSAndroid Build Coastguard Worker     }
1178*77c1e3ccSAndroid Build Coastguard Worker 
1179*77c1e3ccSAndroid Build Coastguard Worker     int cur_gf_index = ppi->cpi->gf_frame_index;
1180*77c1e3ccSAndroid Build Coastguard Worker     int reset_size = AOMMAX(0, ppi->gf_group.size - cur_gf_index);
1181*77c1e3ccSAndroid Build Coastguard Worker     av1_zero_array(&ppi->gf_group.frame_parallel_level[cur_gf_index],
1182*77c1e3ccSAndroid Build Coastguard Worker                    reset_size);
1183*77c1e3ccSAndroid Build Coastguard Worker     av1_zero_array(&ppi->gf_group.is_frame_non_ref[cur_gf_index], reset_size);
1184*77c1e3ccSAndroid Build Coastguard Worker     av1_zero_array(&ppi->gf_group.src_offset[cur_gf_index], reset_size);
1185*77c1e3ccSAndroid Build Coastguard Worker     memset(&ppi->gf_group.skip_frame_refresh[cur_gf_index][0], INVALID_IDX,
1186*77c1e3ccSAndroid Build Coastguard Worker            sizeof(ppi->gf_group.skip_frame_refresh[cur_gf_index][0]) *
1187*77c1e3ccSAndroid Build Coastguard Worker                reset_size * REF_FRAMES);
1188*77c1e3ccSAndroid Build Coastguard Worker     memset(&ppi->gf_group.skip_frame_as_ref[cur_gf_index], INVALID_IDX,
1189*77c1e3ccSAndroid Build Coastguard Worker            sizeof(ppi->gf_group.skip_frame_as_ref[cur_gf_index]) * reset_size);
1190*77c1e3ccSAndroid Build Coastguard Worker     ppi->num_fp_contexts = 1;
1191*77c1e3ccSAndroid Build Coastguard Worker   }
1192*77c1e3ccSAndroid Build Coastguard Worker   return 0;
1193*77c1e3ccSAndroid Build Coastguard Worker }
1194*77c1e3ccSAndroid Build Coastguard Worker 
1195*77c1e3ccSAndroid Build Coastguard Worker // A large value for threads used to compute the max num_enc_workers
1196*77c1e3ccSAndroid Build Coastguard Worker // possible for each resolution.
1197*77c1e3ccSAndroid Build Coastguard Worker #define MAX_THREADS 100
1198*77c1e3ccSAndroid Build Coastguard Worker 
1199*77c1e3ccSAndroid Build Coastguard Worker // Computes the max number of enc workers possible for each resolution.
compute_max_num_enc_workers(CommonModeInfoParams * const mi_params,int mib_size_log2)1200*77c1e3ccSAndroid Build Coastguard Worker static inline int compute_max_num_enc_workers(
1201*77c1e3ccSAndroid Build Coastguard Worker     CommonModeInfoParams *const mi_params, int mib_size_log2) {
1202*77c1e3ccSAndroid Build Coastguard Worker   int num_sb_rows = CEIL_POWER_OF_TWO(mi_params->mi_rows, mib_size_log2);
1203*77c1e3ccSAndroid Build Coastguard Worker   int num_sb_cols = CEIL_POWER_OF_TWO(mi_params->mi_cols, mib_size_log2);
1204*77c1e3ccSAndroid Build Coastguard Worker 
1205*77c1e3ccSAndroid Build Coastguard Worker   return AOMMIN((num_sb_cols + 1) >> 1, num_sb_rows);
1206*77c1e3ccSAndroid Build Coastguard Worker }
1207*77c1e3ccSAndroid Build Coastguard Worker 
1208*77c1e3ccSAndroid Build Coastguard Worker // Computes the number of frame parallel(fp) contexts to be created
1209*77c1e3ccSAndroid Build Coastguard Worker // based on the number of max_enc_workers.
av1_compute_num_fp_contexts(AV1_PRIMARY * ppi,AV1EncoderConfig * oxcf)1210*77c1e3ccSAndroid Build Coastguard Worker int av1_compute_num_fp_contexts(AV1_PRIMARY *ppi, AV1EncoderConfig *oxcf) {
1211*77c1e3ccSAndroid Build Coastguard Worker   ppi->p_mt_info.num_mod_workers[MOD_FRAME_ENC] = 0;
1212*77c1e3ccSAndroid Build Coastguard Worker   if (!av1_check_fpmt_config(ppi, oxcf)) {
1213*77c1e3ccSAndroid Build Coastguard Worker     return 1;
1214*77c1e3ccSAndroid Build Coastguard Worker   }
1215*77c1e3ccSAndroid Build Coastguard Worker   int max_num_enc_workers = compute_max_num_enc_workers(
1216*77c1e3ccSAndroid Build Coastguard Worker       &ppi->cpi->common.mi_params, ppi->cpi->common.seq_params->mib_size_log2);
1217*77c1e3ccSAndroid Build Coastguard Worker   // Scaling factors and rounding factors used to tune worker_per_frame
1218*77c1e3ccSAndroid Build Coastguard Worker   // computation.
1219*77c1e3ccSAndroid Build Coastguard Worker   int rounding_factor[2] = { 2, 4 };
1220*77c1e3ccSAndroid Build Coastguard Worker   int scaling_factor[2] = { 4, 8 };
1221*77c1e3ccSAndroid Build Coastguard Worker   int is_480p_or_lesser =
1222*77c1e3ccSAndroid Build Coastguard Worker       AOMMIN(oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height) <= 480;
1223*77c1e3ccSAndroid Build Coastguard Worker   int is_sb_64 = 0;
1224*77c1e3ccSAndroid Build Coastguard Worker   if (ppi->cpi != NULL)
1225*77c1e3ccSAndroid Build Coastguard Worker     is_sb_64 = ppi->cpi->common.seq_params->sb_size == BLOCK_64X64;
1226*77c1e3ccSAndroid Build Coastguard Worker   // A parallel frame encode has at least 1/4th the
1227*77c1e3ccSAndroid Build Coastguard Worker   // theoretical limit of max enc workers in default case. For resolutions
1228*77c1e3ccSAndroid Build Coastguard Worker   // larger than 480p, if SB size is 64x64, optimal performance is obtained with
1229*77c1e3ccSAndroid Build Coastguard Worker   // limit of 1/8.
1230*77c1e3ccSAndroid Build Coastguard Worker   int index = (!is_480p_or_lesser && is_sb_64) ? 1 : 0;
1231*77c1e3ccSAndroid Build Coastguard Worker   int workers_per_frame =
1232*77c1e3ccSAndroid Build Coastguard Worker       AOMMAX(1, (max_num_enc_workers + rounding_factor[index]) /
1233*77c1e3ccSAndroid Build Coastguard Worker                     scaling_factor[index]);
1234*77c1e3ccSAndroid Build Coastguard Worker   int max_threads = oxcf->max_threads;
1235*77c1e3ccSAndroid Build Coastguard Worker   int num_fp_contexts = max_threads / workers_per_frame;
1236*77c1e3ccSAndroid Build Coastguard Worker   // Based on empirical results, FPMT gains with multi-tile are significant when
1237*77c1e3ccSAndroid Build Coastguard Worker   // more parallel frames are available. Use FPMT with multi-tile encode only
1238*77c1e3ccSAndroid Build Coastguard Worker   // when sufficient threads are available for parallel encode of
1239*77c1e3ccSAndroid Build Coastguard Worker   // MAX_PARALLEL_FRAMES frames.
1240*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->tile_cfg.tile_columns > 0 || oxcf->tile_cfg.tile_rows > 0) {
1241*77c1e3ccSAndroid Build Coastguard Worker     if (num_fp_contexts < MAX_PARALLEL_FRAMES) num_fp_contexts = 1;
1242*77c1e3ccSAndroid Build Coastguard Worker   }
1243*77c1e3ccSAndroid Build Coastguard Worker 
1244*77c1e3ccSAndroid Build Coastguard Worker   num_fp_contexts = AOMMAX(1, AOMMIN(num_fp_contexts, MAX_PARALLEL_FRAMES));
1245*77c1e3ccSAndroid Build Coastguard Worker   // Limit recalculated num_fp_contexts to ppi->num_fp_contexts.
1246*77c1e3ccSAndroid Build Coastguard Worker   num_fp_contexts = (ppi->num_fp_contexts == 1)
1247*77c1e3ccSAndroid Build Coastguard Worker                         ? num_fp_contexts
1248*77c1e3ccSAndroid Build Coastguard Worker                         : AOMMIN(num_fp_contexts, ppi->num_fp_contexts);
1249*77c1e3ccSAndroid Build Coastguard Worker   if (num_fp_contexts > 1) {
1250*77c1e3ccSAndroid Build Coastguard Worker     ppi->p_mt_info.num_mod_workers[MOD_FRAME_ENC] =
1251*77c1e3ccSAndroid Build Coastguard Worker         AOMMIN(max_num_enc_workers * num_fp_contexts, oxcf->max_threads);
1252*77c1e3ccSAndroid Build Coastguard Worker   }
1253*77c1e3ccSAndroid Build Coastguard Worker   return num_fp_contexts;
1254*77c1e3ccSAndroid Build Coastguard Worker }
1255*77c1e3ccSAndroid Build Coastguard Worker 
1256*77c1e3ccSAndroid Build Coastguard Worker // 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)1257*77c1e3ccSAndroid Build Coastguard Worker static inline int compute_num_workers_per_frame(
1258*77c1e3ccSAndroid Build Coastguard Worker     const int num_workers, const int parallel_frame_count) {
1259*77c1e3ccSAndroid Build Coastguard Worker   // Number of level 2 workers per frame context (floor division).
1260*77c1e3ccSAndroid Build Coastguard Worker   int workers_per_frame = (num_workers / parallel_frame_count);
1261*77c1e3ccSAndroid Build Coastguard Worker   return workers_per_frame;
1262*77c1e3ccSAndroid Build Coastguard Worker }
1263*77c1e3ccSAndroid Build Coastguard Worker 
1264*77c1e3ccSAndroid Build Coastguard Worker static inline void restore_workers_after_fpmt(AV1_PRIMARY *ppi,
1265*77c1e3ccSAndroid Build Coastguard Worker                                               int parallel_frame_count,
1266*77c1e3ccSAndroid Build Coastguard Worker                                               int num_fpmt_workers_prepared);
1267*77c1e3ccSAndroid Build Coastguard Worker 
1268*77c1e3ccSAndroid Build Coastguard Worker // Prepare level 1 workers. This function is only called for
1269*77c1e3ccSAndroid Build Coastguard Worker // parallel_frame_count > 1. This function populates the mt_info structure of
1270*77c1e3ccSAndroid Build Coastguard Worker // frame level contexts appropriately by dividing the total number of available
1271*77c1e3ccSAndroid Build Coastguard Worker // workers amongst the frames as level 2 workers. It also populates the hook and
1272*77c1e3ccSAndroid Build Coastguard Worker // data members of level 1 workers.
prepare_fpmt_workers(AV1_PRIMARY * ppi,AV1_COMP_DATA * first_cpi_data,AVxWorkerHook hook,int parallel_frame_count)1273*77c1e3ccSAndroid Build Coastguard Worker static inline void prepare_fpmt_workers(AV1_PRIMARY *ppi,
1274*77c1e3ccSAndroid Build Coastguard Worker                                         AV1_COMP_DATA *first_cpi_data,
1275*77c1e3ccSAndroid Build Coastguard Worker                                         AVxWorkerHook hook,
1276*77c1e3ccSAndroid Build Coastguard Worker                                         int parallel_frame_count) {
1277*77c1e3ccSAndroid Build Coastguard Worker   assert(parallel_frame_count <= ppi->num_fp_contexts &&
1278*77c1e3ccSAndroid Build Coastguard Worker          parallel_frame_count > 1);
1279*77c1e3ccSAndroid Build Coastguard Worker 
1280*77c1e3ccSAndroid Build Coastguard Worker   PrimaryMultiThreadInfo *const p_mt_info = &ppi->p_mt_info;
1281*77c1e3ccSAndroid Build Coastguard Worker   int num_workers = p_mt_info->num_workers;
1282*77c1e3ccSAndroid Build Coastguard Worker 
1283*77c1e3ccSAndroid Build Coastguard Worker   volatile int frame_idx = 0;
1284*77c1e3ccSAndroid Build Coastguard Worker   volatile int i = 0;
1285*77c1e3ccSAndroid Build Coastguard Worker   while (i < num_workers) {
1286*77c1e3ccSAndroid Build Coastguard Worker     // Assign level 1 worker
1287*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *frame_worker = p_mt_info->p_workers[frame_idx] =
1288*77c1e3ccSAndroid Build Coastguard Worker         &p_mt_info->workers[i];
1289*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *cur_cpi = ppi->parallel_cpi[frame_idx];
1290*77c1e3ccSAndroid Build Coastguard Worker     MultiThreadInfo *mt_info = &cur_cpi->mt_info;
1291*77c1e3ccSAndroid Build Coastguard Worker     // This 'aom_internal_error_info' pointer is not derived from the local
1292*77c1e3ccSAndroid Build Coastguard Worker     // pointer ('AV1_COMMON *const cm') to silence the compiler warning
1293*77c1e3ccSAndroid Build Coastguard Worker     // "variable 'cm' might be clobbered by 'longjmp' or 'vfork' [-Wclobbered]".
1294*77c1e3ccSAndroid Build Coastguard Worker     struct aom_internal_error_info *const error = cur_cpi->common.error;
1295*77c1e3ccSAndroid Build Coastguard Worker 
1296*77c1e3ccSAndroid Build Coastguard Worker     // The jmp_buf is valid only within the scope of the function that calls
1297*77c1e3ccSAndroid Build Coastguard Worker     // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
1298*77c1e3ccSAndroid Build Coastguard Worker     // before it returns.
1299*77c1e3ccSAndroid Build Coastguard Worker     if (setjmp(error->jmp)) {
1300*77c1e3ccSAndroid Build Coastguard Worker       error->setjmp = 0;
1301*77c1e3ccSAndroid Build Coastguard Worker       restore_workers_after_fpmt(ppi, parallel_frame_count, i);
1302*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error_copy(&ppi->error, error);
1303*77c1e3ccSAndroid Build Coastguard Worker     }
1304*77c1e3ccSAndroid Build Coastguard Worker     error->setjmp = 1;
1305*77c1e3ccSAndroid Build Coastguard Worker 
1306*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMMON *const cm = &cur_cpi->common;
1307*77c1e3ccSAndroid Build Coastguard Worker     // Assign start of level 2 worker pool
1308*77c1e3ccSAndroid Build Coastguard Worker     mt_info->workers = &p_mt_info->workers[i];
1309*77c1e3ccSAndroid Build Coastguard Worker     mt_info->tile_thr_data = &p_mt_info->tile_thr_data[i];
1310*77c1e3ccSAndroid Build Coastguard Worker     // Assign number of workers for each frame in the parallel encode set.
1311*77c1e3ccSAndroid Build Coastguard Worker     mt_info->num_workers = compute_num_workers_per_frame(
1312*77c1e3ccSAndroid Build Coastguard Worker         num_workers - i, parallel_frame_count - frame_idx);
1313*77c1e3ccSAndroid Build Coastguard Worker     for (int j = MOD_FP; j < NUM_MT_MODULES; j++) {
1314*77c1e3ccSAndroid Build Coastguard Worker       mt_info->num_mod_workers[j] =
1315*77c1e3ccSAndroid Build Coastguard Worker           AOMMIN(mt_info->num_workers, p_mt_info->num_mod_workers[j]);
1316*77c1e3ccSAndroid Build Coastguard Worker     }
1317*77c1e3ccSAndroid Build Coastguard Worker     if (p_mt_info->cdef_worker != NULL) {
1318*77c1e3ccSAndroid Build Coastguard Worker       mt_info->cdef_worker = &p_mt_info->cdef_worker[i];
1319*77c1e3ccSAndroid Build Coastguard Worker 
1320*77c1e3ccSAndroid Build Coastguard Worker       // Back up the original cdef_worker pointers.
1321*77c1e3ccSAndroid Build Coastguard Worker       mt_info->restore_state_buf.cdef_srcbuf = mt_info->cdef_worker->srcbuf;
1322*77c1e3ccSAndroid Build Coastguard Worker       const int num_planes = av1_num_planes(cm);
1323*77c1e3ccSAndroid Build Coastguard Worker       for (int plane = 0; plane < num_planes; plane++)
1324*77c1e3ccSAndroid Build Coastguard Worker         mt_info->restore_state_buf.cdef_colbuf[plane] =
1325*77c1e3ccSAndroid Build Coastguard Worker             mt_info->cdef_worker->colbuf[plane];
1326*77c1e3ccSAndroid Build Coastguard Worker     }
1327*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1328*77c1e3ccSAndroid Build Coastguard Worker     if (is_restoration_used(cm)) {
1329*77c1e3ccSAndroid Build Coastguard Worker       // Back up the original LR buffers before update.
1330*77c1e3ccSAndroid Build Coastguard Worker       int idx = i + mt_info->num_workers - 1;
1331*77c1e3ccSAndroid Build Coastguard Worker       assert(idx < mt_info->lr_row_sync.num_workers);
1332*77c1e3ccSAndroid Build Coastguard Worker       mt_info->restore_state_buf.rst_tmpbuf =
1333*77c1e3ccSAndroid Build Coastguard Worker           mt_info->lr_row_sync.lrworkerdata[idx].rst_tmpbuf;
1334*77c1e3ccSAndroid Build Coastguard Worker       mt_info->restore_state_buf.rlbs =
1335*77c1e3ccSAndroid Build Coastguard Worker           mt_info->lr_row_sync.lrworkerdata[idx].rlbs;
1336*77c1e3ccSAndroid Build Coastguard Worker 
1337*77c1e3ccSAndroid Build Coastguard Worker       // Update LR buffers.
1338*77c1e3ccSAndroid Build Coastguard Worker       mt_info->lr_row_sync.lrworkerdata[idx].rst_tmpbuf = cm->rst_tmpbuf;
1339*77c1e3ccSAndroid Build Coastguard Worker       mt_info->lr_row_sync.lrworkerdata[idx].rlbs = cm->rlbs;
1340*77c1e3ccSAndroid Build Coastguard Worker     }
1341*77c1e3ccSAndroid Build Coastguard Worker #endif
1342*77c1e3ccSAndroid Build Coastguard Worker 
1343*77c1e3ccSAndroid Build Coastguard Worker     i += mt_info->num_workers;
1344*77c1e3ccSAndroid Build Coastguard Worker 
1345*77c1e3ccSAndroid Build Coastguard Worker     // At this stage, the thread specific CDEF buffers for the current frame's
1346*77c1e3ccSAndroid Build Coastguard Worker     // 'common' and 'cdef_sync' only need to be allocated. 'cdef_worker' has
1347*77c1e3ccSAndroid Build Coastguard Worker     // already been allocated across parallel frames.
1348*77c1e3ccSAndroid Build Coastguard Worker     av1_alloc_cdef_buffers(cm, &p_mt_info->cdef_worker, &mt_info->cdef_sync,
1349*77c1e3ccSAndroid Build Coastguard Worker                            p_mt_info->num_workers, 0);
1350*77c1e3ccSAndroid Build Coastguard Worker 
1351*77c1e3ccSAndroid Build Coastguard Worker     frame_worker->hook = hook;
1352*77c1e3ccSAndroid Build Coastguard Worker     frame_worker->data1 = cur_cpi;
1353*77c1e3ccSAndroid Build Coastguard Worker     frame_worker->data2 = (frame_idx == 0)
1354*77c1e3ccSAndroid Build Coastguard Worker                               ? first_cpi_data
1355*77c1e3ccSAndroid Build Coastguard Worker                               : &ppi->parallel_frames_data[frame_idx - 1];
1356*77c1e3ccSAndroid Build Coastguard Worker     frame_idx++;
1357*77c1e3ccSAndroid Build Coastguard Worker     error->setjmp = 0;
1358*77c1e3ccSAndroid Build Coastguard Worker   }
1359*77c1e3ccSAndroid Build Coastguard Worker   p_mt_info->p_num_workers = parallel_frame_count;
1360*77c1e3ccSAndroid Build Coastguard Worker }
1361*77c1e3ccSAndroid Build Coastguard Worker 
1362*77c1e3ccSAndroid Build Coastguard Worker // Launch level 1 workers to perform frame parallel encode.
launch_fpmt_workers(AV1_PRIMARY * ppi)1363*77c1e3ccSAndroid Build Coastguard Worker static inline void launch_fpmt_workers(AV1_PRIMARY *ppi) {
1364*77c1e3ccSAndroid Build Coastguard Worker   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
1365*77c1e3ccSAndroid Build Coastguard Worker   int num_workers = ppi->p_mt_info.p_num_workers;
1366*77c1e3ccSAndroid Build Coastguard Worker 
1367*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
1368*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *const worker = ppi->p_mt_info.p_workers[i];
1369*77c1e3ccSAndroid Build Coastguard Worker     if (i == 0)
1370*77c1e3ccSAndroid Build Coastguard Worker       winterface->execute(worker);
1371*77c1e3ccSAndroid Build Coastguard Worker     else
1372*77c1e3ccSAndroid Build Coastguard Worker       winterface->launch(worker);
1373*77c1e3ccSAndroid Build Coastguard Worker   }
1374*77c1e3ccSAndroid Build Coastguard Worker }
1375*77c1e3ccSAndroid Build Coastguard Worker 
1376*77c1e3ccSAndroid Build Coastguard Worker // Restore worker states after parallel encode.
restore_workers_after_fpmt(AV1_PRIMARY * ppi,int parallel_frame_count,int num_fpmt_workers_prepared)1377*77c1e3ccSAndroid Build Coastguard Worker static inline void restore_workers_after_fpmt(AV1_PRIMARY *ppi,
1378*77c1e3ccSAndroid Build Coastguard Worker                                               int parallel_frame_count,
1379*77c1e3ccSAndroid Build Coastguard Worker                                               int num_fpmt_workers_prepared) {
1380*77c1e3ccSAndroid Build Coastguard Worker   assert(parallel_frame_count <= ppi->num_fp_contexts &&
1381*77c1e3ccSAndroid Build Coastguard Worker          parallel_frame_count > 1);
1382*77c1e3ccSAndroid Build Coastguard Worker   (void)parallel_frame_count;
1383*77c1e3ccSAndroid Build Coastguard Worker 
1384*77c1e3ccSAndroid Build Coastguard Worker   PrimaryMultiThreadInfo *const p_mt_info = &ppi->p_mt_info;
1385*77c1e3ccSAndroid Build Coastguard Worker 
1386*77c1e3ccSAndroid Build Coastguard Worker   int frame_idx = 0;
1387*77c1e3ccSAndroid Build Coastguard Worker   int i = 0;
1388*77c1e3ccSAndroid Build Coastguard Worker   while (i < num_fpmt_workers_prepared) {
1389*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *cur_cpi = ppi->parallel_cpi[frame_idx];
1390*77c1e3ccSAndroid Build Coastguard Worker     MultiThreadInfo *mt_info = &cur_cpi->mt_info;
1391*77c1e3ccSAndroid Build Coastguard Worker     const AV1_COMMON *const cm = &cur_cpi->common;
1392*77c1e3ccSAndroid Build Coastguard Worker     const int num_planes = av1_num_planes(cm);
1393*77c1e3ccSAndroid Build Coastguard Worker 
1394*77c1e3ccSAndroid Build Coastguard Worker     // Restore the original cdef_worker pointers.
1395*77c1e3ccSAndroid Build Coastguard Worker     if (p_mt_info->cdef_worker != NULL) {
1396*77c1e3ccSAndroid Build Coastguard Worker       mt_info->cdef_worker->srcbuf = mt_info->restore_state_buf.cdef_srcbuf;
1397*77c1e3ccSAndroid Build Coastguard Worker       for (int plane = 0; plane < num_planes; plane++)
1398*77c1e3ccSAndroid Build Coastguard Worker         mt_info->cdef_worker->colbuf[plane] =
1399*77c1e3ccSAndroid Build Coastguard Worker             mt_info->restore_state_buf.cdef_colbuf[plane];
1400*77c1e3ccSAndroid Build Coastguard Worker     }
1401*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1402*77c1e3ccSAndroid Build Coastguard Worker     if (is_restoration_used(cm)) {
1403*77c1e3ccSAndroid Build Coastguard Worker       // Restore the original LR buffers.
1404*77c1e3ccSAndroid Build Coastguard Worker       int idx = i + mt_info->num_workers - 1;
1405*77c1e3ccSAndroid Build Coastguard Worker       assert(idx < mt_info->lr_row_sync.num_workers);
1406*77c1e3ccSAndroid Build Coastguard Worker       mt_info->lr_row_sync.lrworkerdata[idx].rst_tmpbuf =
1407*77c1e3ccSAndroid Build Coastguard Worker           mt_info->restore_state_buf.rst_tmpbuf;
1408*77c1e3ccSAndroid Build Coastguard Worker       mt_info->lr_row_sync.lrworkerdata[idx].rlbs =
1409*77c1e3ccSAndroid Build Coastguard Worker           mt_info->restore_state_buf.rlbs;
1410*77c1e3ccSAndroid Build Coastguard Worker     }
1411*77c1e3ccSAndroid Build Coastguard Worker #endif
1412*77c1e3ccSAndroid Build Coastguard Worker 
1413*77c1e3ccSAndroid Build Coastguard Worker     frame_idx++;
1414*77c1e3ccSAndroid Build Coastguard Worker     i += mt_info->num_workers;
1415*77c1e3ccSAndroid Build Coastguard Worker   }
1416*77c1e3ccSAndroid Build Coastguard Worker }
1417*77c1e3ccSAndroid Build Coastguard Worker 
1418*77c1e3ccSAndroid Build Coastguard Worker // Synchronize level 1 workers.
sync_fpmt_workers(AV1_PRIMARY * ppi,int frames_in_parallel_set)1419*77c1e3ccSAndroid Build Coastguard Worker static inline void sync_fpmt_workers(AV1_PRIMARY *ppi,
1420*77c1e3ccSAndroid Build Coastguard Worker                                      int frames_in_parallel_set) {
1421*77c1e3ccSAndroid Build Coastguard Worker   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
1422*77c1e3ccSAndroid Build Coastguard Worker   int num_workers = ppi->p_mt_info.p_num_workers;
1423*77c1e3ccSAndroid Build Coastguard Worker   int had_error = 0;
1424*77c1e3ccSAndroid Build Coastguard Worker   // Points to error in the earliest display order frame in the parallel set.
1425*77c1e3ccSAndroid Build Coastguard Worker   const struct aom_internal_error_info *error = NULL;
1426*77c1e3ccSAndroid Build Coastguard Worker 
1427*77c1e3ccSAndroid Build Coastguard Worker   // Encoding ends.
1428*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; --i) {
1429*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *const worker = ppi->p_mt_info.p_workers[i];
1430*77c1e3ccSAndroid Build Coastguard Worker     if (!winterface->sync(worker)) {
1431*77c1e3ccSAndroid Build Coastguard Worker       had_error = 1;
1432*77c1e3ccSAndroid Build Coastguard Worker       error = ppi->parallel_cpi[i]->common.error;
1433*77c1e3ccSAndroid Build Coastguard Worker     }
1434*77c1e3ccSAndroid Build Coastguard Worker   }
1435*77c1e3ccSAndroid Build Coastguard Worker 
1436*77c1e3ccSAndroid Build Coastguard Worker   restore_workers_after_fpmt(ppi, frames_in_parallel_set,
1437*77c1e3ccSAndroid Build Coastguard Worker                              ppi->p_mt_info.num_workers);
1438*77c1e3ccSAndroid Build Coastguard Worker 
1439*77c1e3ccSAndroid Build Coastguard Worker   if (had_error) aom_internal_error_copy(&ppi->error, error);
1440*77c1e3ccSAndroid Build Coastguard Worker }
1441*77c1e3ccSAndroid Build Coastguard Worker 
get_compressed_data_hook(void * arg1,void * arg2)1442*77c1e3ccSAndroid Build Coastguard Worker static int get_compressed_data_hook(void *arg1, void *arg2) {
1443*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMP *cpi = (AV1_COMP *)arg1;
1444*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMP_DATA *cpi_data = (AV1_COMP_DATA *)arg2;
1445*77c1e3ccSAndroid Build Coastguard Worker   int status = av1_get_compressed_data(cpi, cpi_data);
1446*77c1e3ccSAndroid Build Coastguard Worker 
1447*77c1e3ccSAndroid Build Coastguard Worker   // AOM_CODEC_OK(0) means no error.
1448*77c1e3ccSAndroid Build Coastguard Worker   return !status;
1449*77c1e3ccSAndroid Build Coastguard Worker }
1450*77c1e3ccSAndroid Build Coastguard Worker 
1451*77c1e3ccSAndroid Build Coastguard Worker // This function encodes the raw frame data for each frame in parallel encode
1452*77c1e3ccSAndroid Build Coastguard Worker // 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)1453*77c1e3ccSAndroid Build Coastguard Worker void av1_compress_parallel_frames(AV1_PRIMARY *const ppi,
1454*77c1e3ccSAndroid Build Coastguard Worker                                   AV1_COMP_DATA *const first_cpi_data) {
1455*77c1e3ccSAndroid Build Coastguard Worker   // Bitmask for the frame buffers referenced by cpi->scaled_ref_buf
1456*77c1e3ccSAndroid Build Coastguard Worker   // corresponding to frames in the current parallel encode set.
1457*77c1e3ccSAndroid Build Coastguard Worker   int ref_buffers_used_map = 0;
1458*77c1e3ccSAndroid Build Coastguard Worker   int frames_in_parallel_set = av1_init_parallel_frame_context(
1459*77c1e3ccSAndroid Build Coastguard Worker       first_cpi_data, ppi, &ref_buffers_used_map);
1460*77c1e3ccSAndroid Build Coastguard Worker   prepare_fpmt_workers(ppi, first_cpi_data, get_compressed_data_hook,
1461*77c1e3ccSAndroid Build Coastguard Worker                        frames_in_parallel_set);
1462*77c1e3ccSAndroid Build Coastguard Worker   launch_fpmt_workers(ppi);
1463*77c1e3ccSAndroid Build Coastguard Worker   sync_fpmt_workers(ppi, frames_in_parallel_set);
1464*77c1e3ccSAndroid Build Coastguard Worker 
1465*77c1e3ccSAndroid Build Coastguard Worker   // Release cpi->scaled_ref_buf corresponding to frames in the current parallel
1466*77c1e3ccSAndroid Build Coastguard Worker   // encode set.
1467*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < frames_in_parallel_set; ++i) {
1468*77c1e3ccSAndroid Build Coastguard Worker     av1_release_scaled_references_fpmt(ppi->parallel_cpi[i]);
1469*77c1e3ccSAndroid Build Coastguard Worker   }
1470*77c1e3ccSAndroid Build Coastguard Worker   av1_decrement_ref_counts_fpmt(ppi->cpi->common.buffer_pool,
1471*77c1e3ccSAndroid Build Coastguard Worker                                 ref_buffers_used_map);
1472*77c1e3ccSAndroid Build Coastguard Worker }
1473*77c1e3ccSAndroid Build Coastguard Worker 
launch_workers(MultiThreadInfo * const mt_info,int num_workers)1474*77c1e3ccSAndroid Build Coastguard Worker static inline void launch_workers(MultiThreadInfo *const mt_info,
1475*77c1e3ccSAndroid Build Coastguard Worker                                   int num_workers) {
1476*77c1e3ccSAndroid Build Coastguard Worker   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
1477*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
1478*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *const worker = &mt_info->workers[i];
1479*77c1e3ccSAndroid Build Coastguard Worker     worker->had_error = 0;
1480*77c1e3ccSAndroid Build Coastguard Worker     if (i == 0)
1481*77c1e3ccSAndroid Build Coastguard Worker       winterface->execute(worker);
1482*77c1e3ccSAndroid Build Coastguard Worker     else
1483*77c1e3ccSAndroid Build Coastguard Worker       winterface->launch(worker);
1484*77c1e3ccSAndroid Build Coastguard Worker   }
1485*77c1e3ccSAndroid Build Coastguard Worker }
1486*77c1e3ccSAndroid Build Coastguard Worker 
sync_enc_workers(MultiThreadInfo * const mt_info,AV1_COMMON * const cm,int num_workers)1487*77c1e3ccSAndroid Build Coastguard Worker static inline void sync_enc_workers(MultiThreadInfo *const mt_info,
1488*77c1e3ccSAndroid Build Coastguard Worker                                     AV1_COMMON *const cm, int num_workers) {
1489*77c1e3ccSAndroid Build Coastguard Worker   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
1490*77c1e3ccSAndroid Build Coastguard Worker   const AVxWorker *const worker_main = &mt_info->workers[0];
1491*77c1e3ccSAndroid Build Coastguard Worker   int had_error = worker_main->had_error;
1492*77c1e3ccSAndroid Build Coastguard Worker   struct aom_internal_error_info error_info;
1493*77c1e3ccSAndroid Build Coastguard Worker 
1494*77c1e3ccSAndroid Build Coastguard Worker   // Read the error_info of main thread.
1495*77c1e3ccSAndroid Build Coastguard Worker   if (had_error) {
1496*77c1e3ccSAndroid Build Coastguard Worker     error_info = ((EncWorkerData *)worker_main->data1)->error_info;
1497*77c1e3ccSAndroid Build Coastguard Worker   }
1498*77c1e3ccSAndroid Build Coastguard Worker 
1499*77c1e3ccSAndroid Build Coastguard Worker   // Encoding ends.
1500*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i > 0; i--) {
1501*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *const worker = &mt_info->workers[i];
1502*77c1e3ccSAndroid Build Coastguard Worker     if (!winterface->sync(worker)) {
1503*77c1e3ccSAndroid Build Coastguard Worker       had_error = 1;
1504*77c1e3ccSAndroid Build Coastguard Worker       error_info = ((EncWorkerData *)worker->data1)->error_info;
1505*77c1e3ccSAndroid Build Coastguard Worker     }
1506*77c1e3ccSAndroid Build Coastguard Worker   }
1507*77c1e3ccSAndroid Build Coastguard Worker 
1508*77c1e3ccSAndroid Build Coastguard Worker   if (had_error) aom_internal_error_copy(cm->error, &error_info);
1509*77c1e3ccSAndroid Build Coastguard Worker 
1510*77c1e3ccSAndroid Build Coastguard Worker   // Restore xd->error_info of the main thread back to cm->error so that the
1511*77c1e3ccSAndroid Build Coastguard Worker   // multithreaded code, when executed using a single thread, has a valid
1512*77c1e3ccSAndroid Build Coastguard Worker   // xd->error_info.
1513*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &((EncWorkerData *)worker_main->data1)->td->mb.e_mbd;
1514*77c1e3ccSAndroid Build Coastguard Worker   xd->error_info = cm->error;
1515*77c1e3ccSAndroid Build Coastguard Worker }
1516*77c1e3ccSAndroid Build Coastguard Worker 
accumulate_counters_enc_workers(AV1_COMP * cpi,int num_workers)1517*77c1e3ccSAndroid Build Coastguard Worker static inline void accumulate_counters_enc_workers(AV1_COMP *cpi,
1518*77c1e3ccSAndroid Build Coastguard Worker                                                    int num_workers) {
1519*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
1520*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *const worker = &cpi->mt_info.workers[i];
1521*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
1522*77c1e3ccSAndroid Build Coastguard Worker     cpi->intrabc_used |= thread_data->td->intrabc_used;
1523*77c1e3ccSAndroid Build Coastguard Worker     cpi->deltaq_used |= thread_data->td->deltaq_used;
1524*77c1e3ccSAndroid Build Coastguard Worker     // Accumulate rtc counters.
1525*77c1e3ccSAndroid Build Coastguard Worker     if (!frame_is_intra_only(&cpi->common))
1526*77c1e3ccSAndroid Build Coastguard Worker       av1_accumulate_rtc_counters(cpi, &thread_data->td->mb);
1527*77c1e3ccSAndroid Build Coastguard Worker     cpi->palette_pixel_num += thread_data->td->mb.palette_pixels;
1528*77c1e3ccSAndroid Build Coastguard Worker     if (thread_data->td != &cpi->td) {
1529*77c1e3ccSAndroid Build Coastguard Worker       // Keep these conditional expressions in sync with the corresponding ones
1530*77c1e3ccSAndroid Build Coastguard Worker       // in prepare_enc_workers().
1531*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->sf.inter_sf.mv_cost_upd_level != INTERNAL_COST_UPD_OFF) {
1532*77c1e3ccSAndroid Build Coastguard Worker         aom_free(thread_data->td->mv_costs_alloc);
1533*77c1e3ccSAndroid Build Coastguard Worker         thread_data->td->mv_costs_alloc = NULL;
1534*77c1e3ccSAndroid Build Coastguard Worker       }
1535*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->sf.intra_sf.dv_cost_upd_level != INTERNAL_COST_UPD_OFF) {
1536*77c1e3ccSAndroid Build Coastguard Worker         aom_free(thread_data->td->dv_costs_alloc);
1537*77c1e3ccSAndroid Build Coastguard Worker         thread_data->td->dv_costs_alloc = NULL;
1538*77c1e3ccSAndroid Build Coastguard Worker       }
1539*77c1e3ccSAndroid Build Coastguard Worker     }
1540*77c1e3ccSAndroid Build Coastguard Worker     av1_dealloc_mb_data(&thread_data->td->mb, av1_num_planes(&cpi->common));
1541*77c1e3ccSAndroid Build Coastguard Worker 
1542*77c1e3ccSAndroid Build Coastguard Worker     // Accumulate counters.
1543*77c1e3ccSAndroid Build Coastguard Worker     if (i > 0) {
1544*77c1e3ccSAndroid Build Coastguard Worker       av1_accumulate_frame_counts(&cpi->counts, thread_data->td->counts);
1545*77c1e3ccSAndroid Build Coastguard Worker       accumulate_rd_opt(&cpi->td, thread_data->td);
1546*77c1e3ccSAndroid Build Coastguard Worker       cpi->td.mb.txfm_search_info.txb_split_count +=
1547*77c1e3ccSAndroid Build Coastguard Worker           thread_data->td->mb.txfm_search_info.txb_split_count;
1548*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_SPEED_STATS
1549*77c1e3ccSAndroid Build Coastguard Worker       cpi->td.mb.txfm_search_info.tx_search_count +=
1550*77c1e3ccSAndroid Build Coastguard Worker           thread_data->td->mb.txfm_search_info.tx_search_count;
1551*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_SPEED_STATS
1552*77c1e3ccSAndroid Build Coastguard Worker     }
1553*77c1e3ccSAndroid Build Coastguard Worker   }
1554*77c1e3ccSAndroid Build Coastguard Worker }
1555*77c1e3ccSAndroid Build Coastguard Worker 
prepare_enc_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)1556*77c1e3ccSAndroid Build Coastguard Worker static inline void prepare_enc_workers(AV1_COMP *cpi, AVxWorkerHook hook,
1557*77c1e3ccSAndroid Build Coastguard Worker                                        int num_workers) {
1558*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
1559*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1560*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
1561*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *const worker = &mt_info->workers[i];
1562*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *const thread_data = &mt_info->tile_thr_data[i];
1563*77c1e3ccSAndroid Build Coastguard Worker 
1564*77c1e3ccSAndroid Build Coastguard Worker     worker->hook = hook;
1565*77c1e3ccSAndroid Build Coastguard Worker     worker->data1 = thread_data;
1566*77c1e3ccSAndroid Build Coastguard Worker     worker->data2 = NULL;
1567*77c1e3ccSAndroid Build Coastguard Worker 
1568*77c1e3ccSAndroid Build Coastguard Worker     thread_data->thread_id = i;
1569*77c1e3ccSAndroid Build Coastguard Worker     // Set the starting tile for each thread.
1570*77c1e3ccSAndroid Build Coastguard Worker     thread_data->start = i;
1571*77c1e3ccSAndroid Build Coastguard Worker 
1572*77c1e3ccSAndroid Build Coastguard Worker     thread_data->cpi = cpi;
1573*77c1e3ccSAndroid Build Coastguard Worker     if (i == 0) {
1574*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td = &cpi->td;
1575*77c1e3ccSAndroid Build Coastguard Worker     } else {
1576*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td = thread_data->original_td;
1577*77c1e3ccSAndroid Build Coastguard Worker     }
1578*77c1e3ccSAndroid Build Coastguard Worker 
1579*77c1e3ccSAndroid Build Coastguard Worker     thread_data->td->intrabc_used = 0;
1580*77c1e3ccSAndroid Build Coastguard Worker     thread_data->td->deltaq_used = 0;
1581*77c1e3ccSAndroid Build Coastguard Worker     thread_data->td->abs_sum_level = 0;
1582*77c1e3ccSAndroid Build Coastguard Worker     thread_data->td->rd_counts.seg_tmp_pred_cost[0] = 0;
1583*77c1e3ccSAndroid Build Coastguard Worker     thread_data->td->rd_counts.seg_tmp_pred_cost[1] = 0;
1584*77c1e3ccSAndroid Build Coastguard Worker 
1585*77c1e3ccSAndroid Build Coastguard Worker     // Before encoding a frame, copy the thread data from cpi.
1586*77c1e3ccSAndroid Build Coastguard Worker     if (thread_data->td != &cpi->td) {
1587*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->mb = cpi->td.mb;
1588*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->rd_counts = cpi->td.rd_counts;
1589*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->mb.obmc_buffer = thread_data->td->obmc_buffer;
1590*77c1e3ccSAndroid Build Coastguard Worker 
1591*77c1e3ccSAndroid Build Coastguard Worker       for (int x = 0; x < 2; x++) {
1592*77c1e3ccSAndroid Build Coastguard Worker         for (int y = 0; y < 2; y++) {
1593*77c1e3ccSAndroid Build Coastguard Worker           memcpy(thread_data->td->hash_value_buffer[x][y],
1594*77c1e3ccSAndroid Build Coastguard Worker                  cpi->td.mb.intrabc_hash_info.hash_value_buffer[x][y],
1595*77c1e3ccSAndroid Build Coastguard Worker                  AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
1596*77c1e3ccSAndroid Build Coastguard Worker                      sizeof(*thread_data->td->hash_value_buffer[0][0]));
1597*77c1e3ccSAndroid Build Coastguard Worker           thread_data->td->mb.intrabc_hash_info.hash_value_buffer[x][y] =
1598*77c1e3ccSAndroid Build Coastguard Worker               thread_data->td->hash_value_buffer[x][y];
1599*77c1e3ccSAndroid Build Coastguard Worker         }
1600*77c1e3ccSAndroid Build Coastguard Worker       }
1601*77c1e3ccSAndroid Build Coastguard Worker       // Keep these conditional expressions in sync with the corresponding ones
1602*77c1e3ccSAndroid Build Coastguard Worker       // in accumulate_counters_enc_workers().
1603*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->sf.inter_sf.mv_cost_upd_level != INTERNAL_COST_UPD_OFF) {
1604*77c1e3ccSAndroid Build Coastguard Worker         CHECK_MEM_ERROR(
1605*77c1e3ccSAndroid Build Coastguard Worker             cm, thread_data->td->mv_costs_alloc,
1606*77c1e3ccSAndroid Build Coastguard Worker             (MvCosts *)aom_malloc(sizeof(*thread_data->td->mv_costs_alloc)));
1607*77c1e3ccSAndroid Build Coastguard Worker         thread_data->td->mb.mv_costs = thread_data->td->mv_costs_alloc;
1608*77c1e3ccSAndroid Build Coastguard Worker         memcpy(thread_data->td->mb.mv_costs, cpi->td.mb.mv_costs,
1609*77c1e3ccSAndroid Build Coastguard Worker                sizeof(MvCosts));
1610*77c1e3ccSAndroid Build Coastguard Worker       }
1611*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->sf.intra_sf.dv_cost_upd_level != INTERNAL_COST_UPD_OFF) {
1612*77c1e3ccSAndroid Build Coastguard Worker         // Reset dv_costs to NULL for worker threads when dv cost update is
1613*77c1e3ccSAndroid Build Coastguard Worker         // enabled so that only dv_cost_upd_level needs to be checked before the
1614*77c1e3ccSAndroid Build Coastguard Worker         // aom_free() call for the same.
1615*77c1e3ccSAndroid Build Coastguard Worker         thread_data->td->mb.dv_costs = NULL;
1616*77c1e3ccSAndroid Build Coastguard Worker         if (av1_need_dv_costs(cpi)) {
1617*77c1e3ccSAndroid Build Coastguard Worker           CHECK_MEM_ERROR(cm, thread_data->td->dv_costs_alloc,
1618*77c1e3ccSAndroid Build Coastguard Worker                           (IntraBCMVCosts *)aom_malloc(
1619*77c1e3ccSAndroid Build Coastguard Worker                               sizeof(*thread_data->td->dv_costs_alloc)));
1620*77c1e3ccSAndroid Build Coastguard Worker           thread_data->td->mb.dv_costs = thread_data->td->dv_costs_alloc;
1621*77c1e3ccSAndroid Build Coastguard Worker           memcpy(thread_data->td->mb.dv_costs, cpi->td.mb.dv_costs,
1622*77c1e3ccSAndroid Build Coastguard Worker                  sizeof(IntraBCMVCosts));
1623*77c1e3ccSAndroid Build Coastguard Worker         }
1624*77c1e3ccSAndroid Build Coastguard Worker       }
1625*77c1e3ccSAndroid Build Coastguard Worker     }
1626*77c1e3ccSAndroid Build Coastguard Worker     av1_alloc_mb_data(cpi, &thread_data->td->mb);
1627*77c1e3ccSAndroid Build Coastguard Worker 
1628*77c1e3ccSAndroid Build Coastguard Worker     // Reset rtc counters.
1629*77c1e3ccSAndroid Build Coastguard Worker     av1_init_rtc_counters(&thread_data->td->mb);
1630*77c1e3ccSAndroid Build Coastguard Worker 
1631*77c1e3ccSAndroid Build Coastguard Worker     thread_data->td->mb.palette_pixels = 0;
1632*77c1e3ccSAndroid Build Coastguard Worker 
1633*77c1e3ccSAndroid Build Coastguard Worker     if (thread_data->td->counts != &cpi->counts) {
1634*77c1e3ccSAndroid Build Coastguard Worker       memcpy(thread_data->td->counts, &cpi->counts, sizeof(cpi->counts));
1635*77c1e3ccSAndroid Build Coastguard Worker     }
1636*77c1e3ccSAndroid Build Coastguard Worker 
1637*77c1e3ccSAndroid Build Coastguard Worker     if (i > 0) {
1638*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->mb.palette_buffer = thread_data->td->palette_buffer;
1639*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->mb.comp_rd_buffer = thread_data->td->comp_rd_buffer;
1640*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->mb.tmp_conv_dst = thread_data->td->tmp_conv_dst;
1641*77c1e3ccSAndroid Build Coastguard Worker       for (int j = 0; j < 2; ++j) {
1642*77c1e3ccSAndroid Build Coastguard Worker         thread_data->td->mb.tmp_pred_bufs[j] =
1643*77c1e3ccSAndroid Build Coastguard Worker             thread_data->td->tmp_pred_bufs[j];
1644*77c1e3ccSAndroid Build Coastguard Worker       }
1645*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->mb.pixel_gradient_info =
1646*77c1e3ccSAndroid Build Coastguard Worker           thread_data->td->pixel_gradient_info;
1647*77c1e3ccSAndroid Build Coastguard Worker 
1648*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->mb.src_var_info_of_4x4_sub_blocks =
1649*77c1e3ccSAndroid Build Coastguard Worker           thread_data->td->src_var_info_of_4x4_sub_blocks;
1650*77c1e3ccSAndroid Build Coastguard Worker 
1651*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->mb.e_mbd.tmp_conv_dst = thread_data->td->mb.tmp_conv_dst;
1652*77c1e3ccSAndroid Build Coastguard Worker       for (int j = 0; j < 2; ++j) {
1653*77c1e3ccSAndroid Build Coastguard Worker         thread_data->td->mb.e_mbd.tmp_obmc_bufs[j] =
1654*77c1e3ccSAndroid Build Coastguard Worker             thread_data->td->mb.tmp_pred_bufs[j];
1655*77c1e3ccSAndroid Build Coastguard Worker       }
1656*77c1e3ccSAndroid Build Coastguard Worker     }
1657*77c1e3ccSAndroid Build Coastguard Worker   }
1658*77c1e3ccSAndroid Build Coastguard Worker }
1659*77c1e3ccSAndroid Build Coastguard Worker 
1660*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
fp_prepare_enc_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)1661*77c1e3ccSAndroid Build Coastguard Worker static inline void fp_prepare_enc_workers(AV1_COMP *cpi, AVxWorkerHook hook,
1662*77c1e3ccSAndroid Build Coastguard Worker                                           int num_workers) {
1663*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1664*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
1665*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
1666*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *const worker = &mt_info->workers[i];
1667*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *const thread_data = &mt_info->tile_thr_data[i];
1668*77c1e3ccSAndroid Build Coastguard Worker 
1669*77c1e3ccSAndroid Build Coastguard Worker     worker->hook = hook;
1670*77c1e3ccSAndroid Build Coastguard Worker     worker->data1 = thread_data;
1671*77c1e3ccSAndroid Build Coastguard Worker     worker->data2 = NULL;
1672*77c1e3ccSAndroid Build Coastguard Worker 
1673*77c1e3ccSAndroid Build Coastguard Worker     thread_data->thread_id = i;
1674*77c1e3ccSAndroid Build Coastguard Worker     // Set the starting tile for each thread.
1675*77c1e3ccSAndroid Build Coastguard Worker     thread_data->start = i;
1676*77c1e3ccSAndroid Build Coastguard Worker 
1677*77c1e3ccSAndroid Build Coastguard Worker     thread_data->cpi = cpi;
1678*77c1e3ccSAndroid Build Coastguard Worker     if (i == 0) {
1679*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td = &cpi->td;
1680*77c1e3ccSAndroid Build Coastguard Worker     } else {
1681*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td = thread_data->original_td;
1682*77c1e3ccSAndroid Build Coastguard Worker       // Before encoding a frame, copy the thread data from cpi.
1683*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->mb = cpi->td.mb;
1684*77c1e3ccSAndroid Build Coastguard Worker     }
1685*77c1e3ccSAndroid Build Coastguard Worker     av1_alloc_src_diff_buf(cm, &thread_data->td->mb);
1686*77c1e3ccSAndroid Build Coastguard Worker   }
1687*77c1e3ccSAndroid Build Coastguard Worker }
1688*77c1e3ccSAndroid Build Coastguard Worker #endif
1689*77c1e3ccSAndroid Build Coastguard Worker 
1690*77c1e3ccSAndroid Build Coastguard Worker // Computes the number of workers for row multi-threading of encoding stage
compute_num_enc_row_mt_workers(const AV1_COMMON * cm,int max_threads)1691*77c1e3ccSAndroid Build Coastguard Worker static inline int compute_num_enc_row_mt_workers(const AV1_COMMON *cm,
1692*77c1e3ccSAndroid Build Coastguard Worker                                                  int max_threads) {
1693*77c1e3ccSAndroid Build Coastguard Worker   TileInfo tile_info;
1694*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
1695*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
1696*77c1e3ccSAndroid Build Coastguard Worker   int total_num_threads_row_mt = 0;
1697*77c1e3ccSAndroid Build Coastguard Worker   for (int row = 0; row < tile_rows; row++) {
1698*77c1e3ccSAndroid Build Coastguard Worker     for (int col = 0; col < tile_cols; col++) {
1699*77c1e3ccSAndroid Build Coastguard Worker       av1_tile_init(&tile_info, cm, row, col);
1700*77c1e3ccSAndroid Build Coastguard Worker       const int num_sb_rows_in_tile = av1_get_sb_rows_in_tile(cm, &tile_info);
1701*77c1e3ccSAndroid Build Coastguard Worker       const int num_sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, &tile_info);
1702*77c1e3ccSAndroid Build Coastguard Worker       total_num_threads_row_mt +=
1703*77c1e3ccSAndroid Build Coastguard Worker           AOMMIN((num_sb_cols_in_tile + 1) >> 1, num_sb_rows_in_tile);
1704*77c1e3ccSAndroid Build Coastguard Worker     }
1705*77c1e3ccSAndroid Build Coastguard Worker   }
1706*77c1e3ccSAndroid Build Coastguard Worker   return AOMMIN(max_threads, total_num_threads_row_mt);
1707*77c1e3ccSAndroid Build Coastguard Worker }
1708*77c1e3ccSAndroid Build Coastguard Worker 
1709*77c1e3ccSAndroid Build Coastguard Worker // Computes the number of workers for tile multi-threading of encoding stage
compute_num_enc_tile_mt_workers(const AV1_COMMON * cm,int max_threads)1710*77c1e3ccSAndroid Build Coastguard Worker static inline int compute_num_enc_tile_mt_workers(const AV1_COMMON *cm,
1711*77c1e3ccSAndroid Build Coastguard Worker                                                   int max_threads) {
1712*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
1713*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
1714*77c1e3ccSAndroid Build Coastguard Worker   return AOMMIN(max_threads, tile_cols * tile_rows);
1715*77c1e3ccSAndroid Build Coastguard Worker }
1716*77c1e3ccSAndroid Build Coastguard Worker 
1717*77c1e3ccSAndroid Build Coastguard Worker // Find max worker of all MT stages
av1_get_max_num_workers(const AV1_COMP * cpi)1718*77c1e3ccSAndroid Build Coastguard Worker int av1_get_max_num_workers(const AV1_COMP *cpi) {
1719*77c1e3ccSAndroid Build Coastguard Worker   int max_num_workers = 0;
1720*77c1e3ccSAndroid Build Coastguard Worker   for (int i = MOD_FP; i < NUM_MT_MODULES; i++)
1721*77c1e3ccSAndroid Build Coastguard Worker     max_num_workers =
1722*77c1e3ccSAndroid Build Coastguard Worker         AOMMAX(cpi->ppi->p_mt_info.num_mod_workers[i], max_num_workers);
1723*77c1e3ccSAndroid Build Coastguard Worker   assert(max_num_workers >= 1);
1724*77c1e3ccSAndroid Build Coastguard Worker   return AOMMIN(max_num_workers, cpi->oxcf.max_threads);
1725*77c1e3ccSAndroid Build Coastguard Worker }
1726*77c1e3ccSAndroid Build Coastguard Worker 
1727*77c1e3ccSAndroid Build Coastguard Worker // Computes the number of workers for encoding stage (row/tile multi-threading)
compute_num_enc_workers(const AV1_COMP * cpi,int max_workers)1728*77c1e3ccSAndroid Build Coastguard Worker static int compute_num_enc_workers(const AV1_COMP *cpi, int max_workers) {
1729*77c1e3ccSAndroid Build Coastguard Worker   if (max_workers <= 1) return 1;
1730*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.row_mt)
1731*77c1e3ccSAndroid Build Coastguard Worker     return compute_num_enc_row_mt_workers(&cpi->common, max_workers);
1732*77c1e3ccSAndroid Build Coastguard Worker   else
1733*77c1e3ccSAndroid Build Coastguard Worker     return compute_num_enc_tile_mt_workers(&cpi->common, max_workers);
1734*77c1e3ccSAndroid Build Coastguard Worker }
1735*77c1e3ccSAndroid Build Coastguard Worker 
av1_encode_tiles_mt(AV1_COMP * cpi)1736*77c1e3ccSAndroid Build Coastguard Worker void av1_encode_tiles_mt(AV1_COMP *cpi) {
1737*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1738*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
1739*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
1740*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
1741*77c1e3ccSAndroid Build Coastguard Worker   int num_workers = mt_info->num_mod_workers[MOD_ENC];
1742*77c1e3ccSAndroid Build Coastguard Worker 
1743*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(cpi->tile_data == NULL,
1744*77c1e3ccSAndroid Build Coastguard Worker                  cpi->allocated_tiles < tile_cols * tile_rows));
1745*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->allocated_tiles < tile_cols * tile_rows) av1_alloc_tile_data(cpi);
1746*77c1e3ccSAndroid Build Coastguard Worker 
1747*77c1e3ccSAndroid Build Coastguard Worker   av1_init_tile_data(cpi);
1748*77c1e3ccSAndroid Build Coastguard Worker   num_workers = AOMMIN(num_workers, mt_info->num_workers);
1749*77c1e3ccSAndroid Build Coastguard Worker 
1750*77c1e3ccSAndroid Build Coastguard Worker   prepare_enc_workers(cpi, enc_worker_hook, num_workers);
1751*77c1e3ccSAndroid Build Coastguard Worker   launch_workers(&cpi->mt_info, num_workers);
1752*77c1e3ccSAndroid Build Coastguard Worker   sync_enc_workers(&cpi->mt_info, cm, num_workers);
1753*77c1e3ccSAndroid Build Coastguard Worker   accumulate_counters_enc_workers(cpi, num_workers);
1754*77c1e3ccSAndroid Build Coastguard Worker }
1755*77c1e3ccSAndroid Build Coastguard Worker 
1756*77c1e3ccSAndroid Build Coastguard Worker // Accumulate frame counts. FRAME_COUNTS consist solely of 'unsigned int'
1757*77c1e3ccSAndroid Build Coastguard Worker // 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)1758*77c1e3ccSAndroid Build Coastguard Worker void av1_accumulate_frame_counts(FRAME_COUNTS *acc_counts,
1759*77c1e3ccSAndroid Build Coastguard Worker                                  const FRAME_COUNTS *counts) {
1760*77c1e3ccSAndroid Build Coastguard Worker   unsigned int *const acc = (unsigned int *)acc_counts;
1761*77c1e3ccSAndroid Build Coastguard Worker   const unsigned int *const cnt = (const unsigned int *)counts;
1762*77c1e3ccSAndroid Build Coastguard Worker 
1763*77c1e3ccSAndroid Build Coastguard Worker   const unsigned int n_counts = sizeof(FRAME_COUNTS) / sizeof(unsigned int);
1764*77c1e3ccSAndroid Build Coastguard Worker 
1765*77c1e3ccSAndroid Build Coastguard Worker   for (unsigned int i = 0; i < n_counts; i++) acc[i] += cnt[i];
1766*77c1e3ccSAndroid Build Coastguard Worker }
1767*77c1e3ccSAndroid Build Coastguard Worker 
1768*77c1e3ccSAndroid Build Coastguard Worker // Computes the maximum number of sb rows and sb_cols across tiles which are
1769*77c1e3ccSAndroid Build Coastguard Worker // used to allocate memory for multi-threaded encoding with row-mt=1.
compute_max_sb_rows_cols(const AV1_COMMON * cm,int * max_sb_rows_in_tile,int * max_sb_cols_in_tile)1770*77c1e3ccSAndroid Build Coastguard Worker static inline void compute_max_sb_rows_cols(const AV1_COMMON *cm,
1771*77c1e3ccSAndroid Build Coastguard Worker                                             int *max_sb_rows_in_tile,
1772*77c1e3ccSAndroid Build Coastguard Worker                                             int *max_sb_cols_in_tile) {
1773*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
1774*77c1e3ccSAndroid Build Coastguard Worker   const int mib_size_log2 = cm->seq_params->mib_size_log2;
1775*77c1e3ccSAndroid Build Coastguard Worker   const int num_mi_rows = cm->mi_params.mi_rows;
1776*77c1e3ccSAndroid Build Coastguard Worker   const int *const row_start_sb = cm->tiles.row_start_sb;
1777*77c1e3ccSAndroid Build Coastguard Worker   for (int row = 0; row < tile_rows; row++) {
1778*77c1e3ccSAndroid Build Coastguard Worker     const int mi_row_start = row_start_sb[row] << mib_size_log2;
1779*77c1e3ccSAndroid Build Coastguard Worker     const int mi_row_end =
1780*77c1e3ccSAndroid Build Coastguard Worker         AOMMIN(row_start_sb[row + 1] << mib_size_log2, num_mi_rows);
1781*77c1e3ccSAndroid Build Coastguard Worker     const int num_sb_rows_in_tile =
1782*77c1e3ccSAndroid Build Coastguard Worker         CEIL_POWER_OF_TWO(mi_row_end - mi_row_start, mib_size_log2);
1783*77c1e3ccSAndroid Build Coastguard Worker     *max_sb_rows_in_tile = AOMMAX(*max_sb_rows_in_tile, num_sb_rows_in_tile);
1784*77c1e3ccSAndroid Build Coastguard Worker   }
1785*77c1e3ccSAndroid Build Coastguard Worker 
1786*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
1787*77c1e3ccSAndroid Build Coastguard Worker   const int num_mi_cols = cm->mi_params.mi_cols;
1788*77c1e3ccSAndroid Build Coastguard Worker   const int *const col_start_sb = cm->tiles.col_start_sb;
1789*77c1e3ccSAndroid Build Coastguard Worker   for (int col = 0; col < tile_cols; col++) {
1790*77c1e3ccSAndroid Build Coastguard Worker     const int mi_col_start = col_start_sb[col] << mib_size_log2;
1791*77c1e3ccSAndroid Build Coastguard Worker     const int mi_col_end =
1792*77c1e3ccSAndroid Build Coastguard Worker         AOMMIN(col_start_sb[col + 1] << mib_size_log2, num_mi_cols);
1793*77c1e3ccSAndroid Build Coastguard Worker     const int num_sb_cols_in_tile =
1794*77c1e3ccSAndroid Build Coastguard Worker         CEIL_POWER_OF_TWO(mi_col_end - mi_col_start, mib_size_log2);
1795*77c1e3ccSAndroid Build Coastguard Worker     *max_sb_cols_in_tile = AOMMAX(*max_sb_cols_in_tile, num_sb_cols_in_tile);
1796*77c1e3ccSAndroid Build Coastguard Worker   }
1797*77c1e3ccSAndroid Build Coastguard Worker }
1798*77c1e3ccSAndroid Build Coastguard Worker 
1799*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
1800*77c1e3ccSAndroid Build Coastguard Worker // Computes the number of workers for firstpass stage (row/tile multi-threading)
av1_fp_compute_num_enc_workers(AV1_COMP * cpi)1801*77c1e3ccSAndroid Build Coastguard Worker int av1_fp_compute_num_enc_workers(AV1_COMP *cpi) {
1802*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *cm = &cpi->common;
1803*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
1804*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
1805*77c1e3ccSAndroid Build Coastguard Worker   int total_num_threads_row_mt = 0;
1806*77c1e3ccSAndroid Build Coastguard Worker   TileInfo tile_info;
1807*77c1e3ccSAndroid Build Coastguard Worker 
1808*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.max_threads <= 1) return 1;
1809*77c1e3ccSAndroid Build Coastguard Worker 
1810*77c1e3ccSAndroid Build Coastguard Worker   for (int row = 0; row < tile_rows; row++) {
1811*77c1e3ccSAndroid Build Coastguard Worker     for (int col = 0; col < tile_cols; col++) {
1812*77c1e3ccSAndroid Build Coastguard Worker       av1_tile_init(&tile_info, cm, row, col);
1813*77c1e3ccSAndroid Build Coastguard Worker       const int num_mb_rows_in_tile =
1814*77c1e3ccSAndroid Build Coastguard Worker           av1_get_unit_rows_in_tile(&tile_info, cpi->fp_block_size);
1815*77c1e3ccSAndroid Build Coastguard Worker       const int num_mb_cols_in_tile =
1816*77c1e3ccSAndroid Build Coastguard Worker           av1_get_unit_cols_in_tile(&tile_info, cpi->fp_block_size);
1817*77c1e3ccSAndroid Build Coastguard Worker       total_num_threads_row_mt +=
1818*77c1e3ccSAndroid Build Coastguard Worker           AOMMIN((num_mb_cols_in_tile + 1) >> 1, num_mb_rows_in_tile);
1819*77c1e3ccSAndroid Build Coastguard Worker     }
1820*77c1e3ccSAndroid Build Coastguard Worker   }
1821*77c1e3ccSAndroid Build Coastguard Worker   return AOMMIN(cpi->oxcf.max_threads, total_num_threads_row_mt);
1822*77c1e3ccSAndroid Build Coastguard Worker }
1823*77c1e3ccSAndroid Build Coastguard Worker 
1824*77c1e3ccSAndroid Build Coastguard Worker // Computes the maximum number of mb_rows for row multi-threading of firstpass
1825*77c1e3ccSAndroid Build Coastguard Worker // stage
fp_compute_max_mb_rows(const AV1_COMMON * cm,BLOCK_SIZE fp_block_size)1826*77c1e3ccSAndroid Build Coastguard Worker static inline int fp_compute_max_mb_rows(const AV1_COMMON *cm,
1827*77c1e3ccSAndroid Build Coastguard Worker                                          BLOCK_SIZE fp_block_size) {
1828*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
1829*77c1e3ccSAndroid Build Coastguard Worker   const int unit_height_log2 = mi_size_high_log2[fp_block_size];
1830*77c1e3ccSAndroid Build Coastguard Worker   const int mib_size_log2 = cm->seq_params->mib_size_log2;
1831*77c1e3ccSAndroid Build Coastguard Worker   const int num_mi_rows = cm->mi_params.mi_rows;
1832*77c1e3ccSAndroid Build Coastguard Worker   const int *const row_start_sb = cm->tiles.row_start_sb;
1833*77c1e3ccSAndroid Build Coastguard Worker   int max_mb_rows = 0;
1834*77c1e3ccSAndroid Build Coastguard Worker 
1835*77c1e3ccSAndroid Build Coastguard Worker   for (int row = 0; row < tile_rows; row++) {
1836*77c1e3ccSAndroid Build Coastguard Worker     const int mi_row_start = row_start_sb[row] << mib_size_log2;
1837*77c1e3ccSAndroid Build Coastguard Worker     const int mi_row_end =
1838*77c1e3ccSAndroid Build Coastguard Worker         AOMMIN(row_start_sb[row + 1] << mib_size_log2, num_mi_rows);
1839*77c1e3ccSAndroid Build Coastguard Worker     const int num_mb_rows_in_tile =
1840*77c1e3ccSAndroid Build Coastguard Worker         CEIL_POWER_OF_TWO(mi_row_end - mi_row_start, unit_height_log2);
1841*77c1e3ccSAndroid Build Coastguard Worker     max_mb_rows = AOMMAX(max_mb_rows, num_mb_rows_in_tile);
1842*77c1e3ccSAndroid Build Coastguard Worker   }
1843*77c1e3ccSAndroid Build Coastguard Worker   return max_mb_rows;
1844*77c1e3ccSAndroid Build Coastguard Worker }
1845*77c1e3ccSAndroid Build Coastguard Worker #endif
1846*77c1e3ccSAndroid Build Coastguard Worker 
lpf_pipeline_mt_init(AV1_COMP * cpi,int num_workers)1847*77c1e3ccSAndroid Build Coastguard Worker static void lpf_pipeline_mt_init(AV1_COMP *cpi, int num_workers) {
1848*77c1e3ccSAndroid Build Coastguard Worker   // Pipelining of loop-filtering after encoding is enabled when loop-filter
1849*77c1e3ccSAndroid Build Coastguard Worker   // level is chosen based on quantizer and frame type. It is disabled in case
1850*77c1e3ccSAndroid Build Coastguard Worker   // of 'LOOPFILTER_SELECTIVELY' as the stats collected during encoding stage
1851*77c1e3ccSAndroid Build Coastguard Worker   // decides the filter level. Loop-filtering is disabled in case
1852*77c1e3ccSAndroid Build Coastguard Worker   // of non-reference frames and for frames with intra block copy tool enabled.
1853*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *cm = &cpi->common;
1854*77c1e3ccSAndroid Build Coastguard Worker   const int use_loopfilter = is_loopfilter_used(cm);
1855*77c1e3ccSAndroid Build Coastguard Worker   const int use_superres = av1_superres_scaled(cm);
1856*77c1e3ccSAndroid Build Coastguard Worker   const int use_cdef = is_cdef_used(cm);
1857*77c1e3ccSAndroid Build Coastguard Worker   const int use_restoration = is_restoration_used(cm);
1858*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
1859*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
1860*77c1e3ccSAndroid Build Coastguard Worker 
1861*77c1e3ccSAndroid Build Coastguard Worker   const unsigned int skip_apply_postproc_filters =
1862*77c1e3ccSAndroid Build Coastguard Worker       derive_skip_apply_postproc_filters(cpi, use_loopfilter, use_cdef,
1863*77c1e3ccSAndroid Build Coastguard Worker                                          use_superres, use_restoration);
1864*77c1e3ccSAndroid Build Coastguard Worker   mt_info->pipeline_lpf_mt_with_enc =
1865*77c1e3ccSAndroid Build Coastguard Worker       (cpi->oxcf.mode == REALTIME) && (cpi->oxcf.speed >= 5) &&
1866*77c1e3ccSAndroid Build Coastguard Worker       (cpi->sf.lpf_sf.lpf_pick == LPF_PICK_FROM_Q) &&
1867*77c1e3ccSAndroid Build Coastguard Worker       (cpi->oxcf.algo_cfg.loopfilter_control != LOOPFILTER_SELECTIVELY) &&
1868*77c1e3ccSAndroid Build Coastguard Worker       !cpi->ppi->rtc_ref.non_reference_frame && !cm->features.allow_intrabc &&
1869*77c1e3ccSAndroid Build Coastguard Worker       ((skip_apply_postproc_filters & SKIP_APPLY_LOOPFILTER) == 0);
1870*77c1e3ccSAndroid Build Coastguard Worker 
1871*77c1e3ccSAndroid Build Coastguard Worker   if (!mt_info->pipeline_lpf_mt_with_enc) return;
1872*77c1e3ccSAndroid Build Coastguard Worker 
1873*77c1e3ccSAndroid Build Coastguard Worker   set_postproc_filter_default_params(cm);
1874*77c1e3ccSAndroid Build Coastguard Worker 
1875*77c1e3ccSAndroid Build Coastguard Worker   if (!use_loopfilter) return;
1876*77c1e3ccSAndroid Build Coastguard Worker 
1877*77c1e3ccSAndroid Build Coastguard Worker   const LPF_PICK_METHOD method = cpi->sf.lpf_sf.lpf_pick;
1878*77c1e3ccSAndroid Build Coastguard Worker   assert(method == LPF_PICK_FROM_Q);
1879*77c1e3ccSAndroid Build Coastguard Worker   assert(cpi->oxcf.algo_cfg.loopfilter_control != LOOPFILTER_SELECTIVELY);
1880*77c1e3ccSAndroid Build Coastguard Worker 
1881*77c1e3ccSAndroid Build Coastguard Worker   av1_pick_filter_level(cpi->source, cpi, method);
1882*77c1e3ccSAndroid Build Coastguard Worker 
1883*77c1e3ccSAndroid Build Coastguard Worker   struct loopfilter *lf = &cm->lf;
1884*77c1e3ccSAndroid Build Coastguard Worker   const int plane_start = 0;
1885*77c1e3ccSAndroid Build Coastguard Worker   const int plane_end = av1_num_planes(cm);
1886*77c1e3ccSAndroid Build Coastguard Worker   int planes_to_lf[MAX_MB_PLANE];
1887*77c1e3ccSAndroid Build Coastguard Worker   if (lpf_mt_with_enc_enabled(cpi->mt_info.pipeline_lpf_mt_with_enc,
1888*77c1e3ccSAndroid Build Coastguard Worker                               lf->filter_level)) {
1889*77c1e3ccSAndroid Build Coastguard Worker     set_planes_to_loop_filter(lf, planes_to_lf, plane_start, plane_end);
1890*77c1e3ccSAndroid Build Coastguard Worker     int lpf_opt_level = get_lpf_opt_level(&cpi->sf);
1891*77c1e3ccSAndroid Build Coastguard Worker     assert(lpf_opt_level == 2);
1892*77c1e3ccSAndroid Build Coastguard Worker 
1893*77c1e3ccSAndroid Build Coastguard Worker     const int start_mi_row = 0;
1894*77c1e3ccSAndroid Build Coastguard Worker     const int end_mi_row = start_mi_row + cm->mi_params.mi_rows;
1895*77c1e3ccSAndroid Build Coastguard Worker 
1896*77c1e3ccSAndroid Build Coastguard Worker     av1_loop_filter_frame_init(cm, plane_start, plane_end);
1897*77c1e3ccSAndroid Build Coastguard Worker 
1898*77c1e3ccSAndroid Build Coastguard Worker     assert(mt_info->num_mod_workers[MOD_ENC] ==
1899*77c1e3ccSAndroid Build Coastguard Worker            mt_info->num_mod_workers[MOD_LPF]);
1900*77c1e3ccSAndroid Build Coastguard Worker     loop_filter_frame_mt_init(cm, start_mi_row, end_mi_row, planes_to_lf,
1901*77c1e3ccSAndroid Build Coastguard Worker                               mt_info->num_mod_workers[MOD_LPF],
1902*77c1e3ccSAndroid Build Coastguard Worker                               &mt_info->lf_row_sync, lpf_opt_level,
1903*77c1e3ccSAndroid Build Coastguard Worker                               cm->seq_params->mib_size_log2);
1904*77c1e3ccSAndroid Build Coastguard Worker 
1905*77c1e3ccSAndroid Build Coastguard Worker     for (int i = num_workers - 1; i >= 0; i--) {
1906*77c1e3ccSAndroid Build Coastguard Worker       EncWorkerData *const thread_data = &mt_info->tile_thr_data[i];
1907*77c1e3ccSAndroid Build Coastguard Worker       // Initialize loopfilter data
1908*77c1e3ccSAndroid Build Coastguard Worker       thread_data->lf_sync = &mt_info->lf_row_sync;
1909*77c1e3ccSAndroid Build Coastguard Worker       thread_data->lf_data = &thread_data->lf_sync->lfdata[i];
1910*77c1e3ccSAndroid Build Coastguard Worker       loop_filter_data_reset(thread_data->lf_data, &cm->cur_frame->buf, cm, xd);
1911*77c1e3ccSAndroid Build Coastguard Worker     }
1912*77c1e3ccSAndroid Build Coastguard Worker   }
1913*77c1e3ccSAndroid Build Coastguard Worker }
1914*77c1e3ccSAndroid Build Coastguard Worker 
av1_encode_tiles_row_mt(AV1_COMP * cpi)1915*77c1e3ccSAndroid Build Coastguard Worker void av1_encode_tiles_row_mt(AV1_COMP *cpi) {
1916*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1917*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
1918*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadInfo *const enc_row_mt = &mt_info->enc_row_mt;
1919*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
1920*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
1921*77c1e3ccSAndroid Build Coastguard Worker   const int sb_rows_in_frame = get_sb_rows_in_frame(cm);
1922*77c1e3ccSAndroid Build Coastguard Worker   int *thread_id_to_tile_id = enc_row_mt->thread_id_to_tile_id;
1923*77c1e3ccSAndroid Build Coastguard Worker   int max_sb_rows_in_tile = 0, max_sb_cols_in_tile = 0;
1924*77c1e3ccSAndroid Build Coastguard Worker   int num_workers = mt_info->num_mod_workers[MOD_ENC];
1925*77c1e3ccSAndroid Build Coastguard Worker 
1926*77c1e3ccSAndroid Build Coastguard Worker   compute_max_sb_rows_cols(cm, &max_sb_rows_in_tile, &max_sb_cols_in_tile);
1927*77c1e3ccSAndroid Build Coastguard Worker   const bool alloc_row_mt_mem =
1928*77c1e3ccSAndroid Build Coastguard Worker       (enc_row_mt->allocated_tile_cols != tile_cols ||
1929*77c1e3ccSAndroid Build Coastguard Worker        enc_row_mt->allocated_tile_rows != tile_rows ||
1930*77c1e3ccSAndroid Build Coastguard Worker        enc_row_mt->allocated_rows != max_sb_rows_in_tile ||
1931*77c1e3ccSAndroid Build Coastguard Worker        enc_row_mt->allocated_cols != (max_sb_cols_in_tile - 1) ||
1932*77c1e3ccSAndroid Build Coastguard Worker        enc_row_mt->allocated_sb_rows != sb_rows_in_frame);
1933*77c1e3ccSAndroid Build Coastguard Worker   const bool alloc_tile_data = cpi->allocated_tiles < tile_cols * tile_rows;
1934*77c1e3ccSAndroid Build Coastguard Worker 
1935*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(cpi->tile_data == NULL, alloc_tile_data));
1936*77c1e3ccSAndroid Build Coastguard Worker   if (alloc_tile_data) {
1937*77c1e3ccSAndroid Build Coastguard Worker     av1_alloc_tile_data(cpi);
1938*77c1e3ccSAndroid Build Coastguard Worker   }
1939*77c1e3ccSAndroid Build Coastguard Worker 
1940*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(alloc_tile_data, alloc_row_mt_mem));
1941*77c1e3ccSAndroid Build Coastguard Worker   if (alloc_row_mt_mem) {
1942*77c1e3ccSAndroid Build Coastguard Worker     row_mt_mem_alloc(cpi, max_sb_rows_in_tile, max_sb_cols_in_tile,
1943*77c1e3ccSAndroid Build Coastguard Worker                      cpi->oxcf.algo_cfg.cdf_update_mode);
1944*77c1e3ccSAndroid Build Coastguard Worker   }
1945*77c1e3ccSAndroid Build Coastguard Worker 
1946*77c1e3ccSAndroid Build Coastguard Worker   num_workers = AOMMIN(num_workers, mt_info->num_workers);
1947*77c1e3ccSAndroid Build Coastguard Worker   lpf_pipeline_mt_init(cpi, num_workers);
1948*77c1e3ccSAndroid Build Coastguard Worker 
1949*77c1e3ccSAndroid Build Coastguard Worker   av1_init_tile_data(cpi);
1950*77c1e3ccSAndroid Build Coastguard Worker 
1951*77c1e3ccSAndroid Build Coastguard Worker   memset(thread_id_to_tile_id, -1,
1952*77c1e3ccSAndroid Build Coastguard Worker          sizeof(*thread_id_to_tile_id) * MAX_NUM_THREADS);
1953*77c1e3ccSAndroid Build Coastguard Worker   memset(enc_row_mt->num_tile_cols_done, 0,
1954*77c1e3ccSAndroid Build Coastguard Worker          sizeof(*enc_row_mt->num_tile_cols_done) * sb_rows_in_frame);
1955*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->row_mt_exit = false;
1956*77c1e3ccSAndroid Build Coastguard Worker 
1957*77c1e3ccSAndroid Build Coastguard Worker   for (int tile_row = 0; tile_row < tile_rows; tile_row++) {
1958*77c1e3ccSAndroid Build Coastguard Worker     for (int tile_col = 0; tile_col < tile_cols; tile_col++) {
1959*77c1e3ccSAndroid Build Coastguard Worker       int tile_index = tile_row * tile_cols + tile_col;
1960*77c1e3ccSAndroid Build Coastguard Worker       TileDataEnc *const this_tile = &cpi->tile_data[tile_index];
1961*77c1e3ccSAndroid Build Coastguard Worker       AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
1962*77c1e3ccSAndroid Build Coastguard Worker 
1963*77c1e3ccSAndroid Build Coastguard Worker       // Initialize num_finished_cols to -1 for all rows.
1964*77c1e3ccSAndroid Build Coastguard Worker       memset(row_mt_sync->num_finished_cols, -1,
1965*77c1e3ccSAndroid Build Coastguard Worker              sizeof(*row_mt_sync->num_finished_cols) * max_sb_rows_in_tile);
1966*77c1e3ccSAndroid Build Coastguard Worker       row_mt_sync->next_mi_row = this_tile->tile_info.mi_row_start;
1967*77c1e3ccSAndroid Build Coastguard Worker       row_mt_sync->num_threads_working = 0;
1968*77c1e3ccSAndroid Build Coastguard Worker       row_mt_sync->intrabc_extra_top_right_sb_delay =
1969*77c1e3ccSAndroid Build Coastguard Worker           av1_get_intrabc_extra_top_right_sb_delay(cm);
1970*77c1e3ccSAndroid Build Coastguard Worker 
1971*77c1e3ccSAndroid Build Coastguard Worker       av1_inter_mode_data_init(this_tile);
1972*77c1e3ccSAndroid Build Coastguard Worker       av1_zero_above_context(cm, &cpi->td.mb.e_mbd,
1973*77c1e3ccSAndroid Build Coastguard Worker                              this_tile->tile_info.mi_col_start,
1974*77c1e3ccSAndroid Build Coastguard Worker                              this_tile->tile_info.mi_col_end, tile_row);
1975*77c1e3ccSAndroid Build Coastguard Worker     }
1976*77c1e3ccSAndroid Build Coastguard Worker   }
1977*77c1e3ccSAndroid Build Coastguard Worker 
1978*77c1e3ccSAndroid Build Coastguard Worker   assign_tile_to_thread(thread_id_to_tile_id, tile_cols * tile_rows,
1979*77c1e3ccSAndroid Build Coastguard Worker                         num_workers);
1980*77c1e3ccSAndroid Build Coastguard Worker   prepare_enc_workers(cpi, enc_row_mt_worker_hook, num_workers);
1981*77c1e3ccSAndroid Build Coastguard Worker   launch_workers(&cpi->mt_info, num_workers);
1982*77c1e3ccSAndroid Build Coastguard Worker   sync_enc_workers(&cpi->mt_info, cm, num_workers);
1983*77c1e3ccSAndroid Build Coastguard Worker   if (cm->delta_q_info.delta_lf_present_flag) update_delta_lf_for_row_mt(cpi);
1984*77c1e3ccSAndroid Build Coastguard Worker   accumulate_counters_enc_workers(cpi, num_workers);
1985*77c1e3ccSAndroid Build Coastguard Worker }
1986*77c1e3ccSAndroid Build Coastguard Worker 
1987*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
dealloc_thread_data_src_diff_buf(AV1_COMP * cpi,int num_workers)1988*77c1e3ccSAndroid Build Coastguard Worker static void dealloc_thread_data_src_diff_buf(AV1_COMP *cpi, int num_workers) {
1989*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; --i) {
1990*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *const thread_data = &cpi->mt_info.tile_thr_data[i];
1991*77c1e3ccSAndroid Build Coastguard Worker     if (thread_data->td != &cpi->td)
1992*77c1e3ccSAndroid Build Coastguard Worker       av1_dealloc_src_diff_buf(&thread_data->td->mb,
1993*77c1e3ccSAndroid Build Coastguard Worker                                av1_num_planes(&cpi->common));
1994*77c1e3ccSAndroid Build Coastguard Worker   }
1995*77c1e3ccSAndroid Build Coastguard Worker }
1996*77c1e3ccSAndroid Build Coastguard Worker 
av1_fp_encode_tiles_row_mt(AV1_COMP * cpi)1997*77c1e3ccSAndroid Build Coastguard Worker void av1_fp_encode_tiles_row_mt(AV1_COMP *cpi) {
1998*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
1999*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
2000*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadInfo *const enc_row_mt = &mt_info->enc_row_mt;
2001*77c1e3ccSAndroid Build Coastguard Worker   const int tile_cols = cm->tiles.cols;
2002*77c1e3ccSAndroid Build Coastguard Worker   const int tile_rows = cm->tiles.rows;
2003*77c1e3ccSAndroid Build Coastguard Worker   int *thread_id_to_tile_id = enc_row_mt->thread_id_to_tile_id;
2004*77c1e3ccSAndroid Build Coastguard Worker   int num_workers = 0;
2005*77c1e3ccSAndroid Build Coastguard Worker   int max_mb_rows = 0;
2006*77c1e3ccSAndroid Build Coastguard Worker 
2007*77c1e3ccSAndroid Build Coastguard Worker   max_mb_rows = fp_compute_max_mb_rows(cm, cpi->fp_block_size);
2008*77c1e3ccSAndroid Build Coastguard Worker   const bool alloc_row_mt_mem = enc_row_mt->allocated_tile_cols != tile_cols ||
2009*77c1e3ccSAndroid Build Coastguard Worker                                 enc_row_mt->allocated_tile_rows != tile_rows ||
2010*77c1e3ccSAndroid Build Coastguard Worker                                 enc_row_mt->allocated_rows != max_mb_rows;
2011*77c1e3ccSAndroid Build Coastguard Worker   const bool alloc_tile_data = cpi->allocated_tiles < tile_cols * tile_rows;
2012*77c1e3ccSAndroid Build Coastguard Worker 
2013*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(cpi->tile_data == NULL, alloc_tile_data));
2014*77c1e3ccSAndroid Build Coastguard Worker   if (alloc_tile_data) {
2015*77c1e3ccSAndroid Build Coastguard Worker     av1_alloc_tile_data(cpi);
2016*77c1e3ccSAndroid Build Coastguard Worker   }
2017*77c1e3ccSAndroid Build Coastguard Worker 
2018*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(alloc_tile_data, alloc_row_mt_mem));
2019*77c1e3ccSAndroid Build Coastguard Worker   if (alloc_row_mt_mem) {
2020*77c1e3ccSAndroid Build Coastguard Worker     row_mt_mem_alloc(cpi, max_mb_rows, -1, 0);
2021*77c1e3ccSAndroid Build Coastguard Worker   }
2022*77c1e3ccSAndroid Build Coastguard Worker 
2023*77c1e3ccSAndroid Build Coastguard Worker   av1_init_tile_data(cpi);
2024*77c1e3ccSAndroid Build Coastguard Worker 
2025*77c1e3ccSAndroid Build Coastguard Worker   // For pass = 1, compute the no. of workers needed. For single-pass encode
2026*77c1e3ccSAndroid Build Coastguard Worker   // (pass = 0), no. of workers are already computed.
2027*77c1e3ccSAndroid Build Coastguard Worker   if (mt_info->num_mod_workers[MOD_FP] == 0)
2028*77c1e3ccSAndroid Build Coastguard Worker     num_workers = av1_fp_compute_num_enc_workers(cpi);
2029*77c1e3ccSAndroid Build Coastguard Worker   else
2030*77c1e3ccSAndroid Build Coastguard Worker     num_workers = mt_info->num_mod_workers[MOD_FP];
2031*77c1e3ccSAndroid Build Coastguard Worker 
2032*77c1e3ccSAndroid Build Coastguard Worker   memset(thread_id_to_tile_id, -1,
2033*77c1e3ccSAndroid Build Coastguard Worker          sizeof(*thread_id_to_tile_id) * MAX_NUM_THREADS);
2034*77c1e3ccSAndroid Build Coastguard Worker   enc_row_mt->firstpass_mt_exit = false;
2035*77c1e3ccSAndroid Build Coastguard Worker 
2036*77c1e3ccSAndroid Build Coastguard Worker   for (int tile_row = 0; tile_row < tile_rows; tile_row++) {
2037*77c1e3ccSAndroid Build Coastguard Worker     for (int tile_col = 0; tile_col < tile_cols; tile_col++) {
2038*77c1e3ccSAndroid Build Coastguard Worker       int tile_index = tile_row * tile_cols + tile_col;
2039*77c1e3ccSAndroid Build Coastguard Worker       TileDataEnc *const this_tile = &cpi->tile_data[tile_index];
2040*77c1e3ccSAndroid Build Coastguard Worker       AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
2041*77c1e3ccSAndroid Build Coastguard Worker 
2042*77c1e3ccSAndroid Build Coastguard Worker       // Initialize num_finished_cols to -1 for all rows.
2043*77c1e3ccSAndroid Build Coastguard Worker       memset(row_mt_sync->num_finished_cols, -1,
2044*77c1e3ccSAndroid Build Coastguard Worker              sizeof(*row_mt_sync->num_finished_cols) * max_mb_rows);
2045*77c1e3ccSAndroid Build Coastguard Worker       row_mt_sync->next_mi_row = this_tile->tile_info.mi_row_start;
2046*77c1e3ccSAndroid Build Coastguard Worker       row_mt_sync->num_threads_working = 0;
2047*77c1e3ccSAndroid Build Coastguard Worker 
2048*77c1e3ccSAndroid Build Coastguard Worker       // intraBC mode is not evaluated during first-pass encoding. Hence, no
2049*77c1e3ccSAndroid Build Coastguard Worker       // additional top-right delay is required.
2050*77c1e3ccSAndroid Build Coastguard Worker       row_mt_sync->intrabc_extra_top_right_sb_delay = 0;
2051*77c1e3ccSAndroid Build Coastguard Worker     }
2052*77c1e3ccSAndroid Build Coastguard Worker   }
2053*77c1e3ccSAndroid Build Coastguard Worker 
2054*77c1e3ccSAndroid Build Coastguard Worker   num_workers = AOMMIN(num_workers, mt_info->num_workers);
2055*77c1e3ccSAndroid Build Coastguard Worker   assign_tile_to_thread(thread_id_to_tile_id, tile_cols * tile_rows,
2056*77c1e3ccSAndroid Build Coastguard Worker                         num_workers);
2057*77c1e3ccSAndroid Build Coastguard Worker   fp_prepare_enc_workers(cpi, fp_enc_row_mt_worker_hook, num_workers);
2058*77c1e3ccSAndroid Build Coastguard Worker   launch_workers(&cpi->mt_info, num_workers);
2059*77c1e3ccSAndroid Build Coastguard Worker   sync_enc_workers(&cpi->mt_info, cm, num_workers);
2060*77c1e3ccSAndroid Build Coastguard Worker   dealloc_thread_data_src_diff_buf(cpi, num_workers);
2061*77c1e3ccSAndroid Build Coastguard Worker }
2062*77c1e3ccSAndroid Build Coastguard Worker 
av1_tpl_row_mt_sync_read_dummy(AV1TplRowMultiThreadSync * tpl_mt_sync,int r,int c)2063*77c1e3ccSAndroid Build Coastguard Worker void av1_tpl_row_mt_sync_read_dummy(AV1TplRowMultiThreadSync *tpl_mt_sync,
2064*77c1e3ccSAndroid Build Coastguard Worker                                     int r, int c) {
2065*77c1e3ccSAndroid Build Coastguard Worker   (void)tpl_mt_sync;
2066*77c1e3ccSAndroid Build Coastguard Worker   (void)r;
2067*77c1e3ccSAndroid Build Coastguard Worker   (void)c;
2068*77c1e3ccSAndroid Build Coastguard Worker }
2069*77c1e3ccSAndroid Build Coastguard Worker 
av1_tpl_row_mt_sync_write_dummy(AV1TplRowMultiThreadSync * tpl_mt_sync,int r,int c,int cols)2070*77c1e3ccSAndroid Build Coastguard Worker void av1_tpl_row_mt_sync_write_dummy(AV1TplRowMultiThreadSync *tpl_mt_sync,
2071*77c1e3ccSAndroid Build Coastguard Worker                                      int r, int c, int cols) {
2072*77c1e3ccSAndroid Build Coastguard Worker   (void)tpl_mt_sync;
2073*77c1e3ccSAndroid Build Coastguard Worker   (void)r;
2074*77c1e3ccSAndroid Build Coastguard Worker   (void)c;
2075*77c1e3ccSAndroid Build Coastguard Worker   (void)cols;
2076*77c1e3ccSAndroid Build Coastguard Worker }
2077*77c1e3ccSAndroid Build Coastguard Worker 
av1_tpl_row_mt_sync_read(AV1TplRowMultiThreadSync * tpl_row_mt_sync,int r,int c)2078*77c1e3ccSAndroid Build Coastguard Worker void av1_tpl_row_mt_sync_read(AV1TplRowMultiThreadSync *tpl_row_mt_sync, int r,
2079*77c1e3ccSAndroid Build Coastguard Worker                               int c) {
2080*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2081*77c1e3ccSAndroid Build Coastguard Worker   int nsync = tpl_row_mt_sync->sync_range;
2082*77c1e3ccSAndroid Build Coastguard Worker 
2083*77c1e3ccSAndroid Build Coastguard Worker   if (r) {
2084*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_t *const mutex = &tpl_row_mt_sync->mutex_[r - 1];
2085*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(mutex);
2086*77c1e3ccSAndroid Build Coastguard Worker 
2087*77c1e3ccSAndroid Build Coastguard Worker     while (c > tpl_row_mt_sync->num_finished_cols[r - 1] - nsync)
2088*77c1e3ccSAndroid Build Coastguard Worker       pthread_cond_wait(&tpl_row_mt_sync->cond_[r - 1], mutex);
2089*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(mutex);
2090*77c1e3ccSAndroid Build Coastguard Worker   }
2091*77c1e3ccSAndroid Build Coastguard Worker #else
2092*77c1e3ccSAndroid Build Coastguard Worker   (void)tpl_row_mt_sync;
2093*77c1e3ccSAndroid Build Coastguard Worker   (void)r;
2094*77c1e3ccSAndroid Build Coastguard Worker   (void)c;
2095*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
2096*77c1e3ccSAndroid Build Coastguard Worker }
2097*77c1e3ccSAndroid Build Coastguard Worker 
av1_tpl_row_mt_sync_write(AV1TplRowMultiThreadSync * tpl_row_mt_sync,int r,int c,int cols)2098*77c1e3ccSAndroid Build Coastguard Worker void av1_tpl_row_mt_sync_write(AV1TplRowMultiThreadSync *tpl_row_mt_sync, int r,
2099*77c1e3ccSAndroid Build Coastguard Worker                                int c, int cols) {
2100*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2101*77c1e3ccSAndroid Build Coastguard Worker   int nsync = tpl_row_mt_sync->sync_range;
2102*77c1e3ccSAndroid Build Coastguard Worker   int cur;
2103*77c1e3ccSAndroid Build Coastguard Worker   // Only signal when there are enough encoded blocks for next row to run.
2104*77c1e3ccSAndroid Build Coastguard Worker   int sig = 1;
2105*77c1e3ccSAndroid Build Coastguard Worker 
2106*77c1e3ccSAndroid Build Coastguard Worker   if (c < cols - 1) {
2107*77c1e3ccSAndroid Build Coastguard Worker     cur = c;
2108*77c1e3ccSAndroid Build Coastguard Worker     if (c % nsync) sig = 0;
2109*77c1e3ccSAndroid Build Coastguard Worker   } else {
2110*77c1e3ccSAndroid Build Coastguard Worker     cur = cols + nsync;
2111*77c1e3ccSAndroid Build Coastguard Worker   }
2112*77c1e3ccSAndroid Build Coastguard Worker 
2113*77c1e3ccSAndroid Build Coastguard Worker   if (sig) {
2114*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(&tpl_row_mt_sync->mutex_[r]);
2115*77c1e3ccSAndroid Build Coastguard Worker 
2116*77c1e3ccSAndroid Build Coastguard Worker     // When a thread encounters an error, num_finished_cols[r] is set to maximum
2117*77c1e3ccSAndroid Build Coastguard Worker     // column number. In this case, the AOMMAX operation here ensures that
2118*77c1e3ccSAndroid Build Coastguard Worker     // num_finished_cols[r] is not overwritten with a smaller value thus
2119*77c1e3ccSAndroid Build Coastguard Worker     // preventing the infinite waiting of threads in the relevant sync_read()
2120*77c1e3ccSAndroid Build Coastguard Worker     // function.
2121*77c1e3ccSAndroid Build Coastguard Worker     tpl_row_mt_sync->num_finished_cols[r] =
2122*77c1e3ccSAndroid Build Coastguard Worker         AOMMAX(tpl_row_mt_sync->num_finished_cols[r], cur);
2123*77c1e3ccSAndroid Build Coastguard Worker 
2124*77c1e3ccSAndroid Build Coastguard Worker     pthread_cond_signal(&tpl_row_mt_sync->cond_[r]);
2125*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(&tpl_row_mt_sync->mutex_[r]);
2126*77c1e3ccSAndroid Build Coastguard Worker   }
2127*77c1e3ccSAndroid Build Coastguard Worker #else
2128*77c1e3ccSAndroid Build Coastguard Worker   (void)tpl_row_mt_sync;
2129*77c1e3ccSAndroid Build Coastguard Worker   (void)r;
2130*77c1e3ccSAndroid Build Coastguard Worker   (void)c;
2131*77c1e3ccSAndroid Build Coastguard Worker   (void)cols;
2132*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
2133*77c1e3ccSAndroid Build Coastguard Worker }
2134*77c1e3ccSAndroid Build Coastguard Worker 
set_mode_estimation_done(AV1_COMP * cpi)2135*77c1e3ccSAndroid Build Coastguard Worker static inline void set_mode_estimation_done(AV1_COMP *cpi) {
2136*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
2137*77c1e3ccSAndroid Build Coastguard Worker   TplParams *const tpl_data = &cpi->ppi->tpl_data;
2138*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize =
2139*77c1e3ccSAndroid Build Coastguard Worker       convert_length_to_bsize(cpi->ppi->tpl_data.tpl_bsize_1d);
2140*77c1e3ccSAndroid Build Coastguard Worker   const int mi_height = mi_size_high[bsize];
2141*77c1e3ccSAndroid Build Coastguard Worker   AV1TplRowMultiThreadInfo *const tpl_row_mt = &cpi->mt_info.tpl_row_mt;
2142*77c1e3ccSAndroid Build Coastguard Worker   const int tplb_cols_in_tile =
2143*77c1e3ccSAndroid Build Coastguard Worker       ROUND_POWER_OF_TWO(mi_params->mi_cols, mi_size_wide_log2[bsize]);
2144*77c1e3ccSAndroid Build Coastguard Worker   // In case of tpl row-multithreading, due to top-right dependency, the worker
2145*77c1e3ccSAndroid Build Coastguard Worker   // on an mb_row waits for the completion of the tpl processing of the top and
2146*77c1e3ccSAndroid Build Coastguard Worker   // top-right blocks. Hence, in case a thread (main/worker) encounters an
2147*77c1e3ccSAndroid Build Coastguard Worker   // error, update that the tpl processing of every mb_row in the frame is
2148*77c1e3ccSAndroid Build Coastguard Worker   // complete in order to avoid dependent workers waiting indefinitely.
2149*77c1e3ccSAndroid Build Coastguard Worker   for (int mi_row = 0, tplb_row = 0; mi_row < mi_params->mi_rows;
2150*77c1e3ccSAndroid Build Coastguard Worker        mi_row += mi_height, tplb_row++) {
2151*77c1e3ccSAndroid Build Coastguard Worker     (*tpl_row_mt->sync_write_ptr)(&tpl_data->tpl_mt_sync, tplb_row,
2152*77c1e3ccSAndroid Build Coastguard Worker                                   tplb_cols_in_tile - 1, tplb_cols_in_tile);
2153*77c1e3ccSAndroid Build Coastguard Worker   }
2154*77c1e3ccSAndroid Build Coastguard Worker }
2155*77c1e3ccSAndroid Build Coastguard Worker 
2156*77c1e3ccSAndroid Build Coastguard Worker // Each worker calls tpl_worker_hook() and computes the tpl data.
tpl_worker_hook(void * arg1,void * unused)2157*77c1e3ccSAndroid Build Coastguard Worker static int tpl_worker_hook(void *arg1, void *unused) {
2158*77c1e3ccSAndroid Build Coastguard Worker   (void)unused;
2159*77c1e3ccSAndroid Build Coastguard Worker   EncWorkerData *thread_data = (EncWorkerData *)arg1;
2160*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMP *cpi = thread_data->cpi;
2161*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *cm = &cpi->common;
2162*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *x = &thread_data->td->mb;
2163*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *xd = &x->e_mbd;
2164*77c1e3ccSAndroid Build Coastguard Worker   TplTxfmStats *tpl_txfm_stats = &thread_data->td->tpl_txfm_stats;
2165*77c1e3ccSAndroid Build Coastguard Worker   TplBuffers *tpl_tmp_buffers = &thread_data->td->tpl_tmp_buffers;
2166*77c1e3ccSAndroid Build Coastguard Worker   CommonModeInfoParams *mi_params = &cm->mi_params;
2167*77c1e3ccSAndroid Build Coastguard Worker   int num_active_workers = cpi->ppi->tpl_data.tpl_mt_sync.num_threads_working;
2168*77c1e3ccSAndroid Build Coastguard Worker 
2169*77c1e3ccSAndroid Build Coastguard Worker   struct aom_internal_error_info *const error_info = &thread_data->error_info;
2170*77c1e3ccSAndroid Build Coastguard Worker   xd->error_info = error_info;
2171*77c1e3ccSAndroid Build Coastguard Worker   AV1TplRowMultiThreadInfo *const tpl_row_mt = &cpi->mt_info.tpl_row_mt;
2172*77c1e3ccSAndroid Build Coastguard Worker   (void)tpl_row_mt;
2173*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2174*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *tpl_error_mutex_ = tpl_row_mt->mutex_;
2175*77c1e3ccSAndroid Build Coastguard Worker #endif
2176*77c1e3ccSAndroid Build Coastguard Worker 
2177*77c1e3ccSAndroid Build Coastguard Worker   // The jmp_buf is valid only for the duration of the function that calls
2178*77c1e3ccSAndroid Build Coastguard Worker   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2179*77c1e3ccSAndroid Build Coastguard Worker   // before it returns.
2180*77c1e3ccSAndroid Build Coastguard Worker   if (setjmp(error_info->jmp)) {
2181*77c1e3ccSAndroid Build Coastguard Worker     error_info->setjmp = 0;
2182*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2183*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(tpl_error_mutex_);
2184*77c1e3ccSAndroid Build Coastguard Worker     tpl_row_mt->tpl_mt_exit = true;
2185*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(tpl_error_mutex_);
2186*77c1e3ccSAndroid Build Coastguard Worker #endif
2187*77c1e3ccSAndroid Build Coastguard Worker     set_mode_estimation_done(cpi);
2188*77c1e3ccSAndroid Build Coastguard Worker     return 0;
2189*77c1e3ccSAndroid Build Coastguard Worker   }
2190*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 1;
2191*77c1e3ccSAndroid Build Coastguard Worker 
2192*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE bsize = convert_length_to_bsize(cpi->ppi->tpl_data.tpl_bsize_1d);
2193*77c1e3ccSAndroid Build Coastguard Worker   TX_SIZE tx_size = max_txsize_lookup[bsize];
2194*77c1e3ccSAndroid Build Coastguard Worker   int mi_height = mi_size_high[bsize];
2195*77c1e3ccSAndroid Build Coastguard Worker 
2196*77c1e3ccSAndroid Build Coastguard Worker   av1_init_tpl_txfm_stats(tpl_txfm_stats);
2197*77c1e3ccSAndroid Build Coastguard Worker 
2198*77c1e3ccSAndroid Build Coastguard Worker   for (int mi_row = thread_data->start * mi_height; mi_row < mi_params->mi_rows;
2199*77c1e3ccSAndroid Build Coastguard Worker        mi_row += num_active_workers * mi_height) {
2200*77c1e3ccSAndroid Build Coastguard Worker     // Motion estimation row boundary
2201*77c1e3ccSAndroid Build Coastguard Worker     av1_set_mv_row_limits(mi_params, &x->mv_limits, mi_row, mi_height,
2202*77c1e3ccSAndroid Build Coastguard Worker                           cpi->oxcf.border_in_pixels);
2203*77c1e3ccSAndroid Build Coastguard Worker     xd->mb_to_top_edge = -GET_MV_SUBPEL(mi_row * MI_SIZE);
2204*77c1e3ccSAndroid Build Coastguard Worker     xd->mb_to_bottom_edge =
2205*77c1e3ccSAndroid Build Coastguard Worker         GET_MV_SUBPEL((mi_params->mi_rows - mi_height - mi_row) * MI_SIZE);
2206*77c1e3ccSAndroid Build Coastguard Worker     av1_mc_flow_dispenser_row(cpi, tpl_txfm_stats, tpl_tmp_buffers, x, mi_row,
2207*77c1e3ccSAndroid Build Coastguard Worker                               bsize, tx_size);
2208*77c1e3ccSAndroid Build Coastguard Worker   }
2209*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 0;
2210*77c1e3ccSAndroid Build Coastguard Worker   return 1;
2211*77c1e3ccSAndroid Build Coastguard Worker }
2212*77c1e3ccSAndroid Build Coastguard Worker 
2213*77c1e3ccSAndroid Build Coastguard Worker // Deallocate tpl synchronization related mutex and data.
av1_tpl_dealloc(AV1TplRowMultiThreadSync * tpl_sync)2214*77c1e3ccSAndroid Build Coastguard Worker void av1_tpl_dealloc(AV1TplRowMultiThreadSync *tpl_sync) {
2215*77c1e3ccSAndroid Build Coastguard Worker   assert(tpl_sync != NULL);
2216*77c1e3ccSAndroid Build Coastguard Worker 
2217*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2218*77c1e3ccSAndroid Build Coastguard Worker   if (tpl_sync->mutex_ != NULL) {
2219*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < tpl_sync->rows; ++i)
2220*77c1e3ccSAndroid Build Coastguard Worker       pthread_mutex_destroy(&tpl_sync->mutex_[i]);
2221*77c1e3ccSAndroid Build Coastguard Worker     aom_free(tpl_sync->mutex_);
2222*77c1e3ccSAndroid Build Coastguard Worker   }
2223*77c1e3ccSAndroid Build Coastguard Worker   if (tpl_sync->cond_ != NULL) {
2224*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < tpl_sync->rows; ++i)
2225*77c1e3ccSAndroid Build Coastguard Worker       pthread_cond_destroy(&tpl_sync->cond_[i]);
2226*77c1e3ccSAndroid Build Coastguard Worker     aom_free(tpl_sync->cond_);
2227*77c1e3ccSAndroid Build Coastguard Worker   }
2228*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
2229*77c1e3ccSAndroid Build Coastguard Worker 
2230*77c1e3ccSAndroid Build Coastguard Worker   aom_free(tpl_sync->num_finished_cols);
2231*77c1e3ccSAndroid Build Coastguard Worker   // clear the structure as the source of this call may be a resize in which
2232*77c1e3ccSAndroid Build Coastguard Worker   // case this call will be followed by an _alloc() which may fail.
2233*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(*tpl_sync);
2234*77c1e3ccSAndroid Build Coastguard Worker }
2235*77c1e3ccSAndroid Build Coastguard Worker 
2236*77c1e3ccSAndroid Build Coastguard Worker // Allocate memory for tpl row synchronization.
av1_tpl_alloc(AV1TplRowMultiThreadSync * tpl_sync,AV1_COMMON * cm,int mb_rows)2237*77c1e3ccSAndroid Build Coastguard Worker static void av1_tpl_alloc(AV1TplRowMultiThreadSync *tpl_sync, AV1_COMMON *cm,
2238*77c1e3ccSAndroid Build Coastguard Worker                           int mb_rows) {
2239*77c1e3ccSAndroid Build Coastguard Worker   tpl_sync->rows = mb_rows;
2240*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2241*77c1e3ccSAndroid Build Coastguard Worker   {
2242*77c1e3ccSAndroid Build Coastguard Worker     CHECK_MEM_ERROR(cm, tpl_sync->mutex_,
2243*77c1e3ccSAndroid Build Coastguard Worker                     aom_malloc(sizeof(*tpl_sync->mutex_) * mb_rows));
2244*77c1e3ccSAndroid Build Coastguard Worker     if (tpl_sync->mutex_) {
2245*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < mb_rows; ++i)
2246*77c1e3ccSAndroid Build Coastguard Worker         pthread_mutex_init(&tpl_sync->mutex_[i], NULL);
2247*77c1e3ccSAndroid Build Coastguard Worker     }
2248*77c1e3ccSAndroid Build Coastguard Worker 
2249*77c1e3ccSAndroid Build Coastguard Worker     CHECK_MEM_ERROR(cm, tpl_sync->cond_,
2250*77c1e3ccSAndroid Build Coastguard Worker                     aom_malloc(sizeof(*tpl_sync->cond_) * mb_rows));
2251*77c1e3ccSAndroid Build Coastguard Worker     if (tpl_sync->cond_) {
2252*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < mb_rows; ++i)
2253*77c1e3ccSAndroid Build Coastguard Worker         pthread_cond_init(&tpl_sync->cond_[i], NULL);
2254*77c1e3ccSAndroid Build Coastguard Worker     }
2255*77c1e3ccSAndroid Build Coastguard Worker   }
2256*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
2257*77c1e3ccSAndroid Build Coastguard Worker   CHECK_MEM_ERROR(cm, tpl_sync->num_finished_cols,
2258*77c1e3ccSAndroid Build Coastguard Worker                   aom_malloc(sizeof(*tpl_sync->num_finished_cols) * mb_rows));
2259*77c1e3ccSAndroid Build Coastguard Worker 
2260*77c1e3ccSAndroid Build Coastguard Worker   // Set up nsync.
2261*77c1e3ccSAndroid Build Coastguard Worker   tpl_sync->sync_range = 1;
2262*77c1e3ccSAndroid Build Coastguard Worker }
2263*77c1e3ccSAndroid Build Coastguard Worker 
2264*77c1e3ccSAndroid Build Coastguard Worker // Each worker is prepared by assigning the hook function and individual thread
2265*77c1e3ccSAndroid Build Coastguard Worker // data.
prepare_tpl_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)2266*77c1e3ccSAndroid Build Coastguard Worker static inline void prepare_tpl_workers(AV1_COMP *cpi, AVxWorkerHook hook,
2267*77c1e3ccSAndroid Build Coastguard Worker                                        int num_workers) {
2268*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *mt_info = &cpi->mt_info;
2269*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
2270*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *worker = &mt_info->workers[i];
2271*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *thread_data = &mt_info->tile_thr_data[i];
2272*77c1e3ccSAndroid Build Coastguard Worker 
2273*77c1e3ccSAndroid Build Coastguard Worker     worker->hook = hook;
2274*77c1e3ccSAndroid Build Coastguard Worker     worker->data1 = thread_data;
2275*77c1e3ccSAndroid Build Coastguard Worker     worker->data2 = NULL;
2276*77c1e3ccSAndroid Build Coastguard Worker 
2277*77c1e3ccSAndroid Build Coastguard Worker     thread_data->thread_id = i;
2278*77c1e3ccSAndroid Build Coastguard Worker     // Set the starting tile for each thread.
2279*77c1e3ccSAndroid Build Coastguard Worker     thread_data->start = i;
2280*77c1e3ccSAndroid Build Coastguard Worker 
2281*77c1e3ccSAndroid Build Coastguard Worker     thread_data->cpi = cpi;
2282*77c1e3ccSAndroid Build Coastguard Worker     if (i == 0) {
2283*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td = &cpi->td;
2284*77c1e3ccSAndroid Build Coastguard Worker     } else {
2285*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td = thread_data->original_td;
2286*77c1e3ccSAndroid Build Coastguard Worker     }
2287*77c1e3ccSAndroid Build Coastguard Worker 
2288*77c1e3ccSAndroid Build Coastguard Worker     // Before encoding a frame, copy the thread data from cpi.
2289*77c1e3ccSAndroid Build Coastguard Worker     if (thread_data->td != &cpi->td) {
2290*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->mb = cpi->td.mb;
2291*77c1e3ccSAndroid Build Coastguard Worker       // OBMC buffers are used only to init MS params and remain unused when
2292*77c1e3ccSAndroid Build Coastguard Worker       // called from tpl, hence set the buffers to defaults.
2293*77c1e3ccSAndroid Build Coastguard Worker       av1_init_obmc_buffer(&thread_data->td->mb.obmc_buffer);
2294*77c1e3ccSAndroid Build Coastguard Worker       if (!tpl_alloc_temp_buffers(&thread_data->td->tpl_tmp_buffers,
2295*77c1e3ccSAndroid Build Coastguard Worker                                   cpi->ppi->tpl_data.tpl_bsize_1d)) {
2296*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(cpi->common.error, AOM_CODEC_MEM_ERROR,
2297*77c1e3ccSAndroid Build Coastguard Worker                            "Error allocating tpl data");
2298*77c1e3ccSAndroid Build Coastguard Worker       }
2299*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->mb.tmp_conv_dst = thread_data->td->tmp_conv_dst;
2300*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->mb.e_mbd.tmp_conv_dst = thread_data->td->mb.tmp_conv_dst;
2301*77c1e3ccSAndroid Build Coastguard Worker     }
2302*77c1e3ccSAndroid Build Coastguard Worker   }
2303*77c1e3ccSAndroid Build Coastguard Worker }
2304*77c1e3ccSAndroid Build Coastguard Worker 
2305*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITRATE_ACCURACY
2306*77c1e3ccSAndroid Build Coastguard Worker // Accumulate transform stats after tpl.
tpl_accumulate_txfm_stats(ThreadData * main_td,const MultiThreadInfo * mt_info,int num_workers)2307*77c1e3ccSAndroid Build Coastguard Worker static void tpl_accumulate_txfm_stats(ThreadData *main_td,
2308*77c1e3ccSAndroid Build Coastguard Worker                                       const MultiThreadInfo *mt_info,
2309*77c1e3ccSAndroid Build Coastguard Worker                                       int num_workers) {
2310*77c1e3ccSAndroid Build Coastguard Worker   TplTxfmStats *accumulated_stats = &main_td->tpl_txfm_stats;
2311*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
2312*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *const worker = &mt_info->workers[i];
2313*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
2314*77c1e3ccSAndroid Build Coastguard Worker     ThreadData *td = thread_data->td;
2315*77c1e3ccSAndroid Build Coastguard Worker     if (td != main_td) {
2316*77c1e3ccSAndroid Build Coastguard Worker       const TplTxfmStats *tpl_txfm_stats = &td->tpl_txfm_stats;
2317*77c1e3ccSAndroid Build Coastguard Worker       av1_accumulate_tpl_txfm_stats(tpl_txfm_stats, accumulated_stats);
2318*77c1e3ccSAndroid Build Coastguard Worker     }
2319*77c1e3ccSAndroid Build Coastguard Worker   }
2320*77c1e3ccSAndroid Build Coastguard Worker }
2321*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_BITRATE_ACCURACY
2322*77c1e3ccSAndroid Build Coastguard Worker 
2323*77c1e3ccSAndroid Build Coastguard Worker // Implements multi-threading for tpl.
av1_mc_flow_dispenser_mt(AV1_COMP * cpi)2324*77c1e3ccSAndroid Build Coastguard Worker void av1_mc_flow_dispenser_mt(AV1_COMP *cpi) {
2325*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *cm = &cpi->common;
2326*77c1e3ccSAndroid Build Coastguard Worker   CommonModeInfoParams *mi_params = &cm->mi_params;
2327*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *mt_info = &cpi->mt_info;
2328*77c1e3ccSAndroid Build Coastguard Worker   TplParams *tpl_data = &cpi->ppi->tpl_data;
2329*77c1e3ccSAndroid Build Coastguard Worker   AV1TplRowMultiThreadSync *tpl_sync = &tpl_data->tpl_mt_sync;
2330*77c1e3ccSAndroid Build Coastguard Worker   int mb_rows = mi_params->mb_rows;
2331*77c1e3ccSAndroid Build Coastguard Worker   int num_workers =
2332*77c1e3ccSAndroid Build Coastguard Worker       AOMMIN(mt_info->num_mod_workers[MOD_TPL], mt_info->num_workers);
2333*77c1e3ccSAndroid Build Coastguard Worker 
2334*77c1e3ccSAndroid Build Coastguard Worker   if (mb_rows != tpl_sync->rows) {
2335*77c1e3ccSAndroid Build Coastguard Worker     av1_tpl_dealloc(tpl_sync);
2336*77c1e3ccSAndroid Build Coastguard Worker     av1_tpl_alloc(tpl_sync, cm, mb_rows);
2337*77c1e3ccSAndroid Build Coastguard Worker   }
2338*77c1e3ccSAndroid Build Coastguard Worker   tpl_sync->num_threads_working = num_workers;
2339*77c1e3ccSAndroid Build Coastguard Worker   mt_info->tpl_row_mt.tpl_mt_exit = false;
2340*77c1e3ccSAndroid Build Coastguard Worker 
2341*77c1e3ccSAndroid Build Coastguard Worker   // Initialize cur_mb_col to -1 for all MB rows.
2342*77c1e3ccSAndroid Build Coastguard Worker   memset(tpl_sync->num_finished_cols, -1,
2343*77c1e3ccSAndroid Build Coastguard Worker          sizeof(*tpl_sync->num_finished_cols) * mb_rows);
2344*77c1e3ccSAndroid Build Coastguard Worker 
2345*77c1e3ccSAndroid Build Coastguard Worker   prepare_tpl_workers(cpi, tpl_worker_hook, num_workers);
2346*77c1e3ccSAndroid Build Coastguard Worker   launch_workers(&cpi->mt_info, num_workers);
2347*77c1e3ccSAndroid Build Coastguard Worker   sync_enc_workers(&cpi->mt_info, cm, num_workers);
2348*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITRATE_ACCURACY
2349*77c1e3ccSAndroid Build Coastguard Worker   tpl_accumulate_txfm_stats(&cpi->td, &cpi->mt_info, num_workers);
2350*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_BITRATE_ACCURACY
2351*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
2352*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *thread_data = &mt_info->tile_thr_data[i];
2353*77c1e3ccSAndroid Build Coastguard Worker     ThreadData *td = thread_data->td;
2354*77c1e3ccSAndroid Build Coastguard Worker     if (td != &cpi->td) tpl_dealloc_temp_buffers(&td->tpl_tmp_buffers);
2355*77c1e3ccSAndroid Build Coastguard Worker   }
2356*77c1e3ccSAndroid Build Coastguard Worker }
2357*77c1e3ccSAndroid Build Coastguard Worker 
2358*77c1e3ccSAndroid Build Coastguard Worker // Deallocate memory for temporal filter multi-thread synchronization.
av1_tf_mt_dealloc(AV1TemporalFilterSync * tf_sync)2359*77c1e3ccSAndroid Build Coastguard Worker void av1_tf_mt_dealloc(AV1TemporalFilterSync *tf_sync) {
2360*77c1e3ccSAndroid Build Coastguard Worker   assert(tf_sync != NULL);
2361*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2362*77c1e3ccSAndroid Build Coastguard Worker   if (tf_sync->mutex_ != NULL) {
2363*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_destroy(tf_sync->mutex_);
2364*77c1e3ccSAndroid Build Coastguard Worker     aom_free(tf_sync->mutex_);
2365*77c1e3ccSAndroid Build Coastguard Worker   }
2366*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
2367*77c1e3ccSAndroid Build Coastguard Worker   tf_sync->next_tf_row = 0;
2368*77c1e3ccSAndroid Build Coastguard Worker }
2369*77c1e3ccSAndroid Build Coastguard Worker 
2370*77c1e3ccSAndroid Build Coastguard Worker // Checks if a job is available. If job is available,
2371*77c1e3ccSAndroid Build Coastguard Worker // 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)2372*77c1e3ccSAndroid Build Coastguard Worker static inline int tf_get_next_job(AV1TemporalFilterSync *tf_mt_sync,
2373*77c1e3ccSAndroid Build Coastguard Worker                                   int *current_mb_row, int mb_rows) {
2374*77c1e3ccSAndroid Build Coastguard Worker   int do_next_row = 0;
2375*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2376*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *tf_mutex_ = tf_mt_sync->mutex_;
2377*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_lock(tf_mutex_);
2378*77c1e3ccSAndroid Build Coastguard Worker #endif
2379*77c1e3ccSAndroid Build Coastguard Worker   if (!tf_mt_sync->tf_mt_exit && tf_mt_sync->next_tf_row < mb_rows) {
2380*77c1e3ccSAndroid Build Coastguard Worker     *current_mb_row = tf_mt_sync->next_tf_row;
2381*77c1e3ccSAndroid Build Coastguard Worker     tf_mt_sync->next_tf_row++;
2382*77c1e3ccSAndroid Build Coastguard Worker     do_next_row = 1;
2383*77c1e3ccSAndroid Build Coastguard Worker   }
2384*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2385*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_unlock(tf_mutex_);
2386*77c1e3ccSAndroid Build Coastguard Worker #endif
2387*77c1e3ccSAndroid Build Coastguard Worker   return do_next_row;
2388*77c1e3ccSAndroid Build Coastguard Worker }
2389*77c1e3ccSAndroid Build Coastguard Worker 
2390*77c1e3ccSAndroid Build Coastguard Worker // Hook function for each thread in temporal filter multi-threading.
tf_worker_hook(void * arg1,void * unused)2391*77c1e3ccSAndroid Build Coastguard Worker static int tf_worker_hook(void *arg1, void *unused) {
2392*77c1e3ccSAndroid Build Coastguard Worker   (void)unused;
2393*77c1e3ccSAndroid Build Coastguard Worker   EncWorkerData *thread_data = (EncWorkerData *)arg1;
2394*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMP *cpi = thread_data->cpi;
2395*77c1e3ccSAndroid Build Coastguard Worker   ThreadData *td = thread_data->td;
2396*77c1e3ccSAndroid Build Coastguard Worker   TemporalFilterCtx *tf_ctx = &cpi->tf_ctx;
2397*77c1e3ccSAndroid Build Coastguard Worker   AV1TemporalFilterSync *tf_sync = &cpi->mt_info.tf_sync;
2398*77c1e3ccSAndroid Build Coastguard Worker   const struct scale_factors *scale = &cpi->tf_ctx.sf;
2399*77c1e3ccSAndroid Build Coastguard Worker 
2400*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2401*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *tf_mutex_ = tf_sync->mutex_;
2402*77c1e3ccSAndroid Build Coastguard Worker #endif
2403*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &thread_data->td->mb.e_mbd;
2404*77c1e3ccSAndroid Build Coastguard Worker   struct aom_internal_error_info *const error_info = &thread_data->error_info;
2405*77c1e3ccSAndroid Build Coastguard Worker   xd->error_info = error_info;
2406*77c1e3ccSAndroid Build Coastguard Worker 
2407*77c1e3ccSAndroid Build Coastguard Worker   // The jmp_buf is valid only for the duration of the function that calls
2408*77c1e3ccSAndroid Build Coastguard Worker   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2409*77c1e3ccSAndroid Build Coastguard Worker   // before it returns.
2410*77c1e3ccSAndroid Build Coastguard Worker   if (setjmp(error_info->jmp)) {
2411*77c1e3ccSAndroid Build Coastguard Worker     error_info->setjmp = 0;
2412*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2413*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(tf_mutex_);
2414*77c1e3ccSAndroid Build Coastguard Worker     tf_sync->tf_mt_exit = true;
2415*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(tf_mutex_);
2416*77c1e3ccSAndroid Build Coastguard Worker #endif
2417*77c1e3ccSAndroid Build Coastguard Worker     return 0;
2418*77c1e3ccSAndroid Build Coastguard Worker   }
2419*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 1;
2420*77c1e3ccSAndroid Build Coastguard Worker 
2421*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(&cpi->common);
2422*77c1e3ccSAndroid Build Coastguard Worker   assert(num_planes >= 1 && num_planes <= MAX_MB_PLANE);
2423*77c1e3ccSAndroid Build Coastguard Worker 
2424*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *mbd = &td->mb.e_mbd;
2425*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *input_buffer[MAX_MB_PLANE];
2426*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO **input_mb_mode_info;
2427*77c1e3ccSAndroid Build Coastguard Worker   tf_save_state(mbd, &input_mb_mode_info, input_buffer, num_planes);
2428*77c1e3ccSAndroid Build Coastguard Worker   tf_setup_macroblockd(mbd, &td->tf_data, scale);
2429*77c1e3ccSAndroid Build Coastguard Worker 
2430*77c1e3ccSAndroid Build Coastguard Worker   int current_mb_row = -1;
2431*77c1e3ccSAndroid Build Coastguard Worker 
2432*77c1e3ccSAndroid Build Coastguard Worker   while (tf_get_next_job(tf_sync, &current_mb_row, tf_ctx->mb_rows))
2433*77c1e3ccSAndroid Build Coastguard Worker     av1_tf_do_filtering_row(cpi, td, current_mb_row);
2434*77c1e3ccSAndroid Build Coastguard Worker 
2435*77c1e3ccSAndroid Build Coastguard Worker   tf_restore_state(mbd, input_mb_mode_info, input_buffer, num_planes);
2436*77c1e3ccSAndroid Build Coastguard Worker 
2437*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 0;
2438*77c1e3ccSAndroid Build Coastguard Worker   return 1;
2439*77c1e3ccSAndroid Build Coastguard Worker }
2440*77c1e3ccSAndroid Build Coastguard Worker 
2441*77c1e3ccSAndroid Build Coastguard Worker // 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)2442*77c1e3ccSAndroid Build Coastguard Worker static void prepare_tf_workers(AV1_COMP *cpi, AVxWorkerHook hook,
2443*77c1e3ccSAndroid Build Coastguard Worker                                int num_workers, int is_highbitdepth) {
2444*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *mt_info = &cpi->mt_info;
2445*77c1e3ccSAndroid Build Coastguard Worker   mt_info->tf_sync.next_tf_row = 0;
2446*77c1e3ccSAndroid Build Coastguard Worker   mt_info->tf_sync.tf_mt_exit = false;
2447*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
2448*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *worker = &mt_info->workers[i];
2449*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *thread_data = &mt_info->tile_thr_data[i];
2450*77c1e3ccSAndroid Build Coastguard Worker 
2451*77c1e3ccSAndroid Build Coastguard Worker     worker->hook = hook;
2452*77c1e3ccSAndroid Build Coastguard Worker     worker->data1 = thread_data;
2453*77c1e3ccSAndroid Build Coastguard Worker     worker->data2 = NULL;
2454*77c1e3ccSAndroid Build Coastguard Worker 
2455*77c1e3ccSAndroid Build Coastguard Worker     thread_data->thread_id = i;
2456*77c1e3ccSAndroid Build Coastguard Worker     // Set the starting tile for each thread.
2457*77c1e3ccSAndroid Build Coastguard Worker     thread_data->start = i;
2458*77c1e3ccSAndroid Build Coastguard Worker 
2459*77c1e3ccSAndroid Build Coastguard Worker     thread_data->cpi = cpi;
2460*77c1e3ccSAndroid Build Coastguard Worker     if (i == 0) {
2461*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td = &cpi->td;
2462*77c1e3ccSAndroid Build Coastguard Worker     } else {
2463*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td = thread_data->original_td;
2464*77c1e3ccSAndroid Build Coastguard Worker     }
2465*77c1e3ccSAndroid Build Coastguard Worker 
2466*77c1e3ccSAndroid Build Coastguard Worker     // Before encoding a frame, copy the thread data from cpi.
2467*77c1e3ccSAndroid Build Coastguard Worker     if (thread_data->td != &cpi->td) {
2468*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->mb = cpi->td.mb;
2469*77c1e3ccSAndroid Build Coastguard Worker       // OBMC buffers are used only to init MS params and remain unused when
2470*77c1e3ccSAndroid Build Coastguard Worker       // called from tf, hence set the buffers to defaults.
2471*77c1e3ccSAndroid Build Coastguard Worker       av1_init_obmc_buffer(&thread_data->td->mb.obmc_buffer);
2472*77c1e3ccSAndroid Build Coastguard Worker       if (!tf_alloc_and_reset_data(&thread_data->td->tf_data,
2473*77c1e3ccSAndroid Build Coastguard Worker                                    cpi->tf_ctx.num_pels, is_highbitdepth)) {
2474*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(cpi->common.error, AOM_CODEC_MEM_ERROR,
2475*77c1e3ccSAndroid Build Coastguard Worker                            "Error allocating temporal filter data");
2476*77c1e3ccSAndroid Build Coastguard Worker       }
2477*77c1e3ccSAndroid Build Coastguard Worker     }
2478*77c1e3ccSAndroid Build Coastguard Worker   }
2479*77c1e3ccSAndroid Build Coastguard Worker }
2480*77c1e3ccSAndroid Build Coastguard Worker 
2481*77c1e3ccSAndroid Build Coastguard Worker // Deallocate thread specific data for temporal filter.
tf_dealloc_thread_data(AV1_COMP * cpi,int num_workers,int is_highbitdepth)2482*77c1e3ccSAndroid Build Coastguard Worker static void tf_dealloc_thread_data(AV1_COMP *cpi, int num_workers,
2483*77c1e3ccSAndroid Build Coastguard Worker                                    int is_highbitdepth) {
2484*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *mt_info = &cpi->mt_info;
2485*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
2486*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *thread_data = &mt_info->tile_thr_data[i];
2487*77c1e3ccSAndroid Build Coastguard Worker     ThreadData *td = thread_data->td;
2488*77c1e3ccSAndroid Build Coastguard Worker     if (td != &cpi->td) tf_dealloc_data(&td->tf_data, is_highbitdepth);
2489*77c1e3ccSAndroid Build Coastguard Worker   }
2490*77c1e3ccSAndroid Build Coastguard Worker }
2491*77c1e3ccSAndroid Build Coastguard Worker 
2492*77c1e3ccSAndroid Build Coastguard Worker // Accumulate sse and sum after temporal filtering.
tf_accumulate_frame_diff(AV1_COMP * cpi,int num_workers)2493*77c1e3ccSAndroid Build Coastguard Worker static void tf_accumulate_frame_diff(AV1_COMP *cpi, int num_workers) {
2494*77c1e3ccSAndroid Build Coastguard Worker   FRAME_DIFF *total_diff = &cpi->td.tf_data.diff;
2495*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
2496*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *const worker = &cpi->mt_info.workers[i];
2497*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
2498*77c1e3ccSAndroid Build Coastguard Worker     ThreadData *td = thread_data->td;
2499*77c1e3ccSAndroid Build Coastguard Worker     FRAME_DIFF *diff = &td->tf_data.diff;
2500*77c1e3ccSAndroid Build Coastguard Worker     if (td != &cpi->td) {
2501*77c1e3ccSAndroid Build Coastguard Worker       total_diff->sse += diff->sse;
2502*77c1e3ccSAndroid Build Coastguard Worker       total_diff->sum += diff->sum;
2503*77c1e3ccSAndroid Build Coastguard Worker     }
2504*77c1e3ccSAndroid Build Coastguard Worker   }
2505*77c1e3ccSAndroid Build Coastguard Worker }
2506*77c1e3ccSAndroid Build Coastguard Worker 
2507*77c1e3ccSAndroid Build Coastguard Worker // Implements multi-threading for temporal filter.
av1_tf_do_filtering_mt(AV1_COMP * cpi)2508*77c1e3ccSAndroid Build Coastguard Worker void av1_tf_do_filtering_mt(AV1_COMP *cpi) {
2509*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *cm = &cpi->common;
2510*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *mt_info = &cpi->mt_info;
2511*77c1e3ccSAndroid Build Coastguard Worker   const int is_highbitdepth = cpi->tf_ctx.is_highbitdepth;
2512*77c1e3ccSAndroid Build Coastguard Worker 
2513*77c1e3ccSAndroid Build Coastguard Worker   int num_workers =
2514*77c1e3ccSAndroid Build Coastguard Worker       AOMMIN(mt_info->num_mod_workers[MOD_TF], mt_info->num_workers);
2515*77c1e3ccSAndroid Build Coastguard Worker 
2516*77c1e3ccSAndroid Build Coastguard Worker   prepare_tf_workers(cpi, tf_worker_hook, num_workers, is_highbitdepth);
2517*77c1e3ccSAndroid Build Coastguard Worker   launch_workers(mt_info, num_workers);
2518*77c1e3ccSAndroid Build Coastguard Worker   sync_enc_workers(mt_info, cm, num_workers);
2519*77c1e3ccSAndroid Build Coastguard Worker   tf_accumulate_frame_diff(cpi, num_workers);
2520*77c1e3ccSAndroid Build Coastguard Worker   tf_dealloc_thread_data(cpi, num_workers, is_highbitdepth);
2521*77c1e3ccSAndroid Build Coastguard Worker }
2522*77c1e3ccSAndroid Build Coastguard Worker 
2523*77c1e3ccSAndroid Build Coastguard Worker // Checks if a job is available in the current direction. If a job is available,
2524*77c1e3ccSAndroid Build Coastguard Worker // frame_idx will be populated and returns 1, else returns 0.
get_next_gm_job(AV1_COMP * cpi,int * frame_idx,int cur_dir)2525*77c1e3ccSAndroid Build Coastguard Worker static inline int get_next_gm_job(AV1_COMP *cpi, int *frame_idx, int cur_dir) {
2526*77c1e3ccSAndroid Build Coastguard Worker   GlobalMotionInfo *gm_info = &cpi->gm_info;
2527*77c1e3ccSAndroid Build Coastguard Worker   GlobalMotionJobInfo *job_info = &cpi->mt_info.gm_sync.job_info;
2528*77c1e3ccSAndroid Build Coastguard Worker 
2529*77c1e3ccSAndroid Build Coastguard Worker   int total_refs = gm_info->num_ref_frames[cur_dir];
2530*77c1e3ccSAndroid Build Coastguard Worker   int8_t cur_frame_to_process = job_info->next_frame_to_process[cur_dir];
2531*77c1e3ccSAndroid Build Coastguard Worker 
2532*77c1e3ccSAndroid Build Coastguard Worker   if (cur_frame_to_process < total_refs && !job_info->early_exit[cur_dir]) {
2533*77c1e3ccSAndroid Build Coastguard Worker     *frame_idx = gm_info->reference_frames[cur_dir][cur_frame_to_process].frame;
2534*77c1e3ccSAndroid Build Coastguard Worker     job_info->next_frame_to_process[cur_dir] += 1;
2535*77c1e3ccSAndroid Build Coastguard Worker     return 1;
2536*77c1e3ccSAndroid Build Coastguard Worker   }
2537*77c1e3ccSAndroid Build Coastguard Worker   return 0;
2538*77c1e3ccSAndroid Build Coastguard Worker }
2539*77c1e3ccSAndroid Build Coastguard Worker 
2540*77c1e3ccSAndroid Build Coastguard Worker // Switches the current direction and calls the function get_next_gm_job() if
2541*77c1e3ccSAndroid Build Coastguard Worker // the speed feature 'prune_ref_frame_for_gm_search' is not set.
switch_direction(AV1_COMP * cpi,int * frame_idx,int * cur_dir)2542*77c1e3ccSAndroid Build Coastguard Worker static inline void switch_direction(AV1_COMP *cpi, int *frame_idx,
2543*77c1e3ccSAndroid Build Coastguard Worker                                     int *cur_dir) {
2544*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.gm_sf.prune_ref_frame_for_gm_search) return;
2545*77c1e3ccSAndroid Build Coastguard Worker   // Switch the direction and get next job
2546*77c1e3ccSAndroid Build Coastguard Worker   *cur_dir = !(*cur_dir);
2547*77c1e3ccSAndroid Build Coastguard Worker   get_next_gm_job(cpi, frame_idx, *(cur_dir));
2548*77c1e3ccSAndroid Build Coastguard Worker }
2549*77c1e3ccSAndroid Build Coastguard Worker 
2550*77c1e3ccSAndroid Build Coastguard Worker // Hook function for each thread in global motion multi-threading.
gm_mt_worker_hook(void * arg1,void * unused)2551*77c1e3ccSAndroid Build Coastguard Worker static int gm_mt_worker_hook(void *arg1, void *unused) {
2552*77c1e3ccSAndroid Build Coastguard Worker   (void)unused;
2553*77c1e3ccSAndroid Build Coastguard Worker 
2554*77c1e3ccSAndroid Build Coastguard Worker   EncWorkerData *thread_data = (EncWorkerData *)arg1;
2555*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMP *cpi = thread_data->cpi;
2556*77c1e3ccSAndroid Build Coastguard Worker   GlobalMotionInfo *gm_info = &cpi->gm_info;
2557*77c1e3ccSAndroid Build Coastguard Worker   AV1GlobalMotionSync *gm_sync = &cpi->mt_info.gm_sync;
2558*77c1e3ccSAndroid Build Coastguard Worker   GlobalMotionJobInfo *job_info = &gm_sync->job_info;
2559*77c1e3ccSAndroid Build Coastguard Worker   int thread_id = thread_data->thread_id;
2560*77c1e3ccSAndroid Build Coastguard Worker   GlobalMotionData *gm_thread_data = &thread_data->td->gm_data;
2561*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2562*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *gm_mt_mutex_ = gm_sync->mutex_;
2563*77c1e3ccSAndroid Build Coastguard Worker #endif
2564*77c1e3ccSAndroid Build Coastguard Worker 
2565*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &thread_data->td->mb.e_mbd;
2566*77c1e3ccSAndroid Build Coastguard Worker   struct aom_internal_error_info *const error_info = &thread_data->error_info;
2567*77c1e3ccSAndroid Build Coastguard Worker   xd->error_info = error_info;
2568*77c1e3ccSAndroid Build Coastguard Worker 
2569*77c1e3ccSAndroid Build Coastguard Worker   // The jmp_buf is valid only for the duration of the function that calls
2570*77c1e3ccSAndroid Build Coastguard Worker   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2571*77c1e3ccSAndroid Build Coastguard Worker   // before it returns.
2572*77c1e3ccSAndroid Build Coastguard Worker   if (setjmp(error_info->jmp)) {
2573*77c1e3ccSAndroid Build Coastguard Worker     error_info->setjmp = 0;
2574*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2575*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(gm_mt_mutex_);
2576*77c1e3ccSAndroid Build Coastguard Worker     gm_sync->gm_mt_exit = true;
2577*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(gm_mt_mutex_);
2578*77c1e3ccSAndroid Build Coastguard Worker #endif
2579*77c1e3ccSAndroid Build Coastguard Worker     return 0;
2580*77c1e3ccSAndroid Build Coastguard Worker   }
2581*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 1;
2582*77c1e3ccSAndroid Build Coastguard Worker 
2583*77c1e3ccSAndroid Build Coastguard Worker   int cur_dir = job_info->thread_id_to_dir[thread_id];
2584*77c1e3ccSAndroid Build Coastguard Worker   bool gm_mt_exit = false;
2585*77c1e3ccSAndroid Build Coastguard Worker   while (1) {
2586*77c1e3ccSAndroid Build Coastguard Worker     int ref_buf_idx = -1;
2587*77c1e3ccSAndroid Build Coastguard Worker 
2588*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2589*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(gm_mt_mutex_);
2590*77c1e3ccSAndroid Build Coastguard Worker #endif
2591*77c1e3ccSAndroid Build Coastguard Worker 
2592*77c1e3ccSAndroid Build Coastguard Worker     gm_mt_exit = gm_sync->gm_mt_exit;
2593*77c1e3ccSAndroid Build Coastguard Worker     // Populates ref_buf_idx(the reference frame type) for which global motion
2594*77c1e3ccSAndroid Build Coastguard Worker     // estimation will be done.
2595*77c1e3ccSAndroid Build Coastguard Worker     if (!gm_mt_exit && !get_next_gm_job(cpi, &ref_buf_idx, cur_dir)) {
2596*77c1e3ccSAndroid Build Coastguard Worker       // No jobs are available for the current direction. Switch
2597*77c1e3ccSAndroid Build Coastguard Worker       // to other direction and get the next job, if available.
2598*77c1e3ccSAndroid Build Coastguard Worker       switch_direction(cpi, &ref_buf_idx, &cur_dir);
2599*77c1e3ccSAndroid Build Coastguard Worker     }
2600*77c1e3ccSAndroid Build Coastguard Worker 
2601*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2602*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(gm_mt_mutex_);
2603*77c1e3ccSAndroid Build Coastguard Worker #endif
2604*77c1e3ccSAndroid Build Coastguard Worker 
2605*77c1e3ccSAndroid Build Coastguard Worker     // When gm_mt_exit is set to true, other workers need not pursue any
2606*77c1e3ccSAndroid Build Coastguard Worker     // further jobs.
2607*77c1e3ccSAndroid Build Coastguard Worker     if (gm_mt_exit || ref_buf_idx == -1) break;
2608*77c1e3ccSAndroid Build Coastguard Worker 
2609*77c1e3ccSAndroid Build Coastguard Worker     // Compute global motion for the given ref_buf_idx.
2610*77c1e3ccSAndroid Build Coastguard Worker     av1_compute_gm_for_valid_ref_frames(
2611*77c1e3ccSAndroid Build Coastguard Worker         cpi, error_info, gm_info->ref_buf, ref_buf_idx,
2612*77c1e3ccSAndroid Build Coastguard Worker         gm_thread_data->motion_models, gm_thread_data->segment_map,
2613*77c1e3ccSAndroid Build Coastguard Worker         gm_info->segment_map_w, gm_info->segment_map_h);
2614*77c1e3ccSAndroid Build Coastguard Worker 
2615*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2616*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(gm_mt_mutex_);
2617*77c1e3ccSAndroid Build Coastguard Worker #endif
2618*77c1e3ccSAndroid Build Coastguard Worker     // If global motion w.r.t. current ref frame is
2619*77c1e3ccSAndroid Build Coastguard Worker     // INVALID/TRANSLATION/IDENTITY, skip the evaluation of global motion w.r.t
2620*77c1e3ccSAndroid Build Coastguard Worker     // the remaining ref frames in that direction.
2621*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->sf.gm_sf.prune_ref_frame_for_gm_search &&
2622*77c1e3ccSAndroid Build Coastguard Worker         cpi->common.global_motion[ref_buf_idx].wmtype <= TRANSLATION)
2623*77c1e3ccSAndroid Build Coastguard Worker       job_info->early_exit[cur_dir] = 1;
2624*77c1e3ccSAndroid Build Coastguard Worker 
2625*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2626*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(gm_mt_mutex_);
2627*77c1e3ccSAndroid Build Coastguard Worker #endif
2628*77c1e3ccSAndroid Build Coastguard Worker   }
2629*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 0;
2630*77c1e3ccSAndroid Build Coastguard Worker   return 1;
2631*77c1e3ccSAndroid Build Coastguard Worker }
2632*77c1e3ccSAndroid Build Coastguard Worker 
2633*77c1e3ccSAndroid Build Coastguard Worker // Assigns global motion hook function and thread data to each worker.
prepare_gm_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)2634*77c1e3ccSAndroid Build Coastguard Worker static inline void prepare_gm_workers(AV1_COMP *cpi, AVxWorkerHook hook,
2635*77c1e3ccSAndroid Build Coastguard Worker                                       int num_workers) {
2636*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *mt_info = &cpi->mt_info;
2637*77c1e3ccSAndroid Build Coastguard Worker   mt_info->gm_sync.gm_mt_exit = false;
2638*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
2639*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *worker = &mt_info->workers[i];
2640*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *thread_data = &mt_info->tile_thr_data[i];
2641*77c1e3ccSAndroid Build Coastguard Worker 
2642*77c1e3ccSAndroid Build Coastguard Worker     worker->hook = hook;
2643*77c1e3ccSAndroid Build Coastguard Worker     worker->data1 = thread_data;
2644*77c1e3ccSAndroid Build Coastguard Worker     worker->data2 = NULL;
2645*77c1e3ccSAndroid Build Coastguard Worker 
2646*77c1e3ccSAndroid Build Coastguard Worker     thread_data->thread_id = i;
2647*77c1e3ccSAndroid Build Coastguard Worker     // Set the starting tile for each thread.
2648*77c1e3ccSAndroid Build Coastguard Worker     thread_data->start = i;
2649*77c1e3ccSAndroid Build Coastguard Worker 
2650*77c1e3ccSAndroid Build Coastguard Worker     thread_data->cpi = cpi;
2651*77c1e3ccSAndroid Build Coastguard Worker     if (i == 0) {
2652*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td = &cpi->td;
2653*77c1e3ccSAndroid Build Coastguard Worker     } else {
2654*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td = thread_data->original_td;
2655*77c1e3ccSAndroid Build Coastguard Worker     }
2656*77c1e3ccSAndroid Build Coastguard Worker 
2657*77c1e3ccSAndroid Build Coastguard Worker     if (thread_data->td != &cpi->td)
2658*77c1e3ccSAndroid Build Coastguard Worker       gm_alloc_data(cpi, &thread_data->td->gm_data);
2659*77c1e3ccSAndroid Build Coastguard Worker   }
2660*77c1e3ccSAndroid Build Coastguard Worker }
2661*77c1e3ccSAndroid Build Coastguard Worker 
2662*77c1e3ccSAndroid Build Coastguard Worker // Assigns available threads to past/future direction.
assign_thread_to_dir(int8_t * thread_id_to_dir,int num_workers)2663*77c1e3ccSAndroid Build Coastguard Worker static inline void assign_thread_to_dir(int8_t *thread_id_to_dir,
2664*77c1e3ccSAndroid Build Coastguard Worker                                         int num_workers) {
2665*77c1e3ccSAndroid Build Coastguard Worker   int8_t frame_dir_idx = 0;
2666*77c1e3ccSAndroid Build Coastguard Worker 
2667*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < num_workers; i++) {
2668*77c1e3ccSAndroid Build Coastguard Worker     thread_id_to_dir[i] = frame_dir_idx++;
2669*77c1e3ccSAndroid Build Coastguard Worker     if (frame_dir_idx == MAX_DIRECTIONS) frame_dir_idx = 0;
2670*77c1e3ccSAndroid Build Coastguard Worker   }
2671*77c1e3ccSAndroid Build Coastguard Worker }
2672*77c1e3ccSAndroid Build Coastguard Worker 
2673*77c1e3ccSAndroid Build Coastguard Worker // Computes number of workers for global motion multi-threading.
compute_gm_workers(const AV1_COMP * cpi)2674*77c1e3ccSAndroid Build Coastguard Worker static inline int compute_gm_workers(const AV1_COMP *cpi) {
2675*77c1e3ccSAndroid Build Coastguard Worker   int total_refs =
2676*77c1e3ccSAndroid Build Coastguard Worker       cpi->gm_info.num_ref_frames[0] + cpi->gm_info.num_ref_frames[1];
2677*77c1e3ccSAndroid Build Coastguard Worker   int num_gm_workers = cpi->sf.gm_sf.prune_ref_frame_for_gm_search
2678*77c1e3ccSAndroid Build Coastguard Worker                            ? AOMMIN(MAX_DIRECTIONS, total_refs)
2679*77c1e3ccSAndroid Build Coastguard Worker                            : total_refs;
2680*77c1e3ccSAndroid Build Coastguard Worker   num_gm_workers = AOMMIN(num_gm_workers, cpi->mt_info.num_workers);
2681*77c1e3ccSAndroid Build Coastguard Worker   return (num_gm_workers);
2682*77c1e3ccSAndroid Build Coastguard Worker }
2683*77c1e3ccSAndroid Build Coastguard Worker 
2684*77c1e3ccSAndroid Build Coastguard Worker // Frees the memory allocated for each worker in global motion multi-threading.
gm_dealloc_thread_data(AV1_COMP * cpi,int num_workers)2685*77c1e3ccSAndroid Build Coastguard Worker static inline void gm_dealloc_thread_data(AV1_COMP *cpi, int num_workers) {
2686*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *mt_info = &cpi->mt_info;
2687*77c1e3ccSAndroid Build Coastguard Worker   for (int j = 0; j < num_workers; j++) {
2688*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *thread_data = &mt_info->tile_thr_data[j];
2689*77c1e3ccSAndroid Build Coastguard Worker     ThreadData *td = thread_data->td;
2690*77c1e3ccSAndroid Build Coastguard Worker     if (td != &cpi->td) gm_dealloc_data(&td->gm_data);
2691*77c1e3ccSAndroid Build Coastguard Worker   }
2692*77c1e3ccSAndroid Build Coastguard Worker }
2693*77c1e3ccSAndroid Build Coastguard Worker 
2694*77c1e3ccSAndroid Build Coastguard Worker // Implements multi-threading for global motion.
av1_global_motion_estimation_mt(AV1_COMP * cpi)2695*77c1e3ccSAndroid Build Coastguard Worker void av1_global_motion_estimation_mt(AV1_COMP *cpi) {
2696*77c1e3ccSAndroid Build Coastguard Worker   GlobalMotionJobInfo *job_info = &cpi->mt_info.gm_sync.job_info;
2697*77c1e3ccSAndroid Build Coastguard Worker 
2698*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(*job_info);
2699*77c1e3ccSAndroid Build Coastguard Worker 
2700*77c1e3ccSAndroid Build Coastguard Worker   int num_workers = compute_gm_workers(cpi);
2701*77c1e3ccSAndroid Build Coastguard Worker 
2702*77c1e3ccSAndroid Build Coastguard Worker   assign_thread_to_dir(job_info->thread_id_to_dir, num_workers);
2703*77c1e3ccSAndroid Build Coastguard Worker   prepare_gm_workers(cpi, gm_mt_worker_hook, num_workers);
2704*77c1e3ccSAndroid Build Coastguard Worker   launch_workers(&cpi->mt_info, num_workers);
2705*77c1e3ccSAndroid Build Coastguard Worker   sync_enc_workers(&cpi->mt_info, &cpi->common, num_workers);
2706*77c1e3ccSAndroid Build Coastguard Worker   gm_dealloc_thread_data(cpi, num_workers);
2707*77c1e3ccSAndroid Build Coastguard Worker }
2708*77c1e3ccSAndroid Build Coastguard Worker #endif  // !CONFIG_REALTIME_ONLY
2709*77c1e3ccSAndroid Build Coastguard Worker 
get_next_job_allintra(AV1EncRowMultiThreadSync * const row_mt_sync,const int mi_row_end,int * current_mi_row,int mib_size)2710*77c1e3ccSAndroid Build Coastguard Worker static inline int get_next_job_allintra(
2711*77c1e3ccSAndroid Build Coastguard Worker     AV1EncRowMultiThreadSync *const row_mt_sync, const int mi_row_end,
2712*77c1e3ccSAndroid Build Coastguard Worker     int *current_mi_row, int mib_size) {
2713*77c1e3ccSAndroid Build Coastguard Worker   if (row_mt_sync->next_mi_row < mi_row_end) {
2714*77c1e3ccSAndroid Build Coastguard Worker     *current_mi_row = row_mt_sync->next_mi_row;
2715*77c1e3ccSAndroid Build Coastguard Worker     row_mt_sync->num_threads_working++;
2716*77c1e3ccSAndroid Build Coastguard Worker     row_mt_sync->next_mi_row += mib_size;
2717*77c1e3ccSAndroid Build Coastguard Worker     return 1;
2718*77c1e3ccSAndroid Build Coastguard Worker   }
2719*77c1e3ccSAndroid Build Coastguard Worker   return 0;
2720*77c1e3ccSAndroid Build Coastguard Worker }
2721*77c1e3ccSAndroid Build Coastguard Worker 
prepare_wiener_var_workers(AV1_COMP * const cpi,AVxWorkerHook hook,const int num_workers)2722*77c1e3ccSAndroid Build Coastguard Worker static inline void prepare_wiener_var_workers(AV1_COMP *const cpi,
2723*77c1e3ccSAndroid Build Coastguard Worker                                               AVxWorkerHook hook,
2724*77c1e3ccSAndroid Build Coastguard Worker                                               const int num_workers) {
2725*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
2726*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
2727*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *const worker = &mt_info->workers[i];
2728*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *const thread_data = &mt_info->tile_thr_data[i];
2729*77c1e3ccSAndroid Build Coastguard Worker 
2730*77c1e3ccSAndroid Build Coastguard Worker     worker->hook = hook;
2731*77c1e3ccSAndroid Build Coastguard Worker     worker->data1 = thread_data;
2732*77c1e3ccSAndroid Build Coastguard Worker     worker->data2 = NULL;
2733*77c1e3ccSAndroid Build Coastguard Worker 
2734*77c1e3ccSAndroid Build Coastguard Worker     thread_data->thread_id = i;
2735*77c1e3ccSAndroid Build Coastguard Worker     // Set the starting tile for each thread, in this case the preprocessing
2736*77c1e3ccSAndroid Build Coastguard Worker     // stage does not need tiles. So we set it to 0.
2737*77c1e3ccSAndroid Build Coastguard Worker     thread_data->start = 0;
2738*77c1e3ccSAndroid Build Coastguard Worker 
2739*77c1e3ccSAndroid Build Coastguard Worker     thread_data->cpi = cpi;
2740*77c1e3ccSAndroid Build Coastguard Worker     if (i == 0) {
2741*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td = &cpi->td;
2742*77c1e3ccSAndroid Build Coastguard Worker     } else {
2743*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td = thread_data->original_td;
2744*77c1e3ccSAndroid Build Coastguard Worker     }
2745*77c1e3ccSAndroid Build Coastguard Worker 
2746*77c1e3ccSAndroid Build Coastguard Worker     if (thread_data->td != &cpi->td) {
2747*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td->mb = cpi->td.mb;
2748*77c1e3ccSAndroid Build Coastguard Worker       av1_alloc_mb_wiener_var_pred_buf(&cpi->common, thread_data->td);
2749*77c1e3ccSAndroid Build Coastguard Worker     }
2750*77c1e3ccSAndroid Build Coastguard Worker   }
2751*77c1e3ccSAndroid Build Coastguard Worker }
2752*77c1e3ccSAndroid Build Coastguard Worker 
set_mb_wiener_var_calc_done(AV1_COMP * const cpi)2753*77c1e3ccSAndroid Build Coastguard Worker static void set_mb_wiener_var_calc_done(AV1_COMP *const cpi) {
2754*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
2755*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = cpi->weber_bsize;
2756*77c1e3ccSAndroid Build Coastguard Worker   const int mb_step = mi_size_wide[bsize];
2757*77c1e3ccSAndroid Build Coastguard Worker   assert(MB_WIENER_MT_UNIT_SIZE < BLOCK_SIZES_ALL);
2758*77c1e3ccSAndroid Build Coastguard Worker   const int mt_unit_step = mi_size_wide[MB_WIENER_MT_UNIT_SIZE];
2759*77c1e3ccSAndroid Build Coastguard Worker   const int mt_unit_cols =
2760*77c1e3ccSAndroid Build Coastguard Worker       (mi_params->mi_cols + (mt_unit_step >> 1)) / mt_unit_step;
2761*77c1e3ccSAndroid Build Coastguard Worker   const AV1EncAllIntraMultiThreadInfo *const intra_mt = &cpi->mt_info.intra_mt;
2762*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadSync *const intra_row_mt_sync =
2763*77c1e3ccSAndroid Build Coastguard Worker       &cpi->ppi->intra_row_mt_sync;
2764*77c1e3ccSAndroid Build Coastguard Worker 
2765*77c1e3ccSAndroid Build Coastguard Worker   // Update the wiener variance computation of every row in the frame to
2766*77c1e3ccSAndroid Build Coastguard Worker   // indicate that it is complete in order to avoid dependent workers waiting
2767*77c1e3ccSAndroid Build Coastguard Worker   // indefinitely.
2768*77c1e3ccSAndroid Build Coastguard Worker   for (int mi_row = 0, mt_thread_id = 0; mi_row < mi_params->mi_rows;
2769*77c1e3ccSAndroid Build Coastguard Worker        mi_row += mb_step, ++mt_thread_id) {
2770*77c1e3ccSAndroid Build Coastguard Worker     intra_mt->intra_sync_write_ptr(intra_row_mt_sync, mt_thread_id,
2771*77c1e3ccSAndroid Build Coastguard Worker                                    mt_unit_cols - 1, mt_unit_cols);
2772*77c1e3ccSAndroid Build Coastguard Worker   }
2773*77c1e3ccSAndroid Build Coastguard Worker }
2774*77c1e3ccSAndroid Build Coastguard Worker 
cal_mb_wiener_var_hook(void * arg1,void * unused)2775*77c1e3ccSAndroid Build Coastguard Worker static int cal_mb_wiener_var_hook(void *arg1, void *unused) {
2776*77c1e3ccSAndroid Build Coastguard Worker   (void)unused;
2777*77c1e3ccSAndroid Build Coastguard Worker   EncWorkerData *const thread_data = (EncWorkerData *)arg1;
2778*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMP *const cpi = thread_data->cpi;
2779*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCK *x = &thread_data->td->mb;
2780*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *xd = &x->e_mbd;
2781*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = cpi->weber_bsize;
2782*77c1e3ccSAndroid Build Coastguard Worker   const int mb_step = mi_size_wide[bsize];
2783*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadSync *const intra_row_mt_sync =
2784*77c1e3ccSAndroid Build Coastguard Worker       &cpi->ppi->intra_row_mt_sync;
2785*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
2786*77c1e3ccSAndroid Build Coastguard Worker   (void)enc_row_mt;
2787*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2788*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *enc_row_mt_mutex = enc_row_mt->mutex_;
2789*77c1e3ccSAndroid Build Coastguard Worker #endif
2790*77c1e3ccSAndroid Build Coastguard Worker 
2791*77c1e3ccSAndroid Build Coastguard Worker   struct aom_internal_error_info *const error_info = &thread_data->error_info;
2792*77c1e3ccSAndroid Build Coastguard Worker   xd->error_info = error_info;
2793*77c1e3ccSAndroid Build Coastguard Worker 
2794*77c1e3ccSAndroid Build Coastguard Worker   // The jmp_buf is valid only for the duration of the function that calls
2795*77c1e3ccSAndroid Build Coastguard Worker   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2796*77c1e3ccSAndroid Build Coastguard Worker   // before it returns.
2797*77c1e3ccSAndroid Build Coastguard Worker   if (setjmp(error_info->jmp)) {
2798*77c1e3ccSAndroid Build Coastguard Worker     error_info->setjmp = 0;
2799*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2800*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(enc_row_mt_mutex);
2801*77c1e3ccSAndroid Build Coastguard Worker     enc_row_mt->mb_wiener_mt_exit = true;
2802*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(enc_row_mt_mutex);
2803*77c1e3ccSAndroid Build Coastguard Worker #endif
2804*77c1e3ccSAndroid Build Coastguard Worker     set_mb_wiener_var_calc_done(cpi);
2805*77c1e3ccSAndroid Build Coastguard Worker     return 0;
2806*77c1e3ccSAndroid Build Coastguard Worker   }
2807*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 1;
2808*77c1e3ccSAndroid Build Coastguard Worker   DECLARE_ALIGNED(32, int16_t, src_diff[32 * 32]);
2809*77c1e3ccSAndroid Build Coastguard Worker   DECLARE_ALIGNED(32, tran_low_t, coeff[32 * 32]);
2810*77c1e3ccSAndroid Build Coastguard Worker   DECLARE_ALIGNED(32, tran_low_t, qcoeff[32 * 32]);
2811*77c1e3ccSAndroid Build Coastguard Worker   DECLARE_ALIGNED(32, tran_low_t, dqcoeff[32 * 32]);
2812*77c1e3ccSAndroid Build Coastguard Worker   double sum_rec_distortion = 0;
2813*77c1e3ccSAndroid Build Coastguard Worker   double sum_est_rate = 0;
2814*77c1e3ccSAndroid Build Coastguard Worker   while (1) {
2815*77c1e3ccSAndroid Build Coastguard Worker     int current_mi_row = -1;
2816*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2817*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(enc_row_mt_mutex);
2818*77c1e3ccSAndroid Build Coastguard Worker #endif
2819*77c1e3ccSAndroid Build Coastguard Worker     int has_jobs = enc_row_mt->mb_wiener_mt_exit
2820*77c1e3ccSAndroid Build Coastguard Worker                        ? 0
2821*77c1e3ccSAndroid Build Coastguard Worker                        : get_next_job_allintra(intra_row_mt_sync,
2822*77c1e3ccSAndroid Build Coastguard Worker                                                cpi->common.mi_params.mi_rows,
2823*77c1e3ccSAndroid Build Coastguard Worker                                                &current_mi_row, mb_step);
2824*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2825*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(enc_row_mt_mutex);
2826*77c1e3ccSAndroid Build Coastguard Worker #endif
2827*77c1e3ccSAndroid Build Coastguard Worker     if (!has_jobs) break;
2828*77c1e3ccSAndroid Build Coastguard Worker     // TODO(chengchen): properly accumulate the distortion and rate.
2829*77c1e3ccSAndroid Build Coastguard Worker     av1_calc_mb_wiener_var_row(cpi, x, xd, current_mi_row, src_diff, coeff,
2830*77c1e3ccSAndroid Build Coastguard Worker                                qcoeff, dqcoeff, &sum_rec_distortion,
2831*77c1e3ccSAndroid Build Coastguard Worker                                &sum_est_rate,
2832*77c1e3ccSAndroid Build Coastguard Worker                                thread_data->td->wiener_tmp_pred_buf);
2833*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2834*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(enc_row_mt_mutex);
2835*77c1e3ccSAndroid Build Coastguard Worker #endif
2836*77c1e3ccSAndroid Build Coastguard Worker     intra_row_mt_sync->num_threads_working--;
2837*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
2838*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(enc_row_mt_mutex);
2839*77c1e3ccSAndroid Build Coastguard Worker #endif
2840*77c1e3ccSAndroid Build Coastguard Worker   }
2841*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 0;
2842*77c1e3ccSAndroid Build Coastguard Worker   return 1;
2843*77c1e3ccSAndroid Build Coastguard Worker }
2844*77c1e3ccSAndroid Build Coastguard Worker 
dealloc_mb_wiener_var_mt_data(AV1_COMP * cpi,int num_workers)2845*77c1e3ccSAndroid Build Coastguard Worker static void dealloc_mb_wiener_var_mt_data(AV1_COMP *cpi, int num_workers) {
2846*77c1e3ccSAndroid Build Coastguard Worker   av1_row_mt_sync_mem_dealloc(&cpi->ppi->intra_row_mt_sync);
2847*77c1e3ccSAndroid Build Coastguard Worker 
2848*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *mt_info = &cpi->mt_info;
2849*77c1e3ccSAndroid Build Coastguard Worker   for (int j = 0; j < num_workers; ++j) {
2850*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *thread_data = &mt_info->tile_thr_data[j];
2851*77c1e3ccSAndroid Build Coastguard Worker     ThreadData *td = thread_data->td;
2852*77c1e3ccSAndroid Build Coastguard Worker     if (td != &cpi->td) av1_dealloc_mb_wiener_var_pred_buf(td);
2853*77c1e3ccSAndroid Build Coastguard Worker   }
2854*77c1e3ccSAndroid Build Coastguard Worker }
2855*77c1e3ccSAndroid Build Coastguard Worker 
2856*77c1e3ccSAndroid Build Coastguard Worker // This function is the multi-threading version of computing the wiener
2857*77c1e3ccSAndroid Build Coastguard Worker // variance.
2858*77c1e3ccSAndroid Build Coastguard Worker // Note that the wiener variance is used for allintra mode (1 pass) and its
2859*77c1e3ccSAndroid Build Coastguard Worker // computation is before the frame encoding, so we don't need to consider
2860*77c1e3ccSAndroid Build Coastguard Worker // the number of tiles, instead we allocate all available threads to
2861*77c1e3ccSAndroid Build Coastguard Worker // the computation.
av1_calc_mb_wiener_var_mt(AV1_COMP * cpi,int num_workers,double * sum_rec_distortion,double * sum_est_rate)2862*77c1e3ccSAndroid Build Coastguard Worker void av1_calc_mb_wiener_var_mt(AV1_COMP *cpi, int num_workers,
2863*77c1e3ccSAndroid Build Coastguard Worker                                double *sum_rec_distortion,
2864*77c1e3ccSAndroid Build Coastguard Worker                                double *sum_est_rate) {
2865*77c1e3ccSAndroid Build Coastguard Worker   (void)sum_rec_distortion;
2866*77c1e3ccSAndroid Build Coastguard Worker   (void)sum_est_rate;
2867*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2868*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
2869*77c1e3ccSAndroid Build Coastguard Worker   AV1EncRowMultiThreadSync *const intra_row_mt_sync =
2870*77c1e3ccSAndroid Build Coastguard Worker       &cpi->ppi->intra_row_mt_sync;
2871*77c1e3ccSAndroid Build Coastguard Worker 
2872*77c1e3ccSAndroid Build Coastguard Worker   // TODO(chengchen): the memory usage could be improved.
2873*77c1e3ccSAndroid Build Coastguard Worker   const int mi_rows = cm->mi_params.mi_rows;
2874*77c1e3ccSAndroid Build Coastguard Worker   row_mt_sync_mem_alloc(intra_row_mt_sync, cm, mi_rows);
2875*77c1e3ccSAndroid Build Coastguard Worker 
2876*77c1e3ccSAndroid Build Coastguard Worker   intra_row_mt_sync->intrabc_extra_top_right_sb_delay = 0;
2877*77c1e3ccSAndroid Build Coastguard Worker   intra_row_mt_sync->num_threads_working = num_workers;
2878*77c1e3ccSAndroid Build Coastguard Worker   intra_row_mt_sync->next_mi_row = 0;
2879*77c1e3ccSAndroid Build Coastguard Worker   memset(intra_row_mt_sync->num_finished_cols, -1,
2880*77c1e3ccSAndroid Build Coastguard Worker          sizeof(*intra_row_mt_sync->num_finished_cols) * mi_rows);
2881*77c1e3ccSAndroid Build Coastguard Worker   mt_info->enc_row_mt.mb_wiener_mt_exit = false;
2882*77c1e3ccSAndroid Build Coastguard Worker 
2883*77c1e3ccSAndroid Build Coastguard Worker   prepare_wiener_var_workers(cpi, cal_mb_wiener_var_hook, num_workers);
2884*77c1e3ccSAndroid Build Coastguard Worker   launch_workers(mt_info, num_workers);
2885*77c1e3ccSAndroid Build Coastguard Worker   sync_enc_workers(mt_info, cm, num_workers);
2886*77c1e3ccSAndroid Build Coastguard Worker   dealloc_mb_wiener_var_mt_data(cpi, num_workers);
2887*77c1e3ccSAndroid Build Coastguard Worker }
2888*77c1e3ccSAndroid Build Coastguard Worker 
2889*77c1e3ccSAndroid Build Coastguard Worker // Compare and order tiles based on absolute sum of tx coeffs.
compare_tile_order(const void * a,const void * b)2890*77c1e3ccSAndroid Build Coastguard Worker static int compare_tile_order(const void *a, const void *b) {
2891*77c1e3ccSAndroid Build Coastguard Worker   const PackBSTileOrder *const tile_a = (const PackBSTileOrder *)a;
2892*77c1e3ccSAndroid Build Coastguard Worker   const PackBSTileOrder *const tile_b = (const PackBSTileOrder *)b;
2893*77c1e3ccSAndroid Build Coastguard Worker 
2894*77c1e3ccSAndroid Build Coastguard Worker   if (tile_a->abs_sum_level > tile_b->abs_sum_level)
2895*77c1e3ccSAndroid Build Coastguard Worker     return -1;
2896*77c1e3ccSAndroid Build Coastguard Worker   else if (tile_a->abs_sum_level == tile_b->abs_sum_level)
2897*77c1e3ccSAndroid Build Coastguard Worker     return (tile_a->tile_idx > tile_b->tile_idx ? 1 : -1);
2898*77c1e3ccSAndroid Build Coastguard Worker   else
2899*77c1e3ccSAndroid Build Coastguard Worker     return 1;
2900*77c1e3ccSAndroid Build Coastguard Worker }
2901*77c1e3ccSAndroid Build Coastguard Worker 
2902*77c1e3ccSAndroid Build Coastguard Worker // 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)2903*77c1e3ccSAndroid Build Coastguard Worker static inline int get_next_pack_bs_tile_idx(
2904*77c1e3ccSAndroid Build Coastguard Worker     AV1EncPackBSSync *const pack_bs_sync, const int num_tiles) {
2905*77c1e3ccSAndroid Build Coastguard Worker   assert(pack_bs_sync->next_job_idx <= num_tiles);
2906*77c1e3ccSAndroid Build Coastguard Worker   if (pack_bs_sync->next_job_idx == num_tiles) return -1;
2907*77c1e3ccSAndroid Build Coastguard Worker 
2908*77c1e3ccSAndroid Build Coastguard Worker   return pack_bs_sync->pack_bs_tile_order[pack_bs_sync->next_job_idx++]
2909*77c1e3ccSAndroid Build Coastguard Worker       .tile_idx;
2910*77c1e3ccSAndroid Build Coastguard Worker }
2911*77c1e3ccSAndroid Build Coastguard Worker 
2912*77c1e3ccSAndroid Build Coastguard Worker // Calculates bitstream chunk size based on total buffer size and tile or tile
2913*77c1e3ccSAndroid Build Coastguard Worker // 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)2914*77c1e3ccSAndroid Build Coastguard Worker static inline size_t get_bs_chunk_size(int tg_or_tile_size,
2915*77c1e3ccSAndroid Build Coastguard Worker                                        const int frame_or_tg_size,
2916*77c1e3ccSAndroid Build Coastguard Worker                                        size_t *remain_buf_size,
2917*77c1e3ccSAndroid Build Coastguard Worker                                        size_t max_buf_size, int is_last_chunk) {
2918*77c1e3ccSAndroid Build Coastguard Worker   size_t this_chunk_size;
2919*77c1e3ccSAndroid Build Coastguard Worker   assert(*remain_buf_size > 0);
2920*77c1e3ccSAndroid Build Coastguard Worker   if (is_last_chunk) {
2921*77c1e3ccSAndroid Build Coastguard Worker     this_chunk_size = *remain_buf_size;
2922*77c1e3ccSAndroid Build Coastguard Worker     *remain_buf_size = 0;
2923*77c1e3ccSAndroid Build Coastguard Worker   } else {
2924*77c1e3ccSAndroid Build Coastguard Worker     const uint64_t size_scale = (uint64_t)max_buf_size * tg_or_tile_size;
2925*77c1e3ccSAndroid Build Coastguard Worker     this_chunk_size = (size_t)(size_scale / frame_or_tg_size);
2926*77c1e3ccSAndroid Build Coastguard Worker     *remain_buf_size -= this_chunk_size;
2927*77c1e3ccSAndroid Build Coastguard Worker     assert(*remain_buf_size > 0);
2928*77c1e3ccSAndroid Build Coastguard Worker   }
2929*77c1e3ccSAndroid Build Coastguard Worker   assert(this_chunk_size > 0);
2930*77c1e3ccSAndroid Build Coastguard Worker   return this_chunk_size;
2931*77c1e3ccSAndroid Build Coastguard Worker }
2932*77c1e3ccSAndroid Build Coastguard Worker 
2933*77c1e3ccSAndroid Build Coastguard Worker // 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)2934*77c1e3ccSAndroid Build Coastguard Worker static void init_tile_pack_bs_params(AV1_COMP *const cpi, uint8_t *const dst,
2935*77c1e3ccSAndroid Build Coastguard Worker                                      struct aom_write_bit_buffer *saved_wb,
2936*77c1e3ccSAndroid Build Coastguard Worker                                      PackBSParams *const pack_bs_params_arr,
2937*77c1e3ccSAndroid Build Coastguard Worker                                      uint8_t obu_extn_header) {
2938*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
2939*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
2940*77c1e3ccSAndroid Build Coastguard Worker   const CommonTileParams *const tiles = &cm->tiles;
2941*77c1e3ccSAndroid Build Coastguard Worker   const int num_tiles = tiles->cols * tiles->rows;
2942*77c1e3ccSAndroid Build Coastguard Worker   // Fixed size tile groups for the moment
2943*77c1e3ccSAndroid Build Coastguard Worker   const int num_tg_hdrs = cpi->num_tg;
2944*77c1e3ccSAndroid Build Coastguard Worker   // Tile group size in terms of number of tiles.
2945*77c1e3ccSAndroid Build Coastguard Worker   const int tg_size_in_tiles = (num_tiles + num_tg_hdrs - 1) / num_tg_hdrs;
2946*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *tile_dst = dst;
2947*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *tile_data_curr = dst;
2948*77c1e3ccSAndroid Build Coastguard Worker   // Max tile group count can not be more than MAX_TILES.
2949*77c1e3ccSAndroid Build Coastguard Worker   int tg_size_mi[MAX_TILES] = { 0 };  // Size of tile group in mi units
2950*77c1e3ccSAndroid Build Coastguard Worker   int tile_idx;
2951*77c1e3ccSAndroid Build Coastguard Worker   int tg_idx = 0;
2952*77c1e3ccSAndroid Build Coastguard Worker   int tile_count_in_tg = 0;
2953*77c1e3ccSAndroid Build Coastguard Worker   int new_tg = 1;
2954*77c1e3ccSAndroid Build Coastguard Worker 
2955*77c1e3ccSAndroid Build Coastguard Worker   // Populate pack bitstream params of all tiles.
2956*77c1e3ccSAndroid Build Coastguard Worker   for (tile_idx = 0; tile_idx < num_tiles; tile_idx++) {
2957*77c1e3ccSAndroid Build Coastguard Worker     const TileInfo *const tile_info = &cpi->tile_data[tile_idx].tile_info;
2958*77c1e3ccSAndroid Build Coastguard Worker     PackBSParams *const pack_bs_params = &pack_bs_params_arr[tile_idx];
2959*77c1e3ccSAndroid Build Coastguard Worker     // Calculate tile size in mi units.
2960*77c1e3ccSAndroid Build Coastguard Worker     const int tile_size_mi = (tile_info->mi_col_end - tile_info->mi_col_start) *
2961*77c1e3ccSAndroid Build Coastguard Worker                              (tile_info->mi_row_end - tile_info->mi_row_start);
2962*77c1e3ccSAndroid Build Coastguard Worker     int is_last_tile_in_tg = 0;
2963*77c1e3ccSAndroid Build Coastguard Worker     tile_count_in_tg++;
2964*77c1e3ccSAndroid Build Coastguard Worker     if (tile_count_in_tg == tg_size_in_tiles || tile_idx == (num_tiles - 1))
2965*77c1e3ccSAndroid Build Coastguard Worker       is_last_tile_in_tg = 1;
2966*77c1e3ccSAndroid Build Coastguard Worker 
2967*77c1e3ccSAndroid Build Coastguard Worker     // Populate pack bitstream params of this tile.
2968*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_params->curr_tg_hdr_size = 0;
2969*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_params->obu_extn_header = obu_extn_header;
2970*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_params->saved_wb = saved_wb;
2971*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_params->obu_header_size = 0;
2972*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_params->is_last_tile_in_tg = is_last_tile_in_tg;
2973*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_params->new_tg = new_tg;
2974*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_params->tile_col = tile_info->tile_col;
2975*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_params->tile_row = tile_info->tile_row;
2976*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_params->tile_size_mi = tile_size_mi;
2977*77c1e3ccSAndroid Build Coastguard Worker     tg_size_mi[tg_idx] += tile_size_mi;
2978*77c1e3ccSAndroid Build Coastguard Worker 
2979*77c1e3ccSAndroid Build Coastguard Worker     if (new_tg) new_tg = 0;
2980*77c1e3ccSAndroid Build Coastguard Worker     if (is_last_tile_in_tg) {
2981*77c1e3ccSAndroid Build Coastguard Worker       tile_count_in_tg = 0;
2982*77c1e3ccSAndroid Build Coastguard Worker       new_tg = 1;
2983*77c1e3ccSAndroid Build Coastguard Worker       tg_idx++;
2984*77c1e3ccSAndroid Build Coastguard Worker     }
2985*77c1e3ccSAndroid Build Coastguard Worker   }
2986*77c1e3ccSAndroid Build Coastguard Worker 
2987*77c1e3ccSAndroid Build Coastguard Worker   assert(cpi->available_bs_size > 0);
2988*77c1e3ccSAndroid Build Coastguard Worker   size_t tg_buf_size[MAX_TILES] = { 0 };
2989*77c1e3ccSAndroid Build Coastguard Worker   size_t max_buf_size = cpi->available_bs_size;
2990*77c1e3ccSAndroid Build Coastguard Worker   size_t remain_buf_size = max_buf_size;
2991*77c1e3ccSAndroid Build Coastguard Worker   const int frame_size_mi = cm->mi_params.mi_rows * cm->mi_params.mi_cols;
2992*77c1e3ccSAndroid Build Coastguard Worker 
2993*77c1e3ccSAndroid Build Coastguard Worker   tile_idx = 0;
2994*77c1e3ccSAndroid Build Coastguard Worker   // Prepare obu, tile group and frame header of each tile group.
2995*77c1e3ccSAndroid Build Coastguard Worker   for (tg_idx = 0; tg_idx < cpi->num_tg; tg_idx++) {
2996*77c1e3ccSAndroid Build Coastguard Worker     PackBSParams *const pack_bs_params = &pack_bs_params_arr[tile_idx];
2997*77c1e3ccSAndroid Build Coastguard Worker     int is_last_tg = tg_idx == cpi->num_tg - 1;
2998*77c1e3ccSAndroid Build Coastguard Worker     // Prorate bitstream buffer size based on tile group size and available
2999*77c1e3ccSAndroid Build Coastguard Worker     // buffer size. This buffer will be used to store headers and tile data.
3000*77c1e3ccSAndroid Build Coastguard Worker     tg_buf_size[tg_idx] =
3001*77c1e3ccSAndroid Build Coastguard Worker         get_bs_chunk_size(tg_size_mi[tg_idx], frame_size_mi, &remain_buf_size,
3002*77c1e3ccSAndroid Build Coastguard Worker                           max_buf_size, is_last_tg);
3003*77c1e3ccSAndroid Build Coastguard Worker 
3004*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_params->dst = tile_dst;
3005*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_params->tile_data_curr = tile_dst;
3006*77c1e3ccSAndroid Build Coastguard Worker 
3007*77c1e3ccSAndroid Build Coastguard Worker     // Write obu, tile group and frame header at first tile in the tile
3008*77c1e3ccSAndroid Build Coastguard Worker     // group.
3009*77c1e3ccSAndroid Build Coastguard Worker     av1_write_obu_tg_tile_headers(cpi, xd, pack_bs_params, tile_idx);
3010*77c1e3ccSAndroid Build Coastguard Worker     tile_dst += tg_buf_size[tg_idx];
3011*77c1e3ccSAndroid Build Coastguard Worker 
3012*77c1e3ccSAndroid Build Coastguard Worker     // Exclude headers from tile group buffer size.
3013*77c1e3ccSAndroid Build Coastguard Worker     tg_buf_size[tg_idx] -= pack_bs_params->curr_tg_hdr_size;
3014*77c1e3ccSAndroid Build Coastguard Worker     tile_idx += tg_size_in_tiles;
3015*77c1e3ccSAndroid Build Coastguard Worker   }
3016*77c1e3ccSAndroid Build Coastguard Worker 
3017*77c1e3ccSAndroid Build Coastguard Worker   tg_idx = 0;
3018*77c1e3ccSAndroid Build Coastguard Worker   // Calculate bitstream buffer size of each tile in the tile group.
3019*77c1e3ccSAndroid Build Coastguard Worker   for (tile_idx = 0; tile_idx < num_tiles; tile_idx++) {
3020*77c1e3ccSAndroid Build Coastguard Worker     PackBSParams *const pack_bs_params = &pack_bs_params_arr[tile_idx];
3021*77c1e3ccSAndroid Build Coastguard Worker 
3022*77c1e3ccSAndroid Build Coastguard Worker     if (pack_bs_params->new_tg) {
3023*77c1e3ccSAndroid Build Coastguard Worker       max_buf_size = tg_buf_size[tg_idx];
3024*77c1e3ccSAndroid Build Coastguard Worker       remain_buf_size = max_buf_size;
3025*77c1e3ccSAndroid Build Coastguard Worker     }
3026*77c1e3ccSAndroid Build Coastguard Worker 
3027*77c1e3ccSAndroid Build Coastguard Worker     // Prorate bitstream buffer size of this tile based on tile size and
3028*77c1e3ccSAndroid Build Coastguard Worker     // available buffer size. For this proration, header size is not accounted.
3029*77c1e3ccSAndroid Build Coastguard Worker     const size_t tile_buf_size = get_bs_chunk_size(
3030*77c1e3ccSAndroid Build Coastguard Worker         pack_bs_params->tile_size_mi, tg_size_mi[tg_idx], &remain_buf_size,
3031*77c1e3ccSAndroid Build Coastguard Worker         max_buf_size, pack_bs_params->is_last_tile_in_tg);
3032*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_params->tile_buf_size = tile_buf_size;
3033*77c1e3ccSAndroid Build Coastguard Worker 
3034*77c1e3ccSAndroid Build Coastguard Worker     // Update base address of bitstream buffer for tile and tile group.
3035*77c1e3ccSAndroid Build Coastguard Worker     if (pack_bs_params->new_tg) {
3036*77c1e3ccSAndroid Build Coastguard Worker       tile_dst = pack_bs_params->dst;
3037*77c1e3ccSAndroid Build Coastguard Worker       tile_data_curr = pack_bs_params->tile_data_curr;
3038*77c1e3ccSAndroid Build Coastguard Worker       // Account header size in first tile of a tile group.
3039*77c1e3ccSAndroid Build Coastguard Worker       pack_bs_params->tile_buf_size += pack_bs_params->curr_tg_hdr_size;
3040*77c1e3ccSAndroid Build Coastguard Worker     } else {
3041*77c1e3ccSAndroid Build Coastguard Worker       pack_bs_params->dst = tile_dst;
3042*77c1e3ccSAndroid Build Coastguard Worker       pack_bs_params->tile_data_curr = tile_data_curr;
3043*77c1e3ccSAndroid Build Coastguard Worker     }
3044*77c1e3ccSAndroid Build Coastguard Worker 
3045*77c1e3ccSAndroid Build Coastguard Worker     if (pack_bs_params->is_last_tile_in_tg) tg_idx++;
3046*77c1e3ccSAndroid Build Coastguard Worker     tile_dst += pack_bs_params->tile_buf_size;
3047*77c1e3ccSAndroid Build Coastguard Worker   }
3048*77c1e3ccSAndroid Build Coastguard Worker }
3049*77c1e3ccSAndroid Build Coastguard Worker 
3050*77c1e3ccSAndroid Build Coastguard Worker // Worker hook function of pack bitsteam multithreading.
pack_bs_worker_hook(void * arg1,void * arg2)3051*77c1e3ccSAndroid Build Coastguard Worker static int pack_bs_worker_hook(void *arg1, void *arg2) {
3052*77c1e3ccSAndroid Build Coastguard Worker   EncWorkerData *const thread_data = (EncWorkerData *)arg1;
3053*77c1e3ccSAndroid Build Coastguard Worker   PackBSParams *const pack_bs_params = (PackBSParams *)arg2;
3054*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMP *const cpi = thread_data->cpi;
3055*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
3056*77c1e3ccSAndroid Build Coastguard Worker   AV1EncPackBSSync *const pack_bs_sync = &cpi->mt_info.pack_bs_sync;
3057*77c1e3ccSAndroid Build Coastguard Worker   const CommonTileParams *const tiles = &cm->tiles;
3058*77c1e3ccSAndroid Build Coastguard Worker   const int num_tiles = tiles->cols * tiles->rows;
3059*77c1e3ccSAndroid Build Coastguard Worker 
3060*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
3061*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *const pack_bs_mutex = pack_bs_sync->mutex_;
3062*77c1e3ccSAndroid Build Coastguard Worker #endif
3063*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &thread_data->td->mb.e_mbd;
3064*77c1e3ccSAndroid Build Coastguard Worker   struct aom_internal_error_info *const error_info = &thread_data->error_info;
3065*77c1e3ccSAndroid Build Coastguard Worker   xd->error_info = error_info;
3066*77c1e3ccSAndroid Build Coastguard Worker 
3067*77c1e3ccSAndroid Build Coastguard Worker   // The jmp_buf is valid only for the duration of the function that calls
3068*77c1e3ccSAndroid Build Coastguard Worker   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
3069*77c1e3ccSAndroid Build Coastguard Worker   // before it returns.
3070*77c1e3ccSAndroid Build Coastguard Worker   if (setjmp(error_info->jmp)) {
3071*77c1e3ccSAndroid Build Coastguard Worker     error_info->setjmp = 0;
3072*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
3073*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(pack_bs_mutex);
3074*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_sync->pack_bs_mt_exit = true;
3075*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(pack_bs_mutex);
3076*77c1e3ccSAndroid Build Coastguard Worker #endif
3077*77c1e3ccSAndroid Build Coastguard Worker     return 0;
3078*77c1e3ccSAndroid Build Coastguard Worker   }
3079*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 1;
3080*77c1e3ccSAndroid Build Coastguard Worker 
3081*77c1e3ccSAndroid Build Coastguard Worker   while (1) {
3082*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
3083*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(pack_bs_mutex);
3084*77c1e3ccSAndroid Build Coastguard Worker #endif
3085*77c1e3ccSAndroid Build Coastguard Worker     const int tile_idx =
3086*77c1e3ccSAndroid Build Coastguard Worker         pack_bs_sync->pack_bs_mt_exit
3087*77c1e3ccSAndroid Build Coastguard Worker             ? -1
3088*77c1e3ccSAndroid Build Coastguard Worker             : get_next_pack_bs_tile_idx(pack_bs_sync, num_tiles);
3089*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
3090*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(pack_bs_mutex);
3091*77c1e3ccSAndroid Build Coastguard Worker #endif
3092*77c1e3ccSAndroid Build Coastguard Worker     // When pack_bs_mt_exit is set to true, other workers need not pursue any
3093*77c1e3ccSAndroid Build Coastguard Worker     // further jobs.
3094*77c1e3ccSAndroid Build Coastguard Worker     if (tile_idx == -1) break;
3095*77c1e3ccSAndroid Build Coastguard Worker     TileDataEnc *this_tile = &cpi->tile_data[tile_idx];
3096*77c1e3ccSAndroid Build Coastguard Worker     thread_data->td->mb.e_mbd.tile_ctx = &this_tile->tctx;
3097*77c1e3ccSAndroid Build Coastguard Worker 
3098*77c1e3ccSAndroid Build Coastguard Worker     av1_pack_tile_info(cpi, thread_data->td, &pack_bs_params[tile_idx]);
3099*77c1e3ccSAndroid Build Coastguard Worker   }
3100*77c1e3ccSAndroid Build Coastguard Worker 
3101*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 0;
3102*77c1e3ccSAndroid Build Coastguard Worker   return 1;
3103*77c1e3ccSAndroid Build Coastguard Worker }
3104*77c1e3ccSAndroid Build Coastguard Worker 
3105*77c1e3ccSAndroid Build Coastguard Worker // 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)3106*77c1e3ccSAndroid Build Coastguard Worker static void prepare_pack_bs_workers(AV1_COMP *const cpi,
3107*77c1e3ccSAndroid Build Coastguard Worker                                     PackBSParams *const pack_bs_params,
3108*77c1e3ccSAndroid Build Coastguard Worker                                     AVxWorkerHook hook, const int num_workers) {
3109*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
3110*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
3111*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *worker = &mt_info->workers[i];
3112*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *const thread_data = &mt_info->tile_thr_data[i];
3113*77c1e3ccSAndroid Build Coastguard Worker     if (i == 0) {
3114*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td = &cpi->td;
3115*77c1e3ccSAndroid Build Coastguard Worker     } else {
3116*77c1e3ccSAndroid Build Coastguard Worker       thread_data->td = thread_data->original_td;
3117*77c1e3ccSAndroid Build Coastguard Worker     }
3118*77c1e3ccSAndroid Build Coastguard Worker 
3119*77c1e3ccSAndroid Build Coastguard Worker     if (thread_data->td != &cpi->td) thread_data->td->mb = cpi->td.mb;
3120*77c1e3ccSAndroid Build Coastguard Worker 
3121*77c1e3ccSAndroid Build Coastguard Worker     thread_data->cpi = cpi;
3122*77c1e3ccSAndroid Build Coastguard Worker     thread_data->start = i;
3123*77c1e3ccSAndroid Build Coastguard Worker     thread_data->thread_id = i;
3124*77c1e3ccSAndroid Build Coastguard Worker     av1_reset_pack_bs_thread_data(thread_data->td);
3125*77c1e3ccSAndroid Build Coastguard Worker 
3126*77c1e3ccSAndroid Build Coastguard Worker     worker->hook = hook;
3127*77c1e3ccSAndroid Build Coastguard Worker     worker->data1 = thread_data;
3128*77c1e3ccSAndroid Build Coastguard Worker     worker->data2 = pack_bs_params;
3129*77c1e3ccSAndroid Build Coastguard Worker   }
3130*77c1e3ccSAndroid Build Coastguard Worker 
3131*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &cpi->common;
3132*77c1e3ccSAndroid Build Coastguard Worker   AV1EncPackBSSync *const pack_bs_sync = &mt_info->pack_bs_sync;
3133*77c1e3ccSAndroid Build Coastguard Worker   const uint16_t num_tiles = cm->tiles.rows * cm->tiles.cols;
3134*77c1e3ccSAndroid Build Coastguard Worker   pack_bs_sync->next_job_idx = 0;
3135*77c1e3ccSAndroid Build Coastguard Worker   pack_bs_sync->pack_bs_mt_exit = false;
3136*77c1e3ccSAndroid Build Coastguard Worker 
3137*77c1e3ccSAndroid Build Coastguard Worker   PackBSTileOrder *const pack_bs_tile_order = pack_bs_sync->pack_bs_tile_order;
3138*77c1e3ccSAndroid Build Coastguard Worker   // Reset tile order data of pack bitstream
3139*77c1e3ccSAndroid Build Coastguard Worker   av1_zero_array(pack_bs_tile_order, num_tiles);
3140*77c1e3ccSAndroid Build Coastguard Worker 
3141*77c1e3ccSAndroid Build Coastguard Worker   // Populate pack bitstream tile order structure
3142*77c1e3ccSAndroid Build Coastguard Worker   for (uint16_t tile_idx = 0; tile_idx < num_tiles; tile_idx++) {
3143*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_tile_order[tile_idx].abs_sum_level =
3144*77c1e3ccSAndroid Build Coastguard Worker         cpi->tile_data[tile_idx].abs_sum_level;
3145*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_tile_order[tile_idx].tile_idx = tile_idx;
3146*77c1e3ccSAndroid Build Coastguard Worker   }
3147*77c1e3ccSAndroid Build Coastguard Worker 
3148*77c1e3ccSAndroid Build Coastguard Worker   // Sort tiles in descending order based on tile area.
3149*77c1e3ccSAndroid Build Coastguard Worker   qsort(pack_bs_tile_order, num_tiles, sizeof(*pack_bs_tile_order),
3150*77c1e3ccSAndroid Build Coastguard Worker         compare_tile_order);
3151*77c1e3ccSAndroid Build Coastguard Worker }
3152*77c1e3ccSAndroid Build Coastguard Worker 
3153*77c1e3ccSAndroid Build Coastguard Worker // 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)3154*77c1e3ccSAndroid Build Coastguard Worker static void accumulate_pack_bs_data(
3155*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, const PackBSParams *const pack_bs_params_arr,
3156*77c1e3ccSAndroid Build Coastguard Worker     uint8_t *const dst, uint32_t *total_size, const FrameHeaderInfo *fh_info,
3157*77c1e3ccSAndroid Build Coastguard Worker     int *const largest_tile_id, unsigned int *max_tile_size,
3158*77c1e3ccSAndroid Build Coastguard Worker     uint32_t *const obu_header_size, uint8_t **tile_data_start,
3159*77c1e3ccSAndroid Build Coastguard Worker     const int num_workers) {
3160*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
3161*77c1e3ccSAndroid Build Coastguard Worker   const CommonTileParams *const tiles = &cm->tiles;
3162*77c1e3ccSAndroid Build Coastguard Worker   const int tile_count = tiles->cols * tiles->rows;
3163*77c1e3ccSAndroid Build Coastguard Worker   // Fixed size tile groups for the moment
3164*77c1e3ccSAndroid Build Coastguard Worker   size_t curr_tg_data_size = 0;
3165*77c1e3ccSAndroid Build Coastguard Worker   int is_first_tg = 1;
3166*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *curr_tg_start = dst;
3167*77c1e3ccSAndroid Build Coastguard Worker   size_t src_offset = 0;
3168*77c1e3ccSAndroid Build Coastguard Worker   size_t dst_offset = 0;
3169*77c1e3ccSAndroid Build Coastguard Worker 
3170*77c1e3ccSAndroid Build Coastguard Worker   for (int tile_idx = 0; tile_idx < tile_count; tile_idx++) {
3171*77c1e3ccSAndroid Build Coastguard Worker     // PackBSParams stores all parameters required to pack tile and header
3172*77c1e3ccSAndroid Build Coastguard Worker     // info.
3173*77c1e3ccSAndroid Build Coastguard Worker     const PackBSParams *const pack_bs_params = &pack_bs_params_arr[tile_idx];
3174*77c1e3ccSAndroid Build Coastguard Worker     uint32_t tile_size = 0;
3175*77c1e3ccSAndroid Build Coastguard Worker 
3176*77c1e3ccSAndroid Build Coastguard Worker     if (pack_bs_params->new_tg) {
3177*77c1e3ccSAndroid Build Coastguard Worker       curr_tg_start = dst + *total_size;
3178*77c1e3ccSAndroid Build Coastguard Worker       curr_tg_data_size = pack_bs_params->curr_tg_hdr_size;
3179*77c1e3ccSAndroid Build Coastguard Worker       *tile_data_start += pack_bs_params->curr_tg_hdr_size;
3180*77c1e3ccSAndroid Build Coastguard Worker       *obu_header_size = pack_bs_params->obu_header_size;
3181*77c1e3ccSAndroid Build Coastguard Worker     }
3182*77c1e3ccSAndroid Build Coastguard Worker     curr_tg_data_size +=
3183*77c1e3ccSAndroid Build Coastguard Worker         pack_bs_params->buf.size + (pack_bs_params->is_last_tile_in_tg ? 0 : 4);
3184*77c1e3ccSAndroid Build Coastguard Worker 
3185*77c1e3ccSAndroid Build Coastguard Worker     if (pack_bs_params->buf.size > *max_tile_size) {
3186*77c1e3ccSAndroid Build Coastguard Worker       *largest_tile_id = tile_idx;
3187*77c1e3ccSAndroid Build Coastguard Worker       *max_tile_size = (unsigned int)pack_bs_params->buf.size;
3188*77c1e3ccSAndroid Build Coastguard Worker     }
3189*77c1e3ccSAndroid Build Coastguard Worker     tile_size +=
3190*77c1e3ccSAndroid Build Coastguard Worker         (uint32_t)pack_bs_params->buf.size + *pack_bs_params->total_size;
3191*77c1e3ccSAndroid Build Coastguard Worker 
3192*77c1e3ccSAndroid Build Coastguard Worker     // Pack all the chunks of tile bitstreams together
3193*77c1e3ccSAndroid Build Coastguard Worker     if (tile_idx != 0) memmove(dst + dst_offset, dst + src_offset, tile_size);
3194*77c1e3ccSAndroid Build Coastguard Worker 
3195*77c1e3ccSAndroid Build Coastguard Worker     if (pack_bs_params->is_last_tile_in_tg)
3196*77c1e3ccSAndroid Build Coastguard Worker       av1_write_last_tile_info(
3197*77c1e3ccSAndroid Build Coastguard Worker           cpi, fh_info, pack_bs_params->saved_wb, &curr_tg_data_size,
3198*77c1e3ccSAndroid Build Coastguard Worker           curr_tg_start, &tile_size, tile_data_start, largest_tile_id,
3199*77c1e3ccSAndroid Build Coastguard Worker           &is_first_tg, *obu_header_size, pack_bs_params->obu_extn_header);
3200*77c1e3ccSAndroid Build Coastguard Worker     src_offset += pack_bs_params->tile_buf_size;
3201*77c1e3ccSAndroid Build Coastguard Worker     dst_offset += tile_size;
3202*77c1e3ccSAndroid Build Coastguard Worker     *total_size += tile_size;
3203*77c1e3ccSAndroid Build Coastguard Worker   }
3204*77c1e3ccSAndroid Build Coastguard Worker 
3205*77c1e3ccSAndroid Build Coastguard Worker   // Accumulate thread data
3206*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
3207*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = num_workers - 1; idx >= 0; idx--) {
3208*77c1e3ccSAndroid Build Coastguard Worker     ThreadData const *td = mt_info->tile_thr_data[idx].td;
3209*77c1e3ccSAndroid Build Coastguard Worker     av1_accumulate_pack_bs_thread_data(cpi, td);
3210*77c1e3ccSAndroid Build Coastguard Worker   }
3211*77c1e3ccSAndroid Build Coastguard Worker }
3212*77c1e3ccSAndroid Build Coastguard Worker 
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)3213*77c1e3ccSAndroid Build Coastguard Worker void av1_write_tile_obu_mt(
3214*77c1e3ccSAndroid Build Coastguard Worker     AV1_COMP *const cpi, uint8_t *const dst, uint32_t *total_size,
3215*77c1e3ccSAndroid Build Coastguard Worker     struct aom_write_bit_buffer *saved_wb, uint8_t obu_extn_header,
3216*77c1e3ccSAndroid Build Coastguard Worker     const FrameHeaderInfo *fh_info, int *const largest_tile_id,
3217*77c1e3ccSAndroid Build Coastguard Worker     unsigned int *max_tile_size, uint32_t *const obu_header_size,
3218*77c1e3ccSAndroid Build Coastguard Worker     uint8_t **tile_data_start, const int num_workers) {
3219*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *const mt_info = &cpi->mt_info;
3220*77c1e3ccSAndroid Build Coastguard Worker 
3221*77c1e3ccSAndroid Build Coastguard Worker   PackBSParams pack_bs_params[MAX_TILES];
3222*77c1e3ccSAndroid Build Coastguard Worker   uint32_t tile_size[MAX_TILES] = { 0 };
3223*77c1e3ccSAndroid Build Coastguard Worker 
3224*77c1e3ccSAndroid Build Coastguard Worker   for (int tile_idx = 0; tile_idx < MAX_TILES; tile_idx++)
3225*77c1e3ccSAndroid Build Coastguard Worker     pack_bs_params[tile_idx].total_size = &tile_size[tile_idx];
3226*77c1e3ccSAndroid Build Coastguard Worker 
3227*77c1e3ccSAndroid Build Coastguard Worker   init_tile_pack_bs_params(cpi, dst, saved_wb, pack_bs_params, obu_extn_header);
3228*77c1e3ccSAndroid Build Coastguard Worker   prepare_pack_bs_workers(cpi, pack_bs_params, pack_bs_worker_hook,
3229*77c1e3ccSAndroid Build Coastguard Worker                           num_workers);
3230*77c1e3ccSAndroid Build Coastguard Worker   launch_workers(mt_info, num_workers);
3231*77c1e3ccSAndroid Build Coastguard Worker   sync_enc_workers(mt_info, &cpi->common, num_workers);
3232*77c1e3ccSAndroid Build Coastguard Worker   accumulate_pack_bs_data(cpi, pack_bs_params, dst, total_size, fh_info,
3233*77c1e3ccSAndroid Build Coastguard Worker                           largest_tile_id, max_tile_size, obu_header_size,
3234*77c1e3ccSAndroid Build Coastguard Worker                           tile_data_start, num_workers);
3235*77c1e3ccSAndroid Build Coastguard Worker }
3236*77c1e3ccSAndroid Build Coastguard Worker 
3237*77c1e3ccSAndroid Build Coastguard Worker // Deallocate memory for CDEF search multi-thread synchronization.
av1_cdef_mt_dealloc(AV1CdefSync * cdef_sync)3238*77c1e3ccSAndroid Build Coastguard Worker void av1_cdef_mt_dealloc(AV1CdefSync *cdef_sync) {
3239*77c1e3ccSAndroid Build Coastguard Worker   (void)cdef_sync;
3240*77c1e3ccSAndroid Build Coastguard Worker   assert(cdef_sync != NULL);
3241*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
3242*77c1e3ccSAndroid Build Coastguard Worker   if (cdef_sync->mutex_ != NULL) {
3243*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_destroy(cdef_sync->mutex_);
3244*77c1e3ccSAndroid Build Coastguard Worker     aom_free(cdef_sync->mutex_);
3245*77c1e3ccSAndroid Build Coastguard Worker   }
3246*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
3247*77c1e3ccSAndroid Build Coastguard Worker }
3248*77c1e3ccSAndroid Build Coastguard Worker 
3249*77c1e3ccSAndroid Build Coastguard Worker // Updates the row and column indices of the next job to be processed.
3250*77c1e3ccSAndroid Build Coastguard Worker // 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)3251*77c1e3ccSAndroid Build Coastguard Worker static void update_next_job_info(AV1CdefSync *cdef_sync, int nvfb, int nhfb) {
3252*77c1e3ccSAndroid Build Coastguard Worker   cdef_sync->fbc++;
3253*77c1e3ccSAndroid Build Coastguard Worker   if (cdef_sync->fbc == nhfb) {
3254*77c1e3ccSAndroid Build Coastguard Worker     cdef_sync->fbr++;
3255*77c1e3ccSAndroid Build Coastguard Worker     if (cdef_sync->fbr == nvfb) {
3256*77c1e3ccSAndroid Build Coastguard Worker       cdef_sync->end_of_frame = 1;
3257*77c1e3ccSAndroid Build Coastguard Worker     } else {
3258*77c1e3ccSAndroid Build Coastguard Worker       cdef_sync->fbc = 0;
3259*77c1e3ccSAndroid Build Coastguard Worker     }
3260*77c1e3ccSAndroid Build Coastguard Worker   }
3261*77c1e3ccSAndroid Build Coastguard Worker }
3262*77c1e3ccSAndroid Build Coastguard Worker 
3263*77c1e3ccSAndroid Build Coastguard Worker // Initializes cdef_sync parameters.
cdef_reset_job_info(AV1CdefSync * cdef_sync)3264*77c1e3ccSAndroid Build Coastguard Worker static inline void cdef_reset_job_info(AV1CdefSync *cdef_sync) {
3265*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
3266*77c1e3ccSAndroid Build Coastguard Worker   if (cdef_sync->mutex_) pthread_mutex_init(cdef_sync->mutex_, NULL);
3267*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
3268*77c1e3ccSAndroid Build Coastguard Worker   cdef_sync->end_of_frame = 0;
3269*77c1e3ccSAndroid Build Coastguard Worker   cdef_sync->fbr = 0;
3270*77c1e3ccSAndroid Build Coastguard Worker   cdef_sync->fbc = 0;
3271*77c1e3ccSAndroid Build Coastguard Worker   cdef_sync->cdef_mt_exit = false;
3272*77c1e3ccSAndroid Build Coastguard Worker }
3273*77c1e3ccSAndroid Build Coastguard Worker 
3274*77c1e3ccSAndroid Build Coastguard Worker // Checks if a job is available. If job is available,
3275*77c1e3ccSAndroid Build Coastguard Worker // populates next job information and returns 1, else returns 0.
cdef_get_next_job(AV1CdefSync * cdef_sync,CdefSearchCtx * cdef_search_ctx,volatile int * cur_fbr,volatile int * cur_fbc,volatile int * sb_count)3276*77c1e3ccSAndroid Build Coastguard Worker static inline int cdef_get_next_job(AV1CdefSync *cdef_sync,
3277*77c1e3ccSAndroid Build Coastguard Worker                                     CdefSearchCtx *cdef_search_ctx,
3278*77c1e3ccSAndroid Build Coastguard Worker                                     volatile int *cur_fbr,
3279*77c1e3ccSAndroid Build Coastguard Worker                                     volatile int *cur_fbc,
3280*77c1e3ccSAndroid Build Coastguard Worker                                     volatile int *sb_count) {
3281*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
3282*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_lock(cdef_sync->mutex_);
3283*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
3284*77c1e3ccSAndroid Build Coastguard Worker   int do_next_block = 0;
3285*77c1e3ccSAndroid Build Coastguard Worker   const int nvfb = cdef_search_ctx->nvfb;
3286*77c1e3ccSAndroid Build Coastguard Worker   const int nhfb = cdef_search_ctx->nhfb;
3287*77c1e3ccSAndroid Build Coastguard Worker 
3288*77c1e3ccSAndroid Build Coastguard Worker   // If a block is skip, do not process the block and
3289*77c1e3ccSAndroid Build Coastguard Worker   // check the skip condition for the next block.
3290*77c1e3ccSAndroid Build Coastguard Worker   while (!cdef_sync->cdef_mt_exit && !cdef_sync->end_of_frame &&
3291*77c1e3ccSAndroid Build Coastguard Worker          cdef_sb_skip(cdef_search_ctx->mi_params, cdef_sync->fbr,
3292*77c1e3ccSAndroid Build Coastguard Worker                       cdef_sync->fbc)) {
3293*77c1e3ccSAndroid Build Coastguard Worker     update_next_job_info(cdef_sync, nvfb, nhfb);
3294*77c1e3ccSAndroid Build Coastguard Worker   }
3295*77c1e3ccSAndroid Build Coastguard Worker 
3296*77c1e3ccSAndroid Build Coastguard Worker   // Populates information needed for current job and update the row,
3297*77c1e3ccSAndroid Build Coastguard Worker   // column indices of the next block to be processed.
3298*77c1e3ccSAndroid Build Coastguard Worker   if (!cdef_sync->cdef_mt_exit && cdef_sync->end_of_frame == 0) {
3299*77c1e3ccSAndroid Build Coastguard Worker     do_next_block = 1;
3300*77c1e3ccSAndroid Build Coastguard Worker     *cur_fbr = cdef_sync->fbr;
3301*77c1e3ccSAndroid Build Coastguard Worker     *cur_fbc = cdef_sync->fbc;
3302*77c1e3ccSAndroid Build Coastguard Worker     *sb_count = cdef_search_ctx->sb_count;
3303*77c1e3ccSAndroid Build Coastguard Worker     cdef_search_ctx->sb_count++;
3304*77c1e3ccSAndroid Build Coastguard Worker     update_next_job_info(cdef_sync, nvfb, nhfb);
3305*77c1e3ccSAndroid Build Coastguard Worker   }
3306*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
3307*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_unlock(cdef_sync->mutex_);
3308*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
3309*77c1e3ccSAndroid Build Coastguard Worker   return do_next_block;
3310*77c1e3ccSAndroid Build Coastguard Worker }
3311*77c1e3ccSAndroid Build Coastguard Worker 
3312*77c1e3ccSAndroid Build Coastguard Worker // Hook function for each thread in CDEF search multi-threading.
cdef_filter_block_worker_hook(void * arg1,void * arg2)3313*77c1e3ccSAndroid Build Coastguard Worker static int cdef_filter_block_worker_hook(void *arg1, void *arg2) {
3314*77c1e3ccSAndroid Build Coastguard Worker   EncWorkerData *thread_data = (EncWorkerData *)arg1;
3315*77c1e3ccSAndroid Build Coastguard Worker   AV1CdefSync *const cdef_sync = (AV1CdefSync *)arg2;
3316*77c1e3ccSAndroid Build Coastguard Worker 
3317*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
3318*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *cdef_mutex_ = cdef_sync->mutex_;
3319*77c1e3ccSAndroid Build Coastguard Worker #endif
3320*77c1e3ccSAndroid Build Coastguard Worker   struct aom_internal_error_info *const error_info = &thread_data->error_info;
3321*77c1e3ccSAndroid Build Coastguard Worker   CdefSearchCtx *cdef_search_ctx = thread_data->cpi->cdef_search_ctx;
3322*77c1e3ccSAndroid Build Coastguard Worker 
3323*77c1e3ccSAndroid Build Coastguard Worker   // The jmp_buf is valid only for the duration of the function that calls
3324*77c1e3ccSAndroid Build Coastguard Worker   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
3325*77c1e3ccSAndroid Build Coastguard Worker   // before it returns.
3326*77c1e3ccSAndroid Build Coastguard Worker   if (setjmp(error_info->jmp)) {
3327*77c1e3ccSAndroid Build Coastguard Worker     error_info->setjmp = 0;
3328*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
3329*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(cdef_mutex_);
3330*77c1e3ccSAndroid Build Coastguard Worker     cdef_sync->cdef_mt_exit = true;
3331*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(cdef_mutex_);
3332*77c1e3ccSAndroid Build Coastguard Worker #endif
3333*77c1e3ccSAndroid Build Coastguard Worker     return 0;
3334*77c1e3ccSAndroid Build Coastguard Worker   }
3335*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 1;
3336*77c1e3ccSAndroid Build Coastguard Worker 
3337*77c1e3ccSAndroid Build Coastguard Worker   volatile int cur_fbr, cur_fbc, sb_count;
3338*77c1e3ccSAndroid Build Coastguard Worker   while (cdef_get_next_job(cdef_sync, cdef_search_ctx, &cur_fbr, &cur_fbc,
3339*77c1e3ccSAndroid Build Coastguard Worker                            &sb_count)) {
3340*77c1e3ccSAndroid Build Coastguard Worker     av1_cdef_mse_calc_block(cdef_search_ctx, error_info, cur_fbr, cur_fbc,
3341*77c1e3ccSAndroid Build Coastguard Worker                             sb_count);
3342*77c1e3ccSAndroid Build Coastguard Worker   }
3343*77c1e3ccSAndroid Build Coastguard Worker   error_info->setjmp = 0;
3344*77c1e3ccSAndroid Build Coastguard Worker   return 1;
3345*77c1e3ccSAndroid Build Coastguard Worker }
3346*77c1e3ccSAndroid Build Coastguard Worker 
3347*77c1e3ccSAndroid Build Coastguard Worker // Assigns CDEF search hook function and thread data to each worker.
prepare_cdef_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)3348*77c1e3ccSAndroid Build Coastguard Worker static void prepare_cdef_workers(AV1_COMP *cpi, AVxWorkerHook hook,
3349*77c1e3ccSAndroid Build Coastguard Worker                                  int num_workers) {
3350*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *mt_info = &cpi->mt_info;
3351*77c1e3ccSAndroid Build Coastguard Worker   for (int i = num_workers - 1; i >= 0; i--) {
3352*77c1e3ccSAndroid Build Coastguard Worker     AVxWorker *worker = &mt_info->workers[i];
3353*77c1e3ccSAndroid Build Coastguard Worker     EncWorkerData *thread_data = &mt_info->tile_thr_data[i];
3354*77c1e3ccSAndroid Build Coastguard Worker 
3355*77c1e3ccSAndroid Build Coastguard Worker     thread_data->cpi = cpi;
3356*77c1e3ccSAndroid Build Coastguard Worker     worker->hook = hook;
3357*77c1e3ccSAndroid Build Coastguard Worker     worker->data1 = thread_data;
3358*77c1e3ccSAndroid Build Coastguard Worker     worker->data2 = &mt_info->cdef_sync;
3359*77c1e3ccSAndroid Build Coastguard Worker   }
3360*77c1e3ccSAndroid Build Coastguard Worker }
3361*77c1e3ccSAndroid Build Coastguard Worker 
3362*77c1e3ccSAndroid Build Coastguard Worker // Implements multi-threading for CDEF search.
av1_cdef_mse_calc_frame_mt(AV1_COMP * cpi)3363*77c1e3ccSAndroid Build Coastguard Worker void av1_cdef_mse_calc_frame_mt(AV1_COMP *cpi) {
3364*77c1e3ccSAndroid Build Coastguard Worker   MultiThreadInfo *mt_info = &cpi->mt_info;
3365*77c1e3ccSAndroid Build Coastguard Worker   AV1CdefSync *cdef_sync = &mt_info->cdef_sync;
3366*77c1e3ccSAndroid Build Coastguard Worker   const int num_workers = mt_info->num_mod_workers[MOD_CDEF_SEARCH];
3367*77c1e3ccSAndroid Build Coastguard Worker 
3368*77c1e3ccSAndroid Build Coastguard Worker   cdef_reset_job_info(cdef_sync);
3369*77c1e3ccSAndroid Build Coastguard Worker   prepare_cdef_workers(cpi, cdef_filter_block_worker_hook, num_workers);
3370*77c1e3ccSAndroid Build Coastguard Worker   launch_workers(mt_info, num_workers);
3371*77c1e3ccSAndroid Build Coastguard Worker   sync_enc_workers(mt_info, &cpi->common, num_workers);
3372*77c1e3ccSAndroid Build Coastguard Worker }
3373*77c1e3ccSAndroid Build Coastguard Worker 
3374*77c1e3ccSAndroid Build Coastguard Worker // Computes num_workers for temporal filter multi-threading.
compute_num_tf_workers(const AV1_COMP * cpi)3375*77c1e3ccSAndroid Build Coastguard Worker static inline int compute_num_tf_workers(const AV1_COMP *cpi) {
3376*77c1e3ccSAndroid Build Coastguard Worker   // For single-pass encode, using no. of workers as per tf block size was not
3377*77c1e3ccSAndroid Build Coastguard Worker   // found to improve speed. Hence the thread assignment for single-pass encode
3378*77c1e3ccSAndroid Build Coastguard Worker   // is kept based on compute_num_enc_workers().
3379*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.pass < AOM_RC_SECOND_PASS)
3380*77c1e3ccSAndroid Build Coastguard Worker     return compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
3381*77c1e3ccSAndroid Build Coastguard Worker 
3382*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.max_threads <= 1) return 1;
3383*77c1e3ccSAndroid Build Coastguard Worker 
3384*77c1e3ccSAndroid Build Coastguard Worker   const int frame_height = cpi->common.height;
3385*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE block_size = TF_BLOCK_SIZE;
3386*77c1e3ccSAndroid Build Coastguard Worker   const int mb_height = block_size_high[block_size];
3387*77c1e3ccSAndroid Build Coastguard Worker   const int mb_rows = get_num_blocks(frame_height, mb_height);
3388*77c1e3ccSAndroid Build Coastguard Worker   return AOMMIN(cpi->oxcf.max_threads, mb_rows);
3389*77c1e3ccSAndroid Build Coastguard Worker }
3390*77c1e3ccSAndroid Build Coastguard Worker 
3391*77c1e3ccSAndroid Build Coastguard Worker // Computes num_workers for tpl multi-threading.
compute_num_tpl_workers(AV1_COMP * cpi)3392*77c1e3ccSAndroid Build Coastguard Worker static inline int compute_num_tpl_workers(AV1_COMP *cpi) {
3393*77c1e3ccSAndroid Build Coastguard Worker   return compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
3394*77c1e3ccSAndroid Build Coastguard Worker }
3395*77c1e3ccSAndroid Build Coastguard Worker 
3396*77c1e3ccSAndroid Build Coastguard Worker // Computes num_workers for loop filter multi-threading.
compute_num_lf_workers(AV1_COMP * cpi)3397*77c1e3ccSAndroid Build Coastguard Worker static inline int compute_num_lf_workers(AV1_COMP *cpi) {
3398*77c1e3ccSAndroid Build Coastguard Worker   return compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
3399*77c1e3ccSAndroid Build Coastguard Worker }
3400*77c1e3ccSAndroid Build Coastguard Worker 
3401*77c1e3ccSAndroid Build Coastguard Worker // Computes num_workers for cdef multi-threading.
compute_num_cdef_workers(AV1_COMP * cpi)3402*77c1e3ccSAndroid Build Coastguard Worker static inline int compute_num_cdef_workers(AV1_COMP *cpi) {
3403*77c1e3ccSAndroid Build Coastguard Worker   return compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
3404*77c1e3ccSAndroid Build Coastguard Worker }
3405*77c1e3ccSAndroid Build Coastguard Worker 
3406*77c1e3ccSAndroid Build Coastguard Worker // Computes num_workers for loop-restoration multi-threading.
compute_num_lr_workers(AV1_COMP * cpi)3407*77c1e3ccSAndroid Build Coastguard Worker static inline int compute_num_lr_workers(AV1_COMP *cpi) {
3408*77c1e3ccSAndroid Build Coastguard Worker   return compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
3409*77c1e3ccSAndroid Build Coastguard Worker }
3410*77c1e3ccSAndroid Build Coastguard Worker 
3411*77c1e3ccSAndroid Build Coastguard Worker // Computes num_workers for pack bitstream multi-threading.
compute_num_pack_bs_workers(AV1_COMP * cpi)3412*77c1e3ccSAndroid Build Coastguard Worker static inline int compute_num_pack_bs_workers(AV1_COMP *cpi) {
3413*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.max_threads <= 1) return 1;
3414*77c1e3ccSAndroid Build Coastguard Worker   return compute_num_enc_tile_mt_workers(&cpi->common, cpi->oxcf.max_threads);
3415*77c1e3ccSAndroid Build Coastguard Worker }
3416*77c1e3ccSAndroid Build Coastguard Worker 
3417*77c1e3ccSAndroid Build Coastguard Worker // Computes num_workers for all intra multi-threading.
compute_num_ai_workers(AV1_COMP * cpi)3418*77c1e3ccSAndroid Build Coastguard Worker static inline int compute_num_ai_workers(AV1_COMP *cpi) {
3419*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.max_threads <= 1) return 1;
3420*77c1e3ccSAndroid Build Coastguard Worker   // The multi-threading implementation of deltaq-mode = 3 in allintra
3421*77c1e3ccSAndroid Build Coastguard Worker   // mode is based on row multi threading.
3422*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->oxcf.row_mt) return 1;
3423*77c1e3ccSAndroid Build Coastguard Worker   cpi->weber_bsize = BLOCK_8X8;
3424*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = cpi->weber_bsize;
3425*77c1e3ccSAndroid Build Coastguard Worker   const int mb_step = mi_size_wide[bsize];
3426*77c1e3ccSAndroid Build Coastguard Worker   const int num_mb_rows = cpi->common.mi_params.mi_rows / mb_step;
3427*77c1e3ccSAndroid Build Coastguard Worker   return AOMMIN(num_mb_rows, cpi->oxcf.max_threads);
3428*77c1e3ccSAndroid Build Coastguard Worker }
3429*77c1e3ccSAndroid Build Coastguard Worker 
compute_num_mod_workers(AV1_COMP * cpi,MULTI_THREADED_MODULES mod_name)3430*77c1e3ccSAndroid Build Coastguard Worker static int compute_num_mod_workers(AV1_COMP *cpi,
3431*77c1e3ccSAndroid Build Coastguard Worker                                    MULTI_THREADED_MODULES mod_name) {
3432*77c1e3ccSAndroid Build Coastguard Worker   int num_mod_workers = 0;
3433*77c1e3ccSAndroid Build Coastguard Worker   switch (mod_name) {
3434*77c1e3ccSAndroid Build Coastguard Worker     case MOD_FP:
3435*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->oxcf.pass >= AOM_RC_SECOND_PASS)
3436*77c1e3ccSAndroid Build Coastguard Worker         num_mod_workers = 0;
3437*77c1e3ccSAndroid Build Coastguard Worker       else
3438*77c1e3ccSAndroid Build Coastguard Worker         num_mod_workers = compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
3439*77c1e3ccSAndroid Build Coastguard Worker       break;
3440*77c1e3ccSAndroid Build Coastguard Worker     case MOD_TF: num_mod_workers = compute_num_tf_workers(cpi); break;
3441*77c1e3ccSAndroid Build Coastguard Worker     case MOD_TPL: num_mod_workers = compute_num_tpl_workers(cpi); break;
3442*77c1e3ccSAndroid Build Coastguard Worker     case MOD_GME: num_mod_workers = 1; break;
3443*77c1e3ccSAndroid Build Coastguard Worker     case MOD_ENC:
3444*77c1e3ccSAndroid Build Coastguard Worker       num_mod_workers = compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
3445*77c1e3ccSAndroid Build Coastguard Worker       break;
3446*77c1e3ccSAndroid Build Coastguard Worker     case MOD_LPF: num_mod_workers = compute_num_lf_workers(cpi); break;
3447*77c1e3ccSAndroid Build Coastguard Worker     case MOD_CDEF_SEARCH:
3448*77c1e3ccSAndroid Build Coastguard Worker       num_mod_workers = compute_num_cdef_workers(cpi);
3449*77c1e3ccSAndroid Build Coastguard Worker       break;
3450*77c1e3ccSAndroid Build Coastguard Worker     case MOD_CDEF: num_mod_workers = compute_num_cdef_workers(cpi); break;
3451*77c1e3ccSAndroid Build Coastguard Worker     case MOD_LR: num_mod_workers = compute_num_lr_workers(cpi); break;
3452*77c1e3ccSAndroid Build Coastguard Worker     case MOD_PACK_BS: num_mod_workers = compute_num_pack_bs_workers(cpi); break;
3453*77c1e3ccSAndroid Build Coastguard Worker     case MOD_FRAME_ENC:
3454*77c1e3ccSAndroid Build Coastguard Worker       num_mod_workers = cpi->ppi->p_mt_info.num_mod_workers[MOD_FRAME_ENC];
3455*77c1e3ccSAndroid Build Coastguard Worker       break;
3456*77c1e3ccSAndroid Build Coastguard Worker     case MOD_AI:
3457*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->oxcf.pass == AOM_RC_ONE_PASS) {
3458*77c1e3ccSAndroid Build Coastguard Worker         num_mod_workers = compute_num_ai_workers(cpi);
3459*77c1e3ccSAndroid Build Coastguard Worker       } else {
3460*77c1e3ccSAndroid Build Coastguard Worker         num_mod_workers = 0;
3461*77c1e3ccSAndroid Build Coastguard Worker       }
3462*77c1e3ccSAndroid Build Coastguard Worker       break;
3463*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0); break;
3464*77c1e3ccSAndroid Build Coastguard Worker   }
3465*77c1e3ccSAndroid Build Coastguard Worker   return (num_mod_workers);
3466*77c1e3ccSAndroid Build Coastguard Worker }
3467*77c1e3ccSAndroid Build Coastguard Worker // Computes the number of workers for each MT modules in the encoder
av1_compute_num_workers_for_mt(AV1_COMP * cpi)3468*77c1e3ccSAndroid Build Coastguard Worker void av1_compute_num_workers_for_mt(AV1_COMP *cpi) {
3469*77c1e3ccSAndroid Build Coastguard Worker   for (int i = MOD_FP; i < NUM_MT_MODULES; i++) {
3470*77c1e3ccSAndroid Build Coastguard Worker     cpi->ppi->p_mt_info.num_mod_workers[i] =
3471*77c1e3ccSAndroid Build Coastguard Worker         compute_num_mod_workers(cpi, (MULTI_THREADED_MODULES)i);
3472*77c1e3ccSAndroid Build Coastguard Worker   }
3473*77c1e3ccSAndroid Build Coastguard Worker }
3474