41 #define JP2_SIG_TYPE 0x6A502020
42 #define JP2_SIG_VALUE 0x0D0A870A
43 #define JP2_CODESTREAM 0x6A703263
44 #define JP2_HEADER 0x6A703268
112 s->
bit_index = 7 + (bytestream2_get_byte(&s->
g) != 0xFF
u);
115 res |= (bytestream2_peek_byte(&s->
g) >> s->
bit_index) & 1;
122 if (bytestream2_get_byte(&s->
g) == 0xff)
132 int sp = -1, curval = 0;
137 while (node && !node->
vis) {
145 curval = stack[
sp]->
val;
147 while (curval < threshold && sp >= 0) {
148 if (curval < stack[sp]->
val)
149 curval = stack[
sp]->
val;
150 while (curval < threshold) {
160 stack[
sp]->
val = curval;
167 int bpc, uint32_t log2_chroma_wh,
int pal8)
176 switch (components) {
179 (log2_chroma_wh >> 14 & 3) == 0 &&
180 (log2_chroma_wh >> 12 & 3) == 0;
192 (log2_chroma_wh >> 2 & 3) == 0 &&
193 (log2_chroma_wh & 3) == 0 &&
201 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
202 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16
203 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
204 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
205 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
206 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
207 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
208 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
209 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
210 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
211 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
212 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
213 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
214 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
231 uint32_t log2_chroma_wh = 0;
233 int possible_fmts_nb = 0;
239 s->
width = bytestream2_get_be32u(&s->
g);
240 s->
height = bytestream2_get_be32u(&s->
g);
247 ncomponents = bytestream2_get_be16u(&s->
g);
254 if (ncomponents <= 0) {
260 if (ncomponents > 4) {
278 uint8_t x = bytestream2_get_byteu(&s->
g);
279 s->
cbps[i] = (x & 0x7f) + 1;
281 s->
sgnd[i] = !!(x & 0x80);
282 s->
cdx[i] = bytestream2_get_byteu(&s->
g);
283 s->
cdy[i] = bytestream2_get_byteu(&s->
g);
284 if ( !s->
cdx[i] || s->
cdx[i] == 3 || s->
cdx[i] > 4
285 || !s->
cdy[i] || s->
cdy[i] == 3 || s->
cdy[i] > 4) {
289 log2_chroma_wh |= s->
cdy[i] >> 1 << i * 4 | s->
cdx[i] >> 1 << i * 4 + 2;
344 for (i = 0; i < possible_fmts_nb; ++i) {
350 if (i == possible_fmts_nb) {
352 "Unknown pix_fmt, profile: %d, colour_space: %d, "
353 "components: %d, precision: %d, "
354 "cdx[1]: %d, cdy[1]: %d, cdx[2]: %d, cdy[2]: %d\n",
356 ncomponents > 2 ? s->
cdx[1] : 0,
357 ncomponents > 2 ? s->
cdy[1] : 0,
358 ncomponents > 2 ? s->
cdx[2] : 0,
359 ncomponents > 2 ? s->
cdy[2] : 0);
421 byte = bytestream2_get_byte(&s->
g);
442 tmp.
csty = bytestream2_get_byteu(&s->
g);
447 tmp.
nlayers = bytestream2_get_be16u(&s->
g);
448 tmp.
mct = bytestream2_get_byteu(&s->
g);
452 "MCT %"PRIu8
" with too few components (%d)\n",
457 if ((ret =
get_cox(s, &tmp)) < 0)
460 for (compno = 0; compno < s->
ncomponents; compno++)
461 if (!(properties[compno] &
HAD_COC))
462 memcpy(c + compno, &tmp,
sizeof(tmp));
476 compno = bytestream2_get_byteu(&s->
g);
480 "Invalid compno %d. There are %d components in the image.\n",
486 c->
csty = bytestream2_get_byteu(&s->
g);
503 x = bytestream2_get_byteu(&s->
g);
513 for (i = 0; i <
n; i++)
514 q->
expn[i] = bytestream2_get_byteu(&s->
g) >> 3;
518 x = bytestream2_get_be16u(&s->
g);
519 q->
expn[0] = x >> 11;
520 q->
mant[0] = x & 0x7ff;
522 int curexpn =
FFMAX(0, q->
expn[0] - (i - 1) / 3);
523 q->
expn[i] = curexpn;
531 for (i = 0; i <
n; i++) {
532 x = bytestream2_get_be16u(&s->
g);
533 q->
expn[i] = x >> 11;
534 q->
mant[i] = x & 0x7ff;
547 memset(&tmp, 0,
sizeof(tmp));
549 if ((ret =
get_qcx(s, n, &tmp)) < 0)
551 for (compno = 0; compno < s->
ncomponents; compno++)
552 if (!(properties[compno] &
HAD_QCC))
553 memcpy(q + compno, &tmp,
sizeof(tmp));
567 compno = bytestream2_get_byteu(&s->
g);
571 "Invalid compno %d. There are %d components in the image.\n",
577 return get_qcx(s, n - 1, q + compno);
592 Isot = bytestream2_get_be16u(&s->
g);
597 Psot = bytestream2_get_be32u(&s->
g);
598 TPsot = bytestream2_get_byteu(&s->
g);
601 bytestream2_get_byteu(&s->
g);
639 uint8_t Stlm, ST, SP, tile_tlm, i;
640 bytestream2_get_byte(&s->
g);
641 Stlm = bytestream2_get_byte(&s->
g);
644 ST = (Stlm >> 4) & 0x03;
646 SP = (Stlm >> 6) & 0x01;
647 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
648 for (i = 0; i < tile_tlm; i++) {
653 bytestream2_get_byte(&s->
g);
656 bytestream2_get_be16(&s->
g);
659 bytestream2_get_be32(&s->
g);
663 bytestream2_get_be16(&s->
g);
665 bytestream2_get_be32(&s->
g);
681 for (compno = 0; compno < s->
ncomponents; compno++) {
698 s->
cbps[compno], s->
cdx[compno],
714 return num < 0 ? num : 3 + num;
716 return num < 0 ? num : 6 + num;
718 return num < 0 ? num : 37 + num;
735 int layno,
uint8_t *expn,
int numgbits)
737 int bandno, cblkno,
ret, nb_code_blocks;
745 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
754 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
756 int incl, newpasses, llen;
768 int v = expn[bandno] + numgbits - 1 -
772 "nonzerobits %d invalid\n", v);
784 if (ret >
sizeof(cblk->
data)) {
803 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
808 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
814 "Block length %"PRIu16
" or lengthinc %d is too large\n",
830 int layno, reslevelno, compno, precno, ok_reslevel;
841 for (reslevelno = 0; ok_reslevel; reslevelno++) {
843 for (compno = 0; compno < s->
ncomponents; compno++) {
846 if (reslevelno < codsty->nreslevels) {
854 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
864 for (compno = 0; compno < s->
ncomponents; compno++) {
876 for (y = 0; y < s->
height; y += 256) {
880 for (x = 0; x < s->
width; x += 256) {
881 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
905 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
937 int bpno,
int bandno,
int bpass_csty_symbol,
938 int vert_causal_ctx_csty_symbol)
940 int mask = 3 << (bpno - 1), y0, x,
y;
942 for (y0 = 0; y0 <
height; y0 += 4)
943 for (x = 0; x <
width; x++)
944 for (
y = y0;
y < height &&
y < y0 + 4;
y++) {
948 if (vert_causal_ctx_csty_symbol &&
y == y0 + 3)
952 if (bpass_csty_symbol)
972 phalf = 1 << (bpno - 1);
975 for (y0 = 0; y0 <
height; y0 += 4)
976 for (x = 0; x <
width; x++)
977 for (y = y0; y < height && y < y0 + 4; y++)
990 int seg_symbols,
int vert_causal_ctx_csty_symbol)
992 int mask = 3 << (bpno - 1), y0, x,
y, runlen, dec;
994 for (y0 = 0; y0 <
height; y0 += 4) {
995 for (x = 0; x <
width; x++) {
996 if (y0 + 3 < height &&
1014 for (
y = y0 + runlen;
y < y0 + 4 &&
y <
height;
y++) {
1017 int flags_mask = -1;
1018 if (vert_causal_ctx_csty_symbol &&
y == y0 + 3)
1047 "Segmentation symbol value incorrect\n");
1056 int clnpass_cnt = 0;
1064 memset(t1->
data[
y], 0, width *
sizeof(**t1->
data));
1070 for (
y = 0;
y < height + 2;
y++)
1071 memset(t1->
flags[
y], 0, (width + 2) *
sizeof(**t1->
flags));
1081 bpass_csty_symbol && (clnpass_cnt >= 4),
1082 vert_causal_ctx_csty_symbol);
1086 if (bpass_csty_symbol && clnpass_cnt >= 4)
1092 vert_causal_ctx_csty_symbol);
1093 clnpass_cnt = clnpass_cnt + 1;
1094 if (bpass_csty_symbol && clnpass_cnt >= 4)
1120 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1121 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1122 float *datap = &comp->
f_data[(comp->
coord[0][1] - comp->
coord[0][0]) * (y + j) + x];
1124 for (i = 0; i < w; ++i)
1135 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1136 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1139 for (i = 0; i < w; ++i)
1140 datap[i] = (src[i] * band->
i_stepsize + (1 << 14)) >> 15;
1163 float *srcf[3], i0f, i1f,
i2f;
1165 for (i = 1; i < 3; i++)
1171 for (i = 0; i < 3; i++)
1177 for (i = 0; i < 2; i++)
1182 for (i = 0; i < csize; i++) {
1193 for (i = 0; i < csize; i++) {
1194 i0 = *src[0] + (((
i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
1195 i1 = *src[0] - (((
i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
1197 i2 = *src[0] + (((
i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
1204 for (i = 0; i < csize; i++) {
1205 i1 = *src[0] - (*src[2] + *src[1] >> 2);
1220 int compno, reslevelno, bandno;
1229 for (compno = 0; compno < s->
ncomponents; compno++) {
1237 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1238 int nb_precincts, precno;
1240 int cblkno = 0, bandpos;
1242 bandpos = bandno + (reslevelno > 0);
1244 if (band->
coord[0][0] == band->
coord[0][1] ||
1250 for (precno = 0; precno < nb_precincts; precno++) {
1262 x = cblk->
coord[0][0];
1263 y = cblk->
coord[1][0];
1282 if (s->
cdef[0] < 0) {
1290 for (compno = 0; compno < s->
ncomponents; compno++) {
1293 float *datap = comp->
f_data;
1295 int cbps = s->
cbps[compno];
1304 line = picture->
data[plane] + y / s->
cdy[compno] * picture->
linesize[plane];
1309 dst = line + x / s->
cdx[compno] * pixelsize + compno*!planar;
1312 for (; x < w; x += s->
cdx[compno]) {
1313 int val =
lrintf(*datap) + (1 << (cbps - 1));
1315 val = av_clip(val, 0, (1 << cbps) - 1);
1316 *dst = val << (8 - cbps);
1321 for (; x < w; x += s->
cdx[compno]) {
1322 int val = *i_datap + (1 << (cbps - 1));
1324 val = av_clip(val, 0, (1 << cbps) - 1);
1325 *dst = val << (8 - cbps);
1334 for (compno = 0; compno < s->
ncomponents; compno++) {
1337 float *datap = comp->
f_data;
1340 int cbps = s->
cbps[compno];
1348 linel = (uint16_t *)picture->
data[plane] + y / s->
cdy[compno] * (picture->
linesize[plane] >> 1);
1353 dst = linel + (x / s->
cdx[compno] * pixelsize + compno*!planar);
1355 for (; x < w; x += s-> cdx[compno]) {
1356 int val =
lrintf(*datap) + (1 << (cbps - 1));
1358 val = av_clip(val, 0, (1 << cbps) - 1);
1360 *dst = val << (16 - cbps);
1365 for (; x < w; x += s-> cdx[compno]) {
1366 int val = *i_datap + (1 << (cbps - 1));
1368 val = av_clip(val, 0, (1 << cbps) - 1);
1370 *dst = val << (16 - cbps);
1375 linel += picture->
linesize[plane] >> 1;
1388 for (compno = 0; compno < s->
ncomponents; compno++) {
1419 marker = bytestream2_get_be16u(&s->
g);
1449 len = bytestream2_get_be16(&s->
g);
1460 ret =
get_coc(s, codsty, properties);
1463 ret =
get_cod(s, codsty, properties);
1466 ret =
get_qcc(s, len, qntsty, properties);
1469 ret =
get_qcd(s, len, qntsty, properties);
1472 if (!(ret =
get_sot(s, len))) {
1489 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
1496 "error during processing marker segment %.4"PRIx16
"\n",
1498 return ret ? ret : -1;
1526 uint32_t atom_size, atom, atom_end;
1527 int search_range = 10;
1532 atom_size = bytestream2_get_be32u(&s->
g);
1533 atom = bytestream2_get_be32u(&s->
g);
1544 uint32_t atom2_size, atom2, atom2_end;
1546 atom2_size = bytestream2_get_be32u(&s->
g);
1547 atom2 = bytestream2_get_be32u(&s->
g);
1549 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
1553 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
1554 int method = bytestream2_get_byteu(&s->
g);
1559 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
1560 int i,
size, colour_count, colour_channels, colour_depth[3];
1562 colour_count = bytestream2_get_be16u(&s->
g);
1563 colour_channels = bytestream2_get_byteu(&s->
g);
1565 colour_depth[0] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1566 colour_depth[1] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1567 colour_depth[2] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1568 size = (colour_depth[0] + 7 >> 3) * colour_count +
1569 (colour_depth[1] + 7 >> 3) * colour_count +
1570 (colour_depth[2] + 7 >> 3) * colour_count;
1571 if (colour_count > 256 ||
1572 colour_channels != 3 ||
1573 colour_depth[0] > 16 ||
1574 colour_depth[1] > 16 ||
1575 colour_depth[2] > 16 ||
1576 atom2_size < size) {
1582 for (i = 0; i < colour_count; i++) {
1583 if (colour_depth[0] <= 8) {
1584 r = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[0];
1585 r |= r >> colour_depth[0];
1587 r = bytestream2_get_be16u(&s->
g) >> colour_depth[0] - 8;
1589 if (colour_depth[1] <= 8) {
1590 g = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[1];
1591 r |= r >> colour_depth[1];
1593 g = bytestream2_get_be16u(&s->
g) >> colour_depth[1] - 8;
1595 if (colour_depth[2] <= 8) {
1596 b = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[2];
1597 r |= r >> colour_depth[2];
1599 b = bytestream2_get_be16u(&s->
g) >> colour_depth[2] - 8;
1601 s->
palette[i] = 0xff
u << 24 | r << 16 | g << 8 |
b;
1603 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
1604 int n = bytestream2_get_be16u(&s->
g);
1606 int cn = bytestream2_get_be16(&s->
g);
1607 int av_unused typ = bytestream2_get_be16(&s->
g);
1608 int asoc = bytestream2_get_be16(&s->
g);
1609 if (cn < 4 || asoc < 4)
1614 }
while (atom_end - atom2_end >= 8);
1635 memset(s->
cdef, -1,
sizeof(s->
cdef));
1644 (bytestream2_get_be32u(&s->
g) == 12) &&
1649 "Could not find Jpeg2000 codestream atom.\n");
1686 memcpy(picture->
data[1], s->
palette, 256 *
sizeof(uint32_t));
1701 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1702 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1705 {
"lowres",
"Lower the decoding resolution by a power of two",
1735 .priv_class = &jpeg2000_class,