/src/libwebp/src/dsp/rescaler.c
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2014 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 | | // Rescaling functions |
11 | | // |
12 | | // Author: Skal (pascal.massimino@gmail.com) |
13 | | |
14 | | #include <assert.h> |
15 | | |
16 | | #include "src/dsp/dsp.h" |
17 | | #include "src/utils/rescaler_utils.h" |
18 | | |
19 | | //------------------------------------------------------------------------------ |
20 | | // Implementations of critical functions ImportRow / ExportRow |
21 | | |
22 | 0 | #define ROUNDER (WEBP_RESCALER_ONE >> 1) |
23 | 0 | #define MULT_FIX(x, y) (((uint64_t)(x) * (y) + ROUNDER) >> WEBP_RESCALER_RFIX) |
24 | 0 | #define MULT_FIX_FLOOR(x, y) (((uint64_t)(x) * (y)) >> WEBP_RESCALER_RFIX) |
25 | | |
26 | | //------------------------------------------------------------------------------ |
27 | | // Row import |
28 | | |
29 | | void WebPRescalerImportRowExpand_C(WebPRescaler* const wrk, |
30 | 0 | const uint8_t* src) { |
31 | 0 | const int x_stride = wrk->num_channels; |
32 | 0 | const int x_out_max = wrk->dst_width * wrk->num_channels; |
33 | 0 | int channel; |
34 | 0 | assert(!WebPRescalerInputDone(wrk)); |
35 | 0 | assert(wrk->x_expand); |
36 | 0 | for (channel = 0; channel < x_stride; ++channel) { |
37 | 0 | int x_in = channel; |
38 | 0 | int x_out = channel; |
39 | | // simple bilinear interpolation |
40 | 0 | int accum = wrk->x_add; |
41 | 0 | rescaler_t left = (rescaler_t)src[x_in]; |
42 | 0 | rescaler_t right = |
43 | 0 | (wrk->src_width > 1) ? (rescaler_t)src[x_in + x_stride] : left; |
44 | 0 | x_in += x_stride; |
45 | 0 | while (1) { |
46 | 0 | wrk->frow[x_out] = right * wrk->x_add + (left - right) * accum; |
47 | 0 | x_out += x_stride; |
48 | 0 | if (x_out >= x_out_max) break; |
49 | 0 | accum -= wrk->x_sub; |
50 | 0 | if (accum < 0) { |
51 | 0 | left = right; |
52 | 0 | x_in += x_stride; |
53 | 0 | assert(x_in < wrk->src_width * x_stride); |
54 | 0 | right = (rescaler_t)src[x_in]; |
55 | 0 | accum += wrk->x_add; |
56 | 0 | } |
57 | 0 | } |
58 | 0 | assert(wrk->x_sub == 0 /* <- special case for src_width=1 */ || accum == 0); |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | | void WebPRescalerImportRowShrink_C(WebPRescaler* const wrk, |
63 | 0 | const uint8_t* src) { |
64 | 0 | const int x_stride = wrk->num_channels; |
65 | 0 | const int x_out_max = wrk->dst_width * wrk->num_channels; |
66 | 0 | int channel; |
67 | 0 | assert(!WebPRescalerInputDone(wrk)); |
68 | 0 | assert(!wrk->x_expand); |
69 | 0 | for (channel = 0; channel < x_stride; ++channel) { |
70 | 0 | int x_in = channel; |
71 | 0 | int x_out = channel; |
72 | 0 | uint32_t sum = 0; |
73 | 0 | int accum = 0; |
74 | 0 | while (x_out < x_out_max) { |
75 | 0 | uint32_t base = 0; |
76 | 0 | accum += wrk->x_add; |
77 | 0 | while (accum > 0) { |
78 | 0 | accum -= wrk->x_sub; |
79 | 0 | assert(x_in < wrk->src_width * x_stride); |
80 | 0 | base = src[x_in]; |
81 | 0 | sum += base; |
82 | 0 | x_in += x_stride; |
83 | 0 | } |
84 | 0 | { // Emit next horizontal pixel. |
85 | 0 | const rescaler_t frac = base * (-accum); |
86 | 0 | wrk->frow[x_out] = sum * wrk->x_sub - frac; |
87 | | // fresh fractional start for next pixel |
88 | 0 | sum = (int)MULT_FIX(frac, wrk->fx_scale); |
89 | 0 | } |
90 | 0 | x_out += x_stride; |
91 | 0 | } |
92 | 0 | assert(accum == 0); |
93 | 0 | } |
94 | 0 | } |
95 | | |
96 | | //------------------------------------------------------------------------------ |
97 | | // Row export |
98 | | |
99 | 0 | void WebPRescalerExportRowExpand_C(WebPRescaler* const wrk) { |
100 | 0 | int x_out; |
101 | 0 | uint8_t* const dst = wrk->dst; |
102 | 0 | rescaler_t* const irow = wrk->irow; |
103 | 0 | const int x_out_max = wrk->dst_width * wrk->num_channels; |
104 | 0 | const rescaler_t* const frow = wrk->frow; |
105 | 0 | assert(!WebPRescalerOutputDone(wrk)); |
106 | 0 | assert(wrk->y_accum <= 0); |
107 | 0 | assert(wrk->y_expand); |
108 | 0 | assert(wrk->y_sub != 0); |
109 | 0 | if (wrk->y_accum == 0) { |
110 | 0 | for (x_out = 0; x_out < x_out_max; ++x_out) { |
111 | 0 | const uint32_t J = frow[x_out]; |
112 | 0 | const int v = (int)MULT_FIX(J, wrk->fy_scale); |
113 | 0 | dst[x_out] = (v > 255) ? 255u : (uint8_t)v; |
114 | 0 | } |
115 | 0 | } else { |
116 | 0 | const uint32_t B = WEBP_RESCALER_FRAC(-wrk->y_accum, wrk->y_sub); |
117 | 0 | const uint32_t A = (uint32_t)(WEBP_RESCALER_ONE - B); |
118 | 0 | for (x_out = 0; x_out < x_out_max; ++x_out) { |
119 | 0 | const uint64_t I = (uint64_t)A * frow[x_out] |
120 | 0 | + (uint64_t)B * irow[x_out]; |
121 | 0 | const uint32_t J = (uint32_t)((I + ROUNDER) >> WEBP_RESCALER_RFIX); |
122 | 0 | const int v = (int)MULT_FIX(J, wrk->fy_scale); |
123 | 0 | dst[x_out] = (v > 255) ? 255u : (uint8_t)v; |
124 | 0 | } |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | 0 | void WebPRescalerExportRowShrink_C(WebPRescaler* const wrk) { |
129 | 0 | int x_out; |
130 | 0 | uint8_t* const dst = wrk->dst; |
131 | 0 | rescaler_t* const irow = wrk->irow; |
132 | 0 | const int x_out_max = wrk->dst_width * wrk->num_channels; |
133 | 0 | const rescaler_t* const frow = wrk->frow; |
134 | 0 | const uint32_t yscale = wrk->fy_scale * (-wrk->y_accum); |
135 | 0 | assert(!WebPRescalerOutputDone(wrk)); |
136 | 0 | assert(wrk->y_accum <= 0); |
137 | 0 | assert(!wrk->y_expand); |
138 | 0 | if (yscale) { |
139 | 0 | for (x_out = 0; x_out < x_out_max; ++x_out) { |
140 | 0 | const uint32_t frac = (uint32_t)MULT_FIX_FLOOR(frow[x_out], yscale); |
141 | 0 | const int v = (int)MULT_FIX(irow[x_out] - frac, wrk->fxy_scale); |
142 | 0 | dst[x_out] = (v > 255) ? 255u : (uint8_t)v; |
143 | 0 | irow[x_out] = frac; // new fractional start |
144 | 0 | } |
145 | 0 | } else { |
146 | 0 | for (x_out = 0; x_out < x_out_max; ++x_out) { |
147 | 0 | const int v = (int)MULT_FIX(irow[x_out], wrk->fxy_scale); |
148 | 0 | dst[x_out] = (v > 255) ? 255u : (uint8_t)v; |
149 | 0 | irow[x_out] = 0; |
150 | 0 | } |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | | #undef MULT_FIX_FLOOR |
155 | | #undef MULT_FIX |
156 | | #undef ROUNDER |
157 | | |
158 | | //------------------------------------------------------------------------------ |
159 | | // Main entry calls |
160 | | |
161 | 0 | void WebPRescalerImportRow(WebPRescaler* const wrk, const uint8_t* src) { |
162 | 0 | assert(!WebPRescalerInputDone(wrk)); |
163 | 0 | if (!wrk->x_expand) { |
164 | 0 | WebPRescalerImportRowShrink(wrk, src); |
165 | 0 | } else { |
166 | 0 | WebPRescalerImportRowExpand(wrk, src); |
167 | 0 | } |
168 | 0 | } |
169 | | |
170 | 0 | void WebPRescalerExportRow(WebPRescaler* const wrk) { |
171 | 0 | if (wrk->y_accum <= 0) { |
172 | 0 | assert(!WebPRescalerOutputDone(wrk)); |
173 | 0 | if (wrk->y_expand) { |
174 | 0 | WebPRescalerExportRowExpand(wrk); |
175 | 0 | } else if (wrk->fxy_scale) { |
176 | 0 | WebPRescalerExportRowShrink(wrk); |
177 | 0 | } else { // special case |
178 | 0 | int i; |
179 | 0 | assert(wrk->src_height == wrk->dst_height && wrk->x_add == 1); |
180 | 0 | assert(wrk->src_width == 1 && wrk->dst_width <= 2); |
181 | 0 | for (i = 0; i < wrk->num_channels * wrk->dst_width; ++i) { |
182 | 0 | wrk->dst[i] = wrk->irow[i]; |
183 | 0 | wrk->irow[i] = 0; |
184 | 0 | } |
185 | 0 | } |
186 | 0 | wrk->y_accum += wrk->y_add; |
187 | 0 | wrk->dst += wrk->dst_stride; |
188 | 0 | ++wrk->dst_y; |
189 | 0 | } |
190 | 0 | } |
191 | | |
192 | | //------------------------------------------------------------------------------ |
193 | | |
194 | | WebPRescalerImportRowFunc WebPRescalerImportRowExpand; |
195 | | WebPRescalerImportRowFunc WebPRescalerImportRowShrink; |
196 | | |
197 | | WebPRescalerExportRowFunc WebPRescalerExportRowExpand; |
198 | | WebPRescalerExportRowFunc WebPRescalerExportRowShrink; |
199 | | |
200 | | extern VP8CPUInfo VP8GetCPUInfo; |
201 | | extern void WebPRescalerDspInitSSE2(void); |
202 | | extern void WebPRescalerDspInitMIPS32(void); |
203 | | extern void WebPRescalerDspInitMIPSdspR2(void); |
204 | | extern void WebPRescalerDspInitMSA(void); |
205 | | extern void WebPRescalerDspInitNEON(void); |
206 | | |
207 | 0 | WEBP_DSP_INIT_FUNC(WebPRescalerDspInit) { |
208 | 0 | #if !defined(WEBP_REDUCE_SIZE) |
209 | 0 | #if !WEBP_NEON_OMIT_C_CODE |
210 | 0 | WebPRescalerExportRowExpand = WebPRescalerExportRowExpand_C; |
211 | 0 | WebPRescalerExportRowShrink = WebPRescalerExportRowShrink_C; |
212 | 0 | #endif |
213 | |
|
214 | 0 | WebPRescalerImportRowExpand = WebPRescalerImportRowExpand_C; |
215 | 0 | WebPRescalerImportRowShrink = WebPRescalerImportRowShrink_C; |
216 | |
|
217 | 0 | if (VP8GetCPUInfo != NULL) { |
218 | 0 | #if defined(WEBP_HAVE_SSE2) |
219 | 0 | if (VP8GetCPUInfo(kSSE2)) { |
220 | 0 | WebPRescalerDspInitSSE2(); |
221 | 0 | } |
222 | 0 | #endif |
223 | | #if defined(WEBP_USE_MIPS32) |
224 | | if (VP8GetCPUInfo(kMIPS32)) { |
225 | | WebPRescalerDspInitMIPS32(); |
226 | | } |
227 | | #endif |
228 | | #if defined(WEBP_USE_MIPS_DSP_R2) |
229 | | if (VP8GetCPUInfo(kMIPSdspR2)) { |
230 | | WebPRescalerDspInitMIPSdspR2(); |
231 | | } |
232 | | #endif |
233 | | #if defined(WEBP_USE_MSA) |
234 | | if (VP8GetCPUInfo(kMSA)) { |
235 | | WebPRescalerDspInitMSA(); |
236 | | } |
237 | | #endif |
238 | 0 | } |
239 | |
|
240 | | #if defined(WEBP_HAVE_NEON) |
241 | | if (WEBP_NEON_OMIT_C_CODE || |
242 | | (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kNEON))) { |
243 | | WebPRescalerDspInitNEON(); |
244 | | } |
245 | | #endif |
246 | |
|
247 | 0 | assert(WebPRescalerExportRowExpand != NULL); |
248 | 0 | assert(WebPRescalerExportRowShrink != NULL); |
249 | 0 | assert(WebPRescalerImportRowExpand != NULL); |
250 | 0 | assert(WebPRescalerImportRowShrink != NULL); |
251 | 0 | #endif // WEBP_REDUCE_SIZE |
252 | 0 | } |