Make rescaler methods generic

to be used in rescaling of lossless bitstreams.
(cherry picked from commit 2e12a3045498b6faf13b93bc25391e8226119f0a)
This commit is contained in:
Urvang Joshi 2012-03-21 06:48:50 +00:00 committed by James Zern
parent 3eacee8158
commit efc2016a31
3 changed files with 45 additions and 34 deletions

View File

@ -235,7 +235,7 @@ static int Rescale(const uint8_t* src, int src_stride,
int new_lines, WebPRescaler* const wrk) { int new_lines, WebPRescaler* const wrk) {
int num_lines_out = 0; int num_lines_out = 0;
while (new_lines-- > 0) { // import new contribution of one source row. while (new_lines-- > 0) { // import new contribution of one source row.
WebPRescalerImportRow(src, wrk); WebPRescalerImportRow(src, 0, wrk);
src += src_stride; src += src_stride;
wrk->y_accum -= wrk->y_sub; wrk->y_accum -= wrk->y_sub;
while (wrk->y_accum <= 0) { // emit output row(s) while (wrk->y_accum <= 0) { // emit output row(s)
@ -291,23 +291,23 @@ static int InitYUVRescaler(const VP8Io* const io, WebPDecParams* const p) {
} }
work = (int32_t*)p->memory; work = (int32_t*)p->memory;
WebPRescalerInit(&p->scaler_y, io->mb_w, io->mb_h, WebPRescalerInit(&p->scaler_y, io->mb_w, io->mb_h,
buf->y, out_width, out_height, buf->y_stride, buf->y, out_width, out_height, buf->y_stride, 1,
io->mb_w, out_width, io->mb_h, out_height, io->mb_w, out_width, io->mb_h, out_height,
work); work);
WebPRescalerInit(&p->scaler_u, uv_in_width, uv_in_height, WebPRescalerInit(&p->scaler_u, uv_in_width, uv_in_height,
buf->u, uv_out_width, uv_out_height, buf->u_stride, buf->u, uv_out_width, uv_out_height, buf->u_stride, 1,
uv_in_width, uv_out_width, uv_in_width, uv_out_width,
uv_in_height, uv_out_height, uv_in_height, uv_out_height,
work + work_size); work + work_size);
WebPRescalerInit(&p->scaler_v, uv_in_width, uv_in_height, WebPRescalerInit(&p->scaler_v, uv_in_width, uv_in_height,
buf->v, uv_out_width, uv_out_height, buf->v_stride, buf->v, uv_out_width, uv_out_height, buf->v_stride, 1,
uv_in_width, uv_out_width, uv_in_width, uv_out_width,
uv_in_height, uv_out_height, uv_in_height, uv_out_height,
work + work_size + uv_work_size); work + work_size + uv_work_size);
p->emit = EmitRescaledYUV; p->emit = EmitRescaledYUV;
if (has_alpha) { if (has_alpha) {
WebPRescalerInit(&p->scaler_a, io->mb_w, io->mb_h, WebPRescalerInit(&p->scaler_a, io->mb_w, io->mb_h,
buf->a, out_width, out_height, buf->a_stride, buf->a, out_width, out_height, buf->a_stride, 1,
io->mb_w, out_width, io->mb_h, out_height, io->mb_w, out_width, io->mb_h, out_height,
work + work_size + 2 * uv_work_size); work + work_size + 2 * uv_work_size);
p->emit_alpha = EmitRescaledAlphaYUV; p->emit_alpha = EmitRescaledAlphaYUV;
@ -324,7 +324,7 @@ static int Import(const uint8_t* src, int src_stride,
int new_lines, WebPRescaler* const wrk) { int new_lines, WebPRescaler* const wrk) {
int num_lines_in = 0; int num_lines_in = 0;
while (num_lines_in < new_lines && wrk->y_accum > 0) { while (num_lines_in < new_lines && wrk->y_accum > 0) {
WebPRescalerImportRow(src, wrk); WebPRescalerImportRow(src, 0, wrk);
src += src_stride; src += src_stride;
++num_lines_in; ++num_lines_in;
wrk->y_accum -= wrk->y_sub; wrk->y_accum -= wrk->y_sub;
@ -452,22 +452,22 @@ static int InitRGBRescaler(const VP8Io* const io, WebPDecParams* const p) {
work = (int32_t*)p->memory; work = (int32_t*)p->memory;
tmp = (uint8_t*)(work + tmp_size1); tmp = (uint8_t*)(work + tmp_size1);
WebPRescalerInit(&p->scaler_y, io->mb_w, io->mb_h, WebPRescalerInit(&p->scaler_y, io->mb_w, io->mb_h,
tmp + 0 * out_width, out_width, out_height, 0, tmp + 0 * out_width, out_width, out_height, 0, 1,
io->mb_w, out_width, io->mb_h, out_height, io->mb_w, out_width, io->mb_h, out_height,
work + 0 * work_size); work + 0 * work_size);
WebPRescalerInit(&p->scaler_u, uv_in_width, uv_in_height, WebPRescalerInit(&p->scaler_u, uv_in_width, uv_in_height,
tmp + 1 * out_width, out_width, out_height, 0, tmp + 1 * out_width, out_width, out_height, 0, 1,
io->mb_w, 2 * out_width, io->mb_h, 2 * out_height, io->mb_w, 2 * out_width, io->mb_h, 2 * out_height,
work + 1 * work_size); work + 1 * work_size);
WebPRescalerInit(&p->scaler_v, uv_in_width, uv_in_height, WebPRescalerInit(&p->scaler_v, uv_in_width, uv_in_height,
tmp + 2 * out_width, out_width, out_height, 0, tmp + 2 * out_width, out_width, out_height, 0, 1,
io->mb_w, 2 * out_width, io->mb_h, 2 * out_height, io->mb_w, 2 * out_width, io->mb_h, 2 * out_height,
work + 2 * work_size); work + 2 * work_size);
p->emit = EmitRescaledRGB; p->emit = EmitRescaledRGB;
if (has_alpha) { if (has_alpha) {
WebPRescalerInit(&p->scaler_a, io->mb_w, io->mb_h, WebPRescalerInit(&p->scaler_a, io->mb_w, io->mb_h,
tmp + 3 * out_width, out_width, out_height, 0, tmp + 3 * out_width, out_width, out_height, 0, 1,
io->mb_w, out_width, io->mb_h, out_height, io->mb_w, out_width, io->mb_h, out_height,
work + 3 * work_size); work + 3 * work_size);
p->emit_alpha = EmitRescaledAlphaRGB; p->emit_alpha = EmitRescaledAlphaRGB;

View File

@ -610,7 +610,7 @@ VP8StatusCode WebPDecode(const uint8_t* data, uint32_t data_size,
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Simple picture rescaler // Cropping & rescaling.
int WebPIoInitFromOptions(const WebPDecoderOptions* const options, int WebPIoInitFromOptions(const WebPDecoderOptions* const options,
VP8Io* const io) { VP8Io* const io) {
@ -665,12 +665,12 @@ int WebPIoInitFromOptions(const WebPDecoderOptions* const options,
} }
#define RFIX 30 #define RFIX 30
#define MULT(x,y) (((int64_t)(x) * (y) + (1 << (RFIX - 1))) >> RFIX) #define MULT_FIX(x,y) (((int64_t)(x) * (y) + (1 << (RFIX - 1))) >> RFIX)
void WebPRescalerInit(WebPRescaler* const wrk, int src_width, int src_height, void WebPRescalerInit(WebPRescaler* const wrk, int src_width, int src_height,
uint8_t* dst, int dst_width, int dst_height, uint8_t* const dst, int dst_width, int dst_height,
int dst_stride, int x_add, int x_sub, int y_add, int dst_stride, int num_channels, int x_add, int x_sub,
int y_sub, int32_t* work) { int y_add, int y_sub, int32_t* const work) {
wrk->x_expand = (src_width < dst_width); wrk->x_expand = (src_width < dst_width);
wrk->src_width = src_width; wrk->src_width = src_width;
wrk->src_height = src_height; wrk->src_height = src_height;
@ -678,6 +678,7 @@ void WebPRescalerInit(WebPRescaler* const wrk, int src_width, int src_height,
wrk->dst_height = dst_height; wrk->dst_height = dst_height;
wrk->dst = dst; wrk->dst = dst;
wrk->dst_stride = dst_stride; wrk->dst_stride = dst_stride;
wrk->num_channels = num_channels;
// for 'x_expand', we use bilinear interpolation // for 'x_expand', we use bilinear interpolation
wrk->x_add = wrk->x_expand ? (x_sub - 1) : x_add - x_sub; wrk->x_add = wrk->x_expand ? (x_sub - 1) : x_add - x_sub;
wrk->x_sub = wrk->x_expand ? (x_add - 1) : x_sub; wrk->x_sub = wrk->x_expand ? (x_add - 1) : x_sub;
@ -690,34 +691,40 @@ void WebPRescalerInit(WebPRescaler* const wrk, int src_width, int src_height,
((int64_t)dst_height << RFIX) / (x_sub * src_height) : ((int64_t)dst_height << RFIX) / (x_sub * src_height) :
((int64_t)dst_height << RFIX) / (x_add * src_height); ((int64_t)dst_height << RFIX) / (x_add * src_height);
wrk->irow = work; wrk->irow = work;
wrk->frow = work + dst_width; wrk->frow = work + num_channels * dst_width;
} }
void WebPRescalerImportRow(const uint8_t* const src, WebPRescaler* const wrk) { void WebPRescalerImportRow(const uint8_t* const src, int channel,
int x_in = 0; WebPRescaler* const wrk) {
const int x_stride = wrk->num_channels;
const int x_out_max = wrk->dst_width * wrk->num_channels;
int x_in = channel;
int x_out; int x_out;
int accum = 0; int accum = 0;
if (!wrk->x_expand) { if (!wrk->x_expand) {
int sum = 0; int sum = 0;
for (x_out = 0; x_out < wrk->dst_width; ++x_out) { for (x_out = channel; x_out < x_out_max; x_out += x_stride) {
accum += wrk->x_add; accum += wrk->x_add;
for (; accum > 0; accum -= wrk->x_sub) { for (; accum > 0; accum -= wrk->x_sub) {
sum += src[x_in++]; sum += src[x_in];
x_in += x_stride;
} }
{ // Emit next horizontal pixel. { // Emit next horizontal pixel.
const int32_t base = src[x_in++]; const int32_t base = src[x_in];
const int32_t frac = base * (-accum); const int32_t frac = base * (-accum);
x_in += x_stride;
wrk->frow[x_out] = (sum + base) * wrk->x_sub - frac; wrk->frow[x_out] = (sum + base) * wrk->x_sub - frac;
// fresh fractional start for next pixel // fresh fractional start for next pixel
sum = (int)MULT(frac, wrk->fx_scale); sum = (int)MULT_FIX(frac, wrk->fx_scale);
} }
} }
} else { // simple bilinear interpolation } else { // simple bilinear interpolation
int left = src[0], right = src[0]; int left = src[channel], right = src[channel];
for (x_out = 0; x_out < wrk->dst_width; ++x_out) { for (x_out = channel; x_out < x_out_max; x_out += x_stride) {
if (accum < 0) { if (accum < 0) {
left = right; left = right;
right = src[++x_in]; x_in += x_stride;
right = src[x_in];
accum += wrk->x_add; accum += wrk->x_add;
} }
wrk->frow[x_out] = right * wrk->x_add + (left - right) * accum; wrk->frow[x_out] = right * wrk->x_add + (left - right) * accum;
@ -725,7 +732,7 @@ void WebPRescalerImportRow(const uint8_t* const src, WebPRescaler* const wrk) {
} }
} }
// Accumulate the new row's contribution // Accumulate the new row's contribution
for (x_out = 0; x_out < wrk->dst_width; ++x_out) { for (x_out = channel; x_out < x_out_max; x_out += x_stride) {
wrk->irow[x_out] += wrk->frow[x_out]; wrk->irow[x_out] += wrk->frow[x_out];
} }
} }
@ -733,10 +740,11 @@ void WebPRescalerImportRow(const uint8_t* const src, WebPRescaler* const wrk) {
void WebPRescalerExportRow(WebPRescaler* const wrk) { void WebPRescalerExportRow(WebPRescaler* const wrk) {
int x_out; int x_out;
const int yscale = wrk->fy_scale * (-wrk->y_accum); const int yscale = wrk->fy_scale * (-wrk->y_accum);
const int x_out_max = wrk->dst_width * wrk->num_channels;
assert(wrk->y_accum <= 0); assert(wrk->y_accum <= 0);
for (x_out = 0; x_out < wrk->dst_width; ++x_out) { for (x_out = 0; x_out < x_out_max; ++x_out) {
const int frac = (int)MULT(wrk->frow[x_out], yscale); const int frac = (int)MULT_FIX(wrk->frow[x_out], yscale);
const int v = (int)MULT(wrk->irow[x_out] - frac, wrk->fxy_scale); const int v = (int)MULT_FIX(wrk->irow[x_out] - frac, wrk->fxy_scale);
wrk->dst[x_out] = (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255; wrk->dst[x_out] = (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255;
wrk->irow[x_out] = frac; // new fractional start wrk->irow[x_out] = frac; // new fractional start
} }
@ -744,7 +752,7 @@ void WebPRescalerExportRow(WebPRescaler* const wrk) {
wrk->dst += wrk->dst_stride; wrk->dst += wrk->dst_stride;
} }
#undef MULT #undef MULT_FIX
#undef RFIX #undef RFIX
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -27,6 +27,7 @@ typedef int (*OutputFunc)(const VP8Io* const io, WebPDecParams* const p);
// Structure use for on-the-fly rescaling // Structure use for on-the-fly rescaling
typedef struct { typedef struct {
int x_expand; // true if we're expanding in the x direction int x_expand; // true if we're expanding in the x direction
int num_channels; // bytes to jump between pixels
int fy_scale, fx_scale; // fixed-point scaling factor int fy_scale, fx_scale; // fixed-point scaling factor
int64_t fxy_scale; // '' int64_t fxy_scale; // ''
// we need hpel-precise add/sub increments, for the downsampled U/V planes. // we need hpel-precise add/sub increments, for the downsampled U/V planes.
@ -168,12 +169,14 @@ int WebPIoInitFromOptions(const WebPDecoderOptions* const options,
// Initialize a rescaler given scratch area 'work' and dimensions of src & dst. // Initialize a rescaler given scratch area 'work' and dimensions of src & dst.
void WebPRescalerInit(WebPRescaler* const wrk, int src_width, int src_height, void WebPRescalerInit(WebPRescaler* const wrk, int src_width, int src_height,
uint8_t* dst, int dst_width, int dst_height, uint8_t* const dst, int dst_width, int dst_height,
int dst_stride, int x_add, int x_sub, int y_add, int dst_stride, int num_channels, int x_add, int x_sub,
int y_sub, int32_t* work); int y_add, int y_sub, int32_t* const work);
// Import a row of data and save its contribution in the rescaler. // Import a row of data and save its contribution in the rescaler.
void WebPRescalerImportRow(const uint8_t* const src, WebPRescaler* const wrk); // 'channel' denotes the channel number to be imported.
void WebPRescalerImportRow(const uint8_t* const src, int channel,
WebPRescaler* const wrk);
// Export a row from rescaler. // Export a row from rescaler.
void WebPRescalerExportRow(WebPRescaler* const wrk); void WebPRescalerExportRow(WebPRescaler* const wrk);