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

23
void vp9_yv12_copy_partial_frame_c(YV12_BUFFER_CONFIG *src_ybc,
24
25
26
27
28
                                   YV12_BUFFER_CONFIG *dst_ybc, int fraction) {
  const int height = src_ybc->y_height;
  const int stride = src_ybc->y_stride;
  const int offset = stride * ((height >> 5) * 16 - 8);
  const int lines_to_copy = MAX(height >> (fraction + 4), 1) << 4;
John Koleszar's avatar
John Koleszar committed
29

30
  assert(src_ybc->y_stride == dst_ybc->y_stride);
31
32
  vpx_memcpy(dst_ybc->y_buffer + offset, src_ybc->y_buffer + offset,
             stride * (lines_to_copy + 16));
John Koleszar's avatar
John Koleszar committed
33
}
34

35
36
static int calc_partial_ssl_err(YV12_BUFFER_CONFIG *source,
                                YV12_BUFFER_CONFIG *dest, int Fraction) {
John Koleszar's avatar
John Koleszar committed
37
38
39
  int i, j;
  int Total = 0;
  int srcoffset, dstoffset;
40
41
  uint8_t *src = source->y_buffer;
  uint8_t *dst = dest->y_buffer;
John Koleszar's avatar
John Koleszar committed
42

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

John Koleszar's avatar
John Koleszar committed
45
46
  if (linestocopy < 1)
    linestocopy = 1;
John Koleszar's avatar
John Koleszar committed
47

John Koleszar's avatar
John Koleszar committed
48
  linestocopy <<= 4;
John Koleszar's avatar
John Koleszar committed
49
50


John Koleszar's avatar
John Koleszar committed
51
52
  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
53

John Koleszar's avatar
John Koleszar committed
54
55
  src += srcoffset;
  dst += dstoffset;
John Koleszar's avatar
John Koleszar committed
56

Jim Bankoski's avatar
Jim Bankoski committed
57
58
  // Loop through the raw Y plane and reconstruction data summing the square
  // differences.
John Koleszar's avatar
John Koleszar committed
59
60
61
  for (i = 0; i < linestocopy; i += 16) {
    for (j = 0; j < source->y_width; j += 16) {
      unsigned int sse;
62
      Total += vp9_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride,
63
                            &sse);
John Koleszar's avatar
John Koleszar committed
64
65
    }

John Koleszar's avatar
John Koleszar committed
66
67
68
69
70
    src += 16 * source->y_stride;
    dst += 16 * dest->y_stride;
  }

  return Total;
John Koleszar's avatar
John Koleszar committed
71
72
73
}

// Enforce a minimum filter level based upon baseline Q
74
static int get_min_filter_level(VP9_COMP *cpi, int base_qindex) {
John Koleszar's avatar
John Koleszar committed
75
76
77
78
  int min_filter_level;
  min_filter_level = 0;

  return min_filter_level;
John Koleszar's avatar
John Koleszar committed
79
80
81
}

// Enforce a maximum filter level based upon baseline Q
82
static int get_max_filter_level(VP9_COMP *cpi, int base_qindex) {
Jim Bankoski's avatar
Jim Bankoski committed
83
  int max_filter_level = MAX_LOOP_FILTER;
John Koleszar's avatar
John Koleszar committed
84
  (void)base_qindex;
John Koleszar's avatar
John Koleszar committed
85

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

John Koleszar's avatar
John Koleszar committed
89
  return max_filter_level;
John Koleszar's avatar
John Koleszar committed
90
91
92
93
}


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

97
void vp9_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi, int partial) {
98
99
  VP9_COMMON *const cm = &cpi->common;
  struct loopfilter *const lf = &cm->lf;
John Koleszar's avatar
John Koleszar committed
100

John Koleszar's avatar
John Koleszar committed
101
102
  int best_err = 0;
  int filt_err = 0;
103
104
  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);
John Koleszar's avatar
John Koleszar committed
105

John Koleszar's avatar
John Koleszar committed
106
107
  int filter_step;
  int filt_high = 0;
