t1.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
  8. * Copyright (c) 2002-2014, Professor Benoit Macq
  9. * Copyright (c) 2001-2003, David Janssens
  10. * Copyright (c) 2002-2003, Yannick Verschueren
  11. * Copyright (c) 2003-2007, Francois-Olivier Devaux
  12. * Copyright (c) 2003-2014, Antonin Descampe
  13. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  14. * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
  15. * Copyright (c) 2012, Carl Hetherington
  16. * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. */
  40. #define OPJ_SKIP_POISON
  41. #include "opj_includes.h"
  42. #ifdef __SSE__
  43. #include <xmmintrin.h>
  44. #endif
  45. #ifdef __SSE2__
  46. #include <emmintrin.h>
  47. #endif
  48. #if defined(__GNUC__)
  49. #pragma GCC poison malloc calloc realloc free
  50. #endif
  51. #include "t1_luts.h"
  52. /** @defgroup T1 T1 - Implementation of the tier-1 coding */
  53. /*@{*/
  54. #define T1_FLAGS(x, y) (t1->flags[x + 1 + ((y / 4) + 1) * (t1->w+2)])
  55. #define opj_t1_setcurctx(curctx, ctxno) curctx = &(mqc)->ctxs[(OPJ_UINT32)(ctxno)]
  56. /* Macros to deal with signed integer with just MSB bit set for
  57. * negative values (smr = signed magnitude representation) */
  58. #define opj_smr_abs(x) (((OPJ_UINT32)(x)) & 0x7FFFFFFFU)
  59. #define opj_smr_sign(x) (((OPJ_UINT32)(x)) >> 31)
  60. #define opj_to_smr(x) ((x) >= 0 ? (OPJ_UINT32)(x) : ((OPJ_UINT32)(-x) | 0x80000000U))
  61. /** @name Local static functions */
  62. /*@{*/
  63. static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f);
  64. static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
  65. static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
  66. static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
  67. static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
  68. OPJ_UINT32 s, OPJ_UINT32 stride,
  69. OPJ_UINT32 vsc);
  70. /**
  71. Decode significant pass
  72. */
  73. static INLINE void opj_t1_dec_sigpass_step_raw(
  74. opj_t1_t *t1,
  75. opj_flag_t *flagsp,
  76. OPJ_INT32 *datap,
  77. OPJ_INT32 oneplushalf,
  78. OPJ_UINT32 vsc,
  79. OPJ_UINT32 row);
  80. static INLINE void opj_t1_dec_sigpass_step_mqc(
  81. opj_t1_t *t1,
  82. opj_flag_t *flagsp,
  83. OPJ_INT32 *datap,
  84. OPJ_INT32 oneplushalf,
  85. OPJ_UINT32 row,
  86. OPJ_UINT32 flags_stride,
  87. OPJ_UINT32 vsc);
  88. /**
  89. Encode significant pass
  90. */
  91. static void opj_t1_enc_sigpass(opj_t1_t *t1,
  92. OPJ_INT32 bpno,
  93. OPJ_INT32 *nmsedec,
  94. OPJ_BYTE type,
  95. OPJ_UINT32 cblksty);
  96. /**
  97. Decode significant pass
  98. */
  99. static void opj_t1_dec_sigpass_raw(
  100. opj_t1_t *t1,
  101. OPJ_INT32 bpno,
  102. OPJ_INT32 cblksty);
  103. /**
  104. Encode refinement pass
  105. */
  106. static void opj_t1_enc_refpass(opj_t1_t *t1,
  107. OPJ_INT32 bpno,
  108. OPJ_INT32 *nmsedec,
  109. OPJ_BYTE type);
  110. /**
  111. Decode refinement pass
  112. */
  113. static void opj_t1_dec_refpass_raw(
  114. opj_t1_t *t1,
  115. OPJ_INT32 bpno);
  116. /**
  117. Decode refinement pass
  118. */
  119. static INLINE void opj_t1_dec_refpass_step_raw(
  120. opj_t1_t *t1,
  121. opj_flag_t *flagsp,
  122. OPJ_INT32 *datap,
  123. OPJ_INT32 poshalf,
  124. OPJ_UINT32 row);
  125. static INLINE void opj_t1_dec_refpass_step_mqc(
  126. opj_t1_t *t1,
  127. opj_flag_t *flagsp,
  128. OPJ_INT32 *datap,
  129. OPJ_INT32 poshalf,
  130. OPJ_UINT32 row);
  131. /**
  132. Decode clean-up pass
  133. */
  134. static void opj_t1_dec_clnpass_step(
  135. opj_t1_t *t1,
  136. opj_flag_t *flagsp,
  137. OPJ_INT32 *datap,
  138. OPJ_INT32 oneplushalf,
  139. OPJ_UINT32 row,
  140. OPJ_UINT32 vsc);
  141. /**
  142. Encode clean-up pass
  143. */
  144. static void opj_t1_enc_clnpass(
  145. opj_t1_t *t1,
  146. OPJ_INT32 bpno,
  147. OPJ_INT32 *nmsedec,
  148. OPJ_UINT32 cblksty);
  149. static OPJ_FLOAT64 opj_t1_getwmsedec(
  150. OPJ_INT32 nmsedec,
  151. OPJ_UINT32 compno,
  152. OPJ_UINT32 level,
  153. OPJ_UINT32 orient,
  154. OPJ_INT32 bpno,
  155. OPJ_UINT32 qmfbid,
  156. OPJ_FLOAT64 stepsize,
  157. OPJ_UINT32 numcomps,
  158. const OPJ_FLOAT64 * mct_norms,
  159. OPJ_UINT32 mct_numcomps);
  160. /** Return "cumwmsedec" that should be used to increase tile->distotile */
  161. static double opj_t1_encode_cblk(opj_t1_t *t1,
  162. opj_tcd_cblk_enc_t* cblk,
  163. OPJ_UINT32 orient,
  164. OPJ_UINT32 compno,
  165. OPJ_UINT32 level,
  166. OPJ_UINT32 qmfbid,
  167. OPJ_FLOAT64 stepsize,
  168. OPJ_UINT32 cblksty,
  169. OPJ_UINT32 numcomps,
  170. const OPJ_FLOAT64 * mct_norms,
  171. OPJ_UINT32 mct_numcomps);
  172. /**
  173. Decode 1 code-block
  174. @param t1 T1 handle
  175. @param cblk Code-block coding parameters
  176. @param orient
  177. @param roishift Region of interest shifting value
  178. @param cblksty Code-block style
  179. @param p_manager the event manager
  180. @param p_manager_mutex mutex for the event manager
  181. @param check_pterm whether PTERM correct termination should be checked
  182. */
  183. static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
  184. opj_tcd_cblk_dec_t* cblk,
  185. OPJ_UINT32 orient,
  186. OPJ_UINT32 roishift,
  187. OPJ_UINT32 cblksty,
  188. opj_event_mgr_t *p_manager,
  189. opj_mutex_t* p_manager_mutex,
  190. OPJ_BOOL check_pterm);
  191. /**
  192. Decode 1 HT code-block
  193. @param t1 T1 handle
  194. @param cblk Code-block coding parameters
  195. @param orient
  196. @param roishift Region of interest shifting value
  197. @param cblksty Code-block style
  198. @param p_manager the event manager
  199. @param p_manager_mutex mutex for the event manager
  200. @param check_pterm whether PTERM correct termination should be checked
  201. */
  202. OPJ_BOOL opj_t1_ht_decode_cblk(opj_t1_t *t1,
  203. opj_tcd_cblk_dec_t* cblk,
  204. OPJ_UINT32 orient,
  205. OPJ_UINT32 roishift,
  206. OPJ_UINT32 cblksty,
  207. opj_event_mgr_t *p_manager,
  208. opj_mutex_t* p_manager_mutex,
  209. OPJ_BOOL check_pterm);
  210. static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1,
  211. OPJ_UINT32 w,
  212. OPJ_UINT32 h);
  213. /*@}*/
  214. /*@}*/
  215. /* ----------------------------------------------------------------------- */
  216. static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f)
  217. {
  218. return mqc->lut_ctxno_zc_orient[(f & T1_SIGMA_NEIGHBOURS)];
  219. }
  220. static INLINE OPJ_UINT32 opj_t1_getctxtno_sc_or_spb_index(OPJ_UINT32 fX,
  221. OPJ_UINT32 pfX,
  222. OPJ_UINT32 nfX,
  223. OPJ_UINT32 ci)
  224. {
  225. /*
  226. 0 pfX T1_CHI_THIS T1_LUT_SGN_W
  227. 1 tfX T1_SIGMA_1 T1_LUT_SIG_N
  228. 2 nfX T1_CHI_THIS T1_LUT_SGN_E
  229. 3 tfX T1_SIGMA_3 T1_LUT_SIG_W
  230. 4 fX T1_CHI_(THIS - 1) T1_LUT_SGN_N
  231. 5 tfX T1_SIGMA_5 T1_LUT_SIG_E
  232. 6 fX T1_CHI_(THIS + 1) T1_LUT_SGN_S
  233. 7 tfX T1_SIGMA_7 T1_LUT_SIG_S
  234. */
  235. OPJ_UINT32 lu = (fX >> (ci * 3U)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 |
  236. T1_SIGMA_7);
  237. lu |= (pfX >> (T1_CHI_THIS_I + (ci * 3U))) & (1U << 0);
  238. lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2);
  239. if (ci == 0U) {
  240. lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
  241. } else {
  242. lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4);
  243. }
  244. lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6);
  245. return lu;
  246. }
  247. static INLINE OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 lu)
  248. {
  249. return lut_ctxno_sc[lu];
  250. }
  251. static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f)
  252. {
  253. OPJ_UINT32 tmp = (f & T1_SIGMA_NEIGHBOURS) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
  254. OPJ_UINT32 tmp2 = (f & T1_MU_0) ? T1_CTXNO_MAG + 2 : tmp;
  255. return tmp2;
  256. }
  257. static INLINE OPJ_BYTE opj_t1_getspb(OPJ_UINT32 lu)
  258. {
  259. return lut_spb[lu];
  260. }
  261. static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
  262. {
  263. if (bitpos > 0) {
  264. return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
  265. }
  266. return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
  267. }
  268. static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos)
  269. {
  270. if (bitpos > 0) {
  271. return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
  272. }
  273. return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
  274. }
  275. #define opj_t1_update_flags_macro(flags, flagsp, ci, s, stride, vsc) \
  276. { \
  277. /* east */ \
  278. flagsp[-1] |= T1_SIGMA_5 << (3U * ci); \
  279. \
  280. /* mark target as significant */ \
  281. flags |= ((s << T1_CHI_1_I) | T1_SIGMA_4) << (3U * ci); \
  282. \
  283. /* west */ \
  284. flagsp[1] |= T1_SIGMA_3 << (3U * ci); \
  285. \
  286. /* north-west, north, north-east */ \
  287. if (ci == 0U && !(vsc)) { \
  288. opj_flag_t* north = flagsp - (stride); \
  289. *north |= (s << T1_CHI_5_I) | T1_SIGMA_16; \
  290. north[-1] |= T1_SIGMA_17; \
  291. north[1] |= T1_SIGMA_15; \
  292. } \
  293. \
  294. /* south-west, south, south-east */ \
  295. if (ci == 3U) { \
  296. opj_flag_t* south = flagsp + (stride); \
  297. *south |= (s << T1_CHI_0_I) | T1_SIGMA_1; \
  298. south[-1] |= T1_SIGMA_2; \
  299. south[1] |= T1_SIGMA_0; \
  300. } \
  301. }
  302. static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
  303. OPJ_UINT32 s, OPJ_UINT32 stride,
  304. OPJ_UINT32 vsc)
  305. {
  306. opj_t1_update_flags_macro(*flagsp, flagsp, ci, s, stride, vsc);
  307. }
  308. /**
  309. Encode significant pass
  310. */
  311. #define opj_t1_enc_sigpass_step_macro(mqc, curctx, a, c, ct, flagspIn, datapIn, bpno, one, nmsedec, type, ciIn, vscIn) \
  312. { \
  313. OPJ_UINT32 v; \
  314. const OPJ_UINT32 ci = (ciIn); \
  315. const OPJ_UINT32 vsc = (vscIn); \
  316. const OPJ_INT32* l_datap = (datapIn); \
  317. opj_flag_t* flagsp = (flagspIn); \
  318. OPJ_UINT32 const flags = *flagsp; \
  319. if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U && \
  320. (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) { \
  321. OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
  322. v = (opj_smr_abs(*l_datap) & (OPJ_UINT32)one) ? 1 : 0; \
  323. /* #ifdef DEBUG_ENC_SIG */ \
  324. /* fprintf(stderr, " ctxt1=%d\n", ctxt1); */ \
  325. /* #endif */ \
  326. opj_t1_setcurctx(curctx, ctxt1); \
  327. if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ \
  328. opj_mqc_bypass_enc_macro(mqc, c, ct, v); \
  329. } else { \
  330. opj_mqc_encode_macro(mqc, curctx, a, c, ct, v); \
  331. } \
  332. if (v) { \
  333. OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
  334. *flagsp, \
  335. flagsp[-1], flagsp[1], \
  336. ci); \
  337. OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu); \
  338. v = opj_smr_sign(*l_datap); \
  339. *nmsedec += opj_t1_getnmsedec_sig(opj_smr_abs(*l_datap), \
  340. (OPJ_UINT32)bpno); \
  341. /* #ifdef DEBUG_ENC_SIG */ \
  342. /* fprintf(stderr, " ctxt2=%d\n", ctxt2); */ \
  343. /* #endif */ \
  344. opj_t1_setcurctx(curctx, ctxt2); \
  345. if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ \
  346. opj_mqc_bypass_enc_macro(mqc, c, ct, v); \
  347. } else { \
  348. OPJ_UINT32 spb = opj_t1_getspb(lu); \
  349. /* #ifdef DEBUG_ENC_SIG */ \
  350. /* fprintf(stderr, " spb=%d\n", spb); */ \
  351. /* #endif */ \
  352. opj_mqc_encode_macro(mqc, curctx, a, c, ct, v ^ spb); \
  353. } \
  354. opj_t1_update_flags(flagsp, ci, v, t1->w + 2, vsc); \
  355. } \
  356. *flagsp |= T1_PI_THIS << (ci * 3U); \
  357. } \
  358. }
  359. static INLINE void opj_t1_dec_sigpass_step_raw(
  360. opj_t1_t *t1,
  361. opj_flag_t *flagsp,
  362. OPJ_INT32 *datap,
  363. OPJ_INT32 oneplushalf,
  364. OPJ_UINT32 vsc,
  365. OPJ_UINT32 ci)
  366. {
  367. OPJ_UINT32 v;
  368. opj_mqc_t *mqc = &(t1->mqc); /* RAW component */
  369. OPJ_UINT32 const flags = *flagsp;
  370. if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
  371. (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
  372. if (opj_mqc_raw_decode(mqc)) {
  373. v = opj_mqc_raw_decode(mqc);
  374. *datap = v ? -oneplushalf : oneplushalf;
  375. opj_t1_update_flags(flagsp, ci, v, t1->w + 2, vsc);
  376. }
  377. *flagsp |= T1_PI_THIS << (ci * 3U);
  378. }
  379. }
  380. #define opj_t1_dec_sigpass_step_mqc_macro(flags, flagsp, flags_stride, data, \
  381. data_stride, ci, mqc, curctx, \
  382. v, a, c, ct, oneplushalf, vsc) \
  383. { \
  384. if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U && \
  385. (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) { \
  386. OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
  387. opj_t1_setcurctx(curctx, ctxt1); \
  388. opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
  389. if (v) { \
  390. OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
  391. flags, \
  392. flagsp[-1], flagsp[1], \
  393. ci); \
  394. OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu); \
  395. OPJ_UINT32 spb = opj_t1_getspb(lu); \
  396. opj_t1_setcurctx(curctx, ctxt2); \
  397. opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
  398. v = v ^ spb; \
  399. data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \
  400. opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride, vsc); \
  401. } \
  402. flags |= T1_PI_THIS << (ci * 3U); \
  403. } \
  404. }
  405. static INLINE void opj_t1_dec_sigpass_step_mqc(
  406. opj_t1_t *t1,
  407. opj_flag_t *flagsp,
  408. OPJ_INT32 *datap,
  409. OPJ_INT32 oneplushalf,
  410. OPJ_UINT32 ci,
  411. OPJ_UINT32 flags_stride,
  412. OPJ_UINT32 vsc)
  413. {
  414. OPJ_UINT32 v;
  415. opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
  416. opj_t1_dec_sigpass_step_mqc_macro(*flagsp, flagsp, flags_stride, datap,
  417. 0, ci, mqc, mqc->curctx,
  418. v, mqc->a, mqc->c, mqc->ct, oneplushalf, vsc);
  419. }
  420. static void opj_t1_enc_sigpass(opj_t1_t *t1,
  421. OPJ_INT32 bpno,
  422. OPJ_INT32 *nmsedec,
  423. OPJ_BYTE type,
  424. OPJ_UINT32 cblksty
  425. )
  426. {
  427. OPJ_UINT32 i, k;
  428. OPJ_INT32 const one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
  429. opj_flag_t* f = &T1_FLAGS(0, 0);
  430. OPJ_UINT32 const extra = 2;
  431. opj_mqc_t* mqc = &(t1->mqc);
  432. DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct);
  433. const OPJ_INT32* datap = t1->data;
  434. *nmsedec = 0;
  435. #ifdef DEBUG_ENC_SIG
  436. fprintf(stderr, "enc_sigpass: bpno=%d\n", bpno);
  437. #endif
  438. for (k = 0; k < (t1->h & ~3U); k += 4, f += extra) {
  439. const OPJ_UINT32 w = t1->w;
  440. #ifdef DEBUG_ENC_SIG
  441. fprintf(stderr, " k=%d\n", k);
  442. #endif
  443. for (i = 0; i < w; ++i, ++f, datap += 4) {
  444. #ifdef DEBUG_ENC_SIG
  445. fprintf(stderr, " i=%d\n", i);
  446. #endif
  447. if (*f == 0U) {
  448. /* Nothing to do for any of the 4 data points */
  449. continue;
  450. }
  451. opj_t1_enc_sigpass_step_macro(
  452. mqc, curctx, a, c, ct,
  453. f,
  454. &datap[0],
  455. bpno,
  456. one,
  457. nmsedec,
  458. type,
  459. 0, cblksty & J2K_CCP_CBLKSTY_VSC);
  460. opj_t1_enc_sigpass_step_macro(
  461. mqc, curctx, a, c, ct,
  462. f,
  463. &datap[1],
  464. bpno,
  465. one,
  466. nmsedec,
  467. type,
  468. 1, 0);
  469. opj_t1_enc_sigpass_step_macro(
  470. mqc, curctx, a, c, ct,
  471. f,
  472. &datap[2],
  473. bpno,
  474. one,
  475. nmsedec,
  476. type,
  477. 2, 0);
  478. opj_t1_enc_sigpass_step_macro(
  479. mqc, curctx, a, c, ct,
  480. f,
  481. &datap[3],
  482. bpno,
  483. one,
  484. nmsedec,
  485. type,
  486. 3, 0);
  487. }
  488. }
  489. if (k < t1->h) {
  490. OPJ_UINT32 j;
  491. #ifdef DEBUG_ENC_SIG
  492. fprintf(stderr, " k=%d\n", k);
  493. #endif
  494. for (i = 0; i < t1->w; ++i, ++f) {
  495. #ifdef DEBUG_ENC_SIG
  496. fprintf(stderr, " i=%d\n", i);
  497. #endif
  498. if (*f == 0U) {
  499. /* Nothing to do for any of the 4 data points */
  500. datap += (t1->h - k);
  501. continue;
  502. }
  503. for (j = k; j < t1->h; ++j, ++datap) {
  504. opj_t1_enc_sigpass_step_macro(
  505. mqc, curctx, a, c, ct,
  506. f,
  507. &datap[0],
  508. bpno,
  509. one,
  510. nmsedec,
  511. type,
  512. j - k,
  513. (j == k && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
  514. }
  515. }
  516. }
  517. UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct);
  518. }
  519. static void opj_t1_dec_sigpass_raw(
  520. opj_t1_t *t1,
  521. OPJ_INT32 bpno,
  522. OPJ_INT32 cblksty)
  523. {
  524. OPJ_INT32 one, half, oneplushalf;
  525. OPJ_UINT32 i, j, k;
  526. OPJ_INT32 *data = t1->data;
  527. opj_flag_t *flagsp = &T1_FLAGS(0, 0);
  528. const OPJ_UINT32 l_w = t1->w;
  529. one = 1 << bpno;
  530. half = one >> 1;
  531. oneplushalf = one | half;
  532. for (k = 0; k < (t1->h & ~3U); k += 4, flagsp += 2, data += 3 * l_w) {
  533. for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
  534. opj_flag_t flags = *flagsp;
  535. if (flags != 0) {
  536. opj_t1_dec_sigpass_step_raw(
  537. t1,
  538. flagsp,
  539. data,
  540. oneplushalf,
  541. cblksty & J2K_CCP_CBLKSTY_VSC, /* vsc */
  542. 0U);
  543. opj_t1_dec_sigpass_step_raw(
  544. t1,
  545. flagsp,
  546. data + l_w,
  547. oneplushalf,
  548. OPJ_FALSE, /* vsc */
  549. 1U);
  550. opj_t1_dec_sigpass_step_raw(
  551. t1,
  552. flagsp,
  553. data + 2 * l_w,
  554. oneplushalf,
  555. OPJ_FALSE, /* vsc */
  556. 2U);
  557. opj_t1_dec_sigpass_step_raw(
  558. t1,
  559. flagsp,
  560. data + 3 * l_w,
  561. oneplushalf,
  562. OPJ_FALSE, /* vsc */
  563. 3U);
  564. }
  565. }
  566. }
  567. if (k < t1->h) {
  568. for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
  569. for (j = 0; j < t1->h - k; ++j) {
  570. opj_t1_dec_sigpass_step_raw(
  571. t1,
  572. flagsp,
  573. data + j * l_w,
  574. oneplushalf,
  575. cblksty & J2K_CCP_CBLKSTY_VSC, /* vsc */
  576. j);
  577. }
  578. }
  579. }
  580. }
  581. #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, vsc, w, h, flags_stride) \
  582. { \
  583. OPJ_INT32 one, half, oneplushalf; \
  584. OPJ_UINT32 i, j, k; \
  585. register OPJ_INT32 *data = t1->data; \
  586. register opj_flag_t *flagsp = &t1->flags[(flags_stride) + 1]; \
  587. const OPJ_UINT32 l_w = w; \
  588. opj_mqc_t* mqc = &(t1->mqc); \
  589. DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \
  590. register OPJ_UINT32 v; \
  591. one = 1 << bpno; \
  592. half = one >> 1; \
  593. oneplushalf = one | half; \
  594. for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
  595. for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
  596. opj_flag_t flags = *flagsp; \
  597. if( flags != 0 ) { \
  598. opj_t1_dec_sigpass_step_mqc_macro( \
  599. flags, flagsp, flags_stride, data, \
  600. l_w, 0, mqc, curctx, v, a, c, ct, oneplushalf, vsc); \
  601. opj_t1_dec_sigpass_step_mqc_macro( \
  602. flags, flagsp, flags_stride, data, \
  603. l_w, 1, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \
  604. opj_t1_dec_sigpass_step_mqc_macro( \
  605. flags, flagsp, flags_stride, data, \
  606. l_w, 2, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \
  607. opj_t1_dec_sigpass_step_mqc_macro( \
  608. flags, flagsp, flags_stride, data, \
  609. l_w, 3, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \
  610. *flagsp = flags; \
  611. } \
  612. } \
  613. } \
  614. UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \
  615. if( k < h ) { \
  616. for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
  617. for (j = 0; j < h - k; ++j) { \
  618. opj_t1_dec_sigpass_step_mqc(t1, flagsp, \
  619. data + j * l_w, oneplushalf, j, flags_stride, vsc); \
  620. } \
  621. } \
  622. } \
  623. }
  624. static void opj_t1_dec_sigpass_mqc_64x64_novsc(
  625. opj_t1_t *t1,
  626. OPJ_INT32 bpno)
  627. {
  628. opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_FALSE, 64, 64, 66);
  629. }
  630. static void opj_t1_dec_sigpass_mqc_64x64_vsc(
  631. opj_t1_t *t1,
  632. OPJ_INT32 bpno)
  633. {
  634. opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_TRUE, 64, 64, 66);
  635. }
  636. static void opj_t1_dec_sigpass_mqc_generic_novsc(
  637. opj_t1_t *t1,
  638. OPJ_INT32 bpno)
  639. {
  640. opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_FALSE, t1->w, t1->h,
  641. t1->w + 2U);
  642. }
  643. static void opj_t1_dec_sigpass_mqc_generic_vsc(
  644. opj_t1_t *t1,
  645. OPJ_INT32 bpno)
  646. {
  647. opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_TRUE, t1->w, t1->h,
  648. t1->w + 2U);
  649. }
  650. static void opj_t1_dec_sigpass_mqc(
  651. opj_t1_t *t1,
  652. OPJ_INT32 bpno,
  653. OPJ_INT32 cblksty)
  654. {
  655. if (t1->w == 64 && t1->h == 64) {
  656. if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  657. opj_t1_dec_sigpass_mqc_64x64_vsc(t1, bpno);
  658. } else {
  659. opj_t1_dec_sigpass_mqc_64x64_novsc(t1, bpno);
  660. }
  661. } else {
  662. if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  663. opj_t1_dec_sigpass_mqc_generic_vsc(t1, bpno);
  664. } else {
  665. opj_t1_dec_sigpass_mqc_generic_novsc(t1, bpno);
  666. }
  667. }
  668. }
  669. /**
  670. Encode refinement pass step
  671. */
  672. #define opj_t1_enc_refpass_step_macro(mqc, curctx, a, c, ct, flags, flagsUpdated, datap, bpno, one, nmsedec, type, ci) \
  673. {\
  674. OPJ_UINT32 v; \
  675. if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << ((ci) * 3U))) == (T1_SIGMA_THIS << ((ci) * 3U))) { \
  676. const OPJ_UINT32 shift_flags = (flags >> ((ci) * 3U)); \
  677. OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags); \
  678. OPJ_UINT32 abs_data = opj_smr_abs(*datap); \
  679. *nmsedec += opj_t1_getnmsedec_ref(abs_data, \
  680. (OPJ_UINT32)bpno); \
  681. v = ((OPJ_INT32)abs_data & one) ? 1 : 0; \
  682. /* #ifdef DEBUG_ENC_REF */ \
  683. /* fprintf(stderr, " ctxt=%d\n", ctxt); */ \
  684. /* #endif */ \
  685. opj_t1_setcurctx(curctx, ctxt); \
  686. if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ \
  687. opj_mqc_bypass_enc_macro(mqc, c, ct, v); \
  688. } else { \
  689. opj_mqc_encode_macro(mqc, curctx, a, c, ct, v); \
  690. } \
  691. flagsUpdated |= T1_MU_THIS << ((ci) * 3U); \
  692. } \
  693. }
  694. static INLINE void opj_t1_dec_refpass_step_raw(
  695. opj_t1_t *t1,
  696. opj_flag_t *flagsp,
  697. OPJ_INT32 *datap,
  698. OPJ_INT32 poshalf,
  699. OPJ_UINT32 ci)
  700. {
  701. OPJ_UINT32 v;
  702. opj_mqc_t *mqc = &(t1->mqc); /* RAW component */
  703. if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
  704. (T1_SIGMA_THIS << (ci * 3U))) {
  705. v = opj_mqc_raw_decode(mqc);
  706. *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf;
  707. *flagsp |= T1_MU_THIS << (ci * 3U);
  708. }
  709. }
  710. #define opj_t1_dec_refpass_step_mqc_macro(flags, data, data_stride, ci, \
  711. mqc, curctx, v, a, c, ct, poshalf) \
  712. { \
  713. if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == \
  714. (T1_SIGMA_THIS << (ci * 3U))) { \
  715. OPJ_UINT32 ctxt = opj_t1_getctxno_mag(flags >> (ci * 3U)); \
  716. opj_t1_setcurctx(curctx, ctxt); \
  717. opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
  718. data[ci*data_stride] += (v ^ (data[ci*data_stride] < 0)) ? poshalf : -poshalf; \
  719. flags |= T1_MU_THIS << (ci * 3U); \
  720. } \
  721. }
  722. static INLINE void opj_t1_dec_refpass_step_mqc(
  723. opj_t1_t *t1,
  724. opj_flag_t *flagsp,
  725. OPJ_INT32 *datap,
  726. OPJ_INT32 poshalf,
  727. OPJ_UINT32 ci)
  728. {
  729. OPJ_UINT32 v;
  730. opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
  731. opj_t1_dec_refpass_step_mqc_macro(*flagsp, datap, 0, ci,
  732. mqc, mqc->curctx, v, mqc->a, mqc->c,
  733. mqc->ct, poshalf);
  734. }
  735. static void opj_t1_enc_refpass(
  736. opj_t1_t *t1,
  737. OPJ_INT32 bpno,
  738. OPJ_INT32 *nmsedec,
  739. OPJ_BYTE type)
  740. {
  741. OPJ_UINT32 i, k;
  742. const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
  743. opj_flag_t* f = &T1_FLAGS(0, 0);
  744. const OPJ_UINT32 extra = 2U;
  745. opj_mqc_t* mqc = &(t1->mqc);
  746. DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct);
  747. const OPJ_INT32* datap = t1->data;
  748. *nmsedec = 0;
  749. #ifdef DEBUG_ENC_REF
  750. fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
  751. #endif
  752. for (k = 0; k < (t1->h & ~3U); k += 4, f += extra) {
  753. #ifdef DEBUG_ENC_REF
  754. fprintf(stderr, " k=%d\n", k);
  755. #endif
  756. for (i = 0; i < t1->w; ++i, f++, datap += 4) {
  757. const OPJ_UINT32 flags = *f;
  758. OPJ_UINT32 flagsUpdated = flags;
  759. #ifdef DEBUG_ENC_REF
  760. fprintf(stderr, " i=%d\n", i);
  761. #endif
  762. if ((flags & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
  763. /* none significant */
  764. continue;
  765. }
  766. if ((flags & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
  767. (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
  768. /* all processed by sigpass */
  769. continue;
  770. }
  771. opj_t1_enc_refpass_step_macro(
  772. mqc, curctx, a, c, ct,
  773. flags, flagsUpdated,
  774. &datap[0],
  775. bpno,
  776. one,
  777. nmsedec,
  778. type,
  779. 0);
  780. opj_t1_enc_refpass_step_macro(
  781. mqc, curctx, a, c, ct,
  782. flags, flagsUpdated,
  783. &datap[1],
  784. bpno,
  785. one,
  786. nmsedec,
  787. type,
  788. 1);
  789. opj_t1_enc_refpass_step_macro(
  790. mqc, curctx, a, c, ct,
  791. flags, flagsUpdated,
  792. &datap[2],
  793. bpno,
  794. one,
  795. nmsedec,
  796. type,
  797. 2);
  798. opj_t1_enc_refpass_step_macro(
  799. mqc, curctx, a, c, ct,
  800. flags, flagsUpdated,
  801. &datap[3],
  802. bpno,
  803. one,
  804. nmsedec,
  805. type,
  806. 3);
  807. *f = flagsUpdated;
  808. }
  809. }
  810. if (k < t1->h) {
  811. OPJ_UINT32 j;
  812. const OPJ_UINT32 remaining_lines = t1->h - k;
  813. #ifdef DEBUG_ENC_REF
  814. fprintf(stderr, " k=%d\n", k);
  815. #endif
  816. for (i = 0; i < t1->w; ++i, ++f) {
  817. #ifdef DEBUG_ENC_REF
  818. fprintf(stderr, " i=%d\n", i);
  819. #endif
  820. if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
  821. /* none significant */
  822. datap += remaining_lines;
  823. continue;
  824. }
  825. for (j = 0; j < remaining_lines; ++j, datap ++) {
  826. opj_t1_enc_refpass_step_macro(
  827. mqc, curctx, a, c, ct,
  828. *f, *f,
  829. &datap[0],
  830. bpno,
  831. one,
  832. nmsedec,
  833. type,
  834. j);
  835. }
  836. }
  837. }
  838. UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct);
  839. }
  840. static void opj_t1_dec_refpass_raw(
  841. opj_t1_t *t1,
  842. OPJ_INT32 bpno)
  843. {
  844. OPJ_INT32 one, poshalf;
  845. OPJ_UINT32 i, j, k;
  846. OPJ_INT32 *data = t1->data;
  847. opj_flag_t *flagsp = &T1_FLAGS(0, 0);
  848. const OPJ_UINT32 l_w = t1->w;
  849. one = 1 << bpno;
  850. poshalf = one >> 1;
  851. for (k = 0; k < (t1->h & ~3U); k += 4, flagsp += 2, data += 3 * l_w) {
  852. for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
  853. opj_flag_t flags = *flagsp;
  854. if (flags != 0) {
  855. opj_t1_dec_refpass_step_raw(
  856. t1,
  857. flagsp,
  858. data,
  859. poshalf,
  860. 0U);
  861. opj_t1_dec_refpass_step_raw(
  862. t1,
  863. flagsp,
  864. data + l_w,
  865. poshalf,
  866. 1U);
  867. opj_t1_dec_refpass_step_raw(
  868. t1,
  869. flagsp,
  870. data + 2 * l_w,
  871. poshalf,
  872. 2U);
  873. opj_t1_dec_refpass_step_raw(
  874. t1,
  875. flagsp,
  876. data + 3 * l_w,
  877. poshalf,
  878. 3U);
  879. }
  880. }
  881. }
  882. if (k < t1->h) {
  883. for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
  884. for (j = 0; j < t1->h - k; ++j) {
  885. opj_t1_dec_refpass_step_raw(
  886. t1,
  887. flagsp,
  888. data + j * l_w,
  889. poshalf,
  890. j);
  891. }
  892. }
  893. }
  894. }
  895. #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
  896. { \
  897. OPJ_INT32 one, poshalf; \
  898. OPJ_UINT32 i, j, k; \
  899. register OPJ_INT32 *data = t1->data; \
  900. register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
  901. const OPJ_UINT32 l_w = w; \
  902. opj_mqc_t* mqc = &(t1->mqc); \
  903. DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \
  904. register OPJ_UINT32 v; \
  905. one = 1 << bpno; \
  906. poshalf = one >> 1; \
  907. for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
  908. for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
  909. opj_flag_t flags = *flagsp; \
  910. if( flags != 0 ) { \
  911. opj_t1_dec_refpass_step_mqc_macro( \
  912. flags, data, l_w, 0, \
  913. mqc, curctx, v, a, c, ct, poshalf); \
  914. opj_t1_dec_refpass_step_mqc_macro( \
  915. flags, data, l_w, 1, \
  916. mqc, curctx, v, a, c, ct, poshalf); \
  917. opj_t1_dec_refpass_step_mqc_macro( \
  918. flags, data, l_w, 2, \
  919. mqc, curctx, v, a, c, ct, poshalf); \
  920. opj_t1_dec_refpass_step_mqc_macro( \
  921. flags, data, l_w, 3, \
  922. mqc, curctx, v, a, c, ct, poshalf); \
  923. *flagsp = flags; \
  924. } \
  925. } \
  926. } \
  927. UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \
  928. if( k < h ) { \
  929. for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
  930. for (j = 0; j < h - k; ++j) { \
  931. opj_t1_dec_refpass_step_mqc(t1, flagsp, data + j * l_w, poshalf, j); \
  932. } \
  933. } \
  934. } \
  935. }
  936. static void opj_t1_dec_refpass_mqc_64x64(
  937. opj_t1_t *t1,
  938. OPJ_INT32 bpno)
  939. {
  940. opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
  941. }
  942. static void opj_t1_dec_refpass_mqc_generic(
  943. opj_t1_t *t1,
  944. OPJ_INT32 bpno)
  945. {
  946. opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2U);
  947. }
  948. static void opj_t1_dec_refpass_mqc(
  949. opj_t1_t *t1,
  950. OPJ_INT32 bpno)
  951. {
  952. if (t1->w == 64 && t1->h == 64) {
  953. opj_t1_dec_refpass_mqc_64x64(t1, bpno);
  954. } else {
  955. opj_t1_dec_refpass_mqc_generic(t1, bpno);
  956. }
  957. }
  958. /**
  959. Encode clean-up pass step
  960. */
  961. #define opj_t1_enc_clnpass_step_macro(mqc, curctx, a, c, ct, flagspIn, datapIn, bpno, one, nmsedec, agg, runlen, lim, cblksty) \
  962. { \
  963. OPJ_UINT32 v; \
  964. OPJ_UINT32 ci; \
  965. opj_flag_t* const flagsp = (flagspIn); \
  966. const OPJ_INT32* l_datap = (datapIn); \
  967. const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 | \
  968. T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
  969. \
  970. if ((*flagsp & check) == check) { \
  971. if (runlen == 0) { \
  972. *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
  973. } else if (runlen == 1) { \
  974. *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3); \
  975. } else if (runlen == 2) { \
  976. *flagsp &= ~(T1_PI_2 | T1_PI_3); \
  977. } else if (runlen == 3) { \
  978. *flagsp &= ~(T1_PI_3); \
  979. } \
  980. } \
  981. else \
  982. for (ci = runlen; ci < lim; ++ci) { \
  983. OPJ_BOOL goto_PARTIAL = OPJ_FALSE; \
  984. if ((agg != 0) && (ci == runlen)) { \
  985. goto_PARTIAL = OPJ_TRUE; \
  986. } \
  987. else if (!(*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) { \
  988. OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, *flagsp >> (ci * 3U)); \
  989. /* #ifdef DEBUG_ENC_CLN */ \
  990. /* printf(" ctxt1=%d\n", ctxt1); */ \
  991. /* #endif */ \
  992. opj_t1_setcurctx(curctx, ctxt1); \
  993. v = (opj_smr_abs(*l_datap) & (OPJ_UINT32)one) ? 1 : 0; \
  994. opj_mqc_encode_macro(mqc, curctx, a, c, ct, v); \
  995. if (v) { \
  996. goto_PARTIAL = OPJ_TRUE; \
  997. } \
  998. } \
  999. if( goto_PARTIAL ) { \
  1000. OPJ_UINT32 vsc; \
  1001. OPJ_UINT32 ctxt2, spb; \
  1002. OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
  1003. *flagsp, \
  1004. flagsp[-1], flagsp[1], \
  1005. ci); \
  1006. *nmsedec += opj_t1_getnmsedec_sig(opj_smr_abs(*l_datap), \
  1007. (OPJ_UINT32)bpno); \
  1008. ctxt2 = opj_t1_getctxno_sc(lu); \
  1009. /* #ifdef DEBUG_ENC_CLN */ \
  1010. /* printf(" ctxt2=%d\n", ctxt2); */ \
  1011. /* #endif */ \
  1012. opj_t1_setcurctx(curctx, ctxt2); \
  1013. \
  1014. v = opj_smr_sign(*l_datap); \
  1015. spb = opj_t1_getspb(lu); \
  1016. /* #ifdef DEBUG_ENC_CLN */ \
  1017. /* printf(" spb=%d\n", spb); */\
  1018. /* #endif */ \
  1019. opj_mqc_encode_macro(mqc, curctx, a, c, ct, v ^ spb); \
  1020. vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == 0)) ? 1 : 0; \
  1021. opj_t1_update_flags(flagsp, ci, v, t1->w + 2U, vsc); \
  1022. } \
  1023. *flagsp &= ~(T1_PI_THIS << (3U * ci)); \
  1024. l_datap ++; \
  1025. } \
  1026. }
  1027. #define opj_t1_dec_clnpass_step_macro(check_flags, partial, \
  1028. flags, flagsp, flags_stride, data, \
  1029. data_stride, ci, mqc, curctx, \
  1030. v, a, c, ct, oneplushalf, vsc) \
  1031. { \
  1032. if ( !check_flags || !(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {\
  1033. do { \
  1034. if( !partial ) { \
  1035. OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
  1036. opj_t1_setcurctx(curctx, ctxt1); \
  1037. opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
  1038. if( !v ) \
  1039. break; \
  1040. } \
  1041. { \
  1042. OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
  1043. flags, flagsp[-1], flagsp[1], \
  1044. ci); \
  1045. opj_t1_setcurctx(curctx, opj_t1_getctxno_sc(lu)); \
  1046. opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
  1047. v = v ^ opj_t1_getspb(lu); \
  1048. data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \
  1049. opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride, vsc); \
  1050. } \
  1051. } while(0); \
  1052. } \
  1053. }
  1054. static void opj_t1_dec_clnpass_step(
  1055. opj_t1_t *t1,
  1056. opj_flag_t *flagsp,
  1057. OPJ_INT32 *datap,
  1058. OPJ_INT32 oneplushalf,
  1059. OPJ_UINT32 ci,
  1060. OPJ_UINT32 vsc)
  1061. {
  1062. OPJ_UINT32 v;
  1063. opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
  1064. opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE,
  1065. *flagsp, flagsp, t1->w + 2U, datap,
  1066. 0, ci, mqc, mqc->curctx,
  1067. v, mqc->a, mqc->c, mqc->ct, oneplushalf, vsc);
  1068. }
  1069. static void opj_t1_enc_clnpass(
  1070. opj_t1_t *t1,
  1071. OPJ_INT32 bpno,
  1072. OPJ_INT32 *nmsedec,
  1073. OPJ_UINT32 cblksty)
  1074. {
  1075. OPJ_UINT32 i, k;
  1076. const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
  1077. opj_mqc_t* mqc = &(t1->mqc);
  1078. DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct);
  1079. const OPJ_INT32* datap = t1->data;
  1080. opj_flag_t *f = &T1_FLAGS(0, 0);
  1081. const OPJ_UINT32 extra = 2U;
  1082. *nmsedec = 0;
  1083. #ifdef DEBUG_ENC_CLN
  1084. printf("enc_clnpass: bpno=%d\n", bpno);
  1085. #endif
  1086. for (k = 0; k < (t1->h & ~3U); k += 4, f += extra) {
  1087. #ifdef DEBUG_ENC_CLN
  1088. printf(" k=%d\n", k);
  1089. #endif
  1090. for (i = 0; i < t1->w; ++i, f++) {
  1091. OPJ_UINT32 agg, runlen;
  1092. #ifdef DEBUG_ENC_CLN
  1093. printf(" i=%d\n", i);
  1094. #endif
  1095. agg = !*f;
  1096. #ifdef DEBUG_ENC_CLN
  1097. printf(" agg=%d\n", agg);
  1098. #endif
  1099. if (agg) {
  1100. for (runlen = 0; runlen < 4; ++runlen, ++datap) {
  1101. if (opj_smr_abs(*datap) & (OPJ_UINT32)one) {
  1102. break;
  1103. }
  1104. }
  1105. opj_t1_setcurctx(curctx, T1_CTXNO_AGG);
  1106. opj_mqc_encode_macro(mqc, curctx, a, c, ct, runlen != 4);
  1107. if (runlen == 4) {
  1108. continue;
  1109. }
  1110. opj_t1_setcurctx(curctx, T1_CTXNO_UNI);
  1111. opj_mqc_encode_macro(mqc, curctx, a, c, ct, runlen >> 1);
  1112. opj_mqc_encode_macro(mqc, curctx, a, c, ct, runlen & 1);
  1113. } else {
  1114. runlen = 0;
  1115. }
  1116. opj_t1_enc_clnpass_step_macro(
  1117. mqc, curctx, a, c, ct,
  1118. f,
  1119. datap,
  1120. bpno,
  1121. one,
  1122. nmsedec,
  1123. agg,
  1124. runlen,
  1125. 4U,
  1126. cblksty);
  1127. datap += 4 - runlen;
  1128. }
  1129. }
  1130. if (k < t1->h) {
  1131. const OPJ_UINT32 agg = 0;
  1132. const OPJ_UINT32 runlen = 0;
  1133. #ifdef DEBUG_ENC_CLN
  1134. printf(" k=%d\n", k);
  1135. #endif
  1136. for (i = 0; i < t1->w; ++i, f++) {
  1137. #ifdef DEBUG_ENC_CLN
  1138. printf(" i=%d\n", i);
  1139. printf(" agg=%d\n", agg);
  1140. #endif
  1141. opj_t1_enc_clnpass_step_macro(
  1142. mqc, curctx, a, c, ct,
  1143. f,
  1144. datap,
  1145. bpno,
  1146. one,
  1147. nmsedec,
  1148. agg,
  1149. runlen,
  1150. t1->h - k,
  1151. cblksty);
  1152. datap += t1->h - k;
  1153. }
  1154. }
  1155. UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct);
  1156. }
  1157. #define opj_t1_dec_clnpass_internal(t1, bpno, vsc, w, h, flags_stride) \
  1158. { \
  1159. OPJ_INT32 one, half, oneplushalf; \
  1160. OPJ_UINT32 runlen; \
  1161. OPJ_UINT32 i, j, k; \
  1162. const OPJ_UINT32 l_w = w; \
  1163. opj_mqc_t* mqc = &(t1->mqc); \
  1164. register OPJ_INT32 *data = t1->data; \
  1165. register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
  1166. DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \
  1167. register OPJ_UINT32 v; \
  1168. one = 1 << bpno; \
  1169. half = one >> 1; \
  1170. oneplushalf = one | half; \
  1171. for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
  1172. for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
  1173. opj_flag_t flags = *flagsp; \
  1174. if (flags == 0) { \
  1175. OPJ_UINT32 partial = OPJ_TRUE; \
  1176. opj_t1_setcurctx(curctx, T1_CTXNO_AGG); \
  1177. opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
  1178. if (!v) { \
  1179. continue; \
  1180. } \
  1181. opj_t1_setcurctx(curctx, T1_CTXNO_UNI); \
  1182. opj_mqc_decode_macro(runlen, mqc, curctx, a, c, ct); \
  1183. opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
  1184. runlen = (runlen << 1) | v; \
  1185. switch(runlen) { \
  1186. case 0: \
  1187. opj_t1_dec_clnpass_step_macro(OPJ_FALSE, OPJ_TRUE,\
  1188. flags, flagsp, flags_stride, data, \
  1189. l_w, 0, mqc, curctx, \
  1190. v, a, c, ct, oneplushalf, vsc); \
  1191. partial = OPJ_FALSE; \
  1192. /* FALLTHRU */ \
  1193. case 1: \
  1194. opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
  1195. flags, flagsp, flags_stride, data, \
  1196. l_w, 1, mqc, curctx, \
  1197. v, a, c, ct, oneplushalf, OPJ_FALSE); \
  1198. partial = OPJ_FALSE; \
  1199. /* FALLTHRU */ \
  1200. case 2: \
  1201. opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
  1202. flags, flagsp, flags_stride, data, \
  1203. l_w, 2, mqc, curctx, \
  1204. v, a, c, ct, oneplushalf, OPJ_FALSE); \
  1205. partial = OPJ_FALSE; \
  1206. /* FALLTHRU */ \
  1207. case 3: \
  1208. opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
  1209. flags, flagsp, flags_stride, data, \
  1210. l_w, 3, mqc, curctx, \
  1211. v, a, c, ct, oneplushalf, OPJ_FALSE); \
  1212. break; \
  1213. } \
  1214. } else { \
  1215. opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
  1216. flags, flagsp, flags_stride, data, \
  1217. l_w, 0, mqc, curctx, \
  1218. v, a, c, ct, oneplushalf, vsc); \
  1219. opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
  1220. flags, flagsp, flags_stride, data, \
  1221. l_w, 1, mqc, curctx, \
  1222. v, a, c, ct, oneplushalf, OPJ_FALSE); \
  1223. opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
  1224. flags, flagsp, flags_stride, data, \
  1225. l_w, 2, mqc, curctx, \
  1226. v, a, c, ct, oneplushalf, OPJ_FALSE); \
  1227. opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
  1228. flags, flagsp, flags_stride, data, \
  1229. l_w, 3, mqc, curctx, \
  1230. v, a, c, ct, oneplushalf, OPJ_FALSE); \
  1231. } \
  1232. *flagsp = flags & ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
  1233. } \
  1234. } \
  1235. UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \
  1236. if( k < h ) { \
  1237. for (i = 0; i < l_w; ++i, ++flagsp, ++data) { \
  1238. for (j = 0; j < h - k; ++j) { \
  1239. opj_t1_dec_clnpass_step(t1, flagsp, data + j * l_w, oneplushalf, j, vsc); \
  1240. } \
  1241. *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
  1242. } \
  1243. } \
  1244. }
  1245. static void opj_t1_dec_clnpass_check_segsym(opj_t1_t *t1, OPJ_INT32 cblksty)
  1246. {
  1247. if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
  1248. opj_mqc_t* mqc = &(t1->mqc);
  1249. OPJ_UINT32 v, v2;
  1250. opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
  1251. opj_mqc_decode(v, mqc);
  1252. opj_mqc_decode(v2, mqc);
  1253. v = (v << 1) | v2;
  1254. opj_mqc_decode(v2, mqc);
  1255. v = (v << 1) | v2;
  1256. opj_mqc_decode(v2, mqc);
  1257. v = (v << 1) | v2;
  1258. /*
  1259. if (v!=0xa) {
  1260. opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
  1261. }
  1262. */
  1263. }
  1264. }
  1265. static void opj_t1_dec_clnpass_64x64_novsc(
  1266. opj_t1_t *t1,
  1267. OPJ_INT32 bpno)
  1268. {
  1269. opj_t1_dec_clnpass_internal(t1, bpno, OPJ_FALSE, 64, 64, 66);
  1270. }
  1271. static void opj_t1_dec_clnpass_64x64_vsc(
  1272. opj_t1_t *t1,
  1273. OPJ_INT32 bpno)
  1274. {
  1275. opj_t1_dec_clnpass_internal(t1, bpno, OPJ_TRUE, 64, 64, 66);
  1276. }
  1277. static void opj_t1_dec_clnpass_generic_novsc(
  1278. opj_t1_t *t1,
  1279. OPJ_INT32 bpno)
  1280. {
  1281. opj_t1_dec_clnpass_internal(t1, bpno, OPJ_FALSE, t1->w, t1->h,
  1282. t1->w + 2U);
  1283. }
  1284. static void opj_t1_dec_clnpass_generic_vsc(
  1285. opj_t1_t *t1,
  1286. OPJ_INT32 bpno)
  1287. {
  1288. opj_t1_dec_clnpass_internal(t1, bpno, OPJ_TRUE, t1->w, t1->h,
  1289. t1->w + 2U);
  1290. }
  1291. static void opj_t1_dec_clnpass(
  1292. opj_t1_t *t1,
  1293. OPJ_INT32 bpno,
  1294. OPJ_INT32 cblksty)
  1295. {
  1296. if (t1->w == 64 && t1->h == 64) {
  1297. if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  1298. opj_t1_dec_clnpass_64x64_vsc(t1, bpno);
  1299. } else {
  1300. opj_t1_dec_clnpass_64x64_novsc(t1, bpno);
  1301. }
  1302. } else {
  1303. if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  1304. opj_t1_dec_clnpass_generic_vsc(t1, bpno);
  1305. } else {
  1306. opj_t1_dec_clnpass_generic_novsc(t1, bpno);
  1307. }
  1308. }
  1309. opj_t1_dec_clnpass_check_segsym(t1, cblksty);
  1310. }
  1311. /** mod fixed_quality */
  1312. static OPJ_FLOAT64 opj_t1_getwmsedec(
  1313. OPJ_INT32 nmsedec,
  1314. OPJ_UINT32 compno,
  1315. OPJ_UINT32 level,
  1316. OPJ_UINT32 orient,
  1317. OPJ_INT32 bpno,
  1318. OPJ_UINT32 qmfbid,
  1319. OPJ_FLOAT64 stepsize,
  1320. OPJ_UINT32 numcomps,
  1321. const OPJ_FLOAT64 * mct_norms,
  1322. OPJ_UINT32 mct_numcomps)
  1323. {
  1324. OPJ_FLOAT64 w1 = 1, w2, wmsedec;
  1325. OPJ_ARG_NOT_USED(numcomps);
  1326. if (mct_norms && (compno < mct_numcomps)) {
  1327. w1 = mct_norms[compno];
  1328. }
  1329. if (qmfbid == 1) {
  1330. w2 = opj_dwt_getnorm(level, orient);
  1331. } else { /* if (qmfbid == 0) */
  1332. const OPJ_INT32 log2_gain = (orient == 0) ? 0 :
  1333. (orient == 3) ? 2 : 1;
  1334. w2 = opj_dwt_getnorm_real(level, orient);
  1335. /* Not sure this is right. But preserves past behaviour */
  1336. stepsize /= (1 << log2_gain);
  1337. }
  1338. wmsedec = w1 * w2 * stepsize * (1 << bpno);
  1339. wmsedec *= wmsedec * nmsedec / 8192.0;
  1340. return wmsedec;
  1341. }
  1342. static OPJ_BOOL opj_t1_allocate_buffers(
  1343. opj_t1_t *t1,
  1344. OPJ_UINT32 w,
  1345. OPJ_UINT32 h)
  1346. {
  1347. OPJ_UINT32 flagssize;
  1348. OPJ_UINT32 flags_stride;
  1349. /* No risk of overflow. Prior checks ensure those assert are met */
  1350. /* They are per the specification */
  1351. assert(w <= 1024);
  1352. assert(h <= 1024);
  1353. assert(w * h <= 4096);
  1354. /* encoder uses tile buffer, so no need to allocate */
  1355. {
  1356. OPJ_UINT32 datasize = w * h;
  1357. if (datasize > t1->datasize) {
  1358. opj_aligned_free(t1->data);
  1359. t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
  1360. if (!t1->data) {
  1361. /* FIXME event manager error callback */
  1362. return OPJ_FALSE;
  1363. }
  1364. t1->datasize = datasize;
  1365. }
  1366. /* memset first arg is declared to never be null by gcc */
  1367. if (t1->data != NULL) {
  1368. memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
  1369. }
  1370. }
  1371. flags_stride = w + 2U; /* can't be 0U */
  1372. flagssize = (h + 3U) / 4U + 2U;
  1373. flagssize *= flags_stride;
  1374. {
  1375. opj_flag_t* p;
  1376. OPJ_UINT32 x;
  1377. OPJ_UINT32 flags_height = (h + 3U) / 4U;
  1378. if (flagssize > t1->flagssize) {
  1379. opj_aligned_free(t1->flags);
  1380. t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(
  1381. opj_flag_t));
  1382. if (!t1->flags) {
  1383. /* FIXME event manager error callback */
  1384. return OPJ_FALSE;
  1385. }
  1386. }
  1387. t1->flagssize = flagssize;
  1388. memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
  1389. p = &t1->flags[0];
  1390. for (x = 0; x < flags_stride; ++x) {
  1391. /* magic value to hopefully stop any passes being interested in this entry */
  1392. *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
  1393. }
  1394. p = &t1->flags[((flags_height + 1) * flags_stride)];
  1395. for (x = 0; x < flags_stride; ++x) {
  1396. /* magic value to hopefully stop any passes being interested in this entry */
  1397. *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
  1398. }
  1399. if (h % 4) {
  1400. OPJ_UINT32 v = 0;
  1401. p = &t1->flags[((flags_height) * flags_stride)];
  1402. if (h % 4 == 1) {
  1403. v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
  1404. } else if (h % 4 == 2) {
  1405. v |= T1_PI_2 | T1_PI_3;
  1406. } else if (h % 4 == 3) {
  1407. v |= T1_PI_3;
  1408. }
  1409. for (x = 0; x < flags_stride; ++x) {
  1410. *p++ = v;
  1411. }
  1412. }
  1413. }
  1414. t1->w = w;
  1415. t1->h = h;
  1416. return OPJ_TRUE;
  1417. }
  1418. /* ----------------------------------------------------------------------- */
  1419. /* ----------------------------------------------------------------------- */
  1420. /**
  1421. * Creates a new Tier 1 handle
  1422. * and initializes the look-up tables of the Tier-1 coder/decoder
  1423. * @return a new T1 handle if successful, returns NULL otherwise
  1424. */
  1425. opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
  1426. {
  1427. opj_t1_t *l_t1 = 00;
  1428. l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
  1429. if (!l_t1) {
  1430. return 00;
  1431. }
  1432. l_t1->encoder = isEncoder;
  1433. return l_t1;
  1434. }
  1435. /**
  1436. * Destroys a previously created T1 handle
  1437. *
  1438. * @param p_t1 Tier 1 handle to destroy
  1439. */
  1440. void opj_t1_destroy(opj_t1_t *p_t1)
  1441. {
  1442. if (! p_t1) {
  1443. return;
  1444. }
  1445. if (p_t1->data) {
  1446. opj_aligned_free(p_t1->data);
  1447. p_t1->data = 00;
  1448. }
  1449. if (p_t1->flags) {
  1450. opj_aligned_free(p_t1->flags);
  1451. p_t1->flags = 00;
  1452. }
  1453. opj_free(p_t1->cblkdatabuffer);
  1454. opj_free(p_t1);
  1455. }
  1456. typedef struct {
  1457. OPJ_BOOL whole_tile_decoding;
  1458. OPJ_UINT32 resno;
  1459. opj_tcd_cblk_dec_t* cblk;
  1460. opj_tcd_band_t* band;
  1461. opj_tcd_tilecomp_t* tilec;
  1462. opj_tccp_t* tccp;
  1463. OPJ_BOOL mustuse_cblkdatabuffer;
  1464. volatile OPJ_BOOL* pret;
  1465. opj_event_mgr_t *p_manager;
  1466. opj_mutex_t* p_manager_mutex;
  1467. OPJ_BOOL check_pterm;
  1468. } opj_t1_cblk_decode_processing_job_t;
  1469. static void opj_t1_destroy_wrapper(void* t1)
  1470. {
  1471. opj_t1_destroy((opj_t1_t*) t1);
  1472. }
  1473. static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
  1474. {
  1475. opj_tcd_cblk_dec_t* cblk;
  1476. opj_tcd_band_t* band;
  1477. opj_tcd_tilecomp_t* tilec;
  1478. opj_tccp_t* tccp;
  1479. OPJ_INT32* OPJ_RESTRICT datap;
  1480. OPJ_UINT32 cblk_w, cblk_h;
  1481. OPJ_INT32 x, y;
  1482. OPJ_UINT32 i, j;
  1483. opj_t1_cblk_decode_processing_job_t* job;
  1484. opj_t1_t* t1;
  1485. OPJ_UINT32 resno;
  1486. OPJ_UINT32 tile_w;
  1487. job = (opj_t1_cblk_decode_processing_job_t*) user_data;
  1488. cblk = job->cblk;
  1489. if (!job->whole_tile_decoding) {
  1490. cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
  1491. cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
  1492. cblk->decoded_data = (OPJ_INT32*)opj_aligned_malloc(sizeof(OPJ_INT32) *
  1493. cblk_w * cblk_h);
  1494. if (cblk->decoded_data == NULL) {
  1495. if (job->p_manager_mutex) {
  1496. opj_mutex_lock(job->p_manager_mutex);
  1497. }
  1498. opj_event_msg(job->p_manager, EVT_ERROR,
  1499. "Cannot allocate cblk->decoded_data\n");
  1500. if (job->p_manager_mutex) {
  1501. opj_mutex_unlock(job->p_manager_mutex);
  1502. }
  1503. *(job->pret) = OPJ_FALSE;
  1504. opj_free(job);
  1505. return;
  1506. }
  1507. /* Zero-init required */
  1508. memset(cblk->decoded_data, 0, sizeof(OPJ_INT32) * cblk_w * cblk_h);
  1509. } else if (cblk->decoded_data) {
  1510. /* Not sure if that code path can happen, but better be */
  1511. /* safe than sorry */
  1512. opj_aligned_free(cblk->decoded_data);
  1513. cblk->decoded_data = NULL;
  1514. }
  1515. resno = job->resno;
  1516. band = job->band;
  1517. tilec = job->tilec;
  1518. tccp = job->tccp;
  1519. tile_w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions - 1].x1
  1520. -
  1521. tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
  1522. if (!*(job->pret)) {
  1523. opj_free(job);
  1524. return;
  1525. }
  1526. t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
  1527. if (t1 == NULL) {
  1528. t1 = opj_t1_create(OPJ_FALSE);
  1529. if (t1 == NULL) {
  1530. opj_event_msg(job->p_manager, EVT_ERROR,
  1531. "Cannot allocate Tier 1 handle\n");
  1532. *(job->pret) = OPJ_FALSE;
  1533. opj_free(job);
  1534. return;
  1535. }
  1536. if (!opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper)) {
  1537. opj_event_msg(job->p_manager, EVT_ERROR,
  1538. "Unable to set t1 handle as TLS\n");
  1539. opj_t1_destroy(t1);
  1540. *(job->pret) = OPJ_FALSE;
  1541. opj_free(job);
  1542. return;
  1543. }
  1544. }
  1545. t1->mustuse_cblkdatabuffer = job->mustuse_cblkdatabuffer;
  1546. if ((tccp->cblksty & J2K_CCP_CBLKSTY_HT) != 0) {
  1547. if (OPJ_FALSE == opj_t1_ht_decode_cblk(
  1548. t1,
  1549. cblk,
  1550. band->bandno,
  1551. (OPJ_UINT32)tccp->roishift,
  1552. tccp->cblksty,
  1553. job->p_manager,
  1554. job->p_manager_mutex,
  1555. job->check_pterm)) {
  1556. *(job->pret) = OPJ_FALSE;
  1557. opj_free(job);
  1558. return;
  1559. }
  1560. } else {
  1561. if (OPJ_FALSE == opj_t1_decode_cblk(
  1562. t1,
  1563. cblk,
  1564. band->bandno,
  1565. (OPJ_UINT32)tccp->roishift,
  1566. tccp->cblksty,
  1567. job->p_manager,
  1568. job->p_manager_mutex,
  1569. job->check_pterm)) {
  1570. *(job->pret) = OPJ_FALSE;
  1571. opj_free(job);
  1572. return;
  1573. }
  1574. }
  1575. x = cblk->x0 - band->x0;
  1576. y = cblk->y0 - band->y0;
  1577. if (band->bandno & 1) {
  1578. opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
  1579. x += pres->x1 - pres->x0;
  1580. }
  1581. if (band->bandno & 2) {
  1582. opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
  1583. y += pres->y1 - pres->y0;
  1584. }
  1585. datap = cblk->decoded_data ? cblk->decoded_data : t1->data;
  1586. cblk_w = t1->w;
  1587. cblk_h = t1->h;
  1588. if (tccp->roishift) {
  1589. if (tccp->roishift >= 31) {
  1590. for (j = 0; j < cblk_h; ++j) {
  1591. for (i = 0; i < cblk_w; ++i) {
  1592. datap[(j * cblk_w) + i] = 0;
  1593. }
  1594. }
  1595. } else {
  1596. OPJ_INT32 thresh = 1 << tccp->roishift;
  1597. for (j = 0; j < cblk_h; ++j) {
  1598. for (i = 0; i < cblk_w; ++i) {
  1599. OPJ_INT32 val = datap[(j * cblk_w) + i];
  1600. OPJ_INT32 mag = abs(val);
  1601. if (mag >= thresh) {
  1602. mag >>= tccp->roishift;
  1603. datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
  1604. }
  1605. }
  1606. }
  1607. }
  1608. }
  1609. /* Both can be non NULL if for example decoding a full tile and then */
  1610. /* partially a tile. In which case partial decoding should be the */
  1611. /* priority */
  1612. assert((cblk->decoded_data != NULL) || (tilec->data != NULL));
  1613. if (cblk->decoded_data) {
  1614. OPJ_UINT32 cblk_size = cblk_w * cblk_h;
  1615. if (tccp->qmfbid == 1) {
  1616. for (i = 0; i < cblk_size; ++i) {
  1617. datap[i] /= 2;
  1618. }
  1619. } else { /* if (tccp->qmfbid == 0) */
  1620. const float stepsize = 0.5f * band->stepsize;
  1621. i = 0;
  1622. #ifdef __SSE2__
  1623. {
  1624. const __m128 xmm_stepsize = _mm_set1_ps(stepsize);
  1625. for (; i < (cblk_size & ~15U); i += 16) {
  1626. __m128 xmm0_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)(
  1627. datap + 0)));
  1628. __m128 xmm1_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)(
  1629. datap + 4)));
  1630. __m128 xmm2_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)(
  1631. datap + 8)));
  1632. __m128 xmm3_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)(
  1633. datap + 12)));
  1634. _mm_store_ps((float*)(datap + 0), _mm_mul_ps(xmm0_data, xmm_stepsize));
  1635. _mm_store_ps((float*)(datap + 4), _mm_mul_ps(xmm1_data, xmm_stepsize));
  1636. _mm_store_ps((float*)(datap + 8), _mm_mul_ps(xmm2_data, xmm_stepsize));
  1637. _mm_store_ps((float*)(datap + 12), _mm_mul_ps(xmm3_data, xmm_stepsize));
  1638. datap += 16;
  1639. }
  1640. }
  1641. #endif
  1642. for (; i < cblk_size; ++i) {
  1643. OPJ_FLOAT32 tmp = ((OPJ_FLOAT32)(*datap)) * stepsize;
  1644. memcpy(datap, &tmp, sizeof(tmp));
  1645. datap++;
  1646. }
  1647. }
  1648. } else if (tccp->qmfbid == 1) {
  1649. OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_SIZE_T)y * tile_w +
  1650. (OPJ_SIZE_T)x];
  1651. for (j = 0; j < cblk_h; ++j) {
  1652. i = 0;
  1653. for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
  1654. OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
  1655. OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
  1656. OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
  1657. OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
  1658. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 0U] = tmp0 / 2;
  1659. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 1U] = tmp1 / 2;
  1660. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 2U] = tmp2 / 2;
  1661. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 3U] = tmp3 / 2;
  1662. }
  1663. for (; i < cblk_w; ++i) {
  1664. OPJ_INT32 tmp = datap[(j * cblk_w) + i];
  1665. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i] = tmp / 2;
  1666. }
  1667. }
  1668. } else { /* if (tccp->qmfbid == 0) */
  1669. const float stepsize = 0.5f * band->stepsize;
  1670. OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_SIZE_T)y *
  1671. tile_w + (OPJ_SIZE_T)x];
  1672. for (j = 0; j < cblk_h; ++j) {
  1673. OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
  1674. for (i = 0; i < cblk_w; ++i) {
  1675. OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * stepsize;
  1676. *tiledp2 = tmp;
  1677. datap++;
  1678. tiledp2++;
  1679. }
  1680. tiledp += tile_w;
  1681. }
  1682. }
  1683. opj_free(job);
  1684. }
  1685. void opj_t1_decode_cblks(opj_tcd_t* tcd,
  1686. volatile OPJ_BOOL* pret,
  1687. opj_tcd_tilecomp_t* tilec,
  1688. opj_tccp_t* tccp,
  1689. opj_event_mgr_t *p_manager,
  1690. opj_mutex_t* p_manager_mutex,
  1691. OPJ_BOOL check_pterm
  1692. )
  1693. {
  1694. opj_thread_pool_t* tp = tcd->thread_pool;
  1695. OPJ_UINT32 resno, bandno, precno, cblkno;
  1696. #ifdef DEBUG_VERBOSE
  1697. OPJ_UINT32 codeblocks_decoded = 0;
  1698. printf("Enter opj_t1_decode_cblks()\n");
  1699. #endif
  1700. for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
  1701. opj_tcd_resolution_t* res = &tilec->resolutions[resno];
  1702. for (bandno = 0; bandno < res->numbands; ++bandno) {
  1703. opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
  1704. for (precno = 0; precno < res->pw * res->ph; ++precno) {
  1705. opj_tcd_precinct_t* precinct = &band->precincts[precno];
  1706. if (!opj_tcd_is_subband_area_of_interest(tcd,
  1707. tilec->compno,
  1708. resno,
  1709. band->bandno,
  1710. (OPJ_UINT32)precinct->x0,
  1711. (OPJ_UINT32)precinct->y0,
  1712. (OPJ_UINT32)precinct->x1,
  1713. (OPJ_UINT32)precinct->y1)) {
  1714. for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
  1715. opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
  1716. if (cblk->decoded_data) {
  1717. #ifdef DEBUG_VERBOSE
  1718. printf("Discarding codeblock %d,%d at resno=%d, bandno=%d\n",
  1719. cblk->x0, cblk->y0, resno, bandno);
  1720. #endif
  1721. opj_aligned_free(cblk->decoded_data);
  1722. cblk->decoded_data = NULL;
  1723. }
  1724. }
  1725. continue;
  1726. }
  1727. for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
  1728. opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
  1729. opj_t1_cblk_decode_processing_job_t* job;
  1730. if (!opj_tcd_is_subband_area_of_interest(tcd,
  1731. tilec->compno,
  1732. resno,
  1733. band->bandno,
  1734. (OPJ_UINT32)cblk->x0,
  1735. (OPJ_UINT32)cblk->y0,
  1736. (OPJ_UINT32)cblk->x1,
  1737. (OPJ_UINT32)cblk->y1)) {
  1738. if (cblk->decoded_data) {
  1739. #ifdef DEBUG_VERBOSE
  1740. printf("Discarding codeblock %d,%d at resno=%d, bandno=%d\n",
  1741. cblk->x0, cblk->y0, resno, bandno);
  1742. #endif
  1743. opj_aligned_free(cblk->decoded_data);
  1744. cblk->decoded_data = NULL;
  1745. }
  1746. continue;
  1747. }
  1748. if (!tcd->whole_tile_decoding) {
  1749. OPJ_UINT32 cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
  1750. OPJ_UINT32 cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
  1751. if (cblk->decoded_data != NULL) {
  1752. #ifdef DEBUG_VERBOSE
  1753. printf("Reusing codeblock %d,%d at resno=%d, bandno=%d\n",
  1754. cblk->x0, cblk->y0, resno, bandno);
  1755. #endif
  1756. continue;
  1757. }
  1758. if (cblk_w == 0 || cblk_h == 0) {
  1759. continue;
  1760. }
  1761. #ifdef DEBUG_VERBOSE
  1762. printf("Decoding codeblock %d,%d at resno=%d, bandno=%d\n",
  1763. cblk->x0, cblk->y0, resno, bandno);
  1764. #endif
  1765. }
  1766. job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
  1767. sizeof(opj_t1_cblk_decode_processing_job_t));
  1768. if (!job) {
  1769. *pret = OPJ_FALSE;
  1770. return;
  1771. }
  1772. job->whole_tile_decoding = tcd->whole_tile_decoding;
  1773. job->resno = resno;
  1774. job->cblk = cblk;
  1775. job->band = band;
  1776. job->tilec = tilec;
  1777. job->tccp = tccp;
  1778. job->pret = pret;
  1779. job->p_manager_mutex = p_manager_mutex;
  1780. job->p_manager = p_manager;
  1781. job->check_pterm = check_pterm;
  1782. job->mustuse_cblkdatabuffer = opj_thread_pool_get_thread_count(tp) > 1;
  1783. opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
  1784. #ifdef DEBUG_VERBOSE
  1785. codeblocks_decoded ++;
  1786. #endif
  1787. if (!(*pret)) {
  1788. return;
  1789. }
  1790. } /* cblkno */
  1791. } /* precno */
  1792. } /* bandno */
  1793. } /* resno */
  1794. #ifdef DEBUG_VERBOSE
  1795. printf("Leave opj_t1_decode_cblks(). Number decoded: %d\n", codeblocks_decoded);
  1796. #endif
  1797. return;
  1798. }
  1799. static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
  1800. opj_tcd_cblk_dec_t* cblk,
  1801. OPJ_UINT32 orient,
  1802. OPJ_UINT32 roishift,
  1803. OPJ_UINT32 cblksty,
  1804. opj_event_mgr_t *p_manager,
  1805. opj_mutex_t* p_manager_mutex,
  1806. OPJ_BOOL check_pterm)
  1807. {
  1808. opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
  1809. OPJ_INT32 bpno_plus_one;
  1810. OPJ_UINT32 passtype;
  1811. OPJ_UINT32 segno, passno;
  1812. OPJ_BYTE* cblkdata = NULL;
  1813. OPJ_UINT32 cblkdataindex = 0;
  1814. OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
  1815. OPJ_INT32* original_t1_data = NULL;
  1816. mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
  1817. if (!opj_t1_allocate_buffers(
  1818. t1,
  1819. (OPJ_UINT32)(cblk->x1 - cblk->x0),
  1820. (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
  1821. return OPJ_FALSE;
  1822. }
  1823. bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
  1824. if (bpno_plus_one >= 31) {
  1825. if (p_manager_mutex) {
  1826. opj_mutex_lock(p_manager_mutex);
  1827. }
  1828. opj_event_msg(p_manager, EVT_WARNING,
  1829. "opj_t1_decode_cblk(): unsupported bpno_plus_one = %d >= 31\n",
  1830. bpno_plus_one);
  1831. if (p_manager_mutex) {
  1832. opj_mutex_unlock(p_manager_mutex);
  1833. }
  1834. return OPJ_FALSE;
  1835. }
  1836. passtype = 2;
  1837. opj_mqc_resetstates(mqc);
  1838. opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  1839. opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  1840. opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  1841. /* Even if we have a single chunk, in multi-threaded decoding */
  1842. /* the insertion of our synthetic marker might potentially override */
  1843. /* valid codestream of other codeblocks decoded in parallel. */
  1844. if (cblk->numchunks > 1 || t1->mustuse_cblkdatabuffer) {
  1845. OPJ_UINT32 i;
  1846. OPJ_UINT32 cblk_len;
  1847. /* Compute whole codeblock length from chunk lengths */
  1848. cblk_len = 0;
  1849. for (i = 0; i < cblk->numchunks; i++) {
  1850. cblk_len += cblk->chunks[i].len;
  1851. }
  1852. /* Allocate temporary memory if needed */
  1853. if (cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA > t1->cblkdatabuffersize) {
  1854. cblkdata = (OPJ_BYTE*)opj_realloc(t1->cblkdatabuffer,
  1855. cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA);
  1856. if (cblkdata == NULL) {
  1857. return OPJ_FALSE;
  1858. }
  1859. t1->cblkdatabuffer = cblkdata;
  1860. memset(t1->cblkdatabuffer + cblk_len, 0, OPJ_COMMON_CBLK_DATA_EXTRA);
  1861. t1->cblkdatabuffersize = cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA;
  1862. }
  1863. /* Concatenate all chunks */
  1864. cblkdata = t1->cblkdatabuffer;
  1865. cblk_len = 0;
  1866. for (i = 0; i < cblk->numchunks; i++) {
  1867. memcpy(cblkdata + cblk_len, cblk->chunks[i].data, cblk->chunks[i].len);
  1868. cblk_len += cblk->chunks[i].len;
  1869. }
  1870. } else if (cblk->numchunks == 1) {
  1871. cblkdata = cblk->chunks[0].data;
  1872. } else {
  1873. /* Not sure if that can happen in practice, but avoid Coverity to */
  1874. /* think we will dereference a null cblkdta pointer */
  1875. return OPJ_TRUE;
  1876. }
  1877. /* For subtile decoding, directly decode in the decoded_data buffer of */
  1878. /* the code-block. Hack t1->data to point to it, and restore it later */
  1879. if (cblk->decoded_data) {
  1880. original_t1_data = t1->data;
  1881. t1->data = cblk->decoded_data;
  1882. }
  1883. for (segno = 0; segno < cblk->real_num_segs; ++segno) {
  1884. opj_tcd_seg_t *seg = &cblk->segs[segno];
  1885. /* BYPASS mode */
  1886. type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
  1887. (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
  1888. if (type == T1_TYPE_RAW) {
  1889. opj_mqc_raw_init_dec(mqc, cblkdata + cblkdataindex, seg->len,
  1890. OPJ_COMMON_CBLK_DATA_EXTRA);
  1891. } else {
  1892. opj_mqc_init_dec(mqc, cblkdata + cblkdataindex, seg->len,
  1893. OPJ_COMMON_CBLK_DATA_EXTRA);
  1894. }
  1895. cblkdataindex += seg->len;
  1896. for (passno = 0; (passno < seg->real_num_passes) &&
  1897. (bpno_plus_one >= 1); ++passno) {
  1898. switch (passtype) {
  1899. case 0:
  1900. if (type == T1_TYPE_RAW) {
  1901. opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
  1902. } else {
  1903. opj_t1_dec_sigpass_mqc(t1, bpno_plus_one, (OPJ_INT32)cblksty);
  1904. }
  1905. break;
  1906. case 1:
  1907. if (type == T1_TYPE_RAW) {
  1908. opj_t1_dec_refpass_raw(t1, bpno_plus_one);
  1909. } else {
  1910. opj_t1_dec_refpass_mqc(t1, bpno_plus_one);
  1911. }
  1912. break;
  1913. case 2:
  1914. opj_t1_dec_clnpass(t1, bpno_plus_one, (OPJ_INT32)cblksty);
  1915. break;
  1916. }
  1917. if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
  1918. opj_mqc_resetstates(mqc);
  1919. opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  1920. opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  1921. opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  1922. }
  1923. if (++passtype == 3) {
  1924. passtype = 0;
  1925. bpno_plus_one--;
  1926. }
  1927. }
  1928. opq_mqc_finish_dec(mqc);
  1929. }
  1930. if (check_pterm) {
  1931. if (mqc->bp + 2 < mqc->end) {
  1932. if (p_manager_mutex) {
  1933. opj_mutex_lock(p_manager_mutex);
  1934. }
  1935. opj_event_msg(p_manager, EVT_WARNING,
  1936. "PTERM check failure: %d remaining bytes in code block (%d used / %d)\n",
  1937. (int)(mqc->end - mqc->bp) - 2,
  1938. (int)(mqc->bp - mqc->start),
  1939. (int)(mqc->end - mqc->start));
  1940. if (p_manager_mutex) {
  1941. opj_mutex_unlock(p_manager_mutex);
  1942. }
  1943. } else if (mqc->end_of_byte_stream_counter > 2) {
  1944. if (p_manager_mutex) {
  1945. opj_mutex_lock(p_manager_mutex);
  1946. }
  1947. opj_event_msg(p_manager, EVT_WARNING,
  1948. "PTERM check failure: %d synthetized 0xFF markers read\n",
  1949. mqc->end_of_byte_stream_counter);
  1950. if (p_manager_mutex) {
  1951. opj_mutex_unlock(p_manager_mutex);
  1952. }
  1953. }
  1954. }
  1955. /* Restore original t1->data is needed */
  1956. if (cblk->decoded_data) {
  1957. t1->data = original_t1_data;
  1958. }
  1959. return OPJ_TRUE;
  1960. }
  1961. typedef struct {
  1962. OPJ_UINT32 compno;
  1963. OPJ_UINT32 resno;
  1964. opj_tcd_cblk_enc_t* cblk;
  1965. opj_tcd_tile_t *tile;
  1966. opj_tcd_band_t* band;
  1967. opj_tcd_tilecomp_t* tilec;
  1968. opj_tccp_t* tccp;
  1969. const OPJ_FLOAT64 * mct_norms;
  1970. OPJ_UINT32 mct_numcomps;
  1971. volatile OPJ_BOOL* pret;
  1972. opj_mutex_t* mutex;
  1973. } opj_t1_cblk_encode_processing_job_t;
  1974. /** Procedure to deal with a asynchronous code-block encoding job.
  1975. *
  1976. * @param user_data Pointer to a opj_t1_cblk_encode_processing_job_t* structure
  1977. * @param tls TLS handle.
  1978. */
  1979. static void opj_t1_cblk_encode_processor(void* user_data, opj_tls_t* tls)
  1980. {
  1981. opj_t1_cblk_encode_processing_job_t* job =
  1982. (opj_t1_cblk_encode_processing_job_t*)user_data;
  1983. opj_tcd_cblk_enc_t* cblk = job->cblk;
  1984. const opj_tcd_band_t* band = job->band;
  1985. const opj_tcd_tilecomp_t* tilec = job->tilec;
  1986. const opj_tccp_t* tccp = job->tccp;
  1987. const OPJ_UINT32 resno = job->resno;
  1988. opj_t1_t* t1;
  1989. const OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
  1990. OPJ_INT32* OPJ_RESTRICT tiledp;
  1991. OPJ_UINT32 cblk_w;
  1992. OPJ_UINT32 cblk_h;
  1993. OPJ_UINT32 i, j;
  1994. OPJ_INT32 x = cblk->x0 - band->x0;
  1995. OPJ_INT32 y = cblk->y0 - band->y0;
  1996. if (!*(job->pret)) {
  1997. opj_free(job);
  1998. return;
  1999. }
  2000. t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
  2001. if (t1 == NULL) {
  2002. t1 = opj_t1_create(OPJ_TRUE); /* OPJ_TRUE == T1 for encoding */
  2003. opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
  2004. }
  2005. if (band->bandno & 1) {
  2006. opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
  2007. x += pres->x1 - pres->x0;
  2008. }
  2009. if (band->bandno & 2) {
  2010. opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
  2011. y += pres->y1 - pres->y0;
  2012. }
  2013. if (!opj_t1_allocate_buffers(
  2014. t1,
  2015. (OPJ_UINT32)(cblk->x1 - cblk->x0),
  2016. (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
  2017. *(job->pret) = OPJ_FALSE;
  2018. opj_free(job);
  2019. return;
  2020. }
  2021. cblk_w = t1->w;
  2022. cblk_h = t1->h;
  2023. tiledp = &tilec->data[(OPJ_SIZE_T)y * tile_w + (OPJ_SIZE_T)x];
  2024. if (tccp->qmfbid == 1) {
  2025. /* Do multiplication on unsigned type, even if the
  2026. * underlying type is signed, to avoid potential
  2027. * int overflow on large value (the output will be
  2028. * incorrect in such situation, but whatever...)
  2029. * This assumes complement-to-2 signed integer
  2030. * representation
  2031. * Fixes https://github.com/uclouvain/openjpeg/issues/1053
  2032. */
  2033. OPJ_UINT32* OPJ_RESTRICT tiledp_u = (OPJ_UINT32*) tiledp;
  2034. OPJ_UINT32* OPJ_RESTRICT t1data = (OPJ_UINT32*) t1->data;
  2035. /* Change from "natural" order to "zigzag" order of T1 passes */
  2036. for (j = 0; j < (cblk_h & ~3U); j += 4) {
  2037. for (i = 0; i < cblk_w; ++i) {
  2038. t1data[0] = tiledp_u[(j + 0) * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2039. t1data[1] = tiledp_u[(j + 1) * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2040. t1data[2] = tiledp_u[(j + 2) * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2041. t1data[3] = tiledp_u[(j + 3) * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2042. t1data += 4;
  2043. }
  2044. }
  2045. if (j < cblk_h) {
  2046. for (i = 0; i < cblk_w; ++i) {
  2047. OPJ_UINT32 k;
  2048. for (k = j; k < cblk_h; k++) {
  2049. t1data[0] = tiledp_u[k * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2050. t1data ++;
  2051. }
  2052. }
  2053. }
  2054. } else { /* if (tccp->qmfbid == 0) */
  2055. OPJ_FLOAT32* OPJ_RESTRICT tiledp_f = (OPJ_FLOAT32*) tiledp;
  2056. OPJ_INT32* OPJ_RESTRICT t1data = t1->data;
  2057. /* Change from "natural" order to "zigzag" order of T1 passes */
  2058. for (j = 0; j < (cblk_h & ~3U); j += 4) {
  2059. for (i = 0; i < cblk_w; ++i) {
  2060. t1data[0] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 0) * tile_w + i] /
  2061. band->stepsize) * (1 << T1_NMSEDEC_FRACBITS));
  2062. t1data[1] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 1) * tile_w + i] /
  2063. band->stepsize) * (1 << T1_NMSEDEC_FRACBITS));
  2064. t1data[2] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 2) * tile_w + i] /
  2065. band->stepsize) * (1 << T1_NMSEDEC_FRACBITS));
  2066. t1data[3] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 3) * tile_w + i] /
  2067. band->stepsize) * (1 << T1_NMSEDEC_FRACBITS));
  2068. t1data += 4;
  2069. }
  2070. }
  2071. if (j < cblk_h) {
  2072. for (i = 0; i < cblk_w; ++i) {
  2073. OPJ_UINT32 k;
  2074. for (k = j; k < cblk_h; k++) {
  2075. t1data[0] = (OPJ_INT32)opj_lrintf((tiledp_f[k * tile_w + i] / band->stepsize)
  2076. * (1 << T1_NMSEDEC_FRACBITS));
  2077. t1data ++;
  2078. }
  2079. }
  2080. }
  2081. }
  2082. {
  2083. OPJ_FLOAT64 cumwmsedec =
  2084. opj_t1_encode_cblk(
  2085. t1,
  2086. cblk,
  2087. band->bandno,
  2088. job->compno,
  2089. tilec->numresolutions - 1 - resno,
  2090. tccp->qmfbid,
  2091. band->stepsize,
  2092. tccp->cblksty,
  2093. job->tile->numcomps,
  2094. job->mct_norms,
  2095. job->mct_numcomps);
  2096. if (job->mutex) {
  2097. opj_mutex_lock(job->mutex);
  2098. }
  2099. job->tile->distotile += cumwmsedec;
  2100. if (job->mutex) {
  2101. opj_mutex_unlock(job->mutex);
  2102. }
  2103. }
  2104. opj_free(job);
  2105. }
  2106. OPJ_BOOL opj_t1_encode_cblks(opj_tcd_t* tcd,
  2107. opj_tcd_tile_t *tile,
  2108. opj_tcp_t *tcp,
  2109. const OPJ_FLOAT64 * mct_norms,
  2110. OPJ_UINT32 mct_numcomps
  2111. )
  2112. {
  2113. volatile OPJ_BOOL ret = OPJ_TRUE;
  2114. opj_thread_pool_t* tp = tcd->thread_pool;
  2115. OPJ_UINT32 compno, resno, bandno, precno, cblkno;
  2116. opj_mutex_t* mutex = opj_mutex_create();
  2117. tile->distotile = 0; /* fixed_quality */
  2118. for (compno = 0; compno < tile->numcomps; ++compno) {
  2119. opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
  2120. opj_tccp_t* tccp = &tcp->tccps[compno];
  2121. for (resno = 0; resno < tilec->numresolutions; ++resno) {
  2122. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  2123. for (bandno = 0; bandno < res->numbands; ++bandno) {
  2124. opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
  2125. /* Skip empty bands */
  2126. if (opj_tcd_is_band_empty(band)) {
  2127. continue;
  2128. }
  2129. for (precno = 0; precno < res->pw * res->ph; ++precno) {
  2130. opj_tcd_precinct_t *prc = &band->precincts[precno];
  2131. for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
  2132. opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
  2133. opj_t1_cblk_encode_processing_job_t* job =
  2134. (opj_t1_cblk_encode_processing_job_t*) opj_calloc(1,
  2135. sizeof(opj_t1_cblk_encode_processing_job_t));
  2136. if (!job) {
  2137. ret = OPJ_FALSE;
  2138. goto end;
  2139. }
  2140. job->compno = compno;
  2141. job->tile = tile;
  2142. job->resno = resno;
  2143. job->cblk = cblk;
  2144. job->band = band;
  2145. job->tilec = tilec;
  2146. job->tccp = tccp;
  2147. job->mct_norms = mct_norms;
  2148. job->mct_numcomps = mct_numcomps;
  2149. job->pret = &ret;
  2150. job->mutex = mutex;
  2151. opj_thread_pool_submit_job(tp, opj_t1_cblk_encode_processor, job);
  2152. } /* cblkno */
  2153. } /* precno */
  2154. } /* bandno */
  2155. } /* resno */
  2156. } /* compno */
  2157. end:
  2158. opj_thread_pool_wait_completion(tcd->thread_pool, 0);
  2159. if (mutex) {
  2160. opj_mutex_destroy(mutex);
  2161. }
  2162. return ret;
  2163. }
  2164. /* Returns whether the pass (bpno, passtype) is terminated */
  2165. static int opj_t1_enc_is_term_pass(opj_tcd_cblk_enc_t* cblk,
  2166. OPJ_UINT32 cblksty,
  2167. OPJ_INT32 bpno,
  2168. OPJ_UINT32 passtype)
  2169. {
  2170. /* Is it the last cleanup pass ? */
  2171. if (passtype == 2 && bpno == 0) {
  2172. return OPJ_TRUE;
  2173. }
  2174. if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
  2175. return OPJ_TRUE;
  2176. }
  2177. if ((cblksty & J2K_CCP_CBLKSTY_LAZY)) {
  2178. /* For bypass arithmetic bypass, terminate the 4th cleanup pass */
  2179. if ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2)) {
  2180. return OPJ_TRUE;
  2181. }
  2182. /* and beyond terminate all the magnitude refinement passes (in raw) */
  2183. /* and cleanup passes (in MQC) */
  2184. if ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype > 0)) {
  2185. return OPJ_TRUE;
  2186. }
  2187. }
  2188. return OPJ_FALSE;
  2189. }
  2190. /** mod fixed_quality */
  2191. static OPJ_FLOAT64 opj_t1_encode_cblk(opj_t1_t *t1,
  2192. opj_tcd_cblk_enc_t* cblk,
  2193. OPJ_UINT32 orient,
  2194. OPJ_UINT32 compno,
  2195. OPJ_UINT32 level,
  2196. OPJ_UINT32 qmfbid,
  2197. OPJ_FLOAT64 stepsize,
  2198. OPJ_UINT32 cblksty,
  2199. OPJ_UINT32 numcomps,
  2200. const OPJ_FLOAT64 * mct_norms,
  2201. OPJ_UINT32 mct_numcomps)
  2202. {
  2203. OPJ_FLOAT64 cumwmsedec = 0.0;
  2204. opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
  2205. OPJ_UINT32 passno;
  2206. OPJ_INT32 bpno;
  2207. OPJ_UINT32 passtype;
  2208. OPJ_INT32 nmsedec = 0;
  2209. OPJ_INT32 max;
  2210. OPJ_UINT32 i, j;
  2211. OPJ_BYTE type = T1_TYPE_MQ;
  2212. OPJ_FLOAT64 tempwmsedec;
  2213. OPJ_INT32* datap;
  2214. #ifdef EXTRA_DEBUG
  2215. printf("encode_cblk(x=%d,y=%d,x1=%d,y1=%d,orient=%d,compno=%d,level=%d\n",
  2216. cblk->x0, cblk->y0, cblk->x1, cblk->y1, orient, compno, level);
  2217. #endif
  2218. mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
  2219. max = 0;
  2220. datap = t1->data;
  2221. for (j = 0; j < t1->h; ++j) {
  2222. const OPJ_UINT32 w = t1->w;
  2223. for (i = 0; i < w; ++i, ++datap) {
  2224. OPJ_INT32 tmp = *datap;
  2225. if (tmp < 0) {
  2226. OPJ_UINT32 tmp_unsigned;
  2227. max = opj_int_max(max, -tmp);
  2228. tmp_unsigned = opj_to_smr(tmp);
  2229. memcpy(datap, &tmp_unsigned, sizeof(OPJ_INT32));
  2230. } else {
  2231. max = opj_int_max(max, tmp);
  2232. }
  2233. }
  2234. }
  2235. cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
  2236. T1_NMSEDEC_FRACBITS) : 0;
  2237. if (cblk->numbps == 0) {
  2238. cblk->totalpasses = 0;
  2239. return cumwmsedec;
  2240. }
  2241. bpno = (OPJ_INT32)(cblk->numbps - 1);
  2242. passtype = 2;
  2243. opj_mqc_resetstates(mqc);
  2244. opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  2245. opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  2246. opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  2247. opj_mqc_init_enc(mqc, cblk->data);
  2248. for (passno = 0; bpno >= 0; ++passno) {
  2249. opj_tcd_pass_t *pass = &cblk->passes[passno];
  2250. type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
  2251. (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
  2252. /* If the previous pass was terminating, we need to reset the encoder */
  2253. if (passno > 0 && cblk->passes[passno - 1].term) {
  2254. if (type == T1_TYPE_RAW) {
  2255. opj_mqc_bypass_init_enc(mqc);
  2256. } else {
  2257. opj_mqc_restart_init_enc(mqc);
  2258. }
  2259. }
  2260. switch (passtype) {
  2261. case 0:
  2262. opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
  2263. break;
  2264. case 1:
  2265. opj_t1_enc_refpass(t1, bpno, &nmsedec, type);
  2266. break;
  2267. case 2:
  2268. opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
  2269. /* code switch SEGMARK (i.e. SEGSYM) */
  2270. if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
  2271. opj_mqc_segmark_enc(mqc);
  2272. }
  2273. break;
  2274. }
  2275. /* fixed_quality */
  2276. tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
  2277. stepsize, numcomps, mct_norms, mct_numcomps) ;
  2278. cumwmsedec += tempwmsedec;
  2279. pass->distortiondec = cumwmsedec;
  2280. if (opj_t1_enc_is_term_pass(cblk, cblksty, bpno, passtype)) {
  2281. /* If it is a terminated pass, terminate it */
  2282. if (type == T1_TYPE_RAW) {
  2283. opj_mqc_bypass_flush_enc(mqc, cblksty & J2K_CCP_CBLKSTY_PTERM);
  2284. } else {
  2285. if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
  2286. opj_mqc_erterm_enc(mqc);
  2287. } else {
  2288. opj_mqc_flush(mqc);
  2289. }
  2290. }
  2291. pass->term = 1;
  2292. pass->rate = opj_mqc_numbytes(mqc);
  2293. } else {
  2294. /* Non terminated pass */
  2295. OPJ_UINT32 rate_extra_bytes;
  2296. if (type == T1_TYPE_RAW) {
  2297. rate_extra_bytes = opj_mqc_bypass_get_extra_bytes(
  2298. mqc, (cblksty & J2K_CCP_CBLKSTY_PTERM));
  2299. } else {
  2300. rate_extra_bytes = 3;
  2301. }
  2302. pass->term = 0;
  2303. pass->rate = opj_mqc_numbytes(mqc) + rate_extra_bytes;
  2304. }
  2305. if (++passtype == 3) {
  2306. passtype = 0;
  2307. bpno--;
  2308. }
  2309. /* Code-switch "RESET" */
  2310. if (cblksty & J2K_CCP_CBLKSTY_RESET) {
  2311. opj_mqc_reset_enc(mqc);
  2312. }
  2313. }
  2314. cblk->totalpasses = passno;
  2315. if (cblk->totalpasses) {
  2316. /* Make sure that pass rates are increasing */
  2317. OPJ_UINT32 last_pass_rate = opj_mqc_numbytes(mqc);
  2318. for (passno = cblk->totalpasses; passno > 0;) {
  2319. opj_tcd_pass_t *pass = &cblk->passes[--passno];
  2320. if (pass->rate > last_pass_rate) {
  2321. pass->rate = last_pass_rate;
  2322. } else {
  2323. last_pass_rate = pass->rate;
  2324. }
  2325. }
  2326. }
  2327. for (passno = 0; passno < cblk->totalpasses; passno++) {
  2328. opj_tcd_pass_t *pass = &cblk->passes[passno];
  2329. /* Prevent generation of FF as last data byte of a pass*/
  2330. /* For terminating passes, the flushing procedure ensured this already */
  2331. assert(pass->rate > 0);
  2332. if (cblk->data[pass->rate - 1] == 0xFF) {
  2333. pass->rate--;
  2334. }
  2335. pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
  2336. }
  2337. #ifdef EXTRA_DEBUG
  2338. printf(" len=%d\n", (cblk->totalpasses) ? opj_mqc_numbytes(mqc) : 0);
  2339. /* Check that there not 0xff >=0x90 sequences */
  2340. if (cblk->totalpasses) {
  2341. OPJ_UINT32 i;
  2342. OPJ_UINT32 len = opj_mqc_numbytes(mqc);
  2343. for (i = 1; i < len; ++i) {
  2344. if (cblk->data[i - 1] == 0xff && cblk->data[i] >= 0x90) {
  2345. printf("0xff %02x at offset %d\n", cblk->data[i], i - 1);
  2346. abort();
  2347. }
  2348. }
  2349. }
  2350. #endif
  2351. return cumwmsedec;
  2352. }