4545#include " ojph_codestream_local.h"
4646#include " ojph_codeblock.h"
4747#include " ojph_subband.h"
48+ #include " ojph_resolution.h"
4849
4950namespace ojph {
5051
5152 namespace local
5253 {
5354
5455 // ////////////////////////////////////////////////////////////////////////
55- void codeblock::pre_alloc (codestream *codestream,
56+ void codeblock::pre_alloc (codestream *codestream, ui32 comp_num,
5657 const size& nominal)
5758 {
5859 mem_fixed_allocator* allocator = codestream->get_allocator ();
5960
6061 assert (byte_alignment / sizeof (ui32) > 1 );
6162 const ui32 f = byte_alignment / sizeof (ui32) - 1 ;
6263 ui32 stride = (nominal.w + f) & ~f; // a multiple of 8
63- allocator->pre_alloc_data <ui32>(nominal.h * stride, 0 );
64+
65+ const param_siz* sz = codestream->get_siz ();
66+ const param_cod* cd = codestream->get_cod (comp_num);
67+ ui32 bit_depth = cd->propose_implementation_precision (sz);
68+ if (bit_depth <= 32 )
69+ allocator->pre_alloc_data <ui32>(nominal.h * stride, 0 );
70+ else
71+ allocator->pre_alloc_data <ui64>(nominal.h * stride, 0 );
6472 }
6573
6674 // ////////////////////////////////////////////////////////////////////////
@@ -75,7 +83,19 @@ namespace ojph {
7583 const ui32 f = byte_alignment / sizeof (ui32) - 1 ;
7684 this ->stride = (nominal.w + f) & ~f; // a multiple of 8
7785 this ->buf_size = this ->stride * nominal.h ;
78- this ->buf = allocator->post_alloc_data <ui32>(this ->buf_size , 0 );
86+
87+ ui32 comp_num = parent->get_parent ()->get_comp_num ();
88+ const param_siz* sz = codestream->get_siz ();
89+ const param_cod* cd = codestream->get_cod (comp_num);
90+ ui32 bit_depth = cd->propose_implementation_precision (sz);
91+ if (bit_depth <= 32 ) {
92+ precision = BUF32;
93+ this ->buf32 = allocator->post_alloc_data <ui32>(this ->buf_size , 0 );
94+ }
95+ else {
96+ precision = BUF64;
97+ this ->buf64 = allocator->post_alloc_data <ui64>(this ->buf_size , 0 );
98+ }
7999
80100 this ->nominal_size = nominal;
81101 this ->cb_size = cb_size;
@@ -85,8 +105,8 @@ namespace ojph {
85105 this ->delta = parent->get_delta ();
86106 this ->delta_inv = 1 .0f / this ->delta ;
87107 this ->K_max = K_max;
88- for (int i = 0 ; i < 8 ; ++i)
89- this ->max_val [i] = 0 ;
108+ for (int i = 0 ; i < 4 ; ++i)
109+ this ->max_val64 [i] = 0 ;
90110 ojph::param_cod cod = codestream->access_cod ();
91111 this ->reversible = cod.is_reversible ();
92112 this ->resilient = codestream->is_resilient ();
@@ -100,28 +120,61 @@ namespace ojph {
100120 // ////////////////////////////////////////////////////////////////////////
101121 void codeblock::push (line_buf *line)
102122 {
103- // convert to sign and magnitude and keep max_val
104- const si32 *sp = line->i32 + line_offset;
105- ui32 *dp = buf + cur_line * stride;
106- this ->codeblock_functions .tx_to_cb (sp, dp, K_max, delta_inv, cb_size.w ,
107- max_val);
108- ++cur_line;
123+ // convert to sign and magnitude and keep max_val
124+ if (precision == BUF32)
125+ {
126+ assert (line->flags & line_buf::LFT_32BIT);
127+ const si32 *sp = line->i32 + line_offset;
128+ ui32 *dp = buf32 + cur_line * stride;
129+ this ->codeblock_functions .tx_to_cb32 (sp, dp, K_max, delta_inv,
130+ cb_size.w , max_val32);
131+ ++cur_line;
132+ }
133+ else
134+ {
135+ assert (precision == BUF64);
136+ assert (line->flags & line_buf::LFT_64BIT);
137+ const si64 *sp = line->i64 + line_offset;
138+ ui64 *dp = buf64 + cur_line * stride;
139+ this ->codeblock_functions .tx_to_cb64 (sp, dp, K_max, delta_inv,
140+ cb_size.w , max_val64);
141+ ++cur_line;
142+ }
109143 }
110144
111145 // ////////////////////////////////////////////////////////////////////////
112146 void codeblock::encode (mem_elastic_allocator *elastic)
113147 {
114- ui32 mv = this ->codeblock_functions .find_max_val (max_val);
115- if (mv >= 1u <<(31 - K_max))
148+ if (precision == BUF32)
149+ {
150+ ui32 mv = this ->codeblock_functions .find_max_val32 (max_val32);
151+ if (mv >= 1u << (31 - K_max))
152+ {
153+ coded_cb->missing_msbs = K_max - 1 ;
154+ assert (coded_cb->missing_msbs > 0 );
155+ assert (coded_cb->missing_msbs < K_max);
156+ coded_cb->num_passes = 1 ;
157+
158+ this ->codeblock_functions .encode_cb32 (buf32, K_max-1 , 1 ,
159+ cb_size.w , cb_size.h , stride, coded_cb->pass_length ,
160+ elastic, coded_cb->next_coded );
161+ }
162+ }
163+ else
116164 {
117- coded_cb->missing_msbs = K_max - 1 ;
118- assert (coded_cb->missing_msbs > 0 );
119- assert (coded_cb->missing_msbs < K_max);
120- coded_cb->num_passes = 1 ;
121-
122- this ->codeblock_functions .encode_cb (buf, K_max-1 , 1 ,
123- cb_size.w , cb_size.h , stride, coded_cb->pass_length ,
124- elastic, coded_cb->next_coded );
165+ assert (precision == BUF64);
166+ ui64 mv = this ->codeblock_functions .find_max_val64 (max_val64);
167+ if (mv >= 1ULL << (63 - K_max))
168+ {
169+ coded_cb->missing_msbs = K_max - 1 ;
170+ assert (coded_cb->missing_msbs > 0 );
171+ assert (coded_cb->missing_msbs < K_max);
172+ coded_cb->num_passes = 1 ;
173+
174+ this ->codeblock_functions .encode_cb64 (buf64, K_max-1 , 1 ,
175+ cb_size.w , cb_size.h , stride, coded_cb->pass_length ,
176+ elastic, coded_cb->next_coded );
177+ }
125178 }
126179 }
127180
@@ -132,8 +185,8 @@ namespace ojph {
132185 this ->cb_size = cb_size;
133186 this ->coded_cb = coded_cb;
134187 this ->cur_line = 0 ;
135- for (int i = 0 ; i < 8 ; ++i)
136- this ->max_val [i] = 0 ;
188+ for (int i = 0 ; i < 4 ; ++i)
189+ this ->max_val64 [i] = 0 ;
137190 this ->zero_block = false ;
138191 }
139192
@@ -143,11 +196,24 @@ namespace ojph {
143196 if (coded_cb->pass_length [0 ] > 0 && coded_cb->num_passes > 0 &&
144197 coded_cb->next_coded != NULL )
145198 {
146- bool result = this ->codeblock_functions .decode_cb (
199+ bool result;
200+ if (precision == BUF32)
201+ {
202+ result = this ->codeblock_functions .decode_cb32 (
203+ coded_cb->next_coded ->buf + coded_cb_header::prefix_buf_size,
204+ buf32, coded_cb->missing_msbs , coded_cb->num_passes ,
205+ coded_cb->pass_length [0 ], coded_cb->pass_length [1 ],
206+ cb_size.w , cb_size.h , stride, stripe_causal);
207+ }
208+ else
209+ {
210+ assert (precision == BUF64);
211+ result = this ->codeblock_functions .decode_cb64 (
147212 coded_cb->next_coded ->buf + coded_cb_header::prefix_buf_size,
148- buf , coded_cb->missing_msbs , coded_cb->num_passes ,
213+ buf64 , coded_cb->missing_msbs , coded_cb->num_passes ,
149214 coded_cb->pass_length [0 ], coded_cb->pass_length [1 ],
150215 cb_size.w , cb_size.h , stride, stripe_causal);
216+ }
151217
152218 if (result == false )
153219 {
@@ -167,15 +233,37 @@ namespace ojph {
167233 // ////////////////////////////////////////////////////////////////////////
168234 void codeblock::pull_line (line_buf *line)
169235 {
170- si32 *dp = line-> i32 + line_offset;
171- if (!zero_block )
236+ // convert to sign and magnitude
237+ if (precision == BUF32 )
172238 {
173- // convert to sign and magnitude
174- const ui32 *sp = buf + cur_line * stride;
175- this ->codeblock_functions .tx_from_cb (sp, dp, K_max, delta, cb_size.w );
239+ assert (line->flags & line_buf::LFT_32BIT);
240+ si32 *dp = line->i32 + line_offset;
241+ if (!zero_block)
242+ {
243+ const ui32 *sp = buf32 + cur_line * stride;
244+ this ->codeblock_functions .tx_from_cb32 (sp, dp, K_max, delta,
245+ cb_size.w );
246+ }
247+ else
248+ this ->codeblock_functions .mem_clear32 (dp, cb_size.w * sizeof (ui32));
176249 }
177250 else
178- this ->codeblock_functions .mem_clear (dp, cb_size.w * sizeof (*dp));
251+ {
252+ assert (precision == BUF64);
253+ assert (line->flags & line_buf::LFT_64BIT);
254+ si64 *dp = line->i64 + line_offset;
255+ if (!zero_block)
256+ {
257+ const ui64 *sp = buf64 + cur_line * stride;
258+ this ->codeblock_functions .tx_from_cb64 (sp, dp, K_max, delta,
259+ cb_size.w );
260+ }
261+ else
262+ this ->codeblock_functions .mem_clear64 (dp, cb_size.w * sizeof (*dp));
263+
264+
265+ }
266+
179267 ++cur_line;
180268 assert (cur_line <= cb_size.h );
181269 }
0 commit comments