Coverage Report

Created: 2022-08-24 06:15

/src/aom/av1/common/thread_common.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4
 * This source code is subject to the terms of the BSD 2 Clause License and
5
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6
 * was not distributed with this source code in the LICENSE file, you can
7
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8
 * Media Patent License 1.0 was not distributed with this source code in the
9
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 */
11
12
#include "config/aom_config.h"
13
#include "config/aom_scale_rtcd.h"
14
15
#include "aom_dsp/aom_dsp_common.h"
16
#include "aom_mem/aom_mem.h"
17
#include "av1/common/av1_loopfilter.h"
18
#include "av1/common/entropymode.h"
19
#include "av1/common/thread_common.h"
20
#include "av1/common/reconinter.h"
21
22
// Set up nsync by width.
23
672
static INLINE int get_sync_range(int width) {
24
  // nsync numbers are picked by testing. For example, for 4k
25
  // video, using 4 gives best performance.
26
672
  if (width < 640)
27
672
    return 1;
28
0
  else if (width <= 1280)
29
0
    return 2;
30
0
  else if (width <= 4096)
31
0
    return 4;
32
0
  else
33
0
    return 8;
34
672
}
35
36
#if !CONFIG_REALTIME_ONLY
37
0
static INLINE int get_lr_sync_range(int width) {
38
#if 0
39
  // nsync numbers are picked by testing. For example, for 4k
40
  // video, using 4 gives best performance.
41
  if (width < 640)
42
    return 1;
43
  else if (width <= 1280)
44
    return 2;
45
  else if (width <= 4096)
46
    return 4;
47
  else
48
    return 8;
49
#else
50
0
  (void)width;
51
0
  return 1;
52
0
#endif
53
0
}
54
#endif
55
56
// Allocate memory for lf row synchronization
57
void av1_loop_filter_alloc(AV1LfSync *lf_sync, AV1_COMMON *cm, int rows,
58
672
                           int width, int num_workers) {
59
672
  lf_sync->rows = rows;
60
672
#if CONFIG_MULTITHREAD
61
672
  {
62
672
    int i, j;
63
64
2.68k
    for (j = 0; j < MAX_MB_PLANE; j++) {
65
2.01k
      CHECK_MEM_ERROR(cm, lf_sync->mutex_[j],
66
2.01k
                      aom_malloc(sizeof(*(lf_sync->mutex_[j])) * rows));
67
2.01k
      if (lf_sync->mutex_[j]) {
68
6.17k
        for (i = 0; i < rows; ++i) {
69
4.15k
          pthread_mutex_init(&lf_sync->mutex_[j][i], NULL);
70
4.15k
        }
71
2.01k
      }
72
73
2.01k
      CHECK_MEM_ERROR(cm, lf_sync->cond_[j],
74
2.01k
                      aom_malloc(sizeof(*(lf_sync->cond_[j])) * rows));
75
2.01k
      if (lf_sync->cond_[j]) {
76
6.17k
        for (i = 0; i < rows; ++i) {
77
4.15k
          pthread_cond_init(&lf_sync->cond_[j][i], NULL);
78
4.15k
        }
79
2.01k
      }
80
2.01k
    }
81
82
672
    CHECK_MEM_ERROR(cm, lf_sync->job_mutex,
83
672
                    aom_malloc(sizeof(*(lf_sync->job_mutex))));
84
672
    if (lf_sync->job_mutex) {
85
672
      pthread_mutex_init(lf_sync->job_mutex, NULL);
86
672
    }
87
672
  }
88
672
#endif  // CONFIG_MULTITHREAD
89
672
  CHECK_MEM_ERROR(cm, lf_sync->lfdata,
90
672
                  aom_malloc(num_workers * sizeof(*(lf_sync->lfdata))));
91
672
  lf_sync->num_workers = num_workers;
92
93
2.68k
  for (int j = 0; j < MAX_MB_PLANE; j++) {
94
2.01k
    CHECK_MEM_ERROR(cm, lf_sync->cur_sb_col[j],
95
2.01k
                    aom_malloc(sizeof(*(lf_sync->cur_sb_col[j])) * rows));
96
2.01k
  }
97
672
  CHECK_MEM_ERROR(
98
672
      cm, lf_sync->job_queue,
99
672
      aom_malloc(sizeof(*(lf_sync->job_queue)) * rows * MAX_MB_PLANE * 2));
100
  // Set up nsync.
101
672
  lf_sync->sync_range = get_sync_range(width);
102
672
}
103
104
// Deallocate lf synchronization related mutex and data
105
1.30k
void av1_loop_filter_dealloc(AV1LfSync *lf_sync) {
106
1.30k
  if (lf_sync != NULL) {
107
1.30k
    int j;
108
1.30k
#if CONFIG_MULTITHREAD
109
1.30k
    int i;
110
5.20k
    for (j = 0; j < MAX_MB_PLANE; j++) {
111
3.90k
      if (lf_sync->mutex_[j] != NULL) {
112
6.17k
        for (i = 0; i < lf_sync->rows; ++i) {
113
4.15k
          pthread_mutex_destroy(&lf_sync->mutex_[j][i]);
114
4.15k
        }
115
2.01k
        aom_free(lf_sync->mutex_[j]);
116
2.01k
      }
117
3.90k
      if (lf_sync->cond_[j] != NULL) {
118
6.17k
        for (i = 0; i < lf_sync->rows; ++i) {
119
4.15k
          pthread_cond_destroy(&lf_sync->cond_[j][i]);
120
4.15k
        }
121
2.01k
        aom_free(lf_sync->cond_[j]);
122
2.01k
      }
123
3.90k
    }
124
1.30k
    if (lf_sync->job_mutex != NULL) {
125
672
      pthread_mutex_destroy(lf_sync->job_mutex);
126
672
      aom_free(lf_sync->job_mutex);
127
672
    }
128
1.30k
#endif  // CONFIG_MULTITHREAD
129
1.30k
    aom_free(lf_sync->lfdata);
130
5.20k
    for (j = 0; j < MAX_MB_PLANE; j++) {
131
3.90k
      aom_free(lf_sync->cur_sb_col[j]);
132
3.90k
    }
133
134
1.30k
    aom_free(lf_sync->job_queue);
135
    // clear the structure as the source of this call may be a resize in which
136
    // case this call will be followed by an _alloc() which may fail.
137
1.30k
    av1_zero(*lf_sync);
138
1.30k
  }
139
1.30k
}
140
141
static void loop_filter_data_reset(LFWorkerData *lf_data,
142
                                   YV12_BUFFER_CONFIG *frame_buffer,
143
10.7k
                                   struct AV1Common *cm, MACROBLOCKD *xd) {
144
10.7k
  struct macroblockd_plane *pd = xd->plane;
145
10.7k
  lf_data->frame_buffer = frame_buffer;
146
10.7k
  lf_data->cm = cm;
147
10.7k
  lf_data->xd = xd;
148
43.0k
  for (int i = 0; i < MAX_MB_PLANE; i++) {
149
32.3k
    memcpy(&lf_data->planes[i].dst, &pd[i].dst, sizeof(lf_data->planes[i].dst));
150
32.3k
    lf_data->planes[i].subsampling_x = pd[i].subsampling_x;
151
32.3k
    lf_data->planes[i].subsampling_y = pd[i].subsampling_y;
152
32.3k
  }
153
10.7k
}
154
155
void av1_alloc_cdef_sync(AV1_COMMON *const cm, AV1CdefSync *cdef_sync,
156
0
                         int num_workers) {
157
0
  if (num_workers < 1) return;
158
0
#if CONFIG_MULTITHREAD
159
0
  if (cdef_sync->mutex_ == NULL) {
160
0
    CHECK_MEM_ERROR(cm, cdef_sync->mutex_,
161
0
                    aom_malloc(sizeof(*(cdef_sync->mutex_))));
162
0
    if (cdef_sync->mutex_) pthread_mutex_init(cdef_sync->mutex_, NULL);
163
0
  }
164
#else
165
  (void)cm;
166
  (void)cdef_sync;
167
#endif  // CONFIG_MULTITHREAD
168
0
}
169
170
0
void av1_free_cdef_sync(AV1CdefSync *cdef_sync) {
171
0
  if (cdef_sync == NULL) return;
172
0
#if CONFIG_MULTITHREAD
173
0
  if (cdef_sync->mutex_ != NULL) {
174
0
    pthread_mutex_destroy(cdef_sync->mutex_);
175
0
    aom_free(cdef_sync->mutex_);
176
0
  }
177
0
#endif  // CONFIG_MULTITHREAD
178
0
}
179
180
static INLINE void cdef_row_mt_sync_read(AV1CdefSync *const cdef_sync,
181
0
                                         int row) {
182
0
  if (!row) return;
183
0
#if CONFIG_MULTITHREAD
184
0
  AV1CdefRowSync *const cdef_row_mt = cdef_sync->cdef_row_mt;
185
0
  pthread_mutex_lock(cdef_row_mt[row - 1].row_mutex_);
186
0
  while (cdef_row_mt[row - 1].is_row_done != 1)
187
0
    pthread_cond_wait(cdef_row_mt[row - 1].row_cond_,
188
0
                      cdef_row_mt[row - 1].row_mutex_);
189
0
  cdef_row_mt[row - 1].is_row_done = 0;
190
0
  pthread_mutex_unlock(cdef_row_mt[row - 1].row_mutex_);
191
#else
192
  (void)cdef_sync;
193
#endif  // CONFIG_MULTITHREAD
194
0
}
195
196
static INLINE void cdef_row_mt_sync_write(AV1CdefSync *const cdef_sync,
197
0
                                          int row) {
198
0
#if CONFIG_MULTITHREAD
199
0
  AV1CdefRowSync *const cdef_row_mt = cdef_sync->cdef_row_mt;
200
0
  pthread_mutex_lock(cdef_row_mt[row].row_mutex_);
201
0
  pthread_cond_signal(cdef_row_mt[row].row_cond_);
202
0
  cdef_row_mt[row].is_row_done = 1;
203
0
  pthread_mutex_unlock(cdef_row_mt[row].row_mutex_);
204
#else
205
  (void)cdef_sync;
206
  (void)row;
207
#endif  // CONFIG_MULTITHREAD
208
0
}
209
210
static INLINE void sync_read(AV1LfSync *const lf_sync, int r, int c,
211
46.3k
                             int plane) {
212
46.3k
#if CONFIG_MULTITHREAD
213
46.3k
  const int nsync = lf_sync->sync_range;
214
215
46.3k
  if (r && !(c & (nsync - 1))) {
216
35.3k
    pthread_mutex_t *const mutex = &lf_sync->mutex_[plane][r - 1];
217
35.3k
    pthread_mutex_lock(mutex);
218
219
38.4k
    while (c > lf_sync->cur_sb_col[plane][r - 1] - nsync) {
220
3.08k
      pthread_cond_wait(&lf_sync->cond_[plane][r - 1], mutex);
221
3.08k
    }
222
35.3k
    pthread_mutex_unlock(mutex);
223
35.3k
  }
224
#else
225
  (void)lf_sync;
226
  (void)r;
227
  (void)c;
228
  (void)plane;
229
#endif  // CONFIG_MULTITHREAD
230
46.3k
}
231
232
static INLINE void sync_write(AV1LfSync *const lf_sync, int r, int c,
233
23.1k
                              const int sb_cols, int plane) {
234
23.1k
#if CONFIG_MULTITHREAD
235
23.1k
  const int nsync = lf_sync->sync_range;
236
23.1k
  int cur;
237
  // Only signal when there are enough filtered SB for next row to run.
238
23.1k
  int sig = 1;
239
240
23.1k
  if (c < sb_cols - 1) {
241
13.6k
    cur = c;
242
13.6k
    if (c % nsync) sig = 0;
243
13.6k
  } else {
244
9.52k
    cur = sb_cols + nsync;
245
9.52k
  }
246
247
23.1k
  if (sig) {
248
23.1k
    pthread_mutex_lock(&lf_sync->mutex_[plane][r]);
249
250
23.1k
    lf_sync->cur_sb_col[plane][r] = cur;
251
252
23.1k
    pthread_cond_broadcast(&lf_sync->cond_[plane][r]);
253
23.1k
    pthread_mutex_unlock(&lf_sync->mutex_[plane][r]);
254
23.1k
  }
255
#else
256
  (void)lf_sync;
257
  (void)r;
258
  (void)c;
259
  (void)sb_cols;
260
  (void)plane;
261
#endif  // CONFIG_MULTITHREAD
262
23.1k
}
263
264
static void enqueue_lf_jobs(AV1LfSync *lf_sync, int start, int stop,
265
4.60k
                            const int planes_to_lf[3], int is_realtime) {
266
4.60k
  int mi_row, plane, dir;
267
4.60k
  AV1LfMTInfo *lf_job_queue = lf_sync->job_queue;
268
4.60k
  lf_sync->jobs_enqueued = 0;
269
4.60k
  lf_sync->jobs_dequeued = 0;
270
271
  // Launch all vertical jobs first, as they are blocking the horizontal ones.
272
  // Launch top row jobs for all planes first, in case the output can be
273
  // partially reconstructed row by row.
274
13.8k
  for (dir = 0; dir < 2; ++dir) {
275
28.3k
    for (mi_row = start; mi_row < stop; mi_row += MAX_MIB_SIZE) {
276
76.7k
      for (plane = 0; plane < 3; ++plane) {
277
57.5k
        if (!planes_to_lf[plane]) continue;
278
19.1k
        lf_job_queue->mi_row = mi_row;
279
19.1k
        lf_job_queue->plane = plane;
280
19.1k
        lf_job_queue->dir = dir;
281
19.1k
        lf_job_queue->is_realtime = is_realtime;
282
19.1k
        lf_job_queue++;
283
19.1k
        lf_sync->jobs_enqueued++;
284
19.1k
      }
285
19.1k
    }
286
9.21k
  }
287
4.60k
}
288
289
29.8k
static AV1LfMTInfo *get_lf_job_info(AV1LfSync *lf_sync) {
290
29.8k
  AV1LfMTInfo *cur_job_info = NULL;
291
292
29.8k
#if CONFIG_MULTITHREAD
293
29.8k
  pthread_mutex_lock(lf_sync->job_mutex);
294
295
29.8k
  if (lf_sync->jobs_dequeued < lf_sync->jobs_enqueued) {
296
19.1k
    cur_job_info = lf_sync->job_queue + lf_sync->jobs_dequeued;
297
19.1k
    lf_sync->jobs_dequeued++;
298
19.1k
  }
299
300
29.8k
  pthread_mutex_unlock(lf_sync->job_mutex);
301
#else
302
  (void)lf_sync;
303
#endif
304
305
29.8k
  return cur_job_info;
306
29.8k
}
307
308
// One job of row loopfiltering.
309
static INLINE void thread_loop_filter_rows(
310
    const YV12_BUFFER_CONFIG *const frame_buffer, AV1_COMMON *const cm,
311
    struct macroblockd_plane *planes, MACROBLOCKD *xd, int mi_row, int plane,
312
33.8k
    int dir, int is_realtime, AV1LfSync *const lf_sync) {
313
33.8k
  const int sb_cols =
314
33.8k
      ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols, MAX_MIB_SIZE_LOG2) >>
