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