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