static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,

                                  Jpeg2000CodingStyle *codsty,

                                  Jpeg2000ResLevel *rlevel, int precno,

                                  int layno, uint8_t *expn, int numgbits)

{

    int bandno, cblkno, ret, nb_code_blocks;

    int cwsno;



    if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {

        if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {

            s->g = tile->tile_part[++(*tp_index)].tpg;

        }

    }



    if (bytestream2_peek_be32(&s->g) == 0xFF910004)

        bytestream2_skip(&s->g, 6);



    if (!(ret = get_bits(s, 1))) {

        jpeg2000_flush(s);

        return 0;

    } else if (ret < 0)

        return ret;



    for (bandno = 0; bandno < rlevel->nbands; bandno++) {

        Jpeg2000Band *band = rlevel->band + bandno;

        Jpeg2000Prec *prec = band->prec + precno;



        if (band->coord[0][0] == band->coord[0][1] ||

            band->coord[1][0] == band->coord[1][1])

            continue;

        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 + cblkno, layno + 1) == layno;

            if (!incl)

                continue;

            else if (incl < 0)

                return incl;



            if (!cblk->npasses) {

                int v = expn[bandno] + numgbits - 1 -

                        tag_tree_decode(s, prec->zerobits + cblkno, 100);

                if (v < 0) {

                    av_log(s->avctx, AV_LOG_ERROR,

                           "nonzerobits %d invalid\n", v);

                    return AVERROR_INVALIDDATA;

                }

                cblk->nonzerobits = v;

            }

            if ((newpasses = getnpasses(s)) < 0)

                return newpasses;

            av_assert2(newpasses > 0);

            if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {

                avpriv_request_sample(s->avctx, "Too many passes\n");

                return AVERROR_PATCHWELCOME;

            }

            if ((llen = getlblockinc(s)) < 0)

                return llen;

            if (cblk->lblock + llen + av_log2(newpasses) > 16) {

                avpriv_request_sample(s->avctx,

                                      "Block with length beyond 16 bits\n");

                return AVERROR_PATCHWELCOME;

            }



            cblk->lblock += llen;



            cblk->nb_lengthinc = 0;

            cblk->nb_terminationsinc = 0;

            do {

                int newpasses1 = 0;



                while (newpasses1 < newpasses) {

                    newpasses1 ++;

                    if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {

                        cblk->nb_terminationsinc ++;

                        break;

                    }

                }



                if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)

                    return ret;

                if (ret > sizeof(cblk->data)) {

                    avpriv_request_sample(s->avctx,

                                        "Block with lengthinc greater than %"SIZE_SPECIFIER"",

                                        sizeof(cblk->data));

                    return AVERROR_PATCHWELCOME;

                }

                cblk->lengthinc[cblk->nb_lengthinc++] = ret;

                cblk->npasses  += newpasses1;

                newpasses -= newpasses1;

            } while(newpasses);

        }

    }

    jpeg2000_flush(s);



    if (codsty->csty & JPEG2000_CSTY_EPH) {

        if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)

            bytestream2_skip(&s->g, 2);

        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;

        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;

            for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {

                if (   bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]

                    || sizeof(cblk->data) < cblk->length + cblk->lengthinc[cwsno] + 4

                ) {

                    av_log(s->avctx, AV_LOG_ERROR,

                        "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",

                        cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));

                    return AVERROR_INVALIDDATA;

                }



                bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);

                cblk->length   += cblk->lengthinc[cwsno];

                cblk->lengthinc[cwsno] = 0;

                if (cblk->nb_terminationsinc) {

                    cblk->nb_terminationsinc--;

                    cblk->nb_terminations++;

                    cblk->data[cblk->length++] = 0xFF;

                    cblk->data[cblk->length++] = 0xFF;

                    cblk->data_start[cblk->nb_terminations] = cblk->length;

                }

            }

        }

    }

    return 0;

}

