jpc_enc.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620
  1. /*
  2. * Copyright (c) 1999-2000 Image Power, Inc. and the University of
  3. * British Columbia.
  4. * Copyright (c) 2001-2003 Michael David Adams.
  5. * All rights reserved.
  6. */
  7. /* __START_OF_JASPER_LICENSE__
  8. *
  9. * JasPer License Version 2.0
  10. *
  11. * Copyright (c) 2001-2006 Michael David Adams
  12. * Copyright (c) 1999-2000 Image Power, Inc.
  13. * Copyright (c) 1999-2000 The University of British Columbia
  14. *
  15. * All rights reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person (the
  18. * "User") obtaining a copy of this software and associated documentation
  19. * files (the "Software"), to deal in the Software without restriction,
  20. * including without limitation the rights to use, copy, modify, merge,
  21. * publish, distribute, and/or sell copies of the Software, and to permit
  22. * persons to whom the Software is furnished to do so, subject to the
  23. * following conditions:
  24. *
  25. * 1. The above copyright notices and this permission notice (which
  26. * includes the disclaimer below) shall be included in all copies or
  27. * substantial portions of the Software.
  28. *
  29. * 2. The name of a copyright holder shall not be used to endorse or
  30. * promote products derived from the Software without specific prior
  31. * written permission.
  32. *
  33. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  34. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  35. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  36. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  37. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  38. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  39. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  40. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  41. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  42. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  43. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  44. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  45. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  46. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  47. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  48. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  49. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  50. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  51. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  52. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  53. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  54. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  55. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  56. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  57. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  58. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  59. *
  60. * __END_OF_JASPER_LICENSE__
  61. */
  62. /*
  63. * $Id: jpc_enc.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  64. */
  65. /******************************************************************************\
  66. * Includes.
  67. \******************************************************************************/
  68. #include <stdio.h>
  69. #include <stdlib.h>
  70. #include <assert.h>
  71. #include <math.h>
  72. #include <float.h>
  73. #include "jasper/jas_types.h"
  74. #include "jasper/jas_string.h"
  75. #include "jasper/jas_malloc.h"
  76. #include "jasper/jas_image.h"
  77. #include "jasper/jas_fix.h"
  78. #include "jasper/jas_tvp.h"
  79. #include "jasper/jas_version.h"
  80. #include "jasper/jas_math.h"
  81. #include "jasper/jas_debug.h"
  82. #include "jpc_flt.h"
  83. #include "jpc_fix.h"
  84. #include "jpc_tagtree.h"
  85. #include "jpc_enc.h"
  86. #include "jpc_cs.h"
  87. #include "jpc_mct.h"
  88. #include "jpc_tsfb.h"
  89. #include "jpc_qmfb.h"
  90. #include "jpc_t1enc.h"
  91. #include "jpc_t2enc.h"
  92. #include "jpc_cod.h"
  93. #include "jpc_math.h"
  94. #include "jpc_util.h"
  95. /******************************************************************************\
  96. *
  97. \******************************************************************************/
  98. #define JPC_POW2(n) \
  99. (1 << (n))
  100. #define JPC_FLOORTOMULTPOW2(x, n) \
  101. (((n) > 0) ? ((x) & (~((1 << n) - 1))) : (x))
  102. /* Round to the nearest multiple of the specified power of two in the
  103. direction of negative infinity. */
  104. #define JPC_CEILTOMULTPOW2(x, n) \
  105. (((n) > 0) ? JPC_FLOORTOMULTPOW2(((x) + (1 << (n)) - 1), n) : (x))
  106. /* Round to the nearest multiple of the specified power of two in the
  107. direction of positive infinity. */
  108. #define JPC_POW2(n) \
  109. (1 << (n))
  110. jpc_enc_tile_t *jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno);
  111. void jpc_enc_tile_destroy(jpc_enc_tile_t *tile);
  112. static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp,
  113. jas_image_t *image, jpc_enc_tile_t *tile);
  114. static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt);
  115. static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp,
  116. jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos);
  117. static void rlvl_destroy(jpc_enc_rlvl_t *rlvl);
  118. static jpc_enc_band_t *band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp,
  119. jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos);
  120. static void band_destroy(jpc_enc_band_t *bands);
  121. static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp,
  122. jpc_enc_band_t *band);
  123. static void prc_destroy(jpc_enc_prc_t *prcs);
  124. static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp,
  125. jpc_enc_prc_t *prc);
  126. static void cblk_destroy(jpc_enc_cblk_t *cblks);
  127. int ratestrtosize(char *s, uint_fast32_t rawsize, uint_fast32_t *size);
  128. static void pass_destroy(jpc_enc_pass_t *pass);
  129. void jpc_enc_dump(jpc_enc_t *enc);
  130. /******************************************************************************\
  131. * Local prototypes.
  132. \******************************************************************************/
  133. int dump_passes(jpc_enc_pass_t *passes, int numpasses, jpc_enc_cblk_t *cblk);
  134. void calcrdslopes(jpc_enc_cblk_t *cblk);
  135. void dump_layeringinfo(jpc_enc_t *enc);
  136. static int jpc_calcssexp(jpc_fix_t stepsize);
  137. static int jpc_calcssmant(jpc_fix_t stepsize);
  138. void jpc_quantize(jas_matrix_t *data, jpc_fix_t stepsize);
  139. static int jpc_enc_encodemainhdr(jpc_enc_t *enc);
  140. static int jpc_enc_encodemainbody(jpc_enc_t *enc);
  141. int jpc_enc_encodetiledata(jpc_enc_t *enc);
  142. jpc_enc_t *jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image);
  143. void jpc_enc_destroy(jpc_enc_t *enc);
  144. static int jpc_enc_encodemainhdr(jpc_enc_t *enc);
  145. static int jpc_enc_encodemainbody(jpc_enc_t *enc);
  146. int jpc_enc_encodetiledata(jpc_enc_t *enc);
  147. int rateallocate(jpc_enc_t *enc, int numlyrs, uint_fast32_t *cumlens);
  148. int setins(int numvalues, jpc_flt_t *values, jpc_flt_t value);
  149. static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image);
  150. void jpc_enc_cp_destroy(jpc_enc_cp_t *cp);
  151. static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn);
  152. static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn)
  153. {
  154. int p;
  155. uint_fast32_t mant;
  156. uint_fast32_t expn;
  157. int n;
  158. if (absdelta < 0) {
  159. abort();
  160. }
  161. p = jpc_firstone(absdelta) - JPC_FIX_FRACBITS;
  162. n = 11 - jpc_firstone(absdelta);
  163. mant = ((n < 0) ? (absdelta >> (-n)) : (absdelta << n)) & 0x7ff;
  164. expn = scaleexpn - p;
  165. if (scaleexpn < p) {
  166. abort();
  167. }
  168. return JPC_QCX_EXPN(expn) | JPC_QCX_MANT(mant);
  169. }
  170. typedef enum {
  171. OPT_DEBUG,
  172. OPT_IMGAREAOFFX,
  173. OPT_IMGAREAOFFY,
  174. OPT_TILEGRDOFFX,
  175. OPT_TILEGRDOFFY,
  176. OPT_TILEWIDTH,
  177. OPT_TILEHEIGHT,
  178. OPT_PRCWIDTH,
  179. OPT_PRCHEIGHT,
  180. OPT_CBLKWIDTH,
  181. OPT_CBLKHEIGHT,
  182. OPT_MODE,
  183. OPT_PRG,
  184. OPT_NOMCT,
  185. OPT_MAXRLVLS,
  186. OPT_SOP,
  187. OPT_EPH,
  188. OPT_LAZY,
  189. OPT_TERMALL,
  190. OPT_SEGSYM,
  191. OPT_VCAUSAL,
  192. OPT_RESET,
  193. OPT_PTERM,
  194. OPT_NUMGBITS,
  195. OPT_RATE,
  196. OPT_ILYRRATES,
  197. OPT_JP2OVERHEAD
  198. } optid_t;
  199. jas_taginfo_t encopts[] = {
  200. {OPT_DEBUG, "debug"},
  201. {OPT_IMGAREAOFFX, "imgareatlx"},
  202. {OPT_IMGAREAOFFY, "imgareatly"},
  203. {OPT_TILEGRDOFFX, "tilegrdtlx"},
  204. {OPT_TILEGRDOFFY, "tilegrdtly"},
  205. {OPT_TILEWIDTH, "tilewidth"},
  206. {OPT_TILEHEIGHT, "tileheight"},
  207. {OPT_PRCWIDTH, "prcwidth"},
  208. {OPT_PRCHEIGHT, "prcheight"},
  209. {OPT_CBLKWIDTH, "cblkwidth"},
  210. {OPT_CBLKHEIGHT, "cblkheight"},
  211. {OPT_MODE, "mode"},
  212. {OPT_PRG, "prg"},
  213. {OPT_NOMCT, "nomct"},
  214. {OPT_MAXRLVLS, "numrlvls"},
  215. {OPT_SOP, "sop"},
  216. {OPT_EPH, "eph"},
  217. {OPT_LAZY, "lazy"},
  218. {OPT_TERMALL, "termall"},
  219. {OPT_SEGSYM, "segsym"},
  220. {OPT_VCAUSAL, "vcausal"},
  221. {OPT_PTERM, "pterm"},
  222. {OPT_RESET, "resetprob"},
  223. {OPT_NUMGBITS, "numgbits"},
  224. {OPT_RATE, "rate"},
  225. {OPT_ILYRRATES, "ilyrrates"},
  226. {OPT_JP2OVERHEAD, "_jp2overhead"},
  227. {-1, 0}
  228. };
  229. typedef enum {
  230. PO_L = 0,
  231. PO_R
  232. } poid_t;
  233. jas_taginfo_t prgordtab[] = {
  234. {JPC_COD_LRCPPRG, "lrcp"},
  235. {JPC_COD_RLCPPRG, "rlcp"},
  236. {JPC_COD_RPCLPRG, "rpcl"},
  237. {JPC_COD_PCRLPRG, "pcrl"},
  238. {JPC_COD_CPRLPRG, "cprl"},
  239. {-1, 0}
  240. };
  241. typedef enum {
  242. MODE_INT,
  243. MODE_REAL
  244. } modeid_t;
  245. jas_taginfo_t modetab[] = {
  246. {MODE_INT, "int"},
  247. {MODE_REAL, "real"},
  248. {-1, 0}
  249. };
  250. /******************************************************************************\
  251. * The main encoder entry point.
  252. \******************************************************************************/
  253. int jpc_encode(jas_image_t *image, jas_stream_t *out, char *optstr)
  254. {
  255. jpc_enc_t *enc;
  256. jpc_enc_cp_t *cp;
  257. enc = 0;
  258. cp = 0;
  259. jpc_initluts();
  260. if (!(cp = cp_create(optstr, image))) {
  261. jas_eprintf("invalid JP encoder options\n");
  262. goto error;
  263. }
  264. if (!(enc = jpc_enc_create(cp, out, image))) {
  265. goto error;
  266. }
  267. cp = 0;
  268. /* Encode the main header. */
  269. if (jpc_enc_encodemainhdr(enc)) {
  270. goto error;
  271. }
  272. /* Encode the main body. This constitutes most of the encoding work. */
  273. if (jpc_enc_encodemainbody(enc)) {
  274. goto error;
  275. }
  276. /* Write EOC marker segment. */
  277. if (!(enc->mrk = jpc_ms_create(JPC_MS_EOC))) {
  278. goto error;
  279. }
  280. if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
  281. jas_eprintf("cannot write EOI marker\n");
  282. goto error;
  283. }
  284. jpc_ms_destroy(enc->mrk);
  285. enc->mrk = 0;
  286. if (jas_stream_flush(enc->out)) {
  287. goto error;
  288. }
  289. jpc_enc_destroy(enc);
  290. return 0;
  291. error:
  292. if (cp) {
  293. jpc_enc_cp_destroy(cp);
  294. }
  295. if (enc) {
  296. jpc_enc_destroy(enc);
  297. }
  298. return -1;
  299. }
  300. /******************************************************************************\
  301. * Option parsing code.
  302. \******************************************************************************/
  303. static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image)
  304. {
  305. jpc_enc_cp_t *cp;
  306. jas_tvparser_t *tvp;
  307. int ret;
  308. int numilyrrates;
  309. double *ilyrrates;
  310. int i;
  311. int tagid;
  312. jpc_enc_tcp_t *tcp;
  313. jpc_enc_tccp_t *tccp;
  314. jpc_enc_ccp_t *ccp;
  315. int cmptno;
  316. uint_fast16_t rlvlno;
  317. uint_fast16_t prcwidthexpn;
  318. uint_fast16_t prcheightexpn;
  319. bool enablemct;
  320. uint_fast32_t jp2overhead;
  321. uint_fast16_t lyrno;
  322. uint_fast32_t hsteplcm;
  323. uint_fast32_t vsteplcm;
  324. bool mctvalid;
  325. tvp = 0;
  326. cp = 0;
  327. ilyrrates = 0;
  328. numilyrrates = 0;
  329. if (!(cp = jas_malloc(sizeof(jpc_enc_cp_t)))) {
  330. goto error;
  331. }
  332. prcwidthexpn = 15;
  333. prcheightexpn = 15;
  334. enablemct = true;
  335. jp2overhead = 0;
  336. cp->ccps = 0;
  337. cp->debug = 0;
  338. cp->imgareatlx = UINT_FAST32_MAX;
  339. cp->imgareatly = UINT_FAST32_MAX;
  340. cp->refgrdwidth = 0;
  341. cp->refgrdheight = 0;
  342. cp->tilegrdoffx = UINT_FAST32_MAX;
  343. cp->tilegrdoffy = UINT_FAST32_MAX;
  344. cp->tilewidth = 0;
  345. cp->tileheight = 0;
  346. cp->numcmpts = jas_image_numcmpts(image);
  347. hsteplcm = 1;
  348. vsteplcm = 1;
  349. for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) {
  350. if (jas_image_cmptbrx(image, cmptno) + jas_image_cmpthstep(image, cmptno) <=
  351. jas_image_brx(image) || jas_image_cmptbry(image, cmptno) +
  352. jas_image_cmptvstep(image, cmptno) <= jas_image_bry(image)) {
  353. jas_eprintf("unsupported image type\n");
  354. goto error;
  355. }
  356. /* Note: We ought to be calculating the LCMs here. Fix some day. */
  357. hsteplcm *= jas_image_cmpthstep(image, cmptno);
  358. vsteplcm *= jas_image_cmptvstep(image, cmptno);
  359. }
  360. if (!(cp->ccps = jas_alloc2(cp->numcmpts, sizeof(jpc_enc_ccp_t)))) {
  361. goto error;
  362. }
  363. for (cmptno = 0, ccp = cp->ccps; cmptno < JAS_CAST(int, cp->numcmpts); ++cmptno,
  364. ++ccp) {
  365. ccp->sampgrdstepx = jas_image_cmpthstep(image, cmptno);
  366. ccp->sampgrdstepy = jas_image_cmptvstep(image, cmptno);
  367. /* XXX - this isn't quite correct for more general image */
  368. ccp->sampgrdsubstepx = 0;
  369. ccp->sampgrdsubstepx = 0;
  370. ccp->prec = jas_image_cmptprec(image, cmptno);
  371. ccp->sgnd = jas_image_cmptsgnd(image, cmptno);
  372. ccp->numstepsizes = 0;
  373. memset(ccp->stepsizes, 0, sizeof(ccp->stepsizes));
  374. }
  375. cp->rawsize = jas_image_rawsize(image);
  376. cp->totalsize = UINT_FAST32_MAX;
  377. tcp = &cp->tcp;
  378. tcp->csty = 0;
  379. tcp->intmode = true;
  380. tcp->prg = JPC_COD_LRCPPRG;
  381. tcp->numlyrs = 1;
  382. tcp->ilyrrates = 0;
  383. tccp = &cp->tccp;
  384. tccp->csty = 0;
  385. tccp->maxrlvls = 6;
  386. tccp->cblkwidthexpn = 6;
  387. tccp->cblkheightexpn = 6;
  388. tccp->cblksty = 0;
  389. tccp->numgbits = 2;
  390. if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) {
  391. goto error;
  392. }
  393. while (!(ret = jas_tvparser_next(tvp))) {
  394. switch (jas_taginfo_nonull(jas_taginfos_lookup(encopts,
  395. jas_tvparser_gettag(tvp)))->id) {
  396. case OPT_DEBUG:
  397. cp->debug = atoi(jas_tvparser_getval(tvp));
  398. break;
  399. case OPT_IMGAREAOFFX:
  400. cp->imgareatlx = atoi(jas_tvparser_getval(tvp));
  401. break;
  402. case OPT_IMGAREAOFFY:
  403. cp->imgareatly = atoi(jas_tvparser_getval(tvp));
  404. break;
  405. case OPT_TILEGRDOFFX:
  406. cp->tilegrdoffx = atoi(jas_tvparser_getval(tvp));
  407. break;
  408. case OPT_TILEGRDOFFY:
  409. cp->tilegrdoffy = atoi(jas_tvparser_getval(tvp));
  410. break;
  411. case OPT_TILEWIDTH:
  412. cp->tilewidth = atoi(jas_tvparser_getval(tvp));
  413. break;
  414. case OPT_TILEHEIGHT:
  415. cp->tileheight = atoi(jas_tvparser_getval(tvp));
  416. break;
  417. case OPT_PRCWIDTH:
  418. prcwidthexpn = jpc_floorlog2(atoi(jas_tvparser_getval(tvp)));
  419. break;
  420. case OPT_PRCHEIGHT:
  421. prcheightexpn = jpc_floorlog2(atoi(jas_tvparser_getval(tvp)));
  422. break;
  423. case OPT_CBLKWIDTH:
  424. tccp->cblkwidthexpn =
  425. jpc_floorlog2(atoi(jas_tvparser_getval(tvp)));
  426. break;
  427. case OPT_CBLKHEIGHT:
  428. tccp->cblkheightexpn =
  429. jpc_floorlog2(atoi(jas_tvparser_getval(tvp)));
  430. break;
  431. case OPT_MODE:
  432. if ((tagid = jas_taginfo_nonull(jas_taginfos_lookup(modetab,
  433. jas_tvparser_getval(tvp)))->id) < 0) {
  434. jas_eprintf("ignoring invalid mode %s\n",
  435. jas_tvparser_getval(tvp));
  436. } else {
  437. tcp->intmode = (tagid == MODE_INT);
  438. }
  439. break;
  440. case OPT_PRG:
  441. if ((tagid = jas_taginfo_nonull(jas_taginfos_lookup(prgordtab,
  442. jas_tvparser_getval(tvp)))->id) < 0) {
  443. jas_eprintf("ignoring invalid progression order %s\n",
  444. jas_tvparser_getval(tvp));
  445. } else {
  446. tcp->prg = tagid;
  447. }
  448. break;
  449. case OPT_NOMCT:
  450. enablemct = false;
  451. break;
  452. case OPT_MAXRLVLS:
  453. tccp->maxrlvls = atoi(jas_tvparser_getval(tvp));
  454. break;
  455. case OPT_SOP:
  456. cp->tcp.csty |= JPC_COD_SOP;
  457. break;
  458. case OPT_EPH:
  459. cp->tcp.csty |= JPC_COD_EPH;
  460. break;
  461. case OPT_LAZY:
  462. tccp->cblksty |= JPC_COX_LAZY;
  463. break;
  464. case OPT_TERMALL:
  465. tccp->cblksty |= JPC_COX_TERMALL;
  466. break;
  467. case OPT_SEGSYM:
  468. tccp->cblksty |= JPC_COX_SEGSYM;
  469. break;
  470. case OPT_VCAUSAL:
  471. tccp->cblksty |= JPC_COX_VSC;
  472. break;
  473. case OPT_RESET:
  474. tccp->cblksty |= JPC_COX_RESET;
  475. break;
  476. case OPT_PTERM:
  477. tccp->cblksty |= JPC_COX_PTERM;
  478. break;
  479. case OPT_NUMGBITS:
  480. cp->tccp.numgbits = atoi(jas_tvparser_getval(tvp));
  481. break;
  482. case OPT_RATE:
  483. if (ratestrtosize(jas_tvparser_getval(tvp), cp->rawsize,
  484. &cp->totalsize)) {
  485. jas_eprintf("ignoring bad rate specifier %s\n",
  486. jas_tvparser_getval(tvp));
  487. }
  488. break;
  489. case OPT_ILYRRATES:
  490. if (jpc_atoaf(jas_tvparser_getval(tvp), &numilyrrates,
  491. &ilyrrates)) {
  492. jas_eprintf("warning: invalid intermediate layer rates specifier ignored (%s)\n",
  493. jas_tvparser_getval(tvp));
  494. }
  495. break;
  496. case OPT_JP2OVERHEAD:
  497. jp2overhead = atoi(jas_tvparser_getval(tvp));
  498. break;
  499. default:
  500. jas_eprintf("warning: ignoring invalid option %s\n",
  501. jas_tvparser_gettag(tvp));
  502. break;
  503. }
  504. }
  505. jas_tvparser_destroy(tvp);
  506. tvp = 0;
  507. if (cp->totalsize != UINT_FAST32_MAX) {
  508. cp->totalsize = (cp->totalsize > jp2overhead) ?
  509. (cp->totalsize - jp2overhead) : 0;
  510. }
  511. if (cp->imgareatlx == UINT_FAST32_MAX) {
  512. cp->imgareatlx = 0;
  513. } else {
  514. if (hsteplcm != 1) {
  515. jas_eprintf("warning: overriding imgareatlx value\n");
  516. }
  517. cp->imgareatlx *= hsteplcm;
  518. }
  519. if (cp->imgareatly == UINT_FAST32_MAX) {
  520. cp->imgareatly = 0;
  521. } else {
  522. if (vsteplcm != 1) {
  523. jas_eprintf("warning: overriding imgareatly value\n");
  524. }
  525. cp->imgareatly *= vsteplcm;
  526. }
  527. cp->refgrdwidth = cp->imgareatlx + jas_image_width(image);
  528. cp->refgrdheight = cp->imgareatly + jas_image_height(image);
  529. if (cp->tilegrdoffx == UINT_FAST32_MAX) {
  530. cp->tilegrdoffx = cp->imgareatlx;
  531. }
  532. if (cp->tilegrdoffy == UINT_FAST32_MAX) {
  533. cp->tilegrdoffy = cp->imgareatly;
  534. }
  535. if (!cp->tilewidth) {
  536. cp->tilewidth = cp->refgrdwidth - cp->tilegrdoffx;
  537. }
  538. if (!cp->tileheight) {
  539. cp->tileheight = cp->refgrdheight - cp->tilegrdoffy;
  540. }
  541. if (cp->numcmpts == 3) {
  542. mctvalid = true;
  543. for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) {
  544. if (jas_image_cmptprec(image, cmptno) != jas_image_cmptprec(image, 0) ||
  545. jas_image_cmptsgnd(image, cmptno) != jas_image_cmptsgnd(image, 0) ||
  546. jas_image_cmptwidth(image, cmptno) != jas_image_cmptwidth(image, 0) ||
  547. jas_image_cmptheight(image, cmptno) != jas_image_cmptheight(image, 0)) {
  548. mctvalid = false;
  549. }
  550. }
  551. } else {
  552. mctvalid = false;
  553. }
  554. if (mctvalid && enablemct && jas_clrspc_fam(jas_image_clrspc(image)) != JAS_CLRSPC_FAM_RGB) {
  555. jas_eprintf("warning: color space apparently not RGB\n");
  556. }
  557. if (mctvalid && enablemct && jas_clrspc_fam(jas_image_clrspc(image)) == JAS_CLRSPC_FAM_RGB) {
  558. tcp->mctid = (tcp->intmode) ? (JPC_MCT_RCT) : (JPC_MCT_ICT);
  559. } else {
  560. tcp->mctid = JPC_MCT_NONE;
  561. }
  562. tccp->qmfbid = (tcp->intmode) ? (JPC_COX_RFT) : (JPC_COX_INS);
  563. for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) {
  564. tccp->prcwidthexpns[rlvlno] = prcwidthexpn;
  565. tccp->prcheightexpns[rlvlno] = prcheightexpn;
  566. }
  567. if (prcwidthexpn != 15 || prcheightexpn != 15) {
  568. tccp->csty |= JPC_COX_PRT;
  569. }
  570. /* Ensure that the tile width and height is valid. */
  571. if (!cp->tilewidth) {
  572. jas_eprintf("invalid tile width %lu\n", (unsigned long)
  573. cp->tilewidth);
  574. goto error;
  575. }
  576. if (!cp->tileheight) {
  577. jas_eprintf("invalid tile height %lu\n", (unsigned long)
  578. cp->tileheight);
  579. goto error;
  580. }
  581. /* Ensure that the tile grid offset is valid. */
  582. if (cp->tilegrdoffx > cp->imgareatlx ||
  583. cp->tilegrdoffy > cp->imgareatly ||
  584. cp->tilegrdoffx + cp->tilewidth < cp->imgareatlx ||
  585. cp->tilegrdoffy + cp->tileheight < cp->imgareatly) {
  586. jas_eprintf("invalid tile grid offset (%lu, %lu)\n",
  587. (unsigned long) cp->tilegrdoffx, (unsigned long)
  588. cp->tilegrdoffy);
  589. goto error;
  590. }
  591. cp->numhtiles = JPC_CEILDIV(cp->refgrdwidth - cp->tilegrdoffx,
  592. cp->tilewidth);
  593. cp->numvtiles = JPC_CEILDIV(cp->refgrdheight - cp->tilegrdoffy,
  594. cp->tileheight);
  595. cp->numtiles = cp->numhtiles * cp->numvtiles;
  596. if (ilyrrates && numilyrrates > 0) {
  597. tcp->numlyrs = numilyrrates + 1;
  598. if (!(tcp->ilyrrates = jas_alloc2((tcp->numlyrs - 1),
  599. sizeof(jpc_fix_t)))) {
  600. goto error;
  601. }
  602. for (i = 0; i < JAS_CAST(int, tcp->numlyrs - 1); ++i) {
  603. tcp->ilyrrates[i] = jpc_dbltofix(ilyrrates[i]);
  604. }
  605. }
  606. /* Ensure that the integer mode is used in the case of lossless
  607. coding. */
  608. if (cp->totalsize == UINT_FAST32_MAX && (!cp->tcp.intmode)) {
  609. jas_eprintf("cannot use real mode for lossless coding\n");
  610. goto error;
  611. }
  612. /* Ensure that the precinct width is valid. */
  613. if (prcwidthexpn > 15) {
  614. jas_eprintf("invalid precinct width\n");
  615. goto error;
  616. }
  617. /* Ensure that the precinct height is valid. */
  618. if (prcheightexpn > 15) {
  619. jas_eprintf("invalid precinct height\n");
  620. goto error;
  621. }
  622. /* Ensure that the code block width is valid. */
  623. if (cp->tccp.cblkwidthexpn < 2 || cp->tccp.cblkwidthexpn > 12) {
  624. jas_eprintf("invalid code block width %d\n",
  625. JPC_POW2(cp->tccp.cblkwidthexpn));
  626. goto error;
  627. }
  628. /* Ensure that the code block height is valid. */
  629. if (cp->tccp.cblkheightexpn < 2 || cp->tccp.cblkheightexpn > 12) {
  630. jas_eprintf("invalid code block height %d\n",
  631. JPC_POW2(cp->tccp.cblkheightexpn));
  632. goto error;
  633. }
  634. /* Ensure that the code block size is not too large. */
  635. if (cp->tccp.cblkwidthexpn + cp->tccp.cblkheightexpn > 12) {
  636. jas_eprintf("code block size too large\n");
  637. goto error;
  638. }
  639. /* Ensure that the number of layers is valid. */
  640. if (cp->tcp.numlyrs > 16384) {
  641. jas_eprintf("too many layers\n");
  642. goto error;
  643. }
  644. /* There must be at least one resolution level. */
  645. if (cp->tccp.maxrlvls < 1) {
  646. jas_eprintf("must be at least one resolution level\n");
  647. goto error;
  648. }
  649. /* Ensure that the number of guard bits is valid. */
  650. if (cp->tccp.numgbits > 8) {
  651. jas_eprintf("invalid number of guard bits\n");
  652. goto error;
  653. }
  654. /* Ensure that the rate is within the legal range. */
  655. if (cp->totalsize != UINT_FAST32_MAX && cp->totalsize > cp->rawsize) {
  656. jas_eprintf("warning: specified rate is unreasonably large (%lu > %lu)\n", (unsigned long) cp->totalsize, (unsigned long) cp->rawsize);
  657. }
  658. /* Ensure that the intermediate layer rates are valid. */
  659. if (tcp->numlyrs > 1) {
  660. /* The intermediate layers rates must increase monotonically. */
  661. for (lyrno = 0; lyrno + 2 < tcp->numlyrs; ++lyrno) {
  662. if (tcp->ilyrrates[lyrno] >= tcp->ilyrrates[lyrno + 1]) {
  663. jas_eprintf("intermediate layer rates must increase monotonically\n");
  664. goto error;
  665. }
  666. }
  667. /* The intermediate layer rates must be less than the overall rate. */
  668. if (cp->totalsize != UINT_FAST32_MAX) {
  669. for (lyrno = 0; lyrno < tcp->numlyrs - 1; ++lyrno) {
  670. if (jpc_fixtodbl(tcp->ilyrrates[lyrno]) > ((double) cp->totalsize)
  671. / cp->rawsize) {
  672. jas_eprintf("warning: intermediate layer rates must be less than overall rate\n");
  673. goto error;
  674. }
  675. }
  676. }
  677. }
  678. if (ilyrrates) {
  679. jas_free(ilyrrates);
  680. }
  681. return cp;
  682. error:
  683. if (ilyrrates) {
  684. jas_free(ilyrrates);
  685. }
  686. if (tvp) {
  687. jas_tvparser_destroy(tvp);
  688. }
  689. if (cp) {
  690. jpc_enc_cp_destroy(cp);
  691. }
  692. return 0;
  693. }
  694. void jpc_enc_cp_destroy(jpc_enc_cp_t *cp)
  695. {
  696. if (cp->ccps) {
  697. if (cp->tcp.ilyrrates) {
  698. jas_free(cp->tcp.ilyrrates);
  699. }
  700. jas_free(cp->ccps);
  701. }
  702. jas_free(cp);
  703. }
  704. int ratestrtosize(char *s, uint_fast32_t rawsize, uint_fast32_t *size)
  705. {
  706. char *cp;
  707. jpc_flt_t f;
  708. /* Note: This function must not modify output size on failure. */
  709. if ((cp = strchr(s, 'B'))) {
  710. *size = atoi(s);
  711. } else {
  712. f = atof(s);
  713. if (f < 0) {
  714. *size = 0;
  715. } else if (f > 1.0) {
  716. *size = rawsize + 1;
  717. } else {
  718. *size = f * rawsize;
  719. }
  720. }
  721. return 0;
  722. }
  723. /******************************************************************************\
  724. * Encoder constructor and destructor.
  725. \******************************************************************************/
  726. jpc_enc_t *jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image)
  727. {
  728. jpc_enc_t *enc;
  729. enc = 0;
  730. if (!(enc = jas_malloc(sizeof(jpc_enc_t)))) {
  731. goto error;
  732. }
  733. enc->image = image;
  734. enc->out = out;
  735. enc->cp = cp;
  736. enc->cstate = 0;
  737. enc->tmpstream = 0;
  738. enc->mrk = 0;
  739. enc->curtile = 0;
  740. if (!(enc->cstate = jpc_cstate_create())) {
  741. goto error;
  742. }
  743. enc->len = 0;
  744. enc->mainbodysize = 0;
  745. return enc;
  746. error:
  747. if (enc) {
  748. jpc_enc_destroy(enc);
  749. }
  750. return 0;
  751. }
  752. void jpc_enc_destroy(jpc_enc_t *enc)
  753. {
  754. /* The image object (i.e., enc->image) and output stream object
  755. (i.e., enc->out) are created outside of the encoder.
  756. Therefore, they must not be destroyed here. */
  757. if (enc->curtile) {
  758. jpc_enc_tile_destroy(enc->curtile);
  759. }
  760. if (enc->cp) {
  761. jpc_enc_cp_destroy(enc->cp);
  762. }
  763. if (enc->cstate) {
  764. jpc_cstate_destroy(enc->cstate);
  765. }
  766. if (enc->tmpstream) {
  767. jas_stream_close(enc->tmpstream);
  768. }
  769. jas_free(enc);
  770. }
  771. /******************************************************************************\
  772. * Code.
  773. \******************************************************************************/
  774. static int jpc_calcssmant(jpc_fix_t stepsize)
  775. {
  776. int n;
  777. int e;
  778. int m;
  779. n = jpc_firstone(stepsize);
  780. e = n - JPC_FIX_FRACBITS;
  781. if (n >= 11) {
  782. m = (stepsize >> (n - 11)) & 0x7ff;
  783. } else {
  784. m = (stepsize & ((1 << n) - 1)) << (11 - n);
  785. }
  786. return m;
  787. }
  788. static int jpc_calcssexp(jpc_fix_t stepsize)
  789. {
  790. return jpc_firstone(stepsize) - JPC_FIX_FRACBITS;
  791. }
  792. static int jpc_enc_encodemainhdr(jpc_enc_t *enc)
  793. {
  794. jpc_siz_t *siz;
  795. jpc_cod_t *cod;
  796. jpc_qcd_t *qcd;
  797. int i;
  798. long startoff;
  799. long mainhdrlen;
  800. jpc_enc_cp_t *cp;
  801. jpc_qcc_t *qcc;
  802. jpc_enc_tccp_t *tccp;
  803. uint_fast16_t cmptno;
  804. jpc_tsfb_band_t bandinfos[JPC_MAXBANDS];
  805. jpc_fix_t mctsynweight;
  806. jpc_enc_tcp_t *tcp;
  807. jpc_tsfb_t *tsfb;
  808. jpc_tsfb_band_t *bandinfo;
  809. uint_fast16_t numbands;
  810. uint_fast16_t bandno;
  811. uint_fast16_t rlvlno;
  812. uint_fast16_t analgain;
  813. jpc_fix_t absstepsize;
  814. char buf[1024];
  815. jpc_com_t *com;
  816. cp = enc->cp;
  817. startoff = jas_stream_getrwcount(enc->out);
  818. /* Write SOC marker segment. */
  819. if (!(enc->mrk = jpc_ms_create(JPC_MS_SOC))) {
  820. return -1;
  821. }
  822. if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
  823. jas_eprintf("cannot write SOC marker\n");
  824. return -1;
  825. }
  826. jpc_ms_destroy(enc->mrk);
  827. enc->mrk = 0;
  828. /* Write SIZ marker segment. */
  829. if (!(enc->mrk = jpc_ms_create(JPC_MS_SIZ))) {
  830. return -1;
  831. }
  832. siz = &enc->mrk->parms.siz;
  833. siz->caps = 0;
  834. siz->xoff = cp->imgareatlx;
  835. siz->yoff = cp->imgareatly;
  836. siz->width = cp->refgrdwidth;
  837. siz->height = cp->refgrdheight;
  838. siz->tilexoff = cp->tilegrdoffx;
  839. siz->tileyoff = cp->tilegrdoffy;
  840. siz->tilewidth = cp->tilewidth;
  841. siz->tileheight = cp->tileheight;
  842. siz->numcomps = cp->numcmpts;
  843. siz->comps = jas_alloc2(siz->numcomps, sizeof(jpc_sizcomp_t));
  844. assert(siz->comps);
  845. for (i = 0; i < JAS_CAST(int, cp->numcmpts); ++i) {
  846. siz->comps[i].prec = cp->ccps[i].prec;
  847. siz->comps[i].sgnd = cp->ccps[i].sgnd;
  848. siz->comps[i].hsamp = cp->ccps[i].sampgrdstepx;
  849. siz->comps[i].vsamp = cp->ccps[i].sampgrdstepy;
  850. }
  851. if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
  852. jas_eprintf("cannot write SIZ marker\n");
  853. return -1;
  854. }
  855. jpc_ms_destroy(enc->mrk);
  856. enc->mrk = 0;
  857. if (!(enc->mrk = jpc_ms_create(JPC_MS_COM))) {
  858. return -1;
  859. }
  860. sprintf(buf, "Creator: JasPer Version %s", jas_getversion());
  861. com = &enc->mrk->parms.com;
  862. com->len = strlen(buf);
  863. com->regid = JPC_COM_LATIN;
  864. if (!(com->data = JAS_CAST(uchar *, jas_strdup(buf)))) {
  865. abort();
  866. }
  867. if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
  868. jas_eprintf("cannot write COM marker\n");
  869. return -1;
  870. }
  871. jpc_ms_destroy(enc->mrk);
  872. enc->mrk = 0;
  873. #if 0
  874. if (!(enc->mrk = jpc_ms_create(JPC_MS_CRG))) {
  875. return -1;
  876. }
  877. crg = &enc->mrk->parms.crg;
  878. crg->comps = jas_alloc2(crg->numcomps, sizeof(jpc_crgcomp_t));
  879. if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
  880. jas_eprintf("cannot write CRG marker\n");
  881. return -1;
  882. }
  883. jpc_ms_destroy(enc->mrk);
  884. enc->mrk = 0;
  885. #endif
  886. tcp = &cp->tcp;
  887. tccp = &cp->tccp;
  888. for (cmptno = 0; cmptno < cp->numcmpts; ++cmptno) {
  889. tsfb = jpc_cod_gettsfb(tccp->qmfbid, tccp->maxrlvls - 1);
  890. jpc_tsfb_getbands(tsfb, 0, 0, 1 << tccp->maxrlvls, 1 << tccp->maxrlvls,
  891. bandinfos);
  892. jpc_tsfb_destroy(tsfb);
  893. mctsynweight = jpc_mct_getsynweight(tcp->mctid, cmptno);
  894. numbands = 3 * tccp->maxrlvls - 2;
  895. for (bandno = 0, bandinfo = bandinfos; bandno < numbands;
  896. ++bandno, ++bandinfo) {
  897. rlvlno = (bandno) ? ((bandno - 1) / 3 + 1) : 0;
  898. analgain = JPC_NOMINALGAIN(tccp->qmfbid, tccp->maxrlvls,
  899. rlvlno, bandinfo->orient);
  900. if (!tcp->intmode) {
  901. absstepsize = jpc_fix_div(jpc_inttofix(1 <<
  902. (analgain + 1)), bandinfo->synenergywt);
  903. } else {
  904. absstepsize = jpc_inttofix(1);
  905. }
  906. cp->ccps[cmptno].stepsizes[bandno] =
  907. jpc_abstorelstepsize(absstepsize,
  908. cp->ccps[cmptno].prec + analgain);
  909. }
  910. cp->ccps[cmptno].numstepsizes = numbands;
  911. }
  912. if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) {
  913. return -1;
  914. }
  915. cod = &enc->mrk->parms.cod;
  916. cod->csty = cp->tccp.csty | cp->tcp.csty;
  917. cod->compparms.csty = cp->tccp.csty | cp->tcp.csty;
  918. cod->compparms.numdlvls = cp->tccp.maxrlvls - 1;
  919. cod->compparms.numrlvls = cp->tccp.maxrlvls;
  920. cod->prg = cp->tcp.prg;
  921. cod->numlyrs = cp->tcp.numlyrs;
  922. cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkwidthexpn);
  923. cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkheightexpn);
  924. cod->compparms.cblksty = cp->tccp.cblksty;
  925. cod->compparms.qmfbid = cp->tccp.qmfbid;
  926. cod->mctrans = (cp->tcp.mctid != JPC_MCT_NONE);
  927. if (tccp->csty & JPC_COX_PRT) {
  928. for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) {
  929. cod->compparms.rlvls[rlvlno].parwidthval = tccp->prcwidthexpns[rlvlno];
  930. cod->compparms.rlvls[rlvlno].parheightval = tccp->prcheightexpns[rlvlno];
  931. }
  932. }
  933. if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
  934. jas_eprintf("cannot write COD marker\n");
  935. return -1;
  936. }
  937. jpc_ms_destroy(enc->mrk);
  938. enc->mrk = 0;
  939. if (!(enc->mrk = jpc_ms_create(JPC_MS_QCD))) {
  940. return -1;
  941. }
  942. qcd = &enc->mrk->parms.qcd;
  943. qcd->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ?
  944. JPC_QCX_SEQNT : JPC_QCX_NOQNT;
  945. qcd->compparms.numstepsizes = cp->ccps[0].numstepsizes;
  946. qcd->compparms.numguard = cp->tccp.numgbits;
  947. qcd->compparms.stepsizes = cp->ccps[0].stepsizes;
  948. if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
  949. return -1;
  950. }
  951. /* We do not want the step size array to be freed! */
  952. qcd->compparms.stepsizes = 0;
  953. jpc_ms_destroy(enc->mrk);
  954. enc->mrk = 0;
  955. tccp = &cp->tccp;
  956. for (cmptno = 1; cmptno < cp->numcmpts; ++cmptno) {
  957. if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) {
  958. return -1;
  959. }
  960. qcc = &enc->mrk->parms.qcc;
  961. qcc->compno = cmptno;
  962. qcc->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ?
  963. JPC_QCX_SEQNT : JPC_QCX_NOQNT;
  964. qcc->compparms.numstepsizes = cp->ccps[cmptno].numstepsizes;
  965. qcc->compparms.numguard = cp->tccp.numgbits;
  966. qcc->compparms.stepsizes = cp->ccps[cmptno].stepsizes;
  967. if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
  968. return -1;
  969. }
  970. /* We do not want the step size array to be freed! */
  971. qcc->compparms.stepsizes = 0;
  972. jpc_ms_destroy(enc->mrk);
  973. enc->mrk = 0;
  974. }
  975. #define MAINTLRLEN 2
  976. mainhdrlen = jas_stream_getrwcount(enc->out) - startoff;
  977. enc->len += mainhdrlen;
  978. if (enc->cp->totalsize != UINT_FAST32_MAX) {
  979. uint_fast32_t overhead;
  980. overhead = mainhdrlen + MAINTLRLEN;
  981. enc->mainbodysize = (enc->cp->totalsize >= overhead) ?
  982. (enc->cp->totalsize - overhead) : 0;
  983. } else {
  984. enc->mainbodysize = UINT_FAST32_MAX;
  985. }
  986. return 0;
  987. }
  988. static int jpc_enc_encodemainbody(jpc_enc_t *enc)
  989. {
  990. int tileno;
  991. int tilex;
  992. int tiley;
  993. int i;
  994. jpc_sot_t *sot;
  995. jpc_enc_tcmpt_t *comp;
  996. jpc_enc_tcmpt_t *endcomps;
  997. jpc_enc_band_t *band;
  998. jpc_enc_band_t *endbands;
  999. jpc_enc_rlvl_t *lvl;
  1000. int rlvlno;
  1001. jpc_qcc_t *qcc;
  1002. jpc_cod_t *cod;
  1003. int adjust;
  1004. int j;
  1005. int absbandno;
  1006. long numbytes;
  1007. long tilehdrlen;
  1008. long tilelen;
  1009. jpc_enc_tile_t *tile;
  1010. jpc_enc_cp_t *cp;
  1011. double rho;
  1012. int lyrno;
  1013. int cmptno;
  1014. int samestepsizes;
  1015. jpc_enc_ccp_t *ccps;
  1016. jpc_enc_tccp_t *tccp;
  1017. int bandno;
  1018. uint_fast32_t x;
  1019. uint_fast32_t y;
  1020. int mingbits;
  1021. int actualnumbps;
  1022. jpc_fix_t mxmag;
  1023. jpc_fix_t mag;
  1024. int numgbits;
  1025. cp = enc->cp;
  1026. /* Avoid compile warnings. */
  1027. numbytes = 0;
  1028. for (tileno = 0; tileno < JAS_CAST(int, cp->numtiles); ++tileno) {
  1029. tilex = tileno % cp->numhtiles;
  1030. tiley = tileno / cp->numhtiles;
  1031. if (!(enc->curtile = jpc_enc_tile_create(enc->cp, enc->image, tileno))) {
  1032. abort();
  1033. }
  1034. tile = enc->curtile;
  1035. if (jas_getdbglevel() >= 10) {
  1036. jpc_enc_dump(enc);
  1037. }
  1038. endcomps = &tile->tcmpts[tile->numtcmpts];
  1039. for (cmptno = 0, comp = tile->tcmpts; cmptno < tile->numtcmpts; ++cmptno, ++comp) {
  1040. if (!cp->ccps[cmptno].sgnd) {
  1041. adjust = 1 << (cp->ccps[cmptno].prec - 1);
  1042. for (i = 0; i < jas_matrix_numrows(comp->data); ++i) {
  1043. for (j = 0; j < jas_matrix_numcols(comp->data); ++j) {
  1044. *jas_matrix_getref(comp->data, i, j) -= adjust;
  1045. }
  1046. }
  1047. }
  1048. }
  1049. if (!tile->intmode) {
  1050. endcomps = &tile->tcmpts[tile->numtcmpts];
  1051. for (comp = tile->tcmpts; comp != endcomps; ++comp) {
  1052. jas_matrix_asl(comp->data, JPC_FIX_FRACBITS);
  1053. }
  1054. }
  1055. switch (tile->mctid) {
  1056. case JPC_MCT_RCT:
  1057. assert(jas_image_numcmpts(enc->image) == 3);
  1058. jpc_rct(tile->tcmpts[0].data, tile->tcmpts[1].data,
  1059. tile->tcmpts[2].data);
  1060. break;
  1061. case JPC_MCT_ICT:
  1062. assert(jas_image_numcmpts(enc->image) == 3);
  1063. jpc_ict(tile->tcmpts[0].data, tile->tcmpts[1].data,
  1064. tile->tcmpts[2].data);
  1065. break;
  1066. default:
  1067. break;
  1068. }
  1069. for (i = 0; i < jas_image_numcmpts(enc->image); ++i) {
  1070. comp = &tile->tcmpts[i];
  1071. jpc_tsfb_analyze(comp->tsfb, comp->data);
  1072. }
  1073. endcomps = &tile->tcmpts[tile->numtcmpts];
  1074. for (cmptno = 0, comp = tile->tcmpts; comp != endcomps; ++cmptno, ++comp) {
  1075. mingbits = 0;
  1076. absbandno = 0;
  1077. /* All bands must have a corresponding quantizer step size,
  1078. even if they contain no samples and are never coded. */
  1079. /* Some bands may not be hit by the loop below, so we must
  1080. initialize all of the step sizes to a sane value. */
  1081. memset(comp->stepsizes, 0, sizeof(comp->stepsizes));
  1082. for (rlvlno = 0, lvl = comp->rlvls; rlvlno < comp->numrlvls; ++rlvlno, ++lvl) {
  1083. if (!lvl->bands) {
  1084. absbandno += rlvlno ? 3 : 1;
  1085. continue;
  1086. }
  1087. endbands = &lvl->bands[lvl->numbands];
  1088. for (band = lvl->bands; band != endbands; ++band) {
  1089. if (!band->data) {
  1090. ++absbandno;
  1091. continue;
  1092. }
  1093. actualnumbps = 0;
  1094. mxmag = 0;
  1095. for (y = 0; y < JAS_CAST(uint_fast32_t, jas_matrix_numrows(band->data)); ++y) {
  1096. for (x = 0; x < JAS_CAST(uint_fast32_t, jas_matrix_numcols(band->data)); ++x) {
  1097. mag = abs(jas_matrix_get(band->data, y, x));
  1098. if (mag > mxmag) {
  1099. mxmag = mag;
  1100. }
  1101. }
  1102. }
  1103. if (tile->intmode) {
  1104. actualnumbps = jpc_firstone(mxmag) + 1;
  1105. } else {
  1106. actualnumbps = jpc_firstone(mxmag) + 1 - JPC_FIX_FRACBITS;
  1107. }
  1108. numgbits = actualnumbps - (cp->ccps[cmptno].prec - 1 +
  1109. band->analgain);
  1110. #if 0
  1111. jas_eprintf("%d %d mag=%d actual=%d numgbits=%d\n", cp->ccps[cmptno].prec, band->analgain, mxmag, actualnumbps, numgbits);
  1112. #endif
  1113. if (numgbits > mingbits) {
  1114. mingbits = numgbits;
  1115. }
  1116. if (!tile->intmode) {
  1117. band->absstepsize = jpc_fix_div(jpc_inttofix(1
  1118. << (band->analgain + 1)),
  1119. band->synweight);
  1120. } else {
  1121. band->absstepsize = jpc_inttofix(1);
  1122. }
  1123. band->stepsize = jpc_abstorelstepsize(
  1124. band->absstepsize, cp->ccps[cmptno].prec +
  1125. band->analgain);
  1126. band->numbps = cp->tccp.numgbits +
  1127. JPC_QCX_GETEXPN(band->stepsize) - 1;
  1128. if ((!tile->intmode) && band->data) {
  1129. jpc_quantize(band->data, band->absstepsize);
  1130. }
  1131. comp->stepsizes[absbandno] = band->stepsize;
  1132. ++absbandno;
  1133. }
  1134. }
  1135. assert(JPC_FIX_FRACBITS >= JPC_NUMEXTRABITS);
  1136. if (!tile->intmode) {
  1137. jas_matrix_divpow2(comp->data, JPC_FIX_FRACBITS - JPC_NUMEXTRABITS);
  1138. } else {
  1139. jas_matrix_asl(comp->data, JPC_NUMEXTRABITS);
  1140. }
  1141. #if 0
  1142. jas_eprintf("mingbits %d\n", mingbits);
  1143. #endif
  1144. if (mingbits > cp->tccp.numgbits) {
  1145. jas_eprintf("error: too few guard bits (need at least %d)\n",
  1146. mingbits);
  1147. return -1;
  1148. }
  1149. }
  1150. if (!(enc->tmpstream = jas_stream_memopen(0, 0))) {
  1151. jas_eprintf("cannot open tmp file\n");
  1152. return -1;
  1153. }
  1154. /* Write the tile header. */
  1155. if (!(enc->mrk = jpc_ms_create(JPC_MS_SOT))) {
  1156. return -1;
  1157. }
  1158. sot = &enc->mrk->parms.sot;
  1159. sot->len = 0;
  1160. sot->tileno = tileno;
  1161. sot->partno = 0;
  1162. sot->numparts = 1;
  1163. if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
  1164. jas_eprintf("cannot write SOT marker\n");
  1165. return -1;
  1166. }
  1167. jpc_ms_destroy(enc->mrk);
  1168. enc->mrk = 0;
  1169. /************************************************************************/
  1170. /************************************************************************/
  1171. /************************************************************************/
  1172. tccp = &cp->tccp;
  1173. for (cmptno = 0; cmptno < JAS_CAST(int, cp->numcmpts); ++cmptno) {
  1174. comp = &tile->tcmpts[cmptno];
  1175. if (comp->numrlvls != tccp->maxrlvls) {
  1176. if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) {
  1177. return -1;
  1178. }
  1179. /* XXX = this is not really correct. we are using comp #0's precint sizes
  1180. and other characteristics */
  1181. comp = &tile->tcmpts[0];
  1182. cod = &enc->mrk->parms.cod;
  1183. cod->compparms.csty = 0;
  1184. cod->compparms.numdlvls = comp->numrlvls - 1;
  1185. cod->prg = tile->prg;
  1186. cod->numlyrs = tile->numlyrs;
  1187. cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(comp->cblkwidthexpn);
  1188. cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(comp->cblkheightexpn);
  1189. cod->compparms.cblksty = comp->cblksty;
  1190. cod->compparms.qmfbid = comp->qmfbid;
  1191. cod->mctrans = (tile->mctid != JPC_MCT_NONE);
  1192. for (i = 0; i < comp->numrlvls; ++i) {
  1193. cod->compparms.rlvls[i].parwidthval = comp->rlvls[i].prcwidthexpn;
  1194. cod->compparms.rlvls[i].parheightval = comp->rlvls[i].prcheightexpn;
  1195. }
  1196. if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
  1197. return -1;
  1198. }
  1199. jpc_ms_destroy(enc->mrk);
  1200. enc->mrk = 0;
  1201. }
  1202. }
  1203. for (cmptno = 0, comp = tile->tcmpts; cmptno < JAS_CAST(int, cp->numcmpts); ++cmptno, ++comp) {
  1204. ccps = &cp->ccps[cmptno];
  1205. if (JAS_CAST(int, ccps->numstepsizes) == comp->numstepsizes) {
  1206. samestepsizes = 1;
  1207. for (bandno = 0; bandno < JAS_CAST(int, ccps->numstepsizes); ++bandno) {
  1208. if (ccps->stepsizes[bandno] != comp->stepsizes[bandno]) {
  1209. samestepsizes = 0;
  1210. break;
  1211. }
  1212. }
  1213. } else {
  1214. samestepsizes = 0;
  1215. }
  1216. if (!samestepsizes) {
  1217. if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) {
  1218. return -1;
  1219. }
  1220. qcc = &enc->mrk->parms.qcc;
  1221. qcc->compno = cmptno;
  1222. qcc->compparms.numguard = cp->tccp.numgbits;
  1223. qcc->compparms.qntsty = (comp->qmfbid == JPC_COX_INS) ?
  1224. JPC_QCX_SEQNT : JPC_QCX_NOQNT;
  1225. qcc->compparms.numstepsizes = comp->numstepsizes;
  1226. qcc->compparms.stepsizes = comp->stepsizes;
  1227. if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
  1228. return -1;
  1229. }
  1230. qcc->compparms.stepsizes = 0;
  1231. jpc_ms_destroy(enc->mrk);
  1232. enc->mrk = 0;
  1233. }
  1234. }
  1235. /* Write a SOD marker to indicate the end of the tile header. */
  1236. if (!(enc->mrk = jpc_ms_create(JPC_MS_SOD))) {
  1237. return -1;
  1238. }
  1239. if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
  1240. jas_eprintf("cannot write SOD marker\n");
  1241. return -1;
  1242. }
  1243. jpc_ms_destroy(enc->mrk);
  1244. enc->mrk = 0;
  1245. tilehdrlen = jas_stream_getrwcount(enc->tmpstream);
  1246. /************************************************************************/
  1247. /************************************************************************/
  1248. /************************************************************************/
  1249. if (jpc_enc_enccblks(enc)) {
  1250. abort();
  1251. return -1;
  1252. }
  1253. cp = enc->cp;
  1254. rho = (double) (tile->brx - tile->tlx) * (tile->bry - tile->tly) /
  1255. ((cp->refgrdwidth - cp->imgareatlx) * (cp->refgrdheight -
  1256. cp->imgareatly));
  1257. tile->rawsize = cp->rawsize * rho;
  1258. for (lyrno = 0; lyrno < tile->numlyrs - 1; ++lyrno) {
  1259. tile->lyrsizes[lyrno] = tile->rawsize * jpc_fixtodbl(
  1260. cp->tcp.ilyrrates[lyrno]);
  1261. }
  1262. tile->lyrsizes[tile->numlyrs - 1] = (cp->totalsize != UINT_FAST32_MAX) ?
  1263. (rho * enc->mainbodysize) : UINT_FAST32_MAX;
  1264. for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) {
  1265. if (tile->lyrsizes[lyrno] != UINT_FAST32_MAX) {
  1266. if (tilehdrlen <= JAS_CAST(long, tile->lyrsizes[lyrno])) {
  1267. tile->lyrsizes[lyrno] -= tilehdrlen;
  1268. } else {
  1269. tile->lyrsizes[lyrno] = 0;
  1270. }
  1271. }
  1272. }
  1273. if (rateallocate(enc, tile->numlyrs, tile->lyrsizes)) {
  1274. return -1;
  1275. }
  1276. #if 0
  1277. jas_eprintf("ENCODE TILE DATA\n");
  1278. #endif
  1279. if (jpc_enc_encodetiledata(enc)) {
  1280. jas_eprintf("dotile failed\n");
  1281. return -1;
  1282. }
  1283. /************************************************************************/
  1284. /************************************************************************/
  1285. /************************************************************************/
  1286. /************************************************************************/
  1287. /************************************************************************/
  1288. /************************************************************************/
  1289. tilelen = jas_stream_tell(enc->tmpstream);
  1290. if (jas_stream_seek(enc->tmpstream, 6, SEEK_SET) < 0) {
  1291. return -1;
  1292. }
  1293. jpc_putuint32(enc->tmpstream, tilelen);
  1294. if (jas_stream_seek(enc->tmpstream, 0, SEEK_SET) < 0) {
  1295. return -1;
  1296. }
  1297. if (jpc_putdata(enc->out, enc->tmpstream, -1)) {
  1298. return -1;
  1299. }
  1300. enc->len += tilelen;
  1301. jas_stream_close(enc->tmpstream);
  1302. enc->tmpstream = 0;
  1303. jpc_enc_tile_destroy(enc->curtile);
  1304. enc->curtile = 0;
  1305. }
  1306. return 0;
  1307. }
  1308. int jpc_enc_encodetiledata(jpc_enc_t *enc)
  1309. {
  1310. assert(enc->tmpstream);
  1311. if (jpc_enc_encpkts(enc, enc->tmpstream)) {
  1312. return -1;
  1313. }
  1314. return 0;
  1315. }
  1316. int dump_passes(jpc_enc_pass_t *passes, int numpasses, jpc_enc_cblk_t *cblk)
  1317. {
  1318. jpc_enc_pass_t *pass;
  1319. int i;
  1320. jas_stream_memobj_t *smo;
  1321. smo = cblk->stream->obj_;
  1322. pass = passes;
  1323. for (i = 0; i < numpasses; ++i) {
  1324. jas_eprintf("start=%d end=%d type=%d term=%d lyrno=%d firstchar=%02x size=%ld pos=%ld\n",
  1325. (int)pass->start, (int)pass->end, (int)pass->type, (int)pass->term, (int)pass->lyrno,
  1326. smo->buf_[pass->start], (long)smo->len_, (long)smo->pos_);
  1327. #if 0
  1328. jas_memdump(stderr, &smo->buf_[pass->start], pass->end - pass->start);
  1329. #endif
  1330. ++pass;
  1331. }
  1332. return 0;
  1333. }
  1334. void jpc_quantize(jas_matrix_t *data, jpc_fix_t stepsize)
  1335. {
  1336. int i;
  1337. int j;
  1338. jpc_fix_t t;
  1339. if (stepsize == jpc_inttofix(1)) {
  1340. return;
  1341. }
  1342. for (i = 0; i < jas_matrix_numrows(data); ++i) {
  1343. for (j = 0; j < jas_matrix_numcols(data); ++j) {
  1344. t = jas_matrix_get(data, i, j);
  1345. {
  1346. if (t < 0) {
  1347. t = jpc_fix_neg(jpc_fix_div(jpc_fix_neg(t), stepsize));
  1348. } else {
  1349. t = jpc_fix_div(t, stepsize);
  1350. }
  1351. }
  1352. jas_matrix_set(data, i, j, t);
  1353. }
  1354. }
  1355. }
  1356. void calcrdslopes(jpc_enc_cblk_t *cblk)
  1357. {
  1358. jpc_enc_pass_t *endpasses;
  1359. jpc_enc_pass_t *pass0;
  1360. jpc_enc_pass_t *pass1;
  1361. jpc_enc_pass_t *pass2;
  1362. jpc_flt_t slope0;
  1363. jpc_flt_t slope;
  1364. jpc_flt_t dd;
  1365. long dr;
  1366. endpasses = &cblk->passes[cblk->numpasses];
  1367. pass2 = cblk->passes;
  1368. slope0 = 0;
  1369. while (pass2 != endpasses) {
  1370. pass0 = 0;
  1371. for (pass1 = cblk->passes; pass1 != endpasses; ++pass1) {
  1372. dd = pass1->cumwmsedec;
  1373. dr = pass1->end;
  1374. if (pass0) {
  1375. dd -= pass0->cumwmsedec;
  1376. dr -= pass0->end;
  1377. }
  1378. if (dd <= 0) {
  1379. pass1->rdslope = JPC_BADRDSLOPE;
  1380. if (pass1 >= pass2) {
  1381. pass2 = &pass1[1];
  1382. }
  1383. continue;
  1384. }
  1385. if (pass1 < pass2 && pass1->rdslope <= 0) {
  1386. continue;
  1387. }
  1388. if (!dr) {
  1389. assert(pass0);
  1390. pass0->rdslope = 0;
  1391. break;
  1392. }
  1393. slope = dd / dr;
  1394. if (pass0 && slope >= slope0) {
  1395. pass0->rdslope = 0;
  1396. break;
  1397. }
  1398. pass1->rdslope = slope;
  1399. if (pass1 >= pass2) {
  1400. pass2 = &pass1[1];
  1401. }
  1402. pass0 = pass1;
  1403. slope0 = slope;
  1404. }
  1405. }
  1406. #if 0
  1407. for (pass0 = cblk->passes; pass0 != endpasses; ++pass0) {
  1408. if (pass0->rdslope > 0.0) {
  1409. jas_eprintf("pass %02d nmsedec=%lf dec=%lf end=%d %lf\n", pass0 - cblk->passes,
  1410. fixtodbl(pass0->nmsedec), pass0->wmsedec, pass0->end, pass0->rdslope);
  1411. }
  1412. }
  1413. #endif
  1414. }
  1415. void dump_layeringinfo(jpc_enc_t *enc)
  1416. {
  1417. jpc_enc_tcmpt_t *tcmpt;
  1418. int tcmptno;
  1419. jpc_enc_rlvl_t *rlvl;
  1420. int rlvlno;
  1421. jpc_enc_band_t *band;
  1422. int bandno;
  1423. jpc_enc_prc_t *prc;
  1424. int prcno;
  1425. jpc_enc_cblk_t *cblk;
  1426. int cblkno;
  1427. jpc_enc_pass_t *pass;
  1428. int passno;
  1429. int lyrno;
  1430. jpc_enc_tile_t *tile;
  1431. tile = enc->curtile;
  1432. for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) {
  1433. jas_eprintf("lyrno = %02d\n", lyrno);
  1434. for (tcmptno = 0, tcmpt = tile->tcmpts; tcmptno < tile->numtcmpts;
  1435. ++tcmptno, ++tcmpt) {
  1436. for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
  1437. ++rlvlno, ++rlvl) {
  1438. if (!rlvl->bands) {
  1439. continue;
  1440. }
  1441. for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
  1442. ++bandno, ++band) {
  1443. if (!band->data) {
  1444. continue;
  1445. }
  1446. for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs;
  1447. ++prcno, ++prc) {
  1448. if (!prc->cblks) {
  1449. continue;
  1450. }
  1451. for (cblkno = 0, cblk = prc->cblks; cblkno <
  1452. prc->numcblks; ++cblkno, ++cblk) {
  1453. for (passno = 0, pass = cblk->passes; passno <
  1454. cblk->numpasses && pass->lyrno == lyrno;
  1455. ++passno, ++pass) {
  1456. jas_eprintf("lyrno=%02d cmptno=%02d rlvlno=%02d bandno=%02d prcno=%02d cblkno=%03d passno=%03d\n", lyrno, tcmptno, rlvlno, bandno, prcno, cblkno, passno);
  1457. }
  1458. }
  1459. }
  1460. }
  1461. }
  1462. }
  1463. }
  1464. }
  1465. int rateallocate(jpc_enc_t *enc, int numlyrs, uint_fast32_t *cumlens)
  1466. {
  1467. jpc_flt_t lo;
  1468. jpc_flt_t hi;
  1469. jas_stream_t *out;
  1470. long cumlen;
  1471. int lyrno;
  1472. jpc_flt_t thresh;
  1473. jpc_flt_t goodthresh;
  1474. int success;
  1475. long pos;
  1476. long oldpos;
  1477. int numiters;
  1478. jpc_enc_tcmpt_t *comp;
  1479. jpc_enc_tcmpt_t *endcomps;
  1480. jpc_enc_rlvl_t *lvl;
  1481. jpc_enc_rlvl_t *endlvls;
  1482. jpc_enc_band_t *band;
  1483. jpc_enc_band_t *endbands;
  1484. jpc_enc_cblk_t *cblk;
  1485. jpc_enc_cblk_t *endcblks;
  1486. jpc_enc_pass_t *pass;
  1487. jpc_enc_pass_t *endpasses;
  1488. jpc_enc_pass_t *pass1;
  1489. jpc_flt_t mxrdslope;
  1490. jpc_flt_t mnrdslope;
  1491. jpc_enc_tile_t *tile;
  1492. jpc_enc_prc_t *prc;
  1493. int prcno;
  1494. tile = enc->curtile;
  1495. for (lyrno = 1; lyrno < numlyrs - 1; ++lyrno) {
  1496. if (cumlens[lyrno - 1] > cumlens[lyrno]) {
  1497. abort();
  1498. }
  1499. }
  1500. if (!(out = jas_stream_memopen(0, 0))) {
  1501. return -1;
  1502. }
  1503. /* Find minimum and maximum R-D slope values. */
  1504. mnrdslope = DBL_MAX;
  1505. mxrdslope = 0;
  1506. endcomps = &tile->tcmpts[tile->numtcmpts];
  1507. for (comp = tile->tcmpts; comp != endcomps; ++comp) {
  1508. endlvls = &comp->rlvls[comp->numrlvls];
  1509. for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
  1510. if (!lvl->bands) {
  1511. continue;
  1512. }
  1513. endbands = &lvl->bands[lvl->numbands];
  1514. for (band = lvl->bands; band != endbands; ++band) {
  1515. if (!band->data) {
  1516. continue;
  1517. }
  1518. for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
  1519. if (!prc->cblks) {
  1520. continue;
  1521. }
  1522. endcblks = &prc->cblks[prc->numcblks];
  1523. for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
  1524. calcrdslopes(cblk);
  1525. endpasses = &cblk->passes[cblk->numpasses];
  1526. for (pass = cblk->passes; pass != endpasses; ++pass) {
  1527. if (pass->rdslope > 0) {
  1528. if (pass->rdslope < mnrdslope) {
  1529. mnrdslope = pass->rdslope;
  1530. }
  1531. if (pass->rdslope > mxrdslope) {
  1532. mxrdslope = pass->rdslope;
  1533. }
  1534. }
  1535. }
  1536. }
  1537. }
  1538. }
  1539. }
  1540. }
  1541. if (jas_getdbglevel()) {
  1542. jas_eprintf("min rdslope = %f max rdslope = %f\n", mnrdslope, mxrdslope);
  1543. }
  1544. jpc_init_t2state(enc, 1);
  1545. for (lyrno = 0; lyrno < numlyrs; ++lyrno) {
  1546. lo = mnrdslope;
  1547. hi = mxrdslope;
  1548. success = 0;
  1549. goodthresh = 0;
  1550. numiters = 0;
  1551. do {
  1552. cumlen = cumlens[lyrno];
  1553. if (cumlen == UINT_FAST32_MAX) {
  1554. /* Only the last layer can be free of a rate
  1555. constraint (e.g., for lossless coding). */
  1556. assert(lyrno == numlyrs - 1);
  1557. goodthresh = -1;
  1558. success = 1;
  1559. break;
  1560. }
  1561. thresh = (lo + hi) / 2;
  1562. /* Save the tier 2 coding state. */
  1563. jpc_save_t2state(enc);
  1564. oldpos = jas_stream_tell(out);
  1565. assert(oldpos >= 0);
  1566. /* Assign all passes with R-D slopes greater than or
  1567. equal to the current threshold to this layer. */
  1568. endcomps = &tile->tcmpts[tile->numtcmpts];
  1569. for (comp = tile->tcmpts; comp != endcomps; ++comp) {
  1570. endlvls = &comp->rlvls[comp->numrlvls];
  1571. for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
  1572. if (!lvl->bands) {
  1573. continue;
  1574. }
  1575. endbands = &lvl->bands[lvl->numbands];
  1576. for (band = lvl->bands; band != endbands; ++band) {
  1577. if (!band->data) {
  1578. continue;
  1579. }
  1580. for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
  1581. if (!prc->cblks) {
  1582. continue;
  1583. }
  1584. endcblks = &prc->cblks[prc->numcblks];
  1585. for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
  1586. if (cblk->curpass) {
  1587. endpasses = &cblk->passes[cblk->numpasses];
  1588. pass1 = cblk->curpass;
  1589. for (pass = cblk->curpass; pass != endpasses; ++pass) {
  1590. if (pass->rdslope >= thresh) {
  1591. pass1 = &pass[1];
  1592. }
  1593. }
  1594. for (pass = cblk->curpass; pass != pass1; ++pass) {
  1595. pass->lyrno = lyrno;
  1596. }
  1597. for (; pass != endpasses; ++pass) {
  1598. pass->lyrno = -1;
  1599. }
  1600. }
  1601. }
  1602. }
  1603. }
  1604. }
  1605. }
  1606. /* Perform tier 2 coding. */
  1607. endcomps = &tile->tcmpts[tile->numtcmpts];
  1608. for (comp = tile->tcmpts; comp != endcomps; ++comp) {
  1609. endlvls = &comp->rlvls[comp->numrlvls];
  1610. for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
  1611. if (!lvl->bands) {
  1612. continue;
  1613. }
  1614. for (prcno = 0; prcno < lvl->numprcs; ++prcno) {
  1615. if (jpc_enc_encpkt(enc, out, comp - tile->tcmpts, lvl - comp->rlvls, prcno, lyrno)) {
  1616. return -1;
  1617. }
  1618. }
  1619. }
  1620. }
  1621. pos = jas_stream_tell(out);
  1622. /* Check the rate constraint. */
  1623. assert(pos >= 0);
  1624. if (pos > cumlen) {
  1625. /* The rate is too high. */
  1626. lo = thresh;
  1627. } else if (pos <= cumlen) {
  1628. /* The rate is low enough, so try higher. */
  1629. hi = thresh;
  1630. if (!success || thresh < goodthresh) {
  1631. goodthresh = thresh;
  1632. success = 1;
  1633. }
  1634. }
  1635. /* Save the tier 2 coding state. */
  1636. jpc_restore_t2state(enc);
  1637. if (jas_stream_seek(out, oldpos, SEEK_SET) < 0) {
  1638. abort();
  1639. }
  1640. if (jas_getdbglevel()) {
  1641. jas_eprintf("maxlen=%08ld actuallen=%08ld thresh=%f\n", cumlen, pos, thresh);
  1642. }
  1643. ++numiters;
  1644. } while (lo < hi - 1e-3 && numiters < 32);
  1645. if (!success) {
  1646. jas_eprintf("warning: empty layer generated\n");
  1647. }
  1648. if (jas_getdbglevel()) {
  1649. jas_eprintf("success %d goodthresh %f\n", success, goodthresh);
  1650. }
  1651. /* Assign all passes with R-D slopes greater than or
  1652. equal to the selected threshold to this layer. */
  1653. endcomps = &tile->tcmpts[tile->numtcmpts];
  1654. for (comp = tile->tcmpts; comp != endcomps; ++comp) {
  1655. endlvls = &comp->rlvls[comp->numrlvls];
  1656. for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
  1657. if (!lvl->bands) {
  1658. continue;
  1659. }
  1660. endbands = &lvl->bands[lvl->numbands];
  1661. for (band = lvl->bands; band != endbands; ++band) {
  1662. if (!band->data) {
  1663. continue;
  1664. }
  1665. for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
  1666. if (!prc->cblks) {
  1667. continue;
  1668. }
  1669. endcblks = &prc->cblks[prc->numcblks];
  1670. for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
  1671. if (cblk->curpass) {
  1672. endpasses = &cblk->passes[cblk->numpasses];
  1673. pass1 = cblk->curpass;
  1674. if (success) {
  1675. for (pass = cblk->curpass; pass != endpasses; ++pass) {
  1676. if (pass->rdslope >= goodthresh) {
  1677. pass1 = &pass[1];
  1678. }
  1679. }
  1680. }
  1681. for (pass = cblk->curpass; pass != pass1; ++pass) {
  1682. pass->lyrno = lyrno;
  1683. }
  1684. for (; pass != endpasses; ++pass) {
  1685. pass->lyrno = -1;
  1686. }
  1687. }
  1688. }
  1689. }
  1690. }
  1691. }
  1692. }
  1693. /* Perform tier 2 coding. */
  1694. endcomps = &tile->tcmpts[tile->numtcmpts];
  1695. for (comp = tile->tcmpts; comp != endcomps; ++comp) {
  1696. endlvls = &comp->rlvls[comp->numrlvls];
  1697. for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
  1698. if (!lvl->bands) {
  1699. continue;
  1700. }
  1701. for (prcno = 0; prcno < lvl->numprcs; ++prcno) {
  1702. if (jpc_enc_encpkt(enc, out, comp - tile->tcmpts, lvl - comp->rlvls, prcno, lyrno)) {
  1703. return -1;
  1704. }
  1705. }
  1706. }
  1707. }
  1708. }
  1709. if (jas_getdbglevel() >= 5) {
  1710. dump_layeringinfo(enc);
  1711. }
  1712. jas_stream_close(out);
  1713. JAS_DBGLOG(10, ("done doing rateallocation\n"));
  1714. #if 0
  1715. jas_eprintf("DONE RATE ALLOCATE\n");
  1716. #endif
  1717. return 0;
  1718. }
  1719. /******************************************************************************\
  1720. * Tile constructors and destructors.
  1721. \******************************************************************************/
  1722. jpc_enc_tile_t *jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno)
  1723. {
  1724. jpc_enc_tile_t *tile;
  1725. uint_fast32_t htileno;
  1726. uint_fast32_t vtileno;
  1727. uint_fast16_t lyrno;
  1728. uint_fast16_t cmptno;
  1729. jpc_enc_tcmpt_t *tcmpt;
  1730. if (!(tile = jas_malloc(sizeof(jpc_enc_tile_t)))) {
  1731. goto error;
  1732. }
  1733. /* Initialize a few members used in error recovery. */
  1734. tile->tcmpts = 0;
  1735. tile->lyrsizes = 0;
  1736. tile->numtcmpts = cp->numcmpts;
  1737. tile->pi = 0;
  1738. tile->tileno = tileno;
  1739. htileno = tileno % cp->numhtiles;
  1740. vtileno = tileno / cp->numhtiles;
  1741. /* Calculate the coordinates of the top-left and bottom-right
  1742. corners of the tile. */
  1743. tile->tlx = JAS_MAX(cp->tilegrdoffx + htileno * cp->tilewidth,
  1744. cp->imgareatlx);
  1745. tile->tly = JAS_MAX(cp->tilegrdoffy + vtileno * cp->tileheight,
  1746. cp->imgareatly);
  1747. tile->brx = JAS_MIN(cp->tilegrdoffx + (htileno + 1) * cp->tilewidth,
  1748. cp->refgrdwidth);
  1749. tile->bry = JAS_MIN(cp->tilegrdoffy + (vtileno + 1) * cp->tileheight,
  1750. cp->refgrdheight);
  1751. /* Initialize some tile coding parameters. */
  1752. tile->intmode = cp->tcp.intmode;
  1753. tile->csty = cp->tcp.csty;
  1754. tile->prg = cp->tcp.prg;
  1755. tile->mctid = cp->tcp.mctid;
  1756. tile->numlyrs = cp->tcp.numlyrs;
  1757. if (!(tile->lyrsizes = jas_alloc2(tile->numlyrs,
  1758. sizeof(uint_fast32_t)))) {
  1759. goto error;
  1760. }
  1761. for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) {
  1762. tile->lyrsizes[lyrno] = 0;
  1763. }
  1764. /* Allocate an array for the per-tile-component information. */
  1765. if (!(tile->tcmpts = jas_alloc2(cp->numcmpts, sizeof(jpc_enc_tcmpt_t)))) {
  1766. goto error;
  1767. }
  1768. /* Initialize a few members critical for error recovery. */
  1769. for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts;
  1770. ++cmptno, ++tcmpt) {
  1771. tcmpt->rlvls = 0;
  1772. tcmpt->tsfb = 0;
  1773. tcmpt->data = 0;
  1774. }
  1775. /* Initialize the per-tile-component information. */
  1776. for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts;
  1777. ++cmptno, ++tcmpt) {
  1778. if (!tcmpt_create(tcmpt, cp, image, tile)) {
  1779. goto error;
  1780. }
  1781. }
  1782. /* Initialize the synthesis weights for the MCT. */
  1783. switch (tile->mctid) {
  1784. case JPC_MCT_RCT:
  1785. tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0));
  1786. tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(0.6875));
  1787. tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(0.6875));
  1788. break;
  1789. case JPC_MCT_ICT:
  1790. tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0000));
  1791. tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(3.2584));
  1792. tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(2.4755));
  1793. break;
  1794. default:
  1795. case JPC_MCT_NONE:
  1796. for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts;
  1797. ++cmptno, ++tcmpt) {
  1798. tcmpt->synweight = JPC_FIX_ONE;
  1799. }
  1800. break;
  1801. }
  1802. if (!(tile->pi = jpc_enc_pi_create(cp, tile))) {
  1803. goto error;
  1804. }
  1805. return tile;
  1806. error:
  1807. if (tile) {
  1808. jpc_enc_tile_destroy(tile);
  1809. }
  1810. return 0;
  1811. }
  1812. void jpc_enc_tile_destroy(jpc_enc_tile_t *tile)
  1813. {
  1814. jpc_enc_tcmpt_t *tcmpt;
  1815. uint_fast16_t cmptno;
  1816. if (tile->tcmpts) {
  1817. for (cmptno = 0, tcmpt = tile->tcmpts; cmptno <
  1818. tile->numtcmpts; ++cmptno, ++tcmpt) {
  1819. tcmpt_destroy(tcmpt);
  1820. }
  1821. jas_free(tile->tcmpts);
  1822. }
  1823. if (tile->lyrsizes) {
  1824. jas_free(tile->lyrsizes);
  1825. }
  1826. if (tile->pi) {
  1827. jpc_pi_destroy(tile->pi);
  1828. }
  1829. jas_free(tile);
  1830. }
  1831. static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp,
  1832. jas_image_t *image, jpc_enc_tile_t *tile)
  1833. {
  1834. uint_fast16_t cmptno;
  1835. uint_fast16_t rlvlno;
  1836. jpc_enc_rlvl_t *rlvl;
  1837. uint_fast32_t tlx;
  1838. uint_fast32_t tly;
  1839. uint_fast32_t brx;
  1840. uint_fast32_t bry;
  1841. uint_fast32_t cmpttlx;
  1842. uint_fast32_t cmpttly;
  1843. jpc_enc_ccp_t *ccp;
  1844. jpc_tsfb_band_t bandinfos[JPC_MAXBANDS];
  1845. tcmpt->tile = tile;
  1846. tcmpt->tsfb = 0;
  1847. tcmpt->data = 0;
  1848. tcmpt->rlvls = 0;
  1849. /* Deduce the component number. */
  1850. cmptno = tcmpt - tile->tcmpts;
  1851. ccp = &cp->ccps[cmptno];
  1852. /* Compute the coordinates of the top-left and bottom-right
  1853. corners of this tile-component. */
  1854. tlx = JPC_CEILDIV(tile->tlx, ccp->sampgrdstepx);
  1855. tly = JPC_CEILDIV(tile->tly, ccp->sampgrdstepy);
  1856. brx = JPC_CEILDIV(tile->brx, ccp->sampgrdstepx);
  1857. bry = JPC_CEILDIV(tile->bry, ccp->sampgrdstepy);
  1858. /* Create a sequence to hold the tile-component sample data. */
  1859. if (!(tcmpt->data = jas_seq2d_create(tlx, tly, brx, bry))) {
  1860. goto error;
  1861. }
  1862. /* Get the image data associated with this tile-component. */
  1863. cmpttlx = JPC_CEILDIV(cp->imgareatlx, ccp->sampgrdstepx);
  1864. cmpttly = JPC_CEILDIV(cp->imgareatly, ccp->sampgrdstepy);
  1865. if (jas_image_readcmpt(image, cmptno, tlx - cmpttlx, tly - cmpttly,
  1866. brx - tlx, bry - tly, tcmpt->data)) {
  1867. goto error;
  1868. }
  1869. tcmpt->synweight = 0;
  1870. tcmpt->qmfbid = cp->tccp.qmfbid;
  1871. tcmpt->numrlvls = cp->tccp.maxrlvls;
  1872. tcmpt->numbands = 3 * tcmpt->numrlvls - 2;
  1873. if (!(tcmpt->tsfb = jpc_cod_gettsfb(tcmpt->qmfbid, tcmpt->numrlvls - 1))) {
  1874. goto error;
  1875. }
  1876. for (rlvlno = 0; rlvlno < tcmpt->numrlvls; ++rlvlno) {
  1877. tcmpt->prcwidthexpns[rlvlno] = cp->tccp.prcwidthexpns[rlvlno];
  1878. tcmpt->prcheightexpns[rlvlno] = cp->tccp.prcheightexpns[rlvlno];
  1879. }
  1880. tcmpt->cblkwidthexpn = cp->tccp.cblkwidthexpn;
  1881. tcmpt->cblkheightexpn = cp->tccp.cblkheightexpn;
  1882. tcmpt->cblksty = cp->tccp.cblksty;
  1883. tcmpt->csty = cp->tccp.csty;
  1884. tcmpt->numstepsizes = tcmpt->numbands;
  1885. assert(tcmpt->numstepsizes <= JPC_MAXBANDS);
  1886. memset(tcmpt->stepsizes, 0, tcmpt->numstepsizes *
  1887. sizeof(uint_fast16_t));
  1888. /* Retrieve information about the various bands. */
  1889. jpc_tsfb_getbands(tcmpt->tsfb, jas_seq2d_xstart(tcmpt->data),
  1890. jas_seq2d_ystart(tcmpt->data), jas_seq2d_xend(tcmpt->data),
  1891. jas_seq2d_yend(tcmpt->data), bandinfos);
  1892. if (!(tcmpt->rlvls = jas_alloc2(tcmpt->numrlvls, sizeof(jpc_enc_rlvl_t)))) {
  1893. goto error;
  1894. }
  1895. for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
  1896. ++rlvlno, ++rlvl) {
  1897. rlvl->bands = 0;
  1898. rlvl->tcmpt = tcmpt;
  1899. }
  1900. for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
  1901. ++rlvlno, ++rlvl) {
  1902. if (!rlvl_create(rlvl, cp, tcmpt, bandinfos)) {
  1903. goto error;
  1904. }
  1905. }
  1906. return tcmpt;
  1907. error:
  1908. tcmpt_destroy(tcmpt);
  1909. return 0;
  1910. }
  1911. static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt)
  1912. {
  1913. jpc_enc_rlvl_t *rlvl;
  1914. uint_fast16_t rlvlno;
  1915. if (tcmpt->rlvls) {
  1916. for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
  1917. ++rlvlno, ++rlvl) {
  1918. rlvl_destroy(rlvl);
  1919. }
  1920. jas_free(tcmpt->rlvls);
  1921. }
  1922. if (tcmpt->data) {
  1923. jas_seq2d_destroy(tcmpt->data);
  1924. }
  1925. if (tcmpt->tsfb) {
  1926. jpc_tsfb_destroy(tcmpt->tsfb);
  1927. }
  1928. }
  1929. static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp,
  1930. jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos)
  1931. {
  1932. uint_fast16_t rlvlno;
  1933. uint_fast32_t tlprctlx;
  1934. uint_fast32_t tlprctly;
  1935. uint_fast32_t brprcbrx;
  1936. uint_fast32_t brprcbry;
  1937. uint_fast16_t bandno;
  1938. jpc_enc_band_t *band;
  1939. /* Deduce the resolution level. */
  1940. rlvlno = rlvl - tcmpt->rlvls;
  1941. /* Initialize members required for error recovery. */
  1942. rlvl->bands = 0;
  1943. rlvl->tcmpt = tcmpt;
  1944. /* Compute the coordinates of the top-left and bottom-right
  1945. corners of the tile-component at this resolution. */
  1946. rlvl->tlx = JPC_CEILDIVPOW2(jas_seq2d_xstart(tcmpt->data), tcmpt->numrlvls -
  1947. 1 - rlvlno);
  1948. rlvl->tly = JPC_CEILDIVPOW2(jas_seq2d_ystart(tcmpt->data), tcmpt->numrlvls -
  1949. 1 - rlvlno);
  1950. rlvl->brx = JPC_CEILDIVPOW2(jas_seq2d_xend(tcmpt->data), tcmpt->numrlvls -
  1951. 1 - rlvlno);
  1952. rlvl->bry = JPC_CEILDIVPOW2(jas_seq2d_yend(tcmpt->data), tcmpt->numrlvls -
  1953. 1 - rlvlno);
  1954. if (rlvl->tlx >= rlvl->brx || rlvl->tly >= rlvl->bry) {
  1955. rlvl->numhprcs = 0;
  1956. rlvl->numvprcs = 0;
  1957. rlvl->numprcs = 0;
  1958. return rlvl;
  1959. }
  1960. rlvl->numbands = (!rlvlno) ? 1 : 3;
  1961. rlvl->prcwidthexpn = cp->tccp.prcwidthexpns[rlvlno];
  1962. rlvl->prcheightexpn = cp->tccp.prcheightexpns[rlvlno];
  1963. if (!rlvlno) {
  1964. rlvl->cbgwidthexpn = rlvl->prcwidthexpn;
  1965. rlvl->cbgheightexpn = rlvl->prcheightexpn;
  1966. } else {
  1967. rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1;
  1968. rlvl->cbgheightexpn = rlvl->prcheightexpn - 1;
  1969. }
  1970. rlvl->cblkwidthexpn = JAS_MIN(cp->tccp.cblkwidthexpn, rlvl->cbgwidthexpn);
  1971. rlvl->cblkheightexpn = JAS_MIN(cp->tccp.cblkheightexpn, rlvl->cbgheightexpn);
  1972. /* Compute the number of precincts. */
  1973. tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn);
  1974. tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn);
  1975. brprcbrx = JPC_CEILTOMULTPOW2(rlvl->brx, rlvl->prcwidthexpn);
  1976. brprcbry = JPC_CEILTOMULTPOW2(rlvl->bry, rlvl->prcheightexpn);
  1977. rlvl->numhprcs = JPC_FLOORDIVPOW2(brprcbrx - tlprctlx, rlvl->prcwidthexpn);
  1978. rlvl->numvprcs = JPC_FLOORDIVPOW2(brprcbry - tlprctly, rlvl->prcheightexpn);
  1979. rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs;
  1980. if (!(rlvl->bands = jas_alloc2(rlvl->numbands, sizeof(jpc_enc_band_t)))) {
  1981. goto error;
  1982. }
  1983. for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
  1984. ++bandno, ++band) {
  1985. band->prcs = 0;
  1986. band->data = 0;
  1987. band->rlvl = rlvl;
  1988. }
  1989. for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
  1990. ++bandno, ++band) {
  1991. if (!band_create(band, cp, rlvl, bandinfos)) {
  1992. goto error;
  1993. }
  1994. }
  1995. return rlvl;
  1996. error:
  1997. rlvl_destroy(rlvl);
  1998. return 0;
  1999. }
  2000. static void rlvl_destroy(jpc_enc_rlvl_t *rlvl)
  2001. {
  2002. jpc_enc_band_t *band;
  2003. uint_fast16_t bandno;
  2004. if (rlvl->bands) {
  2005. for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
  2006. ++bandno, ++band) {
  2007. band_destroy(band);
  2008. }
  2009. jas_free(rlvl->bands);
  2010. }
  2011. }
  2012. static jpc_enc_band_t *band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp,
  2013. jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos)
  2014. {
  2015. uint_fast16_t bandno;
  2016. uint_fast16_t gblbandno;
  2017. uint_fast16_t rlvlno;
  2018. jpc_tsfb_band_t *bandinfo;
  2019. jpc_enc_tcmpt_t *tcmpt;
  2020. uint_fast32_t prcno;
  2021. jpc_enc_prc_t *prc;
  2022. tcmpt = rlvl->tcmpt;
  2023. band->data = 0;
  2024. band->prcs = 0;
  2025. band->rlvl = rlvl;
  2026. /* Deduce the resolution level and band number. */
  2027. rlvlno = rlvl - rlvl->tcmpt->rlvls;
  2028. bandno = band - rlvl->bands;
  2029. gblbandno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) + bandno + 1);
  2030. bandinfo = &bandinfos[gblbandno];
  2031. if (bandinfo->xstart != bandinfo->xend && bandinfo->ystart != bandinfo->yend) {
  2032. if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) {
  2033. goto error;
  2034. }
  2035. jas_seq2d_bindsub(band->data, tcmpt->data, bandinfo->locxstart,
  2036. bandinfo->locystart, bandinfo->locxend, bandinfo->locyend);
  2037. jas_seq2d_setshift(band->data, bandinfo->xstart, bandinfo->ystart);
  2038. }
  2039. band->orient = bandinfo->orient;
  2040. band->analgain = JPC_NOMINALGAIN(cp->tccp.qmfbid, tcmpt->numrlvls, rlvlno,
  2041. band->orient);
  2042. band->numbps = 0;
  2043. band->absstepsize = 0;
  2044. band->stepsize = 0;
  2045. band->synweight = bandinfo->synenergywt;
  2046. if (band->data) {
  2047. if (!(band->prcs = jas_alloc2(rlvl->numprcs, sizeof(jpc_enc_prc_t)))) {
  2048. goto error;
  2049. }
  2050. for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno,
  2051. ++prc) {
  2052. prc->cblks = 0;
  2053. prc->incltree = 0;
  2054. prc->nlibtree = 0;
  2055. prc->savincltree = 0;
  2056. prc->savnlibtree = 0;
  2057. prc->band = band;
  2058. }
  2059. for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno,
  2060. ++prc) {
  2061. if (!prc_create(prc, cp, band)) {
  2062. goto error;
  2063. }
  2064. }
  2065. }
  2066. return band;
  2067. error:
  2068. band_destroy(band);
  2069. return 0;
  2070. }
  2071. static void band_destroy(jpc_enc_band_t *band)
  2072. {
  2073. jpc_enc_prc_t *prc;
  2074. jpc_enc_rlvl_t *rlvl;
  2075. uint_fast32_t prcno;
  2076. if (band->prcs) {
  2077. rlvl = band->rlvl;
  2078. for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs;
  2079. ++prcno, ++prc) {
  2080. prc_destroy(prc);
  2081. }
  2082. jas_free(band->prcs);
  2083. }
  2084. if (band->data) {
  2085. jas_seq2d_destroy(band->data);
  2086. }
  2087. }
  2088. static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, jpc_enc_band_t *band)
  2089. {
  2090. uint_fast32_t prcno;
  2091. uint_fast32_t prcxind;
  2092. uint_fast32_t prcyind;
  2093. uint_fast32_t cbgtlx;
  2094. uint_fast32_t cbgtly;
  2095. uint_fast32_t tlprctlx;
  2096. uint_fast32_t tlprctly;
  2097. uint_fast32_t tlcbgtlx;
  2098. uint_fast32_t tlcbgtly;
  2099. uint_fast16_t rlvlno;
  2100. jpc_enc_rlvl_t *rlvl;
  2101. uint_fast32_t tlcblktlx;
  2102. uint_fast32_t tlcblktly;
  2103. uint_fast32_t brcblkbrx;
  2104. uint_fast32_t brcblkbry;
  2105. uint_fast32_t cblkno;
  2106. jpc_enc_cblk_t *cblk;
  2107. jpc_enc_tcmpt_t *tcmpt;
  2108. prc->cblks = 0;
  2109. prc->incltree = 0;
  2110. prc->savincltree = 0;
  2111. prc->nlibtree = 0;
  2112. prc->savnlibtree = 0;
  2113. rlvl = band->rlvl;
  2114. tcmpt = rlvl->tcmpt;
  2115. rlvlno = rlvl - tcmpt->rlvls;
  2116. prcno = prc - band->prcs;
  2117. prcxind = prcno % rlvl->numhprcs;
  2118. prcyind = prcno / rlvl->numhprcs;
  2119. prc->band = band;
  2120. tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn);
  2121. tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn);
  2122. if (!rlvlno) {
  2123. tlcbgtlx = tlprctlx;
  2124. tlcbgtly = tlprctly;
  2125. } else {
  2126. tlcbgtlx = JPC_CEILDIVPOW2(tlprctlx, 1);
  2127. tlcbgtly = JPC_CEILDIVPOW2(tlprctly, 1);
  2128. }
  2129. /* Compute the coordinates of the top-left and bottom-right
  2130. corners of the precinct. */
  2131. cbgtlx = tlcbgtlx + (prcxind << rlvl->cbgwidthexpn);
  2132. cbgtly = tlcbgtly + (prcyind << rlvl->cbgheightexpn);
  2133. prc->tlx = JAS_MAX(jas_seq2d_xstart(band->data), cbgtlx);
  2134. prc->tly = JAS_MAX(jas_seq2d_ystart(band->data), cbgtly);
  2135. prc->brx = JAS_MIN(jas_seq2d_xend(band->data), cbgtlx +
  2136. (1 << rlvl->cbgwidthexpn));
  2137. prc->bry = JAS_MIN(jas_seq2d_yend(band->data), cbgtly +
  2138. (1 << rlvl->cbgheightexpn));
  2139. if (prc->tlx < prc->brx && prc->tly < prc->bry) {
  2140. /* The precinct contains at least one code block. */
  2141. tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn);
  2142. tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn);
  2143. brcblkbrx = JPC_CEILTOMULTPOW2(prc->brx, rlvl->cblkwidthexpn);
  2144. brcblkbry = JPC_CEILTOMULTPOW2(prc->bry, rlvl->cblkheightexpn);
  2145. prc->numhcblks = JPC_FLOORDIVPOW2(brcblkbrx - tlcblktlx,
  2146. rlvl->cblkwidthexpn);
  2147. prc->numvcblks = JPC_FLOORDIVPOW2(brcblkbry - tlcblktly,
  2148. rlvl->cblkheightexpn);
  2149. prc->numcblks = prc->numhcblks * prc->numvcblks;
  2150. if (!(prc->incltree = jpc_tagtree_create(prc->numhcblks,
  2151. prc->numvcblks))) {
  2152. goto error;
  2153. }
  2154. if (!(prc->nlibtree = jpc_tagtree_create(prc->numhcblks,
  2155. prc->numvcblks))) {
  2156. goto error;
  2157. }
  2158. if (!(prc->savincltree = jpc_tagtree_create(prc->numhcblks,
  2159. prc->numvcblks))) {
  2160. goto error;
  2161. }
  2162. if (!(prc->savnlibtree = jpc_tagtree_create(prc->numhcblks,
  2163. prc->numvcblks))) {
  2164. goto error;
  2165. }
  2166. if (!(prc->cblks = jas_alloc2(prc->numcblks, sizeof(jpc_enc_cblk_t)))) {
  2167. goto error;
  2168. }
  2169. for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
  2170. ++cblkno, ++cblk) {
  2171. cblk->passes = 0;
  2172. cblk->stream = 0;
  2173. cblk->mqenc = 0;
  2174. cblk->data = 0;
  2175. cblk->flags = 0;
  2176. cblk->prc = prc;
  2177. }
  2178. for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
  2179. ++cblkno, ++cblk) {
  2180. if (!cblk_create(cblk, cp, prc)) {
  2181. goto error;
  2182. }
  2183. }
  2184. } else {
  2185. /* The precinct does not contain any code blocks. */
  2186. prc->tlx = prc->brx;
  2187. prc->tly = prc->bry;
  2188. prc->numcblks = 0;
  2189. prc->numhcblks = 0;
  2190. prc->numvcblks = 0;
  2191. prc->cblks = 0;
  2192. prc->incltree = 0;
  2193. prc->nlibtree = 0;
  2194. prc->savincltree = 0;
  2195. prc->savnlibtree = 0;
  2196. }
  2197. return prc;
  2198. error:
  2199. prc_destroy(prc);
  2200. return 0;
  2201. }
  2202. static void prc_destroy(jpc_enc_prc_t *prc)
  2203. {
  2204. jpc_enc_cblk_t *cblk;
  2205. uint_fast32_t cblkno;
  2206. if (prc->cblks) {
  2207. for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
  2208. ++cblkno, ++cblk) {
  2209. cblk_destroy(cblk);
  2210. }
  2211. jas_free(prc->cblks);
  2212. }
  2213. if (prc->incltree) {
  2214. jpc_tagtree_destroy(prc->incltree);
  2215. }
  2216. if (prc->nlibtree) {
  2217. jpc_tagtree_destroy(prc->nlibtree);
  2218. }
  2219. if (prc->savincltree) {
  2220. jpc_tagtree_destroy(prc->savincltree);
  2221. }
  2222. if (prc->savnlibtree) {
  2223. jpc_tagtree_destroy(prc->savnlibtree);
  2224. }
  2225. }
  2226. static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, jpc_enc_prc_t *prc)
  2227. {
  2228. jpc_enc_band_t *band;
  2229. uint_fast32_t cblktlx;
  2230. uint_fast32_t cblktly;
  2231. uint_fast32_t cblkbrx;
  2232. uint_fast32_t cblkbry;
  2233. jpc_enc_rlvl_t *rlvl;
  2234. uint_fast32_t cblkxind;
  2235. uint_fast32_t cblkyind;
  2236. uint_fast32_t cblkno;
  2237. uint_fast32_t tlcblktlx;
  2238. uint_fast32_t tlcblktly;
  2239. cblkno = cblk - prc->cblks;
  2240. cblkxind = cblkno % prc->numhcblks;
  2241. cblkyind = cblkno / prc->numhcblks;
  2242. rlvl = prc->band->rlvl;
  2243. cblk->prc = prc;
  2244. cblk->numpasses = 0;
  2245. cblk->passes = 0;
  2246. cblk->numencpasses = 0;
  2247. cblk->numimsbs = 0;
  2248. cblk->numlenbits = 0;
  2249. cblk->stream = 0;
  2250. cblk->mqenc = 0;
  2251. cblk->flags = 0;
  2252. cblk->numbps = 0;
  2253. cblk->curpass = 0;
  2254. cblk->data = 0;
  2255. cblk->savedcurpass = 0;
  2256. cblk->savednumlenbits = 0;
  2257. cblk->savednumencpasses = 0;
  2258. band = prc->band;
  2259. tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn);
  2260. tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn);
  2261. cblktlx = JAS_MAX(tlcblktlx + (cblkxind << rlvl->cblkwidthexpn), prc->tlx);
  2262. cblktly = JAS_MAX(tlcblktly + (cblkyind << rlvl->cblkheightexpn), prc->tly);
  2263. cblkbrx = JAS_MIN(tlcblktlx + ((cblkxind + 1) << rlvl->cblkwidthexpn),
  2264. prc->brx);
  2265. cblkbry = JAS_MIN(tlcblktly + ((cblkyind + 1) << rlvl->cblkheightexpn),
  2266. prc->bry);
  2267. assert(cblktlx < cblkbrx && cblktly < cblkbry);
  2268. if (!(cblk->data = jas_seq2d_create(0, 0, 0, 0))) {
  2269. goto error;
  2270. }
  2271. jas_seq2d_bindsub(cblk->data, band->data, cblktlx, cblktly, cblkbrx, cblkbry);
  2272. return cblk;
  2273. error:
  2274. cblk_destroy(cblk);
  2275. return 0;
  2276. }
  2277. static void cblk_destroy(jpc_enc_cblk_t *cblk)
  2278. {
  2279. uint_fast16_t passno;
  2280. jpc_enc_pass_t *pass;
  2281. if (cblk->passes) {
  2282. for (passno = 0, pass = cblk->passes; passno < cblk->numpasses;
  2283. ++passno, ++pass) {
  2284. pass_destroy(pass);
  2285. }
  2286. jas_free(cblk->passes);
  2287. }
  2288. if (cblk->stream) {
  2289. jas_stream_close(cblk->stream);
  2290. }
  2291. if (cblk->mqenc) {
  2292. jpc_mqenc_destroy(cblk->mqenc);
  2293. }
  2294. if (cblk->data) {
  2295. jas_seq2d_destroy(cblk->data);
  2296. }
  2297. if (cblk->flags) {
  2298. jas_seq2d_destroy(cblk->flags);
  2299. }
  2300. }
  2301. static void pass_destroy(jpc_enc_pass_t *pass)
  2302. {
  2303. /* XXX - need to free resources here */
  2304. }
  2305. void jpc_enc_dump(jpc_enc_t *enc)
  2306. {
  2307. jpc_enc_tile_t *tile;
  2308. jpc_enc_tcmpt_t *tcmpt;
  2309. jpc_enc_rlvl_t *rlvl;
  2310. jpc_enc_band_t *band;
  2311. jpc_enc_prc_t *prc;
  2312. jpc_enc_cblk_t *cblk;
  2313. uint_fast16_t cmptno;
  2314. uint_fast16_t rlvlno;
  2315. uint_fast16_t bandno;
  2316. uint_fast32_t prcno;
  2317. uint_fast32_t cblkno;
  2318. tile = enc->curtile;
  2319. for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < tile->numtcmpts; ++cmptno,
  2320. ++tcmpt) {
  2321. jas_eprintf(" tcmpt %5d %5d %5d %5d\n", jas_seq2d_xstart(tcmpt->data), jas_seq2d_ystart(tcmpt->data), jas_seq2d_xend(tcmpt->data), jas_seq2d_yend(tcmpt->data));
  2322. for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
  2323. ++rlvlno, ++rlvl) {
  2324. jas_eprintf(" rlvl %5d %5d %5d %5d\n", rlvl->tlx, rlvl->tly, rlvl->brx, rlvl->bry);
  2325. for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
  2326. ++bandno, ++band) {
  2327. if (!band->data) {
  2328. continue;
  2329. }
  2330. jas_eprintf(" band %5d %5d %5d %5d\n", jas_seq2d_xstart(band->data), jas_seq2d_ystart(band->data), jas_seq2d_xend(band->data), jas_seq2d_yend(band->data));
  2331. for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs;
  2332. ++prcno, ++prc) {
  2333. jas_eprintf(" prc %5d %5d %5d %5d (%5d %5d)\n", prc->tlx, prc->tly, prc->brx, prc->bry, prc->brx - prc->tlx, prc->bry - prc->tly);
  2334. if (!prc->cblks) {
  2335. continue;
  2336. }
  2337. for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
  2338. ++cblkno, ++cblk) {
  2339. jas_eprintf(" cblk %5d %5d %5d %5d\n", jas_seq2d_xstart(cblk->data), jas_seq2d_ystart(cblk->data), jas_seq2d_xend(cblk->data), jas_seq2d_yend(cblk->data));
  2340. }
  2341. }
  2342. }
  2343. }
  2344. }
  2345. }