t1.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  1. /*
  2. * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  3. * Copyright (c) 2002-2007, Professor Benoit Macq
  4. * Copyright (c) 2001-2003, David Janssens
  5. * Copyright (c) 2002-2003, Yannick Verschueren
  6. * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "opj_includes.h"
  33. #include "t1_luts.h"
  34. /** @defgroup T1 T1 - Implementation of the tier-1 coding */
  35. /*@{*/
  36. /** @name Local static functions */
  37. /*@{*/
  38. static INLINE char t1_getctxno_zc(int f, int orient);
  39. static char t1_getctxno_sc(int f);
  40. static INLINE int t1_getctxno_mag(int f);
  41. static char t1_getspb(int f);
  42. static short t1_getnmsedec_sig(int x, int bitpos);
  43. static short t1_getnmsedec_ref(int x, int bitpos);
  44. static void t1_updateflags(flag_t *flagsp, int s, int stride);
  45. /**
  46. Encode significant pass
  47. */
  48. static void t1_enc_sigpass_step(
  49. opj_t1_t *t1,
  50. flag_t *flagsp,
  51. int *datap,
  52. int orient,
  53. int bpno,
  54. int one,
  55. int *nmsedec,
  56. char type,
  57. int vsc);
  58. /**
  59. Decode significant pass
  60. */
  61. static INLINE void t1_dec_sigpass_step_raw(
  62. opj_t1_t *t1,
  63. flag_t *flagsp,
  64. int *datap,
  65. int orient,
  66. int oneplushalf,
  67. int vsc);
  68. static INLINE void t1_dec_sigpass_step_mqc(
  69. opj_t1_t *t1,
  70. flag_t *flagsp,
  71. int *datap,
  72. int orient,
  73. int oneplushalf);
  74. static INLINE void t1_dec_sigpass_step_mqc_vsc(
  75. opj_t1_t *t1,
  76. flag_t *flagsp,
  77. int *datap,
  78. int orient,
  79. int oneplushalf,
  80. int vsc);
  81. /**
  82. Encode significant pass
  83. */
  84. static void t1_enc_sigpass(
  85. opj_t1_t *t1,
  86. int bpno,
  87. int orient,
  88. int *nmsedec,
  89. char type,
  90. int cblksty);
  91. /**
  92. Decode significant pass
  93. */
  94. static void t1_dec_sigpass_raw(
  95. opj_t1_t *t1,
  96. int bpno,
  97. int orient,
  98. int cblksty);
  99. static void t1_dec_sigpass_mqc(
  100. opj_t1_t *t1,
  101. int bpno,
  102. int orient);
  103. static void t1_dec_sigpass_mqc_vsc(
  104. opj_t1_t *t1,
  105. int bpno,
  106. int orient);
  107. /**
  108. Encode refinement pass
  109. */
  110. static void t1_enc_refpass_step(
  111. opj_t1_t *t1,
  112. flag_t *flagsp,
  113. int *datap,
  114. int bpno,
  115. int one,
  116. int *nmsedec,
  117. char type,
  118. int vsc);
  119. /**
  120. Decode refinement pass
  121. */
  122. static INLINE void t1_dec_refpass_step_raw(
  123. opj_t1_t *t1,
  124. flag_t *flagsp,
  125. int *datap,
  126. int poshalf,
  127. int neghalf,
  128. int vsc);
  129. static INLINE void t1_dec_refpass_step_mqc(
  130. opj_t1_t *t1,
  131. flag_t *flagsp,
  132. int *datap,
  133. int poshalf,
  134. int neghalf);
  135. static INLINE void t1_dec_refpass_step_mqc_vsc(
  136. opj_t1_t *t1,
  137. flag_t *flagsp,
  138. int *datap,
  139. int poshalf,
  140. int neghalf,
  141. int vsc);
  142. /**
  143. Encode refinement pass
  144. */
  145. static void t1_enc_refpass(
  146. opj_t1_t *t1,
  147. int bpno,
  148. int *nmsedec,
  149. char type,
  150. int cblksty);
  151. /**
  152. Decode refinement pass
  153. */
  154. static void t1_dec_refpass_raw(
  155. opj_t1_t *t1,
  156. int bpno,
  157. int cblksty);
  158. static void t1_dec_refpass_mqc(
  159. opj_t1_t *t1,
  160. int bpno);
  161. static void t1_dec_refpass_mqc_vsc(
  162. opj_t1_t *t1,
  163. int bpno);
  164. /**
  165. Encode clean-up pass
  166. */
  167. static void t1_enc_clnpass_step(
  168. opj_t1_t *t1,
  169. flag_t *flagsp,
  170. int *datap,
  171. int orient,
  172. int bpno,
  173. int one,
  174. int *nmsedec,
  175. int partial,
  176. int vsc);
  177. /**
  178. Decode clean-up pass
  179. */
  180. static void t1_dec_clnpass_step_partial(
  181. opj_t1_t *t1,
  182. flag_t *flagsp,
  183. int *datap,
  184. int orient,
  185. int oneplushalf);
  186. static void t1_dec_clnpass_step(
  187. opj_t1_t *t1,
  188. flag_t *flagsp,
  189. int *datap,
  190. int orient,
  191. int oneplushalf);
  192. static void t1_dec_clnpass_step_vsc(
  193. opj_t1_t *t1,
  194. flag_t *flagsp,
  195. int *datap,
  196. int orient,
  197. int oneplushalf,
  198. int partial,
  199. int vsc);
  200. /**
  201. Encode clean-up pass
  202. */
  203. static void t1_enc_clnpass(
  204. opj_t1_t *t1,
  205. int bpno,
  206. int orient,
  207. int *nmsedec,
  208. int cblksty);
  209. /**
  210. Decode clean-up pass
  211. */
  212. static void t1_dec_clnpass(
  213. opj_t1_t *t1,
  214. int bpno,
  215. int orient,
  216. int cblksty);
  217. static double t1_getwmsedec(
  218. int nmsedec,
  219. int compno,
  220. int level,
  221. int orient,
  222. int bpno,
  223. int qmfbid,
  224. double stepsize,
  225. int numcomps,
  226. int mct);
  227. /**
  228. Encode 1 code-block
  229. @param t1 T1 handle
  230. @param cblk Code-block coding parameters
  231. @param orient
  232. @param compno Component number
  233. @param level
  234. @param qmfbid
  235. @param stepsize
  236. @param cblksty Code-block style
  237. @param numcomps
  238. @param mct
  239. @param tile
  240. */
  241. static void t1_encode_cblk(
  242. opj_t1_t *t1,
  243. opj_tcd_cblk_enc_t* cblk,
  244. int orient,
  245. int compno,
  246. int level,
  247. int qmfbid,
  248. double stepsize,
  249. int cblksty,
  250. int numcomps,
  251. int mct,
  252. opj_tcd_tile_t * tile);
  253. /**
  254. Decode 1 code-block
  255. @param t1 T1 handle
  256. @param cblk Code-block coding parameters
  257. @param orient
  258. @param roishift Region of interest shifting value
  259. @param cblksty Code-block style
  260. */
  261. static void t1_decode_cblk(
  262. opj_t1_t *t1,
  263. opj_tcd_cblk_dec_t* cblk,
  264. int orient,
  265. int roishift,
  266. int cblksty);
  267. /*@}*/
  268. /*@}*/
  269. /* ----------------------------------------------------------------------- */
  270. static char t1_getctxno_zc(int f, int orient) {
  271. return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
  272. }
  273. static char t1_getctxno_sc(int f) {
  274. return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
  275. }
  276. static int t1_getctxno_mag(int f) {
  277. int tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
  278. int tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
  279. return (tmp2);
  280. }
  281. static char t1_getspb(int f) {
  282. return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
  283. }
  284. static short t1_getnmsedec_sig(int x, int bitpos) {
  285. if (bitpos > T1_NMSEDEC_FRACBITS) {
  286. return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
  287. }
  288. return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
  289. }
  290. static short t1_getnmsedec_ref(int x, int bitpos) {
  291. if (bitpos > T1_NMSEDEC_FRACBITS) {
  292. return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
  293. }
  294. return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
  295. }
  296. static void t1_updateflags(flag_t *flagsp, int s, int stride) {
  297. flag_t *np = flagsp - stride;
  298. flag_t *sp = flagsp + stride;
  299. static const flag_t mod[] = {
  300. T1_SIG_S, T1_SIG_S|T1_SGN_S,
  301. T1_SIG_E, T1_SIG_E|T1_SGN_E,
  302. T1_SIG_W, T1_SIG_W|T1_SGN_W,
  303. T1_SIG_N, T1_SIG_N|T1_SGN_N
  304. };
  305. np[-1] |= T1_SIG_SE;
  306. np[0] |= mod[s];
  307. np[1] |= T1_SIG_SW;
  308. flagsp[-1] |= mod[s+2];
  309. flagsp[0] |= T1_SIG;
  310. flagsp[1] |= mod[s+4];
  311. sp[-1] |= T1_SIG_NE;
  312. sp[0] |= mod[s+6];
  313. sp[1] |= T1_SIG_NW;
  314. }
  315. static void t1_enc_sigpass_step(
  316. opj_t1_t *t1,
  317. flag_t *flagsp,
  318. int *datap,
  319. int orient,
  320. int bpno,
  321. int one,
  322. int *nmsedec,
  323. char type,
  324. int vsc)
  325. {
  326. int v, flag;
  327. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  328. flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
  329. if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
  330. v = int_abs(*datap) & one ? 1 : 0;
  331. mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient)); /* ESSAI */
  332. if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
  333. mqc_bypass_enc(mqc, v);
  334. } else {
  335. mqc_encode(mqc, v);
  336. }
  337. if (v) {
  338. v = *datap < 0 ? 1 : 0;
  339. *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
  340. mqc_setcurctx(mqc, t1_getctxno_sc(flag)); /* ESSAI */
  341. if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
  342. mqc_bypass_enc(mqc, v);
  343. } else {
  344. mqc_encode(mqc, v ^ t1_getspb(flag));
  345. }
  346. t1_updateflags(flagsp, v, t1->flags_stride);
  347. }
  348. *flagsp |= T1_VISIT;
  349. }
  350. }
  351. static INLINE void t1_dec_sigpass_step_raw(
  352. opj_t1_t *t1,
  353. flag_t *flagsp,
  354. int *datap,
  355. int orient,
  356. int oneplushalf,
  357. int vsc)
  358. {
  359. int v, flag;
  360. opj_raw_t *raw = t1->raw; /* RAW component */
  361. OPJ_ARG_NOT_USED(orient);
  362. flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
  363. if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
  364. if (raw_decode(raw)) {
  365. v = raw_decode(raw); /* ESSAI */
  366. *datap = v ? -oneplushalf : oneplushalf;
  367. t1_updateflags(flagsp, v, t1->flags_stride);
  368. }
  369. *flagsp |= T1_VISIT;
  370. }
  371. } /* VSC and BYPASS by Antonin */
  372. static INLINE void t1_dec_sigpass_step_mqc(
  373. opj_t1_t *t1,
  374. flag_t *flagsp,
  375. int *datap,
  376. int orient,
  377. int oneplushalf)
  378. {
  379. int v, flag;
  380. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  381. flag = *flagsp;
  382. if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
  383. mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
  384. if (mqc_decode(mqc)) {
  385. mqc_setcurctx(mqc, t1_getctxno_sc(flag));
  386. v = mqc_decode(mqc) ^ t1_getspb(flag);
  387. *datap = v ? -oneplushalf : oneplushalf;
  388. t1_updateflags(flagsp, v, t1->flags_stride);
  389. }
  390. *flagsp |= T1_VISIT;
  391. }
  392. } /* VSC and BYPASS by Antonin */
  393. static INLINE void t1_dec_sigpass_step_mqc_vsc(
  394. opj_t1_t *t1,
  395. flag_t *flagsp,
  396. int *datap,
  397. int orient,
  398. int oneplushalf,
  399. int vsc)
  400. {
  401. int v, flag;
  402. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  403. flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
  404. if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
  405. mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
  406. if (mqc_decode(mqc)) {
  407. mqc_setcurctx(mqc, t1_getctxno_sc(flag));
  408. v = mqc_decode(mqc) ^ t1_getspb(flag);
  409. *datap = v ? -oneplushalf : oneplushalf;
  410. t1_updateflags(flagsp, v, t1->flags_stride);
  411. }
  412. *flagsp |= T1_VISIT;
  413. }
  414. } /* VSC and BYPASS by Antonin */
  415. static void t1_enc_sigpass(
  416. opj_t1_t *t1,
  417. int bpno,
  418. int orient,
  419. int *nmsedec,
  420. char type,
  421. int cblksty)
  422. {
  423. int i, j, k, one, vsc;
  424. *nmsedec = 0;
  425. one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
  426. for (k = 0; k < t1->h; k += 4) {
  427. for (i = 0; i < t1->w; ++i) {
  428. for (j = k; j < k + 4 && j < t1->h; ++j) {
  429. vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
  430. t1_enc_sigpass_step(
  431. t1,
  432. &t1->flags[((j+1) * t1->flags_stride) + i + 1],
  433. &t1->data[(j * t1->w) + i],
  434. orient,
  435. bpno,
  436. one,
  437. nmsedec,
  438. type,
  439. vsc);
  440. }
  441. }
  442. }
  443. }
  444. static void t1_dec_sigpass_raw(
  445. opj_t1_t *t1,
  446. int bpno,
  447. int orient,
  448. int cblksty)
  449. {
  450. int i, j, k, one, half, oneplushalf, vsc;
  451. one = 1 << bpno;
  452. half = one >> 1;
  453. oneplushalf = one | half;
  454. for (k = 0; k < t1->h; k += 4) {
  455. for (i = 0; i < t1->w; ++i) {
  456. for (j = k; j < k + 4 && j < t1->h; ++j) {
  457. vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
  458. t1_dec_sigpass_step_raw(
  459. t1,
  460. &t1->flags[((j+1) * t1->flags_stride) + i + 1],
  461. &t1->data[(j * t1->w) + i],
  462. orient,
  463. oneplushalf,
  464. vsc);
  465. }
  466. }
  467. }
  468. } /* VSC and BYPASS by Antonin */
  469. static void t1_dec_sigpass_mqc(
  470. opj_t1_t *t1,
  471. int bpno,
  472. int orient)
  473. {
  474. int i, j, k, one, half, oneplushalf;
  475. int *data1 = t1->data;
  476. flag_t *flags1 = &t1->flags[1];
  477. one = 1 << bpno;
  478. half = one >> 1;
  479. oneplushalf = one | half;
  480. for (k = 0; k < (t1->h & ~3); k += 4) {
  481. for (i = 0; i < t1->w; ++i) {
  482. int *data2 = data1 + i;
  483. flag_t *flags2 = flags1 + i;
  484. flags2 += t1->flags_stride;
  485. t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
  486. data2 += t1->w;
  487. flags2 += t1->flags_stride;
  488. t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
  489. data2 += t1->w;
  490. flags2 += t1->flags_stride;
  491. t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
  492. data2 += t1->w;
  493. flags2 += t1->flags_stride;
  494. t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
  495. data2 += t1->w;
  496. }
  497. data1 += t1->w << 2;
  498. flags1 += t1->flags_stride << 2;
  499. }
  500. for (i = 0; i < t1->w; ++i) {
  501. int *data2 = data1 + i;
  502. flag_t *flags2 = flags1 + i;
  503. for (j = k; j < t1->h; ++j) {
  504. flags2 += t1->flags_stride;
  505. t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
  506. data2 += t1->w;
  507. }
  508. }
  509. } /* VSC and BYPASS by Antonin */
  510. static void t1_dec_sigpass_mqc_vsc(
  511. opj_t1_t *t1,
  512. int bpno,
  513. int orient)
  514. {
  515. int i, j, k, one, half, oneplushalf, vsc;
  516. one = 1 << bpno;
  517. half = one >> 1;
  518. oneplushalf = one | half;
  519. for (k = 0; k < t1->h; k += 4) {
  520. for (i = 0; i < t1->w; ++i) {
  521. for (j = k; j < k + 4 && j < t1->h; ++j) {
  522. vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
  523. t1_dec_sigpass_step_mqc_vsc(
  524. t1,
  525. &t1->flags[((j+1) * t1->flags_stride) + i + 1],
  526. &t1->data[(j * t1->w) + i],
  527. orient,
  528. oneplushalf,
  529. vsc);
  530. }
  531. }
  532. }
  533. } /* VSC and BYPASS by Antonin */
  534. static void t1_enc_refpass_step(
  535. opj_t1_t *t1,
  536. flag_t *flagsp,
  537. int *datap,
  538. int bpno,
  539. int one,
  540. int *nmsedec,
  541. char type,
  542. int vsc)
  543. {
  544. int v, flag;
  545. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  546. flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
  547. if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
  548. *nmsedec += t1_getnmsedec_ref(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
  549. v = int_abs(*datap) & one ? 1 : 0;
  550. mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
  551. if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
  552. mqc_bypass_enc(mqc, v);
  553. } else {
  554. mqc_encode(mqc, v);
  555. }
  556. *flagsp |= T1_REFINE;
  557. }
  558. }
  559. static INLINE void t1_dec_refpass_step_raw(
  560. opj_t1_t *t1,
  561. flag_t *flagsp,
  562. int *datap,
  563. int poshalf,
  564. int neghalf,
  565. int vsc)
  566. {
  567. int v, t, flag;
  568. opj_raw_t *raw = t1->raw; /* RAW component */
  569. flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
  570. if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
  571. v = raw_decode(raw);
  572. t = v ? poshalf : neghalf;
  573. *datap += *datap < 0 ? -t : t;
  574. *flagsp |= T1_REFINE;
  575. }
  576. } /* VSC and BYPASS by Antonin */
  577. static INLINE void t1_dec_refpass_step_mqc(
  578. opj_t1_t *t1,
  579. flag_t *flagsp,
  580. int *datap,
  581. int poshalf,
  582. int neghalf)
  583. {
  584. int v, t, flag;
  585. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  586. flag = *flagsp;
  587. if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
  588. mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
  589. v = mqc_decode(mqc);
  590. t = v ? poshalf : neghalf;
  591. *datap += *datap < 0 ? -t : t;
  592. *flagsp |= T1_REFINE;
  593. }
  594. } /* VSC and BYPASS by Antonin */
  595. static INLINE void t1_dec_refpass_step_mqc_vsc(
  596. opj_t1_t *t1,
  597. flag_t *flagsp,
  598. int *datap,
  599. int poshalf,
  600. int neghalf,
  601. int vsc)
  602. {
  603. int v, t, flag;
  604. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  605. flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
  606. if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
  607. mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
  608. v = mqc_decode(mqc);
  609. t = v ? poshalf : neghalf;
  610. *datap += *datap < 0 ? -t : t;
  611. *flagsp |= T1_REFINE;
  612. }
  613. } /* VSC and BYPASS by Antonin */
  614. static void t1_enc_refpass(
  615. opj_t1_t *t1,
  616. int bpno,
  617. int *nmsedec,
  618. char type,
  619. int cblksty)
  620. {
  621. int i, j, k, one, vsc;
  622. *nmsedec = 0;
  623. one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
  624. for (k = 0; k < t1->h; k += 4) {
  625. for (i = 0; i < t1->w; ++i) {
  626. for (j = k; j < k + 4 && j < t1->h; ++j) {
  627. vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
  628. t1_enc_refpass_step(
  629. t1,
  630. &t1->flags[((j+1) * t1->flags_stride) + i + 1],
  631. &t1->data[(j * t1->w) + i],
  632. bpno,
  633. one,
  634. nmsedec,
  635. type,
  636. vsc);
  637. }
  638. }
  639. }
  640. }
  641. static void t1_dec_refpass_raw(
  642. opj_t1_t *t1,
  643. int bpno,
  644. int cblksty)
  645. {
  646. int i, j, k, one, poshalf, neghalf;
  647. int vsc;
  648. one = 1 << bpno;
  649. poshalf = one >> 1;
  650. neghalf = bpno > 0 ? -poshalf : -1;
  651. for (k = 0; k < t1->h; k += 4) {
  652. for (i = 0; i < t1->w; ++i) {
  653. for (j = k; j < k + 4 && j < t1->h; ++j) {
  654. vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
  655. t1_dec_refpass_step_raw(
  656. t1,
  657. &t1->flags[((j+1) * t1->flags_stride) + i + 1],
  658. &t1->data[(j * t1->w) + i],
  659. poshalf,
  660. neghalf,
  661. vsc);
  662. }
  663. }
  664. }
  665. } /* VSC and BYPASS by Antonin */
  666. static void t1_dec_refpass_mqc(
  667. opj_t1_t *t1,
  668. int bpno)
  669. {
  670. int i, j, k, one, poshalf, neghalf;
  671. int *data1 = t1->data;
  672. flag_t *flags1 = &t1->flags[1];
  673. one = 1 << bpno;
  674. poshalf = one >> 1;
  675. neghalf = bpno > 0 ? -poshalf : -1;
  676. for (k = 0; k < (t1->h & ~3); k += 4) {
  677. for (i = 0; i < t1->w; ++i) {
  678. int *data2 = data1 + i;
  679. flag_t *flags2 = flags1 + i;
  680. flags2 += t1->flags_stride;
  681. t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
  682. data2 += t1->w;
  683. flags2 += t1->flags_stride;
  684. t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
  685. data2 += t1->w;
  686. flags2 += t1->flags_stride;
  687. t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
  688. data2 += t1->w;
  689. flags2 += t1->flags_stride;
  690. t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
  691. data2 += t1->w;
  692. }
  693. data1 += t1->w << 2;
  694. flags1 += t1->flags_stride << 2;
  695. }
  696. for (i = 0; i < t1->w; ++i) {
  697. int *data2 = data1 + i;
  698. flag_t *flags2 = flags1 + i;
  699. for (j = k; j < t1->h; ++j) {
  700. flags2 += t1->flags_stride;
  701. t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
  702. data2 += t1->w;
  703. }
  704. }
  705. } /* VSC and BYPASS by Antonin */
  706. static void t1_dec_refpass_mqc_vsc(
  707. opj_t1_t *t1,
  708. int bpno)
  709. {
  710. int i, j, k, one, poshalf, neghalf;
  711. int vsc;
  712. one = 1 << bpno;
  713. poshalf = one >> 1;
  714. neghalf = bpno > 0 ? -poshalf : -1;
  715. for (k = 0; k < t1->h; k += 4) {
  716. for (i = 0; i < t1->w; ++i) {
  717. for (j = k; j < k + 4 && j < t1->h; ++j) {
  718. vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
  719. t1_dec_refpass_step_mqc_vsc(
  720. t1,
  721. &t1->flags[((j+1) * t1->flags_stride) + i + 1],
  722. &t1->data[(j * t1->w) + i],
  723. poshalf,
  724. neghalf,
  725. vsc);
  726. }
  727. }
  728. }
  729. } /* VSC and BYPASS by Antonin */
  730. static void t1_enc_clnpass_step(
  731. opj_t1_t *t1,
  732. flag_t *flagsp,
  733. int *datap,
  734. int orient,
  735. int bpno,
  736. int one,
  737. int *nmsedec,
  738. int partial,
  739. int vsc)
  740. {
  741. int v, flag;
  742. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  743. flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
  744. if (partial) {
  745. goto LABEL_PARTIAL;
  746. }
  747. if (!(*flagsp & (T1_SIG | T1_VISIT))) {
  748. mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
  749. v = int_abs(*datap) & one ? 1 : 0;
  750. mqc_encode(mqc, v);
  751. if (v) {
  752. LABEL_PARTIAL:
  753. *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
  754. mqc_setcurctx(mqc, t1_getctxno_sc(flag));
  755. v = *datap < 0 ? 1 : 0;
  756. mqc_encode(mqc, v ^ t1_getspb(flag));
  757. t1_updateflags(flagsp, v, t1->flags_stride);
  758. }
  759. }
  760. *flagsp &= ~T1_VISIT;
  761. }
  762. static void t1_dec_clnpass_step_partial(
  763. opj_t1_t *t1,
  764. flag_t *flagsp,
  765. int *datap,
  766. int orient,
  767. int oneplushalf)
  768. {
  769. int v, flag;
  770. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  771. OPJ_ARG_NOT_USED(orient);
  772. flag = *flagsp;
  773. mqc_setcurctx(mqc, t1_getctxno_sc(flag));
  774. v = mqc_decode(mqc) ^ t1_getspb(flag);
  775. *datap = v ? -oneplushalf : oneplushalf;
  776. t1_updateflags(flagsp, v, t1->flags_stride);
  777. *flagsp &= ~T1_VISIT;
  778. } /* VSC and BYPASS by Antonin */
  779. static void t1_dec_clnpass_step(
  780. opj_t1_t *t1,
  781. flag_t *flagsp,
  782. int *datap,
  783. int orient,
  784. int oneplushalf)
  785. {
  786. int v, flag;
  787. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  788. flag = *flagsp;
  789. if (!(flag & (T1_SIG | T1_VISIT))) {
  790. mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
  791. if (mqc_decode(mqc)) {
  792. mqc_setcurctx(mqc, t1_getctxno_sc(flag));
  793. v = mqc_decode(mqc) ^ t1_getspb(flag);
  794. *datap = v ? -oneplushalf : oneplushalf;
  795. t1_updateflags(flagsp, v, t1->flags_stride);
  796. }
  797. }
  798. *flagsp &= ~T1_VISIT;
  799. } /* VSC and BYPASS by Antonin */
  800. static void t1_dec_clnpass_step_vsc(
  801. opj_t1_t *t1,
  802. flag_t *flagsp,
  803. int *datap,
  804. int orient,
  805. int oneplushalf,
  806. int partial,
  807. int vsc)
  808. {
  809. int v, flag;
  810. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  811. flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
  812. if (partial) {
  813. goto LABEL_PARTIAL;
  814. }
  815. if (!(flag & (T1_SIG | T1_VISIT))) {
  816. mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
  817. if (mqc_decode(mqc)) {
  818. LABEL_PARTIAL:
  819. mqc_setcurctx(mqc, t1_getctxno_sc(flag));
  820. v = mqc_decode(mqc) ^ t1_getspb(flag);
  821. *datap = v ? -oneplushalf : oneplushalf;
  822. t1_updateflags(flagsp, v, t1->flags_stride);
  823. }
  824. }
  825. *flagsp &= ~T1_VISIT;
  826. }
  827. static void t1_enc_clnpass(
  828. opj_t1_t *t1,
  829. int bpno,
  830. int orient,
  831. int *nmsedec,
  832. int cblksty)
  833. {
  834. int i, j, k, one, agg, runlen, vsc;
  835. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  836. *nmsedec = 0;
  837. one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
  838. for (k = 0; k < t1->h; k += 4) {
  839. for (i = 0; i < t1->w; ++i) {
  840. if (k + 3 < t1->h) {
  841. if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  842. agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  843. || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  844. || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  845. || (MACRO_t1_flags(1 + k + 3,1 + i)
  846. & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
  847. } else {
  848. agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  849. || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  850. || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  851. || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
  852. }
  853. } else {
  854. agg = 0;
  855. }
  856. if (agg) {
  857. for (runlen = 0; runlen < 4; ++runlen) {
  858. if (int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
  859. break;
  860. }
  861. mqc_setcurctx(mqc, T1_CTXNO_AGG);
  862. mqc_encode(mqc, runlen != 4);
  863. if (runlen == 4) {
  864. continue;
  865. }
  866. mqc_setcurctx(mqc, T1_CTXNO_UNI);
  867. mqc_encode(mqc, runlen >> 1);
  868. mqc_encode(mqc, runlen & 1);
  869. } else {
  870. runlen = 0;
  871. }
  872. for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
  873. vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
  874. t1_enc_clnpass_step(
  875. t1,
  876. &t1->flags[((j+1) * t1->flags_stride) + i + 1],
  877. &t1->data[(j * t1->w) + i],
  878. orient,
  879. bpno,
  880. one,
  881. nmsedec,
  882. agg && (j == k + runlen),
  883. vsc);
  884. }
  885. }
  886. }
  887. }
  888. static void t1_dec_clnpass(
  889. opj_t1_t *t1,
  890. int bpno,
  891. int orient,
  892. int cblksty)
  893. {
  894. int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
  895. int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
  896. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  897. one = 1 << bpno;
  898. half = one >> 1;
  899. oneplushalf = one | half;
  900. if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  901. for (k = 0; k < t1->h; k += 4) {
  902. for (i = 0; i < t1->w; ++i) {
  903. if (k + 3 < t1->h) {
  904. agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  905. || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  906. || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  907. || (MACRO_t1_flags(1 + k + 3,1 + i)
  908. & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
  909. } else {
  910. agg = 0;
  911. }
  912. if (agg) {
  913. mqc_setcurctx(mqc, T1_CTXNO_AGG);
  914. if (!mqc_decode(mqc)) {
  915. continue;
  916. }
  917. mqc_setcurctx(mqc, T1_CTXNO_UNI);
  918. runlen = mqc_decode(mqc);
  919. runlen = (runlen << 1) | mqc_decode(mqc);
  920. } else {
  921. runlen = 0;
  922. }
  923. for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
  924. vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
  925. t1_dec_clnpass_step_vsc(
  926. t1,
  927. &t1->flags[((j+1) * t1->flags_stride) + i + 1],
  928. &t1->data[(j * t1->w) + i],
  929. orient,
  930. oneplushalf,
  931. agg && (j == k + runlen),
  932. vsc);
  933. }
  934. }
  935. }
  936. } else {
  937. int *data1 = t1->data;
  938. flag_t *flags1 = &t1->flags[1];
  939. for (k = 0; k < (t1->h & ~3); k += 4) {
  940. for (i = 0; i < t1->w; ++i) {
  941. int *data2 = data1 + i;
  942. flag_t *flags2 = flags1 + i;
  943. agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  944. || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  945. || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  946. || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
  947. if (agg) {
  948. mqc_setcurctx(mqc, T1_CTXNO_AGG);
  949. if (!mqc_decode(mqc)) {
  950. continue;
  951. }
  952. mqc_setcurctx(mqc, T1_CTXNO_UNI);
  953. runlen = mqc_decode(mqc);
  954. runlen = (runlen << 1) | mqc_decode(mqc);
  955. flags2 += runlen * t1->flags_stride;
  956. data2 += runlen * t1->w;
  957. for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
  958. flags2 += t1->flags_stride;
  959. if (agg && (j == k + runlen)) {
  960. t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
  961. } else {
  962. t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
  963. }
  964. data2 += t1->w;
  965. }
  966. } else {
  967. flags2 += t1->flags_stride;
  968. t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
  969. data2 += t1->w;
  970. flags2 += t1->flags_stride;
  971. t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
  972. data2 += t1->w;
  973. flags2 += t1->flags_stride;
  974. t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
  975. data2 += t1->w;
  976. flags2 += t1->flags_stride;
  977. t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
  978. data2 += t1->w;
  979. }
  980. }
  981. data1 += t1->w << 2;
  982. flags1 += t1->flags_stride << 2;
  983. }
  984. for (i = 0; i < t1->w; ++i) {
  985. int *data2 = data1 + i;
  986. flag_t *flags2 = flags1 + i;
  987. for (j = k; j < t1->h; ++j) {
  988. flags2 += t1->flags_stride;
  989. t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
  990. data2 += t1->w;
  991. }
  992. }
  993. }
  994. if (segsym) {
  995. int v = 0;
  996. mqc_setcurctx(mqc, T1_CTXNO_UNI);
  997. v = mqc_decode(mqc);
  998. v = (v << 1) | mqc_decode(mqc);
  999. v = (v << 1) | mqc_decode(mqc);
  1000. v = (v << 1) | mqc_decode(mqc);
  1001. /*
  1002. if (v!=0xa) {
  1003. opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
  1004. }
  1005. */
  1006. }
  1007. } /* VSC and BYPASS by Antonin */
  1008. /** mod fixed_quality */
  1009. static double t1_getwmsedec(
  1010. int nmsedec,
  1011. int compno,
  1012. int level,
  1013. int orient,
  1014. int bpno,
  1015. int qmfbid,
  1016. double stepsize,
  1017. int numcomps,
  1018. int mct)
  1019. {
  1020. double w1, w2, wmsedec;
  1021. if (qmfbid == 1) {
  1022. w1 = (mct && numcomps==3) ? mct_getnorm(compno) : 1.0;
  1023. w2 = dwt_getnorm(level, orient);
  1024. } else { /* if (qmfbid == 0) */
  1025. w1 = (mct && numcomps==3) ? mct_getnorm_real(compno) : 1.0;
  1026. w2 = dwt_getnorm_real(level, orient);
  1027. }
  1028. wmsedec = w1 * w2 * stepsize * (1 << bpno);
  1029. wmsedec *= wmsedec * nmsedec / 8192.0;
  1030. return wmsedec;
  1031. }
  1032. static opj_bool allocate_buffers(
  1033. opj_t1_t *t1,
  1034. int w,
  1035. int h)
  1036. {
  1037. int datasize=w * h;
  1038. int flagssize;
  1039. if(datasize > t1->datasize){
  1040. opj_aligned_free(t1->data);
  1041. t1->data = (int*) opj_aligned_malloc(datasize * sizeof(int));
  1042. if(!t1->data){
  1043. return OPJ_FALSE;
  1044. }
  1045. t1->datasize=datasize;
  1046. }
  1047. memset(t1->data,0,datasize * sizeof(int));
  1048. t1->flags_stride=w+2;
  1049. flagssize=t1->flags_stride * (h+2);
  1050. if(flagssize > t1->flagssize){
  1051. opj_aligned_free(t1->flags);
  1052. t1->flags = (flag_t*) opj_aligned_malloc(flagssize * sizeof(flag_t));
  1053. if(!t1->flags){
  1054. return OPJ_FALSE;
  1055. }
  1056. t1->flagssize=flagssize;
  1057. }
  1058. memset(t1->flags,0,flagssize * sizeof(flag_t));
  1059. t1->w=w;
  1060. t1->h=h;
  1061. return OPJ_TRUE;
  1062. }
  1063. /** mod fixed_quality */
  1064. static void t1_encode_cblk(
  1065. opj_t1_t *t1,
  1066. opj_tcd_cblk_enc_t* cblk,
  1067. int orient,
  1068. int compno,
  1069. int level,
  1070. int qmfbid,
  1071. double stepsize,
  1072. int cblksty,
  1073. int numcomps,
  1074. int mct,
  1075. opj_tcd_tile_t * tile)
  1076. {
  1077. double cumwmsedec = 0.0;
  1078. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  1079. int passno, bpno, passtype;
  1080. int nmsedec = 0;
  1081. int i, max;
  1082. char type = T1_TYPE_MQ;
  1083. double tempwmsedec;
  1084. max = 0;
  1085. for (i = 0; i < t1->w * t1->h; ++i) {
  1086. int tmp = abs(t1->data[i]);
  1087. max = int_max(max, tmp);
  1088. }
  1089. cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
  1090. bpno = cblk->numbps - 1;
  1091. passtype = 2;
  1092. mqc_resetstates(mqc);
  1093. mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  1094. mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  1095. mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  1096. mqc_init_enc(mqc, cblk->data);
  1097. for (passno = 0; bpno >= 0; ++passno) {
  1098. opj_tcd_pass_t *pass = &cblk->passes[passno];
  1099. int correction = 3;
  1100. type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
  1101. switch (passtype) {
  1102. case 0:
  1103. t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
  1104. break;
  1105. case 1:
  1106. t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
  1107. break;
  1108. case 2:
  1109. t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
  1110. /* code switch SEGMARK (i.e. SEGSYM) */
  1111. if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
  1112. mqc_segmark_enc(mqc);
  1113. break;
  1114. }
  1115. /* fixed_quality */
  1116. tempwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps, mct);
  1117. cumwmsedec += tempwmsedec;
  1118. tile->distotile += tempwmsedec;
  1119. /* Code switch "RESTART" (i.e. TERMALL) */
  1120. if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
  1121. if (type == T1_TYPE_RAW) {
  1122. mqc_flush(mqc);
  1123. correction = 1;
  1124. /* correction = mqc_bypass_flush_enc(); */
  1125. } else { /* correction = mqc_restart_enc(); */
  1126. mqc_flush(mqc);
  1127. correction = 1;
  1128. }
  1129. pass->term = 1;
  1130. } else {
  1131. if (((bpno < (cblk->numbps - 4) && (passtype > 0))
  1132. || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
  1133. if (type == T1_TYPE_RAW) {
  1134. mqc_flush(mqc);
  1135. correction = 1;
  1136. /* correction = mqc_bypass_flush_enc(); */
  1137. } else { /* correction = mqc_restart_enc(); */
  1138. mqc_flush(mqc);
  1139. correction = 1;
  1140. }
  1141. pass->term = 1;
  1142. } else {
  1143. pass->term = 0;
  1144. }
  1145. }
  1146. if (++passtype == 3) {
  1147. passtype = 0;
  1148. bpno--;
  1149. }
  1150. if (pass->term && bpno > 0) {
  1151. type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
  1152. if (type == T1_TYPE_RAW)
  1153. mqc_bypass_init_enc(mqc);
  1154. else
  1155. mqc_restart_init_enc(mqc);
  1156. }
  1157. pass->distortiondec = cumwmsedec;
  1158. pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
  1159. /* Code-switch "RESET" */
  1160. if (cblksty & J2K_CCP_CBLKSTY_RESET)
  1161. mqc_reset_enc(mqc);
  1162. }
  1163. /* Code switch "ERTERM" (i.e. PTERM) */
  1164. if (cblksty & J2K_CCP_CBLKSTY_PTERM)
  1165. mqc_erterm_enc(mqc);
  1166. else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
  1167. mqc_flush(mqc);
  1168. cblk->totalpasses = passno;
  1169. for (passno = 0; passno<cblk->totalpasses; passno++) {
  1170. opj_tcd_pass_t *pass = &cblk->passes[passno];
  1171. if (pass->rate > mqc_numbytes(mqc))
  1172. pass->rate = mqc_numbytes(mqc);
  1173. /*Preventing generation of FF as last data byte of a pass*/
  1174. if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
  1175. pass->rate--;
  1176. }
  1177. pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
  1178. }
  1179. }
  1180. static void t1_decode_cblk(
  1181. opj_t1_t *t1,
  1182. opj_tcd_cblk_dec_t* cblk,
  1183. int orient,
  1184. int roishift,
  1185. int cblksty)
  1186. {
  1187. opj_raw_t *raw = t1->raw; /* RAW component */
  1188. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  1189. int bpno, passtype;
  1190. int segno, passno;
  1191. char type = T1_TYPE_MQ; /* BYPASS mode */
  1192. if(!allocate_buffers(
  1193. t1,
  1194. cblk->x1 - cblk->x0,
  1195. cblk->y1 - cblk->y0))
  1196. {
  1197. return;
  1198. }
  1199. bpno = roishift + cblk->numbps - 1;
  1200. passtype = 2;
  1201. mqc_resetstates(mqc);
  1202. mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  1203. mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  1204. mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  1205. for (segno = 0; segno < cblk->numsegs; ++segno) {
  1206. opj_tcd_seg_t *seg = &cblk->segs[segno];
  1207. /* BYPASS mode */
  1208. type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
  1209. /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
  1210. if(seg->data == NULL){
  1211. continue;
  1212. }
  1213. if (type == T1_TYPE_RAW) {
  1214. raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
  1215. } else {
  1216. mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len);
  1217. }
  1218. for (passno = 0; passno < seg->numpasses; ++passno) {
  1219. switch (passtype) {
  1220. case 0:
  1221. if (type == T1_TYPE_RAW) {
  1222. t1_dec_sigpass_raw(t1, bpno+1, orient, cblksty);
  1223. } else {
  1224. if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  1225. t1_dec_sigpass_mqc_vsc(t1, bpno+1, orient);
  1226. } else {
  1227. t1_dec_sigpass_mqc(t1, bpno+1, orient);
  1228. }
  1229. }
  1230. break;
  1231. case 1:
  1232. if (type == T1_TYPE_RAW) {
  1233. t1_dec_refpass_raw(t1, bpno+1, cblksty);
  1234. } else {
  1235. if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  1236. t1_dec_refpass_mqc_vsc(t1, bpno+1);
  1237. } else {
  1238. t1_dec_refpass_mqc(t1, bpno+1);
  1239. }
  1240. }
  1241. break;
  1242. case 2:
  1243. t1_dec_clnpass(t1, bpno+1, orient, cblksty);
  1244. break;
  1245. }
  1246. if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
  1247. mqc_resetstates(mqc);
  1248. mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  1249. mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  1250. mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  1251. }
  1252. if (++passtype == 3) {
  1253. passtype = 0;
  1254. bpno--;
  1255. }
  1256. }
  1257. }
  1258. }
  1259. /* ----------------------------------------------------------------------- */
  1260. opj_t1_t* t1_create(opj_common_ptr cinfo) {
  1261. opj_t1_t *t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
  1262. if(!t1)
  1263. return NULL;
  1264. t1->cinfo = cinfo;
  1265. /* create MQC and RAW handles */
  1266. t1->mqc = mqc_create();
  1267. t1->raw = raw_create();
  1268. t1->data=NULL;
  1269. t1->flags=NULL;
  1270. t1->datasize=0;
  1271. t1->flagssize=0;
  1272. return t1;
  1273. }
  1274. void t1_destroy(opj_t1_t *t1) {
  1275. if(t1) {
  1276. /* destroy MQC and RAW handles */
  1277. mqc_destroy(t1->mqc);
  1278. raw_destroy(t1->raw);
  1279. opj_aligned_free(t1->data);
  1280. opj_aligned_free(t1->flags);
  1281. opj_free(t1);
  1282. }
  1283. }
  1284. void t1_encode_cblks(
  1285. opj_t1_t *t1,
  1286. opj_tcd_tile_t *tile,
  1287. opj_tcp_t *tcp)
  1288. {
  1289. int compno, resno, bandno, precno, cblkno;
  1290. tile->distotile = 0; /* fixed_quality */
  1291. for (compno = 0; compno < tile->numcomps; ++compno) {
  1292. opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
  1293. opj_tccp_t* tccp = &tcp->tccps[compno];
  1294. int tile_w = tilec->x1 - tilec->x0;
  1295. for (resno = 0; resno < tilec->numresolutions; ++resno) {
  1296. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  1297. for (bandno = 0; bandno < res->numbands; ++bandno) {
  1298. opj_tcd_band_t* restrict band = &res->bands[bandno];
  1299. int bandconst = 8192 * 8192 / ((int) floor(band->stepsize * 8192));
  1300. for (precno = 0; precno < res->pw * res->ph; ++precno) {
  1301. opj_tcd_precinct_t *prc = &band->precincts[precno];
  1302. for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
  1303. opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
  1304. int* restrict datap;
  1305. int* restrict tiledp;
  1306. int cblk_w;
  1307. int cblk_h;
  1308. int i, j;
  1309. int x = cblk->x0 - band->x0;
  1310. int y = cblk->y0 - band->y0;
  1311. if (band->bandno & 1) {
  1312. opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
  1313. x += pres->x1 - pres->x0;
  1314. }
  1315. if (band->bandno & 2) {
  1316. opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
  1317. y += pres->y1 - pres->y0;
  1318. }
  1319. if(!allocate_buffers(
  1320. t1,
  1321. cblk->x1 - cblk->x0,
  1322. cblk->y1 - cblk->y0))
  1323. {
  1324. return;
  1325. }
  1326. datap=t1->data;
  1327. cblk_w = t1->w;
  1328. cblk_h = t1->h;
  1329. tiledp=&tilec->data[(y * tile_w) + x];
  1330. if (tccp->qmfbid == 1) {
  1331. for (j = 0; j < cblk_h; ++j) {
  1332. for (i = 0; i < cblk_w; ++i) {
  1333. int tmp = tiledp[(j * tile_w) + i];
  1334. datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
  1335. }
  1336. }
  1337. } else { /* if (tccp->qmfbid == 0) */
  1338. for (j = 0; j < cblk_h; ++j) {
  1339. for (i = 0; i < cblk_w; ++i) {
  1340. int tmp = tiledp[(j * tile_w) + i];
  1341. datap[(j * cblk_w) + i] =
  1342. fix_mul(
  1343. tmp,
  1344. bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
  1345. }
  1346. }
  1347. }
  1348. t1_encode_cblk(
  1349. t1,
  1350. cblk,
  1351. band->bandno,
  1352. compno,
  1353. tilec->numresolutions - 1 - resno,
  1354. tccp->qmfbid,
  1355. band->stepsize,
  1356. tccp->cblksty,
  1357. tile->numcomps,
  1358. tcp->mct,
  1359. tile);
  1360. } /* cblkno */
  1361. } /* precno */
  1362. } /* bandno */
  1363. } /* resno */
  1364. } /* compno */
  1365. }
  1366. void t1_decode_cblks(
  1367. opj_t1_t* t1,
  1368. opj_tcd_tilecomp_t* tilec,
  1369. opj_tccp_t* tccp)
  1370. {
  1371. int resno, bandno, precno, cblkno;
  1372. int tile_w = tilec->x1 - tilec->x0;
  1373. for (resno = 0; resno < tilec->numresolutions; ++resno) {
  1374. opj_tcd_resolution_t* res = &tilec->resolutions[resno];
  1375. for (bandno = 0; bandno < res->numbands; ++bandno) {
  1376. opj_tcd_band_t* restrict band = &res->bands[bandno];
  1377. for (precno = 0; precno < res->pw * res->ph; ++precno) {
  1378. opj_tcd_precinct_t* precinct = &band->precincts[precno];
  1379. for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
  1380. opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
  1381. int* restrict datap;
  1382. int cblk_w, cblk_h;
  1383. int x, y;
  1384. int i, j;
  1385. t1_decode_cblk(
  1386. t1,
  1387. cblk,
  1388. band->bandno,
  1389. tccp->roishift,
  1390. tccp->cblksty);
  1391. x = cblk->x0 - band->x0;
  1392. y = cblk->y0 - band->y0;
  1393. if (band->bandno & 1) {
  1394. opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
  1395. x += pres->x1 - pres->x0;
  1396. }
  1397. if (band->bandno & 2) {
  1398. opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
  1399. y += pres->y1 - pres->y0;
  1400. }
  1401. datap=t1->data;
  1402. cblk_w = t1->w;
  1403. cblk_h = t1->h;
  1404. if (tccp->roishift) {
  1405. int thresh = 1 << tccp->roishift;
  1406. for (j = 0; j < cblk_h; ++j) {
  1407. for (i = 0; i < cblk_w; ++i) {
  1408. int val = datap[(j * cblk_w) + i];
  1409. int mag = abs(val);
  1410. if (mag >= thresh) {
  1411. mag >>= tccp->roishift;
  1412. datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
  1413. }
  1414. }
  1415. }
  1416. }
  1417. if (tccp->qmfbid == 1) {
  1418. int* restrict tiledp = &tilec->data[(y * tile_w) + x];
  1419. for (j = 0; j < cblk_h; ++j) {
  1420. for (i = 0; i < cblk_w; ++i) {
  1421. int tmp = datap[(j * cblk_w) + i];
  1422. ((int*)tiledp)[(j * tile_w) + i] = tmp / 2;
  1423. }
  1424. }
  1425. } else { /* if (tccp->qmfbid == 0) */
  1426. float* restrict tiledp = (float*) &tilec->data[(y * tile_w) + x];
  1427. for (j = 0; j < cblk_h; ++j) {
  1428. float* restrict tiledp2 = tiledp;
  1429. for (i = 0; i < cblk_w; ++i) {
  1430. float tmp = *datap * band->stepsize;
  1431. *tiledp2 = tmp;
  1432. datap++;
  1433. tiledp2++;
  1434. }
  1435. tiledp += tile_w;
  1436. }
  1437. }
  1438. opj_free(cblk->data);
  1439. opj_free(cblk->segs);
  1440. } /* cblkno */
  1441. opj_free(precinct->cblks.dec);
  1442. precinct->cblks.dec = NULL;
  1443. } /* precno */
  1444. } /* bandno */
  1445. } /* resno */
  1446. }