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// filter estimation
11//
12// Author: Urvang (urvang@google.com)
13
14#include "./filters_utils.h"
15#include <stdlib.h>
16#include <string.h>
17
18// -----------------------------------------------------------------------------
19// Quick estimate of a potentially interesting filter mode to try.
20
21#define SMAX 16
22#define SDIFF(a, b) (abs((a) - (b)) >> 4)   // Scoring diff, in [0..SMAX)
23
24static WEBP_INLINE int GradientPredictor(uint8_t a, uint8_t b, uint8_t c) {
25  const int g = a + b - c;
26  return ((g & ~0xff) == 0) ? g : (g < 0) ? 0 : 255;  // clip to 8bit
27}
28
29WEBP_FILTER_TYPE WebPEstimateBestFilter(const uint8_t* data,
30                                        int width, int height, int stride) {
31  int i, j;
32  int bins[WEBP_FILTER_LAST][SMAX];
33  memset(bins, 0, sizeof(bins));
34
35  // We only sample every other pixels. That's enough.
36  for (j = 2; j < height - 1; j += 2) {
37    const uint8_t* const p = data + j * stride;
38    int mean = p[0];
39    for (i = 2; i < width - 1; i += 2) {
40      const int diff0 = SDIFF(p[i], mean);
41      const int diff1 = SDIFF(p[i], p[i - 1]);
42      const int diff2 = SDIFF(p[i], p[i - width]);
43      const int grad_pred =
44          GradientPredictor(p[i - 1], p[i - width], p[i - width - 1]);
45      const int diff3 = SDIFF(p[i], grad_pred);
46      bins[WEBP_FILTER_NONE][diff0] = 1;
47      bins[WEBP_FILTER_HORIZONTAL][diff1] = 1;
48      bins[WEBP_FILTER_VERTICAL][diff2] = 1;
49      bins[WEBP_FILTER_GRADIENT][diff3] = 1;
50      mean = (3 * mean + p[i] + 2) >> 2;
51    }
52  }
53  {
54    int filter;
55    WEBP_FILTER_TYPE best_filter = WEBP_FILTER_NONE;
56    int best_score = 0x7fffffff;
57    for (filter = WEBP_FILTER_NONE; filter < WEBP_FILTER_LAST; ++filter) {
58      int score = 0;
59      for (i = 0; i < SMAX; ++i) {
60        if (bins[filter][i] > 0) {
61          score += i;
62        }
63      }
64      if (score < best_score) {
65        best_score = score;
66        best_filter = (WEBP_FILTER_TYPE)filter;
67      }
68    }
69    return best_filter;
70  }
71}
72
73#undef SMAX
74#undef SDIFF
75
76//------------------------------------------------------------------------------
77