threading.c 41.1 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9 10 11
 */


John Koleszar's avatar
John Koleszar committed
12 13
#include "vpx_config.h"
#include "vpx_rtcd.h"
14
#if !defined(WIN32) && CONFIG_OS_SUPPORT == 1
John Koleszar's avatar
John Koleszar committed
15 16 17 18
# include <unistd.h>
#endif
#include "onyxd_int.h"
#include "vpx_mem/vpx_mem.h"
John Koleszar's avatar
John Koleszar committed
19
#include "vp8/common/threading.h"
John Koleszar's avatar
John Koleszar committed
20

John Koleszar's avatar
John Koleszar committed
21 22
#include "vp8/common/loopfilter.h"
#include "vp8/common/extend.h"
John Koleszar's avatar
John Koleszar committed
23
#include "vpx_ports/vpx_timer.h"
24
#include "detokenize.h"
John Koleszar's avatar
John Koleszar committed
25
#include "vp8/common/reconinter.h"
26
#include "reconintra_mt.h"
27 28 29
#if CONFIG_ERROR_CONCEALMENT
#include "error_concealment.h"
#endif
John Koleszar's avatar
John Koleszar committed
30

31
extern void mb_init_dequantizer(VP8D_COMP *pbi, MACROBLOCKD *xd);
John Koleszar's avatar
John Koleszar committed
32

33
static void setup_decoding_thread_data(VP8D_COMP *pbi, MACROBLOCKD *xd, MB_ROW_DEC *mbrd, int count)
John Koleszar's avatar
John Koleszar committed
34 35
{
    VP8_COMMON *const pc = & pbi->common;
36
    int i;
John Koleszar's avatar
John Koleszar committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

    for (i = 0; i < count; i++)
    {
        MACROBLOCKD *mbd = &mbrd[i].mbd;
        mbd->subpixel_predict        = xd->subpixel_predict;
        mbd->subpixel_predict8x4     = xd->subpixel_predict8x4;
        mbd->subpixel_predict8x8     = xd->subpixel_predict8x8;
        mbd->subpixel_predict16x16   = xd->subpixel_predict16x16;

        mbd->mode_info_context = pc->mi   + pc->mode_info_stride * (i + 1);
        mbd->mode_info_stride  = pc->mode_info_stride;

        mbd->frame_type = pc->frame_type;
        mbd->frames_since_golden      = pc->frames_since_golden;
        mbd->frames_till_alt_ref_frame  = pc->frames_till_alt_ref_frame;

53 54
        mbd->pre = pc->yv12_fb[pc->lst_fb_idx];
        mbd->dst = pc->yv12_fb[pc->new_fb_idx];
John Koleszar's avatar
John Koleszar committed
55 56 57 58 59 60 61

        vp8_setup_block_dptrs(mbd);
        vp8_build_block_doffsets(mbd);
        mbd->segmentation_enabled    = xd->segmentation_enabled;
        mbd->mb_segement_abs_delta     = xd->mb_segement_abs_delta;
        vpx_memcpy(mbd->segment_feature_data, xd->segment_feature_data, sizeof(xd->segment_feature_data));

62
        /*signed char ref_lf_deltas[MAX_REF_LF_DELTAS];*/
63
        vpx_memcpy(mbd->ref_lf_deltas, xd->ref_lf_deltas, sizeof(xd->ref_lf_deltas));
64
        /*signed char mode_lf_deltas[MAX_MODE_LF_DELTAS];*/
65
        vpx_memcpy(mbd->mode_lf_deltas, xd->mode_lf_deltas, sizeof(xd->mode_lf_deltas));
66 67
        /*unsigned char mode_ref_lf_delta_enabled;
        unsigned char mode_ref_lf_delta_update;*/
68 69 70
        mbd->mode_ref_lf_delta_enabled    = xd->mode_ref_lf_delta_enabled;
        mbd->mode_ref_lf_delta_update    = xd->mode_ref_lf_delta_update;

John Koleszar's avatar
John Koleszar committed
71 72
        mbd->current_bc = &pbi->bc2;

73 74 75 76
        vpx_memcpy(mbd->dequant_y1_dc, xd->dequant_y1_dc, sizeof(xd->dequant_y1_dc));
        vpx_memcpy(mbd->dequant_y1, xd->dequant_y1, sizeof(xd->dequant_y1));
        vpx_memcpy(mbd->dequant_y2, xd->dequant_y2, sizeof(xd->dequant_y2));
        vpx_memcpy(mbd->dequant_uv, xd->dequant_uv, sizeof(xd->dequant_uv));
77 78 79 80 81

        mbd->fullpixel_mask = 0xffffffff;
        if(pc->full_pixel)
            mbd->fullpixel_mask = 0xfffffff8;

John Koleszar's avatar
John Koleszar committed
82 83
    }

84
    for (i=0; i< pc->mb_rows; i++)
85
        pbi->mt_current_mb_col[i]=-1;
John Koleszar's avatar
John Koleszar committed
86 87
}

88

