t1.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
  8. * Copyright (c) 2002-2014, Professor Benoit Macq
  9. * Copyright (c) 2001-2003, David Janssens
  10. * Copyright (c) 2002-2003, Yannick Verschueren
  11. * Copyright (c) 2003-2007, Francois-Olivier Devaux
  12. * Copyright (c) 2003-2014, Antonin Descampe
  13. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  14. * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
  15. * Copyright (c) 2012, Carl Hetherington
  16. * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. */
  40. #define OPJ_SKIP_POISON
  41. #include "opj_includes.h"
  42. #ifdef __SSE__
  43. #include <xmmintrin.h>
  44. #endif
  45. #ifdef __SSE2__
  46. #include <emmintrin.h>
  47. #endif
  48. #if defined(__GNUC__)
  49. #pragma GCC poison malloc calloc realloc free
  50. #endif
  51. #include "t1_luts.h"
  52. /** @defgroup T1 T1 - Implementation of the tier-1 coding */
  53. /*@{*/
  54. #define T1_FLAGS(x, y) (t1->flags[x + 1 + ((y / 4) + 1) * (t1->w+2)])
  55. #define opj_t1_setcurctx(curctx, ctxno) curctx = &(mqc)->ctxs[(OPJ_UINT32)(ctxno)]
  56. /* Macros to deal with signed integer with just MSB bit set for
  57. * negative values (smr = signed magnitude representation) */
  58. #define opj_smr_abs(x) (((OPJ_UINT32)(x)) & 0x7FFFFFFFU)
  59. #define opj_smr_sign(x) (((OPJ_UINT32)(x)) >> 31)
  60. #define opj_to_smr(x) ((x) >= 0 ? (OPJ_UINT32)(x) : ((OPJ_UINT32)(-x) | 0x80000000U))
  61. /** @name Local static functions */
  62. /*@{*/
  63. static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f);
  64. static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
  65. static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
  66. static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
  67. static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
  68. OPJ_UINT32 s, OPJ_UINT32 stride,
  69. OPJ_UINT32 vsc);
  70. /**
  71. Decode significant pass
  72. */
  73. static INLINE void opj_t1_dec_sigpass_step_raw(
  74. opj_t1_t *t1,
  75. opj_flag_t *flagsp,
  76. OPJ_INT32 *datap,
  77. OPJ_INT32 oneplushalf,
  78. OPJ_UINT32 vsc,
  79. OPJ_UINT32 row);
  80. static INLINE void opj_t1_dec_sigpass_step_mqc(
  81. opj_t1_t *t1,
  82. opj_flag_t *flagsp,
  83. OPJ_INT32 *datap,
  84. OPJ_INT32 oneplushalf,
  85. OPJ_UINT32 row,
  86. OPJ_UINT32 flags_stride,
  87. OPJ_UINT32 vsc);
  88. /**
  89. Encode significant pass
  90. */
  91. static void opj_t1_enc_sigpass(opj_t1_t *t1,
  92. OPJ_INT32 bpno,
  93. OPJ_INT32 *nmsedec,
  94. OPJ_BYTE type,
  95. OPJ_UINT32 cblksty);
  96. /**
  97. Decode significant pass
  98. */
  99. static void opj_t1_dec_sigpass_raw(
  100. opj_t1_t *t1,
  101. OPJ_INT32 bpno,
  102. OPJ_INT32 cblksty);
  103. /**
  104. Encode refinement pass
  105. */
  106. static void opj_t1_enc_refpass(opj_t1_t *t1,
  107. OPJ_INT32 bpno,
  108. OPJ_INT32 *nmsedec,
  109. OPJ_BYTE type);
  110. /**
  111. Decode refinement pass
  112. */
  113. static void opj_t1_dec_refpass_raw(
  114. opj_t1_t *t1,
  115. OPJ_INT32 bpno);
  116. /**
  117. Decode refinement pass
  118. */
  119. static INLINE void opj_t1_dec_refpass_step_raw(
  120. opj_t1_t *t1,
  121. opj_flag_t *flagsp,
  122. OPJ_INT32 *datap,
  123. OPJ_INT32 poshalf,
  124. OPJ_UINT32 row);
  125. static INLINE void opj_t1_dec_refpass_step_mqc(
  126. opj_t1_t *t1,
  127. opj_flag_t *flagsp,
  128. OPJ_INT32 *datap,
  129. OPJ_INT32 poshalf,
  130. OPJ_UINT32 row);
  131. /**
  132. Decode clean-up pass
  133. */
  134. static void opj_t1_dec_clnpass_step(
  135. opj_t1_t *t1,
  136. opj_flag_t *flagsp,
  137. OPJ_INT32 *datap,
  138. OPJ_INT32 oneplushalf,
  139. OPJ_UINT32 row,
  140. OPJ_UINT32 vsc);
  141. /**
  142. Encode clean-up pass
  143. */
  144. static void opj_t1_enc_clnpass(
  145. opj_t1_t *t1,
  146. OPJ_INT32 bpno,
  147. OPJ_INT32 *nmsedec,
  148. OPJ_UINT32 cblksty);
  149. static OPJ_FLOAT64 opj_t1_getwmsedec(
  150. OPJ_INT32 nmsedec,
  151. OPJ_UINT32 compno,
  152. OPJ_UINT32 level,
  153. OPJ_UINT32 orient,
  154. OPJ_INT32 bpno,
  155. OPJ_UINT32 qmfbid,
  156. OPJ_FLOAT64 stepsize,
  157. OPJ_UINT32 numcomps,
  158. const OPJ_FLOAT64 * mct_norms,
  159. OPJ_UINT32 mct_numcomps);
  160. /** Return "cumwmsedec" that should be used to increase tile->distotile */
  161. static double opj_t1_encode_cblk(opj_t1_t *t1,
  162. opj_tcd_cblk_enc_t* cblk,
  163. OPJ_UINT32 orient,
  164. OPJ_UINT32 compno,
  165. OPJ_UINT32 level,
  166. OPJ_UINT32 qmfbid,
  167. OPJ_FLOAT64 stepsize,
  168. OPJ_UINT32 cblksty,
  169. OPJ_UINT32 numcomps,
  170. const OPJ_FLOAT64 * mct_norms,
  171. OPJ_UINT32 mct_numcomps);
  172. /**
  173. Decode 1 code-block
  174. @param t1 T1 handle
  175. @param cblk Code-block coding parameters
  176. @param orient
  177. @param roishift Region of interest shifting value
  178. @param cblksty Code-block style
  179. @param p_manager the event manager
  180. @param p_manager_mutex mutex for the event manager
  181. @param check_pterm whether PTERM correct termination should be checked
  182. */
  183. static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
  184. opj_tcd_cblk_dec_t* cblk,
  185. OPJ_UINT32 orient,
  186. OPJ_UINT32 roishift,
  187. OPJ_UINT32 cblksty,
  188. opj_event_mgr_t *p_manager,
  189. opj_mutex_t* p_manager_mutex,
  190. OPJ_BOOL check_pterm);
  191. static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1,
  192. OPJ_UINT32 w,
  193. OPJ_UINT32 h);
  194. /*@}*/
  195. /*@}*/
  196. /* ----------------------------------------------------------------------- */
  197. static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f)
  198. {
  199. return mqc->lut_ctxno_zc_orient[(f & T1_SIGMA_NEIGHBOURS)];
  200. }
  201. static INLINE OPJ_UINT32 opj_t1_getctxtno_sc_or_spb_index(OPJ_UINT32 fX,
  202. OPJ_UINT32 pfX,
  203. OPJ_UINT32 nfX,
  204. OPJ_UINT32 ci)
  205. {
  206. /*
  207. 0 pfX T1_CHI_THIS T1_LUT_SGN_W
  208. 1 tfX T1_SIGMA_1 T1_LUT_SIG_N
  209. 2 nfX T1_CHI_THIS T1_LUT_SGN_E
  210. 3 tfX T1_SIGMA_3 T1_LUT_SIG_W
  211. 4 fX T1_CHI_(THIS - 1) T1_LUT_SGN_N
  212. 5 tfX T1_SIGMA_5 T1_LUT_SIG_E
  213. 6 fX T1_CHI_(THIS + 1) T1_LUT_SGN_S
  214. 7 tfX T1_SIGMA_7 T1_LUT_SIG_S
  215. */
  216. OPJ_UINT32 lu = (fX >> (ci * 3U)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 |
  217. T1_SIGMA_7);
  218. lu |= (pfX >> (T1_CHI_THIS_I + (ci * 3U))) & (1U << 0);
  219. lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2);
  220. if (ci == 0U) {
  221. lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
  222. } else {
  223. lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4);
  224. }
  225. lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6);
  226. return lu;
  227. }
  228. static INLINE OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 lu)
  229. {
  230. return lut_ctxno_sc[lu];
  231. }
  232. static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f)
  233. {
  234. OPJ_UINT32 tmp = (f & T1_SIGMA_NEIGHBOURS) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
  235. OPJ_UINT32 tmp2 = (f & T1_MU_0) ? T1_CTXNO_MAG + 2 : tmp;
  236. return tmp2;
  237. }
  238. static INLINE OPJ_BYTE opj_t1_getspb(OPJ_UINT32 lu)
  239. {
  240. return lut_spb[lu];
  241. }
  242. static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
  243. {
  244. if (bitpos > 0) {
  245. return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
  246. }
  247. return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
  248. }
  249. static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos)
  250. {
  251. if (bitpos > 0) {
  252. return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
  253. }
  254. return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
  255. }
  256. #define opj_t1_update_flags_macro(flags, flagsp, ci, s, stride, vsc) \
  257. { \
  258. /* east */ \
  259. flagsp[-1] |= T1_SIGMA_5 << (3U * ci); \
  260. \
  261. /* mark target as significant */ \
  262. flags |= ((s << T1_CHI_1_I) | T1_SIGMA_4) << (3U * ci); \
  263. \
  264. /* west */ \
  265. flagsp[1] |= T1_SIGMA_3 << (3U * ci); \
  266. \
  267. /* north-west, north, north-east */ \
  268. if (ci == 0U && !(vsc)) { \
  269. opj_flag_t* north = flagsp - (stride); \
  270. *north |= (s << T1_CHI_5_I) | T1_SIGMA_16; \
  271. north[-1] |= T1_SIGMA_17; \
  272. north[1] |= T1_SIGMA_15; \
  273. } \
  274. \
  275. /* south-west, south, south-east */ \
  276. if (ci == 3U) { \
  277. opj_flag_t* south = flagsp + (stride); \
  278. *south |= (s << T1_CHI_0_I) | T1_SIGMA_1; \
  279. south[-1] |= T1_SIGMA_2; \
  280. south[1] |= T1_SIGMA_0; \
  281. } \
  282. }
  283. static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
  284. OPJ_UINT32 s, OPJ_UINT32 stride,
  285. OPJ_UINT32 vsc)
  286. {
  287. opj_t1_update_flags_macro(*flagsp, flagsp, ci, s, stride, vsc);
  288. }
  289. /**
  290. Encode significant pass
  291. */
  292. #define opj_t1_enc_sigpass_step_macro(mqc, curctx, a, c, ct, flagspIn, datapIn, bpno, one, nmsedec, type, ciIn, vscIn) \
  293. { \
  294. OPJ_UINT32 v; \
  295. const OPJ_UINT32 ci = (ciIn); \
  296. const OPJ_UINT32 vsc = (vscIn); \
  297. const OPJ_INT32* l_datap = (datapIn); \
  298. opj_flag_t* flagsp = (flagspIn); \
  299. OPJ_UINT32 const flags = *flagsp; \
  300. if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U && \
  301. (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) { \
  302. OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
  303. v = (opj_smr_abs(*l_datap) & (OPJ_UINT32)one) ? 1 : 0; \
  304. /* #ifdef DEBUG_ENC_SIG */ \
  305. /* fprintf(stderr, " ctxt1=%d\n", ctxt1); */ \
  306. /* #endif */ \
  307. opj_t1_setcurctx(curctx, ctxt1); \
  308. if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ \
  309. opj_mqc_bypass_enc_macro(mqc, c, ct, v); \
  310. } else { \
  311. opj_mqc_encode_macro(mqc, curctx, a, c, ct, v); \
  312. } \
  313. if (v) { \
  314. OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
  315. *flagsp, \
  316. flagsp[-1], flagsp[1], \
  317. ci); \
  318. OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu); \
  319. v = opj_smr_sign(*l_datap); \
  320. *nmsedec += opj_t1_getnmsedec_sig(opj_smr_abs(*l_datap), \
  321. (OPJ_UINT32)bpno); \
  322. /* #ifdef DEBUG_ENC_SIG */ \
  323. /* fprintf(stderr, " ctxt2=%d\n", ctxt2); */ \
  324. /* #endif */ \
  325. opj_t1_setcurctx(curctx, ctxt2); \
  326. if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ \
  327. opj_mqc_bypass_enc_macro(mqc, c, ct, v); \
  328. } else { \
  329. OPJ_UINT32 spb = opj_t1_getspb(lu); \
  330. /* #ifdef DEBUG_ENC_SIG */ \
  331. /* fprintf(stderr, " spb=%d\n", spb); */ \
  332. /* #endif */ \
  333. opj_mqc_encode_macro(mqc, curctx, a, c, ct, v ^ spb); \
  334. } \
  335. opj_t1_update_flags(flagsp, ci, v, t1->w + 2, vsc); \
  336. } \
  337. *flagsp |= T1_PI_THIS << (ci * 3U); \
  338. } \
  339. }
  340. static INLINE void opj_t1_dec_sigpass_step_raw(
  341. opj_t1_t *t1,
  342. opj_flag_t *flagsp,
  343. OPJ_INT32 *datap,
  344. OPJ_INT32 oneplushalf,
  345. OPJ_UINT32 vsc,
  346. OPJ_UINT32 ci)
  347. {
  348. OPJ_UINT32 v;
  349. opj_mqc_t *mqc = &(t1->mqc); /* RAW component */
  350. OPJ_UINT32 const flags = *flagsp;
  351. if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
  352. (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
  353. if (opj_mqc_raw_decode(mqc)) {
  354. v = opj_mqc_raw_decode(mqc);
  355. *datap = v ? -oneplushalf : oneplushalf;
  356. opj_t1_update_flags(flagsp, ci, v, t1->w + 2, vsc);
  357. }
  358. *flagsp |= T1_PI_THIS << (ci * 3U);
  359. }
  360. }
  361. #define opj_t1_dec_sigpass_step_mqc_macro(flags, flagsp, flags_stride, data, \
  362. data_stride, ci, mqc, curctx, \
  363. v, a, c, ct, oneplushalf, vsc) \
  364. { \
  365. if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U && \
  366. (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) { \
  367. OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
  368. opj_t1_setcurctx(curctx, ctxt1); \
  369. opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
  370. if (v) { \
  371. OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
  372. flags, \
  373. flagsp[-1], flagsp[1], \
  374. ci); \
  375. OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu); \
  376. OPJ_UINT32 spb = opj_t1_getspb(lu); \
  377. opj_t1_setcurctx(curctx, ctxt2); \
  378. opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
  379. v = v ^ spb; \
  380. data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \
  381. opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride, vsc); \
  382. } \
  383. flags |= T1_PI_THIS << (ci * 3U); \
  384. } \
  385. }
  386. static INLINE void opj_t1_dec_sigpass_step_mqc(
  387. opj_t1_t *t1,
  388. opj_flag_t *flagsp,
  389. OPJ_INT32 *datap,
  390. OPJ_INT32 oneplushalf,
  391. OPJ_UINT32 ci,
  392. OPJ_UINT32 flags_stride,
  393. OPJ_UINT32 vsc)
  394. {
  395. OPJ_UINT32 v;
  396. opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
  397. opj_t1_dec_sigpass_step_mqc_macro(*flagsp, flagsp, flags_stride, datap,
  398. 0, ci, mqc, mqc->curctx,
  399. v, mqc->a, mqc->c, mqc->ct, oneplushalf, vsc);
  400. }
  401. static void opj_t1_enc_sigpass(opj_t1_t *t1,
  402. OPJ_INT32 bpno,
  403. OPJ_INT32 *nmsedec,
  404. OPJ_BYTE type,
  405. OPJ_UINT32 cblksty
  406. )
  407. {
  408. OPJ_UINT32 i, k;
  409. OPJ_INT32 const one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
  410. opj_flag_t* f = &T1_FLAGS(0, 0);
  411. OPJ_UINT32 const extra = 2;
  412. opj_mqc_t* mqc = &(t1->mqc);
  413. DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct);
  414. const OPJ_INT32* datap = t1->data;
  415. *nmsedec = 0;
  416. #ifdef DEBUG_ENC_SIG
  417. fprintf(stderr, "enc_sigpass: bpno=%d\n", bpno);
  418. #endif
  419. for (k = 0; k < (t1->h & ~3U); k += 4, f += extra) {
  420. const OPJ_UINT32 w = t1->w;
  421. #ifdef DEBUG_ENC_SIG
  422. fprintf(stderr, " k=%d\n", k);
  423. #endif
  424. for (i = 0; i < w; ++i, ++f, datap += 4) {
  425. #ifdef DEBUG_ENC_SIG
  426. fprintf(stderr, " i=%d\n", i);
  427. #endif
  428. if (*f == 0U) {
  429. /* Nothing to do for any of the 4 data points */
  430. continue;
  431. }
  432. opj_t1_enc_sigpass_step_macro(
  433. mqc, curctx, a, c, ct,
  434. f,
  435. &datap[0],
  436. bpno,
  437. one,
  438. nmsedec,
  439. type,
  440. 0, cblksty & J2K_CCP_CBLKSTY_VSC);
  441. opj_t1_enc_sigpass_step_macro(
  442. mqc, curctx, a, c, ct,
  443. f,
  444. &datap[1],
  445. bpno,
  446. one,
  447. nmsedec,
  448. type,
  449. 1, 0);
  450. opj_t1_enc_sigpass_step_macro(
  451. mqc, curctx, a, c, ct,
  452. f,
  453. &datap[2],
  454. bpno,
  455. one,
  456. nmsedec,
  457. type,
  458. 2, 0);
  459. opj_t1_enc_sigpass_step_macro(
  460. mqc, curctx, a, c, ct,
  461. f,
  462. &datap[3],
  463. bpno,
  464. one,
  465. nmsedec,
  466. type,
  467. 3, 0);
  468. }
  469. }
  470. if (k < t1->h) {
  471. OPJ_UINT32 j;
  472. #ifdef DEBUG_ENC_SIG
  473. fprintf(stderr, " k=%d\n", k);
  474. #endif
  475. for (i = 0; i < t1->w; ++i, ++f) {
  476. #ifdef DEBUG_ENC_SIG
  477. fprintf(stderr, " i=%d\n", i);
  478. #endif
  479. if (*f == 0U) {
  480. /* Nothing to do for any of the 4 data points */
  481. datap += (t1->h - k);
  482. continue;
  483. }
  484. for (j = k; j < t1->h; ++j, ++datap) {
  485. opj_t1_enc_sigpass_step_macro(
  486. mqc, curctx, a, c, ct,
  487. f,
  488. &datap[0],
  489. bpno,
  490. one,
  491. nmsedec,
  492. type,
  493. j - k,
  494. (j == k && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
  495. }
  496. }
  497. }
  498. UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct);
  499. }
  500. static void opj_t1_dec_sigpass_raw(
  501. opj_t1_t *t1,
  502. OPJ_INT32 bpno,
  503. OPJ_INT32 cblksty)
  504. {
  505. OPJ_INT32 one, half, oneplushalf;
  506. OPJ_UINT32 i, j, k;
  507. OPJ_INT32 *data = t1->data;
  508. opj_flag_t *flagsp = &T1_FLAGS(0, 0);
  509. const OPJ_UINT32 l_w = t1->w;
  510. one = 1 << bpno;
  511. half = one >> 1;
  512. oneplushalf = one | half;
  513. for (k = 0; k < (t1->h & ~3U); k += 4, flagsp += 2, data += 3 * l_w) {
  514. for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
  515. opj_flag_t flags = *flagsp;
  516. if (flags != 0) {
  517. opj_t1_dec_sigpass_step_raw(
  518. t1,
  519. flagsp,
  520. data,
  521. oneplushalf,
  522. cblksty & J2K_CCP_CBLKSTY_VSC, /* vsc */
  523. 0U);
  524. opj_t1_dec_sigpass_step_raw(
  525. t1,
  526. flagsp,
  527. data + l_w,
  528. oneplushalf,
  529. OPJ_FALSE, /* vsc */
  530. 1U);
  531. opj_t1_dec_sigpass_step_raw(
  532. t1,
  533. flagsp,
  534. data + 2 * l_w,
  535. oneplushalf,
  536. OPJ_FALSE, /* vsc */
  537. 2U);
  538. opj_t1_dec_sigpass_step_raw(
  539. t1,
  540. flagsp,
  541. data + 3 * l_w,
  542. oneplushalf,
  543. OPJ_FALSE, /* vsc */
  544. 3U);
  545. }
  546. }
  547. }
  548. if (k < t1->h) {
  549. for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
  550. for (j = 0; j < t1->h - k; ++j) {
  551. opj_t1_dec_sigpass_step_raw(
  552. t1,
  553. flagsp,
  554. data + j * l_w,
  555. oneplushalf,
  556. cblksty & J2K_CCP_CBLKSTY_VSC, /* vsc */
  557. j);
  558. }
  559. }
  560. }
  561. }
  562. #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, vsc, w, h, flags_stride) \
  563. { \
  564. OPJ_INT32 one, half, oneplushalf; \
  565. OPJ_UINT32 i, j, k; \
  566. register OPJ_INT32 *data = t1->data; \
  567. register opj_flag_t *flagsp = &t1->flags[(flags_stride) + 1]; \
  568. const OPJ_UINT32 l_w = w; \
  569. opj_mqc_t* mqc = &(t1->mqc); \
  570. DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \
  571. register OPJ_UINT32 v; \
  572. one = 1 << bpno; \
  573. half = one >> 1; \
  574. oneplushalf = one | half; \
  575. for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
  576. for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
  577. opj_flag_t flags = *flagsp; \
  578. if( flags != 0 ) { \
  579. opj_t1_dec_sigpass_step_mqc_macro( \
  580. flags, flagsp, flags_stride, data, \
  581. l_w, 0, mqc, curctx, v, a, c, ct, oneplushalf, vsc); \
  582. opj_t1_dec_sigpass_step_mqc_macro( \
  583. flags, flagsp, flags_stride, data, \
  584. l_w, 1, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \
  585. opj_t1_dec_sigpass_step_mqc_macro( \
  586. flags, flagsp, flags_stride, data, \
  587. l_w, 2, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \
  588. opj_t1_dec_sigpass_step_mqc_macro( \
  589. flags, flagsp, flags_stride, data, \
  590. l_w, 3, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \
  591. *flagsp = flags; \
  592. } \
  593. } \
  594. } \
  595. UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \
  596. if( k < h ) { \
  597. for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
  598. for (j = 0; j < h - k; ++j) { \
  599. opj_t1_dec_sigpass_step_mqc(t1, flagsp, \
  600. data + j * l_w, oneplushalf, j, flags_stride, vsc); \
  601. } \
  602. } \
  603. } \
  604. }
  605. static void opj_t1_dec_sigpass_mqc_64x64_novsc(
  606. opj_t1_t *t1,
  607. OPJ_INT32 bpno)
  608. {
  609. opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_FALSE, 64, 64, 66);
  610. }
  611. static void opj_t1_dec_sigpass_mqc_64x64_vsc(
  612. opj_t1_t *t1,
  613. OPJ_INT32 bpno)
  614. {
  615. opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_TRUE, 64, 64, 66);
  616. }
  617. static void opj_t1_dec_sigpass_mqc_generic_novsc(
  618. opj_t1_t *t1,
  619. OPJ_INT32 bpno)
  620. {
  621. opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_FALSE, t1->w, t1->h,
  622. t1->w + 2U);
  623. }
  624. static void opj_t1_dec_sigpass_mqc_generic_vsc(
  625. opj_t1_t *t1,
  626. OPJ_INT32 bpno)
  627. {
  628. opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_TRUE, t1->w, t1->h,
  629. t1->w + 2U);
  630. }
  631. static void opj_t1_dec_sigpass_mqc(
  632. opj_t1_t *t1,
  633. OPJ_INT32 bpno,
  634. OPJ_INT32 cblksty)
  635. {
  636. if (t1->w == 64 && t1->h == 64) {
  637. if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  638. opj_t1_dec_sigpass_mqc_64x64_vsc(t1, bpno);
  639. } else {
  640. opj_t1_dec_sigpass_mqc_64x64_novsc(t1, bpno);
  641. }
  642. } else {
  643. if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  644. opj_t1_dec_sigpass_mqc_generic_vsc(t1, bpno);
  645. } else {
  646. opj_t1_dec_sigpass_mqc_generic_novsc(t1, bpno);
  647. }
  648. }
  649. }
  650. /**
  651. Encode refinement pass step
  652. */
  653. #define opj_t1_enc_refpass_step_macro(mqc, curctx, a, c, ct, flags, flagsUpdated, datap, bpno, one, nmsedec, type, ci) \
  654. {\
  655. OPJ_UINT32 v; \
  656. if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << ((ci) * 3U))) == (T1_SIGMA_THIS << ((ci) * 3U))) { \
  657. const OPJ_UINT32 shift_flags = (flags >> ((ci) * 3U)); \
  658. OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags); \
  659. OPJ_UINT32 abs_data = opj_smr_abs(*datap); \
  660. *nmsedec += opj_t1_getnmsedec_ref(abs_data, \
  661. (OPJ_UINT32)bpno); \
  662. v = ((OPJ_INT32)abs_data & one) ? 1 : 0; \
  663. /* #ifdef DEBUG_ENC_REF */ \
  664. /* fprintf(stderr, " ctxt=%d\n", ctxt); */ \
  665. /* #endif */ \
  666. opj_t1_setcurctx(curctx, ctxt); \
  667. if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ \
  668. opj_mqc_bypass_enc_macro(mqc, c, ct, v); \
  669. } else { \
  670. opj_mqc_encode_macro(mqc, curctx, a, c, ct, v); \
  671. } \
  672. flagsUpdated |= T1_MU_THIS << ((ci) * 3U); \
  673. } \
  674. }
  675. static INLINE void opj_t1_dec_refpass_step_raw(
  676. opj_t1_t *t1,
  677. opj_flag_t *flagsp,
  678. OPJ_INT32 *datap,
  679. OPJ_INT32 poshalf,
  680. OPJ_UINT32 ci)
  681. {
  682. OPJ_UINT32 v;
  683. opj_mqc_t *mqc = &(t1->mqc); /* RAW component */
  684. if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
  685. (T1_SIGMA_THIS << (ci * 3U))) {
  686. v = opj_mqc_raw_decode(mqc);
  687. *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf;
  688. *flagsp |= T1_MU_THIS << (ci * 3U);
  689. }
  690. }
  691. #define opj_t1_dec_refpass_step_mqc_macro(flags, data, data_stride, ci, \
  692. mqc, curctx, v, a, c, ct, poshalf) \
  693. { \
  694. if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == \
  695. (T1_SIGMA_THIS << (ci * 3U))) { \
  696. OPJ_UINT32 ctxt = opj_t1_getctxno_mag(flags >> (ci * 3U)); \
  697. opj_t1_setcurctx(curctx, ctxt); \
  698. opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
  699. data[ci*data_stride] += (v ^ (data[ci*data_stride] < 0)) ? poshalf : -poshalf; \
  700. flags |= T1_MU_THIS << (ci * 3U); \
  701. } \
  702. }
  703. static INLINE void opj_t1_dec_refpass_step_mqc(
  704. opj_t1_t *t1,
  705. opj_flag_t *flagsp,
  706. OPJ_INT32 *datap,
  707. OPJ_INT32 poshalf,
  708. OPJ_UINT32 ci)
  709. {
  710. OPJ_UINT32 v;
  711. opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
  712. opj_t1_dec_refpass_step_mqc_macro(*flagsp, datap, 0, ci,
  713. mqc, mqc->curctx, v, mqc->a, mqc->c,
  714. mqc->ct, poshalf);
  715. }
  716. static void opj_t1_enc_refpass(
  717. opj_t1_t *t1,
  718. OPJ_INT32 bpno,
  719. OPJ_INT32 *nmsedec,
  720. OPJ_BYTE type)
  721. {
  722. OPJ_UINT32 i, k;
  723. const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
  724. opj_flag_t* f = &T1_FLAGS(0, 0);
  725. const OPJ_UINT32 extra = 2U;
  726. opj_mqc_t* mqc = &(t1->mqc);
  727. DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct);
  728. const OPJ_INT32* datap = t1->data;
  729. *nmsedec = 0;
  730. #ifdef DEBUG_ENC_REF
  731. fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
  732. #endif
  733. for (k = 0; k < (t1->h & ~3U); k += 4, f += extra) {
  734. #ifdef DEBUG_ENC_REF
  735. fprintf(stderr, " k=%d\n", k);
  736. #endif
  737. for (i = 0; i < t1->w; ++i, f++, datap += 4) {
  738. const OPJ_UINT32 flags = *f;
  739. OPJ_UINT32 flagsUpdated = flags;
  740. #ifdef DEBUG_ENC_REF
  741. fprintf(stderr, " i=%d\n", i);
  742. #endif
  743. if ((flags & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
  744. /* none significant */
  745. continue;
  746. }
  747. if ((flags & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
  748. (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
  749. /* all processed by sigpass */
  750. continue;
  751. }
  752. opj_t1_enc_refpass_step_macro(
  753. mqc, curctx, a, c, ct,
  754. flags, flagsUpdated,
  755. &datap[0],
  756. bpno,
  757. one,
  758. nmsedec,
  759. type,
  760. 0);
  761. opj_t1_enc_refpass_step_macro(
  762. mqc, curctx, a, c, ct,
  763. flags, flagsUpdated,
  764. &datap[1],
  765. bpno,
  766. one,
  767. nmsedec,
  768. type,
  769. 1);
  770. opj_t1_enc_refpass_step_macro(
  771. mqc, curctx, a, c, ct,
  772. flags, flagsUpdated,
  773. &datap[2],
  774. bpno,
  775. one,
  776. nmsedec,
  777. type,
  778. 2);
  779. opj_t1_enc_refpass_step_macro(
  780. mqc, curctx, a, c, ct,
  781. flags, flagsUpdated,
  782. &datap[3],
  783. bpno,
  784. one,
  785. nmsedec,
  786. type,
  787. 3);
  788. *f = flagsUpdated;
  789. }
  790. }
  791. if (k < t1->h) {
  792. OPJ_UINT32 j;
  793. const OPJ_UINT32 remaining_lines = t1->h - k;
  794. #ifdef DEBUG_ENC_REF
  795. fprintf(stderr, " k=%d\n", k);
  796. #endif
  797. for (i = 0; i < t1->w; ++i, ++f) {
  798. #ifdef DEBUG_ENC_REF
  799. fprintf(stderr, " i=%d\n", i);
  800. #endif
  801. if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
  802. /* none significant */
  803. datap += remaining_lines;
  804. continue;
  805. }
  806. for (j = 0; j < remaining_lines; ++j, datap ++) {
  807. opj_t1_enc_refpass_step_macro(
  808. mqc, curctx, a, c, ct,
  809. *f, *f,
  810. &datap[0],
  811. bpno,
  812. one,
  813. nmsedec,
  814. type,
  815. j);
  816. }
  817. }
  818. }
  819. UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct);
  820. }
  821. static void opj_t1_dec_refpass_raw(
  822. opj_t1_t *t1,
  823. OPJ_INT32 bpno)
  824. {
  825. OPJ_INT32 one, poshalf;
  826. OPJ_UINT32 i, j, k;
  827. OPJ_INT32 *data = t1->data;
  828. opj_flag_t *flagsp = &T1_FLAGS(0, 0);
  829. const OPJ_UINT32 l_w = t1->w;
  830. one = 1 << bpno;
  831. poshalf = one >> 1;
  832. for (k = 0; k < (t1->h & ~3U); k += 4, flagsp += 2, data += 3 * l_w) {
  833. for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
  834. opj_flag_t flags = *flagsp;
  835. if (flags != 0) {
  836. opj_t1_dec_refpass_step_raw(
  837. t1,
  838. flagsp,
  839. data,
  840. poshalf,
  841. 0U);
  842. opj_t1_dec_refpass_step_raw(
  843. t1,
  844. flagsp,
  845. data + l_w,
  846. poshalf,
  847. 1U);
  848. opj_t1_dec_refpass_step_raw(
  849. t1,
  850. flagsp,
  851. data + 2 * l_w,
  852. poshalf,
  853. 2U);
  854. opj_t1_dec_refpass_step_raw(
  855. t1,
  856. flagsp,
  857. data + 3 * l_w,
  858. poshalf,
  859. 3U);
  860. }
  861. }
  862. }
  863. if (k < t1->h) {
  864. for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
  865. for (j = 0; j < t1->h - k; ++j) {
  866. opj_t1_dec_refpass_step_raw(
  867. t1,
  868. flagsp,
  869. data + j * l_w,
  870. poshalf,
  871. j);
  872. }
  873. }
  874. }
  875. }
  876. #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
  877. { \
  878. OPJ_INT32 one, poshalf; \
  879. OPJ_UINT32 i, j, k; \
  880. register OPJ_INT32 *data = t1->data; \
  881. register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
  882. const OPJ_UINT32 l_w = w; \
  883. opj_mqc_t* mqc = &(t1->mqc); \
  884. DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \
  885. register OPJ_UINT32 v; \
  886. one = 1 << bpno; \
  887. poshalf = one >> 1; \
  888. for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
  889. for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
  890. opj_flag_t flags = *flagsp; \
  891. if( flags != 0 ) { \
  892. opj_t1_dec_refpass_step_mqc_macro( \
  893. flags, data, l_w, 0, \
  894. mqc, curctx, v, a, c, ct, poshalf); \
  895. opj_t1_dec_refpass_step_mqc_macro( \
  896. flags, data, l_w, 1, \
  897. mqc, curctx, v, a, c, ct, poshalf); \
  898. opj_t1_dec_refpass_step_mqc_macro( \
  899. flags, data, l_w, 2, \
  900. mqc, curctx, v, a, c, ct, poshalf); \
  901. opj_t1_dec_refpass_step_mqc_macro( \
  902. flags, data, l_w, 3, \
  903. mqc, curctx, v, a, c, ct, poshalf); \
  904. *flagsp = flags; \
  905. } \
  906. } \
  907. } \
  908. UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \
  909. if( k < h ) { \
  910. for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
  911. for (j = 0; j < h - k; ++j) { \
  912. opj_t1_dec_refpass_step_mqc(t1, flagsp, data + j * l_w, poshalf, j); \
  913. } \
  914. } \
  915. } \
  916. }
  917. static void opj_t1_dec_refpass_mqc_64x64(
  918. opj_t1_t *t1,
  919. OPJ_INT32 bpno)
  920. {
  921. opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
  922. }
  923. static void opj_t1_dec_refpass_mqc_generic(
  924. opj_t1_t *t1,
  925. OPJ_INT32 bpno)
  926. {
  927. opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2U);
  928. }
  929. static void opj_t1_dec_refpass_mqc(
  930. opj_t1_t *t1,
  931. OPJ_INT32 bpno)
  932. {
  933. if (t1->w == 64 && t1->h == 64) {
  934. opj_t1_dec_refpass_mqc_64x64(t1, bpno);
  935. } else {
  936. opj_t1_dec_refpass_mqc_generic(t1, bpno);
  937. }
  938. }
  939. /**
  940. Encode clean-up pass step
  941. */
  942. #define opj_t1_enc_clnpass_step_macro(mqc, curctx, a, c, ct, flagspIn, datapIn, bpno, one, nmsedec, agg, runlen, lim, cblksty) \
  943. { \
  944. OPJ_UINT32 v; \
  945. OPJ_UINT32 ci; \
  946. opj_flag_t* const flagsp = (flagspIn); \
  947. const OPJ_INT32* l_datap = (datapIn); \
  948. const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 | \
  949. T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
  950. \
  951. if ((*flagsp & check) == check) { \
  952. if (runlen == 0) { \
  953. *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
  954. } else if (runlen == 1) { \
  955. *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3); \
  956. } else if (runlen == 2) { \
  957. *flagsp &= ~(T1_PI_2 | T1_PI_3); \
  958. } else if (runlen == 3) { \
  959. *flagsp &= ~(T1_PI_3); \
  960. } \
  961. } \
  962. else \
  963. for (ci = runlen; ci < lim; ++ci) { \
  964. OPJ_BOOL goto_PARTIAL = OPJ_FALSE; \
  965. if ((agg != 0) && (ci == runlen)) { \
  966. goto_PARTIAL = OPJ_TRUE; \
  967. } \
  968. else if (!(*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) { \
  969. OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, *flagsp >> (ci * 3U)); \
  970. /* #ifdef DEBUG_ENC_CLN */ \
  971. /* printf(" ctxt1=%d\n", ctxt1); */ \
  972. /* #endif */ \
  973. opj_t1_setcurctx(curctx, ctxt1); \
  974. v = (opj_smr_abs(*l_datap) & (OPJ_UINT32)one) ? 1 : 0; \
  975. opj_mqc_encode_macro(mqc, curctx, a, c, ct, v); \
  976. if (v) { \
  977. goto_PARTIAL = OPJ_TRUE; \
  978. } \
  979. } \
  980. if( goto_PARTIAL ) { \
  981. OPJ_UINT32 vsc; \
  982. OPJ_UINT32 ctxt2, spb; \
  983. OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
  984. *flagsp, \
  985. flagsp[-1], flagsp[1], \
  986. ci); \
  987. *nmsedec += opj_t1_getnmsedec_sig(opj_smr_abs(*l_datap), \
  988. (OPJ_UINT32)bpno); \
  989. ctxt2 = opj_t1_getctxno_sc(lu); \
  990. /* #ifdef DEBUG_ENC_CLN */ \
  991. /* printf(" ctxt2=%d\n", ctxt2); */ \
  992. /* #endif */ \
  993. opj_t1_setcurctx(curctx, ctxt2); \
  994. \
  995. v = opj_smr_sign(*l_datap); \
  996. spb = opj_t1_getspb(lu); \
  997. /* #ifdef DEBUG_ENC_CLN */ \
  998. /* printf(" spb=%d\n", spb); */\
  999. /* #endif */ \
  1000. opj_mqc_encode_macro(mqc, curctx, a, c, ct, v ^ spb); \
  1001. vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == 0)) ? 1 : 0; \
  1002. opj_t1_update_flags(flagsp, ci, v, t1->w + 2U, vsc); \
  1003. } \
  1004. *flagsp &= ~(T1_PI_THIS << (3U * ci)); \
  1005. l_datap ++; \
  1006. } \
  1007. }
  1008. #define opj_t1_dec_clnpass_step_macro(check_flags, partial, \
  1009. flags, flagsp, flags_stride, data, \
  1010. data_stride, ci, mqc, curctx, \
  1011. v, a, c, ct, oneplushalf, vsc) \
  1012. { \
  1013. if ( !check_flags || !(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {\
  1014. do { \
  1015. if( !partial ) { \
  1016. OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
  1017. opj_t1_setcurctx(curctx, ctxt1); \
  1018. opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
  1019. if( !v ) \
  1020. break; \
  1021. } \
  1022. { \
  1023. OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
  1024. flags, flagsp[-1], flagsp[1], \
  1025. ci); \
  1026. opj_t1_setcurctx(curctx, opj_t1_getctxno_sc(lu)); \
  1027. opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
  1028. v = v ^ opj_t1_getspb(lu); \
  1029. data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \
  1030. opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride, vsc); \
  1031. } \
  1032. } while(0); \
  1033. } \
  1034. }
  1035. static void opj_t1_dec_clnpass_step(
  1036. opj_t1_t *t1,
  1037. opj_flag_t *flagsp,
  1038. OPJ_INT32 *datap,
  1039. OPJ_INT32 oneplushalf,
  1040. OPJ_UINT32 ci,
  1041. OPJ_UINT32 vsc)
  1042. {
  1043. OPJ_UINT32 v;
  1044. opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
  1045. opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE,
  1046. *flagsp, flagsp, t1->w + 2U, datap,
  1047. 0, ci, mqc, mqc->curctx,
  1048. v, mqc->a, mqc->c, mqc->ct, oneplushalf, vsc);
  1049. }
  1050. static void opj_t1_enc_clnpass(
  1051. opj_t1_t *t1,
  1052. OPJ_INT32 bpno,
  1053. OPJ_INT32 *nmsedec,
  1054. OPJ_UINT32 cblksty)
  1055. {
  1056. OPJ_UINT32 i, k;
  1057. const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
  1058. opj_mqc_t* mqc = &(t1->mqc);
  1059. DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct);
  1060. const OPJ_INT32* datap = t1->data;
  1061. opj_flag_t *f = &T1_FLAGS(0, 0);
  1062. const OPJ_UINT32 extra = 2U;
  1063. *nmsedec = 0;
  1064. #ifdef DEBUG_ENC_CLN
  1065. printf("enc_clnpass: bpno=%d\n", bpno);
  1066. #endif
  1067. for (k = 0; k < (t1->h & ~3U); k += 4, f += extra) {
  1068. #ifdef DEBUG_ENC_CLN
  1069. printf(" k=%d\n", k);
  1070. #endif
  1071. for (i = 0; i < t1->w; ++i, f++) {
  1072. OPJ_UINT32 agg, runlen;
  1073. #ifdef DEBUG_ENC_CLN
  1074. printf(" i=%d\n", i);
  1075. #endif
  1076. agg = !*f;
  1077. #ifdef DEBUG_ENC_CLN
  1078. printf(" agg=%d\n", agg);
  1079. #endif
  1080. if (agg) {
  1081. for (runlen = 0; runlen < 4; ++runlen, ++datap) {
  1082. if (opj_smr_abs(*datap) & (OPJ_UINT32)one) {
  1083. break;
  1084. }
  1085. }
  1086. opj_t1_setcurctx(curctx, T1_CTXNO_AGG);
  1087. opj_mqc_encode_macro(mqc, curctx, a, c, ct, runlen != 4);
  1088. if (runlen == 4) {
  1089. continue;
  1090. }
  1091. opj_t1_setcurctx(curctx, T1_CTXNO_UNI);
  1092. opj_mqc_encode_macro(mqc, curctx, a, c, ct, runlen >> 1);
  1093. opj_mqc_encode_macro(mqc, curctx, a, c, ct, runlen & 1);
  1094. } else {
  1095. runlen = 0;
  1096. }
  1097. opj_t1_enc_clnpass_step_macro(
  1098. mqc, curctx, a, c, ct,
  1099. f,
  1100. datap,
  1101. bpno,
  1102. one,
  1103. nmsedec,
  1104. agg,
  1105. runlen,
  1106. 4U,
  1107. cblksty);
  1108. datap += 4 - runlen;
  1109. }
  1110. }
  1111. if (k < t1->h) {
  1112. const OPJ_UINT32 agg = 0;
  1113. const OPJ_UINT32 runlen = 0;
  1114. #ifdef DEBUG_ENC_CLN
  1115. printf(" k=%d\n", k);
  1116. #endif
  1117. for (i = 0; i < t1->w; ++i, f++) {
  1118. #ifdef DEBUG_ENC_CLN
  1119. printf(" i=%d\n", i);
  1120. printf(" agg=%d\n", agg);
  1121. #endif
  1122. opj_t1_enc_clnpass_step_macro(
  1123. mqc, curctx, a, c, ct,
  1124. f,
  1125. datap,
  1126. bpno,
  1127. one,
  1128. nmsedec,
  1129. agg,
  1130. runlen,
  1131. t1->h - k,
  1132. cblksty);
  1133. datap += t1->h - k;
  1134. }
  1135. }
  1136. UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct);
  1137. }
  1138. #define opj_t1_dec_clnpass_internal(t1, bpno, vsc, w, h, flags_stride) \
  1139. { \
  1140. OPJ_INT32 one, half, oneplushalf; \
  1141. OPJ_UINT32 runlen; \
  1142. OPJ_UINT32 i, j, k; \
  1143. const OPJ_UINT32 l_w = w; \
  1144. opj_mqc_t* mqc = &(t1->mqc); \
  1145. register OPJ_INT32 *data = t1->data; \
  1146. register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
  1147. DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \
  1148. register OPJ_UINT32 v; \
  1149. one = 1 << bpno; \
  1150. half = one >> 1; \
  1151. oneplushalf = one | half; \
  1152. for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
  1153. for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
  1154. opj_flag_t flags = *flagsp; \
  1155. if (flags == 0) { \
  1156. OPJ_UINT32 partial = OPJ_TRUE; \
  1157. opj_t1_setcurctx(curctx, T1_CTXNO_AGG); \
  1158. opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
  1159. if (!v) { \
  1160. continue; \
  1161. } \
  1162. opj_t1_setcurctx(curctx, T1_CTXNO_UNI); \
  1163. opj_mqc_decode_macro(runlen, mqc, curctx, a, c, ct); \
  1164. opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
  1165. runlen = (runlen << 1) | v; \
  1166. switch(runlen) { \
  1167. case 0: \
  1168. opj_t1_dec_clnpass_step_macro(OPJ_FALSE, OPJ_TRUE,\
  1169. flags, flagsp, flags_stride, data, \
  1170. l_w, 0, mqc, curctx, \
  1171. v, a, c, ct, oneplushalf, vsc); \
  1172. partial = OPJ_FALSE; \
  1173. /* FALLTHRU */ \
  1174. case 1: \
  1175. opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
  1176. flags, flagsp, flags_stride, data, \
  1177. l_w, 1, mqc, curctx, \
  1178. v, a, c, ct, oneplushalf, OPJ_FALSE); \
  1179. partial = OPJ_FALSE; \
  1180. /* FALLTHRU */ \
  1181. case 2: \
  1182. opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
  1183. flags, flagsp, flags_stride, data, \
  1184. l_w, 2, mqc, curctx, \
  1185. v, a, c, ct, oneplushalf, OPJ_FALSE); \
  1186. partial = OPJ_FALSE; \
  1187. /* FALLTHRU */ \
  1188. case 3: \
  1189. opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
  1190. flags, flagsp, flags_stride, data, \
  1191. l_w, 3, mqc, curctx, \
  1192. v, a, c, ct, oneplushalf, OPJ_FALSE); \
  1193. break; \
  1194. } \
  1195. } else { \
  1196. opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
  1197. flags, flagsp, flags_stride, data, \
  1198. l_w, 0, mqc, curctx, \
  1199. v, a, c, ct, oneplushalf, vsc); \
  1200. opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
  1201. flags, flagsp, flags_stride, data, \
  1202. l_w, 1, mqc, curctx, \
  1203. v, a, c, ct, oneplushalf, OPJ_FALSE); \
  1204. opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
  1205. flags, flagsp, flags_stride, data, \
  1206. l_w, 2, mqc, curctx, \
  1207. v, a, c, ct, oneplushalf, OPJ_FALSE); \
  1208. opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
  1209. flags, flagsp, flags_stride, data, \
  1210. l_w, 3, mqc, curctx, \
  1211. v, a, c, ct, oneplushalf, OPJ_FALSE); \
  1212. } \
  1213. *flagsp = flags & ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
  1214. } \
  1215. } \
  1216. UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \
  1217. if( k < h ) { \
  1218. for (i = 0; i < l_w; ++i, ++flagsp, ++data) { \
  1219. for (j = 0; j < h - k; ++j) { \
  1220. opj_t1_dec_clnpass_step(t1, flagsp, data + j * l_w, oneplushalf, j, vsc); \
  1221. } \
  1222. *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
  1223. } \
  1224. } \
  1225. }
  1226. static void opj_t1_dec_clnpass_check_segsym(opj_t1_t *t1, OPJ_INT32 cblksty)
  1227. {
  1228. if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
  1229. opj_mqc_t* mqc = &(t1->mqc);
  1230. OPJ_UINT32 v, v2;
  1231. opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
  1232. opj_mqc_decode(v, mqc);
  1233. opj_mqc_decode(v2, mqc);
  1234. v = (v << 1) | v2;
  1235. opj_mqc_decode(v2, mqc);
  1236. v = (v << 1) | v2;
  1237. opj_mqc_decode(v2, mqc);
  1238. v = (v << 1) | v2;
  1239. /*
  1240. if (v!=0xa) {
  1241. opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
  1242. }
  1243. */
  1244. }
  1245. }
  1246. static void opj_t1_dec_clnpass_64x64_novsc(
  1247. opj_t1_t *t1,
  1248. OPJ_INT32 bpno)
  1249. {
  1250. opj_t1_dec_clnpass_internal(t1, bpno, OPJ_FALSE, 64, 64, 66);
  1251. }
  1252. static void opj_t1_dec_clnpass_64x64_vsc(
  1253. opj_t1_t *t1,
  1254. OPJ_INT32 bpno)
  1255. {
  1256. opj_t1_dec_clnpass_internal(t1, bpno, OPJ_TRUE, 64, 64, 66);
  1257. }
  1258. static void opj_t1_dec_clnpass_generic_novsc(
  1259. opj_t1_t *t1,
  1260. OPJ_INT32 bpno)
  1261. {
  1262. opj_t1_dec_clnpass_internal(t1, bpno, OPJ_FALSE, t1->w, t1->h,
  1263. t1->w + 2U);
  1264. }
  1265. static void opj_t1_dec_clnpass_generic_vsc(
  1266. opj_t1_t *t1,
  1267. OPJ_INT32 bpno)
  1268. {
  1269. opj_t1_dec_clnpass_internal(t1, bpno, OPJ_TRUE, t1->w, t1->h,
  1270. t1->w + 2U);
  1271. }
  1272. static void opj_t1_dec_clnpass(
  1273. opj_t1_t *t1,
  1274. OPJ_INT32 bpno,
  1275. OPJ_INT32 cblksty)
  1276. {
  1277. if (t1->w == 64 && t1->h == 64) {
  1278. if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  1279. opj_t1_dec_clnpass_64x64_vsc(t1, bpno);
  1280. } else {
  1281. opj_t1_dec_clnpass_64x64_novsc(t1, bpno);
  1282. }
  1283. } else {
  1284. if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  1285. opj_t1_dec_clnpass_generic_vsc(t1, bpno);
  1286. } else {
  1287. opj_t1_dec_clnpass_generic_novsc(t1, bpno);
  1288. }
  1289. }
  1290. opj_t1_dec_clnpass_check_segsym(t1, cblksty);
  1291. }
  1292. /** mod fixed_quality */
  1293. static OPJ_FLOAT64 opj_t1_getwmsedec(
  1294. OPJ_INT32 nmsedec,
  1295. OPJ_UINT32 compno,
  1296. OPJ_UINT32 level,
  1297. OPJ_UINT32 orient,
  1298. OPJ_INT32 bpno,
  1299. OPJ_UINT32 qmfbid,
  1300. OPJ_FLOAT64 stepsize,
  1301. OPJ_UINT32 numcomps,
  1302. const OPJ_FLOAT64 * mct_norms,
  1303. OPJ_UINT32 mct_numcomps)
  1304. {
  1305. OPJ_FLOAT64 w1 = 1, w2, wmsedec;
  1306. OPJ_ARG_NOT_USED(numcomps);
  1307. if (mct_norms && (compno < mct_numcomps)) {
  1308. w1 = mct_norms[compno];
  1309. }
  1310. if (qmfbid == 1) {
  1311. w2 = opj_dwt_getnorm(level, orient);
  1312. } else { /* if (qmfbid == 0) */
  1313. const OPJ_INT32 log2_gain = (orient == 0) ? 0 :
  1314. (orient == 3) ? 2 : 1;
  1315. w2 = opj_dwt_getnorm_real(level, orient);
  1316. /* Not sure this is right. But preserves past behaviour */
  1317. stepsize /= (1 << log2_gain);
  1318. }
  1319. wmsedec = w1 * w2 * stepsize * (1 << bpno);
  1320. wmsedec *= wmsedec * nmsedec / 8192.0;
  1321. return wmsedec;
  1322. }
  1323. static OPJ_BOOL opj_t1_allocate_buffers(
  1324. opj_t1_t *t1,
  1325. OPJ_UINT32 w,
  1326. OPJ_UINT32 h)
  1327. {
  1328. OPJ_UINT32 flagssize;
  1329. OPJ_UINT32 flags_stride;
  1330. /* No risk of overflow. Prior checks ensure those assert are met */
  1331. /* They are per the specification */
  1332. assert(w <= 1024);
  1333. assert(h <= 1024);
  1334. assert(w * h <= 4096);
  1335. /* encoder uses tile buffer, so no need to allocate */
  1336. {
  1337. OPJ_UINT32 datasize = w * h;
  1338. if (datasize > t1->datasize) {
  1339. opj_aligned_free(t1->data);
  1340. t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
  1341. if (!t1->data) {
  1342. /* FIXME event manager error callback */
  1343. return OPJ_FALSE;
  1344. }
  1345. t1->datasize = datasize;
  1346. }
  1347. /* memset first arg is declared to never be null by gcc */
  1348. if (t1->data != NULL) {
  1349. memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
  1350. }
  1351. }
  1352. flags_stride = w + 2U; /* can't be 0U */
  1353. flagssize = (h + 3U) / 4U + 2U;
  1354. flagssize *= flags_stride;
  1355. {
  1356. opj_flag_t* p;
  1357. OPJ_UINT32 x;
  1358. OPJ_UINT32 flags_height = (h + 3U) / 4U;
  1359. if (flagssize > t1->flagssize) {
  1360. opj_aligned_free(t1->flags);
  1361. t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(
  1362. opj_flag_t));
  1363. if (!t1->flags) {
  1364. /* FIXME event manager error callback */
  1365. return OPJ_FALSE;
  1366. }
  1367. }
  1368. t1->flagssize = flagssize;
  1369. memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
  1370. p = &t1->flags[0];
  1371. for (x = 0; x < flags_stride; ++x) {
  1372. /* magic value to hopefully stop any passes being interested in this entry */
  1373. *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
  1374. }
  1375. p = &t1->flags[((flags_height + 1) * flags_stride)];
  1376. for (x = 0; x < flags_stride; ++x) {
  1377. /* magic value to hopefully stop any passes being interested in this entry */
  1378. *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
  1379. }
  1380. if (h % 4) {
  1381. OPJ_UINT32 v = 0;
  1382. p = &t1->flags[((flags_height) * flags_stride)];
  1383. if (h % 4 == 1) {
  1384. v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
  1385. } else if (h % 4 == 2) {
  1386. v |= T1_PI_2 | T1_PI_3;
  1387. } else if (h % 4 == 3) {
  1388. v |= T1_PI_3;
  1389. }
  1390. for (x = 0; x < flags_stride; ++x) {
  1391. *p++ = v;
  1392. }
  1393. }
  1394. }
  1395. t1->w = w;
  1396. t1->h = h;
  1397. return OPJ_TRUE;
  1398. }
  1399. /* ----------------------------------------------------------------------- */
  1400. /* ----------------------------------------------------------------------- */
  1401. /**
  1402. * Creates a new Tier 1 handle
  1403. * and initializes the look-up tables of the Tier-1 coder/decoder
  1404. * @return a new T1 handle if successful, returns NULL otherwise
  1405. */
  1406. opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
  1407. {
  1408. opj_t1_t *l_t1 = 00;
  1409. l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
  1410. if (!l_t1) {
  1411. return 00;
  1412. }
  1413. l_t1->encoder = isEncoder;
  1414. return l_t1;
  1415. }
  1416. /**
  1417. * Destroys a previously created T1 handle
  1418. *
  1419. * @param p_t1 Tier 1 handle to destroy
  1420. */
  1421. void opj_t1_destroy(opj_t1_t *p_t1)
  1422. {
  1423. if (! p_t1) {
  1424. return;
  1425. }
  1426. if (p_t1->data) {
  1427. opj_aligned_free(p_t1->data);
  1428. p_t1->data = 00;
  1429. }
  1430. if (p_t1->flags) {
  1431. opj_aligned_free(p_t1->flags);
  1432. p_t1->flags = 00;
  1433. }
  1434. opj_free(p_t1->cblkdatabuffer);
  1435. opj_free(p_t1);
  1436. }
  1437. typedef struct {
  1438. OPJ_BOOL whole_tile_decoding;
  1439. OPJ_UINT32 resno;
  1440. opj_tcd_cblk_dec_t* cblk;
  1441. opj_tcd_band_t* band;
  1442. opj_tcd_tilecomp_t* tilec;
  1443. opj_tccp_t* tccp;
  1444. OPJ_BOOL mustuse_cblkdatabuffer;
  1445. volatile OPJ_BOOL* pret;
  1446. opj_event_mgr_t *p_manager;
  1447. opj_mutex_t* p_manager_mutex;
  1448. OPJ_BOOL check_pterm;
  1449. } opj_t1_cblk_decode_processing_job_t;
  1450. static void opj_t1_destroy_wrapper(void* t1)
  1451. {
  1452. opj_t1_destroy((opj_t1_t*) t1);
  1453. }
  1454. static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
  1455. {
  1456. opj_tcd_cblk_dec_t* cblk;
  1457. opj_tcd_band_t* band;
  1458. opj_tcd_tilecomp_t* tilec;
  1459. opj_tccp_t* tccp;
  1460. OPJ_INT32* OPJ_RESTRICT datap;
  1461. OPJ_UINT32 cblk_w, cblk_h;
  1462. OPJ_INT32 x, y;
  1463. OPJ_UINT32 i, j;
  1464. opj_t1_cblk_decode_processing_job_t* job;
  1465. opj_t1_t* t1;
  1466. OPJ_UINT32 resno;
  1467. OPJ_UINT32 tile_w;
  1468. job = (opj_t1_cblk_decode_processing_job_t*) user_data;
  1469. cblk = job->cblk;
  1470. if (!job->whole_tile_decoding) {
  1471. cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
  1472. cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
  1473. cblk->decoded_data = (OPJ_INT32*)opj_aligned_malloc(sizeof(OPJ_INT32) *
  1474. cblk_w * cblk_h);
  1475. if (cblk->decoded_data == NULL) {
  1476. if (job->p_manager_mutex) {
  1477. opj_mutex_lock(job->p_manager_mutex);
  1478. }
  1479. opj_event_msg(job->p_manager, EVT_ERROR,
  1480. "Cannot allocate cblk->decoded_data\n");
  1481. if (job->p_manager_mutex) {
  1482. opj_mutex_unlock(job->p_manager_mutex);
  1483. }
  1484. *(job->pret) = OPJ_FALSE;
  1485. opj_free(job);
  1486. return;
  1487. }
  1488. /* Zero-init required */
  1489. memset(cblk->decoded_data, 0, sizeof(OPJ_INT32) * cblk_w * cblk_h);
  1490. } else if (cblk->decoded_data) {
  1491. /* Not sure if that code path can happen, but better be */
  1492. /* safe than sorry */
  1493. opj_aligned_free(cblk->decoded_data);
  1494. cblk->decoded_data = NULL;
  1495. }
  1496. resno = job->resno;
  1497. band = job->band;
  1498. tilec = job->tilec;
  1499. tccp = job->tccp;
  1500. tile_w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions - 1].x1
  1501. -
  1502. tilec->resolutions[tilec->minimum_num_resolutions - 1].x0);
  1503. if (!*(job->pret)) {
  1504. opj_free(job);
  1505. return;
  1506. }
  1507. t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
  1508. if (t1 == NULL) {
  1509. t1 = opj_t1_create(OPJ_FALSE);
  1510. if (t1 == NULL) {
  1511. opj_event_msg(job->p_manager, EVT_ERROR,
  1512. "Cannot allocate Tier 1 handle\n");
  1513. *(job->pret) = OPJ_FALSE;
  1514. opj_free(job);
  1515. return;
  1516. }
  1517. if (!opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper)) {
  1518. opj_event_msg(job->p_manager, EVT_ERROR,
  1519. "Unable to set t1 handle as TLS\n");
  1520. opj_t1_destroy(t1);
  1521. *(job->pret) = OPJ_FALSE;
  1522. opj_free(job);
  1523. return;
  1524. }
  1525. }
  1526. t1->mustuse_cblkdatabuffer = job->mustuse_cblkdatabuffer;
  1527. if (OPJ_FALSE == opj_t1_decode_cblk(
  1528. t1,
  1529. cblk,
  1530. band->bandno,
  1531. (OPJ_UINT32)tccp->roishift,
  1532. tccp->cblksty,
  1533. job->p_manager,
  1534. job->p_manager_mutex,
  1535. job->check_pterm)) {
  1536. *(job->pret) = OPJ_FALSE;
  1537. opj_free(job);
  1538. return;
  1539. }
  1540. x = cblk->x0 - band->x0;
  1541. y = cblk->y0 - band->y0;
  1542. if (band->bandno & 1) {
  1543. opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
  1544. x += pres->x1 - pres->x0;
  1545. }
  1546. if (band->bandno & 2) {
  1547. opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
  1548. y += pres->y1 - pres->y0;
  1549. }
  1550. datap = cblk->decoded_data ? cblk->decoded_data : t1->data;
  1551. cblk_w = t1->w;
  1552. cblk_h = t1->h;
  1553. if (tccp->roishift) {
  1554. if (tccp->roishift >= 31) {
  1555. for (j = 0; j < cblk_h; ++j) {
  1556. for (i = 0; i < cblk_w; ++i) {
  1557. datap[(j * cblk_w) + i] = 0;
  1558. }
  1559. }
  1560. } else {
  1561. OPJ_INT32 thresh = 1 << tccp->roishift;
  1562. for (j = 0; j < cblk_h; ++j) {
  1563. for (i = 0; i < cblk_w; ++i) {
  1564. OPJ_INT32 val = datap[(j * cblk_w) + i];
  1565. OPJ_INT32 mag = abs(val);
  1566. if (mag >= thresh) {
  1567. mag >>= tccp->roishift;
  1568. datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
  1569. }
  1570. }
  1571. }
  1572. }
  1573. }
  1574. /* Both can be non NULL if for example decoding a full tile and then */
  1575. /* partially a tile. In which case partial decoding should be the */
  1576. /* priority */
  1577. assert((cblk->decoded_data != NULL) || (tilec->data != NULL));
  1578. if (cblk->decoded_data) {
  1579. OPJ_UINT32 cblk_size = cblk_w * cblk_h;
  1580. if (tccp->qmfbid == 1) {
  1581. for (i = 0; i < cblk_size; ++i) {
  1582. datap[i] /= 2;
  1583. }
  1584. } else { /* if (tccp->qmfbid == 0) */
  1585. const float stepsize = 0.5f * band->stepsize;
  1586. i = 0;
  1587. #ifdef __SSE2__
  1588. {
  1589. const __m128 xmm_stepsize = _mm_set1_ps(stepsize);
  1590. for (; i < (cblk_size & ~15U); i += 16) {
  1591. __m128 xmm0_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)(
  1592. datap + 0)));
  1593. __m128 xmm1_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)(
  1594. datap + 4)));
  1595. __m128 xmm2_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)(
  1596. datap + 8)));
  1597. __m128 xmm3_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)(
  1598. datap + 12)));
  1599. _mm_store_ps((float*)(datap + 0), _mm_mul_ps(xmm0_data, xmm_stepsize));
  1600. _mm_store_ps((float*)(datap + 4), _mm_mul_ps(xmm1_data, xmm_stepsize));
  1601. _mm_store_ps((float*)(datap + 8), _mm_mul_ps(xmm2_data, xmm_stepsize));
  1602. _mm_store_ps((float*)(datap + 12), _mm_mul_ps(xmm3_data, xmm_stepsize));
  1603. datap += 16;
  1604. }
  1605. }
  1606. #endif
  1607. for (; i < cblk_size; ++i) {
  1608. OPJ_FLOAT32 tmp = ((OPJ_FLOAT32)(*datap)) * stepsize;
  1609. memcpy(datap, &tmp, sizeof(tmp));
  1610. datap++;
  1611. }
  1612. }
  1613. } else if (tccp->qmfbid == 1) {
  1614. OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_SIZE_T)y * tile_w +
  1615. (OPJ_SIZE_T)x];
  1616. for (j = 0; j < cblk_h; ++j) {
  1617. i = 0;
  1618. for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
  1619. OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
  1620. OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
  1621. OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
  1622. OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
  1623. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 0U] = tmp0 / 2;
  1624. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 1U] = tmp1 / 2;
  1625. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 2U] = tmp2 / 2;
  1626. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 3U] = tmp3 / 2;
  1627. }
  1628. for (; i < cblk_w; ++i) {
  1629. OPJ_INT32 tmp = datap[(j * cblk_w) + i];
  1630. ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i] = tmp / 2;
  1631. }
  1632. }
  1633. } else { /* if (tccp->qmfbid == 0) */
  1634. const float stepsize = 0.5f * band->stepsize;
  1635. OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_SIZE_T)y *
  1636. tile_w + (OPJ_SIZE_T)x];
  1637. for (j = 0; j < cblk_h; ++j) {
  1638. OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
  1639. for (i = 0; i < cblk_w; ++i) {
  1640. OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * stepsize;
  1641. *tiledp2 = tmp;
  1642. datap++;
  1643. tiledp2++;
  1644. }
  1645. tiledp += tile_w;
  1646. }
  1647. }
  1648. opj_free(job);
  1649. }
  1650. void opj_t1_decode_cblks(opj_tcd_t* tcd,
  1651. volatile OPJ_BOOL* pret,
  1652. opj_tcd_tilecomp_t* tilec,
  1653. opj_tccp_t* tccp,
  1654. opj_event_mgr_t *p_manager,
  1655. opj_mutex_t* p_manager_mutex,
  1656. OPJ_BOOL check_pterm
  1657. )
  1658. {
  1659. opj_thread_pool_t* tp = tcd->thread_pool;
  1660. OPJ_UINT32 resno, bandno, precno, cblkno;
  1661. #ifdef DEBUG_VERBOSE
  1662. OPJ_UINT32 codeblocks_decoded = 0;
  1663. printf("Enter opj_t1_decode_cblks()\n");
  1664. #endif
  1665. for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
  1666. opj_tcd_resolution_t* res = &tilec->resolutions[resno];
  1667. for (bandno = 0; bandno < res->numbands; ++bandno) {
  1668. opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
  1669. for (precno = 0; precno < res->pw * res->ph; ++precno) {
  1670. opj_tcd_precinct_t* precinct = &band->precincts[precno];
  1671. if (!opj_tcd_is_subband_area_of_interest(tcd,
  1672. tilec->compno,
  1673. resno,
  1674. band->bandno,
  1675. (OPJ_UINT32)precinct->x0,
  1676. (OPJ_UINT32)precinct->y0,
  1677. (OPJ_UINT32)precinct->x1,
  1678. (OPJ_UINT32)precinct->y1)) {
  1679. for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
  1680. opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
  1681. if (cblk->decoded_data) {
  1682. #ifdef DEBUG_VERBOSE
  1683. printf("Discarding codeblock %d,%d at resno=%d, bandno=%d\n",
  1684. cblk->x0, cblk->y0, resno, bandno);
  1685. #endif
  1686. opj_aligned_free(cblk->decoded_data);
  1687. cblk->decoded_data = NULL;
  1688. }
  1689. }
  1690. continue;
  1691. }
  1692. for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
  1693. opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
  1694. opj_t1_cblk_decode_processing_job_t* job;
  1695. if (!opj_tcd_is_subband_area_of_interest(tcd,
  1696. tilec->compno,
  1697. resno,
  1698. band->bandno,
  1699. (OPJ_UINT32)cblk->x0,
  1700. (OPJ_UINT32)cblk->y0,
  1701. (OPJ_UINT32)cblk->x1,
  1702. (OPJ_UINT32)cblk->y1)) {
  1703. if (cblk->decoded_data) {
  1704. #ifdef DEBUG_VERBOSE
  1705. printf("Discarding codeblock %d,%d at resno=%d, bandno=%d\n",
  1706. cblk->x0, cblk->y0, resno, bandno);
  1707. #endif
  1708. opj_aligned_free(cblk->decoded_data);
  1709. cblk->decoded_data = NULL;
  1710. }
  1711. continue;
  1712. }
  1713. if (!tcd->whole_tile_decoding) {
  1714. OPJ_UINT32 cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0);
  1715. OPJ_UINT32 cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0);
  1716. if (cblk->decoded_data != NULL) {
  1717. #ifdef DEBUG_VERBOSE
  1718. printf("Reusing codeblock %d,%d at resno=%d, bandno=%d\n",
  1719. cblk->x0, cblk->y0, resno, bandno);
  1720. #endif
  1721. continue;
  1722. }
  1723. if (cblk_w == 0 || cblk_h == 0) {
  1724. continue;
  1725. }
  1726. #ifdef DEBUG_VERBOSE
  1727. printf("Decoding codeblock %d,%d at resno=%d, bandno=%d\n",
  1728. cblk->x0, cblk->y0, resno, bandno);
  1729. #endif
  1730. }
  1731. job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
  1732. sizeof(opj_t1_cblk_decode_processing_job_t));
  1733. if (!job) {
  1734. *pret = OPJ_FALSE;
  1735. return;
  1736. }
  1737. job->whole_tile_decoding = tcd->whole_tile_decoding;
  1738. job->resno = resno;
  1739. job->cblk = cblk;
  1740. job->band = band;
  1741. job->tilec = tilec;
  1742. job->tccp = tccp;
  1743. job->pret = pret;
  1744. job->p_manager_mutex = p_manager_mutex;
  1745. job->p_manager = p_manager;
  1746. job->check_pterm = check_pterm;
  1747. job->mustuse_cblkdatabuffer = opj_thread_pool_get_thread_count(tp) > 1;
  1748. opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
  1749. #ifdef DEBUG_VERBOSE
  1750. codeblocks_decoded ++;
  1751. #endif
  1752. if (!(*pret)) {
  1753. return;
  1754. }
  1755. } /* cblkno */
  1756. } /* precno */
  1757. } /* bandno */
  1758. } /* resno */
  1759. #ifdef DEBUG_VERBOSE
  1760. printf("Leave opj_t1_decode_cblks(). Number decoded: %d\n", codeblocks_decoded);
  1761. #endif
  1762. return;
  1763. }
  1764. static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
  1765. opj_tcd_cblk_dec_t* cblk,
  1766. OPJ_UINT32 orient,
  1767. OPJ_UINT32 roishift,
  1768. OPJ_UINT32 cblksty,
  1769. opj_event_mgr_t *p_manager,
  1770. opj_mutex_t* p_manager_mutex,
  1771. OPJ_BOOL check_pterm)
  1772. {
  1773. opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
  1774. OPJ_INT32 bpno_plus_one;
  1775. OPJ_UINT32 passtype;
  1776. OPJ_UINT32 segno, passno;
  1777. OPJ_BYTE* cblkdata = NULL;
  1778. OPJ_UINT32 cblkdataindex = 0;
  1779. OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
  1780. OPJ_INT32* original_t1_data = NULL;
  1781. mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
  1782. if (!opj_t1_allocate_buffers(
  1783. t1,
  1784. (OPJ_UINT32)(cblk->x1 - cblk->x0),
  1785. (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
  1786. return OPJ_FALSE;
  1787. }
  1788. bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
  1789. if (bpno_plus_one >= 31) {
  1790. if (p_manager_mutex) {
  1791. opj_mutex_lock(p_manager_mutex);
  1792. }
  1793. opj_event_msg(p_manager, EVT_WARNING,
  1794. "opj_t1_decode_cblk(): unsupported bpno_plus_one = %d >= 31\n",
  1795. bpno_plus_one);
  1796. if (p_manager_mutex) {
  1797. opj_mutex_unlock(p_manager_mutex);
  1798. }
  1799. return OPJ_FALSE;
  1800. }
  1801. passtype = 2;
  1802. opj_mqc_resetstates(mqc);
  1803. opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  1804. opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  1805. opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  1806. /* Even if we have a single chunk, in multi-threaded decoding */
  1807. /* the insertion of our synthetic marker might potentially override */
  1808. /* valid codestream of other codeblocks decoded in parallel. */
  1809. if (cblk->numchunks > 1 || t1->mustuse_cblkdatabuffer) {
  1810. OPJ_UINT32 i;
  1811. OPJ_UINT32 cblk_len;
  1812. /* Compute whole codeblock length from chunk lengths */
  1813. cblk_len = 0;
  1814. for (i = 0; i < cblk->numchunks; i++) {
  1815. cblk_len += cblk->chunks[i].len;
  1816. }
  1817. /* Allocate temporary memory if needed */
  1818. if (cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA > t1->cblkdatabuffersize) {
  1819. cblkdata = (OPJ_BYTE*)opj_realloc(t1->cblkdatabuffer,
  1820. cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA);
  1821. if (cblkdata == NULL) {
  1822. return OPJ_FALSE;
  1823. }
  1824. t1->cblkdatabuffer = cblkdata;
  1825. memset(t1->cblkdatabuffer + cblk_len, 0, OPJ_COMMON_CBLK_DATA_EXTRA);
  1826. t1->cblkdatabuffersize = cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA;
  1827. }
  1828. /* Concatenate all chunks */
  1829. cblkdata = t1->cblkdatabuffer;
  1830. cblk_len = 0;
  1831. for (i = 0; i < cblk->numchunks; i++) {
  1832. memcpy(cblkdata + cblk_len, cblk->chunks[i].data, cblk->chunks[i].len);
  1833. cblk_len += cblk->chunks[i].len;
  1834. }
  1835. } else if (cblk->numchunks == 1) {
  1836. cblkdata = cblk->chunks[0].data;
  1837. } else {
  1838. /* Not sure if that can happen in practice, but avoid Coverity to */
  1839. /* think we will dereference a null cblkdta pointer */
  1840. return OPJ_TRUE;
  1841. }
  1842. /* For subtile decoding, directly decode in the decoded_data buffer of */
  1843. /* the code-block. Hack t1->data to point to it, and restore it later */
  1844. if (cblk->decoded_data) {
  1845. original_t1_data = t1->data;
  1846. t1->data = cblk->decoded_data;
  1847. }
  1848. for (segno = 0; segno < cblk->real_num_segs; ++segno) {
  1849. opj_tcd_seg_t *seg = &cblk->segs[segno];
  1850. /* BYPASS mode */
  1851. type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
  1852. (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
  1853. if (type == T1_TYPE_RAW) {
  1854. opj_mqc_raw_init_dec(mqc, cblkdata + cblkdataindex, seg->len,
  1855. OPJ_COMMON_CBLK_DATA_EXTRA);
  1856. } else {
  1857. opj_mqc_init_dec(mqc, cblkdata + cblkdataindex, seg->len,
  1858. OPJ_COMMON_CBLK_DATA_EXTRA);
  1859. }
  1860. cblkdataindex += seg->len;
  1861. for (passno = 0; (passno < seg->real_num_passes) &&
  1862. (bpno_plus_one >= 1); ++passno) {
  1863. switch (passtype) {
  1864. case 0:
  1865. if (type == T1_TYPE_RAW) {
  1866. opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
  1867. } else {
  1868. opj_t1_dec_sigpass_mqc(t1, bpno_plus_one, (OPJ_INT32)cblksty);
  1869. }
  1870. break;
  1871. case 1:
  1872. if (type == T1_TYPE_RAW) {
  1873. opj_t1_dec_refpass_raw(t1, bpno_plus_one);
  1874. } else {
  1875. opj_t1_dec_refpass_mqc(t1, bpno_plus_one);
  1876. }
  1877. break;
  1878. case 2:
  1879. opj_t1_dec_clnpass(t1, bpno_plus_one, (OPJ_INT32)cblksty);
  1880. break;
  1881. }
  1882. if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
  1883. opj_mqc_resetstates(mqc);
  1884. opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  1885. opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  1886. opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  1887. }
  1888. if (++passtype == 3) {
  1889. passtype = 0;
  1890. bpno_plus_one--;
  1891. }
  1892. }
  1893. opq_mqc_finish_dec(mqc);
  1894. }
  1895. if (check_pterm) {
  1896. if (mqc->bp + 2 < mqc->end) {
  1897. if (p_manager_mutex) {
  1898. opj_mutex_lock(p_manager_mutex);
  1899. }
  1900. opj_event_msg(p_manager, EVT_WARNING,
  1901. "PTERM check failure: %d remaining bytes in code block (%d used / %d)\n",
  1902. (int)(mqc->end - mqc->bp) - 2,
  1903. (int)(mqc->bp - mqc->start),
  1904. (int)(mqc->end - mqc->start));
  1905. if (p_manager_mutex) {
  1906. opj_mutex_unlock(p_manager_mutex);
  1907. }
  1908. } else if (mqc->end_of_byte_stream_counter > 2) {
  1909. if (p_manager_mutex) {
  1910. opj_mutex_lock(p_manager_mutex);
  1911. }
  1912. opj_event_msg(p_manager, EVT_WARNING,
  1913. "PTERM check failure: %d synthetized 0xFF markers read\n",
  1914. mqc->end_of_byte_stream_counter);
  1915. if (p_manager_mutex) {
  1916. opj_mutex_unlock(p_manager_mutex);
  1917. }
  1918. }
  1919. }
  1920. /* Restore original t1->data is needed */
  1921. if (cblk->decoded_data) {
  1922. t1->data = original_t1_data;
  1923. }
  1924. return OPJ_TRUE;
  1925. }
  1926. typedef struct {
  1927. OPJ_UINT32 compno;
  1928. OPJ_UINT32 resno;
  1929. opj_tcd_cblk_enc_t* cblk;
  1930. opj_tcd_tile_t *tile;
  1931. opj_tcd_band_t* band;
  1932. opj_tcd_tilecomp_t* tilec;
  1933. opj_tccp_t* tccp;
  1934. const OPJ_FLOAT64 * mct_norms;
  1935. OPJ_UINT32 mct_numcomps;
  1936. volatile OPJ_BOOL* pret;
  1937. opj_mutex_t* mutex;
  1938. } opj_t1_cblk_encode_processing_job_t;
  1939. /** Procedure to deal with a asynchronous code-block encoding job.
  1940. *
  1941. * @param user_data Pointer to a opj_t1_cblk_encode_processing_job_t* structure
  1942. * @param tls TLS handle.
  1943. */
  1944. static void opj_t1_cblk_encode_processor(void* user_data, opj_tls_t* tls)
  1945. {
  1946. opj_t1_cblk_encode_processing_job_t* job =
  1947. (opj_t1_cblk_encode_processing_job_t*)user_data;
  1948. opj_tcd_cblk_enc_t* cblk = job->cblk;
  1949. const opj_tcd_band_t* band = job->band;
  1950. const opj_tcd_tilecomp_t* tilec = job->tilec;
  1951. const opj_tccp_t* tccp = job->tccp;
  1952. const OPJ_UINT32 resno = job->resno;
  1953. opj_t1_t* t1;
  1954. const OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
  1955. OPJ_INT32* OPJ_RESTRICT tiledp;
  1956. OPJ_UINT32 cblk_w;
  1957. OPJ_UINT32 cblk_h;
  1958. OPJ_UINT32 i, j;
  1959. OPJ_INT32 x = cblk->x0 - band->x0;
  1960. OPJ_INT32 y = cblk->y0 - band->y0;
  1961. if (!*(job->pret)) {
  1962. opj_free(job);
  1963. return;
  1964. }
  1965. t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
  1966. if (t1 == NULL) {
  1967. t1 = opj_t1_create(OPJ_TRUE); /* OPJ_TRUE == T1 for encoding */
  1968. opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
  1969. }
  1970. if (band->bandno & 1) {
  1971. opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
  1972. x += pres->x1 - pres->x0;
  1973. }
  1974. if (band->bandno & 2) {
  1975. opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
  1976. y += pres->y1 - pres->y0;
  1977. }
  1978. if (!opj_t1_allocate_buffers(
  1979. t1,
  1980. (OPJ_UINT32)(cblk->x1 - cblk->x0),
  1981. (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
  1982. *(job->pret) = OPJ_FALSE;
  1983. opj_free(job);
  1984. return;
  1985. }
  1986. cblk_w = t1->w;
  1987. cblk_h = t1->h;
  1988. tiledp = &tilec->data[(OPJ_SIZE_T)y * tile_w + (OPJ_SIZE_T)x];
  1989. if (tccp->qmfbid == 1) {
  1990. /* Do multiplication on unsigned type, even if the
  1991. * underlying type is signed, to avoid potential
  1992. * int overflow on large value (the output will be
  1993. * incorrect in such situation, but whatever...)
  1994. * This assumes complement-to-2 signed integer
  1995. * representation
  1996. * Fixes https://github.com/uclouvain/openjpeg/issues/1053
  1997. */
  1998. OPJ_UINT32* OPJ_RESTRICT tiledp_u = (OPJ_UINT32*) tiledp;
  1999. OPJ_UINT32* OPJ_RESTRICT t1data = (OPJ_UINT32*) t1->data;
  2000. /* Change from "natural" order to "zigzag" order of T1 passes */
  2001. for (j = 0; j < (cblk_h & ~3U); j += 4) {
  2002. for (i = 0; i < cblk_w; ++i) {
  2003. t1data[0] = tiledp_u[(j + 0) * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2004. t1data[1] = tiledp_u[(j + 1) * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2005. t1data[2] = tiledp_u[(j + 2) * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2006. t1data[3] = tiledp_u[(j + 3) * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2007. t1data += 4;
  2008. }
  2009. }
  2010. if (j < cblk_h) {
  2011. for (i = 0; i < cblk_w; ++i) {
  2012. OPJ_UINT32 k;
  2013. for (k = j; k < cblk_h; k++) {
  2014. t1data[0] = tiledp_u[k * tile_w + i] << T1_NMSEDEC_FRACBITS;
  2015. t1data ++;
  2016. }
  2017. }
  2018. }
  2019. } else { /* if (tccp->qmfbid == 0) */
  2020. OPJ_FLOAT32* OPJ_RESTRICT tiledp_f = (OPJ_FLOAT32*) tiledp;
  2021. OPJ_INT32* OPJ_RESTRICT t1data = t1->data;
  2022. /* Change from "natural" order to "zigzag" order of T1 passes */
  2023. for (j = 0; j < (cblk_h & ~3U); j += 4) {
  2024. for (i = 0; i < cblk_w; ++i) {
  2025. t1data[0] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 0) * tile_w + i] /
  2026. band->stepsize) * (1 << T1_NMSEDEC_FRACBITS));
  2027. t1data[1] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 1) * tile_w + i] /
  2028. band->stepsize) * (1 << T1_NMSEDEC_FRACBITS));
  2029. t1data[2] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 2) * tile_w + i] /
  2030. band->stepsize) * (1 << T1_NMSEDEC_FRACBITS));
  2031. t1data[3] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 3) * tile_w + i] /
  2032. band->stepsize) * (1 << T1_NMSEDEC_FRACBITS));
  2033. t1data += 4;
  2034. }
  2035. }
  2036. if (j < cblk_h) {
  2037. for (i = 0; i < cblk_w; ++i) {
  2038. OPJ_UINT32 k;
  2039. for (k = j; k < cblk_h; k++) {
  2040. t1data[0] = (OPJ_INT32)opj_lrintf((tiledp_f[k * tile_w + i] / band->stepsize)
  2041. * (1 << T1_NMSEDEC_FRACBITS));
  2042. t1data ++;
  2043. }
  2044. }
  2045. }
  2046. }
  2047. {
  2048. OPJ_FLOAT64 cumwmsedec =
  2049. opj_t1_encode_cblk(
  2050. t1,
  2051. cblk,
  2052. band->bandno,
  2053. job->compno,
  2054. tilec->numresolutions - 1 - resno,
  2055. tccp->qmfbid,
  2056. band->stepsize,
  2057. tccp->cblksty,
  2058. job->tile->numcomps,
  2059. job->mct_norms,
  2060. job->mct_numcomps);
  2061. if (job->mutex) {
  2062. opj_mutex_lock(job->mutex);
  2063. }
  2064. job->tile->distotile += cumwmsedec;
  2065. if (job->mutex) {
  2066. opj_mutex_unlock(job->mutex);
  2067. }
  2068. }
  2069. opj_free(job);
  2070. }
  2071. OPJ_BOOL opj_t1_encode_cblks(opj_tcd_t* tcd,
  2072. opj_tcd_tile_t *tile,
  2073. opj_tcp_t *tcp,
  2074. const OPJ_FLOAT64 * mct_norms,
  2075. OPJ_UINT32 mct_numcomps
  2076. )
  2077. {
  2078. volatile OPJ_BOOL ret = OPJ_TRUE;
  2079. opj_thread_pool_t* tp = tcd->thread_pool;
  2080. OPJ_UINT32 compno, resno, bandno, precno, cblkno;
  2081. opj_mutex_t* mutex = opj_mutex_create();
  2082. tile->distotile = 0; /* fixed_quality */
  2083. for (compno = 0; compno < tile->numcomps; ++compno) {
  2084. opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
  2085. opj_tccp_t* tccp = &tcp->tccps[compno];
  2086. for (resno = 0; resno < tilec->numresolutions; ++resno) {
  2087. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  2088. for (bandno = 0; bandno < res->numbands; ++bandno) {
  2089. opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
  2090. /* Skip empty bands */
  2091. if (opj_tcd_is_band_empty(band)) {
  2092. continue;
  2093. }
  2094. for (precno = 0; precno < res->pw * res->ph; ++precno) {
  2095. opj_tcd_precinct_t *prc = &band->precincts[precno];
  2096. for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
  2097. opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
  2098. opj_t1_cblk_encode_processing_job_t* job =
  2099. (opj_t1_cblk_encode_processing_job_t*) opj_calloc(1,
  2100. sizeof(opj_t1_cblk_encode_processing_job_t));
  2101. if (!job) {
  2102. ret = OPJ_FALSE;
  2103. goto end;
  2104. }
  2105. job->compno = compno;
  2106. job->tile = tile;
  2107. job->resno = resno;
  2108. job->cblk = cblk;
  2109. job->band = band;
  2110. job->tilec = tilec;
  2111. job->tccp = tccp;
  2112. job->mct_norms = mct_norms;
  2113. job->mct_numcomps = mct_numcomps;
  2114. job->pret = &ret;
  2115. job->mutex = mutex;
  2116. opj_thread_pool_submit_job(tp, opj_t1_cblk_encode_processor, job);
  2117. } /* cblkno */
  2118. } /* precno */
  2119. } /* bandno */
  2120. } /* resno */
  2121. } /* compno */
  2122. end:
  2123. opj_thread_pool_wait_completion(tcd->thread_pool, 0);
  2124. if (mutex) {
  2125. opj_mutex_destroy(mutex);
  2126. }
  2127. return ret;
  2128. }
  2129. /* Returns whether the pass (bpno, passtype) is terminated */
  2130. static int opj_t1_enc_is_term_pass(opj_tcd_cblk_enc_t* cblk,
  2131. OPJ_UINT32 cblksty,
  2132. OPJ_INT32 bpno,
  2133. OPJ_UINT32 passtype)
  2134. {
  2135. /* Is it the last cleanup pass ? */
  2136. if (passtype == 2 && bpno == 0) {
  2137. return OPJ_TRUE;
  2138. }
  2139. if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
  2140. return OPJ_TRUE;
  2141. }
  2142. if ((cblksty & J2K_CCP_CBLKSTY_LAZY)) {
  2143. /* For bypass arithmetic bypass, terminate the 4th cleanup pass */
  2144. if ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2)) {
  2145. return OPJ_TRUE;
  2146. }
  2147. /* and beyond terminate all the magnitude refinement passes (in raw) */
  2148. /* and cleanup passes (in MQC) */
  2149. if ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype > 0)) {
  2150. return OPJ_TRUE;
  2151. }
  2152. }
  2153. return OPJ_FALSE;
  2154. }
  2155. /** mod fixed_quality */
  2156. static OPJ_FLOAT64 opj_t1_encode_cblk(opj_t1_t *t1,
  2157. opj_tcd_cblk_enc_t* cblk,
  2158. OPJ_UINT32 orient,
  2159. OPJ_UINT32 compno,
  2160. OPJ_UINT32 level,
  2161. OPJ_UINT32 qmfbid,
  2162. OPJ_FLOAT64 stepsize,
  2163. OPJ_UINT32 cblksty,
  2164. OPJ_UINT32 numcomps,
  2165. const OPJ_FLOAT64 * mct_norms,
  2166. OPJ_UINT32 mct_numcomps)
  2167. {
  2168. OPJ_FLOAT64 cumwmsedec = 0.0;
  2169. opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
  2170. OPJ_UINT32 passno;
  2171. OPJ_INT32 bpno;
  2172. OPJ_UINT32 passtype;
  2173. OPJ_INT32 nmsedec = 0;
  2174. OPJ_INT32 max;
  2175. OPJ_UINT32 i, j;
  2176. OPJ_BYTE type = T1_TYPE_MQ;
  2177. OPJ_FLOAT64 tempwmsedec;
  2178. OPJ_INT32* datap;
  2179. #ifdef EXTRA_DEBUG
  2180. printf("encode_cblk(x=%d,y=%d,x1=%d,y1=%d,orient=%d,compno=%d,level=%d\n",
  2181. cblk->x0, cblk->y0, cblk->x1, cblk->y1, orient, compno, level);
  2182. #endif
  2183. mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
  2184. max = 0;
  2185. datap = t1->data;
  2186. for (j = 0; j < t1->h; ++j) {
  2187. const OPJ_UINT32 w = t1->w;
  2188. for (i = 0; i < w; ++i, ++datap) {
  2189. OPJ_INT32 tmp = *datap;
  2190. if (tmp < 0) {
  2191. OPJ_UINT32 tmp_unsigned;
  2192. max = opj_int_max(max, -tmp);
  2193. tmp_unsigned = opj_to_smr(tmp);
  2194. memcpy(datap, &tmp_unsigned, sizeof(OPJ_INT32));
  2195. } else {
  2196. max = opj_int_max(max, tmp);
  2197. }
  2198. }
  2199. }
  2200. cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
  2201. T1_NMSEDEC_FRACBITS) : 0;
  2202. if (cblk->numbps == 0) {
  2203. cblk->totalpasses = 0;
  2204. return cumwmsedec;
  2205. }
  2206. bpno = (OPJ_INT32)(cblk->numbps - 1);
  2207. passtype = 2;
  2208. opj_mqc_resetstates(mqc);
  2209. opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  2210. opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  2211. opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  2212. opj_mqc_init_enc(mqc, cblk->data);
  2213. for (passno = 0; bpno >= 0; ++passno) {
  2214. opj_tcd_pass_t *pass = &cblk->passes[passno];
  2215. type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
  2216. (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
  2217. /* If the previous pass was terminating, we need to reset the encoder */
  2218. if (passno > 0 && cblk->passes[passno - 1].term) {
  2219. if (type == T1_TYPE_RAW) {
  2220. opj_mqc_bypass_init_enc(mqc);
  2221. } else {
  2222. opj_mqc_restart_init_enc(mqc);
  2223. }
  2224. }
  2225. switch (passtype) {
  2226. case 0:
  2227. opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
  2228. break;
  2229. case 1:
  2230. opj_t1_enc_refpass(t1, bpno, &nmsedec, type);
  2231. break;
  2232. case 2:
  2233. opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
  2234. /* code switch SEGMARK (i.e. SEGSYM) */
  2235. if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
  2236. opj_mqc_segmark_enc(mqc);
  2237. }
  2238. break;
  2239. }
  2240. /* fixed_quality */
  2241. tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
  2242. stepsize, numcomps, mct_norms, mct_numcomps) ;
  2243. cumwmsedec += tempwmsedec;
  2244. pass->distortiondec = cumwmsedec;
  2245. if (opj_t1_enc_is_term_pass(cblk, cblksty, bpno, passtype)) {
  2246. /* If it is a terminated pass, terminate it */
  2247. if (type == T1_TYPE_RAW) {
  2248. opj_mqc_bypass_flush_enc(mqc, cblksty & J2K_CCP_CBLKSTY_PTERM);
  2249. } else {
  2250. if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
  2251. opj_mqc_erterm_enc(mqc);
  2252. } else {
  2253. opj_mqc_flush(mqc);
  2254. }
  2255. }
  2256. pass->term = 1;
  2257. pass->rate = opj_mqc_numbytes(mqc);
  2258. } else {
  2259. /* Non terminated pass */
  2260. OPJ_UINT32 rate_extra_bytes;
  2261. if (type == T1_TYPE_RAW) {
  2262. rate_extra_bytes = opj_mqc_bypass_get_extra_bytes(
  2263. mqc, (cblksty & J2K_CCP_CBLKSTY_PTERM));
  2264. } else {
  2265. rate_extra_bytes = 3;
  2266. }
  2267. pass->term = 0;
  2268. pass->rate = opj_mqc_numbytes(mqc) + rate_extra_bytes;
  2269. }
  2270. if (++passtype == 3) {
  2271. passtype = 0;
  2272. bpno--;
  2273. }
  2274. /* Code-switch "RESET" */
  2275. if (cblksty & J2K_CCP_CBLKSTY_RESET) {
  2276. opj_mqc_reset_enc(mqc);
  2277. }
  2278. }
  2279. cblk->totalpasses = passno;
  2280. if (cblk->totalpasses) {
  2281. /* Make sure that pass rates are increasing */
  2282. OPJ_UINT32 last_pass_rate = opj_mqc_numbytes(mqc);
  2283. for (passno = cblk->totalpasses; passno > 0;) {
  2284. opj_tcd_pass_t *pass = &cblk->passes[--passno];
  2285. if (pass->rate > last_pass_rate) {
  2286. pass->rate = last_pass_rate;
  2287. } else {
  2288. last_pass_rate = pass->rate;
  2289. }
  2290. }
  2291. }
  2292. for (passno = 0; passno < cblk->totalpasses; passno++) {
  2293. opj_tcd_pass_t *pass = &cblk->passes[passno];
  2294. /* Prevent generation of FF as last data byte of a pass*/
  2295. /* For terminating passes, the flushing procedure ensured this already */
  2296. assert(pass->rate > 0);
  2297. if (cblk->data[pass->rate - 1] == 0xFF) {
  2298. pass->rate--;
  2299. }
  2300. pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
  2301. }
  2302. #ifdef EXTRA_DEBUG
  2303. printf(" len=%d\n", (cblk->totalpasses) ? opj_mqc_numbytes(mqc) : 0);
  2304. /* Check that there not 0xff >=0x90 sequences */
  2305. if (cblk->totalpasses) {
  2306. OPJ_UINT32 i;
  2307. OPJ_UINT32 len = opj_mqc_numbytes(mqc);
  2308. for (i = 1; i < len; ++i) {
  2309. if (cblk->data[i - 1] == 0xff && cblk->data[i] >= 0x90) {
  2310. printf("0xff %02x at offset %d\n", cblk->data[i], i - 1);
  2311. abort();
  2312. }
  2313. }
  2314. }
  2315. #endif
  2316. return cumwmsedec;
  2317. }