Coverage Report

Created: 2025-06-22 08:04

/src/aom/av1/common/thread_common.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 "aom/aom_image.h"
13
#include "config/aom_config.h"
14
#include "config/aom_scale_rtcd.h"
15
16
#include "aom_dsp/aom_dsp_common.h"
17
#include "aom_dsp/txfm_common.h"
18
#include "aom_mem/aom_mem.h"
19
#include "aom_util/aom_pthread.h"
20
#include "aom_util/aom_thread.h"
21
#include "av1/common/av1_loopfilter.h"
22
#include "av1/common/blockd.h"
23
#include "av1/common/cdef.h"
24
#include "av1/common/entropymode.h"
25
#include "av1/common/enums.h"
26
#include "av1/common/thread_common.h"
27
#include "av1/common/reconinter.h"
28
#include "av1/common/reconintra.h"
29
#include "av1/common/restoration.h"
30
31
// Set up nsync by width.
32
4.27k
static inline int get_sync_range(int width) {
33
  // nsync numbers are picked by testing. For example, for 4k
34
  // video, using 4 gives best performance.
35
4.27k
  if (width < 640)
36
4.13k
    return 1;
37
145
  else if (width <= 1280)
38
130
    return 2;
39
15
  else if (width <= 4096)
40
15
    return 4;
41
0
  else
42
0
    return 8;
43
4.27k
}
44
45
#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
46
1.62k
static inline int get_lr_sync_range(int width) {
47
#if 0
48
  // nsync numbers are picked by testing. For example, for 4k
49
  // video, using 4 gives best performance.
50
  if (width < 640)
51
    return 1;
52
  else if (width <= 1280)
53
    return 2;
54
  else if (width <= 4096)
55
    return 4;
56
  else
57
    return 8;
58
#else
59
1.62k
  (void)width;
60
1.62k
  return 1;
61
1.62k
#endif
62
1.62k
}
63
#endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
64
65
// Allocate memory for lf row synchronization
66
void av1_loop_filter_alloc(AV1LfSync *lf_sync, AV1_COMMON *cm, int rows,
67
4.27k
                           int width, int num_workers) {
68
4.27k
  lf_sync->rows = rows;
69
4.27k
#if CONFIG_MULTITHREAD
70
4.27k
  {
71
4.27k
    int i, j;
72
73
17.1k
    for (j = 0; j < MAX_MB_PLANE; j++) {
74
12.8k
      CHECK_MEM_ERROR(cm, lf_sync->mutex_[j],
75
12.8k
                      aom_malloc(sizeof(*(lf_sync->mutex_[j])) * rows));
76
12.8k
      if (lf_sync->mutex_[j]) {
77
77.7k
        for (i = 0; i < rows; ++i) {
78
64.8k
          pthread_mutex_init(&lf_sync->mutex_[j][i], NULL);
79
64.8k
        }
80
12.8k
      }
81
82
12.8k
      CHECK_MEM_ERROR(cm, lf_sync->cond_[j],
83
12.8k
                      aom_malloc(sizeof(*(lf_sync->cond_[j])) * rows));
84
12.8k
      if (lf_sync->cond_[j]) {
85
77.7k
        for (i = 0; i < rows; ++i) {
86
64.8k
          pthread_cond_init(&lf_sync->cond_[j][i], NULL);
87
64.8k
        }
88
12.8k
      }
89
12.8k
    }
90
91
4.27k
    CHECK_MEM_ERROR(cm, lf_sync->job_mutex,
92
4.27k
                    aom_malloc(sizeof(*(lf_sync->job_mutex))));
93
4.27k
    if (lf_sync->job_mutex) {
94
4.27k
      pthread_mutex_init(lf_sync->job_mutex, NULL);
95
4.27k
    }
96
4.27k
  }
97
4.27k
#endif  // CONFIG_MULTITHREAD
98
4.27k
  CHECK_MEM_ERROR(cm, lf_sync->lfdata,
99
4.27k
                  aom_malloc(num_workers * sizeof(*(lf_sync->lfdata))));
100
4.27k
  lf_sync->num_workers = num_workers;
101
102
17.1k
  for (int j = 0; j < MAX_MB_PLANE; j++) {
103
12.8k
    CHECK_MEM_ERROR(cm, lf_sync->cur_sb_col[j],
104
12.8k
                    aom_malloc(sizeof(*(lf_sync->cur_sb_col[j])) * rows));
105
12.8k
  }
106
4.27k
  CHECK_MEM_ERROR(
107
4.27k
      cm, lf_sync->job_queue,
108
4.27k
      aom_malloc(sizeof(*(lf_sync->job_queue)) * rows * MAX_MB_PLANE * 2));
109
  // Set up nsync.
110
4.27k
  lf_sync->sync_range = get_sync_range(width);
111
4.27k
}
112
113
// Deallocate lf synchronization related mutex and data
114
22.0k
void av1_loop_filter_dealloc(AV1LfSync *lf_sync) {
115
22.0k
  if (lf_sync != NULL) {
116
22.0k
    int j;
117
22.0k
#if CONFIG_MULTITHREAD
118
22.0k
    int i;
119
88.0k
    for (j = 0; j < MAX_MB_PLANE; j++) {
120
66.0k
      if (lf_sync->mutex_[j] != NULL) {
121
77.7k
        for (i = 0; i < lf_sync->rows; ++i) {
122
64.8k
          pthread_mutex_destroy(&lf_sync->mutex_[j][i]);
123
64.8k
        }
124
12.8k
        aom_free(lf_sync->mutex_[j]);
125
12.8k
      }
126
66.0k
      if (lf_sync->cond_[j] != NULL) {
127
77.7k
        for (i = 0; i < lf_sync->rows; ++i) {
128
64.8k
          pthread_cond_destroy(&lf_sync->cond_[j][i]);
129
64.8k
        }
130
12.8k
        aom_free(lf_sync->cond_[j]);
131
12.8k
      }
132
66.0k
    }
133
22.0k
    if (lf_sync->job_mutex != NULL) {
134
4.27k
      pthread_mutex_destroy(lf_sync->job_mutex);
135
4.27k
      aom_free(lf_sync->job_mutex);
136
4.27k
    }
137
22.0k
#endif  // CONFIG_MULTITHREAD
138
22.0k
    aom_free(lf_sync->lfdata);
139
88.0k
    for (j = 0; j < MAX_MB_PLANE; j++) {
140
66.0k
      aom_free(lf_sync->cur_sb_col[j]);
141
66.0k
    }
142
143
22.0k
    aom_free(lf_sync->job_queue);
144
    // clear the structure as the source of this call may be a resize in which
145
    // case this call will be followed by an _alloc() which may fail.
146
22.0k
    av1_zero(*lf_sync);
147
22.0k
  }
148
22.0k
}
149
150
void av1_alloc_cdef_sync(AV1_COMMON *const cm, AV1CdefSync *cdef_sync,
151
33.7k
                         int num_workers) {
152
33.7k
  if (num_workers < 1) return;
153
30.4k
#if CONFIG_MULTITHREAD
154
30.4k
  if (cdef_sync->mutex_ == NULL) {
155
8.67k
    CHECK_MEM_ERROR(cm, cdef_sync->mutex_,
156
8.67k
                    aom_malloc(sizeof(*(cdef_sync->mutex_))));
157
8.67k
    if (cdef_sync->mutex_) pthread_mutex_init(cdef_sync->mutex_, NULL);
158
8.67k
  }
159
#else
160
  (void)cm;
161
  (void)cdef_sync;
162
#endif  // CONFIG_MULTITHREAD
163
30.4k
}
164
165
28.2k
void av1_free_cdef_sync(AV1CdefSync *cdef_sync) {
166
28.2k
  if (cdef_sync == NULL) return;
167
28.2k
#if CONFIG_MULTITHREAD
168
28.2k
  if (cdef_sync->mutex_ != NULL) {
169
8.67k
    pthread_mutex_destroy(cdef_sync->mutex_);
170
8.67k
    aom_free(cdef_sync->mutex_);
171
8.67k
  }
172
28.2k
#endif  // CONFIG_MULTITHREAD
173
28.2k
}
174
175
static inline void cdef_row_mt_sync_read(AV1CdefSync *const cdef_sync,
176
32.8k
                                         int row) {
177
32.8k
  if (!row) return;
178
27.5k
#if CONFIG_MULTITHREAD
179
27.5k
  AV1CdefRowSync *const cdef_row_mt = cdef_sync->cdef_row_mt;
180
27.5k
  pthread_mutex_lock(cdef_row_mt[row - 1].row_mutex_);
181
31.5k
  while (cdef_row_mt[row - 1].is_row_done != 1)
182
3.98k
    pthread_cond_wait(cdef_row_mt[row - 1].row_cond_,
183
3.98k
                      cdef_row_mt[row - 1].row_mutex_);
184
27.5k
  cdef_row_mt[row - 1].is_row_done = 0;
185
27.5k
  pthread_mutex_unlock(cdef_row_mt[row - 1].row_mutex_);
186
#else
187
  (void)cdef_sync;
188
#endif  // CONFIG_MULTITHREAD
189
27.5k
}
190
191
static inline void cdef_row_mt_sync_write(AV1CdefSync *const cdef_sync,
192
32.3k
                                          int row) {
193
32.3k
#if CONFIG_MULTITHREAD
194
32.3k
  AV1CdefRowSync *const cdef_row_mt = cdef_sync->cdef_row_mt;
195
32.3k
  pthread_mutex_lock(cdef_row_mt[row].row_mutex_);
196
32.3k
  pthread_cond_signal(cdef_row_mt[row].row_cond_);
197
32.3k
  cdef_row_mt[row].is_row_done = 1;
198
32.3k
  pthread_mutex_unlock(cdef_row_mt[row].row_mutex_);
199
#else
200
  (void)cdef_sync;
201
  (void)row;
202
#endif  // CONFIG_MULTITHREAD
203
32.3k
}
204
205
static inline void sync_read(AV1LfSync *const lf_sync, int r, int c,
206
224k
                             int plane) {
207
224k
#if CONFIG_MULTITHREAD
208
224k
  const int nsync = lf_sync->sync_range;
209
210
224k
  if (r && !(c & (nsync - 1))) {
211
173k
    pthread_mutex_t *const mutex = &lf_sync->mutex_[plane][r - 1];
212
173k
    pthread_mutex_lock(mutex);
213
214
188k
    while (c > lf_sync->cur_sb_col[plane][r - 1] - nsync) {
215
15.6k
      pthread_cond_wait(&lf_sync->cond_[plane][r - 1], mutex);
216
15.6k
    }
217
173k
    pthread_mutex_unlock(mutex);
218
173k
  }
219
#else
220
  (void)lf_sync;
221
  (void)r;
222
  (void)c;
223
  (void)plane;
224
#endif  // CONFIG_MULTITHREAD
225
224k
}
226
227
static inline void sync_write(AV1LfSync *const lf_sync, int r, int c,
228
108k
                              const int sb_cols, int plane) {
229
108k
#if CONFIG_MULTITHREAD
230
108k
  const int nsync = lf_sync->sync_range;
231
108k
  int cur;
232
  // Only signal when there are enough filtered SB for next row to run.
233
108k
  int sig = 1;
234
235
108k
  if (c < sb_cols - 1) {
236
8.21k
    cur = c;
237
8.21k
    if (c % nsync) sig = 0;
238
100k
  } else {
239
100k
    cur = sb_cols + nsync;
240
100k
  }
241
242
108k
  if (sig) {
243
106k
    pthread_mutex_lock(&lf_sync->mutex_[plane][r]);
244
245
    // When a thread encounters an error, cur_sb_col[plane][r] is set to maximum
246
    // column number. In this case, the AOMMAX operation here ensures that
247
    // cur_sb_col[plane][r] is not overwritten with a smaller value thus
248
    // preventing the infinite waiting of threads in the relevant sync_read()
249
    // function.
250
106k
    lf_sync->cur_sb_col[plane][r] = AOMMAX(lf_sync->cur_sb_col[plane][r], cur);
251
252
106k
    pthread_cond_broadcast(&lf_sync->cond_[plane][r]);
253
106k
    pthread_mutex_unlock(&lf_sync->mutex_[plane][r]);
254
106k
  }
255
#else
256
  (void)lf_sync;
257
  (void)r;
258
  (void)c;
259
  (void)sb_cols;
260
  (void)plane;
261
#endif  // CONFIG_MULTITHREAD
262
108k
}
263
264
// One job of row loopfiltering.
265
void av1_thread_loop_filter_rows(
266
    const YV12_BUFFER_CONFIG *const frame_buffer, AV1_COMMON *const cm,
267
    struct macroblockd_plane *planes, MACROBLOCKD *xd, int mi_row, int plane,
268
    int dir, int lpf_opt_level, AV1LfSync *const lf_sync,
269
    struct aom_internal_error_info *error_info,
270
    AV1_DEBLOCKING_PARAMETERS *params_buf, TX_SIZE *tx_buf,
271
234k
    int num_mis_in_lpf_unit_height_log2) {
272
  // TODO(aomedia:3276): Pass error_info to the low-level functions as required
273
  // in future to handle error propagation.
274
234k
  (void)error_info;
275
234k
  const int sb_cols =
276
234k
      CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, MAX_MIB_SIZE_LOG2);
