vp9_picklpf.c 10.2 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
 */


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

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

John Koleszar's avatar
John Koleszar committed
30 31
  yheight  = src_ybc->y_height;
  ystride  = src_ybc->y_stride;
John Koleszar's avatar
John Koleszar committed
32

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

John Koleszar's avatar
John Koleszar committed
35 36
  if (linestocopy < 1)
    linestocopy = 1;
John Koleszar's avatar
John Koleszar committed
37

John Koleszar's avatar
John Koleszar committed
38
  linestocopy <<= 4;
John Koleszar's avatar
John Koleszar committed
39

John Koleszar's avatar
John Koleszar committed
40 41 42
  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
43

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

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

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

John Koleszar's avatar
John Koleszar committed
57 58
  if (linestocopy < 1)
    linestocopy = 1;
John Koleszar's avatar
John Koleszar committed
59

John Koleszar's avatar
John Koleszar committed
60
  linestocopy <<= 4;
John Koleszar's avatar
John Koleszar committed
61 62


John Koleszar's avatar
John Koleszar committed
63 64
  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
65

John Koleszar's avatar
John Koleszar committed
66 67
  src += srcoffset;
  dst += dstoffset;
John Koleszar's avatar
John Koleszar committed
68

John Koleszar's avatar
John Koleszar committed
69 70 71 72
  // 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;
73
      Total += vp9_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride,
74
                            &sse);
John Koleszar's avatar
John Koleszar committed
75 76
    }

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

  return Total;
John Koleszar's avatar
John Koleszar committed
82 83 84
}

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

  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
104 105 106
}

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

John Koleszar's avatar
John Koleszar committed
110 111 112 113
  // 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
114

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

John Koleszar's avatar
John Koleszar committed
118
  return max_filter_level;
John Koleszar's avatar
John Koleszar committed
119 120
}

121 122
void vp9_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
Johann's avatar
Johann committed
123

John Koleszar's avatar
John Koleszar committed
124 125 126 127 128 129
  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);
  int filt_val;
  int best_filt_val = cm->filter_level;
John Koleszar's avatar
John Koleszar committed
130

John Koleszar's avatar
John Koleszar committed
131
  //  Make a copy of the unfiltered / processed recon buffer
132
  vp9_yv12_copy_partial_frame(cm->frame_to_show, &cpi->last_frame_uf, 3);
John Koleszar's avatar
John Koleszar committed
133

John Koleszar's avatar
John Koleszar committed
134 135 136 137
  if (cm->frame_type == KEY_FRAME)
    cm->sharpness_level = 0;
  else
    cm->sharpness_level = cpi->oxcf.Sharpness;
John Koleszar's avatar
John Koleszar committed
138

John Koleszar's avatar
John Koleszar committed
139
  if (cm->sharpness_level != cm->last_sharpness_level) {
140
    vp9_loop_filter_update_sharpness(&cm->lf_info, cm->sharpness_level);
John Koleszar's avatar
John Koleszar committed
141 142
    cm->last_sharpness_level = cm->sharpness_level;
  }
John Koleszar's avatar
John Koleszar committed
143

John Koleszar's avatar
John Koleszar committed
144 145 146 147 148
  // Start the search at the previous frame filter level unless it is now out of range.
  if (cm->filter_level < min_filter_level)
    cm->filter_level = min_filter_level;
  else if (cm->filter_level > max_filter_level)
    cm->filter_level = max_filter_level;
John Koleszar's avatar
John Koleszar committed
149

John Koleszar's avatar
John Koleszar committed
150 151
  filt_val = cm->filter_level;
  best_filt_val = filt_val;
John Koleszar's avatar
John Koleszar committed
152

John Koleszar's avatar
John Koleszar committed
153
  // Get the err using the previous frame's filter value.
154
  vp9_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val);
John Koleszar's avatar
John Koleszar committed
155

156
  best_err = calc_partial_ssl_err(sd, cm->frame_to_show, 3);
John Koleszar's avatar
John Koleszar committed
157

John Koleszar's avatar
John Koleszar committed
158
  //  Re-instate the unfiltered frame
