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