277
234k
  const int r = mi_row >> num_mis_in_lpf_unit_height_log2;
278
234k
  int mi_col, c;
279
280
234k
  const bool joint_filter_chroma = (lpf_opt_level == 2) && plane > AOM_PLANE_Y;
281
234k
  const int num_planes = joint_filter_chroma ? 2 : 1;
282
234k
  assert(IMPLIES(joint_filter_chroma, plane == AOM_PLANE_U));
283
284
234k
  if (dir == 0) {
285
264k
    for (mi_col = 0; mi_col < cm->mi_params.mi_cols; mi_col += MAX_MIB_SIZE) {
286
147k
      c = mi_col >> MAX_MIB_SIZE_LOG2;
287
288
147k
      av1_setup_dst_planes(planes, cm->seq_params->sb_size, frame_buffer,
289
147k
                           mi_row, mi_col, plane, plane + num_planes);
290
147k
      if (lpf_opt_level) {
291
0
        if (plane == AOM_PLANE_Y) {
292
0
          av1_filter_block_plane_vert_opt(cm, xd, &planes[plane], mi_row,
293
0
                                          mi_col, params_buf, tx_buf,
294
0
                                          num_mis_in_lpf_unit_height_log2);
295
0
        } else {
296
0
          av1_filter_block_plane_vert_opt_chroma(
297
0
              cm, xd, &planes[plane], mi_row, mi_col, params_buf, tx_buf, plane,
298
0
              joint_filter_chroma, num_mis_in_lpf_unit_height_log2);
299
0
        }
300
147k
      } else {
301
147k
        av1_filter_block_plane_vert(cm, xd, plane, &planes[plane], mi_row,
302
147k
                                    mi_col);
303
147k
      }
304
147k
      if (lf_sync != NULL) {
305
108k
        sync_write(lf_sync, r, c, sb_cols, plane);
306
108k
      }
307
147k
    }
308
117k
  } else if (dir == 1) {
309
264k
    for (mi_col = 0; mi_col < cm->mi_params.mi_cols; mi_col += MAX_MIB_SIZE) {
310
147k
      c = mi_col >> MAX_MIB_SIZE_LOG2;
311
312
147k
      if (lf_sync != NULL) {
313
        // Wait for vertical edge filtering of the top-right block to be
314
        // completed
315
112k
        sync_read(lf_sync, r, c, plane);
316
317
        // Wait for vertical edge filtering of the right block to be completed
318
112k
        sync_read(lf_sync, r + 1, c, plane);
319
112k
      }
320
321
147k
#if CONFIG_MULTITHREAD
322
147k
      if (lf_sync && lf_sync->num_workers > 1) {
323
112k
        pthread_mutex_lock(lf_sync->job_mutex);
324
112k
        const bool lf_mt_exit = lf_sync->lf_mt_exit;
325
112k
        pthread_mutex_unlock(lf_sync->job_mutex);
326
        // Exit in case any worker has encountered an error.
327
112k
        if (lf_mt_exit) return;
328
112k
      }
329
147k
#endif
330
331
147k
      av1_setup_dst_planes(planes, cm->seq_params->sb_size, frame_buffer,
332
147k
                           mi_row, mi_col, plane, plane + num_planes);
333
147k
      if (lpf_opt_level) {
334
0
        if (plane == AOM_PLANE_Y) {
335
0
          av1_filter_block_plane_horz_opt(cm, xd, &planes[plane], mi_row,
336
0
                                          mi_col, params_buf, tx_buf,
337
0
                                          num_mis_in_lpf_unit_height_log2);
338
0
        } else {
339
0
          av1_filter_block_plane_horz_opt_chroma(
340
0
              cm, xd, &planes[plane], mi_row, mi_col, params_buf, tx_buf, plane,
341
0
              joint_filter_chroma, num_mis_in_lpf_unit_height_log2);
342
0
        }
343
147k
      } else {
344
147k
        av1_filter_block_plane_horz(cm, xd, plane, &planes[plane], mi_row,
345
147k
                                    mi_col);
346
147k
      }
347
147k
    }
348
117k
  }
