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