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