]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/j2kdec.c
avcodec/vp8: Cosmetics, maintain alphabetical order in threading headers
[ffmpeg] / libavcodec / j2kdec.c
index 0c88ba9423852dddc0b0141feef58f14c055a0a0..a1fd5558d67051e0ffd175d8964a3e2f750d590d 100644 (file)
  * @author Kamil Nowosad
  */
 
+#include "libavutil/common.h"
+#include "libavutil/opt.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "internal.h"
 #include "thread.h"
 #include "j2k.h"
-#include "libavutil/common.h"
 
 #define JP2_SIG_TYPE    0x6A502020
 #define JP2_SIG_VALUE   0x0D0A870A
 #define HAD_QCC 0x02
 
 typedef struct Jpeg2000Tile {
-   Jpeg2000Component *comp;
-   uint8_t properties[4];
-   Jpeg2000CodingStyle codsty[4];
-   Jpeg2000QuantStyle  qntsty[4];
+    Jpeg2000Component  *comp;
+    uint8_t             properties[4];
+    Jpeg2000CodingStyle codsty[4];
+    Jpeg2000QuantStyle  qntsty[4];
 } Jpeg2000Tile;
 
 typedef struct Jpeg2000DecoderContext {
@@ -74,8 +75,16 @@ typedef struct Jpeg2000DecoderContext {
     int             curtileno;
 
     Jpeg2000Tile    *tile;
+
+    /*options parameters*/
+    int             lowres;
+    int             reduction_factor;
 } Jpeg2000DecoderContext;
 
+/* get_bits functions for JPEG2000 packet bitstream
+ * It is a get_bit function with a bit-stuffing routine. If the value of the
+ * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
+ * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
 static int get_bits(Jpeg2000DecoderContext *s, int n)
 {
     int res = 0;
@@ -98,8 +107,9 @@ static void j2k_flush(Jpeg2000DecoderContext *s)
     s->bit_index = 8;
 }
 
-/** decode the value stored in node */
-static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node, int threshold)
+/* decode the value stored in node */
+static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
+                           int threshold)
 {
     Jpeg2000TgtNode *stack[30];
     int sp = -1, curval = 0;
@@ -109,7 +119,7 @@ static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node, int
 
     while (node && !node->vis) {
         stack[++sp] = node;
-        node = node->parent;
+        node        = node->parent;
     }
 
     if (node)
@@ -169,34 +179,39 @@ static int get_siz(Jpeg2000DecoderContext *s)
 
     for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
         uint8_t x = bytestream2_get_byteu(&s->g);
-        s->cbps[i] = (x & 0x7f) + 1;
+        s->cbps[i]   = (x & 0x7f) + 1;
         s->precision = FFMAX(s->cbps[i], s->precision);
         s->sgnd[i] = !!(x & 0x80);
         s->cdx[i] = bytestream2_get_byteu(&s->g);
         s->cdy[i] = bytestream2_get_byteu(&s->g);
+        if (s->cdx[i] != 1 || s->cdy[i] != 1) {
+            av_log(s->avctx, AV_LOG_ERROR, "unsupported/ CDxy values\n");
+        }
     }
 
-    s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
+    s->numXtiles = ff_jpeg2000_ceildiv(s->width  - s->tile_offset_x, s->tile_width);
     s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
 
     if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(Jpeg2000Tile))
         return AVERROR(EINVAL);
 
-    s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(Jpeg2000Tile));
+    s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(*s->tile));
     if (!s->tile)
         return AVERROR(ENOMEM);
 
     for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
         Jpeg2000Tile *tile = s->tile + i;
 
-        tile->comp = av_mallocz(s->ncomponents * sizeof(Jpeg2000Component));
+        tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
         if (!tile->comp)
             return AVERROR(ENOMEM);
     }
 
