adapthuff.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. //*@@@+++@@@@******************************************************************
  2. //
  3. // Copyright © Microsoft Corp.
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are met:
  8. //
  9. // • Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. // • Redistributions in binary form must reproduce the above copyright notice,
  12. // this list of conditions and the following disclaimer in the documentation
  13. // and/or other materials provided with the distribution.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. // POSSIBILITY OF SUCH DAMAGE.
  26. //
  27. //*@@@---@@@@******************************************************************
  28. #include "strcodec.h"
  29. #ifdef MEM_TRACE
  30. #define TRACE_MALLOC 1
  31. #define TRACE_NEW 0
  32. #define TRACE_HEAP 0
  33. #include "memtrace.h"
  34. #endif
  35. // Huffman lookup tables
  36. static const short g4HuffLookupTable[40] = {
  37. 19,19,19,19,27,27,27,27,10,10,10,10,10,10,10,10,
  38. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  39. 0,0,0,0,0,0,0,0 };
  40. static const short g5HuffLookupTable[2][42] = {{
  41. 28,28,36,36,19,19,19,19,10,10,10,10,10,10,10,10,
  42. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  43. 0,0,0,0,0,0,0,0,0,0 },
  44. {
  45. 11,11,11,11,19,19,19,19,27,27,27,27,35,35,35,35,
  46. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  47. 0,0,0,0,0,0,0,0,0,0 }};
  48. static const short g6HuffLookupTable[4][44] = {{
  49. 13,29,44,44,19,19,19,19,34,34,34,34,34,34,34,34,
  50. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  51. 0,0,0,0,0,0,0,0,0,0,0,0 },
  52. {
  53. 12,12,28,28,43,43,43,43,2,2,2,2,2,2,2,2,
  54. 18,18,18,18,18,18,18,18,34,34,34,34,34,34,34,34,
  55. 0,0,0,0,0,0,0,0,0,0,0,0 },
  56. {
  57. 4,4,12,12,43,43,43,43,18,18,18,18,18,18,18,18,
  58. 26,26,26,26,26,26,26,26,34,34,34,34,34,34,34,34,
  59. 0,0,0,0,0,0,0,0,0,0,0,0 },
  60. {
  61. 5,13,36,36,43,43,43,43,18,18,18,18,18,18,18,18,
  62. 25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,
  63. 0,0,0,0,0,0,0,0,0,0,0,0 }};
  64. static const short g7HuffLookupTable[2][46] = {{
  65. 45,53,36,36,27,27,27,27,2,2,2,2,2,2,2,2,
  66. 10,10,10,10,10,10,10,10,18,18,18,18,18,18,18,18,
  67. 0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  68. {
  69. -32736,37,28,28,19,19,19,19,10,10,10,10,10,10,10,10,
  70. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  71. 5,6,0,0,0,0,0,0,0,0,0,0,0,0 }};
  72. static const short g8HuffLookupTable[2][48] = {{
  73. 53,21,28,28,11,11,11,11,43,43,43,43,59,59,59,59,
  74. 2,2,2,2,2,2,2,2,34,34,34,34,34,34,34,34,
  75. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  76. {
  77. 52,52,20,20,3,3,3,3,11,11,11,11,27,27,27,27,
  78. 35,35,35,35,43,43,43,43,58,58,58,58,58,58,58,58,
  79. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }};
  80. static const short g9HuffLookupTable[2][50] = {{
  81. 13,29,37,61,20,20,68,68,3,3,3,3,51,51,51,51,
  82. 41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,
  83. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  84. 0,0 },
  85. {
  86. -32736,53,28,28,11,11,11,11,19,19,19,19,43,43,43,43,
  87. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  88. -32734,4,7,8,0,0,0,0,0,0,0,0,0,0,0,0,
  89. 0,0 }};
  90. static const short g12HuffLookupTable[5][56] = {{
  91. -32736,5,76,76,37,53,69,85,43,43,43,43,91,91,91,91,
  92. 57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,
  93. -32734,1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,
  94. 0,0,0,0,0,0,0,0 },
  95. {
  96. -32736,85,13,53,4,4,36,36,43,43,43,43,67,67,67,67,
  97. 75,75,75,75,91,91,91,91,58,58,58,58,58,58,58,58,
  98. 2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  99. 0,0,0,0,0,0,0,0 },
  100. {
  101. -32736,37,92,92,11,11,11,11,43,43,43,43,59,59,59,59,
  102. 67,67,67,67,75,75,75,75,2,2,2,2,2,2,2,2,
  103. -32734,-32732,2,3,6,10,0,0,0,0,0,0,0,0,0,0,
  104. 0,0,0,0,0,0,0,0 },
  105. {
  106. -32736,29,37,69,3,3,3,3,43,43,43,43,59,59,59,59,
  107. 75,75,75,75,91,91,91,91,10,10,10,10,10,10,10,10,
  108. -32734,10,2,6,0,0,0,0,0,0,0,0,0,0,0,0,
  109. 0,0,0,0,0,0,0,0 },
  110. {
  111. -32736,93,28,28,60,60,76,76,3,3,3,3,43,43,43,43,
  112. 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  113. -32734,-32732,-32730,2,4,8,6,10,0,0,0,0,0,0,0,0,
  114. 0,0,0,0,0,0,0,0 }};
  115. /**********************************************************************
  116. Allocation and dellocation
  117. **********************************************************************/
  118. Void Clean (CAdaptiveHuffman *pAdHuff)
  119. {
  120. if (pAdHuff == NULL)
  121. return;
  122. free (pAdHuff);
  123. }
  124. CAdaptiveHuffman *Allocate (Int iNSymbols, CODINGMODE cm)
  125. {
  126. CAdaptiveHuffman *pAdHuff = (CAdaptiveHuffman *) malloc (sizeof (CAdaptiveHuffman));
  127. UNREFERENCED_PARAMETER(cm);
  128. if (pAdHuff == NULL)
  129. return NULL;
  130. if (iNSymbols > 255 || iNSymbols <= 0)
  131. goto ErrorExit;
  132. memset (pAdHuff, 0, sizeof (CAdaptiveHuffman));
  133. pAdHuff->m_iNSymbols = iNSymbols;
  134. pAdHuff->m_pDelta = NULL;
  135. pAdHuff->m_iDiscriminant = pAdHuff->m_iUpperBound = pAdHuff->m_iLowerBound = 0;
  136. return pAdHuff;
  137. ErrorExit:
  138. Clean (pAdHuff);
  139. return NULL;
  140. }
  141. /**********************************************************************
  142. Adapt Huffman table
  143. **********************************************************************/
  144. // Alphabet size = 4
  145. static const Int g_Index4Table[] = {
  146. 1,2,3,3
  147. };
  148. static const Int g4CodeTable[] = {
  149. 4,
  150. 1, 1,
  151. 1, 2,
  152. 0, 3,
  153. 1, 3
  154. };
  155. // Alphabet size = 5
  156. static const Int g_Index5Table[] = {
  157. 1,2,3,4,4,
  158. 1,3,3,3,3
  159. };
  160. static const Int g5CodeTable[] = {
  161. 5,
  162. 1, 1,
  163. 1, 2,
  164. 1, 3,
  165. 0, 4,
  166. 1, 4,
  167. 5,
  168. 1, 1,
  169. 0, 3,
  170. 1, 3,
  171. 2, 3,
  172. 3, 3,
  173. };
  174. static const Int g5DeltaTable[] = { 0,-1,0,1,1 };
  175. // Alphabet size = 6
  176. static const Int g_Index6Table[] = {
  177. 1,5,3,5,2,4,
  178. 2,4,2,4,2,3,
  179. 4,4,2,2,2,3,
  180. 5,5,2,1,4,3,
  181. };
  182. static const Int g6CodeTable[] = {
  183. 6,
  184. 1, 1,
  185. 0, 5,
  186. 1, 3,
  187. 1, 5,
  188. 1, 2,
  189. 1, 4,
  190. 6,
  191. 1, 2,
  192. 0, 4,
  193. 2, 2,
  194. 1, 4,
  195. 3, 2,
  196. 1, 3,
  197. 6,
  198. 0, 4,
  199. 1, 4,
  200. 1, 2,
  201. 2, 2,
  202. 3, 2,
  203. 1, 3,
  204. 6,
  205. 0, 5,
  206. 1, 5,
  207. 1, 2,
  208. 1, 1,
  209. 1, 4,
  210. 1, 3
  211. };
  212. static const Int g6DeltaTable[] = {
  213. -1, 1, 1, 1, 0, 1,
  214. -2, 0, 0, 2, 0, 0,
  215. -1,-1, 0, 1,-2, 0
  216. };
  217. // Alphabet size = 7
  218. static const Int g_Index7Table[] = { 2,2,2,3,4,5,5,
  219. 1,2,3,4,5,6,6 };
  220. static const Int g7CodeTable[] = {
  221. 7,
  222. 1, 2,
  223. 2, 2,
  224. 3, 2,
  225. 1, 3,
  226. 1, 4,
  227. 0, 5,
  228. 1, 5,
  229. 7,
  230. 1, 1,
  231. 1, 2,
  232. 1, 3,
  233. 1, 4,
  234. 1, 5,
  235. 0, 6,
  236. 1, 6
  237. };
  238. static const Int g7DeltaTable[] = { 1,0,-1,-1,-1,-1,-1 };
  239. // Alphabet size = 8
  240. static const Int g_Index8Table[] = { 2,3,5,4,2,3,5,3,
  241. 3,3,4,3,3,3,4,2};
  242. static const Int g8CodeTable[] = {
  243. 8,
  244. 2, 2,
  245. 1, 3,
  246. 1, 5,
  247. 1, 4,
  248. 3, 2,
  249. 2, 3,
  250. 0, 5,
  251. 3, 3,
  252. 8,
  253. 1, 3,
  254. 2, 3,
  255. 1, 4,
  256. 3, 3,
  257. 4, 3,
  258. 5, 3,
  259. 0, 4,
  260. 3, 2
  261. };
  262. static const Int g8DeltaTable[] = { -1,0,1,1,-1,0,1,1 };
  263. static const Int g_Index9Table[] = {
  264. 3,5,4,5,5,1,3,5,4,
  265. 1,3,3,4,6,3,5,7,7,
  266. };
  267. static const Int g9CodeTable[] = {
  268. 9,
  269. 2, 3,
  270. 0, 5,
  271. 2, 4,
  272. 1, 5,
  273. 2, 5,
  274. 1, 1,
  275. 3, 3,
  276. 3, 5,
  277. 3, 4,
  278. 9,
  279. 1, 1,
  280. 1, 3,
  281. 2, 3,
  282. 1, 4,
  283. 1, 6,
  284. 3, 3,
  285. 1, 5,
  286. 0, 7,
  287. 1, 7,
  288. };
  289. static const Int g9DeltaTable[] = { 2,2,1,1,-1,-2,-2,-2,-3 };
  290. // Alphabet size = 12
  291. static const Int g_Index12Table[] = { // index12 is the most critical symbol
  292. 5,6,7,7,5,3,5,1,5,4,5,3,
  293. 4,5,6,6,4,3,5,2,3,3,5,3,
  294. 2,3,7,7,5,3,7,3,3,3,7,4,
  295. 3,2,7,5,5,3,7,3,5,3,6,3,
  296. 3,1,7,4,7,3,8,4,7,4,8,5,
  297. };
  298. static const Int g12CodeTable[] = {
  299. 12,
  300. 1, 5,
  301. 1, 6,
  302. 0, 7,
  303. 1, 7,
  304. 4, 5,
  305. 2, 3,
  306. 5, 5,
  307. 1, 1,
  308. 6, 5,
  309. 1, 4,
  310. 7, 5,
  311. 3, 3,
  312. 12,
  313. 2, 4,
  314. 2, 5,
  315. 0, 6,
  316. 1, 6,
  317. 3, 4,
  318. 2, 3,
  319. 3, 5,
  320. 3, 2,
  321. 3, 3,
  322. 4, 3,
  323. 1, 5,
  324. 5, 3,
  325. 12,
  326. 3, 2,
  327. 1, 3,
  328. 0, 7,
  329. 1, 7,
  330. 1, 5,
  331. 2, 3,
  332. 2, 7,
  333. 3, 3,
  334. 4, 3,
  335. 5, 3,
  336. 3, 7,
  337. 1, 4,
  338. 12,
  339. 1, 3,
  340. 3, 2,
  341. 0, 7,
  342. 1, 5,
  343. 2, 5,
  344. 2, 3,
  345. 1, 7,
  346. 3, 3,
  347. 3, 5,
  348. 4, 3,
  349. 1, 6,
  350. 5, 3,
  351. 12,
  352. 2, 3,
  353. 1, 1,
  354. 1, 7,
  355. 1, 4,
  356. 2, 7,
  357. 3, 3,
  358. 0, 8,
  359. 2, 4,
  360. 3, 7,
  361. 3, 4,
  362. 1, 8,
  363. 1, 5
  364. };
  365. static const Int g12DeltaTable[] = {
  366. 1, 1, 1, 1, 1, 0, 0,-1, 2, 1, 0, 0,
  367. 2, 2,-1,-1,-1, 0,-2,-1, 0, 0,-2,-1,
  368. -1, 1, 0, 2, 0, 0, 0, 0,-2, 0, 1, 1,
  369. 0, 1, 0, 1,-2, 0,-1,-1,-2,-1,-2,-2
  370. };
  371. /**********************************************************************
  372. Adapt fixed length codes based on discriminant
  373. **********************************************************************/
  374. static const Int THRESHOLD = 8;
  375. static const Int MEMORY = 8;
  376. Void AdaptDiscriminant (CAdaptiveHuffman *pAdHuff)
  377. {
  378. Int iSym = pAdHuff->m_iNSymbols, t, dL, dH;
  379. const Int *pCodes, *pDelta = NULL;
  380. Bool bChange = FALSE;
  381. static const Int gMaxTables[] = { 0,0,0,0, 1,2, 4,2, 2,2, 0,0,5 };
  382. static const Int gSecondDisc[]= { 0,0,0,0, 0,0, 1,0, 0,0, 0,0,1 };
  383. if (!pAdHuff->m_bInitialize) {
  384. pAdHuff->m_bInitialize = 1;
  385. pAdHuff->m_iDiscriminant = pAdHuff->m_iDiscriminant1 = 0;
  386. pAdHuff->m_iTableIndex = gSecondDisc[iSym];//(gMaxTables[iSym] - 1) >> 1;
  387. }
  388. dL = dH = pAdHuff->m_iDiscriminant;
  389. if (gSecondDisc[iSym]) {
  390. dH = pAdHuff->m_iDiscriminant1;
  391. }
  392. if (dL < pAdHuff->m_iLowerBound) {
  393. pAdHuff->m_iTableIndex--;
  394. bChange = TRUE;
  395. }
  396. else if (dH > pAdHuff->m_iUpperBound) {
  397. pAdHuff->m_iTableIndex++;
  398. bChange = TRUE;
  399. }
  400. if (bChange) {
  401. /** if initialization is fixed, we can exit on !bChange **/
  402. pAdHuff->m_iDiscriminant = 0;
  403. pAdHuff->m_iDiscriminant1 = 0;
  404. }
  405. {
  406. if (pAdHuff->m_iDiscriminant < -THRESHOLD * MEMORY)
  407. pAdHuff->m_iDiscriminant = -THRESHOLD * MEMORY;
  408. else if (pAdHuff->m_iDiscriminant > THRESHOLD * MEMORY)
  409. pAdHuff->m_iDiscriminant = THRESHOLD * MEMORY;
  410. if (pAdHuff->m_iDiscriminant1 < -THRESHOLD * MEMORY)
  411. pAdHuff->m_iDiscriminant1 = -THRESHOLD * MEMORY;
  412. else if (pAdHuff->m_iDiscriminant1 > THRESHOLD * MEMORY)
  413. pAdHuff->m_iDiscriminant1 = THRESHOLD * MEMORY;
  414. }
  415. t = pAdHuff->m_iTableIndex;
  416. assert (t >= 0);
  417. assert (t < gMaxTables[iSym]);
  418. //pAdHuff->m_iDiscriminant >>= 1;
  419. pAdHuff->m_iLowerBound = (t == 0) ? (-1 << 31) : -THRESHOLD;
  420. pAdHuff->m_iUpperBound = (t == gMaxTables[iSym] - 1) ? (1 << 30) : THRESHOLD;
  421. switch (iSym) {
  422. case 4:
  423. pCodes = g4CodeTable;
  424. pAdHuff->m_hufDecTable = (short *) g4HuffLookupTable;
  425. break;
  426. case 5:
  427. pCodes = g5CodeTable + (iSym * 2 + 1) * t;
  428. pDelta = g5DeltaTable;
  429. pAdHuff->m_hufDecTable = g5HuffLookupTable[t];
  430. break;
  431. case 6:
  432. pCodes = g6CodeTable + (iSym * 2 + 1) * t;
  433. pAdHuff->m_pDelta1 = g6DeltaTable + iSym * (t - (t + 1 == gMaxTables[iSym]));
  434. pDelta = g6DeltaTable + (t - 1 + (t == 0)) * iSym;
  435. pAdHuff->m_hufDecTable = g6HuffLookupTable[t];
  436. break;
  437. case 7:
  438. pCodes = g7CodeTable + (iSym * 2 + 1) * t;
  439. pDelta = g7DeltaTable;
  440. pAdHuff->m_hufDecTable = g7HuffLookupTable[t];
  441. break;
  442. case 8:
  443. //printf ("%d ", t);
  444. pCodes = g8CodeTable;// + (iSym * 2 + 1) * t;
  445. //pDelta = g8DeltaTable;
  446. pAdHuff->m_hufDecTable = g8HuffLookupTable[0];
  447. break;
  448. case 9:
  449. pCodes = g9CodeTable + (iSym * 2 + 1) * t;
  450. pDelta = g9DeltaTable;
  451. pAdHuff->m_hufDecTable = g9HuffLookupTable[t];
  452. break;
  453. case 12:
  454. pCodes = g12CodeTable + (iSym * 2 + 1) * t;
  455. pAdHuff->m_pDelta1 = g12DeltaTable + iSym * (t - (t + 1 == gMaxTables[iSym]));
  456. pDelta = g12DeltaTable + (t - 1 + (t == 0)) * iSym;
  457. pAdHuff->m_hufDecTable = g12HuffLookupTable[t];
  458. break;
  459. default:
  460. assert (0); // undefined fixed length table
  461. return;
  462. }
  463. pAdHuff->m_pTable = pCodes;
  464. pAdHuff->m_pDelta = pDelta;
  465. }