Skip to content

Commit

Permalink
This closes issue #28. The scratch field has been moved to ojph::loca…
Browse files Browse the repository at this point in the history
…l::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.
  • Loading branch information
aous72 committed Apr 18, 2020
1 parent cab1d26 commit a27e7d6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 43 deletions.
80 changes: 40 additions & 40 deletions src/core/codestream/ojph_codestream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ namespace ojph {

////////////////////////////////////////////////////////////////////////////
codestream::codestream()
: allocator(NULL), elastic_alloc(NULL)
: allocator(NULL), elastic_alloc(NULL), precinct_scratch(NULL)
{
tiles = NULL;
line = NULL;
Expand All @@ -199,6 +199,8 @@ namespace ojph {
cur_line = 0;
cur_tile_row = 0;

precinct_scratch_needed_bytes = 0;

allocator = new mem_fixed_allocator;
elastic_alloc = new mem_elastic_allocator(1048576); //1 megabyte

Expand Down Expand Up @@ -286,13 +288,45 @@ namespace ojph {
int num_pairs = (int)num_tiles.area();
allocator->pre_alloc_obj<param_tlm::Ttlm_Ptlm_pair>(num_pairs);
}

//precinct scratch buffer
ojph::param_cod cd = access_cod();
int num_decomps = cd.get_num_decompositions();
size log_cb = cd.get_log_block_dims();

size ratio;
for (int r = 0; r <= num_decomps; ++r)
{
size log_PP = cd.get_log_precinct_size(r);
log_PP.w -= (r ? 1 : 0);
log_PP.h -= (r ? 1 : 0);
ratio.w = ojph_max(ratio.w, log_PP.w - ojph_min(log_cb.w, log_PP.w));
ratio.h = ojph_max(ratio.h, log_PP.h - ojph_min(log_cb.h, log_PP.h));
}
int max_ratio = ojph_max(ratio.w, ratio.h);
max_ratio = 1 << max_ratio;
// assuming that we have a hierarchy of n levels.
// This needs 4/3 times the area, rounded up
// (rounding up leaves one extra entry).
// This exta entry is necessary
// We need 4 such tables. These tables store
// 1. missing msbs and 2. their flags,
// 3. number of layers and 4. their flags
precinct_scratch_needed_bytes =
4 * (int)((max_ratio * max_ratio * 4 + 2) / 3);

allocator->pre_alloc_obj<ui8>(precinct_scratch_needed_bytes);
}

//////////////////////////////////////////////////////////////////////////
void codestream::finalize_alloc()
{
allocator->alloc();

//precinct scratch buffer
precinct_scratch =
allocator->post_alloc_obj<ui8>(precinct_scratch_needed_bytes);

//get tiles
tiles = this->allocator->post_alloc_obj<tile>(num_tiles.area());

Expand Down Expand Up @@ -1359,8 +1393,11 @@ namespace ojph {
for (int c = 0; c < num_comps; ++c)
num_comp_bytes[c] = comps[c].prepare_precincts();
else
{
num_comp_bytes[0] = 0;
for (int c = 0; c < num_comps; ++c)
num_comp_bytes[0] += comps[c].prepare_precincts();
}
}

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1994,14 +2031,15 @@ namespace ojph {
pp->bands = bands;
pp->may_use_sop = cd.packets_may_use_sop();
pp->uses_eph = cd.packets_use_eph();
pp->scratch = codestream->get_precinct_scratch();
}
}
if (num_bands == 1)
bands[0].get_cb_indices(num_precincts, precincts);
else
for (int i = 1; i < 4; ++i)
bands[i].get_cb_indices(num_precincts, precincts);
precinct::alloc_scratch(codestream);

size log_cb = cd.get_log_block_dims();
log_PP.w -= (res_num?1:0);
log_PP.h -= (res_num?1:0);
Expand Down Expand Up @@ -2533,44 +2571,6 @@ namespace ojph {
//
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
ui8* precinct::scratch = NULL;

//////////////////////////////////////////////////////////////////////////
void precinct::alloc_scratch(codestream *codestream)
{
if (scratch == NULL)
{
ojph::param_cod cd = codestream->access_cod();
int num_decomps = cd.get_num_decompositions();
size log_cb = cd.get_log_block_dims();

size ratio;
for (int r = 0; r <= num_decomps; ++r)
{
size log_PP = cd.get_log_precinct_size(r);
log_PP.w -= (r?1:0);
log_PP.h -= (r?1:0);
ratio.w = ojph_max(ratio.w, log_PP.w-ojph_min(log_cb.w, log_PP.w));
ratio.h = ojph_max(ratio.h, log_PP.h-ojph_min(log_cb.h, log_PP.h));
}
int max_ratio = ojph_max(ratio.w, ratio.h);
max_ratio = 1 << max_ratio;
// assuming that we have a hierarchy of n levels.
// This needs 4/3 times the area, rounded up
// (rounding up leaves one extra entry).
// This exta entry is necessary
// We need to store missing msbs and number of layers,
// and indicators if they have been transmitted
int needed_bytes = 4 * (int)((max_ratio * max_ratio * 4 + 2) / 3);

mem_elastic_allocator *elastic = codestream->get_elastic_alloc();
coded_lists *p;
elastic->get_buffer(needed_bytes, p);
scratch = p->buf;
}
}

//////////////////////////////////////////////////////////////////////////
struct bit_write_buf
{
Expand Down
9 changes: 7 additions & 2 deletions src/core/codestream/ojph_codestream_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ namespace ojph {
void check_imf_validity();
void check_boardcast_validity();

ui8* get_precinct_scratch() { return precinct_scratch; }

private:
int precinct_scratch_needed_bytes;
ui8* precinct_scratch;

private:
int cur_line;
int cur_comp;
Expand Down Expand Up @@ -263,8 +269,7 @@ namespace ojph {
mem_elastic_allocator *elastic,
int& data_left, infile_base *file);

static ui8 *scratch;
static void alloc_scratch(codestream *codestream);
ui8 *scratch;
point img_point; //the precinct projected to full resolution
rect cb_idxs[4]; //indices of codeblocks
subband *bands; //the subbands
Expand Down
2 changes: 1 addition & 1 deletion src/core/common/ojph_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ typedef int64_t si64;
/////////////////////////////////////////////////////////////////////////////
#define OJPH_CORE_VER_MAJOR 0
#define OJPH_CORE_VER_MINOR 6
#define OJPH_CORE_VER_SUBMINOR 2
#define OJPH_CORE_VER_SUBMINOR 3

/////////////////////////////////////////////////////////////////////////////
#define OJPH_INT_STRINGIFY(I) #I
Expand Down

0 comments on commit a27e7d6

Please sign in to comment.