315
33.8k
      MAX_MIB_SIZE_LOG2;
316
33.8k
  const int r = mi_row >> MAX_MIB_SIZE_LOG2;
317
33.8k
  int mi_col, c;
318
319
33.8k
  if (dir == 0) {
320
48.8k
    for (mi_col = 0; mi_col < cm->mi_params.mi_cols; mi_col += MAX_MIB_SIZE) {
321
31.9k
      c = mi_col >> MAX_MIB_SIZE_LOG2;
322
323
31.9k
      av1_setup_dst_planes(planes, cm->seq_params->sb_size, frame_buffer,
324
31.9k
                           mi_row, mi_col, plane, plane + 1);
325
31.9k
#if CONFIG_AV1_HIGHBITDEPTH
326
31.9k
      (void)is_realtime;
327
31.9k
      av1_filter_block_plane_vert(cm, xd, plane, &planes[plane], mi_row,
328
31.9k
                                  mi_col);
329
#else
330
      if (is_realtime) {
331
        av1_filter_block_plane_vert_rt(cm, xd, plane, &planes[plane], mi_row,
332
                                       mi_col);
333
334
      } else {
335
        av1_filter_block_plane_vert(cm, xd, plane, &planes[plane], mi_row,
336
                                    mi_col);
337
      }
338
#endif
339
31.9k
      if (lf_sync != NULL) sync_write(lf_sync, r, c, sb_cols, plane);
340
31.9k
    }
341
16.9k
  } else if (dir == 1) {
342
48.8k
    for (mi_col = 0; mi_col < cm->mi_params.mi_cols; mi_col += MAX_MIB_SIZE) {
343
31.9k
      c = mi_col >> MAX_MIB_SIZE_LOG2;
344
345
31.9k
      if (lf_sync != NULL) {
346
        // Wait for vertical edge filtering of the top-right block to be
347
        // completed
348
23.1k
        sync_read(lf_sync, r, c, plane);
349
350
        // Wait for vertical edge filtering of the right block to be completed
351
23.1k
        sync_read(lf_sync, r + 1, c, plane);
352
23.1k
      }
353
354
31.9k
      av1_setup_dst_planes(planes, cm->seq_params->sb_size, frame_buffer,
355
31.9k
                           mi_row, mi_col, plane, plane + 1);
356
31.9k
#if CONFIG_AV1_HIGHBITDEPTH
357
31.9k
      (void)is_realtime;
358
31.9k
      av1_filter_block_plane_horz(cm, xd, plane, &planes[plane], mi_row,
359
31.9k
                                  mi_col);
360
#else
361
      if (is_realtime) {
362
        av1_filter_block_plane_horz_rt(cm, xd, plane, &planes[plane], mi_row,
363
                                       mi_col);
364
      } else {
365
        av1_filter_block_plane_horz(cm, xd, plane, &planes[plane], mi_row,
366
                                    mi_col);
367
      }
368
#endif
369
31.9k
    }
370
16.9k
  }