-    s->avctx->width  = s->width  - s->image_offset_x;
-    s->avctx->height = s->height - s->image_offset_y;
-
+    /* compute image size with reduction factor */
+    s->avctx->width  = ff_jpeg2000_ceildivpow2(s->width  - s->image_offset_x,
+                                               s->reduction_factor);
+    s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
+                                               s->reduction_factor);
     switch(s->ncomponents) {
     case 1:
         if (s->precision > 8) {
@@ -227,14 +242,33 @@ static int get_siz(Jpeg2000DecoderContext *s)
     return 0;
 }
 
-/** get common part for COD and COC segments */
+/* get common part for COD and COC segments */
 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
 {
+    uint8_t byte;
+
     if (bytestream2_get_bytes_left(&s->g) < 5)
         return AVERROR(EINVAL);
-          c->nreslevels = bytestream2_get_byteu(&s->g) + 1; // num of resolution levels - 1
-     c->log2_cblk_width = bytestream2_get_byteu(&s->g) + 2; // cblk width
-    c->log2_cblk_height = bytestream2_get_byteu(&s->g) + 2; // cblk height
+    c->nreslevels = bytestream2_get_byteu(&s->g) + 1; // num of resolution levels - 1
+    if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
+        av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
+        return AVERROR_INVALIDDATA;
+    }
+
+    /* compute number of resolution levels to decode */
+    if (c->nreslevels < s->reduction_factor)
+        c->nreslevels2decode = 1;
+    else
+        c->nreslevels2decode = c->nreslevels - s->reduction_factor;
+
+    c->log2_cblk_width  = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
+    c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
+
+    if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
+        c->log2_cblk_width + c->log2_cblk_height > 14) {
+        av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
+        return AVERROR_INVALIDDATA;
+    }
 
     c->cblk_style = bytestream2_get_byteu(&s->g);
     if (c->cblk_style != 0) { // cblk style
@@ -243,15 +277,21 @@ static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
     c->transform = bytestream2_get_byteu(&s->g); // transformation
     if (c->csty & JPEG2000_CSTY_PREC) {
         int i;
-
-        for (i = 0; i < c->nreslevels; i++)
-            bytestream2_get_byte(&s->g);
+        for (i = 0; i < c->nreslevels; i++) {
+            byte = bytestream2_get_byte(&s->g);
+            c->log2_prec_widths[i]  =  byte       & 0x0F;    // precinct PPx
+            c->log2_prec_heights[i] = (byte >> 4) & 0x0F;    // precinct PPy
+        }
+    } else {
+        memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
+        memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
     }
     return 0;
 }
 
-/** get coding parameters for a particular tile or whole image*/
-static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, uint8_t *properties)
+/* get coding parameters for a particular tile or whole image*/
+static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
+                   uint8_t *properties)
 {
     Jpeg2000CodingStyle tmp;
     int compno;
@@ -259,9 +299,6 @@ static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, uint8_t *p
     if (bytestream2_get_bytes_left(&s->g) < 5)
         return AVERROR(EINVAL);
 
-    tmp.log2_prec_width  =
-    tmp.log2_prec_height = 15;
-
     tmp.csty = bytestream2_get_byteu(&s->g);
 
     // get progression order
@@ -271,18 +308,19 @@ static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, uint8_t *p
     }
 
     tmp.nlayers = bytestream2_get_be16u(&s->g);
-        tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
+    tmp.mct     = bytestream2_get_byteu(&s->g); // multiple component transformation
 
     get_cox(s, &tmp);
-    for (compno = 0; compno < s->ncomponents; compno++) {
+    for (compno = 0; compno < s->ncomponents; compno++)
         if (!(properties[compno] & HAD_COC))
-            memcpy(c + compno, &tmp, sizeof(Jpeg2000CodingStyle));
-    }
+            memcpy(c + compno, &tmp, sizeof(tmp));
     return 0;
 }
 
-/** get coding parameters for a component in the whole image on a particular tile */
-static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, uint8_t *properties)
+/* Get coding parameters for a component in the whole image or a
+ * particular tile. */
+static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
+                   uint8_t *properties)
 {
     int compno;
 
@@ -292,14 +330,14 @@ static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, uint8_t *p
     compno = bytestream2_get_byteu(&s->g);
 
     c += compno;
-    c->csty = bytestream2_get_byte(&s->g);
+    c->csty = bytestream2_get_byteu(&s->g);
     get_cox(s, c);
 
     properties[compno] |= HAD_COC;
     return 0;
 }
 
