2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include "./vpx_config.h"
13 #include "vpx_mem/vpx_mem.h"
15 #include "vp9/common/vp9_reconinter.h"
17 #include "vp9/decoder/vp9_dthread.h"
18 #include "vp9/decoder/vp9_decoder.h"
20 #if CONFIG_MULTITHREAD
21 static INLINE void mutex_lock(pthread_mutex_t *const mutex) {
22 const int kMaxTryLocks = 4000;
26 for (i = 0; i < kMaxTryLocks; ++i) {
27 if (!pthread_mutex_trylock(mutex)) {
34 pthread_mutex_lock(mutex);
36 #endif // CONFIG_MULTITHREAD
38 static INLINE void sync_read(VP9LfSync *const lf_sync, int r, int c) {
39 #if CONFIG_MULTITHREAD
40 const int nsync = lf_sync->sync_range;
42 if (r && !(c & (nsync - 1))) {
43 pthread_mutex_t *const mutex = &lf_sync->mutex_[r - 1];
46 while (c > lf_sync->cur_sb_col[r - 1] - nsync) {
47 pthread_cond_wait(&lf_sync->cond_[r - 1], mutex);
49 pthread_mutex_unlock(mutex);
55 #endif // CONFIG_MULTITHREAD
58 static INLINE void sync_write(VP9LfSync *const lf_sync, int r, int c,
60 #if CONFIG_MULTITHREAD
61 const int nsync = lf_sync->sync_range;
63 // Only signal when there are enough filtered SB for next row to run.
66 if (c < sb_cols - 1) {
71 cur = sb_cols + nsync;
75 mutex_lock(&lf_sync->mutex_[r]);
77 lf_sync->cur_sb_col[r] = cur;
79 pthread_cond_signal(&lf_sync->cond_[r]);
80 pthread_mutex_unlock(&lf_sync->mutex_[r]);
87 #endif // CONFIG_MULTITHREAD
90 // Implement row loopfiltering for each thread.
91 static void loop_filter_rows_mt(const YV12_BUFFER_CONFIG *const frame_buffer,
92 VP9_COMMON *const cm, MACROBLOCKD *const xd,
93 int start, int stop, int y_only,
94 VP9LfSync *const lf_sync, int num_lf_workers) {
95 const int num_planes = y_only ? 1 : MAX_MB_PLANE;
96 int r, c; // SB row and col
97 const int sb_cols = mi_cols_aligned_to_sb(cm->mi_cols) >> MI_BLOCK_SIZE_LOG2;
99 for (r = start; r < stop; r += num_lf_workers) {
100 const int mi_row = r << MI_BLOCK_SIZE_LOG2;
101 MODE_INFO **const mi = cm->mi_grid_visible + mi_row * cm->mi_stride;
103 for (c = 0; c < sb_cols; ++c) {
104 const int mi_col = c << MI_BLOCK_SIZE_LOG2;
105 LOOP_FILTER_MASK lfm;
108 sync_read(lf_sync, r, c);
110 vp9_setup_dst_planes(xd, frame_buffer, mi_row, mi_col);
111 vp9_setup_mask(cm, mi_row, mi_col, mi + mi_col, cm->mi_stride, &lfm);
113 for (plane = 0; plane < num_planes; ++plane) {
114 vp9_filter_block_plane(cm, &xd->plane[plane], mi_row, &lfm);
117 sync_write(lf_sync, r, c, sb_cols);
122 // Row-based multi-threaded loopfilter hook
123 static int loop_filter_row_worker(void *arg1, void *arg2) {
124 TileWorkerData *const tile_data = (TileWorkerData*)arg1;
125 LFWorkerData *const lf_data = &tile_data->lfdata;
127 loop_filter_rows_mt(lf_data->frame_buffer, lf_data->cm, &lf_data->xd,
128 lf_data->start, lf_data->stop, lf_data->y_only,
129 lf_data->lf_sync, lf_data->num_lf_workers);
133 // VP9 decoder: Implement multi-threaded loopfilter that uses the tile
135 void vp9_loop_filter_frame_mt(VP9Decoder *pbi,
137 int frame_filter_level,
138 int y_only, int partial_frame) {
139 VP9LfSync *const lf_sync = &pbi->lf_row_sync;
140 // Number of superblock rows and cols
141 const int sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
142 const int tile_cols = 1 << cm->log2_tile_cols;
143 const int num_workers = MIN(pbi->oxcf.max_threads & ~1, tile_cols);
146 // Allocate memory used in thread synchronization.
147 // This always needs to be done even if frame_filter_level is 0.
148 if (!cm->current_video_frame || cm->last_height != cm->height) {
149 if (cm->last_height != cm->height) {
150 const int aligned_last_height =
151 ALIGN_POWER_OF_TWO(cm->last_height, MI_SIZE_LOG2);
152 const int last_sb_rows =
153 mi_cols_aligned_to_sb(aligned_last_height >> MI_SIZE_LOG2) >>
156 vp9_loop_filter_dealloc(lf_sync, last_sb_rows);
159 vp9_loop_filter_alloc(cm, lf_sync, sb_rows, cm->width);
162 if (!frame_filter_level) return;
164 vp9_loop_filter_frame_init(cm, frame_filter_level);
166 // Initialize cur_sb_col to -1 for all SB rows.
167 vpx_memset(lf_sync->cur_sb_col, -1, sizeof(*lf_sync->cur_sb_col) * sb_rows);
169 // Set up loopfilter thread data.
170 // The decoder is using num_workers instead of pbi->num_tile_workers
171 // because it has been observed that using more threads on the
172 // loopfilter, than there are tile columns in the frame will hurt
173 // performance on Android. This is because the system will only
174 // schedule the tile decode workers on cores equal to the number
175 // of tile columns. Then if the decoder tries to use more threads for the
176 // loopfilter, it will hurt performance because of contention. If the
177 // multithreading code changes in the future then the number of workers
178 // used by the loopfilter should be revisited.
179 for (i = 0; i < num_workers; ++i) {
180 VP9Worker *const worker = &pbi->tile_workers[i];
181 TileWorkerData *const tile_data = (TileWorkerData*)worker->data1;
182 LFWorkerData *const lf_data = &tile_data->lfdata;
184 worker->hook = (VP9WorkerHook)loop_filter_row_worker;
187 lf_data->frame_buffer = get_frame_new_buffer(cm);
189 lf_data->xd = pbi->mb;
191 lf_data->stop = sb_rows;
192 lf_data->y_only = y_only; // always do all planes in decoder
194 lf_data->lf_sync = lf_sync;
195 lf_data->num_lf_workers = num_workers;
197 // Start loopfiltering
198 if (i == num_workers - 1) {
199 vp9_worker_execute(worker);
201 vp9_worker_launch(worker);
205 // Wait till all rows are finished
206 for (i = 0; i < num_workers; ++i) {
207 vp9_worker_sync(&pbi->tile_workers[i]);
211 // Set up nsync by width.
212 static int get_sync_range(int width) {
213 // nsync numbers are picked by testing. For example, for 4k
214 // video, using 4 gives best performance.
217 else if (width <= 1280)
219 else if (width <= 4096)
225 // Allocate memory for lf row synchronization
226 void vp9_loop_filter_alloc(VP9_COMMON *cm, VP9LfSync *lf_sync, int rows,
228 #if CONFIG_MULTITHREAD
231 CHECK_MEM_ERROR(cm, lf_sync->mutex_,
232 vpx_malloc(sizeof(*lf_sync->mutex_) * rows));
233 for (i = 0; i < rows; ++i) {
234 pthread_mutex_init(&lf_sync->mutex_[i], NULL);
237 CHECK_MEM_ERROR(cm, lf_sync->cond_,
238 vpx_malloc(sizeof(*lf_sync->cond_) * rows));
239 for (i = 0; i < rows; ++i) {
240 pthread_cond_init(&lf_sync->cond_[i], NULL);
242 #endif // CONFIG_MULTITHREAD
244 CHECK_MEM_ERROR(cm, lf_sync->cur_sb_col,
245 vpx_malloc(sizeof(*lf_sync->cur_sb_col) * rows));
248 lf_sync->sync_range = get_sync_range(width);
251 // Deallocate lf synchronization related mutex and data
252 void vp9_loop_filter_dealloc(VP9LfSync *lf_sync, int rows) {
253 #if !CONFIG_MULTITHREAD
255 #endif // !CONFIG_MULTITHREAD
257 if (lf_sync != NULL) {
258 #if CONFIG_MULTITHREAD
261 if (lf_sync->mutex_ != NULL) {
262 for (i = 0; i < rows; ++i) {
263 pthread_mutex_destroy(&lf_sync->mutex_[i]);
265 vpx_free(lf_sync->mutex_);
267 if (lf_sync->cond_ != NULL) {
268 for (i = 0; i < rows; ++i) {
269 pthread_cond_destroy(&lf_sync->cond_[i]);
271 vpx_free(lf_sync->cond_);
273 #endif // CONFIG_MULTITHREAD
274 vpx_free(lf_sync->cur_sb_col);
275 // clear the structure as the source of this call may be a resize in which
276 // case this call will be followed by an _alloc() which may fail.