static int get_siz(Jpeg2000DecoderContext *s)
{
    int i;
    int ncomponents;
    uint32_t log2_chroma_wh = 0;
    const enum AVPixelFormat *possible_fmts = NULL;
    int possible_fmts_nb = 0;
    if (bytestream2_get_bytes_left(&s->g) < 36) {
        av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
    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
    ncomponents       = bytestream2_get_be16u(&s->g); // CSiz
    if (s->image_offset_x || s->image_offset_y) {
        avpriv_request_sample(s->avctx, "Support for image offsets");
        return AVERROR_PATCHWELCOME;
    if (av_image_check_size(s->width, s->height, 0, s->avctx)) {
        avpriv_request_sample(s->avctx, "Large Dimensions");
        return AVERROR_PATCHWELCOME;
    if (ncomponents <= 0) {
        av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
    if (ncomponents > 4) {
        avpriv_request_sample(s->avctx, "Support for %d components",
                              ncomponents);
        return AVERROR_PATCHWELCOME;
    s->ncomponents = ncomponents;
    if (s->tile_width <= 0 || s->tile_height <= 0) {
        av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
               s->tile_width, s->tile_height);
    if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
        av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
    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] || s->cdx[i] == 3 || s->cdx[i] > 4
            || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
            av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
        log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
    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(*s->tile)) {
        s->numXtiles = s->numYtiles = 0;
        return AVERROR(EINVAL);
    s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
    if (!s->tile) {
        s->numXtiles = s->numYtiles = 0;
        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);
    if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
        s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
        possible_fmts = xyz_pix_fmts;
        possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
    } else {
        switch (s->colour_space) {
        case 16:
            possible_fmts = rgb_pix_fmts;
            possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
            break;
        case 17:
            possible_fmts = gray_pix_fmts;
            possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
            break;
        case 18:
            possible_fmts = yuv_pix_fmts;
            possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
            break;
        default:
            possible_fmts = all_pix_fmts;
            possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
            break;
    for (i = 0; i < possible_fmts_nb; ++i) {
        if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
            s->avctx->pix_fmt = possible_fmts[i];
            break;
    if (i == possible_fmts_nb) {
        if (ncomponents == 4 &&
            s->cdy[0] == 1 && s->cdx[0] == 1 &&
            s->cdy[1] == 1 && s->cdx[1] == 1 &&
            s->cdy[2] == s->cdy[3] && s->cdx[2] == s->cdx[3]) {
            if (s->precision == 8 && s->cdy[2] == 2 && s->cdx[2] == 2 && !s->pal8) {
                s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
                s->cdef[0] = 0;
                s->cdef[1] = 1;
                s->cdef[2] = 2;
                s->cdef[3] = 3;
                i = 0;
    if (i == possible_fmts_nb) {
        av_log(s->avctx, AV_LOG_ERROR,
               "Unknown pix_fmt, profile: %d, colour_space: %d, "
               "components: %d, precision: %d\n"
               "cdx[0]: %d, cdy[0]: %d\n"
               "cdx[1]: %d, cdy[1]: %d\n"
               "cdx[2]: %d, cdy[2]: %d\n"
               "cdx[3]: %d, cdy[3]: %d\n",
               s->avctx->profile, s->colour_space, ncomponents, s->precision,
               s->cdx[0],
               s->cdy[0],
               ncomponents > 1 ? s->cdx[1] : 0,
               ncomponents > 1 ? s->cdy[1] : 0,
               ncomponents > 2 ? s->cdx[2] : 0,
               ncomponents > 2 ? s->cdy[2] : 0,
               ncomponents > 3 ? s->cdx[3] : 0,
               ncomponents > 3 ? s->cdy[3] : 0);
        return AVERROR_PATCHWELCOME;
    s->avctx->bits_per_raw_sample = s->precision;
    return 0;
static int get_siz(Jpeg2000DecoderContext *s)
{
    int i;
    int ncomponents;
    uint32_t log2_chroma_wh = 0;
    const enum AVPixelFormat *possible_fmts = NULL;
    int possible_fmts_nb = 0;
    if (bytestream2_get_bytes_left(&s->g) < 36) {
        av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
    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); 
    ncomponents       = bytestream2_get_be16u(&s->g); 
    if (s->image_offset_x || s->image_offset_y) {
        avpriv_request_sample(s->avctx, "Support for image offsets");
        return AVERROR_PATCHWELCOME;
    if (av_image_check_size(s->width, s->height, 0, s->avctx)) {
        avpriv_request_sample(s->avctx, "Large Dimensions");
        return AVERROR_PATCHWELCOME;
    if (ncomponents <= 0) {
        av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
    if (ncomponents > 4) {
        avpriv_request_sample(s->avctx, "Support for %d components",
                              ncomponents);
        return AVERROR_PATCHWELCOME;
    s->ncomponents = ncomponents;
    if (s->tile_width <= 0 || s->tile_height <= 0) {
        av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
               s->tile_width, s->tile_height);
    if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
        av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
    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] || s->cdx[i] == 3 || s->cdx[i] > 4
            || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
            av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
        log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
    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(*s->tile)) {
        s->numXtiles = s->numYtiles = 0;
        return AVERROR(EINVAL);
    s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
    if (!s->tile) {
        s->numXtiles = s->numYtiles = 0;
        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);
    if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
        s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
        possible_fmts = xyz_pix_fmts;
        possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
    } else {
        switch (s->colour_space) {
        case 16:
            possible_fmts = rgb_pix_fmts;
            possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
            break;
        case 17:
            possible_fmts = gray_pix_fmts;
            possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
            break;
        case 18:
            possible_fmts = yuv_pix_fmts;
            possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
            break;
        default:
            possible_fmts = all_pix_fmts;
            possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
            break;
    for (i = 0; i < possible_fmts_nb; ++i) {
        if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
            s->avctx->pix_fmt = possible_fmts[i];
            break;
    if (i == possible_fmts_nb) {
        if (ncomponents == 4 &&
            s->cdy[0] == 1 && s->cdx[0] == 1 &&
            s->cdy[1] == 1 && s->cdx[1] == 1 &&
            s->cdy[2] == s->cdy[3] && s->cdx[2] == s->cdx[3]) {
            if (s->precision == 8 && s->cdy[2] == 2 && s->cdx[2] == 2 && !s->pal8) {
                s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
                s->cdef[0] = 0;
                s->cdef[1] = 1;
                s->cdef[2] = 2;
                s->cdef[3] = 3;
                i = 0;
    if (i == possible_fmts_nb) {
        av_log(s->avctx, AV_LOG_ERROR,
               "Unknown pix_fmt, profile: %d, colour_space: %d, "
               "components: %d, precision: %d\n"
               "cdx[0]: %d, cdy[0]: %d\n"
               "cdx[1]: %d, cdy[1]: %d\n"
               "cdx[2]: %d, cdy[2]: %d\n"
               "cdx[3]: %d, cdy[3]: %d\n",
               s->avctx->profile, s->colour_space, ncomponents, s->precision,
               s->cdx[0],
               s->cdy[0],
               ncomponents > 1 ? s->cdx[1] : 0,
               ncomponents > 1 ? s->cdy[1] : 0,
               ncomponents > 2 ? s->cdx[2] : 0,
               ncomponents > 2 ? s->cdy[2] : 0,
               ncomponents > 3 ? s->cdx[3] : 0,
               ncomponents > 3 ? s->cdy[3] : 0);
        return AVERROR_PATCHWELCOME;
    s->avctx->bits_per_raw_sample = s->precision;
    return 0;
static int FUNC_0(Jpeg2000DecoderContext *VAR_0)
{
    int VAR_1;
    int VAR_2;
    uint32_t log2_chroma_wh = 0;
    const enum AVPixelFormat *VAR_3 = NULL;
    int VAR_4 = 0;
    if (bytestream2_get_bytes_left(&VAR_0->g) < 36) {
        av_log(VAR_0->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
    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_2       = bytestream2_get_be16u(&VAR_0->g); 
    if (VAR_0->image_offset_x || VAR_0->image_offset_y) {
        avpriv_request_sample(VAR_0->avctx, "Support for image offsets");
        return AVERROR_PATCHWELCOME;
    if (av_image_check_size(VAR_0->width, VAR_0->height, 0, VAR_0->avctx)) {
        avpriv_request_sample(VAR_0->avctx, "Large Dimensions");
        return AVERROR_PATCHWELCOME;
    if (VAR_2 <= 0) {
        av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
    if (VAR_2 > 4) {
        avpriv_request_sample(VAR_0->avctx, "Support for %d components",
                              VAR_2);
        return AVERROR_PATCHWELCOME;
    VAR_0->VAR_2 = VAR_2;
    if (VAR_0->tile_width <= 0 || VAR_0->tile_height <= 0) {
        av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
               VAR_0->tile_width, VAR_0->tile_height);
    if (bytestream2_get_bytes_left(&VAR_0->g) < 3 * VAR_0->VAR_2) {
        av_log(VAR_0->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", VAR_0->VAR_2);
    for (VAR_1 = 0; VAR_1 < VAR_0->VAR_2; 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] || VAR_0->cdx[VAR_1] == 3 || VAR_0->cdx[VAR_1] > 4
            || !VAR_0->cdy[VAR_1] || VAR_0->cdy[VAR_1] == 3 || VAR_0->cdy[VAR_1] > 4) {
            av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", VAR_0->cdx[VAR_1], VAR_0->cdy[VAR_1]);
        log2_chroma_wh |= VAR_0->cdy[VAR_1] >> 1 << VAR_1 * 4 | VAR_0->cdx[VAR_1] >> 1 << VAR_1 * 4 + 2;
    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(*VAR_0->tile)) {
        VAR_0->numXtiles = VAR_0->numYtiles = 0;
        return AVERROR(EINVAL);
    VAR_0->tile = av_mallocz_array(VAR_0->numXtiles * VAR_0->numYtiles, sizeof(*VAR_0->tile));
    if (!VAR_0->tile) {
        VAR_0->numXtiles = VAR_0->numYtiles = 0;
        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->VAR_2 * 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);
    if (VAR_0->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
        VAR_0->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
        VAR_3 = xyz_pix_fmts;
        VAR_4 = FF_ARRAY_ELEMS(xyz_pix_fmts);
    } else {
        switch (VAR_0->colour_space) {
        case 16:
            VAR_3 = rgb_pix_fmts;
            VAR_4 = FF_ARRAY_ELEMS(rgb_pix_fmts);
            break;
        case 17:
            VAR_3 = gray_pix_fmts;
            VAR_4 = FF_ARRAY_ELEMS(gray_pix_fmts);
            break;
        case 18:
            VAR_3 = yuv_pix_fmts;
            VAR_4 = FF_ARRAY_ELEMS(yuv_pix_fmts);
            break;
        default:
            VAR_3 = all_pix_fmts;
            VAR_4 = FF_ARRAY_ELEMS(all_pix_fmts);
            break;
    for (VAR_1 = 0; VAR_1 < VAR_4; ++VAR_1) {
        if (pix_fmt_match(VAR_3[VAR_1], VAR_2, VAR_0->precision, log2_chroma_wh, VAR_0->pal8)) {
            VAR_0->avctx->pix_fmt = VAR_3[VAR_1];
            break;
    if (VAR_1 == VAR_4) {
        if (VAR_2 == 4 &&
            VAR_0->cdy[0] == 1 && VAR_0->cdx[0] == 1 &&
            VAR_0->cdy[1] == 1 && VAR_0->cdx[1] == 1 &&
            VAR_0->cdy[2] == VAR_0->cdy[3] && VAR_0->cdx[2] == VAR_0->cdx[3]) {
            if (VAR_0->precision == 8 && VAR_0->cdy[2] == 2 && VAR_0->cdx[2] == 2 && !VAR_0->pal8) {
                VAR_0->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
                VAR_0->cdef[0] = 0;
                VAR_0->cdef[1] = 1;
                VAR_0->cdef[2] = 2;
                VAR_0->cdef[3] = 3;
                VAR_1 = 0;
    if (VAR_1 == VAR_4) {
        av_log(VAR_0->avctx, AV_LOG_ERROR,
               "Unknown pix_fmt, profile: %d, colour_space: %d, "
               "components: %d, precision: %d\n"
               "cdx[0]: %d, cdy[0]: %d\n"
               "cdx[1]: %d, cdy[1]: %d\n"
               "cdx[2]: %d, cdy[2]: %d\n"
               "cdx[3]: %d, cdy[3]: %d\n",
               VAR_0->avctx->profile, VAR_0->colour_space, VAR_2, VAR_0->precision,
               VAR_0->cdx[0],
               VAR_0->cdy[0],
               VAR_2 > 1 ? VAR_0->cdx[1] : 0,
               VAR_2 > 1 ? VAR_0->cdy[1] : 0,
               VAR_2 > 2 ? VAR_0->cdx[2] : 0,
               VAR_2 > 2 ? VAR_0->cdy[2] : 0,
               VAR_2 > 3 ? VAR_0->cdx[3] : 0,
               VAR_2 > 3 ? VAR_0->cdy[3] : 0);
        return AVERROR_PATCHWELCOME;
    VAR_0->avctx->bits_per_raw_sample = VAR_0->precision;
    return 0;
static int FUNC_0(Jpeg2000DecoderContext *VAR_0)
{ int VAR_1; int VAR_2; uint32_t log2_chroma_wh = 0; const enum AVPixelFormat *VAR_3 = NULL; int VAR_4 = 0; if (bytestream2_get_bytes_left(&VAR_0->g) < 36) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n"); 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_2       = bytestream2_get_be16u(&VAR_0->g); if (VAR_0->image_offset_x || VAR_0->image_offset_y) { avpriv_request_sample(VAR_0->avctx, "Support for image offsets"); return AVERROR_PATCHWELCOME; if (av_image_check_size(VAR_0->width, VAR_0->height, 0, VAR_0->avctx)) { avpriv_request_sample(VAR_0->avctx, "Large Dimensions"); return AVERROR_PATCHWELCOME; if (VAR_2 <= 0) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
if (VAR_2 > 4) { avpriv_request_sample(VAR_0->avctx, "Support for %d components",
VAR_2); return AVERROR_PATCHWELCOME; VAR_0->VAR_2 = VAR_2; if (VAR_0->tile_width <= 0 || VAR_0->tile_height <= 0) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
VAR_0->tile_width, VAR_0->tile_height); if (bytestream2_get_bytes_left(&VAR_0->g) < 3 * VAR_0->VAR_2) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", VAR_0->VAR_2); for (VAR_1 = 0; VAR_1 < VAR_0->VAR_2; 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] || VAR_0->cdx[VAR_1] == 3 || VAR_0->cdx[VAR_1] > 4
|| !VAR_0->cdy[VAR_1] || VAR_0->cdy[VAR_1] == 3 || VAR_0->cdy[VAR_1] > 4) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", VAR_0->cdx[VAR_1], VAR_0->cdy[VAR_1]); log2_chroma_wh |= VAR_0->cdy[VAR_1] >> 1 << VAR_1 * 4 | VAR_0->cdx[VAR_1] >> 1 << VAR_1 * 4 + 2; 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(*VAR_0->tile)) { VAR_0->numXtiles = VAR_0->numYtiles = 0; return AVERROR(EINVAL); VAR_0->tile = av_mallocz_array(VAR_0->numXtiles * VAR_0->numYtiles, sizeof(*VAR_0->tile)); if (!VAR_0->tile) { VAR_0->numXtiles = VAR_0->numYtiles = 0; 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->VAR_2 * 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); if (VAR_0->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
VAR_0->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) { VAR_3 = xyz_pix_fmts; VAR_4 = FF_ARRAY_ELEMS(xyz_pix_fmts); } else { switch (VAR_0->colour_space) { case 16:
VAR_3 = rgb_pix_fmts; VAR_4 = FF_ARRAY_ELEMS(rgb_pix_fmts); break; case 17:
VAR_3 = gray_pix_fmts; VAR_4 = FF_ARRAY_ELEMS(gray_pix_fmts); break; case 18:
VAR_3 = yuv_pix_fmts; VAR_4 = FF_ARRAY_ELEMS(yuv_pix_fmts); break; default:
VAR_3 = all_pix_fmts; VAR_4 = FF_ARRAY_ELEMS(all_pix_fmts); break; for (VAR_1 = 0; VAR_1 < VAR_4; ++VAR_1) { if (pix_fmt_match(VAR_3[VAR_1], VAR_2, VAR_0->precision, log2_chroma_wh, VAR_0->pal8)) { VAR_0->avctx->pix_fmt = VAR_3[VAR_1]; break; if (VAR_1 == VAR_4) { if (VAR_2 == 4 &&
VAR_0->cdy[0] == 1 && VAR_0->cdx[0] == 1 &&
VAR_0->cdy[1] == 1 && VAR_0->cdx[1] == 1 &&
VAR_0->cdy[2] == VAR_0->cdy[3] && VAR_0->cdx[2] == VAR_0->cdx[3]) { if (VAR_0->precision == 8 && VAR_0->cdy[2] == 2 && VAR_0->cdx[2] == 2 && !VAR_0->pal8) { VAR_0->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; VAR_0->cdef[0] = 0; VAR_0->cdef[1] = 1; VAR_0->cdef[2] = 2; VAR_0->cdef[3] = 3; VAR_1 = 0; if (VAR_1 == VAR_4) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Unknown pix_fmt, profile: %d, colour_space: %d, "
"components: %d, precision: %d\n"
"cdx[0]: %d, cdy[0]: %d\n"
"cdx[1]: %d, cdy[1]: %d\n"
"cdx[2]: %d, cdy[2]: %d\n"
"cdx[3]: %d, cdy[3]: %d\n",
VAR_0->avctx->profile, VAR_0->colour_space, VAR_2, VAR_0->precision,
VAR_0->cdx[0],
VAR_0->cdy[0],
VAR_2 > 1 ? VAR_0->cdx[1] : 0,
VAR_2 > 1 ? VAR_0->cdy[1] : 0,
VAR_2 > 2 ? VAR_0->cdx[2] : 0,
VAR_2 > 2 ? VAR_0->cdy[2] : 0,
VAR_2 > 3 ? VAR_0->cdx[3] : 0,
VAR_2 > 3 ? VAR_0->cdy[3] : 0); return AVERROR_PATCHWELCOME; VAR_0->avctx->bits_per_raw_sample = VAR_0->precision; return 0;