89
static void decode_macroblock(VP8D_COMP *pbi, MACROBLOCKD *xd, int mb_row, int mb_col)
90
{
91
    int eobtotal = 0;
92
    int throw_residual = 0;
93
    int i;
94

95
    if (xd->mode_info_context->mbmi.mb_skip_coeff)
96
    {
97 98
        vp8_reset_mb_tokens_context(xd);
    }
99
    else if (!vp8dx_bool_error(xd->current_bc))
100 101 102
    {
        eobtotal = vp8_decode_mb_tokens(pbi, xd);
    }
103

104 105
    eobtotal |= (xd->mode_info_context->mbmi.mode == B_PRED ||
                  xd->mode_info_context->mbmi.mode == SPLITMV);
106
    if (!eobtotal && !vp8dx_bool_error(xd->current_bc))
107
    {
Scott LaVarnway's avatar
Scott LaVarnway committed
108 109 110 111 112
        /* Special case:  Force the loopfilter to skip when eobtotal and
         * mb_skip_coeff are zero.
         * */
        xd->mode_info_context->mbmi.mb_skip_coeff = 1;

113
        /*mt_skip_recon_mb(pbi, xd, mb_row, mb_col);*/
114
        if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
115 116 117 118 119 120
        {
            vp8mt_build_intra_predictors_mbuv_s(pbi, xd, mb_row, mb_col);
            vp8mt_build_intra_predictors_mby_s(pbi, xd, mb_row, mb_col);
        }
        else
        {
121 122 123
            vp8_build_inter16x16_predictors_mb(xd, xd->dst.y_buffer,
                                               xd->dst.u_buffer, xd->dst.v_buffer,
                                               xd->dst.y_stride, xd->dst.uv_stride);
124 125 126
        }
        return;
    }
127

128 129
    if (xd->segmentation_enabled)
        mb_init_dequantizer(pbi, xd);
130

131
    /* do prediction */
132
    if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
133
    {
134
        vp8mt_build_intra_predictors_mbuv_s(pbi, xd, mb_row, mb_col);
135 136 137

        if (xd->mode_info_context->mbmi.mode != B_PRED)
        {
138
            vp8mt_build_intra_predictors_mby_s(pbi, xd, mb_row, mb_col);
139 140 141 142 143 144 145 146
        } else {
            vp8mt_intra_prediction_down_copy(pbi, xd, mb_row, mb_col);
        }
    }
    else
    {
        vp8_build_inter_predictors_mb(xd);
    }
147

148 149 150 151 152 153 154
    /* When we have independent partitions we can apply residual even
     * though other partitions within the frame are corrupt.
     */
    throw_residual = (!pbi->independent_partitions &&
                      pbi->frame_corrupt_residual);
    throw_residual = (throw_residual || vp8dx_bool_error(xd->current_bc));

155
#if CONFIG_ERROR_CONCEALMENT
156
    if (pbi->ec_active &&
157
        (mb_row * pbi->common.mb_cols + mb_col >= pbi->mvs_corrupt_from_mb ||
158
         throw_residual))
159 160 161 162
    {
        /* MB with corrupt residuals or corrupt mode/motion vectors.
         * Better to use the predictor as reconstruction.
         */
163
        pbi->frame_corrupt_residual = 1;
164 165 166 167 168 169
        vpx_memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
        vp8_conceal_corrupt_mb(xd);
        return;
    }
#endif

170
    /* dequantization and idct */
171
    if (xd->mode_info_context->mbmi.mode == B_PRED)
172
    {
173 174
        short *DQC = xd->dequant_y1;

175 176 177
        for (i = 0; i < 16; i++)
        {
            BLOCKD *b = &xd->block[i];
178
            int b_mode = xd->mode_info_context->bmi[i].as_mode;
Scott LaVarnway's avatar
Scott LaVarnway committed
179

180 181
            vp8mt_predict_intra4x4(pbi, xd, b_mode, *(b->base_dst) + b->dst,
                                   b->dst_stride, mb_row, mb_col, i);
Scott LaVarnway's avatar
Scott LaVarnway committed
182

183 184 185 186
            if (xd->eobs[i] )
            {
                if (xd->eobs[i] > 1)
                {
John Koleszar's avatar
John Koleszar committed
187
                    vp8_dequant_idct_add
188
                        (b->qcoeff, DQC,
189 190 191 192
                        *(b->base_dst) + b->dst, b->dst_stride);
                }
                else
                {
193
                    vp8_dc_only_idct_add
194
                        (b->qcoeff[0] * DQC[0],
195 196 197 198 199 200 201 202 203
                        *(b->base_dst) + b->dst, b->dst_stride,
                        *(b->base_dst) + b->dst, b->dst_stride);
                    ((int *)b->qcoeff)[0] = 0;
                }
            }
        }
    }
    else
    {
204
        short *DQC = xd->dequant_y1;
205 206 207 208 209 210 211

        if (xd->mode_info_context->mbmi.mode != SPLITMV)
        {
            BLOCKD *b = &xd->block[24];

            /* do 2nd order transform on the dc block */
            if (xd->eobs[24] > 1)
212
            {
John Koleszar's avatar
John Koleszar committed
213
                vp8_dequantize_b(b, xd->dequant_y2);
214

215
                vp8_short_inv_walsh4x4(&b->dqcoeff[0],
216 217 218 219 220 221 222 223 224
                    xd->qcoeff);
                ((int *)b->qcoeff)[0] = 0;
                ((int *)b->qcoeff)[1] = 0;
                ((int *)b->qcoeff)[2] = 0;
                ((int *)b->qcoeff)[3] = 0;
                ((int *)b->qcoeff)[4] = 0;
                ((int *)b->qcoeff)[5] = 0;
                ((int *)b->qcoeff)[6] = 0;
                ((int *)b->qcoeff)[7] = 0;
225 226 227
            }
            else
            {
228
                b->dqcoeff[0] = b->qcoeff[0] * xd->dequant_y2[0];
229
                vp8_short_inv_walsh4x4_1(&b->dqcoeff[0], xd->qcoeff);
230 231
                ((int *)b->qcoeff)[0] = 0;
            }
232 233

            /* override the dc dequant constant */
234
            DQC = xd->dequant_y1_dc;
235
        }
236

John Koleszar's avatar
John Koleszar committed
237
        vp8_dequant_idct_add_y_block
238
                        (xd->qcoeff, DQC,
239
                         xd->dst.y_buffer,
240 241 242
                         xd->dst.y_stride, xd->eobs);
    }

John Koleszar's avatar
John Koleszar committed
243
    vp8_dequant_idct_add_uv_block
244
                    (xd->qcoeff+16*16, xd->dequant_uv,
245
                     xd->dst.u_buffer, xd->dst.v_buffer,
246
                     xd->dst.uv_stride, xd->eobs+16);
247
}
John Koleszar's avatar
John Koleszar committed
248

