t1.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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 <[email protected]>
  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(opj_t1_t* t1, flag_t* flagsp, int* datap, int orient,
  49. int bpno, int one, int* nmsedec, char type, int vsc);
  50. /**
  51. Decode significant pass
  52. */
  53. static INLINE void t1_dec_sigpass_step_raw(opj_t1_t* t1, flag_t* flagsp, int* datap,
  54. int orient, int oneplushalf, int vsc);
  55. static INLINE void t1_dec_sigpass_step_mqc(opj_t1_t* t1, flag_t* flagsp, int* datap,
  56. int orient, int oneplushalf);
  57. static INLINE void t1_dec_sigpass_step_mqc_vsc(opj_t1_t* t1, flag_t* flagsp, int* datap,
  58. int orient, int oneplushalf, int vsc);
  59. /**
  60. Encode significant pass
  61. */
  62. static void t1_enc_sigpass(opj_t1_t* t1, int bpno, int orient, int* nmsedec,
  63. char type, int cblksty);
  64. /**
  65. Decode significant pass
  66. */
  67. static void t1_dec_sigpass_raw(opj_t1_t* t1, int bpno, int orient, int cblksty);
  68. static void t1_dec_sigpass_mqc(opj_t1_t* t1, int bpno, int orient);
  69. static void t1_dec_sigpass_mqc_vsc(opj_t1_t* t1, int bpno, int orient);
  70. /**
  71. Encode refinement pass
  72. */
  73. static void t1_enc_refpass_step(opj_t1_t* t1, flag_t* flagsp, int* datap, int bpno,
  74. int one, int* nmsedec, char type, int vsc);
  75. /**
  76. Decode refinement pass
  77. */
  78. static INLINE void t1_dec_refpass_step_raw(opj_t1_t* t1, flag_t* flagsp, int* datap,
  79. int poshalf, int neghalf, int vsc);
  80. static INLINE void t1_dec_refpass_step_mqc(opj_t1_t* t1, flag_t* flagsp, int* datap,
  81. int poshalf, int neghalf);
  82. static INLINE void t1_dec_refpass_step_mqc_vsc(opj_t1_t* t1, flag_t* flagsp, int* datap,
  83. int poshalf, int neghalf, int vsc);
  84. /**
  85. Encode refinement pass
  86. */
  87. static void t1_enc_refpass(opj_t1_t* t1, int bpno, int* nmsedec, char type, int cblksty);
  88. /**
  89. Decode refinement pass
  90. */
  91. static void t1_dec_refpass_raw(opj_t1_t* t1, int bpno, int cblksty);
  92. static void t1_dec_refpass_mqc(opj_t1_t* t1, int bpno);
  93. static void t1_dec_refpass_mqc_vsc(opj_t1_t* t1, int bpno);
  94. /**
  95. Encode clean-up pass
  96. */
  97. static void t1_enc_clnpass_step(opj_t1_t* t1, flag_t* flagsp, int* datap, int orient,
  98. int bpno, int one, int* nmsedec, int partial, int vsc);
  99. /**
  100. Decode clean-up pass
  101. */
  102. static void t1_dec_clnpass_step_partial(opj_t1_t* t1, flag_t* flagsp, int* datap,
  103. int orient, int oneplushalf);
  104. static void t1_dec_clnpass_step(opj_t1_t *t1, flag_t* flagsp, int* datap,
  105. int orient, int oneplushalf);
  106. static void t1_dec_clnpass_step_vsc(opj_t1_t* t1, flag_t* flagsp, int* datap,
  107. int orient, int oneplushalf, int partial, int vsc);
  108. /**
  109. Encode clean-up pass
  110. */
  111. static void t1_enc_clnpass(opj_t1_t* t1, int bpno, int orient, int* nmsedec, int cblksty);
  112. /**
  113. Decode clean-up pass
  114. */
  115. static void t1_dec_clnpass(opj_t1_t* t1, int bpno, int orient, int cblksty);
  116. static double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno,
  117. int qmfbid, double stepsize, int numcomps, int mct);
  118. /**
  119. Encode 1 code-block
  120. @param t1 T1 handle
  121. @param cblk Code-block coding parameters
  122. @param orient
  123. @param compno Component number
  124. @param level
  125. @param qmfbid
  126. @param stepsize
  127. @param cblksty Code-block style
  128. @param numcomps
  129. @param tile
  130. */
  131. static void t1_encode_cblk(opj_t1_t* t1, opj_tcd_cblk_enc_t* cblk, int orient,
  132. int compno, int level, int qmfbid, double stepsize,
  133. int cblksty, int numcomps, int mct, opj_tcd_tile_t* tile);
  134. /**
  135. Decode 1 code-block
  136. @param t1 T1 handle
  137. @param cblk Code-block coding parameters
  138. @param orient
  139. @param roishift Region of interest shifting value
  140. @param cblksty Code-block style
  141. */
  142. static void t1_decode_cblk(opj_t1_t* t1, opj_tcd_cblk_dec_t* cblk,
  143. int orient, int roishift, int cblksty);
  144. /*@}*/
  145. /*@}*/
  146. /* ----------------------------------------------------------------------- */
  147. static char t1_getctxno_zc(int f, int orient) {
  148. return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
  149. }
  150. static char t1_getctxno_sc(int f) {
  151. return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
  152. }
  153. static int t1_getctxno_mag(int f) {
  154. int tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
  155. int tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
  156. return (tmp2);
  157. }
  158. static char t1_getspb(int f) {
  159. return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
  160. }
  161. static short t1_getnmsedec_sig(int x, int bitpos) {
  162. if (bitpos > T1_NMSEDEC_FRACBITS) {
  163. return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
  164. }
  165. return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
  166. }
  167. static short t1_getnmsedec_ref(int x, int bitpos) {
  168. if (bitpos > T1_NMSEDEC_FRACBITS) {
  169. return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
  170. }
  171. return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
  172. }
  173. static void t1_updateflags(flag_t* flagsp, int s, int stride) {
  174. flag_t* np = flagsp - stride;
  175. flag_t* sp = flagsp + stride;
  176. static const flag_t mod[] = {
  177. T1_SIG_S, T1_SIG_S|T1_SGN_S,
  178. T1_SIG_E, T1_SIG_E|T1_SGN_E,
  179. T1_SIG_W, T1_SIG_W|T1_SGN_W,
  180. T1_SIG_N, T1_SIG_N|T1_SGN_N
  181. };
  182. np[-1] |= T1_SIG_SE;
  183. np[0] |= mod[s];
  184. np[1] |= T1_SIG_SW;
  185. flagsp[-1] |= mod[s + 2];
  186. flagsp[0] |= T1_SIG;
  187. flagsp[1] |= mod[s + 4];
  188. sp[-1] |= T1_SIG_NE;
  189. sp[0] |= mod[s + 6];
  190. sp[1] |= T1_SIG_NW;
  191. }
  192. static void t1_enc_sigpass_step(opj_t1_t* t1, flag_t* flagsp, int* datap, int orient,
  193. int bpno, int one, int* nmsedec, char type, int vsc)
  194. {
  195. opj_mqc_t* mqc = t1->mqc; /* MQC component */
  196. int flag = vsc ? *flagsp & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)) : *flagsp;
  197. if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
  198. int v = int_abs(*datap) & one ? 1 : 0;
  199. mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient)); /* ESSAI */
  200. if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
  201. mqc_bypass_enc(mqc, v);
  202. } else {
  203. mqc_encode(mqc, v);
  204. }
  205. if (v) {
  206. v = *datap < 0 ? 1 : 0;
  207. *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
  208. mqc_setcurctx(mqc, t1_getctxno_sc(flag)); /* ESSAI */
  209. if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
  210. mqc_bypass_enc(mqc, v);
  211. } else {
  212. mqc_encode(mqc, v ^ t1_getspb(flag));
  213. }
  214. t1_updateflags(flagsp, v, t1->flags_stride);
  215. }
  216. *flagsp |= T1_VISIT;
  217. }
  218. }
  219. static INLINE void t1_dec_sigpass_step_raw(opj_t1_t* t1, flag_t* flagsp, int* datap,
  220. int orient, int oneplushalf, int vsc)
  221. {
  222. opj_raw_t* raw = t1->raw; /* RAW component */
  223. int flag = vsc ? *flagsp & ~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S) : *flagsp;
  224. if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
  225. if (raw_decode(raw)) {
  226. int v = raw_decode(raw); /* ESSAI */
  227. *datap = v ? -oneplushalf : oneplushalf;
  228. t1_updateflags(flagsp, v, t1->flags_stride);
  229. }
  230. *flagsp |= T1_VISIT;
  231. }
  232. } /* VSC and BYPASS by Antonin */
  233. static INLINE void t1_dec_sigpass_step_mqc(opj_t1_t* t1, flag_t* flagsp, int* datap,
  234. int orient, int oneplushalf)
  235. {
  236. opj_mqc_t* mqc = t1->mqc; /* MQC component */
  237. int flag = *flagsp;
  238. if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
  239. mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
  240. if (mqc_decode(mqc)) {
  241. mqc_setcurctx(mqc, t1_getctxno_sc(flag));
  242. int v = mqc_decode(mqc) ^ t1_getspb(flag);
  243. *datap = v ? -oneplushalf : oneplushalf;
  244. t1_updateflags(flagsp, v, t1->flags_stride);
  245. }
  246. *flagsp |= T1_VISIT;
  247. }
  248. } /* VSC and BYPASS by Antonin */
  249. static INLINE void t1_dec_sigpass_step_mqc_vsc(opj_t1_t* t1, flag_t* flagsp, int* datap,
  250. int orient, int oneplushalf, int vsc)
  251. {
  252. opj_mqc_t* mqc = t1->mqc; /* MQC component */
  253. int flag = vsc ? *flagsp & ~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S) : *flagsp;
  254. if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
  255. mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
  256. if (mqc_decode(mqc)) {
  257. mqc_setcurctx(mqc, t1_getctxno_sc(flag));
  258. int v = mqc_decode(mqc) ^ t1_getspb(flag);
  259. *datap = v ? -oneplushalf : oneplushalf;
  260. t1_updateflags(flagsp, v, t1->flags_stride);
  261. }
  262. *flagsp |= T1_VISIT;
  263. }
  264. } /* VSC and BYPASS by Antonin */
  265. static void t1_enc_sigpass(opj_t1_t* t1, int bpno, int orient, int* nmsedec, char type,
  266. int cblksty)
  267. {
  268. int i, j, k, vsc;
  269. *nmsedec = 0;
  270. int one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
  271. const int height = t1->h;
  272. const int width = t1->w;
  273. for (k = 0; k < height; k += 4) {
  274. for (i = 0; i < width; ++i) {
  275. int jw = k * width; /* j * width */
  276. for (j = k; j < k + 4 && j < height; ++j) {
  277. vsc = (cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == height - 1) ? 1 : 0;
  278. t1_enc_sigpass_step(t1, &t1->flags[(j + 1) * t1->flags_stride + i + 1],
  279. &t1->data[jw + i], orient, bpno, one, nmsedec, type, vsc);
  280. jw += width;
  281. }
  282. }
  283. }
  284. }
  285. static void t1_dec_sigpass_raw(opj_t1_t* t1, int bpno, int orient, int cblksty)
  286. {
  287. int i, j, k, vsc;
  288. int one = 1 << bpno;
  289. int half = one >> 1;
  290. int oneplushalf = one | half;
  291. const int height = t1->h;
  292. const int width = t1->w;
  293. for (k = 0; k < height; k += 4) {
  294. for (i = 0; i < width; ++i) {
  295. int jw = k * width; /* j * width */
  296. for (j = k; j < k + 4 && j < height; ++j) {
  297. vsc = (cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == height - 1) ? 1 : 0;
  298. t1_dec_sigpass_step_raw(t1, &t1->flags[((j+1) * t1->flags_stride) + i + 1],
  299. &t1->data[jw + i], orient, oneplushalf, vsc);
  300. jw += width;
  301. }
  302. }
  303. }
  304. } /* VSC and BYPASS by Antonin */
  305. static void t1_dec_sigpass_mqc(opj_t1_t* t1, int bpno, int orient)
  306. {
  307. int i, j, k;
  308. int* data1 = t1->data;
  309. flag_t* flags1 = &t1->flags[1];
  310. int one = 1 << bpno;
  311. int half = one >> 1;
  312. int oneplushalf = one | half;
  313. const int cnt = t1->h & ~3;
  314. const int width = t1->w;
  315. for (k = 0; k < cnt; k += 4) {
  316. for (i = 0; i < width; ++i) {
  317. int* data2 = data1 + i;
  318. flag_t* flags2 = flags1 + i;
  319. flags2 += t1->flags_stride;
  320. t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
  321. data2 += width;
  322. flags2 += t1->flags_stride;
  323. t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
  324. data2 += width;
  325. flags2 += t1->flags_stride;
  326. t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
  327. data2 += width;
  328. flags2 += t1->flags_stride;
  329. t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
  330. data2 += width;
  331. }
  332. data1 += width << 2;
  333. flags1 += t1->flags_stride << 2;
  334. }
  335. const int height = t1->h;
  336. for (i = 0; i < width; ++i) {
  337. int* data2 = data1 + i;
  338. flag_t* flags2 = flags1 + i;
  339. for (j = k; j < height; ++j) {
  340. flags2 += t1->flags_stride;
  341. t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
  342. data2 += width;
  343. }
  344. }
  345. } /* VSC and BYPASS by Antonin */
  346. static void t1_dec_sigpass_mqc_vsc(opj_t1_t* t1, int bpno, int orient)
  347. {
  348. int i, j, k, vsc;
  349. int one = 1 << bpno;
  350. int half = one >> 1;
  351. int oneplushalf = one | half;
  352. const int height = t1->h;
  353. const int width = t1->w;
  354. for (k = 0; k < height; k += 4) {
  355. for (i = 0; i < width; ++i) {
  356. int jw = k * width; /* j * width */
  357. for (j = k; j < k + 4 && j < height; ++j) {
  358. vsc = j == k + 3 || j == height - 1 ? 1 : 0;
  359. t1_dec_sigpass_step_mqc_vsc(t1, &t1->flags[(j + 1) * t1->flags_stride + i + 1],
  360. &t1->data[jw + i], orient, oneplushalf, vsc);
  361. jw += width;
  362. }
  363. }
  364. }
  365. } /* VSC and BYPASS by Antonin */
  366. static void t1_enc_refpass_step(opj_t1_t* t1, flag_t* flagsp, int* datap, int bpno,
  367. int one, int* nmsedec, char type, int vsc)
  368. {
  369. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  370. int flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
  371. if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
  372. *nmsedec += t1_getnmsedec_ref(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
  373. int v = int_abs(*datap) & one ? 1 : 0;
  374. mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
  375. if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
  376. mqc_bypass_enc(mqc, v);
  377. } else {
  378. mqc_encode(mqc, v);
  379. }
  380. *flagsp |= T1_REFINE;
  381. }
  382. }
  383. static INLINE void t1_dec_refpass_step_raw(opj_t1_t* t1, flag_t* flagsp, int* datap,
  384. int poshalf, int neghalf, int vsc)
  385. {
  386. opj_raw_t* raw = t1->raw; /* RAW component */
  387. int flag = vsc ? *flagsp & ~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S) : *flagsp;
  388. if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
  389. int v = raw_decode(raw);
  390. int t = v ? poshalf : neghalf;
  391. *datap += *datap < 0 ? -t : t;
  392. *flagsp |= T1_REFINE;
  393. }
  394. } /* VSC and BYPASS by Antonin */
  395. static INLINE void t1_dec_refpass_step_mqc(opj_t1_t* t1, flag_t* flagsp, int* datap,
  396. int poshalf, int neghalf)
  397. {
  398. opj_mqc_t* mqc = t1->mqc; /* MQC component */
  399. int flag = *flagsp;
  400. if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
  401. mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
  402. int v = mqc_decode(mqc);
  403. int t = v ? poshalf : neghalf;
  404. *datap += *datap < 0 ? -t : t;
  405. *flagsp |= T1_REFINE;
  406. }
  407. } /* VSC and BYPASS by Antonin */
  408. static INLINE void t1_dec_refpass_step_mqc_vsc(opj_t1_t* t1, flag_t* flagsp, int* datap,
  409. int poshalf, int neghalf, int vsc)
  410. {
  411. opj_mqc_t* mqc = t1->mqc; /* MQC component */
  412. int flag = vsc ? *flagsp & ~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S) : *flagsp;
  413. if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
  414. mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
  415. int v = mqc_decode(mqc);
  416. int t = v ? poshalf : neghalf;
  417. *datap += *datap < 0 ? -t : t;
  418. *flagsp |= T1_REFINE;
  419. }
  420. } /* VSC and BYPASS by Antonin */
  421. static void t1_enc_refpass(opj_t1_t* t1, int bpno, int* nmsedec, char type, int cblksty)
  422. {
  423. int i, j, k, vsc;
  424. *nmsedec = 0;
  425. int one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
  426. const int height = t1->h;
  427. const int width = t1->w;
  428. for (k = 0; k < height; k += 4) {
  429. for (i = 0; i < width; ++i) {
  430. int jw = k * width; /* j * width */
  431. for (j = k; j < k + 4 && j < height; ++j) {
  432. vsc = (cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == height - 1) ? 1 : 0;
  433. t1_enc_refpass_step(t1, &t1->flags[(j + 1) * t1->flags_stride + i + 1],
  434. &t1->data[jw + i], bpno, one, nmsedec, type, vsc);
  435. jw += width;
  436. }
  437. }
  438. }
  439. }
  440. static void t1_dec_refpass_raw(opj_t1_t* t1, int bpno, int cblksty)
  441. {
  442. int i, j, k, vsc;
  443. int one = 1 << bpno;
  444. int poshalf = one >> 1;
  445. int neghalf = bpno > 0 ? -poshalf : -1;
  446. const int height = t1->h;
  447. const int width = t1->w;
  448. for (k = 0; k < height; k += 4) {
  449. for (i = 0; i < t1->w; ++i) {
  450. int jw = k * width; /* j * width */
  451. for (j = k; j < k + 4 && j < height; ++j) {
  452. vsc = (cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == height - 1) ? 1 : 0;
  453. t1_dec_refpass_step_raw(t1, &t1->flags[(j + 1) * t1->flags_stride + i + 1],
  454. &t1->data[jw + i], poshalf, neghalf, vsc);
  455. jw += width;
  456. }
  457. }
  458. }
  459. } /* VSC and BYPASS by Antonin */
  460. static void t1_dec_refpass_mqc(opj_t1_t* t1, int bpno)
  461. {
  462. int i, j, k;
  463. int* data1 = t1->data;
  464. flag_t* flags1 = &t1->flags[1];
  465. int one = 1 << bpno;
  466. int poshalf = one >> 1;
  467. int neghalf = bpno > 0 ? -poshalf : -1;
  468. const int width = t1->w;
  469. const int cnt = t1->h & ~3;
  470. for (k = 0; k < cnt; k += 4) {
  471. for (i = 0; i < width; ++i) {
  472. int* data2 = data1 + i;
  473. flag_t* flags2 = flags1 + i;
  474. flags2 += t1->flags_stride;
  475. t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
  476. data2 += width;
  477. flags2 += t1->flags_stride;
  478. t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
  479. data2 += width;
  480. flags2 += t1->flags_stride;
  481. t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
  482. data2 += width;
  483. flags2 += t1->flags_stride;
  484. t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
  485. data2 += width;
  486. }
  487. data1 += width << 2;
  488. flags1 += t1->flags_stride << 2;
  489. }
  490. const int height = t1->h;
  491. for (i = 0; i < width; ++i) {
  492. int* data2 = data1 + i;
  493. flag_t* flags2 = flags1 + i;
  494. for (j = k; j < height; ++j) {
  495. flags2 += t1->flags_stride;
  496. t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
  497. data2 += width;
  498. }
  499. }
  500. } /* VSC and BYPASS by Antonin */
  501. static void t1_dec_refpass_mqc_vsc(opj_t1_t* t1, int bpno)
  502. {
  503. int i, j, k;
  504. int vsc;
  505. int one = 1 << bpno;
  506. int poshalf = one >> 1;
  507. int neghalf = bpno > 0 ? -poshalf : -1;
  508. const int width = t1->w;
  509. const int height = t1->h;
  510. for (k = 0; k < height; k += 4) {
  511. for (i = 0; i < width; ++i) {
  512. int jw = k * width; /* j * width */
  513. for (j = k; j < k + 4 && j < height; ++j) {
  514. vsc = j == k + 3 || j == height - 1 ? 1 : 0;
  515. t1_dec_refpass_step_mqc_vsc(t1, &t1->flags[(j + 1) * t1->flags_stride + i + 1],
  516. &t1->data[jw + i], poshalf, neghalf, vsc);
  517. }
  518. }
  519. }
  520. } /* VSC and BYPASS by Antonin */
  521. static void t1_enc_clnpass_step(opj_t1_t* t1, flag_t* flagsp, int* datap, int orient,
  522. int bpno, int one, int* nmsedec, int partial, int vsc)
  523. {
  524. opj_mqc_t* mqc = t1->mqc; /* MQC component */
  525. int flag = vsc ? *flagsp & ~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S) : *flagsp;
  526. if (partial) {
  527. goto LABEL_PARTIAL;
  528. }
  529. if (!(*flagsp & (T1_SIG | T1_VISIT))) {
  530. mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
  531. int v = int_abs(*datap) & one ? 1 : 0;
  532. mqc_encode(mqc, v);
  533. if (v) {
  534. LABEL_PARTIAL:
  535. *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
  536. mqc_setcurctx(mqc, t1_getctxno_sc(flag));
  537. v = *datap < 0 ? 1 : 0;
  538. mqc_encode(mqc, v ^ t1_getspb(flag));
  539. t1_updateflags(flagsp, v, t1->flags_stride);
  540. }
  541. }
  542. *flagsp &= ~T1_VISIT;
  543. }
  544. static void t1_dec_clnpass_step_partial(opj_t1_t* t1, flag_t* flagsp, int* datap,
  545. int orient, int oneplushalf)
  546. {
  547. opj_mqc_t* mqc = t1->mqc; /* MQC component */
  548. int flag = *flagsp;
  549. mqc_setcurctx(mqc, t1_getctxno_sc(flag));
  550. int v = mqc_decode(mqc) ^ t1_getspb(flag);
  551. *datap = v ? -oneplushalf : oneplushalf;
  552. t1_updateflags(flagsp, v, t1->flags_stride);
  553. *flagsp &= ~T1_VISIT;
  554. } /* VSC and BYPASS by Antonin */
  555. static void t1_dec_clnpass_step(opj_t1_t* t1, flag_t* flagsp, int* datap, int orient,
  556. int oneplushalf)
  557. {
  558. opj_mqc_t* mqc = t1->mqc; /* MQC component */
  559. int flag = *flagsp;
  560. if (!(flag & (T1_SIG | T1_VISIT))) {
  561. mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
  562. if (mqc_decode(mqc)) {
  563. mqc_setcurctx(mqc, t1_getctxno_sc(flag));
  564. int v = mqc_decode(mqc) ^ t1_getspb(flag);
  565. *datap = v ? -oneplushalf : oneplushalf;
  566. t1_updateflags(flagsp, v, t1->flags_stride);
  567. }
  568. }
  569. *flagsp &= ~T1_VISIT;
  570. } /* VSC and BYPASS by Antonin */
  571. static void t1_dec_clnpass_step_vsc(opj_t1_t* t1, flag_t* flagsp, int* datap, int orient,
  572. int oneplushalf, int partial, int vsc)
  573. {
  574. opj_mqc_t *mqc = t1->mqc; /* MQC component */
  575. int flag = vsc ? *flagsp & ~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S) : *flagsp;
  576. if (partial) {
  577. goto LABEL_PARTIAL;
  578. }
  579. if (!(flag & (T1_SIG | T1_VISIT))) {
  580. mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
  581. if (mqc_decode(mqc)) {
  582. LABEL_PARTIAL:
  583. mqc_setcurctx(mqc, t1_getctxno_sc(flag));
  584. int v = mqc_decode(mqc) ^ t1_getspb(flag);
  585. *datap = v ? -oneplushalf : oneplushalf;
  586. t1_updateflags(flagsp, v, t1->flags_stride);
  587. }
  588. }
  589. *flagsp &= ~T1_VISIT;
  590. }
  591. static void t1_enc_clnpass(opj_t1_t* t1, int bpno, int orient, int* nmsedec, int cblksty)
  592. {
  593. int i, j, k, agg, runlen, vsc;
  594. opj_mqc_t* mqc = t1->mqc; /* MQC component */
  595. *nmsedec = 0;
  596. int one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
  597. for (k = 0; k < t1->h; k += 4) {
  598. for (i = 0; i < t1->w; ++i) {
  599. if (k + 3 < t1->h) {
  600. if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  601. agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  602. || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  603. || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  604. || (MACRO_t1_flags(1 + k + 3,1 + i)
  605. & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
  606. } else {
  607. agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  608. || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  609. || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  610. || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
  611. }
  612. } else {
  613. agg = 0;
  614. }
  615. if (agg) {
  616. for (runlen = 0; runlen < 4; ++runlen) {
  617. if (int_abs(t1->data[((k + runlen) * t1->w) + i]) & one)
  618. break;
  619. }
  620. mqc_setcurctx(mqc, T1_CTXNO_AGG);
  621. mqc_encode(mqc, runlen != 4);
  622. if (runlen == 4) {
  623. continue;
  624. }
  625. mqc_setcurctx(mqc, T1_CTXNO_UNI);
  626. mqc_encode(mqc, runlen >> 1);
  627. mqc_encode(mqc, runlen & 1);
  628. } else {
  629. runlen = 0;
  630. }
  631. for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
  632. vsc = (cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1) ? 1 : 0;
  633. t1_enc_clnpass_step(t1,
  634. &t1->flags[(j + 1) * t1->flags_stride + i + 1],
  635. &t1->data[j * t1->w + i], orient, bpno, one, nmsedec,
  636. agg && (j == k + runlen), vsc);
  637. }
  638. }
  639. }
  640. }
  641. static void t1_dec_clnpass(opj_t1_t* t1, int bpno, int orient, int cblksty)
  642. {
  643. int i, j, k, agg, runlen, vsc;
  644. int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
  645. opj_mqc_t* mqc = t1->mqc; /* MQC component */
  646. int one = 1 << bpno;
  647. int half = one >> 1;
  648. int oneplushalf = one | half;
  649. const int width = t1->w;
  650. const int height = t1->h;
  651. if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  652. for (k = 0; k < height; k += 4) {
  653. for (i = 0; i < width; ++i) {
  654. if (k + 3 < height) {
  655. agg = !(MACRO_t1_flags(1 + k, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  656. || MACRO_t1_flags(1 + k + 1, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  657. || MACRO_t1_flags(1 + k + 2, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  658. || (MACRO_t1_flags(1 + k + 3, 1 + i)
  659. & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
  660. } else {
  661. agg = 0;
  662. }
  663. if (agg) {
  664. mqc_setcurctx(mqc, T1_CTXNO_AGG);
  665. if (!mqc_decode(mqc)) {
  666. continue;
  667. }
  668. mqc_setcurctx(mqc, T1_CTXNO_UNI);
  669. runlen = mqc_decode(mqc);
  670. runlen = (runlen << 1) | mqc_decode(mqc);
  671. } else {
  672. runlen = 0;
  673. }
  674. int jw = (k + runlen) * width; /* j * width */
  675. for (j = k + runlen; j < k + 4 && j < height; ++j) {
  676. vsc = j == k + 3 || j == height - 1 ? 1 : 0;
  677. t1_dec_clnpass_step_vsc(t1, &t1->flags[(j + 1) * t1->flags_stride + i + 1],
  678. &t1->data[jw + i], orient, oneplushalf,
  679. agg && (j == k + runlen), vsc);
  680. jw += width;
  681. }
  682. }
  683. }
  684. } else {
  685. int* data1 = t1->data;
  686. flag_t *flags1 = &t1->flags[1];
  687. for (k = 0; k < (height & ~3); k += 4) {
  688. for (i = 0; i < width; ++i) {
  689. int* data2 = data1 + i;
  690. flag_t *flags2 = flags1 + i;
  691. agg = !(MACRO_t1_flags(1 + k, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  692. || MACRO_t1_flags(1 + k + 1, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  693. || MACRO_t1_flags(1 + k + 2, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
  694. || MACRO_t1_flags(1 + k + 3, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
  695. if (agg) {
  696. mqc_setcurctx(mqc, T1_CTXNO_AGG);
  697. if (!mqc_decode(mqc)) {
  698. continue;
  699. }
  700. mqc_setcurctx(mqc, T1_CTXNO_UNI);
  701. runlen = mqc_decode(mqc);
  702. runlen = (runlen << 1) | mqc_decode(mqc);
  703. flags2 += runlen * t1->flags_stride;
  704. data2 += runlen * width;
  705. for (j = k + runlen; j < k + 4 && j < height; ++j) {
  706. flags2 += t1->flags_stride;
  707. if (agg && j == k + runlen) {
  708. t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
  709. } else {
  710. t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
  711. }
  712. data2 += width;
  713. }
  714. } else {
  715. flags2 += t1->flags_stride;
  716. t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
  717. data2 += width;
  718. flags2 += t1->flags_stride;
  719. t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
  720. data2 += width;
  721. flags2 += t1->flags_stride;
  722. t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
  723. data2 += width;
  724. flags2 += t1->flags_stride;
  725. t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
  726. data2 += width;
  727. }
  728. }
  729. data1 += width << 2;
  730. flags1 += t1->flags_stride << 2;
  731. }
  732. for (i = 0; i < width; ++i) {
  733. int* data2 = data1 + i;
  734. flag_t *flags2 = flags1 + i;
  735. for (j = k; j < height; ++j) {
  736. flags2 += t1->flags_stride;
  737. t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
  738. data2 += width;
  739. }
  740. }
  741. }
  742. if (segsym) {
  743. int v = 0;
  744. mqc_setcurctx(mqc, T1_CTXNO_UNI);
  745. v = mqc_decode(mqc);
  746. v = (v << 1) | mqc_decode(mqc);
  747. v = (v << 1) | mqc_decode(mqc);
  748. v = (v << 1) | mqc_decode(mqc);
  749. /*
  750. if (v != 0xa) {
  751. opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
  752. }
  753. */
  754. }
  755. } /* VSC and BYPASS by Antonin */
  756. /** mod fixed_quality */
  757. static double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno,
  758. int qmfbid, double stepsize, int numcomps, int mct)
  759. {
  760. double w1 = 1, w2, wmsedec;
  761. // Prevent running an MCT on more than 3 components. NB openjpeg v2.0 will support this via
  762. // custom MCT tables that can be passed as encode parameters, 1.3 cannot support this as it
  763. // uses a static table of 3 entries and there for can only cope with 3 components with out an
  764. // array overflow
  765. if (numcomps == 3) {
  766. if (qmfbid == 1) {
  767. w1 = numcomps > 1 ? mct_getnorm(compno) : 1.0;
  768. } else {
  769. w1 = numcomps > 1 ? mct_getnorm_real(compno) : 1.0;
  770. }
  771. }
  772. if (qmfbid == 1) {
  773. w2 = dwt_getnorm(level, orient);
  774. } else { /* if (qmfbid == 0) */
  775. w2 = dwt_getnorm_real(level, orient);
  776. }
  777. wmsedec = w1 * w2 * stepsize * (1 << bpno);
  778. wmsedec *= wmsedec * nmsedec / 8192.0;
  779. return wmsedec;
  780. }
  781. static bool allocate_buffers(opj_t1_t* t1, int w, int h)
  782. {
  783. int datasize = w * h;
  784. if (datasize > t1->datasize) {
  785. opj_aligned_free(t1->data);
  786. t1->data = (int*)opj_aligned_malloc(datasize * sizeof(int));
  787. if (!t1->data) {
  788. return false;
  789. }
  790. t1->datasize = datasize;
  791. }
  792. memset(t1->data,0,datasize * sizeof(int));
  793. t1->flags_stride = w + 2;
  794. int flagssize = t1->flags_stride * (h + 2);
  795. if (flagssize > t1->flagssize){
  796. opj_aligned_free(t1->flags);
  797. t1->flags = (flag_t*)opj_aligned_malloc(flagssize * sizeof(flag_t));
  798. if (!t1->flags) {
  799. return false;
  800. }
  801. t1->flagssize = flagssize;
  802. }
  803. memset(t1->flags, 0, flagssize * sizeof(flag_t));
  804. t1->w = w;
  805. t1->h = h;
  806. return true;
  807. }
  808. /** mod fixed_quality */
  809. static void t1_encode_cblk(opj_t1_t* t1, opj_tcd_cblk_enc_t* cblk, int orient,
  810. int compno, int level, int qmfbid, double stepsize,
  811. int cblksty, int numcomps, int mct, opj_tcd_tile_t* tile)
  812. {
  813. double cumwmsedec = 0.0;
  814. opj_mqc_t* mqc = t1->mqc; /* MQC component */
  815. int passno, bpno, passtype;
  816. int nmsedec = 0;
  817. char type = T1_TYPE_MQ;
  818. int max = 0;
  819. int i;
  820. for (i = 0; i < t1->w * t1->h; ++i) {
  821. int tmp = abs(t1->data[i]);
  822. max = int_max(max, tmp);
  823. }
  824. cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
  825. bpno = cblk->numbps - 1;
  826. passtype = 2;
  827. mqc_resetstates(mqc);
  828. mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  829. mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  830. mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  831. mqc_init_enc(mqc, cblk->data);
  832. for (passno = 0; bpno >= 0; ++passno) {
  833. opj_tcd_pass_t* pass = &cblk->passes[passno];
  834. int correction = 3;
  835. type = bpno < cblk->numbps - 4 && passtype < 2 && (cblksty & J2K_CCP_CBLKSTY_LAZY) ? T1_TYPE_RAW : T1_TYPE_MQ;
  836. switch (passtype) {
  837. case 0:
  838. t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
  839. break;
  840. case 1:
  841. t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
  842. break;
  843. case 2:
  844. t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
  845. /* code switch SEGMARK (i.e. SEGSYM) */
  846. if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
  847. mqc_segmark_enc(mqc);
  848. }
  849. }
  850. /* fixed_quality */
  851. double tempwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno,
  852. qmfbid, stepsize, numcomps, mct);
  853. cumwmsedec += tempwmsedec;
  854. tile->distotile += tempwmsedec;
  855. /* Code switch "RESTART" (i.e. TERMALL) */
  856. if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !(passtype == 2 && bpno - 1 < 0)) {
  857. if (type == T1_TYPE_RAW) {
  858. mqc_flush(mqc);
  859. correction = 1;
  860. /* correction = mqc_bypass_flush_enc(); */
  861. } else { /* correction = mqc_restart_enc(); */
  862. mqc_flush(mqc);
  863. correction = 1;
  864. }
  865. pass->term = 1;
  866. } else if (((bpno < cblk->numbps - 4 && passtype > 0) ||
  867. (bpno == cblk->numbps - 4 && passtype == 2)) &&
  868. (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
  869. if (type == T1_TYPE_RAW) {
  870. mqc_flush(mqc);
  871. correction = 1;
  872. /* correction = mqc_bypass_flush_enc(); */
  873. } else { /* correction = mqc_restart_enc(); */
  874. mqc_flush(mqc);
  875. correction = 1;
  876. }
  877. pass->term = 1;
  878. } else {
  879. pass->term = 0;
  880. }
  881. if (++passtype == 3) {
  882. passtype = 0;
  883. --bpno;
  884. }
  885. if (pass->term && bpno > 0) {
  886. type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
  887. if (type == T1_TYPE_RAW) {
  888. mqc_bypass_init_enc(mqc);
  889. } else {
  890. mqc_restart_init_enc(mqc);
  891. }
  892. }
  893. pass->distortiondec = cumwmsedec;
  894. pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
  895. /* Code-switch "RESET" */
  896. if (cblksty & J2K_CCP_CBLKSTY_RESET) {
  897. mqc_reset_enc(mqc);
  898. }
  899. }
  900. /* Code switch "ERTERM" (i.e. PTERM) */
  901. if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
  902. mqc_erterm_enc(mqc);
  903. } else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) {
  904. mqc_flush(mqc);
  905. }
  906. cblk->totalpasses = passno;
  907. for (passno = 0; passno < cblk->totalpasses; ++passno) {
  908. opj_tcd_pass_t* pass = &cblk->passes[passno];
  909. if (pass->rate > mqc_numbytes(mqc)) {
  910. pass->rate = mqc_numbytes(mqc);
  911. }
  912. /*Preventing generation of FF as last data byte of a pass*/
  913. if (pass->rate > 1 && cblk->data[pass->rate - 1] == 0xFF) {
  914. --pass->rate;
  915. }
  916. pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
  917. }
  918. }
  919. static void t1_decode_cblk(opj_t1_t* t1, opj_tcd_cblk_dec_t* cblk, int orient,
  920. int roishift, int cblksty)
  921. {
  922. opj_raw_t* raw = t1->raw; /* RAW component */
  923. opj_mqc_t* mqc = t1->mqc; /* MQC component */
  924. if (!allocate_buffers(t1, cblk->x1 - cblk->x0, cblk->y1 - cblk->y0))
  925. {
  926. return;
  927. }
  928. int bpno;
  929. int segno, passno;
  930. char type = T1_TYPE_MQ; /* BYPASS mode */
  931. bpno = roishift + cblk->numbps - 1;
  932. int passtype = 2;
  933. mqc_resetstates(mqc);
  934. mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  935. mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  936. mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  937. for (segno = 0; segno < cblk->numsegs; ++segno) {
  938. opj_tcd_seg_t* seg = &cblk->segs[segno];
  939. /* BYPASS mode */
  940. type = bpno <= cblk->numbps - 5 && passtype < 2 && (cblksty & J2K_CCP_CBLKSTY_LAZY) ? T1_TYPE_RAW : T1_TYPE_MQ;
  941. /* FIXME: slviewer gets here with a null pointer.
  942. Why? Partially downloaded and/or corrupt textures ? */
  943. if (!seg->data) {
  944. continue;
  945. }
  946. if (type == T1_TYPE_RAW) {
  947. raw_init_dec(raw, *seg->data + seg->dataindex, seg->len);
  948. } else {
  949. mqc_init_dec(mqc, *seg->data + seg->dataindex, seg->len);
  950. }
  951. for (passno = 0; passno < seg->numpasses; ++passno) {
  952. switch (passtype) {
  953. case 0:
  954. if (type == T1_TYPE_RAW) {
  955. t1_dec_sigpass_raw(t1, bpno + 1, orient, cblksty);
  956. } else if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  957. t1_dec_sigpass_mqc_vsc(t1, bpno + 1, orient);
  958. } else {
  959. t1_dec_sigpass_mqc(t1, bpno + 1, orient);
  960. }
  961. break;
  962. case 1:
  963. if (type == T1_TYPE_RAW) {
  964. t1_dec_refpass_raw(t1, bpno + 1, cblksty);
  965. } else if (cblksty & J2K_CCP_CBLKSTY_VSC) {
  966. t1_dec_refpass_mqc_vsc(t1, bpno + 1);
  967. } else {
  968. t1_dec_refpass_mqc(t1, bpno + 1);
  969. }
  970. break;
  971. case 2:
  972. t1_dec_clnpass(t1, bpno+1, orient, cblksty);
  973. }
  974. if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
  975. mqc_resetstates(mqc);
  976. mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  977. mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  978. mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  979. }
  980. if (++passtype == 3) {
  981. passtype = 0;
  982. --bpno;
  983. }
  984. }
  985. }
  986. }
  987. /* ----------------------------------------------------------------------- */
  988. opj_t1_t* t1_create(opj_common_ptr cinfo) {
  989. opj_t1_t* t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));
  990. if (!t1) {
  991. return NULL;
  992. }
  993. t1->cinfo = cinfo;
  994. /* create MQC and RAW handles */
  995. t1->mqc = mqc_create();
  996. t1->raw = raw_create();
  997. t1->data = NULL;
  998. t1->flags = NULL;
  999. t1->datasize = 0;
  1000. t1->flagssize = 0;
  1001. return t1;
  1002. }
  1003. void t1_destroy(opj_t1_t* t1) {
  1004. if (t1) {
  1005. /* destroy MQC and RAW handles */
  1006. mqc_destroy(t1->mqc);
  1007. raw_destroy(t1->raw);
  1008. opj_aligned_free(t1->data);
  1009. opj_aligned_free(t1->flags);
  1010. opj_free(t1);
  1011. }
  1012. }
  1013. void t1_encode_cblks(opj_t1_t* t1, opj_tcd_tile_t* tile, opj_tcp_t* tcp)
  1014. {
  1015. tile->distotile = 0; /* fixed_quality */
  1016. int compno;
  1017. for (compno = 0; compno < tile->numcomps; ++compno) {
  1018. opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
  1019. opj_tccp_t* tccp = &tcp->tccps[compno];
  1020. int tile_w = tilec->x1 - tilec->x0;
  1021. opj_tcd_resolution_t* pres = NULL;
  1022. int resno;
  1023. for (resno = 0; resno < tilec->numresolutions; ++resno) {
  1024. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  1025. int res_h, res_v;
  1026. if (pres) {
  1027. res_h = pres->x1 - pres->x0;
  1028. res_v = pres->y1 - pres->y0;
  1029. } else {
  1030. res_h = res_v = 0;
  1031. }
  1032. pres = res;
  1033. int bandno;
  1034. for (bandno = 0; bandno < res->numbands; ++bandno) {
  1035. opj_tcd_band_t* restrict band = &res->bands[bandno];
  1036. int bandconst = 8192 * 8192 / ((int) floor(band->stepsize * 8192));
  1037. int precno;
  1038. for (precno = 0; precno < res->pw * res->ph; ++precno) {
  1039. opj_tcd_precinct_t *prc = &band->precincts[precno];
  1040. int cblkno;
  1041. for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
  1042. opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
  1043. int x = cblk->x0 - band->x0;
  1044. int y = cblk->y0 - band->y0;
  1045. if (band->bandno & 1) {
  1046. x += res_h;
  1047. }
  1048. if (band->bandno & 2) {
  1049. y += res_v;
  1050. }
  1051. if (!allocate_buffers(t1, cblk->x1 - cblk->x0, cblk->y1 - cblk->y0))
  1052. {
  1053. return;
  1054. }
  1055. int* restrict datap = t1->data;
  1056. int cblk_w = t1->w;
  1057. int cblk_h = t1->h;
  1058. int* restrict tiledp = &tilec->data[y * tile_w + x];
  1059. if (tccp->qmfbid == 1) {
  1060. int i, j;
  1061. for (j = 0; j < cblk_h; ++j) {
  1062. for (i = 0; i < cblk_w; ++i) {
  1063. int tmp = tiledp[j * tile_w + i];
  1064. datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
  1065. }
  1066. }
  1067. } else { /* if (tccp->qmfbid == 0) */
  1068. int i, j;
  1069. for (j = 0; j < cblk_h; ++j) {
  1070. for (i = 0; i < cblk_w; ++i) {
  1071. int tmp = tiledp[j * tile_w + i];
  1072. datap[j * cblk_w + i] =
  1073. fix_mul(tmp,
  1074. bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
  1075. }
  1076. }
  1077. }
  1078. t1_encode_cblk(t1, cblk, band->bandno, compno,
  1079. tilec->numresolutions - 1 - resno,
  1080. tccp->qmfbid, band->stepsize, tccp->cblksty,
  1081. tile->numcomps, tcp->mct, tile);
  1082. } /* cblkno */
  1083. } /* precno */
  1084. } /* bandno */
  1085. } /* resno */
  1086. } /* compno */
  1087. }
  1088. void t1_decode_cblks(opj_t1_t* t1, opj_tcd_tilecomp_t* tilec, opj_tccp_t* tccp)
  1089. {
  1090. int tile_w = tilec->x1 - tilec->x0;
  1091. opj_tcd_resolution_t* pres = NULL;
  1092. int resno;
  1093. for (resno = 0; resno < tilec->numresolutions; ++resno) {
  1094. opj_tcd_resolution_t* res = &tilec->resolutions[resno];
  1095. int res_h, res_v;
  1096. if (pres) {
  1097. res_h = pres->x1 - pres->x0;
  1098. res_v = pres->y1 - pres->y0;
  1099. } else {
  1100. res_h = res_v = 0;
  1101. }
  1102. pres = res;
  1103. int bandno;
  1104. for (bandno = 0; bandno < res->numbands; ++bandno) {
  1105. opj_tcd_band_t* restrict band = &res->bands[bandno];
  1106. int precno;
  1107. for (precno = 0; precno < res->pw * res->ph; ++precno) {
  1108. opj_tcd_precinct_t* precinct = &band->precincts[precno];
  1109. int cnt = precinct->cw * precinct->ch;
  1110. int cblkno;
  1111. for (cblkno = 0; cblkno < cnt; ++cblkno) {
  1112. opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
  1113. t1_decode_cblk(t1, cblk, band->bandno, tccp->roishift, tccp->cblksty);
  1114. int x = cblk->x0 - band->x0;
  1115. int y = cblk->y0 - band->y0;
  1116. if (band->bandno & 1) {
  1117. x += res_h;
  1118. }
  1119. if (band->bandno & 2) {
  1120. y += res_v;
  1121. }
  1122. int* restrict datap = t1->data;
  1123. int cblk_w = t1->w;
  1124. int cblk_h = t1->h;
  1125. int i = tccp->roishift;
  1126. if (i >= 31) {
  1127. memset((void*)datap, 0, cblk_h * cblk_w * sizeof(int));
  1128. } else if (i) {
  1129. int thresh = 1 << i;
  1130. int j;
  1131. for (j = 0; j < cblk_h; ++j) {
  1132. const int jcblkw = j * cblk_w;
  1133. for (i = 0; i < cblk_w; ++i) {
  1134. int val = datap[jcblkw + i];
  1135. int mag = abs(val);
  1136. if (mag >= thresh) {
  1137. mag >>= tccp->roishift;
  1138. datap[jcblkw + i] = val < 0 ? -mag : mag;
  1139. }
  1140. }
  1141. }
  1142. }
  1143. if (tccp->qmfbid == 1) {
  1144. int* restrict tiledp = &tilec->data[(y * tile_w) + x];
  1145. int j;
  1146. for (j = 0; j < cblk_h; ++j) {
  1147. const int jcblkw = j * cblk_w;
  1148. const int jtilew = j * tile_w;
  1149. int cnt = cblk_w & ~3U;
  1150. int i = 0;
  1151. for ( ; i < cnt; i += 4) {
  1152. int tmp0 = datap[jcblkw + i];
  1153. int tmp1 = datap[jcblkw + i + 1];
  1154. int tmp2 = datap[jcblkw + i + 2];
  1155. int tmp3 = datap[jcblkw + i + 3];
  1156. ((int*)tiledp)[jtilew + i] = tmp0 / 2;
  1157. ((int*)tiledp)[jtilew + i + 1] = tmp1 / 2;
  1158. ((int*)tiledp)[jtilew + i + 2] = tmp2 / 2;
  1159. ((int*)tiledp)[jtilew + i + 3] = tmp3 / 2;
  1160. }
  1161. for ( ; i < cblk_w; ++i) {
  1162. int tmp = datap[jcblkw + i];
  1163. ((int*)tiledp)[jtilew + i] = tmp / 2;
  1164. }
  1165. }
  1166. } else { /* if (tccp->qmfbid == 0) */
  1167. float* restrict tiledp = (float*)&tilec->data[(y * tile_w) + x];
  1168. int i, j;
  1169. for (j = 0; j < cblk_h; ++j) {
  1170. float* restrict tiledp2 = tiledp;
  1171. for (i = 0; i < cblk_w; ++i) {
  1172. *tiledp2 = (float)(*datap * band->stepsize);
  1173. ++datap;
  1174. ++tiledp2;
  1175. }
  1176. tiledp += tile_w;
  1177. }
  1178. }
  1179. opj_free(cblk->data);
  1180. opj_free(cblk->segs);
  1181. } /* cblkno */
  1182. opj_free(precinct->cblks.dec);
  1183. } /* precno */
  1184. } /* bandno */
  1185. } /* resno */
  1186. }