static int get_siz(Jpeg2000DecoderContext *s)

{

    int i;



    if (bytestream2_get_bytes_left(&s->g) < 36)

        return AVERROR(EINVAL);



    s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz

    s->width          = bytestream2_get_be32u(&s->g); // Width

    s->height         = bytestream2_get_be32u(&s->g); // Height

    s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz

    s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz

    s->tile_width     = bytestream2_get_be32u(&s->g); // XTSiz

    s->tile_height    = bytestream2_get_be32u(&s->g); // YTSiz

    s->tile_offset_x  = bytestream2_get_be32u(&s->g); // XT0Siz

    s->tile_offset_y  = bytestream2_get_be32u(&s->g); // YT0Siz

    s->ncomponents    = bytestream2_get_be16u(&s->g); // CSiz



    if (s->ncomponents <= 0 || s->ncomponents > 4) {

        av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);

        return AVERROR(EINVAL);

    }

    if (s->tile_width<=0 || s->tile_height<=0)

        return AVERROR(EINVAL);



    if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)

        return AVERROR(EINVAL);



    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->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 %d %d for component %d\n", s->cdx[i], s->cdy[i], i);

            if (!s->cdx[i] || !s->cdy[i])

                return AVERROR_INVALIDDATA;

        }

    }



    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(*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(*tile->comp));

        if (!tile->comp)

            return AVERROR(ENOMEM);

    }



    /* 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)

            s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;

        else

            s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;

        break;

    case 3:

        switch (s->avctx->profile) {

        case FF_PROFILE_JPEG2000_DCINEMA_2K:

        case FF_PROFILE_JPEG2000_DCINEMA_4K:

            /* XYZ color-space for digital cinema profiles */

            s->avctx->pix_fmt = AV_PIX_FMT_XYZ12;

            break;

        default:

            if (s->precision > 8)

                s->avctx->pix_fmt = AV_PIX_FMT_RGB48;

            else

                s->avctx->pix_fmt = AV_PIX_FMT_RGB24;

            break;

        }

        break;

    case 4:

        s->avctx->pix_fmt = AV_PIX_FMT_RGBA;

        break;

    default:

        /* pixel format can not be identified */

        s->avctx->pix_fmt = AV_PIX_FMT_NONE;

        break;

    }

    return 0;

}

static int get_siz(Jpeg2000DecoderContext *s)

{

    int i;



    if (bytestream2_get_bytes_left(&s->g) < 36)

        return AVERROR(EINVAL);



    s->avctx->profile = bytestream2_get_be16u(&s->g); 

    s->width          = bytestream2_get_be32u(&s->g); 

    s->height         = bytestream2_get_be32u(&s->g); 

    s->image_offset_x = bytestream2_get_be32u(&s->g); 

    s->image_offset_y = bytestream2_get_be32u(&s->g); 

    s->tile_width     = bytestream2_get_be32u(&s->g); 

    s->tile_height    = bytestream2_get_be32u(&s->g); 

    s->tile_offset_x  = bytestream2_get_be32u(&s->g); 

    s->tile_offset_y  = bytestream2_get_be32u(&s->g); 

    s->ncomponents    = bytestream2_get_be16u(&s->g); 



    if (s->ncomponents <= 0 || s->ncomponents > 4) {

        av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);

        return AVERROR(EINVAL);

    }

    if (s->tile_width<=0 || s->tile_height<=0)

        return AVERROR(EINVAL);



    if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)

        return AVERROR(EINVAL);



    for (i = 0; i < s->ncomponents; i++) { 

        uint8_t x = bytestream2_get_byteu(&s->g);

        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 %d %d for component %d\n", s->cdx[i], s->cdy[i], i);

            if (!s->cdx[i] || !s->cdy[i])

                return AVERROR_INVALIDDATA;

        }

    }



    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(*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(*tile->comp));

        if (!tile->comp)

            return AVERROR(ENOMEM);

    }



    

    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)

            s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;

        else

            s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;

        break;

    case 3:

        switch (s->avctx->profile) {

        case FF_PROFILE_JPEG2000_DCINEMA_2K:

        case FF_PROFILE_JPEG2000_DCINEMA_4K:

            

            s->avctx->pix_fmt = AV_PIX_FMT_XYZ12;

            break;

        default:

            if (s->precision > 8)

                s->avctx->pix_fmt = AV_PIX_FMT_RGB48;

            else

                s->avctx->pix_fmt = AV_PIX_FMT_RGB24;

            break;

        }

        break;

    case 4:

        s->avctx->pix_fmt = AV_PIX_FMT_RGBA;

        break;

    default:

        

        s->avctx->pix_fmt = AV_PIX_FMT_NONE;

        break;

    }

    return 0;

}

static int FUNC_0(Jpeg2000DecoderContext *VAR_0)

