Coverage Report

Created: 2026-06-30 06:53

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