38 #define JP2_SIG_TYPE 0x6A502020
39 #define JP2_SIG_VALUE 0x0D0A870A
40 #define JP2_CODESTREAM 0x6A703263
41 #define JP2_HEADER 0x6A703268
109 s->
bit_index = 7 + (bytestream2_get_byte(&s->
g) != 0xFF
u);
112 res |= (bytestream2_peek_byte(&s->
g) >> s->
bit_index) & 1;
119 if (bytestream2_get_byte(&s->
g) == 0xff)
129 int sp = -1, curval = 0;
134 while (node && !node->
vis) {
142 curval = stack[
sp]->
val;
144 while (curval < threshold && sp >= 0) {
145 if (curval < stack[sp]->
val)
146 curval = stack[
sp]->
val;
147 while (curval < threshold) {
157 stack[
sp]->
val = curval;
164 int bpc, uint32_t log2_chroma_wh,
int pal8)
173 switch (components) {
176 (log2_chroma_wh >> 14 & 3) == 0 &&
177 (log2_chroma_wh >> 12 & 3) == 0;
189 (log2_chroma_wh >> 2 & 3) == 0 &&
190 (log2_chroma_wh & 3) == 0 &&
198 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
199 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16
200 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
201 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
202 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
203 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
204 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
205 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
206 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
207 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
208 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
209 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
210 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
211 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
228 uint32_t log2_chroma_wh = 0;
230 int possible_fmts_nb = 0;
236 s->
width = bytestream2_get_be32u(&s->
g);
237 s->
height = bytestream2_get_be32u(&s->
g);
244 ncomponents = bytestream2_get_be16u(&s->
g);
251 if (ncomponents <= 0) {
257 if (ncomponents > 4) {
275 uint8_t x = bytestream2_get_byteu(&s->
g);
276 s->
cbps[i] = (x & 0x7f) + 1;
278 s->
sgnd[i] = !!(x & 0x80);
279 s->
cdx[i] = bytestream2_get_byteu(&s->
g);
280 s->
cdy[i] = bytestream2_get_byteu(&s->
g);
281 if ( !s->
cdx[i] || s->
cdx[i] == 3 || s->
cdx[i] > 4
282 || !s->
cdy[i] || s->
cdy[i] == 3 || s->
cdy[i] > 4) {
286 log2_chroma_wh |= s->
cdy[i] >> 1 << i * 4 | s->
cdx[i] >> 1 << i * 4 + 2;
341 for (i = 0; i < possible_fmts_nb; ++i) {
347 if (i == possible_fmts_nb) {
349 "Unknown pix_fmt, profile: %d, colour_space: %d, "
350 "components: %d, precision: %d, "
351 "cdx[1]: %d, cdy[1]: %d, cdx[2]: %d, cdy[2]: %d\n",
353 ncomponents > 2 ? s->
cdx[1] : 0,
354 ncomponents > 2 ? s->
cdy[1] : 0,
355 ncomponents > 2 ? s->
cdx[2] : 0,
356 ncomponents > 2 ? s->
cdy[2] : 0);
418 byte = bytestream2_get_byte(&s->
g);
439 tmp.
csty = bytestream2_get_byteu(&s->
g);
444 tmp.
nlayers = bytestream2_get_be16u(&s->
g);
445 tmp.
mct = bytestream2_get_byteu(&s->
g);
449 "MCT %d with too few components (%d)\n",
454 if ((ret =
get_cox(s, &tmp)) < 0)
457 for (compno = 0; compno < s->
ncomponents; compno++)
458 if (!(properties[compno] &
HAD_COC))
459 memcpy(c + compno, &tmp,
sizeof(tmp));
473 compno = bytestream2_get_byteu(&s->
g);
477 "Invalid compno %d. There are %d components in the image.\n",
483 c->
csty = bytestream2_get_byteu(&s->
g);
500 x = bytestream2_get_byteu(&s->
g);
510 for (i = 0; i <
n; i++)
511 q->
expn[i] = bytestream2_get_byteu(&s->
g) >> 3;
515 x = bytestream2_get_be16u(&s->
g);
516 q->
expn[0] = x >> 11;
517 q->
mant[0] = x & 0x7ff;
519 int curexpn =
FFMAX(0, q->
expn[0] - (i - 1) / 3);
520 q->
expn[i] = curexpn;
528 for (i = 0; i <
n; i++) {
529 x = bytestream2_get_be16u(&s->
g);
530 q->
expn[i] = x >> 11;
531 q->
mant[i] = x & 0x7ff;
544 memset(&tmp, 0,
sizeof(tmp));
546 if ((ret =
get_qcx(s, n, &tmp)) < 0)
548 for (compno = 0; compno < s->
ncomponents; compno++)
549 if (!(properties[compno] &
HAD_QCC))
550 memcpy(q + compno, &tmp,
sizeof(tmp));
564 compno = bytestream2_get_byteu(&s->
g);
568 "Invalid compno %d. There are %d components in the image.\n",
574 return get_qcx(s, n - 1, q + compno);
589 Isot = bytestream2_get_be16u(&s->
g);
594 Psot = bytestream2_get_be32u(&s->
g);
595 TPsot = bytestream2_get_byteu(&s->
g);
598 bytestream2_get_byteu(&s->
g);
636 uint8_t Stlm, ST, SP, tile_tlm, i;
637 bytestream2_get_byte(&s->
g);
638 Stlm = bytestream2_get_byte(&s->
g);
641 ST = (Stlm >> 4) & 0x03;
643 SP = (Stlm >> 6) & 0x01;
644 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
645 for (i = 0; i < tile_tlm; i++) {
650 bytestream2_get_byte(&s->
g);
653 bytestream2_get_be16(&s->
g);
656 bytestream2_get_be32(&s->
g);
660 bytestream2_get_be16(&s->
g);
662 bytestream2_get_be32(&s->
g);
678 for (compno = 0; compno < s->
ncomponents; compno++) {
695 s->
cbps[compno], s->
cdx[compno],
711 return num < 0 ? num : 3 + num;
713 return num < 0 ? num : 6 + num;
715 return num < 0 ? num : 37 + num;
732 int layno,
uint8_t *expn,
int numgbits)
734 int bandno, cblkno,
ret, nb_code_blocks;
742 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
751 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
753 int incl, newpasses, llen;
765 int v = expn[bandno] + numgbits - 1 -
769 "nonzerobits %d invalid\n", v);
781 if (ret >
sizeof(cblk->
data)) {
783 "Block with lengthinc greater than %zu",
800 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
805 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
811 "Block length %d or lengthinc %d is too large\n",
827 int layno, reslevelno, compno, precno, ok_reslevel;
838 for (reslevelno = 0; ok_reslevel; reslevelno++) {
840 for (compno = 0; compno < s->
ncomponents; compno++) {
843 if (reslevelno < codsty->nreslevels) {
851 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
861 for (compno = 0; compno < s->
ncomponents; compno++) {
873 for (y = 0; y < s->
height; y += 256) {
877 for (x = 0; x < s->
width; x += 256) {
878 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
902 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
934 int bpno,
int bandno,
int bpass_csty_symbol,
935 int vert_causal_ctx_csty_symbol)
937 int mask = 3 << (bpno - 1), y0, x,
y;
939 for (y0 = 0; y0 <
height; y0 += 4)
940 for (x = 0; x <
width; x++)
941 for (
y = y0;
y < height &&
y < y0 + 4;
y++) {
945 if (vert_causal_ctx_csty_symbol &&
y == y0 + 3)
949 if (bpass_csty_symbol)
969 phalf = 1 << (bpno - 1);
972 for (y0 = 0; y0 <
height; y0 += 4)
973 for (x = 0; x <
width; x++)
974 for (y = y0; y < height && y < y0 + 4; y++)
987 int seg_symbols,
int vert_causal_ctx_csty_symbol)
989 int mask = 3 << (bpno - 1), y0, x,
y, runlen, dec;
991 for (y0 = 0; y0 <
height; y0 += 4) {
992 for (x = 0; x <
width; x++) {
993 if (y0 + 3 < height &&
1011 for (
y = y0 + runlen;
y < y0 + 4 &&
y <
height;
y++) {
1014 int flags_mask = -1;
1015 if (vert_causal_ctx_csty_symbol &&
y == y0 + 3)
1044 "Segmentation symbol value incorrect\n");
1053 int clnpass_cnt = 0;
1061 memset(t1->
data[
y], 0, width *
sizeof(**t1->
data));
1067 for (
y = 0;
y < height + 2;
y++)
1068 memset(t1->
flags[
y], 0, (width + 2) *
sizeof(**t1->
flags));
1078 bpass_csty_symbol && (clnpass_cnt >= 4),
1079 vert_causal_ctx_csty_symbol);
1083 if (bpass_csty_symbol && clnpass_cnt >= 4)
1089 vert_causal_ctx_csty_symbol);
1090 clnpass_cnt = clnpass_cnt + 1;
1091 if (bpass_csty_symbol && clnpass_cnt >= 4)
1117 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1118 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1119 float *datap = &comp->
f_data[(comp->
coord[0][1] - comp->
coord[0][0]) * (y + j) + x];
1121 for (i = 0; i < w; ++i)
1132 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1133 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1136 for (i = 0; i < w; ++i)
1137 datap[i] = (src[i] * band->
i_stepsize + (1 << 14)) >> 15;
1160 float *srcf[3], i0f, i1f,
i2f;
1162 for (i = 1; i < 3; i++)
1168 for (i = 0; i < 3; i++)
1174 for (i = 0; i < 2; i++)
1179 for (i = 0; i < csize; i++) {
1190 for (i = 0; i < csize; i++) {
1191 i0 = *src[0] + (((
i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
1192 i1 = *src[0] - (((
i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
1194 i2 = *src[0] + (((
i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
1201 for (i = 0; i < csize; i++) {
1202 i1 = *src[0] - (*src[2] + *src[1] >> 2);
1217 int compno, reslevelno, bandno;
1226 for (compno = 0; compno < s->
ncomponents; compno++) {
1234 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1235 int nb_precincts, precno;
1237 int cblkno = 0, bandpos;
1239 bandpos = bandno + (reslevelno > 0);
1241 if (band->
coord[0][0] == band->
coord[0][1] ||
1247 for (precno = 0; precno < nb_precincts; precno++) {
1259 x = cblk->
coord[0][0];
1260 y = cblk->
coord[1][0];
1279 if (s->
cdef[0] < 0) {
1287 for (compno = 0; compno < s->
ncomponents; compno++) {
1290 float *datap = comp->
f_data;
1292 int cbps = s->
cbps[compno];
1301 line = picture->
data[plane] + y / s->
cdy[compno] * picture->
linesize[plane];
1306 dst = line + x / s->
cdx[compno] * pixelsize + compno*!planar;
1309 for (; x < w; x += s->
cdx[compno]) {
1310 int val =
lrintf(*datap) + (1 << (cbps - 1));
1312 val = av_clip(val, 0, (1 << cbps) - 1);
1313 *dst = val << (8 - cbps);
1318 for (; x < w; x += s->
cdx[compno]) {
1319 int val = *i_datap + (1 << (cbps - 1));
1321 val = av_clip(val, 0, (1 << cbps) - 1);
1322 *dst = val << (8 - cbps);
1331 for (compno = 0; compno < s->
ncomponents; compno++) {
1334 float *datap = comp->
f_data;
1337 int cbps = s->
cbps[compno];
1345 linel = (uint16_t *)picture->
data[plane] + y / s->
cdy[compno] * (picture->
linesize[plane] >> 1);
1350 dst = linel + (x / s->
cdx[compno] * pixelsize + compno*!planar);
1352 for (; x < w; x += s-> cdx[compno]) {
1353 int val =
lrintf(*datap) + (1 << (cbps - 1));
1355 val = av_clip(val, 0, (1 << cbps) - 1);
1357 *dst = val << (16 - cbps);
1362 for (; x < w; x += s-> cdx[compno]) {
1363 int val = *i_datap + (1 << (cbps - 1));
1365 val = av_clip(val, 0, (1 << cbps) - 1);
1367 *dst = val << (16 - cbps);
1372 linel += picture->
linesize[plane] >> 1;
1385 for (compno = 0; compno < s->
ncomponents; compno++) {
1416 marker = bytestream2_get_be16u(&s->
g);
1446 len = bytestream2_get_be16(&s->
g);
1457 ret =
get_coc(s, codsty, properties);
1460 ret =
get_cod(s, codsty, properties);
1463 ret =
get_qcc(s, len, qntsty, properties);
1466 ret =
get_qcd(s, len, qntsty, properties);
1469 if (!(ret =
get_sot(s, len))) {
1486 "unsupported marker 0x%.4X at pos 0x%X\n",
1493 "error during processing marker segment %.4x\n", marker);
1494 return ret ? ret : -1;
1522 uint32_t atom_size, atom, atom_end;
1523 int search_range = 10;
1528 atom_size = bytestream2_get_be32u(&s->
g);
1529 atom = bytestream2_get_be32u(&s->
g);
1540 uint32_t atom2_size, atom2, atom2_end;
1542 atom2_size = bytestream2_get_be32u(&s->
g);
1543 atom2 = bytestream2_get_be32u(&s->
g);
1545 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
1549 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
1550 int method = bytestream2_get_byteu(&s->
g);
1555 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
1556 int i,
size, colour_count, colour_channels, colour_depth[3];
1558 colour_count = bytestream2_get_be16u(&s->
g);
1559 colour_channels = bytestream2_get_byteu(&s->
g);
1561 colour_depth[0] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1562 colour_depth[1] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1563 colour_depth[2] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1564 size = (colour_depth[0] + 7 >> 3) * colour_count +
1565 (colour_depth[1] + 7 >> 3) * colour_count +
1566 (colour_depth[2] + 7 >> 3) * colour_count;
1567 if (colour_count > 256 ||
1568 colour_channels != 3 ||
1569 colour_depth[0] > 16 ||
1570 colour_depth[1] > 16 ||
1571 colour_depth[2] > 16 ||
1572 atom2_size < size) {
1578 for (i = 0; i < colour_count; i++) {
1579 if (colour_depth[0] <= 8) {
1580 r = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[0];
1581 r |= r >> colour_depth[0];
1583 r = bytestream2_get_be16u(&s->
g) >> colour_depth[0] - 8;
1585 if (colour_depth[1] <= 8) {
1586 g = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[1];
1587 r |= r >> colour_depth[1];
1589 g = bytestream2_get_be16u(&s->
g) >> colour_depth[1] - 8;
1591 if (colour_depth[2] <= 8) {
1592 b = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[2];
1593 r |= r >> colour_depth[2];
1595 b = bytestream2_get_be16u(&s->
g) >> colour_depth[2] - 8;
1597 s->
palette[i] = 0xff
u << 24 | r << 16 | g << 8 |
b;
1599 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
1600 int n = bytestream2_get_be16u(&s->
g);
1602 int cn = bytestream2_get_be16(&s->
g);
1603 int av_unused typ = bytestream2_get_be16(&s->
g);
1604 int asoc = bytestream2_get_be16(&s->
g);
1605 if (cn < 4 || asoc < 4)
1610 }
while (atom_end - atom2_end >= 8);
1631 memset(s->
cdef, -1,
sizeof(s->
cdef));
1640 (bytestream2_get_be32u(&s->
g) == 12) &&
1645 "Could not find Jpeg2000 codestream atom.\n");
1682 memcpy(picture->
data[1], s->
palette, 256 *
sizeof(uint32_t));
1697 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1698 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1701 {
"lowres",
"Lower the decoding resolution by a power of two",
1731 .priv_class = &jpeg2000_class,