Home | History | Annotate | Download | only in decoder
      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 
     12 #include "vpx_config.h"
     13 #include "vp8_rtcd.h"
     14 #if !defined(WIN32) && CONFIG_OS_SUPPORT == 1
     15 # include <unistd.h>
     16 #endif
     17 #include "onyxd_int.h"
     18 #include "vpx_mem/vpx_mem.h"
     19 #include "vp8/common/threading.h"
     20 
     21 #include "vp8/common/loopfilter.h"
     22 #include "vp8/common/extend.h"
     23 #include "vpx_ports/vpx_timer.h"
     24 #include "detokenize.h"
     25 #include "vp8/common/reconintra4x4.h"
     26 #include "vp8/common/reconinter.h"
     27 #include "vp8/common/setupintrarecon.h"
     28 #if CONFIG_ERROR_CONCEALMENT
     29 #include "error_concealment.h"
     30 #endif
     31 
     32 #define CALLOC_ARRAY(p, n) CHECK_MEM_ERROR((p), vpx_calloc(sizeof(*(p)), (n)))
     33 #define CALLOC_ARRAY_ALIGNED(p, n, algn) do {                      \
     34   CHECK_MEM_ERROR((p), vpx_memalign((algn), sizeof(*(p)) * (n)));  \
     35   memset((p), 0, (n) * sizeof(*(p)));                              \
     36 } while (0)
     37 
     38 
     39 void vp8_mb_init_dequantizer(VP8D_COMP *pbi, MACROBLOCKD *xd);
     40 
     41 static void setup_decoding_thread_data(VP8D_COMP *pbi, MACROBLOCKD *xd, MB_ROW_DEC *mbrd, int count)
     42 {
     43     VP8_COMMON *const pc = & pbi->common;
     44     int i;
     45 
     46     for (i = 0; i < count; i++)
     47     {
     48         MACROBLOCKD *mbd = &mbrd[i].mbd;
     49         mbd->subpixel_predict        = xd->subpixel_predict;
     50         mbd->subpixel_predict8x4     = xd->subpixel_predict8x4;
     51         mbd->subpixel_predict8x8     = xd->subpixel_predict8x8;
     52         mbd->subpixel_predict16x16   = xd->subpixel_predict16x16;
     53 
     54         mbd->mode_info_context = pc->mi   + pc->mode_info_stride * (i + 1);
     55         mbd->mode_info_stride  = pc->mode_info_stride;
     56 
     57         mbd->frame_type = pc->frame_type;
     58         mbd->pre = xd->pre;
     59         mbd->dst = xd->dst;
     60 
     61         mbd->segmentation_enabled    = xd->segmentation_enabled;
     62         mbd->mb_segement_abs_delta     = xd->mb_segement_abs_delta;
     63         vpx_memcpy(mbd->segment_feature_data, xd->segment_feature_data, sizeof(xd->segment_feature_data));
     64 
     65         /*signed char ref_lf_deltas[MAX_REF_LF_DELTAS];*/
     66         vpx_memcpy(mbd->ref_lf_deltas, xd->ref_lf_deltas, sizeof(xd->ref_lf_deltas));
     67         /*signed char mode_lf_deltas[MAX_MODE_LF_DELTAS];*/
     68         vpx_memcpy(mbd->mode_lf_deltas, xd->mode_lf_deltas, sizeof(xd->mode_lf_deltas));
     69         /*unsigned char mode_ref_lf_delta_enabled;
     70         unsigned char mode_ref_lf_delta_update;*/
     71         mbd->mode_ref_lf_delta_enabled    = xd->mode_ref_lf_delta_enabled;
     72         mbd->mode_ref_lf_delta_update    = xd->mode_ref_lf_delta_update;
     73 
     74         mbd->current_bc = &pbi->mbc[0];
     75 
     76         vpx_memcpy(mbd->dequant_y1_dc, xd->dequant_y1_dc, sizeof(xd->dequant_y1_dc));
     77         vpx_memcpy(mbd->dequant_y1, xd->dequant_y1, sizeof(xd->dequant_y1));
     78         vpx_memcpy(mbd->dequant_y2, xd->dequant_y2, sizeof(xd->dequant_y2));
     79         vpx_memcpy(mbd->dequant_uv, xd->dequant_uv, sizeof(xd->dequant_uv));
     80 
     81         mbd->fullpixel_mask = 0xffffffff;
     82 
     83         if (pc->full_pixel)
     84             mbd->fullpixel_mask = 0xfffffff8;
     85 
     86     }
     87 
     88     for (i = 0; i < pc->mb_rows; i++)
     89         pbi->mt_current_mb_col[i] = -1;
     90 }
     91 
     92 static void mt_decode_macroblock(VP8D_COMP *pbi, MACROBLOCKD *xd,
     93                                  unsigned int mb_idx)
     94 {
     95     MB_PREDICTION_MODE mode;
     96     int i;
     97 #if CONFIG_ERROR_CONCEALMENT
     98     int corruption_detected = 0;
     99 #endif
    100     (void)mb_idx;
    101     if (xd->mode_info_context->mbmi.mb_skip_coeff)
    102     {
    103         vp8_reset_mb_tokens_context(xd);
    104     }
    105     else if (!vp8dx_bool_error(xd->current_bc))
    106     {
    107         int eobtotal;
    108         eobtotal = vp8_decode_mb_tokens(pbi, xd);
    109 
    110         /* Special case:  Force the loopfilter to skip when eobtotal is zero */
    111         xd->mode_info_context->mbmi.mb_skip_coeff = (eobtotal==0);
    112     }
    113 
    114     mode = xd->mode_info_context->mbmi.mode;
    115 
    116     if (xd->segmentation_enabled)
    117         vp8_mb_init_dequantizer(pbi, xd);
    118 
    119 
    120 #if CONFIG_ERROR_CONCEALMENT
    121 
    122     if(pbi->ec_active)
    123     {
    124         int throw_residual;
    125         /* When we have independent partitions we can apply residual even
    126          * though other partitions within the frame are corrupt.
    127          */
    128         throw_residual = (!pbi->independent_partitions &&
    129                           pbi->frame_corrupt_residual);
    130         throw_residual = (throw_residual || vp8dx_bool_error(xd->current_bc));
    131 
    132         if ((mb_idx >= pbi->mvs_corrupt_from_mb || throw_residual))
    133         {
    134             /* MB with corrupt residuals or corrupt mode/motion vectors.
    135              * Better to use the predictor as reconstruction.
    136              */
    137             pbi->frame_corrupt_residual = 1;
    138             vpx_memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
    139             vp8_conceal_corrupt_mb(xd);
    140 
    141 
    142             corruption_detected = 1;
    143 
    144             /* force idct to be skipped for B_PRED and use the
    145              * prediction only for reconstruction
    146              * */
    147             vpx_memset(xd->eobs, 0, 25);
    148         }
    149     }
    150 #endif
    151 
    152     /* do prediction */
    153     if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
    154     {
    155         vp8_build_intra_predictors_mbuv_s(xd,
    156                                           xd->recon_above[1],
    157                                           xd->recon_above[2],
    158                                           xd->recon_left[1],
    159                                           xd->recon_left[2],
    160                                           xd->recon_left_stride[1],
    161                                           xd->dst.u_buffer, xd->dst.v_buffer,
    162                                           xd->dst.uv_stride);
    163 
    164         if (mode != B_PRED)
    165         {
    166             vp8_build_intra_predictors_mby_s(xd,
    167                                                  xd->recon_above[0],
    168                                                  xd->recon_left[0],
    169                                                  xd->recon_left_stride[0],
    170                                                  xd->dst.y_buffer,
    171                                                  xd->dst.y_stride);
    172         }
    173         else
    174         {
    175             short *DQC = xd->dequant_y1;
    176             int dst_stride = xd->dst.y_stride;
    177 
    178             /* clear out residual eob info */
    179             if(xd->mode_info_context->mbmi.mb_skip_coeff)
    180                 vpx_memset(xd->eobs, 0, 25);
    181 
    182             intra_prediction_down_copy(xd, xd->recon_above[0] + 16);
    183 
    184             for (i = 0; i < 16; i++)
    185             {
    186                 BLOCKD *b = &xd->block[i];
    187                 unsigned char *dst = xd->dst.y_buffer + b->offset;
    188                 B_PREDICTION_MODE b_mode =
    189                     xd->mode_info_context->bmi[i].as_mode;
    190                 unsigned char *Above;
    191                 unsigned char *yleft;
    192                 int left_stride;
    193                 unsigned char top_left;
    194 
    195                 /*Caution: For some b_mode, it needs 8 pixels (4 above + 4 above-right).*/
    196                 if (i < 4 && pbi->common.filter_level)
    197                     Above = xd->recon_above[0] + b->offset;
    198                 else
    199                     Above = dst - dst_stride;
    200 
    201                 if (i%4==0 && pbi->common.filter_level)
    202                 {
    203                     yleft = xd->recon_left[0] + i;
    204                     left_stride = 1;
    205                 }
    206                 else
    207                 {
    208                     yleft = dst - 1;
    209                     left_stride = dst_stride;
    210                 }
    211 
    212                 if ((i==4 || i==8 || i==12) && pbi->common.filter_level)
    213                     top_left = *(xd->recon_left[0] + i - 1);
    214                 else
    215                     top_left = Above[-1];
    216 
    217                 vp8_intra4x4_predict(Above, yleft, left_stride,
    218                                      b_mode, dst, dst_stride, top_left);
    219 
    220                 if (xd->eobs[i] )
    221                 {
    222                     if (xd->eobs[i] > 1)
    223                     {
    224                         vp8_dequant_idct_add(b->qcoeff, DQC, dst, dst_stride);
    225                     }
    226                     else
    227                     {
    228                         vp8_dc_only_idct_add(b->qcoeff[0] * DQC[0],
    229                                              dst, dst_stride, dst, dst_stride);
    230                         vpx_memset(b->qcoeff, 0, 2 * sizeof(b->qcoeff[0]));
    231                     }
    232                 }
    233             }
    234         }
    235     }
    236     else
    237     {
    238         vp8_build_inter_predictors_mb(xd);
    239     }
    240 
    241 
    242 #if CONFIG_ERROR_CONCEALMENT
    243     if (corruption_detected)
    244     {
    245         return;
    246     }
    247 #endif
    248 
    249     if(!xd->mode_info_context->mbmi.mb_skip_coeff)
    250     {
    251         /* dequantization and idct */
    252         if (mode != B_PRED)
    253         {
    254             short *DQC = xd->dequant_y1;
    255 
    256             if (mode != SPLITMV)
    257             {
    258                 BLOCKD *b = &xd->block[24];
    259 
    260                 /* do 2nd order transform on the dc block */
    261                 if (xd->eobs[24] > 1)
    262                 {
    263                     vp8_dequantize_b(b, xd->dequant_y2);
    264 
    265                     vp8_short_inv_walsh4x4(&b->dqcoeff[0],
    266                         xd->qcoeff);
    267                     vpx_memset(b->qcoeff, 0, 16 * sizeof(b->qcoeff[0]));
    268                 }
    269                 else
    270                 {
    271                     b->dqcoeff[0] = b->qcoeff[0] * xd->dequant_y2[0];
    272                     vp8_short_inv_walsh4x4_1(&b->dqcoeff[0],
    273                         xd->qcoeff);
    274                     vpx_memset(b->qcoeff, 0, 2 * sizeof(b->qcoeff[0]));
    275                 }
    276 
    277                 /* override the dc dequant constant in order to preserve the
    278                  * dc components
    279                  */
    280                 DQC = xd->dequant_y1_dc;
    281             }
    282 
    283             vp8_dequant_idct_add_y_block
    284                             (xd->qcoeff, DQC,
    285                              xd->dst.y_buffer,
    286                              xd->dst.y_stride, xd->eobs);
    287         }
    288 
    289         vp8_dequant_idct_add_uv_block
    290                         (xd->qcoeff+16*16, xd->dequant_uv,
    291                          xd->dst.u_buffer, xd->dst.v_buffer,
    292                          xd->dst.uv_stride, xd->eobs+16);
    293     }
    294 }
    295 
    296 static void mt_decode_mb_rows(VP8D_COMP *pbi, MACROBLOCKD *xd, int start_mb_row)
    297 {
    298     volatile const int *last_row_current_mb_col;
    299     volatile int *current_mb_col;
    300     int mb_row;
    301     VP8_COMMON *pc = &pbi->common;
    302     const int nsync = pbi->sync_range;
    303     const int first_row_no_sync_above = pc->mb_cols + nsync;
    304     int num_part = 1 << pbi->common.multi_token_partition;
    305     int last_mb_row = start_mb_row;
    306 
    307     YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME];
    308     YV12_BUFFER_CONFIG *yv12_fb_lst = pbi->dec_fb_ref[LAST_FRAME];
    309 
    310     int recon_y_stride = yv12_fb_new->y_stride;
    311     int recon_uv_stride = yv12_fb_new->uv_stride;
    312 
    313     unsigned char *ref_buffer[MAX_REF_FRAMES][3];
    314     unsigned char *dst_buffer[3];
    315     int i;
    316     int ref_fb_corrupted[MAX_REF_FRAMES];
    317 
    318     ref_fb_corrupted[INTRA_FRAME] = 0;
    319 
    320     for(i = 1; i < MAX_REF_FRAMES; i++)
    321     {
    322         YV12_BUFFER_CONFIG *this_fb = pbi->dec_fb_ref[i];
    323 
    324         ref_buffer[i][0] = this_fb->y_buffer;
    325         ref_buffer[i][1] = this_fb->u_buffer;
    326         ref_buffer[i][2] = this_fb->v_buffer;
    327 
    328         ref_fb_corrupted[i] = this_fb->corrupted;
    329     }
    330 
    331     dst_buffer[0] = yv12_fb_new->y_buffer;
    332     dst_buffer[1] = yv12_fb_new->u_buffer;
    333     dst_buffer[2] = yv12_fb_new->v_buffer;
    334 
    335     xd->up_available = (start_mb_row != 0);
    336 
    337     for (mb_row = start_mb_row; mb_row < pc->mb_rows; mb_row += (pbi->decoding_thread_count + 1))
    338     {
    339        int recon_yoffset, recon_uvoffset;
    340        int mb_col;
    341        int filter_level;
    342        loop_filter_info_n *lfi_n = &pc->lf_info;
    343 
    344        /* save last row processed by this thread */
    345        last_mb_row = mb_row;
    346        /* select bool coder for current partition */
    347        xd->current_bc =  &pbi->mbc[mb_row%num_part];
    348 
    349        if (mb_row > 0)
    350            last_row_current_mb_col = &pbi->mt_current_mb_col[mb_row -1];
    351        else
    352            last_row_current_mb_col = &first_row_no_sync_above;
    353 
    354        current_mb_col = &pbi->mt_current_mb_col[mb_row];
    355 
    356        recon_yoffset = mb_row * recon_y_stride * 16;
    357        recon_uvoffset = mb_row * recon_uv_stride * 8;
    358 
    359        /* reset contexts */
    360        xd->above_context = pc->above_context;
    361        vpx_memset(xd->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
    362 
    363        xd->left_available = 0;
    364 
    365        xd->mb_to_top_edge = -((mb_row * 16)) << 3;
    366        xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
    367 
    368        if (pbi->common.filter_level)
    369        {
    370           xd->recon_above[0] = pbi->mt_yabove_row[mb_row] + 0*16 +32;
    371           xd->recon_above[1] = pbi->mt_uabove_row[mb_row] + 0*8 +16;
    372           xd->recon_above[2] = pbi->mt_vabove_row[mb_row] + 0*8 +16;
    373 
    374           xd->recon_left[0] = pbi->mt_yleft_col[mb_row];
    375           xd->recon_left[1] = pbi->mt_uleft_col[mb_row];
    376           xd->recon_left[2] = pbi->mt_vleft_col[mb_row];
    377 
    378           /* TODO: move to outside row loop */
    379           xd->recon_left_stride[0] = 1;
    380           xd->recon_left_stride[1] = 1;
    381        }
    382        else
    383        {
    384           xd->recon_above[0] = dst_buffer[0] + recon_yoffset;
    385           xd->recon_above[1] = dst_buffer[1] + recon_uvoffset;
    386           xd->recon_above[2] = dst_buffer[2] + recon_uvoffset;
    387 
    388           xd->recon_left[0] = xd->recon_above[0] - 1;
    389           xd->recon_left[1] = xd->recon_above[1] - 1;
    390           xd->recon_left[2] = xd->recon_above[2] - 1;
    391 
    392           xd->recon_above[0] -= xd->dst.y_stride;
    393           xd->recon_above[1] -= xd->dst.uv_stride;
    394           xd->recon_above[2] -= xd->dst.uv_stride;
    395 
    396           /* TODO: move to outside row loop */
    397           xd->recon_left_stride[0] = xd->dst.y_stride;
    398           xd->recon_left_stride[1] = xd->dst.uv_stride;
    399 
    400           setup_intra_recon_left(xd->recon_left[0], xd->recon_left[1],
    401                                  xd->recon_left[2], xd->dst.y_stride,
    402                                  xd->dst.uv_stride);
    403        }
    404 
    405        for (mb_col = 0; mb_col < pc->mb_cols; mb_col++)
    406        {
    407            *current_mb_col = mb_col - 1;
    408 
    409            if ((mb_col & (nsync - 1)) == 0)
    410            {
    411                while (mb_col > (*last_row_current_mb_col - nsync))
    412                {
    413                    x86_pause_hint();
    414                    thread_sleep(0);
    415                }
    416            }
    417 
    418            /* Distance of MB to the various image edges.
    419             * These are specified to 8th pel as they are always
    420             * compared to values that are in 1/8th pel units.
    421             */
    422            xd->mb_to_left_edge = -((mb_col * 16) << 3);
    423            xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
    424 
    425     #if CONFIG_ERROR_CONCEALMENT
    426            {
    427                int corrupt_residual =
    428                            (!pbi->independent_partitions &&
    429                            pbi->frame_corrupt_residual) ||
    430                            vp8dx_bool_error(xd->current_bc);
    431                if (pbi->ec_active &&
    432                    (xd->mode_info_context->mbmi.ref_frame ==
    433                                                     INTRA_FRAME) &&
    434                    corrupt_residual)
    435                {
    436                    /* We have an intra block with corrupt
    437                     * coefficients, better to conceal with an inter
    438                     * block.
    439                     * Interpolate MVs from neighboring MBs
    440                     *
    441                     * Note that for the first mb with corrupt
    442                     * residual in a frame, we might not discover
    443                     * that before decoding the residual. That
    444                     * happens after this check, and therefore no
    445                     * inter concealment will be done.
    446                     */
    447                    vp8_interpolate_motion(xd,
    448                                           mb_row, mb_col,
    449                                           pc->mb_rows, pc->mb_cols,
    450                                           pc->mode_info_stride);
    451                }
    452            }
    453     #endif
    454 
    455 
    456            xd->dst.y_buffer = dst_buffer[0] + recon_yoffset;
    457            xd->dst.u_buffer = dst_buffer[1] + recon_uvoffset;
    458            xd->dst.v_buffer = dst_buffer[2] + recon_uvoffset;
    459 
    460            xd->pre.y_buffer = ref_buffer[xd->mode_info_context->mbmi.ref_frame][0] + recon_yoffset;
    461            xd->pre.u_buffer = ref_buffer[xd->mode_info_context->mbmi.ref_frame][1] + recon_uvoffset;
    462            xd->pre.v_buffer = ref_buffer[xd->mode_info_context->mbmi.ref_frame][2] + recon_uvoffset;
    463 
    464            /* propagate errors from reference frames */
    465            xd->corrupted |= ref_fb_corrupted[xd->mode_info_context->mbmi.ref_frame];
    466 
    467            mt_decode_macroblock(pbi, xd, 0);
    468 
    469            xd->left_available = 1;
    470 
    471            /* check if the boolean decoder has suffered an error */
    472            xd->corrupted |= vp8dx_bool_error(xd->current_bc);
    473 
    474            xd->recon_above[0] += 16;
    475            xd->recon_above[1] += 8;
    476            xd->recon_above[2] += 8;
    477 
    478            if (!pbi->common.filter_level)
    479            {
    480               xd->recon_left[0] += 16;
    481               xd->recon_left[1] += 8;
    482               xd->recon_left[2] += 8;
    483            }
    484 
    485            if (pbi->common.filter_level)
    486            {
    487                int skip_lf = (xd->mode_info_context->mbmi.mode != B_PRED &&
    488                                xd->mode_info_context->mbmi.mode != SPLITMV &&
    489                                xd->mode_info_context->mbmi.mb_skip_coeff);
    490 
    491                const int mode_index = lfi_n->mode_lf_lut[xd->mode_info_context->mbmi.mode];
    492                const int seg = xd->mode_info_context->mbmi.segment_id;
    493                const int ref_frame = xd->mode_info_context->mbmi.ref_frame;
    494 
    495                filter_level = lfi_n->lvl[seg][ref_frame][mode_index];
    496 
    497                if( mb_row != pc->mb_rows-1 )
    498                {
    499                    /* Save decoded MB last row data for next-row decoding */
    500                    vpx_memcpy((pbi->mt_yabove_row[mb_row + 1] + 32 + mb_col*16), (xd->dst.y_buffer + 15 * recon_y_stride), 16);
    501                    vpx_memcpy((pbi->mt_uabove_row[mb_row + 1] + 16 + mb_col*8), (xd->dst.u_buffer + 7 * recon_uv_stride), 8);
    502                    vpx_memcpy((pbi->mt_vabove_row[mb_row + 1] + 16 + mb_col*8), (xd->dst.v_buffer + 7 * recon_uv_stride), 8);
    503                }
    504 
    505                /* save left_col for next MB decoding */
    506                if(mb_col != pc->mb_cols-1)
    507                {
    508                    MODE_INFO *next = xd->mode_info_context +1;
    509 
    510                    if (next->mbmi.ref_frame == INTRA_FRAME)
    511                    {
    512                        for (i = 0; i < 16; i++)
    513                            pbi->mt_yleft_col[mb_row][i] = xd->dst.y_buffer [i* recon_y_stride + 15];
    514                        for (i = 0; i < 8; i++)
    515                        {
    516                            pbi->mt_uleft_col[mb_row][i] = xd->dst.u_buffer [i* recon_uv_stride + 7];
    517                            pbi->mt_vleft_col[mb_row][i] = xd->dst.v_buffer [i* recon_uv_stride + 7];
    518                        }
    519                    }
    520                }
    521 
    522                /* loopfilter on this macroblock. */
    523                if (filter_level)
    524                {
    525                    if(pc->filter_type == NORMAL_LOOPFILTER)
    526                    {
    527                        loop_filter_info lfi;
    528                        FRAME_TYPE frame_type = pc->frame_type;
    529                        const int hev_index = lfi_n->hev_thr_lut[frame_type][filter_level];
    530                        lfi.mblim = lfi_n->mblim[filter_level];
    531                        lfi.blim = lfi_n->blim[filter_level];
    532                        lfi.lim = lfi_n->lim[filter_level];
    533                        lfi.hev_thr = lfi_n->hev_thr[hev_index];
    534 
    535                        if (mb_col > 0)
    536                            vp8_loop_filter_mbv
    537                            (xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi);
    538 
    539                        if (!skip_lf)
    540                            vp8_loop_filter_bv
    541                            (xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi);
    542 
    543                        /* don't apply across umv border */
    544                        if (mb_row > 0)
    545                            vp8_loop_filter_mbh
    546                            (xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi);
    547 
    548                        if (!skip_lf)
    549                            vp8_loop_filter_bh
    550                            (xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer,  recon_y_stride, recon_uv_stride, &lfi);
    551                    }
    552                    else
    553                    {
    554                        if (mb_col > 0)
    555                            vp8_loop_filter_simple_mbv
    556                            (xd->dst.y_buffer, recon_y_stride, lfi_n->mblim[filter_level]);
    557 
    558                        if (!skip_lf)
    559                            vp8_loop_filter_simple_bv
    560                            (xd->dst.y_buffer, recon_y_stride, lfi_n->blim[filter_level]);
    561 
    562                        /* don't apply across umv border */
    563                        if (mb_row > 0)
    564                            vp8_loop_filter_simple_mbh
    565                            (xd->dst.y_buffer, recon_y_stride, lfi_n->mblim[filter_level]);
    566 
    567                        if (!skip_lf)
    568                            vp8_loop_filter_simple_bh
    569                            (xd->dst.y_buffer, recon_y_stride, lfi_n->blim[filter_level]);
    570                    }
    571                }
    572 
    573            }
    574 
    575            recon_yoffset += 16;
    576            recon_uvoffset += 8;
    577 
    578            ++xd->mode_info_context;  /* next mb */
    579 
    580            xd->above_context++;
    581        }
    582 
    583        /* adjust to the next row of mbs */
    584        if (pbi->common.filter_level)
    585        {
    586            if(mb_row != pc->mb_rows-1)
    587            {
    588                int lasty = yv12_fb_lst->y_width + VP8BORDERINPIXELS;
    589                int lastuv = (yv12_fb_lst->y_width>>1) + (VP8BORDERINPIXELS>>1);
    590 
    591                for (i = 0; i < 4; i++)
    592                {
    593                    pbi->mt_yabove_row[mb_row +1][lasty + i] = pbi->mt_yabove_row[mb_row +1][lasty -1];
    594                    pbi->mt_uabove_row[mb_row +1][lastuv + i] = pbi->mt_uabove_row[mb_row +1][lastuv -1];
    595                    pbi->mt_vabove_row[mb_row +1][lastuv + i] = pbi->mt_vabove_row[mb_row +1][lastuv -1];
    596                }
    597            }
    598        }
    599        else
    600            vp8_extend_mb_row(yv12_fb_new, xd->dst.y_buffer + 16,
    601                              xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
    602 
    603        /* last MB of row is ready just after extension is done */
    604        *current_mb_col = mb_col + nsync;
    605 
    606        ++xd->mode_info_context;      /* skip prediction column */
    607        xd->up_available = 1;
    608 
    609        /* since we have multithread */
    610        xd->mode_info_context += xd->mode_info_stride * pbi->decoding_thread_count;
    611     }
    612 
    613     /* signal end of frame decoding if this thread processed the last mb_row */
    614     if (last_mb_row == (pc->mb_rows - 1))
    615         sem_post(&pbi->h_event_end_decoding);
    616 
    617 }
    618 
    619 
    620 static THREAD_FUNCTION thread_decoding_proc(void *p_data)
    621 {
    622     int ithread = ((DECODETHREAD_DATA *)p_data)->ithread;
    623     VP8D_COMP *pbi = (VP8D_COMP *)(((DECODETHREAD_DATA *)p_data)->ptr1);
    624     MB_ROW_DEC *mbrd = (MB_ROW_DEC *)(((DECODETHREAD_DATA *)p_data)->ptr2);
    625     ENTROPY_CONTEXT_PLANES mb_row_left_context;
    626 
    627     while (1)
    628     {
    629         if (pbi->b_multithreaded_rd == 0)
    630             break;
    631 
    632         if (sem_wait(&pbi->h_event_start_decoding[ithread]) == 0)
    633         {
    634             if (pbi->b_multithreaded_rd == 0)
    635                 break;
    636             else
    637             {
    638                 MACROBLOCKD *xd = &mbrd->mbd;
    639                 xd->left_context = &mb_row_left_context;
    640 
    641                 mt_decode_mb_rows(pbi, xd, ithread+1);
    642             }
    643         }
    644     }
    645 
    646     return 0 ;
    647 }
    648 
    649 
    650 void vp8_decoder_create_threads(VP8D_COMP *pbi)
    651 {
    652     int core_count = 0;
    653     unsigned int ithread;
    654 
    655     pbi->b_multithreaded_rd = 0;
    656     pbi->allocated_decoding_thread_count = 0;
    657 
    658     /* limit decoding threads to the max number of token partitions */
    659     core_count = (pbi->max_threads > 8) ? 8 : pbi->max_threads;
    660 
    661     /* limit decoding threads to the available cores */
    662     if (core_count > pbi->common.processor_core_count)
    663         core_count = pbi->common.processor_core_count;
    664 
    665     if (core_count > 1)
    666     {
    667         pbi->b_multithreaded_rd = 1;
    668         pbi->decoding_thread_count = core_count - 1;
    669 
    670         CALLOC_ARRAY(pbi->h_decoding_thread, pbi->decoding_thread_count);
    671         CALLOC_ARRAY(pbi->h_event_start_decoding, pbi->decoding_thread_count);
    672         CALLOC_ARRAY_ALIGNED(pbi->mb_row_di, pbi->decoding_thread_count, 32);
    673         CALLOC_ARRAY(pbi->de_thread_data, pbi->decoding_thread_count);
    674 
    675         for (ithread = 0; ithread < pbi->decoding_thread_count; ithread++)
    676         {
    677             sem_init(&pbi->h_event_start_decoding[ithread], 0, 0);
    678 
    679             vp8_setup_block_dptrs(&pbi->mb_row_di[ithread].mbd);
    680 
    681             pbi->de_thread_data[ithread].ithread  = ithread;
    682             pbi->de_thread_data[ithread].ptr1     = (void *)pbi;
    683             pbi->de_thread_data[ithread].ptr2     = (void *) &pbi->mb_row_di[ithread];
    684 
    685             pthread_create(&pbi->h_decoding_thread[ithread], 0, thread_decoding_proc, (&pbi->de_thread_data[ithread]));
    686         }
    687 
    688         sem_init(&pbi->h_event_end_decoding, 0, 0);
    689 
    690         pbi->allocated_decoding_thread_count = pbi->decoding_thread_count;
    691     }
    692 }
    693 
    694 
    695 void vp8mt_de_alloc_temp_buffers(VP8D_COMP *pbi, int mb_rows)
    696 {
    697     int i;
    698 
    699     if (pbi->b_multithreaded_rd)
    700     {
    701             vpx_free(pbi->mt_current_mb_col);
    702             pbi->mt_current_mb_col = NULL ;
    703 
    704         /* Free above_row buffers. */
    705         if (pbi->mt_yabove_row)
    706         {
    707             for (i=0; i< mb_rows; i++)
    708             {
    709                     vpx_free(pbi->mt_yabove_row[i]);
    710                     pbi->mt_yabove_row[i] = NULL ;
    711             }
    712             vpx_free(pbi->mt_yabove_row);
    713             pbi->mt_yabove_row = NULL ;
    714         }
    715 
    716         if (pbi->mt_uabove_row)
    717         {
    718             for (i=0; i< mb_rows; i++)
    719             {
    720                     vpx_free(pbi->mt_uabove_row[i]);
    721                     pbi->mt_uabove_row[i] = NULL ;
    722             }
    723             vpx_free(pbi->mt_uabove_row);
    724             pbi->mt_uabove_row = NULL ;
    725         }
    726 
    727         if (pbi->mt_vabove_row)
    728         {
    729             for (i=0; i< mb_rows; i++)
    730             {
    731                     vpx_free(pbi->mt_vabove_row[i]);
    732                     pbi->mt_vabove_row[i] = NULL ;
    733             }
    734             vpx_free(pbi->mt_vabove_row);
    735             pbi->mt_vabove_row = NULL ;
    736         }
    737 
    738         /* Free left_col buffers. */
    739         if (pbi->mt_yleft_col)
    740         {
    741             for (i=0; i< mb_rows; i++)
    742             {
    743                     vpx_free(pbi->mt_yleft_col[i]);
    744                     pbi->mt_yleft_col[i] = NULL ;
    745             }
    746             vpx_free(pbi->mt_yleft_col);
    747             pbi->mt_yleft_col = NULL ;
    748         }
    749 
    750         if (pbi->mt_uleft_col)
    751         {
    752             for (i=0; i< mb_rows; i++)
    753             {
    754                     vpx_free(pbi->mt_uleft_col[i]);
    755                     pbi->mt_uleft_col[i] = NULL ;
    756             }
    757             vpx_free(pbi->mt_uleft_col);
    758             pbi->mt_uleft_col = NULL ;
    759         }
    760 
    761         if (pbi->mt_vleft_col)
    762         {
    763             for (i=0; i< mb_rows; i++)
    764             {
    765                     vpx_free(pbi->mt_vleft_col[i]);
    766                     pbi->mt_vleft_col[i] = NULL ;
    767             }
    768             vpx_free(pbi->mt_vleft_col);
    769             pbi->mt_vleft_col = NULL ;
    770         }
    771     }
    772 }
    773 
    774 
    775 void vp8mt_alloc_temp_buffers(VP8D_COMP *pbi, int width, int prev_mb_rows)
    776 {
    777     VP8_COMMON *const pc = & pbi->common;
    778     int i;
    779     int uv_width;
    780 
    781     if (pbi->b_multithreaded_rd)
    782     {
    783         vp8mt_de_alloc_temp_buffers(pbi, prev_mb_rows);
    784 
    785         /* our internal buffers are always multiples of 16 */
    786         if ((width & 0xf) != 0)
    787             width += 16 - (width & 0xf);
    788 
    789         if (width < 640) pbi->sync_range = 1;
    790         else if (width <= 1280) pbi->sync_range = 8;
    791         else if (width <= 2560) pbi->sync_range =16;
    792         else pbi->sync_range = 32;
    793 
    794         uv_width = width >>1;
    795 
    796         /* Allocate an int for each mb row. */
    797         CALLOC_ARRAY(pbi->mt_current_mb_col, pc->mb_rows);
    798 
    799         /* Allocate memory for above_row buffers. */
    800         CALLOC_ARRAY(pbi->mt_yabove_row, pc->mb_rows);
    801         for (i = 0; i < pc->mb_rows; i++)
    802             CHECK_MEM_ERROR(pbi->mt_yabove_row[i], vpx_memalign(16,sizeof(unsigned char) * (width + (VP8BORDERINPIXELS<<1))));
    803 
    804         CALLOC_ARRAY(pbi->mt_uabove_row, pc->mb_rows);
    805         for (i = 0; i < pc->mb_rows; i++)
    806             CHECK_MEM_ERROR(pbi->mt_uabove_row[i], vpx_memalign(16,sizeof(unsigned char) * (uv_width + VP8BORDERINPIXELS)));
    807 
    808         CALLOC_ARRAY(pbi->mt_vabove_row, pc->mb_rows);
    809         for (i = 0; i < pc->mb_rows; i++)
    810             CHECK_MEM_ERROR(pbi->mt_vabove_row[i], vpx_memalign(16,sizeof(unsigned char) * (uv_width + VP8BORDERINPIXELS)));
    811 
    812         /* Allocate memory for left_col buffers. */
    813         CALLOC_ARRAY(pbi->mt_yleft_col, pc->mb_rows);
    814         for (i = 0; i < pc->mb_rows; i++)
    815             CHECK_MEM_ERROR(pbi->mt_yleft_col[i], vpx_calloc(sizeof(unsigned char) * 16, 1));
    816 
    817         CALLOC_ARRAY(pbi->mt_uleft_col, pc->mb_rows);
    818         for (i = 0; i < pc->mb_rows; i++)
    819             CHECK_MEM_ERROR(pbi->mt_uleft_col[i], vpx_calloc(sizeof(unsigned char) * 8, 1));
    820 
    821         CALLOC_ARRAY(pbi->mt_vleft_col, pc->mb_rows);
    822         for (i = 0; i < pc->mb_rows; i++)
    823             CHECK_MEM_ERROR(pbi->mt_vleft_col[i], vpx_calloc(sizeof(unsigned char) * 8, 1));
    824     }
    825 }
    826 
    827 
    828 void vp8_decoder_remove_threads(VP8D_COMP *pbi)
    829 {
    830     /* shutdown MB Decoding thread; */
    831     if (pbi->b_multithreaded_rd)
    832     {
    833         int i;
    834 
    835         pbi->b_multithreaded_rd = 0;
    836 
    837         /* allow all threads to exit */
    838         for (i = 0; i < pbi->allocated_decoding_thread_count; i++)
    839         {
    840             sem_post(&pbi->h_event_start_decoding[i]);
    841             pthread_join(pbi->h_decoding_thread[i], NULL);
    842         }
    843 
    844         for (i = 0; i < pbi->allocated_decoding_thread_count; i++)
    845         {
    846             sem_destroy(&pbi->h_event_start_decoding[i]);
    847         }
    848 
    849         sem_destroy(&pbi->h_event_end_decoding);
    850 
    851             vpx_free(pbi->h_decoding_thread);
    852             pbi->h_decoding_thread = NULL;
    853 
    854             vpx_free(pbi->h_event_start_decoding);
    855             pbi->h_event_start_decoding = NULL;
    856 
    857             vpx_free(pbi->mb_row_di);
    858             pbi->mb_row_di = NULL ;
    859 
    860             vpx_free(pbi->de_thread_data);
    861             pbi->de_thread_data = NULL;
    862     }
    863 }
    864 
    865 void vp8mt_decode_mb_rows( VP8D_COMP *pbi, MACROBLOCKD *xd)
    866 {
    867     VP8_COMMON *pc = &pbi->common;
    868     unsigned int i;
    869     int j;
    870 
    871     int filter_level = pc->filter_level;
    872     YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME];
    873 
    874     if (filter_level)
    875     {
    876         /* Set above_row buffer to 127 for decoding first MB row */
    877         vpx_memset(pbi->mt_yabove_row[0] + VP8BORDERINPIXELS-1, 127, yv12_fb_new->y_width + 5);
    878         vpx_memset(pbi->mt_uabove_row[0] + (VP8BORDERINPIXELS>>1)-1, 127, (yv12_fb_new->y_width>>1) +5);
    879         vpx_memset(pbi->mt_vabove_row[0] + (VP8BORDERINPIXELS>>1)-1, 127, (yv12_fb_new->y_width>>1) +5);
    880 
    881         for (j=1; j<pc->mb_rows; j++)
    882         {
    883             vpx_memset(pbi->mt_yabove_row[j] + VP8BORDERINPIXELS-1, (unsigned char)129, 1);
    884             vpx_memset(pbi->mt_uabove_row[j] + (VP8BORDERINPIXELS>>1)-1, (unsigned char)129, 1);
    885             vpx_memset(pbi->mt_vabove_row[j] + (VP8BORDERINPIXELS>>1)-1, (unsigned char)129, 1);
    886         }
    887 
    888         /* Set left_col to 129 initially */
    889         for (j=0; j<pc->mb_rows; j++)
    890         {
    891             vpx_memset(pbi->mt_yleft_col[j], (unsigned char)129, 16);
    892             vpx_memset(pbi->mt_uleft_col[j], (unsigned char)129, 8);
    893             vpx_memset(pbi->mt_vleft_col[j], (unsigned char)129, 8);
    894         }
    895 
    896         /* Initialize the loop filter for this frame. */
    897         vp8_loop_filter_frame_init(pc, &pbi->mb, filter_level);
    898     }
    899     else
    900         vp8_setup_intra_recon_top_line(yv12_fb_new);
    901 
    902     setup_decoding_thread_data(pbi, xd, pbi->mb_row_di, pbi->decoding_thread_count);
    903 
    904     for (i = 0; i < pbi->decoding_thread_count; i++)
    905         sem_post(&pbi->h_event_start_decoding[i]);
    906 
    907     mt_decode_mb_rows(pbi, xd, 0);
    908 
    909     sem_wait(&pbi->h_event_end_decoding);   /* add back for each frame */
    910 }
    911