Coverage Report

Created: 2026-05-16 06:27

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