36 #define JP2_SIG_TYPE 0x6A502020
37 #define JP2_SIG_VALUE 0x0D0A870A
38 #define JP2_CODESTREAM 0x6A703263
100 s->
bit_index = 7 + (bytestream2_get_byte(&s->
g) != 0xFFu);
103 res |= (bytestream2_peek_byte(&s->
g) >> s->
bit_index) & 1;
110 if (bytestream2_get_byte(&s->
g) == 0xff)
120 int sp = -1, curval = 0;
125 while (node && !node->
vis) {
133 curval = stack[sp]->
val;
135 while (curval < threshold && sp >= 0) {
136 if (curval < stack[sp]->val)
137 curval = stack[sp]->
val;
138 while (curval < threshold) {
148 stack[sp]->
val = curval;
165 s->
width = bytestream2_get_be32u(&s->
g);
166 s->
height = bytestream2_get_be32u(&s->
g);
173 ncomponents = bytestream2_get_be16u(&s->
g);
175 if (ncomponents <= 0) {
181 if (ncomponents > 4) {
200 uint8_t x = bytestream2_get_byteu(&s->
g);
201 s->
cbps[i] = (x & 0x7f) + 1;
203 s->
sgnd[i] = !!(x & 0x80);
204 s->
cdx[i] = bytestream2_get_byteu(&s->
g);
205 s->
cdy[i] = bytestream2_get_byteu(&s->
g);
207 if (s->
cdx[i] != 1 || s->
cdy[i] != 1) {
209 "CDxy values %d %d for component %d",
211 if (!s->
cdx[i] || !s->
cdy[i])
317 byte = bytestream2_get_byte(&s->
g);
338 tmp.
csty = bytestream2_get_byteu(&s->
g);
343 tmp.
nlayers = bytestream2_get_be16u(&s->
g);
344 tmp.
mct = bytestream2_get_byteu(&s->
g);
348 "MCT %d with too few components (%d)\n",
353 if ((ret =
get_cox(s, &tmp)) < 0)
356 for (compno = 0; compno < s->
ncomponents; compno++)
357 if (!(properties[compno] &
HAD_COC))
358 memcpy(c + compno, &tmp,
sizeof(tmp));
372 compno = bytestream2_get_byteu(&s->
g);
376 "Invalid compno %d. There are %d components in the image.\n",
382 c->
csty = bytestream2_get_byteu(&s->
g);
399 x = bytestream2_get_byteu(&s->
g);
409 for (i = 0; i < n; i++)
410 q->
expn[i] = bytestream2_get_byteu(&s->
g) >> 3;
414 x = bytestream2_get_be16u(&s->
g);
415 q->
expn[0] = x >> 11;
416 q->
mant[0] = x & 0x7ff;
418 int curexpn =
FFMAX(0, q->
expn[0] - (i - 1) / 3);
419 q->
expn[i] = curexpn;
427 for (i = 0; i < n; i++) {
428 x = bytestream2_get_be16u(&s->
g);
429 q->
expn[i] = x >> 11;
430 q->
mant[i] = x & 0x7ff;
443 if ((ret =
get_qcx(s, n, &tmp)) < 0)
445 for (compno = 0; compno < s->
ncomponents; compno++)
446 if (!(properties[compno] &
HAD_QCC))
447 memcpy(q + compno, &tmp,
sizeof(tmp));
461 compno = bytestream2_get_byteu(&s->
g);
465 "Invalid compno %d. There are %d components in the image.\n",
471 return get_qcx(s, n - 1, q + compno);
485 Isot = bytestream2_get_be16u(&s->
g);
493 Psot = bytestream2_get_be32u(&s->
g);
494 TPsot = bytestream2_get_byteu(&s->
g);
497 bytestream2_get_byteu(&s->
g);
535 uint8_t Stlm, ST, SP, tile_tlm, i;
536 bytestream2_get_byte(&s->
g);
537 Stlm = bytestream2_get_byte(&s->
g);
540 ST = (Stlm >> 4) & 0x03;
542 SP = (Stlm >> 6) & 0x01;
543 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
544 for (i = 0; i < tile_tlm; i++) {
549 bytestream2_get_byte(&s->
g);
552 bytestream2_get_be16(&s->
g);
555 bytestream2_get_be32(&s->
g);
559 bytestream2_get_be16(&s->
g);
561 bytestream2_get_be32(&s->
g);
577 for (compno = 0; compno < s->
ncomponents; compno++) {
594 s->
cbps[compno], s->
cdx[compno],
610 return num < 0 ? num : 3 + num;
612 return num < 0 ? num : 6 + num;
614 return num < 0 ? num : 37 + num;
631 int layno,
uint8_t *expn,
int numgbits)
633 int bandno, cblkno, ret, nb_code_blocks;
641 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
650 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
652 int incl, newpasses, llen;
664 int v = expn[bandno] + numgbits - 1 -
668 "nonzerobits %d invalid\n", v);
680 if (ret >
sizeof(cblk->
data)) {
682 "Block with lengthinc greater than %zu",
699 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
704 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
713 cblk->
data[0] = 0xFF;
714 cblk->
data[1] = 0xFF;
721 "Block length %d > data size %zd\n",
733 int layno, reslevelno, compno, precno, ok_reslevel;
741 for (reslevelno = 0; ok_reslevel; reslevelno++) {
743 for (compno = 0; compno < s->
ncomponents; compno++) {
746 if (reslevelno < codsty->nreslevels) {
754 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
764 for (compno = 0; compno < s->
ncomponents; compno++) {
776 for (y = 0; y < s->
height; y += 256) {
780 for (x = 0; x < s->
width; x += 256) {
781 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
801 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
838 int bpno,
int bandno,
int bpass_csty_symbol,
839 int vert_causal_ctx_csty_symbol)
841 int mask = 3 << (bpno - 1), y0, x, y;
843 for (y0 = 0; y0 <
height; y0 += 4)
844 for (x = 0; x <
width; x++)
845 for (y = y0; y < height && y < y0 + 4; y++) {
849 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
853 if (bpass_csty_symbol)
873 phalf = 1 << (bpno - 1);
876 for (y0 = 0; y0 <
height; y0 += 4)
877 for (x = 0; x <
width; x++)
878 for (y = y0; y < height && y < y0 + 4; y++)
884 t1->
data[y][x] += t1->
data[y][x] < 0 ? -r :
r;
891 int seg_symbols,
int vert_causal_ctx_csty_symbol)
893 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
895 for (y0 = 0; y0 <
height; y0 += 4) {
896 for (x = 0; x <
width; x++) {
897 if (y0 + 3 < height &&
915 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++) {
919 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
948 "Segmentation symbol value incorrect\n");
961 for (y = 0; y <
height; y++)
962 memset(t1->
data[y], 0, width *
sizeof(**t1->
data));
967 for (y = 0; y < height + 2; y++)
968 memset(t1->
flags[y], 0, (width + 2) *
sizeof(**t1->
flags));
978 bpass_csty_symbol && (clnpass_cnt >= 4),
979 vert_causal_ctx_csty_symbol);
983 if (bpass_csty_symbol && clnpass_cnt >= 4)
989 vert_causal_ctx_csty_symbol);
990 clnpass_cnt = clnpass_cnt + 1;
991 if (bpass_csty_symbol && clnpass_cnt >= 4)
1017 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1018 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1019 float *datap = &comp->
f_data[(comp->
coord[0][1] - comp->
coord[0][0]) * (y + j) + x];
1020 int *src = t1->
data[j];
1021 for (i = 0; i < w; ++i)
1032 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1033 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1035 int *src = t1->
data[j];
1036 for (i = 0; i < w; ++i)
1037 datap[i] = (src[i] * band->
i_stepsize + (1 << 15)) >> 16;
1060 float *srcf[3], i0f, i1f,
i2f;
1062 for (i = 0; i < 3; i++)
1068 for (i = 0; i < 2; i++)
1072 for (i = 0; i < csize; i++) {
1083 for (i = 0; i < csize; i++) {
1084 i0 = *src[0] + (((
i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
1085 i1 = *src[0] - (((
i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
1087 i2 = *src[0] + (((
i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
1094 for (i = 0; i < csize; i++) {
1095 i1 = *src[0] - (*src[2] + *src[1] >> 2);
1109 int compno, reslevelno, bandno;
1116 for (compno = 0; compno < s->
ncomponents; compno++) {
1123 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1124 uint16_t nb_precincts, precno;
1126 int cblkno = 0, bandpos;
1127 bandpos = bandno + (reslevelno > 0);
1129 if (band->
coord[0][0] == band->
coord[0][1] ||
1135 for (precno = 0; precno < nb_precincts; precno++) {
1147 x = cblk->
coord[0][0];
1148 y = cblk->
coord[1][0];
1168 for (compno = 0; compno < s->
ncomponents; compno++) {
1171 float *datap = comp->
f_data;
1173 int cbps = s->
cbps[compno];
1185 for (; x < w; x += s->
cdx[compno]) {
1186 int val =
lrintf(*datap) + (1 << (cbps - 1));
1188 val = av_clip(val, 0, (1 << cbps) - 1);
1189 *dst = val << (8 - cbps);
1194 for (; x < w; x += s->
cdx[compno]) {
1195 int val = *i_datap + (1 << (cbps - 1));
1197 val = av_clip(val, 0, (1 << cbps) - 1);
1198 *dst = val << (8 - cbps);
1207 for (compno = 0; compno < s->
ncomponents; compno++) {
1210 float *datap = comp->
f_data;
1213 int cbps = s->
cbps[compno];
1217 linel = (uint16_t *)picture->
data[0] + y * (picture->
linesize[0] >> 1);
1223 for (; x < w; x += s-> cdx[compno]) {
1224 int val =
lrintf(*datap) + (1 << (cbps - 1));
1226 val = av_clip(val, 0, (1 << cbps) - 1);
1228 *dst = val << (16 - cbps);
1233 for (; x < w; x += s-> cdx[compno]) {
1234 int val = *i_datap + (1 << (cbps - 1));
1236 val = av_clip(val, 0, (1 << cbps) - 1);
1238 *dst = val << (16 - cbps);
1243 linel += picture->
linesize[0] >> 1;
1255 for (compno = 0; compno < s->
ncomponents; compno++) {
1283 marker = bytestream2_get_be16u(&s->
g);
1309 len = bytestream2_get_be16u(&s->
g);
1318 ret =
get_coc(s, codsty, properties);
1321 ret =
get_cod(s, codsty, properties);
1324 ret =
get_qcc(s, len, qntsty, properties);
1327 ret =
get_qcd(s, len, qntsty, properties);
1330 if (!(ret =
get_sot(s, len))) {
1346 "unsupported marker 0x%.4X at pos 0x%X\n",
1353 "error during processing marker segment %.4x\n", marker);
1354 return ret ? ret : -1;
1382 uint32_t atom_size, atom;
1383 int found_codestream = 0, search_range = 10;
1385 while(!found_codestream && search_range
1388 atom_size = bytestream2_get_be32u(&s->
g);
1389 atom = bytestream2_get_be32u(&s->
g);
1391 found_codestream = 1;
1400 if (found_codestream)
1424 (bytestream2_get_be32u(&s->
g) == 12) &&
1429 "Could not find Jpeg2000 codestream atom.\n");
1476 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1477 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1480 {
"lowres",
"Lower the decoding resolution by a power of two",
1494 static const AVClass class = {
1510 .priv_class = &
class,