t1.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592
  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. static OPJ_FLOAT64 opj_t1_getwmsedec(
  1312. OPJ_INT32 nmsedec,
  1313. OPJ_UINT32 compno,
  1314. OPJ_UINT32 level,
  1315. OPJ_UINT32 orient,
  1316. OPJ_INT32 bpno,
  1317. OPJ_UINT32 qmfbid,
  1318. OPJ_FLOAT64 stepsize,
  1319. OPJ_UINT32 numcomps,
  1320. const OPJ_FLOAT64 * mct_norms,
  1321. OPJ_UINT32 mct_numcomps)
  1322. {
  1323. OPJ_FLOAT64 w1 = 1, w2, wmsedec;
  1324. OPJ_ARG_NOT_USED(numcomps);
  1325. if (mct_norms && (compno < mct_numcomps)) {
  1326. w1 = mct_norms[compno];
  1327. }
  1328. if (qmfbid == 1) {
  1329. w2 = opj_dwt_getnorm(level, orient);
  1330. } else { /* if (qmfbid == 0) */
  1331. const OPJ_INT32 log2_gain = (orient == 0) ? 0 :
  1332. (orient == 3) ? 2 : 1;
  1333. w2 = opj_dwt_getnorm_real(level, orient);
  1334. /* Not sure this is right. But preserves past behaviour */
  1335. stepsize /= (1 << log2_gain);
  1336. }
  1337. wmsedec = w1 * w2 * stepsize * (1 << bpno);
  1338. wmsedec *= wmsedec * nmsedec / 8192.0;
  1339. return wmsedec;
  1340. }
  1341. static OPJ_BOOL opj_t1_allocate_buffers(
  1342. opj_t1_t *t1,
  1343. OPJ_UINT32 w,
  1344. OPJ_UINT32 h)
  1345. {
  1346. OPJ_UINT32 flagssize;
  1347. OPJ_UINT32 flags_stride;
  1348. /* No risk of overflow. Prior checks ensure those assert are met */
  1349. /* They are per the specification */
  1350. assert(w <= 1024);
  1351. assert(h <= 1024);
  1352. assert(w * h <= 4096);
  1353. /* encoder uses tile buffer, so no need to allocate */
  1354. {
  1355. OPJ_UINT32 datasize = w * h;
  1356. if (datasize > t1->datasize) {
  1357. opj_aligned_free(t1->data);
  1358. t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
  1359. if (!t1->data) {
  1360. /* FIXME event manager error callback */
  1361. return OPJ_FALSE;
  1362. }
  1363. t1->datasize = datasize;
  1364. }
  1365. /* memset first arg is declared to never be null by gcc */
  1366. if (t1->data != NULL) {
  1367. memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
  1368. }
  1369. }
  1370. flags_stride = w + 2U; /* can't be 0U */
  1371. flagssize = (h + 3U) / 4U + 2U;
  1372. flagssize *= flags_stride;
  1373. {
  1374. opj_flag_t* p;
  1375. OPJ_UINT32 x;
  1376. OPJ_UINT32 flags_height = (h + 3U) / 4U;
  1377. if (flagssize > t1->flagssize) {
  1378. opj_aligned_free(t1->flags);
  1379. t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(
  1380. opj_flag_t));
  1381. if (!t1->flags) {
  1382. /* FIXME event manager error callback */
  1383. return OPJ_FALSE;
  1384. }
  1385. }
  1386. t1->flagssize = flagssize;
  1387. memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
  1388. p = &t1->flags[0];
  1389. for (x = 0; x < flags_stride; ++x) {
  1390. /* magic value to hopefully stop any passes being interested in this entry */
  1391. *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
  1392. }
  1393. p = &t1->flags[((flags_height + 1) * flags_stride)];
  1394. for (x = 0; x < flags_stride; ++x) {
  1395. /* magic value to hopefully stop any passes being interested in this entry */
  1396. *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
  1397. }
  1398. if (h % 4) {
  1399. OPJ_UINT32 v = 0;
  1400. p = &t1->flags[((flags_height) * flags_stride)];
  1401. if (h % 4 == 1) {
  1402. v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
  1403. } else if (h % 4 == 2) {
  1404. v |= T1_PI_2 | T1_PI_3;
  1405. } else if (h % 4 == 3) {
  1406. v |= T1_PI_3;
  1407. }
  1408. for (x = 0; x < flags_stride; ++x) {
  1409. *p++ = v;
  1410. }
  1411. }
  1412. }
  1413. t1->w = w;
  1414. t1->h = h;
  1415. return OPJ_TRUE;
  1416. }
  1417. /* ----------------------------------------------------------------------- */
  1418. /* ----------------------------------------------------------------------- */
  1419. /**
  1420. * Creates a new Tier 1 handle
  1421. * and initializes the look-up tables of the Tier-1 coder/decoder
  1422. * @return a new T1 handle if successful, returns NULL otherwise
  1423. */
  1424. opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
  1425. {
  1426. opj_t1_t *l_t1 = 00;
  1427. l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
  1428. if (!l_t1) {
  1429. return 00;
  1430. }
  1431. l_t1->encoder = isEncoder;
  1432. return l_t1;
  1433. }
  1434. /**
  1435. * Destroys a previously created T1 handle
  1436. *
  1437. * @param p_t1 Tier 1 handle to destroy
  1438. */
  1439. void opj_t1_destroy(opj_t1_t *p_t1)
  1440. {
  1441. if (! p_t1) {
  1442. return;
  1443. }
  1444. if (p_t1->data) {
  1445. opj_aligned_free(p_t1->data);
  1446. p_t1->data = 00;
  1447. }
  1448. if (p_t1->flags) {
  1449. opj_aligned_free(p_t1->flags);
  1450. p_t1->flags = 00;
  1451. }
  1452. opj_free(p_t1->cblkdatabuffer);
  1453. opj_free(p_t1);
  1454. }
  1455. typedef struct {
  1456. OPJ_BOOL whole_tile_decoding;
  1457. OPJ_UINT32 resno;
  1458. opj_tcd_cblk_dec_t* cblk;
  1459. opj_tcd_band_t* band;
  1460. opj_tcd_tilecomp_t* tilec;
  1461. opj_tccp_t* tccp;
  1462. OPJ_BOOL mustuse_cblkdatabuffer;
  1463. volatile OPJ_BOOL* pret;
  1464. opj_event_mgr_t *p_manager;
  1465. opj_mutex_t* p_manager_mutex;
  1466. OPJ_BOOL check_pterm;
  1467. } opj_t1_cblk_decode_processing_job_t;
  1468. static void opj_t1_destroy_wrapper(void* t1)
  1469. {
  1470. opj_t1_destroy((opj_t1_t*) t1);
  1471. }
  1472. static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
  1473. {
  1474. opj_tcd_cblk_dec_t* cblk;
  1475. opj_tcd_band_t* band;
  1476. opj_tcd_tilecomp_t* tilec;
  1477. opj_tccp_t* tccp;
  1478. OPJ_INT32* OPJ_RESTRICT datap;
  1479. OPJ_UINT32 cblk_w, cblk_h;
  1480. OPJ_INT32 x, y;
  1481. OPJ_UINT32 i, j;
  1482. opj_t1_cblk_decode_processing_job_t* job;
  1483. opj_t1_t* t1;
  1484. OPJ_UINT32 resno;
  1485. OPJ_UINT32 tile_w;
  1486. job = (opj_t1_cblk_decode_processing_job_t*) user_data;
  1487. cblk = job->cblk;
  1488. if (!job->whole_tile_decoding) {
  1489. cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
  1490. cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
  1491. cblk->decoded_data = (OPJ_INT32*)opj_aligned_malloc(sizeof(OPJ_INT32) *
  1492. cblk_w * cblk_h);
  1493. if (cblk->decoded_data == NULL) {
  1494. if (job->p_manager_mutex) {
  1495. opj_mutex_lock(job->p_manager_mutex);
  1496. }
  1497. opj_event_msg(job->p_manager, EVT_ERROR,
  1498. "Cannot allocate cblk->decoded_data\n");
  1499. if (job->p_manager_mutex) {
  1500. opj_mutex_unlock(job->p_manager_mutex);
  1501. }
  1502. *(job->pret) = OPJ_FALSE;
  1503. opj_free(job);
  1504. return;
  1505. }
  1506. /* Zero-init required */
  1507. memset(cblk->decoded_data, 0, sizeof(OPJ_INT32) * cblk_w * cblk_h);
  1508. } else if (cblk->decoded_data) {
  1509. /* Not sure if that code path can happen, but better be */
  1510. /* safe than sorry */
  1511. opj_aligned_free(cblk->decoded_data);
  1512. cblk->decoded_data = NULL;
  1513. }
  1514. resno = job->resno;
  1515. band = job->band;
  1516. tilec = job->tilec;
  1517. tccp = job->tccp;
  1518. tile_w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions - 1].x1
  1519. -
  1520. tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
  1521. if (!*(job->pret)) {
  1522. opj_free(job);
  1523. return;
  1524. }
  1525. t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
  1526. if (t1 == NULL) {
  1527. t1 = opj_t1_create(OPJ_FALSE);
  1528. if (t1 == NULL) {
  1529. opj_event_msg(job->p_manager, EVT_ERROR,
  1530. "Cannot allocate Tier 1 handle\n");
  1531. *(job->pret) = OPJ_FALSE;
  1532. opj_free(job);
  1533. return;
  1534. }
  1535. if (!opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper)) {
  1536. opj_event_msg(job->p_manager, EVT_ERROR,
  1537. "Unable to set t1 handle as TLS\n");
  1538. opj_t1_destroy(t1);
  1539. *(job->pret) = OPJ_FALSE;
  1540. opj_free(job);
  1541. return;
  1542. }
  1543. }
  1544. t1->mustuse_cblkdatabuffer = job->mustuse_cblkdatabuffer;
  1545. if ((tccp->cblksty & J2K_CCP_CBLKSTY_HT) != 0) {
  1546. if (OPJ_FALSE == opj_t1_ht_decode_cblk(
  1547. t1,
  1548. cblk,
  1549. band->bandno,
  1550. (OPJ_UINT32)tccp->roishift,
  1551. tccp->cblksty,
  1552. job->p_manager,
  1553. job->p_manager_mutex,
  1554. job->check_pterm)) {
  1555. *(job->pret) = OPJ_FALSE;
  1556. opj_free(job);
  1557. return;
  1558. }
  1559. } else {
  1560. if (OPJ_FALSE == opj_t1_decode_cblk(
  1561. t1,
  1562. cblk,
  1563. band->bandno,
  1564. (OPJ_UINT32)tccp->roishift,
  1565. tccp->cblksty,
  1566. job->p_manager,
  1567. job->p_manager_mutex,
  1568. job->check_pterm)) {
  1569. *(job->pret) = OPJ_FALSE;
  1570. opj_free(job);
  1571. return;
  1572. }
  1573. }
  1574. x = cblk->x0 - band->x0;
  1575. y = cblk->y0 - band->y0;
  1576. if (band->bandno & 1) {
  1577. opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
  1578. x += pres->x1 - pres->x0;
  1579. }
  1580. if (band->bandno & 2) {
  1581. opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
  1582. y += pres->y1 - pres->y0;
  1583. }
  1584. datap = cblk->decoded_data ? cblk->decoded_data : t1->data;
  1585. cblk_w = t1->w;
  1586. cblk_h = t1->h;
  1587. if (tccp->roishift) {
  1588. if (tccp->roishift >= 31) {
  1589. for (j = 0; j < cblk_h; ++j) {
  1590. for (i = 0; i < cblk_w; ++i) {
  1591. datap[(j * cblk_w) + i] = 0;
  1592. }
  1593. }
  1594. } else {
  1595. OPJ_INT32 thresh = 1 << tccp->roishift;
  1596. for (j = 0; j < cblk_h; ++j) {
  1597. for (i = 0; i < cblk_w; ++i) {
  1598. OPJ_INT32 val = datap[(j * cblk_w) + i];
  1599. OPJ_INT32 mag = abs(val);
  1600. if (mag >= thresh) {
  1601. mag >>= tccp->roishift;
  1602. datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
  1603. }
  1604. }
  1605. }
  1606. }
  1607. }
  1608. /* Both can be non NULL if for example decoding a full tile and then */
  1609. /* partially a tile. In which case partial decoding should be the */
  1610. /* priority */
  1611. assert((cblk->decoded_data != NULL) || (tilec->data != NULL));
  1612. if (cblk->decoded_data) {
  1613. OPJ_UINT32 cblk_size = cblk_w * cblk_h;
  1614. if (tccp->qmfbid == 1) {
  1615. for (i = 0; i < cblk_size; ++i) {
  1616. datap[i] /= 2;
  1617. }
  1618. } else { /* if (tccp->qmfbid == 0) */
  1619. const float stepsize = 0.5f * band->stepsize;
  1620. i = 0;
  1621. #ifdef __SSE2__
  1622. {
  1623. const __m128 xmm_stepsize = _mm_set1_ps(stepsize);
  1624. for (; i < (cblk_size & ~15U); i += 16) {
  1625. __m128 xmm0_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)(
  1626. datap + 0)));
  1627. __m128 xmm1_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)(
  1628. datap + 4)));
  1629. __m128 xmm2_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)(
  1630. datap + 8)));
  1631. __m128 xmm3_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)(
  1632. datap + 12)));
  1633. _mm_store_ps((float*)(datap + 0), _mm_mul_ps(xmm0_data, xmm_stepsize));
  1634. _mm_store_ps((float*)(datap + 4), _mm_mul_ps(xmm1_data, xmm_stepsize));
  1635. _mm_store_ps((float*)(datap + 8), _mm_mul_ps(xmm2_data, xmm_stepsize));
  1636. _mm_store_ps((float*)(datap + 12), _mm_mul_ps(xmm3_data, xmm_stepsize));
  1637. datap += 16;
  1638. }
  1639. }
  1640. #endif
  1641. for (; i < cblk_size; ++i) {
  1642. OPJ_FLOAT32 tmp = ((OPJ_FLOAT32)(*datap)) * stepsize;
  1643. memcpy(datap, &tmp, sizeof(tmp));
  1644. datap++;
  1645. }
  1646. }
  1647. } else if (tccp->qmfbid == 1) {
  1648. OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_SIZE_T)y * tile_w +
  1649. (OPJ_SIZE_T)x];
  1650. for (j = 0; j < cblk_h; ++j) {
  1651. i = 0;
  1652. for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
  1653. OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
  1654. OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
  1655. OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
  1656. OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
  1657. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 0U] = tmp0 / 2;
  1658. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 1U] = tmp1 / 2;
  1659. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 2U] = tmp2 / 2;
  1660. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 3U] = tmp3 / 2;
  1661. }
  1662. for (; i < cblk_w; ++i) {
  1663. OPJ_INT32 tmp = datap[(j * cblk_w) + i];
  1664. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i] = tmp / 2;
  1665. }
  1666. }
  1667. } else { /* if (tccp->qmfbid == 0) */
  1668. const float stepsize = 0.5f * band->stepsize;
  1669. OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_SIZE_T)y *
  1670. tile_w + (OPJ_SIZE_T)x];
  1671. for (j = 0; j < cblk_h; ++j) {
  1672. OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
  1673. for (i = 0; i < cblk_w; ++i) {
  1674. OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * stepsize;
  1675. *tiledp2 = tmp;
  1676. datap++;
  1677. tiledp2++;
  1678. }
  1679. tiledp += tile_w;
  1680. }
  1681. }
  1682. opj_free(job);
  1683. }
  1684. void opj_t1_decode_cblks(opj_tcd_t* tcd,
  1685. volatile OPJ_BOOL* pret,
  1686. opj_tcd_tilecomp_t* tilec,
  1687. opj_tccp_t* tccp,
  1688. opj_event_mgr_t *p_manager,
  1689. opj_mutex_t* p_manager_mutex,
  1690. OPJ_BOOL check_pterm
  1691. )
  1692. {
  1693. opj_thread_pool_t* tp = tcd->thread_pool;
  1694. OPJ_UINT32 resno, bandno, precno, cblkno;
  1695. #ifdef DEBUG_VERBOSE
  1696. OPJ_UINT32 codeblocks_decoded = 0;
  1697. printf("Enter opj_t1_decode_cblks()\n");
  1698. #endif
  1699. for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
  1700. opj_tcd_resolution_t* res = &tilec->resolutions[resno];
  1701. for (bandno = 0; bandno < res->numbands; ++bandno) {
  1702. opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
  1703. for (precno = 0; precno < res->pw * res->ph; ++precno) {
  1704. opj_tcd_precinct_t* precinct = &band->precincts[precno];
  1705. if (!opj_tcd_is_subband_area_of_interest(tcd,
  1706. tilec->compno,
  1707. resno,
  1708. band->bandno,
  1709. (OPJ_UINT32)precinct->x0,
  1710. (OPJ_UINT32)precinct->y0,
  1711. (OPJ_UINT32)precinct->x1,
  1712. (OPJ_UINT32)precinct->y1)) {
  1713. for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
  1714. opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
  1715. if (cblk->decoded_data) {
  1716. #ifdef DEBUG_VERBOSE
  1717. printf("Discarding codeblock %d,%d at resno=%d, bandno=%d\n",
  1718. cblk->x0, cblk->y0, resno, bandno);
  1719. #endif
  1720. opj_aligned_free(cblk->decoded_data);
  1721. cblk->decoded_data = NULL;
  1722. }
  1723. }
  1724. continue;
  1725. }
  1726. for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
  1727. opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
  1728. opj_t1_cblk_decode_processing_job_t* job;
  1729. if (!opj_tcd_is_subband_area_of_interest(tcd,
  1730. tilec->compno,
  1731. resno,
  1732. band->bandno,
  1733. (OPJ_UINT32)cblk->x0,
  1734. (OPJ_UINT32)cblk->y0,
  1735. (OPJ_UINT32)cblk->x1,
  1736. (OPJ_UINT32)cblk->y1)) {
  1737. if (cblk->decoded_data) {
  1738. #ifdef DEBUG_VERBOSE
  1739. printf("Discarding codeblock %d,%d at resno=%d, bandno=%d\n",
  1740. cblk->x0, cblk->y0, resno, bandno);
  1741. #endif
  1742. opj_aligned_free(cblk->decoded_data);
  1743. cblk->decoded_data = NULL;
  1744. }
  1745. continue;
  1746. }
  1747. if (!tcd->whole_tile_decoding) {
  1748. OPJ_UINT32 cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
  1749. OPJ_UINT32 cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
  1750. if (cblk->decoded_data != NULL) {
  1751. #ifdef DEBUG_VERBOSE
  1752. printf("Reusing codeblock %d,%d at resno=%d, bandno=%d\n",
  1753. cblk->x0, cblk->y0, resno, bandno);
  1754. #endif
  1755. continue;
  1756. }
  1757. if (cblk_w == 0 || cblk_h == 0) {
  1758. continue;
  1759. }
  1760. #ifdef DEBUG_VERBOSE
  1761. printf("Decoding codeblock %d,%d at resno=%d, bandno=%d\n",
  1762. cblk->x0, cblk->y0, resno, bandno);
  1763. #endif
  1764. }
  1765. job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
  1766. sizeof(opj_t1_cblk_decode_processing_job_t));
  1767. if (!job) {
  1768. *pret = OPJ_FALSE;
  1769. return;
  1770. }
  1771. job->whole_tile_decoding = tcd->whole_tile_decoding;
  1772. job->resno = resno;
  1773. job->cblk = cblk;
  1774. job->band = band;
  1775. job->tilec = tilec;
  1776. job->tccp = tccp;
  1777. job->pret = pret;
  1778. job->p_manager_mutex = p_manager_mutex;
  1779. job->p_manager = p_manager;
  1780. job->check_pterm = check_pterm;
  1781. job->mustuse_cblkdatabuffer = opj_thread_pool_get_thread_count(tp) > 1;
  1782. opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
  1783. #ifdef DEBUG_VERBOSE
  1784. codeblocks_decoded ++;
  1785. #endif
  1786. if (!(*pret)) {
  1787. return;
  1788. }
  1789. } /* cblkno */
  1790. } /* precno */
  1791. } /* bandno */
  1792. } /* resno */
  1793. #ifdef DEBUG_VERBOSE
  1794. printf("Leave opj_t1_decode_cblks(). Number decoded: %d\n", codeblocks_decoded);
  1795. #endif
  1796. return;
  1797. }
  1798. static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
  1799. opj_tcd_cblk_dec_t* cblk,
  1800. OPJ_UINT32 orient,
  1801. OPJ_UINT32 roishift,
  1802. OPJ_UINT32 cblksty,
  1803. opj_event_mgr_t *p_manager,
  1804. opj_mutex_t* p_manager_mutex,
  1805. OPJ_BOOL check_pterm)
  1806. {
  1807. opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
  1808. OPJ_INT32 bpno_plus_one;
  1809. OPJ_UINT32 passtype;
  1810. OPJ_UINT32 segno, passno;
  1811. OPJ_BYTE* cblkdata = NULL;
  1812. OPJ_UINT32 cblkdataindex = 0;
  1813. OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
  1814. OPJ_INT32* original_t1_data = NULL;
  1815. mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
  1816. if (!opj_t1_allocate_buffers(
  1817. t1,
  1818. (OPJ_UINT32)(cblk->x1 - cblk->x0),
  1819. (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
  1820. return OPJ_FALSE;
  1821. }
  1822. bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
  1823. if (bpno_plus_one >= 31) {
  1824. if (p_manager_mutex) {
  1825. opj_mutex_lock(p_manager_mutex);
  1826. }
  1827. opj_event_msg(p_manager, EVT_WARNING,
  1828. "opj_t1_decode_cblk(): unsupported bpno_plus_one = %d >= 31\n",
  1829. bpno_plus_one);
  1830. if (p_manager_mutex) {
  1831. opj_mutex_unlock(p_manager_mutex);
  1832. }
  1833. return OPJ_FALSE;
  1834. }
  1835. passtype = 2;
  1836. opj_mqc_resetstates(mqc);
  1837. opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  1838. opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  1839. opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  1840. /* Even if we have a single chunk, in multi-threaded decoding */
  1841. /* the insertion of our synthetic marker might potentially override */
  1842. /* valid codestream of other codeblocks decoded in parallel. */
  1843. if (cblk->numchunks > 1 || t1->mustuse_cblkdatabuffer) {
  1844. OPJ_UINT32 i;
  1845. OPJ_UINT32 cblk_len;
  1846. /* Compute whole codeblock length from chunk lengths */
  1847. cblk_len = 0;
  1848. for (i = 0; i < cblk->numchunks; i++) {
  1849. cblk_len += cblk->chunks[i].len;
  1850. }
  1851. /* Allocate temporary memory if needed */
  1852. if (cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA > t1->cblkdatabuffersize) {
  1853. cblkdata = (OPJ_BYTE*)opj_realloc(t1->cblkdatabuffer,
  1854. cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA);
  1855. if (cblkdata == NULL) {
  1856. return OPJ_FALSE;
  1857. }
  1858. t1->cblkdatabuffer = cblkdata;
  1859. memset(t1->cblkdatabuffer + cblk_len, 0, OPJ_COMMON_CBLK_DATA_EXTRA);
  1860. t1->cblkdatabuffersize = cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA;
  1861. }
  1862. /* Concatenate all chunks */
  1863. cblkdata = t1->cblkdatabuffer;
  1864. cblk_len = 0;
  1865. for (i = 0; i < cblk->numchunks; i++) {
  1866. memcpy(cblkdata + cblk_len, cblk->chunks[i].data, cblk->chunks[i].len);
  1867. cblk_len += cblk->chunks[i].len;
  1868. }
  1869. } else if (cblk->numchunks == 1) {
  1870. cblkdata = cblk->chunks[0].data;
  1871. } else {
  1872. /* Not sure if that can happen in practice, but avoid Coverity to */
  1873. /* think we will dereference a null cblkdta pointer */
  1874. return OPJ_TRUE;
  1875. }
  1876. /* For subtile decoding, directly decode in the decoded_data buffer of */
  1877. /* the code-block. Hack t1->data to point to it, and restore it later */
  1878. if (cblk->decoded_data) {
  1879. original_t1_data = t1->data;
  1880. t1->data = cblk->decoded_data;
  1881. }
  1882. for (segno = 0; segno < cblk->real_num_segs; ++segno) {
  1883. opj_tcd_seg_t *seg = &cblk->segs[segno];
  1884. /* BYPASS mode */
  1885. type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
  1886. (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
  1887. if (type == T1_TYPE_RAW) {
  1888. opj_mqc_raw_init_dec(mqc, cblkdata + cblkdataindex, seg->len,
  1889. OPJ_COMMON_CBLK_DATA_EXTRA);
  1890. } else {
  1891. opj_mqc_init_dec(mqc, cblkdata + cblkdataindex, seg->len,
  1892. OPJ_COMMON_CBLK_DATA_EXTRA);
  1893. }
  1894. cblkdataindex += seg->len;
  1895. for (passno = 0; (passno < seg->real_num_passes) &&
  1896. (bpno_plus_one >= 1); ++passno) {
  1897. switch (passtype) {
  1898. case 0:
  1899. if (type == T1_TYPE_RAW) {
  1900. opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
  1901. } else {
  1902. opj_t1_dec_sigpass_mqc(t1, bpno_plus_one, (OPJ_INT32)cblksty);
  1903. }
  1904. break;
  1905. case 1:
  1906. if (type == T1_TYPE_RAW) {
  1907. opj_t1_dec_refpass_raw(t1, bpno_plus_one);
  1908. } else {
  1909. opj_t1_dec_refpass_mqc(t1, bpno_plus_one);
  1910. }
  1911. break;
  1912. case 2:
  1913. opj_t1_dec_clnpass(t1, bpno_plus_one, (OPJ_INT32)cblksty);
  1914. break;
  1915. }
  1916. if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
  1917. opj_mqc_resetstates(mqc);
  1918. opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  1919. opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  1920. opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  1921. }
  1922. if (++passtype == 3) {
  1923. passtype = 0;
  1924. bpno_plus_one--;
  1925. }
  1926. }
  1927. opq_mqc_finish_dec(mqc);
  1928. }
  1929. if (check_pterm) {
  1930. if (mqc->bp + 2 < mqc->end) {
  1931. if (p_manager_mutex) {
  1932. opj_mutex_lock(p_manager_mutex);
  1933. }
  1934. opj_event_msg(p_manager, EVT_WARNING,
  1935. "PTERM check failure: %d remaining bytes in code block (%d used / %d)\n",
  1936. (int)(mqc->end - mqc->bp) - 2,
  1937. (int)(mqc->bp - mqc->start),
  1938. (int)(mqc->end - mqc->start));
  1939. if (p_manager_mutex) {
  1940. opj_mutex_unlock(p_manager_mutex);
  1941. }
  1942. } else if (mqc->end_of_byte_stream_counter > 2) {
  1943. if (p_manager_mutex) {
  1944. opj_mutex_lock(p_manager_mutex);
  1945. }
  1946. opj_event_msg(p_manager, EVT_WARNING,
  1947. "PTERM check failure: %d synthetized 0xFF markers read\n",
  1948. mqc->end_of_byte_stream_counter);
  1949. if (p_manager_mutex) {
  1950. opj_mutex_unlock(p_manager_mutex);
  1951. }
  1952. }
  1953. }
  1954. /* Restore original t1->data is needed */
  1955. if (cblk->decoded_data) {
  1956. t1->data = original_t1_data;
  1957. }
  1958. return OPJ_TRUE;
  1959. }
  1960. typedef struct {
  1961. OPJ_UINT32 compno;
  1962. OPJ_UINT32 resno;
  1963. opj_tcd_cblk_enc_t* cblk;
  1964. opj_tcd_tile_t *tile;
  1965. opj_tcd_band_t* band;
  1966. opj_tcd_tilecomp_t* tilec;
  1967. opj_tccp_t* tccp;
  1968. const OPJ_FLOAT64 * mct_norms;
  1969. OPJ_UINT32 mct_numcomps;
  1970. volatile OPJ_BOOL* pret;
  1971. opj_mutex_t* mutex;
  1972. } opj_t1_cblk_encode_processing_job_t;
  1973. /** Procedure to deal with a asynchronous code-block encoding job.
  1974. *
  1975. * @param user_data Pointer to a opj_t1_cblk_encode_processing_job_t* structure
  1976. * @param tls TLS handle.
  1977. */
  1978. static void opj_t1_cblk_encode_processor(void* user_data, opj_tls_t* tls)
  1979. {
  1980. opj_t1_cblk_encode_processing_job_t* job =
  1981. (opj_t1_cblk_encode_processing_job_t*)user_data;
  1982. opj_tcd_cblk_enc_t* cblk = job->cblk;
  1983. const opj_tcd_band_t* band = job->band;
  1984. const opj_tcd_tilecomp_t* tilec = job->tilec;
  1985. const opj_tccp_t* tccp = job->tccp;
  1986. const OPJ_UINT32 resno = job->resno;
  1987. opj_t1_t* t1;
  1988. const OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
  1989. OPJ_INT32* OPJ_RESTRICT tiledp;
  1990. OPJ_UINT32 cblk_w;
  1991. OPJ_UINT32 cblk_h;
  1992. OPJ_UINT32 i, j;
  1993. OPJ_INT32 x = cblk->x0 - band->x0;
  1994. OPJ_INT32 y = cblk->y0 - band->y0;
  1995. if (!*(job->pret)) {
  1996. opj_free(job);
  1997. return;
  1998. }
  1999. t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
  2000. if (t1 == NULL) {
  2001. t1 = opj_t1_create(OPJ_TRUE); /* OPJ_TRUE == T1 for encoding */
  2002. opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
  2003. }
  2004. if (band->bandno & 1) {
  2005. opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
  2006. x += pres->x1 - pres->x0;
  2007. }
  2008. if (band->bandno & 2) {
  2009. opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
  2010. y += pres->y1 - pres->y0;
  2011. }
  2012. if (!opj_t1_allocate_buffers(
  2013. t1,
  2014. (OPJ_UINT32)(cblk->x1 - cblk->x0),
  2015. (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
  2016. *(job->pret) = OPJ_FALSE;
  2017. opj_free(job);
  2018. return;
  2019. }
  2020. cblk_w = t1->w;
  2021. cblk_h = t1->h;
  2022. tiledp = &tilec->data[(OPJ_SIZE_T)y * tile_w + (OPJ_SIZE_T)x];
  2023. if (tccp->qmfbid == 1) {
  2024. /* Do multiplication on unsigned type, even if the
  2025. * underlying type is signed, to avoid potential
  2026. * int overflow on large value (the output will be
  2027. * incorrect in such situation, but whatever...)
  2028. * This assumes complement-to-2 signed integer
  2029. * representation
  2030. * Fixes https://github.com/uclouvain/openjpeg/issues/1053
  2031. */
  2032. OPJ_UINT32* OPJ_RESTRICT tiledp_u = (OPJ_UINT32*) tiledp;
  2033. OPJ_UINT32* OPJ_RESTRICT t1data = (OPJ_UINT32*) t1->data;
  2034. /* Change from "natural" order to "zigzag" order of T1 passes */
  2035. for (j = 0; j < (cblk_h & ~3U); j += 4) {
  2036. for (i = 0; i < cblk_w; ++i) {
  2037. t1data[0] = tiledp_u[(j + 0) * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2038. t1data[1] = tiledp_u[(j + 1) * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2039. t1data[2] = tiledp_u[(j + 2) * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2040. t1data[3] = tiledp_u[(j + 3) * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2041. t1data += 4;
  2042. }
  2043. }
  2044. if (j < cblk_h) {
  2045. for (i = 0; i < cblk_w; ++i) {
  2046. OPJ_UINT32 k;
  2047. for (k = j; k < cblk_h; k++) {
  2048. t1data[0] = tiledp_u[k * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2049. t1data ++;
  2050. }
  2051. }
  2052. }
  2053. } else { /* if (tccp->qmfbid == 0) */
  2054. OPJ_FLOAT32* OPJ_RESTRICT tiledp_f = (OPJ_FLOAT32*) tiledp;
  2055. OPJ_INT32* OPJ_RESTRICT t1data = t1->data;
  2056. /* Change from "natural" order to "zigzag" order of T1 passes */
  2057. for (j = 0; j < (cblk_h & ~3U); j += 4) {
  2058. for (i = 0; i < cblk_w; ++i) {
  2059. t1data[0] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 0) * tile_w + i] /
  2060. band->stepsize) * (1 << T1_NMSEDEC_FRACBITS));
  2061. t1data[1] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 1) * tile_w + i] /
  2062. band->stepsize) * (1 << T1_NMSEDEC_FRACBITS));
  2063. t1data[2] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 2) * tile_w + i] /
  2064. band->stepsize) * (1 << T1_NMSEDEC_FRACBITS));
  2065. t1data[3] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 3) * tile_w + i] /
  2066. band->stepsize) * (1 << T1_NMSEDEC_FRACBITS));
  2067. t1data += 4;
  2068. }
  2069. }
  2070. if (j < cblk_h) {
  2071. for (i = 0; i < cblk_w; ++i) {
  2072. OPJ_UINT32 k;
  2073. for (k = j; k < cblk_h; k++) {
  2074. t1data[0] = (OPJ_INT32)opj_lrintf((tiledp_f[k * tile_w + i] / band->stepsize)
  2075. * (1 << T1_NMSEDEC_FRACBITS));
  2076. t1data ++;
  2077. }
  2078. }
  2079. }
  2080. }
  2081. {
  2082. OPJ_FLOAT64 cumwmsedec =
  2083. opj_t1_encode_cblk(
  2084. t1,
  2085. cblk,
  2086. band->bandno,
  2087. job->compno,
  2088. tilec->numresolutions - 1 - resno,
  2089. tccp->qmfbid,
  2090. band->stepsize,
  2091. tccp->cblksty,
  2092. job->tile->numcomps,
  2093. job->mct_norms,
  2094. job->mct_numcomps);
  2095. if (job->mutex) {
  2096. opj_mutex_lock(job->mutex);
  2097. }
  2098. job->tile->distotile += cumwmsedec;
  2099. if (job->mutex) {
  2100. opj_mutex_unlock(job->mutex);
  2101. }
  2102. }
  2103. opj_free(job);
  2104. }
  2105. OPJ_BOOL opj_t1_encode_cblks(opj_tcd_t* tcd,
  2106. opj_tcd_tile_t *tile,
  2107. opj_tcp_t *tcp,
  2108. const OPJ_FLOAT64 * mct_norms,
  2109. OPJ_UINT32 mct_numcomps
  2110. )
  2111. {
  2112. volatile OPJ_BOOL ret = OPJ_TRUE;
  2113. opj_thread_pool_t* tp = tcd->thread_pool;
  2114. OPJ_UINT32 compno, resno, bandno, precno, cblkno;
  2115. opj_mutex_t* mutex = opj_mutex_create();
  2116. tile->distotile = 0;
  2117. for (compno = 0; compno < tile->numcomps; ++compno) {
  2118. opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
  2119. opj_tccp_t* tccp = &tcp->tccps[compno];
  2120. for (resno = 0; resno < tilec->numresolutions; ++resno) {
  2121. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  2122. for (bandno = 0; bandno < res->numbands; ++bandno) {
  2123. opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
  2124. /* Skip empty bands */
  2125. if (opj_tcd_is_band_empty(band)) {
  2126. continue;
  2127. }
  2128. for (precno = 0; precno < res->pw * res->ph; ++precno) {
  2129. opj_tcd_precinct_t *prc = &band->precincts[precno];
  2130. for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
  2131. opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
  2132. opj_t1_cblk_encode_processing_job_t* job =
  2133. (opj_t1_cblk_encode_processing_job_t*) opj_calloc(1,
  2134. sizeof(opj_t1_cblk_encode_processing_job_t));
  2135. if (!job) {
  2136. ret = OPJ_FALSE;
  2137. goto end;
  2138. }
  2139. job->compno = compno;
  2140. job->tile = tile;
  2141. job->resno = resno;
  2142. job->cblk = cblk;
  2143. job->band = band;
  2144. job->tilec = tilec;
  2145. job->tccp = tccp;
  2146. job->mct_norms = mct_norms;
  2147. job->mct_numcomps = mct_numcomps;
  2148. job->pret = &ret;
  2149. job->mutex = mutex;
  2150. opj_thread_pool_submit_job(tp, opj_t1_cblk_encode_processor, job);
  2151. } /* cblkno */
  2152. } /* precno */
  2153. } /* bandno */
  2154. } /* resno */
  2155. } /* compno */
  2156. end:
  2157. opj_thread_pool_wait_completion(tcd->thread_pool, 0);
  2158. if (mutex) {
  2159. opj_mutex_destroy(mutex);
  2160. }
  2161. return ret;
  2162. }
  2163. /* Returns whether the pass (bpno, passtype) is terminated */
  2164. static int opj_t1_enc_is_term_pass(opj_tcd_cblk_enc_t* cblk,
  2165. OPJ_UINT32 cblksty,
  2166. OPJ_INT32 bpno,
  2167. OPJ_UINT32 passtype)
  2168. {
  2169. /* Is it the last cleanup pass ? */
  2170. if (passtype == 2 && bpno == 0) {
  2171. return OPJ_TRUE;
  2172. }
  2173. if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
  2174. return OPJ_TRUE;
  2175. }
  2176. if ((cblksty & J2K_CCP_CBLKSTY_LAZY)) {
  2177. /* For bypass arithmetic bypass, terminate the 4th cleanup pass */
  2178. if ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2)) {
  2179. return OPJ_TRUE;
  2180. }
  2181. /* and beyond terminate all the magnitude refinement passes (in raw) */
  2182. /* and cleanup passes (in MQC) */
  2183. if ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype > 0)) {
  2184. return OPJ_TRUE;
  2185. }
  2186. }
  2187. return OPJ_FALSE;
  2188. }
  2189. static OPJ_FLOAT64 opj_t1_encode_cblk(opj_t1_t *t1,
  2190. opj_tcd_cblk_enc_t* cblk,
  2191. OPJ_UINT32 orient,
  2192. OPJ_UINT32 compno,
  2193. OPJ_UINT32 level,
  2194. OPJ_UINT32 qmfbid,
  2195. OPJ_FLOAT64 stepsize,
  2196. OPJ_UINT32 cblksty,
  2197. OPJ_UINT32 numcomps,
  2198. const OPJ_FLOAT64 * mct_norms,
  2199. OPJ_UINT32 mct_numcomps)
  2200. {
  2201. OPJ_FLOAT64 cumwmsedec = 0.0;
  2202. opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
  2203. OPJ_UINT32 passno;
  2204. OPJ_INT32 bpno;
  2205. OPJ_UINT32 passtype;
  2206. OPJ_INT32 nmsedec = 0;
  2207. OPJ_INT32 max;
  2208. OPJ_UINT32 i, j;
  2209. OPJ_BYTE type = T1_TYPE_MQ;
  2210. OPJ_FLOAT64 tempwmsedec;
  2211. OPJ_INT32* datap;
  2212. #ifdef EXTRA_DEBUG
  2213. printf("encode_cblk(x=%d,y=%d,x1=%d,y1=%d,orient=%d,compno=%d,level=%d\n",
  2214. cblk->x0, cblk->y0, cblk->x1, cblk->y1, orient, compno, level);
  2215. #endif
  2216. mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
  2217. max = 0;
  2218. datap = t1->data;
  2219. for (j = 0; j < t1->h; ++j) {
  2220. const OPJ_UINT32 w = t1->w;
  2221. for (i = 0; i < w; ++i, ++datap) {
  2222. OPJ_INT32 tmp = *datap;
  2223. if (tmp < 0) {
  2224. OPJ_UINT32 tmp_unsigned;
  2225. if (tmp == INT_MIN) {
  2226. /* To avoid undefined behaviour when negating INT_MIN */
  2227. /* but if we go here, it means we have supplied an input */
  2228. /* with more bit depth than we we can really support. */
  2229. /* Cf https://github.com/uclouvain/openjpeg/issues/1432 */
  2230. tmp = INT_MIN + 1;
  2231. }
  2232. max = opj_int_max(max, -tmp);
  2233. tmp_unsigned = opj_to_smr(tmp);
  2234. memcpy(datap, &tmp_unsigned, sizeof(OPJ_INT32));
  2235. } else {
  2236. max = opj_int_max(max, tmp);
  2237. }
  2238. }
  2239. }
  2240. cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
  2241. T1_NMSEDEC_FRACBITS) : 0;
  2242. if (cblk->numbps == 0) {
  2243. cblk->totalpasses = 0;
  2244. return cumwmsedec;
  2245. }
  2246. bpno = (OPJ_INT32)(cblk->numbps - 1);
  2247. passtype = 2;
  2248. opj_mqc_resetstates(mqc);
  2249. opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  2250. opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  2251. opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  2252. opj_mqc_init_enc(mqc, cblk->data);
  2253. for (passno = 0; bpno >= 0; ++passno) {
  2254. opj_tcd_pass_t *pass = &cblk->passes[passno];
  2255. type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
  2256. (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
  2257. /* If the previous pass was terminating, we need to reset the encoder */
  2258. if (passno > 0 && cblk->passes[passno - 1].term) {
  2259. if (type == T1_TYPE_RAW) {
  2260. opj_mqc_bypass_init_enc(mqc);
  2261. } else {
  2262. opj_mqc_restart_init_enc(mqc);
  2263. }
  2264. }
  2265. switch (passtype) {
  2266. case 0:
  2267. opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
  2268. break;
  2269. case 1:
  2270. opj_t1_enc_refpass(t1, bpno, &nmsedec, type);
  2271. break;
  2272. case 2:
  2273. opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
  2274. /* code switch SEGMARK (i.e. SEGSYM) */
  2275. if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
  2276. opj_mqc_segmark_enc(mqc);
  2277. }
  2278. break;
  2279. }
  2280. tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
  2281. stepsize, numcomps, mct_norms, mct_numcomps) ;
  2282. cumwmsedec += tempwmsedec;
  2283. pass->distortiondec = cumwmsedec;
  2284. if (opj_t1_enc_is_term_pass(cblk, cblksty, bpno, passtype)) {
  2285. /* If it is a terminated pass, terminate it */
  2286. if (type == T1_TYPE_RAW) {
  2287. opj_mqc_bypass_flush_enc(mqc, cblksty & J2K_CCP_CBLKSTY_PTERM);
  2288. } else {
  2289. if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
  2290. opj_mqc_erterm_enc(mqc);
  2291. } else {
  2292. opj_mqc_flush(mqc);
  2293. }
  2294. }
  2295. pass->term = 1;
  2296. pass->rate = opj_mqc_numbytes(mqc);
  2297. } else {
  2298. /* Non terminated pass */
  2299. OPJ_UINT32 rate_extra_bytes;
  2300. if (type == T1_TYPE_RAW) {
  2301. rate_extra_bytes = opj_mqc_bypass_get_extra_bytes(
  2302. mqc, (cblksty & J2K_CCP_CBLKSTY_PTERM));
  2303. } else {
  2304. rate_extra_bytes = 3;
  2305. }
  2306. pass->term = 0;
  2307. pass->rate = opj_mqc_numbytes(mqc) + rate_extra_bytes;
  2308. }
  2309. if (++passtype == 3) {
  2310. passtype = 0;
  2311. bpno--;
  2312. }
  2313. /* Code-switch "RESET" */
  2314. if (cblksty & J2K_CCP_CBLKSTY_RESET) {
  2315. opj_mqc_reset_enc(mqc);
  2316. }
  2317. }
  2318. cblk->totalpasses = passno;
  2319. if (cblk->totalpasses) {
  2320. /* Make sure that pass rates are increasing */
  2321. OPJ_UINT32 last_pass_rate = opj_mqc_numbytes(mqc);
  2322. for (passno = cblk->totalpasses; passno > 0;) {
  2323. opj_tcd_pass_t *pass = &cblk->passes[--passno];
  2324. if (pass->rate > last_pass_rate) {
  2325. pass->rate = last_pass_rate;
  2326. } else {
  2327. last_pass_rate = pass->rate;
  2328. }
  2329. }
  2330. }
  2331. for (passno = 0; passno < cblk->totalpasses; passno++) {
  2332. opj_tcd_pass_t *pass = &cblk->passes[passno];
  2333. /* Prevent generation of FF as last data byte of a pass*/
  2334. /* For terminating passes, the flushing procedure ensured this already */
  2335. assert(pass->rate > 0);
  2336. if (cblk->data[pass->rate - 1] == 0xFF) {
  2337. pass->rate--;
  2338. }
  2339. pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
  2340. }
  2341. #ifdef EXTRA_DEBUG
  2342. printf(" len=%d\n", (cblk->totalpasses) ? opj_mqc_numbytes(mqc) : 0);
  2343. /* Check that there not 0xff >=0x90 sequences */
  2344. if (cblk->totalpasses) {
  2345. OPJ_UINT32 i;
  2346. OPJ_UINT32 len = opj_mqc_numbytes(mqc);
  2347. for (i = 1; i < len; ++i) {
  2348. if (cblk->data[i - 1] == 0xff && cblk->data[i] >= 0x90) {
  2349. printf("0xff %02x at offset %d\n", cblk->data[i], i - 1);
  2350. abort();
  2351. }
  2352. }
  2353. }
  2354. #endif
  2355. return cumwmsedec;
  2356. }