|
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 #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" |
|
17 #include "vpx_mem/vpx_mem.h" |
|
18 #include "vpx_scale/vpx_scale.h" |
|
19 #include "vp9/common/vp9_alloccommon.h" |
|
20 #include "vp9/common/vp9_loopfilter.h" |
|
21 #include "./vpx_scale_rtcd.h" |
|
22 |
|
23 void vp9_yv12_copy_partial_frame_c(YV12_BUFFER_CONFIG *src_ybc, |
|
24 YV12_BUFFER_CONFIG *dst_ybc, int fraction) { |
|
25 const int height = src_ybc->y_height; |
|
26 const int stride = src_ybc->y_stride; |
|
27 const int offset = stride * ((height >> 5) * 16 - 8); |
|
28 const int lines_to_copy = MAX(height >> (fraction + 4), 1) << 4; |
|
29 |
|
30 assert(src_ybc->y_stride == dst_ybc->y_stride); |
|
31 vpx_memcpy(dst_ybc->y_buffer + offset, src_ybc->y_buffer + offset, |
|
32 stride * (lines_to_copy + 16)); |
|
33 } |
|
34 |
|
35 static int calc_partial_ssl_err(YV12_BUFFER_CONFIG *source, |
|
36 YV12_BUFFER_CONFIG *dest, int Fraction) { |
|
37 int i, j; |
|
38 int Total = 0; |
|
39 int srcoffset, dstoffset; |
|
40 uint8_t *src = source->y_buffer; |
|
41 uint8_t *dst = dest->y_buffer; |
|
42 |
|
43 int linestocopy = (source->y_height >> (Fraction + 4)); |
|
44 |
|
45 if (linestocopy < 1) |
|
46 linestocopy = 1; |
|
47 |
|
48 linestocopy <<= 4; |
|
49 |
|
50 |
|
51 srcoffset = source->y_stride * (dest->y_height >> 5) * 16; |
|
52 dstoffset = dest->y_stride * (dest->y_height >> 5) * 16; |
|
53 |
|
54 src += srcoffset; |
|
55 dst += dstoffset; |
|
56 |
|
57 // Loop through the raw Y plane and reconstruction data summing the square |
|
58 // differences. |
|
59 for (i = 0; i < linestocopy; i += 16) { |
|
60 for (j = 0; j < source->y_width; j += 16) { |
|
61 unsigned int sse; |
|
62 Total += vp9_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride, |
|
63 &sse); |
|
64 } |
|
65 |
|
66 src += 16 * source->y_stride; |
|
67 dst += 16 * dest->y_stride; |
|
68 } |
|
69 |
|
70 return Total; |
|
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) { |
|
75 int min_filter_level; |
|
76 min_filter_level = 0; |
|
77 |
|
78 return min_filter_level; |
|
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) { |
|
83 int max_filter_level = MAX_LOOP_FILTER; |
|
84 (void)base_qindex; |
|
85 |
|
86 if (cpi->twopass.section_intra_rating > 8) |
|
87 max_filter_level = MAX_LOOP_FILTER * 3 / 4; |
|
88 |
|
89 return max_filter_level; |
|
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 } |
|
96 |
|
97 void vp9_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi, int partial) { |
|
98 VP9_COMMON *const cm = &cpi->common; |
|
99 struct loopfilter *const lf = &cm->lf; |
|
100 |
|
101 int best_err = 0; |
|
102 int filt_err = 0; |
|
103 const int min_filter_level = get_min_filter_level(cpi, cm->base_qindex); |
|
104 const int max_filter_level = get_max_filter_level(cpi, cm->base_qindex); |
|
105 |
|
106 int filter_step; |
|
107 int filt_high = 0; |
|
108 // Start search at previous frame filter level |
|
109 int filt_mid = lf->filter_level; |
|
110 int filt_low = 0; |
|
111 int filt_best; |
|
112 int filt_direction = 0; |
|
113 |
|
114 int Bias = 0; // Bias against raising loop filter in favor of lowering it. |
|
115 |
|
116 // Make a copy of the unfiltered / processed recon buffer |
|
117 vpx_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf); |
|
118 |
|
119 lf->sharpness_level = cm->frame_type == KEY_FRAME ? 0 |
|
120 : cpi->oxcf.Sharpness; |
|
121 |
|
122 // Start the search at the previous frame filter level unless it is now out of |
|
123 // range. |
|
124 filt_mid = clamp(lf->filter_level, min_filter_level, max_filter_level); |
|
125 |
|
126 // Define the initial step size |
|
127 filter_step = filt_mid < 16 ? 4 : filt_mid / 4; |
|
128 |
|
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); |
|
132 |
|
133 best_err = vp9_calc_ss_err(sd, cm->frame_to_show); |
|
134 filt_best = filt_mid; |
|
135 |
|
136 // Re-instate the unfiltered frame |
|
137 vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show); |
|
138 |
|
139 while (filter_step > 0) { |
|
140 Bias = (best_err >> (15 - (filt_mid / 8))) * filter_step; |
|
141 |
|
142 if (cpi->twopass.section_intra_rating < 20) |
|
143 Bias = Bias * cpi->twopass.section_intra_rating / 20; |
|
144 |
|
145 // yx, bias less for large block size |
|
146 if (cpi->common.tx_mode != ONLY_4X4) |
|
147 Bias >>= 1; |
|
148 |
|
149 filt_high = ((filt_mid + filter_step) > max_filter_level) |
|
150 ? max_filter_level |
|
151 : (filt_mid + filter_step); |
|
152 filt_low = ((filt_mid - filter_step) < min_filter_level) |
|
153 ? min_filter_level |
|
154 : (filt_mid - filter_step); |
|
155 |
|
156 if ((filt_direction <= 0) && (filt_low != filt_mid)) { |
|
157 // 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); |
|
160 |
|
161 filt_err = vp9_calc_ss_err(sd, cm->frame_to_show); |
|
162 |
|
163 // Re-instate the unfiltered frame |
|
164 vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show); |
|
165 |
|
166 // If value is close to the best so far then bias towards a lower loop |
|
167 // filter value. |
|
168 if ((filt_err - Bias) < best_err) { |
|
169 // Was it actually better than the previous best? |
|
170 if (filt_err < best_err) |
|
171 best_err = filt_err; |
|
172 |
|
173 filt_best = filt_low; |
|
174 } |
|
175 } |
|
176 |
|
177 // Now look at filt_high |
|
178 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); |
|
181 |
|
182 filt_err = vp9_calc_ss_err(sd, cm->frame_to_show); |
|
183 |
|
184 // Re-instate the unfiltered frame |
|
185 vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show); |
|
186 |
|
187 // Was it better than the previous best? |
|
188 if (filt_err < (best_err - Bias)) { |
|
189 best_err = filt_err; |
|
190 filt_best = filt_high; |
|
191 } |
|
192 } |
|
193 |
|
194 // Half the step distance if the best filter value was the same as last time |
|
195 if (filt_best == filt_mid) { |
|
196 filter_step = filter_step / 2; |
|
197 filt_direction = 0; |
|
198 } else { |
|
199 filt_direction = (filt_best < filt_mid) ? -1 : 1; |
|
200 filt_mid = filt_best; |
|
201 } |
|
202 } |
|
203 |
|
204 lf->filter_level = filt_best; |
|
205 } |