1/*
2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  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.
9 */
10
11#include <assert.h>
12#include <limits.h>
13
14#include "./vpx_scale_rtcd.h"
15
16#include "vpx_mem/vpx_mem.h"
17
18#include "vp9/common/vp9_loopfilter.h"
19#include "vp9/common/vp9_onyxc_int.h"
20#include "vp9/common/vp9_quant_common.h"
21
22#include "vp9/encoder/vp9_onyx_int.h"
23#include "vp9/encoder/vp9_picklpf.h"
24#include "vp9/encoder/vp9_quantize.h"
25
26static int get_max_filter_level(VP9_COMP *cpi) {
27  return cpi->twopass.section_intra_rating > 8 ? MAX_LOOP_FILTER * 3 / 4
28                                               : MAX_LOOP_FILTER;
29}
30
31
32static int try_filter_frame(const YV12_BUFFER_CONFIG *sd, VP9_COMP *const cpi,
33                            int filt_level, int partial_frame) {
34  VP9_COMMON *const cm = &cpi->common;
35  int filt_err;
36
37  vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_level, 1, partial_frame);
38  filt_err = vp9_calc_ss_err(sd, cm->frame_to_show);
39
40  // Re-instate the unfiltered frame
41  vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
42
43  return filt_err;
44}
45
46static void search_filter_level(const YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi,
47                                int partial_frame) {
48  VP9_COMMON *const cm = &cpi->common;
49  struct loopfilter *const lf = &cm->lf;
50  const int min_filter_level = 0;
51  const int max_filter_level = get_max_filter_level(cpi);
52  int best_err;
53  int filt_best;
54  int filt_direction = 0;
55  // Start the search at the previous frame filter level unless it is now out of
56  // range.
57  int filt_mid = clamp(lf->filter_level, min_filter_level, max_filter_level);
58  int filter_step = filt_mid < 16 ? 4 : filt_mid / 4;
59  // Sum squared error at each filter level
60  int ss_err[MAX_LOOP_FILTER + 1];
61
62  // Set each entry to -1
63  vpx_memset(ss_err, 0xFF, sizeof(ss_err));
64
65  //  Make a copy of the unfiltered / processed recon buffer
66  vpx_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf);
67
68  best_err = try_filter_frame(sd, cpi, filt_mid, partial_frame);
69  filt_best = filt_mid;
70  ss_err[filt_mid] = best_err;
71
72  while (filter_step > 0) {
73    const int filt_high = MIN(filt_mid + filter_step, max_filter_level);
74    const int filt_low = MAX(filt_mid - filter_step, min_filter_level);
75    int filt_err;
76
77    // Bias against raising loop filter in favor of lowering it.
78    int bias = (best_err >> (15 - (filt_mid / 8))) * filter_step;
79
80    if (cpi->twopass.section_intra_rating < 20)
81      bias = bias * cpi->twopass.section_intra_rating / 20;
82
83    // yx, bias less for large block size
84    if (cm->tx_mode != ONLY_4X4)
85      bias >>= 1;
86
87    if (filt_direction <= 0 && filt_low != filt_mid) {
88      // Get Low filter error score
89      if (ss_err[filt_low] < 0) {
90        filt_err = try_filter_frame(sd, cpi, filt_low, partial_frame);
91        ss_err[filt_low] = filt_err;
92      } else {
93        filt_err = ss_err[filt_low];
94      }
95      // If value is close to the best so far then bias towards a lower loop
96      // filter value.
97      if ((filt_err - bias) < best_err) {
98        // Was it actually better than the previous best?
99        if (filt_err < best_err)
100          best_err = filt_err;
101
102        filt_best = filt_low;
103      }
104    }
105
106    // Now look at filt_high
107    if (filt_direction >= 0 && filt_high != filt_mid) {
108      if (ss_err[filt_high] < 0) {
109        filt_err = try_filter_frame(sd, cpi, filt_high, partial_frame);
110        ss_err[filt_high] = filt_err;
111      } else {
112        filt_err = ss_err[filt_high];
113      }
114      // Was it better than the previous best?
115      if (filt_err < (best_err - bias)) {
116        best_err = filt_err;
117        filt_best = filt_high;
118      }
119    }
120
121    // Half the step distance if the best filter value was the same as last time
122    if (filt_best == filt_mid) {
123      filter_step /= 2;
124      filt_direction = 0;
125    } else {
126      filt_direction = (filt_best < filt_mid) ? -1 : 1;
127      filt_mid = filt_best;
128    }
129  }
130
131  lf->filter_level = filt_best;
132}
133
134void vp9_pick_filter_level(const YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi,
135                           LPF_PICK_METHOD method) {
136  VP9_COMMON *const cm = &cpi->common;
137  struct loopfilter *const lf = &cm->lf;
138
139  lf->sharpness_level = cm->frame_type == KEY_FRAME ? 0
140                                                    : cpi->oxcf.sharpness;
141
142  if (method == LPF_PICK_FROM_Q) {
143    const int min_filter_level = 0;
144    const int max_filter_level = get_max_filter_level(cpi);
145    const int q = vp9_ac_quant(cm->base_qindex, 0);
146    // These values were determined by linear fitting the result of the
147    // searched level, filt_guess = q * 0.316206 + 3.87252
148    int filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 1015158, 18);
149    if (cm->frame_type == KEY_FRAME)
150      filt_guess -= 4;
151    lf->filter_level = clamp(filt_guess, min_filter_level, max_filter_level);
152  } else {
153    search_filter_level(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE);
154  }
155}
156