371
33.8k
}
372
373
// Row-based multi-threaded loopfilter hook
374
10.7k
static int loop_filter_row_worker(void *arg1, void *arg2) {
375
10.7k
  AV1LfSync *const lf_sync = (AV1LfSync *)arg1;
376
10.7k
  LFWorkerData *const lf_data = (LFWorkerData *)arg2;
377
10.7k
  AV1LfMTInfo *cur_job_info;
378
29.9k
  while ((cur_job_info = get_lf_job_info(lf_sync)) != NULL) {
379
19.1k
    const int is_realtime = cur_job_info->is_realtime && !cur_job_info->plane;
380
19.1k
    thread_loop_filter_rows(lf_data->frame_buffer, lf_data->cm, lf_data->planes,
381
19.1k
                            lf_data->xd, cur_job_info->mi_row,
382
19.1k
                            cur_job_info->plane, cur_job_info->dir, is_realtime,
383
19.1k
                            lf_sync);
384
19.1k
  }
385
10.7k
  return 1;
386
10.7k
}
387
388
static void loop_filter_rows_mt(YV12_BUFFER_CONFIG *frame, AV1_COMMON *cm,
389
                                MACROBLOCKD *xd, int start, int stop,
390
                                const int planes_to_lf[3], AVxWorker *workers,
391
                                int num_workers, AV1LfSync *lf_sync,
392
4.60k
                                int is_realtime) {
393
4.60k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
394
  // Number of superblock rows and cols
395
4.60k
  const int sb_rows =
396
4.60k
      ALIGN_POWER_OF_TWO(cm->mi_params.mi_rows, MAX_MIB_SIZE_LOG2) >>
397
4.60k
      MAX_MIB_SIZE_LOG2;
398
4.60k
  int i;
399
400
4.60k
  if (!lf_sync->sync_range || sb_rows != lf_sync->rows ||
401
4.60k
      num_workers > lf_sync->num_workers) {
402
44
    av1_loop_filter_dealloc(lf_sync);
403
44
    av1_loop_filter_alloc(lf_sync, cm, sb_rows, cm->width, num_workers);
404
44
  }
405
406
  // Initialize cur_sb_col to -1 for all SB rows.
407
18.4k
  for (i = 0; i < MAX_MB_PLANE; i++) {
408
13.8k
    memset(lf_sync->cur_sb_col[i], -1,
409
13.8k
           sizeof(*(lf_sync->cur_sb_col[i])) * sb_rows);
410
13.8k
  }
411
412
4.60k
  enqueue_lf_jobs(lf_sync, start, stop, planes_to_lf, is_realtime);
413
414
  // Set up loopfilter thread data.
415
15.3k
  for (i = num_workers - 1; i >= 0; --i) {
416
10.7k
    AVxWorker *const worker = &workers[i];
417
10.7k
    LFWorkerData *const lf_data = &lf_sync->lfdata[i];
418
419
10.7k
    worker->hook = loop_filter_row_worker;
420
10.7k
    worker->data1 = lf_sync;
421
10.7k
    worker->data2 = lf_data;
422
423
    // Loopfilter data
424
10.7k
    loop_filter_data_reset(lf_data, frame, cm, xd);
425
426
    // Start loopfiltering
427
10.7k
    if (i == 0) {
428
4.60k
      winterface->execute(worker);
429
6.15k
    } else {
430
6.15k
      winterface->launch(worker);
431
6.15k
    }
432
10.7k
  }
433
434
  // Wait till all rows are finished
435
10.7k
  for (i = 1; i < num_workers; ++i) {
436
6.15k
    winterface->sync(&workers[i]);
437
6.15k
  }
438
4.60k
}
439
440
static void loop_filter_rows(YV12_BUFFER_CONFIG *frame, AV1_COMMON *cm,
441
                             MACROBLOCKD *xd, int start, int stop,
442
5.50k
                             const int planes_to_lf[3], int is_realtime) {
443
  // Filter top rows of all planes first, in case the output can be partially
444
  // reconstructed row by row.
445
5.50k
  int mi_row, plane, dir;
446
12.8k
  for (mi_row = start; mi_row < stop; mi_row += MAX_MIB_SIZE) {
447
29.3k
    for (plane = 0; plane < 3; ++plane) {
448
22.0k
      if (!planes_to_lf[plane]) continue;
449
22.0k
      for (dir = 0; dir < 2; ++dir) {
450
14.7k
        thread_loop_filter_rows(frame, cm, xd->plane, xd, mi_row, plane, dir,
451
14.7k
                                is_realtime && !plane, /*lf_sync=*/NULL);
452
14.7k
      }
453
7.35k
    }
454
7.33k
  }
455
5.50k
}
456
457
void av1_loop_filter_frame_mt(YV12_BUFFER_CONFIG *frame, AV1_COMMON *cm,
458
                              MACROBLOCKD *xd, int plane_start, int plane_end,
459
                              int partial_frame, AVxWorker *workers,
460
                              int num_workers, AV1LfSync *lf_sync,
461
13.2k
                              int is_realtime) {
462
13.2k
  int start_mi_row, end_mi_row, mi_rows_to_filter;
463
13.2k
  int planes_to_lf[3];
464
465
  // For each luma and chroma plane, whether to filter it or not.
466
13.2k
  planes_to_lf[0] = (cm->lf.filter_level[0] || cm->lf.filter_level[1]) &&
467
13.2k
                    plane_start <= 0 && 0 < plane_end;
468
13.2k
  planes_to_lf[1] = cm->lf.filter_level_u && plane_start <= 1 && 1 < plane_end;
469
13.2k
  planes_to_lf[2] = cm->lf.filter_level_v && plane_start <= 2 && 2 < plane_end;
470
  // If the luma plane is purposely not filtered, neither are the chroma planes.
471
13.2k
  if (!planes_to_lf[0] && plane_start <= 0 && 0 < plane_end) return;
472
  // Early exit.
473
12.1k
  if (!planes_to_lf[0] && !planes_to_lf[1] && !planes_to_lf[2]) return;
474
475
10.1k
  start_mi_row = 0;
476
10.1k
  mi_rows_to_filter = cm->mi_params.mi_rows;
477
10.1k
  if (partial_frame && cm->mi_params.mi_rows > 8) {
478
0
    start_mi_row = cm->mi_params.mi_rows >> 1;
479
0
    start_mi_row &= 0xfffffff8;
480
0
    mi_rows_to_filter = AOMMAX(cm->mi_params.mi_rows / 8, 8);
481
0
  }
482
10.1k
  end_mi_row = start_mi_row + mi_rows_to_filter;
483
10.1k
  av1_loop_filter_frame_init(cm, plane_start, plane_end);
484
485
10.1k
  if (num_workers > 1) {
486
    // Enqueue and execute loopfiltering jobs.
487
4.60k
    loop_filter_rows_mt(frame, cm, xd, start_mi_row, end_mi_row, planes_to_lf,
488
4.60k
                        workers, num_workers, lf_sync, is_realtime);
489
5.50k
  } else {
490
    // Directly filter in the main thread.
491
5.50k
    loop_filter_rows(frame, cm, xd, start_mi_row, end_mi_row, planes_to_lf,
492
5.50k
                     is_realtime);
493
5.50k
  }
494
10.1k
}
495
496
#if !CONFIG_REALTIME_ONLY
497
0
static INLINE void lr_sync_read(void *const lr_sync, int r, int c, int plane) {
498
0
#if CONFIG_MULTITHREAD
499
0
  AV1LrSync *const loop_res_sync = (AV1LrSync *)lr_sync;
500
0
  const int nsync = loop_res_sync->sync_range;
501
502
0
  if (r && !(c & (nsync - 1))) {
503
0
    pthread_mutex_t *const mutex = &loop_res_sync->mutex_[plane][r - 1];
504
0
    pthread_mutex_lock(mutex);
505
506
0
    while (c > loop_res_sync->cur_sb_col[plane][r - 1] - nsync) {
507
0
      pthread_cond_wait(&loop_res_sync->cond_[plane][r - 1], mutex);
508
0
    }
509
0
    pthread_mutex_unlock(mutex);
510
0
  }
511
#else
512
  (void)lr_sync;
513
  (void)r;
514
  (void)c;
515
  (void)plane;
516
#endif  // CONFIG_MULTITHREAD
517
0
}
518
519
static INLINE void lr_sync_write(void *const lr_sync, int r, int c,
520
0
                                 const int sb_cols, int plane) {
521
0
#if CONFIG_MULTITHREAD
522
0
  AV1LrSync *const loop_res_sync = (AV1LrSync *)lr_sync;
523
0
  const int nsync = loop_res_sync->sync_range;
524
0
  int cur;
525
  // Only signal when there are enough filtered SB for next row to run.
526
0
  int sig = 1;
527
528
0
  if (c < sb_cols - 1) {
529
0
    cur = c;
530
0
    if (c % nsync) sig = 0;
531
0
  } else {
532
0
    cur = sb_cols + nsync;
533
0
  }
534
535
0
  if (sig) {
536
0
    pthread_mutex_lock(&loop_res_sync->mutex_[plane][r]);
537
538
0
    loop_res_sync->cur_sb_col[plane][r] = cur;
539
540
0
    pthread_cond_broadcast(&loop_res_sync->cond_[plane][r]);
541
0
    pthread_mutex_unlock(&loop_res_sync->mutex_[plane][r]);
542
0
  }
543
#else
544
  (void)lr_sync;
545
  (void)r;
546
  (void)c;
547
  (void)sb_cols;
548
  (void)plane;
549
#endif  // CONFIG_MULTITHREAD
550
0
}
551
552
// Allocate memory for loop restoration row synchronization
553
void av1_loop_restoration_alloc(AV1LrSync *lr_sync, AV1_COMMON *cm,
554
                                int num_workers, int num_rows_lr,
555
0
                                int num_planes, int width) {
556
0
  lr_sync->rows = num_rows_lr;
557
0
  lr_sync->num_planes = num_planes;
558
0
#if CONFIG_MULTITHREAD
559
0
  {
560
0
    int i, j;
561
562
0
    for (j = 0; j < num_planes; j++) {
563
0
      CHECK_MEM_ERROR(cm, lr_sync->mutex_[j],
564
0
                      aom_malloc(sizeof(*(lr_sync->mutex_[j])) * num_rows_lr));
565
0
      if (lr_sync->mutex_[j]) {
566
0
        for (i = 0; i < num_rows_lr; ++i) {
567
0
          pthread_mutex_init(&lr_sync->mutex_[j][i], NULL);
568
0
        }
569
0
      }
570
571
0
      CHECK_MEM_ERROR(cm, lr_sync->cond_[j],
572
0
                      aom_malloc(sizeof(*(lr_sync->cond_[j])) * num_rows_lr));
573
0
      if (lr_sync->cond_[j]) {
574
0
        for (i = 0; i < num_rows_lr; ++i) {
575
0
          pthread_cond_init(&lr_sync->cond_[j][i], NULL);
576
0
        }
577
0
      }
578
0
    }
579
580
0
    CHECK_MEM_ERROR(cm, lr_sync->job_mutex,
581
0
                    aom_malloc(sizeof(*(lr_sync->job_mutex))));
582
0
    if (lr_sync->job_mutex) {
583
0
      pthread_mutex_init(lr_sync->job_mutex, NULL);
584
0
    }
585
0
  }
586
0
#endif  // CONFIG_MULTITHREAD
587
0
  CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata,
588
0
                  aom_malloc(num_workers * sizeof(*(lr_sync->lrworkerdata))));
