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