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.
get_sync_range(int width)23 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 if (width < 640)
27 return 1;
28 else if (width <= 1280)
29 return 2;
30 else if (width <= 4096)
31 return 4;
32 else
33 return 8;
34 }
35
get_lr_sync_range(int width)36 static INLINE int get_lr_sync_range(int width) {
37 #if 0
38 // nsync numbers are picked by testing. For example, for 4k
39 // video, using 4 gives best performance.
40 if (width < 640)
41 return 1;
42 else if (width <= 1280)
43 return 2;
44 else if (width <= 4096)
45 return 4;
46 else
47 return 8;
48 #else
49 (void)width;
50 return 1;
51 #endif
52 }
53
54 // Allocate memory for lf row synchronization
loop_filter_alloc(AV1LfSync * lf_sync,AV1_COMMON * cm,int rows,int width,int num_workers)55 static void loop_filter_alloc(AV1LfSync *lf_sync, AV1_COMMON *cm, int rows,
56 int width, int num_workers) {
57 lf_sync->rows = rows;
58 #if CONFIG_MULTITHREAD
59 {
60 int i, j;
61
62 for (j = 0; j < MAX_MB_PLANE; j++) {
63 CHECK_MEM_ERROR(cm, lf_sync->mutex_[j],
64 aom_malloc(sizeof(*(lf_sync->mutex_[j])) * rows));
65 if (lf_sync->mutex_[j]) {
66 for (i = 0; i < rows; ++i) {
67 pthread_mutex_init(&lf_sync->mutex_[j][i], NULL);
68 }
69 }
70
71 CHECK_MEM_ERROR(cm, lf_sync->cond_[j],
72 aom_malloc(sizeof(*(lf_sync->cond_[j])) * rows));
73 if (lf_sync->cond_[j]) {
74 for (i = 0; i < rows; ++i) {
75 pthread_cond_init(&lf_sync->cond_[j][i], NULL);
76 }
77 }
78 }
79
80 CHECK_MEM_ERROR(cm, lf_sync->job_mutex,
81 aom_malloc(sizeof(*(lf_sync->job_mutex))));
82 if (lf_sync->job_mutex) {
83 pthread_mutex_init(lf_sync->job_mutex, NULL);
84 }
85 }
86 #endif // CONFIG_MULTITHREAD
87 CHECK_MEM_ERROR(cm, lf_sync->lfdata,
88 aom_malloc(num_workers * sizeof(*(lf_sync->lfdata))));
89 lf_sync->num_workers = num_workers;
90
91 for (int j = 0; j < MAX_MB_PLANE; j++) {
92 CHECK_MEM_ERROR(cm, lf_sync->cur_sb_col[j],
93 aom_malloc(sizeof(*(lf_sync->cur_sb_col[j])) * rows));
94 }
95 CHECK_MEM_ERROR(
96 cm, lf_sync->job_queue,
97 aom_malloc(sizeof(*(lf_sync->job_queue)) * rows * MAX_MB_PLANE * 2));
98 // Set up nsync.
99 lf_sync->sync_range = get_sync_range(width);
100 }
101
102 // Deallocate lf synchronization related mutex and data
av1_loop_filter_dealloc(AV1LfSync * lf_sync)103 void av1_loop_filter_dealloc(AV1LfSync *lf_sync) {
104 if (lf_sync != NULL) {
105 int j;
106 #if CONFIG_MULTITHREAD
107 int i;
108 for (j = 0; j < MAX_MB_PLANE; j++) {
109 if (lf_sync->mutex_[j] != NULL) {
110 for (i = 0; i < lf_sync->rows; ++i) {
111 pthread_mutex_destroy(&lf_sync->mutex_[j][i]);
112 }
113 aom_free(lf_sync->mutex_[j]);
114 }
115 if (lf_sync->cond_[j] != NULL) {
116 for (i = 0; i < lf_sync->rows; ++i) {
117 pthread_cond_destroy(&lf_sync->cond_[j][i]);
118 }
119 aom_free(lf_sync->cond_[j]);
120 }
121 }
122 if (lf_sync->job_mutex != NULL) {
123 pthread_mutex_destroy(lf_sync->job_mutex);
124 aom_free(lf_sync->job_mutex);
125 }
126 #endif // CONFIG_MULTITHREAD
127 aom_free(lf_sync->lfdata);
128 for (j = 0; j < MAX_MB_PLANE; j++) {
129 aom_free(lf_sync->cur_sb_col[j]);
130 }
131
132 aom_free(lf_sync->job_queue);
133 // clear the structure as the source of this call may be a resize in which
134 // case this call will be followed by an _alloc() which may fail.
135 av1_zero(*lf_sync);
136 }
137 }
138
loop_filter_data_reset(LFWorkerData * lf_data,YV12_BUFFER_CONFIG * frame_buffer,struct AV1Common * cm,MACROBLOCKD * xd)139 static void loop_filter_data_reset(LFWorkerData *lf_data,
140 YV12_BUFFER_CONFIG *frame_buffer,
141 struct AV1Common *cm, MACROBLOCKD *xd) {
142 struct macroblockd_plane *pd = xd->plane;
143 lf_data->frame_buffer = frame_buffer;
144 lf_data->cm = cm;
145 lf_data->xd = xd;
146 for (int i = 0; i < MAX_MB_PLANE; i++) {
147 memcpy(&lf_data->planes[i].dst, &pd[i].dst, sizeof(lf_data->planes[i].dst));
148 lf_data->planes[i].subsampling_x = pd[i].subsampling_x;
149 lf_data->planes[i].subsampling_y = pd[i].subsampling_y;
150 }
151 }
152
sync_read(AV1LfSync * const lf_sync,int r,int c,int plane)153 static INLINE void sync_read(AV1LfSync *const lf_sync, int r, int c,
154 int plane) {
155 #if CONFIG_MULTITHREAD
156 const int nsync = lf_sync->sync_range;
157
158 if (r && !(c & (nsync - 1))) {
159 pthread_mutex_t *const mutex = &lf_sync->mutex_[plane][r - 1];
160 pthread_mutex_lock(mutex);
161
162 while (c > lf_sync->cur_sb_col[plane][r - 1] - nsync) {
163 pthread_cond_wait(&lf_sync->cond_[plane][r - 1], mutex);
164 }
165 pthread_mutex_unlock(mutex);
166 }
167 #else
168 (void)lf_sync;
169 (void)r;
170 (void)c;
171 (void)plane;
172 #endif // CONFIG_MULTITHREAD
173 }
174
sync_write(AV1LfSync * const lf_sync,int r,int c,const int sb_cols,int plane)175 static INLINE void sync_write(AV1LfSync *const lf_sync, int r, int c,
176 const int sb_cols, int plane) {
177 #if CONFIG_MULTITHREAD
178 const int nsync = lf_sync->sync_range;
179 int cur;
180 // Only signal when there are enough filtered SB for next row to run.
181 int sig = 1;
182
183 if (c < sb_cols - 1) {
184 cur = c;
185 if (c % nsync) sig = 0;
186 } else {
187 cur = sb_cols + nsync;
188 }
189
190 if (sig) {
191 pthread_mutex_lock(&lf_sync->mutex_[plane][r]);
192
193 lf_sync->cur_sb_col[plane][r] = cur;
194
195 pthread_cond_broadcast(&lf_sync->cond_[plane][r]);
196 pthread_mutex_unlock(&lf_sync->mutex_[plane][r]);
197 }
198 #else
199 (void)lf_sync;
200 (void)r;
201 (void)c;
202 (void)sb_cols;
203 (void)plane;
204 #endif // CONFIG_MULTITHREAD
205 }
206
enqueue_lf_jobs(AV1LfSync * lf_sync,AV1_COMMON * cm,int start,int stop,int is_decoding,int plane_start,int plane_end)207 static void enqueue_lf_jobs(AV1LfSync *lf_sync, AV1_COMMON *cm, int start,
208 int stop,
209 #if CONFIG_LPF_MASK
210 int is_decoding,
211 #endif
212 int plane_start, int plane_end) {
213 int mi_row, plane, dir;
214 AV1LfMTInfo *lf_job_queue = lf_sync->job_queue;
215 lf_sync->jobs_enqueued = 0;
216 lf_sync->jobs_dequeued = 0;
217
218 for (dir = 0; dir < 2; dir++) {
219 for (plane = plane_start; plane < plane_end; plane++) {
220 if (plane == 0 && !(cm->lf.filter_level[0]) && !(cm->lf.filter_level[1]))
221 break;
222 else if (plane == 1 && !(cm->lf.filter_level_u))
223 continue;
224 else if (plane == 2 && !(cm->lf.filter_level_v))
225 continue;
226 #if CONFIG_LPF_MASK
227 int step = MAX_MIB_SIZE;
228 if (is_decoding) {
229 step = MI_SIZE_64X64;
230 }
231 for (mi_row = start; mi_row < stop; mi_row += step)
232 #else
233 for (mi_row = start; mi_row < stop; mi_row += MAX_MIB_SIZE)
234 #endif
235 {
236 lf_job_queue->mi_row = mi_row;
237 lf_job_queue->plane = plane;
238 lf_job_queue->dir = dir;
239 lf_job_queue++;
240 lf_sync->jobs_enqueued++;
241 }
242 }
243 }
244 }
245
get_lf_job_info(AV1LfSync * lf_sync)246 static AV1LfMTInfo *get_lf_job_info(AV1LfSync *lf_sync) {
247 AV1LfMTInfo *cur_job_info = NULL;
248
249 #if CONFIG_MULTITHREAD
250 pthread_mutex_lock(lf_sync->job_mutex);
251
252 if (lf_sync->jobs_dequeued < lf_sync->jobs_enqueued) {
253 cur_job_info = lf_sync->job_queue + lf_sync->jobs_dequeued;
254 lf_sync->jobs_dequeued++;
255 }
256
257 pthread_mutex_unlock(lf_sync->job_mutex);
258 #else
259 (void)lf_sync;
260 #endif
261
262 return cur_job_info;
263 }
264
265 // Implement row loopfiltering for each thread.
thread_loop_filter_rows(const YV12_BUFFER_CONFIG * const frame_buffer,AV1_COMMON * const cm,struct macroblockd_plane * planes,MACROBLOCKD * xd,AV1LfSync * const lf_sync)266 static INLINE void thread_loop_filter_rows(
267 const YV12_BUFFER_CONFIG *const frame_buffer, AV1_COMMON *const cm,
268 struct macroblockd_plane *planes, MACROBLOCKD *xd,
269 AV1LfSync *const lf_sync) {
270 const int sb_cols =
271 ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols, MAX_MIB_SIZE_LOG2) >>
272 MAX_MIB_SIZE_LOG2;
273 int mi_row, mi_col, plane, dir;
274 int r, c;
275
276 while (1) {
277 AV1LfMTInfo *cur_job_info = get_lf_job_info(lf_sync);
278
279 if (cur_job_info != NULL) {
280 mi_row = cur_job_info->mi_row;
281 plane = cur_job_info->plane;
282 dir = cur_job_info->dir;
283 r = mi_row >> MAX_MIB_SIZE_LOG2;
284
285 if (dir == 0) {
286 for (mi_col = 0; mi_col < cm->mi_params.mi_cols;
287 mi_col += MAX_MIB_SIZE) {
288 c = mi_col >> MAX_MIB_SIZE_LOG2;
289
290 av1_setup_dst_planes(planes, cm->seq_params.sb_size, frame_buffer,
291 mi_row, mi_col, plane, plane + 1);
292
293 av1_filter_block_plane_vert(cm, xd, plane, &planes[plane], mi_row,
294 mi_col);
295 sync_write(lf_sync, r, c, sb_cols, plane);
296 }
297 } else if (dir == 1) {
298 for (mi_col = 0; mi_col < cm->mi_params.mi_cols;
299 mi_col += MAX_MIB_SIZE) {
300 c = mi_col >> MAX_MIB_SIZE_LOG2;
301
302 // Wait for vertical edge filtering of the top-right block to be
303 // completed
304 sync_read(lf_sync, r, c, plane);
305
306 // Wait for vertical edge filtering of the right block to be
307 // completed
308 sync_read(lf_sync, r + 1, c, plane);
309
310 av1_setup_dst_planes(planes, cm->seq_params.sb_size, frame_buffer,
311 mi_row, mi_col, plane, plane + 1);
312 av1_filter_block_plane_horz(cm, xd, plane, &planes[plane], mi_row,
313 mi_col);
314 }
315 }
316 } else {
317 break;
318 }
319 }
320 }
321
322 // Row-based multi-threaded loopfilter hook
loop_filter_row_worker(void * arg1,void * arg2)323 static int loop_filter_row_worker(void *arg1, void *arg2) {
324 AV1LfSync *const lf_sync = (AV1LfSync *)arg1;
325 LFWorkerData *const lf_data = (LFWorkerData *)arg2;
326 thread_loop_filter_rows(lf_data->frame_buffer, lf_data->cm, lf_data->planes,
327 lf_data->xd, lf_sync);
328 return 1;
329 }
330
331 #if CONFIG_LPF_MASK
thread_loop_filter_bitmask_rows(const YV12_BUFFER_CONFIG * const frame_buffer,AV1_COMMON * const cm,struct macroblockd_plane * planes,MACROBLOCKD * xd,AV1LfSync * const lf_sync)332 static INLINE void thread_loop_filter_bitmask_rows(
333 const YV12_BUFFER_CONFIG *const frame_buffer, AV1_COMMON *const cm,
334 struct macroblockd_plane *planes, MACROBLOCKD *xd,
335 AV1LfSync *const lf_sync) {
336 const int sb_cols =
337 ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols, MIN_MIB_SIZE_LOG2) >>
338 MIN_MIB_SIZE_LOG2;
339 int mi_row, mi_col, plane, dir;
340 int r, c;
341 (void)xd;
342
343 while (1) {
344 AV1LfMTInfo *cur_job_info = get_lf_job_info(lf_sync);
345
346 if (cur_job_info != NULL) {
347 mi_row = cur_job_info->mi_row;
348 plane = cur_job_info->plane;
349 dir = cur_job_info->dir;
350 r = mi_row >> MIN_MIB_SIZE_LOG2;
351
352 if (dir == 0) {
353 for (mi_col = 0; mi_col < cm->mi_params.mi_cols;
354 mi_col += MI_SIZE_64X64) {
355 c = mi_col >> MIN_MIB_SIZE_LOG2;
356
357 av1_setup_dst_planes(planes, BLOCK_64X64, frame_buffer, mi_row,
358 mi_col, plane, plane + 1);
359
360 av1_filter_block_plane_bitmask_vert(cm, &planes[plane], plane, mi_row,
361 mi_col);
362 sync_write(lf_sync, r, c, sb_cols, plane);
363 }
364 } else if (dir == 1) {
365 for (mi_col = 0; mi_col < cm->mi_params.mi_cols;
366 mi_col += MI_SIZE_64X64) {
367 c = mi_col >> MIN_MIB_SIZE_LOG2;
368
369 // Wait for vertical edge filtering of the top-right block to be
370 // completed
371 sync_read(lf_sync, r, c, plane);
372
373 // Wait for vertical edge filtering of the right block to be
374 // completed
375 sync_read(lf_sync, r + 1, c, plane);
376
377 av1_setup_dst_planes(planes, BLOCK_64X64, frame_buffer, mi_row,
378 mi_col, plane, plane + 1);
379 av1_filter_block_plane_bitmask_horz(cm, &planes[plane], plane, mi_row,
380 mi_col);
381 }
382 }
383 } else {
384 break;
385 }
386 }
387 }
388
389 // Row-based multi-threaded loopfilter hook
loop_filter_bitmask_row_worker(void * arg1,void * arg2)390 static int loop_filter_bitmask_row_worker(void *arg1, void *arg2) {
391 AV1LfSync *const lf_sync = (AV1LfSync *)arg1;
392 LFWorkerData *const lf_data = (LFWorkerData *)arg2;
393 thread_loop_filter_bitmask_rows(lf_data->frame_buffer, lf_data->cm,
394 lf_data->planes, lf_data->xd, lf_sync);
395 return 1;
396 }
397 #endif // CONFIG_LPF_MASK
398
loop_filter_rows_mt(YV12_BUFFER_CONFIG * frame,AV1_COMMON * cm,MACROBLOCKD * xd,int start,int stop,int plane_start,int plane_end,int is_decoding,AVxWorker * workers,int nworkers,AV1LfSync * lf_sync)399 static void loop_filter_rows_mt(YV12_BUFFER_CONFIG *frame, AV1_COMMON *cm,
400 MACROBLOCKD *xd, int start, int stop,
401 int plane_start, int plane_end,
402 #if CONFIG_LPF_MASK
403 int is_decoding,
404 #endif
405 AVxWorker *workers, int nworkers,
406 AV1LfSync *lf_sync) {
407 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
408 #if CONFIG_LPF_MASK
409 int sb_rows;
410 if (is_decoding) {
411 sb_rows = ALIGN_POWER_OF_TWO(cm->mi_params.mi_rows, MIN_MIB_SIZE_LOG2) >>
412 MIN_MIB_SIZE_LOG2;
413 } else {
414 sb_rows = ALIGN_POWER_OF_TWO(cm->mi_params.mi_rows, MAX_MIB_SIZE_LOG2) >>
415 MAX_MIB_SIZE_LOG2;
416 }
417 #else
418 // Number of superblock rows and cols
419 const int sb_rows =
420 ALIGN_POWER_OF_TWO(cm->mi_params.mi_rows, MAX_MIB_SIZE_LOG2) >>
421 MAX_MIB_SIZE_LOG2;
422 #endif
423 const int num_workers = nworkers;
424 int i;
425
426 if (!lf_sync->sync_range || sb_rows != lf_sync->rows ||
427 num_workers > lf_sync->num_workers) {
428 av1_loop_filter_dealloc(lf_sync);
429 loop_filter_alloc(lf_sync, cm, sb_rows, cm->width, num_workers);
430 }
431
432 // Initialize cur_sb_col to -1 for all SB rows.
433 for (i = 0; i < MAX_MB_PLANE; i++) {
434 memset(lf_sync->cur_sb_col[i], -1,
435 sizeof(*(lf_sync->cur_sb_col[i])) * sb_rows);
436 }
437
438 enqueue_lf_jobs(lf_sync, cm, start, stop,
439 #if CONFIG_LPF_MASK
440 is_decoding,
441 #endif
442 plane_start, plane_end);
443
444 // Set up loopfilter thread data.
445 for (i = 0; i < num_workers; ++i) {
446 AVxWorker *const worker = &workers[i];
447 LFWorkerData *const lf_data = &lf_sync->lfdata[i];
448
449 #if CONFIG_LPF_MASK
450 if (is_decoding) {
451 worker->hook = loop_filter_bitmask_row_worker;
452 } else {
453 worker->hook = loop_filter_row_worker;
454 }
455 #else
456 worker->hook = loop_filter_row_worker;
457 #endif
458 worker->data1 = lf_sync;
459 worker->data2 = lf_data;
460
461 // Loopfilter data
462 loop_filter_data_reset(lf_data, frame, cm, xd);
463
464 // Start loopfiltering
465 if (i == num_workers - 1) {
466 winterface->execute(worker);
467 } else {
468 winterface->launch(worker);
469 }
470 }
471
472 // Wait till all rows are finished
473 for (i = 0; i < num_workers; ++i) {
474 winterface->sync(&workers[i]);
475 }
476 }
477
av1_loop_filter_frame_mt(YV12_BUFFER_CONFIG * frame,AV1_COMMON * cm,MACROBLOCKD * xd,int plane_start,int plane_end,int partial_frame,int is_decoding,AVxWorker * workers,int num_workers,AV1LfSync * lf_sync)478 void av1_loop_filter_frame_mt(YV12_BUFFER_CONFIG *frame, AV1_COMMON *cm,
479 MACROBLOCKD *xd, int plane_start, int plane_end,
480 int partial_frame,
481 #if CONFIG_LPF_MASK
482 int is_decoding,
483 #endif
484 AVxWorker *workers, int num_workers,
485 AV1LfSync *lf_sync) {
486 int start_mi_row, end_mi_row, mi_rows_to_filter;
487
488 start_mi_row = 0;
489 mi_rows_to_filter = cm->mi_params.mi_rows;
490 if (partial_frame && cm->mi_params.mi_rows > 8) {
491 start_mi_row = cm->mi_params.mi_rows >> 1;
492 start_mi_row &= 0xfffffff8;
493 mi_rows_to_filter = AOMMAX(cm->mi_params.mi_rows / 8, 8);
494 }
495 end_mi_row = start_mi_row + mi_rows_to_filter;
496 av1_loop_filter_frame_init(cm, plane_start, plane_end);
497
498 #if CONFIG_LPF_MASK
499 if (is_decoding) {
500 cm->is_decoding = is_decoding;
501 // TODO(chengchen): currently use one thread to build bitmasks for the
502 // frame. Make it support multi-thread later.
503 for (int plane = plane_start; plane < plane_end; plane++) {
504 if (plane == 0 && !(cm->lf.filter_level[0]) && !(cm->lf.filter_level[1]))
505 break;
506 else if (plane == 1 && !(cm->lf.filter_level_u))
507 continue;
508 else if (plane == 2 && !(cm->lf.filter_level_v))
509 continue;
510
511 // TODO(chengchen): can we remove this?
512 struct macroblockd_plane *pd = xd->plane;
513 av1_setup_dst_planes(pd, cm->seq_params.sb_size, frame, 0, 0, plane,
514 plane + 1);
515
516 av1_build_bitmask_vert_info(cm, &pd[plane], plane);
517 av1_build_bitmask_horz_info(cm, &pd[plane], plane);
518 }
519 loop_filter_rows_mt(frame, cm, xd, start_mi_row, end_mi_row, plane_start,
520 plane_end, 1, workers, num_workers, lf_sync);
521 } else {
522 loop_filter_rows_mt(frame, cm, xd, start_mi_row, end_mi_row, plane_start,
523 plane_end, 0, workers, num_workers, lf_sync);
524 }
525 #else
526 loop_filter_rows_mt(frame, cm, xd, start_mi_row, end_mi_row, plane_start,
527 plane_end, workers, num_workers, lf_sync);
528 #endif
529 }
530
lr_sync_read(void * const lr_sync,int r,int c,int plane)531 static INLINE void lr_sync_read(void *const lr_sync, int r, int c, int plane) {
532 #if CONFIG_MULTITHREAD
533 AV1LrSync *const loop_res_sync = (AV1LrSync *)lr_sync;
534 const int nsync = loop_res_sync->sync_range;
535
536 if (r && !(c & (nsync - 1))) {
537 pthread_mutex_t *const mutex = &loop_res_sync->mutex_[plane][r - 1];
538 pthread_mutex_lock(mutex);
539
540 while (c > loop_res_sync->cur_sb_col[plane][r - 1] - nsync) {
541 pthread_cond_wait(&loop_res_sync->cond_[plane][r - 1], mutex);
542 }
543 pthread_mutex_unlock(mutex);
544 }
545 #else
546 (void)lr_sync;
547 (void)r;
548 (void)c;
549 (void)plane;
550 #endif // CONFIG_MULTITHREAD
551 }
552
lr_sync_write(void * const lr_sync,int r,int c,const int sb_cols,int plane)553 static INLINE void lr_sync_write(void *const lr_sync, int r, int c,
554 const int sb_cols, int plane) {
555 #if CONFIG_MULTITHREAD
556 AV1LrSync *const loop_res_sync = (AV1LrSync *)lr_sync;
557 const int nsync = loop_res_sync->sync_range;
558 int cur;
559 // Only signal when there are enough filtered SB for next row to run.
560 int sig = 1;
561
562 if (c < sb_cols - 1) {
563 cur = c;
564 if (c % nsync) sig = 0;
565 } else {
566 cur = sb_cols + nsync;
567 }
568
569 if (sig) {
570 pthread_mutex_lock(&loop_res_sync->mutex_[plane][r]);
571
572 loop_res_sync->cur_sb_col[plane][r] = cur;
573
574 pthread_cond_broadcast(&loop_res_sync->cond_[plane][r]);
575 pthread_mutex_unlock(&loop_res_sync->mutex_[plane][r]);
576 }
577 #else
578 (void)lr_sync;
579 (void)r;
580 (void)c;
581 (void)sb_cols;
582 (void)plane;
583 #endif // CONFIG_MULTITHREAD
584 }
585
586 // Allocate memory for loop restoration row synchronization
loop_restoration_alloc(AV1LrSync * lr_sync,AV1_COMMON * cm,int num_workers,int num_rows_lr,int num_planes,int width)587 static void loop_restoration_alloc(AV1LrSync *lr_sync, AV1_COMMON *cm,
588 int num_workers, int num_rows_lr,
589 int num_planes, int width) {
590 lr_sync->rows = num_rows_lr;
591 lr_sync->num_planes = num_planes;
592 #if CONFIG_MULTITHREAD
593 {
594 int i, j;
595
596 for (j = 0; j < num_planes; j++) {
597 CHECK_MEM_ERROR(cm, lr_sync->mutex_[j],
598 aom_malloc(sizeof(*(lr_sync->mutex_[j])) * num_rows_lr));
599 if (lr_sync->mutex_[j]) {
600 for (i = 0; i < num_rows_lr; ++i) {
601 pthread_mutex_init(&lr_sync->mutex_[j][i], NULL);
602 }
603 }
604
605 CHECK_MEM_ERROR(cm, lr_sync->cond_[j],
606 aom_malloc(sizeof(*(lr_sync->cond_[j])) * num_rows_lr));
607 if (lr_sync->cond_[j]) {
608 for (i = 0; i < num_rows_lr; ++i) {
609 pthread_cond_init(&lr_sync->cond_[j][i], NULL);
610 }
611 }
612 }
613
614 CHECK_MEM_ERROR(cm, lr_sync->job_mutex,
615 aom_malloc(sizeof(*(lr_sync->job_mutex))));
616 if (lr_sync->job_mutex) {
617 pthread_mutex_init(lr_sync->job_mutex, NULL);
618 }
619 }
620 #endif // CONFIG_MULTITHREAD
621 CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata,
622 aom_malloc(num_workers * sizeof(*(lr_sync->lrworkerdata))));
623
624 for (int worker_idx = 0; worker_idx < num_workers; ++worker_idx) {
625 if (worker_idx < num_workers - 1) {
626 CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata[worker_idx].rst_tmpbuf,
627 (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE));
628 CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata[worker_idx].rlbs,
629 aom_malloc(sizeof(RestorationLineBuffers)));
630
631 } else {
632 lr_sync->lrworkerdata[worker_idx].rst_tmpbuf = cm->rst_tmpbuf;
633 lr_sync->lrworkerdata[worker_idx].rlbs = cm->rlbs;
634 }
635 }
636
637 lr_sync->num_workers = num_workers;
638
639 for (int j = 0; j < num_planes; j++) {
640 CHECK_MEM_ERROR(
641 cm, lr_sync->cur_sb_col[j],
642 aom_malloc(sizeof(*(lr_sync->cur_sb_col[j])) * num_rows_lr));
643 }
644 CHECK_MEM_ERROR(
645 cm, lr_sync->job_queue,
646 aom_malloc(sizeof(*(lr_sync->job_queue)) * num_rows_lr * num_planes));
647 // Set up nsync.
648 lr_sync->sync_range = get_lr_sync_range(width);
649 }
650
651 // Deallocate loop restoration synchronization related mutex and data
av1_loop_restoration_dealloc(AV1LrSync * lr_sync,int num_workers)652 void av1_loop_restoration_dealloc(AV1LrSync *lr_sync, int num_workers) {
653 if (lr_sync != NULL) {
654 int j;
655 #if CONFIG_MULTITHREAD
656 int i;
657 for (j = 0; j < MAX_MB_PLANE; j++) {
658 if (lr_sync->mutex_[j] != NULL) {
659 for (i = 0; i < lr_sync->rows; ++i) {
660 pthread_mutex_destroy(&lr_sync->mutex_[j][i]);
661 }
662 aom_free(lr_sync->mutex_[j]);
663 }
664 if (lr_sync->cond_[j] != NULL) {
665 for (i = 0; i < lr_sync->rows; ++i) {
666 pthread_cond_destroy(&lr_sync->cond_[j][i]);
667 }
668 aom_free(lr_sync->cond_[j]);
669 }
670 }
671 if (lr_sync->job_mutex != NULL) {
672 pthread_mutex_destroy(lr_sync->job_mutex);
673 aom_free(lr_sync->job_mutex);
674 }
675 #endif // CONFIG_MULTITHREAD
676 for (j = 0; j < MAX_MB_PLANE; j++) {
677 aom_free(lr_sync->cur_sb_col[j]);
678 }
679
680 aom_free(lr_sync->job_queue);
681
682 if (lr_sync->lrworkerdata) {
683 for (int worker_idx = 0; worker_idx < num_workers - 1; worker_idx++) {
684 LRWorkerData *const workerdata_data =
685 lr_sync->lrworkerdata + worker_idx;
686
687 aom_free(workerdata_data->rst_tmpbuf);
688 aom_free(workerdata_data->rlbs);
689 }
690 aom_free(lr_sync->lrworkerdata);
691 }
692
693 // clear the structure as the source of this call may be a resize in which
694 // case this call will be followed by an _alloc() which may fail.
695 av1_zero(*lr_sync);
696 }
697 }
698
enqueue_lr_jobs(AV1LrSync * lr_sync,AV1LrStruct * lr_ctxt,AV1_COMMON * cm)699 static void enqueue_lr_jobs(AV1LrSync *lr_sync, AV1LrStruct *lr_ctxt,
700 AV1_COMMON *cm) {
701 FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
702
703 const int num_planes = av1_num_planes(cm);
704 AV1LrMTInfo *lr_job_queue = lr_sync->job_queue;
705 int32_t lr_job_counter[2], num_even_lr_jobs = 0;
706 lr_sync->jobs_enqueued = 0;
707 lr_sync->jobs_dequeued = 0;
708
709 for (int plane = 0; plane < num_planes; plane++) {
710 if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
711 num_even_lr_jobs =
712 num_even_lr_jobs + ((ctxt[plane].rsi->vert_units_per_tile + 1) >> 1);
713 }
714 lr_job_counter[0] = 0;
715 lr_job_counter[1] = num_even_lr_jobs;
716
717 for (int plane = 0; plane < num_planes; plane++) {
718 if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
719 const int is_uv = plane > 0;
720 const int ss_y = is_uv && cm->seq_params.subsampling_y;
721
722 AV1PixelRect tile_rect = ctxt[plane].tile_rect;
723 const int unit_size = ctxt[plane].rsi->restoration_unit_size;
724
725 const int tile_h = tile_rect.bottom - tile_rect.top;
726 const int ext_size = unit_size * 3 / 2;
727
728 int y0 = 0, i = 0;
729 while (y0 < tile_h) {
730 int remaining_h = tile_h - y0;
731 int h = (remaining_h < ext_size) ? remaining_h : unit_size;
732
733 RestorationTileLimits limits;
734 limits.v_start = tile_rect.top + y0;
735 limits.v_end = tile_rect.top + y0 + h;
736 assert(limits.v_end <= tile_rect.bottom);
737 // Offset the tile upwards to align with the restoration processing stripe
738 const int voffset = RESTORATION_UNIT_OFFSET >> ss_y;
739 limits.v_start = AOMMAX(tile_rect.top, limits.v_start - voffset);
740 if (limits.v_end < tile_rect.bottom) limits.v_end -= voffset;
741
742 assert(lr_job_counter[0] <= num_even_lr_jobs);
743
744 lr_job_queue[lr_job_counter[i & 1]].lr_unit_row = i;
745 lr_job_queue[lr_job_counter[i & 1]].plane = plane;
746 lr_job_queue[lr_job_counter[i & 1]].v_start = limits.v_start;
747 lr_job_queue[lr_job_counter[i & 1]].v_end = limits.v_end;
748 lr_job_queue[lr_job_counter[i & 1]].sync_mode = i & 1;
749 if ((i & 1) == 0) {
750 lr_job_queue[lr_job_counter[i & 1]].v_copy_start =
751 limits.v_start + RESTORATION_BORDER;
752 lr_job_queue[lr_job_counter[i & 1]].v_copy_end =
753 limits.v_end - RESTORATION_BORDER;
754 if (i == 0) {
755 assert(limits.v_start == tile_rect.top);
756 lr_job_queue[lr_job_counter[i & 1]].v_copy_start = tile_rect.top;
757 }
758 if (i == (ctxt[plane].rsi->vert_units_per_tile - 1)) {
759 assert(limits.v_end == tile_rect.bottom);
760 lr_job_queue[lr_job_counter[i & 1]].v_copy_end = tile_rect.bottom;
761 }
762 } else {
763 lr_job_queue[lr_job_counter[i & 1]].v_copy_start =
764 AOMMAX(limits.v_start - RESTORATION_BORDER, tile_rect.top);
765 lr_job_queue[lr_job_counter[i & 1]].v_copy_end =
766 AOMMIN(limits.v_end + RESTORATION_BORDER, tile_rect.bottom);
767 }
768 lr_job_counter[i & 1]++;
769 lr_sync->jobs_enqueued++;
770
771 y0 += h;
772 ++i;
773 }
774 }
775 }
776
get_lr_job_info(AV1LrSync * lr_sync)777 static AV1LrMTInfo *get_lr_job_info(AV1LrSync *lr_sync) {
778 AV1LrMTInfo *cur_job_info = NULL;
779
780 #if CONFIG_MULTITHREAD
781 pthread_mutex_lock(lr_sync->job_mutex);
782
783 if (lr_sync->jobs_dequeued < lr_sync->jobs_enqueued) {
784 cur_job_info = lr_sync->job_queue + lr_sync->jobs_dequeued;
785 lr_sync->jobs_dequeued++;
786 }
787
788 pthread_mutex_unlock(lr_sync->job_mutex);
789 #else
790 (void)lr_sync;
791 #endif
792
793 return cur_job_info;
794 }
795
796 // Implement row loop restoration for each thread.
loop_restoration_row_worker(void * arg1,void * arg2)797 static int loop_restoration_row_worker(void *arg1, void *arg2) {
798 AV1LrSync *const lr_sync = (AV1LrSync *)arg1;
799 LRWorkerData *lrworkerdata = (LRWorkerData *)arg2;
800 AV1LrStruct *lr_ctxt = (AV1LrStruct *)lrworkerdata->lr_ctxt;
801 FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
802 int lr_unit_row;
803 int plane;
804 const int tile_row = LR_TILE_ROW;
805 const int tile_col = LR_TILE_COL;
806 const int tile_cols = LR_TILE_COLS;
807 const int tile_idx = tile_col + tile_row * tile_cols;
808 typedef void (*copy_fun)(const YV12_BUFFER_CONFIG *src_ybc,
809 YV12_BUFFER_CONFIG *dst_ybc, int hstart, int hend,
810 int vstart, int vend);
811 static const copy_fun copy_funs[3] = { aom_yv12_partial_coloc_copy_y,
812 aom_yv12_partial_coloc_copy_u,
813 aom_yv12_partial_coloc_copy_v };
814
815 while (1) {
816 AV1LrMTInfo *cur_job_info = get_lr_job_info(lr_sync);
817 if (cur_job_info != NULL) {
818 RestorationTileLimits limits;
819 sync_read_fn_t on_sync_read;
820 sync_write_fn_t on_sync_write;
821 limits.v_start = cur_job_info->v_start;
822 limits.v_end = cur_job_info->v_end;
823 lr_unit_row = cur_job_info->lr_unit_row;
824 plane = cur_job_info->plane;
825 const int unit_idx0 = tile_idx * ctxt[plane].rsi->units_per_tile;
826
827 // sync_mode == 1 implies only sync read is required in LR Multi-threading
828 // sync_mode == 0 implies only sync write is required.
829 on_sync_read =
830 cur_job_info->sync_mode == 1 ? lr_sync_read : av1_lr_sync_read_dummy;
831 on_sync_write = cur_job_info->sync_mode == 0 ? lr_sync_write
832 : av1_lr_sync_write_dummy;
833
834 av1_foreach_rest_unit_in_row(
835 &limits, &(ctxt[plane].tile_rect), lr_ctxt->on_rest_unit, lr_unit_row,
836 ctxt[plane].rsi->restoration_unit_size, unit_idx0,
837 ctxt[plane].rsi->horz_units_per_tile,
838 ctxt[plane].rsi->vert_units_per_tile, plane, &ctxt[plane],
839 lrworkerdata->rst_tmpbuf, lrworkerdata->rlbs, on_sync_read,
840 on_sync_write, lr_sync);
841
842 copy_funs[plane](lr_ctxt->dst, lr_ctxt->frame, ctxt[plane].tile_rect.left,
843 ctxt[plane].tile_rect.right, cur_job_info->v_copy_start,
844 cur_job_info->v_copy_end);
845 } else {
846 break;
847 }
848 }
849 return 1;
850 }
851
foreach_rest_unit_in_planes_mt(AV1LrStruct * lr_ctxt,AVxWorker * workers,int nworkers,AV1LrSync * lr_sync,AV1_COMMON * cm)852 static void foreach_rest_unit_in_planes_mt(AV1LrStruct *lr_ctxt,
853 AVxWorker *workers, int nworkers,
854 AV1LrSync *lr_sync, AV1_COMMON *cm) {
855 FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
856
857 const int num_planes = av1_num_planes(cm);
858
859 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
860 int num_rows_lr = 0;
861
862 for (int plane = 0; plane < num_planes; plane++) {
863 if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
864
865 const AV1PixelRect tile_rect = ctxt[plane].tile_rect;
866 const int max_tile_h = tile_rect.bottom - tile_rect.top;
867
868 const int unit_size = cm->rst_info[plane].restoration_unit_size;
869
870 num_rows_lr =
871 AOMMAX(num_rows_lr, av1_lr_count_units_in_tile(unit_size, max_tile_h));
872 }
873
874 const int num_workers = nworkers;
875 int i;
876 assert(MAX_MB_PLANE == 3);
877
878 if (!lr_sync->sync_range || num_rows_lr != lr_sync->rows ||
879 num_workers > lr_sync->num_workers || num_planes != lr_sync->num_planes) {
880 av1_loop_restoration_dealloc(lr_sync, num_workers);
881 loop_restoration_alloc(lr_sync, cm, num_workers, num_rows_lr, num_planes,
882 cm->width);
883 }
884
885 // Initialize cur_sb_col to -1 for all SB rows.
886 for (i = 0; i < num_planes; i++) {
887 memset(lr_sync->cur_sb_col[i], -1,
888 sizeof(*(lr_sync->cur_sb_col[i])) * num_rows_lr);
889 }
890
891 enqueue_lr_jobs(lr_sync, lr_ctxt, cm);
892
893 // Set up looprestoration thread data.
894 for (i = 0; i < num_workers; ++i) {
895 AVxWorker *const worker = &workers[i];
896 lr_sync->lrworkerdata[i].lr_ctxt = (void *)lr_ctxt;
897 worker->hook = loop_restoration_row_worker;
898 worker->data1 = lr_sync;
899 worker->data2 = &lr_sync->lrworkerdata[i];
900
901 // Start loopfiltering
902 if (i == num_workers - 1) {
903 winterface->execute(worker);
904 } else {
905 winterface->launch(worker);
906 }
907 }
908
909 // Wait till all rows are finished
910 for (i = 0; i < num_workers; ++i) {
911 winterface->sync(&workers[i]);
912 }
913 }
914
av1_loop_restoration_filter_frame_mt(YV12_BUFFER_CONFIG * frame,AV1_COMMON * cm,int optimized_lr,AVxWorker * workers,int num_workers,AV1LrSync * lr_sync,void * lr_ctxt)915 void av1_loop_restoration_filter_frame_mt(YV12_BUFFER_CONFIG *frame,
916 AV1_COMMON *cm, int optimized_lr,
917 AVxWorker *workers, int num_workers,
918 AV1LrSync *lr_sync, void *lr_ctxt) {
919 assert(!cm->features.all_lossless);
920
921 const int num_planes = av1_num_planes(cm);
922
923 AV1LrStruct *loop_rest_ctxt = (AV1LrStruct *)lr_ctxt;
924
925 av1_loop_restoration_filter_frame_init(loop_rest_ctxt, frame, cm,
926 optimized_lr, num_planes);
927
928 foreach_rest_unit_in_planes_mt(loop_rest_ctxt, workers, num_workers, lr_sync,
929 cm);
930 }
931