159
  vp9_yv12_copy_partial_frame(&cpi->last_frame_uf, cm->frame_to_show, 3);
John Koleszar's avatar
John Koleszar committed
160

John Koleszar's avatar
John Koleszar committed
161
  filt_val -= (1 + ((filt_val > 10) ? 1 : 0));
John Koleszar's avatar
John Koleszar committed
162

John Koleszar's avatar
John Koleszar committed
163 164 165
  // Search lower filter levels
  while (filt_val >= min_filter_level) {
    // Apply the loop filter
166
    vp9_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val);
John Koleszar's avatar
John Koleszar committed
167 168

    // Get the err for filtered frame
169
    filt_err = calc_partial_ssl_err(sd, cm->frame_to_show, 3);
John Koleszar's avatar
John Koleszar committed
170 171

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


John Koleszar's avatar
John Koleszar committed
175 176 177 178 179 180 181 182 183 184
    // Update the best case record or exit loop.
    if (filt_err < best_err) {
      best_err = filt_err;
      best_filt_val = filt_val;
    } else
      break;

    // Adjust filter level
    filt_val -= (1 + ((filt_val > 10) ? 1 : 0));
  }
John Koleszar's avatar
John Koleszar committed
185

John Koleszar's avatar
John Koleszar committed
186 187
  // Search up (note that we have already done filt_val = cm->filter_level)
  filt_val = cm->filter_level + (1 + ((filt_val > 10) ? 1 : 0));
John Koleszar's avatar
John Koleszar committed
188

John Koleszar's avatar
John Koleszar committed
189 190 191
  if (best_filt_val == cm->filter_level) {
    // Resist raising filter level for very small gains
    best_err -= (best_err >> 10);
John Koleszar's avatar
John Koleszar committed
192

John Koleszar's avatar
John Koleszar committed
193 194
    while (filt_val < max_filter_level) {
      // Apply the loop filter
195
      vp9_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val);
John Koleszar's avatar
John Koleszar committed
196

John Koleszar's avatar
John Koleszar committed
197
      // Get the err for filtered frame
198
      filt_err = calc_partial_ssl_err(sd, cm->frame_to_show, 3);
John Koleszar's avatar
John Koleszar committed
199

John Koleszar's avatar
John Koleszar committed
200
      //  Re-instate the unfiltered frame
201
      vp9_yv12_copy_partial_frame(&cpi->last_frame_uf,
202
                                      cm->frame_to_show, 3);
John Koleszar's avatar
John Koleszar committed
203

John Koleszar's avatar
John Koleszar committed
204 205 206 207 208 209 210 211 212 213 214
      // Update the best case record or exit loop.
      if (filt_err < best_err) {
        // Do not raise filter level if improvement is < 1 part in 4096
        best_err = filt_err - (filt_err >> 10);

        best_filt_val = filt_val;
      } else
        break;

      // Adjust filter level
      filt_val += (1 + ((filt_val > 10) ? 1 : 0));
John Koleszar's avatar
John Koleszar committed
215
    }
John Koleszar's avatar
John Koleszar committed
216
  }
John Koleszar's avatar
John Koleszar committed
217

John Koleszar's avatar
John Koleszar committed
218
  cm->filter_level = best_filt_val;
John Koleszar's avatar
John Koleszar committed
219

John Koleszar's avatar
John Koleszar committed
220 221
  if (cm->filter_level < min_filter_level)
    cm->filter_level = min_filter_level;
John Koleszar's avatar
John Koleszar committed
222

John Koleszar's avatar
John Koleszar committed
223 224
  if (cm->filter_level > max_filter_level)
    cm->filter_level = max_filter_level;
John Koleszar's avatar
John Koleszar committed
225 226 227
}

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

