vp9_picklpf.c 7.14 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9 10
 */

11
#include <assert.h>
12
#include <limits.h>
13
#include "vp9/common/vp9_onyxc_int.h"
14
#include "vp9/encoder/vp9_onyx_int.h"
15
#include "vp9/encoder/vp9_picklpf.h"
16
#include "vp9/encoder/vp9_quantize.h"
John Koleszar's avatar
John Koleszar committed
17
#include "vpx_mem/vpx_mem.h"
Johann's avatar
Johann committed
18
#include "vpx_scale/vpx_scale.h"
19 20
#include "vp9/common/vp9_alloccommon.h"
#include "vp9/common/vp9_loopfilter.h"
21
#include "./vpx_scale_rtcd.h"
John Koleszar's avatar
John Koleszar committed
22

23 24
void vp9_yv12_copy_partial_frame_c(YV12_BUFFER_CONFIG *src_ybc,
                                   YV12_BUFFER_CONFIG *dst_ybc, int Fraction) {
25
  uint8_t *src_y, *dst_y;
John Koleszar's avatar
John Koleszar committed
26 27 28 29
  int yheight;
  int ystride;
  int yoffset;
  int linestocopy;
John Koleszar's avatar
John Koleszar committed
30

31
  assert(src_ybc->y_stride == dst_ybc->y_stride);
John Koleszar's avatar
John Koleszar committed
32 33
  yheight  = src_ybc->y_height;
  ystride  = src_ybc->y_stride;
John Koleszar's avatar
John Koleszar committed
34

John Koleszar's avatar
John Koleszar committed
35
  linestocopy = (yheight >> (Fraction + 4));
John Koleszar's avatar
John Koleszar committed
36

John Koleszar's avatar
John Koleszar committed
37 38
  if (linestocopy < 1)
    linestocopy = 1;
John Koleszar's avatar
John Koleszar committed
39

John Koleszar's avatar
John Koleszar committed
40
  linestocopy <<= 4;
John Koleszar's avatar
John Koleszar committed
41

John Koleszar's avatar
John Koleszar committed
42 43 44
  yoffset  = ystride * ((yheight >> 5) * 16 - 8);
  src_y = src_ybc->y_buffer + yoffset;
  dst_y = dst_ybc->y_buffer + yoffset;
John Koleszar's avatar
John Koleszar committed
45

John Koleszar's avatar
John Koleszar committed
46
  vpx_memcpy(dst_y, src_y, ystride * (linestocopy + 16));
John Koleszar's avatar
John Koleszar committed
47
}
48

49 50
static int calc_partial_ssl_err(YV12_BUFFER_CONFIG *source,
                                YV12_BUFFER_CONFIG *dest, int Fraction) {
John Koleszar's avatar
John Koleszar committed
51 52 53
  int i, j;
  int Total = 0;
  int srcoffset, dstoffset;
54 55
  uint8_t *src = source->y_buffer;
  uint8_t *dst = dest->y_buffer;
John Koleszar's avatar
John Koleszar committed
56

John Koleszar's avatar
John Koleszar committed
57
  int linestocopy = (source->y_height >> (Fraction + 4));
John Koleszar's avatar
John Koleszar committed
58

John Koleszar's avatar
John Koleszar committed
59 60
  if (linestocopy < 1)
    linestocopy = 1;
John Koleszar's avatar
John Koleszar committed
61

John Koleszar's avatar
John Koleszar committed
62
  linestocopy <<= 4;
John Koleszar's avatar
John Koleszar committed
63 64


John Koleszar's avatar
John Koleszar committed
65 66
  srcoffset = source->y_stride   * (dest->y_height >> 5) * 16;
  dstoffset = dest->y_stride     * (dest->y_height >> 5) * 16;
John Koleszar's avatar
John Koleszar committed
67

John Koleszar's avatar
John Koleszar committed
68 69
  src += srcoffset;
  dst += dstoffset;
John Koleszar's avatar
John Koleszar committed
70

John Koleszar's avatar
John Koleszar committed
71 72 73 74
  // Loop through the Y plane raw and reconstruction data summing (square differences)
  for (i = 0; i < linestocopy; i += 16) {
    for (j = 0; j < source->y_width; j += 16) {
      unsigned int sse;
75
      Total += vp9_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride,
76
                            &sse);
John Koleszar's avatar
John Koleszar committed
77 78
    }

John Koleszar's avatar
John Koleszar committed
79 80 81 82 83
    src += 16 * source->y_stride;
    dst += 16 * dest->y_stride;
  }

  return Total;
