Coverage Report

Created: 2025-07-16 07:53

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