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 "vp9/common/vp9_onyxc_int.h" michael@0: #include "vp9/encoder/vp9_onyx_int.h" michael@0: #include "vp9/encoder/vp9_picklpf.h" michael@0: #include "vp9/encoder/vp9_quantize.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: #include "vpx_scale/vpx_scale.h" michael@0: #include "vp9/common/vp9_alloccommon.h" michael@0: #include "vp9/common/vp9_loopfilter.h" michael@0: #include "./vpx_scale_rtcd.h" michael@0: michael@0: void vp9_yv12_copy_partial_frame_c(YV12_BUFFER_CONFIG *src_ybc, michael@0: YV12_BUFFER_CONFIG *dst_ybc, int fraction) { michael@0: const int height = src_ybc->y_height; michael@0: const int stride = src_ybc->y_stride; michael@0: const int offset = stride * ((height >> 5) * 16 - 8); michael@0: const int lines_to_copy = MAX(height >> (fraction + 4), 1) << 4; michael@0: michael@0: assert(src_ybc->y_stride == dst_ybc->y_stride); michael@0: vpx_memcpy(dst_ybc->y_buffer + offset, src_ybc->y_buffer + offset, michael@0: stride * (lines_to_copy + 16)); michael@0: } michael@0: michael@0: static int calc_partial_ssl_err(YV12_BUFFER_CONFIG *source, michael@0: YV12_BUFFER_CONFIG *dest, int Fraction) { michael@0: int i, j; michael@0: int Total = 0; michael@0: int srcoffset, dstoffset; michael@0: uint8_t *src = source->y_buffer; michael@0: uint8_t *dst = dest->y_buffer; michael@0: michael@0: int linestocopy = (source->y_height >> (Fraction + 4)); michael@0: michael@0: if (linestocopy < 1) michael@0: linestocopy = 1; michael@0: michael@0: linestocopy <<= 4; michael@0: michael@0: michael@0: srcoffset = source->y_stride * (dest->y_height >> 5) * 16; michael@0: dstoffset = dest->y_stride * (dest->y_height >> 5) * 16; michael@0: michael@0: src += srcoffset; michael@0: dst += dstoffset; michael@0: michael@0: // Loop through the raw Y plane and reconstruction data summing the square michael@0: // differences. michael@0: for (i = 0; i < linestocopy; i += 16) { michael@0: for (j = 0; j < source->y_width; j += 16) { michael@0: unsigned int sse; michael@0: Total += vp9_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride, michael@0: &sse); michael@0: } michael@0: michael@0: src += 16 * source->y_stride; michael@0: dst += 16 * dest->y_stride; michael@0: } michael@0: michael@0: return Total; michael@0: } michael@0: michael@0: // Enforce a minimum filter level based upon baseline Q michael@0: static int get_min_filter_level(VP9_COMP *cpi, int base_qindex) { michael@0: int min_filter_level; michael@0: min_filter_level = 0; michael@0: michael@0: return min_filter_level; michael@0: } michael@0: michael@0: // Enforce a maximum filter level based upon baseline Q michael@0: static int get_max_filter_level(VP9_COMP *cpi, int base_qindex) { michael@0: int max_filter_level = MAX_LOOP_FILTER; michael@0: (void)base_qindex; michael@0: michael@0: if (cpi->twopass.section_intra_rating > 8) michael@0: max_filter_level = MAX_LOOP_FILTER * 3 / 4; michael@0: michael@0: return max_filter_level; michael@0: } michael@0: michael@0: michael@0: // Stub function for now Alt LF not used michael@0: void vp9_set_alt_lf_level(VP9_COMP *cpi, int filt_val) { michael@0: } michael@0: michael@0: void vp9_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi, int partial) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: struct loopfilter *const lf = &cm->lf; michael@0: michael@0: int best_err = 0; michael@0: int filt_err = 0; michael@0: const int min_filter_level = get_min_filter_level(cpi, cm->base_qindex); michael@0: const int max_filter_level = get_max_filter_level(cpi, cm->base_qindex); michael@0: michael@0: int filter_step; michael@0: int filt_high = 0; michael@0: // Start search at previous frame filter level michael@0: int filt_mid = lf->filter_level; michael@0: int filt_low = 0; michael@0: int filt_best; michael@0: int filt_direction = 0; michael@0: michael@0: int Bias = 0; // Bias against raising loop filter in favor of lowering it. michael@0: michael@0: // Make a copy of the unfiltered / processed recon buffer michael@0: vpx_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf); michael@0: michael@0: lf->sharpness_level = cm->frame_type == KEY_FRAME ? 0 michael@0: : cpi->oxcf.Sharpness; michael@0: michael@0: // Start the search at the previous frame filter level unless it is now out of michael@0: // range. michael@0: filt_mid = clamp(lf->filter_level, min_filter_level, max_filter_level); michael@0: michael@0: // Define the initial step size michael@0: filter_step = filt_mid < 16 ? 4 : filt_mid / 4; michael@0: michael@0: // Get baseline error score michael@0: vp9_set_alt_lf_level(cpi, filt_mid); michael@0: vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_mid, 1, partial); michael@0: michael@0: best_err = vp9_calc_ss_err(sd, cm->frame_to_show); michael@0: filt_best = filt_mid; michael@0: michael@0: // Re-instate the unfiltered frame michael@0: vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show); michael@0: michael@0: while (filter_step > 0) { michael@0: Bias = (best_err >> (15 - (filt_mid / 8))) * filter_step; michael@0: michael@0: if (cpi->twopass.section_intra_rating < 20) michael@0: Bias = Bias * cpi->twopass.section_intra_rating / 20; michael@0: michael@0: // yx, bias less for large block size michael@0: if (cpi->common.tx_mode != ONLY_4X4) michael@0: Bias >>= 1; michael@0: michael@0: filt_high = ((filt_mid + filter_step) > max_filter_level) michael@0: ? max_filter_level michael@0: : (filt_mid + filter_step); michael@0: filt_low = ((filt_mid - filter_step) < min_filter_level) michael@0: ? min_filter_level michael@0: : (filt_mid - filter_step); michael@0: michael@0: if ((filt_direction <= 0) && (filt_low != filt_mid)) { michael@0: // Get Low filter error score michael@0: vp9_set_alt_lf_level(cpi, filt_low); michael@0: vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_low, 1, partial); michael@0: michael@0: filt_err = vp9_calc_ss_err(sd, cm->frame_to_show); michael@0: michael@0: // Re-instate the unfiltered frame michael@0: vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show); michael@0: michael@0: // If value is close to the best so far then bias towards a lower loop michael@0: // filter value. michael@0: if ((filt_err - Bias) < best_err) { michael@0: // Was it actually better than the previous best? michael@0: if (filt_err < best_err) michael@0: best_err = filt_err; michael@0: michael@0: filt_best = filt_low; michael@0: } michael@0: } michael@0: michael@0: // Now look at filt_high michael@0: if ((filt_direction >= 0) && (filt_high != filt_mid)) { michael@0: vp9_set_alt_lf_level(cpi, filt_high); michael@0: vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_high, 1, partial); michael@0: michael@0: filt_err = vp9_calc_ss_err(sd, cm->frame_to_show); michael@0: michael@0: // Re-instate the unfiltered frame michael@0: vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show); michael@0: michael@0: // Was it better than the previous best? michael@0: if (filt_err < (best_err - Bias)) { michael@0: best_err = filt_err; michael@0: filt_best = filt_high; michael@0: } michael@0: } michael@0: michael@0: // Half the step distance if the best filter value was the same as last time michael@0: if (filt_best == filt_mid) { michael@0: filter_step = filter_step / 2; michael@0: filt_direction = 0; michael@0: } else { michael@0: filt_direction = (filt_best < filt_mid) ? -1 : 1; michael@0: filt_mid = filt_best; michael@0: } michael@0: } michael@0: michael@0: lf->filter_level = filt_best; michael@0: }