45 #define JP2_SIG_TYPE 0x6A502020
46 #define JP2_SIG_VALUE 0x0D0A870A
47 #define JP2_CODESTREAM 0x6A703263
48 #define JP2_HEADER 0x6A703268
150 if (
s->bit_index == 0) {
151 s->bit_index = 7 + (bytestream2_get_byte(&
s->g) != 0xFF
u);
154 res |= (bytestream2_peek_byte(&
s->g) >>
s->bit_index) & 1;
161 if (bytestream2_get_byte(&
s->g) == 0xff)
171 int sp = -1, curval = 0;
178 while (node && !node->
vis) {
186 curval = stack[
sp]->
val;
188 while (curval < threshold && sp >= 0) {
189 if (curval < stack[
sp]->
val)
190 curval = stack[
sp]->
val;
191 while (curval < threshold) {
201 stack[
sp]->
val = curval;
208 int bpc, uint32_t log2_chroma_wh,
int pal8)
215 if (
desc->nb_components != components) {
219 switch (components) {
221 match = match &&
desc->comp[3].depth >= bpc &&
222 (log2_chroma_wh >> 14 & 3) == 0 &&
223 (log2_chroma_wh >> 12 & 3) == 0;
225 match = match &&
desc->comp[2].depth >= bpc &&
226 (log2_chroma_wh >> 10 & 3) ==
desc->log2_chroma_w &&
227 (log2_chroma_wh >> 8 & 3) ==
desc->log2_chroma_h;
229 match = match &&
desc->comp[1].depth >= bpc &&
230 (log2_chroma_wh >> 6 & 3) ==
desc->log2_chroma_w &&
231 (log2_chroma_wh >> 4 & 3) ==
desc->log2_chroma_h;
234 match = match &&
desc->comp[0].depth >= bpc &&
235 (log2_chroma_wh >> 2 & 3) == 0 &&
236 (log2_chroma_wh & 3) == 0 &&
244 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
245 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
246 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
247 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
248 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
249 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
250 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
251 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
252 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
253 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
254 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
255 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
256 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
257 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
275 uint32_t log2_chroma_wh = 0;
277 int possible_fmts_nb = 0;
287 s->avctx->profile = bytestream2_get_be16u(&
s->g);
288 s->width = bytestream2_get_be32u(&
s->g);
289 s->height = bytestream2_get_be32u(&
s->g);
290 s->image_offset_x = bytestream2_get_be32u(&
s->g);
291 s->image_offset_y = bytestream2_get_be32u(&
s->g);
292 s->tile_width = bytestream2_get_be32u(&
s->g);
293 s->tile_height = bytestream2_get_be32u(&
s->g);
294 s->tile_offset_x = bytestream2_get_be32u(&
s->g);
295 s->tile_offset_y = bytestream2_get_be32u(&
s->g);
296 ncomponents = bytestream2_get_be16u(&
s->g);
303 if (ncomponents <= 0) {
309 if (ncomponents > 4) {
315 if (
s->tile_offset_x < 0 ||
s->tile_offset_y < 0 ||
316 s->image_offset_x <
s->tile_offset_x ||
317 s->image_offset_y <
s->tile_offset_y ||
318 s->tile_width + (int64_t)
s->tile_offset_x <=
s->image_offset_x ||
319 s->tile_height + (int64_t)
s->tile_offset_y <=
s->image_offset_y
325 s->ncomponents = ncomponents;
327 if (
s->tile_width <= 0 ||
s->tile_height <= 0) {
329 s->tile_width,
s->tile_height);
334 av_log(
s->avctx,
AV_LOG_ERROR,
"Insufficient space for %d components in SIZ\n",
s->ncomponents);
338 for (
i = 0;
i <
s->ncomponents;
i++) {
339 uint8_t x = bytestream2_get_byteu(&
s->g);
340 s->cbps[
i] = (x & 0x7f) + 1;
341 s->precision =
FFMAX(
s->cbps[
i],
s->precision);
342 s->sgnd[
i] = !!(x & 0x80);
343 s->cdx[
i] = bytestream2_get_byteu(&
s->g);
344 s->cdy[
i] = bytestream2_get_byteu(&
s->g);
345 if ( !
s->cdx[
i] ||
s->cdx[
i] == 3 ||
s->cdx[
i] > 4
346 || !
s->cdy[
i] ||
s->cdy[
i] == 3 ||
s->cdy[
i] > 4) {
350 log2_chroma_wh |=
s->cdy[
i] >> 1 <<
i * 4 |
s->cdx[
i] >> 1 <<
i * 4 + 2;
357 if (
s->numXtiles * (uint64_t)
s->numYtiles > INT_MAX/
sizeof(*
s->tile) ||
360 s->numXtiles =
s->numYtiles = 0;
364 s->tile =
av_calloc(
s->numXtiles *
s->numYtiles,
sizeof(*
s->tile));
366 s->numXtiles =
s->numYtiles = 0;
370 for (
i = 0;
i <
s->numXtiles *
s->numYtiles;
i++) {
380 s->reduction_factor);
382 s->reduction_factor);
385 for (
i = 1;
i <
s->ncomponents;
i++) {
399 switch (
s->colour_space) {
419 && !
pix_fmt_match(
s->avctx->pix_fmt, ncomponents,
s->precision, log2_chroma_wh,
s->pal8))
422 for (
i = 0;
i < possible_fmts_nb; ++
i) {
423 if (
pix_fmt_match(possible_fmts[
i], ncomponents,
s->precision, log2_chroma_wh,
s->pal8)) {
424 s->avctx->pix_fmt = possible_fmts[
i];
429 if (
i == possible_fmts_nb) {
430 if (ncomponents == 4 &&
431 s->cdy[0] == 1 &&
s->cdx[0] == 1 &&
432 s->cdy[1] == 1 &&
s->cdx[1] == 1 &&
433 s->cdy[2] ==
s->cdy[3] &&
s->cdx[2] ==
s->cdx[3]) {
434 if (
s->precision == 8 &&
s->cdy[2] == 2 &&
s->cdx[2] == 2 && !
s->pal8) {
442 }
else if (ncomponents == 3 &&
s->precision == 8 &&
443 s->cdx[0] ==
s->cdx[1] &&
s->cdx[0] ==
s->cdx[2] &&
444 s->cdy[0] ==
s->cdy[1] &&
s->cdy[0] ==
s->cdy[2]) {
447 }
else if (ncomponents == 2 &&
s->precision == 8 &&
448 s->cdx[0] ==
s->cdx[1] &&
s->cdy[0] ==
s->cdy[1]) {
451 }
else if (ncomponents == 1 &&
s->precision == 8) {
458 if (
i == possible_fmts_nb) {
460 "Unknown pix_fmt, profile: %d, colour_space: %d, "
461 "components: %d, precision: %d\n"
462 "cdx[0]: %d, cdy[0]: %d\n"
463 "cdx[1]: %d, cdy[1]: %d\n"
464 "cdx[2]: %d, cdy[2]: %d\n"
465 "cdx[3]: %d, cdy[3]: %d\n",
466 s->avctx->profile,
s->colour_space, ncomponents,
s->precision,
469 ncomponents > 1 ?
s->cdx[1] : 0,
470 ncomponents > 1 ?
s->cdy[1] : 0,
471 ncomponents > 2 ?
s->cdx[2] : 0,
472 ncomponents > 2 ?
s->cdy[2] : 0,
473 ncomponents > 3 ?
s->cdx[3] : 0,
474 ncomponents > 3 ?
s->cdy[3] : 0);
477 s->avctx->bits_per_raw_sample =
s->precision;
493 c->nreslevels = bytestream2_get_byteu(&
s->g) + 1;
499 if (
c->nreslevels <=
s->reduction_factor) {
504 av_log(
s->avctx,
AV_LOG_ERROR,
"reduction_factor too large for this bitstream, max is %d\n",
c->nreslevels - 1);
505 s->reduction_factor =
c->nreslevels - 1;
510 c->nreslevels2decode =
c->nreslevels -
s->reduction_factor;
512 c->log2_cblk_width = (bytestream2_get_byteu(&
s->g) & 15) + 2;
513 c->log2_cblk_height = (bytestream2_get_byteu(&
s->g) & 15) + 2;
515 if (
c->log2_cblk_width > 10 ||
c->log2_cblk_height > 10 ||
516 c->log2_cblk_width +
c->log2_cblk_height > 12) {
521 c->cblk_style = bytestream2_get_byteu(&
s->g);
522 if (
c->cblk_style != 0) {
527 c->transform = bytestream2_get_byteu(&
s->g);
537 for (
i = 0;
i <
c->nreslevels;
i++) {
538 byte = bytestream2_get_byte(&
s->g);
539 c->log2_prec_widths[
i] =
byte & 0x0F;
540 c->log2_prec_heights[
i] = (
byte >> 4) & 0x0F;
542 if (
c->log2_prec_widths[
i] == 0 ||
c->log2_prec_heights[
i] == 0) {
544 c->log2_prec_widths[
i],
c->log2_prec_heights[
i]);
545 c->log2_prec_widths[
i] =
c->log2_prec_heights[
i] = 1;
550 memset(
c->log2_prec_widths , 15,
sizeof(
c->log2_prec_widths ));
551 memset(
c->log2_prec_heights, 15,
sizeof(
c->log2_prec_heights));
568 tmp.csty = bytestream2_get_byteu(&
s->g);
571 tmp.prog_order = bytestream2_get_byteu(&
s->g);
573 tmp.nlayers = bytestream2_get_be16u(&
s->g);
574 tmp.mct = bytestream2_get_byteu(&
s->g);
576 if (
tmp.mct &&
s->ncomponents < 3) {
578 "MCT %"PRIu8
" with too few components (%d)\n",
579 tmp.mct,
s->ncomponents);
586 for (compno = 0; compno <
s->ncomponents; compno++)
587 if (!(properties[compno] &
HAD_COC))
588 memcpy(
c + compno, &
tmp,
sizeof(
tmp));
598 uint8_t has_eph, has_sop;
605 compno = bytestream2_get_byteu(&
s->g);
607 if (compno >=
s->ncomponents) {
609 "Invalid compno %d. There are %d components in the image.\n",
610 compno,
s->ncomponents);
617 c->csty = bytestream2_get_byteu(&
s->g);
632 compno = (
s->ncomponents < 257)? bytestream2_get_byte(&
s->g):
633 bytestream2_get_be16u(&
s->g);
634 if (bytestream2_get_byte(&
s->g)) {
641 if (compno < s->ncomponents) {
643 if (
s->curtileno == -1) {
644 v = bytestream2_get_byte(&
s->g);
647 s->roi_shift[compno] = v;
649 if (
s->tile[
s->curtileno].tp_idx != 0)
651 v = bytestream2_get_byte(&
s->g);
654 s->tile[
s->curtileno].comp[compno].roi_shift = v;
669 x = bytestream2_get_byteu(&
s->g);
679 for (
i = 0;
i < n;
i++)
680 q->
expn[
i] = bytestream2_get_byteu(&
s->g) >> 3;
684 x = bytestream2_get_be16u(&
s->g);
685 q->
expn[0] = x >> 11;
686 q->
mant[0] = x & 0x7ff;
688 int curexpn =
FFMAX(0, q->
expn[0] - (
i - 1) / 3);
689 q->
expn[
i] = curexpn;
697 for (
i = 0;
i < n;
i++) {
698 x = bytestream2_get_be16u(&
s->g);
699 q->
expn[
i] = x >> 11;
700 q->
mant[
i] = x & 0x7ff;
713 memset(&
tmp, 0,
sizeof(
tmp));
717 for (compno = 0; compno <
s->ncomponents; compno++)
718 if (!(properties[compno] &
HAD_QCC))
719 memcpy(q + compno, &
tmp,
sizeof(
tmp));
733 compno = bytestream2_get_byteu(&
s->g);
735 if (compno >=
s->ncomponents) {
737 "Invalid compno %d. There are %d components in the image.\n",
738 compno,
s->ncomponents);
743 return get_qcx(
s, n - 1, q + compno);
749 int elem_size =
s->ncomponents <= 257 ? 7 : 9;
762 tmp.nb_poc = (
size - 2) / elem_size;
768 for (
i = 0;
i<
tmp.nb_poc;
i++) {
770 e->
RSpoc = bytestream2_get_byteu(&
s->g);
771 e->
CSpoc = bytestream2_get_byteu(&
s->g);
772 e->
LYEpoc = bytestream2_get_be16u(&
s->g);
773 e->
REpoc = bytestream2_get_byteu(&
s->g);
774 e->
CEpoc = bytestream2_get_byteu(&
s->g);
775 e->
Ppoc = bytestream2_get_byteu(&
s->g);
778 if (e->
CEpoc >
s->ncomponents)
779 e->
CEpoc =
s->ncomponents;
819 Isot = bytestream2_get_be16u(&
s->g);
820 if (Isot >=
s->numXtiles *
s->numYtiles)
824 Psot = bytestream2_get_be32u(&
s->g);
825 TPsot = bytestream2_get_byteu(&
s->g);
828 bytestream2_get_byteu(&
s->g);
843 s->tile[Isot].tp_idx = TPsot;
844 tp =
s->tile[Isot].tile_part + TPsot;
846 tp->
tp_end =
s->g.buffer + Psot - n - 2;
854 memcpy(&tile->
poc , &
s->poc ,
sizeof(tile->
poc));
863 if (
s->ncomponents*4 != n - 2) {
880 uint8_t Stlm, ST,
SP, tile_tlm,
i;
881 bytestream2_get_byte(&
s->g);
882 Stlm = bytestream2_get_byte(&
s->g);
885 ST = (Stlm >> 4) & 0x03;
891 SP = (Stlm >> 6) & 0x01;
892 tile_tlm = (n - 4) / ((
SP + 1) * 2 + ST);
893 for (
i = 0;
i < tile_tlm;
i++) {
898 bytestream2_get_byte(&
s->g);
901 bytestream2_get_be16(&
s->g);
904 bytestream2_get_be32(&
s->g);
908 bytestream2_get_be16(&
s->g);
910 bytestream2_get_be32(&
s->g);
927 bytestream2_get_byte(&
s->g);
929 for (
i = 0;
i < n - 3;
i++) {
930 v = bytestream2_get_byte(&
s->g);
946 bytestream2_get_byte(&
s->g);
948 s->packed_headers_size + n - 3);
950 s->packed_headers =
new;
954 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
957 s->packed_headers_size += n - 3;
971 if (
s->curtileno < 0)
974 tile = &
s->tile[
s->curtileno];
977 "PPT marker can occur only on first tile part of a tile.\n");
982 bytestream2_get_byte(&
s->g);
1001 int tilex = tileno %
s->numXtiles;
1002 int tiley = tileno /
s->numXtiles;
1008 tile->
coord[0][0] =
av_clip(tilex * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
1009 tile->
coord[0][1] =
av_clip((tilex + 1) * (int64_t)
s->tile_width +
s->tile_offset_x,
s->image_offset_x,
s->width);
1010 tile->
coord[1][0] =
av_clip(tiley * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
1011 tile->
coord[1][1] =
av_clip((tiley + 1) * (int64_t)
s->tile_height +
s->tile_offset_y,
s->image_offset_y,
s->height);
1013 for (compno = 0; compno <
s->ncomponents; compno++) {
1019 comp->coord_o[0][0] = tile->
coord[0][0];
1020 comp->coord_o[0][1] = tile->
coord[0][1];
1021 comp->coord_o[1][0] = tile->
coord[1][0];
1022 comp->coord_o[1][1] = tile->
coord[1][1];
1034 if (!
comp->roi_shift)
1035 comp->roi_shift =
s->roi_shift[compno];
1039 s->cbps[compno],
s->cdx[compno],
1040 s->cdy[compno],
s->avctx))
1055 return num < 0 ? num : 3 + num;
1057 return num < 0 ? num : 6 + num;
1059 return num < 0 ? num : 37 + num;
1097 av_log(
s->avctx,
AV_LOG_ERROR,
"SOP marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1104 int layno, uint8_t *expn,
int numgbits)
1106 int bandno, cblkno,
ret, nb_code_blocks;
1109 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1126 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1130 if (band->
coord[0][0] == band->
coord[0][1] ||
1135 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1137 int incl, newpasses, llen;
1150 int v = expn[bandno] + numgbits - 1 -
1152 if (v < 0 || v > 30) {
1154 "nonzerobits %d invalid or unsupported\n", v);
1170 "Block with length beyond 16 bits");
1189 while (newpasses1 < newpasses) {
1215 newpasses -= newpasses1;
1225 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1236 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1241 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1245 for (cwsno = 0; cwsno < cblk->
nb_lengthinc; cwsno ++) {
1258 "Block length %"PRIu16
" or lengthinc %d is too large, left %d\n",
1286 av_log(
s->avctx,
AV_LOG_ERROR,
"EPH marker not found. instead %X\n", bytestream2_peek_be32(&
s->g));
1300 int RSpoc,
int CSpoc,
1301 int LYEpoc,
int REpoc,
int CEpoc,
1302 int Ppoc,
int *tp_index)
1305 int layno, reslevelno, compno, precno, ok_reslevel;
1313 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1315 for (layno = 0; layno < LYEpoc; layno++) {
1316 for (compno = CSpoc; compno < CEpoc; compno++) {
1319 if (reslevelno < codsty->nreslevels) {
1327 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1338 for (layno = 0; layno < LYEpoc; layno++) {
1340 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1342 for (compno = CSpoc; compno < CEpoc; compno++) {
1345 if (reslevelno < codsty->nreslevels) {
1353 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1364 for (compno = CSpoc; compno < CEpoc; compno++) {
1374 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1375 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1380 if (step_x >= 31 || step_y >= 31){
1387 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1388 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1389 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1390 unsigned prcx, prcy;
1391 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1393 int xc = x /
s->cdx[compno];
1394 int yc = y /
s->cdy[compno];
1416 for (layno = 0; layno < LYEpoc; layno++) {
1419 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1432 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1436 for (compno = CSpoc; compno < CEpoc; compno++) {
1440 if (reslevelno < codsty->nreslevels) {
1441 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1450 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1451 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1452 for (compno = CSpoc; compno < CEpoc; compno++) {
1456 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1458 unsigned prcx, prcy;
1461 if (!
s->cdx[compno] || !
s->cdy[compno])
1470 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1471 (y == tile->
coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_height)))))
1474 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1475 (x == tile->
coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_width)))))
1493 for (layno = 0; layno < LYEpoc; layno++) {
1497 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1511 for (compno = CSpoc; compno < CEpoc; compno++) {
1515 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1516 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1522 if (step_x >= 31 || step_y >= 31){
1529 for (y = tile->
coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1530 for (x = tile->
coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1531 for (compno = CSpoc; compno < CEpoc; compno++) {
1536 if (!
s->cdx[compno] || !
s->cdy[compno])
1539 for (reslevelno = RSpoc; reslevelno <
FFMIN(codsty->
nreslevels, REpoc); reslevelno++) {
1540 unsigned prcx, prcy;
1541 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1548 if (!(y % ((uint64_t)
s->cdy[compno] << (rlevel->
log2_prec_height + reducedresno)) == 0 ||
1549 (y == tile->
coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_height)))))
1552 if (!(x % ((uint64_t)
s->cdx[compno] << (rlevel->
log2_prec_width + reducedresno)) == 0 ||
1553 (x == tile->
coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->
log2_prec_width)))))
1570 for (layno = 0; layno < LYEpoc; layno++) {
1573 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1628 int bpno,
int bandno,
1629 int vert_causal_ctx_csty_symbol)
1631 int mask = 3 << (bpno - 1), y0, x, y;
1633 for (y0 = 0; y0 <
height; y0 += 4)
1634 for (x = 0; x <
width; x++)
1635 for (y = y0; y <
height && y < y0 + 4; y++) {
1636 int flags_mask = -1;
1637 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1650 t1->data[(y) *
t1->stride + x] < 0);
1658 int bpno,
int vert_causal_ctx_csty_symbol)
1663 phalf = 1 << (bpno - 1);
1666 for (y0 = 0; y0 <
height; y0 += 4)
1667 for (x = 0; x <
width; x++)
1668 for (y = y0; y <
height && y < y0 + 4; y++)
1670 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1674 t1->mqc.cx_states + ctxno)
1676 t1->data[(y) *
t1->stride + x] +=
t1->data[(y) *
t1->stride + x] < 0 ? -
r :
r;
1683 int seg_symbols,
int vert_causal_ctx_csty_symbol)
1685 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1687 for (y0 = 0; y0 <
height; y0 += 4) {
1688 for (x = 0; x <
width; x++) {
1689 int flags_mask = -1;
1690 if (vert_causal_ctx_csty_symbol)
1710 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++) {
1711 int flags_mask = -1;
1712 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1725 t1->mqc.cx_states + ctxno) ^
1743 "Segmentation symbol value incorrect\n");
1749 int width,
int height,
int bandpos, uint8_t roi_shift)
1760 memset(
t1->data, 0,
t1->stride *
height *
sizeof(*
t1->data));
1766 memset(
t1->flags, 0,
t1->stride * (
height + 2) *
sizeof(*
t1->flags));
1773 if (bpno < 0 || bpno > 29) {
1780 vert_causal_ctx_csty_symbol);
1789 vert_causal_ctx_csty_symbol);
1834 roi_shift =
comp->roi_shift;
1835 val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1837 if (
val > (1 << roi_shift))
1838 return (quan_parameter < 0)?-(
val >> roi_shift):(
val >> roi_shift);
1839 return quan_parameter;
1855 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1856 float *datap = &
comp->f_data[(
comp->coord[0][1] -
comp->coord[0][0]) * (y + j) + x];
1857 int *
src =
t1->data + j*
t1->stride;
1858 for (
i = 0;
i <
w; ++
i)
1870 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1872 int *
src =
t1->data + j*
t1->stride;
1874 for (
i = 0;
i <
w; ++
i)
1875 datap[
i] =
src[
i] / 2;
1878 for (
i = 0;
i <
w; ++
i)
1890 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1892 int *
src =
t1->data + j*
t1->stride;
1893 for (
i = 0;
i <
w; ++
i)
1903 for (
i = 1;
i < 3;
i++) {
1914 for (
i = 0;
i < 3;
i++)
1920 for (
i = 0;
i < 2;
i++)
1932 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1933 int *
src =
t1->data + j*
t1->stride;
1934 for (
i = 0;
i <
w; ++
i)
1943 int compno, reslevelno, bandno;
1946 for (compno = 0; compno <
s->ncomponents; compno++) {
1957 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1958 int nb_precincts, precno;
1960 int cblkno = 0, bandpos;
1962 bandpos = bandno + (reslevelno > 0);
1964 if (band->
coord[0][0] == band->
coord[0][1] ||
1970 for (precno = 0; precno < nb_precincts; precno++) {
1982 bandpos,
comp->roi_shift);
1990 if (
comp->roi_shift)
2010 #define WRITE_FRAME(D, PIXEL) \
2011 static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
2012 AVFrame * picture, int precision) \
2014 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
2015 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
2016 int pixelsize = planar ? 1 : pixdesc->nb_components; \
2021 for (compno = 0; compno < s->ncomponents; compno++) { \
2022 Jpeg2000Component *comp = tile->comp + compno; \
2023 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
2025 float *datap = comp->f_data; \
2026 int32_t *i_datap = comp->i_data; \
2027 int cbps = s->cbps[compno]; \
2028 int w = tile->comp[compno].coord[0][1] - \
2029 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2030 int h = tile->comp[compno].coord[1][1] - \
2031 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2035 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
2037 y = tile->comp[compno].coord[1][0] - \
2038 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2039 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2040 for (; y < h; y++) { \
2043 x = tile->comp[compno].coord[0][0] - \
2044 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2045 dst = line + x * pixelsize + compno*!planar; \
2047 if (codsty->transform == FF_DWT97) { \
2048 for (; x < w; x++) { \
2049 int val = lrintf(*datap) + (1 << (cbps - 1)); \
2051 val = av_clip(val, 0, (1 << cbps) - 1); \
2052 *dst = val << (precision - cbps); \
2057 for (; x < w; x++) { \
2058 int val = *i_datap + (1 << (cbps - 1)); \
2060 val = av_clip(val, 0, (1 << cbps) - 1); \
2061 *dst = val << (precision - cbps); \
2066 line += picture->linesize[plane] / sizeof(PIXEL); \
2078 int jobnr,
int threadnr)
2091 for (x = 0; x <
s->ncomponents; x++) {
2092 if (
s->cdef[x] < 0) {
2093 for (x = 0; x <
s->ncomponents; x++) {
2096 if ((
s->ncomponents & 1) == 0)
2097 s->cdef[
s->ncomponents-1] = 0;
2102 if (
s->precision <= 8) {
2103 write_frame_8(
s, tile, picture, 8);
2110 write_frame_16(
s, tile, picture, precision);
2119 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2120 if (
s->tile[tileno].comp) {
2121 for (compno = 0; compno <
s->ncomponents; compno++) {
2128 av_freep(&
s->tile[tileno].packed_headers);
2129 s->tile[tileno].packed_headers_size = 0;
2133 s->packed_headers_size = 0;
2134 memset(&
s->packed_headers_stream, 0,
sizeof(
s->packed_headers_stream));
2136 memset(
s->codsty, 0,
sizeof(
s->codsty));
2137 memset(
s->qntsty, 0,
sizeof(
s->qntsty));
2138 memset(
s->properties, 0,
sizeof(
s->properties));
2139 memset(&
s->poc , 0,
sizeof(
s->poc));
2140 s->numXtiles =
s->numYtiles = 0;
2149 uint8_t *properties =
s->properties;
2161 marker = bytestream2_get_be16u(&
s->g);
2163 if (marker >= 0xFF30 && marker <= 0xFF3F)
2173 if (
s->curtileno < 0) {
2178 tile =
s->tile +
s->curtileno;
2180 if (tp->
tp_end <
s->g.buffer) {
2186 uint32_t tp_header_size = bytestream2_get_be32(&
s->packed_headers_stream);
2204 len = bytestream2_get_be16(&
s->g);
2216 if (
s->ncomponents) {
2222 s->numXtiles =
s->numYtiles = 0;
2243 if (!
s->in_tile_headers) {
2244 s->in_tile_headers = 1;
2251 codsty =
s->tile[
s->curtileno].codsty;
2252 qntsty =
s->tile[
s->curtileno].qntsty;
2253 poc = &
s->tile[
s->curtileno].poc;
2254 properties =
s->tile[
s->curtileno].properties;
2276 if (
s->in_tile_headers) {
2286 "Cannot have both PPT and PPM marker.\n");
2294 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
2301 "error during processing marker segment %.4"PRIx16
"\n",
2315 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++) {
2330 uint32_t atom_size, atom, atom_end;
2331 int search_range = 10;
2336 atom_size = bytestream2_get_be32u(&
s->g);
2337 atom = bytestream2_get_be32u(&
s->g);
2338 if (atom_size == 1) {
2339 if (bytestream2_get_be32u(&
s->g)) {
2343 atom_size = bytestream2_get_be32u(&
s->g);
2344 if (atom_size < 16 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 16 > INT_MAX)
2348 if (atom_size < 8 || (int64_t)
bytestream2_tell(&
s->g) + atom_size - 8 > INT_MAX)
2361 uint32_t atom2_size, atom2, atom2_end;
2365 atom2_size = bytestream2_get_be32u(&
s->g);
2366 atom2 = bytestream2_get_be32u(&
s->g);
2368 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2373 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
2374 int method = bytestream2_get_byteu(&
s->g);
2377 s->colour_space = bytestream2_get_be32u(&
s->g);
2379 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
2380 int i,
size, colour_count, colour_channels, colour_depth[3];
2381 colour_count = bytestream2_get_be16u(&
s->g);
2382 colour_channels = bytestream2_get_byteu(&
s->g);
2384 colour_depth[0] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2385 colour_depth[1] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2386 colour_depth[2] = (bytestream2_get_byteu(&
s->g) & 0x7f) + 1;
2387 size = (colour_depth[0] + 7 >> 3) * colour_count +
2388 (colour_depth[1] + 7 >> 3) * colour_count +
2389 (colour_depth[2] + 7 >> 3) * colour_count;
2391 colour_channels != 3 ||
2392 colour_depth[0] > 16 ||
2393 colour_depth[1] > 16 ||
2394 colour_depth[2] > 16 ||
2395 atom2_size <
size) {
2401 for (
i = 0;
i < colour_count;
i++) {
2403 if (colour_depth[0] <= 8) {
2404 r = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[0];
2405 r |=
r >> colour_depth[0];
2407 r = bytestream2_get_be16u(&
s->g) >> colour_depth[0] - 8;
2409 if (colour_depth[1] <= 8) {
2410 g = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[1];
2411 g |=
g >> colour_depth[1];
2413 g = bytestream2_get_be16u(&
s->g) >> colour_depth[1] - 8;
2415 if (colour_depth[2] <= 8) {
2416 b = bytestream2_get_byteu(&
s->g) << 8 - colour_depth[2];
2417 b |=
b >> colour_depth[2];
2419 b = bytestream2_get_be16u(&
s->g) >> colour_depth[2] - 8;
2421 s->palette[
i] = 0xff
u << 24 |
r << 16 |
g << 8 |
b;
2423 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
2424 int n = bytestream2_get_be16u(&
s->g);
2426 int cn = bytestream2_get_be16(&
s->g);
2427 int av_unused typ = bytestream2_get_be16(&
s->g);
2428 int asoc = bytestream2_get_be16(&
s->g);
2429 if (cn < 4 && asoc < 4)
2432 }
else if (atom2 ==
MKBETAG(
'r',
'e',
's',
' ') && atom2_size >= 18) {
2433 int64_t vnum, vden, hnum, hden, vexp, hexp;
2436 resx = bytestream2_get_be32u(&
s->g);
2437 if (resx !=
MKBETAG(
'r',
'e',
's',
'c') && resx !=
MKBETAG(
'r',
'e',
's',
'd')) {
2441 vnum = bytestream2_get_be16u(&
s->g);
2442 vden = bytestream2_get_be16u(&
s->g);
2443 hnum = bytestream2_get_be16u(&
s->g);
2444 hden = bytestream2_get_be16u(&
s->g);
2445 vexp = bytestream2_get_byteu(&
s->g);
2446 hexp = bytestream2_get_byteu(&
s->g);
2447 if (!vnum || !vden || !hnum || !hden) {
2459 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2460 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2462 hnum * vden * pow(10, hexp),
2463 vnum * hden * pow(10, vexp),
2467 }
while (atom_end - atom2_end >= 8);
2498 memset(
s->cdef, -1,
sizeof(
s->cdef));
2507 (bytestream2_get_be32u(&
s->g) == 12) &&
2512 "Could not find Jpeg2000 codestream atom.\n");
2547 memcpy(picture->
data[1],
s->palette, 256 *
sizeof(uint32_t));
2548 if (
s->sar.num &&
s->sar.den)
2550 s->sar.num =
s->sar.den = 0;
2559 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2560 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2563 {
"lowres",
"Lower the decoding resolution by a power of two",