Coverage Report

Created: 2026-10-27 06:32

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