Coverage Report

Created: 2025-07-23 06:32

/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.21k
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.21k
  if (width < 640)
36
4.02k
    return 1;
37
183
  else if (width <= 1280)
38
36
    return 2;
39
147
  else if (width <= 4096)
40
113
    return 4;
41
34
  else
42
34
    return 8;
43
4.21k
}
44
45
#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
46
1.22k
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.22k
  (void)width;
60
1.22k
  return 1;
61
1.22k
#endif
62
1.22k
}
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.21k
                           int width, int num_workers) {
68
4.21k
  lf_sync->rows = rows;
69
4.21k
#if CONFIG_MULTITHREAD
70
4.21k
  {
71
4.21k
    int i, j;
72
73
16.8k
    for (j = 0; j < MAX_MB_PLANE; j++) {
74
12.6k
      CHECK_MEM_ERROR(cm, lf_sync->mutex_[j],
75
12.6k
                      aom_malloc(sizeof(*(lf_sync->mutex_[j])) * rows));
76
12.6k
      if (lf_sync->mutex_[j]) {
77
49.8k
        for (i = 0; i < rows; ++i) {
78
37.2k
          pthread_mutex_init(&lf_sync->mutex_[j][i], NULL);
79
37.2k
        }
80
12.6k
      }
81
82
12.6k
      CHECK_MEM_ERROR(cm, lf_sync->cond_[j],
83
12.6k
                      aom_malloc(sizeof(*(lf_sync->cond_[j])) * rows));
84
12.6k
      if (lf_sync->cond_[j]) {
85
49.8k
        for (i = 0; i < rows; ++i) {
86
37.2k
          pthread_cond_init(&lf_sync->cond_[j][i], NULL);
87
37.2k
        }
88
12.6k
      }
89
12.6k
    }
90
91
4.21k
    CHECK_MEM_ERROR(cm, lf_sync->job_mutex,
92
4.21k
                    aom_malloc(sizeof(*(lf_sync->job_mutex))));
93
4.21k
    if (lf_sync->job_mutex) {
94
4.21k
      pthread_mutex_init(lf_sync->job_mutex, NULL);
95
4.21k
    }
96
4.21k
  }
97
4.21k
#endif  // CONFIG_MULTITHREAD
98
4.21k
  CHECK_MEM_ERROR(cm, lf_sync->lfdata,
99
4.21k
                  aom_malloc(num_workers * sizeof(*(lf_sync->lfdata))));
100
4.21k
  lf_sync->num_workers = num_workers;
101
102
16.8k
  for (int j = 0; j < MAX_MB_PLANE; j++) {
103
12.6k
    CHECK_MEM_ERROR(cm, lf_sync->cur_sb_col[j],
104
12.6k
                    aom_malloc(sizeof(*(lf_sync->cur_sb_col[j])) * rows));
105
12.6k
  }
106
4.21k
  CHECK_MEM_ERROR(
107
4.21k
      cm, lf_sync->job_queue,
108
4.21k
      aom_malloc(sizeof(*(lf_sync->job_queue)) * rows * MAX_MB_PLANE * 2));
109
  // Set up nsync.
110
4.21k
  lf_sync->sync_range = get_sync_range(width);
111
4.21k
}
112
113
// Deallocate lf synchronization related mutex and data
114
7.88k
void av1_loop_filter_dealloc(AV1LfSync *lf_sync) {
115
7.88k
  if (lf_sync != NULL) {
116
7.88k
    int j;
117
7.88k
#if CONFIG_MULTITHREAD
118
7.88k
    int i;
119
31.5k
    for (j = 0; j < MAX_MB_PLANE; j++) {
120
23.6k
      if (lf_sync->mutex_[j] != NULL) {
121
49.8k
        for (i = 0; i < lf_sync->rows; ++i) {
122
37.2k
          pthread_mutex_destroy(&lf_sync->mutex_[j][i]);
123
37.2k
        }
124
12.6k
        aom_free(lf_sync->mutex_[j]);
125
12.6k
      }
126
23.6k
      if (lf_sync->cond_[j] != NULL) {
127
49.8k
        for (i = 0; i < lf_sync->rows; ++i) {
128
37.2k
          pthread_cond_destroy(&lf_sync->cond_[j][i]);
129
37.2k
        }
130
12.6k
        aom_free(lf_sync->cond_[j]);
131
12.6k
      }
132
23.6k
    }
133
7.88k
    if (lf_sync->job_mutex != NULL) {
134
4.21k
      pthread_mutex_destroy(lf_sync->job_mutex);
135
4.21k
      aom_free(lf_sync->job_mutex);
136
4.21k
    }
137
7.88k
#endif  // CONFIG_MULTITHREAD
138
7.88k
    aom_free(lf_sync->lfdata);
139
31.5k
    for (j = 0; j < MAX_MB_PLANE; j++) {
140
23.6k
      aom_free(lf_sync->cur_sb_col[j]);
141
23.6k
    }
142
143
7.88k
    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
7.88k
    av1_zero(*lf_sync);
147
7.88k
  }
148
7.88k
}
149
150
void av1_alloc_cdef_sync(AV1_COMMON *const cm, AV1CdefSync *cdef_sync,
151
92.7k
                         int num_workers) {
152
92.7k
  if (num_workers < 1) return;
153
59.1k
#if CONFIG_MULTITHREAD
154
59.1k
  if (cdef_sync->mutex_ == NULL) {
155
2.82k
    CHECK_MEM_ERROR(cm, cdef_sync->mutex_,
156
2.82k
                    aom_malloc(sizeof(*(cdef_sync->mutex_))));
157
2.82k
    if (cdef_sync->mutex_) pthread_mutex_init(cdef_sync->mutex_, NULL);
158
2.82k
  }
159
#else
160
  (void)cm;
161
  (void)cdef_sync;
162
#endif  // CONFIG_MULTITHREAD
163
59.1k
}
164
165
17.3k
void av1_free_cdef_sync(AV1CdefSync *cdef_sync) {
166
17.3k
  if (cdef_sync == NULL) return;
167
17.3k
#if CONFIG_MULTITHREAD
168
17.3k
  if (cdef_sync->mutex_ != NULL) {
169
2.82k
    pthread_mutex_destroy(cdef_sync->mutex_);
170
2.82k
    aom_free(cdef_sync->mutex_);
171
2.82k
  }
172
17.3k
#endif  // CONFIG_MULTITHREAD
173
17.3k
}
174
175
static inline void cdef_row_mt_sync_read(AV1CdefSync *const cdef_sync,
176
65.6k
                                         int row) {
177
65.6k
  if (!row) return;
178
48.9k
#if CONFIG_MULTITHREAD
179
48.9k
  AV1CdefRowSync *const cdef_row_mt = cdef_sync->cdef_row_mt;
180
48.9k
  pthread_mutex_lock(cdef_row_mt[row - 1].row_mutex_);
181
58.0k
  while (cdef_row_mt[row - 1].is_row_done != 1)
182
9.07k
    pthread_cond_wait(cdef_row_mt[row - 1].row_cond_,
183
9.07k
                      cdef_row_mt[row - 1].row_mutex_);
184
48.9k
  cdef_row_mt[row - 1].is_row_done = 0;
185
48.9k
  pthread_mutex_unlock(cdef_row_mt[row - 1].row_mutex_);
186
#else
187
  (void)cdef_sync;
188
#endif  // CONFIG_MULTITHREAD
189
48.9k
}
190
191
static inline void cdef_row_mt_sync_write(AV1CdefSync *const cdef_sync,
192
64.5k
                                          int row) {
193
64.5k
#if CONFIG_MULTITHREAD
194
64.5k
  AV1CdefRowSync *const cdef_row_mt = cdef_sync->cdef_row_mt;
195
64.5k
  pthread_mutex_lock(cdef_row_mt[row].row_mutex_);
196
64.5k
  pthread_cond_signal(cdef_row_mt[row].row_cond_);
197
64.5k
  cdef_row_mt[row].is_row_done = 1;
198
64.5k
  pthread_mutex_unlock(cdef_row_mt[row].row_mutex_);
199
#else
200
  (void)cdef_sync;
201
  (void)row;
202
#endif  // CONFIG_MULTITHREAD
203
64.5k
}
204
205
static inline void sync_read(AV1LfSync *const lf_sync, int r, int c,
206
934k
                             int plane) {
207
934k
#if CONFIG_MULTITHREAD
208
934k
  const int nsync = lf_sync->sync_range;
209
210
934k
  if (r && !(c & (nsync - 1))) {
211
503k
    pthread_mutex_t *const mutex = &lf_sync->mutex_[plane][r - 1];
212
503k
    pthread_mutex_lock(mutex);
213
214
633k
    while (c > lf_sync->cur_sb_col[plane][r - 1] - nsync) {
215
129k
      pthread_cond_wait(&lf_sync->cond_[plane][r - 1], mutex);
216
129k
    }
217
503k
    pthread_mutex_unlock(mutex);
218
503k
  }
219
#else
220
  (void)lf_sync;
221
  (void)r;
222
  (void)c;
223
  (void)plane;
224
#endif  // CONFIG_MULTITHREAD
225
934k
}
226
227
static inline void sync_write(AV1LfSync *const lf_sync, int r, int c,
228
462k
                              const int sb_cols, int plane) {
229
462k
#if CONFIG_MULTITHREAD
230
462k
  const int nsync = lf_sync->sync_range;
231
462k
  int cur;
232
  // Only signal when there are enough filtered SB for next row to run.
233
462k
  int sig = 1;
234
235
462k
  if (c < sb_cols - 1) {
236
330k
    cur = c;
237
330k
    if (c % nsync) sig = 0;
238
330k
  } else {
239
132k
    cur = sb_cols + nsync;
240
132k
  }
241
242
462k
  if (sig) {
243
305k
    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
305k
    lf_sync->cur_sb_col[plane][r] = AOMMAX(lf_sync->cur_sb_col[plane][r], cur);
251
252
305k
    pthread_cond_broadcast(&lf_sync->cond_[plane][r]);
253
305k
    pthread_mutex_unlock(&lf_sync->mutex_[plane][r]);
254
305k
  }
255
#else
256
  (void)lf_sync;
257
  (void)r;
258
  (void)c;
259
  (void)sb_cols;
260
  (void)plane;
261
#endif  // CONFIG_MULTITHREAD
262
462k
}
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
347k
    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
