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