Coverage Report

Created: 2022-08-24 06:15

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