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