t2.c 62 KB

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