John Koleszar's avatar
John Koleszar committed
84 85 86
}

// Enforce a minimum filter level based upon baseline Q
87
static int get_min_filter_level(VP9_COMP *cpi, int base_qindex) {
John Koleszar's avatar
John Koleszar committed
88
  int min_filter_level;
89
  /*int q = (int) vp9_convert_qindex_to_q(base_qindex);
John Koleszar's avatar
John Koleszar committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

  if (cpi->source_alt_ref_active && cpi->common.refresh_golden_frame && !cpi->common.refresh_alt_ref_frame)
      min_filter_level = 0;
  else
  {
      if (q <= 10)
          min_filter_level = 0;
      else if (q <= 64)
          min_filter_level = 1;
      else
          min_filter_level = (q >> 6);
  }
  */
  min_filter_level = 0;

  return min_filter_level;
John Koleszar's avatar
John Koleszar committed
106 107 108
}

// Enforce a maximum filter level based upon baseline Q
109
static int get_max_filter_level(VP9_COMP *cpi, int base_qindex) {
John Koleszar's avatar
John Koleszar committed
110
  // PGW August 2006: Highest filter values almost always a bad idea
John Koleszar's avatar
John Koleszar committed
111

John Koleszar's avatar
John Koleszar committed
112 113 114 115
  // jbb chg: 20100118 - not so any more with this overquant stuff allow high values
  // with lots of intra coming in.
  int max_filter_level = MAX_LOOP_FILTER;// * 3 / 4;
  (void)base_qindex;
John Koleszar's avatar
John Koleszar committed
116

John Koleszar's avatar
John Koleszar committed
117 118
  if (cpi->twopass.section_intra_rating > 8)
    max_filter_level = MAX_LOOP_FILTER * 3 / 4;
John Koleszar's avatar
John Koleszar committed
119

John Koleszar's avatar
John Koleszar committed
120
  return max_filter_level;
John Koleszar's avatar
John Koleszar committed
121 122 123 124
}


// Stub function for now Alt LF not used
125
void vp9_set_alt_lf_level(VP9_COMP *cpi, int filt_val) {
126
}
John Koleszar's avatar
John Koleszar committed
127

128
void vp9_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi, int partial) {
129
  VP9_COMMON *cm = &cpi->common;
130
  struct loopfilter *lf = &cpi->common.lf;
John Koleszar's avatar
John Koleszar committed
131

John Koleszar's avatar
John Koleszar committed
132 133 134 135
  int best_err = 0;
  int filt_err = 0;
  int min_filter_level = get_min_filter_level(cpi, cm->base_qindex);
  int max_filter_level = get_max_filter_level(cpi, cm->base_qindex);
John Koleszar's avatar
John Koleszar committed
136

John Koleszar's avatar
John Koleszar committed
137 138
  int filter_step;
  int filt_high = 0;
139 140
  // Start search at previous frame filter level
  int filt_mid = lf->filter_level;
John Koleszar's avatar
John Koleszar committed
141 142 143
  int filt_low = 0;
  int filt_best;
  int filt_direction = 0;
John Koleszar's avatar
John Koleszar committed
144

John Koleszar's avatar
John Koleszar committed
145
  int Bias = 0;                       // Bias against raising loop filter and in favour of lowering it
John Koleszar's avatar
John Koleszar committed
146

John Koleszar's avatar
John Koleszar committed
147
  //  Make a copy of the unfiltered / processed recon buffer
148
  vpx_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf);
John Koleszar's avatar
John Koleszar committed
149

John Koleszar's avatar
John Koleszar committed
150
  if (cm->frame_type == KEY_FRAME)
151
    lf->sharpness_level = 0;
John Koleszar's avatar
John Koleszar committed
152
  else
153
    lf->sharpness_level = cpi->oxcf.Sharpness;
John Koleszar's avatar
John Koleszar committed
154

John Koleszar's avatar
John Koleszar committed
155
  // Start the search at the previous frame filter level unless it is now out of range.
156
  filt_mid = lf->filter_level;
John Koleszar's avatar
John Koleszar committed
157

John Koleszar's avatar
John Koleszar committed
158 159 160 161
  if (filt_mid < min_filter_level)
    filt_mid = min_filter_level;
  else if (filt_mid > max_filter_level)
    filt_mid = max_filter_level;
John Koleszar's avatar
John Koleszar committed
162

John Koleszar's avatar
John Koleszar committed
163 164
  // Define the initial step size
  filter_step = (filt_mid < 16) ? 4 : filt_mid / 4;
