PageRenderTime 68ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 1ms

/org/xiph/fvorbis/CodeBook.hx

https://github.com/kenmcd/ffmp3
Haxe | 634 lines | 503 code | 37 blank | 94 comment | 135 complexity | b6c37df0f0b65df9d097c50e39caa905 MD5 | raw file
Possible License(s): LGPL-2.1
  1. package org.xiph.fvorbis;
  2. import org.xiph.system.Bytes;
  3. import flash.Vector;
  4. import org.xiph.fogg.Buffer;
  5. class CodeBook {
  6. /*
  7. * generated source for CodeBook
  8. */
  9. public var dim : Int;
  10. var entries : Int;
  11. // discarded initializer: 'StaticCodeBook()';
  12. var c : StaticCodeBook;
  13. var valuelist : Vector<Float>;
  14. var codelist : Vector<Int>;
  15. var decode_tree : DecodeAux;
  16. // modifiers:
  17. function encode(a : Int, b : Buffer) : Int {
  18. b.write(codelist[a], c.lengthlist[a]);
  19. return c.lengthlist[a];
  20. }
  21. // modifiers:
  22. function errorv(a : Vector<Float>) : Int {
  23. var best : Int = best(a, 1);
  24. // for-while;
  25. var k : Int = 0;
  26. while (k < dim) {
  27. a[k] = valuelist[(best * dim) + k];
  28. k++;
  29. };
  30. return best;
  31. }
  32. // modifiers:
  33. function encodev(best : Int, a : Vector<Float>, b : Buffer) : Int {
  34. // for-while;
  35. var k : Int = 0;
  36. while (k < dim) {
  37. a[k] = valuelist[(best * dim) + k];
  38. k++;
  39. };
  40. return encode(best, b);
  41. }
  42. // modifiers:
  43. function encodevs(a : Vector<Float>, b : Buffer, step : Int, addmul : Int) : Int {
  44. var best : Int = besterror(a, step, addmul);
  45. return encode(best, b);
  46. }
  47. // discarded initializer: 'new int[15]';
  48. private var t : Vector<Int>;
  49. // modifiers: synchronized
  50. public function decodevs_add(a : Vector<Float>, offset : Int, b : Buffer, n : Int) : Int {
  51. var step : UInt = Std.int(n / dim);
  52. var entry : Int;
  53. var i : UInt;
  54. var j : UInt;
  55. var o : Int;
  56. if (t.length < step) {
  57. //t = new int[step];
  58. t = new Vector(step);
  59. };
  60. // for-while;
  61. i = 0;
  62. while (i < step) {
  63. entry = decode(b);
  64. if (entry == -1) {
  65. return -1;
  66. };
  67. t[i] = (entry * dim);
  68. i++;
  69. };
  70. // for-while;
  71. i = 0;
  72. o = 0;
  73. while (i < cast(dim,UInt)) {
  74. // for-while;
  75. j = 0;
  76. while (j < step) {
  77. a[(offset + o) + j] += valuelist[t[j] + i];
  78. j++;
  79. };
  80. i++; o += step;
  81. };
  82. return 0;
  83. }
  84. // modifiers:
  85. public function decodev_add(a : Vector<Float>, offset : Int, b : Buffer, n : Int) : Int {
  86. var i : Int;
  87. var j : Int;
  88. var entry : Int;
  89. var t : Int;
  90. if (dim > 8) {
  91. // for-while;
  92. i = 0;
  93. while (i < n) {
  94. entry = decode(b);
  95. if (entry == -1) {
  96. return -1;
  97. };
  98. t = (entry * dim);
  99. // for-while;
  100. j = 0;
  101. while (j < dim) {
  102. // HAXE200BUG
  103. // a[offset + i++] += valuelist[t + j++];
  104. a[offset + i] += valuelist[t + j];
  105. i++; j++;
  106. };
  107. };
  108. }
  109. else {
  110. // for-while;
  111. i = 0;
  112. while (i < n) {
  113. entry = decode(b);
  114. if (entry == -1) {
  115. return -1;
  116. };
  117. t = (entry * dim);
  118. j = 0;
  119. {
  120. // FIXME: why isn't this a loop, but some voodoo
  121. // sideeffects-and-oh-wait-a-switch?!
  122. var fall : Bool = false;
  123. if (dim == 8) {
  124. // HAXE200BUG
  125. // a[offset + i++] += valuelist[t + j++];
  126. a[offset + i] += valuelist[t + j];
  127. i++; j++;
  128. fall = true;
  129. };
  130. if (fall || dim == 7) {
  131. // HAXE200BUG
  132. // a[offset + i++] += valuelist[t + j++];
  133. a[offset + i] += valuelist[t + j];
  134. i++; j++;
  135. fall = true;
  136. };
  137. if (fall || dim == 6) {
  138. // HAXE200BUG
  139. // a[offset + i++] += valuelist[t + j++];
  140. a[offset + i] += valuelist[t + j];
  141. i++; j++;
  142. fall = true;
  143. };
  144. if (fall || dim == 5) {
  145. // HAXE200BUG
  146. // a[offset + i++] += valuelist[t + j++];
  147. a[offset + i] += valuelist[t + j];
  148. i++; j++;
  149. fall = true;
  150. };
  151. if (fall || dim == 4) {
  152. // HAXE200BUG
  153. // a[offset + i++] += valuelist[t + j++];
  154. a[offset + i] += valuelist[t + j];
  155. i++; j++;
  156. fall = true;
  157. };
  158. if (fall || dim == 3) {
  159. // HAXE200BUG
  160. // a[offset + i++] += valuelist[t + j++];
  161. a[offset + i] += valuelist[t + j];
  162. i++; j++;
  163. fall = true;
  164. };
  165. if (fall || dim == 2) {
  166. // HAXE200BUG
  167. // a[offset + i++] += valuelist[t + j++];
  168. a[offset + i] += valuelist[t + j];
  169. i++; j++;
  170. fall = true;
  171. };
  172. if (fall || dim == 1) {
  173. // HAXE200BUG
  174. // a[offset + i++] += valuelist[t + j++];
  175. a[offset + i] += valuelist[t + j];
  176. i++; j++;
  177. };
  178. /*
  179. if dim == 0 {
  180. };
  181. */
  182. };
  183. };
  184. };
  185. return 0;
  186. }
  187. // modifiers:
  188. public function decodev_set(a : Vector<Float>, offset : Int, b : Buffer, n : Int) : Int {
  189. var i : Int;
  190. var j : Int;
  191. var entry : Int;
  192. var t : Int;
  193. // for-while;
  194. i = 0;
  195. while (i < n) {
  196. entry = decode(b);
  197. if (entry == -1) {
  198. return -1;
  199. };
  200. t = (entry * dim);
  201. // for-while;
  202. j = 0;
  203. while (j < dim) {
  204. a[offset + i++] = valuelist[t + j++];
  205. };
  206. };
  207. return 0;
  208. }
  209. // modifiers:
  210. public function decodevv_add(a : Array<Vector<Float>>, offset : Int, ch : Int, b : Buffer, n : Int) : Int {
  211. var i : Int;
  212. var j : Int;
  213. var k : Int;
  214. var entry : Int;
  215. var chptr : Int = 0;
  216. // for-while;
  217. i = Std.int(offset / ch);
  218. while (i < ((offset + n) / ch)) {
  219. entry = decode(b);
  220. if (entry == -1) {
  221. return -1;
  222. };
  223. var t : Int = entry * dim;
  224. // for-while;
  225. j = 0;
  226. while (j < dim) {
  227. // HAXE200BUG
  228. // haXe 2.00 has a bug causing the following code line
  229. // to increment chptr twice (because of the "+=" operator?):
  230. // a[chptr++][i] += valuelist[t + j];
  231. a[chptr][i] += valuelist[t + j];
  232. chptr++;
  233. if (chptr == ch) {
  234. chptr = 0;
  235. i++;
  236. };
  237. j++;
  238. };
  239. };
  240. return 0;
  241. }
  242. // modifiers:
  243. public function decode(b : Buffer) : Int {
  244. var ptr : Int = 0;
  245. var t : DecodeAux = decode_tree;
  246. var lok : Int = b.look(t.tabn);
  247. if (lok >= 0) {
  248. ptr = t.tab[lok];
  249. b.adv(t.tabl[lok]);
  250. if (ptr <= 0) {
  251. return -ptr;
  252. };
  253. };
  254. do {
  255. {
  256. var swval = b.read1();
  257. if (swval == 0) {
  258. ptr = t.ptr0[ptr];
  259. } else if (swval == 1) {
  260. ptr = t.ptr1[ptr];
  261. } else {
  262. return -1;
  263. };
  264. };
  265. } while (ptr > 0);
  266. return -ptr;
  267. }
  268. // modifiers:
  269. public function decodevs(a : Vector<Float>, index : Int, b : Buffer, step : Int, addmul : Int) : Int {
  270. var entry : Int = decode(b);
  271. if (entry == -1) {
  272. return -1;
  273. };
  274. switch (addmul) {
  275. case -1:
  276. // for-while;
  277. var i : Int = 0;
  278. var o : Int = 0;
  279. while (i < dim) {
  280. a[index + o] = valuelist[(entry * dim) + i];
  281. i++; o += step;
  282. };
  283. case 0:
  284. // for-while;
  285. var i : Int = 0;
  286. var o : Int = 0;
  287. while (i < dim) {
  288. a[index + o] += valuelist[(entry * dim) + i];
  289. i++; o += step;
  290. };
  291. case 1:
  292. // for-while;
  293. var i : Int = 0;
  294. var o : Int = 0;
  295. while (i < dim) {
  296. a[index + o] *= valuelist[(entry * dim) + i];
  297. i++; o += step;
  298. };
  299. default:
  300. trace("CodeBook.decodeves: addmul=" + addmul);
  301. };
  302. return entry;
  303. }
  304. // modifiers:
  305. function best(a : Vector<Float>, step : Int) : Int {
  306. var nt : EncodeAuxNearestMatch = c.nearest_tree;
  307. var tt : EncodeAuxThreshMatch = c.thresh_tree;
  308. var ptr : Int = 0;
  309. if (tt != null) {
  310. var index : Int = 0;
  311. // for-while;
  312. var k : Int = 0;
  313. var o : Int = step * (dim - 1);
  314. while (k < dim) {
  315. var i : Int;
  316. // for-while;
  317. i = 0;
  318. while (i < (tt.threshvals - 1)) {
  319. if (a[o] < tt.quantthresh[i]) {
  320. break;
  321. };
  322. i++;
  323. };
  324. index = ((index * tt.quantvals) + tt.quantmap[i]);
  325. k++; o -= step;
  326. };
  327. if (c.lengthlist[index] > 0) {
  328. return index;
  329. };
  330. };
  331. if (nt != null) {
  332. while (true) {
  333. var c : Float = 0.;
  334. var p : Int = nt.p[ptr];
  335. var q : Int = nt.q[ptr];
  336. // for-while;
  337. var k : Int = 0;
  338. var o : Int = 0;
  339. while (k < dim) {
  340. c += ((valuelist[p + k] - valuelist[q + k]) * (a[o] - ((valuelist[p + k] + valuelist[q + k]) * 0.5)));
  341. k++; o += step;
  342. };
  343. if (c > 0.) {
  344. ptr = -nt.ptr0[ptr];
  345. }
  346. else {
  347. ptr = -nt.ptr1[ptr];
  348. };
  349. if (ptr <= 0) {
  350. break;
  351. };
  352. };
  353. return -ptr;
  354. };
  355. var besti : Int = -1;
  356. var best : Float = 0.;
  357. var e : Int = 0;
  358. // for-while;
  359. var i : Int = 0;
  360. while (i < entries) {
  361. if (c.lengthlist[i] > 0) {
  362. var _this : Float = CodeBook.dist(dim, valuelist, e, a, step);
  363. if ((besti == -1) || (_this < best)) {
  364. best = _this;
  365. besti = i;
  366. };
  367. };
  368. e += dim;
  369. i++;
  370. };
  371. return besti;
  372. }
  373. // modifiers:
  374. function besterror(a : Vector<Float>, step : Int, addmul : Int) : Int {
  375. var best : Int = best(a, step);
  376. switch (addmul) {
  377. case 0:
  378. // for-while;
  379. var i : Int = 0;
  380. var o : Int = 0;
  381. while (i < dim) {
  382. a[o] -= valuelist[(best * dim) + i];
  383. i++; o += step;
  384. };
  385. case 1:
  386. // for-while;
  387. var i : Int = 0;
  388. var o : Int = 0;
  389. while (i < dim) {
  390. var val : Float = valuelist[(best * dim) + i];
  391. if (val == 0) {
  392. a[o] = 0;
  393. }
  394. else {
  395. a[o] /= val;
  396. };
  397. i++; o += step;
  398. };
  399. };
  400. return best;
  401. }
  402. // modifiers:
  403. function clear() : Void {
  404. }
  405. // modifiers: static, private
  406. static private function dist(el : Int, ref : Vector<Float>, index : Int, b : Vector<Float>, step : Int) : Float {
  407. var acc : Float = 0.;
  408. // for-while;
  409. var i : Int = 0;
  410. while (i < el) {
  411. var val : Float = ref[index + i] - b[i * step];
  412. acc += (val * val);
  413. i++;
  414. };
  415. return acc;
  416. }
  417. // modifiers:
  418. public function init_decode(s : StaticCodeBook) : Int {
  419. c = s;
  420. entries = s.entries;
  421. dim = s.dim;
  422. valuelist = s.unquantize();
  423. decode_tree = make_decode_tree();
  424. if (decode_tree == null) {
  425. clear();
  426. return -1;
  427. };
  428. return 0;
  429. }
  430. // modifiers: static
  431. static function make_words(l : Vector<Int>, n : Int) : Vector<Int> {
  432. //var marker : Vector<Int> = new int[33];
  433. var marker : Vector<Int> = new Vector(33, true);
  434. //var r : Vector<Int> = new int[n];
  435. var r : Vector<Int> = new Vector(n, true);
  436. // for-while;
  437. var i : Int = 0;
  438. while (i < n) {
  439. var length : Int = l[i];
  440. if (length > 0) {
  441. var entry : Int = marker[length];
  442. if ((length < 32) && ((entry >>> length) != 0)) {
  443. return null;
  444. };
  445. r[i] = entry;
  446. // for-while;
  447. var j : Int = length;
  448. while (j > 0) {
  449. if ((marker[j] & 1) != 0) {
  450. if (j == 1) {
  451. marker[1]++;
  452. }
  453. else {
  454. marker[j] = (marker[j - 1] << 1);
  455. };
  456. break;
  457. };
  458. marker[j]++;
  459. j--;
  460. };
  461. // for-while;
  462. var j : Int = length + 1;
  463. while (j < 33) {
  464. if ((marker[j] >>> 1) == entry) {
  465. entry = marker[j];
  466. marker[j] = (marker[j - 1] << 1);
  467. }
  468. else {
  469. break;
  470. };
  471. j++;
  472. };
  473. };
  474. i++;
  475. };
  476. // for-while;
  477. var i : Int = 0;
  478. while (i < n) {
  479. var temp : Int = 0;
  480. // for-while;
  481. var j : Int = 0;
  482. while (j < l[i]) {
  483. temp <<= 1;
  484. temp |= ((r[i] >>> j) & 1);
  485. j++;
  486. };
  487. r[i] = temp;
  488. i++;
  489. };
  490. return r;
  491. }
  492. // modifiers:
  493. function make_decode_tree() : DecodeAux {
  494. var top : Int = 0;
  495. var t : DecodeAux = new DecodeAux();
  496. //var ptr0 : Vector<Int> = t.ptr0 = new int[entries * 2];
  497. var ptr0 : Vector<Int> = t.ptr0 = new Vector(entries * 2, true);
  498. //var ptr1 : Vector<Int> = t.ptr1 = new int[entries * 2];
  499. var ptr1 : Vector<Int> = t.ptr1 = new Vector(entries * 2, true);
  500. var codelist : Vector<Int> = make_words(c.lengthlist, c.entries);
  501. if (codelist == null) {
  502. return null;
  503. };
  504. t.aux = (entries * 2);
  505. // for-while;
  506. var i : Int = 0;
  507. while (i < entries) {
  508. if (c.lengthlist[i] > 0) {
  509. var ptr : Int = 0;
  510. var j : Int;
  511. // for-while;
  512. j = 0;
  513. while (j < (c.lengthlist[i] - 1)) {
  514. var bit : Int = (codelist[i] >>> j) & 1;
  515. if (bit == 0) {
  516. if (ptr0[ptr] == 0) {
  517. ptr0[ptr] = ++top;
  518. };
  519. ptr = ptr0[ptr];
  520. }
  521. else {
  522. if (ptr1[ptr] == 0) {
  523. ptr1[ptr] = ++top;
  524. };
  525. ptr = ptr1[ptr];
  526. };
  527. j++;
  528. };
  529. if (((codelist[i] >>> j) & 1) == 0) {
  530. ptr0[ptr] = -i;
  531. }
  532. else {
  533. ptr1[ptr] = -i;
  534. };
  535. };
  536. i++;
  537. };
  538. t.tabn = (ilog(entries) - 4);
  539. if (t.tabn < 5) {
  540. t.tabn = 5;
  541. };
  542. var n : Int = 1 << t.tabn;
  543. //t.tab = new int[n];
  544. t.tab = new Vector(n, true);
  545. //t.tabl = new int[n];
  546. t.tabl = new Vector(n, true);
  547. // for-while;
  548. var i : Int = 0;
  549. while (i < n) {
  550. var p : Int = 0;
  551. var j : Int = 0;
  552. // for-while;
  553. j = 0;
  554. while ((j < t.tabn) && ((p > 0) || (j == 0))) {
  555. if ((i & (1 << j)) != 0) {
  556. p = ptr1[p];
  557. }
  558. else {
  559. p = ptr0[p];
  560. };
  561. j++;
  562. };
  563. t.tab[i] = p;
  564. t.tabl[i] = j;
  565. i++;
  566. };
  567. return t;
  568. }
  569. // modifiers: static, private
  570. static private function ilog(v : Int) : Int {
  571. var ret : Int = 0;
  572. while (v != 0) {
  573. ret++;
  574. v >>>= 1;
  575. };
  576. return ret;
  577. }
  578. public function new() {
  579. // discarded initializer: 'StaticCodeBook()';
  580. c = new StaticCodeBook();
  581. // discarded initializer: 'new int[15]';
  582. t = new Vector(15, true);
  583. }
  584. }
  585. class DecodeAux {
  586. /*
  587. * generated source for DecodeAux
  588. */
  589. public var tab : Vector<Int>;
  590. public var tabl : Vector<Int>;
  591. public var tabn : Int;
  592. public var ptr0 : Vector<Int>;
  593. public var ptr1 : Vector<Int>;
  594. public var aux : Int;
  595. public function new() {
  596. }
  597. }