static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,

                                  Jpeg2000CodingStyle *codsty,

                                  Jpeg2000ResLevel *rlevel, int precno,

                                  int layno, uint8_t *expn, int numgbits)

{

    int bandno, cblkno, ret, nb_code_blocks;

    int cwsno;



    if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {

        if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {

            s->g = tile->tile_part[++(*tp_index)].tpg;

        }

    }



    if (bytestream2_peek_be32(&s->g) == 0xFF910004)

        bytestream2_skip(&s->g, 6);



    if (!(ret = get_bits(s, 1))) {

        jpeg2000_flush(s);

        return 0;

    } else if (ret < 0)

        return ret;



    for (bandno = 0; bandno < rlevel->nbands; bandno++) {

        Jpeg2000Band *band = rlevel->band + bandno;

        Jpeg2000Prec *prec = band->prec + precno;



        if (band->coord[0][0] == band->coord[0][1] ||

            band->coord[1][0] == band->coord[1][1])

            continue;

        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 + cblkno, layno + 1) == layno;

            if (!incl)

                continue;

            else if (incl < 0)

                return incl;



            if (!cblk->npasses) {

                int v = expn[bandno] + numgbits - 1 -

                        tag_tree_decode(s, prec->zerobits + cblkno, 100);

                if (v < 0) {

                    av_log(s->avctx, AV_LOG_ERROR,

                           "nonzerobits %d invalid\n", v);

                    return AVERROR_INVALIDDATA;

                }

                cblk->nonzerobits = v;

            }

            if ((newpasses = getnpasses(s)) < 0)

                return newpasses;

            av_assert2(newpasses > 0);

            if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {

                avpriv_request_sample(s->avctx, "Too many passes\n");

                return AVERROR_PATCHWELCOME;

            }

            if ((llen = getlblockinc(s)) < 0)

                return llen;

            if (cblk->lblock + llen + av_log2(newpasses) > 16) {

                avpriv_request_sample(s->avctx,

                                      "Block with length beyond 16 bits\n");

                return AVERROR_PATCHWELCOME;

            }



            cblk->lblock += llen;



            cblk->nb_lengthinc = 0;

            cblk->nb_terminationsinc = 0;

            do {

                int newpasses1 = 0;



                while (newpasses1 < newpasses) {

                    newpasses1 ++;

                    if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {

                        cblk->nb_terminationsinc ++;

                        break;

                    }

                }



                if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)

                    return ret;

                if (ret > sizeof(cblk->data)) {

                    avpriv_request_sample(s->avctx,

                                        "Block with lengthinc greater than %"SIZE_SPECIFIER"",

                                        sizeof(cblk->data));

                    return AVERROR_PATCHWELCOME;

                }

                cblk->lengthinc[cblk->nb_lengthinc++] = ret;

                cblk->npasses  += newpasses1;

                newpasses -= newpasses1;

            } while(newpasses);

        }

    }

    jpeg2000_flush(s);



    if (codsty->csty & JPEG2000_CSTY_EPH) {

        if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)

            bytestream2_skip(&s->g, 2);

        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;

        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;

            for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {

                if (   bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]

                    || sizeof(cblk->data) < cblk->length + cblk->lengthinc[cwsno] + 4

                ) {

                    av_log(s->avctx, AV_LOG_ERROR,

                        "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",

                        cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));

                    return AVERROR_INVALIDDATA;

                }



                bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);

                cblk->length   += cblk->lengthinc[cwsno];

                cblk->lengthinc[cwsno] = 0;

                if (cblk->nb_terminationsinc) {

                    cblk->nb_terminationsinc--;

                    cblk->nb_terminations++;

                    cblk->data[cblk->length++] = 0xFF;

                    cblk->data[cblk->length++] = 0xFF;

                    cblk->data_start[cblk->nb_terminations] = cblk->length;

                }

            }

        }

    }

    return 0;

}

