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 if ((ret =
get_qcx(s, n, &tmp)) < 0)
546 for (compno = 0; compno < s->
ncomponents; compno++)
547 if (!(properties[compno] &
HAD_QCC))
548 memcpy(q + compno, &tmp,
sizeof(tmp));
562 compno = bytestream2_get_byteu(&s->
g);
566 "Invalid compno %d. There are %d components in the image.\n",
572 return get_qcx(s, n - 1, q + compno);
587 Isot = bytestream2_get_be16u(&s->
g);
592 Psot = bytestream2_get_be32u(&s->
g);
593 TPsot = bytestream2_get_byteu(&s->
g);
596 bytestream2_get_byteu(&s->
g);
634 uint8_t Stlm, ST, SP, tile_tlm, i;
635 bytestream2_get_byte(&s->
g);
636 Stlm = bytestream2_get_byte(&s->
g);
639 ST = (Stlm >> 4) & 0x03;
641 SP = (Stlm >> 6) & 0x01;
642 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
643 for (i = 0; i < tile_tlm; i++) {
648 bytestream2_get_byte(&s->
g);
651 bytestream2_get_be16(&s->
g);
654 bytestream2_get_be32(&s->
g);
658 bytestream2_get_be16(&s->
g);
660 bytestream2_get_be32(&s->
g);
676 for (compno = 0; compno < s->
ncomponents; compno++) {
693 s->
cbps[compno], s->
cdx[compno],
709 return num < 0 ? num : 3 + num;
711 return num < 0 ? num : 6 + num;
713 return num < 0 ? num : 37 + num;
730 int layno,
uint8_t *expn,
int numgbits)
732 int bandno, cblkno,
ret, nb_code_blocks;
740 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
749 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
751 int incl, newpasses, llen;
763 int v = expn[bandno] + numgbits - 1 -
767 "nonzerobits %d invalid\n", v);
779 if (ret >
sizeof(cblk->
data)) {
781 "Block with lengthinc greater than %zu",
798 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
803 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
809 "Block length %d or lengthinc %d is too large\n",
825 int layno, reslevelno, compno, precno, ok_reslevel;
836 for (reslevelno = 0; ok_reslevel; reslevelno++) {
838 for (compno = 0; compno < s->
ncomponents; compno++) {
841 if (reslevelno < codsty->nreslevels) {
849 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
859 for (compno = 0; compno < s->
ncomponents; compno++) {
871 for (y = 0; y < s->
height; y += 256) {
875 for (x = 0; x < s->
width; x += 256) {
876 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
900 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
932 int bpno,
int bandno,
int bpass_csty_symbol,
933 int vert_causal_ctx_csty_symbol)
935 int mask = 3 << (bpno - 1), y0, x,
y;
937 for (y0 = 0; y0 <
height; y0 += 4)
938 for (x = 0; x <
width; x++)
939 for (
y = y0;
y < height &&
y < y0 + 4;
y++) {
943 if (vert_causal_ctx_csty_symbol &&
y == y0 + 3)
947 if (bpass_csty_symbol)
967 phalf = 1 << (bpno - 1);
970 for (y0 = 0; y0 <
height; y0 += 4)
971 for (x = 0; x <
width; x++)
972 for (y = y0; y < height && y < y0 + 4; y++)
985 int seg_symbols,
int vert_causal_ctx_csty_symbol)
987 int mask = 3 << (bpno - 1), y0, x,
y, runlen, dec;
989 for (y0 = 0; y0 <
height; y0 += 4) {
990 for (x = 0; x <
width; x++) {
991 if (y0 + 3 < height &&
1009 for (
y = y0 + runlen;
y < y0 + 4 &&
y <
height;
y++) {
1012 int flags_mask = -1;
1013 if (vert_causal_ctx_csty_symbol &&
y == y0 + 3)
1042 "Segmentation symbol value incorrect\n");
1051 int clnpass_cnt = 0;
1059 memset(t1->
data[
y], 0, width *
sizeof(**t1->
data));
1065 for (
y = 0;
y < height + 2;
y++)
1066 memset(t1->
flags[
y], 0, (width + 2) *
sizeof(**t1->
flags));
1076 bpass_csty_symbol && (clnpass_cnt >= 4),
1077 vert_causal_ctx_csty_symbol);
1081 if (bpass_csty_symbol && clnpass_cnt >= 4)
1087 vert_causal_ctx_csty_symbol);
1088 clnpass_cnt = clnpass_cnt + 1;
1089 if (bpass_csty_symbol && clnpass_cnt >= 4)
1115 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1116 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1117 float *datap = &comp->
f_data[(comp->
coord[0][1] - comp->
coord[0][0]) * (y + j) + x];
1119 for (i = 0; i < w; ++i)
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) {
1134 for (i = 0; i < w; ++i)
1135 datap[i] = (src[i] * band->
i_stepsize + (1 << 14)) >> 15;
1158 float *srcf[3], i0f, i1f,
i2f;
1160 for (i = 1; i < 3; i++)
1166 for (i = 0; i < 3; i++)
1172 for (i = 0; i < 2; i++)
1177 for (i = 0; i < csize; i++) {
1188 for (i = 0; i < csize; i++) {
1189 i0 = *src[0] + (((
i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
1190 i1 = *src[0] - (((
i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
1192 i2 = *src[0] + (((
i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
1199 for (i = 0; i < csize; i++) {
1200 i1 = *src[0] - (*src[2] + *src[1] >> 2);
1214 int compno, reslevelno, bandno;
1221 for (compno = 0; compno < s->
ncomponents; compno++) {
1229 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1230 int nb_precincts, precno;
1232 int cblkno = 0, bandpos;
1234 bandpos = bandno + (reslevelno > 0);
1236 if (band->
coord[0][0] == band->
coord[0][1] ||
1242 for (precno = 0; precno < nb_precincts; precno++) {
1254 x = cblk->
coord[0][0];
1255 y = cblk->
coord[1][0];
1274 if (s->
cdef[0] < 0) {
1282 for (compno = 0; compno < s->
ncomponents; compno++) {
1285 float *datap = comp->
f_data;
1287 int cbps = s->
cbps[compno];
1289 int planar = !!picture->
data[2];
1298 line = picture->
data[plane] + y / s->
cdy[compno] * picture->
linesize[plane];
1303 dst = line + x / s->
cdx[compno] * pixelsize + compno*!planar;
1306 for (; x < w; x += s->
cdx[compno]) {
1307 int val =
lrintf(*datap) + (1 << (cbps - 1));
1309 val = av_clip(val, 0, (1 << cbps) - 1);
1310 *dst = val << (8 - cbps);
1315 for (; x < w; x += s->
cdx[compno]) {
1316 int val = *i_datap + (1 << (cbps - 1));
1318 val = av_clip(val, 0, (1 << cbps) - 1);
1319 *dst = val << (8 - cbps);
1328 for (compno = 0; compno < s->
ncomponents; compno++) {
1331 float *datap = comp->
f_data;
1334 int cbps = s->
cbps[compno];
1336 int planar = !!picture->
data[2];
1344 linel = (uint16_t *)picture->
data[plane] + y / s->
cdy[compno] * (picture->
linesize[plane] >> 1);
1349 dst = linel + (x / s->
cdx[compno] * pixelsize + compno*!planar);
1351 for (; x < w; x += s-> cdx[compno]) {
1352 int val =
lrintf(*datap) + (1 << (cbps - 1));
1354 val = av_clip(val, 0, (1 << cbps) - 1);
1356 *dst = val << (16 - cbps);
1361 for (; x < w; x += s-> cdx[compno]) {
1362 int val = *i_datap + (1 << (cbps - 1));
1364 val = av_clip(val, 0, (1 << cbps) - 1);
1366 *dst = val << (16 - cbps);
1371 linel += picture->
linesize[plane] >> 1;
1384 for (compno = 0; compno < s->
ncomponents; compno++) {
1415 marker = bytestream2_get_be16u(&s->
g);
1445 len = bytestream2_get_be16(&s->
g);
1456 ret =
get_coc(s, codsty, properties);
1459 ret =
get_cod(s, codsty, properties);
1462 ret =
get_qcc(s, len, qntsty, properties);
1465 ret =
get_qcd(s, len, qntsty, properties);
1468 if (!(ret =
get_sot(s, len))) {
1485 "unsupported marker 0x%.4X at pos 0x%X\n",
1492 "error during processing marker segment %.4x\n", marker);
1493 return ret ? ret : -1;
1521 uint32_t atom_size, atom, atom_end;
1522 int search_range = 10;
1527 atom_size = bytestream2_get_be32u(&s->
g);
1528 atom = bytestream2_get_be32u(&s->
g);
1539 uint32_t atom2_size, atom2, atom2_end;
1541 atom2_size = bytestream2_get_be32u(&s->
g);
1542 atom2 = bytestream2_get_be32u(&s->
g);
1544 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
1548 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
1549 int method = bytestream2_get_byteu(&s->
g);
1554 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
1555 int i,
size, colour_count, colour_channels, colour_depth[3];
1557 colour_count = bytestream2_get_be16u(&s->
g);
1558 colour_channels = bytestream2_get_byteu(&s->
g);
1560 colour_depth[0] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1561 colour_depth[1] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1562 colour_depth[2] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1563 size = (colour_depth[0] + 7 >> 3) * colour_count +
1564 (colour_depth[1] + 7 >> 3) * colour_count +
1565 (colour_depth[2] + 7 >> 3) * colour_count;
1566 if (colour_count > 256 ||
1567 colour_channels != 3 ||
1568 colour_depth[0] > 16 ||
1569 colour_depth[1] > 16 ||
1570 colour_depth[2] > 16 ||
1571 atom2_size < size) {
1577 for (i = 0; i < colour_count; i++) {
1578 if (colour_depth[0] <= 8) {
1579 r = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[0];
1580 r |= r >> colour_depth[0];
1582 r = bytestream2_get_be16u(&s->
g) >> colour_depth[0] - 8;
1584 if (colour_depth[1] <= 8) {
1585 g = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[1];
1586 r |= r >> colour_depth[1];
1588 g = bytestream2_get_be16u(&s->
g) >> colour_depth[1] - 8;
1590 if (colour_depth[2] <= 8) {
1591 b = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[2];
1592 r |= r >> colour_depth[2];
1594 b = bytestream2_get_be16u(&s->
g) >> colour_depth[2] - 8;
1596 s->
palette[i] = 0xff
u << 24 | r << 16 | g << 8 |
b;
1598 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
1599 int n = bytestream2_get_be16u(&s->
g);
1601 int cn = bytestream2_get_be16(&s->
g);
1602 int av_unused typ = bytestream2_get_be16(&s->
g);
1603 int asoc = bytestream2_get_be16(&s->
g);
1604 if (cn < 4 || asoc < 4)
1609 }
while (atom_end - atom2_end >= 8);
1630 memset(s->
cdef, -1,
sizeof(s->
cdef));
1639 (bytestream2_get_be32u(&s->
g) == 12) &&
1644 "Could not find Jpeg2000 codestream atom.\n");
1681 memcpy(picture->
data[1], s->
palette, 256 *
sizeof(uint32_t));
1696 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1697 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1700 {
"lowres",
"Lower the decoding resolution by a power of two",
1730 .priv_class = &jpeg2000_class,