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
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);
256 if (ncomponents <= 0) {
262 if (ncomponents > 4) {
280 uint8_t x = bytestream2_get_byteu(&s->
g);
281 s->
cbps[i] = (x & 0x7f) + 1;
283 s->
sgnd[i] = !!(x & 0x80);
284 s->
cdx[i] = bytestream2_get_byteu(&s->
g);
285 s->
cdy[i] = bytestream2_get_byteu(&s->
g);
286 if ( !s->
cdx[i] || s->
cdx[i] == 3 || s->
cdx[i] > 4
287 || !s->
cdy[i] || s->
cdy[i] == 3 || s->
cdy[i] > 4) {
291 log2_chroma_wh |= s->
cdy[i] >> 1 << i * 4 | s->
cdx[i] >> 1 << i * 4 + 2;
346 for (i = 0; i < possible_fmts_nb; ++i) {
352 if (i == possible_fmts_nb) {
354 "Unknown pix_fmt, profile: %d, colour_space: %d, "
355 "components: %d, precision: %d, "
356 "cdx[1]: %d, cdy[1]: %d, cdx[2]: %d, cdy[2]: %d\n",
358 ncomponents > 2 ? s->
cdx[1] : 0,
359 ncomponents > 2 ? s->
cdy[1] : 0,
360 ncomponents > 2 ? s->
cdx[2] : 0,
361 ncomponents > 2 ? s->
cdy[2] : 0);
423 byte = bytestream2_get_byte(&s->
g);
444 tmp.
csty = bytestream2_get_byteu(&s->
g);
449 tmp.
nlayers = bytestream2_get_be16u(&s->
g);
450 tmp.
mct = bytestream2_get_byteu(&s->
g);
454 "MCT %"PRIu8
" with too few components (%d)\n",
459 if ((ret =
get_cox(s, &tmp)) < 0)
462 for (compno = 0; compno < s->
ncomponents; compno++)
463 if (!(properties[compno] &
HAD_COC))
464 memcpy(c + compno, &tmp,
sizeof(tmp));
478 compno = bytestream2_get_byteu(&s->
g);
482 "Invalid compno %d. There are %d components in the image.\n",
488 c->
csty = bytestream2_get_byteu(&s->
g);
505 x = bytestream2_get_byteu(&s->
g);
515 for (i = 0; i <
n; i++)
516 q->
expn[i] = bytestream2_get_byteu(&s->
g) >> 3;
520 x = bytestream2_get_be16u(&s->
g);
521 q->
expn[0] = x >> 11;
522 q->
mant[0] = x & 0x7ff;
524 int curexpn =
FFMAX(0, q->
expn[0] - (i - 1) / 3);
525 q->
expn[i] = curexpn;
533 for (i = 0; i <
n; i++) {
534 x = bytestream2_get_be16u(&s->
g);
535 q->
expn[i] = x >> 11;
536 q->
mant[i] = x & 0x7ff;
549 memset(&tmp, 0,
sizeof(tmp));
551 if ((ret =
get_qcx(s, n, &tmp)) < 0)
553 for (compno = 0; compno < s->
ncomponents; compno++)
554 if (!(properties[compno] &
HAD_QCC))
555 memcpy(q + compno, &tmp,
sizeof(tmp));
569 compno = bytestream2_get_byteu(&s->
g);
573 "Invalid compno %d. There are %d components in the image.\n",
579 return get_qcx(s, n - 1, q + compno);
594 Isot = bytestream2_get_be16u(&s->
g);
599 Psot = bytestream2_get_be32u(&s->
g);
600 TPsot = bytestream2_get_byteu(&s->
g);
603 bytestream2_get_byteu(&s->
g);
641 uint8_t Stlm, ST, SP, tile_tlm, i;
642 bytestream2_get_byte(&s->
g);
643 Stlm = bytestream2_get_byte(&s->
g);
646 ST = (Stlm >> 4) & 0x03;
648 SP = (Stlm >> 6) & 0x01;
649 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
650 for (i = 0; i < tile_tlm; i++) {
655 bytestream2_get_byte(&s->
g);
658 bytestream2_get_be16(&s->
g);
661 bytestream2_get_be32(&s->
g);
665 bytestream2_get_be16(&s->
g);
667 bytestream2_get_be32(&s->
g);
683 for (compno = 0; compno < s->
ncomponents; compno++) {
700 s->
cbps[compno], s->
cdx[compno],
716 return num < 0 ? num : 3 + num;
718 return num < 0 ? num : 6 + num;
720 return num < 0 ? num : 37 + num;
737 int layno,
uint8_t *expn,
int numgbits)
739 int bandno, cblkno,
ret, nb_code_blocks;
747 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
756 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
758 int incl, newpasses, llen;
770 int v = expn[bandno] + numgbits - 1 -
774 "nonzerobits %d invalid\n", v);
786 if (ret >
sizeof(cblk->
data)) {
805 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
810 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
816 "Block length %"PRIu16
" or lengthinc %d is too large\n",
832 int layno, reslevelno, compno, precno, ok_reslevel;
843 for (reslevelno = 0; ok_reslevel; reslevelno++) {
845 for (compno = 0; compno < s->
ncomponents; compno++) {
848 if (reslevelno < codsty->nreslevels) {
856 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
866 for (compno = 0; compno < s->
ncomponents; compno++) {
878 for (y = 0; y < s->
height; y += 256) {
882 for (x = 0; x < s->
width; x += 256) {
883 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
907 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
939 int bpno,
int bandno,
int bpass_csty_symbol,
940 int vert_causal_ctx_csty_symbol)
942 int mask = 3 << (bpno - 1), y0, x,
y;
944 for (y0 = 0; y0 <
height; y0 += 4)
945 for (x = 0; x <
width; x++)
946 for (
y = y0;
y < height &&
y < y0 + 4;
y++) {
950 if (vert_causal_ctx_csty_symbol &&
y == y0 + 3)
954 if (bpass_csty_symbol)
974 phalf = 1 << (bpno - 1);
977 for (y0 = 0; y0 <
height; y0 += 4)
978 for (x = 0; x <
width; x++)
979 for (y = y0; y < height && y < y0 + 4; y++)
992 int seg_symbols,
int vert_causal_ctx_csty_symbol)
994 int mask = 3 << (bpno - 1), y0, x,
y, runlen, dec;
996 for (y0 = 0; y0 <
height; y0 += 4) {
997 for (x = 0; x <
width; x++) {
998 if (y0 + 3 < height &&
1016 for (
y = y0 + runlen;
y < y0 + 4 &&
y <
height;
y++) {
1019 int flags_mask = -1;
1020 if (vert_causal_ctx_csty_symbol &&
y == y0 + 3)
1049 "Segmentation symbol value incorrect\n");
1058 int clnpass_cnt = 0;
1066 memset(t1->
data[
y], 0, width *
sizeof(**t1->
data));
1072 for (
y = 0;
y < height + 2;
y++)
1073 memset(t1->
flags[
y], 0, (width + 2) *
sizeof(**t1->
flags));
1083 bpass_csty_symbol && (clnpass_cnt >= 4),
1084 vert_causal_ctx_csty_symbol);
1088 if (bpass_csty_symbol && clnpass_cnt >= 4)
1094 vert_causal_ctx_csty_symbol);
1095 clnpass_cnt = clnpass_cnt + 1;
1096 if (bpass_csty_symbol && clnpass_cnt >= 4)
1122 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1123 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1124 float *datap = &comp->
f_data[(comp->
coord[0][1] - comp->
coord[0][0]) * (y + j) + x];
1126 for (i = 0; i < w; ++i)
1137 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1138 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1141 for (i = 0; i < w; ++i)
1142 datap[i] = (src[i] * band->
i_stepsize + (1 << 14)) >> 15;
1151 for (i = 1; i < 3; i++)
1157 for (i = 0; i < 3; i++)
1163 for (i = 0; i < 2; i++)
1173 int compno, reslevelno, bandno;
1182 for (compno = 0; compno < s->
ncomponents; compno++) {
1190 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1191 int nb_precincts, precno;
1193 int cblkno = 0, bandpos;
1195 bandpos = bandno + (reslevelno > 0);
1197 if (band->
coord[0][0] == band->
coord[0][1] ||
1203 for (precno = 0; precno < nb_precincts; precno++) {
1215 x = cblk->
coord[0][0];
1216 y = cblk->
coord[1][0];
1235 if (s->
cdef[0] < 0) {
1243 for (compno = 0; compno < s->
ncomponents; compno++) {
1246 float *datap = comp->
f_data;
1248 int cbps = s->
cbps[compno];
1257 line = picture->
data[plane] + y / s->
cdy[compno] * picture->
linesize[plane];
1262 dst = line + x / s->
cdx[compno] * pixelsize + compno*!planar;
1265 for (; x < w; x += s->
cdx[compno]) {
1266 int val =
lrintf(*datap) + (1 << (cbps - 1));
1268 val = av_clip(val, 0, (1 << cbps) - 1);
1269 *dst = val << (8 - cbps);
1274 for (; x < w; x += s->
cdx[compno]) {
1275 int val = *i_datap + (1 << (cbps - 1));
1277 val = av_clip(val, 0, (1 << cbps) - 1);
1278 *dst = val << (8 - cbps);
1287 for (compno = 0; compno < s->
ncomponents; compno++) {
1290 float *datap = comp->
f_data;
1293 int cbps = s->
cbps[compno];
1301 linel = (uint16_t *)picture->
data[plane] + y / s->
cdy[compno] * (picture->
linesize[plane] >> 1);
1306 dst = linel + (x / s->
cdx[compno] * pixelsize + compno*!planar);
1308 for (; x < w; x += s-> cdx[compno]) {
1309 int val =
lrintf(*datap) + (1 << (cbps - 1));
1311 val = av_clip(val, 0, (1 << cbps) - 1);
1313 *dst = val << (16 - 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);
1323 *dst = val << (16 - cbps);
1328 linel += picture->
linesize[plane] >> 1;
1341 for (compno = 0; compno < s->
ncomponents; compno++) {
1372 marker = bytestream2_get_be16u(&s->
g);
1402 len = bytestream2_get_be16(&s->
g);
1413 ret =
get_coc(s, codsty, properties);
1416 ret =
get_cod(s, codsty, properties);
1419 ret =
get_qcc(s, len, qntsty, properties);
1422 ret =
get_qcd(s, len, qntsty, properties);
1425 if (!(ret =
get_sot(s, len))) {
1442 "unsupported marker 0x%.4"PRIX16
" at pos 0x%X\n",
1449 "error during processing marker segment %.4"PRIx16
"\n",
1451 return ret ? ret : -1;
1479 uint32_t atom_size, atom, atom_end;
1480 int search_range = 10;
1485 atom_size = bytestream2_get_be32u(&s->
g);
1486 atom = bytestream2_get_be32u(&s->
g);
1497 uint32_t atom2_size, atom2, atom2_end;
1499 atom2_size = bytestream2_get_be32u(&s->
g);
1500 atom2 = bytestream2_get_be32u(&s->
g);
1502 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
1506 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
1507 int method = bytestream2_get_byteu(&s->
g);
1512 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
1513 int i,
size, colour_count, colour_channels, colour_depth[3];
1515 colour_count = bytestream2_get_be16u(&s->
g);
1516 colour_channels = bytestream2_get_byteu(&s->
g);
1518 colour_depth[0] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1519 colour_depth[1] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1520 colour_depth[2] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1521 size = (colour_depth[0] + 7 >> 3) * colour_count +
1522 (colour_depth[1] + 7 >> 3) * colour_count +
1523 (colour_depth[2] + 7 >> 3) * colour_count;
1524 if (colour_count > 256 ||
1525 colour_channels != 3 ||
1526 colour_depth[0] > 16 ||
1527 colour_depth[1] > 16 ||
1528 colour_depth[2] > 16 ||
1529 atom2_size < size) {
1535 for (i = 0; i < colour_count; i++) {
1536 if (colour_depth[0] <= 8) {
1537 r = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[0];
1538 r |= r >> colour_depth[0];
1540 r = bytestream2_get_be16u(&s->
g) >> colour_depth[0] - 8;
1542 if (colour_depth[1] <= 8) {
1543 g = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[1];
1544 r |= r >> colour_depth[1];
1546 g = bytestream2_get_be16u(&s->
g) >> colour_depth[1] - 8;
1548 if (colour_depth[2] <= 8) {
1549 b = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[2];
1550 r |= r >> colour_depth[2];
1552 b = bytestream2_get_be16u(&s->
g) >> colour_depth[2] - 8;
1554 s->
palette[i] = 0xff
u << 24 | r << 16 | g << 8 |
b;
1556 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
1557 int n = bytestream2_get_be16u(&s->
g);
1559 int cn = bytestream2_get_be16(&s->
g);
1560 int av_unused typ = bytestream2_get_be16(&s->
g);
1561 int asoc = bytestream2_get_be16(&s->
g);
1562 if (cn < 4 || asoc < 4)
1567 }
while (atom_end - atom2_end >= 8);
1597 memset(s->
cdef, -1,
sizeof(s->
cdef));
1606 (bytestream2_get_be32u(&s->
g) == 12) &&
1611 "Could not find Jpeg2000 codestream atom.\n");
1648 memcpy(picture->
data[1], s->
palette, 256 *
sizeof(uint32_t));
1663 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1664 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1667 {
"lowres",
"Lower the decoding resolution by a power of two",
1698 .priv_class = &jpeg2000_class,