34 #include "libavutil/avassert.h"
35 #include "libavutil/common.h"
37 #define JP2_SIG_TYPE 0x6A502020
38 #define JP2_SIG_VALUE 0x0D0A870A
39 #define JP2_CODESTREAM 0x6A703263
86 s->
bit_index = 7 + (bytestream2_get_byte(&s->
g) != 0xFF
u);
89 res |= (bytestream2_peek_byte(&s->
g) >> s->
bit_index) & 1;
96 if (bytestream2_get_byte(&s->
g) == 0xff)
104 for (i = 0; i < comp->y1 - comp->y0; i++)
105 ff_j2k_printv(comp->
data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
108 static void nspaces(FILE *fd,
int n)
110 while(n--) putc(
' ', fd);
115 int tileno, compno, reslevelno, bandno, precno;
116 fprintf(fd,
"XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
117 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
124 fprintf(fd,
"tile %d:\n", tileno);
125 for(compno = 0; compno < s->
ncomponents; compno++){
128 fprintf(fd,
"component %d:\n", compno);
130 fprintf(fd,
"x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
131 comp->x0, comp->x1, comp->y0, comp->y1);
132 for(reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
135 fprintf(fd,
"reslevel %d:\n", reslevelno);
137 fprintf(fd,
"x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
138 reslevel->x0, reslevel->x1, reslevel->y0,
139 reslevel->y1, reslevel->
nbands);
140 for(bandno = 0; bandno < reslevel->
nbands; bandno++){
143 fprintf(fd,
"band %d:\n", bandno);
145 fprintf(fd,
"x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
146 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
154 fprintf(fd,
"prec %d:\n", precno);
156 fprintf(fd,
"xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
170 int sp = -1, curval = 0;
175 while(node && !node->
vis){
183 curval = stack[
sp]->
val;
185 while(curval < threshold && sp >= 0){
186 if (curval < stack[sp]->val)
187 curval = stack[
sp]->
val;
188 while (curval < threshold){
198 stack[
sp]->
val = curval;
213 bytestream2_get_be16u(&s->
g);
214 s->
width = bytestream2_get_be32u(&s->
g);
215 s->
height = bytestream2_get_be32u(&s->
g);
236 uint8_t x = bytestream2_get_byteu(&s->
g);
237 s->
cbps[i] = (x & 0x7f) + 1;
239 s->
sgnd[i] = !!(x & 0x80);
240 s->
cdx[i] = bytestream2_get_byteu(&s->
g);
241 s->
cdy[i] = bytestream2_get_byteu(&s->
g);
319 bytestream2_get_byte(&s->
g);
336 tmp.
csty = bytestream2_get_byteu(&s->
g);
338 if (bytestream2_get_byteu(&s->
g)){
343 tmp.
nlayers = bytestream2_get_be16u(&s->
g);
344 tmp.
mct = bytestream2_get_byteu(&s->
g);
347 for (compno = 0; compno < s->
ncomponents; compno++){
348 if (!(properties[compno] &
HAD_COC))
362 compno = bytestream2_get_byteu(&s->
g);
365 c->
csty = bytestream2_get_byte(&s->
g);
380 x = bytestream2_get_byteu(&s->
g);
389 for (i = 0; i < n; i++)
390 q->
expn[i] = bytestream2_get_byteu(&s->
g) >> 3;
394 x = bytestream2_get_be16u(&s->
g);
395 q->
expn[0] = x >> 11;
396 q->
mant[0] = x & 0x7ff;
397 for (i = 1; i < 32 * 3; i++){
398 int curexpn =
FFMAX(0, q->
expn[0] - (i-1)/3);
399 q->
expn[i] = curexpn;
406 for (i = 0; i < n; i++){
407 x = bytestream2_get_be16u(&s->
g);
408 q->
expn[i] = x >> 11;
409 q->
mant[i] = x & 0x7ff;
423 for (compno = 0; compno < s->
ncomponents; compno++)
424 if (!(properties[compno] &
HAD_QCC))
437 compno = bytestream2_get_byteu(&s->
g);
439 return get_qcx(s, n-1, q+compno);
456 if (!bytestream2_get_byteu(&s->
g)){
463 bytestream2_get_byteu(&s->
g);
477 for (compno = 0; compno < s->
ncomponents; compno++){
503 return num < 0 ? num : 3 + num;
505 return num < 0 ? num : 6 + num;
507 return num < 0 ? num : 37 + num;
522 int layno,
uint8_t *expn,
int numgbits)
524 int bandno, cblkny, cblknx, cblkno, ret;
532 for (bandno = 0; bandno < rlevel->
nbands; bandno++){
541 for (cblkny = prec->
yi0; cblkny < prec->yi1; cblkny++)
542 for(cblknx = prec->
xi0, cblkno = cblkny * band->
cblknx + cblknx; cblknx < prec->xi1; cblknx++, cblkno++, pos++){
544 int incl, newpasses, llen;
571 if (bytestream2_peek_be16(&s->
g) ==
J2K_EPH) {
578 for (bandno = 0; bandno < rlevel->
nbands; bandno++){
581 for (yi = band->
prec[precno].
yi0; yi < band->prec[precno].yi1; yi++){
583 for (xi = band->
prec[precno].
xi0; xi < band->prec[precno].xi1; xi++){
598 int layno, reslevelno, compno, precno, ok_reslevel;
602 for (reslevelno = 0; ok_reslevel; reslevelno++){
604 for (compno = 0; compno < s->
ncomponents; compno++){
607 if (reslevelno < codsty->nreslevels){
612 (reslevelno ? 3*(reslevelno-1)+1 : 0), qntsty->
nguardbits))
624 int vert_causal_ctx_csty_symbol)
626 int mask = 3 << (bpno - 1), y0, x,
y;
628 for (y0 = 0; y0 <
height; y0 += 4)
629 for (x = 0; x <
width; x++)
630 for (
y = y0;
y < height &&
y < y0+4;
y++){
633 int vert_causal_ctx_csty_loc_symbol = vert_causal_ctx_csty_symbol && (x == 3 &&
y == 3);
635 vert_causal_ctx_csty_loc_symbol))){
637 if (bpass_csty_symbol)
655 phalf = 1 << (bpno - 1);
658 for (y0 = 0; y0 <
height; y0 += 4)
659 for (x = 0; x <
width; x++)
660 for (y = y0; y < height && y < y0+4; y++){
671 int bpno,
int bandno,
int seg_symbols)
673 int mask = 3 << (bpno - 1), y0, x,
y, runlen, dec;
675 for (y0 = 0; y0 <
height; y0 += 4) {
676 for (x = 0; x <
width; x++){
677 if (y0 + 3 < height && !(
692 for (
y = y0 + runlen;
y < y0 + 4 &&
y <
height;
y++){
730 for (
y = 0;
y < height+2;
y++)
731 memset(t1->
flags[
y], 0, (width+2)*
sizeof(
int));
734 memset(t1->
data[
y], 0, width*
sizeof(
int));
743 bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
746 if (bpass_csty_symbol && clnpass_cnt >= 4)
751 clnpass_cnt = clnpass_cnt + 1;
752 if (bpass_csty_symbol && clnpass_cnt >= 4)
768 int i, *src[3], i0, i1, i2, csize = 1;
770 for (i = 0; i < 3; i++)
773 for (i = 0; i < 2; i++)
777 for (i = 0; i < csize; i++){
778 i0 = *src[0] + (*src[2] * 46802 >> 16);
779 i1 = *src[0] - (*src[1] * 22553 + *src[2] * 46802 >> 16);
780 i2 = *src[0] + (116130 * *src[1] >> 16);
786 for (i = 0; i < csize; i++){
787 i1 = *src[0] - (*src[2] + *src[1] >> 2);
799 int compno, reslevelno, bandno;
804 for (compno = 0; compno < s->
ncomponents; compno++){
808 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++){
810 for (bandno = 0; bandno < rlevel->
nbands; bandno++){
812 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
814 bandpos = bandno + (reslevelno > 0);
824 for (cblky = 0; cblky < band->
cblkny; cblky++){
825 if (reslevelno == 0 || bandno == 1)
833 for (cblkx = 0; cblkx < band->
cblknx; cblkx++, cblkno++){
835 decode_cblk(s, codsty, &t1, band->
cblk + cblkno, xx1 - xx0, yy1 - yy0, bandpos);
837 for (y = yy0; y < yy1; y+=s->
cdy[compno]){
838 int *ptr = t1.
data[y-yy0];
839 for (x = xx0; x < xx1; x+=s->
cdx[compno]){
840 comp->
data[(comp->
coord[0][1] - comp->
coord[0][0]) * y + x] = *ptr++ >> 1;
844 for (y = yy0; y < yy1; y+=s->
cdy[compno]){
845 int *ptr = t1.
data[y-yy0];
846 for (x = xx0; x < xx1; x+=s->
cdx[compno]){
847 int tmp = ((int64_t)*ptr++) * ((int64_t)band->
stepsize) >> 13, tmp2;
848 tmp2 =
FFABS(tmp>>1) + (tmp&1);
849 comp->
data[(comp->
coord[0][1] - comp->
coord[0][0]) * y + x] = tmp < 0 ? -tmp2 : tmp2;
862 src[compno] = comp->
data;
868 for (compno = 0; compno < s->
ncomponents; compno++){
878 *src[compno] += 1 << (s->
cbps[compno]-1);
879 if (*src[compno] < 0)
881 else if (*src[compno] >= (1 << s->
cbps[compno]))
882 *src[compno] = (1 << s->
cbps[compno]) - 1;
883 *dst = *src[compno]++;
890 for (compno = 0; compno < s->
ncomponents; compno++) {
897 dst = (uint16_t *)(line + (x * s->
ncomponents + compno) * 2);
901 val = *src[compno]++ << (16 - s->
cbps[compno]);
903 val = av_clip(val, 0, (1 << 16) - 1);
918 for (compno = 0; compno < s->
ncomponents; compno++){
936 int oldpos, marker,
len, ret = 0;
943 marker = bytestream2_get_be16u(&s->
g);
964 len = bytestream2_get_be16u(&s->
g);
970 ret =
get_coc(s, codsty, properties);
973 ret =
get_cod(s, codsty, properties);
976 ret =
get_qcc(s, len, qntsty, properties);
979 ret =
get_qcd(s, len, qntsty, properties);
999 return ret ? ret : -1;
1007 uint32_t atom_size, atom;
1008 int found_codestream = 0, search_range = 10;
1011 atom_size = bytestream2_get_be32u(&s->
g);
1012 atom = bytestream2_get_be32u(&s->
g);
1014 found_codestream = 1;
1023 if (found_codestream)
1029 void *
data,
int *got_frame,
1046 (bytestream2_get_be32u(&s->
g) == 12) &&
1058 if (bytestream2_get_be16u(&s->
g) !=
J2K_SOC){