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