/src/FreeImage/Source/LibOpenJPEG/t2.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 793 lines · 561 code · 99 blank · 133 comment · 190 complexity · 7fbbdb8a4f3035c45f7f53beeb3fa933 MD5 · raw file

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