static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,

                                  Jpeg2000CodingStyle *codsty,

                                  Jpeg2000ResLevel *rlevel, int precno,

                                  int layno, uint8_t *expn, int numgbits)

{

    int bandno, cblkno, ret, nb_code_blocks;



    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;

        prec->yi0 = 0;

        prec->xi0 = 0;

        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)

                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;

        }

    }

    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;

            if (bytestream2_get_bytes_left(&s->g) < 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;

}

static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,

                                  Jpeg2000CodingStyle *codsty,

                                  Jpeg2000ResLevel *rlevel, int precno,

                                  int layno, uint8_t *expn, int numgbits)

{

    int bandno, cblkno, ret, nb_code_blocks;



    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;

        prec->yi0 = 0;

        prec->xi0 = 0;

        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)

                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;

        }

    }

    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;

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

                return AVERROR(EINVAL);

            



            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;

}

static int FUNC_0(Jpeg2000DecoderContext *VAR_0,

                                  Jpeg2000CodingStyle *VAR_1,

                                  Jpeg2000ResLevel *VAR_2, int VAR_3,

                                  int VAR_4, uint8_t *VAR_5, int VAR_6)

{

    int VAR_7, VAR_8, VAR_9, VAR_10;



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

        jpeg2000_flush(VAR_0);

        return 0;

    } else if (VAR_9 < 0)

        return VAR_9;



    for (VAR_7 = 0; VAR_7 < VAR_2->nbands; VAR_7++) {

        Jpeg2000Band *band = VAR_2->band + VAR_7;

        Jpeg2000Prec *prec = band->prec + VAR_3;



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

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

            continue;

        prec->yi0 = 0;

        prec->xi0 = 0;

        VAR_10 =  prec->nb_codeblocks_height *

                          prec->nb_codeblocks_width;

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

            Jpeg2000Cblk *cblk = prec->cblk + VAR_8;

            int incl, newpasses, llen;



            if (cblk->npasses)

                incl = get_bits(VAR_0, 1);

            else

                incl = tag_tree_decode(VAR_0, prec->cblkincl + VAR_8, VAR_4 + 1) == VAR_4;

            if (!incl)

                continue;

            else if (incl < 0)

                return incl;



            if (!cblk->npasses)

                cblk->nonzerobits = VAR_5[VAR_7] + VAR_6 - 1 -

                                    tag_tree_decode(VAR_0, prec->zerobits + VAR_8,

                                                    100);

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

                return newpasses;

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

                return llen;

            cblk->lblock += llen;

            if ((VAR_9 = get_bits(VAR_0, av_log2(newpasses) + cblk->lblock)) < 0)

                return VAR_9;

            cblk->lengthinc = VAR_9;

            cblk->npasses  += newpasses;

        }

    }

    jpeg2000_flush(VAR_0);



    if (VAR_1->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_7 = 0; VAR_7 < VAR_2->nbands; VAR_7++) {

        Jpeg2000Band *band = VAR_2->band + VAR_7;

        Jpeg2000Prec *prec = band->prec + VAR_3;



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

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

            Jpeg2000Cblk *cblk = prec->cblk + VAR_8;

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

                return AVERROR(EINVAL);

            



            if (cblk->lengthinc > 0) {

                bytestream2_get_bufferu(&VAR_0->g, cblk->data, cblk->lengthinc);

            } else {

                cblk->data[0] = 0xFF;

                cblk->data[1] = 0xFF;

            }

            cblk->length   += cblk->lengthinc;

            cblk->lengthinc = 0;

        }

    }

    return 0;

}

static int FUNC_0(Jpeg2000DecoderContext *VAR_0,
Jpeg2000CodingStyle *VAR_1,
Jpeg2000ResLevel *VAR_2, int VAR_3,
int VAR_4, uint8_t *VAR_5, int VAR_6)
{ int VAR_7, VAR_8, VAR_9, VAR_10; if (!(VAR_9 = get_bits(VAR_0, 1))) { jpeg2000_flush(VAR_0); return 0; } else if (VAR_9 < 0) return VAR_9; for (VAR_7 = 0; VAR_7 < VAR_2->nbands; VAR_7++) { Jpeg2000Band *band = VAR_2->band + VAR_7; Jpeg2000Prec *prec = band->prec + VAR_3; if (band->coord[0][0] == band->coord[0][1] ||
band->coord[1][0] == band->coord[1][1])
continue; prec->yi0 = 0; prec->xi0 = 0; VAR_10 =  prec->nb_codeblocks_height *
prec->nb_codeblocks_width; for (VAR_8 = 0; VAR_8 < VAR_10; VAR_8++) { Jpeg2000Cblk *cblk = prec->cblk + VAR_8; int incl, newpasses, llen; if (cblk->npasses)
incl = get_bits(VAR_0, 1); else
incl = tag_tree_decode(VAR_0, prec->cblkincl + VAR_8, VAR_4 + 1) == VAR_4; if (!incl)
continue; else if (incl < 0)
return incl; if (!cblk->npasses)
cblk->nonzerobits = VAR_5[VAR_7] + VAR_6 - 1 -
tag_tree_decode(VAR_0, prec->zerobits + VAR_8,
100); if ((newpasses = getnpasses(VAR_0)) < 0)
return newpasses; if ((llen = getlblockinc(VAR_0)) < 0)
return llen; cblk->lblock += llen; if ((VAR_9 = get_bits(VAR_0, av_log2(newpasses) + cblk->lblock)) < 0)
return VAR_9; cblk->lengthinc = VAR_9; cblk->npasses  += newpasses; } } jpeg2000_flush(VAR_0); if (VAR_1->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_7 = 0; VAR_7 < VAR_2->nbands; VAR_7++) { Jpeg2000Band *band = VAR_2->band + VAR_7; Jpeg2000Prec *prec = band->prec + VAR_3; VAR_10 = prec->nb_codeblocks_height * prec->nb_codeblocks_width; for (VAR_8 = 0; VAR_8 < VAR_10; VAR_8++) { Jpeg2000Cblk *cblk = prec->cblk + VAR_8; if (bytestream2_get_bytes_left(&VAR_0->g) < cblk->lengthinc)
return AVERROR(EINVAL); if (cblk->lengthinc > 0) { bytestream2_get_bufferu(&VAR_0->g, cblk->data, cblk->lengthinc); } else { cblk->data[0] = 0xFF; cblk->data[1] = 0xFF; } cblk->length   += cblk->lengthinc; cblk->lengthinc = 0; } } return 0; }