42 #define JP2_SIG_TYPE 0x6A502020
43 #define JP2_SIG_VALUE 0x0D0A870A
44 #define JP2_CODESTREAM 0x6A703263
45 #define JP2_HEADER 0x6A703268
114 s->
bit_index = 7 + (bytestream2_get_byte(&s->
g) != 0xFF
u);
117 res |= (bytestream2_peek_byte(&s->
g) >> s->
bit_index) & 1;
124 if (bytestream2_get_byte(&s->
g) == 0xff)
134 int sp = -1, curval = 0;
139 while (node && !node->
vis) {
147 curval = stack[
sp]->
val;
149 while (curval < threshold && sp >= 0) {
150 if (curval < stack[sp]->
val)
151 curval = stack[
sp]->
val;
152 while (curval < threshold) {
162 stack[
sp]->
val = curval;
169 int bpc, uint32_t log2_chroma_wh,
int pal8)
178 switch (components) {
181 (log2_chroma_wh >> 14 & 3) == 0 &&
182 (log2_chroma_wh >> 12 & 3) == 0;
194 (log2_chroma_wh >> 2 & 3) == 0 &&
195 (log2_chroma_wh & 3) == 0 &&
203 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
204 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
205 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
206 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
207 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
208 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
209 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
210 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
211 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
212 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
213 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
214 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
215 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
216 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
233 uint32_t log2_chroma_wh = 0;
235 int possible_fmts_nb = 0;
241 s->
width = bytestream2_get_be32u(&s->
g);
242 s->
height = bytestream2_get_be32u(&s->
g);
249 ncomponents = bytestream2_get_be16u(&s->
g);
260 if (ncomponents <= 0) {
266 if (ncomponents > 4) {
284 uint8_t x = bytestream2_get_byteu(&s->
g);
285 s->
cbps[i] = (x & 0x7f) + 1;
287 s->
sgnd[i] = !!(x & 0x80);
288 s->
cdx[i] = bytestream2_get_byteu(&s->
g);
289 s->
cdy[i] = bytestream2_get_byteu(&s->
g);
290 if ( !s->
cdx[i] || s->
cdx[i] == 3 || s->
cdx[i] > 4
291 || !s->
cdy[i] || s->
cdy[i] == 3 || s->
cdy[i] > 4) {
295 log2_chroma_wh |= s->
cdy[i] >> 1 << i * 4 | s->
cdx[i] >> 1 << i * 4 + 2;
350 for (i = 0; i < possible_fmts_nb; ++i) {
356 if (i == possible_fmts_nb) {
358 "Unknown pix_fmt, profile: %d, colour_space: %d, "
359 "components: %d, precision: %d, "
360 "cdx[1]: %d, cdy[1]: %d, cdx[2]: %d, cdy[2]: %d\n",
362 ncomponents > 2 ? s->
cdx[1] : 0,
363 ncomponents > 2 ? s->
cdy[1] : 0,
364 ncomponents > 2 ? s->
cdx[2] : 0,
365 ncomponents > 2 ? s->
cdy[2] : 0);
427 byte = bytestream2_get_byte(&s->
g);
448 tmp.
csty = bytestream2_get_byteu(&s->
g);
453 tmp.
nlayers = bytestream2_get_be16u(&s->
g);
454 tmp.
mct = bytestream2_get_byteu(&s->
g);
458 "MCT %"PRIu8
" with too few components (%d)\n",
463 if ((ret =
get_cox(s, &tmp)) < 0)
466 for (compno = 0; compno < s->
ncomponents; compno++)
467 if (!(properties[compno] &
HAD_COC))
468 memcpy(c + compno, &tmp,
sizeof(tmp));
482 compno = bytestream2_get_byteu(&s->
g);
486 "Invalid compno %d. There are %d components in the image.\n",
492 c->
csty = bytestream2_get_byteu(&s->
g);
509 x = bytestream2_get_byteu(&s->
g);
519 for (i = 0; i <
n; i++)
520 q->
expn[i] = bytestream2_get_byteu(&s->
g) >> 3;
524 x = bytestream2_get_be16u(&s->
g);
525 q->
expn[0] = x >> 11;
526 q->
mant[0] = x & 0x7ff;
528 int curexpn =
FFMAX(0, q->
expn[0] - (i - 1) / 3);
529 q->
expn[i] = curexpn;
537 for (i = 0; i <
n; i++) {
538 x = bytestream2_get_be16u(&s->
g);
539 q->
expn[i] = x >> 11;
540 q->
mant[i] = x & 0x7ff;
553 memset(&tmp, 0,
sizeof(tmp));
555 if ((ret =
get_qcx(s, n, &tmp)) < 0)
557 for (compno = 0; compno < s->
ncomponents; compno++)
558 if (!(properties[compno] &
HAD_QCC))
559 memcpy(q + compno, &tmp,
sizeof(tmp));
573 compno = bytestream2_get_byteu(&s->
g);
577 "Invalid compno %d. There are %d components in the image.\n",
583 return get_qcx(s, n - 1, q + compno);
598 Isot = bytestream2_get_be16u(&s->
g);
603 Psot = bytestream2_get_be32u(&s->
g);
604 TPsot = bytestream2_get_byteu(&s->
g);
607 bytestream2_get_byteu(&s->
g);
646 bytestream2_get_byte(&s->
g);
647 Stlm = bytestream2_get_byte(&s->
g);
650 ST = (Stlm >> 4) & 0x03;
652 SP = (Stlm >> 6) & 0x01;
653 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
654 for (i = 0; i < tile_tlm; i++) {
659 bytestream2_get_byte(&s->
g);
662 bytestream2_get_be16(&s->
g);
665 bytestream2_get_be32(&s->
g);
669 bytestream2_get_be16(&s->
g);
671 bytestream2_get_be32(&s->
g);
687 for (compno = 0; compno < s->
ncomponents; compno++) {
704 s->
cbps[compno], s->
cdx[compno],
720 return num < 0 ? num : 3 + num;
722 return num < 0 ? num : 6 + num;
724 return num < 0 ? num : 37 + num;
741 int layno,
uint8_t *expn,
int numgbits)
743 int bandno, cblkno,
ret, nb_code_blocks;
751 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
760 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
762 int incl, newpasses, llen;
774 int v = expn[bandno] + numgbits - 1 -
778 "nonzerobits %d invalid\n", v);
790 if (ret >
sizeof(cblk->
data)) {
809 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
814 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
820 "Block length %"PRIu16
" or lengthinc %d is too large\n",
836 int layno, reslevelno, compno, precno, ok_reslevel;
847 for (reslevelno = 0; ok_reslevel; reslevelno++) {
849 for (compno = 0; compno < s->
ncomponents; compno++) {
852 if (reslevelno < codsty->nreslevels) {
860 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
870 for (compno = 0; compno < s->
ncomponents; compno++) {
882 for (y = 0; y < s->
height; y += 256) {
886 for (x = 0; x < s->
width; x += 256) {
887 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
911 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
943 int bpno,
int bandno,
int bpass_csty_symbol,
944 int vert_causal_ctx_csty_symbol)
946 int mask = 3 << (bpno - 1), y0, x,
y;
948 for (y0 = 0; y0 <
height; y0 += 4)
949 for (x = 0; x <
width; x++)
950 for (
y = y0;
y < height &&
y < y0 + 4;
y++) {
954 if (vert_causal_ctx_csty_symbol &&
y == y0 + 3)
958 if (bpass_csty_symbol)
978 phalf = 1 << (bpno - 1);
981 for (y0 = 0; y0 <
height; y0 += 4)
982 for (x = 0; x <
width; x++)
983 for (y = y0; y < height && y < y0 + 4; y++)
996 int seg_symbols,
int vert_causal_ctx_csty_symbol)
998 int mask = 3 << (bpno - 1), y0, x,
y, runlen, dec;
1000 for (y0 = 0; y0 <
height; y0 += 4) {
1001 for (x = 0; x <
width; x++) {
1002 if (y0 + 3 < height &&
1020 for (
y = y0 + runlen;
y < y0 + 4 &&
y <
height;
y++) {
1023 int flags_mask = -1;
1024 if (vert_causal_ctx_csty_symbol &&
y == y0 + 3)
1053 "Segmentation symbol value incorrect\n");
1062 int clnpass_cnt = 0;
1070 memset(t1->
data[
y], 0, width *
sizeof(**t1->
data));
1076 for (
y = 0;
y < height + 2;
y++)
1077 memset(t1->
flags[
y], 0, (width + 2) *
sizeof(**t1->
flags));
1091 bpass_csty_symbol && (clnpass_cnt >= 4),
1092 vert_causal_ctx_csty_symbol);
1096 if (bpass_csty_symbol && clnpass_cnt >= 4)
1102 vert_causal_ctx_csty_symbol);
1103 clnpass_cnt = clnpass_cnt + 1;
1104 if (bpass_csty_symbol && clnpass_cnt >= 4)
1130 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1131 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1132 float *datap = &comp->
f_data[(comp->
coord[0][1] - comp->
coord[0][0]) * (y + j) + x];
1134 for (i = 0; i < w; ++i)
1145 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1146 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1149 for (i = 0; i < w; ++i)
1150 datap[i] = (src[i] * band->
i_stepsize + (1 << 14)) >> 15;
1159 for (i = 1; i < 3; i++) {
1170 for (i = 0; i < 3; i++)
1176 for (i = 0; i < 2; i++)
1186 int compno, reslevelno, bandno;
1195 for (compno = 0; compno < s->
ncomponents; compno++) {
1203 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1204 int nb_precincts, precno;
1206 int cblkno = 0, bandpos;
1208 bandpos = bandno + (reslevelno > 0);
1210 if (band->
coord[0][0] == band->
coord[0][1] ||
1216 for (precno = 0; precno < nb_precincts; precno++) {
1228 x = cblk->
coord[0][0];
1229 y = cblk->
coord[1][0];
1249 if (s->
cdef[x] < 0) {
1260 for (compno = 0; compno < s->
ncomponents; compno++) {
1263 float *datap = comp->
f_data;
1265 int cbps = s->
cbps[compno];
1274 line = picture->
data[plane] + y / s->
cdy[compno] * picture->
linesize[plane];
1279 dst = line + x / s->
cdx[compno] * pixelsize + compno*!planar;
1282 for (; x < w; x += s->
cdx[compno]) {
1283 int val =
lrintf(*datap) + (1 << (cbps - 1));
1285 val = av_clip(val, 0, (1 << cbps) - 1);
1286 *dst = val << (8 - cbps);
1291 for (; x < w; x += s->
cdx[compno]) {
1292 int val = *i_datap + (1 << (cbps - 1));
1294 val = av_clip(val, 0, (1 << cbps) - 1);
1295 *dst = val << (8 - cbps);
1304 for (compno = 0; compno < s->
ncomponents; compno++) {
1307 float *datap = comp->
f_data;
1310 int cbps = s->
cbps[compno];
1318 linel = (uint16_t *)picture->
data[plane] + y / s->
cdy[compno] * (picture->
linesize[plane] >> 1);
1323 dst = linel + (x / s->
cdx[compno] * pixelsize + compno*!planar);
1325 for (; x < w; x += s-> cdx[compno]) {
1326 int val =
lrintf(*datap) + (1 << (cbps - 1));
1328 val = av_clip(val, 0, (1 << cbps) - 1);
1330 *dst = val << (16 - cbps);
1335 for (; x < w; x += s-> cdx[compno]) {
1336 int val = *i_datap + (1 << (cbps - 1));
1338 val = av_clip(val, 0, (1 << cbps) - 1);
1340 *dst = val << (16 - cbps);
1345 linel += picture->
linesize[plane] >> 1;
1358 for (compno = 0; compno < s->
ncomponents; compno++) {
1390 marker = bytestream2_get_be16u(&s->
g);
1420 len = bytestream2_get_be16(&s->
g);
1435 ret =
get_coc(s, codsty, properties);
1438 ret =
get_cod(s, codsty, properties);
1441 ret =
get_qcc(s, len, qntsty, properties);
1444 ret =
get_qcd(s, len, qntsty, properties);
1447 if (!(ret =
get_sot(s, len))) {
1464 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
1471 "error during processing marker segment %.4"PRIx16
"\n",
1473 return ret ? ret : -1;
1501 uint32_t atom_size, atom, atom_end;
1502 int search_range = 10;
1507 atom_size = bytestream2_get_be32u(&s->
g);
1508 atom = bytestream2_get_be32u(&s->
g);
1519 uint32_t atom2_size, atom2, atom2_end;
1521 atom2_size = bytestream2_get_be32u(&s->
g);
1522 atom2 = bytestream2_get_be32u(&s->
g);
1524 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
1528 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
1529 int method = bytestream2_get_byteu(&s->
g);
1534 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
1535 int i,
size, colour_count, colour_channels, colour_depth[3];
1537 colour_count = bytestream2_get_be16u(&s->
g);
1538 colour_channels = bytestream2_get_byteu(&s->
g);
1540 colour_depth[0] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1541 colour_depth[1] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1542 colour_depth[2] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1543 size = (colour_depth[0] + 7 >> 3) * colour_count +
1544 (colour_depth[1] + 7 >> 3) * colour_count +
1545 (colour_depth[2] + 7 >> 3) * colour_count;
1546 if (colour_count > 256 ||
1547 colour_channels != 3 ||
1548 colour_depth[0] > 16 ||
1549 colour_depth[1] > 16 ||
1550 colour_depth[2] > 16 ||
1551 atom2_size < size) {
1557 for (i = 0; i < colour_count; i++) {
1558 if (colour_depth[0] <= 8) {
1559 r = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[0];
1560 r |= r >> colour_depth[0];
1562 r = bytestream2_get_be16u(&s->
g) >> colour_depth[0] - 8;
1564 if (colour_depth[1] <= 8) {
1565 g = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[1];
1566 r |= r >> colour_depth[1];
1568 g = bytestream2_get_be16u(&s->
g) >> colour_depth[1] - 8;
1570 if (colour_depth[2] <= 8) {
1571 b = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[2];
1572 r |= r >> colour_depth[2];
1574 b = bytestream2_get_be16u(&s->
g) >> colour_depth[2] - 8;
1576 s->
palette[i] = 0xff
u << 24 | r << 16 | g << 8 |
b;
1578 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
1579 int n = bytestream2_get_be16u(&s->
g);
1581 int cn = bytestream2_get_be16(&s->
g);
1582 int av_unused typ = bytestream2_get_be16(&s->
g);
1583 int asoc = bytestream2_get_be16(&s->
g);
1584 if (cn < 4 && asoc < 4)
1589 }
while (atom_end - atom2_end >= 8);
1619 memset(s->
cdef, -1,
sizeof(s->
cdef));
1628 (bytestream2_get_be32u(&s->
g) == 12) &&
1633 "Could not find Jpeg2000 codestream atom.\n");
1670 memcpy(picture->
data[1], s->
palette, 256 *
sizeof(uint32_t));
1685 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1686 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1689 {
"lowres",
"Lower the decoding resolution by a power of two",
1720 .priv_class = &jpeg2000_class,