589
590
0
  for (int worker_idx = 0; worker_idx < num_workers; ++worker_idx) {
591
0
    if (worker_idx < num_workers - 1) {
592
0
      CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata[worker_idx].rst_tmpbuf,
593
0
                      (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE));
594
0
      CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata[worker_idx].rlbs,
595
0
                      aom_malloc(sizeof(RestorationLineBuffers)));
596
597
0
    } else {
598
0
      lr_sync->lrworkerdata[worker_idx].rst_tmpbuf = cm->rst_tmpbuf;
599
0
      lr_sync->lrworkerdata[worker_idx].rlbs = cm->rlbs;
600
0
    }
601
0
  }
602
603
0
  lr_sync->num_workers = num_workers;
604
605
0
  for (int j = 0; j < num_planes; j++) {
606
0
    CHECK_MEM_ERROR(
607
0
        cm, lr_sync->cur_sb_col[j],
608
0
        aom_malloc(sizeof(*(lr_sync->cur_sb_col[j])) * num_rows_lr));
609
0
  }
610
0
  CHECK_MEM_ERROR(
611
0
      cm, lr_sync->job_queue,
612
0
      aom_malloc(sizeof(*(lr_sync->job_queue)) * num_rows_lr * num_planes));
613
  // Set up nsync.
