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