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: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "./vp9_rtcd.h" michael@0: #include "./vpx_config.h" michael@0: michael@0: #include "vpx_ports/vpx_timer.h" michael@0: michael@0: #include "vp9/common/vp9_common.h" michael@0: #include "vp9/common/vp9_entropy.h" michael@0: #include "vp9/common/vp9_entropymode.h" michael@0: #include "vp9/common/vp9_extend.h" michael@0: #include "vp9/common/vp9_findnearmv.h" michael@0: #include "vp9/common/vp9_idct.h" michael@0: #include "vp9/common/vp9_mvref_common.h" michael@0: #include "vp9/common/vp9_pred_common.h" michael@0: #include "vp9/common/vp9_quant_common.h" michael@0: #include "vp9/common/vp9_reconintra.h" michael@0: #include "vp9/common/vp9_reconinter.h" michael@0: #include "vp9/common/vp9_seg_common.h" michael@0: #include "vp9/common/vp9_tile_common.h" michael@0: #include "vp9/encoder/vp9_encodeframe.h" michael@0: #include "vp9/encoder/vp9_encodeintra.h" michael@0: #include "vp9/encoder/vp9_encodemb.h" michael@0: #include "vp9/encoder/vp9_encodemv.h" michael@0: #include "vp9/encoder/vp9_onyx_int.h" michael@0: #include "vp9/encoder/vp9_rdopt.h" michael@0: #include "vp9/encoder/vp9_segmentation.h" michael@0: #include "vp9/common/vp9_systemdependent.h" michael@0: #include "vp9/encoder/vp9_tokenize.h" michael@0: #include "vp9/encoder/vp9_vaq.h" michael@0: michael@0: michael@0: #define DBG_PRNT_SEGMAP 0 michael@0: michael@0: michael@0: // #define ENC_DEBUG michael@0: #ifdef ENC_DEBUG michael@0: int enc_debug = 0; michael@0: #endif michael@0: michael@0: static INLINE uint8_t *get_sb_index(MACROBLOCK *x, BLOCK_SIZE subsize) { michael@0: switch (subsize) { michael@0: case BLOCK_64X64: michael@0: case BLOCK_64X32: michael@0: case BLOCK_32X64: michael@0: case BLOCK_32X32: michael@0: return &x->sb_index; michael@0: case BLOCK_32X16: michael@0: case BLOCK_16X32: michael@0: case BLOCK_16X16: michael@0: return &x->mb_index; michael@0: case BLOCK_16X8: michael@0: case BLOCK_8X16: michael@0: case BLOCK_8X8: michael@0: return &x->b_index; michael@0: case BLOCK_8X4: michael@0: case BLOCK_4X8: michael@0: case BLOCK_4X4: michael@0: return &x->ab_index; michael@0: default: michael@0: assert(0); michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: static void encode_superblock(VP9_COMP *cpi, TOKENEXTRA **t, int output_enabled, michael@0: int mi_row, int mi_col, BLOCK_SIZE bsize); michael@0: michael@0: static void adjust_act_zbin(VP9_COMP *cpi, MACROBLOCK *x); michael@0: michael@0: /* activity_avg must be positive, or flat regions could get a zero weight michael@0: * (infinite lambda), which confounds analysis. michael@0: * This also avoids the need for divide by zero checks in michael@0: * vp9_activity_masking(). michael@0: */ michael@0: #define ACTIVITY_AVG_MIN (64) michael@0: michael@0: /* Motion vector component magnitude threshold for defining fast motion. */ michael@0: #define FAST_MOTION_MV_THRESH (24) michael@0: michael@0: /* This is used as a reference when computing the source variance for the michael@0: * purposes of activity masking. michael@0: * Eventually this should be replaced by custom no-reference routines, michael@0: * which will be faster. michael@0: */ michael@0: static const uint8_t VP9_VAR_OFFS[64] = { michael@0: 128, 128, 128, 128, 128, 128, 128, 128, michael@0: 128, 128, 128, 128, 128, 128, 128, 128, michael@0: 128, 128, 128, 128, 128, 128, 128, 128, michael@0: 128, 128, 128, 128, 128, 128, 128, 128, michael@0: 128, 128, 128, 128, 128, 128, 128, 128, michael@0: 128, 128, 128, 128, 128, 128, 128, 128, michael@0: 128, 128, 128, 128, 128, 128, 128, 128, michael@0: 128, 128, 128, 128, 128, 128, 128, 128 michael@0: }; michael@0: michael@0: static unsigned int get_sby_perpixel_variance(VP9_COMP *cpi, MACROBLOCK *x, michael@0: BLOCK_SIZE bs) { michael@0: unsigned int var, sse; michael@0: var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf, michael@0: x->plane[0].src.stride, michael@0: VP9_VAR_OFFS, 0, &sse); michael@0: return (var + (1 << (num_pels_log2_lookup[bs] - 1))) >> michael@0: num_pels_log2_lookup[bs]; michael@0: } michael@0: michael@0: // Original activity measure from Tim T's code. michael@0: static unsigned int tt_activity_measure(MACROBLOCK *x) { michael@0: unsigned int act; michael@0: unsigned int sse; michael@0: /* TODO: This could also be done over smaller areas (8x8), but that would michael@0: * require extensive changes elsewhere, as lambda is assumed to be fixed michael@0: * over an entire MB in most of the code. michael@0: * Another option is to compute four 8x8 variances, and pick a single michael@0: * lambda using a non-linear combination (e.g., the smallest, or second michael@0: * smallest, etc.). michael@0: */ michael@0: act = vp9_variance16x16(x->plane[0].src.buf, x->plane[0].src.stride, michael@0: VP9_VAR_OFFS, 0, &sse); michael@0: act <<= 4; michael@0: michael@0: /* If the region is flat, lower the activity some more. */ michael@0: if (act < 8 << 12) michael@0: act = act < 5 << 12 ? act : 5 << 12; michael@0: michael@0: return act; michael@0: } michael@0: michael@0: // Stub for alternative experimental activity measures. michael@0: static unsigned int alt_activity_measure(MACROBLOCK *x, int use_dc_pred) { michael@0: return vp9_encode_intra(x, use_dc_pred); michael@0: } michael@0: michael@0: // Measure the activity of the current macroblock michael@0: // What we measure here is TBD so abstracted to this function michael@0: #define ALT_ACT_MEASURE 1 michael@0: static unsigned int mb_activity_measure(MACROBLOCK *x, int mb_row, int mb_col) { michael@0: unsigned int mb_activity; michael@0: michael@0: if (ALT_ACT_MEASURE) { michael@0: int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); michael@0: michael@0: // Or use and alternative. michael@0: mb_activity = alt_activity_measure(x, use_dc_pred); michael@0: } else { michael@0: // Original activity measure from Tim T's code. michael@0: mb_activity = tt_activity_measure(x); michael@0: } michael@0: michael@0: if (mb_activity < ACTIVITY_AVG_MIN) michael@0: mb_activity = ACTIVITY_AVG_MIN; michael@0: michael@0: return mb_activity; michael@0: } michael@0: michael@0: // Calculate an "average" mb activity value for the frame michael@0: #define ACT_MEDIAN 0 michael@0: static void calc_av_activity(VP9_COMP *cpi, int64_t activity_sum) { michael@0: #if ACT_MEDIAN michael@0: // Find median: Simple n^2 algorithm for experimentation michael@0: { michael@0: unsigned int median; michael@0: unsigned int i, j; michael@0: unsigned int *sortlist; michael@0: unsigned int tmp; michael@0: michael@0: // Create a list to sort to michael@0: CHECK_MEM_ERROR(&cpi->common, sortlist, vpx_calloc(sizeof(unsigned int), michael@0: cpi->common.MBs)); michael@0: michael@0: // Copy map to sort list michael@0: vpx_memcpy(sortlist, cpi->mb_activity_map, michael@0: sizeof(unsigned int) * cpi->common.MBs); michael@0: michael@0: // Ripple each value down to its correct position michael@0: for (i = 1; i < cpi->common.MBs; i ++) { michael@0: for (j = i; j > 0; j --) { michael@0: if (sortlist[j] < sortlist[j - 1]) { michael@0: // Swap values michael@0: tmp = sortlist[j - 1]; michael@0: sortlist[j - 1] = sortlist[j]; michael@0: sortlist[j] = tmp; michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Even number MBs so estimate median as mean of two either side. michael@0: median = (1 + sortlist[cpi->common.MBs >> 1] + michael@0: sortlist[(cpi->common.MBs >> 1) + 1]) >> 1; michael@0: michael@0: cpi->activity_avg = median; michael@0: michael@0: vpx_free(sortlist); michael@0: } michael@0: #else michael@0: // Simple mean for now michael@0: cpi->activity_avg = (unsigned int) (activity_sum / cpi->common.MBs); michael@0: #endif // ACT_MEDIAN michael@0: michael@0: if (cpi->activity_avg < ACTIVITY_AVG_MIN) michael@0: cpi->activity_avg = ACTIVITY_AVG_MIN; michael@0: michael@0: // Experimental code: return fixed value normalized for several clips michael@0: if (ALT_ACT_MEASURE) michael@0: cpi->activity_avg = 100000; michael@0: } michael@0: michael@0: #define USE_ACT_INDEX 0 michael@0: #define OUTPUT_NORM_ACT_STATS 0 michael@0: michael@0: #if USE_ACT_INDEX michael@0: // Calculate an activity index for each mb michael@0: static void calc_activity_index(VP9_COMP *cpi, MACROBLOCK *x) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: int mb_row, mb_col; michael@0: michael@0: int64_t act; michael@0: int64_t a; michael@0: int64_t b; michael@0: michael@0: #if OUTPUT_NORM_ACT_STATS michael@0: FILE *f = fopen("norm_act.stt", "a"); michael@0: fprintf(f, "\n%12d\n", cpi->activity_avg); michael@0: #endif michael@0: michael@0: // Reset pointers to start of activity map michael@0: x->mb_activity_ptr = cpi->mb_activity_map; michael@0: michael@0: // Calculate normalized mb activity number. michael@0: for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) { michael@0: // for each macroblock col in image michael@0: for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) { michael@0: // Read activity from the map michael@0: act = *(x->mb_activity_ptr); michael@0: michael@0: // Calculate a normalized activity number michael@0: a = act + 4 * cpi->activity_avg; michael@0: b = 4 * act + cpi->activity_avg; michael@0: michael@0: if (b >= a) michael@0: *(x->activity_ptr) = (int)((b + (a >> 1)) / a) - 1; michael@0: else michael@0: *(x->activity_ptr) = 1 - (int)((a + (b >> 1)) / b); michael@0: michael@0: #if OUTPUT_NORM_ACT_STATS michael@0: fprintf(f, " %6d", *(x->mb_activity_ptr)); michael@0: #endif michael@0: // Increment activity map pointers michael@0: x->mb_activity_ptr++; michael@0: } michael@0: michael@0: #if OUTPUT_NORM_ACT_STATS michael@0: fprintf(f, "\n"); michael@0: #endif michael@0: } michael@0: michael@0: #if OUTPUT_NORM_ACT_STATS michael@0: fclose(f); michael@0: #endif michael@0: } michael@0: #endif // USE_ACT_INDEX michael@0: michael@0: // Loop through all MBs. Note activity of each, average activity and michael@0: // calculate a normalized activity for each michael@0: static void build_activity_map(VP9_COMP *cpi) { michael@0: MACROBLOCK * const x = &cpi->mb; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: VP9_COMMON * const cm = &cpi->common; michael@0: michael@0: #if ALT_ACT_MEASURE michael@0: YV12_BUFFER_CONFIG *new_yv12 = get_frame_new_buffer(cm); michael@0: int recon_yoffset; michael@0: int recon_y_stride = new_yv12->y_stride; michael@0: #endif michael@0: michael@0: int mb_row, mb_col; michael@0: unsigned int mb_activity; michael@0: int64_t activity_sum = 0; michael@0: michael@0: x->mb_activity_ptr = cpi->mb_activity_map; michael@0: michael@0: // for each macroblock row in image michael@0: for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) { michael@0: #if ALT_ACT_MEASURE michael@0: // reset above block coeffs michael@0: xd->up_available = (mb_row != 0); michael@0: recon_yoffset = (mb_row * recon_y_stride * 16); michael@0: #endif michael@0: // for each macroblock col in image michael@0: for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) { michael@0: #if ALT_ACT_MEASURE michael@0: xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; michael@0: xd->left_available = (mb_col != 0); michael@0: recon_yoffset += 16; michael@0: #endif michael@0: michael@0: // measure activity michael@0: mb_activity = mb_activity_measure(x, mb_row, mb_col); michael@0: michael@0: // Keep frame sum michael@0: activity_sum += mb_activity; michael@0: michael@0: // Store MB level activity details. michael@0: *x->mb_activity_ptr = mb_activity; michael@0: michael@0: // Increment activity map pointer michael@0: x->mb_activity_ptr++; michael@0: michael@0: // adjust to the next column of source macroblocks michael@0: x->plane[0].src.buf += 16; michael@0: } michael@0: michael@0: // adjust to the next row of mbs michael@0: x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols; michael@0: } michael@0: michael@0: // Calculate an "average" MB activity michael@0: calc_av_activity(cpi, activity_sum); michael@0: michael@0: #if USE_ACT_INDEX michael@0: // Calculate an activity index number of each mb michael@0: calc_activity_index(cpi, x); michael@0: #endif michael@0: } michael@0: michael@0: // Macroblock activity masking michael@0: void vp9_activity_masking(VP9_COMP *cpi, MACROBLOCK *x) { michael@0: #if USE_ACT_INDEX michael@0: x->rdmult += *(x->mb_activity_ptr) * (x->rdmult >> 2); michael@0: x->errorperbit = x->rdmult * 100 / (110 * x->rddiv); michael@0: x->errorperbit += (x->errorperbit == 0); michael@0: #else michael@0: int64_t a; michael@0: int64_t b; michael@0: int64_t act = *(x->mb_activity_ptr); michael@0: michael@0: // Apply the masking to the RD multiplier. michael@0: a = act + (2 * cpi->activity_avg); michael@0: b = (2 * act) + cpi->activity_avg; michael@0: michael@0: x->rdmult = (unsigned int) (((int64_t) x->rdmult * b + (a >> 1)) / a); michael@0: x->errorperbit = x->rdmult * 100 / (110 * x->rddiv); michael@0: x->errorperbit += (x->errorperbit == 0); michael@0: #endif michael@0: michael@0: // Activity based Zbin adjustment michael@0: adjust_act_zbin(cpi, x); michael@0: } michael@0: michael@0: static void update_state(VP9_COMP *cpi, PICK_MODE_CONTEXT *ctx, michael@0: BLOCK_SIZE bsize, int output_enabled) { michael@0: int i, x_idx, y; michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCK *const x = &cpi->mb; michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: struct macroblock_plane *const p = x->plane; michael@0: struct macroblockd_plane *const pd = xd->plane; michael@0: MODE_INFO *mi = &ctx->mic; michael@0: MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; michael@0: MODE_INFO *mi_addr = xd->mi_8x8[0]; michael@0: michael@0: int mb_mode_index = ctx->best_mode_index; michael@0: const int mis = cm->mode_info_stride; michael@0: const int mi_width = num_8x8_blocks_wide_lookup[bsize]; michael@0: const int mi_height = num_8x8_blocks_high_lookup[bsize]; michael@0: int max_plane; michael@0: michael@0: assert(mi->mbmi.mode < MB_MODE_COUNT); michael@0: assert(mi->mbmi.ref_frame[0] < MAX_REF_FRAMES); michael@0: assert(mi->mbmi.ref_frame[1] < MAX_REF_FRAMES); michael@0: assert(mi->mbmi.sb_type == bsize); michael@0: michael@0: *mi_addr = *mi; michael@0: michael@0: max_plane = is_inter_block(mbmi) ? MAX_MB_PLANE : 1; michael@0: for (i = 0; i < max_plane; ++i) { michael@0: p[i].coeff = ctx->coeff_pbuf[i][1]; michael@0: pd[i].qcoeff = ctx->qcoeff_pbuf[i][1]; michael@0: pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1]; michael@0: pd[i].eobs = ctx->eobs_pbuf[i][1]; michael@0: } michael@0: michael@0: for (i = max_plane; i < MAX_MB_PLANE; ++i) { michael@0: p[i].coeff = ctx->coeff_pbuf[i][2]; michael@0: pd[i].qcoeff = ctx->qcoeff_pbuf[i][2]; michael@0: pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][2]; michael@0: pd[i].eobs = ctx->eobs_pbuf[i][2]; michael@0: } michael@0: michael@0: // Restore the coding context of the MB to that that was in place michael@0: // when the mode was picked for it michael@0: for (y = 0; y < mi_height; y++) michael@0: for (x_idx = 0; x_idx < mi_width; x_idx++) michael@0: if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx michael@0: && (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) michael@0: xd->mi_8x8[x_idx + y * mis] = mi_addr; michael@0: michael@0: if (cpi->oxcf.aq_mode == VARIANCE_AQ) { michael@0: vp9_mb_init_quantizer(cpi, x); michael@0: } michael@0: michael@0: // FIXME(rbultje) I'm pretty sure this should go to the end of this block michael@0: // (i.e. after the output_enabled) michael@0: if (bsize < BLOCK_32X32) { michael@0: if (bsize < BLOCK_16X16) michael@0: ctx->tx_rd_diff[ALLOW_16X16] = ctx->tx_rd_diff[ALLOW_8X8]; michael@0: ctx->tx_rd_diff[ALLOW_32X32] = ctx->tx_rd_diff[ALLOW_16X16]; michael@0: } michael@0: michael@0: if (is_inter_block(mbmi) && mbmi->sb_type < BLOCK_8X8) { michael@0: mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int; michael@0: mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int; michael@0: } michael@0: michael@0: x->skip = ctx->skip; michael@0: vpx_memcpy(x->zcoeff_blk[mbmi->tx_size], ctx->zcoeff_blk, michael@0: sizeof(uint8_t) * ctx->num_4x4_blk); michael@0: michael@0: if (!output_enabled) michael@0: return; michael@0: michael@0: if (!vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) { michael@0: for (i = 0; i < TX_MODES; i++) michael@0: cpi->rd_tx_select_diff[i] += ctx->tx_rd_diff[i]; michael@0: } michael@0: michael@0: if (frame_is_intra_only(cm)) { michael@0: #if CONFIG_INTERNAL_STATS michael@0: static const int kf_mode_index[] = { michael@0: THR_DC /*DC_PRED*/, michael@0: THR_V_PRED /*V_PRED*/, michael@0: THR_H_PRED /*H_PRED*/, michael@0: THR_D45_PRED /*D45_PRED*/, michael@0: THR_D135_PRED /*D135_PRED*/, michael@0: THR_D117_PRED /*D117_PRED*/, michael@0: THR_D153_PRED /*D153_PRED*/, michael@0: THR_D207_PRED /*D207_PRED*/, michael@0: THR_D63_PRED /*D63_PRED*/, michael@0: THR_TM /*TM_PRED*/, michael@0: }; michael@0: cpi->mode_chosen_counts[kf_mode_index[mi->mbmi.mode]]++; michael@0: #endif michael@0: } else { michael@0: // Note how often each mode chosen as best michael@0: cpi->mode_chosen_counts[mb_mode_index]++; michael@0: if (is_inter_block(mbmi) michael@0: && (mbmi->sb_type < BLOCK_8X8 || mbmi->mode == NEWMV)) { michael@0: int_mv best_mv[2]; michael@0: const MV_REFERENCE_FRAME rf1 = mbmi->ref_frame[0]; michael@0: const MV_REFERENCE_FRAME rf2 = mbmi->ref_frame[1]; michael@0: best_mv[0].as_int = ctx->best_ref_mv.as_int; michael@0: best_mv[1].as_int = ctx->second_best_ref_mv.as_int; michael@0: if (mbmi->mode == NEWMV) { michael@0: best_mv[0].as_int = mbmi->ref_mvs[rf1][0].as_int; michael@0: if (rf2 > 0) michael@0: best_mv[1].as_int = mbmi->ref_mvs[rf2][0].as_int; michael@0: } michael@0: mbmi->best_mv[0].as_int = best_mv[0].as_int; michael@0: mbmi->best_mv[1].as_int = best_mv[1].as_int; michael@0: vp9_update_mv_count(cpi, x, best_mv); michael@0: } michael@0: michael@0: if (cm->mcomp_filter_type == SWITCHABLE && is_inter_mode(mbmi->mode)) { michael@0: const int ctx = vp9_get_pred_context_switchable_interp(xd); michael@0: ++cm->counts.switchable_interp[ctx][mbmi->interp_filter]; michael@0: } michael@0: michael@0: cpi->rd_comp_pred_diff[SINGLE_PREDICTION_ONLY] += ctx->single_pred_diff; michael@0: cpi->rd_comp_pred_diff[COMP_PREDICTION_ONLY] += ctx->comp_pred_diff; michael@0: cpi->rd_comp_pred_diff[HYBRID_PREDICTION] += ctx->hybrid_pred_diff; michael@0: michael@0: for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) michael@0: cpi->rd_filter_diff[i] += ctx->best_filter_diff[i]; michael@0: } michael@0: } michael@0: michael@0: void vp9_setup_src_planes(MACROBLOCK *x, const YV12_BUFFER_CONFIG *src, michael@0: int mi_row, int mi_col) { michael@0: uint8_t *const buffers[4] = {src->y_buffer, src->u_buffer, src->v_buffer, michael@0: src->alpha_buffer}; michael@0: const int strides[4] = {src->y_stride, src->uv_stride, src->uv_stride, michael@0: src->alpha_stride}; michael@0: int i; michael@0: michael@0: for (i = 0; i < MAX_MB_PLANE; i++) michael@0: setup_pred_plane(&x->plane[i].src, buffers[i], strides[i], mi_row, mi_col, michael@0: NULL, x->e_mbd.plane[i].subsampling_x, michael@0: x->e_mbd.plane[i].subsampling_y); michael@0: } michael@0: michael@0: static void set_offsets(VP9_COMP *cpi, const TileInfo *const tile, michael@0: int mi_row, int mi_col, BLOCK_SIZE bsize) { michael@0: MACROBLOCK *const x = &cpi->mb; michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: MB_MODE_INFO *mbmi; michael@0: const int dst_fb_idx = cm->new_fb_idx; michael@0: const int idx_str = xd->mode_info_stride * mi_row + mi_col; michael@0: const int mi_width = num_8x8_blocks_wide_lookup[bsize]; michael@0: const int mi_height = num_8x8_blocks_high_lookup[bsize]; michael@0: const int mb_row = mi_row >> 1; michael@0: const int mb_col = mi_col >> 1; michael@0: const int idx_map = mb_row * cm->mb_cols + mb_col; michael@0: const struct segmentation *const seg = &cm->seg; michael@0: michael@0: set_skip_context(xd, cpi->above_context, cpi->left_context, mi_row, mi_col); michael@0: michael@0: // Activity map pointer michael@0: x->mb_activity_ptr = &cpi->mb_activity_map[idx_map]; michael@0: x->active_ptr = cpi->active_map + idx_map; michael@0: michael@0: xd->mi_8x8 = cm->mi_grid_visible + idx_str; michael@0: xd->prev_mi_8x8 = cm->prev_mi_grid_visible + idx_str; michael@0: michael@0: // Special case: if prev_mi is NULL, the previous mode info context michael@0: // cannot be used. michael@0: xd->last_mi = cm->prev_mi ? xd->prev_mi_8x8[0] : NULL; michael@0: michael@0: xd->mi_8x8[0] = cm->mi + idx_str; michael@0: michael@0: mbmi = &xd->mi_8x8[0]->mbmi; michael@0: michael@0: // Set up destination pointers michael@0: setup_dst_planes(xd, &cm->yv12_fb[dst_fb_idx], mi_row, mi_col); michael@0: michael@0: // Set up limit values for MV components michael@0: // mv beyond the range do not produce new/different prediction block michael@0: x->mv_row_min = -(((mi_row + mi_height) * MI_SIZE) + VP9_INTERP_EXTEND); michael@0: x->mv_col_min = -(((mi_col + mi_width) * MI_SIZE) + VP9_INTERP_EXTEND); michael@0: x->mv_row_max = (cm->mi_rows - mi_row) * MI_SIZE + VP9_INTERP_EXTEND; michael@0: x->mv_col_max = (cm->mi_cols - mi_col) * MI_SIZE + VP9_INTERP_EXTEND; michael@0: michael@0: // Set up distance of MB to edge of frame in 1/8th pel units michael@0: assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1))); michael@0: set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width, michael@0: cm->mi_rows, cm->mi_cols); michael@0: michael@0: /* set up source buffers */ michael@0: vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col); michael@0: michael@0: /* R/D setup */ michael@0: x->rddiv = cpi->RDDIV; michael@0: x->rdmult = cpi->RDMULT; michael@0: michael@0: /* segment ID */ michael@0: if (seg->enabled) { michael@0: if (!cpi->oxcf.aq_mode == VARIANCE_AQ) { michael@0: uint8_t *map = seg->update_map ? cpi->segmentation_map michael@0: : cm->last_frame_seg_map; michael@0: mbmi->segment_id = vp9_get_segment_id(cm, map, bsize, mi_row, mi_col); michael@0: } michael@0: vp9_mb_init_quantizer(cpi, x); michael@0: michael@0: if (seg->enabled && cpi->seg0_cnt > 0 michael@0: && !vp9_segfeature_active(seg, 0, SEG_LVL_REF_FRAME) michael@0: && vp9_segfeature_active(seg, 1, SEG_LVL_REF_FRAME)) { michael@0: cpi->seg0_progress = (cpi->seg0_idx << 16) / cpi->seg0_cnt; michael@0: } else { michael@0: const int y = mb_row & ~3; michael@0: const int x = mb_col & ~3; michael@0: const int p16 = ((mb_row & 1) << 1) + (mb_col & 1); michael@0: const int p32 = ((mb_row & 2) << 2) + ((mb_col & 2) << 1); michael@0: const int tile_progress = tile->mi_col_start * cm->mb_rows >> 1; michael@0: const int mb_cols = (tile->mi_col_end - tile->mi_col_start) >> 1; michael@0: michael@0: cpi->seg0_progress = ((y * mb_cols + x * 4 + p32 + p16 + tile_progress) michael@0: << 16) / cm->MBs; michael@0: } michael@0: michael@0: x->encode_breakout = cpi->segment_encode_breakout[mbmi->segment_id]; michael@0: } else { michael@0: mbmi->segment_id = 0; michael@0: x->encode_breakout = cpi->oxcf.encode_breakout; michael@0: } michael@0: } michael@0: michael@0: static void pick_sb_modes(VP9_COMP *cpi, const TileInfo *const tile, michael@0: int mi_row, int mi_col, michael@0: int *totalrate, int64_t *totaldist, michael@0: BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx, michael@0: int64_t best_rd) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCK *const x = &cpi->mb; michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: struct macroblock_plane *const p = x->plane; michael@0: struct macroblockd_plane *const pd = xd->plane; michael@0: int i; michael@0: int orig_rdmult = x->rdmult; michael@0: double rdmult_ratio; michael@0: michael@0: vp9_clear_system_state(); // __asm emms; michael@0: rdmult_ratio = 1.0; // avoid uninitialized warnings michael@0: michael@0: // Use the lower precision, but faster, 32x32 fdct for mode selection. michael@0: x->use_lp32x32fdct = 1; michael@0: michael@0: if (bsize < BLOCK_8X8) { michael@0: // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0 michael@0: // there is nothing to be done. michael@0: if (x->ab_index != 0) { michael@0: *totalrate = 0; michael@0: *totaldist = 0; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: set_offsets(cpi, tile, mi_row, mi_col, bsize); michael@0: xd->mi_8x8[0]->mbmi.sb_type = bsize; michael@0: michael@0: for (i = 0; i < MAX_MB_PLANE; ++i) { michael@0: p[i].coeff = ctx->coeff_pbuf[i][0]; michael@0: pd[i].qcoeff = ctx->qcoeff_pbuf[i][0]; michael@0: pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0]; michael@0: pd[i].eobs = ctx->eobs_pbuf[i][0]; michael@0: } michael@0: ctx->is_coded = 0; michael@0: x->skip_recode = 0; michael@0: michael@0: // Set to zero to make sure we do not use the previous encoded frame stats michael@0: xd->mi_8x8[0]->mbmi.skip_coeff = 0; michael@0: michael@0: x->source_variance = get_sby_perpixel_variance(cpi, x, bsize); michael@0: michael@0: if (cpi->oxcf.aq_mode == VARIANCE_AQ) { michael@0: int energy; michael@0: if (bsize <= BLOCK_16X16) { michael@0: energy = x->mb_energy; michael@0: } else { michael@0: energy = vp9_block_energy(cpi, x, bsize); michael@0: } michael@0: michael@0: xd->mi_8x8[0]->mbmi.segment_id = vp9_vaq_segment_id(energy); michael@0: rdmult_ratio = vp9_vaq_rdmult_ratio(energy); michael@0: vp9_mb_init_quantizer(cpi, x); michael@0: } michael@0: michael@0: if (cpi->oxcf.tuning == VP8_TUNE_SSIM) michael@0: vp9_activity_masking(cpi, x); michael@0: michael@0: if (cpi->oxcf.aq_mode == VARIANCE_AQ) { michael@0: vp9_clear_system_state(); // __asm emms; michael@0: x->rdmult = round(x->rdmult * rdmult_ratio); michael@0: } michael@0: michael@0: // Find best coding mode & reconstruct the MB so it is available michael@0: // as a predictor for MBs that follow in the SB michael@0: if (frame_is_intra_only(cm)) { michael@0: vp9_rd_pick_intra_mode_sb(cpi, x, totalrate, totaldist, bsize, ctx, michael@0: best_rd); michael@0: } else { michael@0: if (bsize >= BLOCK_8X8) michael@0: vp9_rd_pick_inter_mode_sb(cpi, x, tile, mi_row, mi_col, michael@0: totalrate, totaldist, bsize, ctx, best_rd); michael@0: else michael@0: vp9_rd_pick_inter_mode_sub8x8(cpi, x, tile, mi_row, mi_col, totalrate, michael@0: totaldist, bsize, ctx, best_rd); michael@0: } michael@0: michael@0: if (cpi->oxcf.aq_mode == VARIANCE_AQ) { michael@0: x->rdmult = orig_rdmult; michael@0: if (*totalrate != INT_MAX) { michael@0: vp9_clear_system_state(); // __asm emms; michael@0: *totalrate = round(*totalrate * rdmult_ratio); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void update_stats(VP9_COMP *cpi) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCK *const x = &cpi->mb; michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: MODE_INFO *mi = xd->mi_8x8[0]; michael@0: MB_MODE_INFO *const mbmi = &mi->mbmi; michael@0: michael@0: if (!frame_is_intra_only(cm)) { michael@0: const int seg_ref_active = vp9_segfeature_active(&cm->seg, mbmi->segment_id, michael@0: SEG_LVL_REF_FRAME); michael@0: michael@0: if (!seg_ref_active) michael@0: cpi->intra_inter_count[vp9_get_pred_context_intra_inter(xd)] michael@0: [is_inter_block(mbmi)]++; michael@0: michael@0: // If the segment reference feature is enabled we have only a single michael@0: // reference frame allowed for the segment so exclude it from michael@0: // the reference frame counts used to work out probabilities. michael@0: if (is_inter_block(mbmi) && !seg_ref_active) { michael@0: if (cm->comp_pred_mode == HYBRID_PREDICTION) michael@0: cpi->comp_inter_count[vp9_get_pred_context_comp_inter_inter(cm, xd)] michael@0: [has_second_ref(mbmi)]++; michael@0: michael@0: if (has_second_ref(mbmi)) { michael@0: cpi->comp_ref_count[vp9_get_pred_context_comp_ref_p(cm, xd)] michael@0: [mbmi->ref_frame[0] == GOLDEN_FRAME]++; michael@0: } else { michael@0: cpi->single_ref_count[vp9_get_pred_context_single_ref_p1(xd)][0] michael@0: [mbmi->ref_frame[0] != LAST_FRAME]++; michael@0: if (mbmi->ref_frame[0] != LAST_FRAME) michael@0: cpi->single_ref_count[vp9_get_pred_context_single_ref_p2(xd)][1] michael@0: [mbmi->ref_frame[0] != GOLDEN_FRAME]++; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: static BLOCK_SIZE *get_sb_partitioning(MACROBLOCK *x, BLOCK_SIZE bsize) { michael@0: switch (bsize) { michael@0: case BLOCK_64X64: michael@0: return &x->sb64_partitioning; michael@0: case BLOCK_32X32: michael@0: return &x->sb_partitioning[x->sb_index]; michael@0: case BLOCK_16X16: michael@0: return &x->mb_partitioning[x->sb_index][x->mb_index]; michael@0: case BLOCK_8X8: michael@0: return &x->b_partitioning[x->sb_index][x->mb_index][x->b_index]; michael@0: default: michael@0: assert(0); michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: static void restore_context(VP9_COMP *cpi, int mi_row, int mi_col, michael@0: ENTROPY_CONTEXT a[16 * MAX_MB_PLANE], michael@0: ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], michael@0: PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8], michael@0: BLOCK_SIZE bsize) { michael@0: MACROBLOCK *const x = &cpi->mb; michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: int p; michael@0: const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize]; michael@0: const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize]; michael@0: int mi_width = num_8x8_blocks_wide_lookup[bsize]; michael@0: int mi_height = num_8x8_blocks_high_lookup[bsize]; michael@0: for (p = 0; p < MAX_MB_PLANE; p++) { michael@0: vpx_memcpy( michael@0: cpi->above_context[p] + ((mi_col * 2) >> xd->plane[p].subsampling_x), michael@0: a + num_4x4_blocks_wide * p, michael@0: (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >> michael@0: xd->plane[p].subsampling_x); michael@0: vpx_memcpy( michael@0: cpi->left_context[p] michael@0: + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y), michael@0: l + num_4x4_blocks_high * p, michael@0: (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >> michael@0: xd->plane[p].subsampling_y); michael@0: } michael@0: vpx_memcpy(cpi->above_seg_context + mi_col, sa, michael@0: sizeof(*cpi->above_seg_context) * mi_width); michael@0: vpx_memcpy(cpi->left_seg_context + (mi_row & MI_MASK), sl, michael@0: sizeof(cpi->left_seg_context[0]) * mi_height); michael@0: } michael@0: static void save_context(VP9_COMP *cpi, int mi_row, int mi_col, michael@0: ENTROPY_CONTEXT a[16 * MAX_MB_PLANE], michael@0: ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], michael@0: PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8], michael@0: BLOCK_SIZE bsize) { michael@0: const MACROBLOCK *const x = &cpi->mb; michael@0: const MACROBLOCKD *const xd = &x->e_mbd; michael@0: int p; michael@0: const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize]; michael@0: const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize]; michael@0: int mi_width = num_8x8_blocks_wide_lookup[bsize]; michael@0: int mi_height = num_8x8_blocks_high_lookup[bsize]; michael@0: michael@0: // buffer the above/left context information of the block in search. michael@0: for (p = 0; p < MAX_MB_PLANE; ++p) { michael@0: vpx_memcpy( michael@0: a + num_4x4_blocks_wide * p, michael@0: cpi->above_context[p] + (mi_col * 2 >> xd->plane[p].subsampling_x), michael@0: (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >> michael@0: xd->plane[p].subsampling_x); michael@0: vpx_memcpy( michael@0: l + num_4x4_blocks_high * p, michael@0: cpi->left_context[p] michael@0: + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y), michael@0: (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >> michael@0: xd->plane[p].subsampling_y); michael@0: } michael@0: vpx_memcpy(sa, cpi->above_seg_context + mi_col, michael@0: sizeof(*cpi->above_seg_context) * mi_width); michael@0: vpx_memcpy(sl, cpi->left_seg_context + (mi_row & MI_MASK), michael@0: sizeof(cpi->left_seg_context[0]) * mi_height); michael@0: } michael@0: michael@0: static void encode_b(VP9_COMP *cpi, const TileInfo *const tile, michael@0: TOKENEXTRA **tp, int mi_row, int mi_col, michael@0: int output_enabled, BLOCK_SIZE bsize, int sub_index) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCK *const x = &cpi->mb; michael@0: michael@0: if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) michael@0: return; michael@0: michael@0: if (sub_index != -1) michael@0: *get_sb_index(x, bsize) = sub_index; michael@0: michael@0: if (bsize < BLOCK_8X8) { michael@0: // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0 michael@0: // there is nothing to be done. michael@0: if (x->ab_index > 0) michael@0: return; michael@0: } michael@0: set_offsets(cpi, tile, mi_row, mi_col, bsize); michael@0: update_state(cpi, get_block_context(x, bsize), bsize, output_enabled); michael@0: encode_superblock(cpi, tp, output_enabled, mi_row, mi_col, bsize); michael@0: michael@0: if (output_enabled) { michael@0: update_stats(cpi); michael@0: michael@0: (*tp)->token = EOSB_TOKEN; michael@0: (*tp)++; michael@0: } michael@0: } michael@0: michael@0: static void encode_sb(VP9_COMP *cpi, const TileInfo *const tile, michael@0: TOKENEXTRA **tp, int mi_row, int mi_col, michael@0: int output_enabled, BLOCK_SIZE bsize) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCK *const x = &cpi->mb; michael@0: BLOCK_SIZE c1 = BLOCK_8X8; michael@0: const int bsl = b_width_log2(bsize), bs = (1 << bsl) / 4; michael@0: int pl = 0; michael@0: PARTITION_TYPE partition; michael@0: BLOCK_SIZE subsize; michael@0: int i; michael@0: michael@0: if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) michael@0: return; michael@0: michael@0: c1 = BLOCK_4X4; michael@0: if (bsize >= BLOCK_8X8) { michael@0: pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context, michael@0: mi_row, mi_col, bsize); michael@0: c1 = *(get_sb_partitioning(x, bsize)); michael@0: } michael@0: partition = partition_lookup[bsl][c1]; michael@0: michael@0: switch (partition) { michael@0: case PARTITION_NONE: michael@0: if (output_enabled && bsize >= BLOCK_8X8) michael@0: cpi->partition_count[pl][PARTITION_NONE]++; michael@0: encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, c1, -1); michael@0: break; michael@0: case PARTITION_VERT: michael@0: if (output_enabled) michael@0: cpi->partition_count[pl][PARTITION_VERT]++; michael@0: encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, c1, 0); michael@0: encode_b(cpi, tile, tp, mi_row, mi_col + bs, output_enabled, c1, 1); michael@0: break; michael@0: case PARTITION_HORZ: michael@0: if (output_enabled) michael@0: cpi->partition_count[pl][PARTITION_HORZ]++; michael@0: encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, c1, 0); michael@0: encode_b(cpi, tile, tp, mi_row + bs, mi_col, output_enabled, c1, 1); michael@0: break; michael@0: case PARTITION_SPLIT: michael@0: subsize = get_subsize(bsize, PARTITION_SPLIT); michael@0: michael@0: if (output_enabled) michael@0: cpi->partition_count[pl][PARTITION_SPLIT]++; michael@0: michael@0: for (i = 0; i < 4; i++) { michael@0: const int x_idx = i & 1, y_idx = i >> 1; michael@0: michael@0: *get_sb_index(x, subsize) = i; michael@0: encode_sb(cpi, tile, tp, mi_row + y_idx * bs, mi_col + x_idx * bs, michael@0: output_enabled, subsize); michael@0: } michael@0: break; michael@0: default: michael@0: assert(0); michael@0: break; michael@0: } michael@0: michael@0: if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8) michael@0: update_partition_context(cpi->above_seg_context, cpi->left_seg_context, michael@0: mi_row, mi_col, c1, bsize); michael@0: } michael@0: michael@0: // Check to see if the given partition size is allowed for a specified number michael@0: // of 8x8 block rows and columns remaining in the image. michael@0: // If not then return the largest allowed partition size michael@0: static BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize, michael@0: int rows_left, int cols_left, michael@0: int *bh, int *bw) { michael@0: if ((rows_left <= 0) || (cols_left <= 0)) { michael@0: return MIN(bsize, BLOCK_8X8); michael@0: } else { michael@0: for (; bsize > 0; --bsize) { michael@0: *bh = num_8x8_blocks_high_lookup[bsize]; michael@0: *bw = num_8x8_blocks_wide_lookup[bsize]; michael@0: if ((*bh <= rows_left) && (*bw <= cols_left)) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: return bsize; michael@0: } michael@0: michael@0: // This function attempts to set all mode info entries in a given SB64 michael@0: // to the same block partition size. michael@0: // However, at the bottom and right borders of the image the requested size michael@0: // may not be allowed in which case this code attempts to choose the largest michael@0: // allowable partition. michael@0: static void set_partitioning(VP9_COMP *cpi, const TileInfo *const tile, michael@0: MODE_INFO **mi_8x8, int mi_row, int mi_col) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: BLOCK_SIZE bsize = cpi->sf.always_this_block_size; michael@0: const int mis = cm->mode_info_stride; michael@0: int row8x8_remaining = tile->mi_row_end - mi_row; michael@0: int col8x8_remaining = tile->mi_col_end - mi_col; michael@0: int block_row, block_col; michael@0: MODE_INFO * mi_upper_left = cm->mi + mi_row * mis + mi_col; michael@0: int bh = num_8x8_blocks_high_lookup[bsize]; michael@0: int bw = num_8x8_blocks_wide_lookup[bsize]; michael@0: michael@0: assert((row8x8_remaining > 0) && (col8x8_remaining > 0)); michael@0: michael@0: // Apply the requested partition size to the SB64 if it is all "in image" michael@0: if ((col8x8_remaining >= MI_BLOCK_SIZE) && michael@0: (row8x8_remaining >= MI_BLOCK_SIZE)) { michael@0: for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) { michael@0: for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) { michael@0: int index = block_row * mis + block_col; michael@0: mi_8x8[index] = mi_upper_left + index; michael@0: mi_8x8[index]->mbmi.sb_type = bsize; michael@0: } michael@0: } michael@0: } else { michael@0: // Else this is a partial SB64. michael@0: for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) { michael@0: for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) { michael@0: int index = block_row * mis + block_col; michael@0: // Find a partition size that fits michael@0: bsize = find_partition_size(cpi->sf.always_this_block_size, michael@0: (row8x8_remaining - block_row), michael@0: (col8x8_remaining - block_col), &bh, &bw); michael@0: mi_8x8[index] = mi_upper_left + index; michael@0: mi_8x8[index]->mbmi.sb_type = bsize; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void copy_partitioning(VP9_COMP *cpi, MODE_INFO **mi_8x8, michael@0: MODE_INFO **prev_mi_8x8) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: const int mis = cm->mode_info_stride; michael@0: int block_row, block_col; michael@0: michael@0: for (block_row = 0; block_row < 8; ++block_row) { michael@0: for (block_col = 0; block_col < 8; ++block_col) { michael@0: MODE_INFO * prev_mi = prev_mi_8x8[block_row * mis + block_col]; michael@0: BLOCK_SIZE sb_type = prev_mi ? prev_mi->mbmi.sb_type : 0; michael@0: ptrdiff_t offset; michael@0: michael@0: if (prev_mi) { michael@0: offset = prev_mi - cm->prev_mi; michael@0: mi_8x8[block_row * mis + block_col] = cm->mi + offset; michael@0: mi_8x8[block_row * mis + block_col]->mbmi.sb_type = sb_type; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: static int sb_has_motion(VP9_COMP *cpi, MODE_INFO **prev_mi_8x8) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: const int mis = cm->mode_info_stride; michael@0: int block_row, block_col; michael@0: michael@0: if (cm->prev_mi) { michael@0: for (block_row = 0; block_row < 8; ++block_row) { michael@0: for (block_col = 0; block_col < 8; ++block_col) { michael@0: MODE_INFO * prev_mi = prev_mi_8x8[block_row * mis + block_col]; michael@0: if (prev_mi) { michael@0: if (abs(prev_mi->mbmi.mv[0].as_mv.row) >= 8 || michael@0: abs(prev_mi->mbmi.mv[0].as_mv.col) >= 8) michael@0: return 1; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: static void rd_use_partition(VP9_COMP *cpi, michael@0: const TileInfo *const tile, michael@0: MODE_INFO **mi_8x8, michael@0: TOKENEXTRA **tp, int mi_row, int mi_col, michael@0: BLOCK_SIZE bsize, int *rate, int64_t *dist, michael@0: int do_recon) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCK *const x = &cpi->mb; michael@0: const int mis = cm->mode_info_stride; michael@0: int bsl = b_width_log2(bsize); michael@0: const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize]; michael@0: const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize]; michael@0: int ms = num_4x4_blocks_wide / 2; michael@0: int mh = num_4x4_blocks_high / 2; michael@0: int bss = (1 << bsl) / 4; michael@0: int i, pl; michael@0: PARTITION_TYPE partition = PARTITION_NONE; michael@0: BLOCK_SIZE subsize; michael@0: ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE]; michael@0: PARTITION_CONTEXT sl[8], sa[8]; michael@0: int last_part_rate = INT_MAX; michael@0: int64_t last_part_dist = INT_MAX; michael@0: int split_rate = INT_MAX; michael@0: int64_t split_dist = INT_MAX; michael@0: int none_rate = INT_MAX; michael@0: int64_t none_dist = INT_MAX; michael@0: int chosen_rate = INT_MAX; michael@0: int64_t chosen_dist = INT_MAX; michael@0: BLOCK_SIZE sub_subsize = BLOCK_4X4; michael@0: int splits_below = 0; michael@0: BLOCK_SIZE bs_type = mi_8x8[0]->mbmi.sb_type; michael@0: michael@0: if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) michael@0: return; michael@0: michael@0: partition = partition_lookup[bsl][bs_type]; michael@0: michael@0: subsize = get_subsize(bsize, partition); michael@0: michael@0: if (bsize < BLOCK_8X8) { michael@0: // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0 michael@0: // there is nothing to be done. michael@0: if (x->ab_index != 0) { michael@0: *rate = 0; michael@0: *dist = 0; michael@0: return; michael@0: } michael@0: } else { michael@0: *(get_sb_partitioning(x, bsize)) = subsize; michael@0: } michael@0: save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize); michael@0: michael@0: if (bsize == BLOCK_16X16) { michael@0: set_offsets(cpi, tile, mi_row, mi_col, bsize); michael@0: x->mb_energy = vp9_block_energy(cpi, x, bsize); michael@0: } michael@0: michael@0: x->fast_ms = 0; michael@0: x->subblock_ref = 0; michael@0: michael@0: if (cpi->sf.adjust_partitioning_from_last_frame) { michael@0: // Check if any of the sub blocks are further split. michael@0: if (partition == PARTITION_SPLIT && subsize > BLOCK_8X8) { michael@0: sub_subsize = get_subsize(subsize, PARTITION_SPLIT); michael@0: splits_below = 1; michael@0: for (i = 0; i < 4; i++) { michael@0: int jj = i >> 1, ii = i & 0x01; michael@0: MODE_INFO * this_mi = mi_8x8[jj * bss * mis + ii * bss]; michael@0: if (this_mi && this_mi->mbmi.sb_type >= sub_subsize) { michael@0: splits_below = 0; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // If partition is not none try none unless each of the 4 splits are split michael@0: // even further.. michael@0: if (partition != PARTITION_NONE && !splits_below && michael@0: mi_row + (ms >> 1) < cm->mi_rows && michael@0: mi_col + (ms >> 1) < cm->mi_cols) { michael@0: *(get_sb_partitioning(x, bsize)) = bsize; michael@0: pick_sb_modes(cpi, tile, mi_row, mi_col, &none_rate, &none_dist, bsize, michael@0: get_block_context(x, bsize), INT64_MAX); michael@0: michael@0: pl = partition_plane_context(cpi->above_seg_context, michael@0: cpi->left_seg_context, michael@0: mi_row, mi_col, bsize); michael@0: none_rate += x->partition_cost[pl][PARTITION_NONE]; michael@0: michael@0: restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize); michael@0: mi_8x8[0]->mbmi.sb_type = bs_type; michael@0: *(get_sb_partitioning(x, bsize)) = subsize; michael@0: } michael@0: } michael@0: michael@0: switch (partition) { michael@0: case PARTITION_NONE: michael@0: pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist, michael@0: bsize, get_block_context(x, bsize), INT64_MAX); michael@0: break; michael@0: case PARTITION_HORZ: michael@0: *get_sb_index(x, subsize) = 0; michael@0: pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist, michael@0: subsize, get_block_context(x, subsize), INT64_MAX); michael@0: if (last_part_rate != INT_MAX && michael@0: bsize >= BLOCK_8X8 && mi_row + (mh >> 1) < cm->mi_rows) { michael@0: int rt = 0; michael@0: int64_t dt = 0; michael@0: update_state(cpi, get_block_context(x, subsize), subsize, 0); michael@0: encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize); michael@0: *get_sb_index(x, subsize) = 1; michael@0: pick_sb_modes(cpi, tile, mi_row + (ms >> 1), mi_col, &rt, &dt, subsize, michael@0: get_block_context(x, subsize), INT64_MAX); michael@0: if (rt == INT_MAX || dt == INT_MAX) { michael@0: last_part_rate = INT_MAX; michael@0: last_part_dist = INT_MAX; michael@0: break; michael@0: } michael@0: michael@0: last_part_rate += rt; michael@0: last_part_dist += dt; michael@0: } michael@0: break; michael@0: case PARTITION_VERT: michael@0: *get_sb_index(x, subsize) = 0; michael@0: pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist, michael@0: subsize, get_block_context(x, subsize), INT64_MAX); michael@0: if (last_part_rate != INT_MAX && michael@0: bsize >= BLOCK_8X8 && mi_col + (ms >> 1) < cm->mi_cols) { michael@0: int rt = 0; michael@0: int64_t dt = 0; michael@0: update_state(cpi, get_block_context(x, subsize), subsize, 0); michael@0: encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize); michael@0: *get_sb_index(x, subsize) = 1; michael@0: pick_sb_modes(cpi, tile, mi_row, mi_col + (ms >> 1), &rt, &dt, subsize, michael@0: get_block_context(x, subsize), INT64_MAX); michael@0: if (rt == INT_MAX || dt == INT_MAX) { michael@0: last_part_rate = INT_MAX; michael@0: last_part_dist = INT_MAX; michael@0: break; michael@0: } michael@0: last_part_rate += rt; michael@0: last_part_dist += dt; michael@0: } michael@0: break; michael@0: case PARTITION_SPLIT: michael@0: // Split partition. michael@0: last_part_rate = 0; michael@0: last_part_dist = 0; michael@0: for (i = 0; i < 4; i++) { michael@0: int x_idx = (i & 1) * (ms >> 1); michael@0: int y_idx = (i >> 1) * (ms >> 1); michael@0: int jj = i >> 1, ii = i & 0x01; michael@0: int rt; michael@0: int64_t dt; michael@0: michael@0: if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols)) michael@0: continue; michael@0: michael@0: *get_sb_index(x, subsize) = i; michael@0: michael@0: rd_use_partition(cpi, tile, mi_8x8 + jj * bss * mis + ii * bss, tp, michael@0: mi_row + y_idx, mi_col + x_idx, subsize, &rt, &dt, michael@0: i != 3); michael@0: if (rt == INT_MAX || dt == INT_MAX) { michael@0: last_part_rate = INT_MAX; michael@0: last_part_dist = INT_MAX; michael@0: break; michael@0: } michael@0: last_part_rate += rt; michael@0: last_part_dist += dt; michael@0: } michael@0: break; michael@0: default: michael@0: assert(0); michael@0: } michael@0: michael@0: pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context, michael@0: mi_row, mi_col, bsize); michael@0: if (last_part_rate < INT_MAX) michael@0: last_part_rate += x->partition_cost[pl][partition]; michael@0: michael@0: if (cpi->sf.adjust_partitioning_from_last_frame michael@0: && partition != PARTITION_SPLIT && bsize > BLOCK_8X8 michael@0: && (mi_row + ms < cm->mi_rows || mi_row + (ms >> 1) == cm->mi_rows) michael@0: && (mi_col + ms < cm->mi_cols || mi_col + (ms >> 1) == cm->mi_cols)) { michael@0: BLOCK_SIZE split_subsize = get_subsize(bsize, PARTITION_SPLIT); michael@0: split_rate = 0; michael@0: split_dist = 0; michael@0: restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize); michael@0: michael@0: // Split partition. michael@0: for (i = 0; i < 4; i++) { michael@0: int x_idx = (i & 1) * (num_4x4_blocks_wide >> 2); michael@0: int y_idx = (i >> 1) * (num_4x4_blocks_wide >> 2); michael@0: int rt = 0; michael@0: int64_t dt = 0; michael@0: ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE]; michael@0: PARTITION_CONTEXT sl[8], sa[8]; michael@0: michael@0: if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols)) michael@0: continue; michael@0: michael@0: *get_sb_index(x, split_subsize) = i; michael@0: *get_sb_partitioning(x, bsize) = split_subsize; michael@0: *get_sb_partitioning(x, split_subsize) = split_subsize; michael@0: michael@0: save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize); michael@0: michael@0: pick_sb_modes(cpi, tile, mi_row + y_idx, mi_col + x_idx, &rt, &dt, michael@0: split_subsize, get_block_context(x, split_subsize), michael@0: INT64_MAX); michael@0: michael@0: restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize); michael@0: michael@0: if (rt == INT_MAX || dt == INT_MAX) { michael@0: split_rate = INT_MAX; michael@0: split_dist = INT_MAX; michael@0: break; michael@0: } michael@0: michael@0: if (i != 3) michael@0: encode_sb(cpi, tile, tp, mi_row + y_idx, mi_col + x_idx, 0, michael@0: split_subsize); michael@0: michael@0: split_rate += rt; michael@0: split_dist += dt; michael@0: pl = partition_plane_context(cpi->above_seg_context, michael@0: cpi->left_seg_context, michael@0: mi_row + y_idx, mi_col + x_idx, bsize); michael@0: split_rate += x->partition_cost[pl][PARTITION_NONE]; michael@0: } michael@0: pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context, michael@0: mi_row, mi_col, bsize); michael@0: if (split_rate < INT_MAX) { michael@0: split_rate += x->partition_cost[pl][PARTITION_SPLIT]; michael@0: michael@0: chosen_rate = split_rate; michael@0: chosen_dist = split_dist; michael@0: } michael@0: } michael@0: michael@0: // If last_part is better set the partitioning to that... michael@0: if (RDCOST(x->rdmult, x->rddiv, last_part_rate, last_part_dist) michael@0: < RDCOST(x->rdmult, x->rddiv, chosen_rate, chosen_dist)) { michael@0: mi_8x8[0]->mbmi.sb_type = bsize; michael@0: if (bsize >= BLOCK_8X8) michael@0: *(get_sb_partitioning(x, bsize)) = subsize; michael@0: chosen_rate = last_part_rate; michael@0: chosen_dist = last_part_dist; michael@0: } michael@0: // If none was better set the partitioning to that... michael@0: if (RDCOST(x->rdmult, x->rddiv, chosen_rate, chosen_dist) michael@0: > RDCOST(x->rdmult, x->rddiv, none_rate, none_dist)) { michael@0: if (bsize >= BLOCK_8X8) michael@0: *(get_sb_partitioning(x, bsize)) = bsize; michael@0: chosen_rate = none_rate; michael@0: chosen_dist = none_dist; michael@0: } michael@0: michael@0: restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize); michael@0: michael@0: // We must have chosen a partitioning and encoding or we'll fail later on. michael@0: // No other opportunities for success. michael@0: if ( bsize == BLOCK_64X64) michael@0: assert(chosen_rate < INT_MAX && chosen_dist < INT_MAX); michael@0: michael@0: if (do_recon) michael@0: encode_sb(cpi, tile, tp, mi_row, mi_col, bsize == BLOCK_64X64, bsize); michael@0: michael@0: *rate = chosen_rate; michael@0: *dist = chosen_dist; michael@0: } michael@0: michael@0: static const BLOCK_SIZE min_partition_size[BLOCK_SIZES] = { michael@0: BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, michael@0: BLOCK_4X4, BLOCK_4X4, BLOCK_8X8, BLOCK_8X8, michael@0: BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16 michael@0: }; michael@0: michael@0: static const BLOCK_SIZE max_partition_size[BLOCK_SIZES] = { michael@0: BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, michael@0: BLOCK_32X32, BLOCK_32X32, BLOCK_32X32, BLOCK_64X64, michael@0: BLOCK_64X64, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64 michael@0: }; michael@0: michael@0: // Look at all the mode_info entries for blocks that are part of this michael@0: // partition and find the min and max values for sb_type. michael@0: // At the moment this is designed to work on a 64x64 SB but could be michael@0: // adjusted to use a size parameter. michael@0: // michael@0: // The min and max are assumed to have been initialized prior to calling this michael@0: // function so repeat calls can accumulate a min and max of more than one sb64. michael@0: static void get_sb_partition_size_range(VP9_COMP *cpi, MODE_INFO ** mi_8x8, michael@0: BLOCK_SIZE * min_block_size, michael@0: BLOCK_SIZE * max_block_size ) { michael@0: MACROBLOCKD *const xd = &cpi->mb.e_mbd; michael@0: int sb_width_in_blocks = MI_BLOCK_SIZE; michael@0: int sb_height_in_blocks = MI_BLOCK_SIZE; michael@0: int i, j; michael@0: int index = 0; michael@0: michael@0: // Check the sb_type for each block that belongs to this region. michael@0: for (i = 0; i < sb_height_in_blocks; ++i) { michael@0: for (j = 0; j < sb_width_in_blocks; ++j) { michael@0: MODE_INFO * mi = mi_8x8[index+j]; michael@0: BLOCK_SIZE sb_type = mi ? mi->mbmi.sb_type : 0; michael@0: *min_block_size = MIN(*min_block_size, sb_type); michael@0: *max_block_size = MAX(*max_block_size, sb_type); michael@0: } michael@0: index += xd->mode_info_stride; michael@0: } michael@0: } michael@0: michael@0: // Look at neighboring blocks and set a min and max partition size based on michael@0: // what they chose. michael@0: static void rd_auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile, michael@0: int row, int col, michael@0: BLOCK_SIZE *min_block_size, michael@0: BLOCK_SIZE *max_block_size) { michael@0: VP9_COMMON * const cm = &cpi->common; michael@0: MACROBLOCKD *const xd = &cpi->mb.e_mbd; michael@0: MODE_INFO ** mi_8x8 = xd->mi_8x8; michael@0: MODE_INFO ** prev_mi_8x8 = xd->prev_mi_8x8; michael@0: michael@0: const int left_in_image = xd->left_available && mi_8x8[-1]; michael@0: const int above_in_image = xd->up_available && michael@0: mi_8x8[-xd->mode_info_stride]; michael@0: MODE_INFO ** above_sb64_mi_8x8; michael@0: MODE_INFO ** left_sb64_mi_8x8; michael@0: michael@0: int row8x8_remaining = tile->mi_row_end - row; michael@0: int col8x8_remaining = tile->mi_col_end - col; michael@0: int bh, bw; michael@0: michael@0: // Trap case where we do not have a prediction. michael@0: if (!left_in_image && !above_in_image && michael@0: ((cm->frame_type == KEY_FRAME) || !cm->prev_mi)) { michael@0: *min_block_size = BLOCK_4X4; michael@0: *max_block_size = BLOCK_64X64; michael@0: } else { michael@0: // Default "min to max" and "max to min" michael@0: *min_block_size = BLOCK_64X64; michael@0: *max_block_size = BLOCK_4X4; michael@0: michael@0: // NOTE: each call to get_sb_partition_size_range() uses the previous michael@0: // passed in values for min and max as a starting point. michael@0: // michael@0: // Find the min and max partition used in previous frame at this location michael@0: if (cm->prev_mi && (cm->frame_type != KEY_FRAME)) { michael@0: get_sb_partition_size_range(cpi, prev_mi_8x8, michael@0: min_block_size, max_block_size); michael@0: } michael@0: michael@0: // Find the min and max partition sizes used in the left SB64 michael@0: if (left_in_image) { michael@0: left_sb64_mi_8x8 = &mi_8x8[-MI_BLOCK_SIZE]; michael@0: get_sb_partition_size_range(cpi, left_sb64_mi_8x8, michael@0: min_block_size, max_block_size); michael@0: } michael@0: michael@0: // Find the min and max partition sizes used in the above SB64. michael@0: if (above_in_image) { michael@0: above_sb64_mi_8x8 = &mi_8x8[-xd->mode_info_stride * MI_BLOCK_SIZE]; michael@0: get_sb_partition_size_range(cpi, above_sb64_mi_8x8, michael@0: min_block_size, max_block_size); michael@0: } michael@0: } michael@0: michael@0: // Give a bit of leaway either side of the observed min and max michael@0: *min_block_size = min_partition_size[*min_block_size]; michael@0: *max_block_size = max_partition_size[*max_block_size]; michael@0: michael@0: // Check border cases where max and min from neighbours may not be legal. michael@0: *max_block_size = find_partition_size(*max_block_size, michael@0: row8x8_remaining, col8x8_remaining, michael@0: &bh, &bw); michael@0: *min_block_size = MIN(*min_block_size, *max_block_size); michael@0: } michael@0: michael@0: static void compute_fast_motion_search_level(VP9_COMP *cpi, BLOCK_SIZE bsize) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCK *const x = &cpi->mb; michael@0: michael@0: // Only use 8x8 result for non HD videos. michael@0: // int use_8x8 = (MIN(cpi->common.width, cpi->common.height) < 720) ? 1 : 0; michael@0: int use_8x8 = 1; michael@0: michael@0: if (cm->frame_type && !cpi->is_src_frame_alt_ref && michael@0: ((use_8x8 && bsize == BLOCK_16X16) || michael@0: bsize == BLOCK_32X32 || bsize == BLOCK_64X64)) { michael@0: int ref0 = 0, ref1 = 0, ref2 = 0, ref3 = 0; michael@0: PICK_MODE_CONTEXT *block_context = NULL; michael@0: michael@0: if (bsize == BLOCK_16X16) { michael@0: block_context = x->sb8x8_context[x->sb_index][x->mb_index]; michael@0: } else if (bsize == BLOCK_32X32) { michael@0: block_context = x->mb_context[x->sb_index]; michael@0: } else if (bsize == BLOCK_64X64) { michael@0: block_context = x->sb32_context; michael@0: } michael@0: michael@0: if (block_context) { michael@0: ref0 = block_context[0].mic.mbmi.ref_frame[0]; michael@0: ref1 = block_context[1].mic.mbmi.ref_frame[0]; michael@0: ref2 = block_context[2].mic.mbmi.ref_frame[0]; michael@0: ref3 = block_context[3].mic.mbmi.ref_frame[0]; michael@0: } michael@0: michael@0: // Currently, only consider 4 inter reference frames. michael@0: if (ref0 && ref1 && ref2 && ref3) { michael@0: int d01, d23, d02, d13; michael@0: michael@0: // Motion vectors for the four subblocks. michael@0: int16_t mvr0 = block_context[0].mic.mbmi.mv[0].as_mv.row; michael@0: int16_t mvc0 = block_context[0].mic.mbmi.mv[0].as_mv.col; michael@0: int16_t mvr1 = block_context[1].mic.mbmi.mv[0].as_mv.row; michael@0: int16_t mvc1 = block_context[1].mic.mbmi.mv[0].as_mv.col; michael@0: int16_t mvr2 = block_context[2].mic.mbmi.mv[0].as_mv.row; michael@0: int16_t mvc2 = block_context[2].mic.mbmi.mv[0].as_mv.col; michael@0: int16_t mvr3 = block_context[3].mic.mbmi.mv[0].as_mv.row; michael@0: int16_t mvc3 = block_context[3].mic.mbmi.mv[0].as_mv.col; michael@0: michael@0: // Adjust sign if ref is alt_ref. michael@0: if (cm->ref_frame_sign_bias[ref0]) { michael@0: mvr0 *= -1; michael@0: mvc0 *= -1; michael@0: } michael@0: michael@0: if (cm->ref_frame_sign_bias[ref1]) { michael@0: mvr1 *= -1; michael@0: mvc1 *= -1; michael@0: } michael@0: michael@0: if (cm->ref_frame_sign_bias[ref2]) { michael@0: mvr2 *= -1; michael@0: mvc2 *= -1; michael@0: } michael@0: michael@0: if (cm->ref_frame_sign_bias[ref3]) { michael@0: mvr3 *= -1; michael@0: mvc3 *= -1; michael@0: } michael@0: michael@0: // Calculate mv distances. michael@0: d01 = MAX(abs(mvr0 - mvr1), abs(mvc0 - mvc1)); michael@0: d23 = MAX(abs(mvr2 - mvr3), abs(mvc2 - mvc3)); michael@0: d02 = MAX(abs(mvr0 - mvr2), abs(mvc0 - mvc2)); michael@0: d13 = MAX(abs(mvr1 - mvr3), abs(mvc1 - mvc3)); michael@0: michael@0: if (d01 < FAST_MOTION_MV_THRESH && d23 < FAST_MOTION_MV_THRESH && michael@0: d02 < FAST_MOTION_MV_THRESH && d13 < FAST_MOTION_MV_THRESH) { michael@0: // Set fast motion search level. michael@0: x->fast_ms = 1; michael@0: michael@0: if (ref0 == ref1 && ref1 == ref2 && ref2 == ref3 && michael@0: d01 < 2 && d23 < 2 && d02 < 2 && d13 < 2) { michael@0: // Set fast motion search level. michael@0: x->fast_ms = 2; michael@0: michael@0: if (!d01 && !d23 && !d02 && !d13) { michael@0: x->fast_ms = 3; michael@0: x->subblock_ref = ref0; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: static INLINE void store_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) { michael@0: vpx_memcpy(ctx->pred_mv, x->pred_mv, sizeof(x->pred_mv)); michael@0: } michael@0: michael@0: static INLINE void load_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) { michael@0: vpx_memcpy(x->pred_mv, ctx->pred_mv, sizeof(x->pred_mv)); michael@0: } michael@0: michael@0: // TODO(jingning,jimbankoski,rbultje): properly skip partition types that are michael@0: // unlikely to be selected depending on previous rate-distortion optimization michael@0: // results, for encoding speed-up. michael@0: static void rd_pick_partition(VP9_COMP *cpi, const TileInfo *const tile, michael@0: TOKENEXTRA **tp, int mi_row, michael@0: int mi_col, BLOCK_SIZE bsize, int *rate, michael@0: int64_t *dist, int do_recon, int64_t best_rd) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCK *const x = &cpi->mb; michael@0: const int ms = num_8x8_blocks_wide_lookup[bsize] / 2; michael@0: ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE]; michael@0: PARTITION_CONTEXT sl[8], sa[8]; michael@0: TOKENEXTRA *tp_orig = *tp; michael@0: int i, pl; michael@0: BLOCK_SIZE subsize; michael@0: int this_rate, sum_rate = 0, best_rate = INT_MAX; michael@0: int64_t this_dist, sum_dist = 0, best_dist = INT64_MAX; michael@0: int64_t sum_rd = 0; michael@0: int do_split = bsize >= BLOCK_8X8; michael@0: int do_rect = 1; michael@0: // Override skipping rectangular partition operations for edge blocks michael@0: const int force_horz_split = (mi_row + ms >= cm->mi_rows); michael@0: const int force_vert_split = (mi_col + ms >= cm->mi_cols); michael@0: michael@0: int partition_none_allowed = !force_horz_split && !force_vert_split; michael@0: int partition_horz_allowed = !force_vert_split && bsize >= BLOCK_8X8; michael@0: int partition_vert_allowed = !force_horz_split && bsize >= BLOCK_8X8; michael@0: michael@0: int partition_split_done = 0; michael@0: (void) *tp_orig; michael@0: michael@0: if (bsize < BLOCK_8X8) { michael@0: // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0 michael@0: // there is nothing to be done. michael@0: if (x->ab_index != 0) { michael@0: *rate = 0; michael@0: *dist = 0; michael@0: return; michael@0: } michael@0: } michael@0: assert(num_8x8_blocks_wide_lookup[bsize] == michael@0: num_8x8_blocks_high_lookup[bsize]); michael@0: michael@0: if (bsize == BLOCK_16X16) { michael@0: set_offsets(cpi, tile, mi_row, mi_col, bsize); michael@0: x->mb_energy = vp9_block_energy(cpi, x, bsize); michael@0: } michael@0: michael@0: // Determine partition types in search according to the speed features. michael@0: // The threshold set here has to be of square block size. michael@0: if (cpi->sf.auto_min_max_partition_size) { michael@0: partition_none_allowed &= (bsize <= cpi->sf.max_partition_size && michael@0: bsize >= cpi->sf.min_partition_size); michael@0: partition_horz_allowed &= ((bsize <= cpi->sf.max_partition_size && michael@0: bsize > cpi->sf.min_partition_size) || michael@0: force_horz_split); michael@0: partition_vert_allowed &= ((bsize <= cpi->sf.max_partition_size && michael@0: bsize > cpi->sf.min_partition_size) || michael@0: force_vert_split); michael@0: do_split &= bsize > cpi->sf.min_partition_size; michael@0: } michael@0: if (cpi->sf.use_square_partition_only) { michael@0: partition_horz_allowed &= force_horz_split; michael@0: partition_vert_allowed &= force_vert_split; michael@0: } michael@0: michael@0: save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize); michael@0: michael@0: if (cpi->sf.disable_split_var_thresh && partition_none_allowed) { michael@0: unsigned int source_variancey; michael@0: vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col); michael@0: source_variancey = get_sby_perpixel_variance(cpi, x, bsize); michael@0: if (source_variancey < cpi->sf.disable_split_var_thresh) { michael@0: do_split = 0; michael@0: if (source_variancey < cpi->sf.disable_split_var_thresh / 2) michael@0: do_rect = 0; michael@0: } michael@0: } michael@0: michael@0: // PARTITION_NONE michael@0: if (partition_none_allowed) { michael@0: pick_sb_modes(cpi, tile, mi_row, mi_col, &this_rate, &this_dist, bsize, michael@0: get_block_context(x, bsize), best_rd); michael@0: if (this_rate != INT_MAX) { michael@0: if (bsize >= BLOCK_8X8) { michael@0: pl = partition_plane_context(cpi->above_seg_context, michael@0: cpi->left_seg_context, michael@0: mi_row, mi_col, bsize); michael@0: this_rate += x->partition_cost[pl][PARTITION_NONE]; michael@0: } michael@0: sum_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_dist); michael@0: if (sum_rd < best_rd) { michael@0: int64_t stop_thresh = 2048; michael@0: michael@0: best_rate = this_rate; michael@0: best_dist = this_dist; michael@0: best_rd = sum_rd; michael@0: if (bsize >= BLOCK_8X8) michael@0: *(get_sb_partitioning(x, bsize)) = bsize; michael@0: michael@0: // Adjust threshold according to partition size. michael@0: stop_thresh >>= 8 - (b_width_log2_lookup[bsize] + michael@0: b_height_log2_lookup[bsize]); michael@0: michael@0: // If obtained distortion is very small, choose current partition michael@0: // and stop splitting. michael@0: if (this_dist < stop_thresh) { michael@0: do_split = 0; michael@0: do_rect = 0; michael@0: } michael@0: } michael@0: } michael@0: restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize); michael@0: } michael@0: michael@0: // store estimated motion vector michael@0: if (cpi->sf.adaptive_motion_search) michael@0: store_pred_mv(x, get_block_context(x, bsize)); michael@0: michael@0: // PARTITION_SPLIT michael@0: sum_rd = 0; michael@0: // TODO(jingning): use the motion vectors given by the above search as michael@0: // the starting point of motion search in the following partition type check. michael@0: if (do_split) { michael@0: subsize = get_subsize(bsize, PARTITION_SPLIT); michael@0: for (i = 0; i < 4 && sum_rd < best_rd; ++i) { michael@0: const int x_idx = (i & 1) * ms; michael@0: const int y_idx = (i >> 1) * ms; michael@0: michael@0: if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols) michael@0: continue; michael@0: michael@0: *get_sb_index(x, subsize) = i; michael@0: if (cpi->sf.adaptive_motion_search) michael@0: load_pred_mv(x, get_block_context(x, bsize)); michael@0: rd_pick_partition(cpi, tile, tp, mi_row + y_idx, mi_col + x_idx, subsize, michael@0: &this_rate, &this_dist, i != 3, best_rd - sum_rd); michael@0: michael@0: if (this_rate == INT_MAX) { michael@0: sum_rd = INT64_MAX; michael@0: } else { michael@0: sum_rate += this_rate; michael@0: sum_dist += this_dist; michael@0: sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist); michael@0: } michael@0: } michael@0: if (sum_rd < best_rd && i == 4) { michael@0: pl = partition_plane_context(cpi->above_seg_context, michael@0: cpi->left_seg_context, michael@0: mi_row, mi_col, bsize); michael@0: sum_rate += x->partition_cost[pl][PARTITION_SPLIT]; michael@0: sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist); michael@0: if (sum_rd < best_rd) { michael@0: best_rate = sum_rate; michael@0: best_dist = sum_dist; michael@0: best_rd = sum_rd; michael@0: *(get_sb_partitioning(x, bsize)) = subsize; michael@0: } michael@0: } else { michael@0: // skip rectangular partition test when larger block size michael@0: // gives better rd cost michael@0: if (cpi->sf.less_rectangular_check) michael@0: do_rect &= !partition_none_allowed; michael@0: } michael@0: partition_split_done = 1; michael@0: restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize); michael@0: } michael@0: michael@0: x->fast_ms = 0; michael@0: x->subblock_ref = 0; michael@0: michael@0: if (partition_split_done && michael@0: cpi->sf.using_small_partition_info) { michael@0: compute_fast_motion_search_level(cpi, bsize); michael@0: } michael@0: michael@0: // PARTITION_HORZ michael@0: if (partition_horz_allowed && do_rect) { michael@0: subsize = get_subsize(bsize, PARTITION_HORZ); michael@0: *get_sb_index(x, subsize) = 0; michael@0: if (cpi->sf.adaptive_motion_search) michael@0: load_pred_mv(x, get_block_context(x, bsize)); michael@0: pick_sb_modes(cpi, tile, mi_row, mi_col, &sum_rate, &sum_dist, subsize, michael@0: get_block_context(x, subsize), best_rd); michael@0: sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist); michael@0: michael@0: if (sum_rd < best_rd && mi_row + ms < cm->mi_rows) { michael@0: update_state(cpi, get_block_context(x, subsize), subsize, 0); michael@0: encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize); michael@0: michael@0: *get_sb_index(x, subsize) = 1; michael@0: if (cpi->sf.adaptive_motion_search) michael@0: load_pred_mv(x, get_block_context(x, bsize)); michael@0: pick_sb_modes(cpi, tile, mi_row + ms, mi_col, &this_rate, michael@0: &this_dist, subsize, get_block_context(x, subsize), michael@0: best_rd - sum_rd); michael@0: if (this_rate == INT_MAX) { michael@0: sum_rd = INT64_MAX; michael@0: } else { michael@0: sum_rate += this_rate; michael@0: sum_dist += this_dist; michael@0: sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist); michael@0: } michael@0: } michael@0: if (sum_rd < best_rd) { michael@0: pl = partition_plane_context(cpi->above_seg_context, michael@0: cpi->left_seg_context, michael@0: mi_row, mi_col, bsize); michael@0: sum_rate += x->partition_cost[pl][PARTITION_HORZ]; michael@0: sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist); michael@0: if (sum_rd < best_rd) { michael@0: best_rd = sum_rd; michael@0: best_rate = sum_rate; michael@0: best_dist = sum_dist; michael@0: *(get_sb_partitioning(x, bsize)) = subsize; michael@0: } michael@0: } michael@0: restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize); michael@0: } michael@0: michael@0: // PARTITION_VERT michael@0: if (partition_vert_allowed && do_rect) { michael@0: subsize = get_subsize(bsize, PARTITION_VERT); michael@0: michael@0: *get_sb_index(x, subsize) = 0; michael@0: if (cpi->sf.adaptive_motion_search) michael@0: load_pred_mv(x, get_block_context(x, bsize)); michael@0: pick_sb_modes(cpi, tile, mi_row, mi_col, &sum_rate, &sum_dist, subsize, michael@0: get_block_context(x, subsize), best_rd); michael@0: sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist); michael@0: if (sum_rd < best_rd && mi_col + ms < cm->mi_cols) { michael@0: update_state(cpi, get_block_context(x, subsize), subsize, 0); michael@0: encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize); michael@0: michael@0: *get_sb_index(x, subsize) = 1; michael@0: if (cpi->sf.adaptive_motion_search) michael@0: load_pred_mv(x, get_block_context(x, bsize)); michael@0: pick_sb_modes(cpi, tile, mi_row, mi_col + ms, &this_rate, michael@0: &this_dist, subsize, get_block_context(x, subsize), michael@0: best_rd - sum_rd); michael@0: if (this_rate == INT_MAX) { michael@0: sum_rd = INT64_MAX; michael@0: } else { michael@0: sum_rate += this_rate; michael@0: sum_dist += this_dist; michael@0: sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist); michael@0: } michael@0: } michael@0: if (sum_rd < best_rd) { michael@0: pl = partition_plane_context(cpi->above_seg_context, michael@0: cpi->left_seg_context, michael@0: mi_row, mi_col, bsize); michael@0: sum_rate += x->partition_cost[pl][PARTITION_VERT]; michael@0: sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist); michael@0: if (sum_rd < best_rd) { michael@0: best_rate = sum_rate; michael@0: best_dist = sum_dist; michael@0: best_rd = sum_rd; michael@0: *(get_sb_partitioning(x, bsize)) = subsize; michael@0: } michael@0: } michael@0: restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize); michael@0: } michael@0: michael@0: michael@0: *rate = best_rate; michael@0: *dist = best_dist; michael@0: michael@0: if (best_rate < INT_MAX && best_dist < INT64_MAX && do_recon) michael@0: encode_sb(cpi, tile, tp, mi_row, mi_col, bsize == BLOCK_64X64, bsize); michael@0: if (bsize == BLOCK_64X64) { michael@0: assert(tp_orig < *tp); michael@0: assert(best_rate < INT_MAX); michael@0: assert(best_dist < INT_MAX); michael@0: } else { michael@0: assert(tp_orig == *tp); michael@0: } michael@0: } michael@0: michael@0: // Examines 64x64 block and chooses a best reference frame michael@0: static void rd_pick_reference_frame(VP9_COMP *cpi, const TileInfo *const tile, michael@0: int mi_row, int mi_col) { michael@0: VP9_COMMON * const cm = &cpi->common; michael@0: MACROBLOCK * const x = &cpi->mb; michael@0: int bsl = b_width_log2(BLOCK_64X64), bs = 1 << bsl; michael@0: int ms = bs / 2; michael@0: ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE]; michael@0: PARTITION_CONTEXT sl[8], sa[8]; michael@0: int pl; michael@0: int r; michael@0: int64_t d; michael@0: michael@0: save_context(cpi, mi_row, mi_col, a, l, sa, sl, BLOCK_64X64); michael@0: michael@0: // Default is non mask (all reference frames allowed. michael@0: cpi->ref_frame_mask = 0; michael@0: michael@0: // Do RD search for 64x64. michael@0: if ((mi_row + (ms >> 1) < cm->mi_rows) && michael@0: (mi_col + (ms >> 1) < cm->mi_cols)) { michael@0: cpi->set_ref_frame_mask = 1; michael@0: pick_sb_modes(cpi, tile, mi_row, mi_col, &r, &d, BLOCK_64X64, michael@0: get_block_context(x, BLOCK_64X64), INT64_MAX); michael@0: pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context, michael@0: mi_row, mi_col, BLOCK_64X64); michael@0: r += x->partition_cost[pl][PARTITION_NONE]; michael@0: michael@0: *(get_sb_partitioning(x, BLOCK_64X64)) = BLOCK_64X64; michael@0: cpi->set_ref_frame_mask = 0; michael@0: } michael@0: michael@0: restore_context(cpi, mi_row, mi_col, a, l, sa, sl, BLOCK_64X64); michael@0: } michael@0: michael@0: static void encode_sb_row(VP9_COMP *cpi, const TileInfo *const tile, michael@0: int mi_row, TOKENEXTRA **tp) { michael@0: VP9_COMMON * const cm = &cpi->common; michael@0: int mi_col; michael@0: michael@0: // Initialize the left context for the new SB row michael@0: vpx_memset(&cpi->left_context, 0, sizeof(cpi->left_context)); michael@0: vpx_memset(cpi->left_seg_context, 0, sizeof(cpi->left_seg_context)); michael@0: michael@0: // Code each SB in the row michael@0: for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end; michael@0: mi_col += MI_BLOCK_SIZE) { michael@0: int dummy_rate; michael@0: int64_t dummy_dist; michael@0: michael@0: vp9_zero(cpi->mb.pred_mv); michael@0: michael@0: if (cpi->sf.reference_masking) michael@0: rd_pick_reference_frame(cpi, tile, mi_row, mi_col); michael@0: michael@0: if (cpi->sf.use_lastframe_partitioning || michael@0: cpi->sf.use_one_partition_size_always ) { michael@0: const int idx_str = cm->mode_info_stride * mi_row + mi_col; michael@0: MODE_INFO **mi_8x8 = cm->mi_grid_visible + idx_str; michael@0: MODE_INFO **prev_mi_8x8 = cm->prev_mi_grid_visible + idx_str; michael@0: michael@0: cpi->mb.source_variance = UINT_MAX; michael@0: if (cpi->sf.use_one_partition_size_always) { michael@0: set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64); michael@0: set_partitioning(cpi, tile, mi_8x8, mi_row, mi_col); michael@0: rd_use_partition(cpi, tile, mi_8x8, tp, mi_row, mi_col, BLOCK_64X64, michael@0: &dummy_rate, &dummy_dist, 1); michael@0: } else { michael@0: if ((cpi->common.current_video_frame michael@0: % cpi->sf.last_partitioning_redo_frequency) == 0 michael@0: || cm->prev_mi == 0 michael@0: || cpi->common.show_frame == 0 michael@0: || cpi->common.frame_type == KEY_FRAME michael@0: || cpi->is_src_frame_alt_ref michael@0: || ((cpi->sf.use_lastframe_partitioning == michael@0: LAST_FRAME_PARTITION_LOW_MOTION) && michael@0: sb_has_motion(cpi, prev_mi_8x8))) { michael@0: // If required set upper and lower partition size limits michael@0: if (cpi->sf.auto_min_max_partition_size) { michael@0: set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64); michael@0: rd_auto_partition_range(cpi, tile, mi_row, mi_col, michael@0: &cpi->sf.min_partition_size, michael@0: &cpi->sf.max_partition_size); michael@0: } michael@0: rd_pick_partition(cpi, tile, tp, mi_row, mi_col, BLOCK_64X64, michael@0: &dummy_rate, &dummy_dist, 1, INT64_MAX); michael@0: } else { michael@0: copy_partitioning(cpi, mi_8x8, prev_mi_8x8); michael@0: rd_use_partition(cpi, tile, mi_8x8, tp, mi_row, mi_col, BLOCK_64X64, michael@0: &dummy_rate, &dummy_dist, 1); michael@0: } michael@0: } michael@0: } else { michael@0: // If required set upper and lower partition size limits michael@0: if (cpi->sf.auto_min_max_partition_size) { michael@0: set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64); michael@0: rd_auto_partition_range(cpi, tile, mi_row, mi_col, michael@0: &cpi->sf.min_partition_size, michael@0: &cpi->sf.max_partition_size); michael@0: } michael@0: rd_pick_partition(cpi, tile, tp, mi_row, mi_col, BLOCK_64X64, michael@0: &dummy_rate, &dummy_dist, 1, INT64_MAX); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void init_encode_frame_mb_context(VP9_COMP *cpi) { michael@0: MACROBLOCK *const x = &cpi->mb; michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols); michael@0: michael@0: x->act_zbin_adj = 0; michael@0: cpi->seg0_idx = 0; michael@0: michael@0: xd->mode_info_stride = cm->mode_info_stride; michael@0: michael@0: // reset intra mode contexts michael@0: if (frame_is_intra_only(cm)) michael@0: vp9_init_mbmode_probs(cm); michael@0: michael@0: // Copy data over into macro block data structures. michael@0: vp9_setup_src_planes(x, cpi->Source, 0, 0); michael@0: michael@0: // TODO(jkoleszar): are these initializations required? michael@0: setup_pre_planes(xd, 0, &cm->yv12_fb[cm->ref_frame_map[cpi->lst_fb_idx]], michael@0: 0, 0, NULL); michael@0: setup_dst_planes(xd, get_frame_new_buffer(cm), 0, 0); michael@0: michael@0: setup_block_dptrs(&x->e_mbd, cm->subsampling_x, cm->subsampling_y); michael@0: michael@0: xd->mi_8x8[0]->mbmi.mode = DC_PRED; michael@0: xd->mi_8x8[0]->mbmi.uv_mode = DC_PRED; michael@0: michael@0: vp9_zero(cpi->y_mode_count); michael@0: vp9_zero(cpi->y_uv_mode_count); michael@0: vp9_zero(cm->counts.inter_mode); michael@0: vp9_zero(cpi->partition_count); michael@0: vp9_zero(cpi->intra_inter_count); michael@0: vp9_zero(cpi->comp_inter_count); michael@0: vp9_zero(cpi->single_ref_count); michael@0: vp9_zero(cpi->comp_ref_count); michael@0: vp9_zero(cm->counts.tx); michael@0: vp9_zero(cm->counts.mbskip); michael@0: michael@0: // Note: this memset assumes above_context[0], [1] and [2] michael@0: // are allocated as part of the same buffer. michael@0: vpx_memset(cpi->above_context[0], 0, michael@0: sizeof(*cpi->above_context[0]) * michael@0: 2 * aligned_mi_cols * MAX_MB_PLANE); michael@0: vpx_memset(cpi->above_seg_context, 0, michael@0: sizeof(*cpi->above_seg_context) * aligned_mi_cols); michael@0: } michael@0: michael@0: static void switch_lossless_mode(VP9_COMP *cpi, int lossless) { michael@0: if (lossless) { michael@0: // printf("Switching to lossless\n"); michael@0: cpi->mb.fwd_txm4x4 = vp9_fwht4x4; michael@0: cpi->mb.e_mbd.itxm_add = vp9_iwht4x4_add; michael@0: cpi->mb.optimize = 0; michael@0: cpi->common.lf.filter_level = 0; michael@0: cpi->zbin_mode_boost_enabled = 0; michael@0: cpi->common.tx_mode = ONLY_4X4; michael@0: } else { michael@0: // printf("Not lossless\n"); michael@0: cpi->mb.fwd_txm4x4 = vp9_fdct4x4; michael@0: cpi->mb.e_mbd.itxm_add = vp9_idct4x4_add; michael@0: } michael@0: } michael@0: michael@0: static void switch_tx_mode(VP9_COMP *cpi) { michael@0: if (cpi->sf.tx_size_search_method == USE_LARGESTALL && michael@0: cpi->common.tx_mode >= ALLOW_32X32) michael@0: cpi->common.tx_mode = ALLOW_32X32; michael@0: } michael@0: michael@0: static void encode_frame_internal(VP9_COMP *cpi) { michael@0: int mi_row; michael@0: MACROBLOCK * const x = &cpi->mb; michael@0: VP9_COMMON * const cm = &cpi->common; michael@0: MACROBLOCKD * const xd = &x->e_mbd; michael@0: michael@0: // fprintf(stderr, "encode_frame_internal frame %d (%d) type %d\n", michael@0: // cpi->common.current_video_frame, cpi->common.show_frame, michael@0: // cm->frame_type); michael@0: michael@0: // debug output michael@0: #if DBG_PRNT_SEGMAP michael@0: { michael@0: FILE *statsfile; michael@0: statsfile = fopen("segmap2.stt", "a"); michael@0: fprintf(statsfile, "\n"); michael@0: fclose(statsfile); michael@0: } michael@0: #endif michael@0: michael@0: vp9_zero(cm->counts.switchable_interp); michael@0: vp9_zero(cpi->tx_stepdown_count); michael@0: michael@0: xd->mi_8x8 = cm->mi_grid_visible; michael@0: // required for vp9_frame_init_quantizer michael@0: xd->mi_8x8[0] = cm->mi; michael@0: michael@0: xd->last_mi = cm->prev_mi; michael@0: michael@0: vp9_zero(cpi->NMVcount); michael@0: vp9_zero(cpi->coef_counts); michael@0: vp9_zero(cm->counts.eob_branch); michael@0: michael@0: cpi->mb.e_mbd.lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0 michael@0: && cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0; michael@0: switch_lossless_mode(cpi, cpi->mb.e_mbd.lossless); michael@0: michael@0: vp9_frame_init_quantizer(cpi); michael@0: michael@0: vp9_initialize_rd_consts(cpi); michael@0: vp9_initialize_me_consts(cpi, cm->base_qindex); michael@0: switch_tx_mode(cpi); michael@0: michael@0: if (cpi->oxcf.tuning == VP8_TUNE_SSIM) { michael@0: // Initialize encode frame context. michael@0: init_encode_frame_mb_context(cpi); michael@0: michael@0: // Build a frame level activity map michael@0: build_activity_map(cpi); michael@0: } michael@0: michael@0: // Re-initialize encode frame context. michael@0: init_encode_frame_mb_context(cpi); michael@0: michael@0: vp9_zero(cpi->rd_comp_pred_diff); michael@0: vp9_zero(cpi->rd_filter_diff); michael@0: vp9_zero(cpi->rd_tx_select_diff); michael@0: vp9_zero(cpi->rd_tx_select_threshes); michael@0: michael@0: set_prev_mi(cm); michael@0: michael@0: { michael@0: struct vpx_usec_timer emr_timer; michael@0: vpx_usec_timer_start(&emr_timer); michael@0: michael@0: { michael@0: // Take tiles into account and give start/end MB michael@0: int tile_col, tile_row; michael@0: TOKENEXTRA *tp = cpi->tok; michael@0: const int tile_cols = 1 << cm->log2_tile_cols; michael@0: const int tile_rows = 1 << cm->log2_tile_rows; michael@0: michael@0: for (tile_row = 0; tile_row < tile_rows; tile_row++) { michael@0: for (tile_col = 0; tile_col < tile_cols; tile_col++) { michael@0: TileInfo tile; michael@0: TOKENEXTRA *tp_old = tp; michael@0: michael@0: // For each row of SBs in the frame michael@0: vp9_tile_init(&tile, cm, tile_row, tile_col); michael@0: for (mi_row = tile.mi_row_start; michael@0: mi_row < tile.mi_row_end; mi_row += 8) michael@0: encode_sb_row(cpi, &tile, mi_row, &tp); michael@0: michael@0: cpi->tok_count[tile_row][tile_col] = (unsigned int)(tp - tp_old); michael@0: assert(tp - cpi->tok <= get_token_alloc(cm->mb_rows, cm->mb_cols)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: vpx_usec_timer_mark(&emr_timer); michael@0: cpi->time_encode_sb_row += vpx_usec_timer_elapsed(&emr_timer); michael@0: } michael@0: michael@0: if (cpi->sf.skip_encode_sb) { michael@0: int j; michael@0: unsigned int intra_count = 0, inter_count = 0; michael@0: for (j = 0; j < INTRA_INTER_CONTEXTS; ++j) { michael@0: intra_count += cpi->intra_inter_count[j][0]; michael@0: inter_count += cpi->intra_inter_count[j][1]; michael@0: } michael@0: cpi->sf.skip_encode_frame = ((intra_count << 2) < inter_count); michael@0: cpi->sf.skip_encode_frame &= (cm->frame_type != KEY_FRAME); michael@0: cpi->sf.skip_encode_frame &= cm->show_frame; michael@0: } else { michael@0: cpi->sf.skip_encode_frame = 0; michael@0: } michael@0: michael@0: #if 0 michael@0: // Keep record of the total distortion this time around for future use michael@0: cpi->last_frame_distortion = cpi->frame_distortion; michael@0: #endif michael@0: } michael@0: michael@0: static int check_dual_ref_flags(VP9_COMP *cpi) { michael@0: const int ref_flags = cpi->ref_frame_flags; michael@0: michael@0: if (vp9_segfeature_active(&cpi->common.seg, 1, SEG_LVL_REF_FRAME)) { michael@0: return 0; michael@0: } else { michael@0: return (!!(ref_flags & VP9_GOLD_FLAG) + !!(ref_flags & VP9_LAST_FLAG) michael@0: + !!(ref_flags & VP9_ALT_FLAG)) >= 2; michael@0: } michael@0: } michael@0: michael@0: static int get_skip_flag(MODE_INFO **mi_8x8, int mis, int ymbs, int xmbs) { michael@0: int x, y; michael@0: michael@0: for (y = 0; y < ymbs; y++) { michael@0: for (x = 0; x < xmbs; x++) { michael@0: if (!mi_8x8[y * mis + x]->mbmi.skip_coeff) michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: static void set_txfm_flag(MODE_INFO **mi_8x8, int mis, int ymbs, int xmbs, michael@0: TX_SIZE tx_size) { michael@0: int x, y; michael@0: michael@0: for (y = 0; y < ymbs; y++) { michael@0: for (x = 0; x < xmbs; x++) michael@0: mi_8x8[y * mis + x]->mbmi.tx_size = tx_size; michael@0: } michael@0: } michael@0: michael@0: static void reset_skip_txfm_size_b(VP9_COMP *cpi, MODE_INFO **mi_8x8, michael@0: int mis, TX_SIZE max_tx_size, int bw, int bh, michael@0: int mi_row, int mi_col, BLOCK_SIZE bsize) { michael@0: VP9_COMMON * const cm = &cpi->common; michael@0: michael@0: if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) { michael@0: return; michael@0: } else { michael@0: MB_MODE_INFO * const mbmi = &mi_8x8[0]->mbmi; michael@0: if (mbmi->tx_size > max_tx_size) { michael@0: const int ymbs = MIN(bh, cm->mi_rows - mi_row); michael@0: const int xmbs = MIN(bw, cm->mi_cols - mi_col); michael@0: michael@0: assert(vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) || michael@0: get_skip_flag(mi_8x8, mis, ymbs, xmbs)); michael@0: set_txfm_flag(mi_8x8, mis, ymbs, xmbs, max_tx_size); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void reset_skip_txfm_size_sb(VP9_COMP *cpi, MODE_INFO **mi_8x8, michael@0: TX_SIZE max_tx_size, int mi_row, int mi_col, michael@0: BLOCK_SIZE bsize) { michael@0: VP9_COMMON * const cm = &cpi->common; michael@0: const int mis = cm->mode_info_stride; michael@0: int bw, bh; michael@0: const int bs = num_8x8_blocks_wide_lookup[bsize], hbs = bs / 2; michael@0: michael@0: if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) michael@0: return; michael@0: michael@0: bw = num_8x8_blocks_wide_lookup[mi_8x8[0]->mbmi.sb_type]; michael@0: bh = num_8x8_blocks_high_lookup[mi_8x8[0]->mbmi.sb_type]; michael@0: michael@0: if (bw == bs && bh == bs) { michael@0: reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, bs, bs, mi_row, michael@0: mi_col, bsize); michael@0: } else if (bw == bs && bh < bs) { michael@0: reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, bs, hbs, mi_row, michael@0: mi_col, bsize); michael@0: reset_skip_txfm_size_b(cpi, mi_8x8 + hbs * mis, mis, max_tx_size, bs, hbs, michael@0: mi_row + hbs, mi_col, bsize); michael@0: } else if (bw < bs && bh == bs) { michael@0: reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, hbs, bs, mi_row, michael@0: mi_col, bsize); michael@0: reset_skip_txfm_size_b(cpi, mi_8x8 + hbs, mis, max_tx_size, hbs, bs, mi_row, michael@0: mi_col + hbs, bsize); michael@0: michael@0: } else { michael@0: const BLOCK_SIZE subsize = subsize_lookup[PARTITION_SPLIT][bsize]; michael@0: int n; michael@0: michael@0: assert(bw < bs && bh < bs); michael@0: michael@0: for (n = 0; n < 4; n++) { michael@0: const int mi_dc = hbs * (n & 1); michael@0: const int mi_dr = hbs * (n >> 1); michael@0: michael@0: reset_skip_txfm_size_sb(cpi, &mi_8x8[mi_dr * mis + mi_dc], max_tx_size, michael@0: mi_row + mi_dr, mi_col + mi_dc, subsize); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void reset_skip_txfm_size(VP9_COMP *cpi, TX_SIZE txfm_max) { michael@0: VP9_COMMON * const cm = &cpi->common; michael@0: int mi_row, mi_col; michael@0: const int mis = cm->mode_info_stride; michael@0: // MODE_INFO *mi, *mi_ptr = cm->mi; michael@0: MODE_INFO **mi_8x8, **mi_ptr = cm->mi_grid_visible; michael@0: michael@0: for (mi_row = 0; mi_row < cm->mi_rows; mi_row += 8, mi_ptr += 8 * mis) { michael@0: mi_8x8 = mi_ptr; michael@0: for (mi_col = 0; mi_col < cm->mi_cols; mi_col += 8, mi_8x8 += 8) { michael@0: reset_skip_txfm_size_sb(cpi, mi_8x8, txfm_max, mi_row, mi_col, michael@0: BLOCK_64X64); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static int get_frame_type(VP9_COMP *cpi) { michael@0: int frame_type; michael@0: if (frame_is_intra_only(&cpi->common)) michael@0: frame_type = 0; michael@0: else if (cpi->is_src_frame_alt_ref && cpi->refresh_golden_frame) michael@0: frame_type = 3; michael@0: else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) michael@0: frame_type = 1; michael@0: else michael@0: frame_type = 2; michael@0: return frame_type; michael@0: } michael@0: michael@0: static void select_tx_mode(VP9_COMP *cpi) { michael@0: if (cpi->oxcf.lossless) { michael@0: cpi->common.tx_mode = ONLY_4X4; michael@0: } else if (cpi->common.current_video_frame == 0) { michael@0: cpi->common.tx_mode = TX_MODE_SELECT; michael@0: } else { michael@0: if (cpi->sf.tx_size_search_method == USE_LARGESTALL) { michael@0: cpi->common.tx_mode = ALLOW_32X32; michael@0: } else if (cpi->sf.tx_size_search_method == USE_FULL_RD) { michael@0: int frame_type = get_frame_type(cpi); michael@0: cpi->common.tx_mode = michael@0: cpi->rd_tx_select_threshes[frame_type][ALLOW_32X32] michael@0: > cpi->rd_tx_select_threshes[frame_type][TX_MODE_SELECT] ? michael@0: ALLOW_32X32 : TX_MODE_SELECT; michael@0: } else { michael@0: unsigned int total = 0; michael@0: int i; michael@0: for (i = 0; i < TX_SIZES; ++i) michael@0: total += cpi->tx_stepdown_count[i]; michael@0: if (total) { michael@0: double fraction = (double)cpi->tx_stepdown_count[0] / total; michael@0: cpi->common.tx_mode = fraction > 0.90 ? ALLOW_32X32 : TX_MODE_SELECT; michael@0: // printf("fraction = %f\n", fraction); michael@0: } // else keep unchanged michael@0: } michael@0: } michael@0: } michael@0: michael@0: void vp9_encode_frame(VP9_COMP *cpi) { michael@0: VP9_COMMON * const cm = &cpi->common; michael@0: michael@0: // In the longer term the encoder should be generalized to match the michael@0: // decoder such that we allow compound where one of the 3 buffers has a michael@0: // different sign bias and that buffer is then the fixed ref. However, this michael@0: // requires further work in the rd loop. For now the only supported encoder michael@0: // side behavior is where the ALT ref buffer has opposite sign bias to michael@0: // the other two. michael@0: if (!frame_is_intra_only(cm)) { michael@0: if ((cm->ref_frame_sign_bias[ALTREF_FRAME] michael@0: == cm->ref_frame_sign_bias[GOLDEN_FRAME]) michael@0: || (cm->ref_frame_sign_bias[ALTREF_FRAME] michael@0: == cm->ref_frame_sign_bias[LAST_FRAME])) { michael@0: cm->allow_comp_inter_inter = 0; michael@0: } else { michael@0: cm->allow_comp_inter_inter = 1; michael@0: cm->comp_fixed_ref = ALTREF_FRAME; michael@0: cm->comp_var_ref[0] = LAST_FRAME; michael@0: cm->comp_var_ref[1] = GOLDEN_FRAME; michael@0: } michael@0: } michael@0: michael@0: if (cpi->sf.RD) { michael@0: int i, pred_type; michael@0: INTERPOLATION_TYPE filter_type; michael@0: /* michael@0: * This code does a single RD pass over the whole frame assuming michael@0: * either compound, single or hybrid prediction as per whatever has michael@0: * worked best for that type of frame in the past. michael@0: * It also predicts whether another coding mode would have worked michael@0: * better that this coding mode. If that is the case, it remembers michael@0: * that for subsequent frames. michael@0: * It does the same analysis for transform size selection also. michael@0: */ michael@0: int frame_type = get_frame_type(cpi); michael@0: michael@0: /* prediction (compound, single or hybrid) mode selection */ michael@0: if (frame_type == 3 || !cm->allow_comp_inter_inter) michael@0: pred_type = SINGLE_PREDICTION_ONLY; michael@0: else if (cpi->rd_prediction_type_threshes[frame_type][1] michael@0: > cpi->rd_prediction_type_threshes[frame_type][0] michael@0: && cpi->rd_prediction_type_threshes[frame_type][1] michael@0: > cpi->rd_prediction_type_threshes[frame_type][2] michael@0: && check_dual_ref_flags(cpi) && cpi->static_mb_pct == 100) michael@0: pred_type = COMP_PREDICTION_ONLY; michael@0: else if (cpi->rd_prediction_type_threshes[frame_type][0] michael@0: > cpi->rd_prediction_type_threshes[frame_type][2]) michael@0: pred_type = SINGLE_PREDICTION_ONLY; michael@0: else michael@0: pred_type = HYBRID_PREDICTION; michael@0: michael@0: /* filter type selection */ michael@0: // FIXME(rbultje) for some odd reason, we often select smooth_filter michael@0: // as default filter for ARF overlay frames. This is a REALLY BAD michael@0: // IDEA so we explicitly disable it here. michael@0: if (frame_type != 3 && michael@0: cpi->rd_filter_threshes[frame_type][1] > michael@0: cpi->rd_filter_threshes[frame_type][0] && michael@0: cpi->rd_filter_threshes[frame_type][1] > michael@0: cpi->rd_filter_threshes[frame_type][2] && michael@0: cpi->rd_filter_threshes[frame_type][1] > michael@0: cpi->rd_filter_threshes[frame_type][SWITCHABLE_FILTERS]) { michael@0: filter_type = EIGHTTAP_SMOOTH; michael@0: } else if (cpi->rd_filter_threshes[frame_type][2] > michael@0: cpi->rd_filter_threshes[frame_type][0] && michael@0: cpi->rd_filter_threshes[frame_type][2] > michael@0: cpi->rd_filter_threshes[frame_type][SWITCHABLE_FILTERS]) { michael@0: filter_type = EIGHTTAP_SHARP; michael@0: } else if (cpi->rd_filter_threshes[frame_type][0] > michael@0: cpi->rd_filter_threshes[frame_type][SWITCHABLE_FILTERS]) { michael@0: filter_type = EIGHTTAP; michael@0: } else { michael@0: filter_type = SWITCHABLE; michael@0: } michael@0: michael@0: cpi->mb.e_mbd.lossless = 0; michael@0: if (cpi->oxcf.lossless) { michael@0: cpi->mb.e_mbd.lossless = 1; michael@0: } michael@0: michael@0: /* transform size selection (4x4, 8x8, 16x16 or select-per-mb) */ michael@0: select_tx_mode(cpi); michael@0: cpi->common.comp_pred_mode = pred_type; michael@0: cpi->common.mcomp_filter_type = filter_type; michael@0: encode_frame_internal(cpi); michael@0: michael@0: for (i = 0; i < NB_PREDICTION_TYPES; ++i) { michael@0: const int diff = (int) (cpi->rd_comp_pred_diff[i] / cpi->common.MBs); michael@0: cpi->rd_prediction_type_threshes[frame_type][i] += diff; michael@0: cpi->rd_prediction_type_threshes[frame_type][i] >>= 1; michael@0: } michael@0: michael@0: for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) { michael@0: const int64_t diff = cpi->rd_filter_diff[i] / cpi->common.MBs; michael@0: cpi->rd_filter_threshes[frame_type][i] = michael@0: (cpi->rd_filter_threshes[frame_type][i] + diff) / 2; michael@0: } michael@0: michael@0: for (i = 0; i < TX_MODES; ++i) { michael@0: int64_t pd = cpi->rd_tx_select_diff[i]; michael@0: int diff; michael@0: if (i == TX_MODE_SELECT) michael@0: pd -= RDCOST(cpi->mb.rdmult, cpi->mb.rddiv, michael@0: 2048 * (TX_SIZES - 1), 0); michael@0: diff = (int) (pd / cpi->common.MBs); michael@0: cpi->rd_tx_select_threshes[frame_type][i] += diff; michael@0: cpi->rd_tx_select_threshes[frame_type][i] /= 2; michael@0: } michael@0: michael@0: if (cpi->common.comp_pred_mode == HYBRID_PREDICTION) { michael@0: int single_count_zero = 0; michael@0: int comp_count_zero = 0; michael@0: michael@0: for (i = 0; i < COMP_INTER_CONTEXTS; i++) { michael@0: single_count_zero += cpi->comp_inter_count[i][0]; michael@0: comp_count_zero += cpi->comp_inter_count[i][1]; michael@0: } michael@0: michael@0: if (comp_count_zero == 0) { michael@0: cpi->common.comp_pred_mode = SINGLE_PREDICTION_ONLY; michael@0: vp9_zero(cpi->comp_inter_count); michael@0: } else if (single_count_zero == 0) { michael@0: cpi->common.comp_pred_mode = COMP_PREDICTION_ONLY; michael@0: vp9_zero(cpi->comp_inter_count); michael@0: } michael@0: } michael@0: michael@0: if (cpi->common.tx_mode == TX_MODE_SELECT) { michael@0: int count4x4 = 0; michael@0: int count8x8_lp = 0, count8x8_8x8p = 0; michael@0: int count16x16_16x16p = 0, count16x16_lp = 0; michael@0: int count32x32 = 0; michael@0: michael@0: for (i = 0; i < TX_SIZE_CONTEXTS; ++i) { michael@0: count4x4 += cm->counts.tx.p32x32[i][TX_4X4]; michael@0: count4x4 += cm->counts.tx.p16x16[i][TX_4X4]; michael@0: count4x4 += cm->counts.tx.p8x8[i][TX_4X4]; michael@0: michael@0: count8x8_lp += cm->counts.tx.p32x32[i][TX_8X8]; michael@0: count8x8_lp += cm->counts.tx.p16x16[i][TX_8X8]; michael@0: count8x8_8x8p += cm->counts.tx.p8x8[i][TX_8X8]; michael@0: michael@0: count16x16_16x16p += cm->counts.tx.p16x16[i][TX_16X16]; michael@0: count16x16_lp += cm->counts.tx.p32x32[i][TX_16X16]; michael@0: count32x32 += cm->counts.tx.p32x32[i][TX_32X32]; michael@0: } michael@0: michael@0: if (count4x4 == 0 && count16x16_lp == 0 && count16x16_16x16p == 0 michael@0: && count32x32 == 0) { michael@0: cpi->common.tx_mode = ALLOW_8X8; michael@0: reset_skip_txfm_size(cpi, TX_8X8); michael@0: } else if (count8x8_8x8p == 0 && count16x16_16x16p == 0 michael@0: && count8x8_lp == 0 && count16x16_lp == 0 && count32x32 == 0) { michael@0: cpi->common.tx_mode = ONLY_4X4; michael@0: reset_skip_txfm_size(cpi, TX_4X4); michael@0: } else if (count8x8_lp == 0 && count16x16_lp == 0 && count4x4 == 0) { michael@0: cpi->common.tx_mode = ALLOW_32X32; michael@0: } else if (count32x32 == 0 && count8x8_lp == 0 && count4x4 == 0) { michael@0: cpi->common.tx_mode = ALLOW_16X16; michael@0: reset_skip_txfm_size(cpi, TX_16X16); michael@0: } michael@0: } michael@0: } else { michael@0: encode_frame_internal(cpi); michael@0: } michael@0: } michael@0: michael@0: static void sum_intra_stats(VP9_COMP *cpi, const MODE_INFO *mi) { michael@0: const MB_PREDICTION_MODE y_mode = mi->mbmi.mode; michael@0: const MB_PREDICTION_MODE uv_mode = mi->mbmi.uv_mode; michael@0: const BLOCK_SIZE bsize = mi->mbmi.sb_type; michael@0: michael@0: ++cpi->y_uv_mode_count[y_mode][uv_mode]; michael@0: michael@0: if (bsize < BLOCK_8X8) { michael@0: int idx, idy; michael@0: const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize]; michael@0: const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize]; michael@0: for (idy = 0; idy < 2; idy += num_4x4_blocks_high) michael@0: for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) michael@0: ++cpi->y_mode_count[0][mi->bmi[idy * 2 + idx].as_mode]; michael@0: } else { michael@0: ++cpi->y_mode_count[size_group_lookup[bsize]][y_mode]; michael@0: } michael@0: } michael@0: michael@0: // Experimental stub function to create a per MB zbin adjustment based on michael@0: // some previously calculated measure of MB activity. michael@0: static void adjust_act_zbin(VP9_COMP *cpi, MACROBLOCK *x) { michael@0: #if USE_ACT_INDEX michael@0: x->act_zbin_adj = *(x->mb_activity_ptr); michael@0: #else michael@0: int64_t a; michael@0: int64_t b; michael@0: int64_t act = *(x->mb_activity_ptr); michael@0: michael@0: // Apply the masking to the RD multiplier. michael@0: a = act + 4 * cpi->activity_avg; michael@0: b = 4 * act + cpi->activity_avg; michael@0: michael@0: if (act > cpi->activity_avg) michael@0: x->act_zbin_adj = (int) (((int64_t) b + (a >> 1)) / a) - 1; michael@0: else michael@0: x->act_zbin_adj = 1 - (int) (((int64_t) a + (b >> 1)) / b); michael@0: #endif michael@0: } michael@0: static void encode_superblock(VP9_COMP *cpi, TOKENEXTRA **t, int output_enabled, michael@0: int mi_row, int mi_col, BLOCK_SIZE bsize) { michael@0: VP9_COMMON * const cm = &cpi->common; michael@0: MACROBLOCK * const x = &cpi->mb; michael@0: MACROBLOCKD * const xd = &x->e_mbd; michael@0: MODE_INFO **mi_8x8 = xd->mi_8x8; michael@0: MODE_INFO *mi = mi_8x8[0]; michael@0: MB_MODE_INFO *mbmi = &mi->mbmi; michael@0: PICK_MODE_CONTEXT *ctx = get_block_context(x, bsize); michael@0: unsigned int segment_id = mbmi->segment_id; michael@0: const int mis = cm->mode_info_stride; michael@0: const int mi_width = num_8x8_blocks_wide_lookup[bsize]; michael@0: const int mi_height = num_8x8_blocks_high_lookup[bsize]; michael@0: x->skip_recode = !x->select_txfm_size && mbmi->sb_type >= BLOCK_8X8; michael@0: x->skip_optimize = ctx->is_coded; michael@0: ctx->is_coded = 1; michael@0: x->use_lp32x32fdct = cpi->sf.use_lp32x32fdct; michael@0: x->skip_encode = (!output_enabled && cpi->sf.skip_encode_frame && michael@0: x->q_index < QIDX_SKIP_THRESH); michael@0: if (x->skip_encode) michael@0: return; michael@0: michael@0: if (cm->frame_type == KEY_FRAME) { michael@0: if (cpi->oxcf.tuning == VP8_TUNE_SSIM) { michael@0: adjust_act_zbin(cpi, x); michael@0: vp9_update_zbin_extra(cpi, x); michael@0: } michael@0: } else { michael@0: vp9_setup_interp_filters(xd, mbmi->interp_filter, cm); michael@0: michael@0: if (cpi->oxcf.tuning == VP8_TUNE_SSIM) { michael@0: // Adjust the zbin based on this MB rate. michael@0: adjust_act_zbin(cpi, x); michael@0: } michael@0: michael@0: // Experimental code. Special case for gf and arf zeromv modes. michael@0: // Increase zbin size to suppress noise michael@0: cpi->zbin_mode_boost = 0; michael@0: if (cpi->zbin_mode_boost_enabled) { michael@0: if (is_inter_block(mbmi)) { michael@0: if (mbmi->mode == ZEROMV) { michael@0: if (mbmi->ref_frame[0] != LAST_FRAME) michael@0: cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST; michael@0: else michael@0: cpi->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST; michael@0: } else if (mbmi->sb_type < BLOCK_8X8) { michael@0: cpi->zbin_mode_boost = SPLIT_MV_ZBIN_BOOST; michael@0: } else { michael@0: cpi->zbin_mode_boost = MV_ZBIN_BOOST; michael@0: } michael@0: } else { michael@0: cpi->zbin_mode_boost = INTRA_ZBIN_BOOST; michael@0: } michael@0: } michael@0: michael@0: vp9_update_zbin_extra(cpi, x); michael@0: } michael@0: michael@0: if (!is_inter_block(mbmi)) { michael@0: vp9_encode_intra_block_y(x, MAX(bsize, BLOCK_8X8)); michael@0: vp9_encode_intra_block_uv(x, MAX(bsize, BLOCK_8X8)); michael@0: if (output_enabled) michael@0: sum_intra_stats(cpi, mi); michael@0: } else { michael@0: int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, mbmi->ref_frame[0])]; michael@0: YV12_BUFFER_CONFIG *ref_fb = &cm->yv12_fb[idx]; michael@0: YV12_BUFFER_CONFIG *second_ref_fb = NULL; michael@0: if (has_second_ref(mbmi)) { michael@0: idx = cm->ref_frame_map[get_ref_frame_idx(cpi, mbmi->ref_frame[1])]; michael@0: second_ref_fb = &cm->yv12_fb[idx]; michael@0: } michael@0: michael@0: assert(cm->frame_type != KEY_FRAME); michael@0: michael@0: setup_pre_planes(xd, 0, ref_fb, mi_row, mi_col, michael@0: &xd->scale_factor[0]); michael@0: setup_pre_planes(xd, 1, second_ref_fb, mi_row, mi_col, michael@0: &xd->scale_factor[1]); michael@0: michael@0: vp9_build_inter_predictors_sb(xd, mi_row, mi_col, MAX(bsize, BLOCK_8X8)); michael@0: } michael@0: michael@0: if (!is_inter_block(mbmi)) { michael@0: vp9_tokenize_sb(cpi, t, !output_enabled, MAX(bsize, BLOCK_8X8)); michael@0: } else if (!x->skip) { michael@0: vp9_encode_sb(x, MAX(bsize, BLOCK_8X8)); michael@0: vp9_tokenize_sb(cpi, t, !output_enabled, MAX(bsize, BLOCK_8X8)); michael@0: } else { michael@0: int mb_skip_context = xd->left_available ? mi_8x8[-1]->mbmi.skip_coeff : 0; michael@0: mb_skip_context += mi_8x8[-mis] ? mi_8x8[-mis]->mbmi.skip_coeff : 0; michael@0: michael@0: mbmi->skip_coeff = 1; michael@0: if (output_enabled) michael@0: cm->counts.mbskip[mb_skip_context][1]++; michael@0: reset_skip_context(xd, MAX(bsize, BLOCK_8X8)); michael@0: } michael@0: michael@0: if (output_enabled) { michael@0: if (cm->tx_mode == TX_MODE_SELECT && michael@0: mbmi->sb_type >= BLOCK_8X8 && michael@0: !(is_inter_block(mbmi) && michael@0: (mbmi->skip_coeff || michael@0: vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)))) { michael@0: const uint8_t context = vp9_get_pred_context_tx_size(xd); michael@0: ++get_tx_counts(max_txsize_lookup[bsize], michael@0: context, &cm->counts.tx)[mbmi->tx_size]; michael@0: } else { michael@0: int x, y; michael@0: TX_SIZE sz = tx_mode_to_biggest_tx_size[cm->tx_mode]; michael@0: assert(sizeof(tx_mode_to_biggest_tx_size) / michael@0: sizeof(tx_mode_to_biggest_tx_size[0]) == TX_MODES); michael@0: // The new intra coding scheme requires no change of transform size michael@0: if (is_inter_block(&mi->mbmi)) { michael@0: if (sz == TX_32X32 && bsize < BLOCK_32X32) michael@0: sz = TX_16X16; michael@0: if (sz == TX_16X16 && bsize < BLOCK_16X16) michael@0: sz = TX_8X8; michael@0: if (sz == TX_8X8 && bsize < BLOCK_8X8) michael@0: sz = TX_4X4; michael@0: } else if (bsize >= BLOCK_8X8) { michael@0: sz = mbmi->tx_size; michael@0: } else { michael@0: sz = TX_4X4; michael@0: } michael@0: michael@0: for (y = 0; y < mi_height; y++) michael@0: for (x = 0; x < mi_width; x++) michael@0: if (mi_col + x < cm->mi_cols && mi_row + y < cm->mi_rows) michael@0: mi_8x8[mis * y + x]->mbmi.tx_size = sz; michael@0: } michael@0: } michael@0: }