tcd.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
  8. * Copyright (c) 2002-2014, Professor Benoit Macq
  9. * Copyright (c) 2001-2003, David Janssens
  10. * Copyright (c) 2002-2003, Yannick Verschueren
  11. * Copyright (c) 2003-2007, Francois-Olivier Devaux
  12. * Copyright (c) 2003-2014, Antonin Descampe
  13. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  14. * Copyright (c) 2006-2007, Parvatha Elangovan
  15. * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
  16. * Copyright (c) 2012, CS Systemes d'Information, France
  17. * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
  18. * All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. * 1. Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  30. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  33. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  34. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  35. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  36. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  37. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  38. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40. */
  41. #include "opj_includes.h"
  42. #include "opj_common.h"
  43. // #define DEBUG_RATE_ALLOC
  44. /* ----------------------------------------------------------------------- */
  45. /* TODO MSD: */
  46. #ifdef TODO_MSD
  47. void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img)
  48. {
  49. int tileno, compno, resno, bandno, precno;/*, cblkno;*/
  50. fprintf(fd, "image {\n");
  51. fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
  52. img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0,
  53. tcd->image->y1);
  54. for (tileno = 0; tileno < img->th * img->tw; tileno++) {
  55. opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
  56. fprintf(fd, " tile {\n");
  57. fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
  58. tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
  59. for (compno = 0; compno < tile->numcomps; compno++) {
  60. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  61. fprintf(fd, " tilec {\n");
  62. fprintf(fd,
  63. " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
  64. tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
  65. for (resno = 0; resno < tilec->numresolutions; resno++) {
  66. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  67. fprintf(fd, "\n res {\n");
  68. fprintf(fd,
  69. " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
  70. res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
  71. for (bandno = 0; bandno < res->numbands; bandno++) {
  72. opj_tcd_band_t *band = &res->bands[bandno];
  73. fprintf(fd, " band {\n");
  74. fprintf(fd,
  75. " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
  76. band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
  77. for (precno = 0; precno < res->pw * res->ph; precno++) {
  78. opj_tcd_precinct_t *prec = &band->precincts[precno];
  79. fprintf(fd, " prec {\n");
  80. fprintf(fd,
  81. " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
  82. prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
  83. /*
  84. for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
  85. opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
  86. fprintf(fd, " cblk {\n");
  87. fprintf(fd,
  88. " x0=%d, y0=%d, x1=%d, y1=%d\n",
  89. cblk->x0, cblk->y0, cblk->x1, cblk->y1);
  90. fprintf(fd, " }\n");
  91. }
  92. */
  93. fprintf(fd, " }\n");
  94. }
  95. fprintf(fd, " }\n");
  96. }
  97. fprintf(fd, " }\n");
  98. }
  99. fprintf(fd, " }\n");
  100. }
  101. fprintf(fd, " }\n");
  102. }
  103. fprintf(fd, "}\n");
  104. }
  105. #endif
  106. /**
  107. * Initializes tile coding/decoding
  108. */
  109. static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
  110. OPJ_BOOL isEncoder, OPJ_SIZE_T sizeof_block,
  111. opj_event_mgr_t* manager);
  112. /**
  113. * Allocates memory for a decoding code block.
  114. */
  115. static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
  116. p_code_block);
  117. /**
  118. * Deallocates the decoding data of the given precinct.
  119. */
  120. static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct);
  121. /**
  122. * Allocates memory for an encoding code block (but not data).
  123. */
  124. static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
  125. p_code_block);
  126. /**
  127. * Allocates data for an encoding code block
  128. */
  129. static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
  130. p_code_block);
  131. /**
  132. * Deallocates the encoding data of the given precinct.
  133. */
  134. static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct);
  135. static
  136. void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno,
  137. OPJ_UINT32 final);
  138. /**
  139. Free the memory allocated for encoding
  140. @param tcd TCD handle
  141. */
  142. static void opj_tcd_free_tile(opj_tcd_t *tcd);
  143. static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
  144. OPJ_BYTE * p_src_data,
  145. OPJ_UINT32 * p_data_read,
  146. OPJ_UINT32 p_max_src_size,
  147. opj_codestream_index_t *p_cstr_index,
  148. opj_event_mgr_t *p_manager);
  149. static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd,
  150. opj_event_mgr_t *p_manager);
  151. static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd);
  152. static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd,
  153. opj_event_mgr_t *p_manager);
  154. static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd);
  155. static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd);
  156. static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd);
  157. static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd);
  158. static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd);
  159. static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
  160. OPJ_BYTE * p_dest_data,
  161. OPJ_UINT32 * p_data_written,
  162. OPJ_UINT32 p_max_dest_size,
  163. opj_codestream_info_t *p_cstr_info,
  164. opj_tcd_marker_info_t* p_marker_info,
  165. opj_event_mgr_t *p_manager);
  166. static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
  167. OPJ_BYTE * p_dest_data,
  168. OPJ_UINT32 p_max_dest_size,
  169. opj_codestream_info_t *p_cstr_info,
  170. opj_event_mgr_t *p_manager);
  171. static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *tcd,
  172. OPJ_UINT32 compno);
  173. /* ----------------------------------------------------------------------- */
  174. /**
  175. Create a new TCD handle
  176. */
  177. opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
  178. {
  179. opj_tcd_t *l_tcd = 00;
  180. /* create the tcd structure */
  181. l_tcd = (opj_tcd_t*) opj_calloc(1, sizeof(opj_tcd_t));
  182. if (!l_tcd) {
  183. return 00;
  184. }
  185. l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
  186. l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1, sizeof(opj_tcd_image_t));
  187. if (!l_tcd->tcd_image) {
  188. opj_free(l_tcd);
  189. return 00;
  190. }
  191. return l_tcd;
  192. }
  193. /* ----------------------------------------------------------------------- */
  194. static
  195. void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd)
  196. {
  197. OPJ_UINT32 layno;
  198. for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
  199. opj_tcd_makelayer_fixed(tcd, layno, 1);
  200. }
  201. }
  202. /* ----------------------------------------------------------------------- */
  203. /** Returns OPJ_TRUE if the layer allocation is unchanged w.r.t to the previous
  204. * invokation with a different threshold */
  205. static
  206. OPJ_BOOL opj_tcd_makelayer(opj_tcd_t *tcd,
  207. OPJ_UINT32 layno,
  208. OPJ_FLOAT64 thresh,
  209. OPJ_UINT32 final)
  210. {
  211. OPJ_UINT32 compno, resno, bandno, precno, cblkno;
  212. OPJ_UINT32 passno;
  213. opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
  214. OPJ_BOOL layer_allocation_is_same = OPJ_TRUE;
  215. tcd_tile->distolayer[layno] = 0;
  216. for (compno = 0; compno < tcd_tile->numcomps; compno++) {
  217. opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
  218. for (resno = 0; resno < tilec->numresolutions; resno++) {
  219. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  220. for (bandno = 0; bandno < res->numbands; bandno++) {
  221. opj_tcd_band_t *band = &res->bands[bandno];
  222. /* Skip empty bands */
  223. if (opj_tcd_is_band_empty(band)) {
  224. continue;
  225. }
  226. for (precno = 0; precno < res->pw * res->ph; precno++) {
  227. opj_tcd_precinct_t *prc = &band->precincts[precno];
  228. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  229. opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
  230. opj_tcd_layer_t *layer = &cblk->layers[layno];
  231. OPJ_UINT32 n;
  232. if (layno == 0) {
  233. cblk->numpassesinlayers = 0;
  234. }
  235. n = cblk->numpassesinlayers;
  236. if (thresh < 0) {
  237. /* Special value to indicate to use all passes */
  238. n = cblk->totalpasses;
  239. } else {
  240. for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
  241. OPJ_UINT32 dr;
  242. OPJ_FLOAT64 dd;
  243. opj_tcd_pass_t *pass = &cblk->passes[passno];
  244. if (n == 0) {
  245. dr = pass->rate;
  246. dd = pass->distortiondec;
  247. } else {
  248. dr = pass->rate - cblk->passes[n - 1].rate;
  249. dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
  250. }
  251. if (!dr) {
  252. if (dd != 0) {
  253. n = passno + 1;
  254. }
  255. continue;
  256. }
  257. if (thresh - (dd / dr) <
  258. DBL_EPSILON) { /* do not rely on float equality, check with DBL_EPSILON margin */
  259. n = passno + 1;
  260. }
  261. }
  262. }
  263. if (layer->numpasses != n - cblk->numpassesinlayers) {
  264. layer_allocation_is_same = OPJ_FALSE;
  265. layer->numpasses = n - cblk->numpassesinlayers;
  266. }
  267. if (!layer->numpasses) {
  268. layer->disto = 0;
  269. continue;
  270. }
  271. if (cblk->numpassesinlayers == 0) {
  272. layer->len = cblk->passes[n - 1].rate;
  273. layer->data = cblk->data;
  274. layer->disto = cblk->passes[n - 1].distortiondec;
  275. } else {
  276. layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
  277. 1].rate;
  278. layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
  279. layer->disto = cblk->passes[n - 1].distortiondec -
  280. cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
  281. }
  282. tcd_tile->distolayer[layno] += layer->disto;
  283. if (final) {
  284. cblk->numpassesinlayers = n;
  285. }
  286. }
  287. }
  288. }
  289. }
  290. }
  291. return layer_allocation_is_same;
  292. }
  293. /** For m_quality_layer_alloc_strategy == FIXED_LAYER */
  294. static
  295. void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno,
  296. OPJ_UINT32 final)
  297. {
  298. OPJ_UINT32 compno, resno, bandno, precno, cblkno;
  299. OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
  300. OPJ_INT32 matrice[J2K_TCD_MATRIX_MAX_LAYER_COUNT][J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT][3];
  301. OPJ_UINT32 i, j, k;
  302. opj_cp_t *cp = tcd->cp;
  303. opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
  304. opj_tcp_t *tcd_tcp = tcd->tcp;
  305. for (compno = 0; compno < tcd_tile->numcomps; compno++) {
  306. opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
  307. for (i = 0; i < tcd_tcp->numlayers; i++) {
  308. for (j = 0; j < tilec->numresolutions; j++) {
  309. for (k = 0; k < 3; k++) {
  310. matrice[i][j][k] =
  311. (OPJ_INT32)((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i *
  312. tilec->numresolutions * 3 + j * 3 + k]
  313. * (OPJ_FLOAT32)(tcd->image->comps[compno].prec / 16.0));
  314. }
  315. }
  316. }
  317. for (resno = 0; resno < tilec->numresolutions; resno++) {
  318. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  319. for (bandno = 0; bandno < res->numbands; bandno++) {
  320. opj_tcd_band_t *band = &res->bands[bandno];
  321. /* Skip empty bands */
  322. if (opj_tcd_is_band_empty(band)) {
  323. continue;
  324. }
  325. for (precno = 0; precno < res->pw * res->ph; precno++) {
  326. opj_tcd_precinct_t *prc = &band->precincts[precno];
  327. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  328. opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
  329. opj_tcd_layer_t *layer = &cblk->layers[layno];
  330. OPJ_UINT32 n;
  331. OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec -
  332. cblk->numbps); /* number of bit-plan equal to zero */
  333. /* Correction of the matrix of coefficient to include the IMSB information */
  334. if (layno == 0) {
  335. value = matrice[layno][resno][bandno];
  336. if (imsb >= value) {
  337. value = 0;
  338. } else {
  339. value -= imsb;
  340. }
  341. } else {
  342. value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
  343. if (imsb >= matrice[layno - 1][resno][bandno]) {
  344. value -= (imsb - matrice[layno - 1][resno][bandno]);
  345. if (value < 0) {
  346. value = 0;
  347. }
  348. }
  349. }
  350. if (layno == 0) {
  351. cblk->numpassesinlayers = 0;
  352. }
  353. n = cblk->numpassesinlayers;
  354. if (cblk->numpassesinlayers == 0) {
  355. if (value != 0) {
  356. n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
  357. } else {
  358. n = cblk->numpassesinlayers;
  359. }
  360. } else {
  361. n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
  362. }
  363. layer->numpasses = n - cblk->numpassesinlayers;
  364. if (!layer->numpasses) {
  365. continue;
  366. }
  367. if (cblk->numpassesinlayers == 0) {
  368. layer->len = cblk->passes[n - 1].rate;
  369. layer->data = cblk->data;
  370. } else {
  371. layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
  372. 1].rate;
  373. layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
  374. }
  375. if (final) {
  376. cblk->numpassesinlayers = n;
  377. }
  378. }
  379. }
  380. }
  381. }
  382. }
  383. }
  384. /** Rate allocation for the following methods:
  385. * - allocation by rate/distortio (m_quality_layer_alloc_strategy == RATE_DISTORTION_RATIO)
  386. * - allocation by fixed quality (m_quality_layer_alloc_strategy == FIXED_DISTORTION_RATIO)
  387. */
  388. static
  389. OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
  390. OPJ_BYTE *dest,
  391. OPJ_UINT32 * p_data_written,
  392. OPJ_UINT32 len,
  393. opj_codestream_info_t *cstr_info,
  394. opj_event_mgr_t *p_manager)
  395. {
  396. OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
  397. OPJ_UINT32 passno;
  398. OPJ_FLOAT64 min, max;
  399. OPJ_FLOAT64 cumdisto[100];
  400. const OPJ_FLOAT64 K = 1;
  401. OPJ_FLOAT64 maxSE = 0;
  402. opj_cp_t *cp = tcd->cp;
  403. opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
  404. opj_tcp_t *tcd_tcp = tcd->tcp;
  405. min = DBL_MAX;
  406. max = 0;
  407. tcd_tile->numpix = 0;
  408. for (compno = 0; compno < tcd_tile->numcomps; compno++) {
  409. opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
  410. tilec->numpix = 0;
  411. for (resno = 0; resno < tilec->numresolutions; resno++) {
  412. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  413. for (bandno = 0; bandno < res->numbands; bandno++) {
  414. opj_tcd_band_t *band = &res->bands[bandno];
  415. /* Skip empty bands */
  416. if (opj_tcd_is_band_empty(band)) {
  417. continue;
  418. }
  419. for (precno = 0; precno < res->pw * res->ph; precno++) {
  420. opj_tcd_precinct_t *prc = &band->precincts[precno];
  421. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  422. opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
  423. for (passno = 0; passno < cblk->totalpasses; passno++) {
  424. opj_tcd_pass_t *pass = &cblk->passes[passno];
  425. OPJ_INT32 dr;
  426. OPJ_FLOAT64 dd, rdslope;
  427. if (passno == 0) {
  428. dr = (OPJ_INT32)pass->rate;
  429. dd = pass->distortiondec;
  430. } else {
  431. dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
  432. dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
  433. }
  434. if (dr == 0) {
  435. continue;
  436. }
  437. rdslope = dd / dr;
  438. if (rdslope < min) {
  439. min = rdslope;
  440. }
  441. if (rdslope > max) {
  442. max = rdslope;
  443. }
  444. } /* passno */
  445. {
  446. const OPJ_SIZE_T cblk_pix_count = (OPJ_SIZE_T)((cblk->x1 - cblk->x0) *
  447. (cblk->y1 - cblk->y0));
  448. tcd_tile->numpix += cblk_pix_count;
  449. tilec->numpix += cblk_pix_count;
  450. }
  451. } /* cbklno */
  452. } /* precno */
  453. } /* bandno */
  454. } /* resno */
  455. maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
  456. * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0))
  457. * ((OPJ_FLOAT64)(tilec->numpix));
  458. } /* compno */
  459. /* index file */
  460. if (cstr_info) {
  461. opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
  462. tile_info->numpix = (int)tcd_tile->numpix;
  463. tile_info->distotile = (int)tcd_tile->distotile;
  464. tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(
  465. OPJ_FLOAT64));
  466. if (!tile_info->thresh) {
  467. /* FIXME event manager error callback */
  468. return OPJ_FALSE;
  469. }
  470. }
  471. for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
  472. OPJ_FLOAT64 lo = min;
  473. OPJ_FLOAT64 hi = max;
  474. OPJ_UINT32 maxlen = tcd_tcp->rates[layno] > 0.0f ? opj_uint_min(((
  475. OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
  476. OPJ_FLOAT64 goodthresh = 0;
  477. OPJ_FLOAT64 stable_thresh = 0;
  478. OPJ_UINT32 i;
  479. OPJ_FLOAT64 distotarget;
  480. distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10,
  481. tcd_tcp->distoratio[layno] / 10));
  482. /* Don't try to find an optimal threshold but rather take everything not included yet, if
  483. -r xx,yy,zz,0 (m_quality_layer_alloc_strategy == RATE_DISTORTION_RATIO and rates == NULL)
  484. -q xx,yy,zz,0 (m_quality_layer_alloc_strategy == FIXED_DISTORTION_RATIO and distoratio == NULL)
  485. ==> possible to have some lossy layers and the last layer for sure lossless */
  486. if (((cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy ==
  487. RATE_DISTORTION_RATIO) &&
  488. (tcd_tcp->rates[layno] > 0.0f)) ||
  489. ((cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy ==
  490. FIXED_DISTORTION_RATIO) &&
  491. (tcd_tcp->distoratio[layno] > 0.0))) {
  492. opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
  493. OPJ_FLOAT64 thresh = 0;
  494. OPJ_BOOL last_layer_allocation_ok = OPJ_FALSE;
  495. if (t2 == 00) {
  496. return OPJ_FALSE;
  497. }
  498. for (i = 0; i < 128; ++i) {
  499. OPJ_FLOAT64 distoachieved = 0;
  500. OPJ_BOOL layer_allocation_is_same;
  501. OPJ_FLOAT64 new_thresh = (lo + hi) / 2;
  502. /* Stop iterating when the threshold has stabilized enough */
  503. /* 0.5 * 1e-5 is somewhat arbitrary, but has been selected */
  504. /* so that this doesn't change the results of the regression */
  505. /* test suite. */
  506. if (fabs(new_thresh - thresh) <= 0.5 * 1e-5 * thresh) {
  507. break;
  508. }
  509. thresh = new_thresh;
  510. #ifdef DEBUG_RATE_ALLOC
  511. opj_event_msg(p_manager, EVT_INFO, "layno=%u, iter=%u, thresh=%g",
  512. layno, i, new_thresh);
  513. #endif
  514. layer_allocation_is_same = opj_tcd_makelayer(tcd, layno, thresh, 0) && i != 0;
  515. #ifdef DEBUG_RATE_ALLOC
  516. opj_event_msg(p_manager, EVT_INFO, "--> layer_allocation_is_same = %d",
  517. layer_allocation_is_same);
  518. #endif
  519. if (cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy ==
  520. FIXED_DISTORTION_RATIO) {
  521. if (OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)) {
  522. if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
  523. p_data_written, maxlen, cstr_info, NULL, tcd->cur_tp_num, tcd->tp_pos,
  524. tcd->cur_pino,
  525. THRESH_CALC, p_manager)) {
  526. lo = thresh;
  527. continue;
  528. } else {
  529. distoachieved = layno == 0 ?
  530. tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
  531. if (distoachieved < distotarget) {
  532. hi = thresh;
  533. stable_thresh = thresh;
  534. continue;
  535. } else {
  536. lo = thresh;
  537. }
  538. }
  539. } else {
  540. distoachieved = (layno == 0) ?
  541. tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
  542. if (distoachieved < distotarget) {
  543. hi = thresh;
  544. stable_thresh = thresh;
  545. continue;
  546. }
  547. lo = thresh;
  548. }
  549. } else { /* Disto/rate based optimization */
  550. /* Check if the layer allocation done by opj_tcd_makelayer()
  551. * is compatible of the maximum rate allocation. If not,
  552. * retry with a higher threshold.
  553. * If OK, try with a lower threshold.
  554. * Call opj_t2_encode_packets() only if opj_tcd_makelayer()
  555. * has resulted in different truncation points since its last
  556. * call. */
  557. if ((layer_allocation_is_same && !last_layer_allocation_ok) ||
  558. (!layer_allocation_is_same &&
  559. ! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
  560. p_data_written, maxlen, cstr_info, NULL, tcd->cur_tp_num, tcd->tp_pos,
  561. tcd->cur_pino,
  562. THRESH_CALC, p_manager))) {
  563. #ifdef DEBUG_RATE_ALLOC
  564. if (!layer_allocation_is_same) {
  565. opj_event_msg(p_manager, EVT_INFO,
  566. "--> check rate alloc failed (> maxlen=%u)\n", maxlen);
  567. }
  568. #endif
  569. last_layer_allocation_ok = OPJ_FALSE;
  570. lo = thresh;
  571. continue;
  572. }
  573. #ifdef DEBUG_RATE_ALLOC
  574. if (!layer_allocation_is_same) {
  575. opj_event_msg(p_manager, EVT_INFO,
  576. "--> check rate alloc success (len=%u <= maxlen=%u)\n", *p_data_written,
  577. maxlen);
  578. }
  579. #endif
  580. last_layer_allocation_ok = OPJ_TRUE;
  581. hi = thresh;
  582. stable_thresh = thresh;
  583. }
  584. }
  585. goodthresh = stable_thresh == 0 ? thresh : stable_thresh;
  586. opj_t2_destroy(t2);
  587. } else {
  588. /* Special value to indicate to use all passes */
  589. goodthresh = -1;
  590. }
  591. if (cstr_info) { /* Threshold for Marcela Index */
  592. cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
  593. }
  594. opj_tcd_makelayer(tcd, layno, goodthresh, 1);
  595. cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] :
  596. (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
  597. }
  598. return OPJ_TRUE;
  599. }
  600. OPJ_BOOL opj_tcd_init(opj_tcd_t *p_tcd,
  601. opj_image_t * p_image,
  602. opj_cp_t * p_cp,
  603. opj_thread_pool_t* p_tp)
  604. {
  605. p_tcd->image = p_image;
  606. p_tcd->cp = p_cp;
  607. p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,
  608. sizeof(opj_tcd_tile_t));
  609. if (! p_tcd->tcd_image->tiles) {
  610. return OPJ_FALSE;
  611. }
  612. p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(
  613. p_image->numcomps, sizeof(opj_tcd_tilecomp_t));
  614. if (! p_tcd->tcd_image->tiles->comps) {
  615. return OPJ_FALSE;
  616. }
  617. p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
  618. p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
  619. p_tcd->thread_pool = p_tp;
  620. return OPJ_TRUE;
  621. }
  622. /**
  623. Destroy a previously created TCD handle
  624. */
  625. void opj_tcd_destroy(opj_tcd_t *tcd)
  626. {
  627. if (tcd) {
  628. opj_tcd_free_tile(tcd);
  629. if (tcd->tcd_image) {
  630. opj_free(tcd->tcd_image);
  631. tcd->tcd_image = 00;
  632. }
  633. opj_free(tcd->used_component);
  634. opj_free(tcd);
  635. }
  636. }
  637. OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
  638. {
  639. if ((l_tilec->data == 00) ||
  640. ((l_tilec->data_size_needed > l_tilec->data_size) &&
  641. (l_tilec->ownsData == OPJ_FALSE))) {
  642. l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
  643. if (!l_tilec->data && l_tilec->data_size_needed != 0) {
  644. return OPJ_FALSE;
  645. }
  646. /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
  647. l_tilec->data_size = l_tilec->data_size_needed;
  648. l_tilec->ownsData = OPJ_TRUE;
  649. } else if (l_tilec->data_size_needed > l_tilec->data_size) {
  650. /* We don't need to keep old data */
  651. opj_image_data_free(l_tilec->data);
  652. l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
  653. if (! l_tilec->data) {
  654. l_tilec->data_size = 0;
  655. l_tilec->data_size_needed = 0;
  656. l_tilec->ownsData = OPJ_FALSE;
  657. return OPJ_FALSE;
  658. }
  659. /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
  660. l_tilec->data_size = l_tilec->data_size_needed;
  661. l_tilec->ownsData = OPJ_TRUE;
  662. }
  663. return OPJ_TRUE;
  664. }
  665. /* ----------------------------------------------------------------------- */
  666. static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
  667. OPJ_BOOL isEncoder, OPJ_SIZE_T sizeof_block,
  668. opj_event_mgr_t* manager)
  669. {
  670. OPJ_UINT32 compno, resno, bandno, precno, cblkno;
  671. opj_tcp_t * l_tcp = 00;
  672. opj_cp_t * l_cp = 00;
  673. opj_tcd_tile_t * l_tile = 00;
  674. opj_tccp_t *l_tccp = 00;
  675. opj_tcd_tilecomp_t *l_tilec = 00;
  676. opj_image_comp_t * l_image_comp = 00;
  677. opj_tcd_resolution_t *l_res = 00;
  678. opj_tcd_band_t *l_band = 00;
  679. opj_stepsize_t * l_step_size = 00;
  680. opj_tcd_precinct_t *l_current_precinct = 00;
  681. opj_image_t *l_image = 00;
  682. OPJ_UINT32 p, q;
  683. OPJ_UINT32 l_level_no;
  684. OPJ_UINT32 l_pdx, l_pdy;
  685. OPJ_INT32 l_x0b, l_y0b;
  686. OPJ_UINT32 l_tx0, l_ty0;
  687. /* extent of precincts , top left, bottom right**/
  688. OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
  689. /* number of precinct for a resolution */
  690. OPJ_UINT32 l_nb_precincts;
  691. /* room needed to store l_nb_precinct precinct for a resolution */
  692. OPJ_UINT32 l_nb_precinct_size;
  693. /* number of code blocks for a precinct*/
  694. OPJ_UINT32 l_nb_code_blocks;
  695. /* room needed to store l_nb_code_blocks code blocks for a precinct*/
  696. OPJ_UINT32 l_nb_code_blocks_size;
  697. /* size of data for a tile */
  698. OPJ_UINT32 l_data_size;
  699. l_cp = p_tcd->cp;
  700. l_tcp = &(l_cp->tcps[p_tile_no]);
  701. l_tile = p_tcd->tcd_image->tiles;
  702. l_tccp = l_tcp->tccps;
  703. l_tilec = l_tile->comps;
  704. l_image = p_tcd->image;
  705. l_image_comp = p_tcd->image->comps;
  706. p = p_tile_no % l_cp->tw; /* tile coordinates */
  707. q = p_tile_no / l_cp->tw;
  708. /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
  709. /* 4 borders of the tile rescale on the image if necessary */
  710. l_tx0 = l_cp->tx0 + p *
  711. l_cp->tdx; /* can't be greater than l_image->x1 so won't overflow */
  712. l_tile->x0 = (OPJ_INT32)opj_uint_max(l_tx0, l_image->x0);
  713. l_tile->x1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, l_cp->tdx),
  714. l_image->x1);
  715. /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
  716. if ((l_tile->x0 < 0) || (l_tile->x1 <= l_tile->x0)) {
  717. opj_event_msg(manager, EVT_ERROR, "Tile X coordinates are not supported\n");
  718. return OPJ_FALSE;
  719. }
  720. l_ty0 = l_cp->ty0 + q *
  721. l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */
  722. l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0);
  723. l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy),
  724. l_image->y1);
  725. /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
  726. if ((l_tile->y0 < 0) || (l_tile->y1 <= l_tile->y0)) {
  727. opj_event_msg(manager, EVT_ERROR, "Tile Y coordinates are not supported\n");
  728. return OPJ_FALSE;
  729. }
  730. /* testcase 1888.pdf.asan.35.988 */
  731. if (l_tccp->numresolutions == 0) {
  732. opj_event_msg(manager, EVT_ERROR, "tiles require at least one resolution\n");
  733. return OPJ_FALSE;
  734. }
  735. /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
  736. /*tile->numcomps = image->numcomps; */
  737. for (compno = 0; compno < l_tile->numcomps; ++compno) {
  738. /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
  739. l_image_comp->resno_decoded = 0;
  740. /* border of each l_tile component (global) */
  741. l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
  742. l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
  743. l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
  744. l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
  745. l_tilec->compno = compno;
  746. /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
  747. l_tilec->numresolutions = l_tccp->numresolutions;
  748. if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
  749. l_tilec->minimum_num_resolutions = 1;
  750. } else {
  751. l_tilec->minimum_num_resolutions = l_tccp->numresolutions -
  752. l_cp->m_specific_param.m_dec.m_reduce;
  753. }
  754. if (isEncoder) {
  755. OPJ_SIZE_T l_tile_data_size;
  756. /* compute l_data_size with overflow check */
  757. OPJ_SIZE_T w = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0);
  758. OPJ_SIZE_T h = (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0);
  759. /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
  760. if (h > 0 && w > SIZE_MAX / h) {
  761. opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
  762. return OPJ_FALSE;
  763. }
  764. l_tile_data_size = w * h;
  765. if (SIZE_MAX / sizeof(OPJ_UINT32) < l_tile_data_size) {
  766. opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
  767. return OPJ_FALSE;
  768. }
  769. l_tile_data_size = l_tile_data_size * sizeof(OPJ_UINT32);
  770. l_tilec->data_size_needed = l_tile_data_size;
  771. }
  772. l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(
  773. opj_tcd_resolution_t);
  774. opj_image_data_free(l_tilec->data_win);
  775. l_tilec->data_win = NULL;
  776. l_tilec->win_x0 = 0;
  777. l_tilec->win_y0 = 0;
  778. l_tilec->win_x1 = 0;
  779. l_tilec->win_y1 = 0;
  780. if (l_tilec->resolutions == 00) {
  781. l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
  782. if (! l_tilec->resolutions) {
  783. return OPJ_FALSE;
  784. }
  785. /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
  786. l_tilec->resolutions_size = l_data_size;
  787. memset(l_tilec->resolutions, 0, l_data_size);
  788. } else if (l_data_size > l_tilec->resolutions_size) {
  789. opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(
  790. l_tilec->resolutions, l_data_size);
  791. if (! new_resolutions) {
  792. opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n");
  793. opj_free(l_tilec->resolutions);
  794. l_tilec->resolutions = NULL;
  795. l_tilec->resolutions_size = 0;
  796. return OPJ_FALSE;
  797. }
  798. l_tilec->resolutions = new_resolutions;
  799. /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
  800. memset(((OPJ_BYTE*) l_tilec->resolutions) + l_tilec->resolutions_size, 0,
  801. l_data_size - l_tilec->resolutions_size);
  802. l_tilec->resolutions_size = l_data_size;
  803. }
  804. l_level_no = l_tilec->numresolutions;
  805. l_res = l_tilec->resolutions;
  806. l_step_size = l_tccp->stepsizes;
  807. /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
  808. for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
  809. /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
  810. OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
  811. OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
  812. OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
  813. --l_level_no;
  814. /* border for each resolution level (global) */
  815. l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
  816. l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
  817. l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
  818. l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
  819. /*fprintf(stderr, "\t\t\tres_x0= %d, res_y0 =%d, res_x1=%d, res_y1=%d\n", l_res->x0, l_res->y0, l_res->x1, l_res->y1);*/
  820. /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
  821. l_pdx = l_tccp->prcw[resno];
  822. l_pdy = l_tccp->prch[resno];
  823. /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
  824. /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
  825. l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
  826. l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
  827. {
  828. OPJ_UINT32 tmp = ((OPJ_UINT32)opj_int_ceildivpow2(l_res->x1,
  829. (OPJ_INT32)l_pdx)) << l_pdx;
  830. if (tmp > (OPJ_UINT32)INT_MAX) {
  831. opj_event_msg(manager, EVT_ERROR, "Integer overflow\n");
  832. return OPJ_FALSE;
  833. }
  834. l_br_prc_x_end = (OPJ_INT32)tmp;
  835. }
  836. {
  837. OPJ_UINT32 tmp = ((OPJ_UINT32)opj_int_ceildivpow2(l_res->y1,
  838. (OPJ_INT32)l_pdy)) << l_pdy;
  839. if (tmp > (OPJ_UINT32)INT_MAX) {
  840. opj_event_msg(manager, EVT_ERROR, "Integer overflow\n");
  841. return OPJ_FALSE;
  842. }
  843. l_br_prc_y_end = (OPJ_INT32)tmp;
  844. }
  845. /*fprintf(stderr, "\t\t\tprc_x_start=%d, prc_y_start=%d, br_prc_x_end=%d, br_prc_y_end=%d \n", l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end ,l_br_prc_y_end );*/
  846. l_res->pw = (l_res->x0 == l_res->x1) ? 0U : (OPJ_UINT32)((
  847. l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
  848. l_res->ph = (l_res->y0 == l_res->y1) ? 0U : (OPJ_UINT32)((
  849. l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
  850. /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
  851. if ((l_res->pw != 0U) && ((((OPJ_UINT32) - 1) / l_res->pw) < l_res->ph)) {
  852. opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
  853. return OPJ_FALSE;
  854. }
  855. l_nb_precincts = l_res->pw * l_res->ph;
  856. if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(opj_tcd_precinct_t)) <
  857. l_nb_precincts) {
  858. opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
  859. return OPJ_FALSE;
  860. }
  861. l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
  862. if (resno == 0) {
  863. tlcbgxstart = l_tl_prc_x_start;
  864. tlcbgystart = l_tl_prc_y_start;
  865. /*brcbgxend = l_br_prc_x_end;*/
  866. /* brcbgyend = l_br_prc_y_end;*/
  867. cbgwidthexpn = l_pdx;
  868. cbgheightexpn = l_pdy;
  869. l_res->numbands = 1;
  870. } else {
  871. tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
  872. tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
  873. /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
  874. /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
  875. cbgwidthexpn = l_pdx - 1;
  876. cbgheightexpn = l_pdy - 1;
  877. l_res->numbands = 3;
  878. }
  879. cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
  880. cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
  881. l_band = l_res->bands;
  882. for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band, ++l_step_size) {
  883. /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
  884. if (resno == 0) {
  885. l_band->bandno = 0 ;
  886. l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
  887. l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
  888. l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
  889. l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
  890. } else {
  891. l_band->bandno = bandno + 1;
  892. /* x0b = 1 if bandno = 1 or 3 */
  893. l_x0b = l_band->bandno & 1;
  894. /* y0b = 1 if bandno = 2 or 3 */
  895. l_y0b = (OPJ_INT32)((l_band->bandno) >> 1);
  896. /* l_band border (global) */
  897. l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b <<
  898. l_level_no), (OPJ_INT32)(l_level_no + 1));
  899. l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b <<
  900. l_level_no), (OPJ_INT32)(l_level_no + 1));
  901. l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b <<
  902. l_level_no), (OPJ_INT32)(l_level_no + 1));
  903. l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b <<
  904. l_level_no), (OPJ_INT32)(l_level_no + 1));
  905. }
  906. if (isEncoder) {
  907. /* Skip empty bands */
  908. if (opj_tcd_is_band_empty(l_band)) {
  909. /* Do not zero l_band->precints to avoid leaks */
  910. /* but make sure we don't use it later, since */
  911. /* it will point to precincts of previous bands... */
  912. continue;
  913. }
  914. }
  915. {
  916. /* Table E-1 - Sub-band gains */
  917. /* BUG_WEIRD_TWO_INVK (look for this identifier in dwt.c): */
  918. /* the test (!isEncoder && l_tccp->qmfbid == 0) is strongly */
  919. /* linked to the use of two_invK instead of invK */
  920. const OPJ_INT32 log2_gain = (!isEncoder &&
  921. l_tccp->qmfbid == 0) ? 0 : (l_band->bandno == 0) ? 0 :
  922. (l_band->bandno == 3) ? 2 : 1;
  923. /* Nominal dynamic range. Equation E-4 */
  924. const OPJ_INT32 Rb = (OPJ_INT32)l_image_comp->prec + log2_gain;
  925. /* Delta_b value of Equation E-3 in "E.1 Inverse quantization
  926. * procedure" of the standard */
  927. l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0,
  928. (OPJ_INT32)(Rb - l_step_size->expn))));
  929. }
  930. /* Mb value of Equation E-2 in "E.1 Inverse quantization
  931. * procedure" of the standard */
  932. l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits -
  933. 1;
  934. if (!l_band->precincts && (l_nb_precincts > 0U)) {
  935. l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */
  936. l_nb_precinct_size);
  937. if (! l_band->precincts) {
  938. opj_event_msg(manager, EVT_ERROR,
  939. "Not enough memory to handle band precints\n");
  940. return OPJ_FALSE;
  941. }
  942. /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */
  943. memset(l_band->precincts, 0, l_nb_precinct_size);
  944. l_band->precincts_data_size = l_nb_precinct_size;
  945. } else if (l_band->precincts_data_size < l_nb_precinct_size) {
  946. opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(
  947. l_band->precincts,/*3 * */ l_nb_precinct_size);
  948. if (! new_precincts) {
  949. opj_event_msg(manager, EVT_ERROR,
  950. "Not enough memory to handle band precints\n");
  951. opj_free(l_band->precincts);
  952. l_band->precincts = NULL;
  953. l_band->precincts_data_size = 0;
  954. return OPJ_FALSE;
  955. }
  956. l_band->precincts = new_precincts;
  957. /*fprintf(stderr, "\t\t\t\tReallocate precincts of a band (opj_tcd_precinct_t): from %d to %d\n",l_band->precincts_data_size, l_nb_precinct_size);*/
  958. memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size, 0,
  959. l_nb_precinct_size - l_band->precincts_data_size);
  960. l_band->precincts_data_size = l_nb_precinct_size;
  961. }
  962. l_current_precinct = l_band->precincts;
  963. for (precno = 0; precno < l_nb_precincts; ++precno) {
  964. OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
  965. OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) *
  966. (1 << cbgwidthexpn);
  967. OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) *
  968. (1 << cbgheightexpn);
  969. OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
  970. OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
  971. /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
  972. /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
  973. /* precinct size (global) */
  974. /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
  975. l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
  976. l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
  977. l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
  978. l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
  979. /*fprintf(stderr, "\t prc_x0=%d; prc_y0=%d, prc_x1=%d; prc_y1=%d\n",l_current_precinct->x0, l_current_precinct->y0 ,l_current_precinct->x1, l_current_precinct->y1);*/
  980. tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0,
  981. (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
  982. /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
  983. tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0,
  984. (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
  985. /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
  986. brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1,
  987. (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
  988. /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
  989. brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1,
  990. (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
  991. /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
  992. l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >>
  993. cblkwidthexpn);
  994. l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >>
  995. cblkheightexpn);
  996. l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
  997. /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */
  998. if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof_block) <
  999. l_nb_code_blocks) {
  1000. opj_event_msg(manager, EVT_ERROR,
  1001. "Size of code block data exceeds system limits\n");
  1002. return OPJ_FALSE;
  1003. }
  1004. l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
  1005. if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) {
  1006. l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
  1007. if (! l_current_precinct->cblks.blocks) {
  1008. return OPJ_FALSE;
  1009. }
  1010. /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
  1011. memset(l_current_precinct->cblks.blocks, 0, l_nb_code_blocks_size);
  1012. l_current_precinct->block_size = l_nb_code_blocks_size;
  1013. } else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
  1014. void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks,
  1015. l_nb_code_blocks_size);
  1016. if (! new_blocks) {
  1017. opj_free(l_current_precinct->cblks.blocks);
  1018. l_current_precinct->cblks.blocks = NULL;
  1019. l_current_precinct->block_size = 0;
  1020. opj_event_msg(manager, EVT_ERROR,
  1021. "Not enough memory for current precinct codeblock element\n");
  1022. return OPJ_FALSE;
  1023. }
  1024. l_current_precinct->cblks.blocks = new_blocks;
  1025. /*fprintf(stderr, "\t\t\t\tReallocate cblks of a precinct (opj_tcd_cblk_dec_t): from %d to %d\n",l_current_precinct->block_size, l_nb_code_blocks_size); */
  1026. memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) +
  1027. l_current_precinct->block_size
  1028. , 0
  1029. , l_nb_code_blocks_size - l_current_precinct->block_size);
  1030. l_current_precinct->block_size = l_nb_code_blocks_size;
  1031. }
  1032. if (! l_current_precinct->incltree) {
  1033. l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,
  1034. l_current_precinct->ch, manager);
  1035. } else {
  1036. l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,
  1037. l_current_precinct->cw, l_current_precinct->ch, manager);
  1038. }
  1039. if (! l_current_precinct->imsbtree) {
  1040. l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw,
  1041. l_current_precinct->ch, manager);
  1042. } else {
  1043. l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree,
  1044. l_current_precinct->cw, l_current_precinct->ch, manager);
  1045. }
  1046. for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
  1047. OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno %
  1048. l_current_precinct->cw) * (1 << cblkwidthexpn);
  1049. OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno /
  1050. l_current_precinct->cw) * (1 << cblkheightexpn);
  1051. OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
  1052. OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
  1053. if (isEncoder) {
  1054. opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
  1055. if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
  1056. return OPJ_FALSE;
  1057. }
  1058. /* code-block size (global) */
  1059. l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
  1060. l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
  1061. l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
  1062. l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
  1063. if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
  1064. return OPJ_FALSE;
  1065. }
  1066. } else {
  1067. opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
  1068. if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
  1069. return OPJ_FALSE;
  1070. }
  1071. /* code-block size (global) */
  1072. l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
  1073. l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
  1074. l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
  1075. l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
  1076. }
  1077. }
  1078. ++l_current_precinct;
  1079. } /* precno */
  1080. } /* bandno */
  1081. ++l_res;
  1082. } /* resno */
  1083. ++l_tccp;
  1084. ++l_tilec;
  1085. ++l_image_comp;
  1086. } /* compno */
  1087. return OPJ_TRUE;
  1088. }
  1089. OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
  1090. opj_event_mgr_t* p_manager)
  1091. {
  1092. return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE,
  1093. sizeof(opj_tcd_cblk_enc_t), p_manager);
  1094. }
  1095. OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
  1096. opj_event_mgr_t* p_manager)
  1097. {
  1098. return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE,
  1099. sizeof(opj_tcd_cblk_dec_t), p_manager);
  1100. }
  1101. /**
  1102. * Allocates memory for an encoding code block (but not data memory).
  1103. */
  1104. static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
  1105. p_code_block)
  1106. {
  1107. if (! p_code_block->layers) {
  1108. /* no memset since data */
  1109. p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100,
  1110. sizeof(opj_tcd_layer_t));
  1111. if (! p_code_block->layers) {
  1112. return OPJ_FALSE;
  1113. }
  1114. }
  1115. if (! p_code_block->passes) {
  1116. p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100,
  1117. sizeof(opj_tcd_pass_t));
  1118. if (! p_code_block->passes) {
  1119. return OPJ_FALSE;
  1120. }
  1121. }
  1122. return OPJ_TRUE;
  1123. }
  1124. /**
  1125. * Allocates data memory for an encoding code block.
  1126. */
  1127. static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
  1128. p_code_block)
  1129. {
  1130. OPJ_UINT32 l_data_size;
  1131. /* +1 is needed for https://github.com/uclouvain/openjpeg/issues/835 */
  1132. /* and actually +2 required for https://github.com/uclouvain/openjpeg/issues/982 */
  1133. /* and +7 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 3) */
  1134. /* and +26 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 7) */
  1135. /* and +28 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 44) */
  1136. /* and +33 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4) */
  1137. /* and +63 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4 -IMF 2K) */
  1138. /* and +74 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4 -n 8 -s 7,7 -I) */
  1139. /* TODO: is there a theoretical upper-bound for the compressed code */
  1140. /* block size ? */
  1141. l_data_size = 74 + (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) *
  1142. (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
  1143. if (l_data_size > p_code_block->data_size) {
  1144. if (p_code_block->data) {
  1145. /* We refer to data - 1 since below we incremented it */
  1146. opj_free(p_code_block->data - 1);
  1147. }
  1148. p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1);
  1149. if (! p_code_block->data) {
  1150. p_code_block->data_size = 0U;
  1151. return OPJ_FALSE;
  1152. }
  1153. p_code_block->data_size = l_data_size;
  1154. /* We reserve the initial byte as a fake byte to a non-FF value */
  1155. /* and increment the data pointer, so that opj_mqc_init_enc() */
  1156. /* can do bp = data - 1, and opj_mqc_byteout() can safely dereference */
  1157. /* it. */
  1158. p_code_block->data[0] = 0;
  1159. p_code_block->data += 1; /*why +1 ?*/
  1160. }
  1161. return OPJ_TRUE;
  1162. }
  1163. void opj_tcd_reinit_segment(opj_tcd_seg_t* seg)
  1164. {
  1165. memset(seg, 0, sizeof(opj_tcd_seg_t));
  1166. }
  1167. /**
  1168. * Allocates memory for a decoding code block.
  1169. */
  1170. static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
  1171. p_code_block)
  1172. {
  1173. if (! p_code_block->segs) {
  1174. p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,
  1175. sizeof(opj_tcd_seg_t));
  1176. if (! p_code_block->segs) {
  1177. return OPJ_FALSE;
  1178. }
  1179. /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
  1180. p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
  1181. /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
  1182. } else {
  1183. /* sanitize */
  1184. opj_tcd_seg_t * l_segs = p_code_block->segs;
  1185. OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
  1186. opj_tcd_seg_data_chunk_t* l_chunks = p_code_block->chunks;
  1187. OPJ_UINT32 l_numchunksalloc = p_code_block->numchunksalloc;
  1188. OPJ_UINT32 i;
  1189. opj_aligned_free(p_code_block->decoded_data);
  1190. p_code_block->decoded_data = 00;
  1191. memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
  1192. p_code_block->segs = l_segs;
  1193. p_code_block->m_current_max_segs = l_current_max_segs;
  1194. for (i = 0; i < l_current_max_segs; ++i) {
  1195. opj_tcd_reinit_segment(&l_segs[i]);
  1196. }
  1197. p_code_block->chunks = l_chunks;
  1198. p_code_block->numchunksalloc = l_numchunksalloc;
  1199. }
  1200. return OPJ_TRUE;
  1201. }
  1202. OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd,
  1203. OPJ_BOOL take_into_account_partial_decoding)
  1204. {
  1205. OPJ_UINT32 i;
  1206. OPJ_UINT32 l_data_size = 0;
  1207. opj_image_comp_t * l_img_comp = 00;
  1208. opj_tcd_tilecomp_t * l_tile_comp = 00;
  1209. opj_tcd_resolution_t * l_res = 00;
  1210. OPJ_UINT32 l_size_comp, l_remaining;
  1211. OPJ_UINT32 l_temp;
  1212. l_tile_comp = p_tcd->tcd_image->tiles->comps;
  1213. l_img_comp = p_tcd->image->comps;
  1214. for (i = 0; i < p_tcd->image->numcomps; ++i) {
  1215. OPJ_UINT32 w, h;
  1216. l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
  1217. l_remaining = l_img_comp->prec & 7; /* (%8) */
  1218. if (l_remaining) {
  1219. ++l_size_comp;
  1220. }
  1221. if (l_size_comp == 3) {
  1222. l_size_comp = 4;
  1223. }
  1224. l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
  1225. if (take_into_account_partial_decoding && !p_tcd->whole_tile_decoding) {
  1226. w = l_res->win_x1 - l_res->win_x0;
  1227. h = l_res->win_y1 - l_res->win_y0;
  1228. } else {
  1229. w = (OPJ_UINT32)(l_res->x1 - l_res->x0);
  1230. h = (OPJ_UINT32)(l_res->y1 - l_res->y0);
  1231. }
  1232. if (h > 0 && UINT_MAX / w < h) {
  1233. return UINT_MAX;
  1234. }
  1235. l_temp = w * h;
  1236. if (l_size_comp && UINT_MAX / l_size_comp < l_temp) {
  1237. return UINT_MAX;
  1238. }
  1239. l_temp *= l_size_comp;
  1240. if (l_temp > UINT_MAX - l_data_size) {
  1241. return UINT_MAX;
  1242. }
  1243. l_data_size += l_temp;
  1244. ++l_img_comp;
  1245. ++l_tile_comp;
  1246. }
  1247. return l_data_size;
  1248. }
  1249. OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
  1250. OPJ_UINT32 p_tile_no,
  1251. OPJ_BYTE *p_dest,
  1252. OPJ_UINT32 * p_data_written,
  1253. OPJ_UINT32 p_max_length,
  1254. opj_codestream_info_t *p_cstr_info,
  1255. opj_tcd_marker_info_t* p_marker_info,
  1256. opj_event_mgr_t *p_manager)
  1257. {
  1258. if (p_tcd->cur_tp_num == 0) {
  1259. p_tcd->tcd_tileno = p_tile_no;
  1260. p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
  1261. /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
  1262. if (p_cstr_info) {
  1263. OPJ_UINT32 l_num_packs = 0;
  1264. OPJ_UINT32 i;
  1265. opj_tcd_tilecomp_t *l_tilec_idx =
  1266. &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
  1267. opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
  1268. for (i = 0; i < l_tilec_idx->numresolutions; i++) {
  1269. opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
  1270. p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
  1271. p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
  1272. l_num_packs += l_res_idx->pw * l_res_idx->ph;
  1273. p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
  1274. p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
  1275. }
  1276. p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((
  1277. OPJ_SIZE_T)p_cstr_info->numcomps * (OPJ_SIZE_T)p_cstr_info->numlayers *
  1278. l_num_packs,
  1279. sizeof(opj_packet_info_t));
  1280. if (!p_cstr_info->tile[p_tile_no].packet) {
  1281. /* FIXME event manager error callback */
  1282. return OPJ_FALSE;
  1283. }
  1284. }
  1285. /* << INDEX */
  1286. /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
  1287. /*---------------TILE-------------------*/
  1288. if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
  1289. return OPJ_FALSE;
  1290. }
  1291. /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
  1292. /* FIXME _ProfStart(PGROUP_MCT); */
  1293. if (! opj_tcd_mct_encode(p_tcd)) {
  1294. return OPJ_FALSE;
  1295. }
  1296. /* FIXME _ProfStop(PGROUP_MCT); */
  1297. /* FIXME _ProfStart(PGROUP_DWT); */
  1298. if (! opj_tcd_dwt_encode(p_tcd)) {
  1299. return OPJ_FALSE;
  1300. }
  1301. /* FIXME _ProfStop(PGROUP_DWT); */
  1302. /* FIXME _ProfStart(PGROUP_T1); */
  1303. if (! opj_tcd_t1_encode(p_tcd)) {
  1304. return OPJ_FALSE;
  1305. }
  1306. /* FIXME _ProfStop(PGROUP_T1); */
  1307. /* FIXME _ProfStart(PGROUP_RATE); */
  1308. if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length,
  1309. p_cstr_info, p_manager)) {
  1310. return OPJ_FALSE;
  1311. }
  1312. /* FIXME _ProfStop(PGROUP_RATE); */
  1313. }
  1314. /*--------------TIER2------------------*/
  1315. /* INDEX */
  1316. if (p_cstr_info) {
  1317. p_cstr_info->index_write = 1;
  1318. }
  1319. /* FIXME _ProfStart(PGROUP_T2); */
  1320. if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
  1321. p_cstr_info, p_marker_info, p_manager)) {
  1322. return OPJ_FALSE;
  1323. }
  1324. /* FIXME _ProfStop(PGROUP_T2); */
  1325. /*---------------CLEAN-------------------*/
  1326. return OPJ_TRUE;
  1327. }
  1328. OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd,
  1329. OPJ_UINT32 win_x0,
  1330. OPJ_UINT32 win_y0,
  1331. OPJ_UINT32 win_x1,
  1332. OPJ_UINT32 win_y1,
  1333. OPJ_UINT32 numcomps_to_decode,
  1334. const OPJ_UINT32 *comps_indices,
  1335. OPJ_BYTE *p_src,
  1336. OPJ_UINT32 p_max_length,
  1337. OPJ_UINT32 p_tile_no,
  1338. opj_codestream_index_t *p_cstr_index,
  1339. opj_event_mgr_t *p_manager
  1340. )
  1341. {
  1342. OPJ_UINT32 l_data_read;
  1343. OPJ_UINT32 compno;
  1344. p_tcd->tcd_tileno = p_tile_no;
  1345. p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
  1346. p_tcd->win_x0 = win_x0;
  1347. p_tcd->win_y0 = win_y0;
  1348. p_tcd->win_x1 = win_x1;
  1349. p_tcd->win_y1 = win_y1;
  1350. p_tcd->whole_tile_decoding = OPJ_TRUE;
  1351. opj_free(p_tcd->used_component);
  1352. p_tcd->used_component = NULL;
  1353. if (numcomps_to_decode) {
  1354. OPJ_BOOL* used_component = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL),
  1355. p_tcd->image->numcomps);
  1356. if (used_component == NULL) {
  1357. return OPJ_FALSE;
  1358. }
  1359. for (compno = 0; compno < numcomps_to_decode; compno++) {
  1360. used_component[ comps_indices[compno] ] = OPJ_TRUE;
  1361. }
  1362. p_tcd->used_component = used_component;
  1363. }
  1364. for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
  1365. if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
  1366. continue;
  1367. }
  1368. if (!opj_tcd_is_whole_tilecomp_decoding(p_tcd, compno)) {
  1369. p_tcd->whole_tile_decoding = OPJ_FALSE;
  1370. break;
  1371. }
  1372. }
  1373. if (p_tcd->whole_tile_decoding) {
  1374. for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
  1375. opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
  1376. opj_tcd_resolution_t *l_res = &
  1377. (tilec->resolutions[tilec->minimum_num_resolutions - 1]);
  1378. OPJ_SIZE_T l_data_size;
  1379. /* compute l_data_size with overflow check */
  1380. OPJ_SIZE_T res_w = (OPJ_SIZE_T)(l_res->x1 - l_res->x0);
  1381. OPJ_SIZE_T res_h = (OPJ_SIZE_T)(l_res->y1 - l_res->y0);
  1382. if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
  1383. continue;
  1384. }
  1385. /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
  1386. if (res_h > 0 && res_w > SIZE_MAX / res_h) {
  1387. opj_event_msg(p_manager, EVT_ERROR,
  1388. "Size of tile data exceeds system limits\n");
  1389. return OPJ_FALSE;
  1390. }
  1391. l_data_size = res_w * res_h;
  1392. if (SIZE_MAX / sizeof(OPJ_UINT32) < l_data_size) {
  1393. opj_event_msg(p_manager, EVT_ERROR,
  1394. "Size of tile data exceeds system limits\n");
  1395. return OPJ_FALSE;
  1396. }
  1397. l_data_size *= sizeof(OPJ_UINT32);
  1398. tilec->data_size_needed = l_data_size;
  1399. if (!opj_alloc_tile_component_data(tilec)) {
  1400. opj_event_msg(p_manager, EVT_ERROR,
  1401. "Size of tile data exceeds system limits\n");
  1402. return OPJ_FALSE;
  1403. }
  1404. }
  1405. } else {
  1406. /* Compute restricted tile-component and tile-resolution coordinates */
  1407. /* of the window of interest, but defer the memory allocation until */
  1408. /* we know the resno_decoded */
  1409. for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
  1410. OPJ_UINT32 resno;
  1411. opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
  1412. opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
  1413. if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
  1414. continue;
  1415. }
  1416. /* Compute the intersection of the area of interest, expressed in tile coordinates */
  1417. /* with the tile coordinates */
  1418. tilec->win_x0 = opj_uint_max(
  1419. (OPJ_UINT32)tilec->x0,
  1420. opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx));
  1421. tilec->win_y0 = opj_uint_max(
  1422. (OPJ_UINT32)tilec->y0,
  1423. opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy));
  1424. tilec->win_x1 = opj_uint_min(
  1425. (OPJ_UINT32)tilec->x1,
  1426. opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx));
  1427. tilec->win_y1 = opj_uint_min(
  1428. (OPJ_UINT32)tilec->y1,
  1429. opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy));
  1430. if (tilec->win_x1 < tilec->win_x0 ||
  1431. tilec->win_y1 < tilec->win_y0) {
  1432. /* We should not normally go there. The circumstance is when */
  1433. /* the tile coordinates do not intersect the area of interest */
  1434. /* Upper level logic should not even try to decode that tile */
  1435. opj_event_msg(p_manager, EVT_ERROR,
  1436. "Invalid tilec->win_xxx values\n");
  1437. return OPJ_FALSE;
  1438. }
  1439. for (resno = 0; resno < tilec->numresolutions; ++resno) {
  1440. opj_tcd_resolution_t *res = tilec->resolutions + resno;
  1441. res->win_x0 = opj_uint_ceildivpow2(tilec->win_x0,
  1442. tilec->numresolutions - 1 - resno);
  1443. res->win_y0 = opj_uint_ceildivpow2(tilec->win_y0,
  1444. tilec->numresolutions - 1 - resno);
  1445. res->win_x1 = opj_uint_ceildivpow2(tilec->win_x1,
  1446. tilec->numresolutions - 1 - resno);
  1447. res->win_y1 = opj_uint_ceildivpow2(tilec->win_y1,
  1448. tilec->numresolutions - 1 - resno);
  1449. }
  1450. }
  1451. }
  1452. #ifdef TODO_MSD /* FIXME */
  1453. /* INDEX >> */
  1454. if (p_cstr_info) {
  1455. OPJ_UINT32 resno, compno, numprec = 0;
  1456. for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
  1457. opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
  1458. opj_tccp_t *tccp = &tcp->tccps[compno];
  1459. opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
  1460. for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
  1461. opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
  1462. p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
  1463. p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
  1464. numprec += res_idx->pw * res_idx->ph;
  1465. p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
  1466. p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
  1467. }
  1468. }
  1469. p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(
  1470. p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
  1471. p_cstr_info->packno = 0;
  1472. }
  1473. /* << INDEX */
  1474. #endif
  1475. /*--------------TIER2------------------*/
  1476. /* FIXME _ProfStart(PGROUP_T2); */
  1477. l_data_read = 0;
  1478. if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index,
  1479. p_manager)) {
  1480. return OPJ_FALSE;
  1481. }
  1482. /* FIXME _ProfStop(PGROUP_T2); */
  1483. /*------------------TIER1-----------------*/
  1484. /* FIXME _ProfStart(PGROUP_T1); */
  1485. if (! opj_tcd_t1_decode(p_tcd, p_manager)) {
  1486. return OPJ_FALSE;
  1487. }
  1488. /* FIXME _ProfStop(PGROUP_T1); */
  1489. /* For subtile decoding, now we know the resno_decoded, we can allocate */
  1490. /* the tile data buffer */
  1491. if (!p_tcd->whole_tile_decoding) {
  1492. for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
  1493. opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
  1494. opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
  1495. opj_tcd_resolution_t *res = tilec->resolutions + image_comp->resno_decoded;
  1496. OPJ_SIZE_T w = res->win_x1 - res->win_x0;
  1497. OPJ_SIZE_T h = res->win_y1 - res->win_y0;
  1498. OPJ_SIZE_T l_data_size;
  1499. opj_image_data_free(tilec->data_win);
  1500. tilec->data_win = NULL;
  1501. if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
  1502. continue;
  1503. }
  1504. if (w > 0 && h > 0) {
  1505. if (w > SIZE_MAX / h) {
  1506. opj_event_msg(p_manager, EVT_ERROR,
  1507. "Size of tile data exceeds system limits\n");
  1508. return OPJ_FALSE;
  1509. }
  1510. l_data_size = w * h;
  1511. if (l_data_size > SIZE_MAX / sizeof(OPJ_INT32)) {
  1512. opj_event_msg(p_manager, EVT_ERROR,
  1513. "Size of tile data exceeds system limits\n");
  1514. return OPJ_FALSE;
  1515. }
  1516. l_data_size *= sizeof(OPJ_INT32);
  1517. tilec->data_win = (OPJ_INT32*) opj_image_data_alloc(l_data_size);
  1518. if (tilec->data_win == NULL) {
  1519. opj_event_msg(p_manager, EVT_ERROR,
  1520. "Size of tile data exceeds system limits\n");
  1521. return OPJ_FALSE;
  1522. }
  1523. }
  1524. }
  1525. }
  1526. /*----------------DWT---------------------*/
  1527. /* FIXME _ProfStart(PGROUP_DWT); */
  1528. if
  1529. (! opj_tcd_dwt_decode(p_tcd)) {
  1530. return OPJ_FALSE;
  1531. }
  1532. /* FIXME _ProfStop(PGROUP_DWT); */
  1533. /*----------------MCT-------------------*/
  1534. /* FIXME _ProfStart(PGROUP_MCT); */
  1535. if
  1536. (! opj_tcd_mct_decode(p_tcd, p_manager)) {
  1537. return OPJ_FALSE;
  1538. }
  1539. /* FIXME _ProfStop(PGROUP_MCT); */
  1540. /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
  1541. if
  1542. (! opj_tcd_dc_level_shift_decode(p_tcd)) {
  1543. return OPJ_FALSE;
  1544. }
  1545. /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
  1546. /*---------------TILE-------------------*/
  1547. return OPJ_TRUE;
  1548. }
  1549. OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd,
  1550. OPJ_BYTE * p_dest,
  1551. OPJ_UINT32 p_dest_length
  1552. )
  1553. {
  1554. OPJ_UINT32 i, j, k, l_data_size = 0;
  1555. opj_image_comp_t * l_img_comp = 00;
  1556. opj_tcd_tilecomp_t * l_tilec = 00;
  1557. opj_tcd_resolution_t * l_res;
  1558. OPJ_UINT32 l_size_comp, l_remaining;
  1559. OPJ_UINT32 l_stride, l_width, l_height;
  1560. l_data_size = opj_tcd_get_decoded_tile_size(p_tcd, OPJ_TRUE);
  1561. if (l_data_size == UINT_MAX || l_data_size > p_dest_length) {
  1562. return OPJ_FALSE;
  1563. }
  1564. l_tilec = p_tcd->tcd_image->tiles->comps;
  1565. l_img_comp = p_tcd->image->comps;
  1566. for (i = 0; i < p_tcd->image->numcomps; ++i) {
  1567. const OPJ_INT32* l_src_data;
  1568. l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
  1569. l_remaining = l_img_comp->prec & 7; /* (%8) */
  1570. l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
  1571. if (p_tcd->whole_tile_decoding) {
  1572. l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
  1573. l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
  1574. l_stride = (OPJ_UINT32)(l_tilec->resolutions[l_tilec->minimum_num_resolutions -
  1575. 1].x1 -
  1576. l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0) - l_width;
  1577. l_src_data = l_tilec->data;
  1578. } else {
  1579. l_width = l_res->win_x1 - l_res->win_x0;
  1580. l_height = l_res->win_y1 - l_res->win_y0;
  1581. l_stride = 0;
  1582. l_src_data = l_tilec->data_win;
  1583. }
  1584. if (l_remaining) {
  1585. ++l_size_comp;
  1586. }
  1587. if (l_size_comp == 3) {
  1588. l_size_comp = 4;
  1589. }
  1590. switch (l_size_comp) {
  1591. case 1: {
  1592. OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
  1593. const OPJ_INT32 * l_src_ptr = l_src_data;
  1594. if (l_img_comp->sgnd) {
  1595. for (j = 0; j < l_height; ++j) {
  1596. for (k = 0; k < l_width; ++k) {
  1597. *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++));
  1598. }
  1599. l_src_ptr += l_stride;
  1600. }
  1601. } else {
  1602. for (j = 0; j < l_height; ++j) {
  1603. for (k = 0; k < l_width; ++k) {
  1604. *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff);
  1605. }
  1606. l_src_ptr += l_stride;
  1607. }
  1608. }
  1609. p_dest = (OPJ_BYTE *)l_dest_ptr;
  1610. }
  1611. break;
  1612. case 2: {
  1613. const OPJ_INT32 * l_src_ptr = l_src_data;
  1614. OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
  1615. if (l_img_comp->sgnd) {
  1616. for (j = 0; j < l_height; ++j) {
  1617. for (k = 0; k < l_width; ++k) {
  1618. OPJ_INT16 val = (OPJ_INT16)(*(l_src_ptr++));
  1619. memcpy(l_dest_ptr, &val, sizeof(val));
  1620. l_dest_ptr ++;
  1621. }
  1622. l_src_ptr += l_stride;
  1623. }
  1624. } else {
  1625. for (j = 0; j < l_height; ++j) {
  1626. for (k = 0; k < l_width; ++k) {
  1627. OPJ_INT16 val = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
  1628. memcpy(l_dest_ptr, &val, sizeof(val));
  1629. l_dest_ptr ++;
  1630. }
  1631. l_src_ptr += l_stride;
  1632. }
  1633. }
  1634. p_dest = (OPJ_BYTE*) l_dest_ptr;
  1635. }
  1636. break;
  1637. case 4: {
  1638. OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
  1639. const OPJ_INT32 * l_src_ptr = l_src_data;
  1640. for (j = 0; j < l_height; ++j) {
  1641. memcpy(l_dest_ptr, l_src_ptr, l_width * sizeof(OPJ_INT32));
  1642. l_dest_ptr += l_width;
  1643. l_src_ptr += l_width + l_stride;
  1644. }
  1645. p_dest = (OPJ_BYTE*) l_dest_ptr;
  1646. }
  1647. break;
  1648. }
  1649. ++l_img_comp;
  1650. ++l_tilec;
  1651. }
  1652. return OPJ_TRUE;
  1653. }
  1654. static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
  1655. {
  1656. OPJ_UINT32 compno, resno, bandno, precno;
  1657. opj_tcd_tile_t *l_tile = 00;
  1658. opj_tcd_tilecomp_t *l_tile_comp = 00;
  1659. opj_tcd_resolution_t *l_res = 00;
  1660. opj_tcd_band_t *l_band = 00;
  1661. opj_tcd_precinct_t *l_precinct = 00;
  1662. OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
  1663. void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00;
  1664. if (! p_tcd) {
  1665. return;
  1666. }
  1667. if (! p_tcd->tcd_image) {
  1668. return;
  1669. }
  1670. if (p_tcd->m_is_decoder) {
  1671. l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
  1672. } else {
  1673. l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
  1674. }
  1675. l_tile = p_tcd->tcd_image->tiles;
  1676. if (! l_tile) {
  1677. return;
  1678. }
  1679. l_tile_comp = l_tile->comps;
  1680. for (compno = 0; compno < l_tile->numcomps; ++compno) {
  1681. l_res = l_tile_comp->resolutions;
  1682. if (l_res) {
  1683. l_nb_resolutions = l_tile_comp->resolutions_size / (OPJ_UINT32)sizeof(
  1684. opj_tcd_resolution_t);
  1685. for (resno = 0; resno < l_nb_resolutions; ++resno) {
  1686. l_band = l_res->bands;
  1687. for (bandno = 0; bandno < 3; ++bandno) {
  1688. l_precinct = l_band->precincts;
  1689. if (l_precinct) {
  1690. l_nb_precincts = l_band->precincts_data_size / (OPJ_UINT32)sizeof(
  1691. opj_tcd_precinct_t);
  1692. for (precno = 0; precno < l_nb_precincts; ++precno) {
  1693. opj_tgt_destroy(l_precinct->incltree);
  1694. l_precinct->incltree = 00;
  1695. opj_tgt_destroy(l_precinct->imsbtree);
  1696. l_precinct->imsbtree = 00;
  1697. (*l_tcd_code_block_deallocate)(l_precinct);
  1698. ++l_precinct;
  1699. }
  1700. opj_free(l_band->precincts);
  1701. l_band->precincts = 00;
  1702. }
  1703. ++l_band;
  1704. } /* for (resno */
  1705. ++l_res;
  1706. }
  1707. opj_free(l_tile_comp->resolutions);
  1708. l_tile_comp->resolutions = 00;
  1709. }
  1710. if (l_tile_comp->ownsData && l_tile_comp->data) {
  1711. opj_image_data_free(l_tile_comp->data);
  1712. l_tile_comp->data = 00;
  1713. l_tile_comp->ownsData = 0;
  1714. l_tile_comp->data_size = 0;
  1715. l_tile_comp->data_size_needed = 0;
  1716. }
  1717. opj_image_data_free(l_tile_comp->data_win);
  1718. ++l_tile_comp;
  1719. }
  1720. opj_free(l_tile->comps);
  1721. l_tile->comps = 00;
  1722. opj_free(p_tcd->tcd_image->tiles);
  1723. p_tcd->tcd_image->tiles = 00;
  1724. }
  1725. static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
  1726. OPJ_BYTE * p_src_data,
  1727. OPJ_UINT32 * p_data_read,
  1728. OPJ_UINT32 p_max_src_size,
  1729. opj_codestream_index_t *p_cstr_index,
  1730. opj_event_mgr_t *p_manager
  1731. )
  1732. {
  1733. opj_t2_t * l_t2;
  1734. l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
  1735. if (l_t2 == 00) {
  1736. return OPJ_FALSE;
  1737. }
  1738. if (! opj_t2_decode_packets(
  1739. p_tcd,
  1740. l_t2,
  1741. p_tcd->tcd_tileno,
  1742. p_tcd->tcd_image->tiles,
  1743. p_src_data,
  1744. p_data_read,
  1745. p_max_src_size,
  1746. p_cstr_index,
  1747. p_manager)) {
  1748. opj_t2_destroy(l_t2);
  1749. return OPJ_FALSE;
  1750. }
  1751. opj_t2_destroy(l_t2);
  1752. /*---------------CLEAN-------------------*/
  1753. return OPJ_TRUE;
  1754. }
  1755. static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
  1756. {
  1757. OPJ_UINT32 compno;
  1758. opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
  1759. opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
  1760. opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
  1761. volatile OPJ_BOOL ret = OPJ_TRUE;
  1762. OPJ_BOOL check_pterm = OPJ_FALSE;
  1763. opj_mutex_t* p_manager_mutex = NULL;
  1764. p_manager_mutex = opj_mutex_create();
  1765. /* Only enable PTERM check if we decode all layers */
  1766. if (p_tcd->tcp->num_layers_to_decode == p_tcd->tcp->numlayers &&
  1767. (l_tccp->cblksty & J2K_CCP_CBLKSTY_PTERM) != 0) {
  1768. check_pterm = OPJ_TRUE;
  1769. }
  1770. for (compno = 0; compno < l_tile->numcomps;
  1771. ++compno, ++l_tile_comp, ++l_tccp) {
  1772. if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
  1773. continue;
  1774. }
  1775. opj_t1_decode_cblks(p_tcd, &ret, l_tile_comp, l_tccp,
  1776. p_manager, p_manager_mutex, check_pterm);
  1777. if (!ret) {
  1778. break;
  1779. }
  1780. }
  1781. opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
  1782. if (p_manager_mutex) {
  1783. opj_mutex_destroy(p_manager_mutex);
  1784. }
  1785. return ret;
  1786. }
  1787. static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd)
  1788. {
  1789. OPJ_UINT32 compno;
  1790. opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
  1791. opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
  1792. opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
  1793. opj_image_comp_t * l_img_comp = p_tcd->image->comps;
  1794. for (compno = 0; compno < l_tile->numcomps;
  1795. compno++, ++l_tile_comp, ++l_img_comp, ++l_tccp) {
  1796. if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
  1797. continue;
  1798. }
  1799. if (l_tccp->qmfbid == 1) {
  1800. if (! opj_dwt_decode(p_tcd, l_tile_comp,
  1801. l_img_comp->resno_decoded + 1)) {
  1802. return OPJ_FALSE;
  1803. }
  1804. } else {
  1805. if (! opj_dwt_decode_real(p_tcd, l_tile_comp,
  1806. l_img_comp->resno_decoded + 1)) {
  1807. return OPJ_FALSE;
  1808. }
  1809. }
  1810. }
  1811. return OPJ_TRUE;
  1812. }
  1813. static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
  1814. {
  1815. opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
  1816. opj_tcp_t * l_tcp = p_tcd->tcp;
  1817. opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
  1818. OPJ_SIZE_T l_samples;
  1819. OPJ_UINT32 i;
  1820. if (l_tcp->mct == 0 || p_tcd->used_component != NULL) {
  1821. return OPJ_TRUE;
  1822. }
  1823. if (p_tcd->whole_tile_decoding) {
  1824. opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions +
  1825. l_tile_comp->minimum_num_resolutions - 1;
  1826. /* A bit inefficient: we process more data than needed if */
  1827. /* resno_decoded < l_tile_comp->minimum_num_resolutions-1, */
  1828. /* but we would need to take into account a stride then */
  1829. l_samples = (OPJ_SIZE_T)(res_comp0->x1 - res_comp0->x0) *
  1830. (OPJ_SIZE_T)(res_comp0->y1 - res_comp0->y0);
  1831. if (l_tile->numcomps >= 3) {
  1832. if (l_tile_comp->minimum_num_resolutions !=
  1833. l_tile->comps[1].minimum_num_resolutions ||
  1834. l_tile_comp->minimum_num_resolutions !=
  1835. l_tile->comps[2].minimum_num_resolutions) {
  1836. opj_event_msg(p_manager, EVT_ERROR,
  1837. "Tiles don't all have the same dimension. Skip the MCT step.\n");
  1838. return OPJ_FALSE;
  1839. }
  1840. }
  1841. if (l_tile->numcomps >= 3) {
  1842. opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions +
  1843. l_tile_comp->minimum_num_resolutions - 1;
  1844. opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions +
  1845. l_tile_comp->minimum_num_resolutions - 1;
  1846. /* testcase 1336.pdf.asan.47.376 */
  1847. if (p_tcd->image->comps[0].resno_decoded !=
  1848. p_tcd->image->comps[1].resno_decoded ||
  1849. p_tcd->image->comps[0].resno_decoded !=
  1850. p_tcd->image->comps[2].resno_decoded ||
  1851. (OPJ_SIZE_T)(res_comp1->x1 - res_comp1->x0) *
  1852. (OPJ_SIZE_T)(res_comp1->y1 - res_comp1->y0) != l_samples ||
  1853. (OPJ_SIZE_T)(res_comp2->x1 - res_comp2->x0) *
  1854. (OPJ_SIZE_T)(res_comp2->y1 - res_comp2->y0) != l_samples) {
  1855. opj_event_msg(p_manager, EVT_ERROR,
  1856. "Tiles don't all have the same dimension. Skip the MCT step.\n");
  1857. return OPJ_FALSE;
  1858. }
  1859. }
  1860. } else {
  1861. opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions +
  1862. p_tcd->image->comps[0].resno_decoded;
  1863. l_samples = (OPJ_SIZE_T)(res_comp0->win_x1 - res_comp0->win_x0) *
  1864. (OPJ_SIZE_T)(res_comp0->win_y1 - res_comp0->win_y0);
  1865. if (l_tile->numcomps >= 3) {
  1866. opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions +
  1867. p_tcd->image->comps[1].resno_decoded;
  1868. opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions +
  1869. p_tcd->image->comps[2].resno_decoded;
  1870. /* testcase 1336.pdf.asan.47.376 */
  1871. if (p_tcd->image->comps[0].resno_decoded !=
  1872. p_tcd->image->comps[1].resno_decoded ||
  1873. p_tcd->image->comps[0].resno_decoded !=
  1874. p_tcd->image->comps[2].resno_decoded ||
  1875. (OPJ_SIZE_T)(res_comp1->win_x1 - res_comp1->win_x0) *
  1876. (OPJ_SIZE_T)(res_comp1->win_y1 - res_comp1->win_y0) != l_samples ||
  1877. (OPJ_SIZE_T)(res_comp2->win_x1 - res_comp2->win_x0) *
  1878. (OPJ_SIZE_T)(res_comp2->win_y1 - res_comp2->win_y0) != l_samples) {
  1879. opj_event_msg(p_manager, EVT_ERROR,
  1880. "Tiles don't all have the same dimension. Skip the MCT step.\n");
  1881. return OPJ_FALSE;
  1882. }
  1883. }
  1884. }
  1885. if (l_tile->numcomps >= 3) {
  1886. if (l_tcp->mct == 2) {
  1887. OPJ_BYTE ** l_data;
  1888. if (! l_tcp->m_mct_decoding_matrix) {
  1889. return OPJ_TRUE;
  1890. }
  1891. l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
  1892. if (! l_data) {
  1893. return OPJ_FALSE;
  1894. }
  1895. for (i = 0; i < l_tile->numcomps; ++i) {
  1896. if (p_tcd->whole_tile_decoding) {
  1897. l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
  1898. } else {
  1899. l_data[i] = (OPJ_BYTE*) l_tile_comp->data_win;
  1900. }
  1901. ++l_tile_comp;
  1902. }
  1903. if (! opj_mct_decode_custom(/* MCT data */
  1904. (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
  1905. /* size of components */
  1906. l_samples,
  1907. /* components */
  1908. l_data,
  1909. /* nb of components (i.e. size of pData) */
  1910. l_tile->numcomps,
  1911. /* tells if the data is signed */
  1912. p_tcd->image->comps->sgnd)) {
  1913. opj_free(l_data);
  1914. return OPJ_FALSE;
  1915. }
  1916. opj_free(l_data);
  1917. } else {
  1918. if (l_tcp->tccps->qmfbid == 1) {
  1919. if (p_tcd->whole_tile_decoding) {
  1920. opj_mct_decode(l_tile->comps[0].data,
  1921. l_tile->comps[1].data,
  1922. l_tile->comps[2].data,
  1923. l_samples);
  1924. } else {
  1925. opj_mct_decode(l_tile->comps[0].data_win,
  1926. l_tile->comps[1].data_win,
  1927. l_tile->comps[2].data_win,
  1928. l_samples);
  1929. }
  1930. } else {
  1931. if (p_tcd->whole_tile_decoding) {
  1932. opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
  1933. (OPJ_FLOAT32*)l_tile->comps[1].data,
  1934. (OPJ_FLOAT32*)l_tile->comps[2].data,
  1935. l_samples);
  1936. } else {
  1937. opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data_win,
  1938. (OPJ_FLOAT32*)l_tile->comps[1].data_win,
  1939. (OPJ_FLOAT32*)l_tile->comps[2].data_win,
  1940. l_samples);
  1941. }
  1942. }
  1943. }
  1944. } else {
  1945. opj_event_msg(p_manager, EVT_ERROR,
  1946. "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
  1947. l_tile->numcomps);
  1948. }
  1949. return OPJ_TRUE;
  1950. }
  1951. static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
  1952. {
  1953. OPJ_UINT32 compno;
  1954. opj_tcd_tilecomp_t * l_tile_comp = 00;
  1955. opj_tccp_t * l_tccp = 00;
  1956. opj_image_comp_t * l_img_comp = 00;
  1957. opj_tcd_resolution_t* l_res = 00;
  1958. opj_tcd_tile_t * l_tile;
  1959. OPJ_UINT32 l_width, l_height, i, j;
  1960. OPJ_INT32 * l_current_ptr;
  1961. OPJ_INT32 l_min, l_max;
  1962. OPJ_UINT32 l_stride;
  1963. l_tile = p_tcd->tcd_image->tiles;
  1964. l_tile_comp = l_tile->comps;
  1965. l_tccp = p_tcd->tcp->tccps;
  1966. l_img_comp = p_tcd->image->comps;
  1967. for (compno = 0; compno < l_tile->numcomps;
  1968. compno++, ++l_img_comp, ++l_tccp, ++l_tile_comp) {
  1969. if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
  1970. continue;
  1971. }
  1972. l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
  1973. if (!p_tcd->whole_tile_decoding) {
  1974. l_width = l_res->win_x1 - l_res->win_x0;
  1975. l_height = l_res->win_y1 - l_res->win_y0;
  1976. l_stride = 0;
  1977. l_current_ptr = l_tile_comp->data_win;
  1978. } else {
  1979. l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
  1980. l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
  1981. l_stride = (OPJ_UINT32)(
  1982. l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x1 -
  1983. l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x0)
  1984. - l_width;
  1985. l_current_ptr = l_tile_comp->data;
  1986. assert(l_height == 0 ||
  1987. l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
  1988. }
  1989. if (l_img_comp->sgnd) {
  1990. l_min = -(1 << (l_img_comp->prec - 1));
  1991. l_max = (1 << (l_img_comp->prec - 1)) - 1;
  1992. } else {
  1993. l_min = 0;
  1994. l_max = (OPJ_INT32)((1U << l_img_comp->prec) - 1);
  1995. }
  1996. if (l_width == 0 || l_height == 0) {
  1997. continue;
  1998. }
  1999. if (l_tccp->qmfbid == 1) {
  2000. for (j = 0; j < l_height; ++j) {
  2001. for (i = 0; i < l_width; ++i) {
  2002. /* TODO: do addition on int64 ? */
  2003. *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
  2004. l_max);
  2005. ++l_current_ptr;
  2006. }
  2007. l_current_ptr += l_stride;
  2008. }
  2009. } else {
  2010. for (j = 0; j < l_height; ++j) {
  2011. for (i = 0; i < l_width; ++i) {
  2012. OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
  2013. if (l_value > (OPJ_FLOAT32)INT_MAX) {
  2014. *l_current_ptr = l_max;
  2015. } else if (l_value < INT_MIN) {
  2016. *l_current_ptr = l_min;
  2017. } else {
  2018. /* Do addition on int64 to avoid overflows */
  2019. OPJ_INT64 l_value_int = (OPJ_INT64)opj_lrintf(l_value);
  2020. *l_current_ptr = (OPJ_INT32)opj_int64_clamp(
  2021. l_value_int + l_tccp->m_dc_level_shift, l_min, l_max);
  2022. }
  2023. ++l_current_ptr;
  2024. }
  2025. l_current_ptr += l_stride;
  2026. }
  2027. }
  2028. }
  2029. return OPJ_TRUE;
  2030. }
  2031. /**
  2032. * Deallocates the encoding data of the given precinct.
  2033. */
  2034. static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)
  2035. {
  2036. OPJ_UINT32 cblkno, l_nb_code_blocks;
  2037. opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
  2038. if (l_code_block) {
  2039. /*fprintf(stderr,"deallocate codeblock:{\n");*/
  2040. /*fprintf(stderr,"\t x0=%d, y0=%d, x1=%d, y1=%d\n",l_code_block->x0, l_code_block->y0, l_code_block->x1, l_code_block->y1);*/
  2041. /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
  2042. l_code_block->numbps, l_code_block->numlenbits, l_code_block->len, l_code_block->numnewpasses, l_code_block->real_num_segs, l_code_block->m_current_max_segs );*/
  2043. l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof(
  2044. opj_tcd_cblk_dec_t);
  2045. /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
  2046. for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
  2047. if (l_code_block->segs) {
  2048. opj_free(l_code_block->segs);
  2049. l_code_block->segs = 00;
  2050. }
  2051. if (l_code_block->chunks) {
  2052. opj_free(l_code_block->chunks);
  2053. l_code_block->chunks = 00;
  2054. }
  2055. opj_aligned_free(l_code_block->decoded_data);
  2056. l_code_block->decoded_data = NULL;
  2057. ++l_code_block;
  2058. }
  2059. opj_free(p_precinct->cblks.dec);
  2060. p_precinct->cblks.dec = 00;
  2061. }
  2062. }
  2063. /**
  2064. * Deallocates the encoding data of the given precinct.
  2065. */
  2066. static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)
  2067. {
  2068. OPJ_UINT32 cblkno, l_nb_code_blocks;
  2069. opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
  2070. if (l_code_block) {
  2071. l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof(
  2072. opj_tcd_cblk_enc_t);
  2073. for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
  2074. if (l_code_block->data) {
  2075. /* We refer to data - 1 since below we incremented it */
  2076. /* in opj_tcd_code_block_enc_allocate_data() */
  2077. opj_free(l_code_block->data - 1);
  2078. l_code_block->data = 00;
  2079. }
  2080. if (l_code_block->layers) {
  2081. opj_free(l_code_block->layers);
  2082. l_code_block->layers = 00;
  2083. }
  2084. if (l_code_block->passes) {
  2085. opj_free(l_code_block->passes);
  2086. l_code_block->passes = 00;
  2087. }
  2088. ++l_code_block;
  2089. }
  2090. opj_free(p_precinct->cblks.enc);
  2091. p_precinct->cblks.enc = 00;
  2092. }
  2093. }
  2094. OPJ_SIZE_T opj_tcd_get_encoder_input_buffer_size(opj_tcd_t *p_tcd)
  2095. {
  2096. OPJ_UINT32 i;
  2097. OPJ_SIZE_T l_data_size = 0;
  2098. opj_image_comp_t * l_img_comp = 00;
  2099. opj_tcd_tilecomp_t * l_tilec = 00;
  2100. OPJ_UINT32 l_size_comp, l_remaining;
  2101. l_tilec = p_tcd->tcd_image->tiles->comps;
  2102. l_img_comp = p_tcd->image->comps;
  2103. for (i = 0; i < p_tcd->image->numcomps; ++i) {
  2104. l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
  2105. l_remaining = l_img_comp->prec & 7; /* (%8) */
  2106. if (l_remaining) {
  2107. ++l_size_comp;
  2108. }
  2109. if (l_size_comp == 3) {
  2110. l_size_comp = 4;
  2111. }
  2112. l_data_size += l_size_comp * ((OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) *
  2113. (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0));
  2114. ++l_img_comp;
  2115. ++l_tilec;
  2116. }
  2117. return l_data_size;
  2118. }
  2119. static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd)
  2120. {
  2121. OPJ_UINT32 compno;
  2122. opj_tcd_tilecomp_t * l_tile_comp = 00;
  2123. opj_tccp_t * l_tccp = 00;
  2124. opj_image_comp_t * l_img_comp = 00;
  2125. opj_tcd_tile_t * l_tile;
  2126. OPJ_SIZE_T l_nb_elem, i;
  2127. OPJ_INT32 * l_current_ptr;
  2128. l_tile = p_tcd->tcd_image->tiles;
  2129. l_tile_comp = l_tile->comps;
  2130. l_tccp = p_tcd->tcp->tccps;
  2131. l_img_comp = p_tcd->image->comps;
  2132. for (compno = 0; compno < l_tile->numcomps; compno++) {
  2133. l_current_ptr = l_tile_comp->data;
  2134. l_nb_elem = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) *
  2135. (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0);
  2136. if (l_tccp->qmfbid == 1) {
  2137. for (i = 0; i < l_nb_elem; ++i) {
  2138. *l_current_ptr -= l_tccp->m_dc_level_shift ;
  2139. ++l_current_ptr;
  2140. }
  2141. } else {
  2142. for (i = 0; i < l_nb_elem; ++i) {
  2143. *((OPJ_FLOAT32 *) l_current_ptr) = (OPJ_FLOAT32)(*l_current_ptr -
  2144. l_tccp->m_dc_level_shift);
  2145. ++l_current_ptr;
  2146. }
  2147. }
  2148. ++l_img_comp;
  2149. ++l_tccp;
  2150. ++l_tile_comp;
  2151. }
  2152. return OPJ_TRUE;
  2153. }
  2154. static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd)
  2155. {
  2156. opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
  2157. opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
  2158. OPJ_SIZE_T samples = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) *
  2159. (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0);
  2160. OPJ_UINT32 i;
  2161. OPJ_BYTE ** l_data = 00;
  2162. opj_tcp_t * l_tcp = p_tcd->tcp;
  2163. if (!p_tcd->tcp->mct) {
  2164. return OPJ_TRUE;
  2165. }
  2166. if (p_tcd->tcp->mct == 2) {
  2167. if (! p_tcd->tcp->m_mct_coding_matrix) {
  2168. return OPJ_TRUE;
  2169. }
  2170. l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
  2171. if (! l_data) {
  2172. return OPJ_FALSE;
  2173. }
  2174. for (i = 0; i < l_tile->numcomps; ++i) {
  2175. l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
  2176. ++l_tile_comp;
  2177. }
  2178. if (! opj_mct_encode_custom(/* MCT data */
  2179. (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
  2180. /* size of components */
  2181. samples,
  2182. /* components */
  2183. l_data,
  2184. /* nb of components (i.e. size of pData) */
  2185. l_tile->numcomps,
  2186. /* tells if the data is signed */
  2187. p_tcd->image->comps->sgnd)) {
  2188. opj_free(l_data);
  2189. return OPJ_FALSE;
  2190. }
  2191. opj_free(l_data);
  2192. } else if (l_tcp->tccps->qmfbid == 0) {
  2193. opj_mct_encode_real(
  2194. (OPJ_FLOAT32*)l_tile->comps[0].data,
  2195. (OPJ_FLOAT32*)l_tile->comps[1].data,
  2196. (OPJ_FLOAT32*)l_tile->comps[2].data,
  2197. samples);
  2198. } else {
  2199. opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data,
  2200. l_tile->comps[2].data, samples);
  2201. }
  2202. return OPJ_TRUE;
  2203. }
  2204. static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd)
  2205. {
  2206. opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
  2207. opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
  2208. opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
  2209. OPJ_UINT32 compno;
  2210. for (compno = 0; compno < l_tile->numcomps; ++compno) {
  2211. if (l_tccp->qmfbid == 1) {
  2212. if (! opj_dwt_encode(p_tcd, l_tile_comp)) {
  2213. return OPJ_FALSE;
  2214. }
  2215. } else if (l_tccp->qmfbid == 0) {
  2216. if (! opj_dwt_encode_real(p_tcd, l_tile_comp)) {
  2217. return OPJ_FALSE;
  2218. }
  2219. }
  2220. ++l_tile_comp;
  2221. ++l_tccp;
  2222. }
  2223. return OPJ_TRUE;
  2224. }
  2225. static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd)
  2226. {
  2227. const OPJ_FLOAT64 * l_mct_norms;
  2228. OPJ_UINT32 l_mct_numcomps = 0U;
  2229. opj_tcp_t * l_tcp = p_tcd->tcp;
  2230. if (l_tcp->mct == 1) {
  2231. l_mct_numcomps = 3U;
  2232. /* irreversible encoding */
  2233. if (l_tcp->tccps->qmfbid == 0) {
  2234. l_mct_norms = opj_mct_get_mct_norms_real();
  2235. } else {
  2236. l_mct_norms = opj_mct_get_mct_norms();
  2237. }
  2238. } else {
  2239. l_mct_numcomps = p_tcd->image->numcomps;
  2240. l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms);
  2241. }
  2242. return opj_t1_encode_cblks(p_tcd,
  2243. p_tcd->tcd_image->tiles, l_tcp, l_mct_norms,
  2244. l_mct_numcomps);
  2245. return OPJ_TRUE;
  2246. }
  2247. static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
  2248. OPJ_BYTE * p_dest_data,
  2249. OPJ_UINT32 * p_data_written,
  2250. OPJ_UINT32 p_max_dest_size,
  2251. opj_codestream_info_t *p_cstr_info,
  2252. opj_tcd_marker_info_t* p_marker_info,
  2253. opj_event_mgr_t *p_manager)
  2254. {
  2255. opj_t2_t * l_t2;
  2256. l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
  2257. if (l_t2 == 00) {
  2258. return OPJ_FALSE;
  2259. }
  2260. if (! opj_t2_encode_packets(
  2261. l_t2,
  2262. p_tcd->tcd_tileno,
  2263. p_tcd->tcd_image->tiles,
  2264. p_tcd->tcp->numlayers,
  2265. p_dest_data,
  2266. p_data_written,
  2267. p_max_dest_size,
  2268. p_cstr_info,
  2269. p_marker_info,
  2270. p_tcd->tp_num,
  2271. p_tcd->tp_pos,
  2272. p_tcd->cur_pino,
  2273. FINAL_PASS,
  2274. p_manager)) {
  2275. opj_t2_destroy(l_t2);
  2276. return OPJ_FALSE;
  2277. }
  2278. opj_t2_destroy(l_t2);
  2279. /*---------------CLEAN-------------------*/
  2280. return OPJ_TRUE;
  2281. }
  2282. static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
  2283. OPJ_BYTE * p_dest_data,
  2284. OPJ_UINT32 p_max_dest_size,
  2285. opj_codestream_info_t *p_cstr_info,
  2286. opj_event_mgr_t *p_manager)
  2287. {
  2288. opj_cp_t * l_cp = p_tcd->cp;
  2289. OPJ_UINT32 l_nb_written = 0;
  2290. if (p_cstr_info) {
  2291. p_cstr_info->index_write = 0;
  2292. }
  2293. if (l_cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy ==
  2294. RATE_DISTORTION_RATIO ||
  2295. l_cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy ==
  2296. FIXED_DISTORTION_RATIO) {
  2297. if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
  2298. p_cstr_info, p_manager)) {
  2299. return OPJ_FALSE;
  2300. }
  2301. } else {
  2302. /* Fixed layer allocation */
  2303. opj_tcd_rateallocate_fixed(p_tcd);
  2304. }
  2305. return OPJ_TRUE;
  2306. }
  2307. OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd,
  2308. OPJ_BYTE * p_src,
  2309. OPJ_SIZE_T p_src_length)
  2310. {
  2311. OPJ_UINT32 i;
  2312. OPJ_SIZE_T j;
  2313. OPJ_SIZE_T l_data_size = 0;
  2314. opj_image_comp_t * l_img_comp = 00;
  2315. opj_tcd_tilecomp_t * l_tilec = 00;
  2316. OPJ_UINT32 l_size_comp, l_remaining;
  2317. OPJ_SIZE_T l_nb_elem;
  2318. l_data_size = opj_tcd_get_encoder_input_buffer_size(p_tcd);
  2319. if (l_data_size != p_src_length) {
  2320. return OPJ_FALSE;
  2321. }
  2322. l_tilec = p_tcd->tcd_image->tiles->comps;
  2323. l_img_comp = p_tcd->image->comps;
  2324. for (i = 0; i < p_tcd->image->numcomps; ++i) {
  2325. l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
  2326. l_remaining = l_img_comp->prec & 7; /* (%8) */
  2327. l_nb_elem = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) *
  2328. (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0);
  2329. if (l_remaining) {
  2330. ++l_size_comp;
  2331. }
  2332. if (l_size_comp == 3) {
  2333. l_size_comp = 4;
  2334. }
  2335. switch (l_size_comp) {
  2336. case 1: {
  2337. OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
  2338. OPJ_INT32 * l_dest_ptr = l_tilec->data;
  2339. if (l_img_comp->sgnd) {
  2340. for (j = 0; j < l_nb_elem; ++j) {
  2341. *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
  2342. }
  2343. } else {
  2344. for (j = 0; j < l_nb_elem; ++j) {
  2345. *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff;
  2346. }
  2347. }
  2348. p_src = (OPJ_BYTE*) l_src_ptr;
  2349. }
  2350. break;
  2351. case 2: {
  2352. OPJ_INT32 * l_dest_ptr = l_tilec->data;
  2353. OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
  2354. if (l_img_comp->sgnd) {
  2355. for (j = 0; j < l_nb_elem; ++j) {
  2356. *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
  2357. }
  2358. } else {
  2359. for (j = 0; j < l_nb_elem; ++j) {
  2360. *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff;
  2361. }
  2362. }
  2363. p_src = (OPJ_BYTE*) l_src_ptr;
  2364. }
  2365. break;
  2366. case 4: {
  2367. OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
  2368. OPJ_INT32 * l_dest_ptr = l_tilec->data;
  2369. for (j = 0; j < l_nb_elem; ++j) {
  2370. *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
  2371. }
  2372. p_src = (OPJ_BYTE*) l_src_ptr;
  2373. }
  2374. break;
  2375. }
  2376. ++l_img_comp;
  2377. ++l_tilec;
  2378. }
  2379. return OPJ_TRUE;
  2380. }
  2381. OPJ_BOOL opj_tcd_is_band_empty(opj_tcd_band_t* band)
  2382. {
  2383. return (band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0);
  2384. }
  2385. OPJ_BOOL opj_tcd_is_subband_area_of_interest(opj_tcd_t *tcd,
  2386. OPJ_UINT32 compno,
  2387. OPJ_UINT32 resno,
  2388. OPJ_UINT32 bandno,
  2389. OPJ_UINT32 band_x0,
  2390. OPJ_UINT32 band_y0,
  2391. OPJ_UINT32 band_x1,
  2392. OPJ_UINT32 band_y1)
  2393. {
  2394. /* Note: those values for filter_margin are in part the result of */
  2395. /* experimentation. The value 2 for QMFBID=1 (5x3 filter) can be linked */
  2396. /* to the maximum left/right extension given in tables F.2 and F.3 of the */
  2397. /* standard. The value 3 for QMFBID=0 (9x7 filter) is more suspicious, */
  2398. /* since F.2 and F.3 would lead to 4 instead, so the current 3 might be */
  2399. /* needed to be bumped to 4, in case inconsistencies are found while */
  2400. /* decoding parts of irreversible coded images. */
  2401. /* See opj_dwt_decode_partial_53 and opj_dwt_decode_partial_97 as well */
  2402. OPJ_UINT32 filter_margin = (tcd->tcp->tccps[compno].qmfbid == 1) ? 2 : 3;
  2403. opj_tcd_tilecomp_t *tilec = &(tcd->tcd_image->tiles->comps[compno]);
  2404. opj_image_comp_t* image_comp = &(tcd->image->comps[compno]);
  2405. /* Compute the intersection of the area of interest, expressed in tile coordinates */
  2406. /* with the tile coordinates */
  2407. OPJ_UINT32 tcx0 = opj_uint_max(
  2408. (OPJ_UINT32)tilec->x0,
  2409. opj_uint_ceildiv(tcd->win_x0, image_comp->dx));
  2410. OPJ_UINT32 tcy0 = opj_uint_max(
  2411. (OPJ_UINT32)tilec->y0,
  2412. opj_uint_ceildiv(tcd->win_y0, image_comp->dy));
  2413. OPJ_UINT32 tcx1 = opj_uint_min(
  2414. (OPJ_UINT32)tilec->x1,
  2415. opj_uint_ceildiv(tcd->win_x1, image_comp->dx));
  2416. OPJ_UINT32 tcy1 = opj_uint_min(
  2417. (OPJ_UINT32)tilec->y1,
  2418. opj_uint_ceildiv(tcd->win_y1, image_comp->dy));
  2419. /* Compute number of decomposition for this band. See table F-1 */
  2420. OPJ_UINT32 nb = (resno == 0) ?
  2421. tilec->numresolutions - 1 :
  2422. tilec->numresolutions - resno;
  2423. /* Map above tile-based coordinates to sub-band-based coordinates per */
  2424. /* equation B-15 of the standard */
  2425. OPJ_UINT32 x0b = bandno & 1;
  2426. OPJ_UINT32 y0b = bandno >> 1;
  2427. OPJ_UINT32 tbx0 = (nb == 0) ? tcx0 :
  2428. (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
  2429. opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
  2430. OPJ_UINT32 tby0 = (nb == 0) ? tcy0 :
  2431. (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
  2432. opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
  2433. OPJ_UINT32 tbx1 = (nb == 0) ? tcx1 :
  2434. (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
  2435. opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
  2436. OPJ_UINT32 tby1 = (nb == 0) ? tcy1 :
  2437. (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
  2438. opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
  2439. OPJ_BOOL intersects;
  2440. if (tbx0 < filter_margin) {
  2441. tbx0 = 0;
  2442. } else {
  2443. tbx0 -= filter_margin;
  2444. }
  2445. if (tby0 < filter_margin) {
  2446. tby0 = 0;
  2447. } else {
  2448. tby0 -= filter_margin;
  2449. }
  2450. tbx1 = opj_uint_adds(tbx1, filter_margin);
  2451. tby1 = opj_uint_adds(tby1, filter_margin);
  2452. intersects = band_x0 < tbx1 && band_y0 < tby1 && band_x1 > tbx0 &&
  2453. band_y1 > tby0;
  2454. #ifdef DEBUG_VERBOSE
  2455. printf("compno=%u resno=%u nb=%u bandno=%u x0b=%u y0b=%u band=%u,%u,%u,%u tb=%u,%u,%u,%u -> %u\n",
  2456. compno, resno, nb, bandno, x0b, y0b,
  2457. band_x0, band_y0, band_x1, band_y1,
  2458. tbx0, tby0, tbx1, tby1, intersects);
  2459. #endif
  2460. return intersects;
  2461. }
  2462. /** Returns whether a tile componenent is fully decoded, taking into account
  2463. * p_tcd->win_* members.
  2464. *
  2465. * @param p_tcd TCD handle.
  2466. * @param compno Component number
  2467. * @return OPJ_TRUE whether the tile componenent is fully decoded
  2468. */
  2469. static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *p_tcd,
  2470. OPJ_UINT32 compno)
  2471. {
  2472. opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
  2473. opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
  2474. /* Compute the intersection of the area of interest, expressed in tile coordinates */
  2475. /* with the tile coordinates */
  2476. OPJ_UINT32 tcx0 = opj_uint_max(
  2477. (OPJ_UINT32)tilec->x0,
  2478. opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx));
  2479. OPJ_UINT32 tcy0 = opj_uint_max(
  2480. (OPJ_UINT32)tilec->y0,
  2481. opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy));
  2482. OPJ_UINT32 tcx1 = opj_uint_min(
  2483. (OPJ_UINT32)tilec->x1,
  2484. opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx));
  2485. OPJ_UINT32 tcy1 = opj_uint_min(
  2486. (OPJ_UINT32)tilec->y1,
  2487. opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy));
  2488. OPJ_UINT32 shift = tilec->numresolutions - tilec->minimum_num_resolutions;
  2489. /* Tolerate small margin within the reduced resolution factor to consider if */
  2490. /* the whole tile path must be taken */
  2491. return (tcx0 >= (OPJ_UINT32)tilec->x0 &&
  2492. tcy0 >= (OPJ_UINT32)tilec->y0 &&
  2493. tcx1 <= (OPJ_UINT32)tilec->x1 &&
  2494. tcy1 <= (OPJ_UINT32)tilec->y1 &&
  2495. (shift >= 32 ||
  2496. (((tcx0 - (OPJ_UINT32)tilec->x0) >> shift) == 0 &&
  2497. ((tcy0 - (OPJ_UINT32)tilec->y0) >> shift) == 0 &&
  2498. (((OPJ_UINT32)tilec->x1 - tcx1) >> shift) == 0 &&
  2499. (((OPJ_UINT32)tilec->y1 - tcy1) >> shift) == 0)));
  2500. }
  2501. /* ----------------------------------------------------------------------- */
  2502. opj_tcd_marker_info_t* opj_tcd_marker_info_create(OPJ_BOOL need_PLT)
  2503. {
  2504. opj_tcd_marker_info_t *l_tcd_marker_info =
  2505. (opj_tcd_marker_info_t*) opj_calloc(1, sizeof(opj_tcd_marker_info_t));
  2506. if (!l_tcd_marker_info) {
  2507. return NULL;
  2508. }
  2509. l_tcd_marker_info->need_PLT = need_PLT;
  2510. return l_tcd_marker_info;
  2511. }
  2512. /* ----------------------------------------------------------------------- */
  2513. void opj_tcd_marker_info_destroy(opj_tcd_marker_info_t *p_tcd_marker_info)
  2514. {
  2515. if (p_tcd_marker_info) {
  2516. opj_free(p_tcd_marker_info->p_packet_size);
  2517. opj_free(p_tcd_marker_info);
  2518. }
  2519. }
  2520. /* ----------------------------------------------------------------------- */