347k
  (void)error_info;
275
347k
  const int sb_cols =
276
347k
      CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, MAX_MIB_SIZE_LOG2);
277
347k
  const int r = mi_row >> num_mis_in_lpf_unit_height_log2;
278
347k
  int mi_col, c;
279
280
347k
  const bool joint_filter_chroma = (lpf_opt_level == 2) && plane > AOM_PLANE_Y;
281
347k
  const int num_planes = joint_filter_chroma ? 2 : 1;
282
347k
  assert(IMPLIES(joint_filter_chroma, plane == AOM_PLANE_U));
283
284
347k
  if (dir == 0) {
285
731k
    for (mi_col = 0; mi_col < cm->mi_params.mi_cols; mi_col += MAX_MIB_SIZE) {
286
557k
      c = mi_col >> MAX_MIB_SIZE_LOG2;
287
288
557k
      av1_setup_dst_planes(planes, cm->seq_params->sb_size, frame_buffer,
289
557k
                           mi_row, mi_col, plane, plane + num_planes);
290
557k
      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
557k
      } else {
301
557k
        av1_filter_block_plane_vert(cm, xd, plane, &planes[plane], mi_row,
302
557k
                                    mi_col);
303
557k
      }
304
557k
      if (lf_sync != NULL) {
305
462k
        sync_write(lf_sync, r, c, sb_cols, plane);
306
462k
      }
307
557k
    }
308
173k
  } else if (dir == 1) {
309
733k
    for (mi_col = 0; mi_col < cm->mi_params.mi_cols; mi_col += MAX_MIB_SIZE) {
310
559k
      c = mi_col >> MAX_MIB_SIZE_LOG2;
311
312
559k
      if (lf_sync != NULL) {
313
        // Wait for vertical edge filtering of the top-right block to be
314
        // completed
315
467k
        sync_read(lf_sync, r, c, plane);
316
317
        // Wait for vertical edge filtering of the right block to be completed
318
467k
        sync_read(lf_sync, r + 1, c, plane);
319
467k
      }
320
321
559k
#if CONFIG_MULTITHREAD
322
559k
      if (lf_sync && lf_sync->num_workers > 1) {
323
468k
        pthread_mutex_lock(lf_sync->job_mutex);
324
468k
        const bool lf_mt_exit = lf_sync->lf_mt_exit;
325
468k
        pthread_mutex_unlock(lf_sync->job_mutex);
326
        // Exit in case any worker has encountered an error.
327
468k
        if (lf_mt_exit) return;
328
468k
      }
329
559k
#endif
330
331
559k
      av1_setup_dst_planes(planes, cm->seq_params->sb_size, frame_buffer,
332
559k
                           mi_row, mi_col, plane, plane + num_planes);
333
559k
      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
559k
      } else {
344
559k
        av1_filter_block_plane_horz(cm, xd, plane, &planes[plane], mi_row,
345
559k
                                    mi_col);
346
559k
      }
347
559k
    }
