crn_prefix_coding.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // File: crn_prefix_coding.cpp
  2. // See Copyright Notice and license at the end of inc/crnlib.h
  3. #include "crn_core.h"
  4. #include "crn_prefix_coding.h"
  5. //#include "rand.h"
  6. #ifdef CRNLIB_BUILD_DEBUG
  7. //#define TEST_DECODER_TABLES
  8. #endif
  9. namespace crnlib
  10. {
  11. namespace prefix_coding
  12. {
  13. bool limit_max_code_size(uint num_syms, uint8* pCodesizes, uint max_code_size)
  14. {
  15. const uint cMaxEverCodeSize = 34;
  16. if ((!num_syms) || (num_syms > cMaxSupportedSyms) || (max_code_size < 1) || (max_code_size > cMaxEverCodeSize))
  17. return false;
  18. uint num_codes[cMaxEverCodeSize + 1];
  19. utils::zero_object(num_codes);
  20. bool should_limit = false;
  21. for (uint i = 0; i < num_syms; i++)
  22. {
  23. uint c = pCodesizes[i];
  24. if (c)
  25. {
  26. CRNLIB_ASSERT(c <= cMaxEverCodeSize);
  27. num_codes[c]++;
  28. if (c > max_code_size)
  29. should_limit = true;
  30. }
  31. }
  32. if (!should_limit)
  33. return true;
  34. uint ofs = 0;
  35. uint next_sorted_ofs[cMaxEverCodeSize + 1];
  36. for (uint i = 1; i <= cMaxEverCodeSize; i++)
  37. {
  38. next_sorted_ofs[i] = ofs;
  39. ofs += num_codes[i];
  40. }
  41. if ((ofs < 2) || (ofs > cMaxSupportedSyms))
  42. return true;
  43. if (ofs > (1U << max_code_size))
  44. return false;
  45. for (uint i = max_code_size + 1; i <= cMaxEverCodeSize; i++)
  46. num_codes[max_code_size] += num_codes[i];
  47. // Technique of adjusting tree to enforce maximum code size from LHArc.
  48. uint total = 0;
  49. for (uint i = max_code_size; i; --i)
  50. total += (num_codes[i] << (max_code_size - i));
  51. if (total == (1U << max_code_size))
  52. return true;
  53. do
  54. {
  55. num_codes[max_code_size]--;
  56. uint i;
  57. for (i = max_code_size - 1; i; --i)
  58. {
  59. if (!num_codes[i])
  60. continue;
  61. num_codes[i]--;
  62. num_codes[i + 1] += 2;
  63. break;
  64. }
  65. if (!i)
  66. return false;
  67. total--;
  68. } while (total != (1U << max_code_size));
  69. uint8 new_codesizes[cMaxSupportedSyms];
  70. uint8* p = new_codesizes;
  71. for (uint i = 1; i <= max_code_size; i++)
  72. {
  73. uint n = num_codes[i];
  74. if (n)
  75. {
  76. memset(p, i, n);
  77. p += n;
  78. }
  79. }
  80. for (uint i = 0; i < num_syms; i++)
  81. {
  82. const uint c = pCodesizes[i];
  83. if (c)
  84. {
  85. uint ofs = next_sorted_ofs[c];
  86. next_sorted_ofs[c] = ofs + 1;
  87. pCodesizes[i] = static_cast<uint8>(new_codesizes[ofs]);
  88. }
  89. }
  90. return true;
  91. }
  92. bool generate_codes(uint num_syms, const uint8* pCodesizes, uint16* pCodes)
  93. {
  94. uint num_codes[cMaxExpectedCodeSize + 1];
  95. utils::zero_object(num_codes);
  96. for (uint i = 0; i < num_syms; i++)
  97. {
  98. uint c = pCodesizes[i];
  99. if (c)
  100. {
  101. CRNLIB_ASSERT(c <= cMaxExpectedCodeSize);
  102. num_codes[c]++;
  103. }
  104. }
  105. uint code = 0;
  106. uint next_code[cMaxExpectedCodeSize + 1];
  107. next_code[0] = 0;
  108. for (uint i = 1; i <= cMaxExpectedCodeSize; i++)
  109. {
  110. next_code[i] = code;
  111. code = (code + num_codes[i]) << 1;
  112. }
  113. if (code != (1 << (cMaxExpectedCodeSize + 1)))
  114. {
  115. uint t = 0;
  116. for (uint i = 1; i <= cMaxExpectedCodeSize; i++)
  117. {
  118. t += num_codes[i];
  119. if (t > 1)
  120. return false;
  121. }
  122. }
  123. for (uint i = 0; i < num_syms; i++)
  124. {
  125. uint c = pCodesizes[i];
  126. if (c)
  127. {
  128. CRNLIB_ASSERT(next_code[c] <= cUINT16_MAX);
  129. pCodes[i] = static_cast<uint16>(next_code[c]++);
  130. CRNLIB_ASSERT(math::total_bits(pCodes[i]) <= pCodesizes[i]);
  131. }
  132. }
  133. return true;
  134. }
  135. bool generate_decoder_tables(uint num_syms, const uint8* pCodesizes, decoder_tables* pTables, uint table_bits)
  136. {
  137. uint min_codes[cMaxExpectedCodeSize];
  138. if ((!num_syms) || (table_bits > cMaxTableBits))
  139. return false;
  140. pTables->m_num_syms = num_syms;
  141. uint num_codes[cMaxExpectedCodeSize + 1];
  142. utils::zero_object(num_codes);
  143. for (uint i = 0; i < num_syms; i++)
  144. {
  145. uint c = pCodesizes[i];
  146. if (c)
  147. num_codes[c]++;
  148. }
  149. uint sorted_positions[cMaxExpectedCodeSize + 1];
  150. uint code = 0;
  151. uint total_used_syms = 0;
  152. uint max_code_size = 0;
  153. uint min_code_size = UINT_MAX;
  154. for (uint i = 1; i <= cMaxExpectedCodeSize; i++)
  155. {
  156. const uint n = num_codes[i];
  157. if (!n)
  158. pTables->m_max_codes[i - 1] = 0;//UINT_MAX;
  159. else
  160. {
  161. min_code_size = math::minimum(min_code_size, i);
  162. max_code_size = math::maximum(max_code_size, i);
  163. min_codes[i - 1] = code;
  164. pTables->m_max_codes[i - 1] = code + n - 1;
  165. pTables->m_max_codes[i - 1] = 1 + ((pTables->m_max_codes[i - 1] << (16 - i)) | ((1 << (16 - i)) - 1));
  166. pTables->m_val_ptrs[i - 1] = total_used_syms;
  167. sorted_positions[i] = total_used_syms;
  168. code += n;
  169. total_used_syms += n;
  170. }
  171. code <<= 1;
  172. }
  173. pTables->m_total_used_syms = total_used_syms;
  174. if (total_used_syms > pTables->m_cur_sorted_symbol_order_size)
  175. {
  176. pTables->m_cur_sorted_symbol_order_size = total_used_syms;
  177. if (!math::is_power_of_2(total_used_syms))
  178. pTables->m_cur_sorted_symbol_order_size = math::minimum<uint>(num_syms, math::next_pow2(total_used_syms));
  179. if (pTables->m_sorted_symbol_order)
  180. {
  181. crnlib_delete_array(pTables->m_sorted_symbol_order);
  182. pTables->m_sorted_symbol_order = NULL;
  183. }
  184. pTables->m_sorted_symbol_order = crnlib_new_array<uint16>(pTables->m_cur_sorted_symbol_order_size);
  185. }
  186. pTables->m_min_code_size = static_cast<uint8>(min_code_size);
  187. pTables->m_max_code_size = static_cast<uint8>(max_code_size);
  188. for (uint i = 0; i < num_syms; i++)
  189. {
  190. uint c = pCodesizes[i];
  191. if (c)
  192. {
  193. CRNLIB_ASSERT(num_codes[c]);
  194. uint sorted_pos = sorted_positions[c]++;
  195. CRNLIB_ASSERT(sorted_pos < total_used_syms);
  196. pTables->m_sorted_symbol_order[sorted_pos] = static_cast<uint16>(i);
  197. }
  198. }
  199. if (table_bits <= pTables->m_min_code_size)
  200. table_bits = 0;
  201. pTables->m_table_bits = table_bits;
  202. if (table_bits)
  203. {
  204. uint table_size = 1 << table_bits;
  205. if (table_size > pTables->m_cur_lookup_size)
  206. {
  207. pTables->m_cur_lookup_size = table_size;
  208. if (pTables->m_lookup)
  209. {
  210. crnlib_delete_array(pTables->m_lookup);
  211. pTables->m_lookup = NULL;
  212. }
  213. pTables->m_lookup = crnlib_new_array<uint32>(table_size);
  214. }
  215. memset(pTables->m_lookup, 0xFF, static_cast<uint>(sizeof(pTables->m_lookup[0])) * (1UL << table_bits));
  216. for (uint codesize = 1; codesize <= table_bits; codesize++)
  217. {
  218. if (!num_codes[codesize])
  219. continue;
  220. const uint fillsize = table_bits - codesize;
  221. const uint fillnum = 1 << fillsize;
  222. const uint min_code = min_codes[codesize - 1];
  223. const uint max_code = pTables->get_unshifted_max_code(codesize);
  224. const uint val_ptr = pTables->m_val_ptrs[codesize - 1];
  225. for (uint code = min_code; code <= max_code; code++)
  226. {
  227. const uint sym_index = pTables->m_sorted_symbol_order[ val_ptr + code - min_code ];
  228. CRNLIB_ASSERT( pCodesizes[sym_index] == codesize );
  229. for (uint j = 0; j < fillnum; j++)
  230. {
  231. const uint t = j + (code << fillsize);
  232. CRNLIB_ASSERT(t < (1U << table_bits));
  233. CRNLIB_ASSERT(pTables->m_lookup[t] == cUINT32_MAX);
  234. pTables->m_lookup[t] = sym_index | (codesize << 16U);
  235. }
  236. }
  237. }
  238. }
  239. for (uint i = 0; i < cMaxExpectedCodeSize; i++)
  240. pTables->m_val_ptrs[i] -= min_codes[i];
  241. pTables->m_table_max_code = 0;
  242. pTables->m_decode_start_code_size = pTables->m_min_code_size;
  243. if (table_bits)
  244. {
  245. uint i;
  246. for (i = table_bits; i >= 1; i--)
  247. {
  248. if (num_codes[i])
  249. {
  250. pTables->m_table_max_code = pTables->m_max_codes[i - 1];
  251. break;
  252. }
  253. }
  254. if (i >= 1)
  255. {
  256. pTables->m_decode_start_code_size = table_bits + 1;
  257. for (uint i = table_bits + 1; i <= max_code_size; i++)
  258. {
  259. if (num_codes[i])
  260. {
  261. pTables->m_decode_start_code_size = i;
  262. break;
  263. }
  264. }
  265. }
  266. }
  267. // sentinels
  268. pTables->m_max_codes[cMaxExpectedCodeSize] = UINT_MAX;
  269. pTables->m_val_ptrs[cMaxExpectedCodeSize] = 0xFFFFF;
  270. pTables->m_table_shift = 32 - pTables->m_table_bits;
  271. return true;
  272. }
  273. } // namespace prefix_codig
  274. } // namespace crnlib