| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved. | |
| 3 * | |
| 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. | |
| 9 */ | |
| 10 | |
| 11 #include "./vpx_config.h" | |
| 12 | |
| 13 #include "vpx_mem/vpx_mem.h" | |
| 14 | |
| 15 #include "vp9/common/vp9_reconinter.h" | |
| 16 | |
| 17 #include "vp9/decoder/vp9_dthread.h" | |
| 18 #include "vp9/decoder/vp9_decoder.h" | |
| 19 | |
| 20 #if CONFIG_MULTITHREAD | |
| 21 static INLINE void mutex_lock(pthread_mutex_t *const mutex) { | |
| 22 const int kMaxTryLocks = 4000; | |
| 23 int locked = 0; | |
| 24 int i; | |
| 25 | |
| 26 for (i = 0; i < kMaxTryLocks; ++i) { | |
| 27 if (!pthread_mutex_trylock(mutex)) { | |
| 28 locked = 1; | |
| 29 break; | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 if (!locked) | |
| 34 pthread_mutex_lock(mutex); | |
| 35 } | |
| 36 #endif // CONFIG_MULTITHREAD | |
| 37 | |
| 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; | |
| 41 | |
| 42 if (r && !(c & (nsync - 1))) { | |
| 43 pthread_mutex_t *const mutex = &lf_sync->mutex_[r - 1]; | |
| 44 mutex_lock(mutex); | |
| 45 | |
| 46 while (c > lf_sync->cur_sb_col[r - 1] - nsync) { | |
| 47 pthread_cond_wait(&lf_sync->cond_[r - 1], mutex); | |
| 48 } | |
| 49 pthread_mutex_unlock(mutex); | |
| 50 } | |
| 51 #else | |
| 52 (void)lf_sync; | |
| 53 (void)r; | |
| 54 (void)c; | |
| 55 #endif // CONFIG_MULTITHREAD | |
| 56 } | |
| 57 | |
| 58 static INLINE void sync_write(VP9LfSync *const lf_sync, int r, int c, | |
| 59 const int sb_cols) { | |
| 60 #if CONFIG_MULTITHREAD | |
| 61 const int nsync = lf_sync->sync_range; | |
| 62 int cur; | |
| 63 // Only signal when there are enough filtered SB for next row to run. | |
| 64 int sig = 1; | |
| 65 | |
| 66 if (c < sb_cols - 1) { | |
| 67 cur = c; | |
| 68 if (c % nsync) | |
| 69 sig = 0; | |
| 70 } else { | |
| 71 cur = sb_cols + nsync; | |
| 72 } | |
| 73 | |
| 74 if (sig) { | |
| 75 mutex_lock(&lf_sync->mutex_[r]); | |
| 76 | |
| 77 lf_sync->cur_sb_col[r] = cur; | |
| 78 | |
| 79 pthread_cond_signal(&lf_sync->cond_[r]); | |
| 80 pthread_mutex_unlock(&lf_sync->mutex_[r]); | |
| 81 } | |
| 82 #else | |
| 83 (void)lf_sync; | |
| 84 (void)r; | |
| 85 (void)c; | |
| 86 (void)sb_cols; | |
| 87 #endif // CONFIG_MULTITHREAD | |
| 88 } | |
| 89 | |
| 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, | |
| 93 struct macroblockd_plane planes[MAX_MB_PLANE], | |
| 94 int start, int stop, int y_only, | |
| 95 VP9LfSync *const lf_sync) { | |
| 96 const int num_planes = y_only ? 1 : MAX_MB_PLANE; | |
| 97 int r, c; // SB row and col | |
| 98 const int sb_cols = mi_cols_aligned_to_sb(cm->mi_cols) >> MI_BLOCK_SIZE_LOG2; | |
| 99 | |
| 100 for (r = start; r < stop; r += lf_sync->num_workers) { | |
| 101 const int mi_row = r << MI_BLOCK_SIZE_LOG2; | |
| 102 MODE_INFO *const mi = cm->mi + mi_row * cm->mi_stride; | |
| 103 | |
| 104 for (c = 0; c < sb_cols; ++c) { | |
| 105 const int mi_col = c << MI_BLOCK_SIZE_LOG2; | |
| 106 LOOP_FILTER_MASK lfm; | |
| 107 int plane; | |
| 108 | |
| 109 sync_read(lf_sync, r, c); | |
| 110 | |
| 111 vp9_setup_dst_planes(planes, frame_buffer, mi_row, mi_col); | |
| 112 vp9_setup_mask(cm, mi_row, mi_col, mi + mi_col, cm->mi_stride, &lfm); | |
| 113 | |
| 114 for (plane = 0; plane < num_planes; ++plane) { | |
| 115 vp9_filter_block_plane(cm, &planes[plane], mi_row, &lfm); | |
| 116 } | |
| 117 | |
| 118 sync_write(lf_sync, r, c, sb_cols); | |
| 119 } | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 // Row-based multi-threaded loopfilter hook | |
| 124 static int loop_filter_row_worker(VP9LfSync *const lf_sync, | |
| 125 LFWorkerData *const lf_data) { | |
| 126 loop_filter_rows_mt(lf_data->frame_buffer, lf_data->cm, lf_data->planes, | |
| 127 lf_data->start, lf_data->stop, lf_data->y_only, lf_sync); | |
| 128 return 1; | |
| 129 } | |
| 130 | |
| 131 // VP9 decoder: Implement multi-threaded loopfilter that uses the tile | |
| 132 // threads. | |
| 133 void vp9_loop_filter_frame_mt(VP9LfSync *lf_sync, | |
| 134 YV12_BUFFER_CONFIG *frame, | |
| 135 struct macroblockd_plane planes[MAX_MB_PLANE], | |
| 136 VP9_COMMON *cm, | |
| 137 VP9Worker *workers, int nworkers, | |
| 138 int frame_filter_level, | |
| 139 int y_only) { | |
| 140 const VP9WorkerInterface *const winterface = vp9_get_worker_interface(); | |
| 141 // Number of superblock rows and cols | |
| 142 const int sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2; | |
| 143 const int tile_cols = 1 << cm->log2_tile_cols; | |
| 144 const int num_workers = MIN(nworkers, tile_cols); | |
| 145 int i; | |
| 146 | |
| 147 if (!frame_filter_level) return; | |
| 148 | |
| 149 if (!lf_sync->sync_range || cm->last_height != cm->height || | |
| 150 num_workers > lf_sync->num_workers) { | |
| 151 vp9_loop_filter_dealloc(lf_sync); | |
| 152 vp9_loop_filter_alloc(lf_sync, cm, sb_rows, cm->width, num_workers); | |
| 153 } | |
| 154 | |
| 155 vp9_loop_filter_frame_init(cm, frame_filter_level); | |
| 156 | |
| 157 // Initialize cur_sb_col to -1 for all SB rows. | |
| 158 vpx_memset(lf_sync->cur_sb_col, -1, sizeof(*lf_sync->cur_sb_col) * sb_rows); | |
| 159 | |
| 160 // Set up loopfilter thread data. | |
| 161 // The decoder is capping num_workers because it has been observed that using | |
| 162 // more threads on the loopfilter than there are cores will hurt performance | |
| 163 // on Android. This is because the system will only schedule the tile decode | |
| 164 // workers on cores equal to the number of tile columns. Then if the decoder | |
| 165 // tries to use more threads for the loopfilter, it will hurt performance | |
| 166 // because of contention. If the multithreading code changes in the future | |
| 167 // then the number of workers used by the loopfilter should be revisited. | |
| 168 for (i = 0; i < num_workers; ++i) { | |
| 169 VP9Worker *const worker = &workers[i]; | |
| 170 LFWorkerData *const lf_data = &lf_sync->lfdata[i]; | |
| 171 | |
| 172 worker->hook = (VP9WorkerHook)loop_filter_row_worker; | |
| 173 worker->data1 = lf_sync; | |
| 174 worker->data2 = lf_data; | |
| 175 | |
| 176 // Loopfilter data | |
| 177 vp9_loop_filter_data_reset(lf_data, frame, cm, planes); | |
| 178 lf_data->start = i; | |
| 179 lf_data->stop = sb_rows; | |
| 180 lf_data->y_only = y_only; | |
| 181 | |
| 182 // Start loopfiltering | |
| 183 if (i == num_workers - 1) { | |
| 184 winterface->execute(worker); | |
| 185 } else { | |
| 186 winterface->launch(worker); | |
| 187 } | |
| 188 } | |
| 189 | |
| 190 // Wait till all rows are finished | |
| 191 for (i = 0; i < num_workers; ++i) { | |
| 192 winterface->sync(&workers[i]); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 // Set up nsync by width. | |
| 197 static int get_sync_range(int width) { | |
| 198 // nsync numbers are picked by testing. For example, for 4k | |
| 199 // video, using 4 gives best performance. | |
| 200 if (width < 640) | |
| 201 return 1; | |
| 202 else if (width <= 1280) | |
| 203 return 2; | |
| 204 else if (width <= 4096) | |
| 205 return 4; | |
| 206 else | |
| 207 return 8; | |
| 208 } | |
| 209 | |
| 210 // Allocate memory for lf row synchronization | |
| 211 void vp9_loop_filter_alloc(VP9LfSync *lf_sync, VP9_COMMON *cm, int rows, | |
| 212 int width, int num_workers) { | |
| 213 lf_sync->rows = rows; | |
| 214 #if CONFIG_MULTITHREAD | |
| 215 { | |
| 216 int i; | |
| 217 | |
| 218 CHECK_MEM_ERROR(cm, lf_sync->mutex_, | |
| 219 vpx_malloc(sizeof(*lf_sync->mutex_) * rows)); | |
| 220 if (lf_sync->mutex_) { | |
| 221 for (i = 0; i < rows; ++i) { | |
| 222 pthread_mutex_init(&lf_sync->mutex_[i], NULL); | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 CHECK_MEM_ERROR(cm, lf_sync->cond_, | |
| 227 vpx_malloc(sizeof(*lf_sync->cond_) * rows)); | |
| 228 if (lf_sync->cond_) { | |
| 229 for (i = 0; i < rows; ++i) { | |
| 230 pthread_cond_init(&lf_sync->cond_[i], NULL); | |
| 231 } | |
| 232 } | |
| 233 } | |
| 234 #endif // CONFIG_MULTITHREAD | |
| 235 | |
| 236 CHECK_MEM_ERROR(cm, lf_sync->lfdata, | |
| 237 vpx_malloc(num_workers * sizeof(*lf_sync->lfdata))); | |
| 238 lf_sync->num_workers = num_workers; | |
| 239 | |
| 240 CHECK_MEM_ERROR(cm, lf_sync->cur_sb_col, | |
| 241 vpx_malloc(sizeof(*lf_sync->cur_sb_col) * rows)); | |
| 242 | |
| 243 // Set up nsync. | |
| 244 lf_sync->sync_range = get_sync_range(width); | |
| 245 } | |
| 246 | |
| 247 // Deallocate lf synchronization related mutex and data | |
| 248 void vp9_loop_filter_dealloc(VP9LfSync *lf_sync) { | |
| 249 if (lf_sync != NULL) { | |
| 250 #if CONFIG_MULTITHREAD | |
| 251 int i; | |
| 252 | |
| 253 if (lf_sync->mutex_ != NULL) { | |
| 254 for (i = 0; i < lf_sync->rows; ++i) { | |
| 255 pthread_mutex_destroy(&lf_sync->mutex_[i]); | |
| 256 } | |
| 257 vpx_free(lf_sync->mutex_); | |
| 258 } | |
| 259 if (lf_sync->cond_ != NULL) { | |
| 260 for (i = 0; i < lf_sync->rows; ++i) { | |
| 261 pthread_cond_destroy(&lf_sync->cond_[i]); | |
| 262 } | |
| 263 vpx_free(lf_sync->cond_); | |
| 264 } | |
| 265 #endif // CONFIG_MULTITHREAD | |
| 266 vpx_free(lf_sync->lfdata); | |
| 267 vpx_free(lf_sync->cur_sb_col); | |
| 268 // clear the structure as the source of this call may be a resize in which | |
| 269 // case this call will be followed by an _alloc() which may fail. | |
| 270 vp9_zero(*lf_sync); | |
| 271 } | |
| 272 } | |
| OLD | NEW |