Coverage Report

Created: 2026-07-16 06:21

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