Coverage Report

Created: 2025-06-13 07:07

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