ht_dec.c 107 KB

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