614
0
  lr_sync->sync_range = get_lr_sync_range(width);
615
0
}
616
617
// Deallocate loop restoration synchronization related mutex and data
618
628
void av1_loop_restoration_dealloc(AV1LrSync *lr_sync, int num_workers) {
619
628
  if (lr_sync != NULL) {
620
628
    int j;
621
628
#if CONFIG_MULTITHREAD
622
628
    int i;
623
2.51k
    for (j = 0; j < MAX_MB_PLANE; j++) {
624
1.88k
      if (lr_sync->mutex_[j] != NULL) {
625
0
        for (i = 0; i < lr_sync->rows; ++i) {
626
0
          pthread_mutex_destroy(&lr_sync->mutex_[j][i]);
627
0
        }
628
0
        aom_free(lr_sync->mutex_[j]);
629
0
      }
630
1.88k
      if (lr_sync->cond_[j] != NULL) {
631
0
        for (i = 0; i < lr_sync->rows; ++i) {
632
0
          pthread_cond_destroy(&lr_sync->cond_[j][i]);
633
0
        }
634
0
        aom_free(lr_sync->cond_[j]);
635
0
      }
636
1.88k
    }
637
628
    if (lr_sync->job_mutex != NULL) {
638
0
      pthread_mutex_destroy(lr_sync->job_mutex);
639
0
      aom_free(lr_sync->job_mutex);
640
0
    }
641
628
#endif  // CONFIG_MULTITHREAD
642
2.51k
    for (j = 0; j < MAX_MB_PLANE; j++) {
643
1.88k
      aom_free(lr_sync->cur_sb_col[j]);
644
1.88k
    }
645
646
628
    aom_free(lr_sync->job_queue);
647
648
628
    if (lr_sync->lrworkerdata) {
649
0
      for (int worker_idx = 0; worker_idx < num_workers - 1; worker_idx++) {
650
0
        LRWorkerData *const workerdata_data =
651
0
            lr_sync->lrworkerdata + worker_idx;
652
653
0
        aom_free(workerdata_data->rst_tmpbuf);
654
0
        aom_free(workerdata_data->rlbs);
655
0
      }
656
0
      aom_free(lr_sync->lrworkerdata);
657
0
    }
658
659
    // clear the structure as the source of this call may be a resize in which
660
    // case this call will be followed by an _alloc() which may fail.
661
628
    av1_zero(*lr_sync);
662
628
  }
663
628
}
664
665
static void enqueue_lr_jobs(AV1LrSync *lr_sync, AV1LrStruct *lr_ctxt,
666
0
                            AV1_COMMON *cm) {
667
0
  FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
668
669
0
  const int num_planes = av1_num_planes(cm);
670
0
  AV1LrMTInfo *lr_job_queue = lr_sync->job_queue;
671
0
  int32_t lr_job_counter[2], num_even_lr_jobs = 0;
672
0
  lr_sync->jobs_enqueued = 0;
673
0
  lr_sync->jobs_dequeued = 0;
674
675
0
  for (int plane = 0; plane < num_planes; plane++) {
676
0
    if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
677
0
    num_even_lr_jobs =
678
0
        num_even_lr_jobs + ((ctxt[plane].rsi->vert_units_per_tile + 1) >> 1);
679
0
  }
680
0
  lr_job_counter[0] = 0;
681
0
  lr_job_counter[1] = num_even_lr_jobs;
682
683
0
  for (int plane = 0; plane < num_planes; plane++) {
684
0
    if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
685
0
    const int is_uv = plane > 0;
686
0
    const int ss_y = is_uv && cm->seq_params->subsampling_y;
687
688
0
    AV1PixelRect tile_rect = ctxt[plane].tile_rect;
689
0
    const int unit_size = ctxt[plane].rsi->restoration_unit_size;
690
691
0
    const int tile_h = tile_rect.bottom - tile_rect.top;
692
0
    const int ext_size = unit_size * 3 / 2;
693
694
0
    int y0 = 0, i = 0;
695
0
    while (y0 < tile_h) {
696
0
      int remaining_h = tile_h - y0;
697
0
      int h = (remaining_h < ext_size) ? remaining_h : unit_size;
698
699
0
      RestorationTileLimits limits;
700
0
      limits.v_start = tile_rect.top + y0;
701
0
      limits.v_end = tile_rect.top + y0 + h;
702
0
      assert(limits.v_end <= tile_rect.bottom);
703
      // Offset the tile upwards to align with the restoration processing stripe
704
0
      const int voffset = RESTORATION_UNIT_OFFSET >> ss_y;
705
0
      limits.v_start = AOMMAX(tile_rect.top, limits.v_start - voffset);
706
0
      if (limits.v_end < tile_rect.bottom) limits.v_end -= voffset;
707
708
0
      assert(lr_job_counter[0] <= num_even_lr_jobs);
709
710
0
      lr_job_queue[lr_job_counter[i & 1]].lr_unit_row = i;
711
0
      lr_job_queue[lr_job_counter[i & 1]].plane = plane;
712
0
      lr_job_queue[lr_job_counter[i & 1]].v_start = limits.v_start;
713
0
      lr_job_queue[lr_job_counter[i & 1]].v_end = limits.v_end;
714
0
      lr_job_queue[lr_job_counter[i & 1]].sync_mode = i & 1;
715
0
      if ((i & 1) == 0) {
716
0
        lr_job_queue[lr_job_counter[i & 1]].v_copy_start =
717
0
            limits.v_start + RESTORATION_BORDER;
718
0
        lr_job_queue[lr_job_counter[i & 1]].v_copy_end =
719
0
            limits.v_end - RESTORATION_BORDER;
720
0
        if (i == 0) {
721
0
          assert(limits.v_start == tile_rect.top);
722
0
          lr_job_queue[lr_job_counter[i & 1]].v_copy_start = tile_rect.top;
723
0
        }
724
0
        if (i == (ctxt[plane].rsi->vert_units_per_tile - 1)) {
725
0
          assert(limits.v_end == tile_rect.bottom);
726
0
          lr_job_queue[lr_job_counter[i & 1]].v_copy_end = tile_rect.bottom;
727
0
        }
728
0
      } else {
729
0
        lr_job_queue[lr_job_counter[i & 1]].v_copy_start =
730
0
            AOMMAX(limits.v_start - RESTORATION_BORDER, tile_rect.top);
731
0
        lr_job_queue[lr_job_counter[i & 1]].v_copy_end =
732
0
            AOMMIN(limits.v_end + RESTORATION_BORDER, tile_rect.bottom);
733
0
      }
734
0
      lr_job_counter[i & 1]++;
735
0
      lr_sync->jobs_enqueued++;
736
737
0
      y0 += h;
738
0
      ++i;
739
0
    }
740
0
  }
741
0
}
742
743
0
static AV1LrMTInfo *get_lr_job_info(AV1LrSync *lr_sync) {
744
0
  AV1LrMTInfo *cur_job_info = NULL;
745
746
0
#if CONFIG_MULTITHREAD
747
0
  pthread_mutex_lock(lr_sync->job_mutex);
748
749
0
  if (lr_sync->jobs_dequeued < lr_sync->jobs_enqueued) {
750
0
    cur_job_info = lr_sync->job_queue + lr_sync->jobs_dequeued;
751
0
    lr_sync->jobs_dequeued++;
752
0
  }
753
754
0
  pthread_mutex_unlock(lr_sync->job_mutex);
755
#else
756
  (void)lr_sync;
757
#endif
758
759
0
  return cur_job_info;
760
0
}
761
762
// Implement row loop restoration for each thread.
763
0
static int loop_restoration_row_worker(void *arg1, void *arg2) {
764
0
  AV1LrSync *const lr_sync = (AV1LrSync *)arg1;
765
0
  LRWorkerData *lrworkerdata = (LRWorkerData *)arg2;
766
0
  AV1LrStruct *lr_ctxt = (AV1LrStruct *)lrworkerdata->lr_ctxt;
767
0
  FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
768
0
  int lr_unit_row;
769
0
  int plane;
770
0
  const int tile_row = LR_TILE_ROW;
771
0
  const int tile_col = LR_TILE_COL;
772
0
  const int tile_cols = LR_TILE_COLS;
773
0
  const int tile_idx = tile_col + tile_row * tile_cols;
774
0
  typedef void (*copy_fun)(const YV12_BUFFER_CONFIG *src_ybc,
775
0
                           YV12_BUFFER_CONFIG *dst_ybc, int hstart, int hend,
776
0
                           int vstart, int vend);
777
0
  static const copy_fun copy_funs[3] = { aom_yv12_partial_coloc_copy_y,
778
0
                                         aom_yv12_partial_coloc_copy_u,
779
0
                                         aom_yv12_partial_coloc_copy_v };
780
781
0
  while (1) {
782
0
    AV1LrMTInfo *cur_job_info = get_lr_job_info(lr_sync);
783
0
    if (cur_job_info != NULL) {
784
0
      RestorationTileLimits limits;
785
0
      sync_read_fn_t on_sync_read;
786
0
      sync_write_fn_t on_sync_write;
787
0
      limits.v_start = cur_job_info->v_start;
788
0
      limits.v_end = cur_job_info->v_end;
789
0
      lr_unit_row = cur_job_info->lr_unit_row;
790
0
      plane = cur_job_info->plane;
791
0
      const int unit_idx0 = tile_idx * ctxt[plane].rsi->units_per_tile;
792
793
      // sync_mode == 1 implies only sync read is required in LR Multi-threading
794
      // sync_mode == 0 implies only sync write is required.
795
0
      on_sync_read =
796
0
          cur_job_info->sync_mode == 1 ? lr_sync_read : av1_lr_sync_read_dummy;
797
0
      on_sync_write = cur_job_info->sync_mode == 0 ? lr_sync_write
798
0
                                                   : av1_lr_sync_write_dummy;
799
800
0
      av1_foreach_rest_unit_in_row(
801
0
          &limits, &(ctxt[plane].tile_rect), lr_ctxt->on_rest_unit, lr_unit_row,
802
0
          ctxt[plane].rsi->restoration_unit_size, unit_idx0,
803
0
          ctxt[plane].rsi->horz_units_per_tile,
804
0
          ctxt[plane].rsi->vert_units_per_tile, plane, &ctxt[plane],
805
0
          lrworkerdata->rst_tmpbuf, lrworkerdata->rlbs, on_sync_read,
806
0
          on_sync_write, lr_sync);
807
808
0
      copy_funs[plane](lr_ctxt->dst, lr_ctxt->frame, ctxt[plane].tile_rect.left,
809
0
                       ctxt[plane].tile_rect.right, cur_job_info->v_copy_start,
810
0
                       cur_job_info->v_copy_end);
811
0
    } else {
812
0
      break;
813
0
    }
814
0
  }
815
0
  return 1;
816
0
}
817
818
static void foreach_rest_unit_in_planes_mt(AV1LrStruct *lr_ctxt,
819
                                           AVxWorker *workers, int nworkers,
820
0
                                           AV1LrSync *lr_sync, AV1_COMMON *cm) {
821
0
  FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
822
823
0
  const int num_planes = av1_num_planes(cm);
824
825
0
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
826
0
  int num_rows_lr = 0;
827
828
0
  for (int plane = 0; plane < num_planes; plane++) {
829
0
    if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
830
831
0
    const AV1PixelRect tile_rect = ctxt[plane].tile_rect;
832
0
    const int max_tile_h = tile_rect.bottom - tile_rect.top;
833
834
0
    const int unit_size = cm->rst_info[plane].restoration_unit_size;
835
836
0
    num_rows_lr =
837
0
        AOMMAX(num_rows_lr, av1_lr_count_units_in_tile(unit_size, max_tile_h));
838
0
  }
839
840
0
  const int num_workers = nworkers;
841
0
  int i;
842
0
  assert(MAX_MB_PLANE == 3);
843
844
0
  if (!lr_sync->sync_range || num_rows_lr > lr_sync->rows ||
845
0
      num_workers > lr_sync->num_workers || num_planes > lr_sync->num_planes) {
846
0
    av1_loop_restoration_dealloc(lr_sync, num_workers);
847
0
    av1_loop_restoration_alloc(lr_sync, cm, num_workers, num_rows_lr,
848
0
                               num_planes, cm->width);
849
0
  }
850
851
  // Initialize cur_sb_col to -1 for all SB rows.
852
0
  for (i = 0; i < num_planes; i++) {
853
0
    memset(lr_sync->cur_sb_col[i], -1,
854
0
           sizeof(*(lr_sync->cur_sb_col[i])) * num_rows_lr);
855
0
  }
856
857
0
  enqueue_lr_jobs(lr_sync, lr_ctxt, cm);
858
859
  // Set up looprestoration thread data.
860
0
  for (i = num_workers - 1; i >= 0; --i) {
861
0
    AVxWorker *const worker = &workers[i];
862
0
    lr_sync->lrworkerdata[i].lr_ctxt = (void *)lr_ctxt;
863
0
    worker->hook = loop_restoration_row_worker;
864
0
    worker->data1 = lr_sync;
865
0
    worker->data2 = &lr_sync->lrworkerdata[i];
866
867
    // Start loop restoration
868
0
    if (i == 0) {
869
0
      winterface->execute(worker);
870
0
    } else {
871
0
      winterface->launch(worker);
872
0
    }
873
0
  }
874
875
  // Wait till all rows are finished
876
0
  for (i = 1; i < num_workers; ++i) {
877
0
    winterface->sync(&workers[i]);
878
0
  }
879
0
}
880
881
void av1_loop_restoration_filter_frame_mt(YV12_BUFFER_CONFIG *frame,
882
                                          AV1_COMMON *cm, int optimized_lr,
883
                                          AVxWorker *workers, int num_workers,
884
0
                                          AV1LrSync *lr_sync, void *lr_ctxt) {
885
0
  assert(!cm->features.all_lossless);
886
887
0
  const int num_planes = av1_num_planes(cm);
888
889
0
  AV1LrStruct *loop_rest_ctxt = (AV1LrStruct *)lr_ctxt;
890
891
0
  av1_loop_restoration_filter_frame_init(loop_rest_ctxt, frame, cm,
892
0
                                         optimized_lr, num_planes);
893
894
0
  foreach_rest_unit_in_planes_mt(loop_rest_ctxt, workers, num_workers, lr_sync,
895
0
                                 cm);
896
0
}
897
#endif
898
899
// Initializes cdef_sync parameters.
900
0
static AOM_INLINE void reset_cdef_job_info(AV1CdefSync *const cdef_sync) {
901
0
  cdef_sync->end_of_frame = 0;
902
0
  cdef_sync->fbr = 0;
903
0
  cdef_sync->fbc = 0;
904
0
}
905
906
static AOM_INLINE void launch_cdef_workers(AVxWorker *const workers,
907
0
                                           int num_workers) {
908
0
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
909
0
  for (int i = num_workers - 1; i >= 0; i--) {
910
0
    AVxWorker *const worker = &workers[i];
911
0
    if (i == 0)
912
0
      winterface->execute(worker);
913
0
    else
914
0
      winterface->launch(worker);
915
0
  }
916
0
}
917
918
static AOM_INLINE void sync_cdef_workers(AVxWorker *const workers,
919
                                         AV1_COMMON *const cm,
920
0
                                         int num_workers) {
921
0
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
922
0
  int had_error = 0;
923
924
  // Wait for completion of Cdef frame.
925
0
  for (int i = num_workers - 1; i > 0; i--) {
926
0
    AVxWorker *const worker = &workers[i];
927
0
    had_error |= !winterface->sync(worker);
928
0
  }
929
0
  if (had_error)
930
0
    aom_internal_error(cm->error, AOM_CODEC_ERROR,
931
0
                       "Failed to process cdef frame");
932
0
}
933
934
// Updates the row index of the next job to be processed.
935
// Also updates end_of_frame flag when the processing of all rows is complete.
936
static void update_cdef_row_next_job_info(AV1CdefSync *const cdef_sync,
937
0
                                          const int nvfb) {
938
0
  cdef_sync->fbr++;
939
0
  if (cdef_sync->fbr == nvfb) {
940
0
    cdef_sync->end_of_frame = 1;
941
0
  }
942
0
}
943
944
// Checks if a job is available. If job is available,
945
// populates next job information and returns 1, else returns 0.
946
static AOM_INLINE int get_cdef_row_next_job(AV1CdefSync *const cdef_sync,
947
0
                                            int *cur_fbr, const int nvfb) {
948
0
#if CONFIG_MULTITHREAD
949
0
  pthread_mutex_lock(cdef_sync->mutex_);
950
0
#endif  // CONFIG_MULTITHREAD
951
0
  int do_next_row = 0;
952
  // Populates information needed for current job and update the row
953
  // index of the next row to be processed.
954
0
  if (cdef_sync->end_of_frame == 0) {
955
0
    do_next_row = 1;
956
0
    *cur_fbr = cdef_sync->fbr;
957
0
    update_cdef_row_next_job_info(cdef_sync, nvfb);
958
0
  }
959
0
#if CONFIG_MULTITHREAD
960
0
  pthread_mutex_unlock(cdef_sync->mutex_);
961
0
#endif  // CONFIG_MULTITHREAD
962
0
  return do_next_row;
963
0
}
964
965
// Hook function for each thread in CDEF multi-threading.
966
0
static int cdef_sb_row_worker_hook(void *arg1, void *arg2) {
967
0
  AV1CdefSync *const cdef_sync = (AV1CdefSync *)arg1;
968
0
  AV1CdefWorkerData *const cdef_worker = (AV1CdefWorkerData *)arg2;
969
0
  const int nvfb =
970
0
      (cdef_worker->cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
971
0
  int cur_fbr;
972
0
  while (get_cdef_row_next_job(cdef_sync, &cur_fbr, nvfb)) {
973
0
    av1_cdef_fb_row(cdef_worker->cm, cdef_worker->xd, cdef_worker->linebuf,
974
0
                    cdef_worker->colbuf, cdef_worker->srcbuf, cur_fbr,
975
0
                    cdef_worker->cdef_init_fb_row_fn, cdef_sync);
976
0
  }
977
0
  return 1;
978
0
}
979
980
// Assigns CDEF hook function and thread data to each worker.
981
static void prepare_cdef_frame_workers(
982
    AV1_COMMON *const cm, MACROBLOCKD *xd, AV1CdefWorkerData *const cdef_worker,
983
    AVxWorkerHook hook, AVxWorker *const workers, AV1CdefSync *const cdef_sync,
984
0
    int num_workers, cdef_init_fb_row_t cdef_init_fb_row_fn) {
985
0
  const int num_planes = av1_num_planes(cm);
986
987
0
  cdef_worker[0].srcbuf = cm->cdef_info.srcbuf;
988
0
  for (int plane = 0; plane < num_planes; plane++)
989
0
    cdef_worker[0].colbuf[plane] = cm->cdef_info.colbuf[plane];
990
0
  for (int i = num_workers - 1; i >= 0; i--) {
991
0
    AVxWorker *const worker = &workers[i];
992
0
    cdef_worker[i].cm = cm;
993
0
    cdef_worker[i].xd = xd;
994
0
    cdef_worker[i].cdef_init_fb_row_fn = cdef_init_fb_row_fn;
995
0
    for (int plane = 0; plane < num_planes; plane++)
996
0
      cdef_worker[i].linebuf[plane] = cm->cdef_info.linebuf[plane];
997
998
0
    worker->hook = hook;
999
0
    worker->data1 = cdef_sync;
1000
0
    worker->data2 = &cdef_worker[i];
1001
0
  }
1002
0
}
1003
1004
// Initializes row-level parameters for CDEF frame.
1005
void av1_cdef_init_fb_row_mt(const AV1_COMMON *const cm,
1006
                             const MACROBLOCKD *const xd,
1007
                             CdefBlockInfo *const fb_info,
1008
                             uint16_t **const linebuf, uint16_t *const src,
1009
0
                             struct AV1CdefSyncData *const cdef_sync, int fbr) {
1010
0
  const int num_planes = av1_num_planes(cm);
1011
0
  const int nvfb = (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
1012
0
  const int luma_stride =
1013
0
      ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols << MI_SIZE_LOG2, 4);
1014
1015
  // for the current filter block, it's top left corner mi structure (mi_tl)
1016
  // is first accessed to check whether the top and left boundaries are
1017
  // frame boundaries. Then bottom-left and top-right mi structures are
1018
  // accessed to check whether the bottom and right boundaries
1019
  // (respectively) are frame boundaries.
1020
  //
1021
  // Note that we can't just check the bottom-right mi structure - eg. if
1022
  // we're at the right-hand edge of the frame but not the bottom, then
1023
  // the bottom-right mi is NULL but the bottom-left is not.
1024
0
  fb_info->frame_boundary[TOP] = (MI_SIZE_64X64 * fbr == 0) ? 1 : 0;
1025
0
  if (fbr != nvfb - 1)
1026
0
    fb_info->frame_boundary[BOTTOM] =
1027
0
        (MI_SIZE_64X64 * (fbr + 1) == cm->mi_params.mi_rows) ? 1 : 0;
1028
0
  else
1029
0
    fb_info->frame_boundary[BOTTOM] = 1;
1030
1031
0
  fb_info->src = src;
1032
0
  fb_info->damping = cm->cdef_info.cdef_damping;
1033
0
  fb_info->coeff_shift = AOMMAX(cm->seq_params->bit_depth - 8, 0);
1034
0
  av1_zero(fb_info->dir);
1035
0
  av1_zero(fb_info->var);
1036
1037
0
  for (int plane = 0; plane < num_planes; plane++) {
1038
0
    const int stride = luma_stride >> xd->plane[plane].subsampling_x;
1039
0
    uint16_t *top_linebuf = &linebuf[plane][0];
1040
0
    uint16_t *bot_linebuf = &linebuf[plane][nvfb * CDEF_VBORDER * stride];
1041
0
    {
1042
0
      const int mi_high_l2 = MI_SIZE_LOG2 - xd->plane[plane].subsampling_y;
1043
0
      const int top_offset = MI_SIZE_64X64 * (fbr + 1) << mi_high_l2;
1044
0
      const int bot_offset = MI_SIZE_64X64 * (fbr + 1) << mi_high_l2;
1045
1046
0
      if (fbr != nvfb - 1)  // if (fbr != 0)  // top line buffer copy
1047
0
        av1_cdef_copy_sb8_16(
1048
0
            cm, &top_linebuf[(fbr + 1) * CDEF_VBORDER * stride], stride,
1049
0
            xd->plane[plane].dst.buf, top_offset - CDEF_VBORDER, 0,
1050
0
            xd->plane[plane].dst.stride, CDEF_VBORDER, stride);
1051
0
      if (fbr != nvfb - 1)  // bottom line buffer copy
1052
0
        av1_cdef_copy_sb8_16(cm, &bot_linebuf[fbr * CDEF_VBORDER * stride],
1053
0
                             stride, xd->plane[plane].dst.buf, bot_offset, 0,
1054
0
                             xd->plane[plane].dst.stride, CDEF_VBORDER, stride);
1055
0
    }
1056
1057
0
    fb_info->top_linebuf[plane] = &linebuf[plane][fbr * CDEF_VBORDER * stride];
1058
0
    fb_info->bot_linebuf[plane] =
1059
0
        &linebuf[plane]
1060
0
                [nvfb * CDEF_VBORDER * stride + (fbr * CDEF_VBORDER * stride)];
1061
0
  }
1062
1063
0
  cdef_row_mt_sync_write(cdef_sync, fbr);
1064
0
  cdef_row_mt_sync_read(cdef_sync, fbr);
1065
0
}
1066
1067
// Implements multi-threading for CDEF.
1068
// Perform CDEF on input frame.
1069
// Inputs:
1070
//   frame: Pointer to input frame buffer.
1071
//   cm: Pointer to common structure.
1072
//   xd: Pointer to common current coding block structure.
1073
// Returns:
1074
//   Nothing will be returned.
1075
void av1_cdef_frame_mt(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1076
                       AV1CdefWorkerData *const cdef_worker,
1077
                       AVxWorker *const workers, AV1CdefSync *const cdef_sync,
1078
                       int num_workers,
1079
0
                       cdef_init_fb_row_t cdef_init_fb_row_fn) {
1080
0
  YV12_BUFFER_CONFIG *frame = &cm->cur_frame->buf;
1081
0
  const int num_planes = av1_num_planes(cm);
1082
1083
0
  av1_setup_dst_planes(xd->plane, cm->seq_params->sb_size, frame, 0, 0, 0,
1084
0
                       num_planes);
1085
1086
0
  reset_cdef_job_info(cdef_sync);
1087
0
  prepare_cdef_frame_workers(cm, xd, cdef_worker, cdef_sb_row_worker_hook,
1088
0
                             workers, cdef_sync, num_workers,
1089
0
                             cdef_init_fb_row_fn);
1090
0
  launch_cdef_workers(workers, num_workers);
1091
0
  sync_cdef_workers(workers, cm, num_workers);
1092
0
}