t2.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  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. /* SOP markers are allowed (i.e. optional), just warn */
  958. if (p_max_length < 6) {
  959. opj_event_msg(p_manager, EVT_WARNING,
  960. "Not enough space for expected SOP marker\n");
  961. } else if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
  962. opj_event_msg(p_manager, EVT_WARNING, "Expected SOP marker\n");
  963. } else {
  964. l_current_data += 6;
  965. }
  966. /** TODO : check the Nsop value */
  967. }
  968. /*
  969. When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
  970. This part deal with this characteristic
  971. step 1: Read packet header in the saved structure
  972. step 2: Return to codestream for decoding
  973. */
  974. l_bio = opj_bio_create();
  975. if (! l_bio) {
  976. return OPJ_FALSE;
  977. }
  978. if (l_cp->ppm == 1) { /* PPM */
  979. l_header_data_start = &l_cp->ppm_data;
  980. l_header_data = *l_header_data_start;
  981. l_modified_length_ptr = &(l_cp->ppm_len);
  982. } else if (p_tcp->ppt == 1) { /* PPT */
  983. l_header_data_start = &(p_tcp->ppt_data);
  984. l_header_data = *l_header_data_start;
  985. l_modified_length_ptr = &(p_tcp->ppt_len);
  986. } else { /* Normal Case */
  987. l_header_data_start = &(l_current_data);
  988. l_header_data = *l_header_data_start;
  989. l_remaining_length = (OPJ_UINT32)(p_src_data + p_max_length - l_header_data);
  990. l_modified_length_ptr = &(l_remaining_length);
  991. }
  992. opj_bio_init_dec(l_bio, l_header_data, *l_modified_length_ptr);
  993. l_present = opj_bio_read(l_bio, 1);
  994. JAS_FPRINTF(stderr, "present=%d \n", l_present);
  995. if (!l_present) {
  996. /* TODO MSD: no test to control the output of this function*/
  997. opj_bio_inalign(l_bio);
  998. l_header_data += opj_bio_numbytes(l_bio);
  999. opj_bio_destroy(l_bio);
  1000. /* EPH markers */
  1001. if (p_tcp->csty & J2K_CP_CSTY_EPH) {
  1002. /* EPH markers are required */
  1003. if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data -
  1004. *l_header_data_start)) < 2U) {
  1005. opj_event_msg(p_manager, EVT_ERROR,
  1006. "Not enough space for required EPH marker\n");
  1007. return OPJ_FALSE;
  1008. } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
  1009. opj_event_msg(p_manager, EVT_ERROR, "Expected EPH marker\n");
  1010. return OPJ_FALSE;
  1011. } else {
  1012. l_header_data += 2;
  1013. }
  1014. }
  1015. l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
  1016. *l_modified_length_ptr -= l_header_length;
  1017. *l_header_data_start += l_header_length;
  1018. /* << INDEX */
  1019. /* End of packet header position. Currently only represents the distance to start of packet
  1020. Will be updated later by incrementing with packet start value */
  1021. if (p_pack_info) {
  1022. p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
  1023. }
  1024. /* INDEX >> */
  1025. * p_is_data_present = OPJ_FALSE;
  1026. *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
  1027. return OPJ_TRUE;
  1028. }
  1029. l_band = l_res->bands;
  1030. for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band) {
  1031. opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]);
  1032. if (opj_tcd_is_band_empty(l_band)) {
  1033. continue;
  1034. }
  1035. l_nb_code_blocks = l_prc->cw * l_prc->ch;
  1036. l_cblk = l_prc->cblks.dec;
  1037. for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) {
  1038. OPJ_UINT32 l_included, l_increment, l_segno;
  1039. OPJ_INT32 n;
  1040. /* if cblk not yet included before --> inclusion tagtree */
  1041. if (!l_cblk->numsegs) {
  1042. l_included = opj_tgt_decode(l_bio, l_prc->incltree, cblkno,
  1043. (OPJ_INT32)(p_pi->layno + 1));
  1044. /* else one bit */
  1045. } else {
  1046. l_included = opj_bio_read(l_bio, 1);
  1047. }
  1048. /* if cblk not included */
  1049. if (!l_included) {
  1050. l_cblk->numnewpasses = 0;
  1051. ++l_cblk;
  1052. JAS_FPRINTF(stderr, "included=%d \n", l_included);
  1053. continue;
  1054. }
  1055. /* if cblk not yet included --> zero-bitplane tagtree */
  1056. if (!l_cblk->numsegs) {
  1057. OPJ_UINT32 i = 0;
  1058. while (!opj_tgt_decode(l_bio, l_prc->imsbtree, cblkno, (OPJ_INT32)i)) {
  1059. ++i;
  1060. }
  1061. l_cblk->Mb = (OPJ_UINT32)l_band->numbps;
  1062. if ((OPJ_UINT32)l_band->numbps + 1 < i) {
  1063. /* Not totally sure what we should do in that situation,
  1064. * but that avoids the integer overflow of
  1065. * https://github.com/uclouvain/openjpeg/pull/1488
  1066. * while keeping the regression test suite happy.
  1067. */
  1068. l_cblk->numbps = (OPJ_UINT32)(l_band->numbps + 1 - (int)i);
  1069. } else {
  1070. l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i;
  1071. }
  1072. l_cblk->numlenbits = 3;
  1073. }
  1074. /* number of coding passes */
  1075. l_cblk->numnewpasses = opj_t2_getnumpasses(l_bio);
  1076. l_increment = opj_t2_getcommacode(l_bio);
  1077. /* length indicator increment */
  1078. l_cblk->numlenbits += l_increment;
  1079. l_segno = 0;
  1080. if (!l_cblk->numsegs) {
  1081. if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
  1082. opj_bio_destroy(l_bio);
  1083. return OPJ_FALSE;
  1084. }
  1085. } else {
  1086. l_segno = l_cblk->numsegs - 1;
  1087. if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
  1088. ++l_segno;
  1089. if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
  1090. opj_bio_destroy(l_bio);
  1091. return OPJ_FALSE;
  1092. }
  1093. }
  1094. }
  1095. n = (OPJ_INT32)l_cblk->numnewpasses;
  1096. if ((p_tcp->tccps[p_pi->compno].cblksty & J2K_CCP_CBLKSTY_HT) != 0)
  1097. do {
  1098. OPJ_UINT32 bit_number;
  1099. l_cblk->segs[l_segno].numnewpasses = l_segno == 0 ? 1 : (OPJ_UINT32)n;
  1100. bit_number = l_cblk->numlenbits + opj_uint_floorlog2(
  1101. l_cblk->segs[l_segno].numnewpasses);
  1102. if (bit_number > 32) {
  1103. opj_event_msg(p_manager, EVT_ERROR,
  1104. "Invalid bit number %d in opj_t2_read_packet_header()\n",
  1105. bit_number);
  1106. opj_bio_destroy(l_bio);
  1107. return OPJ_FALSE;
  1108. }
  1109. l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, bit_number);
  1110. JAS_FPRINTF(stderr, "included=%d numnewpasses=%d increment=%d len=%d \n",
  1111. l_included, l_cblk->segs[l_segno].numnewpasses, l_increment,
  1112. l_cblk->segs[l_segno].newlen);
  1113. n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses;
  1114. if (n > 0) {
  1115. ++l_segno;
  1116. if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
  1117. opj_bio_destroy(l_bio);
  1118. return OPJ_FALSE;
  1119. }
  1120. }
  1121. } while (n > 0);
  1122. else
  1123. do {
  1124. OPJ_UINT32 bit_number;
  1125. l_cblk->segs[l_segno].numnewpasses = (OPJ_UINT32)opj_int_min((OPJ_INT32)(
  1126. l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses), n);
  1127. bit_number = l_cblk->numlenbits + opj_uint_floorlog2(
  1128. l_cblk->segs[l_segno].numnewpasses);
  1129. if (bit_number > 32) {
  1130. opj_event_msg(p_manager, EVT_ERROR,
  1131. "Invalid bit number %d in opj_t2_read_packet_header()\n",
  1132. bit_number);
  1133. opj_bio_destroy(l_bio);
  1134. return OPJ_FALSE;
  1135. }
  1136. l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, bit_number);
  1137. JAS_FPRINTF(stderr, "included=%d numnewpasses=%d increment=%d len=%d \n",
  1138. l_included, l_cblk->segs[l_segno].numnewpasses, l_increment,
  1139. l_cblk->segs[l_segno].newlen);
  1140. n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses;
  1141. if (n > 0) {
  1142. ++l_segno;
  1143. if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
  1144. opj_bio_destroy(l_bio);
  1145. return OPJ_FALSE;
  1146. }
  1147. }
  1148. } while (n > 0);
  1149. ++l_cblk;
  1150. }
  1151. }
  1152. if (!opj_bio_inalign(l_bio)) {
  1153. opj_bio_destroy(l_bio);
  1154. return OPJ_FALSE;
  1155. }
  1156. l_header_data += opj_bio_numbytes(l_bio);
  1157. opj_bio_destroy(l_bio);
  1158. /* EPH markers */
  1159. if (p_tcp->csty & J2K_CP_CSTY_EPH) {
  1160. /* EPH markers are required */
  1161. if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data -
  1162. *l_header_data_start)) < 2U) {
  1163. opj_event_msg(p_manager, EVT_ERROR,
  1164. "Not enough space for required EPH marker\n");
  1165. return OPJ_FALSE;
  1166. } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
  1167. opj_event_msg(p_manager, EVT_ERROR, "Expected EPH marker\n");
  1168. return OPJ_FALSE;
  1169. } else {
  1170. l_header_data += 2;
  1171. }
  1172. }
  1173. l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
  1174. JAS_FPRINTF(stderr, "hdrlen=%d \n", l_header_length);
  1175. if (!l_header_length) {
  1176. return OPJ_FALSE;
  1177. }
  1178. JAS_FPRINTF(stderr, "packet body\n");
  1179. *l_modified_length_ptr -= l_header_length;
  1180. *l_header_data_start += l_header_length;
  1181. /* << INDEX */
  1182. /* End of packet header position. Currently only represents the distance to start of packet
  1183. Will be updated later by incrementing with packet start value */
  1184. if (p_pack_info) {
  1185. p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
  1186. }
  1187. /* INDEX >> */
  1188. *p_is_data_present = OPJ_TRUE;
  1189. *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
  1190. return OPJ_TRUE;
  1191. }
  1192. static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
  1193. opj_tcd_tile_t *p_tile,
  1194. opj_pi_iterator_t *p_pi,
  1195. OPJ_BYTE *p_src_data,
  1196. OPJ_UINT32 * p_data_read,
  1197. OPJ_UINT32 p_max_length,
  1198. opj_packet_info_t *pack_info,
  1199. opj_event_mgr_t* p_manager)
  1200. {
  1201. OPJ_UINT32 bandno, cblkno;
  1202. OPJ_UINT32 l_nb_code_blocks;
  1203. OPJ_BYTE *l_current_data = p_src_data;
  1204. opj_tcd_band_t *l_band = 00;
  1205. opj_tcd_cblk_dec_t* l_cblk = 00;
  1206. opj_tcd_resolution_t* l_res =
  1207. &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
  1208. OPJ_BOOL partial_buffer = OPJ_FALSE;
  1209. OPJ_ARG_NOT_USED(p_t2);
  1210. OPJ_ARG_NOT_USED(pack_info);
  1211. l_band = l_res->bands;
  1212. for (bandno = 0; bandno < l_res->numbands; ++bandno) {
  1213. opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
  1214. if ((l_band->x1 - l_band->x0 == 0) || (l_band->y1 - l_band->y0 == 0)) {
  1215. ++l_band;
  1216. continue;
  1217. }
  1218. l_nb_code_blocks = l_prc->cw * l_prc->ch;
  1219. l_cblk = l_prc->cblks.dec;
  1220. for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno, ++l_cblk) {
  1221. opj_tcd_seg_t *l_seg = 00;
  1222. if (!l_cblk->numnewpasses) {
  1223. /* nothing to do */
  1224. continue;
  1225. }
  1226. if (partial_buffer || l_cblk->corrupted) {
  1227. /* if a previous segment in this packet couldn't be decoded,
  1228. * or if this code block was corrupted in a previous layer,
  1229. * then mark it as corrupted.
  1230. */
  1231. l_cblk->numchunks = 0;
  1232. l_cblk->corrupted = OPJ_TRUE;
  1233. continue;
  1234. }
  1235. if (!l_cblk->numsegs) {
  1236. l_seg = l_cblk->segs;
  1237. ++l_cblk->numsegs;
  1238. } else {
  1239. l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
  1240. if (l_seg->numpasses == l_seg->maxpasses) {
  1241. ++l_seg;
  1242. ++l_cblk->numsegs;
  1243. }
  1244. }
  1245. do {
  1246. /* Check possible overflow (on l_current_data only, assumes input args already checked) then size */
  1247. if ((((OPJ_SIZE_T)l_current_data + (OPJ_SIZE_T)l_seg->newlen) <
  1248. (OPJ_SIZE_T)l_current_data) ||
  1249. (l_current_data + l_seg->newlen > p_src_data + p_max_length) ||
  1250. (partial_buffer)) {
  1251. if (p_t2->cp->strict) {
  1252. opj_event_msg(p_manager, EVT_ERROR,
  1253. "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1254. l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
  1255. p_pi->compno);
  1256. return OPJ_FALSE;
  1257. } else {
  1258. opj_event_msg(p_manager, EVT_WARNING,
  1259. "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1260. l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
  1261. p_pi->compno);
  1262. /* skip this codeblock (and following ones in this
  1263. * packet) since it is a partial read
  1264. */
  1265. partial_buffer = OPJ_TRUE;
  1266. l_cblk->corrupted = OPJ_TRUE;
  1267. l_cblk->numchunks = 0;
  1268. break;
  1269. }
  1270. }
  1271. #ifdef USE_JPWL
  1272. /* we need here a j2k handle to verify if making a check to
  1273. the validity of cblocks parameters is selected from user (-W) */
  1274. /* let's check that we are not exceeding */
  1275. if ((l_cblk->len + l_seg->newlen) > 8192) {
  1276. opj_event_msg(p_manager, EVT_WARNING,
  1277. "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1278. l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
  1279. if (!JPWL_ASSUME) {
  1280. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  1281. return OPJ_FALSE;
  1282. }
  1283. l_seg->newlen = 8192 - l_cblk->len;
  1284. opj_event_msg(p_manager, EVT_WARNING, " - truncating segment to %d\n",
  1285. l_seg->newlen);
  1286. break;
  1287. };
  1288. #endif /* USE_JPWL */
  1289. if (l_cblk->numchunks == l_cblk->numchunksalloc) {
  1290. OPJ_UINT32 l_numchunksalloc = l_cblk->numchunksalloc * 2 + 1;
  1291. opj_tcd_seg_data_chunk_t* l_chunks =
  1292. (opj_tcd_seg_data_chunk_t*)opj_realloc(l_cblk->chunks,
  1293. l_numchunksalloc * sizeof(opj_tcd_seg_data_chunk_t));
  1294. if (l_chunks == NULL) {
  1295. opj_event_msg(p_manager, EVT_ERROR,
  1296. "cannot allocate opj_tcd_seg_data_chunk_t* array");
  1297. return OPJ_FALSE;
  1298. }
  1299. l_cblk->chunks = l_chunks;
  1300. l_cblk->numchunksalloc = l_numchunksalloc;
  1301. }
  1302. l_cblk->chunks[l_cblk->numchunks].data = l_current_data;
  1303. l_cblk->chunks[l_cblk->numchunks].len = l_seg->newlen;
  1304. l_cblk->numchunks ++;
  1305. l_current_data += l_seg->newlen;
  1306. l_seg->len += l_seg->newlen;
  1307. l_seg->numpasses += l_seg->numnewpasses;
  1308. l_cblk->numnewpasses -= l_seg->numnewpasses;
  1309. l_seg->real_num_passes = l_seg->numpasses;
  1310. if (l_cblk->numnewpasses > 0) {
  1311. ++l_seg;
  1312. ++l_cblk->numsegs;
  1313. }
  1314. } while (l_cblk->numnewpasses > 0);
  1315. l_cblk->real_num_segs = l_cblk->numsegs;
  1316. } /* next code_block */
  1317. ++l_band;
  1318. }
  1319. // return the number of bytes read
  1320. if (partial_buffer) {
  1321. *(p_data_read) = p_max_length;
  1322. } else {
  1323. *(p_data_read) = (OPJ_UINT32)(l_current_data - p_src_data);
  1324. }
  1325. return OPJ_TRUE;
  1326. }
  1327. static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2,
  1328. opj_tcd_tile_t *p_tile,
  1329. opj_pi_iterator_t *p_pi,
  1330. OPJ_UINT32 * p_data_read,
  1331. OPJ_UINT32 p_max_length,
  1332. opj_packet_info_t *pack_info,
  1333. opj_event_mgr_t *p_manager)
  1334. {
  1335. OPJ_UINT32 bandno, cblkno;
  1336. OPJ_UINT32 l_nb_code_blocks;
  1337. opj_tcd_band_t *l_band = 00;
  1338. opj_tcd_cblk_dec_t* l_cblk = 00;
  1339. opj_tcd_resolution_t* l_res =
  1340. &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
  1341. OPJ_ARG_NOT_USED(p_t2);
  1342. OPJ_ARG_NOT_USED(pack_info);
  1343. *p_data_read = 0;
  1344. l_band = l_res->bands;
  1345. for (bandno = 0; bandno < l_res->numbands; ++bandno) {
  1346. opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
  1347. if ((l_band->x1 - l_band->x0 == 0) || (l_band->y1 - l_band->y0 == 0)) {
  1348. ++l_band;
  1349. continue;
  1350. }
  1351. l_nb_code_blocks = l_prc->cw * l_prc->ch;
  1352. l_cblk = l_prc->cblks.dec;
  1353. for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
  1354. opj_tcd_seg_t *l_seg = 00;
  1355. if (!l_cblk->numnewpasses) {
  1356. /* nothing to do */
  1357. ++l_cblk;
  1358. continue;
  1359. }
  1360. if (!l_cblk->numsegs) {
  1361. l_seg = l_cblk->segs;
  1362. ++l_cblk->numsegs;
  1363. } else {
  1364. l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
  1365. if (l_seg->numpasses == l_seg->maxpasses) {
  1366. ++l_seg;
  1367. ++l_cblk->numsegs;
  1368. }
  1369. }
  1370. do {
  1371. /* Check possible overflow then size */
  1372. if (((*p_data_read + l_seg->newlen) < (*p_data_read)) ||
  1373. ((*p_data_read + l_seg->newlen) > p_max_length)) {
  1374. if (p_t2->cp->strict) {
  1375. opj_event_msg(p_manager, EVT_ERROR,
  1376. "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1377. l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
  1378. p_pi->compno);
  1379. return OPJ_FALSE;
  1380. } else {
  1381. opj_event_msg(p_manager, EVT_WARNING,
  1382. "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1383. l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
  1384. p_pi->compno);
  1385. *p_data_read = p_max_length;
  1386. return OPJ_TRUE;
  1387. }
  1388. }
  1389. #ifdef USE_JPWL
  1390. /* we need here a j2k handle to verify if making a check to
  1391. the validity of cblocks parameters is selected from user (-W) */
  1392. /* let's check that we are not exceeding */
  1393. if ((l_cblk->len + l_seg->newlen) > 8192) {
  1394. opj_event_msg(p_manager, EVT_WARNING,
  1395. "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1396. l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
  1397. if (!JPWL_ASSUME) {
  1398. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  1399. return -999;
  1400. }
  1401. l_seg->newlen = 8192 - l_cblk->len;
  1402. opj_event_msg(p_manager, EVT_WARNING, " - truncating segment to %d\n",
  1403. l_seg->newlen);
  1404. break;
  1405. };
  1406. #endif /* USE_JPWL */
  1407. JAS_FPRINTF(stderr, "p_data_read (%d) newlen (%d) \n", *p_data_read,
  1408. l_seg->newlen);
  1409. *(p_data_read) += l_seg->newlen;
  1410. l_seg->numpasses += l_seg->numnewpasses;
  1411. l_cblk->numnewpasses -= l_seg->numnewpasses;
  1412. if (l_cblk->numnewpasses > 0) {
  1413. ++l_seg;
  1414. ++l_cblk->numsegs;
  1415. }
  1416. } while (l_cblk->numnewpasses > 0);
  1417. ++l_cblk;
  1418. }
  1419. ++l_band;
  1420. }
  1421. return OPJ_TRUE;
  1422. }
  1423. static OPJ_BOOL opj_t2_init_seg(opj_tcd_cblk_dec_t* cblk,
  1424. OPJ_UINT32 index,
  1425. OPJ_UINT32 cblksty,
  1426. OPJ_UINT32 first)
  1427. {
  1428. opj_tcd_seg_t* seg = 00;
  1429. OPJ_UINT32 l_nb_segs = index + 1;
  1430. if (l_nb_segs > cblk->m_current_max_segs) {
  1431. opj_tcd_seg_t* new_segs;
  1432. OPJ_UINT32 l_m_current_max_segs = cblk->m_current_max_segs +
  1433. OPJ_J2K_DEFAULT_NB_SEGS;
  1434. new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs,
  1435. l_m_current_max_segs * sizeof(opj_tcd_seg_t));
  1436. if (! new_segs) {
  1437. /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */
  1438. return OPJ_FALSE;
  1439. }
  1440. cblk->segs = new_segs;
  1441. memset(new_segs + cblk->m_current_max_segs,
  1442. 0, OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));
  1443. cblk->m_current_max_segs = l_m_current_max_segs;
  1444. }
  1445. seg = &cblk->segs[index];
  1446. opj_tcd_reinit_segment(seg);
  1447. if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
  1448. seg->maxpasses = 1;
  1449. } else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
  1450. if (first) {
  1451. seg->maxpasses = 10;
  1452. } else {
  1453. seg->maxpasses = (((seg - 1)->maxpasses == 1) ||
  1454. ((seg - 1)->maxpasses == 10)) ? 2 : 1;
  1455. }
  1456. } else {
  1457. /* See paragraph "B.10.6 Number of coding passes" of the standard.
  1458. * Probably that 109 must be interpreted a (Mb-1)*3 + 1 with Mb=37,
  1459. * Mb being the maximum number of bit-planes available for the
  1460. * representation of coefficients in the sub-band */
  1461. seg->maxpasses = 109;
  1462. }
  1463. return OPJ_TRUE;
  1464. }