231 232
void vp9_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi) {
  VP9_COMMON *cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
233

John Koleszar's avatar
John Koleszar committed
234 235 236 237
  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
238

John Koleszar's avatar
John Koleszar committed
239 240 241 242 243 244
  int filter_step;
  int filt_high = 0;
  int filt_mid = cm->filter_level;      // Start search at previous frame filter level
  int filt_low = 0;
  int filt_best;
  int filt_direction = 0;
John Koleszar's avatar
John Koleszar committed
245

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

John Koleszar's avatar
John Koleszar committed
248
  //  Make a copy of the unfiltered / processed recon buffer
Johann's avatar
Johann committed
249
  vp8_yv12_copy_frame(cm->frame_to_show, &cpi->last_frame_uf);
John Koleszar's avatar
John Koleszar committed
250

John Koleszar's avatar
John Koleszar committed
251 252 253 254
  if (cm->frame_type == KEY_FRAME)
    cm->sharpness_level = 0;
  else
    cm->sharpness_level = cpi->oxcf.Sharpness;
John Koleszar's avatar
John Koleszar committed
255

John Koleszar's avatar
John Koleszar committed
256 257
  // Start the search at the previous frame filter level unless it is now out of range.
  filt_mid = cm->filter_level;
John Koleszar's avatar
John Koleszar committed
258

John Koleszar's avatar
John Koleszar committed
259 260 261 262
  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
263

John Koleszar's avatar
John Koleszar committed
264 265
  // Define the initial step size
  filter_step = (filt_mid < 16) ? 4 : filt_mid / 4;
John Koleszar's avatar
John Koleszar committed
266

John Koleszar's avatar
John Koleszar committed
267
  // Get baseline error score
268
  vp9_set_alt_lf_level(cpi, filt_mid);
269
  vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_mid, 1);
John Koleszar's avatar
John Koleszar committed
270

271
  best_err = vp9_calc_ss_err(sd, cm->frame_to_show);
John Koleszar's avatar
John Koleszar committed
272
  filt_best = filt_mid;
John Koleszar's avatar
John Koleszar committed
273

John Koleszar's avatar
John Koleszar committed
274
  //  Re-instate the unfiltered frame
Johann's avatar
Johann committed
275
  vp8_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
John Koleszar's avatar
John Koleszar committed
276

John Koleszar's avatar
John Koleszar committed
277 278
  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
279

John Koleszar's avatar
John Koleszar committed
280 281 282
    // 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
283

John Koleszar's avatar
John Koleszar committed
284
    // yx, bias less for large block size
285
    if (cpi->common.txfm_mode != ONLY_4X4)
John Koleszar's avatar
John Koleszar committed
286
      Bias >>= 1;
287

John Koleszar's avatar
John Koleszar committed
288 289
    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
290

John Koleszar's avatar
John Koleszar committed
291 292
    if ((filt_direction <= 0) && (filt_low != filt_mid)) {
      // Get Low filter error score
293
      vp9_set_alt_lf_level(cpi, filt_low);
294
      vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_low, 1);
John Koleszar's avatar
John Koleszar committed
295

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

John Koleszar's avatar
John Koleszar committed
298
      //  Re-instate the unfiltered frame
Johann's avatar
Johann committed
299
      vp8_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
John Koleszar's avatar
John Koleszar committed
300

John Koleszar's avatar
John Koleszar committed
301 302 303 304 305
      // 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
306

John Koleszar's avatar
John Koleszar committed
307 308 309
        filt_best = filt_low;
      }
    }
John Koleszar's avatar
John Koleszar committed
310

John Koleszar's avatar
John Koleszar committed
311 312
    // Now look at filt_high
    if ((filt_direction >= 0) && (filt_high != filt_mid)) {
313
      vp9_set_alt_lf_level(cpi, filt_high);
314
      vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_high, 1);
John Koleszar's avatar
John Koleszar committed
315

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

John Koleszar's avatar
John Koleszar committed
318
      //  Re-instate the unfiltered frame
Johann's avatar
Johann committed
319
      vp8_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
John Koleszar's avatar
John Koleszar committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

      // 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
335
    }
John Koleszar's avatar
John Koleszar committed
336
  }
John Koleszar's avatar
John Koleszar committed
337

John Koleszar's avatar
John Koleszar committed
338
  cm->filter_level = filt_best;
John Koleszar's avatar
John Koleszar committed
339
}