From fe7136ef8202b19fda262ea91adc09b3367d37d7 Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Tue, 28 May 2013 22:12:34 +0200 Subject: [PATCH] j2k/jpeg2000: split stepsize in float & int variables This is more clear and less prone to mistakes. Signed-off-by: Michael Niedermayer --- libavcodec/j2k.c | 18 ++++++++---------- libavcodec/j2k.h | 3 ++- libavcodec/j2kdec.c | 2 +- libavcodec/j2kenc.c | 4 ++-- libavcodec/jpeg2000.c | 18 +++++++++--------- libavcodec/jpeg2000.h | 5 ++--- libavcodec/jpeg2000dec.c | 4 ++-- 7 files changed, 26 insertions(+), 28 deletions(-) diff --git a/libavcodec/j2k.c b/libavcodec/j2k.c index 96fe2597cb..5a7949737b 100644 --- a/libavcodec/j2k.c +++ b/libavcodec/j2k.c @@ -248,7 +248,6 @@ int ff_j2k_init_component(Jpeg2000Component *comp, Jpeg2000Band *band = reslevel->band + bandno; int cblkno, precno; int nb_precincts; - double stepsize; /* TODO: Implementation of quantization step not finished, * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */ @@ -257,13 +256,13 @@ int ff_j2k_init_component(Jpeg2000Component *comp, int numbps; case JPEG2000_QSTY_NONE: /* TODO: to verify. No quantization in this case */ - stepsize = 1; + band->f_stepsize = 1; break; case JPEG2000_QSTY_SI: /*TODO: Compute formula to implement. */ numbps = cbps + lut_gain[codsty->transform][bandno + (reslevelno > 0)]; - stepsize = SHL(2048 + qntsty->mant[gbandno], + band->f_stepsize = SHL(2048 + qntsty->mant[gbandno], 2 + numbps - qntsty->expn[gbandno]); break; case JPEG2000_QSTY_SE: @@ -276,21 +275,20 @@ int ff_j2k_init_component(Jpeg2000Component *comp, * but it works (compared to OpenJPEG). Why? * Further investigation needed. */ gain = cbps; - stepsize = pow(2.0, gain - qntsty->expn[gbandno]); - stepsize *= (qntsty->mant[gbandno] / 2048.0 + 1.0); + band->f_stepsize = pow(2.0, gain - qntsty->expn[gbandno]); + band->f_stepsize *= (qntsty->mant[gbandno] / 2048.0 + 1.0); break; default: - stepsize = 0; + band->f_stepsize = 0; av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n"); break; } /* FIXME: In openjepg code stespize = stepsize * 0.5. Why? * If not set output of entropic decoder is not correct. */ if (!av_codec_is_encoder(avctx->codec)) - stepsize *= 0.5; - /* BITEXACT computing case --> convert to int */ -// if (avctx->flags & CODEC_FLAG_BITEXACT) - band->stepsize = stepsize * (1 << 16); + band->f_stepsize *= 0.5; + + band->i_stepsize = band->f_stepsize * (1 << 16); /* computation of tbx_0, tbx_1, tby_0, tby_1 * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1 diff --git a/libavcodec/j2k.h b/libavcodec/j2k.h index aacd006a1d..65ecc7ab8b 100644 --- a/libavcodec/j2k.h +++ b/libavcodec/j2k.h @@ -179,7 +179,8 @@ typedef struct Jpeg2000Prec { typedef struct Jpeg2000Band { uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} uint16_t log2_cblk_width, log2_cblk_height; - uint32_t stepsize; // quantization stepsize (* 2^13) + int i_stepsize; // quantization stepsize + float f_stepsize; // quantization stepsize Jpeg2000Prec *prec; } Jpeg2000Band; // subband diff --git a/libavcodec/j2kdec.c b/libavcodec/j2kdec.c index 42862b927a..228c045172 100644 --- a/libavcodec/j2kdec.c +++ b/libavcodec/j2kdec.c @@ -827,7 +827,7 @@ static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk, for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) { idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i; datap[idx] = - ((int32_t)(t1->data[j][i]) * ((int32_t)band->stepsize) + (1 << 15)) >> 16; + ((int32_t)(t1->data[j][i]) * band->i_stepsize + (1 << 15)) >> 16; } } diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c index 906ef61449..4b7228c187 100644 --- a/libavcodec/j2kenc.c +++ b/libavcodec/j2kenc.c @@ -802,7 +802,7 @@ static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile) Jpeg2000Cblk *cblk = prec->cblk + cblkno; cblk->ninclpasses = getcut(cblk, s->lambda, - (int64_t)dwt_norms[codsty->transform][bandpos][lev] * (int64_t)band->stepsize >> 16); + (int64_t)dwt_norms[codsty->transform][bandpos][lev] * (int64_t)band->i_stepsize >> 16); } } } @@ -863,7 +863,7 @@ static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno int *ptr = t1.data[y-yy0]; for (x = xx0; x < xx1; x++){ *ptr = (comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]); - *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->stepsize) >> 14 - NMSEDEC_FRACBITS; + *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 14 - NMSEDEC_FRACBITS; ptr++; } } diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c index 626a157d72..bcbb25c806 100644 --- a/libavcodec/jpeg2000.c +++ b/libavcodec/jpeg2000.c @@ -267,13 +267,13 @@ int ff_jpeg2000_init_component(Jpeg2000Component *comp, int numbps; case JPEG2000_QSTY_NONE: /* TODO: to verify. No quantization in this case */ - band->stepsize = (float) (1 << 13); + band->f_stepsize = (float) (1 << 13); break; case JPEG2000_QSTY_SI: /*TODO: Compute formula to implement. */ numbps = cbps + lut_gain[codsty->transform][bandno + (reslevelno > 0)]; - band->stepsize = (float)SHL(2048 + qntsty->mant[gbandno], + band->f_stepsize = (float)SHL(2048 + qntsty->mant[gbandno], 2 + numbps - qntsty->expn[gbandno]); break; case JPEG2000_QSTY_SE: @@ -286,20 +286,20 @@ int ff_jpeg2000_init_component(Jpeg2000Component *comp, * but it works (compared to OpenJPEG). Why? * Further investigation needed. */ gain = cbps; - band->stepsize = pow(2.0, gain - qntsty->expn[gbandno]); - band->stepsize *= (float)qntsty->mant[gbandno] / 2048.0 + 1.0; + band->f_stepsize = pow(2.0, gain - qntsty->expn[gbandno]); + band->f_stepsize *= (float)qntsty->mant[gbandno] / 2048.0 + 1.0; break; default: - band->stepsize = 0; + band->f_stepsize = 0; av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n"); break; } /* FIXME: In openjepg code stespize = stepsize * 0.5. Why? * If not set output of entropic decoder is not correct. */ - band->stepsize *= 0.5; - /* BITEXACT computing case --> convert to int */ - if (avctx->flags & CODEC_FLAG_BITEXACT) - band->stepsize = (int32_t)(band->stepsize * (1 << 16)); + if (!av_codec_is_encoder(avctx->codec)) + band->f_stepsize *= 0.5; + + band->i_stepsize = (int32_t)(band->f_stepsize * (1 << 16)); /* computation of tbx_0, tbx_1, tby_0, tby_1 * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1 diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h index 22b8927b80..274bae96c5 100644 --- a/libavcodec/jpeg2000.h +++ b/libavcodec/jpeg2000.h @@ -176,12 +176,11 @@ typedef struct Jpeg2000Prec { uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} } Jpeg2000Prec; // precinct -/* TODO: stepsize can be float or integer depending on - * reversible or irreversible transformation. */ typedef struct Jpeg2000Band { uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} uint16_t log2_cblk_width, log2_cblk_height; - float stepsize; // quantization stepsize + int i_stepsize; // quantization stepsize + float f_stepsize; // quantization stepsize Jpeg2000Prec *prec; } Jpeg2000Band; // subband diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index da446f158b..ccb35cc77a 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -930,7 +930,7 @@ static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk, for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) { idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i; - datap[idx] = (float)(t1->data[j][i]) * ((float)band->stepsize); + datap[idx] = (float)(t1->data[j][i]) * band->f_stepsize; } } @@ -946,7 +946,7 @@ static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk, for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) { idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i; datap[idx] = - ((int32_t)(t1->data[j][i]) * ((int32_t)band->stepsize) + (1 << 15)) >> 16; + ((int32_t)(t1->data[j][i]) * band->i_stepsize + (1 << 15)) >> 16; } }