Coverage Report

Created: 2024-06-18 06:05

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