Skip to content

Commit 630217f

Browse files
committed
Bug fixes. There are other bugs for 32 lossless.
1 parent abd993d commit 630217f

File tree

5 files changed

+42
-31
lines changed

5 files changed

+42
-31
lines changed

src/core/codestream/ojph_bitbuffer_write.h

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,33 +109,25 @@ namespace ojph {
109109
}
110110
}
111111

112+
//////////////////////////////////////////////////////////////////////////
113+
static inline
114+
void bb_put_zeros(bit_write_buf *bbp, int num_zeros,
115+
mem_elastic_allocator *elastic,
116+
coded_lists*& cur_coded_list, ui32& ph_bytes)
117+
{
118+
for (int i = num_zeros; i > 0; --i)
119+
bb_put_bit(bbp, 0, elastic, cur_coded_list, ph_bytes);
120+
}
121+
112122
//////////////////////////////////////////////////////////////////////////
113123
static inline
114124
void bb_put_bits(bit_write_buf *bbp, ui32 data, int num_bits,
115125
mem_elastic_allocator *elastic,
116126
coded_lists*& cur_coded_list, ui32& ph_bytes)
117127
{
118-
// assert(num_bits <= 32);
119-
for (int i = num_bits - 1; i >= 0; --i)
128+
assert(num_bits <= 32);
129+
for (int i = num_bits - 1; i >= 0; --i)
120130
bb_put_bit(bbp, data >> i, elastic, cur_coded_list, ph_bytes);
121-
// while (num_bits) {
122-
// int tx_bits = num_bits < bbp->avail_bits ? num_bits : bbp->avail_bits;
123-
// bbp->tmp |= (data >> (num_bits - tx_bits)) & ((1 << tx_bits) - 1);
124-
// bbp->avail_bits -= tx_bits;
125-
// if (bbp->avail_bits <= 0)
126-
// {
127-
// bbp->avail_bits = 8 - (bbp->tmp != 0xFF ? 0 : 1);
128-
// bbp->buf[bbp->buf_size - bbp->avail_size] = (ui8)(bbp->tmp & 0xFF);
129-
// bbp->tmp = 0;
130-
// --bbp->avail_size;
131-
// if (bbp->avail_size == 0)
132-
// {
133-
// bb_expand_buf(bbp, elastic, cur_coded_list->next_list);
134-
// cur_coded_list = cur_coded_list->next_list;
135-
// ph_bytes += bit_buffer::needed;
136-
// }
137-
// }
138-
// }
139131
}
140132

141133
//////////////////////////////////////////////////////////////////////////

src/core/codestream/ojph_params.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -952,16 +952,16 @@ namespace ojph {
952952
int s = 0;
953953
double bibo_l = bibo_gains::get_bibo_gain_l(num_decomps, true);
954954
ui32 X = (ui32) ceil(log(bibo_l * bibo_l) / M_LN2);
955-
u8_SPqcd[s++] = (ui8)((B + X) << 3);
955+
u8_SPqcd[s++] = (ui8)((B + X) << reversible_SPqcd_shift);
956956
for (ui32 d = num_decomps; d > 0; --d)
957957
{
958958
double bibo_l = bibo_gains::get_bibo_gain_l(d, true);
959959
double bibo_h = bibo_gains::get_bibo_gain_h(d - 1, true);
960960
X = (ui32) ceil(log(bibo_h * bibo_l) / M_LN2);
961-
u8_SPqcd[s++] = (ui8)((B + X) << 3);
962-
u8_SPqcd[s++] = (ui8)((B + X) << 3);
961+
u8_SPqcd[s++] = (ui8)((B + X) << reversible_SPqcd_shift);
962+
u8_SPqcd[s++] = (ui8)((B + X) << reversible_SPqcd_shift);
963963
X = (ui32) ceil(log(bibo_h * bibo_h) / M_LN2);
964-
u8_SPqcd[s++] = (ui8)((B + X) << 3);
964+
u8_SPqcd[s++] = (ui8)((B + X) << reversible_SPqcd_shift);
965965
}
966966
}
967967