349
234k
}
350
351
void av1_set_vert_loop_filter_done(AV1_COMMON *cm, AV1LfSync *lf_sync,
352
0
                                   int num_mis_in_lpf_unit_height_log2) {
353
0
  int plane, sb_row;
354
0
  const int sb_cols =
355
0
      CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, num_mis_in_lpf_unit_height_log2);
356
0
  const int sb_rows =
357
0
      CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, num_mis_in_lpf_unit_height_log2);
358
359
  // In case of loopfilter row-multithreading, the worker on an SB row waits for
360
  // the vertical edge filtering of the right and top-right SBs. Hence, in case
361
  // a thread (main/worker) encounters an error, update that vertical
362
  // loopfiltering of every SB row in the frame is complete in order to avoid
363
  // dependent workers waiting indefinitely.
364
0
  for (sb_row = 0; sb_row < sb_rows; ++sb_row)
365
0
    for (plane = 0; plane < MAX_MB_PLANE; ++plane)
366
0
      sync_write(lf_sync, sb_row, sb_cols - 1, sb_cols, plane);
367
0
}
368
369
static inline void sync_lf_workers(AVxWorker *const workers,
370
19.3k
                                   AV1_COMMON *const cm, int num_workers) {
371
19.3k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
372
19.3k
  int had_error = workers[0].had_error;
373
19.3k
  struct aom_internal_error_info error_info;
374
375
  // Read the error_info of main thread.
376
19.3k
  if (had_error) {
377
0
    AVxWorker *const worker = &workers[0];
378
0
    error_info = ((LFWorkerData *)worker->data2)->error_info;
379
0
  }
380
381
  // Wait till all rows are finished.
382
618k
  for (int i = num_workers - 1; i > 0; --i) {
383
598k
    AVxWorker *const worker = &workers[i];
384
598k
    if (!winterface->sync(worker)) {
385
0
      had_error = 1;
386
0
      error_info = ((LFWorkerData *)worker->data2)->error_info;
387
0
    }
388
598k
  }
389
19.3k
  if (had_error) aom_internal_error_copy(cm->error, &error_info);
390
19.3k
}
391
392
// Row-based multi-threaded loopfilter hook
393
613k
static int loop_filter_row_worker(void *arg1, void *arg2) {
394
613k
  AV1LfSync *const lf_sync = (AV1LfSync *)arg1;
395
613k
  LFWorkerData *const lf_data = (LFWorkerData *)arg2;
396
613k
  AV1LfMTInfo *cur_job_info;
397
398
613k
#if CONFIG_MULTITHREAD
399
613k
  pthread_mutex_t *job_mutex_ = lf_sync->job_mutex;
400
613k
#endif
401
402
613k
  struct aom_internal_error_info *const error_info = &lf_data->error_info;
403
404
  // The jmp_buf is valid only for the duration of the function that calls
405
  // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
406
  // before it returns.
407
613k
  if (setjmp(error_info->jmp)) {
408
0
    error_info->setjmp = 0;
409
0
#if CONFIG_MULTITHREAD
410
0
    pthread_mutex_lock(job_mutex_);
411
0
    lf_sync->lf_mt_exit = true;
412
0
    pthread_mutex_unlock(job_mutex_);
413
0
#endif
414
0
    av1_set_vert_loop_filter_done(lf_data->cm, lf_sync, MAX_MIB_SIZE_LOG2);
415
0
    return 0;
416
0
  }
417
613k
  error_info->setjmp = 1;
418
419
822k
  while ((cur_job_info = get_lf_job_info(lf_sync)) != NULL) {
420
208k
    const int lpf_opt_level = cur_job_info->lpf_opt_level;
421
208k
    av1_thread_loop_filter_rows(
422
208k
        lf_data->frame_buffer, lf_data->cm, lf_data->planes, lf_data->xd,
423
208k
        cur_job_info->mi_row, cur_job_info->plane, cur_job_info->dir,
424
208k
        lpf_opt_level, lf_sync, error_info, lf_data->params_buf,
425
208k
        lf_data->tx_buf, MAX_MIB_SIZE_LOG2);
426
208k
  }
427
613k
  error_info->setjmp = 0;
428
613k
  return 1;
429
613k
}
430
431
static void loop_filter_rows_mt(YV12_BUFFER_CONFIG *frame, AV1_COMMON *cm,
432
                                MACROBLOCKD *xd, int start, int stop,
433
                                const int planes_to_lf[MAX_MB_PLANE],
434
                                AVxWorker *workers, int num_workers,
435
19.3k
                                AV1LfSync *lf_sync, int lpf_opt_level) {
436
19.3k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
437
19.3k
  int i;
438
19.3k
  loop_filter_frame_mt_init(cm, start, stop, planes_to_lf, num_workers, lf_sync,
439
19.3k
                            lpf_opt_level, MAX_MIB_SIZE_LOG2);
440
441
  // Set up loopfilter thread data.
442
637k
  for (i = num_workers - 1; i >= 0; --i) {
443
618k
    AVxWorker *const worker = &workers[i];
444
618k
    LFWorkerData *const lf_data = &lf_sync->lfdata[i];
445
446
618k
    worker->hook = loop_filter_row_worker;
447
618k
    worker->data1 = lf_sync;
448
618k
    worker->data2 = lf_data;
449
450
    // Loopfilter data
451
618k
    loop_filter_data_reset(lf_data, frame, cm, xd);
452
453
    // Start loopfiltering
454
618k
    worker->had_error = 0;
455
618k
    if (i == 0) {
456
19.3k
      winterface->execute(worker);
457
598k
    } else {
458
598k
      winterface->launch(worker);
459
598k
    }
460
618k
  }
461
462
19.3k
  sync_lf_workers(workers, cm, num_workers);
463
19.3k
}
464
465
static void loop_filter_rows(YV12_BUFFER_CONFIG *frame, AV1_COMMON *cm,
466
                             MACROBLOCKD *xd, int start, int stop,
467
                             const int planes_to_lf[MAX_MB_PLANE],
468
2.26k
                             int lpf_opt_level) {
469
  // Filter top rows of all planes first, in case the output can be partially
470
  // reconstructed row by row.
471
2.26k
  int mi_row, plane, dir;
472
473
2.26k
  AV1_DEBLOCKING_PARAMETERS params_buf[MAX_MIB_SIZE];
474
2.26k
  TX_SIZE tx_buf[MAX_MIB_SIZE];
475
7.83k
  for (mi_row = start; mi_row < stop; mi_row += MAX_MIB_SIZE) {
476
22.2k
    for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
477
16.7k
      if (skip_loop_filter_plane(planes_to_lf, plane, lpf_opt_level)) {
478
3.57k
        continue;
479
3.57k
      }
480
481
39.4k
      for (dir = 0; dir < 2; ++dir) {
482
26.2k
        av1_thread_loop_filter_rows(frame, cm, xd->plane, xd, mi_row, plane,
483
26.2k
                                    dir, lpf_opt_level, /*lf_sync=*/NULL,
484
26.2k
                                    xd->error_info, params_buf, tx_buf,
485
26.2k
                                    MAX_MIB_SIZE_LOG2);
486
26.2k
      }
487
13.1k
    }
488
5.57k
  }
