ht_dec.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640
  1. //***************************************************************************/
  2. // This software is released under the 2-Clause BSD license, included
  3. // below.
  4. //
  5. // Copyright (c) 2021, Aous Naman
  6. // Copyright (c) 2021, Kakadu Software Pty Ltd, Australia
  7. // Copyright (c) 2021, 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 OpenJpeg software implementation.
  33. // File: ht_dec.c
  34. // Author: Aous Naman
  35. // Date: 01 September 2021
  36. //***************************************************************************/
  37. //***************************************************************************/
  38. /** @file ht_dec.c
  39. * @brief implements HTJ2K block decoder
  40. */
  41. #include <assert.h>
  42. #include <string.h>
  43. #include "opj_includes.h"
  44. #include "t1_ht_luts.h"
  45. /////////////////////////////////////////////////////////////////////////////
  46. // compiler detection
  47. /////////////////////////////////////////////////////////////////////////////
  48. #ifdef _MSC_VER
  49. #define OPJ_COMPILER_MSVC
  50. #elif (defined __GNUC__)
  51. #define OPJ_COMPILER_GNUC
  52. #endif
  53. //************************************************************************/
  54. /** @brief Displays the error message for disabling the decoding of SPP and
  55. * MRP passes
  56. */
  57. static OPJ_BOOL only_cleanup_pass_is_decoded = OPJ_FALSE;
  58. //************************************************************************/
  59. /** @brief Generates population count (i.e., the number of set bits)
  60. *
  61. * @param [in] val is the value for which population count is sought
  62. */
  63. static INLINE
  64. OPJ_UINT32 population_count(OPJ_UINT32 val)
  65. {
  66. #ifdef OPJ_COMPILER_MSVC
  67. return (OPJ_UINT32)__popcnt(val);
  68. #elif (defined OPJ_COMPILER_GNUC)
  69. return (OPJ_UINT32)__builtin_popcount(val);
  70. #else
  71. val -= ((val >> 1) & 0x55555555);
  72. val = (((val >> 2) & 0x33333333) + (val & 0x33333333));
  73. val = (((val >> 4) + val) & 0x0f0f0f0f);
  74. val += (val >> 8);
  75. val += (val >> 16);
  76. return (OPJ_UINT32)(val & 0x0000003f);
  77. #endif
  78. }
  79. //************************************************************************/
  80. /** @brief Counts the number of leading zeros
  81. *
  82. * @param [in] val is the value for which leading zero count is sought
  83. */
  84. #ifdef OPJ_COMPILER_MSVC
  85. #pragma intrinsic(_BitScanReverse)
  86. #endif
  87. static INLINE
  88. OPJ_UINT32 count_leading_zeros(OPJ_UINT32 val)
  89. {
  90. #ifdef OPJ_COMPILER_MSVC
  91. unsigned long result = 0;
  92. _BitScanReverse(&result, val);
  93. return 31U ^ (OPJ_UINT32)result;
  94. #elif (defined OPJ_COMPILER_GNUC)
  95. return (OPJ_UINT32)__builtin_clz(val);
  96. #else
  97. val |= (val >> 1);
  98. val |= (val >> 2);
  99. val |= (val >> 4);
  100. val |= (val >> 8);
  101. val |= (val >> 16);
  102. return 32U - population_count(val);
  103. #endif
  104. }
  105. //************************************************************************/
  106. /** @brief Read a little-endian serialized UINT32.
  107. *
  108. * @param [in] dataIn pointer to byte stream to read from
  109. */
  110. static INLINE OPJ_UINT32 read_le_uint32(const void* dataIn)
  111. {
  112. #if defined(OPJ_BIG_ENDIAN)
  113. const OPJ_UINT8* data = (const OPJ_UINT8*)dataIn;
  114. return ((OPJ_UINT32)data[0]) | (OPJ_UINT32)(data[1] << 8) | (OPJ_UINT32)(
  115. data[2] << 16) | (((
  116. OPJ_UINT32)data[3]) <<
  117. 24U);
  118. #else
  119. return *(OPJ_UINT32*)dataIn;
  120. #endif
  121. }
  122. //************************************************************************/
  123. /** @brief MEL state structure for reading and decoding the MEL bitstream
  124. *
  125. * A number of events is decoded from the MEL bitstream ahead of time
  126. * and stored in run/num_runs.
  127. * Each run represents the number of zero events before a one event.
  128. */
  129. typedef struct dec_mel {
  130. // data decoding machinery
  131. OPJ_UINT8* data; //!<the address of data (or bitstream)
  132. OPJ_UINT64 tmp; //!<temporary buffer for read data
  133. int bits; //!<number of bits stored in tmp
  134. int size; //!<number of bytes in MEL code
  135. OPJ_BOOL unstuff; //!<true if the next bit needs to be unstuffed
  136. int k; //!<state of MEL decoder
  137. // queue of decoded runs
  138. int num_runs; //!<number of decoded runs left in runs (maximum 8)
  139. OPJ_UINT64 runs; //!<runs of decoded MEL codewords (7 bits/run)
  140. } dec_mel_t;
  141. //************************************************************************/
  142. /** @brief Reads and unstuffs the MEL bitstream
  143. *
  144. * This design needs more bytes in the codeblock buffer than the length
  145. * of the cleanup pass by up to 2 bytes.
  146. *
  147. * Unstuffing removes the MSB of the byte following a byte whose
  148. * value is 0xFF; this prevents sequences larger than 0xFF7F in value
  149. * from appearing the bitstream.
  150. *
  151. * @param [in] melp is a pointer to dec_mel_t structure
  152. */
  153. static INLINE
  154. void mel_read(dec_mel_t *melp)
  155. {
  156. OPJ_UINT32 val;
  157. int bits;
  158. OPJ_UINT32 t;
  159. OPJ_BOOL unstuff;
  160. if (melp->bits > 32) { //there are enough bits in the tmp variable
  161. return; // return without reading new data
  162. }
  163. val = 0xFFFFFFFF; // feed in 0xFF if buffer is exhausted
  164. if (melp->size > 4) { // if there is more than 4 bytes the MEL segment
  165. val = read_le_uint32(melp->data); // read 32 bits from MEL data
  166. melp->data += 4; // advance pointer
  167. melp->size -= 4; // reduce counter
  168. } else if (melp->size > 0) { // 4 or less
  169. OPJ_UINT32 m, v;
  170. int i = 0;
  171. while (melp->size > 1) {
  172. OPJ_UINT32 v = *melp->data++; // read one byte at a time
  173. OPJ_UINT32 m = ~(0xFFu << i); // mask of location
  174. val = (val & m) | (v << i); // put byte in its correct location
  175. --melp->size;
  176. i += 8;
  177. }
  178. // size equal to 1
  179. v = *melp->data++; // the one before the last is different
  180. v |= 0xF; // MEL and VLC segments can overlap
  181. m = ~(0xFFu << i);
  182. val = (val & m) | (v << i);
  183. --melp->size;
  184. }
  185. // next we unstuff them before adding them to the buffer
  186. bits = 32 - melp->unstuff; // number of bits in val, subtract 1 if
  187. // the previously read byte requires
  188. // unstuffing
  189. // data is unstuffed and accumulated in t
  190. // bits has the number of bits in t
  191. t = val & 0xFF;
  192. unstuff = ((val & 0xFF) == 0xFF); // true if the byte needs unstuffing
  193. bits -= unstuff; // there is one less bit in t if unstuffing is needed
  194. t = t << (8 - unstuff); // move up to make room for the next byte
  195. //this is a repeat of the above
  196. t |= (val >> 8) & 0xFF;
  197. unstuff = (((val >> 8) & 0xFF) == 0xFF);
  198. bits -= unstuff;
  199. t = t << (8 - unstuff);
  200. t |= (val >> 16) & 0xFF;
  201. unstuff = (((val >> 16) & 0xFF) == 0xFF);
  202. bits -= unstuff;
  203. t = t << (8 - unstuff);
  204. t |= (val >> 24) & 0xFF;
  205. melp->unstuff = (((val >> 24) & 0xFF) == 0xFF);
  206. // move t to tmp, and push the result all the way up, so we read from
  207. // the MSB
  208. melp->tmp |= ((OPJ_UINT64)t) << (64 - bits - melp->bits);
  209. melp->bits += bits; //increment the number of bits in tmp
  210. }
  211. //************************************************************************/
  212. /** @brief Decodes unstuffed MEL segment bits stored in tmp to runs
  213. *
  214. * Runs are stored in "runs" and the number of runs in "num_runs".
  215. * Each run represents a number of zero events that may or may not
  216. * terminate in a 1 event.
  217. * Each run is stored in 7 bits. The LSB is 1 if the run terminates in
  218. * a 1 event, 0 otherwise. The next 6 bits, for the case terminating
  219. * with 1, contain the number of consecutive 0 zero events * 2; for the
  220. * case terminating with 0, they store (number of consecutive 0 zero
  221. * events - 1) * 2.
  222. * A total of 6 bits (made up of 1 + 5) should have been enough.
  223. *
  224. * @param [in] melp is a pointer to dec_mel_t structure
  225. */
  226. static INLINE
  227. void mel_decode(dec_mel_t *melp)
  228. {
  229. static const int mel_exp[13] = { //MEL exponents
  230. 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5
  231. };
  232. if (melp->bits < 6) { // if there are less than 6 bits in tmp
  233. mel_read(melp); // then read from the MEL bitstream
  234. }
  235. // 6 bits is the largest decodable MEL cwd
  236. //repeat so long that there is enough decodable bits in tmp,
  237. // and the runs store is not full (num_runs < 8)
  238. while (melp->bits >= 6 && melp->num_runs < 8) {
  239. int eval = mel_exp[melp->k]; // number of bits associated with state
  240. int run = 0;
  241. if (melp->tmp & (1ull << 63)) { //The next bit to decode (stored in MSB)
  242. //one is found
  243. run = 1 << eval;
  244. run--; // consecutive runs of 0 events - 1
  245. melp->k = melp->k + 1 < 12 ? melp->k + 1 : 12;//increment, max is 12
  246. melp->tmp <<= 1; // consume one bit from tmp
  247. melp->bits -= 1;
  248. run = run << 1; // a stretch of zeros not terminating in one
  249. } else {
  250. //0 is found
  251. run = (int)(melp->tmp >> (63 - eval)) & ((1 << eval) - 1);
  252. melp->k = melp->k - 1 > 0 ? melp->k - 1 : 0; //decrement, min is 0
  253. melp->tmp <<= eval + 1; //consume eval + 1 bits (max is 6)
  254. melp->bits -= eval + 1;
  255. run = (run << 1) + 1; // a stretch of zeros terminating with one
  256. }
  257. eval = melp->num_runs * 7; // 7 bits per run
  258. melp->runs &= ~((OPJ_UINT64)0x3F << eval); // 6 bits are sufficient
  259. melp->runs |= ((OPJ_UINT64)run) << eval; // store the value in runs
  260. melp->num_runs++; // increment count
  261. }
  262. }
  263. //************************************************************************/
  264. /** @brief Initiates a dec_mel_t structure for MEL decoding and reads
  265. * some bytes in order to get the read address to a multiple
  266. * of 4
  267. *
  268. * @param [in] melp is a pointer to dec_mel_t structure
  269. * @param [in] bbuf is a pointer to byte buffer
  270. * @param [in] lcup is the length of MagSgn+MEL+VLC segments
  271. * @param [in] scup is the length of MEL+VLC segments
  272. */
  273. static INLINE
  274. void mel_init(dec_mel_t *melp, OPJ_UINT8* bbuf, int lcup, int scup)
  275. {
  276. int num;
  277. int i;
  278. melp->data = bbuf + lcup - scup; // move the pointer to the start of MEL
  279. melp->bits = 0; // 0 bits in tmp
  280. melp->tmp = 0; //
  281. melp->unstuff = OPJ_FALSE; // no unstuffing
  282. melp->size = scup - 1; // size is the length of MEL+VLC-1
  283. melp->k = 0; // 0 for state
  284. melp->num_runs = 0; // num_runs is 0
  285. melp->runs = 0; //
  286. //This code is borrowed; original is for a different architecture
  287. //These few lines take care of the case where data is not at a multiple
  288. // of 4 boundary. It reads 1,2,3 up to 4 bytes from the MEL segment
  289. num = 4 - (int)((intptr_t)(melp->data) & 0x3);
  290. for (i = 0; i < num; ++i) { // this code is similar to mel_read
  291. OPJ_UINT64 d;
  292. int d_bits;
  293. assert(melp->unstuff == OPJ_FALSE || melp->data[0] <= 0x8F);
  294. d = (melp->size > 0) ? *melp->data : 0xFF; // if buffer is consumed
  295. // set data to 0xFF
  296. if (melp->size == 1) {
  297. d |= 0xF; //if this is MEL+VLC-1, set LSBs to 0xF
  298. }
  299. // see the standard
  300. melp->data += melp->size-- > 0; //increment if the end is not reached
  301. d_bits = 8 - melp->unstuff; //if unstuffing is needed, reduce by 1
  302. melp->tmp = (melp->tmp << d_bits) | d; //store bits in tmp
  303. melp->bits += d_bits; //increment tmp by number of bits
  304. melp->unstuff = ((d & 0xFF) == 0xFF); //true of next byte needs
  305. //unstuffing
  306. }
  307. melp->tmp <<= (64 - melp->bits); //push all the way up so the first bit
  308. // is the MSB
  309. }
  310. //************************************************************************/
  311. /** @brief Retrieves one run from dec_mel_t; if there are no runs stored
  312. * MEL segment is decoded
  313. *
  314. * @param [in] melp is a pointer to dec_mel_t structure
  315. */
  316. static INLINE
  317. int mel_get_run(dec_mel_t *melp)
  318. {
  319. int t;
  320. if (melp->num_runs == 0) { //if no runs, decode more bit from MEL segment
  321. mel_decode(melp);
  322. }
  323. t = melp->runs & 0x7F; //retrieve one run
  324. melp->runs >>= 7; // remove the retrieved run
  325. melp->num_runs--;
  326. return t; // return run
  327. }
  328. //************************************************************************/
  329. /** @brief A structure for reading and unstuffing a segment that grows
  330. * backward, such as VLC and MRP
  331. */
  332. typedef struct rev_struct {
  333. //storage
  334. OPJ_UINT8* data; //!<pointer to where to read data
  335. OPJ_UINT64 tmp; //!<temporary buffer of read data
  336. OPJ_UINT32 bits; //!<number of bits stored in tmp
  337. int size; //!<number of bytes left
  338. OPJ_BOOL unstuff; //!<true if the last byte is more than 0x8F
  339. //!<then the current byte is unstuffed if it is 0x7F
  340. } rev_struct_t;
  341. //************************************************************************/
  342. /** @brief Read and unstuff data from a backwardly-growing segment
  343. *
  344. * This reader can read up to 8 bytes from before the VLC segment.
  345. * Care must be taken not read from unreadable memory, causing a
  346. * segmentation fault.
  347. *
  348. * Note that there is another subroutine rev_read_mrp that is slightly
  349. * different. The other one fills zeros when the buffer is exhausted.
  350. * This one basically does not care if the bytes are consumed, because
  351. * any extra data should not be used in the actual decoding.
  352. *
  353. * Unstuffing is needed to prevent sequences more than 0xFF8F from
  354. * appearing in the bits stream; since we are reading backward, we keep
  355. * watch when a value larger than 0x8F appears in the bitstream.
  356. * If the byte following this is 0x7F, we unstuff this byte (ignore the
  357. * MSB of that byte, which should be 0).
  358. *
  359. * @param [in] vlcp is a pointer to rev_struct_t structure
  360. */
  361. static INLINE
  362. void rev_read(rev_struct_t *vlcp)
  363. {
  364. OPJ_UINT32 val;
  365. OPJ_UINT32 tmp;
  366. OPJ_UINT32 bits;
  367. OPJ_BOOL unstuff;
  368. //process 4 bytes at a time
  369. if (vlcp->bits > 32) { // if there are more than 32 bits in tmp, then
  370. return; // reading 32 bits can overflow vlcp->tmp
  371. }
  372. val = 0;
  373. //the next line (the if statement) needs to be tested first
  374. if (vlcp->size > 3) { // if there are more than 3 bytes left in VLC
  375. // (vlcp->data - 3) move pointer back to read 32 bits at once
  376. val = read_le_uint32(vlcp->data - 3); // then read 32 bits
  377. vlcp->data -= 4; // move data pointer back by 4
  378. vlcp->size -= 4; // reduce available byte by 4
  379. } else if (vlcp->size > 0) { // 4 or less
  380. int i = 24;
  381. while (vlcp->size > 0) {
  382. OPJ_UINT32 v = *vlcp->data--; // read one byte at a time
  383. val |= (v << i); // put byte in its correct location
  384. --vlcp->size;
  385. i -= 8;
  386. }
  387. }
  388. //accumulate in tmp, number of bits in tmp are stored in bits
  389. tmp = val >> 24; //start with the MSB byte
  390. // test unstuff (previous byte is >0x8F), and this byte is 0x7F
  391. bits = 8u - ((vlcp->unstuff && (((val >> 24) & 0x7F) == 0x7F)) ? 1u : 0u);
  392. unstuff = (val >> 24) > 0x8F; //this is for the next byte
  393. tmp |= ((val >> 16) & 0xFF) << bits; //process the next byte
  394. bits += 8u - ((unstuff && (((val >> 16) & 0x7F) == 0x7F)) ? 1u : 0u);
  395. unstuff = ((val >> 16) & 0xFF) > 0x8F;
  396. tmp |= ((val >> 8) & 0xFF) << bits;
  397. bits += 8u - ((unstuff && (((val >> 8) & 0x7F) == 0x7F)) ? 1u : 0u);
  398. unstuff = ((val >> 8) & 0xFF) > 0x8F;
  399. tmp |= (val & 0xFF) << bits;
  400. bits += 8u - ((unstuff && ((val & 0x7F) == 0x7F)) ? 1u : 0u);
  401. unstuff = (val & 0xFF) > 0x8F;
  402. // now move the read and unstuffed bits into vlcp->tmp
  403. vlcp->tmp |= (OPJ_UINT64)tmp << vlcp->bits;
  404. vlcp->bits += bits;
  405. vlcp->unstuff = unstuff; // this for the next read
  406. }
  407. //************************************************************************/
  408. /** @brief Initiates the rev_struct_t structure and reads a few bytes to
  409. * move the read address to multiple of 4
  410. *
  411. * There is another similar rev_init_mrp subroutine. The difference is
  412. * that this one, rev_init, discards the first 12 bits (they have the
  413. * sum of the lengths of VLC and MEL segments), and first unstuff depends
  414. * on first 4 bits.
  415. *
  416. * @param [in] vlcp is a pointer to rev_struct_t structure
  417. * @param [in] data is a pointer to byte at the start of the cleanup pass
  418. * @param [in] lcup is the length of MagSgn+MEL+VLC segments
  419. * @param [in] scup is the length of MEL+VLC segments
  420. */
  421. static INLINE
  422. void rev_init(rev_struct_t *vlcp, OPJ_UINT8* data, int lcup, int scup)
  423. {
  424. OPJ_UINT32 d;
  425. int num, tnum, i;
  426. //first byte has only the upper 4 bits
  427. vlcp->data = data + lcup - 2;
  428. //size can not be larger than this, in fact it should be smaller
  429. vlcp->size = scup - 2;
  430. d = *vlcp->data--; // read one byte (this is a half byte)
  431. vlcp->tmp = d >> 4; // both initialize and set
  432. vlcp->bits = 4 - ((vlcp->tmp & 7) == 7); //check standard
  433. vlcp->unstuff = (d | 0xF) > 0x8F; //this is useful for the next byte
  434. //This code is designed for an architecture that read address should
  435. // align to the read size (address multiple of 4 if read size is 4)
  436. //These few lines take care of the case where data is not at a multiple
  437. // of 4 boundary. It reads 1,2,3 up to 4 bytes from the VLC bitstream.
  438. // To read 32 bits, read from (vlcp->data - 3)
  439. num = 1 + (int)((intptr_t)(vlcp->data) & 0x3);
  440. tnum = num < vlcp->size ? num : vlcp->size;
  441. for (i = 0; i < tnum; ++i) {
  442. OPJ_UINT64 d;
  443. OPJ_UINT32 d_bits;
  444. d = *vlcp->data--; // read one byte and move read pointer
  445. //check if the last byte was >0x8F (unstuff == true) and this is 0x7F
  446. d_bits = 8u - ((vlcp->unstuff && ((d & 0x7F) == 0x7F)) ? 1u : 0u);
  447. vlcp->tmp |= d << vlcp->bits; // move data to vlcp->tmp
  448. vlcp->bits += d_bits;
  449. vlcp->unstuff = d > 0x8F; // for next byte
  450. }
  451. vlcp->size -= tnum;
  452. rev_read(vlcp); // read another 32 buts
  453. }
  454. //************************************************************************/
  455. /** @brief Retrieves 32 bits from the head of a rev_struct structure
  456. *
  457. * By the end of this call, vlcp->tmp must have no less than 33 bits
  458. *
  459. * @param [in] vlcp is a pointer to rev_struct structure
  460. */
  461. static INLINE
  462. OPJ_UINT32 rev_fetch(rev_struct_t *vlcp)
  463. {
  464. if (vlcp->bits < 32) { // if there are less then 32 bits, read more
  465. rev_read(vlcp); // read 32 bits, but unstuffing might reduce this
  466. if (vlcp->bits < 32) { // if there is still space in vlcp->tmp for 32 bits
  467. rev_read(vlcp); // read another 32
  468. }
  469. }
  470. return (OPJ_UINT32)vlcp->tmp; // return the head (bottom-most) of vlcp->tmp
  471. }
  472. //************************************************************************/
  473. /** @brief Consumes num_bits from a rev_struct structure
  474. *
  475. * @param [in] vlcp is a pointer to rev_struct structure
  476. * @param [in] num_bits is the number of bits to be removed
  477. */
  478. static INLINE
  479. OPJ_UINT32 rev_advance(rev_struct_t *vlcp, OPJ_UINT32 num_bits)
  480. {
  481. assert(num_bits <= vlcp->bits); // vlcp->tmp must have more than num_bits
  482. vlcp->tmp >>= num_bits; // remove bits
  483. vlcp->bits -= num_bits; // decrement the number of bits
  484. return (OPJ_UINT32)vlcp->tmp;
  485. }
  486. //************************************************************************/
  487. /** @brief Reads and unstuffs from rev_struct
  488. *
  489. * This is different than rev_read in that this fills in zeros when the
  490. * the available data is consumed. The other does not care about the
  491. * values when all data is consumed.
  492. *
  493. * See rev_read for more information about unstuffing
  494. *
  495. * @param [in] mrp is a pointer to rev_struct structure
  496. */
  497. static INLINE
  498. void rev_read_mrp(rev_struct_t *mrp)
  499. {
  500. OPJ_UINT32 val;
  501. OPJ_UINT32 tmp;
  502. OPJ_UINT32 bits;
  503. OPJ_BOOL unstuff;
  504. //process 4 bytes at a time
  505. if (mrp->bits > 32) {
  506. return;
  507. }
  508. val = 0;
  509. if (mrp->size > 3) { // If there are 3 byte or more
  510. // (mrp->data - 3) move pointer back to read 32 bits at once
  511. val = read_le_uint32(mrp->data - 3); // read 32 bits
  512. mrp->data -= 4; // move back pointer
  513. mrp->size -= 4; // reduce count
  514. } else if (mrp->size > 0) {
  515. int i = 24;
  516. while (mrp->size > 0) {
  517. OPJ_UINT32 v = *mrp->data--; // read one byte at a time
  518. val |= (v << i); // put byte in its correct location
  519. --mrp->size;
  520. i -= 8;
  521. }
  522. }
  523. //accumulate in tmp, and keep count in bits
  524. tmp = val >> 24;
  525. //test if the last byte > 0x8F (unstuff must be true) and this is 0x7F
  526. bits = 8u - ((mrp->unstuff && (((val >> 24) & 0x7F) == 0x7F)) ? 1u : 0u);
  527. unstuff = (val >> 24) > 0x8F;
  528. //process the next byte
  529. tmp |= ((val >> 16) & 0xFF) << bits;
  530. bits += 8u - ((unstuff && (((val >> 16) & 0x7F) == 0x7F)) ? 1u : 0u);
  531. unstuff = ((val >> 16) & 0xFF) > 0x8F;
  532. tmp |= ((val >> 8) & 0xFF) << bits;
  533. bits += 8u - ((unstuff && (((val >> 8) & 0x7F) == 0x7F)) ? 1u : 0u);
  534. unstuff = ((val >> 8) & 0xFF) > 0x8F;
  535. tmp |= (val & 0xFF) << bits;
  536. bits += 8u - ((unstuff && ((val & 0x7F) == 0x7F)) ? 1u : 0u);
  537. unstuff = (val & 0xFF) > 0x8F;
  538. mrp->tmp |= (OPJ_UINT64)tmp << mrp->bits; // move data to mrp pointer
  539. mrp->bits += bits;
  540. mrp->unstuff = unstuff; // next byte
  541. }
  542. //************************************************************************/
  543. /** @brief Initialized rev_struct structure for MRP segment, and reads
  544. * a number of bytes such that the next 32 bits read are from
  545. * an address that is a multiple of 4. Note this is designed for
  546. * an architecture that read size must be compatible with the
  547. * alignment of the read address
  548. *
  549. * There is another similar subroutine rev_init. This subroutine does
  550. * NOT skip the first 12 bits, and starts with unstuff set to true.
  551. *
  552. * @param [in] mrp is a pointer to rev_struct structure
  553. * @param [in] data is a pointer to byte at the start of the cleanup pass
  554. * @param [in] lcup is the length of MagSgn+MEL+VLC segments
  555. * @param [in] len2 is the length of SPP+MRP segments
  556. */
  557. static INLINE
  558. void rev_init_mrp(rev_struct_t *mrp, OPJ_UINT8* data, int lcup, int len2)
  559. {
  560. int num, i;
  561. mrp->data = data + lcup + len2 - 1;
  562. mrp->size = len2;
  563. mrp->unstuff = OPJ_TRUE;
  564. mrp->bits = 0;
  565. mrp->tmp = 0;
  566. //This code is designed for an architecture that read address should
  567. // align to the read size (address multiple of 4 if read size is 4)
  568. //These few lines take care of the case where data is not at a multiple
  569. // of 4 boundary. It reads 1,2,3 up to 4 bytes from the MRP stream
  570. num = 1 + (int)((intptr_t)(mrp->data) & 0x3);
  571. for (i = 0; i < num; ++i) {
  572. OPJ_UINT64 d;
  573. OPJ_UINT32 d_bits;
  574. //read a byte, 0 if no more data
  575. d = (mrp->size-- > 0) ? *mrp->data-- : 0;
  576. //check if unstuffing is needed
  577. d_bits = 8u - ((mrp->unstuff && ((d & 0x7F) == 0x7F)) ? 1u : 0u);
  578. mrp->tmp |= d << mrp->bits; // move data to vlcp->tmp
  579. mrp->bits += d_bits;
  580. mrp->unstuff = d > 0x8F; // for next byte
  581. }
  582. rev_read_mrp(mrp);
  583. }
  584. //************************************************************************/
  585. /** @brief Retrieves 32 bits from the head of a rev_struct structure
  586. *
  587. * By the end of this call, mrp->tmp must have no less than 33 bits
  588. *
  589. * @param [in] mrp is a pointer to rev_struct structure
  590. */
  591. static INLINE
  592. OPJ_UINT32 rev_fetch_mrp(rev_struct_t *mrp)
  593. {
  594. if (mrp->bits < 32) { // if there are less than 32 bits in mrp->tmp
  595. rev_read_mrp(mrp); // read 30-32 bits from mrp
  596. if (mrp->bits < 32) { // if there is a space of 32 bits
  597. rev_read_mrp(mrp); // read more
  598. }
  599. }
  600. return (OPJ_UINT32)mrp->tmp; // return the head of mrp->tmp
  601. }
  602. //************************************************************************/
  603. /** @brief Consumes num_bits from a rev_struct structure
  604. *
  605. * @param [in] mrp is a pointer to rev_struct structure
  606. * @param [in] num_bits is the number of bits to be removed
  607. */
  608. static INLINE
  609. OPJ_UINT32 rev_advance_mrp(rev_struct_t *mrp, OPJ_UINT32 num_bits)
  610. {
  611. assert(num_bits <= mrp->bits); // we must not consume more than mrp->bits
  612. mrp->tmp >>= num_bits; // discard the lowest num_bits bits
  613. mrp->bits -= num_bits;
  614. return (OPJ_UINT32)mrp->tmp; // return data after consumption
  615. }
  616. //************************************************************************/
  617. /** @brief Decode initial UVLC to get the u value (or u_q)
  618. *
  619. * @param [in] vlc is the head of the VLC bitstream
  620. * @param [in] mode is 0, 1, 2, 3, or 4. Values in 0 to 3 are composed of
  621. * u_off of 1st quad and 2nd quad of a quad pair. The value
  622. * 4 occurs when both bits are 1, and the event decoded
  623. * from MEL bitstream is also 1.
  624. * @param [out] u is the u value (or u_q) + 1. Note: we produce u + 1;
  625. * this value is a partial calculation of u + kappa.
  626. */
  627. static INLINE
  628. OPJ_UINT32 decode_init_uvlc(OPJ_UINT32 vlc, OPJ_UINT32 mode, OPJ_UINT32 *u)
  629. {
  630. //table stores possible decoding three bits from vlc
  631. // there are 8 entries for xx1, x10, 100, 000, where x means do not care
  632. // table value is made up of
  633. // 2 bits in the LSB for prefix length
  634. // 3 bits for suffix length
  635. // 3 bits in the MSB for prefix value (u_pfx in Table 3 of ITU T.814)
  636. static const OPJ_UINT8 dec[8] = { // the index is the prefix codeword
  637. 3 | (5 << 2) | (5 << 5), //000 == 000, prefix codeword "000"
  638. 1 | (0 << 2) | (1 << 5), //001 == xx1, prefix codeword "1"
  639. 2 | (0 << 2) | (2 << 5), //010 == x10, prefix codeword "01"
  640. 1 | (0 << 2) | (1 << 5), //011 == xx1, prefix codeword "1"
  641. 3 | (1 << 2) | (3 << 5), //100 == 100, prefix codeword "001"
  642. 1 | (0 << 2) | (1 << 5), //101 == xx1, prefix codeword "1"
  643. 2 | (0 << 2) | (2 << 5), //110 == x10, prefix codeword "01"
  644. 1 | (0 << 2) | (1 << 5) //111 == xx1, prefix codeword "1"
  645. };
  646. OPJ_UINT32 consumed_bits = 0;
  647. if (mode == 0) { // both u_off are 0
  648. u[0] = u[1] = 1; //Kappa is 1 for initial line
  649. } else if (mode <= 2) { // u_off are either 01 or 10
  650. OPJ_UINT32 d;
  651. OPJ_UINT32 suffix_len;
  652. d = dec[vlc & 0x7]; //look at the least significant 3 bits
  653. vlc >>= d & 0x3; //prefix length
  654. consumed_bits += d & 0x3;
  655. suffix_len = ((d >> 2) & 0x7);
  656. consumed_bits += suffix_len;
  657. d = (d >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
  658. u[0] = (mode == 1) ? d + 1 : 1; // kappa is 1 for initial line
  659. u[1] = (mode == 1) ? 1 : d + 1; // kappa is 1 for initial line
  660. } else if (mode == 3) { // both u_off are 1, and MEL event is 0
  661. OPJ_UINT32 d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
  662. vlc >>= d1 & 0x3; // Consume bits
  663. consumed_bits += d1 & 0x3;
  664. if ((d1 & 0x3) > 2) {
  665. OPJ_UINT32 suffix_len;
  666. //u_{q_2} prefix
  667. u[1] = (vlc & 1) + 1 + 1; //Kappa is 1 for initial line
  668. ++consumed_bits;
  669. vlc >>= 1;
  670. suffix_len = ((d1 >> 2) & 0x7);
  671. consumed_bits += suffix_len;
  672. d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
  673. u[0] = d1 + 1; //Kappa is 1 for initial line
  674. } else {
  675. OPJ_UINT32 d2;
  676. OPJ_UINT32 suffix_len;
  677. d2 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
  678. vlc >>= d2 & 0x3; // Consume bits
  679. consumed_bits += d2 & 0x3;
  680. suffix_len = ((d1 >> 2) & 0x7);
  681. consumed_bits += suffix_len;
  682. d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
  683. u[0] = d1 + 1; //Kappa is 1 for initial line
  684. vlc >>= suffix_len;
  685. suffix_len = ((d2 >> 2) & 0x7);
  686. consumed_bits += suffix_len;
  687. d2 = (d2 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
  688. u[1] = d2 + 1; //Kappa is 1 for initial line
  689. }
  690. } else if (mode == 4) { // both u_off are 1, and MEL event is 1
  691. OPJ_UINT32 d1;
  692. OPJ_UINT32 d2;
  693. OPJ_UINT32 suffix_len;
  694. d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
  695. vlc >>= d1 & 0x3; // Consume bits
  696. consumed_bits += d1 & 0x3;
  697. d2 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
  698. vlc >>= d2 & 0x3; // Consume bits
  699. consumed_bits += d2 & 0x3;
  700. suffix_len = ((d1 >> 2) & 0x7);
  701. consumed_bits += suffix_len;
  702. d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
  703. u[0] = d1 + 3; // add 2+kappa
  704. vlc >>= suffix_len;
  705. suffix_len = ((d2 >> 2) & 0x7);
  706. consumed_bits += suffix_len;
  707. d2 = (d2 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
  708. u[1] = d2 + 3; // add 2+kappa
  709. }
  710. return consumed_bits;
  711. }
  712. //************************************************************************/
  713. /** @brief Decode non-initial UVLC to get the u value (or u_q)
  714. *
  715. * @param [in] vlc is the head of the VLC bitstream
  716. * @param [in] mode is 0, 1, 2, or 3. The 1st bit is u_off of 1st quad
  717. * and 2nd for 2nd quad of a quad pair
  718. * @param [out] u is the u value (or u_q) + 1. Note: we produce u + 1;
  719. * this value is a partial calculation of u + kappa.
  720. */
  721. static INLINE
  722. OPJ_UINT32 decode_noninit_uvlc(OPJ_UINT32 vlc, OPJ_UINT32 mode, OPJ_UINT32 *u)
  723. {
  724. //table stores possible decoding three bits from vlc
  725. // there are 8 entries for xx1, x10, 100, 000, where x means do not care
  726. // table value is made up of
  727. // 2 bits in the LSB for prefix length
  728. // 3 bits for suffix length
  729. // 3 bits in the MSB for prefix value (u_pfx in Table 3 of ITU T.814)
  730. static const OPJ_UINT8 dec[8] = {
  731. 3 | (5 << 2) | (5 << 5), //000 == 000, prefix codeword "000"
  732. 1 | (0 << 2) | (1 << 5), //001 == xx1, prefix codeword "1"
  733. 2 | (0 << 2) | (2 << 5), //010 == x10, prefix codeword "01"
  734. 1 | (0 << 2) | (1 << 5), //011 == xx1, prefix codeword "1"
  735. 3 | (1 << 2) | (3 << 5), //100 == 100, prefix codeword "001"
  736. 1 | (0 << 2) | (1 << 5), //101 == xx1, prefix codeword "1"
  737. 2 | (0 << 2) | (2 << 5), //110 == x10, prefix codeword "01"
  738. 1 | (0 << 2) | (1 << 5) //111 == xx1, prefix codeword "1"
  739. };
  740. OPJ_UINT32 consumed_bits = 0;
  741. if (mode == 0) {
  742. u[0] = u[1] = 1; //for kappa
  743. } else if (mode <= 2) { //u_off are either 01 or 10
  744. OPJ_UINT32 d;
  745. OPJ_UINT32 suffix_len;
  746. d = dec[vlc & 0x7]; //look at the least significant 3 bits
  747. vlc >>= d & 0x3; //prefix length
  748. consumed_bits += d & 0x3;
  749. suffix_len = ((d >> 2) & 0x7);
  750. consumed_bits += suffix_len;
  751. d = (d >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
  752. u[0] = (mode == 1) ? d + 1 : 1; //for kappa
  753. u[1] = (mode == 1) ? 1 : d + 1; //for kappa
  754. } else if (mode == 3) { // both u_off are 1
  755. OPJ_UINT32 d1;
  756. OPJ_UINT32 d2;
  757. OPJ_UINT32 suffix_len;
  758. d1 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
  759. vlc >>= d1 & 0x3; // Consume bits
  760. consumed_bits += d1 & 0x3;
  761. d2 = dec[vlc & 0x7]; // LSBs of VLC are prefix codeword
  762. vlc >>= d2 & 0x3; // Consume bits
  763. consumed_bits += d2 & 0x3;
  764. suffix_len = ((d1 >> 2) & 0x7);
  765. consumed_bits += suffix_len;
  766. d1 = (d1 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
  767. u[0] = d1 + 1; //1 for kappa
  768. vlc >>= suffix_len;
  769. suffix_len = ((d2 >> 2) & 0x7);
  770. consumed_bits += suffix_len;
  771. d2 = (d2 >> 5) + (vlc & ((1U << suffix_len) - 1)); // u value
  772. u[1] = d2 + 1; //1 for kappa
  773. }
  774. return consumed_bits;
  775. }
  776. //************************************************************************/
  777. /** @brief State structure for reading and unstuffing of forward-growing
  778. * bitstreams; these are: MagSgn and SPP bitstreams
  779. */
  780. typedef struct frwd_struct {
  781. const OPJ_UINT8* data; //!<pointer to bitstream
  782. OPJ_UINT64 tmp; //!<temporary buffer of read data
  783. OPJ_UINT32 bits; //!<number of bits stored in tmp
  784. OPJ_BOOL unstuff; //!<true if a bit needs to be unstuffed from next byte
  785. int size; //!<size of data
  786. OPJ_UINT32 X; //!<0 or 0xFF, X's are inserted at end of bitstream
  787. } frwd_struct_t;
  788. //************************************************************************/
  789. /** @brief Read and unstuffs 32 bits from forward-growing bitstream
  790. *
  791. * A subroutine to read from both the MagSgn or SPP bitstreams;
  792. * in particular, when MagSgn bitstream is consumed, 0xFF's are fed,
  793. * while when SPP is exhausted 0's are fed in.
  794. * X controls this value.
  795. *
  796. * Unstuffing prevent sequences that are more than 0xFF7F from appearing
  797. * in the conpressed sequence. So whenever a value of 0xFF is coded, the
  798. * MSB of the next byte is set 0 and must be ignored during decoding.
  799. *
  800. * Reading can go beyond the end of buffer by up to 3 bytes.
  801. *
  802. * @param [in] msp is a pointer to frwd_struct_t structure
  803. *
  804. */
  805. static INLINE
  806. void frwd_read(frwd_struct_t *msp)
  807. {
  808. OPJ_UINT32 val;
  809. OPJ_UINT32 bits;
  810. OPJ_UINT32 t;
  811. OPJ_BOOL unstuff;
  812. assert(msp->bits <= 32); // assert that there is a space for 32 bits
  813. val = 0u;
  814. if (msp->size > 3) {
  815. val = read_le_uint32(msp->data); // read 32 bits
  816. msp->data += 4; // increment pointer
  817. msp->size -= 4; // reduce size
  818. } else if (msp->size > 0) {
  819. int i = 0;
  820. val = msp->X != 0 ? 0xFFFFFFFFu : 0;
  821. while (msp->size > 0) {
  822. OPJ_UINT32 v = *msp->data++; // read one byte at a time
  823. OPJ_UINT32 m = ~(0xFFu << i); // mask of location
  824. val = (val & m) | (v << i); // put one byte in its correct location
  825. --msp->size;
  826. i += 8;
  827. }
  828. } else {
  829. val = msp->X != 0 ? 0xFFFFFFFFu : 0;
  830. }
  831. // we accumulate in t and keep a count of the number of bits in bits
  832. bits = 8u - (msp->unstuff ? 1u : 0u);
  833. t = val & 0xFF;
  834. unstuff = ((val & 0xFF) == 0xFF); // Do we need unstuffing next?
  835. t |= ((val >> 8) & 0xFF) << bits;
  836. bits += 8u - (unstuff ? 1u : 0u);
  837. unstuff = (((val >> 8) & 0xFF) == 0xFF);
  838. t |= ((val >> 16) & 0xFF) << bits;
  839. bits += 8u - (unstuff ? 1u : 0u);
  840. unstuff = (((val >> 16) & 0xFF) == 0xFF);
  841. t |= ((val >> 24) & 0xFF) << bits;
  842. bits += 8u - (unstuff ? 1u : 0u);
  843. msp->unstuff = (((val >> 24) & 0xFF) == 0xFF); // for next byte
  844. msp->tmp |= ((OPJ_UINT64)t) << msp->bits; // move data to msp->tmp
  845. msp->bits += bits;
  846. }
  847. //************************************************************************/
  848. /** @brief Initialize frwd_struct_t struct and reads some bytes
  849. *
  850. * @param [in] msp is a pointer to frwd_struct_t
  851. * @param [in] data is a pointer to the start of data
  852. * @param [in] size is the number of byte in the bitstream
  853. * @param [in] X is the value fed in when the bitstream is exhausted.
  854. * See frwd_read.
  855. */
  856. static INLINE
  857. void frwd_init(frwd_struct_t *msp, const OPJ_UINT8* data, int size,
  858. OPJ_UINT32 X)
  859. {
  860. int num, i;
  861. msp->data = data;
  862. msp->tmp = 0;
  863. msp->bits = 0;
  864. msp->unstuff = OPJ_FALSE;
  865. msp->size = size;
  866. msp->X = X;
  867. assert(msp->X == 0 || msp->X == 0xFF);
  868. //This code is designed for an architecture that read address should
  869. // align to the read size (address multiple of 4 if read size is 4)
  870. //These few lines take care of the case where data is not at a multiple
  871. // of 4 boundary. It reads 1,2,3 up to 4 bytes from the bitstream
  872. num = 4 - (int)((intptr_t)(msp->data) & 0x3);
  873. for (i = 0; i < num; ++i) {
  874. OPJ_UINT64 d;
  875. //read a byte if the buffer is not exhausted, otherwise set it to X
  876. d = msp->size-- > 0 ? *msp->data++ : msp->X;
  877. msp->tmp |= (d << msp->bits); // store data in msp->tmp
  878. msp->bits += 8u - (msp->unstuff ? 1u : 0u); // number of bits added to msp->tmp
  879. msp->unstuff = ((d & 0xFF) == 0xFF); // unstuffing for next byte
  880. }
  881. frwd_read(msp); // read 32 bits more
  882. }
  883. //************************************************************************/
  884. /** @brief Consume num_bits bits from the bitstream of frwd_struct_t
  885. *
  886. * @param [in] msp is a pointer to frwd_struct_t
  887. * @param [in] num_bits is the number of bit to consume
  888. */
  889. static INLINE
  890. void frwd_advance(frwd_struct_t *msp, OPJ_UINT32 num_bits)
  891. {
  892. assert(num_bits <= msp->bits);
  893. msp->tmp >>= num_bits; // consume num_bits
  894. msp->bits -= num_bits;
  895. }
  896. //************************************************************************/
  897. /** @brief Fetches 32 bits from the frwd_struct_t bitstream
  898. *
  899. * @param [in] msp is a pointer to frwd_struct_t
  900. */
  901. static INLINE
  902. OPJ_UINT32 frwd_fetch(frwd_struct_t *msp)
  903. {
  904. if (msp->bits < 32) {
  905. frwd_read(msp);
  906. if (msp->bits < 32) { //need to test
  907. frwd_read(msp);
  908. }
  909. }
  910. return (OPJ_UINT32)msp->tmp;
  911. }
  912. //************************************************************************/
  913. /** @brief Allocates T1 buffers
  914. *
  915. * @param [in, out] t1 is codeblock cofficients storage
  916. * @param [in] w is codeblock width
  917. * @param [in] h is codeblock height
  918. */
  919. static OPJ_BOOL opj_t1_allocate_buffers(
  920. opj_t1_t *t1,
  921. OPJ_UINT32 w,
  922. OPJ_UINT32 h)
  923. {
  924. OPJ_UINT32 flagssize;
  925. /* No risk of overflow. Prior checks ensure those assert are met */
  926. /* They are per the specification */
  927. assert(w <= 1024);
  928. assert(h <= 1024);
  929. assert(w * h <= 4096);
  930. /* encoder uses tile buffer, so no need to allocate */
  931. {
  932. OPJ_UINT32 datasize = w * h;
  933. if (datasize > t1->datasize) {
  934. opj_aligned_free(t1->data);
  935. t1->data = (OPJ_INT32*)
  936. opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
  937. if (!t1->data) {
  938. /* FIXME event manager error callback */
  939. return OPJ_FALSE;
  940. }
  941. t1->datasize = datasize;
  942. }
  943. /* memset first arg is declared to never be null by gcc */
  944. if (t1->data != NULL) {
  945. memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
  946. }
  947. }
  948. // We expand these buffers to multiples of 16 bytes.
  949. // We need 4 buffers of 129 integers each, expanded to 132 integers each
  950. // We also need 514 bytes of buffer, expanded to 528 bytes
  951. flagssize = 132U * sizeof(OPJ_UINT32) * 4U; // expanded to multiple of 16
  952. flagssize += 528U; // 514 expanded to multiples of 16
  953. {
  954. if (flagssize > t1->flagssize) {
  955. opj_aligned_free(t1->flags);
  956. t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize);
  957. if (!t1->flags) {
  958. /* FIXME event manager error callback */
  959. return OPJ_FALSE;
  960. }
  961. }
  962. t1->flagssize = flagssize;
  963. memset(t1->flags, 0, flagssize);
  964. }
  965. t1->w = w;
  966. t1->h = h;
  967. return OPJ_TRUE;
  968. }
  969. //************************************************************************/
  970. /** @brief Decodes one codeblock, processing the cleanup, siginificance
  971. * propagation, and magnitude refinement pass
  972. *
  973. * @param [in, out] t1 is codeblock cofficients storage
  974. * @param [in] cblk is codeblock properties
  975. * @param [in] orient is the subband to which the codeblock belongs (not needed)
  976. * @param [in] roishift is region of interest shift
  977. * @param [in] cblksty is codeblock style
  978. * @param [in] p_manager is events print manager
  979. * @param [in] p_manager_mutex a mutex to control access to p_manager
  980. * @param [in] check_pterm: check termination (not used)
  981. */
  982. OPJ_BOOL opj_t1_ht_decode_cblk(opj_t1_t *t1,
  983. opj_tcd_cblk_dec_t* cblk,
  984. OPJ_UINT32 orient,
  985. OPJ_UINT32 roishift,
  986. OPJ_UINT32 cblksty,
  987. opj_event_mgr_t *p_manager,
  988. opj_mutex_t* p_manager_mutex,
  989. OPJ_BOOL check_pterm)
  990. {
  991. OPJ_BYTE* cblkdata = NULL;
  992. OPJ_UINT8* coded_data;
  993. OPJ_UINT32* decoded_data;
  994. OPJ_UINT32 zero_bplanes;
  995. OPJ_UINT32 num_passes;
  996. OPJ_UINT32 lengths1;
  997. OPJ_UINT32 lengths2;
  998. OPJ_INT32 width;
  999. OPJ_INT32 height;
  1000. OPJ_INT32 stride;
  1001. OPJ_UINT32 *pflags, *sigma1, *sigma2, *mbr1, *mbr2, *sip, sip_shift;
  1002. OPJ_UINT32 p;
  1003. OPJ_UINT32 zero_bplanes_p1;
  1004. int lcup, scup;
  1005. dec_mel_t mel;
  1006. rev_struct_t vlc;
  1007. frwd_struct_t magsgn;
  1008. frwd_struct_t sigprop;
  1009. rev_struct_t magref;
  1010. OPJ_UINT8 *lsp, *line_state;
  1011. int run;
  1012. OPJ_UINT32 vlc_val; // fetched data from VLC bitstream
  1013. OPJ_UINT32 qinf[2];
  1014. OPJ_UINT32 c_q;
  1015. OPJ_UINT32* sp;
  1016. OPJ_INT32 x, y; // loop indices
  1017. OPJ_BOOL stripe_causal = (cblksty & J2K_CCP_CBLKSTY_VSC) != 0;
  1018. OPJ_UINT32 cblk_len = 0;
  1019. (void)(orient); // stops unused parameter message
  1020. (void)(check_pterm); // stops unused parameter message
  1021. // We ignor orient, because the same decoder is used for all subbands
  1022. // We also ignore check_pterm, because I am not sure how it applies
  1023. if (roishift != 0) {
  1024. if (p_manager_mutex) {
  1025. opj_mutex_lock(p_manager_mutex);
  1026. }
  1027. opj_event_msg(p_manager, EVT_ERROR, "We do not support ROI in decoding "
  1028. "HT codeblocks\n");
  1029. if (p_manager_mutex) {
  1030. opj_mutex_unlock(p_manager_mutex);
  1031. }
  1032. return OPJ_FALSE;
  1033. }
  1034. if (!opj_t1_allocate_buffers(
  1035. t1,
  1036. (OPJ_UINT32)(cblk->x1 - cblk->x0),
  1037. (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
  1038. return OPJ_FALSE;
  1039. }
  1040. if (cblk->Mb == 0) {
  1041. return OPJ_TRUE;
  1042. }
  1043. /* numbps = Mb + 1 - zero_bplanes, Mb = Kmax, zero_bplanes = missing_msbs */
  1044. zero_bplanes = (cblk->Mb + 1) - cblk->numbps;
  1045. /* Compute whole codeblock length from chunk lengths */
  1046. cblk_len = 0;
  1047. {
  1048. OPJ_UINT32 i;
  1049. for (i = 0; i < cblk->numchunks; i++) {
  1050. cblk_len += cblk->chunks[i].len;
  1051. }
  1052. }
  1053. if (cblk->numchunks > 1 || t1->mustuse_cblkdatabuffer) {
  1054. OPJ_UINT32 i;
  1055. /* Allocate temporary memory if needed */
  1056. if (cblk_len > t1->cblkdatabuffersize) {
  1057. cblkdata = (OPJ_BYTE*)opj_realloc(
  1058. t1->cblkdatabuffer, cblk_len);
  1059. if (cblkdata == NULL) {
  1060. return OPJ_FALSE;
  1061. }
  1062. t1->cblkdatabuffer = cblkdata;
  1063. t1->cblkdatabuffersize = cblk_len;
  1064. }
  1065. /* Concatenate all chunks */
  1066. cblkdata = t1->cblkdatabuffer;
  1067. cblk_len = 0;
  1068. for (i = 0; i < cblk->numchunks; i++) {
  1069. memcpy(cblkdata + cblk_len, cblk->chunks[i].data, cblk->chunks[i].len);
  1070. cblk_len += cblk->chunks[i].len;
  1071. }
  1072. } else if (cblk->numchunks == 1) {
  1073. cblkdata = cblk->chunks[0].data;
  1074. } else {
  1075. /* Not sure if that can happen in practice, but avoid Coverity to */
  1076. /* think we will dereference a null cblkdta pointer */
  1077. return OPJ_TRUE;
  1078. }
  1079. // OPJ_BYTE* coded_data is a pointer to bitstream
  1080. coded_data = cblkdata;
  1081. // OPJ_UINT32* decoded_data is a pointer to decoded codeblock data buf.
  1082. decoded_data = (OPJ_UINT32*)t1->data;
  1083. // OPJ_UINT32 num_passes is the number of passes: 1 if CUP only, 2 for
  1084. // CUP+SPP, and 3 for CUP+SPP+MRP
  1085. num_passes = cblk->numsegs > 0 ? cblk->segs[0].real_num_passes : 0;
  1086. num_passes += cblk->numsegs > 1 ? cblk->segs[1].real_num_passes : 0;
  1087. // OPJ_UINT32 lengths1 is the length of cleanup pass
  1088. lengths1 = num_passes > 0 ? cblk->segs[0].len : 0;
  1089. // OPJ_UINT32 lengths2 is the length of refinement passes (either SPP only or SPP+MRP)
  1090. lengths2 = num_passes > 1 ? cblk->segs[1].len : 0;
  1091. // OPJ_INT32 width is the decoded codeblock width
  1092. width = cblk->x1 - cblk->x0;
  1093. // OPJ_INT32 height is the decoded codeblock height
  1094. height = cblk->y1 - cblk->y0;
  1095. // OPJ_INT32 stride is the decoded codeblock buffer stride
  1096. stride = width;
  1097. /* sigma1 and sigma2 contains significant (i.e., non-zero) pixel
  1098. * locations. The buffers are used interchangeably, because we need
  1099. * more than 4 rows of significance information at a given time.
  1100. * Each 32 bits contain significance information for 4 rows of 8
  1101. * columns each. If we denote 32 bits by 0xaaaaaaaa, the each "a" is
  1102. * called a nibble and has significance information for 4 rows.
  1103. * The least significant nibble has information for the first column,
  1104. * and so on. The nibble's LSB is for the first row, and so on.
  1105. * Since, at most, we can have 1024 columns in a quad, we need 128
  1106. * entries; we added 1 for convenience when propagation of signifcance
  1107. * goes outside the structure
  1108. * To work in OpenJPEG these buffers has been expanded to 132.
  1109. */
  1110. // OPJ_UINT32 *pflags, *sigma1, *sigma2, *mbr1, *mbr2, *sip, sip_shift;
  1111. pflags = (OPJ_UINT32 *)t1->flags;
  1112. sigma1 = pflags;
  1113. sigma2 = sigma1 + 132;
  1114. // mbr arrangement is similar to sigma; mbr contains locations
  1115. // that become significant during significance propagation pass
  1116. mbr1 = sigma2 + 132;
  1117. mbr2 = mbr1 + 132;
  1118. //a pointer to sigma
  1119. sip = sigma1; //pointers to arrays to be used interchangeably
  1120. sip_shift = 0; //the amount of shift needed for sigma
  1121. if (num_passes > 1 && lengths2 == 0) {
  1122. if (p_manager_mutex) {
  1123. opj_mutex_lock(p_manager_mutex);
  1124. }
  1125. opj_event_msg(p_manager, EVT_WARNING, "A malformed codeblock that has "
  1126. "more than one coding pass, but zero length for "
  1127. "2nd and potentially the 3rd pass in an HT codeblock.\n");
  1128. if (p_manager_mutex) {
  1129. opj_mutex_unlock(p_manager_mutex);
  1130. }
  1131. num_passes = 1;
  1132. }
  1133. if (num_passes > 3) {
  1134. if (p_manager_mutex) {
  1135. opj_mutex_lock(p_manager_mutex);
  1136. }
  1137. opj_event_msg(p_manager, EVT_ERROR, "We do not support more than 3 "
  1138. "coding passes in an HT codeblock; This codeblocks has "
  1139. "%d passes.\n", num_passes);
  1140. if (p_manager_mutex) {
  1141. opj_mutex_unlock(p_manager_mutex);
  1142. }
  1143. return OPJ_FALSE;
  1144. }
  1145. if (cblk->Mb > 30) {
  1146. /* This check is better moved to opj_t2_read_packet_header() in t2.c
  1147. We do not have enough precision to decode any passes
  1148. The design of openjpeg assumes that the bits of a 32-bit integer are
  1149. assigned as follows:
  1150. bit 31 is for sign
  1151. bits 30-1 are for magnitude
  1152. bit 0 is for the center of the quantization bin
  1153. Therefore we can only do values of cblk->Mb <= 30
  1154. */
  1155. if (p_manager_mutex) {
  1156. opj_mutex_lock(p_manager_mutex);
  1157. }
  1158. opj_event_msg(p_manager, EVT_ERROR, "32 bits are not enough to "
  1159. "decode this codeblock, since the number of "
  1160. "bitplane, %d, is larger than 30.\n", cblk->Mb);
  1161. if (p_manager_mutex) {
  1162. opj_mutex_unlock(p_manager_mutex);
  1163. }
  1164. return OPJ_FALSE;
  1165. }
  1166. if (zero_bplanes > cblk->Mb) {
  1167. /* This check is better moved to opj_t2_read_packet_header() in t2.c,
  1168. in the line "l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i;"
  1169. where i is the zero bitplanes, and should be no larger than cblk->Mb
  1170. We cannot have more zero bitplanes than there are planes. */
  1171. if (p_manager_mutex) {
  1172. opj_mutex_lock(p_manager_mutex);
  1173. }
  1174. opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
  1175. "Decoding this codeblock is stopped. There are "
  1176. "%d zero bitplanes in %d bitplanes.\n",
  1177. zero_bplanes, cblk->Mb);
  1178. if (p_manager_mutex) {
  1179. opj_mutex_unlock(p_manager_mutex);
  1180. }
  1181. return OPJ_FALSE;
  1182. } else if (zero_bplanes == cblk->Mb && num_passes > 1) {
  1183. /* When the number of zero bitplanes is equal to the number of bitplanes,
  1184. only the cleanup pass makes sense*/
  1185. if (only_cleanup_pass_is_decoded == OPJ_FALSE) {
  1186. if (p_manager_mutex) {
  1187. opj_mutex_lock(p_manager_mutex);
  1188. }
  1189. /* We have a second check to prevent the possibility of an overrun condition,
  1190. in the very unlikely event of a second thread discovering that
  1191. only_cleanup_pass_is_decoded is false before the first thread changing
  1192. the condition. */
  1193. if (only_cleanup_pass_is_decoded == OPJ_FALSE) {
  1194. only_cleanup_pass_is_decoded = OPJ_TRUE;
  1195. opj_event_msg(p_manager, EVT_WARNING, "Malformed HT codeblock. "
  1196. "When the number of zero planes bitplanes is "
  1197. "equal to the number of bitplanes, only the cleanup "
  1198. "pass makes sense, but we have %d passes in this "
  1199. "codeblock. Therefore, only the cleanup pass will be "
  1200. "decoded. This message will not be displayed again.\n",
  1201. num_passes);
  1202. }
  1203. if (p_manager_mutex) {
  1204. opj_mutex_unlock(p_manager_mutex);
  1205. }
  1206. }
  1207. num_passes = 1;
  1208. }
  1209. /* OPJ_UINT32 */
  1210. p = cblk->numbps;
  1211. // OPJ_UINT32 zero planes plus 1
  1212. zero_bplanes_p1 = zero_bplanes + 1;
  1213. if (lengths1 < 2 || (OPJ_UINT32)lengths1 > cblk_len ||
  1214. (OPJ_UINT32)(lengths1 + lengths2) > cblk_len) {
  1215. if (p_manager_mutex) {
  1216. opj_mutex_lock(p_manager_mutex);
  1217. }
  1218. opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
  1219. "Invalid codeblock length values.\n");
  1220. if (p_manager_mutex) {
  1221. opj_mutex_unlock(p_manager_mutex);
  1222. }
  1223. return OPJ_FALSE;
  1224. }
  1225. // read scup and fix the bytes there
  1226. lcup = (int)lengths1; // length of CUP
  1227. //scup is the length of MEL + VLC
  1228. scup = (((int)coded_data[lcup - 1]) << 4) + (coded_data[lcup - 2] & 0xF);
  1229. if (scup < 2 || scup > lcup || scup > 4079) { //something is wrong
  1230. /* The standard stipulates 2 <= Scup <= min(Lcup, 4079) */
  1231. if (p_manager_mutex) {
  1232. opj_mutex_lock(p_manager_mutex);
  1233. }
  1234. opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
  1235. "One of the following condition is not met: "
  1236. "2 <= Scup <= min(Lcup, 4079)\n");
  1237. if (p_manager_mutex) {
  1238. opj_mutex_unlock(p_manager_mutex);
  1239. }
  1240. return OPJ_FALSE;
  1241. }
  1242. // init structures
  1243. mel_init(&mel, coded_data, lcup, scup);
  1244. rev_init(&vlc, coded_data, lcup, scup);
  1245. frwd_init(&magsgn, coded_data, lcup - scup, 0xFF);
  1246. if (num_passes > 1) { // needs to be tested
  1247. frwd_init(&sigprop, coded_data + lengths1, (int)lengths2, 0);
  1248. }
  1249. if (num_passes > 2) {
  1250. rev_init_mrp(&magref, coded_data, (int)lengths1, (int)lengths2);
  1251. }
  1252. /** State storage
  1253. * One byte per quad; for 1024 columns, or 512 quads, we need
  1254. * 512 bytes. We are using 2 extra bytes one on the left and one on
  1255. * the right for convenience.
  1256. *
  1257. * The MSB bit in each byte is (\sigma^nw | \sigma^n), and the 7 LSBs
  1258. * contain max(E^nw | E^n)
  1259. */
  1260. // 514 is enough for a block width of 1024, +2 extra
  1261. // here expanded to 528
  1262. line_state = (OPJ_UINT8 *)(mbr2 + 132);
  1263. //initial 2 lines
  1264. /////////////////
  1265. lsp = line_state; // point to line state
  1266. lsp[0] = 0; // for initial row of quad, we set to 0
  1267. run = mel_get_run(&mel); // decode runs of events from MEL bitstrm
  1268. // data represented as runs of 0 events
  1269. // See mel_decode description
  1270. qinf[0] = qinf[1] = 0; // quad info decoded from VLC bitstream
  1271. c_q = 0; // context for quad q
  1272. sp = decoded_data; // decoded codeblock samples
  1273. // vlc_val; // fetched data from VLC bitstream
  1274. for (x = 0; x < width; x += 4) { // one iteration per quad pair
  1275. OPJ_UINT32 U_q[2]; // u values for the quad pair
  1276. OPJ_UINT32 uvlc_mode;
  1277. OPJ_UINT32 consumed_bits;
  1278. OPJ_UINT32 m_n, v_n;
  1279. OPJ_UINT32 ms_val;
  1280. OPJ_UINT32 locs;
  1281. // decode VLC
  1282. /////////////
  1283. //first quad
  1284. // Get the head of the VLC bitstream. One fetch is enough for two
  1285. // quads, since the largest VLC code is 7 bits, and maximum number of
  1286. // bits used for u is 8. Therefore for two quads we need 30 bits
  1287. // (if we include unstuffing, then 32 bits are enough, since we have
  1288. // a maximum of one stuffing per two bytes)
  1289. vlc_val = rev_fetch(&vlc);
  1290. //decode VLC using the context c_q and the head of the VLC bitstream
  1291. qinf[0] = vlc_tbl0[(c_q << 7) | (vlc_val & 0x7F) ];
  1292. if (c_q == 0) { // if zero context, we need to use one MEL event
  1293. run -= 2; //the number of 0 events is multiplied by 2, so subtract 2
  1294. // Is the run terminated in 1? if so, use decoded VLC code,
  1295. // otherwise, discard decoded data, since we will decoded again
  1296. // using a different context
  1297. qinf[0] = (run == -1) ? qinf[0] : 0;
  1298. // is run -1 or -2? this means a run has been consumed
  1299. if (run < 0) {
  1300. run = mel_get_run(&mel); // get another run
  1301. }
  1302. }
  1303. // prepare context for the next quad; eqn. 1 in ITU T.814
  1304. c_q = ((qinf[0] & 0x10) >> 4) | ((qinf[0] & 0xE0) >> 5);
  1305. //remove data from vlc stream (0 bits are removed if qinf is not used)
  1306. vlc_val = rev_advance(&vlc, qinf[0] & 0x7);
  1307. //update sigma
  1308. // The update depends on the value of x; consider one OPJ_UINT32
  1309. // if x is 0, 8, 16 and so on, then this line update c locations
  1310. // nibble (4 bits) number 0 1 2 3 4 5 6 7
  1311. // LSB c c 0 0 0 0 0 0
  1312. // c c 0 0 0 0 0 0
  1313. // 0 0 0 0 0 0 0 0
  1314. // 0 0 0 0 0 0 0 0
  1315. // if x is 4, 12, 20, then this line update locations c
  1316. // nibble (4 bits) number 0 1 2 3 4 5 6 7
  1317. // LSB 0 0 0 0 c c 0 0
  1318. // 0 0 0 0 c c 0 0
  1319. // 0 0 0 0 0 0 0 0
  1320. // 0 0 0 0 0 0 0 0
  1321. *sip |= (((qinf[0] & 0x30) >> 4) | ((qinf[0] & 0xC0) >> 2)) << sip_shift;
  1322. //second quad
  1323. qinf[1] = 0;
  1324. if (x + 2 < width) { // do not run if codeblock is narrower
  1325. //decode VLC using the context c_q and the head of the VLC bitstream
  1326. qinf[1] = vlc_tbl0[(c_q << 7) | (vlc_val & 0x7F)];
  1327. // if context is zero, use one MEL event
  1328. if (c_q == 0) { //zero context
  1329. run -= 2; //subtract 2, since events number if multiplied by 2
  1330. // if event is 0, discard decoded qinf
  1331. qinf[1] = (run == -1) ? qinf[1] : 0;
  1332. if (run < 0) { // have we consumed all events in a run
  1333. run = mel_get_run(&mel); // if yes, then get another run
  1334. }
  1335. }
  1336. //prepare context for the next quad, eqn. 1 in ITU T.814
  1337. c_q = ((qinf[1] & 0x10) >> 4) | ((qinf[1] & 0xE0) >> 5);
  1338. //remove data from vlc stream, if qinf is not used, cwdlen is 0
  1339. vlc_val = rev_advance(&vlc, qinf[1] & 0x7);
  1340. }
  1341. //update sigma
  1342. // The update depends on the value of x; consider one OPJ_UINT32
  1343. // if x is 0, 8, 16 and so on, then this line update c locations
  1344. // nibble (4 bits) number 0 1 2 3 4 5 6 7
  1345. // LSB 0 0 c c 0 0 0 0
  1346. // 0 0 c c 0 0 0 0
  1347. // 0 0 0 0 0 0 0 0
  1348. // 0 0 0 0 0 0 0 0
  1349. // if x is 4, 12, 20, then this line update locations c
  1350. // nibble (4 bits) number 0 1 2 3 4 5 6 7
  1351. // LSB 0 0 0 0 0 0 c c
  1352. // 0 0 0 0 0 0 c c
  1353. // 0 0 0 0 0 0 0 0
  1354. // 0 0 0 0 0 0 0 0
  1355. *sip |= (((qinf[1] & 0x30) | ((qinf[1] & 0xC0) << 2))) << (4 + sip_shift);
  1356. sip += x & 0x7 ? 1 : 0; // move sigma pointer to next entry
  1357. sip_shift ^= 0x10; // increment/decrement sip_shift by 16
  1358. // retrieve u
  1359. /////////////
  1360. // uvlc_mode is made up of u_offset bits from the quad pair
  1361. uvlc_mode = ((qinf[0] & 0x8) >> 3) | ((qinf[1] & 0x8) >> 2);
  1362. if (uvlc_mode == 3) { // if both u_offset are set, get an event from
  1363. // the MEL run of events
  1364. run -= 2; //subtract 2, since events number if multiplied by 2
  1365. uvlc_mode += (run == -1) ? 1 : 0; //increment uvlc_mode if event is 1
  1366. if (run < 0) { // if run is consumed (run is -1 or -2), get another run
  1367. run = mel_get_run(&mel);
  1368. }
  1369. }
  1370. //decode uvlc_mode to get u for both quads
  1371. consumed_bits = decode_init_uvlc(vlc_val, uvlc_mode, U_q);
  1372. if (U_q[0] > zero_bplanes_p1 || U_q[1] > zero_bplanes_p1) {
  1373. if (p_manager_mutex) {
  1374. opj_mutex_lock(p_manager_mutex);
  1375. }
  1376. opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. Decoding "
  1377. "this codeblock is stopped. U_q is larger than zero "
  1378. "bitplanes + 1 \n");
  1379. if (p_manager_mutex) {
  1380. opj_mutex_unlock(p_manager_mutex);
  1381. }
  1382. return OPJ_FALSE;
  1383. }
  1384. //consume u bits in the VLC code
  1385. vlc_val = rev_advance(&vlc, consumed_bits);
  1386. //decode magsgn and update line_state
  1387. /////////////////////////////////////
  1388. //We obtain a mask for the samples locations that needs evaluation
  1389. locs = 0xFF;
  1390. if (x + 4 > width) {
  1391. locs >>= (x + 4 - width) << 1; // limits width
  1392. }
  1393. locs = height > 1 ? locs : (locs & 0x55); // limits height
  1394. if ((((qinf[0] & 0xF0) >> 4) | (qinf[1] & 0xF0)) & ~locs) {
  1395. if (p_manager_mutex) {
  1396. opj_mutex_lock(p_manager_mutex);
  1397. }
  1398. opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
  1399. "VLC code produces significant samples outside "
  1400. "the codeblock area.\n");
  1401. if (p_manager_mutex) {
  1402. opj_mutex_unlock(p_manager_mutex);
  1403. }
  1404. return OPJ_FALSE;
  1405. }
  1406. //first quad, starting at first sample in quad and moving on
  1407. if (qinf[0] & 0x10) { //is it significant? (sigma_n)
  1408. OPJ_UINT32 val;
  1409. ms_val = frwd_fetch(&magsgn); //get 32 bits of magsgn data
  1410. m_n = U_q[0] - ((qinf[0] >> 12) & 1); //evaluate m_n (number of bits
  1411. // to read from bitstream), using EMB e_k
  1412. frwd_advance(&magsgn, m_n); //consume m_n
  1413. val = ms_val << 31; //get sign bit
  1414. v_n = ms_val & ((1U << m_n) - 1); //keep only m_n bits
  1415. v_n |= ((qinf[0] & 0x100) >> 8) << m_n; //add EMB e_1 as MSB
  1416. v_n |= 1; //add center of bin
  1417. //v_n now has 2 * (\mu - 1) + 0.5 with correct sign bit
  1418. //add 2 to make it 2*\mu+0.5, shift it up to missing MSBs
  1419. sp[0] = val | ((v_n + 2) << (p - 1));
  1420. } else if (locs & 0x1) { // if this is inside the codeblock, set the
  1421. sp[0] = 0; // sample to zero
  1422. }
  1423. if (qinf[0] & 0x20) { //sigma_n
  1424. OPJ_UINT32 val, t;
  1425. ms_val = frwd_fetch(&magsgn); //get 32 bits
  1426. m_n = U_q[0] - ((qinf[0] >> 13) & 1); //m_n, uses EMB e_k
  1427. frwd_advance(&magsgn, m_n); //consume m_n
  1428. val = ms_val << 31; //get sign bit
  1429. v_n = ms_val & ((1U << m_n) - 1); //keep only m_n bits
  1430. v_n |= ((qinf[0] & 0x200) >> 9) << m_n; //add EMB e_1
  1431. v_n |= 1; //bin center
  1432. //v_n now has 2 * (\mu - 1) + 0.5 with correct sign bit
  1433. //add 2 to make it 2*\mu+0.5, shift it up to missing MSBs
  1434. sp[stride] = val | ((v_n + 2) << (p - 1));
  1435. //update line_state: bit 7 (\sigma^N), and E^N
  1436. t = lsp[0] & 0x7F; // keep E^NW
  1437. v_n = 32 - count_leading_zeros(v_n);
  1438. lsp[0] = (OPJ_UINT8)(0x80 | (t > v_n ? t : v_n)); //max(E^NW, E^N) | s
  1439. } else if (locs & 0x2) { // if this is inside the codeblock, set the
  1440. sp[stride] = 0; // sample to zero
  1441. }
  1442. ++lsp; // move to next quad information
  1443. ++sp; // move to next column of samples
  1444. //this is similar to the above two samples
  1445. if (qinf[0] & 0x40) {
  1446. OPJ_UINT32 val;
  1447. ms_val = frwd_fetch(&magsgn);
  1448. m_n = U_q[0] - ((qinf[0] >> 14) & 1);
  1449. frwd_advance(&magsgn, m_n);
  1450. val = ms_val << 31;
  1451. v_n = ms_val & ((1U << m_n) - 1);
  1452. v_n |= (((qinf[0] & 0x400) >> 10) << m_n);
  1453. v_n |= 1;
  1454. sp[0] = val | ((v_n + 2) << (p - 1));
  1455. } else if (locs & 0x4) {
  1456. sp[0] = 0;
  1457. }
  1458. lsp[0] = 0;
  1459. if (qinf[0] & 0x80) {
  1460. OPJ_UINT32 val;
  1461. ms_val = frwd_fetch(&magsgn);
  1462. m_n = U_q[0] - ((qinf[0] >> 15) & 1); //m_n
  1463. frwd_advance(&magsgn, m_n);
  1464. val = ms_val << 31;
  1465. v_n = ms_val & ((1U << m_n) - 1);
  1466. v_n |= ((qinf[0] & 0x800) >> 11) << m_n;
  1467. v_n |= 1; //center of bin
  1468. sp[stride] = val | ((v_n + 2) << (p - 1));
  1469. //line_state: bit 7 (\sigma^NW), and E^NW for next quad
  1470. lsp[0] = (OPJ_UINT8)(0x80 | (32 - count_leading_zeros(v_n)));
  1471. } else if (locs & 0x8) { //if outside set to 0
  1472. sp[stride] = 0;
  1473. }
  1474. ++sp; //move to next column
  1475. //second quad
  1476. if (qinf[1] & 0x10) {
  1477. OPJ_UINT32 val;
  1478. ms_val = frwd_fetch(&magsgn);
  1479. m_n = U_q[1] - ((qinf[1] >> 12) & 1); //m_n
  1480. frwd_advance(&magsgn, m_n);
  1481. val = ms_val << 31;
  1482. v_n = ms_val & ((1U << m_n) - 1);
  1483. v_n |= (((qinf[1] & 0x100) >> 8) << m_n);
  1484. v_n |= 1;
  1485. sp[0] = val | ((v_n + 2) << (p - 1));
  1486. } else if (locs & 0x10) {
  1487. sp[0] = 0;
  1488. }
  1489. if (qinf[1] & 0x20) {
  1490. OPJ_UINT32 val, t;
  1491. ms_val = frwd_fetch(&magsgn);
  1492. m_n = U_q[1] - ((qinf[1] >> 13) & 1); //m_n
  1493. frwd_advance(&magsgn, m_n);
  1494. val = ms_val << 31;
  1495. v_n = ms_val & ((1U << m_n) - 1);
  1496. v_n |= (((qinf[1] & 0x200) >> 9) << m_n);
  1497. v_n |= 1;
  1498. sp[stride] = val | ((v_n + 2) << (p - 1));
  1499. //update line_state: bit 7 (\sigma^N), and E^N
  1500. t = lsp[0] & 0x7F; //E^NW
  1501. v_n = 32 - count_leading_zeros(v_n); //E^N
  1502. lsp[0] = (OPJ_UINT8)(0x80 | (t > v_n ? t : v_n)); //max(E^NW, E^N) | s
  1503. } else if (locs & 0x20) {
  1504. sp[stride] = 0; //no need to update line_state
  1505. }
  1506. ++lsp; //move line state to next quad
  1507. ++sp; //move to next sample
  1508. if (qinf[1] & 0x40) {
  1509. OPJ_UINT32 val;
  1510. ms_val = frwd_fetch(&magsgn);
  1511. m_n = U_q[1] - ((qinf[1] >> 14) & 1); //m_n
  1512. frwd_advance(&magsgn, m_n);
  1513. val = ms_val << 31;
  1514. v_n = ms_val & ((1U << m_n) - 1);
  1515. v_n |= (((qinf[1] & 0x400) >> 10) << m_n);
  1516. v_n |= 1;
  1517. sp[0] = val | ((v_n + 2) << (p - 1));
  1518. } else if (locs & 0x40) {
  1519. sp[0] = 0;
  1520. }
  1521. lsp[0] = 0;
  1522. if (qinf[1] & 0x80) {
  1523. OPJ_UINT32 val;
  1524. ms_val = frwd_fetch(&magsgn);
  1525. m_n = U_q[1] - ((qinf[1] >> 15) & 1); //m_n
  1526. frwd_advance(&magsgn, m_n);
  1527. val = ms_val << 31;
  1528. v_n = ms_val & ((1U << m_n) - 1);
  1529. v_n |= (((qinf[1] & 0x800) >> 11) << m_n);
  1530. v_n |= 1; //center of bin
  1531. sp[stride] = val | ((v_n + 2) << (p - 1));
  1532. //line_state: bit 7 (\sigma^NW), and E^NW for next quad
  1533. lsp[0] = (OPJ_UINT8)(0x80 | (32 - count_leading_zeros(v_n)));
  1534. } else if (locs & 0x80) {
  1535. sp[stride] = 0;
  1536. }
  1537. ++sp;
  1538. }
  1539. //non-initial lines
  1540. //////////////////////////
  1541. for (y = 2; y < height; /*done at the end of loop*/) {
  1542. OPJ_UINT32 *sip;
  1543. OPJ_UINT8 ls0;
  1544. OPJ_INT32 x;
  1545. sip_shift ^= 0x2; // shift sigma to the upper half od the nibble
  1546. sip_shift &= 0xFFFFFFEFU; //move back to 0 (it might have been at 0x10)
  1547. sip = y & 0x4 ? sigma2 : sigma1; //choose sigma array
  1548. lsp = line_state;
  1549. ls0 = lsp[0]; // read the line state value
  1550. lsp[0] = 0; // and set it to zero
  1551. sp = decoded_data + y * stride; // generated samples
  1552. c_q = 0; // context
  1553. for (x = 0; x < width; x += 4) {
  1554. OPJ_UINT32 U_q[2];
  1555. OPJ_UINT32 uvlc_mode, consumed_bits;
  1556. OPJ_UINT32 m_n, v_n;
  1557. OPJ_UINT32 ms_val;
  1558. OPJ_UINT32 locs;
  1559. // decode vlc
  1560. /////////////
  1561. //first quad
  1562. // get context, eqn. 2 ITU T.814
  1563. // c_q has \sigma^W | \sigma^SW
  1564. c_q |= (ls0 >> 7); //\sigma^NW | \sigma^N
  1565. c_q |= (lsp[1] >> 5) & 0x4; //\sigma^NE | \sigma^NF
  1566. //the following is very similar to previous code, so please refer to
  1567. // that
  1568. vlc_val = rev_fetch(&vlc);
  1569. qinf[0] = vlc_tbl1[(c_q << 7) | (vlc_val & 0x7F)];
  1570. if (c_q == 0) { //zero context
  1571. run -= 2;
  1572. qinf[0] = (run == -1) ? qinf[0] : 0;
  1573. if (run < 0) {
  1574. run = mel_get_run(&mel);
  1575. }
  1576. }
  1577. //prepare context for the next quad, \sigma^W | \sigma^SW
  1578. c_q = ((qinf[0] & 0x40) >> 5) | ((qinf[0] & 0x80) >> 6);
  1579. //remove data from vlc stream
  1580. vlc_val = rev_advance(&vlc, qinf[0] & 0x7);
  1581. //update sigma
  1582. // The update depends on the value of x and y; consider one OPJ_UINT32
  1583. // if x is 0, 8, 16 and so on, and y is 2, 6, etc., then this
  1584. // line update c locations
  1585. // nibble (4 bits) number 0 1 2 3 4 5 6 7
  1586. // LSB 0 0 0 0 0 0 0 0
  1587. // 0 0 0 0 0 0 0 0
  1588. // c c 0 0 0 0 0 0
  1589. // c c 0 0 0 0 0 0
  1590. *sip |= (((qinf[0] & 0x30) >> 4) | ((qinf[0] & 0xC0) >> 2)) << sip_shift;
  1591. //second quad
  1592. qinf[1] = 0;
  1593. if (x + 2 < width) {
  1594. c_q |= (lsp[1] >> 7);
  1595. c_q |= (lsp[2] >> 5) & 0x4;
  1596. qinf[1] = vlc_tbl1[(c_q << 7) | (vlc_val & 0x7F)];
  1597. if (c_q == 0) { //zero context
  1598. run -= 2;
  1599. qinf[1] = (run == -1) ? qinf[1] : 0;
  1600. if (run < 0) {
  1601. run = mel_get_run(&mel);
  1602. }
  1603. }
  1604. //prepare context for the next quad
  1605. c_q = ((qinf[1] & 0x40) >> 5) | ((qinf[1] & 0x80) >> 6);
  1606. //remove data from vlc stream
  1607. vlc_val = rev_advance(&vlc, qinf[1] & 0x7);
  1608. }
  1609. //update sigma
  1610. *sip |= (((qinf[1] & 0x30) | ((qinf[1] & 0xC0) << 2))) << (4 + sip_shift);
  1611. sip += x & 0x7 ? 1 : 0;
  1612. sip_shift ^= 0x10;
  1613. //retrieve u
  1614. ////////////
  1615. uvlc_mode = ((qinf[0] & 0x8) >> 3) | ((qinf[1] & 0x8) >> 2);
  1616. consumed_bits = decode_noninit_uvlc(vlc_val, uvlc_mode, U_q);
  1617. vlc_val = rev_advance(&vlc, consumed_bits);
  1618. //calculate E^max and add it to U_q, eqns 5 and 6 in ITU T.814
  1619. if ((qinf[0] & 0xF0) & ((qinf[0] & 0xF0) - 1)) { // is \gamma_q 1?
  1620. OPJ_UINT32 E = (ls0 & 0x7Fu);
  1621. E = E > (lsp[1] & 0x7Fu) ? E : (lsp[1] & 0x7Fu); //max(E, E^NE, E^NF)
  1622. //since U_q already has u_q + 1, we subtract 2 instead of 1
  1623. U_q[0] += E > 2 ? E - 2 : 0;
  1624. }
  1625. if ((qinf[1] & 0xF0) & ((qinf[1] & 0xF0) - 1)) { //is \gamma_q 1?
  1626. OPJ_UINT32 E = (lsp[1] & 0x7Fu);
  1627. E = E > (lsp[2] & 0x7Fu) ? E : (lsp[2] & 0x7Fu); //max(E, E^NE, E^NF)
  1628. //since U_q already has u_q + 1, we subtract 2 instead of 1
  1629. U_q[1] += E > 2 ? E - 2 : 0;
  1630. }
  1631. if (U_q[0] > zero_bplanes_p1 || U_q[1] > zero_bplanes_p1) {
  1632. if (p_manager_mutex) {
  1633. opj_mutex_lock(p_manager_mutex);
  1634. }
  1635. opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
  1636. "Decoding this codeblock is stopped. U_q is"
  1637. "larger than bitplanes + 1 \n");
  1638. if (p_manager_mutex) {
  1639. opj_mutex_unlock(p_manager_mutex);
  1640. }
  1641. return OPJ_FALSE;
  1642. }
  1643. ls0 = lsp[2]; //for next double quad
  1644. lsp[1] = lsp[2] = 0;
  1645. //decode magsgn and update line_state
  1646. /////////////////////////////////////
  1647. //locations where samples need update
  1648. locs = 0xFF;
  1649. if (x + 4 > width) {
  1650. locs >>= (x + 4 - width) << 1;
  1651. }
  1652. locs = y + 2 <= height ? locs : (locs & 0x55);
  1653. if ((((qinf[0] & 0xF0) >> 4) | (qinf[1] & 0xF0)) & ~locs) {
  1654. if (p_manager_mutex) {
  1655. opj_mutex_lock(p_manager_mutex);
  1656. }
  1657. opj_event_msg(p_manager, EVT_ERROR, "Malformed HT codeblock. "
  1658. "VLC code produces significant samples outside "
  1659. "the codeblock area.\n");
  1660. if (p_manager_mutex) {
  1661. opj_mutex_unlock(p_manager_mutex);
  1662. }
  1663. return OPJ_FALSE;
  1664. }
  1665. if (qinf[0] & 0x10) { //sigma_n
  1666. OPJ_UINT32 val;
  1667. ms_val = frwd_fetch(&magsgn);
  1668. m_n = U_q[0] - ((qinf[0] >> 12) & 1); //m_n
  1669. frwd_advance(&magsgn, m_n);
  1670. val = ms_val << 31;
  1671. v_n = ms_val & ((1U << m_n) - 1);
  1672. v_n |= ((qinf[0] & 0x100) >> 8) << m_n;
  1673. v_n |= 1; //center of bin
  1674. sp[0] = val | ((v_n + 2) << (p - 1));
  1675. } else if (locs & 0x1) {
  1676. sp[0] = 0;
  1677. }
  1678. if (qinf[0] & 0x20) { //sigma_n
  1679. OPJ_UINT32 val, t;
  1680. ms_val = frwd_fetch(&magsgn);
  1681. m_n = U_q[0] - ((qinf[0] >> 13) & 1); //m_n
  1682. frwd_advance(&magsgn, m_n);
  1683. val = ms_val << 31;
  1684. v_n = ms_val & ((1U << m_n) - 1);
  1685. v_n |= ((qinf[0] & 0x200) >> 9) << m_n;
  1686. v_n |= 1; //center of bin
  1687. sp[stride] = val | ((v_n + 2) << (p - 1));
  1688. //update line_state: bit 7 (\sigma^N), and E^N
  1689. t = lsp[0] & 0x7F; //E^NW
  1690. v_n = 32 - count_leading_zeros(v_n);
  1691. lsp[0] = (OPJ_UINT8)(0x80 | (t > v_n ? t : v_n));
  1692. } else if (locs & 0x2) {
  1693. sp[stride] = 0; //no need to update line_state
  1694. }
  1695. ++lsp;
  1696. ++sp;
  1697. if (qinf[0] & 0x40) { //sigma_n
  1698. OPJ_UINT32 val;
  1699. ms_val = frwd_fetch(&magsgn);
  1700. m_n = U_q[0] - ((qinf[0] >> 14) & 1); //m_n
  1701. frwd_advance(&magsgn, m_n);
  1702. val = ms_val << 31;
  1703. v_n = ms_val & ((1U << m_n) - 1);
  1704. v_n |= (((qinf[0] & 0x400) >> 10) << m_n);
  1705. v_n |= 1; //center of bin
  1706. sp[0] = val | ((v_n + 2) << (p - 1));
  1707. } else if (locs & 0x4) {
  1708. sp[0] = 0;
  1709. }
  1710. if (qinf[0] & 0x80) { //sigma_n
  1711. OPJ_UINT32 val;
  1712. ms_val = frwd_fetch(&magsgn);
  1713. m_n = U_q[0] - ((qinf[0] >> 15) & 1); //m_n
  1714. frwd_advance(&magsgn, m_n);
  1715. val = ms_val << 31;
  1716. v_n = ms_val & ((1U << m_n) - 1);
  1717. v_n |= ((qinf[0] & 0x800) >> 11) << m_n;
  1718. v_n |= 1; //center of bin
  1719. sp[stride] = val | ((v_n + 2) << (p - 1));
  1720. //update line_state: bit 7 (\sigma^NW), and E^NW for next quad
  1721. lsp[0] = (OPJ_UINT8)(0x80 | (32 - count_leading_zeros(v_n)));
  1722. } else if (locs & 0x8) {
  1723. sp[stride] = 0;
  1724. }
  1725. ++sp;
  1726. if (qinf[1] & 0x10) { //sigma_n
  1727. OPJ_UINT32 val;
  1728. ms_val = frwd_fetch(&magsgn);
  1729. m_n = U_q[1] - ((qinf[1] >> 12) & 1); //m_n
  1730. frwd_advance(&magsgn, m_n);
  1731. val = ms_val << 31;
  1732. v_n = ms_val & ((1U << m_n) - 1);
  1733. v_n |= (((qinf[1] & 0x100) >> 8) << m_n);
  1734. v_n |= 1; //center of bin
  1735. sp[0] = val | ((v_n + 2) << (p - 1));
  1736. } else if (locs & 0x10) {
  1737. sp[0] = 0;
  1738. }
  1739. if (qinf[1] & 0x20) { //sigma_n
  1740. OPJ_UINT32 val, t;
  1741. ms_val = frwd_fetch(&magsgn);
  1742. m_n = U_q[1] - ((qinf[1] >> 13) & 1); //m_n
  1743. frwd_advance(&magsgn, m_n);
  1744. val = ms_val << 31;
  1745. v_n = ms_val & ((1U << m_n) - 1);
  1746. v_n |= (((qinf[1] & 0x200) >> 9) << m_n);
  1747. v_n |= 1; //center of bin
  1748. sp[stride] = val | ((v_n + 2) << (p - 1));
  1749. //update line_state: bit 7 (\sigma^N), and E^N
  1750. t = lsp[0] & 0x7F; //E^NW
  1751. v_n = 32 - count_leading_zeros(v_n);
  1752. lsp[0] = (OPJ_UINT8)(0x80 | (t > v_n ? t : v_n));
  1753. } else if (locs & 0x20) {
  1754. sp[stride] = 0; //no need to update line_state
  1755. }
  1756. ++lsp;
  1757. ++sp;
  1758. if (qinf[1] & 0x40) { //sigma_n
  1759. OPJ_UINT32 val;
  1760. ms_val = frwd_fetch(&magsgn);
  1761. m_n = U_q[1] - ((qinf[1] >> 14) & 1); //m_n
  1762. frwd_advance(&magsgn, m_n);
  1763. val = ms_val << 31;
  1764. v_n = ms_val & ((1U << m_n) - 1);
  1765. v_n |= (((qinf[1] & 0x400) >> 10) << m_n);
  1766. v_n |= 1; //center of bin
  1767. sp[0] = val | ((v_n + 2) << (p - 1));
  1768. } else if (locs & 0x40) {
  1769. sp[0] = 0;
  1770. }
  1771. if (qinf[1] & 0x80) { //sigma_n
  1772. OPJ_UINT32 val;
  1773. ms_val = frwd_fetch(&magsgn);
  1774. m_n = U_q[1] - ((qinf[1] >> 15) & 1); //m_n
  1775. frwd_advance(&magsgn, m_n);
  1776. val = ms_val << 31;
  1777. v_n = ms_val & ((1U << m_n) - 1);
  1778. v_n |= (((qinf[1] & 0x800) >> 11) << m_n);
  1779. v_n |= 1; //center of bin
  1780. sp[stride] = val | ((v_n + 2) << (p - 1));
  1781. //update line_state: bit 7 (\sigma^NW), and E^NW for next quad
  1782. lsp[0] = (OPJ_UINT8)(0x80 | (32 - count_leading_zeros(v_n)));
  1783. } else if (locs & 0x80) {
  1784. sp[stride] = 0;
  1785. }
  1786. ++sp;
  1787. }
  1788. y += 2;
  1789. if (num_passes > 1 && (y & 3) == 0) { //executed at multiples of 4
  1790. // This is for SPP and potentially MRP
  1791. if (num_passes > 2) { //do MRP
  1792. // select the current stripe
  1793. OPJ_UINT32 *cur_sig = y & 0x4 ? sigma1 : sigma2;
  1794. // the address of the data that needs updating
  1795. OPJ_UINT32 *dpp = decoded_data + (y - 4) * stride;
  1796. OPJ_UINT32 half = 1u << (p - 2); // half the center of the bin
  1797. OPJ_INT32 i;
  1798. for (i = 0; i < width; i += 8) {
  1799. //Process one entry from sigma array at a time
  1800. // Each nibble (4 bits) in the sigma array represents 4 rows,
  1801. // and the 32 bits contain 8 columns
  1802. OPJ_UINT32 cwd = rev_fetch_mrp(&magref); // get 32 bit data
  1803. OPJ_UINT32 sig = *cur_sig++; // 32 bit that will be processed now
  1804. OPJ_UINT32 col_mask = 0xFu; // a mask for a column in sig
  1805. OPJ_UINT32 *dp = dpp + i; // next column in decode samples
  1806. if (sig) { // if any of the 32 bits are set
  1807. int j;
  1808. for (j = 0; j < 8; ++j, dp++) { //one column at a time
  1809. if (sig & col_mask) { // lowest nibble
  1810. OPJ_UINT32 sample_mask = 0x11111111u & col_mask; //LSB
  1811. if (sig & sample_mask) { //if LSB is set
  1812. OPJ_UINT32 sym;
  1813. assert(dp[0] != 0); // decoded value cannot be zero
  1814. sym = cwd & 1; // get it value
  1815. // remove center of bin if sym is 0
  1816. dp[0] ^= (1 - sym) << (p - 1);
  1817. dp[0] |= half; // put half the center of bin
  1818. cwd >>= 1; //consume word
  1819. }
  1820. sample_mask += sample_mask; //next row
  1821. if (sig & sample_mask) {
  1822. OPJ_UINT32 sym;
  1823. assert(dp[stride] != 0);
  1824. sym = cwd & 1;
  1825. dp[stride] ^= (1 - sym) << (p - 1);
  1826. dp[stride] |= half;
  1827. cwd >>= 1;
  1828. }
  1829. sample_mask += sample_mask;
  1830. if (sig & sample_mask) {
  1831. OPJ_UINT32 sym;
  1832. assert(dp[2 * stride] != 0);
  1833. sym = cwd & 1;
  1834. dp[2 * stride] ^= (1 - sym) << (p - 1);
  1835. dp[2 * stride] |= half;
  1836. cwd >>= 1;
  1837. }
  1838. sample_mask += sample_mask;
  1839. if (sig & sample_mask) {
  1840. OPJ_UINT32 sym;
  1841. assert(dp[3 * stride] != 0);
  1842. sym = cwd & 1;
  1843. dp[3 * stride] ^= (1 - sym) << (p - 1);
  1844. dp[3 * stride] |= half;
  1845. cwd >>= 1;
  1846. }
  1847. sample_mask += sample_mask;
  1848. }
  1849. col_mask <<= 4; //next column
  1850. }
  1851. }
  1852. // consume data according to the number of bits set
  1853. rev_advance_mrp(&magref, population_count(sig));
  1854. }
  1855. }
  1856. if (y >= 4) { // update mbr array at the end of each stripe
  1857. //generate mbr corresponding to a stripe
  1858. OPJ_UINT32 *sig = y & 0x4 ? sigma1 : sigma2;
  1859. OPJ_UINT32 *mbr = y & 0x4 ? mbr1 : mbr2;
  1860. //data is processed in patches of 8 columns, each
  1861. // each 32 bits in sigma1 or mbr1 represent 4 rows
  1862. //integrate horizontally
  1863. OPJ_UINT32 prev = 0; // previous columns
  1864. OPJ_INT32 i;
  1865. for (i = 0; i < width; i += 8, mbr++, sig++) {
  1866. OPJ_UINT32 t, z;
  1867. mbr[0] = sig[0]; //start with significant samples
  1868. mbr[0] |= prev >> 28; //for first column, left neighbors
  1869. mbr[0] |= sig[0] << 4; //left neighbors
  1870. mbr[0] |= sig[0] >> 4; //right neighbors
  1871. mbr[0] |= sig[1] << 28; //for last column, right neighbors
  1872. prev = sig[0]; // for next group of columns
  1873. //integrate vertically
  1874. t = mbr[0], z = mbr[0];
  1875. z |= (t & 0x77777777) << 1; //above neighbors
  1876. z |= (t & 0xEEEEEEEE) >> 1; //below neighbors
  1877. mbr[0] = z & ~sig[0]; //remove already significance samples
  1878. }
  1879. }
  1880. if (y >= 8) { //wait until 8 rows has been processed
  1881. OPJ_UINT32 *cur_sig, *cur_mbr, *nxt_sig, *nxt_mbr;
  1882. OPJ_UINT32 prev;
  1883. OPJ_UINT32 val;
  1884. OPJ_INT32 i;
  1885. // add membership from the next stripe, obtained above
  1886. cur_sig = y & 0x4 ? sigma2 : sigma1;
  1887. cur_mbr = y & 0x4 ? mbr2 : mbr1;
  1888. nxt_sig = y & 0x4 ? sigma1 : sigma2; //future samples
  1889. prev = 0; // the columns before these group of 8 columns
  1890. for (i = 0; i < width; i += 8, cur_mbr++, cur_sig++, nxt_sig++) {
  1891. OPJ_UINT32 t = nxt_sig[0];
  1892. t |= prev >> 28; //for first column, left neighbors
  1893. t |= nxt_sig[0] << 4; //left neighbors
  1894. t |= nxt_sig[0] >> 4; //right neighbors
  1895. t |= nxt_sig[1] << 28; //for last column, right neighbors
  1896. prev = nxt_sig[0]; // for next group of columns
  1897. if (!stripe_causal) {
  1898. cur_mbr[0] |= (t & 0x11111111u) << 3; //propagate up to cur_mbr
  1899. }
  1900. cur_mbr[0] &= ~cur_sig[0]; //remove already significance samples
  1901. }
  1902. //find new locations and get signs
  1903. cur_sig = y & 0x4 ? sigma2 : sigma1;
  1904. cur_mbr = y & 0x4 ? mbr2 : mbr1;
  1905. nxt_sig = y & 0x4 ? sigma1 : sigma2; //future samples
  1906. nxt_mbr = y & 0x4 ? mbr1 : mbr2; //future samples
  1907. val = 3u << (p - 2); // sample values for newly discovered
  1908. // significant samples including the bin center
  1909. for (i = 0; i < width;
  1910. i += 8, cur_sig++, cur_mbr++, nxt_sig++, nxt_mbr++) {
  1911. OPJ_UINT32 ux, tx;
  1912. OPJ_UINT32 mbr = *cur_mbr;
  1913. OPJ_UINT32 new_sig = 0;
  1914. if (mbr) { //are there any samples that might be significant
  1915. OPJ_INT32 n;
  1916. for (n = 0; n < 8; n += 4) {
  1917. OPJ_UINT32 col_mask;
  1918. OPJ_UINT32 inv_sig;
  1919. OPJ_INT32 end;
  1920. OPJ_INT32 j;
  1921. OPJ_UINT32 cwd = frwd_fetch(&sigprop); //get 32 bits
  1922. OPJ_UINT32 cnt = 0;
  1923. OPJ_UINT32 *dp = decoded_data + (y - 8) * stride;
  1924. dp += i + n; //address for decoded samples
  1925. col_mask = 0xFu << (4 * n); //a mask to select a column
  1926. inv_sig = ~cur_sig[0]; // insignificant samples
  1927. //find the last sample we operate on
  1928. end = n + 4 + i < width ? n + 4 : width - i;
  1929. for (j = n; j < end; ++j, ++dp, col_mask <<= 4) {
  1930. OPJ_UINT32 sample_mask;
  1931. if ((col_mask & mbr) == 0) { //no samples need checking
  1932. continue;
  1933. }
  1934. //scan mbr to find a new significant sample
  1935. sample_mask = 0x11111111u & col_mask; // LSB
  1936. if (mbr & sample_mask) {
  1937. assert(dp[0] == 0); // the sample must have been 0
  1938. if (cwd & 1) { //if this sample has become significant
  1939. // must propagate it to nearby samples
  1940. OPJ_UINT32 t;
  1941. new_sig |= sample_mask; // new significant samples
  1942. t = 0x32u << (j * 4);// propagation to neighbors
  1943. mbr |= t & inv_sig; //remove already significant samples
  1944. }
  1945. cwd >>= 1;
  1946. ++cnt; //consume bit and increment number of
  1947. //consumed bits
  1948. }
  1949. sample_mask += sample_mask; // next row
  1950. if (mbr & sample_mask) {
  1951. assert(dp[stride] == 0);
  1952. if (cwd & 1) {
  1953. OPJ_UINT32 t;
  1954. new_sig |= sample_mask;
  1955. t = 0x74u << (j * 4);
  1956. mbr |= t & inv_sig;
  1957. }
  1958. cwd >>= 1;
  1959. ++cnt;
  1960. }
  1961. sample_mask += sample_mask;
  1962. if (mbr & sample_mask) {
  1963. assert(dp[2 * stride] == 0);
  1964. if (cwd & 1) {
  1965. OPJ_UINT32 t;
  1966. new_sig |= sample_mask;
  1967. t = 0xE8u << (j * 4);
  1968. mbr |= t & inv_sig;
  1969. }
  1970. cwd >>= 1;
  1971. ++cnt;
  1972. }
  1973. sample_mask += sample_mask;
  1974. if (mbr & sample_mask) {
  1975. assert(dp[3 * stride] == 0);
  1976. if (cwd & 1) {
  1977. OPJ_UINT32 t;
  1978. new_sig |= sample_mask;
  1979. t = 0xC0u << (j * 4);
  1980. mbr |= t & inv_sig;
  1981. }
  1982. cwd >>= 1;
  1983. ++cnt;
  1984. }
  1985. }
  1986. //obtain signs here
  1987. if (new_sig & (0xFFFFu << (4 * n))) { //if any
  1988. OPJ_UINT32 col_mask;
  1989. OPJ_INT32 j;
  1990. OPJ_UINT32 *dp = decoded_data + (y - 8) * stride;
  1991. dp += i + n; // decoded samples address
  1992. col_mask = 0xFu << (4 * n); //mask to select a column
  1993. for (j = n; j < end; ++j, ++dp, col_mask <<= 4) {
  1994. OPJ_UINT32 sample_mask;
  1995. if ((col_mask & new_sig) == 0) { //if non is significant
  1996. continue;
  1997. }
  1998. //scan 4 signs
  1999. sample_mask = 0x11111111u & col_mask;
  2000. if (new_sig & sample_mask) {
  2001. assert(dp[0] == 0);
  2002. dp[0] |= ((cwd & 1) << 31) | val; //put value and sign
  2003. cwd >>= 1;
  2004. ++cnt; //consume bit and increment number
  2005. //of consumed bits
  2006. }
  2007. sample_mask += sample_mask;
  2008. if (new_sig & sample_mask) {
  2009. assert(dp[stride] == 0);
  2010. dp[stride] |= ((cwd & 1) << 31) | val;
  2011. cwd >>= 1;
  2012. ++cnt;
  2013. }
  2014. sample_mask += sample_mask;
  2015. if (new_sig & sample_mask) {
  2016. assert(dp[2 * stride] == 0);
  2017. dp[2 * stride] |= ((cwd & 1) << 31) | val;
  2018. cwd >>= 1;
  2019. ++cnt;
  2020. }
  2021. sample_mask += sample_mask;
  2022. if (new_sig & sample_mask) {
  2023. assert(dp[3 * stride] == 0);
  2024. dp[3 * stride] |= ((cwd & 1) << 31) | val;
  2025. cwd >>= 1;
  2026. ++cnt;
  2027. }
  2028. }
  2029. }
  2030. frwd_advance(&sigprop, cnt); //consume the bits from bitstrm
  2031. cnt = 0;
  2032. //update the next 8 columns
  2033. if (n == 4) {
  2034. //horizontally
  2035. OPJ_UINT32 t = new_sig >> 28;
  2036. t |= ((t & 0xE) >> 1) | ((t & 7) << 1);
  2037. cur_mbr[1] |= t & ~cur_sig[1];
  2038. }
  2039. }
  2040. }
  2041. //update the next stripe (vertically propagation)
  2042. new_sig |= cur_sig[0];
  2043. ux = (new_sig & 0x88888888) >> 3;
  2044. tx = ux | (ux << 4) | (ux >> 4); //left and right neighbors
  2045. if (i > 0) {
  2046. nxt_mbr[-1] |= (ux << 28) & ~nxt_sig[-1];
  2047. }
  2048. nxt_mbr[0] |= tx & ~nxt_sig[0];
  2049. nxt_mbr[1] |= (ux >> 28) & ~nxt_sig[1];
  2050. }
  2051. //clear current sigma
  2052. //mbr need not be cleared because it is overwritten
  2053. cur_sig = y & 0x4 ? sigma2 : sigma1;
  2054. memset(cur_sig, 0, ((((OPJ_UINT32)width + 7u) >> 3) + 1u) << 2);
  2055. }
  2056. }
  2057. }
  2058. //terminating
  2059. if (num_passes > 1) {
  2060. OPJ_INT32 st, y;
  2061. if (num_passes > 2 && ((height & 3) == 1 || (height & 3) == 2)) {
  2062. //do magref
  2063. OPJ_UINT32 *cur_sig = height & 0x4 ? sigma2 : sigma1; //reversed
  2064. OPJ_UINT32 *dpp = decoded_data + (height & 0xFFFFFC) * stride;
  2065. OPJ_UINT32 half = 1u << (p - 2);
  2066. OPJ_INT32 i;
  2067. for (i = 0; i < width; i += 8) {
  2068. OPJ_UINT32 cwd = rev_fetch_mrp(&magref);
  2069. OPJ_UINT32 sig = *cur_sig++;
  2070. OPJ_UINT32 col_mask = 0xF;
  2071. OPJ_UINT32 *dp = dpp + i;
  2072. if (sig) {
  2073. int j;
  2074. for (j = 0; j < 8; ++j, dp++) {
  2075. if (sig & col_mask) {
  2076. OPJ_UINT32 sample_mask = 0x11111111 & col_mask;
  2077. if (sig & sample_mask) {
  2078. OPJ_UINT32 sym;
  2079. assert(dp[0] != 0);
  2080. sym = cwd & 1;
  2081. dp[0] ^= (1 - sym) << (p - 1);
  2082. dp[0] |= half;
  2083. cwd >>= 1;
  2084. }
  2085. sample_mask += sample_mask;
  2086. if (sig & sample_mask) {
  2087. OPJ_UINT32 sym;
  2088. assert(dp[stride] != 0);
  2089. sym = cwd & 1;
  2090. dp[stride] ^= (1 - sym) << (p - 1);
  2091. dp[stride] |= half;
  2092. cwd >>= 1;
  2093. }
  2094. sample_mask += sample_mask;
  2095. if (sig & sample_mask) {
  2096. OPJ_UINT32 sym;
  2097. assert(dp[2 * stride] != 0);
  2098. sym = cwd & 1;
  2099. dp[2 * stride] ^= (1 - sym) << (p - 1);
  2100. dp[2 * stride] |= half;
  2101. cwd >>= 1;
  2102. }
  2103. sample_mask += sample_mask;
  2104. if (sig & sample_mask) {
  2105. OPJ_UINT32 sym;
  2106. assert(dp[3 * stride] != 0);
  2107. sym = cwd & 1;
  2108. dp[3 * stride] ^= (1 - sym) << (p - 1);
  2109. dp[3 * stride] |= half;
  2110. cwd >>= 1;
  2111. }
  2112. sample_mask += sample_mask;
  2113. }
  2114. col_mask <<= 4;
  2115. }
  2116. }
  2117. rev_advance_mrp(&magref, population_count(sig));
  2118. }
  2119. }
  2120. //do the last incomplete stripe
  2121. // for cases of (height & 3) == 0 and 3
  2122. // the should have been processed previously
  2123. if ((height & 3) == 1 || (height & 3) == 2) {
  2124. //generate mbr of first stripe
  2125. OPJ_UINT32 *sig = height & 0x4 ? sigma2 : sigma1;
  2126. OPJ_UINT32 *mbr = height & 0x4 ? mbr2 : mbr1;
  2127. //integrate horizontally
  2128. OPJ_UINT32 prev = 0;
  2129. OPJ_INT32 i;
  2130. for (i = 0; i < width; i += 8, mbr++, sig++) {
  2131. OPJ_UINT32 t, z;
  2132. mbr[0] = sig[0];
  2133. mbr[0] |= prev >> 28; //for first column, left neighbors
  2134. mbr[0] |= sig[0] << 4; //left neighbors
  2135. mbr[0] |= sig[0] >> 4; //left neighbors
  2136. mbr[0] |= sig[1] << 28; //for last column, right neighbors
  2137. prev = sig[0];
  2138. //integrate vertically
  2139. t = mbr[0], z = mbr[0];
  2140. z |= (t & 0x77777777) << 1; //above neighbors
  2141. z |= (t & 0xEEEEEEEE) >> 1; //below neighbors
  2142. mbr[0] = z & ~sig[0]; //remove already significance samples
  2143. }
  2144. }
  2145. st = height;
  2146. st -= height > 6 ? (((height + 1) & 3) + 3) : height;
  2147. for (y = st; y < height; y += 4) {
  2148. OPJ_UINT32 *cur_sig, *cur_mbr, *nxt_sig, *nxt_mbr;
  2149. OPJ_UINT32 val;
  2150. OPJ_INT32 i;
  2151. OPJ_UINT32 pattern = 0xFFFFFFFFu; // a pattern needed samples
  2152. if (height - y == 3) {
  2153. pattern = 0x77777777u;
  2154. } else if (height - y == 2) {
  2155. pattern = 0x33333333u;
  2156. } else if (height - y == 1) {
  2157. pattern = 0x11111111u;
  2158. }
  2159. //add membership from the next stripe, obtained above
  2160. if (height - y > 4) {
  2161. OPJ_UINT32 prev = 0;
  2162. OPJ_INT32 i;
  2163. cur_sig = y & 0x4 ? sigma2 : sigma1;
  2164. cur_mbr = y & 0x4 ? mbr2 : mbr1;
  2165. nxt_sig = y & 0x4 ? sigma1 : sigma2;
  2166. for (i = 0; i < width; i += 8, cur_mbr++, cur_sig++, nxt_sig++) {
  2167. OPJ_UINT32 t = nxt_sig[0];
  2168. t |= prev >> 28; //for first column, left neighbors
  2169. t |= nxt_sig[0] << 4; //left neighbors
  2170. t |= nxt_sig[0] >> 4; //left neighbors
  2171. t |= nxt_sig[1] << 28; //for last column, right neighbors
  2172. prev = nxt_sig[0];
  2173. if (!stripe_causal) {
  2174. cur_mbr[0] |= (t & 0x11111111u) << 3;
  2175. }
  2176. //remove already significance samples
  2177. cur_mbr[0] &= ~cur_sig[0];
  2178. }
  2179. }
  2180. //find new locations and get signs
  2181. cur_sig = y & 0x4 ? sigma2 : sigma1;
  2182. cur_mbr = y & 0x4 ? mbr2 : mbr1;
  2183. nxt_sig = y & 0x4 ? sigma1 : sigma2;
  2184. nxt_mbr = y & 0x4 ? mbr1 : mbr2;
  2185. val = 3u << (p - 2);
  2186. for (i = 0; i < width; i += 8,
  2187. cur_sig++, cur_mbr++, nxt_sig++, nxt_mbr++) {
  2188. OPJ_UINT32 mbr = *cur_mbr & pattern; //skip unneeded samples
  2189. OPJ_UINT32 new_sig = 0;
  2190. OPJ_UINT32 ux, tx;
  2191. if (mbr) {
  2192. OPJ_INT32 n;
  2193. for (n = 0; n < 8; n += 4) {
  2194. OPJ_UINT32 col_mask;
  2195. OPJ_UINT32 inv_sig;
  2196. OPJ_INT32 end;
  2197. OPJ_INT32 j;
  2198. OPJ_UINT32 cwd = frwd_fetch(&sigprop);
  2199. OPJ_UINT32 cnt = 0;
  2200. OPJ_UINT32 *dp = decoded_data + y * stride;
  2201. dp += i + n;
  2202. col_mask = 0xFu << (4 * n);
  2203. inv_sig = ~cur_sig[0] & pattern;
  2204. end = n + 4 + i < width ? n + 4 : width - i;
  2205. for (j = n; j < end; ++j, ++dp, col_mask <<= 4) {
  2206. OPJ_UINT32 sample_mask;
  2207. if ((col_mask & mbr) == 0) {
  2208. continue;
  2209. }
  2210. //scan 4 mbr
  2211. sample_mask = 0x11111111u & col_mask;
  2212. if (mbr & sample_mask) {
  2213. assert(dp[0] == 0);
  2214. if (cwd & 1) {
  2215. OPJ_UINT32 t;
  2216. new_sig |= sample_mask;
  2217. t = 0x32u << (j * 4);
  2218. mbr |= t & inv_sig;
  2219. }
  2220. cwd >>= 1;
  2221. ++cnt;
  2222. }
  2223. sample_mask += sample_mask;
  2224. if (mbr & sample_mask) {
  2225. assert(dp[stride] == 0);
  2226. if (cwd & 1) {
  2227. OPJ_UINT32 t;
  2228. new_sig |= sample_mask;
  2229. t = 0x74u << (j * 4);
  2230. mbr |= t & inv_sig;
  2231. }
  2232. cwd >>= 1;
  2233. ++cnt;
  2234. }
  2235. sample_mask += sample_mask;
  2236. if (mbr & sample_mask) {
  2237. assert(dp[2 * stride] == 0);
  2238. if (cwd & 1) {
  2239. OPJ_UINT32 t;
  2240. new_sig |= sample_mask;
  2241. t = 0xE8u << (j * 4);
  2242. mbr |= t & inv_sig;
  2243. }
  2244. cwd >>= 1;
  2245. ++cnt;
  2246. }
  2247. sample_mask += sample_mask;
  2248. if (mbr & sample_mask) {
  2249. assert(dp[3 * stride] == 0);
  2250. if (cwd & 1) {
  2251. OPJ_UINT32 t;
  2252. new_sig |= sample_mask;
  2253. t = 0xC0u << (j * 4);
  2254. mbr |= t & inv_sig;
  2255. }
  2256. cwd >>= 1;
  2257. ++cnt;
  2258. }
  2259. }
  2260. //signs here
  2261. if (new_sig & (0xFFFFu << (4 * n))) {
  2262. OPJ_UINT32 col_mask;
  2263. OPJ_INT32 j;
  2264. OPJ_UINT32 *dp = decoded_data + y * stride;
  2265. dp += i + n;
  2266. col_mask = 0xFu << (4 * n);
  2267. for (j = n; j < end; ++j, ++dp, col_mask <<= 4) {
  2268. OPJ_UINT32 sample_mask;
  2269. if ((col_mask & new_sig) == 0) {
  2270. continue;
  2271. }
  2272. //scan 4 signs
  2273. sample_mask = 0x11111111u & col_mask;
  2274. if (new_sig & sample_mask) {
  2275. assert(dp[0] == 0);
  2276. dp[0] |= ((cwd & 1) << 31) | val;
  2277. cwd >>= 1;
  2278. ++cnt;
  2279. }
  2280. sample_mask += sample_mask;
  2281. if (new_sig & sample_mask) {
  2282. assert(dp[stride] == 0);
  2283. dp[stride] |= ((cwd & 1) << 31) | val;
  2284. cwd >>= 1;
  2285. ++cnt;
  2286. }
  2287. sample_mask += sample_mask;
  2288. if (new_sig & sample_mask) {
  2289. assert(dp[2 * stride] == 0);
  2290. dp[2 * stride] |= ((cwd & 1) << 31) | val;
  2291. cwd >>= 1;
  2292. ++cnt;
  2293. }
  2294. sample_mask += sample_mask;
  2295. if (new_sig & sample_mask) {
  2296. assert(dp[3 * stride] == 0);
  2297. dp[3 * stride] |= ((cwd & 1) << 31) | val;
  2298. cwd >>= 1;
  2299. ++cnt;
  2300. }
  2301. }
  2302. }
  2303. frwd_advance(&sigprop, cnt);
  2304. cnt = 0;
  2305. //update next columns
  2306. if (n == 4) {
  2307. //horizontally
  2308. OPJ_UINT32 t = new_sig >> 28;
  2309. t |= ((t & 0xE) >> 1) | ((t & 7) << 1);
  2310. cur_mbr[1] |= t & ~cur_sig[1];
  2311. }
  2312. }
  2313. }
  2314. //propagate down (vertically propagation)
  2315. new_sig |= cur_sig[0];
  2316. ux = (new_sig & 0x88888888) >> 3;
  2317. tx = ux | (ux << 4) | (ux >> 4);
  2318. if (i > 0) {
  2319. nxt_mbr[-1] |= (ux << 28) & ~nxt_sig[-1];
  2320. }
  2321. nxt_mbr[0] |= tx & ~nxt_sig[0];
  2322. nxt_mbr[1] |= (ux >> 28) & ~nxt_sig[1];
  2323. }
  2324. }
  2325. }
  2326. {
  2327. OPJ_INT32 x, y;
  2328. for (y = 0; y < height; ++y) {
  2329. OPJ_INT32* sp = (OPJ_INT32*)decoded_data + y * stride;
  2330. for (x = 0; x < width; ++x, ++sp) {
  2331. OPJ_INT32 val = (*sp & 0x7FFFFFFF);
  2332. *sp = ((OPJ_UINT32) * sp & 0x80000000) ? -val : val;
  2333. }
  2334. }
  2335. }
  2336. return OPJ_TRUE;
  2337. }