| 1 |
//***************************************************************************/ |
| 2 |
// This software is released under the 2-Clause BSD license, included |
| 3 |
// below. |
| 4 |
// |
| 5 |
// Copyright (c) 2019, Aous Naman |
| 6 |
// Copyright (c) 2019, Kakadu Software Pty Ltd, Australia |
| 7 |
// Copyright (c) 2019, The University of New South Wales, Australia |
| 8 |
// |
| 9 |
// Redistribution and use in source and binary forms, with or without |
| 10 |
// modification, are permitted provided that the following conditions are |
| 11 |
// met: |
| 12 |
// |
| 13 |
// 1. Redistributions of source code must retain the above copyright |
| 14 |
// notice, this list of conditions and the following disclaimer. |
| 15 |
// |
| 16 |
// 2. Redistributions in binary form must reproduce the above copyright |
| 17 |
// notice, this list of conditions and the following disclaimer in the |
| 18 |
// documentation and/or other materials provided with the distribution. |
| 19 |
// |
| 20 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 21 |
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 |
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 23 |
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 |
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 26 |
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 |
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 |
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 |
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
//***************************************************************************/ |
| 32 |
// This file is part of the OpenJPH software implementation. |
| 33 |
// File: ojph_block_encoder.cpp |
| 34 |
// Author: Aous Naman |
| 35 |
// Date: 17 September 2019 |
| 36 |
//***************************************************************************/ |
| 37 |
|
| 38 |
//***************************************************************************/ |
| 39 |
/** @file ojph_block_encoder.cpp |
| 40 |
* @brief implements HTJ2K block encoder |
| 41 |
*/ |
| 42 |
|
| 43 |
#include <cassert> |
| 44 |
#include <cstring> |
| 45 |
#include <cstdint> |
| 46 |
#include <climits> |
| 47 |
#include "grok.h" |
| 48 |
#include "logger.h" |
| 49 |
|
| 50 |
#include "ojph_mem.h" |
| 51 |
#include "ojph_arch.h" |
| 52 |
#include "ojph_block_encoder.h" |
| 53 |
|
| 54 |
namespace ojph { |
| 55 |
namespace local { |
| 56 |
|
| 57 |
///////////////////////////////////////////////////////////////////////// |
| 58 |
// tables |
| 59 |
///////////////////////////////////////////////////////////////////////// |
| 60 |
|
| 61 |
//VLC encoding |
| 62 |
// index is (c_q << 8) + (rho << 4) + eps |
| 63 |
// data is (cwd << 8) + (cwd_len << 4) + eps |
| 64 |
// table 0 is for the initial line of quads |
| 65 |
static ui16 vlc_tbl0[2048] = { 0 }; |
| 66 |
static ui16 vlc_tbl1[2048] = { 0 }; |
| 67 |
|
| 68 |
//UVLC encoding |
| 69 |
static int ulvc_cwd_pre[33]; |
| 70 |
static int ulvc_cwd_pre_len[33]; |
| 71 |
static int ulvc_cwd_suf[33]; |
| 72 |
static int ulvc_cwd_suf_len[33]; |
| 73 |
|
| 74 |
///////////////////////////////////////////////////////////////////////// |
| 75 |
static bool vlc_init_tables() |
| 76 |
{ |
| 77 |
struct vlc_src_table { int c_q, rho, u_off, e_k, e_1, cwd, cwd_len; }; |
| 78 |
vlc_src_table tbl0[] = { |
| 79 |
#include "table0.h" |
| 80 |
}; |
| 81 |
size_t tbl0_size = sizeof(tbl0) / sizeof(vlc_src_table); |
| 82 |
|
| 83 |
si32 pattern_popcnt[16]; |
| 84 |
for (ui32 i = 0; i < 16; ++i) |
| 85 |
pattern_popcnt[i] = (si32)population_count(i); |
| 86 |
|
| 87 |
vlc_src_table* src_tbl = tbl0; |
| 88 |
ui16 *tgt_tbl = vlc_tbl0; |
| 89 |
size_t tbl_size = tbl0_size; |
| 90 |
for (int i = 0; i < 2048; ++i) |
| 91 |
{ |
| 92 |
int c_q = i >> 8, rho = (i >> 4) & 0xF, emb = i & 0xF; |
| 93 |
if (((emb & rho) != emb) || (rho == 0 && c_q == 0)) |
| 94 |
tgt_tbl[i] = 0; |
| 95 |
else |
| 96 |
{ |
| 97 |
vlc_src_table *best_entry = NULL; |
| 98 |
if (emb) // u_off = 1 |
| 99 |
{ |
| 100 |
int best_e_k = -1; |
| 101 |
for (size_t j = 0; j < tbl_size; ++j) |
| 102 |
{ |
| 103 |
if (src_tbl[j].c_q == c_q && src_tbl[j].rho == rho) |
| 104 |
if (src_tbl[j].u_off == 1) |
| 105 |
if ((emb & src_tbl[j].e_k) == src_tbl[j].e_1) |
| 106 |
{ |
| 107 |
//now we need to find the smallest cwd with the highest |
| 108 |
// number of bits set in e_k |
| 109 |
int ones_count = pattern_popcnt[src_tbl[j].e_k]; |
| 110 |
if (ones_count >= best_e_k) |
| 111 |
{ |
| 112 |
best_entry = src_tbl + j; |
| 113 |
best_e_k = ones_count; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
else // u_off = 0 |
| 119 |
{ |
| 120 |
for (size_t j = 0; j < tbl_size; ++j) |
| 121 |
{ |
| 122 |
if (src_tbl[j].c_q == c_q && src_tbl[j].rho == rho) |
| 123 |
if (src_tbl[j].u_off == 0) |
| 124 |
{ |
| 125 |
best_entry = src_tbl + j; |
| 126 |
break; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
assert(best_entry); |
| 131 |
tgt_tbl[i] = (ui16)((best_entry->cwd<<8) + (best_entry->cwd_len<<4) |
| 132 |
+ best_entry->e_k); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
vlc_src_table tbl1[] = { |
| 137 |
#include "table1.h" |
| 138 |
}; |
| 139 |
size_t tbl1_size = sizeof(tbl1) / sizeof(vlc_src_table); |
| 140 |
|
| 141 |
src_tbl = tbl1; |
| 142 |
tgt_tbl = vlc_tbl1; |
| 143 |
tbl_size = tbl1_size; |
| 144 |
for (int i = 0; i < 2048; ++i) |
| 145 |
{ |
| 146 |
int c_q = i >> 8, rho = (i >> 4) & 0xF, emb = i & 0xF; |
| 147 |
if (((emb & rho) != emb) || (rho == 0 && c_q == 0)) |
| 148 |
tgt_tbl[i] = 0; |
| 149 |
else |
| 150 |
{ |
| 151 |
vlc_src_table *best_entry = NULL; |
| 152 |
if (emb) // u_off = 1 |
| 153 |
{ |
| 154 |
int best_e_k = -1; |
| 155 |
for (size_t j = 0; j < tbl_size; ++j) |
| 156 |
{ |
| 157 |
if (src_tbl[j].c_q == c_q && src_tbl[j].rho == rho) |
| 158 |
if (src_tbl[j].u_off == 1) |
| 159 |
if ((emb & src_tbl[j].e_k) == src_tbl[j].e_1) |
| 160 |
{ |
| 161 |
//now we need to find the smallest cwd with the highest |
| 162 |
// number of bits set in e_k |
| 163 |
int ones_count = pattern_popcnt[src_tbl[j].e_k]; |
| 164 |
if (ones_count >= best_e_k) |
| 165 |
{ |
| 166 |
best_entry = src_tbl + j; |
| 167 |
best_e_k = ones_count; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
else // u_off = 0 |
| 173 |
{ |
| 174 |
for (size_t j = 0; j < tbl_size; ++j) |
| 175 |
{ |
| 176 |
if (src_tbl[j].c_q == c_q && src_tbl[j].rho == rho) |
| 177 |
if (src_tbl[j].u_off == 0) |
| 178 |
{ |
| 179 |
best_entry = src_tbl + j; |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
assert(best_entry); |
| 185 |
tgt_tbl[i] = (ui16)((best_entry->cwd<<8) + (best_entry->cwd_len<<4) |
| 186 |
+ best_entry->e_k); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
///////////////////////////////////////////////////////////////////////// |
| 195 |
static bool uvlc_init_tables() |
| 196 |
{ |
| 197 |
//code goes from 0 to 31, extension and 32 are not supported here |
| 198 |
ulvc_cwd_pre[0] = 0; ulvc_cwd_pre[1] = 1; ulvc_cwd_pre[2] = 2; |
| 199 |
ulvc_cwd_pre[3] = 4; ulvc_cwd_pre[4] = 4; |
| 200 |
ulvc_cwd_pre_len[0] = 0; ulvc_cwd_pre_len[1] = 1; |
| 201 |
ulvc_cwd_pre_len[2] = 2; |
| 202 |
ulvc_cwd_pre_len[3] = 3; ulvc_cwd_pre_len[4] = 3; |
| 203 |
ulvc_cwd_suf[0] = 0; ulvc_cwd_suf[1] = 0; ulvc_cwd_suf[2] = 0; |
| 204 |
ulvc_cwd_suf[3] = 0; ulvc_cwd_suf[4] = 1; |
| 205 |
ulvc_cwd_suf_len[0] = 0; ulvc_cwd_suf_len[1] = 0; |
| 206 |
ulvc_cwd_suf_len[2] = 0; |
| 207 |
ulvc_cwd_suf_len[3] = 1; ulvc_cwd_suf_len[4] = 1; |
| 208 |
for (int i = 5; i < 33; ++i) |
| 209 |
{ |
| 210 |
ulvc_cwd_pre[i] = 0; |
| 211 |
ulvc_cwd_pre_len[i] = 3; |
| 212 |
ulvc_cwd_suf[i] = i-5; |
| 213 |
ulvc_cwd_suf_len[i] = 5; |
| 214 |
} |
| 215 |
return true; |
| 216 |
} |
| 217 |
|
| 218 |
///////////////////////////////////////////////////////////////////////// |
| 219 |
static bool vlc_tables_initialized = vlc_init_tables(); |
| 220 |
static bool uvlc_tables_initialized = uvlc_init_tables(); |
| 221 |
|
| 222 |
///////////////////////////////////////////////////////////////////////// |
| 223 |
// |
| 224 |
///////////////////////////////////////////////////////////////////////// |
| 225 |
struct mel_struct { |
| 226 |
//storage |
| 227 |
ui8* buf; //pointer to data buffer |
| 228 |
ui32 pos; //position of next writing within buf |
| 229 |
ui32 buf_size; //size of buffer, which we must not exceed |
| 230 |
|
| 231 |
// all these can be replaced by bytes |
| 232 |
int remaining_bits; //number of empty bits in tmp |
| 233 |
int tmp; //temporary storage of coded bits |
| 234 |
int run; //number of 0 run |
| 235 |
int k; //state |
| 236 |
int threshold; //threshold where one bit must be coded |
| 237 |
}; |
| 238 |
|
| 239 |
////////////////////////////////////////////////////////////////////////// |
| 240 |
static inline void |
| 241 |
mel_init(mel_struct* melp, ui32 buffer_size, ui8* data) |
| 242 |
{ |
| 243 |
melp->buf = data; |
| 244 |
melp->pos = 0; |
| 245 |
melp->buf_size = buffer_size; |
| 246 |
melp->remaining_bits = 8; |
| 247 |
melp->tmp = 0; |
| 248 |
melp->run = 0; |
| 249 |
melp->k = 0; |
| 250 |
melp->threshold = 1; // this is 1 << mel_exp[melp->k]; |
| 251 |
} |
| 252 |
|
| 253 |
////////////////////////////////////////////////////////////////////////// |
| 254 |
static inline void |
| 255 |
mel_emit_bit(mel_struct* melp, int v) |
| 256 |
{ |
| 257 |
assert(v == 0 || v == 1); |
| 258 |
melp->tmp = (melp->tmp << 1) + v; |
| 259 |
melp->remaining_bits--; |
| 260 |
if (melp->remaining_bits == 0) |
| 261 |
{ |
| 262 |
if (melp->pos >= melp->buf_size) |
| 263 |
grk::GRK_ERROR( "mel encoder's buffer is full"); |
| 264 |
|
| 265 |
melp->buf[melp->pos++] = (ui8)melp->tmp; |
| 266 |
melp->remaining_bits = (melp->tmp == 0xFF ? 7 : 8); |
| 267 |
melp->tmp = 0; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
////////////////////////////////////////////////////////////////////////// |
| 272 |
static inline void |
| 273 |
mel_encode(mel_struct* melp, bool bit) |
| 274 |
{ |
| 275 |
//MEL exponent |
| 276 |
static const int mel_exp[13] = {0,0,0,1,1,1,2,2,2,3,3,4,5}; |
| 277 |
|
| 278 |
if (bit == false) |
| 279 |
{ |
| 280 |
++melp->run; |
| 281 |
if (melp->run >= melp->threshold) |
| 282 |
{ |
| 283 |
mel_emit_bit(melp, 1); |
| 284 |
melp->run = 0; |
| 285 |
melp->k = ojph_min(12, melp->k + 1); |
| 286 |
melp->threshold = 1 << mel_exp[melp->k]; |
| 287 |
} |
| 288 |
} |
| 289 |
else |
| 290 |
{ |
| 291 |
mel_emit_bit(melp, 0); |
| 292 |
int t = mel_exp[melp->k]; |
| 293 |
while (t > 0) |
| 294 |
mel_emit_bit(melp, (melp->run >> --t) & 1); |
| 295 |
melp->run = 0; |
| 296 |
melp->k = ojph_max(0, melp->k - 1); |
| 297 |
melp->threshold = 1 << mel_exp[melp->k]; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
///////////////////////////////////////////////////////////////////////// |
| 302 |
// |
| 303 |
///////////////////////////////////////////////////////////////////////// |
| 304 |
struct vlc_struct { |
| 305 |
//storage |
| 306 |
ui8* buf; //pointer to data buffer |
| 307 |
ui32 pos; //position of next writing within buf |
| 308 |
ui32 buf_size; //size of buffer, which we must not exceed |
| 309 |
|
| 310 |
int used_bits; //number of occupied bits in tmp |
| 311 |
int tmp; //temporary storage of coded bits |
| 312 |
bool last_greater_than_8F; //true if last byte us greater than 0x8F |
| 313 |
}; |
| 314 |
|
| 315 |
////////////////////////////////////////////////////////////////////////// |
| 316 |
static inline void |
| 317 |
vlc_init(vlc_struct* vlcp, ui32 buffer_size, ui8* data) |
| 318 |
{ |
| 319 |
vlcp->buf = data + buffer_size - 1; //points to last byte |
| 320 |
vlcp->pos = 1; //locations will be all -pos |
| 321 |
vlcp->buf_size = buffer_size; |
| 322 |
|
| 323 |
vlcp->buf[0] = 0xFF; |
| 324 |
vlcp->used_bits = 4; |
| 325 |
vlcp->tmp = 0xF; |
| 326 |
vlcp->last_greater_than_8F = true; |
| 327 |
} |
| 328 |
|
| 329 |
////////////////////////////////////////////////////////////////////////// |
| 330 |
static inline void |
| 331 |
vlc_encode(vlc_struct* vlcp, int cwd, int cwd_len) |
| 332 |
{ |
| 333 |
while (cwd_len > 0) |
| 334 |
{ |
| 335 |
if (vlcp->pos >= vlcp->buf_size) |
| 336 |
grk::GRK_ERROR( "vlc encoder's buffer is full"); |
| 337 |
|
| 338 |
int avail_bits = 8 - vlcp->last_greater_than_8F - vlcp->used_bits; |
| 339 |
int t = ojph_min(avail_bits, cwd_len); |
| 340 |
vlcp->tmp |= (cwd & ((1 << t) - 1)) << vlcp->used_bits; |
| 341 |
vlcp->used_bits += t; |
| 342 |
avail_bits -= t; |
| 343 |
cwd_len -= t; |
| 344 |
cwd >>= t; |
| 345 |
if (avail_bits == 0) |
| 346 |
{ |
| 347 |
if (vlcp->last_greater_than_8F && vlcp->tmp != 0x7F) |
| 348 |
{ |
| 349 |
vlcp->last_greater_than_8F = false; |
| 350 |
continue; //one empty bit remaining |
| 351 |
} |
| 352 |
*(vlcp->buf - vlcp->pos) = (ui8)(vlcp->tmp); |
| 353 |
vlcp->pos++; |
| 354 |
vlcp->last_greater_than_8F = vlcp->tmp > 0x8F; |
| 355 |
vlcp->tmp = 0; |
| 356 |
vlcp->used_bits = 0; |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
////////////////////////////////////////////////////////////////////////// |
| 362 |
// |
| 363 |
////////////////////////////////////////////////////////////////////////// |
| 364 |
static inline void |
| 365 |
terminate_mel_vlc(mel_struct* melp, vlc_struct* vlcp) |
| 366 |
{ |
| 367 |
if (melp->run > 0) |
| 368 |
mel_emit_bit(melp, 1); |
| 369 |
|
| 370 |
melp->tmp = melp->tmp << melp->remaining_bits; |
| 371 |
int mel_mask = (0xFF << melp->remaining_bits) & 0xFF; |
| 372 |
int vlc_mask = 0xFF >> (8 - vlcp->used_bits); |
| 373 |
if ((mel_mask | vlc_mask) == 0) |
| 374 |
return; //last mel byte cannot be 0xFF, since then |
| 375 |
//melp->remaining_bits would be < 8 |
| 376 |
if (melp->pos >= melp->buf_size) |
| 377 |
grk::GRK_ERROR( "mel encoder's buffer is full"); |
| 378 |
int fuse = melp->tmp | vlcp->tmp; |
| 379 |
if ( ( ((fuse ^ melp->tmp) & mel_mask) |
| 380 |
| ((fuse ^ vlcp->tmp) & vlc_mask) ) == 0 |
| 381 |
&& (fuse != 0xFF) && vlcp->pos > 1) |
| 382 |
{ |
| 383 |
melp->buf[melp->pos++] = (ui8)fuse; |
| 384 |
} |
| 385 |
else |
| 386 |
{ |
| 387 |
if (vlcp->pos >= vlcp->buf_size) |
| 388 |
grk::GRK_ERROR( "vlc encoder's buffer is full"); |
| 389 |
melp->buf[melp->pos++] = (ui8)melp->tmp; //melp->tmp cannot be 0xFF |
| 390 |
*(vlcp->buf - vlcp->pos) = (ui8)vlcp->tmp; |
| 391 |
vlcp->pos++; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
///////////////////////////////////////////////////////////////////////// |
| 396 |
// |
| 397 |
///////////////////////////////////////////////////////////////////////// |
| 398 |
struct ms_struct { |
| 399 |
//storage |
| 400 |
ui8* buf; //pointer to data buffer |
| 401 |
ui32 pos; //position of next writing within buf |
| 402 |
ui32 buf_size; //size of buffer, which we must not exceed |
| 403 |
|
| 404 |
int max_bits; //maximum number of bits that can be store in tmp |
| 405 |
int used_bits; //number of occupied bits in tmp |
| 406 |
ui32 tmp; //temporary storage of coded bits |
| 407 |
}; |
| 408 |
|
| 409 |
////////////////////////////////////////////////////////////////////////// |
| 410 |
static inline void |
| 411 |
ms_init(ms_struct* msp, ui32 buffer_size, ui8* data) |
| 412 |
{ |
| 413 |
msp->buf = data; |
| 414 |
msp->pos = 0; |
| 415 |
msp->buf_size = buffer_size; |
| 416 |
msp->max_bits = 8; |
| 417 |
msp->used_bits = 0; |
| 418 |
msp->tmp = 0; |
| 419 |
} |
| 420 |
|
| 421 |
////////////////////////////////////////////////////////////////////////// |
| 422 |
static inline void |
| 423 |
ms_encode(ms_struct* msp, ui32 cwd, int cwd_len) |
| 424 |
{ |
| 425 |
while (cwd_len > 0) |
| 426 |
{ |
| 427 |
if (msp->pos >= msp->buf_size) |
| 428 |
grk::GRK_ERROR( "magnitude sign encoder's buffer is full"); |
| 429 |
int t = ojph_min(msp->max_bits - msp->used_bits, cwd_len); |
| 430 |
msp->tmp |= (cwd & ((1U << t) - 1)) << msp->used_bits; |
| 431 |
msp->used_bits += t; |
| 432 |
cwd >>= t; |
| 433 |
cwd_len -= t; |
| 434 |
if (msp->used_bits >= msp->max_bits) |
| 435 |
{ |
| 436 |
msp->buf[msp->pos++] = (ui8)msp->tmp; |
| 437 |
msp->max_bits = (msp->tmp == 0xFF) ? 7 : 8; |
| 438 |
msp->tmp = 0; |
| 439 |
msp->used_bits = 0; |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
////////////////////////////////////////////////////////////////////////// |
| 445 |
static inline void |
| 446 |
ms_terminate(ms_struct* msp) |
| 447 |
{ |
| 448 |
if (msp->used_bits) |
| 449 |
{ |
| 450 |
int t = msp->max_bits - msp->used_bits; //unused bits |
| 451 |
msp->tmp |= (0xFF & ((1U << t) - 1)) << msp->used_bits; |
| 452 |
msp->used_bits += t; |
| 453 |
if (msp->tmp != 0xFF) |
| 454 |
{ |
| 455 |
if (msp->pos >= msp->buf_size) |
| 456 |
grk::GRK_ERROR( "magnitude sign encoder's buffer is full"); |
| 457 |
msp->buf[msp->pos++] = (ui8)msp->tmp; |
| 458 |
} |
| 459 |
} |
| 460 |
else if (msp->max_bits == 7) |
| 461 |
msp->pos--; |
| 462 |
} |
| 463 |
|
| 464 |
////////////////////////////////////////////////////////////////////////// |
| 465 |
// |
| 466 |
// |
| 467 |
// |
| 468 |
// |
| 469 |
// |
| 470 |
////////////////////////////////////////////////////////////////////////// |
| 471 |
void ojph_encode_codeblock(ui32* buf, ui32 missing_msbs, ui32 num_passes, |
| 472 |
ui32 width, ui32 height, ui32 stride, |
| 473 |
ui32* lengths, |
| 474 |
ojph::mem_elastic_allocator *elastic, |
| 475 |
ojph::coded_lists *& coded) |
| 476 |
{ |
| 477 |
assert(num_passes == 1); |
| 478 |
(void)num_passes; //currently not used |
| 479 |
const int ms_size = (16384*16+14)/15; //more than enough |
| 480 |
ui8 ms_buf[ms_size]; |
| 481 |
const int mel_vlc_size = 3072; //more than enough |
| 482 |
ui8 mel_vlc_buf[mel_vlc_size]; |
| 483 |
const int mel_size = 192; |
| 484 |
ui8 *mel_buf = mel_vlc_buf; |
| 485 |
const int vlc_size = mel_vlc_size - mel_size; |
| 486 |
ui8 *vlc_buf = mel_vlc_buf + mel_size; |
| 487 |
|
| 488 |
mel_struct mel; |
| 489 |
mel_init(&mel, mel_size, mel_buf); |
| 490 |
vlc_struct vlc; |
| 491 |
vlc_init(&vlc, vlc_size, vlc_buf); |
| 492 |
ms_struct ms; |
| 493 |
ms_init(&ms, ms_size, ms_buf); |
| 494 |
|
| 495 |
ui32 p = 30 - missing_msbs; |
| 496 |
|
| 497 |
//e_val: E values for a line (these are the highest set bit) |
| 498 |
//cx_val: is the context values |
| 499 |
//Each byte stores the info for the 2 sample. For E, it is maximum |
| 500 |
// of the two samples, while for cx, it is the OR of these two samples. |
| 501 |
//The maximum is between the pixel at the bottom left of one quad |
| 502 |
// and the bottom right of the earlier quad. The same is true for cx. |
| 503 |
//For a 1024 pixels, we need 512 bytes, the 2 extra, |
| 504 |
// one for the non-existing earlier quad, and one for beyond the |
| 505 |
// the end |
| 506 |
ui8 e_val[513]; |
| 507 |
ui8 cx_val[513]; |
| 508 |
ui8* lep = e_val; lep[0] = 0; |
| 509 |
ui8* lcxp = cx_val; lcxp[0] = 0; |
| 510 |
|
| 511 |
//initial row of quads |
| 512 |
int e_qmax[2] = {0,0}, e_q[8] = {0,0,0,0,0,0,0,0}; |
| 513 |
int rho[2] = {0,0}; |
| 514 |
int c_q0 = 0; |
| 515 |
ui32 s[8] = {0,0,0,0,0,0,0,0}, val, t; |
| 516 |
ui32 y = 0; |
| 517 |
ui32 *sp = buf; |
| 518 |
for (ui32 x = 0; x < width; x += 4) |
| 519 |
{ |
| 520 |
//prepare two quads |
| 521 |
t = sp[0]; |
| 522 |
val = t + t; //multiply by 2 and get rid of sign |
| 523 |
val >>= p; // 2 \mu_p + x |
| 524 |
val &= ~1u; // 2 \mu_p |
| 525 |
if (val) |
| 526 |
{ |
| 527 |
rho[0] = 1; |
| 528 |
e_q[0] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 529 |
e_qmax[0] = e_q[0]; |
| 530 |
s[0] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 531 |
} |
| 532 |
|
| 533 |
t = height > 1 ? sp[stride] : 0; |
| 534 |
++sp; |
| 535 |
val = t + t; //multiply by 2 and get rid of sign |
| 536 |
val >>= p; // 2 \mu_p + x |
| 537 |
val &= ~1u;// 2 \mu_p |
| 538 |
if (val) |
| 539 |
{ |
| 540 |
rho[0] += 2; |
| 541 |
e_q[1] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 542 |
e_qmax[0] = ojph_max(e_qmax[0], e_q[1]); |
| 543 |
s[1] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 544 |
} |
| 545 |
|
| 546 |
if (x+1 < width) |
| 547 |
{ |
| 548 |
t = sp[0]; |
| 549 |
val = t + t; //multiply by 2 and get rid of sign |
| 550 |
val >>= p; // 2 \mu_p + x |
| 551 |
val &= ~1u;// 2 \mu_p |
| 552 |
if (val) |
| 553 |
{ |
| 554 |
rho[0] += 4; |
| 555 |
e_q[2] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 556 |
e_qmax[0] = ojph_max(e_qmax[0], e_q[2]); |
| 557 |
s[2] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 558 |
} |
| 559 |
|
| 560 |
t = height > 1 ? sp[stride] : 0; |
| 561 |
++sp; |
| 562 |
val = t + t; //multiply by 2 and get rid of sign |
| 563 |
val >>= p; // 2 \mu_p + x |
| 564 |
val &= ~1u;// 2 \mu_p |
| 565 |
if (val) |
| 566 |
{ |
| 567 |
rho[0] += 8; |
| 568 |
e_q[3] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 569 |
e_qmax[0] = ojph_max(e_qmax[0], e_q[3]); |
| 570 |
s[3] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
int Uq0 = ojph_max(e_qmax[0], 1); //kappa_q = 1 |
| 575 |
int u_q0 = Uq0 - 1, u_q1 = 0; //kappa_q = 1 |
| 576 |
|
| 577 |
int eps0 = 0; |
| 578 |
if (u_q0 > 0) |
| 579 |
{ |
| 580 |
eps0 |= (e_q[0] == e_qmax[0]); |
| 581 |
eps0 |= (e_q[1] == e_qmax[0]) << 1; |
| 582 |
eps0 |= (e_q[2] == e_qmax[0]) << 2; |
| 583 |
eps0 |= (e_q[3] == e_qmax[0]) << 3; |
| 584 |
} |
| 585 |
lep[0] = ojph_max(lep[0], (ui8)e_q[1]); lep++; |
| 586 |
lep[0] = (ui8)e_q[3]; |
| 587 |
lcxp[0] = (ui8)(lcxp[0] | (ui8)((rho[0] & 2) >> 1)); lcxp++; |
| 588 |
lcxp[0] = (ui8)((rho[0] & 8) >> 3); |
| 589 |
|
| 590 |
ui16 tuple0 = vlc_tbl0[(c_q0 << 8) + (rho[0] << 4) + eps0]; |
| 591 |
vlc_encode(&vlc, tuple0 >> 8, (tuple0 >> 4) & 7); |
| 592 |
|
| 593 |
if (c_q0 == 0) |
| 594 |
mel_encode(&mel, rho[0] != 0); |
| 595 |
|
| 596 |
int m = (rho[0] & 1) ? Uq0 - (tuple0 & 1) : 0; |
| 597 |
ms_encode(&ms, s[0] & ((1U<<m)-1), m); |
| 598 |
m = (rho[0] & 2) ? Uq0 - ((tuple0 & 2) >> 1) : 0; |
| 599 |
ms_encode(&ms, s[1] & ((1U<<m)-1), m); |
| 600 |
m = (rho[0] & 4) ? Uq0 - ((tuple0 & 4) >> 2) : 0; |
| 601 |
ms_encode(&ms, s[2] & ((1U<<m)-1), m); |
| 602 |
m = (rho[0] & 8) ? Uq0 - ((tuple0 & 8) >> 3) : 0; |
| 603 |
ms_encode(&ms, s[3] & ((1U<<m)-1), m); |
| 604 |
|
| 605 |
if (x+2 < width) |
| 606 |
{ |
| 607 |
t = sp[0]; |
| 608 |
val = t + t; //multiply by 2 and get rid of sign |
| 609 |
val >>= p; // 2 \mu_p + x |
| 610 |
val &= ~1u;// 2 \mu_p |
| 611 |
if (val) |
| 612 |
{ |
| 613 |
rho[1] = 1; |
| 614 |
e_q[4] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 615 |
e_qmax[1] = e_q[4]; |
| 616 |
s[4] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 617 |
} |
| 618 |
|
| 619 |
t = height > 1 ? sp[stride] : 0; |
| 620 |
++sp; |
| 621 |
val = t + t; //multiply by 2 and get rid of sign |
| 622 |
val >>= p; // 2 \mu_p + x |
| 623 |
val &= ~1u;// 2 \mu_p |
| 624 |
if (val) |
| 625 |
{ |
| 626 |
rho[1] += 2; |
| 627 |
e_q[5] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 628 |
e_qmax[1] = ojph_max(e_qmax[1], e_q[5]); |
| 629 |
s[5] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 630 |
} |
| 631 |
|
| 632 |
if (x+3 < width) |
| 633 |
{ |
| 634 |
t = sp[0]; |
| 635 |
val = t + t; //multiply by 2 and get rid of sign |
| 636 |
val >>= p; // 2 \mu_p + x |
| 637 |
val &= ~1u;// 2 \mu_p |
| 638 |
if (val) |
| 639 |
{ |
| 640 |
rho[1] += 4; |
| 641 |
e_q[6] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 642 |
e_qmax[1] = ojph_max(e_qmax[1], e_q[6]); |
| 643 |
s[6] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 644 |
} |
| 645 |
|
| 646 |
t = height > 1 ? sp[stride] : 0; |
| 647 |
++sp; |
| 648 |
val = t + t; //multiply by 2 and get rid of sign |
| 649 |
val >>= p; // 2 \mu_p + x |
| 650 |
val &= ~1u;// 2 \mu_p |
| 651 |
if (val) |
| 652 |
{ |
| 653 |
rho[1] += 8; |
| 654 |
e_q[7] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 655 |
e_qmax[1] = ojph_max(e_qmax[1], e_q[7]); |
| 656 |
s[7] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
int c_q1 = (rho[0] >> 1) | (rho[0] & 1); |
| 661 |
int Uq1 = ojph_max(e_qmax[1], 1); //kappa_q = 1 |
| 662 |
u_q1 = Uq1 - 1; //kappa_q = 1 |
| 663 |
|
| 664 |
int eps1 = 0; |
| 665 |
if (u_q1 > 0) |
| 666 |
{ |
| 667 |
eps1 |= (e_q[4] == e_qmax[1]); |
| 668 |
eps1 |= (e_q[5] == e_qmax[1]) << 1; |
| 669 |
eps1 |= (e_q[6] == e_qmax[1]) << 2; |
| 670 |
eps1 |= (e_q[7] == e_qmax[1]) << 3; |
| 671 |
} |
| 672 |
lep[0] = ojph_max(lep[0], (ui8)e_q[5]); lep++; |
| 673 |
lep[0] = (ui8)e_q[7]; |
| 674 |
lcxp[0] |= (ui8)(lcxp[0] | (ui8)((rho[1] & 2) >> 1)); lcxp++; |
| 675 |
lcxp[0] = (ui8)((rho[1] & 8) >> 3); |
| 676 |
ui16 tuple1 = vlc_tbl0[(c_q1 << 8) + (rho[1] << 4) + eps1]; |
| 677 |
vlc_encode(&vlc, tuple1 >> 8, (tuple1 >> 4) & 7); |
| 678 |
|
| 679 |
if (c_q1 == 0) |
| 680 |
mel_encode(&mel, rho[1] != 0); |
| 681 |
|
| 682 |
int m = (rho[1] & 1) ? Uq1 - (tuple1 & 1) : 0; |
| 683 |
ms_encode(&ms, s[4] & ((1U<<m)-1), m); |
| 684 |
m = (rho[1] & 2) ? Uq1 - ((tuple1 & 2) >> 1) : 0; |
| 685 |
ms_encode(&ms, s[5] & ((1U<<m)-1), m); |
| 686 |
m = (rho[1] & 4) ? Uq1 - ((tuple1 & 4) >> 2) : 0; |
| 687 |
ms_encode(&ms, s[6] & ((1U<<m)-1), m); |
| 688 |
m = (rho[1] & 8) ? Uq1 - ((tuple1 & 8) >> 3) : 0; |
| 689 |
ms_encode(&ms, s[7] & ((1U<<m)-1), m); |
| 690 |
} |
| 691 |
|
| 692 |
if (u_q0 > 0 && u_q1 > 0) |
| 693 |
mel_encode(&mel, ojph_min(u_q0, u_q1) > 2); |
| 694 |
|
| 695 |
if (u_q0 > 2 && u_q1 > 2) |
| 696 |
{ |
| 697 |
vlc_encode(&vlc, ulvc_cwd_pre[u_q0-2], ulvc_cwd_pre_len[u_q0-2]); |
| 698 |
vlc_encode(&vlc, ulvc_cwd_pre[u_q1-2], ulvc_cwd_pre_len[u_q1-2]); |
| 699 |
vlc_encode(&vlc, ulvc_cwd_suf[u_q0-2], ulvc_cwd_suf_len[u_q0-2]); |
| 700 |
vlc_encode(&vlc, ulvc_cwd_suf[u_q1-2], ulvc_cwd_suf_len[u_q1-2]); |
| 701 |
} |
| 702 |
else if (u_q0 > 2 && u_q1 > 0) |
| 703 |
{ |
| 704 |
vlc_encode(&vlc, ulvc_cwd_pre[u_q0], ulvc_cwd_pre_len[u_q0]); |
| 705 |
vlc_encode(&vlc, u_q1 - 1, 1); |
| 706 |
vlc_encode(&vlc, ulvc_cwd_suf[u_q0], ulvc_cwd_suf_len[u_q0]); |
| 707 |
} |
| 708 |
else |
| 709 |
{ |
| 710 |
vlc_encode(&vlc, ulvc_cwd_pre[u_q0], ulvc_cwd_pre_len[u_q0]); |
| 711 |
vlc_encode(&vlc, ulvc_cwd_pre[u_q1], ulvc_cwd_pre_len[u_q1]); |
| 712 |
vlc_encode(&vlc, ulvc_cwd_suf[u_q0], ulvc_cwd_suf_len[u_q0]); |
| 713 |
vlc_encode(&vlc, ulvc_cwd_suf[u_q1], ulvc_cwd_suf_len[u_q1]); |
| 714 |
} |
| 715 |
|
| 716 |
//prepare for next iteration |
| 717 |
c_q0 = (rho[1] >> 1) | (rho[1] & 1); |
| 718 |
s[0] = s[1] = s[2] = s[3] = s[4] = s[5] = s[6] = s[7] = 0; |
| 719 |
e_q[0]=e_q[1]=e_q[2]=e_q[3]=e_q[4]=e_q[5]=e_q[6]=e_q[7]=0; |
| 720 |
rho[0] = rho[1] = 0; e_qmax[0] = e_qmax[1] = 0; |
| 721 |
} |
| 722 |
|
| 723 |
lep[1] = 0; |
| 724 |
|
| 725 |
for (y = 2; y < height; y += 2) |
| 726 |
{ |
| 727 |
lep = e_val; |
| 728 |
int max_e = ojph_max(lep[0], lep[1]) - 1; |
| 729 |
lep[0] = 0; |
| 730 |
lcxp = cx_val; |
| 731 |
c_q0 = lcxp[0] + (lcxp[1] << 2); |
| 732 |
lcxp[0] = 0; |
| 733 |
|
| 734 |
sp = buf + y * stride; |
| 735 |
for (ui32 x = 0; x < width; x += 4) |
| 736 |
{ |
| 737 |
//prepare two quads |
| 738 |
t = sp[0]; |
| 739 |
val = t + t; //multiply by 2 and get rid of sign |
| 740 |
val >>= p; // 2 \mu_p + x |
| 741 |
val &= ~1u;// 2 \mu_p |
| 742 |
if (val) |
| 743 |
{ |
| 744 |
rho[0] = 1; |
| 745 |
e_q[0] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 746 |
e_qmax[0] = e_q[0]; |
| 747 |
s[0] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 748 |
} |
| 749 |
|
| 750 |
t = y + 1 < height ? sp[stride] : 0; |
| 751 |
++sp; |
| 752 |
val = t + t; //multiply by 2 and get rid of sign |
| 753 |
val >>= p; // 2 \mu_p + x |
| 754 |
val &= ~1u;// 2 \mu_p |
| 755 |
if (val) |
| 756 |
{ |
| 757 |
rho[0] += 2; |
| 758 |
e_q[1] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 759 |
e_qmax[0] = ojph_max(e_qmax[0], e_q[1]); |
| 760 |
s[1] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 761 |
} |
| 762 |
|
| 763 |
if (x+1 < width) |
| 764 |
{ |
| 765 |
t = sp[0]; |
| 766 |
val = t + t; //multiply by 2 and get rid of sign |
| 767 |
val >>= p; // 2 \mu_p + x |
| 768 |
val &= ~1u;// 2 \mu_p |
| 769 |
if (val) |
| 770 |
{ |
| 771 |
rho[0] += 4; |
| 772 |
e_q[2] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 773 |
e_qmax[0] = ojph_max(e_qmax[0], e_q[2]); |
| 774 |
s[2] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 775 |
} |
| 776 |
|
| 777 |
t = y + 1 < height ? sp[stride] : 0; |
| 778 |
++sp; |
| 779 |
val = t + t; //multiply by 2 and get rid of sign |
| 780 |
val >>= p; // 2 \mu_p + x |
| 781 |
val &= ~1u;// 2 \mu_p |
| 782 |
if (val) |
| 783 |
{ |
| 784 |
rho[0] += 8; |
| 785 |
e_q[3] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 786 |
e_qmax[0] = ojph_max(e_qmax[0], e_q[3]); |
| 787 |
s[3] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
int kappa = (rho[0] & (rho[0]-1)) ? ojph_max(1,max_e) : 1; |
| 792 |
int Uq0 = ojph_max(e_qmax[0], kappa); |
| 793 |
int u_q0 = Uq0 - kappa, u_q1 = 0; |
| 794 |
|
| 795 |
int eps0 = 0; |
| 796 |
if (u_q0 > 0) |
| 797 |
{ |
| 798 |
eps0 |= (e_q[0] == e_qmax[0]); |
| 799 |
eps0 |= (e_q[1] == e_qmax[0]) << 1; |
| 800 |
eps0 |= (e_q[2] == e_qmax[0]) << 2; |
| 801 |
eps0 |= (e_q[3] == e_qmax[0]) << 3; |
| 802 |
} |
| 803 |
lep[0] = ojph_max(lep[0], (ui8)e_q[1]); lep++; |
| 804 |
max_e = ojph_max(lep[0], lep[1]) - 1; |
| 805 |
lep[0] = (ui8)e_q[3]; |
| 806 |
lcxp[0] = (ui8)(lcxp[0] | (ui8)((rho[0] & 2) >> 1)); lcxp++; |
| 807 |
int c_q1 = lcxp[0] + (lcxp[1] << 2); |
| 808 |
lcxp[0] = (ui8)((rho[0] & 8) >> 3); |
| 809 |
ui16 tuple0 = vlc_tbl1[(c_q0 << 8) + (rho[0] << 4) + eps0]; |
| 810 |
vlc_encode(&vlc, tuple0 >> 8, (tuple0 >> 4) & 7); |
| 811 |
|
| 812 |
if (c_q0 == 0) |
| 813 |
mel_encode(&mel, rho[0] != 0); |
| 814 |
|
| 815 |
int m = (rho[0] & 1) ? Uq0 - (tuple0 & 1) : 0; |
| 816 |
ms_encode(&ms, s[0] & ((1U<<m)-1), m); |
| 817 |
m = (rho[0] & 2) ? Uq0 - ((tuple0 & 2) >> 1) : 0; |
| 818 |
ms_encode(&ms, s[1] & ((1U<<m)-1), m); |
| 819 |
m = (rho[0] & 4) ? Uq0 - ((tuple0 & 4) >> 2) : 0; |
| 820 |
ms_encode(&ms, s[2] & ((1U<<m)-1), m); |
| 821 |
m = (rho[0] & 8) ? Uq0 - ((tuple0 & 8) >> 3) : 0; |
| 822 |
ms_encode(&ms, s[3] & ((1U<<m)-1), m); |
| 823 |
|
| 824 |
if (x+2 < width) |
| 825 |
{ |
| 826 |
t = sp[0]; |
| 827 |
val = t + t; //multiply by 2 and get rid of sign |
| 828 |
val >>= p; // 2 \mu_p + x |
| 829 |
val &= ~1u;// 2 \mu_p |
| 830 |
if (val) |
| 831 |
{ |
| 832 |
rho[1] = 1; |
| 833 |
e_q[4] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 834 |
e_qmax[1] = e_q[4]; |
| 835 |
s[4] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 836 |
} |
| 837 |
|
| 838 |
t = y + 1 < height ? sp[stride] : 0; |
| 839 |
++sp; |
| 840 |
val = t + t; //multiply by 2 and get rid of sign |
| 841 |
val >>= p; // 2 \mu_p + x |
| 842 |
val &= ~1u;// 2 \mu_p |
| 843 |
if (val) |
| 844 |
{ |
| 845 |
rho[1] += 2; |
| 846 |
e_q[5] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 847 |
e_qmax[1] = ojph_max(e_qmax[1], e_q[5]); |
| 848 |
s[5] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 849 |
} |
| 850 |
|
| 851 |
if (x+3 < width) |
| 852 |
{ |
| 853 |
t = sp[0]; |
| 854 |
val = t + t; //multiply by 2 and get rid of sign |
| 855 |
val >>= p; // 2 \mu_p + x |
| 856 |
val &= ~1u;// 2 \mu_p |
| 857 |
if (val) |
| 858 |
{ |
| 859 |
rho[1] += 4; |
| 860 |
e_q[6] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 861 |
e_qmax[1] = ojph_max(e_qmax[1], e_q[6]); |
| 862 |
s[6] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 863 |
} |
| 864 |
|
| 865 |
t = y + 1 < height ? sp[stride] : 0; |
| 866 |
++sp; |
| 867 |
val = t + t; //multiply by 2 and get rid of sign |
| 868 |
val >>= p; // 2 \mu_p + x |
| 869 |
val &= ~1u;// 2 \mu_p |
| 870 |
if (val) |
| 871 |
{ |
| 872 |
rho[1] += 8; |
| 873 |
e_q[7] = 32 - (int)count_leading_zeros(--val); //2\mu_p - 1 |
| 874 |
e_qmax[1] = ojph_max(e_qmax[1], e_q[7]); |
| 875 |
s[7] = --val + (t >> 31); //v_n = 2(\mu_p-1) + s_n |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
kappa = (rho[1] & (rho[1]-1)) ? ojph_max(1,max_e) : 1; |
| 880 |
c_q1 |= ((rho[0] & 4) >> 1) | ((rho[0] & 8) >> 2); |
| 881 |
int Uq1 = ojph_max(e_qmax[1], kappa); |
| 882 |
u_q1 = Uq1 - kappa; |
| 883 |
|
| 884 |
int eps1 = 0; |
| 885 |
if (u_q1 > 0) |
| 886 |
{ |
| 887 |
eps1 |= (e_q[4] == e_qmax[1]); |
| 888 |
eps1 |= (e_q[5] == e_qmax[1]) << 1; |
| 889 |
eps1 |= (e_q[6] == e_qmax[1]) << 2; |
| 890 |
eps1 |= (e_q[7] == e_qmax[1]) << 3; |
| 891 |
} |
| 892 |
lep[0] = ojph_max(lep[0], (ui8)e_q[5]); lep++; |
| 893 |
max_e = ojph_max(lep[0], lep[1]) - 1; |
| 894 |
lep[0] = (ui8)e_q[7]; |
| 895 |
lcxp[0] = (ui8)(lcxp[0] | (ui8)((rho[1] & 2) >> 1)); lcxp++; |
| 896 |
c_q0 = lcxp[0] + (lcxp[1] << 2); |
| 897 |
lcxp[0] = (ui8)((rho[1] & 8) >> 3); |
| 898 |
ui16 tuple1 = vlc_tbl1[(c_q1 << 8) + (rho[1] << 4) + eps1]; |
| 899 |
vlc_encode(&vlc, tuple1 >> 8, (tuple1 >> 4) & 7); |
| 900 |
|
| 901 |
if (c_q1 == 0) |
| 902 |
mel_encode(&mel, rho[1] != 0); |
| 903 |
|
| 904 |
int m = (rho[1] & 1) ? Uq1 - (tuple1 & 1) : 0; |
| 905 |
ms_encode(&ms, s[4] & ((1U<<m)-1), m); |
| 906 |
m = (rho[1] & 2) ? Uq1 - ((tuple1 & 2) >> 1) : 0; |
| 907 |
ms_encode(&ms, s[5] & ((1U<<m)-1), m); |
| 908 |
m = (rho[1] & 4) ? Uq1 - ((tuple1 & 4) >> 2) : 0; |
| 909 |
ms_encode(&ms, s[6] & ((1U<<m)-1), m); |
| 910 |
m = (rho[1] & 8) ? Uq1 - ((tuple1 & 8) >> 3) : 0; |
| 911 |
ms_encode(&ms, s[7] & ((1U<<m)-1), m); |
| 912 |
} |
| 913 |
|
| 914 |
vlc_encode(&vlc, ulvc_cwd_pre[u_q0], ulvc_cwd_pre_len[u_q0]); |
| 915 |
vlc_encode(&vlc, ulvc_cwd_pre[u_q1], ulvc_cwd_pre_len[u_q1]); |
| 916 |
vlc_encode(&vlc, ulvc_cwd_suf[u_q0], ulvc_cwd_suf_len[u_q0]); |
| 917 |
vlc_encode(&vlc, ulvc_cwd_suf[u_q1], ulvc_cwd_suf_len[u_q1]); |
| 918 |
|
| 919 |
//prepare for next iteration |
| 920 |
c_q0 |= ((rho[1] & 4) >> 1) | ((rho[1] & 8) >> 2); |
| 921 |
s[0] = s[1] = s[2] = s[3] = s[4] = s[5] = s[6] = s[7] = 0; |
| 922 |
e_q[0]=e_q[1]=e_q[2]=e_q[3]=e_q[4]=e_q[5]=e_q[6]=e_q[7]=0; |
| 923 |
rho[0] = rho[1] = 0; e_qmax[0] = e_qmax[1] = 0; |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
terminate_mel_vlc(&mel, &vlc); |
| 929 |
ms_terminate(&ms); |
| 930 |
|
| 931 |
//copy to elastic |
| 932 |
lengths[0] = mel.pos + vlc.pos + ms.pos; |
| 933 |
elastic->get_buffer(mel.pos + vlc.pos + ms.pos, coded); |
| 934 |
memcpy(coded->buf, ms.buf, ms.pos); |
| 935 |
memcpy(coded->buf + ms.pos, mel.buf, mel.pos); |
| 936 |
memcpy(coded->buf + ms.pos + mel.pos, vlc.buf - vlc.pos + 1, vlc.pos); |
| 937 |
|
| 938 |
// put in the interface locator word |
| 939 |
ui32 num_bytes = mel.pos + vlc.pos; |
| 940 |
coded->buf[lengths[0]-1] = (ui8)(num_bytes >> 4); |
| 941 |
coded->buf[lengths[0]-2] = coded->buf[lengths[0]-2] & 0xF0; |
| 942 |
coded->buf[lengths[0]-2] = |
| 943 |
(ui8)(coded->buf[lengths[0]-2] | (num_bytes & 0xF)); |
| 944 |
|
| 945 |
coded->avail_size -= lengths[0]; |
| 946 |
} |
| 947 |
} |
| 948 |
} |