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