vp9_picklpf.c 5.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"
17
#include "vp9/common/vp9_quant_common.h"
John Koleszar's avatar
John Koleszar committed
18
#include "vpx_mem/vpx_mem.h"
Johann's avatar
Johann committed
19
#include "vpx_scale/vpx_scale.h"
20 21
#include "vp9/common/vp9_alloccommon.h"
#include "vp9/common/vp9_loopfilter.h"
22
#include "./vpx_scale_rtcd.h"
John Koleszar's avatar
John Koleszar committed
23

24
static int get_min_filter_level(VP9_COMP *cpi, int base_qindex) {
25
  return 0;
John Koleszar's avatar
John Koleszar committed
26 27
}

28
static int get_max_filter_level(VP9_COMP *cpi, int base_qindex) {
29 30
  return cpi->twopass.section_intra_rating > 8 ? MAX_LOOP_FILTER * 3 / 4
                                               : MAX_LOOP_FILTER;
John Koleszar's avatar
John Koleszar committed
31 32 33
}

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

37 38
static void search_filter_level(YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi,
                                int partial) {
39
  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
40 41 42 43
  VP9_COMMON *const cm = &cpi->common;
  struct loopfilter *const lf = &cm->lf;
  const int min_filter_level = get_min_filter_level(cpi, cm->base_qindex);
  const int max_filter_level = get_max_filter_level(cpi, cm->base_qindex);
44 45
  int best_err = 0;
  int filt_err = 0;
John Koleszar's avatar
John Koleszar committed
46 47
  int filt_best;
  int filt_direction = 0;
48 49 50 51
  // Start the search at the previous frame filter level unless it is now out of
  // range.
  int filt_mid = clamp(lf->filter_level, min_filter_level, max_filter_level);
  int filter_step = filt_mid < 16 ? 4 : filt_mid / 4;
John Koleszar's avatar
John Koleszar committed
52

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

John Koleszar's avatar
John Koleszar committed
56
  // Get baseline error score
57
  vp9_set_alt_lf_level(cpi, filt_mid);
58
  vp9_loop_filter_frame(cm, xd, filt_mid, 1, partial);
John Koleszar's avatar
John Koleszar committed
59

60
  best_err = vp9_calc_ss_err(sd, cm->frame_to_show);
John Koleszar's avatar
John Koleszar committed
61
  filt_best = filt_mid;
John Koleszar's avatar
John Koleszar committed
62

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

John Koleszar's avatar
John Koleszar committed
66
  while (filter_step > 0) {
67 68 69 70 71
    const int filt_high = MIN(filt_mid + filter_step, max_filter_level);
    const int filt_low = MAX(filt_mid - filter_step, min_filter_level);

    // Bias against raising loop filter in favor of lowering it.
    int bias = (best_err >> (15 - (filt_mid / 8))) * filter_step;
John Koleszar's avatar
John Koleszar committed
72

John Koleszar's avatar
John Koleszar committed
73
    if (cpi->twopass.section_intra_rating < 20)
74
      bias = bias * cpi->twopass.section_intra_rating / 20;
John Koleszar's avatar
John Koleszar committed
75

John Koleszar's avatar
John Koleszar committed
76
    // yx, bias less for large block size
77 78
    if (cm->tx_mode != ONLY_4X4)
      bias >>= 1;
John Koleszar's avatar
John Koleszar committed
79

80
    if (filt_direction <= 0 && filt_low != filt_mid) {
John Koleszar's avatar
John Koleszar committed
81
      // Get Low filter error score
82
      vp9_set_alt_lf_level(cpi, filt_low);
83
      vp9_loop_filter_frame(cm, xd, filt_low, 1, partial);
John Koleszar's avatar
John Koleszar committed
84

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

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

Jim Bankoski's avatar
Jim Bankoski committed
90 91
      // If value is close to the best so far then bias towards a lower loop
      // filter value.
92
      if ((filt_err - bias) < best_err) {
John Koleszar's avatar
John Koleszar committed
93 94 95
        // Was it actually better than the previous best?
        if (filt_err < best_err)
          best_err = filt_err;
John Koleszar's avatar
John Koleszar committed
96

John Koleszar's avatar
John Koleszar committed
97 98 99
        filt_best = filt_low;
      }
    }
John Koleszar's avatar
John Koleszar committed
100

John Koleszar's avatar
John Koleszar committed
101
    // Now look at filt_high
102
    if (filt_direction >= 0 && filt_high != filt_mid) {
103
      vp9_set_alt_lf_level(cpi, filt_high);
104
      vp9_loop_filter_frame(cm, xd, filt_high, 1, partial);
John Koleszar's avatar
John Koleszar committed
105

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

John Koleszar's avatar
John Koleszar committed
108
      //  Re-instate the unfiltered frame
109
      vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
John Koleszar's avatar
John Koleszar committed
110 111

      // Was it better than the previous best?
112
      if (filt_err < (best_err - bias)) {
John Koleszar's avatar
John Koleszar committed
113 114 115 116 117 118 119 120 121 122 123 124
        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
125
    }
John Koleszar's avatar
John Koleszar committed
126
  }
John Koleszar's avatar
John Koleszar committed
127

128
  lf->filter_level = filt_best;
John Koleszar's avatar
John Koleszar committed
129
}
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

void vp9_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi, int method) {
  VP9_COMMON *const cm = &cpi->common;
  struct loopfilter *const lf = &cm->lf;

  lf->sharpness_level = cm->frame_type == KEY_FRAME ? 0
                                                    : cpi->oxcf.sharpness;

  if (method == 2) {
    const int min_filter_level = get_min_filter_level(cpi, cm->base_qindex);
    const int max_filter_level = get_max_filter_level(cpi, cm->base_qindex);
    const int q = vp9_ac_quant(cm->base_qindex, 0);
    // These values were determined by linear fitting the result of the
    // searched level
    // filt_guess = q * 0.316206 + 3.87252
    int filt_guess = (q * 20723 + 1015158 + (1 << 17)) >> 18;
    if (cm->frame_type == KEY_FRAME)
      filt_guess -= 4;
    lf->filter_level = clamp(filt_guess, min_filter_level, max_filter_level);
  } else {
    search_filter_level(sd, cpi, method == 1);
  }
}