OpenJPH
Open-source implementation of JPEG2000 Part-15
Toggle main menu visibility
Loading...
Searching...
No Matches
ojph_codeblock.cpp
Go to the documentation of this file.
1
2
//***************************************************************************/
3
// This software is released under the 2-Clause BSD license, included
4
// below.
5
//
6
// Copyright (c) 2019, Aous Naman
7
// Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
8
// Copyright (c) 2019, The University of New South Wales, Australia
9
//
10
// Redistribution and use in source and binary forms, with or without
11
// modification, are permitted provided that the following conditions are
12
// met:
13
//
14
// 1. Redistributions of source code must retain the above copyright
15
// notice, this list of conditions and the following disclaimer.
16
//
17
// 2. Redistributions in binary form must reproduce the above copyright
18
// notice, this list of conditions and the following disclaimer in the
19
// documentation and/or other materials provided with the distribution.
20
//
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
//***************************************************************************/
33
// This file is part of the OpenJPH software implementation.
34
// File: ojph_codeblock.cpp
35
// Author: Aous Naman
36
// Date: 28 August 2019
37
//***************************************************************************/
38
39
40
#include <climits>
41
#include <cmath>
42
43
#include "
ojph_mem.h
"
44
#include "
ojph_params.h
"
45
#include "
ojph_codestream_local.h
"
46
#include "
ojph_codeblock.h
"
47
#include "
ojph_subband.h
"
48
#include "
ojph_resolution.h
"
49
50
namespace
ojph
{
51
52
namespace
local
53
{
54
56
void
codeblock::pre_alloc
(
codestream
*
codestream
,
const
size
& nominal,
57
ui32
precision
)
58
{
59
mem_fixed_allocator
* allocator =
codestream
->
get_allocator
();
60
61
assert(
byte_alignment
/
sizeof
(
ui32
) > 1);
62
const
ui32
f =
byte_alignment
/
sizeof
(
ui32
) - 1;
63
ui32
stride
= (nominal.
w
+ f) & ~f;
// a multiple of 8
64
// the HT cleanup pass writes two rows per quad row, so the sample
65
// buffer must hold an even number of rows
66
ui32
rows = (nominal.
h
+ 1u) & ~1u;
67
68
if
(
precision
<= 32)
69
allocator->
pre_alloc_data
<
ui32
>(rows * (size_t)
stride
, 0);
70
else
71
allocator->
pre_alloc_data
<
ui64
>(rows * (size_t)
stride
, 0);
72
}
73
75
void
codeblock::finalize_alloc
(
codestream
*
codestream
,
76
subband
*
parent
,
const
size
& nominal,
77
const
size
&
cb_size
,
78
coded_cb_header
*
coded_cb
,
79
ui32
K_max
,
int
line_offset
,
80
ui32
precision
,
ui32
comp_idx)
81
{
82
mem_fixed_allocator
* allocator =
codestream
->
get_allocator
();
83
84
const
ui32
f =
byte_alignment
/
sizeof
(
ui32
) - 1;
85
this->
stride
= (nominal.
w
+ f) & ~f;
// a multiple of 8
86
// the HT cleanup pass writes two rows per quad row, so the sample
87
// buffer must hold an even number of rows
88
this->
buf_size
= this->
stride
* ((nominal.
h
+ 1u) & ~1u);
89
90
if
(
precision
<= 32) {
91
this->precision =
BUF32
;
92
this->buf32 = allocator->
post_alloc_data
<
ui32
>(this->
buf_size
, 0);
93
}
94
else
{
95
this->precision =
BUF64
;
96
this->buf64 = allocator->
post_alloc_data
<
ui64
>(this->
buf_size
, 0);
97
}
98
99
this->
nominal_size
= nominal;
100
this->cb_size =
cb_size
;
101
this->parent =
parent
;
102
this->line_offset =
line_offset
;
103
this->
cur_line
= 0;
104
this->
delta
=
parent
->
get_delta
();
105
this->
delta_inv
= 1.0f / this->
delta
;
106
this->K_max =
K_max
;
107
for
(
int
i = 0; i < 4; ++i)
108
this->max_val64[i] = 0;
109
const
param_cod
* coc =
codestream
->
get_coc
(comp_idx);
110
this->
reversible
= coc->
is_reversible
();
111
this->
resilient
=
codestream
->
is_resilient
();
112
this->
stripe_causal
= coc->
get_block_vertical_causality
();
113
this->
zero_block
=
false
;
114
this->coded_cb =
coded_cb
;
115
116
this->
codeblock_functions
.
init
(
reversible
);
117
}
118
120
void
codeblock::push
(
line_buf
*line)
121
{
122
// convert to sign and magnitude and keep max_val
123
if
(
precision
==
BUF32
)
124
{
125
assert(line->
flags
&
line_buf::LFT_32BIT
);
126
const
void
*sp = (line->
flags
&
line_buf::LFT_INTEGER
)
127
? (
const
void
*)(line->i32 +
line_offset
)
128
: (
const
void
*)(line->f32 +
line_offset
);
129
ui32
*dp = buf32 +
cur_line
*
stride
;
130
this->
codeblock_functions
.
tx_to_cb32
(sp, dp,
K_max
,
delta_inv
,
131
cb_size
.
w
, max_val32);
132
++
cur_line
;
133
}
134
else
135
{
136
assert(
precision
==
BUF64
);
137
assert(line->
flags
&
line_buf::LFT_64BIT
);
138
const
si64
*sp = line->i64 +
line_offset
;
139
ui64
*dp = buf64 +
cur_line
*
stride
;
140
this->
codeblock_functions
.
tx_to_cb64
(sp, dp,
K_max
,
delta_inv
,
141
cb_size
.
w
, max_val64);
142
++
cur_line
;
143
}
144
}
145
147
void
codeblock::encode
(
mem_elastic_allocator
*elastic)
148
{
149
if
(
precision
==
BUF32
)
150
{
151
ui32
mv = this->
codeblock_functions
.
find_max_val32
(max_val32);
152
if
(mv >= 1u << (31 -
K_max
))
153
{
154
coded_cb
->
missing_msbs
=
K_max
- 1;
155
assert(
coded_cb
->
missing_msbs
> 0);
156
assert(
coded_cb
->
missing_msbs
<
K_max
);
157
coded_cb
->
num_passes
= 1;
158
159
this->
codeblock_functions
.
encode_cb32
(buf32,
K_max
-1, 1,
160
cb_size
.
w
,
cb_size
.
h
,
stride
,
coded_cb
->
pass_length
,
161
elastic,
coded_cb
->
next_coded
);
162
}
163
}
164
else
165
{
166
assert(
precision
==
BUF64
);
167
ui64
mv = this->
codeblock_functions
.
find_max_val64
(max_val64);
168
if
(mv >= 1ULL << (63 -
K_max
))
169
{
170
coded_cb
->
missing_msbs
=
K_max
- 1;
171
assert(
coded_cb
->
missing_msbs
> 0);
172
assert(
coded_cb
->
missing_msbs
<
K_max
);
173
coded_cb
->
num_passes
= 1;
174
175
this->
codeblock_functions
.
encode_cb64
(buf64,
K_max
-1, 1,
176
cb_size
.
w
,
cb_size
.
h
,
stride
,
coded_cb
->
pass_length
,
177
elastic,
coded_cb
->
next_coded
);
178
}
179
}
180
}
181
183
void
codeblock::recreate
(
const
size
&
cb_size
,
coded_cb_header
*
coded_cb
)
184
{
185
assert(
cb_size
.
h
*
stride
<=
buf_size
&&
cb_size
.
w
<=
stride
);
186
this->cb_size =
cb_size
;
187
this->coded_cb =
coded_cb
;
188
this->
cur_line
= 0;
189
for
(
int
i = 0; i < 4; ++i)
190
this->max_val64[i] = 0;
191
this->
zero_block
=
false
;
192
}
193
195
void
codeblock::decode
()
196
{
197
if
(
coded_cb
->
pass_length
[0] > 0 &&
coded_cb
->
num_passes
> 0 &&
198
coded_cb
->
next_coded
!= NULL)
199
{
200
bool
result;
201
if
(
precision
==
BUF32
)
202
{
203
result = this->
codeblock_functions
.
decode_cb32
(
204
coded_cb
->
next_coded
->
buf
+
coded_cb_header::prefix_buf_size
,
205
buf32,
coded_cb
->
missing_msbs
,
coded_cb
->
num_passes
,
206
coded_cb
->
pass_length
[0],
coded_cb
->
pass_length
[1],
207
cb_size
.
w
,
cb_size
.
h
,
stride
,
stripe_causal
);
208
}
209
else
210
{
211
assert(
precision
==
BUF64
);
212
result = this->
codeblock_functions
.
decode_cb64
(
213
coded_cb
->
next_coded
->
buf
+
coded_cb_header::prefix_buf_size
,
214
buf64,
coded_cb
->
missing_msbs
,
coded_cb
->
num_passes
,
215
coded_cb
->
pass_length
[0],
coded_cb
->
pass_length
[1],
216
cb_size
.
w
,
cb_size
.
h
,
stride
,
stripe_causal
);
217
}
218
219
if
(result ==
false
)
220
{
221
if
(
resilient
==
true
) {
222
OJPH_INFO
(0x000300A1,
"Error decoding a codeblock."
);
223
zero_block
=
true
;
224
}
225
else
226
OJPH_ERROR
(0x000300A1,
"Error decoding a codeblock."
);
227
}
228
}
229
else
230
zero_block
=
true
;
231
}
232
233
235
void
codeblock::pull_line
(
line_buf
*line)
236
{
237
//convert to sign and magnitude
238
if
(
precision
==
BUF32
)
239
{
240
assert(line->
flags
&
line_buf::LFT_32BIT
);
241
void
*dp = (line->
flags
&
line_buf::LFT_INTEGER
)
242
? (
void
*)(line->i32 +
line_offset
)
243
: (
void
*)(line->f32 +
line_offset
);
244
if
(!
zero_block
)
245
{
246
const
ui32
*sp = buf32 +
cur_line
*
stride
;
247
this->
codeblock_functions
.
tx_from_cb32
(sp, dp,
K_max
,
delta
,
248
cb_size
.
w
);
249
}
250
else
251
this->
codeblock_functions
.
mem_clear
(dp,
cb_size
.
w
*
sizeof
(
ui32
));
252
}
253
else
254
{
255
assert(
precision
==
BUF64
);
256
assert((
reversible
&& (line->
flags
&
line_buf::LFT_64BIT
))
257
|| (!
reversible
&& (line->
flags
&
line_buf::LFT_32BIT
)));
258
si64
*dp = line->i64 +
line_offset
;
259
if
(!
zero_block
)
260
{
261
const
ui64
*sp = buf64 +
cur_line
*
stride
;
262
this->
codeblock_functions
.
tx_from_cb64
(sp, dp,
K_max
,
delta
,
263
cb_size
.
w
);
264
}
265
else
266
this->
codeblock_functions
.
mem_clear
(dp,
cb_size
.
w
*
sizeof
(*dp));
267
}
268
269
++
cur_line
;
270
assert(
cur_line
<=
cb_size
.
h
);
271
}
272
273
}
274
}
ojph::line_buf
Definition
ojph_mem.h:161
ojph::line_buf::LFT_INTEGER
@ LFT_INTEGER
Definition
ojph_mem.h:170
ojph::line_buf::LFT_32BIT
@ LFT_32BIT
Definition
ojph_mem.h:168
ojph::line_buf::LFT_64BIT
@ LFT_64BIT
Definition
ojph_mem.h:169
ojph::line_buf::flags
ui32 flags
Definition
ojph_mem.h:183
ojph::local::codeblock::nominal_size
size nominal_size
Definition
ojph_codeblock.h:93
ojph::local::codeblock::line_offset
int line_offset
Definition
ojph_codeblock.h:98
ojph::local::codeblock::stripe_causal
bool stripe_causal
Definition
ojph_codeblock.h:104
ojph::local::codeblock::resilient
bool resilient
Definition
ojph_codeblock.h:103
ojph::local::codeblock::coded_cb
coded_cb_header * coded_cb
Definition
ojph_codeblock.h:110
ojph::local::codeblock::decode
void decode()
Definition
ojph_codeblock.cpp:195
ojph::local::codeblock::parent
subband * parent
Definition
ojph_codeblock.h:97
ojph::local::codeblock::finalize_alloc
void finalize_alloc(codestream *codestream, subband *parent, const size &nominal, const size &cb_size, coded_cb_header *coded_cb, ui32 K_max, int tbx0, ui32 precision, ui32 comp_idx)
Definition
ojph_codeblock.cpp:75
ojph::local::codeblock::precision
ui32 precision
Definition
ojph_codeblock.h:88
ojph::local::codeblock::push
void push(line_buf *line)
Definition
ojph_codeblock.cpp:120
ojph::local::codeblock::cb_size
size cb_size
Definition
ojph_codeblock.h:94
ojph::local::codeblock::buf_size
ui32 buf_size
Definition
ojph_codeblock.h:96
ojph::local::codeblock::K_max
ui32 K_max
Definition
ojph_codeblock.h:101
ojph::local::codeblock::pre_alloc
static void pre_alloc(codestream *codestream, const size &nominal, ui32 precision)
Definition
ojph_codeblock.cpp:56
ojph::local::codeblock::delta_inv
float delta_inv
Definition
ojph_codeblock.h:100
ojph::local::codeblock::encode
void encode(mem_elastic_allocator *elastic)
Definition
ojph_codeblock.cpp:147
ojph::local::codeblock::BUF32
@ BUF32
Definition
ojph_codeblock.h:69
ojph::local::codeblock::BUF64
@ BUF64
Definition
ojph_codeblock.h:70
ojph::local::codeblock::recreate
void recreate(const size &cb_size, coded_cb_header *coded_cb)
Definition
ojph_codeblock.cpp:183
ojph::local::codeblock::delta
float delta
Definition
ojph_codeblock.h:100
ojph::local::codeblock::stride
ui32 stride
Definition
ojph_codeblock.h:95
ojph::local::codeblock::reversible
bool reversible
Definition
ojph_codeblock.h:102
ojph::local::codeblock::cur_line
ui32 cur_line
Definition
ojph_codeblock.h:99
ojph::local::codeblock::zero_block
bool zero_block
Definition
ojph_codeblock.h:105
ojph::local::codeblock::codeblock_functions
codeblock_fun codeblock_functions
Definition
ojph_codeblock.h:111
ojph::local::codeblock::pull_line
void pull_line(line_buf *line)
Definition
ojph_codeblock.cpp:235
ojph::local::codestream
Definition
ojph_codestream_local.h:65
ojph::local::codestream::is_resilient
bool is_resilient()
Definition
ojph_codestream_local.h:101
ojph::local::codestream::get_coc
const param_cod * get_coc(ui32 comp_num)
Definition
ojph_codestream_local.h:85
ojph::local::codestream::get_allocator
mem_fixed_allocator * get_allocator()
Definition
ojph_codestream_local.h:93
ojph::local::subband
Definition
ojph_subband.h:63
ojph::local::subband::get_delta
float get_delta()
Definition
ojph_subband.h:93
ojph::mem_elastic_allocator
Definition
ojph_mem.h:218
ojph::mem_fixed_allocator
Definition
ojph_mem.h:59
ojph::mem_fixed_allocator::pre_alloc_data
void pre_alloc_data(size_t num_ele, ui32 pre_size)
Definition
ojph_mem.h:72
ojph::mem_fixed_allocator::post_alloc_data
T * post_alloc_data(size_t num_ele, ui32 pre_size)
Definition
ojph_mem.h:112
ojph::local
Definition
ojph_bitbuffer_read.h:53
ojph
Definition
ojph_img_io.h:52
ojph::byte_alignment
const ui32 byte_alignment
Definition
ojph_arch.h:345
ojph::si64
int64_t si64
Definition
ojph_defs.h:57
ojph::ui64
uint64_t ui64
Definition
ojph_defs.h:56
ojph::ui32
uint32_t ui32
Definition
ojph_defs.h:54
ojph_codeblock.h
ojph_codestream_local.h
ojph_mem.h
OJPH_INFO
#define OJPH_INFO(t,...)
MACROs to insert file and line number for info, warning, and error.
Definition
ojph_message.h:284
OJPH_ERROR
#define OJPH_ERROR(t,...)
Definition
ojph_message.h:288
ojph_params.h
ojph_resolution.h
ojph_subband.h
ojph::coded_lists::buf
ui8 * buf
Definition
ojph_mem.h:213
ojph::local::codeblock_fun::tx_to_cb64
tx_to_cb_fun64 tx_to_cb64
Definition
ojph_codeblock_fun.h:106
ojph::local::codeblock_fun::encode_cb64
cb_encoder_fun64 encode_cb64
Definition
ojph_codeblock_fun.h:118
ojph::local::codeblock_fun::init
void init(bool reversible)
Definition
ojph_codeblock_fun.cpp:147
ojph::local::codeblock_fun::find_max_val32
find_max_val_fun32 find_max_val32
Definition
ojph_codeblock_fun.h:101
ojph::local::codeblock_fun::tx_from_cb32
tx_from_cb_fun32 tx_from_cb32
Definition
ojph_codeblock_fun.h:109
ojph::local::codeblock_fun::tx_to_cb32
tx_to_cb_fun32 tx_to_cb32
Definition
ojph_codeblock_fun.h:105
ojph::local::codeblock_fun::find_max_val64
find_max_val_fun64 find_max_val64
Definition
ojph_codeblock_fun.h:102
ojph::local::codeblock_fun::decode_cb32
cb_decoder_fun32 decode_cb32
Definition
ojph_codeblock_fun.h:113
ojph::local::codeblock_fun::encode_cb32
cb_encoder_fun32 encode_cb32
Definition
ojph_codeblock_fun.h:117
ojph::local::codeblock_fun::mem_clear
mem_clear_fun mem_clear
Definition
ojph_codeblock_fun.h:98
ojph::local::codeblock_fun::decode_cb64
cb_decoder_fun64 decode_cb64
Definition
ojph_codeblock_fun.h:114
ojph::local::codeblock_fun::tx_from_cb64
tx_from_cb_fun64 tx_from_cb64
Definition
ojph_codeblock_fun.h:110
ojph::local::coded_cb_header
Definition
ojph_codeblock.h:116
ojph::local::coded_cb_header::next_coded
coded_lists * next_coded
Definition
ojph_codeblock.h:121
ojph::local::coded_cb_header::pass_length
ui32 pass_length[2]
Definition
ojph_codeblock.h:117
ojph::local::coded_cb_header::missing_msbs
ui32 missing_msbs
Definition
ojph_codeblock.h:120
ojph::local::coded_cb_header::prefix_buf_size
static const int prefix_buf_size
Definition
ojph_codeblock.h:123
ojph::local::coded_cb_header::num_passes
ui32 num_passes
Definition
ojph_codeblock.h:118
ojph::local::param_cod
Definition
ojph_params_local.h:385
ojph::local::param_cod::get_block_vertical_causality
bool get_block_vertical_causality() const
Definition
ojph_params_local.h:578
ojph::local::param_cod::is_reversible
bool is_reversible() const
Definition
ojph_params.cpp:1061
ojph::size
Definition
ojph_base.h:48
ojph::size::w
ui32 w
Definition
ojph_base.h:50
ojph::size::h
ui32 h
Definition
ojph_base.h:51
src
core
codestream
ojph_codeblock.cpp
Generated by
1.18.0