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 #include "vp9/common/vp9_reconinter.h" |
| 13 #include "vp9/decoder/vp9_dthread.h" |
| 14 #include "vp9/decoder/vp9_onyxd_int.h" |
| 15 #include "vpx_mem/vpx_mem.h" |
| 16 |
| 17 #if CONFIG_MULTITHREAD |
| 18 static INLINE void mutex_lock(pthread_mutex_t *const mutex) { |
| 19 const int kMaxTryLocks = 4000; |
| 20 int locked = 0; |
| 21 int i; |
| 22 |
| 23 for (i = 0; i < kMaxTryLocks; ++i) { |
| 24 if (!pthread_mutex_trylock(mutex)) { |
| 25 locked = 1; |
| 26 break; |
| 27 } |
| 28 } |
| 29 |
| 30 if (!locked) |
| 31 pthread_mutex_lock(mutex); |
| 32 } |
| 33 #endif // CONFIG_MULTITHREAD |
| 34 |
| 35 static INLINE void sync_read(VP9LfSync *const lf_sync, int r, int c) { |
| 36 #if CONFIG_MULTITHREAD |
| 37 const int nsync = lf_sync->sync_range; |
| 38 |
| 39 if (r && !(c & (nsync - 1))) { |
| 40 mutex_lock(&lf_sync->mutex_[r - 1]); |
| 41 |
| 42 while (c > lf_sync->cur_sb_col[r - 1] - nsync) { |
| 43 pthread_cond_wait(&lf_sync->cond_[r - 1], |
| 44 &lf_sync->mutex_[r - 1]); |
| 45 } |
| 46 pthread_mutex_unlock(&lf_sync->mutex_[r - 1]); |
| 47 } |
| 48 #else |
| 49 (void)lf_sync; |
| 50 (void)r; |
| 51 (void)c; |
| 52 #endif // CONFIG_MULTITHREAD |
| 53 } |
| 54 |
| 55 static INLINE void sync_write(VP9LfSync *const lf_sync, int r, int c, |
| 56 const int sb_cols) { |
| 57 #if CONFIG_MULTITHREAD |
| 58 const int nsync = lf_sync->sync_range; |
| 59 int cur; |
| 60 // Only signal when there are enough filtered SB for next row to run. |
| 61 int sig = 1; |
| 62 |
| 63 if (c < sb_cols - 1) { |
| 64 cur = c; |
| 65 if (c % nsync) |
| 66 sig = 0; |
| 67 } else { |
| 68 cur = sb_cols + nsync; |
| 69 } |
| 70 |
| 71 if (sig) { |
| 72 mutex_lock(&lf_sync->mutex_[r]); |
| 73 |
| 74 lf_sync->cur_sb_col[r] = cur; |
| 75 |
| 76 pthread_cond_signal(&lf_sync->cond_[r]); |
| 77 pthread_mutex_unlock(&lf_sync->mutex_[r]); |
| 78 } |
| 79 #else |
| 80 (void)lf_sync; |
| 81 (void)r; |
| 82 (void)c; |
| 83 (void)sb_cols; |
| 84 #endif // CONFIG_MULTITHREAD |
| 85 } |
| 86 |
| 87 // Implement row loopfiltering for each thread. |
| 88 static void loop_filter_rows_mt(const YV12_BUFFER_CONFIG *const frame_buffer, |
| 89 VP9_COMMON *const cm, MACROBLOCKD *const xd, |
| 90 int start, int stop, int y_only, |
| 91 VP9LfSync *const lf_sync, int num_lf_workers) { |
| 92 const int num_planes = y_only ? 1 : MAX_MB_PLANE; |
| 93 int r, c; // SB row and col |
| 94 LOOP_FILTER_MASK lfm; |
| 95 const int sb_cols = mi_cols_aligned_to_sb(cm->mi_cols) >> MI_BLOCK_SIZE_LOG2; |
| 96 |
| 97 for (r = start; r < stop; r += num_lf_workers) { |
| 98 const int mi_row = r << MI_BLOCK_SIZE_LOG2; |
| 99 MODE_INFO **mi_8x8 = cm->mi_grid_visible + mi_row * cm->mode_info_stride; |
| 100 |
| 101 for (c = 0; c < sb_cols; ++c) { |
| 102 const int mi_col = c << MI_BLOCK_SIZE_LOG2; |
| 103 int plane; |
| 104 |
| 105 sync_read(lf_sync, r, c); |
| 106 |
| 107 setup_dst_planes(xd, frame_buffer, mi_row, mi_col); |
| 108 vp9_setup_mask(cm, mi_row, mi_col, mi_8x8 + mi_col, cm->mode_info_stride, |
| 109 &lfm); |
| 110 |
| 111 for (plane = 0; plane < num_planes; ++plane) { |
| 112 vp9_filter_block_plane(cm, &xd->plane[plane], mi_row, &lfm); |
| 113 } |
| 114 |
| 115 sync_write(lf_sync, r, c, sb_cols); |
| 116 } |
| 117 } |
| 118 } |
| 119 |
| 120 // Row-based multi-threaded loopfilter hook |
| 121 static int loop_filter_row_worker(void *arg1, void *arg2) { |
| 122 TileWorkerData *const tile_data = (TileWorkerData*)arg1; |
| 123 LFWorkerData *const lf_data = &tile_data->lfdata; |
| 124 |
| 125 loop_filter_rows_mt(lf_data->frame_buffer, lf_data->cm, &lf_data->xd, |
| 126 lf_data->start, lf_data->stop, lf_data->y_only, |
| 127 lf_data->lf_sync, lf_data->num_lf_workers); |
| 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(VP9D_COMP *pbi, |
| 134 VP9_COMMON *cm, |
| 135 MACROBLOCKD *xd, |
| 136 int frame_filter_level, |
| 137 int y_only, int partial_frame) { |
| 138 // Number of superblock rows and cols |
| 139 const int sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2; |
| 140 int i; |
| 141 |
| 142 // Allocate memory used in thread synchronization. |
| 143 // This always needs to be done even if frame_filter_level is 0. |
| 144 if (!cm->current_video_frame || cm->last_height != cm->height) { |
| 145 VP9LfSync *const lf_sync = &pbi->lf_row_sync; |
| 146 |
| 147 if (cm->last_height != cm->height) { |
| 148 const int aligned_last_height = |
| 149 ALIGN_POWER_OF_TWO(cm->last_height, MI_SIZE_LOG2); |
| 150 const int last_sb_rows = |
| 151 mi_cols_aligned_to_sb(aligned_last_height >> MI_SIZE_LOG2) >> |
| 152 MI_BLOCK_SIZE_LOG2; |
| 153 |
| 154 vp9_loop_filter_dealloc(lf_sync, last_sb_rows); |
| 155 } |
| 156 |
| 157 vp9_loop_filter_alloc(cm, lf_sync, sb_rows, cm->width); |
| 158 } |
| 159 |
| 160 if (!frame_filter_level) return; |
| 161 |
| 162 vp9_loop_filter_frame_init(cm, frame_filter_level); |
| 163 |
| 164 // Initialize cur_sb_col to -1 for all SB rows. |
| 165 vpx_memset(pbi->lf_row_sync.cur_sb_col, -1, |
| 166 sizeof(*pbi->lf_row_sync.cur_sb_col) * sb_rows); |
| 167 |
| 168 // Set up loopfilter thread data. |
| 169 for (i = 0; i < pbi->num_tile_workers; ++i) { |
| 170 VP9Worker *const worker = &pbi->tile_workers[i]; |
| 171 TileWorkerData *const tile_data = (TileWorkerData*)worker->data1; |
| 172 LFWorkerData *const lf_data = &tile_data->lfdata; |
| 173 |
| 174 worker->hook = (VP9WorkerHook)loop_filter_row_worker; |
| 175 |
| 176 // Loopfilter data |
| 177 lf_data->frame_buffer = get_frame_new_buffer(cm); |
| 178 lf_data->cm = cm; |
| 179 lf_data->xd = pbi->mb; |
| 180 lf_data->start = i; |
| 181 lf_data->stop = sb_rows; |
| 182 lf_data->y_only = y_only; // always do all planes in decoder |
| 183 |
| 184 lf_data->lf_sync = &pbi->lf_row_sync; |
| 185 lf_data->num_lf_workers = pbi->num_tile_workers; |
| 186 |
| 187 // Start loopfiltering |
| 188 if (i == pbi->num_tile_workers - 1) { |
| 189 vp9_worker_execute(worker); |
| 190 } else { |
| 191 vp9_worker_launch(worker); |
| 192 } |
| 193 } |
| 194 |
| 195 // Wait till all rows are finished |
| 196 for (i = 0; i < pbi->num_tile_workers; ++i) { |
| 197 vp9_worker_sync(&pbi->tile_workers[i]); |
| 198 } |
| 199 } |
| 200 |
| 201 // Set up nsync by width. |
| 202 static int get_sync_range(int width) { |
| 203 // nsync numbers are picked by testing. For example, for 4k |
| 204 // video, using 4 gives best performance. |
| 205 if (width < 640) |
| 206 return 1; |
| 207 else if (width <= 1280) |
| 208 return 2; |
| 209 else if (width <= 4096) |
| 210 return 4; |
| 211 else |
| 212 return 8; |
| 213 } |
| 214 |
| 215 // Allocate memory for lf row synchronization |
| 216 void vp9_loop_filter_alloc(VP9_COMMON *cm, VP9LfSync *lf_sync, int rows, |
| 217 int width) { |
| 218 #if CONFIG_MULTITHREAD |
| 219 int i; |
| 220 |
| 221 CHECK_MEM_ERROR(cm, lf_sync->mutex_, |
| 222 vpx_malloc(sizeof(*lf_sync->mutex_) * rows)); |
| 223 for (i = 0; i < rows; ++i) { |
| 224 pthread_mutex_init(&lf_sync->mutex_[i], NULL); |
| 225 } |
| 226 |
| 227 CHECK_MEM_ERROR(cm, lf_sync->cond_, |
| 228 vpx_malloc(sizeof(*lf_sync->cond_) * rows)); |
| 229 for (i = 0; i < rows; ++i) { |
| 230 pthread_cond_init(&lf_sync->cond_[i], NULL); |
| 231 } |
| 232 #endif // CONFIG_MULTITHREAD |
| 233 |
| 234 CHECK_MEM_ERROR(cm, lf_sync->cur_sb_col, |
| 235 vpx_malloc(sizeof(*lf_sync->cur_sb_col) * rows)); |
| 236 |
| 237 // Set up nsync. |
| 238 lf_sync->sync_range = get_sync_range(width); |
| 239 } |
| 240 |
| 241 // Deallocate lf synchronization related mutex and data |
| 242 void vp9_loop_filter_dealloc(VP9LfSync *lf_sync, int rows) { |
| 243 #if CONFIG_MULTITHREAD |
| 244 if (lf_sync != NULL) { |
| 245 int i; |
| 246 |
| 247 if (lf_sync->mutex_ != NULL) { |
| 248 for (i = 0; i < rows; ++i) { |
| 249 pthread_mutex_destroy(&lf_sync->mutex_[i]); |
| 250 } |
| 251 vpx_free(lf_sync->mutex_); |
| 252 } |
| 253 if (lf_sync->cond_ != NULL) { |
| 254 for (i = 0; i < rows; ++i) { |
| 255 pthread_cond_destroy(&lf_sync->cond_[i]); |
| 256 } |
| 257 vpx_free(lf_sync->cond_); |
| 258 } |
| 259 |
| 260 vpx_free(lf_sync->cur_sb_col); |
| 261 // clear the structure as the source of this call may be a resize in which |
| 262 // case this call will be followed by an _alloc() which may fail. |
| 263 vpx_memset(lf_sync, 0, sizeof(*lf_sync)); |
| 264 } |
| 265 #else |
| 266 (void)rows; |
| 267 if (lf_sync != NULL) { |
| 268 vpx_free(lf_sync->cur_sb_col); |
| 269 vpx_memset(lf_sync, 0, sizeof(*lf_sync)); |
| 270 } |
| 271 #endif // CONFIG_MULTITHREAD |
| 272 } |
OLD | NEW |