dwt.c 142 KB

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