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 "src/dec/vp8i_dec.h"
17 #include "src/dec/webpi_dec.h"
18 #include "src/dsp/dsp.h"
19 #include "src/dsp/yuv.h"
20 #include "src/utils/utils.h"
21 
22 //------------------------------------------------------------------------------
23 // Main YUV<->RGB conversion functions
24 
EmitYUV(const VP8Io * const io,WebPDecParams * const p)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.
EmitSampledRGB(const VP8Io * const io,WebPDecParams * const p)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
EmitFancyRGB(const VP8Io * const io,WebPDecParams * const p)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 
FillAlphaPlane(uint8_t * dst,int w,int h,int stride)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 
EmitAlphaYUV(const VP8Io * const io,WebPDecParams * const p,int expected_num_lines_out)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 
GetAlphaSourceRow(const VP8Io * const io,const uint8_t ** alpha,int * const num_rows)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 
EmitAlphaRGB(const VP8Io * const io,WebPDecParams * const p,int expected_num_lines_out)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 
EmitAlphaRGBA4444(const VP8Io * const io,WebPDecParams * const p,int expected_num_lines_out)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 #if (WEBP_SWAP_16BIT_CSP == 1)
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 #if !defined(WEBP_REDUCE_SIZE)
Rescale(const uint8_t * src,int src_stride,int new_lines,WebPRescaler * const wrk)245 static int Rescale(const uint8_t* src, int src_stride,
246                    int new_lines, WebPRescaler* const wrk) {
247   int num_lines_out = 0;
248   while (new_lines > 0) {    // import new contributions of source rows.
249     const int lines_in = WebPRescalerImport(wrk, new_lines, src, src_stride);
250     src += lines_in * src_stride;
251     new_lines -= lines_in;
252     num_lines_out += WebPRescalerExport(wrk);    // emit output row(s)
253   }
254   return num_lines_out;
255 }
256 
EmitRescaledYUV(const VP8Io * const io,WebPDecParams * const p)257 static int EmitRescaledYUV(const VP8Io* const io, WebPDecParams* const p) {
258   const int mb_h = io->mb_h;
259   const int uv_mb_h = (mb_h + 1) >> 1;
260   WebPRescaler* const scaler = p->scaler_y;
261   int num_lines_out = 0;
262   if (WebPIsAlphaMode(p->output->colorspace) && io->a != NULL) {
263     // Before rescaling, we premultiply the luma directly into the io->y
264     // internal buffer. This is OK since these samples are not used for
265     // intra-prediction (the top samples are saved in cache_y_/u_/v_).
266     // But we need to cast the const away, though.
267     WebPMultRows((uint8_t*)io->y, io->y_stride,
268                  io->a, io->width, io->mb_w, mb_h, 0);
269   }
270   num_lines_out = Rescale(io->y, io->y_stride, mb_h, scaler);
271   Rescale(io->u, io->uv_stride, uv_mb_h, p->scaler_u);
272   Rescale(io->v, io->uv_stride, uv_mb_h, p->scaler_v);
273   return num_lines_out;
274 }
275 
EmitRescaledAlphaYUV(const VP8Io * const io,WebPDecParams * const p,int expected_num_lines_out)276 static int EmitRescaledAlphaYUV(const VP8Io* const io, WebPDecParams* const p,
277                                 int expected_num_lines_out) {
278   const WebPYUVABuffer* const buf = &p->output->u.YUVA;
279   uint8_t* const dst_a = buf->a + p->last_y * buf->a_stride;
280   if (io->a != NULL) {
281     uint8_t* const dst_y = buf->y + p->last_y * buf->y_stride;
282     const int num_lines_out = Rescale(io->a, io->width, io->mb_h, p->scaler_a);
283     assert(expected_num_lines_out == num_lines_out);
284     if (num_lines_out > 0) {   // unmultiply the Y
285       WebPMultRows(dst_y, buf->y_stride, dst_a, buf->a_stride,
286                    p->scaler_a->dst_width, num_lines_out, 1);
287     }
288   } else if (buf->a != NULL) {
289     // the user requested alpha, but there is none, set it to opaque.
290     assert(p->last_y + expected_num_lines_out <= io->scaled_height);
291     FillAlphaPlane(dst_a, io->scaled_width, expected_num_lines_out,
292                    buf->a_stride);
293   }
294   return 0;
295 }
296 
InitYUVRescaler(const VP8Io * const io,WebPDecParams * const p)297 static int InitYUVRescaler(const VP8Io* const io, WebPDecParams* const p) {
298   const int has_alpha = WebPIsAlphaMode(p->output->colorspace);
299   const WebPYUVABuffer* const buf = &p->output->u.YUVA;
300   const int out_width  = io->scaled_width;
301   const int out_height = io->scaled_height;
302   const int uv_out_width  = (out_width + 1) >> 1;
303   const int uv_out_height = (out_height + 1) >> 1;
304   const int uv_in_width  = (io->mb_w + 1) >> 1;
305   const int uv_in_height = (io->mb_h + 1) >> 1;
306   const size_t work_size = 2 * out_width;   // scratch memory for luma rescaler
307   const size_t uv_work_size = 2 * uv_out_width;  // and for each u/v ones
308   size_t tmp_size, rescaler_size;
309   rescaler_t* work;
310   WebPRescaler* scalers;
311   const int num_rescalers = has_alpha ? 4 : 3;
312 
313   tmp_size = (work_size + 2 * uv_work_size) * sizeof(*work);
314   if (has_alpha) {
315     tmp_size += work_size * sizeof(*work);
316   }
317   rescaler_size = num_rescalers * sizeof(*p->scaler_y) + WEBP_ALIGN_CST;
318 
319   p->memory = WebPSafeMalloc(1ULL, tmp_size + rescaler_size);
320   if (p->memory == NULL) {
321     return 0;   // memory error
322   }
323   work = (rescaler_t*)p->memory;
324 
325   scalers = (WebPRescaler*)WEBP_ALIGN((const uint8_t*)work + tmp_size);
326   p->scaler_y = &scalers[0];
327   p->scaler_u = &scalers[1];
328   p->scaler_v = &scalers[2];
329   p->scaler_a = has_alpha ? &scalers[3] : NULL;
330 
331   WebPRescalerInit(p->scaler_y, io->mb_w, io->mb_h,
332                    buf->y, out_width, out_height, buf->y_stride, 1,
333                    work);
334   WebPRescalerInit(p->scaler_u, uv_in_width, uv_in_height,
335                    buf->u, uv_out_width, uv_out_height, buf->u_stride, 1,
336                    work + work_size);
337   WebPRescalerInit(p->scaler_v, uv_in_width, uv_in_height,
338                    buf->v, uv_out_width, uv_out_height, buf->v_stride, 1,
339                    work + work_size + uv_work_size);
340   p->emit = EmitRescaledYUV;
341 
342   if (has_alpha) {
343     WebPRescalerInit(p->scaler_a, io->mb_w, io->mb_h,
344                      buf->a, out_width, out_height, buf->a_stride, 1,
345                      work + work_size + 2 * uv_work_size);
346     p->emit_alpha = EmitRescaledAlphaYUV;
347     WebPInitAlphaProcessing();
348   }
349   return 1;
350 }
351 
352 //------------------------------------------------------------------------------
353 // RGBA rescaling
354 
ExportRGB(WebPDecParams * const p,int y_pos)355 static int ExportRGB(WebPDecParams* const p, int y_pos) {
356   const WebPYUV444Converter convert =
357       WebPYUV444Converters[p->output->colorspace];
358   const WebPRGBABuffer* const buf = &p->output->u.RGBA;
359   uint8_t* dst = buf->rgba + y_pos * buf->stride;
360   int num_lines_out = 0;
361   // For RGB rescaling, because of the YUV420, current scan position
362   // U/V can be +1/-1 line from the Y one.  Hence the double test.
363   while (WebPRescalerHasPendingOutput(p->scaler_y) &&
364          WebPRescalerHasPendingOutput(p->scaler_u)) {
365     assert(y_pos + num_lines_out < p->output->height);
366     assert(p->scaler_u->y_accum == p->scaler_v->y_accum);
367     WebPRescalerExportRow(p->scaler_y);
368     WebPRescalerExportRow(p->scaler_u);
369     WebPRescalerExportRow(p->scaler_v);
370     convert(p->scaler_y->dst, p->scaler_u->dst, p->scaler_v->dst,
371             dst, p->scaler_y->dst_width);
372     dst += buf->stride;
373     ++num_lines_out;
374   }
375   return num_lines_out;
376 }
377 
EmitRescaledRGB(const VP8Io * const io,WebPDecParams * const p)378 static int EmitRescaledRGB(const VP8Io* const io, WebPDecParams* const p) {
379   const int mb_h = io->mb_h;
380   const int uv_mb_h = (mb_h + 1) >> 1;
381   int j = 0, uv_j = 0;
382   int num_lines_out = 0;
383   while (j < mb_h) {
384     const int y_lines_in =
385         WebPRescalerImport(p->scaler_y, mb_h - j,
386                            io->y + j * io->y_stride, io->y_stride);
387     j += y_lines_in;
388     if (WebPRescaleNeededLines(p->scaler_u, uv_mb_h - uv_j)) {
389       const int u_lines_in =
390           WebPRescalerImport(p->scaler_u, uv_mb_h - uv_j,
391                              io->u + uv_j * io->uv_stride, io->uv_stride);
392       const int v_lines_in =
393           WebPRescalerImport(p->scaler_v, uv_mb_h - uv_j,
394                              io->v + uv_j * io->uv_stride, io->uv_stride);
395       (void)v_lines_in;   // remove a gcc warning
396       assert(u_lines_in == v_lines_in);
397       uv_j += u_lines_in;
398     }
399     num_lines_out += ExportRGB(p, p->last_y + num_lines_out);
400   }
401   return num_lines_out;
402 }
403 
ExportAlpha(WebPDecParams * const p,int y_pos,int max_lines_out)404 static int ExportAlpha(WebPDecParams* const p, int y_pos, int max_lines_out) {
405   const WebPRGBABuffer* const buf = &p->output->u.RGBA;
406   uint8_t* const base_rgba = buf->rgba + y_pos * buf->stride;
407   const WEBP_CSP_MODE colorspace = p->output->colorspace;
408   const int alpha_first =
409       (colorspace == MODE_ARGB || colorspace == MODE_Argb);
410   uint8_t* dst = base_rgba + (alpha_first ? 0 : 3);
411   int num_lines_out = 0;
412   const int is_premult_alpha = WebPIsPremultipliedMode(colorspace);
413   uint32_t non_opaque = 0;
414   const int width = p->scaler_a->dst_width;
415 
416   while (WebPRescalerHasPendingOutput(p->scaler_a) &&
417          num_lines_out < max_lines_out) {
418     assert(y_pos + num_lines_out < p->output->height);
419     WebPRescalerExportRow(p->scaler_a);
420     non_opaque |= WebPDispatchAlpha(p->scaler_a->dst, 0, width, 1, dst, 0);
421     dst += buf->stride;
422     ++num_lines_out;
423   }
424   if (is_premult_alpha && non_opaque) {
425     WebPApplyAlphaMultiply(base_rgba, alpha_first,
426                            width, num_lines_out, buf->stride);
427   }
428   return num_lines_out;
429 }
430 
ExportAlphaRGBA4444(WebPDecParams * const p,int y_pos,int max_lines_out)431 static int ExportAlphaRGBA4444(WebPDecParams* const p, int y_pos,
432                                int max_lines_out) {
433   const WebPRGBABuffer* const buf = &p->output->u.RGBA;
434   uint8_t* const base_rgba = buf->rgba + y_pos * buf->stride;
435 #if (WEBP_SWAP_16BIT_CSP == 1)
436   uint8_t* alpha_dst = base_rgba;
437 #else
438   uint8_t* alpha_dst = base_rgba + 1;
439 #endif
440   int num_lines_out = 0;
441   const WEBP_CSP_MODE colorspace = p->output->colorspace;
442   const int width = p->scaler_a->dst_width;
443   const int is_premult_alpha = WebPIsPremultipliedMode(colorspace);
444   uint32_t alpha_mask = 0x0f;
445 
446   while (WebPRescalerHasPendingOutput(p->scaler_a) &&
447          num_lines_out < max_lines_out) {
448     int i;
449     assert(y_pos + num_lines_out < p->output->height);
450     WebPRescalerExportRow(p->scaler_a);
451     for (i = 0; i < width; ++i) {
452       // Fill in the alpha value (converted to 4 bits).
453       const uint32_t alpha_value = p->scaler_a->dst[i] >> 4;
454       alpha_dst[2 * i] = (alpha_dst[2 * i] & 0xf0) | alpha_value;
455       alpha_mask &= alpha_value;
456     }
457     alpha_dst += buf->stride;
458     ++num_lines_out;
459   }
460   if (is_premult_alpha && alpha_mask != 0x0f) {
461     WebPApplyAlphaMultiply4444(base_rgba, width, num_lines_out, buf->stride);
462   }
463   return num_lines_out;
464 }
465 
EmitRescaledAlphaRGB(const VP8Io * const io,WebPDecParams * const p,int expected_num_out_lines)466 static int EmitRescaledAlphaRGB(const VP8Io* const io, WebPDecParams* const p,
467                                 int expected_num_out_lines) {
468   if (io->a != NULL) {
469     WebPRescaler* const scaler = p->scaler_a;
470     int lines_left = expected_num_out_lines;
471     const int y_end = p->last_y + lines_left;
472     while (lines_left > 0) {
473       const int row_offset = scaler->src_y - io->mb_y;
474       WebPRescalerImport(scaler, io->mb_h + io->mb_y - scaler->src_y,
475                          io->a + row_offset * io->width, io->width);
476       lines_left -= p->emit_alpha_row(p, y_end - lines_left, lines_left);
477     }
478   }
479   return 0;
480 }
481 
InitRGBRescaler(const VP8Io * const io,WebPDecParams * const p)482 static int InitRGBRescaler(const VP8Io* const io, WebPDecParams* const p) {
483   const int has_alpha = WebPIsAlphaMode(p->output->colorspace);
484   const int out_width  = io->scaled_width;
485   const int out_height = io->scaled_height;
486   const int uv_in_width  = (io->mb_w + 1) >> 1;
487   const int uv_in_height = (io->mb_h + 1) >> 1;
488   const size_t work_size = 2 * out_width;   // scratch memory for one rescaler
489   rescaler_t* work;  // rescalers work area
490   uint8_t* tmp;   // tmp storage for scaled YUV444 samples before RGB conversion
491   size_t tmp_size1, tmp_size2, total_size, rescaler_size;
492   WebPRescaler* scalers;
493   const int num_rescalers = has_alpha ? 4 : 3;
494 
495   tmp_size1 = 3 * work_size;
496   tmp_size2 = 3 * out_width;
497   if (has_alpha) {
498     tmp_size1 += work_size;
499     tmp_size2 += out_width;
500   }
501   total_size = tmp_size1 * sizeof(*work) + tmp_size2 * sizeof(*tmp);
502   rescaler_size = num_rescalers * sizeof(*p->scaler_y) + WEBP_ALIGN_CST;
503 
504   p->memory = WebPSafeMalloc(1ULL, total_size + rescaler_size);
505   if (p->memory == NULL) {
506     return 0;   // memory error
507   }
508   work = (rescaler_t*)p->memory;
509   tmp = (uint8_t*)(work + tmp_size1);
510 
511   scalers = (WebPRescaler*)WEBP_ALIGN((const uint8_t*)work + total_size);
512   p->scaler_y = &scalers[0];
513   p->scaler_u = &scalers[1];
514   p->scaler_v = &scalers[2];
515   p->scaler_a = has_alpha ? &scalers[3] : NULL;
516 
517   WebPRescalerInit(p->scaler_y, io->mb_w, io->mb_h,
518                    tmp + 0 * out_width, out_width, out_height, 0, 1,
519                    work + 0 * work_size);
520   WebPRescalerInit(p->scaler_u, uv_in_width, uv_in_height,
521                    tmp + 1 * out_width, out_width, out_height, 0, 1,
522                    work + 1 * work_size);
523   WebPRescalerInit(p->scaler_v, uv_in_width, uv_in_height,
524                    tmp + 2 * out_width, out_width, out_height, 0, 1,
525                    work + 2 * work_size);
526   p->emit = EmitRescaledRGB;
527   WebPInitYUV444Converters();
528 
529   if (has_alpha) {
530     WebPRescalerInit(p->scaler_a, io->mb_w, io->mb_h,
531                      tmp + 3 * out_width, out_width, out_height, 0, 1,
532                      work + 3 * work_size);
533     p->emit_alpha = EmitRescaledAlphaRGB;
534     if (p->output->colorspace == MODE_RGBA_4444 ||
535         p->output->colorspace == MODE_rgbA_4444) {
536       p->emit_alpha_row = ExportAlphaRGBA4444;
537     } else {
538       p->emit_alpha_row = ExportAlpha;
539     }
540     WebPInitAlphaProcessing();
541   }
542   return 1;
543 }
544 
545 #endif  // WEBP_REDUCE_SIZE
546 
547 //------------------------------------------------------------------------------
548 // Default custom functions
549 
CustomSetup(VP8Io * io)550 static int CustomSetup(VP8Io* io) {
551   WebPDecParams* const p = (WebPDecParams*)io->opaque;
552   const WEBP_CSP_MODE colorspace = p->output->colorspace;
553   const int is_rgb = WebPIsRGBMode(colorspace);
554   const int is_alpha = WebPIsAlphaMode(colorspace);
555 
556   p->memory = NULL;
557   p->emit = NULL;
558   p->emit_alpha = NULL;
559   p->emit_alpha_row = NULL;
560   if (!WebPIoInitFromOptions(p->options, io, is_alpha ? MODE_YUV : MODE_YUVA)) {
561     return 0;
562   }
563   if (is_alpha && WebPIsPremultipliedMode(colorspace)) {
564     WebPInitUpsamplers();
565   }
566   if (io->use_scaling) {
567 #if !defined(WEBP_REDUCE_SIZE)
568     const int ok = is_rgb ? InitRGBRescaler(io, p) : InitYUVRescaler(io, p);
569     if (!ok) {
570       return 0;    // memory error
571     }
572 #else
573     return 0;   // rescaling support not compiled
574 #endif
575   } else {
576     if (is_rgb) {
577       WebPInitSamplers();
578       p->emit = EmitSampledRGB;   // default
579       if (io->fancy_upsampling) {
580 #ifdef FANCY_UPSAMPLING
581         const int uv_width = (io->mb_w + 1) >> 1;
582         p->memory = WebPSafeMalloc(1ULL, (size_t)(io->mb_w + 2 * uv_width));
583         if (p->memory == NULL) {
584           return 0;   // memory error.
585         }
586         p->tmp_y = (uint8_t*)p->memory;
587         p->tmp_u = p->tmp_y + io->mb_w;
588         p->tmp_v = p->tmp_u + uv_width;
589         p->emit = EmitFancyRGB;
590         WebPInitUpsamplers();
591 #endif
592       }
593     } else {
594       p->emit = EmitYUV;
595     }
596     if (is_alpha) {  // need transparency output
597       p->emit_alpha =
598           (colorspace == MODE_RGBA_4444 || colorspace == MODE_rgbA_4444) ?
599               EmitAlphaRGBA4444
600           : is_rgb ? EmitAlphaRGB
601           : EmitAlphaYUV;
602       if (is_rgb) {
603         WebPInitAlphaProcessing();
604       }
605     }
606   }
607 
608   return 1;
609 }
610 
611 //------------------------------------------------------------------------------
612 
CustomPut(const VP8Io * io)613 static int CustomPut(const VP8Io* io) {
614   WebPDecParams* const p = (WebPDecParams*)io->opaque;
615   const int mb_w = io->mb_w;
616   const int mb_h = io->mb_h;
617   int num_lines_out;
618   assert(!(io->mb_y & 1));
619 
620   if (mb_w <= 0 || mb_h <= 0) {
621     return 0;
622   }
623   num_lines_out = p->emit(io, p);
624   if (p->emit_alpha != NULL) {
625     p->emit_alpha(io, p, num_lines_out);
626   }
627   p->last_y += num_lines_out;
628   return 1;
629 }
630 
631 //------------------------------------------------------------------------------
632 
CustomTeardown(const VP8Io * io)633 static void CustomTeardown(const VP8Io* io) {
634   WebPDecParams* const p = (WebPDecParams*)io->opaque;
635   WebPSafeFree(p->memory);
636   p->memory = NULL;
637 }
638 
639 //------------------------------------------------------------------------------
640 // Main entry point
641 
WebPInitCustomIo(WebPDecParams * const params,VP8Io * const io)642 void WebPInitCustomIo(WebPDecParams* const params, VP8Io* const io) {
643   io->put      = CustomPut;
644   io->setup    = CustomSetup;
645   io->teardown = CustomTeardown;
646   io->opaque   = params;
647 }
648 
649 //------------------------------------------------------------------------------
650