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