dwt.c 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756
  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, Jonathan Ballard <dzonatas@dzonux.net>
  15. * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
  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. #include <assert.h>
  41. #define OPJ_SKIP_POISON
  42. #include "opj_includes.h"
  43. #ifdef __SSE__
  44. #include <xmmintrin.h>
  45. #endif
  46. #ifdef __SSE2__
  47. #include <emmintrin.h>
  48. #endif
  49. #ifdef __SSSE3__
  50. #include <tmmintrin.h>
  51. #endif
  52. #ifdef __AVX2__
  53. #include <immintrin.h>
  54. #endif
  55. #if defined(__GNUC__)
  56. #pragma GCC poison malloc calloc realloc free
  57. #endif
  58. /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
  59. /*@{*/
  60. #define OPJ_WS(i) v->mem[(i)*2]
  61. #define OPJ_WD(i) v->mem[(1+(i)*2)]
  62. #ifdef __AVX2__
  63. /** Number of int32 values in a AVX2 register */
  64. #define VREG_INT_COUNT 8
  65. #else
  66. /** Number of int32 values in a SSE2 register */
  67. #define VREG_INT_COUNT 4
  68. #endif
  69. /** Number of columns that we can process in parallel in the vertical pass */
  70. #define PARALLEL_COLS_53 (2*VREG_INT_COUNT)
  71. /** @name Local data structures */
  72. /*@{*/
  73. typedef struct dwt_local {
  74. OPJ_INT32* mem;
  75. OPJ_INT32 dn; /* number of elements in high pass band */
  76. OPJ_INT32 sn; /* number of elements in low pass band */
  77. OPJ_INT32 cas; /* 0 = start on even coord, 1 = start on odd coord */
  78. } opj_dwt_t;
  79. #define NB_ELTS_V8 8
  80. typedef union {
  81. OPJ_FLOAT32 f[NB_ELTS_V8];
  82. } opj_v8_t;
  83. typedef struct v8dwt_local {
  84. opj_v8_t* wavelet ;
  85. OPJ_INT32 dn ; /* number of elements in high pass band */
  86. OPJ_INT32 sn ; /* number of elements in low pass band */
  87. OPJ_INT32 cas ; /* 0 = start on even coord, 1 = start on odd coord */
  88. OPJ_UINT32 win_l_x0; /* start coord in low pass band */
  89. OPJ_UINT32 win_l_x1; /* end coord in low pass band */
  90. OPJ_UINT32 win_h_x0; /* start coord in high pass band */
  91. OPJ_UINT32 win_h_x1; /* end coord in high pass band */
  92. } opj_v8dwt_t ;
  93. /* From table F.4 from the standard */
  94. static const OPJ_FLOAT32 opj_dwt_alpha = -1.586134342f;
  95. static const OPJ_FLOAT32 opj_dwt_beta = -0.052980118f;
  96. static const OPJ_FLOAT32 opj_dwt_gamma = 0.882911075f;
  97. static const OPJ_FLOAT32 opj_dwt_delta = 0.443506852f;
  98. static const OPJ_FLOAT32 opj_K = 1.230174105f;
  99. static const OPJ_FLOAT32 opj_invK = (OPJ_FLOAT32)(1.0 / 1.230174105);
  100. /*@}*/
  101. /** @name Local static functions */
  102. /*@{*/
  103. /**
  104. Forward lazy transform (horizontal)
  105. */
  106. static void opj_dwt_deinterleave_h(const OPJ_INT32 * OPJ_RESTRICT a,
  107. OPJ_INT32 * OPJ_RESTRICT b,
  108. OPJ_INT32 dn,
  109. OPJ_INT32 sn, OPJ_INT32 cas);
  110. /**
  111. Forward 9-7 wavelet transform in 1-D
  112. */
  113. static void opj_dwt_encode_1_real(void *a, OPJ_INT32 dn, OPJ_INT32 sn,
  114. OPJ_INT32 cas);
  115. /**
  116. Explicit calculation of the Quantization Stepsizes
  117. */
  118. static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
  119. opj_stepsize_t *bandno_stepsize);
  120. /**
  121. Inverse wavelet transform in 2-D.
  122. */
  123. static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
  124. opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i);
  125. static OPJ_BOOL opj_dwt_decode_partial_tile(
  126. opj_tcd_tilecomp_t* tilec,
  127. OPJ_UINT32 numres);
  128. /* Forward transform, for the vertical pass, processing cols columns */
  129. /* where cols <= NB_ELTS_V8 */
  130. /* Where void* is a OPJ_INT32* for 5x3 and OPJ_FLOAT32* for 9x7 */
  131. typedef void (*opj_encode_and_deinterleave_v_fnptr_type)(
  132. void *array,
  133. void *tmp,
  134. OPJ_UINT32 height,
  135. OPJ_BOOL even,
  136. OPJ_UINT32 stride_width,
  137. OPJ_UINT32 cols);
  138. /* Where void* is a OPJ_INT32* for 5x3 and OPJ_FLOAT32* for 9x7 */
  139. typedef void (*opj_encode_and_deinterleave_h_one_row_fnptr_type)(
  140. void *row,
  141. void *tmp,
  142. OPJ_UINT32 width,
  143. OPJ_BOOL even);
  144. static OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp,
  145. opj_tcd_tilecomp_t * tilec,
  146. opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v,
  147. opj_encode_and_deinterleave_h_one_row_fnptr_type
  148. p_encode_and_deinterleave_h_one_row);
  149. static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
  150. OPJ_UINT32 i);
  151. /* <summary> */
  152. /* Inverse 9-7 wavelet transform in 1-D. */
  153. /* </summary> */
  154. /*@}*/
  155. /*@}*/
  156. #define OPJ_S(i) a[(i)*2]
  157. #define OPJ_D(i) a[(1+(i)*2)]
  158. #define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i)))
  159. #define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i)))
  160. /* new */
  161. #define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i)))
  162. #define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i)))
  163. /* <summary> */
  164. /* This table contains the norms of the 5-3 wavelets for different bands. */
  165. /* </summary> */
  166. /* FIXME! the array should really be extended up to 33 resolution levels */
  167. /* See https://github.com/uclouvain/openjpeg/issues/493 */
  168. static const OPJ_FLOAT64 opj_dwt_norms[4][10] = {
  169. {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
  170. {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
  171. {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
  172. {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
  173. };
  174. /* <summary> */
  175. /* This table contains the norms of the 9-7 wavelets for different bands. */
  176. /* </summary> */
  177. /* FIXME! the array should really be extended up to 33 resolution levels */
  178. /* See https://github.com/uclouvain/openjpeg/issues/493 */
  179. static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = {
  180. {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
  181. {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
  182. {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
  183. {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
  184. };
  185. /*
  186. ==========================================================
  187. local functions
  188. ==========================================================
  189. */
  190. /* <summary> */
  191. /* Forward lazy transform (horizontal). */
  192. /* </summary> */
  193. static void opj_dwt_deinterleave_h(const OPJ_INT32 * OPJ_RESTRICT a,
  194. OPJ_INT32 * OPJ_RESTRICT b,
  195. OPJ_INT32 dn,
  196. OPJ_INT32 sn, OPJ_INT32 cas)
  197. {
  198. OPJ_INT32 i;
  199. OPJ_INT32 * OPJ_RESTRICT l_dest = b;
  200. const OPJ_INT32 * OPJ_RESTRICT l_src = a + cas;
  201. for (i = 0; i < sn; ++i) {
  202. *l_dest++ = *l_src;
  203. l_src += 2;
  204. }
  205. l_dest = b + sn;
  206. l_src = a + 1 - cas;
  207. for (i = 0; i < dn; ++i) {
  208. *l_dest++ = *l_src;
  209. l_src += 2;
  210. }
  211. }
  212. #ifdef STANDARD_SLOW_VERSION
  213. /* <summary> */
  214. /* Inverse lazy transform (horizontal). */
  215. /* </summary> */
  216. static void opj_dwt_interleave_h(const opj_dwt_t* h, OPJ_INT32 *a)
  217. {
  218. const OPJ_INT32 *ai = a;
  219. OPJ_INT32 *bi = h->mem + h->cas;
  220. OPJ_INT32 i = h->sn;
  221. while (i--) {
  222. *bi = *(ai++);
  223. bi += 2;
  224. }
  225. ai = a + h->sn;
  226. bi = h->mem + 1 - h->cas;
  227. i = h->dn ;
  228. while (i--) {
  229. *bi = *(ai++);
  230. bi += 2;
  231. }
  232. }
  233. /* <summary> */
  234. /* Inverse lazy transform (vertical). */
  235. /* </summary> */
  236. static void opj_dwt_interleave_v(const opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x)
  237. {
  238. const OPJ_INT32 *ai = a;
  239. OPJ_INT32 *bi = v->mem + v->cas;
  240. OPJ_INT32 i = v->sn;
  241. while (i--) {
  242. *bi = *ai;
  243. bi += 2;
  244. ai += x;
  245. }
  246. ai = a + (v->sn * (OPJ_SIZE_T)x);
  247. bi = v->mem + 1 - v->cas;
  248. i = v->dn ;
  249. while (i--) {
  250. *bi = *ai;
  251. bi += 2;
  252. ai += x;
  253. }
  254. }
  255. #endif /* STANDARD_SLOW_VERSION */
  256. #ifdef STANDARD_SLOW_VERSION
  257. /* <summary> */
  258. /* Inverse 5-3 wavelet transform in 1-D. */
  259. /* </summary> */
  260. static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
  261. OPJ_INT32 cas)
  262. {
  263. OPJ_INT32 i;
  264. if (!cas) {
  265. if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
  266. for (i = 0; i < sn; i++) {
  267. OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
  268. }
  269. for (i = 0; i < dn; i++) {
  270. OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
  271. }
  272. }
  273. } else {
  274. if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
  275. OPJ_S(0) /= 2;
  276. } else {
  277. for (i = 0; i < sn; i++) {
  278. OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
  279. }
  280. for (i = 0; i < dn; i++) {
  281. OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
  282. }
  283. }
  284. }
  285. }
  286. static void opj_dwt_decode_1(const opj_dwt_t *v)
  287. {
  288. opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
  289. }
  290. #endif /* STANDARD_SLOW_VERSION */
  291. #if !defined(STANDARD_SLOW_VERSION)
  292. static void opj_idwt53_h_cas0(OPJ_INT32* tmp,
  293. const OPJ_INT32 sn,
  294. const OPJ_INT32 len,
  295. OPJ_INT32* tiledp)
  296. {
  297. OPJ_INT32 i, j;
  298. const OPJ_INT32* in_even = &tiledp[0];
  299. const OPJ_INT32* in_odd = &tiledp[sn];
  300. #ifdef TWO_PASS_VERSION
  301. /* For documentation purpose: performs lifting in two iterations, */
  302. /* but without explicit interleaving */
  303. assert(len > 1);
  304. /* Even */
  305. tmp[0] = in_even[0] - ((in_odd[0] + 1) >> 1);
  306. for (i = 2, j = 0; i <= len - 2; i += 2, j++) {
  307. tmp[i] = in_even[j + 1] - ((in_odd[j] + in_odd[j + 1] + 2) >> 2);
  308. }
  309. if (len & 1) { /* if len is odd */
  310. tmp[len - 1] = in_even[(len - 1) / 2] - ((in_odd[(len - 2) / 2] + 1) >> 1);
  311. }
  312. /* Odd */
  313. for (i = 1, j = 0; i < len - 1; i += 2, j++) {
  314. tmp[i] = in_odd[j] + ((tmp[i - 1] + tmp[i + 1]) >> 1);
  315. }
  316. if (!(len & 1)) { /* if len is even */
  317. tmp[len - 1] = in_odd[(len - 1) / 2] + tmp[len - 2];
  318. }
  319. #else
  320. OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
  321. assert(len > 1);
  322. /* Improved version of the TWO_PASS_VERSION: */
  323. /* Performs lifting in one single iteration. Saves memory */
  324. /* accesses and explicit interleaving. */
  325. s1n = in_even[0];
  326. d1n = in_odd[0];
  327. s0n = s1n - ((d1n + 1) >> 1);
  328. for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
  329. d1c = d1n;
  330. s0c = s0n;
  331. s1n = in_even[j];
  332. d1n = in_odd[j];
  333. s0n = s1n - ((d1c + d1n + 2) >> 2);
  334. tmp[i ] = s0c;
  335. tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
  336. }
  337. tmp[i] = s0n;
  338. if (len & 1) {
  339. tmp[len - 1] = in_even[(len - 1) / 2] - ((d1n + 1) >> 1);
  340. tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
  341. } else {
  342. tmp[len - 1] = d1n + s0n;
  343. }
  344. #endif
  345. memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
  346. }
  347. static void opj_idwt53_h_cas1(OPJ_INT32* tmp,
  348. const OPJ_INT32 sn,
  349. const OPJ_INT32 len,
  350. OPJ_INT32* tiledp)
  351. {
  352. OPJ_INT32 i, j;
  353. const OPJ_INT32* in_even = &tiledp[sn];
  354. const OPJ_INT32* in_odd = &tiledp[0];
  355. #ifdef TWO_PASS_VERSION
  356. /* For documentation purpose: performs lifting in two iterations, */
  357. /* but without explicit interleaving */
  358. assert(len > 2);
  359. /* Odd */
  360. for (i = 1, j = 0; i < len - 1; i += 2, j++) {
  361. tmp[i] = in_odd[j] - ((in_even[j] + in_even[j + 1] + 2) >> 2);
  362. }
  363. if (!(len & 1)) {
  364. tmp[len - 1] = in_odd[len / 2 - 1] - ((in_even[len / 2 - 1] + 1) >> 1);
  365. }
  366. /* Even */
  367. tmp[0] = in_even[0] + tmp[1];
  368. for (i = 2, j = 1; i < len - 1; i += 2, j++) {
  369. tmp[i] = in_even[j] + ((tmp[i + 1] + tmp[i - 1]) >> 1);
  370. }
  371. if (len & 1) {
  372. tmp[len - 1] = in_even[len / 2] + tmp[len - 2];
  373. }
  374. #else
  375. OPJ_INT32 s1, s2, dc, dn;
  376. assert(len > 2);
  377. /* Improved version of the TWO_PASS_VERSION: */
  378. /* Performs lifting in one single iteration. Saves memory */
  379. /* accesses and explicit interleaving. */
  380. s1 = in_even[1];
  381. dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
  382. tmp[0] = in_even[0] + dc;
  383. for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
  384. s2 = in_even[j + 1];
  385. dn = in_odd[j] - ((s1 + s2 + 2) >> 2);
  386. tmp[i ] = dc;
  387. tmp[i + 1] = s1 + ((dn + dc) >> 1);
  388. dc = dn;
  389. s1 = s2;
  390. }
  391. tmp[i] = dc;
  392. if (!(len & 1)) {
  393. dn = in_odd[len / 2 - 1] - ((s1 + 1) >> 1);
  394. tmp[len - 2] = s1 + ((dn + dc) >> 1);
  395. tmp[len - 1] = dn;
  396. } else {
  397. tmp[len - 1] = s1 + dc;
  398. }
  399. #endif
  400. memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32));
  401. }
  402. #endif /* !defined(STANDARD_SLOW_VERSION) */
  403. /* <summary> */
  404. /* Inverse 5-3 wavelet transform in 1-D for one row. */
  405. /* </summary> */
  406. /* Performs interleave, inverse wavelet transform and copy back to buffer */
  407. static void opj_idwt53_h(const opj_dwt_t *dwt,
  408. OPJ_INT32* tiledp)
  409. {
  410. #ifdef STANDARD_SLOW_VERSION
  411. /* For documentation purpose */
  412. opj_dwt_interleave_h(dwt, tiledp);
  413. opj_dwt_decode_1(dwt);
  414. memcpy(tiledp, dwt->mem, (OPJ_UINT32)(dwt->sn + dwt->dn) * sizeof(OPJ_INT32));
  415. #else
  416. const OPJ_INT32 sn = dwt->sn;
  417. const OPJ_INT32 len = sn + dwt->dn;
  418. if (dwt->cas == 0) { /* Left-most sample is on even coordinate */
  419. if (len > 1) {
  420. opj_idwt53_h_cas0(dwt->mem, sn, len, tiledp);
  421. } else {
  422. /* Unmodified value */
  423. }
  424. } else { /* Left-most sample is on odd coordinate */
  425. if (len == 1) {
  426. tiledp[0] /= 2;
  427. } else if (len == 2) {
  428. OPJ_INT32* out = dwt->mem;
  429. const OPJ_INT32* in_even = &tiledp[sn];
  430. const OPJ_INT32* in_odd = &tiledp[0];
  431. out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
  432. out[0] = in_even[0] + out[1];
  433. memcpy(tiledp, dwt->mem, (OPJ_UINT32)len * sizeof(OPJ_INT32));
  434. } else if (len > 2) {
  435. opj_idwt53_h_cas1(dwt->mem, sn, len, tiledp);
  436. }
  437. }
  438. #endif
  439. }
  440. #if (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION)
  441. /* Conveniency macros to improve the readabilty of the formulas */
  442. #if __AVX2__
  443. #define VREG __m256i
  444. #define LOAD_CST(x) _mm256_set1_epi32(x)
  445. #define LOAD(x) _mm256_load_si256((const VREG*)(x))
  446. #define LOADU(x) _mm256_loadu_si256((const VREG*)(x))
  447. #define STORE(x,y) _mm256_store_si256((VREG*)(x),(y))
  448. #define STOREU(x,y) _mm256_storeu_si256((VREG*)(x),(y))
  449. #define ADD(x,y) _mm256_add_epi32((x),(y))
  450. #define SUB(x,y) _mm256_sub_epi32((x),(y))
  451. #define SAR(x,y) _mm256_srai_epi32((x),(y))
  452. #else
  453. #define VREG __m128i
  454. #define LOAD_CST(x) _mm_set1_epi32(x)
  455. #define LOAD(x) _mm_load_si128((const VREG*)(x))
  456. #define LOADU(x) _mm_loadu_si128((const VREG*)(x))
  457. #define STORE(x,y) _mm_store_si128((VREG*)(x),(y))
  458. #define STOREU(x,y) _mm_storeu_si128((VREG*)(x),(y))
  459. #define ADD(x,y) _mm_add_epi32((x),(y))
  460. #define SUB(x,y) _mm_sub_epi32((x),(y))
  461. #define SAR(x,y) _mm_srai_epi32((x),(y))
  462. #endif
  463. #define ADD3(x,y,z) ADD(ADD(x,y),z)
  464. static
  465. void opj_idwt53_v_final_memcpy(OPJ_INT32* tiledp_col,
  466. const OPJ_INT32* tmp,
  467. OPJ_INT32 len,
  468. OPJ_SIZE_T stride)
  469. {
  470. OPJ_INT32 i;
  471. for (i = 0; i < len; ++i) {
  472. /* A memcpy(&tiledp_col[i * stride + 0],
  473. &tmp[PARALLEL_COLS_53 * i + 0],
  474. PARALLEL_COLS_53 * sizeof(OPJ_INT32))
  475. would do but would be a tiny bit slower.
  476. We can take here advantage of our knowledge of alignment */
  477. STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + 0],
  478. LOAD(&tmp[PARALLEL_COLS_53 * i + 0]));
  479. STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + VREG_INT_COUNT],
  480. LOAD(&tmp[PARALLEL_COLS_53 * i + VREG_INT_COUNT]));
  481. }
  482. }
  483. /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
  484. * 16 in AVX2, when top-most pixel is on even coordinate */
  485. static void opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(
  486. OPJ_INT32* tmp,
  487. const OPJ_INT32 sn,
  488. const OPJ_INT32 len,
  489. OPJ_INT32* tiledp_col,
  490. const OPJ_SIZE_T stride)
  491. {
  492. const OPJ_INT32* in_even = &tiledp_col[0];
  493. const OPJ_INT32* in_odd = &tiledp_col[(OPJ_SIZE_T)sn * stride];
  494. OPJ_INT32 i;
  495. OPJ_SIZE_T j;
  496. VREG d1c_0, d1n_0, s1n_0, s0c_0, s0n_0;
  497. VREG d1c_1, d1n_1, s1n_1, s0c_1, s0n_1;
  498. const VREG two = LOAD_CST(2);
  499. assert(len > 1);
  500. #if __AVX2__
  501. assert(PARALLEL_COLS_53 == 16);
  502. assert(VREG_INT_COUNT == 8);
  503. #else
  504. assert(PARALLEL_COLS_53 == 8);
  505. assert(VREG_INT_COUNT == 4);
  506. #endif
  507. /* Note: loads of input even/odd values must be done in a unaligned */
  508. /* fashion. But stores in tmp can be done with aligned store, since */
  509. /* the temporary buffer is properly aligned */
  510. assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
  511. s1n_0 = LOADU(in_even + 0);
  512. s1n_1 = LOADU(in_even + VREG_INT_COUNT);
  513. d1n_0 = LOADU(in_odd);
  514. d1n_1 = LOADU(in_odd + VREG_INT_COUNT);
  515. /* s0n = s1n - ((d1n + 1) >> 1); <==> */
  516. /* s0n = s1n - ((d1n + d1n + 2) >> 2); */
  517. s0n_0 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
  518. s0n_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
  519. for (i = 0, j = 1; i < (len - 3); i += 2, j++) {
  520. d1c_0 = d1n_0;
  521. s0c_0 = s0n_0;
  522. d1c_1 = d1n_1;
  523. s0c_1 = s0n_1;
  524. s1n_0 = LOADU(in_even + j * stride);
  525. s1n_1 = LOADU(in_even + j * stride + VREG_INT_COUNT);
  526. d1n_0 = LOADU(in_odd + j * stride);
  527. d1n_1 = LOADU(in_odd + j * stride + VREG_INT_COUNT);
  528. /*s0n = s1n - ((d1c + d1n + 2) >> 2);*/
  529. s0n_0 = SUB(s1n_0, SAR(ADD3(d1c_0, d1n_0, two), 2));
  530. s0n_1 = SUB(s1n_1, SAR(ADD3(d1c_1, d1n_1, two), 2));
  531. STORE(tmp + PARALLEL_COLS_53 * (i + 0), s0c_0);
  532. STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0c_1);
  533. /* d1c + ((s0c + s0n) >> 1) */
  534. STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
  535. ADD(d1c_0, SAR(ADD(s0c_0, s0n_0), 1)));
  536. STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
  537. ADD(d1c_1, SAR(ADD(s0c_1, s0n_1), 1)));
  538. }
  539. STORE(tmp + PARALLEL_COLS_53 * (i + 0) + 0, s0n_0);
  540. STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0n_1);
  541. if (len & 1) {
  542. VREG tmp_len_minus_1;
  543. s1n_0 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride);
  544. /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
  545. tmp_len_minus_1 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2));
  546. STORE(tmp + PARALLEL_COLS_53 * (len - 1), tmp_len_minus_1);
  547. /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
  548. STORE(tmp + PARALLEL_COLS_53 * (len - 2),
  549. ADD(d1n_0, SAR(ADD(s0n_0, tmp_len_minus_1), 1)));
  550. s1n_1 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride + VREG_INT_COUNT);
  551. /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */
  552. tmp_len_minus_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2));
  553. STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
  554. tmp_len_minus_1);
  555. /* d1n + ((s0n + tmp_len_minus_1) >> 1) */
  556. STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
  557. ADD(d1n_1, SAR(ADD(s0n_1, tmp_len_minus_1), 1)));
  558. } else {
  559. STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0,
  560. ADD(d1n_0, s0n_0));
  561. STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
  562. ADD(d1n_1, s0n_1));
  563. }
  564. opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
  565. }
  566. /** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or
  567. * 16 in AVX2, when top-most pixel is on odd coordinate */
  568. static void opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(
  569. OPJ_INT32* tmp,
  570. const OPJ_INT32 sn,
  571. const OPJ_INT32 len,
  572. OPJ_INT32* tiledp_col,
  573. const OPJ_SIZE_T stride)
  574. {
  575. OPJ_INT32 i;
  576. OPJ_SIZE_T j;
  577. VREG s1_0, s2_0, dc_0, dn_0;
  578. VREG s1_1, s2_1, dc_1, dn_1;
  579. const VREG two = LOAD_CST(2);
  580. const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
  581. const OPJ_INT32* in_odd = &tiledp_col[0];
  582. assert(len > 2);
  583. #if __AVX2__
  584. assert(PARALLEL_COLS_53 == 16);
  585. assert(VREG_INT_COUNT == 8);
  586. #else
  587. assert(PARALLEL_COLS_53 == 8);
  588. assert(VREG_INT_COUNT == 4);
  589. #endif
  590. /* Note: loads of input even/odd values must be done in a unaligned */
  591. /* fashion. But stores in tmp can be done with aligned store, since */
  592. /* the temporary buffer is properly aligned */
  593. assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0);
  594. s1_0 = LOADU(in_even + stride);
  595. /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
  596. dc_0 = SUB(LOADU(in_odd + 0),
  597. SAR(ADD3(LOADU(in_even + 0), s1_0, two), 2));
  598. STORE(tmp + PARALLEL_COLS_53 * 0, ADD(LOADU(in_even + 0), dc_0));
  599. s1_1 = LOADU(in_even + stride + VREG_INT_COUNT);
  600. /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */
  601. dc_1 = SUB(LOADU(in_odd + VREG_INT_COUNT),
  602. SAR(ADD3(LOADU(in_even + VREG_INT_COUNT), s1_1, two), 2));
  603. STORE(tmp + PARALLEL_COLS_53 * 0 + VREG_INT_COUNT,
  604. ADD(LOADU(in_even + VREG_INT_COUNT), dc_1));
  605. for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
  606. s2_0 = LOADU(in_even + (j + 1) * stride);
  607. s2_1 = LOADU(in_even + (j + 1) * stride + VREG_INT_COUNT);
  608. /* dn = in_odd[j * stride] - ((s1 + s2 + 2) >> 2); */
  609. dn_0 = SUB(LOADU(in_odd + j * stride),
  610. SAR(ADD3(s1_0, s2_0, two), 2));
  611. dn_1 = SUB(LOADU(in_odd + j * stride + VREG_INT_COUNT),
  612. SAR(ADD3(s1_1, s2_1, two), 2));
  613. STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
  614. STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
  615. /* tmp[i + 1] = s1 + ((dn + dc) >> 1); */
  616. STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0,
  617. ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
  618. STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT,
  619. ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
  620. dc_0 = dn_0;
  621. s1_0 = s2_0;
  622. dc_1 = dn_1;
  623. s1_1 = s2_1;
  624. }
  625. STORE(tmp + PARALLEL_COLS_53 * i, dc_0);
  626. STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1);
  627. if (!(len & 1)) {
  628. /*dn = in_odd[(len / 2 - 1) * stride] - ((s1 + 1) >> 1); */
  629. dn_0 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride),
  630. SAR(ADD3(s1_0, s1_0, two), 2));
  631. dn_1 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride + VREG_INT_COUNT),
  632. SAR(ADD3(s1_1, s1_1, two), 2));
  633. /* tmp[len - 2] = s1 + ((dn + dc) >> 1); */
  634. STORE(tmp + PARALLEL_COLS_53 * (len - 2) + 0,
  635. ADD(s1_0, SAR(ADD(dn_0, dc_0), 1)));
  636. STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT,
  637. ADD(s1_1, SAR(ADD(dn_1, dc_1), 1)));
  638. STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, dn_0);
  639. STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, dn_1);
  640. } else {
  641. STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, ADD(s1_0, dc_0));
  642. STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT,
  643. ADD(s1_1, dc_1));
  644. }
  645. opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride);
  646. }
  647. #undef VREG
  648. #undef LOAD_CST
  649. #undef LOADU
  650. #undef LOAD
  651. #undef STORE
  652. #undef STOREU
  653. #undef ADD
  654. #undef ADD3
  655. #undef SUB
  656. #undef SAR
  657. #endif /* (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION) */
  658. #if !defined(STANDARD_SLOW_VERSION)
  659. /** Vertical inverse 5x3 wavelet transform for one column, when top-most
  660. * pixel is on even coordinate */
  661. static void opj_idwt3_v_cas0(OPJ_INT32* tmp,
  662. const OPJ_INT32 sn,
  663. const OPJ_INT32 len,
  664. OPJ_INT32* tiledp_col,
  665. const OPJ_SIZE_T stride)
  666. {
  667. OPJ_INT32 i, j;
  668. OPJ_INT32 d1c, d1n, s1n, s0c, s0n;
  669. assert(len > 1);
  670. /* Performs lifting in one single iteration. Saves memory */
  671. /* accesses and explicit interleaving. */
  672. s1n = tiledp_col[0];
  673. d1n = tiledp_col[(OPJ_SIZE_T)sn * stride];
  674. s0n = s1n - ((d1n + 1) >> 1);
  675. for (i = 0, j = 0; i < (len - 3); i += 2, j++) {
  676. d1c = d1n;
  677. s0c = s0n;
  678. s1n = tiledp_col[(OPJ_SIZE_T)(j + 1) * stride];
  679. d1n = tiledp_col[(OPJ_SIZE_T)(sn + j + 1) * stride];
  680. s0n = s1n - ((d1c + d1n + 2) >> 2);
  681. tmp[i ] = s0c;
  682. tmp[i + 1] = d1c + ((s0c + s0n) >> 1);
  683. }
  684. tmp[i] = s0n;
  685. if (len & 1) {
  686. tmp[len - 1] =
  687. tiledp_col[(OPJ_SIZE_T)((len - 1) / 2) * stride] -
  688. ((d1n + 1) >> 1);
  689. tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1);
  690. } else {
  691. tmp[len - 1] = d1n + s0n;
  692. }
  693. for (i = 0; i < len; ++i) {
  694. tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i];
  695. }
  696. }
  697. /** Vertical inverse 5x3 wavelet transform for one column, when top-most
  698. * pixel is on odd coordinate */
  699. static void opj_idwt3_v_cas1(OPJ_INT32* tmp,
  700. const OPJ_INT32 sn,
  701. const OPJ_INT32 len,
  702. OPJ_INT32* tiledp_col,
  703. const OPJ_SIZE_T stride)
  704. {
  705. OPJ_INT32 i, j;
  706. OPJ_INT32 s1, s2, dc, dn;
  707. const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
  708. const OPJ_INT32* in_odd = &tiledp_col[0];
  709. assert(len > 2);
  710. /* Performs lifting in one single iteration. Saves memory */
  711. /* accesses and explicit interleaving. */
  712. s1 = in_even[stride];
  713. dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2);
  714. tmp[0] = in_even[0] + dc;
  715. for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) {
  716. s2 = in_even[(OPJ_SIZE_T)(j + 1) * stride];
  717. dn = in_odd[(OPJ_SIZE_T)j * stride] - ((s1 + s2 + 2) >> 2);
  718. tmp[i ] = dc;
  719. tmp[i + 1] = s1 + ((dn + dc) >> 1);
  720. dc = dn;
  721. s1 = s2;
  722. }
  723. tmp[i] = dc;
  724. if (!(len & 1)) {
  725. dn = in_odd[(OPJ_SIZE_T)(len / 2 - 1) * stride] - ((s1 + 1) >> 1);
  726. tmp[len - 2] = s1 + ((dn + dc) >> 1);
  727. tmp[len - 1] = dn;
  728. } else {
  729. tmp[len - 1] = s1 + dc;
  730. }
  731. for (i = 0; i < len; ++i) {
  732. tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i];
  733. }
  734. }
  735. #endif /* !defined(STANDARD_SLOW_VERSION) */
  736. /* <summary> */
  737. /* Inverse vertical 5-3 wavelet transform in 1-D for several columns. */
  738. /* </summary> */
  739. /* Performs interleave, inverse wavelet transform and copy back to buffer */
  740. static void opj_idwt53_v(const opj_dwt_t *dwt,
  741. OPJ_INT32* tiledp_col,
  742. OPJ_SIZE_T stride,
  743. OPJ_INT32 nb_cols)
  744. {
  745. #ifdef STANDARD_SLOW_VERSION
  746. /* For documentation purpose */
  747. OPJ_INT32 k, c;
  748. for (c = 0; c < nb_cols; c ++) {
  749. opj_dwt_interleave_v(dwt, tiledp_col + c, stride);
  750. opj_dwt_decode_1(dwt);
  751. for (k = 0; k < dwt->sn + dwt->dn; ++k) {
  752. tiledp_col[c + k * stride] = dwt->mem[k];
  753. }
  754. }
  755. #else
  756. const OPJ_INT32 sn = dwt->sn;
  757. const OPJ_INT32 len = sn + dwt->dn;
  758. if (dwt->cas == 0) {
  759. /* If len == 1, unmodified value */
  760. #if (defined(__SSE2__) || defined(__AVX2__))
  761. if (len > 1 && nb_cols == PARALLEL_COLS_53) {
  762. /* Same as below general case, except that thanks to SSE2/AVX2 */
  763. /* we can efficiently process 8/16 columns in parallel */
  764. opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
  765. return;
  766. }
  767. #endif
  768. if (len > 1) {
  769. OPJ_INT32 c;
  770. for (c = 0; c < nb_cols; c++, tiledp_col++) {
  771. opj_idwt3_v_cas0(dwt->mem, sn, len, tiledp_col, stride);
  772. }
  773. return;
  774. }
  775. } else {
  776. if (len == 1) {
  777. OPJ_INT32 c;
  778. for (c = 0; c < nb_cols; c++, tiledp_col++) {
  779. tiledp_col[0] /= 2;
  780. }
  781. return;
  782. }
  783. if (len == 2) {
  784. OPJ_INT32 c;
  785. OPJ_INT32* out = dwt->mem;
  786. for (c = 0; c < nb_cols; c++, tiledp_col++) {
  787. OPJ_INT32 i;
  788. const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride];
  789. const OPJ_INT32* in_odd = &tiledp_col[0];
  790. out[1] = in_odd[0] - ((in_even[0] + 1) >> 1);
  791. out[0] = in_even[0] + out[1];
  792. for (i = 0; i < len; ++i) {
  793. tiledp_col[(OPJ_SIZE_T)i * stride] = out[i];
  794. }
  795. }
  796. return;
  797. }
  798. #if (defined(__SSE2__) || defined(__AVX2__))
  799. if (len > 2 && nb_cols == PARALLEL_COLS_53) {
  800. /* Same as below general case, except that thanks to SSE2/AVX2 */
  801. /* we can efficiently process 8/16 columns in parallel */
  802. opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride);
  803. return;
  804. }
  805. #endif
  806. if (len > 2) {
  807. OPJ_INT32 c;
  808. for (c = 0; c < nb_cols; c++, tiledp_col++) {
  809. opj_idwt3_v_cas1(dwt->mem, sn, len, tiledp_col, stride);
  810. }
  811. return;
  812. }
  813. }
  814. #endif
  815. }
  816. #if 0
  817. static void opj_dwt_encode_step1(OPJ_FLOAT32* fw,
  818. OPJ_UINT32 end,
  819. const OPJ_FLOAT32 c)
  820. {
  821. OPJ_UINT32 i = 0;
  822. for (; i < end; ++i) {
  823. fw[0] *= c;
  824. fw += 2;
  825. }
  826. }
  827. #else
  828. static void opj_dwt_encode_step1_combined(OPJ_FLOAT32* fw,
  829. OPJ_UINT32 iters_c1,
  830. OPJ_UINT32 iters_c2,
  831. const OPJ_FLOAT32 c1,
  832. const OPJ_FLOAT32 c2)
  833. {
  834. OPJ_UINT32 i = 0;
  835. const OPJ_UINT32 iters_common = opj_uint_min(iters_c1, iters_c2);
  836. assert((((OPJ_SIZE_T)fw) & 0xf) == 0);
  837. assert(opj_int_abs((OPJ_INT32)iters_c1 - (OPJ_INT32)iters_c2) <= 1);
  838. for (; i + 3 < iters_common; i += 4) {
  839. #ifdef __SSE__
  840. const __m128 vcst = _mm_set_ps(c2, c1, c2, c1);
  841. *(__m128*)fw = _mm_mul_ps(*(__m128*)fw, vcst);
  842. *(__m128*)(fw + 4) = _mm_mul_ps(*(__m128*)(fw + 4), vcst);
  843. #else
  844. fw[0] *= c1;
  845. fw[1] *= c2;
  846. fw[2] *= c1;
  847. fw[3] *= c2;
  848. fw[4] *= c1;
  849. fw[5] *= c2;
  850. fw[6] *= c1;
  851. fw[7] *= c2;
  852. #endif
  853. fw += 8;
  854. }
  855. for (; i < iters_common; i++) {
  856. fw[0] *= c1;
  857. fw[1] *= c2;
  858. fw += 2;
  859. }
  860. if (i < iters_c1) {
  861. fw[0] *= c1;
  862. } else if (i < iters_c2) {
  863. fw[1] *= c2;
  864. }
  865. }
  866. #endif
  867. static void opj_dwt_encode_step2(OPJ_FLOAT32* fl, OPJ_FLOAT32* fw,
  868. OPJ_UINT32 end,
  869. OPJ_UINT32 m,
  870. OPJ_FLOAT32 c)
  871. {
  872. OPJ_UINT32 i;
  873. OPJ_UINT32 imax = opj_uint_min(end, m);
  874. if (imax > 0) {
  875. fw[-1] += (fl[0] + fw[0]) * c;
  876. fw += 2;
  877. i = 1;
  878. for (; i + 3 < imax; i += 4) {
  879. fw[-1] += (fw[-2] + fw[0]) * c;
  880. fw[1] += (fw[0] + fw[2]) * c;
  881. fw[3] += (fw[2] + fw[4]) * c;
  882. fw[5] += (fw[4] + fw[6]) * c;
  883. fw += 8;
  884. }
  885. for (; i < imax; ++i) {
  886. fw[-1] += (fw[-2] + fw[0]) * c;
  887. fw += 2;
  888. }
  889. }
  890. if (m < end) {
  891. assert(m + 1 == end);
  892. fw[-1] += (2 * fw[-2]) * c;
  893. }
  894. }
  895. static void opj_dwt_encode_1_real(void *aIn, OPJ_INT32 dn, OPJ_INT32 sn,
  896. OPJ_INT32 cas)
  897. {
  898. OPJ_FLOAT32* w = (OPJ_FLOAT32*)aIn;
  899. OPJ_INT32 a, b;
  900. assert(dn + sn > 1);
  901. if (cas == 0) {
  902. a = 0;
  903. b = 1;
  904. } else {
  905. a = 1;
  906. b = 0;
  907. }
  908. opj_dwt_encode_step2(w + a, w + b + 1,
  909. (OPJ_UINT32)dn,
  910. (OPJ_UINT32)opj_int_min(dn, sn - b),
  911. opj_dwt_alpha);
  912. opj_dwt_encode_step2(w + b, w + a + 1,
  913. (OPJ_UINT32)sn,
  914. (OPJ_UINT32)opj_int_min(sn, dn - a),
  915. opj_dwt_beta);
  916. opj_dwt_encode_step2(w + a, w + b + 1,
  917. (OPJ_UINT32)dn,
  918. (OPJ_UINT32)opj_int_min(dn, sn - b),
  919. opj_dwt_gamma);
  920. opj_dwt_encode_step2(w + b, w + a + 1,
  921. (OPJ_UINT32)sn,
  922. (OPJ_UINT32)opj_int_min(sn, dn - a),
  923. opj_dwt_delta);
  924. #if 0
  925. opj_dwt_encode_step1(w + b, (OPJ_UINT32)dn,
  926. opj_K);
  927. opj_dwt_encode_step1(w + a, (OPJ_UINT32)sn,
  928. opj_invK);
  929. #else
  930. if (a == 0) {
  931. opj_dwt_encode_step1_combined(w,
  932. (OPJ_UINT32)sn,
  933. (OPJ_UINT32)dn,
  934. opj_invK,
  935. opj_K);
  936. } else {
  937. opj_dwt_encode_step1_combined(w,
  938. (OPJ_UINT32)dn,
  939. (OPJ_UINT32)sn,
  940. opj_K,
  941. opj_invK);
  942. }
  943. #endif
  944. }
  945. static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps,
  946. opj_stepsize_t *bandno_stepsize)
  947. {
  948. OPJ_INT32 p, n;
  949. p = opj_int_floorlog2(stepsize) - 13;
  950. n = 11 - opj_int_floorlog2(stepsize);
  951. bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
  952. bandno_stepsize->expn = numbps - p;
  953. }
  954. /*
  955. ==========================================================
  956. DWT interface
  957. ==========================================================
  958. */
  959. /** Process one line for the horizontal pass of the 5x3 forward transform */
  960. static
  961. void opj_dwt_encode_and_deinterleave_h_one_row(void* rowIn,
  962. void* tmpIn,
  963. OPJ_UINT32 width,
  964. OPJ_BOOL even)
  965. {
  966. OPJ_INT32* OPJ_RESTRICT row = (OPJ_INT32*)rowIn;
  967. OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32*)tmpIn;
  968. const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1);
  969. const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn);
  970. if (even) {
  971. if (width > 1) {
  972. OPJ_INT32 i;
  973. for (i = 0; i < sn - 1; i++) {
  974. tmp[sn + i] = row[2 * i + 1] - ((row[(i) * 2] + row[(i + 1) * 2]) >> 1);
  975. }
  976. if ((width % 2) == 0) {
  977. tmp[sn + i] = row[2 * i + 1] - row[(i) * 2];
  978. }
  979. row[0] += (tmp[sn] + tmp[sn] + 2) >> 2;
  980. for (i = 1; i < dn; i++) {
  981. row[i] = row[2 * i] + ((tmp[sn + (i - 1)] + tmp[sn + i] + 2) >> 2);
  982. }
  983. if ((width % 2) == 1) {
  984. row[i] = row[2 * i] + ((tmp[sn + (i - 1)] + tmp[sn + (i - 1)] + 2) >> 2);
  985. }
  986. memcpy(row + sn, tmp + sn, (OPJ_SIZE_T)dn * sizeof(OPJ_INT32));
  987. }
  988. } else {
  989. if (width == 1) {
  990. row[0] *= 2;
  991. } else {
  992. OPJ_INT32 i;
  993. tmp[sn + 0] = row[0] - row[1];
  994. for (i = 1; i < sn; i++) {
  995. tmp[sn + i] = row[2 * i] - ((row[2 * i + 1] + row[2 * (i - 1) + 1]) >> 1);
  996. }
  997. if ((width % 2) == 1) {
  998. tmp[sn + i] = row[2 * i] - row[2 * (i - 1) + 1];
  999. }
  1000. for (i = 0; i < dn - 1; i++) {
  1001. row[i] = row[2 * i + 1] + ((tmp[sn + i] + tmp[sn + i + 1] + 2) >> 2);
  1002. }
  1003. if ((width % 2) == 0) {
  1004. row[i] = row[2 * i + 1] + ((tmp[sn + i] + tmp[sn + i] + 2) >> 2);
  1005. }
  1006. memcpy(row + sn, tmp + sn, (OPJ_SIZE_T)dn * sizeof(OPJ_INT32));
  1007. }
  1008. }
  1009. }
  1010. /** Process one line for the horizontal pass of the 9x7 forward transform */
  1011. static
  1012. void opj_dwt_encode_and_deinterleave_h_one_row_real(void* rowIn,
  1013. void* tmpIn,
  1014. OPJ_UINT32 width,
  1015. OPJ_BOOL even)
  1016. {
  1017. OPJ_FLOAT32* OPJ_RESTRICT row = (OPJ_FLOAT32*)rowIn;
  1018. OPJ_FLOAT32* OPJ_RESTRICT tmp = (OPJ_FLOAT32*)tmpIn;
  1019. const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1);
  1020. const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn);
  1021. if (width == 1) {
  1022. return;
  1023. }
  1024. memcpy(tmp, row, width * sizeof(OPJ_FLOAT32));
  1025. opj_dwt_encode_1_real(tmp, dn, sn, even ? 0 : 1);
  1026. opj_dwt_deinterleave_h((OPJ_INT32 * OPJ_RESTRICT)tmp,
  1027. (OPJ_INT32 * OPJ_RESTRICT)row,
  1028. dn, sn, even ? 0 : 1);
  1029. }
  1030. typedef struct {
  1031. opj_dwt_t h;
  1032. OPJ_UINT32 rw; /* Width of the resolution to process */
  1033. OPJ_UINT32 w; /* Width of tiledp */
  1034. OPJ_INT32 * OPJ_RESTRICT tiledp;
  1035. OPJ_UINT32 min_j;
  1036. OPJ_UINT32 max_j;
  1037. opj_encode_and_deinterleave_h_one_row_fnptr_type p_function;
  1038. } opj_dwt_encode_h_job_t;
  1039. static void opj_dwt_encode_h_func(void* user_data, opj_tls_t* tls)
  1040. {
  1041. OPJ_UINT32 j;
  1042. opj_dwt_encode_h_job_t* job;
  1043. (void)tls;
  1044. job = (opj_dwt_encode_h_job_t*)user_data;
  1045. for (j = job->min_j; j < job->max_j; j++) {
  1046. OPJ_INT32* OPJ_RESTRICT aj = job->tiledp + j * job->w;
  1047. (*job->p_function)(aj, job->h.mem, job->rw,
  1048. job->h.cas == 0 ? OPJ_TRUE : OPJ_FALSE);
  1049. }
  1050. opj_aligned_free(job->h.mem);
  1051. opj_free(job);
  1052. }
  1053. typedef struct {
  1054. opj_dwt_t v;
  1055. OPJ_UINT32 rh;
  1056. OPJ_UINT32 w;
  1057. OPJ_INT32 * OPJ_RESTRICT tiledp;
  1058. OPJ_UINT32 min_j;
  1059. OPJ_UINT32 max_j;
  1060. opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v;
  1061. } opj_dwt_encode_v_job_t;
  1062. static void opj_dwt_encode_v_func(void* user_data, opj_tls_t* tls)
  1063. {
  1064. OPJ_UINT32 j;
  1065. opj_dwt_encode_v_job_t* job;
  1066. (void)tls;
  1067. job = (opj_dwt_encode_v_job_t*)user_data;
  1068. for (j = job->min_j; j + NB_ELTS_V8 - 1 < job->max_j; j += NB_ELTS_V8) {
  1069. (*job->p_encode_and_deinterleave_v)(job->tiledp + j,
  1070. job->v.mem,
  1071. job->rh,
  1072. job->v.cas == 0,
  1073. job->w,
  1074. NB_ELTS_V8);
  1075. }
  1076. if (j < job->max_j) {
  1077. (*job->p_encode_and_deinterleave_v)(job->tiledp + j,
  1078. job->v.mem,
  1079. job->rh,
  1080. job->v.cas == 0,
  1081. job->w,
  1082. job->max_j - j);
  1083. }
  1084. opj_aligned_free(job->v.mem);
  1085. opj_free(job);
  1086. }
  1087. /** Fetch up to cols <= NB_ELTS_V8 for each line, and put them in tmpOut */
  1088. /* that has a NB_ELTS_V8 interleave factor. */
  1089. static void opj_dwt_fetch_cols_vertical_pass(const void *arrayIn,
  1090. void *tmpOut,
  1091. OPJ_UINT32 height,
  1092. OPJ_UINT32 stride_width,
  1093. OPJ_UINT32 cols)
  1094. {
  1095. const OPJ_INT32* OPJ_RESTRICT array = (const OPJ_INT32 * OPJ_RESTRICT)arrayIn;
  1096. OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32 * OPJ_RESTRICT)tmpOut;
  1097. if (cols == NB_ELTS_V8) {
  1098. OPJ_UINT32 k;
  1099. for (k = 0; k < height; ++k) {
  1100. memcpy(tmp + NB_ELTS_V8 * k,
  1101. array + k * stride_width,
  1102. NB_ELTS_V8 * sizeof(OPJ_INT32));
  1103. }
  1104. } else {
  1105. OPJ_UINT32 k;
  1106. for (k = 0; k < height; ++k) {
  1107. OPJ_UINT32 c;
  1108. for (c = 0; c < cols; c++) {
  1109. tmp[NB_ELTS_V8 * k + c] = array[c + k * stride_width];
  1110. }
  1111. for (; c < NB_ELTS_V8; c++) {
  1112. tmp[NB_ELTS_V8 * k + c] = 0;
  1113. }
  1114. }
  1115. }
  1116. }
  1117. /* Deinterleave result of forward transform, where cols <= NB_ELTS_V8 */
  1118. /* and src contains NB_ELTS_V8 consecutive values for up to NB_ELTS_V8 */
  1119. /* columns. */
  1120. static INLINE void opj_dwt_deinterleave_v_cols(
  1121. const OPJ_INT32 * OPJ_RESTRICT src,
  1122. OPJ_INT32 * OPJ_RESTRICT dst,
  1123. OPJ_INT32 dn,
  1124. OPJ_INT32 sn,
  1125. OPJ_UINT32 stride_width,
  1126. OPJ_INT32 cas,
  1127. OPJ_UINT32 cols)
  1128. {
  1129. OPJ_INT32 k;
  1130. OPJ_INT32 i = sn;
  1131. OPJ_INT32 * OPJ_RESTRICT l_dest = dst;
  1132. const OPJ_INT32 * OPJ_RESTRICT l_src = src + cas * NB_ELTS_V8;
  1133. OPJ_UINT32 c;
  1134. for (k = 0; k < 2; k++) {
  1135. while (i--) {
  1136. if (cols == NB_ELTS_V8) {
  1137. memcpy(l_dest, l_src, NB_ELTS_V8 * sizeof(OPJ_INT32));
  1138. } else {
  1139. c = 0;
  1140. switch (cols) {
  1141. case 7:
  1142. l_dest[c] = l_src[c];
  1143. c++; /* fallthru */
  1144. case 6:
  1145. l_dest[c] = l_src[c];
  1146. c++; /* fallthru */
  1147. case 5:
  1148. l_dest[c] = l_src[c];
  1149. c++; /* fallthru */
  1150. case 4:
  1151. l_dest[c] = l_src[c];
  1152. c++; /* fallthru */
  1153. case 3:
  1154. l_dest[c] = l_src[c];
  1155. c++; /* fallthru */
  1156. case 2:
  1157. l_dest[c] = l_src[c];
  1158. c++; /* fallthru */
  1159. default:
  1160. l_dest[c] = l_src[c];
  1161. break;
  1162. }
  1163. }
  1164. l_dest += stride_width;
  1165. l_src += 2 * NB_ELTS_V8;
  1166. }
  1167. l_dest = dst + (OPJ_SIZE_T)sn * (OPJ_SIZE_T)stride_width;
  1168. l_src = src + (1 - cas) * NB_ELTS_V8;
  1169. i = dn;
  1170. }
  1171. }
  1172. /* Forward 5-3 transform, for the vertical pass, processing cols columns */
  1173. /* where cols <= NB_ELTS_V8 */
  1174. static void opj_dwt_encode_and_deinterleave_v(
  1175. void *arrayIn,
  1176. void *tmpIn,
  1177. OPJ_UINT32 height,
  1178. OPJ_BOOL even,
  1179. OPJ_UINT32 stride_width,
  1180. OPJ_UINT32 cols)
  1181. {
  1182. OPJ_INT32* OPJ_RESTRICT array = (OPJ_INT32 * OPJ_RESTRICT)arrayIn;
  1183. OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32 * OPJ_RESTRICT)tmpIn;
  1184. const OPJ_UINT32 sn = (height + (even ? 1 : 0)) >> 1;
  1185. const OPJ_UINT32 dn = height - sn;
  1186. opj_dwt_fetch_cols_vertical_pass(arrayIn, tmpIn, height, stride_width, cols);
  1187. #define OPJ_Sc(i) tmp[(i)*2* NB_ELTS_V8 + c]
  1188. #define OPJ_Dc(i) tmp[((1+(i)*2))* NB_ELTS_V8 + c]
  1189. #ifdef __SSE2__
  1190. if (height == 1) {
  1191. if (!even) {
  1192. OPJ_UINT32 c;
  1193. for (c = 0; c < NB_ELTS_V8; c++) {
  1194. tmp[c] *= 2;
  1195. }
  1196. }
  1197. } else if (even) {
  1198. OPJ_UINT32 c;
  1199. OPJ_UINT32 i;
  1200. i = 0;
  1201. if (i + 1 < sn) {
  1202. __m128i xmm_Si_0 = *(const __m128i*)(tmp + 4 * 0);
  1203. __m128i xmm_Si_1 = *(const __m128i*)(tmp + 4 * 1);
  1204. for (; i + 1 < sn; i++) {
  1205. __m128i xmm_Sip1_0 = *(const __m128i*)(tmp +
  1206. (i + 1) * 2 * NB_ELTS_V8 + 4 * 0);
  1207. __m128i xmm_Sip1_1 = *(const __m128i*)(tmp +
  1208. (i + 1) * 2 * NB_ELTS_V8 + 4 * 1);
  1209. __m128i xmm_Di_0 = *(const __m128i*)(tmp +
  1210. (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
  1211. __m128i xmm_Di_1 = *(const __m128i*)(tmp +
  1212. (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
  1213. xmm_Di_0 = _mm_sub_epi32(xmm_Di_0,
  1214. _mm_srai_epi32(_mm_add_epi32(xmm_Si_0, xmm_Sip1_0), 1));
  1215. xmm_Di_1 = _mm_sub_epi32(xmm_Di_1,
  1216. _mm_srai_epi32(_mm_add_epi32(xmm_Si_1, xmm_Sip1_1), 1));
  1217. *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Di_0;
  1218. *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Di_1;
  1219. xmm_Si_0 = xmm_Sip1_0;
  1220. xmm_Si_1 = xmm_Sip1_1;
  1221. }
  1222. }
  1223. if (((height) % 2) == 0) {
  1224. for (c = 0; c < NB_ELTS_V8; c++) {
  1225. OPJ_Dc(i) -= OPJ_Sc(i);
  1226. }
  1227. }
  1228. for (c = 0; c < NB_ELTS_V8; c++) {
  1229. OPJ_Sc(0) += (OPJ_Dc(0) + OPJ_Dc(0) + 2) >> 2;
  1230. }
  1231. i = 1;
  1232. if (i < dn) {
  1233. __m128i xmm_Dim1_0 = *(const __m128i*)(tmp + (1 +
  1234. (i - 1) * 2) * NB_ELTS_V8 + 4 * 0);
  1235. __m128i xmm_Dim1_1 = *(const __m128i*)(tmp + (1 +
  1236. (i - 1) * 2) * NB_ELTS_V8 + 4 * 1);
  1237. const __m128i xmm_two = _mm_set1_epi32(2);
  1238. for (; i < dn; i++) {
  1239. __m128i xmm_Di_0 = *(const __m128i*)(tmp +
  1240. (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
  1241. __m128i xmm_Di_1 = *(const __m128i*)(tmp +
  1242. (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
  1243. __m128i xmm_Si_0 = *(const __m128i*)(tmp +
  1244. (i * 2) * NB_ELTS_V8 + 4 * 0);
  1245. __m128i xmm_Si_1 = *(const __m128i*)(tmp +
  1246. (i * 2) * NB_ELTS_V8 + 4 * 1);
  1247. xmm_Si_0 = _mm_add_epi32(xmm_Si_0,
  1248. _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Dim1_0, xmm_Di_0), xmm_two), 2));
  1249. xmm_Si_1 = _mm_add_epi32(xmm_Si_1,
  1250. _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Dim1_1, xmm_Di_1), xmm_two), 2));
  1251. *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Si_0;
  1252. *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Si_1;
  1253. xmm_Dim1_0 = xmm_Di_0;
  1254. xmm_Dim1_1 = xmm_Di_1;
  1255. }
  1256. }
  1257. if (((height) % 2) == 1) {
  1258. for (c = 0; c < NB_ELTS_V8; c++) {
  1259. OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i - 1) + 2) >> 2;
  1260. }
  1261. }
  1262. } else {
  1263. OPJ_UINT32 c;
  1264. OPJ_UINT32 i;
  1265. for (c = 0; c < NB_ELTS_V8; c++) {
  1266. OPJ_Sc(0) -= OPJ_Dc(0);
  1267. }
  1268. i = 1;
  1269. if (i < sn) {
  1270. __m128i xmm_Dim1_0 = *(const __m128i*)(tmp + (1 +
  1271. (i - 1) * 2) * NB_ELTS_V8 + 4 * 0);
  1272. __m128i xmm_Dim1_1 = *(const __m128i*)(tmp + (1 +
  1273. (i - 1) * 2) * NB_ELTS_V8 + 4 * 1);
  1274. for (; i < sn; i++) {
  1275. __m128i xmm_Di_0 = *(const __m128i*)(tmp +
  1276. (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
  1277. __m128i xmm_Di_1 = *(const __m128i*)(tmp +
  1278. (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
  1279. __m128i xmm_Si_0 = *(const __m128i*)(tmp +
  1280. (i * 2) * NB_ELTS_V8 + 4 * 0);
  1281. __m128i xmm_Si_1 = *(const __m128i*)(tmp +
  1282. (i * 2) * NB_ELTS_V8 + 4 * 1);
  1283. xmm_Si_0 = _mm_sub_epi32(xmm_Si_0,
  1284. _mm_srai_epi32(_mm_add_epi32(xmm_Di_0, xmm_Dim1_0), 1));
  1285. xmm_Si_1 = _mm_sub_epi32(xmm_Si_1,
  1286. _mm_srai_epi32(_mm_add_epi32(xmm_Di_1, xmm_Dim1_1), 1));
  1287. *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Si_0;
  1288. *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Si_1;
  1289. xmm_Dim1_0 = xmm_Di_0;
  1290. xmm_Dim1_1 = xmm_Di_1;
  1291. }
  1292. }
  1293. if (((height) % 2) == 1) {
  1294. for (c = 0; c < NB_ELTS_V8; c++) {
  1295. OPJ_Sc(i) -= OPJ_Dc(i - 1);
  1296. }
  1297. }
  1298. i = 0;
  1299. if (i + 1 < dn) {
  1300. __m128i xmm_Si_0 = *((const __m128i*)(tmp + 4 * 0));
  1301. __m128i xmm_Si_1 = *((const __m128i*)(tmp + 4 * 1));
  1302. const __m128i xmm_two = _mm_set1_epi32(2);
  1303. for (; i + 1 < dn; i++) {
  1304. __m128i xmm_Sip1_0 = *(const __m128i*)(tmp +
  1305. (i + 1) * 2 * NB_ELTS_V8 + 4 * 0);
  1306. __m128i xmm_Sip1_1 = *(const __m128i*)(tmp +
  1307. (i + 1) * 2 * NB_ELTS_V8 + 4 * 1);
  1308. __m128i xmm_Di_0 = *(const __m128i*)(tmp +
  1309. (1 + i * 2) * NB_ELTS_V8 + 4 * 0);
  1310. __m128i xmm_Di_1 = *(const __m128i*)(tmp +
  1311. (1 + i * 2) * NB_ELTS_V8 + 4 * 1);
  1312. xmm_Di_0 = _mm_add_epi32(xmm_Di_0,
  1313. _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Si_0, xmm_Sip1_0), xmm_two), 2));
  1314. xmm_Di_1 = _mm_add_epi32(xmm_Di_1,
  1315. _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Si_1, xmm_Sip1_1), xmm_two), 2));
  1316. *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Di_0;
  1317. *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Di_1;
  1318. xmm_Si_0 = xmm_Sip1_0;
  1319. xmm_Si_1 = xmm_Sip1_1;
  1320. }
  1321. }
  1322. if (((height) % 2) == 0) {
  1323. for (c = 0; c < NB_ELTS_V8; c++) {
  1324. OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i) + 2) >> 2;
  1325. }
  1326. }
  1327. }
  1328. #else
  1329. if (even) {
  1330. OPJ_UINT32 c;
  1331. if (height > 1) {
  1332. OPJ_UINT32 i;
  1333. for (i = 0; i + 1 < sn; i++) {
  1334. for (c = 0; c < NB_ELTS_V8; c++) {
  1335. OPJ_Dc(i) -= (OPJ_Sc(i) + OPJ_Sc(i + 1)) >> 1;
  1336. }
  1337. }
  1338. if (((height) % 2) == 0) {
  1339. for (c = 0; c < NB_ELTS_V8; c++) {
  1340. OPJ_Dc(i) -= OPJ_Sc(i);
  1341. }
  1342. }
  1343. for (c = 0; c < NB_ELTS_V8; c++) {
  1344. OPJ_Sc(0) += (OPJ_Dc(0) + OPJ_Dc(0) + 2) >> 2;
  1345. }
  1346. for (i = 1; i < dn; i++) {
  1347. for (c = 0; c < NB_ELTS_V8; c++) {
  1348. OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i) + 2) >> 2;
  1349. }
  1350. }
  1351. if (((height) % 2) == 1) {
  1352. for (c = 0; c < NB_ELTS_V8; c++) {
  1353. OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i - 1) + 2) >> 2;
  1354. }
  1355. }
  1356. }
  1357. } else {
  1358. OPJ_UINT32 c;
  1359. if (height == 1) {
  1360. for (c = 0; c < NB_ELTS_V8; c++) {
  1361. OPJ_Sc(0) *= 2;
  1362. }
  1363. } else {
  1364. OPJ_UINT32 i;
  1365. for (c = 0; c < NB_ELTS_V8; c++) {
  1366. OPJ_Sc(0) -= OPJ_Dc(0);
  1367. }
  1368. for (i = 1; i < sn; i++) {
  1369. for (c = 0; c < NB_ELTS_V8; c++) {
  1370. OPJ_Sc(i) -= (OPJ_Dc(i) + OPJ_Dc(i - 1)) >> 1;
  1371. }
  1372. }
  1373. if (((height) % 2) == 1) {
  1374. for (c = 0; c < NB_ELTS_V8; c++) {
  1375. OPJ_Sc(i) -= OPJ_Dc(i - 1);
  1376. }
  1377. }
  1378. for (i = 0; i + 1 < dn; i++) {
  1379. for (c = 0; c < NB_ELTS_V8; c++) {
  1380. OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i + 1) + 2) >> 2;
  1381. }
  1382. }
  1383. if (((height) % 2) == 0) {
  1384. for (c = 0; c < NB_ELTS_V8; c++) {
  1385. OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i) + 2) >> 2;
  1386. }
  1387. }
  1388. }
  1389. }
  1390. #endif
  1391. if (cols == NB_ELTS_V8) {
  1392. opj_dwt_deinterleave_v_cols(tmp, array, (OPJ_INT32)dn, (OPJ_INT32)sn,
  1393. stride_width, even ? 0 : 1, NB_ELTS_V8);
  1394. } else {
  1395. opj_dwt_deinterleave_v_cols(tmp, array, (OPJ_INT32)dn, (OPJ_INT32)sn,
  1396. stride_width, even ? 0 : 1, cols);
  1397. }
  1398. }
  1399. static void opj_v8dwt_encode_step1(OPJ_FLOAT32* fw,
  1400. OPJ_UINT32 end,
  1401. const OPJ_FLOAT32 cst)
  1402. {
  1403. OPJ_UINT32 i;
  1404. #ifdef __SSE__
  1405. __m128* vw = (__m128*) fw;
  1406. const __m128 vcst = _mm_set1_ps(cst);
  1407. for (i = 0; i < end; ++i) {
  1408. vw[0] = _mm_mul_ps(vw[0], vcst);
  1409. vw[1] = _mm_mul_ps(vw[1], vcst);
  1410. vw += 2 * (NB_ELTS_V8 * sizeof(OPJ_FLOAT32) / sizeof(__m128));
  1411. }
  1412. #else
  1413. OPJ_UINT32 c;
  1414. for (i = 0; i < end; ++i) {
  1415. for (c = 0; c < NB_ELTS_V8; c++) {
  1416. fw[i * 2 * NB_ELTS_V8 + c] *= cst;
  1417. }
  1418. }
  1419. #endif
  1420. }
  1421. static void opj_v8dwt_encode_step2(OPJ_FLOAT32* fl, OPJ_FLOAT32* fw,
  1422. OPJ_UINT32 end,
  1423. OPJ_UINT32 m,
  1424. OPJ_FLOAT32 cst)
  1425. {
  1426. OPJ_UINT32 i;
  1427. OPJ_UINT32 imax = opj_uint_min(end, m);
  1428. #ifdef __SSE__
  1429. __m128* vw = (__m128*) fw;
  1430. __m128 vcst = _mm_set1_ps(cst);
  1431. if (imax > 0) {
  1432. __m128* vl = (__m128*) fl;
  1433. vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vl[0], vw[0]), vcst));
  1434. vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vl[1], vw[1]), vcst));
  1435. vw += 2 * (NB_ELTS_V8 * sizeof(OPJ_FLOAT32) / sizeof(__m128));
  1436. i = 1;
  1437. for (; i < imax; ++i) {
  1438. vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vw[-4], vw[0]), vcst));
  1439. vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vw[-3], vw[1]), vcst));
  1440. vw += 2 * (NB_ELTS_V8 * sizeof(OPJ_FLOAT32) / sizeof(__m128));
  1441. }
  1442. }
  1443. if (m < end) {
  1444. assert(m + 1 == end);
  1445. vcst = _mm_add_ps(vcst, vcst);
  1446. vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(vw[-4], vcst));
  1447. vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(vw[-3], vcst));
  1448. }
  1449. #else
  1450. OPJ_INT32 c;
  1451. if (imax > 0) {
  1452. for (c = 0; c < NB_ELTS_V8; c++) {
  1453. fw[-1 * NB_ELTS_V8 + c] += (fl[0 * NB_ELTS_V8 + c] + fw[0 * NB_ELTS_V8 + c]) *
  1454. cst;
  1455. }
  1456. fw += 2 * NB_ELTS_V8;
  1457. i = 1;
  1458. for (; i < imax; ++i) {
  1459. for (c = 0; c < NB_ELTS_V8; c++) {
  1460. fw[-1 * NB_ELTS_V8 + c] += (fw[-2 * NB_ELTS_V8 + c] + fw[0 * NB_ELTS_V8 + c]) *
  1461. cst;
  1462. }
  1463. fw += 2 * NB_ELTS_V8;
  1464. }
  1465. }
  1466. if (m < end) {
  1467. assert(m + 1 == end);
  1468. for (c = 0; c < NB_ELTS_V8; c++) {
  1469. fw[-1 * NB_ELTS_V8 + c] += (2 * fw[-2 * NB_ELTS_V8 + c]) * cst;
  1470. }
  1471. }
  1472. #endif
  1473. }
  1474. /* Forward 9-7 transform, for the vertical pass, processing cols columns */
  1475. /* where cols <= NB_ELTS_V8 */
  1476. static void opj_dwt_encode_and_deinterleave_v_real(
  1477. void *arrayIn,
  1478. void *tmpIn,
  1479. OPJ_UINT32 height,
  1480. OPJ_BOOL even,
  1481. OPJ_UINT32 stride_width,
  1482. OPJ_UINT32 cols)
  1483. {
  1484. OPJ_FLOAT32* OPJ_RESTRICT array = (OPJ_FLOAT32 * OPJ_RESTRICT)arrayIn;
  1485. OPJ_FLOAT32* OPJ_RESTRICT tmp = (OPJ_FLOAT32 * OPJ_RESTRICT)tmpIn;
  1486. const OPJ_INT32 sn = (OPJ_INT32)((height + (even ? 1 : 0)) >> 1);
  1487. const OPJ_INT32 dn = (OPJ_INT32)(height - (OPJ_UINT32)sn);
  1488. OPJ_INT32 a, b;
  1489. if (height == 1) {
  1490. return;
  1491. }
  1492. opj_dwt_fetch_cols_vertical_pass(arrayIn, tmpIn, height, stride_width, cols);
  1493. if (even) {
  1494. a = 0;
  1495. b = 1;
  1496. } else {
  1497. a = 1;
  1498. b = 0;
  1499. }
  1500. opj_v8dwt_encode_step2(tmp + a * NB_ELTS_V8,
  1501. tmp + (b + 1) * NB_ELTS_V8,
  1502. (OPJ_UINT32)dn,
  1503. (OPJ_UINT32)opj_int_min(dn, sn - b),
  1504. opj_dwt_alpha);
  1505. opj_v8dwt_encode_step2(tmp + b * NB_ELTS_V8,
  1506. tmp + (a + 1) * NB_ELTS_V8,
  1507. (OPJ_UINT32)sn,
  1508. (OPJ_UINT32)opj_int_min(sn, dn - a),
  1509. opj_dwt_beta);
  1510. opj_v8dwt_encode_step2(tmp + a * NB_ELTS_V8,
  1511. tmp + (b + 1) * NB_ELTS_V8,
  1512. (OPJ_UINT32)dn,
  1513. (OPJ_UINT32)opj_int_min(dn, sn - b),
  1514. opj_dwt_gamma);
  1515. opj_v8dwt_encode_step2(tmp + b * NB_ELTS_V8,
  1516. tmp + (a + 1) * NB_ELTS_V8,
  1517. (OPJ_UINT32)sn,
  1518. (OPJ_UINT32)opj_int_min(sn, dn - a),
  1519. opj_dwt_delta);
  1520. opj_v8dwt_encode_step1(tmp + b * NB_ELTS_V8, (OPJ_UINT32)dn,
  1521. opj_K);
  1522. opj_v8dwt_encode_step1(tmp + a * NB_ELTS_V8, (OPJ_UINT32)sn,
  1523. opj_invK);
  1524. if (cols == NB_ELTS_V8) {
  1525. opj_dwt_deinterleave_v_cols((OPJ_INT32*)tmp,
  1526. (OPJ_INT32*)array,
  1527. (OPJ_INT32)dn, (OPJ_INT32)sn,
  1528. stride_width, even ? 0 : 1, NB_ELTS_V8);
  1529. } else {
  1530. opj_dwt_deinterleave_v_cols((OPJ_INT32*)tmp,
  1531. (OPJ_INT32*)array,
  1532. (OPJ_INT32)dn, (OPJ_INT32)sn,
  1533. stride_width, even ? 0 : 1, cols);
  1534. }
  1535. }
  1536. /* <summary> */
  1537. /* Forward 5-3 wavelet transform in 2-D. */
  1538. /* </summary> */
  1539. static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp,
  1540. opj_tcd_tilecomp_t * tilec,
  1541. opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v,
  1542. opj_encode_and_deinterleave_h_one_row_fnptr_type
  1543. p_encode_and_deinterleave_h_one_row)
  1544. {
  1545. OPJ_INT32 i;
  1546. OPJ_INT32 *bj = 00;
  1547. OPJ_UINT32 w;
  1548. OPJ_INT32 l;
  1549. OPJ_SIZE_T l_data_size;
  1550. opj_tcd_resolution_t * l_cur_res = 0;
  1551. opj_tcd_resolution_t * l_last_res = 0;
  1552. const int num_threads = opj_thread_pool_get_thread_count(tp);
  1553. OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
  1554. w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
  1555. l = (OPJ_INT32)tilec->numresolutions - 1;
  1556. l_cur_res = tilec->resolutions + l;
  1557. l_last_res = l_cur_res - 1;
  1558. l_data_size = opj_dwt_max_resolution(tilec->resolutions, tilec->numresolutions);
  1559. /* overflow check */
  1560. if (l_data_size > (SIZE_MAX / (NB_ELTS_V8 * sizeof(OPJ_INT32)))) {
  1561. /* FIXME event manager error callback */
  1562. return OPJ_FALSE;
  1563. }
  1564. l_data_size *= NB_ELTS_V8 * sizeof(OPJ_INT32);
  1565. bj = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
  1566. /* l_data_size is equal to 0 when numresolutions == 1 but bj is not used */
  1567. /* in that case, so do not error out */
  1568. if (l_data_size != 0 && ! bj) {
  1569. return OPJ_FALSE;
  1570. }
  1571. i = l;
  1572. while (i--) {
  1573. OPJ_UINT32 j;
  1574. OPJ_UINT32 rw; /* width of the resolution level computed */
  1575. OPJ_UINT32 rh; /* height of the resolution level computed */
  1576. OPJ_UINT32
  1577. rw1; /* width of the resolution level once lower than computed one */
  1578. OPJ_UINT32
  1579. rh1; /* height of the resolution level once lower than computed one */
  1580. OPJ_INT32 cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
  1581. OPJ_INT32 cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
  1582. OPJ_INT32 dn, sn;
  1583. rw = (OPJ_UINT32)(l_cur_res->x1 - l_cur_res->x0);
  1584. rh = (OPJ_UINT32)(l_cur_res->y1 - l_cur_res->y0);
  1585. rw1 = (OPJ_UINT32)(l_last_res->x1 - l_last_res->x0);
  1586. rh1 = (OPJ_UINT32)(l_last_res->y1 - l_last_res->y0);
  1587. cas_row = l_cur_res->x0 & 1;
  1588. cas_col = l_cur_res->y0 & 1;
  1589. sn = (OPJ_INT32)rh1;
  1590. dn = (OPJ_INT32)(rh - rh1);
  1591. /* Perform vertical pass */
  1592. if (num_threads <= 1 || rw < 2 * NB_ELTS_V8) {
  1593. for (j = 0; j + NB_ELTS_V8 - 1 < rw; j += NB_ELTS_V8) {
  1594. p_encode_and_deinterleave_v(tiledp + j,
  1595. bj,
  1596. rh,
  1597. cas_col == 0,
  1598. w,
  1599. NB_ELTS_V8);
  1600. }
  1601. if (j < rw) {
  1602. p_encode_and_deinterleave_v(tiledp + j,
  1603. bj,
  1604. rh,
  1605. cas_col == 0,
  1606. w,
  1607. rw - j);
  1608. }
  1609. } else {
  1610. OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
  1611. OPJ_UINT32 step_j;
  1612. if (rw < num_jobs) {
  1613. num_jobs = rw;
  1614. }
  1615. step_j = ((rw / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
  1616. for (j = 0; j < num_jobs; j++) {
  1617. opj_dwt_encode_v_job_t* job;
  1618. job = (opj_dwt_encode_v_job_t*) opj_malloc(sizeof(opj_dwt_encode_v_job_t));
  1619. if (!job) {
  1620. opj_thread_pool_wait_completion(tp, 0);
  1621. opj_aligned_free(bj);
  1622. return OPJ_FALSE;
  1623. }
  1624. job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
  1625. if (!job->v.mem) {
  1626. opj_thread_pool_wait_completion(tp, 0);
  1627. opj_free(job);
  1628. opj_aligned_free(bj);
  1629. return OPJ_FALSE;
  1630. }
  1631. job->v.dn = dn;
  1632. job->v.sn = sn;
  1633. job->v.cas = cas_col;
  1634. job->rh = rh;
  1635. job->w = w;
  1636. job->tiledp = tiledp;
  1637. job->min_j = j * step_j;
  1638. job->max_j = (j + 1 == num_jobs) ? rw : (j + 1) * step_j;
  1639. job->p_encode_and_deinterleave_v = p_encode_and_deinterleave_v;
  1640. opj_thread_pool_submit_job(tp, opj_dwt_encode_v_func, job);
  1641. }
  1642. opj_thread_pool_wait_completion(tp, 0);
  1643. }
  1644. sn = (OPJ_INT32)rw1;
  1645. dn = (OPJ_INT32)(rw - rw1);
  1646. /* Perform horizontal pass */
  1647. if (num_threads <= 1 || rh <= 1) {
  1648. for (j = 0; j < rh; j++) {
  1649. OPJ_INT32* OPJ_RESTRICT aj = tiledp + j * w;
  1650. (*p_encode_and_deinterleave_h_one_row)(aj, bj, rw,
  1651. cas_row == 0 ? OPJ_TRUE : OPJ_FALSE);
  1652. }
  1653. } else {
  1654. OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
  1655. OPJ_UINT32 step_j;
  1656. if (rh < num_jobs) {
  1657. num_jobs = rh;
  1658. }
  1659. step_j = (rh / num_jobs);
  1660. for (j = 0; j < num_jobs; j++) {
  1661. opj_dwt_encode_h_job_t* job;
  1662. job = (opj_dwt_encode_h_job_t*) opj_malloc(sizeof(opj_dwt_encode_h_job_t));
  1663. if (!job) {
  1664. opj_thread_pool_wait_completion(tp, 0);
  1665. opj_aligned_free(bj);
  1666. return OPJ_FALSE;
  1667. }
  1668. job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size);
  1669. if (!job->h.mem) {
  1670. opj_thread_pool_wait_completion(tp, 0);
  1671. opj_free(job);
  1672. opj_aligned_free(bj);
  1673. return OPJ_FALSE;
  1674. }
  1675. job->h.dn = dn;
  1676. job->h.sn = sn;
  1677. job->h.cas = cas_row;
  1678. job->rw = rw;
  1679. job->w = w;
  1680. job->tiledp = tiledp;
  1681. job->min_j = j * step_j;
  1682. job->max_j = (j + 1U) * step_j; /* this can overflow */
  1683. if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
  1684. job->max_j = rh;
  1685. }
  1686. job->p_function = p_encode_and_deinterleave_h_one_row;
  1687. opj_thread_pool_submit_job(tp, opj_dwt_encode_h_func, job);
  1688. }
  1689. opj_thread_pool_wait_completion(tp, 0);
  1690. }
  1691. l_cur_res = l_last_res;
  1692. --l_last_res;
  1693. }
  1694. opj_aligned_free(bj);
  1695. return OPJ_TRUE;
  1696. }
  1697. /* Forward 5-3 wavelet transform in 2-D. */
  1698. /* </summary> */
  1699. OPJ_BOOL opj_dwt_encode(opj_tcd_t *p_tcd,
  1700. opj_tcd_tilecomp_t * tilec)
  1701. {
  1702. return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec,
  1703. opj_dwt_encode_and_deinterleave_v,
  1704. opj_dwt_encode_and_deinterleave_h_one_row);
  1705. }
  1706. /* <summary> */
  1707. /* Inverse 5-3 wavelet transform in 2-D. */
  1708. /* </summary> */
  1709. OPJ_BOOL opj_dwt_decode(opj_tcd_t *p_tcd, opj_tcd_tilecomp_t* tilec,
  1710. OPJ_UINT32 numres)
  1711. {
  1712. if (p_tcd->whole_tile_decoding) {
  1713. return opj_dwt_decode_tile(p_tcd->thread_pool, tilec, numres);
  1714. } else {
  1715. return opj_dwt_decode_partial_tile(tilec, numres);
  1716. }
  1717. }
  1718. /* <summary> */
  1719. /* Get norm of 5-3 wavelet. */
  1720. /* </summary> */
  1721. OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient)
  1722. {
  1723. /* FIXME ! This is just a band-aid to avoid a buffer overflow */
  1724. /* but the array should really be extended up to 33 resolution levels */
  1725. /* See https://github.com/uclouvain/openjpeg/issues/493 */
  1726. if (orient == 0 && level >= 10) {
  1727. level = 9;
  1728. } else if (orient > 0 && level >= 9) {
  1729. level = 8;
  1730. }
  1731. return opj_dwt_norms[orient][level];
  1732. }
  1733. /* <summary> */
  1734. /* Forward 9-7 wavelet transform in 2-D. */
  1735. /* </summary> */
  1736. OPJ_BOOL opj_dwt_encode_real(opj_tcd_t *p_tcd,
  1737. opj_tcd_tilecomp_t * tilec)
  1738. {
  1739. return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec,
  1740. opj_dwt_encode_and_deinterleave_v_real,
  1741. opj_dwt_encode_and_deinterleave_h_one_row_real);
  1742. }
  1743. /* <summary> */
  1744. /* Get norm of 9-7 wavelet. */
  1745. /* </summary> */
  1746. OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient)
  1747. {
  1748. /* FIXME ! This is just a band-aid to avoid a buffer overflow */
  1749. /* but the array should really be extended up to 33 resolution levels */
  1750. /* See https://github.com/uclouvain/openjpeg/issues/493 */
  1751. if (orient == 0 && level >= 10) {
  1752. level = 9;
  1753. } else if (orient > 0 && level >= 9) {
  1754. level = 8;
  1755. }
  1756. return opj_dwt_norms_real[orient][level];
  1757. }
  1758. void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec)
  1759. {
  1760. OPJ_UINT32 numbands, bandno;
  1761. numbands = 3 * tccp->numresolutions - 2;
  1762. for (bandno = 0; bandno < numbands; bandno++) {
  1763. OPJ_FLOAT64 stepsize;
  1764. OPJ_UINT32 resno, level, orient, gain;
  1765. resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
  1766. orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
  1767. level = tccp->numresolutions - 1 - resno;
  1768. gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) ||
  1769. (orient == 2)) ? 1 : 2));
  1770. if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
  1771. stepsize = 1.0;
  1772. } else {
  1773. OPJ_FLOAT64 norm = opj_dwt_getnorm_real(level, orient);
  1774. stepsize = (1 << (gain)) / norm;
  1775. }
  1776. opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0),
  1777. (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
  1778. }
  1779. }
  1780. /* <summary> */
  1781. /* Determine maximum computed resolution level for inverse wavelet transform */
  1782. /* </summary> */
  1783. static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r,
  1784. OPJ_UINT32 i)
  1785. {
  1786. OPJ_UINT32 mr = 0;
  1787. OPJ_UINT32 w;
  1788. while (--i) {
  1789. ++r;
  1790. if (mr < (w = (OPJ_UINT32)(r->x1 - r->x0))) {
  1791. mr = w ;
  1792. }
  1793. if (mr < (w = (OPJ_UINT32)(r->y1 - r->y0))) {
  1794. mr = w ;
  1795. }
  1796. }
  1797. return mr ;
  1798. }
  1799. typedef struct {
  1800. opj_dwt_t h;
  1801. OPJ_UINT32 rw;
  1802. OPJ_UINT32 w;
  1803. OPJ_INT32 * OPJ_RESTRICT tiledp;
  1804. OPJ_UINT32 min_j;
  1805. OPJ_UINT32 max_j;
  1806. } opj_dwt_decode_h_job_t;
  1807. static void opj_dwt_decode_h_func(void* user_data, opj_tls_t* tls)
  1808. {
  1809. OPJ_UINT32 j;
  1810. opj_dwt_decode_h_job_t* job;
  1811. (void)tls;
  1812. job = (opj_dwt_decode_h_job_t*)user_data;
  1813. for (j = job->min_j; j < job->max_j; j++) {
  1814. opj_idwt53_h(&job->h, &job->tiledp[j * job->w]);
  1815. }
  1816. opj_aligned_free(job->h.mem);
  1817. opj_free(job);
  1818. }
  1819. typedef struct {
  1820. opj_dwt_t v;
  1821. OPJ_UINT32 rh;
  1822. OPJ_UINT32 w;
  1823. OPJ_INT32 * OPJ_RESTRICT tiledp;
  1824. OPJ_UINT32 min_j;
  1825. OPJ_UINT32 max_j;
  1826. } opj_dwt_decode_v_job_t;
  1827. static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls)
  1828. {
  1829. OPJ_UINT32 j;
  1830. opj_dwt_decode_v_job_t* job;
  1831. (void)tls;
  1832. job = (opj_dwt_decode_v_job_t*)user_data;
  1833. for (j = job->min_j; j + PARALLEL_COLS_53 <= job->max_j;
  1834. j += PARALLEL_COLS_53) {
  1835. opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w,
  1836. PARALLEL_COLS_53);
  1837. }
  1838. if (j < job->max_j)
  1839. opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w,
  1840. (OPJ_INT32)(job->max_j - j));
  1841. opj_aligned_free(job->v.mem);
  1842. opj_free(job);
  1843. }
  1844. /* <summary> */
  1845. /* Inverse wavelet transform in 2-D. */
  1846. /* </summary> */
  1847. static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp,
  1848. opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres)
  1849. {
  1850. opj_dwt_t h;
  1851. opj_dwt_t v;
  1852. opj_tcd_resolution_t* tr = tilec->resolutions;
  1853. OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
  1854. tr->x0); /* width of the resolution level computed */
  1855. OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
  1856. tr->y0); /* height of the resolution level computed */
  1857. OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
  1858. 1].x1 -
  1859. tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
  1860. OPJ_SIZE_T h_mem_size;
  1861. int num_threads;
  1862. if (numres == 1U) {
  1863. return OPJ_TRUE;
  1864. }
  1865. num_threads = opj_thread_pool_get_thread_count(tp);
  1866. h_mem_size = opj_dwt_max_resolution(tr, numres);
  1867. /* overflow check */
  1868. if (h_mem_size > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) {
  1869. /* FIXME event manager error callback */
  1870. return OPJ_FALSE;
  1871. }
  1872. /* We need PARALLEL_COLS_53 times the height of the array, */
  1873. /* since for the vertical pass */
  1874. /* we process PARALLEL_COLS_53 columns at a time */
  1875. h_mem_size *= PARALLEL_COLS_53 * sizeof(OPJ_INT32);
  1876. h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
  1877. if (! h.mem) {
  1878. /* FIXME event manager error callback */
  1879. return OPJ_FALSE;
  1880. }
  1881. v.mem = h.mem;
  1882. while (--numres) {
  1883. OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data;
  1884. OPJ_UINT32 j;
  1885. ++tr;
  1886. h.sn = (OPJ_INT32)rw;
  1887. v.sn = (OPJ_INT32)rh;
  1888. rw = (OPJ_UINT32)(tr->x1 - tr->x0);
  1889. rh = (OPJ_UINT32)(tr->y1 - tr->y0);
  1890. h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
  1891. h.cas = tr->x0 % 2;
  1892. if (num_threads <= 1 || rh <= 1) {
  1893. for (j = 0; j < rh; ++j) {
  1894. opj_idwt53_h(&h, &tiledp[(OPJ_SIZE_T)j * w]);
  1895. }
  1896. } else {
  1897. OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
  1898. OPJ_UINT32 step_j;
  1899. if (rh < num_jobs) {
  1900. num_jobs = rh;
  1901. }
  1902. step_j = (rh / num_jobs);
  1903. for (j = 0; j < num_jobs; j++) {
  1904. opj_dwt_decode_h_job_t* job;
  1905. job = (opj_dwt_decode_h_job_t*) opj_malloc(sizeof(opj_dwt_decode_h_job_t));
  1906. if (!job) {
  1907. /* It would be nice to fallback to single thread case, but */
  1908. /* unfortunately some jobs may be launched and have modified */
  1909. /* tiledp, so it is not practical to recover from that error */
  1910. /* FIXME event manager error callback */
  1911. opj_thread_pool_wait_completion(tp, 0);
  1912. opj_aligned_free(h.mem);
  1913. return OPJ_FALSE;
  1914. }
  1915. job->h = h;
  1916. job->rw = rw;
  1917. job->w = w;
  1918. job->tiledp = tiledp;
  1919. job->min_j = j * step_j;
  1920. job->max_j = (j + 1U) * step_j; /* this can overflow */
  1921. if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
  1922. job->max_j = rh;
  1923. }
  1924. job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
  1925. if (!job->h.mem) {
  1926. /* FIXME event manager error callback */
  1927. opj_thread_pool_wait_completion(tp, 0);
  1928. opj_free(job);
  1929. opj_aligned_free(h.mem);
  1930. return OPJ_FALSE;
  1931. }
  1932. opj_thread_pool_submit_job(tp, opj_dwt_decode_h_func, job);
  1933. }
  1934. opj_thread_pool_wait_completion(tp, 0);
  1935. }
  1936. v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
  1937. v.cas = tr->y0 % 2;
  1938. if (num_threads <= 1 || rw <= 1) {
  1939. for (j = 0; j + PARALLEL_COLS_53 <= rw;
  1940. j += PARALLEL_COLS_53) {
  1941. opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, PARALLEL_COLS_53);
  1942. }
  1943. if (j < rw) {
  1944. opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, (OPJ_INT32)(rw - j));
  1945. }
  1946. } else {
  1947. OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
  1948. OPJ_UINT32 step_j;
  1949. if (rw < num_jobs) {
  1950. num_jobs = rw;
  1951. }
  1952. step_j = (rw / num_jobs);
  1953. for (j = 0; j < num_jobs; j++) {
  1954. opj_dwt_decode_v_job_t* job;
  1955. job = (opj_dwt_decode_v_job_t*) opj_malloc(sizeof(opj_dwt_decode_v_job_t));
  1956. if (!job) {
  1957. /* It would be nice to fallback to single thread case, but */
  1958. /* unfortunately some jobs may be launched and have modified */
  1959. /* tiledp, so it is not practical to recover from that error */
  1960. /* FIXME event manager error callback */
  1961. opj_thread_pool_wait_completion(tp, 0);
  1962. opj_aligned_free(v.mem);
  1963. return OPJ_FALSE;
  1964. }
  1965. job->v = v;
  1966. job->rh = rh;
  1967. job->w = w;
  1968. job->tiledp = tiledp;
  1969. job->min_j = j * step_j;
  1970. job->max_j = (j + 1U) * step_j; /* this can overflow */
  1971. if (j == (num_jobs - 1U)) { /* this will take care of the overflow */
  1972. job->max_j = rw;
  1973. }
  1974. job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
  1975. if (!job->v.mem) {
  1976. /* FIXME event manager error callback */
  1977. opj_thread_pool_wait_completion(tp, 0);
  1978. opj_free(job);
  1979. opj_aligned_free(v.mem);
  1980. return OPJ_FALSE;
  1981. }
  1982. opj_thread_pool_submit_job(tp, opj_dwt_decode_v_func, job);
  1983. }
  1984. opj_thread_pool_wait_completion(tp, 0);
  1985. }
  1986. }
  1987. opj_aligned_free(h.mem);
  1988. return OPJ_TRUE;
  1989. }
  1990. static void opj_dwt_interleave_partial_h(OPJ_INT32 *dest,
  1991. OPJ_INT32 cas,
  1992. opj_sparse_array_int32_t* sa,
  1993. OPJ_UINT32 sa_line,
  1994. OPJ_UINT32 sn,
  1995. OPJ_UINT32 win_l_x0,
  1996. OPJ_UINT32 win_l_x1,
  1997. OPJ_UINT32 win_h_x0,
  1998. OPJ_UINT32 win_h_x1)
  1999. {
  2000. OPJ_BOOL ret;
  2001. ret = opj_sparse_array_int32_read(sa,
  2002. win_l_x0, sa_line,
  2003. win_l_x1, sa_line + 1,
  2004. dest + cas + 2 * win_l_x0,
  2005. 2, 0, OPJ_TRUE);
  2006. assert(ret);
  2007. ret = opj_sparse_array_int32_read(sa,
  2008. sn + win_h_x0, sa_line,
  2009. sn + win_h_x1, sa_line + 1,
  2010. dest + 1 - cas + 2 * win_h_x0,
  2011. 2, 0, OPJ_TRUE);
  2012. assert(ret);
  2013. OPJ_UNUSED(ret);
  2014. }
  2015. static void opj_dwt_interleave_partial_v(OPJ_INT32 *dest,
  2016. OPJ_INT32 cas,
  2017. opj_sparse_array_int32_t* sa,
  2018. OPJ_UINT32 sa_col,
  2019. OPJ_UINT32 nb_cols,
  2020. OPJ_UINT32 sn,
  2021. OPJ_UINT32 win_l_y0,
  2022. OPJ_UINT32 win_l_y1,
  2023. OPJ_UINT32 win_h_y0,
  2024. OPJ_UINT32 win_h_y1)
  2025. {
  2026. OPJ_BOOL ret;
  2027. ret = opj_sparse_array_int32_read(sa,
  2028. sa_col, win_l_y0,
  2029. sa_col + nb_cols, win_l_y1,
  2030. dest + cas * 4 + 2 * 4 * win_l_y0,
  2031. 1, 2 * 4, OPJ_TRUE);
  2032. assert(ret);
  2033. ret = opj_sparse_array_int32_read(sa,
  2034. sa_col, sn + win_h_y0,
  2035. sa_col + nb_cols, sn + win_h_y1,
  2036. dest + (1 - cas) * 4 + 2 * 4 * win_h_y0,
  2037. 1, 2 * 4, OPJ_TRUE);
  2038. assert(ret);
  2039. OPJ_UNUSED(ret);
  2040. }
  2041. static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn,
  2042. OPJ_INT32 cas,
  2043. OPJ_INT32 win_l_x0,
  2044. OPJ_INT32 win_l_x1,
  2045. OPJ_INT32 win_h_x0,
  2046. OPJ_INT32 win_h_x1)
  2047. {
  2048. OPJ_INT32 i;
  2049. if (!cas) {
  2050. if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
  2051. /* Naive version is :
  2052. for (i = win_l_x0; i < i_max; i++) {
  2053. OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
  2054. }
  2055. for (i = win_h_x0; i < win_h_x1; i++) {
  2056. OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
  2057. }
  2058. but the compiler doesn't manage to unroll it to avoid bound
  2059. checking in OPJ_S_ and OPJ_D_ macros
  2060. */
  2061. i = win_l_x0;
  2062. if (i < win_l_x1) {
  2063. OPJ_INT32 i_max;
  2064. /* Left-most case */
  2065. OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
  2066. i ++;
  2067. i_max = win_l_x1;
  2068. if (i_max > dn) {
  2069. i_max = dn;
  2070. }
  2071. for (; i < i_max; i++) {
  2072. /* No bound checking */
  2073. OPJ_S(i) -= (OPJ_D(i - 1) + OPJ_D(i) + 2) >> 2;
  2074. }
  2075. for (; i < win_l_x1; i++) {
  2076. /* Right-most case */
  2077. OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
  2078. }
  2079. }
  2080. i = win_h_x0;
  2081. if (i < win_h_x1) {
  2082. OPJ_INT32 i_max = win_h_x1;
  2083. if (i_max >= sn) {
  2084. i_max = sn - 1;
  2085. }
  2086. for (; i < i_max; i++) {
  2087. /* No bound checking */
  2088. OPJ_D(i) += (OPJ_S(i) + OPJ_S(i + 1)) >> 1;
  2089. }
  2090. for (; i < win_h_x1; i++) {
  2091. /* Right-most case */
  2092. OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
  2093. }
  2094. }
  2095. }
  2096. } else {
  2097. if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
  2098. OPJ_S(0) /= 2;
  2099. } else {
  2100. for (i = win_l_x0; i < win_l_x1; i++) {
  2101. OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
  2102. }
  2103. for (i = win_h_x0; i < win_h_x1; i++) {
  2104. OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
  2105. }
  2106. }
  2107. }
  2108. }
  2109. #define OPJ_S_off(i,off) a[(OPJ_UINT32)(i)*2*4+off]
  2110. #define OPJ_D_off(i,off) a[(1+(OPJ_UINT32)(i)*2)*4+off]
  2111. #define OPJ_S__off(i,off) ((i)<0?OPJ_S_off(0,off):((i)>=sn?OPJ_S_off(sn-1,off):OPJ_S_off(i,off)))
  2112. #define OPJ_D__off(i,off) ((i)<0?OPJ_D_off(0,off):((i)>=dn?OPJ_D_off(dn-1,off):OPJ_D_off(i,off)))
  2113. #define OPJ_SS__off(i,off) ((i)<0?OPJ_S_off(0,off):((i)>=dn?OPJ_S_off(dn-1,off):OPJ_S_off(i,off)))
  2114. #define OPJ_DD__off(i,off) ((i)<0?OPJ_D_off(0,off):((i)>=sn?OPJ_D_off(sn-1,off):OPJ_D_off(i,off)))
  2115. static void opj_dwt_decode_partial_1_parallel(OPJ_INT32 *a,
  2116. OPJ_UINT32 nb_cols,
  2117. OPJ_INT32 dn, OPJ_INT32 sn,
  2118. OPJ_INT32 cas,
  2119. OPJ_INT32 win_l_x0,
  2120. OPJ_INT32 win_l_x1,
  2121. OPJ_INT32 win_h_x0,
  2122. OPJ_INT32 win_h_x1)
  2123. {
  2124. OPJ_INT32 i;
  2125. OPJ_UINT32 off;
  2126. (void)nb_cols;
  2127. if (!cas) {
  2128. if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
  2129. /* Naive version is :
  2130. for (i = win_l_x0; i < i_max; i++) {
  2131. OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
  2132. }
  2133. for (i = win_h_x0; i < win_h_x1; i++) {
  2134. OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
  2135. }
  2136. but the compiler doesn't manage to unroll it to avoid bound
  2137. checking in OPJ_S_ and OPJ_D_ macros
  2138. */
  2139. i = win_l_x0;
  2140. if (i < win_l_x1) {
  2141. OPJ_INT32 i_max;
  2142. /* Left-most case */
  2143. for (off = 0; off < 4; off++) {
  2144. OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2;
  2145. }
  2146. i ++;
  2147. i_max = win_l_x1;
  2148. if (i_max > dn) {
  2149. i_max = dn;
  2150. }
  2151. #ifdef __SSE2__
  2152. if (i + 1 < i_max) {
  2153. const __m128i two = _mm_set1_epi32(2);
  2154. __m128i Dm1 = _mm_load_si128((__m128i * const)(a + 4 + (i - 1) * 8));
  2155. for (; i + 1 < i_max; i += 2) {
  2156. /* No bound checking */
  2157. __m128i S = _mm_load_si128((__m128i * const)(a + i * 8));
  2158. __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8));
  2159. __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8));
  2160. __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8));
  2161. S = _mm_sub_epi32(S,
  2162. _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(Dm1, D), two), 2));
  2163. S1 = _mm_sub_epi32(S1,
  2164. _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(D, D1), two), 2));
  2165. _mm_store_si128((__m128i*)(a + i * 8), S);
  2166. _mm_store_si128((__m128i*)(a + (i + 1) * 8), S1);
  2167. Dm1 = D1;
  2168. }
  2169. }
  2170. #endif
  2171. for (; i < i_max; i++) {
  2172. /* No bound checking */
  2173. for (off = 0; off < 4; off++) {
  2174. OPJ_S_off(i, off) -= (OPJ_D_off(i - 1, off) + OPJ_D_off(i, off) + 2) >> 2;
  2175. }
  2176. }
  2177. for (; i < win_l_x1; i++) {
  2178. /* Right-most case */
  2179. for (off = 0; off < 4; off++) {
  2180. OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2;
  2181. }
  2182. }
  2183. }
  2184. i = win_h_x0;
  2185. if (i < win_h_x1) {
  2186. OPJ_INT32 i_max = win_h_x1;
  2187. if (i_max >= sn) {
  2188. i_max = sn - 1;
  2189. }
  2190. #ifdef __SSE2__
  2191. if (i + 1 < i_max) {
  2192. __m128i S = _mm_load_si128((__m128i * const)(a + i * 8));
  2193. for (; i + 1 < i_max; i += 2) {
  2194. /* No bound checking */
  2195. __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8));
  2196. __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8));
  2197. __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8));
  2198. __m128i S2 = _mm_load_si128((__m128i * const)(a + (i + 2) * 8));
  2199. D = _mm_add_epi32(D, _mm_srai_epi32(_mm_add_epi32(S, S1), 1));
  2200. D1 = _mm_add_epi32(D1, _mm_srai_epi32(_mm_add_epi32(S1, S2), 1));
  2201. _mm_store_si128((__m128i*)(a + 4 + i * 8), D);
  2202. _mm_store_si128((__m128i*)(a + 4 + (i + 1) * 8), D1);
  2203. S = S2;
  2204. }
  2205. }
  2206. #endif
  2207. for (; i < i_max; i++) {
  2208. /* No bound checking */
  2209. for (off = 0; off < 4; off++) {
  2210. OPJ_D_off(i, off) += (OPJ_S_off(i, off) + OPJ_S_off(i + 1, off)) >> 1;
  2211. }
  2212. }
  2213. for (; i < win_h_x1; i++) {
  2214. /* Right-most case */
  2215. for (off = 0; off < 4; off++) {
  2216. OPJ_D_off(i, off) += (OPJ_S__off(i, off) + OPJ_S__off(i + 1, off)) >> 1;
  2217. }
  2218. }
  2219. }
  2220. }
  2221. } else {
  2222. if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */
  2223. for (off = 0; off < 4; off++) {
  2224. OPJ_S_off(0, off) /= 2;
  2225. }
  2226. } else {
  2227. for (i = win_l_x0; i < win_l_x1; i++) {
  2228. for (off = 0; off < 4; off++) {
  2229. OPJ_D_off(i, off) -= (OPJ_SS__off(i, off) + OPJ_SS__off(i + 1, off) + 2) >> 2;
  2230. }
  2231. }
  2232. for (i = win_h_x0; i < win_h_x1; i++) {
  2233. for (off = 0; off < 4; off++) {
  2234. OPJ_S_off(i, off) += (OPJ_DD__off(i, off) + OPJ_DD__off(i - 1, off)) >> 1;
  2235. }
  2236. }
  2237. }
  2238. }
  2239. }
  2240. static void opj_dwt_get_band_coordinates(opj_tcd_tilecomp_t* tilec,
  2241. OPJ_UINT32 resno,
  2242. OPJ_UINT32 bandno,
  2243. OPJ_UINT32 tcx0,
  2244. OPJ_UINT32 tcy0,
  2245. OPJ_UINT32 tcx1,
  2246. OPJ_UINT32 tcy1,
  2247. OPJ_UINT32* tbx0,
  2248. OPJ_UINT32* tby0,
  2249. OPJ_UINT32* tbx1,
  2250. OPJ_UINT32* tby1)
  2251. {
  2252. /* Compute number of decomposition for this band. See table F-1 */
  2253. OPJ_UINT32 nb = (resno == 0) ?
  2254. tilec->numresolutions - 1 :
  2255. tilec->numresolutions - resno;
  2256. /* Map above tile-based coordinates to sub-band-based coordinates per */
  2257. /* equation B-15 of the standard */
  2258. OPJ_UINT32 x0b = bandno & 1;
  2259. OPJ_UINT32 y0b = bandno >> 1;
  2260. if (tbx0) {
  2261. *tbx0 = (nb == 0) ? tcx0 :
  2262. (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
  2263. opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
  2264. }
  2265. if (tby0) {
  2266. *tby0 = (nb == 0) ? tcy0 :
  2267. (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
  2268. opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
  2269. }
  2270. if (tbx1) {
  2271. *tbx1 = (nb == 0) ? tcx1 :
  2272. (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
  2273. opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
  2274. }
  2275. if (tby1) {
  2276. *tby1 = (nb == 0) ? tcy1 :
  2277. (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
  2278. opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
  2279. }
  2280. }
  2281. static void opj_dwt_segment_grow(OPJ_UINT32 filter_width,
  2282. OPJ_UINT32 max_size,
  2283. OPJ_UINT32* start,
  2284. OPJ_UINT32* end)
  2285. {
  2286. *start = opj_uint_subs(*start, filter_width);
  2287. *end = opj_uint_adds(*end, filter_width);
  2288. *end = opj_uint_min(*end, max_size);
  2289. }
  2290. static opj_sparse_array_int32_t* opj_dwt_init_sparse_array(
  2291. opj_tcd_tilecomp_t* tilec,
  2292. OPJ_UINT32 numres)
  2293. {
  2294. opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
  2295. OPJ_UINT32 w = (OPJ_UINT32)(tr_max->x1 - tr_max->x0);
  2296. OPJ_UINT32 h = (OPJ_UINT32)(tr_max->y1 - tr_max->y0);
  2297. OPJ_UINT32 resno, bandno, precno, cblkno;
  2298. opj_sparse_array_int32_t* sa = opj_sparse_array_int32_create(
  2299. w, h, opj_uint_min(w, 64), opj_uint_min(h, 64));
  2300. if (sa == NULL) {
  2301. return NULL;
  2302. }
  2303. for (resno = 0; resno < numres; ++resno) {
  2304. opj_tcd_resolution_t* res = &tilec->resolutions[resno];
  2305. for (bandno = 0; bandno < res->numbands; ++bandno) {
  2306. opj_tcd_band_t* band = &res->bands[bandno];
  2307. for (precno = 0; precno < res->pw * res->ph; ++precno) {
  2308. opj_tcd_precinct_t* precinct = &band->precincts[precno];
  2309. for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
  2310. opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
  2311. if (cblk->decoded_data != NULL) {
  2312. OPJ_UINT32 x = (OPJ_UINT32)(cblk->x0 - band->x0);
  2313. OPJ_UINT32 y = (OPJ_UINT32)(cblk->y0 - band->y0);
  2314. OPJ_UINT32 cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
  2315. OPJ_UINT32 cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
  2316. if (band->bandno & 1) {
  2317. opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
  2318. x += (OPJ_UINT32)(pres->x1 - pres->x0);
  2319. }
  2320. if (band->bandno & 2) {
  2321. opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
  2322. y += (OPJ_UINT32)(pres->y1 - pres->y0);
  2323. }
  2324. if (!opj_sparse_array_int32_write(sa, x, y,
  2325. x + cblk_w, y + cblk_h,
  2326. cblk->decoded_data,
  2327. 1, cblk_w, OPJ_TRUE)) {
  2328. opj_sparse_array_int32_free(sa);
  2329. return NULL;
  2330. }
  2331. }
  2332. }
  2333. }
  2334. }
  2335. }
  2336. return sa;
  2337. }
  2338. static OPJ_BOOL opj_dwt_decode_partial_tile(
  2339. opj_tcd_tilecomp_t* tilec,
  2340. OPJ_UINT32 numres)
  2341. {
  2342. opj_sparse_array_int32_t* sa;
  2343. opj_dwt_t h;
  2344. opj_dwt_t v;
  2345. OPJ_UINT32 resno;
  2346. /* This value matches the maximum left/right extension given in tables */
  2347. /* F.2 and F.3 of the standard. */
  2348. const OPJ_UINT32 filter_width = 2U;
  2349. opj_tcd_resolution_t* tr = tilec->resolutions;
  2350. opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
  2351. OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
  2352. tr->x0); /* width of the resolution level computed */
  2353. OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
  2354. tr->y0); /* height of the resolution level computed */
  2355. OPJ_SIZE_T h_mem_size;
  2356. /* Compute the intersection of the area of interest, expressed in tile coordinates */
  2357. /* with the tile coordinates */
  2358. OPJ_UINT32 win_tcx0 = tilec->win_x0;
  2359. OPJ_UINT32 win_tcy0 = tilec->win_y0;
  2360. OPJ_UINT32 win_tcx1 = tilec->win_x1;
  2361. OPJ_UINT32 win_tcy1 = tilec->win_y1;
  2362. if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) {
  2363. return OPJ_TRUE;
  2364. }
  2365. sa = opj_dwt_init_sparse_array(tilec, numres);
  2366. if (sa == NULL) {
  2367. return OPJ_FALSE;
  2368. }
  2369. if (numres == 1U) {
  2370. OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
  2371. tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
  2372. tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
  2373. tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
  2374. tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
  2375. tilec->data_win,
  2376. 1, tr_max->win_x1 - tr_max->win_x0,
  2377. OPJ_TRUE);
  2378. assert(ret);
  2379. OPJ_UNUSED(ret);
  2380. opj_sparse_array_int32_free(sa);
  2381. return OPJ_TRUE;
  2382. }
  2383. h_mem_size = opj_dwt_max_resolution(tr, numres);
  2384. /* overflow check */
  2385. /* in vertical pass, we process 4 columns at a time */
  2386. if (h_mem_size > (SIZE_MAX / (4 * sizeof(OPJ_INT32)))) {
  2387. /* FIXME event manager error callback */
  2388. opj_sparse_array_int32_free(sa);
  2389. return OPJ_FALSE;
  2390. }
  2391. h_mem_size *= 4 * sizeof(OPJ_INT32);
  2392. h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size);
  2393. if (! h.mem) {
  2394. /* FIXME event manager error callback */
  2395. opj_sparse_array_int32_free(sa);
  2396. return OPJ_FALSE;
  2397. }
  2398. v.mem = h.mem;
  2399. for (resno = 1; resno < numres; resno ++) {
  2400. OPJ_UINT32 i, j;
  2401. /* Window of interest subband-based coordinates */
  2402. OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
  2403. OPJ_UINT32 win_hl_x0, win_hl_x1;
  2404. OPJ_UINT32 win_lh_y0, win_lh_y1;
  2405. /* Window of interest tile-resolution-based coordinates */
  2406. OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
  2407. /* Tile-resolution subband-based coordinates */
  2408. OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
  2409. ++tr;
  2410. h.sn = (OPJ_INT32)rw;
  2411. v.sn = (OPJ_INT32)rh;
  2412. rw = (OPJ_UINT32)(tr->x1 - tr->x0);
  2413. rh = (OPJ_UINT32)(tr->y1 - tr->y0);
  2414. h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
  2415. h.cas = tr->x0 % 2;
  2416. v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
  2417. v.cas = tr->y0 % 2;
  2418. /* Get the subband coordinates for the window of interest */
  2419. /* LL band */
  2420. opj_dwt_get_band_coordinates(tilec, resno, 0,
  2421. win_tcx0, win_tcy0, win_tcx1, win_tcy1,
  2422. &win_ll_x0, &win_ll_y0,
  2423. &win_ll_x1, &win_ll_y1);
  2424. /* HL band */
  2425. opj_dwt_get_band_coordinates(tilec, resno, 1,
  2426. win_tcx0, win_tcy0, win_tcx1, win_tcy1,
  2427. &win_hl_x0, NULL, &win_hl_x1, NULL);
  2428. /* LH band */
  2429. opj_dwt_get_band_coordinates(tilec, resno, 2,
  2430. win_tcx0, win_tcy0, win_tcx1, win_tcy1,
  2431. NULL, &win_lh_y0, NULL, &win_lh_y1);
  2432. /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
  2433. tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
  2434. tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
  2435. tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
  2436. tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
  2437. /* Subtract the origin of the bands for this tile, to the subwindow */
  2438. /* of interest band coordinates, so as to get them relative to the */
  2439. /* tile */
  2440. win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
  2441. win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
  2442. win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
  2443. win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
  2444. win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
  2445. win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
  2446. win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
  2447. win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
  2448. opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
  2449. opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
  2450. opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
  2451. opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
  2452. /* Compute the tile-resolution-based coordinates for the window of interest */
  2453. if (h.cas == 0) {
  2454. win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
  2455. win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
  2456. } else {
  2457. win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
  2458. win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
  2459. }
  2460. if (v.cas == 0) {
  2461. win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
  2462. win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
  2463. } else {
  2464. win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
  2465. win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
  2466. }
  2467. for (j = 0; j < rh; ++j) {
  2468. if ((j >= win_ll_y0 && j < win_ll_y1) ||
  2469. (j >= win_lh_y0 + (OPJ_UINT32)v.sn && j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
  2470. /* Avoids dwt.c:1584:44 (in opj_dwt_decode_partial_1): runtime error: */
  2471. /* signed integer overflow: -1094795586 + -1094795586 cannot be represented in type 'int' */
  2472. /* on opj_decompress -i ../../openjpeg/MAPA.jp2 -o out.tif -d 0,0,256,256 */
  2473. /* This is less extreme than memsetting the whole buffer to 0 */
  2474. /* although we could potentially do better with better handling of edge conditions */
  2475. if (win_tr_x1 >= 1 && win_tr_x1 < rw) {
  2476. h.mem[win_tr_x1 - 1] = 0;
  2477. }
  2478. if (win_tr_x1 < rw) {
  2479. h.mem[win_tr_x1] = 0;
  2480. }
  2481. opj_dwt_interleave_partial_h(h.mem,
  2482. h.cas,
  2483. sa,
  2484. j,
  2485. (OPJ_UINT32)h.sn,
  2486. win_ll_x0,
  2487. win_ll_x1,
  2488. win_hl_x0,
  2489. win_hl_x1);
  2490. opj_dwt_decode_partial_1(h.mem, h.dn, h.sn, h.cas,
  2491. (OPJ_INT32)win_ll_x0,
  2492. (OPJ_INT32)win_ll_x1,
  2493. (OPJ_INT32)win_hl_x0,
  2494. (OPJ_INT32)win_hl_x1);
  2495. if (!opj_sparse_array_int32_write(sa,
  2496. win_tr_x0, j,
  2497. win_tr_x1, j + 1,
  2498. h.mem + win_tr_x0,
  2499. 1, 0, OPJ_TRUE)) {
  2500. /* FIXME event manager error callback */
  2501. opj_sparse_array_int32_free(sa);
  2502. opj_aligned_free(h.mem);
  2503. return OPJ_FALSE;
  2504. }
  2505. }
  2506. }
  2507. for (i = win_tr_x0; i < win_tr_x1;) {
  2508. OPJ_UINT32 nb_cols = opj_uint_min(4U, win_tr_x1 - i);
  2509. opj_dwt_interleave_partial_v(v.mem,
  2510. v.cas,
  2511. sa,
  2512. i,
  2513. nb_cols,
  2514. (OPJ_UINT32)v.sn,
  2515. win_ll_y0,
  2516. win_ll_y1,
  2517. win_lh_y0,
  2518. win_lh_y1);
  2519. opj_dwt_decode_partial_1_parallel(v.mem, nb_cols, v.dn, v.sn, v.cas,
  2520. (OPJ_INT32)win_ll_y0,
  2521. (OPJ_INT32)win_ll_y1,
  2522. (OPJ_INT32)win_lh_y0,
  2523. (OPJ_INT32)win_lh_y1);
  2524. if (!opj_sparse_array_int32_write(sa,
  2525. i, win_tr_y0,
  2526. i + nb_cols, win_tr_y1,
  2527. v.mem + 4 * win_tr_y0,
  2528. 1, 4, OPJ_TRUE)) {
  2529. /* FIXME event manager error callback */
  2530. opj_sparse_array_int32_free(sa);
  2531. opj_aligned_free(h.mem);
  2532. return OPJ_FALSE;
  2533. }
  2534. i += nb_cols;
  2535. }
  2536. }
  2537. opj_aligned_free(h.mem);
  2538. {
  2539. OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
  2540. tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
  2541. tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
  2542. tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
  2543. tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
  2544. tilec->data_win,
  2545. 1, tr_max->win_x1 - tr_max->win_x0,
  2546. OPJ_TRUE);
  2547. assert(ret);
  2548. OPJ_UNUSED(ret);
  2549. }
  2550. opj_sparse_array_int32_free(sa);
  2551. return OPJ_TRUE;
  2552. }
  2553. static void opj_v8dwt_interleave_h(opj_v8dwt_t* OPJ_RESTRICT dwt,
  2554. OPJ_FLOAT32* OPJ_RESTRICT a,
  2555. OPJ_UINT32 width,
  2556. OPJ_UINT32 remaining_height)
  2557. {
  2558. OPJ_FLOAT32* OPJ_RESTRICT bi = (OPJ_FLOAT32*)(dwt->wavelet + dwt->cas);
  2559. OPJ_UINT32 i, k;
  2560. OPJ_UINT32 x0 = dwt->win_l_x0;
  2561. OPJ_UINT32 x1 = dwt->win_l_x1;
  2562. for (k = 0; k < 2; ++k) {
  2563. if (remaining_height >= NB_ELTS_V8 && ((OPJ_SIZE_T) a & 0x0f) == 0 &&
  2564. ((OPJ_SIZE_T) bi & 0x0f) == 0) {
  2565. /* Fast code path */
  2566. for (i = x0; i < x1; ++i) {
  2567. OPJ_UINT32 j = i;
  2568. OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8;
  2569. dst[0] = a[j];
  2570. j += width;
  2571. dst[1] = a[j];
  2572. j += width;
  2573. dst[2] = a[j];
  2574. j += width;
  2575. dst[3] = a[j];
  2576. j += width;
  2577. dst[4] = a[j];
  2578. j += width;
  2579. dst[5] = a[j];
  2580. j += width;
  2581. dst[6] = a[j];
  2582. j += width;
  2583. dst[7] = a[j];
  2584. }
  2585. } else {
  2586. /* Slow code path */
  2587. for (i = x0; i < x1; ++i) {
  2588. OPJ_UINT32 j = i;
  2589. OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8;
  2590. dst[0] = a[j];
  2591. j += width;
  2592. if (remaining_height == 1) {
  2593. continue;
  2594. }
  2595. dst[1] = a[j];
  2596. j += width;
  2597. if (remaining_height == 2) {
  2598. continue;
  2599. }
  2600. dst[2] = a[j];
  2601. j += width;
  2602. if (remaining_height == 3) {
  2603. continue;
  2604. }
  2605. dst[3] = a[j];
  2606. j += width;
  2607. if (remaining_height == 4) {
  2608. continue;
  2609. }
  2610. dst[4] = a[j];
  2611. j += width;
  2612. if (remaining_height == 5) {
  2613. continue;
  2614. }
  2615. dst[5] = a[j];
  2616. j += width;
  2617. if (remaining_height == 6) {
  2618. continue;
  2619. }
  2620. dst[6] = a[j];
  2621. j += width;
  2622. if (remaining_height == 7) {
  2623. continue;
  2624. }
  2625. dst[7] = a[j];
  2626. }
  2627. }
  2628. bi = (OPJ_FLOAT32*)(dwt->wavelet + 1 - dwt->cas);
  2629. a += dwt->sn;
  2630. x0 = dwt->win_h_x0;
  2631. x1 = dwt->win_h_x1;
  2632. }
  2633. }
  2634. static void opj_v8dwt_interleave_partial_h(opj_v8dwt_t* dwt,
  2635. opj_sparse_array_int32_t* sa,
  2636. OPJ_UINT32 sa_line,
  2637. OPJ_UINT32 remaining_height)
  2638. {
  2639. OPJ_UINT32 i;
  2640. for (i = 0; i < remaining_height; i++) {
  2641. OPJ_BOOL ret;
  2642. ret = opj_sparse_array_int32_read(sa,
  2643. dwt->win_l_x0, sa_line + i,
  2644. dwt->win_l_x1, sa_line + i + 1,
  2645. /* Nasty cast from float* to int32* */
  2646. (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0) + i,
  2647. 2 * NB_ELTS_V8, 0, OPJ_TRUE);
  2648. assert(ret);
  2649. ret = opj_sparse_array_int32_read(sa,
  2650. (OPJ_UINT32)dwt->sn + dwt->win_h_x0, sa_line + i,
  2651. (OPJ_UINT32)dwt->sn + dwt->win_h_x1, sa_line + i + 1,
  2652. /* Nasty cast from float* to int32* */
  2653. (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0) + i,
  2654. 2 * NB_ELTS_V8, 0, OPJ_TRUE);
  2655. assert(ret);
  2656. OPJ_UNUSED(ret);
  2657. }
  2658. }
  2659. static INLINE void opj_v8dwt_interleave_v(opj_v8dwt_t* OPJ_RESTRICT dwt,
  2660. OPJ_FLOAT32* OPJ_RESTRICT a,
  2661. OPJ_UINT32 width,
  2662. OPJ_UINT32 nb_elts_read)
  2663. {
  2664. opj_v8_t* OPJ_RESTRICT bi = dwt->wavelet + dwt->cas;
  2665. OPJ_UINT32 i;
  2666. for (i = dwt->win_l_x0; i < dwt->win_l_x1; ++i) {
  2667. memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width],
  2668. (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32));
  2669. }
  2670. a += (OPJ_UINT32)dwt->sn * (OPJ_SIZE_T)width;
  2671. bi = dwt->wavelet + 1 - dwt->cas;
  2672. for (i = dwt->win_h_x0; i < dwt->win_h_x1; ++i) {
  2673. memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width],
  2674. (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32));
  2675. }
  2676. }
  2677. static void opj_v8dwt_interleave_partial_v(opj_v8dwt_t* OPJ_RESTRICT dwt,
  2678. opj_sparse_array_int32_t* sa,
  2679. OPJ_UINT32 sa_col,
  2680. OPJ_UINT32 nb_elts_read)
  2681. {
  2682. OPJ_BOOL ret;
  2683. ret = opj_sparse_array_int32_read(sa,
  2684. sa_col, dwt->win_l_x0,
  2685. sa_col + nb_elts_read, dwt->win_l_x1,
  2686. (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0),
  2687. 1, 2 * NB_ELTS_V8, OPJ_TRUE);
  2688. assert(ret);
  2689. ret = opj_sparse_array_int32_read(sa,
  2690. sa_col, (OPJ_UINT32)dwt->sn + dwt->win_h_x0,
  2691. sa_col + nb_elts_read, (OPJ_UINT32)dwt->sn + dwt->win_h_x1,
  2692. (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0),
  2693. 1, 2 * NB_ELTS_V8, OPJ_TRUE);
  2694. assert(ret);
  2695. OPJ_UNUSED(ret);
  2696. }
  2697. #ifdef __SSE__
  2698. static void opj_v8dwt_decode_step1_sse(opj_v8_t* w,
  2699. OPJ_UINT32 start,
  2700. OPJ_UINT32 end,
  2701. const __m128 c)
  2702. {
  2703. __m128* OPJ_RESTRICT vw = (__m128*) w;
  2704. OPJ_UINT32 i = start;
  2705. /* To be adapted if NB_ELTS_V8 changes */
  2706. vw += 4 * start;
  2707. /* Note: attempt at loop unrolling x2 doesn't help */
  2708. for (; i < end; ++i, vw += 4) {
  2709. vw[0] = _mm_mul_ps(vw[0], c);
  2710. vw[1] = _mm_mul_ps(vw[1], c);
  2711. }
  2712. }
  2713. static void opj_v8dwt_decode_step2_sse(opj_v8_t* l, opj_v8_t* w,
  2714. OPJ_UINT32 start,
  2715. OPJ_UINT32 end,
  2716. OPJ_UINT32 m,
  2717. __m128 c)
  2718. {
  2719. __m128* OPJ_RESTRICT vl = (__m128*) l;
  2720. __m128* OPJ_RESTRICT vw = (__m128*) w;
  2721. /* To be adapted if NB_ELTS_V8 changes */
  2722. OPJ_UINT32 i;
  2723. OPJ_UINT32 imax = opj_uint_min(end, m);
  2724. if (start == 0) {
  2725. if (imax >= 1) {
  2726. vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vl[0], vw[0]), c));
  2727. vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vl[1], vw[1]), c));
  2728. vw += 4;
  2729. start = 1;
  2730. }
  2731. } else {
  2732. vw += start * 4;
  2733. }
  2734. i = start;
  2735. /* Note: attempt at loop unrolling x2 doesn't help */
  2736. for (; i < imax; ++i) {
  2737. vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vw[-4], vw[0]), c));
  2738. vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vw[-3], vw[1]), c));
  2739. vw += 4;
  2740. }
  2741. if (m < end) {
  2742. assert(m + 1 == end);
  2743. c = _mm_add_ps(c, c);
  2744. vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(c, vw[-4]));
  2745. vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(c, vw[-3]));
  2746. }
  2747. }
  2748. #else
  2749. static void opj_v8dwt_decode_step1(opj_v8_t* w,
  2750. OPJ_UINT32 start,
  2751. OPJ_UINT32 end,
  2752. const OPJ_FLOAT32 c)
  2753. {
  2754. OPJ_FLOAT32* OPJ_RESTRICT fw = (OPJ_FLOAT32*) w;
  2755. OPJ_UINT32 i;
  2756. /* To be adapted if NB_ELTS_V8 changes */
  2757. for (i = start; i < end; ++i) {
  2758. fw[i * 2 * 8 ] = fw[i * 2 * 8 ] * c;
  2759. fw[i * 2 * 8 + 1] = fw[i * 2 * 8 + 1] * c;
  2760. fw[i * 2 * 8 + 2] = fw[i * 2 * 8 + 2] * c;
  2761. fw[i * 2 * 8 + 3] = fw[i * 2 * 8 + 3] * c;
  2762. fw[i * 2 * 8 + 4] = fw[i * 2 * 8 + 4] * c;
  2763. fw[i * 2 * 8 + 5] = fw[i * 2 * 8 + 5] * c;
  2764. fw[i * 2 * 8 + 6] = fw[i * 2 * 8 + 6] * c;
  2765. fw[i * 2 * 8 + 7] = fw[i * 2 * 8 + 7] * c;
  2766. }
  2767. }
  2768. static void opj_v8dwt_decode_step2(opj_v8_t* l, opj_v8_t* w,
  2769. OPJ_UINT32 start,
  2770. OPJ_UINT32 end,
  2771. OPJ_UINT32 m,
  2772. OPJ_FLOAT32 c)
  2773. {
  2774. OPJ_FLOAT32* fl = (OPJ_FLOAT32*) l;
  2775. OPJ_FLOAT32* fw = (OPJ_FLOAT32*) w;
  2776. OPJ_UINT32 i;
  2777. OPJ_UINT32 imax = opj_uint_min(end, m);
  2778. if (start > 0) {
  2779. fw += 2 * NB_ELTS_V8 * start;
  2780. fl = fw - 2 * NB_ELTS_V8;
  2781. }
  2782. /* To be adapted if NB_ELTS_V8 changes */
  2783. for (i = start; i < imax; ++i) {
  2784. fw[-8] = fw[-8] + ((fl[0] + fw[0]) * c);
  2785. fw[-7] = fw[-7] + ((fl[1] + fw[1]) * c);
  2786. fw[-6] = fw[-6] + ((fl[2] + fw[2]) * c);
  2787. fw[-5] = fw[-5] + ((fl[3] + fw[3]) * c);
  2788. fw[-4] = fw[-4] + ((fl[4] + fw[4]) * c);
  2789. fw[-3] = fw[-3] + ((fl[5] + fw[5]) * c);
  2790. fw[-2] = fw[-2] + ((fl[6] + fw[6]) * c);
  2791. fw[-1] = fw[-1] + ((fl[7] + fw[7]) * c);
  2792. fl = fw;
  2793. fw += 2 * NB_ELTS_V8;
  2794. }
  2795. if (m < end) {
  2796. assert(m + 1 == end);
  2797. c += c;
  2798. fw[-8] = fw[-8] + fl[0] * c;
  2799. fw[-7] = fw[-7] + fl[1] * c;
  2800. fw[-6] = fw[-6] + fl[2] * c;
  2801. fw[-5] = fw[-5] + fl[3] * c;
  2802. fw[-4] = fw[-4] + fl[4] * c;
  2803. fw[-3] = fw[-3] + fl[5] * c;
  2804. fw[-2] = fw[-2] + fl[6] * c;
  2805. fw[-1] = fw[-1] + fl[7] * c;
  2806. }
  2807. }
  2808. #endif
  2809. /* <summary> */
  2810. /* Inverse 9-7 wavelet transform in 1-D. */
  2811. /* </summary> */
  2812. static void opj_v8dwt_decode(opj_v8dwt_t* OPJ_RESTRICT dwt)
  2813. {
  2814. OPJ_INT32 a, b;
  2815. /* BUG_WEIRD_TWO_INVK (look for this identifier in tcd.c) */
  2816. /* Historic value for 2 / opj_invK */
  2817. /* Normally, we should use invK, but if we do so, we have failures in the */
  2818. /* conformance test, due to MSE and peak errors significantly higher than */
  2819. /* accepted value */
  2820. /* Due to using two_invK instead of invK, we have to compensate in tcd.c */
  2821. /* the computation of the stepsize for the non LL subbands */
  2822. const float two_invK = 1.625732422f;
  2823. if (dwt->cas == 0) {
  2824. if (!((dwt->dn > 0) || (dwt->sn > 1))) {
  2825. return;
  2826. }
  2827. a = 0;
  2828. b = 1;
  2829. } else {
  2830. if (!((dwt->sn > 0) || (dwt->dn > 1))) {
  2831. return;
  2832. }
  2833. a = 1;
  2834. b = 0;
  2835. }
  2836. #ifdef __SSE__
  2837. opj_v8dwt_decode_step1_sse(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
  2838. _mm_set1_ps(opj_K));
  2839. opj_v8dwt_decode_step1_sse(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
  2840. _mm_set1_ps(two_invK));
  2841. opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
  2842. dwt->win_l_x0, dwt->win_l_x1,
  2843. (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
  2844. _mm_set1_ps(-opj_dwt_delta));
  2845. opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
  2846. dwt->win_h_x0, dwt->win_h_x1,
  2847. (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
  2848. _mm_set1_ps(-opj_dwt_gamma));
  2849. opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1,
  2850. dwt->win_l_x0, dwt->win_l_x1,
  2851. (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
  2852. _mm_set1_ps(-opj_dwt_beta));
  2853. opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1,
  2854. dwt->win_h_x0, dwt->win_h_x1,
  2855. (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
  2856. _mm_set1_ps(-opj_dwt_alpha));
  2857. #else
  2858. opj_v8dwt_decode_step1(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1,
  2859. opj_K);
  2860. opj_v8dwt_decode_step1(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1,
  2861. two_invK);
  2862. opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
  2863. dwt->win_l_x0, dwt->win_l_x1,
  2864. (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
  2865. -opj_dwt_delta);
  2866. opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
  2867. dwt->win_h_x0, dwt->win_h_x1,
  2868. (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
  2869. -opj_dwt_gamma);
  2870. opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1,
  2871. dwt->win_l_x0, dwt->win_l_x1,
  2872. (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a),
  2873. -opj_dwt_beta);
  2874. opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1,
  2875. dwt->win_h_x0, dwt->win_h_x1,
  2876. (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b),
  2877. -opj_dwt_alpha);
  2878. #endif
  2879. }
  2880. typedef struct {
  2881. opj_v8dwt_t h;
  2882. OPJ_UINT32 rw;
  2883. OPJ_UINT32 w;
  2884. OPJ_FLOAT32 * OPJ_RESTRICT aj;
  2885. OPJ_UINT32 nb_rows;
  2886. } opj_dwt97_decode_h_job_t;
  2887. static void opj_dwt97_decode_h_func(void* user_data, opj_tls_t* tls)
  2888. {
  2889. OPJ_UINT32 j;
  2890. opj_dwt97_decode_h_job_t* job;
  2891. OPJ_FLOAT32 * OPJ_RESTRICT aj;
  2892. OPJ_UINT32 w;
  2893. (void)tls;
  2894. job = (opj_dwt97_decode_h_job_t*)user_data;
  2895. w = job->w;
  2896. assert((job->nb_rows % NB_ELTS_V8) == 0);
  2897. aj = job->aj;
  2898. for (j = 0; j + NB_ELTS_V8 <= job->nb_rows; j += NB_ELTS_V8) {
  2899. OPJ_UINT32 k;
  2900. opj_v8dwt_interleave_h(&job->h, aj, job->w, NB_ELTS_V8);
  2901. opj_v8dwt_decode(&job->h);
  2902. /* To be adapted if NB_ELTS_V8 changes */
  2903. for (k = 0; k < job->rw; k++) {
  2904. aj[k ] = job->h.wavelet[k].f[0];
  2905. aj[k + (OPJ_SIZE_T)w ] = job->h.wavelet[k].f[1];
  2906. aj[k + (OPJ_SIZE_T)w * 2] = job->h.wavelet[k].f[2];
  2907. aj[k + (OPJ_SIZE_T)w * 3] = job->h.wavelet[k].f[3];
  2908. }
  2909. for (k = 0; k < job->rw; k++) {
  2910. aj[k + (OPJ_SIZE_T)w * 4] = job->h.wavelet[k].f[4];
  2911. aj[k + (OPJ_SIZE_T)w * 5] = job->h.wavelet[k].f[5];
  2912. aj[k + (OPJ_SIZE_T)w * 6] = job->h.wavelet[k].f[6];
  2913. aj[k + (OPJ_SIZE_T)w * 7] = job->h.wavelet[k].f[7];
  2914. }
  2915. aj += w * NB_ELTS_V8;
  2916. }
  2917. opj_aligned_free(job->h.wavelet);
  2918. opj_free(job);
  2919. }
  2920. typedef struct {
  2921. opj_v8dwt_t v;
  2922. OPJ_UINT32 rh;
  2923. OPJ_UINT32 w;
  2924. OPJ_FLOAT32 * OPJ_RESTRICT aj;
  2925. OPJ_UINT32 nb_columns;
  2926. } opj_dwt97_decode_v_job_t;
  2927. static void opj_dwt97_decode_v_func(void* user_data, opj_tls_t* tls)
  2928. {
  2929. OPJ_UINT32 j;
  2930. opj_dwt97_decode_v_job_t* job;
  2931. OPJ_FLOAT32 * OPJ_RESTRICT aj;
  2932. (void)tls;
  2933. job = (opj_dwt97_decode_v_job_t*)user_data;
  2934. assert((job->nb_columns % NB_ELTS_V8) == 0);
  2935. aj = job->aj;
  2936. for (j = 0; j + NB_ELTS_V8 <= job->nb_columns; j += NB_ELTS_V8) {
  2937. OPJ_UINT32 k;
  2938. opj_v8dwt_interleave_v(&job->v, aj, job->w, NB_ELTS_V8);
  2939. opj_v8dwt_decode(&job->v);
  2940. for (k = 0; k < job->rh; ++k) {
  2941. memcpy(&aj[k * (OPJ_SIZE_T)job->w], &job->v.wavelet[k],
  2942. NB_ELTS_V8 * sizeof(OPJ_FLOAT32));
  2943. }
  2944. aj += NB_ELTS_V8;
  2945. }
  2946. opj_aligned_free(job->v.wavelet);
  2947. opj_free(job);
  2948. }
  2949. /* <summary> */
  2950. /* Inverse 9-7 wavelet transform in 2-D. */
  2951. /* </summary> */
  2952. static
  2953. OPJ_BOOL opj_dwt_decode_tile_97(opj_thread_pool_t* tp,
  2954. opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
  2955. OPJ_UINT32 numres)
  2956. {
  2957. opj_v8dwt_t h;
  2958. opj_v8dwt_t v;
  2959. opj_tcd_resolution_t* res = tilec->resolutions;
  2960. OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 -
  2961. res->x0); /* width of the resolution level computed */
  2962. OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 -
  2963. res->y0); /* height of the resolution level computed */
  2964. OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions -
  2965. 1].x1 -
  2966. tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
  2967. OPJ_SIZE_T l_data_size;
  2968. const int num_threads = opj_thread_pool_get_thread_count(tp);
  2969. if (numres == 1) {
  2970. return OPJ_TRUE;
  2971. }
  2972. l_data_size = opj_dwt_max_resolution(res, numres);
  2973. /* overflow check */
  2974. if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) {
  2975. /* FIXME event manager error callback */
  2976. return OPJ_FALSE;
  2977. }
  2978. h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
  2979. if (!h.wavelet) {
  2980. /* FIXME event manager error callback */
  2981. return OPJ_FALSE;
  2982. }
  2983. v.wavelet = h.wavelet;
  2984. while (--numres) {
  2985. OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data;
  2986. OPJ_UINT32 j;
  2987. h.sn = (OPJ_INT32)rw;
  2988. v.sn = (OPJ_INT32)rh;
  2989. ++res;
  2990. rw = (OPJ_UINT32)(res->x1 -
  2991. res->x0); /* width of the resolution level computed */
  2992. rh = (OPJ_UINT32)(res->y1 -
  2993. res->y0); /* height of the resolution level computed */
  2994. h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
  2995. h.cas = res->x0 % 2;
  2996. h.win_l_x0 = 0;
  2997. h.win_l_x1 = (OPJ_UINT32)h.sn;
  2998. h.win_h_x0 = 0;
  2999. h.win_h_x1 = (OPJ_UINT32)h.dn;
  3000. if (num_threads <= 1 || rh < 2 * NB_ELTS_V8) {
  3001. for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) {
  3002. OPJ_UINT32 k;
  3003. opj_v8dwt_interleave_h(&h, aj, w, NB_ELTS_V8);
  3004. opj_v8dwt_decode(&h);
  3005. /* To be adapted if NB_ELTS_V8 changes */
  3006. for (k = 0; k < rw; k++) {
  3007. aj[k ] = h.wavelet[k].f[0];
  3008. aj[k + (OPJ_SIZE_T)w ] = h.wavelet[k].f[1];
  3009. aj[k + (OPJ_SIZE_T)w * 2] = h.wavelet[k].f[2];
  3010. aj[k + (OPJ_SIZE_T)w * 3] = h.wavelet[k].f[3];
  3011. }
  3012. for (k = 0; k < rw; k++) {
  3013. aj[k + (OPJ_SIZE_T)w * 4] = h.wavelet[k].f[4];
  3014. aj[k + (OPJ_SIZE_T)w * 5] = h.wavelet[k].f[5];
  3015. aj[k + (OPJ_SIZE_T)w * 6] = h.wavelet[k].f[6];
  3016. aj[k + (OPJ_SIZE_T)w * 7] = h.wavelet[k].f[7];
  3017. }
  3018. aj += w * NB_ELTS_V8;
  3019. }
  3020. } else {
  3021. OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads;
  3022. OPJ_UINT32 step_j;
  3023. if ((rh / NB_ELTS_V8) < num_jobs) {
  3024. num_jobs = rh / NB_ELTS_V8;
  3025. }
  3026. step_j = ((rh / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
  3027. for (j = 0; j < num_jobs; j++) {
  3028. opj_dwt97_decode_h_job_t* job;
  3029. job = (opj_dwt97_decode_h_job_t*) opj_malloc(sizeof(opj_dwt97_decode_h_job_t));
  3030. if (!job) {
  3031. opj_thread_pool_wait_completion(tp, 0);
  3032. opj_aligned_free(h.wavelet);
  3033. return OPJ_FALSE;
  3034. }
  3035. job->h.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
  3036. if (!job->h.wavelet) {
  3037. opj_thread_pool_wait_completion(tp, 0);
  3038. opj_free(job);
  3039. opj_aligned_free(h.wavelet);
  3040. return OPJ_FALSE;
  3041. }
  3042. job->h.dn = h.dn;
  3043. job->h.sn = h.sn;
  3044. job->h.cas = h.cas;
  3045. job->h.win_l_x0 = h.win_l_x0;
  3046. job->h.win_l_x1 = h.win_l_x1;
  3047. job->h.win_h_x0 = h.win_h_x0;
  3048. job->h.win_h_x1 = h.win_h_x1;
  3049. job->rw = rw;
  3050. job->w = w;
  3051. job->aj = aj;
  3052. job->nb_rows = (j + 1 == num_jobs) ? (rh & (OPJ_UINT32)~
  3053. (NB_ELTS_V8 - 1)) - j * step_j : step_j;
  3054. aj += w * job->nb_rows;
  3055. opj_thread_pool_submit_job(tp, opj_dwt97_decode_h_func, job);
  3056. }
  3057. opj_thread_pool_wait_completion(tp, 0);
  3058. j = rh & (OPJ_UINT32)~(NB_ELTS_V8 - 1);
  3059. }
  3060. if (j < rh) {
  3061. OPJ_UINT32 k;
  3062. opj_v8dwt_interleave_h(&h, aj, w, rh - j);
  3063. opj_v8dwt_decode(&h);
  3064. for (k = 0; k < rw; k++) {
  3065. OPJ_UINT32 l;
  3066. for (l = 0; l < rh - j; l++) {
  3067. aj[k + (OPJ_SIZE_T)w * l ] = h.wavelet[k].f[l];
  3068. }
  3069. }
  3070. }
  3071. v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
  3072. v.cas = res->y0 % 2;
  3073. v.win_l_x0 = 0;
  3074. v.win_l_x1 = (OPJ_UINT32)v.sn;
  3075. v.win_h_x0 = 0;
  3076. v.win_h_x1 = (OPJ_UINT32)v.dn;
  3077. aj = (OPJ_FLOAT32*) tilec->data;
  3078. if (num_threads <= 1 || rw < 2 * NB_ELTS_V8) {
  3079. for (j = rw; j > (NB_ELTS_V8 - 1); j -= NB_ELTS_V8) {
  3080. OPJ_UINT32 k;
  3081. opj_v8dwt_interleave_v(&v, aj, w, NB_ELTS_V8);
  3082. opj_v8dwt_decode(&v);
  3083. for (k = 0; k < rh; ++k) {
  3084. memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k], NB_ELTS_V8 * sizeof(OPJ_FLOAT32));
  3085. }
  3086. aj += NB_ELTS_V8;
  3087. }
  3088. } else {
  3089. /* "bench_dwt -I" shows that scaling is poor, likely due to RAM
  3090. transfer being the limiting factor. So limit the number of
  3091. threads.
  3092. */
  3093. OPJ_UINT32 num_jobs = opj_uint_max((OPJ_UINT32)num_threads / 2, 2U);
  3094. OPJ_UINT32 step_j;
  3095. if ((rw / NB_ELTS_V8) < num_jobs) {
  3096. num_jobs = rw / NB_ELTS_V8;
  3097. }
  3098. step_j = ((rw / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8;
  3099. for (j = 0; j < num_jobs; j++) {
  3100. opj_dwt97_decode_v_job_t* job;
  3101. job = (opj_dwt97_decode_v_job_t*) opj_malloc(sizeof(opj_dwt97_decode_v_job_t));
  3102. if (!job) {
  3103. opj_thread_pool_wait_completion(tp, 0);
  3104. opj_aligned_free(h.wavelet);
  3105. return OPJ_FALSE;
  3106. }
  3107. job->v.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
  3108. if (!job->v.wavelet) {
  3109. opj_thread_pool_wait_completion(tp, 0);
  3110. opj_free(job);
  3111. opj_aligned_free(h.wavelet);
  3112. return OPJ_FALSE;
  3113. }
  3114. job->v.dn = v.dn;
  3115. job->v.sn = v.sn;
  3116. job->v.cas = v.cas;
  3117. job->v.win_l_x0 = v.win_l_x0;
  3118. job->v.win_l_x1 = v.win_l_x1;
  3119. job->v.win_h_x0 = v.win_h_x0;
  3120. job->v.win_h_x1 = v.win_h_x1;
  3121. job->rh = rh;
  3122. job->w = w;
  3123. job->aj = aj;
  3124. job->nb_columns = (j + 1 == num_jobs) ? (rw & (OPJ_UINT32)~
  3125. (NB_ELTS_V8 - 1)) - j * step_j : step_j;
  3126. aj += job->nb_columns;
  3127. opj_thread_pool_submit_job(tp, opj_dwt97_decode_v_func, job);
  3128. }
  3129. opj_thread_pool_wait_completion(tp, 0);
  3130. }
  3131. if (rw & (NB_ELTS_V8 - 1)) {
  3132. OPJ_UINT32 k;
  3133. j = rw & (NB_ELTS_V8 - 1);
  3134. opj_v8dwt_interleave_v(&v, aj, w, j);
  3135. opj_v8dwt_decode(&v);
  3136. for (k = 0; k < rh; ++k) {
  3137. memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k],
  3138. (OPJ_SIZE_T)j * sizeof(OPJ_FLOAT32));
  3139. }
  3140. }
  3141. }
  3142. opj_aligned_free(h.wavelet);
  3143. return OPJ_TRUE;
  3144. }
  3145. static
  3146. OPJ_BOOL opj_dwt_decode_partial_97(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
  3147. OPJ_UINT32 numres)
  3148. {
  3149. opj_sparse_array_int32_t* sa;
  3150. opj_v8dwt_t h;
  3151. opj_v8dwt_t v;
  3152. OPJ_UINT32 resno;
  3153. /* This value matches the maximum left/right extension given in tables */
  3154. /* F.2 and F.3 of the standard. Note: in opj_tcd_is_subband_area_of_interest() */
  3155. /* we currently use 3. */
  3156. const OPJ_UINT32 filter_width = 4U;
  3157. opj_tcd_resolution_t* tr = tilec->resolutions;
  3158. opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]);
  3159. OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 -
  3160. tr->x0); /* width of the resolution level computed */
  3161. OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 -
  3162. tr->y0); /* height of the resolution level computed */
  3163. OPJ_SIZE_T l_data_size;
  3164. /* Compute the intersection of the area of interest, expressed in tile coordinates */
  3165. /* with the tile coordinates */
  3166. OPJ_UINT32 win_tcx0 = tilec->win_x0;
  3167. OPJ_UINT32 win_tcy0 = tilec->win_y0;
  3168. OPJ_UINT32 win_tcx1 = tilec->win_x1;
  3169. OPJ_UINT32 win_tcy1 = tilec->win_y1;
  3170. if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) {
  3171. return OPJ_TRUE;
  3172. }
  3173. sa = opj_dwt_init_sparse_array(tilec, numres);
  3174. if (sa == NULL) {
  3175. return OPJ_FALSE;
  3176. }
  3177. if (numres == 1U) {
  3178. OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
  3179. tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
  3180. tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
  3181. tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
  3182. tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
  3183. tilec->data_win,
  3184. 1, tr_max->win_x1 - tr_max->win_x0,
  3185. OPJ_TRUE);
  3186. assert(ret);
  3187. OPJ_UNUSED(ret);
  3188. opj_sparse_array_int32_free(sa);
  3189. return OPJ_TRUE;
  3190. }
  3191. l_data_size = opj_dwt_max_resolution(tr, numres);
  3192. /* overflow check */
  3193. if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) {
  3194. /* FIXME event manager error callback */
  3195. opj_sparse_array_int32_free(sa);
  3196. return OPJ_FALSE;
  3197. }
  3198. h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t));
  3199. if (!h.wavelet) {
  3200. /* FIXME event manager error callback */
  3201. opj_sparse_array_int32_free(sa);
  3202. return OPJ_FALSE;
  3203. }
  3204. v.wavelet = h.wavelet;
  3205. for (resno = 1; resno < numres; resno ++) {
  3206. OPJ_UINT32 j;
  3207. /* Window of interest subband-based coordinates */
  3208. OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1;
  3209. OPJ_UINT32 win_hl_x0, win_hl_x1;
  3210. OPJ_UINT32 win_lh_y0, win_lh_y1;
  3211. /* Window of interest tile-resolution-based coordinates */
  3212. OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1;
  3213. /* Tile-resolution subband-based coordinates */
  3214. OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0;
  3215. ++tr;
  3216. h.sn = (OPJ_INT32)rw;
  3217. v.sn = (OPJ_INT32)rh;
  3218. rw = (OPJ_UINT32)(tr->x1 - tr->x0);
  3219. rh = (OPJ_UINT32)(tr->y1 - tr->y0);
  3220. h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
  3221. h.cas = tr->x0 % 2;
  3222. v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
  3223. v.cas = tr->y0 % 2;
  3224. /* Get the subband coordinates for the window of interest */
  3225. /* LL band */
  3226. opj_dwt_get_band_coordinates(tilec, resno, 0,
  3227. win_tcx0, win_tcy0, win_tcx1, win_tcy1,
  3228. &win_ll_x0, &win_ll_y0,
  3229. &win_ll_x1, &win_ll_y1);
  3230. /* HL band */
  3231. opj_dwt_get_band_coordinates(tilec, resno, 1,
  3232. win_tcx0, win_tcy0, win_tcx1, win_tcy1,
  3233. &win_hl_x0, NULL, &win_hl_x1, NULL);
  3234. /* LH band */
  3235. opj_dwt_get_band_coordinates(tilec, resno, 2,
  3236. win_tcx0, win_tcy0, win_tcx1, win_tcy1,
  3237. NULL, &win_lh_y0, NULL, &win_lh_y1);
  3238. /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */
  3239. tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0;
  3240. tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0;
  3241. tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0;
  3242. tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0;
  3243. /* Subtract the origin of the bands for this tile, to the subwindow */
  3244. /* of interest band coordinates, so as to get them relative to the */
  3245. /* tile */
  3246. win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0);
  3247. win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0);
  3248. win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0);
  3249. win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0);
  3250. win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0);
  3251. win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0);
  3252. win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0);
  3253. win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0);
  3254. opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1);
  3255. opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1);
  3256. opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1);
  3257. opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1);
  3258. /* Compute the tile-resolution-based coordinates for the window of interest */
  3259. if (h.cas == 0) {
  3260. win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1);
  3261. win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw);
  3262. } else {
  3263. win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1);
  3264. win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw);
  3265. }
  3266. if (v.cas == 0) {
  3267. win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1);
  3268. win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh);
  3269. } else {
  3270. win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1);
  3271. win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh);
  3272. }
  3273. h.win_l_x0 = win_ll_x0;
  3274. h.win_l_x1 = win_ll_x1;
  3275. h.win_h_x0 = win_hl_x0;
  3276. h.win_h_x1 = win_hl_x1;
  3277. for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) {
  3278. if ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) ||
  3279. (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn &&
  3280. j < win_lh_y1 + (OPJ_UINT32)v.sn)) {
  3281. opj_v8dwt_interleave_partial_h(&h, sa, j, opj_uint_min(NB_ELTS_V8, rh - j));
  3282. opj_v8dwt_decode(&h);
  3283. if (!opj_sparse_array_int32_write(sa,
  3284. win_tr_x0, j,
  3285. win_tr_x1, j + NB_ELTS_V8,
  3286. (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0],
  3287. NB_ELTS_V8, 1, OPJ_TRUE)) {
  3288. /* FIXME event manager error callback */
  3289. opj_sparse_array_int32_free(sa);
  3290. opj_aligned_free(h.wavelet);
  3291. return OPJ_FALSE;
  3292. }
  3293. }
  3294. }
  3295. if (j < rh &&
  3296. ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) ||
  3297. (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn &&
  3298. j < win_lh_y1 + (OPJ_UINT32)v.sn))) {
  3299. opj_v8dwt_interleave_partial_h(&h, sa, j, rh - j);
  3300. opj_v8dwt_decode(&h);
  3301. if (!opj_sparse_array_int32_write(sa,
  3302. win_tr_x0, j,
  3303. win_tr_x1, rh,
  3304. (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0],
  3305. NB_ELTS_V8, 1, OPJ_TRUE)) {
  3306. /* FIXME event manager error callback */
  3307. opj_sparse_array_int32_free(sa);
  3308. opj_aligned_free(h.wavelet);
  3309. return OPJ_FALSE;
  3310. }
  3311. }
  3312. v.win_l_x0 = win_ll_y0;
  3313. v.win_l_x1 = win_ll_y1;
  3314. v.win_h_x0 = win_lh_y0;
  3315. v.win_h_x1 = win_lh_y1;
  3316. for (j = win_tr_x0; j < win_tr_x1; j += NB_ELTS_V8) {
  3317. OPJ_UINT32 nb_elts = opj_uint_min(NB_ELTS_V8, win_tr_x1 - j);
  3318. opj_v8dwt_interleave_partial_v(&v, sa, j, nb_elts);
  3319. opj_v8dwt_decode(&v);
  3320. if (!opj_sparse_array_int32_write(sa,
  3321. j, win_tr_y0,
  3322. j + nb_elts, win_tr_y1,
  3323. (OPJ_INT32*)&h.wavelet[win_tr_y0].f[0],
  3324. 1, NB_ELTS_V8, OPJ_TRUE)) {
  3325. /* FIXME event manager error callback */
  3326. opj_sparse_array_int32_free(sa);
  3327. opj_aligned_free(h.wavelet);
  3328. return OPJ_FALSE;
  3329. }
  3330. }
  3331. }
  3332. {
  3333. OPJ_BOOL ret = opj_sparse_array_int32_read(sa,
  3334. tr_max->win_x0 - (OPJ_UINT32)tr_max->x0,
  3335. tr_max->win_y0 - (OPJ_UINT32)tr_max->y0,
  3336. tr_max->win_x1 - (OPJ_UINT32)tr_max->x0,
  3337. tr_max->win_y1 - (OPJ_UINT32)tr_max->y0,
  3338. tilec->data_win,
  3339. 1, tr_max->win_x1 - tr_max->win_x0,
  3340. OPJ_TRUE);
  3341. assert(ret);
  3342. OPJ_UNUSED(ret);
  3343. }
  3344. opj_sparse_array_int32_free(sa);
  3345. opj_aligned_free(h.wavelet);
  3346. return OPJ_TRUE;
  3347. }
  3348. OPJ_BOOL opj_dwt_decode_real(opj_tcd_t *p_tcd,
  3349. opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
  3350. OPJ_UINT32 numres)
  3351. {
  3352. if (p_tcd->whole_tile_decoding) {
  3353. return opj_dwt_decode_tile_97(p_tcd->thread_pool, tilec, numres);
  3354. } else {
  3355. return opj_dwt_decode_partial_97(tilec, numres);
  3356. }
  3357. }