Merge "MODE_INFO size reduction"
[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 #if CONFIG_MULTITHREAD
17
18 extern int vp8cx_encode_inter_macroblock(VP8_COMP *cpi, MACROBLOCK *x,
19                                          TOKENEXTRA **t, int recon_yoffset,
20                                          int recon_uvoffset);
21 extern int vp8cx_encode_intra_macro_block(VP8_COMP *cpi, MACROBLOCK *x,
22                                           TOKENEXTRA **t);
23 extern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x);
24 extern void vp8_build_block_offsets(MACROBLOCK *x);
25 extern void vp8_setup_block_ptrs(MACROBLOCK *x);
26
27 extern void loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm);
28
29 static THREAD_FUNCTION loopfilter_thread(void *p_data)
30 {
31     VP8_COMP *cpi = (VP8_COMP *)(((LPFTHREAD_DATA *)p_data)->ptr1);
32     VP8_COMMON *cm = &cpi->common;
33
34     while (1)
35     {
36         if (cpi->b_multi_threaded == 0)
37             break;
38
39         if (sem_wait(&cpi->h_event_start_lpf) == 0)
40         {
41             if (cpi->b_multi_threaded == FALSE) // we're shutting down
42                 break;
43
44             loopfilter_frame(cpi, cm);
45
46             sem_post(&cpi->h_event_end_lpf);
47         }
48     }
49
50     return 0;
51 }
52
53 static
54 THREAD_FUNCTION thread_encoding_proc(void *p_data)
55 {
56     int ithread = ((ENCODETHREAD_DATA *)p_data)->ithread;
57     VP8_COMP *cpi = (VP8_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr1);
58     MB_ROW_COMP *mbri = (MB_ROW_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr2);
59     ENTROPY_CONTEXT_PLANES mb_row_left_context;
60
61     const int nsync = cpi->mt_sync_range;
62     //printf("Started thread %d\n", ithread);
63
64     while (1)
65     {
66         if (cpi->b_multi_threaded == 0)
67             break;
68
69         //if(WaitForSingleObject(cpi->h_event_mbrencoding[ithread], INFINITE) == WAIT_OBJECT_0)
70         if (sem_wait(&cpi->h_event_start_encoding[ithread]) == 0)
71         {
72             VP8_COMMON *cm = &cpi->common;
73             int mb_row;
74             MACROBLOCK *x = &mbri->mb;
75             MACROBLOCKD *xd = &x->e_mbd;
76             TOKENEXTRA *tp ;
77
78             int *segment_counts = mbri->segment_counts;
79             int *totalrate = &mbri->totalrate;
80
81             if (cpi->b_multi_threaded == FALSE) // we're shutting down
82                 break;
83
84             for (mb_row = ithread + 1; mb_row < cm->mb_rows; mb_row += (cpi->encoding_thread_count + 1))
85             {
86
87                 int i;
88                 int recon_yoffset, recon_uvoffset;
89                 int mb_col;
90                 int ref_fb_idx = cm->lst_fb_idx;
91                 int dst_fb_idx = cm->new_fb_idx;
92                 int recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride;
93                 int recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride;
94                 int map_index = (mb_row * cm->mb_cols);
95                 volatile int *last_row_current_mb_col;
96
97                 tp = cpi->tok + (mb_row * (cm->mb_cols * 16 * 24));
98
99                 last_row_current_mb_col = &cpi->mt_current_mb_col[mb_row - 1];
100
101                 // reset above block coeffs
102                 xd->above_context = cm->above_context;
103                 xd->left_context = &mb_row_left_context;
104
105                 vp8_zero(mb_row_left_context);
106
107                 xd->up_available = (mb_row != 0);
108                 recon_yoffset = (mb_row * recon_y_stride * 16);
109                 recon_uvoffset = (mb_row * recon_uv_stride * 8);
110
111                 cpi->tplist[mb_row].start = tp;
112
113                 //printf("Thread mb_row = %d\n", mb_row);
114
115                 // Set the mb activity pointer to the start of the row.
116                 x->mb_activity_ptr = &cpi->mb_activity_map[map_index];
117                 x->mb_norm_activity_ptr =
118                     &cpi->mb_norm_activity_map[map_index];
119
120                 // for each macroblock col in image
121                 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
122                 {
123                     int seg_map_index = (mb_row * cm->mb_cols);
124
125                     if ((mb_col & (nsync - 1)) == 0)
126                     {
127                         while (mb_col > (*last_row_current_mb_col - nsync) && *last_row_current_mb_col != cm->mb_cols - 1)
128                         {
129                             x86_pause_hint();
130                             thread_sleep(0);
131                         }
132                     }
133
134                     // Distance of Mb to the various image edges.
135                     // These specified to 8th pel as they are always compared to values that are in 1/8th pel units
136                     xd->mb_to_left_edge = -((mb_col * 16) << 3);
137                     xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3;
138                     xd->mb_to_top_edge = -((mb_row * 16) << 3);
139                     xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3;
140
141                     // Set up limit values for motion vectors used to prevent them extending outside the UMV borders
142                     x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
143                     x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
144                     x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
145                     x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
146
147                     xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
148                     xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
149                     xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
150                     xd->left_available = (mb_col != 0);
151
152                     x->rddiv = cpi->RDDIV;
153                     x->rdmult = cpi->RDMULT;
154
155                     if (cpi->oxcf.tuning == VP8_TUNE_SSIM)
156                         vp8_activity_masking(cpi, x);
157
158                     // Is segmentation enabled
159                     // MB level adjutment to quantizer
160                     if (xd->segmentation_enabled)
161                     {
162                         // Code to set segment id in xd->mbmi.segment_id for current MB (with range checking)
163                         if (cpi->segmentation_map[map_index + mb_col] <= 3)
164                             xd->mode_info_context->mbmi.segment_id = cpi->segmentation_map[map_index + mb_col];
165                         else
166                             xd->mode_info_context->mbmi.segment_id = 0;
167
168                         vp8cx_mb_init_quantizer(cpi, x);
169                     }
170                     else
171                         xd->mode_info_context->mbmi.segment_id = 0; // Set to Segment 0 by default
172
173                     x->active_ptr = cpi->active_map + map_index + mb_col;
174
175                     if (cm->frame_type == KEY_FRAME)
176                     {
177                         *totalrate += vp8cx_encode_intra_macro_block(cpi, x, &tp);
178 #ifdef MODE_STATS
179                         y_modes[xd->mbmi.mode] ++;
180 #endif
181                     }
182                     else
183                     {
184                         *totalrate += vp8cx_encode_inter_macroblock(cpi, x, &tp, recon_yoffset, recon_uvoffset);
185
186 #ifdef MODE_STATS
187                         inter_y_modes[xd->mbmi.mode] ++;
188
189                         if (xd->mbmi.mode == SPLITMV)
190                         {
191                             int b;
192
193                             for (b = 0; b < xd->mbmi.partition_count; b++)
194                             {
195                                 inter_b_modes[x->partition->bmi[b].mode] ++;
196                             }
197                         }
198
199 #endif
200
201                         // Count of last ref frame 0,0 useage
202                         if ((xd->mode_info_context->mbmi.mode == ZEROMV) && (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME))
203                             cpi->inter_zz_count++;
204
205                         // Special case code for cyclic refresh
206                         // If cyclic update enabled then copy xd->mbmi.segment_id; (which may have been updated based on mode
207                         // during vp8cx_encode_inter_macroblock()) back into the global sgmentation map
208                         if (cpi->cyclic_refresh_mode_enabled && xd->segmentation_enabled)
209                         {
210                             const MB_MODE_INFO * mbmi = &xd->mode_info_context->mbmi;
211                             cpi->segmentation_map[map_index + mb_col] = mbmi->segment_id;
212
213                             // 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):
214                             // 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)
215                             // else mark it as dirty (1).
216                             if (mbmi->segment_id)
217                                 cpi->cyclic_refresh_map[map_index + mb_col] = -1;
218                             else if ((mbmi->mode == ZEROMV) && (mbmi->ref_frame == LAST_FRAME))
219                             {
220                                 if (cpi->cyclic_refresh_map[map_index + mb_col] == 1)
221                                     cpi->cyclic_refresh_map[map_index + mb_col] = 0;
222                             }
223                             else
224                                 cpi->cyclic_refresh_map[map_index + mb_col] = 1;
225
226                         }
227                     }
228                     cpi->tplist[mb_row].stop = tp;
229
230                     // Increment pointer into gf useage flags structure.
231                     x->gf_active_ptr++;
232
233                     // Increment the activity mask pointers.
234                     x->mb_activity_ptr++;
235                     x->mb_norm_activity_ptr++;
236
237                     if(cm->frame_type != INTRA_FRAME)
238                     {
239                         if (xd->mode_info_context->mbmi.mode != B_PRED)
240                         {
241                             for (i = 0; i < 16; i++)
242                                 xd->mode_info_context->bmi[i].mv.as_int = xd->block[i].bmi.mv.as_int;
243                         }else
244                         {
245                             for (i = 0; i < 16; i++)
246                                 xd->mode_info_context->bmi[i].as_mode = xd->block[i].bmi.mode;
247                         }
248                     }
249                     else {
250                         if(xd->mode_info_context->mbmi.mode != B_PRED)
251                             for (i = 0; i < 16; i++)
252                                 xd->mode_info_context->bmi[i].as_mode = xd->block[i].bmi.mode;
253                     }
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 useage
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                     cpi->mt_current_mb_col[mb_row] = mb_col;
272                 }
273
274                 //extend the recon for intra prediction
275                 vp8_extend_mb_row(
276                     &cm->yv12_fb[dst_fb_idx],
277                     xd->dst.y_buffer + 16,
278                     xd->dst.u_buffer + 8,
279                     xd->dst.v_buffer + 8);
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                     //SetEvent(cpi->h_event_main);
296                     sem_post(&cpi->h_event_end_encoding); /* signal frame encoding end */
297                 }
298             }
299         }
300     }
301
302     //printf("exit thread %d\n", ithread);
303     return 0;
304 }
305
306 static void setup_mbby_copy(MACROBLOCK *mbdst, MACROBLOCK *mbsrc)
307 {
308
309     MACROBLOCK *x = mbsrc;
310     MACROBLOCK *z = mbdst;
311     int i;
312
313     z->ss               = x->ss;
314     z->ss_count          = x->ss_count;
315     z->searches_per_step  = x->searches_per_step;
316     z->errorperbit      = x->errorperbit;
317
318     z->sadperbit16      = x->sadperbit16;
319     z->sadperbit4       = x->sadperbit4;
320
321     /*
322     z->mv_col_min    = x->mv_col_min;
323     z->mv_col_max    = x->mv_col_max;
324     z->mv_row_min    = x->mv_row_min;
325     z->mv_row_max    = x->mv_row_max;
326     z->vector_range = x->vector_range ;
327     */
328
329     z->vp8_short_fdct4x4     = x->vp8_short_fdct4x4;
330     z->vp8_short_fdct8x4     = x->vp8_short_fdct8x4;
331     z->short_walsh4x4    = x->short_walsh4x4;
332     z->quantize_b        = x->quantize_b;
333     z->optimize          = x->optimize;
334
335     /*
336     z->mvc              = x->mvc;
337     z->src.y_buffer      = x->src.y_buffer;
338     z->src.u_buffer      = x->src.u_buffer;
339     z->src.v_buffer      = x->src.v_buffer;
340     */
341
342
343     vpx_memcpy(z->mvcosts,          x->mvcosts,         sizeof(x->mvcosts));
344     z->mvcost[0] = &z->mvcosts[0][mv_max+1];
345     z->mvcost[1] = &z->mvcosts[1][mv_max+1];
346     z->mvsadcost[0] = &z->mvsadcosts[0][mvfp_max+1];
347     z->mvsadcost[1] = &z->mvsadcosts[1][mvfp_max+1];
348
349
350     vpx_memcpy(z->token_costs,       x->token_costs,      sizeof(x->token_costs));
351     vpx_memcpy(z->inter_bmode_costs,  x->inter_bmode_costs, sizeof(x->inter_bmode_costs));
352     //memcpy(z->mvcosts,            x->mvcosts,         sizeof(x->mvcosts));
353     //memcpy(z->mvcost,         x->mvcost,          sizeof(x->mvcost));
354     vpx_memcpy(z->mbmode_cost,       x->mbmode_cost,      sizeof(x->mbmode_cost));
355     vpx_memcpy(z->intra_uv_mode_cost,  x->intra_uv_mode_cost, sizeof(x->intra_uv_mode_cost));
356     vpx_memcpy(z->bmode_costs,       x->bmode_costs,      sizeof(x->bmode_costs));
357
358     for (i = 0; i < 25; i++)
359     {
360         z->block[i].quant           = x->block[i].quant;
361         z->block[i].quant_fast      = x->block[i].quant_fast;
362         z->block[i].quant_shift     = x->block[i].quant_shift;
363         z->block[i].zbin            = x->block[i].zbin;
364         z->block[i].zrun_zbin_boost   = x->block[i].zrun_zbin_boost;
365         z->block[i].round           = x->block[i].round;
366         /*
367         z->block[i].src             = x->block[i].src;
368         */
369         z->block[i].src_stride       = x->block[i].src_stride;
370         z->block[i].force_empty      = x->block[i].force_empty;
371
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         for (i = 0; i < 25; i++)
408         {
409             zd->block[i].dequant = xd->block[i].dequant;
410         }
411     }
412 }
413
414 void vp8cx_init_mbrthread_data(VP8_COMP *cpi,
415                                MACROBLOCK *x,
416                                MB_ROW_COMP *mbr_ei,
417                                int mb_row,
418                                int count
419                               )
420 {
421
422     VP8_COMMON *const cm = & cpi->common;
423     MACROBLOCKD *const xd = & x->e_mbd;
424     int i;
425     (void) mb_row;
426
427     for (i = 0; i < count; i++)
428     {
429         MACROBLOCK *mb = & mbr_ei[i].mb;
430         MACROBLOCKD *mbd = &mb->e_mbd;
431
432         mbd->subpixel_predict        = xd->subpixel_predict;
433         mbd->subpixel_predict8x4     = xd->subpixel_predict8x4;
434         mbd->subpixel_predict8x8     = xd->subpixel_predict8x8;
435         mbd->subpixel_predict16x16   = xd->subpixel_predict16x16;
436 #if CONFIG_RUNTIME_CPU_DETECT
437         mbd->rtcd                   = xd->rtcd;
438 #endif
439         mb->gf_active_ptr            = x->gf_active_ptr;
440
441         mb->vector_range             = 32;
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         mbd->frames_since_golden = cm->frames_since_golden;
454         mbd->frames_till_alt_ref_frame = cm->frames_till_alt_ref_frame;
455
456         mb->src = * cpi->Source;
457         mbd->pre = cm->yv12_fb[cm->lst_fb_idx];
458         mbd->dst = cm->yv12_fb[cm->new_fb_idx];
459
460         mb->src.y_buffer += 16 * x->src.y_stride * (i + 1);
461         mb->src.u_buffer +=  8 * x->src.uv_stride * (i + 1);
462         mb->src.v_buffer +=  8 * x->src.uv_stride * (i + 1);
463
464         vp8_build_block_offsets(mb);
465
466         vp8_setup_block_dptrs(mbd);
467
468         vp8_setup_block_ptrs(mb);
469
470         mbd->left_context = &cm->left_context;
471         mb->mvc = cm->fc.mvc;
472
473         setup_mbby_copy(&mbr_ei[i].mb, x);
474
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
485     if (cm->processor_core_count > 1 && cpi->oxcf.multi_threaded > 1)
486     {
487         int ithread;
488         int th_count = cpi->oxcf.multi_threaded - 1;
489
490         /* don't allocate more threads than cores available */
491         if (cpi->oxcf.multi_threaded > cm->processor_core_count)
492             th_count = cm->processor_core_count - 1;
493
494         /* we have th_count + 1 (main) threads processing one row each */
495         /* no point to have more threads than the sync range allows */
496         if(th_count > ((cm->mb_cols / cpi->mt_sync_range) - 1))
497         {
498             th_count = (cm->mb_cols / cpi->mt_sync_range) - 1;
499         }
500
501         if(th_count == 0)
502             return;
503
504         CHECK_MEM_ERROR(cpi->h_encoding_thread, vpx_malloc(sizeof(pthread_t) * th_count));
505         CHECK_MEM_ERROR(cpi->h_event_start_encoding, vpx_malloc(sizeof(sem_t) * th_count));
506         CHECK_MEM_ERROR(cpi->mb_row_ei, vpx_memalign(32, sizeof(MB_ROW_COMP) * th_count));
507         vpx_memset(cpi->mb_row_ei, 0, sizeof(MB_ROW_COMP) * th_count);
508         CHECK_MEM_ERROR(cpi->en_thread_data,
509                         vpx_malloc(sizeof(ENCODETHREAD_DATA) * th_count));
510         CHECK_MEM_ERROR(cpi->mt_current_mb_col,
511                         vpx_malloc(sizeof(*cpi->mt_current_mb_col) * cm->mb_rows));
512
513         sem_init(&cpi->h_event_end_encoding, 0, 0);
514
515         cpi->b_multi_threaded = 1;
516         cpi->encoding_thread_count = th_count;
517
518         /*
519         printf("[VP8:] multi_threaded encoding is enabled with %d threads\n\n",
520                (cpi->encoding_thread_count +1));
521         */
522
523         for (ithread = 0; ithread < th_count; ithread++)
524         {
525             ENCODETHREAD_DATA * ethd = &cpi->en_thread_data[ithread];
526
527             sem_init(&cpi->h_event_start_encoding[ithread], 0, 0);
528             ethd->ithread = ithread;
529             ethd->ptr1 = (void *)cpi;
530             ethd->ptr2 = (void *)&cpi->mb_row_ei[ithread];
531
532             pthread_create(&cpi->h_encoding_thread[ithread], 0, thread_encoding_proc, ethd);
533         }
534
535         {
536             LPFTHREAD_DATA * lpfthd = &cpi->lpf_thread_data;
537
538             sem_init(&cpi->h_event_start_lpf, 0, 0);
539             sem_init(&cpi->h_event_end_lpf, 0, 0);
540
541             lpfthd->ptr1 = (void *)cpi;
542             pthread_create(&cpi->h_filter_thread, 0, loopfilter_thread, lpfthd);
543         }
544     }
545
546 }
547
548 void vp8cx_remove_encoder_threads(VP8_COMP *cpi)
549 {
550     if (cpi->b_multi_threaded)
551     {
552         //shutdown other threads
553         cpi->b_multi_threaded = 0;
554         {
555             int i;
556
557             for (i = 0; i < cpi->encoding_thread_count; i++)
558             {
559                 //SetEvent(cpi->h_event_mbrencoding[i]);
560                 sem_post(&cpi->h_event_start_encoding[i]);
561                 pthread_join(cpi->h_encoding_thread[i], 0);
562
563                 sem_destroy(&cpi->h_event_start_encoding[i]);
564             }
565
566             sem_post(&cpi->h_event_start_lpf);
567             pthread_join(cpi->h_filter_thread, 0);
568         }
569
570         sem_destroy(&cpi->h_event_end_encoding);
571         sem_destroy(&cpi->h_event_end_lpf);
572         sem_destroy(&cpi->h_event_start_lpf);
573
574         //free thread related resources
575         vpx_free(cpi->h_event_start_encoding);
576         vpx_free(cpi->h_encoding_thread);
577         vpx_free(cpi->mb_row_ei);
578         vpx_free(cpi->en_thread_data);
579         vpx_free(cpi->mt_current_mb_col);
580     }
581 }
582 #endif