OLD | NEW |
(Empty) | |
| 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 |
| 12 #include "vp9/common/vp9_onyxc_int.h" |
| 13 #include "vp9/encoder/vp9_onyx_int.h" |
| 14 #include "vp9/encoder/vp9_picklpf.h" |
| 15 #include "vp9/encoder/vp9_quantize.h" |
| 16 #include "vpx_mem/vpx_mem.h" |
| 17 #include "vpx_scale/vpxscale.h" |
| 18 #include "vp9/common/vp9_alloccommon.h" |
| 19 #include "vp9/common/vp9_loopfilter.h" |
| 20 #include "./vpx_scale_rtcd.h" |
| 21 |
| 22 void vp9_yv12_copy_partial_frame_c(YV12_BUFFER_CONFIG *src_ybc, |
| 23 YV12_BUFFER_CONFIG *dst_ybc, int Fraction) { |
| 24 unsigned char *src_y, *dst_y; |
| 25 int yheight; |
| 26 int ystride; |
| 27 int border; |
| 28 int yoffset; |
| 29 int linestocopy; |
| 30 |
| 31 border = src_ybc->border; |
| 32 yheight = src_ybc->y_height; |
| 33 ystride = src_ybc->y_stride; |
| 34 |
| 35 linestocopy = (yheight >> (Fraction + 4)); |
| 36 |
| 37 if (linestocopy < 1) |
| 38 linestocopy = 1; |
| 39 |
| 40 linestocopy <<= 4; |
| 41 |
| 42 yoffset = ystride * ((yheight >> 5) * 16 - 8); |
| 43 src_y = src_ybc->y_buffer + yoffset; |
| 44 dst_y = dst_ybc->y_buffer + yoffset; |
| 45 |
| 46 vpx_memcpy(dst_y, src_y, ystride * (linestocopy + 16)); |
| 47 } |
| 48 |
| 49 static int calc_partial_ssl_err(YV12_BUFFER_CONFIG *source, |
| 50 YV12_BUFFER_CONFIG *dest, int Fraction) { |
| 51 int i, j; |
| 52 int Total = 0; |
| 53 int srcoffset, dstoffset; |
| 54 unsigned char *src = source->y_buffer; |
| 55 unsigned char *dst = dest->y_buffer; |
| 56 |
| 57 int linestocopy = (source->y_height >> (Fraction + 4)); |
| 58 |
| 59 if (linestocopy < 1) |
| 60 linestocopy = 1; |
| 61 |
| 62 linestocopy <<= 4; |
| 63 |
| 64 |
| 65 srcoffset = source->y_stride * (dest->y_height >> 5) * 16; |
| 66 dstoffset = dest->y_stride * (dest->y_height >> 5) * 16; |
| 67 |
| 68 src += srcoffset; |
| 69 dst += dstoffset; |
| 70 |
| 71 // Loop through the Y plane raw and reconstruction data summing (square differ
ences) |
| 72 for (i = 0; i < linestocopy; i += 16) { |
| 73 for (j = 0; j < source->y_width; j += 16) { |
| 74 unsigned int sse; |
| 75 Total += vp9_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride, |
| 76 &sse); |
| 77 } |
| 78 |
| 79 src += 16 * source->y_stride; |
| 80 dst += 16 * dest->y_stride; |
| 81 } |
| 82 |
| 83 return Total; |
| 84 } |
| 85 |
| 86 // Enforce a minimum filter level based upon baseline Q |
| 87 static int get_min_filter_level(VP9_COMP *cpi, int base_qindex) { |
| 88 int min_filter_level; |
| 89 /*int q = (int) vp9_convert_qindex_to_q(base_qindex); |
| 90 |
| 91 if (cpi->source_alt_ref_active && cpi->common.refresh_golden_frame && !cpi->co
mmon.refresh_alt_ref_frame) |
| 92 min_filter_level = 0; |
| 93 else |
| 94 { |
| 95 if (q <= 10) |
| 96 min_filter_level = 0; |
| 97 else if (q <= 64) |
| 98 min_filter_level = 1; |
| 99 else |
| 100 min_filter_level = (q >> 6); |
| 101 } |
| 102 */ |
| 103 min_filter_level = 0; |
| 104 |
| 105 return min_filter_level; |
| 106 } |
| 107 |
| 108 // Enforce a maximum filter level based upon baseline Q |
| 109 static int get_max_filter_level(VP9_COMP *cpi, int base_qindex) { |
| 110 // PGW August 2006: Highest filter values almost always a bad idea |
| 111 |
| 112 // jbb chg: 20100118 - not so any more with this overquant stuff allow high va
lues |
| 113 // with lots of intra coming in. |
| 114 int max_filter_level = MAX_LOOP_FILTER;// * 3 / 4; |
| 115 (void)base_qindex; |
| 116 |
| 117 if (cpi->twopass.section_intra_rating > 8) |
| 118 max_filter_level = MAX_LOOP_FILTER * 3 / 4; |
| 119 |
| 120 return max_filter_level; |
| 121 } |
| 122 |
| 123 void vp9_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi) { |
| 124 VP9_COMMON *cm = &cpi->common; |
| 125 |
| 126 int best_err = 0; |
| 127 int filt_err = 0; |
| 128 int min_filter_level = get_min_filter_level(cpi, cm->base_qindex); |
| 129 int max_filter_level = get_max_filter_level(cpi, cm->base_qindex); |
| 130 int filt_val; |
| 131 int best_filt_val = cm->filter_level; |
| 132 |
| 133 // Make a copy of the unfiltered / processed recon buffer |
| 134 vp9_yv12_copy_partial_frame(cm->frame_to_show, &cpi->last_frame_uf, 3); |
| 135 |
| 136 if (cm->frame_type == KEY_FRAME) |
| 137 cm->sharpness_level = 0; |
| 138 else |
| 139 cm->sharpness_level = cpi->oxcf.Sharpness; |
| 140 |
| 141 if (cm->sharpness_level != cm->last_sharpness_level) { |
| 142 vp9_loop_filter_update_sharpness(&cm->lf_info, cm->sharpness_level); |
| 143 cm->last_sharpness_level = cm->sharpness_level; |
| 144 } |
| 145 |
| 146 // Start the search at the previous frame filter level unless it is now out of
range. |
| 147 if (cm->filter_level < min_filter_level) |
| 148 cm->filter_level = min_filter_level; |
| 149 else if (cm->filter_level > max_filter_level) |
| 150 cm->filter_level = max_filter_level; |
| 151 |
| 152 filt_val = cm->filter_level; |
| 153 best_filt_val = filt_val; |
| 154 |
| 155 // Get the err using the previous frame's filter value. |
| 156 vp9_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val); |
| 157 |
| 158 best_err = calc_partial_ssl_err(sd, cm->frame_to_show, 3); |
| 159 |
| 160 // Re-instate the unfiltered frame |
| 161 vp9_yv12_copy_partial_frame(&cpi->last_frame_uf, cm->frame_to_show, 3); |
| 162 |
| 163 filt_val -= (1 + ((filt_val > 10) ? 1 : 0)); |
| 164 |
| 165 // Search lower filter levels |
| 166 while (filt_val >= min_filter_level) { |
| 167 // Apply the loop filter |
| 168 vp9_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val); |
| 169 |
| 170 // Get the err for filtered frame |
| 171 filt_err = calc_partial_ssl_err(sd, cm->frame_to_show, 3); |
| 172 |
| 173 // Re-instate the unfiltered frame |
| 174 vp9_yv12_copy_partial_frame(&cpi->last_frame_uf, cm->frame_to_show, 3); |
| 175 |
| 176 |
| 177 // Update the best case record or exit loop. |
| 178 if (filt_err < best_err) { |
| 179 best_err = filt_err; |
| 180 best_filt_val = filt_val; |
| 181 } else |
| 182 break; |
| 183 |
| 184 // Adjust filter level |
| 185 filt_val -= (1 + ((filt_val > 10) ? 1 : 0)); |
| 186 } |
| 187 |
| 188 // Search up (note that we have already done filt_val = cm->filter_level) |
| 189 filt_val = cm->filter_level + (1 + ((filt_val > 10) ? 1 : 0)); |
| 190 |
| 191 if (best_filt_val == cm->filter_level) { |
| 192 // Resist raising filter level for very small gains |
| 193 best_err -= (best_err >> 10); |
| 194 |
| 195 while (filt_val < max_filter_level) { |
| 196 // Apply the loop filter |
| 197 vp9_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val); |
| 198 |
| 199 // Get the err for filtered frame |
| 200 filt_err = calc_partial_ssl_err(sd, cm->frame_to_show, 3); |
| 201 |
| 202 // Re-instate the unfiltered frame |
| 203 vp9_yv12_copy_partial_frame(&cpi->last_frame_uf, |
| 204 cm->frame_to_show, 3); |
| 205 |
| 206 // Update the best case record or exit loop. |
| 207 if (filt_err < best_err) { |
| 208 // Do not raise filter level if improvement is < 1 part in 4096 |
| 209 best_err = filt_err - (filt_err >> 10); |
| 210 |
| 211 best_filt_val = filt_val; |
| 212 } else |
| 213 break; |
| 214 |
| 215 // Adjust filter level |
| 216 filt_val += (1 + ((filt_val > 10) ? 1 : 0)); |
| 217 } |
| 218 } |
| 219 |
| 220 cm->filter_level = best_filt_val; |
| 221 |
| 222 if (cm->filter_level < min_filter_level) |
| 223 cm->filter_level = min_filter_level; |
| 224 |
| 225 if (cm->filter_level > max_filter_level) |
| 226 cm->filter_level = max_filter_level; |
| 227 } |
| 228 |
| 229 // Stub function for now Alt LF not used |
| 230 void vp9_set_alt_lf_level(VP9_COMP *cpi, int filt_val) { |
| 231 } |
| 232 |
| 233 void vp9_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi) { |
| 234 VP9_COMMON *cm = &cpi->common; |
| 235 |
| 236 int best_err = 0; |
| 237 int filt_err = 0; |
| 238 int min_filter_level = get_min_filter_level(cpi, cm->base_qindex); |
| 239 int max_filter_level = get_max_filter_level(cpi, cm->base_qindex); |
| 240 |
| 241 int filter_step; |
| 242 int filt_high = 0; |
| 243 int filt_mid = cm->filter_level; // Start search at previous frame filter
level |
| 244 int filt_low = 0; |
| 245 int filt_best; |
| 246 int filt_direction = 0; |
| 247 |
| 248 int Bias = 0; // Bias against raising loop filter and in
favour of lowering it |
| 249 |
| 250 // Make a copy of the unfiltered / processed recon buffer |
| 251 vp8_yv12_copy_frame(cm->frame_to_show, &cpi->last_frame_uf); |
| 252 |
| 253 if (cm->frame_type == KEY_FRAME) |
| 254 cm->sharpness_level = 0; |
| 255 else |
| 256 cm->sharpness_level = cpi->oxcf.Sharpness; |
| 257 |
| 258 // Start the search at the previous frame filter level unless it is now out of
range. |
| 259 filt_mid = cm->filter_level; |
| 260 |
| 261 if (filt_mid < min_filter_level) |
| 262 filt_mid = min_filter_level; |
| 263 else if (filt_mid > max_filter_level) |
| 264 filt_mid = max_filter_level; |
| 265 |
| 266 // Define the initial step size |
| 267 filter_step = (filt_mid < 16) ? 4 : filt_mid / 4; |
| 268 |
| 269 // Get baseline error score |
| 270 vp9_set_alt_lf_level(cpi, filt_mid); |
| 271 vp9_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_mid); |
| 272 |
| 273 best_err = vp9_calc_ss_err(sd, cm->frame_to_show); |
| 274 filt_best = filt_mid; |
| 275 |
| 276 // Re-instate the unfiltered frame |
| 277 vp8_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show); |
| 278 |
| 279 while (filter_step > 0) { |
| 280 Bias = (best_err >> (15 - (filt_mid / 8))) * filter_step; // PGW change 12/1
2/06 for small images |
| 281 |
| 282 // jbb chg: 20100118 - in sections with lots of new material coming in don't
bias as much to a low filter value |
| 283 if (cpi->twopass.section_intra_rating < 20) |
| 284 Bias = Bias * cpi->twopass.section_intra_rating / 20; |
| 285 |
| 286 // yx, bias less for large block size |
| 287 if (cpi->common.txfm_mode != ONLY_4X4) |
| 288 Bias >>= 1; |
| 289 |
| 290 filt_high = ((filt_mid + filter_step) > max_filter_level) ? max_filter_level
: (filt_mid + filter_step); |
| 291 filt_low = ((filt_mid - filter_step) < min_filter_level) ? min_filter_level
: (filt_mid - filter_step); |
| 292 |
| 293 if ((filt_direction <= 0) && (filt_low != filt_mid)) { |
| 294 // Get Low filter error score |
| 295 vp9_set_alt_lf_level(cpi, filt_low); |
| 296 vp9_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_low); |
| 297 |
| 298 filt_err = vp9_calc_ss_err(sd, cm->frame_to_show); |
| 299 |
| 300 // Re-instate the unfiltered frame |
| 301 vp8_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show); |
| 302 |
| 303 // If value is close to the best so far then bias towards a lower loop fil
ter value. |
| 304 if ((filt_err - Bias) < best_err) { |
| 305 // Was it actually better than the previous best? |
| 306 if (filt_err < best_err) |
| 307 best_err = filt_err; |
| 308 |
| 309 filt_best = filt_low; |
| 310 } |
| 311 } |
| 312 |
| 313 // Now look at filt_high |
| 314 if ((filt_direction >= 0) && (filt_high != filt_mid)) { |
| 315 vp9_set_alt_lf_level(cpi, filt_high); |
| 316 vp9_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_high); |
| 317 |
| 318 filt_err = vp9_calc_ss_err(sd, cm->frame_to_show); |
| 319 |
| 320 // Re-instate the unfiltered frame |
| 321 vp8_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show); |
| 322 |
| 323 // Was it better than the previous best? |
| 324 if (filt_err < (best_err - Bias)) { |
| 325 best_err = filt_err; |
| 326 filt_best = filt_high; |
| 327 } |
| 328 } |
| 329 |
| 330 // Half the step distance if the best filter value was the same as last time |
| 331 if (filt_best == filt_mid) { |
| 332 filter_step = filter_step / 2; |
| 333 filt_direction = 0; |
| 334 } else { |
| 335 filt_direction = (filt_best < filt_mid) ? -1 : 1; |
| 336 filt_mid = filt_best; |
| 337 } |
| 338 } |
| 339 |
| 340 cm->filter_level = filt_best; |
| 341 } |
OLD | NEW |