/src/libwebp/src/dec/io_dec.c
Line | Count | Source |
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 | | // functions for sample output. |
11 | | // |
12 | | // Author: Skal (pascal.massimino@gmail.com) |
13 | | |
14 | | #include <assert.h> |
15 | | #include <stddef.h> |
16 | | #include <stdlib.h> |
17 | | #include <string.h> |
18 | | |
19 | | #include "src/dec/vp8_dec.h" |
20 | | #include "src/dec/vp8i_dec.h" |
21 | | #include "src/dec/webpi_dec.h" |
22 | | #include "src/dsp/cpu.h" |
23 | | #include "src/dsp/dsp.h" |
24 | | #include "src/dsp/yuv.h" |
25 | | #include "src/utils/rescaler_utils.h" |
26 | | #include "src/utils/utils.h" |
27 | | #include "src/webp/decode.h" |
28 | | #include "src/webp/types.h" |
29 | | |
30 | | WEBP_ASSUME_UNSAFE_INDEXABLE_ABI |
31 | | |
32 | | //------------------------------------------------------------------------------ |
33 | | // Main YUV<->RGB conversion functions |
34 | | |
35 | 0 | static int EmitYUV(const VP8Io* const io, WebPDecParams* const p) { |
36 | 0 | WebPDecBuffer* output = p->output; |
37 | 0 | const WebPYUVABuffer* const buf = &output->u.YUVA; |
38 | 0 | uint8_t* const y_dst = buf->y + (ptrdiff_t)io->mb_y * buf->y_stride; |
39 | 0 | uint8_t* const u_dst = buf->u + (ptrdiff_t)(io->mb_y >> 1) * buf->u_stride; |
40 | 0 | uint8_t* const v_dst = buf->v + (ptrdiff_t)(io->mb_y >> 1) * buf->v_stride; |
41 | 0 | const int mb_w = io->mb_w; |
42 | 0 | const int mb_h = io->mb_h; |
43 | 0 | const int uv_w = (mb_w + 1) / 2; |
44 | 0 | const int uv_h = (mb_h + 1) / 2; |
45 | 0 | WebPCopyPlane(io->y, io->y_stride, y_dst, buf->y_stride, mb_w, mb_h); |
46 | 0 | WebPCopyPlane(io->u, io->uv_stride, u_dst, buf->u_stride, uv_w, uv_h); |
47 | 0 | WebPCopyPlane(io->v, io->uv_stride, v_dst, buf->v_stride, uv_w, uv_h); |
48 | 0 | return io->mb_h; |
49 | 0 | } |
50 | | |
51 | | // Point-sampling U/V sampler. |
52 | 0 | static int EmitSampledRGB(const VP8Io* const io, WebPDecParams* const p) { |
53 | 0 | WebPDecBuffer* const output = p->output; |
54 | 0 | WebPRGBABuffer* const buf = &output->u.RGBA; |
55 | 0 | uint8_t* const dst = buf->rgba + (ptrdiff_t)io->mb_y * buf->stride; |
56 | 0 | WebPSamplerProcessPlane(io->y, io->y_stride, io->u, io->v, io->uv_stride, dst, |
57 | 0 | buf->stride, io->mb_w, io->mb_h, |
58 | 0 | WebPSamplers[output->colorspace]); |
59 | 0 | return io->mb_h; |
60 | 0 | } |
61 | | |
62 | | //------------------------------------------------------------------------------ |
63 | | // Fancy upsampling |
64 | | |
65 | | #ifdef FANCY_UPSAMPLING |
66 | 29.6k | static int EmitFancyRGB(const VP8Io* const io, WebPDecParams* const p) { |
67 | 29.6k | int num_lines_out = io->mb_h; // a priori guess |
68 | 29.6k | const WebPRGBABuffer* const buf = &p->output->u.RGBA; |
69 | 29.6k | uint8_t* dst = buf->rgba + (ptrdiff_t)io->mb_y * buf->stride; |
70 | 29.6k | WebPUpsampleLinePairFunc upsample = WebPUpsamplers[p->output->colorspace]; |
71 | 29.6k | const uint8_t* cur_y = io->y; |
72 | 29.6k | const uint8_t* cur_u = io->u; |
73 | 29.6k | const uint8_t* cur_v = io->v; |
74 | 29.6k | const uint8_t* top_u = p->tmp_u; |
75 | 29.6k | const uint8_t* top_v = p->tmp_v; |
76 | 29.6k | int y = io->mb_y; |
77 | 29.6k | const int y_end = io->mb_y + io->mb_h; |
78 | 29.6k | const int mb_w = io->mb_w; |
79 | 29.6k | const int uv_w = (mb_w + 1) / 2; |
80 | | |
81 | 29.6k | if (y == 0) { |
82 | | // First line is special cased. We mirror the u/v samples at boundary. |
83 | 1.12k | upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst, NULL, mb_w); |
84 | 28.5k | } else { |
85 | | // We can finish the left-over line from previous call. |
86 | 28.5k | upsample(p->tmp_y, cur_y, top_u, top_v, cur_u, cur_v, dst - buf->stride, |
87 | 28.5k | dst, mb_w); |
88 | 28.5k | ++num_lines_out; |
89 | 28.5k | } |
90 | | // Loop over each output pairs of row. |
91 | 234k | for (; y + 2 < y_end; y += 2) { |
92 | 204k | top_u = cur_u; |
93 | 204k | top_v = cur_v; |
94 | 204k | cur_u += io->uv_stride; |
95 | 204k | cur_v += io->uv_stride; |
96 | 204k | dst += 2 * buf->stride; |
97 | 204k | cur_y += 2 * io->y_stride; |
98 | 204k | upsample(cur_y - io->y_stride, cur_y, top_u, top_v, cur_u, cur_v, |
99 | 204k | dst - buf->stride, dst, mb_w); |
100 | 204k | } |
101 | | // move to last row |
102 | 29.6k | cur_y += io->y_stride; |
103 | 29.6k | if (io->crop_top + y_end < io->crop_bottom) { |
104 | | // Save the unfinished samples for next call (as we're not done yet). |
105 | 29.5k | WEBP_UNSAFE_MEMCPY(p->tmp_y, cur_y, mb_w * sizeof(*p->tmp_y)); |
106 | 29.5k | WEBP_UNSAFE_MEMCPY(p->tmp_u, cur_u, uv_w * sizeof(*p->tmp_u)); |
107 | 29.5k | WEBP_UNSAFE_MEMCPY(p->tmp_v, cur_v, uv_w * sizeof(*p->tmp_v)); |
108 | | // The fancy upsampler leaves a row unfinished behind |
109 | | // (except for the very last row) |
110 | 29.5k | num_lines_out--; |
111 | 29.5k | } else { |
112 | | // Process the very last row of even-sized picture |
113 | 132 | if (!(y_end & 1)) { |
114 | 58 | upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst + buf->stride, NULL, |
115 | 58 | mb_w); |
116 | 58 | } |
117 | 132 | } |
118 | 29.6k | return num_lines_out; |
119 | 29.6k | } |
120 | | |
121 | | #endif /* FANCY_UPSAMPLING */ |
122 | | |
123 | | //------------------------------------------------------------------------------ |
124 | | |
125 | 0 | static void FillAlphaPlane(uint8_t* dst, int w, int h, int stride) { |
126 | 0 | int j; |
127 | 0 | for (j = 0; j < h; ++j) { |
128 | 0 | WEBP_UNSAFE_MEMSET(dst, 0xff, w * sizeof(*dst)); |
129 | 0 | dst += stride; |
130 | 0 | } |
131 | 0 | } |
132 | | |
133 | | static int EmitAlphaYUV(const VP8Io* const io, WebPDecParams* const p, |
134 | 0 | int expected_num_lines_out) { |
135 | 0 | const uint8_t* alpha = io->a; |
136 | 0 | const WebPYUVABuffer* const buf = &p->output->u.YUVA; |
137 | 0 | const int mb_w = io->mb_w; |
138 | 0 | const int mb_h = io->mb_h; |
139 | 0 | uint8_t* dst = buf->a + (ptrdiff_t)io->mb_y * buf->a_stride; |
140 | 0 | int j; |
141 | 0 | (void)expected_num_lines_out; |
142 | 0 | assert(expected_num_lines_out == mb_h); |
143 | 0 | if (alpha != NULL) { |
144 | 0 | for (j = 0; j < mb_h; ++j) { |
145 | 0 | WEBP_UNSAFE_MEMCPY(dst, alpha, mb_w * sizeof(*dst)); |
146 | 0 | alpha += io->width; |
147 | 0 | dst += buf->a_stride; |
148 | 0 | } |
149 | 0 | } else if (buf->a != NULL) { |
150 | | // the user requested alpha, but there is none, set it to opaque. |
151 | 0 | FillAlphaPlane(dst, mb_w, mb_h, buf->a_stride); |
152 | 0 | } |
153 | 0 | return 0; |
154 | 0 | } |
155 | | |
156 | | static int GetAlphaSourceRow(const VP8Io* const io, const uint8_t** alpha, |
157 | 0 | int* const num_rows) { |
158 | 0 | int start_y = io->mb_y; |
159 | 0 | *num_rows = io->mb_h; |
160 | | |
161 | | // Compensate for the 1-line delay of the fancy upscaler. |
162 | | // This is similar to EmitFancyRGB(). |
163 | 0 | if (io->fancy_upsampling) { |
164 | 0 | if (start_y == 0) { |
165 | | // We don't process the last row yet. It'll be done during the next call. |
166 | 0 | --*num_rows; |
167 | 0 | } else { |
168 | 0 | --start_y; |
169 | | // Fortunately, *alpha data is persistent, so we can go back |
170 | | // one row and finish alpha blending, now that the fancy upscaler |
171 | | // completed the YUV->RGB interpolation. |
172 | 0 | *alpha -= io->width; |
173 | 0 | } |
174 | 0 | if (io->crop_top + io->mb_y + io->mb_h == io->crop_bottom) { |
175 | | // If it's the very last call, we process all the remaining rows! |
176 | 0 | *num_rows = io->crop_bottom - io->crop_top - start_y; |
177 | 0 | } |
178 | 0 | } |
179 | 0 | return start_y; |
180 | 0 | } |
181 | | |
182 | | static int EmitAlphaRGB(const VP8Io* const io, WebPDecParams* const p, |
183 | 29.6k | int expected_num_lines_out) { |
184 | 29.6k | const uint8_t* alpha = io->a; |
185 | 29.6k | if (alpha != NULL) { |
186 | 0 | const int mb_w = io->mb_w; |
187 | 0 | const WEBP_CSP_MODE colorspace = p->output->colorspace; |
188 | 0 | const int alpha_first = |
189 | 0 | (colorspace == MODE_ARGB || colorspace == MODE_Argb); |
190 | 0 | const WebPRGBABuffer* const buf = &p->output->u.RGBA; |
191 | 0 | int num_rows; |
192 | 0 | const int start_y = GetAlphaSourceRow(io, &alpha, &num_rows); |
193 | 0 | uint8_t* const base_rgba = buf->rgba + (ptrdiff_t)start_y * buf->stride; |
194 | 0 | uint8_t* const dst = base_rgba + (alpha_first ? 0 : 3); |
195 | 0 | const int has_alpha = |
196 | 0 | WebPDispatchAlpha(alpha, io->width, mb_w, num_rows, dst, buf->stride); |
197 | 0 | (void)expected_num_lines_out; |
198 | 0 | assert(expected_num_lines_out == num_rows); |
199 | | // has_alpha is true if there's non-trivial alpha to premultiply with. |
200 | 0 | if (has_alpha && WebPIsPremultipliedMode(colorspace)) { |
201 | 0 | WebPApplyAlphaMultiply(base_rgba, alpha_first, mb_w, num_rows, |
202 | 0 | buf->stride); |
203 | 0 | } |
204 | 0 | } |
205 | 29.6k | return 0; |
206 | 29.6k | } |
207 | | |
208 | | static int EmitAlphaRGBA4444(const VP8Io* const io, WebPDecParams* const p, |
209 | 0 | int expected_num_lines_out) { |
210 | 0 | const uint8_t* alpha = io->a; |
211 | 0 | if (alpha != NULL) { |
212 | 0 | const int mb_w = io->mb_w; |
213 | 0 | const WEBP_CSP_MODE colorspace = p->output->colorspace; |
214 | 0 | const WebPRGBABuffer* const buf = &p->output->u.RGBA; |
215 | 0 | int num_rows; |
216 | 0 | const int start_y = GetAlphaSourceRow(io, &alpha, &num_rows); |
217 | 0 | uint8_t* const base_rgba = buf->rgba + (ptrdiff_t)start_y * buf->stride; |
218 | | #if (WEBP_SWAP_16BIT_CSP == 1) |
219 | | uint8_t* alpha_dst = base_rgba; |
220 | | #else |
221 | 0 | uint8_t* alpha_dst = base_rgba + 1; |
222 | 0 | #endif |
223 | 0 | uint32_t alpha_mask = 0x0f; |
224 | 0 | int i, j; |
225 | 0 | for (j = 0; j < num_rows; ++j) { |
226 | 0 | for (i = 0; i < mb_w; ++i) { |
227 | | // Fill in the alpha value (converted to 4 bits). |
228 | 0 | const uint32_t alpha_value = alpha[i] >> 4; |
229 | 0 | alpha_dst[2 * i] = (alpha_dst[2 * i] & 0xf0) | alpha_value; |
230 | 0 | alpha_mask &= alpha_value; |
231 | 0 | } |
232 | 0 | alpha += io->width; |
233 | 0 | alpha_dst += buf->stride; |
234 | 0 | } |
235 | 0 | (void)expected_num_lines_out; |
236 | 0 | assert(expected_num_lines_out == num_rows); |
237 | 0 | if (alpha_mask != 0x0f && WebPIsPremultipliedMode(colorspace)) { |
238 | 0 | WebPApplyAlphaMultiply4444(base_rgba, mb_w, num_rows, buf->stride); |
239 | 0 | } |
240 | 0 | } |
241 | 0 | return 0; |
242 | 0 | } |
243 | | |
244 | | //------------------------------------------------------------------------------ |
245 | | // YUV rescaling (no final RGB conversion needed) |
246 | | |
247 | | #if !defined(WEBP_REDUCE_SIZE) |
248 | | static int Rescale(const uint8_t* src, int src_stride, int new_lines, |
249 | 0 | WebPRescaler* const wrk) { |
250 | 0 | int num_lines_out = 0; |
251 | 0 | while (new_lines > 0) { // import new contributions of source rows. |
252 | 0 | const int lines_in = WebPRescalerImport(wrk, new_lines, src, src_stride); |
253 | 0 | src += lines_in * src_stride; |
254 | 0 | new_lines -= lines_in; |
255 | 0 | num_lines_out += WebPRescalerExport(wrk); // emit output row(s) |
256 | 0 | } |
257 | 0 | return num_lines_out; |
258 | 0 | } |
259 | | |
260 | 0 | static int EmitRescaledYUV(const VP8Io* const io, WebPDecParams* const p) { |
261 | 0 | const int mb_h = io->mb_h; |
262 | 0 | const int uv_mb_h = (mb_h + 1) >> 1; |
263 | 0 | WebPRescaler* const scaler = p->scaler_y; |
264 | 0 | int num_lines_out = 0; |
265 | 0 | if (WebPIsAlphaMode(p->output->colorspace) && io->a != NULL) { |
266 | | // Before rescaling, we premultiply the luma directly into the io->y |
267 | | // internal buffer. This is OK since these samples are not used for |
268 | | // intra-prediction (the top samples are saved in cache_y/u/v). |
269 | | // But we need to cast the const away, though. |
270 | 0 | WebPMultRows((uint8_t*)io->y, io->y_stride, io->a, io->width, io->mb_w, |
271 | 0 | mb_h, 0); |
272 | 0 | } |
273 | 0 | num_lines_out = Rescale(io->y, io->y_stride, mb_h, scaler); |
274 | 0 | Rescale(io->u, io->uv_stride, uv_mb_h, p->scaler_u); |
275 | 0 | Rescale(io->v, io->uv_stride, uv_mb_h, p->scaler_v); |
276 | 0 | return num_lines_out; |
277 | 0 | } |
278 | | |
279 | | static int EmitRescaledAlphaYUV(const VP8Io* const io, WebPDecParams* const p, |
280 | 0 | int expected_num_lines_out) { |
281 | 0 | const WebPYUVABuffer* const buf = &p->output->u.YUVA; |
282 | 0 | uint8_t* const dst_a = buf->a + (ptrdiff_t)p->last_y * buf->a_stride; |
283 | 0 | if (io->a != NULL) { |
284 | 0 | uint8_t* const dst_y = buf->y + (ptrdiff_t)p->last_y * buf->y_stride; |
285 | 0 | const int num_lines_out = Rescale(io->a, io->width, io->mb_h, p->scaler_a); |
286 | 0 | assert(expected_num_lines_out == num_lines_out); |
287 | 0 | if (num_lines_out > 0) { // unmultiply the Y |
288 | 0 | WebPMultRows(dst_y, buf->y_stride, dst_a, buf->a_stride, |
289 | 0 | p->scaler_a->dst_width, num_lines_out, 1); |
290 | 0 | } |
291 | 0 | } else if (buf->a != NULL) { |
292 | | // the user requested alpha, but there is none, set it to opaque. |
293 | 0 | assert(p->last_y + expected_num_lines_out <= io->scaled_height); |
294 | 0 | FillAlphaPlane(dst_a, io->scaled_width, expected_num_lines_out, |
295 | 0 | buf->a_stride); |
296 | 0 | } |
297 | 0 | return 0; |
298 | 0 | } |
299 | | |
300 | 0 | static int InitYUVRescaler(const VP8Io* const io, WebPDecParams* const p) { |
301 | 0 | const int has_alpha = WebPIsAlphaMode(p->output->colorspace); |
302 | 0 | const WebPYUVABuffer* const buf = &p->output->u.YUVA; |
303 | 0 | const int out_width = io->scaled_width; |
304 | 0 | const int out_height = io->scaled_height; |
305 | 0 | const int uv_out_width = (out_width + 1) >> 1; |
306 | 0 | const int uv_out_height = (out_height + 1) >> 1; |
307 | 0 | const int uv_in_width = (io->mb_w + 1) >> 1; |
308 | 0 | const int uv_in_height = (io->mb_h + 1) >> 1; |
309 | | // scratch memory for luma rescaler |
310 | 0 | const size_t work_size = 2 * (size_t)out_width; |
311 | 0 | const size_t uv_work_size = 2 * uv_out_width; // and for each u/v ones |
312 | 0 | uint64_t total_size; |
313 | 0 | size_t rescaler_size; |
314 | 0 | rescaler_t* WEBP_BIDI_INDEXABLE work; |
315 | 0 | WebPRescaler* scalers; |
316 | 0 | const int num_rescalers = has_alpha ? 4 : 3; |
317 | |
|
318 | 0 | total_size = ((uint64_t)work_size + 2 * uv_work_size) * sizeof(*work); |
319 | 0 | if (has_alpha) { |
320 | 0 | total_size += (uint64_t)work_size * sizeof(*work); |
321 | 0 | } |
322 | 0 | rescaler_size = num_rescalers * sizeof(*p->scaler_y) + WEBP_ALIGN_CST; |
323 | 0 | total_size += rescaler_size; |
324 | 0 | if (!CheckSizeOverflow(total_size)) { |
325 | 0 | return 0; |
326 | 0 | } |
327 | | |
328 | 0 | work = (rescaler_t*)WebPSafeMalloc(1ULL, (size_t)total_size); |
329 | 0 | if (work == NULL) { |
330 | 0 | return 0; // memory error |
331 | 0 | } |
332 | 0 | p->memory = work; |
333 | |
|
334 | 0 | scalers = (WebPRescaler*)WEBP_ALIGN((const uint8_t*)work + total_size - |
335 | 0 | rescaler_size); |
336 | 0 | p->scaler_y = &scalers[0]; |
337 | 0 | p->scaler_u = &scalers[1]; |
338 | 0 | p->scaler_v = &scalers[2]; |
339 | 0 | p->scaler_a = has_alpha ? &scalers[3] : NULL; |
340 | |
|
341 | 0 | if (!WebPRescalerInit(p->scaler_y, io->mb_w, io->mb_h, buf->y, out_width, |
342 | 0 | out_height, buf->y_stride, 1, work) || |
343 | 0 | !WebPRescalerInit(p->scaler_u, uv_in_width, uv_in_height, buf->u, |
344 | 0 | uv_out_width, uv_out_height, buf->u_stride, 1, |
345 | 0 | work + work_size) || |
346 | 0 | !WebPRescalerInit(p->scaler_v, uv_in_width, uv_in_height, buf->v, |
347 | 0 | uv_out_width, uv_out_height, buf->v_stride, 1, |
348 | 0 | work + work_size + uv_work_size)) { |
349 | 0 | return 0; |
350 | 0 | } |
351 | 0 | p->emit = EmitRescaledYUV; |
352 | |
|
353 | 0 | if (has_alpha) { |
354 | 0 | if (!WebPRescalerInit(p->scaler_a, io->mb_w, io->mb_h, buf->a, out_width, |
355 | 0 | out_height, buf->a_stride, 1, |
356 | 0 | work + work_size + 2 * uv_work_size)) { |
357 | 0 | return 0; |
358 | 0 | } |
359 | 0 | p->emit_alpha = EmitRescaledAlphaYUV; |
360 | 0 | WebPInitAlphaProcessing(); |
361 | 0 | } |
362 | 0 | return 1; |
363 | 0 | } |
364 | | |
365 | | //------------------------------------------------------------------------------ |
366 | | // RGBA rescaling |
367 | | |
368 | 0 | static int ExportRGB(WebPDecParams* const p, int y_pos) { |
369 | 0 | const WebPYUV444Converter convert = |
370 | 0 | WebPYUV444Converters[p->output->colorspace]; |
371 | 0 | const WebPRGBABuffer* const buf = &p->output->u.RGBA; |
372 | 0 | uint8_t* dst = buf->rgba + (ptrdiff_t)y_pos * buf->stride; |
373 | 0 | int num_lines_out = 0; |
374 | | // For RGB rescaling, because of the YUV420, current scan position |
375 | | // U/V can be +1/-1 line from the Y one. Hence the double test. |
376 | 0 | while (WebPRescalerHasPendingOutput(p->scaler_y) && |
377 | 0 | WebPRescalerHasPendingOutput(p->scaler_u)) { |
378 | 0 | assert(y_pos + num_lines_out < p->output->height); |
379 | 0 | assert(p->scaler_u->y_accum == p->scaler_v->y_accum); |
380 | 0 | WebPRescalerExportRow(p->scaler_y); |
381 | 0 | WebPRescalerExportRow(p->scaler_u); |
382 | 0 | WebPRescalerExportRow(p->scaler_v); |
383 | 0 | convert(p->scaler_y->dst, p->scaler_u->dst, p->scaler_v->dst, dst, |
384 | 0 | p->scaler_y->dst_width); |
385 | 0 | dst += buf->stride; |
386 | 0 | ++num_lines_out; |
387 | 0 | } |
388 | 0 | return num_lines_out; |
389 | 0 | } |
390 | | |
391 | 0 | static int EmitRescaledRGB(const VP8Io* const io, WebPDecParams* const p) { |
392 | 0 | const int mb_h = io->mb_h; |
393 | 0 | const int uv_mb_h = (mb_h + 1) >> 1; |
394 | 0 | int j = 0, uv_j = 0; |
395 | 0 | int num_lines_out = 0; |
396 | 0 | while (j < mb_h) { |
397 | 0 | const int y_lines_in = |
398 | 0 | WebPRescalerImport(p->scaler_y, mb_h - j, |
399 | 0 | io->y + (ptrdiff_t)j * io->y_stride, io->y_stride); |
400 | 0 | j += y_lines_in; |
401 | 0 | if (WebPRescaleNeededLines(p->scaler_u, uv_mb_h - uv_j)) { |
402 | 0 | const int u_lines_in = WebPRescalerImport( |
403 | 0 | p->scaler_u, uv_mb_h - uv_j, io->u + (ptrdiff_t)uv_j * io->uv_stride, |
404 | 0 | io->uv_stride); |
405 | 0 | const int v_lines_in = WebPRescalerImport( |
406 | 0 | p->scaler_v, uv_mb_h - uv_j, io->v + (ptrdiff_t)uv_j * io->uv_stride, |
407 | 0 | io->uv_stride); |
408 | 0 | (void)v_lines_in; // remove a gcc warning |
409 | 0 | assert(u_lines_in == v_lines_in); |
410 | 0 | uv_j += u_lines_in; |
411 | 0 | } |
412 | 0 | num_lines_out += ExportRGB(p, p->last_y + num_lines_out); |
413 | 0 | } |
414 | 0 | return num_lines_out; |
415 | 0 | } |
416 | | |
417 | 0 | static int ExportAlpha(WebPDecParams* const p, int y_pos, int max_lines_out) { |
418 | 0 | const WebPRGBABuffer* const buf = &p->output->u.RGBA; |
419 | 0 | uint8_t* const base_rgba = buf->rgba + (ptrdiff_t)y_pos * buf->stride; |
420 | 0 | const WEBP_CSP_MODE colorspace = p->output->colorspace; |
421 | 0 | const int alpha_first = (colorspace == MODE_ARGB || colorspace == MODE_Argb); |
422 | 0 | uint8_t* dst = base_rgba + (alpha_first ? 0 : 3); |
423 | 0 | int num_lines_out = 0; |
424 | 0 | const int is_premult_alpha = WebPIsPremultipliedMode(colorspace); |
425 | 0 | uint32_t non_opaque = 0; |
426 | 0 | const int width = p->scaler_a->dst_width; |
427 | |
|
428 | 0 | while (WebPRescalerHasPendingOutput(p->scaler_a) && |
429 | 0 | num_lines_out < max_lines_out) { |
430 | 0 | assert(y_pos + num_lines_out < p->output->height); |
431 | 0 | WebPRescalerExportRow(p->scaler_a); |
432 | 0 | non_opaque |= WebPDispatchAlpha(p->scaler_a->dst, 0, width, 1, dst, 0); |
433 | 0 | dst += buf->stride; |
434 | 0 | ++num_lines_out; |
435 | 0 | } |
436 | 0 | if (is_premult_alpha && non_opaque) { |
437 | 0 | WebPApplyAlphaMultiply(base_rgba, alpha_first, width, num_lines_out, |
438 | 0 | buf->stride); |
439 | 0 | } |
440 | 0 | return num_lines_out; |
441 | 0 | } |
442 | | |
443 | | static int ExportAlphaRGBA4444(WebPDecParams* const p, int y_pos, |
444 | 0 | int max_lines_out) { |
445 | 0 | const WebPRGBABuffer* const buf = &p->output->u.RGBA; |
446 | 0 | uint8_t* const base_rgba = buf->rgba + (ptrdiff_t)y_pos * buf->stride; |
447 | | #if (WEBP_SWAP_16BIT_CSP == 1) |
448 | | uint8_t* alpha_dst = base_rgba; |
449 | | #else |
450 | 0 | uint8_t* alpha_dst = base_rgba + 1; |
451 | 0 | #endif |
452 | 0 | int num_lines_out = 0; |
453 | 0 | const WEBP_CSP_MODE colorspace = p->output->colorspace; |
454 | 0 | const int width = p->scaler_a->dst_width; |
455 | 0 | const int is_premult_alpha = WebPIsPremultipliedMode(colorspace); |
456 | 0 | uint32_t alpha_mask = 0x0f; |
457 | |
|
458 | 0 | while (WebPRescalerHasPendingOutput(p->scaler_a) && |
459 | 0 | num_lines_out < max_lines_out) { |
460 | 0 | int i; |
461 | 0 | assert(y_pos + num_lines_out < p->output->height); |
462 | 0 | WebPRescalerExportRow(p->scaler_a); |
463 | 0 | for (i = 0; i < width; ++i) { |
464 | | // Fill in the alpha value (converted to 4 bits). |
465 | 0 | const uint32_t alpha_value = p->scaler_a->dst[i] >> 4; |
466 | 0 | alpha_dst[2 * i] = (alpha_dst[2 * i] & 0xf0) | alpha_value; |
467 | 0 | alpha_mask &= alpha_value; |
468 | 0 | } |
469 | 0 | alpha_dst += buf->stride; |
470 | 0 | ++num_lines_out; |
471 | 0 | } |
472 | 0 | if (is_premult_alpha && alpha_mask != 0x0f) { |
473 | 0 | WebPApplyAlphaMultiply4444(base_rgba, width, num_lines_out, buf->stride); |
474 | 0 | } |
475 | 0 | return num_lines_out; |
476 | 0 | } |
477 | | |
478 | | static int EmitRescaledAlphaRGB(const VP8Io* const io, WebPDecParams* const p, |
479 | 0 | int expected_num_out_lines) { |
480 | 0 | if (io->a != NULL) { |
481 | 0 | WebPRescaler* const scaler = p->scaler_a; |
482 | 0 | int lines_left = expected_num_out_lines; |
483 | 0 | const int y_end = p->last_y + lines_left; |
484 | 0 | while (lines_left > 0) { |
485 | 0 | const int64_t row_offset = (ptrdiff_t)scaler->src_y - io->mb_y; |
486 | 0 | WebPRescalerImport(scaler, io->mb_h + io->mb_y - scaler->src_y, |
487 | 0 | io->a + row_offset * io->width, io->width); |
488 | 0 | lines_left -= p->emit_alpha_row(p, y_end - lines_left, lines_left); |
489 | 0 | } |
490 | 0 | } |
491 | 0 | return 0; |
492 | 0 | } |
493 | | |
494 | 0 | static int InitRGBRescaler(const VP8Io* const io, WebPDecParams* const p) { |
495 | 0 | const int has_alpha = WebPIsAlphaMode(p->output->colorspace); |
496 | 0 | const int out_width = io->scaled_width; |
497 | 0 | const int out_height = io->scaled_height; |
498 | 0 | const int uv_in_width = (io->mb_w + 1) >> 1; |
499 | 0 | const int uv_in_height = (io->mb_h + 1) >> 1; |
500 | | // scratch memory for one rescaler |
501 | 0 | const size_t work_size = 2 * (size_t)out_width; |
502 | 0 | rescaler_t* WEBP_BIDI_INDEXABLE work; // rescalers work area |
503 | 0 | uint8_t* WEBP_BIDI_INDEXABLE |
504 | 0 | tmp; // tmp storage for scaled YUV444 samples before RGB conversion |
505 | 0 | uint64_t tmp_size1, tmp_size2, total_size; |
506 | 0 | size_t rescaler_size; |
507 | 0 | WebPRescaler* scalers; |
508 | 0 | const int num_rescalers = has_alpha ? 4 : 3; |
509 | |
|
510 | 0 | tmp_size1 = (uint64_t)num_rescalers * work_size; |
511 | 0 | tmp_size2 = (uint64_t)num_rescalers * out_width; |
512 | 0 | total_size = tmp_size1 * sizeof(*work) + tmp_size2 * sizeof(*tmp); |
513 | 0 | rescaler_size = num_rescalers * sizeof(*p->scaler_y) + WEBP_ALIGN_CST; |
514 | 0 | total_size += rescaler_size; |
515 | 0 | if (!CheckSizeOverflow(total_size)) { |
516 | 0 | return 0; |
517 | 0 | } |
518 | | |
519 | 0 | work = (rescaler_t*)WebPSafeMalloc(1ULL, (size_t)total_size); |
520 | 0 | if (work == NULL) { |
521 | 0 | return 0; // memory error |
522 | 0 | } |
523 | 0 | p->memory = work; |
524 | 0 | tmp = (uint8_t*)(work + tmp_size1); |
525 | |
|
526 | 0 | scalers = (WebPRescaler*)WEBP_ALIGN((const uint8_t*)work + total_size - |
527 | 0 | rescaler_size); |
528 | 0 | p->scaler_y = &scalers[0]; |
529 | 0 | p->scaler_u = &scalers[1]; |
530 | 0 | p->scaler_v = &scalers[2]; |
531 | 0 | p->scaler_a = has_alpha ? &scalers[3] : NULL; |
532 | |
|
533 | 0 | if (!WebPRescalerInit(p->scaler_y, io->mb_w, io->mb_h, tmp + 0 * out_width, |
534 | 0 | out_width, out_height, 0, 1, work + 0 * work_size) || |
535 | 0 | !WebPRescalerInit(p->scaler_u, uv_in_width, uv_in_height, |
536 | 0 | tmp + 1 * out_width, out_width, out_height, 0, 1, |
537 | 0 | work + 1 * work_size) || |
538 | 0 | !WebPRescalerInit(p->scaler_v, uv_in_width, uv_in_height, |
539 | 0 | tmp + 2 * out_width, out_width, out_height, 0, 1, |
540 | 0 | work + 2 * work_size)) { |
541 | 0 | return 0; |
542 | 0 | } |
543 | 0 | p->emit = EmitRescaledRGB; |
544 | 0 | WebPInitYUV444Converters(); |
545 | |
|
546 | 0 | if (has_alpha) { |
547 | 0 | if (!WebPRescalerInit(p->scaler_a, io->mb_w, io->mb_h, tmp + 3 * out_width, |
548 | 0 | out_width, out_height, 0, 1, work + 3 * work_size)) { |
549 | 0 | return 0; |
550 | 0 | } |
551 | 0 | p->emit_alpha = EmitRescaledAlphaRGB; |
552 | 0 | if (p->output->colorspace == MODE_RGBA_4444 || |
553 | 0 | p->output->colorspace == MODE_rgbA_4444) { |
554 | 0 | p->emit_alpha_row = ExportAlphaRGBA4444; |
555 | 0 | } else { |
556 | 0 | p->emit_alpha_row = ExportAlpha; |
557 | 0 | } |
558 | 0 | WebPInitAlphaProcessing(); |
559 | 0 | } |
560 | 0 | return 1; |
561 | 0 | } |
562 | | |
563 | | #endif // WEBP_REDUCE_SIZE |
564 | | |
565 | | //------------------------------------------------------------------------------ |
566 | | // Default custom functions |
567 | | |
568 | 1.71k | static int CustomSetup(VP8Io* io) { |
569 | 1.71k | WebPDecParams* const p = (WebPDecParams*)io->opaque; |
570 | 1.71k | const WEBP_CSP_MODE colorspace = p->output->colorspace; |
571 | 1.71k | const int is_rgb = WebPIsRGBMode(colorspace); |
572 | 1.71k | const int is_alpha = WebPIsAlphaMode(colorspace); |
573 | | |
574 | 1.71k | p->memory = NULL; |
575 | 1.71k | p->emit = NULL; |
576 | 1.71k | p->emit_alpha = NULL; |
577 | 1.71k | p->emit_alpha_row = NULL; |
578 | | // Note: WebPIoInitFromOptions() does not distinguish between MODE_YUV and |
579 | | // MODE_YUVA, only RGB vs YUV. |
580 | 1.71k | if (!WebPIoInitFromOptions(p->options, io, /*src_colorspace=*/MODE_YUV)) { |
581 | 0 | return 0; |
582 | 0 | } |
583 | 1.71k | if (is_alpha && WebPIsPremultipliedMode(colorspace)) { |
584 | 0 | WebPInitUpsamplers(); |
585 | 0 | } |
586 | 1.71k | if (io->use_scaling) { |
587 | 0 | #if !defined(WEBP_REDUCE_SIZE) |
588 | 0 | const int ok = is_rgb ? InitRGBRescaler(io, p) : InitYUVRescaler(io, p); |
589 | 0 | if (!ok) { |
590 | 0 | return 0; // memory error |
591 | 0 | } |
592 | | #else |
593 | | return 0; // rescaling support not compiled |
594 | | #endif |
595 | 1.71k | } else { |
596 | 1.71k | if (is_rgb) { |
597 | 1.71k | WebPInitSamplers(); |
598 | 1.71k | p->emit = EmitSampledRGB; // default |
599 | 1.71k | if (io->fancy_upsampling) { |
600 | 1.71k | #ifdef FANCY_UPSAMPLING |
601 | 1.71k | const int uv_width = (io->mb_w + 1) >> 1; |
602 | 1.71k | p->memory = WebPSafeMalloc(1ULL, (size_t)(io->mb_w + 2 * uv_width)); |
603 | 1.71k | if (p->memory == NULL) { |
604 | 0 | return 0; // memory error. |
605 | 0 | } |
606 | 1.71k | p->tmp_y = (uint8_t*)p->memory; |
607 | 1.71k | p->tmp_u = p->tmp_y + io->mb_w; |
608 | 1.71k | p->tmp_v = p->tmp_u + uv_width; |
609 | 1.71k | p->emit = EmitFancyRGB; |
610 | 1.71k | WebPInitUpsamplers(); |
611 | 1.71k | #endif |
612 | 1.71k | } |
613 | 1.71k | } else { |
614 | 0 | p->emit = EmitYUV; |
615 | 0 | } |
616 | 1.71k | if (is_alpha) { // need transparency output |
617 | 1.71k | p->emit_alpha = |
618 | 1.71k | (colorspace == MODE_RGBA_4444 || colorspace == MODE_rgbA_4444) |
619 | 1.71k | ? EmitAlphaRGBA4444 |
620 | 1.71k | : is_rgb ? EmitAlphaRGB |
621 | 1.71k | : EmitAlphaYUV; |
622 | 1.71k | if (is_rgb) { |
623 | 1.71k | WebPInitAlphaProcessing(); |
624 | 1.71k | } |
625 | 1.71k | } |
626 | 1.71k | } |
627 | | |
628 | 1.71k | return 1; |
629 | 1.71k | } |
630 | | |
631 | | //------------------------------------------------------------------------------ |
632 | | |
633 | 29.6k | static int CustomPut(const VP8Io* io) { |
634 | 29.6k | WebPDecParams* const p = (WebPDecParams*)io->opaque; |
635 | 29.6k | const int mb_w = io->mb_w; |
636 | 29.6k | const int mb_h = io->mb_h; |
637 | 29.6k | int num_lines_out; |
638 | 29.6k | assert(!(io->mb_y & 1)); |
639 | | |
640 | 29.6k | if (mb_w <= 0 || mb_h <= 0) { |
641 | 0 | return 0; |
642 | 0 | } |
643 | 29.6k | num_lines_out = p->emit(io, p); |
644 | 29.6k | if (p->emit_alpha != NULL) { |
645 | 29.6k | p->emit_alpha(io, p, num_lines_out); |
646 | 29.6k | } |
647 | 29.6k | p->last_y += num_lines_out; |
648 | 29.6k | return 1; |
649 | 29.6k | } |
650 | | |
651 | | //------------------------------------------------------------------------------ |
652 | | |
653 | 1.71k | static void CustomTeardown(const VP8Io* io) { |
654 | 1.71k | WebPDecParams* const p = (WebPDecParams*)io->opaque; |
655 | 1.71k | WebPSafeFree(p->memory); |
656 | 1.71k | p->memory = NULL; |
657 | 1.71k | } |
658 | | |
659 | | //------------------------------------------------------------------------------ |
660 | | // Main entry point |
661 | | |
662 | 3.58k | void WebPInitCustomIo(WebPDecParams* const params, VP8Io* const io) { |
663 | 3.58k | io->put = CustomPut; |
664 | 3.58k | io->setup = CustomSetup; |
665 | 3.58k | io->teardown = CustomTeardown; |
666 | 3.58k | io->opaque = params; |
667 | 3.58k | } |
668 | | |
669 | | //------------------------------------------------------------------------------ |