t2.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
  8. * Copyright (c) 2002-2014, Professor Benoit Macq
  9. * Copyright (c) 2001-2003, David Janssens
  10. * Copyright (c) 2002-2003, Yannick Verschueren
  11. * Copyright (c) 2003-2007, Francois-Olivier Devaux
  12. * Copyright (c) 2003-2014, Antonin Descampe
  13. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  14. * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
  15. * Copyright (c) 2012, CS Systemes d'Information, France
  16. * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. */
  40. #include "opj_includes.h"
  41. #include "opj_common.h"
  42. /** @defgroup T2 T2 - Implementation of a tier-2 coding */
  43. /*@{*/
  44. /** @name Local static functions */
  45. /*@{*/
  46. static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n);
  47. static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio);
  48. /**
  49. Variable length code for signalling delta Zil (truncation point)
  50. @param bio Bit Input/Output component
  51. @param n delta Zil
  52. */
  53. static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n);
  54. static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio);
  55. /**
  56. Encode a packet of a tile to a destination buffer
  57. @param tileno Number of the tile encoded
  58. @param tile Tile for which to write the packets
  59. @param tcp Tile coding parameters
  60. @param pi Packet identity
  61. @param dest Destination buffer
  62. @param p_data_written FIXME DOC
  63. @param len Length of the destination buffer
  64. @param cstr_info Codestream information structure
  65. @param p_t2_mode If == THRESH_CALC In Threshold calculation ,If == FINAL_PASS Final pass
  66. @param p_manager the user event manager
  67. @return
  68. */
  69. static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
  70. opj_tcd_tile_t *tile,
  71. opj_tcp_t *tcp,
  72. opj_pi_iterator_t *pi,
  73. OPJ_BYTE *dest,
  74. OPJ_UINT32 * p_data_written,
  75. OPJ_UINT32 len,
  76. opj_codestream_info_t *cstr_info,
  77. J2K_T2_MODE p_t2_mode,
  78. opj_event_mgr_t *p_manager);
  79. /**
  80. Decode a packet of a tile from a source buffer
  81. @param t2 T2 handle
  82. @param tile Tile for which to write the packets
  83. @param tcp Tile coding parameters
  84. @param pi Packet identity
  85. @param src Source buffer
  86. @param data_read FIXME DOC
  87. @param max_length FIXME DOC
  88. @param pack_info Packet information
  89. @param p_manager the user event manager
  90. @return FIXME DOC
  91. */
  92. static OPJ_BOOL opj_t2_decode_packet(opj_t2_t* t2,
  93. opj_tcd_tile_t *tile,
  94. opj_tcp_t *tcp,
  95. opj_pi_iterator_t *pi,
  96. OPJ_BYTE *src,
  97. OPJ_UINT32 * data_read,
  98. OPJ_UINT32 max_length,
  99. opj_packet_info_t *pack_info,
  100. opj_event_mgr_t *p_manager);
  101. static OPJ_BOOL opj_t2_skip_packet(opj_t2_t* p_t2,
  102. opj_tcd_tile_t *p_tile,
  103. opj_tcp_t *p_tcp,
  104. opj_pi_iterator_t *p_pi,
  105. OPJ_BYTE *p_src,
  106. OPJ_UINT32 * p_data_read,
  107. OPJ_UINT32 p_max_length,
  108. opj_packet_info_t *p_pack_info,
  109. opj_event_mgr_t *p_manager);
  110. static OPJ_BOOL opj_t2_read_packet_header(opj_t2_t* p_t2,
  111. opj_tcd_tile_t *p_tile,
  112. opj_tcp_t *p_tcp,
  113. opj_pi_iterator_t *p_pi,
  114. OPJ_BOOL * p_is_data_present,
  115. OPJ_BYTE *p_src_data,
  116. OPJ_UINT32 * p_data_read,
  117. OPJ_UINT32 p_max_length,
  118. opj_packet_info_t *p_pack_info,
  119. opj_event_mgr_t *p_manager);
  120. static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
  121. opj_tcd_tile_t *p_tile,
  122. opj_pi_iterator_t *p_pi,
  123. OPJ_BYTE *p_src_data,
  124. OPJ_UINT32 * p_data_read,
  125. OPJ_UINT32 p_max_length,
  126. opj_packet_info_t *pack_info,
  127. opj_event_mgr_t *p_manager);
  128. static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2,
  129. opj_tcd_tile_t *p_tile,
  130. opj_pi_iterator_t *p_pi,
  131. OPJ_UINT32 * p_data_read,
  132. OPJ_UINT32 p_max_length,
  133. opj_packet_info_t *pack_info,
  134. opj_event_mgr_t *p_manager);
  135. /**
  136. @param cblk
  137. @param index
  138. @param cblksty
  139. @param first
  140. */
  141. static OPJ_BOOL opj_t2_init_seg(opj_tcd_cblk_dec_t* cblk,
  142. OPJ_UINT32 index,
  143. OPJ_UINT32 cblksty,
  144. OPJ_UINT32 first);
  145. /*@}*/
  146. /*@}*/
  147. /* ----------------------------------------------------------------------- */
  148. /* #define RESTART 0x04 */
  149. static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n)
  150. {
  151. while (--n >= 0) {
  152. opj_bio_putbit(bio, 1);
  153. }
  154. opj_bio_putbit(bio, 0);
  155. }
  156. static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio)
  157. {
  158. OPJ_UINT32 n = 0;
  159. while (opj_bio_read(bio, 1)) {
  160. ++n;
  161. }
  162. return n;
  163. }
  164. static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n)
  165. {
  166. if (n == 1) {
  167. opj_bio_putbit(bio, 0);
  168. } else if (n == 2) {
  169. opj_bio_write(bio, 2, 2);
  170. } else if (n <= 5) {
  171. opj_bio_write(bio, 0xc | (n - 3), 4);
  172. } else if (n <= 36) {
  173. opj_bio_write(bio, 0x1e0 | (n - 6), 9);
  174. } else if (n <= 164) {
  175. opj_bio_write(bio, 0xff80 | (n - 37), 16);
  176. }
  177. }
  178. static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio)
  179. {
  180. OPJ_UINT32 n;
  181. if (!opj_bio_read(bio, 1)) {
  182. return 1;
  183. }
  184. if (!opj_bio_read(bio, 1)) {
  185. return 2;
  186. }
  187. if ((n = opj_bio_read(bio, 2)) != 3) {
  188. return (3 + n);
  189. }
  190. if ((n = opj_bio_read(bio, 5)) != 31) {
  191. return (6 + n);
  192. }
  193. return (37 + opj_bio_read(bio, 7));
  194. }
  195. /* ----------------------------------------------------------------------- */
  196. OPJ_BOOL opj_t2_encode_packets(opj_t2_t* p_t2,
  197. OPJ_UINT32 p_tile_no,
  198. opj_tcd_tile_t *p_tile,
  199. OPJ_UINT32 p_maxlayers,
  200. OPJ_BYTE *p_dest,
  201. OPJ_UINT32 * p_data_written,
  202. OPJ_UINT32 p_max_len,
  203. opj_codestream_info_t *cstr_info,
  204. opj_tcd_marker_info_t* p_marker_info,
  205. OPJ_UINT32 p_tp_num,
  206. OPJ_INT32 p_tp_pos,
  207. OPJ_UINT32 p_pino,
  208. J2K_T2_MODE p_t2_mode,
  209. opj_event_mgr_t *p_manager)
  210. {
  211. OPJ_BYTE *l_current_data = p_dest;
  212. OPJ_UINT32 l_nb_bytes = 0;
  213. OPJ_UINT32 compno;
  214. OPJ_UINT32 poc;
  215. opj_pi_iterator_t *l_pi = 00;
  216. opj_pi_iterator_t *l_current_pi = 00;
  217. opj_image_t *l_image = p_t2->image;
  218. opj_cp_t *l_cp = p_t2->cp;
  219. opj_tcp_t *l_tcp = &l_cp->tcps[p_tile_no];
  220. OPJ_UINT32 pocno = (l_cp->rsiz == OPJ_PROFILE_CINEMA_4K) ? 2 : 1;
  221. OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ?
  222. l_image->numcomps : 1;
  223. OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
  224. l_pi = opj_pi_initialise_encode(l_image, l_cp, p_tile_no, p_t2_mode, p_manager);
  225. if (!l_pi) {
  226. return OPJ_FALSE;
  227. }
  228. * p_data_written = 0;
  229. if (p_t2_mode == THRESH_CALC) { /* Calculating threshold */
  230. l_current_pi = l_pi;
  231. for (compno = 0; compno < l_max_comp; ++compno) {
  232. OPJ_UINT32 l_comp_len = 0;
  233. l_current_pi = l_pi;
  234. for (poc = 0; poc < pocno ; ++poc) {
  235. OPJ_UINT32 l_tp_num = compno;
  236. /* TODO MSD : check why this function cannot fail (cf. v1) */
  237. opj_pi_create_encode(l_pi, l_cp, p_tile_no, poc, l_tp_num, p_tp_pos, p_t2_mode);
  238. if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
  239. /* TODO ADE : add an error */
  240. opj_pi_destroy(l_pi, l_nb_pocs);
  241. return OPJ_FALSE;
  242. }
  243. while (opj_pi_next(l_current_pi)) {
  244. if (l_current_pi->layno < p_maxlayers) {
  245. l_nb_bytes = 0;
  246. if (! opj_t2_encode_packet(p_tile_no, p_tile, l_tcp, l_current_pi,
  247. l_current_data, &l_nb_bytes,
  248. p_max_len, cstr_info,
  249. p_t2_mode,
  250. p_manager)) {
  251. opj_pi_destroy(l_pi, l_nb_pocs);
  252. return OPJ_FALSE;
  253. }
  254. l_comp_len += l_nb_bytes;
  255. l_current_data += l_nb_bytes;
  256. p_max_len -= l_nb_bytes;
  257. * p_data_written += l_nb_bytes;
  258. }
  259. }
  260. if (l_cp->m_specific_param.m_enc.m_max_comp_size) {
  261. if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) {
  262. opj_pi_destroy(l_pi, l_nb_pocs);
  263. return OPJ_FALSE;
  264. }
  265. }
  266. ++l_current_pi;
  267. }
  268. }
  269. } else { /* t2_mode == FINAL_PASS */
  270. opj_pi_create_encode(l_pi, l_cp, p_tile_no, p_pino, p_tp_num, p_tp_pos,
  271. p_t2_mode);
  272. l_current_pi = &l_pi[p_pino];
  273. if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
  274. /* TODO ADE : add an error */
  275. opj_pi_destroy(l_pi, l_nb_pocs);
  276. return OPJ_FALSE;
  277. }
  278. if (p_marker_info && p_marker_info->need_PLT) {
  279. /* One time use intended */
  280. assert(p_marker_info->packet_count == 0);
  281. assert(p_marker_info->p_packet_size == NULL);
  282. p_marker_info->p_packet_size = (OPJ_UINT32*) opj_malloc(
  283. opj_get_encoding_packet_count(l_image, l_cp, p_tile_no) * sizeof(OPJ_UINT32));
  284. if (p_marker_info->p_packet_size == NULL) {
  285. opj_pi_destroy(l_pi, l_nb_pocs);
  286. return OPJ_FALSE;
  287. }
  288. }
  289. while (opj_pi_next(l_current_pi)) {
  290. if (l_current_pi->layno < p_maxlayers) {
  291. l_nb_bytes = 0;
  292. if (! opj_t2_encode_packet(p_tile_no, p_tile, l_tcp, l_current_pi,
  293. l_current_data, &l_nb_bytes, p_max_len,
  294. cstr_info, p_t2_mode, p_manager)) {
  295. opj_pi_destroy(l_pi, l_nb_pocs);
  296. return OPJ_FALSE;
  297. }
  298. l_current_data += l_nb_bytes;
  299. p_max_len -= l_nb_bytes;
  300. * p_data_written += l_nb_bytes;
  301. if (p_marker_info && p_marker_info->need_PLT) {
  302. p_marker_info->p_packet_size[p_marker_info->packet_count] = l_nb_bytes;
  303. p_marker_info->packet_count ++;
  304. }
  305. /* INDEX >> */
  306. if (cstr_info) {
  307. if (cstr_info->index_write) {
  308. opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no];
  309. opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
  310. if (!cstr_info->packno) {
  311. info_PK->start_pos = info_TL->end_header + 1;
  312. } else {
  313. info_PK->start_pos = ((l_cp->m_specific_param.m_enc.m_tp_on | l_tcp->POC) &&
  314. info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno -
  315. 1].end_pos + 1;
  316. }
  317. info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1;
  318. info_PK->end_ph_pos += info_PK->start_pos -
  319. 1; /* End of packet header which now only represents the distance
  320. to start of packet is incremented by value of start of packet*/
  321. }
  322. cstr_info->packno++;
  323. }
  324. /* << INDEX */
  325. ++p_tile->packno;
  326. }
  327. }
  328. }
  329. opj_pi_destroy(l_pi, l_nb_pocs);
  330. return OPJ_TRUE;
  331. }
  332. /* see issue 80 */
  333. #if 0
  334. #define JAS_FPRINTF fprintf
  335. #else
  336. /* issue 290 */
  337. static void opj_null_jas_fprintf(FILE* file, const char * format, ...)
  338. {
  339. (void)file;
  340. (void)format;
  341. }
  342. #define JAS_FPRINTF opj_null_jas_fprintf
  343. #endif
  344. OPJ_BOOL opj_t2_decode_packets(opj_tcd_t* tcd,
  345. opj_t2_t *p_t2,
  346. OPJ_UINT32 p_tile_no,
  347. opj_tcd_tile_t *p_tile,
  348. OPJ_BYTE *p_src,
  349. OPJ_UINT32 * p_data_read,
  350. OPJ_UINT32 p_max_len,
  351. opj_codestream_index_t *p_cstr_index,
  352. opj_event_mgr_t *p_manager)
  353. {
  354. OPJ_BYTE *l_current_data = p_src;
  355. opj_pi_iterator_t *l_pi = 00;
  356. OPJ_UINT32 pino;
  357. opj_image_t *l_image = p_t2->image;
  358. opj_cp_t *l_cp = p_t2->cp;
  359. opj_tcp_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
  360. OPJ_UINT32 l_nb_bytes_read;
  361. OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
  362. opj_pi_iterator_t *l_current_pi = 00;
  363. #ifdef TODO_MSD
  364. OPJ_UINT32 curtp = 0;
  365. OPJ_UINT32 tp_start_packno;
  366. #endif
  367. opj_packet_info_t *l_pack_info = 00;
  368. opj_image_comp_t* l_img_comp = 00;
  369. OPJ_ARG_NOT_USED(p_cstr_index);
  370. #ifdef TODO_MSD
  371. if (p_cstr_index) {
  372. l_pack_info = p_cstr_index->tile_index[p_tile_no].packet;
  373. }
  374. #endif
  375. /* create a packet iterator */
  376. l_pi = opj_pi_create_decode(l_image, l_cp, p_tile_no, p_manager);
  377. if (!l_pi) {
  378. return OPJ_FALSE;
  379. }
  380. l_current_pi = l_pi;
  381. for (pino = 0; pino <= l_tcp->numpocs; ++pino) {
  382. /* if the resolution needed is too low, one dim of the tilec could be equal to zero
  383. * and no packets are used to decode this resolution and
  384. * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions
  385. * and no l_img_comp->resno_decoded are computed
  386. */
  387. OPJ_BOOL* first_pass_failed = NULL;
  388. if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
  389. /* TODO ADE : add an error */
  390. opj_pi_destroy(l_pi, l_nb_pocs);
  391. return OPJ_FALSE;
  392. }
  393. first_pass_failed = (OPJ_BOOL*)opj_malloc(l_image->numcomps * sizeof(OPJ_BOOL));
  394. if (!first_pass_failed) {
  395. opj_pi_destroy(l_pi, l_nb_pocs);
  396. return OPJ_FALSE;
  397. }
  398. memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(OPJ_BOOL));
  399. while (opj_pi_next(l_current_pi)) {
  400. OPJ_BOOL skip_packet = OPJ_FALSE;
  401. JAS_FPRINTF(stderr,
  402. "packet offset=00000166 prg=%d cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d\n\n",
  403. l_current_pi->poc.prg1, l_current_pi->compno, l_current_pi->resno,
  404. l_current_pi->precno, l_current_pi->layno);
  405. /* If the packet layer is greater or equal than the maximum */
  406. /* number of layers, skip the packet */
  407. if (l_current_pi->layno >= l_tcp->num_layers_to_decode) {
  408. skip_packet = OPJ_TRUE;
  409. }
  410. /* If the packet resolution number is greater than the minimum */
  411. /* number of resolution allowed, skip the packet */
  412. else if (l_current_pi->resno >=
  413. p_tile->comps[l_current_pi->compno].minimum_num_resolutions) {
  414. skip_packet = OPJ_TRUE;
  415. } else {
  416. /* If no precincts of any band intersects the area of interest, */
  417. /* skip the packet */
  418. OPJ_UINT32 bandno;
  419. opj_tcd_tilecomp_t *tilec = &p_tile->comps[l_current_pi->compno];
  420. opj_tcd_resolution_t *res = &tilec->resolutions[l_current_pi->resno];
  421. skip_packet = OPJ_TRUE;
  422. for (bandno = 0; bandno < res->numbands; ++bandno) {
  423. opj_tcd_band_t* band = &res->bands[bandno];
  424. opj_tcd_precinct_t* prec = &band->precincts[l_current_pi->precno];
  425. if (opj_tcd_is_subband_area_of_interest(tcd,
  426. l_current_pi->compno,
  427. l_current_pi->resno,
  428. band->bandno,
  429. (OPJ_UINT32)prec->x0,
  430. (OPJ_UINT32)prec->y0,
  431. (OPJ_UINT32)prec->x1,
  432. (OPJ_UINT32)prec->y1)) {
  433. skip_packet = OPJ_FALSE;
  434. break;
  435. }
  436. }
  437. /*
  438. printf("packet cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d -> %s\n",
  439. l_current_pi->compno, l_current_pi->resno,
  440. l_current_pi->precno, l_current_pi->layno, skip_packet ? "skipped" : "kept");
  441. */
  442. }
  443. if (!skip_packet) {
  444. l_nb_bytes_read = 0;
  445. first_pass_failed[l_current_pi->compno] = OPJ_FALSE;
  446. if (! opj_t2_decode_packet(p_t2, p_tile, l_tcp, l_current_pi, l_current_data,
  447. &l_nb_bytes_read, p_max_len, l_pack_info, p_manager)) {
  448. opj_pi_destroy(l_pi, l_nb_pocs);
  449. opj_free(first_pass_failed);
  450. return OPJ_FALSE;
  451. }
  452. l_img_comp = &(l_image->comps[l_current_pi->compno]);
  453. l_img_comp->resno_decoded = opj_uint_max(l_current_pi->resno,
  454. l_img_comp->resno_decoded);
  455. } else {
  456. l_nb_bytes_read = 0;
  457. if (! opj_t2_skip_packet(p_t2, p_tile, l_tcp, l_current_pi, l_current_data,
  458. &l_nb_bytes_read, p_max_len, l_pack_info, p_manager)) {
  459. opj_pi_destroy(l_pi, l_nb_pocs);
  460. opj_free(first_pass_failed);
  461. return OPJ_FALSE;
  462. }
  463. }
  464. if (first_pass_failed[l_current_pi->compno]) {
  465. l_img_comp = &(l_image->comps[l_current_pi->compno]);
  466. if (l_img_comp->resno_decoded == 0) {
  467. l_img_comp->resno_decoded =
  468. p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1;
  469. }
  470. }
  471. l_current_data += l_nb_bytes_read;
  472. p_max_len -= l_nb_bytes_read;
  473. /* INDEX >> */
  474. #ifdef TODO_MSD
  475. if (p_cstr_info) {
  476. opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no];
  477. opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
  478. tp_start_packno = 0;
  479. if (!p_cstr_info->packno) {
  480. info_PK->start_pos = info_TL->end_header + 1;
  481. } else if (info_TL->packet[p_cstr_info->packno - 1].end_pos >=
  482. (OPJ_INT32)
  483. p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_pos) { /* New tile part */
  484. info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno -
  485. tp_start_packno; /* Number of packets in previous tile-part */
  486. tp_start_packno = p_cstr_info->packno;
  487. curtp++;
  488. info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header + 1;
  489. } else {
  490. info_PK->start_pos = (l_cp->m_specific_param.m_enc.m_tp_on &&
  491. info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[p_cstr_info->packno -
  492. 1].end_pos + 1;
  493. }
  494. info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
  495. info_PK->end_ph_pos += info_PK->start_pos -
  496. 1; /* End of packet header which now only represents the distance */
  497. ++p_cstr_info->packno;
  498. }
  499. #endif
  500. /* << INDEX */
  501. }
  502. ++l_current_pi;
  503. opj_free(first_pass_failed);
  504. }
  505. /* INDEX >> */
  506. #ifdef TODO_MSD
  507. if
  508. (p_cstr_info) {
  509. p_cstr_info->tile[p_tile_no].tp[curtp].tp_numpacks = p_cstr_info->packno -
  510. tp_start_packno; /* Number of packets in last tile-part */
  511. }
  512. #endif
  513. /* << INDEX */
  514. /* don't forget to release pi */
  515. opj_pi_destroy(l_pi, l_nb_pocs);
  516. *p_data_read = (OPJ_UINT32)(l_current_data - p_src);
  517. return OPJ_TRUE;
  518. }
  519. /* ----------------------------------------------------------------------- */
  520. /**
  521. * Creates a Tier 2 handle
  522. *
  523. * @param p_image Source or destination image
  524. * @param p_cp Image coding parameters.
  525. * @return a new T2 handle if successful, NULL otherwise.
  526. */
  527. opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp)
  528. {
  529. /* create the t2 structure */
  530. opj_t2_t *l_t2 = (opj_t2_t*)opj_calloc(1, sizeof(opj_t2_t));
  531. if (!l_t2) {
  532. return NULL;
  533. }
  534. l_t2->image = p_image;
  535. l_t2->cp = p_cp;
  536. return l_t2;
  537. }
  538. void opj_t2_destroy(opj_t2_t *t2)
  539. {
  540. if (t2) {
  541. opj_free(t2);
  542. }
  543. }
  544. static OPJ_BOOL opj_t2_decode_packet(opj_t2_t* p_t2,
  545. opj_tcd_tile_t *p_tile,
  546. opj_tcp_t *p_tcp,
  547. opj_pi_iterator_t *p_pi,
  548. OPJ_BYTE *p_src,
  549. OPJ_UINT32 * p_data_read,
  550. OPJ_UINT32 p_max_length,
  551. opj_packet_info_t *p_pack_info,
  552. opj_event_mgr_t *p_manager)
  553. {
  554. OPJ_BOOL l_read_data;
  555. OPJ_UINT32 l_nb_bytes_read = 0;
  556. OPJ_UINT32 l_nb_total_bytes_read = 0;
  557. *p_data_read = 0;
  558. if (! opj_t2_read_packet_header(p_t2, p_tile, p_tcp, p_pi, &l_read_data, p_src,
  559. &l_nb_bytes_read, p_max_length, p_pack_info, p_manager)) {
  560. return OPJ_FALSE;
  561. }
  562. p_src += l_nb_bytes_read;
  563. l_nb_total_bytes_read += l_nb_bytes_read;
  564. p_max_length -= l_nb_bytes_read;
  565. /* we should read data for the packet */
  566. if (l_read_data) {
  567. l_nb_bytes_read = 0;
  568. if (! opj_t2_read_packet_data(p_t2, p_tile, p_pi, p_src, &l_nb_bytes_read,
  569. p_max_length, p_pack_info, p_manager)) {
  570. return OPJ_FALSE;
  571. }
  572. l_nb_total_bytes_read += l_nb_bytes_read;
  573. }
  574. *p_data_read = l_nb_total_bytes_read;
  575. return OPJ_TRUE;
  576. }
  577. static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,
  578. opj_tcd_tile_t * tile,
  579. opj_tcp_t * tcp,
  580. opj_pi_iterator_t *pi,
  581. OPJ_BYTE *dest,
  582. OPJ_UINT32 * p_data_written,
  583. OPJ_UINT32 length,
  584. opj_codestream_info_t *cstr_info,
  585. J2K_T2_MODE p_t2_mode,
  586. opj_event_mgr_t *p_manager)
  587. {
  588. OPJ_UINT32 bandno, cblkno;
  589. OPJ_BYTE* c = dest;
  590. OPJ_UINT32 l_nb_bytes;
  591. OPJ_UINT32 compno = pi->compno; /* component value */
  592. OPJ_UINT32 resno = pi->resno; /* resolution level value */
  593. OPJ_UINT32 precno = pi->precno; /* precinct value */
  594. OPJ_UINT32 layno = pi->layno; /* quality layer value */
  595. OPJ_UINT32 l_nb_blocks;
  596. opj_tcd_band_t *band = 00;
  597. opj_tcd_cblk_enc_t* cblk = 00;
  598. opj_tcd_pass_t *pass = 00;
  599. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  600. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  601. opj_bio_t *bio = 00; /* BIO component */
  602. #ifdef ENABLE_EMPTY_PACKET_OPTIMIZATION
  603. OPJ_BOOL packet_empty = OPJ_TRUE;
  604. #else
  605. OPJ_BOOL packet_empty = OPJ_FALSE;
  606. #endif
  607. #ifdef DEBUG_VERBOSE
  608. if (p_t2_mode == FINAL_PASS) {
  609. fprintf(stderr,
  610. "encode packet compono=%d, resno=%d, precno=%d, layno=%d\n",
  611. compno, resno, precno, layno);
  612. }
  613. #endif
  614. /* <SOP 0xff91> */
  615. if (tcp->csty & J2K_CP_CSTY_SOP) {
  616. if (length < 6) {
  617. if (p_t2_mode == FINAL_PASS) {
  618. opj_event_msg(p_manager, EVT_ERROR,
  619. "opj_t2_encode_packet(): only %u bytes remaining in "
  620. "output buffer. %u needed.\n",
  621. length, 6);
  622. }
  623. return OPJ_FALSE;
  624. }
  625. c[0] = 255;
  626. c[1] = 145;
  627. c[2] = 0;
  628. c[3] = 4;
  629. #if 0
  630. c[4] = (tile->packno % 65536) / 256;
  631. c[5] = (tile->packno % 65536) % 256;
  632. #else
  633. c[4] = (tile->packno >> 8) & 0xff; /* packno is uint32_t */
  634. c[5] = tile->packno & 0xff;
  635. #endif
  636. c += 6;
  637. length -= 6;
  638. }
  639. /* </SOP> */
  640. if (!layno) {
  641. band = res->bands;
  642. for (bandno = 0; bandno < res->numbands; ++bandno, ++band) {
  643. opj_tcd_precinct_t *prc;
  644. /* Skip empty bands */
  645. if (opj_tcd_is_band_empty(band)) {
  646. continue;
  647. }
  648. /* Avoid out of bounds access of https://github.com/uclouvain/openjpeg/issues/1294 */
  649. /* but likely not a proper fix. */
  650. if (precno >= res->pw * res->ph) {
  651. opj_event_msg(p_manager, EVT_ERROR,
  652. "opj_t2_encode_packet(): accessing precno=%u >= %u\n",
  653. precno, res->pw * res->ph);
  654. return OPJ_FALSE;
  655. }
  656. prc = &band->precincts[precno];
  657. opj_tgt_reset(prc->incltree);
  658. opj_tgt_reset(prc->imsbtree);
  659. l_nb_blocks = prc->cw * prc->ch;
  660. for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
  661. cblk = &prc->cblks.enc[cblkno];
  662. cblk->numpasses = 0;
  663. opj_tgt_setvalue(prc->imsbtree, cblkno, band->numbps - (OPJ_INT32)cblk->numbps);
  664. }
  665. }
  666. }
  667. bio = opj_bio_create();
  668. if (!bio) {
  669. /* FIXME event manager error callback */
  670. return OPJ_FALSE;
  671. }
  672. opj_bio_init_enc(bio, c, length);
  673. #ifdef ENABLE_EMPTY_PACKET_OPTIMIZATION
  674. /* WARNING: this code branch is disabled, since it has been reported that */
  675. /* such packets cause decoding issues with cinema J2K hardware */
  676. /* decoders: https://groups.google.com/forum/#!topic/openjpeg/M7M_fLX_Bco */
  677. /* Check if the packet is empty */
  678. /* Note: we could also skip that step and always write a packet header */
  679. band = res->bands;
  680. for (bandno = 0; bandno < res->numbands; ++bandno, ++band) {
  681. opj_tcd_precinct_t *prc;
  682. /* Skip empty bands */
  683. if (opj_tcd_is_band_empty(band)) {
  684. continue;
  685. }
  686. prc = &band->precincts[precno];
  687. l_nb_blocks = prc->cw * prc->ch;
  688. cblk = prc->cblks.enc;
  689. for (cblkno = 0; cblkno < l_nb_blocks; cblkno++, ++cblk) {
  690. opj_tcd_layer_t *layer = &cblk->layers[layno];
  691. /* if cblk not included, go to the next cblk */
  692. if (!layer->numpasses) {
  693. continue;
  694. }
  695. packet_empty = OPJ_FALSE;
  696. break;
  697. }
  698. if (!packet_empty) {
  699. break;
  700. }
  701. }
  702. #endif
  703. opj_bio_putbit(bio, packet_empty ? 0 : 1); /* Empty header bit */
  704. /* Writing Packet header */
  705. band = res->bands;
  706. for (bandno = 0; !packet_empty &&
  707. bandno < res->numbands; ++bandno, ++band) {
  708. opj_tcd_precinct_t *prc;
  709. /* Skip empty bands */
  710. if (opj_tcd_is_band_empty(band)) {
  711. continue;
  712. }
  713. /* Avoid out of bounds access of https://github.com/uclouvain/openjpeg/issues/1297 */
  714. /* but likely not a proper fix. */
  715. if (precno >= res->pw * res->ph) {
  716. opj_event_msg(p_manager, EVT_ERROR,
  717. "opj_t2_encode_packet(): accessing precno=%u >= %u\n",
  718. precno, res->pw * res->ph);
  719. return OPJ_FALSE;
  720. }
  721. prc = &band->precincts[precno];
  722. l_nb_blocks = prc->cw * prc->ch;
  723. cblk = prc->cblks.enc;
  724. for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
  725. opj_tcd_layer_t *layer = &cblk->layers[layno];
  726. if (!cblk->numpasses && layer->numpasses) {
  727. opj_tgt_setvalue(prc->incltree, cblkno, (OPJ_INT32)layno);
  728. }
  729. ++cblk;
  730. }
  731. cblk = prc->cblks.enc;
  732. for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) {
  733. opj_tcd_layer_t *layer = &cblk->layers[layno];
  734. OPJ_UINT32 increment = 0;
  735. OPJ_UINT32 nump = 0;
  736. OPJ_UINT32 len = 0, passno;
  737. OPJ_UINT32 l_nb_passes;
  738. /* cblk inclusion bits */
  739. if (!cblk->numpasses) {
  740. opj_tgt_encode(bio, prc->incltree, cblkno, (OPJ_INT32)(layno + 1));
  741. } else {
  742. opj_bio_putbit(bio, layer->numpasses != 0);
  743. }
  744. /* if cblk not included, go to the next cblk */
  745. if (!layer->numpasses) {
  746. ++cblk;
  747. continue;
  748. }
  749. /* if first instance of cblk --> zero bit-planes information */
  750. if (!cblk->numpasses) {
  751. cblk->numlenbits = 3;
  752. opj_tgt_encode(bio, prc->imsbtree, cblkno, 999);
  753. }
  754. /* number of coding passes included */
  755. opj_t2_putnumpasses(bio, layer->numpasses);
  756. l_nb_passes = cblk->numpasses + layer->numpasses;
  757. pass = cblk->passes + cblk->numpasses;
  758. /* computation of the increase of the length indicator and insertion in the header */
  759. for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
  760. ++nump;
  761. len += pass->len;
  762. if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
  763. increment = (OPJ_UINT32)opj_int_max((OPJ_INT32)increment,
  764. opj_int_floorlog2((OPJ_INT32)len) + 1
  765. - ((OPJ_INT32)cblk->numlenbits + opj_int_floorlog2((OPJ_INT32)nump)));
  766. len = 0;
  767. nump = 0;
  768. }
  769. ++pass;
  770. }
  771. opj_t2_putcommacode(bio, (OPJ_INT32)increment);
  772. /* computation of the new Length indicator */
  773. cblk->numlenbits += increment;
  774. pass = cblk->passes + cblk->numpasses;
  775. /* insertion of the codeword segment length */
  776. for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
  777. nump++;
  778. len += pass->len;
  779. if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
  780. opj_bio_write(bio, (OPJ_UINT32)len,
  781. cblk->numlenbits + (OPJ_UINT32)opj_int_floorlog2((OPJ_INT32)nump));
  782. len = 0;
  783. nump = 0;
  784. }
  785. ++pass;
  786. }
  787. ++cblk;
  788. }
  789. }
  790. if (!opj_bio_flush(bio)) {
  791. opj_bio_destroy(bio);
  792. return OPJ_FALSE; /* modified to eliminate longjmp !! */
  793. }
  794. l_nb_bytes = (OPJ_UINT32)opj_bio_numbytes(bio);
  795. c += l_nb_bytes;
  796. length -= l_nb_bytes;
  797. opj_bio_destroy(bio);
  798. /* <EPH 0xff92> */
  799. if (tcp->csty & J2K_CP_CSTY_EPH) {
  800. if (length < 2) {
  801. if (p_t2_mode == FINAL_PASS) {
  802. opj_event_msg(p_manager, EVT_ERROR,
  803. "opj_t2_encode_packet(): only %u bytes remaining in "
  804. "output buffer. %u needed.\n",
  805. length, 2);
  806. }
  807. return OPJ_FALSE;
  808. }
  809. c[0] = 255;
  810. c[1] = 146;
  811. c += 2;
  812. length -= 2;
  813. }
  814. /* </EPH> */
  815. /* << INDEX */
  816. /* End of packet header position. Currently only represents the distance to start of packet
  817. Will be updated later by incrementing with packet start value*/
  818. if (cstr_info && cstr_info->index_write) {
  819. opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
  820. info_PK->end_ph_pos = (OPJ_INT32)(c - dest);
  821. }
  822. /* INDEX >> */
  823. /* Writing the packet body */
  824. band = res->bands;
  825. for (bandno = 0; !packet_empty && bandno < res->numbands; bandno++, ++band) {
  826. opj_tcd_precinct_t *prc;
  827. /* Skip empty bands */
  828. if (opj_tcd_is_band_empty(band)) {
  829. continue;
  830. }
  831. prc = &band->precincts[precno];
  832. l_nb_blocks = prc->cw * prc->ch;
  833. cblk = prc->cblks.enc;
  834. for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
  835. opj_tcd_layer_t *layer = &cblk->layers[layno];
  836. if (!layer->numpasses) {
  837. ++cblk;
  838. continue;
  839. }
  840. if (layer->len > length) {
  841. if (p_t2_mode == FINAL_PASS) {
  842. opj_event_msg(p_manager, EVT_ERROR,
  843. "opj_t2_encode_packet(): only %u bytes remaining in "
  844. "output buffer. %u needed.\n",
  845. length, layer->len);
  846. }
  847. return OPJ_FALSE;
  848. }
  849. if (p_t2_mode == FINAL_PASS) {
  850. memcpy(c, layer->data, layer->len);
  851. }
  852. cblk->numpasses += layer->numpasses;
  853. c += layer->len;
  854. length -= layer->len;
  855. /* << INDEX */
  856. if (cstr_info && cstr_info->index_write) {
  857. opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
  858. info_PK->disto += layer->disto;
  859. if (cstr_info->D_max < info_PK->disto) {
  860. cstr_info->D_max = info_PK->disto;
  861. }
  862. }
  863. ++cblk;
  864. /* INDEX >> */
  865. }
  866. }
  867. assert(c >= dest);
  868. * p_data_written += (OPJ_UINT32)(c - dest);
  869. return OPJ_TRUE;
  870. }
  871. static OPJ_BOOL opj_t2_skip_packet(opj_t2_t* p_t2,
  872. opj_tcd_tile_t *p_tile,
  873. opj_tcp_t *p_tcp,
  874. opj_pi_iterator_t *p_pi,
  875. OPJ_BYTE *p_src,
  876. OPJ_UINT32 * p_data_read,
  877. OPJ_UINT32 p_max_length,
  878. opj_packet_info_t *p_pack_info,
  879. opj_event_mgr_t *p_manager)
  880. {
  881. OPJ_BOOL l_read_data;
  882. OPJ_UINT32 l_nb_bytes_read = 0;
  883. OPJ_UINT32 l_nb_total_bytes_read = 0;
  884. *p_data_read = 0;
  885. if (! opj_t2_read_packet_header(p_t2, p_tile, p_tcp, p_pi, &l_read_data, p_src,
  886. &l_nb_bytes_read, p_max_length, p_pack_info, p_manager)) {
  887. return OPJ_FALSE;
  888. }
  889. p_src += l_nb_bytes_read;
  890. l_nb_total_bytes_read += l_nb_bytes_read;
  891. p_max_length -= l_nb_bytes_read;
  892. /* we should read data for the packet */
  893. if (l_read_data) {
  894. l_nb_bytes_read = 0;
  895. if (! opj_t2_skip_packet_data(p_t2, p_tile, p_pi, &l_nb_bytes_read,
  896. p_max_length, p_pack_info, p_manager)) {
  897. return OPJ_FALSE;
  898. }
  899. l_nb_total_bytes_read += l_nb_bytes_read;
  900. }
  901. *p_data_read = l_nb_total_bytes_read;
  902. return OPJ_TRUE;
  903. }
  904. static OPJ_BOOL opj_t2_read_packet_header(opj_t2_t* p_t2,
  905. opj_tcd_tile_t *p_tile,
  906. opj_tcp_t *p_tcp,
  907. opj_pi_iterator_t *p_pi,
  908. OPJ_BOOL * p_is_data_present,
  909. OPJ_BYTE *p_src_data,
  910. OPJ_UINT32 * p_data_read,
  911. OPJ_UINT32 p_max_length,
  912. opj_packet_info_t *p_pack_info,
  913. opj_event_mgr_t *p_manager)
  914. {
  915. /* loop */
  916. OPJ_UINT32 bandno, cblkno;
  917. OPJ_UINT32 l_nb_code_blocks;
  918. OPJ_UINT32 l_remaining_length;
  919. OPJ_UINT32 l_header_length;
  920. OPJ_UINT32 * l_modified_length_ptr = 00;
  921. OPJ_BYTE *l_current_data = p_src_data;
  922. opj_cp_t *l_cp = p_t2->cp;
  923. opj_bio_t *l_bio = 00; /* BIO component */
  924. opj_tcd_band_t *l_band = 00;
  925. opj_tcd_cblk_dec_t* l_cblk = 00;
  926. opj_tcd_resolution_t* l_res =
  927. &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
  928. OPJ_BYTE *l_header_data = 00;
  929. OPJ_BYTE **l_header_data_start = 00;
  930. OPJ_UINT32 l_present;
  931. if (p_pi->layno == 0) {
  932. l_band = l_res->bands;
  933. /* reset tagtrees */
  934. for (bandno = 0; bandno < l_res->numbands; ++bandno) {
  935. if (!opj_tcd_is_band_empty(l_band)) {
  936. opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
  937. if (!(p_pi->precno < (l_band->precincts_data_size / sizeof(
  938. opj_tcd_precinct_t)))) {
  939. opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct\n");
  940. return OPJ_FALSE;
  941. }
  942. opj_tgt_reset(l_prc->incltree);
  943. opj_tgt_reset(l_prc->imsbtree);
  944. l_cblk = l_prc->cblks.dec;
  945. l_nb_code_blocks = l_prc->cw * l_prc->ch;
  946. for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
  947. l_cblk->numsegs = 0;
  948. l_cblk->real_num_segs = 0;
  949. ++l_cblk;
  950. }
  951. }
  952. ++l_band;
  953. }
  954. }
  955. /* SOP markers */
  956. if (p_tcp->csty & J2K_CP_CSTY_SOP) {
  957. if (p_max_length < 6) {
  958. opj_event_msg(p_manager, EVT_WARNING,
  959. "Not enough space for expected SOP marker\n");
  960. } else if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
  961. opj_event_msg(p_manager, EVT_WARNING, "Expected SOP marker\n");
  962. } else {
  963. l_current_data += 6;
  964. }
  965. /** TODO : check the Nsop value */
  966. }
  967. /*
  968. When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
  969. This part deal with this characteristic
  970. step 1: Read packet header in the saved structure
  971. step 2: Return to codestream for decoding
  972. */
  973. l_bio = opj_bio_create();
  974. if (! l_bio) {
  975. return OPJ_FALSE;
  976. }
  977. if (l_cp->ppm == 1) { /* PPM */
  978. l_header_data_start = &l_cp->ppm_data;
  979. l_header_data = *l_header_data_start;
  980. l_modified_length_ptr = &(l_cp->ppm_len);
  981. } else if (p_tcp->ppt == 1) { /* PPT */
  982. l_header_data_start = &(p_tcp->ppt_data);
  983. l_header_data = *l_header_data_start;
  984. l_modified_length_ptr = &(p_tcp->ppt_len);
  985. } else { /* Normal Case */
  986. l_header_data_start = &(l_current_data);
  987. l_header_data = *l_header_data_start;
  988. l_remaining_length = (OPJ_UINT32)(p_src_data + p_max_length - l_header_data);
  989. l_modified_length_ptr = &(l_remaining_length);
  990. }
  991. opj_bio_init_dec(l_bio, l_header_data, *l_modified_length_ptr);
  992. l_present = opj_bio_read(l_bio, 1);
  993. JAS_FPRINTF(stderr, "present=%d \n", l_present);
  994. if (!l_present) {
  995. /* TODO MSD: no test to control the output of this function*/
  996. opj_bio_inalign(l_bio);
  997. l_header_data += opj_bio_numbytes(l_bio);
  998. opj_bio_destroy(l_bio);
  999. /* EPH markers */
  1000. if (p_tcp->csty & J2K_CP_CSTY_EPH) {
  1001. if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data -
  1002. *l_header_data_start)) < 2U) {
  1003. opj_event_msg(p_manager, EVT_WARNING,
  1004. "Not enough space for expected EPH marker\n");
  1005. } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
  1006. opj_event_msg(p_manager, EVT_WARNING, "Expected EPH marker\n");
  1007. } else {
  1008. l_header_data += 2;
  1009. }
  1010. }
  1011. l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
  1012. *l_modified_length_ptr -= l_header_length;
  1013. *l_header_data_start += l_header_length;
  1014. /* << INDEX */
  1015. /* End of packet header position. Currently only represents the distance to start of packet
  1016. Will be updated later by incrementing with packet start value */
  1017. if (p_pack_info) {
  1018. p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
  1019. }
  1020. /* INDEX >> */
  1021. * p_is_data_present = OPJ_FALSE;
  1022. *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
  1023. return OPJ_TRUE;
  1024. }
  1025. l_band = l_res->bands;
  1026. for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band) {
  1027. opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]);
  1028. if (opj_tcd_is_band_empty(l_band)) {
  1029. continue;
  1030. }
  1031. l_nb_code_blocks = l_prc->cw * l_prc->ch;
  1032. l_cblk = l_prc->cblks.dec;
  1033. for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) {
  1034. OPJ_UINT32 l_included, l_increment, l_segno;
  1035. OPJ_INT32 n;
  1036. /* if cblk not yet included before --> inclusion tagtree */
  1037. if (!l_cblk->numsegs) {
  1038. l_included = opj_tgt_decode(l_bio, l_prc->incltree, cblkno,
  1039. (OPJ_INT32)(p_pi->layno + 1));
  1040. /* else one bit */
  1041. } else {
  1042. l_included = opj_bio_read(l_bio, 1);
  1043. }
  1044. /* if cblk not included */
  1045. if (!l_included) {
  1046. l_cblk->numnewpasses = 0;
  1047. ++l_cblk;
  1048. JAS_FPRINTF(stderr, "included=%d \n", l_included);
  1049. continue;
  1050. }
  1051. /* if cblk not yet included --> zero-bitplane tagtree */
  1052. if (!l_cblk->numsegs) {
  1053. OPJ_UINT32 i = 0;
  1054. while (!opj_tgt_decode(l_bio, l_prc->imsbtree, cblkno, (OPJ_INT32)i)) {
  1055. ++i;
  1056. }
  1057. l_cblk->Mb = (OPJ_UINT32)l_band->numbps;
  1058. if ((OPJ_UINT32)l_band->numbps + 1 < i) {
  1059. /* Not totally sure what we should do in that situation,
  1060. * but that avoids the integer overflow of
  1061. * https://github.com/uclouvain/openjpeg/pull/1488
  1062. * while keeping the regression test suite happy.
  1063. */
  1064. l_cblk->numbps = (OPJ_UINT32)(l_band->numbps + 1 - (int)i);
  1065. } else {
  1066. l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i;
  1067. }
  1068. l_cblk->numlenbits = 3;
  1069. }
  1070. /* number of coding passes */
  1071. l_cblk->numnewpasses = opj_t2_getnumpasses(l_bio);
  1072. l_increment = opj_t2_getcommacode(l_bio);
  1073. /* length indicator increment */
  1074. l_cblk->numlenbits += l_increment;
  1075. l_segno = 0;
  1076. if (!l_cblk->numsegs) {
  1077. if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
  1078. opj_bio_destroy(l_bio);
  1079. return OPJ_FALSE;
  1080. }
  1081. } else {
  1082. l_segno = l_cblk->numsegs - 1;
  1083. if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
  1084. ++l_segno;
  1085. if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
  1086. opj_bio_destroy(l_bio);
  1087. return OPJ_FALSE;
  1088. }
  1089. }
  1090. }
  1091. n = (OPJ_INT32)l_cblk->numnewpasses;
  1092. if ((p_tcp->tccps[p_pi->compno].cblksty & J2K_CCP_CBLKSTY_HT) != 0)
  1093. do {
  1094. OPJ_UINT32 bit_number;
  1095. l_cblk->segs[l_segno].numnewpasses = l_segno == 0 ? 1 : (OPJ_UINT32)n;
  1096. bit_number = l_cblk->numlenbits + opj_uint_floorlog2(
  1097. l_cblk->segs[l_segno].numnewpasses);
  1098. if (bit_number > 32) {
  1099. opj_event_msg(p_manager, EVT_ERROR,
  1100. "Invalid bit number %d in opj_t2_read_packet_header()\n",
  1101. bit_number);
  1102. opj_bio_destroy(l_bio);
  1103. return OPJ_FALSE;
  1104. }
  1105. l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, bit_number);
  1106. JAS_FPRINTF(stderr, "included=%d numnewpasses=%d increment=%d len=%d \n",
  1107. l_included, l_cblk->segs[l_segno].numnewpasses, l_increment,
  1108. l_cblk->segs[l_segno].newlen);
  1109. n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses;
  1110. if (n > 0) {
  1111. ++l_segno;
  1112. if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
  1113. opj_bio_destroy(l_bio);
  1114. return OPJ_FALSE;
  1115. }
  1116. }
  1117. } while (n > 0);
  1118. else
  1119. do {
  1120. OPJ_UINT32 bit_number;
  1121. l_cblk->segs[l_segno].numnewpasses = (OPJ_UINT32)opj_int_min((OPJ_INT32)(
  1122. l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses), n);
  1123. bit_number = l_cblk->numlenbits + opj_uint_floorlog2(
  1124. l_cblk->segs[l_segno].numnewpasses);
  1125. if (bit_number > 32) {
  1126. opj_event_msg(p_manager, EVT_ERROR,
  1127. "Invalid bit number %d in opj_t2_read_packet_header()\n",
  1128. bit_number);
  1129. opj_bio_destroy(l_bio);
  1130. return OPJ_FALSE;
  1131. }
  1132. l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, bit_number);
  1133. JAS_FPRINTF(stderr, "included=%d numnewpasses=%d increment=%d len=%d \n",
  1134. l_included, l_cblk->segs[l_segno].numnewpasses, l_increment,
  1135. l_cblk->segs[l_segno].newlen);
  1136. n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses;
  1137. if (n > 0) {
  1138. ++l_segno;
  1139. if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
  1140. opj_bio_destroy(l_bio);
  1141. return OPJ_FALSE;
  1142. }
  1143. }
  1144. } while (n > 0);
  1145. ++l_cblk;
  1146. }
  1147. }
  1148. if (!opj_bio_inalign(l_bio)) {
  1149. opj_bio_destroy(l_bio);
  1150. return OPJ_FALSE;
  1151. }
  1152. l_header_data += opj_bio_numbytes(l_bio);
  1153. opj_bio_destroy(l_bio);
  1154. /* EPH markers */
  1155. if (p_tcp->csty & J2K_CP_CSTY_EPH) {
  1156. if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data -
  1157. *l_header_data_start)) < 2U) {
  1158. opj_event_msg(p_manager, EVT_WARNING,
  1159. "Not enough space for expected EPH marker\n");
  1160. } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
  1161. opj_event_msg(p_manager, EVT_WARNING, "Expected EPH marker\n");
  1162. } else {
  1163. l_header_data += 2;
  1164. }
  1165. }
  1166. l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
  1167. JAS_FPRINTF(stderr, "hdrlen=%d \n", l_header_length);
  1168. JAS_FPRINTF(stderr, "packet body\n");
  1169. *l_modified_length_ptr -= l_header_length;
  1170. *l_header_data_start += l_header_length;
  1171. /* << INDEX */
  1172. /* End of packet header position. Currently only represents the distance to start of packet
  1173. Will be updated later by incrementing with packet start value */
  1174. if (p_pack_info) {
  1175. p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
  1176. }
  1177. /* INDEX >> */
  1178. *p_is_data_present = OPJ_TRUE;
  1179. *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
  1180. return OPJ_TRUE;
  1181. }
  1182. static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
  1183. opj_tcd_tile_t *p_tile,
  1184. opj_pi_iterator_t *p_pi,
  1185. OPJ_BYTE *p_src_data,
  1186. OPJ_UINT32 * p_data_read,
  1187. OPJ_UINT32 p_max_length,
  1188. opj_packet_info_t *pack_info,
  1189. opj_event_mgr_t* p_manager)
  1190. {
  1191. OPJ_UINT32 bandno, cblkno;
  1192. OPJ_UINT32 l_nb_code_blocks;
  1193. OPJ_BYTE *l_current_data = p_src_data;
  1194. opj_tcd_band_t *l_band = 00;
  1195. opj_tcd_cblk_dec_t* l_cblk = 00;
  1196. opj_tcd_resolution_t* l_res =
  1197. &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
  1198. OPJ_BOOL partial_buffer = OPJ_FALSE;
  1199. OPJ_ARG_NOT_USED(p_t2);
  1200. OPJ_ARG_NOT_USED(pack_info);
  1201. l_band = l_res->bands;
  1202. for (bandno = 0; bandno < l_res->numbands; ++bandno) {
  1203. opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
  1204. if ((l_band->x1 - l_band->x0 == 0) || (l_band->y1 - l_band->y0 == 0)) {
  1205. ++l_band;
  1206. continue;
  1207. }
  1208. l_nb_code_blocks = l_prc->cw * l_prc->ch;
  1209. l_cblk = l_prc->cblks.dec;
  1210. for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
  1211. opj_tcd_seg_t *l_seg = 00;
  1212. // if we have a partial data stream, set numchunks to zero
  1213. // since we have no data to actually decode.
  1214. if (partial_buffer) {
  1215. l_cblk->numchunks = 0;
  1216. }
  1217. if (!l_cblk->numnewpasses) {
  1218. /* nothing to do */
  1219. ++l_cblk;
  1220. continue;
  1221. }
  1222. if (!l_cblk->numsegs) {
  1223. l_seg = l_cblk->segs;
  1224. ++l_cblk->numsegs;
  1225. } else {
  1226. l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
  1227. if (l_seg->numpasses == l_seg->maxpasses) {
  1228. ++l_seg;
  1229. ++l_cblk->numsegs;
  1230. }
  1231. }
  1232. do {
  1233. /* Check possible overflow (on l_current_data only, assumes input args already checked) then size */
  1234. if ((((OPJ_SIZE_T)l_current_data + (OPJ_SIZE_T)l_seg->newlen) <
  1235. (OPJ_SIZE_T)l_current_data) ||
  1236. (l_current_data + l_seg->newlen > p_src_data + p_max_length) ||
  1237. (partial_buffer)) {
  1238. if (p_t2->cp->strict) {
  1239. opj_event_msg(p_manager, EVT_ERROR,
  1240. "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1241. l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
  1242. p_pi->compno);
  1243. return OPJ_FALSE;
  1244. } else {
  1245. opj_event_msg(p_manager, EVT_WARNING,
  1246. "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1247. l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
  1248. p_pi->compno);
  1249. // skip this codeblock since it is a partial read
  1250. partial_buffer = OPJ_TRUE;
  1251. l_cblk->numchunks = 0;
  1252. l_seg->numpasses += l_seg->numnewpasses;
  1253. l_cblk->numnewpasses -= l_seg->numnewpasses;
  1254. if (l_cblk->numnewpasses > 0) {
  1255. ++l_seg;
  1256. ++l_cblk->numsegs;
  1257. break;
  1258. }
  1259. continue;
  1260. }
  1261. }
  1262. #ifdef USE_JPWL
  1263. /* we need here a j2k handle to verify if making a check to
  1264. the validity of cblocks parameters is selected from user (-W) */
  1265. /* let's check that we are not exceeding */
  1266. if ((l_cblk->len + l_seg->newlen) > 8192) {
  1267. opj_event_msg(p_manager, EVT_WARNING,
  1268. "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1269. l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
  1270. if (!JPWL_ASSUME) {
  1271. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  1272. return OPJ_FALSE;
  1273. }
  1274. l_seg->newlen = 8192 - l_cblk->len;
  1275. opj_event_msg(p_manager, EVT_WARNING, " - truncating segment to %d\n",
  1276. l_seg->newlen);
  1277. break;
  1278. };
  1279. #endif /* USE_JPWL */
  1280. if (l_cblk->numchunks == l_cblk->numchunksalloc) {
  1281. OPJ_UINT32 l_numchunksalloc = l_cblk->numchunksalloc * 2 + 1;
  1282. opj_tcd_seg_data_chunk_t* l_chunks =
  1283. (opj_tcd_seg_data_chunk_t*)opj_realloc(l_cblk->chunks,
  1284. l_numchunksalloc * sizeof(opj_tcd_seg_data_chunk_t));
  1285. if (l_chunks == NULL) {
  1286. opj_event_msg(p_manager, EVT_ERROR,
  1287. "cannot allocate opj_tcd_seg_data_chunk_t* array");
  1288. return OPJ_FALSE;
  1289. }
  1290. l_cblk->chunks = l_chunks;
  1291. l_cblk->numchunksalloc = l_numchunksalloc;
  1292. }
  1293. l_cblk->chunks[l_cblk->numchunks].data = l_current_data;
  1294. l_cblk->chunks[l_cblk->numchunks].len = l_seg->newlen;
  1295. l_cblk->numchunks ++;
  1296. l_current_data += l_seg->newlen;
  1297. l_seg->len += l_seg->newlen;
  1298. l_seg->numpasses += l_seg->numnewpasses;
  1299. l_cblk->numnewpasses -= l_seg->numnewpasses;
  1300. l_seg->real_num_passes = l_seg->numpasses;
  1301. if (l_cblk->numnewpasses > 0) {
  1302. ++l_seg;
  1303. ++l_cblk->numsegs;
  1304. }
  1305. } while (l_cblk->numnewpasses > 0);
  1306. l_cblk->real_num_segs = l_cblk->numsegs;
  1307. ++l_cblk;
  1308. } /* next code_block */
  1309. ++l_band;
  1310. }
  1311. // return the number of bytes read
  1312. if (partial_buffer) {
  1313. *(p_data_read) = p_max_length;
  1314. } else {
  1315. *(p_data_read) = (OPJ_UINT32)(l_current_data - p_src_data);
  1316. }
  1317. return OPJ_TRUE;
  1318. }
  1319. static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2,
  1320. opj_tcd_tile_t *p_tile,
  1321. opj_pi_iterator_t *p_pi,
  1322. OPJ_UINT32 * p_data_read,
  1323. OPJ_UINT32 p_max_length,
  1324. opj_packet_info_t *pack_info,
  1325. opj_event_mgr_t *p_manager)
  1326. {
  1327. OPJ_UINT32 bandno, cblkno;
  1328. OPJ_UINT32 l_nb_code_blocks;
  1329. opj_tcd_band_t *l_band = 00;
  1330. opj_tcd_cblk_dec_t* l_cblk = 00;
  1331. opj_tcd_resolution_t* l_res =
  1332. &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
  1333. OPJ_ARG_NOT_USED(p_t2);
  1334. OPJ_ARG_NOT_USED(pack_info);
  1335. *p_data_read = 0;
  1336. l_band = l_res->bands;
  1337. for (bandno = 0; bandno < l_res->numbands; ++bandno) {
  1338. opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
  1339. if ((l_band->x1 - l_band->x0 == 0) || (l_band->y1 - l_band->y0 == 0)) {
  1340. ++l_band;
  1341. continue;
  1342. }
  1343. l_nb_code_blocks = l_prc->cw * l_prc->ch;
  1344. l_cblk = l_prc->cblks.dec;
  1345. for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
  1346. opj_tcd_seg_t *l_seg = 00;
  1347. if (!l_cblk->numnewpasses) {
  1348. /* nothing to do */
  1349. ++l_cblk;
  1350. continue;
  1351. }
  1352. if (!l_cblk->numsegs) {
  1353. l_seg = l_cblk->segs;
  1354. ++l_cblk->numsegs;
  1355. } else {
  1356. l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
  1357. if (l_seg->numpasses == l_seg->maxpasses) {
  1358. ++l_seg;
  1359. ++l_cblk->numsegs;
  1360. }
  1361. }
  1362. do {
  1363. /* Check possible overflow then size */
  1364. if (((*p_data_read + l_seg->newlen) < (*p_data_read)) ||
  1365. ((*p_data_read + l_seg->newlen) > p_max_length)) {
  1366. if (p_t2->cp->strict) {
  1367. opj_event_msg(p_manager, EVT_ERROR,
  1368. "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1369. l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
  1370. p_pi->compno);
  1371. return OPJ_FALSE;
  1372. } else {
  1373. opj_event_msg(p_manager, EVT_WARNING,
  1374. "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1375. l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
  1376. p_pi->compno);
  1377. return OPJ_TRUE;
  1378. }
  1379. }
  1380. #ifdef USE_JPWL
  1381. /* we need here a j2k handle to verify if making a check to
  1382. the validity of cblocks parameters is selected from user (-W) */
  1383. /* let's check that we are not exceeding */
  1384. if ((l_cblk->len + l_seg->newlen) > 8192) {
  1385. opj_event_msg(p_manager, EVT_WARNING,
  1386. "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1387. l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
  1388. if (!JPWL_ASSUME) {
  1389. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  1390. return -999;
  1391. }
  1392. l_seg->newlen = 8192 - l_cblk->len;
  1393. opj_event_msg(p_manager, EVT_WARNING, " - truncating segment to %d\n",
  1394. l_seg->newlen);
  1395. break;
  1396. };
  1397. #endif /* USE_JPWL */
  1398. JAS_FPRINTF(stderr, "p_data_read (%d) newlen (%d) \n", *p_data_read,
  1399. l_seg->newlen);
  1400. *(p_data_read) += l_seg->newlen;
  1401. l_seg->numpasses += l_seg->numnewpasses;
  1402. l_cblk->numnewpasses -= l_seg->numnewpasses;
  1403. if (l_cblk->numnewpasses > 0) {
  1404. ++l_seg;
  1405. ++l_cblk->numsegs;
  1406. }
  1407. } while (l_cblk->numnewpasses > 0);
  1408. ++l_cblk;
  1409. }
  1410. ++l_band;
  1411. }
  1412. return OPJ_TRUE;
  1413. }
  1414. static OPJ_BOOL opj_t2_init_seg(opj_tcd_cblk_dec_t* cblk,
  1415. OPJ_UINT32 index,
  1416. OPJ_UINT32 cblksty,
  1417. OPJ_UINT32 first)
  1418. {
  1419. opj_tcd_seg_t* seg = 00;
  1420. OPJ_UINT32 l_nb_segs = index + 1;
  1421. if (l_nb_segs > cblk->m_current_max_segs) {
  1422. opj_tcd_seg_t* new_segs;
  1423. OPJ_UINT32 l_m_current_max_segs = cblk->m_current_max_segs +
  1424. OPJ_J2K_DEFAULT_NB_SEGS;
  1425. new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs,
  1426. l_m_current_max_segs * sizeof(opj_tcd_seg_t));
  1427. if (! new_segs) {
  1428. /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */
  1429. return OPJ_FALSE;
  1430. }
  1431. cblk->segs = new_segs;
  1432. memset(new_segs + cblk->m_current_max_segs,
  1433. 0, OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));
  1434. cblk->m_current_max_segs = l_m_current_max_segs;
  1435. }
  1436. seg = &cblk->segs[index];
  1437. opj_tcd_reinit_segment(seg);
  1438. if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
  1439. seg->maxpasses = 1;
  1440. } else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
  1441. if (first) {
  1442. seg->maxpasses = 10;
  1443. } else {
  1444. seg->maxpasses = (((seg - 1)->maxpasses == 1) ||
  1445. ((seg - 1)->maxpasses == 10)) ? 2 : 1;
  1446. }
  1447. } else {
  1448. /* See paragraph "B.10.6 Number of coding passes" of the standard.
  1449. * Probably that 109 must be interpreted a (Mb-1)*3 + 1 with Mb=37,
  1450. * Mb being the maximum number of bit-planes available for the
  1451. * representation of coefficients in the sub-band */
  1452. seg->maxpasses = 109;
  1453. }
  1454. return OPJ_TRUE;
  1455. }