489
2.26k
}
490
491
void av1_loop_filter_frame_mt(YV12_BUFFER_CONFIG *frame, AV1_COMMON *cm,
492
                              MACROBLOCKD *xd, int plane_start, int plane_end,
493
                              int partial_frame, AVxWorker *workers,
494
                              int num_workers, AV1LfSync *lf_sync,
495
21.5k
                              int lpf_opt_level) {
496
21.5k
  int start_mi_row, end_mi_row, mi_rows_to_filter;
497
21.5k
  int planes_to_lf[MAX_MB_PLANE];
498
499
21.5k
  if (!check_planes_to_loop_filter(&cm->lf, planes_to_lf, plane_start,
500
21.5k
                                   plane_end))
501
0
    return;
502
503
21.5k
  start_mi_row = 0;
504
21.5k
  mi_rows_to_filter = cm->mi_params.mi_rows;
505
21.5k
  if (partial_frame && cm->mi_params.mi_rows > 8) {
506
0
    start_mi_row = cm->mi_params.mi_rows >> 1;
507
0
    start_mi_row &= 0xfffffff8;
508
0
    mi_rows_to_filter = AOMMAX(cm->mi_params.mi_rows / 8, 8);
509
0
  }
510
21.5k
  end_mi_row = start_mi_row + mi_rows_to_filter;
511
21.5k
  av1_loop_filter_frame_init(cm, plane_start, plane_end);
512
513
21.5k
  if (num_workers > 1) {
514
    // Enqueue and execute loopfiltering jobs.
515
19.3k
    loop_filter_rows_mt(frame, cm, xd, start_mi_row, end_mi_row, planes_to_lf,
516
19.3k
                        workers, num_workers, lf_sync, lpf_opt_level);
517
19.3k
  } else {
518
    // Directly filter in the main thread.
519
2.26k
    loop_filter_rows(frame, cm, xd, start_mi_row, end_mi_row, planes_to_lf,
520
2.26k
                     lpf_opt_level);
521
2.26k
  }
522
21.5k
}
523
524
#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
525
25.0k
static inline void lr_sync_read(void *const lr_sync, int r, int c, int plane) {
526
25.0k
#if CONFIG_MULTITHREAD
527
25.0k
  AV1LrSync *const loop_res_sync = (AV1LrSync *)lr_sync;
528
25.0k
  const int nsync = loop_res_sync->sync_range;
529
530
25.0k
  if (r && !(c & (nsync - 1))) {
531
25.0k
    pthread_mutex_t *const mutex = &loop_res_sync->mutex_[plane][r - 1];
532
25.0k
    pthread_mutex_lock(mutex);
533
534
32.2k
    while (c > loop_res_sync->cur_sb_col[plane][r - 1] - nsync) {
535
7.23k
      pthread_cond_wait(&loop_res_sync->cond_[plane][r - 1], mutex);
536
7.23k
    }
537
25.0k
    pthread_mutex_unlock(mutex);
538
25.0k
  }
539
#else
540
  (void)lr_sync;
541
  (void)r;
542
  (void)c;
543
  (void)plane;
544
#endif  // CONFIG_MULTITHREAD
545
25.0k
}
546
547
static inline void lr_sync_write(void *const lr_sync, int r, int c,
548
20.2k
                                 const int sb_cols, int plane) {
549
20.2k
#if CONFIG_MULTITHREAD
550
20.2k
  AV1LrSync *const loop_res_sync = (AV1LrSync *)lr_sync;
551
20.2k
  const int nsync = loop_res_sync->sync_range;
552
20.2k
  int cur;
553
  // Only signal when there are enough filtered SB for next row to run.
554
20.2k
  int sig = 1;
555
556
20.2k
  if (c < sb_cols - 1) {
557
1.55k
    cur = c;
558
1.55k
    if (c % nsync) sig = 0;
559
18.6k
  } else {
560
18.6k
    cur = sb_cols + nsync;
561
18.6k
  }
562
563
20.2k
  if (sig) {
564
20.2k
    pthread_mutex_lock(&loop_res_sync->mutex_[plane][r]);
565
566
    // When a thread encounters an error, cur_sb_col[plane][r] is set to maximum
567
    // column number. In this case, the AOMMAX operation here ensures that
568
    // cur_sb_col[plane][r] is not overwritten with a smaller value thus
569
    // preventing the infinite waiting of threads in the relevant sync_read()
570
    // function.
571
20.2k
    loop_res_sync->cur_sb_col[plane][r] =
572
20.2k
        AOMMAX(loop_res_sync->cur_sb_col[plane][r], cur);
573
574
20.2k
    pthread_cond_broadcast(&loop_res_sync->cond_[plane][r]);
575
20.2k
    pthread_mutex_unlock(&loop_res_sync->mutex_[plane][r]);
576
20.2k
  }
577
#else
578
  (void)lr_sync;
579
  (void)r;
580
  (void)c;
581
  (void)sb_cols;
582
  (void)plane;
583
#endif  // CONFIG_MULTITHREAD
584
20.2k
}
585
586
// Allocate memory for loop restoration row synchronization
587
void av1_loop_restoration_alloc(AV1LrSync *lr_sync, AV1_COMMON *cm,
588
                                int num_workers, int num_rows_lr,
589
1.62k
                                int num_planes, int width) {
590
1.62k
  lr_sync->rows = num_rows_lr;
591
1.62k
  lr_sync->num_planes = num_planes;
592
1.62k
#if CONFIG_MULTITHREAD
593
1.62k
  {
594
1.62k
    int i, j;
595
596
6.13k
    for (j = 0; j < num_planes; j++) {
597
4.51k
      CHECK_MEM_ERROR(cm, lr_sync->mutex_[j],
598
4.51k
                      aom_malloc(sizeof(*(lr_sync->mutex_[j])) * num_rows_lr));
599
4.51k
      if (lr_sync->mutex_[j]) {
600
31.2k
        for (i = 0; i < num_rows_lr; ++i) {
601
26.7k
          pthread_mutex_init(&lr_sync->mutex_[j][i], NULL);
602
26.7k
        }
603
4.51k
      }
604
605
4.51k
      CHECK_MEM_ERROR(cm, lr_sync->cond_[j],
606
4.51k
                      aom_malloc(sizeof(*(lr_sync->cond_[j])) * num_rows_lr));
607
4.51k
      if (lr_sync->cond_[j]) {
608
31.2k
        for (i = 0; i < num_rows_lr; ++i) {
609
26.7k
          pthread_cond_init(&lr_sync->cond_[j][i], NULL);
610
26.7k
        }
611
4.51k
      }
612
4.51k
    }
613
614
1.62k
    CHECK_MEM_ERROR(cm, lr_sync->job_mutex,
615
1.62k
                    aom_malloc(sizeof(*(lr_sync->job_mutex))));
616
1.62k
    if (lr_sync->job_mutex) {
617
1.62k
      pthread_mutex_init(lr_sync->job_mutex, NULL);
618
1.62k
    }
619
1.62k
  }
620
1.62k
#endif  // CONFIG_MULTITHREAD
621
1.62k
  CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata,
622
1.62k
                  aom_calloc(num_workers, sizeof(*(lr_sync->lrworkerdata))));
623
1.62k
  lr_sync->num_workers = num_workers;
624
625
53.6k
  for (int worker_idx = 0; worker_idx < num_workers; ++worker_idx) {
626
52.0k
    if (worker_idx < num_workers - 1) {
627
50.4k
      CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata[worker_idx].rst_tmpbuf,
628
50.4k
                      (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE));
629
50.4k
      CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata[worker_idx].rlbs,
630
50.4k
                      aom_malloc(sizeof(RestorationLineBuffers)));
631
632
50.4k
    } else {
633
1.62k
      lr_sync->lrworkerdata[worker_idx].rst_tmpbuf = cm->rst_tmpbuf;
634
1.62k
      lr_sync->lrworkerdata[worker_idx].rlbs = cm->rlbs;
635
1.62k
    }
636
52.0k
  }
637
638
6.13k
  for (int j = 0; j < num_planes; j++) {
639
4.51k
    CHECK_MEM_ERROR(
640
4.51k
        cm, lr_sync->cur_sb_col[j],
641
4.51k
        aom_malloc(sizeof(*(lr_sync->cur_sb_col[j])) * num_rows_lr));
642
4.51k
  }
643
1.62k
  CHECK_MEM_ERROR(
644
1.62k
      cm, lr_sync->job_queue,
645
1.62k
      aom_malloc(sizeof(*(lr_sync->job_queue)) * num_rows_lr * num_planes));
646
  // Set up nsync.
647
1.62k
  lr_sync->sync_range = get_lr_sync_range(width);
