speed-up GetResidualCost()

+ misc cosmetics and code polishing

Change-Id: I5830cd2f268d64c072b1cbccc0a4674833875055
This commit is contained in:
Pascal Massimino
2012-01-23 00:58:09 -08:00
parent 378086bd0a
commit 28a9d9b41a
10 changed files with 163 additions and 144 deletions

View File

@ -33,7 +33,7 @@ static int IsValidColorspace(int webp_csp_mode) {
static VP8StatusCode CheckDecBuffer(const WebPDecBuffer* const buffer) {
int ok = 1;
WEBP_CSP_MODE mode = buffer->colorspace;
const WEBP_CSP_MODE mode = buffer->colorspace;
const int width = buffer->width;
const int height = buffer->height;
if (!IsValidColorspace(mode)) {
@ -65,22 +65,21 @@ static VP8StatusCode CheckDecBuffer(const WebPDecBuffer* const buffer) {
static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) {
const int w = buffer->width;
const int h = buffer->height;
const WEBP_CSP_MODE mode = buffer->colorspace;
if (w <= 0 || h <= 0 || !IsValidColorspace(buffer->colorspace)) {
if (w <= 0 || h <= 0 || !IsValidColorspace(mode)) {
return VP8_STATUS_INVALID_PARAM;
}
if (!buffer->is_external_memory && buffer->private_memory == NULL) {
uint8_t* output;
WEBP_CSP_MODE mode = buffer->colorspace;
int stride;
int uv_stride = 0, a_stride = 0;
int uv_size = 0;
uint64_t size, a_size = 0, total_size;
uint64_t a_size = 0, total_size;
// We need memory and it hasn't been allocated yet.
// => initialize output buffer, now that dimensions are known.
stride = w * kModeBpp[mode];
size = (uint64_t)stride * h;
const int stride = w * kModeBpp[mode];
const uint64_t size = (uint64_t)stride * h;
if (mode >= MODE_YUV) {
uv_stride = (w + 1) / 2;
@ -150,7 +149,7 @@ VP8StatusCode WebPAllocateDecBuffer(int w, int h,
if (options->scaled_width <= 0 || options->scaled_height <= 0) {
return VP8_STATUS_INVALID_PARAM;
}
w = options->scaled_width;
w = options->scaled_width;
h = options->scaled_height;
}
}
@ -166,13 +165,13 @@ VP8StatusCode WebPAllocateDecBuffer(int w, int h,
int WebPInitDecBufferInternal(WebPDecBuffer* const buffer, int version) {
if (version != WEBP_DECODER_ABI_VERSION) return 0; // version mismatch
if (!buffer) return 0;
if (buffer == NULL) return 0;
memset(buffer, 0, sizeof(*buffer));
return 1;
}
void WebPFreeDecBuffer(WebPDecBuffer* const buffer) {
if (buffer) {
if (buffer != NULL) {
if (!buffer->is_external_memory)
free(buffer->private_memory);
buffer->private_memory = NULL;
@ -181,9 +180,9 @@ void WebPFreeDecBuffer(WebPDecBuffer* const buffer) {
void WebPCopyDecBuffer(const WebPDecBuffer* const src,
WebPDecBuffer* const dst) {
if (src && dst) {
if (src != NULL && dst != NULL) {
*dst = *src;
if (src->private_memory) {
if (src->private_memory != NULL) {
dst->is_external_memory = 1; // dst buffer doesn't own the memory.
dst->private_memory = NULL;
}
@ -192,9 +191,9 @@ void WebPCopyDecBuffer(const WebPDecBuffer* const src,
// Copy and transfer ownership from src to dst (beware of parameter order!)
void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst) {
if (src && dst) {
if (src != NULL && dst != NULL) {
*dst = *src;
if (src->private_memory) {
if (src->private_memory != NULL) {
src->is_external_memory = 1; // src relinquishes ownership
src->private_memory = NULL;
}

View File

@ -78,24 +78,24 @@ static const uint8_t kFilterExtraRows[3] = { 0, 2, 8 };
static int AllocateMemory(VP8Decoder* const dec) {
const int num_caches = dec->num_caches_;
const int mb_w = dec->mb_w_;
const int intra_pred_mode_size = 4 * mb_w * sizeof(uint8_t);
const int top_size = (16 + 8 + 8) * mb_w;
const int mb_info_size = (mb_w + 1) * sizeof(VP8MB);
const int f_info_size =
(dec->filter_type_ > 0) ?
mb_w * (dec->use_threads_ ? 2 : 1) * sizeof(VP8FInfo)
: 0;
const int yuv_size = YUV_SIZE * sizeof(*dec->yuv_b_);
const int coeffs_size = 384 * sizeof(*dec->coeffs_);
const int cache_height = (16 * num_caches
+ kFilterExtraRows[dec->filter_type_]) * 3 / 2;
const int cache_size = top_size * cache_height;
const int alpha_size =
dec->alpha_data_ ? (dec->pic_hdr_.width_ * dec->pic_hdr_.height_) : 0;
const int needed = intra_pred_mode_size
+ top_size + mb_info_size + f_info_size
+ yuv_size + coeffs_size
+ cache_size + alpha_size + ALIGN_MASK;
const size_t intra_pred_mode_size = 4 * mb_w * sizeof(uint8_t);
const size_t top_size = (16 + 8 + 8) * mb_w;
const size_t mb_info_size = (mb_w + 1) * sizeof(VP8MB);
const size_t f_info_size =
(dec->filter_type_ > 0) ?
mb_w * (dec->use_threads_ ? 2 : 1) * sizeof(VP8FInfo)
: 0;
const size_t yuv_size = YUV_SIZE * sizeof(*dec->yuv_b_);
const size_t coeffs_size = 384 * sizeof(*dec->coeffs_);
const size_t cache_height = (16 * num_caches
+ kFilterExtraRows[dec->filter_type_]) * 3 / 2;
const size_t cache_size = top_size * cache_height;
const size_t alpha_size =
dec->alpha_data_ ? (dec->pic_hdr_.width_ * dec->pic_hdr_.height_) : 0;
const size_t needed = intra_pred_mode_size
+ top_size + mb_info_size + f_info_size
+ yuv_size + coeffs_size
+ cache_size + alpha_size + ALIGN_MASK;
uint8_t* mem;
if (needed > dec->mem_size_) {

View File

@ -32,11 +32,12 @@ static int EmitYUV(const VP8Io* const io, WebPDecParams* const p) {
const int mb_w = io->mb_w;
const int mb_h = io->mb_h;
const int uv_w = (mb_w + 1) / 2;
const int uv_h = (mb_h + 1) / 2;
int j;
for (j = 0; j < mb_h; ++j) {
memcpy(y_dst + j * buf->y_stride, io->y + j * io->y_stride, mb_w);
}
for (j = 0; j < (mb_h + 1) / 2; ++j) {
for (j = 0; j < uv_h; ++j) {
memcpy(u_dst + j * buf->u_stride, io->u + j * io->uv_stride, uv_w);
memcpy(v_dst + j * buf->v_stride, io->v + j * io->uv_stride, uv_w);
}
@ -127,7 +128,7 @@ static int EmitFancyRGB(const VP8Io* const io, WebPDecParams* const p) {
// are not lagging one line behind but are already written.
upsample(p->tmp_y, cur_y, top_u, top_v, cur_u, cur_v,
dst - buf->stride, dst, mb_w);
num_lines_out++;
++num_lines_out;
}
// Loop over each output pairs of row.
for (; y + 2 < y_end; y += 2) {
@ -166,13 +167,13 @@ static int EmitFancyRGB(const VP8Io* const io, WebPDecParams* const p) {
//------------------------------------------------------------------------------
static int EmitAlphaYUV(const VP8Io* const io, WebPDecParams* const p) {
const int mb_w = io->mb_w;
const int mb_h = io->mb_h;
int j;
const WebPYUVABuffer* const buf = &p->output->u.YUVA;
uint8_t* dst = buf->a + io->mb_y * buf->a_stride;
const uint8_t* alpha = io->a;
if (alpha) {
if (alpha != NULL) {
int j;
const int mb_w = io->mb_w;
const int mb_h = io->mb_h;
const WebPYUVABuffer* const buf = &p->output->u.YUVA;
uint8_t* dst = buf->a + io->mb_y * buf->a_stride;
for (j = 0; j < mb_h; ++j) {
memcpy(dst, alpha, mb_w * sizeof(*dst));
alpha += io->width;
@ -331,7 +332,7 @@ static int Rescale(const uint8_t* src, int src_stride,
wrk->y_accum -= wrk->y_sub;
while (wrk->y_accum <= 0) { // emit output row(s)
ExportRow(wrk);
num_lines_out++;
++num_lines_out;
}
}
return num_lines_out;
@ -347,7 +348,7 @@ static int EmitRescaledYUV(const VP8Io* const io, WebPDecParams* const p) {
}
static int EmitRescaledAlphaYUV(const VP8Io* const io, WebPDecParams* const p) {
if (io->a) {
if (io->a != NULL) {
Rescale(io->a, io->width, io->mb_h, &p->scaler_a);
}
return 0;
@ -440,7 +441,7 @@ static int ExportRGB(WebPDecParams* const p, int y_pos) {
convert(p->scaler_y.dst, p->scaler_u.dst, p->scaler_v.dst,
dst, p->scaler_y.dst_width);
dst += buf->stride;
num_lines_out++;
++num_lines_out;
}
return num_lines_out;
}
@ -479,7 +480,7 @@ static int ExportAlpha(WebPDecParams* const p, int y_pos) {
dst[4 * i] = p->scaler_a.dst[i];
}
dst += buf->stride;
num_lines_out++;
++num_lines_out;
}
return num_lines_out;
}
@ -498,18 +499,21 @@ static int ExportAlphaRGBA4444(WebPDecParams* const p, int y_pos) {
dst[2 * i] = (dst[2 * i] & 0xf0) | alpha_val;
}
dst += buf->stride;
num_lines_out++;
++num_lines_out;
}
return num_lines_out;
}
static int EmitRescaledAlphaRGB(const VP8Io* const io, WebPDecParams* const p) {
if (io->a) {
if (io->a != NULL) {
int (* const output_func)(WebPDecParams* const, int) =
(p->output->colorspace == MODE_RGBA_4444) ? ExportAlphaRGBA4444
: ExportAlpha;
WebPRescaler* const scaler = &p->scaler_a;
int j = 0, pos = 0;
while (j < io->mb_h) {
j += Import(io->a + j * io->width, io->width, io->mb_h - j, &p->scaler_a);
pos += (p->output->colorspace == MODE_RGBA_4444) ?
ExportAlphaRGBA4444(p, pos) : ExportAlpha(p, pos);
j += Import(io->a + j * io->width, io->width, io->mb_h - j, scaler);
pos += output_func(p, pos);
}
}
return 0;

View File

@ -250,7 +250,7 @@ struct VP8Decoder {
// main memory chunk for the above data. Persistent.
void* mem_;
int mem_size_;
size_t mem_size_;
// Per macroblock non-persistent infos.
int mb_x_, mb_y_; // current position, in macroblock units