michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: michael@0: #include "vpx_config.h" michael@0: #include "vp8_rtcd.h" michael@0: #include "encodemb.h" michael@0: #include "encodemv.h" michael@0: #include "vp8/common/common.h" michael@0: #include "onyx_int.h" michael@0: #include "vp8/common/extend.h" michael@0: #include "vp8/common/entropymode.h" michael@0: #include "vp8/common/quant_common.h" michael@0: #include "segmentation.h" michael@0: #include "vp8/common/setupintrarecon.h" michael@0: #include "encodeintra.h" michael@0: #include "vp8/common/reconinter.h" michael@0: #include "rdopt.h" michael@0: #include "pickinter.h" michael@0: #include "vp8/common/findnearmv.h" michael@0: #include michael@0: #include michael@0: #include "vp8/common/invtrans.h" michael@0: #include "vpx_ports/vpx_timer.h" michael@0: #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING michael@0: #include "bitstream.h" michael@0: #endif michael@0: #include "encodeframe.h" michael@0: michael@0: extern void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t) ; michael@0: extern void vp8_calc_ref_frame_costs(int *ref_frame_cost, michael@0: int prob_intra, michael@0: int prob_last, michael@0: int prob_garf michael@0: ); michael@0: extern void vp8_convert_rfct_to_prob(VP8_COMP *const cpi); michael@0: extern void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex); michael@0: extern void vp8_auto_select_speed(VP8_COMP *cpi); michael@0: extern void vp8cx_init_mbrthread_data(VP8_COMP *cpi, michael@0: MACROBLOCK *x, michael@0: MB_ROW_COMP *mbr_ei, michael@0: int count); michael@0: static void adjust_act_zbin( VP8_COMP *cpi, MACROBLOCK *x ); michael@0: michael@0: #ifdef MODE_STATS michael@0: unsigned int inter_y_modes[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; michael@0: unsigned int inter_uv_modes[4] = {0, 0, 0, 0}; michael@0: unsigned int inter_b_modes[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; michael@0: unsigned int y_modes[5] = {0, 0, 0, 0, 0}; michael@0: unsigned int uv_modes[4] = {0, 0, 0, 0}; michael@0: unsigned int b_modes[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; michael@0: #endif michael@0: 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: * vp8_activity_masking(). michael@0: */ michael@0: #define VP8_ACTIVITY_AVG_MIN (64) 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 unsigned char VP8_VAR_OFFS[16]= michael@0: { michael@0: 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128 michael@0: }; michael@0: michael@0: michael@0: /* Original activity measure from Tim T's code. */ michael@0: static unsigned int tt_activity_measure( VP8_COMP *cpi, MACROBLOCK *x ) michael@0: { 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 = vp8_variance16x16(x->src.y_buffer, michael@0: x->src.y_stride, VP8_VAR_OFFS, 0, &sse); michael@0: act = 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( VP8_COMP *cpi, michael@0: MACROBLOCK *x, int use_dc_pred ) michael@0: { michael@0: return vp8_encode_intra(cpi,x, use_dc_pred); michael@0: } 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: */ michael@0: #define ALT_ACT_MEASURE 1 michael@0: static unsigned int mb_activity_measure( VP8_COMP *cpi, MACROBLOCK *x, michael@0: int mb_row, int mb_col) michael@0: { michael@0: unsigned int mb_activity; michael@0: michael@0: if ( ALT_ACT_MEASURE ) michael@0: { 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( cpi, x, use_dc_pred ); michael@0: } michael@0: else michael@0: { michael@0: /* Original activity measure from Tim T's code. */ michael@0: mb_activity = tt_activity_measure( cpi, x ); michael@0: } michael@0: michael@0: if ( mb_activity < VP8_ACTIVITY_AVG_MIN ) michael@0: mb_activity = VP8_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( VP8_COMP *cpi, int64_t activity_sum ) michael@0: { 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(sortlist, michael@0: 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: michael@0: /* Ripple each value down to its correct position */ michael@0: for ( i = 1; i < cpi->common.MBs; i ++ ) michael@0: { michael@0: for ( j = i; j > 0; j -- ) michael@0: { michael@0: if ( sortlist[j] < sortlist[j-1] ) michael@0: { michael@0: /* Swap values */ michael@0: tmp = sortlist[j-1]; michael@0: sortlist[j-1] = sortlist[j]; michael@0: sortlist[j] = tmp; michael@0: } michael@0: else michael@0: break; 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 michael@0: michael@0: if (cpi->activity_avg < VP8_ACTIVITY_AVG_MIN) michael@0: cpi->activity_avg = VP8_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 and activity index for each mb */ michael@0: static void calc_activity_index( VP8_COMP *cpi, MACROBLOCK *x ) michael@0: { michael@0: VP8_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: { michael@0: /* for each macroblock col in image */ michael@0: for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) michael@0: { 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: michael@0: #if OUTPUT_NORM_ACT_STATS michael@0: fclose(f); michael@0: #endif michael@0: michael@0: } michael@0: #endif 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: */ michael@0: static void build_activity_map( VP8_COMP *cpi ) michael@0: { michael@0: MACROBLOCK *const x = & cpi->mb; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: VP8_COMMON *const cm = & cpi->common; michael@0: michael@0: #if ALT_ACT_MEASURE michael@0: YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx]; 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: /* for each macroblock row in image */ michael@0: for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) michael@0: { 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: { michael@0: #if ALT_ACT_MEASURE michael@0: xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset; michael@0: xd->left_available = (mb_col != 0); michael@0: recon_yoffset += 16; michael@0: #endif michael@0: /* Copy current mb to a buffer */ michael@0: vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16); michael@0: michael@0: /* measure activity */ michael@0: mb_activity = mb_activity_measure( cpi, 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->src.y_buffer += 16; michael@0: } michael@0: michael@0: michael@0: /* adjust to the next row of mbs */ michael@0: x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols; michael@0: michael@0: #if ALT_ACT_MEASURE michael@0: /* extend the recon for intra prediction */ michael@0: vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, michael@0: xd->dst.u_buffer + 8, xd->dst.v_buffer + 8); michael@0: #endif michael@0: 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: michael@0: /* Macroblock activity masking */ michael@0: void vp8_activity_masking(VP8_COMP *cpi, MACROBLOCK *x) michael@0: { 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 michael@0: void encode_mb_row(VP8_COMP *cpi, michael@0: VP8_COMMON *cm, michael@0: int mb_row, michael@0: MACROBLOCK *x, michael@0: MACROBLOCKD *xd, michael@0: TOKENEXTRA **tp, michael@0: int *segment_counts, michael@0: int *totalrate) michael@0: { michael@0: int recon_yoffset, recon_uvoffset; michael@0: int mb_col; michael@0: int ref_fb_idx = cm->lst_fb_idx; michael@0: int dst_fb_idx = cm->new_fb_idx; michael@0: int recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride; michael@0: int recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride; michael@0: int map_index = (mb_row * cpi->common.mb_cols); michael@0: michael@0: #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) michael@0: const int num_part = (1 << cm->multi_token_partition); michael@0: TOKENEXTRA * tp_start = cpi->tok; michael@0: vp8_writer *w; michael@0: #endif michael@0: michael@0: #if CONFIG_MULTITHREAD michael@0: const int nsync = cpi->mt_sync_range; michael@0: const int rightmost_col = cm->mb_cols + nsync; michael@0: volatile const int *last_row_current_mb_col; michael@0: volatile int *current_mb_col = &cpi->mt_current_mb_col[mb_row]; michael@0: michael@0: if ((cpi->b_multi_threaded != 0) && (mb_row != 0)) michael@0: last_row_current_mb_col = &cpi->mt_current_mb_col[mb_row - 1]; michael@0: else michael@0: last_row_current_mb_col = &rightmost_col; michael@0: #endif michael@0: michael@0: #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) michael@0: if(num_part > 1) michael@0: w= &cpi->bc[1 + (mb_row % num_part)]; michael@0: else michael@0: w = &cpi->bc[1]; michael@0: #endif michael@0: michael@0: /* reset above block coeffs */ michael@0: xd->above_context = cm->above_context; michael@0: michael@0: xd->up_available = (mb_row != 0); michael@0: recon_yoffset = (mb_row * recon_y_stride * 16); michael@0: recon_uvoffset = (mb_row * recon_uv_stride * 8); michael@0: michael@0: cpi->tplist[mb_row].start = *tp; michael@0: /* printf("Main mb_row = %d\n", mb_row); */ michael@0: michael@0: /* Distance of Mb to the top & bottom edges, specified in 1/8th pel michael@0: * units as they are always compared to values that are in 1/8th pel michael@0: */ michael@0: xd->mb_to_top_edge = -((mb_row * 16) << 3); michael@0: xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3; michael@0: michael@0: /* Set up limit values for vertical motion vector components michael@0: * to prevent them extending beyond the UMV borders michael@0: */ michael@0: x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16)); michael@0: x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) michael@0: + (VP8BORDERINPIXELS - 16); michael@0: michael@0: /* Set the mb activity pointer to the start of the row. */ michael@0: x->mb_activity_ptr = &cpi->mb_activity_map[map_index]; michael@0: michael@0: /* for each macroblock col in image */ michael@0: for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) michael@0: { michael@0: michael@0: #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) michael@0: *tp = cpi->tok; michael@0: #endif michael@0: /* Distance of Mb to the left & right edges, specified in michael@0: * 1/8th pel units as they are always compared to values michael@0: * that are in 1/8th pel units michael@0: */ michael@0: xd->mb_to_left_edge = -((mb_col * 16) << 3); michael@0: xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3; michael@0: michael@0: /* Set up limit values for horizontal motion vector components michael@0: * to prevent them extending beyond the UMV borders michael@0: */ michael@0: x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16)); michael@0: x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) michael@0: + (VP8BORDERINPIXELS - 16); michael@0: michael@0: xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset; michael@0: xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset; michael@0: xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset; michael@0: xd->left_available = (mb_col != 0); michael@0: michael@0: x->rddiv = cpi->RDDIV; michael@0: x->rdmult = cpi->RDMULT; michael@0: michael@0: /* Copy current mb to a buffer */ michael@0: vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16); michael@0: michael@0: #if CONFIG_MULTITHREAD michael@0: if (cpi->b_multi_threaded != 0) michael@0: { michael@0: *current_mb_col = mb_col - 1; /* set previous MB done */ michael@0: michael@0: if ((mb_col & (nsync - 1)) == 0) michael@0: { michael@0: while (mb_col > (*last_row_current_mb_col - nsync)) michael@0: { michael@0: x86_pause_hint(); michael@0: thread_sleep(0); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: if(cpi->oxcf.tuning == VP8_TUNE_SSIM) michael@0: vp8_activity_masking(cpi, x); michael@0: michael@0: /* Is segmentation enabled */ michael@0: /* MB level adjustment to quantizer */ michael@0: if (xd->segmentation_enabled) michael@0: { michael@0: /* Code to set segment id in xd->mbmi.segment_id for current MB michael@0: * (with range checking) michael@0: */ michael@0: if (cpi->segmentation_map[map_index+mb_col] <= 3) michael@0: xd->mode_info_context->mbmi.segment_id = cpi->segmentation_map[map_index+mb_col]; michael@0: else michael@0: xd->mode_info_context->mbmi.segment_id = 0; michael@0: michael@0: vp8cx_mb_init_quantizer(cpi, x, 1); michael@0: } michael@0: else michael@0: /* Set to Segment 0 by default */ michael@0: xd->mode_info_context->mbmi.segment_id = 0; michael@0: michael@0: x->active_ptr = cpi->active_map + map_index + mb_col; michael@0: michael@0: if (cm->frame_type == KEY_FRAME) michael@0: { michael@0: *totalrate += vp8cx_encode_intra_macroblock(cpi, x, tp); michael@0: #ifdef MODE_STATS michael@0: y_modes[xd->mbmi.mode] ++; michael@0: #endif michael@0: } michael@0: else michael@0: { michael@0: *totalrate += vp8cx_encode_inter_macroblock(cpi, x, tp, recon_yoffset, recon_uvoffset, mb_row, mb_col); michael@0: michael@0: #ifdef MODE_STATS michael@0: inter_y_modes[xd->mbmi.mode] ++; michael@0: michael@0: if (xd->mbmi.mode == SPLITMV) michael@0: { michael@0: int b; michael@0: michael@0: for (b = 0; b < xd->mbmi.partition_count; b++) michael@0: { michael@0: inter_b_modes[x->partition->bmi[b].mode] ++; michael@0: } michael@0: } michael@0: michael@0: #endif michael@0: michael@0: /* Special case code for cyclic refresh michael@0: * If cyclic update enabled then copy xd->mbmi.segment_id; (which michael@0: * may have been updated based on mode during michael@0: * vp8cx_encode_inter_macroblock()) back into the global michael@0: * segmentation map michael@0: */ michael@0: if ((cpi->current_layer == 0) && michael@0: (cpi->cyclic_refresh_mode_enabled && michael@0: xd->segmentation_enabled)) michael@0: { michael@0: cpi->segmentation_map[map_index+mb_col] = xd->mode_info_context->mbmi.segment_id; michael@0: michael@0: /* If the block has been refreshed mark it as clean (the michael@0: * magnitude of the -ve influences how long it will be before michael@0: * we consider another refresh): michael@0: * Else if it was coded (last frame 0,0) and has not already michael@0: * been refreshed then mark it as a candidate for cleanup michael@0: * next time (marked 0) else mark it as dirty (1). michael@0: */ michael@0: if (xd->mode_info_context->mbmi.segment_id) michael@0: cpi->cyclic_refresh_map[map_index+mb_col] = -1; michael@0: else if ((xd->mode_info_context->mbmi.mode == ZEROMV) && (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME)) michael@0: { michael@0: if (cpi->cyclic_refresh_map[map_index+mb_col] == 1) michael@0: cpi->cyclic_refresh_map[map_index+mb_col] = 0; michael@0: } michael@0: else michael@0: cpi->cyclic_refresh_map[map_index+mb_col] = 1; michael@0: michael@0: } michael@0: } michael@0: michael@0: cpi->tplist[mb_row].stop = *tp; michael@0: michael@0: #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING michael@0: /* pack tokens for this MB */ michael@0: { michael@0: int tok_count = *tp - tp_start; michael@0: pack_tokens(w, tp_start, tok_count); michael@0: } michael@0: #endif michael@0: /* Increment pointer into gf usage flags structure. */ michael@0: x->gf_active_ptr++; michael@0: michael@0: /* Increment the activity mask pointers. */ michael@0: x->mb_activity_ptr++; michael@0: michael@0: /* adjust to the next column of macroblocks */ michael@0: x->src.y_buffer += 16; michael@0: x->src.u_buffer += 8; michael@0: x->src.v_buffer += 8; michael@0: michael@0: recon_yoffset += 16; michael@0: recon_uvoffset += 8; michael@0: michael@0: /* Keep track of segment usage */ michael@0: segment_counts[xd->mode_info_context->mbmi.segment_id] ++; michael@0: michael@0: /* skip to next mb */ michael@0: xd->mode_info_context++; michael@0: x->partition_info++; michael@0: xd->above_context++; michael@0: } michael@0: michael@0: /* extend the recon for intra prediction */ michael@0: vp8_extend_mb_row( &cm->yv12_fb[dst_fb_idx], michael@0: xd->dst.y_buffer + 16, michael@0: xd->dst.u_buffer + 8, michael@0: xd->dst.v_buffer + 8); michael@0: michael@0: #if CONFIG_MULTITHREAD michael@0: if (cpi->b_multi_threaded != 0) michael@0: *current_mb_col = rightmost_col; michael@0: #endif michael@0: michael@0: /* this is to account for the border */ michael@0: xd->mode_info_context++; michael@0: x->partition_info++; michael@0: } michael@0: michael@0: static void init_encode_frame_mb_context(VP8_COMP *cpi) michael@0: { michael@0: MACROBLOCK *const x = & cpi->mb; michael@0: VP8_COMMON *const cm = & cpi->common; michael@0: MACROBLOCKD *const xd = & x->e_mbd; michael@0: michael@0: /* GF active flags data structure */ michael@0: x->gf_active_ptr = (signed char *)cpi->gf_active_flags; michael@0: michael@0: /* Activity map pointer */ michael@0: x->mb_activity_ptr = cpi->mb_activity_map; michael@0: michael@0: x->act_zbin_adj = 0; michael@0: michael@0: x->partition_info = x->pi; michael@0: michael@0: xd->mode_info_context = cm->mi; michael@0: xd->mode_info_stride = cm->mode_info_stride; michael@0: michael@0: xd->frame_type = cm->frame_type; michael@0: michael@0: /* reset intra mode contexts */ michael@0: if (cm->frame_type == KEY_FRAME) michael@0: vp8_init_mbmode_probs(cm); michael@0: michael@0: /* Copy data over into macro block data structures. */ michael@0: x->src = * cpi->Source; michael@0: xd->pre = cm->yv12_fb[cm->lst_fb_idx]; michael@0: xd->dst = cm->yv12_fb[cm->new_fb_idx]; michael@0: michael@0: /* set up frame for intra coded blocks */ michael@0: vp8_setup_intra_recon(&cm->yv12_fb[cm->new_fb_idx]); michael@0: michael@0: vp8_build_block_offsets(x); michael@0: michael@0: xd->mode_info_context->mbmi.mode = DC_PRED; michael@0: xd->mode_info_context->mbmi.uv_mode = DC_PRED; michael@0: michael@0: xd->left_context = &cm->left_context; michael@0: michael@0: x->mvc = cm->fc.mvc; michael@0: michael@0: vpx_memset(cm->above_context, 0, michael@0: sizeof(ENTROPY_CONTEXT_PLANES) * cm->mb_cols); michael@0: michael@0: /* Special case treatment when GF and ARF are not sensible options michael@0: * for reference michael@0: */ michael@0: if (cpi->ref_frame_flags == VP8_LAST_FRAME) michael@0: vp8_calc_ref_frame_costs(x->ref_frame_cost, michael@0: cpi->prob_intra_coded,255,128); michael@0: else if ((cpi->oxcf.number_of_layers > 1) && michael@0: (cpi->ref_frame_flags == VP8_GOLD_FRAME)) michael@0: vp8_calc_ref_frame_costs(x->ref_frame_cost, michael@0: cpi->prob_intra_coded,1,255); michael@0: else if ((cpi->oxcf.number_of_layers > 1) && michael@0: (cpi->ref_frame_flags == VP8_ALTR_FRAME)) michael@0: vp8_calc_ref_frame_costs(x->ref_frame_cost, michael@0: cpi->prob_intra_coded,1,1); michael@0: else michael@0: vp8_calc_ref_frame_costs(x->ref_frame_cost, michael@0: cpi->prob_intra_coded, michael@0: cpi->prob_last_coded, michael@0: cpi->prob_gf_coded); michael@0: michael@0: xd->fullpixel_mask = 0xffffffff; michael@0: if(cm->full_pixel) michael@0: xd->fullpixel_mask = 0xfffffff8; michael@0: michael@0: vp8_zero(x->coef_counts); michael@0: vp8_zero(x->ymode_count); michael@0: vp8_zero(x->uv_mode_count) michael@0: x->prediction_error = 0; michael@0: x->intra_error = 0; michael@0: vp8_zero(x->count_mb_ref_frame_usage); michael@0: } michael@0: michael@0: static void sum_coef_counts(MACROBLOCK *x, MACROBLOCK *x_thread) michael@0: { michael@0: int i = 0; michael@0: do michael@0: { michael@0: int j = 0; michael@0: do michael@0: { michael@0: int k = 0; michael@0: do michael@0: { michael@0: /* at every context */ michael@0: michael@0: /* calc probs and branch cts for this frame only */ michael@0: int t = 0; /* token/prob index */ michael@0: michael@0: do michael@0: { michael@0: x->coef_counts [i][j][k][t] += michael@0: x_thread->coef_counts [i][j][k][t]; michael@0: } michael@0: while (++t < ENTROPY_NODES); michael@0: } michael@0: while (++k < PREV_COEF_CONTEXTS); michael@0: } michael@0: while (++j < COEF_BANDS); michael@0: } michael@0: while (++i < BLOCK_TYPES); michael@0: } michael@0: michael@0: void vp8_encode_frame(VP8_COMP *cpi) michael@0: { michael@0: int mb_row; michael@0: MACROBLOCK *const x = & cpi->mb; michael@0: VP8_COMMON *const cm = & cpi->common; michael@0: MACROBLOCKD *const xd = & x->e_mbd; michael@0: TOKENEXTRA *tp = cpi->tok; michael@0: int segment_counts[MAX_MB_SEGMENTS]; michael@0: int totalrate; michael@0: #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING michael@0: BOOL_CODER * bc = &cpi->bc[1]; /* bc[0] is for control partition */ michael@0: const int num_part = (1 << cm->multi_token_partition); michael@0: #endif michael@0: michael@0: vpx_memset(segment_counts, 0, sizeof(segment_counts)); michael@0: totalrate = 0; michael@0: michael@0: if (cpi->compressor_speed == 2) michael@0: { michael@0: if (cpi->oxcf.cpu_used < 0) michael@0: cpi->Speed = -(cpi->oxcf.cpu_used); michael@0: else michael@0: vp8_auto_select_speed(cpi); michael@0: } michael@0: michael@0: /* Functions setup for all frame types so we can use MC in AltRef */ michael@0: if(!cm->use_bilinear_mc_filter) michael@0: { michael@0: xd->subpixel_predict = vp8_sixtap_predict4x4; michael@0: xd->subpixel_predict8x4 = vp8_sixtap_predict8x4; michael@0: xd->subpixel_predict8x8 = vp8_sixtap_predict8x8; michael@0: xd->subpixel_predict16x16 = vp8_sixtap_predict16x16; michael@0: } michael@0: else michael@0: { michael@0: xd->subpixel_predict = vp8_bilinear_predict4x4; michael@0: xd->subpixel_predict8x4 = vp8_bilinear_predict8x4; michael@0: xd->subpixel_predict8x8 = vp8_bilinear_predict8x8; michael@0: xd->subpixel_predict16x16 = vp8_bilinear_predict16x16; michael@0: } michael@0: michael@0: cpi->mb.skip_true_count = 0; michael@0: cpi->tok_count = 0; michael@0: michael@0: #if 0 michael@0: /* Experimental code */ michael@0: cpi->frame_distortion = 0; michael@0: cpi->last_mb_distortion = 0; michael@0: #endif michael@0: michael@0: xd->mode_info_context = cm->mi; michael@0: michael@0: vp8_zero(cpi->mb.MVcount); michael@0: michael@0: vp8cx_frame_init_quantizer(cpi); michael@0: michael@0: vp8_initialize_rd_consts(cpi, x, michael@0: vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q)); michael@0: michael@0: vp8cx_initialize_me_consts(cpi, cm->base_qindex); michael@0: michael@0: if(cpi->oxcf.tuning == VP8_TUNE_SSIM) michael@0: { 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-init encode frame context. */ michael@0: init_encode_frame_mb_context(cpi); michael@0: michael@0: #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING michael@0: { michael@0: int i; michael@0: for(i = 0; i < num_part; i++) michael@0: { michael@0: vp8_start_encode(&bc[i], cpi->partition_d[i + 1], michael@0: cpi->partition_d_end[i + 1]); michael@0: bc[i].error = &cm->error; michael@0: } michael@0: } michael@0: michael@0: #endif michael@0: michael@0: { michael@0: struct vpx_usec_timer emr_timer; michael@0: vpx_usec_timer_start(&emr_timer); michael@0: michael@0: #if CONFIG_MULTITHREAD michael@0: if (cpi->b_multi_threaded) michael@0: { michael@0: int i; michael@0: michael@0: vp8cx_init_mbrthread_data(cpi, x, cpi->mb_row_ei, michael@0: cpi->encoding_thread_count); michael@0: michael@0: for (i = 0; i < cm->mb_rows; i++) michael@0: cpi->mt_current_mb_col[i] = -1; michael@0: michael@0: for (i = 0; i < cpi->encoding_thread_count; i++) michael@0: { michael@0: sem_post(&cpi->h_event_start_encoding[i]); michael@0: } michael@0: michael@0: for (mb_row = 0; mb_row < cm->mb_rows; mb_row += (cpi->encoding_thread_count + 1)) michael@0: { michael@0: vp8_zero(cm->left_context) michael@0: michael@0: #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING michael@0: tp = cpi->tok; michael@0: #else michael@0: tp = cpi->tok + mb_row * (cm->mb_cols * 16 * 24); michael@0: #endif michael@0: michael@0: encode_mb_row(cpi, cm, mb_row, x, xd, &tp, segment_counts, &totalrate); michael@0: michael@0: /* adjust to the next row of mbs */ michael@0: x->src.y_buffer += 16 * x->src.y_stride * (cpi->encoding_thread_count + 1) - 16 * cm->mb_cols; michael@0: x->src.u_buffer += 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols; michael@0: x->src.v_buffer += 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols; michael@0: michael@0: xd->mode_info_context += xd->mode_info_stride * cpi->encoding_thread_count; michael@0: x->partition_info += xd->mode_info_stride * cpi->encoding_thread_count; michael@0: x->gf_active_ptr += cm->mb_cols * cpi->encoding_thread_count; michael@0: michael@0: if(mb_row == cm->mb_rows - 1) michael@0: { michael@0: sem_post(&cpi->h_event_end_encoding); /* signal frame encoding end */ michael@0: } michael@0: } michael@0: michael@0: sem_wait(&cpi->h_event_end_encoding); /* wait for other threads to finish */ michael@0: michael@0: for (mb_row = 0; mb_row < cm->mb_rows; mb_row ++) michael@0: { michael@0: cpi->tok_count += (unsigned int) michael@0: (cpi->tplist[mb_row].stop - cpi->tplist[mb_row].start); michael@0: } michael@0: michael@0: if (xd->segmentation_enabled) michael@0: { michael@0: int j; michael@0: michael@0: if (xd->segmentation_enabled) michael@0: { michael@0: for (i = 0; i < cpi->encoding_thread_count; i++) michael@0: { michael@0: for (j = 0; j < 4; j++) michael@0: segment_counts[j] += cpi->mb_row_ei[i].segment_counts[j]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: for (i = 0; i < cpi->encoding_thread_count; i++) michael@0: { michael@0: int mode_count; michael@0: int c_idx; michael@0: totalrate += cpi->mb_row_ei[i].totalrate; michael@0: michael@0: cpi->mb.skip_true_count += cpi->mb_row_ei[i].mb.skip_true_count; michael@0: michael@0: for(mode_count = 0; mode_count < VP8_YMODES; mode_count++) michael@0: cpi->mb.ymode_count[mode_count] += michael@0: cpi->mb_row_ei[i].mb.ymode_count[mode_count]; michael@0: michael@0: for(mode_count = 0; mode_count < VP8_UV_MODES; mode_count++) michael@0: cpi->mb.uv_mode_count[mode_count] += michael@0: cpi->mb_row_ei[i].mb.uv_mode_count[mode_count]; michael@0: michael@0: for(c_idx = 0; c_idx < MVvals; c_idx++) michael@0: { michael@0: cpi->mb.MVcount[0][c_idx] += michael@0: cpi->mb_row_ei[i].mb.MVcount[0][c_idx]; michael@0: cpi->mb.MVcount[1][c_idx] += michael@0: cpi->mb_row_ei[i].mb.MVcount[1][c_idx]; michael@0: } michael@0: michael@0: cpi->mb.prediction_error += michael@0: cpi->mb_row_ei[i].mb.prediction_error; michael@0: cpi->mb.intra_error += cpi->mb_row_ei[i].mb.intra_error; michael@0: michael@0: for(c_idx = 0; c_idx < MAX_REF_FRAMES; c_idx++) michael@0: cpi->mb.count_mb_ref_frame_usage[c_idx] += michael@0: cpi->mb_row_ei[i].mb.count_mb_ref_frame_usage[c_idx]; michael@0: michael@0: for(c_idx = 0; c_idx < MAX_ERROR_BINS; c_idx++) michael@0: cpi->mb.error_bins[c_idx] += michael@0: cpi->mb_row_ei[i].mb.error_bins[c_idx]; michael@0: michael@0: /* add up counts for each thread */ michael@0: sum_coef_counts(x, &cpi->mb_row_ei[i].mb); michael@0: } michael@0: michael@0: } michael@0: else michael@0: #endif michael@0: { 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: { michael@0: vp8_zero(cm->left_context) michael@0: michael@0: #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING michael@0: tp = cpi->tok; michael@0: #endif michael@0: michael@0: encode_mb_row(cpi, cm, mb_row, x, xd, &tp, segment_counts, &totalrate); michael@0: michael@0: /* adjust to the next row of mbs */ michael@0: x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols; michael@0: x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols; michael@0: x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols; michael@0: } michael@0: michael@0: cpi->tok_count = (unsigned int)(tp - cpi->tok); michael@0: } michael@0: michael@0: #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING michael@0: { michael@0: int i; michael@0: for(i = 0; i < num_part; i++) michael@0: { michael@0: vp8_stop_encode(&bc[i]); michael@0: cpi->partition_sz[i+1] = bc[i].pos; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: vpx_usec_timer_mark(&emr_timer); michael@0: cpi->time_encode_mb_row += vpx_usec_timer_elapsed(&emr_timer); michael@0: } michael@0: michael@0: michael@0: // Work out the segment probabilities if segmentation is enabled michael@0: // and needs to be updated michael@0: if (xd->segmentation_enabled && xd->update_mb_segmentation_map) michael@0: { michael@0: int tot_count; michael@0: int i; michael@0: michael@0: /* Set to defaults */ michael@0: vpx_memset(xd->mb_segment_tree_probs, 255 , sizeof(xd->mb_segment_tree_probs)); michael@0: michael@0: tot_count = segment_counts[0] + segment_counts[1] + segment_counts[2] + segment_counts[3]; michael@0: michael@0: if (tot_count) michael@0: { michael@0: xd->mb_segment_tree_probs[0] = ((segment_counts[0] + segment_counts[1]) * 255) / tot_count; michael@0: michael@0: tot_count = segment_counts[0] + segment_counts[1]; michael@0: michael@0: if (tot_count > 0) michael@0: { michael@0: xd->mb_segment_tree_probs[1] = (segment_counts[0] * 255) / tot_count; michael@0: } michael@0: michael@0: tot_count = segment_counts[2] + segment_counts[3]; michael@0: michael@0: if (tot_count > 0) michael@0: xd->mb_segment_tree_probs[2] = (segment_counts[2] * 255) / tot_count; michael@0: michael@0: /* Zero probabilities not allowed */ michael@0: for (i = 0; i < MB_FEATURE_TREE_PROBS; i ++) michael@0: { michael@0: if (xd->mb_segment_tree_probs[i] == 0) michael@0: xd->mb_segment_tree_probs[i] = 1; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* projected_frame_size in units of BYTES */ michael@0: cpi->projected_frame_size = totalrate >> 8; michael@0: michael@0: /* Make a note of the percentage MBs coded Intra. */ michael@0: if (cm->frame_type == KEY_FRAME) michael@0: { michael@0: cpi->this_frame_percent_intra = 100; michael@0: } michael@0: else michael@0: { michael@0: int tot_modes; michael@0: michael@0: tot_modes = cpi->mb.count_mb_ref_frame_usage[INTRA_FRAME] michael@0: + cpi->mb.count_mb_ref_frame_usage[LAST_FRAME] michael@0: + cpi->mb.count_mb_ref_frame_usage[GOLDEN_FRAME] michael@0: + cpi->mb.count_mb_ref_frame_usage[ALTREF_FRAME]; michael@0: michael@0: if (tot_modes) michael@0: cpi->this_frame_percent_intra = michael@0: cpi->mb.count_mb_ref_frame_usage[INTRA_FRAME] * 100 / tot_modes; michael@0: michael@0: } michael@0: michael@0: #if ! CONFIG_REALTIME_ONLY michael@0: /* Adjust the projected reference frame usage probability numbers to michael@0: * reflect what we have just seen. This may be useful when we make michael@0: * multiple iterations of the recode loop rather than continuing to use michael@0: * values from the previous frame. michael@0: */ michael@0: if ((cm->frame_type != KEY_FRAME) && ((cpi->oxcf.number_of_layers > 1) || michael@0: (!cm->refresh_alt_ref_frame && !cm->refresh_golden_frame))) michael@0: { michael@0: vp8_convert_rfct_to_prob(cpi); michael@0: } michael@0: #endif michael@0: } michael@0: void vp8_setup_block_ptrs(MACROBLOCK *x) michael@0: { michael@0: int r, c; michael@0: int i; michael@0: michael@0: for (r = 0; r < 4; r++) michael@0: { michael@0: for (c = 0; c < 4; c++) michael@0: { michael@0: x->block[r*4+c].src_diff = x->src_diff + r * 4 * 16 + c * 4; michael@0: } michael@0: } michael@0: michael@0: for (r = 0; r < 2; r++) michael@0: { michael@0: for (c = 0; c < 2; c++) michael@0: { michael@0: x->block[16 + r*2+c].src_diff = x->src_diff + 256 + r * 4 * 8 + c * 4; michael@0: } michael@0: } michael@0: michael@0: michael@0: for (r = 0; r < 2; r++) michael@0: { michael@0: for (c = 0; c < 2; c++) michael@0: { michael@0: x->block[20 + r*2+c].src_diff = x->src_diff + 320 + r * 4 * 8 + c * 4; michael@0: } michael@0: } michael@0: michael@0: x->block[24].src_diff = x->src_diff + 384; michael@0: michael@0: michael@0: for (i = 0; i < 25; i++) michael@0: { michael@0: x->block[i].coeff = x->coeff + i * 16; michael@0: } michael@0: } michael@0: michael@0: void vp8_build_block_offsets(MACROBLOCK *x) michael@0: { michael@0: int block = 0; michael@0: int br, bc; michael@0: michael@0: vp8_build_block_doffsets(&x->e_mbd); michael@0: michael@0: /* y blocks */ michael@0: x->thismb_ptr = &x->thismb[0]; michael@0: for (br = 0; br < 4; br++) michael@0: { michael@0: for (bc = 0; bc < 4; bc++) michael@0: { michael@0: BLOCK *this_block = &x->block[block]; michael@0: this_block->base_src = &x->thismb_ptr; michael@0: this_block->src_stride = 16; michael@0: this_block->src = 4 * br * 16 + 4 * bc; michael@0: ++block; michael@0: } michael@0: } michael@0: michael@0: /* u blocks */ michael@0: for (br = 0; br < 2; br++) michael@0: { michael@0: for (bc = 0; bc < 2; bc++) michael@0: { michael@0: BLOCK *this_block = &x->block[block]; michael@0: this_block->base_src = &x->src.u_buffer; michael@0: this_block->src_stride = x->src.uv_stride; michael@0: this_block->src = 4 * br * this_block->src_stride + 4 * bc; michael@0: ++block; michael@0: } michael@0: } michael@0: michael@0: /* v blocks */ michael@0: for (br = 0; br < 2; br++) michael@0: { michael@0: for (bc = 0; bc < 2; bc++) michael@0: { michael@0: BLOCK *this_block = &x->block[block]; michael@0: this_block->base_src = &x->src.v_buffer; michael@0: this_block->src_stride = x->src.uv_stride; michael@0: this_block->src = 4 * br * this_block->src_stride + 4 * bc; michael@0: ++block; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void sum_intra_stats(VP8_COMP *cpi, MACROBLOCK *x) michael@0: { michael@0: const MACROBLOCKD *xd = & x->e_mbd; michael@0: const MB_PREDICTION_MODE m = xd->mode_info_context->mbmi.mode; michael@0: const MB_PREDICTION_MODE uvm = xd->mode_info_context->mbmi.uv_mode; michael@0: michael@0: #ifdef MODE_STATS michael@0: const int is_key = cpi->common.frame_type == KEY_FRAME; michael@0: michael@0: ++ (is_key ? uv_modes : inter_uv_modes)[uvm]; michael@0: michael@0: if (m == B_PRED) michael@0: { michael@0: unsigned int *const bct = is_key ? b_modes : inter_b_modes; michael@0: michael@0: int b = 0; michael@0: michael@0: do michael@0: { michael@0: ++ bct[xd->block[b].bmi.mode]; michael@0: } michael@0: while (++b < 16); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: ++x->ymode_count[m]; michael@0: ++x->uv_mode_count[uvm]; 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: */ michael@0: static void adjust_act_zbin( VP8_COMP *cpi, MACROBLOCK *x ) michael@0: { 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: michael@0: int vp8cx_encode_intra_macroblock(VP8_COMP *cpi, MACROBLOCK *x, michael@0: TOKENEXTRA **t) michael@0: { michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: int rate; michael@0: michael@0: if (cpi->sf.RD && cpi->compressor_speed != 2) michael@0: vp8_rd_pick_intra_mode(x, &rate); michael@0: else michael@0: vp8_pick_intra_mode(x, &rate); michael@0: michael@0: if(cpi->oxcf.tuning == VP8_TUNE_SSIM) michael@0: { michael@0: adjust_act_zbin( cpi, x ); michael@0: vp8_update_zbin_extra(cpi, x); michael@0: } michael@0: michael@0: if (x->e_mbd.mode_info_context->mbmi.mode == B_PRED) michael@0: vp8_encode_intra4x4mby(x); michael@0: else michael@0: vp8_encode_intra16x16mby(x); michael@0: michael@0: vp8_encode_intra16x16mbuv(x); michael@0: michael@0: sum_intra_stats(cpi, x); michael@0: michael@0: vp8_tokenize_mb(cpi, x, t); michael@0: michael@0: if (xd->mode_info_context->mbmi.mode != B_PRED) michael@0: vp8_inverse_transform_mby(xd); michael@0: michael@0: vp8_dequant_idct_add_uv_block michael@0: (xd->qcoeff+16*16, xd->dequant_uv, michael@0: xd->dst.u_buffer, xd->dst.v_buffer, michael@0: xd->dst.uv_stride, xd->eobs+16); michael@0: return rate; michael@0: } michael@0: #ifdef SPEEDSTATS michael@0: extern int cnt_pm; michael@0: #endif michael@0: michael@0: extern void vp8_fix_contexts(MACROBLOCKD *x); michael@0: michael@0: int vp8cx_encode_inter_macroblock michael@0: ( michael@0: VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t, michael@0: int recon_yoffset, int recon_uvoffset, michael@0: int mb_row, int mb_col michael@0: ) michael@0: { michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: int intra_error = 0; michael@0: int rate; michael@0: int distortion; michael@0: michael@0: x->skip = 0; michael@0: michael@0: if (xd->segmentation_enabled) michael@0: x->encode_breakout = cpi->segment_encode_breakout[xd->mode_info_context->mbmi.segment_id]; michael@0: else michael@0: x->encode_breakout = cpi->oxcf.encode_breakout; michael@0: michael@0: #if CONFIG_TEMPORAL_DENOISING michael@0: /* Reset the best sse mode/mv for each macroblock. */ michael@0: x->best_reference_frame = INTRA_FRAME; michael@0: x->best_zeromv_reference_frame = INTRA_FRAME; michael@0: x->best_sse_inter_mode = 0; michael@0: x->best_sse_mv.as_int = 0; michael@0: x->need_to_clamp_best_mvs = 0; michael@0: #endif michael@0: michael@0: if (cpi->sf.RD) michael@0: { michael@0: int zbin_mode_boost_enabled = x->zbin_mode_boost_enabled; michael@0: michael@0: /* Are we using the fast quantizer for the mode selection? */ michael@0: if(cpi->sf.use_fastquant_for_pick) michael@0: { michael@0: x->quantize_b = vp8_fast_quantize_b; michael@0: x->quantize_b_pair = vp8_fast_quantize_b_pair; michael@0: michael@0: /* the fast quantizer does not use zbin_extra, so michael@0: * do not recalculate */ michael@0: x->zbin_mode_boost_enabled = 0; michael@0: } michael@0: vp8_rd_pick_inter_mode(cpi, x, recon_yoffset, recon_uvoffset, &rate, michael@0: &distortion, &intra_error); michael@0: michael@0: /* switch back to the regular quantizer for the encode */ michael@0: if (cpi->sf.improved_quant) michael@0: { michael@0: x->quantize_b = vp8_regular_quantize_b; michael@0: x->quantize_b_pair = vp8_regular_quantize_b_pair; michael@0: } michael@0: michael@0: /* restore cpi->zbin_mode_boost_enabled */ michael@0: x->zbin_mode_boost_enabled = zbin_mode_boost_enabled; michael@0: michael@0: } michael@0: else michael@0: { michael@0: vp8_pick_inter_mode(cpi, x, recon_yoffset, recon_uvoffset, &rate, michael@0: &distortion, &intra_error, mb_row, mb_col); michael@0: } michael@0: michael@0: x->prediction_error += distortion; michael@0: x->intra_error += intra_error; michael@0: michael@0: if(cpi->oxcf.tuning == VP8_TUNE_SSIM) michael@0: { michael@0: /* Adjust the zbin based on this MB rate. */ michael@0: adjust_act_zbin( cpi, x ); michael@0: } michael@0: michael@0: #if 0 michael@0: /* Experimental RD code */ michael@0: cpi->frame_distortion += distortion; michael@0: cpi->last_mb_distortion = distortion; michael@0: #endif michael@0: michael@0: /* MB level adjutment to quantizer setup */ michael@0: if (xd->segmentation_enabled) michael@0: { michael@0: /* If cyclic update enabled */ michael@0: if (cpi->current_layer == 0 && cpi->cyclic_refresh_mode_enabled) michael@0: { michael@0: /* Clear segment_id back to 0 if not coded (last frame 0,0) */ michael@0: if ((xd->mode_info_context->mbmi.segment_id == 1) && michael@0: ((xd->mode_info_context->mbmi.ref_frame != LAST_FRAME) || (xd->mode_info_context->mbmi.mode != ZEROMV))) michael@0: { michael@0: xd->mode_info_context->mbmi.segment_id = 0; michael@0: michael@0: /* segment_id changed, so update */ michael@0: vp8cx_mb_init_quantizer(cpi, x, 1); michael@0: } michael@0: } michael@0: } michael@0: michael@0: { michael@0: /* Experimental code. michael@0: * Special case for gf and arf zeromv modes, for 1 temporal layer. michael@0: * Increase zbin size to supress noise. michael@0: */ michael@0: x->zbin_mode_boost = 0; michael@0: if (x->zbin_mode_boost_enabled) michael@0: { michael@0: if ( xd->mode_info_context->mbmi.ref_frame != INTRA_FRAME ) michael@0: { michael@0: if (xd->mode_info_context->mbmi.mode == ZEROMV) michael@0: { michael@0: if (xd->mode_info_context->mbmi.ref_frame != LAST_FRAME && michael@0: cpi->oxcf.number_of_layers == 1) michael@0: x->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST; michael@0: else michael@0: x->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST; michael@0: } michael@0: else if (xd->mode_info_context->mbmi.mode == SPLITMV) michael@0: x->zbin_mode_boost = 0; michael@0: else michael@0: x->zbin_mode_boost = MV_ZBIN_BOOST; michael@0: } michael@0: } michael@0: michael@0: /* The fast quantizer doesn't use zbin_extra, only do so with michael@0: * the regular quantizer. */ michael@0: if (cpi->sf.improved_quant) michael@0: vp8_update_zbin_extra(cpi, x); michael@0: } michael@0: michael@0: x->count_mb_ref_frame_usage[xd->mode_info_context->mbmi.ref_frame] ++; michael@0: michael@0: if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME) michael@0: { michael@0: vp8_encode_intra16x16mbuv(x); michael@0: michael@0: if (xd->mode_info_context->mbmi.mode == B_PRED) michael@0: { michael@0: vp8_encode_intra4x4mby(x); michael@0: } michael@0: else michael@0: { michael@0: vp8_encode_intra16x16mby(x); michael@0: } michael@0: michael@0: sum_intra_stats(cpi, x); michael@0: } michael@0: else michael@0: { michael@0: int ref_fb_idx; michael@0: michael@0: if (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME) michael@0: ref_fb_idx = cpi->common.lst_fb_idx; michael@0: else if (xd->mode_info_context->mbmi.ref_frame == GOLDEN_FRAME) michael@0: ref_fb_idx = cpi->common.gld_fb_idx; michael@0: else michael@0: ref_fb_idx = cpi->common.alt_fb_idx; michael@0: michael@0: xd->pre.y_buffer = cpi->common.yv12_fb[ref_fb_idx].y_buffer + recon_yoffset; michael@0: xd->pre.u_buffer = cpi->common.yv12_fb[ref_fb_idx].u_buffer + recon_uvoffset; michael@0: xd->pre.v_buffer = cpi->common.yv12_fb[ref_fb_idx].v_buffer + recon_uvoffset; michael@0: michael@0: if (!x->skip) michael@0: { michael@0: vp8_encode_inter16x16(x); michael@0: } michael@0: else michael@0: vp8_build_inter16x16_predictors_mb(xd, xd->dst.y_buffer, michael@0: xd->dst.u_buffer, xd->dst.v_buffer, michael@0: xd->dst.y_stride, xd->dst.uv_stride); michael@0: michael@0: } michael@0: michael@0: if (!x->skip) michael@0: { michael@0: vp8_tokenize_mb(cpi, x, t); michael@0: michael@0: if (xd->mode_info_context->mbmi.mode != B_PRED) michael@0: vp8_inverse_transform_mby(xd); michael@0: michael@0: vp8_dequant_idct_add_uv_block michael@0: (xd->qcoeff+16*16, xd->dequant_uv, michael@0: xd->dst.u_buffer, xd->dst.v_buffer, michael@0: xd->dst.uv_stride, xd->eobs+16); michael@0: } michael@0: else michael@0: { michael@0: /* always set mb_skip_coeff as it is needed by the loopfilter */ michael@0: xd->mode_info_context->mbmi.mb_skip_coeff = 1; michael@0: michael@0: if (cpi->common.mb_no_coeff_skip) michael@0: { michael@0: x->skip_true_count ++; michael@0: vp8_fix_contexts(xd); michael@0: } michael@0: else michael@0: { michael@0: vp8_stuff_mb(cpi, x, t); michael@0: } michael@0: } michael@0: michael@0: return rate; michael@0: }