{

    int VAR_1;



    if (bytestream2_get_bytes_left(&VAR_0->g) < 36)

        return AVERROR(EINVAL);



    VAR_0->avctx->profile = bytestream2_get_be16u(&VAR_0->g); 

    VAR_0->width          = bytestream2_get_be32u(&VAR_0->g); 

    VAR_0->height         = bytestream2_get_be32u(&VAR_0->g); 

    VAR_0->image_offset_x = bytestream2_get_be32u(&VAR_0->g); 

    VAR_0->image_offset_y = bytestream2_get_be32u(&VAR_0->g); 

    VAR_0->tile_width     = bytestream2_get_be32u(&VAR_0->g); 

    VAR_0->tile_height    = bytestream2_get_be32u(&VAR_0->g); 

    VAR_0->tile_offset_x  = bytestream2_get_be32u(&VAR_0->g); 

    VAR_0->tile_offset_y  = bytestream2_get_be32u(&VAR_0->g); 

    VAR_0->ncomponents    = bytestream2_get_be16u(&VAR_0->g); 



    if (VAR_0->ncomponents <= 0 || VAR_0->ncomponents > 4) {

        av_log(VAR_0->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", VAR_0->ncomponents);

        return AVERROR(EINVAL);

    }

    if (VAR_0->tile_width<=0 || VAR_0->tile_height<=0)

        return AVERROR(EINVAL);



    if (bytestream2_get_bytes_left(&VAR_0->g) < 3 * VAR_0->ncomponents)

        return AVERROR(EINVAL);



    for (VAR_1 = 0; VAR_1 < VAR_0->ncomponents; VAR_1++) { 

        uint8_t x = bytestream2_get_byteu(&VAR_0->g);

        VAR_0->cbps[VAR_1]   = (x & 0x7f) + 1;

        VAR_0->precision = FFMAX(VAR_0->cbps[VAR_1], VAR_0->precision);

        VAR_0->sgnd[VAR_1]   = !!(x & 0x80);

        VAR_0->cdx[VAR_1]    = bytestream2_get_byteu(&VAR_0->g);

        VAR_0->cdy[VAR_1]    = bytestream2_get_byteu(&VAR_0->g);

        if (VAR_0->cdx[VAR_1] != 1 || VAR_0->cdy[VAR_1] != 1) {

            av_log(VAR_0->avctx, AV_LOG_ERROR, "unsupported/ CDxy values %d %d for component %d\n", VAR_0->cdx[VAR_1], VAR_0->cdy[VAR_1], VAR_1);

            if (!VAR_0->cdx[VAR_1] || !VAR_0->cdy[VAR_1])

                return AVERROR_INVALIDDATA;

        }

    }



    VAR_0->numXtiles = ff_jpeg2000_ceildiv(VAR_0->width  - VAR_0->tile_offset_x, VAR_0->tile_width);

    VAR_0->numYtiles = ff_jpeg2000_ceildiv(VAR_0->height - VAR_0->tile_offset_y, VAR_0->tile_height);



    if (VAR_0->numXtiles * (uint64_t)VAR_0->numYtiles > INT_MAX/sizeof(Jpeg2000Tile))

        return AVERROR(EINVAL);



    VAR_0->tile = av_mallocz(VAR_0->numXtiles * VAR_0->numYtiles * sizeof(*VAR_0->tile));

    if (!VAR_0->tile)

        return AVERROR(ENOMEM);



    for (VAR_1 = 0; VAR_1 < VAR_0->numXtiles * VAR_0->numYtiles; VAR_1++) {

        Jpeg2000Tile *tile = VAR_0->tile + VAR_1;



        tile->comp = av_mallocz(VAR_0->ncomponents * sizeof(*tile->comp));

        if (!tile->comp)

            return AVERROR(ENOMEM);

    }



    

    VAR_0->avctx->width  = ff_jpeg2000_ceildivpow2(VAR_0->width  - VAR_0->image_offset_x,

                                               VAR_0->reduction_factor);

    VAR_0->avctx->height = ff_jpeg2000_ceildivpow2(VAR_0->height - VAR_0->image_offset_y,

                                               VAR_0->reduction_factor);



    switch(VAR_0->ncomponents) {

    case 1:

        if (VAR_0->precision > 8)

            VAR_0->avctx->pix_fmt = AV_PIX_FMT_GRAY16;

        else

            VAR_0->avctx->pix_fmt = AV_PIX_FMT_GRAY8;

        break;

    case 3:

        switch (VAR_0->avctx->profile) {

        case FF_PROFILE_JPEG2000_DCINEMA_2K:

        case FF_PROFILE_JPEG2000_DCINEMA_4K:

            

            VAR_0->avctx->pix_fmt = AV_PIX_FMT_XYZ12;

            break;

        default:

            if (VAR_0->precision > 8)

                VAR_0->avctx->pix_fmt = AV_PIX_FMT_RGB48;

            else

                VAR_0->avctx->pix_fmt = AV_PIX_FMT_RGB24;

            break;

        }

        break;

    case 4:

        VAR_0->avctx->pix_fmt = AV_PIX_FMT_RGBA;

        break;

    default:

        

        VAR_0->avctx->pix_fmt = AV_PIX_FMT_NONE;

        break;

    }

    return 0;

}

static int FUNC_0(Jpeg2000DecoderContext *VAR_0)
{ int VAR_1; if (bytestream2_get_bytes_left(&VAR_0->g) < 36)
return AVERROR(EINVAL); VAR_0->avctx->profile = bytestream2_get_be16u(&VAR_0->g); VAR_0->width          = bytestream2_get_be32u(&VAR_0->g); VAR_0->height         = bytestream2_get_be32u(&VAR_0->g); VAR_0->image_offset_x = bytestream2_get_be32u(&VAR_0->g); VAR_0->image_offset_y = bytestream2_get_be32u(&VAR_0->g); VAR_0->tile_width     = bytestream2_get_be32u(&VAR_0->g); VAR_0->tile_height    = bytestream2_get_be32u(&VAR_0->g); VAR_0->tile_offset_x  = bytestream2_get_be32u(&VAR_0->g); VAR_0->tile_offset_y  = bytestream2_get_be32u(&VAR_0->g); VAR_0->ncomponents    = bytestream2_get_be16u(&VAR_0->g); if (VAR_0->ncomponents <= 0 || VAR_0->ncomponents > 4) { av_log(VAR_0->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", VAR_0->ncomponents); return AVERROR(EINVAL); } if (VAR_0->tile_width<=0 || VAR_0->tile_height<=0)
return AVERROR(EINVAL); if (bytestream2_get_bytes_left(&VAR_0->g) < 3 * VAR_0->ncomponents)
return AVERROR(EINVAL); for (VAR_1 = 0; VAR_1 < VAR_0->ncomponents; VAR_1++) { uint8_t x = bytestream2_get_byteu(&VAR_0->g); VAR_0->cbps[VAR_1]   = (x & 0x7f) + 1; VAR_0->precision = FFMAX(VAR_0->cbps[VAR_1], VAR_0->precision); VAR_0->sgnd[VAR_1]   = !!(x & 0x80); VAR_0->cdx[VAR_1]    = bytestream2_get_byteu(&VAR_0->g); VAR_0->cdy[VAR_1]    = bytestream2_get_byteu(&VAR_0->g); if (VAR_0->cdx[VAR_1] != 1 || VAR_0->cdy[VAR_1] != 1) { av_log(VAR_0->avctx, AV_LOG_ERROR, "unsupported/ CDxy values %d %d for component %d\n", VAR_0->cdx[VAR_1], VAR_0->cdy[VAR_1], VAR_1); if (!VAR_0->cdx[VAR_1] || !VAR_0->cdy[VAR_1])
return AVERROR_INVALIDDATA; } } VAR_0->numXtiles = ff_jpeg2000_ceildiv(VAR_0->width  - VAR_0->tile_offset_x, VAR_0->tile_width); VAR_0->numYtiles = ff_jpeg2000_ceildiv(VAR_0->height - VAR_0->tile_offset_y, VAR_0->tile_height); if (VAR_0->numXtiles * (uint64_t)VAR_0->numYtiles > INT_MAX/sizeof(Jpeg2000Tile))
return AVERROR(EINVAL); VAR_0->tile = av_mallocz(VAR_0->numXtiles * VAR_0->numYtiles * sizeof(*VAR_0->tile)); if (!VAR_0->tile)
return AVERROR(ENOMEM); for (VAR_1 = 0; VAR_1 < VAR_0->numXtiles * VAR_0->numYtiles; VAR_1++) { Jpeg2000Tile *tile = VAR_0->tile + VAR_1; tile->comp = av_mallocz(VAR_0->ncomponents * sizeof(*tile->comp)); if (!tile->comp)
return AVERROR(ENOMEM); } VAR_0->avctx->width  = ff_jpeg2000_ceildivpow2(VAR_0->width  - VAR_0->image_offset_x,
VAR_0->reduction_factor); VAR_0->avctx->height = ff_jpeg2000_ceildivpow2(VAR_0->height - VAR_0->image_offset_y,
VAR_0->reduction_factor); switch(VAR_0->ncomponents) { case 1:
if (VAR_0->precision > 8)
VAR_0->avctx->pix_fmt = AV_PIX_FMT_GRAY16; else
VAR_0->avctx->pix_fmt = AV_PIX_FMT_GRAY8; break; case 3:
switch (VAR_0->avctx->profile) { case FF_PROFILE_JPEG2000_DCINEMA_2K:
case FF_PROFILE_JPEG2000_DCINEMA_4K:
VAR_0->avctx->pix_fmt = AV_PIX_FMT_XYZ12; break; default:
if (VAR_0->precision > 8)
VAR_0->avctx->pix_fmt = AV_PIX_FMT_RGB48; else
VAR_0->avctx->pix_fmt = AV_PIX_FMT_RGB24; break; } break; case 4:
VAR_0->avctx->pix_fmt = AV_PIX_FMT_RGBA; break; default:
VAR_0->avctx->pix_fmt = AV_PIX_FMT_NONE; break; } return 0; }