t2.c 23 KB

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