dwt.c 134 KB

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