michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: michael@0: #include "vpx_config.h" michael@0: #include "vp8_rtcd.h" michael@0: #if !defined(WIN32) && CONFIG_OS_SUPPORT == 1 michael@0: # include michael@0: #endif michael@0: #include "onyxd_int.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: #include "vp8/common/threading.h" michael@0: michael@0: #include "vp8/common/loopfilter.h" michael@0: #include "vp8/common/extend.h" michael@0: #include "vpx_ports/vpx_timer.h" michael@0: #include "detokenize.h" michael@0: #include "vp8/common/reconintra4x4.h" michael@0: #include "vp8/common/reconinter.h" michael@0: #include "vp8/common/setupintrarecon.h" michael@0: #if CONFIG_ERROR_CONCEALMENT michael@0: #include "error_concealment.h" michael@0: #endif michael@0: michael@0: #define CALLOC_ARRAY(p, n) CHECK_MEM_ERROR((p), vpx_calloc(sizeof(*(p)), (n))) michael@0: #define CALLOC_ARRAY_ALIGNED(p, n, algn) do { \ michael@0: CHECK_MEM_ERROR((p), vpx_memalign((algn), sizeof(*(p)) * (n))); \ michael@0: memset((p), 0, (n) * sizeof(*(p))); \ michael@0: } while (0) michael@0: michael@0: michael@0: void vp8_mb_init_dequantizer(VP8D_COMP *pbi, MACROBLOCKD *xd); michael@0: michael@0: static void setup_decoding_thread_data(VP8D_COMP *pbi, MACROBLOCKD *xd, MB_ROW_DEC *mbrd, int count) michael@0: { michael@0: VP8_COMMON *const pc = & pbi->common; michael@0: int i; michael@0: michael@0: for (i = 0; i < count; i++) michael@0: { michael@0: MACROBLOCKD *mbd = &mbrd[i].mbd; michael@0: mbd->subpixel_predict = xd->subpixel_predict; michael@0: mbd->subpixel_predict8x4 = xd->subpixel_predict8x4; michael@0: mbd->subpixel_predict8x8 = xd->subpixel_predict8x8; michael@0: mbd->subpixel_predict16x16 = xd->subpixel_predict16x16; michael@0: michael@0: mbd->mode_info_context = pc->mi + pc->mode_info_stride * (i + 1); michael@0: mbd->mode_info_stride = pc->mode_info_stride; michael@0: michael@0: mbd->frame_type = pc->frame_type; michael@0: mbd->pre = xd->pre; michael@0: mbd->dst = xd->dst; michael@0: michael@0: mbd->segmentation_enabled = xd->segmentation_enabled; michael@0: mbd->mb_segement_abs_delta = xd->mb_segement_abs_delta; michael@0: vpx_memcpy(mbd->segment_feature_data, xd->segment_feature_data, sizeof(xd->segment_feature_data)); michael@0: michael@0: /*signed char ref_lf_deltas[MAX_REF_LF_DELTAS];*/ michael@0: vpx_memcpy(mbd->ref_lf_deltas, xd->ref_lf_deltas, sizeof(xd->ref_lf_deltas)); michael@0: /*signed char mode_lf_deltas[MAX_MODE_LF_DELTAS];*/ michael@0: vpx_memcpy(mbd->mode_lf_deltas, xd->mode_lf_deltas, sizeof(xd->mode_lf_deltas)); michael@0: /*unsigned char mode_ref_lf_delta_enabled; michael@0: unsigned char mode_ref_lf_delta_update;*/ michael@0: mbd->mode_ref_lf_delta_enabled = xd->mode_ref_lf_delta_enabled; michael@0: mbd->mode_ref_lf_delta_update = xd->mode_ref_lf_delta_update; michael@0: michael@0: mbd->current_bc = &pbi->mbc[0]; michael@0: michael@0: vpx_memcpy(mbd->dequant_y1_dc, xd->dequant_y1_dc, sizeof(xd->dequant_y1_dc)); michael@0: vpx_memcpy(mbd->dequant_y1, xd->dequant_y1, sizeof(xd->dequant_y1)); michael@0: vpx_memcpy(mbd->dequant_y2, xd->dequant_y2, sizeof(xd->dequant_y2)); michael@0: vpx_memcpy(mbd->dequant_uv, xd->dequant_uv, sizeof(xd->dequant_uv)); michael@0: michael@0: mbd->fullpixel_mask = 0xffffffff; michael@0: michael@0: if (pc->full_pixel) michael@0: mbd->fullpixel_mask = 0xfffffff8; michael@0: michael@0: } michael@0: michael@0: for (i = 0; i < pc->mb_rows; i++) michael@0: pbi->mt_current_mb_col[i] = -1; michael@0: } michael@0: michael@0: static void mt_decode_macroblock(VP8D_COMP *pbi, MACROBLOCKD *xd, michael@0: unsigned int mb_idx) michael@0: { michael@0: MB_PREDICTION_MODE mode; michael@0: int i; michael@0: #if CONFIG_ERROR_CONCEALMENT michael@0: int corruption_detected = 0; michael@0: #endif michael@0: michael@0: if (xd->mode_info_context->mbmi.mb_skip_coeff) michael@0: { michael@0: vp8_reset_mb_tokens_context(xd); michael@0: } michael@0: else if (!vp8dx_bool_error(xd->current_bc)) michael@0: { michael@0: int eobtotal; michael@0: eobtotal = vp8_decode_mb_tokens(pbi, xd); michael@0: michael@0: /* Special case: Force the loopfilter to skip when eobtotal is zero */ michael@0: xd->mode_info_context->mbmi.mb_skip_coeff = (eobtotal==0); michael@0: } michael@0: michael@0: mode = xd->mode_info_context->mbmi.mode; michael@0: michael@0: if (xd->segmentation_enabled) michael@0: vp8_mb_init_dequantizer(pbi, xd); michael@0: michael@0: michael@0: #if CONFIG_ERROR_CONCEALMENT michael@0: michael@0: if(pbi->ec_active) michael@0: { michael@0: int throw_residual; michael@0: /* When we have independent partitions we can apply residual even michael@0: * though other partitions within the frame are corrupt. michael@0: */ michael@0: throw_residual = (!pbi->independent_partitions && michael@0: pbi->frame_corrupt_residual); michael@0: throw_residual = (throw_residual || vp8dx_bool_error(xd->current_bc)); michael@0: michael@0: if ((mb_idx >= pbi->mvs_corrupt_from_mb || throw_residual)) michael@0: { michael@0: /* MB with corrupt residuals or corrupt mode/motion vectors. michael@0: * Better to use the predictor as reconstruction. michael@0: */ michael@0: pbi->frame_corrupt_residual = 1; michael@0: vpx_memset(xd->qcoeff, 0, sizeof(xd->qcoeff)); michael@0: vp8_conceal_corrupt_mb(xd); michael@0: michael@0: michael@0: corruption_detected = 1; michael@0: michael@0: /* force idct to be skipped for B_PRED and use the michael@0: * prediction only for reconstruction michael@0: * */ michael@0: vpx_memset(xd->eobs, 0, 25); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* do prediction */ michael@0: if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME) michael@0: { michael@0: vp8_build_intra_predictors_mbuv_s(xd, michael@0: xd->recon_above[1], michael@0: xd->recon_above[2], michael@0: xd->recon_left[1], michael@0: xd->recon_left[2], michael@0: xd->recon_left_stride[1], michael@0: xd->dst.u_buffer, xd->dst.v_buffer, michael@0: xd->dst.uv_stride); michael@0: michael@0: if (mode != B_PRED) michael@0: { michael@0: vp8_build_intra_predictors_mby_s(xd, michael@0: xd->recon_above[0], michael@0: xd->recon_left[0], michael@0: xd->recon_left_stride[0], michael@0: xd->dst.y_buffer, michael@0: xd->dst.y_stride); michael@0: } michael@0: else michael@0: { michael@0: short *DQC = xd->dequant_y1; michael@0: int dst_stride = xd->dst.y_stride; michael@0: michael@0: /* clear out residual eob info */ michael@0: if(xd->mode_info_context->mbmi.mb_skip_coeff) michael@0: vpx_memset(xd->eobs, 0, 25); michael@0: michael@0: intra_prediction_down_copy(xd, xd->recon_above[0] + 16); michael@0: michael@0: for (i = 0; i < 16; i++) michael@0: { michael@0: BLOCKD *b = &xd->block[i]; michael@0: unsigned char *dst = xd->dst.y_buffer + b->offset; michael@0: B_PREDICTION_MODE b_mode = michael@0: xd->mode_info_context->bmi[i].as_mode; michael@0: unsigned char *Above; michael@0: unsigned char *yleft; michael@0: int left_stride; michael@0: unsigned char top_left; michael@0: michael@0: /*Caution: For some b_mode, it needs 8 pixels (4 above + 4 above-right).*/ michael@0: if (i < 4 && pbi->common.filter_level) michael@0: Above = xd->recon_above[0] + b->offset; michael@0: else michael@0: Above = dst - dst_stride; michael@0: michael@0: if (i%4==0 && pbi->common.filter_level) michael@0: { michael@0: yleft = xd->recon_left[0] + i; michael@0: left_stride = 1; michael@0: } michael@0: else michael@0: { michael@0: yleft = dst - 1; michael@0: left_stride = dst_stride; michael@0: } michael@0: michael@0: if ((i==4 || i==8 || i==12) && pbi->common.filter_level) michael@0: top_left = *(xd->recon_left[0] + i - 1); michael@0: else michael@0: top_left = Above[-1]; michael@0: michael@0: vp8_intra4x4_predict(Above, yleft, left_stride, michael@0: b_mode, dst, dst_stride, top_left); michael@0: michael@0: if (xd->eobs[i] ) michael@0: { michael@0: if (xd->eobs[i] > 1) michael@0: { michael@0: vp8_dequant_idct_add(b->qcoeff, DQC, dst, dst_stride); michael@0: } michael@0: else michael@0: { michael@0: vp8_dc_only_idct_add(b->qcoeff[0] * DQC[0], michael@0: dst, dst_stride, dst, dst_stride); michael@0: vpx_memset(b->qcoeff, 0, 2 * sizeof(b->qcoeff[0])); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: else michael@0: { michael@0: vp8_build_inter_predictors_mb(xd); michael@0: } michael@0: michael@0: michael@0: #if CONFIG_ERROR_CONCEALMENT michael@0: if (corruption_detected) michael@0: { michael@0: return; michael@0: } michael@0: #endif michael@0: michael@0: if(!xd->mode_info_context->mbmi.mb_skip_coeff) michael@0: { michael@0: /* dequantization and idct */ michael@0: if (mode != B_PRED) michael@0: { michael@0: short *DQC = xd->dequant_y1; michael@0: michael@0: if (mode != SPLITMV) michael@0: { michael@0: BLOCKD *b = &xd->block[24]; michael@0: michael@0: /* do 2nd order transform on the dc block */ michael@0: if (xd->eobs[24] > 1) michael@0: { michael@0: vp8_dequantize_b(b, xd->dequant_y2); michael@0: michael@0: vp8_short_inv_walsh4x4(&b->dqcoeff[0], michael@0: xd->qcoeff); michael@0: vpx_memset(b->qcoeff, 0, 16 * sizeof(b->qcoeff[0])); michael@0: } michael@0: else michael@0: { michael@0: b->dqcoeff[0] = b->qcoeff[0] * xd->dequant_y2[0]; michael@0: vp8_short_inv_walsh4x4_1(&b->dqcoeff[0], michael@0: xd->qcoeff); michael@0: vpx_memset(b->qcoeff, 0, 2 * sizeof(b->qcoeff[0])); michael@0: } michael@0: michael@0: /* override the dc dequant constant in order to preserve the michael@0: * dc components michael@0: */ michael@0: DQC = xd->dequant_y1_dc; michael@0: } michael@0: michael@0: vp8_dequant_idct_add_y_block michael@0: (xd->qcoeff, DQC, michael@0: xd->dst.y_buffer, michael@0: xd->dst.y_stride, xd->eobs); michael@0: } michael@0: michael@0: vp8_dequant_idct_add_uv_block michael@0: (xd->qcoeff+16*16, xd->dequant_uv, michael@0: xd->dst.u_buffer, xd->dst.v_buffer, michael@0: xd->dst.uv_stride, xd->eobs+16); michael@0: } michael@0: } michael@0: michael@0: static void mt_decode_mb_rows(VP8D_COMP *pbi, MACROBLOCKD *xd, int start_mb_row) michael@0: { michael@0: volatile const int *last_row_current_mb_col; michael@0: volatile int *current_mb_col; michael@0: int mb_row; michael@0: VP8_COMMON *pc = &pbi->common; michael@0: const int nsync = pbi->sync_range; michael@0: const int first_row_no_sync_above = pc->mb_cols + nsync; michael@0: int num_part = 1 << pbi->common.multi_token_partition; michael@0: int last_mb_row = start_mb_row; michael@0: michael@0: YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME]; michael@0: YV12_BUFFER_CONFIG *yv12_fb_lst = pbi->dec_fb_ref[LAST_FRAME]; michael@0: michael@0: int recon_y_stride = yv12_fb_new->y_stride; michael@0: int recon_uv_stride = yv12_fb_new->uv_stride; michael@0: michael@0: unsigned char *ref_buffer[MAX_REF_FRAMES][3]; michael@0: unsigned char *dst_buffer[3]; michael@0: int i; michael@0: int ref_fb_corrupted[MAX_REF_FRAMES]; michael@0: michael@0: ref_fb_corrupted[INTRA_FRAME] = 0; michael@0: michael@0: for(i = 1; i < MAX_REF_FRAMES; i++) michael@0: { michael@0: YV12_BUFFER_CONFIG *this_fb = pbi->dec_fb_ref[i]; michael@0: michael@0: ref_buffer[i][0] = this_fb->y_buffer; michael@0: ref_buffer[i][1] = this_fb->u_buffer; michael@0: ref_buffer[i][2] = this_fb->v_buffer; michael@0: michael@0: ref_fb_corrupted[i] = this_fb->corrupted; michael@0: } michael@0: michael@0: dst_buffer[0] = yv12_fb_new->y_buffer; michael@0: dst_buffer[1] = yv12_fb_new->u_buffer; michael@0: dst_buffer[2] = yv12_fb_new->v_buffer; michael@0: michael@0: xd->up_available = (start_mb_row != 0); michael@0: michael@0: for (mb_row = start_mb_row; mb_row < pc->mb_rows; mb_row += (pbi->decoding_thread_count + 1)) michael@0: { michael@0: int recon_yoffset, recon_uvoffset; michael@0: int mb_col; michael@0: int filter_level; michael@0: loop_filter_info_n *lfi_n = &pc->lf_info; michael@0: michael@0: /* save last row processed by this thread */ michael@0: last_mb_row = mb_row; michael@0: /* select bool coder for current partition */ michael@0: xd->current_bc = &pbi->mbc[mb_row%num_part]; michael@0: michael@0: if (mb_row > 0) michael@0: last_row_current_mb_col = &pbi->mt_current_mb_col[mb_row -1]; michael@0: else michael@0: last_row_current_mb_col = &first_row_no_sync_above; michael@0: michael@0: current_mb_col = &pbi->mt_current_mb_col[mb_row]; michael@0: michael@0: recon_yoffset = mb_row * recon_y_stride * 16; michael@0: recon_uvoffset = mb_row * recon_uv_stride * 8; michael@0: michael@0: /* reset contexts */ michael@0: xd->above_context = pc->above_context; michael@0: vpx_memset(xd->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: michael@0: xd->left_available = 0; michael@0: michael@0: xd->mb_to_top_edge = -((mb_row * 16)) << 3; michael@0: xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3; michael@0: michael@0: if (pbi->common.filter_level) michael@0: { michael@0: xd->recon_above[0] = pbi->mt_yabove_row[mb_row] + 0*16 +32; michael@0: xd->recon_above[1] = pbi->mt_uabove_row[mb_row] + 0*8 +16; michael@0: xd->recon_above[2] = pbi->mt_vabove_row[mb_row] + 0*8 +16; michael@0: michael@0: xd->recon_left[0] = pbi->mt_yleft_col[mb_row]; michael@0: xd->recon_left[1] = pbi->mt_uleft_col[mb_row]; michael@0: xd->recon_left[2] = pbi->mt_vleft_col[mb_row]; michael@0: michael@0: /* TODO: move to outside row loop */ michael@0: xd->recon_left_stride[0] = 1; michael@0: xd->recon_left_stride[1] = 1; michael@0: } michael@0: else michael@0: { michael@0: xd->recon_above[0] = dst_buffer[0] + recon_yoffset; michael@0: xd->recon_above[1] = dst_buffer[1] + recon_uvoffset; michael@0: xd->recon_above[2] = dst_buffer[2] + recon_uvoffset; michael@0: michael@0: xd->recon_left[0] = xd->recon_above[0] - 1; michael@0: xd->recon_left[1] = xd->recon_above[1] - 1; michael@0: xd->recon_left[2] = xd->recon_above[2] - 1; michael@0: michael@0: xd->recon_above[0] -= xd->dst.y_stride; michael@0: xd->recon_above[1] -= xd->dst.uv_stride; michael@0: xd->recon_above[2] -= xd->dst.uv_stride; michael@0: michael@0: /* TODO: move to outside row loop */ michael@0: xd->recon_left_stride[0] = xd->dst.y_stride; michael@0: xd->recon_left_stride[1] = xd->dst.uv_stride; michael@0: michael@0: setup_intra_recon_left(xd->recon_left[0], xd->recon_left[1], michael@0: xd->recon_left[2], xd->dst.y_stride, michael@0: xd->dst.uv_stride); michael@0: } michael@0: michael@0: for (mb_col = 0; mb_col < pc->mb_cols; mb_col++) michael@0: { michael@0: *current_mb_col = mb_col - 1; michael@0: michael@0: if ((mb_col & (nsync - 1)) == 0) michael@0: { michael@0: while (mb_col > (*last_row_current_mb_col - nsync)) michael@0: { michael@0: x86_pause_hint(); michael@0: thread_sleep(0); michael@0: } michael@0: } michael@0: michael@0: /* Distance of MB to the various image edges. michael@0: * These are specified to 8th pel as they are always michael@0: * compared to values that are in 1/8th pel units. michael@0: */ michael@0: xd->mb_to_left_edge = -((mb_col * 16) << 3); michael@0: xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3; michael@0: michael@0: #if CONFIG_ERROR_CONCEALMENT michael@0: { michael@0: int corrupt_residual = michael@0: (!pbi->independent_partitions && michael@0: pbi->frame_corrupt_residual) || michael@0: vp8dx_bool_error(xd->current_bc); michael@0: if (pbi->ec_active && michael@0: (xd->mode_info_context->mbmi.ref_frame == michael@0: INTRA_FRAME) && michael@0: corrupt_residual) michael@0: { michael@0: /* We have an intra block with corrupt michael@0: * coefficients, better to conceal with an inter michael@0: * block. michael@0: * Interpolate MVs from neighboring MBs michael@0: * michael@0: * Note that for the first mb with corrupt michael@0: * residual in a frame, we might not discover michael@0: * that before decoding the residual. That michael@0: * happens after this check, and therefore no michael@0: * inter concealment will be done. michael@0: */ michael@0: vp8_interpolate_motion(xd, michael@0: mb_row, mb_col, michael@0: pc->mb_rows, pc->mb_cols, michael@0: pc->mode_info_stride); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: michael@0: xd->dst.y_buffer = dst_buffer[0] + recon_yoffset; michael@0: xd->dst.u_buffer = dst_buffer[1] + recon_uvoffset; michael@0: xd->dst.v_buffer = dst_buffer[2] + recon_uvoffset; michael@0: michael@0: xd->pre.y_buffer = ref_buffer[xd->mode_info_context->mbmi.ref_frame][0] + recon_yoffset; michael@0: xd->pre.u_buffer = ref_buffer[xd->mode_info_context->mbmi.ref_frame][1] + recon_uvoffset; michael@0: xd->pre.v_buffer = ref_buffer[xd->mode_info_context->mbmi.ref_frame][2] + recon_uvoffset; michael@0: michael@0: /* propagate errors from reference frames */ michael@0: xd->corrupted |= ref_fb_corrupted[xd->mode_info_context->mbmi.ref_frame]; michael@0: michael@0: mt_decode_macroblock(pbi, xd, 0); michael@0: michael@0: xd->left_available = 1; michael@0: michael@0: /* check if the boolean decoder has suffered an error */ michael@0: xd->corrupted |= vp8dx_bool_error(xd->current_bc); michael@0: michael@0: xd->recon_above[0] += 16; michael@0: xd->recon_above[1] += 8; michael@0: xd->recon_above[2] += 8; michael@0: michael@0: if (!pbi->common.filter_level) michael@0: { michael@0: xd->recon_left[0] += 16; michael@0: xd->recon_left[1] += 8; michael@0: xd->recon_left[2] += 8; michael@0: } michael@0: michael@0: if (pbi->common.filter_level) michael@0: { michael@0: int skip_lf = (xd->mode_info_context->mbmi.mode != B_PRED && michael@0: xd->mode_info_context->mbmi.mode != SPLITMV && michael@0: xd->mode_info_context->mbmi.mb_skip_coeff); michael@0: michael@0: const int mode_index = lfi_n->mode_lf_lut[xd->mode_info_context->mbmi.mode]; michael@0: const int seg = xd->mode_info_context->mbmi.segment_id; michael@0: const int ref_frame = xd->mode_info_context->mbmi.ref_frame; michael@0: michael@0: filter_level = lfi_n->lvl[seg][ref_frame][mode_index]; michael@0: michael@0: if( mb_row != pc->mb_rows-1 ) michael@0: { michael@0: /* Save decoded MB last row data for next-row decoding */ michael@0: vpx_memcpy((pbi->mt_yabove_row[mb_row + 1] + 32 + mb_col*16), (xd->dst.y_buffer + 15 * recon_y_stride), 16); michael@0: vpx_memcpy((pbi->mt_uabove_row[mb_row + 1] + 16 + mb_col*8), (xd->dst.u_buffer + 7 * recon_uv_stride), 8); michael@0: vpx_memcpy((pbi->mt_vabove_row[mb_row + 1] + 16 + mb_col*8), (xd->dst.v_buffer + 7 * recon_uv_stride), 8); michael@0: } michael@0: michael@0: /* save left_col for next MB decoding */ michael@0: if(mb_col != pc->mb_cols-1) michael@0: { michael@0: MODE_INFO *next = xd->mode_info_context +1; michael@0: michael@0: if (next->mbmi.ref_frame == INTRA_FRAME) michael@0: { michael@0: for (i = 0; i < 16; i++) michael@0: pbi->mt_yleft_col[mb_row][i] = xd->dst.y_buffer [i* recon_y_stride + 15]; michael@0: for (i = 0; i < 8; i++) michael@0: { michael@0: pbi->mt_uleft_col[mb_row][i] = xd->dst.u_buffer [i* recon_uv_stride + 7]; michael@0: pbi->mt_vleft_col[mb_row][i] = xd->dst.v_buffer [i* recon_uv_stride + 7]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* loopfilter on this macroblock. */ michael@0: if (filter_level) michael@0: { michael@0: if(pc->filter_type == NORMAL_LOOPFILTER) michael@0: { michael@0: loop_filter_info lfi; michael@0: FRAME_TYPE frame_type = pc->frame_type; michael@0: const int hev_index = lfi_n->hev_thr_lut[frame_type][filter_level]; michael@0: lfi.mblim = lfi_n->mblim[filter_level]; michael@0: lfi.blim = lfi_n->blim[filter_level]; michael@0: lfi.lim = lfi_n->lim[filter_level]; michael@0: lfi.hev_thr = lfi_n->hev_thr[hev_index]; michael@0: michael@0: if (mb_col > 0) michael@0: vp8_loop_filter_mbv michael@0: (xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi); michael@0: michael@0: if (!skip_lf) michael@0: vp8_loop_filter_bv michael@0: (xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi); michael@0: michael@0: /* don't apply across umv border */ michael@0: if (mb_row > 0) michael@0: vp8_loop_filter_mbh michael@0: (xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi); michael@0: michael@0: if (!skip_lf) michael@0: vp8_loop_filter_bh michael@0: (xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi); michael@0: } michael@0: else michael@0: { michael@0: if (mb_col > 0) michael@0: vp8_loop_filter_simple_mbv michael@0: (xd->dst.y_buffer, recon_y_stride, lfi_n->mblim[filter_level]); michael@0: michael@0: if (!skip_lf) michael@0: vp8_loop_filter_simple_bv michael@0: (xd->dst.y_buffer, recon_y_stride, lfi_n->blim[filter_level]); michael@0: michael@0: /* don't apply across umv border */ michael@0: if (mb_row > 0) michael@0: vp8_loop_filter_simple_mbh michael@0: (xd->dst.y_buffer, recon_y_stride, lfi_n->mblim[filter_level]); michael@0: michael@0: if (!skip_lf) michael@0: vp8_loop_filter_simple_bh michael@0: (xd->dst.y_buffer, recon_y_stride, lfi_n->blim[filter_level]); michael@0: } michael@0: } michael@0: michael@0: } michael@0: michael@0: recon_yoffset += 16; michael@0: recon_uvoffset += 8; michael@0: michael@0: ++xd->mode_info_context; /* next mb */ michael@0: michael@0: xd->above_context++; michael@0: } michael@0: michael@0: /* adjust to the next row of mbs */ michael@0: if (pbi->common.filter_level) michael@0: { michael@0: if(mb_row != pc->mb_rows-1) michael@0: { michael@0: int lasty = yv12_fb_lst->y_width + VP8BORDERINPIXELS; michael@0: int lastuv = (yv12_fb_lst->y_width>>1) + (VP8BORDERINPIXELS>>1); michael@0: michael@0: for (i = 0; i < 4; i++) michael@0: { michael@0: pbi->mt_yabove_row[mb_row +1][lasty + i] = pbi->mt_yabove_row[mb_row +1][lasty -1]; michael@0: pbi->mt_uabove_row[mb_row +1][lastuv + i] = pbi->mt_uabove_row[mb_row +1][lastuv -1]; michael@0: pbi->mt_vabove_row[mb_row +1][lastuv + i] = pbi->mt_vabove_row[mb_row +1][lastuv -1]; michael@0: } michael@0: } michael@0: } michael@0: else michael@0: vp8_extend_mb_row(yv12_fb_new, xd->dst.y_buffer + 16, michael@0: xd->dst.u_buffer + 8, xd->dst.v_buffer + 8); michael@0: michael@0: /* last MB of row is ready just after extension is done */ michael@0: *current_mb_col = mb_col + nsync; michael@0: michael@0: ++xd->mode_info_context; /* skip prediction column */ michael@0: xd->up_available = 1; michael@0: michael@0: /* since we have multithread */ michael@0: xd->mode_info_context += xd->mode_info_stride * pbi->decoding_thread_count; michael@0: } michael@0: michael@0: /* signal end of frame decoding if this thread processed the last mb_row */ michael@0: if (last_mb_row == (pc->mb_rows - 1)) michael@0: sem_post(&pbi->h_event_end_decoding); michael@0: michael@0: } michael@0: michael@0: michael@0: static THREAD_FUNCTION thread_decoding_proc(void *p_data) michael@0: { michael@0: int ithread = ((DECODETHREAD_DATA *)p_data)->ithread; michael@0: VP8D_COMP *pbi = (VP8D_COMP *)(((DECODETHREAD_DATA *)p_data)->ptr1); michael@0: MB_ROW_DEC *mbrd = (MB_ROW_DEC *)(((DECODETHREAD_DATA *)p_data)->ptr2); michael@0: ENTROPY_CONTEXT_PLANES mb_row_left_context; michael@0: michael@0: while (1) michael@0: { michael@0: if (pbi->b_multithreaded_rd == 0) michael@0: break; michael@0: michael@0: if (sem_wait(&pbi->h_event_start_decoding[ithread]) == 0) michael@0: { michael@0: if (pbi->b_multithreaded_rd == 0) michael@0: break; michael@0: else michael@0: { michael@0: MACROBLOCKD *xd = &mbrd->mbd; michael@0: xd->left_context = &mb_row_left_context; michael@0: michael@0: mt_decode_mb_rows(pbi, xd, ithread+1); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return 0 ; michael@0: } michael@0: michael@0: michael@0: void vp8_decoder_create_threads(VP8D_COMP *pbi) michael@0: { michael@0: int core_count = 0; michael@0: unsigned int ithread; michael@0: michael@0: pbi->b_multithreaded_rd = 0; michael@0: pbi->allocated_decoding_thread_count = 0; michael@0: michael@0: /* limit decoding threads to the max number of token partitions */ michael@0: core_count = (pbi->max_threads > 8) ? 8 : pbi->max_threads; michael@0: michael@0: /* limit decoding threads to the available cores */ michael@0: if (core_count > pbi->common.processor_core_count) michael@0: core_count = pbi->common.processor_core_count; michael@0: michael@0: if (core_count > 1) michael@0: { michael@0: pbi->b_multithreaded_rd = 1; michael@0: pbi->decoding_thread_count = core_count - 1; michael@0: michael@0: CALLOC_ARRAY(pbi->h_decoding_thread, pbi->decoding_thread_count); michael@0: CALLOC_ARRAY(pbi->h_event_start_decoding, pbi->decoding_thread_count); michael@0: CALLOC_ARRAY_ALIGNED(pbi->mb_row_di, pbi->decoding_thread_count, 32); michael@0: CALLOC_ARRAY(pbi->de_thread_data, pbi->decoding_thread_count); michael@0: michael@0: for (ithread = 0; ithread < pbi->decoding_thread_count; ithread++) michael@0: { michael@0: sem_init(&pbi->h_event_start_decoding[ithread], 0, 0); michael@0: michael@0: vp8_setup_block_dptrs(&pbi->mb_row_di[ithread].mbd); michael@0: michael@0: pbi->de_thread_data[ithread].ithread = ithread; michael@0: pbi->de_thread_data[ithread].ptr1 = (void *)pbi; michael@0: pbi->de_thread_data[ithread].ptr2 = (void *) &pbi->mb_row_di[ithread]; michael@0: michael@0: pthread_create(&pbi->h_decoding_thread[ithread], 0, thread_decoding_proc, (&pbi->de_thread_data[ithread])); michael@0: } michael@0: michael@0: sem_init(&pbi->h_event_end_decoding, 0, 0); michael@0: michael@0: pbi->allocated_decoding_thread_count = pbi->decoding_thread_count; michael@0: } michael@0: } michael@0: michael@0: michael@0: void vp8mt_de_alloc_temp_buffers(VP8D_COMP *pbi, int mb_rows) michael@0: { michael@0: int i; michael@0: michael@0: if (pbi->b_multithreaded_rd) michael@0: { michael@0: vpx_free(pbi->mt_current_mb_col); michael@0: pbi->mt_current_mb_col = NULL ; michael@0: michael@0: /* Free above_row buffers. */ michael@0: if (pbi->mt_yabove_row) michael@0: { michael@0: for (i=0; i< mb_rows; i++) michael@0: { michael@0: vpx_free(pbi->mt_yabove_row[i]); michael@0: pbi->mt_yabove_row[i] = NULL ; michael@0: } michael@0: vpx_free(pbi->mt_yabove_row); michael@0: pbi->mt_yabove_row = NULL ; michael@0: } michael@0: michael@0: if (pbi->mt_uabove_row) michael@0: { michael@0: for (i=0; i< mb_rows; i++) michael@0: { michael@0: vpx_free(pbi->mt_uabove_row[i]); michael@0: pbi->mt_uabove_row[i] = NULL ; michael@0: } michael@0: vpx_free(pbi->mt_uabove_row); michael@0: pbi->mt_uabove_row = NULL ; michael@0: } michael@0: michael@0: if (pbi->mt_vabove_row) michael@0: { michael@0: for (i=0; i< mb_rows; i++) michael@0: { michael@0: vpx_free(pbi->mt_vabove_row[i]); michael@0: pbi->mt_vabove_row[i] = NULL ; michael@0: } michael@0: vpx_free(pbi->mt_vabove_row); michael@0: pbi->mt_vabove_row = NULL ; michael@0: } michael@0: michael@0: /* Free left_col buffers. */ michael@0: if (pbi->mt_yleft_col) michael@0: { michael@0: for (i=0; i< mb_rows; i++) michael@0: { michael@0: vpx_free(pbi->mt_yleft_col[i]); michael@0: pbi->mt_yleft_col[i] = NULL ; michael@0: } michael@0: vpx_free(pbi->mt_yleft_col); michael@0: pbi->mt_yleft_col = NULL ; michael@0: } michael@0: michael@0: if (pbi->mt_uleft_col) michael@0: { michael@0: for (i=0; i< mb_rows; i++) michael@0: { michael@0: vpx_free(pbi->mt_uleft_col[i]); michael@0: pbi->mt_uleft_col[i] = NULL ; michael@0: } michael@0: vpx_free(pbi->mt_uleft_col); michael@0: pbi->mt_uleft_col = NULL ; michael@0: } michael@0: michael@0: if (pbi->mt_vleft_col) michael@0: { michael@0: for (i=0; i< mb_rows; i++) michael@0: { michael@0: vpx_free(pbi->mt_vleft_col[i]); michael@0: pbi->mt_vleft_col[i] = NULL ; michael@0: } michael@0: vpx_free(pbi->mt_vleft_col); michael@0: pbi->mt_vleft_col = NULL ; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: void vp8mt_alloc_temp_buffers(VP8D_COMP *pbi, int width, int prev_mb_rows) michael@0: { michael@0: VP8_COMMON *const pc = & pbi->common; michael@0: int i; michael@0: int uv_width; michael@0: michael@0: if (pbi->b_multithreaded_rd) michael@0: { michael@0: vp8mt_de_alloc_temp_buffers(pbi, prev_mb_rows); michael@0: michael@0: /* our internal buffers are always multiples of 16 */ michael@0: if ((width & 0xf) != 0) michael@0: width += 16 - (width & 0xf); michael@0: michael@0: if (width < 640) pbi->sync_range = 1; michael@0: else if (width <= 1280) pbi->sync_range = 8; michael@0: else if (width <= 2560) pbi->sync_range =16; michael@0: else pbi->sync_range = 32; michael@0: michael@0: uv_width = width >>1; michael@0: michael@0: /* Allocate an int for each mb row. */ michael@0: CALLOC_ARRAY(pbi->mt_current_mb_col, pc->mb_rows); michael@0: michael@0: /* Allocate memory for above_row buffers. */ michael@0: CALLOC_ARRAY(pbi->mt_yabove_row, pc->mb_rows); michael@0: for (i = 0; i < pc->mb_rows; i++) michael@0: CHECK_MEM_ERROR(pbi->mt_yabove_row[i], vpx_memalign(16,sizeof(unsigned char) * (width + (VP8BORDERINPIXELS<<1)))); michael@0: michael@0: CALLOC_ARRAY(pbi->mt_uabove_row, pc->mb_rows); michael@0: for (i = 0; i < pc->mb_rows; i++) michael@0: CHECK_MEM_ERROR(pbi->mt_uabove_row[i], vpx_memalign(16,sizeof(unsigned char) * (uv_width + VP8BORDERINPIXELS))); michael@0: michael@0: CALLOC_ARRAY(pbi->mt_vabove_row, pc->mb_rows); michael@0: for (i = 0; i < pc->mb_rows; i++) michael@0: CHECK_MEM_ERROR(pbi->mt_vabove_row[i], vpx_memalign(16,sizeof(unsigned char) * (uv_width + VP8BORDERINPIXELS))); michael@0: michael@0: /* Allocate memory for left_col buffers. */ michael@0: CALLOC_ARRAY(pbi->mt_yleft_col, pc->mb_rows); michael@0: for (i = 0; i < pc->mb_rows; i++) michael@0: CHECK_MEM_ERROR(pbi->mt_yleft_col[i], vpx_calloc(sizeof(unsigned char) * 16, 1)); michael@0: michael@0: CALLOC_ARRAY(pbi->mt_uleft_col, pc->mb_rows); michael@0: for (i = 0; i < pc->mb_rows; i++) michael@0: CHECK_MEM_ERROR(pbi->mt_uleft_col[i], vpx_calloc(sizeof(unsigned char) * 8, 1)); michael@0: michael@0: CALLOC_ARRAY(pbi->mt_vleft_col, pc->mb_rows); michael@0: for (i = 0; i < pc->mb_rows; i++) michael@0: CHECK_MEM_ERROR(pbi->mt_vleft_col[i], vpx_calloc(sizeof(unsigned char) * 8, 1)); michael@0: } michael@0: } michael@0: michael@0: michael@0: void vp8_decoder_remove_threads(VP8D_COMP *pbi) michael@0: { michael@0: /* shutdown MB Decoding thread; */ michael@0: if (pbi->b_multithreaded_rd) michael@0: { michael@0: int i; michael@0: michael@0: pbi->b_multithreaded_rd = 0; michael@0: michael@0: /* allow all threads to exit */ michael@0: for (i = 0; i < pbi->allocated_decoding_thread_count; i++) michael@0: { michael@0: sem_post(&pbi->h_event_start_decoding[i]); michael@0: pthread_join(pbi->h_decoding_thread[i], NULL); michael@0: } michael@0: michael@0: for (i = 0; i < pbi->allocated_decoding_thread_count; i++) michael@0: { michael@0: sem_destroy(&pbi->h_event_start_decoding[i]); michael@0: } michael@0: michael@0: sem_destroy(&pbi->h_event_end_decoding); michael@0: michael@0: vpx_free(pbi->h_decoding_thread); michael@0: pbi->h_decoding_thread = NULL; michael@0: michael@0: vpx_free(pbi->h_event_start_decoding); michael@0: pbi->h_event_start_decoding = NULL; michael@0: michael@0: vpx_free(pbi->mb_row_di); michael@0: pbi->mb_row_di = NULL ; michael@0: michael@0: vpx_free(pbi->de_thread_data); michael@0: pbi->de_thread_data = NULL; michael@0: } michael@0: } michael@0: michael@0: void vp8mt_decode_mb_rows( VP8D_COMP *pbi, MACROBLOCKD *xd) michael@0: { michael@0: VP8_COMMON *pc = &pbi->common; michael@0: unsigned int i; michael@0: int j; michael@0: michael@0: int filter_level = pc->filter_level; michael@0: YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME]; michael@0: michael@0: if (filter_level) michael@0: { michael@0: /* Set above_row buffer to 127 for decoding first MB row */ michael@0: vpx_memset(pbi->mt_yabove_row[0] + VP8BORDERINPIXELS-1, 127, yv12_fb_new->y_width + 5); michael@0: vpx_memset(pbi->mt_uabove_row[0] + (VP8BORDERINPIXELS>>1)-1, 127, (yv12_fb_new->y_width>>1) +5); michael@0: vpx_memset(pbi->mt_vabove_row[0] + (VP8BORDERINPIXELS>>1)-1, 127, (yv12_fb_new->y_width>>1) +5); michael@0: michael@0: for (j=1; jmb_rows; j++) michael@0: { michael@0: vpx_memset(pbi->mt_yabove_row[j] + VP8BORDERINPIXELS-1, (unsigned char)129, 1); michael@0: vpx_memset(pbi->mt_uabove_row[j] + (VP8BORDERINPIXELS>>1)-1, (unsigned char)129, 1); michael@0: vpx_memset(pbi->mt_vabove_row[j] + (VP8BORDERINPIXELS>>1)-1, (unsigned char)129, 1); michael@0: } michael@0: michael@0: /* Set left_col to 129 initially */ michael@0: for (j=0; jmb_rows; j++) michael@0: { michael@0: vpx_memset(pbi->mt_yleft_col[j], (unsigned char)129, 16); michael@0: vpx_memset(pbi->mt_uleft_col[j], (unsigned char)129, 8); michael@0: vpx_memset(pbi->mt_vleft_col[j], (unsigned char)129, 8); michael@0: } michael@0: michael@0: /* Initialize the loop filter for this frame. */ michael@0: vp8_loop_filter_frame_init(pc, &pbi->mb, filter_level); michael@0: } michael@0: else michael@0: vp8_setup_intra_recon_top_line(yv12_fb_new); michael@0: michael@0: setup_decoding_thread_data(pbi, xd, pbi->mb_row_di, pbi->decoding_thread_count); michael@0: michael@0: for (i = 0; i < pbi->decoding_thread_count; i++) michael@0: sem_post(&pbi->h_event_start_decoding[i]); michael@0: michael@0: mt_decode_mb_rows(pbi, xd, 0); michael@0: michael@0: sem_wait(&pbi->h_event_end_decoding); /* add back for each frame */ michael@0: }