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