Coverage Report

Created: 2025-08-11 08:01

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