tcd.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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) 2006-2007, Parvatha Elangovan
  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. #define _ISOC99_SOURCE /* lrintf is C99 */
  33. #include "opj_includes.h"
  34. #include <assert.h>
  35. void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
  36. int tileno, compno, resno, bandno, precno;/*, cblkno;*/
  37. fprintf(fd, "image {\n");
  38. fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
  39. img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
  40. for (tileno = 0; tileno < img->th * img->tw; tileno++) {
  41. opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
  42. fprintf(fd, " tile {\n");
  43. fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
  44. tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
  45. for (compno = 0; compno < tile->numcomps; compno++) {
  46. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  47. fprintf(fd, " tilec {\n");
  48. fprintf(fd,
  49. " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
  50. tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
  51. for (resno = 0; resno < tilec->numresolutions; resno++) {
  52. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  53. fprintf(fd, "\n res {\n");
  54. fprintf(fd,
  55. " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
  56. res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
  57. for (bandno = 0; bandno < res->numbands; bandno++) {
  58. opj_tcd_band_t *band = &res->bands[bandno];
  59. fprintf(fd, " band {\n");
  60. fprintf(fd,
  61. " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
  62. band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
  63. for (precno = 0; precno < res->pw * res->ph; precno++) {
  64. opj_tcd_precinct_t *prec = &band->precincts[precno];
  65. fprintf(fd, " prec {\n");
  66. fprintf(fd,
  67. " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
  68. prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
  69. /*
  70. for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
  71. opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
  72. fprintf(fd, " cblk {\n");
  73. fprintf(fd,
  74. " x0=%d, y0=%d, x1=%d, y1=%d\n",
  75. cblk->x0, cblk->y0, cblk->x1, cblk->y1);
  76. fprintf(fd, " }\n");
  77. }
  78. */
  79. fprintf(fd, " }\n");
  80. }
  81. fprintf(fd, " }\n");
  82. }
  83. fprintf(fd, " }\n");
  84. }
  85. fprintf(fd, " }\n");
  86. }
  87. fprintf(fd, " }\n");
  88. }
  89. fprintf(fd, "}\n");
  90. }
  91. /* ----------------------------------------------------------------------- */
  92. /**
  93. Create a new TCD handle
  94. */
  95. opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
  96. /* create the tcd structure */
  97. opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
  98. if(!tcd) return NULL;
  99. tcd->cinfo = cinfo;
  100. tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
  101. if(!tcd->tcd_image) {
  102. opj_free(tcd);
  103. return NULL;
  104. }
  105. return tcd;
  106. }
  107. /**
  108. Destroy a previously created TCD handle
  109. */
  110. void tcd_destroy(opj_tcd_t *tcd) {
  111. if(tcd) {
  112. opj_free(tcd->tcd_image);
  113. opj_free(tcd);
  114. }
  115. }
  116. /* ----------------------------------------------------------------------- */
  117. void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
  118. int tileno, compno, resno, bandno, precno, cblkno;
  119. tcd->image = image;
  120. tcd->cp = cp;
  121. tcd->tcd_image->tw = cp->tw;
  122. tcd->tcd_image->th = cp->th;
  123. tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
  124. for (tileno = 0; tileno < 1; tileno++) {
  125. opj_tcp_t *tcp = &cp->tcps[curtileno];
  126. int j;
  127. /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
  128. int p = curtileno % cp->tw; /* si numerotation matricielle .. */
  129. int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
  130. /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
  131. opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
  132. /* 4 borders of the tile rescale on the image if necessary */
  133. tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
  134. tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
  135. tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
  136. tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
  137. tile->numcomps = image->numcomps;
  138. /* tile->PPT=image->PPT; */
  139. /* Modification of the RATE >> */
  140. for (j = 0; j < tcp->numlayers; j++) {
  141. tcp->rates[j] = tcp->rates[j] ?
  142. cp->tp_on ?
  143. (((float) (tile->numcomps
  144. * (tile->x1 - tile->x0)
  145. * (tile->y1 - tile->y0)
  146. * image->comps[0].prec))
  147. /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
  148. :
  149. ((float) (tile->numcomps
  150. * (tile->x1 - tile->x0)
  151. * (tile->y1 - tile->y0)
  152. * image->comps[0].prec))/
  153. (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
  154. : 0;
  155. if (tcp->rates[j]) {
  156. if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
  157. tcp->rates[j] = tcp->rates[j - 1] + 20;
  158. } else {
  159. if (!j && tcp->rates[j] < 30)
  160. tcp->rates[j] = 30;
  161. }
  162. if(j == (tcp->numlayers-1)){
  163. tcp->rates[j] = tcp->rates[j]- 2;
  164. }
  165. }
  166. }
  167. /* << Modification of the RATE */
  168. tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
  169. for (compno = 0; compno < tile->numcomps; compno++) {
  170. opj_tccp_t *tccp = &tcp->tccps[compno];
  171. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  172. /* border of each tile component (global) */
  173. tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
  174. tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
  175. tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
  176. tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
  177. tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
  178. tilec->numresolutions = tccp->numresolutions;
  179. tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
  180. for (resno = 0; resno < tilec->numresolutions; resno++) {
  181. int pdx, pdy;
  182. int levelno = tilec->numresolutions - 1 - resno;
  183. int tlprcxstart, tlprcystart, brprcxend, brprcyend;
  184. int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
  185. int cbgwidthexpn, cbgheightexpn;
  186. int cblkwidthexpn, cblkheightexpn;
  187. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  188. /* border for each resolution level (global) */
  189. res->x0 = int_ceildivpow2(tilec->x0, levelno);
  190. res->y0 = int_ceildivpow2(tilec->y0, levelno);
  191. res->x1 = int_ceildivpow2(tilec->x1, levelno);
  192. res->y1 = int_ceildivpow2(tilec->y1, levelno);
  193. res->numbands = resno == 0 ? 1 : 3;
  194. /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
  195. if (tccp->csty & J2K_CCP_CSTY_PRT) {
  196. pdx = tccp->prcw[resno];
  197. pdy = tccp->prch[resno];
  198. } else {
  199. pdx = 15;
  200. pdy = 15;
  201. }
  202. /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
  203. tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
  204. tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
  205. brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
  206. brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
  207. res->pw = (brprcxend - tlprcxstart) >> pdx;
  208. res->ph = (brprcyend - tlprcystart) >> pdy;
  209. if (resno == 0) {
  210. tlcbgxstart = tlprcxstart;
  211. tlcbgystart = tlprcystart;
  212. brcbgxend = brprcxend;
  213. brcbgyend = brprcyend;
  214. cbgwidthexpn = pdx;
  215. cbgheightexpn = pdy;
  216. } else {
  217. tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
  218. tlcbgystart = int_ceildivpow2(tlprcystart, 1);
  219. brcbgxend = int_ceildivpow2(brprcxend, 1);
  220. brcbgyend = int_ceildivpow2(brprcyend, 1);
  221. cbgwidthexpn = pdx - 1;
  222. cbgheightexpn = pdy - 1;
  223. }
  224. (void)brcbgyend;
  225. (void)brcbgxend;
  226. cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
  227. cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
  228. for (bandno = 0; bandno < res->numbands; bandno++) {
  229. int x0b, y0b, i;
  230. int gain, numbps;
  231. opj_stepsize_t *ss = NULL;
  232. opj_tcd_band_t *band = &res->bands[bandno];
  233. band->bandno = resno == 0 ? 0 : bandno + 1;
  234. x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
  235. y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
  236. if (band->bandno == 0) {
  237. /* band border (global) */
  238. band->x0 = int_ceildivpow2(tilec->x0, levelno);
  239. band->y0 = int_ceildivpow2(tilec->y0, levelno);
  240. band->x1 = int_ceildivpow2(tilec->x1, levelno);
  241. band->y1 = int_ceildivpow2(tilec->y1, levelno);
  242. } else {
  243. /* band border (global) */
  244. band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
  245. band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
  246. band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
  247. band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
  248. }
  249. ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
  250. gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
  251. numbps = image->comps[compno].prec + gain;
  252. band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
  253. band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
  254. band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
  255. for (i = 0; i < res->pw * res->ph * 3; i++) {
  256. band->precincts[i].imsbtree = NULL;
  257. band->precincts[i].incltree = NULL;
  258. band->precincts[i].cblks.enc = NULL;
  259. }
  260. for (precno = 0; precno < res->pw * res->ph; precno++) {
  261. int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
  262. int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
  263. int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
  264. int cbgxend = cbgxstart + (1 << cbgwidthexpn);
  265. int cbgyend = cbgystart + (1 << cbgheightexpn);
  266. opj_tcd_precinct_t *prc = &band->precincts[precno];
  267. /* precinct size (global) */
  268. prc->x0 = int_max(cbgxstart, band->x0);
  269. prc->y0 = int_max(cbgystart, band->y0);
  270. prc->x1 = int_min(cbgxend, band->x1);
  271. prc->y1 = int_min(cbgyend, band->y1);
  272. tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
  273. tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
  274. brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
  275. brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
  276. prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
  277. prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
  278. prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
  279. prc->incltree = tgt_create(prc->cw, prc->ch);
  280. prc->imsbtree = tgt_create(prc->cw, prc->ch);
  281. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  282. int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
  283. int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
  284. int cblkxend = cblkxstart + (1 << cblkwidthexpn);
  285. int cblkyend = cblkystart + (1 << cblkheightexpn);
  286. opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
  287. /* code-block size (global) */
  288. cblk->x0 = int_max(cblkxstart, prc->x0);
  289. cblk->y0 = int_max(cblkystart, prc->y0);
  290. cblk->x1 = int_min(cblkxend, prc->x1);
  291. cblk->y1 = int_min(cblkyend, prc->y1);
  292. cblk->data = (unsigned char*) opj_calloc(9728+2, sizeof(unsigned char));
  293. /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
  294. cblk->data[0] = 0;
  295. cblk->data[1] = 0;
  296. cblk->data += 2;
  297. cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
  298. cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
  299. }
  300. }
  301. }
  302. }
  303. }
  304. }
  305. /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
  306. }
  307. void tcd_free_encode(opj_tcd_t *tcd) {
  308. int tileno, compno, resno, bandno, precno, cblkno;
  309. for (tileno = 0; tileno < 1; tileno++) {
  310. opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
  311. for (compno = 0; compno < tile->numcomps; compno++) {
  312. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  313. for (resno = 0; resno < tilec->numresolutions; resno++) {
  314. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  315. for (bandno = 0; bandno < res->numbands; bandno++) {
  316. opj_tcd_band_t *band = &res->bands[bandno];
  317. for (precno = 0; precno < res->pw * res->ph; precno++) {
  318. opj_tcd_precinct_t *prc = &band->precincts[precno];
  319. if (prc->incltree != NULL) {
  320. tgt_destroy(prc->incltree);
  321. prc->incltree = NULL;
  322. }
  323. if (prc->imsbtree != NULL) {
  324. tgt_destroy(prc->imsbtree);
  325. prc->imsbtree = NULL;
  326. }
  327. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  328. opj_free(prc->cblks.enc[cblkno].data - 2);
  329. opj_free(prc->cblks.enc[cblkno].layers);
  330. opj_free(prc->cblks.enc[cblkno].passes);
  331. }
  332. opj_free(prc->cblks.enc);
  333. } /* for (precno */
  334. opj_free(band->precincts);
  335. band->precincts = NULL;
  336. } /* for (bandno */
  337. } /* for (resno */
  338. opj_free(tilec->resolutions);
  339. tilec->resolutions = NULL;
  340. } /* for (compno */
  341. opj_free(tile->comps);
  342. tile->comps = NULL;
  343. } /* for (tileno */
  344. opj_free(tcd->tcd_image->tiles);
  345. tcd->tcd_image->tiles = NULL;
  346. }
  347. void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
  348. int tileno, compno, resno, bandno, precno, cblkno;
  349. for (tileno = 0; tileno < 1; tileno++) {
  350. opj_tcp_t *tcp = &cp->tcps[curtileno];
  351. int j;
  352. /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
  353. int p = curtileno % cp->tw;
  354. int q = curtileno / cp->tw;
  355. opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
  356. /* 4 borders of the tile rescale on the image if necessary */
  357. tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
  358. tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
  359. tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
  360. tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
  361. tile->numcomps = image->numcomps;
  362. /* tile->PPT=image->PPT; */
  363. /* Modification of the RATE >> */
  364. for (j = 0; j < tcp->numlayers; j++) {
  365. tcp->rates[j] = tcp->rates[j] ?
  366. cp->tp_on ?
  367. (((float) (tile->numcomps
  368. * (tile->x1 - tile->x0)
  369. * (tile->y1 - tile->y0)
  370. * image->comps[0].prec))
  371. /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
  372. :
  373. ((float) (tile->numcomps
  374. * (tile->x1 - tile->x0)
  375. * (tile->y1 - tile->y0)
  376. * image->comps[0].prec))/
  377. (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
  378. : 0;
  379. if (tcp->rates[j]) {
  380. if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
  381. tcp->rates[j] = tcp->rates[j - 1] + 20;
  382. } else {
  383. if (!j && tcp->rates[j] < 30)
  384. tcp->rates[j] = 30;
  385. }
  386. }
  387. }
  388. /* << Modification of the RATE */
  389. /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
  390. for (compno = 0; compno < tile->numcomps; compno++) {
  391. opj_tccp_t *tccp = &tcp->tccps[compno];
  392. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  393. /* border of each tile component (global) */
  394. tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
  395. tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
  396. tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
  397. tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
  398. tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
  399. tilec->numresolutions = tccp->numresolutions;
  400. /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
  401. for (resno = 0; resno < tilec->numresolutions; resno++) {
  402. int pdx, pdy;
  403. int levelno = tilec->numresolutions - 1 - resno;
  404. int tlprcxstart, tlprcystart, brprcxend, brprcyend;
  405. int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
  406. int cbgwidthexpn, cbgheightexpn;
  407. int cblkwidthexpn, cblkheightexpn;
  408. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  409. /* border for each resolution level (global) */
  410. res->x0 = int_ceildivpow2(tilec->x0, levelno);
  411. res->y0 = int_ceildivpow2(tilec->y0, levelno);
  412. res->x1 = int_ceildivpow2(tilec->x1, levelno);
  413. res->y1 = int_ceildivpow2(tilec->y1, levelno);
  414. res->numbands = resno == 0 ? 1 : 3;
  415. /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
  416. if (tccp->csty & J2K_CCP_CSTY_PRT) {
  417. pdx = tccp->prcw[resno];
  418. pdy = tccp->prch[resno];
  419. } else {
  420. pdx = 15;
  421. pdy = 15;
  422. }
  423. /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
  424. tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
  425. tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
  426. brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
  427. brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
  428. res->pw = (brprcxend - tlprcxstart) >> pdx;
  429. res->ph = (brprcyend - tlprcystart) >> pdy;
  430. if (resno == 0) {
  431. tlcbgxstart = tlprcxstart;
  432. tlcbgystart = tlprcystart;
  433. brcbgxend = brprcxend;
  434. brcbgyend = brprcyend;
  435. cbgwidthexpn = pdx;
  436. cbgheightexpn = pdy;
  437. } else {
  438. tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
  439. tlcbgystart = int_ceildivpow2(tlprcystart, 1);
  440. brcbgxend = int_ceildivpow2(brprcxend, 1);
  441. brcbgyend = int_ceildivpow2(brprcyend, 1);
  442. cbgwidthexpn = pdx - 1;
  443. cbgheightexpn = pdy - 1;
  444. }
  445. (void)brcbgyend;
  446. (void)brcbgxend;
  447. cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
  448. cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
  449. for (bandno = 0; bandno < res->numbands; bandno++) {
  450. int x0b, y0b;
  451. int gain, numbps;
  452. opj_stepsize_t *ss = NULL;
  453. opj_tcd_band_t *band = &res->bands[bandno];
  454. band->bandno = resno == 0 ? 0 : bandno + 1;
  455. x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
  456. y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
  457. if (band->bandno == 0) {
  458. /* band border */
  459. band->x0 = int_ceildivpow2(tilec->x0, levelno);
  460. band->y0 = int_ceildivpow2(tilec->y0, levelno);
  461. band->x1 = int_ceildivpow2(tilec->x1, levelno);
  462. band->y1 = int_ceildivpow2(tilec->y1, levelno);
  463. } else {
  464. band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
  465. band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
  466. band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
  467. band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
  468. }
  469. ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
  470. gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
  471. numbps = image->comps[compno].prec + gain;
  472. band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
  473. band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
  474. for (precno = 0; precno < res->pw * res->ph; precno++) {
  475. int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
  476. int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
  477. int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
  478. int cbgxend = cbgxstart + (1 << cbgwidthexpn);
  479. int cbgyend = cbgystart + (1 << cbgheightexpn);
  480. opj_tcd_precinct_t *prc = &band->precincts[precno];
  481. /* precinct size (global) */
  482. prc->x0 = int_max(cbgxstart, band->x0);
  483. prc->y0 = int_max(cbgystart, band->y0);
  484. prc->x1 = int_min(cbgxend, band->x1);
  485. prc->y1 = int_min(cbgyend, band->y1);
  486. tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
  487. tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
  488. brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
  489. brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
  490. prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
  491. prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
  492. opj_free(prc->cblks.enc);
  493. prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
  494. if (prc->incltree != NULL) {
  495. tgt_destroy(prc->incltree);
  496. }
  497. if (prc->imsbtree != NULL) {
  498. tgt_destroy(prc->imsbtree);
  499. }
  500. prc->incltree = tgt_create(prc->cw, prc->ch);
  501. prc->imsbtree = tgt_create(prc->cw, prc->ch);
  502. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  503. int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
  504. int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
  505. int cblkxend = cblkxstart + (1 << cblkwidthexpn);
  506. int cblkyend = cblkystart + (1 << cblkheightexpn);
  507. opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
  508. /* code-block size (global) */
  509. cblk->x0 = int_max(cblkxstart, prc->x0);
  510. cblk->y0 = int_max(cblkystart, prc->y0);
  511. cblk->x1 = int_min(cblkxend, prc->x1);
  512. cblk->y1 = int_min(cblkyend, prc->y1);
  513. cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
  514. /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
  515. cblk->data[0] = 0;
  516. cblk->data[1] = 0;
  517. cblk->data += 2;
  518. cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
  519. cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
  520. }
  521. } /* precno */
  522. } /* bandno */
  523. } /* resno */
  524. } /* compno */
  525. } /* tileno */
  526. /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
  527. }
  528. void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
  529. int i, j, tileno, p, q;
  530. unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
  531. tcd->image = image;
  532. tcd->tcd_image->tw = cp->tw;
  533. tcd->tcd_image->th = cp->th;
  534. tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(cp->tw * cp->th, sizeof(opj_tcd_tile_t));
  535. /*
  536. Allocate place to store the decoded data = final image
  537. Place limited by the tile really present in the codestream
  538. */
  539. for (j = 0; j < cp->tileno_size; j++) {
  540. opj_tcd_tile_t *tile;
  541. tileno = cp->tileno[j];
  542. tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
  543. tile->numcomps = image->numcomps;
  544. tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
  545. }
  546. for (i = 0; i < image->numcomps; i++) {
  547. for (j = 0; j < cp->tileno_size; j++) {
  548. opj_tcd_tile_t *tile;
  549. opj_tcd_tilecomp_t *tilec;
  550. /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
  551. tileno = cp->tileno[j];
  552. tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
  553. tilec = &tile->comps[i];
  554. p = tileno % cp->tw; /* si numerotation matricielle .. */
  555. q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
  556. /* 4 borders of the tile rescale on the image if necessary */
  557. tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
  558. tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
  559. tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
  560. tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
  561. tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
  562. tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
  563. tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
  564. tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
  565. x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
  566. y0 = j == 0 ? tilec->y0 : int_min(y0, (unsigned int) tilec->y0);
  567. x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1);
  568. y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1);
  569. }
  570. w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
  571. h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
  572. image->comps[i].w = w;
  573. image->comps[i].h = h;
  574. image->comps[i].x0 = x0;
  575. image->comps[i].y0 = y0;
  576. }
  577. }
  578. void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int tileno, opj_codestream_info_t *cstr_info) {
  579. int compno, resno, bandno, precno, cblkno;
  580. opj_tcp_t *tcp;
  581. opj_tcd_tile_t *tile;
  582. OPJ_ARG_NOT_USED(cstr_info);
  583. tcd->cp = cp;
  584. tcp = &(cp->tcps[cp->tileno[tileno]]);
  585. tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
  586. tileno = cp->tileno[tileno];
  587. for (compno = 0; compno < tile->numcomps; compno++) {
  588. opj_tccp_t *tccp = &tcp->tccps[compno];
  589. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  590. if (tccp->numresolutions <= 0)
  591. {
  592. cp->tileno[tileno] = -1;
  593. return;
  594. }
  595. /* border of each tile component (global) */
  596. tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
  597. tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
  598. tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
  599. tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
  600. tilec->numresolutions = tccp->numresolutions;
  601. tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
  602. for (resno = 0; resno < tilec->numresolutions; resno++) {
  603. int pdx, pdy;
  604. int levelno = tilec->numresolutions - 1 - resno;
  605. int tlprcxstart, tlprcystart, brprcxend, brprcyend;
  606. int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
  607. int cbgwidthexpn, cbgheightexpn;
  608. int cblkwidthexpn, cblkheightexpn;
  609. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  610. /* border for each resolution level (global) */
  611. res->x0 = int_ceildivpow2(tilec->x0, levelno);
  612. res->y0 = int_ceildivpow2(tilec->y0, levelno);
  613. res->x1 = int_ceildivpow2(tilec->x1, levelno);
  614. res->y1 = int_ceildivpow2(tilec->y1, levelno);
  615. res->numbands = resno == 0 ? 1 : 3;
  616. /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
  617. if (tccp->csty & J2K_CCP_CSTY_PRT) {
  618. pdx = tccp->prcw[resno];
  619. pdy = tccp->prch[resno];
  620. } else {
  621. pdx = 15;
  622. pdy = 15;
  623. }
  624. /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
  625. tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
  626. tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
  627. brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
  628. brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
  629. res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
  630. res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
  631. if (resno == 0) {
  632. tlcbgxstart = tlprcxstart;
  633. tlcbgystart = tlprcystart;
  634. brcbgxend = brprcxend;
  635. brcbgyend = brprcyend;
  636. cbgwidthexpn = pdx;
  637. cbgheightexpn = pdy;
  638. } else {
  639. tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
  640. tlcbgystart = int_ceildivpow2(tlprcystart, 1);
  641. brcbgxend = int_ceildivpow2(brprcxend, 1);
  642. brcbgyend = int_ceildivpow2(brprcyend, 1);
  643. cbgwidthexpn = pdx - 1;
  644. cbgheightexpn = pdy - 1;
  645. }
  646. (void)brcbgyend;
  647. (void)brcbgxend;
  648. cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
  649. cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
  650. for (bandno = 0; bandno < res->numbands; bandno++) {
  651. int x0b, y0b;
  652. int gain, numbps;
  653. opj_stepsize_t *ss = NULL;
  654. opj_tcd_band_t *band = &res->bands[bandno];
  655. band->bandno = resno == 0 ? 0 : bandno + 1;
  656. x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
  657. y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
  658. if (band->bandno == 0) {
  659. /* band border (global) */
  660. band->x0 = int_ceildivpow2(tilec->x0, levelno);
  661. band->y0 = int_ceildivpow2(tilec->y0, levelno);
  662. band->x1 = int_ceildivpow2(tilec->x1, levelno);
  663. band->y1 = int_ceildivpow2(tilec->y1, levelno);
  664. } else {
  665. /* band border (global) */
  666. band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
  667. band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
  668. band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
  669. band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
  670. }
  671. ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
  672. gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
  673. numbps = image->comps[compno].prec + gain;
  674. band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
  675. band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
  676. band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
  677. for (precno = 0; precno < res->pw * res->ph; precno++) {
  678. int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
  679. int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
  680. int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
  681. int cbgxend = cbgxstart + (1 << cbgwidthexpn);
  682. int cbgyend = cbgystart + (1 << cbgheightexpn);
  683. opj_tcd_precinct_t *prc = &band->precincts[precno];
  684. /* precinct size (global) */
  685. prc->x0 = int_max(cbgxstart, band->x0);
  686. prc->y0 = int_max(cbgystart, band->y0);
  687. prc->x1 = int_min(cbgxend, band->x1);
  688. prc->y1 = int_min(cbgyend, band->y1);
  689. tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
  690. tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
  691. brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
  692. brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
  693. prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
  694. prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
  695. prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
  696. prc->incltree = tgt_create(prc->cw, prc->ch);
  697. prc->imsbtree = tgt_create(prc->cw, prc->ch);
  698. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  699. int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
  700. int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
  701. int cblkxend = cblkxstart + (1 << cblkwidthexpn);
  702. int cblkyend = cblkystart + (1 << cblkheightexpn);
  703. opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
  704. cblk->data = NULL;
  705. cblk->segs = NULL;
  706. /* code-block size (global) */
  707. cblk->x0 = int_max(cblkxstart, prc->x0);
  708. cblk->y0 = int_max(cblkystart, prc->y0);
  709. cblk->x1 = int_min(cblkxend, prc->x1);
  710. cblk->y1 = int_min(cblkyend, prc->y1);
  711. cblk->numsegs = 0;
  712. }
  713. } /* precno */
  714. } /* bandno */
  715. } /* resno */
  716. } /* compno */
  717. /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
  718. }
  719. void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
  720. int compno, resno, bandno, precno, cblkno;
  721. int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
  722. int matrice[10][10][3];
  723. int i, j, k;
  724. opj_cp_t *cp = tcd->cp;
  725. opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
  726. opj_tcp_t *tcd_tcp = tcd->tcp;
  727. /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
  728. for (compno = 0; compno < tcd_tile->numcomps; compno++) {
  729. opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
  730. for (i = 0; i < tcd_tcp->numlayers; i++) {
  731. for (j = 0; j < tilec->numresolutions; j++) {
  732. for (k = 0; k < 3; k++) {
  733. matrice[i][j][k] =
  734. (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
  735. * (float) (tcd->image->comps[compno].prec / 16.0));
  736. }
  737. }
  738. }
  739. for (resno = 0; resno < tilec->numresolutions; resno++) {
  740. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  741. for (bandno = 0; bandno < res->numbands; bandno++) {
  742. opj_tcd_band_t *band = &res->bands[bandno];
  743. for (precno = 0; precno < res->pw * res->ph; precno++) {
  744. opj_tcd_precinct_t *prc = &band->precincts[precno];
  745. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  746. opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
  747. opj_tcd_layer_t *layer = &cblk->layers[layno];
  748. int n;
  749. int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
  750. /* Correction of the matrix of coefficient to include the IMSB information */
  751. if (layno == 0) {
  752. value = matrice[layno][resno][bandno];
  753. if (imsb >= value) {
  754. value = 0;
  755. } else {
  756. value -= imsb;
  757. }
  758. } else {
  759. value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
  760. if (imsb >= matrice[layno - 1][resno][bandno]) {
  761. value -= (imsb - matrice[layno - 1][resno][bandno]);
  762. if (value < 0) {
  763. value = 0;
  764. }
  765. }
  766. }
  767. if (layno == 0) {
  768. cblk->numpassesinlayers = 0;
  769. }
  770. n = cblk->numpassesinlayers;
  771. if (cblk->numpassesinlayers == 0) {
  772. if (value != 0) {
  773. n = 3 * value - 2 + cblk->numpassesinlayers;
  774. } else {
  775. n = cblk->numpassesinlayers;
  776. }
  777. } else {
  778. n = 3 * value + cblk->numpassesinlayers;
  779. }
  780. layer->numpasses = n - cblk->numpassesinlayers;
  781. if (!layer->numpasses)
  782. continue;
  783. if (cblk->numpassesinlayers == 0) {
  784. layer->len = cblk->passes[n - 1].rate;
  785. layer->data = cblk->data;
  786. } else {
  787. layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
  788. layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
  789. }
  790. if (final)
  791. cblk->numpassesinlayers = n;
  792. }
  793. }
  794. }
  795. }
  796. }
  797. }
  798. void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
  799. int layno;
  800. for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
  801. tcd_makelayer_fixed(tcd, layno, 1);
  802. }
  803. }
  804. void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
  805. int compno, resno, bandno, precno, cblkno, passno;
  806. opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
  807. tcd_tile->distolayer[layno] = 0; /* fixed_quality */
  808. for (compno = 0; compno < tcd_tile->numcomps; compno++) {
  809. opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
  810. for (resno = 0; resno < tilec->numresolutions; resno++) {
  811. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  812. for (bandno = 0; bandno < res->numbands; bandno++) {
  813. opj_tcd_band_t *band = &res->bands[bandno];
  814. for (precno = 0; precno < res->pw * res->ph; precno++) {
  815. opj_tcd_precinct_t *prc = &band->precincts[precno];
  816. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  817. opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
  818. opj_tcd_layer_t *layer = &cblk->layers[layno];
  819. int n;
  820. if (layno == 0) {
  821. cblk->numpassesinlayers = 0;
  822. }
  823. n = cblk->numpassesinlayers;
  824. for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
  825. int dr;
  826. double dd;
  827. opj_tcd_pass_t *pass = &cblk->passes[passno];
  828. if (n == 0) {
  829. dr = pass->rate;
  830. dd = pass->distortiondec;
  831. } else {
  832. dr = pass->rate - cblk->passes[n - 1].rate;
  833. dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
  834. }
  835. if (!dr) {
  836. if (dd != 0)
  837. n = passno + 1;
  838. continue;
  839. }
  840. if (dd / dr >= thresh)
  841. n = passno + 1;
  842. }
  843. layer->numpasses = n - cblk->numpassesinlayers;
  844. if (!layer->numpasses) {
  845. layer->disto = 0;
  846. continue;
  847. }
  848. if (cblk->numpassesinlayers == 0) {
  849. layer->len = cblk->passes[n - 1].rate;
  850. layer->data = cblk->data;
  851. layer->disto = cblk->passes[n - 1].distortiondec;
  852. } else {
  853. layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
  854. layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
  855. layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
  856. }
  857. tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
  858. if (final)
  859. cblk->numpassesinlayers = n;
  860. }
  861. }
  862. }
  863. }
  864. }
  865. }
  866. opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
  867. int compno, resno, bandno, precno, cblkno, passno, layno;
  868. double min, max;
  869. double cumdisto[100]; /* fixed_quality */
  870. const double K = 1; /* 1.1; fixed_quality */
  871. double maxSE = 0;
  872. opj_cp_t *cp = tcd->cp;
  873. opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
  874. opj_tcp_t *tcd_tcp = tcd->tcp;
  875. min = DBL_MAX;
  876. max = 0;
  877. tcd_tile->numpix = 0; /* fixed_quality */
  878. for (compno = 0; compno < tcd_tile->numcomps; compno++) {
  879. opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
  880. tilec->numpix = 0;
  881. for (resno = 0; resno < tilec->numresolutions; resno++) {
  882. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  883. for (bandno = 0; bandno < res->numbands; bandno++) {
  884. opj_tcd_band_t *band = &res->bands[bandno];
  885. for (precno = 0; precno < res->pw * res->ph; precno++) {
  886. opj_tcd_precinct_t *prc = &band->precincts[precno];
  887. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  888. opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
  889. for (passno = 0; passno < cblk->totalpasses; passno++) {
  890. opj_tcd_pass_t *pass = &cblk->passes[passno];
  891. int dr;
  892. double dd, rdslope;
  893. if (passno == 0) {
  894. dr = pass->rate;
  895. dd = pass->distortiondec;
  896. } else {
  897. dr = pass->rate - cblk->passes[passno - 1].rate;
  898. dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
  899. }
  900. if (dr == 0) {
  901. continue;
  902. }
  903. rdslope = dd / dr;
  904. if (rdslope < min) {
  905. min = rdslope;
  906. }
  907. if (rdslope > max) {
  908. max = rdslope;
  909. }
  910. } /* passno */
  911. /* fixed_quality */
  912. tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
  913. tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
  914. } /* cbklno */
  915. } /* precno */
  916. } /* bandno */
  917. } /* resno */
  918. maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
  919. * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
  920. * ((double)(tilec->numpix));
  921. } /* compno */
  922. /* index file */
  923. if(cstr_info) {
  924. opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
  925. tile_info->numpix = tcd_tile->numpix;
  926. tile_info->distotile = tcd_tile->distotile;
  927. tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
  928. }
  929. for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
  930. double lo = min;
  931. double hi = max;
  932. int success = 0;
  933. int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
  934. double goodthresh = 0;
  935. double stable_thresh = 0;
  936. int i;
  937. double distotarget; /* fixed_quality */
  938. /* fixed_quality */
  939. distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
  940. /* Don't try to find an optimal threshold but rather take everything not included yet, if
  941. -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
  942. -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
  943. ==> possible to have some lossy layers and the last layer for sure lossless */
  944. if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
  945. opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
  946. double thresh = 0;
  947. for (i = 0; i < 128; i++) {
  948. int l = 0;
  949. double distoachieved = 0; /* fixed_quality */
  950. thresh = (lo + hi) / 2;
  951. tcd_makelayer(tcd, layno, thresh, 0);
  952. if (cp->fixed_quality) { /* fixed_quality */
  953. if(cp->cinema){
  954. l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp);
  955. if (l == -999) {
  956. lo = thresh;
  957. continue;
  958. }else{
  959. distoachieved = layno == 0 ?
  960. tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
  961. if (distoachieved < distotarget) {
  962. hi=thresh;
  963. stable_thresh = thresh;
  964. continue;
  965. }else{
  966. lo=thresh;
  967. }
  968. }
  969. }else{
  970. distoachieved = (layno == 0) ?
  971. tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
  972. if (distoachieved < distotarget) {
  973. hi = thresh;
  974. stable_thresh = thresh;
  975. continue;
  976. }
  977. lo = thresh;
  978. }
  979. } else {
  980. l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp);
  981. /* TODO: what to do with l ??? seek / tell ??? */
  982. /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
  983. if (l == -999) {
  984. lo = thresh;
  985. continue;
  986. }
  987. hi = thresh;
  988. stable_thresh = thresh;
  989. }
  990. }
  991. success = 1;
  992. goodthresh = stable_thresh == 0? thresh : stable_thresh;
  993. t2_destroy(t2);
  994. } else {
  995. success = 1;
  996. goodthresh = min;
  997. }
  998. if (!success) {
  999. return OPJ_FALSE;
  1000. }
  1001. if(cstr_info) { /* Threshold for Marcela Index */
  1002. cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
  1003. }
  1004. tcd_makelayer(tcd, layno, goodthresh, 1);
  1005. /* fixed_quality */
  1006. cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
  1007. }
  1008. return OPJ_TRUE;
  1009. }
  1010. int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
  1011. int compno;
  1012. int l, i, numpacks = 0;
  1013. opj_tcd_tile_t *tile = NULL;
  1014. opj_tcp_t *tcd_tcp = NULL;
  1015. opj_cp_t *cp = NULL;
  1016. opj_tcp_t *tcp = &tcd->cp->tcps[0];
  1017. opj_tccp_t *tccp = &tcp->tccps[0];
  1018. opj_image_t *image = tcd->image;
  1019. opj_t1_t *t1 = NULL; /* T1 component */
  1020. opj_t2_t *t2 = NULL; /* T2 component */
  1021. tcd->tcd_tileno = tileno;
  1022. tcd->tcd_tile = tcd->tcd_image->tiles;
  1023. tcd->tcp = &tcd->cp->tcps[tileno];
  1024. tile = tcd->tcd_tile;
  1025. tcd_tcp = tcd->tcp;
  1026. cp = tcd->cp;
  1027. if(tcd->cur_tp_num == 0){
  1028. tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
  1029. /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
  1030. if(cstr_info) {
  1031. opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
  1032. for (i = 0; i < tilec_idx->numresolutions; i++) {
  1033. opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
  1034. cstr_info->tile[tileno].pw[i] = res_idx->pw;
  1035. cstr_info->tile[tileno].ph[i] = res_idx->ph;
  1036. numpacks += res_idx->pw * res_idx->ph;
  1037. cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
  1038. cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
  1039. }
  1040. cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
  1041. }
  1042. /* << INDEX */
  1043. /*---------------TILE-------------------*/
  1044. for (compno = 0; compno < tile->numcomps; compno++) {
  1045. int x, y;
  1046. int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
  1047. int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
  1048. int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
  1049. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1050. int tw = tilec->x1 - tilec->x0;
  1051. int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
  1052. /* extract tile data */
  1053. if (tcd_tcp->tccps[compno].qmfbid == 1) {
  1054. for (y = tilec->y0; y < tilec->y1; y++) {
  1055. /* start of the src tile scanline */
  1056. int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
  1057. /* start of the dst tile scanline */
  1058. int *tile_data = &tilec->data[(y - tilec->y0) * tw];
  1059. for (x = tilec->x0; x < tilec->x1; x++) {
  1060. *tile_data++ = *data++ - adjust;
  1061. }
  1062. }
  1063. } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
  1064. for (y = tilec->y0; y < tilec->y1; y++) {
  1065. /* start of the src tile scanline */
  1066. int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
  1067. /* start of the dst tile scanline */
  1068. int *tile_data = &tilec->data[(y - tilec->y0) * tw];
  1069. for (x = tilec->x0; x < tilec->x1; x++) {
  1070. *tile_data++ = (*data++ - adjust) << 11;
  1071. }
  1072. }
  1073. }
  1074. }
  1075. /*----------------MCT-------------------*/
  1076. if (tcd_tcp->mct) {
  1077. int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
  1078. if (tcd_tcp->tccps[0].qmfbid == 0) {
  1079. mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
  1080. } else {
  1081. mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
  1082. }
  1083. }
  1084. /*----------------DWT---------------------*/
  1085. for (compno = 0; compno < tile->numcomps; compno++) {
  1086. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1087. if (tcd_tcp->tccps[compno].qmfbid == 1) {
  1088. dwt_encode(tilec);
  1089. } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
  1090. dwt_encode_real(tilec);
  1091. }
  1092. }
  1093. /*------------------TIER1-----------------*/
  1094. t1 = t1_create(tcd->cinfo);
  1095. t1_encode_cblks(t1, tile, tcd_tcp);
  1096. t1_destroy(t1);
  1097. /*-----------RATE-ALLOCATE------------------*/
  1098. /* INDEX */
  1099. if(cstr_info) {
  1100. cstr_info->index_write = 0;
  1101. }
  1102. if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
  1103. /* Normal Rate/distortion allocation */
  1104. tcd_rateallocate(tcd, dest, len, cstr_info);
  1105. } else {
  1106. /* Fixed layer allocation */
  1107. tcd_rateallocate_fixed(tcd);
  1108. }
  1109. }
  1110. /*--------------TIER2------------------*/
  1111. /* INDEX */
  1112. if(cstr_info) {
  1113. cstr_info->index_write = 1;
  1114. }
  1115. t2 = t2_create(tcd->cinfo, image, cp);
  1116. l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, cstr_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS,tcd->cur_totnum_tp);
  1117. t2_destroy(t2);
  1118. /*---------------CLEAN-------------------*/
  1119. if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
  1120. tcd->encoding_time = opj_clock() - tcd->encoding_time;
  1121. opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
  1122. /* cleaning memory */
  1123. for (compno = 0; compno < tile->numcomps; compno++) {
  1124. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1125. opj_aligned_free(tilec->data);
  1126. }
  1127. }
  1128. return l;
  1129. }
  1130. opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
  1131. int l;
  1132. int compno;
  1133. int eof = 0;
  1134. double tile_time, t1_time, dwt_time;
  1135. opj_tcd_tile_t *tile = NULL;
  1136. opj_t1_t *t1 = NULL; /* T1 component */
  1137. opj_t2_t *t2 = NULL; /* T2 component */
  1138. tcd->tcd_tileno = tileno;
  1139. tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
  1140. tcd->tcp = &(tcd->cp->tcps[tileno]);
  1141. tile = tcd->tcd_tile;
  1142. tile_time = opj_clock(); /* time needed to decode a tile */
  1143. opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
  1144. /* INDEX >> */
  1145. if(cstr_info) {
  1146. int resno, compno, numprec = 0;
  1147. for (compno = 0; compno < cstr_info->numcomps; compno++) {
  1148. opj_tcp_t *tcp = &tcd->cp->tcps[0];
  1149. opj_tccp_t *tccp = &tcp->tccps[compno];
  1150. opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];
  1151. for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
  1152. opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
  1153. cstr_info->tile[tileno].pw[resno] = res_idx->pw;
  1154. cstr_info->tile[tileno].ph[resno] = res_idx->ph;
  1155. numprec += res_idx->pw * res_idx->ph;
  1156. if (tccp->csty & J2K_CP_CSTY_PRT) {
  1157. cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
  1158. cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
  1159. }
  1160. else {
  1161. cstr_info->tile[tileno].pdx[resno] = 15;
  1162. cstr_info->tile[tileno].pdy[resno] = 15;
  1163. }
  1164. }
  1165. }
  1166. cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
  1167. cstr_info->packno = 0;
  1168. }
  1169. /* << INDEX */
  1170. /*--------------TIER2------------------*/
  1171. t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
  1172. l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
  1173. t2_destroy(t2);
  1174. if (l == -999) {
  1175. eof = 1;
  1176. opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
  1177. return OPJ_FALSE;
  1178. }
  1179. /*------------------TIER1-----------------*/
  1180. t1_time = opj_clock(); /* time needed to decode a tile */
  1181. t1 = t1_create(tcd->cinfo);
  1182. if (t1 == NULL)
  1183. {
  1184. opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
  1185. t1_destroy(t1);
  1186. return OPJ_FALSE;
  1187. }
  1188. for (compno = 0; compno < tile->numcomps; ++compno) {
  1189. opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
  1190. /* The +3 is headroom required by the vectorized DWT */
  1191. tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
  1192. if (tilec->data == NULL)
  1193. {
  1194. opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
  1195. t1_destroy(t1);
  1196. return OPJ_FALSE;
  1197. }
  1198. t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
  1199. }
  1200. t1_destroy(t1);
  1201. t1_time = opj_clock() - t1_time;
  1202. opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
  1203. /*----------------DWT---------------------*/
  1204. dwt_time = opj_clock(); /* time needed to decode a tile */
  1205. for (compno = 0; compno < tile->numcomps; compno++) {
  1206. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1207. int numres2decode;
  1208. if (tcd->cp->reduce != 0) {
  1209. if ( tile->comps[compno].numresolutions < ( tcd->cp->reduce - 1 ) ) {
  1210. opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
  1211. " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
  1212. return OPJ_FALSE;
  1213. }
  1214. else {
  1215. tcd->image->comps[compno].resno_decoded =
  1216. tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
  1217. }
  1218. }
  1219. numres2decode = tcd->image->comps[compno].resno_decoded + 1;
  1220. if(numres2decode > 0){
  1221. if (tcd->tcp->tccps[compno].qmfbid == 1) {
  1222. dwt_decode(tilec, numres2decode);
  1223. } else {
  1224. dwt_decode_real(tilec, numres2decode);
  1225. }
  1226. }
  1227. }
  1228. dwt_time = opj_clock() - dwt_time;
  1229. opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
  1230. /*----------------MCT-------------------*/
  1231. if (tcd->tcp->mct) {
  1232. int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
  1233. if (tile->numcomps >= 3 ){
  1234. /* testcase 1336.pdf.asan.47.376 */
  1235. if ((tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0) < n ||
  1236. ( tile->comps[1].x1 - tile->comps[1].x0) * (tile->comps[1].y1 - tile->comps[1].y0) < n ||
  1237. ( tile->comps[2].x1 - tile->comps[2].x0) * (tile->comps[2].y1 - tile->comps[2].y0) < n) {
  1238. opj_event_msg(tcd->cinfo, EVT_ERROR, "Tiles don't all have the same dimension. Skip the MCT step.\n");
  1239. return OPJ_FALSE;
  1240. }
  1241. if (tcd->tcp->tccps[0].qmfbid == 1) {
  1242. mct_decode(
  1243. tile->comps[0].data,
  1244. tile->comps[1].data,
  1245. tile->comps[2].data,
  1246. n);
  1247. } else {
  1248. mct_decode_real(
  1249. (float*)tile->comps[0].data,
  1250. (float*)tile->comps[1].data,
  1251. (float*)tile->comps[2].data,
  1252. n);
  1253. }
  1254. } else{
  1255. opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
  1256. }
  1257. }
  1258. /*---------------TILE-------------------*/
  1259. for (compno = 0; compno < tile->numcomps; ++compno) {
  1260. opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
  1261. opj_image_comp_t* imagec = &tcd->image->comps[compno];
  1262. opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
  1263. int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
  1264. int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
  1265. int max = imagec->sgnd ? (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
  1266. int tw = tilec->x1 - tilec->x0;
  1267. int w = imagec->w;
  1268. int i, j;
  1269. int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
  1270. int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
  1271. /* NR-DEC-2977.pdf.asan.67.2198.jp2-52-decode */
  1272. if( res->x0 - offset_x < 0 || res->x1 - offset_x < 0
  1273. || res->y0 - offset_y < 0 || res->y1 - offset_y < 0 )
  1274. {
  1275. opj_event_msg(tcd->cinfo, EVT_ERROR, "Impossible offsets %d / %d\n", offset_x, offset_y);
  1276. return OPJ_FALSE;
  1277. }
  1278. assert( 0 <= res->x0 - offset_x && 0 <= res->x1 - offset_x );
  1279. assert( 0 <= res->y0 - offset_y && 0 <= res->y1 - offset_y );
  1280. if(!imagec->data){
  1281. imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
  1282. }
  1283. if (!imagec->data)
  1284. {
  1285. opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
  1286. return OPJ_FALSE;
  1287. }
  1288. if(tcd->tcp->tccps[compno].qmfbid == 1) {
  1289. for(j = res->y0; j < res->y1; ++j) {
  1290. for(i = res->x0; i < res->x1; ++i) {
  1291. int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
  1292. v += adjust;
  1293. /*assert( (i - offset_x) + (j - offset_y) * w >= 0 );*/
  1294. imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
  1295. }
  1296. }
  1297. }else{
  1298. for(j = res->y0; j < res->y1; ++j) {
  1299. for(i = res->x0; i < res->x1; ++i) {
  1300. float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
  1301. int v = lrintf(tmp);
  1302. v += adjust;
  1303. /*assert( (i - offset_x) + (j - offset_y) * w >= 0 );*/
  1304. imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
  1305. }
  1306. }
  1307. }
  1308. opj_aligned_free(tilec->data);
  1309. }
  1310. tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
  1311. opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
  1312. if (eof) {
  1313. return OPJ_FALSE;
  1314. }
  1315. return OPJ_TRUE;
  1316. }
  1317. void tcd_free_decode(opj_tcd_t *tcd) {
  1318. opj_tcd_image_t *tcd_image = tcd->tcd_image;
  1319. int i = 0;
  1320. for (i = 0; i < tcd_image->tw * tcd_image->th; i++)
  1321. {
  1322. tcd_free_decode_tile(tcd, i);
  1323. }
  1324. opj_free(tcd_image->tiles);
  1325. }
  1326. void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
  1327. int compno,resno,bandno,precno,cblkno;
  1328. opj_tcd_image_t *tcd_image = tcd->tcd_image;
  1329. opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
  1330. if (tile->comps != NULL) {
  1331. for (compno = 0; compno < tile->numcomps; compno++) {
  1332. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1333. for (resno = 0; resno < tilec->numresolutions; resno++) {
  1334. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  1335. for (bandno = 0; bandno < res->numbands; bandno++) {
  1336. opj_tcd_band_t *band = &res->bands[bandno];
  1337. for (precno = 0; precno < res->ph * res->pw; precno++) {
  1338. opj_tcd_precinct_t *prec = &band->precincts[precno];
  1339. if (prec->cblks.dec != NULL) {
  1340. for (cblkno = 0; cblkno < prec->cw * prec->ch; ++cblkno) {
  1341. opj_tcd_cblk_dec_t* cblk = &prec->cblks.dec[cblkno];
  1342. opj_free(cblk->data);
  1343. opj_free(cblk->segs);
  1344. }
  1345. opj_free(prec->cblks.dec);
  1346. }
  1347. if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
  1348. if (prec->incltree != NULL) tgt_destroy(prec->incltree);
  1349. }
  1350. opj_free(band->precincts);
  1351. }
  1352. }
  1353. opj_free(tilec->resolutions);
  1354. }
  1355. opj_free(tile->comps);
  1356. tile->comps = NULL;
  1357. }
  1358. }