| OLD | NEW |
| (Empty) |
| 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 "./vp8enci.h" | |
| 16 #include "../dsp/dsp.h" | |
| 17 | |
| 18 // This table gives, for a given sharpness, the filtering strength to be | |
| 19 // used (at least) in order to filter a given edge step delta. | |
| 20 // This is constructed by brute force inspection: for all delta, we iterate | |
| 21 // over all possible filtering strength / thresh until needs_filter() returns | |
| 22 // true. | |
| 23 #define MAX_DELTA_SIZE 64 | |
| 24 static const uint8_t kLevelsFromDelta[8][MAX_DELTA_SIZE] = { | |
| 25 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, | |
| 26 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, | |
| 27 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, | |
| 28 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 }, | |
| 29 { 0, 1, 2, 3, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 17, 18, | |
| 30 20, 21, 23, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 39, 41, 42, | |
| 31 44, 45, 47, 48, 50, 51, 53, 54, 56, 57, 59, 60, 62, 63, 63, 63, | |
| 32 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }, | |
| 33 { 0, 1, 2, 3, 5, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 19, | |
| 34 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 43, | |
| 35 44, 46, 47, 49, 50, 52, 53, 55, 56, 58, 59, 61, 62, 63, 63, 63, | |
| 36 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }, | |
| 37 { 0, 1, 2, 3, 5, 6, 7, 8, 9, 11, 12, 13, 15, 16, 18, 19, | |
| 38 21, 22, 24, 25, 27, 28, 30, 31, 33, 34, 36, 37, 39, 40, 42, 43, | |
| 39 45, 46, 48, 49, 51, 52, 54, 55, 57, 58, 60, 61, 63, 63, 63, 63, | |
| 40 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }, | |
| 41 { 0, 1, 2, 3, 5, 6, 7, 8, 9, 11, 12, 14, 15, 17, 18, 20, | |
| 42 21, 23, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 39, 41, 42, 44, | |
| 43 45, 47, 48, 50, 51, 53, 54, 56, 57, 59, 60, 62, 63, 63, 63, 63, | |
| 44 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }, | |
| 45 { 0, 1, 2, 4, 5, 7, 8, 9, 11, 12, 13, 15, 16, 17, 19, 20, | |
| 46 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 43, 44, | |
| 47 46, 47, 49, 50, 52, 53, 55, 56, 58, 59, 61, 62, 63, 63, 63, 63, | |
| 48 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }, | |
| 49 { 0, 1, 2, 4, 5, 7, 8, 9, 11, 12, 13, 15, 16, 18, 19, 21, | |
| 50 22, 24, 25, 27, 28, 30, 31, 33, 34, 36, 37, 39, 40, 42, 43, 45, | |
| 51 46, 48, 49, 51, 52, 54, 55, 57, 58, 60, 61, 63, 63, 63, 63, 63, | |
| 52 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }, | |
| 53 { 0, 1, 2, 4, 5, 7, 8, 9, 11, 12, 14, 15, 17, 18, 20, 21, | |
| 54 23, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 39, 41, 42, 44, 45, | |
| 55 47, 48, 50, 51, 53, 54, 56, 57, 59, 60, 62, 63, 63, 63, 63, 63, | |
| 56 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 } | |
| 57 }; | |
| 58 | |
| 59 int VP8FilterStrengthFromDelta(int sharpness, int delta) { | |
| 60 const int pos = (delta < MAX_DELTA_SIZE) ? delta : MAX_DELTA_SIZE - 1; | |
| 61 assert(sharpness >= 0 && sharpness <= 7); | |
| 62 return kLevelsFromDelta[sharpness][pos]; | |
| 63 } | |
| 64 | |
| 65 //------------------------------------------------------------------------------ | |
| 66 // Paragraph 15.4: compute the inner-edge filtering strength | |
| 67 | |
| 68 static int GetILevel(int sharpness, int level) { | |
| 69 if (sharpness > 0) { | |
| 70 if (sharpness > 4) { | |
| 71 level >>= 2; | |
| 72 } else { | |
| 73 level >>= 1; | |
| 74 } | |
| 75 if (level > 9 - sharpness) { | |
| 76 level = 9 - sharpness; | |
| 77 } | |
| 78 } | |
| 79 if (level < 1) level = 1; | |
| 80 return level; | |
| 81 } | |
| 82 | |
| 83 static void DoFilter(const VP8EncIterator* const it, int level) { | |
| 84 const VP8Encoder* const enc = it->enc_; | |
| 85 const int ilevel = GetILevel(enc->config_->filter_sharpness, level); | |
| 86 const int limit = 2 * level + ilevel; | |
| 87 | |
| 88 uint8_t* const y_dst = it->yuv_out2_ + Y_OFF_ENC; | |
| 89 uint8_t* const u_dst = it->yuv_out2_ + U_OFF_ENC; | |
| 90 uint8_t* const v_dst = it->yuv_out2_ + V_OFF_ENC; | |
| 91 | |
| 92 // copy current block to yuv_out2_ | |
| 93 memcpy(y_dst, it->yuv_out_, YUV_SIZE_ENC * sizeof(uint8_t)); | |
| 94 | |
| 95 if (enc->filter_hdr_.simple_ == 1) { // simple | |
| 96 VP8SimpleHFilter16i(y_dst, BPS, limit); | |
| 97 VP8SimpleVFilter16i(y_dst, BPS, limit); | |
| 98 } else { // complex | |
| 99 const int hev_thresh = (level >= 40) ? 2 : (level >= 15) ? 1 : 0; | |
| 100 VP8HFilter16i(y_dst, BPS, limit, ilevel, hev_thresh); | |
| 101 VP8HFilter8i(u_dst, v_dst, BPS, limit, ilevel, hev_thresh); | |
| 102 VP8VFilter16i(y_dst, BPS, limit, ilevel, hev_thresh); | |
| 103 VP8VFilter8i(u_dst, v_dst, BPS, limit, ilevel, hev_thresh); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 //------------------------------------------------------------------------------ | |
| 108 // SSIM metric | |
| 109 | |
| 110 static const double kMinValue = 1.e-10; // minimal threshold | |
| 111 | |
| 112 void VP8SSIMAddStats(const VP8DistoStats* const src, VP8DistoStats* const dst) { | |
| 113 dst->w += src->w; | |
| 114 dst->xm += src->xm; | |
| 115 dst->ym += src->ym; | |
| 116 dst->xxm += src->xxm; | |
| 117 dst->xym += src->xym; | |
| 118 dst->yym += src->yym; | |
| 119 } | |
| 120 | |
| 121 double VP8SSIMGet(const VP8DistoStats* const stats) { | |
| 122 const double xmxm = stats->xm * stats->xm; | |
| 123 const double ymym = stats->ym * stats->ym; | |
| 124 const double xmym = stats->xm * stats->ym; | |
| 125 const double w2 = stats->w * stats->w; | |
| 126 double sxx = stats->xxm * stats->w - xmxm; | |
| 127 double syy = stats->yym * stats->w - ymym; | |
| 128 double sxy = stats->xym * stats->w - xmym; | |
| 129 double C1, C2; | |
| 130 double fnum; | |
| 131 double fden; | |
| 132 // small errors are possible, due to rounding. Clamp to zero. | |
| 133 if (sxx < 0.) sxx = 0.; | |
| 134 if (syy < 0.) syy = 0.; | |
| 135 C1 = 6.5025 * w2; | |
| 136 C2 = 58.5225 * w2; | |
| 137 fnum = (2 * xmym + C1) * (2 * sxy + C2); | |
| 138 fden = (xmxm + ymym + C1) * (sxx + syy + C2); | |
| 139 return (fden != 0.) ? fnum / fden : kMinValue; | |
| 140 } | |
| 141 | |
| 142 double VP8SSIMGetSquaredError(const VP8DistoStats* const s) { | |
| 143 if (s->w > 0.) { | |
| 144 const double iw2 = 1. / (s->w * s->w); | |
| 145 const double sxx = s->xxm * s->w - s->xm * s->xm; | |
| 146 const double syy = s->yym * s->w - s->ym * s->ym; | |
| 147 const double sxy = s->xym * s->w - s->xm * s->ym; | |
| 148 const double SSE = iw2 * (sxx + syy - 2. * sxy); | |
| 149 if (SSE > kMinValue) return SSE; | |
| 150 } | |
| 151 return kMinValue; | |
| 152 } | |
| 153 | |
| 154 #define LIMIT(A, M) ((A) > (M) ? (M) : (A)) | |
| 155 static void VP8SSIMAccumulateRow(const uint8_t* src1, int stride1, | |
| 156 const uint8_t* src2, int stride2, | |
| 157 int y, int W, int H, | |
| 158 VP8DistoStats* const stats) { | |
| 159 int x = 0; | |
| 160 const int w0 = LIMIT(VP8_SSIM_KERNEL, W); | |
| 161 for (x = 0; x < w0; ++x) { | |
| 162 VP8SSIMAccumulateClipped(src1, stride1, src2, stride2, x, y, W, H, stats); | |
| 163 } | |
| 164 for (; x <= W - 8 + VP8_SSIM_KERNEL; ++x) { | |
| 165 VP8SSIMAccumulate( | |
| 166 src1 + (y - VP8_SSIM_KERNEL) * stride1 + (x - VP8_SSIM_KERNEL), stride1, | |
| 167 src2 + (y - VP8_SSIM_KERNEL) * stride2 + (x - VP8_SSIM_KERNEL), stride2, | |
| 168 stats); | |
| 169 } | |
| 170 for (; x < W; ++x) { | |
| 171 VP8SSIMAccumulateClipped(src1, stride1, src2, stride2, x, y, W, H, stats); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 void VP8SSIMAccumulatePlane(const uint8_t* src1, int stride1, | |
| 176 const uint8_t* src2, int stride2, | |
| 177 int W, int H, VP8DistoStats* const stats) { | |
| 178 int x, y; | |
| 179 const int h0 = LIMIT(VP8_SSIM_KERNEL, H); | |
| 180 const int h1 = LIMIT(VP8_SSIM_KERNEL, H - VP8_SSIM_KERNEL); | |
| 181 for (y = 0; y < h0; ++y) { | |
| 182 for (x = 0; x < W; ++x) { | |
| 183 VP8SSIMAccumulateClipped(src1, stride1, src2, stride2, x, y, W, H, stats); | |
| 184 } | |
| 185 } | |
| 186 for (; y < h1; ++y) { | |
| 187 VP8SSIMAccumulateRow(src1, stride1, src2, stride2, y, W, H, stats); | |
| 188 } | |
| 189 for (; y < H; ++y) { | |
| 190 for (x = 0; x < W; ++x) { | |
| 191 VP8SSIMAccumulateClipped(src1, stride1, src2, stride2, x, y, W, H, stats); | |
| 192 } | |
| 193 } | |
| 194 } | |
| 195 #undef LIMIT | |
| 196 | |
| 197 static double GetMBSSIM(const uint8_t* yuv1, const uint8_t* yuv2) { | |
| 198 int x, y; | |
| 199 VP8DistoStats s = { .0, .0, .0, .0, .0, .0 }; | |
| 200 | |
| 201 // compute SSIM in a 10 x 10 window | |
| 202 for (y = VP8_SSIM_KERNEL; y < 16 - VP8_SSIM_KERNEL; y++) { | |
| 203 for (x = VP8_SSIM_KERNEL; x < 16 - VP8_SSIM_KERNEL; x++) { | |
| 204 VP8SSIMAccumulateClipped(yuv1 + Y_OFF_ENC, BPS, yuv2 + Y_OFF_ENC, BPS, | |
| 205 x, y, 16, 16, &s); | |
| 206 } | |
| 207 } | |
| 208 for (x = 1; x < 7; x++) { | |
| 209 for (y = 1; y < 7; y++) { | |
| 210 VP8SSIMAccumulateClipped(yuv1 + U_OFF_ENC, BPS, yuv2 + U_OFF_ENC, BPS, | |
| 211 x, y, 8, 8, &s); | |
| 212 VP8SSIMAccumulateClipped(yuv1 + V_OFF_ENC, BPS, yuv2 + V_OFF_ENC, BPS, | |
| 213 x, y, 8, 8, &s); | |
| 214 } | |
| 215 } | |
| 216 return VP8SSIMGet(&s); | |
| 217 } | |
| 218 | |
| 219 //------------------------------------------------------------------------------ | |
| 220 // Exposed APIs: Encoder should call the following 3 functions to adjust | |
| 221 // loop filter strength | |
| 222 | |
| 223 void VP8InitFilter(VP8EncIterator* const it) { | |
| 224 if (it->lf_stats_ != NULL) { | |
| 225 int s, i; | |
| 226 for (s = 0; s < NUM_MB_SEGMENTS; s++) { | |
| 227 for (i = 0; i < MAX_LF_LEVELS; i++) { | |
| 228 (*it->lf_stats_)[s][i] = 0; | |
| 229 } | |
| 230 } | |
| 231 VP8SSIMDspInit(); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 void VP8StoreFilterStats(VP8EncIterator* const it) { | |
| 236 int d; | |
| 237 VP8Encoder* const enc = it->enc_; | |
| 238 const int s = it->mb_->segment_; | |
| 239 const int level0 = enc->dqm_[s].fstrength_; | |
| 240 | |
| 241 // explore +/-quant range of values around level0 | |
| 242 const int delta_min = -enc->dqm_[s].quant_; | |
| 243 const int delta_max = enc->dqm_[s].quant_; | |
| 244 const int step_size = (delta_max - delta_min >= 4) ? 4 : 1; | |
| 245 | |
| 246 if (it->lf_stats_ == NULL) return; | |
| 247 | |
| 248 // NOTE: Currently we are applying filter only across the sublock edges | |
| 249 // There are two reasons for that. | |
| 250 // 1. Applying filter on macro block edges will change the pixels in | |
| 251 // the left and top macro blocks. That will be hard to restore | |
| 252 // 2. Macro Blocks on the bottom and right are not yet compressed. So we | |
| 253 // cannot apply filter on the right and bottom macro block edges. | |
| 254 if (it->mb_->type_ == 1 && it->mb_->skip_) return; | |
| 255 | |
| 256 // Always try filter level zero | |
| 257 (*it->lf_stats_)[s][0] += GetMBSSIM(it->yuv_in_, it->yuv_out_); | |
| 258 | |
| 259 for (d = delta_min; d <= delta_max; d += step_size) { | |
| 260 const int level = level0 + d; | |
| 261 if (level <= 0 || level >= MAX_LF_LEVELS) { | |
| 262 continue; | |
| 263 } | |
| 264 DoFilter(it, level); | |
| 265 (*it->lf_stats_)[s][level] += GetMBSSIM(it->yuv_in_, it->yuv_out2_); | |
| 266 } | |
| 267 } | |
| 268 | |
| 269 void VP8AdjustFilterStrength(VP8EncIterator* const it) { | |
| 270 VP8Encoder* const enc = it->enc_; | |
| 271 if (it->lf_stats_ != NULL) { | |
| 272 int s; | |
| 273 for (s = 0; s < NUM_MB_SEGMENTS; s++) { | |
| 274 int i, best_level = 0; | |
| 275 // Improvement over filter level 0 should be at least 1e-5 (relatively) | |
| 276 double best_v = 1.00001 * (*it->lf_stats_)[s][0]; | |
| 277 for (i = 1; i < MAX_LF_LEVELS; i++) { | |
| 278 const double v = (*it->lf_stats_)[s][i]; | |
| 279 if (v > best_v) { | |
| 280 best_v = v; | |
| 281 best_level = i; | |
| 282 } | |
| 283 } | |
| 284 enc->dqm_[s].fstrength_ = best_level; | |
| 285 } | |
| 286 } else if (enc->config_->filter_strength > 0) { | |
| 287 int max_level = 0; | |
| 288 int s; | |
| 289 for (s = 0; s < NUM_MB_SEGMENTS; s++) { | |
| 290 VP8SegmentInfo* const dqm = &enc->dqm_[s]; | |
| 291 // this '>> 3' accounts for some inverse WHT scaling | |
| 292 const int delta = (dqm->max_edge_ * dqm->y2_.q_[1]) >> 3; | |
| 293 const int level = | |
| 294 VP8FilterStrengthFromDelta(enc->filter_hdr_.sharpness_, delta); | |
| 295 if (level > dqm->fstrength_) { | |
| 296 dqm->fstrength_ = level; | |
| 297 } | |
| 298 if (max_level < dqm->fstrength_) { | |
| 299 max_level = dqm->fstrength_; | |
| 300 } | |
| 301 } | |
| 302 enc->filter_hdr_.level_ = max_level; | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 // ----------------------------------------------------------------------------- | |
| OLD | NEW |