348
173k
  }
349
347k
}
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
23.0k
                                   AV1_COMMON *const cm, int num_workers) {
371
23.0k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
372
23.0k
  int had_error = workers[0].had_error;
373
23.0k
  struct aom_internal_error_info error_info;
374
375
  // Read the error_info of main thread.
376
23.0k
  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
923k
  for (int i = num_workers - 1; i > 0; --i) {
383
900k
    AVxWorker *const worker = &workers[i];
384
900k
    if (!winterface->sync(worker)) {
385
0
      had_error = 1;
386
0
      error_info = ((LFWorkerData *)worker->data2)->error_info;
387
0
    }
388
900k
  }
389
23.0k
  if (had_error) aom_internal_error_copy(cm->error, &error_info);
390
23.0k
}
391
392
// Row-based multi-threaded loopfilter hook
393
922k
static int loop_filter_row_worker(void *arg1, void *arg2) {
394
922k
  AV1LfSync *const lf_sync = (AV1LfSync *)arg1;
395
922k
  LFWorkerData *const lf_data = (LFWorkerData *)arg2;
396
922k
  AV1LfMTInfo *cur_job_info;
397
398
922k
#if CONFIG_MULTITHREAD
399
922k
  pthread_mutex_t *job_mutex_ = lf_sync->job_mutex;
400
922k
#endif
401
402
922k
  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
922k
  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
922k
  error_info->setjmp = 1;
418
419
1.18M
  while ((cur_job_info = get_lf_job_info(lf_sync)) != NULL) {
420
267k
    const int lpf_opt_level = cur_job_info->lpf_opt_level;
421
267k
    av1_thread_loop_filter_rows(
422
267k
        lf_data->frame_buffer, lf_data->cm, lf_data->planes, lf_data->xd,
423
267k
        cur_job_info->mi_row, cur_job_info->plane, cur_job_info->dir,
424
267k
        lpf_opt_level, lf_sync, error_info, lf_data->params_buf,
425
267k
        lf_data->tx_buf, MAX_MIB_SIZE_LOG2);
426
267k
  }
427
922k
  error_info->setjmp = 0;
428
922k
  return 1;
429
922k
}
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
23.0k
                                AV1LfSync *lf_sync, int lpf_opt_level) {
436
23.0k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
437
23.0k
  int i;
438
23.0k
  loop_filter_frame_mt_init(cm, start, stop, planes_to_lf, num_workers, lf_sync,
439
23.0k
                            lpf_opt_level, MAX_MIB_SIZE_LOG2);
440
441
  // Set up loopfilter thread data.
442
946k
  for (i = num_workers - 1; i >= 0; --i) {
443
923k
    AVxWorker *const worker = &workers[i];
444
923k
    LFWorkerData *const lf_data = &lf_sync->lfdata[i];
445
446
923k
    worker->hook = loop_filter_row_worker;
447
923k
    worker->data1 = lf_sync;
448
923k
    worker->data2 = lf_data;
449
450
    // Loopfilter data
451
923k
    loop_filter_data_reset(lf_data, frame, cm, xd);
452
453
    // Start loopfiltering
454
923k
    worker->had_error = 0;
455
923k
    if (i == 0) {
456
23.0k
      winterface->execute(worker);
457
900k
    } else {
458
900k
      winterface->launch(worker);
459
900k
    }
460
923k
  }
461
462
23.0k
  sync_lf_workers(workers, cm, num_workers);
463
23.0k
}
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
9.16k
                             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
9.16k
  int mi_row, plane, dir;
472
473
9.16k
  AV1_DEBLOCKING_PARAMETERS params_buf[MAX_MIB_SIZE];
474
9.16k
  TX_SIZE tx_buf[MAX_MIB_SIZE];
475
26.2k
  for (mi_row = start; mi_row < stop; mi_row += MAX_MIB_SIZE) {
476
68.2k
    for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
477
51.1k
      if (skip_loop_filter_plane(planes_to_lf, plane, lpf_opt_level)) {
478
10.9k
        continue;
479
10.9k
      }
480
481
120k
      for (dir = 0; dir < 2; ++dir) {
482
80.4k
        av1_thread_loop_filter_rows(frame, cm, xd->plane, xd, mi_row, plane,
483
80.4k
                                    dir, lpf_opt_level, /*lf_sync=*/NULL,
484
80.4k
                                    xd->error_info, params_buf, tx_buf,
485
80.4k
                                    MAX_MIB_SIZE_LOG2);
486
80.4k
      }
487
40.2k
    }
488
17.0k
  }
