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