t2.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  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 caracteristic
  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->numbps = (OPJ_UINT32)l_band->numbps + 1 - i;
  1056. l_cblk->numlenbits = 3;
  1057. }
  1058. /* number of coding passes */
  1059. l_cblk->numnewpasses = opj_t2_getnumpasses(l_bio);
  1060. l_increment = opj_t2_getcommacode(l_bio);
  1061. /* length indicator increment */
  1062. l_cblk->numlenbits += l_increment;
  1063. l_segno = 0;
  1064. if (!l_cblk->numsegs) {
  1065. if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
  1066. opj_bio_destroy(l_bio);
  1067. return OPJ_FALSE;
  1068. }
  1069. } else {
  1070. l_segno = l_cblk->numsegs - 1;
  1071. if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
  1072. ++l_segno;
  1073. if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
  1074. opj_bio_destroy(l_bio);
  1075. return OPJ_FALSE;
  1076. }
  1077. }
  1078. }
  1079. n = (OPJ_INT32)l_cblk->numnewpasses;
  1080. do {
  1081. OPJ_UINT32 bit_number;
  1082. l_cblk->segs[l_segno].numnewpasses = (OPJ_UINT32)opj_int_min((OPJ_INT32)(
  1083. l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses), n);
  1084. bit_number = l_cblk->numlenbits + opj_uint_floorlog2(
  1085. l_cblk->segs[l_segno].numnewpasses);
  1086. if (bit_number > 32) {
  1087. opj_event_msg(p_manager, EVT_ERROR,
  1088. "Invalid bit number %d in opj_t2_read_packet_header()\n",
  1089. bit_number);
  1090. opj_bio_destroy(l_bio);
  1091. return OPJ_FALSE;
  1092. }
  1093. l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, bit_number);
  1094. JAS_FPRINTF(stderr, "included=%d numnewpasses=%d increment=%d len=%d \n",
  1095. l_included, l_cblk->segs[l_segno].numnewpasses, l_increment,
  1096. l_cblk->segs[l_segno].newlen);
  1097. n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses;
  1098. if (n > 0) {
  1099. ++l_segno;
  1100. if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
  1101. opj_bio_destroy(l_bio);
  1102. return OPJ_FALSE;
  1103. }
  1104. }
  1105. } while (n > 0);
  1106. ++l_cblk;
  1107. }
  1108. }
  1109. if (!opj_bio_inalign(l_bio)) {
  1110. opj_bio_destroy(l_bio);
  1111. return OPJ_FALSE;
  1112. }
  1113. l_header_data += opj_bio_numbytes(l_bio);
  1114. opj_bio_destroy(l_bio);
  1115. /* EPH markers */
  1116. if (p_tcp->csty & J2K_CP_CSTY_EPH) {
  1117. if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data -
  1118. *l_header_data_start)) < 2U) {
  1119. opj_event_msg(p_manager, EVT_WARNING,
  1120. "Not enough space for expected EPH marker\n");
  1121. } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
  1122. opj_event_msg(p_manager, EVT_WARNING, "Expected EPH marker\n");
  1123. } else {
  1124. l_header_data += 2;
  1125. }
  1126. }
  1127. l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
  1128. JAS_FPRINTF(stderr, "hdrlen=%d \n", l_header_length);
  1129. JAS_FPRINTF(stderr, "packet body\n");
  1130. *l_modified_length_ptr -= l_header_length;
  1131. *l_header_data_start += l_header_length;
  1132. /* << INDEX */
  1133. /* End of packet header position. Currently only represents the distance to start of packet
  1134. Will be updated later by incrementing with packet start value */
  1135. if (p_pack_info) {
  1136. p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
  1137. }
  1138. /* INDEX >> */
  1139. *p_is_data_present = OPJ_TRUE;
  1140. *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
  1141. return OPJ_TRUE;
  1142. }
  1143. static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
  1144. opj_tcd_tile_t *p_tile,
  1145. opj_pi_iterator_t *p_pi,
  1146. OPJ_BYTE *p_src_data,
  1147. OPJ_UINT32 * p_data_read,
  1148. OPJ_UINT32 p_max_length,
  1149. opj_packet_info_t *pack_info,
  1150. opj_event_mgr_t* p_manager)
  1151. {
  1152. OPJ_UINT32 bandno, cblkno;
  1153. OPJ_UINT32 l_nb_code_blocks;
  1154. OPJ_BYTE *l_current_data = p_src_data;
  1155. opj_tcd_band_t *l_band = 00;
  1156. opj_tcd_cblk_dec_t* l_cblk = 00;
  1157. opj_tcd_resolution_t* l_res =
  1158. &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
  1159. OPJ_ARG_NOT_USED(p_t2);
  1160. OPJ_ARG_NOT_USED(pack_info);
  1161. l_band = l_res->bands;
  1162. for (bandno = 0; bandno < l_res->numbands; ++bandno) {
  1163. opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
  1164. if ((l_band->x1 - l_band->x0 == 0) || (l_band->y1 - l_band->y0 == 0)) {
  1165. ++l_band;
  1166. continue;
  1167. }
  1168. l_nb_code_blocks = l_prc->cw * l_prc->ch;
  1169. l_cblk = l_prc->cblks.dec;
  1170. for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
  1171. opj_tcd_seg_t *l_seg = 00;
  1172. if (!l_cblk->numnewpasses) {
  1173. /* nothing to do */
  1174. ++l_cblk;
  1175. continue;
  1176. }
  1177. if (!l_cblk->numsegs) {
  1178. l_seg = l_cblk->segs;
  1179. ++l_cblk->numsegs;
  1180. } else {
  1181. l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
  1182. if (l_seg->numpasses == l_seg->maxpasses) {
  1183. ++l_seg;
  1184. ++l_cblk->numsegs;
  1185. }
  1186. }
  1187. do {
  1188. /* Check possible overflow (on l_current_data only, assumes input args already checked) then size */
  1189. if ((((OPJ_SIZE_T)l_current_data + (OPJ_SIZE_T)l_seg->newlen) <
  1190. (OPJ_SIZE_T)l_current_data) ||
  1191. (l_current_data + l_seg->newlen > p_src_data + p_max_length)) {
  1192. opj_event_msg(p_manager, EVT_ERROR,
  1193. "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1194. l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
  1195. p_pi->compno);
  1196. return OPJ_FALSE;
  1197. }
  1198. #ifdef USE_JPWL
  1199. /* we need here a j2k handle to verify if making a check to
  1200. the validity of cblocks parameters is selected from user (-W) */
  1201. /* let's check that we are not exceeding */
  1202. if ((l_cblk->len + l_seg->newlen) > 8192) {
  1203. opj_event_msg(p_manager, EVT_WARNING,
  1204. "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1205. l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
  1206. if (!JPWL_ASSUME) {
  1207. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  1208. return OPJ_FALSE;
  1209. }
  1210. l_seg->newlen = 8192 - l_cblk->len;
  1211. opj_event_msg(p_manager, EVT_WARNING, " - truncating segment to %d\n",
  1212. l_seg->newlen);
  1213. break;
  1214. };
  1215. #endif /* USE_JPWL */
  1216. if (l_cblk->numchunks == l_cblk->numchunksalloc) {
  1217. OPJ_UINT32 l_numchunksalloc = l_cblk->numchunksalloc * 2 + 1;
  1218. opj_tcd_seg_data_chunk_t* l_chunks =
  1219. (opj_tcd_seg_data_chunk_t*)opj_realloc(l_cblk->chunks,
  1220. l_numchunksalloc * sizeof(opj_tcd_seg_data_chunk_t));
  1221. if (l_chunks == NULL) {
  1222. opj_event_msg(p_manager, EVT_ERROR,
  1223. "cannot allocate opj_tcd_seg_data_chunk_t* array");
  1224. return OPJ_FALSE;
  1225. }
  1226. l_cblk->chunks = l_chunks;
  1227. l_cblk->numchunksalloc = l_numchunksalloc;
  1228. }
  1229. l_cblk->chunks[l_cblk->numchunks].data = l_current_data;
  1230. l_cblk->chunks[l_cblk->numchunks].len = l_seg->newlen;
  1231. l_cblk->numchunks ++;
  1232. l_current_data += l_seg->newlen;
  1233. l_seg->len += l_seg->newlen;
  1234. l_seg->numpasses += l_seg->numnewpasses;
  1235. l_cblk->numnewpasses -= l_seg->numnewpasses;
  1236. l_seg->real_num_passes = l_seg->numpasses;
  1237. if (l_cblk->numnewpasses > 0) {
  1238. ++l_seg;
  1239. ++l_cblk->numsegs;
  1240. }
  1241. } while (l_cblk->numnewpasses > 0);
  1242. l_cblk->real_num_segs = l_cblk->numsegs;
  1243. ++l_cblk;
  1244. } /* next code_block */
  1245. ++l_band;
  1246. }
  1247. *(p_data_read) = (OPJ_UINT32)(l_current_data - p_src_data);
  1248. return OPJ_TRUE;
  1249. }
  1250. static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2,
  1251. opj_tcd_tile_t *p_tile,
  1252. opj_pi_iterator_t *p_pi,
  1253. OPJ_UINT32 * p_data_read,
  1254. OPJ_UINT32 p_max_length,
  1255. opj_packet_info_t *pack_info,
  1256. opj_event_mgr_t *p_manager)
  1257. {
  1258. OPJ_UINT32 bandno, cblkno;
  1259. OPJ_UINT32 l_nb_code_blocks;
  1260. opj_tcd_band_t *l_band = 00;
  1261. opj_tcd_cblk_dec_t* l_cblk = 00;
  1262. opj_tcd_resolution_t* l_res =
  1263. &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
  1264. OPJ_ARG_NOT_USED(p_t2);
  1265. OPJ_ARG_NOT_USED(pack_info);
  1266. *p_data_read = 0;
  1267. l_band = l_res->bands;
  1268. for (bandno = 0; bandno < l_res->numbands; ++bandno) {
  1269. opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
  1270. if ((l_band->x1 - l_band->x0 == 0) || (l_band->y1 - l_band->y0 == 0)) {
  1271. ++l_band;
  1272. continue;
  1273. }
  1274. l_nb_code_blocks = l_prc->cw * l_prc->ch;
  1275. l_cblk = l_prc->cblks.dec;
  1276. for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
  1277. opj_tcd_seg_t *l_seg = 00;
  1278. if (!l_cblk->numnewpasses) {
  1279. /* nothing to do */
  1280. ++l_cblk;
  1281. continue;
  1282. }
  1283. if (!l_cblk->numsegs) {
  1284. l_seg = l_cblk->segs;
  1285. ++l_cblk->numsegs;
  1286. } else {
  1287. l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
  1288. if (l_seg->numpasses == l_seg->maxpasses) {
  1289. ++l_seg;
  1290. ++l_cblk->numsegs;
  1291. }
  1292. }
  1293. do {
  1294. /* Check possible overflow then size */
  1295. if (((*p_data_read + l_seg->newlen) < (*p_data_read)) ||
  1296. ((*p_data_read + l_seg->newlen) > p_max_length)) {
  1297. opj_event_msg(p_manager, EVT_ERROR,
  1298. "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1299. l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
  1300. p_pi->compno);
  1301. return OPJ_FALSE;
  1302. }
  1303. #ifdef USE_JPWL
  1304. /* we need here a j2k handle to verify if making a check to
  1305. the validity of cblocks parameters is selected from user (-W) */
  1306. /* let's check that we are not exceeding */
  1307. if ((l_cblk->len + l_seg->newlen) > 8192) {
  1308. opj_event_msg(p_manager, EVT_WARNING,
  1309. "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
  1310. l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
  1311. if (!JPWL_ASSUME) {
  1312. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  1313. return -999;
  1314. }
  1315. l_seg->newlen = 8192 - l_cblk->len;
  1316. opj_event_msg(p_manager, EVT_WARNING, " - truncating segment to %d\n",
  1317. l_seg->newlen);
  1318. break;
  1319. };
  1320. #endif /* USE_JPWL */
  1321. JAS_FPRINTF(stderr, "p_data_read (%d) newlen (%d) \n", *p_data_read,
  1322. l_seg->newlen);
  1323. *(p_data_read) += l_seg->newlen;
  1324. l_seg->numpasses += l_seg->numnewpasses;
  1325. l_cblk->numnewpasses -= l_seg->numnewpasses;
  1326. if (l_cblk->numnewpasses > 0) {
  1327. ++l_seg;
  1328. ++l_cblk->numsegs;
  1329. }
  1330. } while (l_cblk->numnewpasses > 0);
  1331. ++l_cblk;
  1332. }
  1333. ++l_band;
  1334. }
  1335. return OPJ_TRUE;
  1336. }
  1337. static OPJ_BOOL opj_t2_init_seg(opj_tcd_cblk_dec_t* cblk,
  1338. OPJ_UINT32 index,
  1339. OPJ_UINT32 cblksty,
  1340. OPJ_UINT32 first)
  1341. {
  1342. opj_tcd_seg_t* seg = 00;
  1343. OPJ_UINT32 l_nb_segs = index + 1;
  1344. if (l_nb_segs > cblk->m_current_max_segs) {
  1345. opj_tcd_seg_t* new_segs;
  1346. OPJ_UINT32 l_m_current_max_segs = cblk->m_current_max_segs +
  1347. OPJ_J2K_DEFAULT_NB_SEGS;
  1348. new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs,
  1349. l_m_current_max_segs * sizeof(opj_tcd_seg_t));
  1350. if (! new_segs) {
  1351. /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */
  1352. return OPJ_FALSE;
  1353. }
  1354. cblk->segs = new_segs;
  1355. memset(new_segs + cblk->m_current_max_segs,
  1356. 0, OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));
  1357. cblk->m_current_max_segs = l_m_current_max_segs;
  1358. }
  1359. seg = &cblk->segs[index];
  1360. opj_tcd_reinit_segment(seg);
  1361. if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
  1362. seg->maxpasses = 1;
  1363. } else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
  1364. if (first) {
  1365. seg->maxpasses = 10;
  1366. } else {
  1367. seg->maxpasses = (((seg - 1)->maxpasses == 1) ||
  1368. ((seg - 1)->maxpasses == 10)) ? 2 : 1;
  1369. }
  1370. } else {
  1371. /* See paragraph "B.10.6 Number of coding passes" of the standard.
  1372. * Probably that 109 must be interpreted a (Mb-1)*3 + 1 with Mb=37,
  1373. * Mb being the maximum number of bit-planes available for the
  1374. * representation of coefficients in the sub-band */
  1375. seg->maxpasses = 109;
  1376. }
  1377. return OPJ_TRUE;
  1378. }