1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
/**
* @file llblockencoder.cpp
* @brief Image block compression
*
* $LicenseInfo:firstyear=2010&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "llblockencoder.h"
// KDU core header files
#include "kdu/kdu_elementary.h"
#include "kdu/kdu_messaging.h"
#include "kdu/kdu_params.h"
#include "kdu/kdu_compressed.h"
#include "kdu/kdu_sample_processing.h"
// KDU utility functions.
#include "kdc_flow_control.h"
#include "llkdumem.h"
#include "llblockdata.h"
#include "llerror.h"
LLBlockEncoder::LLBlockEncoder()
{
mBPP = 0.f;
}
U8 *LLBlockEncoder::encode(const LLBlockData &block_data, U32 &output_size) const
{
switch (block_data.getType())
{
case LLBlockData::BLOCK_TYPE_U32:
{
LLBlockDataU32 &bd_u32 = (LLBlockDataU32 &)block_data;
return encodeU32(bd_u32, output_size);
}
case LLBlockData::BLOCK_TYPE_F32:
{
LLBlockDataF32 &bd_f32 = (LLBlockDataF32 &)block_data;
return encodeF32(bd_f32, output_size);
}
default:
llerrs << "Unsupported block type!" << llendl;
output_size = 0;
return NULL;
}
}
U8 *LLBlockEncoder::encodeU32(const LLBlockDataU32 &block_data, U32 &output_size) const
{
// OK, for now, just use the standard KDU encoder, with a single channel
// integer channel.
// Collect simple arguments.
bool allow_rate_prediction, allow_shorts, mem, quiet, no_weights;
allow_rate_prediction = true;
allow_shorts = false;
no_weights = false;
bool use_absolute = false;
mem = false;
quiet = false;
// Set codestream options
siz_params siz;
S16 precision = block_data.getPrecision();
siz.set(Sdims,0,0,(U16)block_data.getHeight());
siz.set(Sdims,0,1,(U16)block_data.getWidth());
siz.set(Ssigned,0,0,false);
siz.set(Scomponents,0,0,1);
siz.set(Sprecision,0,0, precision);
// Construct the `kdu_codestream' object and parse all remaining arguments.
output_size = block_data.getSize();
if (output_size < 1000)
{
output_size = 1000;
}
U8 *output_buffer = new U8[output_size];
LLKDUMemTarget output(output_buffer, output_size, block_data.getSize());
kdu_codestream codestream;
codestream.create(&siz,&output);
codestream.access_siz()->parse_string("Clayers=1");
codestream.access_siz()->finalize_all();
kdu_tile tile = codestream.open_tile(kdu_coords(0,0));
// Open tile-components and create processing engines and resources
kdu_dims dims;
kdu_sample_allocator allocator;
kdu_tile_comp tile_comp;
kdu_line_buf line;
kdu_push_ifc engine;
tile_comp = tile.access_component(0);
kdu_resolution res = tile_comp.access_resolution(); // Get top resolution
res.get_dims(dims);
line.pre_create(&allocator,dims.size.x, use_absolute, allow_shorts);
if (res.which() == 0) // No DWT levels (should not occur in this example)
{
engine = kdu_encoder(res.access_subband(LL_BAND),&allocator, use_absolute);
}
else
{
engine = kdu_analysis(res,&allocator, use_absolute);
}
allocator.finalize(); // Actually creates buffering resources
line.create(); // Grabs resources from the allocator.
// Now walk through the lines of the buffer, pushing them into the
// relevant tile-component processing engines.
U32 *source_u32 = NULL;
F32 scale_inv = 1.f / (1 << precision);
S32 y;
for (y = 0; y < dims.size.y; y++)
{
source_u32 = (U32*)(block_data.getData() + y * block_data.getRowStride());
kdu_sample32 *dest = line.get_buf32();
for (S32 n=dims.size.x; n > 0; n--, dest++, source_u32++)
{
// Just pack it in, for now.
dest->fval = (F32)(*source_u32) * scale_inv - 0.5f;// - 0.5f;
}
engine.push(line, true);
}
// Cleanup
engine.destroy(); // engines are interfaces; no default destructors
// Produce the final compressed output.
kdu_long layer_bytes[1] = {0};
layer_bytes[0] = (kdu_long) (mBPP*block_data.getWidth()*block_data.getHeight());
// Here we are not requesting specific sizes for any of the 12
// quality layers. As explained in the description of
// "kdu_codestream::flush" (see "kdu_compressed.h"), the rate allocator
// will then assign the layers in such a way as to achieve roughly
// two quality layers per octave change in bit-rate, with the final
// layer reaching true lossless quality.
codestream.flush(layer_bytes,1);
// You can see how many bytes were assigned
// to each quality layer by looking at the entries of `layer_bytes' here.
// The flush function can do a lot of interesting things which you may
// want to spend some time looking into. In addition to targeting
// specific bit-rates for each quality layer, it can be configured to
// use rate-distortion slope thresholds of your choosing and to return
// the thresholds which it finds to be best for any particular set of
// target layer sizes. This opens the door to feedback-oriented rate
// control for video. You should also look into the
// "kdu_codestream::set_max_bytes" and
// "kdu_codestream::set_min_slope_threshold" functions which can be
// used to significantly speed up compression.
codestream.destroy(); // All done: simple as that.
// Now that we're done encoding, create the new data buffer for the compressed
// image and stick it there.
U8 *output_data = new U8[output_size];
memcpy(output_data, output_buffer, output_size);
output.close(); // Not really necessary here.
return output_data;
}
U8 *LLBlockEncoder::encodeF32(const LLBlockDataF32 &block_data, U32 &output_size) const
{
// OK, for now, just use the standard KDU encoder, with a single channel
// integer channel.
// Collect simple arguments.
bool allow_rate_prediction, allow_shorts, mem, quiet, no_weights;
allow_rate_prediction = true;
allow_shorts = false;
no_weights = false;
bool use_absolute = false;
mem = false;
quiet = false;
F32 min, max, range, range_inv, offset;
min = block_data.getMin();
max = block_data.getMax();
range = max - min;
range_inv = 1.f / range;
offset = 0.5f*(max + min);
// Set codestream options
siz_params siz;
S16 precision = block_data.getPrecision(); // Assume precision is always 32 bits for floating point.
siz.set(Sdims,0,0,(U16)block_data.getHeight());
siz.set(Sdims,0,1,(U16)block_data.getWidth());
siz.set(Ssigned,0,0,false);
siz.set(Scomponents,0,0,1);
siz.set(Sprecision,0,0, precision);
// Construct the `kdu_codestream' object and parse all remaining arguments.
output_size = block_data.getSize();
if (output_size < 1000)
{
output_size = 1000;
}
U8 *output_buffer = new U8[output_size*2];
LLKDUMemTarget output(output_buffer, output_size, block_data.getSize());
kdu_codestream codestream;
codestream.create(&siz,&output);
codestream.access_siz()->parse_string("Clayers=1");
codestream.access_siz()->finalize_all();
kdu_tile tile = codestream.open_tile(kdu_coords(0,0));
// Open tile-components and create processing engines and resources
kdu_dims dims;
kdu_sample_allocator allocator;
kdu_tile_comp tile_comp;
kdu_line_buf line;
kdu_push_ifc engine;
tile_comp = tile.access_component(0);
kdu_resolution res = tile_comp.access_resolution(); // Get top resolution
res.get_dims(dims);
line.pre_create(&allocator,dims.size.x, use_absolute, allow_shorts);
if (res.which() == 0) // No DWT levels (should not occur in this example)
{
engine = kdu_encoder(res.access_subband(LL_BAND),&allocator, use_absolute);
}
else
{
engine = kdu_analysis(res,&allocator, use_absolute);
}
allocator.finalize(); // Actually creates buffering resources
line.create(); // Grabs resources from the allocator.
// Now walk through the lines of the buffer, pushing them into the
// relevant tile-component processing engines.
F32 *source_f32 = NULL;
S32 y;
for (y = 0; y < dims.size.y; y++)
{
source_f32 = (F32*)(block_data.getData() + y * block_data.getRowStride());
kdu_sample32 *dest = line.get_buf32();
for (S32 n=dims.size.x; n > 0; n--, dest++, source_f32++)
{
dest->fval = ((*source_f32) - offset) * range_inv;
}
engine.push(line, true);
}
// Cleanup
engine.destroy(); // engines are interfaces; no default destructors
// Produce the final compressed output.
kdu_long layer_bytes[1] = {0};
layer_bytes[0] = (kdu_long) (mBPP*block_data.getWidth()*block_data.getHeight());
// Here we are not requesting specific sizes for any of the 12
// quality layers. As explained in the description of
// "kdu_codestream::flush" (see "kdu_compressed.h"), the rate allocator
// will then assign the layers in such a way as to achieve roughly
// two quality layers per octave change in bit-rate, with the final
// layer reaching true lossless quality.
codestream.flush(layer_bytes,1);
// You can see how many bytes were assigned
// to each quality layer by looking at the entries of `layer_bytes' here.
// The flush function can do a lot of interesting things which you may
// want to spend some time looking into. In addition to targeting
// specific bit-rates for each quality layer, it can be configured to
// use rate-distortion slope thresholds of your choosing and to return
// the thresholds which it finds to be best for any particular set of
// target layer sizes. This opens the door to feedback-oriented rate
// control for video. You should also look into the
// "kdu_codestream::set_max_bytes" and
// "kdu_codestream::set_min_slope_threshold" functions which can be
// used to significantly speed up compression.
codestream.destroy(); // All done: simple as that.
// Now that we're done encoding, create the new data buffer for the compressed
// image and stick it there.
U8 *output_data = new U8[output_size];
memcpy(output_data, output_buffer, output_size);
output.close(); // Not really necessary here.
delete[] output_buffer;
return output_data;
}
void LLBlockEncoder::setBPP(const F32 bpp)
{
mBPP = bpp;
}
|