36 #define JP2_SIG_TYPE 0x6A502020
37 #define JP2_SIG_VALUE 0x0D0A870A
38 #define JP2_CODESTREAM 0x6A703263
85 s->
bit_index = 7 + (bytestream2_get_byte(&s->
g) != 0xFFu);
88 res |= (bytestream2_peek_byte(&s->
g) >> s->
bit_index) & 1;
95 if (bytestream2_get_byte(&s->
g) == 0xff)
103 for (i = 0; i < comp->y1 - comp->y0; i++)
104 ff_j2k_printv(comp->
data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
107 static void nspaces(FILE *fd,
int n)
109 while(n--) putc(
' ', fd);
114 int tileno, compno, reslevelno, bandno, precno;
115 fprintf(fd,
"XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
116 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
123 fprintf(fd,
"tile %d:\n", tileno);
124 for(compno = 0; compno < s->
ncomponents; compno++){
127 fprintf(fd,
"component %d:\n", compno);
129 fprintf(fd,
"x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
130 comp->x0, comp->x1, comp->y0, comp->y1);
131 for(reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
134 fprintf(fd,
"reslevel %d:\n", reslevelno);
136 fprintf(fd,
"x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
137 reslevel->x0, reslevel->x1, reslevel->y0,
138 reslevel->y1, reslevel->
nbands);
139 for(bandno = 0; bandno < reslevel->
nbands; bandno++){
142 fprintf(fd,
"band %d:\n", bandno);
144 fprintf(fd,
"x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
145 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
153 fprintf(fd,
"prec %d:\n", precno);
155 fprintf(fd,
"xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
169 int sp = -1, curval = 0;
174 while(node && !node->
vis){
182 curval = stack[
sp]->
val;
184 while(curval < threshold && sp >= 0){
185 if (curval < stack[sp]->val)
186 curval = stack[
sp]->
val;
187 while (curval < threshold){
197 stack[
sp]->
val = curval;
212 bytestream2_get_be16u(&s->
g);
213 s->
width = bytestream2_get_be32u(&s->
g);
214 s->
height = bytestream2_get_be32u(&s->
g);
235 uint8_t x = bytestream2_get_byteu(&s->
g);
236 s->
cbps[i] = (x & 0x7f) + 1;
238 s->
sgnd[i] = !!(x & 0x80);
239 s->
cdx[i] = bytestream2_get_byteu(&s->
g);
240 s->
cdy[i] = bytestream2_get_byteu(&s->
g);
314 bytestream2_get_byte(&s->
g);
331 tmp.
csty = bytestream2_get_byteu(&s->
g);
333 if (bytestream2_get_byteu(&s->
g)){
338 tmp.
nlayers = bytestream2_get_be16u(&s->
g);
339 tmp.
mct = bytestream2_get_byteu(&s->
g);
342 for (compno = 0; compno < s->
ncomponents; compno++){
343 if (!(properties[compno] &
HAD_COC))
357 compno = bytestream2_get_byteu(&s->
g);
360 c->
csty = bytestream2_get_byte(&s->
g);
375 x = bytestream2_get_byteu(&s->
g);
384 for (i = 0; i < n; i++)
385 q->
expn[i] = bytestream2_get_byteu(&s->
g) >> 3;
389 x = bytestream2_get_be16u(&s->
g);
390 q->
expn[0] = x >> 11;
391 q->
mant[0] = x & 0x7ff;
392 for (i = 1; i < 32 * 3; i++){
393 int curexpn =
FFMAX(0, q->
expn[0] - (i-1)/3);
394 q->
expn[i] = curexpn;
401 for (i = 0; i < n; i++){
402 x = bytestream2_get_be16u(&s->
g);
403 q->
expn[i] = x >> 11;
404 q->
mant[i] = x & 0x7ff;
418 for (compno = 0; compno < s->
ncomponents; compno++)
419 if (!(properties[compno] &
HAD_QCC))
432 compno = bytestream2_get_byteu(&s->
g);
434 return get_qcx(s, n-1, q+compno);
451 if (!bytestream2_get_byteu(&s->
g)){
458 bytestream2_get_byteu(&s->
g);
472 for (compno = 0; compno < s->
ncomponents; compno++){
498 return num < 0 ? num : 3 + num;
500 return num < 0 ? num : 6 + num;
502 return num < 0 ? num : 37 + num;
517 int layno,
uint8_t *expn,
int numgbits)
519 int bandno, cblkny, cblknx, cblkno, ret;
527 for (bandno = 0; bandno < rlevel->
nbands; bandno++){
536 for (cblkny = prec->
yi0; cblkny < prec->yi1; cblkny++)
537 for(cblknx = prec->
xi0, cblkno = cblkny * band->
cblknx + cblknx; cblknx < prec->xi1; cblknx++, cblkno++, pos++){
539 int incl, newpasses, llen;
566 if (bytestream2_peek_be16(&s->
g) ==
J2K_EPH) {
573 for (bandno = 0; bandno < rlevel->
nbands; bandno++){
576 for (yi = band->
prec[precno].
yi0; yi < band->prec[precno].yi1; yi++){
578 for (xi = band->
prec[precno].
xi0; xi < band->prec[precno].xi1; xi++){
593 int layno, reslevelno, compno, precno, ok_reslevel;
597 for (reslevelno = 0; ok_reslevel; reslevelno++){
599 for (compno = 0; compno < s->
ncomponents; compno++){
602 if (reslevelno < codsty->nreslevels){
607 (reslevelno ? 3*(reslevelno-1)+1 : 0), qntsty->
nguardbits))
619 int vert_causal_ctx_csty_symbol)
621 int mask = 3 << (bpno - 1), y0, x, y;
623 for (y0 = 0; y0 <
height; y0 += 4)
624 for (x = 0; x <
width; x++)
625 for (y = y0; y < height && y < y0+4; y++){
628 int vert_causal_ctx_csty_loc_symbol = vert_causal_ctx_csty_symbol && (x == 3 && y == 3);
630 vert_causal_ctx_csty_loc_symbol))){
632 if (bpass_csty_symbol)
650 phalf = 1 << (bpno - 1);
653 for (y0 = 0; y0 <
height; y0 += 4)
654 for (x = 0; x <
width; x++)
655 for (y = y0; y < height && y < y0+4; y++){
659 t1->
data[y][x] += t1->
data[y][x] < 0 ? -r :
r;
666 int bpno,
int bandno,
int seg_symbols)
668 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
670 for (y0 = 0; y0 <
height; y0 += 4) {
671 for (x = 0; x <
width; x++){
672 if (y0 + 3 < height && !(
687 for (y = y0 + runlen; y < y0 + 4 && y <
height; y++){
718 int passno = cblk->
npasses, pass_t = 2, bpno = cblk->
nonzerobits - 1, y, clnpass_cnt = 0;
722 for (y = 0; y < height+2; y++)
723 memset(t1->
flags[y], 0, (width+2)*
sizeof(
int));
725 for (y = 0; y <
height; y++)
726 memset(t1->
data[y], 0, width*
sizeof(
int));
735 bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
738 if (bpass_csty_symbol && clnpass_cnt >= 4)
743 clnpass_cnt = clnpass_cnt + 1;
744 if (bpass_csty_symbol && clnpass_cnt >= 4)
760 int i, *src[3], i0, i1, i2, csize = 1;
762 for (i = 0; i < 3; i++)
765 for (i = 0; i < 2; i++)
769 for (i = 0; i < csize; i++){
770 i0 = *src[0] + (*src[2] * 46802 >> 16);
771 i1 = *src[0] - (*src[1] * 22553 + *src[2] * 46802 >> 16);
772 i2 = *src[0] + (116130 * *src[1] >> 16);
778 for (i = 0; i < csize; i++){
779 i1 = *src[0] - (*src[2] + *src[1] >> 2);
791 int compno, reslevelno, bandno;
796 for (compno = 0; compno < s->
ncomponents; compno++){
800 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++){
802 for (bandno = 0; bandno < rlevel->
nbands; bandno++){
804 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
806 bandpos = bandno + (reslevelno > 0);
816 for (cblky = 0; cblky < band->
cblkny; cblky++){
817 if (reslevelno == 0 || bandno == 1)
825 for (cblkx = 0; cblkx < band->
cblknx; cblkx++, cblkno++){
827 decode_cblk(s, codsty, &t1, band->
cblk + cblkno, xx1 - xx0, yy1 - yy0, bandpos);
829 for (y = yy0; y < yy1; y+=s->
cdy[compno]){
830 int *ptr = t1.
data[y-yy0];
831 for (x = xx0; x < xx1; x+=s->
cdx[compno]){
832 comp->
data[(comp->
coord[0][1] - comp->
coord[0][0]) * y + x] = *ptr++ >> 1;
836 for (y = yy0; y < yy1; y+=s->
cdy[compno]){
837 int *ptr = t1.
data[y-yy0];
838 for (x = xx0; x < xx1; x+=s->
cdx[compno]){
839 int tmp = ((int64_t)*ptr++) * ((int64_t)band->
stepsize) >> 13, tmp2;
840 tmp2 =
FFABS(tmp>>1) + (tmp&1);
841 comp->
data[(comp->
coord[0][1] - comp->
coord[0][0]) * y + x] = tmp < 0 ? -tmp2 : tmp2;
854 src[compno] = comp->
data;
860 for (compno = 0; compno < s->
ncomponents; compno++){
870 *src[compno] += 1 << (s->
cbps[compno]-1);
871 if (*src[compno] < 0)
873 else if (*src[compno] >= (1 << s->
cbps[compno]))
874 *src[compno] = (1 << s->
cbps[compno]) - 1;
875 *dst = *src[compno]++;
882 for (compno = 0; compno < s->
ncomponents; compno++) {
889 dst = (uint16_t *)(line + (x * s->
ncomponents + compno) * 2);
893 val = *src[compno]++ << (16 - s->
cbps[compno]);
895 val = av_clip(val, 0, (1 << 16) - 1);
910 for (compno = 0; compno < s->
ncomponents; compno++){
928 int oldpos, marker,
len, ret = 0;
935 marker = bytestream2_get_be16u(&s->
g);
956 len = bytestream2_get_be16u(&s->
g);
962 ret =
get_coc(s, codsty, properties);
965 ret =
get_cod(s, codsty, properties);
968 ret =
get_qcc(s, len, qntsty, properties);
971 ret =
get_qcd(s, len, qntsty, properties);
991 return ret ? ret : -1;
999 uint32_t atom_size, atom;
1000 int found_codestream = 0, search_range = 10;
1003 atom_size = bytestream2_get_be32u(&s->
g);
1004 atom = bytestream2_get_be32u(&s->
g);
1006 found_codestream = 1;
1015 if (found_codestream)
1021 void *
data,
int *got_frame,
1038 (bytestream2_get_be32u(&s->
g) == 12) &&
1050 if (bytestream2_get_be16u(&s->
g) !=
J2K_SOC){