-/** get common part for QCD and QCC segments */
+/* Get common part for QCD and QCC segments. */
 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
 {
     int i, x;
@@ -310,7 +348,7 @@ static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
     x = bytestream2_get_byteu(&s->g); // Sqcd
 
     q->nguardbits = x >> 5;
-      q->quantsty = x & 0x1f;
+    q->quantsty   = x & 0x1f;
 
     if (q->quantsty == JPEG2000_QSTY_NONE) {
         n -= 3;
@@ -321,20 +359,20 @@ static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
     } else if (q->quantsty == JPEG2000_QSTY_SI) {
         if (bytestream2_get_bytes_left(&s->g) < 2)
             return AVERROR(EINVAL);
-        x = bytestream2_get_be16u(&s->g);
+        x          = bytestream2_get_be16u(&s->g);
         q->expn[0] = x >> 11;
         q->mant[0] = x & 0x7ff;
         for (i = 1; i < 32 * 3; i++) {
-            int curexpn = FFMAX(0, q->expn[0] - (i-1)/3);
+            int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
             q->expn[i] = curexpn;
             q->mant[i] = q->mant[0];
         }
-    } else{
+    } else {
         n = (n - 3) >> 1;
         if (bytestream2_get_bytes_left(&s->g) < 2 * n || 32*3 < n)
             return AVERROR(EINVAL);
         for (i = 0; i < n; i++) {
-            x = bytestream2_get_be16u(&s->g);
+            x          = bytestream2_get_be16u(&s->g);
             q->expn[i] = x >> 11;
             q->mant[i] = x & 0x7ff;
         }
@@ -342,8 +380,9 @@ static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
     return 0;
 }
 
-/** get quantization parameters for a particular tile or a whole image */
-static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, uint8_t *properties)
+/* Get quantization parameters for a particular tile or a whole image. */
+static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
+                   uint8_t *properties)
 {
     Jpeg2000QuantStyle tmp;
     int compno;
@@ -352,21 +391,23 @@ static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, uint
         return -1;
     for (compno = 0; compno < s->ncomponents; compno++)
         if (!(properties[compno] & HAD_QCC))
-            memcpy(q + compno, &tmp, sizeof(Jpeg2000QuantStyle));
+            memcpy(q + compno, &tmp, sizeof(tmp));
     return 0;
 }
 
-/** get quantization parameters for a component in the whole image on in a particular tile */
-static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, uint8_t *properties)
+/* Get quantization parameters for a component in the whole image
+ * on in a particular tile. */
+static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
+                   uint8_t *properties)
 {
     int compno;
 
     if (bytestream2_get_bytes_left(&s->g) < 1)
         return AVERROR(EINVAL);
 
-    compno = bytestream2_get_byteu(&s->g);
+    compno              = bytestream2_get_byteu(&s->g);
     properties[compno] |= HAD_QCC;
-    return get_qcx(s, n-1, q+compno);
+    return get_qcx(s, n - 1, q + compno);
 }
 
 /** get start of tile segment */
@@ -395,11 +436,53 @@ static int get_sot(Jpeg2000DecoderContext *s)
     return 0;
 }
 
+/* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
+ * Used to know the number of tile parts and lengths.
+ * There may be multiple TLMs in the header.
+ * TODO: The function is not used for tile-parts management, nor anywhere else.
+ * It can be useful to allocate memory for tile parts, before managing the SOT
+ * markers. Parsing the TLM header is needed to increment the input header
+ * buffer.
+ * This marker is mandatory for DCI. */
+static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
+{
+    uint8_t Stlm, ST, SP, tile_tlm, i;
+    bytestream2_get_byte(&s->g);               /* Ztlm: skipped */
+    Stlm = bytestream2_get_byte(&s->g);
+
+    // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
+    ST = (Stlm >> 4) & 0x03;
+    // TODO: Manage case of ST = 0b11 --> raise error
+    SP       = (Stlm >> 6) & 0x01;
+    tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
+    for (i = 0; i < tile_tlm; i++) {
+        switch (ST) {
+        case 0:
+            break;
+        case 1:
+            bytestream2_get_byte(&s->g);
+            break;
+        case 2:
+            bytestream2_get_be16(&s->g);
+            break;
+        case 3:
+            bytestream2_get_be32(&s->g);
+            break;
+        }
+        if (SP == 0) {
+            bytestream2_get_be16(&s->g);
+        } else {
+            bytestream2_get_be32(&s->g);
+        }
+    }
+    return 0;
+}
+
 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
 {
-    int compno,
-        tilex = tileno % s->numXtiles,
-        tiley = tileno / s->numXtiles;
+    int compno;
+    int tilex = tileno % s->numXtiles;
+    int tiley = tileno / s->numXtiles;
     Jpeg2000Tile *tile = s->tile + tileno;
 
     if (!tile->comp)
@@ -410,12 +493,17 @@ static int init_tile(Jpeg2000DecoderContext *s, int tileno)
         Jpeg2000QuantStyle  *qntsty = tile->qntsty + compno;
         int ret; // global bandno
 
-        comp->coord[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
-        comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width + s->tile_offset_x, s->width);
-        comp->coord[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
-        comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height + s->tile_offset_y, s->height);
+        comp->coord_o[0][0] = FFMAX(tilex       * s->tile_width  + s->tile_offset_x, s->image_offset_x);
+        comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width  + s->tile_offset_x, s->width);
+        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);
 