648
1.62k
}
649
650
// Deallocate loop restoration synchronization related mutex and data
651
19.3k
void av1_loop_restoration_dealloc(AV1LrSync *lr_sync) {
652
19.3k
  if (lr_sync != NULL) {
653
19.3k
    int j;
654
19.3k
#if CONFIG_MULTITHREAD
655
19.3k
    int i;
656
77.4k
    for (j = 0; j < MAX_MB_PLANE; j++) {
657
58.1k
      if (lr_sync->mutex_[j] != NULL) {
658
31.2k
        for (i = 0; i < lr_sync->rows; ++i) {
659
26.7k
          pthread_mutex_destroy(&lr_sync->mutex_[j][i]);
660
26.7k
        }
661
4.51k
        aom_free(lr_sync->mutex_[j]);
662
4.51k
      }
663
58.1k
      if (lr_sync->cond_[j] != NULL) {
664
31.2k
        for (i = 0; i < lr_sync->rows; ++i) {
665
26.7k
          pthread_cond_destroy(&lr_sync->cond_[j][i]);
666
26.7k
        }
667
4.51k
        aom_free(lr_sync->cond_[j]);
668
4.51k
      }
669
58.1k
    }
670
19.3k
    if (lr_sync->job_mutex != NULL) {
671
1.62k
      pthread_mutex_destroy(lr_sync->job_mutex);
672
1.62k
      aom_free(lr_sync->job_mutex);
673
1.62k
    }
674
19.3k
#endif  // CONFIG_MULTITHREAD
675
77.4k
    for (j = 0; j < MAX_MB_PLANE; j++) {
676
58.1k
      aom_free(lr_sync->cur_sb_col[j]);
677
58.1k
    }
678
679
19.3k
    aom_free(lr_sync->job_queue);
680
681
19.3k
    if (lr_sync->lrworkerdata) {
682
52.0k
      for (int worker_idx = 0; worker_idx < lr_sync->num_workers - 1;
683
50.4k
           worker_idx++) {
684
50.4k
        LRWorkerData *const workerdata_data =
685
50.4k
            lr_sync->lrworkerdata + worker_idx;
686
687
50.4k
        aom_free(workerdata_data->rst_tmpbuf);
688
50.4k
        aom_free(workerdata_data->rlbs);
689
50.4k
      }
690
1.62k
      aom_free(lr_sync->lrworkerdata);
691
1.62k
    }
692
693
    // clear the structure as the source of this call may be a resize in which
694
    // case this call will be followed by an _alloc() which may fail.
695
19.3k
    av1_zero(*lr_sync);
696
19.3k
  }
697
19.3k
}
698
699
static void enqueue_lr_jobs(AV1LrSync *lr_sync, AV1LrStruct *lr_ctxt,
700
4.92k
                            AV1_COMMON *cm) {
701
4.92k
  FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
702
703
4.92k
  const int num_planes = av1_num_planes(cm);
704
4.92k
  AV1LrMTInfo *lr_job_queue = lr_sync->job_queue;
705
4.92k
  int32_t lr_job_counter[2], num_even_lr_jobs = 0;
706
4.92k
  lr_sync->jobs_enqueued = 0;
707
4.92k
  lr_sync->jobs_dequeued = 0;
708
709
19.2k
  for (int plane = 0; plane < num_planes; plane++) {
710
14.3k
    if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
711
8.66k
    num_even_lr_jobs =
712
8.66k
        num_even_lr_jobs + ((ctxt[plane].rsi->vert_units + 1) >> 1);
713
8.66k
  }
714
4.92k
  lr_job_counter[0] = 0;
715
4.92k
  lr_job_counter[1] = num_even_lr_jobs;
716
717
19.2k
  for (int plane = 0; plane < num_planes; plane++) {
718
14.3k
    if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
719
8.66k
    const int is_uv = plane > 0;
720
8.66k
    const int ss_y = is_uv && cm->seq_params->subsampling_y;
721
8.66k
    const int unit_size = ctxt[plane].rsi->restoration_unit_size;
722
8.66k
    const int plane_h = ctxt[plane].plane_h;
723
8.66k
    const int ext_size = unit_size * 3 / 2;
724
725
8.66k
    int y0 = 0, i = 0;
726
41.9k
    while (y0 < plane_h) {
727
33.3k
      int remaining_h = plane_h - y0;
728
33.3k
      int h = (remaining_h < ext_size) ? remaining_h : unit_size;
729
730
33.3k
      RestorationTileLimits limits;
731
33.3k
      limits.v_start = y0;
732
33.3k
      limits.v_end = y0 + h;
733
33.3k
      assert(limits.v_end <= plane_h);
734
      // Offset upwards to align with the restoration processing stripe
735
33.3k
      const int voffset = RESTORATION_UNIT_OFFSET >> ss_y;
736
33.3k
      limits.v_start = AOMMAX(0, limits.v_start - voffset);
737
33.3k
      if (limits.v_end < plane_h) limits.v_end -= voffset;
738
739
33.3k
      assert(lr_job_counter[0] <= num_even_lr_jobs);
740
741
33.3k
      lr_job_queue[lr_job_counter[i & 1]].lr_unit_row = i;
742
33.3k
      lr_job_queue[lr_job_counter[i & 1]].plane = plane;
743
33.3k
      lr_job_queue[lr_job_counter[i & 1]].v_start = limits.v_start;
744
33.3k
      lr_job_queue[lr_job_counter[i & 1]].v_end = limits.v_end;
745
33.3k
      lr_job_queue[lr_job_counter[i & 1]].sync_mode = i & 1;
746
33.3k
      if ((i & 1) == 0) {
747
18.9k
        lr_job_queue[lr_job_counter[i & 1]].v_copy_start =
748
18.9k
            limits.v_start + RESTORATION_BORDER;
749
18.9k
        lr_job_queue[lr_job_counter[i & 1]].v_copy_end =
750
18.9k
            limits.v_end - RESTORATION_BORDER;
751
18.9k
        if (i == 0) {
752
8.66k
          assert(limits.v_start == 0);
753
8.66k
          lr_job_queue[lr_job_counter[i & 1]].v_copy_start = 0;
754
8.66k
        }
755
18.9k
        if (i == (ctxt[plane].rsi->vert_units - 1)) {
756
4.55k
          assert(limits.v_end == plane_h);
757
4.55k
          lr_job_queue[lr_job_counter[i & 1]].v_copy_end = plane_h;
758
4.55k
        }
759
18.9k
      } else {
760
14.3k
        lr_job_queue[lr_job_counter[i & 1]].v_copy_start =
761
14.3k
            AOMMAX(limits.v_start - RESTORATION_BORDER, 0);
762
14.3k
        lr_job_queue[lr_job_counter[i & 1]].v_copy_end =
763
14.3k
            AOMMIN(limits.v_end + RESTORATION_BORDER, plane_h);
764
14.3k
      }
765
33.3k
      lr_job_counter[i & 1]++;
766
33.3k
      lr_sync->jobs_enqueued++;
767
768
33.3k
      y0 += h;
769
33.3k
      ++i;
770
33.3k
    }
771
8.66k
  }
772
4.92k
}
773
774
188k
static AV1LrMTInfo *get_lr_job_info(AV1LrSync *lr_sync) {
775
188k
  AV1LrMTInfo *cur_job_info = NULL;
776
777
188k
#if CONFIG_MULTITHREAD
778
188k
  pthread_mutex_lock(lr_sync->job_mutex);
779
780
190k
  if (!lr_sync->lr_mt_exit && lr_sync->jobs_dequeued < lr_sync->jobs_enqueued) {
781
33.3k
    cur_job_info = lr_sync->job_queue + lr_sync->jobs_dequeued;
782
33.3k
    lr_sync->jobs_dequeued++;
783
33.3k
  }
784
785
188k
  pthread_mutex_unlock(lr_sync->job_mutex);
786
#else
787
  (void)lr_sync;
788
#endif
789
790
188k
  return cur_job_info;
791
188k
}
792
793
static void set_loop_restoration_done(AV1LrSync *const lr_sync,
794
0
                                      FilterFrameCtxt *const ctxt) {
795
0
  for (int plane = 0; plane < MAX_MB_PLANE; ++plane) {
796
0
    if (ctxt[plane].rsi->frame_restoration_type == RESTORE_NONE) continue;
797
0
    int y0 = 0, row_number = 0;
798
0
    const int unit_size = ctxt[plane].rsi->restoration_unit_size;
799
0
    const int plane_h = ctxt[plane].plane_h;
800
0
    const int ext_size = unit_size * 3 / 2;
801
0
    const int hnum_rest_units = ctxt[plane].rsi->horz_units;
802
0
    while (y0 < plane_h) {
803
0
      const int remaining_h = plane_h - y0;
804
0
      const int h = (remaining_h < ext_size) ? remaining_h : unit_size;
805
0
      lr_sync_write(lr_sync, row_number, hnum_rest_units - 1, hnum_rest_units,
806
0
                    plane);
807
0
      y0 += h;
808
0
      ++row_number;
809
0
    }
810
0
  }
811
0
}
812
813
// Implement row loop restoration for each thread.
814
156k
static int loop_restoration_row_worker(void *arg1, void *arg2) {
815
156k
  AV1LrSync *const lr_sync = (AV1LrSync *)arg1;
816
156k
  LRWorkerData *lrworkerdata = (LRWorkerData *)arg2;
817
156k
  AV1LrStruct *lr_ctxt = (AV1LrStruct *)lrworkerdata->lr_ctxt;
818
156k
  FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
819
156k
  int lr_unit_row;
820
156k
  int plane;
821
156k
  int plane_w;
822
156k
#if CONFIG_MULTITHREAD
823
156k
  pthread_mutex_t *job_mutex_ = lr_sync->job_mutex;
824
156k
#endif
825
156k
  struct aom_internal_error_info *const error_info = &lrworkerdata->error_info;
826
827
  // The jmp_buf is valid only for the duration of the function that calls
828
  // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
829
  // before it returns.
830
156k
  if (setjmp(error_info->jmp)) {
831
0
    error_info->setjmp = 0;
832
0
#if CONFIG_MULTITHREAD
833
0
    pthread_mutex_lock(job_mutex_);
834
0
    lr_sync->lr_mt_exit = true;
835
0
    pthread_mutex_unlock(job_mutex_);
836
0
#endif
837
    // In case of loop restoration multithreading, the worker on an even lr
838
    // block row waits for the completion of the filtering of the top-right and
839
    // bottom-right blocks. Hence, in case a thread (main/worker) encounters an
840
    // error, update that filtering of every row in the frame is complete in
841
    // order to avoid the dependent workers from waiting indefinitely.
842
0
    set_loop_restoration_done(lr_sync, lr_ctxt->ctxt);
843
0
    return 0;
844
0
  }
845
156k
  error_info->setjmp = 1;
846
847
156k
  typedef void (*copy_fun)(const YV12_BUFFER_CONFIG *src_ybc,
848
156k
                           YV12_BUFFER_CONFIG *dst_ybc, int hstart, int hend,
849
156k
                           int vstart, int vend);
850
156k
  static const copy_fun copy_funs[MAX_MB_PLANE] = {
851
156k
    aom_yv12_partial_coloc_copy_y, aom_yv12_partial_coloc_copy_u,
852
156k
    aom_yv12_partial_coloc_copy_v
853
156k
  };
854
855
189k
  while (1) {
856
188k
    AV1LrMTInfo *cur_job_info = get_lr_job_info(lr_sync);
857
188k
    if (cur_job_info != NULL) {
858
33.2k
      RestorationTileLimits limits;
859
33.2k
      sync_read_fn_t on_sync_read;
860
33.2k
      sync_write_fn_t on_sync_write;
861
33.2k
      limits.v_start = cur_job_info->v_start;
862
33.2k
      limits.v_end = cur_job_info->v_end;
863
33.2k
      lr_unit_row = cur_job_info->lr_unit_row;
864
33.2k
      plane = cur_job_info->plane;
865
33.2k
      plane_w = ctxt[plane].plane_w;
866
867
      // sync_mode == 1 implies only sync read is required in LR Multi-threading
868
      // sync_mode == 0 implies only sync write is required.
869
33.2k
      on_sync_read =
870
33.2k
          cur_job_info->sync_mode == 1 ? lr_sync_read : av1_lr_sync_read_dummy;
871
33.2k
      on_sync_write = cur_job_info->sync_mode == 0 ? lr_sync_write
872
33.2k
                                                   : av1_lr_sync_write_dummy;
873
874
33.2k
      av1_foreach_rest_unit_in_row(
875
33.2k
          &limits, plane_w, lr_ctxt->on_rest_unit, lr_unit_row,
876
33.2k
          ctxt[plane].rsi->restoration_unit_size, ctxt[plane].rsi->horz_units,
877
33.2k
          ctxt[plane].rsi->vert_units, plane, &ctxt[plane],
878
33.2k
          lrworkerdata->rst_tmpbuf, lrworkerdata->rlbs, on_sync_read,
879
33.2k
          on_sync_write, lr_sync, error_info);
880
881
33.2k
      copy_funs[plane](lr_ctxt->dst, lr_ctxt->frame, 0, plane_w,
882
33.2k
                       cur_job_info->v_copy_start, cur_job_info->v_copy_end);
883
884
33.2k
      if (lrworkerdata->do_extend_border) {
885
0
        aom_extend_frame_borders_plane_row(lr_ctxt->frame, plane,
886
0
                                           cur_job_info->v_copy_start,
887
0
                                           cur_job_info->v_copy_end);
888
0
      }
889
155k
    } else {
890
155k
      break;
891
155k
    }
892
188k
  }
893
156k
  error_info->setjmp = 0;
894
156k
  return 1;
895
156k
}
896
897
static inline void sync_lr_workers(AVxWorker *const workers,
898
4.92k
                                   AV1_COMMON *const cm, int num_workers) {
899
4.92k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
900
4.92k
  int had_error = workers[0].had_error;
901
4.92k
  struct aom_internal_error_info error_info;
902
903
  // Read the error_info of main thread.
904
4.92k
  if (had_error) {
905
0
    AVxWorker *const worker = &workers[0];
906
0
    error_info = ((LRWorkerData *)worker->data2)->error_info;
907
0
  }
908
909
  // Wait till all rows are finished.
910
157k
  for (int i = num_workers - 1; i > 0; --i) {
911
152k
    AVxWorker *const worker = &workers[i];
912
152k
    if (!winterface->sync(worker)) {
913
0
      had_error = 1;
914
0
      error_info = ((LRWorkerData *)worker->data2)->error_info;
915
0
    }
916
152k
  }
917
4.92k
  if (had_error) aom_internal_error_copy(cm->error, &error_info);
918
4.92k
}
919
920
static void foreach_rest_unit_in_planes_mt(AV1LrStruct *lr_ctxt,
921
                                           AVxWorker *workers, int num_workers,
922
                                           AV1LrSync *lr_sync, AV1_COMMON *cm,
923
4.92k
                                           int do_extend_border) {
924
4.92k
  FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
925
926
4.92k
  const int num_planes = av1_num_planes(cm);
927
928
4.92k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
929
4.92k
  int num_rows_lr = 0;
930
931
19.2k
  for (int plane = 0; plane < num_planes; plane++) {
932
14.3k
    if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
933
934
8.66k
    const int plane_h = ctxt[plane].plane_h;
935
8.66k
    const int unit_size = cm->rst_info[plane].restoration_unit_size;
936
937
8.66k
    num_rows_lr = AOMMAX(num_rows_lr, av1_lr_count_units(unit_size, plane_h));
938
8.66k
  }
939
940
4.92k
  int i;
941
4.92k
  assert(MAX_MB_PLANE == 3);
942
943
4.92k
  if (!lr_sync->sync_range || num_rows_lr > lr_sync->rows ||
944
4.92k
      num_workers > lr_sync->num_workers || num_planes > lr_sync->num_planes) {
945
1.62k
    av1_loop_restoration_dealloc(lr_sync);
946
1.62k
    av1_loop_restoration_alloc(lr_sync, cm, num_workers, num_rows_lr,
947
1.62k
                               num_planes, cm->width);
948
1.62k
  }
949
4.92k
  lr_sync->lr_mt_exit = false;
950
951
  // Initialize cur_sb_col to -1 for all SB rows.
952
19.2k
  for (i = 0; i < num_planes; i++) {
953
14.3k
    memset(lr_sync->cur_sb_col[i], -1,
954
14.3k
           sizeof(*(lr_sync->cur_sb_col[i])) * num_rows_lr);
955
14.3k
  }
956
957
4.92k
  enqueue_lr_jobs(lr_sync, lr_ctxt, cm);
958
959
  // Set up looprestoration thread data.
960
162k
  for (i = num_workers - 1; i >= 0; --i) {
961
157k
    AVxWorker *const worker = &workers[i];
962
157k
    lr_sync->lrworkerdata[i].lr_ctxt = (void *)lr_ctxt;
963
157k
    lr_sync->lrworkerdata[i].do_extend_border = do_extend_border;
964
157k
    worker->hook = loop_restoration_row_worker;
965
157k
    worker->data1 = lr_sync;
966
157k
    worker->data2 = &lr_sync->lrworkerdata[i];
967
968
    // Start loop restoration
969
157k
    worker->had_error = 0;
970
157k
    if (i == 0) {
971
4.92k
      winterface->execute(worker);
972
152k
    } else {
973
152k
      winterface->launch(worker);
974
152k
    }
975
157k
  }
976
977
4.92k
  sync_lr_workers(workers, cm, num_workers);
978
4.92k
}
979
980
void av1_loop_restoration_filter_frame_mt(YV12_BUFFER_CONFIG *frame,
981
                                          AV1_COMMON *cm, int optimized_lr,
982
                                          AVxWorker *workers, int num_workers,
983
                                          AV1LrSync *lr_sync, void *lr_ctxt,
984
4.92k
                                          int do_extend_border) {
985
4.92k
  assert(!cm->features.all_lossless);
986
987
4.92k
  const int num_planes = av1_num_planes(cm);
988
989
4.92k
  AV1LrStruct *loop_rest_ctxt = (AV1LrStruct *)lr_ctxt;
990
991
4.92k
  av1_loop_restoration_filter_frame_init(loop_rest_ctxt, frame, cm,
992
4.92k
                                         optimized_lr, num_planes);
993
994
4.92k
  foreach_rest_unit_in_planes_mt(loop_rest_ctxt, workers, num_workers, lr_sync,
995
4.92k
                                 cm, do_extend_border);
996
4.92k
}
997
#endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
998
999
// Initializes cdef_sync parameters.
1000
5.29k
static inline void reset_cdef_job_info(AV1CdefSync *const cdef_sync) {
1001
5.29k
  cdef_sync->end_of_frame = 0;
1002
5.29k
  cdef_sync->fbr = 0;
1003
5.29k
  cdef_sync->fbc = 0;
1004
5.29k
  cdef_sync->cdef_mt_exit = false;
1005
5.29k
}
1006
1007
static inline void launch_cdef_workers(AVxWorker *const workers,
1008
5.29k
                                       int num_workers) {
1009
5.29k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
1010
174k
  for (int i = num_workers - 1; i >= 0; i--) {
1011
169k
    AVxWorker *const worker = &workers[i];
1012
169k
    worker->had_error = 0;
1013
169k
    if (i == 0)
1014
5.29k
      winterface->execute(worker);
1015
164k
    else
1016
164k
      winterface->launch(worker);
1017
169k
  }
1018
5.29k
}
1019
1020
static inline void sync_cdef_workers(AVxWorker *const workers,
1021
5.29k
                                     AV1_COMMON *const cm, int num_workers) {
1022
5.29k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
1023
5.29k
  int had_error = workers[0].had_error;
1024
5.29k
  struct aom_internal_error_info error_info;
1025
1026
  // Read the error_info of main thread.
1027
5.29k
  if (had_error) {
1028
0
    AVxWorker *const worker = &workers[0];
1029
0
    error_info = ((AV1CdefWorkerData *)worker->data2)->error_info;
1030
0
  }
1031
1032
  // Wait till all rows are finished.
1033
169k
  for (int i = num_workers - 1; i > 0; --i) {
1034
164k
    AVxWorker *const worker = &workers[i];
1035
164k
    if (!winterface->sync(worker)) {
1036
0
      had_error = 1;
1037
0
      error_info = ((AV1CdefWorkerData *)worker->data2)->error_info;
1038
0
    }
1039
164k
  }
1040
5.29k
  if (had_error) aom_internal_error_copy(cm->error, &error_info);
1041
5.29k
}
1042
1043
// Updates the row index of the next job to be processed.
1044
// Also updates end_of_frame flag when the processing of all rows is complete.
1045
static void update_cdef_row_next_job_info(AV1CdefSync *const cdef_sync,
1046
33.0k
                                          const int nvfb) {
1047
33.0k
  cdef_sync->fbr++;
1048
33.0k
  if (cdef_sync->fbr == nvfb) {
1049
5.29k
    cdef_sync->end_of_frame = 1;
1050
5.29k
  }
1051
33.0k
}
1052
1053
// Checks if a job is available. If job is available,
1054
// populates next job information and returns 1, else returns 0.
1055
static inline int get_cdef_row_next_job(AV1CdefSync *const cdef_sync,
1056
200k
                                        volatile int *cur_fbr, const int nvfb) {
1057
200k
#if CONFIG_MULTITHREAD
1058
200k
  pthread_mutex_lock(cdef_sync->mutex_);
1059
200k
#endif  // CONFIG_MULTITHREAD
1060
200k
  int do_next_row = 0;
1061
  // Populates information needed for current job and update the row
1062
  // index of the next row to be processed.
1063
202k
  if (!cdef_sync->cdef_mt_exit && cdef_sync->end_of_frame == 0) {
1064
33.0k
    do_next_row = 1;
1065
33.0k
    *cur_fbr = cdef_sync->fbr;
1066
33.0k
    update_cdef_row_next_job_info(cdef_sync, nvfb);
1067
33.0k
  }
1068
200k
#if CONFIG_MULTITHREAD
1069
200k
  pthread_mutex_unlock(cdef_sync->mutex_);
1070
200k
#endif  // CONFIG_MULTITHREAD
1071
200k
  return do_next_row;
1072
200k
}
1073
1074
0
static void set_cdef_init_fb_row_done(AV1CdefSync *const cdef_sync, int nvfb) {
1075
0
  for (int fbr = 0; fbr < nvfb; fbr++) cdef_row_mt_sync_write(cdef_sync, fbr);
1076
0
}
1077
1078
// Hook function for each thread in CDEF multi-threading.
1079
168k
static int cdef_sb_row_worker_hook(void *arg1, void *arg2) {
1080
168k
  AV1CdefSync *const cdef_sync = (AV1CdefSync *)arg1;
1081
168k
  AV1CdefWorkerData *const cdef_worker = (AV1CdefWorkerData *)arg2;
1082
168k
  AV1_COMMON *cm = cdef_worker->cm;
1083
168k
  const int nvfb = (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
1084
1085
168k
#if CONFIG_MULTITHREAD
1086
168k
  pthread_mutex_t *job_mutex_ = cdef_sync->mutex_;
1087
168k
#endif
1088
168k
  struct aom_internal_error_info *const error_info = &cdef_worker->error_info;
1089
1090
  // The jmp_buf is valid only for the duration of the function that calls
1091
  // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
1092
  // before it returns.
1093
168k
  if (setjmp(error_info->jmp)) {
1094
0
    error_info->setjmp = 0;
1095
0
#if CONFIG_MULTITHREAD
1096
0
    pthread_mutex_lock(job_mutex_);
1097
0
    cdef_sync->cdef_mt_exit = true;
1098
0
    pthread_mutex_unlock(job_mutex_);
1099
0
#endif
1100
    // In case of cdef row-multithreading, the worker on a filter block row
1101
    // (fbr) waits for the line buffers (top and bottom) copy of the above row.
1102
    // Hence, in case a thread (main/worker) encounters an error before copying
1103
    // of the line buffers, update that line buffer copy is complete in order to
1104
    // avoid dependent workers waiting indefinitely.
1105
0
    set_cdef_init_fb_row_done(cdef_sync, nvfb);
1106
0
    return 0;
1107
0
  }
1108
168k
  error_info->setjmp = 1;
1109
1110
168k
  volatile int cur_fbr;
1111
168k
  const int num_planes = av1_num_planes(cm);
1112
201k
  while (get_cdef_row_next_job(cdef_sync, &cur_fbr, nvfb)) {
1113
32.9k
    MACROBLOCKD *xd = cdef_worker->xd;
1114
32.9k
    av1_cdef_fb_row(cm, xd, cdef_worker->linebuf, cdef_worker->colbuf,
1115
32.9k
                    cdef_worker->srcbuf, cur_fbr,
1116
32.9k
                    cdef_worker->cdef_init_fb_row_fn, cdef_sync, error_info);
1117
32.9k
    if (cdef_worker->do_extend_border) {
1118
0
      for (int plane = 0; plane < num_planes; ++plane) {
1119
0
        const YV12_BUFFER_CONFIG *ybf = &cm->cur_frame->buf;
1120
0
        const int is_uv = plane > 0;
1121
0
        const int mi_high = MI_SIZE_LOG2 - xd->plane[plane].subsampling_y;
1122
0
        const int unit_height = MI_SIZE_64X64 << mi_high;
1123
0
        const int v_start = cur_fbr * unit_height;
1124
0
        const int v_end =
1125
0
            AOMMIN(v_start + unit_height, ybf->crop_heights[is_uv]);
1126
0
        aom_extend_frame_borders_plane_row(ybf, plane, v_start, v_end);
1127
0
      }
1128
0
    }
1129
32.9k
  }
1130
168k
  error_info->setjmp = 0;
1131
168k
  return 1;
1132
168k
}
1133
1134
// Assigns CDEF hook function and thread data to each worker.
1135
static void prepare_cdef_frame_workers(
1136
    AV1_COMMON *const cm, MACROBLOCKD *xd, AV1CdefWorkerData *const cdef_worker,
1137
    AVxWorkerHook hook, AVxWorker *const workers, AV1CdefSync *const cdef_sync,
1138
    int num_workers, cdef_init_fb_row_t cdef_init_fb_row_fn,
1139
5.29k
    int do_extend_border) {
1140
5.29k
  const int num_planes = av1_num_planes(cm);
1141
1142
5.29k
  cdef_worker[0].srcbuf = cm->cdef_info.srcbuf;
1143
20.5k
  for (int plane = 0; plane < num_planes; plane++)
1144
15.2k
    cdef_worker[0].colbuf[plane] = cm->cdef_info.colbuf[plane];
1145
174k
  for (int i = num_workers - 1; i >= 0; i--) {
1146
169k
    AVxWorker *const worker = &workers[i];
1147
169k
    cdef_worker[i].cm = cm;
1148
169k
    cdef_worker[i].xd = xd;
1149
169k
    cdef_worker[i].cdef_init_fb_row_fn = cdef_init_fb_row_fn;
1150
169k
    cdef_worker[i].do_extend_border = do_extend_border;
1151
657k
    for (int plane = 0; plane < num_planes; plane++)
1152
488k
      cdef_worker[i].linebuf[plane] = cm->cdef_info.linebuf[plane];
1153
1154
169k
    worker->hook = hook;
1155
169k
    worker->data1 = cdef_sync;
1156
169k
    worker->data2 = &cdef_worker[i];
1157
169k
  }
1158
5.29k
}
1159
1160
// Initializes row-level parameters for CDEF frame.
1161
void av1_cdef_init_fb_row_mt(const AV1_COMMON *const cm,
1162
                             const MACROBLOCKD *const xd,
1163
                             CdefBlockInfo *const fb_info,
1164
                             uint16_t **const linebuf, uint16_t *const src,
1165
32.9k
                             struct AV1CdefSyncData *const cdef_sync, int fbr) {
1166
32.9k
  const int num_planes = av1_num_planes(cm);
1167
32.9k
  const int nvfb = (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
1168
32.9k
  const int luma_stride =
1169
32.9k
      ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols << MI_SIZE_LOG2, 4);
1170
1171
  // for the current filter block, it's top left corner mi structure (mi_tl)
1172
  // is first accessed to check whether the top and left boundaries are
1173
  // frame boundaries. Then bottom-left and top-right mi structures are
1174
  // accessed to check whether the bottom and right boundaries
1175
  // (respectively) are frame boundaries.
1176
  //
1177
  // Note that we can't just check the bottom-right mi structure - eg. if
1178
  // we're at the right-hand edge of the frame but not the bottom, then
1179
  // the bottom-right mi is NULL but the bottom-left is not.
1180
32.9k
  fb_info->frame_boundary[TOP] = (MI_SIZE_64X64 * fbr == 0) ? 1 : 0;
1181
32.9k
  if (fbr != nvfb - 1)
1182
27.6k
    fb_info->frame_boundary[BOTTOM] =
1183
27.6k
        (MI_SIZE_64X64 * (fbr + 1) == cm->mi_params.mi_rows) ? 1 : 0;
1184
5.32k
  else
1185
5.32k
    fb_info->frame_boundary[BOTTOM] = 1;
1186
1187
32.9k
  fb_info->src = src;
1188
32.9k
  fb_info->damping = cm->cdef_info.cdef_damping;
1189
32.9k
  fb_info->coeff_shift = AOMMAX(cm->seq_params->bit_depth - 8, 0);
1190
32.9k
  av1_zero(fb_info->dir);
1191
32.9k
  av1_zero(fb_info->var);
1192
1193
116k
  for (int plane = 0; plane < num_planes; plane++) {
1194
83.1k
    const int stride = luma_stride >> xd->plane[plane].subsampling_x;
1195
83.1k
    uint16_t *top_linebuf = &linebuf[plane][0];
1196
83.1k
    uint16_t *bot_linebuf = &linebuf[plane][nvfb * CDEF_VBORDER * stride];
1197
83.1k
    {
1198
83.1k
      const int mi_high_l2 = MI_SIZE_LOG2 - xd->plane[plane].subsampling_y;
1199
83.1k
      const int top_offset = MI_SIZE_64X64 * (fbr + 1) << mi_high_l2;
1200
83.1k
      const int bot_offset = MI_SIZE_64X64 * (fbr + 1) << mi_high_l2;
1201
1202
83.1k
      if (fbr != nvfb - 1)  // if (fbr != 0)  // top line buffer copy
1203
69.1k
        av1_cdef_copy_sb8_16(
1204
69.1k
            cm, &top_linebuf[(fbr + 1) * CDEF_VBORDER * stride], stride,
1205
69.1k
            xd->plane[plane].dst.buf, top_offset - CDEF_VBORDER, 0,
1206
69.1k
            xd->plane[plane].dst.stride, CDEF_VBORDER, stride);
1207
83.1k
      if (fbr != nvfb - 1)  // bottom line buffer copy
1208
67.9k
        av1_cdef_copy_sb8_16(cm, &bot_linebuf[fbr * CDEF_VBORDER * stride],
1209
67.9k
                             stride, xd->plane[plane].dst.buf, bot_offset, 0,
1210
67.9k
                             xd->plane[plane].dst.stride, CDEF_VBORDER, stride);
1211
83.1k
    }
1212
1213
83.1k
    fb_info->top_linebuf[plane] = &linebuf[plane][fbr * CDEF_VBORDER * stride];
1214
83.1k
    fb_info->bot_linebuf[plane] =
1215
83.1k
        &linebuf[plane]
1216
83.1k
                [nvfb * CDEF_VBORDER * stride + (fbr * CDEF_VBORDER * stride)];
1217
83.1k
  }
1218
1219
32.9k
  cdef_row_mt_sync_write(cdef_sync, fbr);
1220
32.9k
  cdef_row_mt_sync_read(cdef_sync, fbr);
1221
32.9k
}
1222
1223
// Implements multi-threading for CDEF.
1224
// Perform CDEF on input frame.
1225
// Inputs:
1226
//   frame: Pointer to input frame buffer.
1227
//   cm: Pointer to common structure.
1228
//   xd: Pointer to common current coding block structure.
1229
// Returns:
1230
//   Nothing will be returned.
1231
void av1_cdef_frame_mt(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1232
                       AV1CdefWorkerData *const cdef_worker,
1233
                       AVxWorker *const workers, AV1CdefSync *const cdef_sync,
1234
                       int num_workers, cdef_init_fb_row_t cdef_init_fb_row_fn,
1235
5.29k
                       int do_extend_border) {
1236
5.29k
  YV12_BUFFER_CONFIG *frame = &cm->cur_frame->buf;
1237
5.29k
  const int num_planes = av1_num_planes(cm);
1238
1239
5.29k
  av1_setup_dst_planes(xd->plane, cm->seq_params->sb_size, frame, 0, 0, 0,
1240
5.29k
                       num_planes);
1241
1242
5.29k
  reset_cdef_job_info(cdef_sync);
1243
5.29k
  prepare_cdef_frame_workers(cm, xd, cdef_worker, cdef_sb_row_worker_hook,
1244
5.29k
                             workers, cdef_sync, num_workers,
1245
5.29k
                             cdef_init_fb_row_fn, do_extend_border);
1246
5.29k
  launch_cdef_workers(workers, num_workers);
1247
5.29k
  sync_cdef_workers(workers, cm, num_workers);
1248
5.29k
}
1249
1250
47.5k
int av1_get_intrabc_extra_top_right_sb_delay(const AV1_COMMON *cm) {
1251
  // No additional top-right delay when intraBC tool is not enabled.
1252
47.5k
  if (!av1_allow_intrabc(cm)) return 0;
1253
  // Due to the hardware constraints on processing the intraBC tool with row
1254
  // multithreading, a top-right delay of 3 superblocks of size 128x128 or 5
1255
  // superblocks of size 64x64 is mandated. However, a minimum top-right delay
1256
  // of 1 superblock is assured with 'sync_range'. Hence return only the
1257
  // additional superblock delay when the intraBC tool is enabled.
1258
3.90k
  return cm->seq_params->sb_size == BLOCK_128X128 ? 2 : 4;
1259
47.5k
}