249
static THREAD_FUNCTION thread_decoding_proc(void *p_data)
John Koleszar's avatar
John Koleszar committed
250 251 252 253
{
    int ithread = ((DECODETHREAD_DATA *)p_data)->ithread;
    VP8D_COMP *pbi = (VP8D_COMP *)(((DECODETHREAD_DATA *)p_data)->ptr1);
    MB_ROW_DEC *mbrd = (MB_ROW_DEC *)(((DECODETHREAD_DATA *)p_data)->ptr2);
254
    ENTROPY_CONTEXT_PLANES mb_row_left_context;
John Koleszar's avatar
John Koleszar committed
255 256 257 258 259 260

    while (1)
    {
        if (pbi->b_multithreaded_rd == 0)
            break;

261
        /*if(WaitForSingleObject(pbi->h_event_start_decoding[ithread], INFINITE) == WAIT_OBJECT_0)*/
262
        if (sem_wait(&pbi->h_event_start_decoding[ithread]) == 0)
John Koleszar's avatar
John Koleszar committed
263 264 265 266 267 268 269 270
        {
            if (pbi->b_multithreaded_rd == 0)
                break;
            else
            {
                VP8_COMMON *pc = &pbi->common;
                MACROBLOCKD *xd = &mbrd->mbd;

271 272
                int mb_row;
                int num_part = 1 << pbi->common.multi_token_partition;
John Koleszar's avatar
John Koleszar committed
273
                volatile int *last_row_current_mb_col;
274
                int nsync = pbi->sync_range;
John Koleszar's avatar
John Koleszar committed
275

276
                for (mb_row = ithread+1; mb_row < pc->mb_rows; mb_row += (pbi->decoding_thread_count + 1))
John Koleszar's avatar
John Koleszar committed
277
                {
278 279 280 281 282 283 284 285
                    int i;
                    int recon_yoffset, recon_uvoffset;
                    int mb_col;
                    int ref_fb_idx = pc->lst_fb_idx;
                    int dst_fb_idx = pc->new_fb_idx;
                    int recon_y_stride = pc->yv12_fb[ref_fb_idx].y_stride;
                    int recon_uv_stride = pc->yv12_fb[ref_fb_idx].uv_stride;

286
                    int filter_level;
Attila Nagy's avatar
Attila Nagy committed
287
                    loop_filter_info_n *lfi_n = &pc->lf_info;
288

289 290 291
                    pbi->mb_row_di[ithread].mb_row = mb_row;
                    pbi->mb_row_di[ithread].mbd.current_bc =  &pbi->mbc[mb_row%num_part];

292
                    last_row_current_mb_col = &pbi->mt_current_mb_col[mb_row -1];
293 294 295

                    recon_yoffset = mb_row * recon_y_stride * 16;
                    recon_uvoffset = mb_row * recon_uv_stride * 8;
296
                    /* reset above block coeffs */
297

298 299 300
                    xd->above_context = pc->above_context;
                    xd->left_context = &mb_row_left_context;
                    vpx_memset(&mb_row_left_context, 0, sizeof(mb_row_left_context));
301 302 303 304 305 306
                    xd->up_available = (mb_row != 0);

                    xd->mb_to_top_edge = -((mb_row * 16)) << 3;
                    xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;

                    for (mb_col = 0; mb_col < pc->mb_cols; mb_col++)
John Koleszar's avatar
John Koleszar committed
307
                    {
308
                        if ((mb_col & (nsync-1)) == 0)
309
                        {
310
                            while (mb_col > (*last_row_current_mb_col - nsync) && *last_row_current_mb_col != pc->mb_cols - 1)
311 312 313 314 315
                            {
                                x86_pause_hint();
                                thread_sleep(0);
                            }
                        }
John Koleszar's avatar
John Koleszar committed
316

317 318 319
                        /* Distance of MB to the various image edges.
                         * These are specified to 8th pel as they are always
                         * compared to values that are in 1/8th pel units.
320
                         */
321 322
                        xd->mb_to_left_edge = -((mb_col * 16) << 3);
                        xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
John Koleszar's avatar
John Koleszar committed
323

324 325
#if CONFIG_ERROR_CONCEALMENT
                        {
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
                            int corrupt_residual =
                                        (!pbi->independent_partitions &&
                                        pbi->frame_corrupt_residual) ||
                                        vp8dx_bool_error(xd->current_bc);
                            if (pbi->ec_active &&
                                (xd->mode_info_context->mbmi.ref_frame ==
                                                                 INTRA_FRAME) &&
                                corrupt_residual)
                            {
                                /* We have an intra block with corrupt
                                 * coefficients, better to conceal with an inter
                                 * block.
                                 * Interpolate MVs from neighboring MBs
                                 *
                                 * Note that for the first mb with corrupt
                                 * residual in a frame, we might not discover
                                 * that before decoding the residual. That
                                 * happens after this check, and therefore no
                                 * inter concealment will be done.
                                 */
                                vp8_interpolate_motion(xd,
                                                       mb_row, mb_col,
                                                       pc->mb_rows, pc->mb_cols,
                                                       pc->mode_info_stride);
                            }
351 352 353 354
                        }
#endif


355 356 357
                        xd->dst.y_buffer = pc->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
                        xd->dst.u_buffer = pc->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
                        xd->dst.v_buffer = pc->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
John Koleszar's avatar
John Koleszar committed
358

359
                        xd->left_available = (mb_col != 0);
360

361
                        /* Select the appropriate reference frame for this MB */
362
                        if (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME)
363
                            ref_fb_idx = pc->lst_fb_idx;
364
                        else if (xd->mode_info_context->mbmi.ref_frame == GOLDEN_FRAME)
365 366 367
                            ref_fb_idx = pc->gld_fb_idx;
                        else
                            ref_fb_idx = pc->alt_fb_idx;
John Koleszar's avatar
John Koleszar committed
368

369 370 371
                        xd->pre.y_buffer = pc->yv12_fb[ref_fb_idx].y_buffer + recon_yoffset;
                        xd->pre.u_buffer = pc->yv12_fb[ref_fb_idx].u_buffer + recon_uvoffset;
                        xd->pre.v_buffer = pc->yv12_fb[ref_fb_idx].v_buffer + recon_uvoffset;
John Koleszar's avatar
John Koleszar committed
372

373 374 375 376 377 378 379
                        if (xd->mode_info_context->mbmi.ref_frame !=
                                INTRA_FRAME)
                        {
                            /* propagate errors from reference frames */
                            xd->corrupted |= pc->yv12_fb[ref_fb_idx].corrupted;
                        }

380
                        decode_macroblock(pbi, xd, mb_row, mb_col);
John Koleszar's avatar
John Koleszar committed
381

382 383 384
                        /* check if the boolean decoder has suffered an error */
                        xd->corrupted |= vp8dx_bool_error(xd->current_bc);

385 386
                        if (pbi->common.filter_level)
                        {
Attila Nagy's avatar
Attila Nagy committed
387 388 389 390 391 392 393 394 395 396
                            int skip_lf = (xd->mode_info_context->mbmi.mode != B_PRED &&
                                            xd->mode_info_context->mbmi.mode != SPLITMV &&
                                            xd->mode_info_context->mbmi.mb_skip_coeff);

                            const int mode_index = lfi_n->mode_lf_lut[xd->mode_info_context->mbmi.mode];
                            const int seg = xd->mode_info_context->mbmi.segment_id;
                            const int ref_frame = xd->mode_info_context->mbmi.ref_frame;

                            filter_level = lfi_n->lvl[seg][ref_frame][mode_index];

397 398
                            if( mb_row != pc->mb_rows-1 )
                            {
399
                                /* Save decoded MB last row data for next-row decoding */
400 401 402 403 404
                                vpx_memcpy((pbi->mt_yabove_row[mb_row + 1] + 32 + mb_col*16), (xd->dst.y_buffer + 15 * recon_y_stride), 16);
                                vpx_memcpy((pbi->mt_uabove_row[mb_row + 1] + 16 + mb_col*8), (xd->dst.u_buffer + 7 * recon_uv_stride), 8);
                                vpx_memcpy((pbi->mt_vabove_row[mb_row + 1] + 16 + mb_col*8), (xd->dst.v_buffer + 7 * recon_uv_stride), 8);
                            }

405
                            /* save left_col for next MB decoding */
406 407 408 409
                            if(mb_col != pc->mb_cols-1)
                            {
                                MODE_INFO *next = xd->mode_info_context +1;

410
                                if (next->mbmi.ref_frame == INTRA_FRAME)
411 412 413 414 415 416 417 418 419 420 421
                                {
                                    for (i = 0; i < 16; i++)
                                        pbi->mt_yleft_col[mb_row][i] = xd->dst.y_buffer [i* recon_y_stride + 15];
                                    for (i = 0; i < 8; i++)
                                    {
                                        pbi->mt_uleft_col[mb_row][i] = xd->dst.u_buffer [i* recon_uv_stride + 7];
                                        pbi->mt_vleft_col[mb_row][i] = xd->dst.v_buffer [i* recon_uv_stride + 7];
                                    }
                                }
                            }

Ralph Giles's avatar
Ralph Giles committed
422
                            /* loopfilter on this macroblock. */
423 424
                            if (filter_level)
                            {
Attila Nagy's avatar
Attila Nagy committed
425 426 427 428 429 430 431 432 433 434 435
                                if(pc->filter_type == NORMAL_LOOPFILTER)
                                {
                                    loop_filter_info lfi;
                                    FRAME_TYPE frame_type = pc->frame_type;
                                    const int hev_index = lfi_n->hev_thr_lut[frame_type][filter_level];
                                    lfi.mblim = lfi_n->mblim[filter_level];
                                    lfi.blim = lfi_n->blim[filter_level];
                                    lfi.lim = lfi_n->lim[filter_level];
                                    lfi.hev_thr = lfi_n->hev_thr[hev_index];

                                    if (mb_col > 0)
John Koleszar's avatar
John Koleszar committed
436
                                        vp8_loop_filter_mbv
Attila Nagy's avatar
Attila Nagy committed
437 438 439
                                        (xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi);

                                    if (!skip_lf)
John Koleszar's avatar
John Koleszar committed
440
                                        vp8_loop_filter_bv
Attila Nagy's avatar
Attila Nagy committed
441 442 443 444
                                        (xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi);

                                    /* don't apply across umv border */
                                    if (mb_row > 0)
John Koleszar's avatar
John Koleszar committed
445
                                        vp8_loop_filter_mbh
Attila Nagy's avatar
Attila Nagy committed
446 447 448
                                        (xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi);

                                    if (!skip_lf)
John Koleszar's avatar
John Koleszar committed
449
                                        vp8_loop_filter_bh
Attila Nagy's avatar
Attila Nagy committed
450 451 452 453 454
                                        (xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer,  recon_y_stride, recon_uv_stride, &lfi);
                                }
                                else
                                {
                                    if (mb_col > 0)
John Koleszar's avatar
John Koleszar committed
455
                                        vp8_loop_filter_simple_mbv
Attila Nagy's avatar
Attila Nagy committed
456 457 458
                                        (xd->dst.y_buffer, recon_y_stride, lfi_n->mblim[filter_level]);

                                    if (!skip_lf)
John Koleszar's avatar
John Koleszar committed
459
                                        vp8_loop_filter_simple_bv
Attila Nagy's avatar
Attila Nagy committed
460 461 462 463
                                        (xd->dst.y_buffer, recon_y_stride, lfi_n->blim[filter_level]);

                                    /* don't apply across umv border */
                                    if (mb_row > 0)
John Koleszar's avatar
John Koleszar committed
464
                                        vp8_loop_filter_simple_mbh
Attila Nagy's avatar
Attila Nagy committed
465 466 467
                                        (xd->dst.y_buffer, recon_y_stride, lfi_n->mblim[filter_level]);

                                    if (!skip_lf)
John Koleszar's avatar
John Koleszar committed
468
                                        vp8_loop_filter_simple_bh
Attila Nagy's avatar
Attila Nagy committed
469 470
                                        (xd->dst.y_buffer, recon_y_stride, lfi_n->blim[filter_level]);
                                }
471
                            }
Attila Nagy's avatar
Attila Nagy committed
472

473
                        }
John Koleszar's avatar
John Koleszar committed
474

475 476
                        recon_yoffset += 16;
                        recon_uvoffset += 8;
John Koleszar's avatar
John Koleszar committed
477

478
                        ++xd->mode_info_context;  /* next mb */
John Koleszar's avatar
John Koleszar committed
479

480
                        xd->above_context++;
John Koleszar's avatar
John Koleszar committed
481

482
                        /*pbi->mb_row_di[ithread].current_mb_col = mb_col;*/
483
                        pbi->mt_current_mb_col[mb_row] = mb_col;
484
                    }
John Koleszar's avatar
John Koleszar committed
485

486
                    /* adjust to the next row of mbs */
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
                    if (pbi->common.filter_level)
                    {
                        if(mb_row != pc->mb_rows-1)
                        {
                            int lasty = pc->yv12_fb[ref_fb_idx].y_width + VP8BORDERINPIXELS;
                            int lastuv = (pc->yv12_fb[ref_fb_idx].y_width>>1) + (VP8BORDERINPIXELS>>1);

                            for (i = 0; i < 4; i++)
                            {
                                pbi->mt_yabove_row[mb_row +1][lasty + i] = pbi->mt_yabove_row[mb_row +1][lasty -1];
                                pbi->mt_uabove_row[mb_row +1][lastuv + i] = pbi->mt_uabove_row[mb_row +1][lastuv -1];
                                pbi->mt_vabove_row[mb_row +1][lastuv + i] = pbi->mt_vabove_row[mb_row +1][lastuv -1];
                            }
                        }
                    } else
                        vp8_extend_mb_row(&pc->yv12_fb[dst_fb_idx], xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
John Koleszar's avatar
John Koleszar committed
503

504
                    ++xd->mode_info_context;      /* skip prediction column */
John Koleszar's avatar
John Koleszar committed
505

506
                    /* since we have multithread */
507
                    xd->mode_info_context += xd->mode_info_stride * pbi->decoding_thread_count;
John Koleszar's avatar
John Koleszar committed
508 509 510
                }
            }
        }
511
        /*  add this to each frame */
512 513
        if ((mbrd->mb_row == pbi->common.mb_rows-1) || ((mbrd->mb_row == pbi->common.mb_rows-2) && (pbi->common.mb_rows % (pbi->decoding_thread_count+1))==1))
        {
514
            /*SetEvent(pbi->h_event_end_decoding);*/
515 516 517
            sem_post(&pbi->h_event_end_decoding);
        }
    }
John Koleszar's avatar
John Koleszar committed
518 519 520 521

    return 0 ;
}

522

523
void vp8_decoder_create_threads(VP8D_COMP *pbi)
John Koleszar's avatar
John Koleszar committed
524
{
525 526
    int core_count = 0;
    int ithread;
John Koleszar's avatar
John Koleszar committed
527

528 529
    pbi->b_multithreaded_rd = 0;
    pbi->allocated_decoding_thread_count = 0;
530 531 532 533 534 535 536

    /* limit decoding threads to the max number of token partitions */
    core_count = (pbi->max_threads > 8) ? 8 : pbi->max_threads;

    /* limit decoding threads to the available cores */
    if (core_count > pbi->common.processor_core_count)
        core_count = pbi->common.processor_core_count;
John Koleszar's avatar
John Koleszar committed
537

538 539 540
    if (core_count > 1)
    {
        pbi->b_multithreaded_rd = 1;
541
        pbi->decoding_thread_count = core_count - 1;
John Koleszar's avatar
John Koleszar committed
542

543 544 545 546 547
        CHECK_MEM_ERROR(pbi->h_decoding_thread, vpx_malloc(sizeof(pthread_t) * pbi->decoding_thread_count));
        CHECK_MEM_ERROR(pbi->h_event_start_decoding, vpx_malloc(sizeof(sem_t) * pbi->decoding_thread_count));
        CHECK_MEM_ERROR(pbi->mb_row_di, vpx_memalign(32, sizeof(MB_ROW_DEC) * pbi->decoding_thread_count));
        vpx_memset(pbi->mb_row_di, 0, sizeof(MB_ROW_DEC) * pbi->decoding_thread_count);
        CHECK_MEM_ERROR(pbi->de_thread_data, vpx_malloc(sizeof(DECODETHREAD_DATA) * pbi->decoding_thread_count));
John Koleszar's avatar
John Koleszar committed
548

549 550 551
        for (ithread = 0; ithread < pbi->decoding_thread_count; ithread++)
        {
            sem_init(&pbi->h_event_start_decoding[ithread], 0, 0);
John Koleszar's avatar
John Koleszar committed
552

553 554 555
            pbi->de_thread_data[ithread].ithread  = ithread;
            pbi->de_thread_data[ithread].ptr1     = (void *)pbi;
            pbi->de_thread_data[ithread].ptr2     = (void *) &pbi->mb_row_di[ithread];
John Koleszar's avatar
John Koleszar committed
556

557
            pthread_create(&pbi->h_decoding_thread[ithread], 0, thread_decoding_proc, (&pbi->de_thread_data[ithread]));
558
        }
559

560
        sem_init(&pbi->h_event_end_decoding, 0, 0);
John Koleszar's avatar
John Koleszar committed
561

562 563 564
        pbi->allocated_decoding_thread_count = pbi->decoding_thread_count;
    }
}
John Koleszar's avatar
John Koleszar committed
565 566


567 568 569
void vp8mt_de_alloc_temp_buffers(VP8D_COMP *pbi, int mb_rows)
{
    int i;
John Koleszar's avatar
John Koleszar committed
570

571 572 573 574
    if (pbi->b_multithreaded_rd)
    {
            vpx_free(pbi->mt_current_mb_col);
            pbi->mt_current_mb_col = NULL ;
John Koleszar's avatar
John Koleszar committed
575

576
        /* Free above_row buffers. */
577 578 579 580 581 582 583 584 585 586
        if (pbi->mt_yabove_row)
        {
            for (i=0; i< mb_rows; i++)
            {
                    vpx_free(pbi->mt_yabove_row[i]);
                    pbi->mt_yabove_row[i] = NULL ;
            }
            vpx_free(pbi->mt_yabove_row);
            pbi->mt_yabove_row = NULL ;
        }
John Koleszar's avatar
John Koleszar committed
587

588 589 590 591 592 593 594 595 596 597
        if (pbi->mt_uabove_row)
        {
            for (i=0; i< mb_rows; i++)
            {
                    vpx_free(pbi->mt_uabove_row[i]);
                    pbi->mt_uabove_row[i] = NULL ;
            }
            vpx_free(pbi->mt_uabove_row);
            pbi->mt_uabove_row = NULL ;
        }
John Koleszar's avatar
John Koleszar committed
598

599 600 601 602 603 604 605 606 607 608
        if (pbi->mt_vabove_row)
        {
            for (i=0; i< mb_rows; i++)
            {
                    vpx_free(pbi->mt_vabove_row[i]);
                    pbi->mt_vabove_row[i] = NULL ;
            }
            vpx_free(pbi->mt_vabove_row);
            pbi->mt_vabove_row = NULL ;
        }
John Koleszar's avatar
John Koleszar committed
609

610
        /* Free left_col buffers. */
611 612 613 614 615 616 617 618 619 620
        if (pbi->mt_yleft_col)
        {
            for (i=0; i< mb_rows; i++)
            {
                    vpx_free(pbi->mt_yleft_col[i]);
                    pbi->mt_yleft_col[i] = NULL ;
            }
            vpx_free(pbi->mt_yleft_col);
            pbi->mt_yleft_col = NULL ;
        }
John Koleszar's avatar
John Koleszar committed
621

622 623 624 625 626 627
        if (pbi->mt_uleft_col)
        {
            for (i=0; i< mb_rows; i++)
            {
                    vpx_free(pbi->mt_uleft_col[i]);
                    pbi->mt_uleft_col[i] = NULL ;
628
            }
629 630
            vpx_free(pbi->mt_uleft_col);
            pbi->mt_uleft_col = NULL ;
John Koleszar's avatar
John Koleszar committed
631 632
        }

633
        if (pbi->mt_vleft_col)
634
        {
635 636 637 638 639 640 641
            for (i=0; i< mb_rows; i++)
            {
                    vpx_free(pbi->mt_vleft_col[i]);
                    pbi->mt_vleft_col[i] = NULL ;
            }
            vpx_free(pbi->mt_vleft_col);
            pbi->mt_vleft_col = NULL ;
642
        }
643
    }
John Koleszar's avatar
John Koleszar committed
644 645
}

646

647
void vp8mt_alloc_temp_buffers(VP8D_COMP *pbi, int width, int prev_mb_rows)
John Koleszar's avatar
John Koleszar committed
648
{
649 650 651
    VP8_COMMON *const pc = & pbi->common;
    int i;
    int uv_width;
John Koleszar's avatar
John Koleszar committed
652

653
    if (pbi->b_multithreaded_rd)
John Koleszar's avatar
John Koleszar committed
654
    {
655
        vp8mt_de_alloc_temp_buffers(pbi, prev_mb_rows);
John Koleszar's avatar
John Koleszar committed
656

657
        /* our internal buffers are always multiples of 16 */
658 659
        if ((width & 0xf) != 0)
            width += 16 - (width & 0xf);
John Koleszar's avatar
John Koleszar committed
660

661 662 663 664 665
        if (width < 640) pbi->sync_range = 1;
        else if (width <= 1280) pbi->sync_range = 8;
        else if (width <= 2560) pbi->sync_range =16;
        else pbi->sync_range = 32;

666
        uv_width = width >>1;
667

668
        /* Allocate an int for each mb row. */
669
        CHECK_MEM_ERROR(pbi->mt_current_mb_col, vpx_malloc(sizeof(int) * pc->mb_rows));
John Koleszar's avatar
John Koleszar committed
670

671
        /* Allocate memory for above_row buffers. */
672 673 674
        CHECK_MEM_ERROR(pbi->mt_yabove_row, vpx_malloc(sizeof(unsigned char *) * pc->mb_rows));
        for (i=0; i< pc->mb_rows; i++)
            CHECK_MEM_ERROR(pbi->mt_yabove_row[i], vpx_calloc(sizeof(unsigned char) * (width + (VP8BORDERINPIXELS<<1)), 1));
John Koleszar's avatar
John Koleszar committed
675

676 677 678
        CHECK_MEM_ERROR(pbi->mt_uabove_row, vpx_malloc(sizeof(unsigned char *) * pc->mb_rows));
        for (i=0; i< pc->mb_rows; i++)
            CHECK_MEM_ERROR(pbi->mt_uabove_row[i], vpx_calloc(sizeof(unsigned char) * (uv_width + VP8BORDERINPIXELS), 1));
John Koleszar's avatar
John Koleszar committed
679

680 681 682
        CHECK_MEM_ERROR(pbi->mt_vabove_row, vpx_malloc(sizeof(unsigned char *) * pc->mb_rows));
        for (i=0; i< pc->mb_rows; i++)
            CHECK_MEM_ERROR(pbi->mt_vabove_row[i], vpx_calloc(sizeof(unsigned char) * (uv_width + VP8BORDERINPIXELS), 1));
683

684
        /* Allocate memory for left_col buffers. */
685 686 687
        CHECK_MEM_ERROR(pbi->mt_yleft_col, vpx_malloc(sizeof(unsigned char *) * pc->mb_rows));
        for (i=0; i< pc->mb_rows; i++)
            CHECK_MEM_ERROR(pbi->mt_yleft_col[i], vpx_calloc(sizeof(unsigned char) * 16, 1));
John Koleszar's avatar
John Koleszar committed
688

689 690 691 692 693 694 695 696
        CHECK_MEM_ERROR(pbi->mt_uleft_col, vpx_malloc(sizeof(unsigned char *) * pc->mb_rows));
        for (i=0; i< pc->mb_rows; i++)
            CHECK_MEM_ERROR(pbi->mt_uleft_col[i], vpx_calloc(sizeof(unsigned char) * 8, 1));

        CHECK_MEM_ERROR(pbi->mt_vleft_col, vpx_malloc(sizeof(unsigned char *) * pc->mb_rows));
        for (i=0; i< pc->mb_rows; i++)
            CHECK_MEM_ERROR(pbi->mt_vleft_col[i], vpx_calloc(sizeof(unsigned char) * 8, 1));
    }
John Koleszar's avatar
John Koleszar committed
697 698
}

699

John Koleszar's avatar
John Koleszar committed
700 701
void vp8_decoder_remove_threads(VP8D_COMP *pbi)
{
702
    /* shutdown MB Decoding thread; */
John Koleszar's avatar
John Koleszar committed
703 704
    if (pbi->b_multithreaded_rd)
    {
705 706
        int i;

John Koleszar's avatar
John Koleszar committed
707
        pbi->b_multithreaded_rd = 0;
708

709
        /* allow all threads to exit */
710
        for (i = 0; i < pbi->allocated_decoding_thread_count; i++)
John Koleszar's avatar
John Koleszar committed
711
        {
712 713
            sem_post(&pbi->h_event_start_decoding[i]);
            pthread_join(pbi->h_decoding_thread[i], NULL);
John Koleszar's avatar
John Koleszar committed
714 715
        }

716 717 718
        for (i = 0; i < pbi->allocated_decoding_thread_count; i++)
        {
            sem_destroy(&pbi->h_event_start_decoding[i]);
John Koleszar's avatar
John Koleszar committed
719 720
        }

721
        sem_destroy(&pbi->h_event_end_decoding);
John Koleszar's avatar
John Koleszar committed
722 723 724 725

            vpx_free(pbi->h_decoding_thread);
            pbi->h_decoding_thread = NULL;

726 727 728
            vpx_free(pbi->h_event_start_decoding);
            pbi->h_event_start_decoding = NULL;

John Koleszar's avatar
John Koleszar committed
729 730 731 732 733
            vpx_free(pbi->mb_row_di);
            pbi->mb_row_di = NULL ;

            vpx_free(pbi->de_thread_data);
            pbi->de_thread_data = NULL;
734
    }
John Koleszar's avatar
John Koleszar committed
735 736
}

737
void vp8mt_decode_mb_rows( VP8D_COMP *pbi, MACROBLOCKD *xd)
John Koleszar's avatar
John Koleszar committed
738 739 740 741 742
{
    int mb_row;
    VP8_COMMON *pc = &pbi->common;

    int num_part = 1 << pbi->common.multi_token_partition;
Henrik Lundin's avatar
Henrik Lundin committed
743
    int i;
744
    volatile int *last_row_current_mb_col = NULL;
745
    int nsync = pbi->sync_range;
John Koleszar's avatar
John Koleszar committed
746

Attila Nagy's avatar
Attila Nagy committed
747 748
    int filter_level = pc->filter_level;
    loop_filter_info_n *lfi_n = &pc->lf_info;
749

Attila Nagy's avatar
Attila Nagy committed
750
    if (filter_level)
751
    {
752
        /* Set above_row buffer to 127 for decoding first MB row */
753 754 755 756 757 758 759 760 761 762 763
        vpx_memset(pbi->mt_yabove_row[0] + VP8BORDERINPIXELS-1, 127, pc->yv12_fb[pc->lst_fb_idx].y_width + 5);
        vpx_memset(pbi->mt_uabove_row[0] + (VP8BORDERINPIXELS>>1)-1, 127, (pc->yv12_fb[pc->lst_fb_idx].y_width>>1) +5);
        vpx_memset(pbi->mt_vabove_row[0] + (VP8BORDERINPIXELS>>1)-1, 127, (pc->yv12_fb[pc->lst_fb_idx].y_width>>1) +5);

        for (i=1; i<pc->mb_rows; i++)
        {
            vpx_memset(pbi->mt_yabove_row[i] + VP8BORDERINPIXELS-1, (unsigned char)129, 1);
            vpx_memset(pbi->mt_uabove_row[i] + (VP8BORDERINPIXELS>>1)-1, (unsigned char)129, 1);
            vpx_memset(pbi->mt_vabove_row[i] + (VP8BORDERINPIXELS>>1)-1, (unsigned char)129, 1);
        }

764
        /* Set left_col to 129 initially */
765 766 767 768 769 770
        for (i=0; i<pc->mb_rows; i++)
        {
            vpx_memset(pbi->mt_yleft_col[i], (unsigned char)129, 16);
            vpx_memset(pbi->mt_uleft_col[i], (unsigned char)129, 8);
            vpx_memset(pbi->mt_vleft_col[i], (unsigned char)129, 8);
        }
Attila Nagy's avatar
Attila Nagy committed
771 772

        /* Initialize the loop filter for this frame. */
Johann's avatar
Johann committed
773
        vp8_loop_filter_frame_init(pc, &pbi->mb, filter_level);
774 775
    }

776
    setup_decoding_thread_data(pbi, xd, pbi->mb_row_di, pbi->decoding_thread_count);
John Koleszar's avatar
John Koleszar committed
777

778 779 780
    for (i = 0; i < pbi->decoding_thread_count; i++)
        sem_post(&pbi->h_event_start_decoding[i]);

John Koleszar's avatar
John Koleszar committed
781 782
    for (mb_row = 0; mb_row < pc->mb_rows; mb_row += (pbi->decoding_thread_count + 1))
    {
783
        xd->current_bc = &pbi->mbc[mb_row%num_part];
John Koleszar's avatar
John Koleszar committed
784

785
        /* vp8_decode_mb_row(pbi, pc, mb_row, xd); */
John Koleszar's avatar
John Koleszar committed
786
        {
787 788 789 790 791 792 793 794
            int i;
            int recon_yoffset, recon_uvoffset;
            int mb_col;
            int ref_fb_idx = pc->lst_fb_idx;
            int dst_fb_idx = pc->new_fb_idx;
            int recon_y_stride = pc->yv12_fb[ref_fb_idx].y_stride;
            int recon_uv_stride = pc->yv12_fb[ref_fb_idx].uv_stride;

795
           /* volatile int *last_row_current_mb_col = NULL; */
796
            if (mb_row > 0)
797
                last_row_current_mb_col = &pbi->mt_current_mb_col[mb_row -1];
798

799
            vpx_memset(&pc->left_context, 0, sizeof(pc->left_context));
800 801
            recon_yoffset = mb_row * recon_y_stride * 16;
            recon_uvoffset = mb_row * recon_uv_stride * 8;
802
            /* reset above block coeffs */
803

804
            xd->above_context = pc->above_context;
805 806 807 808 809 810 811
            xd->up_available = (mb_row != 0);

            xd->mb_to_top_edge = -((mb_row * 16)) << 3;
            xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;

            for (mb_col = 0; mb_col < pc->mb_cols; mb_col++)
            {
812 813
                if ( mb_row > 0 && (mb_col & (nsync-1)) == 0){
                    while (mb_col > (*last_row_current_mb_col - nsync) && *last_row_current_mb_col != pc->mb_cols - 1)
814 815 816 817
                    {
                        x86_pause_hint();
                        thread_sleep(0);
                    }
818
                }
John Koleszar's avatar
John Koleszar committed
819

820 821 822
                /* Distance of MB to the various image edges.
                 * These are specified to 8th pel as they are always compared to
                 * values that are in 1/8th pel units.
823
                 */
824 825 826
                xd->mb_to_left_edge = -((mb_col * 16) << 3);
                xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;

827 828
#if CONFIG_ERROR_CONCEALMENT
                {
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
                    int corrupt_residual = (!pbi->independent_partitions &&
                                            pbi->frame_corrupt_residual) ||
                                            vp8dx_bool_error(xd->current_bc);
                    if (pbi->ec_active &&
                        (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME) &&
                        corrupt_residual)
                    {
                        /* We have an intra block with corrupt coefficients,
                         * better to conceal with an inter block. Interpolate
                         * MVs from neighboring MBs
                         *
                         * Note that for the first mb with corrupt residual in a
                         * frame, we might not discover that before decoding the
                         * residual. That happens after this check, and
                         * therefore no inter concealment will be done.
                         */
                        vp8_interpolate_motion(xd,
                                               mb_row, mb_col,
                                               pc->mb_rows, pc->mb_cols,
                                               pc->mode_info_stride);
                    }
850 851 852 853
                }
#endif


854 855 856 857 858 859
                xd->dst.y_buffer = pc->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
                xd->dst.u_buffer = pc->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
                xd->dst.v_buffer = pc->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;

                xd->left_available = (mb_col != 0);

860
                /* Select the appropriate reference frame for this MB */
861
                if (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME)
862
                    ref_fb_idx = pc->lst_fb_idx;
863
                else if (xd->mode_info_context->mbmi.ref_frame == GOLDEN_FRAME)
864 865 866 867 868 869 870 871
                    ref_fb_idx = pc->gld_fb_idx;
                else
                    ref_fb_idx = pc->alt_fb_idx;

                xd->pre.y_buffer = pc->yv12_fb[ref_fb_idx].y_buffer + recon_yoffset;
                xd->pre.u_buffer = pc->yv12_fb[ref_fb_idx].u_buffer + recon_uvoffset;
                xd->pre.v_buffer = pc->yv12_fb[ref_fb_idx].v_buffer + recon_uvoffset;

872 873 874 875 876 877
                if (xd->mode_info_context->mbmi.ref_frame != INTRA_FRAME)
                {
                    /* propagate errors from reference frames */
                    xd->corrupted |= pc->yv12_fb[ref_fb_idx].corrupted;
                }

878
                decode_macroblock(pbi, xd, mb_row, mb_col);
879

880 881 882
                /* check if the boolean decoder has suffered an error */
                xd->corrupted |= vp8dx_bool_error(xd->current_bc);