filter_enc.c (8957B)
1 // Copyright 2011 Google Inc. All Rights Reserved. 2 // 3 // Use of this source code is governed by a BSD-style license 4 // that can be found in the COPYING file in the root of the source 5 // tree. An additional intellectual property rights grant can be found 6 // in the file PATENTS. All contributing project authors may 7 // be found in the AUTHORS file in the root of the source tree. 8 // ----------------------------------------------------------------------------- 9 // 10 // Selecting filter level 11 // 12 // Author: somnath@google.com (Somnath Banerjee) 13 14 #include <assert.h> 15 #include <stddef.h> 16 #include <string.h> 17 18 #include "src/dec/common_dec.h" 19 #include "src/webp/types.h" 20 #include "src/dsp/dsp.h" 21 #include "src/enc/vp8i_enc.h" 22 23 // This table gives, for a given sharpness, the filtering strength to be 24 // used (at least) in order to filter a given edge step delta. 25 // This is constructed by brute force inspection: for all delta, we iterate 26 // over all possible filtering strength / thresh until needs_filter() returns 27 // true. 28 #define MAX_DELTA_SIZE 64 29 static const uint8_t kLevelsFromDelta[8][MAX_DELTA_SIZE] = { 30 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 31 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 33 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 }, 34 { 0, 1, 2, 3, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 17, 18, 35 20, 21, 23, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 39, 41, 42, 36 44, 45, 47, 48, 50, 51, 53, 54, 56, 57, 59, 60, 62, 63, 63, 63, 37 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }, 38 { 0, 1, 2, 3, 5, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 19, 39 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 43, 40 44, 46, 47, 49, 50, 52, 53, 55, 56, 58, 59, 61, 62, 63, 63, 63, 41 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }, 42 { 0, 1, 2, 3, 5, 6, 7, 8, 9, 11, 12, 13, 15, 16, 18, 19, 43 21, 22, 24, 25, 27, 28, 30, 31, 33, 34, 36, 37, 39, 40, 42, 43, 44 45, 46, 48, 49, 51, 52, 54, 55, 57, 58, 60, 61, 63, 63, 63, 63, 45 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }, 46 { 0, 1, 2, 3, 5, 6, 7, 8, 9, 11, 12, 14, 15, 17, 18, 20, 47 21, 23, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 39, 41, 42, 44, 48 45, 47, 48, 50, 51, 53, 54, 56, 57, 59, 60, 62, 63, 63, 63, 63, 49 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }, 50 { 0, 1, 2, 4, 5, 7, 8, 9, 11, 12, 13, 15, 16, 17, 19, 20, 51 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 43, 44, 52 46, 47, 49, 50, 52, 53, 55, 56, 58, 59, 61, 62, 63, 63, 63, 63, 53 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }, 54 { 0, 1, 2, 4, 5, 7, 8, 9, 11, 12, 13, 15, 16, 18, 19, 21, 55 22, 24, 25, 27, 28, 30, 31, 33, 34, 36, 37, 39, 40, 42, 43, 45, 56 46, 48, 49, 51, 52, 54, 55, 57, 58, 60, 61, 63, 63, 63, 63, 63, 57 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }, 58 { 0, 1, 2, 4, 5, 7, 8, 9, 11, 12, 14, 15, 17, 18, 20, 21, 59 23, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 39, 41, 42, 44, 45, 60 47, 48, 50, 51, 53, 54, 56, 57, 59, 60, 62, 63, 63, 63, 63, 63, 61 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 } 62 }; 63 64 int VP8FilterStrengthFromDelta(int sharpness, int delta) { 65 const int pos = (delta < MAX_DELTA_SIZE) ? delta : MAX_DELTA_SIZE - 1; 66 assert(sharpness >= 0 && sharpness <= 7); 67 return kLevelsFromDelta[sharpness][pos]; 68 } 69 70 //------------------------------------------------------------------------------ 71 // Paragraph 15.4: compute the inner-edge filtering strength 72 73 #if !defined(WEBP_REDUCE_SIZE) 74 75 static int GetILevel(int sharpness, int level) { 76 if (sharpness > 0) { 77 if (sharpness > 4) { 78 level >>= 2; 79 } else { 80 level >>= 1; 81 } 82 if (level > 9 - sharpness) { 83 level = 9 - sharpness; 84 } 85 } 86 if (level < 1) level = 1; 87 return level; 88 } 89 90 static void DoFilter(const VP8EncIterator* const it, int level) { 91 const VP8Encoder* const enc = it->enc; 92 const int ilevel = GetILevel(enc->config->filter_sharpness, level); 93 const int limit = 2 * level + ilevel; 94 95 uint8_t* const y_dst = it->yuv_out2 + Y_OFF_ENC; 96 uint8_t* const u_dst = it->yuv_out2 + U_OFF_ENC; 97 uint8_t* const v_dst = it->yuv_out2 + V_OFF_ENC; 98 99 // copy current block to yuv_out2 100 memcpy(y_dst, it->yuv_out, YUV_SIZE_ENC * sizeof(uint8_t)); 101 102 if (enc->filter_hdr.simple == 1) { // simple 103 VP8SimpleHFilter16i(y_dst, BPS, limit); 104 VP8SimpleVFilter16i(y_dst, BPS, limit); 105 } else { // complex 106 const int hev_thresh = (level >= 40) ? 2 : (level >= 15) ? 1 : 0; 107 VP8HFilter16i(y_dst, BPS, limit, ilevel, hev_thresh); 108 VP8HFilter8i(u_dst, v_dst, BPS, limit, ilevel, hev_thresh); 109 VP8VFilter16i(y_dst, BPS, limit, ilevel, hev_thresh); 110 VP8VFilter8i(u_dst, v_dst, BPS, limit, ilevel, hev_thresh); 111 } 112 } 113 114 //------------------------------------------------------------------------------ 115 // SSIM metric for one macroblock 116 117 static double GetMBSSIM(const uint8_t* yuv1, const uint8_t* yuv2) { 118 int x, y; 119 double sum = 0.; 120 121 // compute SSIM in a 10 x 10 window 122 for (y = VP8_SSIM_KERNEL; y < 16 - VP8_SSIM_KERNEL; y++) { 123 for (x = VP8_SSIM_KERNEL; x < 16 - VP8_SSIM_KERNEL; x++) { 124 sum += VP8SSIMGetClipped(yuv1 + Y_OFF_ENC, BPS, yuv2 + Y_OFF_ENC, BPS, 125 x, y, 16, 16); 126 } 127 } 128 for (x = 1; x < 7; x++) { 129 for (y = 1; y < 7; y++) { 130 sum += VP8SSIMGetClipped(yuv1 + U_OFF_ENC, BPS, yuv2 + U_OFF_ENC, BPS, 131 x, y, 8, 8); 132 sum += VP8SSIMGetClipped(yuv1 + V_OFF_ENC, BPS, yuv2 + V_OFF_ENC, BPS, 133 x, y, 8, 8); 134 } 135 } 136 return sum; 137 } 138 139 #endif // !defined(WEBP_REDUCE_SIZE) 140 141 //------------------------------------------------------------------------------ 142 // Exposed APIs: Encoder should call the following 3 functions to adjust 143 // loop filter strength 144 145 void VP8InitFilter(VP8EncIterator* const it) { 146 #if !defined(WEBP_REDUCE_SIZE) 147 if (it->lf_stats != NULL) { 148 int s, i; 149 for (s = 0; s < NUM_MB_SEGMENTS; s++) { 150 for (i = 0; i < MAX_LF_LEVELS; i++) { 151 (*it->lf_stats)[s][i] = 0; 152 } 153 } 154 VP8SSIMDspInit(); 155 } 156 #else 157 (void)it; 158 #endif 159 } 160 161 void VP8StoreFilterStats(VP8EncIterator* const it) { 162 #if !defined(WEBP_REDUCE_SIZE) 163 int d; 164 VP8Encoder* const enc = it->enc; 165 const int s = it->mb->segment; 166 const int level0 = enc->dqm[s].fstrength; 167 168 // explore +/-quant range of values around level0 169 const int delta_min = -enc->dqm[s].quant; 170 const int delta_max = enc->dqm[s].quant; 171 const int step_size = (delta_max - delta_min >= 4) ? 4 : 1; 172 173 if (it->lf_stats == NULL) return; 174 175 // NOTE: Currently we are applying filter only across the sublock edges 176 // There are two reasons for that. 177 // 1. Applying filter on macro block edges will change the pixels in 178 // the left and top macro blocks. That will be hard to restore 179 // 2. Macro Blocks on the bottom and right are not yet compressed. So we 180 // cannot apply filter on the right and bottom macro block edges. 181 if (it->mb->type == 1 && it->mb->skip) return; 182 183 // Always try filter level zero 184 (*it->lf_stats)[s][0] += GetMBSSIM(it->yuv_in, it->yuv_out); 185 186 for (d = delta_min; d <= delta_max; d += step_size) { 187 const int level = level0 + d; 188 if (level <= 0 || level >= MAX_LF_LEVELS) { 189 continue; 190 } 191 DoFilter(it, level); 192 (*it->lf_stats)[s][level] += GetMBSSIM(it->yuv_in, it->yuv_out2); 193 } 194 #else // defined(WEBP_REDUCE_SIZE) 195 (void)it; 196 #endif // !defined(WEBP_REDUCE_SIZE) 197 } 198 199 void VP8AdjustFilterStrength(VP8EncIterator* const it) { 200 VP8Encoder* const enc = it->enc; 201 #if !defined(WEBP_REDUCE_SIZE) 202 if (it->lf_stats != NULL) { 203 int s; 204 for (s = 0; s < NUM_MB_SEGMENTS; s++) { 205 int i, best_level = 0; 206 // Improvement over filter level 0 should be at least 1e-5 (relatively) 207 double best_v = 1.00001 * (*it->lf_stats)[s][0]; 208 for (i = 1; i < MAX_LF_LEVELS; i++) { 209 const double v = (*it->lf_stats)[s][i]; 210 if (v > best_v) { 211 best_v = v; 212 best_level = i; 213 } 214 } 215 enc->dqm[s].fstrength = best_level; 216 } 217 return; 218 } 219 #endif // !defined(WEBP_REDUCE_SIZE) 220 if (enc->config->filter_strength > 0) { 221 int max_level = 0; 222 int s; 223 for (s = 0; s < NUM_MB_SEGMENTS; s++) { 224 VP8SegmentInfo* const dqm = &enc->dqm[s]; 225 // this '>> 3' accounts for some inverse WHT scaling 226 const int delta = (dqm->max_edge * dqm->y2.q[1]) >> 3; 227 const int level = 228 VP8FilterStrengthFromDelta(enc->filter_hdr.sharpness, delta); 229 if (level > dqm->fstrength) { 230 dqm->fstrength = level; 231 } 232 if (max_level < dqm->fstrength) { 233 max_level = dqm->fstrength; 234 } 235 } 236 enc->filter_hdr.level = max_level; 237 } 238 } 239 240 // -----------------------------------------------------------------------------