static int FUNC_0(Jpeg2000DecoderContext *VAR_0, Jpeg2000Tile *VAR_1, int *VAR_2,

                                  Jpeg2000CodingStyle *VAR_3,

                                  Jpeg2000ResLevel *VAR_4, int VAR_5,

                                  int VAR_6, uint8_t *VAR_7, int VAR_8)

{

    int VAR_9, VAR_10, VAR_11, VAR_12;

    int VAR_13;



    if (bytestream2_get_bytes_left(&VAR_0->g) == 0 && VAR_0->bit_index == 8) {

        if (*VAR_2 < FF_ARRAY_ELEMS(VAR_1->tile_part) - 1) {

            VAR_0->g = VAR_1->tile_part[++(*VAR_2)].tpg;

        }

    }



    if (bytestream2_peek_be32(&VAR_0->g) == 0xFF910004)

        bytestream2_skip(&VAR_0->g, 6);



    if (!(VAR_11 = get_bits(VAR_0, 1))) {

        jpeg2000_flush(VAR_0);

        return 0;

    } else if (VAR_11 < 0)

        return VAR_11;



    for (VAR_9 = 0; VAR_9 < VAR_4->nbands; VAR_9++) {

        Jpeg2000Band *band = VAR_4->band + VAR_9;

        Jpeg2000Prec *prec = band->prec + VAR_5;



        if (band->coord[0][0] == band->coord[0][1] ||

            band->coord[1][0] == band->coord[1][1])

            continue;

        VAR_12 =  prec->nb_codeblocks_height *

                          prec->nb_codeblocks_width;

        for (VAR_10 = 0; VAR_10 < VAR_12; VAR_10++) {

            Jpeg2000Cblk *cblk = prec->cblk + VAR_10;

            int incl, newpasses, llen;



            if (cblk->npasses)

                incl = get_bits(VAR_0, 1);

            else

                incl = tag_tree_decode(VAR_0, prec->cblkincl + VAR_10, VAR_6 + 1) == VAR_6;

            if (!incl)

                continue;

            else if (incl < 0)

                return incl;



            if (!cblk->npasses) {

                int v = VAR_7[VAR_9] + VAR_8 - 1 -

                        tag_tree_decode(VAR_0, prec->zerobits + VAR_10, 100);

                if (v < 0) {

                    av_log(VAR_0->avctx, AV_LOG_ERROR,

                           "nonzerobits %d invalid\n", v);

                    return AVERROR_INVALIDDATA;

                }

                cblk->nonzerobits = v;

            }

            if ((newpasses = getnpasses(VAR_0)) < 0)

                return newpasses;

            av_assert2(newpasses > 0);

            if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {

                avpriv_request_sample(VAR_0->avctx, "Too many passes\n");

                return AVERROR_PATCHWELCOME;

            }

            if ((llen = getlblockinc(VAR_0)) < 0)

                return llen;

            if (cblk->lblock + llen + av_log2(newpasses) > 16) {

                avpriv_request_sample(VAR_0->avctx,

                                      "Block with length beyond 16 bits\n");

                return AVERROR_PATCHWELCOME;

            }



            cblk->lblock += llen;



            cblk->nb_lengthinc = 0;

            cblk->nb_terminationsinc = 0;

            do {

                int newpasses1 = 0;



                while (newpasses1 < newpasses) {

                    newpasses1 ++;

                    if (needs_termination(VAR_3->cblk_style, cblk->npasses + newpasses1 - 1)) {

                        cblk->nb_terminationsinc ++;

                        break;

                    }

                }



                if ((VAR_11 = get_bits(VAR_0, av_log2(newpasses1) + cblk->lblock)) < 0)

                    return VAR_11;

                if (VAR_11 > sizeof(cblk->data)) {

                    avpriv_request_sample(VAR_0->avctx,

                                        "Block with lengthinc greater than %"SIZE_SPECIFIER"",

                                        sizeof(cblk->data));

                    return AVERROR_PATCHWELCOME;

                }

                cblk->lengthinc[cblk->nb_lengthinc++] = VAR_11;

                cblk->npasses  += newpasses1;

                newpasses -= newpasses1;

            } while(newpasses);

        }

    }

    jpeg2000_flush(VAR_0);



    if (VAR_3->csty & JPEG2000_CSTY_EPH) {

        if (bytestream2_peek_be16(&VAR_0->g) == JPEG2000_EPH)

            bytestream2_skip(&VAR_0->g, 2);

        else

            av_log(VAR_0->avctx, AV_LOG_ERROR, "EPH marker not found.\n");

    }



    for (VAR_9 = 0; VAR_9 < VAR_4->nbands; VAR_9++) {

        Jpeg2000Band *band = VAR_4->band + VAR_9;

        Jpeg2000Prec *prec = band->prec + VAR_5;



        VAR_12 = prec->nb_codeblocks_height * prec->nb_codeblocks_width;

        for (VAR_10 = 0; VAR_10 < VAR_12; VAR_10++) {

            Jpeg2000Cblk *cblk = prec->cblk + VAR_10;

            for (VAR_13 = 0; VAR_13 < cblk->nb_lengthinc; VAR_13 ++) {

                if (   bytestream2_get_bytes_left(&VAR_0->g) < cblk->lengthinc[VAR_13]

                    || sizeof(cblk->data) < cblk->length + cblk->lengthinc[VAR_13] + 4

                ) {

                    av_log(VAR_0->avctx, AV_LOG_ERROR,

                        "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",

                        cblk->length, cblk->lengthinc[VAR_13], bytestream2_get_bytes_left(&VAR_0->g));

                    return AVERROR_INVALIDDATA;

                }



                bytestream2_get_bufferu(&VAR_0->g, cblk->data + cblk->length, cblk->lengthinc[VAR_13]);

                cblk->length   += cblk->lengthinc[VAR_13];

                cblk->lengthinc[VAR_13] = 0;

                if (cblk->nb_terminationsinc) {

                    cblk->nb_terminationsinc--;

                    cblk->nb_terminations++;

                    cblk->data[cblk->length++] = 0xFF;

                    cblk->data[cblk->length++] = 0xFF;

                    cblk->data_start[cblk->nb_terminations] = cblk->length;

                }

            }

        }

    }

    return 0;

}