108
109
  // Start search at previous frame filter level
  int filt_mid = lf->filter_level;
John Koleszar's avatar
John Koleszar committed
110
111
112
  int filt_low = 0;
  int filt_best;
  int filt_direction = 0;
John Koleszar's avatar
John Koleszar committed
113

Jim Bankoski's avatar
Jim Bankoski committed
114
  int Bias = 0;  // Bias against raising loop filter in favor of lowering it.
John Koleszar's avatar
John Koleszar committed
115

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

119
  lf->sharpness_level = cm->frame_type == KEY_FRAME ? 0
120
                                                    : cpi->oxcf.sharpness;
John Koleszar's avatar
John Koleszar committed
121

Jim Bankoski's avatar
Jim Bankoski committed
122
123
  // Start the search at the previous frame filter level unless it is now out of
  // range.
124
  filt_mid = clamp(lf->filter_level, min_filter_level, max_filter_level);
John Koleszar's avatar
John Koleszar committed
125

John Koleszar's avatar
John Koleszar committed
126
  // Define the initial step size
127
  filter_step = filt_mid < 16 ? 4 : filt_mid / 4;
John Koleszar's avatar
John Koleszar committed
128

John Koleszar's avatar
John Koleszar committed
129
  // Get baseline error score
130
  vp9_set_alt_lf_level(cpi, filt_mid);
131
  vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_mid, 1, partial);
John Koleszar's avatar
John Koleszar committed
132

133
  best_err = vp9_calc_ss_err(sd, cm->frame_to_show);
John Koleszar's avatar
John Koleszar committed
134
  filt_best = filt_mid;
John Koleszar's avatar
John Koleszar committed
135

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

John Koleszar's avatar
John Koleszar committed
139
  while (filter_step > 0) {
Jim Bankoski's avatar
Jim Bankoski committed
140
    Bias = (best_err >> (15 - (filt_mid / 8))) * filter_step;
John Koleszar's avatar
John Koleszar committed
141

John Koleszar's avatar
John Koleszar committed
142
143
    if (cpi->twopass.section_intra_rating < 20)
      Bias = Bias * cpi->twopass.section_intra_rating / 20;
John Koleszar's avatar
John Koleszar committed
144

John Koleszar's avatar
John Koleszar committed
145
    // yx, bias less for large block size
146
    if (cpi->common.tx_mode != ONLY_4X4)
John Koleszar's avatar
John Koleszar committed
147
      Bias >>= 1;
148

Jim Bankoski's avatar
Jim Bankoski committed
149
150
151
152
153
154
    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
155

John Koleszar's avatar
John Koleszar committed
156
157
    if ((filt_direction <= 0) && (filt_low != filt_mid)) {
      // Get Low filter error score
158
      vp9_set_alt_lf_level(cpi, filt_low);
159
      vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_low, 1, partial);
John Koleszar's avatar
John Koleszar committed
160

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

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

Jim Bankoski's avatar
Jim Bankoski committed
166
167
      // If value is close to the best so far then bias towards a lower loop
      // filter value.
John Koleszar's avatar
John Koleszar committed
168
169
170
171
      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
172

John Koleszar's avatar
John Koleszar committed
173
174
175
        filt_best = filt_low;
      }
    }
John Koleszar's avatar
John Koleszar committed
176

John Koleszar's avatar
John Koleszar committed
177
178
    // Now look at filt_high
    if ((filt_direction >= 0) && (filt_high != filt_mid)) {
179
      vp9_set_alt_lf_level(cpi, filt_high);
180
      vp9_loop_filter_frame(cm, &cpi->mb.e_mbd, filt_high, 1, partial);
John Koleszar's avatar
John Koleszar committed
181

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

John Koleszar's avatar
John Koleszar committed
184
      //  Re-instate the unfiltered frame
185
      vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
John Koleszar's avatar
John Koleszar committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

      // 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
201
    }
John Koleszar's avatar
John Koleszar committed
202
  }
John Koleszar's avatar
John Koleszar committed
203

204
  lf->filter_level = filt_best;
John Koleszar's avatar
John Koleszar committed
205
}