489
9.16k
}
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
32.2k
                              int lpf_opt_level) {
496
32.2k
  int start_mi_row, end_mi_row, mi_rows_to_filter;
497
32.2k
  int planes_to_lf[MAX_MB_PLANE];
498
499
32.2k
  if (!check_planes_to_loop_filter(&cm->lf, planes_to_lf, plane_start,
500
32.2k
                                   plane_end))
501
0
    return;
502
503
32.2k
  start_mi_row = 0;
504
32.2k
  mi_rows_to_filter = cm->mi_params.mi_rows;
505
32.2k
  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
32.2k
  end_mi_row = start_mi_row + mi_rows_to_filter;
511
32.2k
  av1_loop_filter_frame_init(cm, plane_start, plane_end);
512
513
32.2k
  if (num_workers > 1) {
514
    // Enqueue and execute loopfiltering jobs.
515
23.0k
    loop_filter_rows_mt(frame, cm, xd, start_mi_row, end_mi_row, planes_to_lf,
516
23.0k
                        workers, num_workers, lf_sync, lpf_opt_level);
517
23.0k
  } else {
518
    // Directly filter in the main thread.
519
9.16k
    loop_filter_rows(frame, cm, xd, start_mi_row, end_mi_row, planes_to_lf,
520
9.16k
                     lpf_opt_level);
521
9.16k
  }
522
32.2k
}
523
524
#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
525
40.5k
static inline void lr_sync_read(void *const lr_sync, int r, int c, int plane) {
526
40.5k
#if CONFIG_MULTITHREAD
527
40.5k
  AV1LrSync *const loop_res_sync = (AV1LrSync *)lr_sync;
528
40.5k
  const int nsync = loop_res_sync->sync_range;
529
530
40.5k
  if (r && !(c & (nsync - 1))) {
531
40.5k
    pthread_mutex_t *const mutex = &loop_res_sync->mutex_[plane][r - 1];
532
40.5k
    pthread_mutex_lock(mutex);
533
534
55.8k
    while (c > loop_res_sync->cur_sb_col[plane][r - 1] - nsync) {
535
15.3k
      pthread_cond_wait(&loop_res_sync->cond_[plane][r - 1], mutex);
536
15.3k
    }
537
40.5k
    pthread_mutex_unlock(mutex);
538
40.5k
  }
539
#else
540
  (void)lr_sync;
541
  (void)r;
542
  (void)c;
543
  (void)plane;
544
#endif  // CONFIG_MULTITHREAD
545
40.5k
}
546
547
static inline void lr_sync_write(void *const lr_sync, int r, int c,
548
59.9k
                                 const int sb_cols, int plane) {
549
59.9k
#if CONFIG_MULTITHREAD
550
59.9k
  AV1LrSync *const loop_res_sync = (AV1LrSync *)lr_sync;
551
59.9k
  const int nsync = loop_res_sync->sync_range;
552
59.9k
  int cur;
553
  // Only signal when there are enough filtered SB for next row to run.
554
59.9k
  int sig = 1;
555
556
59.9k
  if (c < sb_cols - 1) {
557
24.2k
    cur = c;
558
24.2k
    if (c % nsync) sig = 0;
559
35.6k
  } else {
560
35.6k
    cur = sb_cols + nsync;
561
35.6k
  }
562
563
59.9k
  if (sig) {
564
59.9k
    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
59.9k
    loop_res_sync->cur_sb_col[plane][r] =
572
59.9k
        AOMMAX(loop_res_sync->cur_sb_col[plane][r], cur);
573
574
59.9k
    pthread_cond_broadcast(&loop_res_sync->cond_[plane][r]);
575
59.9k
    pthread_mutex_unlock(&loop_res_sync->mutex_[plane][r]);
576
59.9k
  }
577
#else
578
  (void)lr_sync;
579
  (void)r;
580
  (void)c;
581
  (void)sb_cols;
582
  (void)plane;
583
#endif  // CONFIG_MULTITHREAD
584
59.9k
}
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.22k
                                int num_planes, int width) {
590
1.22k
  lr_sync->rows = num_rows_lr;
591
1.22k
  lr_sync->num_planes = num_planes;
592
1.22k
#if CONFIG_MULTITHREAD
593
1.22k
  {
594
1.22k
    int i, j;
595
596
4.77k
    for (j = 0; j < num_planes; j++) {
597
3.55k
      CHECK_MEM_ERROR(cm, lr_sync->mutex_[j],
598
3.55k
                      aom_malloc(sizeof(*(lr_sync->mutex_[j])) * num_rows_lr));
599
3.55k
      if (lr_sync->mutex_[j]) {
600
13.3k
        for (i = 0; i < num_rows_lr; ++i) {
601
9.78k
          pthread_mutex_init(&lr_sync->mutex_[j][i], NULL);
602
9.78k
        }
603
3.55k
      }
604
605
3.55k
      CHECK_MEM_ERROR(cm, lr_sync->cond_[j],
606
3.55k
                      aom_malloc(sizeof(*(lr_sync->cond_[j])) * num_rows_lr));
607
3.55k
      if (lr_sync->cond_[j]) {
608
13.3k
        for (i = 0; i < num_rows_lr; ++i) {
609
9.78k
          pthread_cond_init(&lr_sync->cond_[j][i], NULL);
610
9.78k
        }
611
3.55k
      }
612
3.55k
    }
613
614
1.22k
    CHECK_MEM_ERROR(cm, lr_sync->job_mutex,
615
1.22k
                    aom_malloc(sizeof(*(lr_sync->job_mutex))));
616
1.22k
    if (lr_sync->job_mutex) {
617
1.22k
      pthread_mutex_init(lr_sync->job_mutex, NULL);
618
1.22k
    }
619
1.22k
  }
620
1.22k
#endif  // CONFIG_MULTITHREAD
621
1.22k
  CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata,
622
1.22k
                  aom_calloc(num_workers, sizeof(*(lr_sync->lrworkerdata))));
623
1.22k
  lr_sync->num_workers = num_workers;
624
625
39.5k
  for (int worker_idx = 0; worker_idx < num_workers; ++worker_idx) {
626
38.3k
    if (worker_idx < num_workers - 1) {
627
37.1k
      CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata[worker_idx].rst_tmpbuf,
628
37.1k
                      (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE));
629
37.1k
      CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata[worker_idx].rlbs,
630
37.1k
                      aom_malloc(sizeof(RestorationLineBuffers)));
631
632
37.1k
    } else {
633
1.22k
      lr_sync->lrworkerdata[worker_idx].rst_tmpbuf = cm->rst_tmpbuf;
634
1.22k
      lr_sync->lrworkerdata[worker_idx].rlbs = cm->rlbs;
635
1.22k
    }
636
38.3k
  }
637
638
4.77k
  for (int j = 0; j < num_planes; j++) {
639
3.55k
    CHECK_MEM_ERROR(
640
3.55k
        cm, lr_sync->cur_sb_col[j],
641
3.55k
        aom_malloc(sizeof(*(lr_sync->cur_sb_col[j])) * num_rows_lr));
642
3.55k
  }
643
1.22k
  CHECK_MEM_ERROR(
644
1.22k
      cm, lr_sync->job_queue,
645
1.22k
      aom_malloc(sizeof(*(lr_sync->job_queue)) * num_rows_lr * num_planes));
646
  // Set up nsync.
647
1.22k
  lr_sync->sync_range = get_lr_sync_range(width);
