Coverage Report

Created: 2025-06-13 06:48

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