1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include "av1/common/warped_motion.h"
13
14 #include "av1/encoder/encodeframe.h"
15 #include "av1/encoder/encoder.h"
16 #include "av1/encoder/encoder_alloc.h"
17 #include "av1/encoder/ethread.h"
18 #if !CONFIG_REALTIME_ONLY
19 #include "av1/encoder/firstpass.h"
20 #endif
21 #include "av1/encoder/global_motion.h"
22 #include "av1/encoder/global_motion_facade.h"
23 #include "av1/encoder/rdopt.h"
24 #include "aom_dsp/aom_dsp_common.h"
25 #include "av1/encoder/temporal_filter.h"
26 #include "av1/encoder/tpl_model.h"
27
accumulate_rd_opt(ThreadData * td,ThreadData * td_t)28 static AOM_INLINE void accumulate_rd_opt(ThreadData *td, ThreadData *td_t) {
29 for (int i = 0; i < REFERENCE_MODES; i++)
30 td->rd_counts.comp_pred_diff[i] += td_t->rd_counts.comp_pred_diff[i];
31
32 td->rd_counts.compound_ref_used_flag |=
33 td_t->rd_counts.compound_ref_used_flag;
34 td->rd_counts.skip_mode_used_flag |= td_t->rd_counts.skip_mode_used_flag;
35
36 for (int i = 0; i < TX_SIZES_ALL; i++) {
37 for (int j = 0; j < TX_TYPES; j++)
38 td->rd_counts.tx_type_used[i][j] += td_t->rd_counts.tx_type_used[i][j];
39 }
40
41 for (int i = 0; i < BLOCK_SIZES_ALL; i++) {
42 for (int j = 0; j < 2; j++) {
43 td->rd_counts.obmc_used[i][j] += td_t->rd_counts.obmc_used[i][j];
44 }
45 }
46
47 for (int i = 0; i < 2; i++) {
48 td->rd_counts.warped_used[i] += td_t->rd_counts.warped_used[i];
49 }
50 }
51
update_delta_lf_for_row_mt(AV1_COMP * cpi)52 static AOM_INLINE void update_delta_lf_for_row_mt(AV1_COMP *cpi) {
53 AV1_COMMON *cm = &cpi->common;
54 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
55 const int mib_size = cm->seq_params.mib_size;
56 const int frame_lf_count =
57 av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
58 for (int row = 0; row < cm->tiles.rows; row++) {
59 for (int col = 0; col < cm->tiles.cols; col++) {
60 TileDataEnc *tile_data = &cpi->tile_data[row * cm->tiles.cols + col];
61 const TileInfo *const tile_info = &tile_data->tile_info;
62 for (int mi_row = tile_info->mi_row_start; mi_row < tile_info->mi_row_end;
63 mi_row += mib_size) {
64 if (mi_row == tile_info->mi_row_start)
65 av1_reset_loop_filter_delta(xd, av1_num_planes(cm));
66 for (int mi_col = tile_info->mi_col_start;
67 mi_col < tile_info->mi_col_end; mi_col += mib_size) {
68 const int idx_str = cm->mi_params.mi_stride * mi_row + mi_col;
69 MB_MODE_INFO **mi = cm->mi_params.mi_grid_base + idx_str;
70 MB_MODE_INFO *mbmi = mi[0];
71 if (mbmi->skip_txfm == 1 && (mbmi->bsize == cm->seq_params.sb_size)) {
72 for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id)
73 mbmi->delta_lf[lf_id] = xd->delta_lf[lf_id];
74 mbmi->delta_lf_from_base = xd->delta_lf_from_base;
75 } else {
76 if (cm->delta_q_info.delta_lf_multi) {
77 for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id)
78 xd->delta_lf[lf_id] = mbmi->delta_lf[lf_id];
79 } else {
80 xd->delta_lf_from_base = mbmi->delta_lf_from_base;
81 }
82 }
83 }
84 }
85 }
86 }
87 }
88
av1_row_mt_sync_read_dummy(AV1EncRowMultiThreadSync * row_mt_sync,int r,int c)89 void av1_row_mt_sync_read_dummy(AV1EncRowMultiThreadSync *row_mt_sync, int r,
90 int c) {
91 (void)row_mt_sync;
92 (void)r;
93 (void)c;
94 return;
95 }
96
av1_row_mt_sync_write_dummy(AV1EncRowMultiThreadSync * row_mt_sync,int r,int c,int cols)97 void av1_row_mt_sync_write_dummy(AV1EncRowMultiThreadSync *row_mt_sync, int r,
98 int c, int cols) {
99 (void)row_mt_sync;
100 (void)r;
101 (void)c;
102 (void)cols;
103 return;
104 }
105
av1_row_mt_sync_read(AV1EncRowMultiThreadSync * row_mt_sync,int r,int c)106 void av1_row_mt_sync_read(AV1EncRowMultiThreadSync *row_mt_sync, int r, int c) {
107 #if CONFIG_MULTITHREAD
108 const int nsync = row_mt_sync->sync_range;
109
110 if (r) {
111 pthread_mutex_t *const mutex = &row_mt_sync->mutex_[r - 1];
112 pthread_mutex_lock(mutex);
113
114 while (c > row_mt_sync->num_finished_cols[r - 1] - nsync) {
115 pthread_cond_wait(&row_mt_sync->cond_[r - 1], mutex);
116 }
117 pthread_mutex_unlock(mutex);
118 }
119 #else
120 (void)row_mt_sync;
121 (void)r;
122 (void)c;
123 #endif // CONFIG_MULTITHREAD
124 }
125
av1_row_mt_sync_write(AV1EncRowMultiThreadSync * row_mt_sync,int r,int c,int cols)126 void av1_row_mt_sync_write(AV1EncRowMultiThreadSync *row_mt_sync, int r, int c,
127 int cols) {
128 #if CONFIG_MULTITHREAD
129 const int nsync = row_mt_sync->sync_range;
130 int cur;
131 // Only signal when there are enough encoded blocks for next row to run.
132 int sig = 1;
133
134 if (c < cols - 1) {
135 cur = c;
136 if (c % nsync) sig = 0;
137 } else {
138 cur = cols + nsync;
139 }
140
141 if (sig) {
142 pthread_mutex_lock(&row_mt_sync->mutex_[r]);
143
144 row_mt_sync->num_finished_cols[r] = cur;
145
146 pthread_cond_signal(&row_mt_sync->cond_[r]);
147 pthread_mutex_unlock(&row_mt_sync->mutex_[r]);
148 }
149 #else
150 (void)row_mt_sync;
151 (void)r;
152 (void)c;
153 (void)cols;
154 #endif // CONFIG_MULTITHREAD
155 }
156
157 // Allocate memory for row synchronization
row_mt_sync_mem_alloc(AV1EncRowMultiThreadSync * row_mt_sync,AV1_COMMON * cm,int rows)158 static void row_mt_sync_mem_alloc(AV1EncRowMultiThreadSync *row_mt_sync,
159 AV1_COMMON *cm, int rows) {
160 #if CONFIG_MULTITHREAD
161 int i;
162
163 CHECK_MEM_ERROR(cm, row_mt_sync->mutex_,
164 aom_malloc(sizeof(*row_mt_sync->mutex_) * rows));
165 if (row_mt_sync->mutex_) {
166 for (i = 0; i < rows; ++i) {
167 pthread_mutex_init(&row_mt_sync->mutex_[i], NULL);
168 }
169 }
170
171 CHECK_MEM_ERROR(cm, row_mt_sync->cond_,
172 aom_malloc(sizeof(*row_mt_sync->cond_) * rows));
173 if (row_mt_sync->cond_) {
174 for (i = 0; i < rows; ++i) {
175 pthread_cond_init(&row_mt_sync->cond_[i], NULL);
176 }
177 }
178 #endif // CONFIG_MULTITHREAD
179
180 CHECK_MEM_ERROR(cm, row_mt_sync->num_finished_cols,
181 aom_malloc(sizeof(*row_mt_sync->num_finished_cols) * rows));
182
183 row_mt_sync->rows = rows;
184 // Set up nsync.
185 row_mt_sync->sync_range = 1;
186 }
187
188 // Deallocate row based multi-threading synchronization related mutex and data
row_mt_sync_mem_dealloc(AV1EncRowMultiThreadSync * row_mt_sync)189 static void row_mt_sync_mem_dealloc(AV1EncRowMultiThreadSync *row_mt_sync) {
190 if (row_mt_sync != NULL) {
191 #if CONFIG_MULTITHREAD
192 int i;
193
194 if (row_mt_sync->mutex_ != NULL) {
195 for (i = 0; i < row_mt_sync->rows; ++i) {
196 pthread_mutex_destroy(&row_mt_sync->mutex_[i]);
197 }
198 aom_free(row_mt_sync->mutex_);
199 }
200 if (row_mt_sync->cond_ != NULL) {
201 for (i = 0; i < row_mt_sync->rows; ++i) {
202 pthread_cond_destroy(&row_mt_sync->cond_[i]);
203 }
204 aom_free(row_mt_sync->cond_);
205 }
206 #endif // CONFIG_MULTITHREAD
207 aom_free(row_mt_sync->num_finished_cols);
208
209 // clear the structure as the source of this call may be dynamic change
210 // in tiles in which case this call will be followed by an _alloc()
211 // which may fail.
212 av1_zero(*row_mt_sync);
213 }
214 }
215
row_mt_mem_alloc(AV1_COMP * cpi,int max_rows,int max_cols,int alloc_row_ctx)216 static void row_mt_mem_alloc(AV1_COMP *cpi, int max_rows, int max_cols,
217 int alloc_row_ctx) {
218 struct AV1Common *cm = &cpi->common;
219 AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
220 const int tile_cols = cm->tiles.cols;
221 const int tile_rows = cm->tiles.rows;
222 int tile_col, tile_row;
223
224 // Allocate memory for row based multi-threading
225 for (tile_row = 0; tile_row < tile_rows; tile_row++) {
226 for (tile_col = 0; tile_col < tile_cols; tile_col++) {
227 int tile_index = tile_row * tile_cols + tile_col;
228 TileDataEnc *const this_tile = &cpi->tile_data[tile_index];
229
230 row_mt_sync_mem_alloc(&this_tile->row_mt_sync, cm, max_rows);
231
232 this_tile->row_ctx = NULL;
233 if (alloc_row_ctx) {
234 assert(max_cols > 0);
235 const int num_row_ctx = AOMMAX(1, (max_cols - 1));
236 CHECK_MEM_ERROR(cm, this_tile->row_ctx,
237 (FRAME_CONTEXT *)aom_memalign(
238 16, num_row_ctx * sizeof(*this_tile->row_ctx)));
239 }
240 }
241 }
242 enc_row_mt->allocated_tile_cols = tile_cols;
243 enc_row_mt->allocated_tile_rows = tile_rows;
244 enc_row_mt->allocated_rows = max_rows;
245 enc_row_mt->allocated_cols = max_cols - 1;
246 }
247
av1_row_mt_mem_dealloc(AV1_COMP * cpi)248 void av1_row_mt_mem_dealloc(AV1_COMP *cpi) {
249 AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
250 const int tile_cols = enc_row_mt->allocated_tile_cols;
251 const int tile_rows = enc_row_mt->allocated_tile_rows;
252 int tile_col, tile_row;
253
254 // Free row based multi-threading sync memory
255 for (tile_row = 0; tile_row < tile_rows; tile_row++) {
256 for (tile_col = 0; tile_col < tile_cols; tile_col++) {
257 int tile_index = tile_row * tile_cols + tile_col;
258 TileDataEnc *const this_tile = &cpi->tile_data[tile_index];
259
260 row_mt_sync_mem_dealloc(&this_tile->row_mt_sync);
261
262 if (cpi->oxcf.algo_cfg.cdf_update_mode) aom_free(this_tile->row_ctx);
263 }
264 }
265 enc_row_mt->allocated_rows = 0;
266 enc_row_mt->allocated_cols = 0;
267 enc_row_mt->allocated_tile_cols = 0;
268 enc_row_mt->allocated_tile_rows = 0;
269 }
270
assign_tile_to_thread(int * thread_id_to_tile_id,int num_tiles,int num_workers)271 static AOM_INLINE void assign_tile_to_thread(int *thread_id_to_tile_id,
272 int num_tiles, int num_workers) {
273 int tile_id = 0;
274 int i;
275
276 for (i = 0; i < num_workers; i++) {
277 thread_id_to_tile_id[i] = tile_id++;
278 if (tile_id == num_tiles) tile_id = 0;
279 }
280 }
281
get_next_job(TileDataEnc * const tile_data,int * current_mi_row,int mib_size)282 static AOM_INLINE int get_next_job(TileDataEnc *const tile_data,
283 int *current_mi_row, int mib_size) {
284 AV1EncRowMultiThreadSync *const row_mt_sync = &tile_data->row_mt_sync;
285 const int mi_row_end = tile_data->tile_info.mi_row_end;
286
287 if (row_mt_sync->next_mi_row < mi_row_end) {
288 *current_mi_row = row_mt_sync->next_mi_row;
289 row_mt_sync->num_threads_working++;
290 row_mt_sync->next_mi_row += mib_size;
291 return 1;
292 }
293 return 0;
294 }
295
switch_tile_and_get_next_job(AV1_COMMON * const cm,TileDataEnc * const tile_data,int * cur_tile_id,int * current_mi_row,int * end_of_frame,int is_firstpass,const BLOCK_SIZE fp_block_size)296 static AOM_INLINE void switch_tile_and_get_next_job(
297 AV1_COMMON *const cm, TileDataEnc *const tile_data, int *cur_tile_id,
298 int *current_mi_row, int *end_of_frame, int is_firstpass,
299 const BLOCK_SIZE fp_block_size) {
300 const int tile_cols = cm->tiles.cols;
301 const int tile_rows = cm->tiles.rows;
302
303 int tile_id = -1; // Stores the tile ID with minimum proc done
304 int max_mis_to_encode = 0;
305 int min_num_threads_working = INT_MAX;
306
307 for (int tile_row = 0; tile_row < tile_rows; tile_row++) {
308 for (int tile_col = 0; tile_col < tile_cols; tile_col++) {
309 int tile_index = tile_row * tile_cols + tile_col;
310 TileDataEnc *const this_tile = &tile_data[tile_index];
311 AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
312
313 #if CONFIG_REALTIME_ONLY
314 int num_b_rows_in_tile =
315 av1_get_sb_rows_in_tile(cm, this_tile->tile_info);
316 int num_b_cols_in_tile =
317 av1_get_sb_cols_in_tile(cm, this_tile->tile_info);
318 #else
319 int num_b_rows_in_tile =
320 is_firstpass
321 ? av1_get_unit_rows_in_tile(this_tile->tile_info, fp_block_size)
322 : av1_get_sb_rows_in_tile(cm, this_tile->tile_info);
323 int num_b_cols_in_tile =
324 is_firstpass
325 ? av1_get_unit_cols_in_tile(this_tile->tile_info, fp_block_size)
326 : av1_get_sb_cols_in_tile(cm, this_tile->tile_info);
327 #endif
328 int theoretical_limit_on_threads =
329 AOMMIN((num_b_cols_in_tile + 1) >> 1, num_b_rows_in_tile);
330 int num_threads_working = row_mt_sync->num_threads_working;
331
332 if (num_threads_working < theoretical_limit_on_threads) {
333 int num_mis_to_encode =
334 this_tile->tile_info.mi_row_end - row_mt_sync->next_mi_row;
335
336 // Tile to be processed by this thread is selected on the basis of
337 // availability of jobs:
338 // 1) If jobs are available, tile to be processed is chosen on the
339 // basis of minimum number of threads working for that tile. If two or
340 // more tiles have same number of threads working for them, then the
341 // tile with maximum number of jobs available will be chosen.
342 // 2) If no jobs are available, then end_of_frame is reached.
343 if (num_mis_to_encode > 0) {
344 if (num_threads_working < min_num_threads_working) {
345 min_num_threads_working = num_threads_working;
346 max_mis_to_encode = 0;
347 }
348 if (num_threads_working == min_num_threads_working &&
349 num_mis_to_encode > max_mis_to_encode) {
350 tile_id = tile_index;
351 max_mis_to_encode = num_mis_to_encode;
352 }
353 }
354 }
355 }
356 }
357 if (tile_id == -1) {
358 *end_of_frame = 1;
359 } else {
360 // Update the current tile id to the tile id that will be processed next,
361 // which will be the least processed tile.
362 *cur_tile_id = tile_id;
363 const int unit_height = mi_size_high[fp_block_size];
364 get_next_job(&tile_data[tile_id], current_mi_row,
365 is_firstpass ? unit_height : cm->seq_params.mib_size);
366 }
367 }
368
369 #if !CONFIG_REALTIME_ONLY
fp_enc_row_mt_worker_hook(void * arg1,void * unused)370 static int fp_enc_row_mt_worker_hook(void *arg1, void *unused) {
371 EncWorkerData *const thread_data = (EncWorkerData *)arg1;
372 AV1_COMP *const cpi = thread_data->cpi;
373 AV1_COMMON *const cm = &cpi->common;
374 int thread_id = thread_data->thread_id;
375 AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
376 int cur_tile_id = enc_row_mt->thread_id_to_tile_id[thread_id];
377 #if CONFIG_MULTITHREAD
378 pthread_mutex_t *enc_row_mt_mutex_ = enc_row_mt->mutex_;
379 #endif
380 (void)unused;
381
382 assert(cur_tile_id != -1);
383
384 const BLOCK_SIZE fp_block_size = cpi->fp_block_size;
385 const int unit_height = mi_size_high[fp_block_size];
386 int end_of_frame = 0;
387 while (1) {
388 int current_mi_row = -1;
389 #if CONFIG_MULTITHREAD
390 pthread_mutex_lock(enc_row_mt_mutex_);
391 #endif
392 if (!get_next_job(&cpi->tile_data[cur_tile_id], ¤t_mi_row,
393 unit_height)) {
394 // No jobs are available for the current tile. Query for the status of
395 // other tiles and get the next job if available
396 switch_tile_and_get_next_job(cm, cpi->tile_data, &cur_tile_id,
397 ¤t_mi_row, &end_of_frame, 1,
398 fp_block_size);
399 }
400 #if CONFIG_MULTITHREAD
401 pthread_mutex_unlock(enc_row_mt_mutex_);
402 #endif
403 if (end_of_frame == 1) break;
404
405 TileDataEnc *const this_tile = &cpi->tile_data[cur_tile_id];
406 AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
407 ThreadData *td = thread_data->td;
408
409 assert(current_mi_row != -1 &&
410 current_mi_row <= this_tile->tile_info.mi_row_end);
411
412 const int unit_height_log2 = mi_size_high_log2[fp_block_size];
413 av1_first_pass_row(cpi, td, this_tile, current_mi_row >> unit_height_log2,
414 fp_block_size);
415 #if CONFIG_MULTITHREAD
416 pthread_mutex_lock(enc_row_mt_mutex_);
417 #endif
418 row_mt_sync->num_threads_working--;
419 #if CONFIG_MULTITHREAD
420 pthread_mutex_unlock(enc_row_mt_mutex_);
421 #endif
422 }
423
424 return 1;
425 }
426 #endif
427
enc_row_mt_worker_hook(void * arg1,void * unused)428 static int enc_row_mt_worker_hook(void *arg1, void *unused) {
429 EncWorkerData *const thread_data = (EncWorkerData *)arg1;
430 AV1_COMP *const cpi = thread_data->cpi;
431 AV1_COMMON *const cm = &cpi->common;
432 int thread_id = thread_data->thread_id;
433 AV1EncRowMultiThreadInfo *const enc_row_mt = &cpi->mt_info.enc_row_mt;
434 int cur_tile_id = enc_row_mt->thread_id_to_tile_id[thread_id];
435 #if CONFIG_MULTITHREAD
436 pthread_mutex_t *enc_row_mt_mutex_ = enc_row_mt->mutex_;
437 #endif
438 (void)unused;
439
440 assert(cur_tile_id != -1);
441
442 const BLOCK_SIZE fp_block_size = cpi->fp_block_size;
443 int end_of_frame = 0;
444 while (1) {
445 int current_mi_row = -1;
446 #if CONFIG_MULTITHREAD
447 pthread_mutex_lock(enc_row_mt_mutex_);
448 #endif
449 if (!get_next_job(&cpi->tile_data[cur_tile_id], ¤t_mi_row,
450 cm->seq_params.mib_size)) {
451 // No jobs are available for the current tile. Query for the status of
452 // other tiles and get the next job if available
453 switch_tile_and_get_next_job(cm, cpi->tile_data, &cur_tile_id,
454 ¤t_mi_row, &end_of_frame, 0,
455 fp_block_size);
456 }
457 #if CONFIG_MULTITHREAD
458 pthread_mutex_unlock(enc_row_mt_mutex_);
459 #endif
460 if (end_of_frame == 1) break;
461
462 TileDataEnc *const this_tile = &cpi->tile_data[cur_tile_id];
463 AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
464 const TileInfo *const tile_info = &this_tile->tile_info;
465 const int tile_row = tile_info->tile_row;
466 const int tile_col = tile_info->tile_col;
467 ThreadData *td = thread_data->td;
468
469 assert(current_mi_row != -1 && current_mi_row <= tile_info->mi_row_end);
470
471 td->mb.e_mbd.tile_ctx = td->tctx;
472 td->mb.tile_pb_ctx = &this_tile->tctx;
473
474 if (this_tile->allow_update_cdf) {
475 td->mb.row_ctx = this_tile->row_ctx;
476 if (current_mi_row == tile_info->mi_row_start)
477 memcpy(td->mb.e_mbd.tile_ctx, &this_tile->tctx, sizeof(FRAME_CONTEXT));
478 } else {
479 memcpy(td->mb.e_mbd.tile_ctx, &this_tile->tctx, sizeof(FRAME_CONTEXT));
480 }
481
482 av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), tile_row,
483 &td->mb.e_mbd);
484
485 cfl_init(&td->mb.e_mbd.cfl, &cm->seq_params);
486 if (td->mb.txfm_search_info.txb_rd_records != NULL) {
487 av1_crc32c_calculator_init(
488 &td->mb.txfm_search_info.txb_rd_records->mb_rd_record.crc_calculator);
489 }
490
491 av1_encode_sb_row(cpi, td, tile_row, tile_col, current_mi_row);
492 #if CONFIG_MULTITHREAD
493 pthread_mutex_lock(enc_row_mt_mutex_);
494 #endif
495 row_mt_sync->num_threads_working--;
496 #if CONFIG_MULTITHREAD
497 pthread_mutex_unlock(enc_row_mt_mutex_);
498 #endif
499 }
500
501 return 1;
502 }
503
enc_worker_hook(void * arg1,void * unused)504 static int enc_worker_hook(void *arg1, void *unused) {
505 EncWorkerData *const thread_data = (EncWorkerData *)arg1;
506 AV1_COMP *const cpi = thread_data->cpi;
507 const AV1_COMMON *const cm = &cpi->common;
508 const int tile_cols = cm->tiles.cols;
509 const int tile_rows = cm->tiles.rows;
510 int t;
511
512 (void)unused;
513
514 for (t = thread_data->start; t < tile_rows * tile_cols;
515 t += cpi->mt_info.num_workers) {
516 int tile_row = t / tile_cols;
517 int tile_col = t % tile_cols;
518
519 TileDataEnc *const this_tile =
520 &cpi->tile_data[tile_row * cm->tiles.cols + tile_col];
521 thread_data->td->mb.e_mbd.tile_ctx = &this_tile->tctx;
522 thread_data->td->mb.tile_pb_ctx = &this_tile->tctx;
523 av1_encode_tile(cpi, thread_data->td, tile_row, tile_col);
524 }
525
526 return 1;
527 }
528
av1_create_second_pass_workers(AV1_COMP * cpi,int num_workers)529 void av1_create_second_pass_workers(AV1_COMP *cpi, int num_workers) {
530 AV1_COMMON *const cm = &cpi->common;
531 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
532 MultiThreadInfo *const mt_info = &cpi->mt_info;
533
534 assert(mt_info->workers != NULL);
535 assert(mt_info->tile_thr_data != NULL);
536
537 #if CONFIG_MULTITHREAD
538 if (cpi->oxcf.row_mt == 1) {
539 AV1EncRowMultiThreadInfo *enc_row_mt = &mt_info->enc_row_mt;
540 if (enc_row_mt->mutex_ == NULL) {
541 CHECK_MEM_ERROR(cm, enc_row_mt->mutex_,
542 aom_malloc(sizeof(*(enc_row_mt->mutex_))));
543 if (enc_row_mt->mutex_) pthread_mutex_init(enc_row_mt->mutex_, NULL);
544 }
545 }
546 AV1GlobalMotionSync *gm_sync = &mt_info->gm_sync;
547 if (gm_sync->mutex_ == NULL) {
548 CHECK_MEM_ERROR(cm, gm_sync->mutex_,
549 aom_malloc(sizeof(*(gm_sync->mutex_))));
550 if (gm_sync->mutex_) pthread_mutex_init(gm_sync->mutex_, NULL);
551 }
552 AV1TemporalFilterSync *tf_sync = &mt_info->tf_sync;
553 if (tf_sync->mutex_ == NULL) {
554 CHECK_MEM_ERROR(cm, tf_sync->mutex_, aom_malloc(sizeof(*tf_sync->mutex_)));
555 if (tf_sync->mutex_) pthread_mutex_init(tf_sync->mutex_, NULL);
556 }
557 AV1CdefSync *cdef_sync = &mt_info->cdef_sync;
558 if (cdef_sync->mutex_ == NULL) {
559 CHECK_MEM_ERROR(cm, cdef_sync->mutex_,
560 aom_malloc(sizeof(*(cdef_sync->mutex_))));
561 if (cdef_sync->mutex_) pthread_mutex_init(cdef_sync->mutex_, NULL);
562 }
563 #endif
564
565 for (int i = num_workers - 1; i >= 0; i--) {
566 AVxWorker *const worker = &mt_info->workers[i];
567 EncWorkerData *const thread_data = &mt_info->tile_thr_data[i];
568
569 thread_data->cpi = cpi;
570 thread_data->thread_id = i;
571 // Set the starting tile for each thread.
572 thread_data->start = i;
573
574 if (i > 0) {
575 // alloc_obmc_buffers(&thread_data->td->obmc_buffer, cm);
576
577 // Create threads
578 if (!winterface->reset(worker))
579 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
580 "Tile encoder thread creation failed");
581 } else {
582 // Main thread acts as a worker and uses the thread data in cpi.
583 thread_data->td = &cpi->td;
584 }
585 winterface->sync(worker);
586 }
587 }
588
create_enc_workers(AV1_COMP * cpi,int num_workers)589 static AOM_INLINE void create_enc_workers(AV1_COMP *cpi, int num_workers) {
590 AV1_COMMON *const cm = &cpi->common;
591 MultiThreadInfo *const mt_info = &cpi->mt_info;
592
593 assert(mt_info->workers != NULL);
594 assert(mt_info->tile_thr_data != NULL);
595
596 for (int i = num_workers - 1; i >= 0; i--) {
597 EncWorkerData *const thread_data = &mt_info->tile_thr_data[i];
598
599 if (i > 0) {
600 // Set up sms_tree.
601 av1_setup_sms_tree(cpi, thread_data->td);
602
603 alloc_obmc_buffers(&thread_data->td->obmc_buffer, cm);
604
605 CHECK_MEM_ERROR(cm, thread_data->td->inter_modes_info,
606 (InterModesInfo *)aom_malloc(
607 sizeof(*thread_data->td->inter_modes_info)));
608
609 for (int x = 0; x < 2; x++)
610 for (int y = 0; y < 2; y++)
611 CHECK_MEM_ERROR(
612 cm, thread_data->td->hash_value_buffer[x][y],
613 (uint32_t *)aom_malloc(
614 AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
615 sizeof(*thread_data->td->hash_value_buffer[0][0])));
616
617 // Allocate frame counters in thread data.
618 CHECK_MEM_ERROR(cm, thread_data->td->counts,
619 aom_calloc(1, sizeof(*thread_data->td->counts)));
620
621 // Allocate buffers used by palette coding mode.
622 CHECK_MEM_ERROR(
623 cm, thread_data->td->palette_buffer,
624 aom_memalign(16, sizeof(*thread_data->td->palette_buffer)));
625
626 alloc_compound_type_rd_buffers(cm, &thread_data->td->comp_rd_buffer);
627
628 CHECK_MEM_ERROR(
629 cm, thread_data->td->tmp_conv_dst,
630 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE *
631 sizeof(*thread_data->td->tmp_conv_dst)));
632 for (int j = 0; j < 2; ++j) {
633 CHECK_MEM_ERROR(
634 cm, thread_data->td->tmp_pred_bufs[j],
635 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
636 sizeof(*thread_data->td->tmp_pred_bufs[j])));
637 }
638
639 if (cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION) {
640 const int num_64x64_blocks =
641 (cm->seq_params.sb_size == BLOCK_64X64) ? 1 : 4;
642 CHECK_MEM_ERROR(
643 cm, thread_data->td->vt64x64,
644 aom_malloc(sizeof(*thread_data->td->vt64x64) * num_64x64_blocks));
645 }
646 } else {
647 thread_data->td = &cpi->td;
648 }
649 if (cpi->oxcf.row_mt == 1)
650 CHECK_MEM_ERROR(
651 cm, thread_data->td->tctx,
652 (FRAME_CONTEXT *)aom_memalign(16, sizeof(*thread_data->td->tctx)));
653 }
654 mt_info->enc_mt_buf_init_done = 1;
655 }
656
av1_create_workers(AV1_COMP * cpi,int num_workers)657 void av1_create_workers(AV1_COMP *cpi, int num_workers) {
658 AV1_COMMON *const cm = &cpi->common;
659 MultiThreadInfo *const mt_info = &cpi->mt_info;
660 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
661
662 CHECK_MEM_ERROR(cm, mt_info->workers,
663 aom_malloc(num_workers * sizeof(*mt_info->workers)));
664
665 CHECK_MEM_ERROR(cm, mt_info->tile_thr_data,
666 aom_calloc(num_workers, sizeof(*mt_info->tile_thr_data)));
667
668 for (int i = num_workers - 1; i >= 0; i--) {
669 AVxWorker *const worker = &mt_info->workers[i];
670 EncWorkerData *const thread_data = &mt_info->tile_thr_data[i];
671
672 winterface->init(worker);
673 worker->thread_name = "aom enc worker";
674
675 if (i > 0) {
676 // Allocate thread data.
677 CHECK_MEM_ERROR(cm, thread_data->td,
678 aom_memalign(32, sizeof(*thread_data->td)));
679 av1_zero(*thread_data->td);
680
681 // Set up shared coeff buffers.
682 av1_setup_shared_coeff_buffer(cm, &thread_data->td->shared_coeff_buf);
683 }
684 ++mt_info->num_workers;
685 }
686 }
687
688 #if !CONFIG_REALTIME_ONLY
fp_create_enc_workers(AV1_COMP * cpi,int num_workers)689 static AOM_INLINE void fp_create_enc_workers(AV1_COMP *cpi, int num_workers) {
690 AV1_COMMON *const cm = &cpi->common;
691 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
692 MultiThreadInfo *const mt_info = &cpi->mt_info;
693 // For single-pass encode, threads are already created during call to
694 // av1_create_second_pass_workers(). Create threads only in the case of
695 // pass = 1.
696 const int create_workers = (mt_info->num_mod_workers[MOD_FP] == 0) ? 1 : 0;
697
698 assert(mt_info->workers != NULL);
699 assert(mt_info->tile_thr_data != NULL);
700
701 #if CONFIG_MULTITHREAD
702 AV1EncRowMultiThreadInfo *enc_row_mt = &mt_info->enc_row_mt;
703 if (enc_row_mt->mutex_ == NULL) {
704 CHECK_MEM_ERROR(cm, enc_row_mt->mutex_,
705 aom_malloc(sizeof(*(enc_row_mt->mutex_))));
706 if (enc_row_mt->mutex_) pthread_mutex_init(enc_row_mt->mutex_, NULL);
707 }
708 #endif
709
710 for (int i = num_workers - 1; i >= 0; i--) {
711 AVxWorker *const worker = &mt_info->workers[i];
712 EncWorkerData *const thread_data = &mt_info->tile_thr_data[i];
713
714 thread_data->cpi = cpi;
715 thread_data->thread_id = i;
716 // Set the starting tile for each thread.
717 thread_data->start = i;
718
719 if (i > 0) {
720 // Set up firstpass PICK_MODE_CONTEXT.
721 thread_data->td->firstpass_ctx =
722 av1_alloc_pmc(cpi, BLOCK_16X16, &thread_data->td->shared_coeff_buf);
723
724 if (create_workers) {
725 // Create threads
726 if (!winterface->reset(worker))
727 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
728 "Tile encoder thread creation failed");
729 }
730 } else {
731 // Main thread acts as a worker and uses the thread data in cpi.
732 thread_data->td = &cpi->td;
733 }
734 if (create_workers) {
735 winterface->sync(worker);
736 ++mt_info->num_mod_workers[MOD_FP];
737 }
738 }
739 mt_info->fp_mt_buf_init_done = 1;
740 }
741 #endif
742
launch_workers(MultiThreadInfo * const mt_info,int num_workers)743 static AOM_INLINE void launch_workers(MultiThreadInfo *const mt_info,
744 int num_workers) {
745 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
746 for (int i = num_workers - 1; i >= 0; i--) {
747 AVxWorker *const worker = &mt_info->workers[i];
748 if (i == 0)
749 winterface->execute(worker);
750 else
751 winterface->launch(worker);
752 }
753 }
754
sync_enc_workers(MultiThreadInfo * const mt_info,AV1_COMMON * const cm,int num_workers)755 static AOM_INLINE void sync_enc_workers(MultiThreadInfo *const mt_info,
756 AV1_COMMON *const cm, int num_workers) {
757 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
758 int had_error = 0;
759
760 // Encoding ends.
761 for (int i = num_workers - 1; i >= 0; i--) {
762 AVxWorker *const worker = &mt_info->workers[i];
763 had_error |= !winterface->sync(worker);
764 }
765
766 if (had_error)
767 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
768 "Failed to encode tile data");
769 }
770
accumulate_counters_enc_workers(AV1_COMP * cpi,int num_workers)771 static AOM_INLINE void accumulate_counters_enc_workers(AV1_COMP *cpi,
772 int num_workers) {
773 for (int i = num_workers - 1; i >= 0; i--) {
774 AVxWorker *const worker = &cpi->mt_info.workers[i];
775 EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
776 cpi->intrabc_used |= thread_data->td->intrabc_used;
777 cpi->deltaq_used |= thread_data->td->deltaq_used;
778 if (thread_data->td->mb.txfm_search_info.txb_rd_records) {
779 aom_free(thread_data->td->mb.txfm_search_info.txb_rd_records);
780 thread_data->td->mb.txfm_search_info.txb_rd_records = NULL;
781 }
782 if (thread_data->td != &cpi->td &&
783 cpi->oxcf.cost_upd_freq.mv < COST_UPD_OFF) {
784 aom_free(thread_data->td->mb.mv_costs);
785 }
786
787 // Accumulate counters.
788 if (i > 0) {
789 av1_accumulate_frame_counts(&cpi->counts, thread_data->td->counts);
790 accumulate_rd_opt(&cpi->td, thread_data->td);
791 cpi->td.mb.txfm_search_info.txb_split_count +=
792 thread_data->td->mb.txfm_search_info.txb_split_count;
793 #if CONFIG_SPEED_STATS
794 cpi->td.mb.txfm_search_info.tx_search_count +=
795 thread_data->td->mb.txfm_search_info.tx_search_count;
796 #endif // CONFIG_SPEED_STATS
797 }
798 }
799 }
800
prepare_enc_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)801 static AOM_INLINE void prepare_enc_workers(AV1_COMP *cpi, AVxWorkerHook hook,
802 int num_workers) {
803 MultiThreadInfo *const mt_info = &cpi->mt_info;
804 AV1_COMMON *const cm = &cpi->common;
805 for (int i = num_workers - 1; i >= 0; i--) {
806 AVxWorker *const worker = &mt_info->workers[i];
807 EncWorkerData *const thread_data = &mt_info->tile_thr_data[i];
808
809 worker->hook = hook;
810 worker->data1 = thread_data;
811 worker->data2 = NULL;
812
813 thread_data->cpi = cpi;
814 if (i == 0) {
815 thread_data->td = &cpi->td;
816 }
817
818 thread_data->td->intrabc_used = 0;
819 thread_data->td->deltaq_used = 0;
820
821 // Before encoding a frame, copy the thread data from cpi.
822 if (thread_data->td != &cpi->td) {
823 thread_data->td->mb = cpi->td.mb;
824 thread_data->td->rd_counts = cpi->td.rd_counts;
825 thread_data->td->mb.obmc_buffer = thread_data->td->obmc_buffer;
826
827 thread_data->td->mb.inter_modes_info = thread_data->td->inter_modes_info;
828 for (int x = 0; x < 2; x++) {
829 for (int y = 0; y < 2; y++) {
830 memcpy(thread_data->td->hash_value_buffer[x][y],
831 cpi->td.mb.intrabc_hash_info.hash_value_buffer[x][y],
832 AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
833 sizeof(*thread_data->td->hash_value_buffer[0][0]));
834 thread_data->td->mb.intrabc_hash_info.hash_value_buffer[x][y] =
835 thread_data->td->hash_value_buffer[x][y];
836 }
837 }
838 if (cpi->oxcf.cost_upd_freq.mv < COST_UPD_OFF) {
839 CHECK_MEM_ERROR(cm, thread_data->td->mb.mv_costs,
840 (MvCosts *)aom_malloc(sizeof(MvCosts)));
841 memcpy(thread_data->td->mb.mv_costs, cpi->td.mb.mv_costs,
842 sizeof(MvCosts));
843 }
844 }
845 if (!cpi->sf.rt_sf.use_nonrd_pick_mode) {
846 CHECK_MEM_ERROR(cm, thread_data->td->mb.txfm_search_info.txb_rd_records,
847 (TxbRdRecords *)aom_malloc(sizeof(TxbRdRecords)));
848 }
849
850 if (thread_data->td->counts != &cpi->counts) {
851 memcpy(thread_data->td->counts, &cpi->counts, sizeof(cpi->counts));
852 }
853
854 if (i > 0) {
855 thread_data->td->mb.palette_buffer = thread_data->td->palette_buffer;
856 thread_data->td->mb.comp_rd_buffer = thread_data->td->comp_rd_buffer;
857 thread_data->td->mb.tmp_conv_dst = thread_data->td->tmp_conv_dst;
858 for (int j = 0; j < 2; ++j) {
859 thread_data->td->mb.tmp_pred_bufs[j] =
860 thread_data->td->tmp_pred_bufs[j];
861 }
862
863 thread_data->td->mb.e_mbd.tmp_conv_dst = thread_data->td->mb.tmp_conv_dst;
864 for (int j = 0; j < 2; ++j) {
865 thread_data->td->mb.e_mbd.tmp_obmc_bufs[j] =
866 thread_data->td->mb.tmp_pred_bufs[j];
867 }
868 }
869 }
870 }
871
872 #if !CONFIG_REALTIME_ONLY
fp_prepare_enc_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)873 static AOM_INLINE void fp_prepare_enc_workers(AV1_COMP *cpi, AVxWorkerHook hook,
874 int num_workers) {
875 AV1_COMMON *const cm = &cpi->common;
876 MultiThreadInfo *const mt_info = &cpi->mt_info;
877 for (int i = num_workers - 1; i >= 0; i--) {
878 AVxWorker *const worker = &mt_info->workers[i];
879 EncWorkerData *const thread_data = &mt_info->tile_thr_data[i];
880
881 worker->hook = hook;
882 worker->data1 = thread_data;
883 worker->data2 = NULL;
884
885 thread_data->cpi = cpi;
886 if (i == 0) {
887 thread_data->td = &cpi->td;
888 }
889
890 // Before encoding a frame, copy the thread data from cpi.
891 if (thread_data->td != &cpi->td) {
892 thread_data->td->mb = cpi->td.mb;
893 if (cpi->oxcf.cost_upd_freq.mv < COST_UPD_OFF) {
894 CHECK_MEM_ERROR(cm, thread_data->td->mb.mv_costs,
895 (MvCosts *)aom_malloc(sizeof(MvCosts)));
896 memcpy(thread_data->td->mb.mv_costs, cpi->td.mb.mv_costs,
897 sizeof(MvCosts));
898 }
899 }
900 if (!cpi->sf.rt_sf.use_nonrd_pick_mode) {
901 CHECK_MEM_ERROR(cm, thread_data->td->mb.txfm_search_info.txb_rd_records,
902 (TxbRdRecords *)aom_malloc(sizeof(TxbRdRecords)));
903 }
904 }
905 }
906 #endif
907
908 // Computes the number of workers for row multi-threading of encoding stage
compute_num_enc_row_mt_workers(AV1_COMMON * const cm,int max_threads)909 static AOM_INLINE int compute_num_enc_row_mt_workers(AV1_COMMON *const cm,
910 int max_threads) {
911 TileInfo tile_info;
912 const int tile_cols = cm->tiles.cols;
913 const int tile_rows = cm->tiles.rows;
914 int total_num_threads_row_mt = 0;
915 for (int row = 0; row < tile_rows; row++) {
916 for (int col = 0; col < tile_cols; col++) {
917 av1_tile_init(&tile_info, cm, row, col);
918 const int num_sb_rows_in_tile = av1_get_sb_rows_in_tile(cm, tile_info);
919 const int num_sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile_info);
920 total_num_threads_row_mt +=
921 AOMMIN((num_sb_cols_in_tile + 1) >> 1, num_sb_rows_in_tile);
922 }
923 }
924 return AOMMIN(max_threads, total_num_threads_row_mt);
925 }
926
927 // Computes the number of workers for tile multi-threading of encoding stage
compute_num_enc_tile_mt_workers(AV1_COMMON * const cm,int max_threads)928 static AOM_INLINE int compute_num_enc_tile_mt_workers(AV1_COMMON *const cm,
929 int max_threads) {
930 const int tile_cols = cm->tiles.cols;
931 const int tile_rows = cm->tiles.rows;
932 return AOMMIN(max_threads, tile_cols * tile_rows);
933 }
934
935 // Find max worker of all MT stages
av1_get_max_num_workers(AV1_COMP * cpi)936 int av1_get_max_num_workers(AV1_COMP *cpi) {
937 int max_num_workers = 0;
938 for (int i = MOD_FP; i < NUM_MT_MODULES; i++)
939 max_num_workers = AOMMAX(cpi->mt_info.num_mod_workers[i], max_num_workers);
940 assert(max_num_workers >= 1);
941 return AOMMIN(max_num_workers, cpi->oxcf.max_threads);
942 }
943
944 // Computes the number of workers for encoding stage (row/tile multi-threading)
compute_num_enc_workers(AV1_COMP * cpi,int max_workers)945 static AOM_INLINE int compute_num_enc_workers(AV1_COMP *cpi, int max_workers) {
946 if (max_workers <= 1) return 1;
947 if (cpi->oxcf.row_mt)
948 return compute_num_enc_row_mt_workers(&cpi->common, max_workers);
949 else
950 return compute_num_enc_tile_mt_workers(&cpi->common, max_workers);
951 }
952
av1_encode_tiles_mt(AV1_COMP * cpi)953 void av1_encode_tiles_mt(AV1_COMP *cpi) {
954 AV1_COMMON *const cm = &cpi->common;
955 MultiThreadInfo *const mt_info = &cpi->mt_info;
956 const int tile_cols = cm->tiles.cols;
957 const int tile_rows = cm->tiles.rows;
958 int num_workers = mt_info->num_mod_workers[MOD_ENC];
959
960 assert(IMPLIES(cpi->tile_data == NULL,
961 cpi->allocated_tiles < tile_cols * tile_rows));
962 if (cpi->allocated_tiles < tile_cols * tile_rows) av1_alloc_tile_data(cpi);
963
964 av1_init_tile_data(cpi);
965 // Only run once to create threads and allocate thread data.
966 if (mt_info->enc_mt_buf_init_done == 0) {
967 create_enc_workers(cpi, num_workers);
968 } else {
969 num_workers = AOMMIN(num_workers, mt_info->num_workers);
970 }
971 prepare_enc_workers(cpi, enc_worker_hook, num_workers);
972 launch_workers(&cpi->mt_info, num_workers);
973 sync_enc_workers(&cpi->mt_info, cm, num_workers);
974 accumulate_counters_enc_workers(cpi, num_workers);
975 }
976
977 // Accumulate frame counts. FRAME_COUNTS consist solely of 'unsigned int'
978 // members, so we treat it as an array, and sum over the whole length.
av1_accumulate_frame_counts(FRAME_COUNTS * acc_counts,const FRAME_COUNTS * counts)979 void av1_accumulate_frame_counts(FRAME_COUNTS *acc_counts,
980 const FRAME_COUNTS *counts) {
981 unsigned int *const acc = (unsigned int *)acc_counts;
982 const unsigned int *const cnt = (const unsigned int *)counts;
983
984 const unsigned int n_counts = sizeof(FRAME_COUNTS) / sizeof(unsigned int);
985
986 for (unsigned int i = 0; i < n_counts; i++) acc[i] += cnt[i];
987 }
988
989 // Computes the maximum number of sb_rows for row multi-threading of encoding
990 // stage
compute_max_sb_rows_cols(AV1_COMP * cpi,int * max_sb_rows,int * max_sb_cols)991 static AOM_INLINE void compute_max_sb_rows_cols(AV1_COMP *cpi, int *max_sb_rows,
992 int *max_sb_cols) {
993 AV1_COMMON *const cm = &cpi->common;
994 const int tile_cols = cm->tiles.cols;
995 const int tile_rows = cm->tiles.rows;
996 for (int row = 0; row < tile_rows; row++) {
997 for (int col = 0; col < tile_cols; col++) {
998 const int tile_index = row * cm->tiles.cols + col;
999 TileInfo tile_info = cpi->tile_data[tile_index].tile_info;
1000 const int num_sb_rows_in_tile = av1_get_sb_rows_in_tile(cm, tile_info);
1001 const int num_sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile_info);
1002 *max_sb_rows = AOMMAX(*max_sb_rows, num_sb_rows_in_tile);
1003 *max_sb_cols = AOMMAX(*max_sb_cols, num_sb_cols_in_tile);
1004 }
1005 }
1006 }
1007
1008 #if !CONFIG_REALTIME_ONLY
1009 // Computes the number of workers for firstpass stage (row/tile multi-threading)
av1_fp_compute_num_enc_workers(AV1_COMP * cpi)1010 int av1_fp_compute_num_enc_workers(AV1_COMP *cpi) {
1011 AV1_COMMON *cm = &cpi->common;
1012 const int tile_cols = cm->tiles.cols;
1013 const int tile_rows = cm->tiles.rows;
1014 int total_num_threads_row_mt = 0;
1015 TileInfo tile_info;
1016
1017 if (cpi->oxcf.max_threads <= 1) return 1;
1018
1019 for (int row = 0; row < tile_rows; row++) {
1020 for (int col = 0; col < tile_cols; col++) {
1021 av1_tile_init(&tile_info, cm, row, col);
1022 const int num_mb_rows_in_tile =
1023 av1_get_unit_rows_in_tile(tile_info, cpi->fp_block_size);
1024 const int num_mb_cols_in_tile =
1025 av1_get_unit_cols_in_tile(tile_info, cpi->fp_block_size);
1026 total_num_threads_row_mt +=
1027 AOMMIN((num_mb_cols_in_tile + 1) >> 1, num_mb_rows_in_tile);
1028 }
1029 }
1030 return AOMMIN(cpi->oxcf.max_threads, total_num_threads_row_mt);
1031 }
1032
1033 // Computes the maximum number of mb_rows for row multi-threading of firstpass
1034 // stage
fp_compute_max_mb_rows(const AV1_COMMON * const cm,const TileDataEnc * const tile_data,const BLOCK_SIZE fp_block_size)1035 static AOM_INLINE int fp_compute_max_mb_rows(const AV1_COMMON *const cm,
1036 const TileDataEnc *const tile_data,
1037 const BLOCK_SIZE fp_block_size) {
1038 const int tile_cols = cm->tiles.cols;
1039 const int tile_rows = cm->tiles.rows;
1040 int max_mb_rows = 0;
1041 for (int row = 0; row < tile_rows; row++) {
1042 for (int col = 0; col < tile_cols; col++) {
1043 const int tile_index = row * cm->tiles.cols + col;
1044 TileInfo tile_info = tile_data[tile_index].tile_info;
1045 const int num_mb_rows_in_tile =
1046 av1_get_unit_rows_in_tile(tile_info, fp_block_size);
1047 max_mb_rows = AOMMAX(max_mb_rows, num_mb_rows_in_tile);
1048 }
1049 }
1050 return max_mb_rows;
1051 }
1052 #endif
1053
av1_encode_tiles_row_mt(AV1_COMP * cpi)1054 void av1_encode_tiles_row_mt(AV1_COMP *cpi) {
1055 AV1_COMMON *const cm = &cpi->common;
1056 MultiThreadInfo *const mt_info = &cpi->mt_info;
1057 AV1EncRowMultiThreadInfo *const enc_row_mt = &mt_info->enc_row_mt;
1058 const int tile_cols = cm->tiles.cols;
1059 const int tile_rows = cm->tiles.rows;
1060 int *thread_id_to_tile_id = enc_row_mt->thread_id_to_tile_id;
1061 int max_sb_rows = 0, max_sb_cols = 0;
1062 int num_workers = mt_info->num_mod_workers[MOD_ENC];
1063
1064 assert(IMPLIES(cpi->tile_data == NULL,
1065 cpi->allocated_tiles < tile_cols * tile_rows));
1066 if (cpi->allocated_tiles < tile_cols * tile_rows) {
1067 av1_row_mt_mem_dealloc(cpi);
1068 av1_alloc_tile_data(cpi);
1069 }
1070
1071 av1_init_tile_data(cpi);
1072
1073 compute_max_sb_rows_cols(cpi, &max_sb_rows, &max_sb_cols);
1074
1075 if (enc_row_mt->allocated_tile_cols != tile_cols ||
1076 enc_row_mt->allocated_tile_rows != tile_rows ||
1077 enc_row_mt->allocated_rows != max_sb_rows ||
1078 enc_row_mt->allocated_cols != (max_sb_cols - 1)) {
1079 av1_row_mt_mem_dealloc(cpi);
1080 row_mt_mem_alloc(cpi, max_sb_rows, max_sb_cols,
1081 cpi->oxcf.algo_cfg.cdf_update_mode);
1082 }
1083
1084 memset(thread_id_to_tile_id, -1,
1085 sizeof(*thread_id_to_tile_id) * MAX_NUM_THREADS);
1086
1087 for (int tile_row = 0; tile_row < tile_rows; tile_row++) {
1088 for (int tile_col = 0; tile_col < tile_cols; tile_col++) {
1089 int tile_index = tile_row * tile_cols + tile_col;
1090 TileDataEnc *const this_tile = &cpi->tile_data[tile_index];
1091 AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
1092
1093 // Initialize num_finished_cols to -1 for all rows.
1094 memset(row_mt_sync->num_finished_cols, -1,
1095 sizeof(*row_mt_sync->num_finished_cols) * max_sb_rows);
1096 row_mt_sync->next_mi_row = this_tile->tile_info.mi_row_start;
1097 row_mt_sync->num_threads_working = 0;
1098
1099 av1_inter_mode_data_init(this_tile);
1100 av1_zero_above_context(cm, &cpi->td.mb.e_mbd,
1101 this_tile->tile_info.mi_col_start,
1102 this_tile->tile_info.mi_col_end, tile_row);
1103 }
1104 }
1105
1106 // Only run once to create threads and allocate thread data.
1107 if (mt_info->enc_mt_buf_init_done == 0) {
1108 create_enc_workers(cpi, num_workers);
1109 } else {
1110 num_workers = AOMMIN(num_workers, mt_info->num_workers);
1111 }
1112 assign_tile_to_thread(thread_id_to_tile_id, tile_cols * tile_rows,
1113 num_workers);
1114 prepare_enc_workers(cpi, enc_row_mt_worker_hook, num_workers);
1115 launch_workers(&cpi->mt_info, num_workers);
1116 sync_enc_workers(&cpi->mt_info, cm, num_workers);
1117 if (cm->delta_q_info.delta_lf_present_flag) update_delta_lf_for_row_mt(cpi);
1118 accumulate_counters_enc_workers(cpi, num_workers);
1119 }
1120
1121 #if !CONFIG_REALTIME_ONLY
av1_fp_encode_tiles_row_mt(AV1_COMP * cpi)1122 void av1_fp_encode_tiles_row_mt(AV1_COMP *cpi) {
1123 AV1_COMMON *const cm = &cpi->common;
1124 MultiThreadInfo *const mt_info = &cpi->mt_info;
1125 AV1EncRowMultiThreadInfo *const enc_row_mt = &mt_info->enc_row_mt;
1126 const int tile_cols = cm->tiles.cols;
1127 const int tile_rows = cm->tiles.rows;
1128 int *thread_id_to_tile_id = enc_row_mt->thread_id_to_tile_id;
1129 int num_workers = 0;
1130 int max_mb_rows = 0;
1131
1132 assert(IMPLIES(cpi->tile_data == NULL,
1133 cpi->allocated_tiles < tile_cols * tile_rows));
1134 if (cpi->allocated_tiles < tile_cols * tile_rows) {
1135 av1_row_mt_mem_dealloc(cpi);
1136 av1_alloc_tile_data(cpi);
1137 }
1138
1139 av1_init_tile_data(cpi);
1140
1141 const BLOCK_SIZE fp_block_size = cpi->fp_block_size;
1142 max_mb_rows = fp_compute_max_mb_rows(cm, cpi->tile_data, fp_block_size);
1143
1144 // For pass = 1, compute the no. of workers needed. For single-pass encode
1145 // (pass = 0), no. of workers are already computed.
1146 if (mt_info->num_mod_workers[MOD_FP] == 0)
1147 num_workers = av1_fp_compute_num_enc_workers(cpi);
1148 else
1149 num_workers = mt_info->num_mod_workers[MOD_FP];
1150
1151 if (enc_row_mt->allocated_tile_cols != tile_cols ||
1152 enc_row_mt->allocated_tile_rows != tile_rows ||
1153 enc_row_mt->allocated_rows != max_mb_rows) {
1154 av1_row_mt_mem_dealloc(cpi);
1155 row_mt_mem_alloc(cpi, max_mb_rows, -1, 0);
1156 }
1157
1158 memset(thread_id_to_tile_id, -1,
1159 sizeof(*thread_id_to_tile_id) * MAX_NUM_THREADS);
1160
1161 for (int tile_row = 0; tile_row < tile_rows; tile_row++) {
1162 for (int tile_col = 0; tile_col < tile_cols; tile_col++) {
1163 int tile_index = tile_row * tile_cols + tile_col;
1164 TileDataEnc *const this_tile = &cpi->tile_data[tile_index];
1165 AV1EncRowMultiThreadSync *const row_mt_sync = &this_tile->row_mt_sync;
1166
1167 // Initialize num_finished_cols to -1 for all rows.
1168 memset(row_mt_sync->num_finished_cols, -1,
1169 sizeof(*row_mt_sync->num_finished_cols) * max_mb_rows);
1170 row_mt_sync->next_mi_row = this_tile->tile_info.mi_row_start;
1171 row_mt_sync->num_threads_working = 0;
1172 }
1173 }
1174
1175 num_workers = AOMMIN(num_workers, mt_info->num_workers);
1176 // Only run once to create threads and allocate thread data.
1177 if (mt_info->fp_mt_buf_init_done == 0)
1178 fp_create_enc_workers(cpi, num_workers);
1179 assign_tile_to_thread(thread_id_to_tile_id, tile_cols * tile_rows,
1180 num_workers);
1181 fp_prepare_enc_workers(cpi, fp_enc_row_mt_worker_hook, num_workers);
1182 launch_workers(&cpi->mt_info, num_workers);
1183 sync_enc_workers(&cpi->mt_info, cm, num_workers);
1184 for (int i = num_workers - 1; i >= 0; i--) {
1185 EncWorkerData *const thread_data = &cpi->mt_info.tile_thr_data[i];
1186 if (thread_data->td != &cpi->td &&
1187 cpi->oxcf.cost_upd_freq.mv < COST_UPD_OFF) {
1188 aom_free(thread_data->td->mb.mv_costs);
1189 }
1190 if (thread_data->td->mb.txfm_search_info.txb_rd_records) {
1191 aom_free(thread_data->td->mb.txfm_search_info.txb_rd_records);
1192 }
1193 }
1194 }
1195
av1_tpl_row_mt_sync_read_dummy(AV1TplRowMultiThreadSync * tpl_mt_sync,int r,int c)1196 void av1_tpl_row_mt_sync_read_dummy(AV1TplRowMultiThreadSync *tpl_mt_sync,
1197 int r, int c) {
1198 (void)tpl_mt_sync;
1199 (void)r;
1200 (void)c;
1201 return;
1202 }
1203
av1_tpl_row_mt_sync_write_dummy(AV1TplRowMultiThreadSync * tpl_mt_sync,int r,int c,int cols)1204 void av1_tpl_row_mt_sync_write_dummy(AV1TplRowMultiThreadSync *tpl_mt_sync,
1205 int r, int c, int cols) {
1206 (void)tpl_mt_sync;
1207 (void)r;
1208 (void)c;
1209 (void)cols;
1210 return;
1211 }
1212
av1_tpl_row_mt_sync_read(AV1TplRowMultiThreadSync * tpl_row_mt_sync,int r,int c)1213 void av1_tpl_row_mt_sync_read(AV1TplRowMultiThreadSync *tpl_row_mt_sync, int r,
1214 int c) {
1215 #if CONFIG_MULTITHREAD
1216 int nsync = tpl_row_mt_sync->sync_range;
1217
1218 if (r) {
1219 pthread_mutex_t *const mutex = &tpl_row_mt_sync->mutex_[r - 1];
1220 pthread_mutex_lock(mutex);
1221
1222 while (c > tpl_row_mt_sync->num_finished_cols[r - 1] - nsync)
1223 pthread_cond_wait(&tpl_row_mt_sync->cond_[r - 1], mutex);
1224 pthread_mutex_unlock(mutex);
1225 }
1226 #else
1227 (void)tpl_row_mt_sync;
1228 (void)r;
1229 (void)c;
1230 #endif // CONFIG_MULTITHREAD
1231 }
1232
av1_tpl_row_mt_sync_write(AV1TplRowMultiThreadSync * tpl_row_mt_sync,int r,int c,int cols)1233 void av1_tpl_row_mt_sync_write(AV1TplRowMultiThreadSync *tpl_row_mt_sync, int r,
1234 int c, int cols) {
1235 #if CONFIG_MULTITHREAD
1236 int nsync = tpl_row_mt_sync->sync_range;
1237 int cur;
1238 // Only signal when there are enough encoded blocks for next row to run.
1239 int sig = 1;
1240
1241 if (c < cols - 1) {
1242 cur = c;
1243 if (c % nsync) sig = 0;
1244 } else {
1245 cur = cols + nsync;
1246 }
1247
1248 if (sig) {
1249 pthread_mutex_lock(&tpl_row_mt_sync->mutex_[r]);
1250
1251 tpl_row_mt_sync->num_finished_cols[r] = cur;
1252
1253 pthread_cond_signal(&tpl_row_mt_sync->cond_[r]);
1254 pthread_mutex_unlock(&tpl_row_mt_sync->mutex_[r]);
1255 }
1256 #else
1257 (void)tpl_row_mt_sync;
1258 (void)r;
1259 (void)c;
1260 (void)cols;
1261 #endif // CONFIG_MULTITHREAD
1262 }
1263
1264 // Each worker calls tpl_worker_hook() and computes the tpl data.
tpl_worker_hook(void * arg1,void * unused)1265 static int tpl_worker_hook(void *arg1, void *unused) {
1266 (void)unused;
1267 EncWorkerData *thread_data = (EncWorkerData *)arg1;
1268 AV1_COMP *cpi = thread_data->cpi;
1269 AV1_COMMON *cm = &cpi->common;
1270 MACROBLOCK *x = &thread_data->td->mb;
1271 MACROBLOCKD *xd = &x->e_mbd;
1272 CommonModeInfoParams *mi_params = &cm->mi_params;
1273 BLOCK_SIZE bsize = convert_length_to_bsize(cpi->tpl_data.tpl_bsize_1d);
1274 TX_SIZE tx_size = max_txsize_lookup[bsize];
1275 int mi_height = mi_size_high[bsize];
1276 int num_active_workers = cpi->tpl_data.tpl_mt_sync.num_threads_working;
1277 for (int mi_row = thread_data->start * mi_height; mi_row < mi_params->mi_rows;
1278 mi_row += num_active_workers * mi_height) {
1279 // Motion estimation row boundary
1280 av1_set_mv_row_limits(mi_params, &x->mv_limits, mi_row, mi_height,
1281 cpi->oxcf.border_in_pixels);
1282 xd->mb_to_top_edge = -GET_MV_SUBPEL(mi_row * MI_SIZE);
1283 xd->mb_to_bottom_edge =
1284 GET_MV_SUBPEL((mi_params->mi_rows - mi_height - mi_row) * MI_SIZE);
1285 av1_mc_flow_dispenser_row(cpi, x, mi_row, bsize, tx_size);
1286 }
1287 return 1;
1288 }
1289
1290 // Deallocate tpl synchronization related mutex and data.
av1_tpl_dealloc(AV1TplRowMultiThreadSync * tpl_sync)1291 void av1_tpl_dealloc(AV1TplRowMultiThreadSync *tpl_sync) {
1292 assert(tpl_sync != NULL);
1293
1294 #if CONFIG_MULTITHREAD
1295 if (tpl_sync->mutex_ != NULL) {
1296 for (int i = 0; i < tpl_sync->rows; ++i)
1297 pthread_mutex_destroy(&tpl_sync->mutex_[i]);
1298 aom_free(tpl_sync->mutex_);
1299 }
1300 if (tpl_sync->cond_ != NULL) {
1301 for (int i = 0; i < tpl_sync->rows; ++i)
1302 pthread_cond_destroy(&tpl_sync->cond_[i]);
1303 aom_free(tpl_sync->cond_);
1304 }
1305 #endif // CONFIG_MULTITHREAD
1306
1307 aom_free(tpl_sync->num_finished_cols);
1308 // clear the structure as the source of this call may be a resize in which
1309 // case this call will be followed by an _alloc() which may fail.
1310 av1_zero(*tpl_sync);
1311 }
1312
1313 // Allocate memory for tpl row synchronization.
av1_tpl_alloc(AV1TplRowMultiThreadSync * tpl_sync,AV1_COMMON * cm,int mb_rows)1314 void av1_tpl_alloc(AV1TplRowMultiThreadSync *tpl_sync, AV1_COMMON *cm,
1315 int mb_rows) {
1316 tpl_sync->rows = mb_rows;
1317 #if CONFIG_MULTITHREAD
1318 {
1319 CHECK_MEM_ERROR(cm, tpl_sync->mutex_,
1320 aom_malloc(sizeof(*tpl_sync->mutex_) * mb_rows));
1321 if (tpl_sync->mutex_) {
1322 for (int i = 0; i < mb_rows; ++i)
1323 pthread_mutex_init(&tpl_sync->mutex_[i], NULL);
1324 }
1325
1326 CHECK_MEM_ERROR(cm, tpl_sync->cond_,
1327 aom_malloc(sizeof(*tpl_sync->cond_) * mb_rows));
1328 if (tpl_sync->cond_) {
1329 for (int i = 0; i < mb_rows; ++i)
1330 pthread_cond_init(&tpl_sync->cond_[i], NULL);
1331 }
1332 }
1333 #endif // CONFIG_MULTITHREAD
1334 CHECK_MEM_ERROR(cm, tpl_sync->num_finished_cols,
1335 aom_malloc(sizeof(*tpl_sync->num_finished_cols) * mb_rows));
1336
1337 // Set up nsync.
1338 tpl_sync->sync_range = 1;
1339 }
1340
1341 // Each worker is prepared by assigning the hook function and individual thread
1342 // data.
prepare_tpl_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)1343 static AOM_INLINE void prepare_tpl_workers(AV1_COMP *cpi, AVxWorkerHook hook,
1344 int num_workers) {
1345 MultiThreadInfo *mt_info = &cpi->mt_info;
1346 for (int i = num_workers - 1; i >= 0; i--) {
1347 AVxWorker *worker = &mt_info->workers[i];
1348 EncWorkerData *thread_data = &mt_info->tile_thr_data[i];
1349
1350 worker->hook = hook;
1351 worker->data1 = thread_data;
1352 worker->data2 = NULL;
1353
1354 thread_data->cpi = cpi;
1355 if (i == 0) {
1356 thread_data->td = &cpi->td;
1357 }
1358
1359 // Before encoding a frame, copy the thread data from cpi.
1360 if (thread_data->td != &cpi->td) {
1361 thread_data->td->mb = cpi->td.mb;
1362 // OBMC buffers are used only to init MS params and remain unused when
1363 // called from tpl, hence set the buffers to defaults.
1364 av1_init_obmc_buffer(&thread_data->td->mb.obmc_buffer);
1365 }
1366 }
1367 }
1368
1369 // Implements multi-threading for tpl.
av1_mc_flow_dispenser_mt(AV1_COMP * cpi)1370 void av1_mc_flow_dispenser_mt(AV1_COMP *cpi) {
1371 AV1_COMMON *cm = &cpi->common;
1372 CommonModeInfoParams *mi_params = &cm->mi_params;
1373 MultiThreadInfo *mt_info = &cpi->mt_info;
1374 TplParams *tpl_data = &cpi->tpl_data;
1375 AV1TplRowMultiThreadSync *tpl_sync = &tpl_data->tpl_mt_sync;
1376 int mb_rows = mi_params->mb_rows;
1377 int num_workers =
1378 AOMMIN(mt_info->num_mod_workers[MOD_TPL], mt_info->num_workers);
1379
1380 if (mb_rows != tpl_sync->rows) {
1381 av1_tpl_dealloc(tpl_sync);
1382 av1_tpl_alloc(tpl_sync, cm, mb_rows);
1383 }
1384 tpl_sync->num_threads_working = num_workers;
1385
1386 // Initialize cur_mb_col to -1 for all MB rows.
1387 memset(tpl_sync->num_finished_cols, -1,
1388 sizeof(*tpl_sync->num_finished_cols) * mb_rows);
1389
1390 prepare_tpl_workers(cpi, tpl_worker_hook, num_workers);
1391 launch_workers(&cpi->mt_info, num_workers);
1392 sync_enc_workers(&cpi->mt_info, cm, num_workers);
1393 }
1394
1395 // Deallocate memory for temporal filter multi-thread synchronization.
av1_tf_mt_dealloc(AV1TemporalFilterSync * tf_sync)1396 void av1_tf_mt_dealloc(AV1TemporalFilterSync *tf_sync) {
1397 assert(tf_sync != NULL);
1398 #if CONFIG_MULTITHREAD
1399 if (tf_sync->mutex_ != NULL) {
1400 pthread_mutex_destroy(tf_sync->mutex_);
1401 aom_free(tf_sync->mutex_);
1402 }
1403 #endif // CONFIG_MULTITHREAD
1404 tf_sync->next_tf_row = 0;
1405 }
1406
1407 // Checks if a job is available. If job is available,
1408 // populates next_tf_row and returns 1, else returns 0.
tf_get_next_job(AV1TemporalFilterSync * tf_mt_sync,int * current_mb_row,int mb_rows)1409 static AOM_INLINE int tf_get_next_job(AV1TemporalFilterSync *tf_mt_sync,
1410 int *current_mb_row, int mb_rows) {
1411 int do_next_row = 0;
1412 #if CONFIG_MULTITHREAD
1413 pthread_mutex_t *tf_mutex_ = tf_mt_sync->mutex_;
1414 pthread_mutex_lock(tf_mutex_);
1415 #endif
1416 if (tf_mt_sync->next_tf_row < mb_rows) {
1417 *current_mb_row = tf_mt_sync->next_tf_row;
1418 tf_mt_sync->next_tf_row++;
1419 do_next_row = 1;
1420 }
1421 #if CONFIG_MULTITHREAD
1422 pthread_mutex_unlock(tf_mutex_);
1423 #endif
1424 return do_next_row;
1425 }
1426
1427 // Hook function for each thread in temporal filter multi-threading.
tf_worker_hook(void * arg1,void * unused)1428 static int tf_worker_hook(void *arg1, void *unused) {
1429 (void)unused;
1430 EncWorkerData *thread_data = (EncWorkerData *)arg1;
1431 AV1_COMP *cpi = thread_data->cpi;
1432 ThreadData *td = thread_data->td;
1433 TemporalFilterCtx *tf_ctx = &cpi->tf_ctx;
1434 AV1TemporalFilterSync *tf_sync = &cpi->mt_info.tf_sync;
1435 const struct scale_factors *scale = &cpi->tf_ctx.sf;
1436 const int num_planes = av1_num_planes(&cpi->common);
1437 assert(num_planes >= 1 && num_planes <= MAX_MB_PLANE);
1438
1439 MACROBLOCKD *mbd = &td->mb.e_mbd;
1440 uint8_t *input_buffer[MAX_MB_PLANE];
1441 MB_MODE_INFO **input_mb_mode_info;
1442 tf_save_state(mbd, &input_mb_mode_info, input_buffer, num_planes);
1443 tf_setup_macroblockd(mbd, &td->tf_data, scale);
1444
1445 int current_mb_row = -1;
1446
1447 while (tf_get_next_job(tf_sync, ¤t_mb_row, tf_ctx->mb_rows))
1448 av1_tf_do_filtering_row(cpi, td, current_mb_row);
1449
1450 tf_restore_state(mbd, input_mb_mode_info, input_buffer, num_planes);
1451
1452 return 1;
1453 }
1454
1455 // Assigns temporal filter hook function and thread data to each worker.
prepare_tf_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers,int is_highbitdepth)1456 static void prepare_tf_workers(AV1_COMP *cpi, AVxWorkerHook hook,
1457 int num_workers, int is_highbitdepth) {
1458 MultiThreadInfo *mt_info = &cpi->mt_info;
1459 mt_info->tf_sync.next_tf_row = 0;
1460 for (int i = num_workers - 1; i >= 0; i--) {
1461 AVxWorker *worker = &mt_info->workers[i];
1462 EncWorkerData *thread_data = &mt_info->tile_thr_data[i];
1463
1464 worker->hook = hook;
1465 worker->data1 = thread_data;
1466 worker->data2 = NULL;
1467
1468 thread_data->cpi = cpi;
1469 if (i == 0) {
1470 thread_data->td = &cpi->td;
1471 }
1472
1473 // Before encoding a frame, copy the thread data from cpi.
1474 if (thread_data->td != &cpi->td) {
1475 thread_data->td->mb = cpi->td.mb;
1476 // OBMC buffers are used only to init MS params and remain unused when
1477 // called from tf, hence set the buffers to defaults.
1478 av1_init_obmc_buffer(&thread_data->td->mb.obmc_buffer);
1479 tf_alloc_and_reset_data(&thread_data->td->tf_data, cpi->tf_ctx.num_pels,
1480 is_highbitdepth);
1481 }
1482 }
1483 }
1484
1485 // Deallocate thread specific data for temporal filter.
tf_dealloc_thread_data(AV1_COMP * cpi,int num_workers,int is_highbitdepth)1486 static void tf_dealloc_thread_data(AV1_COMP *cpi, int num_workers,
1487 int is_highbitdepth) {
1488 MultiThreadInfo *mt_info = &cpi->mt_info;
1489 for (int i = num_workers - 1; i >= 0; i--) {
1490 EncWorkerData *thread_data = &mt_info->tile_thr_data[i];
1491 ThreadData *td = thread_data->td;
1492 if (td != &cpi->td) tf_dealloc_data(&td->tf_data, is_highbitdepth);
1493 }
1494 }
1495
1496 // Accumulate sse and sum after temporal filtering.
tf_accumulate_frame_diff(AV1_COMP * cpi,int num_workers)1497 static void tf_accumulate_frame_diff(AV1_COMP *cpi, int num_workers) {
1498 FRAME_DIFF *total_diff = &cpi->td.tf_data.diff;
1499 for (int i = num_workers - 1; i >= 0; i--) {
1500 AVxWorker *const worker = &cpi->mt_info.workers[i];
1501 EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
1502 ThreadData *td = thread_data->td;
1503 FRAME_DIFF *diff = &td->tf_data.diff;
1504 if (td != &cpi->td) {
1505 total_diff->sse += diff->sse;
1506 total_diff->sum += diff->sum;
1507 }
1508 }
1509 }
1510
1511 // Implements multi-threading for temporal filter.
av1_tf_do_filtering_mt(AV1_COMP * cpi)1512 void av1_tf_do_filtering_mt(AV1_COMP *cpi) {
1513 AV1_COMMON *cm = &cpi->common;
1514 MultiThreadInfo *mt_info = &cpi->mt_info;
1515 const int is_highbitdepth = cpi->tf_ctx.is_highbitdepth;
1516
1517 int num_workers =
1518 AOMMIN(mt_info->num_mod_workers[MOD_TF], mt_info->num_workers);
1519
1520 prepare_tf_workers(cpi, tf_worker_hook, num_workers, is_highbitdepth);
1521 launch_workers(mt_info, num_workers);
1522 sync_enc_workers(mt_info, cm, num_workers);
1523 tf_accumulate_frame_diff(cpi, num_workers);
1524 tf_dealloc_thread_data(cpi, num_workers, is_highbitdepth);
1525 }
1526
1527 // Checks if a job is available in the current direction. If a job is available,
1528 // frame_idx will be populated and returns 1, else returns 0.
get_next_gm_job(AV1_COMP * cpi,int * frame_idx,int cur_dir)1529 static AOM_INLINE int get_next_gm_job(AV1_COMP *cpi, int *frame_idx,
1530 int cur_dir) {
1531 GlobalMotionInfo *gm_info = &cpi->gm_info;
1532 JobInfo *job_info = &cpi->mt_info.gm_sync.job_info;
1533
1534 int total_refs = gm_info->num_ref_frames[cur_dir];
1535 int8_t cur_frame_to_process = job_info->next_frame_to_process[cur_dir];
1536
1537 if (cur_frame_to_process < total_refs && !job_info->early_exit[cur_dir]) {
1538 *frame_idx = gm_info->reference_frames[cur_dir][cur_frame_to_process].frame;
1539 job_info->next_frame_to_process[cur_dir] += 1;
1540 return 1;
1541 }
1542 return 0;
1543 }
1544
1545 // Switches the current direction and calls the function get_next_gm_job() if
1546 // the speed feature 'prune_ref_frame_for_gm_search' is not set.
switch_direction(AV1_COMP * cpi,int * frame_idx,int * cur_dir)1547 static AOM_INLINE void switch_direction(AV1_COMP *cpi, int *frame_idx,
1548 int *cur_dir) {
1549 if (cpi->sf.gm_sf.prune_ref_frame_for_gm_search) return;
1550 // Switch the direction and get next job
1551 *cur_dir = !(*cur_dir);
1552 get_next_gm_job(cpi, frame_idx, *(cur_dir));
1553 }
1554
1555 // Initializes inliers, num_inliers and segment_map.
init_gm_thread_data(const GlobalMotionInfo * gm_info,GlobalMotionThreadData * thread_data)1556 static AOM_INLINE void init_gm_thread_data(
1557 const GlobalMotionInfo *gm_info, GlobalMotionThreadData *thread_data) {
1558 for (int m = 0; m < RANSAC_NUM_MOTIONS; m++) {
1559 MotionModel motion_params = thread_data->params_by_motion[m];
1560 av1_zero(motion_params.params);
1561 motion_params.num_inliers = 0;
1562 }
1563
1564 av1_zero_array(thread_data->segment_map,
1565 gm_info->segment_map_w * gm_info->segment_map_h);
1566 }
1567
1568 // Hook function for each thread in global motion multi-threading.
gm_mt_worker_hook(void * arg1,void * unused)1569 static int gm_mt_worker_hook(void *arg1, void *unused) {
1570 (void)unused;
1571
1572 EncWorkerData *thread_data = (EncWorkerData *)arg1;
1573 AV1_COMP *cpi = thread_data->cpi;
1574 GlobalMotionInfo *gm_info = &cpi->gm_info;
1575 MultiThreadInfo *mt_info = &cpi->mt_info;
1576 JobInfo *job_info = &mt_info->gm_sync.job_info;
1577 int thread_id = thread_data->thread_id;
1578 GlobalMotionThreadData *gm_thread_data =
1579 &mt_info->gm_sync.thread_data[thread_id];
1580 int cur_dir = job_info->thread_id_to_dir[thread_id];
1581 #if CONFIG_MULTITHREAD
1582 pthread_mutex_t *gm_mt_mutex_ = mt_info->gm_sync.mutex_;
1583 #endif
1584
1585 while (1) {
1586 int ref_buf_idx = -1;
1587 int ref_frame_idx = -1;
1588
1589 #if CONFIG_MULTITHREAD
1590 pthread_mutex_lock(gm_mt_mutex_);
1591 #endif
1592
1593 // Populates ref_buf_idx(the reference frame type) for which global motion
1594 // estimation will be done.
1595 if (!get_next_gm_job(cpi, &ref_buf_idx, cur_dir)) {
1596 // No jobs are available for the current direction. Switch
1597 // to other direction and get the next job, if available.
1598 switch_direction(cpi, &ref_buf_idx, &cur_dir);
1599 }
1600
1601 // 'ref_frame_idx' holds the index of the current reference frame type in
1602 // gm_info->reference_frames. job_info->next_frame_to_process will be
1603 // incremented in get_next_gm_job() and hence subtracting by 1.
1604 ref_frame_idx = job_info->next_frame_to_process[cur_dir] - 1;
1605
1606 #if CONFIG_MULTITHREAD
1607 pthread_mutex_unlock(gm_mt_mutex_);
1608 #endif
1609
1610 if (ref_buf_idx == -1) break;
1611
1612 init_gm_thread_data(gm_info, gm_thread_data);
1613
1614 // Compute global motion for the given ref_buf_idx.
1615 av1_compute_gm_for_valid_ref_frames(
1616 cpi, gm_info->ref_buf, ref_buf_idx, gm_info->num_src_corners,
1617 gm_info->src_corners, gm_info->src_buffer,
1618 gm_thread_data->params_by_motion, gm_thread_data->segment_map,
1619 gm_info->segment_map_w, gm_info->segment_map_h);
1620
1621 #if CONFIG_MULTITHREAD
1622 pthread_mutex_lock(gm_mt_mutex_);
1623 #endif
1624 assert(ref_frame_idx != -1);
1625 // If global motion w.r.t. current ref frame is
1626 // INVALID/TRANSLATION/IDENTITY, skip the evaluation of global motion w.r.t
1627 // the remaining ref frames in that direction. The below exit is disabled
1628 // when ref frame distance w.r.t. current frame is zero. E.g.:
1629 // source_alt_ref_frame w.r.t. ARF frames.
1630 if (cpi->sf.gm_sf.prune_ref_frame_for_gm_search &&
1631 gm_info->reference_frames[cur_dir][ref_frame_idx].distance != 0 &&
1632 cpi->common.global_motion[ref_buf_idx].wmtype != ROTZOOM)
1633 job_info->early_exit[cur_dir] = 1;
1634
1635 #if CONFIG_MULTITHREAD
1636 pthread_mutex_unlock(gm_mt_mutex_);
1637 #endif
1638 }
1639 return 1;
1640 }
1641
1642 // Assigns global motion hook function and thread data to each worker.
prepare_gm_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)1643 static AOM_INLINE void prepare_gm_workers(AV1_COMP *cpi, AVxWorkerHook hook,
1644 int num_workers) {
1645 MultiThreadInfo *mt_info = &cpi->mt_info;
1646 for (int i = num_workers - 1; i >= 0; i--) {
1647 AVxWorker *worker = &mt_info->workers[i];
1648 EncWorkerData *thread_data = &mt_info->tile_thr_data[i];
1649
1650 worker->hook = hook;
1651 worker->data1 = thread_data;
1652 worker->data2 = NULL;
1653
1654 thread_data->cpi = cpi;
1655 }
1656 }
1657
1658 // Assigns available threads to past/future direction.
assign_thread_to_dir(int8_t * thread_id_to_dir,int num_workers)1659 static AOM_INLINE void assign_thread_to_dir(int8_t *thread_id_to_dir,
1660 int num_workers) {
1661 int8_t frame_dir_idx = 0;
1662
1663 for (int i = 0; i < num_workers; i++) {
1664 thread_id_to_dir[i] = frame_dir_idx++;
1665 if (frame_dir_idx == MAX_DIRECTIONS) frame_dir_idx = 0;
1666 }
1667 }
1668
1669 // Computes number of workers for global motion multi-threading.
compute_gm_workers(const AV1_COMP * cpi)1670 static AOM_INLINE int compute_gm_workers(const AV1_COMP *cpi) {
1671 int total_refs =
1672 cpi->gm_info.num_ref_frames[0] + cpi->gm_info.num_ref_frames[1];
1673 int num_gm_workers = cpi->sf.gm_sf.prune_ref_frame_for_gm_search
1674 ? AOMMIN(MAX_DIRECTIONS, total_refs)
1675 : total_refs;
1676 num_gm_workers = AOMMIN(num_gm_workers, cpi->mt_info.num_workers);
1677 return (num_gm_workers);
1678 }
1679
1680 // Frees the memory allocated for each worker in global motion multi-threading.
av1_gm_dealloc(AV1GlobalMotionSync * gm_sync_data)1681 void av1_gm_dealloc(AV1GlobalMotionSync *gm_sync_data) {
1682 if (gm_sync_data->thread_data != NULL) {
1683 for (int j = 0; j < gm_sync_data->allocated_workers; j++) {
1684 GlobalMotionThreadData *thread_data = &gm_sync_data->thread_data[j];
1685 aom_free(thread_data->segment_map);
1686
1687 for (int m = 0; m < RANSAC_NUM_MOTIONS; m++)
1688 aom_free(thread_data->params_by_motion[m].inliers);
1689 }
1690 aom_free(gm_sync_data->thread_data);
1691 }
1692 }
1693
1694 // Allocates memory for inliers and segment_map for each worker in global motion
1695 // multi-threading.
gm_alloc(AV1_COMP * cpi,int num_workers)1696 static AOM_INLINE void gm_alloc(AV1_COMP *cpi, int num_workers) {
1697 AV1_COMMON *cm = &cpi->common;
1698 AV1GlobalMotionSync *gm_sync = &cpi->mt_info.gm_sync;
1699 GlobalMotionInfo *gm_info = &cpi->gm_info;
1700
1701 gm_sync->allocated_workers = num_workers;
1702 gm_sync->allocated_width = cpi->source->y_width;
1703 gm_sync->allocated_height = cpi->source->y_height;
1704
1705 CHECK_MEM_ERROR(cm, gm_sync->thread_data,
1706 aom_malloc(sizeof(*gm_sync->thread_data) * num_workers));
1707
1708 for (int i = 0; i < num_workers; i++) {
1709 GlobalMotionThreadData *thread_data = &gm_sync->thread_data[i];
1710 CHECK_MEM_ERROR(
1711 cm, thread_data->segment_map,
1712 aom_malloc(sizeof(*thread_data->segment_map) * gm_info->segment_map_w *
1713 gm_info->segment_map_h));
1714
1715 for (int m = 0; m < RANSAC_NUM_MOTIONS; m++) {
1716 CHECK_MEM_ERROR(
1717 cm, thread_data->params_by_motion[m].inliers,
1718 aom_malloc(sizeof(*thread_data->params_by_motion[m].inliers) * 2 *
1719 MAX_CORNERS));
1720 }
1721 }
1722 }
1723
1724 // Implements multi-threading for global motion.
av1_global_motion_estimation_mt(AV1_COMP * cpi)1725 void av1_global_motion_estimation_mt(AV1_COMP *cpi) {
1726 AV1GlobalMotionSync *gm_sync = &cpi->mt_info.gm_sync;
1727 JobInfo *job_info = &gm_sync->job_info;
1728
1729 av1_zero(*job_info);
1730
1731 int num_workers = compute_gm_workers(cpi);
1732
1733 if (num_workers > gm_sync->allocated_workers ||
1734 cpi->source->y_width != gm_sync->allocated_width ||
1735 cpi->source->y_height != gm_sync->allocated_height) {
1736 av1_gm_dealloc(gm_sync);
1737 gm_alloc(cpi, num_workers);
1738 }
1739
1740 assign_thread_to_dir(job_info->thread_id_to_dir, num_workers);
1741 prepare_gm_workers(cpi, gm_mt_worker_hook, num_workers);
1742 launch_workers(&cpi->mt_info, num_workers);
1743 sync_enc_workers(&cpi->mt_info, &cpi->common, num_workers);
1744 }
1745 #endif // !CONFIG_REALTIME_ONLY
1746
1747 // Deallocate memory for CDEF search multi-thread synchronization.
av1_cdef_mt_dealloc(AV1CdefSync * cdef_sync)1748 void av1_cdef_mt_dealloc(AV1CdefSync *cdef_sync) {
1749 (void)cdef_sync;
1750 assert(cdef_sync != NULL);
1751 #if CONFIG_MULTITHREAD
1752 if (cdef_sync->mutex_ != NULL) {
1753 pthread_mutex_destroy(cdef_sync->mutex_);
1754 aom_free(cdef_sync->mutex_);
1755 }
1756 #endif // CONFIG_MULTITHREAD
1757 }
1758
1759 // Updates the row and column indices of the next job to be processed.
1760 // Also updates end_of_frame flag when the processing of all blocks is complete.
update_next_job_info(AV1CdefSync * cdef_sync,int nvfb,int nhfb)1761 static void update_next_job_info(AV1CdefSync *cdef_sync, int nvfb, int nhfb) {
1762 cdef_sync->fbc++;
1763 if (cdef_sync->fbc == nhfb) {
1764 cdef_sync->fbr++;
1765 if (cdef_sync->fbr == nvfb) {
1766 cdef_sync->end_of_frame = 1;
1767 } else {
1768 cdef_sync->fbc = 0;
1769 }
1770 }
1771 }
1772
1773 // Initializes cdef_sync parameters.
cdef_reset_job_info(AV1CdefSync * cdef_sync)1774 static AOM_INLINE void cdef_reset_job_info(AV1CdefSync *cdef_sync) {
1775 cdef_sync->end_of_frame = 0;
1776 cdef_sync->fbr = 0;
1777 cdef_sync->fbc = 0;
1778 }
1779
1780 // Checks if a job is available. If job is available,
1781 // populates next job information and returns 1, else returns 0.
cdef_get_next_job(AV1CdefSync * cdef_sync,CdefSearchCtx * cdef_search_ctx,int * cur_fbr,int * cur_fbc,int * sb_count)1782 static AOM_INLINE int cdef_get_next_job(AV1CdefSync *cdef_sync,
1783 CdefSearchCtx *cdef_search_ctx,
1784 int *cur_fbr, int *cur_fbc,
1785 int *sb_count) {
1786 #if CONFIG_MULTITHREAD
1787 pthread_mutex_lock(cdef_sync->mutex_);
1788 #endif // CONFIG_MULTITHREAD
1789 int do_next_block = 0;
1790 const int nvfb = cdef_search_ctx->nvfb;
1791 const int nhfb = cdef_search_ctx->nhfb;
1792
1793 // If a block is skip, do not process the block and
1794 // check the skip condition for the next block.
1795 while ((!cdef_sync->end_of_frame) &&
1796 (cdef_sb_skip(cdef_search_ctx->mi_params, cdef_sync->fbr,
1797 cdef_sync->fbc))) {
1798 update_next_job_info(cdef_sync, nvfb, nhfb);
1799 }
1800
1801 // Populates information needed for current job and update the row,
1802 // column indices of the next block to be processed.
1803 if (cdef_sync->end_of_frame == 0) {
1804 do_next_block = 1;
1805 *cur_fbr = cdef_sync->fbr;
1806 *cur_fbc = cdef_sync->fbc;
1807 *sb_count = cdef_search_ctx->sb_count;
1808 cdef_search_ctx->sb_count++;
1809 update_next_job_info(cdef_sync, nvfb, nhfb);
1810 }
1811 #if CONFIG_MULTITHREAD
1812 pthread_mutex_unlock(cdef_sync->mutex_);
1813 #endif // CONFIG_MULTITHREAD
1814 return do_next_block;
1815 }
1816
1817 // Hook function for each thread in CDEF search multi-threading.
cdef_filter_block_worker_hook(void * arg1,void * arg2)1818 static int cdef_filter_block_worker_hook(void *arg1, void *arg2) {
1819 AV1CdefSync *const cdef_sync = (AV1CdefSync *)arg1;
1820 CdefSearchCtx *cdef_search_ctx = (CdefSearchCtx *)arg2;
1821 int cur_fbr, cur_fbc, sb_count;
1822 while (cdef_get_next_job(cdef_sync, cdef_search_ctx, &cur_fbr, &cur_fbc,
1823 &sb_count)) {
1824 av1_cdef_mse_calc_block(cdef_search_ctx, cur_fbr, cur_fbc, sb_count);
1825 }
1826 return 1;
1827 }
1828
1829 // Assigns CDEF search hook function and thread data to each worker.
prepare_cdef_workers(MultiThreadInfo * mt_info,CdefSearchCtx * cdef_search_ctx,AVxWorkerHook hook,int num_workers)1830 static void prepare_cdef_workers(MultiThreadInfo *mt_info,
1831 CdefSearchCtx *cdef_search_ctx,
1832 AVxWorkerHook hook, int num_workers) {
1833 for (int i = num_workers - 1; i >= 0; i--) {
1834 AVxWorker *worker = &mt_info->workers[i];
1835 worker->hook = hook;
1836 worker->data1 = &mt_info->cdef_sync;
1837 worker->data2 = cdef_search_ctx;
1838 }
1839 }
1840
1841 // Implements multi-threading for CDEF search.
av1_cdef_mse_calc_frame_mt(AV1_COMMON * cm,MultiThreadInfo * mt_info,CdefSearchCtx * cdef_search_ctx)1842 void av1_cdef_mse_calc_frame_mt(AV1_COMMON *cm, MultiThreadInfo *mt_info,
1843 CdefSearchCtx *cdef_search_ctx) {
1844 AV1CdefSync *cdef_sync = &mt_info->cdef_sync;
1845 const int num_workers = mt_info->num_mod_workers[MOD_CDEF_SEARCH];
1846
1847 cdef_reset_job_info(cdef_sync);
1848 prepare_cdef_workers(mt_info, cdef_search_ctx, cdef_filter_block_worker_hook,
1849 num_workers);
1850 launch_workers(mt_info, num_workers);
1851 sync_enc_workers(mt_info, cm, num_workers);
1852 }
1853
1854 // Computes num_workers for temporal filter multi-threading.
compute_num_tf_workers(AV1_COMP * cpi)1855 static AOM_INLINE int compute_num_tf_workers(AV1_COMP *cpi) {
1856 // For single-pass encode, using no. of workers as per tf block size was not
1857 // found to improve speed. Hence the thread assignment for single-pass encode
1858 // is kept based on compute_num_enc_workers().
1859 if (cpi->oxcf.pass != 2)
1860 return (compute_num_enc_workers(cpi, cpi->oxcf.max_threads));
1861
1862 if (cpi->oxcf.max_threads <= 1) return 1;
1863
1864 const int frame_height = cpi->common.height;
1865 const BLOCK_SIZE block_size = TF_BLOCK_SIZE;
1866 const int mb_height = block_size_high[block_size];
1867 const int mb_rows = get_num_blocks(frame_height, mb_height);
1868 return AOMMIN(cpi->oxcf.max_threads, mb_rows);
1869 }
1870
1871 // Computes num_workers for tpl multi-threading.
compute_num_tpl_workers(AV1_COMP * cpi)1872 static AOM_INLINE int compute_num_tpl_workers(AV1_COMP *cpi) {
1873 return compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
1874 }
1875
1876 // Computes num_workers for loop filter multi-threading.
compute_num_lf_workers(AV1_COMP * cpi)1877 static AOM_INLINE int compute_num_lf_workers(AV1_COMP *cpi) {
1878 return compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
1879 }
1880
1881 // Computes num_workers for cdef multi-threading.
compute_num_cdef_workers(AV1_COMP * cpi)1882 static AOM_INLINE int compute_num_cdef_workers(AV1_COMP *cpi) {
1883 return compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
1884 }
1885
1886 // Computes num_workers for loop-restoration multi-threading.
compute_num_lr_workers(AV1_COMP * cpi)1887 static AOM_INLINE int compute_num_lr_workers(AV1_COMP *cpi) {
1888 return compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
1889 }
1890
compute_num_mod_workers(AV1_COMP * cpi,MULTI_THREADED_MODULES mod_name)1891 int compute_num_mod_workers(AV1_COMP *cpi, MULTI_THREADED_MODULES mod_name) {
1892 int num_mod_workers = 0;
1893 switch (mod_name) {
1894 case MOD_FP:
1895 if (cpi->oxcf.pass == 2)
1896 num_mod_workers = 0;
1897 else
1898 num_mod_workers = compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
1899 break;
1900 case MOD_TF: num_mod_workers = compute_num_tf_workers(cpi); break;
1901 case MOD_TPL: num_mod_workers = compute_num_tpl_workers(cpi); break;
1902 case MOD_GME: num_mod_workers = 1; break;
1903 case MOD_ENC:
1904 num_mod_workers = compute_num_enc_workers(cpi, cpi->oxcf.max_threads);
1905 break;
1906 case MOD_LPF: num_mod_workers = compute_num_lf_workers(cpi); break;
1907 case MOD_CDEF_SEARCH:
1908 num_mod_workers = compute_num_cdef_workers(cpi);
1909 break;
1910 case MOD_LR: num_mod_workers = compute_num_lr_workers(cpi); break;
1911 default: assert(0); break;
1912 }
1913 return (num_mod_workers);
1914 }
1915 // Computes the number of workers for each MT modules in the encoder
av1_compute_num_workers_for_mt(AV1_COMP * cpi)1916 void av1_compute_num_workers_for_mt(AV1_COMP *cpi) {
1917 for (int i = MOD_FP; i < NUM_MT_MODULES; i++)
1918 cpi->mt_info.num_mod_workers[i] =
1919 compute_num_mod_workers(cpi, (MULTI_THREADED_MODULES)i);
1920 }
1921