| 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_decoder.cpp |
| 34 |
// Author: Aous Naman |
| 35 |
// Date: 13 May 2022 |
| 36 |
//***************************************************************************/ |
| 37 |
|
| 38 |
//***************************************************************************/ |
| 39 |
/** @file ojph_block_decoder.cpp |
| 40 |
* @brief implements a HTJ2K block decoder |
| 41 |
*/ |
| 42 |
|
| 43 |
#include <string> |
| 44 |
#include <iostream> |
| 45 |
|
| 46 |
#include <cassert> |
| 47 |
#include <cstring> |
| 48 |
#include "grok.h" |
| 49 |
#include "logger.h" |
| 50 |
|
| 51 |
#ifndef _MSC_VER |
| 52 |
#pragma GCC diagnostic push |
| 53 |
#pragma GCC diagnostic ignored "-Wconversion" |
| 54 |
#pragma GCC diagnostic ignored "-Wsign-conversion" |
| 55 |
#pragma GCC diagnostic ignored "-Wunused-parameter" |
| 56 |
#pragma GCC diagnostic ignored "-Wunused-variable" |
| 57 |
#pragma GCC diagnostic ignored "-Wsign-compare" |
| 58 |
#pragma GCC diagnostic ignored "-Wparentheses" |
| 59 |
#endif |
| 60 |
|
| 61 |
#include "ojph_block_common.h" |
| 62 |
#include "ojph_block_decoder.h" |
| 63 |
#include "ojph_arch.h" |
| 64 |
#include "ojph_message.h" |
| 65 |
|
| 66 |
namespace ojph { |
| 67 |
namespace local { |
| 68 |
|
| 69 |
//************************************************************************/ |
| 70 |
/** @brief MEL state structure for reading and decoding the MEL bitstream |
| 71 |
* |
| 72 |
* A number of events is decoded from the MEL bitstream ahead of time |
| 73 |
* and stored in run/num_runs. |
| 74 |
* Each run represents the number of zero events before a one event. |
| 75 |
*/ |
| 76 |
struct dec_mel_st { |
| 77 |
dec_mel_st() : data(NULL), tmp(0), bits(0), size(0), unstuff(false), |
| 78 |
k(0), num_runs(0), runs(0) |
| 79 |
{} |
| 80 |
// data decoding machinary |
| 81 |
ui8* data; //!<the address of data (or bitstream) |
| 82 |
ui64 tmp; //!<temporary buffer for read data |
| 83 |
int bits; //!<number of bits stored in tmp |
| 84 |
int size; //!<number of bytes in MEL code |
| 85 |
bool unstuff; //!<true if the next bit needs to be unstuffed |
| 86 |
int k; //!<state of MEL decoder |
| 87 |
|
| 88 |
// queue of decoded runs |
| 89 |
int num_runs; //!<number of decoded runs left in runs (maximum 8) |
| 90 |
ui64 runs; //!<runs of decoded MEL codewords (7 bits/run) |
| 91 |
}; |
| 92 |
|
| 93 |
//************************************************************************/ |
| 94 |
/** @brief Reads and unstuffs the MEL bitstream |
| 95 |
* |
| 96 |
* This design needs more bytes in the codeblock buffer than the length |
| 97 |
* of the cleanup pass by up to 2 bytes. |
| 98 |
* |
| 99 |
* Unstuffing removes the MSB of the byte following a byte whose |
| 100 |
* value is 0xFF; this prevents sequences larger than 0xFF7F in value |
| 101 |
* from appearing the bitstream. |
| 102 |
* |
| 103 |
* @param [in] melp is a pointer to dec_mel_st structure |
| 104 |
*/ |
| 105 |
static inline |
| 106 |
void mel_read(dec_mel_st *melp) |
| 107 |
{ |
| 108 |
if (melp->bits > 32) //there are enough bits in the tmp variable |
| 109 |
return; // return without reading new data |
| 110 |
|
| 111 |
ui32 val = 0xFFFFFFFF; // feed in 0xFF if buffer is exhausted |
| 112 |
if (melp->size > 4) { // if there is data in the MEL segment |
| 113 |
val = *(ui32*)melp->data; // read 32 bits from MEL data |
| 114 |
melp->data += 4; // advance pointer |
| 115 |
melp->size -= 4; // reduce counter |
| 116 |
} |
| 117 |
else if (melp->size > 0) |
| 118 |
{ // 4 or less |
| 119 |
int i = 0; |
| 120 |
while (melp->size > 1) { |
| 121 |
ui32 v = *melp->data++; // read one byte at a time |
| 122 |
ui32 m = ~(0xFFu << i); // mask of location |
| 123 |
val = (val & m) | (v << i);// put one byte in its correct location |
| 124 |
--melp->size; |
| 125 |
i += 8; |
| 126 |
} |
| 127 |
// size equal to 1 |
| 128 |
ui32 v = *melp->data++; // the one before the last is different |
| 129 |
v |= 0xF; // MEL and VLC segments can overlap |
| 130 |
ui32 m = ~(0xFFu << i); |
| 131 |
val = (val & m) | (v << i); |
| 132 |
--melp->size; |
| 133 |
} |
| 134 |
|
| 135 |
// next we unstuff them before adding them to the buffer |
| 136 |
int bits = 32 - melp->unstuff; // number of bits in val, subtract 1 if |
| 137 |
// the previously read byte requires |
| 138 |
// unstuffing |
| 139 |
|
| 140 |
// data is unstuffed and accumulated in t |
| 141 |
// bits has the number of bits in t |
| 142 |
ui32 t = val & 0xFF; |
| 143 |
bool unstuff = ((val & 0xFF) == 0xFF); // true if we need unstuffing |
| 144 |
bits -= unstuff; // there is one less bit in t if unstuffing is needed |
| 145 |
t = t << (8 - unstuff); // move up to make room for the next byte |
| 146 |
|
| 147 |
//this is a repeat of the above |
| 148 |
t |= (val>>8) & 0xFF; |
| 149 |
unstuff = (((val >> 8) & 0xFF) == 0xFF); |
| 150 |
bits -= unstuff; |
| 151 |
t = t << (8 - unstuff); |
| 152 |
|
| 153 |
t |= (val>>16) & 0xFF; |
| 154 |
unstuff = (((val >> 16) & 0xFF) == 0xFF); |
| 155 |
bits -= unstuff; |
| 156 |
t = t << (8 - unstuff); |
| 157 |
|
| 158 |
t |= (val>>24) & 0xFF; |
| 159 |
melp->unstuff = (((val >> 24) & 0xFF) == 0xFF); |
| 160 |
|
| 161 |
// move t to tmp, and push the result all the way up, so we read from |
| 162 |
// the MSB |
| 163 |
melp->tmp |= ((ui64)t) << (64 - bits - melp->bits); |
| 164 |
melp->bits += bits; //increment the number of bits in tmp |
| 165 |
} |
| 166 |
|
| 167 |
//************************************************************************/ |
| 168 |
/** @brief Decodes unstuffed MEL segment bits stored in tmp to runs |
| 169 |
* |
| 170 |
* Runs are stored in "runs" and the number of runs in "num_runs". |
| 171 |
* Each run represents a number of zero events that may or may not |
| 172 |
* terminate in a 1 event. |
| 173 |
* Each run is stored in 7 bits. The LSB is 1 if the run terminates in |
| 174 |
* a 1 event, 0 otherwise. The next 6 bits, for the case terminating |
| 175 |
* with 1, contain the number of consecutive 0 zero events * 2; for the |
| 176 |
* case terminating with 0, they store (number of consecutive 0 zero |
| 177 |
* events - 1) * 2. |
| 178 |
* A total of 6 bits (made up of 1 + 5) should have been enough. |
| 179 |
* |
| 180 |
* @param [in] melp is a pointer to dec_mel_st structure |
| 181 |
*/ |
| 182 |
static inline |
| 183 |
void mel_decode(dec_mel_st *melp) |
| 184 |
{ |
| 185 |
static const int mel_exp[13] = { //MEL exponents |
| 186 |
0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5 |
| 187 |
}; |
| 188 |
|
| 189 |
if (melp->bits < 6) // if there are less than 6 bits in tmp |
| 190 |
mel_read(melp); // then read from the MEL bitstream |
| 191 |
// 6 bits is the largest decodable MEL cwd |
| 192 |
|
| 193 |
//repeat so long that there is enough decodable bits in tmp, |
| 194 |
// and the runs store is not full (num_runs < 8) |
| 195 |
while (melp->bits >= 6 && melp->num_runs < 8) |
| 196 |
{ |
| 197 |
int eval = mel_exp[melp->k]; // number of bits associated with state |
| 198 |
int run = 0; |
| 199 |
if (melp->tmp & (1ull<<63)) //The next bit to decode (stored in MSB) |
| 200 |
{ //one is found |
| 201 |
run = 1 << eval; |
| 202 |
run--; // consecutive runs of 0 events - 1 |
| 203 |
melp->k = melp->k + 1 < 12 ? melp->k + 1 : 12;//increment, max is 12 |
| 204 |
melp->tmp <<= 1; // consume one bit from tmp |
| 205 |
melp->bits -= 1; |
| 206 |
run = run << 1; // a stretch of zeros not terminating in one |
| 207 |
} |
| 208 |
else |
| 209 |
{ //0 is found |
| 210 |
run = (int)(melp->tmp >> (63 - eval)) & ((1 << eval) - 1); |
| 211 |
melp->k = melp->k - 1 > 0 ? melp->k - 1 : 0; //decrement, min is 0 |
| 212 |
melp->tmp <<= eval + 1; //consume eval + 1 bits (max is 6) |
| 213 |
melp->bits -= eval + 1; |
| 214 |
run = (run << 1) + 1; // a stretch of zeros terminating with one |
| 215 |
} |
| 216 |
eval = melp->num_runs * 7; // 7 bits per run |
| 217 |
melp->runs &= ~((ui64)0x3F << eval); // 6 bits are sufficient |
| 218 |
melp->runs |= ((ui64)run) << eval; // store the value in runs |
| 219 |
melp->num_runs++; // increment count |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
//************************************************************************/ |
| 224 |
/** @brief Initiates a dec_mel_st structure for MEL decoding and reads |
| 225 |
* some bytes in order to get the read address to a multiple |
| 226 |
* of 4 |
| 227 |
* |
| 228 |
* @param [in] melp is a pointer to dec_mel_st structure |
| 229 |
* @param [in] bbuf is a pointer to byte buffer |
| 230 |
* @param [in] lcup is the length of MagSgn+MEL+VLC segments |
| 231 |
* @param [in] scup is the length of MEL+VLC segments |
| 232 |
*/ |
| 233 |
static inline |
| 234 |
void mel_init(dec_mel_st *melp, ui8* bbuf, int lcup, int scup) |
| 235 |
{ |
| 236 |
melp->data = bbuf + lcup - scup; // move the pointer to the start of MEL |
| 237 |
melp->bits = 0; // 0 bits in tmp |
| 238 |
melp->tmp = 0; // |
| 239 |
melp->unstuff = false; // no unstuffing |
| 240 |
melp->size = scup - 1; // size is the length of MEL+VLC-1 |
| 241 |
melp->k = 0; // 0 for state |
| 242 |
melp->num_runs = 0; // num_runs is 0 |
| 243 |
melp->runs = 0; // |
| 244 |
|
| 245 |
//This code is borrowed; original is for a different architecture |
| 246 |
//These few lines take care of the case where data is not at a multiple |
| 247 |
// of 4 boundary. It reads 1,2,3 up to 4 bytes from the MEL segment |
| 248 |
int num = 4 - (int)(intptr_t(melp->data) & 0x3); |
| 249 |
for (int i = 0; i < num; ++i) { // this code is similar to mel_read |
| 250 |
assert(melp->unstuff == false || melp->data[0] <= 0x8F); |
| 251 |
ui64 d = (melp->size > 0) ? *melp->data : 0xFF;//if buffer is consumed |
| 252 |
//set data to 0xFF |
| 253 |
if (melp->size == 1) d |= 0xF; //if this is MEL+VLC-1, set LSBs to 0xF |
| 254 |
// see the standard |
| 255 |
melp->data += melp->size-- > 0; //increment if the end is not reached |
| 256 |
int d_bits = 8 - melp->unstuff; //if unstuffing is needed, reduce by 1 |
| 257 |
melp->tmp = (melp->tmp << d_bits) | d; //store bits in tmp |
| 258 |
melp->bits += d_bits; //increment tmp by number of bits |
| 259 |
melp->unstuff = ((d & 0xFF) == 0xFF); //true of next byte needs |
| 260 |
//unstuffing |
| 261 |
} |
| 262 |
melp->tmp <<= (64 - melp->bits); //push all the way up so the first bit |
| 263 |
// is the MSB |
| 264 |
} |
| 265 |
|
| 266 |
//************************************************************************/ |
| 267 |
/** @brief Retrieves one run from dec_mel_st; if there are no runs stored |
| 268 |
* MEL segment is decoded |
| 269 |
* |
| 270 |
* @param [in] melp is a pointer to dec_mel_st structure |
| 271 |
*/ |
| 272 |
static inline |
| 273 |
int mel_get_run(dec_mel_st *melp) |
| 274 |
{ |
| 275 |
if (melp->num_runs == 0) //if no runs, decode more bit from MEL segment |
| 276 |
mel_decode(melp); |
| 277 |
|
| 278 |
int t = melp->runs & 0x7F; //retrieve one run |
| 279 |
melp->runs >>= 7; // remove the retrieved run |
| 280 |
melp->num_runs--; |
| 281 |
return t; // return run |
| 282 |
} |
| 283 |
|
| 284 |
//************************************************************************/ |
| 285 |
/** @brief A structure for reading and unstuffing a segment that grows |
| 286 |
* backward, such as VLC and MRP |
| 287 |
*/ |
| 288 |
struct rev_struct { |
| 289 |
rev_struct() : data(NULL), tmp(0), bits(0), size(0), unstuff(false) |
| 290 |
{} |
| 291 |
//storage |
| 292 |
ui8* data; //!<pointer to where to read data |
| 293 |
ui64 tmp; //!<temporary buffer of read data |
| 294 |
ui32 bits; //!<number of bits stored in tmp |
| 295 |
int size; //!<number of bytes left |
| 296 |
bool unstuff; //!<true if the last byte is more than 0x8F |
| 297 |
//!<then the current byte is unstuffed if it is 0x7F |
| 298 |
}; |
| 299 |
|
| 300 |
//************************************************************************/ |
| 301 |
/** @brief Read and unstuff data from a backwardly-growing segment |
| 302 |
* |
| 303 |
* This reader can read up to 8 bytes from before the VLC segment. |
| 304 |
* Care must be taken not read from unreadable memory, causing a |
| 305 |
* segmentation fault. |
| 306 |
* |
| 307 |
* Note that there is another subroutine rev_read_mrp that is slightly |
| 308 |
* different. The other one fills zeros when the buffer is exhausted. |
| 309 |
* This one basically does not care if the bytes are consumed, because |
| 310 |
* any extra data should not be used in the actual decoding. |
| 311 |
* |
| 312 |
* Unstuffing is needed to prevent sequences more than 0xFF8F from |
| 313 |
* appearing in the bits stream; since we are reading backward, we keep |
| 314 |
* watch when a value larger than 0x8F appears in the bitstream. |
| 315 |
* If the byte following this is 0x7F, we unstuff this byte (ignore the |
| 316 |
* MSB of that byte, which should be 0). |
| 317 |
* |
| 318 |
* @param [in] vlcp is a pointer to rev_struct structure |
| 319 |
*/ |
| 320 |
static inline |
| 321 |
void rev_read(rev_struct *vlcp) |
| 322 |
{ |
| 323 |
//process 4 bytes at a time |
| 324 |
if (vlcp->bits > 32) // if there are more than 32 bits in tmp, then |
| 325 |
return; // reading 32 bits can overflow vlcp->tmp |
| 326 |
ui32 val = 0; |
| 327 |
//the next line (the if statement) needs to be tested first |
| 328 |
if (vlcp->size > 3) // if there are more than 3 bytes left in VLC |
| 329 |
{ |
| 330 |
// (vlcp->data - 3) move pointer back to read 32 bits at once |
| 331 |
val = *(ui32*)(vlcp->data - 3); // then read 32 bits |
| 332 |
vlcp->data -= 4; // move data pointer back by 4 |
| 333 |
vlcp->size -= 4; // reduce available byte by 4 |
| 334 |
} |
| 335 |
else if (vlcp->size > 0) |
| 336 |
{ // 4 or less |
| 337 |
int i = 24; |
| 338 |
while (vlcp->size > 0) { |
| 339 |
ui32 v = *vlcp->data--; // read one byte at a time |
| 340 |
val |= (v << i); // put byte in its correct location |
| 341 |
--vlcp->size; |
| 342 |
i -= 8; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
//accumulate in tmp, number of bits in tmp are stored in bits |
| 347 |
ui32 tmp = val >> 24; //start with the MSB byte |
| 348 |
ui32 bits; |
| 349 |
|
| 350 |
// test unstuff (previous byte is >0x8F), and this byte is 0x7F |
| 351 |
bits = 8 - ((vlcp->unstuff && (((val >> 24) & 0x7F) == 0x7F)) ? 1 : 0); |
| 352 |
bool unstuff = (val >> 24) > 0x8F; //this is for the next byte |
| 353 |
|
| 354 |
tmp |= ((val >> 16) & 0xFF) << bits; //process the next byte |
| 355 |
bits += 8 - ((unstuff && (((val >> 16) & 0x7F) == 0x7F)) ? 1 : 0); |
| 356 |
unstuff = ((val >> 16) & 0xFF) > 0x8F; |
| 357 |
|
| 358 |
tmp |= ((val >> 8) & 0xFF) << bits; |
| 359 |
bits += 8 - ((unstuff && (((val >> 8) & 0x7F) == 0x7F)) ? 1 : 0); |
| 360 |
unstuff = ((val >> 8) & 0xFF) > 0x8F; |
| 361 |
|
| 362 |
tmp |= (val & 0xFF) << bits; |
| 363 |
bits += 8 - ((unstuff && ((val & 0x7F) == 0x7F)) ? 1 : 0); |
| 364 |
unstuff = (val & 0xFF) > 0x8F; |
| 365 |
|
| 366 |
// now move the read and unstuffed bits into vlcp->tmp |
| 367 |
vlcp->tmp |= (ui64)tmp << vlcp->bits; |
| 368 |
vlcp->bits += bits; |
| 369 |
vlcp->unstuff = unstuff; // this for the next read |
| 370 |
} |
| 371 |
|
| 372 |
//************************************************************************/ |
| 373 |
/** @brief Initiates the rev_struct structure and reads a few bytes to |
| 374 |
* move the read address to multiple of 4 |
| 375 |
* |
| 376 |
* There is another similar rev_init_mrp subroutine. The difference is |
| 377 |
* that this one, rev_init, discards the first 12 bits (they have the |
| 378 |
* sum of the lengths of VLC and MEL segments), and first unstuff depends |
| 379 |
* on first 4 bits. |
| 380 |
* |
| 381 |
* @param [in] vlcp is a pointer to rev_struct structure |
| 382 |
* @param [in] data is a pointer to byte at the start of the cleanup pass |
| 383 |
* @param [in] lcup is the length of MagSgn+MEL+VLC segments |
| 384 |
* @param [in] scup is the length of MEL+VLC segments |
| 385 |
*/ |
| 386 |
static inline |
| 387 |
void rev_init(rev_struct *vlcp, ui8* data, int lcup, int scup) |
| 388 |
{ |
| 389 |
//first byte has only the upper 4 bits |
| 390 |
vlcp->data = data + lcup - 2; |
| 391 |
|
| 392 |
//size can not be larger than this, in fact it should be smaller |
| 393 |
vlcp->size = scup - 2; |
| 394 |
|
| 395 |
ui32 d = *vlcp->data--; // read one byte (this is a half byte) |
| 396 |
vlcp->tmp = d >> 4; // both initialize and set |
| 397 |
vlcp->bits = 4 - ((vlcp->tmp & 7) == 7); //check standard |
| 398 |
vlcp->unstuff = (d | 0xF) > 0x8F; //this is useful for the next byte |
| 399 |
|
| 400 |
//This code is designed for an architecture that read address should |
| 401 |
// align to the read size (address multiple of 4 if read size is 4) |
| 402 |
//These few lines take care of the case where data is not at a multiple |
| 403 |
// of 4 boundary. It reads 1,2,3 up to 4 bytes from the VLC bitstream. |
| 404 |
// To read 32 bits, read from (vlcp->data - 3) |
| 405 |
int num = 1 + (int)(intptr_t(vlcp->data) & 0x3); |
| 406 |
int tnum = num < vlcp->size ? num : vlcp->size; |
| 407 |
for (int i = 0; i < tnum; ++i) { |
| 408 |
ui64 d; |
| 409 |
d = *vlcp->data--; // read one byte and move read pointer |
| 410 |
//check if the last byte was >0x8F (unstuff == true) and this is 0x7F |
| 411 |
ui32 d_bits = 8 - ((vlcp->unstuff && ((d & 0x7F) == 0x7F)) ? 1 : 0); |
| 412 |
vlcp->tmp |= d << vlcp->bits; // move data to vlcp->tmp |
| 413 |
vlcp->bits += d_bits; |
| 414 |
vlcp->unstuff = d > 0x8F; // for next byte |
| 415 |
} |
| 416 |
vlcp->size -= tnum; |
| 417 |
rev_read(vlcp); // read another 32 buts |
| 418 |
} |
| 419 |
|
| 420 |
//************************************************************************/ |
| 421 |
/** @brief Retrieves 32 bits from the head of a rev_struct structure |
| 422 |
* |
| 423 |
* By the end of this call, vlcp->tmp must have no less than 33 bits |
| 424 |
* |
| 425 |
* @param [in] vlcp is a pointer to rev_struct structure |
| 426 |
*/ |
| 427 |
static inline |
| 428 |
ui32 rev_fetch(rev_struct *vlcp) |
| 429 |
{ |
| 430 |
if (vlcp->bits < 32) // if there are less then 32 bits, read more |
| 431 |
{ |
| 432 |
rev_read(vlcp); // read 32 bits, but unstuffing might reduce this |
| 433 |
if (vlcp->bits < 32)// if there is still space in vlcp->tmp for 32 bits |
| 434 |
rev_read(vlcp); // read another 32 |
| 435 |
} |
| 436 |
return (ui32)vlcp->tmp; // return the head (bottom-most) of vlcp->tmp |
| 437 |
} |
| 438 |
|
| 439 |
//************************************************************************/ |
| 440 |
/** @brief Consumes num_bits from a rev_struct structure |
| 441 |
* |
| 442 |
* @param [in] vlcp is a pointer to rev_struct structure |
| 443 |
* @param [in] num_bits is the number of bits to be removed |
| 444 |
*/ |
| 445 |
static inline |
| 446 |
ui32 rev_advance(rev_struct *vlcp, ui32 num_bits) |
| 447 |
{ |
| 448 |
assert(num_bits <= vlcp->bits); // vlcp->tmp must have more than num_bits |
| 449 |
vlcp->tmp >>= num_bits; // remove bits |
| 450 |
vlcp->bits -= num_bits; // decrement the number of bits |
| 451 |
return (ui32)vlcp->tmp; |
| 452 |
} |
| 453 |
|
| 454 |
//************************************************************************/ |
| 455 |
/** @brief Reads and unstuffs from rev_struct |
| 456 |
* |
| 457 |
* This is different than rev_read in that this fills in zeros when the |
| 458 |
* the available data is consumed. The other does not care about the |
| 459 |
* values when all data is consumed. |
| 460 |
* |
| 461 |
* See rev_read for more information about unstuffing |
| 462 |
* |
| 463 |
* @param [in] mrp is a pointer to rev_struct structure |
| 464 |
*/ |
| 465 |
static inline |
| 466 |
void rev_read_mrp(rev_struct *mrp) |
| 467 |
{ |
| 468 |
//process 4 bytes at a time |
| 469 |
if (mrp->bits > 32) |
| 470 |
return; |
| 471 |
ui32 val = 0; |
| 472 |
if (mrp->size > 3) // If there are 3 byte or more |
| 473 |
{ // (mrp->data - 3) move pointer back to read 32 bits at once |
| 474 |
val = *(ui32*)(mrp->data - 3); // read 32 bits |
| 475 |
mrp->data -= 4; // move back pointer |
| 476 |
mrp->size -= 4; // reduce count |
| 477 |
} |
| 478 |
else if (mrp->size > 0) |
| 479 |
{ |
| 480 |
int i = 24; |
| 481 |
while (mrp->size > 0) { |
| 482 |
ui32 v = *mrp->data--; // read one byte at a time |
| 483 |
val |= (v << i); // put byte in its correct location |
| 484 |
--mrp->size; |
| 485 |
i -= 8; |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
//accumulate in tmp, and keep count in bits |
| 490 |
ui32 bits, tmp = val >> 24; |
| 491 |
|
| 492 |
//test if the last byte > 0x8F (unstuff must be true) and this is 0x7F |
| 493 |
bits = 8 - ((mrp->unstuff && (((val >> 24) & 0x7F) == 0x7F)) ? 1 : 0); |
| 494 |
bool unstuff = (val >> 24) > 0x8F; |
| 495 |
|
| 496 |
//process the next byte |
| 497 |
tmp |= ((val >> 16) & 0xFF) << bits; |
| 498 |
bits += 8 - ((unstuff && (((val >> 16) & 0x7F) == 0x7F)) ? 1 : 0); |
| 499 |
unstuff = ((val >> 16) & 0xFF) > 0x8F; |
| 500 |
|
| 501 |
tmp |= ((val >> 8) & 0xFF) << bits; |
| 502 |
bits += 8 - ((unstuff && (((val >> 8) & 0x7F) == 0x7F)) ? 1 : 0); |
| 503 |
unstuff = ((val >> 8) & 0xFF) > 0x8F; |
| 504 |
|
| 505 |
tmp |= (val & 0xFF) << bits; |
| 506 |
bits += 8 - ((unstuff && ((val & 0x7F) == 0x7F)) ? 1 : 0); |
| 507 |
unstuff = (val & 0xFF) > 0x8F; |
| 508 |
|
| 509 |
mrp->tmp |= (ui64)tmp << mrp->bits; // move data to mrp pointer |
| 510 |
mrp->bits += bits; |
| 511 |
mrp->unstuff = unstuff; // next byte |
| 512 |
} |
| 513 |
|
| 514 |
//************************************************************************/ |
| 515 |
/** @brief Initialized rev_struct structure for MRP segment, and reads |
| 516 |
* a number of bytes such that the next 32 bits read are from |
| 517 |
* an address that is a multiple of 4. Note this is designed for |
| 518 |
* an architecture that read size must be compatible with the |
| 519 |
* alignment of the read address |
| 520 |
* |
| 521 |
* There is another simiar subroutine rev_init. This subroutine does |
| 522 |
* NOT skip the first 12 bits, and starts with unstuff set to true. |
| 523 |
* |
| 524 |
* @param [in] mrp is a pointer to rev_struct structure |
| 525 |
* @param [in] data is a pointer to byte at the start of the cleanup pass |
| 526 |
* @param [in] lcup is the length of MagSgn+MEL+VLC segments |
| 527 |
* @param [in] len2 is the length of SPP+MRP segments |
| 528 |
*/ |
| 529 |
static inline |
| 530 |
void rev_init_mrp(rev_struct *mrp, ui8* data, int lcup, int len2) |
| 531 |
{ |
| 532 |
mrp->data = data + lcup + len2 - 1; |
| 533 |
mrp->size = len2; |
| 534 |
mrp->unstuff = true; |
| 535 |
mrp->bits = 0; |
| 536 |
mrp->tmp = 0; |
| 537 |
|
| 538 |
//This code is designed for an architecture that read address should |
| 539 |
// align to the read size (address multiple of 4 if read size is 4) |
| 540 |
//These few lines take care of the case where data is not at a multiple |
| 541 |
// of 4 boundary. It reads 1,2,3 up to 4 bytes from the MRP stream |
| 542 |
int num = 1 + (int)(intptr_t(mrp->data) & 0x3); |
| 543 |
for (int i = 0; i < num; ++i) { |
| 544 |
ui64 d; |
| 545 |
//read a byte, 0 if no more data |
| 546 |
d = (mrp->size-- > 0) ? *mrp->data-- : 0; |
| 547 |
//check if unstuffing is needed |
| 548 |
ui32 d_bits = 8 - ((mrp->unstuff && ((d & 0x7F) == 0x7F)) ? 1 : 0); |
| 549 |
mrp->tmp |= d << mrp->bits; // move data to vlcp->tmp |
| 550 |
mrp->bits += d_bits; |
| 551 |
mrp->unstuff = d > 0x8F; // for next byte |
| 552 |
} |
| 553 |
rev_read_mrp(mrp); |
| 554 |
} |
| 555 |
|
| 556 |
//************************************************************************/ |
| 557 |
/** @brief Retrieves 32 bits from the head of a rev_struct structure |
| 558 |
* |
| 559 |
* By the end of this call, mrp->tmp must have no less than 33 bits |
| 560 |
* |
| 561 |
* @param [in] mrp is a pointer to rev_struct structure |
| 562 |
*/ |
| 563 |
static inline |
| 564 |
ui32 rev_fetch_mrp(rev_struct *mrp) |
| 565 |
{ |
| 566 |
if (mrp->bits < 32) // if there are less than 32 bits in mrp->tmp |
| 567 |
{ |
| 568 |
rev_read_mrp(mrp); // read 30-32 bits from mrp |
| 569 |
if (mrp->bits < 32) // if there is a space of 32 bits |
| 570 |
rev_read_mrp(mrp); // read more |
| 571 |
} |
| 572 |
return (ui32)mrp->tmp; // return the head of mrp->tmp |
| 573 |
} |
| 574 |
|
| 575 |
//************************************************************************/ |
| 576 |
/** @brief Consumes num_bits from a rev_struct structure |
| 577 |
* |
| 578 |
* @param [in] mrp is a pointer to rev_struct structure |
| 579 |
* @param [in] num_bits is the number of bits to be removed |
| 580 |
*/ |
| 581 |
static inline |
| 582 |
ui32 rev_advance_mrp(rev_struct *mrp, ui32 num_bits) |
| 583 |
{ |
| 584 |
assert(num_bits <= mrp->bits); // we must not consume more than mrp->bits |
| 585 |
mrp->tmp >>= num_bits; // discard the lowest num_bits bits |
| 586 |
mrp->bits -= num_bits; |
| 587 |
return (ui32)mrp->tmp; // return data after consumption |
| 588 |
} |
| 589 |
|
| 590 |
//************************************************************************/ |
| 591 |
/** @brief State structure for reading and unstuffing of forward-growing |
| 592 |
* bitstreams; these are: MagSgn and SPP bitstreams |
| 593 |
*/ |
| 594 |
struct frwd_struct { |
| 595 |
const ui8* data; //!<pointer to bitstream |
| 596 |
ui64 tmp; //!<temporary buffer of read data |
| 597 |
ui32 bits; //!<number of bits stored in tmp |
| 598 |
ui32 unstuff; //!<1 if a bit needs to be unstuffed from next byte |
| 599 |
int size; //!<size of data |
| 600 |
}; |
| 601 |
|
| 602 |
//************************************************************************/ |
| 603 |
/** @brief Read and unstuffs 32 bits from forward-growing bitstream |
| 604 |
* |
| 605 |
* A template is used to accommodate a different requirement for |
| 606 |
* MagSgn and SPP bitstreams; in particular, when MagSgn bitstream is |
| 607 |
* consumed, 0xFF's are fed, while when SPP is exhausted 0's are fed in. |
| 608 |
* X controls this value. |
| 609 |
* |
| 610 |
* Unstuffing prevent sequences that are more than 0xFF7F from appearing |
| 611 |
* in the conpressed sequence. So whenever a value of 0xFF is coded, the |
| 612 |
* MSB of the next byte is set 0 and must be ignored during decoding. |
| 613 |
* |
| 614 |
* Reading can go beyond the end of buffer by up to 3 bytes. |
| 615 |
* |
| 616 |
* @tparam X is the value fed in when the bitstream is exhausted |
| 617 |
* @param [in] msp is a pointer to frwd_struct structure |
| 618 |
* |
| 619 |
*/ |
| 620 |
template<int X> |
| 621 |
static inline |
| 622 |
void frwd_read(frwd_struct *msp) |
| 623 |
{ |
| 624 |
assert(msp->bits <= 32); // assert that there is a space for 32 bits |
| 625 |
|
| 626 |
ui32 val = 0; |
| 627 |
if (msp->size > 3) { |
| 628 |
val = *(ui32*)msp->data; // read 32 bits |
| 629 |
msp->data += 4; // increment pointer |
| 630 |
msp->size -= 4; // reduce size |
| 631 |
} |
| 632 |
else if (msp->size > 0) |
| 633 |
{ |
| 634 |
int i = 0; |
| 635 |
val = X != 0 ? 0xFFFFFFFFu : 0; |
| 636 |
while (msp->size > 0) { |
| 637 |
ui32 v = *msp->data++; // read one byte at a time |
| 638 |
ui32 m = ~(0xFFu << i); // mask of location |
| 639 |
val = (val & m) | (v << i);// put one byte in its correct location |
| 640 |
--msp->size; |
| 641 |
i += 8; |
| 642 |
} |
| 643 |
} |
| 644 |
else |
| 645 |
val = X != 0 ? 0xFFFFFFFFu : 0; |
| 646 |
|
| 647 |
// we accumulate in t and keep a count of the number of bits in bits |
| 648 |
ui32 bits = 8 - msp->unstuff; |
| 649 |
ui32 t = val & 0xFF; |
| 650 |
bool unstuff = ((val & 0xFF) == 0xFF); // Do we need unstuffing next? |
| 651 |
|
| 652 |
t |= ((val >> 8) & 0xFF) << bits; |
| 653 |
bits += 8 - unstuff; |
| 654 |
unstuff = (((val >> 8) & 0xFF) == 0xFF); |
| 655 |
|
| 656 |
t |= ((val >> 16) & 0xFF) << bits; |
| 657 |
bits += 8 - unstuff; |
| 658 |
unstuff = (((val >> 16) & 0xFF) == 0xFF); |
| 659 |
|
| 660 |
t |= ((val >> 24) & 0xFF) << bits; |
| 661 |
bits += 8 - unstuff; |
| 662 |
msp->unstuff = (((val >> 24) & 0xFF) == 0xFF); // for next byte |
| 663 |
|
| 664 |
msp->tmp |= ((ui64)t) << msp->bits; // move data to msp->tmp |
| 665 |
msp->bits += bits; |
| 666 |
} |
| 667 |
|
| 668 |
//************************************************************************/ |
| 669 |
/** @brief Initialize frwd_struct struct and reads some bytes |
| 670 |
* |
| 671 |
* @tparam X is the value fed in when the bitstream is exhausted. |
| 672 |
* See frwd_read regarding the template |
| 673 |
* @param [in] msp is a pointer to frwd_struct |
| 674 |
* @param [in] data is a pointer to the start of data |
| 675 |
* @param [in] size is the number of byte in the bitstream |
| 676 |
*/ |
| 677 |
template<int X> |
| 678 |
static inline |
| 679 |
void frwd_init(frwd_struct *msp, const ui8* data, int size) |
| 680 |
{ |
| 681 |
msp->data = data; |
| 682 |
msp->tmp = 0; |
| 683 |
msp->bits = 0; |
| 684 |
msp->unstuff = 0; |
| 685 |
msp->size = size; |
| 686 |
|
| 687 |
//This code is designed for an architecture that read address should |
| 688 |
// align to the read size (address multiple of 4 if read size is 4) |
| 689 |
//These few lines take care of the case where data is not at a multiple |
| 690 |
// of 4 boundary. It reads 1,2,3 up to 4 bytes from the bitstream |
| 691 |
int num = 4 - (int)(intptr_t(msp->data) & 0x3); |
| 692 |
for (int i = 0; i < num; ++i) |
| 693 |
{ |
| 694 |
ui64 d; |
| 695 |
//read a byte if the buffer is not exhausted, otherwise set it to X |
| 696 |
d = msp->size-- > 0 ? *msp->data++ : X; |
| 697 |
msp->tmp |= (d << msp->bits); // store data in msp->tmp |
| 698 |
msp->bits += 8 - msp->unstuff; // number of bits added to msp->tmp |
| 699 |
msp->unstuff = ((d & 0xFF) == 0xFF); // unstuffing for next byte |
| 700 |
} |
| 701 |
frwd_read<X>(msp); // read 32 bits more |
| 702 |
} |
| 703 |
|
| 704 |
//************************************************************************/ |
| 705 |
/** @brief Consume num_bits bits from the bitstream of frwd_struct |
| 706 |
* |
| 707 |
* @param [in] msp is a pointer to frwd_struct |
| 708 |
* @param [in] num_bits is the number of bit to consume |
| 709 |
*/ |
| 710 |
static inline |
| 711 |
void frwd_advance(frwd_struct *msp, ui32 num_bits) |
| 712 |
{ |
| 713 |
assert(num_bits <= msp->bits); |
| 714 |
msp->tmp >>= num_bits; // consume num_bits |
| 715 |
msp->bits -= num_bits; |
| 716 |
} |
| 717 |
|
| 718 |
//************************************************************************/ |
| 719 |
/** @brief Fetches 32 bits from the frwd_struct bitstream |
| 720 |
* |
| 721 |
* @tparam X is the value fed in when the bitstream is exhausted. |
| 722 |
* See frwd_read regarding the template |
| 723 |
* @param [in] msp is a pointer to frwd_struct |
| 724 |
*/ |
| 725 |
template<int X> |
| 726 |
static inline |
| 727 |
ui32 frwd_fetch(frwd_struct *msp) |
| 728 |
{ |
| 729 |
if (msp->bits < 32) |
| 730 |
{ |
| 731 |
frwd_read<X>(msp); |
| 732 |
if (msp->bits < 32) //need to test |
| 733 |
frwd_read<X>(msp); |
| 734 |
} |
| 735 |
return (ui32)msp->tmp; |
| 736 |
} |
| 737 |
|
| 738 |
//************************************************************************/ |
| 739 |
/** @brief Decodes one codeblock, processing the cleanup, siginificance |
| 740 |
* propagation, and magnitude refinement pass |
| 741 |
* |
| 742 |
* @param [in] coded_data is a pointer to bitstream |
| 743 |
* @param [in] decoded_data is a pointer to decoded codeblock data buf. |
| 744 |
* @param [in] missing_msbs is the number of missing MSBs |
| 745 |
* @param [in] num_passes is the number of passes: 1 if CUP only, |
| 746 |
* 2 for CUP+SPP, and 3 for CUP+SPP+MRP |
| 747 |
* @param [in] lengths1 is the length of cleanup pass |
| 748 |
* @param [in] lengths2 is the length of refinement passes (either SPP |
| 749 |
* only or SPP+MRP) |
| 750 |
* @param [in] width is the decoded codeblock width |
| 751 |
* @param [in] height is the decoded codeblock height |
| 752 |
* @param [in] stride is the decoded codeblock buffer stride |
| 753 |
* @param [in] stripe_causal is true for stripe causal mode |
| 754 |
*/ |
| 755 |
bool ojph_decode_codeblock(ui8* coded_data, ui32* decoded_data, |
| 756 |
ui32 missing_msbs, ui32 num_passes, |
| 757 |
ui32 lengths1, ui32 lengths2, |
| 758 |
ui32 width, ui32 height, ui32 stride, |
| 759 |
bool stripe_causal) |
| 760 |
{ |
| 761 |
static bool insufficient_precision = false; |
| 762 |
static bool modify_code = false; |
| 763 |
static bool truncate_spp_mrp = false; |
| 764 |
|
| 765 |
if (num_passes > 1 && lengths2 == 0) |
| 766 |
{ |
| 767 |
grk::GRK_WARN("A malformed codeblock that has more than " |
| 768 |
"one coding pass, but zero length for " |
| 769 |
"2nd and potential 3rd pass.\n"); |
| 770 |
num_passes = 1; |
| 771 |
} |
| 772 |
|
| 773 |
if (num_passes > 3) |
| 774 |
{ |
| 775 |
grk::GRK_WARN("We do not support more than 3 coding passes; " |
| 776 |
"This codeblocks has %d passes.\n", |
| 777 |
num_passes); |
| 778 |
return false; |
| 779 |
} |
| 780 |
|
| 781 |
if (missing_msbs > 30) // p < 0 |
| 782 |
{ |
| 783 |
if (insufficient_precision == false) |
| 784 |
{ |
| 785 |
insufficient_precision = true; |
| 786 |
grk::GRK_WARN( "32 bits are not enough to decode this " |
| 787 |
"codeblock. This message will not be " |
| 788 |
"displayed again.\n"); |
| 789 |
} |
| 790 |
return false; |
| 791 |
} |
| 792 |
else if (missing_msbs == 30) // p == 0 |
| 793 |
{ // not enough precision to decode and set the bin center to 1 |
| 794 |
if (modify_code == false) { |
| 795 |
modify_code = true; |
| 796 |
grk::GRK_WARN("Not enough precision to decode the cleanup " |
| 797 |
"pass. The code can be modified to support " |
| 798 |
"this case. This message will not be " |
| 799 |
"displayed again.\n"); |
| 800 |
} |
| 801 |
return false; // 32 bits are not enough to decode this |
| 802 |
} |
| 803 |
else if (missing_msbs == 29) // if p is 1, then num_passes must be 1 |
| 804 |
{ |
| 805 |
if (num_passes > 1) { |
| 806 |
num_passes = 1; |
| 807 |
if (truncate_spp_mrp == false) { |
| 808 |
truncate_spp_mrp = true; |
| 809 |
grk::GRK_WARN("Not enough precision to decode the SgnProp " |
| 810 |
"nor MagRef passes; both will be skipped. " |
| 811 |
"This message will not be displayed " |
| 812 |
"again.\n"); |
| 813 |
} |
| 814 |
} |
| 815 |
} |
| 816 |
ui32 p = 30 - missing_msbs; // The least significant bitplane for CUP |
| 817 |
// There is a way to handle the case of p == 0, but a different path |
| 818 |
// is required |
| 819 |
|
| 820 |
if (lengths1 < 2) |
| 821 |
{ |
| 822 |
grk::GRK_WARN("Wrong codeblock length.\n"); |
| 823 |
return false; |
| 824 |
} |
| 825 |
|
| 826 |
// read scup and fix the bytes there |
| 827 |
int lcup, scup; |
| 828 |
lcup = (int)lengths1; // length of CUP |
| 829 |
//scup is the length of MEL + VLC |
| 830 |
scup = (((int)coded_data[lcup-1]) << 4) + (coded_data[lcup-2] & 0xF); |
| 831 |
if (scup < 2 || scup > lcup || scup > 4079) //something is wrong |
| 832 |
return false; |
| 833 |
|
| 834 |
// The temporary storage scratch holds two types of data in an |
| 835 |
// interleaved fashion. The interleaving allows us to use one |
| 836 |
// memory pointer. |
| 837 |
// We have one entry for a decoded VLC code, and one entry for UVLC. |
| 838 |
// Entries are 16 bits each, corresponding to one quad, |
| 839 |
// but since we want to use XMM registers of the SSE family |
| 840 |
// of SIMD; we allocated 16 bytes or more per quad row; that is, |
| 841 |
// the width is no smaller than 16 bytes (or 8 entries), and the |
| 842 |
// height is 512 quads |
| 843 |
// Each VLC entry contains, in the following order, starting |
| 844 |
// from MSB |
| 845 |
// e_k (4bits), e_1 (4bits), rho (4bits), useless for step 2 (4bits) |
| 846 |
// Each entry in UVLC contains u_q |
| 847 |
// One extra row to handle the case of SPP propagating downwards |
| 848 |
// when codeblock width is 4 |
| 849 |
ui16 scratch[8 * 513] = {0}; // 8 kB |
| 850 |
|
| 851 |
// We need an extra two entries (one inf and one u_q) beyond |
| 852 |
// the last column. |
| 853 |
// If the block width is 4 (2 quads), then we use sstr of 8 |
| 854 |
// (enough for 4 quads). If width is 8 (4 quads) we use |
| 855 |
// sstr is 16 (enough for 8 quads). For a width of 16 (8 |
| 856 |
// quads), we use 24 (enough for 12 quads). |
| 857 |
ui32 sstr = ((width + 2u) + 7u) & ~7u; // multiples of 8 |
| 858 |
|
| 859 |
ui32 mmsbp2 = missing_msbs + 2; |
| 860 |
|
| 861 |
// The cleanup pass is decoded in two steps; in step one, |
| 862 |
// the VLC and MEL segments are decoded, generating a record that |
| 863 |
// has 2 bytes per quad. The 2 bytes contain, u, rho, e^1 & e^k. |
| 864 |
// This information should be sufficient for the next step. |
| 865 |
// In step 2, we decode the MagSgn segment. |
| 866 |
|
| 867 |
// step 1 decoding VLC and MEL segments |
| 868 |
{ |
| 869 |
// init structures |
| 870 |
dec_mel_st mel; |
| 871 |
mel_init(&mel, coded_data, lcup, scup); |
| 872 |
rev_struct vlc; |
| 873 |
rev_init(&vlc, coded_data, lcup, scup); |
| 874 |
|
| 875 |
int run = mel_get_run(&mel); // decode runs of events from MEL bitstrm |
| 876 |
// data represented as runs of 0 events |
| 877 |
// See mel_decode description |
| 878 |
|
| 879 |
ui32 vlc_val; |
| 880 |
ui32 c_q = 0; |
| 881 |
ui16 *sp = scratch; |
| 882 |
//initial quad row |
| 883 |
for (ui32 x = 0; x < width; sp += 4) |
| 884 |
{ |
| 885 |
// decode VLC |
| 886 |
///////////// |
| 887 |
|
| 888 |
// first quad |
| 889 |
vlc_val = rev_fetch(&vlc); |
| 890 |
|
| 891 |
//decode VLC using the context c_q and the head of VLC bitstream |
| 892 |
ui16 t0 = vlc_tbl0[ c_q + (vlc_val & 0x7F) ]; |
| 893 |
|
| 894 |
// if context is zero, use one MEL event |
| 895 |
if (c_q == 0) //zero context |
| 896 |
{ |
| 897 |
run -= 2; //subtract 2, since events number if multiplied by 2 |
| 898 |
|
| 899 |
// Is the run terminated in 1? if so, use decoded VLC code, |
| 900 |
// otherwise, discard decoded data, since we will decoded again |
| 901 |
// using a different context |
| 902 |
t0 = (run == -1) ? t0 : 0; |
| 903 |
|
| 904 |
// is run -1 or -2? this means a run has been consumed |
| 905 |
if (run < 0) |
| 906 |
run = mel_get_run(&mel); // get another run |
| 907 |
} |
| 908 |
//run -= (c_q == 0) ? 2 : 0; |
| 909 |
//t0 = (c_q != 0 || run == -1) ? t0 : 0; |
| 910 |
//if (run < 0) |
| 911 |
// run = mel_get_run(&mel); // get another run |
| 912 |
sp[0] = t0; |
| 913 |
x += 2; |
| 914 |
|
| 915 |
// prepare context for the next quad; eqn. 1 in ITU T.814 |
| 916 |
c_q = ((t0 & 0x10U) << 3) | ((t0 & 0xE0U) << 2); |
| 917 |
|
| 918 |
//remove data from vlc stream (0 bits are removed if vlc is not used) |
| 919 |
vlc_val = rev_advance(&vlc, t0 & 0x7); |
| 920 |
|
| 921 |
//second quad |
| 922 |
ui16 t1 = 0; |
| 923 |
|
| 924 |
//decode VLC using the context c_q and the head of VLC bitstream |
| 925 |
t1 = vlc_tbl0[c_q + (vlc_val & 0x7F)]; |
| 926 |
|
| 927 |
// if context is zero, use one MEL event |
| 928 |
if (c_q == 0 && x < width) //zero context |
| 929 |
{ |
| 930 |
run -= 2; //subtract 2, since events number if multiplied by 2 |
| 931 |
|
| 932 |
// if event is 0, discard decoded t1 |
| 933 |
t1 = (run == -1) ? t1 : 0; |
| 934 |
|
| 935 |
if (run < 0) // have we consumed all events in a run |
| 936 |
run = mel_get_run(&mel); // if yes, then get another run |
| 937 |
} |
| 938 |
t1 = x < width ? t1 : 0; |
| 939 |
//run -= (c_q == 0 && x < width) ? 2 : 0; |
| 940 |
//t1 = (c_q != 0 || run == -1) ? t1 : 0; |
| 941 |
//if (run < 0) |
| 942 |
// run = mel_get_run(&mel); // get another run |
| 943 |
sp[2] = t1; |
| 944 |
x += 2; |
| 945 |
|
| 946 |
//prepare context for the next quad, eqn. 1 in ITU T.814 |
| 947 |
c_q = ((t1 & 0x10U) << 3) | ((t1 & 0xE0U) << 2); |
| 948 |
|
| 949 |
//remove data from vlc stream, if qinf is not used, cwdlen is 0 |
| 950 |
vlc_val = rev_advance(&vlc, t1 & 0x7); |
| 951 |
|
| 952 |
// decode u |
| 953 |
///////////// |
| 954 |
// uvlc_mode is made up of u_offset bits from the quad pair |
| 955 |
ui32 uvlc_mode = ((t0 & 0x8U) << 3) | ((t1 & 0x8U) << 4); |
| 956 |
if (uvlc_mode == 0xc0)// if both u_offset are set, get an event from |
| 957 |
{ // the MEL run of events |
| 958 |
run -= 2; //subtract 2, since events number if multiplied by 2 |
| 959 |
|
| 960 |
uvlc_mode += (run == -1) ? 0x40 : 0; // increment uvlc_mode by |
| 961 |
// is 0x40 |
| 962 |
|
| 963 |
if (run < 0)//if run is consumed (run is -1 or -2), get another run |
| 964 |
run = mel_get_run(&mel); |
| 965 |
} |
| 966 |
//run -= (uvlc_mode == 0xc0) ? 2 : 0; |
| 967 |
//uvlc_mode += (uvlc_mode == 0xc0 && run == -1) ? 0x40 : 0; |
| 968 |
//if (run < 0) |
| 969 |
// run = mel_get_run(&mel); // get another run |
| 970 |
|
| 971 |
//decode uvlc_mode to get u for both quads |
| 972 |
ui32 uvlc_entry = uvlc_tbl0[uvlc_mode + (vlc_val & 0x3F)]; |
| 973 |
//remove total prefix length |
| 974 |
vlc_val = rev_advance(&vlc, uvlc_entry & 0x7); |
| 975 |
uvlc_entry >>= 3; |
| 976 |
//extract suffixes for quad 0 and 1 |
| 977 |
ui32 len = uvlc_entry & 0xF; //suffix length for 2 quads |
| 978 |
ui32 tmp = vlc_val & ((1 << len) - 1); //suffix value for 2 quads |
| 979 |
vlc_val = rev_advance(&vlc, len); |
| 980 |
uvlc_entry >>= 4; |
| 981 |
// quad 0 length |
| 982 |
len = uvlc_entry & 0x7; // quad 0 suffix length |
| 983 |
uvlc_entry >>= 3; |
| 984 |
ui16 u_q = (ui16)(1 + (uvlc_entry&7) + (tmp&~(0xFFU<<len)));//kap. 1 |
| 985 |
sp[1] = u_q; |
| 986 |
u_q = (ui16)(1 + (uvlc_entry >> 3) + (tmp >> len)); //kappa == 1 |
| 987 |
sp[3]= u_q; |
| 988 |
} |
| 989 |
sp[0] = sp[1] = 0; |
| 990 |
|
| 991 |
//non initial quad rows |
| 992 |
for (ui32 y = 2; y < height; y += 2) |
| 993 |
{ |
| 994 |
c_q = 0; // context |
| 995 |
ui16 *sp = scratch + (y >> 1) * sstr; // this row of quads |
| 996 |
|
| 997 |
for (ui32 x = 0; x < width; sp += 4) |
| 998 |
{ |
| 999 |
// decode VLC |
| 1000 |
///////////// |
| 1001 |
|
| 1002 |
// sigma_q (n, ne, nf) |
| 1003 |
c_q |= ((sp[0 - (si32)sstr] & 0xA0U) << 2); |
| 1004 |
c_q |= ((sp[2 - (si32)sstr] & 0x20U) << 4); |
| 1005 |
|
| 1006 |
// first quad |
| 1007 |
vlc_val = rev_fetch(&vlc); |
| 1008 |
|
| 1009 |
//decode VLC using the context c_q and the head of VLC bitstream |
| 1010 |
ui16 t0 = vlc_tbl1[ c_q + (vlc_val & 0x7F) ]; |
| 1011 |
|
| 1012 |
// if context is zero, use one MEL event |
| 1013 |
if (c_q == 0) //zero context |
| 1014 |
{ |
| 1015 |
run -= 2; //subtract 2, since events number is multiplied by 2 |
| 1016 |
|
| 1017 |
// Is the run terminated in 1? if so, use decoded VLC code, |
| 1018 |
// otherwise, discard decoded data, since we will decoded again |
| 1019 |
// using a different context |
| 1020 |
t0 = (run == -1) ? t0 : 0; |
| 1021 |
|
| 1022 |
// is run -1 or -2? this means a run has been consumed |
| 1023 |
if (run < 0) |
| 1024 |
run = mel_get_run(&mel); // get another run |
| 1025 |
} |
| 1026 |
//run -= (c_q == 0) ? 2 : 0; |
| 1027 |
//t0 = (c_q != 0 || run == -1) ? t0 : 0; |
| 1028 |
//if (run < 0) |
| 1029 |
// run = mel_get_run(&mel); // get another run |
| 1030 |
sp[0] = t0; |
| 1031 |
x += 2; |
| 1032 |
|
| 1033 |
// prepare context for the next quad; eqn. 2 in ITU T.814 |
| 1034 |
// sigma_q (w, sw) |
| 1035 |
c_q = ((t0 & 0x40U) << 2) | ((t0 & 0x80U) << 1); |
| 1036 |
// sigma_q (nw) |
| 1037 |
c_q |= sp[0 - (si32)sstr] & 0x80; |
| 1038 |
// sigma_q (n, ne, nf) |
| 1039 |
c_q |= ((sp[2 - (si32)sstr] & 0xA0U) << 2); |
| 1040 |
c_q |= ((sp[4 - (si32)sstr] & 0x20U) << 4); |
| 1041 |
|
| 1042 |
//remove data from vlc stream (0 bits are removed if vlc is unused) |
| 1043 |
vlc_val = rev_advance(&vlc, t0 & 0x7); |
| 1044 |
|
| 1045 |
//second quad |
| 1046 |
ui16 t1 = 0; |
| 1047 |
|
| 1048 |
//decode VLC using the context c_q and the head of VLC bitstream |
| 1049 |
t1 = vlc_tbl1[ c_q + (vlc_val & 0x7F)]; |
| 1050 |
|
| 1051 |
// if context is zero, use one MEL event |
| 1052 |
if (c_q == 0 && x < width) //zero context |
| 1053 |
{ |
| 1054 |
run -= 2; //subtract 2, since events number if multiplied by 2 |
| 1055 |
|
| 1056 |
// if event is 0, discard decoded t1 |
| 1057 |
t1 = (run == -1) ? t1 : 0; |
| 1058 |
|
| 1059 |
if (run < 0) // have we consumed all events in a run |
| 1060 |
run = mel_get_run(&mel); // if yes, then get another run |
| 1061 |
} |
| 1062 |
t1 = x < width ? t1 : 0; |
| 1063 |
//run -= (c_q == 0 && x < width) ? 2 : 0; |
| 1064 |
//t1 = (c_q != 0 || run == -1) ? t1 : 0; |
| 1065 |
//if (run < 0) |
| 1066 |
// run = mel_get_run(&mel); // get another run |
| 1067 |
sp[2] = t1; |
| 1068 |
x += 2; |
| 1069 |
|
| 1070 |
// partial c_q, will be completed when we process the next quad |
| 1071 |
// sigma_q (w, sw) |
| 1072 |
c_q = ((t1 & 0x40U) << 2) | ((t1 & 0x80U) << 1); |
| 1073 |
// sigma_q (nw) |
| 1074 |
c_q |= sp[2 - (si32)sstr] & 0x80; |
| 1075 |
|
| 1076 |
//remove data from vlc stream, if qinf is not used, cwdlen is 0 |
| 1077 |
vlc_val = rev_advance(&vlc, t1 & 0x7); |
| 1078 |
|
| 1079 |
// decode u |
| 1080 |
///////////// |
| 1081 |
// uvlc_mode is made up of u_offset bits from the quad pair |
| 1082 |
ui32 uvlc_mode = ((t0 & 0x8U) << 3) | ((t1 & 0x8U) << 4); |
| 1083 |
ui32 uvlc_entry = uvlc_tbl1[uvlc_mode + (vlc_val & 0x3F)]; |
| 1084 |
//remove total prefix length |
| 1085 |
vlc_val = rev_advance(&vlc, uvlc_entry & 0x7); |
| 1086 |
uvlc_entry >>= 3; |
| 1087 |
//extract suffixes for quad 0 and 1 |
| 1088 |
ui32 len = uvlc_entry & 0xF; //suffix length for 2 quads |
| 1089 |
ui32 tmp = vlc_val & ((1 << len) - 1); //suffix value for 2 quads |
| 1090 |
vlc_val = rev_advance(&vlc, len); |
| 1091 |
uvlc_entry >>= 4; |
| 1092 |
// quad 0 length |
| 1093 |
len = uvlc_entry & 0x7; // quad 0 suffix length |
| 1094 |
uvlc_entry >>= 3; |
| 1095 |
ui16 u_q = (ui16)((uvlc_entry & 7) + (tmp & ~(0xFU << len))); //u_q |
| 1096 |
sp[1] = u_q; |
| 1097 |
u_q = (ui16)((uvlc_entry >> 3) + (tmp >> len)); // u_q |
| 1098 |
sp[3] = u_q; |
| 1099 |
} |
| 1100 |
sp[0] = sp[1] = 0; |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|
| 1104 |
// step2 we decode magsgn |
| 1105 |
{ |
| 1106 |
// We allocate a scratch row for storing v_n values. |
| 1107 |
// We have 512 quads horizontally. |
| 1108 |
// We need an extra entry to handle the case of vp[1] |
| 1109 |
// when vp is at the last column. |
| 1110 |
// Here, we allocate 4 instead of 1 to make the buffer size |
| 1111 |
// a multipled of 16 bytes. |
| 1112 |
const int v_n_size = 512 + 4; |
| 1113 |
ui32 v_n_scratch[v_n_size] = {0}; // 2+ kB |
| 1114 |
|
| 1115 |
frwd_struct magsgn; |
| 1116 |
frwd_init<0xFF>(&magsgn, coded_data, lcup - scup); |
| 1117 |
|
| 1118 |
ui16 *sp = scratch; |
| 1119 |
ui32 *vp = v_n_scratch; |
| 1120 |
ui32 *dp = decoded_data; |
| 1121 |
|
| 1122 |
ui32 prev_v_n = 0; |
| 1123 |
for (ui32 x = 0; x < width; sp += 2, ++vp) |
| 1124 |
{ |
| 1125 |
ui32 inf = sp[0]; |
| 1126 |
ui32 U_q = sp[1]; |
| 1127 |
if (U_q > mmsbp2) |
| 1128 |
return false; |
| 1129 |
|
| 1130 |
ui32 v_n; |
| 1131 |
ui32 val = 0; |
| 1132 |
ui32 bit = 0; |
| 1133 |
if (inf & (1 << (4 + bit))) |
| 1134 |
{ |
| 1135 |
//get 32 bits of magsgn data |
| 1136 |
ui32 ms_val = frwd_fetch<0xFF>(&magsgn); |
| 1137 |
ui32 m_n = U_q - ((inf >> (12 + bit)) & 1); // remove e_k |
| 1138 |
frwd_advance(&magsgn, m_n); //consume m_n |
| 1139 |
|
| 1140 |
val = ms_val << 31; // get sign bit |
| 1141 |
v_n = ms_val & ((1 << m_n) - 1); // keep only m_n bits |
| 1142 |
v_n |= ((inf >> (8 + bit)) & 1) << m_n; // add EMB e_1 as MSB |
| 1143 |
v_n |= 1; // add center of bin |
| 1144 |
//v_n now has 2 * (\mu - 1) + 0.5 with correct sign bit |
| 1145 |
//add 2 to make it 2*\mu+0.5, shift it up to missing MSBs |
| 1146 |
val |= (v_n + 2) << (p - 1); |
| 1147 |
} |
| 1148 |
dp[0] = val; |
| 1149 |
|
| 1150 |
v_n = 0; |
| 1151 |
val = 0; |
| 1152 |
bit = 1; |
| 1153 |
if (inf & (1 << (4 + bit))) |
| 1154 |
{ |
| 1155 |
//get 32 bits of magsgn data |
| 1156 |
ui32 ms_val = frwd_fetch<0xFF>(&magsgn); |
| 1157 |
ui32 m_n = U_q - ((inf >> (12 + bit)) & 1); // remove e_k |
| 1158 |
frwd_advance(&magsgn, m_n); //consume m_n |
| 1159 |
|
| 1160 |
val = ms_val << 31; // get sign bit |
| 1161 |
v_n = ms_val & ((1 << m_n) - 1); // keep only m_n bits |
| 1162 |
v_n |= ((inf >> (8 + bit)) & 1) << m_n; // add EMB e_1 as MSB |
| 1163 |
v_n |= 1; // add center of bin |
| 1164 |
//v_n now has 2 * (\mu - 1) + 0.5 with correct sign bit |
| 1165 |
//add 2 to make it 2*\mu+0.5, shift it up to missing MSBs |
| 1166 |
val |= (v_n + 2) << (p - 1); |
| 1167 |
} |
| 1168 |
dp[stride] = val; |
| 1169 |
vp[0] = prev_v_n | v_n; |
| 1170 |
prev_v_n = 0; |
| 1171 |
++dp; |
| 1172 |
if (++x >= width) |
| 1173 |
{ ++vp; break; } |
| 1174 |
|
| 1175 |
val = 0; |
| 1176 |
bit = 2; |
| 1177 |
if (inf & (1 << (4 + bit))) |
| 1178 |
{ |
| 1179 |
//get 32 bits of magsgn data |
| 1180 |
ui32 ms_val = frwd_fetch<0xFF>(&magsgn); |
| 1181 |
ui32 m_n = U_q - ((inf >> (12 + bit)) & 1); // remove e_k |
| 1182 |
frwd_advance(&magsgn, m_n); //consume m_n |
| 1183 |
|
| 1184 |
val = ms_val << 31; // get sign bit |
| 1185 |
v_n = ms_val & ((1 << m_n) - 1); // keep only m_n bits |
| 1186 |
v_n |= ((inf >> (8 + bit)) & 1) << m_n; // add EMB e_1 as MSB |
| 1187 |
v_n |= 1; // add center of bin |
| 1188 |
//v_n now has 2 * (\mu - 1) + 0.5 with correct sign bit |
| 1189 |
//add 2 to make it 2*\mu+0.5, shift it up to missing MSBs |
| 1190 |
val |= (v_n + 2) << (p - 1); |
| 1191 |
} |
| 1192 |
dp[0] = val; |
| 1193 |
|
| 1194 |
v_n = 0; |
| 1195 |
val = 0; |
| 1196 |
bit = 3; |
| 1197 |
if (inf & (1 << (4 + bit))) |
| 1198 |
{ |
| 1199 |
//get 32 bits of magsgn data |
| 1200 |
ui32 ms_val = frwd_fetch<0xFF>(&magsgn); |
| 1201 |
ui32 m_n = U_q - ((inf >> (12 + bit)) & 1); // remove e_k |
| 1202 |
frwd_advance(&magsgn, m_n); //consume m_n |
| 1203 |
|
| 1204 |
val = ms_val << 31; // get sign bit |
| 1205 |
v_n = ms_val & ((1 << m_n) - 1); // keep only m_n bits |
| 1206 |
v_n |= ((inf >> (8 + bit)) & 1) << m_n; // add EMB e_1 as MSB |
| 1207 |
v_n |= 1; // add center of bin |
| 1208 |
//v_n now has 2 * (\mu - 1) + 0.5 with correct sign bit |
| 1209 |
//add 2 to make it 2*\mu+0.5, shift it up to missing MSBs |
| 1210 |
val |= (v_n + 2) << (p - 1); |
| 1211 |
} |
| 1212 |
dp[stride] = val; |
| 1213 |
prev_v_n = v_n; |
| 1214 |
++dp; |
| 1215 |
++x; |
| 1216 |
} |
| 1217 |
vp[0] = prev_v_n; |
| 1218 |
|
| 1219 |
for (ui32 y = 2; y < height; y += 2) |
| 1220 |
{ |
| 1221 |
ui16 *sp = scratch + (y >> 1) * sstr; |
| 1222 |
ui32 *vp = v_n_scratch; |
| 1223 |
ui32 *dp = decoded_data + y * stride; |
| 1224 |
|
| 1225 |
prev_v_n = 0; |
| 1226 |
for (ui32 x = 0; x < width; sp += 2, ++vp) |
| 1227 |
{ |
| 1228 |
ui32 inf = sp[0]; |
| 1229 |
ui32 u_q = sp[1]; |
| 1230 |
|
| 1231 |
ui32 gamma = inf & 0xF0; gamma &= gamma - 0x10; //is gamma_q 1? |
| 1232 |
ui32 emax = vp[0] | vp[1]; |
| 1233 |
emax = 31 - count_leading_zeros(emax | 2); // emax - 1 |
| 1234 |
ui32 kappa = gamma ? emax : 1; |
| 1235 |
|
| 1236 |
ui32 U_q = u_q + kappa; |
| 1237 |
if (U_q > mmsbp2) |
| 1238 |
return false; |
| 1239 |
|
| 1240 |
ui32 v_n; |
| 1241 |
ui32 val = 0; |
| 1242 |
ui32 bit = 0; |
| 1243 |
if (inf & (1 << (4 + bit))) |
| 1244 |
{ |
| 1245 |
//get 32 bits of magsgn data |
| 1246 |
ui32 ms_val = frwd_fetch<0xFF>(&magsgn); |
| 1247 |
ui32 m_n = U_q - ((inf >> (12 + bit)) & 1); // remove e_k |
| 1248 |
frwd_advance(&magsgn, m_n); //consume m_n |
| 1249 |
|
| 1250 |
val = ms_val << 31; // get sign bit |
| 1251 |
v_n = ms_val & ((1 << m_n) - 1); // keep only m_n bits |
| 1252 |
v_n |= ((inf >> (8 + bit)) & 1) << m_n; // add EMB e_1 as MSB |
| 1253 |
v_n |= 1; // add center of bin |
| 1254 |
//v_n now has 2 * (\mu - 1) + 0.5 with correct sign bit |
| 1255 |
//add 2 to make it 2*\mu+0.5, shift it up to missing MSBs |
| 1256 |
val |= (v_n + 2) << (p - 1); |
| 1257 |
} |
| 1258 |
dp[0] = val; |
| 1259 |
|
| 1260 |
v_n = 0; |
| 1261 |
val = 0; |
| 1262 |
bit = 1; |
| 1263 |
if (inf & (1 << (4 + bit))) |
| 1264 |
{ |
| 1265 |
//get 32 bits of magsgn data |
| 1266 |
ui32 ms_val = frwd_fetch<0xFF>(&magsgn); |
| 1267 |
ui32 m_n = U_q - ((inf >> (12 + bit)) & 1); // remove e_k |
| 1268 |
frwd_advance(&magsgn, m_n); //consume m_n |
| 1269 |
|
| 1270 |
val = ms_val << 31; // get sign bit |
| 1271 |
v_n = ms_val & ((1 << m_n) - 1); // keep only m_n bits |
| 1272 |
v_n |= ((inf >> (8 + bit)) & 1) << m_n; // add EMB e_1 as MSB |
| 1273 |
v_n |= 1; // add center of bin |
| 1274 |
//v_n now has 2 * (\mu - 1) + 0.5 with correct sign bit |
| 1275 |
//add 2 to make it 2*\mu+0.5, shift it up to missing MSBs |
| 1276 |
val |= (v_n + 2) << (p - 1); |
| 1277 |
} |
| 1278 |
dp[stride] = val; |
| 1279 |
vp[0] = prev_v_n | v_n; |
| 1280 |
prev_v_n = 0; |
| 1281 |
++dp; |
| 1282 |
if (++x >= width) |
| 1283 |
{ ++vp; break; } |
| 1284 |
|
| 1285 |
val = 0; |
| 1286 |
bit = 2; |
| 1287 |
if (inf & (1 << (4 + bit))) |
| 1288 |
{ |
| 1289 |
//get 32 bits of magsgn data |
| 1290 |
ui32 ms_val = frwd_fetch<0xFF>(&magsgn); |
| 1291 |
ui32 m_n = U_q - ((inf >> (12 + bit)) & 1); // remove e_k |
| 1292 |
frwd_advance(&magsgn, m_n); //consume m_n |
| 1293 |
|
| 1294 |
val = ms_val << 31; // get sign bit |
| 1295 |
v_n = ms_val & ((1 << m_n) - 1); // keep only m_n bits |
| 1296 |
v_n |= ((inf >> (8 + bit)) & 1) << m_n; // add EMB e_1 as MSB |
| 1297 |
v_n |= 1; // add center of bin |
| 1298 |
//v_n now has 2 * (\mu - 1) + 0.5 with correct sign bit |
| 1299 |
//add 2 to make it 2*\mu+0.5, shift it up to missing MSBs |
| 1300 |
val |= (v_n + 2) << (p - 1); |
| 1301 |
} |
| 1302 |
dp[0] = val; |
| 1303 |
|
| 1304 |
v_n = 0; |
| 1305 |
val = 0; |
| 1306 |
bit = 3; |
| 1307 |
if (inf & (1 << (4 + bit))) |
| 1308 |
{ |
| 1309 |
//get 32 bits of magsgn data |
| 1310 |
ui32 ms_val = frwd_fetch<0xFF>(&magsgn); |
| 1311 |
ui32 m_n = U_q - ((inf >> (12 + bit)) & 1); // remove e_k |
| 1312 |
frwd_advance(&magsgn, m_n); //consume m_n |
| 1313 |
|
| 1314 |
val = ms_val << 31; // get sign bit |
| 1315 |
v_n = ms_val & ((1 << m_n) - 1); // keep only m_n bits |
| 1316 |
v_n |= ((inf >> (8 + bit)) & 1) << m_n; // add EMB e_1 as MSB |
| 1317 |
v_n |= 1; // add center of bin |
| 1318 |
//v_n now has 2 * (\mu - 1) + 0.5 with correct sign bit |
| 1319 |
//add 2 to make it 2*\mu+0.5, shift it up to missing MSBs |
| 1320 |
val |= (v_n + 2) << (p - 1); |
| 1321 |
} |
| 1322 |
dp[stride] = val; |
| 1323 |
prev_v_n = v_n; |
| 1324 |
++dp; |
| 1325 |
++x; |
| 1326 |
} |
| 1327 |
vp[0] = prev_v_n; |
| 1328 |
} |
| 1329 |
} |
| 1330 |
|
| 1331 |
if (num_passes > 1) |
| 1332 |
{ |
| 1333 |
// We use scratch again, we can divide it into multiple regions |
| 1334 |
// sigma holds all the significant samples, and it cannot |
| 1335 |
// be modified after it is set. it will be used during the |
| 1336 |
// Magnitude Refinement Pass |
| 1337 |
ui16* const sigma = scratch; |
| 1338 |
|
| 1339 |
ui32 mstr = (width + 3u) >> 2; // divide by 4, since each |
| 1340 |
// ui16 contains 4 columns |
| 1341 |
mstr = ((mstr + 2u) + 7u) & ~7u; // multiples of 8 |
| 1342 |
|
| 1343 |
// We re-arrange quad significance, where each 4 consecutive |
| 1344 |
// bits represent one quad, into column significance, where, |
| 1345 |
// each 4 consequtive bits represent one column of 4 rows |
| 1346 |
{ |
| 1347 |
ui32 y; |
| 1348 |
for (y = 0; y < height; y += 4) |
| 1349 |
{ |
| 1350 |
ui16* sp = scratch + (y >> 1) * sstr; |
| 1351 |
ui16* dp = sigma + (y >> 2) * mstr; |
| 1352 |
for (ui32 x = 0; x < width; x += 4, sp += 4, ++dp) { |
| 1353 |
ui32 t0 = 0, t1 = 0; |
| 1354 |
t0 = ((sp[0 ] & 0x30u) >> 4) | ((sp[0 ] & 0xC0u) >> 2); |
| 1355 |
t0 |= ((sp[2 ] & 0x30u) << 4) | ((sp[2 ] & 0xC0u) << 6); |
| 1356 |
t1 = ((sp[0+sstr] & 0x30u) >> 2) | ((sp[0+sstr] & 0xC0u) ); |
| 1357 |
t1 |= ((sp[2+sstr] & 0x30u) << 6) | ((sp[2+sstr] & 0xC0u) << 8); |
| 1358 |
dp[0] = (ui16)(t0 | t1); |
| 1359 |
} |
| 1360 |
dp[0] = 0; // set an extra entry on the right with 0 |
| 1361 |
} |
| 1362 |
{ |
| 1363 |
// reset one row after the codeblock |
| 1364 |
ui16* dp = sigma + (y >> 2) * mstr; |
| 1365 |
for (ui32 x = 0; x < width; x += 4, ++dp) |
| 1366 |
dp[0] = 0; |
| 1367 |
dp[0] = 0; // set an extra entry on the right with 0 |
| 1368 |
} |
| 1369 |
} |
| 1370 |
|
| 1371 |
// We perform Significance Propagation Pass here |
| 1372 |
{ |
| 1373 |
// This stores significance information of the previous |
| 1374 |
// 4 rows. Significance information in this array includes |
| 1375 |
// all signicant samples in bitplane p - 1; that is, |
| 1376 |
// significant samples for bitplane p (discovered during the |
| 1377 |
// cleanup pass and stored in sigma) and samples that have recently |
| 1378 |
// became significant (during the SPP) in bitplane p-1. |
| 1379 |
// We store enough for the widest row, containing 1024 columns, |
| 1380 |
// which is equivalent to 256 of ui16, since each stores 4 columns. |
| 1381 |
// We add an extra 8 entries, just in case we need more |
| 1382 |
ui16 prev_row_sig[256 + 8] = {0}; // 528 Bytes |
| 1383 |
|
| 1384 |
frwd_struct sigprop; |
| 1385 |
frwd_init<0>(&sigprop, coded_data + lengths1, (int)lengths2); |
| 1386 |
|
| 1387 |
for (ui32 y = 0; y < height; y += 4) |
| 1388 |
{ |
| 1389 |
ui32 pattern = 0xFFFFu; // a pattern needed samples |
| 1390 |
if (height - y < 4) { |
| 1391 |
pattern = 0x7777u; |
| 1392 |
if (height - y < 3) { |
| 1393 |
pattern = 0x3333u; |
| 1394 |
if (height - y < 2) |
| 1395 |
pattern = 0x1111u; |
| 1396 |
} |
| 1397 |
} |
| 1398 |
|
| 1399 |
// prev holds sign. info. for the previous quad, together |
| 1400 |
// with the rows on top of it and below it. |
| 1401 |
ui32 prev = 0; |
| 1402 |
ui16 *prev_sig = prev_row_sig; |
| 1403 |
ui16 *cur_sig = sigma + (y >> 2) * mstr; |
| 1404 |
ui32 *dpp = decoded_data + y * stride; |
| 1405 |
for (ui32 x = 0; x < width; x += 4, ++cur_sig, ++prev_sig) |
| 1406 |
{ |
| 1407 |
// only rows and columns inside the stripe are included |
| 1408 |
si32 s = (si32)x + 4 - (si32)width; |
| 1409 |
s = ojph_max(s, 0); |
| 1410 |
pattern = pattern >> (s * 4); |
| 1411 |
|
| 1412 |
// We first find locations that need to be tested (potential |
| 1413 |
// SPP members); these location will end up in mbr |
| 1414 |
// In each iteration, we produce 16 bits because cwd can have |
| 1415 |
// up to 16 bits of significance information, followed by the |
| 1416 |
// corresponding 16 bits of sign information; therefore, it is |
| 1417 |
// sufficient to fetch 32 bit data per loop. |
| 1418 |
|
| 1419 |
// Althougth we are interested in 16 bits only, we load 32 bits. |
| 1420 |
// For the 16 bits we are producing, we need the next 4 bits -- |
| 1421 |
// We need data for at least 5 columns out of 8. |
| 1422 |
// Therefore loading 32 bits is easier than loading 16 bits |
| 1423 |
// twice. |
| 1424 |
ui32 ps = *(ui32*)prev_sig; |
| 1425 |
ui32 ns = *(ui32*)(cur_sig + mstr); |
| 1426 |
ui32 u = (ps & 0x88888888) >> 3; // the row on top |
| 1427 |
if (!stripe_causal) |
| 1428 |
u |= (ns & 0x11111111) << 3; // the row below |
| 1429 |
|
| 1430 |
ui32 cs = *(ui32*)cur_sig; |
| 1431 |
// vertical integration |
| 1432 |
ui32 mbr = cs; // this sig. info. |
| 1433 |
mbr |= (cs & 0x77777777) << 1; //above neighbors |
| 1434 |
mbr |= (cs & 0xEEEEEEEE) >> 1; //below neighbors |
| 1435 |
mbr |= u; |
| 1436 |
// horizontal integration |
| 1437 |
ui32 t = mbr; |
| 1438 |
mbr |= t << 4; // neighbors on the left |
| 1439 |
mbr |= t >> 4; // neighbors on the right |
| 1440 |
mbr |= prev >> 12; // significance of previous group |
| 1441 |
|
| 1442 |
// remove outside samples, and already significant samples |
| 1443 |
mbr &= pattern; |
| 1444 |
mbr &= ~cs; |
| 1445 |
|
| 1446 |
// find samples that become significant during the SPP |
| 1447 |
ui32 new_sig = mbr; |
| 1448 |
if (new_sig) |
| 1449 |
{ |
| 1450 |
ui32 cwd = frwd_fetch<0>(&sigprop); |
| 1451 |
|
| 1452 |
ui32 cnt = 0; |
| 1453 |
ui32 col_mask = 0xFu; |
| 1454 |
ui32 inv_sig = ~cs & pattern; |
| 1455 |
for (int i = 0; i < 16; i += 4, col_mask <<= 4) |
| 1456 |
{ |
| 1457 |
if ((col_mask & new_sig) == 0) |
| 1458 |
continue; |
| 1459 |
|
| 1460 |
//scan one column |
| 1461 |
ui32 sample_mask = 0x1111u & col_mask; |
| 1462 |
if (new_sig & sample_mask) |
| 1463 |
{ |
| 1464 |
new_sig &= ~sample_mask; |
| 1465 |
if (cwd & 1) |
| 1466 |
{ |
| 1467 |
ui32 t = 0x33u << i; |
| 1468 |
new_sig |= t & inv_sig; |
| 1469 |
} |
| 1470 |
cwd >>= 1; ++cnt; |
| 1471 |
} |
| 1472 |
|
| 1473 |
sample_mask <<= 1; |
| 1474 |
if (new_sig & sample_mask) |
| 1475 |
{ |
| 1476 |
new_sig &= ~sample_mask; |
| 1477 |
if (cwd & 1) |
| 1478 |
{ |
| 1479 |
ui32 t = 0x76u << i; |
| 1480 |
new_sig |= t & inv_sig; |
| 1481 |
} |
| 1482 |
cwd >>= 1; ++cnt; |
| 1483 |
} |
| 1484 |
|
| 1485 |
sample_mask <<= 1; |
| 1486 |
if (new_sig & sample_mask) |
| 1487 |
{ |
| 1488 |
new_sig &= ~sample_mask; |
| 1489 |
if (cwd & 1) |
| 1490 |
{ |
| 1491 |
ui32 t = 0xECu << i; |
| 1492 |
new_sig |= t & inv_sig; |
| 1493 |
} |
| 1494 |
cwd >>= 1; ++cnt; |
| 1495 |
} |
| 1496 |
|
| 1497 |
sample_mask <<= 1; |
| 1498 |
if (new_sig & sample_mask) |
| 1499 |
{ |
| 1500 |
new_sig &= ~sample_mask; |
| 1501 |
if (cwd & 1) |
| 1502 |
{ |
| 1503 |
ui32 t = 0xC8u << i; |
| 1504 |
new_sig |= t & inv_sig; |
| 1505 |
} |
| 1506 |
cwd >>= 1; ++cnt; |
| 1507 |
} |
| 1508 |
} |
| 1509 |
|
| 1510 |
if (new_sig) |
| 1511 |
{ |
| 1512 |
// new_sig has newly-discovered sig. samples during SPP |
| 1513 |
// find the signs and update decoded_data |
| 1514 |
ui32 *dp = dpp + x; |
| 1515 |
ui32 val = 3u << (p - 2); |
| 1516 |
col_mask = 0xFu; |
| 1517 |
for (int i = 0; i < 4; ++i, ++dp, col_mask <<= 4) |
| 1518 |
{ |
| 1519 |
if ((col_mask & new_sig) == 0) |
| 1520 |
continue; |
| 1521 |
|
| 1522 |
//scan 4 signs |
| 1523 |
ui32 sample_mask = 0x1111u & col_mask; |
| 1524 |
if (new_sig & sample_mask) |
| 1525 |
{ |
| 1526 |
assert(dp[0] == 0); |
| 1527 |
dp[0] = (cwd << 31) | val; |
| 1528 |
cwd >>= 1; ++cnt; |
| 1529 |
} |
| 1530 |
|
| 1531 |
sample_mask += sample_mask; |
| 1532 |
if (new_sig & sample_mask) |
| 1533 |
{ |
| 1534 |
assert(dp[stride] == 0); |
| 1535 |
dp[stride] = (cwd << 31) | val; |
| 1536 |
cwd >>= 1; ++cnt; |
| 1537 |
} |
| 1538 |
|
| 1539 |
sample_mask += sample_mask; |
| 1540 |
if (new_sig & sample_mask) |
| 1541 |
{ |
| 1542 |
assert(dp[2 * stride] == 0); |
| 1543 |
dp[2 * stride] = (cwd << 31) | val; |
| 1544 |
cwd >>= 1; ++cnt; |
| 1545 |
} |
| 1546 |
|
| 1547 |
sample_mask += sample_mask; |
| 1548 |
if (new_sig & sample_mask) |
| 1549 |
{ |
| 1550 |
assert(dp[3 * stride] == 0); |
| 1551 |
dp[3 * stride] = (cwd << 31) | val; |
| 1552 |
cwd >>= 1; ++cnt; |
| 1553 |
} |
| 1554 |
} |
| 1555 |
} |
| 1556 |
frwd_advance(&sigprop, cnt); |
| 1557 |
} |
| 1558 |
|
| 1559 |
new_sig |= cs; |
| 1560 |
*prev_sig = (ui16)(new_sig); |
| 1561 |
|
| 1562 |
// vertical integration for the new sig. info. |
| 1563 |
t = new_sig; |
| 1564 |
new_sig |= (t & 0x7777) << 1; //above neighbors |
| 1565 |
new_sig |= (t & 0xEEEE) >> 1; //below neighbors |
| 1566 |
// add sig. info. from the row on top and below |
| 1567 |
prev = new_sig | u; |
| 1568 |
// we need only the bits in 0xF000 |
| 1569 |
prev &= 0xF000; |
| 1570 |
} |
| 1571 |
} |
| 1572 |
} |
| 1573 |
|
| 1574 |
// We perform Magnitude Refinement Pass here |
| 1575 |
if (num_passes > 2) |
| 1576 |
{ |
| 1577 |
rev_struct magref; |
| 1578 |
rev_init_mrp(&magref, coded_data, (int)lengths1, (int)lengths2); |
| 1579 |
|
| 1580 |
for (ui32 y = 0; y < height; y += 4) |
| 1581 |
{ |
| 1582 |
ui32 *cur_sig = (ui32*)(sigma + (y >> 2) * mstr); |
| 1583 |
ui32 *dpp = decoded_data + y * stride; |
| 1584 |
ui32 half = 1 << (p - 2); |
| 1585 |
for (ui32 i = 0; i < width; i += 8) |
| 1586 |
{ |
| 1587 |
//Process one entry from sigma array at a time |
| 1588 |
// Each nibble (4 bits) in the sigma array represents 4 rows, |
| 1589 |
// and the 32 bits contain 8 columns |
| 1590 |
ui32 cwd = rev_fetch_mrp(&magref); // get 32 bit data |
| 1591 |
ui32 sig = *cur_sig++; // 32 bit that will be processed now |
| 1592 |
ui32 col_mask = 0xFu; // a mask for a column in sig |
| 1593 |
if (sig) // if any of the 32 bits are set |
| 1594 |
{ |
| 1595 |
for (int j = 0; j < 8; ++j) //one column at a time |
| 1596 |
{ |
| 1597 |
if (sig & col_mask) // lowest nibble |
| 1598 |
{ |
| 1599 |
ui32 *dp = dpp + i + j; // next column in decoded samples |
| 1600 |
ui32 sample_mask = 0x11111111u & col_mask; //LSB |
| 1601 |
|
| 1602 |
for (int k = 0; k < 4; ++k) { |
| 1603 |
if (sig & sample_mask) //if LSB is set |
| 1604 |
{ |
| 1605 |
assert(dp[0] != 0); // decoded value cannot be zero |
| 1606 |
assert((dp[0] & half) == 0); // no half |
| 1607 |
ui32 sym = cwd & 1; // get it value |
| 1608 |
sym = (1 - sym) << (p - 1); // previous center of bin |
| 1609 |
sym |= half; // put half the center of bin |
| 1610 |
dp[0] ^= sym; // remove old bin center and put new |
| 1611 |
cwd >>= 1; // consume word |
| 1612 |
} |
| 1613 |
sample_mask += sample_mask; //next row |
| 1614 |
dp += stride; // next samples row |
| 1615 |
} |
| 1616 |
} |
| 1617 |
col_mask <<= 4; //next column |
| 1618 |
} |
| 1619 |
} |
| 1620 |
// consume data according to the number of bits set |
| 1621 |
rev_advance_mrp(&magref, population_count(sig)); |
| 1622 |
} |
| 1623 |
} |
| 1624 |
} |
| 1625 |
} |
| 1626 |
return true; |
| 1627 |
} |
| 1628 |
} |
| 1629 |
} |
| 1630 |
|
| 1631 |
#ifndef _MSC_VER |
| 1632 |
#pragma GCC diagnostic pop |
| 1633 |
#endif |