1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // This code is licensed under the same terms as WebM:
4 // Software License Agreement: http://www.webmproject.org/license/software/
5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6 // -----------------------------------------------------------------------------
7 //
8 // Selecting filter level
9 //
10 // Author: somnath@google.com (Somnath Banerjee)
11
12 #include "./vp8enci.h"
13
14 #if defined(__cplusplus) || defined(c_plusplus)
15 extern "C" {
16 #endif
17
18 // NOTE: clip1, tables and InitTables are repeated entries of dsp.c
19 static uint8_t abs0[255 + 255 + 1]; // abs(i)
20 static uint8_t abs1[255 + 255 + 1]; // abs(i)>>1
21 static int8_t sclip1[1020 + 1020 + 1]; // clips [-1020, 1020] to [-128, 127]
22 static int8_t sclip2[112 + 112 + 1]; // clips [-112, 112] to [-16, 15]
23 static uint8_t clip1[255 + 510 + 1]; // clips [-255,510] to [0,255]
24
25 static int tables_ok = 0;
26
InitTables(void)27 static void InitTables(void) {
28 if (!tables_ok) {
29 int i;
30 for (i = -255; i <= 255; ++i) {
31 abs0[255 + i] = (i < 0) ? -i : i;
32 abs1[255 + i] = abs0[255 + i] >> 1;
33 }
34 for (i = -1020; i <= 1020; ++i) {
35 sclip1[1020 + i] = (i < -128) ? -128 : (i > 127) ? 127 : i;
36 }
37 for (i = -112; i <= 112; ++i) {
38 sclip2[112 + i] = (i < -16) ? -16 : (i > 15) ? 15 : i;
39 }
40 for (i = -255; i <= 255 + 255; ++i) {
41 clip1[255 + i] = (i < 0) ? 0 : (i > 255) ? 255 : i;
42 }
43 tables_ok = 1;
44 }
45 }
46
47 //------------------------------------------------------------------------------
48 // Edge filtering functions
49
50 // 4 pixels in, 2 pixels out
do_filter2(uint8_t * p,int step)51 static WEBP_INLINE void do_filter2(uint8_t* p, int step) {
52 const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
53 const int a = 3 * (q0 - p0) + sclip1[1020 + p1 - q1];
54 const int a1 = sclip2[112 + ((a + 4) >> 3)];
55 const int a2 = sclip2[112 + ((a + 3) >> 3)];
56 p[-step] = clip1[255 + p0 + a2];
57 p[ 0] = clip1[255 + q0 - a1];
58 }
59
60 // 4 pixels in, 4 pixels out
do_filter4(uint8_t * p,int step)61 static WEBP_INLINE void do_filter4(uint8_t* p, int step) {
62 const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
63 const int a = 3 * (q0 - p0);
64 const int a1 = sclip2[112 + ((a + 4) >> 3)];
65 const int a2 = sclip2[112 + ((a + 3) >> 3)];
66 const int a3 = (a1 + 1) >> 1;
67 p[-2*step] = clip1[255 + p1 + a3];
68 p[- step] = clip1[255 + p0 + a2];
69 p[ 0] = clip1[255 + q0 - a1];
70 p[ step] = clip1[255 + q1 - a3];
71 }
72
73 // high edge-variance
hev(const uint8_t * p,int step,int thresh)74 static WEBP_INLINE int hev(const uint8_t* p, int step, int thresh) {
75 const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
76 return (abs0[255 + p1 - p0] > thresh) || (abs0[255 + q1 - q0] > thresh);
77 }
78
needs_filter(const uint8_t * p,int step,int thresh)79 static WEBP_INLINE int needs_filter(const uint8_t* p, int step, int thresh) {
80 const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
81 return (2 * abs0[255 + p0 - q0] + abs1[255 + p1 - q1]) <= thresh;
82 }
83
needs_filter2(const uint8_t * p,int step,int t,int it)84 static WEBP_INLINE int needs_filter2(const uint8_t* p,
85 int step, int t, int it) {
86 const int p3 = p[-4*step], p2 = p[-3*step], p1 = p[-2*step], p0 = p[-step];
87 const int q0 = p[0], q1 = p[step], q2 = p[2*step], q3 = p[3*step];
88 if ((2 * abs0[255 + p0 - q0] + abs1[255 + p1 - q1]) > t)
89 return 0;
90 return abs0[255 + p3 - p2] <= it && abs0[255 + p2 - p1] <= it &&
91 abs0[255 + p1 - p0] <= it && abs0[255 + q3 - q2] <= it &&
92 abs0[255 + q2 - q1] <= it && abs0[255 + q1 - q0] <= it;
93 }
94
95 //------------------------------------------------------------------------------
96 // Simple In-loop filtering (Paragraph 15.2)
97
SimpleVFilter16(uint8_t * p,int stride,int thresh)98 static void SimpleVFilter16(uint8_t* p, int stride, int thresh) {
99 int i;
100 for (i = 0; i < 16; ++i) {
101 if (needs_filter(p + i, stride, thresh)) {
102 do_filter2(p + i, stride);
103 }
104 }
105 }
106
SimpleHFilter16(uint8_t * p,int stride,int thresh)107 static void SimpleHFilter16(uint8_t* p, int stride, int thresh) {
108 int i;
109 for (i = 0; i < 16; ++i) {
110 if (needs_filter(p + i * stride, 1, thresh)) {
111 do_filter2(p + i * stride, 1);
112 }
113 }
114 }
115
SimpleVFilter16i(uint8_t * p,int stride,int thresh)116 static void SimpleVFilter16i(uint8_t* p, int stride, int thresh) {
117 int k;
118 for (k = 3; k > 0; --k) {
119 p += 4 * stride;
120 SimpleVFilter16(p, stride, thresh);
121 }
122 }
123
SimpleHFilter16i(uint8_t * p,int stride,int thresh)124 static void SimpleHFilter16i(uint8_t* p, int stride, int thresh) {
125 int k;
126 for (k = 3; k > 0; --k) {
127 p += 4;
128 SimpleHFilter16(p, stride, thresh);
129 }
130 }
131
132 //------------------------------------------------------------------------------
133 // Complex In-loop filtering (Paragraph 15.3)
134
FilterLoop24(uint8_t * p,int hstride,int vstride,int size,int thresh,int ithresh,int hev_thresh)135 static WEBP_INLINE void FilterLoop24(uint8_t* p,
136 int hstride, int vstride, int size,
137 int thresh, int ithresh, int hev_thresh) {
138 while (size-- > 0) {
139 if (needs_filter2(p, hstride, thresh, ithresh)) {
140 if (hev(p, hstride, hev_thresh)) {
141 do_filter2(p, hstride);
142 } else {
143 do_filter4(p, hstride);
144 }
145 }
146 p += vstride;
147 }
148 }
149
150 // on three inner edges
VFilter16i(uint8_t * p,int stride,int thresh,int ithresh,int hev_thresh)151 static void VFilter16i(uint8_t* p, int stride,
152 int thresh, int ithresh, int hev_thresh) {
153 int k;
154 for (k = 3; k > 0; --k) {
155 p += 4 * stride;
156 FilterLoop24(p, stride, 1, 16, thresh, ithresh, hev_thresh);
157 }
158 }
159
HFilter16i(uint8_t * p,int stride,int thresh,int ithresh,int hev_thresh)160 static void HFilter16i(uint8_t* p, int stride,
161 int thresh, int ithresh, int hev_thresh) {
162 int k;
163 for (k = 3; k > 0; --k) {
164 p += 4;
165 FilterLoop24(p, 1, stride, 16, thresh, ithresh, hev_thresh);
166 }
167 }
168
VFilter8i(uint8_t * u,uint8_t * v,int stride,int thresh,int ithresh,int hev_thresh)169 static void VFilter8i(uint8_t* u, uint8_t* v, int stride,
170 int thresh, int ithresh, int hev_thresh) {
171 FilterLoop24(u + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
172 FilterLoop24(v + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
173 }
174
HFilter8i(uint8_t * u,uint8_t * v,int stride,int thresh,int ithresh,int hev_thresh)175 static void HFilter8i(uint8_t* u, uint8_t* v, int stride,
176 int thresh, int ithresh, int hev_thresh) {
177 FilterLoop24(u + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
178 FilterLoop24(v + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
179 }
180
181 //------------------------------------------------------------------------------
182
183 void (*VP8EncVFilter16i)(uint8_t*, int, int, int, int) = VFilter16i;
184 void (*VP8EncHFilter16i)(uint8_t*, int, int, int, int) = HFilter16i;
185 void (*VP8EncVFilter8i)(uint8_t*, uint8_t*, int, int, int, int) = VFilter8i;
186 void (*VP8EncHFilter8i)(uint8_t*, uint8_t*, int, int, int, int) = HFilter8i;
187
188 void (*VP8EncSimpleVFilter16i)(uint8_t*, int, int) = SimpleVFilter16i;
189 void (*VP8EncSimpleHFilter16i)(uint8_t*, int, int) = SimpleHFilter16i;
190
191 //------------------------------------------------------------------------------
192 // Paragraph 15.4: compute the inner-edge filtering strength
193
GetILevel(int sharpness,int level)194 static int GetILevel(int sharpness, int level) {
195 if (sharpness > 0) {
196 if (sharpness > 4) {
197 level >>= 2;
198 } else {
199 level >>= 1;
200 }
201 if (level > 9 - sharpness) {
202 level = 9 - sharpness;
203 }
204 }
205 if (level < 1) level = 1;
206 return level;
207 }
208
DoFilter(const VP8EncIterator * const it,int level)209 static void DoFilter(const VP8EncIterator* const it, int level) {
210 const VP8Encoder* const enc = it->enc_;
211 const int ilevel = GetILevel(enc->config_->filter_sharpness, level);
212 const int limit = 2 * level + ilevel;
213
214 uint8_t* const y_dst = it->yuv_out2_ + Y_OFF;
215 uint8_t* const u_dst = it->yuv_out2_ + U_OFF;
216 uint8_t* const v_dst = it->yuv_out2_ + V_OFF;
217
218 // copy current block to yuv_out2_
219 memcpy(y_dst, it->yuv_out_, YUV_SIZE * sizeof(uint8_t));
220
221 if (enc->filter_hdr_.simple_ == 1) { // simple
222 VP8EncSimpleHFilter16i(y_dst, BPS, limit);
223 VP8EncSimpleVFilter16i(y_dst, BPS, limit);
224 } else { // complex
225 const int hev_thresh = (level >= 40) ? 2 : (level >= 15) ? 1 : 0;
226 VP8EncHFilter16i(y_dst, BPS, limit, ilevel, hev_thresh);
227 VP8EncHFilter8i(u_dst, v_dst, BPS, limit, ilevel, hev_thresh);
228 VP8EncVFilter16i(y_dst, BPS, limit, ilevel, hev_thresh);
229 VP8EncVFilter8i(u_dst, v_dst, BPS, limit, ilevel, hev_thresh);
230 }
231 }
232
233 //------------------------------------------------------------------------------
234 // SSIM metric
235
236 enum { KERNEL = 3 };
237 static const double kMinValue = 1.e-10; // minimal threshold
238
VP8SSIMAddStats(const DistoStats * const src,DistoStats * const dst)239 void VP8SSIMAddStats(const DistoStats* const src, DistoStats* const dst) {
240 dst->w += src->w;
241 dst->xm += src->xm;
242 dst->ym += src->ym;
243 dst->xxm += src->xxm;
244 dst->xym += src->xym;
245 dst->yym += src->yym;
246 }
247
VP8SSIMAccumulate(const uint8_t * src1,int stride1,const uint8_t * src2,int stride2,int xo,int yo,int W,int H,DistoStats * const stats)248 static void VP8SSIMAccumulate(const uint8_t* src1, int stride1,
249 const uint8_t* src2, int stride2,
250 int xo, int yo, int W, int H,
251 DistoStats* const stats) {
252 const int ymin = (yo - KERNEL < 0) ? 0 : yo - KERNEL;
253 const int ymax = (yo + KERNEL > H - 1) ? H - 1 : yo + KERNEL;
254 const int xmin = (xo - KERNEL < 0) ? 0 : xo - KERNEL;
255 const int xmax = (xo + KERNEL > W - 1) ? W - 1 : xo + KERNEL;
256 int x, y;
257 src1 += ymin * stride1;
258 src2 += ymin * stride2;
259 for (y = ymin; y <= ymax; ++y, src1 += stride1, src2 += stride2) {
260 for (x = xmin; x <= xmax; ++x) {
261 const int s1 = src1[x];
262 const int s2 = src2[x];
263 stats->w += 1;
264 stats->xm += s1;
265 stats->ym += s2;
266 stats->xxm += s1 * s1;
267 stats->xym += s1 * s2;
268 stats->yym += s2 * s2;
269 }
270 }
271 }
272
VP8SSIMGet(const DistoStats * const stats)273 double VP8SSIMGet(const DistoStats* const stats) {
274 const double xmxm = stats->xm * stats->xm;
275 const double ymym = stats->ym * stats->ym;
276 const double xmym = stats->xm * stats->ym;
277 const double w2 = stats->w * stats->w;
278 double sxx = stats->xxm * stats->w - xmxm;
279 double syy = stats->yym * stats->w - ymym;
280 double sxy = stats->xym * stats->w - xmym;
281 double C1, C2;
282 double fnum;
283 double fden;
284 // small errors are possible, due to rounding. Clamp to zero.
285 if (sxx < 0.) sxx = 0.;
286 if (syy < 0.) syy = 0.;
287 C1 = 6.5025 * w2;
288 C2 = 58.5225 * w2;
289 fnum = (2 * xmym + C1) * (2 * sxy + C2);
290 fden = (xmxm + ymym + C1) * (sxx + syy + C2);
291 return (fden != 0.) ? fnum / fden : kMinValue;
292 }
293
VP8SSIMGetSquaredError(const DistoStats * const s)294 double VP8SSIMGetSquaredError(const DistoStats* const s) {
295 if (s->w > 0.) {
296 const double iw2 = 1. / (s->w * s->w);
297 const double sxx = s->xxm * s->w - s->xm * s->xm;
298 const double syy = s->yym * s->w - s->ym * s->ym;
299 const double sxy = s->xym * s->w - s->xm * s->ym;
300 const double SSE = iw2 * (sxx + syy - 2. * sxy);
301 if (SSE > kMinValue) return SSE;
302 }
303 return kMinValue;
304 }
305
VP8SSIMAccumulatePlane(const uint8_t * src1,int stride1,const uint8_t * src2,int stride2,int W,int H,DistoStats * const stats)306 void VP8SSIMAccumulatePlane(const uint8_t* src1, int stride1,
307 const uint8_t* src2, int stride2,
308 int W, int H, DistoStats* const stats) {
309 int x, y;
310 for (y = 0; y < H; ++y) {
311 for (x = 0; x < W; ++x) {
312 VP8SSIMAccumulate(src1, stride1, src2, stride2, x, y, W, H, stats);
313 }
314 }
315 }
316
GetMBSSIM(const uint8_t * yuv1,const uint8_t * yuv2)317 static double GetMBSSIM(const uint8_t* yuv1, const uint8_t* yuv2) {
318 int x, y;
319 DistoStats s = { .0, .0, .0, .0, .0, .0 };
320
321 // compute SSIM in a 10 x 10 window
322 for (x = 3; x < 13; x++) {
323 for (y = 3; y < 13; y++) {
324 VP8SSIMAccumulate(yuv1 + Y_OFF, BPS, yuv2 + Y_OFF, BPS, x, y, 16, 16, &s);
325 }
326 }
327 for (x = 1; x < 7; x++) {
328 for (y = 1; y < 7; y++) {
329 VP8SSIMAccumulate(yuv1 + U_OFF, BPS, yuv2 + U_OFF, BPS, x, y, 8, 8, &s);
330 VP8SSIMAccumulate(yuv1 + V_OFF, BPS, yuv2 + V_OFF, BPS, x, y, 8, 8, &s);
331 }
332 }
333 return VP8SSIMGet(&s);
334 }
335
336 //------------------------------------------------------------------------------
337 // Exposed APIs: Encoder should call the following 3 functions to adjust
338 // loop filter strength
339
VP8InitFilter(VP8EncIterator * const it)340 void VP8InitFilter(VP8EncIterator* const it) {
341 int s, i;
342 if (!it->lf_stats_) return;
343
344 InitTables();
345 for (s = 0; s < NUM_MB_SEGMENTS; s++) {
346 for (i = 0; i < MAX_LF_LEVELS; i++) {
347 (*it->lf_stats_)[s][i] = 0;
348 }
349 }
350 }
351
VP8StoreFilterStats(VP8EncIterator * const it)352 void VP8StoreFilterStats(VP8EncIterator* const it) {
353 int d;
354 const int s = it->mb_->segment_;
355 const int level0 = it->enc_->dqm_[s].fstrength_; // TODO: ref_lf_delta[]
356
357 // explore +/-quant range of values around level0
358 const int delta_min = -it->enc_->dqm_[s].quant_;
359 const int delta_max = it->enc_->dqm_[s].quant_;
360 const int step_size = (delta_max - delta_min >= 4) ? 4 : 1;
361
362 if (!it->lf_stats_) return;
363
364 // NOTE: Currently we are applying filter only across the sublock edges
365 // There are two reasons for that.
366 // 1. Applying filter on macro block edges will change the pixels in
367 // the left and top macro blocks. That will be hard to restore
368 // 2. Macro Blocks on the bottom and right are not yet compressed. So we
369 // cannot apply filter on the right and bottom macro block edges.
370 if (it->mb_->type_ == 1 && it->mb_->skip_) return;
371
372 // Always try filter level zero
373 (*it->lf_stats_)[s][0] += GetMBSSIM(it->yuv_in_, it->yuv_out_);
374
375 for (d = delta_min; d <= delta_max; d += step_size) {
376 const int level = level0 + d;
377 if (level <= 0 || level >= MAX_LF_LEVELS) {
378 continue;
379 }
380 DoFilter(it, level);
381 (*it->lf_stats_)[s][level] += GetMBSSIM(it->yuv_in_, it->yuv_out2_);
382 }
383 }
384
VP8AdjustFilterStrength(VP8EncIterator * const it)385 void VP8AdjustFilterStrength(VP8EncIterator* const it) {
386 int s;
387 VP8Encoder* const enc = it->enc_;
388
389 if (!it->lf_stats_) {
390 return;
391 }
392 for (s = 0; s < NUM_MB_SEGMENTS; s++) {
393 int i, best_level = 0;
394 // Improvement over filter level 0 should be at least 1e-5 (relatively)
395 double best_v = 1.00001 * (*it->lf_stats_)[s][0];
396 for (i = 1; i < MAX_LF_LEVELS; i++) {
397 const double v = (*it->lf_stats_)[s][i];
398 if (v > best_v) {
399 best_v = v;
400 best_level = i;
401 }
402 }
403 enc->dqm_[s].fstrength_ = best_level;
404 }
405 }
406
407 #if defined(__cplusplus) || defined(c_plusplus)
408 } // extern "C"
409 #endif
410