37 #define JP2_SIG_TYPE 0x6A502020
38 #define JP2_SIG_VALUE 0x0D0A870A
39 #define JP2_CODESTREAM 0x6A703263
103 s->
bit_index = 7 + (bytestream2_get_byte(&s->
g) != 0xFF
u);
106 res |= (bytestream2_peek_byte(&s->
g) >> s->
bit_index) & 1;
113 if (bytestream2_get_byte(&s->
g) == 0xff)
123 int sp = -1, curval = 0;
128 while (node && !node->
vis) {
136 curval = stack[
sp]->
val;
138 while (curval < threshold && sp >= 0) {
139 if (curval < stack[sp]->
val)
140 curval = stack[
sp]->
val;
141 while (curval < threshold) {
151 stack[
sp]->
val = curval;
168 s->
width = bytestream2_get_be32u(&s->
g);
169 s->
height = bytestream2_get_be32u(&s->
g);
176 ncomponents = bytestream2_get_be16u(&s->
g);
178 if (ncomponents <= 0) {
184 if (ncomponents > 4) {
202 uint8_t x = bytestream2_get_byteu(&s->
g);
203 s->
cbps[i] = (x & 0x7f) + 1;
205 s->
sgnd[i] = !!(x & 0x80);
206 s->
cdx[i] = bytestream2_get_byteu(&s->
g);
207 s->
cdy[i] = bytestream2_get_byteu(&s->
g);
208 if (s->
cdx[i] != 1 || s->
cdy[i] != 1) {
210 "CDxy values %d %d for component %d",
212 if (!s->
cdx[i] || !s->
cdy[i])
321 byte = bytestream2_get_byte(&s->
g);
342 tmp.
csty = bytestream2_get_byteu(&s->
g);
347 tmp.
nlayers = bytestream2_get_be16u(&s->
g);
348 tmp.
mct = bytestream2_get_byteu(&s->
g);
352 "MCT %d with too few components (%d)\n",
357 if ((ret =
get_cox(s, &tmp)) < 0)
360 for (compno = 0; compno < s->
ncomponents; compno++)
361 if (!(properties[compno] &
HAD_COC))
362 memcpy(c + compno, &tmp,
sizeof(tmp));
376 compno = bytestream2_get_byteu(&s->
g);
380 "Invalid compno %d. There are %d components in the image.\n",
386 c->
csty = bytestream2_get_byteu(&s->
g);
403 x = bytestream2_get_byteu(&s->
g);
413 for (i = 0; i <
n; i++)
414 q->
expn[i] = bytestream2_get_byteu(&s->
g) >> 3;
418 x = bytestream2_get_be16u(&s->
g);
419 q->
expn[0] = x >> 11;
420 q->
mant[0] = x & 0x7ff;
422 int curexpn =
FFMAX(0, q->
expn[0] - (i - 1) / 3);
423 q->
expn[i] = curexpn;
431 for (i = 0; i <
n; i++) {
432 x = bytestream2_get_be16u(&s->
g);
433 q->
expn[i] = x >> 11;
434 q->
mant[i] = x & 0x7ff;
447 if ((ret =
get_qcx(s, n, &tmp)) < 0)
449 for (compno = 0; compno < s->
ncomponents; compno++)
450 if (!(properties[compno] &
HAD_QCC))
451 memcpy(q + compno, &tmp,
sizeof(tmp));
465 compno = bytestream2_get_byteu(&s->
g);
469 "Invalid compno %d. There are %d components in the image.\n",
475 return get_qcx(s, n - 1, q + compno);
490 Isot = bytestream2_get_be16u(&s->
g);
495 Psot = bytestream2_get_be32u(&s->
g);
496 TPsot = bytestream2_get_byteu(&s->
g);
499 bytestream2_get_byteu(&s->
g);
537 uint8_t Stlm, ST, SP, tile_tlm, i;
538 bytestream2_get_byte(&s->
g);
539 Stlm = bytestream2_get_byte(&s->
g);
542 ST = (Stlm >> 4) & 0x03;
544 SP = (Stlm >> 6) & 0x01;
545 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
546 for (i = 0; i < tile_tlm; i++) {
551 bytestream2_get_byte(&s->
g);
554 bytestream2_get_be16(&s->
g);
557 bytestream2_get_be32(&s->
g);
561 bytestream2_get_be16(&s->
g);
563 bytestream2_get_be32(&s->
g);
579 for (compno = 0; compno < s->
ncomponents; compno++) {
596 s->
cbps[compno], s->
cdx[compno],
612 return num < 0 ? num : 3 + num;
614 return num < 0 ? num : 6 + num;
616 return num < 0 ? num : 37 + num;
633 int layno,
uint8_t *expn,
int numgbits)
635 int bandno, cblkno,
ret, nb_code_blocks;
643 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
652 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
654 int incl, newpasses, llen;
666 int v = expn[bandno] + numgbits - 1 -
670 "nonzerobits %d invalid\n", v);
682 if (ret >
sizeof(cblk->
data)) {
684 "Block with lengthinc greater than %zu",
701 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
706 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
724 int layno, reslevelno, compno, precno, ok_reslevel;
735 for (reslevelno = 0; ok_reslevel; reslevelno++) {
737 for (compno = 0; compno < s->
ncomponents; compno++) {
740 if (reslevelno < codsty->nreslevels) {
748 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
758 for (compno = 0; compno < s->
ncomponents; compno++) {
770 for (y = 0; y < s->
height; y += 256) {
774 for (x = 0; x < s->
width; x += 256) {
775 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
795 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
827 int bpno,
int bandno,
int bpass_csty_symbol,
828 int vert_causal_ctx_csty_symbol)
830 int mask = 3 << (bpno - 1), y0, x,
y;
832 for (y0 = 0; y0 <
height; y0 += 4)
833 for (x = 0; x <
width; x++)
834 for (
y = y0;
y < height &&
y < y0 + 4;
y++) {
838 if (vert_causal_ctx_csty_symbol &&
y == y0 + 3)
842 if (bpass_csty_symbol)
862 phalf = 1 << (bpno - 1);
865 for (y0 = 0; y0 <
height; y0 += 4)
866 for (x = 0; x <
width; x++)
867 for (y = y0; y < height && y < y0 + 4; y++)
880 int seg_symbols,
int vert_causal_ctx_csty_symbol)
882 int mask = 3 << (bpno - 1), y0, x,
y, runlen, dec;
884 for (y0 = 0; y0 <
height; y0 += 4) {
885 for (x = 0; x <
width; x++) {
886 if (y0 + 3 < height &&
904 for (
y = y0 + runlen;
y < y0 + 4 &&
y <
height;
y++) {
908 if (vert_causal_ctx_csty_symbol &&
y == y0 + 3)
937 "Segmentation symbol value incorrect\n");
951 memset(t1->
data[
y], 0, width *
sizeof(**t1->
data));
957 for (
y = 0;
y < height + 2;
y++)
958 memset(t1->
flags[
y], 0, (width + 2) *
sizeof(**t1->
flags));
968 bpass_csty_symbol && (clnpass_cnt >= 4),
969 vert_causal_ctx_csty_symbol);
973 if (bpass_csty_symbol && clnpass_cnt >= 4)
979 vert_causal_ctx_csty_symbol);
980 clnpass_cnt = clnpass_cnt + 1;
981 if (bpass_csty_symbol && clnpass_cnt >= 4)
1007 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1008 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1009 float *datap = &comp->
f_data[(comp->
coord[0][1] - comp->
coord[0][0]) * (y + j) + x];
1011 for (i = 0; i < w; ++i)
1022 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1023 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1026 for (i = 0; i < w; ++i)
1027 datap[i] = (src[i] * band->
i_stepsize + (1 << 15)) >> 16;
1050 float *srcf[3], i0f, i1f,
i2f;
1052 for (i = 0; i < 3; i++)
1058 for (i = 0; i < 2; i++)
1063 for (i = 0; i < csize; i++) {
1074 for (i = 0; i < csize; i++) {
1075 i0 = *src[0] + (((
i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
1076 i1 = *src[0] - (((
i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
1078 i2 = *src[0] + (((
i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
1085 for (i = 0; i < csize; i++) {
1086 i1 = *src[0] - (*src[2] + *src[1] >> 2);
1100 int compno, reslevelno, bandno;
1107 for (compno = 0; compno < s->
ncomponents; compno++) {
1115 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1116 int nb_precincts, precno;
1118 int cblkno = 0, bandpos;
1120 bandpos = bandno + (reslevelno > 0);
1122 if (band->
coord[0][0] == band->
coord[0][1] ||
1128 for (precno = 0; precno < nb_precincts; precno++) {
1140 x = cblk->
coord[0][0];
1141 y = cblk->
coord[1][0];
1161 for (compno = 0; compno < s->
ncomponents; compno++) {
1164 float *datap = comp->
f_data;
1166 int cbps = s->
cbps[compno];
1178 for (; x < w; x += s->
cdx[compno]) {
1179 int val =
lrintf(*datap) + (1 << (cbps - 1));
1181 val = av_clip(val, 0, (1 << cbps) - 1);
1182 *dst = val << (8 - cbps);
1187 for (; x < w; x += s->
cdx[compno]) {
1188 int val = *i_datap + (1 << (cbps - 1));
1190 val = av_clip(val, 0, (1 << cbps) - 1);
1191 *dst = val << (8 - cbps);
1200 for (compno = 0; compno < s->
ncomponents; compno++) {
1203 float *datap = comp->
f_data;
1206 int cbps = s->
cbps[compno];
1210 linel = (uint16_t *)picture->
data[0] + y * (picture->
linesize[0] >> 1);
1217 for (; x < w; x += s-> cdx[compno]) {
1218 int val =
lrintf(*datap) + (1 << (cbps - 1));
1220 val = av_clip(val, 0, (1 << cbps) - 1);
1222 *dst = val << (16 - cbps);
1227 for (; x < w; x += s-> cdx[compno]) {
1228 int val = *i_datap + (1 << (cbps - 1));
1230 val = av_clip(val, 0, (1 << cbps) - 1);
1232 *dst = val << (16 - cbps);
1237 linel += picture->
linesize[0] >> 1;
1249 for (compno = 0; compno < s->
ncomponents; compno++) {
1277 marker = bytestream2_get_be16u(&s->
g);
1303 len = bytestream2_get_be16(&s->
g);
1314 ret =
get_coc(s, codsty, properties);
1317 ret =
get_cod(s, codsty, properties);
1320 ret =
get_qcc(s, len, qntsty, properties);
1323 ret =
get_qcd(s, len, qntsty, properties);
1326 if (!(ret =
get_sot(s, len))) {
1343 "unsupported marker 0x%.4X at pos 0x%X\n",
1350 "error during processing marker segment %.4x\n", marker);
1351 return ret ? ret : -1;
1379 uint32_t atom_size, atom;
1380 int found_codestream = 0, search_range = 10;
1382 while (!found_codestream && search_range
1385 atom_size = bytestream2_get_be32u(&s->
g);
1386 atom = bytestream2_get_be32u(&s->
g);
1388 found_codestream = 1;
1397 if (found_codestream)
1421 (bytestream2_get_be32u(&s->
g) == 12) &&
1426 "Could not find Jpeg2000 codestream atom.\n");
1471 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1472 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1475 {
"lowres",
"Lower the decoding resolution by a power of two",
1505 .priv_class = &jpeg2000_class,