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