tcd.c 112 KB

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