2
0

ethreading.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * Copyright (c) 2010 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 "onyx_int.h"
  11. #include "vp8/common/threading.h"
  12. #include "vp8/common/common.h"
  13. #include "vp8/common/extend.h"
  14. #include "bitstream.h"
  15. #include "encodeframe.h"
  16. #include "ethreading.h"
  17. #if CONFIG_MULTITHREAD
  18. extern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x,
  19. int ok_to_skip);
  20. static THREAD_FUNCTION thread_loopfilter(void *p_data) {
  21. VP8_COMP *cpi = (VP8_COMP *)(((LPFTHREAD_DATA *)p_data)->ptr1);
  22. VP8_COMMON *cm = &cpi->common;
  23. while (1) {
  24. if (vpx_atomic_load_acquire(&cpi->b_multi_threaded) == 0) break;
  25. if (sem_wait(&cpi->h_event_start_lpf) == 0) {
  26. /* we're shutting down */
  27. if (vpx_atomic_load_acquire(&cpi->b_multi_threaded) == 0) break;
  28. vp8_loopfilter_frame(cpi, cm);
  29. sem_post(&cpi->h_event_end_lpf);
  30. }
  31. }
  32. return 0;
  33. }
  34. static THREAD_FUNCTION thread_encoding_proc(void *p_data) {
  35. int ithread = ((ENCODETHREAD_DATA *)p_data)->ithread;
  36. VP8_COMP *cpi = (VP8_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr1);
  37. MB_ROW_COMP *mbri = (MB_ROW_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr2);
  38. ENTROPY_CONTEXT_PLANES mb_row_left_context;
  39. while (1) {
  40. if (vpx_atomic_load_acquire(&cpi->b_multi_threaded) == 0) break;
  41. if (sem_wait(&cpi->h_event_start_encoding[ithread]) == 0) {
  42. const int nsync = cpi->mt_sync_range;
  43. VP8_COMMON *cm = &cpi->common;
  44. int mb_row;
  45. MACROBLOCK *x = &mbri->mb;
  46. MACROBLOCKD *xd = &x->e_mbd;
  47. TOKENEXTRA *tp;
  48. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  49. TOKENEXTRA *tp_start = cpi->tok + (1 + ithread) * (16 * 24);
  50. const int num_part = (1 << cm->multi_token_partition);
  51. #endif
  52. int *segment_counts = mbri->segment_counts;
  53. int *totalrate = &mbri->totalrate;
  54. /* we're shutting down */
  55. if (vpx_atomic_load_acquire(&cpi->b_multi_threaded) == 0) break;
  56. xd->mode_info_context = cm->mi + cm->mode_info_stride * (ithread + 1);
  57. xd->mode_info_stride = cm->mode_info_stride;
  58. for (mb_row = ithread + 1; mb_row < cm->mb_rows;
  59. mb_row += (cpi->encoding_thread_count + 1)) {
  60. int recon_yoffset, recon_uvoffset;
  61. int mb_col;
  62. int ref_fb_idx = cm->lst_fb_idx;
  63. int dst_fb_idx = cm->new_fb_idx;
  64. int recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride;
  65. int recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride;
  66. int map_index = (mb_row * cm->mb_cols);
  67. const vpx_atomic_int *last_row_current_mb_col;
  68. vpx_atomic_int *current_mb_col = &cpi->mt_current_mb_col[mb_row];
  69. #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
  70. vp8_writer *w = &cpi->bc[1 + (mb_row % num_part)];
  71. #else
  72. tp = cpi->tok + (mb_row * (cm->mb_cols * 16 * 24));
  73. cpi->tplist[mb_row].start = tp;
  74. #endif
  75. last_row_current_mb_col = &cpi->mt_current_mb_col[mb_row - 1];
  76. /* reset above block coeffs */
  77. xd->above_context = cm->above_context;
  78. xd->left_context = &mb_row_left_context;
  79. vp8_zero(mb_row_left_context);
  80. xd->up_available = (mb_row != 0);
  81. recon_yoffset = (mb_row * recon_y_stride * 16);
  82. recon_uvoffset = (mb_row * recon_uv_stride * 8);
  83. /* Set the mb activity pointer to the start of the row. */
  84. x->mb_activity_ptr = &cpi->mb_activity_map[map_index];
  85. /* for each macroblock col in image */
  86. for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
  87. if (((mb_col - 1) % nsync) == 0) {
  88. vpx_atomic_store_release(current_mb_col, mb_col - 1);
  89. }
  90. if (mb_row && !(mb_col & (nsync - 1))) {
  91. vp8_atomic_spin_wait(mb_col, last_row_current_mb_col, nsync);
  92. }
  93. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  94. tp = tp_start;
  95. #endif
  96. /* Distance of Mb to the various image edges.
  97. * These specified to 8th pel as they are always compared
  98. * to values that are in 1/8th pel units
  99. */
  100. xd->mb_to_left_edge = -((mb_col * 16) << 3);
  101. xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3;
  102. xd->mb_to_top_edge = -((mb_row * 16) << 3);
  103. xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3;
  104. /* Set up limit values for motion vectors used to prevent
  105. * them extending outside the UMV borders
  106. */
  107. x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
  108. x->mv_col_max =
  109. ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
  110. x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
  111. x->mv_row_max =
  112. ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
  113. xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
  114. xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
  115. xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
  116. xd->left_available = (mb_col != 0);
  117. x->rddiv = cpi->RDDIV;
  118. x->rdmult = cpi->RDMULT;
  119. /* Copy current mb to a buffer */
  120. vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
  121. if (cpi->oxcf.tuning == VP8_TUNE_SSIM) vp8_activity_masking(cpi, x);
  122. /* Is segmentation enabled */
  123. /* MB level adjustment to quantizer */
  124. if (xd->segmentation_enabled) {
  125. /* Code to set segment id in xd->mbmi.segment_id for
  126. * current MB (with range checking)
  127. */
  128. if (cpi->segmentation_map[map_index + mb_col] <= 3) {
  129. xd->mode_info_context->mbmi.segment_id =
  130. cpi->segmentation_map[map_index + mb_col];
  131. } else {
  132. xd->mode_info_context->mbmi.segment_id = 0;
  133. }
  134. vp8cx_mb_init_quantizer(cpi, x, 1);
  135. } else {
  136. /* Set to Segment 0 by default */
  137. xd->mode_info_context->mbmi.segment_id = 0;
  138. }
  139. x->active_ptr = cpi->active_map + map_index + mb_col;
  140. if (cm->frame_type == KEY_FRAME) {
  141. *totalrate += vp8cx_encode_intra_macroblock(cpi, x, &tp);
  142. #ifdef MODE_STATS
  143. y_modes[xd->mbmi.mode]++;
  144. #endif
  145. } else {
  146. *totalrate += vp8cx_encode_inter_macroblock(
  147. cpi, x, &tp, recon_yoffset, recon_uvoffset, mb_row, mb_col);
  148. #ifdef MODE_STATS
  149. inter_y_modes[xd->mbmi.mode]++;
  150. if (xd->mbmi.mode == SPLITMV) {
  151. int b;
  152. for (b = 0; b < xd->mbmi.partition_count; ++b) {
  153. inter_b_modes[x->partition->bmi[b].mode]++;
  154. }
  155. }
  156. #endif
  157. // Keep track of how many (consecutive) times a block
  158. // is coded as ZEROMV_LASTREF, for base layer frames.
  159. // Reset to 0 if its coded as anything else.
  160. if (cpi->current_layer == 0) {
  161. if (xd->mode_info_context->mbmi.mode == ZEROMV &&
  162. xd->mode_info_context->mbmi.ref_frame == LAST_FRAME) {
  163. // Increment, check for wrap-around.
  164. if (cpi->consec_zero_last[map_index + mb_col] < 255) {
  165. cpi->consec_zero_last[map_index + mb_col] += 1;
  166. }
  167. if (cpi->consec_zero_last_mvbias[map_index + mb_col] < 255) {
  168. cpi->consec_zero_last_mvbias[map_index + mb_col] += 1;
  169. }
  170. } else {
  171. cpi->consec_zero_last[map_index + mb_col] = 0;
  172. cpi->consec_zero_last_mvbias[map_index + mb_col] = 0;
  173. }
  174. if (x->zero_last_dot_suppress) {
  175. cpi->consec_zero_last_mvbias[map_index + mb_col] = 0;
  176. }
  177. }
  178. /* Special case code for cyclic refresh
  179. * If cyclic update enabled then copy
  180. * xd->mbmi.segment_id; (which may have been updated
  181. * based on mode during
  182. * vp8cx_encode_inter_macroblock()) back into the
  183. * global segmentation map
  184. */
  185. if ((cpi->current_layer == 0) &&
  186. (cpi->cyclic_refresh_mode_enabled &&
  187. xd->segmentation_enabled)) {
  188. const MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
  189. cpi->segmentation_map[map_index + mb_col] = mbmi->segment_id;
  190. /* If the block has been refreshed mark it as clean
  191. * (the magnitude of the -ve influences how long it
  192. * will be before we consider another refresh):
  193. * Else if it was coded (last frame 0,0) and has
  194. * not already been refreshed then mark it as a
  195. * candidate for cleanup next time (marked 0) else
  196. * mark it as dirty (1).
  197. */
  198. if (mbmi->segment_id) {
  199. cpi->cyclic_refresh_map[map_index + mb_col] = -1;
  200. } else if ((mbmi->mode == ZEROMV) &&
  201. (mbmi->ref_frame == LAST_FRAME)) {
  202. if (cpi->cyclic_refresh_map[map_index + mb_col] == 1) {
  203. cpi->cyclic_refresh_map[map_index + mb_col] = 0;
  204. }
  205. } else {
  206. cpi->cyclic_refresh_map[map_index + mb_col] = 1;
  207. }
  208. }
  209. }
  210. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  211. /* pack tokens for this MB */
  212. {
  213. int tok_count = tp - tp_start;
  214. vp8_pack_tokens(w, tp_start, tok_count);
  215. }
  216. #else
  217. cpi->tplist[mb_row].stop = tp;
  218. #endif
  219. /* Increment pointer into gf usage flags structure. */
  220. x->gf_active_ptr++;
  221. /* Increment the activity mask pointers. */
  222. x->mb_activity_ptr++;
  223. /* adjust to the next column of macroblocks */
  224. x->src.y_buffer += 16;
  225. x->src.u_buffer += 8;
  226. x->src.v_buffer += 8;
  227. recon_yoffset += 16;
  228. recon_uvoffset += 8;
  229. /* Keep track of segment usage */
  230. segment_counts[xd->mode_info_context->mbmi.segment_id]++;
  231. /* skip to next mb */
  232. xd->mode_info_context++;
  233. x->partition_info++;
  234. xd->above_context++;
  235. }
  236. vp8_extend_mb_row(&cm->yv12_fb[dst_fb_idx], xd->dst.y_buffer + 16,
  237. xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
  238. vpx_atomic_store_release(current_mb_col, mb_col + nsync);
  239. /* this is to account for the border */
  240. xd->mode_info_context++;
  241. x->partition_info++;
  242. x->src.y_buffer +=
  243. 16 * x->src.y_stride * (cpi->encoding_thread_count + 1) -
  244. 16 * cm->mb_cols;
  245. x->src.u_buffer +=
  246. 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) -
  247. 8 * cm->mb_cols;
  248. x->src.v_buffer +=
  249. 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) -
  250. 8 * cm->mb_cols;
  251. xd->mode_info_context +=
  252. xd->mode_info_stride * cpi->encoding_thread_count;
  253. x->partition_info += xd->mode_info_stride * cpi->encoding_thread_count;
  254. x->gf_active_ptr += cm->mb_cols * cpi->encoding_thread_count;
  255. }
  256. /* Signal that this thread has completed processing its rows. */
  257. sem_post(&cpi->h_event_end_encoding[ithread]);
  258. }
  259. }
  260. /* printf("exit thread %d\n", ithread); */
  261. return 0;
  262. }
  263. static void setup_mbby_copy(MACROBLOCK *mbdst, MACROBLOCK *mbsrc) {
  264. MACROBLOCK *x = mbsrc;
  265. MACROBLOCK *z = mbdst;
  266. int i;
  267. z->ss = x->ss;
  268. z->ss_count = x->ss_count;
  269. z->searches_per_step = x->searches_per_step;
  270. z->errorperbit = x->errorperbit;
  271. z->sadperbit16 = x->sadperbit16;
  272. z->sadperbit4 = x->sadperbit4;
  273. /*
  274. z->mv_col_min = x->mv_col_min;
  275. z->mv_col_max = x->mv_col_max;
  276. z->mv_row_min = x->mv_row_min;
  277. z->mv_row_max = x->mv_row_max;
  278. */
  279. z->short_fdct4x4 = x->short_fdct4x4;
  280. z->short_fdct8x4 = x->short_fdct8x4;
  281. z->short_walsh4x4 = x->short_walsh4x4;
  282. z->quantize_b = x->quantize_b;
  283. z->optimize = x->optimize;
  284. /*
  285. z->mvc = x->mvc;
  286. z->src.y_buffer = x->src.y_buffer;
  287. z->src.u_buffer = x->src.u_buffer;
  288. z->src.v_buffer = x->src.v_buffer;
  289. */
  290. z->mvcost[0] = x->mvcost[0];
  291. z->mvcost[1] = x->mvcost[1];
  292. z->mvsadcost[0] = x->mvsadcost[0];
  293. z->mvsadcost[1] = x->mvsadcost[1];
  294. z->token_costs = x->token_costs;
  295. z->inter_bmode_costs = x->inter_bmode_costs;
  296. z->mbmode_cost = x->mbmode_cost;
  297. z->intra_uv_mode_cost = x->intra_uv_mode_cost;
  298. z->bmode_costs = x->bmode_costs;
  299. for (i = 0; i < 25; ++i) {
  300. z->block[i].quant = x->block[i].quant;
  301. z->block[i].quant_fast = x->block[i].quant_fast;
  302. z->block[i].quant_shift = x->block[i].quant_shift;
  303. z->block[i].zbin = x->block[i].zbin;
  304. z->block[i].zrun_zbin_boost = x->block[i].zrun_zbin_boost;
  305. z->block[i].round = x->block[i].round;
  306. z->block[i].src_stride = x->block[i].src_stride;
  307. }
  308. z->q_index = x->q_index;
  309. z->act_zbin_adj = x->act_zbin_adj;
  310. z->last_act_zbin_adj = x->last_act_zbin_adj;
  311. {
  312. MACROBLOCKD *xd = &x->e_mbd;
  313. MACROBLOCKD *zd = &z->e_mbd;
  314. /*
  315. zd->mode_info_context = xd->mode_info_context;
  316. zd->mode_info = xd->mode_info;
  317. zd->mode_info_stride = xd->mode_info_stride;
  318. zd->frame_type = xd->frame_type;
  319. zd->up_available = xd->up_available ;
  320. zd->left_available = xd->left_available;
  321. zd->left_context = xd->left_context;
  322. zd->last_frame_dc = xd->last_frame_dc;
  323. zd->last_frame_dccons = xd->last_frame_dccons;
  324. zd->gold_frame_dc = xd->gold_frame_dc;
  325. zd->gold_frame_dccons = xd->gold_frame_dccons;
  326. zd->mb_to_left_edge = xd->mb_to_left_edge;
  327. zd->mb_to_right_edge = xd->mb_to_right_edge;
  328. zd->mb_to_top_edge = xd->mb_to_top_edge ;
  329. zd->mb_to_bottom_edge = xd->mb_to_bottom_edge;
  330. zd->gf_active_ptr = xd->gf_active_ptr;
  331. zd->frames_since_golden = xd->frames_since_golden;
  332. zd->frames_till_alt_ref_frame = xd->frames_till_alt_ref_frame;
  333. */
  334. zd->subpixel_predict = xd->subpixel_predict;
  335. zd->subpixel_predict8x4 = xd->subpixel_predict8x4;
  336. zd->subpixel_predict8x8 = xd->subpixel_predict8x8;
  337. zd->subpixel_predict16x16 = xd->subpixel_predict16x16;
  338. zd->segmentation_enabled = xd->segmentation_enabled;
  339. zd->mb_segement_abs_delta = xd->mb_segement_abs_delta;
  340. memcpy(zd->segment_feature_data, xd->segment_feature_data,
  341. sizeof(xd->segment_feature_data));
  342. memcpy(zd->dequant_y1_dc, xd->dequant_y1_dc, sizeof(xd->dequant_y1_dc));
  343. memcpy(zd->dequant_y1, xd->dequant_y1, sizeof(xd->dequant_y1));
  344. memcpy(zd->dequant_y2, xd->dequant_y2, sizeof(xd->dequant_y2));
  345. memcpy(zd->dequant_uv, xd->dequant_uv, sizeof(xd->dequant_uv));
  346. #if 1
  347. /*TODO: Remove dequant from BLOCKD. This is a temporary solution until
  348. * the quantizer code uses a passed in pointer to the dequant constants.
  349. * This will also require modifications to the x86 and neon assembly.
  350. * */
  351. for (i = 0; i < 16; ++i) zd->block[i].dequant = zd->dequant_y1;
  352. for (i = 16; i < 24; ++i) zd->block[i].dequant = zd->dequant_uv;
  353. zd->block[24].dequant = zd->dequant_y2;
  354. #endif
  355. memcpy(z->rd_threshes, x->rd_threshes, sizeof(x->rd_threshes));
  356. memcpy(z->rd_thresh_mult, x->rd_thresh_mult, sizeof(x->rd_thresh_mult));
  357. z->zbin_over_quant = x->zbin_over_quant;
  358. z->zbin_mode_boost_enabled = x->zbin_mode_boost_enabled;
  359. z->zbin_mode_boost = x->zbin_mode_boost;
  360. memset(z->error_bins, 0, sizeof(z->error_bins));
  361. }
  362. }
  363. void vp8cx_init_mbrthread_data(VP8_COMP *cpi, MACROBLOCK *x,
  364. MB_ROW_COMP *mbr_ei, int count) {
  365. VP8_COMMON *const cm = &cpi->common;
  366. MACROBLOCKD *const xd = &x->e_mbd;
  367. int i;
  368. for (i = 0; i < count; ++i) {
  369. MACROBLOCK *mb = &mbr_ei[i].mb;
  370. MACROBLOCKD *mbd = &mb->e_mbd;
  371. mbd->subpixel_predict = xd->subpixel_predict;
  372. mbd->subpixel_predict8x4 = xd->subpixel_predict8x4;
  373. mbd->subpixel_predict8x8 = xd->subpixel_predict8x8;
  374. mbd->subpixel_predict16x16 = xd->subpixel_predict16x16;
  375. mb->gf_active_ptr = x->gf_active_ptr;
  376. memset(mbr_ei[i].segment_counts, 0, sizeof(mbr_ei[i].segment_counts));
  377. mbr_ei[i].totalrate = 0;
  378. mb->partition_info = x->pi + x->e_mbd.mode_info_stride * (i + 1);
  379. mbd->frame_type = cm->frame_type;
  380. mb->src = *cpi->Source;
  381. mbd->pre = cm->yv12_fb[cm->lst_fb_idx];
  382. mbd->dst = cm->yv12_fb[cm->new_fb_idx];
  383. mb->src.y_buffer += 16 * x->src.y_stride * (i + 1);
  384. mb->src.u_buffer += 8 * x->src.uv_stride * (i + 1);
  385. mb->src.v_buffer += 8 * x->src.uv_stride * (i + 1);
  386. vp8_build_block_offsets(mb);
  387. mbd->left_context = &cm->left_context;
  388. mb->mvc = cm->fc.mvc;
  389. setup_mbby_copy(&mbr_ei[i].mb, x);
  390. mbd->fullpixel_mask = 0xffffffff;
  391. if (cm->full_pixel) mbd->fullpixel_mask = 0xfffffff8;
  392. vp8_zero(mb->coef_counts);
  393. vp8_zero(x->ymode_count);
  394. mb->skip_true_count = 0;
  395. vp8_zero(mb->MVcount);
  396. mb->prediction_error = 0;
  397. mb->intra_error = 0;
  398. vp8_zero(mb->count_mb_ref_frame_usage);
  399. mb->mbs_tested_so_far = 0;
  400. mb->mbs_zero_last_dot_suppress = 0;
  401. }
  402. }
  403. int vp8cx_create_encoder_threads(VP8_COMP *cpi) {
  404. const VP8_COMMON *cm = &cpi->common;
  405. vpx_atomic_init(&cpi->b_multi_threaded, 0);
  406. cpi->encoding_thread_count = 0;
  407. cpi->b_lpf_running = 0;
  408. if (cm->processor_core_count > 1 && cpi->oxcf.multi_threaded > 1) {
  409. int ithread;
  410. int th_count = cpi->oxcf.multi_threaded - 1;
  411. int rc = 0;
  412. /* don't allocate more threads than cores available */
  413. if (cpi->oxcf.multi_threaded > cm->processor_core_count) {
  414. th_count = cm->processor_core_count - 1;
  415. }
  416. /* we have th_count + 1 (main) threads processing one row each */
  417. /* no point to have more threads than the sync range allows */
  418. if (th_count > ((cm->mb_cols / cpi->mt_sync_range) - 1)) {
  419. th_count = (cm->mb_cols / cpi->mt_sync_range) - 1;
  420. }
  421. if (th_count == 0) return 0;
  422. CHECK_MEM_ERROR(cpi->h_encoding_thread,
  423. vpx_malloc(sizeof(pthread_t) * th_count));
  424. CHECK_MEM_ERROR(cpi->h_event_start_encoding,
  425. vpx_malloc(sizeof(sem_t) * th_count));
  426. CHECK_MEM_ERROR(cpi->h_event_end_encoding,
  427. vpx_malloc(sizeof(sem_t) * th_count));
  428. CHECK_MEM_ERROR(cpi->mb_row_ei,
  429. vpx_memalign(32, sizeof(MB_ROW_COMP) * th_count));
  430. memset(cpi->mb_row_ei, 0, sizeof(MB_ROW_COMP) * th_count);
  431. CHECK_MEM_ERROR(cpi->en_thread_data,
  432. vpx_malloc(sizeof(ENCODETHREAD_DATA) * th_count));
  433. vpx_atomic_store_release(&cpi->b_multi_threaded, 1);
  434. cpi->encoding_thread_count = th_count;
  435. /*
  436. printf("[VP8:] multi_threaded encoding is enabled with %d threads\n\n",
  437. (cpi->encoding_thread_count +1));
  438. */
  439. for (ithread = 0; ithread < th_count; ++ithread) {
  440. ENCODETHREAD_DATA *ethd = &cpi->en_thread_data[ithread];
  441. /* Setup block ptrs and offsets */
  442. vp8_setup_block_ptrs(&cpi->mb_row_ei[ithread].mb);
  443. vp8_setup_block_dptrs(&cpi->mb_row_ei[ithread].mb.e_mbd);
  444. sem_init(&cpi->h_event_start_encoding[ithread], 0, 0);
  445. sem_init(&cpi->h_event_end_encoding[ithread], 0, 0);
  446. ethd->ithread = ithread;
  447. ethd->ptr1 = (void *)cpi;
  448. ethd->ptr2 = (void *)&cpi->mb_row_ei[ithread];
  449. rc = pthread_create(&cpi->h_encoding_thread[ithread], 0,
  450. thread_encoding_proc, ethd);
  451. if (rc) break;
  452. }
  453. if (rc) {
  454. /* shutdown other threads */
  455. vpx_atomic_store_release(&cpi->b_multi_threaded, 0);
  456. for (--ithread; ithread >= 0; ithread--) {
  457. pthread_join(cpi->h_encoding_thread[ithread], 0);
  458. sem_destroy(&cpi->h_event_start_encoding[ithread]);
  459. sem_destroy(&cpi->h_event_end_encoding[ithread]);
  460. }
  461. /* free thread related resources */
  462. vpx_free(cpi->h_event_start_encoding);
  463. vpx_free(cpi->h_event_end_encoding);
  464. vpx_free(cpi->h_encoding_thread);
  465. vpx_free(cpi->mb_row_ei);
  466. vpx_free(cpi->en_thread_data);
  467. return -1;
  468. }
  469. {
  470. LPFTHREAD_DATA *lpfthd = &cpi->lpf_thread_data;
  471. sem_init(&cpi->h_event_start_lpf, 0, 0);
  472. sem_init(&cpi->h_event_end_lpf, 0, 0);
  473. lpfthd->ptr1 = (void *)cpi;
  474. rc = pthread_create(&cpi->h_filter_thread, 0, thread_loopfilter, lpfthd);
  475. if (rc) {
  476. /* shutdown other threads */
  477. vpx_atomic_store_release(&cpi->b_multi_threaded, 0);
  478. for (--ithread; ithread >= 0; ithread--) {
  479. sem_post(&cpi->h_event_start_encoding[ithread]);
  480. sem_post(&cpi->h_event_end_encoding[ithread]);
  481. pthread_join(cpi->h_encoding_thread[ithread], 0);
  482. sem_destroy(&cpi->h_event_start_encoding[ithread]);
  483. sem_destroy(&cpi->h_event_end_encoding[ithread]);
  484. }
  485. sem_destroy(&cpi->h_event_end_lpf);
  486. sem_destroy(&cpi->h_event_start_lpf);
  487. /* free thread related resources */
  488. vpx_free(cpi->h_event_start_encoding);
  489. vpx_free(cpi->h_event_end_encoding);
  490. vpx_free(cpi->h_encoding_thread);
  491. vpx_free(cpi->mb_row_ei);
  492. vpx_free(cpi->en_thread_data);
  493. return -2;
  494. }
  495. }
  496. }
  497. return 0;
  498. }
  499. void vp8cx_remove_encoder_threads(VP8_COMP *cpi) {
  500. if (vpx_atomic_load_acquire(&cpi->b_multi_threaded)) {
  501. /* shutdown other threads */
  502. vpx_atomic_store_release(&cpi->b_multi_threaded, 0);
  503. {
  504. int i;
  505. for (i = 0; i < cpi->encoding_thread_count; ++i) {
  506. sem_post(&cpi->h_event_start_encoding[i]);
  507. sem_post(&cpi->h_event_end_encoding[i]);
  508. pthread_join(cpi->h_encoding_thread[i], 0);
  509. sem_destroy(&cpi->h_event_start_encoding[i]);
  510. sem_destroy(&cpi->h_event_end_encoding[i]);
  511. }
  512. sem_post(&cpi->h_event_start_lpf);
  513. pthread_join(cpi->h_filter_thread, 0);
  514. }
  515. sem_destroy(&cpi->h_event_end_lpf);
  516. sem_destroy(&cpi->h_event_start_lpf);
  517. /* free thread related resources */
  518. vpx_free(cpi->h_event_start_encoding);
  519. vpx_free(cpi->h_event_end_encoding);
  520. vpx_free(cpi->h_encoding_thread);
  521. vpx_free(cpi->mb_row_ei);
  522. vpx_free(cpi->en_thread_data);
  523. }
  524. }
  525. #endif