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