648
1.22k
}
649
650
// Deallocate loop restoration synchronization related mutex and data
651
4.89k
void av1_loop_restoration_dealloc(AV1LrSync *lr_sync) {
652
4.89k
  if (lr_sync != NULL) {
653
4.89k
    int j;
654
4.89k
#if CONFIG_MULTITHREAD
655
4.89k
    int i;
656
19.5k
    for (j = 0; j < MAX_MB_PLANE; j++) {
657
14.6k
      if (lr_sync->mutex_[j] != NULL) {
658
13.3k
        for (i = 0; i < lr_sync->rows; ++i) {
659
9.78k
          pthread_mutex_destroy(&lr_sync->mutex_[j][i]);
660
9.78k
        }
661
3.55k
        aom_free(lr_sync->mutex_[j]);
662
3.55k
      }
663
14.6k
      if (lr_sync->cond_[j] != NULL) {
664
13.3k
        for (i = 0; i < lr_sync->rows; ++i) {
665
9.78k
          pthread_cond_destroy(&lr_sync->cond_[j][i]);
666
9.78k
        }
667
3.55k
        aom_free(lr_sync->cond_[j]);
668
3.55k
      }
669
14.6k
    }
670
4.89k
    if (lr_sync->job_mutex != NULL) {
671
1.22k
      pthread_mutex_destroy(lr_sync->job_mutex);
672
1.22k
      aom_free(lr_sync->job_mutex);
673
1.22k
    }
674
4.89k
#endif  // CONFIG_MULTITHREAD
675
19.5k
    for (j = 0; j < MAX_MB_PLANE; j++) {
676
14.6k
      aom_free(lr_sync->cur_sb_col[j]);
677
14.6k
    }
678
679
4.89k
    aom_free(lr_sync->job_queue);
680
681
4.89k
    if (lr_sync->lrworkerdata) {
682
38.3k
      for (int worker_idx = 0; worker_idx < lr_sync->num_workers - 1;
683
37.1k
           worker_idx++) {
684
37.1k
        LRWorkerData *const workerdata_data =
685
37.1k
            lr_sync->lrworkerdata + worker_idx;
686
687
37.1k
        aom_free(workerdata_data->rst_tmpbuf);
688
37.1k
        aom_free(workerdata_data->rlbs);
689
37.1k
      }
690
1.22k
      aom_free(lr_sync->lrworkerdata);
691
1.22k
    }
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
4.89k
    av1_zero(*lr_sync);
696
4.89k
  }
697
4.89k
}
698
699
static void enqueue_lr_jobs(AV1LrSync *lr_sync, AV1LrStruct *lr_ctxt,
700
15.1k
                            AV1_COMMON *cm) {
701
15.1k
  FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
702
703
15.1k
  const int num_planes = av1_num_planes(cm);
704
15.1k
  AV1LrMTInfo *lr_job_queue = lr_sync->job_queue;
705
15.1k
  int32_t lr_job_counter[2], num_even_lr_jobs = 0;
706
15.1k
  lr_sync->jobs_enqueued = 0;
707
15.1k
  lr_sync->jobs_dequeued = 0;
708
709
59.0k
  for (int plane = 0; plane < num_planes; plane++) {
710
43.8k
    if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
711
34.1k
    num_even_lr_jobs =
712
34.1k
        num_even_lr_jobs + ((ctxt[plane].rsi->vert_units + 1) >> 1);
713
34.1k
  }
714
15.1k
  lr_job_counter[0] = 0;
715
15.1k
  lr_job_counter[1] = num_even_lr_jobs;
716
717
59.0k
  for (int plane = 0; plane < num_planes; plane++) {
718
43.8k
    if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
719
34.1k
    const int is_uv = plane > 0;
720
34.1k
    const int ss_y = is_uv && cm->seq_params->subsampling_y;
721
34.1k
    const int unit_size = ctxt[plane].rsi->restoration_unit_size;
722
34.1k
    const int plane_h = ctxt[plane].plane_h;
723
34.1k
    const int ext_size = unit_size * 3 / 2;
724
725
34.1k
    int y0 = 0, i = 0;
726
82.7k
    while (y0 < plane_h) {
727
48.6k
      int remaining_h = plane_h - y0;
728
48.6k
      int h = (remaining_h < ext_size) ? remaining_h : unit_size;
729
730
48.6k
      RestorationTileLimits limits;
731
48.6k
      limits.v_start = y0;
732
48.6k
      limits.v_end = y0 + h;
733
48.6k
      assert(limits.v_end <= plane_h);
734
      // Offset upwards to align with the restoration processing stripe
735
48.6k
      const int voffset = RESTORATION_UNIT_OFFSET >> ss_y;
736
48.6k
      limits.v_start = AOMMAX(0, limits.v_start - voffset);
737
48.6k
      if (limits.v_end < plane_h) limits.v_end -= voffset;
738
739
48.6k
      assert(lr_job_counter[0] <= num_even_lr_jobs);
740
741
48.6k
      lr_job_queue[lr_job_counter[i & 1]].lr_unit_row = i;
742
48.6k
      lr_job_queue[lr_job_counter[i & 1]].plane = plane;
743
48.6k
      lr_job_queue[lr_job_counter[i & 1]].v_start = limits.v_start;
744
48.6k
      lr_job_queue[lr_job_counter[i & 1]].v_end = limits.v_end;
745
48.6k
      lr_job_queue[lr_job_counter[i & 1]].sync_mode = i & 1;
746
48.6k
      if ((i & 1) == 0) {
747
35.6k
        lr_job_queue[lr_job_counter[i & 1]].v_copy_start =
748
35.6k
            limits.v_start + RESTORATION_BORDER;
749
35.6k
        lr_job_queue[lr_job_counter[i & 1]].v_copy_end =
750
35.6k
            limits.v_end - RESTORATION_BORDER;
751
35.6k
        if (i == 0) {
752
34.1k
          assert(limits.v_start == 0);
753
34.1k
          lr_job_queue[lr_job_counter[i & 1]].v_copy_start = 0;
754
34.1k
        }
755
35.6k
        if (i == (ctxt[plane].rsi->vert_units - 1)) {
756
22.7k
          assert(limits.v_end == plane_h);
757
22.7k
          lr_job_queue[lr_job_counter[i & 1]].v_copy_end = plane_h;
758
22.7k
        }
759
35.6k
      } else {
760
12.9k
        lr_job_queue[lr_job_counter[i & 1]].v_copy_start =
761
12.9k
            AOMMAX(limits.v_start - RESTORATION_BORDER, 0);
762
12.9k
        lr_job_queue[lr_job_counter[i & 1]].v_copy_end =
763
12.9k
            AOMMIN(limits.v_end + RESTORATION_BORDER, plane_h);
764
12.9k
      }
765
48.6k
      lr_job_counter[i & 1]++;
766
48.6k
      lr_sync->jobs_enqueued++;
767
768
48.6k
      y0 += h;
769
48.6k
      ++i;
770
48.6k
    }
771
34.1k
  }
772
15.1k
}
773
774
630k
static AV1LrMTInfo *get_lr_job_info(AV1LrSync *lr_sync) {
775
630k
  AV1LrMTInfo *cur_job_info = NULL;
776
777
630k
#if CONFIG_MULTITHREAD
778
630k
  pthread_mutex_lock(lr_sync->job_mutex);
779
780
632k
  if (!lr_sync->lr_mt_exit && lr_sync->jobs_dequeued < lr_sync->jobs_enqueued) {
781
48.6k
    cur_job_info = lr_sync->job_queue + lr_sync->jobs_dequeued;
782
48.6k
    lr_sync->jobs_dequeued++;
783
48.6k
  }
784
785
630k
  pthread_mutex_unlock(lr_sync->job_mutex);
786
#else
787
  (void)lr_sync;
788
#endif
789
790
630k
  return cur_job_info;
791
630k
}
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
583k
static int loop_restoration_row_worker(void *arg1, void *arg2) {
815
583k
  AV1LrSync *const lr_sync = (AV1LrSync *)arg1;
816
583k
  LRWorkerData *lrworkerdata = (LRWorkerData *)arg2;
817
583k
  AV1LrStruct *lr_ctxt = (AV1LrStruct *)lrworkerdata->lr_ctxt;
818
583k
  FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
819
583k
  int lr_unit_row;
820
583k
  int plane;
821
583k
  int plane_w;
822
583k
#if CONFIG_MULTITHREAD
823
583k
  pthread_mutex_t *job_mutex_ = lr_sync->job_mutex;
824
583k
#endif
825
583k
  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
583k
  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
583k
  error_info->setjmp = 1;
846
847
583k
  typedef void (*copy_fun)(const YV12_BUFFER_CONFIG *src_ybc,
848
583k
                           YV12_BUFFER_CONFIG *dst_ybc, int hstart, int hend,
849
583k
                           int vstart, int vend);
850
583k
  static const copy_fun copy_funs[MAX_MB_PLANE] = {
851
583k
    aom_yv12_partial_coloc_copy_y, aom_yv12_partial_coloc_copy_u,
852
583k
    aom_yv12_partial_coloc_copy_v
853
583k
  };
854
855
631k
  while (1) {
856
630k
    AV1LrMTInfo *cur_job_info = get_lr_job_info(lr_sync);
857
630k
    if (cur_job_info != NULL) {
858
48.6k
      RestorationTileLimits limits;
859
48.6k
      sync_read_fn_t on_sync_read;
860
48.6k
      sync_write_fn_t on_sync_write;
861
48.6k
      limits.v_start = cur_job_info->v_start;
862
48.6k
      limits.v_end = cur_job_info->v_end;
863
48.6k
      lr_unit_row = cur_job_info->lr_unit_row;
864
48.6k
      plane = cur_job_info->plane;
865
48.6k
      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
48.6k
      on_sync_read =
870
48.6k
          cur_job_info->sync_mode == 1 ? lr_sync_read : av1_lr_sync_read_dummy;
871
48.6k
      on_sync_write = cur_job_info->sync_mode == 0 ? lr_sync_write
872
48.6k
                                                   : av1_lr_sync_write_dummy;
873
874
48.6k
      av1_foreach_rest_unit_in_row(
875
48.6k
          &limits, plane_w, lr_ctxt->on_rest_unit, lr_unit_row,
876
48.6k
          ctxt[plane].rsi->restoration_unit_size, ctxt[plane].rsi->horz_units,
877
48.6k
          ctxt[plane].rsi->vert_units, plane, &ctxt[plane],
878
48.6k
          lrworkerdata->rst_tmpbuf, lrworkerdata->rlbs, on_sync_read,
879
48.6k
          on_sync_write, lr_sync, error_info);
880
881
48.6k
      copy_funs[plane](lr_ctxt->dst, lr_ctxt->frame, 0, plane_w,
882
48.6k
                       cur_job_info->v_copy_start, cur_job_info->v_copy_end);
883
884
48.6k
      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
582k
    } else {
890
582k
      break;
891
582k
    }
892
630k
  }
893
583k
  error_info->setjmp = 0;
894
583k
  return 1;
895
583k
}
896
897
static inline void sync_lr_workers(AVxWorker *const workers,
898
15.1k
                                   AV1_COMMON *const cm, int num_workers) {
899
15.1k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
900
15.1k
  int had_error = workers[0].had_error;
901
15.1k
  struct aom_internal_error_info error_info;
902
903
  // Read the error_info of main thread.
904
15.1k
  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
584k
  for (int i = num_workers - 1; i > 0; --i) {
911
568k
    AVxWorker *const worker = &workers[i];
912
568k
    if (!winterface->sync(worker)) {
913
0
      had_error = 1;
914
0
      error_info = ((LRWorkerData *)worker->data2)->error_info;
915
0
    }
916
568k
  }
917
15.1k
  if (had_error) aom_internal_error_copy(cm->error, &error_info);
918
15.1k
}
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
15.1k
                                           int do_extend_border) {
924
15.1k
  FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
925
926
15.1k
  const int num_planes = av1_num_planes(cm);
927
928
15.1k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
929
15.1k
  int num_rows_lr = 0;
930
931
59.0k
  for (int plane = 0; plane < num_planes; plane++) {
932
43.8k
    if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
933
934
34.1k
    const int plane_h = ctxt[plane].plane_h;
935
34.1k
    const int unit_size = cm->rst_info[plane].restoration_unit_size;
936
937
34.1k
    num_rows_lr = AOMMAX(num_rows_lr, av1_lr_count_units(unit_size, plane_h));
938
34.1k
  }
939
940
15.1k
  int i;
941
15.1k
  assert(MAX_MB_PLANE == 3);
942
943
15.1k
  if (!lr_sync->sync_range || num_rows_lr > lr_sync->rows ||
944
15.1k
      num_workers > lr_sync->num_workers || num_planes > lr_sync->num_planes) {
945
1.22k
    av1_loop_restoration_dealloc(lr_sync);
946
1.22k
    av1_loop_restoration_alloc(lr_sync, cm, num_workers, num_rows_lr,
947
1.22k
                               num_planes, cm->width);
948
1.22k
  }
949
15.1k
  lr_sync->lr_mt_exit = false;
950
951
  // Initialize cur_sb_col to -1 for all SB rows.
952
59.0k
  for (i = 0; i < num_planes; i++) {
953
43.8k
    memset(lr_sync->cur_sb_col[i], -1,
954
43.8k
           sizeof(*(lr_sync->cur_sb_col[i])) * num_rows_lr);
955
43.8k
  }
956
957
15.1k
  enqueue_lr_jobs(lr_sync, lr_ctxt, cm);
958
959
  // Set up looprestoration thread data.
960
599k
  for (i = num_workers - 1; i >= 0; --i) {
961
584k
    AVxWorker *const worker = &workers[i];
962
584k
    lr_sync->lrworkerdata[i].lr_ctxt = (void *)lr_ctxt;
963
584k
    lr_sync->lrworkerdata[i].do_extend_border = do_extend_border;
964
584k
    worker->hook = loop_restoration_row_worker;
965
584k
    worker->data1 = lr_sync;
966
584k
    worker->data2 = &lr_sync->lrworkerdata[i];
967
968
    // Start loop restoration
969
584k
    worker->had_error = 0;
970
584k
    if (i == 0) {
971
15.1k
      winterface->execute(worker);
972
568k
    } else {
973
568k
      winterface->launch(worker);
974
568k
    }
975
584k
  }
976
977
15.1k
  sync_lr_workers(workers, cm, num_workers);
978
15.1k
}
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
15.1k
                                          int do_extend_border) {
985
15.1k
  assert(!cm->features.all_lossless);
986
987
15.1k
  const int num_planes = av1_num_planes(cm);
988
989
15.1k
  AV1LrStruct *loop_rest_ctxt = (AV1LrStruct *)lr_ctxt;
990
991
15.1k
  av1_loop_restoration_filter_frame_init(loop_rest_ctxt, frame, cm,
992
15.1k
                                         optimized_lr, num_planes);
993
994
15.1k
  foreach_rest_unit_in_planes_mt(loop_rest_ctxt, workers, num_workers, lr_sync,
995
15.1k
                                 cm, do_extend_border);
996
15.1k
}
997
#endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
998
999
// Initializes cdef_sync parameters.
1000
16.7k
static inline void reset_cdef_job_info(AV1CdefSync *const cdef_sync) {
1001
16.7k
  cdef_sync->end_of_frame = 0;
1002
16.7k
  cdef_sync->fbr = 0;
1003
16.7k
  cdef_sync->fbc = 0;
1004
16.7k
  cdef_sync->cdef_mt_exit = false;
1005
16.7k
}
1006
1007
static inline void launch_cdef_workers(AVxWorker *const workers,
1008
16.7k
                                       int num_workers) {
1009
16.7k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
1010
631k
  for (int i = num_workers - 1; i >= 0; i--) {
1011
615k
    AVxWorker *const worker = &workers[i];
1012
615k
    worker->had_error = 0;
1013
615k
    if (i == 0)
1014
16.7k
      winterface->execute(worker);
1015
598k
    else
1016
598k
      winterface->launch(worker);
1017
615k
  }
1018
16.7k
}
1019
1020
static inline void sync_cdef_workers(AVxWorker *const workers,
1021
16.7k
                                     AV1_COMMON *const cm, int num_workers) {
1022
16.7k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
1023
16.7k
  int had_error = workers[0].had_error;
1024
16.7k
  struct aom_internal_error_info error_info;
1025
1026
  // Read the error_info of main thread.
1027
16.7k
  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
615k
  for (int i = num_workers - 1; i > 0; --i) {
1034
598k
    AVxWorker *const worker = &workers[i];
1035
598k
    if (!winterface->sync(worker)) {
1036
0
      had_error = 1;
1037
0
      error_info = ((AV1CdefWorkerData *)worker->data2)->error_info;
1038
0
    }
1039
598k
  }
1040
16.7k
  if (had_error) aom_internal_error_copy(cm->error, &error_info);
1041
16.7k
}
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
65.6k
                                          const int nvfb) {
1047
65.6k
  cdef_sync->fbr++;
1048
65.6k
  if (cdef_sync->fbr == nvfb) {
1049
16.7k
    cdef_sync->end_of_frame = 1;
1050
16.7k
  }
1051
65.6k
}
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
676k
                                        volatile int *cur_fbr, const int nvfb) {
1057
676k
#if CONFIG_MULTITHREAD
1058
676k
  pthread_mutex_lock(cdef_sync->mutex_);
1059
676k
#endif  // CONFIG_MULTITHREAD
1060
676k
  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
680k
  if (!cdef_sync->cdef_mt_exit && cdef_sync->end_of_frame == 0) {
1064
65.6k
    do_next_row = 1;
1065
65.6k
    *cur_fbr = cdef_sync->fbr;
1066
65.6k
    update_cdef_row_next_job_info(cdef_sync, nvfb);
1067
65.6k
  }
1068
676k
#if CONFIG_MULTITHREAD
1069
676k
  pthread_mutex_unlock(cdef_sync->mutex_);
1070
676k
#endif  // CONFIG_MULTITHREAD
1071
676k
  return do_next_row;
1072
676k
}
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
614k
static int cdef_sb_row_worker_hook(void *arg1, void *arg2) {
1080
614k
  AV1CdefSync *const cdef_sync = (AV1CdefSync *)arg1;
1081
614k
  AV1CdefWorkerData *const cdef_worker = (AV1CdefWorkerData *)arg2;
1082
614k
  AV1_COMMON *cm = cdef_worker->cm;
1083
614k
  const int nvfb = (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
1084
1085
614k
#if CONFIG_MULTITHREAD
1086
614k
  pthread_mutex_t *job_mutex_ = cdef_sync->mutex_;
1087
614k
#endif
1088
614k
  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
614k
  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
614k
  error_info->setjmp = 1;
1109
1110
614k
  volatile int cur_fbr;
1111
614k
  const int num_planes = av1_num_planes(cm);
1112
680k
  while (get_cdef_row_next_job(cdef_sync, &cur_fbr, nvfb)) {
1113
65.6k
    MACROBLOCKD *xd = cdef_worker->xd;
1114
65.6k
    av1_cdef_fb_row(cm, xd, cdef_worker->linebuf, cdef_worker->colbuf,
1115
65.6k
                    cdef_worker->srcbuf, cur_fbr,
1116
65.6k
                    cdef_worker->cdef_init_fb_row_fn, cdef_sync, error_info);
1117
65.6k
    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
65.6k
  }
1130
614k
  error_info->setjmp = 0;
1131
614k
  return 1;
1132
614k
}
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
16.7k
    int do_extend_border) {
1140
16.7k
  const int num_planes = av1_num_planes(cm);
1141
1142
16.7k
  cdef_worker[0].srcbuf = cm->cdef_info.srcbuf;
1143
65.1k
  for (int plane = 0; plane < num_planes; plane++)
1144
48.4k
    cdef_worker[0].colbuf[plane] = cm->cdef_info.colbuf[plane];
1145
631k
  for (int i = num_workers - 1; i >= 0; i--) {
1146
615k
    AVxWorker *const worker = &workers[i];
1147
615k
    cdef_worker[i].cm = cm;
1148
615k
    cdef_worker[i].xd = xd;
1149
615k
    cdef_worker[i].cdef_init_fb_row_fn = cdef_init_fb_row_fn;
1150
615k
    cdef_worker[i].do_extend_border = do_extend_border;
1151
2.44M
    for (int plane = 0; plane < num_planes; plane++)
1152
1.83M
      cdef_worker[i].linebuf[plane] = cm->cdef_info.linebuf[plane];
1153
1154
615k
    worker->hook = hook;
1155
615k
    worker->data1 = cdef_sync;
1156
615k
    worker->data2 = &cdef_worker[i];
1157
615k
  }
1158
16.7k
}
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
65.6k
                             struct AV1CdefSyncData *const cdef_sync, int fbr) {
1166
65.6k
  const int num_planes = av1_num_planes(cm);
1167
65.6k
  const int nvfb = (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
1168
65.6k
  const int luma_stride =
1169
65.6k
      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
65.6k
  fb_info->frame_boundary[TOP] = (MI_SIZE_64X64 * fbr == 0) ? 1 : 0;
1181
65.6k
  if (fbr != nvfb - 1)
1182
48.8k
    fb_info->frame_boundary[BOTTOM] =
1183
48.8k
        (MI_SIZE_64X64 * (fbr + 1) == cm->mi_params.mi_rows) ? 1 : 0;
1184
16.8k
  else
1185
16.8k
    fb_info->frame_boundary[BOTTOM] = 1;
1186
1187
65.6k
  fb_info->src = src;
1188
65.6k
  fb_info->damping = cm->cdef_info.cdef_damping;
1189
65.6k
  fb_info->coeff_shift = AOMMAX(cm->seq_params->bit_depth - 8, 0);
1190
65.6k
  av1_zero(fb_info->dir);
1191
65.6k
  av1_zero(fb_info->var);
1192
1193
251k
  for (int plane = 0; plane < num_planes; plane++) {
1194
185k
    const int stride = luma_stride >> xd->plane[plane].subsampling_x;
1195
185k
    uint16_t *top_linebuf = &linebuf[plane][0];
1196
185k
    uint16_t *bot_linebuf = &linebuf[plane][nvfb * CDEF_VBORDER * stride];
1197
185k
    {
1198
185k
      const int mi_high_l2 = MI_SIZE_LOG2 - xd->plane[plane].subsampling_y;
1199
185k
      const int top_offset = MI_SIZE_64X64 * (fbr + 1) << mi_high_l2;
1200
185k
      const int bot_offset = MI_SIZE_64X64 * (fbr + 1) << mi_high_l2;
1201
1202
185k
      if (fbr != nvfb - 1)  // if (fbr != 0)  // top line buffer copy
1203
139k
        av1_cdef_copy_sb8_16(
1204
139k
            cm, &top_linebuf[(fbr + 1) * CDEF_VBORDER * stride], stride,
1205
139k
            xd->plane[plane].dst.buf, top_offset - CDEF_VBORDER, 0,
1206
139k
            xd->plane[plane].dst.stride, CDEF_VBORDER, stride);
1207
185k
      if (fbr != nvfb - 1)  // bottom line buffer copy
1208
137k
        av1_cdef_copy_sb8_16(cm, &bot_linebuf[fbr * CDEF_VBORDER * stride],
1209
137k
                             stride, xd->plane[plane].dst.buf, bot_offset, 0,
1210
137k
                             xd->plane[plane].dst.stride, CDEF_VBORDER, stride);
1211
185k
    }
1212
1213
185k
    fb_info->top_linebuf[plane] = &linebuf[plane][fbr * CDEF_VBORDER * stride];
1214
185k
    fb_info->bot_linebuf[plane] =
1215
185k
        &linebuf[plane]
1216
185k
                [nvfb * CDEF_VBORDER * stride + (fbr * CDEF_VBORDER * stride)];
1217
185k
  }
1218
1219
65.6k
  cdef_row_mt_sync_write(cdef_sync, fbr);
1220
65.6k
  cdef_row_mt_sync_read(cdef_sync, fbr);
1221
65.6k
}
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
16.7k
                       int do_extend_border) {
1236
16.7k
  YV12_BUFFER_CONFIG *frame = &cm->cur_frame->buf;
1237
16.7k
  const int num_planes = av1_num_planes(cm);
1238
1239
16.7k
  av1_setup_dst_planes(xd->plane, cm->seq_params->sb_size, frame, 0, 0, 0,
1240
16.7k
                       num_planes);
1241
1242
16.7k
  reset_cdef_job_info(cdef_sync);
1243
16.7k
  prepare_cdef_frame_workers(cm, xd, cdef_worker, cdef_sb_row_worker_hook,
1244
16.7k
                             workers, cdef_sync, num_workers,
1245
16.7k
                             cdef_init_fb_row_fn, do_extend_border);
1246
16.7k
  launch_cdef_workers(workers, num_workers);
1247
16.7k
  sync_cdef_workers(workers, cm, num_workers);
1248
16.7k
}
1249
1250
92.1k
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
92.1k
  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
6.18k
  return cm->seq_params->sb_size == BLOCK_128X128 ? 2 : 4;
1259
92.1k
}