jpc_enc.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * Copyright (c) 1999-2000 Image Power, Inc. and the University of
  3. * British Columbia.
  4. * Copyright (c) 2001-2002 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.h,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  64. */
  65. #ifndef JPC_ENC_H
  66. #define JPC_ENC_H
  67. /******************************************************************************\
  68. * Includes.
  69. \******************************************************************************/
  70. #include "jasper/jas_seq.h"
  71. #include "jpc_t2cod.h"
  72. #include "jpc_mqenc.h"
  73. #include "jpc_cod.h"
  74. #include "jpc_tagtree.h"
  75. #include "jpc_cs.h"
  76. #include "jpc_flt.h"
  77. #include "jpc_tsfb.h"
  78. /******************************************************************************\
  79. * Constants.
  80. \******************************************************************************/
  81. /* The number of bits used in various lookup tables. */
  82. #define JPC_NUMEXTRABITS JPC_NMSEDEC_FRACBITS
  83. /* An invalid R-D slope value. */
  84. #define JPC_BADRDSLOPE (-1)
  85. /******************************************************************************\
  86. * Coding parameters types.
  87. \******************************************************************************/
  88. /* Per-component coding paramters. */
  89. typedef struct {
  90. /* The horizontal sampling period. */
  91. uint_fast8_t sampgrdstepx;
  92. /* The vertical sampling period. */
  93. uint_fast8_t sampgrdstepy;
  94. /* The sample alignment horizontal offset. */
  95. uint_fast8_t sampgrdsubstepx;
  96. /* The sample alignment vertical offset. */
  97. uint_fast8_t sampgrdsubstepy;
  98. /* The precision of the samples. */
  99. uint_fast8_t prec;
  100. /* The signedness of the samples. */
  101. bool sgnd;
  102. /* The number of step sizes. */
  103. uint_fast16_t numstepsizes;
  104. /* The quantizer step sizes. */
  105. uint_fast16_t stepsizes[JPC_MAXBANDS];
  106. } jpc_enc_ccp_t;
  107. /* Per-tile coding parameters. */
  108. typedef struct {
  109. /* The coding mode. */
  110. bool intmode;
  111. /* The coding style (i.e., SOP, EPH). */
  112. uint_fast8_t csty;
  113. /* The progression order. */
  114. uint_fast8_t prg;
  115. /* The multicomponent transform. */
  116. uint_fast8_t mctid;
  117. /* The number of layers. */
  118. uint_fast16_t numlyrs;
  119. /* The normalized bit rates associated with the various
  120. intermediate layers. */
  121. jpc_fix_t *ilyrrates;
  122. } jpc_enc_tcp_t;
  123. /* Per tile-component coding parameters. */
  124. typedef struct {
  125. /* The coding style (i.e., explicit precinct sizes). */
  126. uint_fast8_t csty;
  127. /* The maximum number of resolution levels allowed. */
  128. uint_fast8_t maxrlvls;
  129. /* The exponent for the nominal code block width. */
  130. uint_fast16_t cblkwidthexpn;
  131. /* The exponent for the nominal code block height. */
  132. uint_fast16_t cblkheightexpn;
  133. /* The code block style parameters (e.g., lazy, terminate all,
  134. segmentation symbols, causal, reset probability models). */
  135. uint_fast8_t cblksty;
  136. /* The QMFB. */
  137. uint_fast8_t qmfbid;
  138. /* The precinct width values. */
  139. uint_fast16_t prcwidthexpns[JPC_MAXRLVLS];
  140. /* The precinct height values. */
  141. uint_fast16_t prcheightexpns[JPC_MAXRLVLS];
  142. /* The number of guard bits. */
  143. uint_fast8_t numgbits;
  144. } jpc_enc_tccp_t;
  145. /* Coding parameters. */
  146. typedef struct {
  147. /* The debug level. */
  148. int debug;
  149. /* The horizontal offset from the origin of the reference grid to the
  150. left edge of the image area. */
  151. uint_fast32_t imgareatlx;
  152. /* The vertical offset from the origin of the reference grid to the
  153. top edge of the image area. */
  154. uint_fast32_t imgareatly;
  155. /* The horizontal offset from the origin of the reference grid to the
  156. right edge of the image area (plus one). */
  157. uint_fast32_t refgrdwidth;
  158. /* The vertical offset from the origin of the reference grid to the
  159. bottom edge of the image area (plus one). */
  160. uint_fast32_t refgrdheight;
  161. /* The horizontal offset from the origin of the tile grid to the
  162. origin of the reference grid. */
  163. uint_fast32_t tilegrdoffx;
  164. /* The vertical offset from the origin of the tile grid to the
  165. origin of the reference grid. */
  166. uint_fast32_t tilegrdoffy;
  167. /* The nominal tile width in units of the image reference grid. */
  168. uint_fast32_t tilewidth;
  169. /* The nominal tile height in units of the image reference grid. */
  170. uint_fast32_t tileheight;
  171. /* The number of tiles spanning the image area in the horizontal
  172. direction. */
  173. uint_fast32_t numhtiles;
  174. /* The number of tiles spanning the image area in the vertical
  175. direction. */
  176. uint_fast32_t numvtiles;
  177. /* The number of tiles. */
  178. uint_fast32_t numtiles;
  179. /* The number of components. */
  180. uint_fast16_t numcmpts;
  181. /* The per-component coding parameters. */
  182. jpc_enc_ccp_t *ccps;
  183. /* The per-tile coding parameters. */
  184. jpc_enc_tcp_t tcp;
  185. /* The per-tile-component coding parameters. */
  186. jpc_enc_tccp_t tccp;
  187. /* The target code stream length in bytes. */
  188. uint_fast32_t totalsize;
  189. /* The raw (i.e., uncompressed) size of the image in bytes. */
  190. uint_fast32_t rawsize;
  191. } jpc_enc_cp_t;
  192. /******************************************************************************\
  193. * Encoder class.
  194. \******************************************************************************/
  195. /* Encoder per-coding-pass state information. */
  196. typedef struct {
  197. /* The starting offset for this pass. */
  198. int start;
  199. /* The ending offset for this pass. */
  200. int end;
  201. /* The type of data in this pass (i.e., MQ or raw). */
  202. int type;
  203. /* Flag indicating that this pass is terminated. */
  204. int term;
  205. /* The entropy coder state after coding this pass. */
  206. jpc_mqencstate_t mqencstate;
  207. /* The layer to which this pass has been assigned. */
  208. int lyrno;
  209. /* The R-D slope for this pass. */
  210. jpc_flt_t rdslope;
  211. /* The weighted MSE reduction associated with this pass. */
  212. jpc_flt_t wmsedec;
  213. /* The cumulative weighted MSE reduction. */
  214. jpc_flt_t cumwmsedec;
  215. /* The normalized MSE reduction. */
  216. long nmsedec;
  217. } jpc_enc_pass_t;
  218. /* Encoder per-code-block state information. */
  219. typedef struct {
  220. /* The number of passes. */
  221. int numpasses;
  222. /* The per-pass information. */
  223. jpc_enc_pass_t *passes;
  224. /* The number of passes encoded so far. */
  225. int numencpasses;
  226. /* The number of insignificant MSBs. */
  227. int numimsbs;
  228. /* The number of bits used to encode pass data lengths. */
  229. int numlenbits;
  230. /* The byte stream for this code block. */
  231. jas_stream_t *stream;
  232. /* The entropy encoder. */
  233. jpc_mqenc_t *mqenc;
  234. /* The data for this code block. */
  235. jas_matrix_t *data;
  236. /* The state for this code block. */
  237. jas_matrix_t *flags;
  238. /* The number of bit planes required for this code block. */
  239. int numbps;
  240. /* The next pass to be encoded. */
  241. jpc_enc_pass_t *curpass;
  242. /* The per-code-block-group state information. */
  243. struct jpc_enc_prc_s *prc;
  244. /* The saved current pass. */
  245. /* This is used by the rate control code. */
  246. jpc_enc_pass_t *savedcurpass;
  247. /* The saved length indicator size. */
  248. /* This is used by the rate control code. */
  249. int savednumlenbits;
  250. /* The saved number of encoded passes. */
  251. /* This is used by the rate control code. */
  252. int savednumencpasses;
  253. } jpc_enc_cblk_t;
  254. /* Encoder per-code-block-group state information. */
  255. typedef struct jpc_enc_prc_s {
  256. /* The x-coordinate of the top-left corner of the precinct. */
  257. uint_fast32_t tlx;
  258. /* The y-coordinate of the top-left corner of the precinct. */
  259. uint_fast32_t tly;
  260. /* The x-coordinate of the bottom-right corner of the precinct
  261. (plus one). */
  262. uint_fast32_t brx;
  263. /* The y-coordinate of the bottom-right corner of the precinct
  264. (plus one). */
  265. uint_fast32_t bry;
  266. /* The number of code blocks spanning the precinct in the horizontal
  267. direction. */
  268. int numhcblks;
  269. /* The number of code blocks spanning the precinct in the vertical
  270. direction. */
  271. int numvcblks;
  272. /* The total number of code blocks. */
  273. int numcblks;
  274. /* The per-code-block information. */
  275. jpc_enc_cblk_t *cblks;
  276. /* The inclusion tag tree. */
  277. jpc_tagtree_t *incltree;
  278. /* The insignifcant MSBs tag tree. */
  279. jpc_tagtree_t *nlibtree;
  280. /* The per-band information. */
  281. struct jpc_enc_band_s *band;
  282. /* The saved inclusion tag tree. */
  283. /* This is used by rate control. */
  284. jpc_tagtree_t *savincltree;
  285. /* The saved leading-insignificant-bit-planes tag tree. */
  286. /* This is used by rate control. */
  287. jpc_tagtree_t *savnlibtree;
  288. } jpc_enc_prc_t;
  289. /* Encoder per-band state information. */
  290. typedef struct jpc_enc_band_s {
  291. /* The per precinct information. */
  292. jpc_enc_prc_t *prcs;
  293. /* The coefficient data for this band. */
  294. jas_matrix_t *data;
  295. /* The orientation of this band (i.e., LL, LH, HL, or HH). */
  296. int orient;
  297. /* The number of bit planes associated with this band. */
  298. int numbps;
  299. /* The quantizer step size. */
  300. jpc_fix_t absstepsize;
  301. /* The encoded quantizer step size. */
  302. int stepsize;
  303. /* The L2 norm of the synthesis basis functions associated with
  304. this band. (The MCT is not considered in this value.) */
  305. jpc_fix_t synweight;
  306. /* The analysis gain for this band. */
  307. int analgain;
  308. /* The per-resolution-level information. */
  309. struct jpc_enc_rlvl_s *rlvl;
  310. } jpc_enc_band_t;
  311. /* Encoder per-resolution-level state information. */
  312. typedef struct jpc_enc_rlvl_s {
  313. /* The x-coordinate of the top-left corner of the tile-component
  314. at this resolution. */
  315. uint_fast32_t tlx;
  316. /* The y-coordinate of the top-left corner of the tile-component
  317. at this resolution. */
  318. uint_fast32_t tly;
  319. /* The x-coordinate of the bottom-right corner of the tile-component
  320. at this resolution (plus one). */
  321. uint_fast32_t brx;
  322. /* The y-coordinate of the bottom-right corner of the tile-component
  323. at this resolution (plus one). */
  324. uint_fast32_t bry;
  325. /* The exponent value for the nominal precinct width measured
  326. relative to the associated LL band. */
  327. int prcwidthexpn;
  328. /* The exponent value for the nominal precinct height measured
  329. relative to the associated LL band. */
  330. int prcheightexpn;
  331. /* The number of precincts spanning the resolution level in the
  332. horizontal direction. */
  333. int numhprcs;
  334. /* The number of precincts spanning the resolution level in the
  335. vertical direction. */
  336. int numvprcs;
  337. /* The total number of precincts. */
  338. int numprcs;
  339. /* The exponent value for the nominal code block group width.
  340. This quantity is associated with the next lower resolution level
  341. (assuming that there is one). */
  342. int cbgwidthexpn;
  343. /* The exponent value for the nominal code block group height.
  344. This quantity is associated with the next lower resolution level
  345. (assuming that there is one). */
  346. int cbgheightexpn;
  347. /* The exponent value for the code block width. */
  348. uint_fast16_t cblkwidthexpn;
  349. /* The exponent value for the code block height. */
  350. uint_fast16_t cblkheightexpn;
  351. /* The number of bands associated with this resolution level. */
  352. int numbands;
  353. /* The per-band information. */
  354. jpc_enc_band_t *bands;
  355. /* The parent tile-component. */
  356. struct jpc_enc_tcmpt_s *tcmpt;
  357. } jpc_enc_rlvl_t;
  358. /* Encoder per-tile-component state information. */
  359. typedef struct jpc_enc_tcmpt_s {
  360. /* The number of resolution levels. */
  361. int numrlvls;
  362. /* The per-resolution-level information. */
  363. jpc_enc_rlvl_t *rlvls;
  364. /* The tile-component data. */
  365. jas_matrix_t *data;
  366. /* The QMFB. */
  367. int qmfbid;
  368. /* The number of bands. */
  369. int numbands;
  370. /* The TSFB. */
  371. jpc_tsfb_t *tsfb;
  372. /* The synthesis energy weight (for the MCT). */
  373. jpc_fix_t synweight;
  374. /* The precinct width exponents. */
  375. int prcwidthexpns[JPC_MAXRLVLS];
  376. /* The precinct height exponents. */
  377. int prcheightexpns[JPC_MAXRLVLS];
  378. /* The code block width exponent. */
  379. int cblkwidthexpn;
  380. /* The code block height exponent. */
  381. int cblkheightexpn;
  382. /* Coding style (i.e., explicit precinct sizes). */
  383. int csty;
  384. /* Code block style. */
  385. int cblksty;
  386. /* The number of quantizer step sizes. */
  387. int numstepsizes;
  388. /* The encoded quantizer step sizes. */
  389. uint_fast16_t stepsizes[JPC_MAXBANDS];
  390. /* The parent tile. */
  391. struct jpc_enc_tile_s *tile;
  392. } jpc_enc_tcmpt_t;
  393. /* Encoder per-tile state information. */
  394. typedef struct jpc_enc_tile_s {
  395. /* The tile number. */
  396. uint_fast32_t tileno;
  397. /* The x-coordinate of the top-left corner of the tile measured with
  398. respect to the reference grid. */
  399. uint_fast32_t tlx;
  400. /* The y-coordinate of the top-left corner of the tile measured with
  401. respect to the reference grid. */
  402. uint_fast32_t tly;
  403. /* The x-coordinate of the bottom-right corner of the tile measured
  404. with respect to the reference grid (plus one). */
  405. uint_fast32_t brx;
  406. /* The y-coordinate of the bottom-right corner of the tile measured
  407. with respect to the reference grid (plus one). */
  408. uint_fast32_t bry;
  409. /* The coding style. */
  410. uint_fast8_t csty;
  411. /* The progression order. */
  412. uint_fast8_t prg;
  413. /* The number of layers. */
  414. int numlyrs;
  415. /* The MCT to employ (if any). */
  416. uint_fast8_t mctid;
  417. /* The packet iterator (used to determine the order of packet
  418. generation). */
  419. jpc_pi_t *pi;
  420. /* The coding mode (i.e., integer or real). */
  421. bool intmode;
  422. /* The number of bytes to allocate to the various layers. */
  423. uint_fast32_t *lyrsizes;
  424. /* The number of tile-components. */
  425. int numtcmpts;
  426. /* The per tile-component information. */
  427. jpc_enc_tcmpt_t *tcmpts;
  428. /* The raw (i.e., uncompressed) size of this tile. */
  429. uint_fast32_t rawsize;
  430. } jpc_enc_tile_t;
  431. /* Encoder class. */
  432. typedef struct jpc_enc_s {
  433. /* The image being encoded. */
  434. jas_image_t *image;
  435. /* The output stream. */
  436. jas_stream_t *out;
  437. /* The coding parameters. */
  438. jpc_enc_cp_t *cp;
  439. /* The tile currently being processed. */
  440. jpc_enc_tile_t *curtile;
  441. /* The code stream state. */
  442. jpc_cstate_t *cstate;
  443. /* The number of bytes output so far. */
  444. uint_fast32_t len;
  445. /* The number of bytes available for the main body of the code stream. */
  446. /* This is used for rate allocation purposes. */
  447. uint_fast32_t mainbodysize;
  448. /* The marker segment currently being processed. */
  449. /* This member is a convenience for making cleanup easier. */
  450. jpc_ms_t *mrk;
  451. /* The stream used to temporarily hold tile-part data. */
  452. jas_stream_t *tmpstream;
  453. } jpc_enc_t;
  454. #endif