Skip to content

Commit

Permalink
lj92: Use zero bits for the diff value for SSSS=16
Browse files Browse the repository at this point in the history
See A.5 and H.1.2.2 in the JPEG specification:
https://www.w3.org/Graphics/JPEG/itu-t81.pdf
  • Loading branch information
SimonSegerblomRex committed Mar 29, 2021
1 parent c283c0d commit 170ec64
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/mlv/liblj92/lj92.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ static int decode(ljp* self) {
}

static int receive(ljp* self,int ssss) {
if (ssss == 16) {
return 1 << 15;
}
int i = 0;
int v = 0;
while (i != ssss) {
Expand Down Expand Up @@ -366,24 +369,29 @@ inline static int nextdiff(ljp* self) {
cnt -= usedbits;
int keepbitsmask = (1 << cnt)-1;
b &= keepbitsmask;
while (cnt < t) {
next = *(u16*)&self->data[ix];
int one = next&0xFF;
int two = next>>8;
b = (b<<16)|(one<<8)|two;
cnt += 16;
ix += 2;
if (one==0xFF) {
b >>= 8;
cnt -= 8;
} else if (two==0xFF) ix++;
}
cnt -= t;
int diff = b >> cnt;
int vt = 1<<(t-1);
if (diff < vt) {
vt = (-1 << t) + 1;
diff += vt;
int diff;
if (t == 16) {

This comment has been minimized.

Copy link
@cgohlke

cgohlke Jan 14, 2023

If t == 16, the indexing self->sssshist[t]++; a couple of lines earlier is out-of-bounds:

int sssshist[16];

int t = ssssused>>8;
self->sssshist[t]++;
cnt -= usedbits;
int keepbitsmask = (1 << cnt)-1;
b &= keepbitsmask;
int diff;
if (t == 16) {

This comment has been minimized.

Copy link
@esoha-nvidia

esoha-nvidia Jan 17, 2023

Contributor

Good catch! File a bug!

diff = 1 << 15;
} else {
while (cnt < t) {
next = *(u16*)&self->data[ix];
int one = next&0xFF;
int two = next>>8;
b = (b<<16)|(one<<8)|two;
cnt += 16;
ix += 2;
if (one==0xFF) {
b >>= 8;
cnt -= 8;
} else if (two==0xFF) ix++;
}
cnt -= t;
diff = b >> cnt;
int vt = 1<<(t-1);
if (diff < vt) {
vt = (-1 << t) + 1;
diff += vt;
}
}
keepbitsmask = (1 << cnt)-1;
self->b = b & keepbitsmask;
Expand Down Expand Up @@ -1068,7 +1076,6 @@ int writeBody(lje* self) {
int huffcode = self->huffsym[ssss];
int huffenc = self->huffenc[huffcode];
int huffbits = self->huffbits[huffcode];
bitcount += huffbits + ssss;

int vt = ssss>0?(1<<(ssss-1)):0;
//printf("%d %d %d %d\n",rows[1][col],Px,diff,Px+diff);
Expand Down Expand Up @@ -1099,7 +1106,10 @@ int writeBody(lje* self) {
}
}
// Write the rest of the bits for the value

if (ssss == 16) {
// Diff values (always -32678) for SSSS=16 are encoded with 0 bits

This comment has been minimized.

Copy link
@esoha-nvidia

esoha-nvidia Jan 13, 2023

Contributor

This ought to be 32768 and not -32768, right?

This comment has been minimized.

Copy link
@SimonSegerblomRex

SimonSegerblomRex Jan 13, 2023

Author Contributor

Hmm, it's been a while since I worked with this. Probably I was using int16 for storing the difference values, but the actual value should be 32678.

ssss = 0;
}
while (ssss>0) {
int usebits = ssss>nextbits?nextbits:ssss;
// Add top usebits from huffval to next usebits of nextbits
Expand Down Expand Up @@ -1207,4 +1217,3 @@ int lj92_encode(uint16_t* image, int width, int height, int bitdepth,
return ret;
}


0 comments on commit 170ec64

Please sign in to comment.