t2.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  3. * Copyright (c) 2002-2007, Professor Benoit Macq
  4. * Copyright (c) 2001-2003, David Janssens
  5. * Copyright (c) 2002-2003, Yannick Verschueren
  6. * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "opj_includes.h"
  32. #include <assert.h>
  33. /** @defgroup T2 T2 - Implementation of a tier-2 coding */
  34. /*@{*/
  35. /** @name Local static functions */
  36. /*@{*/
  37. static void t2_putcommacode(opj_bio_t *bio, int n);
  38. static int t2_getcommacode(opj_bio_t *bio);
  39. /**
  40. Variable length code for signalling delta Zil (truncation point)
  41. @param bio Bit Input/Output component
  42. @param n delta Zil
  43. */
  44. static void t2_putnumpasses(opj_bio_t *bio, int n);
  45. static int t2_getnumpasses(opj_bio_t *bio);
  46. /**
  47. Encode a packet of a tile to a destination buffer
  48. @param tile Tile for which to write the packets
  49. @param tcp Tile coding parameters
  50. @param pi Packet identity
  51. @param dest Destination buffer
  52. @param len Length of the destination buffer
  53. @param cstr_info Codestream information structure
  54. @param tileno Number of the tile encoded
  55. @return
  56. */
  57. static int t2_encode_packet(opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_codestream_info_t *cstr_info, int tileno);
  58. /**
  59. @param cblk
  60. @param index
  61. @param cblksty
  62. @param first
  63. */
  64. static opj_bool t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first);
  65. /**
  66. Decode a packet of a tile from a source buffer
  67. @param t2 T2 handle
  68. @param src Source buffer
  69. @param len Length of the source buffer
  70. @param tile Tile for which to write the packets
  71. @param tcp Tile coding parameters
  72. @param pi Packet identity
  73. @param pack_info Packet information
  74. @return
  75. */
  76. static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile,
  77. opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info);
  78. /*@}*/
  79. /*@}*/
  80. /* ----------------------------------------------------------------------- */
  81. /* #define RESTART 0x04 */
  82. static void t2_putcommacode(opj_bio_t *bio, int n) {
  83. while (--n >= 0) {
  84. bio_write(bio, 1, 1);
  85. }
  86. bio_write(bio, 0, 1);
  87. }
  88. static int t2_getcommacode(opj_bio_t *bio) {
  89. int n;
  90. for (n = 0; bio_read(bio, 1); n++) {
  91. ;
  92. }
  93. return n;
  94. }
  95. static void t2_putnumpasses(opj_bio_t *bio, int n) {
  96. if (n == 1) {
  97. bio_write(bio, 0, 1);
  98. } else if (n == 2) {
  99. bio_write(bio, 2, 2);
  100. } else if (n <= 5) {
  101. bio_write(bio, 0xc | (n - 3), 4);
  102. } else if (n <= 36) {
  103. bio_write(bio, 0x1e0 | (n - 6), 9);
  104. } else if (n <= 164) {
  105. bio_write(bio, 0xff80 | (n - 37), 16);
  106. }
  107. }
  108. static int t2_getnumpasses(opj_bio_t *bio) {
  109. int n;
  110. if (!bio_read(bio, 1))
  111. return 1;
  112. if (!bio_read(bio, 1))
  113. return 2;
  114. if ((n = bio_read(bio, 2)) != 3)
  115. return (3 + n);
  116. if ((n = bio_read(bio, 5)) != 31)
  117. return (6 + n);
  118. return (37 + bio_read(bio, 7));
  119. }
  120. static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int length, opj_codestream_info_t *cstr_info, int tileno) {
  121. int bandno, cblkno;
  122. unsigned char *c = dest;
  123. int compno = pi->compno; /* component value */
  124. int resno = pi->resno; /* resolution level value */
  125. int precno = pi->precno; /* precinct value */
  126. int layno = pi->layno; /* quality layer value */
  127. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  128. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  129. opj_bio_t *bio = NULL; /* BIO component */
  130. /* <SOP 0xff91> */
  131. if (tcp->csty & J2K_CP_CSTY_SOP) {
  132. c[0] = 255;
  133. c[1] = 145;
  134. c[2] = 0;
  135. c[3] = 4;
  136. c[4] = (unsigned char)((tile->packno % 65536) / 256);
  137. c[5] = (unsigned char)((tile->packno % 65536) % 256);
  138. c += 6;
  139. }
  140. /* </SOP> */
  141. if (!layno) {
  142. for (bandno = 0; bandno < res->numbands; bandno++) {
  143. opj_tcd_band_t *band = &res->bands[bandno];
  144. opj_tcd_precinct_t *prc = &band->precincts[precno];
  145. tgt_reset(prc->incltree);
  146. tgt_reset(prc->imsbtree);
  147. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  148. opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
  149. cblk->numpasses = 0;
  150. tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
  151. }
  152. }
  153. }
  154. bio = bio_create();
  155. bio_init_enc(bio, c, length);
  156. bio_write(bio, 1, 1); /* Empty header bit */
  157. /* Writing Packet header */
  158. for (bandno = 0; bandno < res->numbands; bandno++) {
  159. opj_tcd_band_t *band = &res->bands[bandno];
  160. opj_tcd_precinct_t *prc = &band->precincts[precno];
  161. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  162. opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
  163. opj_tcd_layer_t *layer = &cblk->layers[layno];
  164. if (!cblk->numpasses && layer->numpasses) {
  165. tgt_setvalue(prc->incltree, cblkno, layno);
  166. }
  167. }
  168. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  169. opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
  170. opj_tcd_layer_t *layer = &cblk->layers[layno];
  171. int increment = 0;
  172. int nump = 0;
  173. int len = 0, passno;
  174. /* cblk inclusion bits */
  175. if (!cblk->numpasses) {
  176. tgt_encode(bio, prc->incltree, cblkno, layno + 1);
  177. } else {
  178. bio_write(bio, layer->numpasses != 0, 1);
  179. }
  180. /* if cblk not included, go to the next cblk */
  181. if (!layer->numpasses) {
  182. continue;
  183. }
  184. /* if first instance of cblk --> zero bit-planes information */
  185. if (!cblk->numpasses) {
  186. cblk->numlenbits = 3;
  187. tgt_encode(bio, prc->imsbtree, cblkno, 999);
  188. }
  189. /* number of coding passes included */
  190. t2_putnumpasses(bio, layer->numpasses);
  191. /* computation of the increase of the length indicator and insertion in the header */
  192. for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
  193. opj_tcd_pass_t *pass = &cblk->passes[passno];
  194. nump++;
  195. len += pass->len;
  196. if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
  197. increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
  198. len = 0;
  199. nump = 0;
  200. }
  201. }
  202. t2_putcommacode(bio, increment);
  203. /* computation of the new Length indicator */
  204. cblk->numlenbits += increment;
  205. /* insertion of the codeword segment length */
  206. for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
  207. opj_tcd_pass_t *pass = &cblk->passes[passno];
  208. nump++;
  209. len += pass->len;
  210. if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
  211. bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
  212. len = 0;
  213. nump = 0;
  214. }
  215. }
  216. }
  217. }
  218. if (bio_flush(bio)) {
  219. bio_destroy(bio);
  220. return -999; /* modified to eliminate longjmp !! */
  221. }
  222. c += bio_numbytes(bio);
  223. bio_destroy(bio);
  224. /* <EPH 0xff92> */
  225. if (tcp->csty & J2K_CP_CSTY_EPH) {
  226. c[0] = 255;
  227. c[1] = 146;
  228. c += 2;
  229. }
  230. /* </EPH> */
  231. /* << INDEX */
  232. /* End of packet header position. Currently only represents the distance to start of packet
  233. // Will be updated later by incrementing with packet start value */
  234. if(cstr_info && cstr_info->index_write) {
  235. opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
  236. info_PK->end_ph_pos = (int)(c - dest);
  237. }
  238. /* INDEX >> */
  239. /* Writing the packet body */
  240. for (bandno = 0; bandno < res->numbands; bandno++) {
  241. opj_tcd_band_t *band = &res->bands[bandno];
  242. opj_tcd_precinct_t *prc = &band->precincts[precno];
  243. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  244. opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
  245. opj_tcd_layer_t *layer = &cblk->layers[layno];
  246. if (!layer->numpasses) {
  247. continue;
  248. }
  249. if (c + layer->len > dest + length) {
  250. return -999;
  251. }
  252. memcpy(c, layer->data, layer->len);
  253. cblk->numpasses += layer->numpasses;
  254. c += layer->len;
  255. /* << INDEX */
  256. if(cstr_info && cstr_info->index_write) {
  257. opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
  258. info_PK->disto += layer->disto;
  259. if (cstr_info->D_max < info_PK->disto) {
  260. cstr_info->D_max = info_PK->disto;
  261. }
  262. }
  263. /* INDEX >> */
  264. }
  265. }
  266. return (c - dest);
  267. }
  268. static opj_bool t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first) {
  269. opj_tcd_seg_t* seg;
  270. opj_tcd_seg_t* segs;
  271. segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, (index + 1) * sizeof(opj_tcd_seg_t));
  272. if (segs == NULL)
  273. {
  274. return OPJ_FALSE;
  275. }
  276. cblk->segs = segs;
  277. seg = &cblk->segs[index];
  278. seg->data = NULL;
  279. seg->dataindex = 0;
  280. seg->numpasses = 0;
  281. seg->len = 0;
  282. if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
  283. seg->maxpasses = 1;
  284. }
  285. else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
  286. if (first) {
  287. seg->maxpasses = 10;
  288. } else {
  289. seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
  290. }
  291. } else {
  292. seg->maxpasses = 109;
  293. }
  294. return OPJ_TRUE;
  295. }
  296. static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile,
  297. opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info) {
  298. int bandno, cblkno;
  299. unsigned char *c = src;
  300. opj_cp_t *cp = t2->cp;
  301. int compno = pi->compno; /* component value */
  302. int resno = pi->resno; /* resolution level value */
  303. int precno = pi->precno; /* precinct value */
  304. int layno = pi->layno; /* quality layer value */
  305. unsigned char *hd = NULL;
  306. int present;
  307. opj_bio_t *bio = NULL; /* BIO component */
  308. opj_tcd_resolution_t* res;
  309. assert(&tile->comps[compno] != NULL);
  310. res = &tile->comps[compno].resolutions[resno];
  311. if (layno == 0) {
  312. for (bandno = 0; bandno < res->numbands; bandno++) {
  313. opj_tcd_band_t *band = &res->bands[bandno];
  314. opj_tcd_precinct_t *prc = &band->precincts[precno];
  315. if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
  316. tgt_reset(prc->incltree);
  317. tgt_reset(prc->imsbtree);
  318. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  319. opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
  320. cblk->numsegs = 0;
  321. }
  322. }
  323. }
  324. /* SOP markers */
  325. if (tcp->csty & J2K_CP_CSTY_SOP) {
  326. if ((*c) != 0xff || (*(c + 1) != 0x91)) {
  327. opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
  328. } else {
  329. c += 6;
  330. }
  331. /** TODO : check the Nsop value */
  332. }
  333. /*
  334. When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
  335. This part deal with this caracteristic
  336. step 1: Read packet header in the saved structure
  337. step 2: Return to codestream for decoding
  338. */
  339. bio = bio_create();
  340. if (cp->ppm == 1) { /* PPM */
  341. hd = cp->ppm_data;
  342. bio_init_dec(bio, hd, cp->ppm_len);
  343. } else if (tcp->ppt == 1) { /* PPT */
  344. hd = tcp->ppt_data;
  345. bio_init_dec(bio, hd, tcp->ppt_len);
  346. } else { /* Normal Case */
  347. hd = c;
  348. bio_init_dec(bio, hd, src+len-hd);
  349. }
  350. present = bio_read(bio, 1);
  351. if (!present) {
  352. bio_inalign(bio);
  353. hd += bio_numbytes(bio);
  354. bio_destroy(bio);
  355. /* EPH markers */
  356. if (tcp->csty & J2K_CP_CSTY_EPH) {
  357. if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
  358. printf("Error : expected EPH marker\n");
  359. } else {
  360. hd += 2;
  361. }
  362. }
  363. /* << INDEX */
  364. /* End of packet header position. Currently only represents the distance to start of packet
  365. // Will be updated later by incrementing with packet start value*/
  366. if(pack_info) {
  367. pack_info->end_ph_pos = (int)(c - src);
  368. }
  369. /* INDEX >> */
  370. if (cp->ppm == 1) { /* PPM case */
  371. cp->ppm_len += cp->ppm_data-hd;
  372. cp->ppm_data = hd;
  373. return (c - src);
  374. }
  375. if (tcp->ppt == 1) { /* PPT case */
  376. tcp->ppt_len+=tcp->ppt_data-hd;
  377. tcp->ppt_data = hd;
  378. return (c - src);
  379. }
  380. return (hd - src);
  381. }
  382. for (bandno = 0; bandno < res->numbands; bandno++) {
  383. opj_tcd_band_t *band = &res->bands[bandno];
  384. opj_tcd_precinct_t *prc = &band->precincts[precno];
  385. if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
  386. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  387. int included, increment, n, segno;
  388. opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
  389. /* if cblk not yet included before --> inclusion tagtree */
  390. if (!cblk->numsegs) {
  391. included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
  392. /* else one bit */
  393. } else {
  394. included = bio_read(bio, 1);
  395. }
  396. /* if cblk not included */
  397. if (!included) {
  398. cblk->numnewpasses = 0;
  399. continue;
  400. }
  401. /* if cblk not yet included --> zero-bitplane tagtree */
  402. if (!cblk->numsegs) {
  403. int i, numimsbs;
  404. for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
  405. ;
  406. }
  407. numimsbs = i - 1;
  408. cblk->numbps = band->numbps - numimsbs;
  409. cblk->numlenbits = 3;
  410. }
  411. /* number of coding passes */
  412. cblk->numnewpasses = t2_getnumpasses(bio);
  413. increment = t2_getcommacode(bio);
  414. /* length indicator increment */
  415. cblk->numlenbits += increment;
  416. segno = 0;
  417. if (!cblk->numsegs) {
  418. if (!t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 1))
  419. {
  420. opj_event_msg(t2->cinfo, EVT_ERROR, "Out of memory\n");
  421. bio_destroy(bio);
  422. return -999;
  423. }
  424. } else {
  425. segno = cblk->numsegs - 1;
  426. if (cblk->segs[segno].numpasses == cblk->segs[segno].maxpasses) {
  427. ++segno;
  428. if (!t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0))
  429. {
  430. opj_event_msg(t2->cinfo, EVT_ERROR, "Out of memory\n");
  431. bio_destroy(bio);
  432. return -999;
  433. }
  434. }
  435. }
  436. n = cblk->numnewpasses;
  437. do {
  438. cblk->segs[segno].numnewpasses = int_min(cblk->segs[segno].maxpasses - cblk->segs[segno].numpasses, n);
  439. cblk->segs[segno].newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(cblk->segs[segno].numnewpasses));
  440. n -= cblk->segs[segno].numnewpasses;
  441. if (n > 0) {
  442. ++segno;
  443. if (!t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0))
  444. {
  445. opj_event_msg(t2->cinfo, EVT_ERROR, "Out of memory\n");
  446. bio_destroy(bio);
  447. return -999;
  448. }
  449. }
  450. } while (n > 0);
  451. }
  452. }
  453. if (bio_inalign(bio)) {
  454. bio_destroy(bio);
  455. return -999;
  456. }
  457. hd += bio_numbytes(bio);
  458. bio_destroy(bio);
  459. /* EPH markers */
  460. if (tcp->csty & J2K_CP_CSTY_EPH) {
  461. if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
  462. opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
  463. return -999;
  464. } else {
  465. hd += 2;
  466. }
  467. }
  468. /* << INDEX */
  469. /* End of packet header position. Currently only represents the distance to start of packet
  470. // Will be updated later by incrementing with packet start value*/
  471. if(pack_info) {
  472. pack_info->end_ph_pos = (int)(hd - src);
  473. }
  474. /* INDEX >> */
  475. if (cp->ppm==1) {
  476. cp->ppm_len+=cp->ppm_data-hd;
  477. cp->ppm_data = hd;
  478. } else if (tcp->ppt == 1) {
  479. tcp->ppt_len+=tcp->ppt_data-hd;
  480. tcp->ppt_data = hd;
  481. } else {
  482. c=hd;
  483. }
  484. for (bandno = 0; bandno < res->numbands; bandno++) {
  485. opj_tcd_band_t *band = &res->bands[bandno];
  486. opj_tcd_precinct_t *prc = &band->precincts[precno];
  487. if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
  488. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  489. opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
  490. opj_tcd_seg_t *seg = NULL;
  491. if (!cblk->numnewpasses)
  492. continue;
  493. if (!cblk->numsegs) {
  494. seg = &cblk->segs[0];
  495. cblk->numsegs++;
  496. cblk->len = 0;
  497. } else {
  498. seg = &cblk->segs[cblk->numsegs - 1];
  499. if (seg->numpasses == seg->maxpasses) {
  500. seg++;
  501. cblk->numsegs++;
  502. }
  503. }
  504. do {
  505. if (c + seg->newlen > src + len) {
  506. return -999;
  507. }
  508. #ifdef USE_JPWL
  509. /* we need here a j2k handle to verify if making a check to
  510. the validity of cblocks parameters is selected from user (-W) */
  511. /* let's check that we are not exceeding */
  512. if ((cblk->len + seg->newlen) > 8192) {
  513. opj_event_msg(t2->cinfo, EVT_WARNING,
  514. "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  515. seg->newlen, cblkno, precno, bandno, resno, compno);
  516. if (!JPWL_ASSUME) {
  517. opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
  518. return -999;
  519. }
  520. seg->newlen = 8192 - cblk->len;
  521. opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen);
  522. break;
  523. };
  524. #endif /* USE_JPWL */
  525. cblk->data = (unsigned char*) opj_realloc(cblk->data, (cblk->len + seg->newlen) * sizeof(unsigned char));
  526. memcpy(cblk->data + cblk->len, c, seg->newlen);
  527. if (seg->numpasses == 0) {
  528. seg->data = &cblk->data;
  529. seg->dataindex = cblk->len;
  530. }
  531. c += seg->newlen;
  532. cblk->len += seg->newlen;
  533. seg->len += seg->newlen;
  534. seg->numpasses += seg->numnewpasses;
  535. cblk->numnewpasses -= seg->numnewpasses;
  536. if (cblk->numnewpasses > 0) {
  537. seg++;
  538. cblk->numsegs++;
  539. }
  540. } while (cblk->numnewpasses > 0);
  541. }
  542. }
  543. return (c - src);
  544. }
  545. /* ----------------------------------------------------------------------- */
  546. int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_codestream_info_t *cstr_info,int tpnum, int tppos,int pino, J2K_T2_MODE t2_mode, int cur_totnum_tp){
  547. unsigned char *c = dest;
  548. int e = 0;
  549. int compno;
  550. opj_pi_iterator_t *pi = NULL;
  551. int poc;
  552. opj_image_t *image = t2->image;
  553. opj_cp_t *cp = t2->cp;
  554. opj_tcp_t *tcp = &cp->tcps[tileno];
  555. int pocno = cp->cinema == CINEMA4K_24? 2: 1;
  556. int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1;
  557. pi = pi_initialise_encode(image, cp, tileno, t2_mode);
  558. if(!pi) {
  559. /* TODO: throw an error */
  560. return -999;
  561. }
  562. if(t2_mode == THRESH_CALC ){ /* Calculating threshold */
  563. for(compno = 0; compno < maxcomp; compno++ ){
  564. for(poc = 0; poc < pocno ; poc++){
  565. int comp_len = 0;
  566. int tpnum = compno;
  567. if (pi_create_encode(pi, cp,tileno,poc,tpnum,tppos,t2_mode,cur_totnum_tp)) {
  568. opj_event_msg(t2->cinfo, EVT_ERROR, "Error initializing Packet Iterator\n");
  569. pi_destroy(pi, cp, tileno);
  570. return -999;
  571. }
  572. while (pi_next(&pi[poc])) {
  573. if (pi[poc].layno < maxlayers) {
  574. e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno);
  575. comp_len = comp_len + e;
  576. if (e == -999) {
  577. break;
  578. } else {
  579. c += e;
  580. }
  581. }
  582. }
  583. if (e == -999) break;
  584. if (cp->max_comp_size){
  585. if (comp_len > cp->max_comp_size){
  586. e = -999;
  587. break;
  588. }
  589. }
  590. }
  591. if (e == -999) break;
  592. }
  593. }else{ /* t2_mode == FINAL_PASS */
  594. pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode,cur_totnum_tp);
  595. while (pi_next(&pi[pino])) {
  596. if (pi[pino].layno < maxlayers) {
  597. e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno);
  598. if (e == -999) {
  599. break;
  600. } else {
  601. c += e;
  602. }
  603. /* INDEX >> */
  604. if(cstr_info) {
  605. if(cstr_info->index_write) {
  606. opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
  607. opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
  608. if (!cstr_info->packno) {
  609. info_PK->start_pos = info_TL->end_header + 1;
  610. } else {
  611. info_PK->start_pos = ((cp->tp_on | tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
  612. }
  613. info_PK->end_pos = info_PK->start_pos + e - 1;
  614. info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance
  615. // to start of packet is incremented by value of start of packet*/
  616. }
  617. cstr_info->packno++;
  618. }
  619. /* << INDEX */
  620. tile->packno++;
  621. }
  622. }
  623. }
  624. pi_destroy(pi, cp, tileno);
  625. if (e == -999) {
  626. return e;
  627. }
  628. return (c - dest);
  629. }
  630. int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile, opj_codestream_info_t *cstr_info) {
  631. unsigned char *c = src;
  632. opj_pi_iterator_t *pi;
  633. int pino, e = 0;
  634. int n = 0, curtp = 0;
  635. int tp_start_packno;
  636. opj_image_t *image = t2->image;
  637. opj_cp_t *cp = t2->cp;
  638. /* create a packet iterator */
  639. pi = pi_create_decode(image, cp, tileno);
  640. if(!pi) {
  641. /* TODO: throw an error */
  642. return -999;
  643. }
  644. tp_start_packno = 0;
  645. for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
  646. while (pi_next(&pi[pino])) {
  647. if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
  648. opj_packet_info_t *pack_info;
  649. if (cstr_info)
  650. pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno];
  651. else
  652. pack_info = NULL;
  653. e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info);
  654. } else {
  655. e = 0;
  656. }
  657. if(e == -999)
  658. {
  659. pi_destroy(pi, cp, tileno);
  660. return -999;
  661. }
  662. /* progression in resolution */
  663. image->comps[pi[pino].compno].resno_decoded =
  664. (e > 0) ?
  665. int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded)
  666. : image->comps[pi[pino].compno].resno_decoded;
  667. n++;
  668. /* INDEX >> */
  669. if(cstr_info) {
  670. opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
  671. opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
  672. if (!cstr_info->packno) {
  673. info_PK->start_pos = info_TL->end_header + 1;
  674. } else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ /* New tile part*/
  675. info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part*/
  676. info_TL->tp[curtp].tp_start_pack = tp_start_packno;
  677. tp_start_packno = cstr_info->packno;
  678. curtp++;
  679. info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1;
  680. } else {
  681. info_PK->start_pos = (cp->tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
  682. }
  683. info_PK->end_pos = info_PK->start_pos + e - 1;
  684. info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance
  685. // to start of packet is incremented by value of start of packet*/
  686. cstr_info->packno++;
  687. }
  688. /* << INDEX */
  689. if (e == -999) { /* ADD */
  690. break;
  691. } else {
  692. c += e;
  693. }
  694. }
  695. }
  696. /* INDEX >> */
  697. if(cstr_info) {
  698. cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; /* Number of packets in last tile-part*/
  699. cstr_info->tile[tileno].tp[curtp].tp_start_pack = tp_start_packno;
  700. }
  701. /* << INDEX */
  702. /* don't forget to release pi */
  703. pi_destroy(pi, cp, tileno);
  704. if (e == -999) {
  705. return e;
  706. }
  707. return (c - src);
  708. }
  709. /* ----------------------------------------------------------------------- */
  710. opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
  711. /* create the tcd structure */
  712. opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
  713. if(!t2) return NULL;
  714. t2->cinfo = cinfo;
  715. t2->image = image;
  716. t2->cp = cp;
  717. return t2;
  718. }
  719. void t2_destroy(opj_t2_t *t2) {
  720. if(t2) {
  721. opj_free(t2);
  722. }
  723. }