vp9_thread_common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. #include "./vpx_config.h"
  11. #include "vpx_dsp/vpx_dsp_common.h"
  12. #include "vpx_mem/vpx_mem.h"
  13. #include "vp9/common/vp9_entropymode.h"
  14. #include "vp9/common/vp9_thread_common.h"
  15. #include "vp9/common/vp9_reconinter.h"
  16. #include "vp9/common/vp9_loopfilter.h"
  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. for (i = 0; i < kMaxTryLocks; ++i) {
  23. if (!pthread_mutex_trylock(mutex)) {
  24. locked = 1;
  25. break;
  26. }
  27. }
  28. if (!locked) pthread_mutex_lock(mutex);
  29. }
  30. #endif // CONFIG_MULTITHREAD
  31. static INLINE void sync_read(VP9LfSync *const lf_sync, int r, int c) {
  32. #if CONFIG_MULTITHREAD
  33. const int nsync = lf_sync->sync_range;
  34. if (r && !(c & (nsync - 1))) {
  35. pthread_mutex_t *const mutex = &lf_sync->mutex_[r - 1];
  36. mutex_lock(mutex);
  37. while (c > lf_sync->cur_sb_col[r - 1] - nsync) {
  38. pthread_cond_wait(&lf_sync->cond_[r - 1], mutex);
  39. }
  40. pthread_mutex_unlock(mutex);
  41. }
  42. #else
  43. (void)lf_sync;
  44. (void)r;
  45. (void)c;
  46. #endif // CONFIG_MULTITHREAD
  47. }
  48. static INLINE void sync_write(VP9LfSync *const lf_sync, int r, int c,
  49. const int sb_cols) {
  50. #if CONFIG_MULTITHREAD
  51. const int nsync = lf_sync->sync_range;
  52. int cur;
  53. // Only signal when there are enough filtered SB for next row to run.
  54. int sig = 1;
  55. if (c < sb_cols - 1) {
  56. cur = c;
  57. if (c % nsync) sig = 0;
  58. } else {
  59. cur = sb_cols + nsync;
  60. }
  61. if (sig) {
  62. mutex_lock(&lf_sync->mutex_[r]);
  63. lf_sync->cur_sb_col[r] = cur;
  64. pthread_cond_signal(&lf_sync->cond_[r]);
  65. pthread_mutex_unlock(&lf_sync->mutex_[r]);
  66. }
  67. #else
  68. (void)lf_sync;
  69. (void)r;
  70. (void)c;
  71. (void)sb_cols;
  72. #endif // CONFIG_MULTITHREAD
  73. }
  74. // Implement row loopfiltering for each thread.
  75. static INLINE void thread_loop_filter_rows(
  76. const YV12_BUFFER_CONFIG *const frame_buffer, VP9_COMMON *const cm,
  77. struct macroblockd_plane planes[MAX_MB_PLANE], int start, int stop,
  78. int y_only, VP9LfSync *const lf_sync) {
  79. const int num_planes = y_only ? 1 : MAX_MB_PLANE;
  80. const int sb_cols = mi_cols_aligned_to_sb(cm->mi_cols) >> MI_BLOCK_SIZE_LOG2;
  81. int mi_row, mi_col;
  82. enum lf_path path;
  83. if (y_only)
  84. path = LF_PATH_444;
  85. else if (planes[1].subsampling_y == 1 && planes[1].subsampling_x == 1)
  86. path = LF_PATH_420;
  87. else if (planes[1].subsampling_y == 0 && planes[1].subsampling_x == 0)
  88. path = LF_PATH_444;
  89. else
  90. path = LF_PATH_SLOW;
  91. for (mi_row = start; mi_row < stop;
  92. mi_row += lf_sync->num_workers * MI_BLOCK_SIZE) {
  93. MODE_INFO **const mi = cm->mi_grid_visible + mi_row * cm->mi_stride;
  94. LOOP_FILTER_MASK *lfm = get_lfm(&cm->lf, mi_row, 0);
  95. for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MI_BLOCK_SIZE, ++lfm) {
  96. const int r = mi_row >> MI_BLOCK_SIZE_LOG2;
  97. const int c = mi_col >> MI_BLOCK_SIZE_LOG2;
  98. int plane;
  99. sync_read(lf_sync, r, c);
  100. vp9_setup_dst_planes(planes, frame_buffer, mi_row, mi_col);
  101. vp9_adjust_mask(cm, mi_row, mi_col, lfm);
  102. vp9_filter_block_plane_ss00(cm, &planes[0], mi_row, lfm);
  103. for (plane = 1; plane < num_planes; ++plane) {
  104. switch (path) {
  105. case LF_PATH_420:
  106. vp9_filter_block_plane_ss11(cm, &planes[plane], mi_row, lfm);
  107. break;
  108. case LF_PATH_444:
  109. vp9_filter_block_plane_ss00(cm, &planes[plane], mi_row, lfm);
  110. break;
  111. case LF_PATH_SLOW:
  112. vp9_filter_block_plane_non420(cm, &planes[plane], mi + mi_col,
  113. mi_row, mi_col);
  114. break;
  115. }
  116. }
  117. sync_write(lf_sync, r, c, sb_cols);
  118. }
  119. }
  120. }
  121. // Row-based multi-threaded loopfilter hook
  122. static int loop_filter_row_worker(VP9LfSync *const lf_sync,
  123. LFWorkerData *const lf_data) {
  124. thread_loop_filter_rows(lf_data->frame_buffer, lf_data->cm, lf_data->planes,
  125. lf_data->start, lf_data->stop, lf_data->y_only,
  126. lf_sync);
  127. return 1;
  128. }
  129. static void loop_filter_rows_mt(YV12_BUFFER_CONFIG *frame, VP9_COMMON *cm,
  130. struct macroblockd_plane planes[MAX_MB_PLANE],
  131. int start, int stop, int y_only,
  132. VPxWorker *workers, int nworkers,
  133. VP9LfSync *lf_sync) {
  134. const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
  135. // Number of superblock rows and cols
  136. const int sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
  137. // Decoder may allocate more threads than number of tiles based on user's
  138. // input.
  139. const int tile_cols = 1 << cm->log2_tile_cols;
  140. const int num_workers = VPXMIN(nworkers, tile_cols);
  141. int i;
  142. if (!lf_sync->sync_range || sb_rows != lf_sync->rows ||
  143. num_workers > lf_sync->num_workers) {
  144. vp9_loop_filter_dealloc(lf_sync);
  145. vp9_loop_filter_alloc(lf_sync, cm, sb_rows, cm->width, num_workers);
  146. }
  147. // Initialize cur_sb_col to -1 for all SB rows.
  148. memset(lf_sync->cur_sb_col, -1, sizeof(*lf_sync->cur_sb_col) * sb_rows);
  149. // Set up loopfilter thread data.
  150. // The decoder is capping num_workers because it has been observed that using
  151. // more threads on the loopfilter than there are cores will hurt performance
  152. // on Android. This is because the system will only schedule the tile decode
  153. // workers on cores equal to the number of tile columns. Then if the decoder
  154. // tries to use more threads for the loopfilter, it will hurt performance
  155. // because of contention. If the multithreading code changes in the future
  156. // then the number of workers used by the loopfilter should be revisited.
  157. for (i = 0; i < num_workers; ++i) {
  158. VPxWorker *const worker = &workers[i];
  159. LFWorkerData *const lf_data = &lf_sync->lfdata[i];
  160. worker->hook = (VPxWorkerHook)loop_filter_row_worker;
  161. worker->data1 = lf_sync;
  162. worker->data2 = lf_data;
  163. // Loopfilter data
  164. vp9_loop_filter_data_reset(lf_data, frame, cm, planes);
  165. lf_data->start = start + i * MI_BLOCK_SIZE;
  166. lf_data->stop = stop;
  167. lf_data->y_only = y_only;
  168. // Start loopfiltering
  169. if (i == num_workers - 1) {
  170. winterface->execute(worker);
  171. } else {
  172. winterface->launch(worker);
  173. }
  174. }
  175. // Wait till all rows are finished
  176. for (i = 0; i < num_workers; ++i) {
  177. winterface->sync(&workers[i]);
  178. }
  179. }
  180. void vp9_loop_filter_frame_mt(YV12_BUFFER_CONFIG *frame, VP9_COMMON *cm,
  181. struct macroblockd_plane planes[MAX_MB_PLANE],
  182. int frame_filter_level, int y_only,
  183. int partial_frame, VPxWorker *workers,
  184. int num_workers, VP9LfSync *lf_sync) {
  185. int start_mi_row, end_mi_row, mi_rows_to_filter;
  186. if (!frame_filter_level) return;
  187. start_mi_row = 0;
  188. mi_rows_to_filter = cm->mi_rows;
  189. if (partial_frame && cm->mi_rows > 8) {
  190. start_mi_row = cm->mi_rows >> 1;
  191. start_mi_row &= 0xfffffff8;
  192. mi_rows_to_filter = VPXMAX(cm->mi_rows / 8, 8);
  193. }
  194. end_mi_row = start_mi_row + mi_rows_to_filter;
  195. vp9_loop_filter_frame_init(cm, frame_filter_level);
  196. loop_filter_rows_mt(frame, cm, planes, start_mi_row, end_mi_row, y_only,
  197. workers, num_workers, lf_sync);
  198. }
  199. // Set up nsync by width.
  200. static INLINE int get_sync_range(int width) {
  201. // nsync numbers are picked by testing. For example, for 4k
  202. // video, using 4 gives best performance.
  203. if (width < 640)
  204. return 1;
  205. else if (width <= 1280)
  206. return 2;
  207. else if (width <= 4096)
  208. return 4;
  209. else
  210. return 8;
  211. }
  212. // Allocate memory for lf row synchronization
  213. void vp9_loop_filter_alloc(VP9LfSync *lf_sync, VP9_COMMON *cm, int rows,
  214. int width, int num_workers) {
  215. lf_sync->rows = rows;
  216. #if CONFIG_MULTITHREAD
  217. {
  218. int i;
  219. CHECK_MEM_ERROR(cm, lf_sync->mutex_,
  220. vpx_malloc(sizeof(*lf_sync->mutex_) * rows));
  221. if (lf_sync->mutex_) {
  222. for (i = 0; i < rows; ++i) {
  223. pthread_mutex_init(&lf_sync->mutex_[i], NULL);
  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. CHECK_MEM_ERROR(cm, lf_sync->lfdata,
  236. vpx_malloc(num_workers * sizeof(*lf_sync->lfdata)));
  237. lf_sync->num_workers = num_workers;
  238. CHECK_MEM_ERROR(cm, lf_sync->cur_sb_col,
  239. vpx_malloc(sizeof(*lf_sync->cur_sb_col) * rows));
  240. // Set up nsync.
  241. lf_sync->sync_range = get_sync_range(width);
  242. }
  243. // Deallocate lf synchronization related mutex and data
  244. void vp9_loop_filter_dealloc(VP9LfSync *lf_sync) {
  245. if (lf_sync != NULL) {
  246. #if CONFIG_MULTITHREAD
  247. int i;
  248. if (lf_sync->mutex_ != NULL) {
  249. for (i = 0; i < lf_sync->rows; ++i) {
  250. pthread_mutex_destroy(&lf_sync->mutex_[i]);
  251. }
  252. vpx_free(lf_sync->mutex_);
  253. }
  254. if (lf_sync->cond_ != NULL) {
  255. for (i = 0; i < lf_sync->rows; ++i) {
  256. pthread_cond_destroy(&lf_sync->cond_[i]);
  257. }
  258. vpx_free(lf_sync->cond_);
  259. }
  260. #endif // CONFIG_MULTITHREAD
  261. vpx_free(lf_sync->lfdata);
  262. vpx_free(lf_sync->cur_sb_col);
  263. // clear the structure as the source of this call may be a resize in which
  264. // case this call will be followed by an _alloc() which may fail.
  265. vp9_zero(*lf_sync);
  266. }
  267. }
  268. // Accumulate frame counts.
  269. void vp9_accumulate_frame_counts(FRAME_COUNTS *accum,
  270. const FRAME_COUNTS *counts, int is_dec) {
  271. int i, j, k, l, m;
  272. for (i = 0; i < BLOCK_SIZE_GROUPS; i++)
  273. for (j = 0; j < INTRA_MODES; j++)
  274. accum->y_mode[i][j] += counts->y_mode[i][j];
  275. for (i = 0; i < INTRA_MODES; i++)
  276. for (j = 0; j < INTRA_MODES; j++)
  277. accum->uv_mode[i][j] += counts->uv_mode[i][j];
  278. for (i = 0; i < PARTITION_CONTEXTS; i++)
  279. for (j = 0; j < PARTITION_TYPES; j++)
  280. accum->partition[i][j] += counts->partition[i][j];
  281. if (is_dec) {
  282. int n;
  283. for (i = 0; i < TX_SIZES; i++)
  284. for (j = 0; j < PLANE_TYPES; j++)
  285. for (k = 0; k < REF_TYPES; k++)
  286. for (l = 0; l < COEF_BANDS; l++)
  287. for (m = 0; m < COEFF_CONTEXTS; m++) {
  288. accum->eob_branch[i][j][k][l][m] +=
  289. counts->eob_branch[i][j][k][l][m];
  290. for (n = 0; n < UNCONSTRAINED_NODES + 1; n++)
  291. accum->coef[i][j][k][l][m][n] += counts->coef[i][j][k][l][m][n];
  292. }
  293. } else {
  294. for (i = 0; i < TX_SIZES; i++)
  295. for (j = 0; j < PLANE_TYPES; j++)
  296. for (k = 0; k < REF_TYPES; k++)
  297. for (l = 0; l < COEF_BANDS; l++)
  298. for (m = 0; m < COEFF_CONTEXTS; m++)
  299. accum->eob_branch[i][j][k][l][m] +=
  300. counts->eob_branch[i][j][k][l][m];
  301. // In the encoder, coef is only updated at frame
  302. // level, so not need to accumulate it here.
  303. // for (n = 0; n < UNCONSTRAINED_NODES + 1; n++)
  304. // accum->coef[i][j][k][l][m][n] +=
  305. // counts->coef[i][j][k][l][m][n];
  306. }
  307. for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
  308. for (j = 0; j < SWITCHABLE_FILTERS; j++)
  309. accum->switchable_interp[i][j] += counts->switchable_interp[i][j];
  310. for (i = 0; i < INTER_MODE_CONTEXTS; i++)
  311. for (j = 0; j < INTER_MODES; j++)
  312. accum->inter_mode[i][j] += counts->inter_mode[i][j];
  313. for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
  314. for (j = 0; j < 2; j++)
  315. accum->intra_inter[i][j] += counts->intra_inter[i][j];
  316. for (i = 0; i < COMP_INTER_CONTEXTS; i++)
  317. for (j = 0; j < 2; j++) accum->comp_inter[i][j] += counts->comp_inter[i][j];
  318. for (i = 0; i < REF_CONTEXTS; i++)
  319. for (j = 0; j < 2; j++)
  320. for (k = 0; k < 2; k++)
  321. accum->single_ref[i][j][k] += counts->single_ref[i][j][k];
  322. for (i = 0; i < REF_CONTEXTS; i++)
  323. for (j = 0; j < 2; j++) accum->comp_ref[i][j] += counts->comp_ref[i][j];
  324. for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
  325. for (j = 0; j < TX_SIZES; j++)
  326. accum->tx.p32x32[i][j] += counts->tx.p32x32[i][j];
  327. for (j = 0; j < TX_SIZES - 1; j++)
  328. accum->tx.p16x16[i][j] += counts->tx.p16x16[i][j];
  329. for (j = 0; j < TX_SIZES - 2; j++)
  330. accum->tx.p8x8[i][j] += counts->tx.p8x8[i][j];
  331. }
  332. for (i = 0; i < TX_SIZES; i++)
  333. accum->tx.tx_totals[i] += counts->tx.tx_totals[i];
  334. for (i = 0; i < SKIP_CONTEXTS; i++)
  335. for (j = 0; j < 2; j++) accum->skip[i][j] += counts->skip[i][j];
  336. for (i = 0; i < MV_JOINTS; i++) accum->mv.joints[i] += counts->mv.joints[i];
  337. for (k = 0; k < 2; k++) {
  338. nmv_component_counts *const comps = &accum->mv.comps[k];
  339. const nmv_component_counts *const comps_t = &counts->mv.comps[k];
  340. for (i = 0; i < 2; i++) {
  341. comps->sign[i] += comps_t->sign[i];
  342. comps->class0_hp[i] += comps_t->class0_hp[i];
  343. comps->hp[i] += comps_t->hp[i];
  344. }
  345. for (i = 0; i < MV_CLASSES; i++) comps->classes[i] += comps_t->classes[i];
  346. for (i = 0; i < CLASS0_SIZE; i++) {
  347. comps->class0[i] += comps_t->class0[i];
  348. for (j = 0; j < MV_FP_SIZE; j++)
  349. comps->class0_fp[i][j] += comps_t->class0_fp[i][j];
  350. }
  351. for (i = 0; i < MV_OFFSET_BITS; i++)
  352. for (j = 0; j < 2; j++) comps->bits[i][j] += comps_t->bits[i][j];
  353. for (i = 0; i < MV_FP_SIZE; i++) comps->fp[i] += comps_t->fp[i];
  354. }
  355. }