Coverage Report

Created: 2025-08-28 07:12

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