From 4e11b155345101bac6185b48180e6f36c4850c8b Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Mon, 1 Jul 2013 10:01:18 +0200 Subject: [PATCH 1/3] jpeg2000: Calculate code-block coord in ff_jpeg2000_init_component() Signed-off-by: Luca Barbato Signed-off-by: Nicolas Bertrand Signed-off-by: Luca Barbato --- libavcodec/jpeg2000.c | 14 ++++++++++++++ libavcodec/jpeg2000dec.c | 9 --------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c index b1236993ac..b3182d63b0 100644 --- a/libavcodec/jpeg2000.c +++ b/libavcodec/jpeg2000.c @@ -443,6 +443,20 @@ int ff_jpeg2000_init_component(Jpeg2000Component *comp, /* Compute Cy1 */ cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height), prec->coord[1][1]); + /* Update code-blocks coordinates according sub-band position */ + if ((bandno + !!reslevelno) & 1) { + cblk->coord[0][0] += comp->reslevel[reslevelno-1].coord[0][1] - + comp->reslevel[reslevelno-1].coord[0][0]; + cblk->coord[0][1] += comp->reslevel[reslevelno-1].coord[0][1] - + comp->reslevel[reslevelno-1].coord[0][0]; + } + if ((bandno + !!reslevelno) & 2) { + cblk->coord[1][0] += comp->reslevel[reslevelno-1].coord[1][1] - + comp->reslevel[reslevelno-1].coord[1][0]; + cblk->coord[1][1] += comp->reslevel[reslevelno-1].coord[1][1] - + comp->reslevel[reslevelno-1].coord[1][0]; + } + cblk->zero = 0; cblk->lblock = 3; cblk->length = 0; diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index 2abba868a4..9befa8b31d 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -1119,17 +1119,8 @@ static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, cblk->coord[1][1] - cblk->coord[1][0], bandpos); - /* Manage band offsets */ x = cblk->coord[0][0]; y = cblk->coord[1][0]; - if ((reslevelno > 0) && ((bandno + 1) & 1)) { - Jpeg2000ResLevel *pres = comp->reslevel + (reslevelno - 1); - x += pres->coord[0][1] - pres->coord[0][0]; - } - if ((reslevelno > 0) && ((bandno + 1) & 2)) { - Jpeg2000ResLevel *pres = comp->reslevel + (reslevelno - 1); - y += pres->coord[1][1] - pres->coord[1][0]; - } if (s->avctx->flags & CODEC_FLAG_BITEXACT) dequantization_int(x, y, cblk, comp, &t1, band); From 952f7ed3c0b036c1a53834cf2015072a90a1f0ae Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Mon, 1 Jul 2013 10:01:19 +0200 Subject: [PATCH 2/3] jpeg2000: Compute quantization for 'scalar derived' in the correct case. Scalar derived case is represented by the JPEG2K_QSTY_SI define. Signed-off-by: Luca Barbato --- libavcodec/jpeg2000.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c index b3182d63b0..54550bc3b2 100644 --- a/libavcodec/jpeg2000.c +++ b/libavcodec/jpeg2000.c @@ -284,14 +284,14 @@ int ff_jpeg2000_init_component(Jpeg2000Component *comp, int numbps; case JPEG2000_QSTY_NONE: /* TODO: to verify. No quantization in this case */ - numbps = cbps + - lut_gain[codsty->transform][bandno + (reslevelno > 0)]; - band->stepsize = (float)SHL(2048 + qntsty->mant[gbandno], - 2 + numbps - qntsty->expn[gbandno]); + band->f_stepsize = 1; break; case JPEG2000_QSTY_SI: /*TODO: Compute formula to implement. */ - band->stepsize = (float) (1 << 13); + numbps = cbps + + lut_gain[codsty->transform == FF_DWT53][bandno + (reslevelno > 0)]; + band->stepsize = (float)SHL(2048 + qntsty->mant[gbandno], + 2 + numbps - qntsty->expn[gbandno]); break; case JPEG2000_QSTY_SE: /* Exponent quantization step. @@ -305,15 +305,15 @@ int ff_jpeg2000_init_component(Jpeg2000Component *comp, gain = cbps; band->stepsize = pow(2.0, gain - qntsty->expn[gbandno]); band->stepsize *= (float)qntsty->mant[gbandno] / 2048.0 + 1.0; - /* FIXME: In openjepg code stespize = stepsize * 0.5. Why? - * If not set output of entropic decoder is not correct. */ - band->stepsize *= 0.5; break; default: band->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)); From f9581f1414ec8e3e7d0868793cda4f2e4fa113ea Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Mon, 1 Jul 2013 10:01:20 +0200 Subject: [PATCH 3/3] jpeg2000: Improve reduced resolution decoding Correctly scale down the component coordinates and clean up some redundant code. Signed-off-by: Luca Barbato --- libavcodec/jpeg2000dec.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index 9befa8b31d..47b07ebb67 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -84,8 +84,7 @@ typedef struct Jpeg2000DecoderContext { Jpeg2000Tile *tile; /*options parameters*/ - int16_t lowres; - int16_t reduction_factor; + int reduction_factor; } Jpeg2000DecoderContext; /* get_bits functions for JPEG2000 packet bitstream @@ -599,12 +598,10 @@ static int init_tile(Jpeg2000DecoderContext *s, int tileno) comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y); comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height); - // FIXME: add a dcinema profile check ? - // value is guaranteed by profile (orig=0, 1 tile) - comp->coord[0][0] = 0; - comp->coord[0][1] = s->avctx->width; - comp->coord[1][0] = 0; - comp->coord[1][1] = s->avctx->height; + comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor); + comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor); + comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor); + comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor); if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], @@ -1351,9 +1348,6 @@ static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data, bytestream2_init(&s->g, avpkt->data, avpkt->size); s->curtileno = 0; // TODO: only one tile in DCI JP2K. to implement for more tiles - // reduction factor, i.e number of resolution levels to skip - s->reduction_factor = s->lowres; - if (bytestream2_get_bytes_left(&s->g) < 2) return AVERROR_INVALIDDATA; @@ -1413,7 +1407,7 @@ static void jpeg2000_init_static_data(AVCodec *codec) static const AVOption options[] = { { "lowres", "Lower the decoding resolution by a power of two", - OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD }, + OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD }, { NULL }, };