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