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#include "vpx_dsp/psnr.h"
16#include "vpx_mem/vpx_mem.h"
17#include "vpx_ports/mem.h"
18
19#include "vp9/common/vp9_loopfilter.h"
20#include "vp9/common/vp9_onyxc_int.h"
21#include "vp9/common/vp9_quant_common.h"
22
23#include "vp9/encoder/vp9_encoder.h"
24#include "vp9/encoder/vp9_picklpf.h"
25#include "vp9/encoder/vp9_quantize.h"
26
27static int get_max_filter_level(const VP9_COMP *cpi) {
28  if (cpi->oxcf.pass == 2) {
29    return cpi->twopass.section_intra_rating > 8 ? MAX_LOOP_FILTER * 3 / 4
30                                                 : MAX_LOOP_FILTER;
31  } else {
32    return MAX_LOOP_FILTER;
33  }
34}
35
36static int64_t try_filter_frame(const YV12_BUFFER_CONFIG *sd,
37                                VP9_COMP *const cpi, int filt_level,
38                                int partial_frame) {
39  VP9_COMMON *const cm = &cpi->common;
40  int64_t filt_err;
41
42  vp9_build_mask_frame(cm, filt_level, partial_frame);
43
44  if (cpi->num_workers > 1)
45    vp9_loop_filter_frame_mt(cm->frame_to_show, cm, cpi->td.mb.e_mbd.plane,
46                             filt_level, 1, partial_frame, cpi->workers,
47                             cpi->num_workers, &cpi->lf_row_sync);
48  else
49    vp9_loop_filter_frame(cm->frame_to_show, cm, &cpi->td.mb.e_mbd, filt_level,
50                          1, partial_frame);
51
52#if CONFIG_VP9_HIGHBITDEPTH
53  if (cm->use_highbitdepth) {
54    filt_err = vpx_highbd_get_y_sse(sd, cm->frame_to_show);
55  } else {
56    filt_err = vpx_get_y_sse(sd, cm->frame_to_show);
57  }
58#else
59  filt_err = vpx_get_y_sse(sd, cm->frame_to_show);
60#endif  // CONFIG_VP9_HIGHBITDEPTH
61
62  // Re-instate the unfiltered frame
63  vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
64
65  return filt_err;
66}
67
68static int search_filter_level(const YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi,
69                               int partial_frame) {
70  const VP9_COMMON *const cm = &cpi->common;
71  const struct loopfilter *const lf = &cm->lf;
72  const int min_filter_level = 0;
73  const int max_filter_level = get_max_filter_level(cpi);
74  int filt_direction = 0;
75  int64_t best_err;
76  int filt_best;
77
78  // Start the search at the previous frame filter level unless it is now out of
79  // range.
80  int filt_mid = clamp(lf->last_filt_level, min_filter_level, max_filter_level);
81  int filter_step = filt_mid < 16 ? 4 : filt_mid / 4;
82  // Sum squared error at each filter level
83  int64_t ss_err[MAX_LOOP_FILTER + 1];
84
85  // Set each entry to -1
86  memset(ss_err, 0xFF, sizeof(ss_err));
87
88  //  Make a copy of the unfiltered / processed recon buffer
89  vpx_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf);
90
91  best_err = try_filter_frame(sd, cpi, filt_mid, partial_frame);
92  filt_best = filt_mid;
93  ss_err[filt_mid] = best_err;
94
95  while (filter_step > 0) {
96    const int filt_high = VPXMIN(filt_mid + filter_step, max_filter_level);
97    const int filt_low = VPXMAX(filt_mid - filter_step, min_filter_level);
98
99    // Bias against raising loop filter in favor of lowering it.
100    int64_t bias = (best_err >> (15 - (filt_mid / 8))) * filter_step;
101
102    if ((cpi->oxcf.pass == 2) && (cpi->twopass.section_intra_rating < 20))
103      bias = (bias * cpi->twopass.section_intra_rating) / 20;
104
105    // yx, bias less for large block size
106    if (cm->tx_mode != ONLY_4X4) bias >>= 1;
107
108    if (filt_direction <= 0 && filt_low != filt_mid) {
109      // Get Low filter error score
110      if (ss_err[filt_low] < 0) {
111        ss_err[filt_low] = try_filter_frame(sd, cpi, filt_low, partial_frame);
112      }
113      // If value is close to the best so far then bias towards a lower loop
114      // filter value.
115      if ((ss_err[filt_low] - bias) < best_err) {
116        // Was it actually better than the previous best?
117        if (ss_err[filt_low] < best_err) best_err = ss_err[filt_low];
118
119        filt_best = filt_low;
120      }
121    }
122
123    // Now look at filt_high
124    if (filt_direction >= 0 && filt_high != filt_mid) {
125      if (ss_err[filt_high] < 0) {
126        ss_err[filt_high] = try_filter_frame(sd, cpi, filt_high, partial_frame);
127      }
128      // Was it better than the previous best?
129      if (ss_err[filt_high] < (best_err - bias)) {
130        best_err = ss_err[filt_high];
131        filt_best = filt_high;
132      }
133    }
134
135    // Half the step distance if the best filter value was the same as last time
136    if (filt_best == filt_mid) {
137      filter_step /= 2;
138      filt_direction = 0;
139    } else {
140      filt_direction = (filt_best < filt_mid) ? -1 : 1;
141      filt_mid = filt_best;
142    }
143  }
144
145  return filt_best;
146}
147
148void vp9_pick_filter_level(const YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi,
149                           LPF_PICK_METHOD method) {
150  VP9_COMMON *const cm = &cpi->common;
151  struct loopfilter *const lf = &cm->lf;
152
153  lf->sharpness_level = cm->frame_type == KEY_FRAME ? 0 : cpi->oxcf.sharpness;
154
155  if (method == LPF_PICK_MINIMAL_LPF && lf->filter_level) {
156    lf->filter_level = 0;
157  } else if (method >= LPF_PICK_FROM_Q) {
158    const int min_filter_level = 0;
159    const int max_filter_level = get_max_filter_level(cpi);
160    const int q = vp9_ac_quant(cm->base_qindex, 0, cm->bit_depth);
161// These values were determined by linear fitting the result of the
162// searched level, filt_guess = q * 0.316206 + 3.87252
163#if CONFIG_VP9_HIGHBITDEPTH
164    int filt_guess;
165    switch (cm->bit_depth) {
166      case VPX_BITS_8:
167        filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 1015158, 18);
168        break;
169      case VPX_BITS_10:
170        filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 4060632, 20);
171        break;
172      case VPX_BITS_12:
173        filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 16242526, 22);
174        break;
175      default:
176        assert(0 &&
177               "bit_depth should be VPX_BITS_8, VPX_BITS_10 "
178               "or VPX_BITS_12");
179        return;
180    }
181#else
182    int filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 1015158, 18);
183#endif  // CONFIG_VP9_HIGHBITDEPTH
184    if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
185        cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
186        cpi->oxcf.content != VP9E_CONTENT_SCREEN && cm->frame_type != KEY_FRAME)
187      filt_guess = 5 * filt_guess >> 3;
188
189    if (cm->frame_type == KEY_FRAME) filt_guess -= 4;
190    lf->filter_level = clamp(filt_guess, min_filter_level, max_filter_level);
191  } else {
192    lf->filter_level =
193        search_filter_level(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE);
194  }
195}
196