static int FUNC_0(Jpeg2000DecoderContext *VAR_0, Jpeg2000Tile *VAR_1, int *VAR_2,
Jpeg2000CodingStyle *VAR_3,
Jpeg2000ResLevel *VAR_4, int VAR_5,
int VAR_6, uint8_t *VAR_7, int VAR_8)
{ int VAR_9, VAR_10, VAR_11, VAR_12; int VAR_13; if (bytestream2_get_bytes_left(&VAR_0->g) == 0 && VAR_0->bit_index == 8) { if (*VAR_2 < FF_ARRAY_ELEMS(VAR_1->tile_part) - 1) { VAR_0->g = VAR_1->tile_part[++(*VAR_2)].tpg; } } if (bytestream2_peek_be32(&VAR_0->g) == 0xFF910004)
bytestream2_skip(&VAR_0->g, 6); if (!(VAR_11 = get_bits(VAR_0, 1))) { jpeg2000_flush(VAR_0); return 0; } else if (VAR_11 < 0) return VAR_11; for (VAR_9 = 0; VAR_9 < VAR_4->nbands; VAR_9++) { Jpeg2000Band *band = VAR_4->band + VAR_9; Jpeg2000Prec *prec = band->prec + VAR_5; if (band->coord[0][0] == band->coord[0][1] ||
band->coord[1][0] == band->coord[1][1])
continue; VAR_12 =  prec->nb_codeblocks_height *
prec->nb_codeblocks_width; for (VAR_10 = 0; VAR_10 < VAR_12; VAR_10++) { Jpeg2000Cblk *cblk = prec->cblk + VAR_10; int incl, newpasses, llen; if (cblk->npasses)
incl = get_bits(VAR_0, 1); else
incl = tag_tree_decode(VAR_0, prec->cblkincl + VAR_10, VAR_6 + 1) == VAR_6; if (!incl)
continue; else if (incl < 0)
return incl; if (!cblk->npasses) { int v = VAR_7[VAR_9] + VAR_8 - 1 -
tag_tree_decode(VAR_0, prec->zerobits + VAR_10, 100); if (v < 0) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"nonzerobits %d invalid\n", v); return AVERROR_INVALIDDATA; } cblk->nonzerobits = v; } if ((newpasses = getnpasses(VAR_0)) < 0)
return newpasses; av_assert2(newpasses > 0); if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) { avpriv_request_sample(VAR_0->avctx, "Too many passes\n"); return AVERROR_PATCHWELCOME; } if ((llen = getlblockinc(VAR_0)) < 0)
return llen; if (cblk->lblock + llen + av_log2(newpasses) > 16) { avpriv_request_sample(VAR_0->avctx,
"Block with length beyond 16 bits\n"); return AVERROR_PATCHWELCOME; } cblk->lblock += llen; cblk->nb_lengthinc = 0; cblk->nb_terminationsinc = 0; do { int newpasses1 = 0; while (newpasses1 < newpasses) { newpasses1 ++; if (needs_termination(VAR_3->cblk_style, cblk->npasses + newpasses1 - 1)) { cblk->nb_terminationsinc ++; break; } } if ((VAR_11 = get_bits(VAR_0, av_log2(newpasses1) + cblk->lblock)) < 0)
return VAR_11; if (VAR_11 > sizeof(cblk->data)) { avpriv_request_sample(VAR_0->avctx,
"Block with lengthinc greater than %"SIZE_SPECIFIER"",
sizeof(cblk->data)); return AVERROR_PATCHWELCOME; } cblk->lengthinc[cblk->nb_lengthinc++] = VAR_11; cblk->npasses  += newpasses1; newpasses -= newpasses1; } while(newpasses); } } jpeg2000_flush(VAR_0); if (VAR_3->csty & JPEG2000_CSTY_EPH) { if (bytestream2_peek_be16(&VAR_0->g) == JPEG2000_EPH)
bytestream2_skip(&VAR_0->g, 2); else
av_log(VAR_0->avctx, AV_LOG_ERROR, "EPH marker not found.\n"); } for (VAR_9 = 0; VAR_9 < VAR_4->nbands; VAR_9++) { Jpeg2000Band *band = VAR_4->band + VAR_9; Jpeg2000Prec *prec = band->prec + VAR_5; VAR_12 = prec->nb_codeblocks_height * prec->nb_codeblocks_width; for (VAR_10 = 0; VAR_10 < VAR_12; VAR_10++) { Jpeg2000Cblk *cblk = prec->cblk + VAR_10; for (VAR_13 = 0; VAR_13 < cblk->nb_lengthinc; VAR_13 ++) { if (   bytestream2_get_bytes_left(&VAR_0->g) < cblk->lengthinc[VAR_13]
|| sizeof(cblk->data) < cblk->length + cblk->lengthinc[VAR_13] + 4
) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
cblk->length, cblk->lengthinc[VAR_13], bytestream2_get_bytes_left(&VAR_0->g)); return AVERROR_INVALIDDATA; } bytestream2_get_bufferu(&VAR_0->g, cblk->data + cblk->length, cblk->lengthinc[VAR_13]); cblk->length   += cblk->lengthinc[VAR_13]; cblk->lengthinc[VAR_13] = 0; if (cblk->nb_terminationsinc) { cblk->nb_terminationsinc--; cblk->nb_terminations++; cblk->data[cblk->length++] = 0xFF; cblk->data[cblk->length++] = 0xFF; cblk->data_start[cblk->nb_terminations] = cblk->length; } } } } return 0; }