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