media/libvpx/vp9/encoder/vp9_picklpf.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license
michael@0 5 * that can be found in the LICENSE file in the root of the source
michael@0 6 * tree. An additional intellectual property rights grant can be found
michael@0 7 * in the file PATENTS. All contributing project authors may
michael@0 8 * be found in the AUTHORS file in the root of the source tree.
michael@0 9 */
michael@0 10
michael@0 11 #include <assert.h>
michael@0 12 #include <limits.h>
michael@0 13 #include "vp9/common/vp9_onyxc_int.h"
michael@0 14 #include "vp9/encoder/vp9_onyx_int.h"
michael@0 15 #include "vp9/encoder/vp9_picklpf.h"
michael@0 16 #include "vp9/encoder/vp9_quantize.h"
michael@0 17 #include "vpx_mem/vpx_mem.h"
michael@0 18 #include "vpx_scale/vpx_scale.h"
michael@0 19 #include "vp9/common/vp9_alloccommon.h"
michael@0 20 #include "vp9/common/vp9_loopfilter.h"
michael@0 21 #include "./vpx_scale_rtcd.h"
michael@0 22
michael@0 23 void vp9_yv12_copy_partial_frame_c(YV12_BUFFER_CONFIG *src_ybc,
michael@0 24 YV12_BUFFER_CONFIG *dst_ybc, int fraction) {
michael@0 25 const int height = src_ybc->y_height;
michael@0 26 const int stride = src_ybc->y_stride;
michael@0 27 const int offset = stride * ((height >> 5) * 16 - 8);
michael@0 28 const int lines_to_copy = MAX(height >> (fraction + 4), 1) << 4;
michael@0 29
michael@0 30 assert(src_ybc->y_stride == dst_ybc->y_stride);
michael@0 31 vpx_memcpy(dst_ybc->y_buffer + offset, src_ybc->y_buffer + offset,
michael@0 32 stride * (lines_to_copy + 16));
michael@0 33 }
michael@0 34
michael@0 35 static int calc_partial_ssl_err(YV12_BUFFER_CONFIG *source,
michael@0 36 YV12_BUFFER_CONFIG *dest, int Fraction) {
michael@0 37 int i, j;
michael@0 38 int Total = 0;
michael@0 39 int srcoffset, dstoffset;
michael@0 40 uint8_t *src = source->y_buffer;
michael@0 41 uint8_t *dst = dest->y_buffer;
michael@0 42
michael@0 43 int linestocopy = (source->y_height >> (Fraction + 4));
michael@0 44
michael@0 45 if (linestocopy < 1)
michael@0 46 linestocopy = 1;
michael@0 47
michael@0 48 linestocopy <<= 4;
michael@0 49
michael@0 50
michael@0 51 srcoffset = source->y_stride * (dest->y_height >> 5) * 16;
michael@0 52 dstoffset = dest->y_stride * (dest->y_height >> 5) * 16;
michael@0 53
michael@0 54 src += srcoffset;
michael@0 55 dst += dstoffset;
michael@0 56
michael@0 57 // Loop through the raw Y plane and reconstruction data summing the square
michael@0 58 // differences.
michael@0 59 for (i = 0; i < linestocopy; i += 16) {
michael@0 60 for (j = 0; j < source->y_width; j += 16) {
michael@0 61 unsigned int sse;
michael@0 62 Total += vp9_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride,
michael@0 63 &sse);
michael@0 64 }
michael@0 65
michael@0 66 src += 16 * source->y_stride;
michael@0 67 dst += 16 * dest->y_stride;
michael@0 68 }
michael@0 69
michael@0 70 return Total;
michael@0 71 }
michael@0 72
michael@0 73 // Enforce a minimum filter level based upon baseline Q
michael@0 74 static int get_min_filter_level(VP9_COMP *cpi, int base_qindex) {
michael@0 75 int min_filter_level;
michael@0 76 min_filter_level = 0;
michael@0 77
michael@0 78 return min_filter_level;
michael@0 79 }
michael@0 80
michael@0 81 // Enforce a maximum filter level based upon baseline Q
michael@0 82 static int get_max_filter_level(VP9_COMP *cpi, int base_qindex) {
michael@0 83 int max_filter_level = MAX_LOOP_FILTER;
michael@0 84 (void)base_qindex;
michael@0 85
michael@0 86 if (cpi->twopass.section_intra_rating > 8)
michael@0 87 max_filter_level = MAX_LOOP_FILTER * 3 / 4;
michael@0 88
michael@0 89 return max_filter_level;
michael@0 90 }
michael@0 91
michael@0 92
michael@0 93 // Stub function for now Alt LF not used
michael@0 94 void vp9_set_alt_lf_level(VP9_COMP *cpi, int filt_val) {
michael@0 95 }
michael@0 96
michael@0 97 void vp9_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi, int partial) {
michael@0 98 VP9_COMMON *const cm = &cpi->common;
michael@0 99 struct loopfilter *const lf = &cm->lf;
michael@0 100
michael@0 101 int best_err = 0;
michael@0 102 int filt_err = 0;
michael@0 103 const int min_filter_level = get_min_filter_level(cpi, cm->base_qindex);
michael@0 104 const int max_filter_level = get_max_filter_level(cpi, cm->base_qindex);
michael@0 105
michael@0 106 int filter_step;
michael@0 107 int filt_high = 0;
michael@0 108 // Start search at previous frame filter level
michael@0 109 int filt_mid = lf->filter_level;
michael@0 110 int filt_low = 0;
michael@0 111 int filt_best;
michael@0 112 int filt_direction = 0;
michael@0 113
michael@0 114 int Bias = 0; // Bias against raising loop filter in favor of lowering it.
michael@0 115
michael@0 116 // Make a copy of the unfiltered / processed recon buffer
michael@0 117 vpx_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf);
michael@0 118
michael@0 119 lf->sharpness_level = cm->frame_type == KEY_FRAME ? 0
michael@0 120 : cpi->oxcf.Sharpness;
michael@0 121
michael@0 122 // Start the search at the previous frame filter level unless it is now out of
michael@0 123 // range.
michael@0 124 filt_mid = clamp(lf->filter_level, min_filter_level, max_filter_level);
michael@0 125
michael@0 126 // Define the initial step size
michael@0 127 filter_step = filt_mid < 16 ? 4 : filt_mid / 4;
michael@0 128
michael@0 129 // Get baseline error score
michael@0 130 vp9_set_alt_lf_level(cpi, filt_mid);
michael@0 131 vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_mid, 1, partial);
michael@0 132
michael@0 133 best_err = vp9_calc_ss_err(sd, cm->frame_to_show);
michael@0 134 filt_best = filt_mid;
michael@0 135
michael@0 136 // Re-instate the unfiltered frame
michael@0 137 vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
michael@0 138
michael@0 139 while (filter_step > 0) {
michael@0 140 Bias = (best_err >> (15 - (filt_mid / 8))) * filter_step;
michael@0 141
michael@0 142 if (cpi->twopass.section_intra_rating < 20)
michael@0 143 Bias = Bias * cpi->twopass.section_intra_rating / 20;
michael@0 144
michael@0 145 // yx, bias less for large block size
michael@0 146 if (cpi->common.tx_mode != ONLY_4X4)
michael@0 147 Bias >>= 1;
michael@0 148
michael@0 149 filt_high = ((filt_mid + filter_step) > max_filter_level)
michael@0 150 ? max_filter_level
michael@0 151 : (filt_mid + filter_step);
michael@0 152 filt_low = ((filt_mid - filter_step) < min_filter_level)
michael@0 153 ? min_filter_level
michael@0 154 : (filt_mid - filter_step);
michael@0 155
michael@0 156 if ((filt_direction <= 0) && (filt_low != filt_mid)) {
michael@0 157 // Get Low filter error score
michael@0 158 vp9_set_alt_lf_level(cpi, filt_low);
michael@0 159 vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_low, 1, partial);
michael@0 160
michael@0 161 filt_err = vp9_calc_ss_err(sd, cm->frame_to_show);
michael@0 162
michael@0 163 // Re-instate the unfiltered frame
michael@0 164 vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
michael@0 165
michael@0 166 // If value is close to the best so far then bias towards a lower loop
michael@0 167 // filter value.
michael@0 168 if ((filt_err - Bias) < best_err) {
michael@0 169 // Was it actually better than the previous best?
michael@0 170 if (filt_err < best_err)
michael@0 171 best_err = filt_err;
michael@0 172
michael@0 173 filt_best = filt_low;
michael@0 174 }
michael@0 175 }
michael@0 176
michael@0 177 // Now look at filt_high
michael@0 178 if ((filt_direction >= 0) && (filt_high != filt_mid)) {
michael@0 179 vp9_set_alt_lf_level(cpi, filt_high);
michael@0 180 vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_high, 1, partial);
michael@0 181
michael@0 182 filt_err = vp9_calc_ss_err(sd, cm->frame_to_show);
michael@0 183
michael@0 184 // Re-instate the unfiltered frame
michael@0 185 vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
michael@0 186
michael@0 187 // Was it better than the previous best?
michael@0 188 if (filt_err < (best_err - Bias)) {
michael@0 189 best_err = filt_err;
michael@0 190 filt_best = filt_high;
michael@0 191 }
michael@0 192 }
michael@0 193
michael@0 194 // Half the step distance if the best filter value was the same as last time
michael@0 195 if (filt_best == filt_mid) {
michael@0 196 filter_step = filter_step / 2;
michael@0 197 filt_direction = 0;
michael@0 198 } else {
michael@0 199 filt_direction = (filt_best < filt_mid) ? -1 : 1;
michael@0 200 filt_mid = filt_best;
michael@0 201 }
michael@0 202 }
michael@0 203
michael@0 204 lf->filter_level = filt_best;
michael@0 205 }

mercurial