dwt.c 135 KB

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