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