-        if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], s->cdy[compno]))
+        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_j2k_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], s->cdy[compno], s->avctx))
             return ret;
     }
     return 0;
@@ -448,10 +536,12 @@ static int getlblockinc(Jpeg2000DecoderContext *s)
     return res;
 }
 
-static int decode_packet(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000ResLevel *rlevel, int precno,
+static int decode_packet(Jpeg2000DecoderContext *s,
+                         Jpeg2000CodingStyle *codsty,
+                         Jpeg2000ResLevel *rlevel, int precno,
                          int layno, uint8_t *expn, int numgbits)
 {
-    int bandno, cblkny, cblknx, cblkno, ret;
+    int bandno, cblkno, ret, nb_code_blocks;
 
     if (!(ret = get_bits(s, 1))) {
         j2k_flush(s);
@@ -462,64 +552,71 @@ static int decode_packet(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
         Jpeg2000Band *band = rlevel->band + bandno;
         Jpeg2000Prec *prec = band->prec + precno;
-        int pos = 0;
 
-        if (band->coord[0][0] == band->coord[0][1]
-        ||  band->coord[1][0] == band->coord[1][1])
+        if (band->coord[0][0] == band->coord[0][1] ||
+            band->coord[1][0] == band->coord[1][1])
             continue;
 
-        for (cblkny = prec->yi0; cblkny < prec->yi1; cblkny++)
-            for (cblknx = prec->xi0, cblkno = cblkny * band->cblknx + cblknx; cblknx < prec->xi1; cblknx++, cblkno++, pos++) {
-                Jpeg2000Cblk *cblk = band->cblk + cblkno;
-                int incl, newpasses, llen;
+        nb_code_blocks =  prec->nb_codeblocks_height *
+                          prec->nb_codeblocks_width;
+        for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
+            Jpeg2000Cblk *cblk = prec->cblk + cblkno;
+            int incl, newpasses, llen;
 
-                if (cblk->npasses)
-                    incl = get_bits(s, 1);
-                else
-                    incl = tag_tree_decode(s, prec->cblkincl + pos, layno+1) == layno;
-                if (!incl)
-                    continue;
-                else if (incl < 0)
-                    return incl;
-
-                if (!cblk->npasses)
-                    cblk->nonzerobits = expn[bandno] + numgbits - 1 - tag_tree_decode(s, prec->zerobits + pos, 100);
-                if ((newpasses = getnpasses(s)) < 0)
-                    return newpasses;
-                if ((llen = getlblockinc(s)) < 0)
-                    return llen;
-                cblk->lblock += llen;
-                if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
-                    return ret;
-                cblk->lengthinc = ret;
-                cblk->npasses += newpasses;
-            }
+            if (cblk->npasses)
+                incl = get_bits(s, 1);
+            else
+                incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
+            if (!incl)
+                continue;
+            else if (incl < 0)
+                return incl;
+
+            if (!cblk->npasses)
+                cblk->nonzerobits = expn[bandno] + numgbits - 1 -
+                                    tag_tree_decode(s, prec->zerobits + cblkno,
+                                                    100);
+            if ((newpasses = getnpasses(s)) < 0)
+                return newpasses;
+            if ((llen = getlblockinc(s)) < 0)
+                return llen;
+            cblk->lblock += llen;
+            if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
+                return ret;
+            cblk->lengthinc = ret;
+            cblk->npasses  += newpasses;
+        }
     }
     j2k_flush(s);
 
     if (codsty->csty & JPEG2000_CSTY_EPH) {
-        if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH) {
+        if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
             bytestream2_skip(&s->g, 2);
-        } else {
+        else
             av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
-        }
     }
 
     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
         Jpeg2000Band *band = rlevel->band + bandno;
-        int yi, cblknw = band->prec[precno].xi1 - band->prec[precno].xi0;
-        for (yi = band->prec[precno].yi0; yi < band->prec[precno].yi1; yi++) {
-            int xi;
-            for (xi = band->prec[precno].xi0; xi < band->prec[precno].xi1; xi++) {
-                Jpeg2000Cblk *cblk = band->cblk + yi * cblknw + xi;
-                if (   bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
-                    || sizeof(cblk->data) < cblk->lengthinc
-                )
-                    return AVERROR(EINVAL);
-                bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
-                cblk->length += cblk->lengthinc;
-                cblk->lengthinc = 0;
+        Jpeg2000Prec *prec = band->prec + precno;
+
+        nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
+        for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
+            Jpeg2000Cblk *cblk = prec->cblk + cblkno;
+            if (   bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
+                || sizeof(cblk->data) < cblk->lengthinc
+            )
+                return AVERROR(EINVAL);
+            /* Code-block data can be empty. In that case initialize data
+             * with 0xFFFF. */
+            if (cblk->lengthinc > 0) {
+                 bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
+            } else {
+                cblk->data[0] = 0xFF;
+                cblk->data[1] = 0xFF;
             }
+            cblk->length   += cblk->lengthinc;
+            cblk->lengthinc = 0;
         }
     }
     return 0;
@@ -537,11 +634,15 @@ static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile
                 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
                 Jpeg2000QuantStyle  *qntsty = tile->qntsty + compno;
                 if (reslevelno < codsty->nreslevels) {
-                    Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
+                    Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
+                                               reslevelno;
                     ok_reslevel = 1;
                     for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
-                        if (decode_packet(s, codsty, rlevel, precno, layno, qntsty->expn +
-                                          (reslevelno ? 3*(reslevelno-1)+1 : 0), qntsty->nguardbits))
+                        if (decode_packet(s,
+                                          codsty, rlevel,
+                                          precno, layno,
+                                          qntsty->expn + (reslevelno ? 3*(reslevelno-1)+1 : 0),
+                                          qntsty->nguardbits))
                             return -1;
                     }
                 }