@@ -1017,8 +1017,11 @@ namespace ojph {
10171017
ui32 B = 0;
10181018
int irrev = Sqcd & 0x1F;
10191019
if (irrev == 0) //reversible
1020-
for (ui32 i = 0; i < num_subbands; ++i)
1021-
B = ojph_max(B, (u8_SPqcd[i] >> 3) + get_num_guard_bits() - 1u);
1020+
for (ui32 i = 0; i < num_subbands; ++i) {
1021+
ui32 t = (u8_SPqcd[i] >> reversible_SPqcd_shift);
1022+
t += get_num_guard_bits() - 1u;
1023+
B = ojph_max(B, t);
1024+
}
10221025
else if (irrev == 2) //scalar expounded
10231026
for (ui32 i = 0; i < num_subbands; ++i)
10241027
{
@@ -1088,9 +1091,9 @@ namespace ojph {
10881091
}
10891092

10901093
int irrev = Sqcd & 0x1F;
1091-
if (irrev == 0) //reversible; this is (10.22) from the J2K book
1094+
if (irrev == 0) // reversible; this is (10.22) from the J2K book
10921095
{
1093-
num_bits += u8_SPqcd[idx] >> 3;
1096+
num_bits += u8_SPqcd[idx] >> reversible_SPqcd_shift;
10941097
num_bits = num_bits == 0 ? 0 : num_bits - 1;
10951098
}
10961099
else if (irrev == 1)

src/core/codestream/ojph_params_local.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ namespace ojph {
590590
{
591591
friend ::ojph::param_qcd;
592592
public:
593-
param_qcd()
593+
param_qcd() : reversible_SPqcd_shift(3)
594594
{
595595
Lqcd = 0;
596596
Sqcd = 0;
@@ -646,6 +646,7 @@ namespace ojph {
646646
protected:
647647
ui16 Lqcd;
648648
ui8 Sqcd;
649+
const ui8 reversible_SPqcd_shift;
649650
union
650651
{
651652
ui8 u8_SPqcd[97];

src/core/codestream/ojph_precinct.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ namespace ojph {
221221
{
222222
int num_zeros = *mmsb_tag.get(x>>levm1, y>>levm1, levm1);
223223
num_zeros -= *mmsb_tag.get(x>>cur_lev, y>>cur_lev, cur_lev);
224-
bb_put_bits(&bb, 1, num_zeros + 1,
224+
bb_put_zeros(&bb, num_zeros,
225+
elastic, cur_coded_list, ph_bytes);
226+
bb_put_bits(&bb, 1, 1,
225227
elastic, cur_coded_list, ph_bytes);
226228
*mmsb_tag_flags.get(x>>levm1, y>>levm1, levm1) = 1;
227229
}

src/core/coding/ojph_block_encoder.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,8 +1021,21 @@ namespace ojph {
10211021
{
10221022
assert(num_passes == 1);
10231023
(void)num_passes; //currently not used
1024-
const int ms_size = (16384*16+14)/15; //more than enough
1024+
// 38 bits/sample + 1 color + 4 wavelet = 43 bits per sample.
1025+
// * 4096 samples / 8 bits per byte = 22016; then rounded up to the
1026+
// nearest 1 kB, givin 22528. This expanded further to take into
1027+
// consideration stuffing at a max rate of 16 bits per 15 bits
1028+
// (1 bit for every 15 bits of data); in reality, it is much smaller
1029+
// than this.
1030+
const int ms_size = (22528 * 16 + 14) / 15; //more than enough
10251031
ui8 ms_buf[ms_size];
1032+
// For each quad, we need at most, 7 bits for VLC and 12 bits for UVLC.
1033+
// So we have 1024 quads * 19 / 8, which is 2432. This must be
1034+
// multiplied by 16 / 15 to accommodate stuffing.
1035+
// The mel is at most around 1 bit/quad, giving around 128 byte -- in
1036+
// practice there was on case where it got to 132 bytes. Even
1037+
// accounting for stuffing, it is smaller than 192. Therefore,
1038+
// 3072 is more than enough
10261039
const int mel_vlc_size = 3072; //more than enough
10271040
ui8 mel_vlc_buf[mel_vlc_size];
10281041
const int mel_size = 192;

0 commit comments

Comments
 (0)