Skip to content

Commit a27e7d6

Browse files
committed
This closes issue #28. The scratch field has been moved to ojph::local::codestream, and therefore it gets created and destructed with ojph::local::codestream. Thanks to [Dan](https://github.com/dtatut) for reporting this bug. I think it is fair to say that [Aaron](https://github.com/boxerab) in pull request #17 was alluding to this problem.
This also fixes a bug that was inadvertently introduced in the earlier commit.
1 parent cab1d26 commit a27e7d6

File tree

3 files changed

+48
-43
lines changed

3 files changed

+48
-43
lines changed

src/core/codestream/ojph_codestream.cpp

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ namespace ojph {
181181

182182
////////////////////////////////////////////////////////////////////////////
183183
codestream::codestream()
184-
: allocator(NULL), elastic_alloc(NULL)
184+
: allocator(NULL), elastic_alloc(NULL), precinct_scratch(NULL)
185185
{
186186
tiles = NULL;
187187
line = NULL;
@@ -199,6 +199,8 @@ namespace ojph {
199199
cur_line = 0;
200200
cur_tile_row = 0;
201201

202+
precinct_scratch_needed_bytes = 0;
203+
202204
allocator = new mem_fixed_allocator;
203205
elastic_alloc = new mem_elastic_allocator(1048576); //1 megabyte
204206

@@ -286,13 +288,45 @@ namespace ojph {
286288
int num_pairs = (int)num_tiles.area();
287289
allocator->pre_alloc_obj<param_tlm::Ttlm_Ptlm_pair>(num_pairs);
288290
}
291+
292+
//precinct scratch buffer
293+
ojph::param_cod cd = access_cod();
294+
int num_decomps = cd.get_num_decompositions();
295+
size log_cb = cd.get_log_block_dims();
296+
297+
size ratio;
298+
for (int r = 0; r <= num_decomps; ++r)
299+
{
300+
size log_PP = cd.get_log_precinct_size(r);
301+
log_PP.w -= (r ? 1 : 0);
302+
log_PP.h -= (r ? 1 : 0);
303+
ratio.w = ojph_max(ratio.w, log_PP.w - ojph_min(log_cb.w, log_PP.w));
304+
ratio.h = ojph_max(ratio.h, log_PP.h - ojph_min(log_cb.h, log_PP.h));
305+
}
306+
int max_ratio = ojph_max(ratio.w, ratio.h);
307+
max_ratio = 1 << max_ratio;
308+
// assuming that we have a hierarchy of n levels.
309+
// This needs 4/3 times the area, rounded up
310+
// (rounding up leaves one extra entry).
311+
// This exta entry is necessary
312+
// We need 4 such tables. These tables store
313+
// 1. missing msbs and 2. their flags,
314+
// 3. number of layers and 4. their flags
315+
precinct_scratch_needed_bytes =
316+
4 * (int)((max_ratio * max_ratio * 4 + 2) / 3);
317+
318+
allocator->pre_alloc_obj<ui8>(precinct_scratch_needed_bytes);
289319
}
290320

291321
//////////////////////////////////////////////////////////////////////////
292322
void codestream::finalize_alloc()
293323
{
294324
allocator->alloc();
295325

326+
//precinct scratch buffer
327+
precinct_scratch =
328+
allocator->post_alloc_obj<ui8>(precinct_scratch_needed_bytes);
329+
296330
//get tiles
297331
tiles = this->allocator->post_alloc_obj<tile>(num_tiles.area());
298332

@@ -1359,8 +1393,11 @@ namespace ojph {
13591393
for (int c = 0; c < num_comps; ++c)
13601394
num_comp_bytes[c] = comps[c].prepare_precincts();
13611395
else
1396+
{
1397+
num_comp_bytes[0] = 0;
13621398
for (int c = 0; c < num_comps; ++c)
13631399
num_comp_bytes[0] += comps[c].prepare_precincts();
1400+
}
13641401
}
13651402

13661403
//////////////////////////////////////////////////////////////////////////
@@ -1994,14 +2031,15 @@ namespace ojph {
19942031
pp->bands = bands;
19952032
pp->may_use_sop = cd.packets_may_use_sop();
19962033
pp->uses_eph = cd.packets_use_eph();
2034+
pp->scratch = codestream->get_precinct_scratch();
19972035
}
19982036
}
19992037
if (num_bands == 1)
20002038
bands[0].get_cb_indices(num_precincts, precincts);
20012039
else
20022040
for (int i = 1; i < 4; ++i)
20032041
bands[i].get_cb_indices(num_precincts, precincts);
2004-
precinct::alloc_scratch(codestream);
2042+
20052043
size log_cb = cd.get_log_block_dims();
20062044
log_PP.w -= (res_num?1:0);
20072045
log_PP.h -= (res_num?1:0);
@@ -2533,44 +2571,6 @@ namespace ojph {
25332571
//
25342572
//////////////////////////////////////////////////////////////////////////
25352573

2536-
//////////////////////////////////////////////////////////////////////////
2537-
ui8* precinct::scratch = NULL;
2538-
2539-
//////////////////////////////////////////////////////////////////////////
2540-
void precinct::alloc_scratch(codestream *codestream)
2541-
{
2542-
if (scratch == NULL)
2543-
{
2544-
ojph::param_cod cd = codestream->access_cod();
2545-
int num_decomps = cd.get_num_decompositions();
2546-
size log_cb = cd.get_log_block_dims();
2547-
2548-
size ratio;
2549-
for (int r = 0; r <= num_decomps; ++r)
2550-
{
2551-
size log_PP = cd.get_log_precinct_size(r);
2552-
log_PP.w -= (r?1:0);
2553-
log_PP.h -= (r?1:0);
2554-
ratio.w = ojph_max(ratio.w, log_PP.w-ojph_min(log_cb.w, log_PP.w));
2555-
ratio.h = ojph_max(ratio.h, log_PP.h-ojph_min(log_cb.h, log_PP.h));
2556-
}
2557-
int max_ratio = ojph_max(ratio.w, ratio.h);
2558-
max_ratio = 1 << max_ratio;
2559-
// assuming that we have a hierarchy of n levels.
2560-
// This needs 4/3 times the area, rounded up
2561-
// (rounding up leaves one extra entry).
2562-
// This exta entry is necessary
2563-
// We need to store missing msbs and number of layers,
2564-
// and indicators if they have been transmitted
2565-
int needed_bytes = 4 * (int)((max_ratio * max_ratio * 4 + 2) / 3);
2566-
2567-
mem_elastic_allocator *elastic = codestream->get_elastic_alloc();
2568-
coded_lists *p;
2569-
elastic->get_buffer(needed_bytes, p);
2570-
scratch = p->buf;
2571-
}
2572-
}
2573-
25742574
//////////////////////////////////////////////////////////////////////////
25752575
struct bit_write_buf
25762576
{

src/core/codestream/ojph_codestream_local.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ namespace ojph {
103103
void check_imf_validity();
104104
void check_boardcast_validity();
105105

106+
ui8* get_precinct_scratch() { return precinct_scratch; }
107+
108+
private:
109+
int precinct_scratch_needed_bytes;
110+
ui8* precinct_scratch;
111+
106112
private:
107113
int cur_line;
108114
int cur_comp;
@@ -263,8 +269,7 @@ namespace ojph {
263269
mem_elastic_allocator *elastic,
264270
int& data_left, infile_base *file);
265271

266-
static ui8 *scratch;
267-
static void alloc_scratch(codestream *codestream);
272+
ui8 *scratch;
268273
point img_point; //the precinct projected to full resolution
269274
rect cb_idxs[4]; //indices of codeblocks
270275
subband *bands; //the subbands

src/core/common/ojph_defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ typedef int64_t si64;
5858
/////////////////////////////////////////////////////////////////////////////
5959
#define OJPH_CORE_VER_MAJOR 0
6060
#define OJPH_CORE_VER_MINOR 6
61-
#define OJPH_CORE_VER_SUBMINOR 2
61+
#define OJPH_CORE_VER_SUBMINOR 3
6262

6363
/////////////////////////////////////////////////////////////////////////////
6464
#define OJPH_INT_STRINGIFY(I) #I

0 commit comments

Comments
 (0)