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