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 
13 #include "mem.h"
14 
15 #include "vp9_loopfilter.h"
16 #include "vp9_onyxc_int.h"
17 #include "vp9_quant_common.h"
18 
19 #include "vp9_encoder.h"
20 #include "vp9_picklpf.h"
21 #include "vp9_blockd.h"
22 #include "vpx_dsp_common.h"
23 
get_max_filter_level(const VP9_COMP * cpi)24 static int get_max_filter_level(const VP9_COMP *cpi) {
25     (void)cpi;
26 #if 1 // Hsan: filter_level derivation
27     return MAX_LOOP_FILTER;
28 #else
29   if (cpi->oxcf.pass == 2) {
30     return cpi->twopass.section_intra_rating > 8 ? MAX_LOOP_FILTER * 3 / 4
31                                                  : MAX_LOOP_FILTER;
32   } else {
33     return MAX_LOOP_FILTER;
34   }
35 #endif
36 }
37 
eb_vp9_pick_filter_level(VP9_COMP * cpi,LPF_PICK_METHOD method)38 void eb_vp9_pick_filter_level(
39 #if 0
40     const YV12_BUFFER_CONFIG *sd,
41 #endif
42     VP9_COMP *cpi,
43     LPF_PICK_METHOD method) {
44 
45   VP9_COMMON *const cm = &cpi->common;
46   struct loop_filter *const lf = &cm->lf;
47 
48   lf->sharpness_level = 0;
49 
50   if (method == LPF_PICK_MINIMAL_LPF && lf->filter_level) {
51     lf->filter_level = 0;
52   } else if (method >= LPF_PICK_FROM_Q) {
53     const int min_filter_level = 0;
54     const int max_filter_level = get_max_filter_level(cpi);
55     const int q = eb_vp9_ac_quant(cm->base_qindex, 0, cm->bit_depth);
56 // These values were determined by linear fitting the result of the
57 // searched level, filt_guess = q * 0.316206 + 3.87252
58 #if CONFIG_VP9_HIGHBITDEPTH
59     int filt_guess;
60     switch (cm->bit_depth) {
61       case VPX_BITS_8:
62         filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 1015158, 18);
63         break;
64       case VPX_BITS_10:
65         filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 4060632, 20);
66         break;
67       default:
68         assert(cm->bit_depth == VPX_BITS_12);
69         filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 16242526, 22);
70         break;
71     }
72 #else
73     int filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 1015158, 18);
74 #endif  // CONFIG_VP9_HIGHBITDEPTH
75 
76 #if 0 // Hsan: condition not satisfied for SVT-VP9
77     if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
78         cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
79         cpi->oxcf.content != VP9E_CONTENT_SCREEN && cm->frame_type != KEY_FRAME)
80       filt_guess = 5 * filt_guess >> 3;
81 #endif
82 
83     if (cm->frame_type == KEY_FRAME) filt_guess -= 4;
84 
85     lf->filter_level = clamp(filt_guess, min_filter_level, max_filter_level);
86 
87   } else {
88 #if 1 // Hsan: not supported
89       assert(0);
90 #else
91     lf->filter_level =
92         search_filter_level(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE);
93 #endif
94   }
95 
96 }
97