@@ -552,14 +653,15 @@ static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile
 }
 
 /* TIER-1 routines */
-static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bpno, int bandno, int bpass_csty_symbol,
+static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
+                           int bpno, int bandno, int bpass_csty_symbol,
                            int vert_causal_ctx_csty_symbol)
 {
     int mask = 3 << (bpno - 1), y0, x, y;
 
     for (y0 = 0; y0 < height; y0 += 4)
         for (x = 0; x < width; x++)
-            for (y = y0; y < height && y < y0+4; y++) {
+            for (y = y0; y < height && y < y0 + 4; y++) {
                 if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
                 && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
                     int flags_mask = -1;
@@ -575,12 +677,13 @@ static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bpn
 
                         ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
                     }
-                    t1->flags[y+1][x+1] |= JPEG2000_T1_VIS;
+                    t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
                 }
             }
 }
 
-static void decode_refpass(Jpeg2000T1Context *t1, int width, int height, int bpno)
+static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
+                           int bpno)
 {
     int phalf, nhalf;
     int y0, x, y;
@@ -590,36 +693,41 @@ static void decode_refpass(Jpeg2000T1Context *t1, int width, int height, int bpn
 
     for (y0 = 0; y0 < height; y0 += 4)
         for (x = 0; x < width; x++)
-            for (y = y0; y < height && y < y0+4; y++) {
-                if ((t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
-                    int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y+1][x+1]);
-                    int r = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? phalf : nhalf;
-                    t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
-                    t1->flags[y+1][x+1] |= JPEG2000_T1_REF;
+            for (y = y0; y < height && y < y0 + 4; y++)
+                if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
+                    int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
+                    int r     = ff_mqc_decode(&t1->mqc,
+                                              t1->mqc.cx_states + ctxno)
+                                ? phalf : nhalf;
+                    t1->data[y][x]          += t1->data[y][x] < 0 ? -r : r;
+                    t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
                 }
-            }
 }
 
