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