tcd.c 56 KB

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