typedef struct Jpeg2000TilePart {
uint8_t tile_index; // Tile index who refers the tile-part
const uint8_t *tp_end;
+ GetByteContext header_tpg; // bit stream of header if PPM header is used
GetByteContext tpg; // bit stream in tile-part
} Jpeg2000TilePart;
uint8_t cbps[4]; // bits per sample in particular components
uint8_t sgnd[4]; // if a component is signed
uint8_t properties[4];
+
+ uint8_t has_ppm;
+ uint8_t *packed_headers; // contains packed headers. Used only along with PPM marker
+ int packed_headers_size;
+ GetByteContext packed_headers_stream;
+ uint8_t in_tile_headers;
+
int cdx[4], cdy[4];
int precision;
int ncomponents;
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_size2(s->width, s->height, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx)) {
avpriv_request_sample(s->avctx, "Large Dimensions");
return AVERROR_PATCHWELCOME;
uint8_t *properties)
{
int compno, ret;
- uint8_t has_eph;
+ uint8_t has_eph, has_sop;
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;
+ has_sop = c->csty & JPEG2000_CSTY_SOP;
c->csty = bytestream2_get_byteu(&s->g);
c->csty |= has_eph; //do not override eph present bits from COD
+ c->csty |= has_sop; //do not override sop present bits from COD
if ((ret = get_cox(s, c)) < 0)
return ret;
return 0;
}
+static int get_ppm(Jpeg2000DecoderContext *s, int n)
+{
+ void *new;
+
+ if (n < 3) {
+ av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPM data.\n");
+ return AVERROR_INVALIDDATA;
+ }
+ bytestream2_get_byte(&s->g); //Zppm is skipped and not used
+ new = av_realloc(s->packed_headers,
+ s->packed_headers_size + n - 3);
+ if (new) {
+ s->packed_headers = new;
+ } else
+ return AVERROR(ENOMEM);
+ s->has_ppm = 1;
+ memset(&s->packed_headers_stream, 0, sizeof(s->packed_headers_stream));
+ bytestream_get_buffer(&s->g.buffer, s->packed_headers + s->packed_headers_size,
+ n - 3);
+ s->packed_headers_size += n - 3;
+
+ return 0;
+}
+
static int get_ppt(Jpeg2000DecoderContext *s, int n)
{
Jpeg2000Tile *tile;
return res;
}
-static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+static inline void select_header(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
int *tp_index)
+{
+ s->g = tile->tile_part[*tp_index].header_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;
+ }
+ }
+}
+
+static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+ int *tp_index, Jpeg2000CodingStyle *codsty)
{
s->g = tile->tile_part[*tp_index].tpg;
if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
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);
+ if (codsty->csty & JPEG2000_CSTY_SOP) {
+ if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
+ bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
+ else
+ av_log(s->avctx, AV_LOG_ERROR, "SOP marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
+ }
}
static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
return 0;
rlevel->band[0].prec[precno].decoded_layers = layno + 1;
// Select stream to read from
- if (tile->has_ppt)
+ if (s->has_ppm)
+ select_header(s, tile, tp_index);
+ else if (tile->has_ppt)
s->g = tile->packed_headers_stream;
else
- select_stream(s, tile, tp_index);
+ select_stream(s, tile, tp_index, codsty);
if (!(ret = get_bits(s, 1))) {
jpeg2000_flush(s);
}
// Save state of stream
- if (tile->has_ppt) {
+ if (s->has_ppm) {
+ tile->tile_part[*tp_index].header_tpg = s->g;
+ select_stream(s, tile, tp_index, codsty);
+ } else if (tile->has_ppt) {
tile->packed_headers_stream = s->g;
- select_stream(s, tile, tp_index);
+ select_stream(s, tile, tp_index, codsty);
}
for (bandno = 0; bandno < rlevel->nbands; bandno++) {
Jpeg2000Band *band = rlevel->band + bandno;
return 0;
skip_data:
- if (tile->has_ppt)
+ if (codsty->csty & JPEG2000_CSTY_EPH) {
+ if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
+ bytestream2_skip(&s->g, 2);
+ else
+ av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
+ }
+ if (s->has_ppm) {
+ tile->tile_part[*tp_index].header_tpg = s->g;
+ select_stream(s, tile, tp_index, codsty);
+ } else if (tile->has_ppt) {
tile->packed_headers_stream = s->g;
- else
- tile->tile_part[*tp_index].tpg = s->g;
+ select_stream(s, tile, tp_index, codsty);
+ }
+ tile->tile_part[*tp_index].tpg = s->g;
return 0;
}
uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
unsigned prcx, prcy;
+ int trx0, try0;
- int xc = x / s->cdx[compno];
- int yc = y / s->cdy[compno];
+ if (!s->cdx[compno] || !s->cdy[compno])
+ return AVERROR_INVALIDDATA;
if (reslevelno >= codsty->nreslevels)
continue;
- if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
+ trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], (int64_t)s->cdx[compno] << reducedresno);
+ try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], (int64_t)s->cdy[compno] << reducedresno);
+
+ if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
+ (y == tile->coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_height)))))
continue;
- if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
+ if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
+ (x == tile->coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_width)))))
continue;
// check if a precinct exists
- prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
- prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
+ prcx = ff_jpeg2000_ceildiv(x, (int64_t)s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
+ prcy = ff_jpeg2000_ceildiv(y, (int64_t)s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
Jpeg2000Component *comp = tile->comp + compno;
Jpeg2000CodingStyle *codsty = tile->codsty + compno;
Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
- int xc = x / s->cdx[compno];
- int yc = y / s->cdy[compno];
+
+ if (!s->cdx[compno] || !s->cdy[compno])
+ return AVERROR_INVALIDDATA;
for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
unsigned prcx, prcy;
uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
+ int trx0, try0;
- if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
- continue;
+ trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], (int64_t)s->cdx[compno] << reducedresno);
+ try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], (int64_t)s->cdy[compno] << reducedresno);
- if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
- continue;
+ if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
+ (y == tile->coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_height)))))
+ continue;
+
+ if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
+ (x == tile->coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_width)))))
+ continue;
// check if a precinct exists
- prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
- prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
+ prcx = ff_jpeg2000_ceildiv(x, (int64_t)s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
+ prcy = ff_jpeg2000_ceildiv(y, (int64_t)s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
pass_cnt ++;
}
- if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
+ if (cblk->data + cblk->length - 2 > t1->mqc.bp) {
av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
- cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
+ cblk->data + cblk->length - 2 - t1->mqc.bp);
+ }
+
+ if (cblk->data + cblk->length < t1->mqc.bp) {
+ av_log(s->avctx, AV_LOG_WARNING, "Synthetic End of Stream Marker Read.\n");
}
return 1;
s->tile[tileno].packed_headers_size = 0;
}
}
+ av_freep(&s->packed_headers);
+ s->packed_headers_size = 0;
+ memset(&s->packed_headers_stream, 0, sizeof(s->packed_headers_stream));
av_freep(&s->tile);
memset(s->codsty, 0, sizeof(s->codsty));
memset(s->qntsty, 0, sizeof(s->qntsty));
marker = bytestream2_get_be16u(&s->g);
oldpos = bytestream2_tell(&s->g);
-
+ if (marker >= 0xFF30 && marker <= 0xFF3F)
+ continue;
if (marker == JPEG2000_SOD) {
Jpeg2000Tile *tile;
Jpeg2000TilePart *tp;
return AVERROR_INVALIDDATA;
}
+ if (s->has_ppm) {
+ uint32_t tp_header_size = bytestream2_get_be32(&s->packed_headers_stream);
+ if (bytestream2_get_bytes_left(&s->packed_headers_stream) < tp_header_size)
+ return AVERROR_INVALIDDATA;
+ bytestream2_init(&tp->header_tpg, s->packed_headers_stream.buffer, tp_header_size);
+ bytestream2_skip(&s->packed_headers_stream, tp_header_size);
+ }
if (tile->has_ppt && tile->tp_idx == 0) {
bytestream2_init(&tile->packed_headers_stream, tile->packed_headers, tile->packed_headers_size);
}
ret = get_poc(s, len, poc);
break;
case JPEG2000_SOT:
+ if (!s->in_tile_headers) {
+ s->in_tile_headers = 1;
+ if (s->has_ppm) {
+ bytestream2_init(&s->packed_headers_stream, s->packed_headers, s->packed_headers_size);
+ }
+ }
if (!(ret = get_sot(s, len))) {
av_assert1(s->curtileno >= 0);
codsty = s->tile[s->curtileno].codsty;
// Packet length, tile-part header
ret = get_plt(s, len);
break;
+ case JPEG2000_PPM:
+ // Packed headers, main header
+ if (s->in_tile_headers) {
+ av_log(s->avctx, AV_LOG_ERROR, "PPM Marker can only be in Main header\n");
+ return AVERROR_INVALIDDATA;
+ }
+ ret = get_ppm(s, len);
+ break;
case JPEG2000_PPT:
// Packed headers, tile-part header
+ if (s->has_ppm) {
+ av_log(s->avctx, AV_LOG_ERROR,
+ "Cannot have both PPT and PPM marker.\n");
+ return AVERROR_INVALIDDATA;
+ }
+
ret = get_ppt(s, len);
break;
default:
return 0;
}
atom_size = bytestream2_get_be32u(&s->g);
+ if (atom_size < 16 || (int64_t)bytestream2_tell(&s->g) + atom_size - 16 > INT_MAX)
+ return AVERROR_INVALIDDATA;
atom_end = bytestream2_tell(&s->g) + atom_size - 16;
} else {
+ if (atom_size < 8 || (int64_t)bytestream2_tell(&s->g) + atom_size - 8 > INT_MAX)
+ return AVERROR_INVALIDDATA;
atom_end = bytestream2_tell(&s->g) + atom_size - 8;
}
.version = LIBAVUTIL_VERSION_INT,
};
-AVCodec ff_jpeg2000_decoder = {
+const AVCodec ff_jpeg2000_decoder = {
.name = "jpeg2000",
.long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
.type = AVMEDIA_TYPE_VIDEO,