tgt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions
  20. * are met:
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * 2. Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in the
  25. * documentation and/or other materials provided with the distribution.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #include "opj_includes.h"
  40. /*
  41. ==========================================================
  42. Tag-tree coder interface
  43. ==========================================================
  44. */
  45. opj_tgt_tree_t *opj_tgt_create(OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv,
  46. opj_event_mgr_t *p_manager)
  47. {
  48. OPJ_INT32 nplh[32];
  49. OPJ_INT32 nplv[32];
  50. opj_tgt_node_t *node = 00;
  51. opj_tgt_node_t *l_parent_node = 00;
  52. opj_tgt_node_t *l_parent_node0 = 00;
  53. opj_tgt_tree_t *tree = 00;
  54. OPJ_UINT32 i;
  55. OPJ_INT32 j, k;
  56. OPJ_UINT32 numlvls;
  57. OPJ_UINT32 n;
  58. tree = (opj_tgt_tree_t *) opj_calloc(1, sizeof(opj_tgt_tree_t));
  59. if (!tree) {
  60. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tag-tree\n");
  61. return 00;
  62. }
  63. tree->numleafsh = numleafsh;
  64. tree->numleafsv = numleafsv;
  65. numlvls = 0;
  66. nplh[0] = (OPJ_INT32)numleafsh;
  67. nplv[0] = (OPJ_INT32)numleafsv;
  68. tree->numnodes = 0;
  69. do {
  70. n = (OPJ_UINT32)(nplh[numlvls] * nplv[numlvls]);
  71. nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;
  72. nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;
  73. tree->numnodes += n;
  74. ++numlvls;
  75. } while (n > 1);
  76. /* ADD */
  77. if (tree->numnodes == 0) {
  78. opj_free(tree);
  79. return 00;
  80. }
  81. tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes,
  82. sizeof(opj_tgt_node_t));
  83. if (!tree->nodes) {
  84. opj_event_msg(p_manager, EVT_ERROR,
  85. "Not enough memory to create Tag-tree nodes\n");
  86. opj_free(tree);
  87. return 00;
  88. }
  89. tree->nodes_size = tree->numnodes * (OPJ_UINT32)sizeof(opj_tgt_node_t);
  90. node = tree->nodes;
  91. l_parent_node = &tree->nodes[tree->numleafsh * tree->numleafsv];
  92. l_parent_node0 = l_parent_node;
  93. for (i = 0; i < numlvls - 1; ++i) {
  94. for (j = 0; j < nplv[i]; ++j) {
  95. k = nplh[i];
  96. while (--k >= 0) {
  97. node->parent = l_parent_node;
  98. ++node;
  99. if (--k >= 0) {
  100. node->parent = l_parent_node;
  101. ++node;
  102. }
  103. ++l_parent_node;
  104. }
  105. if ((j & 1) || j == nplv[i] - 1) {
  106. l_parent_node0 = l_parent_node;
  107. } else {
  108. l_parent_node = l_parent_node0;
  109. l_parent_node0 += nplh[i];
  110. }
  111. }
  112. }
  113. node->parent = 0;
  114. opj_tgt_reset(tree);
  115. return tree;
  116. }
  117. /**
  118. * Reinitialises a tag-tree from an existing one.
  119. *
  120. * @param p_tree the tree to reinitialize.
  121. * @param p_num_leafs_h the width of the array of leafs of the tree
  122. * @param p_num_leafs_v the height of the array of leafs of the tree
  123. * @return a new tag-tree if successful, NULL otherwise
  124. */
  125. opj_tgt_tree_t *opj_tgt_init(opj_tgt_tree_t * p_tree, OPJ_UINT32 p_num_leafs_h,
  126. OPJ_UINT32 p_num_leafs_v, opj_event_mgr_t *p_manager)
  127. {
  128. OPJ_INT32 l_nplh[32];
  129. OPJ_INT32 l_nplv[32];
  130. opj_tgt_node_t *l_node = 00;
  131. opj_tgt_node_t *l_parent_node = 00;
  132. opj_tgt_node_t *l_parent_node0 = 00;
  133. OPJ_UINT32 i;
  134. OPJ_INT32 j, k;
  135. OPJ_UINT32 l_num_levels;
  136. OPJ_UINT32 n;
  137. OPJ_UINT32 l_node_size;
  138. if (! p_tree) {
  139. return 00;
  140. }
  141. if ((p_tree->numleafsh != p_num_leafs_h) ||
  142. (p_tree->numleafsv != p_num_leafs_v)) {
  143. p_tree->numleafsh = p_num_leafs_h;
  144. p_tree->numleafsv = p_num_leafs_v;
  145. l_num_levels = 0;
  146. l_nplh[0] = (OPJ_INT32)p_num_leafs_h;
  147. l_nplv[0] = (OPJ_INT32)p_num_leafs_v;
  148. p_tree->numnodes = 0;
  149. do {
  150. n = (OPJ_UINT32)(l_nplh[l_num_levels] * l_nplv[l_num_levels]);
  151. l_nplh[l_num_levels + 1] = (l_nplh[l_num_levels] + 1) / 2;
  152. l_nplv[l_num_levels + 1] = (l_nplv[l_num_levels] + 1) / 2;
  153. p_tree->numnodes += n;
  154. ++l_num_levels;
  155. } while (n > 1);
  156. /* ADD */
  157. if (p_tree->numnodes == 0) {
  158. opj_tgt_destroy(p_tree);
  159. return 00;
  160. }
  161. l_node_size = p_tree->numnodes * (OPJ_UINT32)sizeof(opj_tgt_node_t);
  162. if (l_node_size > p_tree->nodes_size) {
  163. opj_tgt_node_t* new_nodes = (opj_tgt_node_t*) opj_realloc(p_tree->nodes,
  164. l_node_size);
  165. if (! new_nodes) {
  166. opj_event_msg(p_manager, EVT_ERROR,
  167. "Not enough memory to reinitialize the tag tree\n");
  168. opj_tgt_destroy(p_tree);
  169. return 00;
  170. }
  171. p_tree->nodes = new_nodes;
  172. memset(((char *) p_tree->nodes) + p_tree->nodes_size, 0,
  173. l_node_size - p_tree->nodes_size);
  174. p_tree->nodes_size = l_node_size;
  175. }
  176. l_node = p_tree->nodes;
  177. l_parent_node = &p_tree->nodes[p_tree->numleafsh * p_tree->numleafsv];
  178. l_parent_node0 = l_parent_node;
  179. for (i = 0; i < l_num_levels - 1; ++i) {
  180. for (j = 0; j < l_nplv[i]; ++j) {
  181. k = l_nplh[i];
  182. while (--k >= 0) {
  183. l_node->parent = l_parent_node;
  184. ++l_node;
  185. if (--k >= 0) {
  186. l_node->parent = l_parent_node;
  187. ++l_node;
  188. }
  189. ++l_parent_node;
  190. }
  191. if ((j & 1) || j == l_nplv[i] - 1) {
  192. l_parent_node0 = l_parent_node;
  193. } else {
  194. l_parent_node = l_parent_node0;
  195. l_parent_node0 += l_nplh[i];
  196. }
  197. }
  198. }
  199. l_node->parent = 0;
  200. }
  201. opj_tgt_reset(p_tree);
  202. return p_tree;
  203. }
  204. void opj_tgt_destroy(opj_tgt_tree_t *p_tree)
  205. {
  206. if (! p_tree) {
  207. return;
  208. }
  209. if (p_tree->nodes) {
  210. opj_free(p_tree->nodes);
  211. p_tree->nodes = 00;
  212. }
  213. opj_free(p_tree);
  214. }
  215. void opj_tgt_reset(opj_tgt_tree_t *p_tree)
  216. {
  217. OPJ_UINT32 i;
  218. opj_tgt_node_t * l_current_node = 00;;
  219. if (! p_tree) {
  220. return;
  221. }
  222. l_current_node = p_tree->nodes;
  223. for (i = 0; i < p_tree->numnodes; ++i) {
  224. l_current_node->value = 999;
  225. l_current_node->low = 0;
  226. l_current_node->known = 0;
  227. ++l_current_node;
  228. }
  229. }
  230. void opj_tgt_setvalue(opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 value)
  231. {
  232. opj_tgt_node_t *node;
  233. node = &tree->nodes[leafno];
  234. while (node && node->value > value) {
  235. node->value = value;
  236. node = node->parent;
  237. }
  238. }
  239. void opj_tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno,
  240. OPJ_INT32 threshold)
  241. {
  242. opj_tgt_node_t *stk[31];
  243. opj_tgt_node_t **stkptr;
  244. opj_tgt_node_t *node;
  245. OPJ_INT32 low;
  246. stkptr = stk;
  247. node = &tree->nodes[leafno];
  248. while (node->parent) {
  249. *stkptr++ = node;
  250. node = node->parent;
  251. }
  252. low = 0;
  253. for (;;) {
  254. if (low > node->low) {
  255. node->low = low;
  256. } else {
  257. low = node->low;
  258. }
  259. while (low < threshold) {
  260. if (low >= node->value) {
  261. if (!node->known) {
  262. opj_bio_write(bio, 1, 1);
  263. node->known = 1;
  264. }
  265. break;
  266. }
  267. opj_bio_write(bio, 0, 1);
  268. ++low;
  269. }
  270. node->low = low;
  271. if (stkptr == stk) {
  272. break;
  273. }
  274. node = *--stkptr;
  275. }
  276. }
  277. OPJ_UINT32 opj_tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree,
  278. OPJ_UINT32 leafno, OPJ_INT32 threshold)
  279. {
  280. opj_tgt_node_t *stk[31];
  281. opj_tgt_node_t **stkptr;
  282. opj_tgt_node_t *node;
  283. OPJ_INT32 low;
  284. stkptr = stk;
  285. node = &tree->nodes[leafno];
  286. while (node->parent) {
  287. *stkptr++ = node;
  288. node = node->parent;
  289. }
  290. low = 0;
  291. for (;;) {
  292. if (low > node->low) {
  293. node->low = low;
  294. } else {
  295. low = node->low;
  296. }
  297. while (low < threshold && low < node->value) {
  298. if (opj_bio_read(bio, 1)) {
  299. node->value = low;
  300. } else {
  301. ++low;
  302. }
  303. }
  304. node->low = low;
  305. if (stkptr == stk) {
  306. break;
  307. }
  308. node = *--stkptr;
  309. }
  310. return (node->value < threshold) ? 1 : 0;
  311. }