John Koleszar's avatar
John Koleszar committed
165

John Koleszar's avatar
John Koleszar committed
166
  // Get baseline error score
167
  vp9_set_alt_lf_level(cpi, filt_mid);
168
  vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_mid, 1, partial);
John Koleszar's avatar
John Koleszar committed
169

170
  best_err = vp9_calc_ss_err(sd, cm->frame_to_show);
John Koleszar's avatar
John Koleszar committed
171
  filt_best = filt_mid;
John Koleszar's avatar
John Koleszar committed
172

John Koleszar's avatar
John Koleszar committed
173
  //  Re-instate the unfiltered frame
174
  vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
John Koleszar's avatar
John Koleszar committed
175

John Koleszar's avatar
John Koleszar committed
176 177
  while (filter_step > 0) {
    Bias = (best_err >> (15 - (filt_mid / 8))) * filter_step; // PGW change 12/12/06 for small images
John Koleszar's avatar
John Koleszar committed
178

John Koleszar's avatar
John Koleszar committed
179 180 181
    // jbb chg: 20100118 - in sections with lots of new material coming in don't bias as much to a low filter value
    if (cpi->twopass.section_intra_rating < 20)
      Bias = Bias * cpi->twopass.section_intra_rating / 20;
John Koleszar's avatar
John Koleszar committed
182

John Koleszar's avatar
John Koleszar committed
183
    // yx, bias less for large block size
184
    if (cpi->common.tx_mode != ONLY_4X4)
John Koleszar's avatar
John Koleszar committed
185
      Bias >>= 1;
186

John Koleszar's avatar
John Koleszar committed
187 188
    filt_high = ((filt_mid + filter_step) > max_filter_level) ? max_filter_level : (filt_mid + filter_step);
    filt_low = ((filt_mid - filter_step) < min_filter_level) ? min_filter_level : (filt_mid - filter_step);
John Koleszar's avatar
John Koleszar committed
189

John Koleszar's avatar
John Koleszar committed
190 191
    if ((filt_direction <= 0) && (filt_low != filt_mid)) {
      // Get Low filter error score
192
      vp9_set_alt_lf_level(cpi, filt_low);
193
      vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_low, 1, partial);
John Koleszar's avatar
John Koleszar committed
194

195
      filt_err = vp9_calc_ss_err(sd, cm->frame_to_show);
John Koleszar's avatar
John Koleszar committed
196

John Koleszar's avatar
John Koleszar committed
197
      //  Re-instate the unfiltered frame
198
      vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
John Koleszar's avatar
John Koleszar committed
199

John Koleszar's avatar
John Koleszar committed
200 201 202 203 204
      // If value is close to the best so far then bias towards a lower loop filter value.
      if ((filt_err - Bias) < best_err) {
        // Was it actually better than the previous best?
        if (filt_err < best_err)
          best_err = filt_err;
John Koleszar's avatar
John Koleszar committed
205

John Koleszar's avatar
John Koleszar committed
206 207 208
        filt_best = filt_low;
      }
    }
John Koleszar's avatar
John Koleszar committed
209

John Koleszar's avatar
John Koleszar committed
210 211
    // Now look at filt_high
    if ((filt_direction >= 0) && (filt_high != filt_mid)) {
212
      vp9_set_alt_lf_level(cpi, filt_high);
213
      vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_high, 1, partial);
John Koleszar's avatar
John Koleszar committed
214

215
      filt_err = vp9_calc_ss_err(sd, cm->frame_to_show);
John Koleszar's avatar
John Koleszar committed
216

John Koleszar's avatar
John Koleszar committed
217
      //  Re-instate the unfiltered frame
218
      vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
John Koleszar's avatar
John Koleszar committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

      // Was it better than the previous best?
      if (filt_err < (best_err - Bias)) {
        best_err = filt_err;
        filt_best = filt_high;
      }
    }

    // Half the step distance if the best filter value was the same as last time
    if (filt_best == filt_mid) {
      filter_step = filter_step / 2;
      filt_direction = 0;
    } else {
      filt_direction = (filt_best < filt_mid) ? -1 : 1;
      filt_mid = filt_best;
John Koleszar's avatar
John Koleszar committed
234
    }
John Koleszar's avatar
John Koleszar committed
235
  }
John Koleszar's avatar
John Koleszar committed
236

237
  lf->filter_level = filt_best;
John Koleszar's avatar
John Koleszar committed
238
}
239