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 "aom/aom_image.h"
13 #include "config/aom_config.h"
14 #include "config/aom_scale_rtcd.h"
15
16 #include "aom_dsp/aom_dsp_common.h"
17 #include "aom_dsp/txfm_common.h"
18 #include "aom_mem/aom_mem.h"
19 #include "aom_util/aom_pthread.h"
20 #include "aom_util/aom_thread.h"
21 #include "av1/common/av1_loopfilter.h"
22 #include "av1/common/blockd.h"
23 #include "av1/common/cdef.h"
24 #include "av1/common/entropymode.h"
25 #include "av1/common/enums.h"
26 #include "av1/common/thread_common.h"
27 #include "av1/common/reconinter.h"
28 #include "av1/common/reconintra.h"
29 #include "av1/common/restoration.h"
30
31 // Set up nsync by width.
get_sync_range(int width)32 static INLINE int get_sync_range(int width) {
33 // nsync numbers are picked by testing. For example, for 4k
34 // video, using 4 gives best performance.
35 if (width < 640)
36 return 1;
37 else if (width <= 1280)
38 return 2;
39 else if (width <= 4096)
40 return 4;
41 else
42 return 8;
43 }
44
get_lr_sync_range(int width)45 static INLINE int get_lr_sync_range(int width) {
46 #if 0
47 // nsync numbers are picked by testing. For example, for 4k
48 // video, using 4 gives best performance.
49 if (width < 640)
50 return 1;
51 else if (width <= 1280)
52 return 2;
53 else if (width <= 4096)
54 return 4;
55 else
56 return 8;
57 #else
58 (void)width;
59 return 1;
60 #endif
61 }
62
63 // Allocate memory for lf row synchronization
av1_loop_filter_alloc(AV1LfSync * lf_sync,AV1_COMMON * cm,int rows,int width,int num_workers)64 void av1_loop_filter_alloc(AV1LfSync *lf_sync, AV1_COMMON *cm, int rows,
65 int width, int num_workers) {
66 lf_sync->rows = rows;
67 #if CONFIG_MULTITHREAD
68 {
69 int i, j;
70
71 for (j = 0; j < MAX_MB_PLANE; j++) {
72 CHECK_MEM_ERROR(cm, lf_sync->mutex_[j],
73 aom_malloc(sizeof(*(lf_sync->mutex_[j])) * rows));
74 if (lf_sync->mutex_[j]) {
75 for (i = 0; i < rows; ++i) {
76 pthread_mutex_init(&lf_sync->mutex_[j][i], NULL);
77 }
78 }
79
80 CHECK_MEM_ERROR(cm, lf_sync->cond_[j],
81 aom_malloc(sizeof(*(lf_sync->cond_[j])) * rows));
82 if (lf_sync->cond_[j]) {
83 for (i = 0; i < rows; ++i) {
84 pthread_cond_init(&lf_sync->cond_[j][i], NULL);
85 }
86 }
87 }
88
89 CHECK_MEM_ERROR(cm, lf_sync->job_mutex,
90 aom_malloc(sizeof(*(lf_sync->job_mutex))));
91 if (lf_sync->job_mutex) {
92 pthread_mutex_init(lf_sync->job_mutex, NULL);
93 }
94 }
95 #endif // CONFIG_MULTITHREAD
96 CHECK_MEM_ERROR(cm, lf_sync->lfdata,
97 aom_malloc(num_workers * sizeof(*(lf_sync->lfdata))));
98 lf_sync->num_workers = num_workers;
99
100 for (int j = 0; j < MAX_MB_PLANE; j++) {
101 CHECK_MEM_ERROR(cm, lf_sync->cur_sb_col[j],
102 aom_malloc(sizeof(*(lf_sync->cur_sb_col[j])) * rows));
103 }
104 CHECK_MEM_ERROR(
105 cm, lf_sync->job_queue,
106 aom_malloc(sizeof(*(lf_sync->job_queue)) * rows * MAX_MB_PLANE * 2));
107 // Set up nsync.
108 lf_sync->sync_range = get_sync_range(width);
109 }
110
111 // Deallocate lf synchronization related mutex and data
av1_loop_filter_dealloc(AV1LfSync * lf_sync)112 void av1_loop_filter_dealloc(AV1LfSync *lf_sync) {
113 if (lf_sync != NULL) {
114 int j;
115 #if CONFIG_MULTITHREAD
116 int i;
117 for (j = 0; j < MAX_MB_PLANE; j++) {
118 if (lf_sync->mutex_[j] != NULL) {
119 for (i = 0; i < lf_sync->rows; ++i) {
120 pthread_mutex_destroy(&lf_sync->mutex_[j][i]);
121 }
122 aom_free(lf_sync->mutex_[j]);
123 }
124 if (lf_sync->cond_[j] != NULL) {
125 for (i = 0; i < lf_sync->rows; ++i) {
126 pthread_cond_destroy(&lf_sync->cond_[j][i]);
127 }
128 aom_free(lf_sync->cond_[j]);
129 }
130 }
131 if (lf_sync->job_mutex != NULL) {
132 pthread_mutex_destroy(lf_sync->job_mutex);
133 aom_free(lf_sync->job_mutex);
134 }
135 #endif // CONFIG_MULTITHREAD
136 aom_free(lf_sync->lfdata);
137 for (j = 0; j < MAX_MB_PLANE; j++) {
138 aom_free(lf_sync->cur_sb_col[j]);
139 }
140
141 aom_free(lf_sync->job_queue);
142 // clear the structure as the source of this call may be a resize in which
143 // case this call will be followed by an _alloc() which may fail.
144 av1_zero(*lf_sync);
145 }
146 }
147
av1_alloc_cdef_sync(AV1_COMMON * const cm,AV1CdefSync * cdef_sync,int num_workers)148 void av1_alloc_cdef_sync(AV1_COMMON *const cm, AV1CdefSync *cdef_sync,
149 int num_workers) {
150 if (num_workers < 1) return;
151 #if CONFIG_MULTITHREAD
152 if (cdef_sync->mutex_ == NULL) {
153 CHECK_MEM_ERROR(cm, cdef_sync->mutex_,
154 aom_malloc(sizeof(*(cdef_sync->mutex_))));
155 if (cdef_sync->mutex_) pthread_mutex_init(cdef_sync->mutex_, NULL);
156 }
157 #else
158 (void)cm;
159 (void)cdef_sync;
160 #endif // CONFIG_MULTITHREAD
161 }
162
av1_free_cdef_sync(AV1CdefSync * cdef_sync)163 void av1_free_cdef_sync(AV1CdefSync *cdef_sync) {
164 if (cdef_sync == NULL) return;
165 #if CONFIG_MULTITHREAD
166 if (cdef_sync->mutex_ != NULL) {
167 pthread_mutex_destroy(cdef_sync->mutex_);
168 aom_free(cdef_sync->mutex_);
169 }
170 #endif // CONFIG_MULTITHREAD
171 }
172
cdef_row_mt_sync_read(AV1CdefSync * const cdef_sync,int row)173 static INLINE void cdef_row_mt_sync_read(AV1CdefSync *const cdef_sync,
174 int row) {
175 if (!row) return;
176 #if CONFIG_MULTITHREAD
177 AV1CdefRowSync *const cdef_row_mt = cdef_sync->cdef_row_mt;
178 pthread_mutex_lock(cdef_row_mt[row - 1].row_mutex_);
179 while (cdef_row_mt[row - 1].is_row_done != 1)
180 pthread_cond_wait(cdef_row_mt[row - 1].row_cond_,
181 cdef_row_mt[row - 1].row_mutex_);
182 cdef_row_mt[row - 1].is_row_done = 0;
183 pthread_mutex_unlock(cdef_row_mt[row - 1].row_mutex_);
184 #else
185 (void)cdef_sync;
186 #endif // CONFIG_MULTITHREAD
187 }
188
cdef_row_mt_sync_write(AV1CdefSync * const cdef_sync,int row)189 static INLINE void cdef_row_mt_sync_write(AV1CdefSync *const cdef_sync,
190 int row) {
191 #if CONFIG_MULTITHREAD
192 AV1CdefRowSync *const cdef_row_mt = cdef_sync->cdef_row_mt;
193 pthread_mutex_lock(cdef_row_mt[row].row_mutex_);
194 pthread_cond_signal(cdef_row_mt[row].row_cond_);
195 cdef_row_mt[row].is_row_done = 1;
196 pthread_mutex_unlock(cdef_row_mt[row].row_mutex_);
197 #else
198 (void)cdef_sync;
199 (void)row;
200 #endif // CONFIG_MULTITHREAD
201 }
202
sync_read(AV1LfSync * const lf_sync,int r,int c,int plane)203 static INLINE void sync_read(AV1LfSync *const lf_sync, int r, int c,
204 int plane) {
205 #if CONFIG_MULTITHREAD
206 const int nsync = lf_sync->sync_range;
207
208 if (r && !(c & (nsync - 1))) {
209 pthread_mutex_t *const mutex = &lf_sync->mutex_[plane][r - 1];
210 pthread_mutex_lock(mutex);
211
212 while (c > lf_sync->cur_sb_col[plane][r - 1] - nsync) {
213 pthread_cond_wait(&lf_sync->cond_[plane][r - 1], mutex);
214 }
215 pthread_mutex_unlock(mutex);
216 }
217 #else
218 (void)lf_sync;
219 (void)r;
220 (void)c;
221 (void)plane;
222 #endif // CONFIG_MULTITHREAD
223 }
224
sync_write(AV1LfSync * const lf_sync,int r,int c,const int sb_cols,int plane)225 static INLINE void sync_write(AV1LfSync *const lf_sync, int r, int c,
226 const int sb_cols, int plane) {
227 #if CONFIG_MULTITHREAD
228 const int nsync = lf_sync->sync_range;
229 int cur;
230 // Only signal when there are enough filtered SB for next row to run.
231 int sig = 1;
232
233 if (c < sb_cols - 1) {
234 cur = c;
235 if (c % nsync) sig = 0;
236 } else {
237 cur = sb_cols + nsync;
238 }
239
240 if (sig) {
241 pthread_mutex_lock(&lf_sync->mutex_[plane][r]);
242
243 // When a thread encounters an error, cur_sb_col[plane][r] is set to maximum
244 // column number. In this case, the AOMMAX operation here ensures that
245 // cur_sb_col[plane][r] is not overwritten with a smaller value thus
246 // preventing the infinite waiting of threads in the relevant sync_read()
247 // function.
248 lf_sync->cur_sb_col[plane][r] = AOMMAX(lf_sync->cur_sb_col[plane][r], cur);
249
250 pthread_cond_broadcast(&lf_sync->cond_[plane][r]);
251 pthread_mutex_unlock(&lf_sync->mutex_[plane][r]);
252 }
253 #else
254 (void)lf_sync;
255 (void)r;
256 (void)c;
257 (void)sb_cols;
258 (void)plane;
259 #endif // CONFIG_MULTITHREAD
260 }
261
262 // One job of row loopfiltering.
av1_thread_loop_filter_rows(const YV12_BUFFER_CONFIG * const frame_buffer,AV1_COMMON * const cm,struct macroblockd_plane * planes,MACROBLOCKD * xd,int mi_row,int plane,int dir,int lpf_opt_level,AV1LfSync * const lf_sync,struct aom_internal_error_info * error_info,AV1_DEBLOCKING_PARAMETERS * params_buf,TX_SIZE * tx_buf,int num_mis_in_lpf_unit_height_log2)263 void av1_thread_loop_filter_rows(
264 const YV12_BUFFER_CONFIG *const frame_buffer, AV1_COMMON *const cm,
265 struct macroblockd_plane *planes, MACROBLOCKD *xd, int mi_row, int plane,
266 int dir, int lpf_opt_level, AV1LfSync *const lf_sync,
267 struct aom_internal_error_info *error_info,
268 AV1_DEBLOCKING_PARAMETERS *params_buf, TX_SIZE *tx_buf,
269 int num_mis_in_lpf_unit_height_log2) {
270 // TODO(aomedia:3276): Pass error_info to the low-level functions as required
271 // in future to handle error propagation.
272 (void)error_info;
273 const int sb_cols =
274 CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, MAX_MIB_SIZE_LOG2);
275 const int r = mi_row >> num_mis_in_lpf_unit_height_log2;
276 int mi_col, c;
277
278 const bool joint_filter_chroma = (lpf_opt_level == 2) && plane > AOM_PLANE_Y;
279 const int num_planes = joint_filter_chroma ? 2 : 1;
280 assert(IMPLIES(joint_filter_chroma, plane == AOM_PLANE_U));
281
282 if (dir == 0) {
283 for (mi_col = 0; mi_col < cm->mi_params.mi_cols; mi_col += MAX_MIB_SIZE) {
284 c = mi_col >> MAX_MIB_SIZE_LOG2;
285
286 av1_setup_dst_planes(planes, cm->seq_params->sb_size, frame_buffer,
287 mi_row, mi_col, plane, plane + num_planes);
288 if (lpf_opt_level) {
289 if (plane == AOM_PLANE_Y) {
290 av1_filter_block_plane_vert_opt(cm, xd, &planes[plane], mi_row,
291 mi_col, params_buf, tx_buf,
292 num_mis_in_lpf_unit_height_log2);
293 } else {
294 av1_filter_block_plane_vert_opt_chroma(
295 cm, xd, &planes[plane], mi_row, mi_col, params_buf, tx_buf, plane,
296 joint_filter_chroma, num_mis_in_lpf_unit_height_log2);
297 }
298 } else {
299 av1_filter_block_plane_vert(cm, xd, plane, &planes[plane], mi_row,
300 mi_col);
301 }
302 if (lf_sync != NULL) {
303 sync_write(lf_sync, r, c, sb_cols, plane);
304 }
305 }
306 } else if (dir == 1) {
307 for (mi_col = 0; mi_col < cm->mi_params.mi_cols; mi_col += MAX_MIB_SIZE) {
308 c = mi_col >> MAX_MIB_SIZE_LOG2;
309
310 if (lf_sync != NULL) {
311 // Wait for vertical edge filtering of the top-right block to be
312 // completed
313 sync_read(lf_sync, r, c, plane);
314
315 // Wait for vertical edge filtering of the right block to be completed
316 sync_read(lf_sync, r + 1, c, plane);
317 }
318
319 #if CONFIG_MULTITHREAD
320 if (lf_sync && lf_sync->num_workers > 1) {
321 pthread_mutex_lock(lf_sync->job_mutex);
322 const bool lf_mt_exit = lf_sync->lf_mt_exit;
323 pthread_mutex_unlock(lf_sync->job_mutex);
324 // Exit in case any worker has encountered an error.
325 if (lf_mt_exit) return;
326 }
327 #endif
328
329 av1_setup_dst_planes(planes, cm->seq_params->sb_size, frame_buffer,
330 mi_row, mi_col, plane, plane + num_planes);
331 if (lpf_opt_level) {
332 if (plane == AOM_PLANE_Y) {
333 av1_filter_block_plane_horz_opt(cm, xd, &planes[plane], mi_row,
334 mi_col, params_buf, tx_buf,
335 num_mis_in_lpf_unit_height_log2);
336 } else {
337 av1_filter_block_plane_horz_opt_chroma(
338 cm, xd, &planes[plane], mi_row, mi_col, params_buf, tx_buf, plane,
339 joint_filter_chroma, num_mis_in_lpf_unit_height_log2);
340 }
341 } else {
342 av1_filter_block_plane_horz(cm, xd, plane, &planes[plane], mi_row,
343 mi_col);
344 }
345 }
346 }
347 }
348
av1_set_vert_loop_filter_done(AV1_COMMON * cm,AV1LfSync * lf_sync,int num_mis_in_lpf_unit_height_log2)349 void av1_set_vert_loop_filter_done(AV1_COMMON *cm, AV1LfSync *lf_sync,
350 int num_mis_in_lpf_unit_height_log2) {
351 int plane, sb_row;
352 const int sb_cols =
353 CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, num_mis_in_lpf_unit_height_log2);
354 const int sb_rows =
355 CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, num_mis_in_lpf_unit_height_log2);
356
357 // In case of loopfilter row-multithreading, the worker on an SB row waits for
358 // the vertical edge filtering of the right and top-right SBs. Hence, in case
359 // a thread (main/worker) encounters an error, update that vertical
360 // loopfiltering of every SB row in the frame is complete in order to avoid
361 // dependent workers waiting indefinitely.
362 for (sb_row = 0; sb_row < sb_rows; ++sb_row)
363 for (plane = 0; plane < MAX_MB_PLANE; ++plane)
364 sync_write(lf_sync, sb_row, sb_cols - 1, sb_cols, plane);
365 }
366
sync_lf_workers(AVxWorker * const workers,AV1_COMMON * const cm,int num_workers)367 static AOM_INLINE void sync_lf_workers(AVxWorker *const workers,
368 AV1_COMMON *const cm, int num_workers) {
369 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
370 int had_error = workers[0].had_error;
371 struct aom_internal_error_info error_info;
372
373 // Read the error_info of main thread.
374 if (had_error) {
375 AVxWorker *const worker = &workers[0];
376 error_info = ((LFWorkerData *)worker->data2)->error_info;
377 }
378
379 // Wait till all rows are finished.
380 for (int i = num_workers - 1; i > 0; --i) {
381 AVxWorker *const worker = &workers[i];
382 if (!winterface->sync(worker)) {
383 had_error = 1;
384 error_info = ((LFWorkerData *)worker->data2)->error_info;
385 }
386 }
387 if (had_error) aom_internal_error_copy(cm->error, &error_info);
388 }
389
390 // Row-based multi-threaded loopfilter hook
loop_filter_row_worker(void * arg1,void * arg2)391 static int loop_filter_row_worker(void *arg1, void *arg2) {
392 AV1LfSync *const lf_sync = (AV1LfSync *)arg1;
393 LFWorkerData *const lf_data = (LFWorkerData *)arg2;
394 AV1LfMTInfo *cur_job_info;
395
396 #if CONFIG_MULTITHREAD
397 pthread_mutex_t *job_mutex_ = lf_sync->job_mutex;
398 #endif
399
400 struct aom_internal_error_info *const error_info = &lf_data->error_info;
401
402 // The jmp_buf is valid only for the duration of the function that calls
403 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
404 // before it returns.
405 if (setjmp(error_info->jmp)) {
406 error_info->setjmp = 0;
407 #if CONFIG_MULTITHREAD
408 pthread_mutex_lock(job_mutex_);
409 lf_sync->lf_mt_exit = true;
410 pthread_mutex_unlock(job_mutex_);
411 #endif
412 av1_set_vert_loop_filter_done(lf_data->cm, lf_sync, MAX_MIB_SIZE_LOG2);
413 return 0;
414 }
415 error_info->setjmp = 1;
416
417 while ((cur_job_info = get_lf_job_info(lf_sync)) != NULL) {
418 const int lpf_opt_level = cur_job_info->lpf_opt_level;
419 av1_thread_loop_filter_rows(
420 lf_data->frame_buffer, lf_data->cm, lf_data->planes, lf_data->xd,
421 cur_job_info->mi_row, cur_job_info->plane, cur_job_info->dir,
422 lpf_opt_level, lf_sync, error_info, lf_data->params_buf,
423 lf_data->tx_buf, MAX_MIB_SIZE_LOG2);
424 }
425 error_info->setjmp = 0;
426 return 1;
427 }
428
loop_filter_rows_mt(YV12_BUFFER_CONFIG * frame,AV1_COMMON * cm,MACROBLOCKD * xd,int start,int stop,const int planes_to_lf[MAX_MB_PLANE],AVxWorker * workers,int num_workers,AV1LfSync * lf_sync,int lpf_opt_level)429 static void loop_filter_rows_mt(YV12_BUFFER_CONFIG *frame, AV1_COMMON *cm,
430 MACROBLOCKD *xd, int start, int stop,
431 const int planes_to_lf[MAX_MB_PLANE],
432 AVxWorker *workers, int num_workers,
433 AV1LfSync *lf_sync, int lpf_opt_level) {
434 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
435 int i;
436 loop_filter_frame_mt_init(cm, start, stop, planes_to_lf, num_workers, lf_sync,
437 lpf_opt_level, MAX_MIB_SIZE_LOG2);
438
439 // Set up loopfilter thread data.
440 for (i = num_workers - 1; i >= 0; --i) {
441 AVxWorker *const worker = &workers[i];
442 LFWorkerData *const lf_data = &lf_sync->lfdata[i];
443
444 worker->hook = loop_filter_row_worker;
445 worker->data1 = lf_sync;
446 worker->data2 = lf_data;
447
448 // Loopfilter data
449 loop_filter_data_reset(lf_data, frame, cm, xd);
450
451 // Start loopfiltering
452 worker->had_error = 0;
453 if (i == 0) {
454 winterface->execute(worker);
455 } else {
456 winterface->launch(worker);
457 }
458 }
459
460 sync_lf_workers(workers, cm, num_workers);
461 }
462
loop_filter_rows(YV12_BUFFER_CONFIG * frame,AV1_COMMON * cm,MACROBLOCKD * xd,int start,int stop,const int planes_to_lf[MAX_MB_PLANE],int lpf_opt_level)463 static void loop_filter_rows(YV12_BUFFER_CONFIG *frame, AV1_COMMON *cm,
464 MACROBLOCKD *xd, int start, int stop,
465 const int planes_to_lf[MAX_MB_PLANE],
466 int lpf_opt_level) {
467 // Filter top rows of all planes first, in case the output can be partially
468 // reconstructed row by row.
469 int mi_row, plane, dir;
470
471 AV1_DEBLOCKING_PARAMETERS params_buf[MAX_MIB_SIZE];
472 TX_SIZE tx_buf[MAX_MIB_SIZE];
473 for (mi_row = start; mi_row < stop; mi_row += MAX_MIB_SIZE) {
474 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
475 if (skip_loop_filter_plane(planes_to_lf, plane, lpf_opt_level)) {
476 continue;
477 }
478
479 for (dir = 0; dir < 2; ++dir) {
480 av1_thread_loop_filter_rows(frame, cm, xd->plane, xd, mi_row, plane,
481 dir, lpf_opt_level, /*lf_sync=*/NULL,
482 xd->error_info, params_buf, tx_buf,
483 MAX_MIB_SIZE_LOG2);
484 }
485 }
486 }
487 }
488
av1_loop_filter_frame_mt(YV12_BUFFER_CONFIG * frame,AV1_COMMON * cm,MACROBLOCKD * xd,int plane_start,int plane_end,int partial_frame,AVxWorker * workers,int num_workers,AV1LfSync * lf_sync,int lpf_opt_level)489 void av1_loop_filter_frame_mt(YV12_BUFFER_CONFIG *frame, AV1_COMMON *cm,
490 MACROBLOCKD *xd, int plane_start, int plane_end,
491 int partial_frame, AVxWorker *workers,
492 int num_workers, AV1LfSync *lf_sync,
493 int lpf_opt_level) {
494 int start_mi_row, end_mi_row, mi_rows_to_filter;
495 int planes_to_lf[MAX_MB_PLANE];
496
497 if (!check_planes_to_loop_filter(&cm->lf, planes_to_lf, plane_start,
498 plane_end))
499 return;
500
501 start_mi_row = 0;
502 mi_rows_to_filter = cm->mi_params.mi_rows;
503 if (partial_frame && cm->mi_params.mi_rows > 8) {
504 start_mi_row = cm->mi_params.mi_rows >> 1;
505 start_mi_row &= 0xfffffff8;
506 mi_rows_to_filter = AOMMAX(cm->mi_params.mi_rows / 8, 8);
507 }
508 end_mi_row = start_mi_row + mi_rows_to_filter;
509 av1_loop_filter_frame_init(cm, plane_start, plane_end);
510
511 if (num_workers > 1) {
512 // Enqueue and execute loopfiltering jobs.
513 loop_filter_rows_mt(frame, cm, xd, start_mi_row, end_mi_row, planes_to_lf,
514 workers, num_workers, lf_sync, lpf_opt_level);
515 } else {
516 // Directly filter in the main thread.
517 loop_filter_rows(frame, cm, xd, start_mi_row, end_mi_row, planes_to_lf,
518 lpf_opt_level);
519 }
520 }
521
lr_sync_read(void * const lr_sync,int r,int c,int plane)522 static INLINE void lr_sync_read(void *const lr_sync, int r, int c, int plane) {
523 #if CONFIG_MULTITHREAD
524 AV1LrSync *const loop_res_sync = (AV1LrSync *)lr_sync;
525 const int nsync = loop_res_sync->sync_range;
526
527 if (r && !(c & (nsync - 1))) {
528 pthread_mutex_t *const mutex = &loop_res_sync->mutex_[plane][r - 1];
529 pthread_mutex_lock(mutex);
530
531 while (c > loop_res_sync->cur_sb_col[plane][r - 1] - nsync) {
532 pthread_cond_wait(&loop_res_sync->cond_[plane][r - 1], mutex);
533 }
534 pthread_mutex_unlock(mutex);
535 }
536 #else
537 (void)lr_sync;
538 (void)r;
539 (void)c;
540 (void)plane;
541 #endif // CONFIG_MULTITHREAD
542 }
543
lr_sync_write(void * const lr_sync,int r,int c,const int sb_cols,int plane)544 static INLINE void lr_sync_write(void *const lr_sync, int r, int c,
545 const int sb_cols, int plane) {
546 #if CONFIG_MULTITHREAD
547 AV1LrSync *const loop_res_sync = (AV1LrSync *)lr_sync;
548 const int nsync = loop_res_sync->sync_range;
549 int cur;
550 // Only signal when there are enough filtered SB for next row to run.
551 int sig = 1;
552
553 if (c < sb_cols - 1) {
554 cur = c;
555 if (c % nsync) sig = 0;
556 } else {
557 cur = sb_cols + nsync;
558 }
559
560 if (sig) {
561 pthread_mutex_lock(&loop_res_sync->mutex_[plane][r]);
562
563 // When a thread encounters an error, cur_sb_col[plane][r] is set to maximum
564 // column number. In this case, the AOMMAX operation here ensures that
565 // cur_sb_col[plane][r] is not overwritten with a smaller value thus
566 // preventing the infinite waiting of threads in the relevant sync_read()
567 // function.
568 loop_res_sync->cur_sb_col[plane][r] =
569 AOMMAX(loop_res_sync->cur_sb_col[plane][r], cur);
570
571 pthread_cond_broadcast(&loop_res_sync->cond_[plane][r]);
572 pthread_mutex_unlock(&loop_res_sync->mutex_[plane][r]);
573 }
574 #else
575 (void)lr_sync;
576 (void)r;
577 (void)c;
578 (void)sb_cols;
579 (void)plane;
580 #endif // CONFIG_MULTITHREAD
581 }
582
583 // Allocate memory for loop restoration row synchronization
av1_loop_restoration_alloc(AV1LrSync * lr_sync,AV1_COMMON * cm,int num_workers,int num_rows_lr,int num_planes,int width)584 void av1_loop_restoration_alloc(AV1LrSync *lr_sync, AV1_COMMON *cm,
585 int num_workers, int num_rows_lr,
586 int num_planes, int width) {
587 lr_sync->rows = num_rows_lr;
588 lr_sync->num_planes = num_planes;
589 #if CONFIG_MULTITHREAD
590 {
591 int i, j;
592
593 for (j = 0; j < num_planes; j++) {
594 CHECK_MEM_ERROR(cm, lr_sync->mutex_[j],
595 aom_malloc(sizeof(*(lr_sync->mutex_[j])) * num_rows_lr));
596 if (lr_sync->mutex_[j]) {
597 for (i = 0; i < num_rows_lr; ++i) {
598 pthread_mutex_init(&lr_sync->mutex_[j][i], NULL);
599 }
600 }
601
602 CHECK_MEM_ERROR(cm, lr_sync->cond_[j],
603 aom_malloc(sizeof(*(lr_sync->cond_[j])) * num_rows_lr));
604 if (lr_sync->cond_[j]) {
605 for (i = 0; i < num_rows_lr; ++i) {
606 pthread_cond_init(&lr_sync->cond_[j][i], NULL);
607 }
608 }
609 }
610
611 CHECK_MEM_ERROR(cm, lr_sync->job_mutex,
612 aom_malloc(sizeof(*(lr_sync->job_mutex))));
613 if (lr_sync->job_mutex) {
614 pthread_mutex_init(lr_sync->job_mutex, NULL);
615 }
616 }
617 #endif // CONFIG_MULTITHREAD
618 CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata,
619 aom_calloc(num_workers, sizeof(*(lr_sync->lrworkerdata))));
620 lr_sync->num_workers = num_workers;
621
622 for (int worker_idx = 0; worker_idx < num_workers; ++worker_idx) {
623 if (worker_idx < num_workers - 1) {
624 CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata[worker_idx].rst_tmpbuf,
625 (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE));
626 CHECK_MEM_ERROR(cm, lr_sync->lrworkerdata[worker_idx].rlbs,
627 aom_malloc(sizeof(RestorationLineBuffers)));
628
629 } else {
630 lr_sync->lrworkerdata[worker_idx].rst_tmpbuf = cm->rst_tmpbuf;
631 lr_sync->lrworkerdata[worker_idx].rlbs = cm->rlbs;
632 }
633 }
634
635 for (int j = 0; j < num_planes; j++) {
636 CHECK_MEM_ERROR(
637 cm, lr_sync->cur_sb_col[j],
638 aom_malloc(sizeof(*(lr_sync->cur_sb_col[j])) * num_rows_lr));
639 }
640 CHECK_MEM_ERROR(
641 cm, lr_sync->job_queue,
642 aom_malloc(sizeof(*(lr_sync->job_queue)) * num_rows_lr * num_planes));
643 // Set up nsync.
644 lr_sync->sync_range = get_lr_sync_range(width);
645 }
646
647 // Deallocate loop restoration synchronization related mutex and data
av1_loop_restoration_dealloc(AV1LrSync * lr_sync)648 void av1_loop_restoration_dealloc(AV1LrSync *lr_sync) {
649 if (lr_sync != NULL) {
650 int j;
651 #if CONFIG_MULTITHREAD
652 int i;
653 for (j = 0; j < MAX_MB_PLANE; j++) {
654 if (lr_sync->mutex_[j] != NULL) {
655 for (i = 0; i < lr_sync->rows; ++i) {
656 pthread_mutex_destroy(&lr_sync->mutex_[j][i]);
657 }
658 aom_free(lr_sync->mutex_[j]);
659 }
660 if (lr_sync->cond_[j] != NULL) {
661 for (i = 0; i < lr_sync->rows; ++i) {
662 pthread_cond_destroy(&lr_sync->cond_[j][i]);
663 }
664 aom_free(lr_sync->cond_[j]);
665 }
666 }
667 if (lr_sync->job_mutex != NULL) {
668 pthread_mutex_destroy(lr_sync->job_mutex);
669 aom_free(lr_sync->job_mutex);
670 }
671 #endif // CONFIG_MULTITHREAD
672 for (j = 0; j < MAX_MB_PLANE; j++) {
673 aom_free(lr_sync->cur_sb_col[j]);
674 }
675
676 aom_free(lr_sync->job_queue);
677
678 if (lr_sync->lrworkerdata) {
679 for (int worker_idx = 0; worker_idx < lr_sync->num_workers - 1;
680 worker_idx++) {
681 LRWorkerData *const workerdata_data =
682 lr_sync->lrworkerdata + worker_idx;
683
684 aom_free(workerdata_data->rst_tmpbuf);
685 aom_free(workerdata_data->rlbs);
686 }
687 aom_free(lr_sync->lrworkerdata);
688 }
689
690 // clear the structure as the source of this call may be a resize in which
691 // case this call will be followed by an _alloc() which may fail.
692 av1_zero(*lr_sync);
693 }
694 }
695
enqueue_lr_jobs(AV1LrSync * lr_sync,AV1LrStruct * lr_ctxt,AV1_COMMON * cm)696 static void enqueue_lr_jobs(AV1LrSync *lr_sync, AV1LrStruct *lr_ctxt,
697 AV1_COMMON *cm) {
698 FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
699
700 const int num_planes = av1_num_planes(cm);
701 AV1LrMTInfo *lr_job_queue = lr_sync->job_queue;
702 int32_t lr_job_counter[2], num_even_lr_jobs = 0;
703 lr_sync->jobs_enqueued = 0;
704 lr_sync->jobs_dequeued = 0;
705
706 for (int plane = 0; plane < num_planes; plane++) {
707 if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
708 num_even_lr_jobs =
709 num_even_lr_jobs + ((ctxt[plane].rsi->vert_units + 1) >> 1);
710 }
711 lr_job_counter[0] = 0;
712 lr_job_counter[1] = num_even_lr_jobs;
713
714 for (int plane = 0; plane < num_planes; plane++) {
715 if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
716 const int is_uv = plane > 0;
717 const int ss_y = is_uv && cm->seq_params->subsampling_y;
718 const int unit_size = ctxt[plane].rsi->restoration_unit_size;
719 const int plane_h = ctxt[plane].plane_h;
720 const int ext_size = unit_size * 3 / 2;
721
722 int y0 = 0, i = 0;
723 while (y0 < plane_h) {
724 int remaining_h = plane_h - y0;
725 int h = (remaining_h < ext_size) ? remaining_h : unit_size;
726
727 RestorationTileLimits limits;
728 limits.v_start = y0;
729 limits.v_end = y0 + h;
730 assert(limits.v_end <= plane_h);
731 // Offset upwards to align with the restoration processing stripe
732 const int voffset = RESTORATION_UNIT_OFFSET >> ss_y;
733 limits.v_start = AOMMAX(0, limits.v_start - voffset);
734 if (limits.v_end < plane_h) limits.v_end -= voffset;
735
736 assert(lr_job_counter[0] <= num_even_lr_jobs);
737
738 lr_job_queue[lr_job_counter[i & 1]].lr_unit_row = i;
739 lr_job_queue[lr_job_counter[i & 1]].plane = plane;
740 lr_job_queue[lr_job_counter[i & 1]].v_start = limits.v_start;
741 lr_job_queue[lr_job_counter[i & 1]].v_end = limits.v_end;
742 lr_job_queue[lr_job_counter[i & 1]].sync_mode = i & 1;
743 if ((i & 1) == 0) {
744 lr_job_queue[lr_job_counter[i & 1]].v_copy_start =
745 limits.v_start + RESTORATION_BORDER;
746 lr_job_queue[lr_job_counter[i & 1]].v_copy_end =
747 limits.v_end - RESTORATION_BORDER;
748 if (i == 0) {
749 assert(limits.v_start == 0);
750 lr_job_queue[lr_job_counter[i & 1]].v_copy_start = 0;
751 }
752 if (i == (ctxt[plane].rsi->vert_units - 1)) {
753 assert(limits.v_end == plane_h);
754 lr_job_queue[lr_job_counter[i & 1]].v_copy_end = plane_h;
755 }
756 } else {
757 lr_job_queue[lr_job_counter[i & 1]].v_copy_start =
758 AOMMAX(limits.v_start - RESTORATION_BORDER, 0);
759 lr_job_queue[lr_job_counter[i & 1]].v_copy_end =
760 AOMMIN(limits.v_end + RESTORATION_BORDER, plane_h);
761 }
762 lr_job_counter[i & 1]++;
763 lr_sync->jobs_enqueued++;
764
765 y0 += h;
766 ++i;
767 }
768 }
769 }
770
get_lr_job_info(AV1LrSync * lr_sync)771 static AV1LrMTInfo *get_lr_job_info(AV1LrSync *lr_sync) {
772 AV1LrMTInfo *cur_job_info = NULL;
773
774 #if CONFIG_MULTITHREAD
775 pthread_mutex_lock(lr_sync->job_mutex);
776
777 if (!lr_sync->lr_mt_exit && lr_sync->jobs_dequeued < lr_sync->jobs_enqueued) {
778 cur_job_info = lr_sync->job_queue + lr_sync->jobs_dequeued;
779 lr_sync->jobs_dequeued++;
780 }
781
782 pthread_mutex_unlock(lr_sync->job_mutex);
783 #else
784 (void)lr_sync;
785 #endif
786
787 return cur_job_info;
788 }
789
set_loop_restoration_done(AV1LrSync * const lr_sync,FilterFrameCtxt * const ctxt)790 static void set_loop_restoration_done(AV1LrSync *const lr_sync,
791 FilterFrameCtxt *const ctxt) {
792 for (int plane = 0; plane < MAX_MB_PLANE; ++plane) {
793 if (ctxt[plane].rsi->frame_restoration_type == RESTORE_NONE) continue;
794 int y0 = 0, row_number = 0;
795 const int unit_size = ctxt[plane].rsi->restoration_unit_size;
796 const int plane_h = ctxt[plane].plane_h;
797 const int ext_size = unit_size * 3 / 2;
798 const int hnum_rest_units = ctxt[plane].rsi->horz_units;
799 while (y0 < plane_h) {
800 const int remaining_h = plane_h - y0;
801 const int h = (remaining_h < ext_size) ? remaining_h : unit_size;
802 lr_sync_write(lr_sync, row_number, hnum_rest_units - 1, hnum_rest_units,
803 plane);
804 y0 += h;
805 ++row_number;
806 }
807 }
808 }
809
810 // Implement row loop restoration for each thread.
loop_restoration_row_worker(void * arg1,void * arg2)811 static int loop_restoration_row_worker(void *arg1, void *arg2) {
812 AV1LrSync *const lr_sync = (AV1LrSync *)arg1;
813 LRWorkerData *lrworkerdata = (LRWorkerData *)arg2;
814 AV1LrStruct *lr_ctxt = (AV1LrStruct *)lrworkerdata->lr_ctxt;
815 FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
816 int lr_unit_row;
817 int plane;
818 int plane_w;
819 #if CONFIG_MULTITHREAD
820 pthread_mutex_t *job_mutex_ = lr_sync->job_mutex;
821 #endif
822 struct aom_internal_error_info *const error_info = &lrworkerdata->error_info;
823
824 // The jmp_buf is valid only for the duration of the function that calls
825 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
826 // before it returns.
827 if (setjmp(error_info->jmp)) {
828 error_info->setjmp = 0;
829 #if CONFIG_MULTITHREAD
830 pthread_mutex_lock(job_mutex_);
831 lr_sync->lr_mt_exit = true;
832 pthread_mutex_unlock(job_mutex_);
833 #endif
834 // In case of loop restoration multithreading, the worker on an even lr
835 // block row waits for the completion of the filtering of the top-right and
836 // bottom-right blocks. Hence, in case a thread (main/worker) encounters an
837 // error, update that filtering of every row in the frame is complete in
838 // order to avoid the dependent workers from waiting indefinitely.
839 set_loop_restoration_done(lr_sync, lr_ctxt->ctxt);
840 return 0;
841 }
842 error_info->setjmp = 1;
843
844 typedef void (*copy_fun)(const YV12_BUFFER_CONFIG *src_ybc,
845 YV12_BUFFER_CONFIG *dst_ybc, int hstart, int hend,
846 int vstart, int vend);
847 static const copy_fun copy_funs[MAX_MB_PLANE] = {
848 aom_yv12_partial_coloc_copy_y, aom_yv12_partial_coloc_copy_u,
849 aom_yv12_partial_coloc_copy_v
850 };
851
852 while (1) {
853 AV1LrMTInfo *cur_job_info = get_lr_job_info(lr_sync);
854 if (cur_job_info != NULL) {
855 RestorationTileLimits limits;
856 sync_read_fn_t on_sync_read;
857 sync_write_fn_t on_sync_write;
858 limits.v_start = cur_job_info->v_start;
859 limits.v_end = cur_job_info->v_end;
860 lr_unit_row = cur_job_info->lr_unit_row;
861 plane = cur_job_info->plane;
862 plane_w = ctxt[plane].plane_w;
863
864 // sync_mode == 1 implies only sync read is required in LR Multi-threading
865 // sync_mode == 0 implies only sync write is required.
866 on_sync_read =
867 cur_job_info->sync_mode == 1 ? lr_sync_read : av1_lr_sync_read_dummy;
868 on_sync_write = cur_job_info->sync_mode == 0 ? lr_sync_write
869 : av1_lr_sync_write_dummy;
870
871 av1_foreach_rest_unit_in_row(
872 &limits, plane_w, lr_ctxt->on_rest_unit, lr_unit_row,
873 ctxt[plane].rsi->restoration_unit_size, ctxt[plane].rsi->horz_units,
874 ctxt[plane].rsi->vert_units, plane, &ctxt[plane],
875 lrworkerdata->rst_tmpbuf, lrworkerdata->rlbs, on_sync_read,
876 on_sync_write, lr_sync, error_info);
877
878 copy_funs[plane](lr_ctxt->dst, lr_ctxt->frame, 0, plane_w,
879 cur_job_info->v_copy_start, cur_job_info->v_copy_end);
880
881 if (lrworkerdata->do_extend_border) {
882 aom_extend_frame_borders_plane_row(lr_ctxt->frame, plane,
883 cur_job_info->v_copy_start,
884 cur_job_info->v_copy_end);
885 }
886 } else {
887 break;
888 }
889 }
890 error_info->setjmp = 0;
891 return 1;
892 }
893
sync_lr_workers(AVxWorker * const workers,AV1_COMMON * const cm,int num_workers)894 static AOM_INLINE void sync_lr_workers(AVxWorker *const workers,
895 AV1_COMMON *const cm, int num_workers) {
896 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
897 int had_error = workers[0].had_error;
898 struct aom_internal_error_info error_info;
899
900 // Read the error_info of main thread.
901 if (had_error) {
902 AVxWorker *const worker = &workers[0];
903 error_info = ((LRWorkerData *)worker->data2)->error_info;
904 }
905
906 // Wait till all rows are finished.
907 for (int i = num_workers - 1; i > 0; --i) {
908 AVxWorker *const worker = &workers[i];
909 if (!winterface->sync(worker)) {
910 had_error = 1;
911 error_info = ((LRWorkerData *)worker->data2)->error_info;
912 }
913 }
914 if (had_error) aom_internal_error_copy(cm->error, &error_info);
915 }
916
foreach_rest_unit_in_planes_mt(AV1LrStruct * lr_ctxt,AVxWorker * workers,int num_workers,AV1LrSync * lr_sync,AV1_COMMON * cm,int do_extend_border)917 static void foreach_rest_unit_in_planes_mt(AV1LrStruct *lr_ctxt,
918 AVxWorker *workers, int num_workers,
919 AV1LrSync *lr_sync, AV1_COMMON *cm,
920 int do_extend_border) {
921 FilterFrameCtxt *ctxt = lr_ctxt->ctxt;
922
923 const int num_planes = av1_num_planes(cm);
924
925 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
926 int num_rows_lr = 0;
927
928 for (int plane = 0; plane < num_planes; plane++) {
929 if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
930
931 const int plane_h = ctxt[plane].plane_h;
932 const int unit_size = cm->rst_info[plane].restoration_unit_size;
933
934 num_rows_lr = AOMMAX(num_rows_lr, av1_lr_count_units(unit_size, plane_h));
935 }
936
937 int i;
938 assert(MAX_MB_PLANE == 3);
939
940 if (!lr_sync->sync_range || num_rows_lr > lr_sync->rows ||
941 num_workers > lr_sync->num_workers || num_planes > lr_sync->num_planes) {
942 av1_loop_restoration_dealloc(lr_sync);
943 av1_loop_restoration_alloc(lr_sync, cm, num_workers, num_rows_lr,
944 num_planes, cm->width);
945 }
946 lr_sync->lr_mt_exit = false;
947
948 // Initialize cur_sb_col to -1 for all SB rows.
949 for (i = 0; i < num_planes; i++) {
950 memset(lr_sync->cur_sb_col[i], -1,
951 sizeof(*(lr_sync->cur_sb_col[i])) * num_rows_lr);
952 }
953
954 enqueue_lr_jobs(lr_sync, lr_ctxt, cm);
955
956 // Set up looprestoration thread data.
957 for (i = num_workers - 1; i >= 0; --i) {
958 AVxWorker *const worker = &workers[i];
959 lr_sync->lrworkerdata[i].lr_ctxt = (void *)lr_ctxt;
960 lr_sync->lrworkerdata[i].do_extend_border = do_extend_border;
961 worker->hook = loop_restoration_row_worker;
962 worker->data1 = lr_sync;
963 worker->data2 = &lr_sync->lrworkerdata[i];
964
965 // Start loop restoration
966 worker->had_error = 0;
967 if (i == 0) {
968 winterface->execute(worker);
969 } else {
970 winterface->launch(worker);
971 }
972 }
973
974 sync_lr_workers(workers, cm, num_workers);
975 }
976
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,int do_extend_border)977 void av1_loop_restoration_filter_frame_mt(YV12_BUFFER_CONFIG *frame,
978 AV1_COMMON *cm, int optimized_lr,
979 AVxWorker *workers, int num_workers,
980 AV1LrSync *lr_sync, void *lr_ctxt,
981 int do_extend_border) {
982 assert(!cm->features.all_lossless);
983
984 const int num_planes = av1_num_planes(cm);
985
986 AV1LrStruct *loop_rest_ctxt = (AV1LrStruct *)lr_ctxt;
987
988 av1_loop_restoration_filter_frame_init(loop_rest_ctxt, frame, cm,
989 optimized_lr, num_planes);
990
991 foreach_rest_unit_in_planes_mt(loop_rest_ctxt, workers, num_workers, lr_sync,
992 cm, do_extend_border);
993 }
994
995 // Initializes cdef_sync parameters.
reset_cdef_job_info(AV1CdefSync * const cdef_sync)996 static AOM_INLINE void reset_cdef_job_info(AV1CdefSync *const cdef_sync) {
997 cdef_sync->end_of_frame = 0;
998 cdef_sync->fbr = 0;
999 cdef_sync->fbc = 0;
1000 cdef_sync->cdef_mt_exit = false;
1001 }
1002
launch_cdef_workers(AVxWorker * const workers,int num_workers)1003 static AOM_INLINE void launch_cdef_workers(AVxWorker *const workers,
1004 int num_workers) {
1005 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
1006 for (int i = num_workers - 1; i >= 0; i--) {
1007 AVxWorker *const worker = &workers[i];
1008 worker->had_error = 0;
1009 if (i == 0)
1010 winterface->execute(worker);
1011 else
1012 winterface->launch(worker);
1013 }
1014 }
1015
sync_cdef_workers(AVxWorker * const workers,AV1_COMMON * const cm,int num_workers)1016 static AOM_INLINE void sync_cdef_workers(AVxWorker *const workers,
1017 AV1_COMMON *const cm,
1018 int num_workers) {
1019 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
1020 int had_error = workers[0].had_error;
1021 struct aom_internal_error_info error_info;
1022
1023 // Read the error_info of main thread.
1024 if (had_error) {
1025 AVxWorker *const worker = &workers[0];
1026 error_info = ((AV1CdefWorkerData *)worker->data2)->error_info;
1027 }
1028
1029 // Wait till all rows are finished.
1030 for (int i = num_workers - 1; i > 0; --i) {
1031 AVxWorker *const worker = &workers[i];
1032 if (!winterface->sync(worker)) {
1033 had_error = 1;
1034 error_info = ((AV1CdefWorkerData *)worker->data2)->error_info;
1035 }
1036 }
1037 if (had_error) aom_internal_error_copy(cm->error, &error_info);
1038 }
1039
1040 // Updates the row index of the next job to be processed.
1041 // Also updates end_of_frame flag when the processing of all rows is complete.
update_cdef_row_next_job_info(AV1CdefSync * const cdef_sync,const int nvfb)1042 static void update_cdef_row_next_job_info(AV1CdefSync *const cdef_sync,
1043 const int nvfb) {
1044 cdef_sync->fbr++;
1045 if (cdef_sync->fbr == nvfb) {
1046 cdef_sync->end_of_frame = 1;
1047 }
1048 }
1049
1050 // Checks if a job is available. If job is available,
1051 // populates next job information and returns 1, else returns 0.
get_cdef_row_next_job(AV1CdefSync * const cdef_sync,volatile int * cur_fbr,const int nvfb)1052 static AOM_INLINE int get_cdef_row_next_job(AV1CdefSync *const cdef_sync,
1053 volatile int *cur_fbr,
1054 const int nvfb) {
1055 #if CONFIG_MULTITHREAD
1056 pthread_mutex_lock(cdef_sync->mutex_);
1057 #endif // CONFIG_MULTITHREAD
1058 int do_next_row = 0;
1059 // Populates information needed for current job and update the row
1060 // index of the next row to be processed.
1061 if (!cdef_sync->cdef_mt_exit && cdef_sync->end_of_frame == 0) {
1062 do_next_row = 1;
1063 *cur_fbr = cdef_sync->fbr;
1064 update_cdef_row_next_job_info(cdef_sync, nvfb);
1065 }
1066 #if CONFIG_MULTITHREAD
1067 pthread_mutex_unlock(cdef_sync->mutex_);
1068 #endif // CONFIG_MULTITHREAD
1069 return do_next_row;
1070 }
1071
set_cdef_init_fb_row_done(AV1CdefSync * const cdef_sync,int nvfb)1072 static void set_cdef_init_fb_row_done(AV1CdefSync *const cdef_sync, int nvfb) {
1073 for (int fbr = 0; fbr < nvfb; fbr++) cdef_row_mt_sync_write(cdef_sync, fbr);
1074 }
1075
1076 // Hook function for each thread in CDEF multi-threading.
cdef_sb_row_worker_hook(void * arg1,void * arg2)1077 static int cdef_sb_row_worker_hook(void *arg1, void *arg2) {
1078 AV1CdefSync *const cdef_sync = (AV1CdefSync *)arg1;
1079 AV1CdefWorkerData *const cdef_worker = (AV1CdefWorkerData *)arg2;
1080 AV1_COMMON *cm = cdef_worker->cm;
1081 const int nvfb = (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
1082
1083 #if CONFIG_MULTITHREAD
1084 pthread_mutex_t *job_mutex_ = cdef_sync->mutex_;
1085 #endif
1086 struct aom_internal_error_info *const error_info = &cdef_worker->error_info;
1087
1088 // The jmp_buf is valid only for the duration of the function that calls
1089 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
1090 // before it returns.
1091 if (setjmp(error_info->jmp)) {
1092 error_info->setjmp = 0;
1093 #if CONFIG_MULTITHREAD
1094 pthread_mutex_lock(job_mutex_);
1095 cdef_sync->cdef_mt_exit = true;
1096 pthread_mutex_unlock(job_mutex_);
1097 #endif
1098 // In case of cdef row-multithreading, the worker on a filter block row
1099 // (fbr) waits for the line buffers (top and bottom) copy of the above row.
1100 // Hence, in case a thread (main/worker) encounters an error before copying
1101 // of the line buffers, update that line buffer copy is complete in order to
1102 // avoid dependent workers waiting indefinitely.
1103 set_cdef_init_fb_row_done(cdef_sync, nvfb);
1104 return 0;
1105 }
1106 error_info->setjmp = 1;
1107
1108 volatile int cur_fbr;
1109 const int num_planes = av1_num_planes(cm);
1110 while (get_cdef_row_next_job(cdef_sync, &cur_fbr, nvfb)) {
1111 MACROBLOCKD *xd = cdef_worker->xd;
1112 av1_cdef_fb_row(cm, xd, cdef_worker->linebuf, cdef_worker->colbuf,
1113 cdef_worker->srcbuf, cur_fbr,
1114 cdef_worker->cdef_init_fb_row_fn, cdef_sync, error_info);
1115 if (cdef_worker->do_extend_border) {
1116 for (int plane = 0; plane < num_planes; ++plane) {
1117 const YV12_BUFFER_CONFIG *ybf = &cm->cur_frame->buf;
1118 const int is_uv = plane > 0;
1119 const int mi_high = MI_SIZE_LOG2 - xd->plane[plane].subsampling_y;
1120 const int unit_height = MI_SIZE_64X64 << mi_high;
1121 const int v_start = cur_fbr * unit_height;
1122 const int v_end =
1123 AOMMIN(v_start + unit_height, ybf->crop_heights[is_uv]);
1124 aom_extend_frame_borders_plane_row(ybf, plane, v_start, v_end);
1125 }
1126 }
1127 }
1128 error_info->setjmp = 0;
1129 return 1;
1130 }
1131
1132 // Assigns CDEF hook function and thread data to each worker.
prepare_cdef_frame_workers(AV1_COMMON * const cm,MACROBLOCKD * xd,AV1CdefWorkerData * const cdef_worker,AVxWorkerHook hook,AVxWorker * const workers,AV1CdefSync * const cdef_sync,int num_workers,cdef_init_fb_row_t cdef_init_fb_row_fn,int do_extend_border)1133 static void prepare_cdef_frame_workers(
1134 AV1_COMMON *const cm, MACROBLOCKD *xd, AV1CdefWorkerData *const cdef_worker,
1135 AVxWorkerHook hook, AVxWorker *const workers, AV1CdefSync *const cdef_sync,
1136 int num_workers, cdef_init_fb_row_t cdef_init_fb_row_fn,
1137 int do_extend_border) {
1138 const int num_planes = av1_num_planes(cm);
1139
1140 cdef_worker[0].srcbuf = cm->cdef_info.srcbuf;
1141 for (int plane = 0; plane < num_planes; plane++)
1142 cdef_worker[0].colbuf[plane] = cm->cdef_info.colbuf[plane];
1143 for (int i = num_workers - 1; i >= 0; i--) {
1144 AVxWorker *const worker = &workers[i];
1145 cdef_worker[i].cm = cm;
1146 cdef_worker[i].xd = xd;
1147 cdef_worker[i].cdef_init_fb_row_fn = cdef_init_fb_row_fn;
1148 cdef_worker[i].do_extend_border = do_extend_border;
1149 for (int plane = 0; plane < num_planes; plane++)
1150 cdef_worker[i].linebuf[plane] = cm->cdef_info.linebuf[plane];
1151
1152 worker->hook = hook;
1153 worker->data1 = cdef_sync;
1154 worker->data2 = &cdef_worker[i];
1155 }
1156 }
1157
1158 // Initializes row-level parameters for CDEF frame.
av1_cdef_init_fb_row_mt(const AV1_COMMON * const cm,const MACROBLOCKD * const xd,CdefBlockInfo * const fb_info,uint16_t ** const linebuf,uint16_t * const src,struct AV1CdefSyncData * const cdef_sync,int fbr)1159 void av1_cdef_init_fb_row_mt(const AV1_COMMON *const cm,
1160 const MACROBLOCKD *const xd,
1161 CdefBlockInfo *const fb_info,
1162 uint16_t **const linebuf, uint16_t *const src,
1163 struct AV1CdefSyncData *const cdef_sync, int fbr) {
1164 const int num_planes = av1_num_planes(cm);
1165 const int nvfb = (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
1166 const int luma_stride =
1167 ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols << MI_SIZE_LOG2, 4);
1168
1169 // for the current filter block, it's top left corner mi structure (mi_tl)
1170 // is first accessed to check whether the top and left boundaries are
1171 // frame boundaries. Then bottom-left and top-right mi structures are
1172 // accessed to check whether the bottom and right boundaries
1173 // (respectively) are frame boundaries.
1174 //
1175 // Note that we can't just check the bottom-right mi structure - eg. if
1176 // we're at the right-hand edge of the frame but not the bottom, then
1177 // the bottom-right mi is NULL but the bottom-left is not.
1178 fb_info->frame_boundary[TOP] = (MI_SIZE_64X64 * fbr == 0) ? 1 : 0;
1179 if (fbr != nvfb - 1)
1180 fb_info->frame_boundary[BOTTOM] =
1181 (MI_SIZE_64X64 * (fbr + 1) == cm->mi_params.mi_rows) ? 1 : 0;
1182 else
1183 fb_info->frame_boundary[BOTTOM] = 1;
1184
1185 fb_info->src = src;
1186 fb_info->damping = cm->cdef_info.cdef_damping;
1187 fb_info->coeff_shift = AOMMAX(cm->seq_params->bit_depth - 8, 0);
1188 av1_zero(fb_info->dir);
1189 av1_zero(fb_info->var);
1190
1191 for (int plane = 0; plane < num_planes; plane++) {
1192 const int stride = luma_stride >> xd->plane[plane].subsampling_x;
1193 uint16_t *top_linebuf = &linebuf[plane][0];
1194 uint16_t *bot_linebuf = &linebuf[plane][nvfb * CDEF_VBORDER * stride];
1195 {
1196 const int mi_high_l2 = MI_SIZE_LOG2 - xd->plane[plane].subsampling_y;
1197 const int top_offset = MI_SIZE_64X64 * (fbr + 1) << mi_high_l2;
1198 const int bot_offset = MI_SIZE_64X64 * (fbr + 1) << mi_high_l2;
1199
1200 if (fbr != nvfb - 1) // if (fbr != 0) // top line buffer copy
1201 av1_cdef_copy_sb8_16(
1202 cm, &top_linebuf[(fbr + 1) * CDEF_VBORDER * stride], stride,
1203 xd->plane[plane].dst.buf, top_offset - CDEF_VBORDER, 0,
1204 xd->plane[plane].dst.stride, CDEF_VBORDER, stride);
1205 if (fbr != nvfb - 1) // bottom line buffer copy
1206 av1_cdef_copy_sb8_16(cm, &bot_linebuf[fbr * CDEF_VBORDER * stride],
1207 stride, xd->plane[plane].dst.buf, bot_offset, 0,
1208 xd->plane[plane].dst.stride, CDEF_VBORDER, stride);
1209 }
1210
1211 fb_info->top_linebuf[plane] = &linebuf[plane][fbr * CDEF_VBORDER * stride];
1212 fb_info->bot_linebuf[plane] =
1213 &linebuf[plane]
1214 [nvfb * CDEF_VBORDER * stride + (fbr * CDEF_VBORDER * stride)];
1215 }
1216
1217 cdef_row_mt_sync_write(cdef_sync, fbr);
1218 cdef_row_mt_sync_read(cdef_sync, fbr);
1219 }
1220
1221 // Implements multi-threading for CDEF.
1222 // Perform CDEF on input frame.
1223 // Inputs:
1224 // frame: Pointer to input frame buffer.
1225 // cm: Pointer to common structure.
1226 // xd: Pointer to common current coding block structure.
1227 // Returns:
1228 // Nothing will be returned.
av1_cdef_frame_mt(AV1_COMMON * const cm,MACROBLOCKD * const xd,AV1CdefWorkerData * const cdef_worker,AVxWorker * const workers,AV1CdefSync * const cdef_sync,int num_workers,cdef_init_fb_row_t cdef_init_fb_row_fn,int do_extend_border)1229 void av1_cdef_frame_mt(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1230 AV1CdefWorkerData *const cdef_worker,
1231 AVxWorker *const workers, AV1CdefSync *const cdef_sync,
1232 int num_workers, cdef_init_fb_row_t cdef_init_fb_row_fn,
1233 int do_extend_border) {
1234 YV12_BUFFER_CONFIG *frame = &cm->cur_frame->buf;
1235 const int num_planes = av1_num_planes(cm);
1236
1237 av1_setup_dst_planes(xd->plane, cm->seq_params->sb_size, frame, 0, 0, 0,
1238 num_planes);
1239
1240 reset_cdef_job_info(cdef_sync);
1241 prepare_cdef_frame_workers(cm, xd, cdef_worker, cdef_sb_row_worker_hook,
1242 workers, cdef_sync, num_workers,
1243 cdef_init_fb_row_fn, do_extend_border);
1244 launch_cdef_workers(workers, num_workers);
1245 sync_cdef_workers(workers, cm, num_workers);
1246 }
1247
av1_get_intrabc_extra_top_right_sb_delay(const AV1_COMMON * cm)1248 int av1_get_intrabc_extra_top_right_sb_delay(const AV1_COMMON *cm) {
1249 // No additional top-right delay when intraBC tool is not enabled.
1250 if (!av1_allow_intrabc(cm)) return 0;
1251 // Due to the hardware constraints on processing the intraBC tool with row
1252 // multithreading, a top-right delay of 3 superblocks of size 128x128 or 5
1253 // superblocks of size 64x64 is mandated. However, a minimum top-right delay
1254 // of 1 superblock is assured with 'sync_range'. Hence return only the
1255 // additional superblock delay when the intraBC tool is enabled.
1256 return cm->seq_params->sb_size == BLOCK_128X128 ? 2 : 4;
1257 }
1258