-static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1, int width, int height,
-                           int bpno, int bandno, int seg_symbols, int vert_causal_ctx_csty_symbol)
+static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
+                           int width, int height, int bpno, int bandno,
+                           int seg_symbols, int vert_causal_ctx_csty_symbol)
 {
     int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
 
     for (y0 = 0; y0 < height; y0 += 4) {
         for (x = 0; x < width; x++) {
-            if (y0 + 3 < height && !(
-            (t1->flags[y0+1][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
-            (t1->flags[y0+2][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
-            (t1->flags[y0+3][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
-            (t1->flags[y0+4][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
+            if (y0 + 3 < height &&
+                !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
+                  (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
+                  (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
+                  (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
                 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
                     continue;
-                runlen = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
-                runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
+                runlen = ff_mqc_decode(&t1->mqc,
+                                       t1->mqc.cx_states + MQC_CX_UNI);
+                runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
+                                                       t1->mqc.cx_states +
+                                                       MQC_CX_UNI);
                 dec = 1;
-            } else{
+            } else {
                 runlen = 0;
-                dec = 0;
+                dec    = 0;
             }
 
             for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
@@ -633,12 +741,17 @@ static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1, int
                     }
                 }
                 if (dec) {
-                    int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
-                    t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ? -mask : mask;
+                    int xorbit;
+                    int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
+                                                        &xorbit);
+                    t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
+                                                    t1->mqc.cx_states + ctxno) ^
+                                      xorbit)
+                                     ? -mask : mask;
                     ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
                 }
                 dec = 0;
-                t1->flags[y+1][x+1] &= ~JPEG2000_T1_VIS;
+                t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
             }
         }
     }
@@ -648,13 +761,14 @@ static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1, int
         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
-        if (val != 0xa) {
-            av_log(s->avctx, AV_LOG_ERROR,"Segmentation symbol value incorrect\n");
-        }
+        if (val != 0xa)
+            av_log(s->avctx, AV_LOG_ERROR,
+                   "Segmentation symbol value incorrect\n");
     }
 }
 
-static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
+static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
+                       Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
                        int width, int height, int bandpos)
 {
     int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
@@ -673,19 +787,22 @@ static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, J
 
     while (passno--) {
         switch(pass_t) {
-            case 0: decode_sigpass(t1, width, height, bpno+1, bandpos,
-                                  bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
-                    break;
-            case 1: decode_refpass(t1, width, height, bpno+1);
-                    if (bpass_csty_symbol && clnpass_cnt >= 4)
-                        ff_mqc_initdec(&t1->mqc, cblk->data);
-                    break;
-            case 2: decode_clnpass(s, t1, width, height, bpno+1, bandpos,
-                                   codsty->cblk_style & JPEG2000_CBLK_SEGSYM, vert_causal_ctx_csty_symbol);
-                    clnpass_cnt = clnpass_cnt + 1;
-                    if (bpass_csty_symbol && clnpass_cnt >= 4)
-                       ff_mqc_initdec(&t1->mqc, cblk->data);
-                    break;
+        case 0:
+            decode_sigpass(t1, width, height, bpno+1, bandpos,
+                           bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
+            break;
+        case 1:
+            decode_refpass(t1, width, height, bpno+1);
+            if (bpass_csty_symbol && clnpass_cnt >= 4)
+                ff_mqc_initdec(&t1->mqc, cblk->data);
+            break;
+        case 2:
+            decode_clnpass(s, t1, width, height, bpno+1, bandpos,
+                           codsty->cblk_style & JPEG2000_CBLK_SEGSYM, vert_causal_ctx_csty_symbol);
+            clnpass_cnt = clnpass_cnt + 1;
+            if (bpass_csty_symbol && clnpass_cnt >= 4)
+                ff_mqc_initdec(&t1->mqc, cblk->data);
+            break;
         }
 
         pass_t++;
@@ -735,66 +852,73 @@ static int decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
     uint8_t *line;
     Jpeg2000T1Context t1;
 
+    /* Loop on tile components */
     for (compno = 0; compno < s->ncomponents; compno++) {
         Jpeg2000Component *comp     = tile->comp + compno;
         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
 
-        for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
+        /* Loop on resolution levels */
+        for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
             Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
+            /* Loop on bands */
             for (bandno = 0; bandno < rlevel->nbands; bandno++) {
+                int nb_precincts, precno;
                 Jpeg2000Band *band = rlevel->band + bandno;
-                int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
+                int cblkx, cblky, cblkno=0, bandpos;
 
                 bandpos = bandno + (reslevelno > 0);
 
-                yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
-                y0 = yy0;
-                yy1 = FFMIN(ff_jpeg2000_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
-                            band->coord[1][1]) - band->coord[1][0] + yy0;
-
                 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
                     continue;
 
-                for (cblky = 0; cblky < band->cblkny; cblky++) {
-                    if (reslevelno == 0 || bandno == 1)
-                        xx0 = 0;
-                    else
-                        xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
-                    x0 = xx0;
-                    xx1 = FFMIN(ff_jpeg2000_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
-                                band->coord[0][1]) - band->coord[0][0] + xx0;
-
-                    for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++) {
-                        int y, x;
-                        decode_cblk(s, codsty, &t1, band->cblk + cblkno, xx1 - xx0, yy1 - yy0, bandpos);
+                nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
+                /* Loop on precincts */
+                for (precno = 0; precno < nb_precincts; precno++) {
+                    Jpeg2000Prec *prec = band->prec + precno;
+
+                    /* Loop on codeblocks */
+                    for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
+                        int x, y;
+                        int i, j;
+                        Jpeg2000Cblk *cblk = prec->cblk + cblkno;
+                        decode_cblk(s, codsty, &t1, cblk,
+                                    cblk->coord[0][1] - cblk->coord[0][0],
+                                    cblk->coord[1][1] - cblk->coord[1][0],
+                                    bandpos);
+
+                        /* Manage band offsets */
+                        x = cblk->coord[0][0];
+                        y = cblk->coord[1][0];
+
                         if (codsty->transform == FF_DWT53) {
-                            for (y = yy0; y < yy1; y+=s->cdy[compno]) {
-                                int *ptr = t1.data[y-yy0];
-                                for (x = xx0; x < xx1; x+=s->cdx[compno]) {
-                                    comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = *ptr++ >> 1;
+                            for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
+                                int *datap = &comp->data[(comp->coord[0][1] - comp->coord[0][0]) * (y+j) + x];
+                                int *ptr = t1.data[j];
+                                for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
+                                    datap[i] = ptr[i] >> 1;
                                 }
                             }
                         } else{
-                            for (y = yy0; y < yy1; y+=s->cdy[compno]) {
-                                int *ptr = t1.data[y-yy0];
-                                for (x = xx0; x < xx1; x+=s->cdx[compno]) {
-                                    int tmp = ((int64_t)*ptr++) * ((int64_t)band->stepsize) >> 13, tmp2;
+                            for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
+                                int *datap = &comp->data[(comp->coord[0][1] - comp->coord[0][0]) * (y+j) + x];
+                                int *ptr = t1.data[j];
+                                for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
+                                    int tmp = ((int64_t)ptr[i]) * ((int64_t)band->stepsize) >> 16, tmp2;
                                     tmp2 = FFABS(tmp>>1) + (tmp&1);
-                                    comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = tmp < 0 ? -tmp2 : tmp2;
+                                    datap[i] = tmp < 0 ? -tmp2 : tmp2;
                                 }
                             }
                         }
-                        xx0 = xx1;
-                        xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
-                    }
-                    yy0 = yy1;
-                    yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
-                }
-            }
-        }
-        ff_j2k_dwt_decode(&comp->dwt, comp->data);
+                   } /* end cblk */
+                } /*end prec */
+            } /* end band */
+        } /* end reslevel */
+
+        ff_dwt_decode(&comp->dwt, comp->data);
         src[compno] = comp->data;
-    }
+    } /*end comp */
+
+    /* inverse MCT transformation */
     if (tile->codsty[0].mct)
         mct_decode(s, tile);
 
@@ -805,7 +929,7 @@ static int decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
             for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
                 uint8_t *dst;
 
-                x = tile->comp[compno].coord[0][0] - s->image_offset_x;
+                x   = tile->comp[compno].coord[0][0] - s->image_offset_x;
                 dst = line + x * s->ncomponents + compno;
 
                 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
@@ -845,12 +969,12 @@ static int decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
     return 0;
 }
 
-static void cleanup(Jpeg2000DecoderContext *s)
+static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
 {
     int tileno, compno;
     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
         for (compno = 0; compno < s->ncomponents; compno++) {
-            Jpeg2000Component *comp = s->tile[tileno].comp + compno;
+            Jpeg2000Component *comp     = s->tile[tileno].comp   + compno;
             Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
 
             ff_j2k_cleanup(comp, codsty);
@@ -863,8 +987,8 @@ static void cleanup(Jpeg2000DecoderContext *s)
 static int decode_codestream(Jpeg2000DecoderContext *s)
 {
     Jpeg2000CodingStyle *codsty = s->codsty;
-    Jpeg2000QuantStyle  *qntsty = s->qntsty;
-    uint8_t *properties = s->properties;
+    Jpeg2000QuantStyle *qntsty  = s->qntsty;
+    uint8_t *properties         = s->properties;
 
     for (;;) {
         int oldpos, marker, len, ret = 0;
@@ -925,13 +1049,20 @@ static int decode_codestream(Jpeg2000DecoderContext *s)
             // the comment is ignored
             bytestream2_skip(&s->g, len - 2);
             break;
+        case JPEG2000_TLM:
+            // Tile-part lengths
+            ret = get_tlm(s, len);
+            break;
         default:
-            av_log(s->avctx, AV_LOG_ERROR, "unsupported marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
+            av_log(s->avctx, AV_LOG_ERROR,
+                   "unsupported marker 0x%.4X at pos 0x%x\n",
+                   marker, bytestream2_tell(&s->g) - 4);
             bytestream2_skip(&s->g, len - 2);
             break;
         }
         if (bytestream2_tell(&s->g) - oldpos != len || ret) {
-            av_log(s->avctx, AV_LOG_ERROR, "error during processing marker segment %.4x\n", marker);
+            av_log(s->avctx, AV_LOG_ERROR,
+                   "error during processing marker segment %.4x\n", marker);
             return ret ? ret : -1;
         }
     }
@@ -961,9 +1092,8 @@ static int jp2_find_codestream(Jpeg2000DecoderContext *s)
     return 0;
 }
 
-static int decode_frame(AVCodecContext *avctx,
-                        void *data, int *got_frame,
-                        AVPacket *avpkt)
+static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
+                                 int *got_frame, AVPacket *avpkt)
 {
     Jpeg2000DecoderContext *s = avctx->priv_data;
     AVFrame *picture = data;
@@ -975,6 +1105,9 @@ static int decode_frame(AVCodecContext *avctx,
     bytestream2_init(&s->g, avpkt->data, avpkt->size);
     s->curtileno = -1;
 
+    // reduction factor, i.e number of resolution levels to skip
+    s->reduction_factor = avctx->lowres;
+
     if (bytestream2_get_bytes_left(&s->g) < 2) {
         ret = AVERROR(EINVAL);
         goto err_out;
@@ -1006,14 +1139,14 @@ static int decode_frame(AVCodecContext *avctx,
         if (ret = decode_tile(s, s->tile + tileno))
             goto err_out;
 
-    cleanup(s);
+    jpeg2000_dec_cleanup(s);
 
     *got_frame = 1;
 
     return bytestream2_tell(&s->g);
 
 err_out:
-    cleanup(s);
+    jpeg2000_dec_cleanup(s);
     return ret;
 }
 
@@ -1022,6 +1155,15 @@ static void jpeg2000_init_static_data(AVCodec *codec)
     ff_jpeg2000_init_tier1_luts();
 }
 
+#define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
+#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
+
+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 },
+    { NULL },
+};
+
 static const AVProfile profiles[] = {
     { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0,  "JPEG 2000 codestream restriction 0"   },
     { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1,  "JPEG 2000 codestream restriction 1"   },
@@ -1031,6 +1173,13 @@ static const AVProfile profiles[] = {
     { FF_PROFILE_UNKNOWN },
 };
 
+static const AVClass class = {
+    .class_name = "j2k",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 AVCodec ff_j2k_decoder = {
     .name             = "j2k",
     .long_name        = NULL_IF_CONFIG_SMALL("JPEG 2000"),
@@ -1039,6 +1188,8 @@ AVCodec ff_j2k_decoder = {
     .capabilities     = CODEC_CAP_EXPERIMENTAL | CODEC_CAP_FRAME_THREADS,
     .priv_data_size   = sizeof(Jpeg2000DecoderContext),
     .init_static_data = jpeg2000_init_static_data,
-    .decode           = decode_frame,
+    .decode           = jpeg2000_decode_frame,
+    .priv_class       = &class,
+    .max_lowres       = 5,
     .profiles         = NULL_IF_CONFIG_SMALL(profiles)
 };