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