Jpeg2000QuantStyle qntsty[4];
Jpeg2000POC poc;
Jpeg2000TilePart tile_part[32];
+ uint8_t has_ppt; // whether this tile has a ppt marker
+ uint8_t *packed_headers; // contains packed headers. Used only along with PPT marker
+ int packed_headers_size; // size in bytes of the packed headers
+ GetByteContext packed_headers_stream; // byte context corresponding to packed headers
uint16_t tp_idx; // Tile-part index
int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
} Jpeg2000Tile;
Jpeg2000CodingStyle codsty[4];
Jpeg2000QuantStyle qntsty[4];
Jpeg2000POC poc;
+ uint8_t roi_shift[4];
int bit_index;
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 ( s->avctx->pix_fmt != AV_PIX_FMT_NONE
+ && !pix_fmt_match(s->avctx->pix_fmt, ncomponents, s->precision, log2_chroma_wh, s->pal8))
+ s->avctx->pix_fmt = AV_PIX_FMT_NONE;
+ if (s->avctx->pix_fmt == AV_PIX_FMT_NONE)
+ 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 &&
uint8_t *properties)
{
int compno, ret;
+ uint8_t has_eph;
if (bytestream2_get_bytes_left(&s->g) < 2) {
av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
}
c += compno;
+ has_eph = c->csty & JPEG2000_CSTY_EPH;
c->csty = bytestream2_get_byteu(&s->g);
+ c->csty |= has_eph; //do not override eph present bits from COD
if ((ret = get_cox(s, c)) < 0)
return ret;
return 0;
}
+static int get_rgn(Jpeg2000DecoderContext *s, int n)
+{
+ uint16_t compno;
+ compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g):
+ bytestream2_get_be16u(&s->g);
+ if (bytestream2_get_byte(&s->g)) {
+ av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
+ return AVERROR_INVALIDDATA; // SRgn field value is 0
+ }
+ // SPrgn field
+ // Currently compno cannot be greater than 4.
+ // However, future implementation should support compno up to 65536
+ if (compno < s->ncomponents) {
+ if (s->curtileno == -1)
+ s->roi_shift[compno] = bytestream2_get_byte(&s->g);
+ else {
+ if (s->tile[s->curtileno].tp_idx != 0)
+ return AVERROR_INVALIDDATA; // marker occurs only in first tile part of tile
+ s->tile[s->curtileno].comp[compno].roi_shift = bytestream2_get_byte(&s->g);
+ }
+ return 0;
+ }
+ return AVERROR_INVALIDDATA;
+}
+
/* Get common part for QCD and QCC segments. */
static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
{
return 0;
}
+static int read_crg(Jpeg2000DecoderContext *s, int n)
+{
+ if (s->ncomponents*4 != n - 2) {
+ av_log(s->avctx, AV_LOG_ERROR, "Invalid CRG marker.\n");
+ return AVERROR_INVALIDDATA;
+ }
+ bytestream2_skip(&s->g, n - 2);
+ return 0;
+}
/* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
* Used to know the number of tile parts and lengths.
* There may be multiple TLMs in the header.
* markers. Parsing the TLM header is needed to increment the input header
* buffer.
* This marker is mandatory for DCI. */
-static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
+static int get_tlm(Jpeg2000DecoderContext *s, int n)
{
uint8_t Stlm, ST, SP, tile_tlm, i;
bytestream2_get_byte(&s->g); /* Ztlm: skipped */
// too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
ST = (Stlm >> 4) & 0x03;
- // TODO: Manage case of ST = 0b11 --> raise error
+ if (ST == 0x03) {
+ av_log(s->avctx, AV_LOG_ERROR, "TLM marker contains invalid ST value.\n");
+ return AVERROR_INVALIDDATA;
+ }
+
SP = (Stlm >> 6) & 0x01;
tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
for (i = 0; i < tile_tlm; i++) {
return 0;
}
-static uint8_t get_plt(Jpeg2000DecoderContext *s, int n)
+static int get_plt(Jpeg2000DecoderContext *s, int n)
{
int i;
+ int v;
av_log(s->avctx, AV_LOG_DEBUG,
"PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
+ if (n < 4)
+ return AVERROR_INVALIDDATA;
+
/*Zplt =*/ bytestream2_get_byte(&s->g);
for (i = 0; i < n - 3; i++) {
- bytestream2_get_byte(&s->g);
+ v = bytestream2_get_byte(&s->g);
+ }
+ if (v & 0x80)
+ return AVERROR_INVALIDDATA;
+
+ return 0;
+}
+
+static int get_ppt(Jpeg2000DecoderContext *s, int n)
+{
+ Jpeg2000Tile *tile;
+ void *new;
+
+ if (n < 3) {
+ av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPT data.\n");
+ return AVERROR_INVALIDDATA;
}
+ if (s->curtileno < 0)
+ return AVERROR_INVALIDDATA;
+
+ tile = &s->tile[s->curtileno];
+ if (tile->tp_idx != 0) {
+ av_log(s->avctx, AV_LOG_ERROR,
+ "PPT marker can occur only on first tile part of a tile.\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ tile->has_ppt = 1; // this tile has a ppt marker
+ bytestream2_get_byte(&s->g); // Zppt is skipped and not used
+ new = av_realloc(tile->packed_headers,
+ tile->packed_headers_size + n - 3);
+ if (new) {
+ tile->packed_headers = new;
+ } else
+ return AVERROR(ENOMEM);
+ memcpy(tile->packed_headers + tile->packed_headers_size,
+ s->g.buffer, n - 3);
+ tile->packed_headers_size += n - 3;
+ bytestream2_skip(&s->g, n - 3);
return 0;
}
comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
+ if (!comp->roi_shift)
+ comp->roi_shift = s->roi_shift[compno];
+
if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
s->cbps[compno], s->cdx[compno],
s->cdy[compno], s->avctx))
return res;
}
+static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+ int *tp_index)
+{
+ s->g = tile->tile_part[*tp_index].tpg;
+ if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
+ if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
+ s->g = tile->tile_part[++(*tp_index)].tpg;
+ }
+ }
+ if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
+ bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
+}
+
static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
Jpeg2000CodingStyle *codsty,
Jpeg2000ResLevel *rlevel, int precno,
if (layno < rlevel->band[0].prec[precno].decoded_layers)
return 0;
rlevel->band[0].prec[precno].decoded_layers = layno + 1;
-
- if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
- if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
- s->g = tile->tile_part[++(*tp_index)].tpg;
- }
- }
-
- if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
- bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
+ // Select stream to read from
+ if (tile->has_ppt)
+ s->g = tile->packed_headers_stream;
+ else
+ select_stream(s, tile, tp_index);
if (!(ret = get_bits(s, 1))) {
jpeg2000_flush(s);
- return 0;
+ goto skip_data;
} else if (ret < 0)
return ret;
av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
}
+ // Save state of stream
+ if (tile->has_ppt) {
+ tile->packed_headers_stream = s->g;
+ select_stream(s, tile, tp_index);
+ }
for (bandno = 0; bandno < rlevel->nbands; bandno++) {
Jpeg2000Band *band = rlevel->band + bandno;
Jpeg2000Prec *prec = band->prec + precno;
av_freep(&cblk->lengthinc);
}
}
+ // Save state of stream
+ tile->tile_part[*tp_index].tpg = s->g;
+ return 0;
+
+skip_data:
+ if (tile->has_ppt)
+ tile->packed_headers_stream = s->g;
+ else
+ tile->tile_part[*tp_index].tpg = s->g;
return 0;
}
step_x = 32;
step_y = 32;
- if (RSpoc > FFMIN(codsty->nreslevels, REpoc))
+ if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
continue;
for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
}
- av_assert0(step_x < 32 && step_y < 32);
+ if (step_x >= 31 || step_y >= 31){
+ avpriv_request_sample(s->avctx, "CPRL with large step");
+ return AVERROR_PATCHWELCOME;
+ }
step_x = 1<<step_x;
step_y = 1<<step_y;
continue;
}
- for (layno = 0; layno < LYEpoc; layno++) {
- if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
- codsty, rlevel,
- precno, layno,
- qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
- qntsty->nguardbits)) < 0)
- return ret;
- }
+ for (layno = 0; layno < LYEpoc; layno++) {
+ if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
+ codsty, rlevel,
+ precno, layno,
+ qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
+ qntsty->nguardbits)) < 0)
+ return ret;
+ }
}
}
}
static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
- int width, int height, int bandpos)
+ int width, int height, int bandpos, uint8_t roi_shift)
{
- int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1;
+ int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift;
int pass_cnt = 0;
int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
int term_cnt = 0;
cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
}
- return 0;
+ return 1;
+}
+
+static inline int roi_shift_param(Jpeg2000Component *comp,
+ int quan_parameter)
+{
+ uint8_t roi_shift;
+ int val;
+ roi_shift = comp->roi_shift;
+ val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
+
+ if (val > (1 << roi_shift))
+ return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
+ return quan_parameter;
}
/* TODO: Verify dequantization for lossless case
s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
}
+static inline void roi_scale_cblk(Jpeg2000Cblk *cblk,
+ Jpeg2000Component *comp,
+ Jpeg2000T1Context *t1)
+{
+ int i, j;
+ int w = cblk->coord[0][1] - cblk->coord[0][0];
+ for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
+ int *src = t1->data + j*t1->stride;
+ for (i = 0; i < w; ++i)
+ src[i] = roi_shift_param(comp, src[i]);
+ }
+}
+
static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
{
Jpeg2000T1Context t1;
for (compno = 0; compno < s->ncomponents; compno++) {
Jpeg2000Component *comp = tile->comp + compno;
Jpeg2000CodingStyle *codsty = tile->codsty + compno;
+ int coded = 0;
t1.stride = (1<<codsty->log2_cblk_width) + 2;
cblkno++) {
int x, y;
Jpeg2000Cblk *cblk = prec->cblk + cblkno;
- decode_cblk(s, codsty, &t1, cblk,
+ int ret = decode_cblk(s, codsty, &t1, cblk,
cblk->coord[0][1] - cblk->coord[0][0],
cblk->coord[1][1] - cblk->coord[1][0],
- bandpos);
-
+ bandpos, comp->roi_shift);
+ if (ret)
+ coded = 1;
+ else
+ continue;
x = cblk->coord[0][0] - band->coord[0][0];
y = cblk->coord[1][0] - band->coord[1][0];
+ if (comp->roi_shift)
+ roi_scale_cblk(cblk, comp, &t1);
if (codsty->transform == FF_DWT97)
dequantization_float(x, y, cblk, comp, &t1, band);
else if (codsty->transform == FF_DWT97_INT)
} /* end reslevel */
/* inverse DWT */
- ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
+ if (coded)
+ ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
+
} /*end comp */
}
ff_jpeg2000_cleanup(comp, codsty);
}
av_freep(&s->tile[tileno].comp);
+ av_freep(&s->tile[tileno].packed_headers);
+ s->tile[tileno].packed_headers_size = 0;
}
}
av_freep(&s->tile);
av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
return AVERROR_INVALIDDATA;
}
+
+ if (tile->has_ppt && tile->tp_idx == 0) {
+ bytestream2_init(&tile->packed_headers_stream, tile->packed_headers, tile->packed_headers_size);
+ }
+
bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
len = bytestream2_get_be16(&s->g);
if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
- av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
- return AVERROR_INVALIDDATA;
+ if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
+ av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
+ return AVERROR_INVALIDDATA;
+ }
+ av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
+ break;
}
switch (marker) {
case JPEG2000_COD:
ret = get_cod(s, codsty, properties);
break;
+ case JPEG2000_RGN:
+ ret = get_rgn(s, len);
+ break;
case JPEG2000_QCC:
ret = get_qcc(s, len, qntsty, properties);
break;
// the comment is ignored
bytestream2_skip(&s->g, len - 2);
break;
+ case JPEG2000_CRG:
+ ret = read_crg(s, len);
+ break;
case JPEG2000_TLM:
// Tile-part lengths
ret = get_tlm(s, len);
// Packet length, tile-part header
ret = get_plt(s, len);
break;
+ case JPEG2000_PPT:
+ // Packed headers, tile-part header
+ ret = get_ppt(s, len);
+ break;
default:
av_log(s->avctx, AV_LOG_ERROR,
"unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
if ((ret = init_tile(s, tileno)) < 0)
return ret;
- s->g = tile->tile_part[0].tpg;
if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
return ret;
}