49 #define JP2_SIG_TYPE    0x6A502020 
   50 #define JP2_SIG_VALUE   0x0D0A870A 
   51 #define JP2_CODESTREAM  0x6A703263 
   52 #define JP2_HEADER      0x6A703268 
   63 #define HT_MIXED 0x80 // bit 7 of SPcod/SPcoc 
   76         if (
s->bit_index == 0) {
 
   77             s->bit_index = 7 + (bytestream2_get_byte(&
s->g) != 0xFF
u);
 
   80         res |= (bytestream2_peek_byte(&
s->g) >> 
s->bit_index) & 1;
 
   87     if (bytestream2_get_byte(&
s->g) == 0xff)
 
   97     int sp = -1, curval = 0;
 
  104     while (node && !node->
vis) {
 
  112         curval = stack[sp]->
val;
 
  114     while (curval < threshold && sp >= 0) {
 
  115         if (curval < stack[sp]->
val)
 
  116             curval = stack[sp]->
val;
 
  117         while (curval < threshold) {
 
  127         stack[sp]->
val = curval;
 
  134                          int bpc, uint32_t log2_chroma_wh, 
int pal8)
 
  141     if (
desc->nb_components != components) {
 
  145     switch (components) {
 
  147         match = match && 
desc->comp[3].depth >= bpc &&
 
  148                          (log2_chroma_wh >> 14 & 3) == 0 &&
 
  149                          (log2_chroma_wh >> 12 & 3) == 0;
 
  151         match = match && 
desc->comp[2].depth >= bpc &&
 
  152                          (log2_chroma_wh >> 10 & 3) == 
desc->log2_chroma_w &&
 
  153                          (log2_chroma_wh >>  8 & 3) == 
desc->log2_chroma_h;
 
  155         match = match && 
desc->comp[1].depth >= bpc &&
 
  156                          (log2_chroma_wh >>  6 & 3) == 
desc->log2_chroma_w &&
 
  157                          (log2_chroma_wh >>  4 & 3) == 
desc->log2_chroma_h;
 
  160         match = match && 
desc->comp[0].depth >= bpc &&
 
  161                          (log2_chroma_wh >>  2 & 3) == 0 &&
 
  162                          (log2_chroma_wh       & 3) == 0 &&
 
  170 #define RGB_PIXEL_FORMATS   AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64 
  171 #define GRAY_PIXEL_FORMATS  AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16 
  172 #define YUV_PIXEL_FORMATS   AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \ 
  173                             AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \ 
  174                             AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \ 
  175                             AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \ 
  176                             AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \ 
  177                             AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \ 
  178                             AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \ 
  179                             AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \ 
  180                             AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \ 
  181                             AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \ 
  182                             AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16 
  183 #define XYZ_PIXEL_FORMATS   AV_PIX_FMT_XYZ12 
  201     uint32_t log2_chroma_wh = 0;
 
  203     int possible_fmts_nb = 0;
 
  213     s->avctx->profile = bytestream2_get_be16u(&
s->g); 
 
  214     s->width          = bytestream2_get_be32u(&
s->g); 
 
  215     s->height         = bytestream2_get_be32u(&
s->g); 
 
  216     s->image_offset_x = bytestream2_get_be32u(&
s->g); 
 
  217     s->image_offset_y = bytestream2_get_be32u(&
s->g); 
 
  218     s->tile_width     = bytestream2_get_be32u(&
s->g); 
 
  219     s->tile_height    = bytestream2_get_be32u(&
s->g); 
 
  220     s->tile_offset_x  = bytestream2_get_be32u(&
s->g); 
 
  221     s->tile_offset_y  = bytestream2_get_be32u(&
s->g); 
 
  222     ncomponents       = bytestream2_get_be16u(&
s->g); 
 
  229     if (ncomponents <= 0) {
 
  235     if (ncomponents > 4) {
 
  241     if (
s->tile_offset_x < 0 || 
s->tile_offset_y < 0 ||
 
  242         s->image_offset_x < 
s->tile_offset_x ||
 
  243         s->image_offset_y < 
s->tile_offset_y ||
 
  244         s->tile_width  + (
int64_t)
s->tile_offset_x <= 
s->image_offset_x ||
 
  245         s->tile_height + (
int64_t)
s->tile_offset_y <= 
s->image_offset_y
 
  251     if (
s->image_offset_x >= 
s->width || 
s->image_offset_y >= 
s->height) {
 
  256     if (
s->reduction_factor && (
s->image_offset_x || 
s->image_offset_y) ){
 
  257         av_log(
s->avctx, 
AV_LOG_ERROR, 
"reduction factor with image offsets is not fully implemented");
 
  261     s->ncomponents = ncomponents;
 
  263     if (
s->tile_width <= 0 || 
s->tile_height <= 0) {
 
  265                s->tile_width, 
s->tile_height);
 
  270         av_log(
s->avctx, 
AV_LOG_ERROR, 
"Insufficient space for %d components in SIZ\n", 
s->ncomponents);
 
  274     for (
i = 0; 
i < 
s->ncomponents; 
i++) {
 
  275         if (
s->cdef[
i] < 0) {
 
  276             for (
i = 0; 
i < 
s->ncomponents; 
i++) {
 
  279             if ((
s->ncomponents & 1) == 0)
 
  280                 s->cdef[
s->ncomponents-1] = 0;
 
  286     for (
i = 0; 
i < 
s->ncomponents; 
i++)
 
  287         cdef_used |= 1<<
s->cdef[
i];
 
  290     if (cdef_used != ((
int[]){0,2,3,14,15})[
s->ncomponents])
 
  293     for (
i = 0; 
i < 
s->ncomponents; 
i++) { 
 
  294         uint8_t x    = bytestream2_get_byteu(&
s->g);
 
  295         s->cbps[
i]   = (x & 0x7f) + 1;
 
  296         s->precision = 
FFMAX(
s->cbps[
i], 
s->precision);
 
  297         s->sgnd[
i]   = !!(x & 0x80);
 
  298         s->cdx[
i]    = bytestream2_get_byteu(&
s->g);
 
  299         s->cdy[
i]    = bytestream2_get_byteu(&
s->g);
 
  300         if (   !
s->cdx[
i] || 
s->cdx[
i] == 3 || 
s->cdx[
i] > 4
 
  301             || !
s->cdy[
i] || 
s->cdy[
i] == 3 || 
s->cdy[
i] > 4) {
 
  305         int i_remapped = 
s->cdef[
i] ? 
s->cdef[
i]-1 : (
s->ncomponents-1);
 
  307         log2_chroma_wh |= 
s->cdy[
i] >> 1 << i_remapped * 4 | 
s->cdx[
i] >> 1 << i_remapped * 4 + 2;
 
  314     if (
s->numXtiles * (uint64_t)
s->numYtiles > INT_MAX/
sizeof(*
s->tile) ||
 
  317         s->numXtiles = 
s->numYtiles = 0;
 
  321     s->tile = 
av_calloc(
s->numXtiles * 
s->numYtiles, 
sizeof(*
s->tile));
 
  323         s->numXtiles = 
s->numYtiles = 0;
 
  327     for (
i = 0; 
i < 
s->numXtiles * 
s->numYtiles; 
i++) {
 
  337                                                s->reduction_factor);
 
  339                                                s->reduction_factor);
 
  342     for (
i = 1; 
i < 
s->ncomponents; 
i++) {
 
  356         switch (
s->colour_space) {
 
  376         && !
pix_fmt_match(
s->avctx->pix_fmt, ncomponents, 
s->precision, log2_chroma_wh, 
s->pal8))
 
  379         for (
i = 0; 
i < possible_fmts_nb; ++
i) {
 
  380             if (
pix_fmt_match(possible_fmts[
i], ncomponents, 
s->precision, log2_chroma_wh, 
s->pal8)) {
 
  381                 s->avctx->pix_fmt = possible_fmts[
i];
 
  386     if (
i == possible_fmts_nb) {
 
  387         if (ncomponents == 4 &&
 
  388             s->cdy[0] == 1 && 
s->cdx[0] == 1 &&
 
  389             s->cdy[1] == 1 && 
s->cdx[1] == 1 &&
 
  390             s->cdy[2] == 
s->cdy[3] && 
s->cdx[2] == 
s->cdx[3]) {
 
  391             if (
s->precision == 8 && 
s->cdy[2] == 2 && 
s->cdx[2] == 2 && !
s->pal8) {
 
  399         } 
else if (ncomponents == 3 && 
s->precision == 8 &&
 
  400                    s->cdx[0] == 
s->cdx[1] && 
s->cdx[0] == 
s->cdx[2] &&
 
  401                    s->cdy[0] == 
s->cdy[1] && 
s->cdy[0] == 
s->cdy[2]) {
 
  404         } 
else if (ncomponents == 2 && 
s->precision == 8 &&
 
  405                    s->cdx[0] == 
s->cdx[1] && 
s->cdy[0] == 
s->cdy[1]) {
 
  408         } 
else if (ncomponents == 2 && 
s->precision == 16 &&
 
  409                    s->cdx[0] == 
s->cdx[1] && 
s->cdy[0] == 
s->cdy[1]) {
 
  412         } 
else if (ncomponents == 1 && 
s->precision == 8) {
 
  415         } 
else if (ncomponents == 1 && 
s->precision == 12) {
 
  422     if (
i == possible_fmts_nb) {
 
  424                "Unknown pix_fmt, profile: %d, colour_space: %d, " 
  425                "components: %d, precision: %d\n" 
  426                "cdx[0]: %d, cdy[0]: %d\n" 
  427                "cdx[1]: %d, cdy[1]: %d\n" 
  428                "cdx[2]: %d, cdy[2]: %d\n" 
  429                "cdx[3]: %d, cdy[3]: %d\n",
 
  430                s->avctx->profile, 
s->colour_space, ncomponents, 
s->precision,
 
  433                ncomponents > 1 ? 
s->cdx[1] : 0,
 
  434                ncomponents > 1 ? 
s->cdy[1] : 0,
 
  435                ncomponents > 2 ? 
s->cdx[2] : 0,
 
  436                ncomponents > 2 ? 
s->cdy[2] : 0,
 
  437                ncomponents > 3 ? 
s->cdx[3] : 0,
 
  438                ncomponents > 3 ? 
s->cdy[3] : 0);
 
  441     s->avctx->bits_per_raw_sample = 
s->precision;
 
  448     uint16_t Ccap_i[32] = { 0 };
 
  457     Pcap = bytestream2_get_be32u(&
s->g);
 
  458     s->isHT = (Pcap >> (31 - (15 - 1))) & 1;
 
  459     for (
int i = 0; 
i < 32; 
i++) {
 
  460         if ((Pcap >> (31 - 
i)) & 1)
 
  461             Ccap_i[
i] = bytestream2_get_be16u(&
s->g);
 
  463     Ccap_15 = Ccap_i[14];
 
  467         switch ((Ccap_15 >> 14) & 0x3) {
 
  483         if ((Ccap_15 >> 13) & 1) {
 
  488         s->Ccap15_b12 = (Ccap_15 >> 12) & 1;
 
  490         s->Ccap15_b11 = (Ccap_15 >> 11) & 1;
 
  492         s->Ccap15_b05 = (Ccap_15 >> 5) & 1;
 
  500             s->HT_B = 4 * (
P - 19) + 27;
 
  524     c->nreslevels = bytestream2_get_byteu(&
s->g) + 1;
 
  530     if (
c->nreslevels <= 
s->reduction_factor) {
 
  535         av_log(
s->avctx, 
AV_LOG_ERROR, 
"reduction_factor too large for this bitstream, max is %d\n", 
c->nreslevels - 1);
 
  536         s->reduction_factor = 
c->nreslevels - 1;
 
  541     c->nreslevels2decode = 
c->nreslevels - 
s->reduction_factor;
 
  543     c->log2_cblk_width  = (bytestream2_get_byteu(&
s->g) & 15) + 2; 
 
  544     c->log2_cblk_height = (bytestream2_get_byteu(&
s->g) & 15) + 2; 
 
  546     if (
c->log2_cblk_width > 10 || 
c->log2_cblk_height > 10 ||
 
  547         c->log2_cblk_width + 
c->log2_cblk_height > 12) {
 
  552     c->cblk_style = bytestream2_get_byteu(&
s->g);
 
  553     if (
c->cblk_style != 0) { 
 
  562     c->transform = bytestream2_get_byteu(&
s->g); 
 
  566 #if FF_API_CODEC_PROPS 
  576         for (
i = 0; 
i < 
c->nreslevels; 
i++) {
 
  577             byte = bytestream2_get_byte(&
s->g);
 
  578             c->log2_prec_widths[
i]  =  
byte       & 0x0F;    
 
  579             c->log2_prec_heights[
i] = (
byte >> 4) & 0x0F;    
 
  581                 if (
c->log2_prec_widths[
i] == 0 || 
c->log2_prec_heights[
i] == 0) {
 
  583                            c->log2_prec_widths[
i], 
c->log2_prec_heights[
i]);
 
  584                     c->log2_prec_widths[
i] = 
c->log2_prec_heights[
i] = 1;
 
  589         memset(
c->log2_prec_widths , 15, 
sizeof(
c->log2_prec_widths ));
 
  590         memset(
c->log2_prec_heights, 15, 
sizeof(
c->log2_prec_heights));
 
  597                    const uint8_t *properties)
 
  607     tmp.csty = bytestream2_get_byteu(&
s->g);
 
  610     tmp.prog_order = bytestream2_get_byteu(&
s->g);
 
  612     tmp.nlayers    = bytestream2_get_be16u(&
s->g);
 
  613     tmp.mct        = bytestream2_get_byteu(&
s->g); 
 
  615     if (
tmp.mct && 
s->ncomponents < 3) {
 
  617                "MCT %"PRIu8
" with too few components (%d)\n",
 
  618                tmp.mct, 
s->ncomponents);
 
  625     for (compno = 0; compno < 
s->ncomponents; compno++)
 
  626         if (!(properties[compno] & 
HAD_COC))
 
  627             memcpy(
c + compno, &
tmp, 
sizeof(
tmp));
 
  637     uint8_t has_eph, has_sop;
 
  644     compno = bytestream2_get_byteu(&
s->g);
 
  646     if (compno >= 
s->ncomponents) {
 
  648                "Invalid compno %d. There are %d components in the image.\n",
 
  649                compno, 
s->ncomponents);
 
  656     c->csty = bytestream2_get_byteu(&
s->g);
 
  671     compno = (
s->ncomponents < 257)? bytestream2_get_byte(&
s->g):
 
  672                                      bytestream2_get_be16u(&
s->g);
 
  673     if (bytestream2_get_byte(&
s->g)) {
 
  680     if (compno < s->ncomponents) {
 
  682         if (
s->curtileno == -1) {
 
  683             v =  bytestream2_get_byte(&
s->g);
 
  686             s->roi_shift[compno] = v;
 
  688             if (
s->tile[
s->curtileno].tp_idx != 0)
 
  690             v = bytestream2_get_byte(&
s->g);
 
  693             s->tile[
s->curtileno].comp[compno].roi_shift = v;
 
  708     x = bytestream2_get_byteu(&
s->g); 
 
  718         for (
i = 0; 
i < n; 
i++)
 
  719             q->
expn[
i] = bytestream2_get_byteu(&
s->g) >> 3;
 
  723         x          = bytestream2_get_be16u(&
s->g);
 
  724         q->
expn[0] = x >> 11;
 
  725         q->
mant[0] = x & 0x7ff;
 
  727             int curexpn = 
FFMAX(0, q->
expn[0] - (
i - 1) / 3);
 
  728             q->
expn[
i] = curexpn;
 
  736         for (
i = 0; 
i < n; 
i++) {
 
  737             x          = bytestream2_get_be16u(&
s->g);
 
  738             q->
expn[
i] = x >> 11;
 
  739             q->
mant[
i] = x & 0x7ff;
 
  747                    const uint8_t *properties)
 
  752     memset(&
tmp, 0, 
sizeof(
tmp));
 
  756     for (compno = 0; compno < 
s->ncomponents; compno++)
 
  757         if (!(properties[compno] & 
HAD_QCC))
 
  758             memcpy(q + compno, &
tmp, 
sizeof(
tmp));
 
  772     compno = bytestream2_get_byteu(&
s->g);
 
  774     if (compno >= 
s->ncomponents) {
 
  776                "Invalid compno %d. There are %d components in the image.\n",
 
  777                compno, 
s->ncomponents);
 
  782     return get_qcx(
s, n - 1, q + compno);
 
  788     int elem_size = 
s->ncomponents <= 257 ? 7 : 9;
 
  801     tmp.nb_poc = (
size - 2) / elem_size;
 
  807     for (
i = 0; 
i<
tmp.nb_poc; 
i++) {
 
  809         e->
RSpoc  = bytestream2_get_byteu(&
s->g);
 
  810         e->
CSpoc  = bytestream2_get_byteu(&
s->g);
 
  811         e->
LYEpoc = bytestream2_get_be16u(&
s->g);
 
  812         e->
REpoc  = bytestream2_get_byteu(&
s->g);
 
  813         e->
CEpoc  = bytestream2_get_byteu(&
s->g);
 
  814         e->
Ppoc   = bytestream2_get_byteu(&
s->g);
 
  817         if (e->
CEpoc > 
s->ncomponents)
 
  818             e->
CEpoc = 
s->ncomponents;
 
  829     if (!
p->nb_poc || 
p->is_default) {
 
  836         memcpy(
p->poc + 
p->nb_poc, 
tmp.poc, 
tmp.nb_poc * 
sizeof(
tmp.poc[0]));
 
  837         p->nb_poc += 
tmp.nb_poc;
 
  858     Isot = bytestream2_get_be16u(&
s->g);        
 
  859     if (Isot >= 
s->numXtiles * 
s->numYtiles)
 
  863     Psot  = bytestream2_get_be32u(&
s->g);       
 
  864     TPsot = bytestream2_get_byteu(&
s->g);       
 
  867     bytestream2_get_byteu(&
s->g);               
 
  882     s->tile[Isot].tp_idx = TPsot;
 
  883     tp             = 
s->tile[Isot].tile_part + TPsot;
 
  885     tp->
tp_end     = 
s->g.buffer + Psot - n - 2;
 
  893         memcpy(&
tile->poc  , &
s->poc  , 
sizeof(
tile->poc));
 
  894         tile->poc.is_default = 1;
 
  902     if (
s->ncomponents*4 != n - 2) {
 
  928     uint8_t Stlm, ST, 
SP, tile_tlm, 
i;
 
  929     bytestream2_get_byte(&
s->g);               
 
  930     Stlm = bytestream2_get_byte(&
s->g);
 
  933     ST = (Stlm >> 4) & 0x03;
 
  939     SP       = (Stlm >> 6) & 0x01;
 
  940     tile_tlm = (n - 4) / ((
SP + 1) * 2 + ST);
 
  941     for (
i = 0; 
i < tile_tlm; 
i++) {
 
  946             bytestream2_get_byte(&
s->g);
 
  949             bytestream2_get_be16(&
s->g);
 
  953             bytestream2_get_be16(&
s->g);
 
  955             bytestream2_get_be32(&
s->g);
 
  972      bytestream2_get_byte(&
s->g);
 
  974     for (
i = 0; 
i < n - 3; 
i++) {
 
  975         v = bytestream2_get_byte(&
s->g);
 
  991     bytestream2_get_byte(&
s->g); 
 
  993                      s->packed_headers_size + n - 3);
 
  995         s->packed_headers = 
new;
 
  999     memset(&
s->packed_headers_stream, 0, 
sizeof(
s->packed_headers_stream));
 
 1002     s->packed_headers_size += n - 3;
 
 1016     if (
s->curtileno < 0)
 
 1019     tile = &
s->tile[
s->curtileno];
 
 1020     if (
tile->tp_idx != 0) {
 
 1022                "PPT marker can occur only on first tile part of a tile.\n");
 
 1027     bytestream2_get_byte(&
s->g); 
 
 1029                      tile->packed_headers_size + n - 3);
 
 1031         tile->packed_headers = 
new;
 
 1034     memset(&
tile->packed_headers_stream, 0, 
sizeof(
tile->packed_headers_stream));
 
 1036     tile->packed_headers_size += n - 3;
 
 1044     int tilex = tileno % 
s->numXtiles;
 
 1045     int tiley = tileno / 
s->numXtiles;
 
 1051     tile->coord[0][0] = 
av_clip(tilex       * (
int64_t)
s->tile_width  + 
s->tile_offset_x, 
s->image_offset_x, 
s->width);
 
 1052     tile->coord[0][1] = 
av_clip((tilex + 1) * (
int64_t)
s->tile_width  + 
s->tile_offset_x, 
s->image_offset_x, 
s->width);
 
 1053     tile->coord[1][0] = 
av_clip(tiley       * (
int64_t)
s->tile_height + 
s->tile_offset_y, 
s->image_offset_y, 
s->height);
 
 1054     tile->coord[1][1] = 
av_clip((tiley + 1) * (
int64_t)
s->tile_height + 
s->tile_offset_y, 
s->image_offset_y, 
s->height);
 
 1056     for (compno = 0; compno < 
s->ncomponents; compno++) {
 
 1062         comp->coord_o[0][0] = 
tile->coord[0][0];
 
 1063         comp->coord_o[0][1] = 
tile->coord[0][1];
 
 1064         comp->coord_o[1][0] = 
tile->coord[1][0];
 
 1065         comp->coord_o[1][1] = 
tile->coord[1][1];
 
 1077         if (!
comp->roi_shift)
 
 1078             comp->roi_shift = 
s->roi_shift[compno];
 
 1081         if (
s->isHT && (!
s->Ccap15_b05) && (!codsty->
transform)) {
 
 1082             av_log(
s->avctx, 
AV_LOG_ERROR, 
"Transformation = 0 (lossy DWT) is found in HTREV HT set\n");
 
 1086             av_log(
s->avctx, 
AV_LOG_ERROR, 
"SPcod/SPcoc value does not match bit 14-15 values of Ccap15\n");
 
 1090                                              s->cbps[compno], 
s->cdx[compno],
 
 1091                                              s->cdy[compno], 
s->avctx))
 
 1106         return num < 0 ? num : 3 + num;
 
 1108         return num < 0 ? num : 6 + num;
 
 1110     return num < 0 ? num : 37 + num;
 
 1127     s->g = 
tile->tile_part[*tp_index].header_tpg;
 
 1131             s->g = 
tile->tile_part[++(*tp_index)].tpg;
 
 1141     s->g = 
tile->tile_part[*tp_index].tpg;
 
 1145     while (is_endof_tp) {
 
 1147             s->g = 
tile->tile_part[++(*tp_index)].tpg;
 
 1157             av_log(
s->avctx, 
AV_LOG_ERROR, 
"SOP marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
 
 1164                                   int layno, 
const uint8_t *expn, 
int numgbits)
 
 1166     int bandno, cblkno, 
ret, nb_code_blocks;
 
 1169     if (layno < rlevel->band[0].prec[precno].decoded_layers)
 
 1175     else if (
tile->has_ppt)
 
 1176         s->g = 
tile->packed_headers_stream;
 
 1186     for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
 
 1190         if (band->
coord[0][0] == band->
coord[0][1] ||
 
 1195         for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
 
 1197             int incl, newpasses, llen;
 
 1211                     int v = expn[bandno] + numgbits - 1 - (zbp - 
tile->comp->roi_shift);
 
 1212                     if (v < 0 || v > 30) {
 
 1214                                "nonzerobits %d invalid or unsupported\n", v);
 
 1227                 uint8_t bypass_term_threshold = 0;
 
 1228                 uint8_t bits_to_read = 0;
 
 1229                 uint32_t segment_bytes = 0;
 
 1231                 uint8_t next_segment_passes = 0;
 
 1232                 int32_t href_passes, pass_bound;
 
 1233                 uint32_t tmp_length = 0;
 
 1234                 int32_t newpasses_copy, npasses_copy;
 
 1246                                           "Block with length beyond 16 bits");
 
 1263                 newpasses_copy = newpasses;
 
 1269                         while (newpasses1 < newpasses_copy) {
 
 1276                         npasses_copy += newpasses1;
 
 1277                         newpasses_copy -= newpasses1;
 
 1278                     } 
while (newpasses_copy);
 
 1282                     href_passes = (cblk->
npasses + newpasses - 1) % 3;
 
 1283                     segment_passes = newpasses - href_passes;
 
 1285                     bits_to_read = cblk->
lblock;
 
 1286                     if (segment_passes < 1) {
 
 1289                         segment_passes = newpasses;
 
 1290                         while (pass_bound <= segment_passes) {
 
 1292                             pass_bound += pass_bound;
 
 1294                         segment_bytes = 
get_bits(
s, bits_to_read);
 
 1295                         if (segment_bytes) {
 
 1305                         while (pass_bound <= segment_passes) {
 
 1307                             pass_bound += pass_bound;
 
 1309                         segment_bytes = 
get_bits(
s, bits_to_read);
 
 1310                         if (segment_bytes) {
 
 1314                                 if (segment_bytes < 2)
 
 1316                                 next_segment_passes = 2;
 
 1320                             } 
else if (cblk->
lblock > 3 && segment_bytes > 1
 
 1321                                        && (segment_bytes >> (bits_to_read - 1)) == 0) {
 
 1323                                 next_segment_passes = 2;
 
 1331                                 segment_passes = newpasses;
 
 1332                                 while (pass_bound <= segment_passes) {
 
 1334                                     pass_bound += pass_bound;
 
 1335                                     segment_bytes <<= 1;
 
 1345                             segment_passes = newpasses;
 
 1346                             if (pass_bound <= segment_passes) {
 
 1349                                     pass_bound += pass_bound;
 
 1350                                     segment_bytes <<= 1;
 
 1352                                     if (pass_bound > segment_passes)
 
 1355                                 if (segment_bytes) {
 
 1368                     if(bits_to_read != 0)
 
 1370                     segment_passes = cblk->
npasses % 3;
 
 1371                     if (segment_passes == 0) {
 
 1374                         next_segment_passes = 2;
 
 1375                         if (segment_bytes == 1)
 
 1380                         segment_passes = newpasses > 1 ? 3 - segment_passes : 1;
 
 1381                         next_segment_passes = 1;
 
 1382                         bits_to_read = 
av_log2(segment_passes);
 
 1384                     bits_to_read = (uint8_t) (bits_to_read + cblk->
lblock);
 
 1385                     segment_bytes = 
get_bits(
s, bits_to_read);
 
 1390                     bits_to_read = (uint8_t) cblk->
lblock + 
av_log2((uint8_t) newpasses);
 
 1391                     segment_bytes = 
get_bits(
s, bits_to_read);
 
 1392                     segment_passes = newpasses;
 
 1395                     bits_to_read = cblk->
lblock;
 
 1396                     segment_bytes = 
get_bits(
s, bits_to_read);
 
 1398                     next_segment_passes = 1;
 
 1401                     bypass_term_threshold = 10;
 
 1402                     if(bits_to_read != 0)
 
 1404                     if (cblk->
npasses < bypass_term_threshold) {
 
 1406                         segment_passes = bypass_term_threshold - cblk->
npasses;
 
 1407                         if (segment_passes > newpasses)
 
 1408                             segment_passes = newpasses;
 
 1409                         while ((2 << bits_to_read) <= segment_passes)
 
 1411                         next_segment_passes = 2;
 
 1412                     } 
else if ((cblk->
npasses - bypass_term_threshold) % 3 < 2) {
 
 1414                         segment_passes = newpasses > 1 ? 2 - (cblk->
npasses - bypass_term_threshold) % 3 : 1;
 
 1415                         bits_to_read = 
av_log2(segment_passes);
 
 1416                         next_segment_passes = 1;
 
 1420                         next_segment_passes = 2;
 
 1422                     bits_to_read = (uint8_t) (bits_to_read + cblk->
lblock);
 
 1423                     segment_bytes = 
get_bits(
s, bits_to_read);
 
 1430                     newpasses -= (uint8_t) segment_passes;
 
 1431                     while (newpasses > 0) {
 
 1432                         segment_passes = newpasses > 1 ? next_segment_passes : 1;
 
 1433                         next_segment_passes = (uint8_t) (3 - next_segment_passes);
 
 1434                         bits_to_read = (uint8_t) (cblk->
lblock + 
av_log2(segment_passes));
 
 1435                         segment_bytes = 
get_bits(
s, bits_to_read);
 
 1436                         newpasses -= (uint8_t) (segment_passes);
 
 1445                     newpasses -= (uint8_t) (segment_passes);
 
 1446                     while (newpasses > 0) {
 
 1447                         if (bypass_term_threshold != 0) {
 
 1448                             segment_passes = newpasses > 1 ? next_segment_passes : 1;
 
 1449                             next_segment_passes = (uint8_t) (3 - next_segment_passes);
 
 1450                             bits_to_read = (uint8_t) (cblk->
lblock + 
av_log2(segment_passes));
 
 1455                             bits_to_read = cblk->
lblock;
 
 1457                         segment_bytes = 
get_bits(
s, bits_to_read);
 
 1458                         newpasses -= (uint8_t) (segment_passes);
 
 1467                     tmp_length = (tmp_length < cblk->lengthinc[
i]) ? cblk->
lengthinc[
i] : tmp_length;
 
 1495             av_log(
s->avctx, 
AV_LOG_ERROR, 
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
 
 1500         tile->tile_part[*tp_index].header_tpg = 
s->g;
 
 1502     } 
else if (
tile->has_ppt) {
 
 1503         tile->packed_headers_stream = 
s->g;
 
 1506     for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
 
 1511         for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
 
 1515             for (cwsno = 0; cwsno < cblk->
nb_lengthinc; cwsno ++) {
 
 1528                         "Block length %"PRIu16
" or lengthinc %d is too large, left %d\n",
 
 1550     tile->tile_part[*tp_index].tpg = 
s->g;
 
 1558             av_log(
s->avctx, 
AV_LOG_ERROR, 
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
 
 1561         tile->tile_part[*tp_index].header_tpg = 
s->g;
 
 1563     } 
else if (
tile->has_ppt) {
 
 1564         tile->packed_headers_stream = 
s->g;
 
 1567     tile->tile_part[*tp_index].tpg = 
s->g;
 
 1572                                              int RSpoc, 
int CSpoc,
 
 1573                                              int LYEpoc, 
int REpoc, 
int CEpoc,
 
 1574                                              int Ppoc, 
int *tp_index)
 
 1577     int layno, reslevelno, compno, precno, ok_reslevel;
 
 1585         for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
 
 1587             for (layno = 0; layno < LYEpoc; layno++) {
 
 1588                 for (compno = CSpoc; compno < CEpoc; compno++) {
 
 1591                     if (reslevelno < codsty->nreslevels) {
 
 1599                                                               qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
 
 1610         for (layno = 0; layno < LYEpoc; layno++) {
 
 1612             for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
 
 1614                 for (compno = CSpoc; compno < CEpoc; compno++) {
 
 1617                     if (reslevelno < codsty->nreslevels) {
 
 1625                                                               qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
 
 1636         for (compno = CSpoc; compno < CEpoc; compno++) {
 
 1646             for (reslevelno = RSpoc; reslevelno < 
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
 
 1647                 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno; 
 
 1652             if (step_x >= 31 || step_y >= 31){
 
 1659             for (y = 
tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
 
 1660                 for (x = 
tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
 
 1661                     for (reslevelno = RSpoc; reslevelno < 
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
 
 1662                         unsigned prcx, prcy;
 
 1663                         uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno; 
 
 1665                         int xc = x / 
s->cdx[compno];
 
 1666                         int yc = y / 
s->cdy[compno];
 
 1688                         for (layno = 0; layno < LYEpoc; layno++) {
 
 1691                                                               qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
 
 1704         for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
 
 1708             for (compno = CSpoc; compno < CEpoc; compno++) {
 
 1712                 if (reslevelno < codsty->nreslevels) {
 
 1713                     uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno; 
 
 1722             for (y = 
tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
 
 1723                 for (x = 
tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
 
 1724                     for (compno = CSpoc; compno < CEpoc; compno++) {
 
 1728                         uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno; 
 
 1730                         unsigned prcx, prcy;
 
 1733                         if (!
s->cdx[compno] || !
s->cdy[compno])
 
 1742                         if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
 
 1746                         if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
 
 1765                         for (layno = 0; layno < LYEpoc; layno++) {
 
 1769                                                               qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
 
 1783         for (compno = CSpoc; compno < CEpoc; compno++) {
 
 1787             for (reslevelno = RSpoc; reslevelno < 
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
 
 1788                 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno; 
 
 1794         if (step_x >= 31 || step_y >= 31){
 
 1801         for (y = 
tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
 
 1802             for (x = 
tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
 
 1803                 for (compno = CSpoc; compno < CEpoc; compno++) {
 
 1808                     if (!
s->cdx[compno] || !
s->cdy[compno])
 
 1811                     for (reslevelno = RSpoc; reslevelno < 
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
 
 1812                         unsigned prcx, prcy;
 
 1813                         uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno; 
 
 1820                         if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
 
 1824                         if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
 
 1842                         for (layno = 0; layno < LYEpoc; layno++) {
 
 1845                                                               qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
 
 1869     if (
tile->poc.nb_poc) {
 
 1870         for (
i=0; 
i<
tile->poc.nb_poc; 
i++) {
 
 1885             tile->codsty[0].nlayers,
 
 1888             tile->codsty[0].prog_order,
 
 1900                            int bpno, 
int bandno,
 
 1901                            int vert_causal_ctx_csty_symbol)
 
 1903     int mask = 3 << (bpno - 1), y0, x, y;
 
 1905     for (y0 = 0; y0 < 
height; y0 += 4)
 
 1906         for (x = 0; x < 
width; x++)
 
 1907             for (y = y0; y < 
height && y < y0 + 4; y++) {
 
 1908                 int flags_mask = -1;
 
 1909                 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
 
 1931                            int bpno, 
int vert_causal_ctx_csty_symbol)
 
 1936     phalf = 1 << (bpno - 1);
 
 1938     for (y0 = 0; y0 < 
height; y0 += 4)
 
 1939         for (x = 0; x < 
width; x++)
 
 1940             for (y = y0; y < 
height && y < y0 + 4; y++)
 
 1942                     int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
 
 1947                         t1->
data[(y) * t1->
stride + x] |= phalf << 1;
 
 1949                         t1->
data[(y) * t1->
stride + x] &= ~(phalf << 1);
 
 1958                            int seg_symbols, 
int vert_causal_ctx_csty_symbol)
 
 1960     int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
 
 1962     for (y0 = 0; y0 < 
height; y0 += 4) {
 
 1963         for (x = 0; x < 
width; x++) {
 
 1964             int flags_mask = -1;
 
 1965             if (vert_causal_ctx_csty_symbol)
 
 1985             for (y = y0 + runlen; y < y0 + 4 && y < 
height; y++) {
 
 1986                 int flags_mask = -1;
 
 1987                 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
 
 2016                    "Segmentation symbol value incorrect\n");
 
 2022                        int width, 
int height, 
int bandpos, uint8_t roi_shift, 
const int M_b)
 
 2024     int passno = cblk->
npasses, pass_t = 2, bpno = cblk->
nonzerobits - 1 + 31 - M_b - 1 - roi_shift;
 
 2046         if (bpno < 0 || bpno > 29) {
 
 2053                            vert_causal_ctx_csty_symbol);
 
 2062                            vert_causal_ctx_csty_symbol);
 
 2100     for (
int y = 0; y < 
height; y++) {
 
 2101         for (
int x = 0; x < 
width; x++) {
 
 2103             const uint32_t 
mask  = UINT32_MAX >> (M_b + 1); 
 
 2105             n = x + (y * t1->
stride);
 
 2107             sign = 
val & INT32_MIN;
 
 2110             if (roi_shift && (((uint32_t)
val & ~
mask) == 0))
 
 2131     const int downshift = 31 - M_b;
 
 2133     fscale /= (
float)(1 << downshift);
 
 2134     for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
 
 2135         float *datap = &
comp->f_data[(
comp->coord[0][1] - 
comp->coord[0][0]) * (y + j) + x];
 
 2137         for (
i = 0; 
i < 
w; ++
i) {
 
 2140                 val = -(
val & INT32_MAX);
 
 2152     const int downshift = 31 - M_b;
 
 2154     for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
 
 2158             for (
i = 0; 
i < 
w; ++
i) {
 
 2161                     val = -((
val & INT32_MAX) >> downshift);
 
 2168             for (
i = 0; 
i < 
w; ++
i) {
 
 2171                     val = -((
val & INT32_MAX) >> downshift);
 
 2187     const int downshift = 31 - M_b;
 
 2188     const int PRESCALE = 6; 
 
 2191     fscale /= (
float)(1 << downshift);
 
 2192     fscale *= (
float)(1 << PRESCALE);
 
 2194     scale = (int)(fscale + 0.5);
 
 2196     for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
 
 2199         for (
i = 0; 
i < 
w; ++
i) {
 
 2202                 val = -(
val & INT32_MAX);
 
 2204             val = (
val + (1 << (PRESCALE - 1))) >> PRESCALE;
 
 2215     for (
i = 1; 
i < 3; 
i++) {
 
 2216         if (
tile->codsty[0].transform != 
tile->codsty[
i].transform) {
 
 2220         if (memcmp(
tile->comp[0].coord, 
tile->comp[
i].coord, 
sizeof(
tile->comp[0].coord))) {
 
 2226     for (
i = 0; 
i < 3; 
i++)
 
 2232     for (
i = 0; 
i < 2; 
i++)
 
 2233         csize *= 
tile->comp[0].coord[
i][1] - 
tile->comp[0].coord[
i][0];
 
 2235     s->dsp.mct_decode[
tile->codsty[0].transform](
src[0], 
src[1], 
src[2], csize);
 
 2243     int compno, reslevelno, bandno;
 
 2246     for (compno = 0; compno < 
s->ncomponents; compno++) {
 
 2260             for (bandno = 0; bandno < rlevel->
nbands; bandno++, subbandno++) {
 
 2261                 int nb_precincts, precno;
 
 2263                 int cblkno = 0, bandpos;
 
 2267                 bandpos = bandno + (reslevelno > 0);
 
 2269                 if (band->
coord[0][0] == band->
coord[0][1] ||
 
 2280                 for (precno = 0; precno < nb_precincts; precno++) {
 
 2295                                                            M_b, 
comp->roi_shift);
 
 2300                                               bandpos, 
comp->roi_shift, M_b);
 
 2328 #define WRITE_FRAME(D, PIXEL)                                                                     \ 
 2329     static inline void write_frame_ ## D(const Jpeg2000DecoderContext * s, Jpeg2000Tile * tile,   \ 
 2330                                          AVFrame * picture, int precision)                        \ 
 2332         const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt);               \ 
 2333         int planar    = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);                              \ 
 2334         int pixelsize = planar ? 1 : pixdesc->nb_components;                                      \ 
 2339         for (compno = 0; compno < s->ncomponents; compno++) {                                     \ 
 2340             Jpeg2000Component *comp     = tile->comp + compno;                                    \ 
 2341             Jpeg2000CodingStyle *codsty = tile->codsty + compno;                                  \ 
 2343             float *datap     = comp->f_data;                                                      \ 
 2344             int32_t *i_datap = comp->i_data;                                                      \ 
 2345             int cbps         = s->cbps[compno];                                                   \ 
 2346             int w            = tile->comp[compno].coord[0][1] -                                   \ 
 2347                                ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]);            \ 
 2348             int h            = tile->comp[compno].coord[1][1] -                                   \ 
 2349                                ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]);            \ 
 2353                 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);                 \ 
 2355             y    = tile->comp[compno].coord[1][0] -                                               \ 
 2356                    ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]);                        \ 
 2357             line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\ 
 2358             for (; y < h; y++) {                                                                  \ 
 2361                 x   = tile->comp[compno].coord[0][0] -                                            \ 
 2362                       ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]);                     \ 
 2363                 dst = line + x * pixelsize + compno*!planar;                                      \ 
 2365                 if (codsty->transform == FF_DWT97) {                                              \ 
 2366                     for (; x < w; x++) {                                                          \ 
 2367                         int val = lrintf(*datap) + (1 << (cbps - 1));                             \ 
 2369                         val  = av_clip(val, 0, (1 << cbps) - 1);                                  \ 
 2370                         *dst = val << (precision - cbps);                                         \ 
 2375                     for (; x < w; x++) {                                                          \ 
 2376                         int val = *i_datap + (1 << (cbps - 1));                                   \ 
 2378                         val  = av_clip(val, 0, (1 << cbps) - 1);                                  \ 
 2379                         *dst = val << (precision - cbps);                                         \ 
 2384                 line += picture->linesize[plane] / sizeof(PIXEL);                                 \ 
 2396                                 int jobnr, 
int threadnr)
 
 2407     if (
tile->codsty[0].mct)
 
 2410     if (
s->precision <= 8) {
 
 2411         write_frame_8(
s, 
tile, picture, 8);
 
 2418         write_frame_16(
s, 
tile, picture, precision);
 
 2427     for (tileno = 0; tileno < 
s->numXtiles * 
s->numYtiles; tileno++) {
 
 2428         if (
s->tile[tileno].comp) {
 
 2429             for (compno = 0; compno < 
s->ncomponents; compno++) {
 
 2436             av_freep(&
s->tile[tileno].packed_headers);
 
 2437             s->tile[tileno].packed_headers_size = 0;
 
 2441     s->packed_headers_size = 0;
 
 2442     memset(&
s->packed_headers_stream, 0, 
sizeof(
s->packed_headers_stream));
 
 2444     memset(
s->codsty, 0, 
sizeof(
s->codsty));
 
 2445     memset(
s->qntsty, 0, 
sizeof(
s->qntsty));
 
 2446     memset(
s->properties, 0, 
sizeof(
s->properties));
 
 2447     memset(&
s->poc  , 0, 
sizeof(
s->poc));
 
 2448     s->numXtiles = 
s->numYtiles = 0;
 
 2457     uint8_t *properties         = 
s->properties;
 
 2458     uint8_t in_tile_headers     = 0;
 
 2470         marker = bytestream2_get_be16u(&
s->g);
 
 2472         if (marker >= 0xFF30 && marker <= 0xFF3F)
 
 2482             if (
s->curtileno < 0) {
 
 2487             tile = 
s->tile + 
s->curtileno;
 
 2488             tp = 
tile->tile_part + 
tile->tp_idx;
 
 2489             if (tp->
tp_end < 
s->g.buffer) {
 
 2495                 uint32_t tp_header_size = bytestream2_get_be32(&
s->packed_headers_stream);
 
 2501             if (
tile->has_ppt && 
tile->tp_idx == 0) {
 
 2513         len = bytestream2_get_be16(&
s->g);
 
 2525             if (
s->ncomponents) {
 
 2531                 s->numXtiles = 
s->numYtiles = 0;
 
 2534             if (!
s->ncomponents) {
 
 2541             if (in_tile_headers == 1 && 
s->isHT && (!
s->Ccap15_b11)) {
 
 2543                     "COC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
 
 2549             if (in_tile_headers == 1 && 
s->isHT && (!
s->Ccap15_b11)) {
 
 2551                     "COD marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
 
 2557             if (in_tile_headers == 1 && 
s->isHT && (!
s->Ccap15_b11)) {
 
 2559                     "RGN marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
 
 2563             if ((!
s->Ccap15_b12) && 
s->isHT) {
 
 2564                 av_log(
s->avctx, 
AV_LOG_ERROR, 
"RGN marker found but the codestream belongs to the RGNFREE set\n");
 
 2569             if (in_tile_headers == 1 && 
s->isHT && (!
s->Ccap15_b11)) {
 
 2571                     "QCC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
 
 2577             if (in_tile_headers == 1 && 
s->isHT && (!
s->Ccap15_b11)) {
 
 2579                     "QCD marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
 
 2585             if (in_tile_headers == 1 && 
s->isHT && (!
s->Ccap15_b11)) {
 
 2587                     "POC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
 
 2593             if (!in_tile_headers) {
 
 2594                 in_tile_headers = 1;
 
 2601                 codsty = 
s->tile[
s->curtileno].codsty;
 
 2602                 qntsty = 
s->tile[
s->curtileno].qntsty;
 
 2603                 poc    = &
s->tile[
s->curtileno].poc;
 
 2604                 properties = 
s->tile[
s->curtileno].properties;
 
 2626             if (in_tile_headers) {
 
 2636                        "Cannot have both PPT and PPM marker.\n");
 
 2639             if ((!
s->Ccap15_b11) && 
s->isHT) {
 
 2640                 av_log(
s->avctx, 
AV_LOG_ERROR, 
"PPT marker found but the codestream belongs to the HOMOGENEOUS set\n");
 
 2651                    "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
 
 2658                    "error during processing marker segment %.4"PRIx16
"\n",
 
 2672     for (tileno = 0; tileno < 
s->numXtiles * 
s->numYtiles; tileno++) {
 
 2687     uint32_t atom_size, atom, atom_end;
 
 2688     int search_range = 10;
 
 2693         atom_size = bytestream2_get_be32u(&
s->g);
 
 2694         atom      = bytestream2_get_be32u(&
s->g);
 
 2695         if (atom_size == 1) {
 
 2696             if (bytestream2_get_be32u(&
s->g)) {
 
 2700             atom_size = bytestream2_get_be32u(&
s->g);
 
 2718             uint32_t atom2_size, atom2, atom2_end;
 
 2722                 atom2_size = bytestream2_get_be32u(&
s->g);
 
 2723                 atom2      = bytestream2_get_be32u(&
s->g);
 
 2725                 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
 
 2730                 } 
else if (atom2 == 
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
 
 2731                     int method = bytestream2_get_byteu(&
s->g);
 
 2734                         s->colour_space = bytestream2_get_be32u(&
s->g);
 
 2736                 } 
else if (atom2 == 
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
 
 2737                     int i, 
size, colour_count, colour_channels, colour_depth[3];
 
 2738                     colour_count = bytestream2_get_be16u(&
s->g);
 
 2739                     colour_channels = bytestream2_get_byteu(&
s->g);
 
 2741                     colour_depth[0] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
 
 2742                     colour_depth[1] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
 
 2743                     colour_depth[2] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
 
 2744                     size = (colour_depth[0] + 7 >> 3) * colour_count +
 
 2745                            (colour_depth[1] + 7 >> 3) * colour_count +
 
 2746                            (colour_depth[2] + 7 >> 3) * colour_count;
 
 2748                         colour_channels != 3 ||
 
 2749                         colour_depth[0] > 16 ||
 
 2750                         colour_depth[1] > 16 ||
 
 2751                         colour_depth[2] > 16 ||
 
 2752                         atom2_size < 
size) {
 
 2758                     for (
i = 0; 
i < colour_count; 
i++) {
 
 2760                         if (colour_depth[0] <= 8) {
 
 2761                             r = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[0];
 
 2762                             r |= 
r >> colour_depth[0];
 
 2764                             r = bytestream2_get_be16u(&
s->g) >> colour_depth[0] - 8;
 
 2766                         if (colour_depth[1] <= 8) {
 
 2767                             g = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[1];
 
 2768                             g |= 
g >> colour_depth[1];
 
 2770                             g = bytestream2_get_be16u(&
s->g) >> colour_depth[1] - 8;
 
 2772                         if (colour_depth[2] <= 8) {
 
 2773                             b = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[2];
 
 2774                             b |= 
b >> colour_depth[2];
 
 2776                             b = bytestream2_get_be16u(&
s->g) >> colour_depth[2] - 8;
 
 2778                         s->palette[
i] = 0xff
u << 24 | 
r << 16 | 
g << 8 | 
b;
 
 2780                 } 
else if (atom2 == 
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
 
 2781                     int n = bytestream2_get_be16u(&
s->g);
 
 2783                         int cn   = bytestream2_get_be16(&
s->g);
 
 2784                         av_unused int typ  = bytestream2_get_be16(&
s->g);
 
 2785                         int asoc = bytestream2_get_be16(&
s->g);
 
 2786                         if (cn < 4 && asoc < 4)
 
 2789                 } 
else if (atom2 == 
MKBETAG(
'r',
'e',
's',
' ') && atom2_size >= 18) {
 
 2790                     int64_t vnum, vden, hnum, hden, vexp, hexp;
 
 2793                     resx = bytestream2_get_be32u(&
s->g);
 
 2794                     if (resx != 
MKBETAG(
'r',
'e',
's',
'c') && resx != 
MKBETAG(
'r',
'e',
's',
'd')) {
 
 2798                     vnum = bytestream2_get_be16u(&
s->g);
 
 2799                     vden = bytestream2_get_be16u(&
s->g);
 
 2800                     hnum = bytestream2_get_be16u(&
s->g);
 
 2801                     hden = bytestream2_get_be16u(&
s->g);
 
 2802                     vexp = bytestream2_get_byteu(&
s->g);
 
 2803                     hexp = bytestream2_get_byteu(&
s->g);
 
 2804                     if (!vnum || !vden || !hnum || !hden) {
 
 2816                     if (   INT64_MAX / (hnum * vden) > pow(10, hexp)
 
 2817                         && INT64_MAX / (vnum * hden) > pow(10, vexp))
 
 2819                                   hnum * vden * pow(10, hexp),
 
 2820                                   vnum * hden * pow(10, vexp),
 
 2824             } 
while (atom_end - atom2_end >= 8);
 
 2841         s->reduction_factor = avctx->
lowres;
 
 2861     memset(
s->cdef, -1, 
sizeof(
s->cdef));
 
 2870        (bytestream2_get_be32u(&
s->g) == 12) &&
 
 2875                    "Could not find Jpeg2000 codestream atom.\n");
 
 2894     if (
s->sar.num && 
s->sar.den)
 
 2896     s->sar.num = 
s->sar.den = 0;
 
 2910     for (
int x = 0; x < 
s->ncomponents && 
s->codsty[x].transform == 
FF_DWT53;)
 
 2911         if (++x == 
s->ncomponents)
 
 2921         memcpy(picture->
data[1], 
s->palette, 256 * 
sizeof(uint32_t));
 
 2930 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x) 
 2931 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM 
 2934     { 
"lowres",  
"Lower the decoding resolution by a power of two",
 
 2947     .
p.
name           = 
"jpeg2000",