vp9_thread_common.c 14 KB

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