void WebPRescalerInit(WebPRescaler* const wrk, int src_width, int src_height, uint8_t* const dst, int dst_width, int dst_height, int dst_stride, int num_channels, rescaler_t* const work) { const int x_add = src_width, x_sub = dst_width; const int y_add = src_height, y_sub = dst_height; wrk->x_expand = (src_width < dst_width); wrk->y_expand = (src_height < dst_height); wrk->src_width = src_width; wrk->src_height = src_height; wrk->dst_width = dst_width; wrk->dst_height = dst_height; wrk->src_y = 0; wrk->dst_y = 0; wrk->dst = dst; wrk->dst_stride = dst_stride; wrk->num_channels = num_channels; // for 'x_expand', we use bilinear interpolation wrk->x_add = wrk->x_expand ? (x_sub - 1) : x_add; wrk->x_sub = wrk->x_expand ? (x_add - 1) : x_sub; if (!wrk->x_expand) { // fx_scale is not used otherwise wrk->fx_scale = WEBP_RESCALER_FRAC(1, wrk->x_sub); } // vertical scaling parameters wrk->y_add = wrk->y_expand ? y_add - 1 : y_add; wrk->y_sub = wrk->y_expand ? y_sub - 1 : y_sub; wrk->y_accum = wrk->y_expand ? wrk->y_sub : wrk->y_add; if (!wrk->y_expand) { // this is WEBP_RESCALER_FRAC(dst_height, x_add * y_add) without the cast. const uint64_t ratio = (uint64_t)dst_height * WEBP_RESCALER_ONE / (wrk->x_add * wrk->y_add); if (ratio != (uint32_t)ratio) { // We can't represent the ratio with the current fixed-point precision. // => We special-case fxy_scale = 0, in WebPRescalerExportRow(). wrk->fxy_scale = 0; } else { wrk->fxy_scale = (uint32_t)ratio; } wrk->fy_scale = WEBP_RESCALER_FRAC(1, wrk->y_sub); } else { wrk->fy_scale = WEBP_RESCALER_FRAC(1, wrk->x_add); // wrk->fxy_scale is unused here. } wrk->irow = work; wrk->frow = work + num_channels * dst_width; memset(work, 0, 2 * dst_width * num_channels * sizeof(*work)); WebPRescalerDspInit(); }
void WebPRescalerExportRowExpandC(WebPRescaler* const wrk) { int x_out; uint8_t* const dst = wrk->dst; rescaler_t* const irow = wrk->irow; const int x_out_max = wrk->dst_width * wrk->num_channels; const rescaler_t* const frow = wrk->frow; assert(!WebPRescalerOutputDone(wrk)); assert(wrk->y_accum <= 0); assert(wrk->y_expand); assert(wrk->y_sub != 0); if (wrk->y_accum == 0) { for (x_out = 0; x_out < x_out_max; ++x_out) { const uint32_t J = frow[x_out]; const int v = (int)MULT_FIX(J, wrk->fy_scale); assert(v >= 0 && v <= 255); dst[x_out] = v; } } else { const uint32_t B = WEBP_RESCALER_FRAC(-wrk->y_accum, wrk->y_sub); const uint32_t A = (uint32_t)(WEBP_RESCALER_ONE - B); for (x_out = 0; x_out < x_out_max; ++x_out) { const uint64_t I = (uint64_t)A * frow[x_out] + (uint64_t)B * irow[x_out]; const uint32_t J = (uint32_t)((I + ROUNDER) >> WEBP_RESCALER_RFIX); const int v = (int)MULT_FIX(J, wrk->fy_scale); assert(v >= 0 && v <= 255); dst[x_out] = v; } } }
static void RescalerExportRowExpandSSE2(WebPRescaler* const wrk) { int x_out; uint8_t* const dst = wrk->dst; rescaler_t* const irow = wrk->irow; const int x_out_max = wrk->dst_width * wrk->num_channels; const rescaler_t* const frow = wrk->frow; const __m128i mult = _mm_set_epi32(0, wrk->fy_scale, 0, wrk->fy_scale); assert(!WebPRescalerOutputDone(wrk)); assert(wrk->y_accum <= 0 && wrk->y_sub + wrk->y_accum >= 0); assert(wrk->y_expand); if (wrk->y_accum == 0) { for (x_out = 0; x_out + 8 <= x_out_max; x_out += 8) { __m128i A0, A1, A2, A3; LoadDispatchAndMult(frow + x_out, NULL, &A0, &A1, &A2, &A3); ProcessRow(&A0, &A1, &A2, &A3, &mult, dst + x_out); } for (; x_out < x_out_max; ++x_out) { const uint32_t J = frow[x_out]; const int v = (int)MULT_FIX(J, wrk->fy_scale); assert(v >= 0 && v <= 255); dst[x_out] = v; } } else { const uint32_t B = WEBP_RESCALER_FRAC(-wrk->y_accum, wrk->y_sub); const uint32_t A = (uint32_t)(WEBP_RESCALER_ONE - B); const __m128i mA = _mm_set_epi32(0, A, 0, A); const __m128i mB = _mm_set_epi32(0, B, 0, B); const __m128i rounder = _mm_set_epi32(0, ROUNDER, 0, ROUNDER); for (x_out = 0; x_out + 8 <= x_out_max; x_out += 8) { __m128i A0, A1, A2, A3, B0, B1, B2, B3; LoadDispatchAndMult(frow + x_out, &mA, &A0, &A1, &A2, &A3); LoadDispatchAndMult(irow + x_out, &mB, &B0, &B1, &B2, &B3); { const __m128i C0 = _mm_add_epi64(A0, B0); const __m128i C1 = _mm_add_epi64(A1, B1); const __m128i C2 = _mm_add_epi64(A2, B2); const __m128i C3 = _mm_add_epi64(A3, B3); const __m128i D0 = _mm_add_epi64(C0, rounder); const __m128i D1 = _mm_add_epi64(C1, rounder); const __m128i D2 = _mm_add_epi64(C2, rounder); const __m128i D3 = _mm_add_epi64(C3, rounder); const __m128i E0 = _mm_srli_epi64(D0, WEBP_RESCALER_RFIX); const __m128i E1 = _mm_srli_epi64(D1, WEBP_RESCALER_RFIX); const __m128i E2 = _mm_srli_epi64(D2, WEBP_RESCALER_RFIX); const __m128i E3 = _mm_srli_epi64(D3, WEBP_RESCALER_RFIX); ProcessRow(&E0, &E1, &E2, &E3, &mult, dst + x_out); } } for (; x_out < x_out_max; ++x_out) { const uint64_t I = (uint64_t)A * frow[x_out] + (uint64_t)B * irow[x_out]; const uint32_t J = (uint32_t)((I + ROUNDER) >> WEBP_RESCALER_RFIX); const int v = (int)MULT_FIX(J, wrk->fy_scale); assert(v >= 0 && v <= 255); dst[x_out] = v; } } }
void WebPRescalerInit(WebPRescaler* const wrk, int src_width, int src_height, uint8_t* const dst, int dst_width, int dst_height, int dst_stride, int num_channels, rescaler_t* const work) { const int x_add = src_width, x_sub = dst_width; const int y_add = src_height, y_sub = dst_height; wrk->x_expand = (src_width < dst_width); wrk->y_expand = (src_height < dst_height); wrk->src_width = src_width; wrk->src_height = src_height; wrk->dst_width = dst_width; wrk->dst_height = dst_height; wrk->src_y = 0; wrk->dst_y = 0; wrk->dst = dst; wrk->dst_stride = dst_stride; wrk->num_channels = num_channels; // for 'x_expand', we use bilinear interpolation wrk->x_add = wrk->x_expand ? (x_sub - 1) : x_add; wrk->x_sub = wrk->x_expand ? (x_add - 1) : x_sub; if (!wrk->x_expand) { // fx_scale is not used otherwise wrk->fx_scale = WEBP_RESCALER_FRAC(1, wrk->x_sub); } // vertical scaling parameters wrk->y_add = wrk->y_expand ? y_add - 1 : y_add; wrk->y_sub = wrk->y_expand ? y_sub - 1 : y_sub; wrk->y_accum = wrk->y_expand ? wrk->y_sub : wrk->y_add; if (!wrk->y_expand) { // note the very special case where x_add = y_add = 1 cannot be represented. // We special-case fxy_scale = 0 in this case, in WebPRescalerExportRow(). wrk->fxy_scale = WEBP_RESCALER_FRAC(dst_height, wrk->x_add * wrk->y_add); wrk->fy_scale = WEBP_RESCALER_FRAC(1, wrk->y_sub); } else { wrk->fy_scale = WEBP_RESCALER_FRAC(1, wrk->x_add); // wrk->fxy_scale is unused here. } wrk->irow = work; wrk->frow = work + num_channels * dst_width; memset(work, 0, 2 * dst_width * num_channels * sizeof(*work)); WebPRescalerDspInit(); }