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