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. #if CONFIG_MULTITHREAD
  17. extern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x,
  18. int ok_to_skip);
  19. static THREAD_FUNCTION thread_loopfilter(void *p_data) {
  20. VP8_COMP *cpi = (VP8_COMP *)(((LPFTHREAD_DATA *)p_data)->ptr1);
  21. VP8_COMMON *cm = &cpi->common;
  22. while (1) {
  23. if (cpi->b_multi_threaded == 0) break;
  24. if (sem_wait(&cpi->h_event_start_lpf) == 0) {
  25. /* we're shutting down */
  26. if (cpi->b_multi_threaded == 0) break;
  27. vp8_loopfilter_frame(cpi, cm);
  28. sem_post(&cpi->h_event_end_lpf);
  29. }
  30. }
  31. return 0;
  32. }
  33. static THREAD_FUNCTION thread_encoding_proc(void *p_data) {
  34. int ithread = ((ENCODETHREAD_DATA *)p_data)->ithread;
  35. VP8_COMP *cpi = (VP8_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr1);
  36. MB_ROW_COMP *mbri = (MB_ROW_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr2);
  37. ENTROPY_CONTEXT_PLANES mb_row_left_context;
  38. while (1) {
  39. if (cpi->b_multi_threaded == 0) break;
  40. if (sem_wait(&cpi->h_event_start_encoding[ithread]) == 0) {
  41. const int nsync = cpi->mt_sync_range;
  42. VP8_COMMON *cm = &cpi->common;
  43. int mb_row;
  44. MACROBLOCK *x = &mbri->mb;
  45. MACROBLOCKD *xd = &x->e_mbd;
  46. TOKENEXTRA *tp;
  47. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  48. TOKENEXTRA *tp_start = cpi->tok + (1 + ithread) * (16 * 24);
  49. const int num_part = (1 << cm->multi_token_partition);
  50. #endif
  51. int *segment_counts = mbri->segment_counts;
  52. int *totalrate = &mbri->totalrate;
  53. /* we're shutting down */
  54. if (cpi->b_multi_threaded == 0) break;
  55. for (mb_row = ithread + 1; mb_row < cm->mb_rows;
  56. mb_row += (cpi->encoding_thread_count + 1)) {
  57. int recon_yoffset, recon_uvoffset;
  58. int mb_col;
  59. int ref_fb_idx = cm->lst_fb_idx;
  60. int dst_fb_idx = cm->new_fb_idx;
  61. int recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride;
  62. int recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride;
  63. int map_index = (mb_row * cm->mb_cols);
  64. volatile const int *last_row_current_mb_col;
  65. volatile int *current_mb_col = &cpi->mt_current_mb_col[mb_row];
  66. #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
  67. vp8_writer *w = &cpi->bc[1 + (mb_row % num_part)];
  68. #else
  69. tp = cpi->tok + (mb_row * (cm->mb_cols * 16 * 24));
  70. cpi->tplist[mb_row].start = tp;
  71. #endif
  72. last_row_current_mb_col = &cpi->mt_current_mb_col[mb_row - 1];
  73. /* reset above block coeffs */
  74. xd->above_context = cm->above_context;
  75. xd->left_context = &mb_row_left_context;
  76. vp8_zero(mb_row_left_context);
  77. xd->up_available = (mb_row != 0);
  78. recon_yoffset = (mb_row * recon_y_stride * 16);
  79. recon_uvoffset = (mb_row * recon_uv_stride * 8);
  80. /* Set the mb activity pointer to the start of the row. */
  81. x->mb_activity_ptr = &cpi->mb_activity_map[map_index];
  82. /* for each macroblock col in image */
  83. for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
  84. *current_mb_col = mb_col - 1;
  85. if ((mb_col & (nsync - 1)) == 0) {
  86. while (mb_col > (*last_row_current_mb_col - nsync)) {
  87. x86_pause_hint();
  88. thread_sleep(1);
  89. }
  90. }
  91. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  92. tp = tp_start;
  93. #endif
  94. /* Distance of Mb to the various image edges.
  95. * These specified to 8th pel as they are always compared
  96. * to values that are in 1/8th pel units
  97. */
  98. xd->mb_to_left_edge = -((mb_col * 16) << 3);
  99. xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3;
  100. xd->mb_to_top_edge = -((mb_row * 16) << 3);
  101. xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3;
  102. /* Set up limit values for motion vectors used to prevent
  103. * them extending outside the UMV borders
  104. */
  105. x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
  106. x->mv_col_max =
  107. ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
  108. x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
  109. x->mv_row_max =
  110. ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
  111. xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
  112. xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
  113. xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
  114. xd->left_available = (mb_col != 0);
  115. x->rddiv = cpi->RDDIV;
  116. x->rdmult = cpi->RDMULT;
  117. /* Copy current mb to a buffer */
  118. vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
  119. if (cpi->oxcf.tuning == VP8_TUNE_SSIM) vp8_activity_masking(cpi, x);
  120. /* Is segmentation enabled */
  121. /* MB level adjustment to quantizer */
  122. if (xd->segmentation_enabled) {
  123. /* Code to set segment id in xd->mbmi.segment_id for
  124. * current MB (with range checking)
  125. */
  126. if (cpi->segmentation_map[map_index + mb_col] <= 3) {
  127. xd->mode_info_context->mbmi.segment_id =
  128. cpi->segmentation_map[map_index + mb_col];
  129. } else {
  130. xd->mode_info_context->mbmi.segment_id = 0;
  131. }
  132. vp8cx_mb_init_quantizer(cpi, x, 1);
  133. } else {
  134. /* Set to Segment 0 by default */
  135. xd->mode_info_context->mbmi.segment_id = 0;
  136. }
  137. x->active_ptr = cpi->active_map + map_index + mb_col;
  138. if (cm->frame_type == KEY_FRAME) {
  139. *totalrate += vp8cx_encode_intra_macroblock(cpi, x, &tp);
  140. #ifdef MODE_STATS
  141. y_modes[xd->mbmi.mode]++;
  142. #endif
  143. } else {
  144. *totalrate += vp8cx_encode_inter_macroblock(
  145. cpi, x, &tp, recon_yoffset, recon_uvoffset, mb_row, mb_col);
  146. #ifdef MODE_STATS
  147. inter_y_modes[xd->mbmi.mode]++;
  148. if (xd->mbmi.mode == SPLITMV) {
  149. int b;
  150. for (b = 0; b < xd->mbmi.partition_count; ++b) {
  151. inter_b_modes[x->partition->bmi[b].mode]++;
  152. }
  153. }
  154. #endif
  155. // Keep track of how many (consecutive) times a block
  156. // is coded as ZEROMV_LASTREF, for base layer frames.
  157. // Reset to 0 if its coded as anything else.
  158. if (cpi->current_layer == 0) {
  159. if (xd->mode_info_context->mbmi.mode == ZEROMV &&
  160. xd->mode_info_context->mbmi.ref_frame == LAST_FRAME) {
  161. // Increment, check for wrap-around.
  162. if (cpi->consec_zero_last[map_index + mb_col] < 255) {
  163. cpi->consec_zero_last[map_index + mb_col] += 1;
  164. }
  165. if (cpi->consec_zero_last_mvbias[map_index + mb_col] < 255) {
  166. cpi->consec_zero_last_mvbias[map_index + mb_col] += 1;
  167. }
  168. } else {
  169. cpi->consec_zero_last[map_index + mb_col] = 0;
  170. cpi->consec_zero_last_mvbias[map_index + mb_col] = 0;
  171. }
  172. if (x->zero_last_dot_suppress) {
  173. cpi->consec_zero_last_mvbias[map_index + mb_col] = 0;
  174. }
  175. }
  176. /* Special case code for cyclic refresh
  177. * If cyclic update enabled then copy
  178. * xd->mbmi.segment_id; (which may have been updated
  179. * based on mode during
  180. * vp8cx_encode_inter_macroblock()) back into the
  181. * global segmentation map
  182. */
  183. if ((cpi->current_layer == 0) &&
  184. (cpi->cyclic_refresh_mode_enabled &&
  185. xd->segmentation_enabled)) {
  186. const MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
  187. cpi->segmentation_map[map_index + mb_col] = mbmi->segment_id;
  188. /* If the block has been refreshed mark it as clean
  189. * (the magnitude of the -ve influences how long it
  190. * will be before we consider another refresh):
  191. * Else if it was coded (last frame 0,0) and has
  192. * not already been refreshed then mark it as a
  193. * candidate for cleanup next time (marked 0) else
  194. * mark it as dirty (1).
  195. */
  196. if (mbmi->segment_id) {
  197. cpi->cyclic_refresh_map[map_index + mb_col] = -1;
  198. } else if ((mbmi->mode == ZEROMV) &&
  199. (mbmi->ref_frame == LAST_FRAME)) {
  200. if (cpi->cyclic_refresh_map[map_index + mb_col] == 1) {
  201. cpi->cyclic_refresh_map[map_index + mb_col] = 0;
  202. }
  203. } else {
  204. cpi->cyclic_refresh_map[map_index + mb_col] = 1;
  205. }
  206. }
  207. }
  208. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  209. /* pack tokens for this MB */
  210. {
  211. int tok_count = tp - tp_start;
  212. vp8_pack_tokens(w, tp_start, tok_count);
  213. }
  214. #else
  215. cpi->tplist[mb_row].stop = tp;
  216. #endif
  217. /* Increment pointer into gf usage flags structure. */
  218. x->gf_active_ptr++;
  219. /* Increment the activity mask pointers. */
  220. x->mb_activity_ptr++;
  221. /* adjust to the next column of macroblocks */
  222. x->src.y_buffer += 16;
  223. x->src.u_buffer += 8;
  224. x->src.v_buffer += 8;
  225. recon_yoffset += 16;
  226. recon_uvoffset += 8;
  227. /* Keep track of segment usage */
  228. segment_counts[xd->mode_info_context->mbmi.segment_id]++;
  229. /* skip to next mb */
  230. xd->mode_info_context++;
  231. x->partition_info++;
  232. xd->above_context++;
  233. }
  234. vp8_extend_mb_row(&cm->yv12_fb[dst_fb_idx], xd->dst.y_buffer + 16,
  235. xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
  236. *current_mb_col = mb_col + nsync;
  237. /* this is to account for the border */
  238. xd->mode_info_context++;
  239. x->partition_info++;
  240. x->src.y_buffer +=
  241. 16 * x->src.y_stride * (cpi->encoding_thread_count + 1) -
  242. 16 * cm->mb_cols;
  243. x->src.u_buffer +=
  244. 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) -
  245. 8 * cm->mb_cols;
  246. x->src.v_buffer +=
  247. 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) -
  248. 8 * cm->mb_cols;
  249. xd->mode_info_context +=
  250. xd->mode_info_stride * cpi->encoding_thread_count;
  251. x->partition_info += xd->mode_info_stride * cpi->encoding_thread_count;
  252. x->gf_active_ptr += cm->mb_cols * cpi->encoding_thread_count;
  253. }
  254. /* Signal that this thread has completed processing its rows. */
  255. sem_post(&cpi->h_event_end_encoding[ithread]);
  256. }
  257. }
  258. /* printf("exit thread %d\n", ithread); */
  259. return 0;
  260. }
  261. static void setup_mbby_copy(MACROBLOCK *mbdst, MACROBLOCK *mbsrc) {
  262. MACROBLOCK *x = mbsrc;
  263. MACROBLOCK *z = mbdst;
  264. int i;
  265. z->ss = x->ss;
  266. z->ss_count = x->ss_count;
  267. z->searches_per_step = x->searches_per_step;
  268. z->errorperbit = x->errorperbit;
  269. z->sadperbit16 = x->sadperbit16;
  270. z->sadperbit4 = x->sadperbit4;
  271. /*
  272. z->mv_col_min = x->mv_col_min;
  273. z->mv_col_max = x->mv_col_max;
  274. z->mv_row_min = x->mv_row_min;
  275. z->mv_row_max = x->mv_row_max;
  276. */
  277. z->short_fdct4x4 = x->short_fdct4x4;
  278. z->short_fdct8x4 = x->short_fdct8x4;
  279. z->short_walsh4x4 = x->short_walsh4x4;
  280. z->quantize_b = x->quantize_b;
  281. z->optimize = x->optimize;
  282. /*
  283. z->mvc = x->mvc;
  284. z->src.y_buffer = x->src.y_buffer;
  285. z->src.u_buffer = x->src.u_buffer;
  286. z->src.v_buffer = x->src.v_buffer;
  287. */
  288. z->mvcost[0] = x->mvcost[0];
  289. z->mvcost[1] = x->mvcost[1];
  290. z->mvsadcost[0] = x->mvsadcost[0];
  291. z->mvsadcost[1] = x->mvsadcost[1];
  292. z->token_costs = x->token_costs;
  293. z->inter_bmode_costs = x->inter_bmode_costs;
  294. z->mbmode_cost = x->mbmode_cost;
  295. z->intra_uv_mode_cost = x->intra_uv_mode_cost;
  296. z->bmode_costs = x->bmode_costs;
  297. for (i = 0; i < 25; ++i) {
  298. z->block[i].quant = x->block[i].quant;
  299. z->block[i].quant_fast = x->block[i].quant_fast;
  300. z->block[i].quant_shift = x->block[i].quant_shift;
  301. z->block[i].zbin = x->block[i].zbin;
  302. z->block[i].zrun_zbin_boost = x->block[i].zrun_zbin_boost;
  303. z->block[i].round = x->block[i].round;
  304. z->block[i].src_stride = x->block[i].src_stride;
  305. }
  306. z->q_index = x->q_index;
  307. z->act_zbin_adj = x->act_zbin_adj;
  308. z->last_act_zbin_adj = x->last_act_zbin_adj;
  309. {
  310. MACROBLOCKD *xd = &x->e_mbd;
  311. MACROBLOCKD *zd = &z->e_mbd;
  312. /*
  313. zd->mode_info_context = xd->mode_info_context;
  314. zd->mode_info = xd->mode_info;
  315. zd->mode_info_stride = xd->mode_info_stride;
  316. zd->frame_type = xd->frame_type;
  317. zd->up_available = xd->up_available ;
  318. zd->left_available = xd->left_available;
  319. zd->left_context = xd->left_context;
  320. zd->last_frame_dc = xd->last_frame_dc;
  321. zd->last_frame_dccons = xd->last_frame_dccons;
  322. zd->gold_frame_dc = xd->gold_frame_dc;
  323. zd->gold_frame_dccons = xd->gold_frame_dccons;
  324. zd->mb_to_left_edge = xd->mb_to_left_edge;
  325. zd->mb_to_right_edge = xd->mb_to_right_edge;
  326. zd->mb_to_top_edge = xd->mb_to_top_edge ;
  327. zd->mb_to_bottom_edge = xd->mb_to_bottom_edge;
  328. zd->gf_active_ptr = xd->gf_active_ptr;
  329. zd->frames_since_golden = xd->frames_since_golden;
  330. zd->frames_till_alt_ref_frame = xd->frames_till_alt_ref_frame;
  331. */
  332. zd->subpixel_predict = xd->subpixel_predict;
  333. zd->subpixel_predict8x4 = xd->subpixel_predict8x4;
  334. zd->subpixel_predict8x8 = xd->subpixel_predict8x8;
  335. zd->subpixel_predict16x16 = xd->subpixel_predict16x16;
  336. zd->segmentation_enabled = xd->segmentation_enabled;
  337. zd->mb_segement_abs_delta = xd->mb_segement_abs_delta;
  338. memcpy(zd->segment_feature_data, xd->segment_feature_data,
  339. sizeof(xd->segment_feature_data));
  340. memcpy(zd->dequant_y1_dc, xd->dequant_y1_dc, sizeof(xd->dequant_y1_dc));
  341. memcpy(zd->dequant_y1, xd->dequant_y1, sizeof(xd->dequant_y1));
  342. memcpy(zd->dequant_y2, xd->dequant_y2, sizeof(xd->dequant_y2));
  343. memcpy(zd->dequant_uv, xd->dequant_uv, sizeof(xd->dequant_uv));
  344. #if 1
  345. /*TODO: Remove dequant from BLOCKD. This is a temporary solution until
  346. * the quantizer code uses a passed in pointer to the dequant constants.
  347. * This will also require modifications to the x86 and neon assembly.
  348. * */
  349. for (i = 0; i < 16; ++i) zd->block[i].dequant = zd->dequant_y1;
  350. for (i = 16; i < 24; ++i) zd->block[i].dequant = zd->dequant_uv;
  351. zd->block[24].dequant = zd->dequant_y2;
  352. #endif
  353. memcpy(z->rd_threshes, x->rd_threshes, sizeof(x->rd_threshes));
  354. memcpy(z->rd_thresh_mult, x->rd_thresh_mult, sizeof(x->rd_thresh_mult));
  355. z->zbin_over_quant = x->zbin_over_quant;
  356. z->zbin_mode_boost_enabled = x->zbin_mode_boost_enabled;
  357. z->zbin_mode_boost = x->zbin_mode_boost;
  358. memset(z->error_bins, 0, sizeof(z->error_bins));
  359. }
  360. }
  361. void vp8cx_init_mbrthread_data(VP8_COMP *cpi, MACROBLOCK *x,
  362. MB_ROW_COMP *mbr_ei, int count) {
  363. VP8_COMMON *const cm = &cpi->common;
  364. MACROBLOCKD *const xd = &x->e_mbd;
  365. int i;
  366. for (i = 0; i < count; ++i) {
  367. MACROBLOCK *mb = &mbr_ei[i].mb;
  368. MACROBLOCKD *mbd = &mb->e_mbd;
  369. mbd->subpixel_predict = xd->subpixel_predict;
  370. mbd->subpixel_predict8x4 = xd->subpixel_predict8x4;
  371. mbd->subpixel_predict8x8 = xd->subpixel_predict8x8;
  372. mbd->subpixel_predict16x16 = xd->subpixel_predict16x16;
  373. mb->gf_active_ptr = x->gf_active_ptr;
  374. memset(mbr_ei[i].segment_counts, 0, sizeof(mbr_ei[i].segment_counts));
  375. mbr_ei[i].totalrate = 0;
  376. mb->partition_info = x->pi + x->e_mbd.mode_info_stride * (i + 1);
  377. mbd->mode_info_context = cm->mi + x->e_mbd.mode_info_stride * (i + 1);
  378. mbd->mode_info_stride = cm->mode_info_stride;
  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. 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. 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. 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. 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 (cpi->b_multi_threaded) {
  501. /* shutdown other threads */
  502. 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