Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: third_party/libwebp/utils/filters.c

Issue 116213006: Update libwebp to 0.4.0 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: After Blink Roll Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/libwebp/utils/filters.h ('k') | third_party/libwebp/utils/huffman.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 Google Inc. All Rights Reserved. 1 // Copyright 2011 Google Inc. All Rights Reserved.
2 // 2 //
3 // Use of this source code is governed by a BSD-style license 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 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 5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may 6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree. 7 // be found in the AUTHORS file in the root of the source tree.
8 // ----------------------------------------------------------------------------- 8 // -----------------------------------------------------------------------------
9 // 9 //
10 // Spatial prediction using various filters 10 // Spatial prediction using various filters
11 // 11 //
12 // Author: Urvang (urvang@google.com) 12 // Author: Urvang (urvang@google.com)
13 13
14 #include "./filters.h" 14 #include "./filters.h"
15 #include <assert.h> 15 #include <assert.h>
16 #include <stdlib.h> 16 #include <stdlib.h>
17 #include <string.h> 17 #include <string.h>
18 18
19 #if defined(__cplusplus) || defined(c_plusplus)
20 extern "C" {
21 #endif
22
23 //------------------------------------------------------------------------------ 19 //------------------------------------------------------------------------------
24 // Helpful macro. 20 // Helpful macro.
25 21
26 # define SANITY_CHECK(in, out) \ 22 # define SANITY_CHECK(in, out) \
27 assert(in != NULL); \ 23 assert(in != NULL); \
28 assert(out != NULL); \ 24 assert(out != NULL); \
29 assert(width > 0); \ 25 assert(width > 0); \
30 assert(height > 0); \ 26 assert(height > 0); \
31 assert(stride >= width); 27 assert(stride >= width); \
28 assert(row >= 0 && num_rows > 0 && row + num_rows <= height); \
29 (void)height; // Silence unused warning.
32 30
33 static WEBP_INLINE void PredictLine(const uint8_t* src, const uint8_t* pred, 31 static WEBP_INLINE void PredictLine(const uint8_t* src, const uint8_t* pred,
34 uint8_t* dst, int length, int inverse) { 32 uint8_t* dst, int length, int inverse) {
35 int i; 33 int i;
36 if (inverse) { 34 if (inverse) {
37 for (i = 0; i < length; ++i) dst[i] = src[i] + pred[i]; 35 for (i = 0; i < length; ++i) dst[i] = src[i] + pred[i];
38 } else { 36 } else {
39 for (i = 0; i < length; ++i) dst[i] = src[i] - pred[i]; 37 for (i = 0; i < length; ++i) dst[i] = src[i] - pred[i];
40 } 38 }
41 } 39 }
42 40
43 //------------------------------------------------------------------------------ 41 //------------------------------------------------------------------------------
44 // Horizontal filter. 42 // Horizontal filter.
45 43
46 static WEBP_INLINE void DoHorizontalFilter(const uint8_t* in, 44 static WEBP_INLINE void DoHorizontalFilter(const uint8_t* in,
47 int width, int height, int stride, 45 int width, int height, int stride,
46 int row, int num_rows,
48 int inverse, uint8_t* out) { 47 int inverse, uint8_t* out) {
49 int h; 48 const uint8_t* preds;
50 const uint8_t* preds = (inverse ? out : in); 49 const size_t start_offset = row * stride;
50 const int last_row = row + num_rows;
51 SANITY_CHECK(in, out); 51 SANITY_CHECK(in, out);
52 in += start_offset;
53 out += start_offset;
54 preds = inverse ? out : in;
52 55
53 // Filter line-by-line. 56 if (row == 0) {
54 for (h = 0; h < height; ++h) { 57 // Leftmost pixel is the same as input for topmost scanline.
55 // Leftmost pixel is predicted from above (except for topmost scanline). 58 out[0] = in[0];
56 if (h == 0) {
57 out[0] = in[0];
58 } else {
59 PredictLine(in, preds - stride, out, 1, inverse);
60 }
61 PredictLine(in + 1, preds, out + 1, width - 1, inverse); 59 PredictLine(in + 1, preds, out + 1, width - 1, inverse);
60 row = 1;
62 preds += stride; 61 preds += stride;
63 in += stride; 62 in += stride;
64 out += stride; 63 out += stride;
64 }
65
66 // Filter line-by-line.
67 while (row < last_row) {
68 // Leftmost pixel is predicted from above.
69 PredictLine(in, preds - stride, out, 1, inverse);
70 PredictLine(in + 1, preds, out + 1, width - 1, inverse);
71 ++row;
72 preds += stride;
73 in += stride;
74 out += stride;
65 } 75 }
66 } 76 }
67 77
68 static void HorizontalFilter(const uint8_t* data, int width, int height, 78 static void HorizontalFilter(const uint8_t* data, int width, int height,
69 int stride, uint8_t* filtered_data) { 79 int stride, uint8_t* filtered_data) {
70 DoHorizontalFilter(data, width, height, stride, 0, filtered_data); 80 DoHorizontalFilter(data, width, height, stride, 0, height, 0, filtered_data);
71 } 81 }
72 82
73 static void HorizontalUnfilter(int width, int height, int stride, 83 static void HorizontalUnfilter(int width, int height, int stride, int row,
74 uint8_t* data) { 84 int num_rows, uint8_t* data) {
75 DoHorizontalFilter(data, width, height, stride, 1, data); 85 DoHorizontalFilter(data, width, height, stride, row, num_rows, 1, data);
76 } 86 }
77 87
78 //------------------------------------------------------------------------------ 88 //------------------------------------------------------------------------------
79 // Vertical filter. 89 // Vertical filter.
80 90
81 static WEBP_INLINE void DoVerticalFilter(const uint8_t* in, 91 static WEBP_INLINE void DoVerticalFilter(const uint8_t* in,
82 int width, int height, int stride, 92 int width, int height, int stride,
93 int row, int num_rows,
83 int inverse, uint8_t* out) { 94 int inverse, uint8_t* out) {
84 int h; 95 const uint8_t* preds;
85 const uint8_t* preds = (inverse ? out : in); 96 const size_t start_offset = row * stride;
97 const int last_row = row + num_rows;
86 SANITY_CHECK(in, out); 98 SANITY_CHECK(in, out);
99 in += start_offset;
100 out += start_offset;
101 preds = inverse ? out : in;
87 102
88 // Very first top-left pixel is copied. 103 if (row == 0) {
89 out[0] = in[0]; 104 // Very first top-left pixel is copied.
90 // Rest of top scan-line is left-predicted. 105 out[0] = in[0];
91 PredictLine(in + 1, preds, out + 1, width - 1, inverse); 106 // Rest of top scan-line is left-predicted.
107 PredictLine(in + 1, preds, out + 1, width - 1, inverse);
108 row = 1;
109 in += stride;
110 out += stride;
111 } else {
112 // We are starting from in-between. Make sure 'preds' points to prev row.
113 preds -= stride;
114 }
92 115
93 // Filter line-by-line. 116 // Filter line-by-line.
94 for (h = 1; h < height; ++h) { 117 while (row < last_row) {
118 PredictLine(in, preds, out, width, inverse);
119 ++row;
120 preds += stride;
95 in += stride; 121 in += stride;
96 out += stride; 122 out += stride;
97 PredictLine(in, preds, out, width, inverse);
98 preds += stride;
99 } 123 }
100 } 124 }
101 125
102 static void VerticalFilter(const uint8_t* data, int width, int height, 126 static void VerticalFilter(const uint8_t* data, int width, int height,
103 int stride, uint8_t* filtered_data) { 127 int stride, uint8_t* filtered_data) {
104 DoVerticalFilter(data, width, height, stride, 0, filtered_data); 128 DoVerticalFilter(data, width, height, stride, 0, height, 0, filtered_data);
105 } 129 }
106 130
107 static void VerticalUnfilter(int width, int height, int stride, uint8_t* data) { 131 static void VerticalUnfilter(int width, int height, int stride, int row,
108 DoVerticalFilter(data, width, height, stride, 1, data); 132 int num_rows, uint8_t* data) {
133 DoVerticalFilter(data, width, height, stride, row, num_rows, 1, data);
109 } 134 }
110 135
111 //------------------------------------------------------------------------------ 136 //------------------------------------------------------------------------------
112 // Gradient filter. 137 // Gradient filter.
113 138
114 static WEBP_INLINE int GradientPredictor(uint8_t a, uint8_t b, uint8_t c) { 139 static WEBP_INLINE int GradientPredictor(uint8_t a, uint8_t b, uint8_t c) {
115 const int g = a + b - c; 140 const int g = a + b - c;
116 return ((g & ~0xff) == 0) ? g : (g < 0) ? 0 : 255; // clip to 8bit 141 return ((g & ~0xff) == 0) ? g : (g < 0) ? 0 : 255; // clip to 8bit
117 } 142 }
118 143
119 static WEBP_INLINE 144 static WEBP_INLINE void DoGradientFilter(const uint8_t* in,
120 void DoGradientFilter(const uint8_t* in, int width, int height, 145 int width, int height, int stride,
121 int stride, int inverse, uint8_t* out) { 146 int row, int num_rows,
122 const uint8_t* preds = (inverse ? out : in); 147 int inverse, uint8_t* out) {
123 int h; 148 const uint8_t* preds;
149 const size_t start_offset = row * stride;
150 const int last_row = row + num_rows;
124 SANITY_CHECK(in, out); 151 SANITY_CHECK(in, out);
152 in += start_offset;
153 out += start_offset;
154 preds = inverse ? out : in;
125 155
126 // left prediction for top scan-line 156 // left prediction for top scan-line
127 out[0] = in[0]; 157 if (row == 0) {
128 PredictLine(in + 1, preds, out + 1, width - 1, inverse); 158 out[0] = in[0];
129 159 PredictLine(in + 1, preds, out + 1, width - 1, inverse);
130 // Filter line-by-line. 160 row = 1;
131 for (h = 1; h < height; ++h) {
132 int w;
133 preds += stride; 161 preds += stride;
134 in += stride; 162 in += stride;
135 out += stride; 163 out += stride;
164 }
165
166 // Filter line-by-line.
167 while (row < last_row) {
168 int w;
136 // leftmost pixel: predict from above. 169 // leftmost pixel: predict from above.
137 PredictLine(in, preds - stride, out, 1, inverse); 170 PredictLine(in, preds - stride, out, 1, inverse);
138 for (w = 1; w < width; ++w) { 171 for (w = 1; w < width; ++w) {
139 const int pred = GradientPredictor(preds[w - 1], 172 const int pred = GradientPredictor(preds[w - 1],
140 preds[w - stride], 173 preds[w - stride],
141 preds[w - stride - 1]); 174 preds[w - stride - 1]);
142 out[w] = in[w] + (inverse ? pred : -pred); 175 out[w] = in[w] + (inverse ? pred : -pred);
143 } 176 }
177 ++row;
178 preds += stride;
179 in += stride;
180 out += stride;
144 } 181 }
145 } 182 }
146 183
147 static void GradientFilter(const uint8_t* data, int width, int height, 184 static void GradientFilter(const uint8_t* data, int width, int height,
148 int stride, uint8_t* filtered_data) { 185 int stride, uint8_t* filtered_data) {
149 DoGradientFilter(data, width, height, stride, 0, filtered_data); 186 DoGradientFilter(data, width, height, stride, 0, height, 0, filtered_data);
150 } 187 }
151 188
152 static void GradientUnfilter(int width, int height, int stride, uint8_t* data) { 189 static void GradientUnfilter(int width, int height, int stride, int row,
153 DoGradientFilter(data, width, height, stride, 1, data); 190 int num_rows, uint8_t* data) {
191 DoGradientFilter(data, width, height, stride, row, num_rows, 1, data);
154 } 192 }
155 193
156 #undef SANITY_CHECK 194 #undef SANITY_CHECK
157 195
158 // ----------------------------------------------------------------------------- 196 // -----------------------------------------------------------------------------
159 // Quick estimate of a potentially interesting filter mode to try. 197 // Quick estimate of a potentially interesting filter mode to try.
160 198
161 #define SMAX 16 199 #define SMAX 16
162 #define SDIFF(a, b) (abs((a) - (b)) >> 4) // Scoring diff, in [0..SMAX) 200 #define SDIFF(a, b) (abs((a) - (b)) >> 4) // Scoring diff, in [0..SMAX)
163 201
(...skipping 15 matching lines...) Expand all
179 GradientPredictor(p[i - 1], p[i - width], p[i - width - 1]); 217 GradientPredictor(p[i - 1], p[i - width], p[i - width - 1]);
180 const int diff3 = SDIFF(p[i], grad_pred); 218 const int diff3 = SDIFF(p[i], grad_pred);
181 bins[WEBP_FILTER_NONE][diff0] = 1; 219 bins[WEBP_FILTER_NONE][diff0] = 1;
182 bins[WEBP_FILTER_HORIZONTAL][diff1] = 1; 220 bins[WEBP_FILTER_HORIZONTAL][diff1] = 1;
183 bins[WEBP_FILTER_VERTICAL][diff2] = 1; 221 bins[WEBP_FILTER_VERTICAL][diff2] = 1;
184 bins[WEBP_FILTER_GRADIENT][diff3] = 1; 222 bins[WEBP_FILTER_GRADIENT][diff3] = 1;
185 mean = (3 * mean + p[i] + 2) >> 2; 223 mean = (3 * mean + p[i] + 2) >> 2;
186 } 224 }
187 } 225 }
188 { 226 {
189 WEBP_FILTER_TYPE filter, best_filter = WEBP_FILTER_NONE; 227 int filter;
228 WEBP_FILTER_TYPE best_filter = WEBP_FILTER_NONE;
190 int best_score = 0x7fffffff; 229 int best_score = 0x7fffffff;
191 for (filter = WEBP_FILTER_NONE; filter < WEBP_FILTER_LAST; ++filter) { 230 for (filter = WEBP_FILTER_NONE; filter < WEBP_FILTER_LAST; ++filter) {
192 int score = 0; 231 int score = 0;
193 for (i = 0; i < SMAX; ++i) { 232 for (i = 0; i < SMAX; ++i) {
194 if (bins[filter][i] > 0) { 233 if (bins[filter][i] > 0) {
195 score += i; 234 score += i;
196 } 235 }
197 } 236 }
198 if (score < best_score) { 237 if (score < best_score) {
199 best_score = score; 238 best_score = score;
200 best_filter = filter; 239 best_filter = (WEBP_FILTER_TYPE)filter;
201 } 240 }
202 } 241 }
203 return best_filter; 242 return best_filter;
204 } 243 }
205 } 244 }
206 245
207 #undef SMAX 246 #undef SMAX
208 #undef SDIFF 247 #undef SDIFF
209 248
210 //------------------------------------------------------------------------------ 249 //------------------------------------------------------------------------------
211 250
212 const WebPFilterFunc WebPFilters[WEBP_FILTER_LAST] = { 251 const WebPFilterFunc WebPFilters[WEBP_FILTER_LAST] = {
213 NULL, // WEBP_FILTER_NONE 252 NULL, // WEBP_FILTER_NONE
214 HorizontalFilter, // WEBP_FILTER_HORIZONTAL 253 HorizontalFilter, // WEBP_FILTER_HORIZONTAL
215 VerticalFilter, // WEBP_FILTER_VERTICAL 254 VerticalFilter, // WEBP_FILTER_VERTICAL
216 GradientFilter // WEBP_FILTER_GRADIENT 255 GradientFilter // WEBP_FILTER_GRADIENT
217 }; 256 };
218 257
219 const WebPUnfilterFunc WebPUnfilters[WEBP_FILTER_LAST] = { 258 const WebPUnfilterFunc WebPUnfilters[WEBP_FILTER_LAST] = {
220 NULL, // WEBP_FILTER_NONE 259 NULL, // WEBP_FILTER_NONE
221 HorizontalUnfilter, // WEBP_FILTER_HORIZONTAL 260 HorizontalUnfilter, // WEBP_FILTER_HORIZONTAL
222 VerticalUnfilter, // WEBP_FILTER_VERTICAL 261 VerticalUnfilter, // WEBP_FILTER_VERTICAL
223 GradientUnfilter // WEBP_FILTER_GRADIENT 262 GradientUnfilter // WEBP_FILTER_GRADIENT
224 }; 263 };
225 264
226 //------------------------------------------------------------------------------ 265 //------------------------------------------------------------------------------
227 266
228 #if defined(__cplusplus) || defined(c_plusplus)
229 } // extern "C"
230 #endif
OLDNEW
« no previous file with comments | « third_party/libwebp/utils/filters.h ('k') | third_party/libwebp/utils/huffman.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698