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